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