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