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