(Synchronize with addition made to binutils sources):
[gcc.git] / gcc / c-decl.c
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
25
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "rtl.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "expr.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "debug.h"
49 #include "opts.h"
50 #include "timevar.h"
51 #include "c-common.h"
52 #include "c-pragma.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "gimple.h"
56 #include "tree-iterator.h"
57 #include "diagnostic.h"
58 #include "tree-dump.h"
59 #include "cgraph.h"
60 #include "hashtab.h"
61 #include "libfuncs.h"
62 #include "except.h"
63 #include "langhooks-def.h"
64 #include "pointer-set.h"
65 #include "gimple.h"
66 #include "plugin.h"
67
68 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
69 enum decl_context
70 { NORMAL, /* Ordinary declaration */
71 FUNCDEF, /* Function definition */
72 PARM, /* Declaration of parm before function body */
73 FIELD, /* Declaration inside struct or union */
74 TYPENAME}; /* Typename (inside cast or sizeof) */
75
76 /* States indicating how grokdeclarator() should handle declspecs marked
77 with __attribute__((deprecated)). An object declared as
78 __attribute__((deprecated)) suppresses warnings of uses of other
79 deprecated items. */
80
81 enum deprecated_states {
82 DEPRECATED_NORMAL,
83 DEPRECATED_SUPPRESS
84 };
85
86 \f
87 /* Nonzero if we have seen an invalid cross reference
88 to a struct, union, or enum, but not yet printed the message. */
89 tree pending_invalid_xref;
90
91 /* File and line to appear in the eventual error message. */
92 location_t pending_invalid_xref_location;
93
94 /* True means we've initialized exception handling. */
95 bool c_eh_initialized_p;
96
97 /* The file and line that the prototype came from if this is an
98 old-style definition; used for diagnostics in
99 store_parm_decls_oldstyle. */
100
101 static location_t current_function_prototype_locus;
102
103 /* Whether this prototype was built-in. */
104
105 static bool current_function_prototype_built_in;
106
107 /* The argument type information of this prototype. */
108
109 static tree current_function_prototype_arg_types;
110
111 /* The argument information structure for the function currently being
112 defined. */
113
114 static struct c_arg_info *current_function_arg_info;
115
116 /* The obstack on which parser and related data structures, which are
117 not live beyond their top-level declaration or definition, are
118 allocated. */
119 struct obstack parser_obstack;
120
121 /* The current statement tree. */
122
123 static GTY(()) struct stmt_tree_s c_stmt_tree;
124
125 /* State saving variables. */
126 tree c_break_label;
127 tree c_cont_label;
128
129 /* True if we are currently parsing the fields of a struct or
130 union. */
131
132 static bool in_struct;
133
134 /* A list of types defined in the current struct or union. */
135
136 static VEC(tree,heap) *struct_types;
137
138 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
139 included in this invocation. Note that the current translation
140 unit is not included in this list. */
141
142 static GTY(()) tree all_translation_units;
143
144 /* A list of decls to be made automatically visible in each file scope. */
145 static GTY(()) tree visible_builtins;
146
147 /* Set to 0 at beginning of a function definition, set to 1 if
148 a return statement that specifies a return value is seen. */
149
150 int current_function_returns_value;
151
152 /* Set to 0 at beginning of a function definition, set to 1 if
153 a return statement with no argument is seen. */
154
155 int current_function_returns_null;
156
157 /* Set to 0 at beginning of a function definition, set to 1 if
158 a call to a noreturn function is seen. */
159
160 int current_function_returns_abnormally;
161
162 /* Set to nonzero by `grokdeclarator' for a function
163 whose return type is defaulted, if warnings for this are desired. */
164
165 static int warn_about_return_type;
166
167 /* Nonzero when the current toplevel function contains a declaration
168 of a nested function which is never defined. */
169
170 static bool undef_nested_function;
171
172 /* True means global_bindings_p should return false even if the scope stack
173 says we are in file scope. */
174 bool c_override_global_bindings_to_false;
175
176 \f
177 /* Each c_binding structure describes one binding of an identifier to
178 a decl. All the decls in a scope - irrespective of namespace - are
179 chained together by the ->prev field, which (as the name implies)
180 runs in reverse order. All the decls in a given namespace bound to
181 a given identifier are chained by the ->shadowed field, which runs
182 from inner to outer scopes.
183
184 The ->decl field usually points to a DECL node, but there are two
185 exceptions. In the namespace of type tags, the bound entity is a
186 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
187 identifier is encountered, it is bound to error_mark_node to
188 suppress further errors about that identifier in the current
189 function.
190
191 The ->type field stores the type of the declaration in this scope;
192 if NULL, the type is the type of the ->decl field. This is only of
193 relevance for objects with external or internal linkage which may
194 be redeclared in inner scopes, forming composite types that only
195 persist for the duration of those scopes. In the external scope,
196 this stores the composite of all the types declared for this
197 object, visible or not. The ->inner_comp field (used only at file
198 scope) stores whether an incomplete array type at file scope was
199 completed at an inner scope to an array size other than 1.
200
201 The depth field is copied from the scope structure that holds this
202 decl. It is used to preserve the proper ordering of the ->shadowed
203 field (see bind()) and also for a handful of special-case checks.
204 Finally, the invisible bit is true for a decl which should be
205 ignored for purposes of normal name lookup, and the nested bit is
206 true for a decl that's been bound a second time in an inner scope;
207 in all such cases, the binding in the outer scope will have its
208 invisible bit true. */
209
210 struct GTY((chain_next ("%h.prev"))) c_binding {
211 tree decl; /* the decl bound */
212 tree type; /* the type in this scope */
213 tree id; /* the identifier it's bound to */
214 struct c_binding *prev; /* the previous decl in this scope */
215 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
216 unsigned int depth : 28; /* depth of this scope */
217 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
218 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
219 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
220 /* one free bit */
221 location_t locus; /* location for nested bindings */
222 };
223 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
224 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
225 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
226 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
227
228 #define I_SYMBOL_BINDING(node) \
229 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
230 #define I_SYMBOL_DECL(node) \
231 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
232
233 #define I_TAG_BINDING(node) \
234 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
235 #define I_TAG_DECL(node) \
236 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
237
238 #define I_LABEL_BINDING(node) \
239 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
240 #define I_LABEL_DECL(node) \
241 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
242
243 /* Each C symbol points to three linked lists of c_binding structures.
244 These describe the values of the identifier in the three different
245 namespaces defined by the language. */
246
247 struct GTY(()) lang_identifier {
248 struct c_common_identifier common_id;
249 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
250 struct c_binding *tag_binding; /* struct/union/enum tags */
251 struct c_binding *label_binding; /* labels */
252 };
253
254 /* Validate c-lang.c's assumptions. */
255 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
256 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
257
258 /* The resulting tree type. */
259
260 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
261 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : ((union lang_tree_node *) TREE_CHAIN (&%h.generic))"))) lang_tree_node
262 {
263 union tree_node GTY ((tag ("0"),
264 desc ("tree_node_structure (&%h)")))
265 generic;
266 struct lang_identifier GTY ((tag ("1"))) identifier;
267 };
268
269 /* Each c_scope structure describes the complete contents of one
270 scope. Four scopes are distinguished specially: the innermost or
271 current scope, the innermost function scope, the file scope (always
272 the second to outermost) and the outermost or external scope.
273
274 Most declarations are recorded in the current scope.
275
276 All normal label declarations are recorded in the innermost
277 function scope, as are bindings of undeclared identifiers to
278 error_mark_node. (GCC permits nested functions as an extension,
279 hence the 'innermost' qualifier.) Explicitly declared labels
280 (using the __label__ extension) appear in the current scope.
281
282 Being in the file scope (current_scope == file_scope) causes
283 special behavior in several places below. Also, under some
284 conditions the Objective-C front end records declarations in the
285 file scope even though that isn't the current scope.
286
287 All declarations with external linkage are recorded in the external
288 scope, even if they aren't visible there; this models the fact that
289 such declarations are visible to the entire program, and (with a
290 bit of cleverness, see pushdecl) allows diagnosis of some violations
291 of C99 6.2.2p7 and 6.2.7p2:
292
293 If, within the same translation unit, the same identifier appears
294 with both internal and external linkage, the behavior is
295 undefined.
296
297 All declarations that refer to the same object or function shall
298 have compatible type; otherwise, the behavior is undefined.
299
300 Initially only the built-in declarations, which describe compiler
301 intrinsic functions plus a subset of the standard library, are in
302 this scope.
303
304 The order of the blocks list matters, and it is frequently appended
305 to. To avoid having to walk all the way to the end of the list on
306 each insertion, or reverse the list later, we maintain a pointer to
307 the last list entry. (FIXME: It should be feasible to use a reversed
308 list here.)
309
310 The bindings list is strictly in reverse order of declarations;
311 pop_scope relies on this. */
312
313
314 struct GTY((chain_next ("%h.outer"))) c_scope {
315 /* The scope containing this one. */
316 struct c_scope *outer;
317
318 /* The next outermost function scope. */
319 struct c_scope *outer_function;
320
321 /* All bindings in this scope. */
322 struct c_binding *bindings;
323
324 /* For each scope (except the global one), a chain of BLOCK nodes
325 for all the scopes that were entered and exited one level down. */
326 tree blocks;
327 tree blocks_last;
328
329 /* The depth of this scope. Used to keep the ->shadowed chain of
330 bindings sorted innermost to outermost. */
331 unsigned int depth : 28;
332
333 /* True if we are currently filling this scope with parameter
334 declarations. */
335 BOOL_BITFIELD parm_flag : 1;
336
337 /* True if we saw [*] in this scope. Used to give an error messages
338 if these appears in a function definition. */
339 BOOL_BITFIELD had_vla_unspec : 1;
340
341 /* True if we already complained about forward parameter decls
342 in this scope. This prevents double warnings on
343 foo (int a; int b; ...) */
344 BOOL_BITFIELD warned_forward_parm_decls : 1;
345
346 /* True if this is the outermost block scope of a function body.
347 This scope contains the parameters, the local variables declared
348 in the outermost block, and all the labels (except those in
349 nested functions, or declared at block scope with __label__). */
350 BOOL_BITFIELD function_body : 1;
351
352 /* True means make a BLOCK for this scope no matter what. */
353 BOOL_BITFIELD keep : 1;
354
355 /* True means that an unsuffixed float constant is _Decimal64. */
356 BOOL_BITFIELD float_const_decimal64 : 1;
357 };
358
359 /* The scope currently in effect. */
360
361 static GTY(()) struct c_scope *current_scope;
362
363 /* The innermost function scope. Ordinary (not explicitly declared)
364 labels, bindings to error_mark_node, and the lazily-created
365 bindings of __func__ and its friends get this scope. */
366
367 static GTY(()) struct c_scope *current_function_scope;
368
369 /* The C file scope. This is reset for each input translation unit. */
370
371 static GTY(()) struct c_scope *file_scope;
372
373 /* The outermost scope. This is used for all declarations with
374 external linkage, and only these, hence the name. */
375
376 static GTY(()) struct c_scope *external_scope;
377
378 /* A chain of c_scope structures awaiting reuse. */
379
380 static GTY((deletable)) struct c_scope *scope_freelist;
381
382 /* A chain of c_binding structures awaiting reuse. */
383
384 static GTY((deletable)) struct c_binding *binding_freelist;
385
386 /* Append VAR to LIST in scope SCOPE. */
387 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
388 struct c_scope *s_ = (scope); \
389 tree d_ = (decl); \
390 if (s_->list##_last) \
391 BLOCK_CHAIN (s_->list##_last) = d_; \
392 else \
393 s_->list = d_; \
394 s_->list##_last = d_; \
395 } while (0)
396
397 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
398 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
399 struct c_scope *t_ = (tscope); \
400 struct c_scope *f_ = (fscope); \
401 if (t_->to##_last) \
402 BLOCK_CHAIN (t_->to##_last) = f_->from; \
403 else \
404 t_->to = f_->from; \
405 t_->to##_last = f_->from##_last; \
406 } while (0)
407
408 /* A c_inline_static structure stores details of a static identifier
409 referenced in a definition of a function that may be an inline
410 definition if no subsequent declaration of that function uses
411 "extern" or does not use "inline". */
412
413 struct GTY((chain_next ("%h.next"))) c_inline_static {
414 /* The location for a diagnostic. */
415 location_t location;
416
417 /* The function that may be an inline definition. */
418 tree function;
419
420 /* The object or function referenced. */
421 tree static_decl;
422
423 /* What sort of reference this is. */
424 enum c_inline_static_type type;
425
426 /* The next such structure or NULL. */
427 struct c_inline_static *next;
428 };
429
430 /* List of static identifiers used or referenced in functions that may
431 be inline definitions. */
432 static GTY(()) struct c_inline_static *c_inline_statics;
433
434 /* True means unconditionally make a BLOCK for the next scope pushed. */
435
436 static bool keep_next_level_flag;
437
438 /* True means the next call to push_scope will be the outermost scope
439 of a function body, so do not push a new scope, merely cease
440 expecting parameter decls. */
441
442 static bool next_is_function_body;
443
444 /* Forward declarations. */
445 static tree lookup_name_in_scope (tree, struct c_scope *);
446 static tree c_make_fname_decl (tree, int);
447 static tree grokdeclarator (const struct c_declarator *,
448 struct c_declspecs *,
449 enum decl_context, bool, tree *, tree *, tree *,
450 bool *, enum deprecated_states);
451 static tree grokparms (struct c_arg_info *, bool);
452 static void layout_array_type (tree);
453 \f
454 /* T is a statement. Add it to the statement-tree. This is the
455 C/ObjC version--C++ has a slightly different version of this
456 function. */
457
458 tree
459 add_stmt (tree t)
460 {
461 enum tree_code code = TREE_CODE (t);
462
463 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
464 {
465 if (!EXPR_HAS_LOCATION (t))
466 SET_EXPR_LOCATION (t, input_location);
467 }
468
469 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
470 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
471
472 /* Add T to the statement-tree. Non-side-effect statements need to be
473 recorded during statement expressions. */
474 append_to_statement_list_force (t, &cur_stmt_list);
475
476 return t;
477 }
478 \f
479
480 void
481 c_print_identifier (FILE *file, tree node, int indent)
482 {
483 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
484 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
485 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
486 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
487 {
488 tree rid = ridpointers[C_RID_CODE (node)];
489 indent_to (file, indent + 4);
490 fprintf (file, "rid %p \"%s\"",
491 (void *) rid, IDENTIFIER_POINTER (rid));
492 }
493 }
494
495 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
496 which may be any of several kinds of DECL or TYPE or error_mark_node,
497 in the scope SCOPE. */
498 static void
499 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
500 bool nested, location_t locus)
501 {
502 struct c_binding *b, **here;
503
504 if (binding_freelist)
505 {
506 b = binding_freelist;
507 binding_freelist = b->prev;
508 }
509 else
510 b = GGC_NEW (struct c_binding);
511
512 b->shadowed = 0;
513 b->decl = decl;
514 b->id = name;
515 b->depth = scope->depth;
516 b->invisible = invisible;
517 b->nested = nested;
518 b->inner_comp = 0;
519 b->locus = locus;
520
521 b->type = 0;
522
523 b->prev = scope->bindings;
524 scope->bindings = b;
525
526 if (!name)
527 return;
528
529 switch (TREE_CODE (decl))
530 {
531 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
532 case ENUMERAL_TYPE:
533 case UNION_TYPE:
534 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
535 case VAR_DECL:
536 case FUNCTION_DECL:
537 case TYPE_DECL:
538 case CONST_DECL:
539 case PARM_DECL:
540 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
541
542 default:
543 gcc_unreachable ();
544 }
545
546 /* Locate the appropriate place in the chain of shadowed decls
547 to insert this binding. Normally, scope == current_scope and
548 this does nothing. */
549 while (*here && (*here)->depth > scope->depth)
550 here = &(*here)->shadowed;
551
552 b->shadowed = *here;
553 *here = b;
554 }
555
556 /* Clear the binding structure B, stick it on the binding_freelist,
557 and return the former value of b->prev. This is used by pop_scope
558 and get_parm_info to iterate destructively over all the bindings
559 from a given scope. */
560 static struct c_binding *
561 free_binding_and_advance (struct c_binding *b)
562 {
563 struct c_binding *prev = b->prev;
564
565 memset (b, 0, sizeof (struct c_binding));
566 b->prev = binding_freelist;
567 binding_freelist = b;
568
569 return prev;
570 }
571
572 \f
573 /* Hook called at end of compilation to assume 1 elt
574 for a file-scope tentative array defn that wasn't complete before. */
575
576 void
577 c_finish_incomplete_decl (tree decl)
578 {
579 if (TREE_CODE (decl) == VAR_DECL)
580 {
581 tree type = TREE_TYPE (decl);
582 if (type != error_mark_node
583 && TREE_CODE (type) == ARRAY_TYPE
584 && !DECL_EXTERNAL (decl)
585 && TYPE_DOMAIN (type) == 0)
586 {
587 warning (0, "array %q+D assumed to have one element", decl);
588
589 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
590
591 layout_decl (decl, 0);
592 }
593 }
594 }
595 \f
596 /* Record that inline function FUNC contains a reference (location
597 LOC) to static DECL (file-scope or function-local according to
598 TYPE). */
599
600 void
601 record_inline_static (location_t loc, tree func, tree decl,
602 enum c_inline_static_type type)
603 {
604 struct c_inline_static *csi = GGC_NEW (struct c_inline_static);
605 csi->location = loc;
606 csi->function = func;
607 csi->static_decl = decl;
608 csi->type = type;
609 csi->next = c_inline_statics;
610 c_inline_statics = csi;
611 }
612
613 /* Check for references to static declarations in inline functions at
614 the end of the translation unit and diagnose them if the functions
615 are still inline definitions. */
616
617 static void
618 check_inline_statics (void)
619 {
620 struct c_inline_static *csi;
621 for (csi = c_inline_statics; csi; csi = csi->next)
622 {
623 if (DECL_EXTERNAL (csi->function))
624 switch (csi->type)
625 {
626 case csi_internal:
627 pedwarn (csi->location, 0,
628 "%qD is static but used in inline function %qD "
629 "which is not static", csi->static_decl, csi->function);
630 break;
631 case csi_modifiable:
632 pedwarn (csi->location, 0,
633 "%q+D is static but declared in inline function %qD "
634 "which is not static", csi->static_decl, csi->function);
635 break;
636 default:
637 gcc_unreachable ();
638 }
639 }
640 c_inline_statics = NULL;
641 }
642 \f
643 /* The Objective-C front-end often needs to determine the current scope. */
644
645 void *
646 objc_get_current_scope (void)
647 {
648 return current_scope;
649 }
650
651 /* The following function is used only by Objective-C. It needs to live here
652 because it accesses the innards of c_scope. */
653
654 void
655 objc_mark_locals_volatile (void *enclosing_blk)
656 {
657 struct c_scope *scope;
658 struct c_binding *b;
659
660 for (scope = current_scope;
661 scope && scope != enclosing_blk;
662 scope = scope->outer)
663 {
664 for (b = scope->bindings; b; b = b->prev)
665 objc_volatilize_decl (b->decl);
666
667 /* Do not climb up past the current function. */
668 if (scope->function_body)
669 break;
670 }
671 }
672
673 /* Nonzero if we are currently in file scope. */
674
675 int
676 global_bindings_p (void)
677 {
678 return (current_scope == file_scope && !c_override_global_bindings_to_false
679 ? -1
680 : 0);
681 }
682
683 void
684 keep_next_level (void)
685 {
686 keep_next_level_flag = true;
687 }
688
689 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
690
691 void
692 set_float_const_decimal64 (void)
693 {
694 current_scope->float_const_decimal64 = true;
695 }
696
697 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
698
699 void
700 clear_float_const_decimal64 (void)
701 {
702 current_scope->float_const_decimal64 = false;
703 }
704
705 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
706
707 bool
708 float_const_decimal64_p (void)
709 {
710 return current_scope->float_const_decimal64;
711 }
712
713 /* Identify this scope as currently being filled with parameters. */
714
715 void
716 declare_parm_level (void)
717 {
718 current_scope->parm_flag = true;
719 }
720
721 void
722 push_scope (void)
723 {
724 if (next_is_function_body)
725 {
726 /* This is the transition from the parameters to the top level
727 of the function body. These are the same scope
728 (C99 6.2.1p4,6) so we do not push another scope structure.
729 next_is_function_body is set only by store_parm_decls, which
730 in turn is called when and only when we are about to
731 encounter the opening curly brace for the function body.
732
733 The outermost block of a function always gets a BLOCK node,
734 because the debugging output routines expect that each
735 function has at least one BLOCK. */
736 current_scope->parm_flag = false;
737 current_scope->function_body = true;
738 current_scope->keep = true;
739 current_scope->outer_function = current_function_scope;
740 current_function_scope = current_scope;
741
742 keep_next_level_flag = false;
743 next_is_function_body = false;
744
745 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
746 if (current_scope->outer)
747 current_scope->float_const_decimal64
748 = current_scope->outer->float_const_decimal64;
749 else
750 current_scope->float_const_decimal64 = false;
751 }
752 else
753 {
754 struct c_scope *scope;
755 if (scope_freelist)
756 {
757 scope = scope_freelist;
758 scope_freelist = scope->outer;
759 }
760 else
761 scope = GGC_CNEW (struct c_scope);
762
763 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
764 if (current_scope)
765 scope->float_const_decimal64 = current_scope->float_const_decimal64;
766 else
767 scope->float_const_decimal64 = false;
768
769 scope->keep = keep_next_level_flag;
770 scope->outer = current_scope;
771 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
772
773 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
774 possible. */
775 if (current_scope && scope->depth == 0)
776 {
777 scope->depth--;
778 sorry ("GCC supports only %u nested scopes", scope->depth);
779 }
780
781 current_scope = scope;
782 keep_next_level_flag = false;
783 }
784 }
785
786 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
787
788 static void
789 set_type_context (tree type, tree context)
790 {
791 for (type = TYPE_MAIN_VARIANT (type); type;
792 type = TYPE_NEXT_VARIANT (type))
793 TYPE_CONTEXT (type) = context;
794 }
795
796 /* Exit a scope. Restore the state of the identifier-decl mappings
797 that were in effect when this scope was entered. Return a BLOCK
798 node containing all the DECLs in this scope that are of interest
799 to debug info generation. */
800
801 tree
802 pop_scope (void)
803 {
804 struct c_scope *scope = current_scope;
805 tree block, context, p;
806 struct c_binding *b;
807
808 bool functionbody = scope->function_body;
809 bool keep = functionbody || scope->keep || scope->bindings;
810
811 c_end_vm_scope (scope->depth);
812
813 /* If appropriate, create a BLOCK to record the decls for the life
814 of this function. */
815 block = 0;
816 if (keep)
817 {
818 block = make_node (BLOCK);
819 BLOCK_SUBBLOCKS (block) = scope->blocks;
820 TREE_USED (block) = 1;
821
822 /* In each subblock, record that this is its superior. */
823 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
824 BLOCK_SUPERCONTEXT (p) = block;
825
826 BLOCK_VARS (block) = 0;
827 }
828
829 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
830 scope must be set so that they point to the appropriate
831 construct, i.e. either to the current FUNCTION_DECL node, or
832 else to the BLOCK node we just constructed.
833
834 Note that for tagged types whose scope is just the formal
835 parameter list for some function type specification, we can't
836 properly set their TYPE_CONTEXTs here, because we don't have a
837 pointer to the appropriate FUNCTION_TYPE node readily available
838 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
839 type nodes get set in `grokdeclarator' as soon as we have created
840 the FUNCTION_TYPE node which will represent the "scope" for these
841 "parameter list local" tagged types. */
842 if (scope->function_body)
843 context = current_function_decl;
844 else if (scope == file_scope)
845 {
846 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
847 TREE_CHAIN (file_decl) = all_translation_units;
848 all_translation_units = file_decl;
849 context = file_decl;
850 }
851 else
852 context = block;
853
854 /* Clear all bindings in this scope. */
855 for (b = scope->bindings; b; b = free_binding_and_advance (b))
856 {
857 p = b->decl;
858 switch (TREE_CODE (p))
859 {
860 case LABEL_DECL:
861 /* Warnings for unused labels, errors for undefined labels. */
862 if (TREE_USED (p) && !DECL_INITIAL (p))
863 {
864 error ("label %q+D used but not defined", p);
865 DECL_INITIAL (p) = error_mark_node;
866 }
867 else
868 warn_for_unused_label (p);
869
870 /* Labels go in BLOCK_VARS. */
871 TREE_CHAIN (p) = BLOCK_VARS (block);
872 BLOCK_VARS (block) = p;
873 gcc_assert (I_LABEL_BINDING (b->id) == b);
874 I_LABEL_BINDING (b->id) = b->shadowed;
875 break;
876
877 case ENUMERAL_TYPE:
878 case UNION_TYPE:
879 case RECORD_TYPE:
880 set_type_context (p, context);
881
882 /* Types may not have tag-names, in which case the type
883 appears in the bindings list with b->id NULL. */
884 if (b->id)
885 {
886 gcc_assert (I_TAG_BINDING (b->id) == b);
887 I_TAG_BINDING (b->id) = b->shadowed;
888 }
889 break;
890
891 case FUNCTION_DECL:
892 /* Propagate TREE_ADDRESSABLE from nested functions to their
893 containing functions. */
894 if (!TREE_ASM_WRITTEN (p)
895 && DECL_INITIAL (p) != 0
896 && TREE_ADDRESSABLE (p)
897 && DECL_ABSTRACT_ORIGIN (p) != 0
898 && DECL_ABSTRACT_ORIGIN (p) != p)
899 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
900 if (!DECL_EXTERNAL (p)
901 && !DECL_INITIAL (p)
902 && scope != file_scope
903 && scope != external_scope)
904 {
905 error ("nested function %q+D declared but never defined", p);
906 undef_nested_function = true;
907 }
908 else if (DECL_DECLARED_INLINE_P (p)
909 && TREE_PUBLIC (p)
910 && !DECL_INITIAL (p))
911 {
912 /* C99 6.7.4p6: "a function with external linkage... declared
913 with an inline function specifier ... shall also be defined
914 in the same translation unit." */
915 if (!flag_gnu89_inline)
916 pedwarn (input_location, 0,
917 "inline function %q+D declared but never defined", p);
918 DECL_EXTERNAL (p) = 1;
919 }
920
921 goto common_symbol;
922
923 case VAR_DECL:
924 /* Warnings for unused variables. */
925 if (!TREE_USED (p)
926 && !TREE_NO_WARNING (p)
927 && !DECL_IN_SYSTEM_HEADER (p)
928 && DECL_NAME (p)
929 && !DECL_ARTIFICIAL (p)
930 && scope != file_scope
931 && scope != external_scope)
932 warning (OPT_Wunused_variable, "unused variable %q+D", p);
933
934 if (b->inner_comp)
935 {
936 error ("type of array %q+D completed incompatibly with"
937 " implicit initialization", p);
938 }
939
940 /* Fall through. */
941 case TYPE_DECL:
942 case CONST_DECL:
943 common_symbol:
944 /* All of these go in BLOCK_VARS, but only if this is the
945 binding in the home scope. */
946 if (!b->nested)
947 {
948 TREE_CHAIN (p) = BLOCK_VARS (block);
949 BLOCK_VARS (block) = p;
950 }
951 else if (VAR_OR_FUNCTION_DECL_P (p))
952 {
953 /* For block local externs add a special
954 DECL_EXTERNAL decl for debug info generation. */
955 tree extp = copy_node (p);
956
957 DECL_EXTERNAL (extp) = 1;
958 TREE_STATIC (extp) = 0;
959 TREE_PUBLIC (extp) = 1;
960 DECL_INITIAL (extp) = NULL_TREE;
961 DECL_LANG_SPECIFIC (extp) = NULL;
962 DECL_CONTEXT (extp) = current_function_decl;
963 if (TREE_CODE (p) == FUNCTION_DECL)
964 {
965 DECL_RESULT (extp) = NULL_TREE;
966 DECL_SAVED_TREE (extp) = NULL_TREE;
967 DECL_STRUCT_FUNCTION (extp) = NULL;
968 }
969 if (b->locus != UNKNOWN_LOCATION)
970 DECL_SOURCE_LOCATION (extp) = b->locus;
971 TREE_CHAIN (extp) = BLOCK_VARS (block);
972 BLOCK_VARS (block) = extp;
973 }
974 /* If this is the file scope, and we are processing more
975 than one translation unit in this compilation, set
976 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
977 This makes same_translation_unit_p work, and causes
978 static declarations to be given disambiguating suffixes. */
979 if (scope == file_scope && num_in_fnames > 1)
980 {
981 DECL_CONTEXT (p) = context;
982 if (TREE_CODE (p) == TYPE_DECL)
983 set_type_context (TREE_TYPE (p), context);
984 }
985
986 /* Fall through. */
987 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
988 already been put there by store_parm_decls. Unused-
989 parameter warnings are handled by function.c.
990 error_mark_node obviously does not go in BLOCK_VARS and
991 does not get unused-variable warnings. */
992 case PARM_DECL:
993 case ERROR_MARK:
994 /* It is possible for a decl not to have a name. We get
995 here with b->id NULL in this case. */
996 if (b->id)
997 {
998 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
999 I_SYMBOL_BINDING (b->id) = b->shadowed;
1000 if (b->shadowed && b->shadowed->type)
1001 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
1002 }
1003 break;
1004
1005 default:
1006 gcc_unreachable ();
1007 }
1008 }
1009
1010
1011 /* Dispose of the block that we just made inside some higher level. */
1012 if ((scope->function_body || scope == file_scope) && context)
1013 {
1014 DECL_INITIAL (context) = block;
1015 BLOCK_SUPERCONTEXT (block) = context;
1016 }
1017 else if (scope->outer)
1018 {
1019 if (block)
1020 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1021 /* If we did not make a block for the scope just exited, any
1022 blocks made for inner scopes must be carried forward so they
1023 will later become subblocks of something else. */
1024 else if (scope->blocks)
1025 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1026 }
1027
1028 /* Pop the current scope, and free the structure for reuse. */
1029 current_scope = scope->outer;
1030 if (scope->function_body)
1031 current_function_scope = scope->outer_function;
1032
1033 memset (scope, 0, sizeof (struct c_scope));
1034 scope->outer = scope_freelist;
1035 scope_freelist = scope;
1036
1037 return block;
1038 }
1039
1040 void
1041 push_file_scope (void)
1042 {
1043 tree decl;
1044
1045 if (file_scope)
1046 return;
1047
1048 push_scope ();
1049 file_scope = current_scope;
1050
1051 start_fname_decls ();
1052
1053 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
1054 bind (DECL_NAME (decl), decl, file_scope,
1055 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1056 }
1057
1058 void
1059 pop_file_scope (void)
1060 {
1061 /* In case there were missing closebraces, get us back to the global
1062 binding level. */
1063 while (current_scope != file_scope)
1064 pop_scope ();
1065
1066 /* __FUNCTION__ is defined at file scope (""). This
1067 call may not be necessary as my tests indicate it
1068 still works without it. */
1069 finish_fname_decls ();
1070
1071 check_inline_statics ();
1072
1073 /* This is the point to write out a PCH if we're doing that.
1074 In that case we do not want to do anything else. */
1075 if (pch_file)
1076 {
1077 c_common_write_pch ();
1078 return;
1079 }
1080
1081 /* Pop off the file scope and close this translation unit. */
1082 pop_scope ();
1083 file_scope = 0;
1084
1085 maybe_apply_pending_pragma_weaks ();
1086 cgraph_finalize_compilation_unit ();
1087 }
1088
1089 \f
1090 /* Push a definition or a declaration of struct, union or enum tag "name".
1091 "type" should be the type node.
1092 We assume that the tag "name" is not already defined.
1093
1094 Note that the definition may really be just a forward reference.
1095 In that case, the TYPE_SIZE will be zero. */
1096
1097 static void
1098 pushtag (tree name, tree type, location_t loc)
1099 {
1100 /* Record the identifier as the type's name if it has none. */
1101 if (name && !TYPE_NAME (type))
1102 TYPE_NAME (type) = name;
1103 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1104
1105 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1106 tagged type we just added to the current scope. This fake
1107 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1108 to output a representation of a tagged type, and it also gives
1109 us a convenient place to record the "scope start" address for the
1110 tagged type. */
1111
1112 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
1113
1114 /* An approximation for now, so we can tell this is a function-scope tag.
1115 This will be updated in pop_scope. */
1116 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1117
1118 if (warn_cxx_compat && name != NULL_TREE)
1119 {
1120 struct c_binding *b = I_SYMBOL_BINDING (name);
1121
1122 if (b != NULL
1123 && b->decl != NULL_TREE
1124 && TREE_CODE (b->decl) == TYPE_DECL
1125 && (B_IN_CURRENT_SCOPE (b)
1126 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1127 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1128 != TYPE_MAIN_VARIANT (type)))
1129 {
1130 warning_at (loc, OPT_Wc___compat,
1131 ("using %qD as both a typedef and a tag is "
1132 "invalid in C++"),
1133 b->decl);
1134 if (b->locus != UNKNOWN_LOCATION)
1135 inform (b->locus, "originally defined here");
1136 }
1137 }
1138 }
1139 \f
1140 /* Subroutine of compare_decls. Allow harmless mismatches in return
1141 and argument types provided that the type modes match. This function
1142 return a unified type given a suitable match, and 0 otherwise. */
1143
1144 static tree
1145 match_builtin_function_types (tree newtype, tree oldtype)
1146 {
1147 tree newrettype, oldrettype;
1148 tree newargs, oldargs;
1149 tree trytype, tryargs;
1150
1151 /* Accept the return type of the new declaration if same modes. */
1152 oldrettype = TREE_TYPE (oldtype);
1153 newrettype = TREE_TYPE (newtype);
1154
1155 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1156 return 0;
1157
1158 oldargs = TYPE_ARG_TYPES (oldtype);
1159 newargs = TYPE_ARG_TYPES (newtype);
1160 tryargs = newargs;
1161
1162 while (oldargs || newargs)
1163 {
1164 if (!oldargs
1165 || !newargs
1166 || !TREE_VALUE (oldargs)
1167 || !TREE_VALUE (newargs)
1168 || TYPE_MODE (TREE_VALUE (oldargs))
1169 != TYPE_MODE (TREE_VALUE (newargs)))
1170 return 0;
1171
1172 oldargs = TREE_CHAIN (oldargs);
1173 newargs = TREE_CHAIN (newargs);
1174 }
1175
1176 trytype = build_function_type (newrettype, tryargs);
1177 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1178 }
1179
1180 /* Subroutine of diagnose_mismatched_decls. Check for function type
1181 mismatch involving an empty arglist vs a nonempty one and give clearer
1182 diagnostics. */
1183 static void
1184 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1185 tree newtype, tree oldtype)
1186 {
1187 tree t;
1188
1189 if (TREE_CODE (olddecl) != FUNCTION_DECL
1190 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1191 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1192 ||
1193 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1194 return;
1195
1196 t = TYPE_ARG_TYPES (oldtype);
1197 if (t == 0)
1198 t = TYPE_ARG_TYPES (newtype);
1199 for (; t; t = TREE_CHAIN (t))
1200 {
1201 tree type = TREE_VALUE (t);
1202
1203 if (TREE_CHAIN (t) == 0
1204 && TYPE_MAIN_VARIANT (type) != void_type_node)
1205 {
1206 inform (input_location, "a parameter list with an ellipsis can%'t match "
1207 "an empty parameter name list declaration");
1208 break;
1209 }
1210
1211 if (c_type_promotes_to (type) != type)
1212 {
1213 inform (input_location, "an argument type that has a default promotion can%'t match "
1214 "an empty parameter name list declaration");
1215 break;
1216 }
1217 }
1218 }
1219
1220 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1221 old-style function definition, NEWDECL is a prototype declaration.
1222 Diagnose inconsistencies in the argument list. Returns TRUE if
1223 the prototype is compatible, FALSE if not. */
1224 static bool
1225 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1226 {
1227 tree newargs, oldargs;
1228 int i;
1229
1230 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1231
1232 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1233 newargs = TYPE_ARG_TYPES (newtype);
1234 i = 1;
1235
1236 for (;;)
1237 {
1238 tree oldargtype = TREE_VALUE (oldargs);
1239 tree newargtype = TREE_VALUE (newargs);
1240
1241 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1242 return false;
1243
1244 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1245 newargtype = TYPE_MAIN_VARIANT (newargtype);
1246
1247 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1248 break;
1249
1250 /* Reaching the end of just one list means the two decls don't
1251 agree on the number of arguments. */
1252 if (END_OF_ARGLIST (oldargtype))
1253 {
1254 error ("prototype for %q+D declares more arguments "
1255 "than previous old-style definition", newdecl);
1256 return false;
1257 }
1258 else if (END_OF_ARGLIST (newargtype))
1259 {
1260 error ("prototype for %q+D declares fewer arguments "
1261 "than previous old-style definition", newdecl);
1262 return false;
1263 }
1264
1265 /* Type for passing arg must be consistent with that declared
1266 for the arg. */
1267 else if (!comptypes (oldargtype, newargtype))
1268 {
1269 error ("prototype for %q+D declares argument %d"
1270 " with incompatible type",
1271 newdecl, i);
1272 return false;
1273 }
1274
1275 oldargs = TREE_CHAIN (oldargs);
1276 newargs = TREE_CHAIN (newargs);
1277 i++;
1278 }
1279
1280 /* If we get here, no errors were found, but do issue a warning
1281 for this poor-style construct. */
1282 warning (0, "prototype for %q+D follows non-prototype definition",
1283 newdecl);
1284 return true;
1285 #undef END_OF_ARGLIST
1286 }
1287
1288 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1289 first in a pair of mismatched declarations, using the diagnostic
1290 function DIAG. */
1291 static void
1292 locate_old_decl (tree decl)
1293 {
1294 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1295 ;
1296 else if (DECL_INITIAL (decl))
1297 inform (input_location, "previous definition of %q+D was here", decl);
1298 else if (C_DECL_IMPLICIT (decl))
1299 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1300 else
1301 inform (input_location, "previous declaration of %q+D was here", decl);
1302 }
1303
1304 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1305 Returns true if the caller should proceed to merge the two, false
1306 if OLDDECL should simply be discarded. As a side effect, issues
1307 all necessary diagnostics for invalid or poor-style combinations.
1308 If it returns true, writes the types of NEWDECL and OLDDECL to
1309 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1310 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1311
1312 static bool
1313 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1314 tree *newtypep, tree *oldtypep)
1315 {
1316 tree newtype, oldtype;
1317 bool pedwarned = false;
1318 bool warned = false;
1319 bool retval = true;
1320
1321 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1322 && DECL_EXTERNAL (DECL))
1323
1324 /* If we have error_mark_node for either decl or type, just discard
1325 the previous decl - we're in an error cascade already. */
1326 if (olddecl == error_mark_node || newdecl == error_mark_node)
1327 return false;
1328 *oldtypep = oldtype = TREE_TYPE (olddecl);
1329 *newtypep = newtype = TREE_TYPE (newdecl);
1330 if (oldtype == error_mark_node || newtype == error_mark_node)
1331 return false;
1332
1333 /* Two different categories of symbol altogether. This is an error
1334 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1335 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1336 {
1337 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1338 && DECL_BUILT_IN (olddecl)
1339 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1340 {
1341 error ("%q+D redeclared as different kind of symbol", newdecl);
1342 locate_old_decl (olddecl);
1343 }
1344 else if (TREE_PUBLIC (newdecl))
1345 warning (0, "built-in function %q+D declared as non-function",
1346 newdecl);
1347 else
1348 warning (OPT_Wshadow, "declaration of %q+D shadows "
1349 "a built-in function", newdecl);
1350 return false;
1351 }
1352
1353 /* Enumerators have no linkage, so may only be declared once in a
1354 given scope. */
1355 if (TREE_CODE (olddecl) == CONST_DECL)
1356 {
1357 error ("redeclaration of enumerator %q+D", newdecl);
1358 locate_old_decl (olddecl);
1359 return false;
1360 }
1361
1362 if (!comptypes (oldtype, newtype))
1363 {
1364 if (TREE_CODE (olddecl) == FUNCTION_DECL
1365 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1366 {
1367 /* Accept harmless mismatch in function types.
1368 This is for the ffs and fprintf builtins. */
1369 tree trytype = match_builtin_function_types (newtype, oldtype);
1370
1371 if (trytype && comptypes (newtype, trytype))
1372 *oldtypep = oldtype = trytype;
1373 else
1374 {
1375 /* If types don't match for a built-in, throw away the
1376 built-in. No point in calling locate_old_decl here, it
1377 won't print anything. */
1378 warning (0, "conflicting types for built-in function %q+D",
1379 newdecl);
1380 return false;
1381 }
1382 }
1383 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1384 && DECL_IS_BUILTIN (olddecl))
1385 {
1386 /* A conflicting function declaration for a predeclared
1387 function that isn't actually built in. Objective C uses
1388 these. The new declaration silently overrides everything
1389 but the volatility (i.e. noreturn) indication. See also
1390 below. FIXME: Make Objective C use normal builtins. */
1391 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1392 return false;
1393 }
1394 /* Permit void foo (...) to match int foo (...) if the latter is
1395 the definition and implicit int was used. See
1396 c-torture/compile/920625-2.c. */
1397 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1398 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1399 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1400 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1401 {
1402 pedwarned = pedwarn (input_location, 0,
1403 "conflicting types for %q+D", newdecl);
1404 /* Make sure we keep void as the return type. */
1405 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1406 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1407 }
1408 /* Permit void foo (...) to match an earlier call to foo (...) with
1409 no declared type (thus, implicitly int). */
1410 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1411 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1412 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1413 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1414 {
1415 pedwarned = pedwarn (input_location, 0,
1416 "conflicting types for %q+D", newdecl);
1417 /* Make sure we keep void as the return type. */
1418 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1419 }
1420 else
1421 {
1422 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1423 error ("conflicting type qualifiers for %q+D", newdecl);
1424 else
1425 error ("conflicting types for %q+D", newdecl);
1426 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1427 locate_old_decl (olddecl);
1428 return false;
1429 }
1430 }
1431
1432 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1433 but silently ignore the redeclaration if either is in a system
1434 header. (Conflicting redeclarations were handled above.) */
1435 if (TREE_CODE (newdecl) == TYPE_DECL)
1436 {
1437 if (DECL_IN_SYSTEM_HEADER (newdecl)
1438 || DECL_IN_SYSTEM_HEADER (olddecl)
1439 || TREE_NO_WARNING (newdecl)
1440 || TREE_NO_WARNING (olddecl))
1441 return true; /* Allow OLDDECL to continue in use. */
1442
1443 error ("redefinition of typedef %q+D", newdecl);
1444 locate_old_decl (olddecl);
1445 return false;
1446 }
1447
1448 /* Function declarations can either be 'static' or 'extern' (no
1449 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1450 can never conflict with each other on account of linkage
1451 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1452 gnu89 mode permits two definitions if one is 'extern inline' and
1453 one is not. The non- extern-inline definition supersedes the
1454 extern-inline definition. */
1455
1456 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1457 {
1458 /* If you declare a built-in function name as static, or
1459 define the built-in with an old-style definition (so we
1460 can't validate the argument list) the built-in definition is
1461 overridden, but optionally warn this was a bad choice of name. */
1462 if (DECL_BUILT_IN (olddecl)
1463 && !C_DECL_DECLARED_BUILTIN (olddecl)
1464 && (!TREE_PUBLIC (newdecl)
1465 || (DECL_INITIAL (newdecl)
1466 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1467 {
1468 warning (OPT_Wshadow, "declaration of %q+D shadows "
1469 "a built-in function", newdecl);
1470 /* Discard the old built-in function. */
1471 return false;
1472 }
1473
1474 if (DECL_INITIAL (newdecl))
1475 {
1476 if (DECL_INITIAL (olddecl))
1477 {
1478 /* If both decls are in the same TU and the new declaration
1479 isn't overriding an extern inline reject the new decl.
1480 In c99, no overriding is allowed in the same translation
1481 unit. */
1482 if ((!DECL_EXTERN_INLINE (olddecl)
1483 || DECL_EXTERN_INLINE (newdecl)
1484 || (!flag_gnu89_inline
1485 && (!DECL_DECLARED_INLINE_P (olddecl)
1486 || !lookup_attribute ("gnu_inline",
1487 DECL_ATTRIBUTES (olddecl)))
1488 && (!DECL_DECLARED_INLINE_P (newdecl)
1489 || !lookup_attribute ("gnu_inline",
1490 DECL_ATTRIBUTES (newdecl))))
1491 )
1492 && same_translation_unit_p (newdecl, olddecl))
1493 {
1494 error ("redefinition of %q+D", newdecl);
1495 locate_old_decl (olddecl);
1496 return false;
1497 }
1498 }
1499 }
1500 /* If we have a prototype after an old-style function definition,
1501 the argument types must be checked specially. */
1502 else if (DECL_INITIAL (olddecl)
1503 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1504 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1505 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1506 {
1507 locate_old_decl (olddecl);
1508 return false;
1509 }
1510 /* A non-static declaration (even an "extern") followed by a
1511 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1512 The same is true for a static forward declaration at block
1513 scope followed by a non-static declaration/definition at file
1514 scope. Static followed by non-static at the same scope is
1515 not undefined behavior, and is the most convenient way to get
1516 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1517 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1518 we do diagnose it if -Wtraditional. */
1519 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1520 {
1521 /* Two exceptions to the rule. If olddecl is an extern
1522 inline, or a predeclared function that isn't actually
1523 built in, newdecl silently overrides olddecl. The latter
1524 occur only in Objective C; see also above. (FIXME: Make
1525 Objective C use normal builtins.) */
1526 if (!DECL_IS_BUILTIN (olddecl)
1527 && !DECL_EXTERN_INLINE (olddecl))
1528 {
1529 error ("static declaration of %q+D follows "
1530 "non-static declaration", newdecl);
1531 locate_old_decl (olddecl);
1532 }
1533 return false;
1534 }
1535 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1536 {
1537 if (DECL_CONTEXT (olddecl))
1538 {
1539 error ("non-static declaration of %q+D follows "
1540 "static declaration", newdecl);
1541 locate_old_decl (olddecl);
1542 return false;
1543 }
1544 else if (warn_traditional)
1545 {
1546 warned |= warning (OPT_Wtraditional,
1547 "non-static declaration of %q+D "
1548 "follows static declaration", newdecl);
1549 }
1550 }
1551
1552 /* Make sure gnu_inline attribute is either not present, or
1553 present on all inline decls. */
1554 if (DECL_DECLARED_INLINE_P (olddecl)
1555 && DECL_DECLARED_INLINE_P (newdecl))
1556 {
1557 bool newa = lookup_attribute ("gnu_inline",
1558 DECL_ATTRIBUTES (newdecl)) != NULL;
1559 bool olda = lookup_attribute ("gnu_inline",
1560 DECL_ATTRIBUTES (olddecl)) != NULL;
1561 if (newa != olda)
1562 {
1563 error ("%<gnu_inline%> attribute present on %q+D",
1564 newa ? newdecl : olddecl);
1565 error ("%Jbut not here", newa ? olddecl : newdecl);
1566 }
1567 }
1568 }
1569 else if (TREE_CODE (newdecl) == VAR_DECL)
1570 {
1571 /* Only variables can be thread-local, and all declarations must
1572 agree on this property. */
1573 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1574 {
1575 /* Nothing to check. Since OLDDECL is marked threadprivate
1576 and NEWDECL does not have a thread-local attribute, we
1577 will merge the threadprivate attribute into NEWDECL. */
1578 ;
1579 }
1580 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1581 {
1582 if (DECL_THREAD_LOCAL_P (newdecl))
1583 error ("thread-local declaration of %q+D follows "
1584 "non-thread-local declaration", newdecl);
1585 else
1586 error ("non-thread-local declaration of %q+D follows "
1587 "thread-local declaration", newdecl);
1588
1589 locate_old_decl (olddecl);
1590 return false;
1591 }
1592
1593 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1594 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1595 {
1596 error ("redefinition of %q+D", newdecl);
1597 locate_old_decl (olddecl);
1598 return false;
1599 }
1600
1601 /* Objects declared at file scope: if the first declaration had
1602 external linkage (even if it was an external reference) the
1603 second must have external linkage as well, or the behavior is
1604 undefined. If the first declaration had internal linkage, then
1605 the second must too, or else be an external reference (in which
1606 case the composite declaration still has internal linkage).
1607 As for function declarations, we warn about the static-then-
1608 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1609 if (DECL_FILE_SCOPE_P (newdecl)
1610 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1611 {
1612 if (DECL_EXTERNAL (newdecl))
1613 {
1614 if (!DECL_FILE_SCOPE_P (olddecl))
1615 {
1616 error ("extern declaration of %q+D follows "
1617 "declaration with no linkage", newdecl);
1618 locate_old_decl (olddecl);
1619 return false;
1620 }
1621 else if (warn_traditional)
1622 {
1623 warned |= warning (OPT_Wtraditional,
1624 "non-static declaration of %q+D "
1625 "follows static declaration", newdecl);
1626 }
1627 }
1628 else
1629 {
1630 if (TREE_PUBLIC (newdecl))
1631 error ("non-static declaration of %q+D follows "
1632 "static declaration", newdecl);
1633 else
1634 error ("static declaration of %q+D follows "
1635 "non-static declaration", newdecl);
1636
1637 locate_old_decl (olddecl);
1638 return false;
1639 }
1640 }
1641 /* Two objects with the same name declared at the same block
1642 scope must both be external references (6.7p3). */
1643 else if (!DECL_FILE_SCOPE_P (newdecl))
1644 {
1645 if (DECL_EXTERNAL (newdecl))
1646 {
1647 /* Extern with initializer at block scope, which will
1648 already have received an error. */
1649 }
1650 else if (DECL_EXTERNAL (olddecl))
1651 {
1652 error ("declaration of %q+D with no linkage follows "
1653 "extern declaration", newdecl);
1654 locate_old_decl (olddecl);
1655 }
1656 else
1657 {
1658 error ("redeclaration of %q+D with no linkage", newdecl);
1659 locate_old_decl (olddecl);
1660 }
1661
1662 return false;
1663 }
1664 }
1665
1666 /* warnings */
1667 /* All decls must agree on a visibility. */
1668 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1669 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1670 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1671 {
1672 warned |= warning (0, "redeclaration of %q+D with different visibility "
1673 "(old visibility preserved)", newdecl);
1674 }
1675
1676 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1677 {
1678 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1679 if (DECL_DECLARED_INLINE_P (newdecl)
1680 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1681 {
1682 warned |= warning (OPT_Wattributes,
1683 "inline declaration of %qD follows "
1684 "declaration with attribute noinline", newdecl);
1685 }
1686 else if (DECL_DECLARED_INLINE_P (olddecl)
1687 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1688 {
1689 warned |= warning (OPT_Wattributes,
1690 "declaration of %q+D with attribute "
1691 "noinline follows inline declaration ", newdecl);
1692 }
1693 }
1694 else /* PARM_DECL, VAR_DECL */
1695 {
1696 /* Redeclaration of a parameter is a constraint violation (this is
1697 not explicitly stated, but follows from C99 6.7p3 [no more than
1698 one declaration of the same identifier with no linkage in the
1699 same scope, except type tags] and 6.2.2p6 [parameters have no
1700 linkage]). We must check for a forward parameter declaration,
1701 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1702 an extension, the mandatory diagnostic for which is handled by
1703 mark_forward_parm_decls. */
1704
1705 if (TREE_CODE (newdecl) == PARM_DECL
1706 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1707 {
1708 error ("redefinition of parameter %q+D", newdecl);
1709 locate_old_decl (olddecl);
1710 return false;
1711 }
1712 }
1713
1714 /* Optional warning for completely redundant decls. */
1715 if (!warned && !pedwarned
1716 && warn_redundant_decls
1717 /* Don't warn about a function declaration followed by a
1718 definition. */
1719 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1720 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1721 /* Don't warn about redundant redeclarations of builtins. */
1722 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1723 && !DECL_BUILT_IN (newdecl)
1724 && DECL_BUILT_IN (olddecl)
1725 && !C_DECL_DECLARED_BUILTIN (olddecl))
1726 /* Don't warn about an extern followed by a definition. */
1727 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1728 /* Don't warn about forward parameter decls. */
1729 && !(TREE_CODE (newdecl) == PARM_DECL
1730 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1731 /* Don't warn about a variable definition following a declaration. */
1732 && !(TREE_CODE (newdecl) == VAR_DECL
1733 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1734 {
1735 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1736 newdecl);
1737 }
1738
1739 /* Report location of previous decl/defn. */
1740 if (warned || pedwarned)
1741 locate_old_decl (olddecl);
1742
1743 #undef DECL_EXTERN_INLINE
1744
1745 return retval;
1746 }
1747
1748 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1749 consistent with OLDDECL, but carries new information. Merge the
1750 new information into OLDDECL. This function issues no
1751 diagnostics. */
1752
1753 static void
1754 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1755 {
1756 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1757 && DECL_INITIAL (newdecl) != 0);
1758 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1759 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1760 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1761 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1762 bool extern_changed = false;
1763
1764 /* For real parm decl following a forward decl, rechain the old decl
1765 in its new location and clear TREE_ASM_WRITTEN (it's not a
1766 forward decl anymore). */
1767 if (TREE_CODE (newdecl) == PARM_DECL
1768 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1769 {
1770 struct c_binding *b, **here;
1771
1772 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1773 if ((*here)->decl == olddecl)
1774 goto found;
1775 gcc_unreachable ();
1776
1777 found:
1778 b = *here;
1779 *here = b->prev;
1780 b->prev = current_scope->bindings;
1781 current_scope->bindings = b;
1782
1783 TREE_ASM_WRITTEN (olddecl) = 0;
1784 }
1785
1786 DECL_ATTRIBUTES (newdecl)
1787 = targetm.merge_decl_attributes (olddecl, newdecl);
1788
1789 /* Merge the data types specified in the two decls. */
1790 TREE_TYPE (newdecl)
1791 = TREE_TYPE (olddecl)
1792 = composite_type (newtype, oldtype);
1793
1794 /* Lay the type out, unless already done. */
1795 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1796 {
1797 if (TREE_TYPE (newdecl) != error_mark_node)
1798 layout_type (TREE_TYPE (newdecl));
1799 if (TREE_CODE (newdecl) != FUNCTION_DECL
1800 && TREE_CODE (newdecl) != TYPE_DECL
1801 && TREE_CODE (newdecl) != CONST_DECL)
1802 layout_decl (newdecl, 0);
1803 }
1804 else
1805 {
1806 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1807 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1808 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1809 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1810 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1811 {
1812 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1813 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
1814 }
1815 }
1816
1817 /* Keep the old rtl since we can safely use it. */
1818 if (HAS_RTL_P (olddecl))
1819 COPY_DECL_RTL (olddecl, newdecl);
1820
1821 /* Merge the type qualifiers. */
1822 if (TREE_READONLY (newdecl))
1823 TREE_READONLY (olddecl) = 1;
1824
1825 if (TREE_THIS_VOLATILE (newdecl))
1826 TREE_THIS_VOLATILE (olddecl) = 1;
1827
1828 /* Merge deprecatedness. */
1829 if (TREE_DEPRECATED (newdecl))
1830 TREE_DEPRECATED (olddecl) = 1;
1831
1832 /* If a decl is in a system header and the other isn't, keep the one on the
1833 system header. Otherwise, keep source location of definition rather than
1834 declaration and of prototype rather than non-prototype unless that
1835 prototype is built-in. */
1836 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1837 && DECL_IN_SYSTEM_HEADER (olddecl)
1838 && !DECL_IN_SYSTEM_HEADER (newdecl) )
1839 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1840 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1841 && DECL_IN_SYSTEM_HEADER (newdecl)
1842 && !DECL_IN_SYSTEM_HEADER (olddecl))
1843 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
1844 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1845 || (old_is_prototype && !new_is_prototype
1846 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1847 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1848
1849 /* Merge the initialization information. */
1850 if (DECL_INITIAL (newdecl) == 0)
1851 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1852
1853 /* Merge the threadprivate attribute. */
1854 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1855 {
1856 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1857 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1858 }
1859
1860 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1861 {
1862 /* Merge the section attribute.
1863 We want to issue an error if the sections conflict but that
1864 must be done later in decl_attributes since we are called
1865 before attributes are assigned. */
1866 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1867 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1868
1869 /* Copy the assembler name.
1870 Currently, it can only be defined in the prototype. */
1871 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1872
1873 /* Use visibility of whichever declaration had it specified */
1874 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1875 {
1876 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1877 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1878 }
1879
1880 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1881 {
1882 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1883 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1884 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1885 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1886 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1887 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1888 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1889 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1890 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1891 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1892 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1893 }
1894
1895 /* Merge the storage class information. */
1896 merge_weak (newdecl, olddecl);
1897
1898 /* For functions, static overrides non-static. */
1899 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1900 {
1901 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1902 /* This is since we don't automatically
1903 copy the attributes of NEWDECL into OLDDECL. */
1904 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1905 /* If this clears `static', clear it in the identifier too. */
1906 if (!TREE_PUBLIC (olddecl))
1907 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1908 }
1909 }
1910
1911 /* In c99, 'extern' declaration before (or after) 'inline' means this
1912 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1913 is present. */
1914 if (TREE_CODE (newdecl) == FUNCTION_DECL
1915 && !flag_gnu89_inline
1916 && (DECL_DECLARED_INLINE_P (newdecl)
1917 || DECL_DECLARED_INLINE_P (olddecl))
1918 && (!DECL_DECLARED_INLINE_P (newdecl)
1919 || !DECL_DECLARED_INLINE_P (olddecl)
1920 || !DECL_EXTERNAL (olddecl))
1921 && DECL_EXTERNAL (newdecl)
1922 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
1923 && !current_function_decl)
1924 DECL_EXTERNAL (newdecl) = 0;
1925
1926 if (DECL_EXTERNAL (newdecl))
1927 {
1928 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1929 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1930
1931 /* An extern decl does not override previous storage class. */
1932 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1933 if (!DECL_EXTERNAL (newdecl))
1934 {
1935 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1936 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1937 }
1938 }
1939 else
1940 {
1941 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1942 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1943 }
1944
1945 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1946 {
1947 /* If we're redefining a function previously defined as extern
1948 inline, make sure we emit debug info for the inline before we
1949 throw it away, in case it was inlined into a function that
1950 hasn't been written out yet. */
1951 if (new_is_definition && DECL_INITIAL (olddecl))
1952 /* The new defn must not be inline. */
1953 DECL_UNINLINABLE (newdecl) = 1;
1954 else
1955 {
1956 /* If either decl says `inline', this fn is inline, unless
1957 its definition was passed already. */
1958 if (DECL_DECLARED_INLINE_P (newdecl)
1959 || DECL_DECLARED_INLINE_P (olddecl))
1960 DECL_DECLARED_INLINE_P (newdecl) = 1;
1961
1962 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1963 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1964
1965 DECL_DISREGARD_INLINE_LIMITS (newdecl)
1966 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1967 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1968 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
1969 }
1970
1971 if (DECL_BUILT_IN (olddecl))
1972 {
1973 /* If redeclaring a builtin function, it stays built in.
1974 But it gets tagged as having been declared. */
1975 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1976 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1977 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1978 if (new_is_prototype)
1979 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1980 else
1981 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1982 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1983 }
1984
1985 /* Preserve function specific target and optimization options */
1986 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1987 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1988 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1989 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1990
1991 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1992 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1993 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1994 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1995
1996 /* Also preserve various other info from the definition. */
1997 if (!new_is_definition)
1998 {
1999 tree t;
2000 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2001 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2002 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2003 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2004 gimple_set_body (newdecl, gimple_body (olddecl));
2005 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2006 for (t = DECL_ARGUMENTS (newdecl); t ; t = TREE_CHAIN (t))
2007 DECL_CONTEXT (t) = newdecl;
2008
2009 /* See if we've got a function to instantiate from. */
2010 if (DECL_SAVED_TREE (olddecl))
2011 DECL_ABSTRACT_ORIGIN (newdecl)
2012 = DECL_ABSTRACT_ORIGIN (olddecl);
2013 }
2014 }
2015
2016 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
2017
2018 /* Merge the USED information. */
2019 if (TREE_USED (olddecl))
2020 TREE_USED (newdecl) = 1;
2021 else if (TREE_USED (newdecl))
2022 TREE_USED (olddecl) = 1;
2023
2024 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2025 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
2026 {
2027 unsigned olddecl_uid = DECL_UID (olddecl);
2028 tree olddecl_context = DECL_CONTEXT (olddecl);
2029 tree olddecl_arguments = NULL;
2030 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2031 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2032
2033 memcpy ((char *) olddecl + sizeof (struct tree_common),
2034 (char *) newdecl + sizeof (struct tree_common),
2035 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2036 switch (TREE_CODE (olddecl))
2037 {
2038 case FUNCTION_DECL:
2039 gimple_set_body (olddecl, gimple_body (newdecl));
2040 /* fall through */
2041
2042 case FIELD_DECL:
2043 case VAR_DECL:
2044 case PARM_DECL:
2045 case LABEL_DECL:
2046 case RESULT_DECL:
2047 case CONST_DECL:
2048 case TYPE_DECL:
2049 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2050 (char *) newdecl + sizeof (struct tree_decl_common),
2051 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2052 break;
2053
2054 default:
2055
2056 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2057 (char *) newdecl + sizeof (struct tree_decl_common),
2058 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2059 }
2060 DECL_UID (olddecl) = olddecl_uid;
2061 DECL_CONTEXT (olddecl) = olddecl_context;
2062 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2063 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2064 }
2065
2066 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2067 so that encode_section_info has a chance to look at the new decl
2068 flags and attributes. */
2069 if (DECL_RTL_SET_P (olddecl)
2070 && (TREE_CODE (olddecl) == FUNCTION_DECL
2071 || (TREE_CODE (olddecl) == VAR_DECL
2072 && TREE_STATIC (olddecl))))
2073 make_decl_rtl (olddecl);
2074
2075 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
2076 and the definition is coming from the old version, cgraph needs
2077 to be called again. */
2078 if (extern_changed && !new_is_definition
2079 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
2080 cgraph_mark_if_needed (olddecl);
2081 }
2082
2083 /* Handle when a new declaration NEWDECL has the same name as an old
2084 one OLDDECL in the same binding contour. Prints an error message
2085 if appropriate.
2086
2087 If safely possible, alter OLDDECL to look like NEWDECL, and return
2088 true. Otherwise, return false. */
2089
2090 static bool
2091 duplicate_decls (tree newdecl, tree olddecl)
2092 {
2093 tree newtype = NULL, oldtype = NULL;
2094
2095 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2096 {
2097 /* Avoid `unused variable' and other warnings for OLDDECL. */
2098 TREE_NO_WARNING (olddecl) = 1;
2099 return false;
2100 }
2101
2102 merge_decls (newdecl, olddecl, newtype, oldtype);
2103 return true;
2104 }
2105
2106 \f
2107 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2108 static void
2109 warn_if_shadowing (tree new_decl)
2110 {
2111 struct c_binding *b;
2112
2113 /* Shadow warnings wanted? */
2114 if (!warn_shadow
2115 /* No shadow warnings for internally generated vars. */
2116 || DECL_IS_BUILTIN (new_decl)
2117 /* No shadow warnings for vars made for inlining. */
2118 || DECL_FROM_INLINE (new_decl))
2119 return;
2120
2121 /* Is anything being shadowed? Invisible decls do not count. */
2122 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2123 if (b->decl && b->decl != new_decl && !b->invisible)
2124 {
2125 tree old_decl = b->decl;
2126
2127 if (old_decl == error_mark_node)
2128 {
2129 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2130 "non-variable", new_decl);
2131 break;
2132 }
2133 else if (TREE_CODE (old_decl) == PARM_DECL)
2134 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2135 new_decl);
2136 else if (DECL_FILE_SCOPE_P (old_decl))
2137 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2138 "declaration", new_decl);
2139 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2140 && DECL_BUILT_IN (old_decl))
2141 {
2142 warning (OPT_Wshadow, "declaration of %q+D shadows "
2143 "a built-in function", new_decl);
2144 break;
2145 }
2146 else
2147 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2148 new_decl);
2149
2150 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2151
2152 break;
2153 }
2154 }
2155
2156 /* Record a decl-node X as belonging to the current lexical scope.
2157 Check for errors (such as an incompatible declaration for the same
2158 name already seen in the same scope).
2159
2160 Returns either X or an old decl for the same name.
2161 If an old decl is returned, it may have been smashed
2162 to agree with what X says. */
2163
2164 tree
2165 pushdecl (tree x)
2166 {
2167 tree name = DECL_NAME (x);
2168 struct c_scope *scope = current_scope;
2169 struct c_binding *b;
2170 bool nested = false;
2171 location_t locus = DECL_SOURCE_LOCATION (x);
2172
2173 /* Must set DECL_CONTEXT for everything not at file scope or
2174 DECL_FILE_SCOPE_P won't work. Local externs don't count
2175 unless they have initializers (which generate code). */
2176 if (current_function_decl
2177 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2178 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2179 DECL_CONTEXT (x) = current_function_decl;
2180
2181 /* If this is of variably modified type, prevent jumping into its
2182 scope. */
2183 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2184 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2185 c_begin_vm_scope (scope->depth);
2186
2187 /* Anonymous decls are just inserted in the scope. */
2188 if (!name)
2189 {
2190 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2191 locus);
2192 return x;
2193 }
2194
2195 /* First, see if there is another declaration with the same name in
2196 the current scope. If there is, duplicate_decls may do all the
2197 work for us. If duplicate_decls returns false, that indicates
2198 two incompatible decls in the same scope; we are to silently
2199 replace the old one (duplicate_decls has issued all appropriate
2200 diagnostics). In particular, we should not consider possible
2201 duplicates in the external scope, or shadowing. */
2202 b = I_SYMBOL_BINDING (name);
2203 if (b && B_IN_SCOPE (b, scope))
2204 {
2205 struct c_binding *b_ext, *b_use;
2206 tree type = TREE_TYPE (x);
2207 tree visdecl = b->decl;
2208 tree vistype = TREE_TYPE (visdecl);
2209 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2210 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2211 b->inner_comp = false;
2212 b_use = b;
2213 b_ext = b;
2214 /* If this is an external linkage declaration, we should check
2215 for compatibility with the type in the external scope before
2216 setting the type at this scope based on the visible
2217 information only. */
2218 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2219 {
2220 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2221 b_ext = b_ext->shadowed;
2222 if (b_ext)
2223 {
2224 b_use = b_ext;
2225 if (b_use->type)
2226 TREE_TYPE (b_use->decl) = b_use->type;
2227 }
2228 }
2229 if (duplicate_decls (x, b_use->decl))
2230 {
2231 if (b_use != b)
2232 {
2233 /* Save the updated type in the external scope and
2234 restore the proper type for this scope. */
2235 tree thistype;
2236 if (comptypes (vistype, type))
2237 thistype = composite_type (vistype, type);
2238 else
2239 thistype = TREE_TYPE (b_use->decl);
2240 b_use->type = TREE_TYPE (b_use->decl);
2241 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2242 && DECL_BUILT_IN (b_use->decl))
2243 thistype
2244 = build_type_attribute_variant (thistype,
2245 TYPE_ATTRIBUTES
2246 (b_use->type));
2247 TREE_TYPE (b_use->decl) = thistype;
2248 }
2249 return b_use->decl;
2250 }
2251 else
2252 goto skip_external_and_shadow_checks;
2253 }
2254
2255 /* All declarations with external linkage, and all external
2256 references, go in the external scope, no matter what scope is
2257 current. However, the binding in that scope is ignored for
2258 purposes of normal name lookup. A separate binding structure is
2259 created in the requested scope; this governs the normal
2260 visibility of the symbol.
2261
2262 The binding in the externals scope is used exclusively for
2263 detecting duplicate declarations of the same object, no matter
2264 what scope they are in; this is what we do here. (C99 6.2.7p2:
2265 All declarations that refer to the same object or function shall
2266 have compatible type; otherwise, the behavior is undefined.) */
2267 if (DECL_EXTERNAL (x) || scope == file_scope)
2268 {
2269 tree type = TREE_TYPE (x);
2270 tree vistype = 0;
2271 tree visdecl = 0;
2272 bool type_saved = false;
2273 if (b && !B_IN_EXTERNAL_SCOPE (b)
2274 && (TREE_CODE (b->decl) == FUNCTION_DECL
2275 || TREE_CODE (b->decl) == VAR_DECL)
2276 && DECL_FILE_SCOPE_P (b->decl))
2277 {
2278 visdecl = b->decl;
2279 vistype = TREE_TYPE (visdecl);
2280 }
2281 if (scope != file_scope
2282 && !DECL_IN_SYSTEM_HEADER (x))
2283 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2284
2285 while (b && !B_IN_EXTERNAL_SCOPE (b))
2286 {
2287 /* If this decl might be modified, save its type. This is
2288 done here rather than when the decl is first bound
2289 because the type may change after first binding, through
2290 being completed or through attributes being added. If we
2291 encounter multiple such decls, only the first should have
2292 its type saved; the others will already have had their
2293 proper types saved and the types will not have changed as
2294 their scopes will not have been re-entered. */
2295 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2296 {
2297 b->type = TREE_TYPE (b->decl);
2298 type_saved = true;
2299 }
2300 if (B_IN_FILE_SCOPE (b)
2301 && TREE_CODE (b->decl) == VAR_DECL
2302 && TREE_STATIC (b->decl)
2303 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2304 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2305 && TREE_CODE (type) == ARRAY_TYPE
2306 && TYPE_DOMAIN (type)
2307 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2308 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2309 {
2310 /* Array type completed in inner scope, which should be
2311 diagnosed if the completion does not have size 1 and
2312 it does not get completed in the file scope. */
2313 b->inner_comp = true;
2314 }
2315 b = b->shadowed;
2316 }
2317
2318 /* If a matching external declaration has been found, set its
2319 type to the composite of all the types of that declaration.
2320 After the consistency checks, it will be reset to the
2321 composite of the visible types only. */
2322 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2323 && b->type)
2324 TREE_TYPE (b->decl) = b->type;
2325
2326 /* The point of the same_translation_unit_p check here is,
2327 we want to detect a duplicate decl for a construct like
2328 foo() { extern bar(); } ... static bar(); but not if
2329 they are in different translation units. In any case,
2330 the static does not go in the externals scope. */
2331 if (b
2332 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2333 && duplicate_decls (x, b->decl))
2334 {
2335 tree thistype;
2336 if (vistype)
2337 {
2338 if (comptypes (vistype, type))
2339 thistype = composite_type (vistype, type);
2340 else
2341 thistype = TREE_TYPE (b->decl);
2342 }
2343 else
2344 thistype = type;
2345 b->type = TREE_TYPE (b->decl);
2346 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2347 thistype
2348 = build_type_attribute_variant (thistype,
2349 TYPE_ATTRIBUTES (b->type));
2350 TREE_TYPE (b->decl) = thistype;
2351 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2352 locus);
2353 return b->decl;
2354 }
2355 else if (TREE_PUBLIC (x))
2356 {
2357 if (visdecl && !b && duplicate_decls (x, visdecl))
2358 {
2359 /* An external declaration at block scope referring to a
2360 visible entity with internal linkage. The composite
2361 type will already be correct for this scope, so we
2362 just need to fall through to make the declaration in
2363 this scope. */
2364 nested = true;
2365 x = visdecl;
2366 }
2367 else
2368 {
2369 bind (name, x, external_scope, /*invisible=*/true,
2370 /*nested=*/false, locus);
2371 nested = true;
2372 }
2373 }
2374 }
2375
2376 if (TREE_CODE (x) != PARM_DECL)
2377 warn_if_shadowing (x);
2378
2379 skip_external_and_shadow_checks:
2380 if (TREE_CODE (x) == TYPE_DECL)
2381 set_underlying_type (x);
2382
2383 bind (name, x, scope, /*invisible=*/false, nested, locus);
2384
2385 /* If x's type is incomplete because it's based on a
2386 structure or union which has not yet been fully declared,
2387 attach it to that structure or union type, so we can go
2388 back and complete the variable declaration later, if the
2389 structure or union gets fully declared.
2390
2391 If the input is erroneous, we can have error_mark in the type
2392 slot (e.g. "f(void a, ...)") - that doesn't count as an
2393 incomplete type. */
2394 if (TREE_TYPE (x) != error_mark_node
2395 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2396 {
2397 tree element = TREE_TYPE (x);
2398
2399 while (TREE_CODE (element) == ARRAY_TYPE)
2400 element = TREE_TYPE (element);
2401 element = TYPE_MAIN_VARIANT (element);
2402
2403 if ((TREE_CODE (element) == RECORD_TYPE
2404 || TREE_CODE (element) == UNION_TYPE)
2405 && (TREE_CODE (x) != TYPE_DECL
2406 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2407 && !COMPLETE_TYPE_P (element))
2408 C_TYPE_INCOMPLETE_VARS (element)
2409 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2410 }
2411 return x;
2412 }
2413
2414 /* Record X as belonging to file scope.
2415 This is used only internally by the Objective-C front end,
2416 and is limited to its needs. duplicate_decls is not called;
2417 if there is any preexisting decl for this identifier, it is an ICE. */
2418
2419 tree
2420 pushdecl_top_level (tree x)
2421 {
2422 tree name;
2423 bool nested = false;
2424 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2425
2426 name = DECL_NAME (x);
2427
2428 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2429
2430 if (TREE_PUBLIC (x))
2431 {
2432 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2433 UNKNOWN_LOCATION);
2434 nested = true;
2435 }
2436 if (file_scope)
2437 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2438
2439 return x;
2440 }
2441 \f
2442 static void
2443 implicit_decl_warning (tree id, tree olddecl)
2444 {
2445 if (warn_implicit_function_declaration)
2446 {
2447 bool warned;
2448
2449 if (flag_isoc99)
2450 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2451 "implicit declaration of function %qE", id);
2452 else
2453 warned = warning (OPT_Wimplicit_function_declaration,
2454 G_("implicit declaration of function %qE"), id);
2455 if (olddecl && warned)
2456 locate_old_decl (olddecl);
2457 }
2458 }
2459
2460 /* Generate an implicit declaration for identifier FUNCTIONID as a
2461 function of type int (). */
2462
2463 tree
2464 implicitly_declare (tree functionid)
2465 {
2466 struct c_binding *b;
2467 tree decl = 0;
2468 tree asmspec_tree;
2469
2470 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2471 {
2472 if (B_IN_SCOPE (b, external_scope))
2473 {
2474 decl = b->decl;
2475 break;
2476 }
2477 }
2478
2479 if (decl)
2480 {
2481 if (decl == error_mark_node)
2482 return decl;
2483
2484 /* FIXME: Objective-C has weird not-really-builtin functions
2485 which are supposed to be visible automatically. They wind up
2486 in the external scope because they're pushed before the file
2487 scope gets created. Catch this here and rebind them into the
2488 file scope. */
2489 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2490 {
2491 bind (functionid, decl, file_scope,
2492 /*invisible=*/false, /*nested=*/true,
2493 DECL_SOURCE_LOCATION (decl));
2494 return decl;
2495 }
2496 else
2497 {
2498 tree newtype = default_function_type;
2499 if (b->type)
2500 TREE_TYPE (decl) = b->type;
2501 /* Implicit declaration of a function already declared
2502 (somehow) in a different scope, or as a built-in.
2503 If this is the first time this has happened, warn;
2504 then recycle the old declaration but with the new type. */
2505 if (!C_DECL_IMPLICIT (decl))
2506 {
2507 implicit_decl_warning (functionid, decl);
2508 C_DECL_IMPLICIT (decl) = 1;
2509 }
2510 if (DECL_BUILT_IN (decl))
2511 {
2512 newtype = build_type_attribute_variant (newtype,
2513 TYPE_ATTRIBUTES
2514 (TREE_TYPE (decl)));
2515 if (!comptypes (newtype, TREE_TYPE (decl)))
2516 {
2517 warning (0, "incompatible implicit declaration of built-in"
2518 " function %qD", decl);
2519 newtype = TREE_TYPE (decl);
2520 }
2521 }
2522 else
2523 {
2524 if (!comptypes (newtype, TREE_TYPE (decl)))
2525 {
2526 error ("incompatible implicit declaration of function %qD",
2527 decl);
2528 locate_old_decl (decl);
2529 }
2530 }
2531 b->type = TREE_TYPE (decl);
2532 TREE_TYPE (decl) = newtype;
2533 bind (functionid, decl, current_scope,
2534 /*invisible=*/false, /*nested=*/true,
2535 DECL_SOURCE_LOCATION (decl));
2536 return decl;
2537 }
2538 }
2539
2540 /* Not seen before. */
2541 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2542 DECL_EXTERNAL (decl) = 1;
2543 TREE_PUBLIC (decl) = 1;
2544 C_DECL_IMPLICIT (decl) = 1;
2545 implicit_decl_warning (functionid, 0);
2546 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2547 if (asmspec_tree)
2548 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2549
2550 /* C89 says implicit declarations are in the innermost block.
2551 So we record the decl in the standard fashion. */
2552 decl = pushdecl (decl);
2553
2554 /* No need to call objc_check_decl here - it's a function type. */
2555 rest_of_decl_compilation (decl, 0, 0);
2556
2557 /* Write a record describing this implicit function declaration
2558 to the prototypes file (if requested). */
2559 gen_aux_info_record (decl, 0, 1, 0);
2560
2561 /* Possibly apply some default attributes to this implicit declaration. */
2562 decl_attributes (&decl, NULL_TREE, 0);
2563
2564 return decl;
2565 }
2566
2567 /* Issue an error message for a reference to an undeclared variable
2568 ID, including a reference to a builtin outside of function-call
2569 context. Establish a binding of the identifier to error_mark_node
2570 in an appropriate scope, which will suppress further errors for the
2571 same identifier. The error message should be given location LOC. */
2572 void
2573 undeclared_variable (tree id, location_t loc)
2574 {
2575 static bool already = false;
2576 struct c_scope *scope;
2577
2578 if (current_function_decl == 0)
2579 {
2580 error ("%H%qE undeclared here (not in a function)", &loc, id);
2581 scope = current_scope;
2582 }
2583 else
2584 {
2585 error ("%H%qE undeclared (first use in this function)", &loc, id);
2586
2587 if (!already)
2588 {
2589 error ("%H(Each undeclared identifier is reported only once", &loc);
2590 error ("%Hfor each function it appears in.)", &loc);
2591 already = true;
2592 }
2593
2594 /* If we are parsing old-style parameter decls, current_function_decl
2595 will be nonnull but current_function_scope will be null. */
2596 scope = current_function_scope ? current_function_scope : current_scope;
2597 }
2598 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
2599 UNKNOWN_LOCATION);
2600 }
2601 \f
2602 /* Subroutine of lookup_label, declare_label, define_label: construct a
2603 LABEL_DECL with all the proper frills. */
2604
2605 static tree
2606 make_label (tree name, location_t location)
2607 {
2608 tree label = build_decl (LABEL_DECL, name, void_type_node);
2609
2610 DECL_CONTEXT (label) = current_function_decl;
2611 DECL_MODE (label) = VOIDmode;
2612 DECL_SOURCE_LOCATION (label) = location;
2613
2614 return label;
2615 }
2616
2617 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2618 Create one if none exists so far for the current function.
2619 This is called when a label is used in a goto expression or
2620 has its address taken. */
2621
2622 tree
2623 lookup_label (tree name)
2624 {
2625 tree label;
2626
2627 if (current_function_decl == 0)
2628 {
2629 error ("label %qE referenced outside of any function", name);
2630 return 0;
2631 }
2632
2633 /* Use a label already defined or ref'd with this name, but not if
2634 it is inherited from a containing function and wasn't declared
2635 using __label__. */
2636 label = I_LABEL_DECL (name);
2637 if (label && (DECL_CONTEXT (label) == current_function_decl
2638 || C_DECLARED_LABEL_FLAG (label)))
2639 {
2640 /* If the label has only been declared, update its apparent
2641 location to point here, for better diagnostics if it
2642 turns out not to have been defined. */
2643 if (!TREE_USED (label))
2644 DECL_SOURCE_LOCATION (label) = input_location;
2645 return label;
2646 }
2647
2648 /* No label binding for that identifier; make one. */
2649 label = make_label (name, input_location);
2650
2651 /* Ordinary labels go in the current function scope. */
2652 bind (name, label, current_function_scope,
2653 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2654 return label;
2655 }
2656
2657 /* Make a label named NAME in the current function, shadowing silently
2658 any that may be inherited from containing functions or containing
2659 scopes. This is called for __label__ declarations. */
2660
2661 tree
2662 declare_label (tree name)
2663 {
2664 struct c_binding *b = I_LABEL_BINDING (name);
2665 tree label;
2666
2667 /* Check to make sure that the label hasn't already been declared
2668 at this scope */
2669 if (b && B_IN_CURRENT_SCOPE (b))
2670 {
2671 error ("duplicate label declaration %qE", name);
2672 locate_old_decl (b->decl);
2673
2674 /* Just use the previous declaration. */
2675 return b->decl;
2676 }
2677
2678 label = make_label (name, input_location);
2679 C_DECLARED_LABEL_FLAG (label) = 1;
2680
2681 /* Declared labels go in the current scope. */
2682 bind (name, label, current_scope,
2683 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2684 return label;
2685 }
2686
2687 /* Define a label, specifying the location in the source file.
2688 Return the LABEL_DECL node for the label, if the definition is valid.
2689 Otherwise return 0. */
2690
2691 tree
2692 define_label (location_t location, tree name)
2693 {
2694 /* Find any preexisting label with this name. It is an error
2695 if that label has already been defined in this function, or
2696 if there is a containing function with a declared label with
2697 the same name. */
2698 tree label = I_LABEL_DECL (name);
2699 struct c_label_list *nlist_se, *nlist_vm;
2700
2701 if (label
2702 && ((DECL_CONTEXT (label) == current_function_decl
2703 && DECL_INITIAL (label) != 0)
2704 || (DECL_CONTEXT (label) != current_function_decl
2705 && C_DECLARED_LABEL_FLAG (label))))
2706 {
2707 error ("%Hduplicate label %qD", &location, label);
2708 locate_old_decl (label);
2709 return 0;
2710 }
2711 else if (label && DECL_CONTEXT (label) == current_function_decl)
2712 {
2713 /* The label has been used or declared already in this function,
2714 but not defined. Update its location to point to this
2715 definition. */
2716 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2717 error ("%Jjump into statement expression", label);
2718 if (C_DECL_UNDEFINABLE_VM (label))
2719 error ("%Jjump into scope of identifier with variably modified type",
2720 label);
2721 DECL_SOURCE_LOCATION (label) = location;
2722 }
2723 else
2724 {
2725 /* No label binding for that identifier; make one. */
2726 label = make_label (name, location);
2727
2728 /* Ordinary labels go in the current function scope. */
2729 bind (name, label, current_function_scope,
2730 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2731 }
2732
2733 if (!in_system_header && lookup_name (name))
2734 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2735 "for labels, identifier %qE conflicts", &location, name);
2736
2737 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2738 nlist_se->next = label_context_stack_se->labels_def;
2739 nlist_se->label = label;
2740 label_context_stack_se->labels_def = nlist_se;
2741
2742 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2743 nlist_vm->next = label_context_stack_vm->labels_def;
2744 nlist_vm->label = label;
2745 label_context_stack_vm->labels_def = nlist_vm;
2746
2747 /* Mark label as having been defined. */
2748 DECL_INITIAL (label) = error_mark_node;
2749 return label;
2750 }
2751 \f
2752 /* Given NAME, an IDENTIFIER_NODE,
2753 return the structure (or union or enum) definition for that name.
2754 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2755 CODE says which kind of type the caller wants;
2756 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2757 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
2758 location where the tag was defined.
2759 If the wrong kind of type is found, an error is reported. */
2760
2761 static tree
2762 lookup_tag (enum tree_code code, tree name, int thislevel_only,
2763 location_t *ploc)
2764 {
2765 struct c_binding *b = I_TAG_BINDING (name);
2766 int thislevel = 0;
2767
2768 if (!b || !b->decl)
2769 return 0;
2770
2771 /* We only care about whether it's in this level if
2772 thislevel_only was set or it might be a type clash. */
2773 if (thislevel_only || TREE_CODE (b->decl) != code)
2774 {
2775 /* For our purposes, a tag in the external scope is the same as
2776 a tag in the file scope. (Primarily relevant to Objective-C
2777 and its builtin structure tags, which get pushed before the
2778 file scope is created.) */
2779 if (B_IN_CURRENT_SCOPE (b)
2780 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2781 thislevel = 1;
2782 }
2783
2784 if (thislevel_only && !thislevel)
2785 return 0;
2786
2787 if (TREE_CODE (b->decl) != code)
2788 {
2789 /* Definition isn't the kind we were looking for. */
2790 pending_invalid_xref = name;
2791 pending_invalid_xref_location = input_location;
2792
2793 /* If in the same binding level as a declaration as a tag
2794 of a different type, this must not be allowed to
2795 shadow that tag, so give the error immediately.
2796 (For example, "struct foo; union foo;" is invalid.) */
2797 if (thislevel)
2798 pending_xref_error ();
2799 }
2800
2801 if (ploc != NULL)
2802 *ploc = b->locus;
2803
2804 return b->decl;
2805 }
2806
2807 /* Print an error message now
2808 for a recent invalid struct, union or enum cross reference.
2809 We don't print them immediately because they are not invalid
2810 when used in the `struct foo;' construct for shadowing. */
2811
2812 void
2813 pending_xref_error (void)
2814 {
2815 if (pending_invalid_xref != 0)
2816 error ("%H%qE defined as wrong kind of tag",
2817 &pending_invalid_xref_location, pending_invalid_xref);
2818 pending_invalid_xref = 0;
2819 }
2820
2821 \f
2822 /* Look up NAME in the current scope and its superiors
2823 in the namespace of variables, functions and typedefs.
2824 Return a ..._DECL node of some kind representing its definition,
2825 or return 0 if it is undefined. */
2826
2827 tree
2828 lookup_name (tree name)
2829 {
2830 struct c_binding *b = I_SYMBOL_BINDING (name);
2831 if (b && !b->invisible)
2832 return b->decl;
2833 return 0;
2834 }
2835
2836 /* Similar to `lookup_name' but look only at the indicated scope. */
2837
2838 static tree
2839 lookup_name_in_scope (tree name, struct c_scope *scope)
2840 {
2841 struct c_binding *b;
2842
2843 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2844 if (B_IN_SCOPE (b, scope))
2845 return b->decl;
2846 return 0;
2847 }
2848 \f
2849 /* Create the predefined scalar types of C,
2850 and some nodes representing standard constants (0, 1, (void *) 0).
2851 Initialize the global scope.
2852 Make definitions for built-in primitive functions. */
2853
2854 void
2855 c_init_decl_processing (void)
2856 {
2857 location_t save_loc = input_location;
2858
2859 /* Initialize reserved words for parser. */
2860 c_parse_init ();
2861
2862 current_function_decl = 0;
2863
2864 gcc_obstack_init (&parser_obstack);
2865
2866 /* Make the externals scope. */
2867 push_scope ();
2868 external_scope = current_scope;
2869
2870 /* Declarations from c_common_nodes_and_builtins must not be associated
2871 with this input file, lest we get differences between using and not
2872 using preprocessed headers. */
2873 input_location = BUILTINS_LOCATION;
2874
2875 build_common_tree_nodes (flag_signed_char, false);
2876
2877 c_common_nodes_and_builtins ();
2878
2879 /* In C, comparisons and TRUTH_* expressions have type int. */
2880 truthvalue_type_node = integer_type_node;
2881 truthvalue_true_node = integer_one_node;
2882 truthvalue_false_node = integer_zero_node;
2883
2884 /* Even in C99, which has a real boolean type. */
2885 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2886 boolean_type_node));
2887
2888 input_location = save_loc;
2889
2890 pedantic_lvalues = true;
2891
2892 make_fname_decl = c_make_fname_decl;
2893 start_fname_decls ();
2894 }
2895
2896 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2897 decl, NAME is the initialization string and TYPE_DEP indicates whether
2898 NAME depended on the type of the function. As we don't yet implement
2899 delayed emission of static data, we mark the decl as emitted
2900 so it is not placed in the output. Anything using it must therefore pull
2901 out the STRING_CST initializer directly. FIXME. */
2902
2903 static tree
2904 c_make_fname_decl (tree id, int type_dep)
2905 {
2906 const char *name = fname_as_string (type_dep);
2907 tree decl, type, init;
2908 size_t length = strlen (name);
2909
2910 type = build_array_type (char_type_node,
2911 build_index_type (size_int (length)));
2912 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2913
2914 decl = build_decl (VAR_DECL, id, type);
2915
2916 TREE_STATIC (decl) = 1;
2917 TREE_READONLY (decl) = 1;
2918 DECL_ARTIFICIAL (decl) = 1;
2919
2920 init = build_string (length + 1, name);
2921 free (CONST_CAST (char *, name));
2922 TREE_TYPE (init) = type;
2923 DECL_INITIAL (decl) = init;
2924
2925 TREE_USED (decl) = 1;
2926
2927 if (current_function_decl
2928 /* For invalid programs like this:
2929
2930 void foo()
2931 const char* p = __FUNCTION__;
2932
2933 the __FUNCTION__ is believed to appear in K&R style function
2934 parameter declarator. In that case we still don't have
2935 function_scope. */
2936 && (!errorcount || current_function_scope))
2937 {
2938 DECL_CONTEXT (decl) = current_function_decl;
2939 bind (id, decl, current_function_scope,
2940 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2941 }
2942
2943 finish_decl (decl, init, NULL_TREE, NULL_TREE);
2944
2945 return decl;
2946 }
2947
2948 tree
2949 c_builtin_function (tree decl)
2950 {
2951 tree type = TREE_TYPE (decl);
2952 tree id = DECL_NAME (decl);
2953
2954 const char *name = IDENTIFIER_POINTER (id);
2955 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2956
2957 /* Should never be called on a symbol with a preexisting meaning. */
2958 gcc_assert (!I_SYMBOL_BINDING (id));
2959
2960 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
2961 UNKNOWN_LOCATION);
2962
2963 /* Builtins in the implementation namespace are made visible without
2964 needing to be explicitly declared. See push_file_scope. */
2965 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2966 {
2967 TREE_CHAIN (decl) = visible_builtins;
2968 visible_builtins = decl;
2969 }
2970
2971 return decl;
2972 }
2973
2974 tree
2975 c_builtin_function_ext_scope (tree decl)
2976 {
2977 tree type = TREE_TYPE (decl);
2978 tree id = DECL_NAME (decl);
2979
2980 const char *name = IDENTIFIER_POINTER (id);
2981 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2982
2983 /* Should never be called on a symbol with a preexisting meaning. */
2984 gcc_assert (!I_SYMBOL_BINDING (id));
2985
2986 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
2987 UNKNOWN_LOCATION);
2988
2989 /* Builtins in the implementation namespace are made visible without
2990 needing to be explicitly declared. See push_file_scope. */
2991 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2992 {
2993 TREE_CHAIN (decl) = visible_builtins;
2994 visible_builtins = decl;
2995 }
2996
2997 return decl;
2998 }
2999 \f
3000 /* Called when a declaration is seen that contains no names to declare.
3001 If its type is a reference to a structure, union or enum inherited
3002 from a containing scope, shadow that tag name for the current scope
3003 with a forward reference.
3004 If its type defines a new named structure or union
3005 or defines an enum, it is valid but we need not do anything here.
3006 Otherwise, it is an error. */
3007
3008 void
3009 shadow_tag (const struct c_declspecs *declspecs)
3010 {
3011 shadow_tag_warned (declspecs, 0);
3012 }
3013
3014 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3015 but no pedwarn. */
3016 void
3017 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
3018 {
3019 bool found_tag = false;
3020
3021 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
3022 {
3023 tree value = declspecs->type;
3024 enum tree_code code = TREE_CODE (value);
3025
3026 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3027 /* Used to test also that TYPE_SIZE (value) != 0.
3028 That caused warning for `struct foo;' at top level in the file. */
3029 {
3030 tree name = TYPE_NAME (value);
3031 tree t;
3032
3033 found_tag = true;
3034
3035 if (declspecs->restrict_p)
3036 {
3037 error ("invalid use of %<restrict%>");
3038 warned = 1;
3039 }
3040
3041 if (name == 0)
3042 {
3043 if (warned != 1 && code != ENUMERAL_TYPE)
3044 /* Empty unnamed enum OK */
3045 {
3046 pedwarn (input_location, 0,
3047 "unnamed struct/union that defines no instances");
3048 warned = 1;
3049 }
3050 }
3051 else if (!declspecs->tag_defined_p
3052 && declspecs->storage_class != csc_none)
3053 {
3054 if (warned != 1)
3055 pedwarn (input_location, 0,
3056 "empty declaration with storage class specifier "
3057 "does not redeclare tag");
3058 warned = 1;
3059 pending_xref_error ();
3060 }
3061 else if (!declspecs->tag_defined_p
3062 && (declspecs->const_p
3063 || declspecs->volatile_p
3064 || declspecs->restrict_p))
3065 {
3066 if (warned != 1)
3067 pedwarn (input_location, 0,
3068 "empty declaration with type qualifier "
3069 "does not redeclare tag");
3070 warned = 1;
3071 pending_xref_error ();
3072 }
3073 else
3074 {
3075 pending_invalid_xref = 0;
3076 t = lookup_tag (code, name, 1, NULL);
3077
3078 if (t == 0)
3079 {
3080 t = make_node (code);
3081 pushtag (name, t, input_location);
3082 }
3083 }
3084 }
3085 else
3086 {
3087 if (warned != 1 && !in_system_header)
3088 {
3089 pedwarn (input_location, 0,
3090 "useless type name in empty declaration");
3091 warned = 1;
3092 }
3093 }
3094 }
3095 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3096 {
3097 pedwarn (input_location, 0, "useless type name in empty declaration");
3098 warned = 1;
3099 }
3100
3101 pending_invalid_xref = 0;
3102
3103 if (declspecs->inline_p)
3104 {
3105 error ("%<inline%> in empty declaration");
3106 warned = 1;
3107 }
3108
3109 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3110 {
3111 error ("%<auto%> in file-scope empty declaration");
3112 warned = 1;
3113 }
3114
3115 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3116 {
3117 error ("%<register%> in file-scope empty declaration");
3118 warned = 1;
3119 }
3120
3121 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3122 {
3123 warning (0, "useless storage class specifier in empty declaration");
3124 warned = 2;
3125 }
3126
3127 if (!warned && !in_system_header && declspecs->thread_p)
3128 {
3129 warning (0, "useless %<__thread%> in empty declaration");
3130 warned = 2;
3131 }
3132
3133 if (!warned && !in_system_header && (declspecs->const_p
3134 || declspecs->volatile_p
3135 || declspecs->restrict_p))
3136 {
3137 warning (0, "useless type qualifier in empty declaration");
3138 warned = 2;
3139 }
3140
3141 if (warned != 1)
3142 {
3143 if (!found_tag)
3144 pedwarn (input_location, 0, "empty declaration");
3145 }
3146 }
3147 \f
3148
3149 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3150 bits. SPECS represents declaration specifiers that the grammar
3151 only permits to contain type qualifiers and attributes. */
3152
3153 int
3154 quals_from_declspecs (const struct c_declspecs *specs)
3155 {
3156 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3157 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3158 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3159 gcc_assert (!specs->type
3160 && !specs->decl_attr
3161 && specs->typespec_word == cts_none
3162 && specs->storage_class == csc_none
3163 && !specs->typedef_p
3164 && !specs->explicit_signed_p
3165 && !specs->deprecated_p
3166 && !specs->long_p
3167 && !specs->long_long_p
3168 && !specs->short_p
3169 && !specs->signed_p
3170 && !specs->unsigned_p
3171 && !specs->complex_p
3172 && !specs->inline_p
3173 && !specs->thread_p);
3174 return quals;
3175 }
3176
3177 /* Construct an array declarator. EXPR is the expression inside [],
3178 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3179 applied to the pointer to which a parameter array is converted).
3180 STATIC_P is true if "static" is inside the [], false otherwise.
3181 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3182 length which is nevertheless a complete type, false otherwise. The
3183 field for the contained declarator is left to be filled in by
3184 set_array_declarator_inner. */
3185
3186 struct c_declarator *
3187 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3188 bool vla_unspec_p)
3189 {
3190 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3191 struct c_declarator);
3192 declarator->kind = cdk_array;
3193 declarator->declarator = 0;
3194 declarator->u.array.dimen = expr;
3195 if (quals)
3196 {
3197 declarator->u.array.attrs = quals->attrs;
3198 declarator->u.array.quals = quals_from_declspecs (quals);
3199 }
3200 else
3201 {
3202 declarator->u.array.attrs = NULL_TREE;
3203 declarator->u.array.quals = 0;
3204 }
3205 declarator->u.array.static_p = static_p;
3206 declarator->u.array.vla_unspec_p = vla_unspec_p;
3207 if (!flag_isoc99)
3208 {
3209 if (static_p || quals != NULL)
3210 pedwarn (input_location, OPT_pedantic,
3211 "ISO C90 does not support %<static%> or type "
3212 "qualifiers in parameter array declarators");
3213 if (vla_unspec_p)
3214 pedwarn (input_location, OPT_pedantic,
3215 "ISO C90 does not support %<[*]%> array declarators");
3216 }
3217 if (vla_unspec_p)
3218 {
3219 if (!current_scope->parm_flag)
3220 {
3221 /* C99 6.7.5.2p4 */
3222 error ("%<[*]%> not allowed in other than function prototype scope");
3223 declarator->u.array.vla_unspec_p = false;
3224 return NULL;
3225 }
3226 current_scope->had_vla_unspec = true;
3227 }
3228 return declarator;
3229 }
3230
3231 /* Set the contained declarator of an array declarator. DECL is the
3232 declarator, as constructed by build_array_declarator; INNER is what
3233 appears on the left of the []. */
3234
3235 struct c_declarator *
3236 set_array_declarator_inner (struct c_declarator *decl,
3237 struct c_declarator *inner)
3238 {
3239 decl->declarator = inner;
3240 return decl;
3241 }
3242
3243 /* INIT is a constructor that forms DECL's initializer. If the final
3244 element initializes a flexible array field, add the size of that
3245 initializer to DECL's size. */
3246
3247 static void
3248 add_flexible_array_elts_to_size (tree decl, tree init)
3249 {
3250 tree elt, type;
3251
3252 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3253 return;
3254
3255 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3256 type = TREE_TYPE (elt);
3257 if (TREE_CODE (type) == ARRAY_TYPE
3258 && TYPE_SIZE (type) == NULL_TREE
3259 && TYPE_DOMAIN (type) != NULL_TREE
3260 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3261 {
3262 complete_array_type (&type, elt, false);
3263 DECL_SIZE (decl)
3264 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3265 DECL_SIZE_UNIT (decl)
3266 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3267 }
3268 }
3269 \f
3270 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3271 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3272 before the type name, and set *EXPR_CONST_OPERANDS, if
3273 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3274 appear in a constant expression. */
3275
3276 tree
3277 groktypename (struct c_type_name *type_name, tree *expr,
3278 bool *expr_const_operands)
3279 {
3280 tree type;
3281 tree attrs = type_name->specs->attrs;
3282
3283 type_name->specs->attrs = NULL_TREE;
3284
3285 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3286 false, NULL, &attrs, expr, expr_const_operands,
3287 DEPRECATED_NORMAL);
3288
3289 /* Apply attributes. */
3290 decl_attributes (&type, attrs, 0);
3291
3292 return type;
3293 }
3294
3295 /* Decode a declarator in an ordinary declaration or data definition.
3296 This is called as soon as the type information and variable name
3297 have been parsed, before parsing the initializer if any.
3298 Here we create the ..._DECL node, fill in its type,
3299 and put it on the list of decls for the current context.
3300 The ..._DECL node is returned as the value.
3301
3302 Exception: for arrays where the length is not specified,
3303 the type is left null, to be filled in by `finish_decl'.
3304
3305 Function definitions do not come here; they go to start_function
3306 instead. However, external and forward declarations of functions
3307 do go through here. Structure field declarations are done by
3308 grokfield and not through here. */
3309
3310 tree
3311 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3312 bool initialized, tree attributes)
3313 {
3314 tree decl;
3315 tree tem;
3316 tree expr = NULL_TREE;
3317 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3318
3319 /* An object declared as __attribute__((deprecated)) suppresses
3320 warnings of uses of other deprecated items. */
3321 if (lookup_attribute ("deprecated", attributes))
3322 deprecated_state = DEPRECATED_SUPPRESS;
3323
3324 decl = grokdeclarator (declarator, declspecs,
3325 NORMAL, initialized, NULL, &attributes, &expr, NULL,
3326 deprecated_state);
3327 if (!decl)
3328 return 0;
3329
3330 if (expr)
3331 add_stmt (expr);
3332
3333 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
3334 warning (OPT_Wmain, "%q+D is usually a function", decl);
3335
3336 if (initialized)
3337 /* Is it valid for this decl to have an initializer at all?
3338 If not, set INITIALIZED to zero, which will indirectly
3339 tell 'finish_decl' to ignore the initializer once it is parsed. */
3340 switch (TREE_CODE (decl))
3341 {
3342 case TYPE_DECL:
3343 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3344 initialized = 0;
3345 break;
3346
3347 case FUNCTION_DECL:
3348 error ("function %qD is initialized like a variable", decl);
3349 initialized = 0;
3350 break;
3351
3352 case PARM_DECL:
3353 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3354 error ("parameter %qD is initialized", decl);
3355 initialized = 0;
3356 break;
3357
3358 default:
3359 /* Don't allow initializations for incomplete types except for
3360 arrays which might be completed by the initialization. */
3361
3362 /* This can happen if the array size is an undefined macro.
3363 We already gave a warning, so we don't need another one. */
3364 if (TREE_TYPE (decl) == error_mark_node)
3365 initialized = 0;
3366 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3367 {
3368 /* A complete type is ok if size is fixed. */
3369
3370 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3371 || C_DECL_VARIABLE_SIZE (decl))
3372 {
3373 error ("variable-sized object may not be initialized");
3374 initialized = 0;
3375 }
3376 }
3377 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3378 {
3379 error ("variable %qD has initializer but incomplete type", decl);
3380 initialized = 0;
3381 }
3382 else if (C_DECL_VARIABLE_SIZE (decl))
3383 {
3384 /* Although C99 is unclear about whether incomplete arrays
3385 of VLAs themselves count as VLAs, it does not make
3386 sense to permit them to be initialized given that
3387 ordinary VLAs may not be initialized. */
3388 error ("variable-sized object may not be initialized");
3389 initialized = 0;
3390 }
3391 }
3392
3393 if (initialized)
3394 {
3395 if (current_scope == file_scope)
3396 TREE_STATIC (decl) = 1;
3397
3398 /* Tell 'pushdecl' this is an initialized decl
3399 even though we don't yet have the initializer expression.
3400 Also tell 'finish_decl' it may store the real initializer. */
3401 DECL_INITIAL (decl) = error_mark_node;
3402 }
3403
3404 /* If this is a function declaration, write a record describing it to the
3405 prototypes file (if requested). */
3406
3407 if (TREE_CODE (decl) == FUNCTION_DECL)
3408 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3409
3410 /* ANSI specifies that a tentative definition which is not merged with
3411 a non-tentative definition behaves exactly like a definition with an
3412 initializer equal to zero. (Section 3.7.2)
3413
3414 -fno-common gives strict ANSI behavior, though this tends to break
3415 a large body of code that grew up without this rule.
3416
3417 Thread-local variables are never common, since there's no entrenched
3418 body of code to break, and it allows more efficient variable references
3419 in the presence of dynamic linking. */
3420
3421 if (TREE_CODE (decl) == VAR_DECL
3422 && !initialized
3423 && TREE_PUBLIC (decl)
3424 && !DECL_THREAD_LOCAL_P (decl)
3425 && !flag_no_common)
3426 DECL_COMMON (decl) = 1;
3427
3428 /* Set attributes here so if duplicate decl, will have proper attributes. */
3429 decl_attributes (&decl, attributes, 0);
3430
3431 /* Handle gnu_inline attribute. */
3432 if (declspecs->inline_p
3433 && !flag_gnu89_inline
3434 && TREE_CODE (decl) == FUNCTION_DECL
3435 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
3436 || current_function_decl))
3437 {
3438 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3439 ;
3440 else if (declspecs->storage_class != csc_static)
3441 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3442 }
3443
3444 if (TREE_CODE (decl) == FUNCTION_DECL
3445 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3446 {
3447 struct c_declarator *ce = declarator;
3448
3449 if (ce->kind == cdk_pointer)
3450 ce = declarator->declarator;
3451 if (ce->kind == cdk_function)
3452 {
3453 tree args = ce->u.arg_info->parms;
3454 for (; args; args = TREE_CHAIN (args))
3455 {
3456 tree type = TREE_TYPE (args);
3457 if (type && INTEGRAL_TYPE_P (type)
3458 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3459 DECL_ARG_TYPE (args) = integer_type_node;
3460 }
3461 }
3462 }
3463
3464 if (TREE_CODE (decl) == FUNCTION_DECL
3465 && DECL_DECLARED_INLINE_P (decl)
3466 && DECL_UNINLINABLE (decl)
3467 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3468 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3469 decl);
3470
3471 /* C99 6.7.4p3: An inline definition of a function with external
3472 linkage shall not contain a definition of a modifiable object
3473 with static storage duration... */
3474 if (TREE_CODE (decl) == VAR_DECL
3475 && current_scope != file_scope
3476 && TREE_STATIC (decl)
3477 && !TREE_READONLY (decl)
3478 && DECL_DECLARED_INLINE_P (current_function_decl)
3479 && DECL_EXTERNAL (current_function_decl))
3480 record_inline_static (input_location, current_function_decl,
3481 decl, csi_modifiable);
3482
3483 /* Add this decl to the current scope.
3484 TEM may equal DECL or it may be a previous decl of the same name. */
3485 tem = pushdecl (decl);
3486
3487 if (initialized && DECL_EXTERNAL (tem))
3488 {
3489 DECL_EXTERNAL (tem) = 0;
3490 TREE_STATIC (tem) = 1;
3491 }
3492
3493 return tem;
3494 }
3495
3496 /* Initialize EH if not initialized yet and exceptions are enabled. */
3497
3498 void
3499 c_maybe_initialize_eh (void)
3500 {
3501 if (!flag_exceptions || c_eh_initialized_p)
3502 return;
3503
3504 c_eh_initialized_p = true;
3505 eh_personality_libfunc
3506 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3507 ? "__gcc_personality_sj0"
3508 : "__gcc_personality_v0");
3509 default_init_unwind_resume_libfunc ();
3510 using_eh_for_cleanups ();
3511 }
3512
3513 /* Finish processing of a declaration;
3514 install its initial value.
3515 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
3516 If the length of an array type is not known before,
3517 it must be determined now, from the initial value, or it is an error. */
3518
3519 void
3520 finish_decl (tree decl, tree init, tree origtype, tree asmspec_tree)
3521 {
3522 tree type;
3523 int was_incomplete = (DECL_SIZE (decl) == 0);
3524 const char *asmspec = 0;
3525
3526 /* If a name was specified, get the string. */
3527 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3528 && DECL_FILE_SCOPE_P (decl))
3529 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3530 if (asmspec_tree)
3531 asmspec = TREE_STRING_POINTER (asmspec_tree);
3532
3533 /* If `start_decl' didn't like having an initialization, ignore it now. */
3534 if (init != 0 && DECL_INITIAL (decl) == 0)
3535 init = 0;
3536
3537 /* Don't crash if parm is initialized. */
3538 if (TREE_CODE (decl) == PARM_DECL)
3539 init = 0;
3540
3541 if (init)
3542 store_init_value (decl, init, origtype);
3543
3544 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3545 || TREE_CODE (decl) == FUNCTION_DECL
3546 || TREE_CODE (decl) == FIELD_DECL))
3547 objc_check_decl (decl);
3548
3549 type = TREE_TYPE (decl);
3550
3551 /* Deduce size of array from initialization, if not already known. */
3552 if (TREE_CODE (type) == ARRAY_TYPE
3553 && TYPE_DOMAIN (type) == 0
3554 && TREE_CODE (decl) != TYPE_DECL)
3555 {
3556 bool do_default
3557 = (TREE_STATIC (decl)
3558 /* Even if pedantic, an external linkage array
3559 may have incomplete type at first. */
3560 ? pedantic && !TREE_PUBLIC (decl)
3561 : !DECL_EXTERNAL (decl));
3562 int failure
3563 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3564 do_default);
3565
3566 /* Get the completed type made by complete_array_type. */
3567 type = TREE_TYPE (decl);
3568
3569 switch (failure)
3570 {
3571 case 1:
3572 error ("initializer fails to determine size of %q+D", decl);
3573 break;
3574
3575 case 2:
3576 if (do_default)
3577 error ("array size missing in %q+D", decl);
3578 /* If a `static' var's size isn't known,
3579 make it extern as well as static, so it does not get
3580 allocated.
3581 If it is not `static', then do not mark extern;
3582 finish_incomplete_decl will give it a default size
3583 and it will get allocated. */
3584 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3585 DECL_EXTERNAL (decl) = 1;
3586 break;
3587
3588 case 3:
3589 error ("zero or negative size array %q+D", decl);
3590 break;
3591
3592 case 0:
3593 /* For global variables, update the copy of the type that
3594 exists in the binding. */
3595 if (TREE_PUBLIC (decl))
3596 {
3597 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3598 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3599 b_ext = b_ext->shadowed;
3600 if (b_ext)
3601 {
3602 if (b_ext->type)
3603 b_ext->type = composite_type (b_ext->type, type);
3604 else
3605 b_ext->type = type;
3606 }
3607 }
3608 break;
3609
3610 default:
3611 gcc_unreachable ();
3612 }
3613
3614 if (DECL_INITIAL (decl))
3615 TREE_TYPE (DECL_INITIAL (decl)) = type;
3616
3617 layout_decl (decl, 0);
3618 }
3619
3620 if (TREE_CODE (decl) == VAR_DECL)
3621 {
3622 if (init && TREE_CODE (init) == CONSTRUCTOR)
3623 add_flexible_array_elts_to_size (decl, init);
3624
3625 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3626 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3627 layout_decl (decl, 0);
3628
3629 if (DECL_SIZE (decl) == 0
3630 /* Don't give an error if we already gave one earlier. */
3631 && TREE_TYPE (decl) != error_mark_node
3632 && (TREE_STATIC (decl)
3633 /* A static variable with an incomplete type
3634 is an error if it is initialized.
3635 Also if it is not file scope.
3636 Otherwise, let it through, but if it is not `extern'
3637 then it may cause an error message later. */
3638 ? (DECL_INITIAL (decl) != 0
3639 || !DECL_FILE_SCOPE_P (decl))
3640 /* An automatic variable with an incomplete type
3641 is an error. */
3642 : !DECL_EXTERNAL (decl)))
3643 {
3644 error ("storage size of %q+D isn%'t known", decl);
3645 TREE_TYPE (decl) = error_mark_node;
3646 }
3647
3648 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3649 && DECL_SIZE (decl) != 0)
3650 {
3651 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3652 constant_expression_warning (DECL_SIZE (decl));
3653 else
3654 {
3655 error ("storage size of %q+D isn%'t constant", decl);
3656 TREE_TYPE (decl) = error_mark_node;
3657 }
3658 }
3659
3660 if (TREE_USED (type))
3661 TREE_USED (decl) = 1;
3662 }
3663
3664 /* If this is a function and an assembler name is specified, reset DECL_RTL
3665 so we can give it its new name. Also, update built_in_decls if it
3666 was a normal built-in. */
3667 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3668 {
3669 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3670 set_builtin_user_assembler_name (decl, asmspec);
3671 set_user_assembler_name (decl, asmspec);
3672 }
3673
3674 /* If #pragma weak was used, mark the decl weak now. */
3675 maybe_apply_pragma_weak (decl);
3676
3677 /* Output the assembler code and/or RTL code for variables and functions,
3678 unless the type is an undefined structure or union.
3679 If not, it will get done when the type is completed. */
3680
3681 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3682 {
3683 /* Determine the ELF visibility. */
3684 if (TREE_PUBLIC (decl))
3685 c_determine_visibility (decl);
3686
3687 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3688 if (c_dialect_objc ())
3689 objc_check_decl (decl);
3690
3691 if (asmspec)
3692 {
3693 /* If this is not a static variable, issue a warning.
3694 It doesn't make any sense to give an ASMSPEC for an
3695 ordinary, non-register local variable. Historically,
3696 GCC has accepted -- but ignored -- the ASMSPEC in
3697 this case. */
3698 if (!DECL_FILE_SCOPE_P (decl)
3699 && TREE_CODE (decl) == VAR_DECL
3700 && !C_DECL_REGISTER (decl)
3701 && !TREE_STATIC (decl))
3702 warning (0, "ignoring asm-specifier for non-static local "
3703 "variable %q+D", decl);
3704 else
3705 set_user_assembler_name (decl, asmspec);
3706 }
3707
3708 if (DECL_FILE_SCOPE_P (decl))
3709 {
3710 if (DECL_INITIAL (decl) == NULL_TREE
3711 || DECL_INITIAL (decl) == error_mark_node)
3712 /* Don't output anything
3713 when a tentative file-scope definition is seen.
3714 But at end of compilation, do output code for them. */
3715 DECL_DEFER_OUTPUT (decl) = 1;
3716 rest_of_decl_compilation (decl, true, 0);
3717 }
3718 else
3719 {
3720 /* In conjunction with an ASMSPEC, the `register'
3721 keyword indicates that we should place the variable
3722 in a particular register. */
3723 if (asmspec && C_DECL_REGISTER (decl))
3724 {
3725 DECL_HARD_REGISTER (decl) = 1;
3726 /* This cannot be done for a structure with volatile
3727 fields, on which DECL_REGISTER will have been
3728 reset. */
3729 if (!DECL_REGISTER (decl))
3730 error ("cannot put object with volatile field into register");
3731 }
3732
3733 if (TREE_CODE (decl) != FUNCTION_DECL)
3734 {
3735 /* If we're building a variable sized type, and we might be
3736 reachable other than via the top of the current binding
3737 level, then create a new BIND_EXPR so that we deallocate
3738 the object at the right time. */
3739 /* Note that DECL_SIZE can be null due to errors. */
3740 if (DECL_SIZE (decl)
3741 && !TREE_CONSTANT (DECL_SIZE (decl))
3742 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3743 {
3744 tree bind;
3745 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3746 TREE_SIDE_EFFECTS (bind) = 1;
3747 add_stmt (bind);
3748 BIND_EXPR_BODY (bind) = push_stmt_list ();
3749 }
3750 add_stmt (build_stmt (DECL_EXPR, decl));
3751 }
3752 }
3753
3754
3755 if (!DECL_FILE_SCOPE_P (decl))
3756 {
3757 /* Recompute the RTL of a local array now
3758 if it used to be an incomplete type. */
3759 if (was_incomplete
3760 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3761 {
3762 /* If we used it already as memory, it must stay in memory. */
3763 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3764 /* If it's still incomplete now, no init will save it. */
3765 if (DECL_SIZE (decl) == 0)
3766 DECL_INITIAL (decl) = 0;
3767 }
3768 }
3769 }
3770
3771 if (TREE_CODE (decl) == TYPE_DECL)
3772 {
3773 if (!DECL_FILE_SCOPE_P (decl)
3774 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3775 add_stmt (build_stmt (DECL_EXPR, decl));
3776
3777 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3778 }
3779
3780 /* At the end of a declaration, throw away any variable type sizes
3781 of types defined inside that declaration. There is no use
3782 computing them in the following function definition. */
3783 if (current_scope == file_scope)
3784 get_pending_sizes ();
3785
3786 /* Install a cleanup (aka destructor) if one was given. */
3787 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3788 {
3789 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3790 if (attr)
3791 {
3792 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3793 tree cleanup_decl = lookup_name (cleanup_id);
3794 tree cleanup;
3795 VEC(tree,gc) *vec;
3796
3797 /* Build "cleanup(&decl)" for the destructor. */
3798 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
3799 vec = VEC_alloc (tree, gc, 1);
3800 VEC_quick_push (tree, vec, cleanup);
3801 cleanup = build_function_call_vec (cleanup_decl, vec, NULL);
3802 VEC_free (tree, gc, vec);
3803
3804 /* Don't warn about decl unused; the cleanup uses it. */
3805 TREE_USED (decl) = 1;
3806 TREE_USED (cleanup_decl) = 1;
3807
3808 /* Initialize EH, if we've been told to do so. */
3809 c_maybe_initialize_eh ();
3810
3811 push_cleanup (decl, cleanup, false);
3812 }
3813 }
3814 }
3815
3816 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3817
3818 tree
3819 grokparm (const struct c_parm *parm)
3820 {
3821 tree attrs = parm->attrs;
3822 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3823 NULL, &attrs, NULL, NULL, DEPRECATED_NORMAL);
3824
3825 decl_attributes (&decl, attrs, 0);
3826
3827 return decl;
3828 }
3829
3830 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3831 and push that on the current scope. */
3832
3833 void
3834 push_parm_decl (const struct c_parm *parm)
3835 {
3836 tree attrs = parm->attrs;
3837 tree decl;
3838
3839 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
3840 &attrs, NULL, NULL, DEPRECATED_NORMAL);
3841 decl_attributes (&decl, attrs, 0);
3842
3843 decl = pushdecl (decl);
3844
3845 finish_decl (decl, NULL_TREE, NULL_TREE, NULL_TREE);
3846 }
3847
3848 /* Mark all the parameter declarations to date as forward decls.
3849 Also diagnose use of this extension. */
3850
3851 void
3852 mark_forward_parm_decls (void)
3853 {
3854 struct c_binding *b;
3855
3856 if (pedantic && !current_scope->warned_forward_parm_decls)
3857 {
3858 pedwarn (input_location, OPT_pedantic,
3859 "ISO C forbids forward parameter declarations");
3860 current_scope->warned_forward_parm_decls = true;
3861 }
3862
3863 for (b = current_scope->bindings; b; b = b->prev)
3864 if (TREE_CODE (b->decl) == PARM_DECL)
3865 TREE_ASM_WRITTEN (b->decl) = 1;
3866 }
3867 \f
3868 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3869 literal, which may be an incomplete array type completed by the
3870 initializer; INIT is a CONSTRUCTOR that initializes the compound
3871 literal. NON_CONST is true if the initializers contain something
3872 that cannot occur in a constant expression. */
3873
3874 tree
3875 build_compound_literal (tree type, tree init, bool non_const)
3876 {
3877 /* We do not use start_decl here because we have a type, not a declarator;
3878 and do not use finish_decl because the decl should be stored inside
3879 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3880 tree decl;
3881 tree complit;
3882 tree stmt;
3883
3884 if (type == error_mark_node)
3885 return error_mark_node;
3886
3887 decl = build_decl (VAR_DECL, NULL_TREE, type);
3888 DECL_EXTERNAL (decl) = 0;
3889 TREE_PUBLIC (decl) = 0;
3890 TREE_STATIC (decl) = (current_scope == file_scope);
3891 DECL_CONTEXT (decl) = current_function_decl;
3892 TREE_USED (decl) = 1;
3893 TREE_TYPE (decl) = type;
3894 TREE_READONLY (decl) = TYPE_READONLY (type);
3895 store_init_value (decl, init, NULL_TREE);
3896
3897 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3898 {
3899 int failure = complete_array_type (&TREE_TYPE (decl),
3900 DECL_INITIAL (decl), true);
3901 gcc_assert (!failure);
3902
3903 type = TREE_TYPE (decl);
3904 TREE_TYPE (DECL_INITIAL (decl)) = type;
3905 }
3906
3907 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3908 return error_mark_node;
3909
3910 stmt = build_stmt (DECL_EXPR, decl);
3911 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3912 TREE_SIDE_EFFECTS (complit) = 1;
3913
3914 layout_decl (decl, 0);
3915
3916 if (TREE_STATIC (decl))
3917 {
3918 /* This decl needs a name for the assembler output. */
3919 set_compound_literal_name (decl);
3920 DECL_DEFER_OUTPUT (decl) = 1;
3921 DECL_COMDAT (decl) = 1;
3922 DECL_ARTIFICIAL (decl) = 1;
3923 DECL_IGNORED_P (decl) = 1;
3924 pushdecl (decl);
3925 rest_of_decl_compilation (decl, 1, 0);
3926 }
3927
3928 if (non_const)
3929 {
3930 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
3931 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
3932 }
3933
3934 return complit;
3935 }
3936
3937 /* Check the type of a compound literal. Here we just check that it
3938 is valid for C++. */
3939
3940 void
3941 check_compound_literal_type (struct c_type_name *type_name, location_t loc)
3942 {
3943 if (warn_cxx_compat && type_name->specs->tag_defined_p)
3944 warning_at (loc, OPT_Wc___compat,
3945 "defining a type in a compound literal is invalid in C++");
3946 }
3947 \f
3948 /* Determine whether TYPE is a structure with a flexible array member,
3949 or a union containing such a structure (possibly recursively). */
3950
3951 static bool
3952 flexible_array_type_p (tree type)
3953 {
3954 tree x;
3955 switch (TREE_CODE (type))
3956 {
3957 case RECORD_TYPE:
3958 x = TYPE_FIELDS (type);
3959 if (x == NULL_TREE)
3960 return false;
3961 while (TREE_CHAIN (x) != NULL_TREE)
3962 x = TREE_CHAIN (x);
3963 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3964 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3965 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3966 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3967 return true;
3968 return false;
3969 case UNION_TYPE:
3970 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3971 {
3972 if (flexible_array_type_p (TREE_TYPE (x)))
3973 return true;
3974 }
3975 return false;
3976 default:
3977 return false;
3978 }
3979 }
3980 \f
3981 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3982 replacing with appropriate values if they are invalid. */
3983 static void
3984 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
3985 {
3986 tree type_mv;
3987 unsigned int max_width;
3988 unsigned HOST_WIDE_INT w;
3989 const char *name = (orig_name
3990 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
3991 : _("<anonymous>"));
3992
3993 /* Detect and ignore out of range field width and process valid
3994 field widths. */
3995 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3996 || TREE_CODE (*width) != INTEGER_CST)
3997 {
3998 error ("bit-field %qs width not an integer constant", name);
3999 *width = integer_one_node;
4000 }
4001 else
4002 {
4003 constant_expression_warning (*width);
4004 if (tree_int_cst_sgn (*width) < 0)
4005 {
4006 error ("negative width in bit-field %qs", name);
4007 *width = integer_one_node;
4008 }
4009 else if (integer_zerop (*width) && orig_name)
4010 {
4011 error ("zero width for bit-field %qs", name);
4012 *width = integer_one_node;
4013 }
4014 }
4015
4016 /* Detect invalid bit-field type. */
4017 if (TREE_CODE (*type) != INTEGER_TYPE
4018 && TREE_CODE (*type) != BOOLEAN_TYPE
4019 && TREE_CODE (*type) != ENUMERAL_TYPE)
4020 {
4021 error ("bit-field %qs has invalid type", name);
4022 *type = unsigned_type_node;
4023 }
4024
4025 type_mv = TYPE_MAIN_VARIANT (*type);
4026 if (!in_system_header
4027 && type_mv != integer_type_node
4028 && type_mv != unsigned_type_node
4029 && type_mv != boolean_type_node)
4030 pedwarn (input_location, OPT_pedantic,
4031 "type of bit-field %qs is a GCC extension", name);
4032
4033 max_width = TYPE_PRECISION (*type);
4034
4035 if (0 < compare_tree_int (*width, max_width))
4036 {
4037 error ("width of %qs exceeds its type", name);
4038 w = max_width;
4039 *width = build_int_cst (NULL_TREE, w);
4040 }
4041 else
4042 w = tree_low_cst (*width, 1);
4043
4044 if (TREE_CODE (*type) == ENUMERAL_TYPE)
4045 {
4046 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4047 if (!lt
4048 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
4049 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
4050 warning (0, "%qs is narrower than values of its type", name);
4051 }
4052 }
4053
4054 \f
4055
4056 /* Print warning about variable length array if necessary. */
4057
4058 static void
4059 warn_variable_length_array (tree name, tree size)
4060 {
4061 int const_size = TREE_CONSTANT (size);
4062
4063 if (!flag_isoc99 && pedantic && warn_vla != 0)
4064 {
4065 if (const_size)
4066 {
4067 if (name)
4068 pedwarn (input_location, OPT_Wvla,
4069 "ISO C90 forbids array %qE whose size "
4070 "can%'t be evaluated",
4071 name);
4072 else
4073 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
4074 "can%'t be evaluated");
4075 }
4076 else
4077 {
4078 if (name)
4079 pedwarn (input_location, OPT_Wvla,
4080 "ISO C90 forbids variable length array %qE",
4081 name);
4082 else
4083 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
4084 }
4085 }
4086 else if (warn_vla > 0)
4087 {
4088 if (const_size)
4089 {
4090 if (name)
4091 warning (OPT_Wvla,
4092 "the size of array %qE can"
4093 "%'t be evaluated", name);
4094 else
4095 warning (OPT_Wvla,
4096 "the size of array can %'t be evaluated");
4097 }
4098 else
4099 {
4100 if (name)
4101 warning (OPT_Wvla,
4102 "variable length array %qE is used",
4103 name);
4104 else
4105 warning (OPT_Wvla,
4106 "variable length array is used");
4107 }
4108 }
4109 }
4110
4111 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR to
4112 serve as the actual size-expression for a type or decl. This is
4113 like variable_size in stor-layout.c, but we make global_bindings_p
4114 return negative to avoid calls to that function from outside the
4115 front end resulting in errors at file scope, then call this version
4116 instead from front-end code. */
4117
4118 static tree
4119 c_variable_size (tree size)
4120 {
4121 tree save;
4122
4123 if (TREE_CONSTANT (size))
4124 return size;
4125
4126 size = save_expr (size);
4127
4128 save = skip_simple_arithmetic (size);
4129
4130 if (cfun && cfun->dont_save_pending_sizes_p)
4131 return size;
4132
4133 if (!global_bindings_p ())
4134 put_pending_size (save);
4135
4136 return size;
4137 }
4138
4139 /* Given declspecs and a declarator,
4140 determine the name and type of the object declared
4141 and construct a ..._DECL node for it.
4142 (In one case we can return a ..._TYPE node instead.
4143 For invalid input we sometimes return 0.)
4144
4145 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4146
4147 DECL_CONTEXT says which syntactic context this declaration is in:
4148 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4149 FUNCDEF for a function definition. Like NORMAL but a few different
4150 error messages in each case. Return value may be zero meaning
4151 this definition is too screwy to try to parse.
4152 PARM for a parameter declaration (either within a function prototype
4153 or before a function body). Make a PARM_DECL, or return void_type_node.
4154 TYPENAME if for a typename (in a cast or sizeof).
4155 Don't make a DECL node; just return the ..._TYPE node.
4156 FIELD for a struct or union field; make a FIELD_DECL.
4157 INITIALIZED is true if the decl has an initializer.
4158 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4159 representing the width of the bit-field.
4160 DECL_ATTRS points to the list of attributes that should be added to this
4161 decl. Any nested attributes that belong on the decl itself will be
4162 added to this list.
4163 If EXPR is not NULL, any expressions that need to be evaluated as
4164 part of evaluating variably modified types will be stored in *EXPR.
4165 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4166 set to indicate whether operands in *EXPR can be used in constant
4167 expressions.
4168 DEPRECATED_STATE is a deprecated_states value indicating whether
4169 deprecation warnings should be suppressed.
4170
4171 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4172 It may also be so in the PARM case, for a prototype where the
4173 argument type is specified but not the name.
4174
4175 This function is where the complicated C meanings of `static'
4176 and `extern' are interpreted. */
4177
4178 static tree
4179 grokdeclarator (const struct c_declarator *declarator,
4180 struct c_declspecs *declspecs,
4181 enum decl_context decl_context, bool initialized, tree *width,
4182 tree *decl_attrs, tree *expr, bool *expr_const_operands,
4183 enum deprecated_states deprecated_state)
4184 {
4185 tree type = declspecs->type;
4186 bool threadp = declspecs->thread_p;
4187 enum c_storage_class storage_class = declspecs->storage_class;
4188 int constp;
4189 int restrictp;
4190 int volatilep;
4191 int type_quals = TYPE_UNQUALIFIED;
4192 tree name = NULL_TREE;
4193 bool funcdef_flag = false;
4194 bool funcdef_syntax = false;
4195 int size_varies = 0;
4196 tree decl_attr = declspecs->decl_attr;
4197 int array_ptr_quals = TYPE_UNQUALIFIED;
4198 tree array_ptr_attrs = NULL_TREE;
4199 int array_parm_static = 0;
4200 bool array_parm_vla_unspec_p = false;
4201 tree returned_attrs = NULL_TREE;
4202 bool bitfield = width != NULL;
4203 tree element_type;
4204 struct c_arg_info *arg_info = 0;
4205 const char *errmsg;
4206 tree expr_dummy;
4207 bool expr_const_operands_dummy;
4208
4209 if (expr == NULL)
4210 expr = &expr_dummy;
4211 if (expr_const_operands == NULL)
4212 expr_const_operands = &expr_const_operands_dummy;
4213
4214 *expr = declspecs->expr;
4215 *expr_const_operands = declspecs->expr_const_operands;
4216
4217 if (decl_context == FUNCDEF)
4218 funcdef_flag = true, decl_context = NORMAL;
4219
4220 /* Look inside a declarator for the name being declared
4221 and get it as an IDENTIFIER_NODE, for an error message. */
4222 {
4223 const struct c_declarator *decl = declarator;
4224
4225 while (decl)
4226 switch (decl->kind)
4227 {
4228 case cdk_function:
4229 case cdk_array:
4230 case cdk_pointer:
4231 funcdef_syntax = (decl->kind == cdk_function);
4232 decl = decl->declarator;
4233 break;
4234
4235 case cdk_attrs:
4236 decl = decl->declarator;
4237 break;
4238
4239 case cdk_id:
4240 if (decl->u.id)
4241 name = decl->u.id;
4242 decl = 0;
4243 break;
4244
4245 default:
4246 gcc_unreachable ();
4247 }
4248 if (name == 0)
4249 {
4250 gcc_assert (decl_context == PARM
4251 || decl_context == TYPENAME
4252 || (decl_context == FIELD
4253 && declarator->kind == cdk_id));
4254 gcc_assert (!initialized);
4255 }
4256 }
4257
4258 /* A function definition's declarator must have the form of
4259 a function declarator. */
4260
4261 if (funcdef_flag && !funcdef_syntax)
4262 return 0;
4263
4264 /* If this looks like a function definition, make it one,
4265 even if it occurs where parms are expected.
4266 Then store_parm_decls will reject it and not use it as a parm. */
4267 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4268 decl_context = PARM;
4269
4270 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4271 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
4272
4273 if ((decl_context == NORMAL || decl_context == FIELD)
4274 && current_scope == file_scope
4275 && variably_modified_type_p (type, NULL_TREE))
4276 {
4277 if (name)
4278 error ("variably modified %qE at file scope", name);
4279 else
4280 error ("variably modified field at file scope");
4281 type = integer_type_node;
4282 }
4283
4284 size_varies = C_TYPE_VARIABLE_SIZE (type);
4285
4286 /* Diagnose defaulting to "int". */
4287
4288 if (declspecs->default_int_p && !in_system_header)
4289 {
4290 /* Issue a warning if this is an ISO C 99 program or if
4291 -Wreturn-type and this is a function, or if -Wimplicit;
4292 prefer the former warning since it is more explicit. */
4293 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4294 && funcdef_flag)
4295 warn_about_return_type = 1;
4296 else
4297 {
4298 if (name)
4299 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4300 "type defaults to %<int%> in declaration of %qE",
4301 name);
4302 else
4303 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4304 "type defaults to %<int%> in type name");
4305 }
4306 }
4307
4308 /* Adjust the type if a bit-field is being declared,
4309 -funsigned-bitfields applied and the type is not explicitly
4310 "signed". */
4311 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4312 && TREE_CODE (type) == INTEGER_TYPE)
4313 type = unsigned_type_for (type);
4314
4315 /* Figure out the type qualifiers for the declaration. There are
4316 two ways a declaration can become qualified. One is something
4317 like `const int i' where the `const' is explicit. Another is
4318 something like `typedef const int CI; CI i' where the type of the
4319 declaration contains the `const'. A third possibility is that
4320 there is a type qualifier on the element type of a typedefed
4321 array type, in which case we should extract that qualifier so
4322 that c_apply_type_quals_to_decl receives the full list of
4323 qualifiers to work with (C90 is not entirely clear about whether
4324 duplicate qualifiers should be diagnosed in this case, but it
4325 seems most appropriate to do so). */
4326 element_type = strip_array_types (type);
4327 constp = declspecs->const_p + TYPE_READONLY (element_type);
4328 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4329 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4330 if (pedantic && !flag_isoc99)
4331 {
4332 if (constp > 1)
4333 pedwarn (input_location, OPT_pedantic, "duplicate %<const%>");
4334 if (restrictp > 1)
4335 pedwarn (input_location, OPT_pedantic, "duplicate %<restrict%>");
4336 if (volatilep > 1)
4337 pedwarn (input_location, OPT_pedantic, "duplicate %<volatile%>");
4338 }
4339 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4340 type = TYPE_MAIN_VARIANT (type);
4341 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4342 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4343 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4344
4345 /* Warn about storage classes that are invalid for certain
4346 kinds of declarations (parameters, typenames, etc.). */
4347
4348 if (funcdef_flag
4349 && (threadp
4350 || storage_class == csc_auto
4351 || storage_class == csc_register
4352 || storage_class == csc_typedef))
4353 {
4354 if (storage_class == csc_auto)
4355 pedwarn (input_location,
4356 (current_scope == file_scope) ? 0 : OPT_pedantic,
4357 "function definition declared %<auto%>");
4358 if (storage_class == csc_register)
4359 error ("function definition declared %<register%>");
4360 if (storage_class == csc_typedef)
4361 error ("function definition declared %<typedef%>");
4362 if (threadp)
4363 error ("function definition declared %<__thread%>");
4364 threadp = false;
4365 if (storage_class == csc_auto
4366 || storage_class == csc_register
4367 || storage_class == csc_typedef)
4368 storage_class = csc_none;
4369 }
4370 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4371 {
4372 if (decl_context == PARM && storage_class == csc_register)
4373 ;
4374 else
4375 {
4376 switch (decl_context)
4377 {
4378 case FIELD:
4379 if (name)
4380 error ("storage class specified for structure field %qE",
4381 name);
4382 else
4383 error ("storage class specified for structure field");
4384 break;
4385 case PARM:
4386 if (name)
4387 error ("storage class specified for parameter %qE", name);
4388 else
4389 error ("storage class specified for unnamed parameter");
4390 break;
4391 default:
4392 error ("storage class specified for typename");
4393 break;
4394 }
4395 storage_class = csc_none;
4396 threadp = false;
4397 }
4398 }
4399 else if (storage_class == csc_extern
4400 && initialized
4401 && !funcdef_flag)
4402 {
4403 /* 'extern' with initialization is invalid if not at file scope. */
4404 if (current_scope == file_scope)
4405 {
4406 /* It is fine to have 'extern const' when compiling at C
4407 and C++ intersection. */
4408 if (!(warn_cxx_compat && constp))
4409 warning (0, "%qE initialized and declared %<extern%>", name);
4410 }
4411 else
4412 error ("%qE has both %<extern%> and initializer", name);
4413 }
4414 else if (current_scope == file_scope)
4415 {
4416 if (storage_class == csc_auto)
4417 error ("file-scope declaration of %qE specifies %<auto%>", name);
4418 if (pedantic && storage_class == csc_register)
4419 pedwarn (input_location, OPT_pedantic,
4420 "file-scope declaration of %qE specifies %<register%>", name);
4421 }
4422 else
4423 {
4424 if (storage_class == csc_extern && funcdef_flag)
4425 error ("nested function %qE declared %<extern%>", name);
4426 else if (threadp && storage_class == csc_none)
4427 {
4428 error ("function-scope %qE implicitly auto and declared "
4429 "%<__thread%>",
4430 name);
4431 threadp = false;
4432 }
4433 }
4434
4435 /* Now figure out the structure of the declarator proper.
4436 Descend through it, creating more complex types, until we reach
4437 the declared identifier (or NULL_TREE, in an absolute declarator).
4438 At each stage we maintain an unqualified version of the type
4439 together with any qualifiers that should be applied to it with
4440 c_build_qualified_type; this way, array types including
4441 multidimensional array types are first built up in unqualified
4442 form and then the qualified form is created with
4443 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4444
4445 while (declarator && declarator->kind != cdk_id)
4446 {
4447 if (type == error_mark_node)
4448 {
4449 declarator = declarator->declarator;
4450 continue;
4451 }
4452
4453 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4454 a cdk_pointer (for *...),
4455 a cdk_function (for ...(...)),
4456 a cdk_attrs (for nested attributes),
4457 or a cdk_id (for the name being declared
4458 or the place in an absolute declarator
4459 where the name was omitted).
4460 For the last case, we have just exited the loop.
4461
4462 At this point, TYPE is the type of elements of an array,
4463 or for a function to return, or for a pointer to point to.
4464 After this sequence of ifs, TYPE is the type of the
4465 array or function or pointer, and DECLARATOR has had its
4466 outermost layer removed. */
4467
4468 if (array_ptr_quals != TYPE_UNQUALIFIED
4469 || array_ptr_attrs != NULL_TREE
4470 || array_parm_static)
4471 {
4472 /* Only the innermost declarator (making a parameter be of
4473 array type which is converted to pointer type)
4474 may have static or type qualifiers. */
4475 error ("static or type qualifiers in non-parameter array declarator");
4476 array_ptr_quals = TYPE_UNQUALIFIED;
4477 array_ptr_attrs = NULL_TREE;
4478 array_parm_static = 0;
4479 }
4480
4481 switch (declarator->kind)
4482 {
4483 case cdk_attrs:
4484 {
4485 /* A declarator with embedded attributes. */
4486 tree attrs = declarator->u.attrs;
4487 const struct c_declarator *inner_decl;
4488 int attr_flags = 0;
4489 declarator = declarator->declarator;
4490 inner_decl = declarator;
4491 while (inner_decl->kind == cdk_attrs)
4492 inner_decl = inner_decl->declarator;
4493 if (inner_decl->kind == cdk_id)
4494 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4495 else if (inner_decl->kind == cdk_function)
4496 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4497 else if (inner_decl->kind == cdk_array)
4498 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4499 returned_attrs = decl_attributes (&type,
4500 chainon (returned_attrs, attrs),
4501 attr_flags);
4502 break;
4503 }
4504 case cdk_array:
4505 {
4506 tree itype = NULL_TREE;
4507 tree size = declarator->u.array.dimen;
4508 /* The index is a signed object `sizetype' bits wide. */
4509 tree index_type = c_common_signed_type (sizetype);
4510
4511 array_ptr_quals = declarator->u.array.quals;
4512 array_ptr_attrs = declarator->u.array.attrs;
4513 array_parm_static = declarator->u.array.static_p;
4514 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4515
4516 declarator = declarator->declarator;
4517
4518 /* Check for some types that there cannot be arrays of. */
4519
4520 if (VOID_TYPE_P (type))
4521 {
4522 if (name)
4523 error ("declaration of %qE as array of voids", name);
4524 else
4525 error ("declaration of type name as array of voids");
4526 type = error_mark_node;
4527 }
4528
4529 if (TREE_CODE (type) == FUNCTION_TYPE)
4530 {
4531 if (name)
4532 error ("declaration of %qE as array of functions", name);
4533 else
4534 error ("declaration of type name as array of functions");
4535 type = error_mark_node;
4536 }
4537
4538 if (pedantic && !in_system_header && flexible_array_type_p (type))
4539 pedwarn (input_location, OPT_pedantic,
4540 "invalid use of structure with flexible array member");
4541
4542 if (size == error_mark_node)
4543 type = error_mark_node;
4544
4545 if (type == error_mark_node)
4546 continue;
4547
4548 /* If size was specified, set ITYPE to a range-type for
4549 that size. Otherwise, ITYPE remains null. finish_decl
4550 may figure it out from an initial value. */
4551
4552 if (size)
4553 {
4554 bool size_maybe_const = true;
4555 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
4556 && !TREE_OVERFLOW (size));
4557 bool this_size_varies = false;
4558
4559 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4560 lvalue. */
4561 STRIP_TYPE_NOPS (size);
4562
4563 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4564 {
4565 if (name)
4566 error ("size of array %qE has non-integer type", name);
4567 else
4568 error ("size of unnamed array has non-integer type");
4569 size = integer_one_node;
4570 }
4571
4572 size = c_fully_fold (size, false, &size_maybe_const);
4573
4574 if (pedantic && size_maybe_const && integer_zerop (size))
4575 {
4576 if (name)
4577 pedwarn (input_location, OPT_pedantic,
4578 "ISO C forbids zero-size array %qE", name);
4579 else
4580 pedwarn (input_location, OPT_pedantic,
4581 "ISO C forbids zero-size array");
4582 }
4583
4584 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
4585 {
4586 constant_expression_warning (size);
4587 if (tree_int_cst_sgn (size) < 0)
4588 {
4589 if (name)
4590 error ("size of array %qE is negative", name);
4591 else
4592 error ("size of unnamed array is negative");
4593 size = integer_one_node;
4594 }
4595 /* Handle a size folded to an integer constant but
4596 not an integer constant expression. */
4597 if (!size_int_const)
4598 {
4599 /* If this is a file scope declaration of an
4600 ordinary identifier, this is invalid code;
4601 diagnosing it here and not subsequently
4602 treating the type as variable-length avoids
4603 more confusing diagnostics later. */
4604 if ((decl_context == NORMAL || decl_context == FIELD)
4605 && current_scope == file_scope)
4606 pedwarn (input_location, 0,
4607 "variably modified %qE at file scope",
4608 name);
4609 else
4610 this_size_varies = size_varies = 1;
4611 warn_variable_length_array (name, size);
4612 }
4613 }
4614 else if ((decl_context == NORMAL || decl_context == FIELD)
4615 && current_scope == file_scope)
4616 {
4617 error ("variably modified %qE at file scope", name);
4618 size = integer_one_node;
4619 }
4620 else
4621 {
4622 /* Make sure the array size remains visibly
4623 nonconstant even if it is (eg) a const variable
4624 with known value. */
4625 this_size_varies = size_varies = 1;
4626 warn_variable_length_array (name, size);
4627 }
4628
4629 if (integer_zerop (size) && !this_size_varies)
4630 {
4631 /* A zero-length array cannot be represented with
4632 an unsigned index type, which is what we'll
4633 get with build_index_type. Create an
4634 open-ended range instead. */
4635 itype = build_range_type (sizetype, size, NULL_TREE);
4636 }
4637 else
4638 {
4639 /* Arrange for the SAVE_EXPR on the inside of the
4640 MINUS_EXPR, which allows the -1 to get folded
4641 with the +1 that happens when building TYPE_SIZE. */
4642 if (size_varies)
4643 size = c_variable_size (size);
4644 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
4645 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4646 integer_zero_node, size);
4647
4648 /* Compute the maximum valid index, that is, size
4649 - 1. Do the calculation in index_type, so that
4650 if it is a variable the computations will be
4651 done in the proper mode. */
4652 itype = fold_build2 (MINUS_EXPR, index_type,
4653 convert (index_type, size),
4654 convert (index_type,
4655 size_one_node));
4656
4657 /* If that overflowed, the array is too big. ???
4658 While a size of INT_MAX+1 technically shouldn't
4659 cause an overflow (because we subtract 1), the
4660 overflow is recorded during the conversion to
4661 index_type, before the subtraction. Handling
4662 this case seems like an unnecessary
4663 complication. */
4664 if (TREE_CODE (itype) == INTEGER_CST
4665 && TREE_OVERFLOW (itype))
4666 {
4667 if (name)
4668 error ("size of array %qE is too large", name);
4669 else
4670 error ("size of unnamed array is too large");
4671 type = error_mark_node;
4672 continue;
4673 }
4674
4675 itype = build_index_type (itype);
4676 }
4677 if (this_size_varies)
4678 {
4679 if (*expr)
4680 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4681 *expr, size);
4682 else
4683 *expr = size;
4684 *expr_const_operands &= size_maybe_const;
4685 }
4686 }
4687 else if (decl_context == FIELD)
4688 {
4689 bool flexible_array_member = false;
4690 if (array_parm_vla_unspec_p)
4691 /* Field names can in fact have function prototype
4692 scope so [*] is disallowed here through making
4693 the field variably modified, not through being
4694 something other than a declaration with function
4695 prototype scope. */
4696 size_varies = 1;
4697 else
4698 {
4699 const struct c_declarator *t = declarator;
4700 while (t->kind == cdk_attrs)
4701 t = t->declarator;
4702 flexible_array_member = (t->kind == cdk_id);
4703 }
4704 if (flexible_array_member
4705 && pedantic && !flag_isoc99 && !in_system_header)
4706 pedwarn (input_location, OPT_pedantic,
4707 "ISO C90 does not support flexible array members");
4708
4709 /* ISO C99 Flexible array members are effectively
4710 identical to GCC's zero-length array extension. */
4711 if (flexible_array_member || array_parm_vla_unspec_p)
4712 itype = build_range_type (sizetype, size_zero_node,
4713 NULL_TREE);
4714 }
4715 else if (decl_context == PARM)
4716 {
4717 if (array_parm_vla_unspec_p)
4718 {
4719 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4720 size_varies = 1;
4721 }
4722 }
4723 else if (decl_context == TYPENAME)
4724 {
4725 if (array_parm_vla_unspec_p)
4726 {
4727 /* C99 6.7.5.2p4 */
4728 warning (0, "%<[*]%> not in a declaration");
4729 /* We use this to avoid messing up with incomplete
4730 array types of the same type, that would
4731 otherwise be modified below. */
4732 itype = build_range_type (sizetype, size_zero_node,
4733 NULL_TREE);
4734 size_varies = 1;
4735 }
4736 }
4737
4738 /* Complain about arrays of incomplete types. */
4739 if (!COMPLETE_TYPE_P (type))
4740 {
4741 error ("array type has incomplete element type");
4742 type = error_mark_node;
4743 }
4744 else
4745 /* When itype is NULL, a shared incomplete array type is
4746 returned for all array of a given type. Elsewhere we
4747 make sure we don't complete that type before copying
4748 it, but here we want to make sure we don't ever
4749 modify the shared type, so we gcc_assert (itype)
4750 below. */
4751 type = build_array_type (type, itype);
4752
4753 if (type != error_mark_node)
4754 {
4755 if (size_varies)
4756 {
4757 /* It is ok to modify type here even if itype is
4758 NULL: if size_varies, we're in a
4759 multi-dimensional array and the inner type has
4760 variable size, so the enclosing shared array type
4761 must too. */
4762 if (size && TREE_CODE (size) == INTEGER_CST)
4763 type
4764 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4765 C_TYPE_VARIABLE_SIZE (type) = 1;
4766 }
4767
4768 /* The GCC extension for zero-length arrays differs from
4769 ISO flexible array members in that sizeof yields
4770 zero. */
4771 if (size && integer_zerop (size))
4772 {
4773 gcc_assert (itype);
4774 TYPE_SIZE (type) = bitsize_zero_node;
4775 TYPE_SIZE_UNIT (type) = size_zero_node;
4776 }
4777 if (array_parm_vla_unspec_p)
4778 {
4779 gcc_assert (itype);
4780 /* The type is complete. C99 6.7.5.2p4 */
4781 TYPE_SIZE (type) = bitsize_zero_node;
4782 TYPE_SIZE_UNIT (type) = size_zero_node;
4783 }
4784 }
4785
4786 if (decl_context != PARM
4787 && (array_ptr_quals != TYPE_UNQUALIFIED
4788 || array_ptr_attrs != NULL_TREE
4789 || array_parm_static))
4790 {
4791 error ("static or type qualifiers in non-parameter array declarator");
4792 array_ptr_quals = TYPE_UNQUALIFIED;
4793 array_ptr_attrs = NULL_TREE;
4794 array_parm_static = 0;
4795 }
4796 break;
4797 }
4798 case cdk_function:
4799 {
4800 /* Say it's a definition only for the declarator closest
4801 to the identifier, apart possibly from some
4802 attributes. */
4803 bool really_funcdef = false;
4804 tree arg_types;
4805 if (funcdef_flag)
4806 {
4807 const struct c_declarator *t = declarator->declarator;
4808 while (t->kind == cdk_attrs)
4809 t = t->declarator;
4810 really_funcdef = (t->kind == cdk_id);
4811 }
4812
4813 /* Declaring a function type. Make sure we have a valid
4814 type for the function to return. */
4815 if (type == error_mark_node)
4816 continue;
4817
4818 size_varies = 0;
4819
4820 /* Warn about some types functions can't return. */
4821 if (TREE_CODE (type) == FUNCTION_TYPE)
4822 {
4823 if (name)
4824 error ("%qE declared as function returning a function",
4825 name);
4826 else
4827 error ("type name declared as function "
4828 "returning a function");
4829 type = integer_type_node;
4830 }
4831 if (TREE_CODE (type) == ARRAY_TYPE)
4832 {
4833 if (name)
4834 error ("%qE declared as function returning an array", name);
4835 else
4836 error ("type name declared as function returning an array");
4837 type = integer_type_node;
4838 }
4839 errmsg = targetm.invalid_return_type (type);
4840 if (errmsg)
4841 {
4842 error (errmsg);
4843 type = integer_type_node;
4844 }
4845
4846 /* Construct the function type and go to the next
4847 inner layer of declarator. */
4848 arg_info = declarator->u.arg_info;
4849 arg_types = grokparms (arg_info, really_funcdef);
4850 if (really_funcdef)
4851 put_pending_sizes (arg_info->pending_sizes);
4852
4853 /* Type qualifiers before the return type of the function
4854 qualify the return type, not the function type. */
4855 if (type_quals)
4856 {
4857 /* Type qualifiers on a function return type are
4858 normally permitted by the standard but have no
4859 effect, so give a warning at -Wreturn-type.
4860 Qualifiers on a void return type are banned on
4861 function definitions in ISO C; GCC used to used
4862 them for noreturn functions. */
4863 if (VOID_TYPE_P (type) && really_funcdef)
4864 pedwarn (input_location, 0,
4865 "function definition has qualified void return type");
4866 else
4867 warning (OPT_Wignored_qualifiers,
4868 "type qualifiers ignored on function return type");
4869
4870 type = c_build_qualified_type (type, type_quals);
4871 }
4872 type_quals = TYPE_UNQUALIFIED;
4873
4874 type = build_function_type (type, arg_types);
4875 declarator = declarator->declarator;
4876
4877 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4878 the formal parameter list of this FUNCTION_TYPE to point to
4879 the FUNCTION_TYPE node itself. */
4880 {
4881 tree link;
4882
4883 for (link = arg_info->tags;
4884 link;
4885 link = TREE_CHAIN (link))
4886 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4887 }
4888 break;
4889 }
4890 case cdk_pointer:
4891 {
4892 /* Merge any constancy or volatility into the target type
4893 for the pointer. */
4894
4895 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4896 && type_quals)
4897 pedwarn (input_location, OPT_pedantic,
4898 "ISO C forbids qualified function types");
4899 if (type_quals)
4900 type = c_build_qualified_type (type, type_quals);
4901 size_varies = 0;
4902
4903 /* When the pointed-to type involves components of variable size,
4904 care must be taken to ensure that the size evaluation code is
4905 emitted early enough to dominate all the possible later uses
4906 and late enough for the variables on which it depends to have
4907 been assigned.
4908
4909 This is expected to happen automatically when the pointed-to
4910 type has a name/declaration of it's own, but special attention
4911 is required if the type is anonymous.
4912
4913 We handle the NORMAL and FIELD contexts here by attaching an
4914 artificial TYPE_DECL to such pointed-to type. This forces the
4915 sizes evaluation at a safe point and ensures it is not deferred
4916 until e.g. within a deeper conditional context.
4917
4918 We expect nothing to be needed here for PARM or TYPENAME.
4919 Pushing a TYPE_DECL at this point for TYPENAME would actually
4920 be incorrect, as we might be in the middle of an expression
4921 with side effects on the pointed-to type size "arguments" prior
4922 to the pointer declaration point and the fake TYPE_DECL in the
4923 enclosing context would force the size evaluation prior to the
4924 side effects. */
4925
4926 if (!TYPE_NAME (type)
4927 && (decl_context == NORMAL || decl_context == FIELD)
4928 && variably_modified_type_p (type, NULL_TREE))
4929 {
4930 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4931 DECL_ARTIFICIAL (decl) = 1;
4932 pushdecl (decl);
4933 finish_decl (decl, NULL_TREE, NULL_TREE, NULL_TREE);
4934 TYPE_NAME (type) = decl;
4935 }
4936
4937 type = build_pointer_type (type);
4938
4939 /* Process type qualifiers (such as const or volatile)
4940 that were given inside the `*'. */
4941 type_quals = declarator->u.pointer_quals;
4942
4943 declarator = declarator->declarator;
4944 break;
4945 }
4946 default:
4947 gcc_unreachable ();
4948 }
4949 }
4950 *decl_attrs = chainon (returned_attrs, *decl_attrs);
4951
4952 /* Now TYPE has the actual type, apart from any qualifiers in
4953 TYPE_QUALS. */
4954
4955 /* Check the type and width of a bit-field. */
4956 if (bitfield)
4957 check_bitfield_type_and_width (&type, width, name);
4958
4959 /* Did array size calculations overflow? */
4960
4961 if (TREE_CODE (type) == ARRAY_TYPE
4962 && COMPLETE_TYPE_P (type)
4963 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4964 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4965 {
4966 if (name)
4967 error ("size of array %qE is too large", name);
4968 else
4969 error ("size of unnamed array is too large");
4970 /* If we proceed with the array type as it is, we'll eventually
4971 crash in tree_low_cst(). */
4972 type = error_mark_node;
4973 }
4974
4975 /* If this is declaring a typedef name, return a TYPE_DECL. */
4976
4977 if (storage_class == csc_typedef)
4978 {
4979 tree decl;
4980 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4981 && type_quals)
4982 pedwarn (input_location, OPT_pedantic,
4983 "ISO C forbids qualified function types");
4984 if (type_quals)
4985 type = c_build_qualified_type (type, type_quals);
4986 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4987 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4988 if (declspecs->explicit_signed_p)
4989 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4990 if (declspecs->inline_p)
4991 pedwarn (input_location, 0,"typedef %q+D declared %<inline%>", decl);
4992
4993 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
4994 {
4995 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
4996
4997 if (b != NULL
4998 && b->decl != NULL_TREE
4999 && (B_IN_CURRENT_SCOPE (b)
5000 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
5001 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
5002 {
5003 warning_at (declarator->id_loc, OPT_Wc___compat,
5004 ("using %qD as both a typedef and a tag is "
5005 "invalid in C++"),
5006 decl);
5007 if (b->locus != UNKNOWN_LOCATION)
5008 inform (b->locus, "originally defined here");
5009 }
5010 }
5011
5012 return decl;
5013 }
5014
5015 /* If this is a type name (such as, in a cast or sizeof),
5016 compute the type and return it now. */
5017
5018 if (decl_context == TYPENAME)
5019 {
5020 /* Note that the grammar rejects storage classes in typenames
5021 and fields. */
5022 gcc_assert (storage_class == csc_none && !threadp
5023 && !declspecs->inline_p);
5024 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5025 && type_quals)
5026 pedwarn (input_location, OPT_pedantic,
5027 "ISO C forbids const or volatile function types");
5028 if (type_quals)
5029 type = c_build_qualified_type (type, type_quals);
5030 return type;
5031 }
5032
5033 if (pedantic && decl_context == FIELD
5034 && variably_modified_type_p (type, NULL_TREE))
5035 {
5036 /* C99 6.7.2.1p8 */
5037 pedwarn (input_location, OPT_pedantic,
5038 "a member of a structure or union cannot have a variably modified type");
5039 }
5040
5041 /* Aside from typedefs and type names (handle above),
5042 `void' at top level (not within pointer)
5043 is allowed only in public variables.
5044 We don't complain about parms either, but that is because
5045 a better error message can be made later. */
5046
5047 if (VOID_TYPE_P (type) && decl_context != PARM
5048 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
5049 && (storage_class == csc_extern
5050 || (current_scope == file_scope
5051 && !(storage_class == csc_static
5052 || storage_class == csc_register)))))
5053 {
5054 error ("variable or field %qE declared void", name);
5055 type = integer_type_node;
5056 }
5057
5058 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
5059 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
5060
5061 {
5062 tree decl;
5063
5064 if (decl_context == PARM)
5065 {
5066 tree promoted_type;
5067
5068 /* A parameter declared as an array of T is really a pointer to T.
5069 One declared as a function is really a pointer to a function. */
5070
5071 if (TREE_CODE (type) == ARRAY_TYPE)
5072 {
5073 /* Transfer const-ness of array into that of type pointed to. */
5074 type = TREE_TYPE (type);
5075 if (type_quals)
5076 type = c_build_qualified_type (type, type_quals);
5077 type = build_pointer_type (type);
5078 type_quals = array_ptr_quals;
5079 if (type_quals)
5080 type = c_build_qualified_type (type, type_quals);
5081
5082 /* We don't yet implement attributes in this context. */
5083 if (array_ptr_attrs != NULL_TREE)
5084 warning (OPT_Wattributes,
5085 "attributes in parameter array declarator ignored");
5086
5087 size_varies = 0;
5088 }
5089 else if (TREE_CODE (type) == FUNCTION_TYPE)
5090 {
5091 if (type_quals)
5092 pedwarn (input_location, OPT_pedantic,
5093 "ISO C forbids qualified function types");
5094 if (type_quals)
5095 type = c_build_qualified_type (type, type_quals);
5096 type = build_pointer_type (type);
5097 type_quals = TYPE_UNQUALIFIED;
5098 }
5099 else if (type_quals)
5100 type = c_build_qualified_type (type, type_quals);
5101
5102 decl = build_decl (PARM_DECL, declarator->u.id, type);
5103 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5104 if (size_varies)
5105 C_DECL_VARIABLE_SIZE (decl) = 1;
5106
5107 /* Compute the type actually passed in the parmlist,
5108 for the case where there is no prototype.
5109 (For example, shorts and chars are passed as ints.)
5110 When there is a prototype, this is overridden later. */
5111
5112 if (type == error_mark_node)
5113 promoted_type = type;
5114 else
5115 promoted_type = c_type_promotes_to (type);
5116
5117 DECL_ARG_TYPE (decl) = promoted_type;
5118 if (declspecs->inline_p)
5119 pedwarn (input_location, 0, "parameter %q+D declared %<inline%>", decl);
5120 }
5121 else if (decl_context == FIELD)
5122 {
5123 /* Note that the grammar rejects storage classes in typenames
5124 and fields. */
5125 gcc_assert (storage_class == csc_none && !threadp
5126 && !declspecs->inline_p);
5127
5128 /* Structure field. It may not be a function. */
5129
5130 if (TREE_CODE (type) == FUNCTION_TYPE)
5131 {
5132 error ("field %qE declared as a function", name);
5133 type = build_pointer_type (type);
5134 }
5135 else if (TREE_CODE (type) != ERROR_MARK
5136 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
5137 {
5138 if (name)
5139 error ("field %qE has incomplete type", name);
5140 else
5141 error ("unnamed field has incomplete type");
5142 type = error_mark_node;
5143 }
5144 type = c_build_qualified_type (type, type_quals);
5145 decl = build_decl (FIELD_DECL, declarator->u.id, type);
5146 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5147 DECL_NONADDRESSABLE_P (decl) = bitfield;
5148 if (bitfield && !declarator->u.id)
5149 TREE_NO_WARNING (decl) = 1;
5150
5151 if (size_varies)
5152 C_DECL_VARIABLE_SIZE (decl) = 1;
5153 }
5154 else if (TREE_CODE (type) == FUNCTION_TYPE)
5155 {
5156 if (storage_class == csc_register || threadp)
5157 {
5158 error ("invalid storage class for function %qE", name);
5159 }
5160 else if (current_scope != file_scope)
5161 {
5162 /* Function declaration not at file scope. Storage
5163 classes other than `extern' are not allowed, C99
5164 6.7.1p5, and `extern' makes no difference. However,
5165 GCC allows 'auto', perhaps with 'inline', to support
5166 nested functions. */
5167 if (storage_class == csc_auto)
5168 pedwarn (input_location, OPT_pedantic,
5169 "invalid storage class for function %qE", name);
5170 else if (storage_class == csc_static)
5171 {
5172 error ("invalid storage class for function %qE", name);
5173 if (funcdef_flag)
5174 storage_class = declspecs->storage_class = csc_none;
5175 else
5176 return 0;
5177 }
5178 }
5179
5180 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
5181 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5182 decl = build_decl_attribute_variant (decl, decl_attr);
5183
5184 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
5185 pedwarn (input_location, OPT_pedantic,
5186 "ISO C forbids qualified function types");
5187
5188 /* GNU C interprets a volatile-qualified function type to indicate
5189 that the function does not return. */
5190 if ((type_quals & TYPE_QUAL_VOLATILE)
5191 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5192 warning (0, "%<noreturn%> function returns non-void value");
5193
5194 /* Every function declaration is an external reference
5195 (DECL_EXTERNAL) except for those which are not at file
5196 scope and are explicitly declared "auto". This is
5197 forbidden by standard C (C99 6.7.1p5) and is interpreted by
5198 GCC to signify a forward declaration of a nested function. */
5199 if (storage_class == csc_auto && current_scope != file_scope)
5200 DECL_EXTERNAL (decl) = 0;
5201 /* In C99, a function which is declared 'inline' with 'extern'
5202 is not an external reference (which is confusing). It
5203 means that the later definition of the function must be output
5204 in this file, C99 6.7.4p6. In GNU C89, a function declared
5205 'extern inline' is an external reference. */
5206 else if (declspecs->inline_p && storage_class != csc_static)
5207 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
5208 == flag_gnu89_inline);
5209 else
5210 DECL_EXTERNAL (decl) = !initialized;
5211
5212 /* Record absence of global scope for `static' or `auto'. */
5213 TREE_PUBLIC (decl)
5214 = !(storage_class == csc_static || storage_class == csc_auto);
5215
5216 /* For a function definition, record the argument information
5217 block where store_parm_decls will look for it. */
5218 if (funcdef_flag)
5219 current_function_arg_info = arg_info;
5220
5221 if (declspecs->default_int_p)
5222 C_FUNCTION_IMPLICIT_INT (decl) = 1;
5223
5224 /* Record presence of `inline', if it is reasonable. */
5225 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
5226 {
5227 if (declspecs->inline_p)
5228 pedwarn (input_location, 0, "cannot inline function %<main%>");
5229 }
5230 else if (declspecs->inline_p)
5231 /* Record that the function is declared `inline'. */
5232 DECL_DECLARED_INLINE_P (decl) = 1;
5233 }
5234 else
5235 {
5236 /* It's a variable. */
5237 /* An uninitialized decl with `extern' is a reference. */
5238 int extern_ref = !initialized && storage_class == csc_extern;
5239
5240 type = c_build_qualified_type (type, type_quals);
5241
5242 /* C99 6.2.2p7: It is invalid (compile-time undefined
5243 behavior) to create an 'extern' declaration for a
5244 variable if there is a global declaration that is
5245 'static' and the global declaration is not visible.
5246 (If the static declaration _is_ currently visible,
5247 the 'extern' declaration is taken to refer to that decl.) */
5248 if (extern_ref && current_scope != file_scope)
5249 {
5250 tree global_decl = identifier_global_value (declarator->u.id);
5251 tree visible_decl = lookup_name (declarator->u.id);
5252
5253 if (global_decl
5254 && global_decl != visible_decl
5255 && TREE_CODE (global_decl) == VAR_DECL
5256 && !TREE_PUBLIC (global_decl))
5257 error ("variable previously declared %<static%> redeclared "
5258 "%<extern%>");
5259 }
5260
5261 decl = build_decl (VAR_DECL, declarator->u.id, type);
5262 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5263 if (size_varies)
5264 C_DECL_VARIABLE_SIZE (decl) = 1;
5265
5266 if (declspecs->inline_p)
5267 pedwarn (input_location, 0, "variable %q+D declared %<inline%>", decl);
5268
5269 /* At file scope, an initialized extern declaration may follow
5270 a static declaration. In that case, DECL_EXTERNAL will be
5271 reset later in start_decl. */
5272 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
5273
5274 /* At file scope, the presence of a `static' or `register' storage
5275 class specifier, or the absence of all storage class specifiers
5276 makes this declaration a definition (perhaps tentative). Also,
5277 the absence of `static' makes it public. */
5278 if (current_scope == file_scope)
5279 {
5280 TREE_PUBLIC (decl) = storage_class != csc_static;
5281 TREE_STATIC (decl) = !extern_ref;
5282 }
5283 /* Not at file scope, only `static' makes a static definition. */
5284 else
5285 {
5286 TREE_STATIC (decl) = (storage_class == csc_static);
5287 TREE_PUBLIC (decl) = extern_ref;
5288 }
5289
5290 if (threadp)
5291 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
5292 }
5293
5294 if ((storage_class == csc_extern
5295 || (storage_class == csc_none
5296 && TREE_CODE (type) == FUNCTION_TYPE
5297 && !funcdef_flag))
5298 && variably_modified_type_p (type, NULL_TREE))
5299 {
5300 /* C99 6.7.5.2p2 */
5301 if (TREE_CODE (type) == FUNCTION_TYPE)
5302 error ("non-nested function with variably modified type");
5303 else
5304 error ("object with variably modified type must have no linkage");
5305 }
5306
5307 /* Record `register' declaration for warnings on &
5308 and in case doing stupid register allocation. */
5309
5310 if (storage_class == csc_register)
5311 {
5312 C_DECL_REGISTER (decl) = 1;
5313 DECL_REGISTER (decl) = 1;
5314 }
5315
5316 /* Record constancy and volatility. */
5317 c_apply_type_quals_to_decl (type_quals, decl);
5318
5319 /* If a type has volatile components, it should be stored in memory.
5320 Otherwise, the fact that those components are volatile
5321 will be ignored, and would even crash the compiler.
5322 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
5323 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
5324 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
5325 || TREE_CODE (decl) == RESULT_DECL))
5326 {
5327 /* It is not an error for a structure with volatile fields to
5328 be declared register, but reset DECL_REGISTER since it
5329 cannot actually go in a register. */
5330 int was_reg = C_DECL_REGISTER (decl);
5331 C_DECL_REGISTER (decl) = 0;
5332 DECL_REGISTER (decl) = 0;
5333 c_mark_addressable (decl);
5334 C_DECL_REGISTER (decl) = was_reg;
5335 }
5336
5337 /* This is the earliest point at which we might know the assembler
5338 name of a variable. Thus, if it's known before this, die horribly. */
5339 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
5340
5341 return decl;
5342 }
5343 }
5344 \f
5345 /* Decode the parameter-list info for a function type or function definition.
5346 The argument is the value returned by `get_parm_info' (or made in c-parse.c
5347 if there is an identifier list instead of a parameter decl list).
5348 These two functions are separate because when a function returns
5349 or receives functions then each is called multiple times but the order
5350 of calls is different. The last call to `grokparms' is always the one
5351 that contains the formal parameter names of a function definition.
5352
5353 Return a list of arg types to use in the FUNCTION_TYPE for this function.
5354
5355 FUNCDEF_FLAG is true for a function definition, false for
5356 a mere declaration. A nonempty identifier-list gets an error message
5357 when FUNCDEF_FLAG is false. */
5358
5359 static tree
5360 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
5361 {
5362 tree arg_types = arg_info->types;
5363
5364 if (funcdef_flag && arg_info->had_vla_unspec)
5365 {
5366 /* A function definition isn't function prototype scope C99 6.2.1p4. */
5367 /* C99 6.7.5.2p4 */
5368 error ("%<[*]%> not allowed in other than function prototype scope");
5369 }
5370
5371 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5372 warning (OPT_Wstrict_prototypes,
5373 "function declaration isn%'t a prototype");
5374
5375 if (arg_types == error_mark_node)
5376 return 0; /* don't set TYPE_ARG_TYPES in this case */
5377
5378 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5379 {
5380 if (!funcdef_flag)
5381 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
5382
5383 arg_info->parms = arg_info->types;
5384 arg_info->types = 0;
5385 return 0;
5386 }
5387 else
5388 {
5389 tree parm, type, typelt;
5390 unsigned int parmno;
5391 const char *errmsg;
5392
5393 /* If there is a parameter of incomplete type in a definition,
5394 this is an error. In a declaration this is valid, and a
5395 struct or union type may be completed later, before any calls
5396 or definition of the function. In the case where the tag was
5397 first declared within the parameter list, a warning has
5398 already been given. If a parameter has void type, then
5399 however the function cannot be defined or called, so
5400 warn. */
5401
5402 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5403 parm;
5404 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5405 {
5406 type = TREE_VALUE (typelt);
5407 if (type == error_mark_node)
5408 continue;
5409
5410 if (!COMPLETE_TYPE_P (type))
5411 {
5412 if (funcdef_flag)
5413 {
5414 if (DECL_NAME (parm))
5415 error ("parameter %u (%q+D) has incomplete type",
5416 parmno, parm);
5417 else
5418 error ("%Jparameter %u has incomplete type",
5419 parm, parmno);
5420
5421 TREE_VALUE (typelt) = error_mark_node;
5422 TREE_TYPE (parm) = error_mark_node;
5423 }
5424 else if (VOID_TYPE_P (type))
5425 {
5426 if (DECL_NAME (parm))
5427 warning (0, "parameter %u (%q+D) has void type",
5428 parmno, parm);
5429 else
5430 warning (0, "%Jparameter %u has void type",
5431 parm, parmno);
5432 }
5433 }
5434
5435 errmsg = targetm.invalid_parameter_type (type);
5436 if (errmsg)
5437 {
5438 error (errmsg);
5439 TREE_VALUE (typelt) = error_mark_node;
5440 TREE_TYPE (parm) = error_mark_node;
5441 }
5442
5443 if (DECL_NAME (parm) && TREE_USED (parm))
5444 warn_if_shadowing (parm);
5445 }
5446 return arg_types;
5447 }
5448 }
5449
5450 /* Take apart the current scope and return a c_arg_info structure with
5451 info on a parameter list just parsed.
5452
5453 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5454
5455 ELLIPSIS being true means the argument list ended in '...' so don't
5456 append a sentinel (void_list_node) to the end of the type-list. */
5457
5458 struct c_arg_info *
5459 get_parm_info (bool ellipsis)
5460 {
5461 struct c_binding *b = current_scope->bindings;
5462 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5463 struct c_arg_info);
5464 tree parms = 0;
5465 tree tags = 0;
5466 tree types = 0;
5467 tree others = 0;
5468
5469 static bool explained_incomplete_types = false;
5470 bool gave_void_only_once_err = false;
5471
5472 arg_info->parms = 0;
5473 arg_info->tags = 0;
5474 arg_info->types = 0;
5475 arg_info->others = 0;
5476 arg_info->pending_sizes = 0;
5477 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5478
5479 /* The bindings in this scope must not get put into a block.
5480 We will take care of deleting the binding nodes. */
5481 current_scope->bindings = 0;
5482
5483 /* This function is only called if there was *something* on the
5484 parameter list. */
5485 gcc_assert (b);
5486
5487 /* A parameter list consisting solely of 'void' indicates that the
5488 function takes no arguments. But if the 'void' is qualified
5489 (by 'const' or 'volatile'), or has a storage class specifier
5490 ('register'), then the behavior is undefined; issue an error.
5491 Typedefs for 'void' are OK (see DR#157). */
5492 if (b->prev == 0 /* one binding */
5493 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5494 && !DECL_NAME (b->decl) /* anonymous */
5495 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5496 {
5497 if (TREE_THIS_VOLATILE (b->decl)
5498 || TREE_READONLY (b->decl)
5499 || C_DECL_REGISTER (b->decl))
5500 error ("%<void%> as only parameter may not be qualified");
5501
5502 /* There cannot be an ellipsis. */
5503 if (ellipsis)
5504 error ("%<void%> must be the only parameter");
5505
5506 arg_info->types = void_list_node;
5507 return arg_info;
5508 }
5509
5510 if (!ellipsis)
5511 types = void_list_node;
5512
5513 /* Break up the bindings list into parms, tags, types, and others;
5514 apply sanity checks; purge the name-to-decl bindings. */
5515 while (b)
5516 {
5517 tree decl = b->decl;
5518 tree type = TREE_TYPE (decl);
5519 const char *keyword;
5520
5521 switch (TREE_CODE (decl))
5522 {
5523 case PARM_DECL:
5524 if (b->id)
5525 {
5526 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5527 I_SYMBOL_BINDING (b->id) = b->shadowed;
5528 }
5529
5530 /* Check for forward decls that never got their actual decl. */
5531 if (TREE_ASM_WRITTEN (decl))
5532 error ("parameter %q+D has just a forward declaration", decl);
5533 /* Check for (..., void, ...) and issue an error. */
5534 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5535 {
5536 if (!gave_void_only_once_err)
5537 {
5538 error ("%<void%> must be the only parameter");
5539 gave_void_only_once_err = true;
5540 }
5541 }
5542 else
5543 {
5544 /* Valid parameter, add it to the list. */
5545 TREE_CHAIN (decl) = parms;
5546 parms = decl;
5547
5548 /* Since there is a prototype, args are passed in their
5549 declared types. The back end may override this later. */
5550 DECL_ARG_TYPE (decl) = type;
5551 types = tree_cons (0, type, types);
5552 }
5553 break;
5554
5555 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5556 case UNION_TYPE: keyword = "union"; goto tag;
5557 case RECORD_TYPE: keyword = "struct"; goto tag;
5558 tag:
5559 /* Types may not have tag-names, in which case the type
5560 appears in the bindings list with b->id NULL. */
5561 if (b->id)
5562 {
5563 gcc_assert (I_TAG_BINDING (b->id) == b);
5564 I_TAG_BINDING (b->id) = b->shadowed;
5565 }
5566
5567 /* Warn about any struct, union or enum tags defined in a
5568 parameter list. The scope of such types is limited to
5569 the parameter list, which is rarely if ever desirable
5570 (it's impossible to call such a function with type-
5571 correct arguments). An anonymous union parm type is
5572 meaningful as a GNU extension, so don't warn for that. */
5573 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5574 {
5575 if (b->id)
5576 /* The %s will be one of 'struct', 'union', or 'enum'. */
5577 warning (0, "%<%s %E%> declared inside parameter list",
5578 keyword, b->id);
5579 else
5580 /* The %s will be one of 'struct', 'union', or 'enum'. */
5581 warning (0, "anonymous %s declared inside parameter list",
5582 keyword);
5583
5584 if (!explained_incomplete_types)
5585 {
5586 warning (0, "its scope is only this definition or declaration,"
5587 " which is probably not what you want");
5588 explained_incomplete_types = true;
5589 }
5590 }
5591
5592 tags = tree_cons (b->id, decl, tags);
5593 break;
5594
5595 case CONST_DECL:
5596 case TYPE_DECL:
5597 case FUNCTION_DECL:
5598 /* CONST_DECLs appear here when we have an embedded enum,
5599 and TYPE_DECLs appear here when we have an embedded struct
5600 or union. No warnings for this - we already warned about the
5601 type itself. FUNCTION_DECLs appear when there is an implicit
5602 function declaration in the parameter list. */
5603
5604 TREE_CHAIN (decl) = others;
5605 others = decl;
5606 /* fall through */
5607
5608 case ERROR_MARK:
5609 /* error_mark_node appears here when we have an undeclared
5610 variable. Just throw it away. */
5611 if (b->id)
5612 {
5613 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5614 I_SYMBOL_BINDING (b->id) = b->shadowed;
5615 }
5616 break;
5617
5618 /* Other things that might be encountered. */
5619 case LABEL_DECL:
5620 case VAR_DECL:
5621 default:
5622 gcc_unreachable ();
5623 }
5624
5625 b = free_binding_and_advance (b);
5626 }
5627
5628 arg_info->parms = parms;
5629 arg_info->tags = tags;
5630 arg_info->types = types;
5631 arg_info->others = others;
5632 arg_info->pending_sizes = get_pending_sizes ();
5633 return arg_info;
5634 }
5635 \f
5636 /* Get the struct, enum or union (CODE says which) with tag NAME.
5637 Define the tag as a forward-reference if it is not defined.
5638 Return a c_typespec structure for the type specifier. */
5639
5640 struct c_typespec
5641 parser_xref_tag (enum tree_code code, tree name, location_t loc)
5642 {
5643 struct c_typespec ret;
5644 tree ref;
5645 location_t refloc;
5646
5647 ret.expr = NULL_TREE;
5648 ret.expr_const_operands = true;
5649
5650 /* If a cross reference is requested, look up the type
5651 already defined for this tag and return it. */
5652
5653 ref = lookup_tag (code, name, 0, &refloc);
5654 /* If this is the right type of tag, return what we found.
5655 (This reference will be shadowed by shadow_tag later if appropriate.)
5656 If this is the wrong type of tag, do not return it. If it was the
5657 wrong type in the same scope, we will have had an error
5658 message already; if in a different scope and declaring
5659 a name, pending_xref_error will give an error message; but if in a
5660 different scope and not declaring a name, this tag should
5661 shadow the previous declaration of a different type of tag, and
5662 this would not work properly if we return the reference found.
5663 (For example, with "struct foo" in an outer scope, "union foo;"
5664 must shadow that tag with a new one of union type.) */
5665 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5666 if (ref && TREE_CODE (ref) == code)
5667 {
5668 if (C_TYPE_DEFINED_IN_STRUCT (ref)
5669 && loc != UNKNOWN_LOCATION
5670 && warn_cxx_compat)
5671 {
5672 switch (code)
5673 {
5674 case ENUMERAL_TYPE:
5675 warning_at (loc, OPT_Wc___compat,
5676 ("enum type defined in struct or union "
5677 "is not visible in C++"));
5678 inform (refloc, "enum type defined here");
5679 break;
5680 case RECORD_TYPE:
5681 warning_at (loc, OPT_Wc___compat,
5682 ("struct defined in struct or union "
5683 "is not visible in C++"));
5684 inform (refloc, "struct defined here");
5685 break;
5686 case UNION_TYPE:
5687 warning_at (loc, OPT_Wc___compat,
5688 ("union defined in struct or union "
5689 "is not visible in C++"));
5690 inform (refloc, "union defined here");
5691 break;
5692 default:
5693 gcc_unreachable();
5694 }
5695 }
5696
5697 ret.spec = ref;
5698 return ret;
5699 }
5700
5701 /* If no such tag is yet defined, create a forward-reference node
5702 and record it as the "definition".
5703 When a real declaration of this type is found,
5704 the forward-reference will be altered into a real type. */
5705
5706 ref = make_node (code);
5707 if (code == ENUMERAL_TYPE)
5708 {
5709 /* Give the type a default layout like unsigned int
5710 to avoid crashing if it does not get defined. */
5711 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
5712 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5713 TYPE_USER_ALIGN (ref) = 0;
5714 TYPE_UNSIGNED (ref) = 1;
5715 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5716 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5717 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5718 }
5719
5720 pushtag (name, ref, loc);
5721
5722 ret.spec = ref;
5723 return ret;
5724 }
5725
5726 /* Get the struct, enum or union (CODE says which) with tag NAME.
5727 Define the tag as a forward-reference if it is not defined.
5728 Return a tree for the type. */
5729
5730 tree
5731 xref_tag (enum tree_code code, tree name)
5732 {
5733 return parser_xref_tag (code, name, UNKNOWN_LOCATION).spec;
5734 }
5735 \f
5736 /* Make sure that the tag NAME is defined *in the current scope*
5737 at least as a forward reference.
5738 CODE says which kind of tag NAME ought to be.
5739
5740 This stores the current value of the file static IN_STRUCT in
5741 *ENCLOSING_IN_STRUCT, and sets IN_STRUCT to true. Similarly, this
5742 sets STRUCT_TYPES in *ENCLOSING_STRUCT_TYPES, and sets STRUCT_TYPES
5743 to an empty vector. The old values are restored in
5744 finish_struct. */
5745
5746 tree
5747 start_struct (enum tree_code code, tree name, bool *enclosing_in_struct,
5748 VEC(tree,heap) **enclosing_struct_types, location_t loc)
5749 {
5750 /* If there is already a tag defined at this scope
5751 (as a forward reference), just return it. */
5752
5753 tree ref = NULL_TREE;
5754 location_t refloc = UNKNOWN_LOCATION;
5755
5756 if (name != NULL_TREE)
5757 ref = lookup_tag (code, name, 1, &refloc);
5758 if (ref && TREE_CODE (ref) == code)
5759 {
5760 if (TYPE_SIZE (ref))
5761 {
5762 if (code == UNION_TYPE)
5763 error_at (loc, "redefinition of %<union %E%>", name);
5764 else
5765 error_at (loc, "redefinition of %<struct %E%>", name);
5766 if (refloc != UNKNOWN_LOCATION)
5767 inform (refloc, "originally defined here");
5768 /* Don't create structures using a name already in use. */
5769 ref = NULL_TREE;
5770 }
5771 else if (C_TYPE_BEING_DEFINED (ref))
5772 {
5773 if (code == UNION_TYPE)
5774 error_at (loc, "nested redefinition of %<union %E%>", name);
5775 else
5776 error_at (loc, "nested redefinition of %<struct %E%>", name);
5777 /* Don't bother to report "originally defined here" for a
5778 nested redefinition; the original definition should be
5779 obvious. */
5780 /* Don't create structures that contain themselves. */
5781 ref = NULL_TREE;
5782 }
5783 }
5784
5785 /* Otherwise create a forward-reference just so the tag is in scope. */
5786
5787 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5788 {
5789 ref = make_node (code);
5790 pushtag (name, ref, loc);
5791 }
5792
5793 C_TYPE_BEING_DEFINED (ref) = 1;
5794 TYPE_PACKED (ref) = flag_pack_struct;
5795
5796 *enclosing_in_struct = in_struct;
5797 *enclosing_struct_types = struct_types;
5798 in_struct = true;
5799 struct_types = VEC_alloc(tree, heap, 0);
5800
5801 /* FIXME: This will issue a warning for a use of a type defined
5802 within a statement expr used within sizeof, et. al. This is not
5803 terribly serious as C++ doesn't permit statement exprs within
5804 sizeof anyhow. */
5805 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
5806 warning_at (loc, OPT_Wc___compat,
5807 "defining type in %qs expression is invalid in C++",
5808 (in_sizeof
5809 ? "sizeof"
5810 : (in_typeof ? "typeof" : "alignof")));
5811
5812 return ref;
5813 }
5814
5815 /* Process the specs, declarator and width (NULL if omitted)
5816 of a structure component, returning a FIELD_DECL node.
5817 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5818 DECL_ATTRS is as for grokdeclarator.
5819
5820 LOC is the location of the structure component.
5821
5822 This is done during the parsing of the struct declaration.
5823 The FIELD_DECL nodes are chained together and the lot of them
5824 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5825
5826 tree
5827 grokfield (location_t loc,
5828 struct c_declarator *declarator, struct c_declspecs *declspecs,
5829 tree width, tree *decl_attrs)
5830 {
5831 tree value;
5832
5833 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5834 && width == NULL_TREE)
5835 {
5836 /* This is an unnamed decl.
5837
5838 If we have something of the form "union { list } ;" then this
5839 is the anonymous union extension. Similarly for struct.
5840
5841 If this is something of the form "struct foo;", then
5842 If MS extensions are enabled, this is handled as an
5843 anonymous struct.
5844 Otherwise this is a forward declaration of a structure tag.
5845
5846 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5847 If MS extensions are enabled and foo names a structure, then
5848 again this is an anonymous struct.
5849 Otherwise this is an error.
5850
5851 Oh what a horrid tangled web we weave. I wonder if MS consciously
5852 took this from Plan 9 or if it was an accident of implementation
5853 that took root before someone noticed the bug... */
5854
5855 tree type = declspecs->type;
5856 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5857 || TREE_CODE (type) == UNION_TYPE);
5858 bool ok = false;
5859
5860 if (type_ok
5861 && (flag_ms_extensions || !declspecs->typedef_p))
5862 {
5863 if (flag_ms_extensions)
5864 ok = true;
5865 else if (flag_iso)
5866 ok = false;
5867 else if (TYPE_NAME (type) == NULL)
5868 ok = true;
5869 else
5870 ok = false;
5871 }
5872 if (!ok)
5873 {
5874 pedwarn (loc, 0, "declaration does not declare anything");
5875 return NULL_TREE;
5876 }
5877 pedwarn (loc, OPT_pedantic, "ISO C doesn%'t support unnamed structs/unions");
5878 }
5879
5880 value = grokdeclarator (declarator, declspecs, FIELD, false,
5881 width ? &width : NULL, decl_attrs, NULL, NULL,
5882 DEPRECATED_NORMAL);
5883
5884 finish_decl (value, NULL_TREE, NULL_TREE, NULL_TREE);
5885 DECL_INITIAL (value) = width;
5886
5887 return value;
5888 }
5889 \f
5890 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5891 the list such that this does not present a problem later. */
5892
5893 static void
5894 detect_field_duplicates (tree fieldlist)
5895 {
5896 tree x, y;
5897 int timeout = 10;
5898
5899 /* First, see if there are more than "a few" fields.
5900 This is trivially true if there are zero or one fields. */
5901 if (!fieldlist)
5902 return;
5903 x = TREE_CHAIN (fieldlist);
5904 if (!x)
5905 return;
5906 do {
5907 timeout--;
5908 x = TREE_CHAIN (x);
5909 } while (timeout > 0 && x);
5910
5911 /* If there were "few" fields, avoid the overhead of allocating
5912 a hash table. Instead just do the nested traversal thing. */
5913 if (timeout > 0)
5914 {
5915 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5916 if (DECL_NAME (x))
5917 {
5918 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5919 if (DECL_NAME (y) == DECL_NAME (x))
5920 {
5921 error ("duplicate member %q+D", x);
5922 DECL_NAME (x) = NULL_TREE;
5923 }
5924 }
5925 }
5926 else
5927 {
5928 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5929 void **slot;
5930
5931 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5932 if ((y = DECL_NAME (x)) != 0)
5933 {
5934 slot = htab_find_slot (htab, y, INSERT);
5935 if (*slot)
5936 {
5937 error ("duplicate member %q+D", x);
5938 DECL_NAME (x) = NULL_TREE;
5939 }
5940 *slot = y;
5941 }
5942
5943 htab_delete (htab);
5944 }
5945 }
5946
5947 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5948 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5949 ATTRIBUTES are attributes to be applied to the structure.
5950
5951 ENCLOSING_IN_STRUCT is the value of IN_STRUCT, and
5952 ENCLOSING_STRUCT_TYPES is the value of STRUCT_TYPES, when the
5953 struct was started. This sets the C_TYPE_DEFINED_IN_STRUCT flag
5954 for any type defined in the current struct. */
5955
5956 tree
5957 finish_struct (tree t, tree fieldlist, tree attributes,
5958 bool enclosing_in_struct,
5959 VEC(tree,heap) *enclosing_struct_types)
5960 {
5961 tree x;
5962 bool toplevel = file_scope == current_scope;
5963 int saw_named_field;
5964 unsigned int ix;
5965
5966 /* If this type was previously laid out as a forward reference,
5967 make sure we lay it out again. */
5968
5969 TYPE_SIZE (t) = 0;
5970
5971 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5972
5973 if (pedantic)
5974 {
5975 for (x = fieldlist; x; x = TREE_CHAIN (x))
5976 if (DECL_NAME (x) != 0)
5977 break;
5978
5979 if (x == 0)
5980 {
5981 if (TREE_CODE (t) == UNION_TYPE)
5982 {
5983 if (fieldlist)
5984 pedwarn (input_location, OPT_pedantic, "union has no named members");
5985 else
5986 pedwarn (input_location, OPT_pedantic, "union has no members");
5987 }
5988 else
5989 {
5990 if (fieldlist)
5991 pedwarn (input_location, OPT_pedantic, "struct has no named members");
5992 else
5993 pedwarn (input_location, OPT_pedantic, "struct has no members");
5994 }
5995 }
5996 }
5997
5998 /* Install struct as DECL_CONTEXT of each field decl.
5999 Also process specified field sizes, found in the DECL_INITIAL,
6000 storing 0 there after the type has been changed to precision equal
6001 to its width, rather than the precision of the specified standard
6002 type. (Correct layout requires the original type to have been preserved
6003 until now.) */
6004
6005 saw_named_field = 0;
6006 for (x = fieldlist; x; x = TREE_CHAIN (x))
6007 {
6008 if (TREE_TYPE (x) == error_mark_node)
6009 continue;
6010
6011 DECL_CONTEXT (x) = t;
6012
6013 /* If any field is const, the structure type is pseudo-const. */
6014 if (TREE_READONLY (x))
6015 C_TYPE_FIELDS_READONLY (t) = 1;
6016 else
6017 {
6018 /* A field that is pseudo-const makes the structure likewise. */
6019 tree t1 = TREE_TYPE (x);
6020 while (TREE_CODE (t1) == ARRAY_TYPE)
6021 t1 = TREE_TYPE (t1);
6022 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
6023 && C_TYPE_FIELDS_READONLY (t1))
6024 C_TYPE_FIELDS_READONLY (t) = 1;
6025 }
6026
6027 /* Any field that is volatile means variables of this type must be
6028 treated in some ways as volatile. */
6029 if (TREE_THIS_VOLATILE (x))
6030 C_TYPE_FIELDS_VOLATILE (t) = 1;
6031
6032 /* Any field of nominal variable size implies structure is too. */
6033 if (C_DECL_VARIABLE_SIZE (x))
6034 C_TYPE_VARIABLE_SIZE (t) = 1;
6035
6036 if (DECL_INITIAL (x))
6037 {
6038 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
6039 DECL_SIZE (x) = bitsize_int (width);
6040 DECL_BIT_FIELD (x) = 1;
6041 SET_DECL_C_BIT_FIELD (x);
6042 }
6043
6044 if (TYPE_PACKED (t)
6045 && (DECL_BIT_FIELD (x)
6046 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
6047 DECL_PACKED (x) = 1;
6048
6049 /* Detect flexible array member in an invalid context. */
6050 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6051 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6052 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6053 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6054 {
6055 if (TREE_CODE (t) == UNION_TYPE)
6056 {
6057 error ("%Jflexible array member in union", x);
6058 TREE_TYPE (x) = error_mark_node;
6059 }
6060 else if (TREE_CHAIN (x) != NULL_TREE)
6061 {
6062 error ("%Jflexible array member not at end of struct", x);
6063 TREE_TYPE (x) = error_mark_node;
6064 }
6065 else if (!saw_named_field)
6066 {
6067 error ("%Jflexible array member in otherwise empty struct", x);
6068 TREE_TYPE (x) = error_mark_node;
6069 }
6070 }
6071
6072 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
6073 && flexible_array_type_p (TREE_TYPE (x)))
6074 pedwarn (input_location, OPT_pedantic,
6075 "%Jinvalid use of structure with flexible array member", x);
6076
6077 if (DECL_NAME (x))
6078 saw_named_field = 1;
6079 }
6080
6081 detect_field_duplicates (fieldlist);
6082
6083 /* Now we have the nearly final fieldlist. Record it,
6084 then lay out the structure or union (including the fields). */
6085
6086 TYPE_FIELDS (t) = fieldlist;
6087
6088 layout_type (t);
6089
6090 /* Give bit-fields their proper types. */
6091 {
6092 tree *fieldlistp = &fieldlist;
6093 while (*fieldlistp)
6094 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
6095 && TREE_TYPE (*fieldlistp) != error_mark_node)
6096 {
6097 unsigned HOST_WIDE_INT width
6098 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
6099 tree type = TREE_TYPE (*fieldlistp);
6100 if (width != TYPE_PRECISION (type))
6101 {
6102 TREE_TYPE (*fieldlistp)
6103 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
6104 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
6105 }
6106 DECL_INITIAL (*fieldlistp) = 0;
6107 }
6108 else
6109 fieldlistp = &TREE_CHAIN (*fieldlistp);
6110 }
6111
6112 /* Now we have the truly final field list.
6113 Store it in this type and in the variants. */
6114
6115 TYPE_FIELDS (t) = fieldlist;
6116
6117 /* If there are lots of fields, sort so we can look through them fast.
6118 We arbitrarily consider 16 or more elts to be "a lot". */
6119
6120 {
6121 int len = 0;
6122
6123 for (x = fieldlist; x; x = TREE_CHAIN (x))
6124 {
6125 if (len > 15 || DECL_NAME (x) == NULL)
6126 break;
6127 len += 1;
6128 }
6129
6130 if (len > 15)
6131 {
6132 tree *field_array;
6133 struct lang_type *space;
6134 struct sorted_fields_type *space2;
6135
6136 len += list_length (x);
6137
6138 /* Use the same allocation policy here that make_node uses, to
6139 ensure that this lives as long as the rest of the struct decl.
6140 All decls in an inline function need to be saved. */
6141
6142 space = GGC_CNEW (struct lang_type);
6143 space2 = GGC_NEWVAR (struct sorted_fields_type,
6144 sizeof (struct sorted_fields_type) + len * sizeof (tree));
6145
6146 len = 0;
6147 space->s = space2;
6148 field_array = &space2->elts[0];
6149 for (x = fieldlist; x; x = TREE_CHAIN (x))
6150 {
6151 field_array[len++] = x;
6152
6153 /* If there is anonymous struct or union, break out of the loop. */
6154 if (DECL_NAME (x) == NULL)
6155 break;
6156 }
6157 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
6158 if (x == NULL)
6159 {
6160 TYPE_LANG_SPECIFIC (t) = space;
6161 TYPE_LANG_SPECIFIC (t)->s->len = len;
6162 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
6163 qsort (field_array, len, sizeof (tree), field_decl_cmp);
6164 }
6165 }
6166 }
6167
6168 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6169 {
6170 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6171 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
6172 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
6173 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
6174 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
6175 }
6176
6177 /* If this was supposed to be a transparent union, but we can't
6178 make it one, warn and turn off the flag. */
6179 if (TREE_CODE (t) == UNION_TYPE
6180 && TYPE_TRANSPARENT_UNION (t)
6181 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
6182 {
6183 TYPE_TRANSPARENT_UNION (t) = 0;
6184 warning (0, "union cannot be made transparent");
6185 }
6186
6187 /* If this structure or union completes the type of any previous
6188 variable declaration, lay it out and output its rtl. */
6189 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
6190 x;
6191 x = TREE_CHAIN (x))
6192 {
6193 tree decl = TREE_VALUE (x);
6194 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6195 layout_array_type (TREE_TYPE (decl));
6196 if (TREE_CODE (decl) != TYPE_DECL)
6197 {
6198 layout_decl (decl, 0);
6199 if (c_dialect_objc ())
6200 objc_check_decl (decl);
6201 rest_of_decl_compilation (decl, toplevel, 0);
6202 if (!toplevel)
6203 expand_decl (decl);
6204 }
6205 }
6206 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
6207
6208 /* Finish debugging output for this type. */
6209 rest_of_type_compilation (t, toplevel);
6210
6211 /* If we're inside a function proper, i.e. not file-scope and not still
6212 parsing parameters, then arrange for the size of a variable sized type
6213 to be bound now. */
6214 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
6215 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
6216
6217 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
6218 the current struct. We do this now at the end of the struct
6219 because the flag is used to issue visibility warnings when using
6220 -Wc++-compat, and we only want to issue those warnings if the
6221 type is referenced outside of the struct declaration. */
6222 for (ix = 0; VEC_iterate (tree, struct_types, ix, x); ++ix)
6223 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
6224
6225 VEC_free (tree, heap, struct_types);
6226
6227 in_struct = enclosing_in_struct;
6228 struct_types = enclosing_struct_types;
6229
6230 /* If this struct is defined inside a struct, add it to
6231 STRUCT_TYPES. */
6232 if (in_struct && !in_sizeof && !in_typeof && !in_alignof)
6233 VEC_safe_push (tree, heap, struct_types, t);
6234
6235 return t;
6236 }
6237
6238 /* Lay out the type T, and its element type, and so on. */
6239
6240 static void
6241 layout_array_type (tree t)
6242 {
6243 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6244 layout_array_type (TREE_TYPE (t));
6245 layout_type (t);
6246 }
6247 \f
6248 /* Begin compiling the definition of an enumeration type.
6249 NAME is its name (or null if anonymous).
6250 Returns the type object, as yet incomplete.
6251 Also records info about it so that build_enumerator
6252 may be used to declare the individual values as they are read. */
6253
6254 tree
6255 start_enum (struct c_enum_contents *the_enum, tree name, location_t loc)
6256 {
6257 tree enumtype = NULL_TREE;
6258 location_t enumloc = UNKNOWN_LOCATION;
6259
6260 /* If this is the real definition for a previous forward reference,
6261 fill in the contents in the same object that used to be the
6262 forward reference. */
6263
6264 if (name != NULL_TREE)
6265 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
6266
6267 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
6268 {
6269 enumtype = make_node (ENUMERAL_TYPE);
6270 pushtag (name, enumtype, loc);
6271 }
6272
6273 if (C_TYPE_BEING_DEFINED (enumtype))
6274 error_at (loc, "nested redefinition of %<enum %E%>", name);
6275
6276 C_TYPE_BEING_DEFINED (enumtype) = 1;
6277
6278 if (TYPE_VALUES (enumtype) != 0)
6279 {
6280 /* This enum is a named one that has been declared already. */
6281 error_at (loc, "redeclaration of %<enum %E%>", name);
6282 if (enumloc != UNKNOWN_LOCATION)
6283 inform (enumloc, "originally defined here");
6284
6285 /* Completely replace its old definition.
6286 The old enumerators remain defined, however. */
6287 TYPE_VALUES (enumtype) = 0;
6288 }
6289
6290 the_enum->enum_next_value = integer_zero_node;
6291 the_enum->enum_overflow = 0;
6292
6293 if (flag_short_enums)
6294 TYPE_PACKED (enumtype) = 1;
6295
6296 /* FIXME: This will issue a warning for a use of a type defined
6297 within sizeof in a statement expr. This is not terribly serious
6298 as C++ doesn't permit statement exprs within sizeof anyhow. */
6299 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
6300 warning_at (loc, OPT_Wc___compat,
6301 "defining type in %qs expression is invalid in C++",
6302 (in_sizeof
6303 ? "sizeof"
6304 : (in_typeof ? "typeof" : "alignof")));
6305
6306 return enumtype;
6307 }
6308
6309 /* After processing and defining all the values of an enumeration type,
6310 install their decls in the enumeration type and finish it off.
6311 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
6312 and ATTRIBUTES are the specified attributes.
6313 Returns ENUMTYPE. */
6314
6315 tree
6316 finish_enum (tree enumtype, tree values, tree attributes)
6317 {
6318 tree pair, tem;
6319 tree minnode = 0, maxnode = 0;
6320 int precision, unsign;
6321 bool toplevel = (file_scope == current_scope);
6322 struct lang_type *lt;
6323
6324 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6325
6326 /* Calculate the maximum value of any enumerator in this type. */
6327
6328 if (values == error_mark_node)
6329 minnode = maxnode = integer_zero_node;
6330 else
6331 {
6332 minnode = maxnode = TREE_VALUE (values);
6333 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
6334 {
6335 tree value = TREE_VALUE (pair);
6336 if (tree_int_cst_lt (maxnode, value))
6337 maxnode = value;
6338 if (tree_int_cst_lt (value, minnode))
6339 minnode = value;
6340 }
6341 }
6342
6343 /* Construct the final type of this enumeration. It is the same
6344 as one of the integral types - the narrowest one that fits, except
6345 that normally we only go as narrow as int - and signed iff any of
6346 the values are negative. */
6347 unsign = (tree_int_cst_sgn (minnode) >= 0);
6348 precision = MAX (tree_int_cst_min_precision (minnode, unsign),
6349 tree_int_cst_min_precision (maxnode, unsign));
6350
6351 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
6352 {
6353 tem = c_common_type_for_size (precision, unsign);
6354 if (tem == NULL)
6355 {
6356 warning (0, "enumeration values exceed range of largest integer");
6357 tem = long_long_integer_type_node;
6358 }
6359 }
6360 else
6361 tem = unsign ? unsigned_type_node : integer_type_node;
6362
6363 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
6364 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
6365 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
6366 TYPE_SIZE (enumtype) = 0;
6367
6368 /* If the precision of the type was specific with an attribute and it
6369 was too small, give an error. Otherwise, use it. */
6370 if (TYPE_PRECISION (enumtype))
6371 {
6372 if (precision > TYPE_PRECISION (enumtype))
6373 error ("specified mode too small for enumeral values");
6374 }
6375 else
6376 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
6377
6378 layout_type (enumtype);
6379
6380 if (values != error_mark_node)
6381 {
6382 /* Change the type of the enumerators to be the enum type. We
6383 need to do this irrespective of the size of the enum, for
6384 proper type checking. Replace the DECL_INITIALs of the
6385 enumerators, and the value slots of the list, with copies
6386 that have the enum type; they cannot be modified in place
6387 because they may be shared (e.g. integer_zero_node) Finally,
6388 change the purpose slots to point to the names of the decls. */
6389 for (pair = values; pair; pair = TREE_CHAIN (pair))
6390 {
6391 tree enu = TREE_PURPOSE (pair);
6392 tree ini = DECL_INITIAL (enu);
6393
6394 TREE_TYPE (enu) = enumtype;
6395
6396 /* The ISO C Standard mandates enumerators to have type int,
6397 even though the underlying type of an enum type is
6398 unspecified. However, GCC allows enumerators of any
6399 integer type as an extensions. build_enumerator()
6400 converts any enumerators that fit in an int to type int,
6401 to avoid promotions to unsigned types when comparing
6402 integers with enumerators that fit in the int range.
6403 When -pedantic is given, build_enumerator() would have
6404 already warned about those that don't fit. Here we
6405 convert the rest to the enumerator type. */
6406 if (TREE_TYPE (ini) != integer_type_node)
6407 ini = convert (enumtype, ini);
6408
6409 DECL_INITIAL (enu) = ini;
6410 TREE_PURPOSE (pair) = DECL_NAME (enu);
6411 TREE_VALUE (pair) = ini;
6412 }
6413
6414 TYPE_VALUES (enumtype) = values;
6415 }
6416
6417 /* Record the min/max values so that we can warn about bit-field
6418 enumerations that are too small for the values. */
6419 lt = GGC_CNEW (struct lang_type);
6420 lt->enum_min = minnode;
6421 lt->enum_max = maxnode;
6422 TYPE_LANG_SPECIFIC (enumtype) = lt;
6423
6424 /* Fix up all variant types of this enum type. */
6425 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
6426 {
6427 if (tem == enumtype)
6428 continue;
6429 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
6430 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
6431 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
6432 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
6433 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
6434 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
6435 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
6436 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
6437 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
6438 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
6439 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
6440 }
6441
6442 /* Finish debugging output for this type. */
6443 rest_of_type_compilation (enumtype, toplevel);
6444
6445 /* If this enum is defined inside a struct, add it to
6446 STRUCT_TYPES. */
6447 if (in_struct && !in_sizeof && !in_typeof && !in_alignof)
6448 VEC_safe_push (tree, heap, struct_types, enumtype);
6449
6450 return enumtype;
6451 }
6452
6453 /* Build and install a CONST_DECL for one value of the
6454 current enumeration type (one that was begun with start_enum).
6455 Return a tree-list containing the CONST_DECL and its value.
6456 Assignment of sequential values by default is handled here. */
6457
6458 tree
6459 build_enumerator (struct c_enum_contents *the_enum, tree name, tree value,
6460 location_t value_loc)
6461 {
6462 tree decl, type;
6463
6464 /* Validate and default VALUE. */
6465
6466 if (value != 0)
6467 {
6468 /* Don't issue more errors for error_mark_node (i.e. an
6469 undeclared identifier) - just ignore the value expression. */
6470 if (value == error_mark_node)
6471 value = 0;
6472 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
6473 {
6474 error ("enumerator value for %qE is not an integer constant", name);
6475 value = 0;
6476 }
6477 else
6478 {
6479 if (TREE_CODE (value) != INTEGER_CST)
6480 {
6481 value = c_fully_fold (value, false, NULL);
6482 if (TREE_CODE (value) == INTEGER_CST)
6483 pedwarn (value_loc, OPT_pedantic,
6484 "enumerator value for %qE is not an integer "
6485 "constant expression", name);
6486 }
6487 if (TREE_CODE (value) != INTEGER_CST)
6488 {
6489 error ("enumerator value for %qE is not an integer constant",
6490 name);
6491 value = 0;
6492 }
6493 else
6494 {
6495 value = default_conversion (value);
6496 constant_expression_warning (value);
6497 }
6498 }
6499 }
6500
6501 /* Default based on previous value. */
6502 /* It should no longer be possible to have NON_LVALUE_EXPR
6503 in the default. */
6504 if (value == 0)
6505 {
6506 value = the_enum->enum_next_value;
6507 if (the_enum->enum_overflow)
6508 error ("overflow in enumeration values");
6509 }
6510 /* Even though the underlying type of an enum is unspecified, the
6511 type of enumeration constants is explicitly defined as int
6512 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
6513 an extension. */
6514 else if (!int_fits_type_p (value, integer_type_node))
6515 pedwarn (value_loc, OPT_pedantic,
6516 "ISO C restricts enumerator values to range of %<int%>");
6517
6518 /* The ISO C Standard mandates enumerators to have type int, even
6519 though the underlying type of an enum type is unspecified.
6520 However, GCC allows enumerators of any integer type as an
6521 extensions. Here we convert any enumerators that fit in an int
6522 to type int, to avoid promotions to unsigned types when comparing
6523 integers with enumerators that fit in the int range. When
6524 -pedantic is given, we would have already warned about those that
6525 don't fit. We have to do this here rather than in finish_enum
6526 because this value may be used to define more enumerators. */
6527 if (int_fits_type_p (value, integer_type_node))
6528 value = convert (integer_type_node, value);
6529
6530 /* Set basis for default for next value. */
6531 the_enum->enum_next_value
6532 = build_binary_op
6533 (EXPR_HAS_LOCATION (value) ? EXPR_LOCATION (value) : input_location,
6534 PLUS_EXPR, value, integer_one_node, 0);
6535 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
6536
6537 /* Now create a declaration for the enum value name. */
6538
6539 type = TREE_TYPE (value);
6540 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6541 TYPE_PRECISION (integer_type_node)),
6542 (TYPE_PRECISION (type)
6543 >= TYPE_PRECISION (integer_type_node)
6544 && TYPE_UNSIGNED (type)));
6545
6546 decl = build_decl (CONST_DECL, name, type);
6547 DECL_INITIAL (decl) = convert (type, value);
6548 pushdecl (decl);
6549
6550 return tree_cons (decl, value, NULL_TREE);
6551 }
6552
6553 \f
6554 /* Create the FUNCTION_DECL for a function definition.
6555 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6556 the declaration; they describe the function's name and the type it returns,
6557 but twisted together in a fashion that parallels the syntax of C.
6558
6559 This function creates a binding context for the function body
6560 as well as setting up the FUNCTION_DECL in current_function_decl.
6561
6562 Returns 1 on success. If the DECLARATOR is not suitable for a function
6563 (it defines a datum instead), we return 0, which tells
6564 yyparse to report a parse error. */
6565
6566 int
6567 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6568 tree attributes)
6569 {
6570 tree decl1, old_decl;
6571 tree restype, resdecl;
6572 struct c_label_context_se *nstack_se;
6573 struct c_label_context_vm *nstack_vm;
6574
6575 current_function_returns_value = 0; /* Assume, until we see it does. */
6576 current_function_returns_null = 0;
6577 current_function_returns_abnormally = 0;
6578 warn_about_return_type = 0;
6579 c_switch_stack = NULL;
6580
6581 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6582 nstack_se->labels_def = NULL;
6583 nstack_se->labels_used = NULL;
6584 nstack_se->next = label_context_stack_se;
6585 label_context_stack_se = nstack_se;
6586
6587 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6588 nstack_vm->labels_def = NULL;
6589 nstack_vm->labels_used = NULL;
6590 nstack_vm->scope = 0;
6591 nstack_vm->next = label_context_stack_vm;
6592 label_context_stack_vm = nstack_vm;
6593
6594 /* Indicate no valid break/continue context by setting these variables
6595 to some non-null, non-label value. We'll notice and emit the proper
6596 error message in c_finish_bc_stmt. */
6597 c_break_label = c_cont_label = size_zero_node;
6598
6599 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
6600 &attributes, NULL, NULL, DEPRECATED_NORMAL);
6601
6602 /* If the declarator is not suitable for a function definition,
6603 cause a syntax error. */
6604 if (decl1 == 0)
6605 {
6606 label_context_stack_se = label_context_stack_se->next;
6607 label_context_stack_vm = label_context_stack_vm->next;
6608 return 0;
6609 }
6610
6611 decl_attributes (&decl1, attributes, 0);
6612
6613 if (DECL_DECLARED_INLINE_P (decl1)
6614 && DECL_UNINLINABLE (decl1)
6615 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6616 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6617 decl1);
6618
6619 /* Handle gnu_inline attribute. */
6620 if (declspecs->inline_p
6621 && !flag_gnu89_inline
6622 && TREE_CODE (decl1) == FUNCTION_DECL
6623 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
6624 || current_function_decl))
6625 {
6626 if (declspecs->storage_class != csc_static)
6627 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6628 }
6629
6630 announce_function (decl1);
6631
6632 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6633 {
6634 error ("return type is an incomplete type");
6635 /* Make it return void instead. */
6636 TREE_TYPE (decl1)
6637 = build_function_type (void_type_node,
6638 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6639 }
6640
6641 if (warn_about_return_type)
6642 pedwarn_c99 (input_location, flag_isoc99 ? 0
6643 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
6644 "return type defaults to %<int%>");
6645
6646 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6647 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6648 DECL_INITIAL (decl1) = error_mark_node;
6649
6650 /* If this definition isn't a prototype and we had a prototype declaration
6651 before, copy the arg type info from that prototype. */
6652 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6653 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6654 old_decl = 0;
6655 current_function_prototype_locus = UNKNOWN_LOCATION;
6656 current_function_prototype_built_in = false;
6657 current_function_prototype_arg_types = NULL_TREE;
6658 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6659 {
6660 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6661 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6662 TREE_TYPE (TREE_TYPE (old_decl))))
6663 {
6664 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6665 TREE_TYPE (decl1));
6666 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6667 current_function_prototype_built_in
6668 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6669 current_function_prototype_arg_types
6670 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6671 }
6672 if (TREE_PUBLIC (decl1))
6673 {
6674 /* If there is an external prototype declaration of this
6675 function, record its location but do not copy information
6676 to this decl. This may be an invisible declaration
6677 (built-in or in a scope which has finished) or simply
6678 have more refined argument types than any declaration
6679 found above. */
6680 struct c_binding *b;
6681 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6682 if (B_IN_SCOPE (b, external_scope))
6683 break;
6684 if (b)
6685 {
6686 tree ext_decl, ext_type;
6687 ext_decl = b->decl;
6688 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6689 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6690 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6691 TREE_TYPE (ext_type)))
6692 {
6693 current_function_prototype_locus
6694 = DECL_SOURCE_LOCATION (ext_decl);
6695 current_function_prototype_built_in
6696 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6697 current_function_prototype_arg_types
6698 = TYPE_ARG_TYPES (ext_type);
6699 }
6700 }
6701 }
6702 }
6703
6704 /* Optionally warn of old-fashioned def with no previous prototype. */
6705 if (warn_strict_prototypes
6706 && old_decl != error_mark_node
6707 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6708 && C_DECL_ISNT_PROTOTYPE (old_decl))
6709 warning (OPT_Wstrict_prototypes,
6710 "function declaration isn%'t a prototype");
6711 /* Optionally warn of any global def with no previous prototype. */
6712 else if (warn_missing_prototypes
6713 && old_decl != error_mark_node
6714 && TREE_PUBLIC (decl1)
6715 && !MAIN_NAME_P (DECL_NAME (decl1))
6716 && C_DECL_ISNT_PROTOTYPE (old_decl))
6717 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6718 /* Optionally warn of any def with no previous prototype
6719 if the function has already been used. */
6720 else if (warn_missing_prototypes
6721 && old_decl != 0
6722 && old_decl != error_mark_node
6723 && TREE_USED (old_decl)
6724 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6725 warning (OPT_Wmissing_prototypes,
6726 "%q+D was used with no prototype before its definition", decl1);
6727 /* Optionally warn of any global def with no previous declaration. */
6728 else if (warn_missing_declarations
6729 && TREE_PUBLIC (decl1)
6730 && old_decl == 0
6731 && !MAIN_NAME_P (DECL_NAME (decl1)))
6732 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6733 decl1);
6734 /* Optionally warn of any def with no previous declaration
6735 if the function has already been used. */
6736 else if (warn_missing_declarations
6737 && old_decl != 0
6738 && old_decl != error_mark_node
6739 && TREE_USED (old_decl)
6740 && C_DECL_IMPLICIT (old_decl))
6741 warning (OPT_Wmissing_declarations,
6742 "%q+D was used with no declaration before its definition", decl1);
6743
6744 /* This function exists in static storage.
6745 (This does not mean `static' in the C sense!) */
6746 TREE_STATIC (decl1) = 1;
6747
6748 /* A nested function is not global. */
6749 if (current_function_decl != 0)
6750 TREE_PUBLIC (decl1) = 0;
6751
6752 /* This is the earliest point at which we might know the assembler
6753 name of the function. Thus, if it's set before this, die horribly. */
6754 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6755
6756 /* If #pragma weak was used, mark the decl weak now. */
6757 if (current_scope == file_scope)
6758 maybe_apply_pragma_weak (decl1);
6759
6760 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6761 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
6762 {
6763 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6764 != integer_type_node)
6765 pedwarn (input_location, OPT_Wmain, "return type of %q+D is not %<int%>", decl1);
6766
6767 check_main_parameter_types (decl1);
6768
6769 if (!TREE_PUBLIC (decl1))
6770 pedwarn (input_location, OPT_Wmain, "%q+D is normally a non-static function", decl1);
6771 }
6772
6773 /* Record the decl so that the function name is defined.
6774 If we already have a decl for this name, and it is a FUNCTION_DECL,
6775 use the old decl. */
6776
6777 current_function_decl = pushdecl (decl1);
6778
6779 push_scope ();
6780 declare_parm_level ();
6781
6782 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6783 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6784 DECL_ARTIFICIAL (resdecl) = 1;
6785 DECL_IGNORED_P (resdecl) = 1;
6786 DECL_RESULT (current_function_decl) = resdecl;
6787
6788 start_fname_decls ();
6789
6790 return 1;
6791 }
6792 \f
6793 /* Subroutine of store_parm_decls which handles new-style function
6794 definitions (prototype format). The parms already have decls, so we
6795 need only record them as in effect and complain if any redundant
6796 old-style parm decls were written. */
6797 static void
6798 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6799 {
6800 tree decl;
6801
6802 if (current_scope->bindings)
6803 {
6804 error ("%Jold-style parameter declarations in prototyped "
6805 "function definition", fndecl);
6806
6807 /* Get rid of the old-style declarations. */
6808 pop_scope ();
6809 push_scope ();
6810 }
6811 /* Don't issue this warning for nested functions, and don't issue this
6812 warning if we got here because ARG_INFO_TYPES was error_mark_node
6813 (this happens when a function definition has just an ellipsis in
6814 its parameter list). */
6815 else if (!in_system_header && !current_function_scope
6816 && arg_info->types != error_mark_node)
6817 warning (OPT_Wtraditional,
6818 "%Jtraditional C rejects ISO C style function definitions",
6819 fndecl);
6820
6821 /* Now make all the parameter declarations visible in the function body.
6822 We can bypass most of the grunt work of pushdecl. */
6823 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6824 {
6825 DECL_CONTEXT (decl) = current_function_decl;
6826 if (DECL_NAME (decl))
6827 {
6828 bind (DECL_NAME (decl), decl, current_scope,
6829 /*invisible=*/false, /*nested=*/false,
6830 UNKNOWN_LOCATION);
6831 if (!TREE_USED (decl))
6832 warn_if_shadowing (decl);
6833 }
6834 else
6835 error ("%Jparameter name omitted", decl);
6836 }
6837
6838 /* Record the parameter list in the function declaration. */
6839 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6840
6841 /* Now make all the ancillary declarations visible, likewise. */
6842 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6843 {
6844 DECL_CONTEXT (decl) = current_function_decl;
6845 if (DECL_NAME (decl))
6846 bind (DECL_NAME (decl), decl, current_scope,
6847 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6848 }
6849
6850 /* And all the tag declarations. */
6851 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6852 if (TREE_PURPOSE (decl))
6853 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6854 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6855 }
6856
6857 /* Subroutine of store_parm_decls which handles old-style function
6858 definitions (separate parameter list and declarations). */
6859
6860 static void
6861 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6862 {
6863 struct c_binding *b;
6864 tree parm, decl, last;
6865 tree parmids = arg_info->parms;
6866 struct pointer_set_t *seen_args = pointer_set_create ();
6867
6868 if (!in_system_header)
6869 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6870 fndecl);
6871
6872 /* Match each formal parameter name with its declaration. Save each
6873 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6874 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6875 {
6876 if (TREE_VALUE (parm) == 0)
6877 {
6878 error ("%Jparameter name missing from parameter list", fndecl);
6879 TREE_PURPOSE (parm) = 0;
6880 continue;
6881 }
6882
6883 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6884 if (b && B_IN_CURRENT_SCOPE (b))
6885 {
6886 decl = b->decl;
6887 /* If we got something other than a PARM_DECL it is an error. */
6888 if (TREE_CODE (decl) != PARM_DECL)
6889 error ("%q+D declared as a non-parameter", decl);
6890 /* If the declaration is already marked, we have a duplicate
6891 name. Complain and ignore the duplicate. */
6892 else if (pointer_set_contains (seen_args, decl))
6893 {
6894 error ("multiple parameters named %q+D", decl);
6895 TREE_PURPOSE (parm) = 0;
6896 continue;
6897 }
6898 /* If the declaration says "void", complain and turn it into
6899 an int. */
6900 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6901 {
6902 error ("parameter %q+D declared with void type", decl);
6903 TREE_TYPE (decl) = integer_type_node;
6904 DECL_ARG_TYPE (decl) = integer_type_node;
6905 layout_decl (decl, 0);
6906 }
6907 warn_if_shadowing (decl);
6908 }
6909 /* If no declaration found, default to int. */
6910 else
6911 {
6912 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6913 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6914 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6915 pushdecl (decl);
6916 warn_if_shadowing (decl);
6917
6918 if (flag_isoc99)
6919 pedwarn (input_location, 0, "type of %q+D defaults to %<int%>", decl);
6920 else
6921 warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6922 }
6923
6924 TREE_PURPOSE (parm) = decl;
6925 pointer_set_insert (seen_args, decl);
6926 }
6927
6928 /* Now examine the parms chain for incomplete declarations
6929 and declarations with no corresponding names. */
6930
6931 for (b = current_scope->bindings; b; b = b->prev)
6932 {
6933 parm = b->decl;
6934 if (TREE_CODE (parm) != PARM_DECL)
6935 continue;
6936
6937 if (TREE_TYPE (parm) != error_mark_node
6938 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6939 {
6940 error ("parameter %q+D has incomplete type", parm);
6941 TREE_TYPE (parm) = error_mark_node;
6942 }
6943
6944 if (!pointer_set_contains (seen_args, parm))
6945 {
6946 error ("declaration for parameter %q+D but no such parameter", parm);
6947
6948 /* Pretend the parameter was not missing.
6949 This gets us to a standard state and minimizes
6950 further error messages. */
6951 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6952 }
6953 }
6954
6955 /* Chain the declarations together in the order of the list of
6956 names. Store that chain in the function decl, replacing the
6957 list of names. Update the current scope to match. */
6958 DECL_ARGUMENTS (fndecl) = 0;
6959
6960 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6961 if (TREE_PURPOSE (parm))
6962 break;
6963 if (parm && TREE_PURPOSE (parm))
6964 {
6965 last = TREE_PURPOSE (parm);
6966 DECL_ARGUMENTS (fndecl) = last;
6967
6968 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6969 if (TREE_PURPOSE (parm))
6970 {
6971 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6972 last = TREE_PURPOSE (parm);
6973 }
6974 TREE_CHAIN (last) = 0;
6975 }
6976
6977 pointer_set_destroy (seen_args);
6978
6979 /* If there was a previous prototype,
6980 set the DECL_ARG_TYPE of each argument according to
6981 the type previously specified, and report any mismatches. */
6982
6983 if (current_function_prototype_arg_types)
6984 {
6985 tree type;
6986 for (parm = DECL_ARGUMENTS (fndecl),
6987 type = current_function_prototype_arg_types;
6988 parm || (type && TREE_VALUE (type) != error_mark_node
6989 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6990 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6991 {
6992 if (parm == 0 || type == 0
6993 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6994 {
6995 if (current_function_prototype_built_in)
6996 warning (0, "number of arguments doesn%'t match "
6997 "built-in prototype");
6998 else
6999 {
7000 error ("number of arguments doesn%'t match prototype");
7001 error ("%Hprototype declaration",
7002 &current_function_prototype_locus);
7003 }
7004 break;
7005 }
7006 /* Type for passing arg must be consistent with that
7007 declared for the arg. ISO C says we take the unqualified
7008 type for parameters declared with qualified type. */
7009 if (TREE_TYPE (parm) != error_mark_node
7010 && TREE_TYPE (type) != error_mark_node
7011 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
7012 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
7013 {
7014 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
7015 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
7016 {
7017 /* Adjust argument to match prototype. E.g. a previous
7018 `int foo(float);' prototype causes
7019 `int foo(x) float x; {...}' to be treated like
7020 `int foo(float x) {...}'. This is particularly
7021 useful for argument types like uid_t. */
7022 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
7023
7024 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
7025 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
7026 && TYPE_PRECISION (TREE_TYPE (parm))
7027 < TYPE_PRECISION (integer_type_node))
7028 DECL_ARG_TYPE (parm) = integer_type_node;
7029
7030 /* ??? Is it possible to get here with a
7031 built-in prototype or will it always have
7032 been diagnosed as conflicting with an
7033 old-style definition and discarded? */
7034 if (current_function_prototype_built_in)
7035 warning (OPT_pedantic, "promoted argument %qD "
7036 "doesn%'t match built-in prototype", parm);
7037 else
7038 {
7039 pedwarn (input_location, OPT_pedantic, "promoted argument %qD "
7040 "doesn%'t match prototype", parm);
7041 pedwarn (current_function_prototype_locus, OPT_pedantic,
7042 "prototype declaration");
7043 }
7044 }
7045 else
7046 {
7047 if (current_function_prototype_built_in)
7048 warning (0, "argument %qD doesn%'t match "
7049 "built-in prototype", parm);
7050 else
7051 {
7052 error ("argument %qD doesn%'t match prototype", parm);
7053 error ("%Hprototype declaration",
7054 &current_function_prototype_locus);
7055 }
7056 }
7057 }
7058 }
7059 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
7060 }
7061
7062 /* Otherwise, create a prototype that would match. */
7063
7064 else
7065 {
7066 tree actual = 0, last = 0, type;
7067
7068 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
7069 {
7070 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
7071 if (last)
7072 TREE_CHAIN (last) = type;
7073 else
7074 actual = type;
7075 last = type;
7076 }
7077 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
7078 if (last)
7079 TREE_CHAIN (last) = type;
7080 else
7081 actual = type;
7082
7083 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
7084 of the type of this function, but we need to avoid having this
7085 affect the types of other similarly-typed functions, so we must
7086 first force the generation of an identical (but separate) type
7087 node for the relevant function type. The new node we create
7088 will be a variant of the main variant of the original function
7089 type. */
7090
7091 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
7092
7093 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
7094 }
7095 }
7096
7097 /* Store parameter declarations passed in ARG_INFO into the current
7098 function declaration. */
7099
7100 void
7101 store_parm_decls_from (struct c_arg_info *arg_info)
7102 {
7103 current_function_arg_info = arg_info;
7104 store_parm_decls ();
7105 }
7106
7107 /* Store the parameter declarations into the current function declaration.
7108 This is called after parsing the parameter declarations, before
7109 digesting the body of the function.
7110
7111 For an old-style definition, construct a prototype out of the old-style
7112 parameter declarations and inject it into the function's type. */
7113
7114 void
7115 store_parm_decls (void)
7116 {
7117 tree fndecl = current_function_decl;
7118 bool proto;
7119
7120 /* The argument information block for FNDECL. */
7121 struct c_arg_info *arg_info = current_function_arg_info;
7122 current_function_arg_info = 0;
7123
7124 /* True if this definition is written with a prototype. Note:
7125 despite C99 6.7.5.3p14, we can *not* treat an empty argument
7126 list in a function definition as equivalent to (void) -- an
7127 empty argument list specifies the function has no parameters,
7128 but only (void) sets up a prototype for future calls. */
7129 proto = arg_info->types != 0;
7130
7131 if (proto)
7132 store_parm_decls_newstyle (fndecl, arg_info);
7133 else
7134 store_parm_decls_oldstyle (fndecl, arg_info);
7135
7136 /* The next call to push_scope will be a function body. */
7137
7138 next_is_function_body = true;
7139
7140 /* Write a record describing this function definition to the prototypes
7141 file (if requested). */
7142
7143 gen_aux_info_record (fndecl, 1, 0, proto);
7144
7145 /* Initialize the RTL code for the function. */
7146 allocate_struct_function (fndecl, false);
7147
7148 /* Begin the statement tree for this function. */
7149 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
7150
7151 /* ??? Insert the contents of the pending sizes list into the function
7152 to be evaluated. The only reason left to have this is
7153 void foo(int n, int array[n++])
7154 because we throw away the array type in favor of a pointer type, and
7155 thus won't naturally see the SAVE_EXPR containing the increment. All
7156 other pending sizes would be handled by gimplify_parameters. */
7157 {
7158 tree t;
7159 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
7160 add_stmt (TREE_VALUE (t));
7161 }
7162
7163 /* Even though we're inside a function body, we still don't want to
7164 call expand_expr to calculate the size of a variable-sized array.
7165 We haven't necessarily assigned RTL to all variables yet, so it's
7166 not safe to try to expand expressions involving them. */
7167 cfun->dont_save_pending_sizes_p = 1;
7168 }
7169 \f
7170 /* Emit diagnostics that require gimple input for detection. Operate on
7171 FNDECL and all its nested functions. */
7172
7173 static void
7174 c_gimple_diagnostics_recursively (tree fndecl)
7175 {
7176 struct cgraph_node *cgn;
7177 gimple_seq body = gimple_body (fndecl);
7178
7179 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
7180 c_warn_unused_result (body);
7181
7182 /* Notice when OpenMP structured block constraints are violated. */
7183 if (flag_openmp)
7184 diagnose_omp_structured_block_errors (fndecl);
7185
7186 /* Finalize all nested functions now. */
7187 cgn = cgraph_node (fndecl);
7188 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
7189 c_gimple_diagnostics_recursively (cgn->decl);
7190 }
7191
7192 /* Finish up a function declaration and compile that function
7193 all the way to assembler language output. The free the storage
7194 for the function definition.
7195
7196 This is called after parsing the body of the function definition. */
7197
7198 void
7199 finish_function (void)
7200 {
7201 tree fndecl = current_function_decl;
7202
7203 label_context_stack_se = label_context_stack_se->next;
7204 label_context_stack_vm = label_context_stack_vm->next;
7205
7206 if (TREE_CODE (fndecl) == FUNCTION_DECL
7207 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
7208 {
7209 tree args = DECL_ARGUMENTS (fndecl);
7210 for (; args; args = TREE_CHAIN (args))
7211 {
7212 tree type = TREE_TYPE (args);
7213 if (INTEGRAL_TYPE_P (type)
7214 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
7215 DECL_ARG_TYPE (args) = integer_type_node;
7216 }
7217 }
7218
7219 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
7220 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7221
7222 /* Must mark the RESULT_DECL as being in this function. */
7223
7224 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
7225 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
7226
7227 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
7228 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
7229 == integer_type_node && flag_isoc99)
7230 {
7231 tree stmt = c_finish_return (integer_zero_node, NULL_TREE);
7232 /* Hack. We don't want the middle-end to warn that this return
7233 is unreachable, so we mark its location as special. Using
7234 UNKNOWN_LOCATION has the problem that it gets clobbered in
7235 annotate_one_with_locus. A cleaner solution might be to
7236 ensure ! should_carry_locus_p (stmt), but that needs a flag.
7237 */
7238 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
7239 }
7240
7241 /* Tie off the statement tree for this function. */
7242 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
7243
7244 finish_fname_decls ();
7245
7246 /* Complain if there's just no return statement. */
7247 if (warn_return_type
7248 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
7249 && !current_function_returns_value && !current_function_returns_null
7250 /* Don't complain if we are no-return. */
7251 && !current_function_returns_abnormally
7252 /* Don't warn for main(). */
7253 && !MAIN_NAME_P (DECL_NAME (fndecl))
7254 /* Or if they didn't actually specify a return type. */
7255 && !C_FUNCTION_IMPLICIT_INT (fndecl)
7256 /* Normally, with -Wreturn-type, flow will complain, but we might
7257 optimize out static functions. */
7258 && !TREE_PUBLIC (fndecl))
7259 {
7260 warning (OPT_Wreturn_type,
7261 "no return statement in function returning non-void");
7262 TREE_NO_WARNING (fndecl) = 1;
7263 }
7264
7265 /* Store the end of the function, so that we get good line number
7266 info for the epilogue. */
7267 cfun->function_end_locus = input_location;
7268
7269 /* Finalize the ELF visibility for the function. */
7270 c_determine_visibility (fndecl);
7271
7272 /* For GNU C extern inline functions disregard inline limits. */
7273 if (DECL_EXTERNAL (fndecl)
7274 && DECL_DECLARED_INLINE_P (fndecl))
7275 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
7276
7277 /* Genericize before inlining. Delay genericizing nested functions
7278 until their parent function is genericized. Since finalizing
7279 requires GENERIC, delay that as well. */
7280
7281 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
7282 && !undef_nested_function)
7283 {
7284 if (!decl_function_context (fndecl))
7285 {
7286 c_genericize (fndecl);
7287 c_gimple_diagnostics_recursively (fndecl);
7288
7289 /* ??? Objc emits functions after finalizing the compilation unit.
7290 This should be cleaned up later and this conditional removed. */
7291 if (cgraph_global_info_ready)
7292 {
7293 cgraph_add_new_function (fndecl, false);
7294 return;
7295 }
7296
7297 cgraph_finalize_function (fndecl, false);
7298 }
7299 else
7300 {
7301 /* Register this function with cgraph just far enough to get it
7302 added to our parent's nested function list. Handy, since the
7303 C front end doesn't have such a list. */
7304 (void) cgraph_node (fndecl);
7305 }
7306 }
7307
7308 if (!decl_function_context (fndecl))
7309 undef_nested_function = false;
7310
7311 /* We're leaving the context of this function, so zap cfun.
7312 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
7313 tree_rest_of_compilation. */
7314 set_cfun (NULL);
7315 current_function_decl = NULL;
7316 }
7317 \f
7318 /* Check the declarations given in a for-loop for satisfying the C99
7319 constraints. If exactly one such decl is found, return it. */
7320
7321 tree
7322 check_for_loop_decls (void)
7323 {
7324 struct c_binding *b;
7325 tree one_decl = NULL_TREE;
7326 int n_decls = 0;
7327
7328 if (!flag_isoc99)
7329 {
7330 static bool hint = true;
7331 /* If we get here, declarations have been used in a for loop without
7332 the C99 for loop scope. This doesn't make much sense, so don't
7333 allow it. */
7334 error ("%<for%> loop initial declarations are only allowed in C99 mode");
7335 if (hint)
7336 {
7337 inform (input_location,
7338 "use option -std=c99 or -std=gnu99 to compile your code");
7339 hint = false;
7340 }
7341 return NULL_TREE;
7342 }
7343 /* C99 subclause 6.8.5 paragraph 3:
7344
7345 [#3] The declaration part of a for statement shall only
7346 declare identifiers for objects having storage class auto or
7347 register.
7348
7349 It isn't clear whether, in this sentence, "identifiers" binds to
7350 "shall only declare" or to "objects" - that is, whether all identifiers
7351 declared must be identifiers for objects, or whether the restriction
7352 only applies to those that are. (A question on this in comp.std.c
7353 in November 2000 received no answer.) We implement the strictest
7354 interpretation, to avoid creating an extension which later causes
7355 problems. */
7356
7357 for (b = current_scope->bindings; b; b = b->prev)
7358 {
7359 tree id = b->id;
7360 tree decl = b->decl;
7361
7362 if (!id)
7363 continue;
7364
7365 switch (TREE_CODE (decl))
7366 {
7367 case VAR_DECL:
7368 if (TREE_STATIC (decl))
7369 error ("declaration of static variable %q+D in %<for%> loop "
7370 "initial declaration", decl);
7371 else if (DECL_EXTERNAL (decl))
7372 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
7373 "initial declaration", decl);
7374 break;
7375
7376 case RECORD_TYPE:
7377 error ("%<struct %E%> declared in %<for%> loop initial declaration",
7378 id);
7379 break;
7380 case UNION_TYPE:
7381 error ("%<union %E%> declared in %<for%> loop initial declaration",
7382 id);
7383 break;
7384 case ENUMERAL_TYPE:
7385 error ("%<enum %E%> declared in %<for%> loop initial declaration",
7386 id);
7387 break;
7388 default:
7389 error ("declaration of non-variable %q+D in %<for%> loop "
7390 "initial declaration", decl);
7391 }
7392
7393 n_decls++;
7394 one_decl = decl;
7395 }
7396
7397 return n_decls == 1 ? one_decl : NULL_TREE;
7398 }
7399 \f
7400 /* Save and reinitialize the variables
7401 used during compilation of a C function. */
7402
7403 void
7404 c_push_function_context (void)
7405 {
7406 struct language_function *p;
7407 p = GGC_NEW (struct language_function);
7408 cfun->language = p;
7409
7410 p->base.x_stmt_tree = c_stmt_tree;
7411 p->x_break_label = c_break_label;
7412 p->x_cont_label = c_cont_label;
7413 p->x_switch_stack = c_switch_stack;
7414 p->arg_info = current_function_arg_info;
7415 p->returns_value = current_function_returns_value;
7416 p->returns_null = current_function_returns_null;
7417 p->returns_abnormally = current_function_returns_abnormally;
7418 p->warn_about_return_type = warn_about_return_type;
7419
7420 push_function_context ();
7421 }
7422
7423 /* Restore the variables used during compilation of a C function. */
7424
7425 void
7426 c_pop_function_context (void)
7427 {
7428 struct language_function *p;
7429
7430 pop_function_context ();
7431 p = cfun->language;
7432 cfun->language = NULL;
7433
7434 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
7435 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7436 {
7437 /* Stop pointing to the local nodes about to be freed. */
7438 /* But DECL_INITIAL must remain nonzero so we know this
7439 was an actual function definition. */
7440 DECL_INITIAL (current_function_decl) = error_mark_node;
7441 DECL_ARGUMENTS (current_function_decl) = 0;
7442 }
7443
7444 c_stmt_tree = p->base.x_stmt_tree;
7445 c_break_label = p->x_break_label;
7446 c_cont_label = p->x_cont_label;
7447 c_switch_stack = p->x_switch_stack;
7448 current_function_arg_info = p->arg_info;
7449 current_function_returns_value = p->returns_value;
7450 current_function_returns_null = p->returns_null;
7451 current_function_returns_abnormally = p->returns_abnormally;
7452 warn_about_return_type = p->warn_about_return_type;
7453 }
7454
7455 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
7456
7457 void
7458 c_dup_lang_specific_decl (tree decl)
7459 {
7460 struct lang_decl *ld;
7461
7462 if (!DECL_LANG_SPECIFIC (decl))
7463 return;
7464
7465 ld = GGC_NEW (struct lang_decl);
7466 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
7467 DECL_LANG_SPECIFIC (decl) = ld;
7468 }
7469
7470 /* The functions below are required for functionality of doing
7471 function at once processing in the C front end. Currently these
7472 functions are not called from anywhere in the C front end, but as
7473 these changes continue, that will change. */
7474
7475 /* Returns the stmt_tree (if any) to which statements are currently
7476 being added. If there is no active statement-tree, NULL is
7477 returned. */
7478
7479 stmt_tree
7480 current_stmt_tree (void)
7481 {
7482 return &c_stmt_tree;
7483 }
7484
7485 /* Return the global value of T as a symbol. */
7486
7487 tree
7488 identifier_global_value (tree t)
7489 {
7490 struct c_binding *b;
7491
7492 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7493 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7494 return b->decl;
7495
7496 return 0;
7497 }
7498
7499 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7500 otherwise the name is found in ridpointers from RID_INDEX. */
7501
7502 void
7503 record_builtin_type (enum rid rid_index, const char *name, tree type)
7504 {
7505 tree id, decl;
7506 if (name == 0)
7507 id = ridpointers[(int) rid_index];
7508 else
7509 id = get_identifier (name);
7510 decl = build_decl (TYPE_DECL, id, type);
7511 pushdecl (decl);
7512 if (debug_hooks->type_decl)
7513 debug_hooks->type_decl (decl, false);
7514 }
7515
7516 /* Build the void_list_node (void_type_node having been created). */
7517 tree
7518 build_void_list_node (void)
7519 {
7520 tree t = build_tree_list (NULL_TREE, void_type_node);
7521 return t;
7522 }
7523
7524 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7525
7526 struct c_parm *
7527 build_c_parm (struct c_declspecs *specs, tree attrs,
7528 struct c_declarator *declarator)
7529 {
7530 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7531 ret->specs = specs;
7532 ret->attrs = attrs;
7533 ret->declarator = declarator;
7534 return ret;
7535 }
7536
7537 /* Return a declarator with nested attributes. TARGET is the inner
7538 declarator to which these attributes apply. ATTRS are the
7539 attributes. */
7540
7541 struct c_declarator *
7542 build_attrs_declarator (tree attrs, struct c_declarator *target)
7543 {
7544 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7545 ret->kind = cdk_attrs;
7546 ret->declarator = target;
7547 ret->u.attrs = attrs;
7548 return ret;
7549 }
7550
7551 /* Return a declarator for a function with arguments specified by ARGS
7552 and return type specified by TARGET. */
7553
7554 struct c_declarator *
7555 build_function_declarator (struct c_arg_info *args,
7556 struct c_declarator *target)
7557 {
7558 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7559 ret->kind = cdk_function;
7560 ret->declarator = target;
7561 ret->u.arg_info = args;
7562 return ret;
7563 }
7564
7565 /* Return a declarator for the identifier IDENT (which may be
7566 NULL_TREE for an abstract declarator). */
7567
7568 struct c_declarator *
7569 build_id_declarator (tree ident)
7570 {
7571 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7572 ret->kind = cdk_id;
7573 ret->declarator = 0;
7574 ret->u.id = ident;
7575 /* Default value - may get reset to a more precise location. */
7576 ret->id_loc = input_location;
7577 return ret;
7578 }
7579
7580 /* Return something to represent absolute declarators containing a *.
7581 TARGET is the absolute declarator that the * contains.
7582 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7583 to apply to the pointer type. */
7584
7585 struct c_declarator *
7586 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7587 struct c_declarator *target)
7588 {
7589 tree attrs;
7590 int quals = 0;
7591 struct c_declarator *itarget = target;
7592 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7593 if (type_quals_attrs)
7594 {
7595 attrs = type_quals_attrs->attrs;
7596 quals = quals_from_declspecs (type_quals_attrs);
7597 if (attrs != NULL_TREE)
7598 itarget = build_attrs_declarator (attrs, target);
7599 }
7600 ret->kind = cdk_pointer;
7601 ret->declarator = itarget;
7602 ret->u.pointer_quals = quals;
7603 return ret;
7604 }
7605
7606 /* Return a pointer to a structure for an empty list of declaration
7607 specifiers. */
7608
7609 struct c_declspecs *
7610 build_null_declspecs (void)
7611 {
7612 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7613 ret->type = 0;
7614 ret->expr = 0;
7615 ret->decl_attr = 0;
7616 ret->attrs = 0;
7617 ret->typespec_word = cts_none;
7618 ret->storage_class = csc_none;
7619 ret->expr_const_operands = true;
7620 ret->declspecs_seen_p = false;
7621 ret->type_seen_p = false;
7622 ret->non_sc_seen_p = false;
7623 ret->typedef_p = false;
7624 ret->tag_defined_p = false;
7625 ret->explicit_signed_p = false;
7626 ret->deprecated_p = false;
7627 ret->default_int_p = false;
7628 ret->long_p = false;
7629 ret->long_long_p = false;
7630 ret->short_p = false;
7631 ret->signed_p = false;
7632 ret->unsigned_p = false;
7633 ret->complex_p = false;
7634 ret->inline_p = false;
7635 ret->thread_p = false;
7636 ret->const_p = false;
7637 ret->volatile_p = false;
7638 ret->restrict_p = false;
7639 ret->saturating_p = false;
7640 return ret;
7641 }
7642
7643 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7644 returning SPECS. */
7645
7646 struct c_declspecs *
7647 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7648 {
7649 enum rid i;
7650 bool dupe = false;
7651 specs->non_sc_seen_p = true;
7652 specs->declspecs_seen_p = true;
7653 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7654 && C_IS_RESERVED_WORD (qual));
7655 i = C_RID_CODE (qual);
7656 switch (i)
7657 {
7658 case RID_CONST:
7659 dupe = specs->const_p;
7660 specs->const_p = true;
7661 break;
7662 case RID_VOLATILE:
7663 dupe = specs->volatile_p;
7664 specs->volatile_p = true;
7665 break;
7666 case RID_RESTRICT:
7667 dupe = specs->restrict_p;
7668 specs->restrict_p = true;
7669 break;
7670 default:
7671 gcc_unreachable ();
7672 }
7673 if (dupe && !flag_isoc99)
7674 pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
7675 return specs;
7676 }
7677
7678 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7679 returning SPECS. */
7680
7681 struct c_declspecs *
7682 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7683 {
7684 tree type = spec.spec;
7685 specs->non_sc_seen_p = true;
7686 specs->declspecs_seen_p = true;
7687 specs->type_seen_p = true;
7688 if (TREE_DEPRECATED (type))
7689 specs->deprecated_p = true;
7690
7691 /* Handle type specifier keywords. */
7692 if (TREE_CODE (type) == IDENTIFIER_NODE
7693 && C_IS_RESERVED_WORD (type)
7694 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
7695 {
7696 enum rid i = C_RID_CODE (type);
7697 if (specs->type)
7698 {
7699 error ("two or more data types in declaration specifiers");
7700 return specs;
7701 }
7702 if ((int) i <= (int) RID_LAST_MODIFIER)
7703 {
7704 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
7705 bool dupe = false;
7706 switch (i)
7707 {
7708 case RID_LONG:
7709 if (specs->long_long_p)
7710 {
7711 error ("%<long long long%> is too long for GCC");
7712 break;
7713 }
7714 if (specs->long_p)
7715 {
7716 if (specs->typespec_word == cts_double)
7717 {
7718 error ("both %<long long%> and %<double%> in "
7719 "declaration specifiers");
7720 break;
7721 }
7722 pedwarn_c90 (input_location, OPT_Wlong_long,
7723 "ISO C90 does not support %<long long%>");
7724 specs->long_long_p = 1;
7725 break;
7726 }
7727 if (specs->short_p)
7728 error ("both %<long%> and %<short%> in "
7729 "declaration specifiers");
7730 else if (specs->typespec_word == cts_void)
7731 error ("both %<long%> and %<void%> in "
7732 "declaration specifiers");
7733 else if (specs->typespec_word == cts_bool)
7734 error ("both %<long%> and %<_Bool%> in "
7735 "declaration specifiers");
7736 else if (specs->typespec_word == cts_char)
7737 error ("both %<long%> and %<char%> in "
7738 "declaration specifiers");
7739 else if (specs->typespec_word == cts_float)
7740 error ("both %<long%> and %<float%> in "
7741 "declaration specifiers");
7742 else if (specs->typespec_word == cts_dfloat32)
7743 error ("both %<long%> and %<_Decimal32%> in "
7744 "declaration specifiers");
7745 else if (specs->typespec_word == cts_dfloat64)
7746 error ("both %<long%> and %<_Decimal64%> in "
7747 "declaration specifiers");
7748 else if (specs->typespec_word == cts_dfloat128)
7749 error ("both %<long%> and %<_Decimal128%> in "
7750 "declaration specifiers");
7751 else
7752 specs->long_p = true;
7753 break;
7754 case RID_SHORT:
7755 dupe = specs->short_p;
7756 if (specs->long_p)
7757 error ("both %<long%> and %<short%> in "
7758 "declaration specifiers");
7759 else if (specs->typespec_word == cts_void)
7760 error ("both %<short%> and %<void%> in "
7761 "declaration specifiers");
7762 else if (specs->typespec_word == cts_bool)
7763 error ("both %<short%> and %<_Bool%> in "
7764 "declaration specifiers");
7765 else if (specs->typespec_word == cts_char)
7766 error ("both %<short%> and %<char%> in "
7767 "declaration specifiers");
7768 else if (specs->typespec_word == cts_float)
7769 error ("both %<short%> and %<float%> in "
7770 "declaration specifiers");
7771 else if (specs->typespec_word == cts_double)
7772 error ("both %<short%> and %<double%> in "
7773 "declaration specifiers");
7774 else if (specs->typespec_word == cts_dfloat32)
7775 error ("both %<short%> and %<_Decimal32%> in "
7776 "declaration specifiers");
7777 else if (specs->typespec_word == cts_dfloat64)
7778 error ("both %<short%> and %<_Decimal64%> in "
7779 "declaration specifiers");
7780 else if (specs->typespec_word == cts_dfloat128)
7781 error ("both %<short%> and %<_Decimal128%> in "
7782 "declaration specifiers");
7783 else
7784 specs->short_p = true;
7785 break;
7786 case RID_SIGNED:
7787 dupe = specs->signed_p;
7788 if (specs->unsigned_p)
7789 error ("both %<signed%> and %<unsigned%> in "
7790 "declaration specifiers");
7791 else if (specs->typespec_word == cts_void)
7792 error ("both %<signed%> and %<void%> in "
7793 "declaration specifiers");
7794 else if (specs->typespec_word == cts_bool)
7795 error ("both %<signed%> and %<_Bool%> in "
7796 "declaration specifiers");
7797 else if (specs->typespec_word == cts_float)
7798 error ("both %<signed%> and %<float%> in "
7799 "declaration specifiers");
7800 else if (specs->typespec_word == cts_double)
7801 error ("both %<signed%> and %<double%> in "
7802 "declaration specifiers");
7803 else if (specs->typespec_word == cts_dfloat32)
7804 error ("both %<signed%> and %<_Decimal32%> in "
7805 "declaration specifiers");
7806 else if (specs->typespec_word == cts_dfloat64)
7807 error ("both %<signed%> and %<_Decimal64%> in "
7808 "declaration specifiers");
7809 else if (specs->typespec_word == cts_dfloat128)
7810 error ("both %<signed%> and %<_Decimal128%> in "
7811 "declaration specifiers");
7812 else
7813 specs->signed_p = true;
7814 break;
7815 case RID_UNSIGNED:
7816 dupe = specs->unsigned_p;
7817 if (specs->signed_p)
7818 error ("both %<signed%> and %<unsigned%> in "
7819 "declaration specifiers");
7820 else if (specs->typespec_word == cts_void)
7821 error ("both %<unsigned%> and %<void%> in "
7822 "declaration specifiers");
7823 else if (specs->typespec_word == cts_bool)
7824 error ("both %<unsigned%> and %<_Bool%> in "
7825 "declaration specifiers");
7826 else if (specs->typespec_word == cts_float)
7827 error ("both %<unsigned%> and %<float%> in "
7828 "declaration specifiers");
7829 else if (specs->typespec_word == cts_double)
7830 error ("both %<unsigned%> and %<double%> in "
7831 "declaration specifiers");
7832 else if (specs->typespec_word == cts_dfloat32)
7833 error ("both %<unsigned%> and %<_Decimal32%> in "
7834 "declaration specifiers");
7835 else if (specs->typespec_word == cts_dfloat64)
7836 error ("both %<unsigned%> and %<_Decimal64%> in "
7837 "declaration specifiers");
7838 else if (specs->typespec_word == cts_dfloat128)
7839 error ("both %<unsigned%> and %<_Decimal128%> in "
7840 "declaration specifiers");
7841 else
7842 specs->unsigned_p = true;
7843 break;
7844 case RID_COMPLEX:
7845 dupe = specs->complex_p;
7846 if (!flag_isoc99 && !in_system_header)
7847 pedwarn (input_location, OPT_pedantic, "ISO C90 does not support complex types");
7848 if (specs->typespec_word == cts_void)
7849 error ("both %<complex%> and %<void%> in "
7850 "declaration specifiers");
7851 else if (specs->typespec_word == cts_bool)
7852 error ("both %<complex%> and %<_Bool%> in "
7853 "declaration specifiers");
7854 else if (specs->typespec_word == cts_dfloat32)
7855 error ("both %<complex%> and %<_Decimal32%> in "
7856 "declaration specifiers");
7857 else if (specs->typespec_word == cts_dfloat64)
7858 error ("both %<complex%> and %<_Decimal64%> in "
7859 "declaration specifiers");
7860 else if (specs->typespec_word == cts_dfloat128)
7861 error ("both %<complex%> and %<_Decimal128%> in "
7862 "declaration specifiers");
7863 else if (specs->typespec_word == cts_fract)
7864 error ("both %<complex%> and %<_Fract%> in "
7865 "declaration specifiers");
7866 else if (specs->typespec_word == cts_accum)
7867 error ("both %<complex%> and %<_Accum%> in "
7868 "declaration specifiers");
7869 else if (specs->saturating_p)
7870 error ("both %<complex%> and %<_Sat%> in "
7871 "declaration specifiers");
7872 else
7873 specs->complex_p = true;
7874 break;
7875 case RID_SAT:
7876 dupe = specs->saturating_p;
7877 pedwarn (input_location, OPT_pedantic, "ISO C does not support saturating types");
7878 if (specs->typespec_word == cts_void)
7879 error ("both %<_Sat%> and %<void%> in "
7880 "declaration specifiers");
7881 else if (specs->typespec_word == cts_bool)
7882 error ("both %<_Sat%> and %<_Bool%> in "
7883 "declaration specifiers");
7884 else if (specs->typespec_word == cts_char)
7885 error ("both %<_Sat%> and %<char%> in "
7886 "declaration specifiers");
7887 else if (specs->typespec_word == cts_int)
7888 error ("both %<_Sat%> and %<int%> in "
7889 "declaration specifiers");
7890 else if (specs->typespec_word == cts_float)
7891 error ("both %<_Sat%> and %<float%> in "
7892 "declaration specifiers");
7893 else if (specs->typespec_word == cts_double)
7894 error ("both %<_Sat%> and %<double%> in "
7895 "declaration specifiers");
7896 else if (specs->typespec_word == cts_dfloat32)
7897 error ("both %<_Sat%> and %<_Decimal32%> in "
7898 "declaration specifiers");
7899 else if (specs->typespec_word == cts_dfloat64)
7900 error ("both %<_Sat%> and %<_Decimal64%> in "
7901 "declaration specifiers");
7902 else if (specs->typespec_word == cts_dfloat128)
7903 error ("both %<_Sat%> and %<_Decimal128%> in "
7904 "declaration specifiers");
7905 else if (specs->complex_p)
7906 error ("both %<_Sat%> and %<complex%> in "
7907 "declaration specifiers");
7908 else
7909 specs->saturating_p = true;
7910 break;
7911 default:
7912 gcc_unreachable ();
7913 }
7914
7915 if (dupe)
7916 error ("duplicate %qE", type);
7917
7918 return specs;
7919 }
7920 else
7921 {
7922 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
7923 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
7924 if (specs->typespec_word != cts_none)
7925 {
7926 error ("two or more data types in declaration specifiers");
7927 return specs;
7928 }
7929 switch (i)
7930 {
7931 case RID_VOID:
7932 if (specs->long_p)
7933 error ("both %<long%> and %<void%> in "
7934 "declaration specifiers");
7935 else if (specs->short_p)
7936 error ("both %<short%> and %<void%> in "
7937 "declaration specifiers");
7938 else if (specs->signed_p)
7939 error ("both %<signed%> and %<void%> in "
7940 "declaration specifiers");
7941 else if (specs->unsigned_p)
7942 error ("both %<unsigned%> and %<void%> in "
7943 "declaration specifiers");
7944 else if (specs->complex_p)
7945 error ("both %<complex%> and %<void%> in "
7946 "declaration specifiers");
7947 else if (specs->saturating_p)
7948 error ("both %<_Sat%> and %<void%> in "
7949 "declaration specifiers");
7950 else
7951 specs->typespec_word = cts_void;
7952 return specs;
7953 case RID_BOOL:
7954 if (specs->long_p)
7955 error ("both %<long%> and %<_Bool%> in "
7956 "declaration specifiers");
7957 else if (specs->short_p)
7958 error ("both %<short%> and %<_Bool%> in "
7959 "declaration specifiers");
7960 else if (specs->signed_p)
7961 error ("both %<signed%> and %<_Bool%> in "
7962 "declaration specifiers");
7963 else if (specs->unsigned_p)
7964 error ("both %<unsigned%> and %<_Bool%> in "
7965 "declaration specifiers");
7966 else if (specs->complex_p)
7967 error ("both %<complex%> and %<_Bool%> in "
7968 "declaration specifiers");
7969 else if (specs->saturating_p)
7970 error ("both %<_Sat%> and %<_Bool%> in "
7971 "declaration specifiers");
7972 else
7973 specs->typespec_word = cts_bool;
7974 return specs;
7975 case RID_CHAR:
7976 if (specs->long_p)
7977 error ("both %<long%> and %<char%> in "
7978 "declaration specifiers");
7979 else if (specs->short_p)
7980 error ("both %<short%> and %<char%> in "
7981 "declaration specifiers");
7982 else if (specs->saturating_p)
7983 error ("both %<_Sat%> and %<char%> in "
7984 "declaration specifiers");
7985 else
7986 specs->typespec_word = cts_char;
7987 return specs;
7988 case RID_INT:
7989 if (specs->saturating_p)
7990 error ("both %<_Sat%> and %<int%> in "
7991 "declaration specifiers");
7992 else
7993 specs->typespec_word = cts_int;
7994 return specs;
7995 case RID_FLOAT:
7996 if (specs->long_p)
7997 error ("both %<long%> and %<float%> in "
7998 "declaration specifiers");
7999 else if (specs->short_p)
8000 error ("both %<short%> and %<float%> in "
8001 "declaration specifiers");
8002 else if (specs->signed_p)
8003 error ("both %<signed%> and %<float%> in "
8004 "declaration specifiers");
8005 else if (specs->unsigned_p)
8006 error ("both %<unsigned%> and %<float%> in "
8007 "declaration specifiers");
8008 else if (specs->saturating_p)
8009 error ("both %<_Sat%> and %<float%> in "
8010 "declaration specifiers");
8011 else
8012 specs->typespec_word = cts_float;
8013 return specs;
8014 case RID_DOUBLE:
8015 if (specs->long_long_p)
8016 error ("both %<long long%> and %<double%> in "
8017 "declaration specifiers");
8018 else if (specs->short_p)
8019 error ("both %<short%> and %<double%> in "
8020 "declaration specifiers");
8021 else if (specs->signed_p)
8022 error ("both %<signed%> and %<double%> in "
8023 "declaration specifiers");
8024 else if (specs->unsigned_p)
8025 error ("both %<unsigned%> and %<double%> in "
8026 "declaration specifiers");
8027 else if (specs->saturating_p)
8028 error ("both %<_Sat%> and %<double%> in "
8029 "declaration specifiers");
8030 else
8031 specs->typespec_word = cts_double;
8032 return specs;
8033 case RID_DFLOAT32:
8034 case RID_DFLOAT64:
8035 case RID_DFLOAT128:
8036 {
8037 const char *str;
8038 if (i == RID_DFLOAT32)
8039 str = "_Decimal32";
8040 else if (i == RID_DFLOAT64)
8041 str = "_Decimal64";
8042 else
8043 str = "_Decimal128";
8044 if (specs->long_long_p)
8045 error ("both %<long long%> and %<%s%> in "
8046 "declaration specifiers", str);
8047 if (specs->long_p)
8048 error ("both %<long%> and %<%s%> in "
8049 "declaration specifiers", str);
8050 else if (specs->short_p)
8051 error ("both %<short%> and %<%s%> in "
8052 "declaration specifiers", str);
8053 else if (specs->signed_p)
8054 error ("both %<signed%> and %<%s%> in "
8055 "declaration specifiers", str);
8056 else if (specs->unsigned_p)
8057 error ("both %<unsigned%> and %<%s%> in "
8058 "declaration specifiers", str);
8059 else if (specs->complex_p)
8060 error ("both %<complex%> and %<%s%> in "
8061 "declaration specifiers", str);
8062 else if (specs->saturating_p)
8063 error ("both %<_Sat%> and %<%s%> in "
8064 "declaration specifiers", str);
8065 else if (i == RID_DFLOAT32)
8066 specs->typespec_word = cts_dfloat32;
8067 else if (i == RID_DFLOAT64)
8068 specs->typespec_word = cts_dfloat64;
8069 else
8070 specs->typespec_word = cts_dfloat128;
8071 }
8072 if (!targetm.decimal_float_supported_p ())
8073 error ("decimal floating point not supported for this target");
8074 pedwarn (input_location, OPT_pedantic,
8075 "ISO C does not support decimal floating point");
8076 return specs;
8077 case RID_FRACT:
8078 case RID_ACCUM:
8079 {
8080 const char *str;
8081 if (i == RID_FRACT)
8082 str = "_Fract";
8083 else
8084 str = "_Accum";
8085 if (specs->complex_p)
8086 error ("both %<complex%> and %<%s%> in "
8087 "declaration specifiers", str);
8088 else if (i == RID_FRACT)
8089 specs->typespec_word = cts_fract;
8090 else
8091 specs->typespec_word = cts_accum;
8092 }
8093 if (!targetm.fixed_point_supported_p ())
8094 error ("fixed-point types not supported for this target");
8095 pedwarn (input_location, OPT_pedantic,
8096 "ISO C does not support fixed-point types");
8097 return specs;
8098 default:
8099 /* ObjC reserved word "id", handled below. */
8100 break;
8101 }
8102 }
8103 }
8104
8105 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
8106 form of ObjC type, cases such as "int" and "long" being handled
8107 above), a TYPE (struct, union, enum and typeof specifiers) or an
8108 ERROR_MARK. In none of these cases may there have previously
8109 been any type specifiers. */
8110 if (specs->type || specs->typespec_word != cts_none
8111 || specs->long_p || specs->short_p || specs->signed_p
8112 || specs->unsigned_p || specs->complex_p)
8113 error ("two or more data types in declaration specifiers");
8114 else if (TREE_CODE (type) == TYPE_DECL)
8115 {
8116 if (TREE_TYPE (type) == error_mark_node)
8117 ; /* Allow the type to default to int to avoid cascading errors. */
8118 else
8119 {
8120 specs->type = TREE_TYPE (type);
8121 specs->decl_attr = DECL_ATTRIBUTES (type);
8122 specs->typedef_p = true;
8123 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
8124 }
8125 }
8126 else if (TREE_CODE (type) == IDENTIFIER_NODE)
8127 {
8128 tree t = lookup_name (type);
8129 if (!t || TREE_CODE (t) != TYPE_DECL)
8130 error ("%qE fails to be a typedef or built in type", type);
8131 else if (TREE_TYPE (t) == error_mark_node)
8132 ;
8133 else
8134 specs->type = TREE_TYPE (t);
8135 }
8136 else if (TREE_CODE (type) != ERROR_MARK)
8137 {
8138 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
8139 specs->tag_defined_p = true;
8140 if (spec.kind == ctsk_typeof)
8141 {
8142 specs->typedef_p = true;
8143 if (spec.expr)
8144 {
8145 if (specs->expr)
8146 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
8147 specs->expr, spec.expr);
8148 else
8149 specs->expr = spec.expr;
8150 specs->expr_const_operands &= spec.expr_const_operands;
8151 }
8152 }
8153 specs->type = type;
8154 }
8155
8156 return specs;
8157 }
8158
8159 /* Add the storage class specifier or function specifier SCSPEC to the
8160 declaration specifiers SPECS, returning SPECS. */
8161
8162 struct c_declspecs *
8163 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
8164 {
8165 enum rid i;
8166 enum c_storage_class n = csc_none;
8167 bool dupe = false;
8168 specs->declspecs_seen_p = true;
8169 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
8170 && C_IS_RESERVED_WORD (scspec));
8171 i = C_RID_CODE (scspec);
8172 if (specs->non_sc_seen_p)
8173 warning (OPT_Wold_style_declaration,
8174 "%qE is not at beginning of declaration", scspec);
8175 switch (i)
8176 {
8177 case RID_INLINE:
8178 /* C99 permits duplicate inline. Although of doubtful utility,
8179 it seems simplest to permit it in gnu89 mode as well, as
8180 there is also little utility in maintaining this as a
8181 difference between gnu89 and C99 inline. */
8182 dupe = false;
8183 specs->inline_p = true;
8184 break;
8185 case RID_THREAD:
8186 dupe = specs->thread_p;
8187 if (specs->storage_class == csc_auto)
8188 error ("%<__thread%> used with %<auto%>");
8189 else if (specs->storage_class == csc_register)
8190 error ("%<__thread%> used with %<register%>");
8191 else if (specs->storage_class == csc_typedef)
8192 error ("%<__thread%> used with %<typedef%>");
8193 else
8194 specs->thread_p = true;
8195 break;
8196 case RID_AUTO:
8197 n = csc_auto;
8198 break;
8199 case RID_EXTERN:
8200 n = csc_extern;
8201 /* Diagnose "__thread extern". */
8202 if (specs->thread_p)
8203 error ("%<__thread%> before %<extern%>");
8204 break;
8205 case RID_REGISTER:
8206 n = csc_register;
8207 break;
8208 case RID_STATIC:
8209 n = csc_static;
8210 /* Diagnose "__thread static". */
8211 if (specs->thread_p)
8212 error ("%<__thread%> before %<static%>");
8213 break;
8214 case RID_TYPEDEF:
8215 n = csc_typedef;
8216 break;
8217 default:
8218 gcc_unreachable ();
8219 }
8220 if (n != csc_none && n == specs->storage_class)
8221 dupe = true;
8222 if (dupe)
8223 error ("duplicate %qE", scspec);
8224 if (n != csc_none)
8225 {
8226 if (specs->storage_class != csc_none && n != specs->storage_class)
8227 {
8228 error ("multiple storage classes in declaration specifiers");
8229 }
8230 else
8231 {
8232 specs->storage_class = n;
8233 if (n != csc_extern && n != csc_static && specs->thread_p)
8234 {
8235 error ("%<__thread%> used with %qE", scspec);
8236 specs->thread_p = false;
8237 }
8238 }
8239 }
8240 return specs;
8241 }
8242
8243 /* Add the attributes ATTRS to the declaration specifiers SPECS,
8244 returning SPECS. */
8245
8246 struct c_declspecs *
8247 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
8248 {
8249 specs->attrs = chainon (attrs, specs->attrs);
8250 specs->declspecs_seen_p = true;
8251 return specs;
8252 }
8253
8254 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
8255 specifiers with any other type specifier to determine the resulting
8256 type. This is where ISO C checks on complex types are made, since
8257 "_Complex long" is a prefix of the valid ISO C type "_Complex long
8258 double". */
8259
8260 struct c_declspecs *
8261 finish_declspecs (struct c_declspecs *specs)
8262 {
8263 /* If a type was specified as a whole, we have no modifiers and are
8264 done. */
8265 if (specs->type != NULL_TREE)
8266 {
8267 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8268 && !specs->signed_p && !specs->unsigned_p
8269 && !specs->complex_p);
8270 return specs;
8271 }
8272
8273 /* If none of "void", "_Bool", "char", "int", "float" or "double"
8274 has been specified, treat it as "int" unless "_Complex" is
8275 present and there are no other specifiers. If we just have
8276 "_Complex", it is equivalent to "_Complex double", but e.g.
8277 "_Complex short" is equivalent to "_Complex short int". */
8278 if (specs->typespec_word == cts_none)
8279 {
8280 if (specs->saturating_p)
8281 {
8282 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
8283 if (!targetm.fixed_point_supported_p ())
8284 error ("fixed-point types not supported for this target");
8285 specs->typespec_word = cts_fract;
8286 }
8287 else if (specs->long_p || specs->short_p
8288 || specs->signed_p || specs->unsigned_p)
8289 {
8290 specs->typespec_word = cts_int;
8291 }
8292 else if (specs->complex_p)
8293 {
8294 specs->typespec_word = cts_double;
8295 pedwarn (input_location, OPT_pedantic,
8296 "ISO C does not support plain %<complex%> meaning "
8297 "%<double complex%>");
8298 }
8299 else
8300 {
8301 specs->typespec_word = cts_int;
8302 specs->default_int_p = true;
8303 /* We don't diagnose this here because grokdeclarator will
8304 give more specific diagnostics according to whether it is
8305 a function definition. */
8306 }
8307 }
8308
8309 /* If "signed" was specified, record this to distinguish "int" and
8310 "signed int" in the case of a bit-field with
8311 -funsigned-bitfields. */
8312 specs->explicit_signed_p = specs->signed_p;
8313
8314 /* Now compute the actual type. */
8315 switch (specs->typespec_word)
8316 {
8317 case cts_void:
8318 gcc_assert (!specs->long_p && !specs->short_p
8319 && !specs->signed_p && !specs->unsigned_p
8320 && !specs->complex_p);
8321 specs->type = void_type_node;
8322 break;
8323 case cts_bool:
8324 gcc_assert (!specs->long_p && !specs->short_p
8325 && !specs->signed_p && !specs->unsigned_p
8326 && !specs->complex_p);
8327 specs->type = boolean_type_node;
8328 break;
8329 case cts_char:
8330 gcc_assert (!specs->long_p && !specs->short_p);
8331 gcc_assert (!(specs->signed_p && specs->unsigned_p));
8332 if (specs->signed_p)
8333 specs->type = signed_char_type_node;
8334 else if (specs->unsigned_p)
8335 specs->type = unsigned_char_type_node;
8336 else
8337 specs->type = char_type_node;
8338 if (specs->complex_p)
8339 {
8340 pedwarn (input_location, OPT_pedantic,
8341 "ISO C does not support complex integer types");
8342 specs->type = build_complex_type (specs->type);
8343 }
8344 break;
8345 case cts_int:
8346 gcc_assert (!(specs->long_p && specs->short_p));
8347 gcc_assert (!(specs->signed_p && specs->unsigned_p));
8348 if (specs->long_long_p)
8349 specs->type = (specs->unsigned_p
8350 ? long_long_unsigned_type_node
8351 : long_long_integer_type_node);
8352 else if (specs->long_p)
8353 specs->type = (specs->unsigned_p
8354 ? long_unsigned_type_node
8355 : long_integer_type_node);
8356 else if (specs->short_p)
8357 specs->type = (specs->unsigned_p
8358 ? short_unsigned_type_node
8359 : short_integer_type_node);
8360 else
8361 specs->type = (specs->unsigned_p
8362 ? unsigned_type_node
8363 : integer_type_node);
8364 if (specs->complex_p)
8365 {
8366 pedwarn (input_location, OPT_pedantic,
8367 "ISO C does not support complex integer types");
8368 specs->type = build_complex_type (specs->type);
8369 }
8370 break;
8371 case cts_float:
8372 gcc_assert (!specs->long_p && !specs->short_p
8373 && !specs->signed_p && !specs->unsigned_p);
8374 specs->type = (specs->complex_p
8375 ? complex_float_type_node
8376 : float_type_node);
8377 break;
8378 case cts_double:
8379 gcc_assert (!specs->long_long_p && !specs->short_p
8380 && !specs->signed_p && !specs->unsigned_p);
8381 if (specs->long_p)
8382 {
8383 specs->type = (specs->complex_p
8384 ? complex_long_double_type_node
8385 : long_double_type_node);
8386 }
8387 else
8388 {
8389 specs->type = (specs->complex_p
8390 ? complex_double_type_node
8391 : double_type_node);
8392 }
8393 break;
8394 case cts_dfloat32:
8395 case cts_dfloat64:
8396 case cts_dfloat128:
8397 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8398 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
8399 if (specs->typespec_word == cts_dfloat32)
8400 specs->type = dfloat32_type_node;
8401 else if (specs->typespec_word == cts_dfloat64)
8402 specs->type = dfloat64_type_node;
8403 else
8404 specs->type = dfloat128_type_node;
8405 break;
8406 case cts_fract:
8407 gcc_assert (!specs->complex_p);
8408 if (!targetm.fixed_point_supported_p ())
8409 specs->type = integer_type_node;
8410 else if (specs->saturating_p)
8411 {
8412 if (specs->long_long_p)
8413 specs->type = specs->unsigned_p
8414 ? sat_unsigned_long_long_fract_type_node
8415 : sat_long_long_fract_type_node;
8416 else if (specs->long_p)
8417 specs->type = specs->unsigned_p
8418 ? sat_unsigned_long_fract_type_node
8419 : sat_long_fract_type_node;
8420 else if (specs->short_p)
8421 specs->type = specs->unsigned_p
8422 ? sat_unsigned_short_fract_type_node
8423 : sat_short_fract_type_node;
8424 else
8425 specs->type = specs->unsigned_p
8426 ? sat_unsigned_fract_type_node
8427 : sat_fract_type_node;
8428 }
8429 else
8430 {
8431 if (specs->long_long_p)
8432 specs->type = specs->unsigned_p
8433 ? unsigned_long_long_fract_type_node
8434 : long_long_fract_type_node;
8435 else if (specs->long_p)
8436 specs->type = specs->unsigned_p
8437 ? unsigned_long_fract_type_node
8438 : long_fract_type_node;
8439 else if (specs->short_p)
8440 specs->type = specs->unsigned_p
8441 ? unsigned_short_fract_type_node
8442 : short_fract_type_node;
8443 else
8444 specs->type = specs->unsigned_p
8445 ? unsigned_fract_type_node
8446 : fract_type_node;
8447 }
8448 break;
8449 case cts_accum:
8450 gcc_assert (!specs->complex_p);
8451 if (!targetm.fixed_point_supported_p ())
8452 specs->type = integer_type_node;
8453 else if (specs->saturating_p)
8454 {
8455 if (specs->long_long_p)
8456 specs->type = specs->unsigned_p
8457 ? sat_unsigned_long_long_accum_type_node
8458 : sat_long_long_accum_type_node;
8459 else if (specs->long_p)
8460 specs->type = specs->unsigned_p
8461 ? sat_unsigned_long_accum_type_node
8462 : sat_long_accum_type_node;
8463 else if (specs->short_p)
8464 specs->type = specs->unsigned_p
8465 ? sat_unsigned_short_accum_type_node
8466 : sat_short_accum_type_node;
8467 else
8468 specs->type = specs->unsigned_p
8469 ? sat_unsigned_accum_type_node
8470 : sat_accum_type_node;
8471 }
8472 else
8473 {
8474 if (specs->long_long_p)
8475 specs->type = specs->unsigned_p
8476 ? unsigned_long_long_accum_type_node
8477 : long_long_accum_type_node;
8478 else if (specs->long_p)
8479 specs->type = specs->unsigned_p
8480 ? unsigned_long_accum_type_node
8481 : long_accum_type_node;
8482 else if (specs->short_p)
8483 specs->type = specs->unsigned_p
8484 ? unsigned_short_accum_type_node
8485 : short_accum_type_node;
8486 else
8487 specs->type = specs->unsigned_p
8488 ? unsigned_accum_type_node
8489 : accum_type_node;
8490 }
8491 break;
8492 default:
8493 gcc_unreachable ();
8494 }
8495
8496 return specs;
8497 }
8498
8499 /* A subroutine of c_write_global_declarations. Perform final processing
8500 on one file scope's declarations (or the external scope's declarations),
8501 GLOBALS. */
8502
8503 static void
8504 c_write_global_declarations_1 (tree globals)
8505 {
8506 tree decl;
8507 bool reconsider;
8508
8509 /* Process the decls in the order they were written. */
8510 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8511 {
8512 /* Check for used but undefined static functions using the C
8513 standard's definition of "used", and set TREE_NO_WARNING so
8514 that check_global_declarations doesn't repeat the check. */
8515 if (TREE_CODE (decl) == FUNCTION_DECL
8516 && DECL_INITIAL (decl) == 0
8517 && DECL_EXTERNAL (decl)
8518 && !TREE_PUBLIC (decl)
8519 && C_DECL_USED (decl))
8520 {
8521 pedwarn (input_location, 0, "%q+F used but never defined", decl);
8522 TREE_NO_WARNING (decl) = 1;
8523 }
8524
8525 wrapup_global_declaration_1 (decl);
8526 }
8527
8528 do
8529 {
8530 reconsider = false;
8531 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8532 reconsider |= wrapup_global_declaration_2 (decl);
8533 }
8534 while (reconsider);
8535
8536 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8537 check_global_declaration_1 (decl);
8538 }
8539
8540 /* A subroutine of c_write_global_declarations Emit debug information for each
8541 of the declarations in GLOBALS. */
8542
8543 static void
8544 c_write_global_declarations_2 (tree globals)
8545 {
8546 tree decl;
8547
8548 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8549 debug_hooks->global_decl (decl);
8550 }
8551
8552 /* Preserve the external declarations scope across a garbage collect. */
8553 static GTY(()) tree ext_block;
8554
8555 void
8556 c_write_global_declarations (void)
8557 {
8558 tree t;
8559
8560 /* We don't want to do this if generating a PCH. */
8561 if (pch_file)
8562 return;
8563
8564 /* Don't waste time on further processing if -fsyntax-only or we've
8565 encountered errors. */
8566 if (flag_syntax_only || errorcount || sorrycount)
8567 return;
8568
8569 /* Close the external scope. */
8570 ext_block = pop_scope ();
8571 external_scope = 0;
8572 gcc_assert (!current_scope);
8573
8574 if (ext_block)
8575 {
8576 tree tmp = BLOCK_VARS (ext_block);
8577 int flags;
8578 FILE * stream = dump_begin (TDI_tu, &flags);
8579 if (stream && tmp)
8580 {
8581 dump_node (tmp, flags & ~TDF_SLIM, stream);
8582 dump_end (TDI_tu, stream);
8583 }
8584 }
8585
8586 /* Process all file scopes in this compilation, and the external_scope,
8587 through wrapup_global_declarations and check_global_declarations. */
8588 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8589 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8590 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8591
8592 /* We're done parsing; proceed to optimize and emit assembly.
8593 FIXME: shouldn't be the front end's responsibility to call this. */
8594 cgraph_optimize ();
8595
8596 /* After cgraph has had a chance to emit everything that's going to
8597 be emitted, output debug information for globals. */
8598 if (errorcount == 0 && sorrycount == 0)
8599 {
8600 timevar_push (TV_SYMOUT);
8601 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8602 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8603 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8604 timevar_pop (TV_SYMOUT);
8605 }
8606
8607 ext_block = NULL;
8608 }
8609
8610 #include "gt-c-decl.h"