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