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