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