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