[Fortran] gfortran.texi - minor style cleanup
[gcc.git] / gcc / c / c-decl.c
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2020 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
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. */
62 enum decl_context
63 { NORMAL, /* Ordinary declaration */
64 FUNCDEF, /* Function definition */
65 PARM, /* Declaration of parm before function body */
66 FIELD, /* Declaration inside struct or union */
67 TYPENAME}; /* Typename (inside cast or sizeof) */
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
74 enum deprecated_states {
75 DEPRECATED_NORMAL,
76 DEPRECATED_SUPPRESS
77 };
78
79 \f
80 /* Nonzero if we have seen an invalid cross reference
81 to a struct, union, or enum, but not yet printed the message. */
82 tree pending_invalid_xref;
83
84 /* File and line to appear in the eventual error message. */
85 location_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
91 static location_t current_function_prototype_locus;
92
93 /* Whether this prototype was built-in. */
94
95 static bool current_function_prototype_built_in;
96
97 /* The argument type information of this prototype. */
98
99 static tree current_function_prototype_arg_types;
100
101 /* The argument information structure for the function currently being
102 defined. */
103
104 static 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. */
109 struct obstack parser_obstack;
110
111 /* The current statement tree. */
112
113 static GTY(()) struct stmt_tree_s c_stmt_tree;
114
115 /* State saving variables. */
116 tree c_break_label;
117 tree c_cont_label;
118
119 /* A list of decls to be made automatically visible in each file scope. */
120 static 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
125 int 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
130 int 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
135 int 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
140 static 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
145 static bool undef_nested_function;
146
147 /* If non-zero, implicit "omp declare target" attribute is added into the
148 attribute lists. */
149 int current_omp_declare_target_attribute;
150 \f
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
187 struct GTY((chain_next ("%h.prev"))) c_binding {
188 union GTY(()) { /* first so GTY desc can use decl */
189 tree GTY((tag ("0"))) type; /* the type in this scope */
190 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
191 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
192 tree decl; /* the decl bound */
193 tree id; /* the identifier it's bound to */
194 struct c_binding *prev; /* the previous decl in this scope */
195 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
196 unsigned int depth : 28; /* depth of this scope */
197 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
198 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
199 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
200 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
201 location_t locus; /* location for nested bindings */
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
212 struct GTY(()) lang_identifier {
213 struct c_common_identifier common_id;
214 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
215 struct c_binding *tag_binding; /* struct/union/enum tags */
216 struct c_binding *label_binding; /* labels */
217 };
218
219 /* Validate c-lang.c's assumptions. */
220 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
221 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
222
223 /* The binding oracle; see c-tree.h. */
224 void (*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
231 static inline struct c_binding* *
232 i_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
260 static inline struct c_binding **
261 i_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
289 static inline struct c_binding **
290 i_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
315 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
316 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
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
330 struct GTY(()) c_spot_bindings {
331 /* The currently open scope which holds bindings defined when the
332 label was defined or the goto statement was found. */
333 struct c_scope *scope;
334 /* The bindings in the scope field which were defined at the point
335 of the label or goto. This lets us look at older or newer
336 bindings in the scope, as appropriate. */
337 struct c_binding *bindings_in_scope;
338 /* The number of statement expressions that have started since this
339 label or goto statement was defined. This is zero if we are at
340 the same statement expression level. It is positive if we are in
341 a statement expression started since this spot. It is negative
342 if this spot was in a statement expression and we have left
343 it. */
344 int stmt_exprs;
345 /* Whether we started in a statement expression but are no longer in
346 it. This is set to true if stmt_exprs ever goes negative. */
347 bool left_stmt_expr;
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
354 struct GTY(()) c_goto_bindings {
355 /* The location of the goto statement. */
356 location_t loc;
357 /* The bindings of the goto statement. */
358 struct c_spot_bindings goto_bindings;
359 };
360
361 typedef 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
366 struct GTY(()) c_label_vars {
367 /* The shadowed c_label_vars, when one label shadows another (which
368 can only happen using a __label__ declaration). */
369 struct c_label_vars *shadowed;
370 /* The bindings when the label was defined. */
371 struct c_spot_bindings label_bindings;
372 /* A list of decls that we care about: decls about which we should
373 warn if a goto branches to this label from later in the function.
374 Decls are added to this list as scopes are popped. We only add
375 the decls that matter. */
376 vec<tree, va_gc> *decls_in_scope;
377 /* A list of goto statements to this label. This is only used for
378 goto statements seen before the label was defined, so that we can
379 issue appropriate warnings for them. */
380 vec<c_goto_bindings_p, va_gc> *gotos;
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
428 struct 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
487 static 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
493 static GTY(()) struct c_scope *current_function_scope;
494
495 /* The C file scope. This is reset for each input translation unit. */
496
497 static 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
502 static GTY(()) struct c_scope *external_scope;
503
504 /* A chain of c_scope structures awaiting reuse. */
505
506 static GTY((deletable)) struct c_scope *scope_freelist;
507
508 /* A chain of c_binding structures awaiting reuse. */
509
510 static 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
539 struct 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. */
558 static GTY(()) struct c_inline_static *c_inline_statics;
559
560 /* True means unconditionally make a BLOCK for the next scope pushed. */
561
562 static 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
568 static bool next_is_function_body;
569
570 /* A vector of pointers to c_binding structures. */
571
572 typedef struct c_binding *c_binding_ptr;
573
574 /* Information that we keep for a struct or union while it is being
575 parsed. */
576
577 class c_struct_parse_info
578 {
579 public:
580 /* If warn_cxx_compat, a list of types defined within this
581 struct. */
582 auto_vec<tree> struct_types;
583 /* If warn_cxx_compat, a list of field names which have bindings,
584 and which are defined in this struct, but which are not defined
585 in any enclosing struct. This is used to clear the in_struct
586 field of the c_bindings structure. */
587 auto_vec<c_binding_ptr> fields;
588 /* If warn_cxx_compat, a list of typedef names used when defining
589 fields in this struct. */
590 auto_vec<tree> typedefs_seen;
591 };
592
593 /* Information for the struct or union currently being parsed, or
594 NULL if not parsing a struct or union. */
595 static class c_struct_parse_info *struct_parse_info;
596
597 /* Forward declarations. */
598 static tree lookup_name_in_scope (tree, struct c_scope *);
599 static tree c_make_fname_decl (location_t, tree, int);
600 static tree grokdeclarator (const struct c_declarator *,
601 struct c_declspecs *,
602 enum decl_context, bool, tree *, tree *, tree *,
603 bool *, enum deprecated_states);
604 static tree grokparms (struct c_arg_info *, bool);
605 static void layout_array_type (tree);
606 static void warn_defaults_to (location_t, int, const char *, ...)
607 ATTRIBUTE_GCC_DIAG(3,4);
608 static const char *header_for_builtin_fn (tree);
609 \f
610 /* T is a statement. Add it to the statement-tree. This is the
611 C/ObjC version--C++ has a slightly different version of this
612 function. */
613
614 tree
615 add_stmt (tree t)
616 {
617 enum tree_code code = TREE_CODE (t);
618
619 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
620 {
621 if (!EXPR_HAS_LOCATION (t))
622 SET_EXPR_LOCATION (t, input_location);
623 }
624
625 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
626 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
627
628 /* Add T to the statement-tree. Non-side-effect statements need to be
629 recorded during statement expressions. */
630 if (!building_stmt_list_p ())
631 push_stmt_list ();
632 append_to_statement_list_force (t, &cur_stmt_list);
633
634 return t;
635 }
636 \f
637 /* Build a pointer type using the default pointer mode. */
638
639 static tree
640 c_build_pointer_type (tree to_type)
641 {
642 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
643 : TYPE_ADDR_SPACE (to_type);
644 machine_mode pointer_mode;
645
646 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
647 pointer_mode = targetm.addr_space.pointer_mode (as);
648 else
649 pointer_mode = c_default_pointer_mode;
650 return build_pointer_type_for_mode (to_type, pointer_mode, false);
651 }
652
653 \f
654 /* Return true if we will want to say something if a goto statement
655 crosses DECL. */
656
657 static bool
658 decl_jump_unsafe (tree decl)
659 {
660 if (error_operand_p (decl))
661 return false;
662
663 /* Don't warn for compound literals. If a goto statement crosses
664 their initialization, it should cross also all the places where
665 the complit is used or where the complit address might be saved
666 into some variable, so code after the label to which goto jumps
667 should not be able to refer to the compound literal. */
668 if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
669 return false;
670
671 /* Always warn about crossing variably modified types. */
672 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
673 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
674 return true;
675
676 /* Otherwise, only warn if -Wgoto-misses-init and this is an
677 initialized automatic decl. */
678 if (warn_jump_misses_init
679 && VAR_P (decl)
680 && !TREE_STATIC (decl)
681 && DECL_INITIAL (decl) != NULL_TREE)
682 return true;
683
684 return false;
685 }
686 \f
687
688 void
689 c_print_identifier (FILE *file, tree node, int indent)
690 {
691 void (*save) (enum c_oracle_request, tree identifier);
692
693 /* Temporarily hide any binding oracle. Without this, calls to
694 debug_tree from the debugger will end up calling into the oracle,
695 making for a confusing debug session. As the oracle isn't needed
696 here for normal operation, it's simplest to suppress it. */
697 save = c_binding_oracle;
698 c_binding_oracle = NULL;
699
700 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
701 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
702 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
703 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
704 {
705 tree rid = ridpointers[C_RID_CODE (node)];
706 indent_to (file, indent + 4);
707 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
708 (void *) rid, IDENTIFIER_POINTER (rid));
709 }
710
711 c_binding_oracle = save;
712 }
713
714 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
715 which may be any of several kinds of DECL or TYPE or error_mark_node,
716 in the scope SCOPE. */
717 static void
718 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
719 bool nested, location_t locus)
720 {
721 struct c_binding *b, **here;
722
723 if (binding_freelist)
724 {
725 b = binding_freelist;
726 binding_freelist = b->prev;
727 }
728 else
729 b = ggc_alloc<c_binding> ();
730
731 b->shadowed = 0;
732 b->decl = decl;
733 b->id = name;
734 b->depth = scope->depth;
735 b->invisible = invisible;
736 b->nested = nested;
737 b->inner_comp = 0;
738 b->in_struct = 0;
739 b->locus = locus;
740
741 b->u.type = NULL;
742
743 b->prev = scope->bindings;
744 scope->bindings = b;
745
746 if (decl_jump_unsafe (decl))
747 scope->has_jump_unsafe_decl = 1;
748
749 if (!name)
750 return;
751
752 switch (TREE_CODE (decl))
753 {
754 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
755 case ENUMERAL_TYPE:
756 case UNION_TYPE:
757 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
758 case VAR_DECL:
759 case FUNCTION_DECL:
760 case TYPE_DECL:
761 case CONST_DECL:
762 case PARM_DECL:
763 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
764
765 default:
766 gcc_unreachable ();
767 }
768
769 /* Locate the appropriate place in the chain of shadowed decls
770 to insert this binding. Normally, scope == current_scope and
771 this does nothing. */
772 while (*here && (*here)->depth > scope->depth)
773 here = &(*here)->shadowed;
774
775 b->shadowed = *here;
776 *here = b;
777 }
778
779 /* Clear the binding structure B, stick it on the binding_freelist,
780 and return the former value of b->prev. This is used by pop_scope
781 and get_parm_info to iterate destructively over all the bindings
782 from a given scope. */
783 static struct c_binding *
784 free_binding_and_advance (struct c_binding *b)
785 {
786 struct c_binding *prev = b->prev;
787
788 memset (b, 0, sizeof (struct c_binding));
789 b->prev = binding_freelist;
790 binding_freelist = b;
791
792 return prev;
793 }
794
795 /* Bind a label. Like bind, but skip fields which aren't used for
796 labels, and add the LABEL_VARS value. */
797 static void
798 bind_label (tree name, tree label, struct c_scope *scope,
799 struct c_label_vars *label_vars)
800 {
801 struct c_binding *b;
802
803 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
804 UNKNOWN_LOCATION);
805
806 scope->has_label_bindings = true;
807
808 b = scope->bindings;
809 gcc_assert (b->decl == label);
810 label_vars->shadowed = b->u.label;
811 b->u.label = label_vars;
812 }
813 \f
814 /* Hook called at end of compilation to assume 1 elt
815 for a file-scope tentative array defn that wasn't complete before. */
816
817 void
818 c_finish_incomplete_decl (tree decl)
819 {
820 if (VAR_P (decl))
821 {
822 tree type = TREE_TYPE (decl);
823 if (type != error_mark_node
824 && TREE_CODE (type) == ARRAY_TYPE
825 && !DECL_EXTERNAL (decl)
826 && TYPE_DOMAIN (type) == NULL_TREE)
827 {
828 warning_at (DECL_SOURCE_LOCATION (decl),
829 0, "array %q+D assumed to have one element", decl);
830
831 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
832
833 relayout_decl (decl);
834 }
835 }
836 }
837 \f
838 /* Record that inline function FUNC contains a reference (location
839 LOC) to static DECL (file-scope or function-local according to
840 TYPE). */
841
842 void
843 record_inline_static (location_t loc, tree func, tree decl,
844 enum c_inline_static_type type)
845 {
846 c_inline_static *csi = ggc_alloc<c_inline_static> ();
847 csi->location = loc;
848 csi->function = func;
849 csi->static_decl = decl;
850 csi->type = type;
851 csi->next = c_inline_statics;
852 c_inline_statics = csi;
853 }
854
855 /* Check for references to static declarations in inline functions at
856 the end of the translation unit and diagnose them if the functions
857 are still inline definitions. */
858
859 static void
860 check_inline_statics (void)
861 {
862 struct c_inline_static *csi;
863 for (csi = c_inline_statics; csi; csi = csi->next)
864 {
865 if (DECL_EXTERNAL (csi->function))
866 switch (csi->type)
867 {
868 case csi_internal:
869 pedwarn (csi->location, 0,
870 "%qD is static but used in inline function %qD "
871 "which is not static", csi->static_decl, csi->function);
872 break;
873 case csi_modifiable:
874 pedwarn (csi->location, 0,
875 "%q+D is static but declared in inline function %qD "
876 "which is not static", csi->static_decl, csi->function);
877 break;
878 default:
879 gcc_unreachable ();
880 }
881 }
882 c_inline_statics = NULL;
883 }
884 \f
885 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
886 for the current state, otherwise set it to uninitialized. */
887
888 static void
889 set_spot_bindings (struct c_spot_bindings *p, bool defining)
890 {
891 if (defining)
892 {
893 p->scope = current_scope;
894 p->bindings_in_scope = current_scope->bindings;
895 }
896 else
897 {
898 p->scope = NULL;
899 p->bindings_in_scope = NULL;
900 }
901 p->stmt_exprs = 0;
902 p->left_stmt_expr = false;
903 }
904
905 /* Update spot bindings P as we pop out of SCOPE. Return true if we
906 should push decls for a label. */
907
908 static bool
909 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
910 {
911 if (p->scope != scope)
912 {
913 /* This label or goto is defined in some other scope, or it is a
914 label which is not yet defined. There is nothing to
915 update. */
916 return false;
917 }
918
919 /* Adjust the spot bindings to refer to the bindings already defined
920 in the enclosing scope. */
921 p->scope = scope->outer;
922 p->bindings_in_scope = p->scope->bindings;
923
924 return true;
925 }
926 \f
927 /* The Objective-C front-end often needs to determine the current scope. */
928
929 void *
930 objc_get_current_scope (void)
931 {
932 return current_scope;
933 }
934
935 /* The following function is used only by Objective-C. It needs to live here
936 because it accesses the innards of c_scope. */
937
938 void
939 objc_mark_locals_volatile (void *enclosing_blk)
940 {
941 struct c_scope *scope;
942 struct c_binding *b;
943
944 for (scope = current_scope;
945 scope && scope != enclosing_blk;
946 scope = scope->outer)
947 {
948 for (b = scope->bindings; b; b = b->prev)
949 objc_volatilize_decl (b->decl);
950
951 /* Do not climb up past the current function. */
952 if (scope->function_body)
953 break;
954 }
955 }
956
957 /* Return true if we are in the global binding level. */
958
959 bool
960 global_bindings_p (void)
961 {
962 return current_scope == file_scope;
963 }
964
965 /* Return true if we're declaring parameters in an old-style function
966 declaration. */
967
968 bool
969 old_style_parameter_scope (void)
970 {
971 /* If processing parameters and there is no function statement list, we
972 * have an old-style function declaration. */
973 return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
974 }
975
976 void
977 keep_next_level (void)
978 {
979 keep_next_level_flag = true;
980 }
981
982 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
983
984 void
985 set_float_const_decimal64 (void)
986 {
987 current_scope->float_const_decimal64 = true;
988 }
989
990 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
991
992 void
993 clear_float_const_decimal64 (void)
994 {
995 current_scope->float_const_decimal64 = false;
996 }
997
998 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
999
1000 bool
1001 float_const_decimal64_p (void)
1002 {
1003 return current_scope->float_const_decimal64;
1004 }
1005
1006 /* Identify this scope as currently being filled with parameters. */
1007
1008 void
1009 declare_parm_level (void)
1010 {
1011 current_scope->parm_flag = true;
1012 }
1013
1014 void
1015 push_scope (void)
1016 {
1017 if (next_is_function_body)
1018 {
1019 /* This is the transition from the parameters to the top level
1020 of the function body. These are the same scope
1021 (C99 6.2.1p4,6) so we do not push another scope structure.
1022 next_is_function_body is set only by store_parm_decls, which
1023 in turn is called when and only when we are about to
1024 encounter the opening curly brace for the function body.
1025
1026 The outermost block of a function always gets a BLOCK node,
1027 because the debugging output routines expect that each
1028 function has at least one BLOCK. */
1029 current_scope->parm_flag = false;
1030 current_scope->function_body = true;
1031 current_scope->keep = true;
1032 current_scope->outer_function = current_function_scope;
1033 current_function_scope = current_scope;
1034
1035 keep_next_level_flag = false;
1036 next_is_function_body = false;
1037
1038 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1039 if (current_scope->outer)
1040 current_scope->float_const_decimal64
1041 = current_scope->outer->float_const_decimal64;
1042 else
1043 current_scope->float_const_decimal64 = false;
1044 }
1045 else
1046 {
1047 struct c_scope *scope;
1048 if (scope_freelist)
1049 {
1050 scope = scope_freelist;
1051 scope_freelist = scope->outer;
1052 }
1053 else
1054 scope = ggc_cleared_alloc<c_scope> ();
1055
1056 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1057 if (current_scope)
1058 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1059 else
1060 scope->float_const_decimal64 = false;
1061
1062 scope->keep = keep_next_level_flag;
1063 scope->outer = current_scope;
1064 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1065
1066 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1067 possible. */
1068 if (current_scope && scope->depth == 0)
1069 {
1070 scope->depth--;
1071 sorry ("GCC supports only %u nested scopes", scope->depth);
1072 }
1073
1074 current_scope = scope;
1075 keep_next_level_flag = false;
1076 }
1077 }
1078
1079 /* This is called when we are leaving SCOPE. For each label defined
1080 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1081 These are the decls whose initialization will be skipped by a goto
1082 later in the function. */
1083
1084 static void
1085 update_label_decls (struct c_scope *scope)
1086 {
1087 struct c_scope *s;
1088
1089 s = scope;
1090 while (s != NULL)
1091 {
1092 if (s->has_label_bindings)
1093 {
1094 struct c_binding *b;
1095
1096 for (b = s->bindings; b != NULL; b = b->prev)
1097 {
1098 struct c_label_vars *label_vars;
1099 struct c_binding *b1;
1100 bool hjud;
1101 unsigned int ix;
1102 struct c_goto_bindings *g;
1103
1104 if (TREE_CODE (b->decl) != LABEL_DECL)
1105 continue;
1106 label_vars = b->u.label;
1107
1108 b1 = label_vars->label_bindings.bindings_in_scope;
1109 if (label_vars->label_bindings.scope == NULL)
1110 hjud = false;
1111 else
1112 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1113 if (update_spot_bindings (scope, &label_vars->label_bindings))
1114 {
1115 /* This label is defined in this scope. */
1116 if (hjud)
1117 {
1118 for (; b1 != NULL; b1 = b1->prev)
1119 {
1120 /* A goto from later in the function to this
1121 label will never see the initialization
1122 of B1, if any. Save it to issue a
1123 warning if needed. */
1124 if (decl_jump_unsafe (b1->decl))
1125 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1126 }
1127 }
1128 }
1129
1130 /* Update the bindings of any goto statements associated
1131 with this label. */
1132 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1133 update_spot_bindings (scope, &g->goto_bindings);
1134 }
1135 }
1136
1137 /* Don't search beyond the current function. */
1138 if (s == current_function_scope)
1139 break;
1140
1141 s = s->outer;
1142 }
1143 }
1144
1145 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1146
1147 static void
1148 set_type_context (tree type, tree context)
1149 {
1150 for (type = TYPE_MAIN_VARIANT (type); type;
1151 type = TYPE_NEXT_VARIANT (type))
1152 TYPE_CONTEXT (type) = context;
1153 }
1154
1155 /* Exit a scope. Restore the state of the identifier-decl mappings
1156 that were in effect when this scope was entered. Return a BLOCK
1157 node containing all the DECLs in this scope that are of interest
1158 to debug info generation. */
1159
1160 tree
1161 pop_scope (void)
1162 {
1163 struct c_scope *scope = current_scope;
1164 tree block, context, p;
1165 struct c_binding *b;
1166
1167 bool functionbody = scope->function_body;
1168 bool keep = functionbody || scope->keep || scope->bindings;
1169
1170 update_label_decls (scope);
1171
1172 /* If appropriate, create a BLOCK to record the decls for the life
1173 of this function. */
1174 block = NULL_TREE;
1175 if (keep)
1176 {
1177 block = make_node (BLOCK);
1178 BLOCK_SUBBLOCKS (block) = scope->blocks;
1179 TREE_USED (block) = 1;
1180
1181 /* In each subblock, record that this is its superior. */
1182 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1183 BLOCK_SUPERCONTEXT (p) = block;
1184
1185 BLOCK_VARS (block) = NULL_TREE;
1186 }
1187
1188 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1189 scope must be set so that they point to the appropriate
1190 construct, i.e. either to the current FUNCTION_DECL node, or
1191 else to the BLOCK node we just constructed.
1192
1193 Note that for tagged types whose scope is just the formal
1194 parameter list for some function type specification, we can't
1195 properly set their TYPE_CONTEXTs here, because we don't have a
1196 pointer to the appropriate FUNCTION_TYPE node readily available
1197 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1198 type nodes get set in `grokdeclarator' as soon as we have created
1199 the FUNCTION_TYPE node which will represent the "scope" for these
1200 "parameter list local" tagged types. */
1201 if (scope->function_body)
1202 context = current_function_decl;
1203 else if (scope == file_scope)
1204 {
1205 tree file_decl
1206 = build_translation_unit_decl (get_identifier (main_input_filename));
1207 context = file_decl;
1208 debug_hooks->register_main_translation_unit (file_decl);
1209 }
1210 else
1211 context = block;
1212
1213 /* Clear all bindings in this scope. */
1214 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1215 {
1216 p = b->decl;
1217 switch (TREE_CODE (p))
1218 {
1219 case LABEL_DECL:
1220 /* Warnings for unused labels, errors for undefined labels. */
1221 if (TREE_USED (p) && !DECL_INITIAL (p))
1222 {
1223 error ("label %q+D used but not defined", p);
1224 DECL_INITIAL (p) = error_mark_node;
1225 }
1226 else
1227 warn_for_unused_label (p);
1228
1229 /* Labels go in BLOCK_VARS. */
1230 DECL_CHAIN (p) = BLOCK_VARS (block);
1231 BLOCK_VARS (block) = p;
1232 gcc_assert (I_LABEL_BINDING (b->id) == b);
1233 I_LABEL_BINDING (b->id) = b->shadowed;
1234
1235 /* Also pop back to the shadowed label_vars. */
1236 release_tree_vector (b->u.label->decls_in_scope);
1237 b->u.label = b->u.label->shadowed;
1238 break;
1239
1240 case ENUMERAL_TYPE:
1241 case UNION_TYPE:
1242 case RECORD_TYPE:
1243 set_type_context (p, context);
1244
1245 /* Types may not have tag-names, in which case the type
1246 appears in the bindings list with b->id NULL. */
1247 if (b->id)
1248 {
1249 gcc_assert (I_TAG_BINDING (b->id) == b);
1250 I_TAG_BINDING (b->id) = b->shadowed;
1251 }
1252 break;
1253
1254 case FUNCTION_DECL:
1255 /* Propagate TREE_ADDRESSABLE from nested functions to their
1256 containing functions. */
1257 if (!TREE_ASM_WRITTEN (p)
1258 && DECL_INITIAL (p) != NULL_TREE
1259 && TREE_ADDRESSABLE (p)
1260 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1261 && DECL_ABSTRACT_ORIGIN (p) != p)
1262 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1263 if (!TREE_PUBLIC (p)
1264 && !DECL_INITIAL (p)
1265 && !b->nested
1266 && scope != file_scope
1267 && scope != external_scope)
1268 {
1269 error ("nested function %q+D declared but never defined", p);
1270 undef_nested_function = true;
1271 }
1272 else if (DECL_DECLARED_INLINE_P (p)
1273 && TREE_PUBLIC (p)
1274 && !DECL_INITIAL (p))
1275 {
1276 /* C99 6.7.4p6: "a function with external linkage... declared
1277 with an inline function specifier ... shall also be defined
1278 in the same translation unit." */
1279 if (!flag_gnu89_inline
1280 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1281 && scope == external_scope)
1282 pedwarn (input_location, 0,
1283 "inline function %q+D declared but never defined", p);
1284 DECL_EXTERNAL (p) = 1;
1285 }
1286
1287 goto common_symbol;
1288
1289 case VAR_DECL:
1290 /* Warnings for unused variables. */
1291 if ((!TREE_USED (p) || !DECL_READ_P (p))
1292 && !TREE_NO_WARNING (p)
1293 && !DECL_IN_SYSTEM_HEADER (p)
1294 && DECL_NAME (p)
1295 && !DECL_ARTIFICIAL (p)
1296 && scope != file_scope
1297 && scope != external_scope)
1298 {
1299 if (!TREE_USED (p))
1300 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1301 else if (DECL_CONTEXT (p) == current_function_decl)
1302 warning_at (DECL_SOURCE_LOCATION (p),
1303 OPT_Wunused_but_set_variable,
1304 "variable %qD set but not used", p);
1305 }
1306
1307 if (b->inner_comp)
1308 {
1309 error ("type of array %q+D completed incompatibly with"
1310 " implicit initialization", p);
1311 }
1312
1313 /* Fall through. */
1314 case TYPE_DECL:
1315 case CONST_DECL:
1316 common_symbol:
1317 /* All of these go in BLOCK_VARS, but only if this is the
1318 binding in the home scope. */
1319 if (!b->nested)
1320 {
1321 DECL_CHAIN (p) = BLOCK_VARS (block);
1322 BLOCK_VARS (block) = p;
1323 }
1324 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1325 {
1326 /* For block local externs add a special
1327 DECL_EXTERNAL decl for debug info generation. */
1328 tree extp = copy_node (p);
1329
1330 DECL_EXTERNAL (extp) = 1;
1331 TREE_STATIC (extp) = 0;
1332 TREE_PUBLIC (extp) = 1;
1333 DECL_INITIAL (extp) = NULL_TREE;
1334 DECL_LANG_SPECIFIC (extp) = NULL;
1335 DECL_CONTEXT (extp) = current_function_decl;
1336 if (TREE_CODE (p) == FUNCTION_DECL)
1337 {
1338 DECL_RESULT (extp) = NULL_TREE;
1339 DECL_SAVED_TREE (extp) = NULL_TREE;
1340 DECL_STRUCT_FUNCTION (extp) = NULL;
1341 }
1342 if (b->locus != UNKNOWN_LOCATION)
1343 DECL_SOURCE_LOCATION (extp) = b->locus;
1344 DECL_CHAIN (extp) = BLOCK_VARS (block);
1345 BLOCK_VARS (block) = extp;
1346 }
1347 /* If this is the file scope set DECL_CONTEXT of each decl to
1348 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1349 work. */
1350 if (scope == file_scope)
1351 {
1352 DECL_CONTEXT (p) = context;
1353 if (TREE_CODE (p) == TYPE_DECL
1354 && TREE_TYPE (p) != error_mark_node)
1355 set_type_context (TREE_TYPE (p), context);
1356 }
1357
1358 gcc_fallthrough ();
1359 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1360 already been put there by store_parm_decls. Unused-
1361 parameter warnings are handled by function.c.
1362 error_mark_node obviously does not go in BLOCK_VARS and
1363 does not get unused-variable warnings. */
1364 case PARM_DECL:
1365 case ERROR_MARK:
1366 /* It is possible for a decl not to have a name. We get
1367 here with b->id NULL in this case. */
1368 if (b->id)
1369 {
1370 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1371 I_SYMBOL_BINDING (b->id) = b->shadowed;
1372 if (b->shadowed && b->shadowed->u.type)
1373 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1374 }
1375 break;
1376
1377 default:
1378 gcc_unreachable ();
1379 }
1380 }
1381
1382
1383 /* Dispose of the block that we just made inside some higher level. */
1384 if ((scope->function_body || scope == file_scope) && context)
1385 {
1386 DECL_INITIAL (context) = block;
1387 BLOCK_SUPERCONTEXT (block) = context;
1388 }
1389 else if (scope->outer)
1390 {
1391 if (block)
1392 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1393 /* If we did not make a block for the scope just exited, any
1394 blocks made for inner scopes must be carried forward so they
1395 will later become subblocks of something else. */
1396 else if (scope->blocks)
1397 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1398 }
1399
1400 /* Pop the current scope, and free the structure for reuse. */
1401 current_scope = scope->outer;
1402 if (scope->function_body)
1403 current_function_scope = scope->outer_function;
1404
1405 memset (scope, 0, sizeof (struct c_scope));
1406 scope->outer = scope_freelist;
1407 scope_freelist = scope;
1408
1409 return block;
1410 }
1411
1412 void
1413 push_file_scope (void)
1414 {
1415 tree decl;
1416
1417 if (file_scope)
1418 return;
1419
1420 push_scope ();
1421 file_scope = current_scope;
1422
1423 start_fname_decls ();
1424
1425 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1426 bind (DECL_NAME (decl), decl, file_scope,
1427 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1428 }
1429
1430 void
1431 pop_file_scope (void)
1432 {
1433 /* In case there were missing closebraces, get us back to the global
1434 binding level. */
1435 while (current_scope != file_scope)
1436 pop_scope ();
1437
1438 /* __FUNCTION__ is defined at file scope (""). This
1439 call may not be necessary as my tests indicate it
1440 still works without it. */
1441 finish_fname_decls ();
1442
1443 check_inline_statics ();
1444
1445 /* This is the point to write out a PCH if we're doing that.
1446 In that case we do not want to do anything else. */
1447 if (pch_file)
1448 {
1449 c_common_write_pch ();
1450 /* Ensure even the callers don't try to finalize the CU. */
1451 flag_syntax_only = 1;
1452 return;
1453 }
1454
1455 /* Pop off the file scope and close this translation unit. */
1456 pop_scope ();
1457 file_scope = 0;
1458
1459 maybe_apply_pending_pragma_weaks ();
1460 }
1461 \f
1462 /* Adjust the bindings for the start of a statement expression. */
1463
1464 void
1465 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1466 {
1467 struct c_scope *scope;
1468
1469 for (scope = current_scope; scope != NULL; scope = scope->outer)
1470 {
1471 struct c_binding *b;
1472
1473 if (!scope->has_label_bindings)
1474 continue;
1475
1476 for (b = scope->bindings; b != NULL; b = b->prev)
1477 {
1478 struct c_label_vars *label_vars;
1479 unsigned int ix;
1480 struct c_goto_bindings *g;
1481
1482 if (TREE_CODE (b->decl) != LABEL_DECL)
1483 continue;
1484 label_vars = b->u.label;
1485 ++label_vars->label_bindings.stmt_exprs;
1486 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1487 ++g->goto_bindings.stmt_exprs;
1488 }
1489 }
1490
1491 if (switch_bindings != NULL)
1492 ++switch_bindings->stmt_exprs;
1493 }
1494
1495 /* Adjust the bindings for the end of a statement expression. */
1496
1497 void
1498 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1499 {
1500 struct c_scope *scope;
1501
1502 for (scope = current_scope; scope != NULL; scope = scope->outer)
1503 {
1504 struct c_binding *b;
1505
1506 if (!scope->has_label_bindings)
1507 continue;
1508
1509 for (b = scope->bindings; b != NULL; b = b->prev)
1510 {
1511 struct c_label_vars *label_vars;
1512 unsigned int ix;
1513 struct c_goto_bindings *g;
1514
1515 if (TREE_CODE (b->decl) != LABEL_DECL)
1516 continue;
1517 label_vars = b->u.label;
1518 --label_vars->label_bindings.stmt_exprs;
1519 if (label_vars->label_bindings.stmt_exprs < 0)
1520 {
1521 label_vars->label_bindings.left_stmt_expr = true;
1522 label_vars->label_bindings.stmt_exprs = 0;
1523 }
1524 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1525 {
1526 --g->goto_bindings.stmt_exprs;
1527 if (g->goto_bindings.stmt_exprs < 0)
1528 {
1529 g->goto_bindings.left_stmt_expr = true;
1530 g->goto_bindings.stmt_exprs = 0;
1531 }
1532 }
1533 }
1534 }
1535
1536 if (switch_bindings != NULL)
1537 {
1538 --switch_bindings->stmt_exprs;
1539 gcc_assert (switch_bindings->stmt_exprs >= 0);
1540 }
1541 }
1542 \f
1543 /* Push a definition or a declaration of struct, union or enum tag "name".
1544 "type" should be the type node.
1545 We assume that the tag "name" is not already defined, and has a location
1546 of LOC.
1547
1548 Note that the definition may really be just a forward reference.
1549 In that case, the TYPE_SIZE will be zero. */
1550
1551 static void
1552 pushtag (location_t loc, tree name, tree type)
1553 {
1554 /* Record the identifier as the type's name if it has none. */
1555 if (name && !TYPE_NAME (type))
1556 TYPE_NAME (type) = name;
1557 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1558
1559 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1560 tagged type we just added to the current scope. This fake
1561 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1562 to output a representation of a tagged type, and it also gives
1563 us a convenient place to record the "scope start" address for the
1564 tagged type. */
1565
1566 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1567 TYPE_DECL, NULL_TREE, type));
1568
1569 /* An approximation for now, so we can tell this is a function-scope tag.
1570 This will be updated in pop_scope. */
1571 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1572
1573 if (warn_cxx_compat && name != NULL_TREE)
1574 {
1575 struct c_binding *b = I_SYMBOL_BINDING (name);
1576
1577 if (b != NULL
1578 && b->decl != NULL_TREE
1579 && TREE_CODE (b->decl) == TYPE_DECL
1580 && (B_IN_CURRENT_SCOPE (b)
1581 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1582 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1583 != TYPE_MAIN_VARIANT (type)))
1584 {
1585 auto_diagnostic_group d;
1586 if (warning_at (loc, OPT_Wc___compat,
1587 ("using %qD as both a typedef and a tag is "
1588 "invalid in C++"), b->decl)
1589 && b->locus != UNKNOWN_LOCATION)
1590 inform (b->locus, "originally defined here");
1591 }
1592 }
1593 }
1594
1595 /* An exported interface to pushtag. This is used by the gdb plugin's
1596 binding oracle to introduce a new tag binding. */
1597
1598 void
1599 c_pushtag (location_t loc, tree name, tree type)
1600 {
1601 pushtag (loc, name, type);
1602 }
1603
1604 /* An exported interface to bind a declaration. LOC is the location
1605 to use. DECL is the declaration to bind. The decl's name is used
1606 to determine how it is bound. If DECL is a VAR_DECL, then
1607 IS_GLOBAL determines whether the decl is put into the global (file
1608 and external) scope or the current function's scope; if DECL is not
1609 a VAR_DECL then it is always put into the file scope. */
1610
1611 void
1612 c_bind (location_t loc, tree decl, bool is_global)
1613 {
1614 struct c_scope *scope;
1615 bool nested = false;
1616
1617 if (!VAR_P (decl) || current_function_scope == NULL)
1618 {
1619 /* Types and functions are always considered to be global. */
1620 scope = file_scope;
1621 DECL_EXTERNAL (decl) = 1;
1622 TREE_PUBLIC (decl) = 1;
1623 }
1624 else if (is_global)
1625 {
1626 /* Also bind it into the external scope. */
1627 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1628 nested = true;
1629 scope = file_scope;
1630 DECL_EXTERNAL (decl) = 1;
1631 TREE_PUBLIC (decl) = 1;
1632 }
1633 else
1634 {
1635 DECL_CONTEXT (decl) = current_function_decl;
1636 TREE_PUBLIC (decl) = 0;
1637 scope = current_function_scope;
1638 }
1639
1640 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1641 }
1642 \f
1643
1644 /* Stores the first FILE*, const struct tm* etc. argument type (whatever it
1645 is) seen in a declaration of a file I/O etc. built-in. Subsequent
1646 declarations of such built-ins are expected to refer to it rather than to
1647 fileptr_type_node etc. which is just void* (or to any other type).
1648 Used only by match_builtin_function_types. */
1649
1650 static GTY(()) tree last_structptr_types[6];
1651
1652 /* Subroutine of compare_decls. Allow harmless mismatches in return
1653 and argument types provided that the type modes match. Set *STRICT
1654 and *ARGNO to the expected argument type and number in case of
1655 an argument type mismatch or null and zero otherwise. Return
1656 a unified type given a suitable match, and 0 otherwise. */
1657
1658 static tree
1659 match_builtin_function_types (tree newtype, tree oldtype,
1660 tree *strict, unsigned *argno)
1661 {
1662 /* Accept the return type of the new declaration if same modes. */
1663 tree oldrettype = TREE_TYPE (oldtype);
1664 tree newrettype = TREE_TYPE (newtype);
1665
1666 *argno = 0;
1667 *strict = NULL_TREE;
1668
1669 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1670 return NULL_TREE;
1671
1672 if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1673 TYPE_MAIN_VARIANT (newrettype)))
1674 *strict = oldrettype;
1675
1676 tree oldargs = TYPE_ARG_TYPES (oldtype);
1677 tree newargs = TYPE_ARG_TYPES (newtype);
1678 tree tryargs = newargs;
1679
1680 gcc_checking_assert ((sizeof (last_structptr_types)
1681 / sizeof (last_structptr_types[0]))
1682 == (sizeof (builtin_structptr_types)
1683 / sizeof (builtin_structptr_types[0])));
1684 for (unsigned i = 1; oldargs || newargs; ++i)
1685 {
1686 if (!oldargs
1687 || !newargs
1688 || !TREE_VALUE (oldargs)
1689 || !TREE_VALUE (newargs))
1690 return NULL_TREE;
1691
1692 tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1693 tree newtype = TYPE_MAIN_VARIANT (TREE_VALUE (newargs));
1694
1695 /* Fail for types with incompatible modes/sizes. */
1696 if (TYPE_MODE (TREE_VALUE (oldargs))
1697 != TYPE_MODE (TREE_VALUE (newargs)))
1698 return NULL_TREE;
1699
1700 /* Fail for function and object pointer mismatches. */
1701 if ((FUNCTION_POINTER_TYPE_P (oldtype)
1702 != FUNCTION_POINTER_TYPE_P (newtype))
1703 || POINTER_TYPE_P (oldtype) != POINTER_TYPE_P (newtype))
1704 return NULL_TREE;
1705
1706 unsigned j = (sizeof (builtin_structptr_types)
1707 / sizeof (builtin_structptr_types[0]));
1708 if (POINTER_TYPE_P (oldtype))
1709 for (j = 0; j < (sizeof (builtin_structptr_types)
1710 / sizeof (builtin_structptr_types[0])); ++j)
1711 {
1712 if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1713 continue;
1714 /* Store the first FILE* etc. argument type (whatever it is), and
1715 expect any subsequent declarations of file I/O etc. built-ins
1716 to refer to it rather than to fileptr_type_node etc. which is
1717 just void* (or const void*). */
1718 if (last_structptr_types[j])
1719 {
1720 if (!comptypes (last_structptr_types[j], newtype))
1721 {
1722 *argno = i;
1723 *strict = last_structptr_types[j];
1724 }
1725 }
1726 else
1727 last_structptr_types[j] = newtype;
1728 break;
1729 }
1730 if (j == (sizeof (builtin_structptr_types)
1731 / sizeof (builtin_structptr_types[0]))
1732 && !*strict
1733 && !comptypes (oldtype, newtype))
1734 {
1735 *argno = i;
1736 *strict = oldtype;
1737 }
1738
1739 oldargs = TREE_CHAIN (oldargs);
1740 newargs = TREE_CHAIN (newargs);
1741 }
1742
1743 tree trytype = build_function_type (newrettype, tryargs);
1744
1745 /* Allow declaration to change transaction_safe attribute. */
1746 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1747 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1748 tree newattrs = TYPE_ATTRIBUTES (newtype);
1749 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1750 if (oldtsafe && !newtsafe)
1751 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1752 else if (newtsafe && !oldtsafe)
1753 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1754 NULL_TREE, oldattrs);
1755
1756 return build_type_attribute_variant (trytype, oldattrs);
1757 }
1758
1759 /* Subroutine of diagnose_mismatched_decls. Check for function type
1760 mismatch involving an empty arglist vs a nonempty one and give clearer
1761 diagnostics. */
1762 static void
1763 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1764 tree newtype, tree oldtype)
1765 {
1766 tree t;
1767
1768 if (TREE_CODE (olddecl) != FUNCTION_DECL
1769 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1770 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1771 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1772 return;
1773
1774 t = TYPE_ARG_TYPES (oldtype);
1775 if (t == NULL_TREE)
1776 t = TYPE_ARG_TYPES (newtype);
1777 for (; t; t = TREE_CHAIN (t))
1778 {
1779 tree type = TREE_VALUE (t);
1780
1781 if (TREE_CHAIN (t) == NULL_TREE
1782 && TYPE_MAIN_VARIANT (type) != void_type_node)
1783 {
1784 inform (input_location, "a parameter list with an ellipsis "
1785 "cannot match an empty parameter name list declaration");
1786 break;
1787 }
1788
1789 if (c_type_promotes_to (type) != type)
1790 {
1791 inform (input_location, "an argument type that has a default "
1792 "promotion cannot match an empty parameter name list "
1793 "declaration");
1794 break;
1795 }
1796 }
1797 }
1798
1799 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1800 old-style function definition, NEWDECL is a prototype declaration.
1801 Diagnose inconsistencies in the argument list. Returns TRUE if
1802 the prototype is compatible, FALSE if not. */
1803 static bool
1804 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1805 {
1806 tree newargs, oldargs;
1807 int i;
1808
1809 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1810
1811 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1812 newargs = TYPE_ARG_TYPES (newtype);
1813 i = 1;
1814
1815 for (;;)
1816 {
1817 tree oldargtype = TREE_VALUE (oldargs);
1818 tree newargtype = TREE_VALUE (newargs);
1819
1820 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1821 return false;
1822
1823 oldargtype = (TYPE_ATOMIC (oldargtype)
1824 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1825 TYPE_QUAL_ATOMIC)
1826 : TYPE_MAIN_VARIANT (oldargtype));
1827 newargtype = (TYPE_ATOMIC (newargtype)
1828 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1829 TYPE_QUAL_ATOMIC)
1830 : TYPE_MAIN_VARIANT (newargtype));
1831
1832 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1833 break;
1834
1835 /* Reaching the end of just one list means the two decls don't
1836 agree on the number of arguments. */
1837 if (END_OF_ARGLIST (oldargtype))
1838 {
1839 error ("prototype for %q+D declares more arguments "
1840 "than previous old-style definition", newdecl);
1841 return false;
1842 }
1843 else if (END_OF_ARGLIST (newargtype))
1844 {
1845 error ("prototype for %q+D declares fewer arguments "
1846 "than previous old-style definition", newdecl);
1847 return false;
1848 }
1849
1850 /* Type for passing arg must be consistent with that declared
1851 for the arg. */
1852 else if (!comptypes (oldargtype, newargtype))
1853 {
1854 error ("prototype for %q+D declares argument %d"
1855 " with incompatible type",
1856 newdecl, i);
1857 return false;
1858 }
1859
1860 oldargs = TREE_CHAIN (oldargs);
1861 newargs = TREE_CHAIN (newargs);
1862 i++;
1863 }
1864
1865 /* If we get here, no errors were found, but do issue a warning
1866 for this poor-style construct. */
1867 warning (0, "prototype for %q+D follows non-prototype definition",
1868 newdecl);
1869 return true;
1870 #undef END_OF_ARGLIST
1871 }
1872
1873 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1874 first in a pair of mismatched declarations, using the diagnostic
1875 function DIAG. */
1876 static void
1877 locate_old_decl (tree decl)
1878 {
1879 if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl)
1880 && !C_DECL_DECLARED_BUILTIN (decl))
1881 ;
1882 else if (DECL_INITIAL (decl))
1883 inform (input_location, "previous definition of %q+D was here", decl);
1884 else if (C_DECL_IMPLICIT (decl))
1885 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1886 else
1887 inform (input_location, "previous declaration of %q+D was here", decl);
1888 }
1889
1890 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1891 Returns true if the caller should proceed to merge the two, false
1892 if OLDDECL should simply be discarded. As a side effect, issues
1893 all necessary diagnostics for invalid or poor-style combinations.
1894 If it returns true, writes the types of NEWDECL and OLDDECL to
1895 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1896 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1897
1898 static bool
1899 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1900 tree *newtypep, tree *oldtypep)
1901 {
1902 tree newtype, oldtype;
1903 bool retval = true;
1904
1905 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1906 && DECL_EXTERNAL (DECL))
1907
1908 /* If we have error_mark_node for either decl or type, just discard
1909 the previous decl - we're in an error cascade already. */
1910 if (olddecl == error_mark_node || newdecl == error_mark_node)
1911 return false;
1912 *oldtypep = oldtype = TREE_TYPE (olddecl);
1913 *newtypep = newtype = TREE_TYPE (newdecl);
1914 if (oldtype == error_mark_node || newtype == error_mark_node)
1915 return false;
1916
1917 /* Two different categories of symbol altogether. This is an error
1918 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1919 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1920 {
1921 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1922 && fndecl_built_in_p (olddecl)
1923 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1924 {
1925 auto_diagnostic_group d;
1926 error ("%q+D redeclared as different kind of symbol", newdecl);
1927 locate_old_decl (olddecl);
1928 }
1929 else if (TREE_PUBLIC (newdecl))
1930 warning (OPT_Wbuiltin_declaration_mismatch,
1931 "built-in function %q+D declared as non-function",
1932 newdecl);
1933 else
1934 warning (OPT_Wshadow, "declaration of %q+D shadows "
1935 "a built-in function", newdecl);
1936 return false;
1937 }
1938
1939 /* Enumerators have no linkage, so may only be declared once in a
1940 given scope. */
1941 if (TREE_CODE (olddecl) == CONST_DECL)
1942 {
1943 auto_diagnostic_group d;
1944 error ("redeclaration of enumerator %q+D", newdecl);
1945 locate_old_decl (olddecl);
1946 return false;
1947 }
1948
1949 bool pedwarned = false;
1950 bool warned = false;
1951 auto_diagnostic_group d;
1952
1953 if (!comptypes (oldtype, newtype))
1954 {
1955 if (TREE_CODE (olddecl) == FUNCTION_DECL
1956 && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
1957 && !C_DECL_DECLARED_BUILTIN (olddecl))
1958 {
1959 /* Accept "harmless" mismatches in function types such
1960 as missing qualifiers or pointer vs same size integer
1961 mismatches. This is for the ffs and fprintf builtins.
1962 However, with -Wextra in effect, diagnose return and
1963 argument types that are incompatible according to
1964 language rules. */
1965 tree mismatch_expect;
1966 unsigned mismatch_argno;
1967
1968 tree trytype = match_builtin_function_types (newtype, oldtype,
1969 &mismatch_expect,
1970 &mismatch_argno);
1971
1972 if (trytype && comptypes (newtype, trytype))
1973 *oldtypep = oldtype = trytype;
1974 else
1975 {
1976 /* If types don't match for a built-in, throw away the
1977 built-in. No point in calling locate_old_decl here, it
1978 won't print anything. */
1979 const char *header = header_for_builtin_fn (olddecl);
1980 location_t loc = DECL_SOURCE_LOCATION (newdecl);
1981 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
1982 "conflicting types for built-in function %q+D; "
1983 "expected %qT",
1984 newdecl, oldtype)
1985 && header)
1986 {
1987 /* Suggest the right header to include as the preferred
1988 solution rather than the spelling of the declaration. */
1989 rich_location richloc (line_table, loc);
1990 maybe_add_include_fixit (&richloc, header, true);
1991 inform (&richloc,
1992 "%qD is declared in header %qs", olddecl, header);
1993 }
1994 return false;
1995 }
1996
1997 if (mismatch_expect && extra_warnings)
1998 {
1999 /* If types match only loosely, print a warning but accept
2000 the redeclaration. */
2001 location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2002 if (mismatch_argno)
2003 warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2004 "mismatch in argument %u type of built-in "
2005 "function %qD; expected %qT",
2006 mismatch_argno, newdecl, mismatch_expect);
2007 else
2008 warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2009 "mismatch in return type of built-in "
2010 "function %qD; expected %qT",
2011 newdecl, mismatch_expect);
2012 }
2013 }
2014 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2015 && DECL_IS_BUILTIN (olddecl))
2016 {
2017 /* A conflicting function declaration for a predeclared
2018 function that isn't actually built in. Objective C uses
2019 these. The new declaration silently overrides everything
2020 but the volatility (i.e. noreturn) indication. See also
2021 below. FIXME: Make Objective C use normal builtins. */
2022 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2023 return false;
2024 }
2025 /* Permit void foo (...) to match int foo (...) if the latter is
2026 the definition and implicit int was used. See
2027 c-torture/compile/920625-2.c. */
2028 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2029 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2030 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2031 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2032 {
2033 pedwarned = pedwarn (input_location, 0,
2034 "conflicting types for %q+D", newdecl);
2035 /* Make sure we keep void as the return type. */
2036 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2037 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2038 }
2039 /* Permit void foo (...) to match an earlier call to foo (...) with
2040 no declared type (thus, implicitly int). */
2041 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2042 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2043 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2044 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2045 {
2046 pedwarned = pedwarn (input_location, 0,
2047 "conflicting types for %q+D", newdecl);
2048 /* Make sure we keep void as the return type. */
2049 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2050 }
2051 else
2052 {
2053 int new_quals = TYPE_QUALS (newtype);
2054 int old_quals = TYPE_QUALS (oldtype);
2055
2056 if (new_quals != old_quals)
2057 {
2058 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2059 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2060 if (new_addr != old_addr)
2061 {
2062 if (ADDR_SPACE_GENERIC_P (new_addr))
2063 error ("conflicting named address spaces (generic vs %s) "
2064 "for %q+D",
2065 c_addr_space_name (old_addr), newdecl);
2066 else if (ADDR_SPACE_GENERIC_P (old_addr))
2067 error ("conflicting named address spaces (%s vs generic) "
2068 "for %q+D",
2069 c_addr_space_name (new_addr), newdecl);
2070 else
2071 error ("conflicting named address spaces (%s vs %s) "
2072 "for %q+D",
2073 c_addr_space_name (new_addr),
2074 c_addr_space_name (old_addr),
2075 newdecl);
2076 }
2077
2078 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2079 != CLEAR_QUAL_ADDR_SPACE (old_quals))
2080 error ("conflicting type qualifiers for %q+D", newdecl);
2081 }
2082 else
2083 error ("conflicting types for %q+D", newdecl);
2084 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
2085 locate_old_decl (olddecl);
2086 return false;
2087 }
2088 }
2089
2090 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2091 but silently ignore the redeclaration if either is in a system
2092 header. (Conflicting redeclarations were handled above.) This
2093 is allowed for C11 if the types are the same, not just
2094 compatible. */
2095 if (TREE_CODE (newdecl) == TYPE_DECL)
2096 {
2097 bool types_different = false;
2098 int comptypes_result;
2099
2100 comptypes_result
2101 = comptypes_check_different_types (oldtype, newtype, &types_different);
2102
2103 if (comptypes_result != 1 || types_different)
2104 {
2105 error ("redefinition of typedef %q+D with different type", newdecl);
2106 locate_old_decl (olddecl);
2107 return false;
2108 }
2109
2110 if (DECL_IN_SYSTEM_HEADER (newdecl)
2111 || DECL_IN_SYSTEM_HEADER (olddecl)
2112 || TREE_NO_WARNING (newdecl)
2113 || TREE_NO_WARNING (olddecl))
2114 return true; /* Allow OLDDECL to continue in use. */
2115
2116 if (variably_modified_type_p (newtype, NULL))
2117 {
2118 error ("redefinition of typedef %q+D with variably modified type",
2119 newdecl);
2120 locate_old_decl (olddecl);
2121 }
2122 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2123 "redefinition of typedef %q+D", newdecl))
2124 locate_old_decl (olddecl);
2125
2126 return true;
2127 }
2128
2129 /* Function declarations can either be 'static' or 'extern' (no
2130 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2131 can never conflict with each other on account of linkage
2132 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2133 gnu89 mode permits two definitions if one is 'extern inline' and
2134 one is not. The non- extern-inline definition supersedes the
2135 extern-inline definition. */
2136
2137 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2138 {
2139 /* If you declare a built-in function name as static, or
2140 define the built-in with an old-style definition (so we
2141 can't validate the argument list) the built-in definition is
2142 overridden, but optionally warn this was a bad choice of name. */
2143 if (fndecl_built_in_p (olddecl)
2144 && !C_DECL_DECLARED_BUILTIN (olddecl))
2145 {
2146 if (!TREE_PUBLIC (newdecl)
2147 || (DECL_INITIAL (newdecl)
2148 && !prototype_p (TREE_TYPE (newdecl))))
2149 {
2150 warning_at (DECL_SOURCE_LOCATION (newdecl),
2151 OPT_Wshadow, "declaration of %qD shadows "
2152 "a built-in function", newdecl);
2153 /* Discard the old built-in function. */
2154 return false;
2155 }
2156
2157 if (!prototype_p (TREE_TYPE (newdecl)))
2158 {
2159 /* Set for built-ins that take no arguments. */
2160 bool func_void_args = false;
2161 if (tree at = TYPE_ARG_TYPES (oldtype))
2162 func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2163
2164 if (extra_warnings && !func_void_args)
2165 warning_at (DECL_SOURCE_LOCATION (newdecl),
2166 OPT_Wbuiltin_declaration_mismatch,
2167 "declaration of built-in function %qD without "
2168 "a prototype; expected %qT",
2169 newdecl, TREE_TYPE (olddecl));
2170 }
2171 }
2172
2173 if (DECL_INITIAL (newdecl))
2174 {
2175 if (DECL_INITIAL (olddecl))
2176 {
2177 /* If both decls are in the same TU and the new declaration
2178 isn't overriding an extern inline reject the new decl.
2179 In c99, no overriding is allowed in the same translation
2180 unit. */
2181 if ((!DECL_EXTERN_INLINE (olddecl)
2182 || DECL_EXTERN_INLINE (newdecl)
2183 || (!flag_gnu89_inline
2184 && (!DECL_DECLARED_INLINE_P (olddecl)
2185 || !lookup_attribute ("gnu_inline",
2186 DECL_ATTRIBUTES (olddecl)))
2187 && (!DECL_DECLARED_INLINE_P (newdecl)
2188 || !lookup_attribute ("gnu_inline",
2189 DECL_ATTRIBUTES (newdecl))))
2190 )
2191 && same_translation_unit_p (newdecl, olddecl))
2192 {
2193 auto_diagnostic_group d;
2194 error ("redefinition of %q+D", newdecl);
2195 locate_old_decl (olddecl);
2196 return false;
2197 }
2198 }
2199 }
2200 /* If we have a prototype after an old-style function definition,
2201 the argument types must be checked specially. */
2202 else if (DECL_INITIAL (olddecl)
2203 && !prototype_p (oldtype) && prototype_p (newtype)
2204 && TYPE_ACTUAL_ARG_TYPES (oldtype))
2205 {
2206 auto_diagnostic_group d;
2207 if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2208 {
2209 locate_old_decl (olddecl);
2210 return false;
2211 }
2212 }
2213 /* A non-static declaration (even an "extern") followed by a
2214 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2215 The same is true for a static forward declaration at block
2216 scope followed by a non-static declaration/definition at file
2217 scope. Static followed by non-static at the same scope is
2218 not undefined behavior, and is the most convenient way to get
2219 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2220 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2221 we do diagnose it if -Wtraditional. */
2222 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2223 {
2224 /* Two exceptions to the rule. If olddecl is an extern
2225 inline, or a predeclared function that isn't actually
2226 built in, newdecl silently overrides olddecl. The latter
2227 occur only in Objective C; see also above. (FIXME: Make
2228 Objective C use normal builtins.) */
2229 if (!DECL_IS_BUILTIN (olddecl)
2230 && !DECL_EXTERN_INLINE (olddecl))
2231 {
2232 auto_diagnostic_group d;
2233 error ("static declaration of %q+D follows "
2234 "non-static declaration", newdecl);
2235 locate_old_decl (olddecl);
2236 }
2237 return false;
2238 }
2239 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2240 {
2241 if (DECL_CONTEXT (olddecl))
2242 {
2243 auto_diagnostic_group d;
2244 error ("non-static declaration of %q+D follows "
2245 "static declaration", newdecl);
2246 locate_old_decl (olddecl);
2247 return false;
2248 }
2249 else if (warn_traditional)
2250 {
2251 warned |= warning (OPT_Wtraditional,
2252 "non-static declaration of %q+D "
2253 "follows static declaration", newdecl);
2254 }
2255 }
2256
2257 /* Make sure gnu_inline attribute is either not present, or
2258 present on all inline decls. */
2259 if (DECL_DECLARED_INLINE_P (olddecl)
2260 && DECL_DECLARED_INLINE_P (newdecl))
2261 {
2262 bool newa = lookup_attribute ("gnu_inline",
2263 DECL_ATTRIBUTES (newdecl)) != NULL;
2264 bool olda = lookup_attribute ("gnu_inline",
2265 DECL_ATTRIBUTES (olddecl)) != NULL;
2266 if (newa != olda)
2267 {
2268 auto_diagnostic_group d;
2269 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2270 newa ? newdecl : olddecl);
2271 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2272 "but not here");
2273 }
2274 }
2275 }
2276 else if (VAR_P (newdecl))
2277 {
2278 /* Only variables can be thread-local, and all declarations must
2279 agree on this property. */
2280 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2281 {
2282 /* Nothing to check. Since OLDDECL is marked threadprivate
2283 and NEWDECL does not have a thread-local attribute, we
2284 will merge the threadprivate attribute into NEWDECL. */
2285 ;
2286 }
2287 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2288 {
2289 auto_diagnostic_group d;
2290 if (DECL_THREAD_LOCAL_P (newdecl))
2291 error ("thread-local declaration of %q+D follows "
2292 "non-thread-local declaration", newdecl);
2293 else
2294 error ("non-thread-local declaration of %q+D follows "
2295 "thread-local declaration", newdecl);
2296
2297 locate_old_decl (olddecl);
2298 return false;
2299 }
2300
2301 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2302 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2303 {
2304 auto_diagnostic_group d;
2305 error ("redefinition of %q+D", newdecl);
2306 locate_old_decl (olddecl);
2307 return false;
2308 }
2309
2310 /* Objects declared at file scope: if the first declaration had
2311 external linkage (even if it was an external reference) the
2312 second must have external linkage as well, or the behavior is
2313 undefined. If the first declaration had internal linkage, then
2314 the second must too, or else be an external reference (in which
2315 case the composite declaration still has internal linkage).
2316 As for function declarations, we warn about the static-then-
2317 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2318 if (DECL_FILE_SCOPE_P (newdecl)
2319 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2320 {
2321 if (DECL_EXTERNAL (newdecl))
2322 {
2323 if (!DECL_FILE_SCOPE_P (olddecl))
2324 {
2325 auto_diagnostic_group d;
2326 error ("extern declaration of %q+D follows "
2327 "declaration with no linkage", newdecl);
2328 locate_old_decl (olddecl);
2329 return false;
2330 }
2331 else if (warn_traditional)
2332 {
2333 warned |= warning (OPT_Wtraditional,
2334 "non-static declaration of %q+D "
2335 "follows static declaration", newdecl);
2336 }
2337 }
2338 else
2339 {
2340 auto_diagnostic_group d;
2341 if (TREE_PUBLIC (newdecl))
2342 error ("non-static declaration of %q+D follows "
2343 "static declaration", newdecl);
2344 else
2345 error ("static declaration of %q+D follows "
2346 "non-static declaration", newdecl);
2347
2348 locate_old_decl (olddecl);
2349 return false;
2350 }
2351 }
2352 /* Two objects with the same name declared at the same block
2353 scope must both be external references (6.7p3). */
2354 else if (!DECL_FILE_SCOPE_P (newdecl))
2355 {
2356 if (DECL_EXTERNAL (newdecl))
2357 {
2358 /* Extern with initializer at block scope, which will
2359 already have received an error. */
2360 }
2361 else if (DECL_EXTERNAL (olddecl))
2362 {
2363 auto_diagnostic_group d;
2364 error ("declaration of %q+D with no linkage follows "
2365 "extern declaration", newdecl);
2366 locate_old_decl (olddecl);
2367 }
2368 else
2369 {
2370 auto_diagnostic_group d;
2371 error ("redeclaration of %q+D with no linkage", newdecl);
2372 locate_old_decl (olddecl);
2373 }
2374
2375 return false;
2376 }
2377
2378 /* C++ does not permit a decl to appear multiple times at file
2379 scope. */
2380 if (warn_cxx_compat
2381 && DECL_FILE_SCOPE_P (newdecl)
2382 && !DECL_EXTERNAL (newdecl)
2383 && !DECL_EXTERNAL (olddecl))
2384 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2385 OPT_Wc___compat,
2386 ("duplicate declaration of %qD is "
2387 "invalid in C++"),
2388 newdecl);
2389 }
2390
2391 /* warnings */
2392 /* All decls must agree on a visibility. */
2393 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2394 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2395 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2396 {
2397 warned |= warning (0, "redeclaration of %q+D with different visibility "
2398 "(old visibility preserved)", newdecl);
2399 }
2400
2401 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2402 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2403 else /* PARM_DECL, VAR_DECL */
2404 {
2405 /* Redeclaration of a parameter is a constraint violation (this is
2406 not explicitly stated, but follows from C99 6.7p3 [no more than
2407 one declaration of the same identifier with no linkage in the
2408 same scope, except type tags] and 6.2.2p6 [parameters have no
2409 linkage]). We must check for a forward parameter declaration,
2410 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2411 an extension, the mandatory diagnostic for which is handled by
2412 mark_forward_parm_decls. */
2413
2414 if (TREE_CODE (newdecl) == PARM_DECL
2415 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2416 {
2417 auto_diagnostic_group d;
2418 error ("redefinition of parameter %q+D", newdecl);
2419 locate_old_decl (olddecl);
2420 return false;
2421 }
2422 }
2423
2424 /* Optional warning for completely redundant decls. */
2425 if (!warned && !pedwarned
2426 && warn_redundant_decls
2427 /* Don't warn about a function declaration followed by a
2428 definition. */
2429 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2430 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2431 /* Don't warn about redundant redeclarations of builtins. */
2432 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2433 && !fndecl_built_in_p (newdecl)
2434 && fndecl_built_in_p (olddecl)
2435 && !C_DECL_DECLARED_BUILTIN (olddecl))
2436 /* Don't warn about an extern followed by a definition. */
2437 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2438 /* Don't warn about forward parameter decls. */
2439 && !(TREE_CODE (newdecl) == PARM_DECL
2440 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2441 /* Don't warn about a variable definition following a declaration. */
2442 && !(VAR_P (newdecl)
2443 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2444 {
2445 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2446 newdecl);
2447 }
2448
2449 /* Report location of previous decl/defn. */
2450 if (warned || pedwarned)
2451 locate_old_decl (olddecl);
2452
2453 #undef DECL_EXTERN_INLINE
2454
2455 return retval;
2456 }
2457
2458 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2459 consistent with OLDDECL, but carries new information. Merge the
2460 new information into OLDDECL. This function issues no
2461 diagnostics. */
2462
2463 static void
2464 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2465 {
2466 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2467 && DECL_INITIAL (newdecl) != NULL_TREE);
2468 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2469 && prototype_p (TREE_TYPE (newdecl)));
2470 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2471 && prototype_p (TREE_TYPE (olddecl)));
2472
2473 /* For real parm decl following a forward decl, rechain the old decl
2474 in its new location and clear TREE_ASM_WRITTEN (it's not a
2475 forward decl anymore). */
2476 if (TREE_CODE (newdecl) == PARM_DECL
2477 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2478 {
2479 struct c_binding *b, **here;
2480
2481 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2482 if ((*here)->decl == olddecl)
2483 goto found;
2484 gcc_unreachable ();
2485
2486 found:
2487 b = *here;
2488 *here = b->prev;
2489 b->prev = current_scope->bindings;
2490 current_scope->bindings = b;
2491
2492 TREE_ASM_WRITTEN (olddecl) = 0;
2493 }
2494
2495 DECL_ATTRIBUTES (newdecl)
2496 = targetm.merge_decl_attributes (olddecl, newdecl);
2497
2498 /* For typedefs use the old type, as the new type's DECL_NAME points
2499 at newdecl, which will be ggc_freed. */
2500 if (TREE_CODE (newdecl) == TYPE_DECL)
2501 {
2502 /* But NEWTYPE might have an attribute, honor that. */
2503 tree tem = newtype;
2504 newtype = oldtype;
2505
2506 if (TYPE_USER_ALIGN (tem))
2507 {
2508 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2509 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2510 TYPE_USER_ALIGN (newtype) = true;
2511 }
2512
2513 /* And remove the new type from the variants list. */
2514 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2515 {
2516 tree remove = TREE_TYPE (newdecl);
2517 if (TYPE_MAIN_VARIANT (remove) == remove)
2518 {
2519 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2520 /* If remove is the main variant, no need to remove that
2521 from the list. One of the DECL_ORIGINAL_TYPE
2522 variants, e.g. created for aligned attribute, might still
2523 refer to the newdecl TYPE_DECL though, so remove that one
2524 in that case. */
2525 if (DECL_ORIGINAL_TYPE (newdecl)
2526 && DECL_ORIGINAL_TYPE (newdecl) != remove)
2527 for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2528 t; t = TYPE_MAIN_VARIANT (t))
2529 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2530 {
2531 TYPE_NEXT_VARIANT (t)
2532 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2533 break;
2534 }
2535 }
2536 else
2537 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2538 t = TYPE_NEXT_VARIANT (t))
2539 if (TYPE_NEXT_VARIANT (t) == remove)
2540 {
2541 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2542 break;
2543 }
2544 }
2545 }
2546
2547 /* Merge the data types specified in the two decls. */
2548 TREE_TYPE (newdecl)
2549 = TREE_TYPE (olddecl)
2550 = composite_type (newtype, oldtype);
2551
2552 /* Lay the type out, unless already done. */
2553 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2554 {
2555 if (TREE_TYPE (newdecl) != error_mark_node)
2556 layout_type (TREE_TYPE (newdecl));
2557 if (TREE_CODE (newdecl) != FUNCTION_DECL
2558 && TREE_CODE (newdecl) != TYPE_DECL
2559 && TREE_CODE (newdecl) != CONST_DECL)
2560 layout_decl (newdecl, 0);
2561 }
2562 else
2563 {
2564 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2565 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2566 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2567 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2568 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2569 {
2570 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2571 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2572 }
2573 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2574 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2575 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2576 DECL_WARN_IF_NOT_ALIGN (olddecl));
2577 }
2578
2579 /* Keep the old rtl since we can safely use it. */
2580 if (HAS_RTL_P (olddecl))
2581 COPY_DECL_RTL (olddecl, newdecl);
2582
2583 /* Merge the type qualifiers. */
2584 if (TREE_READONLY (newdecl))
2585 TREE_READONLY (olddecl) = 1;
2586
2587 if (TREE_THIS_VOLATILE (newdecl))
2588 TREE_THIS_VOLATILE (olddecl) = 1;
2589
2590 /* Merge deprecatedness. */
2591 if (TREE_DEPRECATED (newdecl))
2592 TREE_DEPRECATED (olddecl) = 1;
2593
2594 /* If a decl is in a system header and the other isn't, keep the one on the
2595 system header. Otherwise, keep source location of definition rather than
2596 declaration and of prototype rather than non-prototype unless that
2597 prototype is built-in. */
2598 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2599 && DECL_IN_SYSTEM_HEADER (olddecl)
2600 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2601 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2602 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2603 && DECL_IN_SYSTEM_HEADER (newdecl)
2604 && !DECL_IN_SYSTEM_HEADER (olddecl))
2605 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2606 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2607 && DECL_INITIAL (olddecl) != NULL_TREE)
2608 || (old_is_prototype && !new_is_prototype
2609 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2610 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2611
2612 /* Merge the initialization information. */
2613 if (DECL_INITIAL (newdecl) == NULL_TREE)
2614 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2615
2616 /* Merge the threadprivate attribute. */
2617 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2618 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2619
2620 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2621 {
2622 /* Copy the assembler name.
2623 Currently, it can only be defined in the prototype. */
2624 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2625
2626 /* Use visibility of whichever declaration had it specified */
2627 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2628 {
2629 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2630 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2631 }
2632
2633 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2634 {
2635 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2636 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2637 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2638 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2639 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2640 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2641 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2642 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2643 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2644 if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2645 DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2646 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2647 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2648 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2649 }
2650
2651 /* Merge the storage class information. */
2652 merge_weak (newdecl, olddecl);
2653
2654 /* For functions, static overrides non-static. */
2655 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2656 {
2657 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2658 /* This is since we don't automatically
2659 copy the attributes of NEWDECL into OLDDECL. */
2660 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2661 /* If this clears `static', clear it in the identifier too. */
2662 if (!TREE_PUBLIC (olddecl))
2663 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2664 }
2665 }
2666
2667 /* In c99, 'extern' declaration before (or after) 'inline' means this
2668 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2669 is present. */
2670 if (TREE_CODE (newdecl) == FUNCTION_DECL
2671 && !flag_gnu89_inline
2672 && (DECL_DECLARED_INLINE_P (newdecl)
2673 || DECL_DECLARED_INLINE_P (olddecl))
2674 && (!DECL_DECLARED_INLINE_P (newdecl)
2675 || !DECL_DECLARED_INLINE_P (olddecl)
2676 || !DECL_EXTERNAL (olddecl))
2677 && DECL_EXTERNAL (newdecl)
2678 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2679 && !current_function_decl)
2680 DECL_EXTERNAL (newdecl) = 0;
2681
2682 /* An inline definition following a static declaration is not
2683 DECL_EXTERNAL. */
2684 if (new_is_definition
2685 && (DECL_DECLARED_INLINE_P (newdecl)
2686 || DECL_DECLARED_INLINE_P (olddecl))
2687 && !TREE_PUBLIC (olddecl))
2688 DECL_EXTERNAL (newdecl) = 0;
2689
2690 if (DECL_EXTERNAL (newdecl))
2691 {
2692 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2693 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2694
2695 /* An extern decl does not override previous storage class. */
2696 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2697 if (!DECL_EXTERNAL (newdecl))
2698 {
2699 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2700 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2701 }
2702 }
2703 else
2704 {
2705 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2706 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2707 }
2708
2709 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2710 {
2711 /* If we're redefining a function previously defined as extern
2712 inline, make sure we emit debug info for the inline before we
2713 throw it away, in case it was inlined into a function that
2714 hasn't been written out yet. */
2715 if (new_is_definition && DECL_INITIAL (olddecl))
2716 /* The new defn must not be inline. */
2717 DECL_UNINLINABLE (newdecl) = 1;
2718 else
2719 {
2720 /* If either decl says `inline', this fn is inline, unless
2721 its definition was passed already. */
2722 if (DECL_DECLARED_INLINE_P (newdecl)
2723 || DECL_DECLARED_INLINE_P (olddecl))
2724 DECL_DECLARED_INLINE_P (newdecl) = 1;
2725
2726 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2727 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2728
2729 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2730 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2731 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2732 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2733 }
2734
2735 if (fndecl_built_in_p (olddecl))
2736 {
2737 /* If redeclaring a builtin function, it stays built in.
2738 But it gets tagged as having been declared. */
2739 copy_decl_built_in_function (newdecl, olddecl);
2740 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2741 if (new_is_prototype)
2742 {
2743 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2744 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2745 {
2746 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2747 switch (fncode)
2748 {
2749 /* If a compatible prototype of these builtin functions
2750 is seen, assume the runtime implements it with the
2751 expected semantics. */
2752 case BUILT_IN_STPCPY:
2753 if (builtin_decl_explicit_p (fncode))
2754 set_builtin_decl_implicit_p (fncode, true);
2755 break;
2756 default:
2757 if (builtin_decl_explicit_p (fncode))
2758 set_builtin_decl_declared_p (fncode, true);
2759 break;
2760 }
2761
2762 copy_attributes_to_builtin (newdecl);
2763 }
2764 }
2765 else
2766 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2767 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2768 }
2769
2770 /* Preserve function specific target and optimization options */
2771 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2772 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2773 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2774 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2775
2776 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2777 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2778 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2779 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2780
2781 /* Also preserve various other info from the definition. */
2782 if (!new_is_definition)
2783 {
2784 tree t;
2785 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2786 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2787 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2788 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2789 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2790 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2791 DECL_CONTEXT (t) = newdecl;
2792
2793 /* See if we've got a function to instantiate from. */
2794 if (DECL_SAVED_TREE (olddecl))
2795 DECL_ABSTRACT_ORIGIN (newdecl)
2796 = DECL_ABSTRACT_ORIGIN (olddecl);
2797 }
2798 }
2799
2800 /* Merge the USED information. */
2801 if (TREE_USED (olddecl))
2802 TREE_USED (newdecl) = 1;
2803 else if (TREE_USED (newdecl))
2804 TREE_USED (olddecl) = 1;
2805 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2806 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2807 if (DECL_PRESERVE_P (olddecl))
2808 DECL_PRESERVE_P (newdecl) = 1;
2809 else if (DECL_PRESERVE_P (newdecl))
2810 DECL_PRESERVE_P (olddecl) = 1;
2811
2812 /* Merge DECL_COMMON */
2813 if (VAR_P (olddecl) && VAR_P (newdecl)
2814 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2815 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2816 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2817
2818 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2819 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2820 DECL_ARGUMENTS (if appropriate). */
2821 {
2822 unsigned olddecl_uid = DECL_UID (olddecl);
2823 tree olddecl_context = DECL_CONTEXT (olddecl);
2824 tree olddecl_arguments = NULL;
2825 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2826 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2827
2828 memcpy ((char *) olddecl + sizeof (struct tree_common),
2829 (char *) newdecl + sizeof (struct tree_common),
2830 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2831 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2832 switch (TREE_CODE (olddecl))
2833 {
2834 case FUNCTION_DECL:
2835 case VAR_DECL:
2836 {
2837 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2838
2839 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2840 (char *) newdecl + sizeof (struct tree_decl_common),
2841 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2842 olddecl->decl_with_vis.symtab_node = snode;
2843
2844 if ((DECL_EXTERNAL (olddecl)
2845 || TREE_PUBLIC (olddecl)
2846 || TREE_STATIC (olddecl))
2847 && DECL_SECTION_NAME (newdecl) != NULL)
2848 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2849
2850 /* This isn't quite correct for something like
2851 int __thread x attribute ((tls_model ("local-exec")));
2852 extern int __thread x;
2853 as we'll lose the "local-exec" model. */
2854 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2855 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2856 break;
2857 }
2858
2859 case FIELD_DECL:
2860 case PARM_DECL:
2861 case LABEL_DECL:
2862 case RESULT_DECL:
2863 case CONST_DECL:
2864 case TYPE_DECL:
2865 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2866 (char *) newdecl + sizeof (struct tree_decl_common),
2867 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2868 break;
2869
2870 default:
2871
2872 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2873 (char *) newdecl + sizeof (struct tree_decl_common),
2874 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2875 }
2876 DECL_UID (olddecl) = olddecl_uid;
2877 DECL_CONTEXT (olddecl) = olddecl_context;
2878 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2879 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2880 }
2881
2882 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2883 so that encode_section_info has a chance to look at the new decl
2884 flags and attributes. */
2885 if (DECL_RTL_SET_P (olddecl)
2886 && (TREE_CODE (olddecl) == FUNCTION_DECL
2887 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2888 make_decl_rtl (olddecl);
2889 }
2890
2891 /* Handle when a new declaration NEWDECL has the same name as an old
2892 one OLDDECL in the same binding contour. Prints an error message
2893 if appropriate.
2894
2895 If safely possible, alter OLDDECL to look like NEWDECL, and return
2896 true. Otherwise, return false. */
2897
2898 static bool
2899 duplicate_decls (tree newdecl, tree olddecl)
2900 {
2901 tree newtype = NULL, oldtype = NULL;
2902
2903 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2904 {
2905 /* Avoid `unused variable' and other warnings for OLDDECL. */
2906 TREE_NO_WARNING (olddecl) = 1;
2907 return false;
2908 }
2909
2910 merge_decls (newdecl, olddecl, newtype, oldtype);
2911
2912 /* The NEWDECL will no longer be needed.
2913
2914 Before releasing the node, be sure to remove function from symbol
2915 table that might have been inserted there to record comdat group.
2916 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2917 structure is shared in between NEWDECL and OLDECL. */
2918 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2919 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2920 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2921 {
2922 struct symtab_node *snode = symtab_node::get (newdecl);
2923 if (snode)
2924 snode->remove ();
2925 }
2926 ggc_free (newdecl);
2927 return true;
2928 }
2929
2930 \f
2931 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2932 static void
2933 warn_if_shadowing (tree new_decl)
2934 {
2935 struct c_binding *b;
2936
2937 /* Shadow warnings wanted? */
2938 if (!(warn_shadow
2939 || warn_shadow_local
2940 || warn_shadow_compatible_local)
2941 /* No shadow warnings for internally generated vars. */
2942 || DECL_IS_BUILTIN (new_decl))
2943 return;
2944
2945 /* Is anything being shadowed? Invisible decls do not count. */
2946 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2947 if (b->decl && b->decl != new_decl && !b->invisible
2948 && (b->decl == error_mark_node
2949 || diagnostic_report_warnings_p (global_dc,
2950 DECL_SOURCE_LOCATION (b->decl))))
2951 {
2952 tree old_decl = b->decl;
2953
2954 if (old_decl == error_mark_node)
2955 {
2956 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2957 "non-variable", new_decl);
2958 break;
2959 }
2960
2961 bool warned = false;
2962 auto_diagnostic_group d;
2963 if (TREE_CODE (old_decl) == PARM_DECL)
2964 {
2965 enum opt_code warning_code;
2966
2967 /* If '-Wshadow=compatible-local' is specified without other
2968 -Wshadow= flags, we will warn only when the types of the
2969 shadowing variable (i.e. new_decl) and the shadowed variable
2970 (old_decl) are compatible. */
2971 if (warn_shadow)
2972 warning_code = OPT_Wshadow;
2973 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2974 warning_code = OPT_Wshadow_compatible_local;
2975 else
2976 warning_code = OPT_Wshadow_local;
2977 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2978 "declaration of %qD shadows a parameter",
2979 new_decl);
2980 }
2981 else if (DECL_FILE_SCOPE_P (old_decl))
2982 {
2983 /* Do not warn if a variable shadows a function, unless
2984 the variable is a function or a pointer-to-function. */
2985 if (TREE_CODE (old_decl) == FUNCTION_DECL
2986 && TREE_CODE (new_decl) != FUNCTION_DECL
2987 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2988 continue;
2989
2990 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2991 "declaration of %qD shadows a global "
2992 "declaration",
2993 new_decl);
2994 }
2995 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2996 && fndecl_built_in_p (old_decl))
2997 {
2998 warning (OPT_Wshadow, "declaration of %q+D shadows "
2999 "a built-in function", new_decl);
3000 break;
3001 }
3002 else
3003 {
3004 enum opt_code warning_code;
3005
3006 /* If '-Wshadow=compatible-local' is specified without other
3007 -Wshadow= flags, we will warn only when the types of the
3008 shadowing variable (i.e. new_decl) and the shadowed variable
3009 (old_decl) are compatible. */
3010 if (warn_shadow)
3011 warning_code = OPT_Wshadow;
3012 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3013 warning_code = OPT_Wshadow_compatible_local;
3014 else
3015 warning_code = OPT_Wshadow_local;
3016 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3017 "declaration of %qD shadows a previous local",
3018 new_decl);
3019 }
3020
3021 if (warned)
3022 inform (DECL_SOURCE_LOCATION (old_decl),
3023 "shadowed declaration is here");
3024
3025 break;
3026 }
3027 }
3028
3029 /* Record a decl-node X as belonging to the current lexical scope.
3030 Check for errors (such as an incompatible declaration for the same
3031 name already seen in the same scope).
3032
3033 Returns either X or an old decl for the same name.
3034 If an old decl is returned, it may have been smashed
3035 to agree with what X says. */
3036
3037 tree
3038 pushdecl (tree x)
3039 {
3040 tree name = DECL_NAME (x);
3041 struct c_scope *scope = current_scope;
3042 struct c_binding *b;
3043 bool nested = false;
3044 location_t locus = DECL_SOURCE_LOCATION (x);
3045
3046 /* Must set DECL_CONTEXT for everything not at file scope or
3047 DECL_FILE_SCOPE_P won't work. Local externs don't count
3048 unless they have initializers (which generate code). */
3049 if (current_function_decl
3050 && (!VAR_OR_FUNCTION_DECL_P (x)
3051 || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3052 DECL_CONTEXT (x) = current_function_decl;
3053
3054 /* Anonymous decls are just inserted in the scope. */
3055 if (!name)
3056 {
3057 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3058 locus);
3059 return x;
3060 }
3061
3062 /* First, see if there is another declaration with the same name in
3063 the current scope. If there is, duplicate_decls may do all the
3064 work for us. If duplicate_decls returns false, that indicates
3065 two incompatible decls in the same scope; we are to silently
3066 replace the old one (duplicate_decls has issued all appropriate
3067 diagnostics). In particular, we should not consider possible
3068 duplicates in the external scope, or shadowing. */
3069 b = I_SYMBOL_BINDING (name);
3070 if (b && B_IN_SCOPE (b, scope))
3071 {
3072 struct c_binding *b_ext, *b_use;
3073 tree type = TREE_TYPE (x);
3074 tree visdecl = b->decl;
3075 tree vistype = TREE_TYPE (visdecl);
3076 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3077 && COMPLETE_TYPE_P (TREE_TYPE (x)))
3078 b->inner_comp = false;
3079 b_use = b;
3080 b_ext = b;
3081 /* If this is an external linkage declaration, we should check
3082 for compatibility with the type in the external scope before
3083 setting the type at this scope based on the visible
3084 information only. */
3085 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3086 {
3087 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3088 b_ext = b_ext->shadowed;
3089 if (b_ext)
3090 {
3091 b_use = b_ext;
3092 if (b_use->u.type)
3093 TREE_TYPE (b_use->decl) = b_use->u.type;
3094 }
3095 }
3096 if (duplicate_decls (x, b_use->decl))
3097 {
3098 if (b_use != b)
3099 {
3100 /* Save the updated type in the external scope and
3101 restore the proper type for this scope. */
3102 tree thistype;
3103 if (comptypes (vistype, type))
3104 thistype = composite_type (vistype, type);
3105 else
3106 thistype = TREE_TYPE (b_use->decl);
3107 b_use->u.type = TREE_TYPE (b_use->decl);
3108 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3109 && fndecl_built_in_p (b_use->decl))
3110 thistype
3111 = build_type_attribute_variant (thistype,
3112 TYPE_ATTRIBUTES
3113 (b_use->u.type));
3114 TREE_TYPE (b_use->decl) = thistype;
3115 }
3116 return b_use->decl;
3117 }
3118 else
3119 goto skip_external_and_shadow_checks;
3120 }
3121
3122 /* All declarations with external linkage, and all external
3123 references, go in the external scope, no matter what scope is
3124 current. However, the binding in that scope is ignored for
3125 purposes of normal name lookup. A separate binding structure is
3126 created in the requested scope; this governs the normal
3127 visibility of the symbol.
3128
3129 The binding in the externals scope is used exclusively for
3130 detecting duplicate declarations of the same object, no matter
3131 what scope they are in; this is what we do here. (C99 6.2.7p2:
3132 All declarations that refer to the same object or function shall
3133 have compatible type; otherwise, the behavior is undefined.)
3134 However, in Objective-C, we also want to detect declarations
3135 conflicting with those of the basic types. */
3136 if ((DECL_EXTERNAL (x) || scope == file_scope)
3137 && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3138 {
3139 tree type = TREE_TYPE (x);
3140 tree vistype = NULL_TREE;
3141 tree visdecl = NULL_TREE;
3142 bool type_saved = false;
3143 if (b && !B_IN_EXTERNAL_SCOPE (b)
3144 && VAR_OR_FUNCTION_DECL_P (b->decl)
3145 && DECL_FILE_SCOPE_P (b->decl))
3146 {
3147 visdecl = b->decl;
3148 vistype = TREE_TYPE (visdecl);
3149 }
3150 if (scope != file_scope
3151 && !DECL_IN_SYSTEM_HEADER (x))
3152 warning_at (locus, OPT_Wnested_externs,
3153 "nested extern declaration of %qD", x);
3154
3155 while (b && !B_IN_EXTERNAL_SCOPE (b))
3156 {
3157 /* If this decl might be modified, save its type. This is
3158 done here rather than when the decl is first bound
3159 because the type may change after first binding, through
3160 being completed or through attributes being added. If we
3161 encounter multiple such decls, only the first should have
3162 its type saved; the others will already have had their
3163 proper types saved and the types will not have changed as
3164 their scopes will not have been re-entered. */
3165 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3166 {
3167 b->u.type = TREE_TYPE (b->decl);
3168 type_saved = true;
3169 }
3170 if (B_IN_FILE_SCOPE (b)
3171 && VAR_P (b->decl)
3172 && TREE_STATIC (b->decl)
3173 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3174 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3175 && TREE_CODE (type) == ARRAY_TYPE
3176 && TYPE_DOMAIN (type)
3177 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3178 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3179 {
3180 /* Array type completed in inner scope, which should be
3181 diagnosed if the completion does not have size 1 and
3182 it does not get completed in the file scope. */
3183 b->inner_comp = true;
3184 }
3185 b = b->shadowed;
3186 }
3187
3188 /* If a matching external declaration has been found, set its
3189 type to the composite of all the types of that declaration.
3190 After the consistency checks, it will be reset to the
3191 composite of the visible types only. */
3192 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3193 && b->u.type)
3194 TREE_TYPE (b->decl) = b->u.type;
3195
3196 /* The point of the same_translation_unit_p check here is,
3197 we want to detect a duplicate decl for a construct like
3198 foo() { extern bar(); } ... static bar(); but not if
3199 they are in different translation units. In any case,
3200 the static does not go in the externals scope. */
3201 if (b
3202 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3203 && duplicate_decls (x, b->decl))
3204 {
3205 tree thistype;
3206 if (vistype)
3207 {
3208 if (comptypes (vistype, type))
3209 thistype = composite_type (vistype, type);
3210 else
3211 thistype = TREE_TYPE (b->decl);
3212 }
3213 else
3214 thistype = type;
3215 b->u.type = TREE_TYPE (b->decl);
3216 if (TREE_CODE (b->decl) == FUNCTION_DECL
3217 && fndecl_built_in_p (b->decl))
3218 thistype
3219 = build_type_attribute_variant (thistype,
3220 TYPE_ATTRIBUTES (b->u.type));
3221 TREE_TYPE (b->decl) = thistype;
3222 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3223 locus);
3224 return b->decl;
3225 }
3226 else if (TREE_PUBLIC (x))
3227 {
3228 if (visdecl && !b && duplicate_decls (x, visdecl))
3229 {
3230 /* An external declaration at block scope referring to a
3231 visible entity with internal linkage. The composite
3232 type will already be correct for this scope, so we
3233 just need to fall through to make the declaration in
3234 this scope. */
3235 nested = true;
3236 x = visdecl;
3237 }
3238 else
3239 {
3240 bind (name, x, external_scope, /*invisible=*/true,
3241 /*nested=*/false, locus);
3242 nested = true;
3243 }
3244 }
3245 }
3246
3247 if (TREE_CODE (x) != PARM_DECL)
3248 warn_if_shadowing (x);
3249
3250 skip_external_and_shadow_checks:
3251 if (TREE_CODE (x) == TYPE_DECL)
3252 {
3253 /* So this is a typedef, set its underlying type. */
3254 set_underlying_type (x);
3255
3256 /* If X is a typedef defined in the current function, record it
3257 for the purpose of implementing the -Wunused-local-typedefs
3258 warning. */
3259 record_locally_defined_typedef (x);
3260 }
3261
3262 bind (name, x, scope, /*invisible=*/false, nested, locus);
3263
3264 /* If x's type is incomplete because it's based on a
3265 structure or union which has not yet been fully declared,
3266 attach it to that structure or union type, so we can go
3267 back and complete the variable declaration later, if the
3268 structure or union gets fully declared.
3269
3270 If the input is erroneous, we can have error_mark in the type
3271 slot (e.g. "f(void a, ...)") - that doesn't count as an
3272 incomplete type. */
3273 if (TREE_TYPE (x) != error_mark_node
3274 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3275 {
3276 tree element = TREE_TYPE (x);
3277
3278 while (TREE_CODE (element) == ARRAY_TYPE)
3279 element = TREE_TYPE (element);
3280 element = TYPE_MAIN_VARIANT (element);
3281
3282 if (RECORD_OR_UNION_TYPE_P (element)
3283 && (TREE_CODE (x) != TYPE_DECL
3284 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3285 && !COMPLETE_TYPE_P (element))
3286 C_TYPE_INCOMPLETE_VARS (element)
3287 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3288 }
3289 return x;
3290 }
3291 \f
3292
3293 /* Issue a warning about implicit function declaration. ID is the function
3294 identifier, OLDDECL is a declaration of the function in a different scope,
3295 or NULL_TREE. */
3296
3297 static void
3298 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3299 {
3300 if (!warn_implicit_function_declaration)
3301 return;
3302
3303 bool warned;
3304 auto_diagnostic_group d;
3305 name_hint hint;
3306 if (!olddecl)
3307 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3308
3309 if (flag_isoc99)
3310 {
3311 if (const char *suggestion = hint.suggestion ())
3312 {
3313 gcc_rich_location richloc (loc);
3314 richloc.add_fixit_replace (suggestion);
3315 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3316 "implicit declaration of function %qE;"
3317 " did you mean %qs?",
3318 id, suggestion);
3319 }
3320 else
3321 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3322 "implicit declaration of function %qE", id);
3323 }
3324 else if (const char *suggestion = hint.suggestion ())
3325 {
3326 gcc_rich_location richloc (loc);
3327 richloc.add_fixit_replace (suggestion);
3328 warned = warning_at
3329 (&richloc, OPT_Wimplicit_function_declaration,
3330 G_("implicit declaration of function %qE; did you mean %qs?"),
3331 id, suggestion);
3332 }
3333 else
3334 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3335 G_("implicit declaration of function %qE"), id);
3336
3337 if (olddecl && warned)
3338 locate_old_decl (olddecl);
3339
3340 if (!warned)
3341 hint.suppress ();
3342 }
3343
3344 /* Return the name of the header file that declares built-in function
3345 FNDECL, or null if either we don't know or don't expect to see an
3346 explicit declaration. */
3347
3348 static const char *
3349 header_for_builtin_fn (tree fndecl)
3350 {
3351 if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3352 return NULL;
3353
3354 switch (DECL_FUNCTION_CODE (fndecl))
3355 {
3356 CASE_FLT_FN (BUILT_IN_ACOS):
3357 CASE_FLT_FN (BUILT_IN_ACOSH):
3358 CASE_FLT_FN (BUILT_IN_ASIN):
3359 CASE_FLT_FN (BUILT_IN_ASINH):
3360 CASE_FLT_FN (BUILT_IN_ATAN):
3361 CASE_FLT_FN (BUILT_IN_ATANH):
3362 CASE_FLT_FN (BUILT_IN_ATAN2):
3363 CASE_FLT_FN (BUILT_IN_CBRT):
3364 CASE_FLT_FN (BUILT_IN_CEIL):
3365 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3366 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3367 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3368 CASE_FLT_FN (BUILT_IN_COS):
3369 CASE_FLT_FN (BUILT_IN_COSH):
3370 CASE_FLT_FN (BUILT_IN_ERF):
3371 CASE_FLT_FN (BUILT_IN_ERFC):
3372 CASE_FLT_FN (BUILT_IN_EXP):
3373 CASE_FLT_FN (BUILT_IN_EXP2):
3374 CASE_FLT_FN (BUILT_IN_EXPM1):
3375 CASE_FLT_FN (BUILT_IN_FABS):
3376 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3377 CASE_FLT_FN (BUILT_IN_FDIM):
3378 CASE_FLT_FN (BUILT_IN_FLOOR):
3379 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3380 CASE_FLT_FN (BUILT_IN_FMA):
3381 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3382 CASE_FLT_FN (BUILT_IN_FMAX):
3383 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3384 CASE_FLT_FN (BUILT_IN_FMIN):
3385 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3386 CASE_FLT_FN (BUILT_IN_FMOD):
3387 CASE_FLT_FN (BUILT_IN_FREXP):
3388 CASE_FLT_FN (BUILT_IN_HYPOT):
3389 CASE_FLT_FN (BUILT_IN_ILOGB):
3390 CASE_FLT_FN (BUILT_IN_LDEXP):
3391 CASE_FLT_FN (BUILT_IN_LGAMMA):
3392 CASE_FLT_FN (BUILT_IN_LLRINT):
3393 CASE_FLT_FN (BUILT_IN_LLROUND):
3394 CASE_FLT_FN (BUILT_IN_LOG):
3395 CASE_FLT_FN (BUILT_IN_LOG10):
3396 CASE_FLT_FN (BUILT_IN_LOG1P):
3397 CASE_FLT_FN (BUILT_IN_LOG2):
3398 CASE_FLT_FN (BUILT_IN_LOGB):
3399 CASE_FLT_FN (BUILT_IN_LRINT):
3400 CASE_FLT_FN (BUILT_IN_LROUND):
3401 CASE_FLT_FN (BUILT_IN_MODF):
3402 CASE_FLT_FN (BUILT_IN_NAN):
3403 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3404 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3405 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3406 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3407 CASE_FLT_FN (BUILT_IN_POW):
3408 CASE_FLT_FN (BUILT_IN_REMAINDER):
3409 CASE_FLT_FN (BUILT_IN_REMQUO):
3410 CASE_FLT_FN (BUILT_IN_RINT):
3411 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3412 CASE_FLT_FN (BUILT_IN_ROUND):
3413 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3414 CASE_FLT_FN (BUILT_IN_SCALBLN):
3415 CASE_FLT_FN (BUILT_IN_SCALBN):
3416 CASE_FLT_FN (BUILT_IN_SIN):
3417 CASE_FLT_FN (BUILT_IN_SINH):
3418 CASE_FLT_FN (BUILT_IN_SINCOS):
3419 CASE_FLT_FN (BUILT_IN_SQRT):
3420 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3421 CASE_FLT_FN (BUILT_IN_TAN):
3422 CASE_FLT_FN (BUILT_IN_TANH):
3423 CASE_FLT_FN (BUILT_IN_TGAMMA):
3424 CASE_FLT_FN (BUILT_IN_TRUNC):
3425 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3426 case BUILT_IN_ISINF:
3427 case BUILT_IN_ISNAN:
3428 return "<math.h>";
3429 CASE_FLT_FN (BUILT_IN_CABS):
3430 CASE_FLT_FN (BUILT_IN_CACOS):
3431 CASE_FLT_FN (BUILT_IN_CACOSH):
3432 CASE_FLT_FN (BUILT_IN_CARG):
3433 CASE_FLT_FN (BUILT_IN_CASIN):
3434 CASE_FLT_FN (BUILT_IN_CASINH):
3435 CASE_FLT_FN (BUILT_IN_CATAN):
3436 CASE_FLT_FN (BUILT_IN_CATANH):
3437 CASE_FLT_FN (BUILT_IN_CCOS):
3438 CASE_FLT_FN (BUILT_IN_CCOSH):
3439 CASE_FLT_FN (BUILT_IN_CEXP):
3440 CASE_FLT_FN (BUILT_IN_CIMAG):
3441 CASE_FLT_FN (BUILT_IN_CLOG):
3442 CASE_FLT_FN (BUILT_IN_CONJ):
3443 CASE_FLT_FN (BUILT_IN_CPOW):
3444 CASE_FLT_FN (BUILT_IN_CPROJ):
3445 CASE_FLT_FN (BUILT_IN_CREAL):
3446 CASE_FLT_FN (BUILT_IN_CSIN):
3447 CASE_FLT_FN (BUILT_IN_CSINH):
3448 CASE_FLT_FN (BUILT_IN_CSQRT):
3449 CASE_FLT_FN (BUILT_IN_CTAN):
3450 CASE_FLT_FN (BUILT_IN_CTANH):
3451 return "<complex.h>";
3452 case BUILT_IN_MEMCHR:
3453 case BUILT_IN_MEMCMP:
3454 case BUILT_IN_MEMCPY:
3455 case BUILT_IN_MEMMOVE:
3456 case BUILT_IN_MEMSET:
3457 case BUILT_IN_STRCAT:
3458 case BUILT_IN_STRCHR:
3459 case BUILT_IN_STRCMP:
3460 case BUILT_IN_STRCPY:
3461 case BUILT_IN_STRCSPN:
3462 case BUILT_IN_STRLEN:
3463 case BUILT_IN_STRNCAT:
3464 case BUILT_IN_STRNCMP:
3465 case BUILT_IN_STRNCPY:
3466 case BUILT_IN_STRPBRK:
3467 case BUILT_IN_STRRCHR:
3468 case BUILT_IN_STRSPN:
3469 case BUILT_IN_STRSTR:
3470 return "<string.h>";
3471 case BUILT_IN_FPRINTF:
3472 case BUILT_IN_PUTC:
3473 case BUILT_IN_FPUTC:
3474 case BUILT_IN_FPUTS:
3475 case BUILT_IN_FSCANF:
3476 case BUILT_IN_FWRITE:
3477 case BUILT_IN_PRINTF:
3478 case BUILT_IN_PUTCHAR:
3479 case BUILT_IN_PUTS:
3480 case BUILT_IN_SCANF:
3481 case BUILT_IN_SNPRINTF:
3482 case BUILT_IN_SPRINTF:
3483 case BUILT_IN_SSCANF:
3484 case BUILT_IN_VFPRINTF:
3485 case BUILT_IN_VFSCANF:
3486 case BUILT_IN_VPRINTF:
3487 case BUILT_IN_VSCANF:
3488 case BUILT_IN_VSNPRINTF:
3489 case BUILT_IN_VSPRINTF:
3490 case BUILT_IN_VSSCANF:
3491 return "<stdio.h>";
3492 case BUILT_IN_ISALNUM:
3493 case BUILT_IN_ISALPHA:
3494 case BUILT_IN_ISBLANK:
3495 case BUILT_IN_ISCNTRL:
3496 case BUILT_IN_ISDIGIT:
3497 case BUILT_IN_ISGRAPH:
3498 case BUILT_IN_ISLOWER:
3499 case BUILT_IN_ISPRINT:
3500 case BUILT_IN_ISPUNCT:
3501 case BUILT_IN_ISSPACE:
3502 case BUILT_IN_ISUPPER:
3503 case BUILT_IN_ISXDIGIT:
3504 case BUILT_IN_TOLOWER:
3505 case BUILT_IN_TOUPPER:
3506 return "<ctype.h>";
3507 case BUILT_IN_ISWALNUM:
3508 case BUILT_IN_ISWALPHA:
3509 case BUILT_IN_ISWBLANK:
3510 case BUILT_IN_ISWCNTRL:
3511 case BUILT_IN_ISWDIGIT:
3512 case BUILT_IN_ISWGRAPH:
3513 case BUILT_IN_ISWLOWER:
3514 case BUILT_IN_ISWPRINT:
3515 case BUILT_IN_ISWPUNCT:
3516 case BUILT_IN_ISWSPACE:
3517 case BUILT_IN_ISWUPPER:
3518 case BUILT_IN_ISWXDIGIT:
3519 case BUILT_IN_TOWLOWER:
3520 case BUILT_IN_TOWUPPER:
3521 return "<wctype.h>";
3522 case BUILT_IN_ABORT:
3523 case BUILT_IN_ABS:
3524 case BUILT_IN_CALLOC:
3525 case BUILT_IN_EXIT:
3526 case BUILT_IN_FREE:
3527 case BUILT_IN_LABS:
3528 case BUILT_IN_LLABS:
3529 case BUILT_IN_MALLOC:
3530 case BUILT_IN_REALLOC:
3531 case BUILT_IN__EXIT2:
3532 case BUILT_IN_ALIGNED_ALLOC:
3533 return "<stdlib.h>";
3534 case BUILT_IN_IMAXABS:
3535 return "<inttypes.h>";
3536 case BUILT_IN_STRFTIME:
3537 return "<time.h>";
3538 default:
3539 return NULL;
3540 }
3541 }
3542
3543 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3544 function of type int (). */
3545
3546 tree
3547 implicitly_declare (location_t loc, tree functionid)
3548 {
3549 struct c_binding *b;
3550 tree decl = NULL_TREE;
3551 tree asmspec_tree;
3552
3553 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3554 {
3555 if (B_IN_SCOPE (b, external_scope))
3556 {
3557 decl = b->decl;
3558 break;
3559 }
3560 }
3561
3562 if (decl)
3563 {
3564 if (TREE_CODE (decl) != FUNCTION_DECL)
3565 return decl;
3566
3567 /* FIXME: Objective-C has weird not-really-builtin functions
3568 which are supposed to be visible automatically. They wind up
3569 in the external scope because they're pushed before the file
3570 scope gets created. Catch this here and rebind them into the
3571 file scope. */
3572 if (!fndecl_built_in_p (decl) && DECL_IS_BUILTIN (decl))
3573 {
3574 bind (functionid, decl, file_scope,
3575 /*invisible=*/false, /*nested=*/true,
3576 DECL_SOURCE_LOCATION (decl));
3577 return decl;
3578 }
3579 else
3580 {
3581 tree newtype = default_function_type;
3582 if (b->u.type)
3583 TREE_TYPE (decl) = b->u.type;
3584 /* Implicit declaration of a function already declared
3585 (somehow) in a different scope, or as a built-in.
3586 If this is the first time this has happened, warn;
3587 then recycle the old declaration but with the new type. */
3588 if (!C_DECL_IMPLICIT (decl))
3589 {
3590 implicit_decl_warning (loc, functionid, decl);
3591 C_DECL_IMPLICIT (decl) = 1;
3592 }
3593 if (fndecl_built_in_p (decl))
3594 {
3595 newtype = build_type_attribute_variant (newtype,
3596 TYPE_ATTRIBUTES
3597 (TREE_TYPE (decl)));
3598 if (!comptypes (newtype, TREE_TYPE (decl)))
3599 {
3600 bool warned = warning_at (loc, 0, "incompatible implicit "
3601 "declaration of built-in "
3602 "function %qD", decl);
3603 /* See if we can hint which header to include. */
3604 const char *header = header_for_builtin_fn (decl);
3605 if (header != NULL && warned)
3606 {
3607 rich_location richloc (line_table, loc);
3608 maybe_add_include_fixit (&richloc, header, true);
3609 inform (&richloc,
3610 "include %qs or provide a declaration of %qD",
3611 header, decl);
3612 }
3613 newtype = TREE_TYPE (decl);
3614 }
3615 }
3616 else
3617 {
3618 if (!comptypes (newtype, TREE_TYPE (decl)))
3619 {
3620 auto_diagnostic_group d;
3621 error_at (loc, "incompatible implicit declaration of "
3622 "function %qD", decl);
3623 locate_old_decl (decl);
3624 }
3625 }
3626 b->u.type = TREE_TYPE (decl);
3627 TREE_TYPE (decl) = newtype;
3628 bind (functionid, decl, current_scope,
3629 /*invisible=*/false, /*nested=*/true,
3630 DECL_SOURCE_LOCATION (decl));
3631 return decl;
3632 }
3633 }
3634
3635 /* Not seen before. */
3636 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3637 DECL_EXTERNAL (decl) = 1;
3638 TREE_PUBLIC (decl) = 1;
3639 C_DECL_IMPLICIT (decl) = 1;
3640 implicit_decl_warning (loc, functionid, 0);
3641 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3642 if (asmspec_tree)
3643 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3644
3645 /* C89 says implicit declarations are in the innermost block.
3646 So we record the decl in the standard fashion. */
3647 decl = pushdecl (decl);
3648
3649 /* No need to call objc_check_decl here - it's a function type. */
3650 rest_of_decl_compilation (decl, 0, 0);
3651
3652 /* Write a record describing this implicit function declaration
3653 to the prototypes file (if requested). */
3654 gen_aux_info_record (decl, 0, 1, 0);
3655
3656 /* Possibly apply some default attributes to this implicit declaration. */
3657 decl_attributes (&decl, NULL_TREE, 0);
3658
3659 return decl;
3660 }
3661
3662 /* Issue an error message for a reference to an undeclared variable
3663 ID, including a reference to a builtin outside of function-call
3664 context. Establish a binding of the identifier to error_mark_node
3665 in an appropriate scope, which will suppress further errors for the
3666 same identifier. The error message should be given location LOC. */
3667 void
3668 undeclared_variable (location_t loc, tree id)
3669 {
3670 static bool already = false;
3671 struct c_scope *scope;
3672
3673 auto_diagnostic_group d;
3674 if (current_function_decl == NULL_TREE)
3675 {
3676 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3677 if (const char *suggestion = guessed_id.suggestion ())
3678 {
3679 gcc_rich_location richloc (loc);
3680 richloc.add_fixit_replace (suggestion);
3681 error_at (&richloc,
3682 "%qE undeclared here (not in a function);"
3683 " did you mean %qs?",
3684 id, suggestion);
3685 }
3686 else
3687 error_at (loc, "%qE undeclared here (not in a function)", id);
3688 scope = current_scope;
3689 }
3690 else
3691 {
3692 if (!objc_diagnose_private_ivar (id))
3693 {
3694 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3695 if (const char *suggestion = guessed_id.suggestion ())
3696 {
3697 gcc_rich_location richloc (loc);
3698 richloc.add_fixit_replace (suggestion);
3699 error_at (&richloc,
3700 "%qE undeclared (first use in this function);"
3701 " did you mean %qs?",
3702 id, suggestion);
3703 }
3704 else
3705 error_at (loc, "%qE undeclared (first use in this function)", id);
3706 }
3707 if (!already)
3708 {
3709 inform (loc, "each undeclared identifier is reported only"
3710 " once for each function it appears in");
3711 already = true;
3712 }
3713
3714 /* If we are parsing old-style parameter decls, current_function_decl
3715 will be nonnull but current_function_scope will be null. */
3716 scope = current_function_scope ? current_function_scope : current_scope;
3717 }
3718 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3719 UNKNOWN_LOCATION);
3720 }
3721 \f
3722 /* Subroutine of lookup_label, declare_label, define_label: construct a
3723 LABEL_DECL with all the proper frills. Also create a struct
3724 c_label_vars initialized for the current scope. */
3725
3726 static tree
3727 make_label (location_t location, tree name, bool defining,
3728 struct c_label_vars **p_label_vars)
3729 {
3730 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3731 DECL_CONTEXT (label) = current_function_decl;
3732 SET_DECL_MODE (label, VOIDmode);
3733
3734 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3735 label_vars->shadowed = NULL;
3736 set_spot_bindings (&label_vars->label_bindings, defining);
3737 label_vars->decls_in_scope = make_tree_vector ();
3738 label_vars->gotos = NULL;
3739 *p_label_vars = label_vars;
3740
3741 return label;
3742 }
3743
3744 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3745 Create one if none exists so far for the current function.
3746 This is called when a label is used in a goto expression or
3747 has its address taken. */
3748
3749 tree
3750 lookup_label (tree name)
3751 {
3752 tree label;
3753 struct c_label_vars *label_vars;
3754
3755 if (current_function_scope == 0)
3756 {
3757 error ("label %qE referenced outside of any function", name);
3758 return NULL_TREE;
3759 }
3760
3761 /* Use a label already defined or ref'd with this name, but not if
3762 it is inherited from a containing function and wasn't declared
3763 using __label__. */
3764 label = I_LABEL_DECL (name);
3765 if (label && (DECL_CONTEXT (label) == current_function_decl
3766 || C_DECLARED_LABEL_FLAG (label)))
3767 {
3768 /* If the label has only been declared, update its apparent
3769 location to point here, for better diagnostics if it
3770 turns out not to have been defined. */
3771 if (DECL_INITIAL (label) == NULL_TREE)
3772 DECL_SOURCE_LOCATION (label) = input_location;
3773 return label;
3774 }
3775
3776 /* No label binding for that identifier; make one. */
3777 label = make_label (input_location, name, false, &label_vars);
3778
3779 /* Ordinary labels go in the current function scope. */
3780 bind_label (name, label, current_function_scope, label_vars);
3781
3782 return label;
3783 }
3784
3785 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3786 to LABEL. */
3787
3788 static void
3789 warn_about_goto (location_t goto_loc, tree label, tree decl)
3790 {
3791 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3792 error_at (goto_loc,
3793 "jump into scope of identifier with variably modified type");
3794 else
3795 warning_at (goto_loc, OPT_Wjump_misses_init,
3796 "jump skips variable initialization");
3797 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3798 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3799 }
3800
3801 /* Look up a label because of a goto statement. This is like
3802 lookup_label, but also issues any appropriate warnings. */
3803
3804 tree
3805 lookup_label_for_goto (location_t loc, tree name)
3806 {
3807 tree label;
3808 struct c_label_vars *label_vars;
3809 unsigned int ix;
3810 tree decl;
3811
3812 label = lookup_label (name);
3813 if (label == NULL_TREE)
3814 return NULL_TREE;
3815
3816 /* If we are jumping to a different function, we can't issue any
3817 useful warnings. */
3818 if (DECL_CONTEXT (label) != current_function_decl)
3819 {
3820 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3821 return label;
3822 }
3823
3824 label_vars = I_LABEL_BINDING (name)->u.label;
3825
3826 /* If the label has not yet been defined, then push this goto on a
3827 list for possible later warnings. */
3828 if (label_vars->label_bindings.scope == NULL)
3829 {
3830 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3831
3832 g->loc = loc;
3833 set_spot_bindings (&g->goto_bindings, true);
3834 vec_safe_push (label_vars->gotos, g);
3835 return label;
3836 }
3837
3838 /* If there are any decls in label_vars->decls_in_scope, then this
3839 goto has missed the declaration of the decl. This happens for a
3840 case like
3841 int i = 1;
3842 lab:
3843 ...
3844 goto lab;
3845 Issue a warning or error. */
3846 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3847 warn_about_goto (loc, label, decl);
3848
3849 if (label_vars->label_bindings.left_stmt_expr)
3850 {
3851 error_at (loc, "jump into statement expression");
3852 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3853 }
3854
3855 return label;
3856 }
3857
3858 /* Make a label named NAME in the current function, shadowing silently
3859 any that may be inherited from containing functions or containing
3860 scopes. This is called for __label__ declarations. */
3861
3862 tree
3863 declare_label (tree name)
3864 {
3865 struct c_binding *b = I_LABEL_BINDING (name);
3866 tree label;
3867 struct c_label_vars *label_vars;
3868
3869 /* Check to make sure that the label hasn't already been declared
3870 at this scope */
3871 if (b && B_IN_CURRENT_SCOPE (b))
3872 {
3873 auto_diagnostic_group d;
3874 error ("duplicate label declaration %qE", name);
3875 locate_old_decl (b->decl);
3876
3877 /* Just use the previous declaration. */
3878 return b->decl;
3879 }
3880
3881 label = make_label (input_location, name, false, &label_vars);
3882 C_DECLARED_LABEL_FLAG (label) = 1;
3883
3884 /* Declared labels go in the current scope. */
3885 bind_label (name, label, current_scope, label_vars);
3886
3887 return label;
3888 }
3889
3890 /* When we define a label, issue any appropriate warnings if there are
3891 any gotos earlier in the function which jump to this label. */
3892
3893 static void
3894 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3895 {
3896 unsigned int ix;
3897 struct c_goto_bindings *g;
3898
3899 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3900 {
3901 struct c_binding *b;
3902 struct c_scope *scope;
3903
3904 /* We have a goto to this label. The goto is going forward. In
3905 g->scope, the goto is going to skip any binding which was
3906 defined after g->bindings_in_scope. */
3907 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3908 {
3909 for (b = g->goto_bindings.scope->bindings;
3910 b != g->goto_bindings.bindings_in_scope;
3911 b = b->prev)
3912 {
3913 if (decl_jump_unsafe (b->decl))
3914 warn_about_goto (g->loc, label, b->decl);
3915 }
3916 }
3917
3918 /* We also need to warn about decls defined in any scopes
3919 between the scope of the label and the scope of the goto. */
3920 for (scope = label_vars->label_bindings.scope;
3921 scope != g->goto_bindings.scope;
3922 scope = scope->outer)
3923 {
3924 gcc_assert (scope != NULL);
3925 if (scope->has_jump_unsafe_decl)
3926 {
3927 if (scope == label_vars->label_bindings.scope)
3928 b = label_vars->label_bindings.bindings_in_scope;
3929 else
3930 b = scope->bindings;
3931 for (; b != NULL; b = b->prev)
3932 {
3933 if (decl_jump_unsafe (b->decl))
3934 warn_about_goto (g->loc, label, b->decl);
3935 }
3936 }
3937 }
3938
3939 if (g->goto_bindings.stmt_exprs > 0)
3940 {
3941 error_at (g->loc, "jump into statement expression");
3942 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3943 label);
3944 }
3945 }
3946
3947 /* Now that the label is defined, we will issue warnings about
3948 subsequent gotos to this label when we see them. */
3949 vec_safe_truncate (label_vars->gotos, 0);
3950 label_vars->gotos = NULL;
3951 }
3952
3953 /* Define a label, specifying the location in the source file.
3954 Return the LABEL_DECL node for the label, if the definition is valid.
3955 Otherwise return NULL_TREE. */
3956
3957 tree
3958 define_label (location_t location, tree name)
3959 {
3960 /* Find any preexisting label with this name. It is an error
3961 if that label has already been defined in this function, or
3962 if there is a containing function with a declared label with
3963 the same name. */
3964 tree label = I_LABEL_DECL (name);
3965
3966 if (label
3967 && ((DECL_CONTEXT (label) == current_function_decl
3968 && DECL_INITIAL (label) != NULL_TREE)
3969 || (DECL_CONTEXT (label) != current_function_decl
3970 && C_DECLARED_LABEL_FLAG (label))))
3971 {
3972 auto_diagnostic_group d;
3973 error_at (location, "duplicate label %qD", label);
3974 locate_old_decl (label);
3975 return NULL_TREE;
3976 }
3977 else if (label && DECL_CONTEXT (label) == current_function_decl)
3978 {
3979 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3980
3981 /* The label has been used or declared already in this function,
3982 but not defined. Update its location to point to this
3983 definition. */
3984 DECL_SOURCE_LOCATION (label) = location;
3985 set_spot_bindings (&label_vars->label_bindings, true);
3986
3987 /* Issue warnings as required about any goto statements from
3988 earlier in the function. */
3989 check_earlier_gotos (label, label_vars);
3990 }
3991 else
3992 {
3993 struct c_label_vars *label_vars;
3994
3995 /* No label binding for that identifier; make one. */
3996 label = make_label (location, name, true, &label_vars);
3997
3998 /* Ordinary labels go in the current function scope. */
3999 bind_label (name, label, current_function_scope, label_vars);
4000 }
4001
4002 if (!in_system_header_at (input_location) && lookup_name (name))
4003 warning_at (location, OPT_Wtraditional,
4004 "traditional C lacks a separate namespace "
4005 "for labels, identifier %qE conflicts", name);
4006
4007 /* Mark label as having been defined. */
4008 DECL_INITIAL (label) = error_mark_node;
4009 return label;
4010 }
4011 \f
4012 /* Get the bindings for a new switch statement. This is used to issue
4013 warnings as appropriate for jumps from the switch to case or
4014 default labels. */
4015
4016 struct c_spot_bindings *
4017 c_get_switch_bindings (void)
4018 {
4019 struct c_spot_bindings *switch_bindings;
4020
4021 switch_bindings = XNEW (struct c_spot_bindings);
4022 set_spot_bindings (switch_bindings, true);
4023 return switch_bindings;
4024 }
4025
4026 void
4027 c_release_switch_bindings (struct c_spot_bindings *bindings)
4028 {
4029 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4030 XDELETE (bindings);
4031 }
4032
4033 /* This is called at the point of a case or default label to issue
4034 warnings about decls as needed. It returns true if it found an
4035 error, not just a warning. */
4036
4037 bool
4038 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4039 location_t switch_loc, location_t case_loc)
4040 {
4041 bool saw_error;
4042 struct c_scope *scope;
4043
4044 saw_error = false;
4045 for (scope = current_scope;
4046 scope != switch_bindings->scope;
4047 scope = scope->outer)
4048 {
4049 struct c_binding *b;
4050
4051 gcc_assert (scope != NULL);
4052
4053 if (!scope->has_jump_unsafe_decl)
4054 continue;
4055
4056 for (b = scope->bindings; b != NULL; b = b->prev)
4057 {
4058 if (decl_jump_unsafe (b->decl))
4059 {
4060 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
4061 {
4062 saw_error = true;
4063 error_at (case_loc,
4064 ("switch jumps into scope of identifier with "
4065 "variably modified type"));
4066 }
4067 else
4068 warning_at (case_loc, OPT_Wjump_misses_init,
4069 "switch jumps over variable initialization");
4070 inform (switch_loc, "switch starts here");
4071 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4072 b->decl);
4073 }
4074 }
4075 }
4076
4077 if (switch_bindings->stmt_exprs > 0)
4078 {
4079 saw_error = true;
4080 error_at (case_loc, "switch jumps into statement expression");
4081 inform (switch_loc, "switch starts here");
4082 }
4083
4084 return saw_error;
4085 }
4086 \f
4087 /* Given NAME, an IDENTIFIER_NODE,
4088 return the structure (or union or enum) definition for that name.
4089 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4090 CODE says which kind of type the caller wants;
4091 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4092 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4093 location where the tag was defined.
4094 If the wrong kind of type is found, an error is reported. */
4095
4096 static tree
4097 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4098 location_t *ploc)
4099 {
4100 struct c_binding *b = I_TAG_BINDING (name);
4101 bool thislevel = false;
4102
4103 if (!b || !b->decl)
4104 return NULL_TREE;
4105
4106 /* We only care about whether it's in this level if
4107 thislevel_only was set or it might be a type clash. */
4108 if (thislevel_only || TREE_CODE (b->decl) != code)
4109 {
4110 /* For our purposes, a tag in the external scope is the same as
4111 a tag in the file scope. (Primarily relevant to Objective-C
4112 and its builtin structure tags, which get pushed before the
4113 file scope is created.) */
4114 if (B_IN_CURRENT_SCOPE (b)
4115 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4116 thislevel = true;
4117 }
4118
4119 if (thislevel_only && !thislevel)
4120 return NULL_TREE;
4121
4122 if (TREE_CODE (b->decl) != code)
4123 {
4124 /* Definition isn't the kind we were looking for. */
4125 pending_invalid_xref = name;
4126 pending_invalid_xref_location = input_location;
4127
4128 /* If in the same binding level as a declaration as a tag
4129 of a different type, this must not be allowed to
4130 shadow that tag, so give the error immediately.
4131 (For example, "struct foo; union foo;" is invalid.) */
4132 if (thislevel)
4133 pending_xref_error ();
4134 }
4135
4136 if (ploc != NULL)
4137 *ploc = b->locus;
4138
4139 return b->decl;
4140 }
4141
4142 /* Return true if a definition exists for NAME with code CODE. */
4143
4144 bool
4145 tag_exists_p (enum tree_code code, tree name)
4146 {
4147 struct c_binding *b = I_TAG_BINDING (name);
4148
4149 if (b == NULL || b->decl == NULL_TREE)
4150 return false;
4151 return TREE_CODE (b->decl) == code;
4152 }
4153
4154 /* Print an error message now
4155 for a recent invalid struct, union or enum cross reference.
4156 We don't print them immediately because they are not invalid
4157 when used in the `struct foo;' construct for shadowing. */
4158
4159 void
4160 pending_xref_error (void)
4161 {
4162 if (pending_invalid_xref != NULL_TREE)
4163 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4164 pending_invalid_xref);
4165 pending_invalid_xref = NULL_TREE;
4166 }
4167
4168 \f
4169 /* Look up NAME in the current scope and its superiors
4170 in the namespace of variables, functions and typedefs.
4171 Return a ..._DECL node of some kind representing its definition,
4172 or return NULL_TREE if it is undefined. */
4173
4174 tree
4175 lookup_name (tree name)
4176 {
4177 struct c_binding *b = I_SYMBOL_BINDING (name);
4178 if (b && !b->invisible)
4179 {
4180 maybe_record_typedef_use (b->decl);
4181 return b->decl;
4182 }
4183 return NULL_TREE;
4184 }
4185
4186 /* Similar to `lookup_name' but look only at the indicated scope. */
4187
4188 static tree
4189 lookup_name_in_scope (tree name, struct c_scope *scope)
4190 {
4191 struct c_binding *b;
4192
4193 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4194 if (B_IN_SCOPE (b, scope))
4195 return b->decl;
4196 return NULL_TREE;
4197 }
4198
4199 /* Look for the closest match for NAME within the currently valid
4200 scopes.
4201
4202 This finds the identifier with the lowest Levenshtein distance to
4203 NAME. If there are multiple candidates with equal minimal distance,
4204 the first one found is returned. Scopes are searched from innermost
4205 outwards, and within a scope in reverse order of declaration, thus
4206 benefiting candidates "near" to the current scope.
4207
4208 The function also looks for similar macro names to NAME, since a
4209 misspelled macro name will not be expanded, and hence looks like an
4210 identifier to the C frontend.
4211
4212 It also looks for start_typename keywords, to detect "singed" vs "signed"
4213 typos.
4214
4215 Use LOC for any deferred diagnostics. */
4216
4217 name_hint
4218 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4219 {
4220 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4221
4222 /* First, try some well-known names in the C standard library, in case
4223 the user forgot a #include. */
4224 const char *header_hint
4225 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4226
4227 if (header_hint)
4228 return name_hint (NULL,
4229 new suggest_missing_header (loc,
4230 IDENTIFIER_POINTER (name),
4231 header_hint));
4232
4233 /* Only suggest names reserved for the implementation if NAME begins
4234 with an underscore. */
4235 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4236
4237 best_match<tree, tree> bm (name);
4238
4239 /* Look within currently valid scopes. */
4240 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4241 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4242 {
4243 if (!binding->id || binding->invisible)
4244 continue;
4245 if (binding->decl == error_mark_node)
4246 continue;
4247 /* Don't use bindings from implicitly declared functions,
4248 as they were likely misspellings themselves. */
4249 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4250 if (C_DECL_IMPLICIT (binding->decl))
4251 continue;
4252 /* Don't suggest names that are reserved for use by the
4253 implementation, unless NAME began with an underscore. */
4254 if (!consider_implementation_names)
4255 {
4256 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4257 if (name_reserved_for_implementation_p (suggestion_str))
4258 continue;
4259 }
4260 switch (kind)
4261 {
4262 case FUZZY_LOOKUP_TYPENAME:
4263 if (TREE_CODE (binding->decl) != TYPE_DECL)
4264 continue;
4265 break;
4266
4267 case FUZZY_LOOKUP_FUNCTION_NAME:
4268 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4269 {
4270 /* Allow function pointers. */
4271 if ((VAR_P (binding->decl)
4272 || TREE_CODE (binding->decl) == PARM_DECL)
4273 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4274 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4275 == FUNCTION_TYPE))
4276 break;
4277 continue;
4278 }
4279 break;
4280
4281 default:
4282 break;
4283 }
4284 bm.consider (binding->id);
4285 }
4286
4287 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4288 as:
4289 x = SOME_OTHER_MACRO (y);
4290 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4291 as a misspelled identifier.
4292
4293 Use the best distance so far so that a candidate is only set if
4294 a macro is better than anything so far. This allows early rejection
4295 (without calculating the edit distance) of macro names that must have
4296 distance >= bm.get_best_distance (), and means that we only get a
4297 non-NULL result for best_macro_match if it's better than any of
4298 the identifiers already checked, which avoids needless creation
4299 of identifiers for macro hashnodes. */
4300 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4301 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4302 /* If a macro is the closest so far to NAME, use it, creating an
4303 identifier tree node for it. */
4304 if (best_macro)
4305 {
4306 const char *id = (const char *)best_macro->ident.str;
4307 tree macro_as_identifier
4308 = get_identifier_with_length (id, best_macro->ident.len);
4309 bm.set_best_so_far (macro_as_identifier,
4310 bmm.get_best_distance (),
4311 bmm.get_best_candidate_length ());
4312 }
4313
4314 /* Try the "start_typename" keywords to detect
4315 "singed" vs "signed" typos. */
4316 if (kind == FUZZY_LOOKUP_TYPENAME)
4317 {
4318 for (unsigned i = 0; i < num_c_common_reswords; i++)
4319 {
4320 const c_common_resword *resword = &c_common_reswords[i];
4321 if (!c_keyword_starts_typename (resword->rid))
4322 continue;
4323 tree resword_identifier = ridpointers [resword->rid];
4324 if (!resword_identifier)
4325 continue;
4326 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4327 bm.consider (resword_identifier);
4328 }
4329 }
4330
4331 tree best = bm.get_best_meaningful_candidate ();
4332 if (best)
4333 return name_hint (IDENTIFIER_POINTER (best), NULL);
4334 else
4335 return name_hint (NULL, NULL);
4336 }
4337
4338 \f
4339 /* Table of supported standard (C2x) attributes. */
4340 const struct attribute_spec std_attribute_table[] =
4341 {
4342 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4343 affects_type_identity, handler, exclude } */
4344 { "deprecated", 0, 1, false, false, false, false,
4345 handle_deprecated_attribute, NULL },
4346 { "fallthrough", 0, 0, false, false, false, false,
4347 handle_fallthrough_attribute, NULL },
4348 { "maybe_unused", 0, 0, false, false, false, false,
4349 handle_unused_attribute, NULL },
4350 { NULL, 0, 0, false, false, false, false, NULL, NULL }
4351 };
4352
4353 /* Create the predefined scalar types of C,
4354 and some nodes representing standard constants (0, 1, (void *) 0).
4355 Initialize the global scope.
4356 Make definitions for built-in primitive functions. */
4357
4358 void
4359 c_init_decl_processing (void)
4360 {
4361 location_t save_loc = input_location;
4362
4363 /* Initialize reserved words for parser. */
4364 c_parse_init ();
4365
4366 register_scoped_attributes (std_attribute_table, NULL);
4367
4368 current_function_decl = NULL_TREE;
4369
4370 gcc_obstack_init (&parser_obstack);
4371
4372 /* Make the externals scope. */
4373 push_scope ();
4374 external_scope = current_scope;
4375
4376 /* Declarations from c_common_nodes_and_builtins must not be associated
4377 with this input file, lest we get differences between using and not
4378 using preprocessed headers. */
4379 input_location = BUILTINS_LOCATION;
4380
4381 c_common_nodes_and_builtins ();
4382
4383 /* In C, comparisons and TRUTH_* expressions have type int. */
4384 truthvalue_type_node = integer_type_node;
4385 truthvalue_true_node = integer_one_node;
4386 truthvalue_false_node = integer_zero_node;
4387
4388 /* Even in C99, which has a real boolean type. */
4389 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4390 boolean_type_node));
4391
4392 input_location = save_loc;
4393
4394 make_fname_decl = c_make_fname_decl;
4395 start_fname_decls ();
4396 }
4397
4398 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4399 give the decl, NAME is the initialization string and TYPE_DEP
4400 indicates whether NAME depended on the type of the function. As we
4401 don't yet implement delayed emission of static data, we mark the
4402 decl as emitted so it is not placed in the output. Anything using
4403 it must therefore pull out the STRING_CST initializer directly.
4404 FIXME. */
4405
4406 static tree
4407 c_make_fname_decl (location_t loc, tree id, int type_dep)
4408 {
4409 const char *name = fname_as_string (type_dep);
4410 tree decl, type, init;
4411 size_t length = strlen (name);
4412
4413 type = build_array_type (char_type_node,
4414 build_index_type (size_int (length)));
4415 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4416
4417 decl = build_decl (loc, VAR_DECL, id, type);
4418
4419 TREE_STATIC (decl) = 1;
4420 TREE_READONLY (decl) = 1;
4421 DECL_ARTIFICIAL (decl) = 1;
4422
4423 init = build_string (length + 1, name);
4424 free (CONST_CAST (char *, name));
4425 TREE_TYPE (init) = type;
4426 DECL_INITIAL (decl) = init;
4427
4428 TREE_USED (decl) = 1;
4429
4430 if (current_function_decl
4431 /* For invalid programs like this:
4432
4433 void foo()
4434 const char* p = __FUNCTION__;
4435
4436 the __FUNCTION__ is believed to appear in K&R style function
4437 parameter declarator. In that case we still don't have
4438 function_scope. */
4439 && current_function_scope)
4440 {
4441 DECL_CONTEXT (decl) = current_function_decl;
4442 bind (id, decl, current_function_scope,
4443 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4444 }
4445
4446 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4447
4448 return decl;
4449 }
4450
4451 tree
4452 c_builtin_function (tree decl)
4453 {
4454 tree type = TREE_TYPE (decl);
4455 tree id = DECL_NAME (decl);
4456
4457 const char *name = IDENTIFIER_POINTER (id);
4458 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4459
4460 /* Should never be called on a symbol with a preexisting meaning. */
4461 gcc_assert (!I_SYMBOL_BINDING (id));
4462
4463 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4464 UNKNOWN_LOCATION);
4465
4466 /* Builtins in the implementation namespace are made visible without
4467 needing to be explicitly declared. See push_file_scope. */
4468 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4469 {
4470 DECL_CHAIN (decl) = visible_builtins;
4471 visible_builtins = decl;
4472 }
4473
4474 return decl;
4475 }
4476
4477 tree
4478 c_builtin_function_ext_scope (tree decl)
4479 {
4480 tree type = TREE_TYPE (decl);
4481 tree id = DECL_NAME (decl);
4482
4483 const char *name = IDENTIFIER_POINTER (id);
4484 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4485
4486 if (external_scope)
4487 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4488 UNKNOWN_LOCATION);
4489
4490 /* Builtins in the implementation namespace are made visible without
4491 needing to be explicitly declared. See push_file_scope. */
4492 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4493 {
4494 DECL_CHAIN (decl) = visible_builtins;
4495 visible_builtins = decl;
4496 }
4497
4498 return decl;
4499 }
4500
4501 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4502
4503 tree
4504 c_simulate_builtin_function_decl (tree decl)
4505 {
4506 tree type = TREE_TYPE (decl);
4507 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4508 return pushdecl (decl);
4509 }
4510
4511 /* Warn about attributes in a context where they are unused
4512 (attribute-declarations, except for the "fallthrough" case, and
4513 attributes on statements). */
4514
4515 void
4516 c_warn_unused_attributes (tree attrs)
4517 {
4518 for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
4519 if (get_attribute_namespace (t) == NULL_TREE)
4520 /* The specifications of standard attributes mean this is a
4521 constraint violation. */
4522 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4523 get_attribute_name (t));
4524 else
4525 warning (OPT_Wattributes, "%qE attribute ignored",
4526 get_attribute_name (t));
4527 }
4528
4529 /* Warn for standard attributes being applied to a type that is not
4530 being defined, where that is a constraint violation, and return a
4531 list of attributes with them removed. */
4532
4533 tree
4534 c_warn_type_attributes (tree attrs)
4535 {
4536 tree *attr_ptr = &attrs;
4537 while (*attr_ptr)
4538 if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
4539 {
4540 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4541 get_attribute_name (*attr_ptr));
4542 *attr_ptr = TREE_CHAIN (*attr_ptr);
4543 }
4544 else
4545 attr_ptr = &TREE_CHAIN (*attr_ptr);
4546 return attrs;
4547 }
4548 \f
4549 /* Called when a declaration is seen that contains no names to declare.
4550 If its type is a reference to a structure, union or enum inherited
4551 from a containing scope, shadow that tag name for the current scope
4552 with a forward reference.
4553 If its type defines a new named structure or union
4554 or defines an enum, it is valid but we need not do anything here.
4555 Otherwise, it is an error. */
4556
4557 void
4558 shadow_tag (const struct c_declspecs *declspecs)
4559 {
4560 shadow_tag_warned (declspecs, 0);
4561 }
4562
4563 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4564 but no pedwarn. */
4565 void
4566 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4567 {
4568 bool found_tag = false;
4569
4570 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4571 {
4572 tree value = declspecs->type;
4573 enum tree_code code = TREE_CODE (value);
4574
4575 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4576 /* Used to test also that TYPE_SIZE (value) != 0.
4577 That caused warning for `struct foo;' at top level in the file. */
4578 {
4579 tree name = TYPE_NAME (value);
4580 tree t;
4581
4582 found_tag = true;
4583
4584 if (declspecs->restrict_p)
4585 {
4586 error ("invalid use of %<restrict%>");
4587 warned = 1;
4588 }
4589
4590 if (name == NULL_TREE)
4591 {
4592 if (warned != 1 && code != ENUMERAL_TYPE)
4593 /* Empty unnamed enum OK */
4594 {
4595 pedwarn (input_location, 0,
4596 "unnamed struct/union that defines no instances");
4597 warned = 1;
4598 }
4599 }
4600 else if (declspecs->typespec_kind != ctsk_tagdef
4601 && declspecs->typespec_kind != ctsk_tagfirstref
4602 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4603 && declspecs->storage_class != csc_none)
4604 {
4605 if (warned != 1)
4606 pedwarn (input_location, 0,
4607 "empty declaration with storage class specifier "
4608 "does not redeclare tag");
4609 warned = 1;
4610 pending_xref_error ();
4611 }
4612 else if (declspecs->typespec_kind != ctsk_tagdef
4613 && declspecs->typespec_kind != ctsk_tagfirstref
4614 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4615 && (declspecs->const_p
4616 || declspecs->volatile_p
4617 || declspecs->atomic_p
4618 || declspecs->restrict_p
4619 || declspecs->address_space))
4620 {
4621 if (warned != 1)
4622 pedwarn (input_location, 0,
4623 "empty declaration with type qualifier "
4624 "does not redeclare tag");
4625 warned = 1;
4626 pending_xref_error ();
4627 }
4628 else if (declspecs->typespec_kind != ctsk_tagdef
4629 && declspecs->typespec_kind != ctsk_tagfirstref
4630 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4631 && declspecs->alignas_p)
4632 {
4633 if (warned != 1)
4634 pedwarn (input_location, 0,
4635 "empty declaration with %<_Alignas%> "
4636 "does not redeclare tag");
4637 warned = 1;
4638 pending_xref_error ();
4639 }
4640 else
4641 {
4642 pending_invalid_xref = NULL_TREE;
4643 t = lookup_tag (code, name, true, NULL);
4644
4645 if (t == NULL_TREE)
4646 {
4647 t = make_node (code);
4648 pushtag (input_location, name, t);
4649 }
4650 }
4651 }
4652 else
4653 {
4654 if (warned != 1 && !in_system_header_at (input_location))
4655 {
4656 pedwarn (input_location, 0,
4657 "useless type name in empty declaration");
4658 warned = 1;
4659 }
4660 }
4661 }
4662 else if (warned != 1 && !in_system_header_at (input_location)
4663 && declspecs->typedef_p)
4664 {
4665 pedwarn (input_location, 0, "useless type name in empty declaration");
4666 warned = 1;
4667 }
4668
4669 pending_invalid_xref = NULL_TREE;
4670
4671 if (declspecs->inline_p)
4672 {
4673 error ("%<inline%> in empty declaration");
4674 warned = 1;
4675 }
4676
4677 if (declspecs->noreturn_p)
4678 {
4679 error ("%<_Noreturn%> in empty declaration");
4680 warned = 1;
4681 }
4682
4683 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4684 {
4685 error ("%<auto%> in file-scope empty declaration");
4686 warned = 1;
4687 }
4688
4689 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4690 {
4691 error ("%<register%> in file-scope empty declaration");
4692 warned = 1;
4693 }
4694
4695 if (!warned && !in_system_header_at (input_location)
4696 && declspecs->storage_class != csc_none)
4697 {
4698 warning (0, "useless storage class specifier in empty declaration");
4699 warned = 2;
4700 }
4701
4702 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4703 {
4704 warning (0, "useless %qs in empty declaration",
4705 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4706 warned = 2;
4707 }
4708
4709 if (!warned
4710 && !in_system_header_at (input_location)
4711 && (declspecs->const_p
4712 || declspecs->volatile_p
4713 || declspecs->atomic_p
4714 || declspecs->restrict_p
4715 || declspecs->address_space))
4716 {
4717 warning (0, "useless type qualifier in empty declaration");
4718 warned = 2;
4719 }
4720
4721 if (!warned && !in_system_header_at (input_location)
4722 && declspecs->alignas_p)
4723 {
4724 warning (0, "useless %<_Alignas%> in empty declaration");
4725 warned = 2;
4726 }
4727
4728 if (found_tag
4729 && warned == 2
4730 && (declspecs->typespec_kind == ctsk_tagref_attrs
4731 || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
4732 {
4733 /* Standard attributes after the "struct" or "union" keyword are
4734 only permitted when the contents of the type are defined, or
4735 in the form "struct-or-union attribute-specifier-sequence
4736 identifier;". If the ';' was not present, attributes were
4737 diagnosed in the parser. Here, ensure that any other useless
4738 elements of the declaration result in a pedwarn, not just a
4739 warning. Forward declarations of enum types are not part of
4740 standard C, but handle them the same. */
4741 pedwarn (input_location, 0,
4742 "invalid use of attributes in empty declaration");
4743 warned = 1;
4744 }
4745
4746 if (warned != 1)
4747 {
4748 if (declspecs->declspecs_seen_p
4749 && !declspecs->non_std_attrs_seen_p)
4750 /* An attribute declaration (but not a fallthrough attribute
4751 declaration, which was handled separately); warn if there
4752 are any attributes being ignored (but not if the attributes
4753 were empty). */
4754 c_warn_unused_attributes (declspecs->attrs);
4755 else if (!found_tag)
4756 pedwarn (input_location, 0, "empty declaration");
4757 }
4758 }
4759 \f
4760
4761 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4762 bits. SPECS represents declaration specifiers that the grammar
4763 only permits to contain type qualifiers and attributes. */
4764
4765 int
4766 quals_from_declspecs (const struct c_declspecs *specs)
4767 {
4768 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4769 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4770 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4771 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4772 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4773 gcc_assert (!specs->type
4774 && !specs->decl_attr
4775 && specs->typespec_word == cts_none
4776 && specs->storage_class == csc_none
4777 && !specs->typedef_p
4778 && !specs->explicit_signed_p
4779 && !specs->deprecated_p
4780 && !specs->long_p
4781 && !specs->long_long_p
4782 && !specs->short_p
4783 && !specs->signed_p
4784 && !specs->unsigned_p
4785 && !specs->complex_p
4786 && !specs->inline_p
4787 && !specs->noreturn_p
4788 && !specs->thread_p);
4789 return quals;
4790 }
4791
4792 /* Construct an array declarator. LOC is the location of the
4793 beginning of the array (usually the opening brace). EXPR is the
4794 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4795 inside the [] (to be applied to the pointer to which a parameter
4796 array is converted). STATIC_P is true if "static" is inside the
4797 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4798 VLA of unspecified length which is nevertheless a complete type,
4799 false otherwise. The field for the contained declarator is left to
4800 be filled in by set_array_declarator_inner. */
4801
4802 struct c_declarator *
4803 build_array_declarator (location_t loc,
4804 tree expr, struct c_declspecs *quals, bool static_p,
4805 bool vla_unspec_p)
4806 {
4807 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4808 struct c_declarator);
4809 declarator->id_loc = loc;
4810 declarator->kind = cdk_array;
4811 declarator->declarator = 0;
4812 declarator->u.array.dimen = expr;
4813 if (quals)
4814 {
4815 declarator->u.array.attrs = quals->attrs;
4816 declarator->u.array.quals = quals_from_declspecs (quals);
4817 }
4818 else
4819 {
4820 declarator->u.array.attrs = NULL_TREE;
4821 declarator->u.array.quals = 0;
4822 }
4823 declarator->u.array.static_p = static_p;
4824 declarator->u.array.vla_unspec_p = vla_unspec_p;
4825 if (static_p || quals != NULL)
4826 pedwarn_c90 (loc, OPT_Wpedantic,
4827 "ISO C90 does not support %<static%> or type "
4828 "qualifiers in parameter array declarators");
4829 if (vla_unspec_p)
4830 pedwarn_c90 (loc, OPT_Wpedantic,
4831 "ISO C90 does not support %<[*]%> array declarators");
4832 if (vla_unspec_p)
4833 {
4834 if (!current_scope->parm_flag)
4835 {
4836 /* C99 6.7.5.2p4 */
4837 error_at (loc, "%<[*]%> not allowed in other than "
4838 "function prototype scope");
4839 declarator->u.array.vla_unspec_p = false;
4840 return NULL;
4841 }
4842 current_scope->had_vla_unspec = true;
4843 }
4844 return declarator;
4845 }
4846
4847 /* Set the contained declarator of an array declarator. DECL is the
4848 declarator, as constructed by build_array_declarator; INNER is what
4849 appears on the left of the []. */
4850
4851 struct c_declarator *
4852 set_array_declarator_inner (struct c_declarator *decl,
4853 struct c_declarator *inner)
4854 {
4855 decl->declarator = inner;
4856 return decl;
4857 }
4858
4859 /* INIT is a constructor that forms DECL's initializer. If the final
4860 element initializes a flexible array field, add the size of that
4861 initializer to DECL's size. */
4862
4863 static void
4864 add_flexible_array_elts_to_size (tree decl, tree init)
4865 {
4866 tree elt, type;
4867
4868 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4869 return;
4870
4871 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4872 type = TREE_TYPE (elt);
4873 if (TREE_CODE (type) == ARRAY_TYPE
4874 && TYPE_SIZE (type) == NULL_TREE
4875 && TYPE_DOMAIN (type) != NULL_TREE
4876 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4877 {
4878 complete_array_type (&type, elt, false);
4879 DECL_SIZE (decl)
4880 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4881 DECL_SIZE_UNIT (decl)
4882 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4883 }
4884 }
4885 \f
4886 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4887 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4888 before the type name, and set *EXPR_CONST_OPERANDS, if
4889 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4890 appear in a constant expression. */
4891
4892 tree
4893 groktypename (struct c_type_name *type_name, tree *expr,
4894 bool *expr_const_operands)
4895 {
4896 tree type;
4897 tree attrs = type_name->specs->attrs;
4898
4899 type_name->specs->attrs = NULL_TREE;
4900
4901 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4902 false, NULL, &attrs, expr, expr_const_operands,
4903 DEPRECATED_NORMAL);
4904
4905 /* Apply attributes. */
4906 attrs = c_warn_type_attributes (attrs);
4907 decl_attributes (&type, attrs, 0);
4908
4909 return type;
4910 }
4911
4912 /* Wrapper for decl_attributes that adds some implicit attributes
4913 to VAR_DECLs or FUNCTION_DECLs. */
4914
4915 static tree
4916 c_decl_attributes (tree *node, tree attributes, int flags)
4917 {
4918 /* Add implicit "omp declare target" attribute if requested. */
4919 if (current_omp_declare_target_attribute
4920 && ((VAR_P (*node) && is_global_var (*node))
4921 || TREE_CODE (*node) == FUNCTION_DECL))
4922 {
4923 if (VAR_P (*node)
4924 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4925 attributes = tree_cons (get_identifier ("omp declare target implicit"),
4926 NULL_TREE, attributes);
4927 else
4928 {
4929 attributes = tree_cons (get_identifier ("omp declare target"),
4930 NULL_TREE, attributes);
4931 attributes = tree_cons (get_identifier ("omp declare target block"),
4932 NULL_TREE, attributes);
4933 }
4934 }
4935
4936 /* Look up the current declaration with all the attributes merged
4937 so far so that attributes on the current declaration that's
4938 about to be pushed that conflict with the former can be detected,
4939 diagnosed, and rejected as appropriate. */
4940 tree last_decl = lookup_name (DECL_NAME (*node));
4941 if (!last_decl)
4942 last_decl = lookup_name_in_scope (DECL_NAME (*node), external_scope);
4943
4944 return decl_attributes (node, attributes, flags, last_decl);
4945 }
4946
4947
4948 /* Decode a declarator in an ordinary declaration or data definition.
4949 This is called as soon as the type information and variable name
4950 have been parsed, before parsing the initializer if any.
4951 Here we create the ..._DECL node, fill in its type,
4952 and put it on the list of decls for the current context.
4953 The ..._DECL node is returned as the value.
4954
4955 Exception: for arrays where the length is not specified,
4956 the type is left null, to be filled in by `finish_decl'.
4957
4958 Function definitions do not come here; they go to start_function
4959 instead. However, external and forward declarations of functions
4960 do go through here. Structure field declarations are done by
4961 grokfield and not through here. */
4962
4963 tree
4964 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4965 bool initialized, tree attributes)
4966 {
4967 tree decl;
4968 tree tem;
4969 tree expr = NULL_TREE;
4970 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4971
4972 /* An object declared as __attribute__((deprecated)) suppresses
4973 warnings of uses of other deprecated items. */
4974 if (lookup_attribute ("deprecated", attributes))
4975 deprecated_state = DEPRECATED_SUPPRESS;
4976
4977 decl = grokdeclarator (declarator, declspecs,
4978 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4979 deprecated_state);
4980 if (!decl || decl == error_mark_node)
4981 return NULL_TREE;
4982
4983 if (expr)
4984 add_stmt (fold_convert (void_type_node, expr));
4985
4986 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
4987 && TREE_PUBLIC (decl))
4988 warning (OPT_Wmain, "%q+D is usually a function", decl);
4989
4990 if (initialized)
4991 /* Is it valid for this decl to have an initializer at all?
4992 If not, set INITIALIZED to zero, which will indirectly
4993 tell 'finish_decl' to ignore the initializer once it is parsed. */
4994 switch (TREE_CODE (decl))
4995 {
4996 case TYPE_DECL:
4997 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
4998 initialized = false;
4999 break;
5000
5001 case FUNCTION_DECL:
5002 error ("function %qD is initialized like a variable", decl);
5003 initialized = false;
5004 break;
5005
5006 case PARM_DECL:
5007 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5008 error ("parameter %qD is initialized", decl);
5009 initialized = false;
5010 break;
5011
5012 default:
5013 /* Don't allow initializations for incomplete types except for
5014 arrays which might be completed by the initialization. */
5015
5016 /* This can happen if the array size is an undefined macro.
5017 We already gave a warning, so we don't need another one. */
5018 if (TREE_TYPE (decl) == error_mark_node)
5019 initialized = false;
5020 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5021 {
5022 /* A complete type is ok if size is fixed. */
5023
5024 if (!poly_int_tree_p (TYPE_SIZE (TREE_TYPE (decl)))
5025 || C_DECL_VARIABLE_SIZE (decl))
5026 {
5027 error ("variable-sized object may not be initialized");
5028 initialized = false;
5029 }
5030 }
5031 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5032 {
5033 error ("variable %qD has initializer but incomplete type", decl);
5034 initialized = false;
5035 }
5036 else if (C_DECL_VARIABLE_SIZE (decl))
5037 {
5038 /* Although C99 is unclear about whether incomplete arrays
5039 of VLAs themselves count as VLAs, it does not make
5040 sense to permit them to be initialized given that
5041 ordinary VLAs may not be initialized. */
5042 error ("variable-sized object may not be initialized");
5043 initialized = false;
5044 }
5045 }
5046
5047 if (initialized)
5048 {
5049 if (current_scope == file_scope)
5050 TREE_STATIC (decl) = 1;
5051
5052 /* Tell 'pushdecl' this is an initialized decl
5053 even though we don't yet have the initializer expression.
5054 Also tell 'finish_decl' it may store the real initializer. */
5055 DECL_INITIAL (decl) = error_mark_node;
5056 }
5057
5058 /* If this is a function declaration, write a record describing it to the
5059 prototypes file (if requested). */
5060
5061 if (TREE_CODE (decl) == FUNCTION_DECL)
5062 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5063
5064 /* ANSI specifies that a tentative definition which is not merged with
5065 a non-tentative definition behaves exactly like a definition with an
5066 initializer equal to zero. (Section 3.7.2)
5067
5068 -fno-common gives strict ANSI behavior, though this tends to break
5069 a large body of code that grew up without this rule.
5070
5071 Thread-local variables are never common, since there's no entrenched
5072 body of code to break, and it allows more efficient variable references
5073 in the presence of dynamic linking. */
5074
5075 if (VAR_P (decl)
5076 && !initialized
5077 && TREE_PUBLIC (decl)
5078 && !DECL_THREAD_LOCAL_P (decl)
5079 && !flag_no_common)
5080 DECL_COMMON (decl) = 1;
5081
5082 /* Set attributes here so if duplicate decl, will have proper attributes. */
5083 c_decl_attributes (&decl, attributes, 0);
5084
5085 /* Handle gnu_inline attribute. */
5086 if (declspecs->inline_p
5087 && !flag_gnu89_inline
5088 && TREE_CODE (decl) == FUNCTION_DECL
5089 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5090 || current_function_decl))
5091 {
5092 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5093 ;
5094 else if (declspecs->storage_class != csc_static)
5095 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5096 }
5097
5098 if (TREE_CODE (decl) == FUNCTION_DECL
5099 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
5100 {
5101 struct c_declarator *ce = declarator;
5102
5103 if (ce->kind == cdk_pointer)
5104 ce = declarator->declarator;
5105 if (ce->kind == cdk_function)
5106 {
5107 tree args = ce->u.arg_info->parms;
5108 for (; args; args = DECL_CHAIN (args))
5109 {
5110 tree type = TREE_TYPE (args);
5111 if (type && INTEGRAL_TYPE_P (type)
5112 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5113 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
5114 }
5115 }
5116 }
5117
5118 if (TREE_CODE (decl) == FUNCTION_DECL
5119 && DECL_DECLARED_INLINE_P (decl)
5120 && DECL_UNINLINABLE (decl)
5121 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5122 warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5123 decl, "noinline");
5124
5125 /* C99 6.7.4p3: An inline definition of a function with external
5126 linkage shall not contain a definition of a modifiable object
5127 with static storage duration... */
5128 if (VAR_P (decl)
5129 && current_scope != file_scope
5130 && TREE_STATIC (decl)
5131 && !TREE_READONLY (decl)
5132 && DECL_DECLARED_INLINE_P (current_function_decl)
5133 && DECL_EXTERNAL (current_function_decl))
5134 record_inline_static (input_location, current_function_decl,
5135 decl, csi_modifiable);
5136
5137 if (c_dialect_objc ()
5138 && VAR_OR_FUNCTION_DECL_P (decl))
5139 objc_check_global_decl (decl);
5140
5141 /* Add this decl to the current scope.
5142 TEM may equal DECL or it may be a previous decl of the same name. */
5143 tem = pushdecl (decl);
5144
5145 if (initialized && DECL_EXTERNAL (tem))
5146 {
5147 DECL_EXTERNAL (tem) = 0;
5148 TREE_STATIC (tem) = 1;
5149 }
5150
5151 return tem;
5152 }
5153
5154 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5155 DECL or the non-array element type if DECL is an uninitialized array.
5156 If that type has a const member, diagnose this. */
5157
5158 static void
5159 diagnose_uninitialized_cst_member (tree decl, tree type)
5160 {
5161 tree field;
5162 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5163 {
5164 tree field_type;
5165 if (TREE_CODE (field) != FIELD_DECL)
5166 continue;
5167 field_type = strip_array_types (TREE_TYPE (field));
5168
5169 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5170 {
5171 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5172 "uninitialized const member in %qT is invalid in C++",
5173 strip_array_types (TREE_TYPE (decl)));
5174 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5175 }
5176
5177 if (RECORD_OR_UNION_TYPE_P (field_type))
5178 diagnose_uninitialized_cst_member (decl, field_type);
5179 }
5180 }
5181
5182 /* Finish processing of a declaration;
5183 install its initial value.
5184 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5185 If the length of an array type is not known before,
5186 it must be determined now, from the initial value, or it is an error.
5187
5188 INIT_LOC is the location of the initial value. */
5189
5190 void
5191 finish_decl (tree decl, location_t init_loc, tree init,
5192 tree origtype, tree asmspec_tree)
5193 {
5194 tree type;
5195 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5196 const char *asmspec = 0;
5197
5198 /* If a name was specified, get the string. */
5199 if (VAR_OR_FUNCTION_DECL_P (decl)
5200 && DECL_FILE_SCOPE_P (decl))
5201 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5202 if (asmspec_tree)
5203 asmspec = TREE_STRING_POINTER (asmspec_tree);
5204
5205 if (VAR_P (decl)
5206 && TREE_STATIC (decl)
5207 && global_bindings_p ())
5208 /* So decl is a global variable. Record the types it uses
5209 so that we can decide later to emit debug info for them. */
5210 record_types_used_by_current_var_decl (decl);
5211
5212 /* If `start_decl' didn't like having an initialization, ignore it now. */
5213 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5214 init = NULL_TREE;
5215
5216 /* Don't crash if parm is initialized. */
5217 if (TREE_CODE (decl) == PARM_DECL)
5218 init = NULL_TREE;
5219
5220 if (init)
5221 store_init_value (init_loc, decl, init, origtype);
5222
5223 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5224 || TREE_CODE (decl) == FIELD_DECL))
5225 objc_check_decl (decl);
5226
5227 type = TREE_TYPE (decl);
5228
5229 /* Deduce size of array from initialization, if not already known.
5230 This is only needed for an initialization in the current scope;
5231 it must not be done for a file-scope initialization of a
5232 declaration with external linkage, redeclared in an inner scope
5233 with the outer declaration shadowed in an intermediate scope. */
5234 if (TREE_CODE (type) == ARRAY_TYPE
5235 && TYPE_DOMAIN (type) == NULL_TREE
5236 && TREE_CODE (decl) != TYPE_DECL
5237 && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5238 {
5239 bool do_default
5240 = (TREE_STATIC (decl)
5241 /* Even if pedantic, an external linkage array
5242 may have incomplete type at first. */
5243 ? pedantic && !TREE_PUBLIC (decl)
5244 : !DECL_EXTERNAL (decl));
5245 int failure
5246 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5247 do_default);
5248
5249 /* Get the completed type made by complete_array_type. */
5250 type = TREE_TYPE (decl);
5251
5252 switch (failure)
5253 {
5254 case 1:
5255 error ("initializer fails to determine size of %q+D", decl);
5256 break;
5257
5258 case 2:
5259 if (do_default)
5260 error ("array size missing in %q+D", decl);
5261 break;
5262
5263 case 3:
5264 error ("zero or negative size array %q+D", decl);
5265 break;
5266
5267 case 0:
5268 /* For global variables, update the copy of the type that
5269 exists in the binding. */
5270 if (TREE_PUBLIC (decl))
5271 {
5272 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5273 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5274 b_ext = b_ext->shadowed;
5275 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5276 {
5277 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5278 b_ext->u.type = composite_type (b_ext->u.type, type);
5279 else
5280 b_ext->u.type = type;
5281 }
5282 }
5283 break;
5284
5285 default:
5286 gcc_unreachable ();
5287 }
5288
5289 if (DECL_INITIAL (decl))
5290 TREE_TYPE (DECL_INITIAL (decl)) = type;
5291
5292 relayout_decl (decl);
5293 }
5294
5295 /* Look for braced array initializers for character arrays and
5296 recursively convert them into STRING_CSTs. */
5297 if (tree init = DECL_INITIAL (decl))
5298 DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5299
5300 if (VAR_P (decl))
5301 {
5302 if (init && TREE_CODE (init) == CONSTRUCTOR)
5303 add_flexible_array_elts_to_size (decl, init);
5304
5305 complete_flexible_array_elts (DECL_INITIAL (decl));
5306
5307 if (is_global_var (decl))
5308 {
5309 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5310 ? TCTX_THREAD_STORAGE
5311 : TCTX_STATIC_STORAGE);
5312 if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5313 TREE_TYPE (decl) = error_mark_node;
5314 }
5315
5316 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5317 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5318 layout_decl (decl, 0);
5319
5320 if (DECL_SIZE (decl) == NULL_TREE
5321 /* Don't give an error if we already gave one earlier. */
5322 && TREE_TYPE (decl) != error_mark_node
5323 && (TREE_STATIC (decl)
5324 /* A static variable with an incomplete type
5325 is an error if it is initialized.
5326 Also if it is not file scope.
5327 Otherwise, let it through, but if it is not `extern'
5328 then it may cause an error message later. */
5329 ? (DECL_INITIAL (decl) != NULL_TREE
5330 || !DECL_FILE_SCOPE_P (decl))
5331 /* An automatic variable with an incomplete type
5332 is an error. */
5333 : !DECL_EXTERNAL (decl)))
5334 {
5335 error ("storage size of %q+D isn%'t known", decl);
5336 TREE_TYPE (decl) = error_mark_node;
5337 }
5338
5339 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5340 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5341 && DECL_SIZE (decl) == NULL_TREE
5342 && TREE_STATIC (decl))
5343 incomplete_record_decls.safe_push (decl);
5344
5345 if (is_global_var (decl)
5346 && DECL_SIZE (decl) != NULL_TREE
5347 && TREE_TYPE (decl) != error_mark_node)
5348 {
5349 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5350 constant_expression_warning (DECL_SIZE (decl));
5351 else
5352 {
5353 error ("storage size of %q+D isn%'t constant", decl);
5354 TREE_TYPE (decl) = error_mark_node;
5355 }
5356 }
5357
5358 if (TREE_USED (type))
5359 {
5360 TREE_USED (decl) = 1;
5361 DECL_READ_P (decl) = 1;
5362 }
5363 }
5364
5365 /* If this is a function and an assembler name is specified, reset DECL_RTL
5366 so we can give it its new name. Also, update builtin_decl if it
5367 was a normal built-in. */
5368 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5369 {
5370 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5371 set_builtin_user_assembler_name (decl, asmspec);
5372 set_user_assembler_name (decl, asmspec);
5373 }
5374
5375 /* If #pragma weak was used, mark the decl weak now. */
5376 maybe_apply_pragma_weak (decl);
5377
5378 /* Output the assembler code and/or RTL code for variables and functions,
5379 unless the type is an undefined structure or union.
5380 If not, it will get done when the type is completed. */
5381
5382 if (VAR_OR_FUNCTION_DECL_P (decl))
5383 {
5384 /* Determine the ELF visibility. */
5385 if (TREE_PUBLIC (decl))
5386 c_determine_visibility (decl);
5387
5388 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5389 if (c_dialect_objc ())
5390 objc_check_decl (decl);
5391
5392 if (asmspec)
5393 {
5394 /* If this is not a static variable, issue a warning.
5395 It doesn't make any sense to give an ASMSPEC for an
5396 ordinary, non-register local variable. Historically,
5397 GCC has accepted -- but ignored -- the ASMSPEC in
5398 this case. */
5399 if (!DECL_FILE_SCOPE_P (decl)
5400 && VAR_P (decl)
5401 && !C_DECL_REGISTER (decl)
5402 && !TREE_STATIC (decl))
5403 warning (0, "ignoring %<asm%> specifier for non-static local "
5404 "variable %q+D", decl);
5405 else
5406 set_user_assembler_name (decl, asmspec);
5407 }
5408
5409 if (DECL_FILE_SCOPE_P (decl))
5410 {
5411 if (DECL_INITIAL (decl) == NULL_TREE
5412 || DECL_INITIAL (decl) == error_mark_node)
5413 /* Don't output anything
5414 when a tentative file-scope definition is seen.
5415 But at end of compilation, do output code for them. */
5416 DECL_DEFER_OUTPUT (decl) = 1;
5417 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5418 DECL_HARD_REGISTER (decl) = 1;
5419 rest_of_decl_compilation (decl, true, 0);
5420 }
5421 else
5422 {
5423 /* In conjunction with an ASMSPEC, the `register'
5424 keyword indicates that we should place the variable
5425 in a particular register. */
5426 if (asmspec && C_DECL_REGISTER (decl))
5427 {
5428 DECL_HARD_REGISTER (decl) = 1;
5429 /* This cannot be done for a structure with volatile
5430 fields, on which DECL_REGISTER will have been
5431 reset. */
5432 if (!DECL_REGISTER (decl))
5433 error ("cannot put object with volatile field into register");
5434 }
5435
5436 if (TREE_CODE (decl) != FUNCTION_DECL)
5437 {
5438 /* If we're building a variable sized type, and we might be
5439 reachable other than via the top of the current binding
5440 level, then create a new BIND_EXPR so that we deallocate
5441 the object at the right time. */
5442 /* Note that DECL_SIZE can be null due to errors. */
5443 if (DECL_SIZE (decl)
5444 && !TREE_CONSTANT (DECL_SIZE (decl))
5445 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5446 {
5447 tree bind;
5448 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5449 TREE_SIDE_EFFECTS (bind) = 1;
5450 add_stmt (bind);
5451 BIND_EXPR_BODY (bind) = push_stmt_list ();
5452 }
5453 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5454 DECL_EXPR, decl));
5455 }
5456 }
5457
5458
5459 if (!DECL_FILE_SCOPE_P (decl))
5460 {
5461 /* Recompute the RTL of a local array now
5462 if it used to be an incomplete type. */
5463 if (was_incomplete && !is_global_var (decl))
5464 {
5465 /* If we used it already as memory, it must stay in memory. */
5466 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5467 /* If it's still incomplete now, no init will save it. */
5468 if (DECL_SIZE (decl) == NULL_TREE)
5469 DECL_INITIAL (decl) = NULL_TREE;
5470 }
5471 }
5472 }
5473
5474 if (TREE_CODE (decl) == TYPE_DECL)
5475 {
5476 if (!DECL_FILE_SCOPE_P (decl)
5477 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5478 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5479
5480 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5481 }
5482
5483 /* Install a cleanup (aka destructor) if one was given. */
5484 if (VAR_P (decl) && !TREE_STATIC (decl))
5485 {
5486 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5487 if (attr)
5488 {
5489 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5490 tree cleanup_decl = lookup_name (cleanup_id);
5491 tree cleanup;
5492 vec<tree, va_gc> *v;
5493
5494 /* Build "cleanup(&decl)" for the destructor. */
5495 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5496 vec_alloc (v, 1);
5497 v->quick_push (cleanup);
5498 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5499 vNULL, cleanup_decl, v, NULL);
5500 vec_free (v);
5501
5502 /* Don't warn about decl unused; the cleanup uses it. */
5503 TREE_USED (decl) = 1;
5504 TREE_USED (cleanup_decl) = 1;
5505 DECL_READ_P (decl) = 1;
5506
5507 push_cleanup (decl, cleanup, false);
5508 }
5509 }
5510
5511 if (warn_cxx_compat
5512 && VAR_P (decl)
5513 && !DECL_EXTERNAL (decl)
5514 && DECL_INITIAL (decl) == NULL_TREE)
5515 {
5516 type = strip_array_types (type);
5517 if (TREE_READONLY (decl))
5518 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5519 "uninitialized %<const %D%> is invalid in C++", decl);
5520 else if (RECORD_OR_UNION_TYPE_P (type)
5521 && C_TYPE_FIELDS_READONLY (type))
5522 diagnose_uninitialized_cst_member (decl, type);
5523 }
5524
5525 if (flag_openmp
5526 && VAR_P (decl)
5527 && lookup_attribute ("omp declare target implicit",
5528 DECL_ATTRIBUTES (decl)))
5529 {
5530 DECL_ATTRIBUTES (decl)
5531 = remove_attribute ("omp declare target implicit",
5532 DECL_ATTRIBUTES (decl));
5533 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5534 error ("%q+D in declare target directive does not have mappable type",
5535 decl);
5536 else if (!lookup_attribute ("omp declare target",
5537 DECL_ATTRIBUTES (decl))
5538 && !lookup_attribute ("omp declare target link",
5539 DECL_ATTRIBUTES (decl)))
5540 DECL_ATTRIBUTES (decl)
5541 = tree_cons (get_identifier ("omp declare target"),
5542 NULL_TREE, DECL_ATTRIBUTES (decl));
5543 }
5544
5545 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5546 }
5547
5548 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5549 EXPR is NULL or a pointer to an expression that needs to be
5550 evaluated for the side effects of array size expressions in the
5551 parameters. */
5552
5553 tree
5554 grokparm (const struct c_parm *parm, tree *expr)
5555 {
5556 tree attrs = parm->attrs;
5557 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5558 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5559
5560 decl_attributes (&decl, attrs, 0);
5561
5562 return decl;
5563 }
5564
5565 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5566 and push that on the current scope. EXPR is a pointer to an
5567 expression that needs to be evaluated for the side effects of array
5568 size expressions in the parameters. */
5569
5570 void
5571 push_parm_decl (const struct c_parm *parm, tree *expr)
5572 {
5573 tree attrs = parm->attrs;
5574 tree decl;
5575
5576 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5577 &attrs, expr, NULL, DEPRECATED_NORMAL);
5578 if (decl && DECL_P (decl))
5579 DECL_SOURCE_LOCATION (decl) = parm->loc;
5580 decl_attributes (&decl, attrs, 0);
5581
5582 decl = pushdecl (decl);
5583
5584 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5585 }
5586
5587 /* Mark all the parameter declarations to date as forward decls.
5588 Also diagnose use of this extension. */
5589
5590 void
5591 mark_forward_parm_decls (void)
5592 {
5593 struct c_binding *b;
5594
5595 if (pedantic && !current_scope->warned_forward_parm_decls)
5596 {
5597 pedwarn (input_location, OPT_Wpedantic,
5598 "ISO C forbids forward parameter declarations");
5599 current_scope->warned_forward_parm_decls = true;
5600 }
5601
5602 for (b = current_scope->bindings; b; b = b->prev)
5603 if (TREE_CODE (b->decl) == PARM_DECL)
5604 TREE_ASM_WRITTEN (b->decl) = 1;
5605 }
5606 \f
5607 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5608 literal, which may be an incomplete array type completed by the
5609 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5610 literal. NON_CONST is true if the initializers contain something
5611 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
5612 it is the (valid) alignment for this compound literal, as specified
5613 with _Alignas. */
5614
5615 tree
5616 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
5617 unsigned int alignas_align)
5618 {
5619 /* We do not use start_decl here because we have a type, not a declarator;
5620 and do not use finish_decl because the decl should be stored inside
5621 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5622 tree decl;
5623 tree complit;
5624 tree stmt;
5625
5626 if (type == error_mark_node
5627 || init == error_mark_node)
5628 return error_mark_node;
5629
5630 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5631 DECL_EXTERNAL (decl) = 0;
5632 TREE_PUBLIC (decl) = 0;
5633 TREE_STATIC (decl) = (current_scope == file_scope);
5634 DECL_CONTEXT (decl) = current_function_decl;
5635 TREE_USED (decl) = 1;
5636 DECL_READ_P (decl) = 1;
5637 DECL_ARTIFICIAL (decl) = 1;
5638 DECL_IGNORED_P (decl) = 1;
5639 C_DECL_COMPOUND_LITERAL_P (decl) = 1;
5640 TREE_TYPE (decl) = type;
5641 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
5642 if (alignas_align)
5643 {
5644 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
5645 DECL_USER_ALIGN (decl) = 1;
5646 }
5647 store_init_value (loc, decl, init, NULL_TREE);
5648
5649 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5650 {
5651 int failure = complete_array_type (&TREE_TYPE (decl),
5652 DECL_INITIAL (decl), true);
5653 /* If complete_array_type returns 3, it means that the
5654 initial value of the compound literal is empty. Allow it. */
5655 gcc_assert (failure == 0 || failure == 3);
5656
5657 type = TREE_TYPE (decl);
5658 TREE_TYPE (DECL_INITIAL (decl)) = type;
5659 }
5660
5661 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5662 {
5663 c_incomplete_type_error (loc, NULL_TREE, type);
5664 return error_mark_node;
5665 }
5666
5667 if (TREE_STATIC (decl)
5668 && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
5669 return error_mark_node;
5670
5671 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5672 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5673 TREE_SIDE_EFFECTS (complit) = 1;
5674
5675 layout_decl (decl, 0);
5676
5677 if (TREE_STATIC (decl))
5678 {
5679 /* This decl needs a name for the assembler output. */
5680 set_compound_literal_name (decl);
5681 DECL_DEFER_OUTPUT (decl) = 1;
5682 DECL_COMDAT (decl) = 1;
5683 pushdecl (decl);
5684 rest_of_decl_compilation (decl, 1, 0);
5685 }
5686 else if (current_function_decl && !current_scope->parm_flag)
5687 pushdecl (decl);
5688
5689 if (non_const)
5690 {
5691 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5692 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5693 }
5694
5695 return complit;
5696 }
5697
5698 /* Check the type of a compound literal. Here we just check that it
5699 is valid for C++. */
5700
5701 void
5702 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5703 {
5704 if (warn_cxx_compat
5705 && (type_name->specs->typespec_kind == ctsk_tagdef
5706 || type_name->specs->typespec_kind == ctsk_tagfirstref
5707 || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
5708 warning_at (loc, OPT_Wc___compat,
5709 "defining a type in a compound literal is invalid in C++");
5710 }
5711 \f
5712 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5713 replacing with appropriate values if they are invalid. */
5714
5715 static void
5716 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5717 tree orig_name)
5718 {
5719 tree type_mv;
5720 unsigned int max_width;
5721 unsigned HOST_WIDE_INT w;
5722 const char *name = (orig_name
5723 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5724 : _("<anonymous>"));
5725
5726 /* Detect and ignore out of range field width and process valid
5727 field widths. */
5728 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5729 {
5730 error_at (loc, "bit-field %qs width not an integer constant", name);
5731 *width = integer_one_node;
5732 }
5733 else
5734 {
5735 if (TREE_CODE (*width) != INTEGER_CST)
5736 {
5737 *width = c_fully_fold (*width, false, NULL);
5738 if (TREE_CODE (*width) == INTEGER_CST)
5739 pedwarn (loc, OPT_Wpedantic,
5740 "bit-field %qs width not an integer constant expression",
5741 name);
5742 }
5743 if (TREE_CODE (*width) != INTEGER_CST)
5744 {
5745 error_at (loc, "bit-field %qs width not an integer constant", name);
5746 *width = integer_one_node;
5747 }
5748 constant_expression_warning (*width);
5749 if (tree_int_cst_sgn (*width) < 0)
5750 {
5751 error_at (loc, "negative width in bit-field %qs", name);
5752 *width = integer_one_node;
5753 }
5754 else if (integer_zerop (*width) && orig_name)
5755 {
5756 error_at (loc, "zero width for bit-field %qs", name);
5757 *width = integer_one_node;
5758 }
5759 }
5760
5761 /* Detect invalid bit-field type. */
5762 if (TREE_CODE (*type) != INTEGER_TYPE
5763 && TREE_CODE (*type) != BOOLEAN_TYPE
5764 && TREE_CODE (*type) != ENUMERAL_TYPE)
5765 {
5766 error_at (loc, "bit-field %qs has invalid type", name);
5767 *type = unsigned_type_node;
5768 }
5769
5770 if (TYPE_WARN_IF_NOT_ALIGN (*type))
5771 {
5772 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5773 name);
5774 *type = unsigned_type_node;
5775 }
5776
5777 type_mv = TYPE_MAIN_VARIANT (*type);
5778 if (!in_system_header_at (input_location)
5779 && type_mv != integer_type_node
5780 && type_mv != unsigned_type_node
5781 && type_mv != boolean_type_node)
5782 pedwarn_c90 (loc, OPT_Wpedantic,
5783 "type of bit-field %qs is a GCC extension", name);
5784
5785 max_width = TYPE_PRECISION (*type);
5786
5787 if (compare_tree_int (*width, max_width) > 0)
5788 {
5789 error_at (loc, "width of %qs exceeds its type", name);
5790 w = max_width;
5791 *width = build_int_cst (integer_type_node, w);
5792 }
5793 else
5794 w = tree_to_uhwi (*width);
5795
5796 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5797 {
5798 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5799 if (!lt
5800 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5801 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5802 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5803 }
5804 }
5805
5806 \f
5807
5808 /* Print warning about variable length array if necessary. */
5809
5810 static void
5811 warn_variable_length_array (tree name, tree size)
5812 {
5813 if (TREE_CONSTANT (size))
5814 {
5815 if (name)
5816 pedwarn_c90 (input_location, OPT_Wvla,
5817 "ISO C90 forbids array %qE whose size "
5818 "cannot be evaluated", name);
5819 else
5820 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5821 "whose size cannot be evaluated");
5822 }
5823 else
5824 {
5825 if (name)
5826 pedwarn_c90 (input_location, OPT_Wvla,
5827 "ISO C90 forbids variable length array %qE", name);
5828 else
5829 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5830 "length array");
5831 }
5832 }
5833
5834 /* Print warning about defaulting to int if necessary. */
5835
5836 static void
5837 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5838 {
5839 diagnostic_info diagnostic;
5840 va_list ap;
5841 rich_location richloc (line_table, location);
5842
5843 va_start (ap, gmsgid);
5844 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5845 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5846 diagnostic.option_index = opt;
5847 diagnostic_report_diagnostic (global_dc, &diagnostic);
5848 va_end (ap);
5849 }
5850
5851 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5852 considering only those c_declspec_words found in LIST, which
5853 must be terminated by cdw_number_of_elements. */
5854
5855 static location_t
5856 smallest_type_quals_location (const location_t *locations,
5857 const c_declspec_word *list)
5858 {
5859 location_t loc = UNKNOWN_LOCATION;
5860 while (*list != cdw_number_of_elements)
5861 {
5862 location_t newloc = locations[*list];
5863 if (loc == UNKNOWN_LOCATION
5864 || (newloc != UNKNOWN_LOCATION && newloc < loc))
5865 loc = newloc;
5866 list++;
5867 }
5868
5869 return loc;
5870 }
5871
5872 /* Given declspecs and a declarator,
5873 determine the name and type of the object declared
5874 and construct a ..._DECL node for it.
5875 (In one case we can return a ..._TYPE node instead.
5876 For invalid input we sometimes return NULL_TREE.)
5877
5878 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5879
5880 DECL_CONTEXT says which syntactic context this declaration is in:
5881 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5882 FUNCDEF for a function definition. Like NORMAL but a few different
5883 error messages in each case. Return value may be zero meaning
5884 this definition is too screwy to try to parse.
5885 PARM for a parameter declaration (either within a function prototype
5886 or before a function body). Make a PARM_DECL, or return void_type_node.
5887 TYPENAME if for a typename (in a cast or sizeof).
5888 Don't make a DECL node; just return the ..._TYPE node.
5889 FIELD for a struct or union field; make a FIELD_DECL.
5890 INITIALIZED is true if the decl has an initializer.
5891 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5892 representing the width of the bit-field.
5893 DECL_ATTRS points to the list of attributes that should be added to this
5894 decl. Any nested attributes that belong on the decl itself will be
5895 added to this list.
5896 If EXPR is not NULL, any expressions that need to be evaluated as
5897 part of evaluating variably modified types will be stored in *EXPR.
5898 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5899 set to indicate whether operands in *EXPR can be used in constant
5900 expressions.
5901 DEPRECATED_STATE is a deprecated_states value indicating whether
5902 deprecation warnings should be suppressed.
5903
5904 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5905 It may also be so in the PARM case, for a prototype where the
5906 argument type is specified but not the name.
5907
5908 This function is where the complicated C meanings of `static'
5909 and `extern' are interpreted. */
5910
5911 static tree
5912 grokdeclarator (const struct c_declarator *declarator,
5913 struct c_declspecs *declspecs,
5914 enum decl_context decl_context, bool initialized, tree *width,
5915 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5916 enum deprecated_states deprecated_state)
5917 {
5918 tree type = declspecs->type;
5919 bool threadp = declspecs->thread_p;
5920 enum c_storage_class storage_class = declspecs->storage_class;
5921 int constp;
5922 int restrictp;
5923 int volatilep;
5924 int atomicp;
5925 int type_quals = TYPE_UNQUALIFIED;
5926 tree name = NULL_TREE;
5927 bool funcdef_flag = false;
5928 bool funcdef_syntax = false;
5929 bool size_varies = false;
5930 tree decl_attr = declspecs->decl_attr;
5931 int array_ptr_quals = TYPE_UNQUALIFIED;
5932 tree array_ptr_attrs = NULL_TREE;
5933 bool array_parm_static = false;
5934 bool array_parm_vla_unspec_p = false;
5935 tree returned_attrs = NULL_TREE;
5936 tree decl_id_attrs = NULL_TREE;
5937 bool bitfield = width != NULL;
5938 tree element_type;
5939 tree orig_qual_type = NULL;
5940 size_t orig_qual_indirect = 0;
5941 struct c_arg_info *arg_info = 0;
5942 addr_space_t as1, as2, address_space;
5943 location_t loc = UNKNOWN_LOCATION;
5944 tree expr_dummy;
5945 bool expr_const_operands_dummy;
5946 enum c_declarator_kind first_non_attr_kind;
5947 unsigned int alignas_align = 0;
5948
5949 if (TREE_CODE (type) == ERROR_MARK)
5950 return error_mark_node;
5951 if (expr == NULL)
5952 {
5953 expr = &expr_dummy;
5954 expr_dummy = NULL_TREE;
5955 }
5956 if (expr_const_operands == NULL)
5957 expr_const_operands = &expr_const_operands_dummy;
5958
5959 if (declspecs->expr)
5960 {
5961 if (*expr)
5962 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
5963 declspecs->expr);
5964 else
5965 *expr = declspecs->expr;
5966 }
5967 *expr_const_operands = declspecs->expr_const_operands;
5968
5969 if (decl_context == FUNCDEF)
5970 funcdef_flag = true, decl_context = NORMAL;
5971
5972 /* Look inside a declarator for the name being declared
5973 and get it as an IDENTIFIER_NODE, for an error message. */
5974 {
5975 const struct c_declarator *decl = declarator;
5976
5977 first_non_attr_kind = cdk_attrs;
5978 while (decl)
5979 switch (decl->kind)
5980 {
5981 case cdk_array:
5982 loc = decl->id_loc;
5983 /* FALL THRU. */
5984
5985 case cdk_function:
5986 case cdk_pointer:
5987 funcdef_syntax = (decl->kind == cdk_function);
5988 if (first_non_attr_kind == cdk_attrs)
5989 first_non_attr_kind = decl->kind;
5990 decl = decl->declarator;
5991 break;
5992
5993 case cdk_attrs:
5994 decl = decl->declarator;
5995 break;
5996
5997 case cdk_id:
5998 loc = decl->id_loc;
5999 if (decl->u.id.id)
6000 name = decl->u.id.id;
6001 decl_id_attrs = decl->u.id.attrs;
6002 if (first_non_attr_kind == cdk_attrs)
6003 first_non_attr_kind = decl->kind;
6004 decl = 0;
6005 break;
6006
6007 default:
6008 gcc_unreachable ();
6009 }
6010 if (name == NULL_TREE)
6011 {
6012 gcc_assert (decl_context == PARM
6013 || decl_context == TYPENAME
6014 || (decl_context == FIELD
6015 && declarator->kind == cdk_id));
6016 gcc_assert (!initialized);
6017 }
6018 }
6019
6020 /* A function definition's declarator must have the form of
6021 a function declarator. */
6022
6023 if (funcdef_flag && !funcdef_syntax)
6024 return NULL_TREE;
6025
6026 /* If this looks like a function definition, make it one,
6027 even if it occurs where parms are expected.
6028 Then store_parm_decls will reject it and not use it as a parm. */
6029 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6030 decl_context = PARM;
6031
6032 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
6033 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6034
6035 if ((decl_context == NORMAL || decl_context == FIELD)
6036 && current_scope == file_scope
6037 && variably_modified_type_p (type, NULL_TREE))
6038 {
6039 if (name)
6040 error_at (loc, "variably modified %qE at file scope", name);
6041 else
6042 error_at (loc, "variably modified field at file scope");
6043 type = integer_type_node;
6044 }
6045
6046 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6047
6048 /* Diagnose defaulting to "int". */
6049
6050 if (declspecs->default_int_p && !in_system_header_at (input_location))
6051 {
6052 /* Issue a warning if this is an ISO C 99 program or if
6053 -Wreturn-type and this is a function, or if -Wimplicit;
6054 prefer the former warning since it is more explicit. */
6055 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6056 && funcdef_flag)
6057 warn_about_return_type = 1;
6058 else
6059 {
6060 if (name)
6061 warn_defaults_to (loc, OPT_Wimplicit_int,
6062 "type defaults to %<int%> in declaration "
6063 "of %qE", name);
6064 else
6065 warn_defaults_to (loc, OPT_Wimplicit_int,
6066 "type defaults to %<int%> in type name");
6067 }
6068 }
6069
6070 /* Adjust the type if a bit-field is being declared,
6071 -funsigned-bitfields applied and the type is not explicitly
6072 "signed". */
6073 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6074 && TREE_CODE (type) == INTEGER_TYPE)
6075 type = unsigned_type_for (type);
6076
6077 /* Figure out the type qualifiers for the declaration. There are
6078 two ways a declaration can become qualified. One is something
6079 like `const int i' where the `const' is explicit. Another is
6080 something like `typedef const int CI; CI i' where the type of the
6081 declaration contains the `const'. A third possibility is that
6082 there is a type qualifier on the element type of a typedefed
6083 array type, in which case we should extract that qualifier so
6084 that c_apply_type_quals_to_decl receives the full list of
6085 qualifiers to work with (C90 is not entirely clear about whether
6086 duplicate qualifiers should be diagnosed in this case, but it
6087 seems most appropriate to do so). */
6088 element_type = strip_array_types (type);
6089 constp = declspecs->const_p + TYPE_READONLY (element_type);
6090 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6091 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6092 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6093 as1 = declspecs->address_space;
6094 as2 = TYPE_ADDR_SPACE (element_type);
6095 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6096
6097 if (constp > 1)
6098 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
6099 if (restrictp > 1)
6100 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
6101 if (volatilep > 1)
6102 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
6103 if (atomicp > 1)
6104 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
6105
6106 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6107 error_at (loc, "conflicting named address spaces (%s vs %s)",
6108 c_addr_space_name (as1), c_addr_space_name (as2));
6109
6110 if ((TREE_CODE (type) == ARRAY_TYPE
6111 || first_non_attr_kind == cdk_array)
6112 && TYPE_QUALS (element_type))
6113 {
6114 orig_qual_type = type;
6115 type = TYPE_MAIN_VARIANT (type);
6116 }
6117 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6118 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6119 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6120 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6121 | ENCODE_QUAL_ADDR_SPACE (address_space));
6122 if (type_quals != TYPE_QUALS (element_type))
6123 orig_qual_type = NULL_TREE;
6124
6125 /* Applying the _Atomic qualifier to an array type (through the use
6126 of typedefs or typeof) must be detected here. If the qualifier
6127 is introduced later, any appearance of applying it to an array is
6128 actually applying it to an element of that array. */
6129 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
6130 error_at (loc, "%<_Atomic%>-qualified array type");
6131
6132 /* Warn about storage classes that are invalid for certain
6133 kinds of declarations (parameters, typenames, etc.). */
6134
6135 if (funcdef_flag
6136 && (threadp
6137 || storage_class == csc_auto
6138 || storage_class == csc_register
6139 || storage_class == csc_typedef))
6140 {
6141 if (storage_class == csc_auto)
6142 pedwarn (loc,
6143 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
6144 "function definition declared %<auto%>");
6145 if (storage_class == csc_register)
6146 error_at (loc, "function definition declared %<register%>");
6147 if (storage_class == csc_typedef)
6148 error_at (loc, "function definition declared %<typedef%>");
6149 if (threadp)
6150 error_at (loc, "function definition declared %qs",
6151 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6152 threadp = false;
6153 if (storage_class == csc_auto
6154 || storage_class == csc_register
6155 || storage_class == csc_typedef)
6156 storage_class = csc_none;
6157 }
6158 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
6159 {
6160 if (decl_context == PARM && storage_class == csc_register)
6161 ;
6162 else
6163 {
6164 switch (decl_context)
6165 {
6166 case FIELD:
6167 if (name)
6168 error_at (loc, "storage class specified for structure "
6169 "field %qE", name);
6170 else
6171 error_at (loc, "storage class specified for structure field");
6172 break;
6173 case PARM:
6174 if (name)
6175 error_at (loc, "storage class specified for parameter %qE",
6176 name);
6177 else
6178 error_at (loc, "storage class specified for unnamed parameter");
6179 break;
6180 default:
6181 error_at (loc, "storage class specified for typename");
6182 break;
6183 }
6184 storage_class = csc_none;
6185 threadp = false;
6186 }
6187 }
6188 else if (storage_class == csc_extern
6189 && initialized
6190 && !funcdef_flag)
6191 {
6192 /* 'extern' with initialization is invalid if not at file scope. */
6193 if (current_scope == file_scope)
6194 {
6195 /* It is fine to have 'extern const' when compiling at C
6196 and C++ intersection. */
6197 if (!(warn_cxx_compat && constp))
6198 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
6199 name);
6200 }
6201 else
6202 error_at (loc, "%qE has both %<extern%> and initializer", name);
6203 }
6204 else if (current_scope == file_scope)
6205 {
6206 if (storage_class == csc_auto)
6207 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
6208 name);
6209 if (pedantic && storage_class == csc_register)
6210 pedwarn (input_location, OPT_Wpedantic,
6211 "file-scope declaration of %qE specifies %<register%>", name);
6212 }
6213 else
6214 {
6215 if (storage_class == csc_extern && funcdef_flag)
6216 error_at (loc, "nested function %qE declared %<extern%>", name);
6217 else if (threadp && storage_class == csc_none)
6218 {
6219 error_at (loc, "function-scope %qE implicitly auto and declared "
6220 "%qs", name,
6221 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6222 threadp = false;
6223 }
6224 }
6225
6226 /* Now figure out the structure of the declarator proper.
6227 Descend through it, creating more complex types, until we reach
6228 the declared identifier (or NULL_TREE, in an absolute declarator).
6229 At each stage we maintain an unqualified version of the type
6230 together with any qualifiers that should be applied to it with
6231 c_build_qualified_type; this way, array types including
6232 multidimensional array types are first built up in unqualified
6233 form and then the qualified form is created with
6234 TYPE_MAIN_VARIANT pointing to the unqualified form. */
6235
6236 while (declarator && declarator->kind != cdk_id)
6237 {
6238 if (type == error_mark_node)
6239 {
6240 declarator = declarator->declarator;
6241 continue;
6242 }
6243
6244 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
6245 a cdk_pointer (for *...),
6246 a cdk_function (for ...(...)),
6247 a cdk_attrs (for nested attributes),
6248 or a cdk_id (for the name being declared
6249 or the place in an absolute declarator
6250 where the name was omitted).
6251 For the last case, we have just exited the loop.
6252
6253 At this point, TYPE is the type of elements of an array,
6254 or for a function to return, or for a pointer to point to.
6255 After this sequence of ifs, TYPE is the type of the
6256 array or function or pointer, and DECLARATOR has had its
6257 outermost layer removed. */
6258
6259 if (array_ptr_quals != TYPE_UNQUALIFIED
6260 || array_ptr_attrs != NULL_TREE
6261 || array_parm_static)
6262 {
6263 /* Only the innermost declarator (making a parameter be of
6264 array type which is converted to pointer type)
6265 may have static or type qualifiers. */
6266 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6267 array_ptr_quals = TYPE_UNQUALIFIED;
6268 array_ptr_attrs = NULL_TREE;
6269 array_parm_static = false;
6270 }
6271
6272 switch (declarator->kind)
6273 {
6274 case cdk_attrs:
6275 {
6276 /* A declarator with embedded attributes. */
6277 tree attrs = declarator->u.attrs;
6278 const struct c_declarator *inner_decl;
6279 int attr_flags = 0;
6280 declarator = declarator->declarator;
6281 /* Standard attribute syntax precisely defines what entity
6282 an attribute in each position appertains to, so only
6283 apply laxity about positioning to GNU attribute syntax.
6284 Standard attributes applied to a function or array
6285 declarator apply exactly to that type; standard
6286 attributes applied to the identifier apply to the
6287 declaration rather than to the type, and are specified
6288 using a cdk_id declarator rather than using
6289 cdk_attrs. */
6290 inner_decl = declarator;
6291 while (inner_decl->kind == cdk_attrs)
6292 inner_decl = inner_decl->declarator;
6293 if (!cxx11_attribute_p (attrs))
6294 {
6295 if (inner_decl->kind == cdk_id)
6296 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6297 else if (inner_decl->kind == cdk_function)
6298 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6299 else if (inner_decl->kind == cdk_array)
6300 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6301 }
6302 attrs = c_warn_type_attributes (attrs);
6303 returned_attrs = decl_attributes (&type,
6304 chainon (returned_attrs, attrs),
6305 attr_flags);
6306 break;
6307 }
6308 case cdk_array:
6309 {
6310 tree itype = NULL_TREE;
6311 tree size = declarator->u.array.dimen;
6312 /* The index is a signed object `sizetype' bits wide. */
6313 tree index_type = c_common_signed_type (sizetype);
6314
6315 array_ptr_quals = declarator->u.array.quals;
6316 array_ptr_attrs = declarator->u.array.attrs;
6317 array_parm_static = declarator->u.array.static_p;
6318 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6319
6320 declarator = declarator->declarator;
6321
6322 /* Check for some types that there cannot be arrays of. */
6323
6324 if (VOID_TYPE_P (type))
6325 {
6326 if (name)
6327 error_at (loc, "declaration of %qE as array of voids", name);
6328 else
6329 error_at (loc, "declaration of type name as array of voids");
6330 type = error_mark_node;
6331 }
6332
6333 if (TREE_CODE (type) == FUNCTION_TYPE)
6334 {
6335 if (name)
6336 error_at (loc, "declaration of %qE as array of functions",
6337 name);
6338 else
6339 error_at (loc, "declaration of type name as array of "
6340 "functions");
6341 type = error_mark_node;
6342 }
6343
6344 if (pedantic && !in_system_header_at (input_location)
6345 && flexible_array_type_p (type))
6346 pedwarn (loc, OPT_Wpedantic,
6347 "invalid use of structure with flexible array member");
6348
6349 if (size == error_mark_node)
6350 type = error_mark_node;
6351
6352 if (type == error_mark_node)
6353 continue;
6354
6355 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
6356 {
6357 type = error_mark_node;
6358 continue;
6359 }
6360
6361 /* If size was specified, set ITYPE to a range-type for
6362 that size. Otherwise, ITYPE remains null. finish_decl
6363 may figure it out from an initial value. */
6364
6365 if (size)
6366 {
6367 bool size_maybe_const = true;
6368 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6369 && !TREE_OVERFLOW (size));
6370 bool this_size_varies = false;
6371
6372 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6373 lvalue. */
6374 STRIP_TYPE_NOPS (size);
6375
6376 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6377 {
6378 if (name)
6379 error_at (loc, "size of array %qE has non-integer type",
6380 name);
6381 else
6382 error_at (loc,
6383 "size of unnamed array has non-integer type");
6384 size = integer_one_node;
6385 }
6386 /* This can happen with enum forward declaration. */
6387 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6388 {
6389 if (name)
6390 error_at (loc, "size of array %qE has incomplete type",
6391 name);
6392 else
6393 error_at (loc, "size of unnamed array has incomplete "
6394 "type");
6395 size = integer_one_node;
6396 }
6397
6398 size = c_fully_fold (size, false, &size_maybe_const);
6399
6400 if (pedantic && size_maybe_const && integer_zerop (size))
6401 {
6402 if (name)
6403 pedwarn (loc, OPT_Wpedantic,
6404 "ISO C forbids zero-size array %qE", name);
6405 else
6406 pedwarn (loc, OPT_Wpedantic,
6407 "ISO C forbids zero-size array");
6408 }
6409
6410 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6411 {
6412 constant_expression_warning (size);
6413 if (tree_int_cst_sgn (size) < 0)
6414 {
6415 if (name)
6416 error_at (loc, "size of array %qE is negative", name);
6417 else
6418 error_at (loc, "size of unnamed array is negative");
6419 size = integer_one_node;
6420 }
6421 /* Handle a size folded to an integer constant but
6422 not an integer constant expression. */
6423 if (!size_int_const)
6424 {
6425 /* If this is a file scope declaration of an
6426 ordinary identifier, this is invalid code;
6427 diagnosing it here and not subsequently
6428 treating the type as variable-length avoids
6429 more confusing diagnostics later. */
6430 if ((decl_context == NORMAL || decl_context == FIELD)
6431 && current_scope == file_scope)
6432 pedwarn (input_location, 0,
6433 "variably modified %qE at file scope",
6434 name);
6435 else
6436 this_size_varies = size_varies = true;
6437 warn_variable_length_array (name, size);
6438 }
6439 }
6440 else if ((decl_context == NORMAL || decl_context == FIELD)
6441 && current_scope == file_scope)
6442 {
6443 error_at (loc, "variably modified %qE at file scope", name);
6444 size = integer_one_node;
6445 }
6446 else
6447 {
6448 /* Make sure the array size remains visibly
6449 nonconstant even if it is (eg) a const variable
6450 with known value. */
6451 this_size_varies = size_varies = true;
6452 warn_variable_length_array (name, size);
6453 if (sanitize_flags_p (SANITIZE_VLA)
6454 && current_function_decl != NULL_TREE
6455 && decl_context == NORMAL)
6456 {
6457 /* Evaluate the array size only once. */
6458 size = save_expr (size);
6459 size = c_fully_fold (size, false, NULL);
6460 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6461 ubsan_instrument_vla (loc, size),
6462 size);
6463 }
6464 }
6465
6466 if (integer_zerop (size) && !this_size_varies)
6467 {
6468 /* A zero-length array cannot be represented with
6469 an unsigned index type, which is what we'll
6470 get with build_index_type. Create an
6471 open-ended range instead. */
6472 itype = build_range_type (sizetype, size, NULL_TREE);
6473 }
6474 else
6475 {
6476 /* Arrange for the SAVE_EXPR on the inside of the
6477 MINUS_EXPR, which allows the -1 to get folded
6478 with the +1 that happens when building TYPE_SIZE. */
6479 if (size_varies)
6480 size = save_expr (size);
6481 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6482 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6483 integer_zero_node, size);
6484
6485 /* Compute the maximum valid index, that is, size
6486 - 1. Do the calculation in index_type, so that
6487 if it is a variable the computations will be
6488 done in the proper mode. */
6489 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6490 convert (index_type, size),
6491 convert (index_type,
6492 size_one_node));
6493
6494 /* The above overflows when size does not fit
6495 in index_type.
6496 ??? While a size of INT_MAX+1 technically shouldn't
6497 cause an overflow (because we subtract 1), handling
6498 this case seems like an unnecessary complication. */
6499 if (TREE_CODE (size) == INTEGER_CST
6500 && !int_fits_type_p (size, index_type))
6501 {
6502 if (name)
6503 error_at (loc, "size of array %qE is too large",
6504 name);
6505 else
6506 error_at (loc, "size of unnamed array is too large");
6507 type = error_mark_node;
6508 continue;
6509 }
6510
6511 itype = build_index_type (itype);
6512 }
6513 if (this_size_varies)
6514 {
6515 if (*expr)
6516 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6517 *expr, size);
6518 else
6519 *expr = size;
6520 *expr_const_operands &= size_maybe_const;
6521 }
6522 }
6523 else if (decl_context == FIELD)
6524 {
6525 bool flexible_array_member = false;
6526 if (array_parm_vla_unspec_p)
6527 /* Field names can in fact have function prototype
6528 scope so [*] is disallowed here through making
6529 the field variably modified, not through being
6530 something other than a declaration with function
6531 prototype scope. */
6532 size_varies = true;
6533 else
6534 {
6535 const struct c_declarator *t = declarator;
6536 while (t->kind == cdk_attrs)
6537 t = t->declarator;
6538 flexible_array_member = (t->kind == cdk_id);
6539 }
6540 if (flexible_array_member
6541 && !in_system_header_at (input_location))
6542 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6543 "support flexible array members");
6544
6545 /* ISO C99 Flexible array members are effectively
6546 identical to GCC's zero-length array extension. */
6547 if (flexible_array_member || array_parm_vla_unspec_p)
6548 itype = build_range_type (sizetype, size_zero_node,
6549 NULL_TREE);
6550 }
6551 else if (decl_context == PARM)
6552 {
6553 if (array_parm_vla_unspec_p)
6554 {
6555 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6556 size_varies = true;
6557 }
6558 }
6559 else if (decl_context == TYPENAME)
6560 {
6561 if (array_parm_vla_unspec_p)
6562 {
6563 /* C99 6.7.5.2p4 */
6564 warning (0, "%<[*]%> not in a declaration");
6565 /* We use this to avoid messing up with incomplete
6566 array types of the same type, that would
6567 otherwise be modified below. */
6568 itype = build_range_type (sizetype, size_zero_node,
6569 NULL_TREE);
6570 size_varies = true;
6571 }
6572 }
6573
6574 /* Complain about arrays of incomplete types. */
6575 if (!COMPLETE_TYPE_P (type))
6576 {
6577 error_at (loc, "array type has incomplete element type %qT",
6578 type);
6579 /* See if we can be more helpful. */
6580 if (TREE_CODE (type) == ARRAY_TYPE)
6581 {
6582 if (name)
6583 inform (loc, "declaration of %qE as multidimensional "
6584 "array must have bounds for all dimensions "
6585 "except the first", name);
6586 else
6587 inform (loc, "declaration of multidimensional array "
6588 "must have bounds for all dimensions except "
6589 "the first");
6590 }
6591 type = error_mark_node;
6592 }
6593 else
6594 /* When itype is NULL, a shared incomplete array type is
6595 returned for all array of a given type. Elsewhere we
6596 make sure we don't complete that type before copying
6597 it, but here we want to make sure we don't ever
6598 modify the shared type, so we gcc_assert (itype)
6599 below. */
6600 {
6601 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6602 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6603 type = build_qualified_type (type,
6604 ENCODE_QUAL_ADDR_SPACE (as));
6605
6606 type = build_array_type (type, itype);
6607 }
6608
6609 if (type != error_mark_node)
6610 {
6611 if (size_varies)
6612 {
6613 /* It is ok to modify type here even if itype is
6614 NULL: if size_varies, we're in a
6615 multi-dimensional array and the inner type has
6616 variable size, so the enclosing shared array type
6617 must too. */
6618 if (size && TREE_CODE (size) == INTEGER_CST)
6619 type
6620 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6621 C_TYPE_VARIABLE_SIZE (type) = 1;
6622 }
6623
6624 /* The GCC extension for zero-length arrays differs from
6625 ISO flexible array members in that sizeof yields
6626 zero. */
6627 if (size && integer_zerop (size))
6628 {
6629 gcc_assert (itype);
6630 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6631 TYPE_SIZE (type) = bitsize_zero_node;
6632 TYPE_SIZE_UNIT (type) = size_zero_node;
6633 SET_TYPE_STRUCTURAL_EQUALITY (type);
6634 }
6635 if (array_parm_vla_unspec_p)
6636 {
6637 gcc_assert (itype);
6638 /* The type is complete. C99 6.7.5.2p4 */
6639 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6640 TYPE_SIZE (type) = bitsize_zero_node;
6641 TYPE_SIZE_UNIT (type) = size_zero_node;
6642 SET_TYPE_STRUCTURAL_EQUALITY (type);
6643 }
6644
6645 if (!valid_array_size_p (loc, type, name))
6646 type = error_mark_node;
6647 }
6648
6649 if (decl_context != PARM
6650 && (array_ptr_quals != TYPE_UNQUALIFIED
6651 || array_ptr_attrs != NULL_TREE
6652 || array_parm_static))
6653 {
6654 error_at (loc, "static or type qualifiers in non-parameter "
6655 "array declarator");
6656 array_ptr_quals = TYPE_UNQUALIFIED;
6657 array_ptr_attrs = NULL_TREE;
6658 array_parm_static = false;
6659 }
6660 orig_qual_indirect++;
6661 break;
6662 }
6663 case cdk_function:
6664 {
6665 /* Say it's a definition only for the declarator closest
6666 to the identifier, apart possibly from some
6667 attributes. */
6668 bool really_funcdef = false;
6669 tree arg_types;
6670 orig_qual_type = NULL_TREE;
6671 if (funcdef_flag)
6672 {
6673 const struct c_declarator *t = declarator->declarator;
6674 while (t->kind == cdk_attrs)
6675 t = t->declarator;
6676 really_funcdef = (t->kind == cdk_id);
6677 }
6678
6679 /* Declaring a function type. Make sure we have a valid
6680 type for the function to return. */
6681 if (type == error_mark_node)
6682 continue;
6683
6684 size_varies = false;
6685
6686 /* Warn about some types functions can't return. */
6687 if (TREE_CODE (type) == FUNCTION_TYPE)
6688 {
6689 if (name)
6690 error_at (loc, "%qE declared as function returning a "
6691 "function", name);
6692 else
6693 error_at (loc, "type name declared as function "
6694 "returning a function");
6695 type = integer_type_node;
6696 }
6697 if (TREE_CODE (type) == ARRAY_TYPE)
6698 {
6699 if (name)
6700 error_at (loc, "%qE declared as function returning an array",
6701 name);
6702 else
6703 error_at (loc, "type name declared as function returning "
6704 "an array");
6705 type = integer_type_node;
6706 }
6707
6708 /* Construct the function type and go to the next
6709 inner layer of declarator. */
6710 arg_info = declarator->u.arg_info;
6711 arg_types = grokparms (arg_info, really_funcdef);
6712
6713 /* Type qualifiers before the return type of the function
6714 qualify the return type, not the function type. */
6715 if (type_quals)
6716 {
6717 const enum c_declspec_word ignored_quals_list[] =
6718 {
6719 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
6720 cdw_atomic, cdw_number_of_elements
6721 };
6722 location_t specs_loc
6723 = smallest_type_quals_location (declspecs->locations,
6724 ignored_quals_list);
6725 if (specs_loc == UNKNOWN_LOCATION)
6726 specs_loc = declspecs->locations[cdw_typedef];
6727 if (specs_loc == UNKNOWN_LOCATION)
6728 specs_loc = loc;
6729
6730 /* Type qualifiers on a function return type are
6731 normally permitted by the standard but have no
6732 effect, so give a warning at -Wreturn-type.
6733 Qualifiers on a void return type are banned on
6734 function definitions in ISO C; GCC used to used
6735 them for noreturn functions. The resolution of C11
6736 DR#423 means qualifiers (other than _Atomic) are
6737 actually removed from the return type when
6738 determining the function type. */
6739 int quals_used = type_quals;
6740 if (flag_isoc11)
6741 quals_used &= TYPE_QUAL_ATOMIC;
6742 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6743 pedwarn (specs_loc, 0,
6744 "function definition has qualified void "
6745 "return type");
6746 else
6747 warning_at (specs_loc, OPT_Wignored_qualifiers,
6748 "type qualifiers ignored on function "
6749 "return type");
6750
6751 /* Ensure an error for restrict on invalid types; the
6752 DR#423 resolution is not entirely clear about
6753 this. */
6754 if (flag_isoc11
6755 && (type_quals & TYPE_QUAL_RESTRICT)
6756 && (!POINTER_TYPE_P (type)
6757 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6758 error_at (loc, "invalid use of %<restrict%>");
6759 type = c_build_qualified_type (type, quals_used);
6760 }
6761 type_quals = TYPE_UNQUALIFIED;
6762
6763 type = build_function_type (type, arg_types);
6764 declarator = declarator->declarator;
6765
6766 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6767 the formal parameter list of this FUNCTION_TYPE to point to
6768 the FUNCTION_TYPE node itself. */
6769 {
6770 c_arg_tag *tag;
6771 unsigned ix;
6772
6773 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6774 TYPE_CONTEXT (tag->type) = type;
6775 }
6776 break;
6777 }
6778 case cdk_pointer:
6779 {
6780 /* Merge any constancy or volatility into the target type
6781 for the pointer. */
6782 if ((type_quals & TYPE_QUAL_ATOMIC)
6783 && TREE_CODE (type) == FUNCTION_TYPE)
6784 {
6785 error_at (loc,
6786 "%<_Atomic%>-qualified function type");
6787 type_quals &= ~TYPE_QUAL_ATOMIC;
6788 }
6789 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6790 && type_quals)
6791 pedwarn (loc, OPT_Wpedantic,
6792 "ISO C forbids qualified function types");
6793 if (type_quals)
6794 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6795 orig_qual_indirect);
6796 orig_qual_type = NULL_TREE;
6797 size_varies = false;
6798
6799 /* When the pointed-to type involves components of variable size,
6800 care must be taken to ensure that the size evaluation code is
6801 emitted early enough to dominate all the possible later uses
6802 and late enough for the variables on which it depends to have
6803 been assigned.
6804
6805 This is expected to happen automatically when the pointed-to
6806 type has a name/declaration of it's own, but special attention
6807 is required if the type is anonymous.
6808
6809 We attach an artificial TYPE_DECL to such pointed-to type
6810 and arrange for it to be included in a DECL_EXPR. This
6811 forces the sizes evaluation at a safe point and ensures it
6812 is not deferred until e.g. within a deeper conditional context.
6813
6814 PARM contexts have no enclosing statement list that
6815 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6816 instead, and add it to the list of expressions that
6817 need to be evaluated.
6818
6819 TYPENAME contexts do have an enclosing statement list,
6820 but it would be incorrect to use it, as the size should
6821 only be evaluated if the containing expression is
6822 evaluated. We might also be in the middle of an
6823 expression with side effects on the pointed-to type size
6824 "arguments" prior to the pointer declaration point and
6825 the fake TYPE_DECL in the enclosing context would force
6826 the size evaluation prior to the side effects. We therefore
6827 use BIND_EXPRs in TYPENAME contexts too. */
6828 if (!TYPE_NAME (type)
6829 && variably_modified_type_p (type, NULL_TREE))
6830 {
6831 tree bind = NULL_TREE;
6832 if (decl_context == TYPENAME || decl_context == PARM)
6833 {
6834 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
6835 NULL_TREE, NULL_TREE);
6836 TREE_SIDE_EFFECTS (bind) = 1;
6837 BIND_EXPR_BODY (bind) = push_stmt_list ();
6838 push_scope ();
6839 }
6840 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6841 DECL_ARTIFICIAL (decl) = 1;
6842 pushdecl (decl);
6843 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6844 TYPE_NAME (type) = decl;
6845 if (bind)
6846 {
6847 pop_scope ();
6848 BIND_EXPR_BODY (bind)
6849 = pop_stmt_list (BIND_EXPR_BODY (bind));
6850 if (*expr)
6851 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
6852 bind);
6853 else
6854 *expr = bind;
6855 }
6856 }
6857
6858 type = c_build_pointer_type (type);
6859
6860 /* Process type qualifiers (such as const or volatile)
6861 that were given inside the `*'. */
6862 type_quals = declarator->u.pointer_quals;
6863
6864 declarator = declarator->declarator;
6865 break;
6866 }
6867 default:
6868 gcc_unreachable ();
6869 }
6870 }
6871 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6872 *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
6873
6874 /* Now TYPE has the actual type, apart from any qualifiers in
6875 TYPE_QUALS. */
6876
6877 /* Warn about address space used for things other than static memory or
6878 pointers. */
6879 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6880 if (!ADDR_SPACE_GENERIC_P (address_space))
6881 {
6882 if (decl_context == NORMAL)
6883 {
6884 switch (storage_class)
6885 {
6886 case csc_auto:
6887 error ("%qs combined with %<auto%> qualifier for %qE",
6888 c_addr_space_name (address_space), name);
6889 break;
6890 case csc_register:
6891 error ("%qs combined with %<register%> qualifier for %qE",
6892 c_addr_space_name (address_space), name);
6893 break;
6894 case csc_none:
6895 if (current_function_scope)
6896 {
6897 error ("%qs specified for auto variable %qE",
6898 c_addr_space_name (address_space), name);
6899 break;
6900 }
6901 break;
6902 case csc_static:
6903 case csc_extern:
6904 case csc_typedef:
6905 break;
6906 default:
6907 gcc_unreachable ();
6908 }
6909 }
6910 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6911 {
6912 if (name)
6913 error ("%qs specified for parameter %qE",
6914 c_addr_space_name (address_space), name);
6915 else
6916 error ("%qs specified for unnamed parameter",
6917 c_addr_space_name (address_space));
6918 }
6919 else if (decl_context == FIELD)
6920 {
6921 if (name)
6922 error ("%qs specified for structure field %qE",
6923 c_addr_space_name (address_space), name);
6924 else
6925 error ("%qs specified for structure field",
6926 c_addr_space_name (address_space));
6927 }
6928 }
6929
6930 /* Check the type and width of a bit-field. */
6931 if (bitfield)
6932 {
6933 check_bitfield_type_and_width (loc, &type, width, name);
6934 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6935 atomic types are permitted for bit-fields; we have no code to
6936 make bit-field accesses atomic, so disallow them. */
6937 if (type_quals & TYPE_QUAL_ATOMIC)
6938 {
6939 if (name)
6940 error_at (loc, "bit-field %qE has atomic type", name);
6941 else
6942 error_at (loc, "bit-field has atomic type");
6943 type_quals &= ~TYPE_QUAL_ATOMIC;
6944 }
6945 }
6946
6947 /* Reject invalid uses of _Alignas. */
6948 if (declspecs->alignas_p)
6949 {
6950 if (storage_class == csc_typedef)
6951 error_at (loc, "alignment specified for typedef %qE", name);
6952 else if (storage_class == csc_register)
6953 error_at (loc, "alignment specified for %<register%> object %qE",
6954 name);
6955 else if (decl_context == PARM)
6956 {
6957 if (name)
6958 error_at (loc, "alignment specified for parameter %qE", name);
6959 else
6960 error_at (loc, "alignment specified for unnamed parameter");
6961 }
6962 else if (bitfield)
6963 {
6964 if (name)
6965 error_at (loc, "alignment specified for bit-field %qE", name);
6966 else
6967 error_at (loc, "alignment specified for unnamed bit-field");
6968 }
6969 else if (TREE_CODE (type) == FUNCTION_TYPE)
6970 error_at (loc, "alignment specified for function %qE", name);
6971 else if (declspecs->align_log != -1 && TYPE_P (type))
6972 {
6973 alignas_align = 1U << declspecs->align_log;
6974 if (alignas_align < min_align_of_type (type))
6975 {
6976 if (name)
6977 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6978 "alignment of %qE", name);
6979 else
6980 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6981 "alignment of unnamed field");
6982 alignas_align = 0;
6983 }
6984 }
6985 }
6986
6987 /* If this is declaring a typedef name, return a TYPE_DECL. */
6988
6989 if (storage_class == csc_typedef)
6990 {
6991 tree decl;
6992 if ((type_quals & TYPE_QUAL_ATOMIC)
6993 && TREE_CODE (type) == FUNCTION_TYPE)
6994 {
6995 error_at (loc,
6996 "%<_Atomic%>-qualified function type");
6997 type_quals &= ~TYPE_QUAL_ATOMIC;
6998 }
6999 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7000 && type_quals)
7001 pedwarn (loc, OPT_Wpedantic,
7002 "ISO C forbids qualified function types");
7003 if (type_quals)
7004 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7005 orig_qual_indirect);
7006 decl = build_decl (declarator->id_loc,
7007 TYPE_DECL, declarator->u.id.id, type);
7008 if (declspecs->explicit_signed_p)
7009 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7010 if (declspecs->inline_p)
7011 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7012 if (declspecs->noreturn_p)
7013 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7014
7015 if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7016 {
7017 struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7018
7019 if (b != NULL
7020 && b->decl != NULL_TREE
7021 && (B_IN_CURRENT_SCOPE (b)
7022 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7023 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7024 {
7025 auto_diagnostic_group d;
7026 if (warning_at (declarator->id_loc, OPT_Wc___compat,
7027 ("using %qD as both a typedef and a tag is "
7028 "invalid in C++"), decl)
7029 && b->locus != UNKNOWN_LOCATION)
7030 inform (b->locus, "originally defined here");
7031 }
7032 }
7033
7034 return decl;
7035 }
7036
7037 /* If this is a type name (such as, in a cast or sizeof),
7038 compute the type and return it now. */
7039
7040 if (decl_context == TYPENAME)
7041 {
7042 /* Note that the grammar rejects storage classes in typenames
7043 and fields. */
7044 gcc_assert (storage_class == csc_none && !threadp
7045 && !declspecs->inline_p && !declspecs->noreturn_p);
7046 if ((type_quals & TYPE_QUAL_ATOMIC)
7047 && TREE_CODE (type) == FUNCTION_TYPE)
7048 {
7049 error_at (loc,
7050 "%<_Atomic%>-qualified function type");
7051 type_quals &= ~TYPE_QUAL_ATOMIC;
7052 }
7053 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7054 && type_quals)
7055 pedwarn (loc, OPT_Wpedantic,
7056 "ISO C forbids const or volatile function types");
7057 if (type_quals)
7058 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7059 orig_qual_indirect);
7060 return type;
7061 }
7062
7063 if (pedantic && decl_context == FIELD
7064 && variably_modified_type_p (type, NULL_TREE))
7065 {
7066 /* C99 6.7.2.1p8 */
7067 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7068 "have a variably modified type");
7069 }
7070
7071 /* Aside from typedefs and type names (handle above),
7072 `void' at top level (not within pointer)
7073 is allowed only in public variables.
7074 We don't complain about parms either, but that is because
7075 a better error message can be made later. */
7076
7077 if (VOID_TYPE_P (type) && decl_context != PARM
7078 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7079 && (storage_class == csc_extern
7080 || (current_scope == file_scope
7081 && !(storage_class == csc_static
7082 || storage_class == csc_register)))))
7083 {
7084 error_at (loc, "variable or field %qE declared void", name);
7085 type = integer_type_node;
7086 }
7087
7088 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7089 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7090
7091 {
7092 tree decl;
7093
7094 if (decl_context == PARM)
7095 {
7096 tree promoted_type;
7097 bool array_parameter_p = false;
7098
7099 /* A parameter declared as an array of T is really a pointer to T.
7100 One declared as a function is really a pointer to a function. */
7101
7102 if (TREE_CODE (type) == ARRAY_TYPE)
7103 {
7104 /* Transfer const-ness of array into that of type pointed to. */
7105 type = TREE_TYPE (type);
7106 if (orig_qual_type != NULL_TREE)
7107 {
7108 if (orig_qual_indirect == 0)
7109 orig_qual_type = TREE_TYPE (orig_qual_type);
7110 else
7111 orig_qual_indirect--;
7112 }
7113 if (type_quals)
7114 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7115 orig_qual_indirect);
7116 type = c_build_pointer_type (type);
7117 type_quals = array_ptr_quals;
7118 if (type_quals)
7119 type = c_build_qualified_type (type, type_quals);
7120
7121 /* We don't yet implement attributes in this context. */
7122 if (array_ptr_attrs != NULL_TREE)
7123 warning_at (loc, OPT_Wattributes,
7124 "attributes in parameter array declarator ignored");
7125
7126 size_varies = false;
7127 array_parameter_p = true;
7128 }
7129 else if (TREE_CODE (type) == FUNCTION_TYPE)
7130 {
7131 if (type_quals & TYPE_QUAL_ATOMIC)
7132 {
7133 error_at (loc,
7134 "%<_Atomic%>-qualified function type");
7135 type_quals &= ~TYPE_QUAL_ATOMIC;
7136 }
7137 else if (type_quals)
7138 pedwarn (loc, OPT_Wpedantic,
7139 "ISO C forbids qualified function types");
7140 if (type_quals)
7141 type = c_build_qualified_type (type, type_quals);
7142 type = c_build_pointer_type (type);
7143 type_quals = TYPE_UNQUALIFIED;
7144 }
7145 else if (type_quals)
7146 type = c_build_qualified_type (type, type_quals);
7147
7148 decl = build_decl (declarator->id_loc,
7149 PARM_DECL, declarator->u.id.id, type);
7150 if (size_varies)
7151 C_DECL_VARIABLE_SIZE (decl) = 1;
7152 C_ARRAY_PARAMETER (decl) = array_parameter_p;
7153
7154 /* Compute the type actually passed in the parmlist,
7155 for the case where there is no prototype.
7156 (For example, shorts and chars are passed as ints.)
7157 When there is a prototype, this is overridden later. */
7158
7159 if (type == error_mark_node)
7160 promoted_type = type;
7161 else
7162 promoted_type = c_type_promotes_to (type);
7163
7164 DECL_ARG_TYPE (decl) = promoted_type;
7165 if (declspecs->inline_p)
7166 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
7167 if (declspecs->noreturn_p)
7168 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
7169 }
7170 else if (decl_context == FIELD)
7171 {
7172 /* Note that the grammar rejects storage classes in typenames
7173 and fields. */
7174 gcc_assert (storage_class == csc_none && !threadp
7175 && !declspecs->inline_p && !declspecs->noreturn_p);
7176
7177 /* Structure field. It may not be a function. */
7178
7179 if (TREE_CODE (type) == FUNCTION_TYPE)
7180 {
7181 error_at (loc, "field %qE declared as a function", name);
7182 type = build_pointer_type (type);
7183 }
7184 else if (TREE_CODE (type) != ERROR_MARK
7185 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
7186 {
7187 if (name)
7188 error_at (loc, "field %qE has incomplete type", name);
7189 else
7190 error_at (loc, "unnamed field has incomplete type");
7191 type = error_mark_node;
7192 }
7193 else if (TREE_CODE (type) == ARRAY_TYPE
7194 && TYPE_DOMAIN (type) == NULL_TREE)
7195 {
7196 /* We have a flexible array member through a typedef.
7197 Set suitable range. Whether this is a correct position
7198 for a flexible array member will be determined elsewhere. */
7199 if (!in_system_header_at (input_location))
7200 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7201 "support flexible array members");
7202 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7203 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
7204 NULL_TREE);
7205 if (orig_qual_indirect == 0)
7206 orig_qual_type = NULL_TREE;
7207 }
7208 if (type != error_mark_node
7209 && !verify_type_context (loc, TCTX_FIELD, type))
7210 type = error_mark_node;
7211
7212 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7213 orig_qual_indirect);
7214 decl = build_decl (declarator->id_loc,
7215 FIELD_DECL, declarator->u.id.id, type);
7216 DECL_NONADDRESSABLE_P (decl) = bitfield;
7217 if (bitfield && !declarator->u.id.id)
7218 {
7219 TREE_NO_WARNING (decl) = 1;
7220 DECL_PADDING_P (decl) = 1;
7221 }
7222
7223 if (size_varies)
7224 C_DECL_VARIABLE_SIZE (decl) = 1;
7225 }
7226 else if (TREE_CODE (type) == FUNCTION_TYPE)
7227 {
7228 if (storage_class == csc_register || threadp)
7229 {
7230 error_at (loc, "invalid storage class for function %qE", name);
7231 }
7232 else if (current_scope != file_scope)
7233 {
7234 /* Function declaration not at file scope. Storage
7235 classes other than `extern' are not allowed, C99
7236 6.7.1p5, and `extern' makes no difference. However,
7237 GCC allows 'auto', perhaps with 'inline', to support
7238 nested functions. */
7239 if (storage_class == csc_auto)
7240 pedwarn (loc, OPT_Wpedantic,
7241 "invalid storage class for function %qE", name);
7242 else if (storage_class == csc_static)
7243 {
7244 error_at (loc, "invalid storage class for function %qE", name);
7245 if (funcdef_flag)
7246 storage_class = declspecs->storage_class = csc_none;
7247 else
7248 return NULL_TREE;
7249 }
7250 }
7251
7252 decl = build_decl (declarator->id_loc,
7253 FUNCTION_DECL, declarator->u.id.id, type);
7254 decl = build_decl_attribute_variant (decl, decl_attr);
7255
7256 if (type_quals & TYPE_QUAL_ATOMIC)
7257 {
7258 error_at (loc,
7259 "%<_Atomic%>-qualified function type");
7260 type_quals &= ~TYPE_QUAL_ATOMIC;
7261 }
7262 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
7263 pedwarn (loc, OPT_Wpedantic,
7264 "ISO C forbids qualified function types");
7265
7266 /* Every function declaration is an external reference
7267 (DECL_EXTERNAL) except for those which are not at file
7268 scope and are explicitly declared "auto". This is
7269 forbidden by standard C (C99 6.7.1p5) and is interpreted by
7270 GCC to signify a forward declaration of a nested function. */
7271 if (storage_class == csc_auto && current_scope != file_scope)
7272 DECL_EXTERNAL (decl) = 0;
7273 /* In C99, a function which is declared 'inline' with 'extern'
7274 is not an external reference (which is confusing). It
7275 means that the later definition of the function must be output
7276 in this file, C99 6.7.4p6. In GNU C89, a function declared
7277 'extern inline' is an external reference. */
7278 else if (declspecs->inline_p && storage_class != csc_static)
7279 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
7280 == flag_gnu89_inline);
7281 else
7282 DECL_EXTERNAL (decl) = !initialized;
7283
7284 /* Record absence of global scope for `static' or `auto'. */
7285 TREE_PUBLIC (decl)
7286 = !(storage_class == csc_static || storage_class == csc_auto);
7287
7288 /* For a function definition, record the argument information
7289 block where store_parm_decls will look for it. */
7290 if (funcdef_flag)
7291 current_function_arg_info = arg_info;
7292
7293 if (declspecs->default_int_p)
7294 C_FUNCTION_IMPLICIT_INT (decl) = 1;
7295
7296 /* Record presence of `inline' and `_Noreturn', if it is
7297 reasonable. */
7298 if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
7299 {
7300 if (declspecs->inline_p)
7301 pedwarn (loc, 0, "cannot inline function %<main%>");
7302 if (declspecs->noreturn_p)
7303 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7304 }
7305 else
7306 {
7307 if (declspecs->inline_p)
7308 /* Record that the function is declared `inline'. */
7309 DECL_DECLARED_INLINE_P (decl) = 1;
7310 if (declspecs->noreturn_p)
7311 {
7312 if (flag_isoc99)
7313 pedwarn_c99 (loc, OPT_Wpedantic,
7314 "ISO C99 does not support %<_Noreturn%>");
7315 else
7316 pedwarn_c99 (loc, OPT_Wpedantic,
7317 "ISO C90 does not support %<_Noreturn%>");
7318 TREE_THIS_VOLATILE (decl) = 1;
7319 }
7320 }
7321 }
7322 else
7323 {
7324 /* It's a variable. */
7325 /* An uninitialized decl with `extern' is a reference. */
7326 int extern_ref = !initialized && storage_class == csc_extern;
7327
7328 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7329 orig_qual_indirect);
7330
7331 /* C99 6.2.2p7: It is invalid (compile-time undefined
7332 behavior) to create an 'extern' declaration for a
7333 variable if there is a global declaration that is
7334 'static' and the global declaration is not visible.
7335 (If the static declaration _is_ currently visible,
7336 the 'extern' declaration is taken to refer to that decl.) */
7337 if (extern_ref && current_scope != file_scope)
7338 {
7339 tree global_decl = identifier_global_value (declarator->u.id.id);
7340 tree visible_decl = lookup_name (declarator->u.id.id);
7341
7342 if (global_decl
7343 && global_decl != visible_decl
7344 && VAR_P (global_decl)
7345 && !TREE_PUBLIC (global_decl))
7346 error_at (loc, "variable previously declared %<static%> "
7347 "redeclared %<extern%>");
7348 }
7349
7350 decl = build_decl (declarator->id_loc,
7351 VAR_DECL, declarator->u.id.id, type);
7352 if (size_varies)
7353 C_DECL_VARIABLE_SIZE (decl) = 1;
7354
7355 if (declspecs->inline_p)
7356 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
7357 if (declspecs->noreturn_p)
7358 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7359
7360 /* At file scope, an initialized extern declaration may follow
7361 a static declaration. In that case, DECL_EXTERNAL will be
7362 reset later in start_decl. */
7363 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7364
7365 /* At file scope, the presence of a `static' or `register' storage
7366 class specifier, or the absence of all storage class specifiers
7367 makes this declaration a definition (perhaps tentative). Also,
7368 the absence of `static' makes it public. */
7369 if (current_scope == file_scope)
7370 {
7371 TREE_PUBLIC (decl) = storage_class != csc_static;
7372 TREE_STATIC (decl) = !extern_ref;
7373 }
7374 /* Not at file scope, only `static' makes a static definition. */
7375 else
7376 {
7377 TREE_STATIC (decl) = (storage_class == csc_static);
7378 TREE_PUBLIC (decl) = extern_ref;
7379 }
7380
7381 if (threadp)
7382 set_decl_tls_model (decl, decl_default_tls_model (decl));
7383 }
7384
7385 if ((storage_class == csc_extern
7386 || (storage_class == csc_none
7387 && TREE_CODE (type) == FUNCTION_TYPE
7388 && !funcdef_flag))
7389 && variably_modified_type_p (type, NULL_TREE))
7390 {
7391 /* C99 6.7.5.2p2 */
7392 if (TREE_CODE (type) == FUNCTION_TYPE)
7393 error_at (loc, "non-nested function with variably modified type");
7394 else
7395 error_at (loc, "object with variably modified type must have "
7396 "no linkage");
7397 }
7398
7399 /* For nested functions disqualify ones taking VLAs by value
7400 from inlining since the middle-end cannot deal with this.
7401 ??? We should arrange for those to be passed by reference
7402 with emitting the copy on the caller side in the frontend. */
7403 if (storage_class == csc_none
7404 && TREE_CODE (type) == FUNCTION_TYPE)
7405 for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
7406 {
7407 tree arg = TREE_VALUE (al);
7408 if (arg != error_mark_node
7409 && C_TYPE_VARIABLE_SIZE (arg))
7410 {
7411 DECL_UNINLINABLE (decl) = 1;
7412 break;
7413 }
7414 }
7415
7416 /* Record `register' declaration for warnings on &
7417 and in case doing stupid register allocation. */
7418
7419 if (storage_class == csc_register)
7420 {
7421 C_DECL_REGISTER (decl) = 1;
7422 DECL_REGISTER (decl) = 1;
7423 }
7424
7425 /* Record constancy and volatility. */
7426 c_apply_type_quals_to_decl (type_quals, decl);
7427
7428 /* Apply _Alignas specifiers. */
7429 if (alignas_align)
7430 {
7431 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7432 DECL_USER_ALIGN (decl) = 1;
7433 }
7434
7435 /* If a type has volatile components, it should be stored in memory.
7436 Otherwise, the fact that those components are volatile
7437 will be ignored, and would even crash the compiler.
7438 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7439 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7440 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7441 || TREE_CODE (decl) == RESULT_DECL))
7442 {
7443 /* It is not an error for a structure with volatile fields to
7444 be declared register, but reset DECL_REGISTER since it
7445 cannot actually go in a register. */
7446 int was_reg = C_DECL_REGISTER (decl);
7447 C_DECL_REGISTER (decl) = 0;
7448 DECL_REGISTER (decl) = 0;
7449 c_mark_addressable (decl);
7450 C_DECL_REGISTER (decl) = was_reg;
7451 }
7452
7453 /* This is the earliest point at which we might know the assembler
7454 name of a variable. Thus, if it's known before this, die horribly. */
7455 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7456 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7457
7458 if (warn_cxx_compat
7459 && VAR_P (decl)
7460 && TREE_PUBLIC (decl)
7461 && TREE_STATIC (decl)
7462 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7463 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7464 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7465 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7466 ("non-local variable %qD with anonymous type is "
7467 "questionable in C++"),
7468 decl);
7469
7470 return decl;
7471 }
7472 }
7473 \f
7474 /* Decode the parameter-list info for a function type or function definition.
7475 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7476 if there is an identifier list instead of a parameter decl list).
7477 These two functions are separate because when a function returns
7478 or receives functions then each is called multiple times but the order
7479 of calls is different. The last call to `grokparms' is always the one
7480 that contains the formal parameter names of a function definition.
7481
7482 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7483
7484 FUNCDEF_FLAG is true for a function definition, false for
7485 a mere declaration. A nonempty identifier-list gets an error message
7486 when FUNCDEF_FLAG is false. */
7487
7488 static tree
7489 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7490 {
7491 tree arg_types = arg_info->types;
7492
7493 if (funcdef_flag && arg_info->had_vla_unspec)
7494 {
7495 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7496 /* C99 6.7.5.2p4 */
7497 error ("%<[*]%> not allowed in other than function prototype scope");
7498 }
7499
7500 if (arg_types == NULL_TREE && !funcdef_flag
7501 && !in_system_header_at (input_location))
7502 warning (OPT_Wstrict_prototypes,
7503 "function declaration isn%'t a prototype");
7504
7505 if (arg_types == error_mark_node)
7506 /* Don't set TYPE_ARG_TYPES in this case. */
7507 return NULL_TREE;
7508
7509 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7510 {
7511 if (!funcdef_flag)
7512 {
7513 pedwarn (input_location, 0, "parameter names (without types) in "
7514 "function declaration");
7515 arg_info->parms = NULL_TREE;
7516 }
7517 else
7518 arg_info->parms = arg_info->types;
7519
7520 arg_info->types = NULL_TREE;
7521 return NULL_TREE;
7522 }
7523 else
7524 {
7525 tree parm, type, typelt;
7526 unsigned int parmno;
7527
7528 /* In C2X, convert () in a function definition to (void). */
7529 if (flag_isoc2x
7530 && funcdef_flag
7531 && !arg_types
7532 && !arg_info->parms)
7533 arg_types = arg_info->types = void_list_node;
7534
7535 /* If there is a parameter of incomplete type in a definition,
7536 this is an error. In a declaration this is valid, and a
7537 struct or union type may be completed later, before any calls
7538 or definition of the function. In the case where the tag was
7539 first declared within the parameter list, a warning has
7540 already been given. If a parameter has void type, then
7541 however the function cannot be defined or called, so
7542 warn. */
7543
7544 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7545 parm;
7546 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7547 {
7548 type = TREE_VALUE (typelt);
7549 if (type == error_mark_node)
7550 continue;
7551
7552 if (!COMPLETE_TYPE_P (type))
7553 {
7554 if (funcdef_flag)
7555 {
7556 if (DECL_NAME (parm))
7557 error_at (input_location,
7558 "parameter %u (%q+D) has incomplete type",
7559 parmno, parm);
7560 else
7561 error_at (DECL_SOURCE_LOCATION (parm),
7562 "parameter %u has incomplete type",
7563 parmno);
7564
7565 TREE_VALUE (typelt) = error_mark_node;
7566 TREE_TYPE (parm) = error_mark_node;
7567 arg_types = NULL_TREE;
7568 }
7569 else if (VOID_TYPE_P (type))
7570 {
7571 if (DECL_NAME (parm))
7572 warning_at (input_location, 0,
7573 "parameter %u (%q+D) has void type",
7574 parmno, parm);
7575 else
7576 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7577 "parameter %u has void type",
7578 parmno);
7579 }
7580 }
7581
7582 if (DECL_NAME (parm) && TREE_USED (parm))
7583 warn_if_shadowing (parm);
7584 }
7585 return arg_types;
7586 }
7587 }
7588
7589 /* Allocate and initialize a c_arg_info structure from the parser's
7590 obstack. */
7591
7592 struct c_arg_info *
7593 build_arg_info (void)
7594 {
7595 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7596 ret->parms = NULL_TREE;
7597 ret->tags = NULL;
7598 ret->types = NULL_TREE;
7599 ret->others = NULL_TREE;
7600 ret->pending_sizes = NULL;
7601 ret->had_vla_unspec = 0;
7602 return ret;
7603 }
7604
7605 /* Take apart the current scope and return a c_arg_info structure with
7606 info on a parameter list just parsed.
7607
7608 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7609
7610 ELLIPSIS being true means the argument list ended in '...' so don't
7611 append a sentinel (void_list_node) to the end of the type-list.
7612
7613 EXPR is NULL or an expression that needs to be evaluated for the
7614 side effects of array size expressions in the parameters. */
7615
7616 struct c_arg_info *
7617 get_parm_info (bool ellipsis, tree expr)
7618 {
7619 struct c_binding *b = current_scope->bindings;
7620 struct c_arg_info *arg_info = build_arg_info ();
7621
7622 tree parms = NULL_TREE;
7623 vec<c_arg_tag, va_gc> *tags = NULL;
7624 tree types = NULL_TREE;
7625 tree others = NULL_TREE;
7626
7627 bool gave_void_only_once_err = false;
7628
7629 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7630
7631 /* The bindings in this scope must not get put into a block.
7632 We will take care of deleting the binding nodes. */
7633 current_scope->bindings = 0;
7634
7635 /* This function is only called if there was *something* on the
7636 parameter list. */
7637 gcc_assert (b);
7638
7639 /* A parameter list consisting solely of 'void' indicates that the
7640 function takes no arguments. But if the 'void' is qualified
7641 (by 'const' or 'volatile'), or has a storage class specifier
7642 ('register'), then the behavior is undefined; issue an error.
7643 Typedefs for 'void' are OK (see DR#157). */
7644 if (b->prev == 0 /* one binding */
7645 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7646 && !DECL_NAME (b->decl) /* anonymous */
7647 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7648 {
7649 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7650 || C_DECL_REGISTER (b->decl))
7651 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7652
7653 /* There cannot be an ellipsis. */
7654 if (ellipsis)
7655 error_at (b->locus, "%<void%> must be the only parameter");
7656
7657 arg_info->types = void_list_node;
7658 return arg_info;
7659 }
7660
7661 if (!ellipsis)
7662 types = void_list_node;
7663
7664 /* Break up the bindings list into parms, tags, types, and others;
7665 apply sanity checks; purge the name-to-decl bindings. */
7666 while (b)
7667 {
7668 tree decl = b->decl;
7669 tree type = TREE_TYPE (decl);
7670 c_arg_tag tag;
7671 const char *keyword;
7672
7673 switch (TREE_CODE (decl))
7674 {
7675 case PARM_DECL:
7676 if (b->id)
7677 {
7678 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7679 I_SYMBOL_BINDING (b->id) = b->shadowed;
7680 }
7681
7682 /* Check for forward decls that never got their actual decl. */
7683 if (TREE_ASM_WRITTEN (decl))
7684 error_at (b->locus,
7685 "parameter %q+D has just a forward declaration", decl);
7686 /* Check for (..., void, ...) and issue an error. */
7687 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7688 {
7689 if (!gave_void_only_once_err)
7690 {
7691 error_at (b->locus, "%<void%> must be the only parameter");
7692 gave_void_only_once_err = true;
7693 }
7694 }
7695 else
7696 {
7697 /* Valid parameter, add it to the list. */
7698 DECL_CHAIN (decl) = parms;
7699 parms = decl;
7700
7701 /* Since there is a prototype, args are passed in their
7702 declared types. The back end may override this later. */
7703 DECL_ARG_TYPE (decl) = type;
7704 types = tree_cons (0, type, types);
7705 }
7706 break;
7707
7708 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7709 case UNION_TYPE: keyword = "union"; goto tag;
7710 case RECORD_TYPE: keyword = "struct"; goto tag;
7711 tag:
7712 /* Types may not have tag-names, in which case the type
7713 appears in the bindings list with b->id NULL. */
7714 if (b->id)
7715 {
7716 gcc_assert (I_TAG_BINDING (b->id) == b);
7717 I_TAG_BINDING (b->id) = b->shadowed;
7718 }
7719
7720 /* Warn about any struct, union or enum tags defined in a
7721 parameter list. The scope of such types is limited to
7722 the parameter list, which is rarely if ever desirable
7723 (it's impossible to call such a function with type-
7724 correct arguments). An anonymous union parm type is
7725 meaningful as a GNU extension, so don't warn for that. */
7726 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
7727 {
7728 if (b->id)
7729 /* The %s will be one of 'struct', 'union', or 'enum'. */
7730 warning_at (b->locus, 0,
7731 "%<%s %E%> declared inside parameter list"
7732 " will not be visible outside of this definition or"
7733 " declaration", keyword, b->id);
7734 else
7735 /* The %s will be one of 'struct', 'union', or 'enum'. */
7736 warning_at (b->locus, 0,
7737 "anonymous %s declared inside parameter list"
7738 " will not be visible outside of this definition or"
7739 " declaration", keyword);
7740 }
7741
7742 tag.id = b->id;
7743 tag.type = decl;
7744 vec_safe_push (tags, tag);
7745 break;
7746
7747 case FUNCTION_DECL:
7748 /* FUNCTION_DECLs appear when there is an implicit function
7749 declaration in the parameter list. */
7750 gcc_assert (b->nested || seen_error ());
7751 goto set_shadowed;
7752
7753 case CONST_DECL:
7754 case TYPE_DECL:
7755 /* CONST_DECLs appear here when we have an embedded enum,
7756 and TYPE_DECLs appear here when we have an embedded struct
7757 or union. No warnings for this - we already warned about the
7758 type itself. */
7759
7760 /* When we reinsert this decl in the function body, we need
7761 to reconstruct whether it was marked as nested. */
7762 gcc_assert (!b->nested);
7763 DECL_CHAIN (decl) = others;
7764 others = decl;
7765 /* fall through */
7766
7767 case ERROR_MARK:
7768 set_shadowed:
7769 /* error_mark_node appears here when we have an undeclared
7770 variable. Just throw it away. */
7771 if (b->id)
7772 {
7773 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7774 I_SYMBOL_BINDING (b->id) = b->shadowed;
7775 }
7776 break;
7777
7778 /* Other things that might be encountered. */
7779 case LABEL_DECL:
7780 case VAR_DECL:
7781 default:
7782 gcc_unreachable ();
7783 }
7784
7785 b = free_binding_and_advance (b);
7786 }
7787
7788 arg_info->parms = parms;
7789 arg_info->tags = tags;
7790 arg_info->types = types;
7791 arg_info->others = others;
7792 arg_info->pending_sizes = expr;
7793 return arg_info;
7794 }
7795 \f
7796 /* Get the struct, enum or union (CODE says which) with tag NAME.
7797 Define the tag as a forward-reference with location LOC if it is
7798 not defined. HAVE_STD_ATTRS says whether any standard attributes
7799 were present after the struct, union or enum keyword; ATTRS are the
7800 standard attributes present there. Return a c_typespec structure
7801 for the type specifier. */
7802
7803 struct c_typespec
7804 parser_xref_tag (location_t loc, enum tree_code code, tree name,
7805 bool have_std_attrs, tree attrs)
7806 {
7807 struct c_typespec ret;
7808 tree ref;
7809 location_t refloc;
7810
7811 ret.expr = NULL_TREE;
7812 ret.expr_const_operands = true;
7813
7814 /* If a cross reference is requested, look up the type
7815 already defined for this tag and return it. */
7816
7817 ref = lookup_tag (code, name, false, &refloc);
7818 /* If this is the right type of tag, return what we found.
7819 (This reference will be shadowed by shadow_tag later if appropriate.)
7820 If this is the wrong type of tag, do not return it. If it was the
7821 wrong type in the same scope, we will have had an error
7822 message already; if in a different scope and declaring
7823 a name, pending_xref_error will give an error message; but if in a
7824 different scope and not declaring a name, this tag should
7825 shadow the previous declaration of a different type of tag, and
7826 this would not work properly if we return the reference found.
7827 (For example, with "struct foo" in an outer scope, "union foo;"
7828 must shadow that tag with a new one of union type.) */
7829 ret.kind = (ref
7830 ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
7831 : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
7832 if (ref && TREE_CODE (ref) == code)
7833 {
7834 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
7835 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7836 && loc != UNKNOWN_LOCATION
7837 && warn_cxx_compat)
7838 {
7839 switch (code)
7840 {
7841 case ENUMERAL_TYPE:
7842 warning_at (loc, OPT_Wc___compat,
7843 ("enum type defined in struct or union "
7844 "is not visible in C++"));
7845 inform (refloc, "enum type defined here");
7846 break;
7847 case RECORD_TYPE:
7848 warning_at (loc, OPT_Wc___compat,
7849 ("struct defined in struct or union "
7850 "is not visible in C++"));
7851 inform (refloc, "struct defined here");
7852 break;
7853 case UNION_TYPE:
7854 warning_at (loc, OPT_Wc___compat,
7855 ("union defined in struct or union "
7856 "is not visible in C++"));
7857 inform (refloc, "union defined here");
7858 break;
7859 default:
7860 gcc_unreachable();
7861 }
7862 }
7863
7864 ret.spec = ref;
7865 return ret;
7866 }
7867
7868 /* If no such tag is yet defined, create a forward-reference node
7869 and record it as the "definition".
7870 When a real declaration of this type is found,
7871 the forward-reference will be altered into a real type. */
7872
7873 ref = make_node (code);
7874 if (code == ENUMERAL_TYPE)
7875 {
7876 /* Give the type a default layout like unsigned int
7877 to avoid crashing if it does not get defined. */
7878 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7879 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7880 TYPE_USER_ALIGN (ref) = 0;
7881 TYPE_UNSIGNED (ref) = 1;
7882 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7883 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7884 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7885 }
7886
7887 pushtag (loc, name, ref);
7888 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
7889
7890 ret.spec = ref;
7891 return ret;
7892 }
7893
7894 /* Get the struct, enum or union (CODE says which) with tag NAME.
7895 Define the tag as a forward-reference if it is not defined.
7896 Return a tree for the type. */
7897
7898 tree
7899 xref_tag (enum tree_code code, tree name)
7900 {
7901 return parser_xref_tag (input_location, code, name, false, NULL_TREE).spec;
7902 }
7903 \f
7904 /* Make sure that the tag NAME is defined *in the current scope*
7905 at least as a forward reference.
7906 LOC is the location of the struct's definition.
7907 CODE says which kind of tag NAME ought to be.
7908
7909 This stores the current value of the file static STRUCT_PARSE_INFO
7910 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7911 new c_struct_parse_info structure. The old value of
7912 STRUCT_PARSE_INFO is restored in finish_struct. */
7913
7914 tree
7915 start_struct (location_t loc, enum tree_code code, tree name,
7916 class c_struct_parse_info **enclosing_struct_parse_info)
7917 {
7918 /* If there is already a tag defined at this scope
7919 (as a forward reference), just return it. */
7920
7921 tree ref = NULL_TREE;
7922 location_t refloc = UNKNOWN_LOCATION;
7923
7924 if (name != NULL_TREE)
7925 ref = lookup_tag (code, name, true, &refloc);
7926 if (ref && TREE_CODE (ref) == code)
7927 {
7928 if (TYPE_STUB_DECL (ref))
7929 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
7930
7931 if (TYPE_SIZE (ref))
7932 {
7933 if (code == UNION_TYPE)
7934 error_at (loc, "redefinition of %<union %E%>", name);
7935 else
7936 error_at (loc, "redefinition of %<struct %E%>", name);
7937 if (refloc != UNKNOWN_LOCATION)
7938 inform (refloc, "originally defined here");
7939 /* Don't create structures using a name already in use. */
7940 ref = NULL_TREE;
7941 }
7942 else if (C_TYPE_BEING_DEFINED (ref))
7943 {
7944 if (code == UNION_TYPE)
7945 error_at (loc, "nested redefinition of %<union %E%>", name);
7946 else
7947 error_at (loc, "nested redefinition of %<struct %E%>", name);
7948 /* Don't bother to report "originally defined here" for a
7949 nested redefinition; the original definition should be
7950 obvious. */
7951 /* Don't create structures that contain themselves. */
7952 ref = NULL_TREE;
7953 }
7954 }
7955
7956 /* Otherwise create a forward-reference just so the tag is in scope. */
7957
7958 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7959 {
7960 ref = make_node (code);
7961 pushtag (loc, name, ref);
7962 }
7963
7964 C_TYPE_BEING_DEFINED (ref) = 1;
7965 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7966 TYPE_PACKED (v) = flag_pack_struct;
7967
7968 *enclosing_struct_parse_info = struct_parse_info;
7969 struct_parse_info = new c_struct_parse_info ();
7970
7971 /* FIXME: This will issue a warning for a use of a type defined
7972 within a statement expr used within sizeof, et. al. This is not
7973 terribly serious as C++ doesn't permit statement exprs within
7974 sizeof anyhow. */
7975 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7976 warning_at (loc, OPT_Wc___compat,
7977 "defining type in %qs expression is invalid in C++",
7978 (in_sizeof
7979 ? "sizeof"
7980 : (in_typeof ? "typeof" : "alignof")));
7981
7982 return ref;
7983 }
7984
7985 /* Process the specs, declarator and width (NULL if omitted)
7986 of a structure component, returning a FIELD_DECL node.
7987 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7988 DECL_ATTRS is as for grokdeclarator.
7989
7990 LOC is the location of the structure component.
7991
7992 This is done during the parsing of the struct declaration.
7993 The FIELD_DECL nodes are chained together and the lot of them
7994 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7995
7996 tree
7997 grokfield (location_t loc,
7998 struct c_declarator *declarator, struct c_declspecs *declspecs,
7999 tree width, tree *decl_attrs)
8000 {
8001 tree value;
8002
8003 if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8004 && width == NULL_TREE)
8005 {
8006 /* This is an unnamed decl.
8007
8008 If we have something of the form "union { list } ;" then this
8009 is the anonymous union extension. Similarly for struct.
8010
8011 If this is something of the form "struct foo;", then
8012 If MS or Plan 9 extensions are enabled, this is handled as
8013 an anonymous struct.
8014 Otherwise this is a forward declaration of a structure tag.
8015
8016 If this is something of the form "foo;" and foo is a TYPE_DECL, then
8017 If foo names a structure or union without a tag, then this
8018 is an anonymous struct (this is permitted by C11).
8019 If MS or Plan 9 extensions are enabled and foo names a
8020 structure, then again this is an anonymous struct.
8021 Otherwise this is an error.
8022
8023 Oh what a horrid tangled web we weave. I wonder if MS consciously
8024 took this from Plan 9 or if it was an accident of implementation
8025 that took root before someone noticed the bug... */
8026
8027 tree type = declspecs->type;
8028 bool ok = false;
8029
8030 if (RECORD_OR_UNION_TYPE_P (type)
8031 && (flag_ms_extensions
8032 || flag_plan9_extensions
8033 || !declspecs->typedef_p))
8034 {
8035 if (flag_ms_extensions || flag_plan9_extensions)
8036 ok = true;
8037 else if (TYPE_NAME (type) == NULL)
8038 ok = true;
8039 else
8040 ok = false;
8041 }
8042 if (!ok)
8043 {
8044 pedwarn (loc, 0, "declaration does not declare anything");
8045 return NULL_TREE;
8046 }
8047 if (flag_isoc99)
8048 pedwarn_c99 (loc, OPT_Wpedantic,
8049 "ISO C99 doesn%'t support unnamed structs/unions");
8050 else
8051 pedwarn_c99 (loc, OPT_Wpedantic,
8052 "ISO C90 doesn%'t support unnamed structs/unions");
8053 }
8054
8055 value = grokdeclarator (declarator, declspecs, FIELD, false,
8056 width ? &width : NULL, decl_attrs, NULL, NULL,
8057 DEPRECATED_NORMAL);
8058
8059 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
8060 DECL_INITIAL (value) = width;
8061 if (width)
8062 SET_DECL_C_BIT_FIELD (value);
8063
8064 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
8065 {
8066 /* If we currently have a binding for this field, set the
8067 in_struct field in the binding, so that we warn about lookups
8068 which find it. */
8069 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
8070 if (b != NULL)
8071 {
8072 /* If the in_struct field is not yet set, push it on a list
8073 to be cleared when this struct is finished. */
8074 if (!b->in_struct)
8075 {
8076 struct_parse_info->fields.safe_push (b);
8077 b->in_struct = 1;
8078 }
8079 }
8080 }
8081
8082 return value;
8083 }
8084 \f
8085 /* Subroutine of detect_field_duplicates: return whether X and Y,
8086 which are both fields in the same struct, have duplicate field
8087 names. */
8088
8089 static bool
8090 is_duplicate_field (tree x, tree y)
8091 {
8092 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
8093 return true;
8094
8095 /* When using -fplan9-extensions, an anonymous field whose name is a
8096 typedef can duplicate a field name. */
8097 if (flag_plan9_extensions
8098 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
8099 {
8100 tree xt, xn, yt, yn;
8101
8102 xt = TREE_TYPE (x);
8103 if (DECL_NAME (x) != NULL_TREE)
8104 xn = DECL_NAME (x);
8105 else if (RECORD_OR_UNION_TYPE_P (xt)
8106 && TYPE_NAME (xt) != NULL_TREE
8107 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
8108 xn = DECL_NAME (TYPE_NAME (xt));
8109 else
8110 xn = NULL_TREE;
8111
8112 yt = TREE_TYPE (y);
8113 if (DECL_NAME (y) != NULL_TREE)
8114 yn = DECL_NAME (y);
8115 else if (RECORD_OR_UNION_TYPE_P (yt)
8116 && TYPE_NAME (yt) != NULL_TREE
8117 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
8118 yn = DECL_NAME (TYPE_NAME (yt));
8119 else
8120 yn = NULL_TREE;
8121
8122 if (xn != NULL_TREE && xn == yn)
8123 return true;
8124 }
8125
8126 return false;
8127 }
8128
8129 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
8130 to HTAB, giving errors for any duplicates. */
8131
8132 static void
8133 detect_field_duplicates_hash (tree fieldlist,
8134 hash_table<nofree_ptr_hash <tree_node> > *htab)
8135 {
8136 tree x, y;
8137 tree_node **slot;
8138
8139 for (x = fieldlist; x ; x = DECL_CHAIN (x))
8140 if ((y = DECL_NAME (x)) != NULL_TREE)
8141 {
8142 slot = htab->find_slot (y, INSERT);
8143 if (*slot)
8144 {
8145 error ("duplicate member %q+D", x);
8146 DECL_NAME (x) = NULL_TREE;
8147 }
8148 *slot = y;
8149 }
8150 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8151 {
8152 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
8153
8154 /* When using -fplan9-extensions, an anonymous field whose
8155 name is a typedef can duplicate a field name. */
8156 if (flag_plan9_extensions
8157 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8158 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
8159 {
8160 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
8161 slot = htab->find_slot (xn, INSERT);
8162 if (*slot)
8163 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
8164 *slot = xn;
8165 }
8166 }
8167 }
8168
8169 /* Generate an error for any duplicate field names in FIELDLIST. Munge
8170 the list such that this does not present a problem later. */
8171
8172 static void
8173 detect_field_duplicates (tree fieldlist)
8174 {
8175 tree x, y;
8176 int timeout = 10;
8177
8178 /* If the struct is the list of instance variables of an Objective-C
8179 class, then we need to check all the instance variables of
8180 superclasses when checking for duplicates (since you can't have
8181 an instance variable in a subclass with the same name as an
8182 instance variable in a superclass). We pass on this job to the
8183 Objective-C compiler. objc_detect_field_duplicates() will return
8184 false if we are not checking the list of instance variables and
8185 the C frontend should proceed with the standard field duplicate
8186 checks. If we are checking the list of instance variables, the
8187 ObjC frontend will do the check, emit the errors if needed, and
8188 then return true. */
8189 if (c_dialect_objc ())
8190 if (objc_detect_field_duplicates (false))
8191 return;
8192
8193 /* First, see if there are more than "a few" fields.
8194 This is trivially true if there are zero or one fields. */
8195 if (!fieldlist || !DECL_CHAIN (fieldlist))
8196 return;
8197 x = fieldlist;
8198 do {
8199 timeout--;
8200 if (DECL_NAME (x) == NULL_TREE
8201 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8202 timeout = 0;
8203 x = DECL_CHAIN (x);
8204 } while (timeout > 0 && x);
8205
8206 /* If there were "few" fields and no anonymous structures or unions,
8207 avoid the overhead of allocating a hash table. Instead just do
8208 the nested traversal thing. */
8209 if (timeout > 0)
8210 {
8211 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
8212 /* When using -fplan9-extensions, we can have duplicates
8213 between typedef names and fields. */
8214 if (DECL_NAME (x)
8215 || (flag_plan9_extensions
8216 && DECL_NAME (x) == NULL_TREE
8217 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
8218 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8219 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
8220 {
8221 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
8222 if (is_duplicate_field (y, x))
8223 {
8224 error ("duplicate member %q+D", x);
8225 DECL_NAME (x) = NULL_TREE;
8226 }
8227 }
8228 }
8229 else
8230 {
8231 hash_table<nofree_ptr_hash <tree_node> > htab (37);
8232 detect_field_duplicates_hash (fieldlist, &htab);
8233 }
8234 }
8235
8236 /* Finish up struct info used by -Wc++-compat. */
8237
8238 static void
8239 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
8240 location_t record_loc)
8241 {
8242 unsigned int ix;
8243 tree x;
8244 struct c_binding *b;
8245
8246 if (fieldlist == NULL_TREE)
8247 {
8248 if (code == RECORD_TYPE)
8249 warning_at (record_loc, OPT_Wc___compat,
8250 "empty struct has size 0 in C, size 1 in C++");
8251 else
8252 warning_at (record_loc, OPT_Wc___compat,
8253 "empty union has size 0 in C, size 1 in C++");
8254 }
8255
8256 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
8257 the current struct. We do this now at the end of the struct
8258 because the flag is used to issue visibility warnings, and we
8259 only want to issue those warnings if the type is referenced
8260 outside of the struct declaration. */
8261 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
8262 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
8263
8264 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
8265 typedefs used when declaring fields in this struct. If the name
8266 of any of the fields is also a typedef name then the struct would
8267 not parse in C++, because the C++ lookup rules say that the
8268 typedef name would be looked up in the context of the struct, and
8269 would thus be the field rather than the typedef. */
8270 if (!struct_parse_info->typedefs_seen.is_empty ()
8271 && fieldlist != NULL_TREE)
8272 {
8273 /* Use a hash_set<tree> using the name of the typedef. We can use
8274 a hash_set<tree> because identifiers are interned. */
8275 hash_set<tree> tset;
8276
8277 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
8278 tset.add (DECL_NAME (x));
8279
8280 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
8281 {
8282 if (DECL_NAME (x) != NULL_TREE
8283 && tset.contains (DECL_NAME (x)))
8284 {
8285 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
8286 ("using %qD as both field and typedef name is "
8287 "invalid in C++"),
8288 x);
8289 /* FIXME: It would be nice to report the location where
8290 the typedef name is used. */
8291 }
8292 }
8293 }
8294
8295 /* For each field which has a binding and which was not defined in
8296 an enclosing struct, clear the in_struct field. */
8297 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
8298 b->in_struct = 0;
8299 }
8300
8301 /* Function to help qsort sort FIELD_DECLs by name order. */
8302
8303 static int
8304 field_decl_cmp (const void *x_p, const void *y_p)
8305 {
8306 const tree *const x = (const tree *) x_p;
8307 const tree *const y = (const tree *) y_p;
8308
8309 if (DECL_NAME (*x) == DECL_NAME (*y))
8310 /* A nontype is "greater" than a type. */
8311 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8312 if (DECL_NAME (*x) == NULL_TREE)
8313 return -1;
8314 if (DECL_NAME (*y) == NULL_TREE)
8315 return 1;
8316 if (DECL_NAME (*x) < DECL_NAME (*y))
8317 return -1;
8318 return 1;
8319 }
8320
8321 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8322 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8323 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8324 ATTRIBUTES are attributes to be applied to the structure.
8325
8326 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8327 the struct was started. */
8328
8329 tree
8330 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
8331 class c_struct_parse_info *enclosing_struct_parse_info)
8332 {
8333 tree x;
8334 bool toplevel = file_scope == current_scope;
8335
8336 /* If this type was previously laid out as a forward reference,
8337 make sure we lay it out again. */
8338
8339 TYPE_SIZE (t) = NULL_TREE;
8340
8341 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8342
8343 if (pedantic)
8344 {
8345 for (x = fieldlist; x; x = DECL_CHAIN (x))
8346 {
8347 if (DECL_NAME (x) != NULL_TREE)
8348 break;
8349 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8350 break;
8351 }
8352
8353 if (x == NULL_TREE)
8354 {
8355 if (TREE_CODE (t) == UNION_TYPE)
8356 {
8357 if (fieldlist)
8358 pedwarn (loc, OPT_Wpedantic, "union has no named members");
8359 else
8360 pedwarn (loc, OPT_Wpedantic, "union has no members");
8361 }
8362 else
8363 {
8364 if (fieldlist)
8365 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
8366 else
8367 pedwarn (loc, OPT_Wpedantic, "struct has no members");
8368 }
8369 }
8370 }
8371
8372 /* Install struct as DECL_CONTEXT of each field decl.
8373 Also process specified field sizes, found in the DECL_INITIAL,
8374 storing 0 there after the type has been changed to precision equal
8375 to its width, rather than the precision of the specified standard
8376 type. (Correct layout requires the original type to have been preserved
8377 until now.) */
8378
8379 bool saw_named_field = false;
8380 for (x = fieldlist; x; x = DECL_CHAIN (x))
8381 {
8382 if (TREE_TYPE (x) == error_mark_node)
8383 continue;
8384
8385 DECL_CONTEXT (x) = t;
8386
8387 /* If any field is const, the structure type is pseudo-const. */
8388 if (TREE_READONLY (x))
8389 C_TYPE_FIELDS_READONLY (t) = 1;
8390 else
8391 {
8392 /* A field that is pseudo-const makes the structure likewise. */
8393 tree t1 = strip_array_types (TREE_TYPE (x));
8394 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8395 C_TYPE_FIELDS_READONLY (t) = 1;
8396 }
8397
8398 /* Any field that is volatile means variables of this type must be
8399 treated in some ways as volatile. */
8400 if (TREE_THIS_VOLATILE (x))
8401 C_TYPE_FIELDS_VOLATILE (t) = 1;
8402
8403 /* Any field of nominal variable size implies structure is too. */
8404 if (C_DECL_VARIABLE_SIZE (x))
8405 C_TYPE_VARIABLE_SIZE (t) = 1;
8406
8407 if (DECL_C_BIT_FIELD (x))
8408 {
8409 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8410 DECL_SIZE (x) = bitsize_int (width);
8411 DECL_BIT_FIELD (x) = 1;
8412 }
8413
8414 if (TYPE_PACKED (t)
8415 && (DECL_BIT_FIELD (x)
8416 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8417 DECL_PACKED (x) = 1;
8418
8419 /* Detect flexible array member in an invalid context. */
8420 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8421 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
8422 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
8423 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8424 {
8425 if (TREE_CODE (t) == UNION_TYPE)
8426 {
8427 error_at (DECL_SOURCE_LOCATION (x),
8428 "flexible array member in union");
8429 TREE_TYPE (x) = error_mark_node;
8430 }
8431 else if (DECL_CHAIN (x) != NULL_TREE)
8432 {
8433 error_at (DECL_SOURCE_LOCATION (x),
8434 "flexible array member not at end of struct");
8435 TREE_TYPE (x) = error_mark_node;
8436 }
8437 else if (!saw_named_field)
8438 {
8439 error_at (DECL_SOURCE_LOCATION (x),
8440 "flexible array member in a struct with no named "
8441 "members");
8442 TREE_TYPE (x) = error_mark_node;
8443 }
8444 }
8445
8446 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8447 && flexible_array_type_p (TREE_TYPE (x)))
8448 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8449 "invalid use of structure with flexible array member");
8450
8451 if (DECL_NAME (x)
8452 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8453 saw_named_field = true;
8454 }
8455
8456 detect_field_duplicates (fieldlist);
8457
8458 /* Now we have the nearly final fieldlist. Record it,
8459 then lay out the structure or union (including the fields). */
8460
8461 TYPE_FIELDS (t) = fieldlist;
8462
8463 maybe_apply_pragma_scalar_storage_order (t);
8464
8465 layout_type (t);
8466
8467 if (TYPE_SIZE_UNIT (t)
8468 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8469 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8470 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8471 error ("type %qT is too large", t);
8472
8473 /* Give bit-fields their proper types and rewrite the type of array fields
8474 with scalar component if the enclosing type has reverse storage order. */
8475 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8476 {
8477 if (TREE_CODE (field) == FIELD_DECL
8478 && DECL_INITIAL (field)
8479 && TREE_TYPE (field) != error_mark_node)
8480 {
8481 unsigned HOST_WIDE_INT width
8482 = tree_to_uhwi (DECL_INITIAL (field));
8483 tree type = TREE_TYPE (field);
8484 if (width != TYPE_PRECISION (type))
8485 {
8486 TREE_TYPE (field)
8487 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8488 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8489 }
8490 DECL_INITIAL (field) = NULL_TREE;
8491 }
8492 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8493 && TREE_CODE (field) == FIELD_DECL
8494 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8495 {
8496 tree ftype = TREE_TYPE (field);
8497 tree ctype = strip_array_types (ftype);
8498 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8499 {
8500 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8501 tree *typep = &fmain_type;
8502 do {
8503 *typep = build_distinct_type_copy (*typep);
8504 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8505 typep = &TREE_TYPE (*typep);
8506 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8507 TREE_TYPE (field)
8508 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8509 }
8510 }
8511 }
8512
8513 /* Now we have the truly final field list.
8514 Store it in this type and in the variants. */
8515
8516 TYPE_FIELDS (t) = fieldlist;
8517
8518 /* If there are lots of fields, sort so we can look through them fast.
8519 We arbitrarily consider 16 or more elts to be "a lot". */
8520
8521 {
8522 int len = 0;
8523
8524 for (x = fieldlist; x; x = DECL_CHAIN (x))
8525 {
8526 if (len > 15 || DECL_NAME (x) == NULL)
8527 break;
8528 len += 1;
8529 }
8530
8531 if (len > 15)
8532 {
8533 tree *field_array;
8534 struct lang_type *space;
8535 struct sorted_fields_type *space2;
8536
8537 len += list_length (x);
8538
8539 /* Use the same allocation policy here that make_node uses, to
8540 ensure that this lives as long as the rest of the struct decl.
8541 All decls in an inline function need to be saved. */
8542
8543 space = ggc_cleared_alloc<struct lang_type> ();
8544 space2 = (sorted_fields_type *) ggc_internal_alloc
8545 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8546
8547 len = 0;
8548 space->s = space2;
8549 field_array = &space2->elts[0];
8550 for (x = fieldlist; x; x = DECL_CHAIN (x))
8551 {
8552 field_array[len++] = x;
8553
8554 /* If there is anonymous struct or union, break out of the loop. */
8555 if (DECL_NAME (x) == NULL)
8556 break;
8557 }
8558 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8559 if (x == NULL)
8560 {
8561 TYPE_LANG_SPECIFIC (t) = space;
8562 TYPE_LANG_SPECIFIC (t)->s->len = len;
8563 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8564 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8565 }
8566 }
8567 }
8568
8569 /* If this was supposed to be a transparent union, but we can't
8570 make it one, warn and turn off the flag. */
8571 if (TREE_CODE (t) == UNION_TYPE
8572 && TYPE_TRANSPARENT_AGGR (t)
8573 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8574 {
8575 TYPE_TRANSPARENT_AGGR (t) = 0;
8576 warning_at (loc, 0, "union cannot be made transparent");
8577 }
8578
8579 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8580 in dwarf2out via rest_of_decl_compilation below and means
8581 something totally different. Since we will be clearing
8582 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8583 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8584 C_TYPE_INCOMPLETE_VARS should use some language specific
8585 node. */
8586 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8587 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8588 {
8589 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8590 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8591 TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
8592 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8593 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8594 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8595 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8596 }
8597
8598 /* Update type location to the one of the definition, instead of e.g.
8599 a forward declaration. */
8600 if (TYPE_STUB_DECL (t))
8601 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8602
8603 /* Finish debugging output for this type. */
8604 rest_of_type_compilation (t, toplevel);
8605
8606 /* If this structure or union completes the type of any previous
8607 variable declaration, lay it out and output its rtl. */
8608 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8609 {
8610 tree decl = TREE_VALUE (x);
8611 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8612 layout_array_type (TREE_TYPE (decl));
8613 if (TREE_CODE (decl) != TYPE_DECL)
8614 {
8615 layout_decl (decl, 0);
8616 if (c_dialect_objc ())
8617 objc_check_decl (decl);
8618 rest_of_decl_compilation (decl, toplevel, 0);
8619 }
8620 }
8621
8622 /* If we're inside a function proper, i.e. not file-scope and not still
8623 parsing parameters, then arrange for the size of a variable sized type
8624 to be bound now. */
8625 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8626 add_stmt (build_stmt (loc,
8627 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8628
8629 if (warn_cxx_compat)
8630 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8631
8632 delete struct_parse_info;
8633
8634 struct_parse_info = enclosing_struct_parse_info;
8635
8636 /* If this struct is defined inside a struct, add it to
8637 struct_types. */
8638 if (warn_cxx_compat
8639 && struct_parse_info != NULL
8640 && !in_sizeof && !in_typeof && !in_alignof)
8641 struct_parse_info->struct_types.safe_push (t);
8642
8643 return t;
8644 }
8645
8646 static struct {
8647 gt_pointer_operator new_value;
8648 void *cookie;
8649 } resort_data;
8650
8651 /* This routine compares two fields like field_decl_cmp but using the
8652 pointer operator in resort_data. */
8653
8654 static int
8655 resort_field_decl_cmp (const void *x_p, const void *y_p)
8656 {
8657 const tree *const x = (const tree *) x_p;
8658 const tree *const y = (const tree *) y_p;
8659
8660 if (DECL_NAME (*x) == DECL_NAME (*y))
8661 /* A nontype is "greater" than a type. */
8662 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8663 if (DECL_NAME (*x) == NULL_TREE)
8664 return -1;
8665 if (DECL_NAME (*y) == NULL_TREE)
8666 return 1;
8667 {
8668 tree d1 = DECL_NAME (*x);
8669 tree d2 = DECL_NAME (*y);
8670 resort_data.new_value (&d1, resort_data.cookie);
8671 resort_data.new_value (&d2, resort_data.cookie);
8672 if (d1 < d2)
8673 return -1;
8674 }
8675 return 1;
8676 }
8677
8678 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8679
8680 void
8681 resort_sorted_fields (void *obj,
8682 void * ARG_UNUSED (orig_obj),
8683 gt_pointer_operator new_value,
8684 void *cookie)
8685 {
8686 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8687 resort_data.new_value = new_value;
8688 resort_data.cookie = cookie;
8689 qsort (&sf->elts[0], sf->len, sizeof (tree),
8690 resort_field_decl_cmp);
8691 }
8692
8693 /* Lay out the type T, and its element type, and so on. */
8694
8695 static void
8696 layout_array_type (tree t)
8697 {
8698 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8699 layout_array_type (TREE_TYPE (t));
8700 layout_type (t);
8701 }
8702 \f
8703 /* Begin compiling the definition of an enumeration type.
8704 NAME is its name (or null if anonymous).
8705 LOC is the enum's location.
8706 Returns the type object, as yet incomplete.
8707 Also records info about it so that build_enumerator
8708 may be used to declare the individual values as they are read. */
8709
8710 tree
8711 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8712 {
8713 tree enumtype = NULL_TREE;
8714 location_t enumloc = UNKNOWN_LOCATION;
8715
8716 /* If this is the real definition for a previous forward reference,
8717 fill in the contents in the same object that used to be the
8718 forward reference. */
8719
8720 if (name != NULL_TREE)
8721 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8722
8723 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8724 {
8725 enumtype = make_node (ENUMERAL_TYPE);
8726 pushtag (loc, name, enumtype);
8727 }
8728 /* Update type location to the one of the definition, instead of e.g.
8729 a forward declaration. */
8730 else if (TYPE_STUB_DECL (enumtype))
8731 {
8732 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
8733 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
8734 }
8735
8736 if (C_TYPE_BEING_DEFINED (enumtype))
8737 error_at (loc, "nested redefinition of %<enum %E%>", name);
8738
8739 C_TYPE_BEING_DEFINED (enumtype) = 1;
8740
8741 if (TYPE_VALUES (enumtype) != NULL_TREE)
8742 {
8743 /* This enum is a named one that has been declared already. */
8744 error_at (loc, "redeclaration of %<enum %E%>", name);
8745 if (enumloc != UNKNOWN_LOCATION)
8746 inform (enumloc, "originally defined here");
8747
8748 /* Completely replace its old definition.
8749 The old enumerators remain defined, however. */
8750 TYPE_VALUES (enumtype) = NULL_TREE;
8751 }
8752
8753 the_enum->enum_next_value = integer_zero_node;
8754 the_enum->enum_overflow = 0;
8755
8756 if (flag_short_enums)
8757 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8758 TYPE_PACKED (v) = 1;
8759
8760 /* FIXME: This will issue a warning for a use of a type defined
8761 within sizeof in a statement expr. This is not terribly serious
8762 as C++ doesn't permit statement exprs within sizeof anyhow. */
8763 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8764 warning_at (loc, OPT_Wc___compat,
8765 "defining type in %qs expression is invalid in C++",
8766 (in_sizeof
8767 ? "sizeof"
8768 : (in_typeof ? "typeof" : "alignof")));
8769
8770 return enumtype;
8771 }
8772
8773 /* After processing and defining all the values of an enumeration type,
8774 install their decls in the enumeration type and finish it off.
8775 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8776 and ATTRIBUTES are the specified attributes.
8777 Returns ENUMTYPE. */
8778
8779 tree
8780 finish_enum (tree enumtype, tree values, tree attributes)
8781 {
8782 tree pair, tem;
8783 tree minnode = NULL_TREE, maxnode = NULL_TREE;
8784 int precision;
8785 signop sign;
8786 bool toplevel = (file_scope == current_scope);
8787 struct lang_type *lt;
8788
8789 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8790
8791 /* Calculate the maximum value of any enumerator in this type. */
8792
8793 if (values == error_mark_node)
8794 minnode = maxnode = integer_zero_node;
8795 else
8796 {
8797 minnode = maxnode = TREE_VALUE (values);
8798 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8799 {
8800 tree value = TREE_VALUE (pair);
8801 if (tree_int_cst_lt (maxnode, value))
8802 maxnode = value;
8803 if (tree_int_cst_lt (value, minnode))
8804 minnode = value;
8805 }
8806 }
8807
8808 /* Construct the final type of this enumeration. It is the same
8809 as one of the integral types - the narrowest one that fits, except
8810 that normally we only go as narrow as int - and signed iff any of
8811 the values are negative. */
8812 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8813 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8814 tree_int_cst_min_precision (maxnode, sign));
8815
8816 /* If the precision of the type was specified with an attribute and it
8817 was too small, give an error. Otherwise, use it. */
8818 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8819 {
8820 if (precision > TYPE_PRECISION (enumtype))
8821 {
8822 TYPE_PRECISION (enumtype) = 0;
8823 error ("specified mode too small for enumerated values");
8824 }
8825 else
8826 precision = TYPE_PRECISION (enumtype);
8827 }
8828 else
8829 TYPE_PRECISION (enumtype) = 0;
8830
8831 if (TYPE_PACKED (enumtype)
8832 || precision > TYPE_PRECISION (integer_type_node)
8833 || TYPE_PRECISION (enumtype))
8834 {
8835 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8836 if (tem == NULL)
8837 {
8838 warning (0, "enumeration values exceed range of largest integer");
8839 tem = long_long_integer_type_node;
8840 }
8841 }
8842 else
8843 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8844
8845 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8846 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8847 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8848 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8849 TYPE_SIZE (enumtype) = NULL_TREE;
8850 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8851
8852 layout_type (enumtype);
8853
8854 if (values != error_mark_node)
8855 {
8856 /* Change the type of the enumerators to be the enum type. We
8857 need to do this irrespective of the size of the enum, for
8858 proper type checking. Replace the DECL_INITIALs of the
8859 enumerators, and the value slots of the list, with copies
8860 that have the enum type; they cannot be modified in place
8861 because they may be shared (e.g. integer_zero_node) Finally,
8862 change the purpose slots to point to the names of the decls. */
8863 for (pair = values; pair; pair = TREE_CHAIN (pair))
8864 {
8865 tree enu = TREE_PURPOSE (pair);
8866 tree ini = DECL_INITIAL (enu);
8867
8868 TREE_TYPE (enu) = enumtype;
8869
8870 /* The ISO C Standard mandates enumerators to have type int,
8871 even though the underlying type of an enum type is
8872 unspecified. However, GCC allows enumerators of any
8873 integer type as an extensions. build_enumerator()
8874 converts any enumerators that fit in an int to type int,
8875 to avoid promotions to unsigned types when comparing
8876 integers with enumerators that fit in the int range.
8877 When -pedantic is given, build_enumerator() would have
8878 already warned about those that don't fit. Here we
8879 convert the rest to the enumerator type. */
8880 if (TREE_TYPE (ini) != integer_type_node)
8881 ini = convert (enumtype, ini);
8882
8883 DECL_INITIAL (enu) = ini;
8884 TREE_PURPOSE (pair) = DECL_NAME (enu);
8885 TREE_VALUE (pair) = ini;
8886 }
8887
8888 TYPE_VALUES (enumtype) = values;
8889 }
8890
8891 /* Record the min/max values so that we can warn about bit-field
8892 enumerations that are too small for the values. */
8893 lt = ggc_cleared_alloc<struct lang_type> ();
8894 lt->enum_min = minnode;
8895 lt->enum_max = maxnode;
8896 TYPE_LANG_SPECIFIC (enumtype) = lt;
8897
8898 /* Fix up all variant types of this enum type. */
8899 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8900 {
8901 if (tem == enumtype)
8902 continue;
8903 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8904 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8905 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8906 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8907 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8908 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8909 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8910 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8911 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8912 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8913 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8914 }
8915
8916 /* Finish debugging output for this type. */
8917 rest_of_type_compilation (enumtype, toplevel);
8918
8919 /* If this enum is defined inside a struct, add it to
8920 struct_types. */
8921 if (warn_cxx_compat
8922 && struct_parse_info != NULL
8923 && !in_sizeof && !in_typeof && !in_alignof)
8924 struct_parse_info->struct_types.safe_push (enumtype);
8925
8926 C_TYPE_BEING_DEFINED (enumtype) = 0;
8927
8928 return enumtype;
8929 }
8930
8931 /* Build and install a CONST_DECL for one value of the
8932 current enumeration type (one that was begun with start_enum).
8933 DECL_LOC is the location of the enumerator.
8934 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8935 Return a tree-list containing the CONST_DECL and its value.
8936 Assignment of sequential values by default is handled here. */
8937
8938 tree
8939 build_enumerator (location_t decl_loc, location_t loc,
8940 struct c_enum_contents *the_enum, tree name, tree value)
8941 {
8942 tree decl, type;
8943
8944 /* Validate and default VALUE. */
8945
8946 if (value != NULL_TREE)
8947 {
8948 /* Don't issue more errors for error_mark_node (i.e. an
8949 undeclared identifier) - just ignore the value expression. */
8950 if (value == error_mark_node)
8951 value = NULL_TREE;
8952 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8953 {
8954 error_at (loc, "enumerator value for %qE is not an integer constant",
8955 name);
8956 value = NULL_TREE;
8957 }
8958 else
8959 {
8960 if (TREE_CODE (value) != INTEGER_CST)
8961 {
8962 value = c_fully_fold (value, false, NULL);
8963 if (TREE_CODE (value) == INTEGER_CST)
8964 pedwarn (loc, OPT_Wpedantic,
8965 "enumerator value for %qE is not an integer "
8966 "constant expression", name);
8967 }
8968 if (TREE_CODE (value) != INTEGER_CST)
8969 {
8970 error ("enumerator value for %qE is not an integer constant",
8971 name);
8972 value = NULL_TREE;
8973 }
8974 else
8975 {
8976 value = default_conversion (value);
8977 constant_expression_warning (value);
8978 }
8979 }
8980 }
8981
8982 /* Default based on previous value. */
8983 /* It should no longer be possible to have NON_LVALUE_EXPR
8984 in the default. */
8985 if (value == NULL_TREE)
8986 {
8987 value = the_enum->enum_next_value;
8988 if (the_enum->enum_overflow)
8989 error_at (loc, "overflow in enumeration values");
8990 }
8991 /* Even though the underlying type of an enum is unspecified, the
8992 type of enumeration constants is explicitly defined as int
8993 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8994 an extension. */
8995 else if (!int_fits_type_p (value, integer_type_node))
8996 pedwarn (loc, OPT_Wpedantic,
8997 "ISO C restricts enumerator values to range of %<int%>");
8998
8999 /* The ISO C Standard mandates enumerators to have type int, even
9000 though the underlying type of an enum type is unspecified.
9001 However, GCC allows enumerators of any integer type as an
9002 extensions. Here we convert any enumerators that fit in an int
9003 to type int, to avoid promotions to unsigned types when comparing
9004 integers with enumerators that fit in the int range. When
9005 -pedantic is given, we would have already warned about those that
9006 don't fit. We have to do this here rather than in finish_enum
9007 because this value may be used to define more enumerators. */
9008 if (int_fits_type_p (value, integer_type_node))
9009 value = convert (integer_type_node, value);
9010
9011 /* Set basis for default for next value. */
9012 the_enum->enum_next_value
9013 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
9014 PLUS_EXPR, value, integer_one_node, false);
9015 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
9016
9017 /* Now create a declaration for the enum value name. */
9018
9019 type = TREE_TYPE (value);
9020 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
9021 TYPE_PRECISION (integer_type_node)),
9022 (TYPE_PRECISION (type)
9023 >= TYPE_PRECISION (integer_type_node)
9024 && TYPE_UNSIGNED (type)));
9025
9026 decl = build_decl (decl_loc, CONST_DECL, name, type);
9027 DECL_INITIAL (decl) = convert (type, value);
9028 pushdecl (decl);
9029
9030 return tree_cons (decl, value, NULL_TREE);
9031 }
9032
9033 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
9034
9035 tree
9036 c_simulate_enum_decl (location_t loc, const char *name,
9037 vec<string_int_pair> values)
9038 {
9039 location_t saved_loc = input_location;
9040 input_location = loc;
9041
9042 struct c_enum_contents the_enum;
9043 tree enumtype = start_enum (loc, &the_enum, get_identifier (name));
9044
9045 tree value_chain = NULL_TREE;
9046 string_int_pair *value;
9047 unsigned int i;
9048 FOR_EACH_VEC_ELT (values, i, value)
9049 {
9050 tree decl = build_enumerator (loc, loc, &the_enum,
9051 get_identifier (value->first),
9052 build_int_cst (integer_type_node,
9053 value->second));
9054 TREE_CHAIN (decl) = value_chain;
9055 value_chain = decl;
9056 }
9057
9058 finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
9059
9060 input_location = saved_loc;
9061 return enumtype;
9062 }
9063 \f
9064 /* Create the FUNCTION_DECL for a function definition.
9065 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
9066 the declaration; they describe the function's name and the type it returns,
9067 but twisted together in a fashion that parallels the syntax of C.
9068
9069 This function creates a binding context for the function body
9070 as well as setting up the FUNCTION_DECL in current_function_decl.
9071
9072 Returns true on success. If the DECLARATOR is not suitable for a function
9073 (it defines a datum instead), we return false to report a parse error. */
9074
9075 bool
9076 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
9077 tree attributes)
9078 {
9079 tree decl1, old_decl;
9080 tree restype, resdecl;
9081 location_t loc;
9082
9083 current_function_returns_value = 0; /* Assume, until we see it does. */
9084 current_function_returns_null = 0;
9085 current_function_returns_abnormally = 0;
9086 warn_about_return_type = 0;
9087 c_switch_stack = NULL;
9088
9089 /* Indicate no valid break/continue context by setting these variables
9090 to some non-null, non-label value. We'll notice and emit the proper
9091 error message in c_finish_bc_stmt. */
9092 c_break_label = c_cont_label = size_zero_node;
9093
9094 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
9095 &attributes, NULL, NULL, DEPRECATED_NORMAL);
9096 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
9097
9098 /* If the declarator is not suitable for a function definition,
9099 cause a syntax error. */
9100 if (decl1 == NULL_TREE
9101 || TREE_CODE (decl1) != FUNCTION_DECL)
9102 return false;
9103
9104 loc = DECL_SOURCE_LOCATION (decl1);
9105
9106 /* A nested function is not global. */
9107 if (current_function_decl != NULL_TREE)
9108 TREE_PUBLIC (decl1) = 0;
9109
9110 c_decl_attributes (&decl1, attributes, 0);
9111
9112 if (DECL_DECLARED_INLINE_P (decl1)
9113 && DECL_UNINLINABLE (decl1)
9114 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
9115 warning_at (loc, OPT_Wattributes,
9116 "inline function %qD given attribute %qs",
9117 decl1, "noinline");
9118
9119 /* Handle gnu_inline attribute. */
9120 if (declspecs->inline_p
9121 && !flag_gnu89_inline
9122 && TREE_CODE (decl1) == FUNCTION_DECL
9123 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
9124 || current_function_decl))
9125 {
9126 if (declspecs->storage_class != csc_static)
9127 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
9128 }
9129
9130 announce_function (decl1);
9131
9132 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
9133 {
9134 error_at (loc, "return type is an incomplete type");
9135 /* Make it return void instead. */
9136 TREE_TYPE (decl1)
9137 = build_function_type (void_type_node,
9138 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
9139 }
9140
9141 if (warn_about_return_type)
9142 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
9143 : (warn_return_type > 0 ? OPT_Wreturn_type
9144 : OPT_Wimplicit_int),
9145 "return type defaults to %<int%>");
9146
9147 /* Make the init_value nonzero so pushdecl knows this is not tentative.
9148 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
9149 DECL_INITIAL (decl1) = error_mark_node;
9150
9151 /* If this definition isn't a prototype and we had a prototype declaration
9152 before, copy the arg type info from that prototype. */
9153 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
9154 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
9155 old_decl = NULL_TREE;
9156 current_function_prototype_locus = UNKNOWN_LOCATION;
9157 current_function_prototype_built_in = false;
9158 current_function_prototype_arg_types = NULL_TREE;
9159 if (!prototype_p (TREE_TYPE (decl1)))
9160 {
9161 if (old_decl != NULL_TREE
9162 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
9163 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
9164 TREE_TYPE (TREE_TYPE (old_decl))))
9165 {
9166 if (stdarg_p (TREE_TYPE (old_decl)))
9167 {
9168 auto_diagnostic_group d;
9169 warning_at (loc, 0, "%q+D defined as variadic function "
9170 "without prototype", decl1);
9171 locate_old_decl (old_decl);
9172 }
9173 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
9174 TREE_TYPE (decl1));
9175 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
9176 current_function_prototype_built_in
9177 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
9178 current_function_prototype_arg_types
9179 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
9180 }
9181 if (TREE_PUBLIC (decl1))
9182 {
9183 /* If there is an external prototype declaration of this
9184 function, record its location but do not copy information
9185 to this decl. This may be an invisible declaration
9186 (built-in or in a scope which has finished) or simply
9187 have more refined argument types than any declaration
9188 found above. */
9189 struct c_binding *b;
9190 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
9191 if (B_IN_SCOPE (b, external_scope))
9192 break;
9193 if (b)
9194 {
9195 tree ext_decl, ext_type;
9196 ext_decl = b->decl;
9197 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
9198 if (TREE_CODE (ext_type) == FUNCTION_TYPE
9199 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
9200 TREE_TYPE (ext_type)))
9201 {
9202 current_function_prototype_locus
9203 = DECL_SOURCE_LOCATION (ext_decl);
9204 current_function_prototype_built_in
9205 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
9206 current_function_prototype_arg_types
9207 = TYPE_ARG_TYPES (ext_type);
9208 }
9209 }
9210 }
9211 }
9212
9213 /* Optionally warn of old-fashioned def with no previous prototype. */
9214 if (warn_strict_prototypes
9215 && old_decl != error_mark_node
9216 && !prototype_p (TREE_TYPE (decl1))
9217 && C_DECL_ISNT_PROTOTYPE (old_decl))
9218 warning_at (loc, OPT_Wstrict_prototypes,
9219 "function declaration isn%'t a prototype");
9220 /* Optionally warn of any global def with no previous prototype. */
9221 else if (warn_missing_prototypes
9222 && old_decl != error_mark_node
9223 && TREE_PUBLIC (decl1)
9224 && !MAIN_NAME_P (DECL_NAME (decl1))
9225 && C_DECL_ISNT_PROTOTYPE (old_decl)
9226 && !DECL_DECLARED_INLINE_P (decl1))
9227 warning_at (loc, OPT_Wmissing_prototypes,
9228 "no previous prototype for %qD", decl1);
9229 /* Optionally warn of any def with no previous prototype
9230 if the function has already been used. */
9231 else if (warn_missing_prototypes
9232 && old_decl != NULL_TREE
9233 && old_decl != error_mark_node
9234 && TREE_USED (old_decl)
9235 && !prototype_p (TREE_TYPE (old_decl)))
9236 warning_at (loc, OPT_Wmissing_prototypes,
9237 "%qD was used with no prototype before its definition", decl1);
9238 /* Optionally warn of any global def with no previous declaration. */
9239 else if (warn_missing_declarations
9240 && TREE_PUBLIC (decl1)
9241 && old_decl == NULL_TREE
9242 && !MAIN_NAME_P (DECL_NAME (decl1))
9243 && !DECL_DECLARED_INLINE_P (decl1))
9244 warning_at (loc, OPT_Wmissing_declarations,
9245 "no previous declaration for %qD",
9246 decl1);
9247 /* Optionally warn of any def with no previous declaration
9248 if the function has already been used. */
9249 else if (warn_missing_declarations
9250 && old_decl != NULL_TREE
9251 && old_decl != error_mark_node
9252 && TREE_USED (old_decl)
9253 && C_DECL_IMPLICIT (old_decl))
9254 warning_at (loc, OPT_Wmissing_declarations,
9255 "%qD was used with no declaration before its definition", decl1);
9256
9257 /* This function exists in static storage.
9258 (This does not mean `static' in the C sense!) */
9259 TREE_STATIC (decl1) = 1;
9260
9261 /* This is the earliest point at which we might know the assembler
9262 name of the function. Thus, if it's set before this, die horribly. */
9263 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
9264
9265 /* If #pragma weak was used, mark the decl weak now. */
9266 if (current_scope == file_scope)
9267 maybe_apply_pragma_weak (decl1);
9268
9269 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
9270 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
9271 {
9272 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
9273 != integer_type_node)
9274 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
9275 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
9276 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
9277 decl1);
9278
9279 check_main_parameter_types (decl1);
9280
9281 if (!TREE_PUBLIC (decl1))
9282 pedwarn (loc, OPT_Wmain,
9283 "%qD is normally a non-static function", decl1);
9284 }
9285
9286 /* Record the decl so that the function name is defined.
9287 If we already have a decl for this name, and it is a FUNCTION_DECL,
9288 use the old decl. */
9289
9290 current_function_decl = pushdecl (decl1);
9291
9292 push_scope ();
9293 declare_parm_level ();
9294
9295 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
9296 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
9297 DECL_ARTIFICIAL (resdecl) = 1;
9298 DECL_IGNORED_P (resdecl) = 1;
9299 DECL_RESULT (current_function_decl) = resdecl;
9300
9301 start_fname_decls ();
9302
9303 return true;
9304 }
9305 \f
9306 /* Subroutine of store_parm_decls which handles new-style function
9307 definitions (prototype format). The parms already have decls, so we
9308 need only record them as in effect and complain if any redundant
9309 old-style parm decls were written. */
9310 static void
9311 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
9312 {
9313 tree decl;
9314 c_arg_tag *tag;
9315 unsigned ix;
9316
9317 if (current_scope->bindings)
9318 {
9319 error_at (DECL_SOURCE_LOCATION (fndecl),
9320 "old-style parameter declarations in prototyped "
9321 "function definition");
9322
9323 /* Get rid of the old-style declarations. */
9324 pop_scope ();
9325 push_scope ();
9326 }
9327 /* Don't issue this warning for nested functions, and don't issue this
9328 warning if we got here because ARG_INFO_TYPES was error_mark_node
9329 (this happens when a function definition has just an ellipsis in
9330 its parameter list). */
9331 else if (!in_system_header_at (input_location)
9332 && !current_function_scope
9333 && arg_info->types != error_mark_node)
9334 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
9335 "traditional C rejects ISO C style function definitions");
9336
9337 /* Now make all the parameter declarations visible in the function body.
9338 We can bypass most of the grunt work of pushdecl. */
9339 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
9340 {
9341 DECL_CONTEXT (decl) = current_function_decl;
9342 if (DECL_NAME (decl))
9343 {
9344 bind (DECL_NAME (decl), decl, current_scope,
9345 /*invisible=*/false, /*nested=*/false,
9346 UNKNOWN_LOCATION);
9347 if (!TREE_USED (decl))
9348 warn_if_shadowing (decl);
9349 }
9350 else
9351 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
9352 }
9353
9354 /* Record the parameter list in the function declaration. */
9355 DECL_ARGUMENTS (fndecl) = arg_info->parms;
9356
9357 /* Now make all the ancillary declarations visible, likewise. */
9358 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
9359 {
9360 DECL_CONTEXT (decl) = current_function_decl;
9361 if (DECL_NAME (decl))
9362 bind (DECL_NAME (decl), decl, current_scope,
9363 /*invisible=*/false,
9364 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
9365 UNKNOWN_LOCATION);
9366 }
9367
9368 /* And all the tag declarations. */
9369 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
9370 if (tag->id)
9371 bind (tag->id, tag->type, current_scope,
9372 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
9373 }
9374
9375 /* Subroutine of store_parm_decls which handles old-style function
9376 definitions (separate parameter list and declarations). */
9377
9378 static void
9379 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
9380 {
9381 struct c_binding *b;
9382 tree parm, decl, last;
9383 tree parmids = arg_info->parms;
9384 hash_set<tree> seen_args;
9385
9386 if (!in_system_header_at (input_location))
9387 {
9388 if (flag_isoc2x)
9389 pedwarn (DECL_SOURCE_LOCATION (fndecl),
9390 OPT_Wold_style_definition, "old-style function definition");
9391 else
9392 warning_at (DECL_SOURCE_LOCATION (fndecl),
9393 OPT_Wold_style_definition,
9394 "old-style function definition");
9395 }
9396
9397 if (current_scope->had_vla_unspec)
9398 error ("%<[*]%> not allowed in other than function prototype scope");
9399
9400 /* Match each formal parameter name with its declaration. Save each
9401 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
9402 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9403 {
9404 if (TREE_VALUE (parm) == NULL_TREE)
9405 {
9406 error_at (DECL_SOURCE_LOCATION (fndecl),
9407 "parameter name missing from parameter list");
9408 TREE_PURPOSE (parm) = NULL_TREE;
9409 continue;
9410 }
9411
9412 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
9413 if (b && B_IN_CURRENT_SCOPE (b))
9414 {
9415 decl = b->decl;
9416 /* Skip erroneous parameters. */
9417 if (decl == error_mark_node)
9418 continue;
9419 /* If we got something other than a PARM_DECL it is an error. */
9420 if (TREE_CODE (decl) != PARM_DECL)
9421 {
9422 error_at (DECL_SOURCE_LOCATION (decl),
9423 "%qD declared as a non-parameter", decl);
9424 continue;
9425 }
9426 /* If the declaration is already marked, we have a duplicate
9427 name. Complain and ignore the duplicate. */
9428 else if (seen_args.contains (decl))
9429 {
9430 error_at (DECL_SOURCE_LOCATION (decl),
9431 "multiple parameters named %qD", decl);
9432 TREE_PURPOSE (parm) = NULL_TREE;
9433 continue;
9434 }
9435 /* If the declaration says "void", complain and turn it into
9436 an int. */
9437 else if (VOID_TYPE_P (TREE_TYPE (decl)))
9438 {
9439 error_at (DECL_SOURCE_LOCATION (decl),
9440 "parameter %qD declared with void type", decl);
9441 TREE_TYPE (decl) = integer_type_node;
9442 DECL_ARG_TYPE (decl) = integer_type_node;
9443 layout_decl (decl, 0);
9444 }
9445 warn_if_shadowing (decl);
9446 }
9447 /* If no declaration found, default to int. */
9448 else
9449 {
9450 /* FIXME diagnostics: This should be the location of the argument,
9451 not the FNDECL. E.g., for an old-style declaration
9452
9453 int f10(v) { blah; }
9454
9455 We should use the location of the V, not the F10.
9456 Unfortunately, the V is an IDENTIFIER_NODE which has no
9457 location. In the future we need locations for c_arg_info
9458 entries.
9459
9460 See gcc.dg/Wshadow-3.c for an example of this problem. */
9461 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
9462 PARM_DECL, TREE_VALUE (parm), integer_type_node);
9463 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9464 pushdecl (decl);
9465 warn_if_shadowing (decl);
9466
9467 if (flag_isoc99)
9468 pedwarn (DECL_SOURCE_LOCATION (decl),
9469 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9470 decl);
9471 else
9472 warning_at (DECL_SOURCE_LOCATION (decl),
9473 OPT_Wmissing_parameter_type,
9474 "type of %qD defaults to %<int%>", decl);
9475 }
9476
9477 TREE_PURPOSE (parm) = decl;
9478 seen_args.add (decl);
9479 }
9480
9481 /* Now examine the parms chain for incomplete declarations
9482 and declarations with no corresponding names. */
9483
9484 for (b = current_scope->bindings; b; b = b->prev)
9485 {
9486 parm = b->decl;
9487 if (TREE_CODE (parm) != PARM_DECL)
9488 continue;
9489
9490 if (TREE_TYPE (parm) != error_mark_node
9491 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9492 {
9493 error_at (DECL_SOURCE_LOCATION (parm),
9494 "parameter %qD has incomplete type", parm);
9495 TREE_TYPE (parm) = error_mark_node;
9496 }
9497
9498 if (!seen_args.contains (parm))
9499 {
9500 error_at (DECL_SOURCE_LOCATION (parm),
9501 "declaration for parameter %qD but no such parameter",
9502 parm);
9503
9504 /* Pretend the parameter was not missing.
9505 This gets us to a standard state and minimizes
9506 further error messages. */
9507 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9508 }
9509 }
9510
9511 /* Chain the declarations together in the order of the list of
9512 names. Store that chain in the function decl, replacing the
9513 list of names. Update the current scope to match. */
9514 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9515
9516 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9517 if (TREE_PURPOSE (parm))
9518 break;
9519 if (parm && TREE_PURPOSE (parm))
9520 {
9521 last = TREE_PURPOSE (parm);
9522 DECL_ARGUMENTS (fndecl) = last;
9523
9524 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9525 if (TREE_PURPOSE (parm))
9526 {
9527 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9528 last = TREE_PURPOSE (parm);
9529 }
9530 DECL_CHAIN (last) = NULL_TREE;
9531 }
9532
9533 /* If there was a previous prototype,
9534 set the DECL_ARG_TYPE of each argument according to
9535 the type previously specified, and report any mismatches. */
9536
9537 if (current_function_prototype_arg_types)
9538 {
9539 tree type;
9540 for (parm = DECL_ARGUMENTS (fndecl),
9541 type = current_function_prototype_arg_types;
9542 parm || (type != NULL_TREE
9543 && TREE_VALUE (type) != error_mark_node
9544 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
9545 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
9546 {
9547 if (parm == NULL_TREE
9548 || type == NULL_TREE
9549 || (TREE_VALUE (type) != error_mark_node
9550 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
9551 {
9552 if (current_function_prototype_built_in)
9553 warning_at (DECL_SOURCE_LOCATION (fndecl),
9554 0, "number of arguments doesn%'t match "
9555 "built-in prototype");
9556 else
9557 {
9558 /* FIXME diagnostics: This should be the location of
9559 FNDECL, but there is bug when a prototype is
9560 declared inside function context, but defined
9561 outside of it (e.g., gcc.dg/pr15698-2.c). In
9562 which case FNDECL gets the location of the
9563 prototype, not the definition. */
9564 error_at (input_location,
9565 "number of arguments doesn%'t match prototype");
9566
9567 error_at (current_function_prototype_locus,
9568 "prototype declaration");
9569 }
9570 break;
9571 }
9572 /* Type for passing arg must be consistent with that
9573 declared for the arg. ISO C says we take the unqualified
9574 type for parameters declared with qualified type. */
9575 if (TREE_TYPE (parm) != error_mark_node
9576 && TREE_VALUE (type) != error_mark_node
9577 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9578 != TYPE_ATOMIC (TREE_VALUE (type)))
9579 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9580 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9581 {
9582 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9583 == TYPE_ATOMIC (TREE_VALUE (type)))
9584 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9585 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9586 {
9587 /* Adjust argument to match prototype. E.g. a previous
9588 `int foo(float);' prototype causes
9589 `int foo(x) float x; {...}' to be treated like
9590 `int foo(float x) {...}'. This is particularly
9591 useful for argument types like uid_t. */
9592 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9593
9594 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9595 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9596 && (TYPE_PRECISION (TREE_TYPE (parm))
9597 < TYPE_PRECISION (integer_type_node)))
9598 DECL_ARG_TYPE (parm)
9599 = c_type_promotes_to (TREE_TYPE (parm));
9600
9601 /* ??? Is it possible to get here with a
9602 built-in prototype or will it always have
9603 been diagnosed as conflicting with an
9604 old-style definition and discarded? */
9605 if (current_function_prototype_built_in)
9606 warning_at (DECL_SOURCE_LOCATION (parm),
9607 OPT_Wpedantic, "promoted argument %qD "
9608 "doesn%'t match built-in prototype", parm);
9609 else
9610 {
9611 pedwarn (DECL_SOURCE_LOCATION (parm),
9612 OPT_Wpedantic, "promoted argument %qD "
9613 "doesn%'t match prototype", parm);
9614 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9615 "prototype declaration");
9616 }
9617 }
9618 else
9619 {
9620 if (current_function_prototype_built_in)
9621 warning_at (DECL_SOURCE_LOCATION (parm),
9622 0, "argument %qD doesn%'t match "
9623 "built-in prototype", parm);
9624 else
9625 {
9626 error_at (DECL_SOURCE_LOCATION (parm),
9627 "argument %qD doesn%'t match prototype", parm);
9628 error_at (current_function_prototype_locus,
9629 "prototype declaration");
9630 }
9631 }
9632 }
9633 }
9634 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9635 }
9636
9637 /* Otherwise, create a prototype that would match. */
9638
9639 else
9640 {
9641 tree actual = NULL_TREE, last = NULL_TREE, type;
9642
9643 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9644 {
9645 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9646 if (last)
9647 TREE_CHAIN (last) = type;
9648 else
9649 actual = type;
9650 last = type;
9651 }
9652 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9653 if (last)
9654 TREE_CHAIN (last) = type;
9655 else
9656 actual = type;
9657
9658 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9659 of the type of this function, but we need to avoid having this
9660 affect the types of other similarly-typed functions, so we must
9661 first force the generation of an identical (but separate) type
9662 node for the relevant function type. The new node we create
9663 will be a variant of the main variant of the original function
9664 type. */
9665
9666 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9667
9668 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9669 }
9670 }
9671
9672 /* Store parameter declarations passed in ARG_INFO into the current
9673 function declaration. */
9674
9675 void
9676 store_parm_decls_from (struct c_arg_info *arg_info)
9677 {
9678 current_function_arg_info = arg_info;
9679 store_parm_decls ();
9680 }
9681
9682 /* Called by walk_tree to look for and update context-less labels. */
9683
9684 static tree
9685 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9686 {
9687 if (TREE_CODE (*tp) == LABEL_EXPR
9688 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9689 {
9690 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9691 *walk_subtrees = 0;
9692 }
9693
9694 return NULL_TREE;
9695 }
9696
9697 /* Store the parameter declarations into the current function declaration.
9698 This is called after parsing the parameter declarations, before
9699 digesting the body of the function.
9700
9701 For an old-style definition, construct a prototype out of the old-style
9702 parameter declarations and inject it into the function's type. */
9703
9704 void
9705 store_parm_decls (void)
9706 {
9707 tree fndecl = current_function_decl;
9708 bool proto;
9709
9710 /* The argument information block for FNDECL. */
9711 struct c_arg_info *arg_info = current_function_arg_info;
9712 current_function_arg_info = 0;
9713
9714 /* True if this definition is written with a prototype. In C2X, an
9715 empty argument list was converted to (void) in grokparms; in
9716 older C standard versions, it does not give the function a type
9717 with a prototype for future calls. */
9718 proto = arg_info->types != 0;
9719
9720 if (proto)
9721 store_parm_decls_newstyle (fndecl, arg_info);
9722 else
9723 store_parm_decls_oldstyle (fndecl, arg_info);
9724
9725 /* The next call to push_scope will be a function body. */
9726
9727 next_is_function_body = true;
9728
9729 /* Write a record describing this function definition to the prototypes
9730 file (if requested). */
9731
9732 gen_aux_info_record (fndecl, 1, 0, proto);
9733
9734 /* Initialize the RTL code for the function. */
9735 allocate_struct_function (fndecl, false);
9736
9737 if (warn_unused_local_typedefs)
9738 cfun->language = ggc_cleared_alloc<language_function> ();
9739
9740 /* Begin the statement tree for this function. */
9741 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9742
9743 /* ??? Insert the contents of the pending sizes list into the function
9744 to be evaluated. The only reason left to have this is
9745 void foo(int n, int array[n++])
9746 because we throw away the array type in favor of a pointer type, and
9747 thus won't naturally see the SAVE_EXPR containing the increment. All
9748 other pending sizes would be handled by gimplify_parameters. */
9749 if (arg_info->pending_sizes)
9750 {
9751 /* In very special circumstances, e.g. for code like
9752 _Atomic int i = 5;
9753 void f (int a[i += 2]) {}
9754 we need to execute the atomic assignment on function entry.
9755 But in this case, it is not just a straight store, it has the
9756 op= form, which means that build_atomic_assign has generated
9757 gotos, labels, etc. Because at that time the function decl
9758 for F has not been created yet, those labels do not have any
9759 function context. But we have the fndecl now, so update the
9760 labels accordingly. gimplify_expr would crash otherwise. */
9761 walk_tree_without_duplicates (&arg_info->pending_sizes,
9762 set_labels_context_r, fndecl);
9763 add_stmt (arg_info->pending_sizes);
9764 }
9765 }
9766
9767 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9768 c_finish_omp_declare_simd for function prototypes. No diagnostics
9769 should be done. */
9770
9771 void
9772 temp_store_parm_decls (tree fndecl, tree parms)
9773 {
9774 push_scope ();
9775 for (tree p = parms; p; p = DECL_CHAIN (p))
9776 {
9777 DECL_CONTEXT (p) = fndecl;
9778 if (DECL_NAME (p))
9779 bind (DECL_NAME (p), p, current_scope,
9780 /*invisible=*/false, /*nested=*/false,
9781 UNKNOWN_LOCATION);
9782 }
9783 }
9784
9785 /* Undo what temp_store_parm_decls did. */
9786
9787 void
9788 temp_pop_parm_decls (void)
9789 {
9790 /* Clear all bindings in this temporary scope, so that
9791 pop_scope doesn't create a BLOCK. */
9792 struct c_binding *b = current_scope->bindings;
9793 current_scope->bindings = NULL;
9794 for (; b; b = free_binding_and_advance (b))
9795 {
9796 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9797 || b->decl == error_mark_node);
9798 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9799 I_SYMBOL_BINDING (b->id) = b->shadowed;
9800 if (b->shadowed && b->shadowed->u.type)
9801 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9802 }
9803 pop_scope ();
9804 }
9805 \f
9806
9807 /* Finish up a function declaration and compile that function
9808 all the way to assembler language output. Then free the storage
9809 for the function definition.
9810
9811 This is called after parsing the body of the function definition. */
9812
9813 void
9814 finish_function (void)
9815 {
9816 tree fndecl = current_function_decl;
9817
9818 if (c_dialect_objc ())
9819 objc_finish_function ();
9820
9821 if (TREE_CODE (fndecl) == FUNCTION_DECL
9822 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9823 {
9824 tree args = DECL_ARGUMENTS (fndecl);
9825 for (; args; args = DECL_CHAIN (args))
9826 {
9827 tree type = TREE_TYPE (args);
9828 if (INTEGRAL_TYPE_P (type)
9829 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9830 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9831 }
9832 }
9833
9834 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9835 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9836
9837 /* Must mark the RESULT_DECL as being in this function. */
9838
9839 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9840 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9841
9842 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9843 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9844 == integer_type_node && flag_isoc99)
9845 {
9846 /* Hack. We don't want the middle-end to warn that this return
9847 is unreachable, so we mark its location as special. Using
9848 UNKNOWN_LOCATION has the problem that it gets clobbered in
9849 annotate_one_with_locus. A cleaner solution might be to
9850 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9851 */
9852 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9853 }
9854
9855 /* Tie off the statement tree for this function. */
9856 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9857
9858 finish_fname_decls ();
9859
9860 /* Complain if there's no return statement only if option specified on
9861 command line. */
9862 if (warn_return_type > 0
9863 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9864 && !current_function_returns_value && !current_function_returns_null
9865 /* Don't complain if we are no-return. */
9866 && !current_function_returns_abnormally
9867 /* Don't complain if we are declared noreturn. */
9868 && !TREE_THIS_VOLATILE (fndecl)
9869 /* Don't warn for main(). */
9870 && !MAIN_NAME_P (DECL_NAME (fndecl))
9871 /* Or if they didn't actually specify a return type. */
9872 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9873 /* Normally, with -Wreturn-type, flow will complain, but we might
9874 optimize out static functions. */
9875 && !TREE_PUBLIC (fndecl)
9876 && targetm.warn_func_return (fndecl)
9877 && warning (OPT_Wreturn_type,
9878 "no return statement in function returning non-void"))
9879 TREE_NO_WARNING (fndecl) = 1;
9880
9881 /* Complain about parameters that are only set, but never otherwise used. */
9882 if (warn_unused_but_set_parameter)
9883 {
9884 tree decl;
9885
9886 for (decl = DECL_ARGUMENTS (fndecl);
9887 decl;
9888 decl = DECL_CHAIN (decl))
9889 if (TREE_USED (decl)
9890 && TREE_CODE (decl) == PARM_DECL
9891 && !DECL_READ_P (decl)
9892 && DECL_NAME (decl)
9893 && !DECL_ARTIFICIAL (decl)
9894 && !TREE_NO_WARNING (decl))
9895 warning_at (DECL_SOURCE_LOCATION (decl),
9896 OPT_Wunused_but_set_parameter,
9897 "parameter %qD set but not used", decl);
9898 }
9899
9900 /* Complain about locally defined typedefs that are not used in this
9901 function. */
9902 maybe_warn_unused_local_typedefs ();
9903
9904 /* Possibly warn about unused parameters. */
9905 if (warn_unused_parameter)
9906 do_warn_unused_parameter (fndecl);
9907
9908 /* Store the end of the function, so that we get good line number
9909 info for the epilogue. */
9910 cfun->function_end_locus = input_location;
9911
9912 /* Finalize the ELF visibility for the function. */
9913 c_determine_visibility (fndecl);
9914
9915 /* For GNU C extern inline functions disregard inline limits. */
9916 if (DECL_EXTERNAL (fndecl)
9917 && DECL_DECLARED_INLINE_P (fndecl)
9918 && (flag_gnu89_inline
9919 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
9920 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9921
9922 /* Genericize before inlining. Delay genericizing nested functions
9923 until their parent function is genericized. Since finalizing
9924 requires GENERIC, delay that as well. */
9925
9926 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9927 && !undef_nested_function)
9928 {
9929 if (!decl_function_context (fndecl))
9930 {
9931 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9932 c_genericize (fndecl);
9933
9934 /* ??? Objc emits functions after finalizing the compilation unit.
9935 This should be cleaned up later and this conditional removed. */
9936 if (symtab->global_info_ready)
9937 {
9938 cgraph_node::add_new_function (fndecl, false);
9939 return;
9940 }
9941 cgraph_node::finalize_function (fndecl, false);
9942 }
9943 else
9944 {
9945 /* Register this function with cgraph just far enough to get it
9946 added to our parent's nested function list. Handy, since the
9947 C front end doesn't have such a list. */
9948 (void) cgraph_node::get_create (fndecl);
9949 }
9950 }
9951
9952 if (!decl_function_context (fndecl))
9953 undef_nested_function = false;
9954
9955 if (cfun->language != NULL)
9956 {
9957 ggc_free (cfun->language);
9958 cfun->language = NULL;
9959 }
9960
9961 /* We're leaving the context of this function, so zap cfun.
9962 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9963 tree_rest_of_compilation. */
9964 set_cfun (NULL);
9965 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9966 current_function_decl = NULL;
9967 }
9968 \f
9969 /* Check the declarations given in a for-loop for satisfying the C99
9970 constraints. If exactly one such decl is found, return it. LOC is
9971 the location of the opening parenthesis of the for loop. The last
9972 parameter allows you to control the "for loop initial declarations
9973 are only allowed in C99 mode". Normally, you should pass
9974 flag_isoc99 as that parameter. But in some cases (Objective-C
9975 foreach loop, for example) we want to run the checks in this
9976 function even if not in C99 mode, so we allow the caller to turn
9977 off the error about not being in C99 mode.
9978 */
9979
9980 tree
9981 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9982 {
9983 struct c_binding *b;
9984 tree one_decl = NULL_TREE;
9985 int n_decls = 0;
9986
9987 if (!turn_off_iso_c99_error)
9988 {
9989 static bool hint = true;
9990 /* If we get here, declarations have been used in a for loop without
9991 the C99 for loop scope. This doesn't make much sense, so don't
9992 allow it. */
9993 error_at (loc, "%<for%> loop initial declarations "
9994 "are only allowed in C99 or C11 mode");
9995 if (hint)
9996 {
9997 inform (loc,
9998 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
9999 "%<-std=gnu11%> to compile your code");
10000 hint = false;
10001 }
10002 return NULL_TREE;
10003 }
10004 else
10005 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
10006 "initial declarations");
10007
10008 /* C99 subclause 6.8.5 paragraph 3:
10009
10010 [#3] The declaration part of a for statement shall only
10011 declare identifiers for objects having storage class auto or
10012 register.
10013
10014 It isn't clear whether, in this sentence, "identifiers" binds to
10015 "shall only declare" or to "objects" - that is, whether all identifiers
10016 declared must be identifiers for objects, or whether the restriction
10017 only applies to those that are. (A question on this in comp.std.c
10018 in November 2000 received no answer.) We implement the strictest
10019 interpretation, to avoid creating an extension which later causes
10020 problems. */
10021
10022 for (b = current_scope->bindings; b; b = b->prev)
10023 {
10024 tree id = b->id;
10025 tree decl = b->decl;
10026
10027 if (!id)
10028 continue;
10029
10030 switch (TREE_CODE (decl))
10031 {
10032 case VAR_DECL:
10033 {
10034 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
10035 if (TREE_STATIC (decl))
10036 error_at (decl_loc,
10037 "declaration of static variable %qD in %<for%> loop "
10038 "initial declaration", decl);
10039 else if (DECL_EXTERNAL (decl))
10040 error_at (decl_loc,
10041 "declaration of %<extern%> variable %qD in %<for%> loop "
10042 "initial declaration", decl);
10043 }
10044 break;
10045
10046 case RECORD_TYPE:
10047 error_at (loc,
10048 "%<struct %E%> declared in %<for%> loop initial "
10049 "declaration", id);
10050 break;
10051 case UNION_TYPE:
10052 error_at (loc,
10053 "%<union %E%> declared in %<for%> loop initial declaration",
10054 id);
10055 break;
10056 case ENUMERAL_TYPE:
10057 error_at (loc, "%<enum %E%> declared in %<for%> loop "
10058 "initial declaration", id);
10059 break;
10060 default:
10061 error_at (loc, "declaration of non-variable "
10062 "%qD in %<for%> loop initial declaration", decl);
10063 }
10064
10065 n_decls++;
10066 one_decl = decl;
10067 }
10068
10069 return n_decls == 1 ? one_decl : NULL_TREE;
10070 }
10071 \f
10072 /* Save and reinitialize the variables
10073 used during compilation of a C function. */
10074
10075 void
10076 c_push_function_context (void)
10077 {
10078 struct language_function *p = cfun->language;
10079 /* cfun->language might have been already allocated by the use of
10080 -Wunused-local-typedefs. In that case, just re-use it. */
10081 if (p == NULL)
10082 cfun->language = p = ggc_cleared_alloc<language_function> ();
10083
10084 p->base.x_stmt_tree = c_stmt_tree;
10085 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
10086 p->x_break_label = c_break_label;
10087 p->x_cont_label = c_cont_label;
10088 p->x_switch_stack = c_switch_stack;
10089 p->arg_info = current_function_arg_info;
10090 p->returns_value = current_function_returns_value;
10091 p->returns_null = current_function_returns_null;
10092 p->returns_abnormally = current_function_returns_abnormally;
10093 p->warn_about_return_type = warn_about_return_type;
10094
10095 push_function_context ();
10096 }
10097
10098 /* Restore the variables used during compilation of a C function. */
10099
10100 void
10101 c_pop_function_context (void)
10102 {
10103 struct language_function *p;
10104
10105 pop_function_context ();
10106 p = cfun->language;
10107
10108 /* When -Wunused-local-typedefs is in effect, cfun->languages is
10109 used to store data throughout the life time of the current cfun,
10110 So don't deallocate it. */
10111 if (!warn_unused_local_typedefs)
10112 cfun->language = NULL;
10113
10114 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
10115 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
10116 {
10117 /* Stop pointing to the local nodes about to be freed. */
10118 /* But DECL_INITIAL must remain nonzero so we know this
10119 was an actual function definition. */
10120 DECL_INITIAL (current_function_decl) = error_mark_node;
10121 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
10122 }
10123
10124 c_stmt_tree = p->base.x_stmt_tree;
10125 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
10126 c_break_label = p->x_break_label;
10127 c_cont_label = p->x_cont_label;
10128 c_switch_stack = p->x_switch_stack;
10129 current_function_arg_info = p->arg_info;
10130 current_function_returns_value = p->returns_value;
10131 current_function_returns_null = p->returns_null;
10132 current_function_returns_abnormally = p->returns_abnormally;
10133 warn_about_return_type = p->warn_about_return_type;
10134 }
10135
10136 /* The functions below are required for functionality of doing
10137 function at once processing in the C front end. Currently these
10138 functions are not called from anywhere in the C front end, but as
10139 these changes continue, that will change. */
10140
10141 /* Returns the stmt_tree (if any) to which statements are currently
10142 being added. If there is no active statement-tree, NULL is
10143 returned. */
10144
10145 stmt_tree
10146 current_stmt_tree (void)
10147 {
10148 return &c_stmt_tree;
10149 }
10150
10151 /* Return the global value of T as a symbol. */
10152
10153 tree
10154 identifier_global_value (tree t)
10155 {
10156 struct c_binding *b;
10157
10158 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
10159 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
10160 return b->decl;
10161
10162 return NULL_TREE;
10163 }
10164
10165 /* Return the global value of tag T as a symbol. */
10166
10167 tree
10168 identifier_global_tag (tree t)
10169 {
10170 struct c_binding *b;
10171
10172 for (b = I_TAG_BINDING (t); b; b = b->shadowed)
10173 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
10174 return b->decl;
10175
10176 return NULL_TREE;
10177 }
10178
10179 /* Returns true if NAME refers to a built-in function or function-like
10180 operator. */
10181
10182 bool
10183 names_builtin_p (const char *name)
10184 {
10185 tree id = get_identifier (name);
10186 if (tree decl = identifier_global_value (id))
10187 return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_BUILTIN (decl);
10188
10189 /* Also detect common reserved C words that aren't strictly built-in
10190 functions. */
10191 switch (C_RID_CODE (id))
10192 {
10193 case RID_BUILTIN_CONVERTVECTOR:
10194 case RID_BUILTIN_HAS_ATTRIBUTE:
10195 case RID_BUILTIN_SHUFFLE:
10196 case RID_CHOOSE_EXPR:
10197 case RID_OFFSETOF:
10198 case RID_TYPES_COMPATIBLE_P:
10199 return true;
10200 default:
10201 break;
10202 }
10203
10204 return false;
10205 }
10206
10207 /* In C, the only C-linkage public declaration is at file scope. */
10208
10209 tree
10210 c_linkage_bindings (tree name)
10211 {
10212 return identifier_global_value (name);
10213 }
10214
10215 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
10216 otherwise the name is found in ridpointers from RID_INDEX. */
10217
10218 void
10219 record_builtin_type (enum rid rid_index, const char *name, tree type)
10220 {
10221 tree id, decl;
10222 if (name == 0)
10223 id = ridpointers[(int) rid_index];
10224 else
10225 id = get_identifier (name);
10226 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
10227 pushdecl (decl);
10228 if (debug_hooks->type_decl)
10229 debug_hooks->type_decl (decl, false);
10230 }
10231
10232 /* Build the void_list_node (void_type_node having been created). */
10233 tree
10234 build_void_list_node (void)
10235 {
10236 tree t = build_tree_list (NULL_TREE, void_type_node);
10237 return t;
10238 }
10239
10240 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
10241
10242 struct c_parm *
10243 build_c_parm (struct c_declspecs *specs, tree attrs,
10244 struct c_declarator *declarator,
10245 location_t loc)
10246 {
10247 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
10248 ret->specs = specs;
10249 ret->attrs = attrs;
10250 ret->declarator = declarator;
10251 ret->loc = loc;
10252 return ret;
10253 }
10254
10255 /* Return a declarator with nested attributes. TARGET is the inner
10256 declarator to which these attributes apply. ATTRS are the
10257 attributes. */
10258
10259 struct c_declarator *
10260 build_attrs_declarator (tree attrs, struct c_declarator *target)
10261 {
10262 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10263 ret->kind = cdk_attrs;
10264 ret->declarator = target;
10265 ret->u.attrs = attrs;
10266 return ret;
10267 }
10268
10269 /* Return a declarator for a function with arguments specified by ARGS
10270 and return type specified by TARGET. */
10271
10272 struct c_declarator *
10273 build_function_declarator (struct c_arg_info *args,
10274 struct c_declarator *target)
10275 {
10276 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10277 ret->kind = cdk_function;
10278 ret->declarator = target;
10279 ret->u.arg_info = args;
10280 return ret;
10281 }
10282
10283 /* Return a declarator for the identifier IDENT (which may be
10284 NULL_TREE for an abstract declarator). */
10285
10286 struct c_declarator *
10287 build_id_declarator (tree ident)
10288 {
10289 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10290 ret->kind = cdk_id;
10291 ret->declarator = 0;
10292 ret->u.id.id = ident;
10293 ret->u.id.attrs = NULL_TREE;
10294 /* Default value - may get reset to a more precise location. */
10295 ret->id_loc = input_location;
10296 return ret;
10297 }
10298
10299 /* Return something to represent absolute declarators containing a *.
10300 TARGET is the absolute declarator that the * contains.
10301 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
10302 to apply to the pointer type. */
10303
10304 struct c_declarator *
10305 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
10306 struct c_declarator *target)
10307 {
10308 tree attrs;
10309 int quals = 0;
10310 struct c_declarator *itarget = target;
10311 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10312 if (type_quals_attrs)
10313 {
10314 attrs = type_quals_attrs->attrs;
10315 quals = quals_from_declspecs (type_quals_attrs);
10316 if (attrs != NULL_TREE)
10317 itarget = build_attrs_declarator (attrs, target);
10318 }
10319 ret->kind = cdk_pointer;
10320 ret->declarator = itarget;
10321 ret->u.pointer_quals = quals;
10322 return ret;
10323 }
10324
10325 /* Return a pointer to a structure for an empty list of declaration
10326 specifiers. */
10327
10328 struct c_declspecs *
10329 build_null_declspecs (void)
10330 {
10331 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
10332 memset (ret, 0, sizeof *ret);
10333 ret->align_log = -1;
10334 ret->typespec_word = cts_none;
10335 ret->storage_class = csc_none;
10336 ret->expr_const_operands = true;
10337 ret->typespec_kind = ctsk_none;
10338 ret->address_space = ADDR_SPACE_GENERIC;
10339 return ret;
10340 }
10341
10342 /* Add the address space ADDRSPACE to the declaration specifiers
10343 SPECS, returning SPECS. */
10344
10345 struct c_declspecs *
10346 declspecs_add_addrspace (location_t location,
10347 struct c_declspecs *specs, addr_space_t as)
10348 {
10349 specs->non_sc_seen_p = true;
10350 specs->declspecs_seen_p = true;
10351 specs->non_std_attrs_seen_p = true;
10352
10353 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
10354 && specs->address_space != as)
10355 error ("incompatible address space qualifiers %qs and %qs",
10356 c_addr_space_name (as),
10357 c_addr_space_name (specs->address_space));
10358 else
10359 {
10360 specs->address_space = as;
10361 specs->locations[cdw_address_space] = location;
10362 }
10363 return specs;
10364 }
10365
10366 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
10367 returning SPECS. */
10368
10369 struct c_declspecs *
10370 declspecs_add_qual (location_t loc,
10371 struct c_declspecs *specs, tree qual)
10372 {
10373 enum rid i;
10374 bool dupe = false;
10375 specs->non_sc_seen_p = true;
10376 specs->declspecs_seen_p = true;
10377 specs->non_std_attrs_seen_p = true;
10378 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
10379 && C_IS_RESERVED_WORD (qual));
10380 i = C_RID_CODE (qual);
10381 location_t prev_loc = UNKNOWN_LOCATION;
10382 switch (i)
10383 {
10384 case RID_CONST:
10385 dupe = specs->const_p;
10386 specs->const_p = true;
10387 prev_loc = specs->locations[cdw_const];
10388 specs->locations[cdw_const] = loc;
10389 break;
10390 case RID_VOLATILE:
10391 dupe = specs->volatile_p;
10392 specs->volatile_p = true;
10393 prev_loc = specs->locations[cdw_volatile];
10394 specs->locations[cdw_volatile] = loc;
10395 break;
10396 case RID_RESTRICT:
10397 dupe = specs->restrict_p;
10398 specs->restrict_p = true;
10399 prev_loc = specs->locations[cdw_restrict];
10400 specs->locations[cdw_restrict] = loc;
10401 break;
10402 case RID_ATOMIC:
10403 dupe = specs->atomic_p;
10404 specs->atomic_p = true;
10405 prev_loc = specs->locations[cdw_atomic];
10406 specs->locations[cdw_atomic] = loc;
10407 break;
10408 default:
10409 gcc_unreachable ();
10410 }
10411 if (dupe)
10412 {
10413 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
10414 "duplicate %qE declaration specifier", qual);
10415 if (!warned
10416 && warn_duplicate_decl_specifier
10417 && prev_loc >= RESERVED_LOCATION_COUNT
10418 && !from_macro_expansion_at (prev_loc)
10419 && !from_macro_expansion_at (loc))
10420 warning_at (loc, OPT_Wduplicate_decl_specifier,
10421 "duplicate %qE declaration specifier", qual);
10422 }
10423 return specs;
10424 }
10425
10426 /* Add the type specifier TYPE to the declaration specifiers SPECS,
10427 returning SPECS. */
10428
10429 struct c_declspecs *
10430 declspecs_add_type (location_t loc, struct c_declspecs *specs,
10431 struct c_typespec spec)
10432 {
10433 tree type = spec.spec;
10434 specs->non_sc_seen_p = true;
10435 specs->declspecs_seen_p = true;
10436 specs->non_std_attrs_seen_p = true;
10437 specs->typespec_kind = spec.kind;
10438 if (TREE_DEPRECATED (type))
10439 specs->deprecated_p = true;
10440
10441 /* Handle type specifier keywords. */
10442 if (TREE_CODE (type) == IDENTIFIER_NODE
10443 && C_IS_RESERVED_WORD (type)
10444 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
10445 {
10446 enum rid i = C_RID_CODE (type);
10447 if (specs->type)
10448 {
10449 error_at (loc, "two or more data types in declaration specifiers");
10450 return specs;
10451 }
10452 if ((int) i <= (int) RID_LAST_MODIFIER)
10453 {
10454 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
10455 bool dupe = false;
10456 switch (i)
10457 {
10458 case RID_LONG:
10459 if (specs->long_long_p)
10460 {
10461 error_at (loc, "%<long long long%> is too long for GCC");
10462 break;
10463 }
10464 if (specs->long_p)
10465 {
10466 if (specs->typespec_word == cts_double)
10467 {
10468 error_at (loc,
10469 ("both %<long long%> and %<double%> in "
10470 "declaration specifiers"));
10471 break;
10472 }
10473 pedwarn_c90 (loc, OPT_Wlong_long,
10474 "ISO C90 does not support %<long long%>");
10475 specs->long_long_p = 1;
10476 specs->locations[cdw_long_long] = loc;
10477 break;
10478 }
10479 if (specs->short_p)
10480 error_at (loc,
10481 ("both %<long%> and %<short%> in "
10482 "declaration specifiers"));
10483 else if (specs->typespec_word == cts_auto_type)
10484 error_at (loc,
10485 ("both %<long%> and %<__auto_type%> in "
10486 "declaration specifiers"));
10487 else if (specs->typespec_word == cts_void)
10488 error_at (loc,
10489 ("both %<long%> and %<void%> in "
10490 "declaration specifiers"));
10491 else if (specs->typespec_word == cts_int_n)
10492 error_at (loc,
10493 ("both %<long%> and %<__int%d%> in "
10494 "declaration specifiers"),
10495 int_n_data[specs->int_n_idx].bitsize);
10496 else if (specs->typespec_word == cts_bool)
10497 error_at (loc,
10498 ("both %<long%> and %<_Bool%> in "
10499 "declaration specifiers"));
10500 else if (specs->typespec_word == cts_char)
10501 error_at (loc,
10502 ("both %<long%> and %<char%> in "
10503 "declaration specifiers"));
10504 else if (specs->typespec_word == cts_float)
10505 error_at (loc,
10506 ("both %<long%> and %<float%> in "
10507 "declaration specifiers"));
10508 else if (specs->typespec_word == cts_floatn_nx)
10509 error_at (loc,
10510 ("both %<long%> and %<_Float%d%s%> in "
10511 "declaration specifiers"),
10512 floatn_nx_types[specs->floatn_nx_idx].n,
10513 (floatn_nx_types[specs->floatn_nx_idx].extended
10514 ? "x"
10515 : ""));
10516 else if (specs->typespec_word == cts_dfloat32)
10517 error_at (loc,
10518 ("both %<long%> and %<_Decimal32%> in "
10519 "declaration specifiers"));
10520 else if (specs->typespec_word == cts_dfloat64)
10521 error_at (loc,
10522 ("both %<long%> and %<_Decimal64%> in "
10523 "declaration specifiers"));
10524 else if (specs->typespec_word == cts_dfloat128)
10525 error_at (loc,
10526 ("both %<long%> and %<_Decimal128%> in "
10527 "declaration specifiers"));
10528 else
10529 {
10530 specs->long_p = true;
10531 specs->locations[cdw_long] = loc;
10532 }
10533 break;
10534 case RID_SHORT:
10535 dupe = specs->short_p;
10536 if (specs->long_p)
10537 error_at (loc,
10538 ("both %<long%> and %<short%> in "
10539 "declaration specifiers"));
10540 else if (specs->typespec_word == cts_auto_type)
10541 error_at (loc,
10542 ("both %<short%> and %<__auto_type%> in "
10543 "declaration specifiers"));
10544 else if (specs->typespec_word == cts_void)
10545 error_at (loc,
10546 ("both %<short%> and %<void%> in "
10547 "declaration specifiers"));
10548 else if (specs->typespec_word == cts_int_n)
10549 error_at (loc,
10550 ("both %<short%> and %<__int%d%> in "
10551 "declaration specifiers"),
10552 int_n_data[specs->int_n_idx].bitsize);
10553 else if (specs->typespec_word == cts_bool)
10554 error_at (loc,
10555 ("both %<short%> and %<_Bool%> in "
10556 "declaration specifiers"));
10557 else if (specs->typespec_word == cts_char)
10558 error_at (loc,
10559 ("both %<short%> and %<char%> in "
10560 "declaration specifiers"));
10561 else if (specs->typespec_word == cts_float)
10562 error_at (loc,
10563 ("both %<short%> and %<float%> in "
10564 "declaration specifiers"));
10565 else if (specs->typespec_word == cts_double)
10566 error_at (loc,
10567 ("both %<short%> and %<double%> in "
10568 "declaration specifiers"));
10569 else if (specs->typespec_word == cts_floatn_nx)
10570 error_at (loc,
10571 ("both %<short%> and %<_Float%d%s%> in "
10572 "declaration specifiers"),
10573 floatn_nx_types[specs->floatn_nx_idx].n,
10574 (floatn_nx_types[specs->floatn_nx_idx].extended
10575 ? "x"
10576 : ""));
10577 else if (specs->typespec_word == cts_dfloat32)
10578 error_at (loc,
10579 ("both %<short%> and %<_Decimal32%> in "
10580 "declaration specifiers"));
10581 else if (specs->typespec_word == cts_dfloat64)
10582 error_at (loc,
10583 ("both %<short%> and %<_Decimal64%> in "
10584 "declaration specifiers"));
10585 else if (specs->typespec_word == cts_dfloat128)
10586 error_at (loc,
10587 ("both %<short%> and %<_Decimal128%> in "
10588 "declaration specifiers"));
10589 else
10590 {
10591 specs->short_p = true;
10592 specs->locations[cdw_short] = loc;
10593 }
10594 break;
10595 case RID_SIGNED:
10596 dupe = specs->signed_p;
10597 if (specs->unsigned_p)
10598 error_at (loc,
10599 ("both %<signed%> and %<unsigned%> in "
10600 "declaration specifiers"));
10601 else if (specs->typespec_word == cts_auto_type)
10602 error_at (loc,
10603 ("both %<signed%> and %<__auto_type%> in "
10604 "declaration specifiers"));
10605 else if (specs->typespec_word == cts_void)
10606 error_at (loc,
10607 ("both %<signed%> and %<void%> in "
10608 "declaration specifiers"));
10609 else if (specs->typespec_word == cts_bool)
10610 error_at (loc,
10611 ("both %<signed%> and %<_Bool%> in "
10612 "declaration specifiers"));
10613 else if (specs->typespec_word == cts_float)
10614 error_at (loc,
10615 ("both %<signed%> and %<float%> in "
10616 "declaration specifiers"));
10617 else if (specs->typespec_word == cts_double)
10618 error_at (loc,
10619 ("both %<signed%> and %<double%> in "
10620 "declaration specifiers"));
10621 else if (specs->typespec_word == cts_floatn_nx)
10622 error_at (loc,
10623 ("both %<signed%> and %<_Float%d%s%> in "
10624 "declaration specifiers"),
10625 floatn_nx_types[specs->floatn_nx_idx].n,
10626 (floatn_nx_types[specs->floatn_nx_idx].extended
10627 ? "x"
10628 : ""));
10629 else if (specs->typespec_word == cts_dfloat32)
10630 error_at (loc,
10631 ("both %<signed%> and %<_Decimal32%> in "
10632 "declaration specifiers"));
10633 else if (specs->typespec_word == cts_dfloat64)
10634 error_at (loc,
10635 ("both %<signed%> and %<_Decimal64%> in "
10636 "declaration specifiers"));
10637 else if (specs->typespec_word == cts_dfloat128)
10638 error_at (loc,
10639 ("both %<signed%> and %<_Decimal128%> in "
10640 "declaration specifiers"));
10641 else
10642 {
10643 specs->signed_p = true;
10644 specs->locations[cdw_signed] = loc;
10645 }
10646 break;
10647 case RID_UNSIGNED:
10648 dupe = specs->unsigned_p;
10649 if (specs->signed_p)
10650 error_at (loc,
10651 ("both %<signed%> and %<unsigned%> in "
10652 "declaration specifiers"));
10653 else if (specs->typespec_word == cts_auto_type)
10654 error_at (loc,
10655 ("both %<unsigned%> and %<__auto_type%> in "
10656 "declaration specifiers"));
10657 else if (specs->typespec_word == cts_void)
10658 error_at (loc,
10659 ("both %<unsigned%> and %<void%> in "
10660 "declaration specifiers"));
10661 else if (specs->typespec_word == cts_bool)
10662 error_at (loc,
10663 ("both %<unsigned%> and %<_Bool%> in "
10664 "declaration specifiers"));
10665 else if (specs->typespec_word == cts_float)
10666 error_at (loc,
10667 ("both %<unsigned%> and %<float%> in "
10668 "declaration specifiers"));
10669 else if (specs->typespec_word == cts_double)
10670 error_at (loc,
10671 ("both %<unsigned%> and %<double%> in "
10672 "declaration specifiers"));
10673 else if (specs->typespec_word == cts_floatn_nx)
10674 error_at (loc,
10675 ("both %<unsigned%> and %<_Float%d%s%> in "
10676 "declaration specifiers"),
10677 floatn_nx_types[specs->floatn_nx_idx].n,
10678 (floatn_nx_types[specs->floatn_nx_idx].extended
10679 ? "x"
10680 : ""));
10681 else if (specs->typespec_word == cts_dfloat32)
10682 error_at (loc,
10683 ("both %<unsigned%> and %<_Decimal32%> in "
10684 "declaration specifiers"));
10685 else if (specs->typespec_word == cts_dfloat64)
10686 error_at (loc,
10687 ("both %<unsigned%> and %<_Decimal64%> in "
10688 "declaration specifiers"));
10689 else if (specs->typespec_word == cts_dfloat128)
10690 error_at (loc,
10691 ("both %<unsigned%> and %<_Decimal128%> in "
10692 "declaration specifiers"));
10693 else
10694 {
10695 specs->unsigned_p = true;
10696 specs->locations[cdw_unsigned] = loc;
10697 }
10698 break;
10699 case RID_COMPLEX:
10700 dupe = specs->complex_p;
10701 if (!in_system_header_at (loc))
10702 pedwarn_c90 (loc, OPT_Wpedantic,
10703 "ISO C90 does not support complex types");
10704 if (specs->typespec_word == cts_auto_type)
10705 error_at (loc,
10706 ("both %<complex%> and %<__auto_type%> in "
10707 "declaration specifiers"));
10708 else if (specs->typespec_word == cts_void)
10709 error_at (loc,
10710 ("both %<complex%> and %<void%> in "
10711 "declaration specifiers"));
10712 else if (specs->typespec_word == cts_bool)
10713 error_at (loc,
10714 ("both %<complex%> and %<_Bool%> in "
10715 "declaration specifiers"));
10716 else if (specs->typespec_word == cts_dfloat32)
10717 error_at (loc,
10718 ("both %<complex%> and %<_Decimal32%> in "
10719 "declaration specifiers"));
10720 else if (specs->typespec_word == cts_dfloat64)
10721 error_at (loc,
10722 ("both %<complex%> and %<_Decimal64%> in "
10723 "declaration specifiers"));
10724 else if (specs->typespec_word == cts_dfloat128)
10725 error_at (loc,
10726 ("both %<complex%> and %<_Decimal128%> in "
10727 "declaration specifiers"));
10728 else if (specs->typespec_word == cts_fract)
10729 error_at (loc,
10730 ("both %<complex%> and %<_Fract%> in "
10731 "declaration specifiers"));
10732 else if (specs->typespec_word == cts_accum)
10733 error_at (loc,
10734 ("both %<complex%> and %<_Accum%> in "
10735 "declaration specifiers"));
10736 else if (specs->saturating_p)
10737 error_at (loc,
10738 ("both %<complex%> and %<_Sat%> in "
10739 "declaration specifiers"));
10740 else
10741 {
10742 specs->complex_p = true;
10743 specs->locations[cdw_complex] = loc;
10744 }
10745 break;
10746 case RID_SAT:
10747 dupe = specs->saturating_p;
10748 pedwarn (loc, OPT_Wpedantic,
10749 "ISO C does not support saturating types");
10750 if (specs->typespec_word == cts_int_n)
10751 {
10752 error_at (loc,
10753 ("both %<_Sat%> and %<__int%d%> in "
10754 "declaration specifiers"),
10755 int_n_data[specs->int_n_idx].bitsize);
10756 }
10757 else if (specs->typespec_word == cts_auto_type)
10758 error_at (loc,
10759 ("both %<_Sat%> and %<__auto_type%> in "
10760 "declaration specifiers"));
10761 else if (specs->typespec_word == cts_void)
10762 error_at (loc,
10763 ("both %<_Sat%> and %<void%> in "
10764 "declaration specifiers"));
10765 else if (specs->typespec_word == cts_bool)
10766 error_at (loc,
10767 ("both %<_Sat%> and %<_Bool%> in "
10768 "declaration specifiers"));
10769 else if (specs->typespec_word == cts_char)
10770 error_at (loc,
10771 ("both %<_Sat%> and %<char%> in "
10772 "declaration specifiers"));
10773 else if (specs->typespec_word == cts_int)
10774 error_at (loc,
10775 ("both %<_Sat%> and %<int%> in "
10776 "declaration specifiers"));
10777 else if (specs->typespec_word == cts_float)
10778 error_at (loc,
10779 ("both %<_Sat%> and %<float%> in "
10780 "declaration specifiers"));
10781 else if (specs->typespec_word == cts_double)
10782 error_at (loc,
10783 ("both %<_Sat%> and %<double%> in "
10784 "declaration specifiers"));
10785 else if (specs->typespec_word == cts_floatn_nx)
10786 error_at (loc,
10787 ("both %<_Sat%> and %<_Float%d%s%> in "
10788 "declaration specifiers"),
10789 floatn_nx_types[specs->floatn_nx_idx].n,
10790 (floatn_nx_types[specs->floatn_nx_idx].extended
10791 ? "x"
10792 : ""));
10793 else if (specs->typespec_word == cts_dfloat32)
10794 error_at (loc,
10795 ("both %<_Sat%> and %<_Decimal32%> in "
10796 "declaration specifiers"));
10797 else if (specs->typespec_word == cts_dfloat64)
10798 error_at (loc,
10799 ("both %<_Sat%> and %<_Decimal64%> in "
10800 "declaration specifiers"));
10801 else if (specs->typespec_word == cts_dfloat128)
10802 error_at (loc,
10803 ("both %<_Sat%> and %<_Decimal128%> in "
10804 "declaration specifiers"));
10805 else if (specs->complex_p)
10806 error_at (loc,
10807 ("both %<_Sat%> and %<complex%> in "
10808 "declaration specifiers"));
10809 else
10810 {
10811 specs->saturating_p = true;
10812 specs->locations[cdw_saturating] = loc;
10813 }
10814 break;
10815 default:
10816 gcc_unreachable ();
10817 }
10818
10819 if (dupe)
10820 error_at (loc, "duplicate %qE", type);
10821
10822 return specs;
10823 }
10824 else
10825 {
10826 /* "void", "_Bool", "char", "int", "float", "double",
10827 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10828 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10829 "__auto_type". */
10830 if (specs->typespec_word != cts_none)
10831 {
10832 error_at (loc,
10833 "two or more data types in declaration specifiers");
10834 return specs;
10835 }
10836 switch (i)
10837 {
10838 case RID_AUTO_TYPE:
10839 if (specs->long_p)
10840 error_at (loc,
10841 ("both %<long%> and %<__auto_type%> in "
10842 "declaration specifiers"));
10843 else if (specs->short_p)
10844 error_at (loc,
10845 ("both %<short%> and %<__auto_type%> in "
10846 "declaration specifiers"));
10847 else if (specs->signed_p)
10848 error_at (loc,
10849 ("both %<signed%> and %<__auto_type%> in "
10850 "declaration specifiers"));
10851 else if (specs->unsigned_p)
10852 error_at (loc,
10853 ("both %<unsigned%> and %<__auto_type%> in "
10854 "declaration specifiers"));
10855 else if (specs->complex_p)
10856 error_at (loc,
10857 ("both %<complex%> and %<__auto_type%> in "
10858 "declaration specifiers"));
10859 else if (specs->saturating_p)
10860 error_at (loc,
10861 ("both %<_Sat%> and %<__auto_type%> in "
10862 "declaration specifiers"));
10863 else
10864 {
10865 specs->typespec_word = cts_auto_type;
10866 specs->locations[cdw_typespec] = loc;
10867 }
10868 return specs;
10869 case RID_INT_N_0:
10870 case RID_INT_N_1:
10871 case RID_INT_N_2:
10872 case RID_INT_N_3:
10873 specs->int_n_idx = i - RID_INT_N_0;
10874 if (!in_system_header_at (input_location)
10875 /* If the INT_N type ends in "__", and so is of the format
10876 "__intN__", don't pedwarn. */
10877 && (strncmp (IDENTIFIER_POINTER (type)
10878 + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
10879 pedwarn (loc, OPT_Wpedantic,
10880 "ISO C does not support %<__int%d%> types",
10881 int_n_data[specs->int_n_idx].bitsize);
10882
10883 if (specs->long_p)
10884 error_at (loc,
10885 ("both %<__int%d%> and %<long%> in "
10886 "declaration specifiers"),
10887 int_n_data[specs->int_n_idx].bitsize);
10888 else if (specs->saturating_p)
10889 error_at (loc,
10890 ("both %<_Sat%> and %<__int%d%> in "
10891 "declaration specifiers"),
10892 int_n_data[specs->int_n_idx].bitsize);
10893 else if (specs->short_p)
10894 error_at (loc,
10895 ("both %<__int%d%> and %<short%> in "
10896 "declaration specifiers"),
10897 int_n_data[specs->int_n_idx].bitsize);
10898 else if (! int_n_enabled_p[specs->int_n_idx])
10899 {
10900 specs->typespec_word = cts_int_n;
10901 error_at (loc,
10902 "%<__int%d%> is not supported on this target",
10903 int_n_data[specs->int_n_idx].bitsize);
10904 }
10905 else
10906 {
10907 specs->typespec_word = cts_int_n;
10908 specs->locations[cdw_typespec] = loc;
10909 }
10910 return specs;
10911 case RID_VOID:
10912 if (specs->long_p)
10913 error_at (loc,
10914 ("both %<long%> and %<void%> in "
10915 "declaration specifiers"));
10916 else if (specs->short_p)
10917 error_at (loc,
10918 ("both %<short%> and %<void%> in "
10919 "declaration specifiers"));
10920 else if (specs->signed_p)
10921 error_at (loc,
10922 ("both %<signed%> and %<void%> in "
10923 "declaration specifiers"));
10924 else if (specs->unsigned_p)
10925 error_at (loc,
10926 ("both %<unsigned%> and %<void%> in "
10927 "declaration specifiers"));
10928 else if (specs->complex_p)
10929 error_at (loc,
10930 ("both %<complex%> and %<void%> in "
10931 "declaration specifiers"));
10932 else if (specs->saturating_p)
10933 error_at (loc,
10934 ("both %<_Sat%> and %<void%> in "
10935 "declaration specifiers"));
10936 else
10937 {
10938 specs->typespec_word = cts_void;
10939 specs->locations[cdw_typespec] = loc;
10940 }
10941 return specs;
10942 case RID_BOOL:
10943 if (!in_system_header_at (loc))
10944 pedwarn_c90 (loc, OPT_Wpedantic,
10945 "ISO C90 does not support boolean types");
10946 if (specs->long_p)
10947 error_at (loc,
10948 ("both %<long%> and %<_Bool%> in "
10949 "declaration specifiers"));
10950 else if (specs->short_p)
10951 error_at (loc,
10952 ("both %<short%> and %<_Bool%> in "
10953 "declaration specifiers"));
10954 else if (specs->signed_p)
10955 error_at (loc,
10956 ("both %<signed%> and %<_Bool%> in "
10957 "declaration specifiers"));
10958 else if (specs->unsigned_p)
10959 error_at (loc,
10960 ("both %<unsigned%> and %<_Bool%> in "
10961 "declaration specifiers"));
10962 else if (specs->complex_p)
10963 error_at (loc,
10964 ("both %<complex%> and %<_Bool%> in "
10965 "declaration specifiers"));
10966 else if (specs->saturating_p)
10967 error_at (loc,
10968 ("both %<_Sat%> and %<_Bool%> in "
10969 "declaration specifiers"));
10970 else
10971 {
10972 specs->typespec_word = cts_bool;
10973 specs->locations[cdw_typespec] = loc;
10974 }
10975 return specs;
10976 case RID_CHAR:
10977 if (specs->long_p)
10978 error_at (loc,
10979 ("both %<long%> and %<char%> in "
10980 "declaration specifiers"));
10981 else if (specs->short_p)
10982 error_at (loc,
10983 ("both %<short%> and %<char%> in "
10984 "declaration specifiers"));
10985 else if (specs->saturating_p)
10986 error_at (loc,
10987 ("both %<_Sat%> and %<char%> in "
10988 "declaration specifiers"));
10989 else
10990 {
10991 specs->typespec_word = cts_char;
10992 specs->locations[cdw_typespec] = loc;
10993 }
10994 return specs;
10995 case RID_INT:
10996 if (specs->saturating_p)
10997 error_at (loc,
10998 ("both %<_Sat%> and %<int%> in "
10999 "declaration specifiers"));
11000 else
11001 {
11002 specs->typespec_word = cts_int;
11003 specs->locations[cdw_typespec] = loc;
11004 }
11005 return specs;
11006 case RID_FLOAT:
11007 if (specs->long_p)
11008 error_at (loc,
11009 ("both %<long%> and %<float%> in "
11010 "declaration specifiers"));
11011 else if (specs->short_p)
11012 error_at (loc,
11013 ("both %<short%> and %<float%> in "
11014 "declaration specifiers"));
11015 else if (specs->signed_p)
11016 error_at (loc,
11017 ("both %<signed%> and %<float%> in "
11018 "declaration specifiers"));
11019 else if (specs->unsigned_p)
11020 error_at (loc,
11021 ("both %<unsigned%> and %<float%> in "
11022 "declaration specifiers"));
11023 else if (specs->saturating_p)
11024 error_at (loc,
11025 ("both %<_Sat%> and %<float%> in "
11026 "declaration specifiers"));
11027 else
11028 {
11029 specs->typespec_word = cts_float;
11030 specs->locations[cdw_typespec] = loc;
11031 }
11032 return specs;
11033 case RID_DOUBLE:
11034 if (specs->long_long_p)
11035 error_at (loc,
11036 ("both %<long long%> and %<double%> in "
11037 "declaration specifiers"));
11038 else if (specs->short_p)
11039 error_at (loc,
11040 ("both %<short%> and %<double%> in "
11041 "declaration specifiers"));
11042 else if (specs->signed_p)
11043 error_at (loc,
11044 ("both %<signed%> and %<double%> in "
11045 "declaration specifiers"));
11046 else if (specs->unsigned_p)
11047 error_at (loc,
11048 ("both %<unsigned%> and %<double%> in "
11049 "declaration specifiers"));
11050 else if (specs->saturating_p)
11051 error_at (loc,
11052 ("both %<_Sat%> and %<double%> in "
11053 "declaration specifiers"));
11054 else
11055 {
11056 specs->typespec_word = cts_double;
11057 specs->locations[cdw_typespec] = loc;
11058 }
11059 return specs;
11060 CASE_RID_FLOATN_NX:
11061 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
11062 if (!in_system_header_at (input_location))
11063 pedwarn (loc, OPT_Wpedantic,
11064 "ISO C does not support the %<_Float%d%s%> type",
11065 floatn_nx_types[specs->floatn_nx_idx].n,
11066 (floatn_nx_types[specs->floatn_nx_idx].extended
11067 ? "x"
11068 : ""));
11069
11070 if (specs->long_p)
11071 error_at (loc,
11072 ("both %<long%> and %<_Float%d%s%> in "
11073 "declaration specifiers"),
11074 floatn_nx_types[specs->floatn_nx_idx].n,
11075 (floatn_nx_types[specs->floatn_nx_idx].extended
11076 ? "x"
11077 : ""));
11078 else if (specs->short_p)
11079 error_at (loc,
11080 ("both %<short%> and %<_Float%d%s%> in "
11081 "declaration specifiers"),
11082 floatn_nx_types[specs->floatn_nx_idx].n,
11083 (floatn_nx_types[specs->floatn_nx_idx].extended
11084 ? "x"
11085 : ""));
11086 else if (specs->signed_p)
11087 error_at (loc,
11088 ("both %<signed%> and %<_Float%d%s%> in "
11089 "declaration specifiers"),
11090 floatn_nx_types[specs->floatn_nx_idx].n,
11091 (floatn_nx_types[specs->floatn_nx_idx].extended
11092 ? "x"
11093 : ""));
11094 else if (specs->unsigned_p)
11095 error_at (loc,
11096 ("both %<unsigned%> and %<_Float%d%s%> in "
11097 "declaration specifiers"),
11098 floatn_nx_types[specs->floatn_nx_idx].n,
11099 (floatn_nx_types[specs->floatn_nx_idx].extended
11100 ? "x"
11101 : ""));
11102 else if (specs->saturating_p)
11103 error_at (loc,
11104 ("both %<_Sat%> and %<_Float%d%s%> in "
11105 "declaration specifiers"),
11106 floatn_nx_types[specs->floatn_nx_idx].n,
11107 (floatn_nx_types[specs->floatn_nx_idx].extended
11108 ? "x"
11109 : ""));
11110 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11111 {
11112 specs->typespec_word = cts_floatn_nx;
11113 error_at (loc,
11114 "%<_Float%d%s%> is not supported on this target",
11115 floatn_nx_types[specs->floatn_nx_idx].n,
11116 (floatn_nx_types[specs->floatn_nx_idx].extended
11117 ? "x"
11118 : ""));
11119 }
11120 else
11121 {
11122 specs->typespec_word = cts_floatn_nx;
11123 specs->locations[cdw_typespec] = loc;
11124 }
11125 return specs;
11126 case RID_DFLOAT32:
11127 case RID_DFLOAT64:
11128 case RID_DFLOAT128:
11129 {
11130 const char *str;
11131 if (i == RID_DFLOAT32)
11132 str = "_Decimal32";
11133 else if (i == RID_DFLOAT64)
11134 str = "_Decimal64";
11135 else
11136 str = "_Decimal128";
11137 if (specs->long_long_p)
11138 error_at (loc,
11139 ("both %<long long%> and %qs in "
11140 "declaration specifiers"),
11141 str);
11142 if (specs->long_p)
11143 error_at (loc,
11144 ("both %<long%> and %qs in "
11145 "declaration specifiers"),
11146 str);
11147 else if (specs->short_p)
11148 error_at (loc,
11149 ("both %<short%> and %qs in "
11150 "declaration specifiers"),
11151 str);
11152 else if (specs->signed_p)
11153 error_at (loc,
11154 ("both %<signed%> and %qs in "
11155 "declaration specifiers"),
11156 str);
11157 else if (specs->unsigned_p)
11158 error_at (loc,
11159 ("both %<unsigned%> and %qs in "
11160 "declaration specifiers"),
11161 str);
11162 else if (specs->complex_p)
11163 error_at (loc,
11164 ("both %<complex%> and %qs in "
11165 "declaration specifiers"),
11166 str);
11167 else if (specs->saturating_p)
11168 error_at (loc,
11169 ("both %<_Sat%> and %qs in "
11170 "declaration specifiers"),
11171 str);
11172 else if (i == RID_DFLOAT32)
11173 specs->typespec_word = cts_dfloat32;
11174 else if (i == RID_DFLOAT64)
11175 specs->typespec_word = cts_dfloat64;
11176 else
11177 specs->typespec_word = cts_dfloat128;
11178 specs->locations[cdw_typespec] = loc;
11179 }
11180 if (!targetm.decimal_float_supported_p ())
11181 error_at (loc,
11182 ("decimal floating-point not supported "
11183 "for this target"));
11184 pedwarn_c11 (loc, OPT_Wpedantic,
11185 "ISO C does not support decimal floating-point "
11186 "before C2X");
11187 return specs;
11188 case RID_FRACT:
11189 case RID_ACCUM:
11190 {
11191 const char *str;
11192 if (i == RID_FRACT)
11193 str = "_Fract";
11194 else
11195 str = "_Accum";
11196 if (specs->complex_p)
11197 error_at (loc,
11198 ("both %<complex%> and %qs in "
11199 "declaration specifiers"),
11200 str);
11201 else if (i == RID_FRACT)
11202 specs->typespec_word = cts_fract;
11203 else
11204 specs->typespec_word = cts_accum;
11205 specs->locations[cdw_typespec] = loc;
11206 }
11207 if (!targetm.fixed_point_supported_p ())
11208 error_at (loc,
11209 "fixed-point types not supported for this target");
11210 pedwarn (loc, OPT_Wpedantic,
11211 "ISO C does not support fixed-point types");
11212 return specs;
11213 default:
11214 /* ObjC reserved word "id", handled below. */
11215 break;
11216 }
11217 }
11218 }
11219
11220 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
11221 form of ObjC type, cases such as "int" and "long" being handled
11222 above), a TYPE (struct, union, enum and typeof specifiers) or an
11223 ERROR_MARK. In none of these cases may there have previously
11224 been any type specifiers. */
11225 if (specs->type || specs->typespec_word != cts_none
11226 || specs->long_p || specs->short_p || specs->signed_p
11227 || specs->unsigned_p || specs->complex_p)
11228 error_at (loc, "two or more data types in declaration specifiers");
11229 else if (TREE_CODE (type) == TYPE_DECL)
11230 {
11231 if (TREE_TYPE (type) == error_mark_node)
11232 ; /* Allow the type to default to int to avoid cascading errors. */
11233 else
11234 {
11235 specs->type = TREE_TYPE (type);
11236 specs->decl_attr = DECL_ATTRIBUTES (type);
11237 specs->typedef_p = true;
11238 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
11239 specs->locations[cdw_typedef] = loc;
11240
11241 /* If this typedef name is defined in a struct, then a C++
11242 lookup would return a different value. */
11243 if (warn_cxx_compat
11244 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
11245 warning_at (loc, OPT_Wc___compat,
11246 "C++ lookup of %qD would return a field, not a type",
11247 type);
11248
11249 /* If we are parsing a struct, record that a struct field
11250 used a typedef. */
11251 if (warn_cxx_compat && struct_parse_info != NULL)
11252 struct_parse_info->typedefs_seen.safe_push (type);
11253 }
11254 }
11255 else if (TREE_CODE (type) == IDENTIFIER_NODE)
11256 {
11257 tree t = lookup_name (type);
11258 if (!t || TREE_CODE (t) != TYPE_DECL)
11259 error_at (loc, "%qE fails to be a typedef or built in type", type);
11260 else if (TREE_TYPE (t) == error_mark_node)
11261 ;
11262 else
11263 {
11264 specs->type = TREE_TYPE (t);
11265 specs->locations[cdw_typespec] = loc;
11266 }
11267 }
11268 else
11269 {
11270 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
11271 {
11272 specs->typedef_p = true;
11273 specs->locations[cdw_typedef] = loc;
11274 if (spec.expr)
11275 {
11276 if (specs->expr)
11277 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
11278 specs->expr, spec.expr);
11279 else
11280 specs->expr = spec.expr;
11281 specs->expr_const_operands &= spec.expr_const_operands;
11282 }
11283 }
11284 specs->type = type;
11285 }
11286
11287 return specs;
11288 }
11289
11290 /* Add the storage class specifier or function specifier SCSPEC to the
11291 declaration specifiers SPECS, returning SPECS. */
11292
11293 struct c_declspecs *
11294 declspecs_add_scspec (location_t loc,
11295 struct c_declspecs *specs,
11296 tree scspec)
11297 {
11298 enum rid i;
11299 enum c_storage_class n = csc_none;
11300 bool dupe = false;
11301 specs->declspecs_seen_p = true;
11302 specs->non_std_attrs_seen_p = true;
11303 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
11304 && C_IS_RESERVED_WORD (scspec));
11305 i = C_RID_CODE (scspec);
11306 if (specs->non_sc_seen_p)
11307 warning (OPT_Wold_style_declaration,
11308 "%qE is not at beginning of declaration", scspec);
11309 switch (i)
11310 {
11311 case RID_INLINE:
11312 /* C99 permits duplicate inline. Although of doubtful utility,
11313 it seems simplest to permit it in gnu89 mode as well, as
11314 there is also little utility in maintaining this as a
11315 difference between gnu89 and C99 inline. */
11316 dupe = false;
11317 specs->inline_p = true;
11318 specs->locations[cdw_inline] = loc;
11319 break;
11320 case RID_NORETURN:
11321 /* Duplicate _Noreturn is permitted. */
11322 dupe = false;
11323 specs->noreturn_p = true;
11324 specs->locations[cdw_noreturn] = loc;
11325 break;
11326 case RID_THREAD:
11327 dupe = specs->thread_p;
11328 if (specs->storage_class == csc_auto)
11329 error ("%qE used with %<auto%>", scspec);
11330 else if (specs->storage_class == csc_register)
11331 error ("%qE used with %<register%>", scspec);
11332 else if (specs->storage_class == csc_typedef)
11333 error ("%qE used with %<typedef%>", scspec);
11334 else
11335 {
11336 specs->thread_p = true;
11337 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
11338 "__thread") == 0);
11339 /* A diagnostic is not required for the use of this
11340 identifier in the implementation namespace; only diagnose
11341 it for the C11 spelling because of existing code using
11342 the other spelling. */
11343 if (!specs->thread_gnu_p)
11344 {
11345 if (flag_isoc99)
11346 pedwarn_c99 (loc, OPT_Wpedantic,
11347 "ISO C99 does not support %qE", scspec);
11348 else
11349 pedwarn_c99 (loc, OPT_Wpedantic,
11350 "ISO C90 does not support %qE", scspec);
11351 }
11352 specs->locations[cdw_thread] = loc;
11353 }
11354 break;
11355 case RID_AUTO:
11356 n = csc_auto;
11357 break;
11358 case RID_EXTERN:
11359 n = csc_extern;
11360 /* Diagnose "__thread extern". */
11361 if (specs->thread_p && specs->thread_gnu_p)
11362 error ("%<__thread%> before %<extern%>");
11363 break;
11364 case RID_REGISTER:
11365 n = csc_register;
11366 break;
11367 case RID_STATIC:
11368 n = csc_static;
11369 /* Diagnose "__thread static". */
11370 if (specs->thread_p && specs->thread_gnu_p)
11371 error ("%<__thread%> before %<static%>");
11372 break;
11373 case RID_TYPEDEF:
11374 n = csc_typedef;
11375 break;
11376 default:
11377 gcc_unreachable ();
11378 }
11379 if (n != csc_none && n == specs->storage_class)
11380 dupe = true;
11381 if (dupe)
11382 {
11383 if (i == RID_THREAD)
11384 error ("duplicate %<_Thread_local%> or %<__thread%>");
11385 else
11386 error ("duplicate %qE", scspec);
11387 }
11388 if (n != csc_none)
11389 {
11390 if (specs->storage_class != csc_none && n != specs->storage_class)
11391 {
11392 error ("multiple storage classes in declaration specifiers");
11393 }
11394 else
11395 {
11396 specs->storage_class = n;
11397 specs->locations[cdw_storage_class] = loc;
11398 if (n != csc_extern && n != csc_static && specs->thread_p)
11399 {
11400 error ("%qs used with %qE",
11401 specs->thread_gnu_p ? "__thread" : "_Thread_local",
11402 scspec);
11403 specs->thread_p = false;
11404 }
11405 }
11406 }
11407 return specs;
11408 }
11409
11410 /* Add the attributes ATTRS to the declaration specifiers SPECS,
11411 returning SPECS. */
11412
11413 struct c_declspecs *
11414 declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
11415 {
11416 specs->attrs = chainon (attrs, specs->attrs);
11417 specs->locations[cdw_attributes] = loc;
11418 specs->declspecs_seen_p = true;
11419 /* In the case of standard attributes at the start of the
11420 declaration, the caller will reset this. */
11421 specs->non_std_attrs_seen_p = true;
11422 return specs;
11423 }
11424
11425 /* Add an _Alignas specifier (expression ALIGN, or type whose
11426 alignment is ALIGN) to the declaration specifiers SPECS, returning
11427 SPECS. */
11428 struct c_declspecs *
11429 declspecs_add_alignas (location_t loc,
11430 struct c_declspecs *specs, tree align)
11431 {
11432 specs->alignas_p = true;
11433 specs->locations[cdw_alignas] = loc;
11434 if (align == error_mark_node)
11435 return specs;
11436
11437 /* Only accept the alignment if it's valid and greater than
11438 the current one. Zero is invalid but by C11 required to
11439 be silently ignored. */
11440 int align_log = check_user_alignment (align, false, /* warn_zero = */false);
11441 if (align_log > specs->align_log)
11442 specs->align_log = align_log;
11443 return specs;
11444 }
11445
11446 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
11447 specifiers with any other type specifier to determine the resulting
11448 type. This is where ISO C checks on complex types are made, since
11449 "_Complex long" is a prefix of the valid ISO C type "_Complex long
11450 double". Also apply postfix standard attributes to modify the type. */
11451
11452 struct c_declspecs *
11453 finish_declspecs (struct c_declspecs *specs)
11454 {
11455 /* If a type was specified as a whole, we have no modifiers and are
11456 done. */
11457 if (specs->type != NULL_TREE)
11458 {
11459 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11460 && !specs->signed_p && !specs->unsigned_p
11461 && !specs->complex_p);
11462
11463 /* Set a dummy type. */
11464 if (TREE_CODE (specs->type) == ERROR_MARK)
11465 specs->type = integer_type_node;
11466 goto handle_postfix_attrs;
11467 }
11468
11469 /* If none of "void", "_Bool", "char", "int", "float" or "double"
11470 has been specified, treat it as "int" unless "_Complex" is
11471 present and there are no other specifiers. If we just have
11472 "_Complex", it is equivalent to "_Complex double", but e.g.
11473 "_Complex short" is equivalent to "_Complex short int". */
11474 if (specs->typespec_word == cts_none)
11475 {
11476 if (specs->saturating_p)
11477 {
11478 error_at (specs->locations[cdw_saturating],
11479 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11480 if (!targetm.fixed_point_supported_p ())
11481 error_at (specs->locations[cdw_saturating],
11482 "fixed-point types not supported for this target");
11483 specs->typespec_word = cts_fract;
11484 }
11485 else if (specs->long_p || specs->short_p
11486 || specs->signed_p || specs->unsigned_p)
11487 {
11488 specs->typespec_word = cts_int;
11489 }
11490 else if (specs->complex_p)
11491 {
11492 specs->typespec_word = cts_double;
11493 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11494 "ISO C does not support plain %<complex%> meaning "
11495 "%<double complex%>");
11496 }
11497 else
11498 {
11499 specs->typespec_word = cts_int;
11500 specs->default_int_p = true;
11501 /* We don't diagnose this here because grokdeclarator will
11502 give more specific diagnostics according to whether it is
11503 a function definition. */
11504 }
11505 }
11506
11507 /* If "signed" was specified, record this to distinguish "int" and
11508 "signed int" in the case of a bit-field with
11509 -funsigned-bitfields. */
11510 specs->explicit_signed_p = specs->signed_p;
11511
11512 /* Now compute the actual type. */
11513 switch (specs->typespec_word)
11514 {
11515 case cts_auto_type:
11516 gcc_assert (!specs->long_p && !specs->short_p
11517 && !specs->signed_p && !specs->unsigned_p
11518 && !specs->complex_p);
11519 /* Type to be filled in later. */
11520 if (specs->postfix_attrs)
11521 error ("%<__auto_type%> followed by %<[[]]%> attributes");
11522 break;
11523 case cts_void:
11524 gcc_assert (!specs->long_p && !specs->short_p
11525 && !specs->signed_p && !specs->unsigned_p
11526 && !specs->complex_p);
11527 specs->type = void_type_node;
11528 break;
11529 case cts_bool:
11530 gcc_assert (!specs->long_p && !specs->short_p
11531 && !specs->signed_p && !specs->unsigned_p
11532 && !specs->complex_p);
11533 specs->type = boolean_type_node;
11534 break;
11535 case cts_char:
11536 gcc_assert (!specs->long_p && !specs->short_p);
11537 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11538 if (specs->signed_p)
11539 specs->type = signed_char_type_node;
11540 else if (specs->unsigned_p)
11541 specs->type = unsigned_char_type_node;
11542 else
11543 specs->type = char_type_node;
11544 if (specs->complex_p)
11545 {
11546 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11547 "ISO C does not support complex integer types");
11548 specs->type = build_complex_type (specs->type);
11549 }
11550 break;
11551 case cts_int_n:
11552 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
11553 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11554 if (! int_n_enabled_p[specs->int_n_idx])
11555 specs->type = integer_type_node;
11556 else
11557 specs->type = (specs->unsigned_p
11558 ? int_n_trees[specs->int_n_idx].unsigned_type
11559 : int_n_trees[specs->int_n_idx].signed_type);
11560 if (specs->complex_p)
11561 {
11562 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11563 "ISO C does not support complex integer types");
11564 specs->type = build_complex_type (specs->type);
11565 }
11566 break;
11567 case cts_int:
11568 gcc_assert (!(specs->long_p && specs->short_p));
11569 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11570 if (specs->long_long_p)
11571 specs->type = (specs->unsigned_p
11572 ? long_long_unsigned_type_node
11573 : long_long_integer_type_node);
11574 else if (specs->long_p)
11575 specs->type = (specs->unsigned_p
11576 ? long_unsigned_type_node
11577 : long_integer_type_node);
11578 else if (specs->short_p)
11579 specs->type = (specs->unsigned_p
11580 ? short_unsigned_type_node
11581 : short_integer_type_node);
11582 else
11583 specs->type = (specs->unsigned_p
11584 ? unsigned_type_node
11585 : integer_type_node);
11586 if (specs->complex_p)
11587 {
11588 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11589 "ISO C does not support complex integer types");
11590 specs->type = build_complex_type (specs->type);
11591 }
11592 break;
11593 case cts_float:
11594 gcc_assert (!specs->long_p && !specs->short_p
11595 && !specs->signed_p && !specs->unsigned_p);
11596 specs->type = (specs->complex_p
11597 ? complex_float_type_node
11598 : float_type_node);
11599 break;
11600 case cts_double:
11601 gcc_assert (!specs->long_long_p && !specs->short_p
11602 && !specs->signed_p && !specs->unsigned_p);
11603 if (specs->long_p)
11604 {
11605 specs->type = (specs->complex_p
11606 ? complex_long_double_type_node
11607 : long_double_type_node);
11608 }
11609 else
11610 {
11611 specs->type = (specs->complex_p
11612 ? complex_double_type_node
11613 : double_type_node);
11614 }
11615 break;
11616 case cts_floatn_nx:
11617 gcc_assert (!specs->long_p && !specs->short_p
11618 && !specs->signed_p && !specs->unsigned_p);
11619 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11620 specs->type = integer_type_node;
11621 else if (specs->complex_p)
11622 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11623 else
11624 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11625 break;
11626 case cts_dfloat32:
11627 case cts_dfloat64:
11628 case cts_dfloat128:
11629 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11630 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11631 if (!targetm.decimal_float_supported_p ())
11632 specs->type = integer_type_node;
11633 else if (specs->typespec_word == cts_dfloat32)
11634 specs->type = dfloat32_type_node;
11635 else if (specs->typespec_word == cts_dfloat64)
11636 specs->type = dfloat64_type_node;
11637 else
11638 specs->type = dfloat128_type_node;
11639 break;
11640 case cts_fract:
11641 gcc_assert (!specs->complex_p);
11642 if (!targetm.fixed_point_supported_p ())
11643 specs->type = integer_type_node;
11644 else if (specs->saturating_p)
11645 {
11646 if (specs->long_long_p)
11647 specs->type = specs->unsigned_p
11648 ? sat_unsigned_long_long_fract_type_node
11649 : sat_long_long_fract_type_node;
11650 else if (specs->long_p)
11651 specs->type = specs->unsigned_p
11652 ? sat_unsigned_long_fract_type_node
11653 : sat_long_fract_type_node;
11654 else if (specs->short_p)
11655 specs->type = specs->unsigned_p
11656 ? sat_unsigned_short_fract_type_node
11657 : sat_short_fract_type_node;
11658 else
11659 specs->type = specs->unsigned_p
11660 ? sat_unsigned_fract_type_node
11661 : sat_fract_type_node;
11662 }
11663 else
11664 {
11665 if (specs->long_long_p)
11666 specs->type = specs->unsigned_p
11667 ? unsigned_long_long_fract_type_node
11668 : long_long_fract_type_node;
11669 else if (specs->long_p)
11670 specs->type = specs->unsigned_p
11671 ? unsigned_long_fract_type_node
11672 : long_fract_type_node;
11673 else if (specs->short_p)
11674 specs->type = specs->unsigned_p
11675 ? unsigned_short_fract_type_node
11676 : short_fract_type_node;
11677 else
11678 specs->type = specs->unsigned_p
11679 ? unsigned_fract_type_node
11680 : fract_type_node;
11681 }
11682 break;
11683 case cts_accum:
11684 gcc_assert (!specs->complex_p);
11685 if (!targetm.fixed_point_supported_p ())
11686 specs->type = integer_type_node;
11687 else if (specs->saturating_p)
11688 {
11689 if (specs->long_long_p)
11690 specs->type = specs->unsigned_p
11691 ? sat_unsigned_long_long_accum_type_node
11692 : sat_long_long_accum_type_node;
11693 else if (specs->long_p)
11694 specs->type = specs->unsigned_p
11695 ? sat_unsigned_long_accum_type_node
11696 : sat_long_accum_type_node;
11697 else if (specs->short_p)
11698 specs->type = specs->unsigned_p
11699 ? sat_unsigned_short_accum_type_node
11700 : sat_short_accum_type_node;
11701 else
11702 specs->type = specs->unsigned_p
11703 ? sat_unsigned_accum_type_node
11704 : sat_accum_type_node;
11705 }
11706 else
11707 {
11708 if (specs->long_long_p)
11709 specs->type = specs->unsigned_p
11710 ? unsigned_long_long_accum_type_node
11711 : long_long_accum_type_node;
11712 else if (specs->long_p)
11713 specs->type = specs->unsigned_p
11714 ? unsigned_long_accum_type_node
11715 : long_accum_type_node;
11716 else if (specs->short_p)
11717 specs->type = specs->unsigned_p
11718 ? unsigned_short_accum_type_node
11719 : short_accum_type_node;
11720 else
11721 specs->type = specs->unsigned_p
11722 ? unsigned_accum_type_node
11723 : accum_type_node;
11724 }
11725 break;
11726 default:
11727 gcc_unreachable ();
11728 }
11729 handle_postfix_attrs:
11730 if (specs->type != NULL)
11731 {
11732 specs->postfix_attrs = c_warn_type_attributes (specs->postfix_attrs);
11733 decl_attributes (&specs->type, specs->postfix_attrs, 0);
11734 specs->postfix_attrs = NULL_TREE;
11735 }
11736
11737 return specs;
11738 }
11739
11740 /* Perform final processing on one file scope's declarations (or the
11741 external scope's declarations), GLOBALS. */
11742
11743 static void
11744 c_write_global_declarations_1 (tree globals)
11745 {
11746 tree decl;
11747 bool reconsider;
11748
11749 /* Process the decls in the order they were written. */
11750 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11751 {
11752 /* Check for used but undefined static functions using the C
11753 standard's definition of "used", and set TREE_NO_WARNING so
11754 that check_global_declaration doesn't repeat the check. */
11755 if (TREE_CODE (decl) == FUNCTION_DECL
11756 && DECL_INITIAL (decl) == NULL_TREE
11757 && DECL_EXTERNAL (decl)
11758 && !TREE_PUBLIC (decl))
11759 {
11760 if (C_DECL_USED (decl))
11761 {
11762 if (pedwarn (input_location, 0, "%q+F used but never defined",
11763 decl))
11764 TREE_NO_WARNING (decl) = 1;
11765 }
11766 /* For -Wunused-function warn about unused static prototypes. */
11767 else if (warn_unused_function
11768 && ! DECL_ARTIFICIAL (decl)
11769 && ! TREE_NO_WARNING (decl))
11770 {
11771 if (warning (OPT_Wunused_function,
11772 "%q+F declared %<static%> but never defined",
11773 decl))
11774 TREE_NO_WARNING (decl) = 1;
11775 }
11776 }
11777
11778 wrapup_global_declaration_1 (decl);
11779 }
11780
11781 do
11782 {
11783 reconsider = false;
11784 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11785 reconsider |= wrapup_global_declaration_2 (decl);
11786 }
11787 while (reconsider);
11788 }
11789
11790 /* Preserve the external declarations scope across a garbage collect. */
11791 static GTY(()) tree ext_block;
11792
11793 /* Collect all references relevant to SOURCE_FILE. */
11794
11795 static void
11796 collect_all_refs (const char *source_file)
11797 {
11798 tree t;
11799 unsigned i;
11800
11801 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11802 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11803
11804 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11805 }
11806
11807 /* Collect source file references at global level. */
11808
11809 static void
11810 collect_source_refs (void)
11811 {
11812 tree t;
11813 tree decls;
11814 tree decl;
11815 unsigned i;
11816
11817 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11818 {
11819 decls = DECL_INITIAL (t);
11820 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11821 if (!DECL_IS_BUILTIN (decl))
11822 collect_source_ref (DECL_SOURCE_FILE (decl));
11823 }
11824
11825 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11826 if (!DECL_IS_BUILTIN (decl))
11827 collect_source_ref (DECL_SOURCE_FILE (decl));
11828 }
11829
11830 /* Perform any final parser cleanups and generate initial debugging
11831 information. */
11832
11833 void
11834 c_parse_final_cleanups (void)
11835 {
11836 tree t;
11837 unsigned i;
11838
11839 /* We don't want to do this if generating a PCH. */
11840 if (pch_file)
11841 return;
11842
11843 timevar_stop (TV_PHASE_PARSING);
11844 timevar_start (TV_PHASE_DEFERRED);
11845
11846 /* Do the Objective-C stuff. This is where all the Objective-C
11847 module stuff gets generated (symtab, class/protocol/selector
11848 lists etc). */
11849 if (c_dialect_objc ())
11850 objc_write_global_declarations ();
11851
11852 /* Close the external scope. */
11853 ext_block = pop_scope ();
11854 external_scope = 0;
11855 gcc_assert (!current_scope);
11856
11857 /* Handle -fdump-ada-spec[-slim]. */
11858 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11859 {
11860 /* Build a table of files to generate specs for */
11861 collect_source_ref (main_input_filename);
11862 if (!flag_dump_ada_spec_slim)
11863 collect_source_refs ();
11864
11865 dump_ada_specs (collect_all_refs, NULL);
11866 }
11867
11868 /* Process all file scopes in this compilation, and the external_scope,
11869 through wrapup_global_declarations. */
11870 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11871 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11872 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11873
11874 timevar_stop (TV_PHASE_DEFERRED);
11875 timevar_start (TV_PHASE_PARSING);
11876
11877 ext_block = NULL;
11878 }
11879
11880 /* Register reserved keyword WORD as qualifier for address space AS. */
11881
11882 void
11883 c_register_addr_space (const char *word, addr_space_t as)
11884 {
11885 int rid = RID_FIRST_ADDR_SPACE + as;
11886 tree id;
11887
11888 /* Address space qualifiers are only supported
11889 in C with GNU extensions enabled. */
11890 if (c_dialect_objc () || flag_no_asm)
11891 return;
11892
11893 id = get_identifier (word);
11894 C_SET_RID_CODE (id, rid);
11895 C_IS_RESERVED_WORD (id) = 1;
11896 ridpointers [rid] = id;
11897 }
11898
11899 /* Return identifier to look up for omp declare reduction. */
11900
11901 tree
11902 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11903 {
11904 const char *p = NULL;
11905 switch (reduction_code)
11906 {
11907 case PLUS_EXPR: p = "+"; break;
11908 case MULT_EXPR: p = "*"; break;
11909 case MINUS_EXPR: p = "-"; break;
11910 case BIT_AND_EXPR: p = "&"; break;
11911 case BIT_XOR_EXPR: p = "^"; break;
11912 case BIT_IOR_EXPR: p = "|"; break;
11913 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11914 case TRUTH_ORIF_EXPR: p = "||"; break;
11915 case MIN_EXPR: p = "min"; break;
11916 case MAX_EXPR: p = "max"; break;
11917 default:
11918 break;
11919 }
11920
11921 if (p == NULL)
11922 {
11923 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11924 return error_mark_node;
11925 p = IDENTIFIER_POINTER (reduction_id);
11926 }
11927
11928 const char prefix[] = "omp declare reduction ";
11929 size_t lenp = sizeof (prefix);
11930 size_t len = strlen (p);
11931 char *name = XALLOCAVEC (char, lenp + len);
11932 memcpy (name, prefix, lenp - 1);
11933 memcpy (name + lenp - 1, p, len + 1);
11934 return get_identifier (name);
11935 }
11936
11937 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11938 VAR_DECL, bind it into the current scope and return it. */
11939
11940 tree
11941 c_omp_reduction_decl (tree reduction_id)
11942 {
11943 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11944 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11945 return b->decl;
11946
11947 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11948 reduction_id, integer_type_node);
11949 DECL_ARTIFICIAL (decl) = 1;
11950 DECL_EXTERNAL (decl) = 1;
11951 TREE_STATIC (decl) = 1;
11952 TREE_PUBLIC (decl) = 0;
11953 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11954 return decl;
11955 }
11956
11957 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11958
11959 tree
11960 c_omp_reduction_lookup (tree reduction_id, tree type)
11961 {
11962 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11963 while (b)
11964 {
11965 tree t;
11966 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11967 if (comptypes (TREE_PURPOSE (t), type))
11968 return TREE_VALUE (t);
11969 b = b->shadowed;
11970 }
11971 return error_mark_node;
11972 }
11973
11974 /* Helper function called via walk_tree, to diagnose invalid
11975 #pragma omp declare reduction combiners or initializers. */
11976
11977 tree
11978 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11979 {
11980 tree *vars = (tree *) data;
11981 if (SSA_VAR_P (*tp)
11982 && !DECL_ARTIFICIAL (*tp)
11983 && *tp != vars[0]
11984 && *tp != vars[1])
11985 {
11986 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11987 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11988 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11989 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11990 *tp);
11991 else
11992 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11993 "to variable %qD which is not %<omp_priv%> nor "
11994 "%<omp_orig%>",
11995 *tp);
11996 return *tp;
11997 }
11998 return NULL_TREE;
11999 }
12000
12001 #include "gt-c-c-decl.h"