]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c/c-decl.c
C FE: suggest corrections for misspelled identifiers and type names
[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 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 vec<c_binding_ptr> fields;
583 /* If warn_cxx_compat, a list of typedef names used when defining
584 fields in this struct. */
585 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 tree hint = NULL_TREE;
3092 if (!olddecl)
3093 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_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 %qE?",
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 %qE?"),
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 tree 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 %qE?",
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 tree 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 %qE?",
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 /* Specialization of edit_distance_traits for preprocessor macros. */
3959
3960 template <>
3961 struct edit_distance_traits<cpp_hashnode *>
3962 {
3963 static size_t get_length (cpp_hashnode *hashnode)
3964 {
3965 return hashnode->ident.len;
3966 }
3967
3968 static const char *get_string (cpp_hashnode *hashnode)
3969 {
3970 return (const char *)hashnode->ident.str;
3971 }
3972 };
3973
3974 /* Specialization of best_match<> for finding the closest preprocessor
3975 macro to a given identifier. */
3976
3977 typedef best_match<tree, cpp_hashnode *> best_macro_match;
3978
3979 /* A callback for cpp_forall_identifiers, for use by lookup_name_fuzzy.
3980 Process HASHNODE and update the best_macro_match instance pointed to be
3981 USER_DATA. */
3982
3983 static int
3984 find_closest_macro_cpp_cb (cpp_reader *, cpp_hashnode *hashnode,
3985 void *user_data)
3986 {
3987 if (hashnode->type != NT_MACRO)
3988 return 1;
3989
3990 best_macro_match *bmm = (best_macro_match *)user_data;
3991 bmm->consider (hashnode);
3992
3993 /* Keep iterating. */
3994 return 1;
3995 }
3996
3997 /* Look for the closest match for NAME within the currently valid
3998 scopes.
3999
4000 This finds the identifier with the lowest Levenshtein distance to
4001 NAME. If there are multiple candidates with equal minimal distance,
4002 the first one found is returned. Scopes are searched from innermost
4003 outwards, and within a scope in reverse order of declaration, thus
4004 benefiting candidates "near" to the current scope.
4005
4006 The function also looks for similar macro names to NAME, since a
4007 misspelled macro name will not be expanded, and hence looks like an
4008 identifier to the C frontend.
4009
4010 It also looks for start_typename keywords, to detect "singed" vs "signed"
4011 typos. */
4012
4013 tree
4014 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
4015 {
4016 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4017
4018 best_match<tree, tree> bm (name);
4019
4020 /* Look within currently valid scopes. */
4021 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4022 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4023 {
4024 if (!binding->id)
4025 continue;
4026 /* Don't use bindings from implicitly declared functions,
4027 as they were likely misspellings themselves. */
4028 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4029 if (C_DECL_IMPLICIT (binding->decl))
4030 continue;
4031 if (kind == FUZZY_LOOKUP_TYPENAME)
4032 if (TREE_CODE (binding->decl) != TYPE_DECL)
4033 continue;
4034 bm.consider (binding->id);
4035 }
4036
4037 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4038 as:
4039 x = SOME_OTHER_MACRO (y);
4040 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4041 as a misspelled identifier.
4042
4043 Use the best distance so far so that a candidate is only set if
4044 a macro is better than anything so far. This allows early rejection
4045 (without calculating the edit distance) of macro names that must have
4046 distance >= bm.get_best_distance (), and means that we only get a
4047 non-NULL result for best_macro_match if it's better than any of
4048 the identifiers already checked, which avoids needless creation
4049 of identifiers for macro hashnodes. */
4050 best_macro_match bmm (name, bm.get_best_distance ());
4051 cpp_forall_identifiers (parse_in, find_closest_macro_cpp_cb, &bmm);
4052 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4053 /* If a macro is the closest so far to NAME, use it, creating an
4054 identifier tree node for it. */
4055 if (best_macro)
4056 {
4057 const char *id = (const char *)best_macro->ident.str;
4058 tree macro_as_identifier
4059 = get_identifier_with_length (id, best_macro->ident.len);
4060 bm.set_best_so_far (macro_as_identifier,
4061 bmm.get_best_distance (),
4062 bmm.get_best_candidate_length ());
4063 }
4064
4065 /* Try the "start_typename" keywords to detect
4066 "singed" vs "signed" typos. */
4067 if (kind == FUZZY_LOOKUP_TYPENAME)
4068 {
4069 for (unsigned i = 0; i < num_c_common_reswords; i++)
4070 {
4071 const c_common_resword *resword = &c_common_reswords[i];
4072 if (!c_keyword_starts_typename (resword->rid))
4073 continue;
4074 tree resword_identifier = ridpointers [resword->rid];
4075 if (!resword_identifier)
4076 continue;
4077 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4078 bm.consider (resword_identifier);
4079 }
4080 }
4081
4082 return bm.get_best_meaningful_candidate ();
4083 }
4084
4085 \f
4086 /* Create the predefined scalar types of C,
4087 and some nodes representing standard constants (0, 1, (void *) 0).
4088 Initialize the global scope.
4089 Make definitions for built-in primitive functions. */
4090
4091 void
4092 c_init_decl_processing (void)
4093 {
4094 location_t save_loc = input_location;
4095
4096 /* Initialize reserved words for parser. */
4097 c_parse_init ();
4098
4099 current_function_decl = 0;
4100
4101 gcc_obstack_init (&parser_obstack);
4102
4103 /* Make the externals scope. */
4104 push_scope ();
4105 external_scope = current_scope;
4106
4107 /* Declarations from c_common_nodes_and_builtins must not be associated
4108 with this input file, lest we get differences between using and not
4109 using preprocessed headers. */
4110 input_location = BUILTINS_LOCATION;
4111
4112 c_common_nodes_and_builtins ();
4113
4114 /* In C, comparisons and TRUTH_* expressions have type int. */
4115 truthvalue_type_node = integer_type_node;
4116 truthvalue_true_node = integer_one_node;
4117 truthvalue_false_node = integer_zero_node;
4118
4119 /* Even in C99, which has a real boolean type. */
4120 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4121 boolean_type_node));
4122
4123 input_location = save_loc;
4124
4125 make_fname_decl = c_make_fname_decl;
4126 start_fname_decls ();
4127 }
4128
4129 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4130 give the decl, NAME is the initialization string and TYPE_DEP
4131 indicates whether NAME depended on the type of the function. As we
4132 don't yet implement delayed emission of static data, we mark the
4133 decl as emitted so it is not placed in the output. Anything using
4134 it must therefore pull out the STRING_CST initializer directly.
4135 FIXME. */
4136
4137 static tree
4138 c_make_fname_decl (location_t loc, tree id, int type_dep)
4139 {
4140 const char *name = fname_as_string (type_dep);
4141 tree decl, type, init;
4142 size_t length = strlen (name);
4143
4144 type = build_array_type (char_type_node,
4145 build_index_type (size_int (length)));
4146 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4147
4148 decl = build_decl (loc, VAR_DECL, id, type);
4149
4150 TREE_STATIC (decl) = 1;
4151 TREE_READONLY (decl) = 1;
4152 DECL_ARTIFICIAL (decl) = 1;
4153
4154 init = build_string (length + 1, name);
4155 free (CONST_CAST (char *, name));
4156 TREE_TYPE (init) = type;
4157 DECL_INITIAL (decl) = init;
4158
4159 TREE_USED (decl) = 1;
4160
4161 if (current_function_decl
4162 /* For invalid programs like this:
4163
4164 void foo()
4165 const char* p = __FUNCTION__;
4166
4167 the __FUNCTION__ is believed to appear in K&R style function
4168 parameter declarator. In that case we still don't have
4169 function_scope. */
4170 && current_function_scope)
4171 {
4172 DECL_CONTEXT (decl) = current_function_decl;
4173 bind (id, decl, current_function_scope,
4174 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4175 }
4176
4177 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4178
4179 return decl;
4180 }
4181
4182 tree
4183 c_builtin_function (tree decl)
4184 {
4185 tree type = TREE_TYPE (decl);
4186 tree id = DECL_NAME (decl);
4187
4188 const char *name = IDENTIFIER_POINTER (id);
4189 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4190
4191 /* Should never be called on a symbol with a preexisting meaning. */
4192 gcc_assert (!I_SYMBOL_BINDING (id));
4193
4194 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4195 UNKNOWN_LOCATION);
4196
4197 /* Builtins in the implementation namespace are made visible without
4198 needing to be explicitly declared. See push_file_scope. */
4199 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4200 {
4201 DECL_CHAIN (decl) = visible_builtins;
4202 visible_builtins = decl;
4203 }
4204
4205 return decl;
4206 }
4207
4208 tree
4209 c_builtin_function_ext_scope (tree decl)
4210 {
4211 tree type = TREE_TYPE (decl);
4212 tree id = DECL_NAME (decl);
4213
4214 const char *name = IDENTIFIER_POINTER (id);
4215 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4216
4217 if (external_scope)
4218 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4219 UNKNOWN_LOCATION);
4220
4221 /* Builtins in the implementation namespace are made visible without
4222 needing to be explicitly declared. See push_file_scope. */
4223 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4224 {
4225 DECL_CHAIN (decl) = visible_builtins;
4226 visible_builtins = decl;
4227 }
4228
4229 return decl;
4230 }
4231 \f
4232 /* Called when a declaration is seen that contains no names to declare.
4233 If its type is a reference to a structure, union or enum inherited
4234 from a containing scope, shadow that tag name for the current scope
4235 with a forward reference.
4236 If its type defines a new named structure or union
4237 or defines an enum, it is valid but we need not do anything here.
4238 Otherwise, it is an error. */
4239
4240 void
4241 shadow_tag (const struct c_declspecs *declspecs)
4242 {
4243 shadow_tag_warned (declspecs, 0);
4244 }
4245
4246 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4247 but no pedwarn. */
4248 void
4249 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4250 {
4251 bool found_tag = false;
4252
4253 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4254 {
4255 tree value = declspecs->type;
4256 enum tree_code code = TREE_CODE (value);
4257
4258 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4259 /* Used to test also that TYPE_SIZE (value) != 0.
4260 That caused warning for `struct foo;' at top level in the file. */
4261 {
4262 tree name = TYPE_NAME (value);
4263 tree t;
4264
4265 found_tag = true;
4266
4267 if (declspecs->restrict_p)
4268 {
4269 error ("invalid use of %<restrict%>");
4270 warned = 1;
4271 }
4272
4273 if (name == 0)
4274 {
4275 if (warned != 1 && code != ENUMERAL_TYPE)
4276 /* Empty unnamed enum OK */
4277 {
4278 pedwarn (input_location, 0,
4279 "unnamed struct/union that defines no instances");
4280 warned = 1;
4281 }
4282 }
4283 else if (declspecs->typespec_kind != ctsk_tagdef
4284 && declspecs->typespec_kind != ctsk_tagfirstref
4285 && declspecs->storage_class != csc_none)
4286 {
4287 if (warned != 1)
4288 pedwarn (input_location, 0,
4289 "empty declaration with storage class specifier "
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->const_p
4297 || declspecs->volatile_p
4298 || declspecs->atomic_p
4299 || declspecs->restrict_p
4300 || declspecs->address_space))
4301 {
4302 if (warned != 1)
4303 pedwarn (input_location, 0,
4304 "empty declaration with type qualifier "
4305 "does not redeclare tag");
4306 warned = 1;
4307 pending_xref_error ();
4308 }
4309 else if (declspecs->typespec_kind != ctsk_tagdef
4310 && declspecs->typespec_kind != ctsk_tagfirstref
4311 && declspecs->alignas_p)
4312 {
4313 if (warned != 1)
4314 pedwarn (input_location, 0,
4315 "empty declaration with %<_Alignas%> "
4316 "does not redeclare tag");
4317 warned = 1;
4318 pending_xref_error ();
4319 }
4320 else
4321 {
4322 pending_invalid_xref = 0;
4323 t = lookup_tag (code, name, true, NULL);
4324
4325 if (t == NULL_TREE)
4326 {
4327 t = make_node (code);
4328 pushtag (input_location, name, t);
4329 }
4330 }
4331 }
4332 else
4333 {
4334 if (warned != 1 && !in_system_header_at (input_location))
4335 {
4336 pedwarn (input_location, 0,
4337 "useless type name in empty declaration");
4338 warned = 1;
4339 }
4340 }
4341 }
4342 else if (warned != 1 && !in_system_header_at (input_location)
4343 && declspecs->typedef_p)
4344 {
4345 pedwarn (input_location, 0, "useless type name in empty declaration");
4346 warned = 1;
4347 }
4348
4349 pending_invalid_xref = 0;
4350
4351 if (declspecs->inline_p)
4352 {
4353 error ("%<inline%> in empty declaration");
4354 warned = 1;
4355 }
4356
4357 if (declspecs->noreturn_p)
4358 {
4359 error ("%<_Noreturn%> in empty declaration");
4360 warned = 1;
4361 }
4362
4363 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4364 {
4365 error ("%<auto%> in file-scope empty declaration");
4366 warned = 1;
4367 }
4368
4369 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4370 {
4371 error ("%<register%> in file-scope empty declaration");
4372 warned = 1;
4373 }
4374
4375 if (!warned && !in_system_header_at (input_location)
4376 && declspecs->storage_class != csc_none)
4377 {
4378 warning (0, "useless storage class specifier in empty declaration");
4379 warned = 2;
4380 }
4381
4382 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4383 {
4384 warning (0, "useless %qs in empty declaration",
4385 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4386 warned = 2;
4387 }
4388
4389 if (!warned
4390 && !in_system_header_at (input_location)
4391 && (declspecs->const_p
4392 || declspecs->volatile_p
4393 || declspecs->atomic_p
4394 || declspecs->restrict_p
4395 || declspecs->address_space))
4396 {
4397 warning (0, "useless type qualifier in empty declaration");
4398 warned = 2;
4399 }
4400
4401 if (!warned && !in_system_header_at (input_location)
4402 && declspecs->alignas_p)
4403 {
4404 warning (0, "useless %<_Alignas%> in empty declaration");
4405 warned = 2;
4406 }
4407
4408 if (warned != 1)
4409 {
4410 if (!found_tag)
4411 pedwarn (input_location, 0, "empty declaration");
4412 }
4413 }
4414 \f
4415
4416 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4417 bits. SPECS represents declaration specifiers that the grammar
4418 only permits to contain type qualifiers and attributes. */
4419
4420 int
4421 quals_from_declspecs (const struct c_declspecs *specs)
4422 {
4423 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4424 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4425 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4426 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4427 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4428 gcc_assert (!specs->type
4429 && !specs->decl_attr
4430 && specs->typespec_word == cts_none
4431 && specs->storage_class == csc_none
4432 && !specs->typedef_p
4433 && !specs->explicit_signed_p
4434 && !specs->deprecated_p
4435 && !specs->long_p
4436 && !specs->long_long_p
4437 && !specs->short_p
4438 && !specs->signed_p
4439 && !specs->unsigned_p
4440 && !specs->complex_p
4441 && !specs->inline_p
4442 && !specs->noreturn_p
4443 && !specs->thread_p);
4444 return quals;
4445 }
4446
4447 /* Construct an array declarator. LOC is the location of the
4448 beginning of the array (usually the opening brace). EXPR is the
4449 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4450 inside the [] (to be applied to the pointer to which a parameter
4451 array is converted). STATIC_P is true if "static" is inside the
4452 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4453 VLA of unspecified length which is nevertheless a complete type,
4454 false otherwise. The field for the contained declarator is left to
4455 be filled in by set_array_declarator_inner. */
4456
4457 struct c_declarator *
4458 build_array_declarator (location_t loc,
4459 tree expr, struct c_declspecs *quals, bool static_p,
4460 bool vla_unspec_p)
4461 {
4462 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4463 struct c_declarator);
4464 declarator->id_loc = loc;
4465 declarator->kind = cdk_array;
4466 declarator->declarator = 0;
4467 declarator->u.array.dimen = expr;
4468 if (quals)
4469 {
4470 declarator->u.array.attrs = quals->attrs;
4471 declarator->u.array.quals = quals_from_declspecs (quals);
4472 }
4473 else
4474 {
4475 declarator->u.array.attrs = NULL_TREE;
4476 declarator->u.array.quals = 0;
4477 }
4478 declarator->u.array.static_p = static_p;
4479 declarator->u.array.vla_unspec_p = vla_unspec_p;
4480 if (static_p || quals != NULL)
4481 pedwarn_c90 (loc, OPT_Wpedantic,
4482 "ISO C90 does not support %<static%> or type "
4483 "qualifiers in parameter array declarators");
4484 if (vla_unspec_p)
4485 pedwarn_c90 (loc, OPT_Wpedantic,
4486 "ISO C90 does not support %<[*]%> array declarators");
4487 if (vla_unspec_p)
4488 {
4489 if (!current_scope->parm_flag)
4490 {
4491 /* C99 6.7.5.2p4 */
4492 error_at (loc, "%<[*]%> not allowed in other than "
4493 "function prototype scope");
4494 declarator->u.array.vla_unspec_p = false;
4495 return NULL;
4496 }
4497 current_scope->had_vla_unspec = true;
4498 }
4499 return declarator;
4500 }
4501
4502 /* Set the contained declarator of an array declarator. DECL is the
4503 declarator, as constructed by build_array_declarator; INNER is what
4504 appears on the left of the []. */
4505
4506 struct c_declarator *
4507 set_array_declarator_inner (struct c_declarator *decl,
4508 struct c_declarator *inner)
4509 {
4510 decl->declarator = inner;
4511 return decl;
4512 }
4513
4514 /* INIT is a constructor that forms DECL's initializer. If the final
4515 element initializes a flexible array field, add the size of that
4516 initializer to DECL's size. */
4517
4518 static void
4519 add_flexible_array_elts_to_size (tree decl, tree init)
4520 {
4521 tree elt, type;
4522
4523 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4524 return;
4525
4526 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4527 type = TREE_TYPE (elt);
4528 if (TREE_CODE (type) == ARRAY_TYPE
4529 && TYPE_SIZE (type) == NULL_TREE
4530 && TYPE_DOMAIN (type) != NULL_TREE
4531 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4532 {
4533 complete_array_type (&type, elt, false);
4534 DECL_SIZE (decl)
4535 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4536 DECL_SIZE_UNIT (decl)
4537 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4538 }
4539 }
4540 \f
4541 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4542 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4543 before the type name, and set *EXPR_CONST_OPERANDS, if
4544 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4545 appear in a constant expression. */
4546
4547 tree
4548 groktypename (struct c_type_name *type_name, tree *expr,
4549 bool *expr_const_operands)
4550 {
4551 tree type;
4552 tree attrs = type_name->specs->attrs;
4553
4554 type_name->specs->attrs = NULL_TREE;
4555
4556 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4557 false, NULL, &attrs, expr, expr_const_operands,
4558 DEPRECATED_NORMAL);
4559
4560 /* Apply attributes. */
4561 decl_attributes (&type, attrs, 0);
4562
4563 return type;
4564 }
4565
4566 /* Wrapper for decl_attributes that adds some implicit attributes
4567 to VAR_DECLs or FUNCTION_DECLs. */
4568
4569 static tree
4570 c_decl_attributes (tree *node, tree attributes, int flags)
4571 {
4572 /* Add implicit "omp declare target" attribute if requested. */
4573 if (current_omp_declare_target_attribute
4574 && ((VAR_P (*node) && is_global_var (*node))
4575 || TREE_CODE (*node) == FUNCTION_DECL))
4576 {
4577 if (VAR_P (*node)
4578 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4579 error ("%q+D in declare target directive does not have mappable type",
4580 *node);
4581 else
4582 attributes = tree_cons (get_identifier ("omp declare target"),
4583 NULL_TREE, attributes);
4584 }
4585 return decl_attributes (node, attributes, flags);
4586 }
4587
4588
4589 /* Decode a declarator in an ordinary declaration or data definition.
4590 This is called as soon as the type information and variable name
4591 have been parsed, before parsing the initializer if any.
4592 Here we create the ..._DECL node, fill in its type,
4593 and put it on the list of decls for the current context.
4594 The ..._DECL node is returned as the value.
4595
4596 Exception: for arrays where the length is not specified,
4597 the type is left null, to be filled in by `finish_decl'.
4598
4599 Function definitions do not come here; they go to start_function
4600 instead. However, external and forward declarations of functions
4601 do go through here. Structure field declarations are done by
4602 grokfield and not through here. */
4603
4604 tree
4605 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4606 bool initialized, tree attributes)
4607 {
4608 tree decl;
4609 tree tem;
4610 tree expr = NULL_TREE;
4611 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4612
4613 /* An object declared as __attribute__((deprecated)) suppresses
4614 warnings of uses of other deprecated items. */
4615 if (lookup_attribute ("deprecated", attributes))
4616 deprecated_state = DEPRECATED_SUPPRESS;
4617
4618 decl = grokdeclarator (declarator, declspecs,
4619 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4620 deprecated_state);
4621 if (!decl || decl == error_mark_node)
4622 return NULL_TREE;
4623
4624 if (expr)
4625 add_stmt (fold_convert (void_type_node, expr));
4626
4627 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4628 warning (OPT_Wmain, "%q+D is usually a function", decl);
4629
4630 if (initialized)
4631 /* Is it valid for this decl to have an initializer at all?
4632 If not, set INITIALIZED to zero, which will indirectly
4633 tell 'finish_decl' to ignore the initializer once it is parsed. */
4634 switch (TREE_CODE (decl))
4635 {
4636 case TYPE_DECL:
4637 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4638 initialized = 0;
4639 break;
4640
4641 case FUNCTION_DECL:
4642 error ("function %qD is initialized like a variable", decl);
4643 initialized = 0;
4644 break;
4645
4646 case PARM_DECL:
4647 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4648 error ("parameter %qD is initialized", decl);
4649 initialized = 0;
4650 break;
4651
4652 default:
4653 /* Don't allow initializations for incomplete types except for
4654 arrays which might be completed by the initialization. */
4655
4656 /* This can happen if the array size is an undefined macro.
4657 We already gave a warning, so we don't need another one. */
4658 if (TREE_TYPE (decl) == error_mark_node)
4659 initialized = 0;
4660 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4661 {
4662 /* A complete type is ok if size is fixed. */
4663
4664 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4665 || C_DECL_VARIABLE_SIZE (decl))
4666 {
4667 error ("variable-sized object may not be initialized");
4668 initialized = 0;
4669 }
4670 }
4671 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4672 {
4673 error ("variable %qD has initializer but incomplete type", decl);
4674 initialized = 0;
4675 }
4676 else if (C_DECL_VARIABLE_SIZE (decl))
4677 {
4678 /* Although C99 is unclear about whether incomplete arrays
4679 of VLAs themselves count as VLAs, it does not make
4680 sense to permit them to be initialized given that
4681 ordinary VLAs may not be initialized. */
4682 error ("variable-sized object may not be initialized");
4683 initialized = 0;
4684 }
4685 }
4686
4687 if (initialized)
4688 {
4689 if (current_scope == file_scope)
4690 TREE_STATIC (decl) = 1;
4691
4692 /* Tell 'pushdecl' this is an initialized decl
4693 even though we don't yet have the initializer expression.
4694 Also tell 'finish_decl' it may store the real initializer. */
4695 DECL_INITIAL (decl) = error_mark_node;
4696 }
4697
4698 /* If this is a function declaration, write a record describing it to the
4699 prototypes file (if requested). */
4700
4701 if (TREE_CODE (decl) == FUNCTION_DECL)
4702 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4703
4704 /* ANSI specifies that a tentative definition which is not merged with
4705 a non-tentative definition behaves exactly like a definition with an
4706 initializer equal to zero. (Section 3.7.2)
4707
4708 -fno-common gives strict ANSI behavior, though this tends to break
4709 a large body of code that grew up without this rule.
4710
4711 Thread-local variables are never common, since there's no entrenched
4712 body of code to break, and it allows more efficient variable references
4713 in the presence of dynamic linking. */
4714
4715 if (VAR_P (decl)
4716 && !initialized
4717 && TREE_PUBLIC (decl)
4718 && !DECL_THREAD_LOCAL_P (decl)
4719 && !flag_no_common)
4720 DECL_COMMON (decl) = 1;
4721
4722 /* Set attributes here so if duplicate decl, will have proper attributes. */
4723 c_decl_attributes (&decl, attributes, 0);
4724
4725 /* Handle gnu_inline attribute. */
4726 if (declspecs->inline_p
4727 && !flag_gnu89_inline
4728 && TREE_CODE (decl) == FUNCTION_DECL
4729 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4730 || current_function_decl))
4731 {
4732 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4733 ;
4734 else if (declspecs->storage_class != csc_static)
4735 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4736 }
4737
4738 if (TREE_CODE (decl) == FUNCTION_DECL
4739 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4740 {
4741 struct c_declarator *ce = declarator;
4742
4743 if (ce->kind == cdk_pointer)
4744 ce = declarator->declarator;
4745 if (ce->kind == cdk_function)
4746 {
4747 tree args = ce->u.arg_info->parms;
4748 for (; args; args = DECL_CHAIN (args))
4749 {
4750 tree type = TREE_TYPE (args);
4751 if (type && INTEGRAL_TYPE_P (type)
4752 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4753 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4754 }
4755 }
4756 }
4757
4758 if (TREE_CODE (decl) == FUNCTION_DECL
4759 && DECL_DECLARED_INLINE_P (decl)
4760 && DECL_UNINLINABLE (decl)
4761 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4762 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4763 decl);
4764
4765 /* C99 6.7.4p3: An inline definition of a function with external
4766 linkage shall not contain a definition of a modifiable object
4767 with static storage duration... */
4768 if (VAR_P (decl)
4769 && current_scope != file_scope
4770 && TREE_STATIC (decl)
4771 && !TREE_READONLY (decl)
4772 && DECL_DECLARED_INLINE_P (current_function_decl)
4773 && DECL_EXTERNAL (current_function_decl))
4774 record_inline_static (input_location, current_function_decl,
4775 decl, csi_modifiable);
4776
4777 if (c_dialect_objc ()
4778 && VAR_OR_FUNCTION_DECL_P (decl))
4779 objc_check_global_decl (decl);
4780
4781 /* Add this decl to the current scope.
4782 TEM may equal DECL or it may be a previous decl of the same name. */
4783 tem = pushdecl (decl);
4784
4785 if (initialized && DECL_EXTERNAL (tem))
4786 {
4787 DECL_EXTERNAL (tem) = 0;
4788 TREE_STATIC (tem) = 1;
4789 }
4790
4791 return tem;
4792 }
4793
4794 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4795 DECL or the non-array element type if DECL is an uninitialized array.
4796 If that type has a const member, diagnose this. */
4797
4798 static void
4799 diagnose_uninitialized_cst_member (tree decl, tree type)
4800 {
4801 tree field;
4802 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4803 {
4804 tree field_type;
4805 if (TREE_CODE (field) != FIELD_DECL)
4806 continue;
4807 field_type = strip_array_types (TREE_TYPE (field));
4808
4809 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4810 {
4811 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4812 "uninitialized const member in %qT is invalid in C++",
4813 strip_array_types (TREE_TYPE (decl)));
4814 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4815 }
4816
4817 if (RECORD_OR_UNION_TYPE_P (field_type))
4818 diagnose_uninitialized_cst_member (decl, field_type);
4819 }
4820 }
4821
4822 /* Finish processing of a declaration;
4823 install its initial value.
4824 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4825 If the length of an array type is not known before,
4826 it must be determined now, from the initial value, or it is an error.
4827
4828 INIT_LOC is the location of the initial value. */
4829
4830 void
4831 finish_decl (tree decl, location_t init_loc, tree init,
4832 tree origtype, tree asmspec_tree)
4833 {
4834 tree type;
4835 bool was_incomplete = (DECL_SIZE (decl) == 0);
4836 const char *asmspec = 0;
4837
4838 /* If a name was specified, get the string. */
4839 if (VAR_OR_FUNCTION_DECL_P (decl)
4840 && DECL_FILE_SCOPE_P (decl))
4841 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4842 if (asmspec_tree)
4843 asmspec = TREE_STRING_POINTER (asmspec_tree);
4844
4845 if (VAR_P (decl)
4846 && TREE_STATIC (decl)
4847 && global_bindings_p ())
4848 /* So decl is a global variable. Record the types it uses
4849 so that we can decide later to emit debug info for them. */
4850 record_types_used_by_current_var_decl (decl);
4851
4852 /* If `start_decl' didn't like having an initialization, ignore it now. */
4853 if (init != 0 && DECL_INITIAL (decl) == 0)
4854 init = 0;
4855
4856 /* Don't crash if parm is initialized. */
4857 if (TREE_CODE (decl) == PARM_DECL)
4858 init = 0;
4859
4860 if (init)
4861 store_init_value (init_loc, decl, init, origtype);
4862
4863 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4864 || TREE_CODE (decl) == FIELD_DECL))
4865 objc_check_decl (decl);
4866
4867 type = TREE_TYPE (decl);
4868
4869 /* Deduce size of array from initialization, if not already known. */
4870 if (TREE_CODE (type) == ARRAY_TYPE
4871 && TYPE_DOMAIN (type) == 0
4872 && TREE_CODE (decl) != TYPE_DECL)
4873 {
4874 bool do_default
4875 = (TREE_STATIC (decl)
4876 /* Even if pedantic, an external linkage array
4877 may have incomplete type at first. */
4878 ? pedantic && !TREE_PUBLIC (decl)
4879 : !DECL_EXTERNAL (decl));
4880 int failure
4881 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4882 do_default);
4883
4884 /* Get the completed type made by complete_array_type. */
4885 type = TREE_TYPE (decl);
4886
4887 switch (failure)
4888 {
4889 case 1:
4890 error ("initializer fails to determine size of %q+D", decl);
4891 break;
4892
4893 case 2:
4894 if (do_default)
4895 error ("array size missing in %q+D", decl);
4896 /* If a `static' var's size isn't known,
4897 make it extern as well as static, so it does not get
4898 allocated.
4899 If it is not `static', then do not mark extern;
4900 finish_incomplete_decl will give it a default size
4901 and it will get allocated. */
4902 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4903 DECL_EXTERNAL (decl) = 1;
4904 break;
4905
4906 case 3:
4907 error ("zero or negative size array %q+D", decl);
4908 break;
4909
4910 case 0:
4911 /* For global variables, update the copy of the type that
4912 exists in the binding. */
4913 if (TREE_PUBLIC (decl))
4914 {
4915 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4916 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4917 b_ext = b_ext->shadowed;
4918 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
4919 {
4920 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4921 b_ext->u.type = composite_type (b_ext->u.type, type);
4922 else
4923 b_ext->u.type = type;
4924 }
4925 }
4926 break;
4927
4928 default:
4929 gcc_unreachable ();
4930 }
4931
4932 if (DECL_INITIAL (decl))
4933 TREE_TYPE (DECL_INITIAL (decl)) = type;
4934
4935 relayout_decl (decl);
4936 }
4937
4938 if (VAR_P (decl))
4939 {
4940 if (init && TREE_CODE (init) == CONSTRUCTOR)
4941 add_flexible_array_elts_to_size (decl, init);
4942
4943 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4944 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4945 layout_decl (decl, 0);
4946
4947 if (DECL_SIZE (decl) == 0
4948 /* Don't give an error if we already gave one earlier. */
4949 && TREE_TYPE (decl) != error_mark_node
4950 && (TREE_STATIC (decl)
4951 /* A static variable with an incomplete type
4952 is an error if it is initialized.
4953 Also if it is not file scope.
4954 Otherwise, let it through, but if it is not `extern'
4955 then it may cause an error message later. */
4956 ? (DECL_INITIAL (decl) != 0
4957 || !DECL_FILE_SCOPE_P (decl))
4958 /* An automatic variable with an incomplete type
4959 is an error. */
4960 : !DECL_EXTERNAL (decl)))
4961 {
4962 error ("storage size of %q+D isn%'t known", decl);
4963 TREE_TYPE (decl) = error_mark_node;
4964 }
4965
4966 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
4967 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
4968 && DECL_SIZE (decl) == NULL_TREE
4969 && TREE_STATIC (decl))
4970 incomplete_record_decls.safe_push (decl);
4971
4972 if (is_global_var (decl) && DECL_SIZE (decl) != 0)
4973 {
4974 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4975 constant_expression_warning (DECL_SIZE (decl));
4976 else
4977 {
4978 error ("storage size of %q+D isn%'t constant", decl);
4979 TREE_TYPE (decl) = error_mark_node;
4980 }
4981 }
4982
4983 if (TREE_USED (type))
4984 {
4985 TREE_USED (decl) = 1;
4986 DECL_READ_P (decl) = 1;
4987 }
4988 }
4989
4990 /* If this is a function and an assembler name is specified, reset DECL_RTL
4991 so we can give it its new name. Also, update builtin_decl if it
4992 was a normal built-in. */
4993 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4994 {
4995 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4996 set_builtin_user_assembler_name (decl, asmspec);
4997 set_user_assembler_name (decl, asmspec);
4998 }
4999
5000 /* If #pragma weak was used, mark the decl weak now. */
5001 maybe_apply_pragma_weak (decl);
5002
5003 /* Output the assembler code and/or RTL code for variables and functions,
5004 unless the type is an undefined structure or union.
5005 If not, it will get done when the type is completed. */
5006
5007 if (VAR_OR_FUNCTION_DECL_P (decl))
5008 {
5009 /* Determine the ELF visibility. */
5010 if (TREE_PUBLIC (decl))
5011 c_determine_visibility (decl);
5012
5013 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5014 if (c_dialect_objc ())
5015 objc_check_decl (decl);
5016
5017 if (asmspec)
5018 {
5019 /* If this is not a static variable, issue a warning.
5020 It doesn't make any sense to give an ASMSPEC for an
5021 ordinary, non-register local variable. Historically,
5022 GCC has accepted -- but ignored -- the ASMSPEC in
5023 this case. */
5024 if (!DECL_FILE_SCOPE_P (decl)
5025 && VAR_P (decl)
5026 && !C_DECL_REGISTER (decl)
5027 && !TREE_STATIC (decl))
5028 warning (0, "ignoring asm-specifier for non-static local "
5029 "variable %q+D", decl);
5030 else
5031 set_user_assembler_name (decl, asmspec);
5032 }
5033
5034 if (DECL_FILE_SCOPE_P (decl))
5035 {
5036 if (DECL_INITIAL (decl) == NULL_TREE
5037 || DECL_INITIAL (decl) == error_mark_node)
5038 /* Don't output anything
5039 when a tentative file-scope definition is seen.
5040 But at end of compilation, do output code for them. */
5041 DECL_DEFER_OUTPUT (decl) = 1;
5042 if (asmspec && C_DECL_REGISTER (decl))
5043 DECL_HARD_REGISTER (decl) = 1;
5044 rest_of_decl_compilation (decl, true, 0);
5045 }
5046 else
5047 {
5048 /* In conjunction with an ASMSPEC, the `register'
5049 keyword indicates that we should place the variable
5050 in a particular register. */
5051 if (asmspec && C_DECL_REGISTER (decl))
5052 {
5053 DECL_HARD_REGISTER (decl) = 1;
5054 /* This cannot be done for a structure with volatile
5055 fields, on which DECL_REGISTER will have been
5056 reset. */
5057 if (!DECL_REGISTER (decl))
5058 error ("cannot put object with volatile field into register");
5059 }
5060
5061 if (TREE_CODE (decl) != FUNCTION_DECL)
5062 {
5063 /* If we're building a variable sized type, and we might be
5064 reachable other than via the top of the current binding
5065 level, then create a new BIND_EXPR so that we deallocate
5066 the object at the right time. */
5067 /* Note that DECL_SIZE can be null due to errors. */
5068 if (DECL_SIZE (decl)
5069 && !TREE_CONSTANT (DECL_SIZE (decl))
5070 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5071 {
5072 tree bind;
5073 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5074 TREE_SIDE_EFFECTS (bind) = 1;
5075 add_stmt (bind);
5076 BIND_EXPR_BODY (bind) = push_stmt_list ();
5077 }
5078 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5079 DECL_EXPR, decl));
5080 }
5081 }
5082
5083
5084 if (!DECL_FILE_SCOPE_P (decl))
5085 {
5086 /* Recompute the RTL of a local array now
5087 if it used to be an incomplete type. */
5088 if (was_incomplete && !is_global_var (decl))
5089 {
5090 /* If we used it already as memory, it must stay in memory. */
5091 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5092 /* If it's still incomplete now, no init will save it. */
5093 if (DECL_SIZE (decl) == 0)
5094 DECL_INITIAL (decl) = 0;
5095 }
5096 }
5097 }
5098
5099 if (TREE_CODE (decl) == TYPE_DECL)
5100 {
5101 if (!DECL_FILE_SCOPE_P (decl)
5102 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5103 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5104
5105 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5106 }
5107
5108 /* Install a cleanup (aka destructor) if one was given. */
5109 if (VAR_P (decl) && !TREE_STATIC (decl))
5110 {
5111 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5112 if (attr)
5113 {
5114 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5115 tree cleanup_decl = lookup_name (cleanup_id);
5116 tree cleanup;
5117 vec<tree, va_gc> *v;
5118
5119 /* Build "cleanup(&decl)" for the destructor. */
5120 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
5121 vec_alloc (v, 1);
5122 v->quick_push (cleanup);
5123 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5124 vNULL, cleanup_decl, v, NULL);
5125 vec_free (v);
5126
5127 /* Don't warn about decl unused; the cleanup uses it. */
5128 TREE_USED (decl) = 1;
5129 TREE_USED (cleanup_decl) = 1;
5130 DECL_READ_P (decl) = 1;
5131
5132 push_cleanup (decl, cleanup, false);
5133 }
5134 }
5135
5136 if (warn_cxx_compat
5137 && VAR_P (decl)
5138 && !DECL_EXTERNAL (decl)
5139 && DECL_INITIAL (decl) == NULL_TREE)
5140 {
5141 type = strip_array_types (type);
5142 if (TREE_READONLY (decl))
5143 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5144 "uninitialized const %qD is invalid in C++", decl);
5145 else if (RECORD_OR_UNION_TYPE_P (type)
5146 && C_TYPE_FIELDS_READONLY (type))
5147 diagnose_uninitialized_cst_member (decl, type);
5148 }
5149
5150 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5151 }
5152
5153 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5154 EXPR is NULL or a pointer to an expression that needs to be
5155 evaluated for the side effects of array size expressions in the
5156 parameters. */
5157
5158 tree
5159 grokparm (const struct c_parm *parm, tree *expr)
5160 {
5161 tree attrs = parm->attrs;
5162 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5163 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5164
5165 decl_attributes (&decl, attrs, 0);
5166
5167 return decl;
5168 }
5169
5170 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5171 and push that on the current scope. EXPR is a pointer to an
5172 expression that needs to be evaluated for the side effects of array
5173 size expressions in the parameters. */
5174
5175 void
5176 push_parm_decl (const struct c_parm *parm, tree *expr)
5177 {
5178 tree attrs = parm->attrs;
5179 tree decl;
5180
5181 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5182 &attrs, expr, NULL, DEPRECATED_NORMAL);
5183 decl_attributes (&decl, attrs, 0);
5184
5185 decl = pushdecl (decl);
5186
5187 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5188 }
5189
5190 /* Mark all the parameter declarations to date as forward decls.
5191 Also diagnose use of this extension. */
5192
5193 void
5194 mark_forward_parm_decls (void)
5195 {
5196 struct c_binding *b;
5197
5198 if (pedantic && !current_scope->warned_forward_parm_decls)
5199 {
5200 pedwarn (input_location, OPT_Wpedantic,
5201 "ISO C forbids forward parameter declarations");
5202 current_scope->warned_forward_parm_decls = true;
5203 }
5204
5205 for (b = current_scope->bindings; b; b = b->prev)
5206 if (TREE_CODE (b->decl) == PARM_DECL)
5207 TREE_ASM_WRITTEN (b->decl) = 1;
5208 }
5209 \f
5210 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5211 literal, which may be an incomplete array type completed by the
5212 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5213 literal. NON_CONST is true if the initializers contain something
5214 that cannot occur in a constant expression. */
5215
5216 tree
5217 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
5218 {
5219 /* We do not use start_decl here because we have a type, not a declarator;
5220 and do not use finish_decl because the decl should be stored inside
5221 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5222 tree decl;
5223 tree complit;
5224 tree stmt;
5225
5226 if (type == error_mark_node
5227 || init == error_mark_node)
5228 return error_mark_node;
5229
5230 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5231 DECL_EXTERNAL (decl) = 0;
5232 TREE_PUBLIC (decl) = 0;
5233 TREE_STATIC (decl) = (current_scope == file_scope);
5234 DECL_CONTEXT (decl) = current_function_decl;
5235 TREE_USED (decl) = 1;
5236 DECL_READ_P (decl) = 1;
5237 TREE_TYPE (decl) = type;
5238 TREE_READONLY (decl) = (TYPE_READONLY (type)
5239 || (TREE_CODE (type) == ARRAY_TYPE
5240 && TYPE_READONLY (TREE_TYPE (type))));
5241 store_init_value (loc, decl, init, NULL_TREE);
5242
5243 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5244 {
5245 int failure = complete_array_type (&TREE_TYPE (decl),
5246 DECL_INITIAL (decl), true);
5247 /* If complete_array_type returns 3, it means that the
5248 initial value of the compound literal is empty. Allow it. */
5249 gcc_assert (failure == 0 || failure == 3);
5250
5251 type = TREE_TYPE (decl);
5252 TREE_TYPE (DECL_INITIAL (decl)) = type;
5253 }
5254
5255 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5256 {
5257 c_incomplete_type_error (loc, NULL_TREE, type);
5258 return error_mark_node;
5259 }
5260
5261 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5262 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5263 TREE_SIDE_EFFECTS (complit) = 1;
5264
5265 layout_decl (decl, 0);
5266
5267 if (TREE_STATIC (decl))
5268 {
5269 /* This decl needs a name for the assembler output. */
5270 set_compound_literal_name (decl);
5271 DECL_DEFER_OUTPUT (decl) = 1;
5272 DECL_COMDAT (decl) = 1;
5273 DECL_ARTIFICIAL (decl) = 1;
5274 DECL_IGNORED_P (decl) = 1;
5275 pushdecl (decl);
5276 rest_of_decl_compilation (decl, 1, 0);
5277 }
5278
5279 if (non_const)
5280 {
5281 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5282 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5283 }
5284
5285 return complit;
5286 }
5287
5288 /* Check the type of a compound literal. Here we just check that it
5289 is valid for C++. */
5290
5291 void
5292 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5293 {
5294 if (warn_cxx_compat
5295 && (type_name->specs->typespec_kind == ctsk_tagdef
5296 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5297 warning_at (loc, OPT_Wc___compat,
5298 "defining a type in a compound literal is invalid in C++");
5299 }
5300 \f
5301 /* Determine whether TYPE is a structure with a flexible array member,
5302 or a union containing such a structure (possibly recursively). */
5303
5304 static bool
5305 flexible_array_type_p (tree type)
5306 {
5307 tree x;
5308 switch (TREE_CODE (type))
5309 {
5310 case RECORD_TYPE:
5311 x = TYPE_FIELDS (type);
5312 if (x == NULL_TREE)
5313 return false;
5314 while (DECL_CHAIN (x) != NULL_TREE)
5315 x = DECL_CHAIN (x);
5316 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5317 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5318 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5319 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5320 return true;
5321 return false;
5322 case UNION_TYPE:
5323 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5324 {
5325 if (flexible_array_type_p (TREE_TYPE (x)))
5326 return true;
5327 }
5328 return false;
5329 default:
5330 return false;
5331 }
5332 }
5333 \f
5334 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5335 replacing with appropriate values if they are invalid. */
5336
5337 static void
5338 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5339 tree orig_name)
5340 {
5341 tree type_mv;
5342 unsigned int max_width;
5343 unsigned HOST_WIDE_INT w;
5344 const char *name = (orig_name
5345 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5346 : _("<anonymous>"));
5347
5348 /* Detect and ignore out of range field width and process valid
5349 field widths. */
5350 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5351 {
5352 error_at (loc, "bit-field %qs width not an integer constant", name);
5353 *width = integer_one_node;
5354 }
5355 else
5356 {
5357 if (TREE_CODE (*width) != INTEGER_CST)
5358 {
5359 *width = c_fully_fold (*width, false, NULL);
5360 if (TREE_CODE (*width) == INTEGER_CST)
5361 pedwarn (loc, OPT_Wpedantic,
5362 "bit-field %qs width not an integer constant expression",
5363 name);
5364 }
5365 if (TREE_CODE (*width) != INTEGER_CST)
5366 {
5367 error_at (loc, "bit-field %qs width not an integer constant", name);
5368 *width = integer_one_node;
5369 }
5370 constant_expression_warning (*width);
5371 if (tree_int_cst_sgn (*width) < 0)
5372 {
5373 error_at (loc, "negative width in bit-field %qs", name);
5374 *width = integer_one_node;
5375 }
5376 else if (integer_zerop (*width) && orig_name)
5377 {
5378 error_at (loc, "zero width for bit-field %qs", name);
5379 *width = integer_one_node;
5380 }
5381 }
5382
5383 /* Detect invalid bit-field type. */
5384 if (TREE_CODE (*type) != INTEGER_TYPE
5385 && TREE_CODE (*type) != BOOLEAN_TYPE
5386 && TREE_CODE (*type) != ENUMERAL_TYPE)
5387 {
5388 error_at (loc, "bit-field %qs has invalid type", name);
5389 *type = unsigned_type_node;
5390 }
5391
5392 type_mv = TYPE_MAIN_VARIANT (*type);
5393 if (!in_system_header_at (input_location)
5394 && type_mv != integer_type_node
5395 && type_mv != unsigned_type_node
5396 && type_mv != boolean_type_node)
5397 pedwarn_c90 (loc, OPT_Wpedantic,
5398 "type of bit-field %qs is a GCC extension", name);
5399
5400 max_width = TYPE_PRECISION (*type);
5401
5402 if (0 < compare_tree_int (*width, max_width))
5403 {
5404 error_at (loc, "width of %qs exceeds its type", name);
5405 w = max_width;
5406 *width = build_int_cst (integer_type_node, w);
5407 }
5408 else
5409 w = tree_to_uhwi (*width);
5410
5411 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5412 {
5413 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5414 if (!lt
5415 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5416 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5417 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5418 }
5419 }
5420
5421 \f
5422
5423 /* Print warning about variable length array if necessary. */
5424
5425 static void
5426 warn_variable_length_array (tree name, tree size)
5427 {
5428 if (TREE_CONSTANT (size))
5429 {
5430 if (name)
5431 pedwarn_c90 (input_location, OPT_Wvla,
5432 "ISO C90 forbids array %qE whose size "
5433 "can%'t be evaluated", name);
5434 else
5435 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5436 "whose size can%'t be evaluated");
5437 }
5438 else
5439 {
5440 if (name)
5441 pedwarn_c90 (input_location, OPT_Wvla,
5442 "ISO C90 forbids variable length array %qE", name);
5443 else
5444 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5445 "length array");
5446 }
5447 }
5448
5449 /* Print warning about defaulting to int if necessary. */
5450
5451 static void
5452 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5453 {
5454 diagnostic_info diagnostic;
5455 va_list ap;
5456 rich_location richloc (line_table, location);
5457
5458 va_start (ap, gmsgid);
5459 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5460 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5461 diagnostic.option_index = opt;
5462 report_diagnostic (&diagnostic);
5463 va_end (ap);
5464 }
5465
5466 /* Given declspecs and a declarator,
5467 determine the name and type of the object declared
5468 and construct a ..._DECL node for it.
5469 (In one case we can return a ..._TYPE node instead.
5470 For invalid input we sometimes return 0.)
5471
5472 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5473
5474 DECL_CONTEXT says which syntactic context this declaration is in:
5475 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5476 FUNCDEF for a function definition. Like NORMAL but a few different
5477 error messages in each case. Return value may be zero meaning
5478 this definition is too screwy to try to parse.
5479 PARM for a parameter declaration (either within a function prototype
5480 or before a function body). Make a PARM_DECL, or return void_type_node.
5481 TYPENAME if for a typename (in a cast or sizeof).
5482 Don't make a DECL node; just return the ..._TYPE node.
5483 FIELD for a struct or union field; make a FIELD_DECL.
5484 INITIALIZED is true if the decl has an initializer.
5485 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5486 representing the width of the bit-field.
5487 DECL_ATTRS points to the list of attributes that should be added to this
5488 decl. Any nested attributes that belong on the decl itself will be
5489 added to this list.
5490 If EXPR is not NULL, any expressions that need to be evaluated as
5491 part of evaluating variably modified types will be stored in *EXPR.
5492 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5493 set to indicate whether operands in *EXPR can be used in constant
5494 expressions.
5495 DEPRECATED_STATE is a deprecated_states value indicating whether
5496 deprecation warnings should be suppressed.
5497
5498 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5499 It may also be so in the PARM case, for a prototype where the
5500 argument type is specified but not the name.
5501
5502 This function is where the complicated C meanings of `static'
5503 and `extern' are interpreted. */
5504
5505 static tree
5506 grokdeclarator (const struct c_declarator *declarator,
5507 struct c_declspecs *declspecs,
5508 enum decl_context decl_context, bool initialized, tree *width,
5509 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5510 enum deprecated_states deprecated_state)
5511 {
5512 tree type = declspecs->type;
5513 bool threadp = declspecs->thread_p;
5514 enum c_storage_class storage_class = declspecs->storage_class;
5515 int constp;
5516 int restrictp;
5517 int volatilep;
5518 int atomicp;
5519 int type_quals = TYPE_UNQUALIFIED;
5520 tree name = NULL_TREE;
5521 bool funcdef_flag = false;
5522 bool funcdef_syntax = false;
5523 bool size_varies = false;
5524 tree decl_attr = declspecs->decl_attr;
5525 int array_ptr_quals = TYPE_UNQUALIFIED;
5526 tree array_ptr_attrs = NULL_TREE;
5527 int array_parm_static = 0;
5528 bool array_parm_vla_unspec_p = false;
5529 tree returned_attrs = NULL_TREE;
5530 bool bitfield = width != NULL;
5531 tree element_type;
5532 tree orig_qual_type = NULL;
5533 size_t orig_qual_indirect = 0;
5534 struct c_arg_info *arg_info = 0;
5535 addr_space_t as1, as2, address_space;
5536 location_t loc = UNKNOWN_LOCATION;
5537 tree expr_dummy;
5538 bool expr_const_operands_dummy;
5539 enum c_declarator_kind first_non_attr_kind;
5540 unsigned int alignas_align = 0;
5541
5542 if (TREE_CODE (type) == ERROR_MARK)
5543 return error_mark_node;
5544 if (expr == NULL)
5545 expr = &expr_dummy;
5546 if (expr_const_operands == NULL)
5547 expr_const_operands = &expr_const_operands_dummy;
5548
5549 *expr = declspecs->expr;
5550 *expr_const_operands = declspecs->expr_const_operands;
5551
5552 if (decl_context == FUNCDEF)
5553 funcdef_flag = true, decl_context = NORMAL;
5554
5555 /* Look inside a declarator for the name being declared
5556 and get it as an IDENTIFIER_NODE, for an error message. */
5557 {
5558 const struct c_declarator *decl = declarator;
5559
5560 first_non_attr_kind = cdk_attrs;
5561 while (decl)
5562 switch (decl->kind)
5563 {
5564 case cdk_array:
5565 loc = decl->id_loc;
5566 /* FALL THRU. */
5567
5568 case cdk_function:
5569 case cdk_pointer:
5570 funcdef_syntax = (decl->kind == cdk_function);
5571 if (first_non_attr_kind == cdk_attrs)
5572 first_non_attr_kind = decl->kind;
5573 decl = decl->declarator;
5574 break;
5575
5576 case cdk_attrs:
5577 decl = decl->declarator;
5578 break;
5579
5580 case cdk_id:
5581 loc = decl->id_loc;
5582 if (decl->u.id)
5583 name = decl->u.id;
5584 if (first_non_attr_kind == cdk_attrs)
5585 first_non_attr_kind = decl->kind;
5586 decl = 0;
5587 break;
5588
5589 default:
5590 gcc_unreachable ();
5591 }
5592 if (name == 0)
5593 {
5594 gcc_assert (decl_context == PARM
5595 || decl_context == TYPENAME
5596 || (decl_context == FIELD
5597 && declarator->kind == cdk_id));
5598 gcc_assert (!initialized);
5599 }
5600 }
5601
5602 /* A function definition's declarator must have the form of
5603 a function declarator. */
5604
5605 if (funcdef_flag && !funcdef_syntax)
5606 return 0;
5607
5608 /* If this looks like a function definition, make it one,
5609 even if it occurs where parms are expected.
5610 Then store_parm_decls will reject it and not use it as a parm. */
5611 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5612 decl_context = PARM;
5613
5614 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5615 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5616
5617 if ((decl_context == NORMAL || decl_context == FIELD)
5618 && current_scope == file_scope
5619 && variably_modified_type_p (type, NULL_TREE))
5620 {
5621 if (name)
5622 error_at (loc, "variably modified %qE at file scope", name);
5623 else
5624 error_at (loc, "variably modified field at file scope");
5625 type = integer_type_node;
5626 }
5627
5628 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5629
5630 /* Diagnose defaulting to "int". */
5631
5632 if (declspecs->default_int_p && !in_system_header_at (input_location))
5633 {
5634 /* Issue a warning if this is an ISO C 99 program or if
5635 -Wreturn-type and this is a function, or if -Wimplicit;
5636 prefer the former warning since it is more explicit. */
5637 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5638 && funcdef_flag)
5639 warn_about_return_type = 1;
5640 else
5641 {
5642 if (name)
5643 warn_defaults_to (loc, OPT_Wimplicit_int,
5644 "type defaults to %<int%> in declaration "
5645 "of %qE", name);
5646 else
5647 warn_defaults_to (loc, OPT_Wimplicit_int,
5648 "type defaults to %<int%> in type name");
5649 }
5650 }
5651
5652 /* Adjust the type if a bit-field is being declared,
5653 -funsigned-bitfields applied and the type is not explicitly
5654 "signed". */
5655 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5656 && TREE_CODE (type) == INTEGER_TYPE)
5657 type = unsigned_type_for (type);
5658
5659 /* Figure out the type qualifiers for the declaration. There are
5660 two ways a declaration can become qualified. One is something
5661 like `const int i' where the `const' is explicit. Another is
5662 something like `typedef const int CI; CI i' where the type of the
5663 declaration contains the `const'. A third possibility is that
5664 there is a type qualifier on the element type of a typedefed
5665 array type, in which case we should extract that qualifier so
5666 that c_apply_type_quals_to_decl receives the full list of
5667 qualifiers to work with (C90 is not entirely clear about whether
5668 duplicate qualifiers should be diagnosed in this case, but it
5669 seems most appropriate to do so). */
5670 element_type = strip_array_types (type);
5671 constp = declspecs->const_p + TYPE_READONLY (element_type);
5672 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5673 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5674 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5675 as1 = declspecs->address_space;
5676 as2 = TYPE_ADDR_SPACE (element_type);
5677 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5678
5679 if (constp > 1)
5680 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5681 if (restrictp > 1)
5682 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5683 if (volatilep > 1)
5684 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5685 if (atomicp > 1)
5686 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5687
5688 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5689 error_at (loc, "conflicting named address spaces (%s vs %s)",
5690 c_addr_space_name (as1), c_addr_space_name (as2));
5691
5692 if ((TREE_CODE (type) == ARRAY_TYPE
5693 || first_non_attr_kind == cdk_array)
5694 && TYPE_QUALS (element_type))
5695 {
5696 orig_qual_type = type;
5697 type = TYPE_MAIN_VARIANT (type);
5698 }
5699 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5700 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5701 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5702 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5703 | ENCODE_QUAL_ADDR_SPACE (address_space));
5704 if (type_quals != TYPE_QUALS (element_type))
5705 orig_qual_type = NULL_TREE;
5706
5707 /* Applying the _Atomic qualifier to an array type (through the use
5708 of typedefs or typeof) must be detected here. If the qualifier
5709 is introduced later, any appearance of applying it to an array is
5710 actually applying it to an element of that array. */
5711 if (atomicp && TREE_CODE (type) == ARRAY_TYPE)
5712 error_at (loc, "%<_Atomic%>-qualified array type");
5713
5714 /* Warn about storage classes that are invalid for certain
5715 kinds of declarations (parameters, typenames, etc.). */
5716
5717 if (funcdef_flag
5718 && (threadp
5719 || storage_class == csc_auto
5720 || storage_class == csc_register
5721 || storage_class == csc_typedef))
5722 {
5723 if (storage_class == csc_auto)
5724 pedwarn (loc,
5725 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5726 "function definition declared %<auto%>");
5727 if (storage_class == csc_register)
5728 error_at (loc, "function definition declared %<register%>");
5729 if (storage_class == csc_typedef)
5730 error_at (loc, "function definition declared %<typedef%>");
5731 if (threadp)
5732 error_at (loc, "function definition declared %qs",
5733 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5734 threadp = false;
5735 if (storage_class == csc_auto
5736 || storage_class == csc_register
5737 || storage_class == csc_typedef)
5738 storage_class = csc_none;
5739 }
5740 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5741 {
5742 if (decl_context == PARM && storage_class == csc_register)
5743 ;
5744 else
5745 {
5746 switch (decl_context)
5747 {
5748 case FIELD:
5749 if (name)
5750 error_at (loc, "storage class specified for structure "
5751 "field %qE", name);
5752 else
5753 error_at (loc, "storage class specified for structure field");
5754 break;
5755 case PARM:
5756 if (name)
5757 error_at (loc, "storage class specified for parameter %qE",
5758 name);
5759 else
5760 error_at (loc, "storage class specified for unnamed parameter");
5761 break;
5762 default:
5763 error_at (loc, "storage class specified for typename");
5764 break;
5765 }
5766 storage_class = csc_none;
5767 threadp = false;
5768 }
5769 }
5770 else if (storage_class == csc_extern
5771 && initialized
5772 && !funcdef_flag)
5773 {
5774 /* 'extern' with initialization is invalid if not at file scope. */
5775 if (current_scope == file_scope)
5776 {
5777 /* It is fine to have 'extern const' when compiling at C
5778 and C++ intersection. */
5779 if (!(warn_cxx_compat && constp))
5780 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5781 name);
5782 }
5783 else
5784 error_at (loc, "%qE has both %<extern%> and initializer", name);
5785 }
5786 else if (current_scope == file_scope)
5787 {
5788 if (storage_class == csc_auto)
5789 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5790 name);
5791 if (pedantic && storage_class == csc_register)
5792 pedwarn (input_location, OPT_Wpedantic,
5793 "file-scope declaration of %qE specifies %<register%>", name);
5794 }
5795 else
5796 {
5797 if (storage_class == csc_extern && funcdef_flag)
5798 error_at (loc, "nested function %qE declared %<extern%>", name);
5799 else if (threadp && storage_class == csc_none)
5800 {
5801 error_at (loc, "function-scope %qE implicitly auto and declared "
5802 "%qs", name,
5803 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5804 threadp = false;
5805 }
5806 }
5807
5808 /* Now figure out the structure of the declarator proper.
5809 Descend through it, creating more complex types, until we reach
5810 the declared identifier (or NULL_TREE, in an absolute declarator).
5811 At each stage we maintain an unqualified version of the type
5812 together with any qualifiers that should be applied to it with
5813 c_build_qualified_type; this way, array types including
5814 multidimensional array types are first built up in unqualified
5815 form and then the qualified form is created with
5816 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5817
5818 while (declarator && declarator->kind != cdk_id)
5819 {
5820 if (type == error_mark_node)
5821 {
5822 declarator = declarator->declarator;
5823 continue;
5824 }
5825
5826 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5827 a cdk_pointer (for *...),
5828 a cdk_function (for ...(...)),
5829 a cdk_attrs (for nested attributes),
5830 or a cdk_id (for the name being declared
5831 or the place in an absolute declarator
5832 where the name was omitted).
5833 For the last case, we have just exited the loop.
5834
5835 At this point, TYPE is the type of elements of an array,
5836 or for a function to return, or for a pointer to point to.
5837 After this sequence of ifs, TYPE is the type of the
5838 array or function or pointer, and DECLARATOR has had its
5839 outermost layer removed. */
5840
5841 if (array_ptr_quals != TYPE_UNQUALIFIED
5842 || array_ptr_attrs != NULL_TREE
5843 || array_parm_static)
5844 {
5845 /* Only the innermost declarator (making a parameter be of
5846 array type which is converted to pointer type)
5847 may have static or type qualifiers. */
5848 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5849 array_ptr_quals = TYPE_UNQUALIFIED;
5850 array_ptr_attrs = NULL_TREE;
5851 array_parm_static = 0;
5852 }
5853
5854 switch (declarator->kind)
5855 {
5856 case cdk_attrs:
5857 {
5858 /* A declarator with embedded attributes. */
5859 tree attrs = declarator->u.attrs;
5860 const struct c_declarator *inner_decl;
5861 int attr_flags = 0;
5862 declarator = declarator->declarator;
5863 inner_decl = declarator;
5864 while (inner_decl->kind == cdk_attrs)
5865 inner_decl = inner_decl->declarator;
5866 if (inner_decl->kind == cdk_id)
5867 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5868 else if (inner_decl->kind == cdk_function)
5869 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5870 else if (inner_decl->kind == cdk_array)
5871 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5872 returned_attrs = decl_attributes (&type,
5873 chainon (returned_attrs, attrs),
5874 attr_flags);
5875 break;
5876 }
5877 case cdk_array:
5878 {
5879 tree itype = NULL_TREE;
5880 tree size = declarator->u.array.dimen;
5881 /* The index is a signed object `sizetype' bits wide. */
5882 tree index_type = c_common_signed_type (sizetype);
5883
5884 array_ptr_quals = declarator->u.array.quals;
5885 array_ptr_attrs = declarator->u.array.attrs;
5886 array_parm_static = declarator->u.array.static_p;
5887 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5888
5889 declarator = declarator->declarator;
5890
5891 /* Check for some types that there cannot be arrays of. */
5892
5893 if (VOID_TYPE_P (type))
5894 {
5895 if (name)
5896 error_at (loc, "declaration of %qE as array of voids", name);
5897 else
5898 error_at (loc, "declaration of type name as array of voids");
5899 type = error_mark_node;
5900 }
5901
5902 if (TREE_CODE (type) == FUNCTION_TYPE)
5903 {
5904 if (name)
5905 error_at (loc, "declaration of %qE as array of functions",
5906 name);
5907 else
5908 error_at (loc, "declaration of type name as array of "
5909 "functions");
5910 type = error_mark_node;
5911 }
5912
5913 if (pedantic && !in_system_header_at (input_location)
5914 && flexible_array_type_p (type))
5915 pedwarn (loc, OPT_Wpedantic,
5916 "invalid use of structure with flexible array member");
5917
5918 if (size == error_mark_node)
5919 type = error_mark_node;
5920
5921 if (type == error_mark_node)
5922 continue;
5923
5924 /* If size was specified, set ITYPE to a range-type for
5925 that size. Otherwise, ITYPE remains null. finish_decl
5926 may figure it out from an initial value. */
5927
5928 if (size)
5929 {
5930 bool size_maybe_const = true;
5931 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5932 && !TREE_OVERFLOW (size));
5933 bool this_size_varies = false;
5934
5935 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5936 lvalue. */
5937 STRIP_TYPE_NOPS (size);
5938
5939 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5940 {
5941 if (name)
5942 error_at (loc, "size of array %qE has non-integer type",
5943 name);
5944 else
5945 error_at (loc,
5946 "size of unnamed array has non-integer type");
5947 size = integer_one_node;
5948 }
5949 /* This can happen with enum forward declaration. */
5950 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
5951 {
5952 if (name)
5953 error_at (loc, "size of array %qE has incomplete type",
5954 name);
5955 else
5956 error_at (loc, "size of unnamed array has incomplete "
5957 "type");
5958 size = integer_one_node;
5959 }
5960
5961 size = c_fully_fold (size, false, &size_maybe_const);
5962
5963 if (pedantic && size_maybe_const && integer_zerop (size))
5964 {
5965 if (name)
5966 pedwarn (loc, OPT_Wpedantic,
5967 "ISO C forbids zero-size array %qE", name);
5968 else
5969 pedwarn (loc, OPT_Wpedantic,
5970 "ISO C forbids zero-size array");
5971 }
5972
5973 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5974 {
5975 constant_expression_warning (size);
5976 if (tree_int_cst_sgn (size) < 0)
5977 {
5978 if (name)
5979 error_at (loc, "size of array %qE is negative", name);
5980 else
5981 error_at (loc, "size of unnamed array is negative");
5982 size = integer_one_node;
5983 }
5984 /* Handle a size folded to an integer constant but
5985 not an integer constant expression. */
5986 if (!size_int_const)
5987 {
5988 /* If this is a file scope declaration of an
5989 ordinary identifier, this is invalid code;
5990 diagnosing it here and not subsequently
5991 treating the type as variable-length avoids
5992 more confusing diagnostics later. */
5993 if ((decl_context == NORMAL || decl_context == FIELD)
5994 && current_scope == file_scope)
5995 pedwarn (input_location, 0,
5996 "variably modified %qE at file scope",
5997 name);
5998 else
5999 this_size_varies = size_varies = true;
6000 warn_variable_length_array (name, size);
6001 }
6002 }
6003 else if ((decl_context == NORMAL || decl_context == FIELD)
6004 && current_scope == file_scope)
6005 {
6006 error_at (loc, "variably modified %qE at file scope", name);
6007 size = integer_one_node;
6008 }
6009 else
6010 {
6011 /* Make sure the array size remains visibly
6012 nonconstant even if it is (eg) a const variable
6013 with known value. */
6014 this_size_varies = size_varies = true;
6015 warn_variable_length_array (name, size);
6016 if (flag_sanitize & SANITIZE_VLA
6017 && decl_context == NORMAL
6018 && do_ubsan_in_current_function ())
6019 {
6020 /* Evaluate the array size only once. */
6021 size = c_save_expr (size);
6022 size = c_fully_fold (size, false, NULL);
6023 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6024 ubsan_instrument_vla (loc, size),
6025 size);
6026 }
6027 }
6028
6029 if (integer_zerop (size) && !this_size_varies)
6030 {
6031 /* A zero-length array cannot be represented with
6032 an unsigned index type, which is what we'll
6033 get with build_index_type. Create an
6034 open-ended range instead. */
6035 itype = build_range_type (sizetype, size, NULL_TREE);
6036 }
6037 else
6038 {
6039 /* Arrange for the SAVE_EXPR on the inside of the
6040 MINUS_EXPR, which allows the -1 to get folded
6041 with the +1 that happens when building TYPE_SIZE. */
6042 if (size_varies)
6043 size = save_expr (size);
6044 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6045 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6046 integer_zero_node, size);
6047
6048 /* Compute the maximum valid index, that is, size
6049 - 1. Do the calculation in index_type, so that
6050 if it is a variable the computations will be
6051 done in the proper mode. */
6052 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6053 convert (index_type, size),
6054 convert (index_type,
6055 size_one_node));
6056
6057 /* The above overflows when size does not fit
6058 in index_type.
6059 ??? While a size of INT_MAX+1 technically shouldn't
6060 cause an overflow (because we subtract 1), handling
6061 this case seems like an unnecessary complication. */
6062 if (TREE_CODE (size) == INTEGER_CST
6063 && !int_fits_type_p (size, index_type))
6064 {
6065 if (name)
6066 error_at (loc, "size of array %qE is too large",
6067 name);
6068 else
6069 error_at (loc, "size of unnamed array is too large");
6070 type = error_mark_node;
6071 continue;
6072 }
6073
6074 itype = build_index_type (itype);
6075 }
6076 if (this_size_varies)
6077 {
6078 if (*expr)
6079 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6080 *expr, size);
6081 else
6082 *expr = size;
6083 *expr_const_operands &= size_maybe_const;
6084 }
6085 }
6086 else if (decl_context == FIELD)
6087 {
6088 bool flexible_array_member = false;
6089 if (array_parm_vla_unspec_p)
6090 /* Field names can in fact have function prototype
6091 scope so [*] is disallowed here through making
6092 the field variably modified, not through being
6093 something other than a declaration with function
6094 prototype scope. */
6095 size_varies = true;
6096 else
6097 {
6098 const struct c_declarator *t = declarator;
6099 while (t->kind == cdk_attrs)
6100 t = t->declarator;
6101 flexible_array_member = (t->kind == cdk_id);
6102 }
6103 if (flexible_array_member
6104 && !in_system_header_at (input_location))
6105 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6106 "support flexible array members");
6107
6108 /* ISO C99 Flexible array members are effectively
6109 identical to GCC's zero-length array extension. */
6110 if (flexible_array_member || array_parm_vla_unspec_p)
6111 itype = build_range_type (sizetype, size_zero_node,
6112 NULL_TREE);
6113 }
6114 else if (decl_context == PARM)
6115 {
6116 if (array_parm_vla_unspec_p)
6117 {
6118 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6119 size_varies = true;
6120 }
6121 }
6122 else if (decl_context == TYPENAME)
6123 {
6124 if (array_parm_vla_unspec_p)
6125 {
6126 /* C99 6.7.5.2p4 */
6127 warning (0, "%<[*]%> not in a declaration");
6128 /* We use this to avoid messing up with incomplete
6129 array types of the same type, that would
6130 otherwise be modified below. */
6131 itype = build_range_type (sizetype, size_zero_node,
6132 NULL_TREE);
6133 size_varies = true;
6134 }
6135 }
6136
6137 /* Complain about arrays of incomplete types. */
6138 if (!COMPLETE_TYPE_P (type))
6139 {
6140 error_at (loc, "array type has incomplete element type %qT",
6141 type);
6142 /* See if we can be more helpful. */
6143 if (TREE_CODE (type) == ARRAY_TYPE)
6144 {
6145 if (name)
6146 inform (loc, "declaration of %qE as multidimensional "
6147 "array must have bounds for all dimensions "
6148 "except the first", name);
6149 else
6150 inform (loc, "declaration of multidimensional array "
6151 "must have bounds for all dimensions except "
6152 "the first");
6153 }
6154 type = error_mark_node;
6155 }
6156 else
6157 /* When itype is NULL, a shared incomplete array type is
6158 returned for all array of a given type. Elsewhere we
6159 make sure we don't complete that type before copying
6160 it, but here we want to make sure we don't ever
6161 modify the shared type, so we gcc_assert (itype)
6162 below. */
6163 {
6164 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6165 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6166 type = build_qualified_type (type,
6167 ENCODE_QUAL_ADDR_SPACE (as));
6168
6169 type = build_array_type (type, itype);
6170 }
6171
6172 if (type != error_mark_node)
6173 {
6174 if (size_varies)
6175 {
6176 /* It is ok to modify type here even if itype is
6177 NULL: if size_varies, we're in a
6178 multi-dimensional array and the inner type has
6179 variable size, so the enclosing shared array type
6180 must too. */
6181 if (size && TREE_CODE (size) == INTEGER_CST)
6182 type
6183 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6184 C_TYPE_VARIABLE_SIZE (type) = 1;
6185 }
6186
6187 /* The GCC extension for zero-length arrays differs from
6188 ISO flexible array members in that sizeof yields
6189 zero. */
6190 if (size && integer_zerop (size))
6191 {
6192 gcc_assert (itype);
6193 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6194 TYPE_SIZE (type) = bitsize_zero_node;
6195 TYPE_SIZE_UNIT (type) = size_zero_node;
6196 SET_TYPE_STRUCTURAL_EQUALITY (type);
6197 }
6198 if (array_parm_vla_unspec_p)
6199 {
6200 gcc_assert (itype);
6201 /* The type is complete. C99 6.7.5.2p4 */
6202 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6203 TYPE_SIZE (type) = bitsize_zero_node;
6204 TYPE_SIZE_UNIT (type) = size_zero_node;
6205 SET_TYPE_STRUCTURAL_EQUALITY (type);
6206 }
6207
6208 if (!valid_array_size_p (loc, type, name))
6209 type = error_mark_node;
6210 }
6211
6212 if (decl_context != PARM
6213 && (array_ptr_quals != TYPE_UNQUALIFIED
6214 || array_ptr_attrs != NULL_TREE
6215 || array_parm_static))
6216 {
6217 error_at (loc, "static or type qualifiers in non-parameter "
6218 "array declarator");
6219 array_ptr_quals = TYPE_UNQUALIFIED;
6220 array_ptr_attrs = NULL_TREE;
6221 array_parm_static = 0;
6222 }
6223 orig_qual_indirect++;
6224 break;
6225 }
6226 case cdk_function:
6227 {
6228 /* Say it's a definition only for the declarator closest
6229 to the identifier, apart possibly from some
6230 attributes. */
6231 bool really_funcdef = false;
6232 tree arg_types;
6233 orig_qual_type = NULL_TREE;
6234 if (funcdef_flag)
6235 {
6236 const struct c_declarator *t = declarator->declarator;
6237 while (t->kind == cdk_attrs)
6238 t = t->declarator;
6239 really_funcdef = (t->kind == cdk_id);
6240 }
6241
6242 /* Declaring a function type. Make sure we have a valid
6243 type for the function to return. */
6244 if (type == error_mark_node)
6245 continue;
6246
6247 size_varies = false;
6248
6249 /* Warn about some types functions can't return. */
6250 if (TREE_CODE (type) == FUNCTION_TYPE)
6251 {
6252 if (name)
6253 error_at (loc, "%qE declared as function returning a "
6254 "function", name);
6255 else
6256 error_at (loc, "type name declared as function "
6257 "returning a function");
6258 type = integer_type_node;
6259 }
6260 if (TREE_CODE (type) == ARRAY_TYPE)
6261 {
6262 if (name)
6263 error_at (loc, "%qE declared as function returning an array",
6264 name);
6265 else
6266 error_at (loc, "type name declared as function returning "
6267 "an array");
6268 type = integer_type_node;
6269 }
6270
6271 /* Construct the function type and go to the next
6272 inner layer of declarator. */
6273 arg_info = declarator->u.arg_info;
6274 arg_types = grokparms (arg_info, really_funcdef);
6275
6276 /* Type qualifiers before the return type of the function
6277 qualify the return type, not the function type. */
6278 if (type_quals)
6279 {
6280 int quals_used = type_quals;
6281 /* Type qualifiers on a function return type are
6282 normally permitted by the standard but have no
6283 effect, so give a warning at -Wreturn-type.
6284 Qualifiers on a void return type are banned on
6285 function definitions in ISO C; GCC used to used
6286 them for noreturn functions. The resolution of C11
6287 DR#423 means qualifiers (other than _Atomic) are
6288 actually removed from the return type when
6289 determining the function type. */
6290 if (flag_isoc11)
6291 quals_used &= TYPE_QUAL_ATOMIC;
6292 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6293 pedwarn (loc, 0,
6294 "function definition has qualified void return type");
6295 else
6296 warning_at (loc, OPT_Wignored_qualifiers,
6297 "type qualifiers ignored on function return type");
6298
6299 /* Ensure an error for restrict on invalid types; the
6300 DR#423 resolution is not entirely clear about
6301 this. */
6302 if (flag_isoc11
6303 && (type_quals & TYPE_QUAL_RESTRICT)
6304 && (!POINTER_TYPE_P (type)
6305 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6306 error_at (loc, "invalid use of %<restrict%>");
6307 if (quals_used)
6308 type = c_build_qualified_type (type, quals_used);
6309 }
6310 type_quals = TYPE_UNQUALIFIED;
6311
6312 type = build_function_type (type, arg_types);
6313 declarator = declarator->declarator;
6314
6315 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6316 the formal parameter list of this FUNCTION_TYPE to point to
6317 the FUNCTION_TYPE node itself. */
6318 {
6319 c_arg_tag *tag;
6320 unsigned ix;
6321
6322 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6323 TYPE_CONTEXT (tag->type) = type;
6324 }
6325 break;
6326 }
6327 case cdk_pointer:
6328 {
6329 /* Merge any constancy or volatility into the target type
6330 for the pointer. */
6331 if ((type_quals & TYPE_QUAL_ATOMIC)
6332 && TREE_CODE (type) == FUNCTION_TYPE)
6333 {
6334 error_at (loc,
6335 "%<_Atomic%>-qualified function type");
6336 type_quals &= ~TYPE_QUAL_ATOMIC;
6337 }
6338 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6339 && type_quals)
6340 pedwarn (loc, OPT_Wpedantic,
6341 "ISO C forbids qualified function types");
6342 if (type_quals)
6343 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6344 orig_qual_indirect);
6345 orig_qual_type = NULL_TREE;
6346 size_varies = false;
6347
6348 /* When the pointed-to type involves components of variable size,
6349 care must be taken to ensure that the size evaluation code is
6350 emitted early enough to dominate all the possible later uses
6351 and late enough for the variables on which it depends to have
6352 been assigned.
6353
6354 This is expected to happen automatically when the pointed-to
6355 type has a name/declaration of it's own, but special attention
6356 is required if the type is anonymous.
6357
6358 We handle the NORMAL and FIELD contexts here by attaching an
6359 artificial TYPE_DECL to such pointed-to type. This forces the
6360 sizes evaluation at a safe point and ensures it is not deferred
6361 until e.g. within a deeper conditional context.
6362
6363 We expect nothing to be needed here for PARM or TYPENAME.
6364 Pushing a TYPE_DECL at this point for TYPENAME would actually
6365 be incorrect, as we might be in the middle of an expression
6366 with side effects on the pointed-to type size "arguments" prior
6367 to the pointer declaration point and the fake TYPE_DECL in the
6368 enclosing context would force the size evaluation prior to the
6369 side effects. */
6370
6371 if (!TYPE_NAME (type)
6372 && (decl_context == NORMAL || decl_context == FIELD)
6373 && variably_modified_type_p (type, NULL_TREE))
6374 {
6375 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6376 DECL_ARTIFICIAL (decl) = 1;
6377 pushdecl (decl);
6378 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6379 TYPE_NAME (type) = decl;
6380 }
6381
6382 type = c_build_pointer_type (type);
6383
6384 /* Process type qualifiers (such as const or volatile)
6385 that were given inside the `*'. */
6386 type_quals = declarator->u.pointer_quals;
6387
6388 declarator = declarator->declarator;
6389 break;
6390 }
6391 default:
6392 gcc_unreachable ();
6393 }
6394 }
6395 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6396
6397 /* Now TYPE has the actual type, apart from any qualifiers in
6398 TYPE_QUALS. */
6399
6400 /* Warn about address space used for things other than static memory or
6401 pointers. */
6402 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6403 if (!ADDR_SPACE_GENERIC_P (address_space))
6404 {
6405 if (decl_context == NORMAL)
6406 {
6407 switch (storage_class)
6408 {
6409 case csc_auto:
6410 error ("%qs combined with %<auto%> qualifier for %qE",
6411 c_addr_space_name (address_space), name);
6412 break;
6413 case csc_register:
6414 error ("%qs combined with %<register%> qualifier for %qE",
6415 c_addr_space_name (address_space), name);
6416 break;
6417 case csc_none:
6418 if (current_function_scope)
6419 {
6420 error ("%qs specified for auto variable %qE",
6421 c_addr_space_name (address_space), name);
6422 break;
6423 }
6424 break;
6425 case csc_static:
6426 case csc_extern:
6427 case csc_typedef:
6428 break;
6429 default:
6430 gcc_unreachable ();
6431 }
6432 }
6433 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6434 {
6435 if (name)
6436 error ("%qs specified for parameter %qE",
6437 c_addr_space_name (address_space), name);
6438 else
6439 error ("%qs specified for unnamed parameter",
6440 c_addr_space_name (address_space));
6441 }
6442 else if (decl_context == FIELD)
6443 {
6444 if (name)
6445 error ("%qs specified for structure field %qE",
6446 c_addr_space_name (address_space), name);
6447 else
6448 error ("%qs specified for structure field",
6449 c_addr_space_name (address_space));
6450 }
6451 }
6452
6453 /* Check the type and width of a bit-field. */
6454 if (bitfield)
6455 {
6456 check_bitfield_type_and_width (loc, &type, width, name);
6457 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6458 atomic types are permitted for bit-fields; we have no code to
6459 make bit-field accesses atomic, so disallow them. */
6460 if (type_quals & TYPE_QUAL_ATOMIC)
6461 {
6462 if (name)
6463 error_at (loc, "bit-field %qE has atomic type", name);
6464 else
6465 error_at (loc, "bit-field has atomic type");
6466 type_quals &= ~TYPE_QUAL_ATOMIC;
6467 }
6468 }
6469
6470 /* Reject invalid uses of _Alignas. */
6471 if (declspecs->alignas_p)
6472 {
6473 if (storage_class == csc_typedef)
6474 error_at (loc, "alignment specified for typedef %qE", name);
6475 else if (storage_class == csc_register)
6476 error_at (loc, "alignment specified for %<register%> object %qE",
6477 name);
6478 else if (decl_context == PARM)
6479 {
6480 if (name)
6481 error_at (loc, "alignment specified for parameter %qE", name);
6482 else
6483 error_at (loc, "alignment specified for unnamed parameter");
6484 }
6485 else if (bitfield)
6486 {
6487 if (name)
6488 error_at (loc, "alignment specified for bit-field %qE", name);
6489 else
6490 error_at (loc, "alignment specified for unnamed bit-field");
6491 }
6492 else if (TREE_CODE (type) == FUNCTION_TYPE)
6493 error_at (loc, "alignment specified for function %qE", name);
6494 else if (declspecs->align_log != -1 && TYPE_P (type))
6495 {
6496 alignas_align = 1U << declspecs->align_log;
6497 if (alignas_align < min_align_of_type (type))
6498 {
6499 if (name)
6500 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6501 "alignment of %qE", name);
6502 else
6503 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6504 "alignment of unnamed field");
6505 alignas_align = 0;
6506 }
6507 }
6508 }
6509
6510 /* If this is declaring a typedef name, return a TYPE_DECL. */
6511
6512 if (storage_class == csc_typedef)
6513 {
6514 tree decl;
6515 if ((type_quals & TYPE_QUAL_ATOMIC)
6516 && TREE_CODE (type) == FUNCTION_TYPE)
6517 {
6518 error_at (loc,
6519 "%<_Atomic%>-qualified function type");
6520 type_quals &= ~TYPE_QUAL_ATOMIC;
6521 }
6522 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6523 && type_quals)
6524 pedwarn (loc, OPT_Wpedantic,
6525 "ISO C forbids qualified function types");
6526 if (type_quals)
6527 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6528 orig_qual_indirect);
6529 decl = build_decl (declarator->id_loc,
6530 TYPE_DECL, declarator->u.id, type);
6531 if (declspecs->explicit_signed_p)
6532 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6533 if (declspecs->inline_p)
6534 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6535 if (declspecs->noreturn_p)
6536 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6537
6538 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6539 {
6540 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6541
6542 if (b != NULL
6543 && b->decl != NULL_TREE
6544 && (B_IN_CURRENT_SCOPE (b)
6545 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6546 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6547 {
6548 warning_at (declarator->id_loc, OPT_Wc___compat,
6549 ("using %qD as both a typedef and a tag is "
6550 "invalid in C++"),
6551 decl);
6552 if (b->locus != UNKNOWN_LOCATION)
6553 inform (b->locus, "originally defined here");
6554 }
6555 }
6556
6557 return decl;
6558 }
6559
6560 /* If this is a type name (such as, in a cast or sizeof),
6561 compute the type and return it now. */
6562
6563 if (decl_context == TYPENAME)
6564 {
6565 /* Note that the grammar rejects storage classes in typenames
6566 and fields. */
6567 gcc_assert (storage_class == csc_none && !threadp
6568 && !declspecs->inline_p && !declspecs->noreturn_p);
6569 if ((type_quals & TYPE_QUAL_ATOMIC)
6570 && TREE_CODE (type) == FUNCTION_TYPE)
6571 {
6572 error_at (loc,
6573 "%<_Atomic%>-qualified function type");
6574 type_quals &= ~TYPE_QUAL_ATOMIC;
6575 }
6576 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6577 && type_quals)
6578 pedwarn (loc, OPT_Wpedantic,
6579 "ISO C forbids const or volatile function types");
6580 if (type_quals)
6581 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6582 orig_qual_indirect);
6583 return type;
6584 }
6585
6586 if (pedantic && decl_context == FIELD
6587 && variably_modified_type_p (type, NULL_TREE))
6588 {
6589 /* C99 6.7.2.1p8 */
6590 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6591 "have a variably modified type");
6592 }
6593
6594 /* Aside from typedefs and type names (handle above),
6595 `void' at top level (not within pointer)
6596 is allowed only in public variables.
6597 We don't complain about parms either, but that is because
6598 a better error message can be made later. */
6599
6600 if (VOID_TYPE_P (type) && decl_context != PARM
6601 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6602 && (storage_class == csc_extern
6603 || (current_scope == file_scope
6604 && !(storage_class == csc_static
6605 || storage_class == csc_register)))))
6606 {
6607 error_at (loc, "variable or field %qE declared void", name);
6608 type = integer_type_node;
6609 }
6610
6611 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6612 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6613
6614 {
6615 tree decl;
6616
6617 if (decl_context == PARM)
6618 {
6619 tree promoted_type;
6620 bool array_parameter_p = false;
6621
6622 /* A parameter declared as an array of T is really a pointer to T.
6623 One declared as a function is really a pointer to a function. */
6624
6625 if (TREE_CODE (type) == ARRAY_TYPE)
6626 {
6627 /* Transfer const-ness of array into that of type pointed to. */
6628 type = TREE_TYPE (type);
6629 if (orig_qual_type != NULL_TREE)
6630 {
6631 if (orig_qual_indirect == 0)
6632 orig_qual_type = TREE_TYPE (orig_qual_type);
6633 else
6634 orig_qual_indirect--;
6635 }
6636 if (type_quals)
6637 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6638 orig_qual_indirect);
6639 type = c_build_pointer_type (type);
6640 type_quals = array_ptr_quals;
6641 if (type_quals)
6642 type = c_build_qualified_type (type, type_quals);
6643
6644 /* We don't yet implement attributes in this context. */
6645 if (array_ptr_attrs != NULL_TREE)
6646 warning_at (loc, OPT_Wattributes,
6647 "attributes in parameter array declarator ignored");
6648
6649 size_varies = false;
6650 array_parameter_p = true;
6651 }
6652 else if (TREE_CODE (type) == FUNCTION_TYPE)
6653 {
6654 if (type_quals & TYPE_QUAL_ATOMIC)
6655 {
6656 error_at (loc,
6657 "%<_Atomic%>-qualified function type");
6658 type_quals &= ~TYPE_QUAL_ATOMIC;
6659 }
6660 else if (type_quals)
6661 pedwarn (loc, OPT_Wpedantic,
6662 "ISO C forbids qualified function types");
6663 if (type_quals)
6664 type = c_build_qualified_type (type, type_quals);
6665 type = c_build_pointer_type (type);
6666 type_quals = TYPE_UNQUALIFIED;
6667 }
6668 else if (type_quals)
6669 type = c_build_qualified_type (type, type_quals);
6670
6671 decl = build_decl (declarator->id_loc,
6672 PARM_DECL, declarator->u.id, type);
6673 if (size_varies)
6674 C_DECL_VARIABLE_SIZE (decl) = 1;
6675 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6676
6677 /* Compute the type actually passed in the parmlist,
6678 for the case where there is no prototype.
6679 (For example, shorts and chars are passed as ints.)
6680 When there is a prototype, this is overridden later. */
6681
6682 if (type == error_mark_node)
6683 promoted_type = type;
6684 else
6685 promoted_type = c_type_promotes_to (type);
6686
6687 DECL_ARG_TYPE (decl) = promoted_type;
6688 if (declspecs->inline_p)
6689 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6690 if (declspecs->noreturn_p)
6691 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6692 }
6693 else if (decl_context == FIELD)
6694 {
6695 /* Note that the grammar rejects storage classes in typenames
6696 and fields. */
6697 gcc_assert (storage_class == csc_none && !threadp
6698 && !declspecs->inline_p && !declspecs->noreturn_p);
6699
6700 /* Structure field. It may not be a function. */
6701
6702 if (TREE_CODE (type) == FUNCTION_TYPE)
6703 {
6704 error_at (loc, "field %qE declared as a function", name);
6705 type = build_pointer_type (type);
6706 }
6707 else if (TREE_CODE (type) != ERROR_MARK
6708 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6709 {
6710 if (name)
6711 error_at (loc, "field %qE has incomplete type", name);
6712 else
6713 error_at (loc, "unnamed field has incomplete type");
6714 type = error_mark_node;
6715 }
6716 else if (TREE_CODE (type) == ARRAY_TYPE
6717 && TYPE_DOMAIN (type) == NULL_TREE)
6718 {
6719 /* We have a flexible array member through a typedef.
6720 Set suitable range. Whether this is a correct position
6721 for a flexible array member will be determined elsewhere. */
6722 if (!in_system_header_at (input_location))
6723 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6724 "support flexible array members");
6725 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6726 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6727 NULL_TREE);
6728 }
6729 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6730 orig_qual_indirect);
6731 decl = build_decl (declarator->id_loc,
6732 FIELD_DECL, declarator->u.id, type);
6733 DECL_NONADDRESSABLE_P (decl) = bitfield;
6734 if (bitfield && !declarator->u.id)
6735 TREE_NO_WARNING (decl) = 1;
6736
6737 if (size_varies)
6738 C_DECL_VARIABLE_SIZE (decl) = 1;
6739 }
6740 else if (TREE_CODE (type) == FUNCTION_TYPE)
6741 {
6742 if (storage_class == csc_register || threadp)
6743 {
6744 error_at (loc, "invalid storage class for function %qE", name);
6745 }
6746 else if (current_scope != file_scope)
6747 {
6748 /* Function declaration not at file scope. Storage
6749 classes other than `extern' are not allowed, C99
6750 6.7.1p5, and `extern' makes no difference. However,
6751 GCC allows 'auto', perhaps with 'inline', to support
6752 nested functions. */
6753 if (storage_class == csc_auto)
6754 pedwarn (loc, OPT_Wpedantic,
6755 "invalid storage class for function %qE", name);
6756 else if (storage_class == csc_static)
6757 {
6758 error_at (loc, "invalid storage class for function %qE", name);
6759 if (funcdef_flag)
6760 storage_class = declspecs->storage_class = csc_none;
6761 else
6762 return 0;
6763 }
6764 }
6765
6766 decl = build_decl (declarator->id_loc,
6767 FUNCTION_DECL, declarator->u.id, type);
6768 decl = build_decl_attribute_variant (decl, decl_attr);
6769
6770 if (type_quals & TYPE_QUAL_ATOMIC)
6771 {
6772 error_at (loc,
6773 "%<_Atomic%>-qualified function type");
6774 type_quals &= ~TYPE_QUAL_ATOMIC;
6775 }
6776 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6777 pedwarn (loc, OPT_Wpedantic,
6778 "ISO C forbids qualified function types");
6779
6780 /* Every function declaration is an external reference
6781 (DECL_EXTERNAL) except for those which are not at file
6782 scope and are explicitly declared "auto". This is
6783 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6784 GCC to signify a forward declaration of a nested function. */
6785 if (storage_class == csc_auto && current_scope != file_scope)
6786 DECL_EXTERNAL (decl) = 0;
6787 /* In C99, a function which is declared 'inline' with 'extern'
6788 is not an external reference (which is confusing). It
6789 means that the later definition of the function must be output
6790 in this file, C99 6.7.4p6. In GNU C89, a function declared
6791 'extern inline' is an external reference. */
6792 else if (declspecs->inline_p && storage_class != csc_static)
6793 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6794 == flag_gnu89_inline);
6795 else
6796 DECL_EXTERNAL (decl) = !initialized;
6797
6798 /* Record absence of global scope for `static' or `auto'. */
6799 TREE_PUBLIC (decl)
6800 = !(storage_class == csc_static || storage_class == csc_auto);
6801
6802 /* For a function definition, record the argument information
6803 block where store_parm_decls will look for it. */
6804 if (funcdef_flag)
6805 current_function_arg_info = arg_info;
6806
6807 if (declspecs->default_int_p)
6808 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6809
6810 /* Record presence of `inline' and `_Noreturn', if it is
6811 reasonable. */
6812 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6813 {
6814 if (declspecs->inline_p)
6815 pedwarn (loc, 0, "cannot inline function %<main%>");
6816 if (declspecs->noreturn_p)
6817 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6818 }
6819 else
6820 {
6821 if (declspecs->inline_p)
6822 /* Record that the function is declared `inline'. */
6823 DECL_DECLARED_INLINE_P (decl) = 1;
6824 if (declspecs->noreturn_p)
6825 {
6826 if (flag_isoc99)
6827 pedwarn_c99 (loc, OPT_Wpedantic,
6828 "ISO C99 does not support %<_Noreturn%>");
6829 else
6830 pedwarn_c99 (loc, OPT_Wpedantic,
6831 "ISO C90 does not support %<_Noreturn%>");
6832 TREE_THIS_VOLATILE (decl) = 1;
6833 }
6834 }
6835 }
6836 else
6837 {
6838 /* It's a variable. */
6839 /* An uninitialized decl with `extern' is a reference. */
6840 int extern_ref = !initialized && storage_class == csc_extern;
6841
6842 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6843 orig_qual_indirect);
6844
6845 /* C99 6.2.2p7: It is invalid (compile-time undefined
6846 behavior) to create an 'extern' declaration for a
6847 variable if there is a global declaration that is
6848 'static' and the global declaration is not visible.
6849 (If the static declaration _is_ currently visible,
6850 the 'extern' declaration is taken to refer to that decl.) */
6851 if (extern_ref && current_scope != file_scope)
6852 {
6853 tree global_decl = identifier_global_value (declarator->u.id);
6854 tree visible_decl = lookup_name (declarator->u.id);
6855
6856 if (global_decl
6857 && global_decl != visible_decl
6858 && VAR_P (global_decl)
6859 && !TREE_PUBLIC (global_decl))
6860 error_at (loc, "variable previously declared %<static%> "
6861 "redeclared %<extern%>");
6862 }
6863
6864 decl = build_decl (declarator->id_loc,
6865 VAR_DECL, declarator->u.id, type);
6866 if (size_varies)
6867 C_DECL_VARIABLE_SIZE (decl) = 1;
6868
6869 if (declspecs->inline_p)
6870 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6871 if (declspecs->noreturn_p)
6872 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6873
6874 /* At file scope, an initialized extern declaration may follow
6875 a static declaration. In that case, DECL_EXTERNAL will be
6876 reset later in start_decl. */
6877 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6878
6879 /* At file scope, the presence of a `static' or `register' storage
6880 class specifier, or the absence of all storage class specifiers
6881 makes this declaration a definition (perhaps tentative). Also,
6882 the absence of `static' makes it public. */
6883 if (current_scope == file_scope)
6884 {
6885 TREE_PUBLIC (decl) = storage_class != csc_static;
6886 TREE_STATIC (decl) = !extern_ref;
6887 }
6888 /* Not at file scope, only `static' makes a static definition. */
6889 else
6890 {
6891 TREE_STATIC (decl) = (storage_class == csc_static);
6892 TREE_PUBLIC (decl) = extern_ref;
6893 }
6894
6895 if (threadp)
6896 set_decl_tls_model (decl, decl_default_tls_model (decl));
6897 }
6898
6899 if ((storage_class == csc_extern
6900 || (storage_class == csc_none
6901 && TREE_CODE (type) == FUNCTION_TYPE
6902 && !funcdef_flag))
6903 && variably_modified_type_p (type, NULL_TREE))
6904 {
6905 /* C99 6.7.5.2p2 */
6906 if (TREE_CODE (type) == FUNCTION_TYPE)
6907 error_at (loc, "non-nested function with variably modified type");
6908 else
6909 error_at (loc, "object with variably modified type must have "
6910 "no linkage");
6911 }
6912
6913 /* Record `register' declaration for warnings on &
6914 and in case doing stupid register allocation. */
6915
6916 if (storage_class == csc_register)
6917 {
6918 C_DECL_REGISTER (decl) = 1;
6919 DECL_REGISTER (decl) = 1;
6920 }
6921
6922 /* Record constancy and volatility. */
6923 c_apply_type_quals_to_decl (type_quals, decl);
6924
6925 /* Apply _Alignas specifiers. */
6926 if (alignas_align)
6927 {
6928 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6929 DECL_USER_ALIGN (decl) = 1;
6930 }
6931
6932 /* If a type has volatile components, it should be stored in memory.
6933 Otherwise, the fact that those components are volatile
6934 will be ignored, and would even crash the compiler.
6935 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6936 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6937 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
6938 || TREE_CODE (decl) == RESULT_DECL))
6939 {
6940 /* It is not an error for a structure with volatile fields to
6941 be declared register, but reset DECL_REGISTER since it
6942 cannot actually go in a register. */
6943 int was_reg = C_DECL_REGISTER (decl);
6944 C_DECL_REGISTER (decl) = 0;
6945 DECL_REGISTER (decl) = 0;
6946 c_mark_addressable (decl);
6947 C_DECL_REGISTER (decl) = was_reg;
6948 }
6949
6950 /* This is the earliest point at which we might know the assembler
6951 name of a variable. Thus, if it's known before this, die horribly. */
6952 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6953
6954 if (warn_cxx_compat
6955 && VAR_P (decl)
6956 && TREE_PUBLIC (decl)
6957 && TREE_STATIC (decl)
6958 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
6959 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6960 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6961 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6962 ("non-local variable %qD with anonymous type is "
6963 "questionable in C++"),
6964 decl);
6965
6966 return decl;
6967 }
6968 }
6969 \f
6970 /* Decode the parameter-list info for a function type or function definition.
6971 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6972 if there is an identifier list instead of a parameter decl list).
6973 These two functions are separate because when a function returns
6974 or receives functions then each is called multiple times but the order
6975 of calls is different. The last call to `grokparms' is always the one
6976 that contains the formal parameter names of a function definition.
6977
6978 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6979
6980 FUNCDEF_FLAG is true for a function definition, false for
6981 a mere declaration. A nonempty identifier-list gets an error message
6982 when FUNCDEF_FLAG is false. */
6983
6984 static tree
6985 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6986 {
6987 tree arg_types = arg_info->types;
6988
6989 if (funcdef_flag && arg_info->had_vla_unspec)
6990 {
6991 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6992 /* C99 6.7.5.2p4 */
6993 error ("%<[*]%> not allowed in other than function prototype scope");
6994 }
6995
6996 if (arg_types == 0 && !funcdef_flag
6997 && !in_system_header_at (input_location))
6998 warning (OPT_Wstrict_prototypes,
6999 "function declaration isn%'t a prototype");
7000
7001 if (arg_types == error_mark_node)
7002 return 0; /* don't set TYPE_ARG_TYPES in this case */
7003
7004 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7005 {
7006 if (!funcdef_flag)
7007 {
7008 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
7009 arg_info->parms = NULL_TREE;
7010 }
7011 else
7012 arg_info->parms = arg_info->types;
7013
7014 arg_info->types = 0;
7015 return 0;
7016 }
7017 else
7018 {
7019 tree parm, type, typelt;
7020 unsigned int parmno;
7021
7022 /* If there is a parameter of incomplete type in a definition,
7023 this is an error. In a declaration this is valid, and a
7024 struct or union type may be completed later, before any calls
7025 or definition of the function. In the case where the tag was
7026 first declared within the parameter list, a warning has
7027 already been given. If a parameter has void type, then
7028 however the function cannot be defined or called, so
7029 warn. */
7030
7031 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7032 parm;
7033 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7034 {
7035 type = TREE_VALUE (typelt);
7036 if (type == error_mark_node)
7037 continue;
7038
7039 if (!COMPLETE_TYPE_P (type))
7040 {
7041 if (funcdef_flag)
7042 {
7043 if (DECL_NAME (parm))
7044 error_at (input_location,
7045 "parameter %u (%q+D) has incomplete type",
7046 parmno, parm);
7047 else
7048 error_at (DECL_SOURCE_LOCATION (parm),
7049 "parameter %u has incomplete type",
7050 parmno);
7051
7052 TREE_VALUE (typelt) = error_mark_node;
7053 TREE_TYPE (parm) = error_mark_node;
7054 arg_types = NULL_TREE;
7055 }
7056 else if (VOID_TYPE_P (type))
7057 {
7058 if (DECL_NAME (parm))
7059 warning_at (input_location, 0,
7060 "parameter %u (%q+D) has void type",
7061 parmno, parm);
7062 else
7063 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7064 "parameter %u has void type",
7065 parmno);
7066 }
7067 }
7068
7069 if (DECL_NAME (parm) && TREE_USED (parm))
7070 warn_if_shadowing (parm);
7071 }
7072 return arg_types;
7073 }
7074 }
7075
7076 /* Allocate and initialize a c_arg_info structure from the parser's
7077 obstack. */
7078
7079 struct c_arg_info *
7080 build_arg_info (void)
7081 {
7082 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7083 ret->parms = NULL_TREE;
7084 ret->tags = NULL;
7085 ret->types = NULL_TREE;
7086 ret->others = NULL_TREE;
7087 ret->pending_sizes = NULL;
7088 ret->had_vla_unspec = 0;
7089 return ret;
7090 }
7091
7092 /* Take apart the current scope and return a c_arg_info structure with
7093 info on a parameter list just parsed.
7094
7095 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7096
7097 ELLIPSIS being true means the argument list ended in '...' so don't
7098 append a sentinel (void_list_node) to the end of the type-list.
7099
7100 EXPR is NULL or an expression that needs to be evaluated for the
7101 side effects of array size expressions in the parameters. */
7102
7103 struct c_arg_info *
7104 get_parm_info (bool ellipsis, tree expr)
7105 {
7106 struct c_binding *b = current_scope->bindings;
7107 struct c_arg_info *arg_info = build_arg_info ();
7108
7109 tree parms = 0;
7110 vec<c_arg_tag, va_gc> *tags = NULL;
7111 tree types = 0;
7112 tree others = 0;
7113
7114 bool gave_void_only_once_err = false;
7115
7116 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7117
7118 /* The bindings in this scope must not get put into a block.
7119 We will take care of deleting the binding nodes. */
7120 current_scope->bindings = 0;
7121
7122 /* This function is only called if there was *something* on the
7123 parameter list. */
7124 gcc_assert (b);
7125
7126 /* A parameter list consisting solely of 'void' indicates that the
7127 function takes no arguments. But if the 'void' is qualified
7128 (by 'const' or 'volatile'), or has a storage class specifier
7129 ('register'), then the behavior is undefined; issue an error.
7130 Typedefs for 'void' are OK (see DR#157). */
7131 if (b->prev == 0 /* one binding */
7132 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7133 && !DECL_NAME (b->decl) /* anonymous */
7134 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7135 {
7136 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7137 || C_DECL_REGISTER (b->decl))
7138 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7139
7140 /* There cannot be an ellipsis. */
7141 if (ellipsis)
7142 error_at (b->locus, "%<void%> must be the only parameter");
7143
7144 arg_info->types = void_list_node;
7145 return arg_info;
7146 }
7147
7148 if (!ellipsis)
7149 types = void_list_node;
7150
7151 /* Break up the bindings list into parms, tags, types, and others;
7152 apply sanity checks; purge the name-to-decl bindings. */
7153 while (b)
7154 {
7155 tree decl = b->decl;
7156 tree type = TREE_TYPE (decl);
7157 c_arg_tag tag;
7158 const char *keyword;
7159
7160 switch (TREE_CODE (decl))
7161 {
7162 case PARM_DECL:
7163 if (b->id)
7164 {
7165 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7166 I_SYMBOL_BINDING (b->id) = b->shadowed;
7167 }
7168
7169 /* Check for forward decls that never got their actual decl. */
7170 if (TREE_ASM_WRITTEN (decl))
7171 error_at (b->locus,
7172 "parameter %q+D has just a forward declaration", decl);
7173 /* Check for (..., void, ...) and issue an error. */
7174 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7175 {
7176 if (!gave_void_only_once_err)
7177 {
7178 error_at (b->locus, "%<void%> must be the only parameter");
7179 gave_void_only_once_err = true;
7180 }
7181 }
7182 else
7183 {
7184 /* Valid parameter, add it to the list. */
7185 DECL_CHAIN (decl) = parms;
7186 parms = decl;
7187
7188 /* Since there is a prototype, args are passed in their
7189 declared types. The back end may override this later. */
7190 DECL_ARG_TYPE (decl) = type;
7191 types = tree_cons (0, type, types);
7192 }
7193 break;
7194
7195 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7196 case UNION_TYPE: keyword = "union"; goto tag;
7197 case RECORD_TYPE: keyword = "struct"; goto tag;
7198 tag:
7199 /* Types may not have tag-names, in which case the type
7200 appears in the bindings list with b->id NULL. */
7201 if (b->id)
7202 {
7203 gcc_assert (I_TAG_BINDING (b->id) == b);
7204 I_TAG_BINDING (b->id) = b->shadowed;
7205 }
7206
7207 /* Warn about any struct, union or enum tags defined in a
7208 parameter list. The scope of such types is limited to
7209 the parameter list, which is rarely if ever desirable
7210 (it's impossible to call such a function with type-
7211 correct arguments). An anonymous union parm type is
7212 meaningful as a GNU extension, so don't warn for that. */
7213 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
7214 {
7215 if (b->id)
7216 /* The %s will be one of 'struct', 'union', or 'enum'. */
7217 warning_at (b->locus, 0,
7218 "%<%s %E%> declared inside parameter list"
7219 " will not be visible outside of this definition or"
7220 " declaration", keyword, b->id);
7221 else
7222 /* The %s will be one of 'struct', 'union', or 'enum'. */
7223 warning_at (b->locus, 0,
7224 "anonymous %s declared inside parameter list"
7225 " will not be visible outside of this definition or"
7226 " declaration", keyword);
7227 }
7228
7229 tag.id = b->id;
7230 tag.type = decl;
7231 vec_safe_push (tags, tag);
7232 break;
7233
7234 case FUNCTION_DECL:
7235 /* FUNCTION_DECLs appear when there is an implicit function
7236 declaration in the parameter list. */
7237 gcc_assert (b->nested || seen_error ());
7238 goto set_shadowed;
7239
7240 case CONST_DECL:
7241 case TYPE_DECL:
7242 /* CONST_DECLs appear here when we have an embedded enum,
7243 and TYPE_DECLs appear here when we have an embedded struct
7244 or union. No warnings for this - we already warned about the
7245 type itself. */
7246
7247 /* When we reinsert this decl in the function body, we need
7248 to reconstruct whether it was marked as nested. */
7249 gcc_assert (!b->nested);
7250 DECL_CHAIN (decl) = others;
7251 others = decl;
7252 /* fall through */
7253
7254 case ERROR_MARK:
7255 set_shadowed:
7256 /* error_mark_node appears here when we have an undeclared
7257 variable. Just throw it away. */
7258 if (b->id)
7259 {
7260 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7261 I_SYMBOL_BINDING (b->id) = b->shadowed;
7262 }
7263 break;
7264
7265 /* Other things that might be encountered. */
7266 case LABEL_DECL:
7267 case VAR_DECL:
7268 default:
7269 gcc_unreachable ();
7270 }
7271
7272 b = free_binding_and_advance (b);
7273 }
7274
7275 arg_info->parms = parms;
7276 arg_info->tags = tags;
7277 arg_info->types = types;
7278 arg_info->others = others;
7279 arg_info->pending_sizes = expr;
7280 return arg_info;
7281 }
7282 \f
7283 /* Get the struct, enum or union (CODE says which) with tag NAME.
7284 Define the tag as a forward-reference with location LOC if it is
7285 not defined. Return a c_typespec structure for the type
7286 specifier. */
7287
7288 struct c_typespec
7289 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7290 {
7291 struct c_typespec ret;
7292 tree ref;
7293 location_t refloc;
7294
7295 ret.expr = NULL_TREE;
7296 ret.expr_const_operands = true;
7297
7298 /* If a cross reference is requested, look up the type
7299 already defined for this tag and return it. */
7300
7301 ref = lookup_tag (code, name, false, &refloc);
7302 /* If this is the right type of tag, return what we found.
7303 (This reference will be shadowed by shadow_tag later if appropriate.)
7304 If this is the wrong type of tag, do not return it. If it was the
7305 wrong type in the same scope, we will have had an error
7306 message already; if in a different scope and declaring
7307 a name, pending_xref_error will give an error message; but if in a
7308 different scope and not declaring a name, this tag should
7309 shadow the previous declaration of a different type of tag, and
7310 this would not work properly if we return the reference found.
7311 (For example, with "struct foo" in an outer scope, "union foo;"
7312 must shadow that tag with a new one of union type.) */
7313 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7314 if (ref && TREE_CODE (ref) == code)
7315 {
7316 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7317 && loc != UNKNOWN_LOCATION
7318 && warn_cxx_compat)
7319 {
7320 switch (code)
7321 {
7322 case ENUMERAL_TYPE:
7323 warning_at (loc, OPT_Wc___compat,
7324 ("enum type defined in struct or union "
7325 "is not visible in C++"));
7326 inform (refloc, "enum type defined here");
7327 break;
7328 case RECORD_TYPE:
7329 warning_at (loc, OPT_Wc___compat,
7330 ("struct defined in struct or union "
7331 "is not visible in C++"));
7332 inform (refloc, "struct defined here");
7333 break;
7334 case UNION_TYPE:
7335 warning_at (loc, OPT_Wc___compat,
7336 ("union defined in struct or union "
7337 "is not visible in C++"));
7338 inform (refloc, "union defined here");
7339 break;
7340 default:
7341 gcc_unreachable();
7342 }
7343 }
7344
7345 ret.spec = ref;
7346 return ret;
7347 }
7348
7349 /* If no such tag is yet defined, create a forward-reference node
7350 and record it as the "definition".
7351 When a real declaration of this type is found,
7352 the forward-reference will be altered into a real type. */
7353
7354 ref = make_node (code);
7355 if (code == ENUMERAL_TYPE)
7356 {
7357 /* Give the type a default layout like unsigned int
7358 to avoid crashing if it does not get defined. */
7359 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7360 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7361 TYPE_USER_ALIGN (ref) = 0;
7362 TYPE_UNSIGNED (ref) = 1;
7363 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7364 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7365 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7366 }
7367
7368 pushtag (loc, name, ref);
7369
7370 ret.spec = ref;
7371 return ret;
7372 }
7373
7374 /* Get the struct, enum or union (CODE says which) with tag NAME.
7375 Define the tag as a forward-reference if it is not defined.
7376 Return a tree for the type. */
7377
7378 tree
7379 xref_tag (enum tree_code code, tree name)
7380 {
7381 return parser_xref_tag (input_location, code, name).spec;
7382 }
7383 \f
7384 /* Make sure that the tag NAME is defined *in the current scope*
7385 at least as a forward reference.
7386 LOC is the location of the struct's definition.
7387 CODE says which kind of tag NAME ought to be.
7388
7389 This stores the current value of the file static STRUCT_PARSE_INFO
7390 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7391 new c_struct_parse_info structure. The old value of
7392 STRUCT_PARSE_INFO is restored in finish_struct. */
7393
7394 tree
7395 start_struct (location_t loc, enum tree_code code, tree name,
7396 struct c_struct_parse_info **enclosing_struct_parse_info)
7397 {
7398 /* If there is already a tag defined at this scope
7399 (as a forward reference), just return it. */
7400
7401 tree ref = NULL_TREE;
7402 location_t refloc = UNKNOWN_LOCATION;
7403
7404 if (name != NULL_TREE)
7405 ref = lookup_tag (code, name, true, &refloc);
7406 if (ref && TREE_CODE (ref) == code)
7407 {
7408 if (TYPE_SIZE (ref))
7409 {
7410 if (code == UNION_TYPE)
7411 error_at (loc, "redefinition of %<union %E%>", name);
7412 else
7413 error_at (loc, "redefinition of %<struct %E%>", name);
7414 if (refloc != UNKNOWN_LOCATION)
7415 inform (refloc, "originally defined here");
7416 /* Don't create structures using a name already in use. */
7417 ref = NULL_TREE;
7418 }
7419 else if (C_TYPE_BEING_DEFINED (ref))
7420 {
7421 if (code == UNION_TYPE)
7422 error_at (loc, "nested redefinition of %<union %E%>", name);
7423 else
7424 error_at (loc, "nested redefinition of %<struct %E%>", name);
7425 /* Don't bother to report "originally defined here" for a
7426 nested redefinition; the original definition should be
7427 obvious. */
7428 /* Don't create structures that contain themselves. */
7429 ref = NULL_TREE;
7430 }
7431 }
7432
7433 /* Otherwise create a forward-reference just so the tag is in scope. */
7434
7435 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7436 {
7437 ref = make_node (code);
7438 pushtag (loc, name, ref);
7439 }
7440
7441 C_TYPE_BEING_DEFINED (ref) = 1;
7442 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7443 TYPE_PACKED (v) = flag_pack_struct;
7444
7445 *enclosing_struct_parse_info = struct_parse_info;
7446 struct_parse_info = XNEW (struct c_struct_parse_info);
7447 struct_parse_info->struct_types.create (0);
7448 struct_parse_info->fields.create (0);
7449 struct_parse_info->typedefs_seen.create (0);
7450
7451 /* FIXME: This will issue a warning for a use of a type defined
7452 within a statement expr used within sizeof, et. al. This is not
7453 terribly serious as C++ doesn't permit statement exprs within
7454 sizeof anyhow. */
7455 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7456 warning_at (loc, OPT_Wc___compat,
7457 "defining type in %qs expression is invalid in C++",
7458 (in_sizeof
7459 ? "sizeof"
7460 : (in_typeof ? "typeof" : "alignof")));
7461
7462 return ref;
7463 }
7464
7465 /* Process the specs, declarator and width (NULL if omitted)
7466 of a structure component, returning a FIELD_DECL node.
7467 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7468 DECL_ATTRS is as for grokdeclarator.
7469
7470 LOC is the location of the structure component.
7471
7472 This is done during the parsing of the struct declaration.
7473 The FIELD_DECL nodes are chained together and the lot of them
7474 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7475
7476 tree
7477 grokfield (location_t loc,
7478 struct c_declarator *declarator, struct c_declspecs *declspecs,
7479 tree width, tree *decl_attrs)
7480 {
7481 tree value;
7482
7483 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7484 && width == NULL_TREE)
7485 {
7486 /* This is an unnamed decl.
7487
7488 If we have something of the form "union { list } ;" then this
7489 is the anonymous union extension. Similarly for struct.
7490
7491 If this is something of the form "struct foo;", then
7492 If MS or Plan 9 extensions are enabled, this is handled as
7493 an anonymous struct.
7494 Otherwise this is a forward declaration of a structure tag.
7495
7496 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7497 If foo names a structure or union without a tag, then this
7498 is an anonymous struct (this is permitted by C11).
7499 If MS or Plan 9 extensions are enabled and foo names a
7500 structure, then again this is an anonymous struct.
7501 Otherwise this is an error.
7502
7503 Oh what a horrid tangled web we weave. I wonder if MS consciously
7504 took this from Plan 9 or if it was an accident of implementation
7505 that took root before someone noticed the bug... */
7506
7507 tree type = declspecs->type;
7508 bool type_ok = RECORD_OR_UNION_TYPE_P (type);
7509 bool ok = false;
7510
7511 if (type_ok
7512 && (flag_ms_extensions
7513 || flag_plan9_extensions
7514 || !declspecs->typedef_p))
7515 {
7516 if (flag_ms_extensions || flag_plan9_extensions)
7517 ok = true;
7518 else if (TYPE_NAME (type) == NULL)
7519 ok = true;
7520 else
7521 ok = false;
7522 }
7523 if (!ok)
7524 {
7525 pedwarn (loc, 0, "declaration does not declare anything");
7526 return NULL_TREE;
7527 }
7528 if (flag_isoc99)
7529 pedwarn_c99 (loc, OPT_Wpedantic,
7530 "ISO C99 doesn%'t support unnamed structs/unions");
7531 else
7532 pedwarn_c99 (loc, OPT_Wpedantic,
7533 "ISO C90 doesn%'t support unnamed structs/unions");
7534 }
7535
7536 value = grokdeclarator (declarator, declspecs, FIELD, false,
7537 width ? &width : NULL, decl_attrs, NULL, NULL,
7538 DEPRECATED_NORMAL);
7539
7540 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7541 DECL_INITIAL (value) = width;
7542
7543 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7544 {
7545 /* If we currently have a binding for this field, set the
7546 in_struct field in the binding, so that we warn about lookups
7547 which find it. */
7548 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7549 if (b != NULL)
7550 {
7551 /* If the in_struct field is not yet set, push it on a list
7552 to be cleared when this struct is finished. */
7553 if (!b->in_struct)
7554 {
7555 struct_parse_info->fields.safe_push (b);
7556 b->in_struct = 1;
7557 }
7558 }
7559 }
7560
7561 return value;
7562 }
7563 \f
7564 /* Subroutine of detect_field_duplicates: return whether X and Y,
7565 which are both fields in the same struct, have duplicate field
7566 names. */
7567
7568 static bool
7569 is_duplicate_field (tree x, tree y)
7570 {
7571 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7572 return true;
7573
7574 /* When using -fplan9-extensions, an anonymous field whose name is a
7575 typedef can duplicate a field name. */
7576 if (flag_plan9_extensions
7577 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7578 {
7579 tree xt, xn, yt, yn;
7580
7581 xt = TREE_TYPE (x);
7582 if (DECL_NAME (x) != NULL_TREE)
7583 xn = DECL_NAME (x);
7584 else if (RECORD_OR_UNION_TYPE_P (xt)
7585 && TYPE_NAME (xt) != NULL_TREE
7586 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7587 xn = DECL_NAME (TYPE_NAME (xt));
7588 else
7589 xn = NULL_TREE;
7590
7591 yt = TREE_TYPE (y);
7592 if (DECL_NAME (y) != NULL_TREE)
7593 yn = DECL_NAME (y);
7594 else if (RECORD_OR_UNION_TYPE_P (yt)
7595 && TYPE_NAME (yt) != NULL_TREE
7596 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7597 yn = DECL_NAME (TYPE_NAME (yt));
7598 else
7599 yn = NULL_TREE;
7600
7601 if (xn != NULL_TREE && xn == yn)
7602 return true;
7603 }
7604
7605 return false;
7606 }
7607
7608 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7609 to HTAB, giving errors for any duplicates. */
7610
7611 static void
7612 detect_field_duplicates_hash (tree fieldlist,
7613 hash_table<nofree_ptr_hash <tree_node> > *htab)
7614 {
7615 tree x, y;
7616 tree_node **slot;
7617
7618 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7619 if ((y = DECL_NAME (x)) != 0)
7620 {
7621 slot = htab->find_slot (y, INSERT);
7622 if (*slot)
7623 {
7624 error ("duplicate member %q+D", x);
7625 DECL_NAME (x) = NULL_TREE;
7626 }
7627 *slot = y;
7628 }
7629 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7630 {
7631 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7632
7633 /* When using -fplan9-extensions, an anonymous field whose
7634 name is a typedef can duplicate a field name. */
7635 if (flag_plan9_extensions
7636 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7637 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7638 {
7639 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7640 slot = htab->find_slot (xn, INSERT);
7641 if (*slot)
7642 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7643 *slot = xn;
7644 }
7645 }
7646 }
7647
7648 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7649 the list such that this does not present a problem later. */
7650
7651 static void
7652 detect_field_duplicates (tree fieldlist)
7653 {
7654 tree x, y;
7655 int timeout = 10;
7656
7657 /* If the struct is the list of instance variables of an Objective-C
7658 class, then we need to check all the instance variables of
7659 superclasses when checking for duplicates (since you can't have
7660 an instance variable in a subclass with the same name as an
7661 instance variable in a superclass). We pass on this job to the
7662 Objective-C compiler. objc_detect_field_duplicates() will return
7663 false if we are not checking the list of instance variables and
7664 the C frontend should proceed with the standard field duplicate
7665 checks. If we are checking the list of instance variables, the
7666 ObjC frontend will do the check, emit the errors if needed, and
7667 then return true. */
7668 if (c_dialect_objc ())
7669 if (objc_detect_field_duplicates (false))
7670 return;
7671
7672 /* First, see if there are more than "a few" fields.
7673 This is trivially true if there are zero or one fields. */
7674 if (!fieldlist || !DECL_CHAIN (fieldlist))
7675 return;
7676 x = fieldlist;
7677 do {
7678 timeout--;
7679 if (DECL_NAME (x) == NULL_TREE
7680 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7681 timeout = 0;
7682 x = DECL_CHAIN (x);
7683 } while (timeout > 0 && x);
7684
7685 /* If there were "few" fields and no anonymous structures or unions,
7686 avoid the overhead of allocating a hash table. Instead just do
7687 the nested traversal thing. */
7688 if (timeout > 0)
7689 {
7690 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7691 /* When using -fplan9-extensions, we can have duplicates
7692 between typedef names and fields. */
7693 if (DECL_NAME (x)
7694 || (flag_plan9_extensions
7695 && DECL_NAME (x) == NULL_TREE
7696 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
7697 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7698 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7699 {
7700 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7701 if (is_duplicate_field (y, x))
7702 {
7703 error ("duplicate member %q+D", x);
7704 DECL_NAME (x) = NULL_TREE;
7705 }
7706 }
7707 }
7708 else
7709 {
7710 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7711 detect_field_duplicates_hash (fieldlist, &htab);
7712 }
7713 }
7714
7715 /* Finish up struct info used by -Wc++-compat. */
7716
7717 static void
7718 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7719 location_t record_loc)
7720 {
7721 unsigned int ix;
7722 tree x;
7723 struct c_binding *b;
7724
7725 if (fieldlist == NULL_TREE)
7726 {
7727 if (code == RECORD_TYPE)
7728 warning_at (record_loc, OPT_Wc___compat,
7729 "empty struct has size 0 in C, size 1 in C++");
7730 else
7731 warning_at (record_loc, OPT_Wc___compat,
7732 "empty union has size 0 in C, size 1 in C++");
7733 }
7734
7735 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7736 the current struct. We do this now at the end of the struct
7737 because the flag is used to issue visibility warnings, and we
7738 only want to issue those warnings if the type is referenced
7739 outside of the struct declaration. */
7740 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7741 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7742
7743 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7744 typedefs used when declaring fields in this struct. If the name
7745 of any of the fields is also a typedef name then the struct would
7746 not parse in C++, because the C++ lookup rules say that the
7747 typedef name would be looked up in the context of the struct, and
7748 would thus be the field rather than the typedef. */
7749 if (!struct_parse_info->typedefs_seen.is_empty ()
7750 && fieldlist != NULL_TREE)
7751 {
7752 /* Use a hash_set<tree> using the name of the typedef. We can use
7753 a hash_set<tree> because identifiers are interned. */
7754 hash_set<tree> tset;
7755
7756 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7757 tset.add (DECL_NAME (x));
7758
7759 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7760 {
7761 if (DECL_NAME (x) != NULL_TREE
7762 && tset.contains (DECL_NAME (x)))
7763 {
7764 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7765 ("using %qD as both field and typedef name is "
7766 "invalid in C++"),
7767 x);
7768 /* FIXME: It would be nice to report the location where
7769 the typedef name is used. */
7770 }
7771 }
7772 }
7773
7774 /* For each field which has a binding and which was not defined in
7775 an enclosing struct, clear the in_struct field. */
7776 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7777 b->in_struct = 0;
7778 }
7779
7780 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7781 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7782 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7783 ATTRIBUTES are attributes to be applied to the structure.
7784
7785 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7786 the struct was started. */
7787
7788 tree
7789 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7790 struct c_struct_parse_info *enclosing_struct_parse_info)
7791 {
7792 tree x;
7793 bool toplevel = file_scope == current_scope;
7794 int saw_named_field;
7795
7796 /* If this type was previously laid out as a forward reference,
7797 make sure we lay it out again. */
7798
7799 TYPE_SIZE (t) = 0;
7800
7801 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7802
7803 if (pedantic)
7804 {
7805 for (x = fieldlist; x; x = DECL_CHAIN (x))
7806 {
7807 if (DECL_NAME (x) != 0)
7808 break;
7809 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7810 break;
7811 }
7812
7813 if (x == 0)
7814 {
7815 if (TREE_CODE (t) == UNION_TYPE)
7816 {
7817 if (fieldlist)
7818 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7819 else
7820 pedwarn (loc, OPT_Wpedantic, "union has no members");
7821 }
7822 else
7823 {
7824 if (fieldlist)
7825 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7826 else
7827 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7828 }
7829 }
7830 }
7831
7832 /* Install struct as DECL_CONTEXT of each field decl.
7833 Also process specified field sizes, found in the DECL_INITIAL,
7834 storing 0 there after the type has been changed to precision equal
7835 to its width, rather than the precision of the specified standard
7836 type. (Correct layout requires the original type to have been preserved
7837 until now.) */
7838
7839 saw_named_field = 0;
7840 for (x = fieldlist; x; x = DECL_CHAIN (x))
7841 {
7842 if (TREE_TYPE (x) == error_mark_node)
7843 continue;
7844
7845 DECL_CONTEXT (x) = t;
7846
7847 /* If any field is const, the structure type is pseudo-const. */
7848 if (TREE_READONLY (x))
7849 C_TYPE_FIELDS_READONLY (t) = 1;
7850 else
7851 {
7852 /* A field that is pseudo-const makes the structure likewise. */
7853 tree t1 = strip_array_types (TREE_TYPE (x));
7854 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
7855 C_TYPE_FIELDS_READONLY (t) = 1;
7856 }
7857
7858 /* Any field that is volatile means variables of this type must be
7859 treated in some ways as volatile. */
7860 if (TREE_THIS_VOLATILE (x))
7861 C_TYPE_FIELDS_VOLATILE (t) = 1;
7862
7863 /* Any field of nominal variable size implies structure is too. */
7864 if (C_DECL_VARIABLE_SIZE (x))
7865 C_TYPE_VARIABLE_SIZE (t) = 1;
7866
7867 if (DECL_INITIAL (x))
7868 {
7869 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
7870 DECL_SIZE (x) = bitsize_int (width);
7871 DECL_BIT_FIELD (x) = 1;
7872 SET_DECL_C_BIT_FIELD (x);
7873 }
7874
7875 if (TYPE_PACKED (t)
7876 && (DECL_BIT_FIELD (x)
7877 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7878 DECL_PACKED (x) = 1;
7879
7880 /* Detect flexible array member in an invalid context. */
7881 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7882 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7883 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7884 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7885 {
7886 if (TREE_CODE (t) == UNION_TYPE)
7887 {
7888 error_at (DECL_SOURCE_LOCATION (x),
7889 "flexible array member in union");
7890 TREE_TYPE (x) = error_mark_node;
7891 }
7892 else if (DECL_CHAIN (x) != NULL_TREE)
7893 {
7894 error_at (DECL_SOURCE_LOCATION (x),
7895 "flexible array member not at end of struct");
7896 TREE_TYPE (x) = error_mark_node;
7897 }
7898 else if (!saw_named_field)
7899 {
7900 error_at (DECL_SOURCE_LOCATION (x),
7901 "flexible array member in otherwise empty struct");
7902 TREE_TYPE (x) = error_mark_node;
7903 }
7904 }
7905
7906 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7907 && flexible_array_type_p (TREE_TYPE (x)))
7908 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
7909 "invalid use of structure with flexible array member");
7910
7911 if (DECL_NAME (x)
7912 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7913 saw_named_field = 1;
7914 }
7915
7916 detect_field_duplicates (fieldlist);
7917
7918 /* Now we have the nearly final fieldlist. Record it,
7919 then lay out the structure or union (including the fields). */
7920
7921 TYPE_FIELDS (t) = fieldlist;
7922
7923 maybe_apply_pragma_scalar_storage_order (t);
7924
7925 layout_type (t);
7926
7927 if (TYPE_SIZE_UNIT (t)
7928 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7929 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7930 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7931 error ("type %qT is too large", t);
7932
7933 /* Give bit-fields their proper types and rewrite the type of array fields
7934 with scalar component if the enclosing type has reverse storage order. */
7935 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
7936 {
7937 if (TREE_CODE (field) == FIELD_DECL
7938 && DECL_INITIAL (field)
7939 && TREE_TYPE (field) != error_mark_node)
7940 {
7941 unsigned HOST_WIDE_INT width
7942 = tree_to_uhwi (DECL_INITIAL (field));
7943 tree type = TREE_TYPE (field);
7944 if (width != TYPE_PRECISION (type))
7945 {
7946 TREE_TYPE (field)
7947 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7948 DECL_MODE (field) = TYPE_MODE (TREE_TYPE (field));
7949 }
7950 DECL_INITIAL (field) = 0;
7951 }
7952 else if (TYPE_REVERSE_STORAGE_ORDER (t)
7953 && TREE_CODE (field) == FIELD_DECL
7954 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
7955 {
7956 tree ftype = TREE_TYPE (field);
7957 tree ctype = strip_array_types (ftype);
7958 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
7959 {
7960 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
7961 tree *typep = &fmain_type;
7962 do {
7963 *typep = build_distinct_type_copy (*typep);
7964 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
7965 typep = &TREE_TYPE (*typep);
7966 } while (TREE_CODE (*typep) == ARRAY_TYPE);
7967 TREE_TYPE (field)
7968 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
7969 }
7970 }
7971 }
7972
7973 /* Now we have the truly final field list.
7974 Store it in this type and in the variants. */
7975
7976 TYPE_FIELDS (t) = fieldlist;
7977
7978 /* If there are lots of fields, sort so we can look through them fast.
7979 We arbitrarily consider 16 or more elts to be "a lot". */
7980
7981 {
7982 int len = 0;
7983
7984 for (x = fieldlist; x; x = DECL_CHAIN (x))
7985 {
7986 if (len > 15 || DECL_NAME (x) == NULL)
7987 break;
7988 len += 1;
7989 }
7990
7991 if (len > 15)
7992 {
7993 tree *field_array;
7994 struct lang_type *space;
7995 struct sorted_fields_type *space2;
7996
7997 len += list_length (x);
7998
7999 /* Use the same allocation policy here that make_node uses, to
8000 ensure that this lives as long as the rest of the struct decl.
8001 All decls in an inline function need to be saved. */
8002
8003 space = ggc_cleared_alloc<struct lang_type> ();
8004 space2 = (sorted_fields_type *) ggc_internal_alloc
8005 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8006
8007 len = 0;
8008 space->s = space2;
8009 field_array = &space2->elts[0];
8010 for (x = fieldlist; x; x = DECL_CHAIN (x))
8011 {
8012 field_array[len++] = x;
8013
8014 /* If there is anonymous struct or union, break out of the loop. */
8015 if (DECL_NAME (x) == NULL)
8016 break;
8017 }
8018 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8019 if (x == NULL)
8020 {
8021 TYPE_LANG_SPECIFIC (t) = space;
8022 TYPE_LANG_SPECIFIC (t)->s->len = len;
8023 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8024 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8025 }
8026 }
8027 }
8028
8029 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8030 in dwarf2out via rest_of_decl_compilation below and means
8031 something totally different. Since we will be clearing
8032 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8033 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8034 C_TYPE_INCOMPLETE_VARS should use some language specific
8035 node. */
8036 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8037 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8038 {
8039 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8040 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8041 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8042 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8043 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8044 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8045 }
8046
8047 /* If this was supposed to be a transparent union, but we can't
8048 make it one, warn and turn off the flag. */
8049 if (TREE_CODE (t) == UNION_TYPE
8050 && TYPE_TRANSPARENT_AGGR (t)
8051 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8052 {
8053 TYPE_TRANSPARENT_AGGR (t) = 0;
8054 warning_at (loc, 0, "union cannot be made transparent");
8055 }
8056
8057 /* If this structure or union completes the type of any previous
8058 variable declaration, lay it out and output its rtl. */
8059 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8060 {
8061 tree decl = TREE_VALUE (x);
8062 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8063 layout_array_type (TREE_TYPE (decl));
8064 if (TREE_CODE (decl) != TYPE_DECL)
8065 {
8066 layout_decl (decl, 0);
8067 if (c_dialect_objc ())
8068 objc_check_decl (decl);
8069 rest_of_decl_compilation (decl, toplevel, 0);
8070 }
8071 }
8072
8073 /* Update type location to the one of the definition, instead of e.g.
8074 a forward declaration. */
8075 if (TYPE_STUB_DECL (t))
8076 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8077
8078 /* Finish debugging output for this type. */
8079 rest_of_type_compilation (t, toplevel);
8080
8081 /* If we're inside a function proper, i.e. not file-scope and not still
8082 parsing parameters, then arrange for the size of a variable sized type
8083 to be bound now. */
8084 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8085 add_stmt (build_stmt (loc,
8086 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8087
8088 if (warn_cxx_compat)
8089 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8090
8091 struct_parse_info->struct_types.release ();
8092 struct_parse_info->fields.release ();
8093 struct_parse_info->typedefs_seen.release ();
8094 XDELETE (struct_parse_info);
8095
8096 struct_parse_info = enclosing_struct_parse_info;
8097
8098 /* If this struct is defined inside a struct, add it to
8099 struct_types. */
8100 if (warn_cxx_compat
8101 && struct_parse_info != NULL
8102 && !in_sizeof && !in_typeof && !in_alignof)
8103 struct_parse_info->struct_types.safe_push (t);
8104
8105 return t;
8106 }
8107
8108 /* Lay out the type T, and its element type, and so on. */
8109
8110 static void
8111 layout_array_type (tree t)
8112 {
8113 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8114 layout_array_type (TREE_TYPE (t));
8115 layout_type (t);
8116 }
8117 \f
8118 /* Begin compiling the definition of an enumeration type.
8119 NAME is its name (or null if anonymous).
8120 LOC is the enum's location.
8121 Returns the type object, as yet incomplete.
8122 Also records info about it so that build_enumerator
8123 may be used to declare the individual values as they are read. */
8124
8125 tree
8126 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8127 {
8128 tree enumtype = NULL_TREE;
8129 location_t enumloc = UNKNOWN_LOCATION;
8130
8131 /* If this is the real definition for a previous forward reference,
8132 fill in the contents in the same object that used to be the
8133 forward reference. */
8134
8135 if (name != NULL_TREE)
8136 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8137
8138 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8139 {
8140 enumtype = make_node (ENUMERAL_TYPE);
8141 pushtag (loc, name, enumtype);
8142 }
8143
8144 if (C_TYPE_BEING_DEFINED (enumtype))
8145 error_at (loc, "nested redefinition of %<enum %E%>", name);
8146
8147 C_TYPE_BEING_DEFINED (enumtype) = 1;
8148
8149 if (TYPE_VALUES (enumtype) != 0)
8150 {
8151 /* This enum is a named one that has been declared already. */
8152 error_at (loc, "redeclaration of %<enum %E%>", name);
8153 if (enumloc != UNKNOWN_LOCATION)
8154 inform (enumloc, "originally defined here");
8155
8156 /* Completely replace its old definition.
8157 The old enumerators remain defined, however. */
8158 TYPE_VALUES (enumtype) = 0;
8159 }
8160
8161 the_enum->enum_next_value = integer_zero_node;
8162 the_enum->enum_overflow = 0;
8163
8164 if (flag_short_enums)
8165 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8166 TYPE_PACKED (v) = 1;
8167
8168 /* FIXME: This will issue a warning for a use of a type defined
8169 within sizeof in a statement expr. This is not terribly serious
8170 as C++ doesn't permit statement exprs within sizeof anyhow. */
8171 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8172 warning_at (loc, OPT_Wc___compat,
8173 "defining type in %qs expression is invalid in C++",
8174 (in_sizeof
8175 ? "sizeof"
8176 : (in_typeof ? "typeof" : "alignof")));
8177
8178 return enumtype;
8179 }
8180
8181 /* After processing and defining all the values of an enumeration type,
8182 install their decls in the enumeration type and finish it off.
8183 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8184 and ATTRIBUTES are the specified attributes.
8185 Returns ENUMTYPE. */
8186
8187 tree
8188 finish_enum (tree enumtype, tree values, tree attributes)
8189 {
8190 tree pair, tem;
8191 tree minnode = 0, maxnode = 0;
8192 int precision;
8193 signop sign;
8194 bool toplevel = (file_scope == current_scope);
8195 struct lang_type *lt;
8196
8197 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8198
8199 /* Calculate the maximum value of any enumerator in this type. */
8200
8201 if (values == error_mark_node)
8202 minnode = maxnode = integer_zero_node;
8203 else
8204 {
8205 minnode = maxnode = TREE_VALUE (values);
8206 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8207 {
8208 tree value = TREE_VALUE (pair);
8209 if (tree_int_cst_lt (maxnode, value))
8210 maxnode = value;
8211 if (tree_int_cst_lt (value, minnode))
8212 minnode = value;
8213 }
8214 }
8215
8216 /* Construct the final type of this enumeration. It is the same
8217 as one of the integral types - the narrowest one that fits, except
8218 that normally we only go as narrow as int - and signed iff any of
8219 the values are negative. */
8220 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8221 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8222 tree_int_cst_min_precision (maxnode, sign));
8223
8224 /* If the precision of the type was specified with an attribute and it
8225 was too small, give an error. Otherwise, use it. */
8226 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8227 {
8228 if (precision > TYPE_PRECISION (enumtype))
8229 {
8230 TYPE_PRECISION (enumtype) = 0;
8231 error ("specified mode too small for enumeral values");
8232 }
8233 else
8234 precision = TYPE_PRECISION (enumtype);
8235 }
8236 else
8237 TYPE_PRECISION (enumtype) = 0;
8238
8239 if (TYPE_PACKED (enumtype)
8240 || precision > TYPE_PRECISION (integer_type_node)
8241 || TYPE_PRECISION (enumtype))
8242 {
8243 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8244 if (tem == NULL)
8245 {
8246 warning (0, "enumeration values exceed range of largest integer");
8247 tem = long_long_integer_type_node;
8248 }
8249 }
8250 else
8251 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8252
8253 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8254 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8255 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8256 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8257 TYPE_SIZE (enumtype) = 0;
8258 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8259
8260 layout_type (enumtype);
8261
8262 if (values != error_mark_node)
8263 {
8264 /* Change the type of the enumerators to be the enum type. We
8265 need to do this irrespective of the size of the enum, for
8266 proper type checking. Replace the DECL_INITIALs of the
8267 enumerators, and the value slots of the list, with copies
8268 that have the enum type; they cannot be modified in place
8269 because they may be shared (e.g. integer_zero_node) Finally,
8270 change the purpose slots to point to the names of the decls. */
8271 for (pair = values; pair; pair = TREE_CHAIN (pair))
8272 {
8273 tree enu = TREE_PURPOSE (pair);
8274 tree ini = DECL_INITIAL (enu);
8275
8276 TREE_TYPE (enu) = enumtype;
8277
8278 /* The ISO C Standard mandates enumerators to have type int,
8279 even though the underlying type of an enum type is
8280 unspecified. However, GCC allows enumerators of any
8281 integer type as an extensions. build_enumerator()
8282 converts any enumerators that fit in an int to type int,
8283 to avoid promotions to unsigned types when comparing
8284 integers with enumerators that fit in the int range.
8285 When -pedantic is given, build_enumerator() would have
8286 already warned about those that don't fit. Here we
8287 convert the rest to the enumerator type. */
8288 if (TREE_TYPE (ini) != integer_type_node)
8289 ini = convert (enumtype, ini);
8290
8291 DECL_INITIAL (enu) = ini;
8292 TREE_PURPOSE (pair) = DECL_NAME (enu);
8293 TREE_VALUE (pair) = ini;
8294 }
8295
8296 TYPE_VALUES (enumtype) = values;
8297 }
8298
8299 /* Record the min/max values so that we can warn about bit-field
8300 enumerations that are too small for the values. */
8301 lt = ggc_cleared_alloc<struct lang_type> ();
8302 lt->enum_min = minnode;
8303 lt->enum_max = maxnode;
8304 TYPE_LANG_SPECIFIC (enumtype) = lt;
8305
8306 /* Fix up all variant types of this enum type. */
8307 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8308 {
8309 if (tem == enumtype)
8310 continue;
8311 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8312 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8313 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8314 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8315 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8316 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8317 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8318 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8319 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8320 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8321 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8322 }
8323
8324 /* Finish debugging output for this type. */
8325 rest_of_type_compilation (enumtype, toplevel);
8326
8327 /* If this enum is defined inside a struct, add it to
8328 struct_types. */
8329 if (warn_cxx_compat
8330 && struct_parse_info != NULL
8331 && !in_sizeof && !in_typeof && !in_alignof)
8332 struct_parse_info->struct_types.safe_push (enumtype);
8333
8334 return enumtype;
8335 }
8336
8337 /* Build and install a CONST_DECL for one value of the
8338 current enumeration type (one that was begun with start_enum).
8339 DECL_LOC is the location of the enumerator.
8340 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8341 Return a tree-list containing the CONST_DECL and its value.
8342 Assignment of sequential values by default is handled here. */
8343
8344 tree
8345 build_enumerator (location_t decl_loc, location_t loc,
8346 struct c_enum_contents *the_enum, tree name, tree value)
8347 {
8348 tree decl, type;
8349
8350 /* Validate and default VALUE. */
8351
8352 if (value != 0)
8353 {
8354 /* Don't issue more errors for error_mark_node (i.e. an
8355 undeclared identifier) - just ignore the value expression. */
8356 if (value == error_mark_node)
8357 value = 0;
8358 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8359 {
8360 error_at (loc, "enumerator value for %qE is not an integer constant",
8361 name);
8362 value = 0;
8363 }
8364 else
8365 {
8366 if (TREE_CODE (value) != INTEGER_CST)
8367 {
8368 value = c_fully_fold (value, false, NULL);
8369 if (TREE_CODE (value) == INTEGER_CST)
8370 pedwarn (loc, OPT_Wpedantic,
8371 "enumerator value for %qE is not an integer "
8372 "constant expression", name);
8373 }
8374 if (TREE_CODE (value) != INTEGER_CST)
8375 {
8376 error ("enumerator value for %qE is not an integer constant",
8377 name);
8378 value = 0;
8379 }
8380 else
8381 {
8382 value = default_conversion (value);
8383 constant_expression_warning (value);
8384 }
8385 }
8386 }
8387
8388 /* Default based on previous value. */
8389 /* It should no longer be possible to have NON_LVALUE_EXPR
8390 in the default. */
8391 if (value == 0)
8392 {
8393 value = the_enum->enum_next_value;
8394 if (the_enum->enum_overflow)
8395 error_at (loc, "overflow in enumeration values");
8396 }
8397 /* Even though the underlying type of an enum is unspecified, the
8398 type of enumeration constants is explicitly defined as int
8399 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8400 an extension. */
8401 else if (!int_fits_type_p (value, integer_type_node))
8402 pedwarn (loc, OPT_Wpedantic,
8403 "ISO C restricts enumerator values to range of %<int%>");
8404
8405 /* The ISO C Standard mandates enumerators to have type int, even
8406 though the underlying type of an enum type is unspecified.
8407 However, GCC allows enumerators of any integer type as an
8408 extensions. Here we convert any enumerators that fit in an int
8409 to type int, to avoid promotions to unsigned types when comparing
8410 integers with enumerators that fit in the int range. When
8411 -pedantic is given, we would have already warned about those that
8412 don't fit. We have to do this here rather than in finish_enum
8413 because this value may be used to define more enumerators. */
8414 if (int_fits_type_p (value, integer_type_node))
8415 value = convert (integer_type_node, value);
8416
8417 /* Set basis for default for next value. */
8418 the_enum->enum_next_value
8419 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8420 PLUS_EXPR, value, integer_one_node, 0);
8421 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8422
8423 /* Now create a declaration for the enum value name. */
8424
8425 type = TREE_TYPE (value);
8426 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8427 TYPE_PRECISION (integer_type_node)),
8428 (TYPE_PRECISION (type)
8429 >= TYPE_PRECISION (integer_type_node)
8430 && TYPE_UNSIGNED (type)));
8431
8432 decl = build_decl (decl_loc, CONST_DECL, name, type);
8433 DECL_INITIAL (decl) = convert (type, value);
8434 pushdecl (decl);
8435
8436 return tree_cons (decl, value, NULL_TREE);
8437 }
8438
8439 \f
8440 /* Create the FUNCTION_DECL for a function definition.
8441 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8442 the declaration; they describe the function's name and the type it returns,
8443 but twisted together in a fashion that parallels the syntax of C.
8444
8445 This function creates a binding context for the function body
8446 as well as setting up the FUNCTION_DECL in current_function_decl.
8447
8448 Returns 1 on success. If the DECLARATOR is not suitable for a function
8449 (it defines a datum instead), we return 0, which tells
8450 yyparse to report a parse error. */
8451
8452 int
8453 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8454 tree attributes)
8455 {
8456 tree decl1, old_decl;
8457 tree restype, resdecl;
8458 location_t loc;
8459
8460 current_function_returns_value = 0; /* Assume, until we see it does. */
8461 current_function_returns_null = 0;
8462 current_function_returns_abnormally = 0;
8463 warn_about_return_type = 0;
8464 c_switch_stack = NULL;
8465
8466 /* Indicate no valid break/continue context by setting these variables
8467 to some non-null, non-label value. We'll notice and emit the proper
8468 error message in c_finish_bc_stmt. */
8469 c_break_label = c_cont_label = size_zero_node;
8470
8471 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8472 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8473 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8474
8475 /* If the declarator is not suitable for a function definition,
8476 cause a syntax error. */
8477 if (decl1 == 0
8478 || TREE_CODE (decl1) != FUNCTION_DECL)
8479 return 0;
8480
8481 loc = DECL_SOURCE_LOCATION (decl1);
8482
8483 c_decl_attributes (&decl1, attributes, 0);
8484
8485 if (DECL_DECLARED_INLINE_P (decl1)
8486 && DECL_UNINLINABLE (decl1)
8487 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8488 warning_at (loc, OPT_Wattributes,
8489 "inline function %qD given attribute noinline",
8490 decl1);
8491
8492 /* Handle gnu_inline attribute. */
8493 if (declspecs->inline_p
8494 && !flag_gnu89_inline
8495 && TREE_CODE (decl1) == FUNCTION_DECL
8496 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8497 || current_function_decl))
8498 {
8499 if (declspecs->storage_class != csc_static)
8500 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8501 }
8502
8503 announce_function (decl1);
8504
8505 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8506 {
8507 error_at (loc, "return type is an incomplete type");
8508 /* Make it return void instead. */
8509 TREE_TYPE (decl1)
8510 = build_function_type (void_type_node,
8511 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8512 }
8513
8514 if (warn_about_return_type)
8515 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8516 : (warn_return_type ? OPT_Wreturn_type
8517 : OPT_Wimplicit_int),
8518 "return type defaults to %<int%>");
8519
8520 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8521 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8522 DECL_INITIAL (decl1) = error_mark_node;
8523
8524 /* A nested function is not global. */
8525 if (current_function_decl != 0)
8526 TREE_PUBLIC (decl1) = 0;
8527
8528 /* If this definition isn't a prototype and we had a prototype declaration
8529 before, copy the arg type info from that prototype. */
8530 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8531 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8532 old_decl = 0;
8533 current_function_prototype_locus = UNKNOWN_LOCATION;
8534 current_function_prototype_built_in = false;
8535 current_function_prototype_arg_types = NULL_TREE;
8536 if (!prototype_p (TREE_TYPE (decl1)))
8537 {
8538 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8539 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8540 TREE_TYPE (TREE_TYPE (old_decl))))
8541 {
8542 if (stdarg_p (TREE_TYPE (old_decl)))
8543 {
8544 warning_at (loc, 0, "%q+D defined as variadic function "
8545 "without prototype", decl1);
8546 locate_old_decl (old_decl);
8547 }
8548 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8549 TREE_TYPE (decl1));
8550 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8551 current_function_prototype_built_in
8552 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8553 current_function_prototype_arg_types
8554 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8555 }
8556 if (TREE_PUBLIC (decl1))
8557 {
8558 /* If there is an external prototype declaration of this
8559 function, record its location but do not copy information
8560 to this decl. This may be an invisible declaration
8561 (built-in or in a scope which has finished) or simply
8562 have more refined argument types than any declaration
8563 found above. */
8564 struct c_binding *b;
8565 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8566 if (B_IN_SCOPE (b, external_scope))
8567 break;
8568 if (b)
8569 {
8570 tree ext_decl, ext_type;
8571 ext_decl = b->decl;
8572 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8573 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8574 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8575 TREE_TYPE (ext_type)))
8576 {
8577 current_function_prototype_locus
8578 = DECL_SOURCE_LOCATION (ext_decl);
8579 current_function_prototype_built_in
8580 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8581 current_function_prototype_arg_types
8582 = TYPE_ARG_TYPES (ext_type);
8583 }
8584 }
8585 }
8586 }
8587
8588 /* Optionally warn of old-fashioned def with no previous prototype. */
8589 if (warn_strict_prototypes
8590 && old_decl != error_mark_node
8591 && !prototype_p (TREE_TYPE (decl1))
8592 && C_DECL_ISNT_PROTOTYPE (old_decl))
8593 warning_at (loc, OPT_Wstrict_prototypes,
8594 "function declaration isn%'t a prototype");
8595 /* Optionally warn of any global def with no previous prototype. */
8596 else if (warn_missing_prototypes
8597 && old_decl != error_mark_node
8598 && TREE_PUBLIC (decl1)
8599 && !MAIN_NAME_P (DECL_NAME (decl1))
8600 && C_DECL_ISNT_PROTOTYPE (old_decl)
8601 && !DECL_DECLARED_INLINE_P (decl1))
8602 warning_at (loc, OPT_Wmissing_prototypes,
8603 "no previous prototype for %qD", decl1);
8604 /* Optionally warn of any def with no previous prototype
8605 if the function has already been used. */
8606 else if (warn_missing_prototypes
8607 && old_decl != 0
8608 && old_decl != error_mark_node
8609 && TREE_USED (old_decl)
8610 && !prototype_p (TREE_TYPE (old_decl)))
8611 warning_at (loc, OPT_Wmissing_prototypes,
8612 "%qD was used with no prototype before its definition", decl1);
8613 /* Optionally warn of any global def with no previous declaration. */
8614 else if (warn_missing_declarations
8615 && TREE_PUBLIC (decl1)
8616 && old_decl == 0
8617 && !MAIN_NAME_P (DECL_NAME (decl1))
8618 && !DECL_DECLARED_INLINE_P (decl1))
8619 warning_at (loc, OPT_Wmissing_declarations,
8620 "no previous declaration for %qD",
8621 decl1);
8622 /* Optionally warn of any def with no previous declaration
8623 if the function has already been used. */
8624 else if (warn_missing_declarations
8625 && old_decl != 0
8626 && old_decl != error_mark_node
8627 && TREE_USED (old_decl)
8628 && C_DECL_IMPLICIT (old_decl))
8629 warning_at (loc, OPT_Wmissing_declarations,
8630 "%qD was used with no declaration before its definition", decl1);
8631
8632 /* This function exists in static storage.
8633 (This does not mean `static' in the C sense!) */
8634 TREE_STATIC (decl1) = 1;
8635
8636 /* This is the earliest point at which we might know the assembler
8637 name of the function. Thus, if it's set before this, die horribly. */
8638 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8639
8640 /* If #pragma weak was used, mark the decl weak now. */
8641 if (current_scope == file_scope)
8642 maybe_apply_pragma_weak (decl1);
8643
8644 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8645 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8646 {
8647 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8648 != integer_type_node)
8649 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8650 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8651 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8652 decl1);
8653
8654 check_main_parameter_types (decl1);
8655
8656 if (!TREE_PUBLIC (decl1))
8657 pedwarn (loc, OPT_Wmain,
8658 "%qD is normally a non-static function", decl1);
8659 }
8660
8661 /* Record the decl so that the function name is defined.
8662 If we already have a decl for this name, and it is a FUNCTION_DECL,
8663 use the old decl. */
8664
8665 current_function_decl = pushdecl (decl1);
8666
8667 push_scope ();
8668 declare_parm_level ();
8669
8670 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8671 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8672 DECL_ARTIFICIAL (resdecl) = 1;
8673 DECL_IGNORED_P (resdecl) = 1;
8674 DECL_RESULT (current_function_decl) = resdecl;
8675
8676 start_fname_decls ();
8677
8678 return 1;
8679 }
8680 \f
8681 /* Subroutine of store_parm_decls which handles new-style function
8682 definitions (prototype format). The parms already have decls, so we
8683 need only record them as in effect and complain if any redundant
8684 old-style parm decls were written. */
8685 static void
8686 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8687 {
8688 tree decl;
8689 c_arg_tag *tag;
8690 unsigned ix;
8691
8692 if (current_scope->bindings)
8693 {
8694 error_at (DECL_SOURCE_LOCATION (fndecl),
8695 "old-style parameter declarations in prototyped "
8696 "function definition");
8697
8698 /* Get rid of the old-style declarations. */
8699 pop_scope ();
8700 push_scope ();
8701 }
8702 /* Don't issue this warning for nested functions, and don't issue this
8703 warning if we got here because ARG_INFO_TYPES was error_mark_node
8704 (this happens when a function definition has just an ellipsis in
8705 its parameter list). */
8706 else if (!in_system_header_at (input_location)
8707 && !current_function_scope
8708 && arg_info->types != error_mark_node)
8709 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8710 "traditional C rejects ISO C style function definitions");
8711
8712 /* Now make all the parameter declarations visible in the function body.
8713 We can bypass most of the grunt work of pushdecl. */
8714 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8715 {
8716 DECL_CONTEXT (decl) = current_function_decl;
8717 if (DECL_NAME (decl))
8718 {
8719 bind (DECL_NAME (decl), decl, current_scope,
8720 /*invisible=*/false, /*nested=*/false,
8721 UNKNOWN_LOCATION);
8722 if (!TREE_USED (decl))
8723 warn_if_shadowing (decl);
8724 }
8725 else
8726 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8727 }
8728
8729 /* Record the parameter list in the function declaration. */
8730 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8731
8732 /* Now make all the ancillary declarations visible, likewise. */
8733 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8734 {
8735 DECL_CONTEXT (decl) = current_function_decl;
8736 if (DECL_NAME (decl))
8737 bind (DECL_NAME (decl), decl, current_scope,
8738 /*invisible=*/false,
8739 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8740 UNKNOWN_LOCATION);
8741 }
8742
8743 /* And all the tag declarations. */
8744 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8745 if (tag->id)
8746 bind (tag->id, tag->type, current_scope,
8747 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8748 }
8749
8750 /* Subroutine of store_parm_decls which handles old-style function
8751 definitions (separate parameter list and declarations). */
8752
8753 static void
8754 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8755 {
8756 struct c_binding *b;
8757 tree parm, decl, last;
8758 tree parmids = arg_info->parms;
8759 hash_set<tree> seen_args;
8760
8761 if (!in_system_header_at (input_location))
8762 warning_at (DECL_SOURCE_LOCATION (fndecl),
8763 OPT_Wold_style_definition, "old-style function definition");
8764
8765 /* Match each formal parameter name with its declaration. Save each
8766 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8767 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8768 {
8769 if (TREE_VALUE (parm) == 0)
8770 {
8771 error_at (DECL_SOURCE_LOCATION (fndecl),
8772 "parameter name missing from parameter list");
8773 TREE_PURPOSE (parm) = 0;
8774 continue;
8775 }
8776
8777 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8778 if (b && B_IN_CURRENT_SCOPE (b))
8779 {
8780 decl = b->decl;
8781 /* Skip erroneous parameters. */
8782 if (decl == error_mark_node)
8783 continue;
8784 /* If we got something other than a PARM_DECL it is an error. */
8785 if (TREE_CODE (decl) != PARM_DECL)
8786 {
8787 error_at (DECL_SOURCE_LOCATION (decl),
8788 "%qD declared as a non-parameter", decl);
8789 continue;
8790 }
8791 /* If the declaration is already marked, we have a duplicate
8792 name. Complain and ignore the duplicate. */
8793 else if (seen_args.contains (decl))
8794 {
8795 error_at (DECL_SOURCE_LOCATION (decl),
8796 "multiple parameters named %qD", decl);
8797 TREE_PURPOSE (parm) = 0;
8798 continue;
8799 }
8800 /* If the declaration says "void", complain and turn it into
8801 an int. */
8802 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8803 {
8804 error_at (DECL_SOURCE_LOCATION (decl),
8805 "parameter %qD declared with void type", decl);
8806 TREE_TYPE (decl) = integer_type_node;
8807 DECL_ARG_TYPE (decl) = integer_type_node;
8808 layout_decl (decl, 0);
8809 }
8810 warn_if_shadowing (decl);
8811 }
8812 /* If no declaration found, default to int. */
8813 else
8814 {
8815 /* FIXME diagnostics: This should be the location of the argument,
8816 not the FNDECL. E.g., for an old-style declaration
8817
8818 int f10(v) { blah; }
8819
8820 We should use the location of the V, not the F10.
8821 Unfortunately, the V is an IDENTIFIER_NODE which has no
8822 location. In the future we need locations for c_arg_info
8823 entries.
8824
8825 See gcc.dg/Wshadow-3.c for an example of this problem. */
8826 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8827 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8828 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8829 pushdecl (decl);
8830 warn_if_shadowing (decl);
8831
8832 if (flag_isoc99)
8833 pedwarn (DECL_SOURCE_LOCATION (decl),
8834 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
8835 decl);
8836 else
8837 warning_at (DECL_SOURCE_LOCATION (decl),
8838 OPT_Wmissing_parameter_type,
8839 "type of %qD defaults to %<int%>", decl);
8840 }
8841
8842 TREE_PURPOSE (parm) = decl;
8843 seen_args.add (decl);
8844 }
8845
8846 /* Now examine the parms chain for incomplete declarations
8847 and declarations with no corresponding names. */
8848
8849 for (b = current_scope->bindings; b; b = b->prev)
8850 {
8851 parm = b->decl;
8852 if (TREE_CODE (parm) != PARM_DECL)
8853 continue;
8854
8855 if (TREE_TYPE (parm) != error_mark_node
8856 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8857 {
8858 error_at (DECL_SOURCE_LOCATION (parm),
8859 "parameter %qD has incomplete type", parm);
8860 TREE_TYPE (parm) = error_mark_node;
8861 }
8862
8863 if (!seen_args.contains (parm))
8864 {
8865 error_at (DECL_SOURCE_LOCATION (parm),
8866 "declaration for parameter %qD but no such parameter",
8867 parm);
8868
8869 /* Pretend the parameter was not missing.
8870 This gets us to a standard state and minimizes
8871 further error messages. */
8872 parmids = chainon (parmids, tree_cons (parm, 0, 0));
8873 }
8874 }
8875
8876 /* Chain the declarations together in the order of the list of
8877 names. Store that chain in the function decl, replacing the
8878 list of names. Update the current scope to match. */
8879 DECL_ARGUMENTS (fndecl) = 0;
8880
8881 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8882 if (TREE_PURPOSE (parm))
8883 break;
8884 if (parm && TREE_PURPOSE (parm))
8885 {
8886 last = TREE_PURPOSE (parm);
8887 DECL_ARGUMENTS (fndecl) = last;
8888
8889 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8890 if (TREE_PURPOSE (parm))
8891 {
8892 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8893 last = TREE_PURPOSE (parm);
8894 }
8895 DECL_CHAIN (last) = 0;
8896 }
8897
8898 /* If there was a previous prototype,
8899 set the DECL_ARG_TYPE of each argument according to
8900 the type previously specified, and report any mismatches. */
8901
8902 if (current_function_prototype_arg_types)
8903 {
8904 tree type;
8905 for (parm = DECL_ARGUMENTS (fndecl),
8906 type = current_function_prototype_arg_types;
8907 parm || (type && TREE_VALUE (type) != error_mark_node
8908 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8909 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8910 {
8911 if (parm == 0 || type == 0
8912 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8913 {
8914 if (current_function_prototype_built_in)
8915 warning_at (DECL_SOURCE_LOCATION (fndecl),
8916 0, "number of arguments doesn%'t match "
8917 "built-in prototype");
8918 else
8919 {
8920 /* FIXME diagnostics: This should be the location of
8921 FNDECL, but there is bug when a prototype is
8922 declared inside function context, but defined
8923 outside of it (e.g., gcc.dg/pr15698-2.c). In
8924 which case FNDECL gets the location of the
8925 prototype, not the definition. */
8926 error_at (input_location,
8927 "number of arguments doesn%'t match prototype");
8928
8929 error_at (current_function_prototype_locus,
8930 "prototype declaration");
8931 }
8932 break;
8933 }
8934 /* Type for passing arg must be consistent with that
8935 declared for the arg. ISO C says we take the unqualified
8936 type for parameters declared with qualified type. */
8937 if (TREE_TYPE (parm) != error_mark_node
8938 && TREE_TYPE (type) != error_mark_node
8939 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8940 != TYPE_ATOMIC (TREE_VALUE (type)))
8941 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8942 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
8943 {
8944 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8945 == TYPE_ATOMIC (TREE_VALUE (type)))
8946 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8947 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8948 {
8949 /* Adjust argument to match prototype. E.g. a previous
8950 `int foo(float);' prototype causes
8951 `int foo(x) float x; {...}' to be treated like
8952 `int foo(float x) {...}'. This is particularly
8953 useful for argument types like uid_t. */
8954 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8955
8956 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8957 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8958 && TYPE_PRECISION (TREE_TYPE (parm))
8959 < TYPE_PRECISION (integer_type_node))
8960 DECL_ARG_TYPE (parm)
8961 = c_type_promotes_to (TREE_TYPE (parm));
8962
8963 /* ??? Is it possible to get here with a
8964 built-in prototype or will it always have
8965 been diagnosed as conflicting with an
8966 old-style definition and discarded? */
8967 if (current_function_prototype_built_in)
8968 warning_at (DECL_SOURCE_LOCATION (parm),
8969 OPT_Wpedantic, "promoted argument %qD "
8970 "doesn%'t match built-in prototype", parm);
8971 else
8972 {
8973 pedwarn (DECL_SOURCE_LOCATION (parm),
8974 OPT_Wpedantic, "promoted argument %qD "
8975 "doesn%'t match prototype", parm);
8976 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
8977 "prototype declaration");
8978 }
8979 }
8980 else
8981 {
8982 if (current_function_prototype_built_in)
8983 warning_at (DECL_SOURCE_LOCATION (parm),
8984 0, "argument %qD doesn%'t match "
8985 "built-in prototype", parm);
8986 else
8987 {
8988 error_at (DECL_SOURCE_LOCATION (parm),
8989 "argument %qD doesn%'t match prototype", parm);
8990 error_at (current_function_prototype_locus,
8991 "prototype declaration");
8992 }
8993 }
8994 }
8995 }
8996 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8997 }
8998
8999 /* Otherwise, create a prototype that would match. */
9000
9001 else
9002 {
9003 tree actual = 0, last = 0, type;
9004
9005 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9006 {
9007 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9008 if (last)
9009 TREE_CHAIN (last) = type;
9010 else
9011 actual = type;
9012 last = type;
9013 }
9014 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9015 if (last)
9016 TREE_CHAIN (last) = type;
9017 else
9018 actual = type;
9019
9020 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9021 of the type of this function, but we need to avoid having this
9022 affect the types of other similarly-typed functions, so we must
9023 first force the generation of an identical (but separate) type
9024 node for the relevant function type. The new node we create
9025 will be a variant of the main variant of the original function
9026 type. */
9027
9028 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9029
9030 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9031 }
9032 }
9033
9034 /* Store parameter declarations passed in ARG_INFO into the current
9035 function declaration. */
9036
9037 void
9038 store_parm_decls_from (struct c_arg_info *arg_info)
9039 {
9040 current_function_arg_info = arg_info;
9041 store_parm_decls ();
9042 }
9043
9044 /* Called by walk_tree to look for and update context-less labels. */
9045
9046 static tree
9047 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9048 {
9049 if (TREE_CODE (*tp) == LABEL_EXPR
9050 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9051 {
9052 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9053 *walk_subtrees = 0;
9054 }
9055
9056 return NULL_TREE;
9057 }
9058
9059 /* Store the parameter declarations into the current function declaration.
9060 This is called after parsing the parameter declarations, before
9061 digesting the body of the function.
9062
9063 For an old-style definition, construct a prototype out of the old-style
9064 parameter declarations and inject it into the function's type. */
9065
9066 void
9067 store_parm_decls (void)
9068 {
9069 tree fndecl = current_function_decl;
9070 bool proto;
9071
9072 /* The argument information block for FNDECL. */
9073 struct c_arg_info *arg_info = current_function_arg_info;
9074 current_function_arg_info = 0;
9075
9076 /* True if this definition is written with a prototype. Note:
9077 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9078 list in a function definition as equivalent to (void) -- an
9079 empty argument list specifies the function has no parameters,
9080 but only (void) sets up a prototype for future calls. */
9081 proto = arg_info->types != 0;
9082
9083 if (proto)
9084 store_parm_decls_newstyle (fndecl, arg_info);
9085 else
9086 store_parm_decls_oldstyle (fndecl, arg_info);
9087
9088 /* The next call to push_scope will be a function body. */
9089
9090 next_is_function_body = true;
9091
9092 /* Write a record describing this function definition to the prototypes
9093 file (if requested). */
9094
9095 gen_aux_info_record (fndecl, 1, 0, proto);
9096
9097 /* Initialize the RTL code for the function. */
9098 allocate_struct_function (fndecl, false);
9099
9100 if (warn_unused_local_typedefs)
9101 cfun->language = ggc_cleared_alloc<language_function> ();
9102
9103 /* Begin the statement tree for this function. */
9104 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9105
9106 /* ??? Insert the contents of the pending sizes list into the function
9107 to be evaluated. The only reason left to have this is
9108 void foo(int n, int array[n++])
9109 because we throw away the array type in favor of a pointer type, and
9110 thus won't naturally see the SAVE_EXPR containing the increment. All
9111 other pending sizes would be handled by gimplify_parameters. */
9112 if (arg_info->pending_sizes)
9113 {
9114 /* In very special circumstances, e.g. for code like
9115 _Atomic int i = 5;
9116 void f (int a[i += 2]) {}
9117 we need to execute the atomic assignment on function entry.
9118 But in this case, it is not just a straight store, it has the
9119 op= form, which means that build_atomic_assign has generated
9120 gotos, labels, etc. Because at that time the function decl
9121 for F has not been created yet, those labels do not have any
9122 function context. But we have the fndecl now, so update the
9123 labels accordingly. gimplify_expr would crash otherwise. */
9124 walk_tree_without_duplicates (&arg_info->pending_sizes,
9125 set_labels_context_r, fndecl);
9126 add_stmt (arg_info->pending_sizes);
9127 }
9128 }
9129
9130 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9131 c_finish_omp_declare_simd for function prototypes. No diagnostics
9132 should be done. */
9133
9134 void
9135 temp_store_parm_decls (tree fndecl, tree parms)
9136 {
9137 push_scope ();
9138 for (tree p = parms; p; p = DECL_CHAIN (p))
9139 {
9140 DECL_CONTEXT (p) = fndecl;
9141 if (DECL_NAME (p))
9142 bind (DECL_NAME (p), p, current_scope,
9143 /*invisible=*/false, /*nested=*/false,
9144 UNKNOWN_LOCATION);
9145 }
9146 }
9147
9148 /* Undo what temp_store_parm_decls did. */
9149
9150 void
9151 temp_pop_parm_decls (void)
9152 {
9153 /* Clear all bindings in this temporary scope, so that
9154 pop_scope doesn't create a BLOCK. */
9155 struct c_binding *b = current_scope->bindings;
9156 current_scope->bindings = NULL;
9157 for (; b; b = free_binding_and_advance (b))
9158 {
9159 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9160 || b->decl == error_mark_node);
9161 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9162 I_SYMBOL_BINDING (b->id) = b->shadowed;
9163 if (b->shadowed && b->shadowed->u.type)
9164 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9165 }
9166 pop_scope ();
9167 }
9168 \f
9169
9170 /* Finish up a function declaration and compile that function
9171 all the way to assembler language output. Then free the storage
9172 for the function definition.
9173
9174 This is called after parsing the body of the function definition. */
9175
9176 void
9177 finish_function (void)
9178 {
9179 tree fndecl = current_function_decl;
9180
9181 if (c_dialect_objc ())
9182 objc_finish_function ();
9183
9184 if (TREE_CODE (fndecl) == FUNCTION_DECL
9185 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9186 {
9187 tree args = DECL_ARGUMENTS (fndecl);
9188 for (; args; args = DECL_CHAIN (args))
9189 {
9190 tree type = TREE_TYPE (args);
9191 if (INTEGRAL_TYPE_P (type)
9192 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9193 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9194 }
9195 }
9196
9197 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9198 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9199
9200 /* Must mark the RESULT_DECL as being in this function. */
9201
9202 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9203 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9204
9205 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9206 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9207 == integer_type_node && flag_isoc99)
9208 {
9209 /* Hack. We don't want the middle-end to warn that this return
9210 is unreachable, so we mark its location as special. Using
9211 UNKNOWN_LOCATION has the problem that it gets clobbered in
9212 annotate_one_with_locus. A cleaner solution might be to
9213 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9214 */
9215 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9216 }
9217
9218 /* Tie off the statement tree for this function. */
9219 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9220
9221 /* If the function has _Cilk_spawn in front of a function call inside it
9222 i.e. it is a spawning function, then add the appropriate Cilk plus
9223 functions inside. */
9224 if (fn_contains_cilk_spawn_p (cfun))
9225 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
9226
9227 finish_fname_decls ();
9228
9229 /* Complain if there's just no return statement. */
9230 if (warn_return_type
9231 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9232 && !current_function_returns_value && !current_function_returns_null
9233 /* Don't complain if we are no-return. */
9234 && !current_function_returns_abnormally
9235 /* Don't complain if we are declared noreturn. */
9236 && !TREE_THIS_VOLATILE (fndecl)
9237 /* Don't warn for main(). */
9238 && !MAIN_NAME_P (DECL_NAME (fndecl))
9239 /* Or if they didn't actually specify a return type. */
9240 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9241 /* Normally, with -Wreturn-type, flow will complain, but we might
9242 optimize out static functions. */
9243 && !TREE_PUBLIC (fndecl))
9244 {
9245 warning (OPT_Wreturn_type,
9246 "no return statement in function returning non-void");
9247 TREE_NO_WARNING (fndecl) = 1;
9248 }
9249
9250 /* Complain about parameters that are only set, but never otherwise used. */
9251 if (warn_unused_but_set_parameter)
9252 {
9253 tree decl;
9254
9255 for (decl = DECL_ARGUMENTS (fndecl);
9256 decl;
9257 decl = DECL_CHAIN (decl))
9258 if (TREE_USED (decl)
9259 && TREE_CODE (decl) == PARM_DECL
9260 && !DECL_READ_P (decl)
9261 && DECL_NAME (decl)
9262 && !DECL_ARTIFICIAL (decl)
9263 && !TREE_NO_WARNING (decl))
9264 warning_at (DECL_SOURCE_LOCATION (decl),
9265 OPT_Wunused_but_set_parameter,
9266 "parameter %qD set but not used", decl);
9267 }
9268
9269 /* Complain about locally defined typedefs that are not used in this
9270 function. */
9271 maybe_warn_unused_local_typedefs ();
9272
9273 /* Possibly warn about unused parameters. */
9274 if (warn_unused_parameter)
9275 do_warn_unused_parameter (fndecl);
9276
9277 /* Store the end of the function, so that we get good line number
9278 info for the epilogue. */
9279 cfun->function_end_locus = input_location;
9280
9281 /* Finalize the ELF visibility for the function. */
9282 c_determine_visibility (fndecl);
9283
9284 /* For GNU C extern inline functions disregard inline limits. */
9285 if (DECL_EXTERNAL (fndecl)
9286 && DECL_DECLARED_INLINE_P (fndecl))
9287 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9288
9289 /* Genericize before inlining. Delay genericizing nested functions
9290 until their parent function is genericized. Since finalizing
9291 requires GENERIC, delay that as well. */
9292
9293 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9294 && !undef_nested_function)
9295 {
9296 if (!decl_function_context (fndecl))
9297 {
9298 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9299 c_genericize (fndecl);
9300
9301 /* ??? Objc emits functions after finalizing the compilation unit.
9302 This should be cleaned up later and this conditional removed. */
9303 if (symtab->global_info_ready)
9304 {
9305 cgraph_node::add_new_function (fndecl, false);
9306 return;
9307 }
9308 cgraph_node::finalize_function (fndecl, false);
9309 }
9310 else
9311 {
9312 /* Register this function with cgraph just far enough to get it
9313 added to our parent's nested function list. Handy, since the
9314 C front end doesn't have such a list. */
9315 (void) cgraph_node::get_create (fndecl);
9316 }
9317 }
9318
9319 if (!decl_function_context (fndecl))
9320 undef_nested_function = false;
9321
9322 if (cfun->language != NULL)
9323 {
9324 ggc_free (cfun->language);
9325 cfun->language = NULL;
9326 }
9327
9328 /* We're leaving the context of this function, so zap cfun.
9329 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9330 tree_rest_of_compilation. */
9331 set_cfun (NULL);
9332 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9333 current_function_decl = NULL;
9334 }
9335 \f
9336 /* Check the declarations given in a for-loop for satisfying the C99
9337 constraints. If exactly one such decl is found, return it. LOC is
9338 the location of the opening parenthesis of the for loop. The last
9339 parameter allows you to control the "for loop initial declarations
9340 are only allowed in C99 mode". Normally, you should pass
9341 flag_isoc99 as that parameter. But in some cases (Objective-C
9342 foreach loop, for example) we want to run the checks in this
9343 function even if not in C99 mode, so we allow the caller to turn
9344 off the error about not being in C99 mode.
9345 */
9346
9347 tree
9348 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9349 {
9350 struct c_binding *b;
9351 tree one_decl = NULL_TREE;
9352 int n_decls = 0;
9353
9354 if (!turn_off_iso_c99_error)
9355 {
9356 static bool hint = true;
9357 /* If we get here, declarations have been used in a for loop without
9358 the C99 for loop scope. This doesn't make much sense, so don't
9359 allow it. */
9360 error_at (loc, "%<for%> loop initial declarations "
9361 "are only allowed in C99 or C11 mode");
9362 if (hint)
9363 {
9364 inform (loc,
9365 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9366 "to compile your code");
9367 hint = false;
9368 }
9369 return NULL_TREE;
9370 }
9371 /* C99 subclause 6.8.5 paragraph 3:
9372
9373 [#3] The declaration part of a for statement shall only
9374 declare identifiers for objects having storage class auto or
9375 register.
9376
9377 It isn't clear whether, in this sentence, "identifiers" binds to
9378 "shall only declare" or to "objects" - that is, whether all identifiers
9379 declared must be identifiers for objects, or whether the restriction
9380 only applies to those that are. (A question on this in comp.std.c
9381 in November 2000 received no answer.) We implement the strictest
9382 interpretation, to avoid creating an extension which later causes
9383 problems. */
9384
9385 for (b = current_scope->bindings; b; b = b->prev)
9386 {
9387 tree id = b->id;
9388 tree decl = b->decl;
9389
9390 if (!id)
9391 continue;
9392
9393 switch (TREE_CODE (decl))
9394 {
9395 case VAR_DECL:
9396 {
9397 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9398 if (TREE_STATIC (decl))
9399 error_at (decl_loc,
9400 "declaration of static variable %qD in %<for%> loop "
9401 "initial declaration", decl);
9402 else if (DECL_EXTERNAL (decl))
9403 error_at (decl_loc,
9404 "declaration of %<extern%> variable %qD in %<for%> loop "
9405 "initial declaration", decl);
9406 }
9407 break;
9408
9409 case RECORD_TYPE:
9410 error_at (loc,
9411 "%<struct %E%> declared in %<for%> loop initial "
9412 "declaration", id);
9413 break;
9414 case UNION_TYPE:
9415 error_at (loc,
9416 "%<union %E%> declared in %<for%> loop initial declaration",
9417 id);
9418 break;
9419 case ENUMERAL_TYPE:
9420 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9421 "initial declaration", id);
9422 break;
9423 default:
9424 error_at (loc, "declaration of non-variable "
9425 "%qD in %<for%> loop initial declaration", decl);
9426 }
9427
9428 n_decls++;
9429 one_decl = decl;
9430 }
9431
9432 return n_decls == 1 ? one_decl : NULL_TREE;
9433 }
9434 \f
9435 /* Save and reinitialize the variables
9436 used during compilation of a C function. */
9437
9438 void
9439 c_push_function_context (void)
9440 {
9441 struct language_function *p = cfun->language;
9442 /* cfun->language might have been already allocated by the use of
9443 -Wunused-local-typedefs. In that case, just re-use it. */
9444 if (p == NULL)
9445 cfun->language = p = ggc_cleared_alloc<language_function> ();
9446
9447 p->base.x_stmt_tree = c_stmt_tree;
9448 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9449 p->x_break_label = c_break_label;
9450 p->x_cont_label = c_cont_label;
9451 p->x_switch_stack = c_switch_stack;
9452 p->arg_info = current_function_arg_info;
9453 p->returns_value = current_function_returns_value;
9454 p->returns_null = current_function_returns_null;
9455 p->returns_abnormally = current_function_returns_abnormally;
9456 p->warn_about_return_type = warn_about_return_type;
9457
9458 push_function_context ();
9459 }
9460
9461 /* Restore the variables used during compilation of a C function. */
9462
9463 void
9464 c_pop_function_context (void)
9465 {
9466 struct language_function *p;
9467
9468 pop_function_context ();
9469 p = cfun->language;
9470
9471 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9472 used to store data throughout the life time of the current cfun,
9473 So don't deallocate it. */
9474 if (!warn_unused_local_typedefs)
9475 cfun->language = NULL;
9476
9477 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9478 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9479 {
9480 /* Stop pointing to the local nodes about to be freed. */
9481 /* But DECL_INITIAL must remain nonzero so we know this
9482 was an actual function definition. */
9483 DECL_INITIAL (current_function_decl) = error_mark_node;
9484 DECL_ARGUMENTS (current_function_decl) = 0;
9485 }
9486
9487 c_stmt_tree = p->base.x_stmt_tree;
9488 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9489 c_break_label = p->x_break_label;
9490 c_cont_label = p->x_cont_label;
9491 c_switch_stack = p->x_switch_stack;
9492 current_function_arg_info = p->arg_info;
9493 current_function_returns_value = p->returns_value;
9494 current_function_returns_null = p->returns_null;
9495 current_function_returns_abnormally = p->returns_abnormally;
9496 warn_about_return_type = p->warn_about_return_type;
9497 }
9498
9499 /* The functions below are required for functionality of doing
9500 function at once processing in the C front end. Currently these
9501 functions are not called from anywhere in the C front end, but as
9502 these changes continue, that will change. */
9503
9504 /* Returns the stmt_tree (if any) to which statements are currently
9505 being added. If there is no active statement-tree, NULL is
9506 returned. */
9507
9508 stmt_tree
9509 current_stmt_tree (void)
9510 {
9511 return &c_stmt_tree;
9512 }
9513
9514 /* Return the global value of T as a symbol. */
9515
9516 tree
9517 identifier_global_value (tree t)
9518 {
9519 struct c_binding *b;
9520
9521 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9522 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9523 return b->decl;
9524
9525 return 0;
9526 }
9527
9528 /* In C, the only C-linkage public declaration is at file scope. */
9529
9530 tree
9531 c_linkage_bindings (tree name)
9532 {
9533 return identifier_global_value (name);
9534 }
9535
9536 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9537 otherwise the name is found in ridpointers from RID_INDEX. */
9538
9539 void
9540 record_builtin_type (enum rid rid_index, const char *name, tree type)
9541 {
9542 tree id, decl;
9543 if (name == 0)
9544 id = ridpointers[(int) rid_index];
9545 else
9546 id = get_identifier (name);
9547 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9548 pushdecl (decl);
9549 if (debug_hooks->type_decl)
9550 debug_hooks->type_decl (decl, false);
9551 }
9552
9553 /* Build the void_list_node (void_type_node having been created). */
9554 tree
9555 build_void_list_node (void)
9556 {
9557 tree t = build_tree_list (NULL_TREE, void_type_node);
9558 return t;
9559 }
9560
9561 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9562
9563 struct c_parm *
9564 build_c_parm (struct c_declspecs *specs, tree attrs,
9565 struct c_declarator *declarator)
9566 {
9567 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9568 ret->specs = specs;
9569 ret->attrs = attrs;
9570 ret->declarator = declarator;
9571 return ret;
9572 }
9573
9574 /* Return a declarator with nested attributes. TARGET is the inner
9575 declarator to which these attributes apply. ATTRS are the
9576 attributes. */
9577
9578 struct c_declarator *
9579 build_attrs_declarator (tree attrs, struct c_declarator *target)
9580 {
9581 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9582 ret->kind = cdk_attrs;
9583 ret->declarator = target;
9584 ret->u.attrs = attrs;
9585 return ret;
9586 }
9587
9588 /* Return a declarator for a function with arguments specified by ARGS
9589 and return type specified by TARGET. */
9590
9591 struct c_declarator *
9592 build_function_declarator (struct c_arg_info *args,
9593 struct c_declarator *target)
9594 {
9595 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9596 ret->kind = cdk_function;
9597 ret->declarator = target;
9598 ret->u.arg_info = args;
9599 return ret;
9600 }
9601
9602 /* Return a declarator for the identifier IDENT (which may be
9603 NULL_TREE for an abstract declarator). */
9604
9605 struct c_declarator *
9606 build_id_declarator (tree ident)
9607 {
9608 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9609 ret->kind = cdk_id;
9610 ret->declarator = 0;
9611 ret->u.id = ident;
9612 /* Default value - may get reset to a more precise location. */
9613 ret->id_loc = input_location;
9614 return ret;
9615 }
9616
9617 /* Return something to represent absolute declarators containing a *.
9618 TARGET is the absolute declarator that the * contains.
9619 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9620 to apply to the pointer type. */
9621
9622 struct c_declarator *
9623 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9624 struct c_declarator *target)
9625 {
9626 tree attrs;
9627 int quals = 0;
9628 struct c_declarator *itarget = target;
9629 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9630 if (type_quals_attrs)
9631 {
9632 attrs = type_quals_attrs->attrs;
9633 quals = quals_from_declspecs (type_quals_attrs);
9634 if (attrs != NULL_TREE)
9635 itarget = build_attrs_declarator (attrs, target);
9636 }
9637 ret->kind = cdk_pointer;
9638 ret->declarator = itarget;
9639 ret->u.pointer_quals = quals;
9640 return ret;
9641 }
9642
9643 /* Return a pointer to a structure for an empty list of declaration
9644 specifiers. */
9645
9646 struct c_declspecs *
9647 build_null_declspecs (void)
9648 {
9649 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9650 memset (ret, 0, sizeof *ret);
9651 ret->align_log = -1;
9652 ret->typespec_word = cts_none;
9653 ret->storage_class = csc_none;
9654 ret->expr_const_operands = true;
9655 ret->typespec_kind = ctsk_none;
9656 ret->address_space = ADDR_SPACE_GENERIC;
9657 return ret;
9658 }
9659
9660 /* Add the address space ADDRSPACE to the declaration specifiers
9661 SPECS, returning SPECS. */
9662
9663 struct c_declspecs *
9664 declspecs_add_addrspace (source_location location,
9665 struct c_declspecs *specs, addr_space_t as)
9666 {
9667 specs->non_sc_seen_p = true;
9668 specs->declspecs_seen_p = true;
9669
9670 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9671 && specs->address_space != as)
9672 error ("incompatible address space qualifiers %qs and %qs",
9673 c_addr_space_name (as),
9674 c_addr_space_name (specs->address_space));
9675 else
9676 {
9677 specs->address_space = as;
9678 specs->locations[cdw_address_space] = location;
9679 }
9680 return specs;
9681 }
9682
9683 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9684 returning SPECS. */
9685
9686 struct c_declspecs *
9687 declspecs_add_qual (source_location loc,
9688 struct c_declspecs *specs, tree qual)
9689 {
9690 enum rid i;
9691 bool dupe = false;
9692 specs->non_sc_seen_p = true;
9693 specs->declspecs_seen_p = true;
9694 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9695 && C_IS_RESERVED_WORD (qual));
9696 i = C_RID_CODE (qual);
9697 location_t prev_loc = 0;
9698 switch (i)
9699 {
9700 case RID_CONST:
9701 dupe = specs->const_p;
9702 specs->const_p = true;
9703 prev_loc = specs->locations[cdw_const];
9704 specs->locations[cdw_const] = loc;
9705 break;
9706 case RID_VOLATILE:
9707 dupe = specs->volatile_p;
9708 specs->volatile_p = true;
9709 prev_loc = specs->locations[cdw_volatile];
9710 specs->locations[cdw_volatile] = loc;
9711 break;
9712 case RID_RESTRICT:
9713 dupe = specs->restrict_p;
9714 specs->restrict_p = true;
9715 prev_loc = specs->locations[cdw_restrict];
9716 specs->locations[cdw_restrict] = loc;
9717 break;
9718 case RID_ATOMIC:
9719 dupe = specs->atomic_p;
9720 specs->atomic_p = true;
9721 prev_loc = specs->locations[cdw_atomic];
9722 specs->locations[cdw_atomic] = loc;
9723 break;
9724 default:
9725 gcc_unreachable ();
9726 }
9727 if (dupe)
9728 {
9729 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
9730 "duplicate %qE declaration specifier", qual);
9731 if (!warned
9732 && warn_duplicate_decl_specifier
9733 && prev_loc >= RESERVED_LOCATION_COUNT
9734 && !from_macro_expansion_at (prev_loc)
9735 && !from_macro_expansion_at (loc))
9736 warning_at (loc, OPT_Wduplicate_decl_specifier,
9737 "duplicate %qE declaration specifier", qual);
9738 }
9739 return specs;
9740 }
9741
9742 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9743 returning SPECS. */
9744
9745 struct c_declspecs *
9746 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9747 struct c_typespec spec)
9748 {
9749 tree type = spec.spec;
9750 specs->non_sc_seen_p = true;
9751 specs->declspecs_seen_p = true;
9752 specs->typespec_kind = spec.kind;
9753 if (TREE_DEPRECATED (type))
9754 specs->deprecated_p = true;
9755
9756 /* Handle type specifier keywords. */
9757 if (TREE_CODE (type) == IDENTIFIER_NODE
9758 && C_IS_RESERVED_WORD (type)
9759 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
9760 {
9761 enum rid i = C_RID_CODE (type);
9762 if (specs->type)
9763 {
9764 error_at (loc, "two or more data types in declaration specifiers");
9765 return specs;
9766 }
9767 if ((int) i <= (int) RID_LAST_MODIFIER)
9768 {
9769 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9770 bool dupe = false;
9771 switch (i)
9772 {
9773 case RID_LONG:
9774 if (specs->long_long_p)
9775 {
9776 error_at (loc, "%<long long long%> is too long for GCC");
9777 break;
9778 }
9779 if (specs->long_p)
9780 {
9781 if (specs->typespec_word == cts_double)
9782 {
9783 error_at (loc,
9784 ("both %<long long%> and %<double%> in "
9785 "declaration specifiers"));
9786 break;
9787 }
9788 pedwarn_c90 (loc, OPT_Wlong_long,
9789 "ISO C90 does not support %<long long%>");
9790 specs->long_long_p = 1;
9791 specs->locations[cdw_long_long] = loc;
9792 break;
9793 }
9794 if (specs->short_p)
9795 error_at (loc,
9796 ("both %<long%> and %<short%> in "
9797 "declaration specifiers"));
9798 else if (specs->typespec_word == cts_auto_type)
9799 error_at (loc,
9800 ("both %<long%> and %<__auto_type%> in "
9801 "declaration specifiers"));
9802 else if (specs->typespec_word == cts_void)
9803 error_at (loc,
9804 ("both %<long%> and %<void%> in "
9805 "declaration specifiers"));
9806 else if (specs->typespec_word == cts_int_n)
9807 error_at (loc,
9808 ("both %<long%> and %<__int%d%> in "
9809 "declaration specifiers"),
9810 int_n_data[specs->int_n_idx].bitsize);
9811 else if (specs->typespec_word == cts_bool)
9812 error_at (loc,
9813 ("both %<long%> and %<_Bool%> in "
9814 "declaration specifiers"));
9815 else if (specs->typespec_word == cts_char)
9816 error_at (loc,
9817 ("both %<long%> and %<char%> in "
9818 "declaration specifiers"));
9819 else if (specs->typespec_word == cts_float)
9820 error_at (loc,
9821 ("both %<long%> and %<float%> in "
9822 "declaration specifiers"));
9823 else if (specs->typespec_word == cts_dfloat32)
9824 error_at (loc,
9825 ("both %<long%> and %<_Decimal32%> in "
9826 "declaration specifiers"));
9827 else if (specs->typespec_word == cts_dfloat64)
9828 error_at (loc,
9829 ("both %<long%> and %<_Decimal64%> in "
9830 "declaration specifiers"));
9831 else if (specs->typespec_word == cts_dfloat128)
9832 error_at (loc,
9833 ("both %<long%> and %<_Decimal128%> in "
9834 "declaration specifiers"));
9835 else
9836 {
9837 specs->long_p = true;
9838 specs->locations[cdw_long] = loc;
9839 }
9840 break;
9841 case RID_SHORT:
9842 dupe = specs->short_p;
9843 if (specs->long_p)
9844 error_at (loc,
9845 ("both %<long%> and %<short%> in "
9846 "declaration specifiers"));
9847 else if (specs->typespec_word == cts_auto_type)
9848 error_at (loc,
9849 ("both %<short%> and %<__auto_type%> in "
9850 "declaration specifiers"));
9851 else if (specs->typespec_word == cts_void)
9852 error_at (loc,
9853 ("both %<short%> and %<void%> in "
9854 "declaration specifiers"));
9855 else if (specs->typespec_word == cts_int_n)
9856 error_at (loc,
9857 ("both %<short%> and %<__int%d%> in "
9858 "declaration specifiers"),
9859 int_n_data[specs->int_n_idx].bitsize);
9860 else if (specs->typespec_word == cts_bool)
9861 error_at (loc,
9862 ("both %<short%> and %<_Bool%> in "
9863 "declaration specifiers"));
9864 else if (specs->typespec_word == cts_char)
9865 error_at (loc,
9866 ("both %<short%> and %<char%> in "
9867 "declaration specifiers"));
9868 else if (specs->typespec_word == cts_float)
9869 error_at (loc,
9870 ("both %<short%> and %<float%> in "
9871 "declaration specifiers"));
9872 else if (specs->typespec_word == cts_double)
9873 error_at (loc,
9874 ("both %<short%> and %<double%> in "
9875 "declaration specifiers"));
9876 else if (specs->typespec_word == cts_dfloat32)
9877 error_at (loc,
9878 ("both %<short%> and %<_Decimal32%> in "
9879 "declaration specifiers"));
9880 else if (specs->typespec_word == cts_dfloat64)
9881 error_at (loc,
9882 ("both %<short%> and %<_Decimal64%> in "
9883 "declaration specifiers"));
9884 else if (specs->typespec_word == cts_dfloat128)
9885 error_at (loc,
9886 ("both %<short%> and %<_Decimal128%> in "
9887 "declaration specifiers"));
9888 else
9889 {
9890 specs->short_p = true;
9891 specs->locations[cdw_short] = loc;
9892 }
9893 break;
9894 case RID_SIGNED:
9895 dupe = specs->signed_p;
9896 if (specs->unsigned_p)
9897 error_at (loc,
9898 ("both %<signed%> and %<unsigned%> in "
9899 "declaration specifiers"));
9900 else if (specs->typespec_word == cts_auto_type)
9901 error_at (loc,
9902 ("both %<signed%> and %<__auto_type%> in "
9903 "declaration specifiers"));
9904 else if (specs->typespec_word == cts_void)
9905 error_at (loc,
9906 ("both %<signed%> and %<void%> in "
9907 "declaration specifiers"));
9908 else if (specs->typespec_word == cts_bool)
9909 error_at (loc,
9910 ("both %<signed%> and %<_Bool%> in "
9911 "declaration specifiers"));
9912 else if (specs->typespec_word == cts_float)
9913 error_at (loc,
9914 ("both %<signed%> and %<float%> in "
9915 "declaration specifiers"));
9916 else if (specs->typespec_word == cts_double)
9917 error_at (loc,
9918 ("both %<signed%> and %<double%> in "
9919 "declaration specifiers"));
9920 else if (specs->typespec_word == cts_dfloat32)
9921 error_at (loc,
9922 ("both %<signed%> and %<_Decimal32%> in "
9923 "declaration specifiers"));
9924 else if (specs->typespec_word == cts_dfloat64)
9925 error_at (loc,
9926 ("both %<signed%> and %<_Decimal64%> in "
9927 "declaration specifiers"));
9928 else if (specs->typespec_word == cts_dfloat128)
9929 error_at (loc,
9930 ("both %<signed%> and %<_Decimal128%> in "
9931 "declaration specifiers"));
9932 else
9933 {
9934 specs->signed_p = true;
9935 specs->locations[cdw_signed] = loc;
9936 }
9937 break;
9938 case RID_UNSIGNED:
9939 dupe = specs->unsigned_p;
9940 if (specs->signed_p)
9941 error_at (loc,
9942 ("both %<signed%> and %<unsigned%> in "
9943 "declaration specifiers"));
9944 else if (specs->typespec_word == cts_auto_type)
9945 error_at (loc,
9946 ("both %<unsigned%> and %<__auto_type%> in "
9947 "declaration specifiers"));
9948 else if (specs->typespec_word == cts_void)
9949 error_at (loc,
9950 ("both %<unsigned%> and %<void%> in "
9951 "declaration specifiers"));
9952 else if (specs->typespec_word == cts_bool)
9953 error_at (loc,
9954 ("both %<unsigned%> and %<_Bool%> in "
9955 "declaration specifiers"));
9956 else if (specs->typespec_word == cts_float)
9957 error_at (loc,
9958 ("both %<unsigned%> and %<float%> in "
9959 "declaration specifiers"));
9960 else if (specs->typespec_word == cts_double)
9961 error_at (loc,
9962 ("both %<unsigned%> and %<double%> in "
9963 "declaration specifiers"));
9964 else if (specs->typespec_word == cts_dfloat32)
9965 error_at (loc,
9966 ("both %<unsigned%> and %<_Decimal32%> in "
9967 "declaration specifiers"));
9968 else if (specs->typespec_word == cts_dfloat64)
9969 error_at (loc,
9970 ("both %<unsigned%> and %<_Decimal64%> in "
9971 "declaration specifiers"));
9972 else if (specs->typespec_word == cts_dfloat128)
9973 error_at (loc,
9974 ("both %<unsigned%> and %<_Decimal128%> in "
9975 "declaration specifiers"));
9976 else
9977 {
9978 specs->unsigned_p = true;
9979 specs->locations[cdw_unsigned] = loc;
9980 }
9981 break;
9982 case RID_COMPLEX:
9983 dupe = specs->complex_p;
9984 if (!in_system_header_at (loc))
9985 pedwarn_c90 (loc, OPT_Wpedantic,
9986 "ISO C90 does not support complex types");
9987 if (specs->typespec_word == cts_auto_type)
9988 error_at (loc,
9989 ("both %<complex%> and %<__auto_type%> in "
9990 "declaration specifiers"));
9991 else if (specs->typespec_word == cts_void)
9992 error_at (loc,
9993 ("both %<complex%> and %<void%> in "
9994 "declaration specifiers"));
9995 else if (specs->typespec_word == cts_bool)
9996 error_at (loc,
9997 ("both %<complex%> and %<_Bool%> in "
9998 "declaration specifiers"));
9999 else if (specs->typespec_word == cts_dfloat32)
10000 error_at (loc,
10001 ("both %<complex%> and %<_Decimal32%> in "
10002 "declaration specifiers"));
10003 else if (specs->typespec_word == cts_dfloat64)
10004 error_at (loc,
10005 ("both %<complex%> and %<_Decimal64%> in "
10006 "declaration specifiers"));
10007 else if (specs->typespec_word == cts_dfloat128)
10008 error_at (loc,
10009 ("both %<complex%> and %<_Decimal128%> in "
10010 "declaration specifiers"));
10011 else if (specs->typespec_word == cts_fract)
10012 error_at (loc,
10013 ("both %<complex%> and %<_Fract%> in "
10014 "declaration specifiers"));
10015 else if (specs->typespec_word == cts_accum)
10016 error_at (loc,
10017 ("both %<complex%> and %<_Accum%> in "
10018 "declaration specifiers"));
10019 else if (specs->saturating_p)
10020 error_at (loc,
10021 ("both %<complex%> and %<_Sat%> in "
10022 "declaration specifiers"));
10023 else
10024 {
10025 specs->complex_p = true;
10026 specs->locations[cdw_complex] = loc;
10027 }
10028 break;
10029 case RID_SAT:
10030 dupe = specs->saturating_p;
10031 pedwarn (loc, OPT_Wpedantic,
10032 "ISO C does not support saturating types");
10033 if (specs->typespec_word == cts_int_n)
10034 {
10035 error_at (loc,
10036 ("both %<_Sat%> and %<__int%d%> in "
10037 "declaration specifiers"),
10038 int_n_data[specs->int_n_idx].bitsize);
10039 }
10040 else if (specs->typespec_word == cts_auto_type)
10041 error_at (loc,
10042 ("both %<_Sat%> and %<__auto_type%> in "
10043 "declaration specifiers"));
10044 else if (specs->typespec_word == cts_void)
10045 error_at (loc,
10046 ("both %<_Sat%> and %<void%> in "
10047 "declaration specifiers"));
10048 else if (specs->typespec_word == cts_bool)
10049 error_at (loc,
10050 ("both %<_Sat%> and %<_Bool%> in "
10051 "declaration specifiers"));
10052 else if (specs->typespec_word == cts_char)
10053 error_at (loc,
10054 ("both %<_Sat%> and %<char%> in "
10055 "declaration specifiers"));
10056 else if (specs->typespec_word == cts_int)
10057 error_at (loc,
10058 ("both %<_Sat%> and %<int%> in "
10059 "declaration specifiers"));
10060 else if (specs->typespec_word == cts_float)
10061 error_at (loc,
10062 ("both %<_Sat%> and %<float%> in "
10063 "declaration specifiers"));
10064 else if (specs->typespec_word == cts_double)
10065 error_at (loc,
10066 ("both %<_Sat%> and %<double%> in "
10067 "declaration specifiers"));
10068 else if (specs->typespec_word == cts_dfloat32)
10069 error_at (loc,
10070 ("both %<_Sat%> and %<_Decimal32%> in "
10071 "declaration specifiers"));
10072 else if (specs->typespec_word == cts_dfloat64)
10073 error_at (loc,
10074 ("both %<_Sat%> and %<_Decimal64%> in "
10075 "declaration specifiers"));
10076 else if (specs->typespec_word == cts_dfloat128)
10077 error_at (loc,
10078 ("both %<_Sat%> and %<_Decimal128%> in "
10079 "declaration specifiers"));
10080 else if (specs->complex_p)
10081 error_at (loc,
10082 ("both %<_Sat%> and %<complex%> in "
10083 "declaration specifiers"));
10084 else
10085 {
10086 specs->saturating_p = true;
10087 specs->locations[cdw_saturating] = loc;
10088 }
10089 break;
10090 default:
10091 gcc_unreachable ();
10092 }
10093
10094 if (dupe)
10095 error_at (loc, "duplicate %qE", type);
10096
10097 return specs;
10098 }
10099 else
10100 {
10101 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
10102 "__intN", "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10103 "__auto_type". */
10104 if (specs->typespec_word != cts_none)
10105 {
10106 error_at (loc,
10107 "two or more data types in declaration specifiers");
10108 return specs;
10109 }
10110 switch (i)
10111 {
10112 case RID_AUTO_TYPE:
10113 if (specs->long_p)
10114 error_at (loc,
10115 ("both %<long%> and %<__auto_type%> in "
10116 "declaration specifiers"));
10117 else if (specs->short_p)
10118 error_at (loc,
10119 ("both %<short%> and %<__auto_type%> in "
10120 "declaration specifiers"));
10121 else if (specs->signed_p)
10122 error_at (loc,
10123 ("both %<signed%> and %<__auto_type%> in "
10124 "declaration specifiers"));
10125 else if (specs->unsigned_p)
10126 error_at (loc,
10127 ("both %<unsigned%> and %<__auto_type%> in "
10128 "declaration specifiers"));
10129 else if (specs->complex_p)
10130 error_at (loc,
10131 ("both %<complex%> and %<__auto_type%> in "
10132 "declaration specifiers"));
10133 else if (specs->saturating_p)
10134 error_at (loc,
10135 ("both %<_Sat%> and %<__auto_type%> in "
10136 "declaration specifiers"));
10137 else
10138 {
10139 specs->typespec_word = cts_auto_type;
10140 specs->locations[cdw_typespec] = loc;
10141 }
10142 return specs;
10143 case RID_INT_N_0:
10144 case RID_INT_N_1:
10145 case RID_INT_N_2:
10146 case RID_INT_N_3:
10147 specs->int_n_idx = i - RID_INT_N_0;
10148 if (!in_system_header_at (input_location))
10149 pedwarn (loc, OPT_Wpedantic,
10150 "ISO C does not support %<__int%d%> types",
10151 int_n_data[specs->int_n_idx].bitsize);
10152
10153 if (specs->long_p)
10154 error_at (loc,
10155 ("both %<__int%d%> and %<long%> in "
10156 "declaration specifiers"),
10157 int_n_data[specs->int_n_idx].bitsize);
10158 else if (specs->saturating_p)
10159 error_at (loc,
10160 ("both %<_Sat%> and %<__int%d%> in "
10161 "declaration specifiers"),
10162 int_n_data[specs->int_n_idx].bitsize);
10163 else if (specs->short_p)
10164 error_at (loc,
10165 ("both %<__int%d%> and %<short%> in "
10166 "declaration specifiers"),
10167 int_n_data[specs->int_n_idx].bitsize);
10168 else if (! int_n_enabled_p [specs->int_n_idx])
10169 error_at (loc,
10170 "%<__int%d%> is not supported on this target",
10171 int_n_data[specs->int_n_idx].bitsize);
10172 else
10173 {
10174 specs->typespec_word = cts_int_n;
10175 specs->locations[cdw_typespec] = loc;
10176 }
10177 return specs;
10178 case RID_VOID:
10179 if (specs->long_p)
10180 error_at (loc,
10181 ("both %<long%> and %<void%> in "
10182 "declaration specifiers"));
10183 else if (specs->short_p)
10184 error_at (loc,
10185 ("both %<short%> and %<void%> in "
10186 "declaration specifiers"));
10187 else if (specs->signed_p)
10188 error_at (loc,
10189 ("both %<signed%> and %<void%> in "
10190 "declaration specifiers"));
10191 else if (specs->unsigned_p)
10192 error_at (loc,
10193 ("both %<unsigned%> and %<void%> in "
10194 "declaration specifiers"));
10195 else if (specs->complex_p)
10196 error_at (loc,
10197 ("both %<complex%> and %<void%> in "
10198 "declaration specifiers"));
10199 else if (specs->saturating_p)
10200 error_at (loc,
10201 ("both %<_Sat%> and %<void%> in "
10202 "declaration specifiers"));
10203 else
10204 {
10205 specs->typespec_word = cts_void;
10206 specs->locations[cdw_typespec] = loc;
10207 }
10208 return specs;
10209 case RID_BOOL:
10210 if (!in_system_header_at (loc))
10211 pedwarn_c90 (loc, OPT_Wpedantic,
10212 "ISO C90 does not support boolean types");
10213 if (specs->long_p)
10214 error_at (loc,
10215 ("both %<long%> and %<_Bool%> in "
10216 "declaration specifiers"));
10217 else if (specs->short_p)
10218 error_at (loc,
10219 ("both %<short%> and %<_Bool%> in "
10220 "declaration specifiers"));
10221 else if (specs->signed_p)
10222 error_at (loc,
10223 ("both %<signed%> and %<_Bool%> in "
10224 "declaration specifiers"));
10225 else if (specs->unsigned_p)
10226 error_at (loc,
10227 ("both %<unsigned%> and %<_Bool%> in "
10228 "declaration specifiers"));
10229 else if (specs->complex_p)
10230 error_at (loc,
10231 ("both %<complex%> and %<_Bool%> in "
10232 "declaration specifiers"));
10233 else if (specs->saturating_p)
10234 error_at (loc,
10235 ("both %<_Sat%> and %<_Bool%> in "
10236 "declaration specifiers"));
10237 else
10238 {
10239 specs->typespec_word = cts_bool;
10240 specs->locations[cdw_typespec] = loc;
10241 }
10242 return specs;
10243 case RID_CHAR:
10244 if (specs->long_p)
10245 error_at (loc,
10246 ("both %<long%> and %<char%> in "
10247 "declaration specifiers"));
10248 else if (specs->short_p)
10249 error_at (loc,
10250 ("both %<short%> and %<char%> in "
10251 "declaration specifiers"));
10252 else if (specs->saturating_p)
10253 error_at (loc,
10254 ("both %<_Sat%> and %<char%> in "
10255 "declaration specifiers"));
10256 else
10257 {
10258 specs->typespec_word = cts_char;
10259 specs->locations[cdw_typespec] = loc;
10260 }
10261 return specs;
10262 case RID_INT:
10263 if (specs->saturating_p)
10264 error_at (loc,
10265 ("both %<_Sat%> and %<int%> in "
10266 "declaration specifiers"));
10267 else
10268 {
10269 specs->typespec_word = cts_int;
10270 specs->locations[cdw_typespec] = loc;
10271 }
10272 return specs;
10273 case RID_FLOAT:
10274 if (specs->long_p)
10275 error_at (loc,
10276 ("both %<long%> and %<float%> in "
10277 "declaration specifiers"));
10278 else if (specs->short_p)
10279 error_at (loc,
10280 ("both %<short%> and %<float%> in "
10281 "declaration specifiers"));
10282 else if (specs->signed_p)
10283 error_at (loc,
10284 ("both %<signed%> and %<float%> in "
10285 "declaration specifiers"));
10286 else if (specs->unsigned_p)
10287 error_at (loc,
10288 ("both %<unsigned%> and %<float%> in "
10289 "declaration specifiers"));
10290 else if (specs->saturating_p)
10291 error_at (loc,
10292 ("both %<_Sat%> and %<float%> in "
10293 "declaration specifiers"));
10294 else
10295 {
10296 specs->typespec_word = cts_float;
10297 specs->locations[cdw_typespec] = loc;
10298 }
10299 return specs;
10300 case RID_DOUBLE:
10301 if (specs->long_long_p)
10302 error_at (loc,
10303 ("both %<long long%> and %<double%> in "
10304 "declaration specifiers"));
10305 else if (specs->short_p)
10306 error_at (loc,
10307 ("both %<short%> and %<double%> in "
10308 "declaration specifiers"));
10309 else if (specs->signed_p)
10310 error_at (loc,
10311 ("both %<signed%> and %<double%> in "
10312 "declaration specifiers"));
10313 else if (specs->unsigned_p)
10314 error_at (loc,
10315 ("both %<unsigned%> and %<double%> in "
10316 "declaration specifiers"));
10317 else if (specs->saturating_p)
10318 error_at (loc,
10319 ("both %<_Sat%> and %<double%> in "
10320 "declaration specifiers"));
10321 else
10322 {
10323 specs->typespec_word = cts_double;
10324 specs->locations[cdw_typespec] = loc;
10325 }
10326 return specs;
10327 case RID_DFLOAT32:
10328 case RID_DFLOAT64:
10329 case RID_DFLOAT128:
10330 {
10331 const char *str;
10332 if (i == RID_DFLOAT32)
10333 str = "_Decimal32";
10334 else if (i == RID_DFLOAT64)
10335 str = "_Decimal64";
10336 else
10337 str = "_Decimal128";
10338 if (specs->long_long_p)
10339 error_at (loc,
10340 ("both %<long long%> and %<%s%> in "
10341 "declaration specifiers"),
10342 str);
10343 if (specs->long_p)
10344 error_at (loc,
10345 ("both %<long%> and %<%s%> in "
10346 "declaration specifiers"),
10347 str);
10348 else if (specs->short_p)
10349 error_at (loc,
10350 ("both %<short%> and %<%s%> in "
10351 "declaration specifiers"),
10352 str);
10353 else if (specs->signed_p)
10354 error_at (loc,
10355 ("both %<signed%> and %<%s%> in "
10356 "declaration specifiers"),
10357 str);
10358 else if (specs->unsigned_p)
10359 error_at (loc,
10360 ("both %<unsigned%> and %<%s%> in "
10361 "declaration specifiers"),
10362 str);
10363 else if (specs->complex_p)
10364 error_at (loc,
10365 ("both %<complex%> and %<%s%> in "
10366 "declaration specifiers"),
10367 str);
10368 else if (specs->saturating_p)
10369 error_at (loc,
10370 ("both %<_Sat%> and %<%s%> in "
10371 "declaration specifiers"),
10372 str);
10373 else if (i == RID_DFLOAT32)
10374 specs->typespec_word = cts_dfloat32;
10375 else if (i == RID_DFLOAT64)
10376 specs->typespec_word = cts_dfloat64;
10377 else
10378 specs->typespec_word = cts_dfloat128;
10379 specs->locations[cdw_typespec] = loc;
10380 }
10381 if (!targetm.decimal_float_supported_p ())
10382 error_at (loc,
10383 ("decimal floating point not supported "
10384 "for this target"));
10385 pedwarn (loc, OPT_Wpedantic,
10386 "ISO C does not support decimal floating point");
10387 return specs;
10388 case RID_FRACT:
10389 case RID_ACCUM:
10390 {
10391 const char *str;
10392 if (i == RID_FRACT)
10393 str = "_Fract";
10394 else
10395 str = "_Accum";
10396 if (specs->complex_p)
10397 error_at (loc,
10398 ("both %<complex%> and %<%s%> in "
10399 "declaration specifiers"),
10400 str);
10401 else if (i == RID_FRACT)
10402 specs->typespec_word = cts_fract;
10403 else
10404 specs->typespec_word = cts_accum;
10405 specs->locations[cdw_typespec] = loc;
10406 }
10407 if (!targetm.fixed_point_supported_p ())
10408 error_at (loc,
10409 "fixed-point types not supported for this target");
10410 pedwarn (loc, OPT_Wpedantic,
10411 "ISO C does not support fixed-point types");
10412 return specs;
10413 default:
10414 /* ObjC reserved word "id", handled below. */
10415 break;
10416 }
10417 }
10418 }
10419
10420 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10421 form of ObjC type, cases such as "int" and "long" being handled
10422 above), a TYPE (struct, union, enum and typeof specifiers) or an
10423 ERROR_MARK. In none of these cases may there have previously
10424 been any type specifiers. */
10425 if (specs->type || specs->typespec_word != cts_none
10426 || specs->long_p || specs->short_p || specs->signed_p
10427 || specs->unsigned_p || specs->complex_p)
10428 error_at (loc, "two or more data types in declaration specifiers");
10429 else if (TREE_CODE (type) == TYPE_DECL)
10430 {
10431 if (TREE_TYPE (type) == error_mark_node)
10432 ; /* Allow the type to default to int to avoid cascading errors. */
10433 else
10434 {
10435 specs->type = TREE_TYPE (type);
10436 specs->decl_attr = DECL_ATTRIBUTES (type);
10437 specs->typedef_p = true;
10438 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10439 specs->locations[cdw_typedef] = loc;
10440
10441 /* If this typedef name is defined in a struct, then a C++
10442 lookup would return a different value. */
10443 if (warn_cxx_compat
10444 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10445 warning_at (loc, OPT_Wc___compat,
10446 "C++ lookup of %qD would return a field, not a type",
10447 type);
10448
10449 /* If we are parsing a struct, record that a struct field
10450 used a typedef. */
10451 if (warn_cxx_compat && struct_parse_info != NULL)
10452 struct_parse_info->typedefs_seen.safe_push (type);
10453 }
10454 }
10455 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10456 {
10457 tree t = lookup_name (type);
10458 if (!t || TREE_CODE (t) != TYPE_DECL)
10459 error_at (loc, "%qE fails to be a typedef or built in type", type);
10460 else if (TREE_TYPE (t) == error_mark_node)
10461 ;
10462 else
10463 {
10464 specs->type = TREE_TYPE (t);
10465 specs->locations[cdw_typespec] = loc;
10466 }
10467 }
10468 else
10469 {
10470 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10471 {
10472 specs->typedef_p = true;
10473 specs->locations[cdw_typedef] = loc;
10474 if (spec.expr)
10475 {
10476 if (specs->expr)
10477 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10478 specs->expr, spec.expr);
10479 else
10480 specs->expr = spec.expr;
10481 specs->expr_const_operands &= spec.expr_const_operands;
10482 }
10483 }
10484 specs->type = type;
10485 }
10486
10487 return specs;
10488 }
10489
10490 /* Add the storage class specifier or function specifier SCSPEC to the
10491 declaration specifiers SPECS, returning SPECS. */
10492
10493 struct c_declspecs *
10494 declspecs_add_scspec (source_location loc,
10495 struct c_declspecs *specs,
10496 tree scspec)
10497 {
10498 enum rid i;
10499 enum c_storage_class n = csc_none;
10500 bool dupe = false;
10501 specs->declspecs_seen_p = true;
10502 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10503 && C_IS_RESERVED_WORD (scspec));
10504 i = C_RID_CODE (scspec);
10505 if (specs->non_sc_seen_p)
10506 warning (OPT_Wold_style_declaration,
10507 "%qE is not at beginning of declaration", scspec);
10508 switch (i)
10509 {
10510 case RID_INLINE:
10511 /* C99 permits duplicate inline. Although of doubtful utility,
10512 it seems simplest to permit it in gnu89 mode as well, as
10513 there is also little utility in maintaining this as a
10514 difference between gnu89 and C99 inline. */
10515 dupe = false;
10516 specs->inline_p = true;
10517 specs->locations[cdw_inline] = loc;
10518 break;
10519 case RID_NORETURN:
10520 /* Duplicate _Noreturn is permitted. */
10521 dupe = false;
10522 specs->noreturn_p = true;
10523 specs->locations[cdw_noreturn] = loc;
10524 break;
10525 case RID_THREAD:
10526 dupe = specs->thread_p;
10527 if (specs->storage_class == csc_auto)
10528 error ("%qE used with %<auto%>", scspec);
10529 else if (specs->storage_class == csc_register)
10530 error ("%qE used with %<register%>", scspec);
10531 else if (specs->storage_class == csc_typedef)
10532 error ("%qE used with %<typedef%>", scspec);
10533 else
10534 {
10535 specs->thread_p = true;
10536 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10537 "__thread") == 0);
10538 /* A diagnostic is not required for the use of this
10539 identifier in the implementation namespace; only diagnose
10540 it for the C11 spelling because of existing code using
10541 the other spelling. */
10542 if (!specs->thread_gnu_p)
10543 {
10544 if (flag_isoc99)
10545 pedwarn_c99 (loc, OPT_Wpedantic,
10546 "ISO C99 does not support %qE", scspec);
10547 else
10548 pedwarn_c99 (loc, OPT_Wpedantic,
10549 "ISO C90 does not support %qE", scspec);
10550 }
10551 specs->locations[cdw_thread] = loc;
10552 }
10553 break;
10554 case RID_AUTO:
10555 n = csc_auto;
10556 break;
10557 case RID_EXTERN:
10558 n = csc_extern;
10559 /* Diagnose "__thread extern". */
10560 if (specs->thread_p && specs->thread_gnu_p)
10561 error ("%<__thread%> before %<extern%>");
10562 break;
10563 case RID_REGISTER:
10564 n = csc_register;
10565 break;
10566 case RID_STATIC:
10567 n = csc_static;
10568 /* Diagnose "__thread static". */
10569 if (specs->thread_p && specs->thread_gnu_p)
10570 error ("%<__thread%> before %<static%>");
10571 break;
10572 case RID_TYPEDEF:
10573 n = csc_typedef;
10574 break;
10575 default:
10576 gcc_unreachable ();
10577 }
10578 if (n != csc_none && n == specs->storage_class)
10579 dupe = true;
10580 if (dupe)
10581 {
10582 if (i == RID_THREAD)
10583 error ("duplicate %<_Thread_local%> or %<__thread%>");
10584 else
10585 error ("duplicate %qE", scspec);
10586 }
10587 if (n != csc_none)
10588 {
10589 if (specs->storage_class != csc_none && n != specs->storage_class)
10590 {
10591 error ("multiple storage classes in declaration specifiers");
10592 }
10593 else
10594 {
10595 specs->storage_class = n;
10596 specs->locations[cdw_storage_class] = loc;
10597 if (n != csc_extern && n != csc_static && specs->thread_p)
10598 {
10599 error ("%qs used with %qE",
10600 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10601 scspec);
10602 specs->thread_p = false;
10603 }
10604 }
10605 }
10606 return specs;
10607 }
10608
10609 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10610 returning SPECS. */
10611
10612 struct c_declspecs *
10613 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10614 {
10615 specs->attrs = chainon (attrs, specs->attrs);
10616 specs->locations[cdw_attributes] = loc;
10617 specs->declspecs_seen_p = true;
10618 return specs;
10619 }
10620
10621 /* Add an _Alignas specifier (expression ALIGN, or type whose
10622 alignment is ALIGN) to the declaration specifiers SPECS, returning
10623 SPECS. */
10624 struct c_declspecs *
10625 declspecs_add_alignas (source_location loc,
10626 struct c_declspecs *specs, tree align)
10627 {
10628 int align_log;
10629 specs->alignas_p = true;
10630 specs->locations[cdw_alignas] = loc;
10631 if (align == error_mark_node)
10632 return specs;
10633 align_log = check_user_alignment (align, true);
10634 if (align_log > specs->align_log)
10635 specs->align_log = align_log;
10636 return specs;
10637 }
10638
10639 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10640 specifiers with any other type specifier to determine the resulting
10641 type. This is where ISO C checks on complex types are made, since
10642 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10643 double". */
10644
10645 struct c_declspecs *
10646 finish_declspecs (struct c_declspecs *specs)
10647 {
10648 /* If a type was specified as a whole, we have no modifiers and are
10649 done. */
10650 if (specs->type != NULL_TREE)
10651 {
10652 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10653 && !specs->signed_p && !specs->unsigned_p
10654 && !specs->complex_p);
10655
10656 /* Set a dummy type. */
10657 if (TREE_CODE (specs->type) == ERROR_MARK)
10658 specs->type = integer_type_node;
10659 return specs;
10660 }
10661
10662 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10663 has been specified, treat it as "int" unless "_Complex" is
10664 present and there are no other specifiers. If we just have
10665 "_Complex", it is equivalent to "_Complex double", but e.g.
10666 "_Complex short" is equivalent to "_Complex short int". */
10667 if (specs->typespec_word == cts_none)
10668 {
10669 if (specs->saturating_p)
10670 {
10671 error_at (specs->locations[cdw_saturating],
10672 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10673 if (!targetm.fixed_point_supported_p ())
10674 error_at (specs->locations[cdw_saturating],
10675 "fixed-point types not supported for this target");
10676 specs->typespec_word = cts_fract;
10677 }
10678 else if (specs->long_p || specs->short_p
10679 || specs->signed_p || specs->unsigned_p)
10680 {
10681 specs->typespec_word = cts_int;
10682 }
10683 else if (specs->complex_p)
10684 {
10685 specs->typespec_word = cts_double;
10686 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10687 "ISO C does not support plain %<complex%> meaning "
10688 "%<double complex%>");
10689 }
10690 else
10691 {
10692 specs->typespec_word = cts_int;
10693 specs->default_int_p = true;
10694 /* We don't diagnose this here because grokdeclarator will
10695 give more specific diagnostics according to whether it is
10696 a function definition. */
10697 }
10698 }
10699
10700 /* If "signed" was specified, record this to distinguish "int" and
10701 "signed int" in the case of a bit-field with
10702 -funsigned-bitfields. */
10703 specs->explicit_signed_p = specs->signed_p;
10704
10705 /* Now compute the actual type. */
10706 switch (specs->typespec_word)
10707 {
10708 case cts_auto_type:
10709 gcc_assert (!specs->long_p && !specs->short_p
10710 && !specs->signed_p && !specs->unsigned_p
10711 && !specs->complex_p);
10712 /* Type to be filled in later. */
10713 break;
10714 case cts_void:
10715 gcc_assert (!specs->long_p && !specs->short_p
10716 && !specs->signed_p && !specs->unsigned_p
10717 && !specs->complex_p);
10718 specs->type = void_type_node;
10719 break;
10720 case cts_bool:
10721 gcc_assert (!specs->long_p && !specs->short_p
10722 && !specs->signed_p && !specs->unsigned_p
10723 && !specs->complex_p);
10724 specs->type = boolean_type_node;
10725 break;
10726 case cts_char:
10727 gcc_assert (!specs->long_p && !specs->short_p);
10728 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10729 if (specs->signed_p)
10730 specs->type = signed_char_type_node;
10731 else if (specs->unsigned_p)
10732 specs->type = unsigned_char_type_node;
10733 else
10734 specs->type = char_type_node;
10735 if (specs->complex_p)
10736 {
10737 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10738 "ISO C does not support complex integer types");
10739 specs->type = build_complex_type (specs->type);
10740 }
10741 break;
10742 case cts_int_n:
10743 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
10744 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10745 specs->type = (specs->unsigned_p
10746 ? int_n_trees[specs->int_n_idx].unsigned_type
10747 : int_n_trees[specs->int_n_idx].signed_type);
10748 if (specs->complex_p)
10749 {
10750 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10751 "ISO C does not support complex integer types");
10752 specs->type = build_complex_type (specs->type);
10753 }
10754 break;
10755 case cts_int:
10756 gcc_assert (!(specs->long_p && specs->short_p));
10757 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10758 if (specs->long_long_p)
10759 specs->type = (specs->unsigned_p
10760 ? long_long_unsigned_type_node
10761 : long_long_integer_type_node);
10762 else if (specs->long_p)
10763 specs->type = (specs->unsigned_p
10764 ? long_unsigned_type_node
10765 : long_integer_type_node);
10766 else if (specs->short_p)
10767 specs->type = (specs->unsigned_p
10768 ? short_unsigned_type_node
10769 : short_integer_type_node);
10770 else
10771 specs->type = (specs->unsigned_p
10772 ? unsigned_type_node
10773 : integer_type_node);
10774 if (specs->complex_p)
10775 {
10776 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10777 "ISO C does not support complex integer types");
10778 specs->type = build_complex_type (specs->type);
10779 }
10780 break;
10781 case cts_float:
10782 gcc_assert (!specs->long_p && !specs->short_p
10783 && !specs->signed_p && !specs->unsigned_p);
10784 specs->type = (specs->complex_p
10785 ? complex_float_type_node
10786 : float_type_node);
10787 break;
10788 case cts_double:
10789 gcc_assert (!specs->long_long_p && !specs->short_p
10790 && !specs->signed_p && !specs->unsigned_p);
10791 if (specs->long_p)
10792 {
10793 specs->type = (specs->complex_p
10794 ? complex_long_double_type_node
10795 : long_double_type_node);
10796 }
10797 else
10798 {
10799 specs->type = (specs->complex_p
10800 ? complex_double_type_node
10801 : double_type_node);
10802 }
10803 break;
10804 case cts_dfloat32:
10805 case cts_dfloat64:
10806 case cts_dfloat128:
10807 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10808 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
10809 if (specs->typespec_word == cts_dfloat32)
10810 specs->type = dfloat32_type_node;
10811 else if (specs->typespec_word == cts_dfloat64)
10812 specs->type = dfloat64_type_node;
10813 else
10814 specs->type = dfloat128_type_node;
10815 break;
10816 case cts_fract:
10817 gcc_assert (!specs->complex_p);
10818 if (!targetm.fixed_point_supported_p ())
10819 specs->type = integer_type_node;
10820 else if (specs->saturating_p)
10821 {
10822 if (specs->long_long_p)
10823 specs->type = specs->unsigned_p
10824 ? sat_unsigned_long_long_fract_type_node
10825 : sat_long_long_fract_type_node;
10826 else if (specs->long_p)
10827 specs->type = specs->unsigned_p
10828 ? sat_unsigned_long_fract_type_node
10829 : sat_long_fract_type_node;
10830 else if (specs->short_p)
10831 specs->type = specs->unsigned_p
10832 ? sat_unsigned_short_fract_type_node
10833 : sat_short_fract_type_node;
10834 else
10835 specs->type = specs->unsigned_p
10836 ? sat_unsigned_fract_type_node
10837 : sat_fract_type_node;
10838 }
10839 else
10840 {
10841 if (specs->long_long_p)
10842 specs->type = specs->unsigned_p
10843 ? unsigned_long_long_fract_type_node
10844 : long_long_fract_type_node;
10845 else if (specs->long_p)
10846 specs->type = specs->unsigned_p
10847 ? unsigned_long_fract_type_node
10848 : long_fract_type_node;
10849 else if (specs->short_p)
10850 specs->type = specs->unsigned_p
10851 ? unsigned_short_fract_type_node
10852 : short_fract_type_node;
10853 else
10854 specs->type = specs->unsigned_p
10855 ? unsigned_fract_type_node
10856 : fract_type_node;
10857 }
10858 break;
10859 case cts_accum:
10860 gcc_assert (!specs->complex_p);
10861 if (!targetm.fixed_point_supported_p ())
10862 specs->type = integer_type_node;
10863 else if (specs->saturating_p)
10864 {
10865 if (specs->long_long_p)
10866 specs->type = specs->unsigned_p
10867 ? sat_unsigned_long_long_accum_type_node
10868 : sat_long_long_accum_type_node;
10869 else if (specs->long_p)
10870 specs->type = specs->unsigned_p
10871 ? sat_unsigned_long_accum_type_node
10872 : sat_long_accum_type_node;
10873 else if (specs->short_p)
10874 specs->type = specs->unsigned_p
10875 ? sat_unsigned_short_accum_type_node
10876 : sat_short_accum_type_node;
10877 else
10878 specs->type = specs->unsigned_p
10879 ? sat_unsigned_accum_type_node
10880 : sat_accum_type_node;
10881 }
10882 else
10883 {
10884 if (specs->long_long_p)
10885 specs->type = specs->unsigned_p
10886 ? unsigned_long_long_accum_type_node
10887 : long_long_accum_type_node;
10888 else if (specs->long_p)
10889 specs->type = specs->unsigned_p
10890 ? unsigned_long_accum_type_node
10891 : long_accum_type_node;
10892 else if (specs->short_p)
10893 specs->type = specs->unsigned_p
10894 ? unsigned_short_accum_type_node
10895 : short_accum_type_node;
10896 else
10897 specs->type = specs->unsigned_p
10898 ? unsigned_accum_type_node
10899 : accum_type_node;
10900 }
10901 break;
10902 default:
10903 gcc_unreachable ();
10904 }
10905
10906 return specs;
10907 }
10908
10909 /* Perform final processing on one file scope's declarations (or the
10910 external scope's declarations), GLOBALS. */
10911
10912 static void
10913 c_write_global_declarations_1 (tree globals)
10914 {
10915 tree decl;
10916 bool reconsider;
10917
10918 /* Process the decls in the order they were written. */
10919 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10920 {
10921 /* Check for used but undefined static functions using the C
10922 standard's definition of "used", and set TREE_NO_WARNING so
10923 that check_global_declaration doesn't repeat the check. */
10924 if (TREE_CODE (decl) == FUNCTION_DECL
10925 && DECL_INITIAL (decl) == 0
10926 && DECL_EXTERNAL (decl)
10927 && !TREE_PUBLIC (decl))
10928 {
10929 if (C_DECL_USED (decl))
10930 {
10931 pedwarn (input_location, 0, "%q+F used but never defined", decl);
10932 TREE_NO_WARNING (decl) = 1;
10933 }
10934 /* For -Wunused-function warn about unused static prototypes. */
10935 else if (warn_unused_function
10936 && ! DECL_ARTIFICIAL (decl)
10937 && ! TREE_NO_WARNING (decl))
10938 {
10939 warning (OPT_Wunused_function,
10940 "%q+F declared %<static%> but never defined", decl);
10941 TREE_NO_WARNING (decl) = 1;
10942 }
10943 }
10944
10945 wrapup_global_declaration_1 (decl);
10946 }
10947
10948 do
10949 {
10950 reconsider = false;
10951 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10952 reconsider |= wrapup_global_declaration_2 (decl);
10953 }
10954 while (reconsider);
10955 }
10956
10957 /* Callback to collect a source_ref from a DECL. */
10958
10959 static void
10960 collect_source_ref_cb (tree decl)
10961 {
10962 if (!DECL_IS_BUILTIN (decl))
10963 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
10964 }
10965
10966 /* Preserve the external declarations scope across a garbage collect. */
10967 static GTY(()) tree ext_block;
10968
10969 /* Collect all references relevant to SOURCE_FILE. */
10970
10971 static void
10972 collect_all_refs (const char *source_file)
10973 {
10974 tree t;
10975 unsigned i;
10976
10977 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10978 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
10979
10980 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
10981 }
10982
10983 /* Iterate over all global declarations and call CALLBACK. */
10984
10985 static void
10986 for_each_global_decl (void (*callback) (tree decl))
10987 {
10988 tree t;
10989 tree decls;
10990 tree decl;
10991 unsigned i;
10992
10993 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10994 {
10995 decls = DECL_INITIAL (t);
10996 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
10997 callback (decl);
10998 }
10999
11000 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11001 callback (decl);
11002 }
11003
11004 /* Perform any final parser cleanups and generate initial debugging
11005 information. */
11006
11007 void
11008 c_parse_final_cleanups (void)
11009 {
11010 tree t;
11011 unsigned i;
11012
11013 /* We don't want to do this if generating a PCH. */
11014 if (pch_file)
11015 return;
11016
11017 timevar_stop (TV_PHASE_PARSING);
11018 timevar_start (TV_PHASE_DEFERRED);
11019
11020 /* Do the Objective-C stuff. This is where all the Objective-C
11021 module stuff gets generated (symtab, class/protocol/selector
11022 lists etc). */
11023 if (c_dialect_objc ())
11024 objc_write_global_declarations ();
11025
11026 /* Close the external scope. */
11027 ext_block = pop_scope ();
11028 external_scope = 0;
11029 gcc_assert (!current_scope);
11030
11031 /* Handle -fdump-ada-spec[-slim]. */
11032 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11033 {
11034 /* Build a table of files to generate specs for */
11035 if (flag_dump_ada_spec_slim)
11036 collect_source_ref (main_input_filename);
11037 else
11038 for_each_global_decl (collect_source_ref_cb);
11039
11040 dump_ada_specs (collect_all_refs, NULL);
11041 }
11042
11043 if (ext_block)
11044 {
11045 tree tmp = BLOCK_VARS (ext_block);
11046 int flags;
11047 FILE * stream = dump_begin (TDI_tu, &flags);
11048 if (stream && tmp)
11049 {
11050 dump_node (tmp, flags & ~TDF_SLIM, stream);
11051 dump_end (TDI_tu, stream);
11052 }
11053 }
11054
11055 /* Process all file scopes in this compilation, and the external_scope,
11056 through wrapup_global_declarations. */
11057 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11058 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11059 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11060
11061 timevar_stop (TV_PHASE_DEFERRED);
11062 timevar_start (TV_PHASE_PARSING);
11063
11064 ext_block = NULL;
11065 }
11066
11067 /* Register reserved keyword WORD as qualifier for address space AS. */
11068
11069 void
11070 c_register_addr_space (const char *word, addr_space_t as)
11071 {
11072 int rid = RID_FIRST_ADDR_SPACE + as;
11073 tree id;
11074
11075 /* Address space qualifiers are only supported
11076 in C with GNU extensions enabled. */
11077 if (c_dialect_objc () || flag_no_asm)
11078 return;
11079
11080 id = get_identifier (word);
11081 C_SET_RID_CODE (id, rid);
11082 C_IS_RESERVED_WORD (id) = 1;
11083 ridpointers [rid] = id;
11084 }
11085
11086 /* Return identifier to look up for omp declare reduction. */
11087
11088 tree
11089 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11090 {
11091 const char *p = NULL;
11092 switch (reduction_code)
11093 {
11094 case PLUS_EXPR: p = "+"; break;
11095 case MULT_EXPR: p = "*"; break;
11096 case MINUS_EXPR: p = "-"; break;
11097 case BIT_AND_EXPR: p = "&"; break;
11098 case BIT_XOR_EXPR: p = "^"; break;
11099 case BIT_IOR_EXPR: p = "|"; break;
11100 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11101 case TRUTH_ORIF_EXPR: p = "||"; break;
11102 case MIN_EXPR: p = "min"; break;
11103 case MAX_EXPR: p = "max"; break;
11104 default:
11105 break;
11106 }
11107
11108 if (p == NULL)
11109 {
11110 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11111 return error_mark_node;
11112 p = IDENTIFIER_POINTER (reduction_id);
11113 }
11114
11115 const char prefix[] = "omp declare reduction ";
11116 size_t lenp = sizeof (prefix);
11117 size_t len = strlen (p);
11118 char *name = XALLOCAVEC (char, lenp + len);
11119 memcpy (name, prefix, lenp - 1);
11120 memcpy (name + lenp - 1, p, len + 1);
11121 return get_identifier (name);
11122 }
11123
11124 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11125 VAR_DECL, bind it into the current scope and return it. */
11126
11127 tree
11128 c_omp_reduction_decl (tree reduction_id)
11129 {
11130 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11131 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11132 return b->decl;
11133
11134 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11135 reduction_id, integer_type_node);
11136 DECL_ARTIFICIAL (decl) = 1;
11137 DECL_EXTERNAL (decl) = 1;
11138 TREE_STATIC (decl) = 1;
11139 TREE_PUBLIC (decl) = 0;
11140 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11141 return decl;
11142 }
11143
11144 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11145
11146 tree
11147 c_omp_reduction_lookup (tree reduction_id, tree type)
11148 {
11149 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11150 while (b)
11151 {
11152 tree t;
11153 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11154 if (comptypes (TREE_PURPOSE (t), type))
11155 return TREE_VALUE (t);
11156 b = b->shadowed;
11157 }
11158 return error_mark_node;
11159 }
11160
11161 /* Helper function called via walk_tree, to diagnose invalid
11162 #pragma omp declare reduction combiners or initializers. */
11163
11164 tree
11165 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11166 {
11167 tree *vars = (tree *) data;
11168 if (SSA_VAR_P (*tp)
11169 && !DECL_ARTIFICIAL (*tp)
11170 && *tp != vars[0]
11171 && *tp != vars[1])
11172 {
11173 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11174 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11175 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11176 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11177 *tp);
11178 else
11179 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11180 "to variable %qD which is not %<omp_priv%> nor "
11181 "%<omp_orig%>",
11182 *tp);
11183 return *tp;
11184 }
11185 return NULL_TREE;
11186 }
11187
11188 #include "gt-c-c-decl.h"