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