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