runtime: mark go-context.S as no-executable-stack and split-stack supported
[gcc.git] / gcc / c / c-decl.c
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2019 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) || !DECL_EXTERNAL (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 /* Create the predefined scalar types of C,
4340 and some nodes representing standard constants (0, 1, (void *) 0).
4341 Initialize the global scope.
4342 Make definitions for built-in primitive functions. */
4343
4344 void
4345 c_init_decl_processing (void)
4346 {
4347 location_t save_loc = input_location;
4348
4349 /* Initialize reserved words for parser. */
4350 c_parse_init ();
4351
4352 current_function_decl = NULL_TREE;
4353
4354 gcc_obstack_init (&parser_obstack);
4355
4356 /* Make the externals scope. */
4357 push_scope ();
4358 external_scope = current_scope;
4359
4360 /* Declarations from c_common_nodes_and_builtins must not be associated
4361 with this input file, lest we get differences between using and not
4362 using preprocessed headers. */
4363 input_location = BUILTINS_LOCATION;
4364
4365 c_common_nodes_and_builtins ();
4366
4367 /* In C, comparisons and TRUTH_* expressions have type int. */
4368 truthvalue_type_node = integer_type_node;
4369 truthvalue_true_node = integer_one_node;
4370 truthvalue_false_node = integer_zero_node;
4371
4372 /* Even in C99, which has a real boolean type. */
4373 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4374 boolean_type_node));
4375
4376 input_location = save_loc;
4377
4378 make_fname_decl = c_make_fname_decl;
4379 start_fname_decls ();
4380 }
4381
4382 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4383 give the decl, NAME is the initialization string and TYPE_DEP
4384 indicates whether NAME depended on the type of the function. As we
4385 don't yet implement delayed emission of static data, we mark the
4386 decl as emitted so it is not placed in the output. Anything using
4387 it must therefore pull out the STRING_CST initializer directly.
4388 FIXME. */
4389
4390 static tree
4391 c_make_fname_decl (location_t loc, tree id, int type_dep)
4392 {
4393 const char *name = fname_as_string (type_dep);
4394 tree decl, type, init;
4395 size_t length = strlen (name);
4396
4397 type = build_array_type (char_type_node,
4398 build_index_type (size_int (length)));
4399 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4400
4401 decl = build_decl (loc, VAR_DECL, id, type);
4402
4403 TREE_STATIC (decl) = 1;
4404 TREE_READONLY (decl) = 1;
4405 DECL_ARTIFICIAL (decl) = 1;
4406
4407 init = build_string (length + 1, name);
4408 free (CONST_CAST (char *, name));
4409 TREE_TYPE (init) = type;
4410 DECL_INITIAL (decl) = init;
4411
4412 TREE_USED (decl) = 1;
4413
4414 if (current_function_decl
4415 /* For invalid programs like this:
4416
4417 void foo()
4418 const char* p = __FUNCTION__;
4419
4420 the __FUNCTION__ is believed to appear in K&R style function
4421 parameter declarator. In that case we still don't have
4422 function_scope. */
4423 && current_function_scope)
4424 {
4425 DECL_CONTEXT (decl) = current_function_decl;
4426 bind (id, decl, current_function_scope,
4427 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4428 }
4429
4430 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4431
4432 return decl;
4433 }
4434
4435 tree
4436 c_builtin_function (tree decl)
4437 {
4438 tree type = TREE_TYPE (decl);
4439 tree id = DECL_NAME (decl);
4440
4441 const char *name = IDENTIFIER_POINTER (id);
4442 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4443
4444 /* Should never be called on a symbol with a preexisting meaning. */
4445 gcc_assert (!I_SYMBOL_BINDING (id));
4446
4447 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4448 UNKNOWN_LOCATION);
4449
4450 /* Builtins in the implementation namespace are made visible without
4451 needing to be explicitly declared. See push_file_scope. */
4452 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4453 {
4454 DECL_CHAIN (decl) = visible_builtins;
4455 visible_builtins = decl;
4456 }
4457
4458 return decl;
4459 }
4460
4461 tree
4462 c_builtin_function_ext_scope (tree decl)
4463 {
4464 tree type = TREE_TYPE (decl);
4465 tree id = DECL_NAME (decl);
4466
4467 const char *name = IDENTIFIER_POINTER (id);
4468 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4469
4470 if (external_scope)
4471 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4472 UNKNOWN_LOCATION);
4473
4474 /* Builtins in the implementation namespace are made visible without
4475 needing to be explicitly declared. See push_file_scope. */
4476 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4477 {
4478 DECL_CHAIN (decl) = visible_builtins;
4479 visible_builtins = decl;
4480 }
4481
4482 return decl;
4483 }
4484 \f
4485 /* Called when a declaration is seen that contains no names to declare.
4486 If its type is a reference to a structure, union or enum inherited
4487 from a containing scope, shadow that tag name for the current scope
4488 with a forward reference.
4489 If its type defines a new named structure or union
4490 or defines an enum, it is valid but we need not do anything here.
4491 Otherwise, it is an error. */
4492
4493 void
4494 shadow_tag (const struct c_declspecs *declspecs)
4495 {
4496 shadow_tag_warned (declspecs, 0);
4497 }
4498
4499 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4500 but no pedwarn. */
4501 void
4502 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4503 {
4504 bool found_tag = false;
4505
4506 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4507 {
4508 tree value = declspecs->type;
4509 enum tree_code code = TREE_CODE (value);
4510
4511 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4512 /* Used to test also that TYPE_SIZE (value) != 0.
4513 That caused warning for `struct foo;' at top level in the file. */
4514 {
4515 tree name = TYPE_NAME (value);
4516 tree t;
4517
4518 found_tag = true;
4519
4520 if (declspecs->restrict_p)
4521 {
4522 error ("invalid use of %<restrict%>");
4523 warned = 1;
4524 }
4525
4526 if (name == NULL_TREE)
4527 {
4528 if (warned != 1 && code != ENUMERAL_TYPE)
4529 /* Empty unnamed enum OK */
4530 {
4531 pedwarn (input_location, 0,
4532 "unnamed struct/union that defines no instances");
4533 warned = 1;
4534 }
4535 }
4536 else if (declspecs->typespec_kind != ctsk_tagdef
4537 && declspecs->typespec_kind != ctsk_tagfirstref
4538 && declspecs->storage_class != csc_none)
4539 {
4540 if (warned != 1)
4541 pedwarn (input_location, 0,
4542 "empty declaration with storage class specifier "
4543 "does not redeclare tag");
4544 warned = 1;
4545 pending_xref_error ();
4546 }
4547 else if (declspecs->typespec_kind != ctsk_tagdef
4548 && declspecs->typespec_kind != ctsk_tagfirstref
4549 && (declspecs->const_p
4550 || declspecs->volatile_p
4551 || declspecs->atomic_p
4552 || declspecs->restrict_p
4553 || declspecs->address_space))
4554 {
4555 if (warned != 1)
4556 pedwarn (input_location, 0,
4557 "empty declaration with type qualifier "
4558 "does not redeclare tag");
4559 warned = 1;
4560 pending_xref_error ();
4561 }
4562 else if (declspecs->typespec_kind != ctsk_tagdef
4563 && declspecs->typespec_kind != ctsk_tagfirstref
4564 && declspecs->alignas_p)
4565 {
4566 if (warned != 1)
4567 pedwarn (input_location, 0,
4568 "empty declaration with %<_Alignas%> "
4569 "does not redeclare tag");
4570 warned = 1;
4571 pending_xref_error ();
4572 }
4573 else
4574 {
4575 pending_invalid_xref = NULL_TREE;
4576 t = lookup_tag (code, name, true, NULL);
4577
4578 if (t == NULL_TREE)
4579 {
4580 t = make_node (code);
4581 pushtag (input_location, name, t);
4582 }
4583 }
4584 }
4585 else
4586 {
4587 if (warned != 1 && !in_system_header_at (input_location))
4588 {
4589 pedwarn (input_location, 0,
4590 "useless type name in empty declaration");
4591 warned = 1;
4592 }
4593 }
4594 }
4595 else if (warned != 1 && !in_system_header_at (input_location)
4596 && declspecs->typedef_p)
4597 {
4598 pedwarn (input_location, 0, "useless type name in empty declaration");
4599 warned = 1;
4600 }
4601
4602 pending_invalid_xref = NULL_TREE;
4603
4604 if (declspecs->inline_p)
4605 {
4606 error ("%<inline%> in empty declaration");
4607 warned = 1;
4608 }
4609
4610 if (declspecs->noreturn_p)
4611 {
4612 error ("%<_Noreturn%> in empty declaration");
4613 warned = 1;
4614 }
4615
4616 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4617 {
4618 error ("%<auto%> in file-scope empty declaration");
4619 warned = 1;
4620 }
4621
4622 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4623 {
4624 error ("%<register%> in file-scope empty declaration");
4625 warned = 1;
4626 }
4627
4628 if (!warned && !in_system_header_at (input_location)
4629 && declspecs->storage_class != csc_none)
4630 {
4631 warning (0, "useless storage class specifier in empty declaration");
4632 warned = 2;
4633 }
4634
4635 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4636 {
4637 warning (0, "useless %qs in empty declaration",
4638 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4639 warned = 2;
4640 }
4641
4642 if (!warned
4643 && !in_system_header_at (input_location)
4644 && (declspecs->const_p
4645 || declspecs->volatile_p
4646 || declspecs->atomic_p
4647 || declspecs->restrict_p
4648 || declspecs->address_space))
4649 {
4650 warning (0, "useless type qualifier in empty declaration");
4651 warned = 2;
4652 }
4653
4654 if (!warned && !in_system_header_at (input_location)
4655 && declspecs->alignas_p)
4656 {
4657 warning (0, "useless %<_Alignas%> in empty declaration");
4658 warned = 2;
4659 }
4660
4661 if (warned != 1)
4662 {
4663 if (!found_tag)
4664 pedwarn (input_location, 0, "empty declaration");
4665 }
4666 }
4667 \f
4668
4669 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4670 bits. SPECS represents declaration specifiers that the grammar
4671 only permits to contain type qualifiers and attributes. */
4672
4673 int
4674 quals_from_declspecs (const struct c_declspecs *specs)
4675 {
4676 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4677 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4678 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4679 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4680 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4681 gcc_assert (!specs->type
4682 && !specs->decl_attr
4683 && specs->typespec_word == cts_none
4684 && specs->storage_class == csc_none
4685 && !specs->typedef_p
4686 && !specs->explicit_signed_p
4687 && !specs->deprecated_p
4688 && !specs->long_p
4689 && !specs->long_long_p
4690 && !specs->short_p
4691 && !specs->signed_p
4692 && !specs->unsigned_p
4693 && !specs->complex_p
4694 && !specs->inline_p
4695 && !specs->noreturn_p
4696 && !specs->thread_p);
4697 return quals;
4698 }
4699
4700 /* Construct an array declarator. LOC is the location of the
4701 beginning of the array (usually the opening brace). EXPR is the
4702 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4703 inside the [] (to be applied to the pointer to which a parameter
4704 array is converted). STATIC_P is true if "static" is inside the
4705 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4706 VLA of unspecified length which is nevertheless a complete type,
4707 false otherwise. The field for the contained declarator is left to
4708 be filled in by set_array_declarator_inner. */
4709
4710 struct c_declarator *
4711 build_array_declarator (location_t loc,
4712 tree expr, struct c_declspecs *quals, bool static_p,
4713 bool vla_unspec_p)
4714 {
4715 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4716 struct c_declarator);
4717 declarator->id_loc = loc;
4718 declarator->kind = cdk_array;
4719 declarator->declarator = 0;
4720 declarator->u.array.dimen = expr;
4721 if (quals)
4722 {
4723 declarator->u.array.attrs = quals->attrs;
4724 declarator->u.array.quals = quals_from_declspecs (quals);
4725 }
4726 else
4727 {
4728 declarator->u.array.attrs = NULL_TREE;
4729 declarator->u.array.quals = 0;
4730 }
4731 declarator->u.array.static_p = static_p;
4732 declarator->u.array.vla_unspec_p = vla_unspec_p;
4733 if (static_p || quals != NULL)
4734 pedwarn_c90 (loc, OPT_Wpedantic,
4735 "ISO C90 does not support %<static%> or type "
4736 "qualifiers in parameter array declarators");
4737 if (vla_unspec_p)
4738 pedwarn_c90 (loc, OPT_Wpedantic,
4739 "ISO C90 does not support %<[*]%> array declarators");
4740 if (vla_unspec_p)
4741 {
4742 if (!current_scope->parm_flag)
4743 {
4744 /* C99 6.7.5.2p4 */
4745 error_at (loc, "%<[*]%> not allowed in other than "
4746 "function prototype scope");
4747 declarator->u.array.vla_unspec_p = false;
4748 return NULL;
4749 }
4750 current_scope->had_vla_unspec = true;
4751 }
4752 return declarator;
4753 }
4754
4755 /* Set the contained declarator of an array declarator. DECL is the
4756 declarator, as constructed by build_array_declarator; INNER is what
4757 appears on the left of the []. */
4758
4759 struct c_declarator *
4760 set_array_declarator_inner (struct c_declarator *decl,
4761 struct c_declarator *inner)
4762 {
4763 decl->declarator = inner;
4764 return decl;
4765 }
4766
4767 /* INIT is a constructor that forms DECL's initializer. If the final
4768 element initializes a flexible array field, add the size of that
4769 initializer to DECL's size. */
4770
4771 static void
4772 add_flexible_array_elts_to_size (tree decl, tree init)
4773 {
4774 tree elt, type;
4775
4776 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4777 return;
4778
4779 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4780 type = TREE_TYPE (elt);
4781 if (TREE_CODE (type) == ARRAY_TYPE
4782 && TYPE_SIZE (type) == NULL_TREE
4783 && TYPE_DOMAIN (type) != NULL_TREE
4784 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4785 {
4786 complete_array_type (&type, elt, false);
4787 DECL_SIZE (decl)
4788 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4789 DECL_SIZE_UNIT (decl)
4790 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4791 }
4792 }
4793 \f
4794 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4795 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4796 before the type name, and set *EXPR_CONST_OPERANDS, if
4797 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4798 appear in a constant expression. */
4799
4800 tree
4801 groktypename (struct c_type_name *type_name, tree *expr,
4802 bool *expr_const_operands)
4803 {
4804 tree type;
4805 tree attrs = type_name->specs->attrs;
4806
4807 type_name->specs->attrs = NULL_TREE;
4808
4809 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4810 false, NULL, &attrs, expr, expr_const_operands,
4811 DEPRECATED_NORMAL);
4812
4813 /* Apply attributes. */
4814 decl_attributes (&type, attrs, 0);
4815
4816 return type;
4817 }
4818
4819 /* Wrapper for decl_attributes that adds some implicit attributes
4820 to VAR_DECLs or FUNCTION_DECLs. */
4821
4822 static tree
4823 c_decl_attributes (tree *node, tree attributes, int flags)
4824 {
4825 /* Add implicit "omp declare target" attribute if requested. */
4826 if (current_omp_declare_target_attribute
4827 && ((VAR_P (*node) && is_global_var (*node))
4828 || TREE_CODE (*node) == FUNCTION_DECL))
4829 {
4830 if (VAR_P (*node)
4831 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4832 attributes = tree_cons (get_identifier ("omp declare target implicit"),
4833 NULL_TREE, attributes);
4834 else
4835 attributes = tree_cons (get_identifier ("omp declare target"),
4836 NULL_TREE, attributes);
4837 }
4838
4839 /* Look up the current declaration with all the attributes merged
4840 so far so that attributes on the current declaration that's
4841 about to be pushed that conflict with the former can be detected,
4842 diagnosed, and rejected as appropriate. */
4843 tree last_decl = lookup_name (DECL_NAME (*node));
4844 if (!last_decl)
4845 last_decl = lookup_name_in_scope (DECL_NAME (*node), external_scope);
4846
4847 return decl_attributes (node, attributes, flags, last_decl);
4848 }
4849
4850
4851 /* Decode a declarator in an ordinary declaration or data definition.
4852 This is called as soon as the type information and variable name
4853 have been parsed, before parsing the initializer if any.
4854 Here we create the ..._DECL node, fill in its type,
4855 and put it on the list of decls for the current context.
4856 The ..._DECL node is returned as the value.
4857
4858 Exception: for arrays where the length is not specified,
4859 the type is left null, to be filled in by `finish_decl'.
4860
4861 Function definitions do not come here; they go to start_function
4862 instead. However, external and forward declarations of functions
4863 do go through here. Structure field declarations are done by
4864 grokfield and not through here. */
4865
4866 tree
4867 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4868 bool initialized, tree attributes)
4869 {
4870 tree decl;
4871 tree tem;
4872 tree expr = NULL_TREE;
4873 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4874
4875 /* An object declared as __attribute__((deprecated)) suppresses
4876 warnings of uses of other deprecated items. */
4877 if (lookup_attribute ("deprecated", attributes))
4878 deprecated_state = DEPRECATED_SUPPRESS;
4879
4880 decl = grokdeclarator (declarator, declspecs,
4881 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4882 deprecated_state);
4883 if (!decl || decl == error_mark_node)
4884 return NULL_TREE;
4885
4886 if (expr)
4887 add_stmt (fold_convert (void_type_node, expr));
4888
4889 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
4890 && TREE_PUBLIC (decl))
4891 warning (OPT_Wmain, "%q+D is usually a function", decl);
4892
4893 if (initialized)
4894 /* Is it valid for this decl to have an initializer at all?
4895 If not, set INITIALIZED to zero, which will indirectly
4896 tell 'finish_decl' to ignore the initializer once it is parsed. */
4897 switch (TREE_CODE (decl))
4898 {
4899 case TYPE_DECL:
4900 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
4901 initialized = false;
4902 break;
4903
4904 case FUNCTION_DECL:
4905 error ("function %qD is initialized like a variable", decl);
4906 initialized = false;
4907 break;
4908
4909 case PARM_DECL:
4910 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4911 error ("parameter %qD is initialized", decl);
4912 initialized = false;
4913 break;
4914
4915 default:
4916 /* Don't allow initializations for incomplete types except for
4917 arrays which might be completed by the initialization. */
4918
4919 /* This can happen if the array size is an undefined macro.
4920 We already gave a warning, so we don't need another one. */
4921 if (TREE_TYPE (decl) == error_mark_node)
4922 initialized = false;
4923 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4924 {
4925 /* A complete type is ok if size is fixed. */
4926
4927 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4928 || C_DECL_VARIABLE_SIZE (decl))
4929 {
4930 error ("variable-sized object may not be initialized");
4931 initialized = false;
4932 }
4933 }
4934 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4935 {
4936 error ("variable %qD has initializer but incomplete type", decl);
4937 initialized = false;
4938 }
4939 else if (C_DECL_VARIABLE_SIZE (decl))
4940 {
4941 /* Although C99 is unclear about whether incomplete arrays
4942 of VLAs themselves count as VLAs, it does not make
4943 sense to permit them to be initialized given that
4944 ordinary VLAs may not be initialized. */
4945 error ("variable-sized object may not be initialized");
4946 initialized = false;
4947 }
4948 }
4949
4950 if (initialized)
4951 {
4952 if (current_scope == file_scope)
4953 TREE_STATIC (decl) = 1;
4954
4955 /* Tell 'pushdecl' this is an initialized decl
4956 even though we don't yet have the initializer expression.
4957 Also tell 'finish_decl' it may store the real initializer. */
4958 DECL_INITIAL (decl) = error_mark_node;
4959 }
4960
4961 /* If this is a function declaration, write a record describing it to the
4962 prototypes file (if requested). */
4963
4964 if (TREE_CODE (decl) == FUNCTION_DECL)
4965 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4966
4967 /* ANSI specifies that a tentative definition which is not merged with
4968 a non-tentative definition behaves exactly like a definition with an
4969 initializer equal to zero. (Section 3.7.2)
4970
4971 -fno-common gives strict ANSI behavior, though this tends to break
4972 a large body of code that grew up without this rule.
4973
4974 Thread-local variables are never common, since there's no entrenched
4975 body of code to break, and it allows more efficient variable references
4976 in the presence of dynamic linking. */
4977
4978 if (VAR_P (decl)
4979 && !initialized
4980 && TREE_PUBLIC (decl)
4981 && !DECL_THREAD_LOCAL_P (decl)
4982 && !flag_no_common)
4983 DECL_COMMON (decl) = 1;
4984
4985 /* Set attributes here so if duplicate decl, will have proper attributes. */
4986 c_decl_attributes (&decl, attributes, 0);
4987
4988 /* Handle gnu_inline attribute. */
4989 if (declspecs->inline_p
4990 && !flag_gnu89_inline
4991 && TREE_CODE (decl) == FUNCTION_DECL
4992 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4993 || current_function_decl))
4994 {
4995 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4996 ;
4997 else if (declspecs->storage_class != csc_static)
4998 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4999 }
5000
5001 if (TREE_CODE (decl) == FUNCTION_DECL
5002 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
5003 {
5004 struct c_declarator *ce = declarator;
5005
5006 if (ce->kind == cdk_pointer)
5007 ce = declarator->declarator;
5008 if (ce->kind == cdk_function)
5009 {
5010 tree args = ce->u.arg_info->parms;
5011 for (; args; args = DECL_CHAIN (args))
5012 {
5013 tree type = TREE_TYPE (args);
5014 if (type && INTEGRAL_TYPE_P (type)
5015 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5016 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
5017 }
5018 }
5019 }
5020
5021 if (TREE_CODE (decl) == FUNCTION_DECL
5022 && DECL_DECLARED_INLINE_P (decl)
5023 && DECL_UNINLINABLE (decl)
5024 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5025 warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5026 decl, "noinline");
5027
5028 /* C99 6.7.4p3: An inline definition of a function with external
5029 linkage shall not contain a definition of a modifiable object
5030 with static storage duration... */
5031 if (VAR_P (decl)
5032 && current_scope != file_scope
5033 && TREE_STATIC (decl)
5034 && !TREE_READONLY (decl)
5035 && DECL_DECLARED_INLINE_P (current_function_decl)
5036 && DECL_EXTERNAL (current_function_decl))
5037 record_inline_static (input_location, current_function_decl,
5038 decl, csi_modifiable);
5039
5040 if (c_dialect_objc ()
5041 && VAR_OR_FUNCTION_DECL_P (decl))
5042 objc_check_global_decl (decl);
5043
5044 /* Add this decl to the current scope.
5045 TEM may equal DECL or it may be a previous decl of the same name. */
5046 tem = pushdecl (decl);
5047
5048 if (initialized && DECL_EXTERNAL (tem))
5049 {
5050 DECL_EXTERNAL (tem) = 0;
5051 TREE_STATIC (tem) = 1;
5052 }
5053
5054 return tem;
5055 }
5056
5057 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5058 DECL or the non-array element type if DECL is an uninitialized array.
5059 If that type has a const member, diagnose this. */
5060
5061 static void
5062 diagnose_uninitialized_cst_member (tree decl, tree type)
5063 {
5064 tree field;
5065 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5066 {
5067 tree field_type;
5068 if (TREE_CODE (field) != FIELD_DECL)
5069 continue;
5070 field_type = strip_array_types (TREE_TYPE (field));
5071
5072 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5073 {
5074 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5075 "uninitialized const member in %qT is invalid in C++",
5076 strip_array_types (TREE_TYPE (decl)));
5077 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5078 }
5079
5080 if (RECORD_OR_UNION_TYPE_P (field_type))
5081 diagnose_uninitialized_cst_member (decl, field_type);
5082 }
5083 }
5084
5085 /* Finish processing of a declaration;
5086 install its initial value.
5087 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5088 If the length of an array type is not known before,
5089 it must be determined now, from the initial value, or it is an error.
5090
5091 INIT_LOC is the location of the initial value. */
5092
5093 void
5094 finish_decl (tree decl, location_t init_loc, tree init,
5095 tree origtype, tree asmspec_tree)
5096 {
5097 tree type;
5098 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5099 const char *asmspec = 0;
5100
5101 /* If a name was specified, get the string. */
5102 if (VAR_OR_FUNCTION_DECL_P (decl)
5103 && DECL_FILE_SCOPE_P (decl))
5104 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5105 if (asmspec_tree)
5106 asmspec = TREE_STRING_POINTER (asmspec_tree);
5107
5108 if (VAR_P (decl)
5109 && TREE_STATIC (decl)
5110 && global_bindings_p ())
5111 /* So decl is a global variable. Record the types it uses
5112 so that we can decide later to emit debug info for them. */
5113 record_types_used_by_current_var_decl (decl);
5114
5115 /* If `start_decl' didn't like having an initialization, ignore it now. */
5116 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5117 init = NULL_TREE;
5118
5119 /* Don't crash if parm is initialized. */
5120 if (TREE_CODE (decl) == PARM_DECL)
5121 init = NULL_TREE;
5122
5123 if (init)
5124 store_init_value (init_loc, decl, init, origtype);
5125
5126 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5127 || TREE_CODE (decl) == FIELD_DECL))
5128 objc_check_decl (decl);
5129
5130 type = TREE_TYPE (decl);
5131
5132 /* Deduce size of array from initialization, if not already known.
5133 This is only needed for an initialization in the current scope;
5134 it must not be done for a file-scope initialization of a
5135 declaration with external linkage, redeclared in an inner scope
5136 with the outer declaration shadowed in an intermediate scope. */
5137 if (TREE_CODE (type) == ARRAY_TYPE
5138 && TYPE_DOMAIN (type) == NULL_TREE
5139 && TREE_CODE (decl) != TYPE_DECL
5140 && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5141 {
5142 bool do_default
5143 = (TREE_STATIC (decl)
5144 /* Even if pedantic, an external linkage array
5145 may have incomplete type at first. */
5146 ? pedantic && !TREE_PUBLIC (decl)
5147 : !DECL_EXTERNAL (decl));
5148 int failure
5149 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5150 do_default);
5151
5152 /* Get the completed type made by complete_array_type. */
5153 type = TREE_TYPE (decl);
5154
5155 switch (failure)
5156 {
5157 case 1:
5158 error ("initializer fails to determine size of %q+D", decl);
5159 break;
5160
5161 case 2:
5162 if (do_default)
5163 error ("array size missing in %q+D", decl);
5164 break;
5165
5166 case 3:
5167 error ("zero or negative size array %q+D", decl);
5168 break;
5169
5170 case 0:
5171 /* For global variables, update the copy of the type that
5172 exists in the binding. */
5173 if (TREE_PUBLIC (decl))
5174 {
5175 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5176 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5177 b_ext = b_ext->shadowed;
5178 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5179 {
5180 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5181 b_ext->u.type = composite_type (b_ext->u.type, type);
5182 else
5183 b_ext->u.type = type;
5184 }
5185 }
5186 break;
5187
5188 default:
5189 gcc_unreachable ();
5190 }
5191
5192 if (DECL_INITIAL (decl))
5193 TREE_TYPE (DECL_INITIAL (decl)) = type;
5194
5195 relayout_decl (decl);
5196 }
5197
5198 /* Look for braced array initializers for character arrays and
5199 recursively convert them into STRING_CSTs. */
5200 if (tree init = DECL_INITIAL (decl))
5201 DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5202
5203 if (VAR_P (decl))
5204 {
5205 if (init && TREE_CODE (init) == CONSTRUCTOR)
5206 add_flexible_array_elts_to_size (decl, init);
5207
5208 complete_flexible_array_elts (DECL_INITIAL (decl));
5209
5210 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5211 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5212 layout_decl (decl, 0);
5213
5214 if (DECL_SIZE (decl) == NULL_TREE
5215 /* Don't give an error if we already gave one earlier. */
5216 && TREE_TYPE (decl) != error_mark_node
5217 && (TREE_STATIC (decl)
5218 /* A static variable with an incomplete type
5219 is an error if it is initialized.
5220 Also if it is not file scope.
5221 Otherwise, let it through, but if it is not `extern'
5222 then it may cause an error message later. */
5223 ? (DECL_INITIAL (decl) != NULL_TREE
5224 || !DECL_FILE_SCOPE_P (decl))
5225 /* An automatic variable with an incomplete type
5226 is an error. */
5227 : !DECL_EXTERNAL (decl)))
5228 {
5229 error ("storage size of %q+D isn%'t known", decl);
5230 TREE_TYPE (decl) = error_mark_node;
5231 }
5232
5233 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5234 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5235 && DECL_SIZE (decl) == NULL_TREE
5236 && TREE_STATIC (decl))
5237 incomplete_record_decls.safe_push (decl);
5238
5239 if (is_global_var (decl) && DECL_SIZE (decl) != NULL_TREE)
5240 {
5241 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5242 constant_expression_warning (DECL_SIZE (decl));
5243 else
5244 {
5245 error ("storage size of %q+D isn%'t constant", decl);
5246 TREE_TYPE (decl) = error_mark_node;
5247 }
5248 }
5249
5250 if (TREE_USED (type))
5251 {
5252 TREE_USED (decl) = 1;
5253 DECL_READ_P (decl) = 1;
5254 }
5255 }
5256
5257 /* If this is a function and an assembler name is specified, reset DECL_RTL
5258 so we can give it its new name. Also, update builtin_decl if it
5259 was a normal built-in. */
5260 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5261 {
5262 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5263 set_builtin_user_assembler_name (decl, asmspec);
5264 set_user_assembler_name (decl, asmspec);
5265 }
5266
5267 /* If #pragma weak was used, mark the decl weak now. */
5268 maybe_apply_pragma_weak (decl);
5269
5270 /* Output the assembler code and/or RTL code for variables and functions,
5271 unless the type is an undefined structure or union.
5272 If not, it will get done when the type is completed. */
5273
5274 if (VAR_OR_FUNCTION_DECL_P (decl))
5275 {
5276 /* Determine the ELF visibility. */
5277 if (TREE_PUBLIC (decl))
5278 c_determine_visibility (decl);
5279
5280 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5281 if (c_dialect_objc ())
5282 objc_check_decl (decl);
5283
5284 if (asmspec)
5285 {
5286 /* If this is not a static variable, issue a warning.
5287 It doesn't make any sense to give an ASMSPEC for an
5288 ordinary, non-register local variable. Historically,
5289 GCC has accepted -- but ignored -- the ASMSPEC in
5290 this case. */
5291 if (!DECL_FILE_SCOPE_P (decl)
5292 && VAR_P (decl)
5293 && !C_DECL_REGISTER (decl)
5294 && !TREE_STATIC (decl))
5295 warning (0, "ignoring %<asm%> specifier for non-static local "
5296 "variable %q+D", decl);
5297 else
5298 set_user_assembler_name (decl, asmspec);
5299 }
5300
5301 if (DECL_FILE_SCOPE_P (decl))
5302 {
5303 if (DECL_INITIAL (decl) == NULL_TREE
5304 || DECL_INITIAL (decl) == error_mark_node)
5305 /* Don't output anything
5306 when a tentative file-scope definition is seen.
5307 But at end of compilation, do output code for them. */
5308 DECL_DEFER_OUTPUT (decl) = 1;
5309 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5310 DECL_HARD_REGISTER (decl) = 1;
5311 rest_of_decl_compilation (decl, true, 0);
5312 }
5313 else
5314 {
5315 /* In conjunction with an ASMSPEC, the `register'
5316 keyword indicates that we should place the variable
5317 in a particular register. */
5318 if (asmspec && C_DECL_REGISTER (decl))
5319 {
5320 DECL_HARD_REGISTER (decl) = 1;
5321 /* This cannot be done for a structure with volatile
5322 fields, on which DECL_REGISTER will have been
5323 reset. */
5324 if (!DECL_REGISTER (decl))
5325 error ("cannot put object with volatile field into register");
5326 }
5327
5328 if (TREE_CODE (decl) != FUNCTION_DECL)
5329 {
5330 /* If we're building a variable sized type, and we might be
5331 reachable other than via the top of the current binding
5332 level, then create a new BIND_EXPR so that we deallocate
5333 the object at the right time. */
5334 /* Note that DECL_SIZE can be null due to errors. */
5335 if (DECL_SIZE (decl)
5336 && !TREE_CONSTANT (DECL_SIZE (decl))
5337 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5338 {
5339 tree bind;
5340 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5341 TREE_SIDE_EFFECTS (bind) = 1;
5342 add_stmt (bind);
5343 BIND_EXPR_BODY (bind) = push_stmt_list ();
5344 }
5345 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5346 DECL_EXPR, decl));
5347 }
5348 }
5349
5350
5351 if (!DECL_FILE_SCOPE_P (decl))
5352 {
5353 /* Recompute the RTL of a local array now
5354 if it used to be an incomplete type. */
5355 if (was_incomplete && !is_global_var (decl))
5356 {
5357 /* If we used it already as memory, it must stay in memory. */
5358 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5359 /* If it's still incomplete now, no init will save it. */
5360 if (DECL_SIZE (decl) == NULL_TREE)
5361 DECL_INITIAL (decl) = NULL_TREE;
5362 }
5363 }
5364 }
5365
5366 if (TREE_CODE (decl) == TYPE_DECL)
5367 {
5368 if (!DECL_FILE_SCOPE_P (decl)
5369 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5370 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5371
5372 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5373 }
5374
5375 /* Install a cleanup (aka destructor) if one was given. */
5376 if (VAR_P (decl) && !TREE_STATIC (decl))
5377 {
5378 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5379 if (attr)
5380 {
5381 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5382 tree cleanup_decl = lookup_name (cleanup_id);
5383 tree cleanup;
5384 vec<tree, va_gc> *v;
5385
5386 /* Build "cleanup(&decl)" for the destructor. */
5387 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5388 vec_alloc (v, 1);
5389 v->quick_push (cleanup);
5390 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5391 vNULL, cleanup_decl, v, NULL);
5392 vec_free (v);
5393
5394 /* Don't warn about decl unused; the cleanup uses it. */
5395 TREE_USED (decl) = 1;
5396 TREE_USED (cleanup_decl) = 1;
5397 DECL_READ_P (decl) = 1;
5398
5399 push_cleanup (decl, cleanup, false);
5400 }
5401 }
5402
5403 if (warn_cxx_compat
5404 && VAR_P (decl)
5405 && !DECL_EXTERNAL (decl)
5406 && DECL_INITIAL (decl) == NULL_TREE)
5407 {
5408 type = strip_array_types (type);
5409 if (TREE_READONLY (decl))
5410 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5411 "uninitialized %<const %D%> is invalid in C++", decl);
5412 else if (RECORD_OR_UNION_TYPE_P (type)
5413 && C_TYPE_FIELDS_READONLY (type))
5414 diagnose_uninitialized_cst_member (decl, type);
5415 }
5416
5417 if (flag_openmp
5418 && VAR_P (decl)
5419 && lookup_attribute ("omp declare target implicit",
5420 DECL_ATTRIBUTES (decl)))
5421 {
5422 DECL_ATTRIBUTES (decl)
5423 = remove_attribute ("omp declare target implicit",
5424 DECL_ATTRIBUTES (decl));
5425 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5426 error ("%q+D in declare target directive does not have mappable type",
5427 decl);
5428 else if (!lookup_attribute ("omp declare target",
5429 DECL_ATTRIBUTES (decl))
5430 && !lookup_attribute ("omp declare target link",
5431 DECL_ATTRIBUTES (decl)))
5432 DECL_ATTRIBUTES (decl)
5433 = tree_cons (get_identifier ("omp declare target"),
5434 NULL_TREE, DECL_ATTRIBUTES (decl));
5435 }
5436
5437 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5438 }
5439
5440 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5441 EXPR is NULL or a pointer to an expression that needs to be
5442 evaluated for the side effects of array size expressions in the
5443 parameters. */
5444
5445 tree
5446 grokparm (const struct c_parm *parm, tree *expr)
5447 {
5448 tree attrs = parm->attrs;
5449 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5450 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5451
5452 decl_attributes (&decl, attrs, 0);
5453
5454 return decl;
5455 }
5456
5457 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5458 and push that on the current scope. EXPR is a pointer to an
5459 expression that needs to be evaluated for the side effects of array
5460 size expressions in the parameters. */
5461
5462 void
5463 push_parm_decl (const struct c_parm *parm, tree *expr)
5464 {
5465 tree attrs = parm->attrs;
5466 tree decl;
5467
5468 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5469 &attrs, expr, NULL, DEPRECATED_NORMAL);
5470 if (decl && DECL_P (decl))
5471 DECL_SOURCE_LOCATION (decl) = parm->loc;
5472 decl_attributes (&decl, attrs, 0);
5473
5474 decl = pushdecl (decl);
5475
5476 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5477 }
5478
5479 /* Mark all the parameter declarations to date as forward decls.
5480 Also diagnose use of this extension. */
5481
5482 void
5483 mark_forward_parm_decls (void)
5484 {
5485 struct c_binding *b;
5486
5487 if (pedantic && !current_scope->warned_forward_parm_decls)
5488 {
5489 pedwarn (input_location, OPT_Wpedantic,
5490 "ISO C forbids forward parameter declarations");
5491 current_scope->warned_forward_parm_decls = true;
5492 }
5493
5494 for (b = current_scope->bindings; b; b = b->prev)
5495 if (TREE_CODE (b->decl) == PARM_DECL)
5496 TREE_ASM_WRITTEN (b->decl) = 1;
5497 }
5498 \f
5499 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5500 literal, which may be an incomplete array type completed by the
5501 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5502 literal. NON_CONST is true if the initializers contain something
5503 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
5504 it is the (valid) alignment for this compound literal, as specified
5505 with _Alignas. */
5506
5507 tree
5508 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
5509 unsigned int alignas_align)
5510 {
5511 /* We do not use start_decl here because we have a type, not a declarator;
5512 and do not use finish_decl because the decl should be stored inside
5513 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5514 tree decl;
5515 tree complit;
5516 tree stmt;
5517
5518 if (type == error_mark_node
5519 || init == error_mark_node)
5520 return error_mark_node;
5521
5522 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5523 DECL_EXTERNAL (decl) = 0;
5524 TREE_PUBLIC (decl) = 0;
5525 TREE_STATIC (decl) = (current_scope == file_scope);
5526 DECL_CONTEXT (decl) = current_function_decl;
5527 TREE_USED (decl) = 1;
5528 DECL_READ_P (decl) = 1;
5529 DECL_ARTIFICIAL (decl) = 1;
5530 DECL_IGNORED_P (decl) = 1;
5531 C_DECL_COMPOUND_LITERAL_P (decl) = 1;
5532 TREE_TYPE (decl) = type;
5533 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
5534 if (alignas_align)
5535 {
5536 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
5537 DECL_USER_ALIGN (decl) = 1;
5538 }
5539 store_init_value (loc, decl, init, NULL_TREE);
5540
5541 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5542 {
5543 int failure = complete_array_type (&TREE_TYPE (decl),
5544 DECL_INITIAL (decl), true);
5545 /* If complete_array_type returns 3, it means that the
5546 initial value of the compound literal is empty. Allow it. */
5547 gcc_assert (failure == 0 || failure == 3);
5548
5549 type = TREE_TYPE (decl);
5550 TREE_TYPE (DECL_INITIAL (decl)) = type;
5551 }
5552
5553 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5554 {
5555 c_incomplete_type_error (loc, NULL_TREE, type);
5556 return error_mark_node;
5557 }
5558
5559 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5560 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5561 TREE_SIDE_EFFECTS (complit) = 1;
5562
5563 layout_decl (decl, 0);
5564
5565 if (TREE_STATIC (decl))
5566 {
5567 /* This decl needs a name for the assembler output. */
5568 set_compound_literal_name (decl);
5569 DECL_DEFER_OUTPUT (decl) = 1;
5570 DECL_COMDAT (decl) = 1;
5571 pushdecl (decl);
5572 rest_of_decl_compilation (decl, 1, 0);
5573 }
5574 else if (current_function_decl && !current_scope->parm_flag)
5575 pushdecl (decl);
5576
5577 if (non_const)
5578 {
5579 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5580 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5581 }
5582
5583 return complit;
5584 }
5585
5586 /* Check the type of a compound literal. Here we just check that it
5587 is valid for C++. */
5588
5589 void
5590 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5591 {
5592 if (warn_cxx_compat
5593 && (type_name->specs->typespec_kind == ctsk_tagdef
5594 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5595 warning_at (loc, OPT_Wc___compat,
5596 "defining a type in a compound literal is invalid in C++");
5597 }
5598 \f
5599 /* Determine whether TYPE is a structure with a flexible array member,
5600 or a union containing such a structure (possibly recursively). */
5601
5602 static bool
5603 flexible_array_type_p (tree type)
5604 {
5605 tree x;
5606 switch (TREE_CODE (type))
5607 {
5608 case RECORD_TYPE:
5609 x = TYPE_FIELDS (type);
5610 if (x == NULL_TREE)
5611 return false;
5612 while (DECL_CHAIN (x) != NULL_TREE)
5613 x = DECL_CHAIN (x);
5614 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5615 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5616 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5617 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5618 return true;
5619 return false;
5620 case UNION_TYPE:
5621 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5622 {
5623 if (flexible_array_type_p (TREE_TYPE (x)))
5624 return true;
5625 }
5626 return false;
5627 default:
5628 return false;
5629 }
5630 }
5631 \f
5632 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5633 replacing with appropriate values if they are invalid. */
5634
5635 static void
5636 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5637 tree orig_name)
5638 {
5639 tree type_mv;
5640 unsigned int max_width;
5641 unsigned HOST_WIDE_INT w;
5642 const char *name = (orig_name
5643 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5644 : _("<anonymous>"));
5645
5646 /* Detect and ignore out of range field width and process valid
5647 field widths. */
5648 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5649 {
5650 error_at (loc, "bit-field %qs width not an integer constant", name);
5651 *width = integer_one_node;
5652 }
5653 else
5654 {
5655 if (TREE_CODE (*width) != INTEGER_CST)
5656 {
5657 *width = c_fully_fold (*width, false, NULL);
5658 if (TREE_CODE (*width) == INTEGER_CST)
5659 pedwarn (loc, OPT_Wpedantic,
5660 "bit-field %qs width not an integer constant expression",
5661 name);
5662 }
5663 if (TREE_CODE (*width) != INTEGER_CST)
5664 {
5665 error_at (loc, "bit-field %qs width not an integer constant", name);
5666 *width = integer_one_node;
5667 }
5668 constant_expression_warning (*width);
5669 if (tree_int_cst_sgn (*width) < 0)
5670 {
5671 error_at (loc, "negative width in bit-field %qs", name);
5672 *width = integer_one_node;
5673 }
5674 else if (integer_zerop (*width) && orig_name)
5675 {
5676 error_at (loc, "zero width for bit-field %qs", name);
5677 *width = integer_one_node;
5678 }
5679 }
5680
5681 /* Detect invalid bit-field type. */
5682 if (TREE_CODE (*type) != INTEGER_TYPE
5683 && TREE_CODE (*type) != BOOLEAN_TYPE
5684 && TREE_CODE (*type) != ENUMERAL_TYPE)
5685 {
5686 error_at (loc, "bit-field %qs has invalid type", name);
5687 *type = unsigned_type_node;
5688 }
5689
5690 if (TYPE_WARN_IF_NOT_ALIGN (*type))
5691 {
5692 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5693 name);
5694 *type = unsigned_type_node;
5695 }
5696
5697 type_mv = TYPE_MAIN_VARIANT (*type);
5698 if (!in_system_header_at (input_location)
5699 && type_mv != integer_type_node
5700 && type_mv != unsigned_type_node
5701 && type_mv != boolean_type_node)
5702 pedwarn_c90 (loc, OPT_Wpedantic,
5703 "type of bit-field %qs is a GCC extension", name);
5704
5705 max_width = TYPE_PRECISION (*type);
5706
5707 if (compare_tree_int (*width, max_width) > 0)
5708 {
5709 error_at (loc, "width of %qs exceeds its type", name);
5710 w = max_width;
5711 *width = build_int_cst (integer_type_node, w);
5712 }
5713 else
5714 w = tree_to_uhwi (*width);
5715
5716 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5717 {
5718 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5719 if (!lt
5720 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5721 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5722 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5723 }
5724 }
5725
5726 \f
5727
5728 /* Print warning about variable length array if necessary. */
5729
5730 static void
5731 warn_variable_length_array (tree name, tree size)
5732 {
5733 if (TREE_CONSTANT (size))
5734 {
5735 if (name)
5736 pedwarn_c90 (input_location, OPT_Wvla,
5737 "ISO C90 forbids array %qE whose size "
5738 "cannot be evaluated", name);
5739 else
5740 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5741 "whose size cannot be evaluated");
5742 }
5743 else
5744 {
5745 if (name)
5746 pedwarn_c90 (input_location, OPT_Wvla,
5747 "ISO C90 forbids variable length array %qE", name);
5748 else
5749 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5750 "length array");
5751 }
5752 }
5753
5754 /* Print warning about defaulting to int if necessary. */
5755
5756 static void
5757 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5758 {
5759 diagnostic_info diagnostic;
5760 va_list ap;
5761 rich_location richloc (line_table, location);
5762
5763 va_start (ap, gmsgid);
5764 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5765 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5766 diagnostic.option_index = opt;
5767 diagnostic_report_diagnostic (global_dc, &diagnostic);
5768 va_end (ap);
5769 }
5770
5771 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5772 considering only those c_declspec_words found in LIST, which
5773 must be terminated by cdw_number_of_elements. */
5774
5775 static location_t
5776 smallest_type_quals_location (const location_t *locations,
5777 const c_declspec_word *list)
5778 {
5779 location_t loc = UNKNOWN_LOCATION;
5780 while (*list != cdw_number_of_elements)
5781 {
5782 location_t newloc = locations[*list];
5783 if (loc == UNKNOWN_LOCATION
5784 || (newloc != UNKNOWN_LOCATION && newloc < loc))
5785 loc = newloc;
5786 list++;
5787 }
5788
5789 return loc;
5790 }
5791
5792 /* Given declspecs and a declarator,
5793 determine the name and type of the object declared
5794 and construct a ..._DECL node for it.
5795 (In one case we can return a ..._TYPE node instead.
5796 For invalid input we sometimes return NULL_TREE.)
5797
5798 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5799
5800 DECL_CONTEXT says which syntactic context this declaration is in:
5801 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5802 FUNCDEF for a function definition. Like NORMAL but a few different
5803 error messages in each case. Return value may be zero meaning
5804 this definition is too screwy to try to parse.
5805 PARM for a parameter declaration (either within a function prototype
5806 or before a function body). Make a PARM_DECL, or return void_type_node.
5807 TYPENAME if for a typename (in a cast or sizeof).
5808 Don't make a DECL node; just return the ..._TYPE node.
5809 FIELD for a struct or union field; make a FIELD_DECL.
5810 INITIALIZED is true if the decl has an initializer.
5811 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5812 representing the width of the bit-field.
5813 DECL_ATTRS points to the list of attributes that should be added to this
5814 decl. Any nested attributes that belong on the decl itself will be
5815 added to this list.
5816 If EXPR is not NULL, any expressions that need to be evaluated as
5817 part of evaluating variably modified types will be stored in *EXPR.
5818 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5819 set to indicate whether operands in *EXPR can be used in constant
5820 expressions.
5821 DEPRECATED_STATE is a deprecated_states value indicating whether
5822 deprecation warnings should be suppressed.
5823
5824 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5825 It may also be so in the PARM case, for a prototype where the
5826 argument type is specified but not the name.
5827
5828 This function is where the complicated C meanings of `static'
5829 and `extern' are interpreted. */
5830
5831 static tree
5832 grokdeclarator (const struct c_declarator *declarator,
5833 struct c_declspecs *declspecs,
5834 enum decl_context decl_context, bool initialized, tree *width,
5835 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5836 enum deprecated_states deprecated_state)
5837 {
5838 tree type = declspecs->type;
5839 bool threadp = declspecs->thread_p;
5840 enum c_storage_class storage_class = declspecs->storage_class;
5841 int constp;
5842 int restrictp;
5843 int volatilep;
5844 int atomicp;
5845 int type_quals = TYPE_UNQUALIFIED;
5846 tree name = NULL_TREE;
5847 bool funcdef_flag = false;
5848 bool funcdef_syntax = false;
5849 bool size_varies = false;
5850 tree decl_attr = declspecs->decl_attr;
5851 int array_ptr_quals = TYPE_UNQUALIFIED;
5852 tree array_ptr_attrs = NULL_TREE;
5853 bool array_parm_static = false;
5854 bool array_parm_vla_unspec_p = false;
5855 tree returned_attrs = NULL_TREE;
5856 bool bitfield = width != NULL;
5857 tree element_type;
5858 tree orig_qual_type = NULL;
5859 size_t orig_qual_indirect = 0;
5860 struct c_arg_info *arg_info = 0;
5861 addr_space_t as1, as2, address_space;
5862 location_t loc = UNKNOWN_LOCATION;
5863 tree expr_dummy;
5864 bool expr_const_operands_dummy;
5865 enum c_declarator_kind first_non_attr_kind;
5866 unsigned int alignas_align = 0;
5867
5868 if (TREE_CODE (type) == ERROR_MARK)
5869 return error_mark_node;
5870 if (expr == NULL)
5871 {
5872 expr = &expr_dummy;
5873 expr_dummy = NULL_TREE;
5874 }
5875 if (expr_const_operands == NULL)
5876 expr_const_operands = &expr_const_operands_dummy;
5877
5878 if (declspecs->expr)
5879 {
5880 if (*expr)
5881 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
5882 declspecs->expr);
5883 else
5884 *expr = declspecs->expr;
5885 }
5886 *expr_const_operands = declspecs->expr_const_operands;
5887
5888 if (decl_context == FUNCDEF)
5889 funcdef_flag = true, decl_context = NORMAL;
5890
5891 /* Look inside a declarator for the name being declared
5892 and get it as an IDENTIFIER_NODE, for an error message. */
5893 {
5894 const struct c_declarator *decl = declarator;
5895
5896 first_non_attr_kind = cdk_attrs;
5897 while (decl)
5898 switch (decl->kind)
5899 {
5900 case cdk_array:
5901 loc = decl->id_loc;
5902 /* FALL THRU. */
5903
5904 case cdk_function:
5905 case cdk_pointer:
5906 funcdef_syntax = (decl->kind == cdk_function);
5907 if (first_non_attr_kind == cdk_attrs)
5908 first_non_attr_kind = decl->kind;
5909 decl = decl->declarator;
5910 break;
5911
5912 case cdk_attrs:
5913 decl = decl->declarator;
5914 break;
5915
5916 case cdk_id:
5917 loc = decl->id_loc;
5918 if (decl->u.id)
5919 name = decl->u.id;
5920 if (first_non_attr_kind == cdk_attrs)
5921 first_non_attr_kind = decl->kind;
5922 decl = 0;
5923 break;
5924
5925 default:
5926 gcc_unreachable ();
5927 }
5928 if (name == NULL_TREE)
5929 {
5930 gcc_assert (decl_context == PARM
5931 || decl_context == TYPENAME
5932 || (decl_context == FIELD
5933 && declarator->kind == cdk_id));
5934 gcc_assert (!initialized);
5935 }
5936 }
5937
5938 /* A function definition's declarator must have the form of
5939 a function declarator. */
5940
5941 if (funcdef_flag && !funcdef_syntax)
5942 return NULL_TREE;
5943
5944 /* If this looks like a function definition, make it one,
5945 even if it occurs where parms are expected.
5946 Then store_parm_decls will reject it and not use it as a parm. */
5947 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5948 decl_context = PARM;
5949
5950 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5951 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5952
5953 if ((decl_context == NORMAL || decl_context == FIELD)
5954 && current_scope == file_scope
5955 && variably_modified_type_p (type, NULL_TREE))
5956 {
5957 if (name)
5958 error_at (loc, "variably modified %qE at file scope", name);
5959 else
5960 error_at (loc, "variably modified field at file scope");
5961 type = integer_type_node;
5962 }
5963
5964 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5965
5966 /* Diagnose defaulting to "int". */
5967
5968 if (declspecs->default_int_p && !in_system_header_at (input_location))
5969 {
5970 /* Issue a warning if this is an ISO C 99 program or if
5971 -Wreturn-type and this is a function, or if -Wimplicit;
5972 prefer the former warning since it is more explicit. */
5973 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
5974 && funcdef_flag)
5975 warn_about_return_type = 1;
5976 else
5977 {
5978 if (name)
5979 warn_defaults_to (loc, OPT_Wimplicit_int,
5980 "type defaults to %<int%> in declaration "
5981 "of %qE", name);
5982 else
5983 warn_defaults_to (loc, OPT_Wimplicit_int,
5984 "type defaults to %<int%> in type name");
5985 }
5986 }
5987
5988 /* Adjust the type if a bit-field is being declared,
5989 -funsigned-bitfields applied and the type is not explicitly
5990 "signed". */
5991 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5992 && TREE_CODE (type) == INTEGER_TYPE)
5993 type = unsigned_type_for (type);
5994
5995 /* Figure out the type qualifiers for the declaration. There are
5996 two ways a declaration can become qualified. One is something
5997 like `const int i' where the `const' is explicit. Another is
5998 something like `typedef const int CI; CI i' where the type of the
5999 declaration contains the `const'. A third possibility is that
6000 there is a type qualifier on the element type of a typedefed
6001 array type, in which case we should extract that qualifier so
6002 that c_apply_type_quals_to_decl receives the full list of
6003 qualifiers to work with (C90 is not entirely clear about whether
6004 duplicate qualifiers should be diagnosed in this case, but it
6005 seems most appropriate to do so). */
6006 element_type = strip_array_types (type);
6007 constp = declspecs->const_p + TYPE_READONLY (element_type);
6008 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6009 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6010 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6011 as1 = declspecs->address_space;
6012 as2 = TYPE_ADDR_SPACE (element_type);
6013 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6014
6015 if (constp > 1)
6016 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
6017 if (restrictp > 1)
6018 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
6019 if (volatilep > 1)
6020 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
6021 if (atomicp > 1)
6022 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
6023
6024 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6025 error_at (loc, "conflicting named address spaces (%s vs %s)",
6026 c_addr_space_name (as1), c_addr_space_name (as2));
6027
6028 if ((TREE_CODE (type) == ARRAY_TYPE
6029 || first_non_attr_kind == cdk_array)
6030 && TYPE_QUALS (element_type))
6031 {
6032 orig_qual_type = type;
6033 type = TYPE_MAIN_VARIANT (type);
6034 }
6035 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6036 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6037 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6038 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6039 | ENCODE_QUAL_ADDR_SPACE (address_space));
6040 if (type_quals != TYPE_QUALS (element_type))
6041 orig_qual_type = NULL_TREE;
6042
6043 /* Applying the _Atomic qualifier to an array type (through the use
6044 of typedefs or typeof) must be detected here. If the qualifier
6045 is introduced later, any appearance of applying it to an array is
6046 actually applying it to an element of that array. */
6047 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
6048 error_at (loc, "%<_Atomic%>-qualified array type");
6049
6050 /* Warn about storage classes that are invalid for certain
6051 kinds of declarations (parameters, typenames, etc.). */
6052
6053 if (funcdef_flag
6054 && (threadp
6055 || storage_class == csc_auto
6056 || storage_class == csc_register
6057 || storage_class == csc_typedef))
6058 {
6059 if (storage_class == csc_auto)
6060 pedwarn (loc,
6061 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
6062 "function definition declared %<auto%>");
6063 if (storage_class == csc_register)
6064 error_at (loc, "function definition declared %<register%>");
6065 if (storage_class == csc_typedef)
6066 error_at (loc, "function definition declared %<typedef%>");
6067 if (threadp)
6068 error_at (loc, "function definition declared %qs",
6069 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6070 threadp = false;
6071 if (storage_class == csc_auto
6072 || storage_class == csc_register
6073 || storage_class == csc_typedef)
6074 storage_class = csc_none;
6075 }
6076 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
6077 {
6078 if (decl_context == PARM && storage_class == csc_register)
6079 ;
6080 else
6081 {
6082 switch (decl_context)
6083 {
6084 case FIELD:
6085 if (name)
6086 error_at (loc, "storage class specified for structure "
6087 "field %qE", name);
6088 else
6089 error_at (loc, "storage class specified for structure field");
6090 break;
6091 case PARM:
6092 if (name)
6093 error_at (loc, "storage class specified for parameter %qE",
6094 name);
6095 else
6096 error_at (loc, "storage class specified for unnamed parameter");
6097 break;
6098 default:
6099 error_at (loc, "storage class specified for typename");
6100 break;
6101 }
6102 storage_class = csc_none;
6103 threadp = false;
6104 }
6105 }
6106 else if (storage_class == csc_extern
6107 && initialized
6108 && !funcdef_flag)
6109 {
6110 /* 'extern' with initialization is invalid if not at file scope. */
6111 if (current_scope == file_scope)
6112 {
6113 /* It is fine to have 'extern const' when compiling at C
6114 and C++ intersection. */
6115 if (!(warn_cxx_compat && constp))
6116 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
6117 name);
6118 }
6119 else
6120 error_at (loc, "%qE has both %<extern%> and initializer", name);
6121 }
6122 else if (current_scope == file_scope)
6123 {
6124 if (storage_class == csc_auto)
6125 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
6126 name);
6127 if (pedantic && storage_class == csc_register)
6128 pedwarn (input_location, OPT_Wpedantic,
6129 "file-scope declaration of %qE specifies %<register%>", name);
6130 }
6131 else
6132 {
6133 if (storage_class == csc_extern && funcdef_flag)
6134 error_at (loc, "nested function %qE declared %<extern%>", name);
6135 else if (threadp && storage_class == csc_none)
6136 {
6137 error_at (loc, "function-scope %qE implicitly auto and declared "
6138 "%qs", name,
6139 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6140 threadp = false;
6141 }
6142 }
6143
6144 /* Now figure out the structure of the declarator proper.
6145 Descend through it, creating more complex types, until we reach
6146 the declared identifier (or NULL_TREE, in an absolute declarator).
6147 At each stage we maintain an unqualified version of the type
6148 together with any qualifiers that should be applied to it with
6149 c_build_qualified_type; this way, array types including
6150 multidimensional array types are first built up in unqualified
6151 form and then the qualified form is created with
6152 TYPE_MAIN_VARIANT pointing to the unqualified form. */
6153
6154 while (declarator && declarator->kind != cdk_id)
6155 {
6156 if (type == error_mark_node)
6157 {
6158 declarator = declarator->declarator;
6159 continue;
6160 }
6161
6162 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
6163 a cdk_pointer (for *...),
6164 a cdk_function (for ...(...)),
6165 a cdk_attrs (for nested attributes),
6166 or a cdk_id (for the name being declared
6167 or the place in an absolute declarator
6168 where the name was omitted).
6169 For the last case, we have just exited the loop.
6170
6171 At this point, TYPE is the type of elements of an array,
6172 or for a function to return, or for a pointer to point to.
6173 After this sequence of ifs, TYPE is the type of the
6174 array or function or pointer, and DECLARATOR has had its
6175 outermost layer removed. */
6176
6177 if (array_ptr_quals != TYPE_UNQUALIFIED
6178 || array_ptr_attrs != NULL_TREE
6179 || array_parm_static)
6180 {
6181 /* Only the innermost declarator (making a parameter be of
6182 array type which is converted to pointer type)
6183 may have static or type qualifiers. */
6184 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6185 array_ptr_quals = TYPE_UNQUALIFIED;
6186 array_ptr_attrs = NULL_TREE;
6187 array_parm_static = false;
6188 }
6189
6190 switch (declarator->kind)
6191 {
6192 case cdk_attrs:
6193 {
6194 /* A declarator with embedded attributes. */
6195 tree attrs = declarator->u.attrs;
6196 const struct c_declarator *inner_decl;
6197 int attr_flags = 0;
6198 declarator = declarator->declarator;
6199 inner_decl = declarator;
6200 while (inner_decl->kind == cdk_attrs)
6201 inner_decl = inner_decl->declarator;
6202 if (inner_decl->kind == cdk_id)
6203 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6204 else if (inner_decl->kind == cdk_function)
6205 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6206 else if (inner_decl->kind == cdk_array)
6207 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6208 returned_attrs = decl_attributes (&type,
6209 chainon (returned_attrs, attrs),
6210 attr_flags);
6211 break;
6212 }
6213 case cdk_array:
6214 {
6215 tree itype = NULL_TREE;
6216 tree size = declarator->u.array.dimen;
6217 /* The index is a signed object `sizetype' bits wide. */
6218 tree index_type = c_common_signed_type (sizetype);
6219
6220 array_ptr_quals = declarator->u.array.quals;
6221 array_ptr_attrs = declarator->u.array.attrs;
6222 array_parm_static = declarator->u.array.static_p;
6223 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6224
6225 declarator = declarator->declarator;
6226
6227 /* Check for some types that there cannot be arrays of. */
6228
6229 if (VOID_TYPE_P (type))
6230 {
6231 if (name)
6232 error_at (loc, "declaration of %qE as array of voids", name);
6233 else
6234 error_at (loc, "declaration of type name as array of voids");
6235 type = error_mark_node;
6236 }
6237
6238 if (TREE_CODE (type) == FUNCTION_TYPE)
6239 {
6240 if (name)
6241 error_at (loc, "declaration of %qE as array of functions",
6242 name);
6243 else
6244 error_at (loc, "declaration of type name as array of "
6245 "functions");
6246 type = error_mark_node;
6247 }
6248
6249 if (pedantic && !in_system_header_at (input_location)
6250 && flexible_array_type_p (type))
6251 pedwarn (loc, OPT_Wpedantic,
6252 "invalid use of structure with flexible array member");
6253
6254 if (size == error_mark_node)
6255 type = error_mark_node;
6256
6257 if (type == error_mark_node)
6258 continue;
6259
6260 /* If size was specified, set ITYPE to a range-type for
6261 that size. Otherwise, ITYPE remains null. finish_decl
6262 may figure it out from an initial value. */
6263
6264 if (size)
6265 {
6266 bool size_maybe_const = true;
6267 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6268 && !TREE_OVERFLOW (size));
6269 bool this_size_varies = false;
6270
6271 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6272 lvalue. */
6273 STRIP_TYPE_NOPS (size);
6274
6275 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6276 {
6277 if (name)
6278 error_at (loc, "size of array %qE has non-integer type",
6279 name);
6280 else
6281 error_at (loc,
6282 "size of unnamed array has non-integer type");
6283 size = integer_one_node;
6284 }
6285 /* This can happen with enum forward declaration. */
6286 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6287 {
6288 if (name)
6289 error_at (loc, "size of array %qE has incomplete type",
6290 name);
6291 else
6292 error_at (loc, "size of unnamed array has incomplete "
6293 "type");
6294 size = integer_one_node;
6295 }
6296
6297 size = c_fully_fold (size, false, &size_maybe_const);
6298
6299 if (pedantic && size_maybe_const && integer_zerop (size))
6300 {
6301 if (name)
6302 pedwarn (loc, OPT_Wpedantic,
6303 "ISO C forbids zero-size array %qE", name);
6304 else
6305 pedwarn (loc, OPT_Wpedantic,
6306 "ISO C forbids zero-size array");
6307 }
6308
6309 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6310 {
6311 constant_expression_warning (size);
6312 if (tree_int_cst_sgn (size) < 0)
6313 {
6314 if (name)
6315 error_at (loc, "size of array %qE is negative", name);
6316 else
6317 error_at (loc, "size of unnamed array is negative");
6318 size = integer_one_node;
6319 }
6320 /* Handle a size folded to an integer constant but
6321 not an integer constant expression. */
6322 if (!size_int_const)
6323 {
6324 /* If this is a file scope declaration of an
6325 ordinary identifier, this is invalid code;
6326 diagnosing it here and not subsequently
6327 treating the type as variable-length avoids
6328 more confusing diagnostics later. */
6329 if ((decl_context == NORMAL || decl_context == FIELD)
6330 && current_scope == file_scope)
6331 pedwarn (input_location, 0,
6332 "variably modified %qE at file scope",
6333 name);
6334 else
6335 this_size_varies = size_varies = true;
6336 warn_variable_length_array (name, size);
6337 }
6338 }
6339 else if ((decl_context == NORMAL || decl_context == FIELD)
6340 && current_scope == file_scope)
6341 {
6342 error_at (loc, "variably modified %qE at file scope", name);
6343 size = integer_one_node;
6344 }
6345 else
6346 {
6347 /* Make sure the array size remains visibly
6348 nonconstant even if it is (eg) a const variable
6349 with known value. */
6350 this_size_varies = size_varies = true;
6351 warn_variable_length_array (name, size);
6352 if (sanitize_flags_p (SANITIZE_VLA)
6353 && current_function_decl != NULL_TREE
6354 && decl_context == NORMAL)
6355 {
6356 /* Evaluate the array size only once. */
6357 size = save_expr (size);
6358 size = c_fully_fold (size, false, NULL);
6359 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6360 ubsan_instrument_vla (loc, size),
6361 size);
6362 }
6363 }
6364
6365 if (integer_zerop (size) && !this_size_varies)
6366 {
6367 /* A zero-length array cannot be represented with
6368 an unsigned index type, which is what we'll
6369 get with build_index_type. Create an
6370 open-ended range instead. */
6371 itype = build_range_type (sizetype, size, NULL_TREE);
6372 }
6373 else
6374 {
6375 /* Arrange for the SAVE_EXPR on the inside of the
6376 MINUS_EXPR, which allows the -1 to get folded
6377 with the +1 that happens when building TYPE_SIZE. */
6378 if (size_varies)
6379 size = save_expr (size);
6380 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6381 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6382 integer_zero_node, size);
6383
6384 /* Compute the maximum valid index, that is, size
6385 - 1. Do the calculation in index_type, so that
6386 if it is a variable the computations will be
6387 done in the proper mode. */
6388 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6389 convert (index_type, size),
6390 convert (index_type,
6391 size_one_node));
6392
6393 /* The above overflows when size does not fit
6394 in index_type.
6395 ??? While a size of INT_MAX+1 technically shouldn't
6396 cause an overflow (because we subtract 1), handling
6397 this case seems like an unnecessary complication. */
6398 if (TREE_CODE (size) == INTEGER_CST
6399 && !int_fits_type_p (size, index_type))
6400 {
6401 if (name)
6402 error_at (loc, "size of array %qE is too large",
6403 name);
6404 else
6405 error_at (loc, "size of unnamed array is too large");
6406 type = error_mark_node;
6407 continue;
6408 }
6409
6410 itype = build_index_type (itype);
6411 }
6412 if (this_size_varies)
6413 {
6414 if (*expr)
6415 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6416 *expr, size);
6417 else
6418 *expr = size;
6419 *expr_const_operands &= size_maybe_const;
6420 }
6421 }
6422 else if (decl_context == FIELD)
6423 {
6424 bool flexible_array_member = false;
6425 if (array_parm_vla_unspec_p)
6426 /* Field names can in fact have function prototype
6427 scope so [*] is disallowed here through making
6428 the field variably modified, not through being
6429 something other than a declaration with function
6430 prototype scope. */
6431 size_varies = true;
6432 else
6433 {
6434 const struct c_declarator *t = declarator;
6435 while (t->kind == cdk_attrs)
6436 t = t->declarator;
6437 flexible_array_member = (t->kind == cdk_id);
6438 }
6439 if (flexible_array_member
6440 && !in_system_header_at (input_location))
6441 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6442 "support flexible array members");
6443
6444 /* ISO C99 Flexible array members are effectively
6445 identical to GCC's zero-length array extension. */
6446 if (flexible_array_member || array_parm_vla_unspec_p)
6447 itype = build_range_type (sizetype, size_zero_node,
6448 NULL_TREE);
6449 }
6450 else if (decl_context == PARM)
6451 {
6452 if (array_parm_vla_unspec_p)
6453 {
6454 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6455 size_varies = true;
6456 }
6457 }
6458 else if (decl_context == TYPENAME)
6459 {
6460 if (array_parm_vla_unspec_p)
6461 {
6462 /* C99 6.7.5.2p4 */
6463 warning (0, "%<[*]%> not in a declaration");
6464 /* We use this to avoid messing up with incomplete
6465 array types of the same type, that would
6466 otherwise be modified below. */
6467 itype = build_range_type (sizetype, size_zero_node,
6468 NULL_TREE);
6469 size_varies = true;
6470 }
6471 }
6472
6473 /* Complain about arrays of incomplete types. */
6474 if (!COMPLETE_TYPE_P (type))
6475 {
6476 error_at (loc, "array type has incomplete element type %qT",
6477 type);
6478 /* See if we can be more helpful. */
6479 if (TREE_CODE (type) == ARRAY_TYPE)
6480 {
6481 if (name)
6482 inform (loc, "declaration of %qE as multidimensional "
6483 "array must have bounds for all dimensions "
6484 "except the first", name);
6485 else
6486 inform (loc, "declaration of multidimensional array "
6487 "must have bounds for all dimensions except "
6488 "the first");
6489 }
6490 type = error_mark_node;
6491 }
6492 else
6493 /* When itype is NULL, a shared incomplete array type is
6494 returned for all array of a given type. Elsewhere we
6495 make sure we don't complete that type before copying
6496 it, but here we want to make sure we don't ever
6497 modify the shared type, so we gcc_assert (itype)
6498 below. */
6499 {
6500 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6501 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6502 type = build_qualified_type (type,
6503 ENCODE_QUAL_ADDR_SPACE (as));
6504
6505 type = build_array_type (type, itype);
6506 }
6507
6508 if (type != error_mark_node)
6509 {
6510 if (size_varies)
6511 {
6512 /* It is ok to modify type here even if itype is
6513 NULL: if size_varies, we're in a
6514 multi-dimensional array and the inner type has
6515 variable size, so the enclosing shared array type
6516 must too. */
6517 if (size && TREE_CODE (size) == INTEGER_CST)
6518 type
6519 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6520 C_TYPE_VARIABLE_SIZE (type) = 1;
6521 }
6522
6523 /* The GCC extension for zero-length arrays differs from
6524 ISO flexible array members in that sizeof yields
6525 zero. */
6526 if (size && integer_zerop (size))
6527 {
6528 gcc_assert (itype);
6529 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6530 TYPE_SIZE (type) = bitsize_zero_node;
6531 TYPE_SIZE_UNIT (type) = size_zero_node;
6532 SET_TYPE_STRUCTURAL_EQUALITY (type);
6533 }
6534 if (array_parm_vla_unspec_p)
6535 {
6536 gcc_assert (itype);
6537 /* The type is complete. C99 6.7.5.2p4 */
6538 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6539 TYPE_SIZE (type) = bitsize_zero_node;
6540 TYPE_SIZE_UNIT (type) = size_zero_node;
6541 SET_TYPE_STRUCTURAL_EQUALITY (type);
6542 }
6543
6544 if (!valid_array_size_p (loc, type, name))
6545 type = error_mark_node;
6546 }
6547
6548 if (decl_context != PARM
6549 && (array_ptr_quals != TYPE_UNQUALIFIED
6550 || array_ptr_attrs != NULL_TREE
6551 || array_parm_static))
6552 {
6553 error_at (loc, "static or type qualifiers in non-parameter "
6554 "array declarator");
6555 array_ptr_quals = TYPE_UNQUALIFIED;
6556 array_ptr_attrs = NULL_TREE;
6557 array_parm_static = false;
6558 }
6559 orig_qual_indirect++;
6560 break;
6561 }
6562 case cdk_function:
6563 {
6564 /* Say it's a definition only for the declarator closest
6565 to the identifier, apart possibly from some
6566 attributes. */
6567 bool really_funcdef = false;
6568 tree arg_types;
6569 orig_qual_type = NULL_TREE;
6570 if (funcdef_flag)
6571 {
6572 const struct c_declarator *t = declarator->declarator;
6573 while (t->kind == cdk_attrs)
6574 t = t->declarator;
6575 really_funcdef = (t->kind == cdk_id);
6576 }
6577
6578 /* Declaring a function type. Make sure we have a valid
6579 type for the function to return. */
6580 if (type == error_mark_node)
6581 continue;
6582
6583 size_varies = false;
6584
6585 /* Warn about some types functions can't return. */
6586 if (TREE_CODE (type) == FUNCTION_TYPE)
6587 {
6588 if (name)
6589 error_at (loc, "%qE declared as function returning a "
6590 "function", name);
6591 else
6592 error_at (loc, "type name declared as function "
6593 "returning a function");
6594 type = integer_type_node;
6595 }
6596 if (TREE_CODE (type) == ARRAY_TYPE)
6597 {
6598 if (name)
6599 error_at (loc, "%qE declared as function returning an array",
6600 name);
6601 else
6602 error_at (loc, "type name declared as function returning "
6603 "an array");
6604 type = integer_type_node;
6605 }
6606
6607 /* Construct the function type and go to the next
6608 inner layer of declarator. */
6609 arg_info = declarator->u.arg_info;
6610 arg_types = grokparms (arg_info, really_funcdef);
6611
6612 /* Type qualifiers before the return type of the function
6613 qualify the return type, not the function type. */
6614 if (type_quals)
6615 {
6616 const enum c_declspec_word ignored_quals_list[] =
6617 {
6618 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
6619 cdw_atomic, cdw_number_of_elements
6620 };
6621 location_t specs_loc
6622 = smallest_type_quals_location (declspecs->locations,
6623 ignored_quals_list);
6624 if (specs_loc == UNKNOWN_LOCATION)
6625 specs_loc = declspecs->locations[cdw_typedef];
6626 if (specs_loc == UNKNOWN_LOCATION)
6627 specs_loc = loc;
6628
6629 /* Type qualifiers on a function return type are
6630 normally permitted by the standard but have no
6631 effect, so give a warning at -Wreturn-type.
6632 Qualifiers on a void return type are banned on
6633 function definitions in ISO C; GCC used to used
6634 them for noreturn functions. The resolution of C11
6635 DR#423 means qualifiers (other than _Atomic) are
6636 actually removed from the return type when
6637 determining the function type. */
6638 int quals_used = type_quals;
6639 if (flag_isoc11)
6640 quals_used &= TYPE_QUAL_ATOMIC;
6641 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6642 pedwarn (specs_loc, 0,
6643 "function definition has qualified void "
6644 "return type");
6645 else
6646 warning_at (specs_loc, OPT_Wignored_qualifiers,
6647 "type qualifiers ignored on function "
6648 "return type");
6649
6650 /* Ensure an error for restrict on invalid types; the
6651 DR#423 resolution is not entirely clear about
6652 this. */
6653 if (flag_isoc11
6654 && (type_quals & TYPE_QUAL_RESTRICT)
6655 && (!POINTER_TYPE_P (type)
6656 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6657 error_at (loc, "invalid use of %<restrict%>");
6658 type = c_build_qualified_type (type, quals_used);
6659 }
6660 type_quals = TYPE_UNQUALIFIED;
6661
6662 type = build_function_type (type, arg_types);
6663 declarator = declarator->declarator;
6664
6665 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6666 the formal parameter list of this FUNCTION_TYPE to point to
6667 the FUNCTION_TYPE node itself. */
6668 {
6669 c_arg_tag *tag;
6670 unsigned ix;
6671
6672 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6673 TYPE_CONTEXT (tag->type) = type;
6674 }
6675 break;
6676 }
6677 case cdk_pointer:
6678 {
6679 /* Merge any constancy or volatility into the target type
6680 for the pointer. */
6681 if ((type_quals & TYPE_QUAL_ATOMIC)
6682 && TREE_CODE (type) == FUNCTION_TYPE)
6683 {
6684 error_at (loc,
6685 "%<_Atomic%>-qualified function type");
6686 type_quals &= ~TYPE_QUAL_ATOMIC;
6687 }
6688 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6689 && type_quals)
6690 pedwarn (loc, OPT_Wpedantic,
6691 "ISO C forbids qualified function types");
6692 if (type_quals)
6693 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6694 orig_qual_indirect);
6695 orig_qual_type = NULL_TREE;
6696 size_varies = false;
6697
6698 /* When the pointed-to type involves components of variable size,
6699 care must be taken to ensure that the size evaluation code is
6700 emitted early enough to dominate all the possible later uses
6701 and late enough for the variables on which it depends to have
6702 been assigned.
6703
6704 This is expected to happen automatically when the pointed-to
6705 type has a name/declaration of it's own, but special attention
6706 is required if the type is anonymous.
6707
6708 We attach an artificial TYPE_DECL to such pointed-to type
6709 and arrange for it to be included in a DECL_EXPR. This
6710 forces the sizes evaluation at a safe point and ensures it
6711 is not deferred until e.g. within a deeper conditional context.
6712
6713 PARM contexts have no enclosing statement list that
6714 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6715 instead, and add it to the list of expressions that
6716 need to be evaluated.
6717
6718 TYPENAME contexts do have an enclosing statement list,
6719 but it would be incorrect to use it, as the size should
6720 only be evaluated if the containing expression is
6721 evaluated. We might also be in the middle of an
6722 expression with side effects on the pointed-to type size
6723 "arguments" prior to the pointer declaration point and
6724 the fake TYPE_DECL in the enclosing context would force
6725 the size evaluation prior to the side effects. We therefore
6726 use BIND_EXPRs in TYPENAME contexts too. */
6727 if (!TYPE_NAME (type)
6728 && variably_modified_type_p (type, NULL_TREE))
6729 {
6730 tree bind = NULL_TREE;
6731 if (decl_context == TYPENAME || decl_context == PARM)
6732 {
6733 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
6734 NULL_TREE, NULL_TREE);
6735 TREE_SIDE_EFFECTS (bind) = 1;
6736 BIND_EXPR_BODY (bind) = push_stmt_list ();
6737 push_scope ();
6738 }
6739 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6740 DECL_ARTIFICIAL (decl) = 1;
6741 pushdecl (decl);
6742 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6743 TYPE_NAME (type) = decl;
6744 if (bind)
6745 {
6746 pop_scope ();
6747 BIND_EXPR_BODY (bind)
6748 = pop_stmt_list (BIND_EXPR_BODY (bind));
6749 if (*expr)
6750 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
6751 bind);
6752 else
6753 *expr = bind;
6754 }
6755 }
6756
6757 type = c_build_pointer_type (type);
6758
6759 /* Process type qualifiers (such as const or volatile)
6760 that were given inside the `*'. */
6761 type_quals = declarator->u.pointer_quals;
6762
6763 declarator = declarator->declarator;
6764 break;
6765 }
6766 default:
6767 gcc_unreachable ();
6768 }
6769 }
6770 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6771
6772 /* Now TYPE has the actual type, apart from any qualifiers in
6773 TYPE_QUALS. */
6774
6775 /* Warn about address space used for things other than static memory or
6776 pointers. */
6777 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6778 if (!ADDR_SPACE_GENERIC_P (address_space))
6779 {
6780 if (decl_context == NORMAL)
6781 {
6782 switch (storage_class)
6783 {
6784 case csc_auto:
6785 error ("%qs combined with %<auto%> qualifier for %qE",
6786 c_addr_space_name (address_space), name);
6787 break;
6788 case csc_register:
6789 error ("%qs combined with %<register%> qualifier for %qE",
6790 c_addr_space_name (address_space), name);
6791 break;
6792 case csc_none:
6793 if (current_function_scope)
6794 {
6795 error ("%qs specified for auto variable %qE",
6796 c_addr_space_name (address_space), name);
6797 break;
6798 }
6799 break;
6800 case csc_static:
6801 case csc_extern:
6802 case csc_typedef:
6803 break;
6804 default:
6805 gcc_unreachable ();
6806 }
6807 }
6808 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6809 {
6810 if (name)
6811 error ("%qs specified for parameter %qE",
6812 c_addr_space_name (address_space), name);
6813 else
6814 error ("%qs specified for unnamed parameter",
6815 c_addr_space_name (address_space));
6816 }
6817 else if (decl_context == FIELD)
6818 {
6819 if (name)
6820 error ("%qs specified for structure field %qE",
6821 c_addr_space_name (address_space), name);
6822 else
6823 error ("%qs specified for structure field",
6824 c_addr_space_name (address_space));
6825 }
6826 }
6827
6828 /* Check the type and width of a bit-field. */
6829 if (bitfield)
6830 {
6831 check_bitfield_type_and_width (loc, &type, width, name);
6832 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6833 atomic types are permitted for bit-fields; we have no code to
6834 make bit-field accesses atomic, so disallow them. */
6835 if (type_quals & TYPE_QUAL_ATOMIC)
6836 {
6837 if (name)
6838 error_at (loc, "bit-field %qE has atomic type", name);
6839 else
6840 error_at (loc, "bit-field has atomic type");
6841 type_quals &= ~TYPE_QUAL_ATOMIC;
6842 }
6843 }
6844
6845 /* Reject invalid uses of _Alignas. */
6846 if (declspecs->alignas_p)
6847 {
6848 if (storage_class == csc_typedef)
6849 error_at (loc, "alignment specified for typedef %qE", name);
6850 else if (storage_class == csc_register)
6851 error_at (loc, "alignment specified for %<register%> object %qE",
6852 name);
6853 else if (decl_context == PARM)
6854 {
6855 if (name)
6856 error_at (loc, "alignment specified for parameter %qE", name);
6857 else
6858 error_at (loc, "alignment specified for unnamed parameter");
6859 }
6860 else if (bitfield)
6861 {
6862 if (name)
6863 error_at (loc, "alignment specified for bit-field %qE", name);
6864 else
6865 error_at (loc, "alignment specified for unnamed bit-field");
6866 }
6867 else if (TREE_CODE (type) == FUNCTION_TYPE)
6868 error_at (loc, "alignment specified for function %qE", name);
6869 else if (declspecs->align_log != -1 && TYPE_P (type))
6870 {
6871 alignas_align = 1U << declspecs->align_log;
6872 if (alignas_align < min_align_of_type (type))
6873 {
6874 if (name)
6875 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6876 "alignment of %qE", name);
6877 else
6878 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6879 "alignment of unnamed field");
6880 alignas_align = 0;
6881 }
6882 }
6883 }
6884
6885 /* If this is declaring a typedef name, return a TYPE_DECL. */
6886
6887 if (storage_class == csc_typedef)
6888 {
6889 tree decl;
6890 if ((type_quals & TYPE_QUAL_ATOMIC)
6891 && TREE_CODE (type) == FUNCTION_TYPE)
6892 {
6893 error_at (loc,
6894 "%<_Atomic%>-qualified function type");
6895 type_quals &= ~TYPE_QUAL_ATOMIC;
6896 }
6897 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6898 && type_quals)
6899 pedwarn (loc, OPT_Wpedantic,
6900 "ISO C forbids qualified function types");
6901 if (type_quals)
6902 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6903 orig_qual_indirect);
6904 decl = build_decl (declarator->id_loc,
6905 TYPE_DECL, declarator->u.id, type);
6906 if (declspecs->explicit_signed_p)
6907 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6908 if (declspecs->inline_p)
6909 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6910 if (declspecs->noreturn_p)
6911 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6912
6913 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6914 {
6915 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6916
6917 if (b != NULL
6918 && b->decl != NULL_TREE
6919 && (B_IN_CURRENT_SCOPE (b)
6920 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6921 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6922 {
6923 auto_diagnostic_group d;
6924 if (warning_at (declarator->id_loc, OPT_Wc___compat,
6925 ("using %qD as both a typedef and a tag is "
6926 "invalid in C++"), decl)
6927 && b->locus != UNKNOWN_LOCATION)
6928 inform (b->locus, "originally defined here");
6929 }
6930 }
6931
6932 return decl;
6933 }
6934
6935 /* If this is a type name (such as, in a cast or sizeof),
6936 compute the type and return it now. */
6937
6938 if (decl_context == TYPENAME)
6939 {
6940 /* Note that the grammar rejects storage classes in typenames
6941 and fields. */
6942 gcc_assert (storage_class == csc_none && !threadp
6943 && !declspecs->inline_p && !declspecs->noreturn_p);
6944 if ((type_quals & TYPE_QUAL_ATOMIC)
6945 && TREE_CODE (type) == FUNCTION_TYPE)
6946 {
6947 error_at (loc,
6948 "%<_Atomic%>-qualified function type");
6949 type_quals &= ~TYPE_QUAL_ATOMIC;
6950 }
6951 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6952 && type_quals)
6953 pedwarn (loc, OPT_Wpedantic,
6954 "ISO C forbids const or volatile function types");
6955 if (type_quals)
6956 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6957 orig_qual_indirect);
6958 return type;
6959 }
6960
6961 if (pedantic && decl_context == FIELD
6962 && variably_modified_type_p (type, NULL_TREE))
6963 {
6964 /* C99 6.7.2.1p8 */
6965 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6966 "have a variably modified type");
6967 }
6968
6969 /* Aside from typedefs and type names (handle above),
6970 `void' at top level (not within pointer)
6971 is allowed only in public variables.
6972 We don't complain about parms either, but that is because
6973 a better error message can be made later. */
6974
6975 if (VOID_TYPE_P (type) && decl_context != PARM
6976 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6977 && (storage_class == csc_extern
6978 || (current_scope == file_scope
6979 && !(storage_class == csc_static
6980 || storage_class == csc_register)))))
6981 {
6982 error_at (loc, "variable or field %qE declared void", name);
6983 type = integer_type_node;
6984 }
6985
6986 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6987 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6988
6989 {
6990 tree decl;
6991
6992 if (decl_context == PARM)
6993 {
6994 tree promoted_type;
6995 bool array_parameter_p = false;
6996
6997 /* A parameter declared as an array of T is really a pointer to T.
6998 One declared as a function is really a pointer to a function. */
6999
7000 if (TREE_CODE (type) == ARRAY_TYPE)
7001 {
7002 /* Transfer const-ness of array into that of type pointed to. */
7003 type = TREE_TYPE (type);
7004 if (orig_qual_type != NULL_TREE)
7005 {
7006 if (orig_qual_indirect == 0)
7007 orig_qual_type = TREE_TYPE (orig_qual_type);
7008 else
7009 orig_qual_indirect--;
7010 }
7011 if (type_quals)
7012 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7013 orig_qual_indirect);
7014 type = c_build_pointer_type (type);
7015 type_quals = array_ptr_quals;
7016 if (type_quals)
7017 type = c_build_qualified_type (type, type_quals);
7018
7019 /* We don't yet implement attributes in this context. */
7020 if (array_ptr_attrs != NULL_TREE)
7021 warning_at (loc, OPT_Wattributes,
7022 "attributes in parameter array declarator ignored");
7023
7024 size_varies = false;
7025 array_parameter_p = true;
7026 }
7027 else if (TREE_CODE (type) == FUNCTION_TYPE)
7028 {
7029 if (type_quals & TYPE_QUAL_ATOMIC)
7030 {
7031 error_at (loc,
7032 "%<_Atomic%>-qualified function type");
7033 type_quals &= ~TYPE_QUAL_ATOMIC;
7034 }
7035 else if (type_quals)
7036 pedwarn (loc, OPT_Wpedantic,
7037 "ISO C forbids qualified function types");
7038 if (type_quals)
7039 type = c_build_qualified_type (type, type_quals);
7040 type = c_build_pointer_type (type);
7041 type_quals = TYPE_UNQUALIFIED;
7042 }
7043 else if (type_quals)
7044 type = c_build_qualified_type (type, type_quals);
7045
7046 decl = build_decl (declarator->id_loc,
7047 PARM_DECL, declarator->u.id, type);
7048 if (size_varies)
7049 C_DECL_VARIABLE_SIZE (decl) = 1;
7050 C_ARRAY_PARAMETER (decl) = array_parameter_p;
7051
7052 /* Compute the type actually passed in the parmlist,
7053 for the case where there is no prototype.
7054 (For example, shorts and chars are passed as ints.)
7055 When there is a prototype, this is overridden later. */
7056
7057 if (type == error_mark_node)
7058 promoted_type = type;
7059 else
7060 promoted_type = c_type_promotes_to (type);
7061
7062 DECL_ARG_TYPE (decl) = promoted_type;
7063 if (declspecs->inline_p)
7064 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
7065 if (declspecs->noreturn_p)
7066 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
7067 }
7068 else if (decl_context == FIELD)
7069 {
7070 /* Note that the grammar rejects storage classes in typenames
7071 and fields. */
7072 gcc_assert (storage_class == csc_none && !threadp
7073 && !declspecs->inline_p && !declspecs->noreturn_p);
7074
7075 /* Structure field. It may not be a function. */
7076
7077 if (TREE_CODE (type) == FUNCTION_TYPE)
7078 {
7079 error_at (loc, "field %qE declared as a function", name);
7080 type = build_pointer_type (type);
7081 }
7082 else if (TREE_CODE (type) != ERROR_MARK
7083 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
7084 {
7085 if (name)
7086 error_at (loc, "field %qE has incomplete type", name);
7087 else
7088 error_at (loc, "unnamed field has incomplete type");
7089 type = error_mark_node;
7090 }
7091 else if (TREE_CODE (type) == ARRAY_TYPE
7092 && TYPE_DOMAIN (type) == NULL_TREE)
7093 {
7094 /* We have a flexible array member through a typedef.
7095 Set suitable range. Whether this is a correct position
7096 for a flexible array member will be determined elsewhere. */
7097 if (!in_system_header_at (input_location))
7098 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7099 "support flexible array members");
7100 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7101 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
7102 NULL_TREE);
7103 if (orig_qual_indirect == 0)
7104 orig_qual_type = NULL_TREE;
7105 }
7106 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7107 orig_qual_indirect);
7108 decl = build_decl (declarator->id_loc,
7109 FIELD_DECL, declarator->u.id, type);
7110 DECL_NONADDRESSABLE_P (decl) = bitfield;
7111 if (bitfield && !declarator->u.id)
7112 {
7113 TREE_NO_WARNING (decl) = 1;
7114 DECL_PADDING_P (decl) = 1;
7115 }
7116
7117 if (size_varies)
7118 C_DECL_VARIABLE_SIZE (decl) = 1;
7119 }
7120 else if (TREE_CODE (type) == FUNCTION_TYPE)
7121 {
7122 if (storage_class == csc_register || threadp)
7123 {
7124 error_at (loc, "invalid storage class for function %qE", name);
7125 }
7126 else if (current_scope != file_scope)
7127 {
7128 /* Function declaration not at file scope. Storage
7129 classes other than `extern' are not allowed, C99
7130 6.7.1p5, and `extern' makes no difference. However,
7131 GCC allows 'auto', perhaps with 'inline', to support
7132 nested functions. */
7133 if (storage_class == csc_auto)
7134 pedwarn (loc, OPT_Wpedantic,
7135 "invalid storage class for function %qE", name);
7136 else if (storage_class == csc_static)
7137 {
7138 error_at (loc, "invalid storage class for function %qE", name);
7139 if (funcdef_flag)
7140 storage_class = declspecs->storage_class = csc_none;
7141 else
7142 return NULL_TREE;
7143 }
7144 }
7145
7146 decl = build_decl (declarator->id_loc,
7147 FUNCTION_DECL, declarator->u.id, type);
7148 decl = build_decl_attribute_variant (decl, decl_attr);
7149
7150 if (type_quals & TYPE_QUAL_ATOMIC)
7151 {
7152 error_at (loc,
7153 "%<_Atomic%>-qualified function type");
7154 type_quals &= ~TYPE_QUAL_ATOMIC;
7155 }
7156 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
7157 pedwarn (loc, OPT_Wpedantic,
7158 "ISO C forbids qualified function types");
7159
7160 /* Every function declaration is an external reference
7161 (DECL_EXTERNAL) except for those which are not at file
7162 scope and are explicitly declared "auto". This is
7163 forbidden by standard C (C99 6.7.1p5) and is interpreted by
7164 GCC to signify a forward declaration of a nested function. */
7165 if (storage_class == csc_auto && current_scope != file_scope)
7166 DECL_EXTERNAL (decl) = 0;
7167 /* In C99, a function which is declared 'inline' with 'extern'
7168 is not an external reference (which is confusing). It
7169 means that the later definition of the function must be output
7170 in this file, C99 6.7.4p6. In GNU C89, a function declared
7171 'extern inline' is an external reference. */
7172 else if (declspecs->inline_p && storage_class != csc_static)
7173 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
7174 == flag_gnu89_inline);
7175 else
7176 DECL_EXTERNAL (decl) = !initialized;
7177
7178 /* Record absence of global scope for `static' or `auto'. */
7179 TREE_PUBLIC (decl)
7180 = !(storage_class == csc_static || storage_class == csc_auto);
7181
7182 /* For a function definition, record the argument information
7183 block where store_parm_decls will look for it. */
7184 if (funcdef_flag)
7185 current_function_arg_info = arg_info;
7186
7187 if (declspecs->default_int_p)
7188 C_FUNCTION_IMPLICIT_INT (decl) = 1;
7189
7190 /* Record presence of `inline' and `_Noreturn', if it is
7191 reasonable. */
7192 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
7193 {
7194 if (declspecs->inline_p)
7195 pedwarn (loc, 0, "cannot inline function %<main%>");
7196 if (declspecs->noreturn_p)
7197 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7198 }
7199 else
7200 {
7201 if (declspecs->inline_p)
7202 /* Record that the function is declared `inline'. */
7203 DECL_DECLARED_INLINE_P (decl) = 1;
7204 if (declspecs->noreturn_p)
7205 {
7206 if (flag_isoc99)
7207 pedwarn_c99 (loc, OPT_Wpedantic,
7208 "ISO C99 does not support %<_Noreturn%>");
7209 else
7210 pedwarn_c99 (loc, OPT_Wpedantic,
7211 "ISO C90 does not support %<_Noreturn%>");
7212 TREE_THIS_VOLATILE (decl) = 1;
7213 }
7214 }
7215 }
7216 else
7217 {
7218 /* It's a variable. */
7219 /* An uninitialized decl with `extern' is a reference. */
7220 int extern_ref = !initialized && storage_class == csc_extern;
7221
7222 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7223 orig_qual_indirect);
7224
7225 /* C99 6.2.2p7: It is invalid (compile-time undefined
7226 behavior) to create an 'extern' declaration for a
7227 variable if there is a global declaration that is
7228 'static' and the global declaration is not visible.
7229 (If the static declaration _is_ currently visible,
7230 the 'extern' declaration is taken to refer to that decl.) */
7231 if (extern_ref && current_scope != file_scope)
7232 {
7233 tree global_decl = identifier_global_value (declarator->u.id);
7234 tree visible_decl = lookup_name (declarator->u.id);
7235
7236 if (global_decl
7237 && global_decl != visible_decl
7238 && VAR_P (global_decl)
7239 && !TREE_PUBLIC (global_decl))
7240 error_at (loc, "variable previously declared %<static%> "
7241 "redeclared %<extern%>");
7242 }
7243
7244 decl = build_decl (declarator->id_loc,
7245 VAR_DECL, declarator->u.id, type);
7246 if (size_varies)
7247 C_DECL_VARIABLE_SIZE (decl) = 1;
7248
7249 if (declspecs->inline_p)
7250 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
7251 if (declspecs->noreturn_p)
7252 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7253
7254 /* At file scope, an initialized extern declaration may follow
7255 a static declaration. In that case, DECL_EXTERNAL will be
7256 reset later in start_decl. */
7257 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7258
7259 /* At file scope, the presence of a `static' or `register' storage
7260 class specifier, or the absence of all storage class specifiers
7261 makes this declaration a definition (perhaps tentative). Also,
7262 the absence of `static' makes it public. */
7263 if (current_scope == file_scope)
7264 {
7265 TREE_PUBLIC (decl) = storage_class != csc_static;
7266 TREE_STATIC (decl) = !extern_ref;
7267 }
7268 /* Not at file scope, only `static' makes a static definition. */
7269 else
7270 {
7271 TREE_STATIC (decl) = (storage_class == csc_static);
7272 TREE_PUBLIC (decl) = extern_ref;
7273 }
7274
7275 if (threadp)
7276 set_decl_tls_model (decl, decl_default_tls_model (decl));
7277 }
7278
7279 if ((storage_class == csc_extern
7280 || (storage_class == csc_none
7281 && TREE_CODE (type) == FUNCTION_TYPE
7282 && !funcdef_flag))
7283 && variably_modified_type_p (type, NULL_TREE))
7284 {
7285 /* C99 6.7.5.2p2 */
7286 if (TREE_CODE (type) == FUNCTION_TYPE)
7287 error_at (loc, "non-nested function with variably modified type");
7288 else
7289 error_at (loc, "object with variably modified type must have "
7290 "no linkage");
7291 }
7292
7293 /* Record `register' declaration for warnings on &
7294 and in case doing stupid register allocation. */
7295
7296 if (storage_class == csc_register)
7297 {
7298 C_DECL_REGISTER (decl) = 1;
7299 DECL_REGISTER (decl) = 1;
7300 }
7301
7302 /* Record constancy and volatility. */
7303 c_apply_type_quals_to_decl (type_quals, decl);
7304
7305 /* Apply _Alignas specifiers. */
7306 if (alignas_align)
7307 {
7308 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7309 DECL_USER_ALIGN (decl) = 1;
7310 }
7311
7312 /* If a type has volatile components, it should be stored in memory.
7313 Otherwise, the fact that those components are volatile
7314 will be ignored, and would even crash the compiler.
7315 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7316 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7317 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7318 || TREE_CODE (decl) == RESULT_DECL))
7319 {
7320 /* It is not an error for a structure with volatile fields to
7321 be declared register, but reset DECL_REGISTER since it
7322 cannot actually go in a register. */
7323 int was_reg = C_DECL_REGISTER (decl);
7324 C_DECL_REGISTER (decl) = 0;
7325 DECL_REGISTER (decl) = 0;
7326 c_mark_addressable (decl);
7327 C_DECL_REGISTER (decl) = was_reg;
7328 }
7329
7330 /* This is the earliest point at which we might know the assembler
7331 name of a variable. Thus, if it's known before this, die horribly. */
7332 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7333 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7334
7335 if (warn_cxx_compat
7336 && VAR_P (decl)
7337 && TREE_PUBLIC (decl)
7338 && TREE_STATIC (decl)
7339 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7340 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7341 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7342 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7343 ("non-local variable %qD with anonymous type is "
7344 "questionable in C++"),
7345 decl);
7346
7347 return decl;
7348 }
7349 }
7350 \f
7351 /* Decode the parameter-list info for a function type or function definition.
7352 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7353 if there is an identifier list instead of a parameter decl list).
7354 These two functions are separate because when a function returns
7355 or receives functions then each is called multiple times but the order
7356 of calls is different. The last call to `grokparms' is always the one
7357 that contains the formal parameter names of a function definition.
7358
7359 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7360
7361 FUNCDEF_FLAG is true for a function definition, false for
7362 a mere declaration. A nonempty identifier-list gets an error message
7363 when FUNCDEF_FLAG is false. */
7364
7365 static tree
7366 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7367 {
7368 tree arg_types = arg_info->types;
7369
7370 if (funcdef_flag && arg_info->had_vla_unspec)
7371 {
7372 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7373 /* C99 6.7.5.2p4 */
7374 error ("%<[*]%> not allowed in other than function prototype scope");
7375 }
7376
7377 if (arg_types == NULL_TREE && !funcdef_flag
7378 && !in_system_header_at (input_location))
7379 warning (OPT_Wstrict_prototypes,
7380 "function declaration isn%'t a prototype");
7381
7382 if (arg_types == error_mark_node)
7383 /* Don't set TYPE_ARG_TYPES in this case. */
7384 return NULL_TREE;
7385
7386 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7387 {
7388 if (!funcdef_flag)
7389 {
7390 pedwarn (input_location, 0, "parameter names (without types) in "
7391 "function declaration");
7392 arg_info->parms = NULL_TREE;
7393 }
7394 else
7395 arg_info->parms = arg_info->types;
7396
7397 arg_info->types = NULL_TREE;
7398 return NULL_TREE;
7399 }
7400 else
7401 {
7402 tree parm, type, typelt;
7403 unsigned int parmno;
7404
7405 /* If there is a parameter of incomplete type in a definition,
7406 this is an error. In a declaration this is valid, and a
7407 struct or union type may be completed later, before any calls
7408 or definition of the function. In the case where the tag was
7409 first declared within the parameter list, a warning has
7410 already been given. If a parameter has void type, then
7411 however the function cannot be defined or called, so
7412 warn. */
7413
7414 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7415 parm;
7416 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7417 {
7418 type = TREE_VALUE (typelt);
7419 if (type == error_mark_node)
7420 continue;
7421
7422 if (!COMPLETE_TYPE_P (type))
7423 {
7424 if (funcdef_flag)
7425 {
7426 if (DECL_NAME (parm))
7427 error_at (input_location,
7428 "parameter %u (%q+D) has incomplete type",
7429 parmno, parm);
7430 else
7431 error_at (DECL_SOURCE_LOCATION (parm),
7432 "parameter %u has incomplete type",
7433 parmno);
7434
7435 TREE_VALUE (typelt) = error_mark_node;
7436 TREE_TYPE (parm) = error_mark_node;
7437 arg_types = NULL_TREE;
7438 }
7439 else if (VOID_TYPE_P (type))
7440 {
7441 if (DECL_NAME (parm))
7442 warning_at (input_location, 0,
7443 "parameter %u (%q+D) has void type",
7444 parmno, parm);
7445 else
7446 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7447 "parameter %u has void type",
7448 parmno);
7449 }
7450 }
7451
7452 if (DECL_NAME (parm) && TREE_USED (parm))
7453 warn_if_shadowing (parm);
7454 }
7455 return arg_types;
7456 }
7457 }
7458
7459 /* Allocate and initialize a c_arg_info structure from the parser's
7460 obstack. */
7461
7462 struct c_arg_info *
7463 build_arg_info (void)
7464 {
7465 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7466 ret->parms = NULL_TREE;
7467 ret->tags = NULL;
7468 ret->types = NULL_TREE;
7469 ret->others = NULL_TREE;
7470 ret->pending_sizes = NULL;
7471 ret->had_vla_unspec = 0;
7472 return ret;
7473 }
7474
7475 /* Take apart the current scope and return a c_arg_info structure with
7476 info on a parameter list just parsed.
7477
7478 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7479
7480 ELLIPSIS being true means the argument list ended in '...' so don't
7481 append a sentinel (void_list_node) to the end of the type-list.
7482
7483 EXPR is NULL or an expression that needs to be evaluated for the
7484 side effects of array size expressions in the parameters. */
7485
7486 struct c_arg_info *
7487 get_parm_info (bool ellipsis, tree expr)
7488 {
7489 struct c_binding *b = current_scope->bindings;
7490 struct c_arg_info *arg_info = build_arg_info ();
7491
7492 tree parms = NULL_TREE;
7493 vec<c_arg_tag, va_gc> *tags = NULL;
7494 tree types = NULL_TREE;
7495 tree others = NULL_TREE;
7496
7497 bool gave_void_only_once_err = false;
7498
7499 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7500
7501 /* The bindings in this scope must not get put into a block.
7502 We will take care of deleting the binding nodes. */
7503 current_scope->bindings = 0;
7504
7505 /* This function is only called if there was *something* on the
7506 parameter list. */
7507 gcc_assert (b);
7508
7509 /* A parameter list consisting solely of 'void' indicates that the
7510 function takes no arguments. But if the 'void' is qualified
7511 (by 'const' or 'volatile'), or has a storage class specifier
7512 ('register'), then the behavior is undefined; issue an error.
7513 Typedefs for 'void' are OK (see DR#157). */
7514 if (b->prev == 0 /* one binding */
7515 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7516 && !DECL_NAME (b->decl) /* anonymous */
7517 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7518 {
7519 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7520 || C_DECL_REGISTER (b->decl))
7521 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7522
7523 /* There cannot be an ellipsis. */
7524 if (ellipsis)
7525 error_at (b->locus, "%<void%> must be the only parameter");
7526
7527 arg_info->types = void_list_node;
7528 return arg_info;
7529 }
7530
7531 if (!ellipsis)
7532 types = void_list_node;
7533
7534 /* Break up the bindings list into parms, tags, types, and others;
7535 apply sanity checks; purge the name-to-decl bindings. */
7536 while (b)
7537 {
7538 tree decl = b->decl;
7539 tree type = TREE_TYPE (decl);
7540 c_arg_tag tag;
7541 const char *keyword;
7542
7543 switch (TREE_CODE (decl))
7544 {
7545 case PARM_DECL:
7546 if (b->id)
7547 {
7548 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7549 I_SYMBOL_BINDING (b->id) = b->shadowed;
7550 }
7551
7552 /* Check for forward decls that never got their actual decl. */
7553 if (TREE_ASM_WRITTEN (decl))
7554 error_at (b->locus,
7555 "parameter %q+D has just a forward declaration", decl);
7556 /* Check for (..., void, ...) and issue an error. */
7557 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7558 {
7559 if (!gave_void_only_once_err)
7560 {
7561 error_at (b->locus, "%<void%> must be the only parameter");
7562 gave_void_only_once_err = true;
7563 }
7564 }
7565 else
7566 {
7567 /* Valid parameter, add it to the list. */
7568 DECL_CHAIN (decl) = parms;
7569 parms = decl;
7570
7571 /* Since there is a prototype, args are passed in their
7572 declared types. The back end may override this later. */
7573 DECL_ARG_TYPE (decl) = type;
7574 types = tree_cons (0, type, types);
7575 }
7576 break;
7577
7578 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7579 case UNION_TYPE: keyword = "union"; goto tag;
7580 case RECORD_TYPE: keyword = "struct"; goto tag;
7581 tag:
7582 /* Types may not have tag-names, in which case the type
7583 appears in the bindings list with b->id NULL. */
7584 if (b->id)
7585 {
7586 gcc_assert (I_TAG_BINDING (b->id) == b);
7587 I_TAG_BINDING (b->id) = b->shadowed;
7588 }
7589
7590 /* Warn about any struct, union or enum tags defined in a
7591 parameter list. The scope of such types is limited to
7592 the parameter list, which is rarely if ever desirable
7593 (it's impossible to call such a function with type-
7594 correct arguments). An anonymous union parm type is
7595 meaningful as a GNU extension, so don't warn for that. */
7596 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
7597 {
7598 if (b->id)
7599 /* The %s will be one of 'struct', 'union', or 'enum'. */
7600 warning_at (b->locus, 0,
7601 "%<%s %E%> declared inside parameter list"
7602 " will not be visible outside of this definition or"
7603 " declaration", keyword, b->id);
7604 else
7605 /* The %s will be one of 'struct', 'union', or 'enum'. */
7606 warning_at (b->locus, 0,
7607 "anonymous %s declared inside parameter list"
7608 " will not be visible outside of this definition or"
7609 " declaration", keyword);
7610 }
7611
7612 tag.id = b->id;
7613 tag.type = decl;
7614 vec_safe_push (tags, tag);
7615 break;
7616
7617 case FUNCTION_DECL:
7618 /* FUNCTION_DECLs appear when there is an implicit function
7619 declaration in the parameter list. */
7620 gcc_assert (b->nested || seen_error ());
7621 goto set_shadowed;
7622
7623 case CONST_DECL:
7624 case TYPE_DECL:
7625 /* CONST_DECLs appear here when we have an embedded enum,
7626 and TYPE_DECLs appear here when we have an embedded struct
7627 or union. No warnings for this - we already warned about the
7628 type itself. */
7629
7630 /* When we reinsert this decl in the function body, we need
7631 to reconstruct whether it was marked as nested. */
7632 gcc_assert (!b->nested);
7633 DECL_CHAIN (decl) = others;
7634 others = decl;
7635 /* fall through */
7636
7637 case ERROR_MARK:
7638 set_shadowed:
7639 /* error_mark_node appears here when we have an undeclared
7640 variable. Just throw it away. */
7641 if (b->id)
7642 {
7643 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7644 I_SYMBOL_BINDING (b->id) = b->shadowed;
7645 }
7646 break;
7647
7648 /* Other things that might be encountered. */
7649 case LABEL_DECL:
7650 case VAR_DECL:
7651 default:
7652 gcc_unreachable ();
7653 }
7654
7655 b = free_binding_and_advance (b);
7656 }
7657
7658 arg_info->parms = parms;
7659 arg_info->tags = tags;
7660 arg_info->types = types;
7661 arg_info->others = others;
7662 arg_info->pending_sizes = expr;
7663 return arg_info;
7664 }
7665 \f
7666 /* Get the struct, enum or union (CODE says which) with tag NAME.
7667 Define the tag as a forward-reference with location LOC if it is
7668 not defined. Return a c_typespec structure for the type
7669 specifier. */
7670
7671 struct c_typespec
7672 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7673 {
7674 struct c_typespec ret;
7675 tree ref;
7676 location_t refloc;
7677
7678 ret.expr = NULL_TREE;
7679 ret.expr_const_operands = true;
7680
7681 /* If a cross reference is requested, look up the type
7682 already defined for this tag and return it. */
7683
7684 ref = lookup_tag (code, name, false, &refloc);
7685 /* If this is the right type of tag, return what we found.
7686 (This reference will be shadowed by shadow_tag later if appropriate.)
7687 If this is the wrong type of tag, do not return it. If it was the
7688 wrong type in the same scope, we will have had an error
7689 message already; if in a different scope and declaring
7690 a name, pending_xref_error will give an error message; but if in a
7691 different scope and not declaring a name, this tag should
7692 shadow the previous declaration of a different type of tag, and
7693 this would not work properly if we return the reference found.
7694 (For example, with "struct foo" in an outer scope, "union foo;"
7695 must shadow that tag with a new one of union type.) */
7696 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7697 if (ref && TREE_CODE (ref) == code)
7698 {
7699 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7700 && loc != UNKNOWN_LOCATION
7701 && warn_cxx_compat)
7702 {
7703 switch (code)
7704 {
7705 case ENUMERAL_TYPE:
7706 warning_at (loc, OPT_Wc___compat,
7707 ("enum type defined in struct or union "
7708 "is not visible in C++"));
7709 inform (refloc, "enum type defined here");
7710 break;
7711 case RECORD_TYPE:
7712 warning_at (loc, OPT_Wc___compat,
7713 ("struct defined in struct or union "
7714 "is not visible in C++"));
7715 inform (refloc, "struct defined here");
7716 break;
7717 case UNION_TYPE:
7718 warning_at (loc, OPT_Wc___compat,
7719 ("union defined in struct or union "
7720 "is not visible in C++"));
7721 inform (refloc, "union defined here");
7722 break;
7723 default:
7724 gcc_unreachable();
7725 }
7726 }
7727
7728 ret.spec = ref;
7729 return ret;
7730 }
7731
7732 /* If no such tag is yet defined, create a forward-reference node
7733 and record it as the "definition".
7734 When a real declaration of this type is found,
7735 the forward-reference will be altered into a real type. */
7736
7737 ref = make_node (code);
7738 if (code == ENUMERAL_TYPE)
7739 {
7740 /* Give the type a default layout like unsigned int
7741 to avoid crashing if it does not get defined. */
7742 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7743 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7744 TYPE_USER_ALIGN (ref) = 0;
7745 TYPE_UNSIGNED (ref) = 1;
7746 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7747 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7748 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7749 }
7750
7751 pushtag (loc, name, ref);
7752
7753 ret.spec = ref;
7754 return ret;
7755 }
7756
7757 /* Get the struct, enum or union (CODE says which) with tag NAME.
7758 Define the tag as a forward-reference if it is not defined.
7759 Return a tree for the type. */
7760
7761 tree
7762 xref_tag (enum tree_code code, tree name)
7763 {
7764 return parser_xref_tag (input_location, code, name).spec;
7765 }
7766 \f
7767 /* Make sure that the tag NAME is defined *in the current scope*
7768 at least as a forward reference.
7769 LOC is the location of the struct's definition.
7770 CODE says which kind of tag NAME ought to be.
7771
7772 This stores the current value of the file static STRUCT_PARSE_INFO
7773 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7774 new c_struct_parse_info structure. The old value of
7775 STRUCT_PARSE_INFO is restored in finish_struct. */
7776
7777 tree
7778 start_struct (location_t loc, enum tree_code code, tree name,
7779 class c_struct_parse_info **enclosing_struct_parse_info)
7780 {
7781 /* If there is already a tag defined at this scope
7782 (as a forward reference), just return it. */
7783
7784 tree ref = NULL_TREE;
7785 location_t refloc = UNKNOWN_LOCATION;
7786
7787 if (name != NULL_TREE)
7788 ref = lookup_tag (code, name, true, &refloc);
7789 if (ref && TREE_CODE (ref) == code)
7790 {
7791 if (TYPE_STUB_DECL (ref))
7792 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
7793
7794 if (TYPE_SIZE (ref))
7795 {
7796 if (code == UNION_TYPE)
7797 error_at (loc, "redefinition of %<union %E%>", name);
7798 else
7799 error_at (loc, "redefinition of %<struct %E%>", name);
7800 if (refloc != UNKNOWN_LOCATION)
7801 inform (refloc, "originally defined here");
7802 /* Don't create structures using a name already in use. */
7803 ref = NULL_TREE;
7804 }
7805 else if (C_TYPE_BEING_DEFINED (ref))
7806 {
7807 if (code == UNION_TYPE)
7808 error_at (loc, "nested redefinition of %<union %E%>", name);
7809 else
7810 error_at (loc, "nested redefinition of %<struct %E%>", name);
7811 /* Don't bother to report "originally defined here" for a
7812 nested redefinition; the original definition should be
7813 obvious. */
7814 /* Don't create structures that contain themselves. */
7815 ref = NULL_TREE;
7816 }
7817 }
7818
7819 /* Otherwise create a forward-reference just so the tag is in scope. */
7820
7821 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7822 {
7823 ref = make_node (code);
7824 pushtag (loc, name, ref);
7825 }
7826
7827 C_TYPE_BEING_DEFINED (ref) = 1;
7828 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7829 TYPE_PACKED (v) = flag_pack_struct;
7830
7831 *enclosing_struct_parse_info = struct_parse_info;
7832 struct_parse_info = new c_struct_parse_info ();
7833
7834 /* FIXME: This will issue a warning for a use of a type defined
7835 within a statement expr used within sizeof, et. al. This is not
7836 terribly serious as C++ doesn't permit statement exprs within
7837 sizeof anyhow. */
7838 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7839 warning_at (loc, OPT_Wc___compat,
7840 "defining type in %qs expression is invalid in C++",
7841 (in_sizeof
7842 ? "sizeof"
7843 : (in_typeof ? "typeof" : "alignof")));
7844
7845 return ref;
7846 }
7847
7848 /* Process the specs, declarator and width (NULL if omitted)
7849 of a structure component, returning a FIELD_DECL node.
7850 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7851 DECL_ATTRS is as for grokdeclarator.
7852
7853 LOC is the location of the structure component.
7854
7855 This is done during the parsing of the struct declaration.
7856 The FIELD_DECL nodes are chained together and the lot of them
7857 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7858
7859 tree
7860 grokfield (location_t loc,
7861 struct c_declarator *declarator, struct c_declspecs *declspecs,
7862 tree width, tree *decl_attrs)
7863 {
7864 tree value;
7865
7866 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7867 && width == NULL_TREE)
7868 {
7869 /* This is an unnamed decl.
7870
7871 If we have something of the form "union { list } ;" then this
7872 is the anonymous union extension. Similarly for struct.
7873
7874 If this is something of the form "struct foo;", then
7875 If MS or Plan 9 extensions are enabled, this is handled as
7876 an anonymous struct.
7877 Otherwise this is a forward declaration of a structure tag.
7878
7879 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7880 If foo names a structure or union without a tag, then this
7881 is an anonymous struct (this is permitted by C11).
7882 If MS or Plan 9 extensions are enabled and foo names a
7883 structure, then again this is an anonymous struct.
7884 Otherwise this is an error.
7885
7886 Oh what a horrid tangled web we weave. I wonder if MS consciously
7887 took this from Plan 9 or if it was an accident of implementation
7888 that took root before someone noticed the bug... */
7889
7890 tree type = declspecs->type;
7891 bool ok = false;
7892
7893 if (RECORD_OR_UNION_TYPE_P (type)
7894 && (flag_ms_extensions
7895 || flag_plan9_extensions
7896 || !declspecs->typedef_p))
7897 {
7898 if (flag_ms_extensions || flag_plan9_extensions)
7899 ok = true;
7900 else if (TYPE_NAME (type) == NULL)
7901 ok = true;
7902 else
7903 ok = false;
7904 }
7905 if (!ok)
7906 {
7907 pedwarn (loc, 0, "declaration does not declare anything");
7908 return NULL_TREE;
7909 }
7910 if (flag_isoc99)
7911 pedwarn_c99 (loc, OPT_Wpedantic,
7912 "ISO C99 doesn%'t support unnamed structs/unions");
7913 else
7914 pedwarn_c99 (loc, OPT_Wpedantic,
7915 "ISO C90 doesn%'t support unnamed structs/unions");
7916 }
7917
7918 value = grokdeclarator (declarator, declspecs, FIELD, false,
7919 width ? &width : NULL, decl_attrs, NULL, NULL,
7920 DEPRECATED_NORMAL);
7921
7922 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7923 DECL_INITIAL (value) = width;
7924 if (width)
7925 SET_DECL_C_BIT_FIELD (value);
7926
7927 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7928 {
7929 /* If we currently have a binding for this field, set the
7930 in_struct field in the binding, so that we warn about lookups
7931 which find it. */
7932 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7933 if (b != NULL)
7934 {
7935 /* If the in_struct field is not yet set, push it on a list
7936 to be cleared when this struct is finished. */
7937 if (!b->in_struct)
7938 {
7939 struct_parse_info->fields.safe_push (b);
7940 b->in_struct = 1;
7941 }
7942 }
7943 }
7944
7945 return value;
7946 }
7947 \f
7948 /* Subroutine of detect_field_duplicates: return whether X and Y,
7949 which are both fields in the same struct, have duplicate field
7950 names. */
7951
7952 static bool
7953 is_duplicate_field (tree x, tree y)
7954 {
7955 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7956 return true;
7957
7958 /* When using -fplan9-extensions, an anonymous field whose name is a
7959 typedef can duplicate a field name. */
7960 if (flag_plan9_extensions
7961 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7962 {
7963 tree xt, xn, yt, yn;
7964
7965 xt = TREE_TYPE (x);
7966 if (DECL_NAME (x) != NULL_TREE)
7967 xn = DECL_NAME (x);
7968 else if (RECORD_OR_UNION_TYPE_P (xt)
7969 && TYPE_NAME (xt) != NULL_TREE
7970 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7971 xn = DECL_NAME (TYPE_NAME (xt));
7972 else
7973 xn = NULL_TREE;
7974
7975 yt = TREE_TYPE (y);
7976 if (DECL_NAME (y) != NULL_TREE)
7977 yn = DECL_NAME (y);
7978 else if (RECORD_OR_UNION_TYPE_P (yt)
7979 && TYPE_NAME (yt) != NULL_TREE
7980 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7981 yn = DECL_NAME (TYPE_NAME (yt));
7982 else
7983 yn = NULL_TREE;
7984
7985 if (xn != NULL_TREE && xn == yn)
7986 return true;
7987 }
7988
7989 return false;
7990 }
7991
7992 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7993 to HTAB, giving errors for any duplicates. */
7994
7995 static void
7996 detect_field_duplicates_hash (tree fieldlist,
7997 hash_table<nofree_ptr_hash <tree_node> > *htab)
7998 {
7999 tree x, y;
8000 tree_node **slot;
8001
8002 for (x = fieldlist; x ; x = DECL_CHAIN (x))
8003 if ((y = DECL_NAME (x)) != NULL_TREE)
8004 {
8005 slot = htab->find_slot (y, INSERT);
8006 if (*slot)
8007 {
8008 error ("duplicate member %q+D", x);
8009 DECL_NAME (x) = NULL_TREE;
8010 }
8011 *slot = y;
8012 }
8013 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8014 {
8015 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
8016
8017 /* When using -fplan9-extensions, an anonymous field whose
8018 name is a typedef can duplicate a field name. */
8019 if (flag_plan9_extensions
8020 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8021 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
8022 {
8023 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
8024 slot = htab->find_slot (xn, INSERT);
8025 if (*slot)
8026 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
8027 *slot = xn;
8028 }
8029 }
8030 }
8031
8032 /* Generate an error for any duplicate field names in FIELDLIST. Munge
8033 the list such that this does not present a problem later. */
8034
8035 static void
8036 detect_field_duplicates (tree fieldlist)
8037 {
8038 tree x, y;
8039 int timeout = 10;
8040
8041 /* If the struct is the list of instance variables of an Objective-C
8042 class, then we need to check all the instance variables of
8043 superclasses when checking for duplicates (since you can't have
8044 an instance variable in a subclass with the same name as an
8045 instance variable in a superclass). We pass on this job to the
8046 Objective-C compiler. objc_detect_field_duplicates() will return
8047 false if we are not checking the list of instance variables and
8048 the C frontend should proceed with the standard field duplicate
8049 checks. If we are checking the list of instance variables, the
8050 ObjC frontend will do the check, emit the errors if needed, and
8051 then return true. */
8052 if (c_dialect_objc ())
8053 if (objc_detect_field_duplicates (false))
8054 return;
8055
8056 /* First, see if there are more than "a few" fields.
8057 This is trivially true if there are zero or one fields. */
8058 if (!fieldlist || !DECL_CHAIN (fieldlist))
8059 return;
8060 x = fieldlist;
8061 do {
8062 timeout--;
8063 if (DECL_NAME (x) == NULL_TREE
8064 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8065 timeout = 0;
8066 x = DECL_CHAIN (x);
8067 } while (timeout > 0 && x);
8068
8069 /* If there were "few" fields and no anonymous structures or unions,
8070 avoid the overhead of allocating a hash table. Instead just do
8071 the nested traversal thing. */
8072 if (timeout > 0)
8073 {
8074 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
8075 /* When using -fplan9-extensions, we can have duplicates
8076 between typedef names and fields. */
8077 if (DECL_NAME (x)
8078 || (flag_plan9_extensions
8079 && DECL_NAME (x) == NULL_TREE
8080 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
8081 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8082 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
8083 {
8084 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
8085 if (is_duplicate_field (y, x))
8086 {
8087 error ("duplicate member %q+D", x);
8088 DECL_NAME (x) = NULL_TREE;
8089 }
8090 }
8091 }
8092 else
8093 {
8094 hash_table<nofree_ptr_hash <tree_node> > htab (37);
8095 detect_field_duplicates_hash (fieldlist, &htab);
8096 }
8097 }
8098
8099 /* Finish up struct info used by -Wc++-compat. */
8100
8101 static void
8102 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
8103 location_t record_loc)
8104 {
8105 unsigned int ix;
8106 tree x;
8107 struct c_binding *b;
8108
8109 if (fieldlist == NULL_TREE)
8110 {
8111 if (code == RECORD_TYPE)
8112 warning_at (record_loc, OPT_Wc___compat,
8113 "empty struct has size 0 in C, size 1 in C++");
8114 else
8115 warning_at (record_loc, OPT_Wc___compat,
8116 "empty union has size 0 in C, size 1 in C++");
8117 }
8118
8119 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
8120 the current struct. We do this now at the end of the struct
8121 because the flag is used to issue visibility warnings, and we
8122 only want to issue those warnings if the type is referenced
8123 outside of the struct declaration. */
8124 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
8125 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
8126
8127 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
8128 typedefs used when declaring fields in this struct. If the name
8129 of any of the fields is also a typedef name then the struct would
8130 not parse in C++, because the C++ lookup rules say that the
8131 typedef name would be looked up in the context of the struct, and
8132 would thus be the field rather than the typedef. */
8133 if (!struct_parse_info->typedefs_seen.is_empty ()
8134 && fieldlist != NULL_TREE)
8135 {
8136 /* Use a hash_set<tree> using the name of the typedef. We can use
8137 a hash_set<tree> because identifiers are interned. */
8138 hash_set<tree> tset;
8139
8140 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
8141 tset.add (DECL_NAME (x));
8142
8143 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
8144 {
8145 if (DECL_NAME (x) != NULL_TREE
8146 && tset.contains (DECL_NAME (x)))
8147 {
8148 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
8149 ("using %qD as both field and typedef name is "
8150 "invalid in C++"),
8151 x);
8152 /* FIXME: It would be nice to report the location where
8153 the typedef name is used. */
8154 }
8155 }
8156 }
8157
8158 /* For each field which has a binding and which was not defined in
8159 an enclosing struct, clear the in_struct field. */
8160 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
8161 b->in_struct = 0;
8162 }
8163
8164 /* Function to help qsort sort FIELD_DECLs by name order. */
8165
8166 static int
8167 field_decl_cmp (const void *x_p, const void *y_p)
8168 {
8169 const tree *const x = (const tree *) x_p;
8170 const tree *const y = (const tree *) y_p;
8171
8172 if (DECL_NAME (*x) == DECL_NAME (*y))
8173 /* A nontype is "greater" than a type. */
8174 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8175 if (DECL_NAME (*x) == NULL_TREE)
8176 return -1;
8177 if (DECL_NAME (*y) == NULL_TREE)
8178 return 1;
8179 if (DECL_NAME (*x) < DECL_NAME (*y))
8180 return -1;
8181 return 1;
8182 }
8183
8184 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8185 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8186 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8187 ATTRIBUTES are attributes to be applied to the structure.
8188
8189 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8190 the struct was started. */
8191
8192 tree
8193 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
8194 class c_struct_parse_info *enclosing_struct_parse_info)
8195 {
8196 tree x;
8197 bool toplevel = file_scope == current_scope;
8198
8199 /* If this type was previously laid out as a forward reference,
8200 make sure we lay it out again. */
8201
8202 TYPE_SIZE (t) = NULL_TREE;
8203
8204 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8205
8206 if (pedantic)
8207 {
8208 for (x = fieldlist; x; x = DECL_CHAIN (x))
8209 {
8210 if (DECL_NAME (x) != NULL_TREE)
8211 break;
8212 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8213 break;
8214 }
8215
8216 if (x == NULL_TREE)
8217 {
8218 if (TREE_CODE (t) == UNION_TYPE)
8219 {
8220 if (fieldlist)
8221 pedwarn (loc, OPT_Wpedantic, "union has no named members");
8222 else
8223 pedwarn (loc, OPT_Wpedantic, "union has no members");
8224 }
8225 else
8226 {
8227 if (fieldlist)
8228 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
8229 else
8230 pedwarn (loc, OPT_Wpedantic, "struct has no members");
8231 }
8232 }
8233 }
8234
8235 /* Install struct as DECL_CONTEXT of each field decl.
8236 Also process specified field sizes, found in the DECL_INITIAL,
8237 storing 0 there after the type has been changed to precision equal
8238 to its width, rather than the precision of the specified standard
8239 type. (Correct layout requires the original type to have been preserved
8240 until now.) */
8241
8242 bool saw_named_field = false;
8243 for (x = fieldlist; x; x = DECL_CHAIN (x))
8244 {
8245 if (TREE_TYPE (x) == error_mark_node)
8246 continue;
8247
8248 DECL_CONTEXT (x) = t;
8249
8250 /* If any field is const, the structure type is pseudo-const. */
8251 if (TREE_READONLY (x))
8252 C_TYPE_FIELDS_READONLY (t) = 1;
8253 else
8254 {
8255 /* A field that is pseudo-const makes the structure likewise. */
8256 tree t1 = strip_array_types (TREE_TYPE (x));
8257 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8258 C_TYPE_FIELDS_READONLY (t) = 1;
8259 }
8260
8261 /* Any field that is volatile means variables of this type must be
8262 treated in some ways as volatile. */
8263 if (TREE_THIS_VOLATILE (x))
8264 C_TYPE_FIELDS_VOLATILE (t) = 1;
8265
8266 /* Any field of nominal variable size implies structure is too. */
8267 if (C_DECL_VARIABLE_SIZE (x))
8268 C_TYPE_VARIABLE_SIZE (t) = 1;
8269
8270 if (DECL_C_BIT_FIELD (x))
8271 {
8272 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8273 DECL_SIZE (x) = bitsize_int (width);
8274 DECL_BIT_FIELD (x) = 1;
8275 }
8276
8277 if (TYPE_PACKED (t)
8278 && (DECL_BIT_FIELD (x)
8279 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8280 DECL_PACKED (x) = 1;
8281
8282 /* Detect flexible array member in an invalid context. */
8283 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8284 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
8285 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
8286 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8287 {
8288 if (TREE_CODE (t) == UNION_TYPE)
8289 {
8290 error_at (DECL_SOURCE_LOCATION (x),
8291 "flexible array member in union");
8292 TREE_TYPE (x) = error_mark_node;
8293 }
8294 else if (DECL_CHAIN (x) != NULL_TREE)
8295 {
8296 error_at (DECL_SOURCE_LOCATION (x),
8297 "flexible array member not at end of struct");
8298 TREE_TYPE (x) = error_mark_node;
8299 }
8300 else if (!saw_named_field)
8301 {
8302 error_at (DECL_SOURCE_LOCATION (x),
8303 "flexible array member in a struct with no named "
8304 "members");
8305 TREE_TYPE (x) = error_mark_node;
8306 }
8307 }
8308
8309 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8310 && flexible_array_type_p (TREE_TYPE (x)))
8311 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8312 "invalid use of structure with flexible array member");
8313
8314 if (DECL_NAME (x)
8315 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8316 saw_named_field = true;
8317 }
8318
8319 detect_field_duplicates (fieldlist);
8320
8321 /* Now we have the nearly final fieldlist. Record it,
8322 then lay out the structure or union (including the fields). */
8323
8324 TYPE_FIELDS (t) = fieldlist;
8325
8326 maybe_apply_pragma_scalar_storage_order (t);
8327
8328 layout_type (t);
8329
8330 if (TYPE_SIZE_UNIT (t)
8331 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8332 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8333 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8334 error ("type %qT is too large", t);
8335
8336 /* Give bit-fields their proper types and rewrite the type of array fields
8337 with scalar component if the enclosing type has reverse storage order. */
8338 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8339 {
8340 if (TREE_CODE (field) == FIELD_DECL
8341 && DECL_INITIAL (field)
8342 && TREE_TYPE (field) != error_mark_node)
8343 {
8344 unsigned HOST_WIDE_INT width
8345 = tree_to_uhwi (DECL_INITIAL (field));
8346 tree type = TREE_TYPE (field);
8347 if (width != TYPE_PRECISION (type))
8348 {
8349 TREE_TYPE (field)
8350 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8351 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8352 }
8353 DECL_INITIAL (field) = NULL_TREE;
8354 }
8355 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8356 && TREE_CODE (field) == FIELD_DECL
8357 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8358 {
8359 tree ftype = TREE_TYPE (field);
8360 tree ctype = strip_array_types (ftype);
8361 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8362 {
8363 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8364 tree *typep = &fmain_type;
8365 do {
8366 *typep = build_distinct_type_copy (*typep);
8367 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8368 typep = &TREE_TYPE (*typep);
8369 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8370 TREE_TYPE (field)
8371 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8372 }
8373 }
8374 }
8375
8376 /* Now we have the truly final field list.
8377 Store it in this type and in the variants. */
8378
8379 TYPE_FIELDS (t) = fieldlist;
8380
8381 /* If there are lots of fields, sort so we can look through them fast.
8382 We arbitrarily consider 16 or more elts to be "a lot". */
8383
8384 {
8385 int len = 0;
8386
8387 for (x = fieldlist; x; x = DECL_CHAIN (x))
8388 {
8389 if (len > 15 || DECL_NAME (x) == NULL)
8390 break;
8391 len += 1;
8392 }
8393
8394 if (len > 15)
8395 {
8396 tree *field_array;
8397 struct lang_type *space;
8398 struct sorted_fields_type *space2;
8399
8400 len += list_length (x);
8401
8402 /* Use the same allocation policy here that make_node uses, to
8403 ensure that this lives as long as the rest of the struct decl.
8404 All decls in an inline function need to be saved. */
8405
8406 space = ggc_cleared_alloc<struct lang_type> ();
8407 space2 = (sorted_fields_type *) ggc_internal_alloc
8408 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8409
8410 len = 0;
8411 space->s = space2;
8412 field_array = &space2->elts[0];
8413 for (x = fieldlist; x; x = DECL_CHAIN (x))
8414 {
8415 field_array[len++] = x;
8416
8417 /* If there is anonymous struct or union, break out of the loop. */
8418 if (DECL_NAME (x) == NULL)
8419 break;
8420 }
8421 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8422 if (x == NULL)
8423 {
8424 TYPE_LANG_SPECIFIC (t) = space;
8425 TYPE_LANG_SPECIFIC (t)->s->len = len;
8426 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8427 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8428 }
8429 }
8430 }
8431
8432 /* If this was supposed to be a transparent union, but we can't
8433 make it one, warn and turn off the flag. */
8434 if (TREE_CODE (t) == UNION_TYPE
8435 && TYPE_TRANSPARENT_AGGR (t)
8436 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8437 {
8438 TYPE_TRANSPARENT_AGGR (t) = 0;
8439 warning_at (loc, 0, "union cannot be made transparent");
8440 }
8441
8442 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8443 in dwarf2out via rest_of_decl_compilation below and means
8444 something totally different. Since we will be clearing
8445 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8446 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8447 C_TYPE_INCOMPLETE_VARS should use some language specific
8448 node. */
8449 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8450 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8451 {
8452 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8453 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8454 TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
8455 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8456 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8457 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8458 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8459 }
8460
8461 /* Update type location to the one of the definition, instead of e.g.
8462 a forward declaration. */
8463 if (TYPE_STUB_DECL (t))
8464 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8465
8466 /* Finish debugging output for this type. */
8467 rest_of_type_compilation (t, toplevel);
8468
8469 /* If this structure or union completes the type of any previous
8470 variable declaration, lay it out and output its rtl. */
8471 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8472 {
8473 tree decl = TREE_VALUE (x);
8474 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8475 layout_array_type (TREE_TYPE (decl));
8476 if (TREE_CODE (decl) != TYPE_DECL)
8477 {
8478 layout_decl (decl, 0);
8479 if (c_dialect_objc ())
8480 objc_check_decl (decl);
8481 rest_of_decl_compilation (decl, toplevel, 0);
8482 }
8483 }
8484
8485 /* If we're inside a function proper, i.e. not file-scope and not still
8486 parsing parameters, then arrange for the size of a variable sized type
8487 to be bound now. */
8488 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8489 add_stmt (build_stmt (loc,
8490 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8491
8492 if (warn_cxx_compat)
8493 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8494
8495 delete struct_parse_info;
8496
8497 struct_parse_info = enclosing_struct_parse_info;
8498
8499 /* If this struct is defined inside a struct, add it to
8500 struct_types. */
8501 if (warn_cxx_compat
8502 && struct_parse_info != NULL
8503 && !in_sizeof && !in_typeof && !in_alignof)
8504 struct_parse_info->struct_types.safe_push (t);
8505
8506 return t;
8507 }
8508
8509 static struct {
8510 gt_pointer_operator new_value;
8511 void *cookie;
8512 } resort_data;
8513
8514 /* This routine compares two fields like field_decl_cmp but using the
8515 pointer operator in resort_data. */
8516
8517 static int
8518 resort_field_decl_cmp (const void *x_p, const void *y_p)
8519 {
8520 const tree *const x = (const tree *) x_p;
8521 const tree *const y = (const tree *) y_p;
8522
8523 if (DECL_NAME (*x) == DECL_NAME (*y))
8524 /* A nontype is "greater" than a type. */
8525 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8526 if (DECL_NAME (*x) == NULL_TREE)
8527 return -1;
8528 if (DECL_NAME (*y) == NULL_TREE)
8529 return 1;
8530 {
8531 tree d1 = DECL_NAME (*x);
8532 tree d2 = DECL_NAME (*y);
8533 resort_data.new_value (&d1, resort_data.cookie);
8534 resort_data.new_value (&d2, resort_data.cookie);
8535 if (d1 < d2)
8536 return -1;
8537 }
8538 return 1;
8539 }
8540
8541 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8542
8543 void
8544 resort_sorted_fields (void *obj,
8545 void * ARG_UNUSED (orig_obj),
8546 gt_pointer_operator new_value,
8547 void *cookie)
8548 {
8549 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8550 resort_data.new_value = new_value;
8551 resort_data.cookie = cookie;
8552 qsort (&sf->elts[0], sf->len, sizeof (tree),
8553 resort_field_decl_cmp);
8554 }
8555
8556 /* Lay out the type T, and its element type, and so on. */
8557
8558 static void
8559 layout_array_type (tree t)
8560 {
8561 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8562 layout_array_type (TREE_TYPE (t));
8563 layout_type (t);
8564 }
8565 \f
8566 /* Begin compiling the definition of an enumeration type.
8567 NAME is its name (or null if anonymous).
8568 LOC is the enum's location.
8569 Returns the type object, as yet incomplete.
8570 Also records info about it so that build_enumerator
8571 may be used to declare the individual values as they are read. */
8572
8573 tree
8574 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8575 {
8576 tree enumtype = NULL_TREE;
8577 location_t enumloc = UNKNOWN_LOCATION;
8578
8579 /* If this is the real definition for a previous forward reference,
8580 fill in the contents in the same object that used to be the
8581 forward reference. */
8582
8583 if (name != NULL_TREE)
8584 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8585
8586 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8587 {
8588 enumtype = make_node (ENUMERAL_TYPE);
8589 pushtag (loc, name, enumtype);
8590 }
8591 /* Update type location to the one of the definition, instead of e.g.
8592 a forward declaration. */
8593 else if (TYPE_STUB_DECL (enumtype))
8594 {
8595 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
8596 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
8597 }
8598
8599 if (C_TYPE_BEING_DEFINED (enumtype))
8600 error_at (loc, "nested redefinition of %<enum %E%>", name);
8601
8602 C_TYPE_BEING_DEFINED (enumtype) = 1;
8603
8604 if (TYPE_VALUES (enumtype) != NULL_TREE)
8605 {
8606 /* This enum is a named one that has been declared already. */
8607 error_at (loc, "redeclaration of %<enum %E%>", name);
8608 if (enumloc != UNKNOWN_LOCATION)
8609 inform (enumloc, "originally defined here");
8610
8611 /* Completely replace its old definition.
8612 The old enumerators remain defined, however. */
8613 TYPE_VALUES (enumtype) = NULL_TREE;
8614 }
8615
8616 the_enum->enum_next_value = integer_zero_node;
8617 the_enum->enum_overflow = 0;
8618
8619 if (flag_short_enums)
8620 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8621 TYPE_PACKED (v) = 1;
8622
8623 /* FIXME: This will issue a warning for a use of a type defined
8624 within sizeof in a statement expr. This is not terribly serious
8625 as C++ doesn't permit statement exprs within sizeof anyhow. */
8626 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8627 warning_at (loc, OPT_Wc___compat,
8628 "defining type in %qs expression is invalid in C++",
8629 (in_sizeof
8630 ? "sizeof"
8631 : (in_typeof ? "typeof" : "alignof")));
8632
8633 return enumtype;
8634 }
8635
8636 /* After processing and defining all the values of an enumeration type,
8637 install their decls in the enumeration type and finish it off.
8638 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8639 and ATTRIBUTES are the specified attributes.
8640 Returns ENUMTYPE. */
8641
8642 tree
8643 finish_enum (tree enumtype, tree values, tree attributes)
8644 {
8645 tree pair, tem;
8646 tree minnode = NULL_TREE, maxnode = NULL_TREE;
8647 int precision;
8648 signop sign;
8649 bool toplevel = (file_scope == current_scope);
8650 struct lang_type *lt;
8651
8652 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8653
8654 /* Calculate the maximum value of any enumerator in this type. */
8655
8656 if (values == error_mark_node)
8657 minnode = maxnode = integer_zero_node;
8658 else
8659 {
8660 minnode = maxnode = TREE_VALUE (values);
8661 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8662 {
8663 tree value = TREE_VALUE (pair);
8664 if (tree_int_cst_lt (maxnode, value))
8665 maxnode = value;
8666 if (tree_int_cst_lt (value, minnode))
8667 minnode = value;
8668 }
8669 }
8670
8671 /* Construct the final type of this enumeration. It is the same
8672 as one of the integral types - the narrowest one that fits, except
8673 that normally we only go as narrow as int - and signed iff any of
8674 the values are negative. */
8675 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8676 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8677 tree_int_cst_min_precision (maxnode, sign));
8678
8679 /* If the precision of the type was specified with an attribute and it
8680 was too small, give an error. Otherwise, use it. */
8681 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8682 {
8683 if (precision > TYPE_PRECISION (enumtype))
8684 {
8685 TYPE_PRECISION (enumtype) = 0;
8686 error ("specified mode too small for enumerated values");
8687 }
8688 else
8689 precision = TYPE_PRECISION (enumtype);
8690 }
8691 else
8692 TYPE_PRECISION (enumtype) = 0;
8693
8694 if (TYPE_PACKED (enumtype)
8695 || precision > TYPE_PRECISION (integer_type_node)
8696 || TYPE_PRECISION (enumtype))
8697 {
8698 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8699 if (tem == NULL)
8700 {
8701 warning (0, "enumeration values exceed range of largest integer");
8702 tem = long_long_integer_type_node;
8703 }
8704 }
8705 else
8706 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8707
8708 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8709 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8710 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8711 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8712 TYPE_SIZE (enumtype) = NULL_TREE;
8713 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8714
8715 layout_type (enumtype);
8716
8717 if (values != error_mark_node)
8718 {
8719 /* Change the type of the enumerators to be the enum type. We
8720 need to do this irrespective of the size of the enum, for
8721 proper type checking. Replace the DECL_INITIALs of the
8722 enumerators, and the value slots of the list, with copies
8723 that have the enum type; they cannot be modified in place
8724 because they may be shared (e.g. integer_zero_node) Finally,
8725 change the purpose slots to point to the names of the decls. */
8726 for (pair = values; pair; pair = TREE_CHAIN (pair))
8727 {
8728 tree enu = TREE_PURPOSE (pair);
8729 tree ini = DECL_INITIAL (enu);
8730
8731 TREE_TYPE (enu) = enumtype;
8732
8733 /* The ISO C Standard mandates enumerators to have type int,
8734 even though the underlying type of an enum type is
8735 unspecified. However, GCC allows enumerators of any
8736 integer type as an extensions. build_enumerator()
8737 converts any enumerators that fit in an int to type int,
8738 to avoid promotions to unsigned types when comparing
8739 integers with enumerators that fit in the int range.
8740 When -pedantic is given, build_enumerator() would have
8741 already warned about those that don't fit. Here we
8742 convert the rest to the enumerator type. */
8743 if (TREE_TYPE (ini) != integer_type_node)
8744 ini = convert (enumtype, ini);
8745
8746 DECL_INITIAL (enu) = ini;
8747 TREE_PURPOSE (pair) = DECL_NAME (enu);
8748 TREE_VALUE (pair) = ini;
8749 }
8750
8751 TYPE_VALUES (enumtype) = values;
8752 }
8753
8754 /* Record the min/max values so that we can warn about bit-field
8755 enumerations that are too small for the values. */
8756 lt = ggc_cleared_alloc<struct lang_type> ();
8757 lt->enum_min = minnode;
8758 lt->enum_max = maxnode;
8759 TYPE_LANG_SPECIFIC (enumtype) = lt;
8760
8761 /* Fix up all variant types of this enum type. */
8762 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8763 {
8764 if (tem == enumtype)
8765 continue;
8766 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8767 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8768 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8769 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8770 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8771 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8772 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8773 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8774 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8775 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8776 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8777 }
8778
8779 /* Finish debugging output for this type. */
8780 rest_of_type_compilation (enumtype, toplevel);
8781
8782 /* If this enum is defined inside a struct, add it to
8783 struct_types. */
8784 if (warn_cxx_compat
8785 && struct_parse_info != NULL
8786 && !in_sizeof && !in_typeof && !in_alignof)
8787 struct_parse_info->struct_types.safe_push (enumtype);
8788
8789 C_TYPE_BEING_DEFINED (enumtype) = 0;
8790
8791 return enumtype;
8792 }
8793
8794 /* Build and install a CONST_DECL for one value of the
8795 current enumeration type (one that was begun with start_enum).
8796 DECL_LOC is the location of the enumerator.
8797 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8798 Return a tree-list containing the CONST_DECL and its value.
8799 Assignment of sequential values by default is handled here. */
8800
8801 tree
8802 build_enumerator (location_t decl_loc, location_t loc,
8803 struct c_enum_contents *the_enum, tree name, tree value)
8804 {
8805 tree decl, type;
8806
8807 /* Validate and default VALUE. */
8808
8809 if (value != NULL_TREE)
8810 {
8811 /* Don't issue more errors for error_mark_node (i.e. an
8812 undeclared identifier) - just ignore the value expression. */
8813 if (value == error_mark_node)
8814 value = NULL_TREE;
8815 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8816 {
8817 error_at (loc, "enumerator value for %qE is not an integer constant",
8818 name);
8819 value = NULL_TREE;
8820 }
8821 else
8822 {
8823 if (TREE_CODE (value) != INTEGER_CST)
8824 {
8825 value = c_fully_fold (value, false, NULL);
8826 if (TREE_CODE (value) == INTEGER_CST)
8827 pedwarn (loc, OPT_Wpedantic,
8828 "enumerator value for %qE is not an integer "
8829 "constant expression", name);
8830 }
8831 if (TREE_CODE (value) != INTEGER_CST)
8832 {
8833 error ("enumerator value for %qE is not an integer constant",
8834 name);
8835 value = NULL_TREE;
8836 }
8837 else
8838 {
8839 value = default_conversion (value);
8840 constant_expression_warning (value);
8841 }
8842 }
8843 }
8844
8845 /* Default based on previous value. */
8846 /* It should no longer be possible to have NON_LVALUE_EXPR
8847 in the default. */
8848 if (value == NULL_TREE)
8849 {
8850 value = the_enum->enum_next_value;
8851 if (the_enum->enum_overflow)
8852 error_at (loc, "overflow in enumeration values");
8853 }
8854 /* Even though the underlying type of an enum is unspecified, the
8855 type of enumeration constants is explicitly defined as int
8856 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8857 an extension. */
8858 else if (!int_fits_type_p (value, integer_type_node))
8859 pedwarn (loc, OPT_Wpedantic,
8860 "ISO C restricts enumerator values to range of %<int%>");
8861
8862 /* The ISO C Standard mandates enumerators to have type int, even
8863 though the underlying type of an enum type is unspecified.
8864 However, GCC allows enumerators of any integer type as an
8865 extensions. Here we convert any enumerators that fit in an int
8866 to type int, to avoid promotions to unsigned types when comparing
8867 integers with enumerators that fit in the int range. When
8868 -pedantic is given, we would have already warned about those that
8869 don't fit. We have to do this here rather than in finish_enum
8870 because this value may be used to define more enumerators. */
8871 if (int_fits_type_p (value, integer_type_node))
8872 value = convert (integer_type_node, value);
8873
8874 /* Set basis for default for next value. */
8875 the_enum->enum_next_value
8876 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8877 PLUS_EXPR, value, integer_one_node, false);
8878 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8879
8880 /* Now create a declaration for the enum value name. */
8881
8882 type = TREE_TYPE (value);
8883 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8884 TYPE_PRECISION (integer_type_node)),
8885 (TYPE_PRECISION (type)
8886 >= TYPE_PRECISION (integer_type_node)
8887 && TYPE_UNSIGNED (type)));
8888
8889 decl = build_decl (decl_loc, CONST_DECL, name, type);
8890 DECL_INITIAL (decl) = convert (type, value);
8891 pushdecl (decl);
8892
8893 return tree_cons (decl, value, NULL_TREE);
8894 }
8895
8896 \f
8897 /* Create the FUNCTION_DECL for a function definition.
8898 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8899 the declaration; they describe the function's name and the type it returns,
8900 but twisted together in a fashion that parallels the syntax of C.
8901
8902 This function creates a binding context for the function body
8903 as well as setting up the FUNCTION_DECL in current_function_decl.
8904
8905 Returns true on success. If the DECLARATOR is not suitable for a function
8906 (it defines a datum instead), we return false to report a parse error. */
8907
8908 bool
8909 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8910 tree attributes)
8911 {
8912 tree decl1, old_decl;
8913 tree restype, resdecl;
8914 location_t loc;
8915
8916 current_function_returns_value = 0; /* Assume, until we see it does. */
8917 current_function_returns_null = 0;
8918 current_function_returns_abnormally = 0;
8919 warn_about_return_type = 0;
8920 c_switch_stack = NULL;
8921
8922 /* Indicate no valid break/continue context by setting these variables
8923 to some non-null, non-label value. We'll notice and emit the proper
8924 error message in c_finish_bc_stmt. */
8925 c_break_label = c_cont_label = size_zero_node;
8926
8927 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8928 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8929 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8930
8931 /* If the declarator is not suitable for a function definition,
8932 cause a syntax error. */
8933 if (decl1 == NULL_TREE
8934 || TREE_CODE (decl1) != FUNCTION_DECL)
8935 return false;
8936
8937 loc = DECL_SOURCE_LOCATION (decl1);
8938
8939 /* A nested function is not global. */
8940 if (current_function_decl != NULL_TREE)
8941 TREE_PUBLIC (decl1) = 0;
8942
8943 c_decl_attributes (&decl1, attributes, 0);
8944
8945 if (DECL_DECLARED_INLINE_P (decl1)
8946 && DECL_UNINLINABLE (decl1)
8947 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8948 warning_at (loc, OPT_Wattributes,
8949 "inline function %qD given attribute %qs",
8950 decl1, "noinline");
8951
8952 /* Handle gnu_inline attribute. */
8953 if (declspecs->inline_p
8954 && !flag_gnu89_inline
8955 && TREE_CODE (decl1) == FUNCTION_DECL
8956 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8957 || current_function_decl))
8958 {
8959 if (declspecs->storage_class != csc_static)
8960 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8961 }
8962
8963 announce_function (decl1);
8964
8965 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8966 {
8967 error_at (loc, "return type is an incomplete type");
8968 /* Make it return void instead. */
8969 TREE_TYPE (decl1)
8970 = build_function_type (void_type_node,
8971 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8972 }
8973
8974 if (warn_about_return_type)
8975 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8976 : (warn_return_type > 0 ? OPT_Wreturn_type
8977 : OPT_Wimplicit_int),
8978 "return type defaults to %<int%>");
8979
8980 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8981 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8982 DECL_INITIAL (decl1) = error_mark_node;
8983
8984 /* If this definition isn't a prototype and we had a prototype declaration
8985 before, copy the arg type info from that prototype. */
8986 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8987 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8988 old_decl = NULL_TREE;
8989 current_function_prototype_locus = UNKNOWN_LOCATION;
8990 current_function_prototype_built_in = false;
8991 current_function_prototype_arg_types = NULL_TREE;
8992 if (!prototype_p (TREE_TYPE (decl1)))
8993 {
8994 if (old_decl != NULL_TREE
8995 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8996 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8997 TREE_TYPE (TREE_TYPE (old_decl))))
8998 {
8999 if (stdarg_p (TREE_TYPE (old_decl)))
9000 {
9001 auto_diagnostic_group d;
9002 warning_at (loc, 0, "%q+D defined as variadic function "
9003 "without prototype", decl1);
9004 locate_old_decl (old_decl);
9005 }
9006 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
9007 TREE_TYPE (decl1));
9008 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
9009 current_function_prototype_built_in
9010 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
9011 current_function_prototype_arg_types
9012 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
9013 }
9014 if (TREE_PUBLIC (decl1))
9015 {
9016 /* If there is an external prototype declaration of this
9017 function, record its location but do not copy information
9018 to this decl. This may be an invisible declaration
9019 (built-in or in a scope which has finished) or simply
9020 have more refined argument types than any declaration
9021 found above. */
9022 struct c_binding *b;
9023 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
9024 if (B_IN_SCOPE (b, external_scope))
9025 break;
9026 if (b)
9027 {
9028 tree ext_decl, ext_type;
9029 ext_decl = b->decl;
9030 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
9031 if (TREE_CODE (ext_type) == FUNCTION_TYPE
9032 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
9033 TREE_TYPE (ext_type)))
9034 {
9035 current_function_prototype_locus
9036 = DECL_SOURCE_LOCATION (ext_decl);
9037 current_function_prototype_built_in
9038 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
9039 current_function_prototype_arg_types
9040 = TYPE_ARG_TYPES (ext_type);
9041 }
9042 }
9043 }
9044 }
9045
9046 /* Optionally warn of old-fashioned def with no previous prototype. */
9047 if (warn_strict_prototypes
9048 && old_decl != error_mark_node
9049 && !prototype_p (TREE_TYPE (decl1))
9050 && C_DECL_ISNT_PROTOTYPE (old_decl))
9051 warning_at (loc, OPT_Wstrict_prototypes,
9052 "function declaration isn%'t a prototype");
9053 /* Optionally warn of any global def with no previous prototype. */
9054 else if (warn_missing_prototypes
9055 && old_decl != error_mark_node
9056 && TREE_PUBLIC (decl1)
9057 && !MAIN_NAME_P (DECL_NAME (decl1))
9058 && C_DECL_ISNT_PROTOTYPE (old_decl)
9059 && !DECL_DECLARED_INLINE_P (decl1))
9060 warning_at (loc, OPT_Wmissing_prototypes,
9061 "no previous prototype for %qD", decl1);
9062 /* Optionally warn of any def with no previous prototype
9063 if the function has already been used. */
9064 else if (warn_missing_prototypes
9065 && old_decl != NULL_TREE
9066 && old_decl != error_mark_node
9067 && TREE_USED (old_decl)
9068 && !prototype_p (TREE_TYPE (old_decl)))
9069 warning_at (loc, OPT_Wmissing_prototypes,
9070 "%qD was used with no prototype before its definition", decl1);
9071 /* Optionally warn of any global def with no previous declaration. */
9072 else if (warn_missing_declarations
9073 && TREE_PUBLIC (decl1)
9074 && old_decl == NULL_TREE
9075 && !MAIN_NAME_P (DECL_NAME (decl1))
9076 && !DECL_DECLARED_INLINE_P (decl1))
9077 warning_at (loc, OPT_Wmissing_declarations,
9078 "no previous declaration for %qD",
9079 decl1);
9080 /* Optionally warn of any def with no previous declaration
9081 if the function has already been used. */
9082 else if (warn_missing_declarations
9083 && old_decl != NULL_TREE
9084 && old_decl != error_mark_node
9085 && TREE_USED (old_decl)
9086 && C_DECL_IMPLICIT (old_decl))
9087 warning_at (loc, OPT_Wmissing_declarations,
9088 "%qD was used with no declaration before its definition", decl1);
9089
9090 /* This function exists in static storage.
9091 (This does not mean `static' in the C sense!) */
9092 TREE_STATIC (decl1) = 1;
9093
9094 /* This is the earliest point at which we might know the assembler
9095 name of the function. Thus, if it's set before this, die horribly. */
9096 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
9097
9098 /* If #pragma weak was used, mark the decl weak now. */
9099 if (current_scope == file_scope)
9100 maybe_apply_pragma_weak (decl1);
9101
9102 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
9103 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
9104 {
9105 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
9106 != integer_type_node)
9107 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
9108 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
9109 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
9110 decl1);
9111
9112 check_main_parameter_types (decl1);
9113
9114 if (!TREE_PUBLIC (decl1))
9115 pedwarn (loc, OPT_Wmain,
9116 "%qD is normally a non-static function", decl1);
9117 }
9118
9119 /* Record the decl so that the function name is defined.
9120 If we already have a decl for this name, and it is a FUNCTION_DECL,
9121 use the old decl. */
9122
9123 current_function_decl = pushdecl (decl1);
9124
9125 push_scope ();
9126 declare_parm_level ();
9127
9128 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
9129 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
9130 DECL_ARTIFICIAL (resdecl) = 1;
9131 DECL_IGNORED_P (resdecl) = 1;
9132 DECL_RESULT (current_function_decl) = resdecl;
9133
9134 start_fname_decls ();
9135
9136 return true;
9137 }
9138 \f
9139 /* Subroutine of store_parm_decls which handles new-style function
9140 definitions (prototype format). The parms already have decls, so we
9141 need only record them as in effect and complain if any redundant
9142 old-style parm decls were written. */
9143 static void
9144 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
9145 {
9146 tree decl;
9147 c_arg_tag *tag;
9148 unsigned ix;
9149
9150 if (current_scope->bindings)
9151 {
9152 error_at (DECL_SOURCE_LOCATION (fndecl),
9153 "old-style parameter declarations in prototyped "
9154 "function definition");
9155
9156 /* Get rid of the old-style declarations. */
9157 pop_scope ();
9158 push_scope ();
9159 }
9160 /* Don't issue this warning for nested functions, and don't issue this
9161 warning if we got here because ARG_INFO_TYPES was error_mark_node
9162 (this happens when a function definition has just an ellipsis in
9163 its parameter list). */
9164 else if (!in_system_header_at (input_location)
9165 && !current_function_scope
9166 && arg_info->types != error_mark_node)
9167 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
9168 "traditional C rejects ISO C style function definitions");
9169
9170 /* Now make all the parameter declarations visible in the function body.
9171 We can bypass most of the grunt work of pushdecl. */
9172 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
9173 {
9174 DECL_CONTEXT (decl) = current_function_decl;
9175 if (DECL_NAME (decl))
9176 {
9177 bind (DECL_NAME (decl), decl, current_scope,
9178 /*invisible=*/false, /*nested=*/false,
9179 UNKNOWN_LOCATION);
9180 if (!TREE_USED (decl))
9181 warn_if_shadowing (decl);
9182 }
9183 else
9184 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
9185 }
9186
9187 /* Record the parameter list in the function declaration. */
9188 DECL_ARGUMENTS (fndecl) = arg_info->parms;
9189
9190 /* Now make all the ancillary declarations visible, likewise. */
9191 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
9192 {
9193 DECL_CONTEXT (decl) = current_function_decl;
9194 if (DECL_NAME (decl))
9195 bind (DECL_NAME (decl), decl, current_scope,
9196 /*invisible=*/false,
9197 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
9198 UNKNOWN_LOCATION);
9199 }
9200
9201 /* And all the tag declarations. */
9202 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
9203 if (tag->id)
9204 bind (tag->id, tag->type, current_scope,
9205 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
9206 }
9207
9208 /* Subroutine of store_parm_decls which handles old-style function
9209 definitions (separate parameter list and declarations). */
9210
9211 static void
9212 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
9213 {
9214 struct c_binding *b;
9215 tree parm, decl, last;
9216 tree parmids = arg_info->parms;
9217 hash_set<tree> seen_args;
9218
9219 if (!in_system_header_at (input_location))
9220 warning_at (DECL_SOURCE_LOCATION (fndecl),
9221 OPT_Wold_style_definition, "old-style function definition");
9222
9223 /* Match each formal parameter name with its declaration. Save each
9224 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
9225 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9226 {
9227 if (TREE_VALUE (parm) == NULL_TREE)
9228 {
9229 error_at (DECL_SOURCE_LOCATION (fndecl),
9230 "parameter name missing from parameter list");
9231 TREE_PURPOSE (parm) = NULL_TREE;
9232 continue;
9233 }
9234
9235 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
9236 if (b && B_IN_CURRENT_SCOPE (b))
9237 {
9238 decl = b->decl;
9239 /* Skip erroneous parameters. */
9240 if (decl == error_mark_node)
9241 continue;
9242 /* If we got something other than a PARM_DECL it is an error. */
9243 if (TREE_CODE (decl) != PARM_DECL)
9244 {
9245 error_at (DECL_SOURCE_LOCATION (decl),
9246 "%qD declared as a non-parameter", decl);
9247 continue;
9248 }
9249 /* If the declaration is already marked, we have a duplicate
9250 name. Complain and ignore the duplicate. */
9251 else if (seen_args.contains (decl))
9252 {
9253 error_at (DECL_SOURCE_LOCATION (decl),
9254 "multiple parameters named %qD", decl);
9255 TREE_PURPOSE (parm) = NULL_TREE;
9256 continue;
9257 }
9258 /* If the declaration says "void", complain and turn it into
9259 an int. */
9260 else if (VOID_TYPE_P (TREE_TYPE (decl)))
9261 {
9262 error_at (DECL_SOURCE_LOCATION (decl),
9263 "parameter %qD declared with void type", decl);
9264 TREE_TYPE (decl) = integer_type_node;
9265 DECL_ARG_TYPE (decl) = integer_type_node;
9266 layout_decl (decl, 0);
9267 }
9268 warn_if_shadowing (decl);
9269 }
9270 /* If no declaration found, default to int. */
9271 else
9272 {
9273 /* FIXME diagnostics: This should be the location of the argument,
9274 not the FNDECL. E.g., for an old-style declaration
9275
9276 int f10(v) { blah; }
9277
9278 We should use the location of the V, not the F10.
9279 Unfortunately, the V is an IDENTIFIER_NODE which has no
9280 location. In the future we need locations for c_arg_info
9281 entries.
9282
9283 See gcc.dg/Wshadow-3.c for an example of this problem. */
9284 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
9285 PARM_DECL, TREE_VALUE (parm), integer_type_node);
9286 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9287 pushdecl (decl);
9288 warn_if_shadowing (decl);
9289
9290 if (flag_isoc99)
9291 pedwarn (DECL_SOURCE_LOCATION (decl),
9292 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9293 decl);
9294 else
9295 warning_at (DECL_SOURCE_LOCATION (decl),
9296 OPT_Wmissing_parameter_type,
9297 "type of %qD defaults to %<int%>", decl);
9298 }
9299
9300 TREE_PURPOSE (parm) = decl;
9301 seen_args.add (decl);
9302 }
9303
9304 /* Now examine the parms chain for incomplete declarations
9305 and declarations with no corresponding names. */
9306
9307 for (b = current_scope->bindings; b; b = b->prev)
9308 {
9309 parm = b->decl;
9310 if (TREE_CODE (parm) != PARM_DECL)
9311 continue;
9312
9313 if (TREE_TYPE (parm) != error_mark_node
9314 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9315 {
9316 error_at (DECL_SOURCE_LOCATION (parm),
9317 "parameter %qD has incomplete type", parm);
9318 TREE_TYPE (parm) = error_mark_node;
9319 }
9320
9321 if (!seen_args.contains (parm))
9322 {
9323 error_at (DECL_SOURCE_LOCATION (parm),
9324 "declaration for parameter %qD but no such parameter",
9325 parm);
9326
9327 /* Pretend the parameter was not missing.
9328 This gets us to a standard state and minimizes
9329 further error messages. */
9330 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9331 }
9332 }
9333
9334 /* Chain the declarations together in the order of the list of
9335 names. Store that chain in the function decl, replacing the
9336 list of names. Update the current scope to match. */
9337 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9338
9339 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9340 if (TREE_PURPOSE (parm))
9341 break;
9342 if (parm && TREE_PURPOSE (parm))
9343 {
9344 last = TREE_PURPOSE (parm);
9345 DECL_ARGUMENTS (fndecl) = last;
9346
9347 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9348 if (TREE_PURPOSE (parm))
9349 {
9350 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9351 last = TREE_PURPOSE (parm);
9352 }
9353 DECL_CHAIN (last) = NULL_TREE;
9354 }
9355
9356 /* If there was a previous prototype,
9357 set the DECL_ARG_TYPE of each argument according to
9358 the type previously specified, and report any mismatches. */
9359
9360 if (current_function_prototype_arg_types)
9361 {
9362 tree type;
9363 for (parm = DECL_ARGUMENTS (fndecl),
9364 type = current_function_prototype_arg_types;
9365 parm || (type != NULL_TREE
9366 && TREE_VALUE (type) != error_mark_node
9367 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
9368 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
9369 {
9370 if (parm == NULL_TREE
9371 || type == NULL_TREE
9372 || (TREE_VALUE (type) != error_mark_node
9373 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
9374 {
9375 if (current_function_prototype_built_in)
9376 warning_at (DECL_SOURCE_LOCATION (fndecl),
9377 0, "number of arguments doesn%'t match "
9378 "built-in prototype");
9379 else
9380 {
9381 /* FIXME diagnostics: This should be the location of
9382 FNDECL, but there is bug when a prototype is
9383 declared inside function context, but defined
9384 outside of it (e.g., gcc.dg/pr15698-2.c). In
9385 which case FNDECL gets the location of the
9386 prototype, not the definition. */
9387 error_at (input_location,
9388 "number of arguments doesn%'t match prototype");
9389
9390 error_at (current_function_prototype_locus,
9391 "prototype declaration");
9392 }
9393 break;
9394 }
9395 /* Type for passing arg must be consistent with that
9396 declared for the arg. ISO C says we take the unqualified
9397 type for parameters declared with qualified type. */
9398 if (TREE_TYPE (parm) != error_mark_node
9399 && TREE_VALUE (type) != error_mark_node
9400 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9401 != TYPE_ATOMIC (TREE_VALUE (type)))
9402 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9403 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9404 {
9405 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9406 == TYPE_ATOMIC (TREE_VALUE (type)))
9407 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9408 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9409 {
9410 /* Adjust argument to match prototype. E.g. a previous
9411 `int foo(float);' prototype causes
9412 `int foo(x) float x; {...}' to be treated like
9413 `int foo(float x) {...}'. This is particularly
9414 useful for argument types like uid_t. */
9415 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9416
9417 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9418 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9419 && (TYPE_PRECISION (TREE_TYPE (parm))
9420 < TYPE_PRECISION (integer_type_node)))
9421 DECL_ARG_TYPE (parm)
9422 = c_type_promotes_to (TREE_TYPE (parm));
9423
9424 /* ??? Is it possible to get here with a
9425 built-in prototype or will it always have
9426 been diagnosed as conflicting with an
9427 old-style definition and discarded? */
9428 if (current_function_prototype_built_in)
9429 warning_at (DECL_SOURCE_LOCATION (parm),
9430 OPT_Wpedantic, "promoted argument %qD "
9431 "doesn%'t match built-in prototype", parm);
9432 else
9433 {
9434 pedwarn (DECL_SOURCE_LOCATION (parm),
9435 OPT_Wpedantic, "promoted argument %qD "
9436 "doesn%'t match prototype", parm);
9437 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9438 "prototype declaration");
9439 }
9440 }
9441 else
9442 {
9443 if (current_function_prototype_built_in)
9444 warning_at (DECL_SOURCE_LOCATION (parm),
9445 0, "argument %qD doesn%'t match "
9446 "built-in prototype", parm);
9447 else
9448 {
9449 error_at (DECL_SOURCE_LOCATION (parm),
9450 "argument %qD doesn%'t match prototype", parm);
9451 error_at (current_function_prototype_locus,
9452 "prototype declaration");
9453 }
9454 }
9455 }
9456 }
9457 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9458 }
9459
9460 /* Otherwise, create a prototype that would match. */
9461
9462 else
9463 {
9464 tree actual = NULL_TREE, last = NULL_TREE, type;
9465
9466 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9467 {
9468 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9469 if (last)
9470 TREE_CHAIN (last) = type;
9471 else
9472 actual = type;
9473 last = type;
9474 }
9475 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9476 if (last)
9477 TREE_CHAIN (last) = type;
9478 else
9479 actual = type;
9480
9481 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9482 of the type of this function, but we need to avoid having this
9483 affect the types of other similarly-typed functions, so we must
9484 first force the generation of an identical (but separate) type
9485 node for the relevant function type. The new node we create
9486 will be a variant of the main variant of the original function
9487 type. */
9488
9489 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9490
9491 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9492 }
9493 }
9494
9495 /* Store parameter declarations passed in ARG_INFO into the current
9496 function declaration. */
9497
9498 void
9499 store_parm_decls_from (struct c_arg_info *arg_info)
9500 {
9501 current_function_arg_info = arg_info;
9502 store_parm_decls ();
9503 }
9504
9505 /* Called by walk_tree to look for and update context-less labels. */
9506
9507 static tree
9508 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9509 {
9510 if (TREE_CODE (*tp) == LABEL_EXPR
9511 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9512 {
9513 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9514 *walk_subtrees = 0;
9515 }
9516
9517 return NULL_TREE;
9518 }
9519
9520 /* Store the parameter declarations into the current function declaration.
9521 This is called after parsing the parameter declarations, before
9522 digesting the body of the function.
9523
9524 For an old-style definition, construct a prototype out of the old-style
9525 parameter declarations and inject it into the function's type. */
9526
9527 void
9528 store_parm_decls (void)
9529 {
9530 tree fndecl = current_function_decl;
9531 bool proto;
9532
9533 /* The argument information block for FNDECL. */
9534 struct c_arg_info *arg_info = current_function_arg_info;
9535 current_function_arg_info = 0;
9536
9537 /* True if this definition is written with a prototype. Note:
9538 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9539 list in a function definition as equivalent to (void) -- an
9540 empty argument list specifies the function has no parameters,
9541 but only (void) sets up a prototype for future calls. */
9542 proto = arg_info->types != 0;
9543
9544 if (proto)
9545 store_parm_decls_newstyle (fndecl, arg_info);
9546 else
9547 store_parm_decls_oldstyle (fndecl, arg_info);
9548
9549 /* The next call to push_scope will be a function body. */
9550
9551 next_is_function_body = true;
9552
9553 /* Write a record describing this function definition to the prototypes
9554 file (if requested). */
9555
9556 gen_aux_info_record (fndecl, 1, 0, proto);
9557
9558 /* Initialize the RTL code for the function. */
9559 allocate_struct_function (fndecl, false);
9560
9561 if (warn_unused_local_typedefs)
9562 cfun->language = ggc_cleared_alloc<language_function> ();
9563
9564 /* Begin the statement tree for this function. */
9565 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9566
9567 /* ??? Insert the contents of the pending sizes list into the function
9568 to be evaluated. The only reason left to have this is
9569 void foo(int n, int array[n++])
9570 because we throw away the array type in favor of a pointer type, and
9571 thus won't naturally see the SAVE_EXPR containing the increment. All
9572 other pending sizes would be handled by gimplify_parameters. */
9573 if (arg_info->pending_sizes)
9574 {
9575 /* In very special circumstances, e.g. for code like
9576 _Atomic int i = 5;
9577 void f (int a[i += 2]) {}
9578 we need to execute the atomic assignment on function entry.
9579 But in this case, it is not just a straight store, it has the
9580 op= form, which means that build_atomic_assign has generated
9581 gotos, labels, etc. Because at that time the function decl
9582 for F has not been created yet, those labels do not have any
9583 function context. But we have the fndecl now, so update the
9584 labels accordingly. gimplify_expr would crash otherwise. */
9585 walk_tree_without_duplicates (&arg_info->pending_sizes,
9586 set_labels_context_r, fndecl);
9587 add_stmt (arg_info->pending_sizes);
9588 }
9589 }
9590
9591 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9592 c_finish_omp_declare_simd for function prototypes. No diagnostics
9593 should be done. */
9594
9595 void
9596 temp_store_parm_decls (tree fndecl, tree parms)
9597 {
9598 push_scope ();
9599 for (tree p = parms; p; p = DECL_CHAIN (p))
9600 {
9601 DECL_CONTEXT (p) = fndecl;
9602 if (DECL_NAME (p))
9603 bind (DECL_NAME (p), p, current_scope,
9604 /*invisible=*/false, /*nested=*/false,
9605 UNKNOWN_LOCATION);
9606 }
9607 }
9608
9609 /* Undo what temp_store_parm_decls did. */
9610
9611 void
9612 temp_pop_parm_decls (void)
9613 {
9614 /* Clear all bindings in this temporary scope, so that
9615 pop_scope doesn't create a BLOCK. */
9616 struct c_binding *b = current_scope->bindings;
9617 current_scope->bindings = NULL;
9618 for (; b; b = free_binding_and_advance (b))
9619 {
9620 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9621 || b->decl == error_mark_node);
9622 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9623 I_SYMBOL_BINDING (b->id) = b->shadowed;
9624 if (b->shadowed && b->shadowed->u.type)
9625 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9626 }
9627 pop_scope ();
9628 }
9629 \f
9630
9631 /* Finish up a function declaration and compile that function
9632 all the way to assembler language output. Then free the storage
9633 for the function definition.
9634
9635 This is called after parsing the body of the function definition. */
9636
9637 void
9638 finish_function (void)
9639 {
9640 tree fndecl = current_function_decl;
9641
9642 if (c_dialect_objc ())
9643 objc_finish_function ();
9644
9645 if (TREE_CODE (fndecl) == FUNCTION_DECL
9646 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9647 {
9648 tree args = DECL_ARGUMENTS (fndecl);
9649 for (; args; args = DECL_CHAIN (args))
9650 {
9651 tree type = TREE_TYPE (args);
9652 if (INTEGRAL_TYPE_P (type)
9653 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9654 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9655 }
9656 }
9657
9658 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9659 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9660
9661 /* Must mark the RESULT_DECL as being in this function. */
9662
9663 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9664 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9665
9666 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9667 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9668 == integer_type_node && flag_isoc99)
9669 {
9670 /* Hack. We don't want the middle-end to warn that this return
9671 is unreachable, so we mark its location as special. Using
9672 UNKNOWN_LOCATION has the problem that it gets clobbered in
9673 annotate_one_with_locus. A cleaner solution might be to
9674 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9675 */
9676 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9677 }
9678
9679 /* Tie off the statement tree for this function. */
9680 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9681
9682 finish_fname_decls ();
9683
9684 /* Complain if there's no return statement only if option specified on
9685 command line. */
9686 if (warn_return_type > 0
9687 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9688 && !current_function_returns_value && !current_function_returns_null
9689 /* Don't complain if we are no-return. */
9690 && !current_function_returns_abnormally
9691 /* Don't complain if we are declared noreturn. */
9692 && !TREE_THIS_VOLATILE (fndecl)
9693 /* Don't warn for main(). */
9694 && !MAIN_NAME_P (DECL_NAME (fndecl))
9695 /* Or if they didn't actually specify a return type. */
9696 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9697 /* Normally, with -Wreturn-type, flow will complain, but we might
9698 optimize out static functions. */
9699 && !TREE_PUBLIC (fndecl)
9700 && targetm.warn_func_return (fndecl)
9701 && warning (OPT_Wreturn_type,
9702 "no return statement in function returning non-void"))
9703 TREE_NO_WARNING (fndecl) = 1;
9704
9705 /* Complain about parameters that are only set, but never otherwise used. */
9706 if (warn_unused_but_set_parameter)
9707 {
9708 tree decl;
9709
9710 for (decl = DECL_ARGUMENTS (fndecl);
9711 decl;
9712 decl = DECL_CHAIN (decl))
9713 if (TREE_USED (decl)
9714 && TREE_CODE (decl) == PARM_DECL
9715 && !DECL_READ_P (decl)
9716 && DECL_NAME (decl)
9717 && !DECL_ARTIFICIAL (decl)
9718 && !TREE_NO_WARNING (decl))
9719 warning_at (DECL_SOURCE_LOCATION (decl),
9720 OPT_Wunused_but_set_parameter,
9721 "parameter %qD set but not used", decl);
9722 }
9723
9724 /* Complain about locally defined typedefs that are not used in this
9725 function. */
9726 maybe_warn_unused_local_typedefs ();
9727
9728 /* Possibly warn about unused parameters. */
9729 if (warn_unused_parameter)
9730 do_warn_unused_parameter (fndecl);
9731
9732 /* Store the end of the function, so that we get good line number
9733 info for the epilogue. */
9734 cfun->function_end_locus = input_location;
9735
9736 /* Finalize the ELF visibility for the function. */
9737 c_determine_visibility (fndecl);
9738
9739 /* For GNU C extern inline functions disregard inline limits. */
9740 if (DECL_EXTERNAL (fndecl)
9741 && DECL_DECLARED_INLINE_P (fndecl)
9742 && (flag_gnu89_inline
9743 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
9744 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9745
9746 /* Genericize before inlining. Delay genericizing nested functions
9747 until their parent function is genericized. Since finalizing
9748 requires GENERIC, delay that as well. */
9749
9750 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9751 && !undef_nested_function)
9752 {
9753 if (!decl_function_context (fndecl))
9754 {
9755 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9756 c_genericize (fndecl);
9757
9758 /* ??? Objc emits functions after finalizing the compilation unit.
9759 This should be cleaned up later and this conditional removed. */
9760 if (symtab->global_info_ready)
9761 {
9762 cgraph_node::add_new_function (fndecl, false);
9763 return;
9764 }
9765 cgraph_node::finalize_function (fndecl, false);
9766 }
9767 else
9768 {
9769 /* Register this function with cgraph just far enough to get it
9770 added to our parent's nested function list. Handy, since the
9771 C front end doesn't have such a list. */
9772 (void) cgraph_node::get_create (fndecl);
9773 }
9774 }
9775
9776 if (!decl_function_context (fndecl))
9777 undef_nested_function = false;
9778
9779 if (cfun->language != NULL)
9780 {
9781 ggc_free (cfun->language);
9782 cfun->language = NULL;
9783 }
9784
9785 /* We're leaving the context of this function, so zap cfun.
9786 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9787 tree_rest_of_compilation. */
9788 set_cfun (NULL);
9789 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9790 current_function_decl = NULL;
9791 }
9792 \f
9793 /* Check the declarations given in a for-loop for satisfying the C99
9794 constraints. If exactly one such decl is found, return it. LOC is
9795 the location of the opening parenthesis of the for loop. The last
9796 parameter allows you to control the "for loop initial declarations
9797 are only allowed in C99 mode". Normally, you should pass
9798 flag_isoc99 as that parameter. But in some cases (Objective-C
9799 foreach loop, for example) we want to run the checks in this
9800 function even if not in C99 mode, so we allow the caller to turn
9801 off the error about not being in C99 mode.
9802 */
9803
9804 tree
9805 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9806 {
9807 struct c_binding *b;
9808 tree one_decl = NULL_TREE;
9809 int n_decls = 0;
9810
9811 if (!turn_off_iso_c99_error)
9812 {
9813 static bool hint = true;
9814 /* If we get here, declarations have been used in a for loop without
9815 the C99 for loop scope. This doesn't make much sense, so don't
9816 allow it. */
9817 error_at (loc, "%<for%> loop initial declarations "
9818 "are only allowed in C99 or C11 mode");
9819 if (hint)
9820 {
9821 inform (loc,
9822 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
9823 "%<-std=gnu11%> to compile your code");
9824 hint = false;
9825 }
9826 return NULL_TREE;
9827 }
9828 else
9829 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
9830 "initial declarations");
9831
9832 /* C99 subclause 6.8.5 paragraph 3:
9833
9834 [#3] The declaration part of a for statement shall only
9835 declare identifiers for objects having storage class auto or
9836 register.
9837
9838 It isn't clear whether, in this sentence, "identifiers" binds to
9839 "shall only declare" or to "objects" - that is, whether all identifiers
9840 declared must be identifiers for objects, or whether the restriction
9841 only applies to those that are. (A question on this in comp.std.c
9842 in November 2000 received no answer.) We implement the strictest
9843 interpretation, to avoid creating an extension which later causes
9844 problems. */
9845
9846 for (b = current_scope->bindings; b; b = b->prev)
9847 {
9848 tree id = b->id;
9849 tree decl = b->decl;
9850
9851 if (!id)
9852 continue;
9853
9854 switch (TREE_CODE (decl))
9855 {
9856 case VAR_DECL:
9857 {
9858 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9859 if (TREE_STATIC (decl))
9860 error_at (decl_loc,
9861 "declaration of static variable %qD in %<for%> loop "
9862 "initial declaration", decl);
9863 else if (DECL_EXTERNAL (decl))
9864 error_at (decl_loc,
9865 "declaration of %<extern%> variable %qD in %<for%> loop "
9866 "initial declaration", decl);
9867 }
9868 break;
9869
9870 case RECORD_TYPE:
9871 error_at (loc,
9872 "%<struct %E%> declared in %<for%> loop initial "
9873 "declaration", id);
9874 break;
9875 case UNION_TYPE:
9876 error_at (loc,
9877 "%<union %E%> declared in %<for%> loop initial declaration",
9878 id);
9879 break;
9880 case ENUMERAL_TYPE:
9881 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9882 "initial declaration", id);
9883 break;
9884 default:
9885 error_at (loc, "declaration of non-variable "
9886 "%qD in %<for%> loop initial declaration", decl);
9887 }
9888
9889 n_decls++;
9890 one_decl = decl;
9891 }
9892
9893 return n_decls == 1 ? one_decl : NULL_TREE;
9894 }
9895 \f
9896 /* Save and reinitialize the variables
9897 used during compilation of a C function. */
9898
9899 void
9900 c_push_function_context (void)
9901 {
9902 struct language_function *p = cfun->language;
9903 /* cfun->language might have been already allocated by the use of
9904 -Wunused-local-typedefs. In that case, just re-use it. */
9905 if (p == NULL)
9906 cfun->language = p = ggc_cleared_alloc<language_function> ();
9907
9908 p->base.x_stmt_tree = c_stmt_tree;
9909 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9910 p->x_break_label = c_break_label;
9911 p->x_cont_label = c_cont_label;
9912 p->x_switch_stack = c_switch_stack;
9913 p->arg_info = current_function_arg_info;
9914 p->returns_value = current_function_returns_value;
9915 p->returns_null = current_function_returns_null;
9916 p->returns_abnormally = current_function_returns_abnormally;
9917 p->warn_about_return_type = warn_about_return_type;
9918
9919 push_function_context ();
9920 }
9921
9922 /* Restore the variables used during compilation of a C function. */
9923
9924 void
9925 c_pop_function_context (void)
9926 {
9927 struct language_function *p;
9928
9929 pop_function_context ();
9930 p = cfun->language;
9931
9932 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9933 used to store data throughout the life time of the current cfun,
9934 So don't deallocate it. */
9935 if (!warn_unused_local_typedefs)
9936 cfun->language = NULL;
9937
9938 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9939 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9940 {
9941 /* Stop pointing to the local nodes about to be freed. */
9942 /* But DECL_INITIAL must remain nonzero so we know this
9943 was an actual function definition. */
9944 DECL_INITIAL (current_function_decl) = error_mark_node;
9945 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
9946 }
9947
9948 c_stmt_tree = p->base.x_stmt_tree;
9949 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9950 c_break_label = p->x_break_label;
9951 c_cont_label = p->x_cont_label;
9952 c_switch_stack = p->x_switch_stack;
9953 current_function_arg_info = p->arg_info;
9954 current_function_returns_value = p->returns_value;
9955 current_function_returns_null = p->returns_null;
9956 current_function_returns_abnormally = p->returns_abnormally;
9957 warn_about_return_type = p->warn_about_return_type;
9958 }
9959
9960 /* The functions below are required for functionality of doing
9961 function at once processing in the C front end. Currently these
9962 functions are not called from anywhere in the C front end, but as
9963 these changes continue, that will change. */
9964
9965 /* Returns the stmt_tree (if any) to which statements are currently
9966 being added. If there is no active statement-tree, NULL is
9967 returned. */
9968
9969 stmt_tree
9970 current_stmt_tree (void)
9971 {
9972 return &c_stmt_tree;
9973 }
9974
9975 /* Return the global value of T as a symbol. */
9976
9977 tree
9978 identifier_global_value (tree t)
9979 {
9980 struct c_binding *b;
9981
9982 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9983 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9984 return b->decl;
9985
9986 return NULL_TREE;
9987 }
9988
9989 /* In C, the only C-linkage public declaration is at file scope. */
9990
9991 tree
9992 c_linkage_bindings (tree name)
9993 {
9994 return identifier_global_value (name);
9995 }
9996
9997 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9998 otherwise the name is found in ridpointers from RID_INDEX. */
9999
10000 void
10001 record_builtin_type (enum rid rid_index, const char *name, tree type)
10002 {
10003 tree id, decl;
10004 if (name == 0)
10005 id = ridpointers[(int) rid_index];
10006 else
10007 id = get_identifier (name);
10008 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
10009 pushdecl (decl);
10010 if (debug_hooks->type_decl)
10011 debug_hooks->type_decl (decl, false);
10012 }
10013
10014 /* Build the void_list_node (void_type_node having been created). */
10015 tree
10016 build_void_list_node (void)
10017 {
10018 tree t = build_tree_list (NULL_TREE, void_type_node);
10019 return t;
10020 }
10021
10022 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
10023
10024 struct c_parm *
10025 build_c_parm (struct c_declspecs *specs, tree attrs,
10026 struct c_declarator *declarator,
10027 location_t loc)
10028 {
10029 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
10030 ret->specs = specs;
10031 ret->attrs = attrs;
10032 ret->declarator = declarator;
10033 ret->loc = loc;
10034 return ret;
10035 }
10036
10037 /* Return a declarator with nested attributes. TARGET is the inner
10038 declarator to which these attributes apply. ATTRS are the
10039 attributes. */
10040
10041 struct c_declarator *
10042 build_attrs_declarator (tree attrs, struct c_declarator *target)
10043 {
10044 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10045 ret->kind = cdk_attrs;
10046 ret->declarator = target;
10047 ret->u.attrs = attrs;
10048 return ret;
10049 }
10050
10051 /* Return a declarator for a function with arguments specified by ARGS
10052 and return type specified by TARGET. */
10053
10054 struct c_declarator *
10055 build_function_declarator (struct c_arg_info *args,
10056 struct c_declarator *target)
10057 {
10058 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10059 ret->kind = cdk_function;
10060 ret->declarator = target;
10061 ret->u.arg_info = args;
10062 return ret;
10063 }
10064
10065 /* Return a declarator for the identifier IDENT (which may be
10066 NULL_TREE for an abstract declarator). */
10067
10068 struct c_declarator *
10069 build_id_declarator (tree ident)
10070 {
10071 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10072 ret->kind = cdk_id;
10073 ret->declarator = 0;
10074 ret->u.id = ident;
10075 /* Default value - may get reset to a more precise location. */
10076 ret->id_loc = input_location;
10077 return ret;
10078 }
10079
10080 /* Return something to represent absolute declarators containing a *.
10081 TARGET is the absolute declarator that the * contains.
10082 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
10083 to apply to the pointer type. */
10084
10085 struct c_declarator *
10086 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
10087 struct c_declarator *target)
10088 {
10089 tree attrs;
10090 int quals = 0;
10091 struct c_declarator *itarget = target;
10092 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10093 if (type_quals_attrs)
10094 {
10095 attrs = type_quals_attrs->attrs;
10096 quals = quals_from_declspecs (type_quals_attrs);
10097 if (attrs != NULL_TREE)
10098 itarget = build_attrs_declarator (attrs, target);
10099 }
10100 ret->kind = cdk_pointer;
10101 ret->declarator = itarget;
10102 ret->u.pointer_quals = quals;
10103 return ret;
10104 }
10105
10106 /* Return a pointer to a structure for an empty list of declaration
10107 specifiers. */
10108
10109 struct c_declspecs *
10110 build_null_declspecs (void)
10111 {
10112 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
10113 memset (ret, 0, sizeof *ret);
10114 ret->align_log = -1;
10115 ret->typespec_word = cts_none;
10116 ret->storage_class = csc_none;
10117 ret->expr_const_operands = true;
10118 ret->typespec_kind = ctsk_none;
10119 ret->address_space = ADDR_SPACE_GENERIC;
10120 return ret;
10121 }
10122
10123 /* Add the address space ADDRSPACE to the declaration specifiers
10124 SPECS, returning SPECS. */
10125
10126 struct c_declspecs *
10127 declspecs_add_addrspace (location_t location,
10128 struct c_declspecs *specs, addr_space_t as)
10129 {
10130 specs->non_sc_seen_p = true;
10131 specs->declspecs_seen_p = true;
10132
10133 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
10134 && specs->address_space != as)
10135 error ("incompatible address space qualifiers %qs and %qs",
10136 c_addr_space_name (as),
10137 c_addr_space_name (specs->address_space));
10138 else
10139 {
10140 specs->address_space = as;
10141 specs->locations[cdw_address_space] = location;
10142 }
10143 return specs;
10144 }
10145
10146 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
10147 returning SPECS. */
10148
10149 struct c_declspecs *
10150 declspecs_add_qual (location_t loc,
10151 struct c_declspecs *specs, tree qual)
10152 {
10153 enum rid i;
10154 bool dupe = false;
10155 specs->non_sc_seen_p = true;
10156 specs->declspecs_seen_p = true;
10157 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
10158 && C_IS_RESERVED_WORD (qual));
10159 i = C_RID_CODE (qual);
10160 location_t prev_loc = UNKNOWN_LOCATION;
10161 switch (i)
10162 {
10163 case RID_CONST:
10164 dupe = specs->const_p;
10165 specs->const_p = true;
10166 prev_loc = specs->locations[cdw_const];
10167 specs->locations[cdw_const] = loc;
10168 break;
10169 case RID_VOLATILE:
10170 dupe = specs->volatile_p;
10171 specs->volatile_p = true;
10172 prev_loc = specs->locations[cdw_volatile];
10173 specs->locations[cdw_volatile] = loc;
10174 break;
10175 case RID_RESTRICT:
10176 dupe = specs->restrict_p;
10177 specs->restrict_p = true;
10178 prev_loc = specs->locations[cdw_restrict];
10179 specs->locations[cdw_restrict] = loc;
10180 break;
10181 case RID_ATOMIC:
10182 dupe = specs->atomic_p;
10183 specs->atomic_p = true;
10184 prev_loc = specs->locations[cdw_atomic];
10185 specs->locations[cdw_atomic] = loc;
10186 break;
10187 default:
10188 gcc_unreachable ();
10189 }
10190 if (dupe)
10191 {
10192 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
10193 "duplicate %qE declaration specifier", qual);
10194 if (!warned
10195 && warn_duplicate_decl_specifier
10196 && prev_loc >= RESERVED_LOCATION_COUNT
10197 && !from_macro_expansion_at (prev_loc)
10198 && !from_macro_expansion_at (loc))
10199 warning_at (loc, OPT_Wduplicate_decl_specifier,
10200 "duplicate %qE declaration specifier", qual);
10201 }
10202 return specs;
10203 }
10204
10205 /* Add the type specifier TYPE to the declaration specifiers SPECS,
10206 returning SPECS. */
10207
10208 struct c_declspecs *
10209 declspecs_add_type (location_t loc, struct c_declspecs *specs,
10210 struct c_typespec spec)
10211 {
10212 tree type = spec.spec;
10213 specs->non_sc_seen_p = true;
10214 specs->declspecs_seen_p = true;
10215 specs->typespec_kind = spec.kind;
10216 if (TREE_DEPRECATED (type))
10217 specs->deprecated_p = true;
10218
10219 /* Handle type specifier keywords. */
10220 if (TREE_CODE (type) == IDENTIFIER_NODE
10221 && C_IS_RESERVED_WORD (type)
10222 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
10223 {
10224 enum rid i = C_RID_CODE (type);
10225 if (specs->type)
10226 {
10227 error_at (loc, "two or more data types in declaration specifiers");
10228 return specs;
10229 }
10230 if ((int) i <= (int) RID_LAST_MODIFIER)
10231 {
10232 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
10233 bool dupe = false;
10234 switch (i)
10235 {
10236 case RID_LONG:
10237 if (specs->long_long_p)
10238 {
10239 error_at (loc, "%<long long long%> is too long for GCC");
10240 break;
10241 }
10242 if (specs->long_p)
10243 {
10244 if (specs->typespec_word == cts_double)
10245 {
10246 error_at (loc,
10247 ("both %<long long%> and %<double%> in "
10248 "declaration specifiers"));
10249 break;
10250 }
10251 pedwarn_c90 (loc, OPT_Wlong_long,
10252 "ISO C90 does not support %<long long%>");
10253 specs->long_long_p = 1;
10254 specs->locations[cdw_long_long] = loc;
10255 break;
10256 }
10257 if (specs->short_p)
10258 error_at (loc,
10259 ("both %<long%> and %<short%> in "
10260 "declaration specifiers"));
10261 else if (specs->typespec_word == cts_auto_type)
10262 error_at (loc,
10263 ("both %<long%> and %<__auto_type%> in "
10264 "declaration specifiers"));
10265 else if (specs->typespec_word == cts_void)
10266 error_at (loc,
10267 ("both %<long%> and %<void%> in "
10268 "declaration specifiers"));
10269 else if (specs->typespec_word == cts_int_n)
10270 error_at (loc,
10271 ("both %<long%> and %<__int%d%> in "
10272 "declaration specifiers"),
10273 int_n_data[specs->int_n_idx].bitsize);
10274 else if (specs->typespec_word == cts_bool)
10275 error_at (loc,
10276 ("both %<long%> and %<_Bool%> in "
10277 "declaration specifiers"));
10278 else if (specs->typespec_word == cts_char)
10279 error_at (loc,
10280 ("both %<long%> and %<char%> in "
10281 "declaration specifiers"));
10282 else if (specs->typespec_word == cts_float)
10283 error_at (loc,
10284 ("both %<long%> and %<float%> in "
10285 "declaration specifiers"));
10286 else if (specs->typespec_word == cts_floatn_nx)
10287 error_at (loc,
10288 ("both %<long%> and %<_Float%d%s%> in "
10289 "declaration specifiers"),
10290 floatn_nx_types[specs->floatn_nx_idx].n,
10291 (floatn_nx_types[specs->floatn_nx_idx].extended
10292 ? "x"
10293 : ""));
10294 else if (specs->typespec_word == cts_dfloat32)
10295 error_at (loc,
10296 ("both %<long%> and %<_Decimal32%> in "
10297 "declaration specifiers"));
10298 else if (specs->typespec_word == cts_dfloat64)
10299 error_at (loc,
10300 ("both %<long%> and %<_Decimal64%> in "
10301 "declaration specifiers"));
10302 else if (specs->typespec_word == cts_dfloat128)
10303 error_at (loc,
10304 ("both %<long%> and %<_Decimal128%> in "
10305 "declaration specifiers"));
10306 else
10307 {
10308 specs->long_p = true;
10309 specs->locations[cdw_long] = loc;
10310 }
10311 break;
10312 case RID_SHORT:
10313 dupe = specs->short_p;
10314 if (specs->long_p)
10315 error_at (loc,
10316 ("both %<long%> and %<short%> in "
10317 "declaration specifiers"));
10318 else if (specs->typespec_word == cts_auto_type)
10319 error_at (loc,
10320 ("both %<short%> and %<__auto_type%> in "
10321 "declaration specifiers"));
10322 else if (specs->typespec_word == cts_void)
10323 error_at (loc,
10324 ("both %<short%> and %<void%> in "
10325 "declaration specifiers"));
10326 else if (specs->typespec_word == cts_int_n)
10327 error_at (loc,
10328 ("both %<short%> and %<__int%d%> in "
10329 "declaration specifiers"),
10330 int_n_data[specs->int_n_idx].bitsize);
10331 else if (specs->typespec_word == cts_bool)
10332 error_at (loc,
10333 ("both %<short%> and %<_Bool%> in "
10334 "declaration specifiers"));
10335 else if (specs->typespec_word == cts_char)
10336 error_at (loc,
10337 ("both %<short%> and %<char%> in "
10338 "declaration specifiers"));
10339 else if (specs->typespec_word == cts_float)
10340 error_at (loc,
10341 ("both %<short%> and %<float%> in "
10342 "declaration specifiers"));
10343 else if (specs->typespec_word == cts_double)
10344 error_at (loc,
10345 ("both %<short%> and %<double%> in "
10346 "declaration specifiers"));
10347 else if (specs->typespec_word == cts_floatn_nx)
10348 error_at (loc,
10349 ("both %<short%> and %<_Float%d%s%> in "
10350 "declaration specifiers"),
10351 floatn_nx_types[specs->floatn_nx_idx].n,
10352 (floatn_nx_types[specs->floatn_nx_idx].extended
10353 ? "x"
10354 : ""));
10355 else if (specs->typespec_word == cts_dfloat32)
10356 error_at (loc,
10357 ("both %<short%> and %<_Decimal32%> in "
10358 "declaration specifiers"));
10359 else if (specs->typespec_word == cts_dfloat64)
10360 error_at (loc,
10361 ("both %<short%> and %<_Decimal64%> in "
10362 "declaration specifiers"));
10363 else if (specs->typespec_word == cts_dfloat128)
10364 error_at (loc,
10365 ("both %<short%> and %<_Decimal128%> in "
10366 "declaration specifiers"));
10367 else
10368 {
10369 specs->short_p = true;
10370 specs->locations[cdw_short] = loc;
10371 }
10372 break;
10373 case RID_SIGNED:
10374 dupe = specs->signed_p;
10375 if (specs->unsigned_p)
10376 error_at (loc,
10377 ("both %<signed%> and %<unsigned%> in "
10378 "declaration specifiers"));
10379 else if (specs->typespec_word == cts_auto_type)
10380 error_at (loc,
10381 ("both %<signed%> and %<__auto_type%> in "
10382 "declaration specifiers"));
10383 else if (specs->typespec_word == cts_void)
10384 error_at (loc,
10385 ("both %<signed%> and %<void%> in "
10386 "declaration specifiers"));
10387 else if (specs->typespec_word == cts_bool)
10388 error_at (loc,
10389 ("both %<signed%> and %<_Bool%> in "
10390 "declaration specifiers"));
10391 else if (specs->typespec_word == cts_float)
10392 error_at (loc,
10393 ("both %<signed%> and %<float%> in "
10394 "declaration specifiers"));
10395 else if (specs->typespec_word == cts_double)
10396 error_at (loc,
10397 ("both %<signed%> and %<double%> in "
10398 "declaration specifiers"));
10399 else if (specs->typespec_word == cts_floatn_nx)
10400 error_at (loc,
10401 ("both %<signed%> and %<_Float%d%s%> in "
10402 "declaration specifiers"),
10403 floatn_nx_types[specs->floatn_nx_idx].n,
10404 (floatn_nx_types[specs->floatn_nx_idx].extended
10405 ? "x"
10406 : ""));
10407 else if (specs->typespec_word == cts_dfloat32)
10408 error_at (loc,
10409 ("both %<signed%> and %<_Decimal32%> in "
10410 "declaration specifiers"));
10411 else if (specs->typespec_word == cts_dfloat64)
10412 error_at (loc,
10413 ("both %<signed%> and %<_Decimal64%> in "
10414 "declaration specifiers"));
10415 else if (specs->typespec_word == cts_dfloat128)
10416 error_at (loc,
10417 ("both %<signed%> and %<_Decimal128%> in "
10418 "declaration specifiers"));
10419 else
10420 {
10421 specs->signed_p = true;
10422 specs->locations[cdw_signed] = loc;
10423 }
10424 break;
10425 case RID_UNSIGNED:
10426 dupe = specs->unsigned_p;
10427 if (specs->signed_p)
10428 error_at (loc,
10429 ("both %<signed%> and %<unsigned%> in "
10430 "declaration specifiers"));
10431 else if (specs->typespec_word == cts_auto_type)
10432 error_at (loc,
10433 ("both %<unsigned%> and %<__auto_type%> in "
10434 "declaration specifiers"));
10435 else if (specs->typespec_word == cts_void)
10436 error_at (loc,
10437 ("both %<unsigned%> and %<void%> in "
10438 "declaration specifiers"));
10439 else if (specs->typespec_word == cts_bool)
10440 error_at (loc,
10441 ("both %<unsigned%> and %<_Bool%> in "
10442 "declaration specifiers"));
10443 else if (specs->typespec_word == cts_float)
10444 error_at (loc,
10445 ("both %<unsigned%> and %<float%> in "
10446 "declaration specifiers"));
10447 else if (specs->typespec_word == cts_double)
10448 error_at (loc,
10449 ("both %<unsigned%> and %<double%> in "
10450 "declaration specifiers"));
10451 else if (specs->typespec_word == cts_floatn_nx)
10452 error_at (loc,
10453 ("both %<unsigned%> and %<_Float%d%s%> in "
10454 "declaration specifiers"),
10455 floatn_nx_types[specs->floatn_nx_idx].n,
10456 (floatn_nx_types[specs->floatn_nx_idx].extended
10457 ? "x"
10458 : ""));
10459 else if (specs->typespec_word == cts_dfloat32)
10460 error_at (loc,
10461 ("both %<unsigned%> and %<_Decimal32%> in "
10462 "declaration specifiers"));
10463 else if (specs->typespec_word == cts_dfloat64)
10464 error_at (loc,
10465 ("both %<unsigned%> and %<_Decimal64%> in "
10466 "declaration specifiers"));
10467 else if (specs->typespec_word == cts_dfloat128)
10468 error_at (loc,
10469 ("both %<unsigned%> and %<_Decimal128%> in "
10470 "declaration specifiers"));
10471 else
10472 {
10473 specs->unsigned_p = true;
10474 specs->locations[cdw_unsigned] = loc;
10475 }
10476 break;
10477 case RID_COMPLEX:
10478 dupe = specs->complex_p;
10479 if (!in_system_header_at (loc))
10480 pedwarn_c90 (loc, OPT_Wpedantic,
10481 "ISO C90 does not support complex types");
10482 if (specs->typespec_word == cts_auto_type)
10483 error_at (loc,
10484 ("both %<complex%> and %<__auto_type%> in "
10485 "declaration specifiers"));
10486 else if (specs->typespec_word == cts_void)
10487 error_at (loc,
10488 ("both %<complex%> and %<void%> in "
10489 "declaration specifiers"));
10490 else if (specs->typespec_word == cts_bool)
10491 error_at (loc,
10492 ("both %<complex%> and %<_Bool%> in "
10493 "declaration specifiers"));
10494 else if (specs->typespec_word == cts_dfloat32)
10495 error_at (loc,
10496 ("both %<complex%> and %<_Decimal32%> in "
10497 "declaration specifiers"));
10498 else if (specs->typespec_word == cts_dfloat64)
10499 error_at (loc,
10500 ("both %<complex%> and %<_Decimal64%> in "
10501 "declaration specifiers"));
10502 else if (specs->typespec_word == cts_dfloat128)
10503 error_at (loc,
10504 ("both %<complex%> and %<_Decimal128%> in "
10505 "declaration specifiers"));
10506 else if (specs->typespec_word == cts_fract)
10507 error_at (loc,
10508 ("both %<complex%> and %<_Fract%> in "
10509 "declaration specifiers"));
10510 else if (specs->typespec_word == cts_accum)
10511 error_at (loc,
10512 ("both %<complex%> and %<_Accum%> in "
10513 "declaration specifiers"));
10514 else if (specs->saturating_p)
10515 error_at (loc,
10516 ("both %<complex%> and %<_Sat%> in "
10517 "declaration specifiers"));
10518 else
10519 {
10520 specs->complex_p = true;
10521 specs->locations[cdw_complex] = loc;
10522 }
10523 break;
10524 case RID_SAT:
10525 dupe = specs->saturating_p;
10526 pedwarn (loc, OPT_Wpedantic,
10527 "ISO C does not support saturating types");
10528 if (specs->typespec_word == cts_int_n)
10529 {
10530 error_at (loc,
10531 ("both %<_Sat%> and %<__int%d%> in "
10532 "declaration specifiers"),
10533 int_n_data[specs->int_n_idx].bitsize);
10534 }
10535 else if (specs->typespec_word == cts_auto_type)
10536 error_at (loc,
10537 ("both %<_Sat%> and %<__auto_type%> in "
10538 "declaration specifiers"));
10539 else if (specs->typespec_word == cts_void)
10540 error_at (loc,
10541 ("both %<_Sat%> and %<void%> in "
10542 "declaration specifiers"));
10543 else if (specs->typespec_word == cts_bool)
10544 error_at (loc,
10545 ("both %<_Sat%> and %<_Bool%> in "
10546 "declaration specifiers"));
10547 else if (specs->typespec_word == cts_char)
10548 error_at (loc,
10549 ("both %<_Sat%> and %<char%> in "
10550 "declaration specifiers"));
10551 else if (specs->typespec_word == cts_int)
10552 error_at (loc,
10553 ("both %<_Sat%> and %<int%> in "
10554 "declaration specifiers"));
10555 else if (specs->typespec_word == cts_float)
10556 error_at (loc,
10557 ("both %<_Sat%> and %<float%> in "
10558 "declaration specifiers"));
10559 else if (specs->typespec_word == cts_double)
10560 error_at (loc,
10561 ("both %<_Sat%> and %<double%> in "
10562 "declaration specifiers"));
10563 else if (specs->typespec_word == cts_floatn_nx)
10564 error_at (loc,
10565 ("both %<_Sat%> and %<_Float%d%s%> in "
10566 "declaration specifiers"),
10567 floatn_nx_types[specs->floatn_nx_idx].n,
10568 (floatn_nx_types[specs->floatn_nx_idx].extended
10569 ? "x"
10570 : ""));
10571 else if (specs->typespec_word == cts_dfloat32)
10572 error_at (loc,
10573 ("both %<_Sat%> and %<_Decimal32%> in "
10574 "declaration specifiers"));
10575 else if (specs->typespec_word == cts_dfloat64)
10576 error_at (loc,
10577 ("both %<_Sat%> and %<_Decimal64%> in "
10578 "declaration specifiers"));
10579 else if (specs->typespec_word == cts_dfloat128)
10580 error_at (loc,
10581 ("both %<_Sat%> and %<_Decimal128%> in "
10582 "declaration specifiers"));
10583 else if (specs->complex_p)
10584 error_at (loc,
10585 ("both %<_Sat%> and %<complex%> in "
10586 "declaration specifiers"));
10587 else
10588 {
10589 specs->saturating_p = true;
10590 specs->locations[cdw_saturating] = loc;
10591 }
10592 break;
10593 default:
10594 gcc_unreachable ();
10595 }
10596
10597 if (dupe)
10598 error_at (loc, "duplicate %qE", type);
10599
10600 return specs;
10601 }
10602 else
10603 {
10604 /* "void", "_Bool", "char", "int", "float", "double",
10605 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10606 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10607 "__auto_type". */
10608 if (specs->typespec_word != cts_none)
10609 {
10610 error_at (loc,
10611 "two or more data types in declaration specifiers");
10612 return specs;
10613 }
10614 switch (i)
10615 {
10616 case RID_AUTO_TYPE:
10617 if (specs->long_p)
10618 error_at (loc,
10619 ("both %<long%> and %<__auto_type%> in "
10620 "declaration specifiers"));
10621 else if (specs->short_p)
10622 error_at (loc,
10623 ("both %<short%> and %<__auto_type%> in "
10624 "declaration specifiers"));
10625 else if (specs->signed_p)
10626 error_at (loc,
10627 ("both %<signed%> and %<__auto_type%> in "
10628 "declaration specifiers"));
10629 else if (specs->unsigned_p)
10630 error_at (loc,
10631 ("both %<unsigned%> and %<__auto_type%> in "
10632 "declaration specifiers"));
10633 else if (specs->complex_p)
10634 error_at (loc,
10635 ("both %<complex%> and %<__auto_type%> in "
10636 "declaration specifiers"));
10637 else if (specs->saturating_p)
10638 error_at (loc,
10639 ("both %<_Sat%> and %<__auto_type%> in "
10640 "declaration specifiers"));
10641 else
10642 {
10643 specs->typespec_word = cts_auto_type;
10644 specs->locations[cdw_typespec] = loc;
10645 }
10646 return specs;
10647 case RID_INT_N_0:
10648 case RID_INT_N_1:
10649 case RID_INT_N_2:
10650 case RID_INT_N_3:
10651 specs->int_n_idx = i - RID_INT_N_0;
10652 if (!in_system_header_at (input_location)
10653 /* If the INT_N type ends in "__", and so is of the format
10654 "__intN__", don't pedwarn. */
10655 && (strncmp (IDENTIFIER_POINTER (type)
10656 + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
10657 pedwarn (loc, OPT_Wpedantic,
10658 "ISO C does not support %<__int%d%> types",
10659 int_n_data[specs->int_n_idx].bitsize);
10660
10661 if (specs->long_p)
10662 error_at (loc,
10663 ("both %<__int%d%> and %<long%> in "
10664 "declaration specifiers"),
10665 int_n_data[specs->int_n_idx].bitsize);
10666 else if (specs->saturating_p)
10667 error_at (loc,
10668 ("both %<_Sat%> and %<__int%d%> in "
10669 "declaration specifiers"),
10670 int_n_data[specs->int_n_idx].bitsize);
10671 else if (specs->short_p)
10672 error_at (loc,
10673 ("both %<__int%d%> and %<short%> in "
10674 "declaration specifiers"),
10675 int_n_data[specs->int_n_idx].bitsize);
10676 else if (! int_n_enabled_p[specs->int_n_idx])
10677 {
10678 specs->typespec_word = cts_int_n;
10679 error_at (loc,
10680 "%<__int%d%> is not supported on this target",
10681 int_n_data[specs->int_n_idx].bitsize);
10682 }
10683 else
10684 {
10685 specs->typespec_word = cts_int_n;
10686 specs->locations[cdw_typespec] = loc;
10687 }
10688 return specs;
10689 case RID_VOID:
10690 if (specs->long_p)
10691 error_at (loc,
10692 ("both %<long%> and %<void%> in "
10693 "declaration specifiers"));
10694 else if (specs->short_p)
10695 error_at (loc,
10696 ("both %<short%> and %<void%> in "
10697 "declaration specifiers"));
10698 else if (specs->signed_p)
10699 error_at (loc,
10700 ("both %<signed%> and %<void%> in "
10701 "declaration specifiers"));
10702 else if (specs->unsigned_p)
10703 error_at (loc,
10704 ("both %<unsigned%> and %<void%> in "
10705 "declaration specifiers"));
10706 else if (specs->complex_p)
10707 error_at (loc,
10708 ("both %<complex%> and %<void%> in "
10709 "declaration specifiers"));
10710 else if (specs->saturating_p)
10711 error_at (loc,
10712 ("both %<_Sat%> and %<void%> in "
10713 "declaration specifiers"));
10714 else
10715 {
10716 specs->typespec_word = cts_void;
10717 specs->locations[cdw_typespec] = loc;
10718 }
10719 return specs;
10720 case RID_BOOL:
10721 if (!in_system_header_at (loc))
10722 pedwarn_c90 (loc, OPT_Wpedantic,
10723 "ISO C90 does not support boolean types");
10724 if (specs->long_p)
10725 error_at (loc,
10726 ("both %<long%> and %<_Bool%> in "
10727 "declaration specifiers"));
10728 else if (specs->short_p)
10729 error_at (loc,
10730 ("both %<short%> and %<_Bool%> in "
10731 "declaration specifiers"));
10732 else if (specs->signed_p)
10733 error_at (loc,
10734 ("both %<signed%> and %<_Bool%> in "
10735 "declaration specifiers"));
10736 else if (specs->unsigned_p)
10737 error_at (loc,
10738 ("both %<unsigned%> and %<_Bool%> in "
10739 "declaration specifiers"));
10740 else if (specs->complex_p)
10741 error_at (loc,
10742 ("both %<complex%> and %<_Bool%> in "
10743 "declaration specifiers"));
10744 else if (specs->saturating_p)
10745 error_at (loc,
10746 ("both %<_Sat%> and %<_Bool%> in "
10747 "declaration specifiers"));
10748 else
10749 {
10750 specs->typespec_word = cts_bool;
10751 specs->locations[cdw_typespec] = loc;
10752 }
10753 return specs;
10754 case RID_CHAR:
10755 if (specs->long_p)
10756 error_at (loc,
10757 ("both %<long%> and %<char%> in "
10758 "declaration specifiers"));
10759 else if (specs->short_p)
10760 error_at (loc,
10761 ("both %<short%> and %<char%> in "
10762 "declaration specifiers"));
10763 else if (specs->saturating_p)
10764 error_at (loc,
10765 ("both %<_Sat%> and %<char%> in "
10766 "declaration specifiers"));
10767 else
10768 {
10769 specs->typespec_word = cts_char;
10770 specs->locations[cdw_typespec] = loc;
10771 }
10772 return specs;
10773 case RID_INT:
10774 if (specs->saturating_p)
10775 error_at (loc,
10776 ("both %<_Sat%> and %<int%> in "
10777 "declaration specifiers"));
10778 else
10779 {
10780 specs->typespec_word = cts_int;
10781 specs->locations[cdw_typespec] = loc;
10782 }
10783 return specs;
10784 case RID_FLOAT:
10785 if (specs->long_p)
10786 error_at (loc,
10787 ("both %<long%> and %<float%> in "
10788 "declaration specifiers"));
10789 else if (specs->short_p)
10790 error_at (loc,
10791 ("both %<short%> and %<float%> in "
10792 "declaration specifiers"));
10793 else if (specs->signed_p)
10794 error_at (loc,
10795 ("both %<signed%> and %<float%> in "
10796 "declaration specifiers"));
10797 else if (specs->unsigned_p)
10798 error_at (loc,
10799 ("both %<unsigned%> and %<float%> in "
10800 "declaration specifiers"));
10801 else if (specs->saturating_p)
10802 error_at (loc,
10803 ("both %<_Sat%> and %<float%> in "
10804 "declaration specifiers"));
10805 else
10806 {
10807 specs->typespec_word = cts_float;
10808 specs->locations[cdw_typespec] = loc;
10809 }
10810 return specs;
10811 case RID_DOUBLE:
10812 if (specs->long_long_p)
10813 error_at (loc,
10814 ("both %<long long%> and %<double%> in "
10815 "declaration specifiers"));
10816 else if (specs->short_p)
10817 error_at (loc,
10818 ("both %<short%> and %<double%> in "
10819 "declaration specifiers"));
10820 else if (specs->signed_p)
10821 error_at (loc,
10822 ("both %<signed%> and %<double%> in "
10823 "declaration specifiers"));
10824 else if (specs->unsigned_p)
10825 error_at (loc,
10826 ("both %<unsigned%> and %<double%> in "
10827 "declaration specifiers"));
10828 else if (specs->saturating_p)
10829 error_at (loc,
10830 ("both %<_Sat%> and %<double%> in "
10831 "declaration specifiers"));
10832 else
10833 {
10834 specs->typespec_word = cts_double;
10835 specs->locations[cdw_typespec] = loc;
10836 }
10837 return specs;
10838 CASE_RID_FLOATN_NX:
10839 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
10840 if (!in_system_header_at (input_location))
10841 pedwarn (loc, OPT_Wpedantic,
10842 "ISO C does not support the %<_Float%d%s%> type",
10843 floatn_nx_types[specs->floatn_nx_idx].n,
10844 (floatn_nx_types[specs->floatn_nx_idx].extended
10845 ? "x"
10846 : ""));
10847
10848 if (specs->long_p)
10849 error_at (loc,
10850 ("both %<long%> and %<_Float%d%s%> in "
10851 "declaration specifiers"),
10852 floatn_nx_types[specs->floatn_nx_idx].n,
10853 (floatn_nx_types[specs->floatn_nx_idx].extended
10854 ? "x"
10855 : ""));
10856 else if (specs->short_p)
10857 error_at (loc,
10858 ("both %<short%> and %<_Float%d%s%> in "
10859 "declaration specifiers"),
10860 floatn_nx_types[specs->floatn_nx_idx].n,
10861 (floatn_nx_types[specs->floatn_nx_idx].extended
10862 ? "x"
10863 : ""));
10864 else if (specs->signed_p)
10865 error_at (loc,
10866 ("both %<signed%> and %<_Float%d%s%> in "
10867 "declaration specifiers"),
10868 floatn_nx_types[specs->floatn_nx_idx].n,
10869 (floatn_nx_types[specs->floatn_nx_idx].extended
10870 ? "x"
10871 : ""));
10872 else if (specs->unsigned_p)
10873 error_at (loc,
10874 ("both %<unsigned%> and %<_Float%d%s%> in "
10875 "declaration specifiers"),
10876 floatn_nx_types[specs->floatn_nx_idx].n,
10877 (floatn_nx_types[specs->floatn_nx_idx].extended
10878 ? "x"
10879 : ""));
10880 else if (specs->saturating_p)
10881 error_at (loc,
10882 ("both %<_Sat%> and %<_Float%d%s%> in "
10883 "declaration specifiers"),
10884 floatn_nx_types[specs->floatn_nx_idx].n,
10885 (floatn_nx_types[specs->floatn_nx_idx].extended
10886 ? "x"
10887 : ""));
10888 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10889 {
10890 specs->typespec_word = cts_floatn_nx;
10891 error_at (loc,
10892 "%<_Float%d%s%> is not supported on this target",
10893 floatn_nx_types[specs->floatn_nx_idx].n,
10894 (floatn_nx_types[specs->floatn_nx_idx].extended
10895 ? "x"
10896 : ""));
10897 }
10898 else
10899 {
10900 specs->typespec_word = cts_floatn_nx;
10901 specs->locations[cdw_typespec] = loc;
10902 }
10903 return specs;
10904 case RID_DFLOAT32:
10905 case RID_DFLOAT64:
10906 case RID_DFLOAT128:
10907 {
10908 const char *str;
10909 if (i == RID_DFLOAT32)
10910 str = "_Decimal32";
10911 else if (i == RID_DFLOAT64)
10912 str = "_Decimal64";
10913 else
10914 str = "_Decimal128";
10915 if (specs->long_long_p)
10916 error_at (loc,
10917 ("both %<long long%> and %qs in "
10918 "declaration specifiers"),
10919 str);
10920 if (specs->long_p)
10921 error_at (loc,
10922 ("both %<long%> and %qs in "
10923 "declaration specifiers"),
10924 str);
10925 else if (specs->short_p)
10926 error_at (loc,
10927 ("both %<short%> and %qs in "
10928 "declaration specifiers"),
10929 str);
10930 else if (specs->signed_p)
10931 error_at (loc,
10932 ("both %<signed%> and %qs in "
10933 "declaration specifiers"),
10934 str);
10935 else if (specs->unsigned_p)
10936 error_at (loc,
10937 ("both %<unsigned%> and %qs in "
10938 "declaration specifiers"),
10939 str);
10940 else if (specs->complex_p)
10941 error_at (loc,
10942 ("both %<complex%> and %qs in "
10943 "declaration specifiers"),
10944 str);
10945 else if (specs->saturating_p)
10946 error_at (loc,
10947 ("both %<_Sat%> and %qs in "
10948 "declaration specifiers"),
10949 str);
10950 else if (i == RID_DFLOAT32)
10951 specs->typespec_word = cts_dfloat32;
10952 else if (i == RID_DFLOAT64)
10953 specs->typespec_word = cts_dfloat64;
10954 else
10955 specs->typespec_word = cts_dfloat128;
10956 specs->locations[cdw_typespec] = loc;
10957 }
10958 if (!targetm.decimal_float_supported_p ())
10959 error_at (loc,
10960 ("decimal floating-point not supported "
10961 "for this target"));
10962 pedwarn (loc, OPT_Wpedantic,
10963 "ISO C does not support decimal floating-point");
10964 return specs;
10965 case RID_FRACT:
10966 case RID_ACCUM:
10967 {
10968 const char *str;
10969 if (i == RID_FRACT)
10970 str = "_Fract";
10971 else
10972 str = "_Accum";
10973 if (specs->complex_p)
10974 error_at (loc,
10975 ("both %<complex%> and %qs in "
10976 "declaration specifiers"),
10977 str);
10978 else if (i == RID_FRACT)
10979 specs->typespec_word = cts_fract;
10980 else
10981 specs->typespec_word = cts_accum;
10982 specs->locations[cdw_typespec] = loc;
10983 }
10984 if (!targetm.fixed_point_supported_p ())
10985 error_at (loc,
10986 "fixed-point types not supported for this target");
10987 pedwarn (loc, OPT_Wpedantic,
10988 "ISO C does not support fixed-point types");
10989 return specs;
10990 default:
10991 /* ObjC reserved word "id", handled below. */
10992 break;
10993 }
10994 }
10995 }
10996
10997 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10998 form of ObjC type, cases such as "int" and "long" being handled
10999 above), a TYPE (struct, union, enum and typeof specifiers) or an
11000 ERROR_MARK. In none of these cases may there have previously
11001 been any type specifiers. */
11002 if (specs->type || specs->typespec_word != cts_none
11003 || specs->long_p || specs->short_p || specs->signed_p
11004 || specs->unsigned_p || specs->complex_p)
11005 error_at (loc, "two or more data types in declaration specifiers");
11006 else if (TREE_CODE (type) == TYPE_DECL)
11007 {
11008 if (TREE_TYPE (type) == error_mark_node)
11009 ; /* Allow the type to default to int to avoid cascading errors. */
11010 else
11011 {
11012 specs->type = TREE_TYPE (type);
11013 specs->decl_attr = DECL_ATTRIBUTES (type);
11014 specs->typedef_p = true;
11015 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
11016 specs->locations[cdw_typedef] = loc;
11017
11018 /* If this typedef name is defined in a struct, then a C++
11019 lookup would return a different value. */
11020 if (warn_cxx_compat
11021 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
11022 warning_at (loc, OPT_Wc___compat,
11023 "C++ lookup of %qD would return a field, not a type",
11024 type);
11025
11026 /* If we are parsing a struct, record that a struct field
11027 used a typedef. */
11028 if (warn_cxx_compat && struct_parse_info != NULL)
11029 struct_parse_info->typedefs_seen.safe_push (type);
11030 }
11031 }
11032 else if (TREE_CODE (type) == IDENTIFIER_NODE)
11033 {
11034 tree t = lookup_name (type);
11035 if (!t || TREE_CODE (t) != TYPE_DECL)
11036 error_at (loc, "%qE fails to be a typedef or built in type", type);
11037 else if (TREE_TYPE (t) == error_mark_node)
11038 ;
11039 else
11040 {
11041 specs->type = TREE_TYPE (t);
11042 specs->locations[cdw_typespec] = loc;
11043 }
11044 }
11045 else
11046 {
11047 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
11048 {
11049 specs->typedef_p = true;
11050 specs->locations[cdw_typedef] = loc;
11051 if (spec.expr)
11052 {
11053 if (specs->expr)
11054 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
11055 specs->expr, spec.expr);
11056 else
11057 specs->expr = spec.expr;
11058 specs->expr_const_operands &= spec.expr_const_operands;
11059 }
11060 }
11061 specs->type = type;
11062 }
11063
11064 return specs;
11065 }
11066
11067 /* Add the storage class specifier or function specifier SCSPEC to the
11068 declaration specifiers SPECS, returning SPECS. */
11069
11070 struct c_declspecs *
11071 declspecs_add_scspec (location_t loc,
11072 struct c_declspecs *specs,
11073 tree scspec)
11074 {
11075 enum rid i;
11076 enum c_storage_class n = csc_none;
11077 bool dupe = false;
11078 specs->declspecs_seen_p = true;
11079 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
11080 && C_IS_RESERVED_WORD (scspec));
11081 i = C_RID_CODE (scspec);
11082 if (specs->non_sc_seen_p)
11083 warning (OPT_Wold_style_declaration,
11084 "%qE is not at beginning of declaration", scspec);
11085 switch (i)
11086 {
11087 case RID_INLINE:
11088 /* C99 permits duplicate inline. Although of doubtful utility,
11089 it seems simplest to permit it in gnu89 mode as well, as
11090 there is also little utility in maintaining this as a
11091 difference between gnu89 and C99 inline. */
11092 dupe = false;
11093 specs->inline_p = true;
11094 specs->locations[cdw_inline] = loc;
11095 break;
11096 case RID_NORETURN:
11097 /* Duplicate _Noreturn is permitted. */
11098 dupe = false;
11099 specs->noreturn_p = true;
11100 specs->locations[cdw_noreturn] = loc;
11101 break;
11102 case RID_THREAD:
11103 dupe = specs->thread_p;
11104 if (specs->storage_class == csc_auto)
11105 error ("%qE used with %<auto%>", scspec);
11106 else if (specs->storage_class == csc_register)
11107 error ("%qE used with %<register%>", scspec);
11108 else if (specs->storage_class == csc_typedef)
11109 error ("%qE used with %<typedef%>", scspec);
11110 else
11111 {
11112 specs->thread_p = true;
11113 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
11114 "__thread") == 0);
11115 /* A diagnostic is not required for the use of this
11116 identifier in the implementation namespace; only diagnose
11117 it for the C11 spelling because of existing code using
11118 the other spelling. */
11119 if (!specs->thread_gnu_p)
11120 {
11121 if (flag_isoc99)
11122 pedwarn_c99 (loc, OPT_Wpedantic,
11123 "ISO C99 does not support %qE", scspec);
11124 else
11125 pedwarn_c99 (loc, OPT_Wpedantic,
11126 "ISO C90 does not support %qE", scspec);
11127 }
11128 specs->locations[cdw_thread] = loc;
11129 }
11130 break;
11131 case RID_AUTO:
11132 n = csc_auto;
11133 break;
11134 case RID_EXTERN:
11135 n = csc_extern;
11136 /* Diagnose "__thread extern". */
11137 if (specs->thread_p && specs->thread_gnu_p)
11138 error ("%<__thread%> before %<extern%>");
11139 break;
11140 case RID_REGISTER:
11141 n = csc_register;
11142 break;
11143 case RID_STATIC:
11144 n = csc_static;
11145 /* Diagnose "__thread static". */
11146 if (specs->thread_p && specs->thread_gnu_p)
11147 error ("%<__thread%> before %<static%>");
11148 break;
11149 case RID_TYPEDEF:
11150 n = csc_typedef;
11151 break;
11152 default:
11153 gcc_unreachable ();
11154 }
11155 if (n != csc_none && n == specs->storage_class)
11156 dupe = true;
11157 if (dupe)
11158 {
11159 if (i == RID_THREAD)
11160 error ("duplicate %<_Thread_local%> or %<__thread%>");
11161 else
11162 error ("duplicate %qE", scspec);
11163 }
11164 if (n != csc_none)
11165 {
11166 if (specs->storage_class != csc_none && n != specs->storage_class)
11167 {
11168 error ("multiple storage classes in declaration specifiers");
11169 }
11170 else
11171 {
11172 specs->storage_class = n;
11173 specs->locations[cdw_storage_class] = loc;
11174 if (n != csc_extern && n != csc_static && specs->thread_p)
11175 {
11176 error ("%qs used with %qE",
11177 specs->thread_gnu_p ? "__thread" : "_Thread_local",
11178 scspec);
11179 specs->thread_p = false;
11180 }
11181 }
11182 }
11183 return specs;
11184 }
11185
11186 /* Add the attributes ATTRS to the declaration specifiers SPECS,
11187 returning SPECS. */
11188
11189 struct c_declspecs *
11190 declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
11191 {
11192 specs->attrs = chainon (attrs, specs->attrs);
11193 specs->locations[cdw_attributes] = loc;
11194 specs->declspecs_seen_p = true;
11195 return specs;
11196 }
11197
11198 /* Add an _Alignas specifier (expression ALIGN, or type whose
11199 alignment is ALIGN) to the declaration specifiers SPECS, returning
11200 SPECS. */
11201 struct c_declspecs *
11202 declspecs_add_alignas (location_t loc,
11203 struct c_declspecs *specs, tree align)
11204 {
11205 specs->alignas_p = true;
11206 specs->locations[cdw_alignas] = loc;
11207 if (align == error_mark_node)
11208 return specs;
11209
11210 /* Only accept the alignment if it's valid and greater than
11211 the current one. Zero is invalid but by C11 required to
11212 be silently ignored. */
11213 int align_log = check_user_alignment (align, false, /* warn_zero = */false);
11214 if (align_log > specs->align_log)
11215 specs->align_log = align_log;
11216 return specs;
11217 }
11218
11219 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
11220 specifiers with any other type specifier to determine the resulting
11221 type. This is where ISO C checks on complex types are made, since
11222 "_Complex long" is a prefix of the valid ISO C type "_Complex long
11223 double". */
11224
11225 struct c_declspecs *
11226 finish_declspecs (struct c_declspecs *specs)
11227 {
11228 /* If a type was specified as a whole, we have no modifiers and are
11229 done. */
11230 if (specs->type != NULL_TREE)
11231 {
11232 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11233 && !specs->signed_p && !specs->unsigned_p
11234 && !specs->complex_p);
11235
11236 /* Set a dummy type. */
11237 if (TREE_CODE (specs->type) == ERROR_MARK)
11238 specs->type = integer_type_node;
11239 return specs;
11240 }
11241
11242 /* If none of "void", "_Bool", "char", "int", "float" or "double"
11243 has been specified, treat it as "int" unless "_Complex" is
11244 present and there are no other specifiers. If we just have
11245 "_Complex", it is equivalent to "_Complex double", but e.g.
11246 "_Complex short" is equivalent to "_Complex short int". */
11247 if (specs->typespec_word == cts_none)
11248 {
11249 if (specs->saturating_p)
11250 {
11251 error_at (specs->locations[cdw_saturating],
11252 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11253 if (!targetm.fixed_point_supported_p ())
11254 error_at (specs->locations[cdw_saturating],
11255 "fixed-point types not supported for this target");
11256 specs->typespec_word = cts_fract;
11257 }
11258 else if (specs->long_p || specs->short_p
11259 || specs->signed_p || specs->unsigned_p)
11260 {
11261 specs->typespec_word = cts_int;
11262 }
11263 else if (specs->complex_p)
11264 {
11265 specs->typespec_word = cts_double;
11266 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11267 "ISO C does not support plain %<complex%> meaning "
11268 "%<double complex%>");
11269 }
11270 else
11271 {
11272 specs->typespec_word = cts_int;
11273 specs->default_int_p = true;
11274 /* We don't diagnose this here because grokdeclarator will
11275 give more specific diagnostics according to whether it is
11276 a function definition. */
11277 }
11278 }
11279
11280 /* If "signed" was specified, record this to distinguish "int" and
11281 "signed int" in the case of a bit-field with
11282 -funsigned-bitfields. */
11283 specs->explicit_signed_p = specs->signed_p;
11284
11285 /* Now compute the actual type. */
11286 switch (specs->typespec_word)
11287 {
11288 case cts_auto_type:
11289 gcc_assert (!specs->long_p && !specs->short_p
11290 && !specs->signed_p && !specs->unsigned_p
11291 && !specs->complex_p);
11292 /* Type to be filled in later. */
11293 break;
11294 case cts_void:
11295 gcc_assert (!specs->long_p && !specs->short_p
11296 && !specs->signed_p && !specs->unsigned_p
11297 && !specs->complex_p);
11298 specs->type = void_type_node;
11299 break;
11300 case cts_bool:
11301 gcc_assert (!specs->long_p && !specs->short_p
11302 && !specs->signed_p && !specs->unsigned_p
11303 && !specs->complex_p);
11304 specs->type = boolean_type_node;
11305 break;
11306 case cts_char:
11307 gcc_assert (!specs->long_p && !specs->short_p);
11308 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11309 if (specs->signed_p)
11310 specs->type = signed_char_type_node;
11311 else if (specs->unsigned_p)
11312 specs->type = unsigned_char_type_node;
11313 else
11314 specs->type = char_type_node;
11315 if (specs->complex_p)
11316 {
11317 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11318 "ISO C does not support complex integer types");
11319 specs->type = build_complex_type (specs->type);
11320 }
11321 break;
11322 case cts_int_n:
11323 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
11324 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11325 if (! int_n_enabled_p[specs->int_n_idx])
11326 specs->type = integer_type_node;
11327 else
11328 specs->type = (specs->unsigned_p
11329 ? int_n_trees[specs->int_n_idx].unsigned_type
11330 : int_n_trees[specs->int_n_idx].signed_type);
11331 if (specs->complex_p)
11332 {
11333 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11334 "ISO C does not support complex integer types");
11335 specs->type = build_complex_type (specs->type);
11336 }
11337 break;
11338 case cts_int:
11339 gcc_assert (!(specs->long_p && specs->short_p));
11340 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11341 if (specs->long_long_p)
11342 specs->type = (specs->unsigned_p
11343 ? long_long_unsigned_type_node
11344 : long_long_integer_type_node);
11345 else if (specs->long_p)
11346 specs->type = (specs->unsigned_p
11347 ? long_unsigned_type_node
11348 : long_integer_type_node);
11349 else if (specs->short_p)
11350 specs->type = (specs->unsigned_p
11351 ? short_unsigned_type_node
11352 : short_integer_type_node);
11353 else
11354 specs->type = (specs->unsigned_p
11355 ? unsigned_type_node
11356 : integer_type_node);
11357 if (specs->complex_p)
11358 {
11359 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11360 "ISO C does not support complex integer types");
11361 specs->type = build_complex_type (specs->type);
11362 }
11363 break;
11364 case cts_float:
11365 gcc_assert (!specs->long_p && !specs->short_p
11366 && !specs->signed_p && !specs->unsigned_p);
11367 specs->type = (specs->complex_p
11368 ? complex_float_type_node
11369 : float_type_node);
11370 break;
11371 case cts_double:
11372 gcc_assert (!specs->long_long_p && !specs->short_p
11373 && !specs->signed_p && !specs->unsigned_p);
11374 if (specs->long_p)
11375 {
11376 specs->type = (specs->complex_p
11377 ? complex_long_double_type_node
11378 : long_double_type_node);
11379 }
11380 else
11381 {
11382 specs->type = (specs->complex_p
11383 ? complex_double_type_node
11384 : double_type_node);
11385 }
11386 break;
11387 case cts_floatn_nx:
11388 gcc_assert (!specs->long_p && !specs->short_p
11389 && !specs->signed_p && !specs->unsigned_p);
11390 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11391 specs->type = integer_type_node;
11392 else if (specs->complex_p)
11393 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11394 else
11395 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11396 break;
11397 case cts_dfloat32:
11398 case cts_dfloat64:
11399 case cts_dfloat128:
11400 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11401 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11402 if (specs->typespec_word == cts_dfloat32)
11403 specs->type = dfloat32_type_node;
11404 else if (specs->typespec_word == cts_dfloat64)
11405 specs->type = dfloat64_type_node;
11406 else
11407 specs->type = dfloat128_type_node;
11408 break;
11409 case cts_fract:
11410 gcc_assert (!specs->complex_p);
11411 if (!targetm.fixed_point_supported_p ())
11412 specs->type = integer_type_node;
11413 else if (specs->saturating_p)
11414 {
11415 if (specs->long_long_p)
11416 specs->type = specs->unsigned_p
11417 ? sat_unsigned_long_long_fract_type_node
11418 : sat_long_long_fract_type_node;
11419 else if (specs->long_p)
11420 specs->type = specs->unsigned_p
11421 ? sat_unsigned_long_fract_type_node
11422 : sat_long_fract_type_node;
11423 else if (specs->short_p)
11424 specs->type = specs->unsigned_p
11425 ? sat_unsigned_short_fract_type_node
11426 : sat_short_fract_type_node;
11427 else
11428 specs->type = specs->unsigned_p
11429 ? sat_unsigned_fract_type_node
11430 : sat_fract_type_node;
11431 }
11432 else
11433 {
11434 if (specs->long_long_p)
11435 specs->type = specs->unsigned_p
11436 ? unsigned_long_long_fract_type_node
11437 : long_long_fract_type_node;
11438 else if (specs->long_p)
11439 specs->type = specs->unsigned_p
11440 ? unsigned_long_fract_type_node
11441 : long_fract_type_node;
11442 else if (specs->short_p)
11443 specs->type = specs->unsigned_p
11444 ? unsigned_short_fract_type_node
11445 : short_fract_type_node;
11446 else
11447 specs->type = specs->unsigned_p
11448 ? unsigned_fract_type_node
11449 : fract_type_node;
11450 }
11451 break;
11452 case cts_accum:
11453 gcc_assert (!specs->complex_p);
11454 if (!targetm.fixed_point_supported_p ())
11455 specs->type = integer_type_node;
11456 else if (specs->saturating_p)
11457 {
11458 if (specs->long_long_p)
11459 specs->type = specs->unsigned_p
11460 ? sat_unsigned_long_long_accum_type_node
11461 : sat_long_long_accum_type_node;
11462 else if (specs->long_p)
11463 specs->type = specs->unsigned_p
11464 ? sat_unsigned_long_accum_type_node
11465 : sat_long_accum_type_node;
11466 else if (specs->short_p)
11467 specs->type = specs->unsigned_p
11468 ? sat_unsigned_short_accum_type_node
11469 : sat_short_accum_type_node;
11470 else
11471 specs->type = specs->unsigned_p
11472 ? sat_unsigned_accum_type_node
11473 : sat_accum_type_node;
11474 }
11475 else
11476 {
11477 if (specs->long_long_p)
11478 specs->type = specs->unsigned_p
11479 ? unsigned_long_long_accum_type_node
11480 : long_long_accum_type_node;
11481 else if (specs->long_p)
11482 specs->type = specs->unsigned_p
11483 ? unsigned_long_accum_type_node
11484 : long_accum_type_node;
11485 else if (specs->short_p)
11486 specs->type = specs->unsigned_p
11487 ? unsigned_short_accum_type_node
11488 : short_accum_type_node;
11489 else
11490 specs->type = specs->unsigned_p
11491 ? unsigned_accum_type_node
11492 : accum_type_node;
11493 }
11494 break;
11495 default:
11496 gcc_unreachable ();
11497 }
11498
11499 return specs;
11500 }
11501
11502 /* Perform final processing on one file scope's declarations (or the
11503 external scope's declarations), GLOBALS. */
11504
11505 static void
11506 c_write_global_declarations_1 (tree globals)
11507 {
11508 tree decl;
11509 bool reconsider;
11510
11511 /* Process the decls in the order they were written. */
11512 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11513 {
11514 /* Check for used but undefined static functions using the C
11515 standard's definition of "used", and set TREE_NO_WARNING so
11516 that check_global_declaration doesn't repeat the check. */
11517 if (TREE_CODE (decl) == FUNCTION_DECL
11518 && DECL_INITIAL (decl) == NULL_TREE
11519 && DECL_EXTERNAL (decl)
11520 && !TREE_PUBLIC (decl))
11521 {
11522 if (C_DECL_USED (decl))
11523 {
11524 if (pedwarn (input_location, 0, "%q+F used but never defined",
11525 decl))
11526 TREE_NO_WARNING (decl) = 1;
11527 }
11528 /* For -Wunused-function warn about unused static prototypes. */
11529 else if (warn_unused_function
11530 && ! DECL_ARTIFICIAL (decl)
11531 && ! TREE_NO_WARNING (decl))
11532 {
11533 if (warning (OPT_Wunused_function,
11534 "%q+F declared %<static%> but never defined",
11535 decl))
11536 TREE_NO_WARNING (decl) = 1;
11537 }
11538 }
11539
11540 wrapup_global_declaration_1 (decl);
11541 }
11542
11543 do
11544 {
11545 reconsider = false;
11546 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11547 reconsider |= wrapup_global_declaration_2 (decl);
11548 }
11549 while (reconsider);
11550 }
11551
11552 /* Callback to collect a source_ref from a DECL. */
11553
11554 static void
11555 collect_source_ref_cb (tree decl)
11556 {
11557 if (!DECL_IS_BUILTIN (decl))
11558 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
11559 }
11560
11561 /* Preserve the external declarations scope across a garbage collect. */
11562 static GTY(()) tree ext_block;
11563
11564 /* Collect all references relevant to SOURCE_FILE. */
11565
11566 static void
11567 collect_all_refs (const char *source_file)
11568 {
11569 tree t;
11570 unsigned i;
11571
11572 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11573 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11574
11575 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11576 }
11577
11578 /* Iterate over all global declarations and call CALLBACK. */
11579
11580 static void
11581 for_each_global_decl (void (*callback) (tree decl))
11582 {
11583 tree t;
11584 tree decls;
11585 tree decl;
11586 unsigned i;
11587
11588 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11589 {
11590 decls = DECL_INITIAL (t);
11591 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11592 callback (decl);
11593 }
11594
11595 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11596 callback (decl);
11597 }
11598
11599 /* Perform any final parser cleanups and generate initial debugging
11600 information. */
11601
11602 void
11603 c_parse_final_cleanups (void)
11604 {
11605 tree t;
11606 unsigned i;
11607
11608 /* We don't want to do this if generating a PCH. */
11609 if (pch_file)
11610 return;
11611
11612 timevar_stop (TV_PHASE_PARSING);
11613 timevar_start (TV_PHASE_DEFERRED);
11614
11615 /* Do the Objective-C stuff. This is where all the Objective-C
11616 module stuff gets generated (symtab, class/protocol/selector
11617 lists etc). */
11618 if (c_dialect_objc ())
11619 objc_write_global_declarations ();
11620
11621 /* Close the external scope. */
11622 ext_block = pop_scope ();
11623 external_scope = 0;
11624 gcc_assert (!current_scope);
11625
11626 /* Handle -fdump-ada-spec[-slim]. */
11627 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11628 {
11629 /* Build a table of files to generate specs for */
11630 if (flag_dump_ada_spec_slim)
11631 collect_source_ref (main_input_filename);
11632 else
11633 for_each_global_decl (collect_source_ref_cb);
11634
11635 dump_ada_specs (collect_all_refs, NULL);
11636 }
11637
11638 /* Process all file scopes in this compilation, and the external_scope,
11639 through wrapup_global_declarations. */
11640 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11641 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11642 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11643
11644 timevar_stop (TV_PHASE_DEFERRED);
11645 timevar_start (TV_PHASE_PARSING);
11646
11647 ext_block = NULL;
11648 }
11649
11650 /* Register reserved keyword WORD as qualifier for address space AS. */
11651
11652 void
11653 c_register_addr_space (const char *word, addr_space_t as)
11654 {
11655 int rid = RID_FIRST_ADDR_SPACE + as;
11656 tree id;
11657
11658 /* Address space qualifiers are only supported
11659 in C with GNU extensions enabled. */
11660 if (c_dialect_objc () || flag_no_asm)
11661 return;
11662
11663 id = get_identifier (word);
11664 C_SET_RID_CODE (id, rid);
11665 C_IS_RESERVED_WORD (id) = 1;
11666 ridpointers [rid] = id;
11667 }
11668
11669 /* Return identifier to look up for omp declare reduction. */
11670
11671 tree
11672 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11673 {
11674 const char *p = NULL;
11675 switch (reduction_code)
11676 {
11677 case PLUS_EXPR: p = "+"; break;
11678 case MULT_EXPR: p = "*"; break;
11679 case MINUS_EXPR: p = "-"; break;
11680 case BIT_AND_EXPR: p = "&"; break;
11681 case BIT_XOR_EXPR: p = "^"; break;
11682 case BIT_IOR_EXPR: p = "|"; break;
11683 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11684 case TRUTH_ORIF_EXPR: p = "||"; break;
11685 case MIN_EXPR: p = "min"; break;
11686 case MAX_EXPR: p = "max"; break;
11687 default:
11688 break;
11689 }
11690
11691 if (p == NULL)
11692 {
11693 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11694 return error_mark_node;
11695 p = IDENTIFIER_POINTER (reduction_id);
11696 }
11697
11698 const char prefix[] = "omp declare reduction ";
11699 size_t lenp = sizeof (prefix);
11700 size_t len = strlen (p);
11701 char *name = XALLOCAVEC (char, lenp + len);
11702 memcpy (name, prefix, lenp - 1);
11703 memcpy (name + lenp - 1, p, len + 1);
11704 return get_identifier (name);
11705 }
11706
11707 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11708 VAR_DECL, bind it into the current scope and return it. */
11709
11710 tree
11711 c_omp_reduction_decl (tree reduction_id)
11712 {
11713 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11714 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11715 return b->decl;
11716
11717 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11718 reduction_id, integer_type_node);
11719 DECL_ARTIFICIAL (decl) = 1;
11720 DECL_EXTERNAL (decl) = 1;
11721 TREE_STATIC (decl) = 1;
11722 TREE_PUBLIC (decl) = 0;
11723 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11724 return decl;
11725 }
11726
11727 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11728
11729 tree
11730 c_omp_reduction_lookup (tree reduction_id, tree type)
11731 {
11732 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11733 while (b)
11734 {
11735 tree t;
11736 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11737 if (comptypes (TREE_PURPOSE (t), type))
11738 return TREE_VALUE (t);
11739 b = b->shadowed;
11740 }
11741 return error_mark_node;
11742 }
11743
11744 /* Helper function called via walk_tree, to diagnose invalid
11745 #pragma omp declare reduction combiners or initializers. */
11746
11747 tree
11748 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11749 {
11750 tree *vars = (tree *) data;
11751 if (SSA_VAR_P (*tp)
11752 && !DECL_ARTIFICIAL (*tp)
11753 && *tp != vars[0]
11754 && *tp != vars[1])
11755 {
11756 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11757 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11758 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11759 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11760 *tp);
11761 else
11762 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11763 "to variable %qD which is not %<omp_priv%> nor "
11764 "%<omp_orig%>",
11765 *tp);
11766 return *tp;
11767 }
11768 return NULL_TREE;
11769 }
11770
11771 #include "gt-c-c-decl.h"