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