]> gcc.gnu.org Git - gcc.git/blob - gcc/c-decl.c
c-decl.c (build_compound_literal): Use TYPE_READONLY.
[gcc.git] / gcc / c-decl.c
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004 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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, 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 "tm.h"
33 #include "intl.h"
34 #include "tree.h"
35 #include "tree-inline.h"
36 #include "rtl.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "c-tree.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "tm_p.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "debug.h"
48 #include "opts.h"
49 #include "timevar.h"
50 #include "c-common.h"
51 #include "c-pragma.h"
52 #include "cgraph.h"
53 #include "hashtab.h"
54 #include "libfuncs.h"
55 #include "except.h"
56 #include "langhooks-def.h"
57
58 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
59 enum decl_context
60 { NORMAL, /* Ordinary declaration */
61 FUNCDEF, /* Function definition */
62 PARM, /* Declaration of parm before function body */
63 FIELD, /* Declaration inside struct or union */
64 TYPENAME}; /* Typename (inside cast or sizeof) */
65
66 \f
67 /* Nonzero if we have seen an invalid cross reference
68 to a struct, union, or enum, but not yet printed the message. */
69
70 tree pending_invalid_xref;
71 /* File and line to appear in the eventual error message. */
72 location_t pending_invalid_xref_location;
73
74 /* While defining an enum type, this is 1 plus the last enumerator
75 constant value. Note that will do not have to save this or `enum_overflow'
76 around nested function definition since such a definition could only
77 occur in an enum value expression and we don't use these variables in
78 that case. */
79
80 static tree enum_next_value;
81
82 /* Nonzero means that there was overflow computing enum_next_value. */
83
84 static int enum_overflow;
85
86 /* These #defines are for clarity in working with the information block
87 returned by get_parm_info. */
88 #define ARG_INFO_PARMS(args) TREE_PURPOSE(args)
89 #define ARG_INFO_TAGS(args) TREE_VALUE(args)
90 #define ARG_INFO_TYPES(args) TREE_CHAIN(args)
91 #define ARG_INFO_OTHERS(args) TREE_TYPE(args)
92
93 /* The file and line that the prototype came from if this is an
94 old-style definition; used for diagnostics in
95 store_parm_decls_oldstyle. */
96
97 static location_t current_function_prototype_locus;
98
99 /* The current statement tree. */
100
101 static GTY(()) struct stmt_tree_s c_stmt_tree;
102
103 /* The current scope statement stack. */
104
105 static GTY(()) tree c_scope_stmt_stack;
106
107 /* State saving variables. */
108 int c_in_iteration_stmt;
109 int c_in_case_stmt;
110
111 /* A DECL for the current file-scope context. */
112
113 static GTY(()) tree current_file_decl;
114
115 /* A list of decls to be made automatically visible in each file scope. */
116 static GTY(()) tree visible_builtins;
117
118 /* Set to 0 at beginning of a function definition, set to 1 if
119 a return statement that specifies a return value is seen. */
120
121 int current_function_returns_value;
122
123 /* Set to 0 at beginning of a function definition, set to 1 if
124 a return statement with no argument is seen. */
125
126 int current_function_returns_null;
127
128 /* Set to 0 at beginning of a function definition, set to 1 if
129 a call to a noreturn function is seen. */
130
131 int current_function_returns_abnormally;
132
133 /* Set to nonzero by `grokdeclarator' for a function
134 whose return type is defaulted, if warnings for this are desired. */
135
136 static int warn_about_return_type;
137
138 /* Nonzero when starting a function declared `extern inline'. */
139
140 static int current_extern_inline;
141
142 /* True means global_bindings_p should return false even if the scope stack
143 says we are in file scope. */
144 bool c_override_global_bindings_to_false;
145
146 \f
147 /* Each c_binding structure describes one binding of an identifier to
148 a decl. All the decls in a scope - irrespective of namespace - are
149 chained together by the ->prev field, which (as the name implies)
150 runs in reverse order. All the decls in a given namespace bound to
151 a given identifier are chained by the ->shadowed field, which runs
152 from inner to outer scopes. Finally, the ->contour field points
153 back to the relevant scope structure; this is mainly used to make
154 decls in the externals scope invisible (see below).
155
156 The ->decl field usually points to a DECL node, but there are two
157 exceptions. In the namespace of type tags, the bound entity is a
158 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
159 identifier is encountered, it is bound to error_mark_node to
160 suppress further errors about that identifier in the current
161 function. */
162
163 struct c_binding GTY((chain_next ("%h.prev")))
164 {
165 tree decl; /* the decl bound */
166 tree id; /* the identifier it's bound to */
167 struct c_binding *prev; /* the previous decl in this scope */
168 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
169 struct c_scope *contour; /* the scope in which this decl is bound */
170 };
171
172 #define I_SYMBOL_BINDING(node) \
173 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->symbol_binding)
174 #define I_SYMBOL_DECL(node) \
175 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
176
177 #define I_TAG_BINDING(node) \
178 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->tag_binding)
179 #define I_TAG_DECL(node) \
180 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
181
182 #define I_LABEL_BINDING(node) \
183 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->label_binding)
184 #define I_LABEL_DECL(node) \
185 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
186
187 /* Each C symbol points to three linked lists of c_binding structures.
188 These describe the values of the identifier in the three different
189 namespaces defined by the language. */
190
191 struct lang_identifier GTY(())
192 {
193 struct c_common_identifier common_id;
194 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
195 struct c_binding *tag_binding; /* struct/union/enum tags */
196 struct c_binding *label_binding; /* labels */
197 };
198
199 /* Validate c-lang.c's assumptions. */
200 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
201 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
202
203 /* The resulting tree type. */
204
205 union lang_tree_node
206 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
207 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)")))
208 {
209 union tree_node GTY ((tag ("0"),
210 desc ("tree_node_structure (&%h)")))
211 generic;
212 struct lang_identifier GTY ((tag ("1"))) identifier;
213 };
214
215 /* Each c_scope structure describes the complete contents of one
216 scope. Four scopes are distinguished specially: the innermost or
217 current scope, the innermost function scope, the file scope (always
218 the second to outermost) and the outermost or external scope.
219
220 Most declarations are recorded in the current scope.
221
222 All normal label declarations are recorded in the innermost
223 function scope, as are bindings of undeclared identifiers to
224 error_mark_node. (GCC permits nested functions as an extension,
225 hence the 'innermost' qualifier.) Explicitly declared labels
226 (using the __label__ extension) appear in the current scope.
227
228 Being in the file scope (current_scope == file_scope) causes
229 special behavior in several places below. Also, under some
230 conditions the Objective-C front end records declarations in the
231 file scope even though that isn't the current scope.
232
233 All declarations with external linkage are recorded in the external
234 scope, even if they aren't visible there; this models the fact that
235 such declarations are visible to the entire program, and (with a
236 bit of cleverness, see pushdecl) allows diagnosis of some violations
237 of C99 6.2.2p7 and 6.2.7p2:
238
239 If, within the same translation unit, the same identifier appears
240 with both internal and external linkage, the behavior is
241 undefined.
242
243 All declarations that refer to the same object or function shall
244 have compatible type; otherwise, the behavior is undefined.
245
246 Initially only the built-in declarations, which describe compiler
247 intrinsic functions plus a subset of the standard library, are in
248 this scope.
249
250 The order of the blocks list matters, and it is frequently appended
251 to. To avoid having to walk all the way to the end of the list on
252 each insertion, or reverse the list later, we maintain a pointer to
253 the last list entry. (FIXME: It should be feasible to use a reversed
254 list here.)
255
256 The bindings list is strictly in reverse order of declarations;
257 pop_scope relies on this. */
258
259
260 struct c_scope GTY((chain_next ("%h.outer")))
261 {
262 /* The scope containing this one. */
263 struct c_scope *outer;
264
265 /* The next outermost function scope. */
266 struct c_scope *outer_function;
267
268 /* All bindings in this scope. */
269 struct c_binding *bindings;
270
271 /* For each scope (except the global one), a chain of BLOCK nodes
272 for all the scopes that were entered and exited one level down. */
273 tree blocks;
274 tree blocks_last;
275
276 /* The depth of this scope. Used to keep the ->shadowed chain of
277 bindings sorted innermost to outermost. */
278 unsigned int depth : 28;
279
280 /* True if we are currently filling this scope with parameter
281 declarations. */
282 BOOL_BITFIELD parm_flag : 1;
283
284 /* True if we already complained about forward parameter decls
285 in this scope. This prevents double warnings on
286 foo (int a; int b; ...) */
287 BOOL_BITFIELD warned_forward_parm_decls : 1;
288
289 /* True if this is the outermost block scope of a function body.
290 This scope contains the parameters, the local variables declared
291 in the outermost block, and all the labels (except those in
292 nested functions, or declared at block scope with __label__). */
293 BOOL_BITFIELD function_body : 1;
294
295 /* True means make a BLOCK for this scope no matter what. */
296 BOOL_BITFIELD keep : 1;
297 };
298
299 /* The scope currently in effect. */
300
301 static GTY(()) struct c_scope *current_scope;
302
303 /* The innermost function scope. Ordinary (not explicitly declared)
304 labels, bindings to error_mark_node, and the lazily-created
305 bindings of __func__ and its friends get this scope. */
306
307 static GTY(()) struct c_scope *current_function_scope;
308
309 /* The C file scope. This is reset for each input translation unit. */
310
311 static GTY(()) struct c_scope *file_scope;
312
313 /* The outermost scope. This is used for all declarations with
314 external linkage, and only these, hence the name. */
315
316 static GTY(()) struct c_scope *external_scope;
317
318 /* A chain of c_scope structures awaiting reuse. */
319
320 static GTY((deletable)) struct c_scope *scope_freelist;
321
322 /* A chain of c_binding structures awaiting reuse. */
323
324 static GTY((deletable)) struct c_binding *binding_freelist;
325
326 /* Append VAR to LIST in scope SCOPE. */
327 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
328 struct c_scope *s_ = (scope); \
329 tree d_ = (decl); \
330 if (s_->list##_last) \
331 TREE_CHAIN (s_->list##_last) = d_; \
332 else \
333 s_->list = d_; \
334 s_->list##_last = d_; \
335 } while (0)
336
337 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
338 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
339 struct c_scope *t_ = (tscope); \
340 struct c_scope *f_ = (fscope); \
341 if (t_->to##_last) \
342 TREE_CHAIN (t_->to##_last) = f_->from; \
343 else \
344 t_->to = f_->from; \
345 t_->to##_last = f_->from##_last; \
346 } while (0)
347
348 /* True means unconditionally make a BLOCK for the next scope pushed. */
349
350 static bool keep_next_level_flag;
351
352 /* True means the next call to push_scope will be the outermost scope
353 of a function body, so do not push a new scope, merely cease
354 expecting parameter decls. */
355
356 static bool next_is_function_body;
357
358 /* Functions called automatically at the beginning and end of execution. */
359
360 tree static_ctors, static_dtors;
361
362 /* Forward declarations. */
363 static tree lookup_name_in_scope (tree, struct c_scope *);
364 static tree c_make_fname_decl (tree, int);
365 static tree grokdeclarator (tree, tree, enum decl_context, int, tree *);
366 static tree grokparms (tree, int);
367 static void layout_array_type (tree);
368 \f
369 /* States indicating how grokdeclarator() should handle declspecs marked
370 with __attribute__((deprecated)). An object declared as
371 __attribute__((deprecated)) suppresses warnings of uses of other
372 deprecated items. */
373
374 enum deprecated_states {
375 DEPRECATED_NORMAL,
376 DEPRECATED_SUPPRESS
377 };
378
379 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
380
381 void
382 c_print_identifier (FILE *file, tree node, int indent)
383 {
384 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
385 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
386 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
387 if (C_IS_RESERVED_WORD (node))
388 {
389 tree rid = ridpointers[C_RID_CODE (node)];
390 indent_to (file, indent + 4);
391 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
392 (void *) rid, IDENTIFIER_POINTER (rid));
393 }
394 }
395
396 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
397 which may be any of several kinds of DECL or TYPE or error_mark_node,
398 in the scope SCOPE. */
399 static void
400 bind (tree name, tree decl, struct c_scope *scope)
401 {
402 struct c_binding *b, **here;
403
404 if (binding_freelist)
405 {
406 b = binding_freelist;
407 binding_freelist = b->prev;
408 }
409 else
410 b = ggc_alloc (sizeof (struct c_binding));
411
412 b->shadowed = 0;
413 b->decl = decl;
414 b->id = name;
415 b->contour = scope;
416
417 b->prev = scope->bindings;
418 scope->bindings = b;
419
420 if (!name)
421 return;
422
423 switch (TREE_CODE (decl))
424 {
425 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
426 case ENUMERAL_TYPE:
427 case UNION_TYPE:
428 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
429 case VAR_DECL:
430 case FUNCTION_DECL:
431 case TYPE_DECL:
432 case CONST_DECL:
433 case PARM_DECL:
434 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
435
436 default:
437 abort ();
438 }
439
440 /* Locate the appropriate place in the chain of shadowed decls
441 to insert this binding. Normally, scope == current_scope and
442 this does nothing. */
443 while (*here && (*here)->contour->depth > scope->depth)
444 here = &(*here)->shadowed;
445
446 b->shadowed = *here;
447 *here = b;
448 }
449
450 /* Clear the binding structure B, stick it on the binding_freelist,
451 and return the former value of b->prev. This is used by pop_scope
452 and get_parm_info to iterate destructively over all the bindings
453 from a given scope. */
454 static struct c_binding *
455 free_binding_and_advance (struct c_binding *b)
456 {
457 struct c_binding *prev = b->prev;
458
459 b->id = 0;
460 b->decl = 0;
461 b->contour = 0;
462 b->shadowed = 0;
463 b->prev = binding_freelist;
464 binding_freelist = b;
465
466 return prev;
467 }
468
469 \f
470 /* Hook called at end of compilation to assume 1 elt
471 for a file-scope tentative array defn that wasn't complete before. */
472
473 void
474 c_finish_incomplete_decl (tree decl)
475 {
476 if (TREE_CODE (decl) == VAR_DECL)
477 {
478 tree type = TREE_TYPE (decl);
479 if (type != error_mark_node
480 && TREE_CODE (type) == ARRAY_TYPE
481 && ! DECL_EXTERNAL (decl)
482 && TYPE_DOMAIN (type) == 0)
483 {
484 warning ("%Jarray '%D' assumed to have one element", decl, decl);
485
486 complete_array_type (type, NULL_TREE, 1);
487
488 layout_decl (decl, 0);
489 }
490 }
491 }
492 \f
493 /* The Objective-C front-end often needs to determine the current scope. */
494
495 void *
496 get_current_scope (void)
497 {
498 return current_scope;
499 }
500
501 /* The following function is used only by Objective-C. It needs to live here
502 because it accesses the innards of c_scope. */
503
504 void
505 objc_mark_locals_volatile (void *enclosing_blk)
506 {
507 struct c_scope *scope;
508 struct c_binding *b;
509
510 for (scope = current_scope;
511 scope && scope != enclosing_blk;
512 scope = scope->outer)
513 {
514 for (b = scope->bindings; b; b = b->prev)
515 {
516 if (TREE_CODE (b->decl) == VAR_DECL
517 || TREE_CODE (b->decl) == PARM_DECL)
518 {
519 C_DECL_REGISTER (b->decl) = 0;
520 DECL_REGISTER (b->decl) = 0;
521 TREE_THIS_VOLATILE (b->decl) = 1;
522 }
523 }
524
525 /* Do not climb up past the current function. */
526 if (scope->function_body)
527 break;
528 }
529 }
530
531 /* Nonzero if we are currently in file scope. */
532
533 int
534 global_bindings_p (void)
535 {
536 return current_scope == file_scope && !c_override_global_bindings_to_false;
537 }
538
539 void
540 keep_next_level (void)
541 {
542 keep_next_level_flag = true;
543 }
544
545 /* Identify this scope as currently being filled with parameters. */
546
547 void
548 declare_parm_level (void)
549 {
550 current_scope->parm_flag = true;
551 }
552
553 void
554 push_scope (void)
555 {
556 if (next_is_function_body)
557 {
558 /* This is the transition from the parameters to the top level
559 of the function body. These are the same scope
560 (C99 6.2.1p4,6) so we do not push another scope structure.
561 next_is_function_body is set only by store_parm_decls, which
562 in turn is called when and only when we are about to
563 encounter the opening curly brace for the function body.
564
565 The outermost block of a function always gets a BLOCK node,
566 because the debugging output routines expect that each
567 function has at least one BLOCK. */
568 current_scope->parm_flag = false;
569 current_scope->function_body = true;
570 current_scope->keep = true;
571 current_scope->outer_function = current_function_scope;
572 current_function_scope = current_scope;
573
574 keep_next_level_flag = false;
575 next_is_function_body = false;
576 }
577 else
578 {
579 struct c_scope *scope;
580 if (scope_freelist)
581 {
582 scope = scope_freelist;
583 scope_freelist = scope->outer;
584 }
585 else
586 scope = ggc_alloc_cleared (sizeof (struct c_scope));
587
588 scope->keep = keep_next_level_flag;
589 scope->outer = current_scope;
590 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
591
592 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
593 possible. */
594 if (current_scope && scope->depth == 0)
595 {
596 scope->depth--;
597 sorry ("GCC supports only %u nested scopes\n", scope->depth);
598 }
599
600 current_scope = scope;
601 keep_next_level_flag = false;
602 }
603 }
604
605 /* Exit a scope. Restore the state of the identifier-decl mappings
606 that were in effect when this scope was entered. Return a BLOCK
607 node containing all the DECLs in this scope that are of interest
608 to debug info generation. */
609
610 tree
611 pop_scope (void)
612 {
613 struct c_scope *scope = current_scope;
614 tree block, context, p;
615 struct c_binding *b;
616
617 bool functionbody = scope->function_body;
618 bool keep = functionbody || scope->keep || scope->bindings;
619
620 /* If appropriate, create a BLOCK to record the decls for the life
621 of this function. */
622 block = 0;
623 if (keep)
624 {
625 block = make_node (BLOCK);
626 BLOCK_SUBBLOCKS (block) = scope->blocks;
627 TREE_USED (block) = 1;
628
629 /* In each subblock, record that this is its superior. */
630 for (p = scope->blocks; p; p = TREE_CHAIN (p))
631 BLOCK_SUPERCONTEXT (p) = block;
632
633 BLOCK_VARS (block) = 0;
634 }
635
636 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
637 scope must be set so that they point to the appropriate
638 construct, i.e. either to the current FUNCTION_DECL node, or
639 else to the BLOCK node we just constructed.
640
641 Note that for tagged types whose scope is just the formal
642 parameter list for some function type specification, we can't
643 properly set their TYPE_CONTEXTs here, because we don't have a
644 pointer to the appropriate FUNCTION_TYPE node readily available
645 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
646 type nodes get set in `grokdeclarator' as soon as we have created
647 the FUNCTION_TYPE node which will represent the "scope" for these
648 "parameter list local" tagged types. */
649 if (scope->function_body)
650 context = current_function_decl;
651 else if (scope == file_scope)
652 context = current_file_decl;
653 else
654 context = block;
655
656 /* Clear all bindings in this scope. */
657 for (b = scope->bindings; b; b = free_binding_and_advance (b))
658 {
659 p = b->decl;
660 switch (TREE_CODE (p))
661 {
662 case LABEL_DECL:
663 /* Warnings for unused labels, errors for undefined labels. */
664 if (TREE_USED (p) && !DECL_INITIAL (p))
665 {
666 error ("%Jlabel `%D' used but not defined", p, p);
667 DECL_INITIAL (p) = error_mark_node;
668 }
669 else if (!TREE_USED (p) && warn_unused_label)
670 {
671 if (DECL_INITIAL (p))
672 warning ("%Jlabel `%D' defined but not used", p, p);
673 else
674 warning ("%Jlabel `%D' declared but not defined", p, p);
675 }
676 /* Labels go in BLOCK_VARS. */
677 TREE_CHAIN (p) = BLOCK_VARS (block);
678 BLOCK_VARS (block) = p;
679
680 #ifdef ENABLE_CHECKING
681 if (I_LABEL_BINDING (b->id) != b) abort ();
682 #endif
683 I_LABEL_BINDING (b->id) = b->shadowed;
684 break;
685
686 case ENUMERAL_TYPE:
687 case UNION_TYPE:
688 case RECORD_TYPE:
689 TYPE_CONTEXT (p) = context;
690
691 /* Types may not have tag-names, in which case the type
692 appears in the bindings list with b->id NULL. */
693 if (b->id)
694 {
695 #ifdef ENABLE_CHECKING
696 if (I_TAG_BINDING (b->id) != b) abort ();
697 #endif
698 I_TAG_BINDING (b->id) = b->shadowed;
699 }
700 break;
701
702 case FUNCTION_DECL:
703 /* Propagate TREE_ADDRESSABLE from nested functions to their
704 containing functions. */
705 if (! TREE_ASM_WRITTEN (p)
706 && DECL_INITIAL (p) != 0
707 && TREE_ADDRESSABLE (p)
708 && DECL_ABSTRACT_ORIGIN (p) != 0
709 && DECL_ABSTRACT_ORIGIN (p) != p)
710 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
711 goto common_symbol;
712
713 case VAR_DECL:
714 /* Warnings for unused variables. Keep this in sync with
715 stmt.c:warn_about_unused_variables, which we cannot use
716 since it expects a different data structure. */
717 if (warn_unused_variable
718 && !TREE_USED (p)
719 && !DECL_IN_SYSTEM_HEADER (p)
720 && DECL_NAME (p)
721 && !DECL_ARTIFICIAL (p)
722 && (scope != file_scope
723 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
724 && !TREE_THIS_VOLATILE (p)))
725 && scope != external_scope)
726 warning ("%Junused variable `%D'", p, p);
727
728 /* Fall through. */
729 case TYPE_DECL:
730 case CONST_DECL:
731 common_symbol:
732 /* All of these go in BLOCK_VARS, but only if this is the
733 binding in the home scope. */
734 if (!C_DECL_IN_EXTERNAL_SCOPE (p) || scope == external_scope)
735 {
736 TREE_CHAIN (p) = BLOCK_VARS (block);
737 BLOCK_VARS (block) = p;
738 }
739
740 /* Fall through. */
741 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
742 already been put there by store_parm_decls. Unused-
743 parameter warnings are handled by function.c.
744 error_mark_node obviously does not go in BLOCK_VARS and
745 does not get unused-variable warnings. */
746 case PARM_DECL:
747 case ERROR_MARK:
748 /* It is possible for a decl not to have a name. We get
749 here with b->id NULL in this case. */
750 if (b->id)
751 {
752 #ifdef ENABLE_CHECKING
753 if (I_SYMBOL_BINDING (b->id) != b) abort ();
754 #endif
755 I_SYMBOL_BINDING (b->id) = b->shadowed;
756 }
757 break;
758
759 default:
760 abort ();
761 }
762 }
763
764
765 /* Dispose of the block that we just made inside some higher level. */
766 if ((scope->function_body || scope == file_scope) && context)
767 {
768 DECL_INITIAL (context) = block;
769 BLOCK_SUPERCONTEXT (block) = context;
770 }
771 else if (scope->outer)
772 {
773 if (block)
774 SCOPE_LIST_APPEND (scope->outer, blocks, block);
775 /* If we did not make a block for the scope just exited, any
776 blocks made for inner scopes must be carried forward so they
777 will later become subblocks of something else. */
778 else if (scope->blocks)
779 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
780 }
781
782 /* Pop the current scope, and free the structure for reuse. */
783 current_scope = scope->outer;
784 if (scope->function_body)
785 current_function_scope = scope->outer_function;
786
787 memset (scope, 0, sizeof (struct c_scope));
788 scope->outer = scope_freelist;
789 scope_freelist = scope;
790
791 return block;
792 }
793
794 void
795 push_file_scope (void)
796 {
797 tree decl;
798 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
799 TREE_CHAIN (file_decl) = current_file_decl;
800 current_file_decl = file_decl;
801
802 push_scope ();
803 file_scope = current_scope;
804
805 start_fname_decls ();
806
807 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
808 bind (DECL_NAME (decl), decl, file_scope);
809 }
810
811 void
812 pop_file_scope (void)
813 {
814 /* In case there were missing closebraces, get us back to the global
815 binding level. */
816 while (current_scope != file_scope)
817 pop_scope ();
818
819 /* __FUNCTION__ is defined at file scope (""). This
820 call may not be necessary as my tests indicate it
821 still works without it. */
822 finish_fname_decls ();
823
824 /* Kludge: don't actually pop the file scope if generating a
825 precompiled header, so that macros and local symbols are still
826 visible to the PCH generator. */
827 if (pch_file)
828 return;
829
830 /* And pop off the file scope. */
831 pop_scope ();
832 file_scope = 0;
833
834 cpp_undef_all (parse_in);
835 }
836
837 /* Insert BLOCK at the end of the list of subblocks of the current
838 scope. This is used when a BIND_EXPR is expanded, to handle the
839 BLOCK node inside the BIND_EXPR. */
840
841 void
842 insert_block (tree block)
843 {
844 TREE_USED (block) = 1;
845 SCOPE_LIST_APPEND (current_scope, blocks, block);
846 }
847 \f
848 /* Push a definition or a declaration of struct, union or enum tag "name".
849 "type" should be the type node.
850 We assume that the tag "name" is not already defined.
851
852 Note that the definition may really be just a forward reference.
853 In that case, the TYPE_SIZE will be zero. */
854
855 static void
856 pushtag (tree name, tree type)
857 {
858 /* Record the identifier as the type's name if it has none. */
859 if (name && !TYPE_NAME (type))
860 TYPE_NAME (type) = name;
861 bind (name, type, current_scope);
862
863 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
864 tagged type we just added to the current scope. This fake
865 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
866 to output a representation of a tagged type, and it also gives
867 us a convenient place to record the "scope start" address for the
868 tagged type. */
869
870 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
871
872 /* An approximation for now, so we can tell this is a function-scope tag.
873 This will be updated in pop_scope. */
874 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
875 }
876 \f
877 /* Subroutine of compare_decls. Allow harmless mismatches in return
878 and argument types provided that the type modes match. This function
879 return a unified type given a suitable match, and 0 otherwise. */
880
881 static tree
882 match_builtin_function_types (tree newtype, tree oldtype)
883 {
884 tree newrettype, oldrettype;
885 tree newargs, oldargs;
886 tree trytype, tryargs;
887
888 /* Accept the return type of the new declaration if same modes. */
889 oldrettype = TREE_TYPE (oldtype);
890 newrettype = TREE_TYPE (newtype);
891
892 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
893 return 0;
894
895 oldargs = TYPE_ARG_TYPES (oldtype);
896 newargs = TYPE_ARG_TYPES (newtype);
897 tryargs = newargs;
898
899 while (oldargs || newargs)
900 {
901 if (! oldargs
902 || ! newargs
903 || ! TREE_VALUE (oldargs)
904 || ! TREE_VALUE (newargs)
905 || TYPE_MODE (TREE_VALUE (oldargs))
906 != TYPE_MODE (TREE_VALUE (newargs)))
907 return 0;
908
909 oldargs = TREE_CHAIN (oldargs);
910 newargs = TREE_CHAIN (newargs);
911 }
912
913 trytype = build_function_type (newrettype, tryargs);
914 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
915 }
916
917 /* Subroutine of diagnose_mismatched_decls. Check for function type
918 mismatch involving an empty arglist vs a nonempty one and give clearer
919 diagnostics. */
920 static void
921 diagnose_arglist_conflict (tree newdecl, tree olddecl,
922 tree newtype, tree oldtype)
923 {
924 tree t;
925
926 if (TREE_CODE (olddecl) != FUNCTION_DECL
927 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype), COMPARE_STRICT)
928 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
929 ||
930 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
931 return;
932
933 t = TYPE_ARG_TYPES (oldtype);
934 if (t == 0)
935 t = TYPE_ARG_TYPES (newtype);
936 for (; t; t = TREE_CHAIN (t))
937 {
938 tree type = TREE_VALUE (t);
939
940 if (TREE_CHAIN (t) == 0
941 && TYPE_MAIN_VARIANT (type) != void_type_node)
942 {
943 inform ("a parameter list with an ellipsis can't match "
944 "an empty parameter name list declaration");
945 break;
946 }
947
948 if (c_type_promotes_to (type) != type)
949 {
950 inform ("an argument type that has a default promotion can't match "
951 "an empty parameter name list declaration");
952 break;
953 }
954 }
955 }
956
957 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
958 old-style function definition, NEWDECL is a prototype declaration.
959 Diagnose inconsistencies in the argument list. Returns TRUE if
960 the prototype is compatible, FALSE if not. */
961 static bool
962 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
963 {
964 tree newargs, oldargs;
965 int i;
966
967 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
968 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
969
970 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
971 newargs = TYPE_ARG_TYPES (newtype);
972 i = 1;
973
974 for (;;)
975 {
976 tree oldargtype = TREE_VALUE (oldargs);
977 tree newargtype = TREE_VALUE (newargs);
978
979 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
980 break;
981
982 /* Reaching the end of just one list means the two decls don't
983 agree on the number of arguments. */
984 if (END_OF_ARGLIST (oldargtype))
985 {
986 error ("%Jprototype for '%D' declares more arguments "
987 "than previous old-style definition", newdecl, newdecl);
988 return false;
989 }
990 else if (END_OF_ARGLIST (newargtype))
991 {
992 error ("%Jprototype for '%D' declares fewer arguments "
993 "than previous old-style definition", newdecl, newdecl);
994 return false;
995 }
996
997 /* Type for passing arg must be consistent with that declared
998 for the arg. */
999 else if (! comptypes (oldargtype, newargtype, COMPARE_STRICT))
1000 {
1001 error ("%Jprototype for '%D' declares arg %d with incompatible type",
1002 newdecl, newdecl, i);
1003 return false;
1004 }
1005
1006 oldargs = TREE_CHAIN (oldargs);
1007 newargs = TREE_CHAIN (newargs);
1008 i++;
1009 }
1010
1011 /* If we get here, no errors were found, but do issue a warning
1012 for this poor-style construct. */
1013 warning ("%Jprototype for '%D' follows non-prototype definition",
1014 newdecl, newdecl);
1015 return true;
1016 #undef END_OF_ARGLIST
1017 }
1018
1019 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1020 first in a pair of mismatched declarations, using the diagnostic
1021 function DIAG. */
1022 static void
1023 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1024 {
1025 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1026 ;
1027 else if (DECL_INITIAL (decl))
1028 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
1029 else if (C_DECL_IMPLICIT (decl))
1030 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
1031 else
1032 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
1033 }
1034
1035 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1036 Returns true if the caller should proceed to merge the two, false
1037 if OLDDECL should simply be discarded. As a side effect, issues
1038 all necessary diagnostics for invalid or poor-style combinations.
1039 If it returns true, writes the types of NEWDECL and OLDDECL to
1040 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1041 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1042
1043 static bool
1044 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1045 tree *newtypep, tree *oldtypep)
1046 {
1047 tree newtype, oldtype;
1048 bool pedwarned = false;
1049 bool warned = false;
1050
1051 /* If we have error_mark_node for either decl or type, just discard
1052 the previous decl - we're in an error cascade already. */
1053 if (olddecl == error_mark_node || newdecl == error_mark_node)
1054 return false;
1055 *oldtypep = oldtype = TREE_TYPE (olddecl);
1056 *newtypep = newtype = TREE_TYPE (newdecl);
1057 if (oldtype == error_mark_node || newtype == error_mark_node)
1058 return false;
1059
1060 /* Two different categories of symbol altogether. This is an error
1061 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1062 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1063 {
1064 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1065 && DECL_BUILT_IN (olddecl)
1066 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1067 {
1068 error ("%J'%D' redeclared as different kind of symbol",
1069 newdecl, newdecl);
1070 locate_old_decl (olddecl, error);
1071 }
1072 else if (TREE_PUBLIC (newdecl))
1073 warning ("%Jbuilt-in function '%D' declared as non-function",
1074 newdecl, newdecl);
1075 else if (warn_shadow)
1076 warning ("%Jdeclaration of '%D' shadows a built-in function",
1077 newdecl, newdecl);
1078 return false;
1079 }
1080
1081 if (!comptypes (oldtype, newtype, COMPARE_STRICT))
1082 {
1083 if (TREE_CODE (olddecl) == FUNCTION_DECL
1084 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1085 {
1086 /* Accept harmless mismatch in function types.
1087 This is for the ffs and fprintf builtins. */
1088 tree trytype = match_builtin_function_types (newtype, oldtype);
1089
1090 if (trytype && comptypes (newtype, trytype, COMPARE_STRICT))
1091 *oldtypep = oldtype = trytype;
1092 else
1093 {
1094 /* If types don't match for a built-in, throw away the
1095 built-in. No point in calling locate_old_decl here, it
1096 won't print anything. */
1097 warning ("%Jconflicting types for built-in function '%D'",
1098 newdecl, newdecl);
1099 return false;
1100 }
1101 }
1102 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1103 && DECL_SOURCE_LINE (olddecl) == 0)
1104 {
1105 /* A conflicting function declaration for a predeclared
1106 function that isn't actually built in. Objective C uses
1107 these. The new declaration silently overrides everything
1108 but the volatility (i.e. noreturn) indication. See also
1109 below. FIXME: Make Objective C use normal builtins. */
1110 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1111 return false;
1112 }
1113 /* Permit void foo (...) to match int foo (...) if the latter is
1114 the definition and implicit int was used. See
1115 c-torture/compile/920625-2.c. */
1116 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1117 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1118 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1119 && C_FUNCTION_IMPLICIT_INT (newdecl))
1120 {
1121 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1122 /* Make sure we keep void as the return type. */
1123 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1124 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1125 pedwarned = true;
1126 }
1127 else
1128 {
1129 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1130 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1131 locate_old_decl (olddecl, error);
1132 return false;
1133 }
1134 }
1135
1136 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1137 but silently ignore the redeclaration if either is in a system
1138 header. (Conflicting redeclarations were handled above.) */
1139 if (TREE_CODE (newdecl) == TYPE_DECL)
1140 {
1141 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1142 return true; /* Allow OLDDECL to continue in use. */
1143
1144 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1145 locate_old_decl (olddecl, error);
1146 return false;
1147 }
1148
1149 /* Function declarations can either be 'static' or 'extern' (no
1150 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1151 can never conflict with each other on account of linkage (6.2.2p4).
1152 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1153 two definitions if one is 'extern inline' and one is not. The non-
1154 extern-inline definition supersedes the extern-inline definition. */
1155 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1156 {
1157 /* If you declare a built-in function name as static, or
1158 define the built-in with an old-style definition (so we
1159 can't validate the argument list) the built-in definition is
1160 overridden, but optionally warn this was a bad choice of name. */
1161 if (DECL_BUILT_IN (olddecl)
1162 && !C_DECL_DECLARED_BUILTIN (olddecl)
1163 && (!TREE_PUBLIC (newdecl)
1164 || (DECL_INITIAL (newdecl)
1165 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1166 {
1167 if (warn_shadow)
1168 warning ("%Jdeclaration of '%D' shadows a built-in function",
1169 newdecl, newdecl);
1170 /* Discard the old built-in function. */
1171 return false;
1172 }
1173
1174 if (DECL_INITIAL (newdecl))
1175 {
1176 if (DECL_INITIAL (olddecl)
1177 && !(DECL_DECLARED_INLINE_P (olddecl)
1178 && DECL_EXTERNAL (olddecl)
1179 && !(DECL_DECLARED_INLINE_P (newdecl)
1180 && DECL_EXTERNAL (newdecl))))
1181 {
1182 error ("%Jredefinition of '%D'", newdecl, newdecl);
1183 locate_old_decl (olddecl, error);
1184 return false;
1185 }
1186 }
1187 /* If we have a prototype after an old-style function definition,
1188 the argument types must be checked specially. */
1189 else if (DECL_INITIAL (olddecl)
1190 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1191 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1192 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1193 {
1194 locate_old_decl (olddecl, error);
1195 return false;
1196 }
1197 /* Mismatched non-static and static is considered poor style.
1198 We only diagnose static then non-static if -Wtraditional,
1199 because it is the most convenient way to get some effects
1200 (see e.g. what unwind-dw2-fde-glibc.c does to the definition
1201 of _Unwind_Find_FDE in unwind-dw2-fde.c). Revisit? */
1202 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1203 {
1204 /* A static function declaration for a predeclared function
1205 that isn't actually built in, silently overrides the
1206 default. Objective C uses these. See also above.
1207 FIXME: Make Objective C use normal builtins. */
1208 if (TREE_CODE (olddecl) == FUNCTION_DECL
1209 && DECL_SOURCE_LINE (olddecl) == 0)
1210 return false;
1211 else
1212 {
1213 warning ("%Jstatic declaration of '%D' follows "
1214 "non-static declaration", newdecl, newdecl);
1215 warned = true;
1216 }
1217 }
1218 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)
1219 && warn_traditional)
1220 {
1221 warning ("%Jnon-static declaration of '%D' follows "
1222 "static declaration", newdecl, newdecl);
1223 warned = true;
1224 }
1225 }
1226 else if (TREE_CODE (newdecl) == VAR_DECL)
1227 {
1228 /* Only variables can be thread-local, and all declarations must
1229 agree on this property. */
1230 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1231 {
1232 if (DECL_THREAD_LOCAL (newdecl))
1233 error ("%Jthread-local declaration of '%D' follows "
1234 "non-thread-local declaration", newdecl, newdecl);
1235 else
1236 error ("%Jnon-thread-local declaration of '%D' follows "
1237 "thread-local declaration", newdecl, newdecl);
1238
1239 locate_old_decl (olddecl, error);
1240 return false;
1241 }
1242
1243 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1244 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1245 {
1246 error ("%Jredefinition of '%D'", newdecl, newdecl);
1247 locate_old_decl (olddecl, error);
1248 return false;
1249 }
1250
1251 /* Objects declared at file scope: if at least one is 'extern',
1252 it's fine (6.2.2p4); otherwise the linkage must agree (6.2.2p7). */
1253 if (DECL_FILE_SCOPE_P (newdecl))
1254 {
1255 if (!DECL_EXTERNAL (newdecl)
1256 && !DECL_EXTERNAL (olddecl)
1257 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1258 {
1259 if (TREE_PUBLIC (newdecl))
1260 error ("%Jnon-static declaration of '%D' follows "
1261 "static declaration", newdecl, newdecl);
1262 else
1263 error ("%Jstatic declaration of '%D' follows "
1264 "non-static declaration", newdecl, newdecl);
1265
1266 locate_old_decl (olddecl, error);
1267 return false;
1268 }
1269 }
1270 /* Two objects with the same name declared at the same block
1271 scope must both be external references (6.7p3). */
1272 else if (DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1273 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1274 {
1275 if (DECL_EXTERNAL (newdecl))
1276 error ("%Jextern declaration of '%D' follows "
1277 "declaration with no linkage", newdecl, newdecl);
1278 else if (DECL_EXTERNAL (olddecl))
1279 error ("%Jdeclaration of '%D' with no linkage follows "
1280 "extern declaration", newdecl, newdecl);
1281 else
1282 error ("%Jredeclaration of '%D' with no linkage",
1283 newdecl, newdecl);
1284
1285 locate_old_decl (olddecl, error);
1286 return false;
1287 }
1288 }
1289
1290 /* warnings */
1291 /* All decls must agree on a non-default visibility. */
1292 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1293 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1294 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1295 {
1296 warning ("%Jredeclaration of '%D' with different visibility "
1297 "(old visibility preserved)", newdecl, newdecl);
1298 warned = true;
1299 }
1300
1301 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1302 {
1303 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1304 if (DECL_DECLARED_INLINE_P (newdecl)
1305 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1306 {
1307 warning ("%Jinline declaration of '%D' follows "
1308 "declaration with attribute noinline", newdecl, newdecl);
1309 warned = true;
1310 }
1311 else if (DECL_DECLARED_INLINE_P (olddecl)
1312 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1313 {
1314 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1315 "inline declaration ", newdecl, newdecl);
1316 warned = true;
1317 }
1318
1319 /* Inline declaration after use or definition.
1320 ??? Should we still warn about this now we have unit-at-a-time
1321 mode and can get it right? */
1322 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl))
1323 {
1324 if (TREE_USED (olddecl))
1325 {
1326 warning ("%J'%D' declared inline after being called",
1327 olddecl, olddecl);
1328 warned = true;
1329 }
1330 else if (DECL_INITIAL (olddecl))
1331 {
1332 warning ("%J'%D' declared inline after its definition",
1333 olddecl, olddecl);
1334 warned = true;
1335 }
1336 }
1337 }
1338 else /* PARM_DECL, VAR_DECL */
1339 {
1340 /* Redeclaration of a parameter is a constraint violation (this is
1341 not explicitly stated, but follows from C99 6.7p3 [no more than
1342 one declaration of the same identifier with no linkage in the
1343 same scope, except type tags] and 6.2.2p6 [parameters have no
1344 linkage]). We must check for a forward parameter declaration,
1345 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1346 an extension, the mandatory diagnostic for which is handled by
1347 mark_forward_parm_decls. */
1348
1349 if (TREE_CODE (newdecl) == PARM_DECL
1350 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1351 {
1352 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1353 locate_old_decl (olddecl, error);
1354 return false;
1355 }
1356
1357 /* These bits are only type qualifiers when applied to objects. */
1358 if (TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))
1359 {
1360 if (TREE_THIS_VOLATILE (newdecl))
1361 pedwarn ("%Jvolatile declaration of '%D' follows "
1362 "non-volatile declaration", newdecl, newdecl);
1363 else
1364 pedwarn ("%Jnon-volatile declaration of '%D' follows "
1365 "volatile declaration", newdecl, newdecl);
1366 pedwarned = true;
1367 }
1368 if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl))
1369 {
1370 if (TREE_READONLY (newdecl))
1371 pedwarn ("%Jconst declaration of '%D' follows "
1372 "non-const declaration", newdecl, newdecl);
1373 else
1374 pedwarn ("%Jnon-const declaration of '%D' follows "
1375 "const declaration", newdecl, newdecl);
1376 pedwarned = true;
1377 }
1378 }
1379
1380 /* Optional warning for completely redundant decls. */
1381 if (!warned && !pedwarned
1382 && warn_redundant_decls
1383 /* Don't warn about a function declaration followed by a
1384 definition. */
1385 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1386 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1387 /* Don't warn about an extern followed by a definition. */
1388 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1389 /* Don't warn about forward parameter decls. */
1390 && !(TREE_CODE (newdecl) == PARM_DECL
1391 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1392 {
1393 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1394 warned = true;
1395 }
1396
1397 /* Report location of previous decl/defn in a consistent manner. */
1398 if (warned || pedwarned)
1399 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1400
1401 return true;
1402 }
1403
1404 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1405 consistent with OLDDECL, but carries new information. Merge the
1406 new information into OLDDECL. This function issues no
1407 diagnostics. */
1408
1409 static void
1410 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1411 {
1412 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1413 && DECL_INITIAL (newdecl) != 0);
1414
1415 /* For real parm decl following a forward decl, rechain the old decl
1416 in its new location and clear TREE_ASM_WRITTEN (it's not a
1417 forward decl anymore). */
1418 if (TREE_CODE (newdecl) == PARM_DECL
1419 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1420 {
1421 struct c_binding *b, **here;
1422
1423 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1424 if ((*here)->decl == olddecl)
1425 goto found;
1426 abort ();
1427
1428 found:
1429 b = *here;
1430 *here = b->prev;
1431 b->prev = current_scope->bindings;
1432 current_scope->bindings = b;
1433
1434 TREE_ASM_WRITTEN (olddecl) = 0;
1435 }
1436
1437 DECL_ATTRIBUTES (newdecl)
1438 = targetm.merge_decl_attributes (olddecl, newdecl);
1439
1440 /* Merge the data types specified in the two decls. */
1441 TREE_TYPE (newdecl)
1442 = TREE_TYPE (olddecl)
1443 = common_type (newtype, oldtype);
1444
1445 /* Lay the type out, unless already done. */
1446 if (oldtype != TREE_TYPE (newdecl))
1447 {
1448 if (TREE_TYPE (newdecl) != error_mark_node)
1449 layout_type (TREE_TYPE (newdecl));
1450 if (TREE_CODE (newdecl) != FUNCTION_DECL
1451 && TREE_CODE (newdecl) != TYPE_DECL
1452 && TREE_CODE (newdecl) != CONST_DECL)
1453 layout_decl (newdecl, 0);
1454 }
1455 else
1456 {
1457 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1458 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1459 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1460 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1461 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1462 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1463 {
1464 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1465 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1466 }
1467 }
1468
1469 /* Keep the old rtl since we can safely use it. */
1470 COPY_DECL_RTL (olddecl, newdecl);
1471
1472 /* Merge the type qualifiers. */
1473 if (TREE_READONLY (newdecl))
1474 TREE_READONLY (olddecl) = 1;
1475
1476 if (TREE_THIS_VOLATILE (newdecl))
1477 {
1478 TREE_THIS_VOLATILE (olddecl) = 1;
1479 if (TREE_CODE (newdecl) == VAR_DECL)
1480 make_var_volatile (newdecl);
1481 }
1482
1483 /* Keep source location of definition rather than declaration. */
1484 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1485 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1486
1487 /* Merge the unused-warning information. */
1488 if (DECL_IN_SYSTEM_HEADER (olddecl))
1489 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1490 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1491 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1492
1493 /* Merge the initialization information. */
1494 if (DECL_INITIAL (newdecl) == 0)
1495 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1496
1497 /* Merge the section attribute.
1498 We want to issue an error if the sections conflict but that must be
1499 done later in decl_attributes since we are called before attributes
1500 are assigned. */
1501 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1502 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1503
1504 /* Copy the assembler name.
1505 Currently, it can only be defined in the prototype. */
1506 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1507
1508 /* If either declaration has a nondefault visibility, use it. */
1509 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1510 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1511
1512 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1513 {
1514 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1515 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1516 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1517 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1518 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1519 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1520 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1521 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1522 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1523 }
1524
1525 /* Merge the storage class information. */
1526 merge_weak (newdecl, olddecl);
1527
1528 /* For functions, static overrides non-static. */
1529 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1530 {
1531 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1532 /* This is since we don't automatically
1533 copy the attributes of NEWDECL into OLDDECL. */
1534 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1535 /* If this clears `static', clear it in the identifier too. */
1536 if (! TREE_PUBLIC (olddecl))
1537 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1538 }
1539 if (DECL_EXTERNAL (newdecl))
1540 {
1541 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1542 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1543
1544 /* An extern decl does not override previous storage class. */
1545 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1546 if (! DECL_EXTERNAL (newdecl))
1547 {
1548 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1549 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1550 }
1551 }
1552 else
1553 {
1554 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1555 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1556 }
1557
1558 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1559 {
1560 /* If we're redefining a function previously defined as extern
1561 inline, make sure we emit debug info for the inline before we
1562 throw it away, in case it was inlined into a function that hasn't
1563 been written out yet. */
1564 if (new_is_definition && DECL_INITIAL (olddecl))
1565 {
1566 if (TREE_USED (olddecl)
1567 /* In unit-at-a-time mode we never inline re-defined extern
1568 inline functions. */
1569 && !flag_unit_at_a_time
1570 && cgraph_function_possibly_inlined_p (olddecl))
1571 (*debug_hooks->outlining_inline_function) (olddecl);
1572
1573 /* The new defn must not be inline. */
1574 DECL_INLINE (newdecl) = 0;
1575 DECL_UNINLINABLE (newdecl) = 1;
1576 }
1577 else
1578 {
1579 /* If either decl says `inline', this fn is inline,
1580 unless its definition was passed already. */
1581 if (DECL_DECLARED_INLINE_P (newdecl)
1582 || DECL_DECLARED_INLINE_P (olddecl))
1583 DECL_DECLARED_INLINE_P (newdecl) = 1;
1584
1585 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1586 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1587 }
1588
1589 if (DECL_BUILT_IN (olddecl))
1590 {
1591 /* If redeclaring a builtin function, it stays built in.
1592 But it gets tagged as having been declared. */
1593 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1594 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1595 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1596 }
1597
1598 /* Also preserve various other info from the definition. */
1599 if (! new_is_definition)
1600 {
1601 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1602 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1603 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1604 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1605 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1606
1607 /* Set DECL_INLINE on the declaration if we've got a body
1608 from which to instantiate. */
1609 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1610 {
1611 DECL_INLINE (newdecl) = 1;
1612 DECL_ABSTRACT_ORIGIN (newdecl)
1613 = DECL_ABSTRACT_ORIGIN (olddecl);
1614 }
1615 }
1616 else
1617 {
1618 /* If a previous declaration said inline, mark the
1619 definition as inlinable. */
1620 if (DECL_DECLARED_INLINE_P (newdecl)
1621 && ! DECL_UNINLINABLE (newdecl))
1622 DECL_INLINE (newdecl) = 1;
1623 }
1624 }
1625
1626 /* This bit must not get wiped out. */
1627 C_DECL_IN_EXTERNAL_SCOPE (newdecl) |= C_DECL_IN_EXTERNAL_SCOPE (olddecl);
1628
1629 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1630 But preserve OLDDECL's DECL_UID. */
1631 {
1632 unsigned olddecl_uid = DECL_UID (olddecl);
1633
1634 memcpy ((char *) olddecl + sizeof (struct tree_common),
1635 (char *) newdecl + sizeof (struct tree_common),
1636 sizeof (struct tree_decl) - sizeof (struct tree_common));
1637 DECL_UID (olddecl) = olddecl_uid;
1638 }
1639
1640 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1641 so that encode_section_info has a chance to look at the new decl
1642 flags and attributes. */
1643 if (DECL_RTL_SET_P (olddecl)
1644 && (TREE_CODE (olddecl) == FUNCTION_DECL
1645 || (TREE_CODE (olddecl) == VAR_DECL
1646 && TREE_STATIC (olddecl))))
1647 make_decl_rtl (olddecl, NULL);
1648 }
1649
1650 /* Handle when a new declaration NEWDECL has the same name as an old
1651 one OLDDECL in the same binding contour. Prints an error message
1652 if appropriate.
1653
1654 If safely possible, alter OLDDECL to look like NEWDECL, and return
1655 true. Otherwise, return false. */
1656
1657 static bool
1658 duplicate_decls (tree newdecl, tree olddecl)
1659 {
1660 tree newtype, oldtype;
1661
1662 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1663 return false;
1664
1665 merge_decls (newdecl, olddecl, newtype, oldtype);
1666 return true;
1667 }
1668
1669 \f
1670 /* Check whether decl-node NEW shadows an existing declaration. */
1671 static void
1672 warn_if_shadowing (tree new)
1673 {
1674 struct c_binding *b;
1675
1676 /* Shadow warnings wanted? */
1677 if (!warn_shadow
1678 /* No shadow warnings for internally generated vars. */
1679 || DECL_SOURCE_LINE (new) == 0
1680 /* No shadow warnings for vars made for inlining. */
1681 || DECL_FROM_INLINE (new)
1682 /* Don't warn about the parm names in function declarator
1683 within a function declarator. It would be nice to avoid
1684 warning in any function declarator in a declaration, as
1685 opposed to a definition, but there is no way to tell
1686 it's not a definition at this point. */
1687 || (TREE_CODE (new) == PARM_DECL && current_scope->outer->parm_flag))
1688 return;
1689
1690 /* Is anything being shadowed? Do not be confused by a second binding
1691 to the same decl in the externals scope. */
1692 for (b = I_SYMBOL_BINDING (DECL_NAME (new)); b; b = b->shadowed)
1693 if (b->decl && b->decl != new && b->contour != external_scope)
1694 {
1695 tree old = b->decl;
1696
1697 if (TREE_CODE (old) == PARM_DECL)
1698 warning ("%Jdeclaration of '%D' shadows a parameter", new, new);
1699 else if (DECL_FILE_SCOPE_P (old))
1700 warning ("%Jdeclaration of '%D' shadows a global declaration",
1701 new, new);
1702 else if (TREE_CODE (old) == FUNCTION_DECL && DECL_BUILT_IN (old))
1703 warning ("%Jdeclaration of '%D' shadows a built-in function",
1704 new, new);
1705 else
1706 warning ("%Jdeclaration of '%D' shadows a previous local", new, new);
1707
1708 if (TREE_CODE (old) != FUNCTION_DECL || !DECL_BUILT_IN (old))
1709 warning ("%Jshadowed declaration is here", old);
1710
1711 break;
1712 }
1713 }
1714
1715
1716 /* Subroutine of pushdecl.
1717
1718 X is a TYPE_DECL for a typedef statement. Create a brand new
1719 ..._TYPE node (which will be just a variant of the existing
1720 ..._TYPE node with identical properties) and then install X
1721 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1722
1723 The whole point here is to end up with a situation where each
1724 and every ..._TYPE node the compiler creates will be uniquely
1725 associated with AT MOST one node representing a typedef name.
1726 This way, even though the compiler substitutes corresponding
1727 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1728 early on, later parts of the compiler can always do the reverse
1729 translation and get back the corresponding typedef name. For
1730 example, given:
1731
1732 typedef struct S MY_TYPE;
1733 MY_TYPE object;
1734
1735 Later parts of the compiler might only know that `object' was of
1736 type `struct S' if it were not for code just below. With this
1737 code however, later parts of the compiler see something like:
1738
1739 struct S' == struct S
1740 typedef struct S' MY_TYPE;
1741 struct S' object;
1742
1743 And they can then deduce (from the node for type struct S') that
1744 the original object declaration was:
1745
1746 MY_TYPE object;
1747
1748 Being able to do this is important for proper support of protoize,
1749 and also for generating precise symbolic debugging information
1750 which takes full account of the programmer's (typedef) vocabulary.
1751
1752 Obviously, we don't want to generate a duplicate ..._TYPE node if
1753 the TYPE_DECL node that we are now processing really represents a
1754 standard built-in type.
1755
1756 Since all standard types are effectively declared at line zero
1757 in the source file, we can easily check to see if we are working
1758 on a standard type by checking the current value of lineno. */
1759
1760 static void
1761 clone_underlying_type (tree x)
1762 {
1763 if (DECL_SOURCE_LINE (x) == 0)
1764 {
1765 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1766 TYPE_NAME (TREE_TYPE (x)) = x;
1767 }
1768 else if (TREE_TYPE (x) != error_mark_node
1769 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1770 {
1771 tree tt = TREE_TYPE (x);
1772 DECL_ORIGINAL_TYPE (x) = tt;
1773 tt = build_type_copy (tt);
1774 TYPE_NAME (tt) = x;
1775 TREE_USED (tt) = TREE_USED (x);
1776 TREE_TYPE (x) = tt;
1777 }
1778 }
1779
1780 /* Record a decl-node X as belonging to the current lexical scope.
1781 Check for errors (such as an incompatible declaration for the same
1782 name already seen in the same scope).
1783
1784 Returns either X or an old decl for the same name.
1785 If an old decl is returned, it may have been smashed
1786 to agree with what X says. */
1787
1788 tree
1789 pushdecl (tree x)
1790 {
1791 tree name = DECL_NAME (x);
1792 struct c_scope *scope = current_scope;
1793 struct c_binding *b;
1794
1795 /* Functions need the lang_decl data. */
1796 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1797 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1798
1799 /* A local extern declaration for a function doesn't constitute nesting.
1800 A local auto declaration does, since it's a forward decl
1801 for a nested function coming later. */
1802 if (current_function_decl == NULL
1803 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1804 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1805 DECL_CONTEXT (x) = current_file_decl;
1806 else
1807 DECL_CONTEXT (x) = current_function_decl;
1808
1809 /* Anonymous decls are just inserted in the scope. */
1810 if (!name)
1811 {
1812 bind (name, x, scope);
1813 return x;
1814 }
1815
1816 /* First, see if there is another declaration with the same name in
1817 the current scope. If there is, duplicate_decls may do all the
1818 work for us. If duplicate_decls returns false, that indicates
1819 two incompatible decls in the same scope; we are to silently
1820 replace the old one (duplicate_decls has issued all appropriate
1821 diagnostics). In particular, we should not consider possible
1822 duplicates in the external scope, or shadowing. */
1823 b = I_SYMBOL_BINDING (name);
1824 if (b && b->contour == scope)
1825 {
1826 if (duplicate_decls (x, b->decl))
1827 return b->decl;
1828 else
1829 goto skip_external_and_shadow_checks;
1830 }
1831
1832 /* All declarations with external linkage, and all external
1833 references, go in the external scope, no matter what scope is
1834 current. However, the binding in that scope is ignored for
1835 purposes of normal name lookup. A separate binding structure is
1836 created in the requested scope; this governs the normal
1837 visibility of the symbol.
1838
1839 The binding in the externals scope is used exclusively for
1840 detecting duplicate declarations of the same object, no matter
1841 what scope they are in; this is what we do here. (C99 6.2.7p2:
1842 All declarations that refer to the same object or function shall
1843 have compatible type; otherwise, the behavior is undefined.) */
1844 if (DECL_EXTERNAL (x) || scope == file_scope)
1845 {
1846 if (warn_nested_externs
1847 && scope != file_scope
1848 && !DECL_IN_SYSTEM_HEADER (x))
1849 warning ("nested extern declaration of `%s'",
1850 IDENTIFIER_POINTER (name));
1851
1852 while (b && b->contour != external_scope)
1853 b = b->shadowed;
1854
1855 /* The point of the same_translation_unit_p check here is,
1856 we want to detect a duplicate decl for a construct like
1857 foo() { extern bar(); } ... static bar(); but not if
1858 they are in different translation units. In any case,
1859 the static does not go in the externals scope. */
1860 if (b
1861 && (DECL_EXTERNAL (x) || TREE_PUBLIC (x)
1862 || same_translation_unit_p (x, b->decl))
1863 && duplicate_decls (x, b->decl))
1864 {
1865 bind (name, b->decl, scope);
1866 return b->decl;
1867 }
1868 else if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1869 {
1870 C_DECL_IN_EXTERNAL_SCOPE (x) = 1;
1871 bind (name, x, external_scope);
1872 }
1873 }
1874
1875 warn_if_shadowing (x);
1876
1877 skip_external_and_shadow_checks:
1878 if (TREE_CODE (x) == TYPE_DECL)
1879 clone_underlying_type (x);
1880
1881 bind (name, x, scope);
1882
1883 /* If x's type is incomplete because it's based on a
1884 structure or union which has not yet been fully declared,
1885 attach it to that structure or union type, so we can go
1886 back and complete the variable declaration later, if the
1887 structure or union gets fully declared.
1888
1889 If the input is erroneous, we can have error_mark in the type
1890 slot (e.g. "f(void a, ...)") - that doesn't count as an
1891 incomplete type. */
1892 if (TREE_TYPE (x) != error_mark_node
1893 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1894 {
1895 tree element = TREE_TYPE (x);
1896
1897 while (TREE_CODE (element) == ARRAY_TYPE)
1898 element = TREE_TYPE (element);
1899 element = TYPE_MAIN_VARIANT (element);
1900
1901 if ((TREE_CODE (element) == RECORD_TYPE
1902 || TREE_CODE (element) == UNION_TYPE)
1903 && (TREE_CODE (x) != TYPE_DECL
1904 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1905 && !COMPLETE_TYPE_P (element))
1906 C_TYPE_INCOMPLETE_VARS (element)
1907 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1908 }
1909 return x;
1910 }
1911
1912 /* Record X as belonging to file scope.
1913 This is used only internally by the Objective-C front end,
1914 and is limited to its needs. duplicate_decls is not called;
1915 if there is any preexisting decl for this identifier, it is an ICE. */
1916
1917 tree
1918 pushdecl_top_level (tree x)
1919 {
1920 tree name;
1921
1922 if (TREE_CODE (x) != VAR_DECL)
1923 abort ();
1924
1925 name = DECL_NAME (x);
1926
1927 if (I_SYMBOL_BINDING (name))
1928 abort ();
1929
1930 DECL_CONTEXT (x) = current_file_decl;
1931 if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1932 {
1933 C_DECL_IN_EXTERNAL_SCOPE (x) = 1;
1934 bind (name, x, external_scope);
1935 }
1936 if (file_scope)
1937 bind (name, x, file_scope);
1938
1939 return x;
1940 }
1941 \f
1942 static void
1943 implicit_decl_warning (tree id, tree olddecl)
1944 {
1945 void (*diag) (const char *, ...);
1946 switch (mesg_implicit_function_declaration)
1947 {
1948 case 0: return;
1949 case 1: diag = warning; break;
1950 case 2: diag = error; break;
1951 default: abort ();
1952 }
1953
1954 diag (N_("implicit declaration of function '%E'"), id);
1955 if (olddecl)
1956 locate_old_decl (olddecl, diag);
1957 }
1958
1959 /* Generate an implicit declaration for identifier FUNCTIONID as a
1960 function of type int (). */
1961
1962 tree
1963 implicitly_declare (tree functionid)
1964 {
1965 tree decl = lookup_name_in_scope (functionid, external_scope);
1966
1967 if (decl)
1968 {
1969 /* FIXME: Objective-C has weird not-really-builtin functions
1970 which are supposed to be visible automatically. They wind up
1971 in the external scope because they're pushed before the file
1972 scope gets created. Catch this here and rebind them into the
1973 file scope. */
1974 if (!DECL_BUILT_IN (decl) && DECL_SOURCE_LINE (decl) == 0)
1975 {
1976 bind (functionid, decl, file_scope);
1977 return decl;
1978 }
1979 else
1980 {
1981 /* Implicit declaration of a function already declared
1982 (somehow) in a different scope, or as a built-in.
1983 If this is the first time this has happened, warn;
1984 then recycle the old declaration. */
1985 if (!C_DECL_IMPLICIT (decl))
1986 {
1987 implicit_decl_warning (functionid, decl);
1988 C_DECL_IMPLICIT (decl) = 1;
1989 }
1990 bind (functionid, decl, current_scope);
1991 return decl;
1992 }
1993 }
1994
1995 /* Not seen before. */
1996 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1997 DECL_EXTERNAL (decl) = 1;
1998 TREE_PUBLIC (decl) = 1;
1999 C_DECL_IMPLICIT (decl) = 1;
2000 implicit_decl_warning (functionid, 0);
2001
2002 /* C89 says implicit declarations are in the innermost block.
2003 So we record the decl in the standard fashion. */
2004 decl = pushdecl (decl);
2005
2006 /* No need to call objc_check_decl here - it's a function type. */
2007 rest_of_decl_compilation (decl, NULL, 0, 0);
2008
2009 /* Write a record describing this implicit function declaration
2010 to the prototypes file (if requested). */
2011 gen_aux_info_record (decl, 0, 1, 0);
2012
2013 /* Possibly apply some default attributes to this implicit declaration. */
2014 decl_attributes (&decl, NULL_TREE, 0);
2015
2016 return decl;
2017 }
2018
2019 /* Issue an error message for a reference to an undeclared variable
2020 ID, including a reference to a builtin outside of function-call
2021 context. Establish a binding of the identifier to error_mark_node
2022 in an appropriate scope, which will suppress further errors for the
2023 same identifier. */
2024 void
2025 undeclared_variable (tree id)
2026 {
2027 static bool already = false;
2028 struct c_scope *scope;
2029
2030 if (current_function_decl == 0)
2031 {
2032 error ("'%E' undeclared here (not in a function)", id);
2033 scope = current_scope;
2034 }
2035 else
2036 {
2037 error ("'%E' undeclared (first use in this function)", id);
2038
2039 if (! already)
2040 {
2041 error ("(Each undeclared identifier is reported only once");
2042 error ("for each function it appears in.)");
2043 already = true;
2044 }
2045
2046 /* If we are parsing old-style parameter decls, current_function_decl
2047 will be nonnull but current_function_scope will be null. */
2048 scope = current_function_scope ? current_function_scope : current_scope;
2049 }
2050 bind (id, error_mark_node, scope);
2051 }
2052 \f
2053 /* Subroutine of lookup_label, declare_label, define_label: construct a
2054 LABEL_DECL with all the proper frills. */
2055
2056 static tree
2057 make_label (tree name, location_t location)
2058 {
2059 tree label = build_decl (LABEL_DECL, name, void_type_node);
2060
2061 DECL_CONTEXT (label) = current_function_decl;
2062 DECL_MODE (label) = VOIDmode;
2063 DECL_SOURCE_LOCATION (label) = location;
2064
2065 return label;
2066 }
2067
2068 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2069 Create one if none exists so far for the current function.
2070 This is called when a label is used in a goto expression or
2071 has its address taken. */
2072
2073 tree
2074 lookup_label (tree name)
2075 {
2076 tree label;
2077
2078 if (current_function_decl == 0)
2079 {
2080 error ("label %s referenced outside of any function",
2081 IDENTIFIER_POINTER (name));
2082 return 0;
2083 }
2084
2085 /* Use a label already defined or ref'd with this name, but not if
2086 it is inherited from a containing function and wasn't declared
2087 using __label__. */
2088 label = I_LABEL_DECL (name);
2089 if (label && (DECL_CONTEXT (label) == current_function_decl
2090 || C_DECLARED_LABEL_FLAG (label)))
2091 {
2092 /* If the label has only been declared, update its apparent
2093 location to point here, for better diagnostics if it
2094 turns out not to have been defined. */
2095 if (!TREE_USED (label))
2096 DECL_SOURCE_LOCATION (label) = input_location;
2097 return label;
2098 }
2099
2100 /* No label binding for that identifier; make one. */
2101 label = make_label (name, input_location);
2102
2103 /* Ordinary labels go in the current function scope. */
2104 bind (name, label, current_function_scope);
2105 return label;
2106 }
2107
2108 /* Make a label named NAME in the current function, shadowing silently
2109 any that may be inherited from containing functions or containing
2110 scopes. This is called for __label__ declarations. */
2111
2112 /* Note that valid use, if the label being shadowed comes from another
2113 scope in the same function, requires calling declare_nonlocal_label
2114 right away. (Is this still true? -zw 2003-07-17) */
2115
2116 tree
2117 declare_label (tree name)
2118 {
2119 struct c_binding *b = I_LABEL_BINDING (name);
2120 tree label;
2121
2122 /* Check to make sure that the label hasn't already been declared
2123 at this scope */
2124 if (b && b->contour == current_scope)
2125 {
2126 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2127 locate_old_decl (b->decl, error);
2128
2129 /* Just use the previous declaration. */
2130 return b->decl;
2131 }
2132
2133 label = make_label (name, input_location);
2134 C_DECLARED_LABEL_FLAG (label) = 1;
2135
2136 /* Declared labels go in the current scope. */
2137 bind (name, label, current_scope);
2138 return label;
2139 }
2140
2141 /* Define a label, specifying the location in the source file.
2142 Return the LABEL_DECL node for the label, if the definition is valid.
2143 Otherwise return 0. */
2144
2145 tree
2146 define_label (location_t location, tree name)
2147 {
2148 /* Find any preexisting label with this name. It is an error
2149 if that label has already been defined in this function, or
2150 if there is a containing function with a declared label with
2151 the same name. */
2152 tree label = I_LABEL_DECL (name);
2153
2154 if (label
2155 && ((DECL_CONTEXT (label) == current_function_decl
2156 && DECL_INITIAL (label) != 0)
2157 || (DECL_CONTEXT (label) != current_function_decl
2158 && C_DECLARED_LABEL_FLAG (label))))
2159 {
2160 error ("%Hduplicate label `%D'", &location, label);
2161 locate_old_decl (label, error);
2162 return 0;
2163 }
2164 else if (label && DECL_CONTEXT (label) == current_function_decl)
2165 {
2166 /* The label has been used or declared already in this function,
2167 but not defined. Update its location to point to this
2168 definition. */
2169 DECL_SOURCE_LOCATION (label) = location;
2170 }
2171 else
2172 {
2173 /* No label binding for that identifier; make one. */
2174 label = make_label (name, location);
2175
2176 /* Ordinary labels go in the current function scope. */
2177 bind (name, label, current_function_scope);
2178 }
2179
2180 if (warn_traditional && !in_system_header && lookup_name (name))
2181 warning ("%Htraditional C lacks a separate namespace for labels, "
2182 "identifier `%s' conflicts", &location,
2183 IDENTIFIER_POINTER (name));
2184
2185 /* Mark label as having been defined. */
2186 DECL_INITIAL (label) = error_mark_node;
2187 return label;
2188 }
2189 \f
2190 /* Given NAME, an IDENTIFIER_NODE,
2191 return the structure (or union or enum) definition for that name.
2192 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2193 CODE says which kind of type the caller wants;
2194 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2195 If the wrong kind of type is found, an error is reported. */
2196
2197 static tree
2198 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2199 {
2200 struct c_binding *b = I_TAG_BINDING (name);
2201 int thislevel = 0;
2202
2203 if (!b || !b->decl)
2204 return 0;
2205
2206 /* We only care about whether it's in this level if
2207 thislevel_only was set or it might be a type clash. */
2208 if (thislevel_only || TREE_CODE (b->decl) != code)
2209 {
2210 /* For our purposes, a tag in the external scope is the same as
2211 a tag in the file scope. (Primarily relevant to Objective-C
2212 and its builtin structure tags, which get pushed before the
2213 file scope is created.) */
2214 if (b->contour == current_scope
2215 || (current_scope == file_scope && b->contour == external_scope))
2216 thislevel = 1;
2217 }
2218
2219 if (thislevel_only && !thislevel)
2220 return 0;
2221
2222 if (TREE_CODE (b->decl) != code)
2223 {
2224 /* Definition isn't the kind we were looking for. */
2225 pending_invalid_xref = name;
2226 pending_invalid_xref_location = input_location;
2227
2228 /* If in the same binding level as a declaration as a tag
2229 of a different type, this must not be allowed to
2230 shadow that tag, so give the error immediately.
2231 (For example, "struct foo; union foo;" is invalid.) */
2232 if (thislevel)
2233 pending_xref_error ();
2234 }
2235 return b->decl;
2236 }
2237
2238 /* Print an error message now
2239 for a recent invalid struct, union or enum cross reference.
2240 We don't print them immediately because they are not invalid
2241 when used in the `struct foo;' construct for shadowing. */
2242
2243 void
2244 pending_xref_error (void)
2245 {
2246 if (pending_invalid_xref != 0)
2247 error ("%H`%s' defined as wrong kind of tag",
2248 &pending_invalid_xref_location,
2249 IDENTIFIER_POINTER (pending_invalid_xref));
2250 pending_invalid_xref = 0;
2251 }
2252
2253 \f
2254 /* Look up NAME in the current scope and its superiors
2255 in the namespace of variables, functions and typedefs.
2256 Return a ..._DECL node of some kind representing its definition,
2257 or return 0 if it is undefined. */
2258
2259 tree
2260 lookup_name (tree name)
2261 {
2262 struct c_binding *b = I_SYMBOL_BINDING (name);
2263 if (b && (b->contour != external_scope || TREE_CODE (b->decl) == TYPE_DECL))
2264 return b->decl;
2265 return 0;
2266 }
2267
2268 /* Similar to `lookup_name' but look only at the indicated scope. */
2269
2270 static tree
2271 lookup_name_in_scope (tree name, struct c_scope *scope)
2272 {
2273 struct c_binding *b;
2274
2275 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2276 if (b->contour == scope)
2277 return b->decl;
2278 return 0;
2279 }
2280 \f
2281 /* Create the predefined scalar types of C,
2282 and some nodes representing standard constants (0, 1, (void *) 0).
2283 Initialize the global scope.
2284 Make definitions for built-in primitive functions. */
2285
2286 void
2287 c_init_decl_processing (void)
2288 {
2289 tree endlink;
2290 tree ptr_ftype_void, ptr_ftype_ptr;
2291 location_t save_loc = input_location;
2292
2293 /* Adds some ggc roots, and reserved words for c-parse.in. */
2294 c_parse_init ();
2295
2296 current_function_decl = 0;
2297
2298 /* Make the externals scope. */
2299 push_scope ();
2300 external_scope = current_scope;
2301
2302 /* Declarations from c_common_nodes_and_builtins must not be associated
2303 with this input file, lest we get differences between using and not
2304 using preprocessed headers. */
2305 input_location.file = "<internal>";
2306 input_location.line = 0;
2307
2308 build_common_tree_nodes (flag_signed_char);
2309
2310 c_common_nodes_and_builtins ();
2311
2312 /* In C, comparisons and TRUTH_* expressions have type int. */
2313 truthvalue_type_node = integer_type_node;
2314 truthvalue_true_node = integer_one_node;
2315 truthvalue_false_node = integer_zero_node;
2316
2317 /* Even in C99, which has a real boolean type. */
2318 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2319 boolean_type_node));
2320
2321 endlink = void_list_node;
2322 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2323 ptr_ftype_ptr
2324 = build_function_type (ptr_type_node,
2325 tree_cons (NULL_TREE, ptr_type_node, endlink));
2326
2327 input_location = save_loc;
2328
2329 pedantic_lvalues = true;
2330
2331 make_fname_decl = c_make_fname_decl;
2332 start_fname_decls ();
2333 }
2334
2335 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2336 decl, NAME is the initialization string and TYPE_DEP indicates whether
2337 NAME depended on the type of the function. As we don't yet implement
2338 delayed emission of static data, we mark the decl as emitted
2339 so it is not placed in the output. Anything using it must therefore pull
2340 out the STRING_CST initializer directly. FIXME. */
2341
2342 static tree
2343 c_make_fname_decl (tree id, int type_dep)
2344 {
2345 const char *name = fname_as_string (type_dep);
2346 tree decl, type, init;
2347 size_t length = strlen (name);
2348
2349 type = build_array_type
2350 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2351 build_index_type (size_int (length)));
2352
2353 decl = build_decl (VAR_DECL, id, type);
2354
2355 TREE_STATIC (decl) = 1;
2356 TREE_READONLY (decl) = 1;
2357 DECL_ARTIFICIAL (decl) = 1;
2358
2359 init = build_string (length + 1, name);
2360 TREE_TYPE (init) = type;
2361 DECL_INITIAL (decl) = init;
2362
2363 TREE_USED (decl) = 1;
2364
2365 if (current_function_decl)
2366 {
2367 DECL_CONTEXT (decl) = current_function_decl;
2368 bind (id, decl, current_function_scope);
2369 }
2370
2371 finish_decl (decl, init, NULL_TREE);
2372
2373 return decl;
2374 }
2375
2376 /* Return a definition for a builtin function named NAME and whose data type
2377 is TYPE. TYPE should be a function type with argument types.
2378 FUNCTION_CODE tells later passes how to compile calls to this function.
2379 See tree.h for its possible values.
2380
2381 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2382 the name to be called if we can't opencode the function. If
2383 ATTRS is nonzero, use that for the function's attribute list. */
2384
2385 tree
2386 builtin_function (const char *name, tree type, int function_code,
2387 enum built_in_class class, const char *library_name,
2388 tree attrs)
2389 {
2390 tree id = get_identifier (name);
2391 tree decl = build_decl (FUNCTION_DECL, id, type);
2392 TREE_PUBLIC (decl) = 1;
2393 DECL_EXTERNAL (decl) = 1;
2394 DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared (sizeof (struct lang_decl));
2395 DECL_BUILT_IN_CLASS (decl) = class;
2396 DECL_FUNCTION_CODE (decl) = function_code;
2397 if (library_name)
2398 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2399 make_decl_rtl (decl, NULL);
2400
2401 /* Should never be called on a symbol with a preexisting meaning. */
2402 if (I_SYMBOL_BINDING (id))
2403 abort ();
2404
2405 C_DECL_IN_EXTERNAL_SCOPE (decl) = 1;
2406 bind (id, decl, external_scope);
2407
2408 /* Builtins in the implementation namespace are made visible without
2409 needing to be explicitly declared. See push_file_scope. */
2410 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2411 {
2412 TREE_CHAIN (decl) = visible_builtins;
2413 visible_builtins = decl;
2414 }
2415
2416 /* Possibly apply some default attributes to this built-in function. */
2417 if (attrs)
2418 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2419 else
2420 decl_attributes (&decl, NULL_TREE, 0);
2421
2422 return decl;
2423 }
2424 \f
2425 /* Called when a declaration is seen that contains no names to declare.
2426 If its type is a reference to a structure, union or enum inherited
2427 from a containing scope, shadow that tag name for the current scope
2428 with a forward reference.
2429 If its type defines a new named structure or union
2430 or defines an enum, it is valid but we need not do anything here.
2431 Otherwise, it is an error. */
2432
2433 void
2434 shadow_tag (tree declspecs)
2435 {
2436 shadow_tag_warned (declspecs, 0);
2437 }
2438
2439 void
2440 shadow_tag_warned (tree declspecs, int warned)
2441
2442
2443 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2444 no pedwarn. */
2445 {
2446 int found_tag = 0;
2447 tree link;
2448 tree specs, attrs;
2449
2450 pending_invalid_xref = 0;
2451
2452 /* Remove the attributes from declspecs, since they will confuse the
2453 following code. */
2454 split_specs_attrs (declspecs, &specs, &attrs);
2455
2456 for (link = specs; link; link = TREE_CHAIN (link))
2457 {
2458 tree value = TREE_VALUE (link);
2459 enum tree_code code = TREE_CODE (value);
2460
2461 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2462 /* Used to test also that TYPE_SIZE (value) != 0.
2463 That caused warning for `struct foo;' at top level in the file. */
2464 {
2465 tree name = TYPE_NAME (value);
2466 tree t;
2467
2468 found_tag++;
2469
2470 if (name == 0)
2471 {
2472 if (warned != 1 && code != ENUMERAL_TYPE)
2473 /* Empty unnamed enum OK */
2474 {
2475 pedwarn ("unnamed struct/union that defines no instances");
2476 warned = 1;
2477 }
2478 }
2479 else
2480 {
2481 t = lookup_tag (code, name, 1);
2482
2483 if (t == 0)
2484 {
2485 t = make_node (code);
2486 pushtag (name, t);
2487 }
2488 }
2489 }
2490 else
2491 {
2492 if (!warned && ! in_system_header)
2493 {
2494 warning ("useless keyword or type name in empty declaration");
2495 warned = 2;
2496 }
2497 }
2498 }
2499
2500 if (found_tag > 1)
2501 error ("two types specified in one empty declaration");
2502
2503 if (warned != 1)
2504 {
2505 if (found_tag == 0)
2506 pedwarn ("empty declaration");
2507 }
2508 }
2509 \f
2510 /* Construct an array declarator. EXPR is the expression inside [], or
2511 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2512 to the pointer to which a parameter array is converted). STATIC_P is
2513 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2514 is nonzero is the array is [*], a VLA of unspecified length which is
2515 nevertheless a complete type (not currently implemented by GCC),
2516 zero otherwise. The declarator is constructed as an ARRAY_REF
2517 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2518 left of the [] (filled by in set_array_declarator_type) and operand 1
2519 is the expression inside; whose TREE_TYPE is the type qualifiers and
2520 which has TREE_STATIC set if "static" is used. */
2521
2522 tree
2523 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2524 {
2525 tree decl;
2526 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2527 TREE_TYPE (decl) = quals;
2528 TREE_STATIC (decl) = (static_p ? 1 : 0);
2529 if (pedantic && !flag_isoc99)
2530 {
2531 if (static_p || quals != NULL_TREE)
2532 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2533 if (vla_unspec_p)
2534 pedwarn ("ISO C90 does not support `[*]' array declarators");
2535 }
2536 if (vla_unspec_p)
2537 warning ("GCC does not yet properly implement `[*]' array declarators");
2538 return decl;
2539 }
2540
2541 /* Set the type of an array declarator. DECL is the declarator, as
2542 constructed by build_array_declarator; TYPE is what appears on the left
2543 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2544 abstract declarator, zero otherwise; this is used to reject static and
2545 type qualifiers in abstract declarators, where they are not in the
2546 C99 grammar. */
2547
2548 tree
2549 set_array_declarator_type (tree decl, tree type, int abstract_p)
2550 {
2551 TREE_OPERAND (decl, 0) = type;
2552 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2553 error ("static or type qualifiers in abstract declarator");
2554 return decl;
2555 }
2556 \f
2557 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2558
2559 tree
2560 groktypename (tree typename)
2561 {
2562 tree specs, attrs;
2563
2564 if (TREE_CODE (typename) != TREE_LIST)
2565 return typename;
2566
2567 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2568
2569 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2570 NULL);
2571
2572 /* Apply attributes. */
2573 decl_attributes (&typename, attrs, 0);
2574
2575 return typename;
2576 }
2577
2578 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2579
2580 tree
2581 groktypename_in_parm_context (tree typename)
2582 {
2583 if (TREE_CODE (typename) != TREE_LIST)
2584 return typename;
2585 return grokdeclarator (TREE_VALUE (typename),
2586 TREE_PURPOSE (typename),
2587 PARM, 0, NULL);
2588 }
2589
2590 /* Decode a declarator in an ordinary declaration or data definition.
2591 This is called as soon as the type information and variable name
2592 have been parsed, before parsing the initializer if any.
2593 Here we create the ..._DECL node, fill in its type,
2594 and put it on the list of decls for the current context.
2595 The ..._DECL node is returned as the value.
2596
2597 Exception: for arrays where the length is not specified,
2598 the type is left null, to be filled in by `finish_decl'.
2599
2600 Function definitions do not come here; they go to start_function
2601 instead. However, external and forward declarations of functions
2602 do go through here. Structure field declarations are done by
2603 grokfield and not through here. */
2604
2605 tree
2606 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2607 {
2608 tree decl;
2609 tree tem;
2610
2611 /* An object declared as __attribute__((deprecated)) suppresses
2612 warnings of uses of other deprecated items. */
2613 if (lookup_attribute ("deprecated", attributes))
2614 deprecated_state = DEPRECATED_SUPPRESS;
2615
2616 decl = grokdeclarator (declarator, declspecs,
2617 NORMAL, initialized, NULL);
2618
2619 deprecated_state = DEPRECATED_NORMAL;
2620
2621 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2622 && MAIN_NAME_P (DECL_NAME (decl)))
2623 warning ("%J'%D' is usually a function", decl, decl);
2624
2625 if (initialized)
2626 /* Is it valid for this decl to have an initializer at all?
2627 If not, set INITIALIZED to zero, which will indirectly
2628 tell 'finish_decl' to ignore the initializer once it is parsed. */
2629 switch (TREE_CODE (decl))
2630 {
2631 case TYPE_DECL:
2632 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2633 initialized = 0;
2634 break;
2635
2636 case FUNCTION_DECL:
2637 error ("function '%D' is initialized like a variable", decl);
2638 initialized = 0;
2639 break;
2640
2641 case PARM_DECL:
2642 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2643 error ("parameter '%D' is initialized", decl);
2644 initialized = 0;
2645 break;
2646
2647 default:
2648 /* Don't allow initializations for incomplete types except for
2649 arrays which might be completed by the initialization. */
2650
2651 /* This can happen if the array size is an undefined macro.
2652 We already gave a warning, so we don't need another one. */
2653 if (TREE_TYPE (decl) == error_mark_node)
2654 initialized = 0;
2655 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2656 {
2657 /* A complete type is ok if size is fixed. */
2658
2659 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2660 || C_DECL_VARIABLE_SIZE (decl))
2661 {
2662 error ("variable-sized object may not be initialized");
2663 initialized = 0;
2664 }
2665 }
2666 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2667 {
2668 error ("variable '%D' has initializer but incomplete type", decl);
2669 initialized = 0;
2670 }
2671 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2672 {
2673 error ("elements of array '%D' have incomplete type", decl);
2674 initialized = 0;
2675 }
2676 }
2677
2678 if (initialized)
2679 {
2680 DECL_EXTERNAL (decl) = 0;
2681 if (current_scope == file_scope)
2682 TREE_STATIC (decl) = 1;
2683
2684 /* Tell 'pushdecl' this is an initialized decl
2685 even though we don't yet have the initializer expression.
2686 Also tell 'finish_decl' it may store the real initializer. */
2687 DECL_INITIAL (decl) = error_mark_node;
2688 }
2689
2690 /* If this is a function declaration, write a record describing it to the
2691 prototypes file (if requested). */
2692
2693 if (TREE_CODE (decl) == FUNCTION_DECL)
2694 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2695
2696 /* ANSI specifies that a tentative definition which is not merged with
2697 a non-tentative definition behaves exactly like a definition with an
2698 initializer equal to zero. (Section 3.7.2)
2699
2700 -fno-common gives strict ANSI behavior, though this tends to break
2701 a large body of code that grew up without this rule.
2702
2703 Thread-local variables are never common, since there's no entrenched
2704 body of code to break, and it allows more efficient variable references
2705 in the presence of dynamic linking. */
2706
2707 if (TREE_CODE (decl) == VAR_DECL
2708 && !initialized
2709 && TREE_PUBLIC (decl)
2710 && !DECL_THREAD_LOCAL (decl)
2711 && !flag_no_common)
2712 DECL_COMMON (decl) = 1;
2713
2714 /* Set attributes here so if duplicate decl, will have proper attributes. */
2715 decl_attributes (&decl, attributes, 0);
2716
2717 if (TREE_CODE (decl) == FUNCTION_DECL
2718 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2719 {
2720 tree ce = declarator;
2721
2722 if (TREE_CODE (ce) == INDIRECT_REF)
2723 ce = TREE_OPERAND (declarator, 0);
2724 if (TREE_CODE (ce) == CALL_EXPR)
2725 {
2726 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2727 for (; args; args = TREE_CHAIN (args))
2728 {
2729 tree type = TREE_TYPE (args);
2730 if (INTEGRAL_TYPE_P (type)
2731 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2732 DECL_ARG_TYPE (args) = integer_type_node;
2733 }
2734 }
2735 }
2736
2737 if (TREE_CODE (decl) == FUNCTION_DECL
2738 && DECL_DECLARED_INLINE_P (decl)
2739 && DECL_UNINLINABLE (decl)
2740 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2741 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2742
2743 /* Add this decl to the current scope.
2744 TEM may equal DECL or it may be a previous decl of the same name. */
2745 tem = pushdecl (decl);
2746
2747 return tem;
2748 }
2749
2750 /* Finish processing of a declaration;
2751 install its initial value.
2752 If the length of an array type is not known before,
2753 it must be determined now, from the initial value, or it is an error. */
2754
2755 void
2756 finish_decl (tree decl, tree init, tree asmspec_tree)
2757 {
2758 tree type = TREE_TYPE (decl);
2759 int was_incomplete = (DECL_SIZE (decl) == 0);
2760 const char *asmspec = 0;
2761
2762 /* If a name was specified, get the string. */
2763 if (current_scope == file_scope)
2764 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2765 if (asmspec_tree)
2766 asmspec = TREE_STRING_POINTER (asmspec_tree);
2767
2768 /* If `start_decl' didn't like having an initialization, ignore it now. */
2769 if (init != 0 && DECL_INITIAL (decl) == 0)
2770 init = 0;
2771
2772 /* Don't crash if parm is initialized. */
2773 if (TREE_CODE (decl) == PARM_DECL)
2774 init = 0;
2775
2776 if (init)
2777 store_init_value (decl, init);
2778
2779 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2780 || TREE_CODE (decl) == FUNCTION_DECL
2781 || TREE_CODE (decl) == FIELD_DECL))
2782 objc_check_decl (decl);
2783
2784 /* Deduce size of array from initialization, if not already known. */
2785 if (TREE_CODE (type) == ARRAY_TYPE
2786 && TYPE_DOMAIN (type) == 0
2787 && TREE_CODE (decl) != TYPE_DECL)
2788 {
2789 int do_default
2790 = (TREE_STATIC (decl)
2791 /* Even if pedantic, an external linkage array
2792 may have incomplete type at first. */
2793 ? pedantic && !TREE_PUBLIC (decl)
2794 : !DECL_EXTERNAL (decl));
2795 int failure
2796 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2797
2798 /* Get the completed type made by complete_array_type. */
2799 type = TREE_TYPE (decl);
2800
2801 if (failure == 1)
2802 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2803
2804 else if (failure == 2)
2805 {
2806 if (do_default)
2807 error ("%Jarray size missing in '%D'", decl, decl);
2808 /* If a `static' var's size isn't known,
2809 make it extern as well as static, so it does not get
2810 allocated.
2811 If it is not `static', then do not mark extern;
2812 finish_incomplete_decl will give it a default size
2813 and it will get allocated. */
2814 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2815 DECL_EXTERNAL (decl) = 1;
2816 }
2817
2818 /* TYPE_MAX_VALUE is always one less than the number of elements
2819 in the array, because we start counting at zero. Therefore,
2820 warn only if the value is less than zero. */
2821 else if (pedantic && TYPE_DOMAIN (type) != 0
2822 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2823 error ("%Jzero or negative size array '%D'", decl, decl);
2824
2825 layout_decl (decl, 0);
2826 }
2827
2828 if (TREE_CODE (decl) == VAR_DECL)
2829 {
2830 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2831 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2832 layout_decl (decl, 0);
2833
2834 if (DECL_SIZE (decl) == 0
2835 /* Don't give an error if we already gave one earlier. */
2836 && TREE_TYPE (decl) != error_mark_node
2837 && (TREE_STATIC (decl)
2838 ?
2839 /* A static variable with an incomplete type
2840 is an error if it is initialized.
2841 Also if it is not file scope.
2842 Otherwise, let it through, but if it is not `extern'
2843 then it may cause an error message later. */
2844 (DECL_INITIAL (decl) != 0
2845 || !DECL_FILE_SCOPE_P (decl))
2846 :
2847 /* An automatic variable with an incomplete type
2848 is an error. */
2849 !DECL_EXTERNAL (decl)))
2850 {
2851 error ("%Jstorage size of '%D' isn't known", decl, decl);
2852 TREE_TYPE (decl) = error_mark_node;
2853 }
2854
2855 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2856 && DECL_SIZE (decl) != 0)
2857 {
2858 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2859 constant_expression_warning (DECL_SIZE (decl));
2860 else
2861 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2862 }
2863
2864 if (TREE_USED (type))
2865 TREE_USED (decl) = 1;
2866 }
2867
2868 /* If this is a function and an assembler name is specified, reset DECL_RTL
2869 so we can give it its new name. Also, update built_in_decls if it
2870 was a normal built-in. */
2871 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2872 {
2873 /* ASMSPEC is given, and not the name of a register. Mark the
2874 name with a star so assemble_name won't munge it. */
2875 char *starred = alloca (strlen (asmspec) + 2);
2876 starred[0] = '*';
2877 strcpy (starred + 1, asmspec);
2878
2879 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2880 {
2881 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2882 SET_DECL_RTL (builtin, NULL_RTX);
2883 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2884 #ifdef TARGET_MEM_FUNCTIONS
2885 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2886 init_block_move_fn (starred);
2887 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2888 init_block_clear_fn (starred);
2889 #else
2890 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2891 init_block_move_fn (starred);
2892 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2893 init_block_clear_fn (starred);
2894 #endif
2895 }
2896 SET_DECL_RTL (decl, NULL_RTX);
2897 change_decl_assembler_name (decl, get_identifier (starred));
2898 }
2899
2900 /* If #pragma weak was used, mark the decl weak now. */
2901 if (current_scope == file_scope)
2902 maybe_apply_pragma_weak (decl);
2903
2904 /* Output the assembler code and/or RTL code for variables and functions,
2905 unless the type is an undefined structure or union.
2906 If not, it will get done when the type is completed. */
2907
2908 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2909 {
2910 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2911 if (c_dialect_objc ())
2912 objc_check_decl (decl);
2913
2914 if (DECL_FILE_SCOPE_P (decl))
2915 {
2916 if (DECL_INITIAL (decl) == NULL_TREE
2917 || DECL_INITIAL (decl) == error_mark_node)
2918 /* Don't output anything
2919 when a tentative file-scope definition is seen.
2920 But at end of compilation, do output code for them. */
2921 DECL_DEFER_OUTPUT (decl) = 1;
2922 rest_of_decl_compilation (decl, asmspec, true, 0);
2923 }
2924 else
2925 {
2926 /* This is a local variable. If there is an ASMSPEC, the
2927 user has requested that we handle it specially. */
2928 if (asmspec)
2929 {
2930 /* In conjunction with an ASMSPEC, the `register'
2931 keyword indicates that we should place the variable
2932 in a particular register. */
2933 if (C_DECL_REGISTER (decl))
2934 {
2935 DECL_C_HARD_REGISTER (decl) = 1;
2936 /* This cannot be done for a structure with volatile
2937 fields, on which DECL_REGISTER will have been
2938 reset. */
2939 if (!DECL_REGISTER (decl))
2940 error ("cannot put object with volatile field into register");
2941 }
2942
2943 /* If this is not a static variable, issue a warning.
2944 It doesn't make any sense to give an ASMSPEC for an
2945 ordinary, non-register local variable. Historically,
2946 GCC has accepted -- but ignored -- the ASMSPEC in
2947 this case. */
2948 if (TREE_CODE (decl) == VAR_DECL
2949 && !C_DECL_REGISTER (decl)
2950 && !TREE_STATIC (decl))
2951 warning ("%Jignoring asm-specifier for non-static local "
2952 "variable '%D'", decl, decl);
2953 else
2954 change_decl_assembler_name (decl, get_identifier (asmspec));
2955 }
2956
2957 if (TREE_CODE (decl) != FUNCTION_DECL)
2958 add_decl_stmt (decl);
2959 }
2960
2961 if (!DECL_FILE_SCOPE_P (decl))
2962 {
2963 /* Recompute the RTL of a local array now
2964 if it used to be an incomplete type. */
2965 if (was_incomplete
2966 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2967 {
2968 /* If we used it already as memory, it must stay in memory. */
2969 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2970 /* If it's still incomplete now, no init will save it. */
2971 if (DECL_SIZE (decl) == 0)
2972 DECL_INITIAL (decl) = 0;
2973 }
2974 }
2975 }
2976
2977 /* If this was marked 'used', be sure it will be output. */
2978 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2979 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2980
2981 if (TREE_CODE (decl) == TYPE_DECL)
2982 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2983
2984 /* At the end of a declaration, throw away any variable type sizes
2985 of types defined inside that declaration. There is no use
2986 computing them in the following function definition. */
2987 if (current_scope == file_scope)
2988 get_pending_sizes ();
2989
2990 /* Install a cleanup (aka destructor) if one was given. */
2991 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2992 {
2993 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2994 if (attr)
2995 {
2996 static bool eh_initialized_p;
2997
2998 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2999 tree cleanup_decl = lookup_name (cleanup_id);
3000 tree cleanup;
3001
3002 /* Build "cleanup(&decl)" for the destructor. */
3003 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3004 cleanup = build_tree_list (NULL_TREE, cleanup);
3005 cleanup = build_function_call (cleanup_decl, cleanup);
3006
3007 /* Don't warn about decl unused; the cleanup uses it. */
3008 TREE_USED (decl) = 1;
3009
3010 /* Initialize EH, if we've been told to do so. */
3011 if (flag_exceptions && !eh_initialized_p)
3012 {
3013 eh_initialized_p = true;
3014 eh_personality_libfunc
3015 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3016 ? "__gcc_personality_sj0"
3017 : "__gcc_personality_v0");
3018 using_eh_for_cleanups ();
3019 }
3020
3021 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
3022 }
3023 }
3024 }
3025
3026 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3027 and push that on the current scope. */
3028
3029 void
3030 push_parm_decl (tree parm)
3031 {
3032 tree decl;
3033
3034 /* Don't attempt to expand sizes while parsing this decl.
3035 (We can get here with i_s_e 1 somehow from Objective-C.) */
3036 int save_immediate_size_expand = immediate_size_expand;
3037 immediate_size_expand = 0;
3038
3039 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3040 TREE_PURPOSE (TREE_PURPOSE (parm)),
3041 PARM, 0, NULL);
3042 decl_attributes (&decl, TREE_VALUE (parm), 0);
3043
3044 decl = pushdecl (decl);
3045
3046 finish_decl (decl, NULL_TREE, NULL_TREE);
3047
3048 immediate_size_expand = save_immediate_size_expand;
3049 }
3050
3051 /* Mark all the parameter declarations to date as forward decls.
3052 Also diagnose use of this extension. */
3053
3054 void
3055 mark_forward_parm_decls (void)
3056 {
3057 struct c_binding *b;
3058
3059 if (pedantic && !current_scope->warned_forward_parm_decls)
3060 {
3061 pedwarn ("ISO C forbids forward parameter declarations");
3062 current_scope->warned_forward_parm_decls = true;
3063 }
3064
3065 for (b = current_scope->bindings; b; b = b->prev)
3066 if (TREE_CODE (b->decl) == PARM_DECL)
3067 TREE_ASM_WRITTEN (b->decl) = 1;
3068 }
3069 \f
3070 static GTY(()) int compound_literal_number;
3071
3072 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3073 literal, which may be an incomplete array type completed by the
3074 initializer; INIT is a CONSTRUCTOR that initializes the compound
3075 literal. */
3076
3077 tree
3078 build_compound_literal (tree type, tree init)
3079 {
3080 /* We do not use start_decl here because we have a type, not a declarator;
3081 and do not use finish_decl because the decl should be stored inside
3082 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3083 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3084 tree complit;
3085 tree stmt;
3086 DECL_EXTERNAL (decl) = 0;
3087 TREE_PUBLIC (decl) = 0;
3088 TREE_STATIC (decl) = (current_scope == file_scope);
3089 DECL_CONTEXT (decl) = current_function_decl;
3090 TREE_USED (decl) = 1;
3091 TREE_TYPE (decl) = type;
3092 TREE_READONLY (decl) = TYPE_READONLY (type);
3093 store_init_value (decl, init);
3094
3095 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3096 {
3097 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3098 if (failure)
3099 abort ();
3100 }
3101
3102 type = TREE_TYPE (decl);
3103 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3104 return error_mark_node;
3105
3106 stmt = build_stmt (DECL_STMT, decl);
3107 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3108 TREE_SIDE_EFFECTS (complit) = 1;
3109
3110 layout_decl (decl, 0);
3111
3112 if (TREE_STATIC (decl))
3113 {
3114 /* This decl needs a name for the assembler output. We also need
3115 a unique suffix to be added to the name. */
3116 char *name;
3117
3118 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3119 compound_literal_number);
3120 compound_literal_number++;
3121 DECL_NAME (decl) = get_identifier (name);
3122 DECL_DEFER_OUTPUT (decl) = 1;
3123 DECL_COMDAT (decl) = 1;
3124 DECL_ARTIFICIAL (decl) = 1;
3125 pushdecl (decl);
3126 rest_of_decl_compilation (decl, NULL, 1, 0);
3127 }
3128
3129 return complit;
3130 }
3131 \f
3132 /* Make TYPE a complete type based on INITIAL_VALUE.
3133 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3134 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3135
3136 int
3137 complete_array_type (tree type, tree initial_value, int do_default)
3138 {
3139 tree maxindex = NULL_TREE;
3140 int value = 0;
3141
3142 if (initial_value)
3143 {
3144 /* Note MAXINDEX is really the maximum index,
3145 one less than the size. */
3146 if (TREE_CODE (initial_value) == STRING_CST)
3147 {
3148 int eltsize
3149 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3150 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3151 / eltsize) - 1, 0);
3152 }
3153 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3154 {
3155 tree elts = CONSTRUCTOR_ELTS (initial_value);
3156 maxindex = build_int_2 (-1, -1);
3157 for (; elts; elts = TREE_CHAIN (elts))
3158 {
3159 if (TREE_PURPOSE (elts))
3160 maxindex = TREE_PURPOSE (elts);
3161 else
3162 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3163 maxindex, integer_one_node));
3164 }
3165 maxindex = copy_node (maxindex);
3166 }
3167 else
3168 {
3169 /* Make an error message unless that happened already. */
3170 if (initial_value != error_mark_node)
3171 value = 1;
3172
3173 /* Prevent further error messages. */
3174 maxindex = build_int_2 (0, 0);
3175 }
3176 }
3177
3178 if (!maxindex)
3179 {
3180 if (do_default)
3181 maxindex = build_int_2 (0, 0);
3182 value = 2;
3183 }
3184
3185 if (maxindex)
3186 {
3187 TYPE_DOMAIN (type) = build_index_type (maxindex);
3188 if (!TREE_TYPE (maxindex))
3189 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3190 }
3191
3192 /* Lay out the type now that we can get the real answer. */
3193
3194 layout_type (type);
3195
3196 return value;
3197 }
3198 \f
3199 /* Determine whether TYPE is a structure with a flexible array member,
3200 or a union containing such a structure (possibly recursively). */
3201
3202 static bool
3203 flexible_array_type_p (tree type)
3204 {
3205 tree x;
3206 switch (TREE_CODE (type))
3207 {
3208 case RECORD_TYPE:
3209 x = TYPE_FIELDS (type);
3210 if (x == NULL_TREE)
3211 return false;
3212 while (TREE_CHAIN (x) != NULL_TREE)
3213 x = TREE_CHAIN (x);
3214 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3215 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3216 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3217 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3218 return true;
3219 return false;
3220 case UNION_TYPE:
3221 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3222 {
3223 if (flexible_array_type_p (TREE_TYPE (x)))
3224 return true;
3225 }
3226 return false;
3227 default:
3228 return false;
3229 }
3230 }
3231 \f
3232 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3233 replacing with appropriate values if they are invalid. */
3234 static void
3235 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3236 {
3237 tree type_mv;
3238 unsigned int max_width;
3239 unsigned HOST_WIDE_INT w;
3240 const char *name = orig_name ? orig_name: _("<anonymous>");
3241
3242 /* Necessary? */
3243 STRIP_NOPS (*width);
3244
3245 /* Detect and ignore out of range field width and process valid
3246 field widths. */
3247 if (TREE_CODE (*width) != INTEGER_CST)
3248 {
3249 error ("bit-field `%s' width not an integer constant", name);
3250 *width = integer_one_node;
3251 }
3252 else
3253 {
3254 constant_expression_warning (*width);
3255 if (tree_int_cst_sgn (*width) < 0)
3256 {
3257 error ("negative width in bit-field `%s'", name);
3258 *width = integer_one_node;
3259 }
3260 else if (integer_zerop (*width) && orig_name)
3261 {
3262 error ("zero width for bit-field `%s'", name);
3263 *width = integer_one_node;
3264 }
3265 }
3266
3267 /* Detect invalid bit-field type. */
3268 if (TREE_CODE (*type) != INTEGER_TYPE
3269 && TREE_CODE (*type) != BOOLEAN_TYPE
3270 && TREE_CODE (*type) != ENUMERAL_TYPE)
3271 {
3272 error ("bit-field `%s' has invalid type", name);
3273 *type = unsigned_type_node;
3274 }
3275
3276 type_mv = TYPE_MAIN_VARIANT (*type);
3277 if (pedantic
3278 && type_mv != integer_type_node
3279 && type_mv != unsigned_type_node
3280 && type_mv != boolean_type_node)
3281 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3282
3283 if (type_mv == boolean_type_node)
3284 max_width = CHAR_TYPE_SIZE;
3285 else
3286 max_width = TYPE_PRECISION (*type);
3287
3288 if (0 < compare_tree_int (*width, max_width))
3289 {
3290 error ("width of `%s' exceeds its type", name);
3291 w = max_width;
3292 *width = build_int_2 (w, 0);
3293 }
3294 else
3295 w = tree_low_cst (*width, 1);
3296
3297 if (TREE_CODE (*type) == ENUMERAL_TYPE
3298 && (w < min_precision (TYPE_MIN_VALUE (*type), TYPE_UNSIGNED (*type))
3299 || w < min_precision (TYPE_MAX_VALUE (*type),
3300 TYPE_UNSIGNED (*type))))
3301 warning ("`%s' is narrower than values of its type", name);
3302 }
3303 \f
3304 /* Given declspecs and a declarator,
3305 determine the name and type of the object declared
3306 and construct a ..._DECL node for it.
3307 (In one case we can return a ..._TYPE node instead.
3308 For invalid input we sometimes return 0.)
3309
3310 DECLSPECS is a chain of tree_list nodes whose value fields
3311 are the storage classes and type specifiers.
3312
3313 DECL_CONTEXT says which syntactic context this declaration is in:
3314 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3315 FUNCDEF for a function definition. Like NORMAL but a few different
3316 error messages in each case. Return value may be zero meaning
3317 this definition is too screwy to try to parse.
3318 PARM for a parameter declaration (either within a function prototype
3319 or before a function body). Make a PARM_DECL, or return void_type_node.
3320 TYPENAME if for a typename (in a cast or sizeof).
3321 Don't make a DECL node; just return the ..._TYPE node.
3322 FIELD for a struct or union field; make a FIELD_DECL.
3323 INITIALIZED is 1 if the decl has an initializer.
3324 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3325 representing the width of the bit-field.
3326
3327 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3328 It may also be so in the PARM case, for a prototype where the
3329 argument type is specified but not the name.
3330
3331 This function is where the complicated C meanings of `static'
3332 and `extern' are interpreted. */
3333
3334 static tree
3335 grokdeclarator (tree declarator, tree declspecs,
3336 enum decl_context decl_context, int initialized, tree *width)
3337 {
3338 int specbits = 0;
3339 tree spec;
3340 tree type = NULL_TREE;
3341 int longlong = 0;
3342 int constp;
3343 int restrictp;
3344 int volatilep;
3345 int type_quals = TYPE_UNQUALIFIED;
3346 int inlinep;
3347 int explicit_int = 0;
3348 int explicit_char = 0;
3349 int defaulted_int = 0;
3350 tree typedef_decl = 0;
3351 const char *name, *orig_name;
3352 tree typedef_type = 0;
3353 int funcdef_flag = 0;
3354 enum tree_code innermost_code = ERROR_MARK;
3355 int size_varies = 0;
3356 tree decl_attr = NULL_TREE;
3357 tree array_ptr_quals = NULL_TREE;
3358 int array_parm_static = 0;
3359 tree returned_attrs = NULL_TREE;
3360 bool bitfield = width != NULL;
3361 tree element_type;
3362 tree arg_info = NULL_TREE;
3363
3364 if (decl_context == FUNCDEF)
3365 funcdef_flag = 1, decl_context = NORMAL;
3366
3367 /* Look inside a declarator for the name being declared
3368 and get it as a string, for an error message. */
3369 {
3370 tree decl = declarator;
3371 name = 0;
3372
3373 while (decl)
3374 switch (TREE_CODE (decl))
3375 {
3376 case ARRAY_REF:
3377 case INDIRECT_REF:
3378 case CALL_EXPR:
3379 innermost_code = TREE_CODE (decl);
3380 decl = TREE_OPERAND (decl, 0);
3381 break;
3382
3383 case TREE_LIST:
3384 decl = TREE_VALUE (decl);
3385 break;
3386
3387 case IDENTIFIER_NODE:
3388 name = IDENTIFIER_POINTER (decl);
3389 decl = 0;
3390 break;
3391
3392 default:
3393 abort ();
3394 }
3395 orig_name = name;
3396 if (name == 0)
3397 name = "type name";
3398 }
3399
3400 /* A function definition's declarator must have the form of
3401 a function declarator. */
3402
3403 if (funcdef_flag && innermost_code != CALL_EXPR)
3404 return 0;
3405
3406 /* If this looks like a function definition, make it one,
3407 even if it occurs where parms are expected.
3408 Then store_parm_decls will reject it and not use it as a parm. */
3409 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3410 decl_context = PARM;
3411
3412 /* Look through the decl specs and record which ones appear.
3413 Some typespecs are defined as built-in typenames.
3414 Others, the ones that are modifiers of other types,
3415 are represented by bits in SPECBITS: set the bits for
3416 the modifiers that appear. Storage class keywords are also in SPECBITS.
3417
3418 If there is a typedef name or a type, store the type in TYPE.
3419 This includes builtin typedefs such as `int'.
3420
3421 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3422 and did not come from a user typedef.
3423
3424 Set LONGLONG if `long' is mentioned twice. */
3425
3426 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3427 {
3428 tree id = TREE_VALUE (spec);
3429
3430 /* If the entire declaration is itself tagged as deprecated then
3431 suppress reports of deprecated items. */
3432 if (id && TREE_DEPRECATED (id))
3433 {
3434 if (deprecated_state != DEPRECATED_SUPPRESS)
3435 warn_deprecated_use (id);
3436 }
3437
3438 if (id == ridpointers[(int) RID_INT])
3439 explicit_int = 1;
3440 if (id == ridpointers[(int) RID_CHAR])
3441 explicit_char = 1;
3442
3443 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3444 {
3445 enum rid i = C_RID_CODE (id);
3446 if ((int) i <= (int) RID_LAST_MODIFIER)
3447 {
3448 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3449 {
3450 if (longlong)
3451 error ("`long long long' is too long for GCC");
3452 else
3453 {
3454 if (pedantic && !flag_isoc99 && ! in_system_header
3455 && warn_long_long)
3456 pedwarn ("ISO C90 does not support `long long'");
3457 longlong = 1;
3458 }
3459 }
3460 else if (specbits & (1 << (int) i))
3461 {
3462 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3463 {
3464 if (pedantic && !flag_isoc99)
3465 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3466 }
3467 else
3468 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3469 }
3470
3471 /* Diagnose "__thread extern". Recall that this list
3472 is in the reverse order seen in the text. */
3473 if (i == RID_THREAD
3474 && (specbits & (1 << (int) RID_EXTERN
3475 | 1 << (int) RID_STATIC)))
3476 {
3477 if (specbits & 1 << (int) RID_EXTERN)
3478 error ("`__thread' before `extern'");
3479 else
3480 error ("`__thread' before `static'");
3481 }
3482
3483 specbits |= 1 << (int) i;
3484 goto found;
3485 }
3486 }
3487 if (type)
3488 error ("two or more data types in declaration of `%s'", name);
3489 /* Actual typedefs come to us as TYPE_DECL nodes. */
3490 else if (TREE_CODE (id) == TYPE_DECL)
3491 {
3492 if (TREE_TYPE (id) == error_mark_node)
3493 ; /* Allow the type to default to int to avoid cascading errors. */
3494 else
3495 {
3496 type = TREE_TYPE (id);
3497 decl_attr = DECL_ATTRIBUTES (id);
3498 typedef_decl = id;
3499 }
3500 }
3501 /* Built-in types come as identifiers. */
3502 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3503 {
3504 tree t = lookup_name (id);
3505 if (!t || TREE_CODE (t) != TYPE_DECL)
3506 error ("`%s' fails to be a typedef or built in type",
3507 IDENTIFIER_POINTER (id));
3508 else if (TREE_TYPE (t) == error_mark_node)
3509 ;
3510 else
3511 {
3512 type = TREE_TYPE (t);
3513 typedef_decl = t;
3514 }
3515 }
3516 else if (TREE_CODE (id) != ERROR_MARK)
3517 type = id;
3518
3519 found:
3520 ;
3521 }
3522
3523 typedef_type = type;
3524 if (type)
3525 size_varies = C_TYPE_VARIABLE_SIZE (type);
3526
3527 /* No type at all: default to `int', and set DEFAULTED_INT
3528 because it was not a user-defined typedef. */
3529
3530 if (type == 0)
3531 {
3532 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3533 | (1 << (int) RID_SIGNED)
3534 | (1 << (int) RID_UNSIGNED)
3535 | (1 << (int) RID_COMPLEX))))
3536 /* Don't warn about typedef foo = bar. */
3537 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3538 && ! in_system_header)
3539 {
3540 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3541 and this is a function, or if -Wimplicit; prefer the former
3542 warning since it is more explicit. */
3543 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3544 && funcdef_flag)
3545 warn_about_return_type = 1;
3546 else if (warn_implicit_int || flag_isoc99)
3547 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3548 name);
3549 }
3550
3551 defaulted_int = 1;
3552 type = integer_type_node;
3553 }
3554
3555 /* Now process the modifiers that were specified
3556 and check for invalid combinations. */
3557
3558 /* Long double is a special combination. */
3559
3560 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3561 && TYPE_MAIN_VARIANT (type) == double_type_node)
3562 {
3563 specbits &= ~(1 << (int) RID_LONG);
3564 type = long_double_type_node;
3565 }
3566
3567 /* Check all other uses of type modifiers. */
3568
3569 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3570 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3571 {
3572 int ok = 0;
3573
3574 if ((specbits & 1 << (int) RID_LONG)
3575 && (specbits & 1 << (int) RID_SHORT))
3576 error ("both long and short specified for `%s'", name);
3577 else if (((specbits & 1 << (int) RID_LONG)
3578 || (specbits & 1 << (int) RID_SHORT))
3579 && explicit_char)
3580 error ("long or short specified with char for `%s'", name);
3581 else if (((specbits & 1 << (int) RID_LONG)
3582 || (specbits & 1 << (int) RID_SHORT))
3583 && TREE_CODE (type) == REAL_TYPE)
3584 {
3585 static int already = 0;
3586
3587 error ("long or short specified with floating type for `%s'", name);
3588 if (! already && ! pedantic)
3589 {
3590 error ("the only valid combination is `long double'");
3591 already = 1;
3592 }
3593 }
3594 else if ((specbits & 1 << (int) RID_SIGNED)
3595 && (specbits & 1 << (int) RID_UNSIGNED))
3596 error ("both signed and unsigned specified for `%s'", name);
3597 else if (TREE_CODE (type) != INTEGER_TYPE)
3598 error ("long, short, signed or unsigned invalid for `%s'", name);
3599 else
3600 {
3601 ok = 1;
3602 if (!explicit_int && !defaulted_int && !explicit_char)
3603 {
3604 error ("long, short, signed or unsigned used invalidly for `%s'",
3605 name);
3606 ok = 0;
3607 }
3608 }
3609
3610 /* Discard the type modifiers if they are invalid. */
3611 if (! ok)
3612 {
3613 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3614 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3615 longlong = 0;
3616 }
3617 }
3618
3619 if ((specbits & (1 << (int) RID_COMPLEX))
3620 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3621 {
3622 error ("complex invalid for `%s'", name);
3623 specbits &= ~(1 << (int) RID_COMPLEX);
3624 }
3625
3626 /* Decide whether an integer type is signed or not.
3627 Optionally treat bit-fields as signed by default. */
3628 if (specbits & 1 << (int) RID_UNSIGNED
3629 || (bitfield && ! flag_signed_bitfields
3630 && (explicit_int || defaulted_int || explicit_char
3631 /* A typedef for plain `int' without `signed'
3632 can be controlled just like plain `int'. */
3633 || ! (typedef_decl != 0
3634 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3635 && TREE_CODE (type) != ENUMERAL_TYPE
3636 && !(specbits & 1 << (int) RID_SIGNED)))
3637 {
3638 if (longlong)
3639 type = long_long_unsigned_type_node;
3640 else if (specbits & 1 << (int) RID_LONG)
3641 type = long_unsigned_type_node;
3642 else if (specbits & 1 << (int) RID_SHORT)
3643 type = short_unsigned_type_node;
3644 else if (type == char_type_node)
3645 type = unsigned_char_type_node;
3646 else if (typedef_decl)
3647 type = c_common_unsigned_type (type);
3648 else
3649 type = unsigned_type_node;
3650 }
3651 else if ((specbits & 1 << (int) RID_SIGNED)
3652 && type == char_type_node)
3653 type = signed_char_type_node;
3654 else if (longlong)
3655 type = long_long_integer_type_node;
3656 else if (specbits & 1 << (int) RID_LONG)
3657 type = long_integer_type_node;
3658 else if (specbits & 1 << (int) RID_SHORT)
3659 type = short_integer_type_node;
3660
3661 if (specbits & 1 << (int) RID_COMPLEX)
3662 {
3663 if (pedantic && !flag_isoc99)
3664 pedwarn ("ISO C90 does not support complex types");
3665 /* If we just have "complex", it is equivalent to
3666 "complex double", but if any modifiers at all are specified it is
3667 the complex form of TYPE. E.g, "complex short" is
3668 "complex short int". */
3669
3670 if (defaulted_int && ! longlong
3671 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3672 | (1 << (int) RID_SIGNED)
3673 | (1 << (int) RID_UNSIGNED))))
3674 {
3675 if (pedantic)
3676 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3677 type = complex_double_type_node;
3678 }
3679 else if (type == integer_type_node)
3680 {
3681 if (pedantic)
3682 pedwarn ("ISO C does not support complex integer types");
3683 type = complex_integer_type_node;
3684 }
3685 else if (type == float_type_node)
3686 type = complex_float_type_node;
3687 else if (type == double_type_node)
3688 type = complex_double_type_node;
3689 else if (type == long_double_type_node)
3690 type = complex_long_double_type_node;
3691 else
3692 {
3693 if (pedantic)
3694 pedwarn ("ISO C does not support complex integer types");
3695 type = build_complex_type (type);
3696 }
3697 }
3698
3699 /* Check the type and width of a bit-field. */
3700 if (bitfield)
3701 check_bitfield_type_and_width (&type, width, orig_name);
3702
3703 /* Figure out the type qualifiers for the declaration. There are
3704 two ways a declaration can become qualified. One is something
3705 like `const int i' where the `const' is explicit. Another is
3706 something like `typedef const int CI; CI i' where the type of the
3707 declaration contains the `const'. A third possibility is that
3708 there is a type qualifier on the element type of a typedefed
3709 array type, in which case we should extract that qualifier so
3710 that c_apply_type_quals_to_decls receives the full list of
3711 qualifiers to work with (C90 is not entirely clear about whether
3712 duplicate qualifiers should be diagnosed in this case, but it
3713 seems most appropriate to do so). */
3714 element_type = strip_array_types (type);
3715 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3716 restrictp
3717 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3718 volatilep
3719 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3720 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3721 if (pedantic && !flag_isoc99)
3722 {
3723 if (constp > 1)
3724 pedwarn ("duplicate `const'");
3725 if (restrictp > 1)
3726 pedwarn ("duplicate `restrict'");
3727 if (volatilep > 1)
3728 pedwarn ("duplicate `volatile'");
3729 }
3730 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3731 type = TYPE_MAIN_VARIANT (type);
3732 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3733 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3734 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3735
3736 /* Warn if two storage classes are given. Default to `auto'. */
3737
3738 {
3739 int nclasses = 0;
3740
3741 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3742 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3743 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3744 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3745 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3746
3747 /* "static __thread" and "extern __thread" are allowed. */
3748 if ((specbits & (1 << (int) RID_THREAD
3749 | 1 << (int) RID_STATIC
3750 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3751 nclasses++;
3752
3753 /* Warn about storage classes that are invalid for certain
3754 kinds of declarations (parameters, typenames, etc.). */
3755
3756 if (nclasses > 1)
3757 error ("multiple storage classes in declaration of `%s'", name);
3758 else if (funcdef_flag
3759 && (specbits
3760 & ((1 << (int) RID_REGISTER)
3761 | (1 << (int) RID_AUTO)
3762 | (1 << (int) RID_TYPEDEF)
3763 | (1 << (int) RID_THREAD))))
3764 {
3765 if (specbits & 1 << (int) RID_AUTO
3766 && (pedantic || current_scope == file_scope))
3767 pedwarn ("function definition declared `auto'");
3768 if (specbits & 1 << (int) RID_REGISTER)
3769 error ("function definition declared `register'");
3770 if (specbits & 1 << (int) RID_TYPEDEF)
3771 error ("function definition declared `typedef'");
3772 if (specbits & 1 << (int) RID_THREAD)
3773 error ("function definition declared `__thread'");
3774 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3775 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3776 }
3777 else if (decl_context != NORMAL && nclasses > 0)
3778 {
3779 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3780 ;
3781 else
3782 {
3783 switch (decl_context)
3784 {
3785 case FIELD:
3786 error ("storage class specified for structure field `%s'",
3787 name);
3788 break;
3789 case PARM:
3790 error ("storage class specified for parameter `%s'", name);
3791 break;
3792 default:
3793 error ("storage class specified for typename");
3794 break;
3795 }
3796 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3797 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3798 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3799 }
3800 }
3801 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3802 {
3803 /* `extern' with initialization is invalid if not at file scope. */
3804 if (current_scope == file_scope)
3805 warning ("`%s' initialized and declared `extern'", name);
3806 else
3807 error ("`%s' has both `extern' and initializer", name);
3808 }
3809 else if (current_scope == file_scope)
3810 {
3811 if (specbits & 1 << (int) RID_AUTO)
3812 error ("file-scope declaration of `%s' specifies `auto'", name);
3813 }
3814 else
3815 {
3816 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3817 error ("nested function `%s' declared `extern'", name);
3818 else if ((specbits & (1 << (int) RID_THREAD
3819 | 1 << (int) RID_EXTERN
3820 | 1 << (int) RID_STATIC))
3821 == (1 << (int) RID_THREAD))
3822 {
3823 error ("function-scope `%s' implicitly auto and declared `__thread'",
3824 name);
3825 specbits &= ~(1 << (int) RID_THREAD);
3826 }
3827 }
3828 }
3829
3830 /* Now figure out the structure of the declarator proper.
3831 Descend through it, creating more complex types, until we reach
3832 the declared identifier (or NULL_TREE, in an absolute declarator). */
3833
3834 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3835 {
3836 if (type == error_mark_node)
3837 {
3838 declarator = TREE_OPERAND (declarator, 0);
3839 continue;
3840 }
3841
3842 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3843 an INDIRECT_REF (for *...),
3844 a CALL_EXPR (for ...(...)),
3845 a TREE_LIST (for nested attributes),
3846 an identifier (for the name being declared)
3847 or a null pointer (for the place in an absolute declarator
3848 where the name was omitted).
3849 For the last two cases, we have just exited the loop.
3850
3851 At this point, TYPE is the type of elements of an array,
3852 or for a function to return, or for a pointer to point to.
3853 After this sequence of ifs, TYPE is the type of the
3854 array or function or pointer, and DECLARATOR has had its
3855 outermost layer removed. */
3856
3857 if (array_ptr_quals != NULL_TREE || array_parm_static)
3858 {
3859 /* Only the innermost declarator (making a parameter be of
3860 array type which is converted to pointer type)
3861 may have static or type qualifiers. */
3862 error ("static or type qualifiers in non-parameter array declarator");
3863 array_ptr_quals = NULL_TREE;
3864 array_parm_static = 0;
3865 }
3866
3867 if (TREE_CODE (declarator) == TREE_LIST)
3868 {
3869 /* We encode a declarator with embedded attributes using
3870 a TREE_LIST. */
3871 tree attrs = TREE_PURPOSE (declarator);
3872 tree inner_decl;
3873 int attr_flags = 0;
3874 declarator = TREE_VALUE (declarator);
3875 inner_decl = declarator;
3876 while (inner_decl != NULL_TREE
3877 && TREE_CODE (inner_decl) == TREE_LIST)
3878 inner_decl = TREE_VALUE (inner_decl);
3879 if (inner_decl == NULL_TREE
3880 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3881 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3882 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3883 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3884 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3885 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3886 returned_attrs = decl_attributes (&type,
3887 chainon (returned_attrs, attrs),
3888 attr_flags);
3889 }
3890 else if (TREE_CODE (declarator) == ARRAY_REF)
3891 {
3892 tree itype = NULL_TREE;
3893 tree size = TREE_OPERAND (declarator, 1);
3894 /* The index is a signed object `sizetype' bits wide. */
3895 tree index_type = c_common_signed_type (sizetype);
3896
3897 array_ptr_quals = TREE_TYPE (declarator);
3898 array_parm_static = TREE_STATIC (declarator);
3899
3900 declarator = TREE_OPERAND (declarator, 0);
3901
3902 /* Check for some types that there cannot be arrays of. */
3903
3904 if (VOID_TYPE_P (type))
3905 {
3906 error ("declaration of `%s' as array of voids", name);
3907 type = error_mark_node;
3908 }
3909
3910 if (TREE_CODE (type) == FUNCTION_TYPE)
3911 {
3912 error ("declaration of `%s' as array of functions", name);
3913 type = error_mark_node;
3914 }
3915
3916 if (pedantic && flexible_array_type_p (type))
3917 pedwarn ("invalid use of structure with flexible array member");
3918
3919 if (size == error_mark_node)
3920 type = error_mark_node;
3921
3922 if (type == error_mark_node)
3923 continue;
3924
3925 /* If size was specified, set ITYPE to a range-type for that size.
3926 Otherwise, ITYPE remains null. finish_decl may figure it out
3927 from an initial value. */
3928
3929 if (size)
3930 {
3931 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3932 STRIP_TYPE_NOPS (size);
3933
3934 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3935 {
3936 error ("size of array `%s' has non-integer type", name);
3937 size = integer_one_node;
3938 }
3939
3940 if (pedantic && integer_zerop (size))
3941 pedwarn ("ISO C forbids zero-size array `%s'", name);
3942
3943 if (TREE_CODE (size) == INTEGER_CST)
3944 {
3945 constant_expression_warning (size);
3946 if (tree_int_cst_sgn (size) < 0)
3947 {
3948 error ("size of array `%s' is negative", name);
3949 size = integer_one_node;
3950 }
3951 }
3952 else
3953 {
3954 /* Make sure the array size remains visibly nonconstant
3955 even if it is (eg) a const variable with known value. */
3956 size_varies = 1;
3957
3958 if (!flag_isoc99 && pedantic)
3959 {
3960 if (TREE_CONSTANT (size))
3961 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3962 name);
3963 else
3964 pedwarn ("ISO C90 forbids variable-size array `%s'",
3965 name);
3966 }
3967 }
3968
3969 if (integer_zerop (size))
3970 {
3971 /* A zero-length array cannot be represented with an
3972 unsigned index type, which is what we'll get with
3973 build_index_type. Create an open-ended range instead. */
3974 itype = build_range_type (sizetype, size, NULL_TREE);
3975 }
3976 else
3977 {
3978 /* Compute the maximum valid index, that is, size - 1.
3979 Do the calculation in index_type, so that if it is
3980 a variable the computations will be done in the
3981 proper mode. */
3982 itype = fold (build (MINUS_EXPR, index_type,
3983 convert (index_type, size),
3984 convert (index_type, size_one_node)));
3985
3986 /* If that overflowed, the array is too big.
3987 ??? While a size of INT_MAX+1 technically shouldn't
3988 cause an overflow (because we subtract 1), the overflow
3989 is recorded during the conversion to index_type, before
3990 the subtraction. Handling this case seems like an
3991 unnecessary complication. */
3992 if (TREE_OVERFLOW (itype))
3993 {
3994 error ("size of array `%s' is too large", name);
3995 type = error_mark_node;
3996 continue;
3997 }
3998
3999 if (size_varies)
4000 {
4001 /* We must be able to distinguish the
4002 SAVE_EXPR_CONTEXT for the variably-sized type
4003 so that we can set it correctly in
4004 set_save_expr_context. The convention is
4005 that all SAVE_EXPRs that need to be reset
4006 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
4007 tree cfd = current_function_decl;
4008 if (decl_context == PARM)
4009 current_function_decl = NULL_TREE;
4010 itype = variable_size (itype);
4011 if (decl_context == PARM)
4012 current_function_decl = cfd;
4013 }
4014 itype = build_index_type (itype);
4015 }
4016 }
4017 else if (decl_context == FIELD)
4018 {
4019 if (pedantic && !flag_isoc99 && !in_system_header)
4020 pedwarn ("ISO C90 does not support flexible array members");
4021
4022 /* ISO C99 Flexible array members are effectively identical
4023 to GCC's zero-length array extension. */
4024 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4025 }
4026
4027 /* If pedantic, complain about arrays of incomplete types. */
4028
4029 if (pedantic && !COMPLETE_TYPE_P (type))
4030 pedwarn ("array type has incomplete element type");
4031
4032 /* Build the array type itself, then merge any constancy or
4033 volatility into the target type. We must do it in this order
4034 to ensure that the TYPE_MAIN_VARIANT field of the array type
4035 is set correctly. */
4036
4037 type = build_array_type (type, itype);
4038 if (type_quals)
4039 type = c_build_qualified_type (type, type_quals);
4040
4041 if (size_varies)
4042 C_TYPE_VARIABLE_SIZE (type) = 1;
4043
4044 /* The GCC extension for zero-length arrays differs from
4045 ISO flexible array members in that sizeof yields zero. */
4046 if (size && integer_zerop (size))
4047 {
4048 layout_type (type);
4049 TYPE_SIZE (type) = bitsize_zero_node;
4050 TYPE_SIZE_UNIT (type) = size_zero_node;
4051 }
4052 if (decl_context != PARM
4053 && (array_ptr_quals != NULL_TREE || array_parm_static))
4054 {
4055 error ("static or type qualifiers in non-parameter array declarator");
4056 array_ptr_quals = NULL_TREE;
4057 array_parm_static = 0;
4058 }
4059 }
4060 else if (TREE_CODE (declarator) == CALL_EXPR)
4061 {
4062 /* Say it's a definition only for the CALL_EXPR closest to
4063 the identifier. */
4064 bool really_funcdef = (funcdef_flag
4065 && (TREE_CODE (TREE_OPERAND (declarator, 0))
4066 == IDENTIFIER_NODE));
4067 tree arg_types;
4068
4069 /* Declaring a function type.
4070 Make sure we have a valid type for the function to return. */
4071 if (type == error_mark_node)
4072 continue;
4073
4074 size_varies = 0;
4075
4076 /* Warn about some types functions can't return. */
4077
4078 if (TREE_CODE (type) == FUNCTION_TYPE)
4079 {
4080 error ("`%s' declared as function returning a function", name);
4081 type = integer_type_node;
4082 }
4083 if (TREE_CODE (type) == ARRAY_TYPE)
4084 {
4085 error ("`%s' declared as function returning an array", name);
4086 type = integer_type_node;
4087 }
4088
4089 /* Construct the function type and go to the next
4090 inner layer of declarator. */
4091 arg_info = TREE_OPERAND (declarator, 1);
4092 arg_types = grokparms (arg_info, really_funcdef);
4093
4094 /* Type qualifiers before the return type of the function
4095 qualify the return type, not the function type. */
4096 if (type_quals)
4097 {
4098 /* Type qualifiers on a function return type are normally
4099 permitted by the standard but have no effect, so give a
4100 warning at -Wextra. Qualifiers on a void return type have
4101 meaning as a GNU extension, and are banned on function
4102 definitions in ISO C. FIXME: strictly we shouldn't
4103 pedwarn for qualified void return types except on function
4104 definitions, but not doing so could lead to the undesirable
4105 state of a "volatile void" function return type not being
4106 warned about, and a use of the function being compiled
4107 with GNU semantics, with no diagnostics under -pedantic. */
4108 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4109 pedwarn ("ISO C forbids qualified void function return type");
4110 else if (extra_warnings
4111 && !(VOID_TYPE_P (type)
4112 && type_quals == TYPE_QUAL_VOLATILE))
4113 warning ("type qualifiers ignored on function return type");
4114
4115 type = c_build_qualified_type (type, type_quals);
4116 }
4117 type_quals = TYPE_UNQUALIFIED;
4118
4119 type = build_function_type (type, arg_types);
4120 declarator = TREE_OPERAND (declarator, 0);
4121
4122 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4123 the formal parameter list of this FUNCTION_TYPE to point to
4124 the FUNCTION_TYPE node itself. */
4125
4126 {
4127 tree link;
4128
4129 for (link = ARG_INFO_TAGS (arg_info);
4130 link;
4131 link = TREE_CHAIN (link))
4132 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4133 }
4134 }
4135 else if (TREE_CODE (declarator) == INDIRECT_REF)
4136 {
4137 /* Merge any constancy or volatility into the target type
4138 for the pointer. */
4139
4140 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4141 && type_quals)
4142 pedwarn ("ISO C forbids qualified function types");
4143 if (type_quals)
4144 type = c_build_qualified_type (type, type_quals);
4145 type_quals = TYPE_UNQUALIFIED;
4146 size_varies = 0;
4147
4148 type = build_pointer_type (type);
4149
4150 /* Process a list of type modifier keywords
4151 (such as const or volatile) that were given inside the `*'. */
4152
4153 if (TREE_TYPE (declarator))
4154 {
4155 tree typemodlist;
4156 int erred = 0;
4157
4158 constp = 0;
4159 volatilep = 0;
4160 restrictp = 0;
4161 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4162 typemodlist = TREE_CHAIN (typemodlist))
4163 {
4164 tree qualifier = TREE_VALUE (typemodlist);
4165
4166 if (C_IS_RESERVED_WORD (qualifier))
4167 {
4168 if (C_RID_CODE (qualifier) == RID_CONST)
4169 constp++;
4170 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4171 volatilep++;
4172 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4173 restrictp++;
4174 else
4175 erred++;
4176 }
4177 else
4178 erred++;
4179 }
4180
4181 if (erred)
4182 error ("invalid type modifier within pointer declarator");
4183 if (pedantic && !flag_isoc99)
4184 {
4185 if (constp > 1)
4186 pedwarn ("duplicate `const'");
4187 if (volatilep > 1)
4188 pedwarn ("duplicate `volatile'");
4189 if (restrictp > 1)
4190 pedwarn ("duplicate `restrict'");
4191 }
4192
4193 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4194 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4195 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4196 }
4197
4198 declarator = TREE_OPERAND (declarator, 0);
4199 }
4200 else
4201 abort ();
4202
4203 }
4204
4205 /* Now TYPE has the actual type. */
4206
4207 /* Did array size calculations overflow? */
4208
4209 if (TREE_CODE (type) == ARRAY_TYPE
4210 && COMPLETE_TYPE_P (type)
4211 && TREE_OVERFLOW (TYPE_SIZE (type)))
4212 {
4213 error ("size of array `%s' is too large", name);
4214 /* If we proceed with the array type as it is, we'll eventually
4215 crash in tree_low_cst(). */
4216 type = error_mark_node;
4217 }
4218
4219 /* If this is declaring a typedef name, return a TYPE_DECL. */
4220
4221 if (specbits & (1 << (int) RID_TYPEDEF))
4222 {
4223 tree decl;
4224 /* Note that the grammar rejects storage classes
4225 in typenames, fields or parameters */
4226 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4227 && type_quals)
4228 pedwarn ("ISO C forbids qualified function types");
4229 if (type_quals)
4230 type = c_build_qualified_type (type, type_quals);
4231 decl = build_decl (TYPE_DECL, declarator, type);
4232 if ((specbits & (1 << (int) RID_SIGNED))
4233 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4234 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4235 decl_attributes (&decl, returned_attrs, 0);
4236 return decl;
4237 }
4238
4239 /* Detect the case of an array type of unspecified size
4240 which came, as such, direct from a typedef name.
4241 We must copy the type, so that each identifier gets
4242 a distinct type, so that each identifier's size can be
4243 controlled separately by its own initializer. */
4244
4245 if (type != 0 && typedef_type != 0
4246 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4247 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4248 {
4249 type = build_array_type (TREE_TYPE (type), 0);
4250 if (size_varies)
4251 C_TYPE_VARIABLE_SIZE (type) = 1;
4252 }
4253
4254 /* If this is a type name (such as, in a cast or sizeof),
4255 compute the type and return it now. */
4256
4257 if (decl_context == TYPENAME)
4258 {
4259 /* Note that the grammar rejects storage classes
4260 in typenames, fields or parameters */
4261 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4262 && type_quals)
4263 pedwarn ("ISO C forbids const or volatile function types");
4264 if (type_quals)
4265 type = c_build_qualified_type (type, type_quals);
4266 decl_attributes (&type, returned_attrs, 0);
4267 return type;
4268 }
4269
4270 /* Aside from typedefs and type names (handle above),
4271 `void' at top level (not within pointer)
4272 is allowed only in public variables.
4273 We don't complain about parms either, but that is because
4274 a better error message can be made later. */
4275
4276 if (VOID_TYPE_P (type) && decl_context != PARM
4277 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4278 && ((specbits & (1 << (int) RID_EXTERN))
4279 || (current_scope == file_scope
4280 && !(specbits
4281 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4282 {
4283 error ("variable or field `%s' declared void", name);
4284 type = integer_type_node;
4285 }
4286
4287 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4288 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4289
4290 {
4291 tree decl;
4292
4293 if (decl_context == PARM)
4294 {
4295 tree type_as_written;
4296 tree promoted_type;
4297
4298 /* A parameter declared as an array of T is really a pointer to T.
4299 One declared as a function is really a pointer to a function. */
4300
4301 if (TREE_CODE (type) == ARRAY_TYPE)
4302 {
4303 /* Transfer const-ness of array into that of type pointed to. */
4304 type = TREE_TYPE (type);
4305 if (type_quals)
4306 type = c_build_qualified_type (type, type_quals);
4307 type = build_pointer_type (type);
4308 type_quals = TYPE_UNQUALIFIED;
4309 if (array_ptr_quals)
4310 {
4311 tree new_ptr_quals, new_ptr_attrs;
4312 int erred = 0;
4313 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4314 /* We don't yet implement attributes in this context. */
4315 if (new_ptr_attrs != NULL_TREE)
4316 warning ("attributes in parameter array declarator ignored");
4317
4318 constp = 0;
4319 volatilep = 0;
4320 restrictp = 0;
4321 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4322 {
4323 tree qualifier = TREE_VALUE (new_ptr_quals);
4324
4325 if (C_IS_RESERVED_WORD (qualifier))
4326 {
4327 if (C_RID_CODE (qualifier) == RID_CONST)
4328 constp++;
4329 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4330 volatilep++;
4331 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4332 restrictp++;
4333 else
4334 erred++;
4335 }
4336 else
4337 erred++;
4338 }
4339
4340 if (erred)
4341 error ("invalid type modifier within array declarator");
4342
4343 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4344 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4345 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4346 }
4347 size_varies = 0;
4348 }
4349 else if (TREE_CODE (type) == FUNCTION_TYPE)
4350 {
4351 if (pedantic && type_quals)
4352 pedwarn ("ISO C forbids qualified function types");
4353 if (type_quals)
4354 type = c_build_qualified_type (type, type_quals);
4355 type = build_pointer_type (type);
4356 type_quals = TYPE_UNQUALIFIED;
4357 }
4358 else if (type_quals)
4359 type = c_build_qualified_type (type, type_quals);
4360
4361 type_as_written = type;
4362
4363 decl = build_decl (PARM_DECL, declarator, type);
4364 if (size_varies)
4365 C_DECL_VARIABLE_SIZE (decl) = 1;
4366
4367 /* Compute the type actually passed in the parmlist,
4368 for the case where there is no prototype.
4369 (For example, shorts and chars are passed as ints.)
4370 When there is a prototype, this is overridden later. */
4371
4372 if (type == error_mark_node)
4373 promoted_type = type;
4374 else
4375 promoted_type = c_type_promotes_to (type);
4376
4377 DECL_ARG_TYPE (decl) = promoted_type;
4378 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4379 }
4380 else if (decl_context == FIELD)
4381 {
4382 /* Structure field. It may not be a function. */
4383
4384 if (TREE_CODE (type) == FUNCTION_TYPE)
4385 {
4386 error ("field `%s' declared as a function", name);
4387 type = build_pointer_type (type);
4388 }
4389 else if (TREE_CODE (type) != ERROR_MARK
4390 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4391 {
4392 error ("field `%s' has incomplete type", name);
4393 type = error_mark_node;
4394 }
4395 /* Move type qualifiers down to element of an array. */
4396 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4397 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4398 type_quals),
4399 TYPE_DOMAIN (type));
4400 decl = build_decl (FIELD_DECL, declarator, type);
4401 DECL_NONADDRESSABLE_P (decl) = bitfield;
4402
4403 if (size_varies)
4404 C_DECL_VARIABLE_SIZE (decl) = 1;
4405 }
4406 else if (TREE_CODE (type) == FUNCTION_TYPE)
4407 {
4408 /* Every function declaration is "external"
4409 except for those which are inside a function body
4410 in which `auto' is used.
4411 That is a case not specified by ANSI C,
4412 and we use it for forward declarations for nested functions. */
4413 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4414 || current_scope == file_scope);
4415
4416 if (specbits & (1 << (int) RID_AUTO)
4417 && (pedantic || current_scope == file_scope))
4418 pedwarn ("invalid storage class for function `%s'", name);
4419 if (specbits & (1 << (int) RID_REGISTER))
4420 error ("invalid storage class for function `%s'", name);
4421 if (specbits & (1 << (int) RID_THREAD))
4422 error ("invalid storage class for function `%s'", name);
4423 /* Function declaration not at file scope.
4424 Storage classes other than `extern' are not allowed
4425 and `extern' makes no difference. */
4426 if (current_scope != file_scope
4427 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4428 && pedantic)
4429 pedwarn ("invalid storage class for function `%s'", name);
4430
4431 decl = build_decl (FUNCTION_DECL, declarator, type);
4432 decl = build_decl_attribute_variant (decl, decl_attr);
4433
4434 DECL_LANG_SPECIFIC (decl)
4435 = ggc_alloc_cleared (sizeof (struct lang_decl));
4436
4437 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4438 pedwarn ("ISO C forbids qualified function types");
4439
4440 /* GNU C interprets a `volatile void' return type to indicate
4441 that the function does not return. */
4442 if ((type_quals & TYPE_QUAL_VOLATILE)
4443 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4444 warning ("`noreturn' function returns non-void value");
4445
4446 if (extern_ref)
4447 DECL_EXTERNAL (decl) = 1;
4448 /* Record absence of global scope for `static' or `auto'. */
4449 TREE_PUBLIC (decl)
4450 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4451
4452 /* For a function definition, record the argument information
4453 block in DECL_ARGUMENTS where store_parm_decls will look
4454 for it. */
4455 if (funcdef_flag)
4456 DECL_ARGUMENTS (decl) = arg_info;
4457
4458 if (defaulted_int)
4459 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4460
4461 /* Record presence of `inline', if it is reasonable. */
4462 if (MAIN_NAME_P (declarator))
4463 {
4464 if (inlinep)
4465 warning ("cannot inline function `main'");
4466 }
4467 else if (inlinep)
4468 {
4469 /* Record that the function is declared `inline'. */
4470 DECL_DECLARED_INLINE_P (decl) = 1;
4471
4472 /* Do not mark bare declarations as DECL_INLINE. Doing so
4473 in the presence of multiple declarations can result in
4474 the abstract origin pointing between the declarations,
4475 which will confuse dwarf2out. */
4476 if (initialized)
4477 {
4478 DECL_INLINE (decl) = 1;
4479 if (specbits & (1 << (int) RID_EXTERN))
4480 current_extern_inline = 1;
4481 }
4482 }
4483 /* If -finline-functions, assume it can be inlined. This does
4484 two things: let the function be deferred until it is actually
4485 needed, and let dwarf2 know that the function is inlinable. */
4486 else if (flag_inline_trees == 2 && initialized)
4487 DECL_INLINE (decl) = 1;
4488 }
4489 else
4490 {
4491 /* It's a variable. */
4492 /* An uninitialized decl with `extern' is a reference. */
4493 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4494
4495 /* Move type qualifiers down to element of an array. */
4496 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4497 {
4498 int saved_align = TYPE_ALIGN(type);
4499 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4500 type_quals),
4501 TYPE_DOMAIN (type));
4502 TYPE_ALIGN (type) = saved_align;
4503 }
4504 else if (type_quals)
4505 type = c_build_qualified_type (type, type_quals);
4506
4507 /* C99 6.2.2p7: It is invalid (compile-time undefined
4508 behavior) to create an 'extern' declaration for a
4509 variable if there is a global declaration that is
4510 'static' and the global declaration is not visible.
4511 (If the static declaration _is_ currently visible,
4512 the 'extern' declaration is taken to refer to that decl.) */
4513 if (extern_ref && current_scope != file_scope)
4514 {
4515 tree global_decl = identifier_global_value (declarator);
4516 tree visible_decl = lookup_name (declarator);
4517
4518 if (global_decl
4519 && global_decl != visible_decl
4520 && TREE_CODE (global_decl) == VAR_DECL
4521 && !TREE_PUBLIC (global_decl))
4522 error ("variable previously declared 'static' redeclared "
4523 "'extern'");
4524 }
4525
4526 decl = build_decl (VAR_DECL, declarator, type);
4527 if (size_varies)
4528 C_DECL_VARIABLE_SIZE (decl) = 1;
4529
4530 if (inlinep)
4531 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4532
4533 DECL_EXTERNAL (decl) = extern_ref;
4534
4535 /* At file scope, the presence of a `static' or `register' storage
4536 class specifier, or the absence of all storage class specifiers
4537 makes this declaration a definition (perhaps tentative). Also,
4538 the absence of both `static' and `register' makes it public. */
4539 if (current_scope == file_scope)
4540 {
4541 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4542 | (1 << (int) RID_REGISTER)));
4543 TREE_STATIC (decl) = !extern_ref;
4544 }
4545 /* Not at file scope, only `static' makes a static definition. */
4546 else
4547 {
4548 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4549 TREE_PUBLIC (decl) = extern_ref;
4550 }
4551
4552 if (specbits & 1 << (int) RID_THREAD)
4553 {
4554 if (targetm.have_tls)
4555 DECL_THREAD_LOCAL (decl) = 1;
4556 else
4557 /* A mere warning is sure to result in improper semantics
4558 at runtime. Don't bother to allow this to compile. */
4559 error ("thread-local storage not supported for this target");
4560 }
4561 }
4562
4563 /* Record `register' declaration for warnings on &
4564 and in case doing stupid register allocation. */
4565
4566 if (specbits & (1 << (int) RID_REGISTER))
4567 {
4568 C_DECL_REGISTER (decl) = 1;
4569 DECL_REGISTER (decl) = 1;
4570 }
4571
4572 /* Record constancy and volatility. */
4573 c_apply_type_quals_to_decl (type_quals, decl);
4574
4575 /* If a type has volatile components, it should be stored in memory.
4576 Otherwise, the fact that those components are volatile
4577 will be ignored, and would even crash the compiler. */
4578 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4579 {
4580 /* It is not an error for a structure with volatile fields to
4581 be declared register, but reset DECL_REGISTER since it
4582 cannot actually go in a register. */
4583 int was_reg = C_DECL_REGISTER (decl);
4584 C_DECL_REGISTER (decl) = 0;
4585 DECL_REGISTER (decl) = 0;
4586 c_mark_addressable (decl);
4587 C_DECL_REGISTER (decl) = was_reg;
4588 }
4589
4590 #ifdef ENABLE_CHECKING
4591 /* This is the earliest point at which we might know the assembler
4592 name of a variable. Thus, if it's known before this, die horribly. */
4593 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4594 abort ();
4595 #endif
4596
4597 decl_attributes (&decl, returned_attrs, 0);
4598
4599 return decl;
4600 }
4601 }
4602 \f
4603 /* Decode the parameter-list info for a function type or function definition.
4604 The argument is the value returned by `get_parm_info' (or made in parse.y
4605 if there is an identifier list instead of a parameter decl list).
4606 These two functions are separate because when a function returns
4607 or receives functions then each is called multiple times but the order
4608 of calls is different. The last call to `grokparms' is always the one
4609 that contains the formal parameter names of a function definition.
4610
4611 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4612
4613 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4614 a mere declaration. A nonempty identifier-list gets an error message
4615 when FUNCDEF_FLAG is zero. */
4616
4617 static tree
4618 grokparms (tree arg_info, int funcdef_flag)
4619 {
4620 tree arg_types = ARG_INFO_TYPES (arg_info);
4621
4622 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4623 && !in_system_header)
4624 warning ("function declaration isn't a prototype");
4625
4626 if (arg_types == error_mark_node)
4627 return 0; /* don't set TYPE_ARG_TYPES in this case */
4628
4629 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4630 {
4631 if (! funcdef_flag)
4632 pedwarn ("parameter names (without types) in function declaration");
4633
4634 ARG_INFO_PARMS (arg_info) = ARG_INFO_TYPES (arg_info);
4635 ARG_INFO_TYPES (arg_info) = 0;
4636 return 0;
4637 }
4638 else
4639 {
4640 tree parm, type, typelt;
4641 unsigned int parmno;
4642
4643 /* If the arg types are incomplete in a declaration, they must
4644 include undefined tags. These tags can never be defined in
4645 the scope of the declaration, so the types can never be
4646 completed, and no call can be compiled successfully. */
4647
4648 for (parm = ARG_INFO_PARMS (arg_info), typelt = arg_types, parmno = 1;
4649 parm;
4650 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4651 {
4652 type = TREE_VALUE (typelt);
4653 if (type == error_mark_node)
4654 continue;
4655
4656 if (!COMPLETE_TYPE_P (type))
4657 {
4658 if (funcdef_flag)
4659 {
4660 if (DECL_NAME (parm))
4661 error ("%Jparameter %u ('%D') has incomplete type",
4662 parm, parmno, parm);
4663 else
4664 error ("%Jparameter %u has incomplete type",
4665 parm, parmno);
4666
4667 TREE_VALUE (typelt) = error_mark_node;
4668 TREE_TYPE (parm) = error_mark_node;
4669 }
4670 else
4671 {
4672 if (DECL_NAME (parm))
4673 warning ("%Jparameter %u ('%D') has incomplete type",
4674 parm, parmno, parm);
4675 else
4676 warning ("%Jparameter %u has incomplete type",
4677 parm, parmno);
4678 }
4679 }
4680 }
4681 return arg_types;
4682 }
4683 }
4684
4685 /* Take apart the current scope and return a tree_list node with info
4686 on a parameter list just parsed. This tree_list node should be
4687 examined using the ARG_INFO_* macros, defined above:
4688
4689 ARG_INFO_PARMS: a list of parameter decls.
4690 ARG_INFO_TAGS: a list of structure, union and enum tags defined.
4691 ARG_INFO_TYPES: a list of argument types to go in the FUNCTION_TYPE.
4692 ARG_INFO_OTHERS: a list of non-parameter decls (notably enumeration
4693 constants) defined with the parameters.
4694
4695 This tree_list node is later fed to 'grokparms' and 'store_parm_decls'.
4696
4697 ELLIPSIS being true means the argument list ended in '...' so don't
4698 append a sentinel (void_list_node) to the end of the type-list. */
4699
4700 tree
4701 get_parm_info (bool ellipsis)
4702 {
4703 struct c_binding *b = current_scope->bindings;
4704 tree arg_info = make_node (TREE_LIST);
4705 tree parms = 0;
4706 tree tags = 0;
4707 tree types = 0;
4708 tree others = 0;
4709
4710 static bool explained_incomplete_types = false;
4711 bool gave_void_only_once_err = false;
4712
4713 /* The bindings in this scope must not get put into a block.
4714 We will take care of deleting the binding nodes. */
4715 current_scope->bindings = 0;
4716
4717 /* This function is only called if there was *something* on the
4718 parameter list. */
4719 #ifdef ENABLE_CHECKING
4720 if (b == 0)
4721 abort ();
4722 #endif
4723
4724 /* A parameter list consisting solely of 'void' indicates that the
4725 function takes no arguments. But if the 'void' is qualified
4726 (by 'const' or 'volatile'), or has a storage class specifier
4727 ('register'), then the behavior is undefined; issue an error.
4728 Typedefs for 'void' are OK (see DR#157). */
4729 if (b->prev == 0 /* one binding */
4730 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4731 && !DECL_NAME (b->decl) /* anonymous */
4732 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4733 {
4734 if (TREE_THIS_VOLATILE (b->decl)
4735 || TREE_READONLY (b->decl)
4736 || C_DECL_REGISTER (b->decl))
4737 error ("'void' as only parameter may not be qualified");
4738
4739 /* There cannot be an ellipsis. */
4740 if (ellipsis)
4741 error ("'void' must be the only parameter");
4742
4743 ARG_INFO_TYPES (arg_info) = void_list_node;
4744 return arg_info;
4745 }
4746
4747 if (!ellipsis)
4748 types = void_list_node;
4749
4750 /* Break up the bindings list into parms, tags, types, and others;
4751 apply sanity checks; purge the name-to-decl bindings. */
4752 while (b)
4753 {
4754 tree decl = b->decl;
4755 tree type = TREE_TYPE (decl);
4756 const char *keyword;
4757
4758 switch (TREE_CODE (decl))
4759 {
4760 case PARM_DECL:
4761 if (b->id)
4762 {
4763 #ifdef ENABLE_CHECKING
4764 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4765 #endif
4766 I_SYMBOL_BINDING (b->id) = b->shadowed;
4767 }
4768
4769 /* Check for forward decls that never got their actual decl. */
4770 if (TREE_ASM_WRITTEN (decl))
4771 error ("%Jparameter '%D' has just a forward declaration",
4772 decl, decl);
4773 /* Check for (..., void, ...) and issue an error. */
4774 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4775 {
4776 if (!gave_void_only_once_err)
4777 {
4778 error ("'void' must be the only parameter");
4779 gave_void_only_once_err = true;
4780 }
4781 }
4782 else
4783 {
4784 /* Valid parameter, add it to the list. */
4785 TREE_CHAIN (decl) = parms;
4786 parms = decl;
4787
4788 /* Since there is a prototype, args are passed in their
4789 declared types. The back end may override this later. */
4790 DECL_ARG_TYPE (decl) = type;
4791 types = tree_cons (0, type, types);
4792 }
4793 break;
4794
4795 case ENUMERAL_TYPE: keyword = "struct"; goto tag;
4796 case UNION_TYPE: keyword = "union"; goto tag;
4797 case RECORD_TYPE: keyword = "enum"; goto tag;
4798 tag:
4799 /* Types may not have tag-names, in which case the type
4800 appears in the bindings list with b->id NULL. */
4801 if (b->id)
4802 {
4803 #ifdef ENABLE_CHECKING
4804 if (I_TAG_BINDING (b->id) != b) abort ();
4805 #endif
4806 I_TAG_BINDING (b->id) = b->shadowed;
4807 }
4808
4809 /* Warn about any struct, union or enum tags defined in a
4810 parameter list. The scope of such types is limited to
4811 the parameter list, which is rarely if ever desirable
4812 (it's impossible to call such a function with type-
4813 correct arguments). An anonymous union parm type is
4814 meaningful as a GNU extension, so don't warn for that. */
4815 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4816 {
4817 if (b->id)
4818 /* The %s will be one of 'struct', 'union', or 'enum'. */
4819 warning ("'%s %E' declared inside parameter list",
4820 keyword, b->id);
4821 else
4822 /* The %s will be one of 'struct', 'union', or 'enum'. */
4823 warning ("anonymous %s declared inside parameter list",
4824 keyword);
4825
4826 if (! explained_incomplete_types)
4827 {
4828 warning ("its scope is only this definition or declaration,"
4829 " which is probably not what you want");
4830 explained_incomplete_types = true;
4831 }
4832 }
4833
4834 tags = tree_cons (b->id, decl, tags);
4835 break;
4836
4837 case CONST_DECL:
4838 case TYPE_DECL:
4839 /* CONST_DECLs appear here when we have an embedded enum,
4840 and TYPE_DECLs appear here when we have an embedded struct
4841 or union. No warnings for this - we already warned about the
4842 type itself. */
4843 TREE_CHAIN (decl) = others;
4844 others = decl;
4845 /* fall through */
4846
4847 case ERROR_MARK:
4848 /* error_mark_node appears here when we have an undeclared
4849 variable. Just throw it away. */
4850 if (b->id)
4851 {
4852 #ifdef ENABLE_CHECKING
4853 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4854 #endif
4855 I_SYMBOL_BINDING (b->id) = b->shadowed;
4856 }
4857 break;
4858
4859 /* Other things that might be encountered. */
4860 case LABEL_DECL:
4861 case FUNCTION_DECL:
4862 case VAR_DECL:
4863 default:
4864 abort ();
4865 }
4866
4867 b = free_binding_and_advance (b);
4868 }
4869
4870 ARG_INFO_PARMS (arg_info) = parms;
4871 ARG_INFO_TAGS (arg_info) = tags;
4872 ARG_INFO_TYPES (arg_info) = types;
4873 ARG_INFO_OTHERS (arg_info) = others;
4874 return arg_info;
4875 }
4876 \f
4877 /* Get the struct, enum or union (CODE says which) with tag NAME.
4878 Define the tag as a forward-reference if it is not defined. */
4879
4880 tree
4881 xref_tag (enum tree_code code, tree name)
4882 {
4883 /* If a cross reference is requested, look up the type
4884 already defined for this tag and return it. */
4885
4886 tree ref = lookup_tag (code, name, 0);
4887 /* If this is the right type of tag, return what we found.
4888 (This reference will be shadowed by shadow_tag later if appropriate.)
4889 If this is the wrong type of tag, do not return it. If it was the
4890 wrong type in the same scope, we will have had an error
4891 message already; if in a different scope and declaring
4892 a name, pending_xref_error will give an error message; but if in a
4893 different scope and not declaring a name, this tag should
4894 shadow the previous declaration of a different type of tag, and
4895 this would not work properly if we return the reference found.
4896 (For example, with "struct foo" in an outer scope, "union foo;"
4897 must shadow that tag with a new one of union type.) */
4898 if (ref && TREE_CODE (ref) == code)
4899 return ref;
4900
4901 /* If no such tag is yet defined, create a forward-reference node
4902 and record it as the "definition".
4903 When a real declaration of this type is found,
4904 the forward-reference will be altered into a real type. */
4905
4906 ref = make_node (code);
4907 if (code == ENUMERAL_TYPE)
4908 {
4909 /* Give the type a default layout like unsigned int
4910 to avoid crashing if it does not get defined. */
4911 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4912 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4913 TYPE_USER_ALIGN (ref) = 0;
4914 TYPE_UNSIGNED (ref) = 1;
4915 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4916 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4917 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4918 }
4919
4920 pushtag (name, ref);
4921
4922 return ref;
4923 }
4924 \f
4925 /* Make sure that the tag NAME is defined *in the current scope*
4926 at least as a forward reference.
4927 CODE says which kind of tag NAME ought to be. */
4928
4929 tree
4930 start_struct (enum tree_code code, tree name)
4931 {
4932 /* If there is already a tag defined at this scope
4933 (as a forward reference), just return it. */
4934
4935 tree ref = 0;
4936
4937 if (name != 0)
4938 ref = lookup_tag (code, name, 1);
4939 if (ref && TREE_CODE (ref) == code)
4940 {
4941 if (TYPE_FIELDS (ref))
4942 {
4943 if (code == UNION_TYPE)
4944 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4945 else
4946 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4947 }
4948 }
4949 else
4950 {
4951 /* Otherwise create a forward-reference just so the tag is in scope. */
4952
4953 ref = make_node (code);
4954 pushtag (name, ref);
4955 }
4956
4957 C_TYPE_BEING_DEFINED (ref) = 1;
4958 TYPE_PACKED (ref) = flag_pack_struct;
4959 return ref;
4960 }
4961
4962 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4963 of a structure component, returning a FIELD_DECL node.
4964 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
4965
4966 This is done during the parsing of the struct declaration.
4967 The FIELD_DECL nodes are chained together and the lot of them
4968 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4969
4970 tree
4971 grokfield (tree declarator, tree declspecs, tree width)
4972 {
4973 tree value;
4974
4975 if (declarator == NULL_TREE && width == NULL_TREE)
4976 {
4977 /* This is an unnamed decl.
4978
4979 If we have something of the form "union { list } ;" then this
4980 is the anonymous union extension. Similarly for struct.
4981
4982 If this is something of the form "struct foo;", then
4983 If MS extensions are enabled, this is handled as an
4984 anonymous struct.
4985 Otherwise this is a forward declaration of a structure tag.
4986
4987 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4988 If MS extensions are enabled and foo names a structure, then
4989 again this is an anonymous struct.
4990 Otherwise this is an error.
4991
4992 Oh what a horrid tangled web we weave. I wonder if MS consciously
4993 took this from Plan 9 or if it was an accident of implementation
4994 that took root before someone noticed the bug... */
4995
4996 tree type = TREE_VALUE (declspecs);
4997
4998 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4999 type = TREE_TYPE (type);
5000 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
5001 {
5002 if (flag_ms_extensions)
5003 ; /* ok */
5004 else if (flag_iso)
5005 goto warn_unnamed_field;
5006 else if (TYPE_NAME (type) == NULL)
5007 ; /* ok */
5008 else
5009 goto warn_unnamed_field;
5010 }
5011 else
5012 {
5013 warn_unnamed_field:
5014 warning ("declaration does not declare anything");
5015 return NULL_TREE;
5016 }
5017 }
5018
5019 value = grokdeclarator (declarator, declspecs, FIELD, 0,
5020 width ? &width : NULL);
5021
5022 finish_decl (value, NULL_TREE, NULL_TREE);
5023 DECL_INITIAL (value) = width;
5024
5025 return value;
5026 }
5027 \f
5028 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5029 the list such that this does not present a problem later. */
5030
5031 static void
5032 detect_field_duplicates (tree fieldlist)
5033 {
5034 tree x, y;
5035 int timeout = 10;
5036
5037 /* First, see if there are more than "a few" fields.
5038 This is trivially true if there are zero or one fields. */
5039 if (!fieldlist)
5040 return;
5041 x = TREE_CHAIN (fieldlist);
5042 if (!x)
5043 return;
5044 do {
5045 timeout--;
5046 x = TREE_CHAIN (x);
5047 } while (timeout > 0 && x);
5048
5049 /* If there were "few" fields, avoid the overhead of allocating
5050 a hash table. Instead just do the nested traversal thing. */
5051 if (timeout > 0)
5052 {
5053 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5054 if (DECL_NAME (x))
5055 {
5056 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5057 if (DECL_NAME (y) == DECL_NAME (x))
5058 {
5059 error ("%Jduplicate member '%D'", x, x);
5060 DECL_NAME (x) = NULL_TREE;
5061 }
5062 }
5063 }
5064 else
5065 {
5066 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5067 void **slot;
5068
5069 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5070 if ((y = DECL_NAME (x)) != 0)
5071 {
5072 slot = htab_find_slot (htab, y, INSERT);
5073 if (*slot)
5074 {
5075 error ("%Jduplicate member '%D'", x, x);
5076 DECL_NAME (x) = NULL_TREE;
5077 }
5078 *slot = y;
5079 }
5080
5081 htab_delete (htab);
5082 }
5083 }
5084
5085 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5086 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5087 ATTRIBUTES are attributes to be applied to the structure. */
5088
5089 tree
5090 finish_struct (tree t, tree fieldlist, tree attributes)
5091 {
5092 tree x;
5093 bool toplevel = file_scope == current_scope;
5094 int saw_named_field;
5095
5096 /* If this type was previously laid out as a forward reference,
5097 make sure we lay it out again. */
5098
5099 TYPE_SIZE (t) = 0;
5100
5101 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5102
5103 if (pedantic)
5104 {
5105 for (x = fieldlist; x; x = TREE_CHAIN (x))
5106 if (DECL_NAME (x) != 0)
5107 break;
5108
5109 if (x == 0)
5110 pedwarn ("%s has no %s",
5111 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5112 fieldlist ? _("named members") : _("members"));
5113 }
5114
5115 /* Install struct as DECL_CONTEXT of each field decl.
5116 Also process specified field sizes,m which is found in the DECL_INITIAL.
5117 Store 0 there, except for ": 0" fields (so we can find them
5118 and delete them, below). */
5119
5120 saw_named_field = 0;
5121 for (x = fieldlist; x; x = TREE_CHAIN (x))
5122 {
5123 DECL_CONTEXT (x) = t;
5124 DECL_PACKED (x) |= TYPE_PACKED (t);
5125
5126 /* If any field is const, the structure type is pseudo-const. */
5127 if (TREE_READONLY (x))
5128 C_TYPE_FIELDS_READONLY (t) = 1;
5129 else
5130 {
5131 /* A field that is pseudo-const makes the structure likewise. */
5132 tree t1 = TREE_TYPE (x);
5133 while (TREE_CODE (t1) == ARRAY_TYPE)
5134 t1 = TREE_TYPE (t1);
5135 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5136 && C_TYPE_FIELDS_READONLY (t1))
5137 C_TYPE_FIELDS_READONLY (t) = 1;
5138 }
5139
5140 /* Any field that is volatile means variables of this type must be
5141 treated in some ways as volatile. */
5142 if (TREE_THIS_VOLATILE (x))
5143 C_TYPE_FIELDS_VOLATILE (t) = 1;
5144
5145 /* Any field of nominal variable size implies structure is too. */
5146 if (C_DECL_VARIABLE_SIZE (x))
5147 C_TYPE_VARIABLE_SIZE (t) = 1;
5148
5149 /* Detect invalid nested redefinition. */
5150 if (TREE_TYPE (x) == t)
5151 error ("nested redefinition of `%s'",
5152 IDENTIFIER_POINTER (TYPE_NAME (t)));
5153
5154 if (DECL_INITIAL (x))
5155 {
5156 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5157 DECL_SIZE (x) = bitsize_int (width);
5158 DECL_BIT_FIELD (x) = 1;
5159 SET_DECL_C_BIT_FIELD (x);
5160 }
5161
5162 DECL_INITIAL (x) = 0;
5163
5164 /* Detect flexible array member in an invalid context. */
5165 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5166 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5167 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5168 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5169 {
5170 if (TREE_CODE (t) == UNION_TYPE)
5171 {
5172 error ("%Jflexible array member in union", x);
5173 TREE_TYPE (x) = error_mark_node;
5174 }
5175 else if (TREE_CHAIN (x) != NULL_TREE)
5176 {
5177 error ("%Jflexible array member not at end of struct", x);
5178 TREE_TYPE (x) = error_mark_node;
5179 }
5180 else if (! saw_named_field)
5181 {
5182 error ("%Jflexible array member in otherwise empty struct", x);
5183 TREE_TYPE (x) = error_mark_node;
5184 }
5185 }
5186
5187 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5188 && flexible_array_type_p (TREE_TYPE (x)))
5189 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5190
5191 if (DECL_NAME (x))
5192 saw_named_field = 1;
5193 }
5194
5195 detect_field_duplicates (fieldlist);
5196
5197 /* Now we have the nearly final fieldlist. Record it,
5198 then lay out the structure or union (including the fields). */
5199
5200 TYPE_FIELDS (t) = fieldlist;
5201
5202 layout_type (t);
5203
5204 /* Delete all zero-width bit-fields from the fieldlist. */
5205 {
5206 tree *fieldlistp = &fieldlist;
5207 while (*fieldlistp)
5208 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5209 *fieldlistp = TREE_CHAIN (*fieldlistp);
5210 else
5211 fieldlistp = &TREE_CHAIN (*fieldlistp);
5212 }
5213
5214 /* Now we have the truly final field list.
5215 Store it in this type and in the variants. */
5216
5217 TYPE_FIELDS (t) = fieldlist;
5218
5219 /* If there are lots of fields, sort so we can look through them fast.
5220 We arbitrarily consider 16 or more elts to be "a lot". */
5221
5222 {
5223 int len = 0;
5224
5225 for (x = fieldlist; x; x = TREE_CHAIN (x))
5226 {
5227 if (len > 15 || DECL_NAME (x) == NULL)
5228 break;
5229 len += 1;
5230 }
5231
5232 if (len > 15)
5233 {
5234 tree *field_array;
5235 struct lang_type *space;
5236 struct sorted_fields_type *space2;
5237
5238 len += list_length (x);
5239
5240 /* Use the same allocation policy here that make_node uses, to
5241 ensure that this lives as long as the rest of the struct decl.
5242 All decls in an inline function need to be saved. */
5243
5244 space = ggc_alloc (sizeof (struct lang_type));
5245 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5246
5247 len = 0;
5248 space->s = space2;
5249 field_array = &space2->elts[0];
5250 for (x = fieldlist; x; x = TREE_CHAIN (x))
5251 {
5252 field_array[len++] = x;
5253
5254 /* If there is anonymous struct or union, break out of the loop. */
5255 if (DECL_NAME (x) == NULL)
5256 break;
5257 }
5258 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5259 if (x == NULL)
5260 {
5261 TYPE_LANG_SPECIFIC (t) = space;
5262 TYPE_LANG_SPECIFIC (t)->s->len = len;
5263 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5264 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5265 }
5266 }
5267 }
5268
5269 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5270 {
5271 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5272 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5273 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5274 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5275 }
5276
5277 /* If this was supposed to be a transparent union, but we can't
5278 make it one, warn and turn off the flag. */
5279 if (TREE_CODE (t) == UNION_TYPE
5280 && TYPE_TRANSPARENT_UNION (t)
5281 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5282 {
5283 TYPE_TRANSPARENT_UNION (t) = 0;
5284 warning ("union cannot be made transparent");
5285 }
5286
5287 /* If this structure or union completes the type of any previous
5288 variable declaration, lay it out and output its rtl. */
5289 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5290 x;
5291 x = TREE_CHAIN (x))
5292 {
5293 tree decl = TREE_VALUE (x);
5294 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5295 layout_array_type (TREE_TYPE (decl));
5296 if (TREE_CODE (decl) != TYPE_DECL)
5297 {
5298 layout_decl (decl, 0);
5299 if (c_dialect_objc ())
5300 objc_check_decl (decl);
5301 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5302 if (! toplevel)
5303 expand_decl (decl);
5304 }
5305 }
5306 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5307
5308 /* Finish debugging output for this type. */
5309 rest_of_type_compilation (t, toplevel);
5310
5311 return t;
5312 }
5313
5314 /* Lay out the type T, and its element type, and so on. */
5315
5316 static void
5317 layout_array_type (tree t)
5318 {
5319 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5320 layout_array_type (TREE_TYPE (t));
5321 layout_type (t);
5322 }
5323 \f
5324 /* Begin compiling the definition of an enumeration type.
5325 NAME is its name (or null if anonymous).
5326 Returns the type object, as yet incomplete.
5327 Also records info about it so that build_enumerator
5328 may be used to declare the individual values as they are read. */
5329
5330 tree
5331 start_enum (tree name)
5332 {
5333 tree enumtype = 0;
5334
5335 /* If this is the real definition for a previous forward reference,
5336 fill in the contents in the same object that used to be the
5337 forward reference. */
5338
5339 if (name != 0)
5340 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5341
5342 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5343 {
5344 enumtype = make_node (ENUMERAL_TYPE);
5345 pushtag (name, enumtype);
5346 }
5347
5348 C_TYPE_BEING_DEFINED (enumtype) = 1;
5349
5350 if (TYPE_VALUES (enumtype) != 0)
5351 {
5352 /* This enum is a named one that has been declared already. */
5353 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5354
5355 /* Completely replace its old definition.
5356 The old enumerators remain defined, however. */
5357 TYPE_VALUES (enumtype) = 0;
5358 }
5359
5360 enum_next_value = integer_zero_node;
5361 enum_overflow = 0;
5362
5363 if (flag_short_enums)
5364 TYPE_PACKED (enumtype) = 1;
5365
5366 return enumtype;
5367 }
5368
5369 /* After processing and defining all the values of an enumeration type,
5370 install their decls in the enumeration type and finish it off.
5371 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5372 and ATTRIBUTES are the specified attributes.
5373 Returns ENUMTYPE. */
5374
5375 tree
5376 finish_enum (tree enumtype, tree values, tree attributes)
5377 {
5378 tree pair, tem;
5379 tree minnode = 0, maxnode = 0, enum_value_type;
5380 int precision, unsign;
5381 bool toplevel = (file_scope == current_scope);
5382
5383 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5384
5385 /* Calculate the maximum value of any enumerator in this type. */
5386
5387 if (values == error_mark_node)
5388 minnode = maxnode = integer_zero_node;
5389 else
5390 {
5391 minnode = maxnode = TREE_VALUE (values);
5392 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5393 {
5394 tree value = TREE_VALUE (pair);
5395 if (tree_int_cst_lt (maxnode, value))
5396 maxnode = value;
5397 if (tree_int_cst_lt (value, minnode))
5398 minnode = value;
5399 }
5400 }
5401
5402 /* Construct the final type of this enumeration. It is the same
5403 as one of the integral types - the narrowest one that fits, except
5404 that normally we only go as narrow as int - and signed iff any of
5405 the values are negative. */
5406 unsign = (tree_int_cst_sgn (minnode) >= 0);
5407 precision = MAX (min_precision (minnode, unsign),
5408 min_precision (maxnode, unsign));
5409 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5410 {
5411 tree narrowest = c_common_type_for_size (precision, unsign);
5412 if (narrowest == 0)
5413 {
5414 warning ("enumeration values exceed range of largest integer");
5415 narrowest = long_long_integer_type_node;
5416 }
5417
5418 precision = TYPE_PRECISION (narrowest);
5419 }
5420 else
5421 precision = TYPE_PRECISION (integer_type_node);
5422
5423 if (precision == TYPE_PRECISION (integer_type_node))
5424 enum_value_type = c_common_type_for_size (precision, 0);
5425 else
5426 enum_value_type = enumtype;
5427
5428 TYPE_MIN_VALUE (enumtype) = minnode;
5429 TYPE_MAX_VALUE (enumtype) = maxnode;
5430 TYPE_PRECISION (enumtype) = precision;
5431 TYPE_UNSIGNED (enumtype) = unsign;
5432 TYPE_SIZE (enumtype) = 0;
5433 layout_type (enumtype);
5434
5435 if (values != error_mark_node)
5436 {
5437 /* Change the type of the enumerators to be the enum type. We
5438 need to do this irrespective of the size of the enum, for
5439 proper type checking. Replace the DECL_INITIALs of the
5440 enumerators, and the value slots of the list, with copies
5441 that have the enum type; they cannot be modified in place
5442 because they may be shared (e.g. integer_zero_node) Finally,
5443 change the purpose slots to point to the names of the decls. */
5444 for (pair = values; pair; pair = TREE_CHAIN (pair))
5445 {
5446 tree enu = TREE_PURPOSE (pair);
5447
5448 TREE_TYPE (enu) = enumtype;
5449
5450 /* The ISO C Standard mandates enumerators to have type int,
5451 even though the underlying type of an enum type is
5452 unspecified. Here we convert any enumerators that fit in
5453 an int to type int, to avoid promotions to unsigned types
5454 when comparing integers with enumerators that fit in the
5455 int range. When -pedantic is given, build_enumerator()
5456 would have already taken care of those that don't fit. */
5457 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5458 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5459 else
5460 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5461
5462 TREE_PURPOSE (pair) = DECL_NAME (enu);
5463 TREE_VALUE (pair) = DECL_INITIAL (enu);
5464 }
5465
5466 TYPE_VALUES (enumtype) = values;
5467 }
5468
5469 /* Fix up all variant types of this enum type. */
5470 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5471 {
5472 if (tem == enumtype)
5473 continue;
5474 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5475 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5476 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5477 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5478 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5479 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5480 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5481 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5482 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5483 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5484 }
5485
5486 /* Finish debugging output for this type. */
5487 rest_of_type_compilation (enumtype, toplevel);
5488
5489 return enumtype;
5490 }
5491
5492 /* Build and install a CONST_DECL for one value of the
5493 current enumeration type (one that was begun with start_enum).
5494 Return a tree-list containing the CONST_DECL and its value.
5495 Assignment of sequential values by default is handled here. */
5496
5497 tree
5498 build_enumerator (tree name, tree value)
5499 {
5500 tree decl, type;
5501
5502 /* Validate and default VALUE. */
5503
5504 /* Remove no-op casts from the value. */
5505 if (value)
5506 STRIP_TYPE_NOPS (value);
5507
5508 if (value != 0)
5509 {
5510 /* Don't issue more errors for error_mark_node (i.e. an
5511 undeclared identifier) - just ignore the value expression. */
5512 if (value == error_mark_node)
5513 value = 0;
5514 else if (TREE_CODE (value) != INTEGER_CST)
5515 {
5516 error ("enumerator value for '%E' is not an integer constant", name);
5517 value = 0;
5518 }
5519 else
5520 {
5521 value = default_conversion (value);
5522 constant_expression_warning (value);
5523 }
5524 }
5525
5526 /* Default based on previous value. */
5527 /* It should no longer be possible to have NON_LVALUE_EXPR
5528 in the default. */
5529 if (value == 0)
5530 {
5531 value = enum_next_value;
5532 if (enum_overflow)
5533 error ("overflow in enumeration values");
5534 }
5535
5536 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5537 {
5538 pedwarn ("ISO C restricts enumerator values to range of `int'");
5539 /* XXX This causes -pedantic to change the meaning of the program.
5540 Remove? -zw 2004-03-15 */
5541 value = convert (integer_type_node, value);
5542 }
5543
5544 /* Set basis for default for next value. */
5545 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5546 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5547
5548 /* Now create a declaration for the enum value name. */
5549
5550 type = TREE_TYPE (value);
5551 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5552 TYPE_PRECISION (integer_type_node)),
5553 (TYPE_PRECISION (type)
5554 >= TYPE_PRECISION (integer_type_node)
5555 && TYPE_UNSIGNED (type)));
5556
5557 decl = build_decl (CONST_DECL, name, type);
5558 DECL_INITIAL (decl) = convert (type, value);
5559 pushdecl (decl);
5560
5561 return tree_cons (decl, value, NULL_TREE);
5562 }
5563
5564 \f
5565 /* Create the FUNCTION_DECL for a function definition.
5566 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5567 the declaration; they describe the function's name and the type it returns,
5568 but twisted together in a fashion that parallels the syntax of C.
5569
5570 This function creates a binding context for the function body
5571 as well as setting up the FUNCTION_DECL in current_function_decl.
5572
5573 Returns 1 on success. If the DECLARATOR is not suitable for a function
5574 (it defines a datum instead), we return 0, which tells
5575 yyparse to report a parse error. */
5576
5577 int
5578 start_function (tree declspecs, tree declarator, tree attributes)
5579 {
5580 tree decl1, old_decl;
5581 tree restype;
5582 int old_immediate_size_expand = immediate_size_expand;
5583
5584 current_function_returns_value = 0; /* Assume, until we see it does. */
5585 current_function_returns_null = 0;
5586 current_function_returns_abnormally = 0;
5587 warn_about_return_type = 0;
5588 current_extern_inline = 0;
5589 c_in_iteration_stmt = 0;
5590 c_in_case_stmt = 0;
5591
5592 /* Don't expand any sizes in the return type of the function. */
5593 immediate_size_expand = 0;
5594
5595 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
5596
5597 /* If the declarator is not suitable for a function definition,
5598 cause a syntax error. */
5599 if (decl1 == 0)
5600 {
5601 immediate_size_expand = old_immediate_size_expand;
5602 return 0;
5603 }
5604
5605 decl_attributes (&decl1, attributes, 0);
5606
5607 if (DECL_DECLARED_INLINE_P (decl1)
5608 && DECL_UNINLINABLE (decl1)
5609 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5610 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5611
5612 announce_function (decl1);
5613
5614 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5615 {
5616 error ("return type is an incomplete type");
5617 /* Make it return void instead. */
5618 TREE_TYPE (decl1)
5619 = build_function_type (void_type_node,
5620 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5621 }
5622
5623 if (warn_about_return_type)
5624 pedwarn_c99 ("return type defaults to `int'");
5625
5626 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5627 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5628 DECL_INITIAL (decl1) = error_mark_node;
5629
5630 /* If this definition isn't a prototype and we had a prototype declaration
5631 before, copy the arg type info from that prototype.
5632 But not if what we had before was a builtin function. */
5633 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5634 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5635 && !DECL_BUILT_IN (old_decl)
5636 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5637 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5638 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5639 {
5640 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5641 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5642 }
5643
5644 /* Optionally warn of old-fashioned def with no previous prototype. */
5645 if (warn_strict_prototypes
5646 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5647 && C_DECL_ISNT_PROTOTYPE (old_decl))
5648 warning ("function declaration isn't a prototype");
5649 /* Optionally warn of any global def with no previous prototype. */
5650 else if (warn_missing_prototypes
5651 && TREE_PUBLIC (decl1)
5652 && ! MAIN_NAME_P (DECL_NAME (decl1))
5653 && C_DECL_ISNT_PROTOTYPE (old_decl))
5654 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5655 /* Optionally warn of any def with no previous prototype
5656 if the function has already been used. */
5657 else if (warn_missing_prototypes
5658 && old_decl != 0 && TREE_USED (old_decl)
5659 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5660 warning ("%J'%D' was used with no prototype before its definition",
5661 decl1, decl1);
5662 /* Optionally warn of any global def with no previous declaration. */
5663 else if (warn_missing_declarations
5664 && TREE_PUBLIC (decl1)
5665 && old_decl == 0
5666 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5667 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5668 /* Optionally warn of any def with no previous declaration
5669 if the function has already been used. */
5670 else if (warn_missing_declarations
5671 && old_decl != 0 && TREE_USED (old_decl)
5672 && C_DECL_IMPLICIT (old_decl))
5673 warning ("%J`%D' was used with no declaration before its definition",
5674 decl1, decl1);
5675
5676 /* This is a definition, not a reference.
5677 So normally clear DECL_EXTERNAL.
5678 However, `extern inline' acts like a declaration
5679 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5680 DECL_EXTERNAL (decl1) = current_extern_inline;
5681
5682 /* This function exists in static storage.
5683 (This does not mean `static' in the C sense!) */
5684 TREE_STATIC (decl1) = 1;
5685
5686 /* A nested function is not global. */
5687 if (current_function_decl != 0)
5688 TREE_PUBLIC (decl1) = 0;
5689
5690 #ifdef ENABLE_CHECKING
5691 /* This is the earliest point at which we might know the assembler
5692 name of the function. Thus, if it's set before this, die horribly. */
5693 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5694 abort ();
5695 #endif
5696
5697 /* If #pragma weak was used, mark the decl weak now. */
5698 if (current_scope == file_scope)
5699 maybe_apply_pragma_weak (decl1);
5700
5701 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5702 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5703 {
5704 tree args;
5705 int argct = 0;
5706
5707 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5708 != integer_type_node)
5709 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5710
5711 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5712 args = TREE_CHAIN (args))
5713 {
5714 tree type = args ? TREE_VALUE (args) : 0;
5715
5716 if (type == void_type_node)
5717 break;
5718
5719 ++argct;
5720 switch (argct)
5721 {
5722 case 1:
5723 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5724 pedwarn ("%Jfirst argument of '%D' should be `int'",
5725 decl1, decl1);
5726 break;
5727
5728 case 2:
5729 if (TREE_CODE (type) != POINTER_TYPE
5730 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5731 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5732 != char_type_node))
5733 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5734 decl1, decl1);
5735 break;
5736
5737 case 3:
5738 if (TREE_CODE (type) != POINTER_TYPE
5739 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5740 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5741 != char_type_node))
5742 pedwarn ("%Jthird argument of '%D' should probably be "
5743 "'char **'", decl1, decl1);
5744 break;
5745 }
5746 }
5747
5748 /* It is intentional that this message does not mention the third
5749 argument because it's only mentioned in an appendix of the
5750 standard. */
5751 if (argct > 0 && (argct < 2 || argct > 3))
5752 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5753
5754 if (! TREE_PUBLIC (decl1))
5755 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5756 }
5757
5758 /* Record the decl so that the function name is defined.
5759 If we already have a decl for this name, and it is a FUNCTION_DECL,
5760 use the old decl. */
5761
5762 current_function_decl = pushdecl (decl1);
5763
5764 push_scope ();
5765 declare_parm_level ();
5766
5767 make_decl_rtl (current_function_decl, NULL);
5768
5769 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5770 /* Promote the value to int before returning it. */
5771 if (c_promoting_integer_type_p (restype))
5772 {
5773 /* It retains unsignedness if not really getting wider. */
5774 if (TYPE_UNSIGNED (restype)
5775 && (TYPE_PRECISION (restype)
5776 == TYPE_PRECISION (integer_type_node)))
5777 restype = unsigned_type_node;
5778 else
5779 restype = integer_type_node;
5780 }
5781 DECL_RESULT (current_function_decl)
5782 = build_decl (RESULT_DECL, NULL_TREE, restype);
5783
5784 /* If this fcn was already referenced via a block-scope `extern' decl
5785 (or an implicit decl), propagate certain information about the usage. */
5786 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5787 TREE_ADDRESSABLE (current_function_decl) = 1;
5788
5789 immediate_size_expand = old_immediate_size_expand;
5790
5791 start_fname_decls ();
5792
5793 return 1;
5794 }
5795 \f
5796 /* Subroutine of store_parm_decls which handles new-style function
5797 definitions (prototype format). The parms already have decls, so we
5798 need only record them as in effect and complain if any redundant
5799 old-style parm decls were written. */
5800 static void
5801 store_parm_decls_newstyle (tree fndecl, tree arg_info)
5802 {
5803 tree decl;
5804 tree parms = ARG_INFO_PARMS (arg_info);
5805 tree tags = ARG_INFO_TAGS (arg_info);
5806 tree others = ARG_INFO_OTHERS (arg_info);
5807
5808 if (current_scope->bindings)
5809 {
5810 error ("%Jold-style parameter declarations in prototyped "
5811 "function definition", fndecl);
5812
5813 /* Get rid of the old-style declarations. */
5814 pop_scope ();
5815 push_scope ();
5816 }
5817 /* Don't issue this warning for nested functions, and don't issue this
5818 warning if we got here because ARG_INFO_TYPES was error_mark_node
5819 (this happens when a function definition has just an ellipsis in
5820 its parameter list). */
5821 else if (warn_traditional && !in_system_header
5822 && DECL_CONTEXT (fndecl) == current_file_decl
5823 && ARG_INFO_TYPES (arg_info) != error_mark_node)
5824 warning ("%Jtraditional C rejects ISO C style function definitions",
5825 fndecl);
5826
5827 /* Now make all the parameter declarations visible in the function body.
5828 We can bypass most of the grunt work of pushdecl. */
5829 for (decl = parms; decl; decl = TREE_CHAIN (decl))
5830 {
5831 DECL_CONTEXT (decl) = current_function_decl;
5832 if (DECL_NAME (decl))
5833 bind (DECL_NAME (decl), decl, current_scope);
5834 else
5835 error ("%Jparameter name omitted", decl);
5836 }
5837
5838 /* Record the parameter list in the function declaration. */
5839 DECL_ARGUMENTS (fndecl) = parms;
5840
5841 /* Now make all the ancillary declarations visible, likewise. */
5842 for (decl = others; decl; decl = TREE_CHAIN (decl))
5843 {
5844 DECL_CONTEXT (decl) = current_function_decl;
5845 if (DECL_NAME (decl))
5846 bind (DECL_NAME (decl), decl, current_scope);
5847 }
5848
5849 /* And all the tag declarations. */
5850 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5851 if (TREE_PURPOSE (decl))
5852 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope);
5853 }
5854
5855 /* Subroutine of store_parm_decls which handles old-style function
5856 definitions (separate parameter list and declarations). */
5857
5858 static void
5859 store_parm_decls_oldstyle (tree fndecl, tree arg_info)
5860 {
5861 struct c_binding *b;
5862 tree parm, decl, last;
5863 tree parmids = ARG_INFO_PARMS (arg_info);
5864
5865 /* We use DECL_WEAK as a flag to show which parameters have been
5866 seen already, since it is not used on PARM_DECL. */
5867 #ifdef ENABLE_CHECKING
5868 for (b = current_scope->bindings; b; b = b->prev)
5869 if (TREE_CODE (b->decl) == PARM_DECL && DECL_WEAK (b->decl))
5870 abort ();
5871 #endif
5872
5873 if (warn_old_style_definition && !in_system_header)
5874 warning ("%Jold-style function definition", fndecl);
5875
5876 /* Match each formal parameter name with its declaration. Save each
5877 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5878 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5879 {
5880 if (TREE_VALUE (parm) == 0)
5881 {
5882 error ("%Jparameter name missing from parameter list", fndecl);
5883 TREE_PURPOSE (parm) = 0;
5884 continue;
5885 }
5886
5887 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5888 if (b && b->contour == current_scope)
5889 {
5890 decl = b->decl;
5891 /* If we got something other than a PARM_DECL it is an error. */
5892 if (TREE_CODE (decl) != PARM_DECL)
5893 error ("%J'%D' declared as a non-parameter", decl, decl);
5894 /* If the declaration is already marked, we have a duplicate
5895 name. Complain and ignore the duplicate. */
5896 else if (DECL_WEAK (decl))
5897 {
5898 error ("%Jmultiple parameters named '%D'", decl, decl);
5899 TREE_PURPOSE (parm) = 0;
5900 continue;
5901 }
5902 /* If the declaration says "void", complain and turn it into
5903 an int. */
5904 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5905 {
5906 error ("%Jparameter '%D' declared with void type", decl, decl);
5907 TREE_TYPE (decl) = integer_type_node;
5908 DECL_ARG_TYPE (decl) = integer_type_node;
5909 layout_decl (decl, 0);
5910 }
5911 }
5912 /* If no declaration found, default to int. */
5913 else
5914 {
5915 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5916 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5917 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5918 pushdecl (decl);
5919
5920 if (flag_isoc99)
5921 pedwarn ("%Jtype of '%D' defaults to 'int'", decl, decl);
5922 else if (extra_warnings)
5923 warning ("%Jtype of '%D' defaults to 'int'", decl, decl);
5924 }
5925
5926 TREE_PURPOSE (parm) = decl;
5927 DECL_WEAK (decl) = 1;
5928 }
5929
5930 /* Now examine the parms chain for incomplete declarations
5931 and declarations with no corresponding names. */
5932
5933 for (b = current_scope->bindings; b; b = b->prev)
5934 {
5935 parm = b->decl;
5936 if (TREE_CODE (parm) != PARM_DECL)
5937 continue;
5938
5939 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5940 {
5941 error ("%Jparameter '%D' has incomplete type", parm, parm);
5942 TREE_TYPE (parm) = error_mark_node;
5943 }
5944
5945 if (! DECL_WEAK (parm))
5946 {
5947 error ("%Jdeclaration for parameter '%D' but no such parameter",
5948 parm, parm);
5949
5950 /* Pretend the parameter was not missing.
5951 This gets us to a standard state and minimizes
5952 further error messages. */
5953 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5954 }
5955 }
5956
5957 /* Chain the declarations together in the order of the list of
5958 names. Store that chain in the function decl, replacing the
5959 list of names. Update the current scope to match. */
5960 DECL_ARGUMENTS (fndecl) = 0;
5961
5962 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5963 if (TREE_PURPOSE (parm))
5964 break;
5965 if (parm && TREE_PURPOSE (parm))
5966 {
5967 last = TREE_PURPOSE (parm);
5968 DECL_ARGUMENTS (fndecl) = last;
5969 DECL_WEAK (last) = 0;
5970
5971 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5972 if (TREE_PURPOSE (parm))
5973 {
5974 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5975 last = TREE_PURPOSE (parm);
5976 DECL_WEAK (last) = 0;
5977 }
5978 TREE_CHAIN (last) = 0;
5979 }
5980
5981 /* If there was a previous prototype,
5982 set the DECL_ARG_TYPE of each argument according to
5983 the type previously specified, and report any mismatches. */
5984
5985 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5986 {
5987 tree type;
5988 for (parm = DECL_ARGUMENTS (fndecl),
5989 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5990 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5991 != void_type_node));
5992 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5993 {
5994 if (parm == 0 || type == 0
5995 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5996 {
5997 error ("number of arguments doesn't match prototype");
5998 error ("%Hprototype declaration",
5999 &current_function_prototype_locus);
6000 break;
6001 }
6002 /* Type for passing arg must be consistent with that
6003 declared for the arg. ISO C says we take the unqualified
6004 type for parameters declared with qualified type. */
6005 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6006 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
6007 COMPARE_STRICT))
6008 {
6009 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6010 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6011 {
6012 /* Adjust argument to match prototype. E.g. a previous
6013 `int foo(float);' prototype causes
6014 `int foo(x) float x; {...}' to be treated like
6015 `int foo(float x) {...}'. This is particularly
6016 useful for argument types like uid_t. */
6017 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6018
6019 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6020 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6021 && TYPE_PRECISION (TREE_TYPE (parm))
6022 < TYPE_PRECISION (integer_type_node))
6023 DECL_ARG_TYPE (parm) = integer_type_node;
6024
6025 if (pedantic)
6026 {
6027 pedwarn ("promoted argument '%D' "
6028 "doesn't match prototype", parm);
6029 pedwarn ("%Hprototype declaration",
6030 &current_function_prototype_locus);
6031 }
6032 }
6033 else
6034 {
6035 error ("argument '%D' doesn't match prototype", parm);
6036 error ("%Hprototype declaration",
6037 &current_function_prototype_locus);
6038 }
6039 }
6040 }
6041 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6042 }
6043
6044 /* Otherwise, create a prototype that would match. */
6045
6046 else
6047 {
6048 tree actual = 0, last = 0, type;
6049
6050 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6051 {
6052 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6053 if (last)
6054 TREE_CHAIN (last) = type;
6055 else
6056 actual = type;
6057 last = type;
6058 }
6059 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6060 if (last)
6061 TREE_CHAIN (last) = type;
6062 else
6063 actual = type;
6064
6065 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6066 of the type of this function, but we need to avoid having this
6067 affect the types of other similarly-typed functions, so we must
6068 first force the generation of an identical (but separate) type
6069 node for the relevant function type. The new node we create
6070 will be a variant of the main variant of the original function
6071 type. */
6072
6073 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6074
6075 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6076 }
6077 }
6078
6079 /* Store the parameter declarations into the current function declaration.
6080 This is called after parsing the parameter declarations, before
6081 digesting the body of the function.
6082
6083 For an old-style definition, construct a prototype out of the old-style
6084 parameter declarations and inject it into the function's type. */
6085
6086 void
6087 store_parm_decls (void)
6088 {
6089 tree fndecl = current_function_decl;
6090
6091 /* The function containing FNDECL, if any. */
6092 tree context = decl_function_context (fndecl);
6093
6094 /* The argument information block for FNDECL. */
6095 tree arg_info = DECL_ARGUMENTS (fndecl);
6096
6097 /* True if this definition is written with a prototype. Note:
6098 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6099 list in a function definition as equivalent to (void) -- an
6100 empty argument list specifies the function has no parameters,
6101 but only (void) sets up a prototype for future calls. */
6102 bool proto = ARG_INFO_TYPES (arg_info) != 0;
6103
6104 if (proto)
6105 store_parm_decls_newstyle (fndecl, arg_info);
6106 else
6107 store_parm_decls_oldstyle (fndecl, arg_info);
6108
6109 /* The next call to push_scope will be a function body. */
6110
6111 next_is_function_body = true;
6112
6113 /* Write a record describing this function definition to the prototypes
6114 file (if requested). */
6115
6116 gen_aux_info_record (fndecl, 1, 0, proto);
6117
6118 /* Initialize the RTL code for the function. */
6119 allocate_struct_function (fndecl);
6120
6121 /* Begin the statement tree for this function. */
6122 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
6123
6124 /* If this is a nested function, save away the sizes of any
6125 variable-size types so that we can expand them when generating
6126 RTL. */
6127 if (context)
6128 {
6129 tree t;
6130
6131 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6132 = nreverse (get_pending_sizes ());
6133 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6134 t;
6135 t = TREE_CHAIN (t))
6136 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6137 }
6138
6139 /* This function is being processed in whole-function mode. */
6140 cfun->x_whole_function_mode_p = 1;
6141
6142 /* Even though we're inside a function body, we still don't want to
6143 call expand_expr to calculate the size of a variable-sized array.
6144 We haven't necessarily assigned RTL to all variables yet, so it's
6145 not safe to try to expand expressions involving them. */
6146 immediate_size_expand = 0;
6147 cfun->x_dont_save_pending_sizes_p = 1;
6148 }
6149 \f
6150 /* Finish up a function declaration and compile that function
6151 all the way to assembler language output. The free the storage
6152 for the function definition.
6153
6154 This is called after parsing the body of the function definition. */
6155
6156 void
6157 finish_function (void)
6158 {
6159 tree fndecl = current_function_decl;
6160
6161 /* When a function declaration is totally empty, e.g.
6162 void foo(void) { }
6163 (the argument list is irrelevant) the compstmt rule will not
6164 bother calling push_scope/pop_scope, which means we get here with
6165 the scope stack out of sync. Detect this situation by noticing
6166 that current_scope is still as store_parm_decls left it, and do
6167 a dummy push/pop to get back to consistency.
6168 Note that the call to push_scope does not actually push another
6169 scope - see there for details. */
6170
6171 if (current_scope->parm_flag && next_is_function_body)
6172 {
6173 push_scope ();
6174 pop_scope ();
6175 }
6176
6177 if (TREE_CODE (fndecl) == FUNCTION_DECL
6178 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6179 {
6180 tree args = DECL_ARGUMENTS (fndecl);
6181 for (; args; args = TREE_CHAIN (args))
6182 {
6183 tree type = TREE_TYPE (args);
6184 if (INTEGRAL_TYPE_P (type)
6185 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6186 DECL_ARG_TYPE (args) = integer_type_node;
6187 }
6188 }
6189
6190 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6191 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6192
6193 /* Must mark the RESULT_DECL as being in this function. */
6194
6195 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6196 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6197
6198 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6199 {
6200 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6201 != integer_type_node)
6202 {
6203 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6204 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6205 if (!warn_main)
6206 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6207 }
6208 else
6209 {
6210 #ifdef DEFAULT_MAIN_RETURN
6211 /* Make it so that `main' always returns success by default. */
6212 DEFAULT_MAIN_RETURN;
6213 #else
6214 if (flag_isoc99)
6215 c_expand_return (integer_zero_node);
6216 #endif
6217 }
6218 }
6219
6220 finish_fname_decls ();
6221
6222 /* Tie off the statement tree for this function. */
6223 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6224
6225 /* Complain if there's just no return statement. */
6226 if (warn_return_type
6227 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6228 && !current_function_returns_value && !current_function_returns_null
6229 /* Don't complain if we abort. */
6230 && !current_function_returns_abnormally
6231 /* Don't warn for main(). */
6232 && !MAIN_NAME_P (DECL_NAME (fndecl))
6233 /* Or if they didn't actually specify a return type. */
6234 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6235 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6236 inline function, as we might never be compiled separately. */
6237 && DECL_INLINE (fndecl))
6238 warning ("no return statement in function returning non-void");
6239
6240 /* With just -Wextra, complain only if function returns both with
6241 and without a value. */
6242 if (extra_warnings
6243 && current_function_returns_value
6244 && current_function_returns_null)
6245 warning ("this function may return with or without a value");
6246
6247 /* We're leaving the context of this function, so zap cfun.
6248 It's still in DECL_STRUCT_FUNCTION , and we'll restore it in
6249 tree_rest_of_compilation. */
6250 cfun = NULL;
6251
6252 /* ??? Objc emits functions after finalizing the compilation unit.
6253 This should be cleaned up later and this conditional removed. */
6254 if (!cgraph_global_info_ready)
6255 cgraph_finalize_function (fndecl, false);
6256 else
6257 c_expand_body (fndecl);
6258 current_function_decl = NULL;
6259 }
6260
6261 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6262 then we are already in the process of generating RTL for another
6263 function. */
6264
6265 static void
6266 c_expand_body_1 (tree fndecl, int nested_p)
6267 {
6268 if (nested_p)
6269 {
6270 /* Make sure that we will evaluate variable-sized types involved
6271 in our function's type. */
6272 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6273
6274 /* Squirrel away our current state. */
6275 push_function_context ();
6276 }
6277
6278 tree_rest_of_compilation (fndecl, nested_p);
6279
6280 if (nested_p)
6281 /* Return to the enclosing function. */
6282 pop_function_context ();
6283
6284 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6285 {
6286 if (targetm.have_ctors_dtors)
6287 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6288 DEFAULT_INIT_PRIORITY);
6289 else
6290 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6291 }
6292
6293 if (DECL_STATIC_DESTRUCTOR (fndecl))
6294 {
6295 if (targetm.have_ctors_dtors)
6296 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6297 DEFAULT_INIT_PRIORITY);
6298 else
6299 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6300 }
6301 }
6302
6303 /* Like c_expand_body_1 but only for unnested functions. */
6304
6305 void
6306 c_expand_body (tree fndecl)
6307 {
6308
6309 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6310 c_expand_body_1 (fndecl, 0);
6311 }
6312 \f
6313 /* Check the declarations given in a for-loop for satisfying the C99
6314 constraints. */
6315 void
6316 check_for_loop_decls (void)
6317 {
6318 struct c_binding *b;
6319
6320 if (!flag_isoc99)
6321 {
6322 /* If we get here, declarations have been used in a for loop without
6323 the C99 for loop scope. This doesn't make much sense, so don't
6324 allow it. */
6325 error ("'for' loop initial declaration used outside C99 mode");
6326 return;
6327 }
6328 /* C99 subclause 6.8.5 paragraph 3:
6329
6330 [#3] The declaration part of a for statement shall only
6331 declare identifiers for objects having storage class auto or
6332 register.
6333
6334 It isn't clear whether, in this sentence, "identifiers" binds to
6335 "shall only declare" or to "objects" - that is, whether all identifiers
6336 declared must be identifiers for objects, or whether the restriction
6337 only applies to those that are. (A question on this in comp.std.c
6338 in November 2000 received no answer.) We implement the strictest
6339 interpretation, to avoid creating an extension which later causes
6340 problems. */
6341
6342 for (b = current_scope->bindings; b; b = b->prev)
6343 {
6344 tree id = b->id;
6345 tree decl = b->decl;
6346
6347 if (!id)
6348 continue;
6349
6350 switch (TREE_CODE (decl))
6351 {
6352 case VAR_DECL:
6353 if (TREE_STATIC (decl))
6354 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6355 "initial declaration", decl, decl);
6356 else if (DECL_EXTERNAL (decl))
6357 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6358 "initial declaration", decl, decl);
6359 break;
6360
6361 case RECORD_TYPE:
6362 error ("'struct %E' declared in 'for' loop initial declaration", id);
6363 break;
6364 case UNION_TYPE:
6365 error ("'union %E' declared in 'for' loop initial declaration", id);
6366 break;
6367 case ENUMERAL_TYPE:
6368 error ("'enum %E' declared in 'for' loop initial declaration", id);
6369 break;
6370 default:
6371 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6372 "initial declaration", decl, decl);
6373 }
6374 }
6375 }
6376 \f
6377 /* Save and reinitialize the variables
6378 used during compilation of a C function. */
6379
6380 void
6381 c_push_function_context (struct function *f)
6382 {
6383 struct language_function *p;
6384 p = ggc_alloc (sizeof (struct language_function));
6385 f->language = p;
6386
6387 p->base.x_stmt_tree = c_stmt_tree;
6388 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6389 p->x_in_iteration_stmt = c_in_iteration_stmt;
6390 p->x_in_case_stmt = c_in_case_stmt;
6391 p->returns_value = current_function_returns_value;
6392 p->returns_null = current_function_returns_null;
6393 p->returns_abnormally = current_function_returns_abnormally;
6394 p->warn_about_return_type = warn_about_return_type;
6395 p->extern_inline = current_extern_inline;
6396 }
6397
6398 /* Restore the variables used during compilation of a C function. */
6399
6400 void
6401 c_pop_function_context (struct function *f)
6402 {
6403 struct language_function *p = f->language;
6404
6405 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6406 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6407 {
6408 /* Stop pointing to the local nodes about to be freed. */
6409 /* But DECL_INITIAL must remain nonzero so we know this
6410 was an actual function definition. */
6411 DECL_INITIAL (current_function_decl) = error_mark_node;
6412 DECL_ARGUMENTS (current_function_decl) = 0;
6413 }
6414
6415 c_stmt_tree = p->base.x_stmt_tree;
6416 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6417 c_in_iteration_stmt = p->x_in_iteration_stmt;
6418 c_in_case_stmt = p->x_in_case_stmt;
6419 current_function_returns_value = p->returns_value;
6420 current_function_returns_null = p->returns_null;
6421 current_function_returns_abnormally = p->returns_abnormally;
6422 warn_about_return_type = p->warn_about_return_type;
6423 current_extern_inline = p->extern_inline;
6424
6425 f->language = NULL;
6426 }
6427
6428 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6429
6430 void
6431 c_dup_lang_specific_decl (tree decl)
6432 {
6433 struct lang_decl *ld;
6434
6435 if (!DECL_LANG_SPECIFIC (decl))
6436 return;
6437
6438 ld = ggc_alloc (sizeof (struct lang_decl));
6439 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6440 DECL_LANG_SPECIFIC (decl) = ld;
6441 }
6442
6443 /* The functions below are required for functionality of doing
6444 function at once processing in the C front end. Currently these
6445 functions are not called from anywhere in the C front end, but as
6446 these changes continue, that will change. */
6447
6448 /* Returns nonzero if the current statement is a full expression,
6449 i.e. temporaries created during that statement should be destroyed
6450 at the end of the statement. */
6451
6452 int
6453 stmts_are_full_exprs_p (void)
6454 {
6455 return 0;
6456 }
6457
6458 /* Returns the stmt_tree (if any) to which statements are currently
6459 being added. If there is no active statement-tree, NULL is
6460 returned. */
6461
6462 stmt_tree
6463 current_stmt_tree (void)
6464 {
6465 return &c_stmt_tree;
6466 }
6467
6468 /* Returns the stack of SCOPE_STMTs for the current function. */
6469
6470 tree *
6471 current_scope_stmt_stack (void)
6472 {
6473 return &c_scope_stmt_stack;
6474 }
6475
6476 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6477 C. */
6478
6479 int
6480 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6481 {
6482 return 0;
6483 }
6484
6485 /* Dummy function in place of callback used by C++. */
6486
6487 void
6488 extract_interface_info (void)
6489 {
6490 }
6491
6492 /* Return a new COMPOUND_STMT, after adding it to the current
6493 statement tree. */
6494
6495 tree
6496 c_begin_compound_stmt (void)
6497 {
6498 tree stmt;
6499
6500 /* Create the COMPOUND_STMT. */
6501 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6502
6503 return stmt;
6504 }
6505
6506 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6507 common code. */
6508
6509 void
6510 c_expand_decl_stmt (tree t)
6511 {
6512 tree decl = DECL_STMT_DECL (t);
6513
6514 /* Expand nested functions. */
6515 if (TREE_CODE (decl) == FUNCTION_DECL
6516 && DECL_CONTEXT (decl) == current_function_decl
6517 && DECL_SAVED_TREE (decl))
6518 c_expand_body_1 (decl, 1);
6519 }
6520
6521 /* Return the global value of T as a symbol. */
6522
6523 tree
6524 identifier_global_value (tree t)
6525 {
6526 struct c_binding *b;
6527
6528 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6529 if (b->contour == file_scope || b->contour == external_scope)
6530 return b->decl;
6531
6532 return 0;
6533 }
6534
6535 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6536 otherwise the name is found in ridpointers from RID_INDEX. */
6537
6538 void
6539 record_builtin_type (enum rid rid_index, const char *name, tree type)
6540 {
6541 tree id;
6542 if (name == 0)
6543 id = ridpointers[(int) rid_index];
6544 else
6545 id = get_identifier (name);
6546 pushdecl (build_decl (TYPE_DECL, id, type));
6547 }
6548
6549 /* Build the void_list_node (void_type_node having been created). */
6550 tree
6551 build_void_list_node (void)
6552 {
6553 tree t = build_tree_list (NULL_TREE, void_type_node);
6554 return t;
6555 }
6556
6557 /* Return something to represent absolute declarators containing a *.
6558 TARGET is the absolute declarator that the * contains.
6559 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6560 to apply to the pointer type, represented as identifiers, possible mixed
6561 with attributes.
6562
6563 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6564 if attributes are present) and whose type is the modifier list. */
6565
6566 tree
6567 make_pointer_declarator (tree type_quals_attrs, tree target)
6568 {
6569 tree quals, attrs;
6570 tree itarget = target;
6571 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6572 if (attrs != NULL_TREE)
6573 itarget = tree_cons (attrs, target, NULL_TREE);
6574 return build1 (INDIRECT_REF, quals, itarget);
6575 }
6576
6577 /* A wrapper around lhd_set_decl_assembler_name that gives static
6578 variables their C names if they are at file scope and only one
6579 translation unit is being compiled, for backwards compatibility
6580 with certain bizarre assembler hacks (like crtstuff.c). */
6581
6582 void
6583 c_static_assembler_name (tree decl)
6584 {
6585 if (num_in_fnames == 1
6586 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6587 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6588 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6589 else
6590 lhd_set_decl_assembler_name (decl);
6591 }
6592
6593 /* Perform final processing on file-scope data. */
6594 static void
6595 c_write_global_declarations_1 (tree globals)
6596 {
6597 size_t len = list_length (globals);
6598 tree *vec = xmalloc (sizeof (tree) * len);
6599 size_t i;
6600 tree decl;
6601
6602 /* Process the decls in the order they were written. */
6603 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6604 vec[i] = decl;
6605
6606 wrapup_global_declarations (vec, len);
6607 check_global_declarations (vec, len);
6608
6609 free (vec);
6610 }
6611
6612 void
6613 c_write_global_declarations (void)
6614 {
6615 tree t;
6616
6617 /* We don't want to do this if generating a PCH. */
6618 if (pch_file)
6619 return;
6620
6621 /* Process all file scopes in this compilation. */
6622 for (t = current_file_decl; t; t = TREE_CHAIN (t))
6623 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
6624
6625 /* Now do the same for the externals scope. */
6626 t = pop_scope ();
6627 if (t)
6628 c_write_global_declarations_1 (BLOCK_VARS (t));
6629 }
6630
6631 #include "gt-c-decl.h"
This page took 0.34081 seconds and 5 git commands to generate.