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