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