]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/decl.c
f3cc1b9f6f1a42ca7344001f4d3af06d23e2116b
[gcc.git] / gcc / cp / decl.c
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
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 <stdio.h>
30 #include "config.h"
31 #include "tree.h"
32 #include "rtl.h"
33 #include "flags.h"
34 #include "cp-tree.h"
35 #include "decl.h"
36 #include "lex.h"
37 #include <sys/types.h>
38 #include <signal.h>
39 #include "obstack.h"
40
41 #define obstack_chunk_alloc xmalloc
42 #define obstack_chunk_free free
43
44 extern tree builtin_return_address_fndecl;
45
46 extern struct obstack permanent_obstack;
47
48 extern int current_class_depth;
49
50 extern tree cleanups_this_call;
51
52 /* Stack of places to restore the search obstack back to. */
53
54 /* Obstack used for remembering local class declarations (like
55 enums and static (const) members. */
56 #include "stack.h"
57 static struct obstack decl_obstack;
58 static struct stack_level *decl_stack;
59
60 #ifndef CHAR_TYPE_SIZE
61 #define CHAR_TYPE_SIZE BITS_PER_UNIT
62 #endif
63
64 #ifndef SHORT_TYPE_SIZE
65 #define SHORT_TYPE_SIZE (BITS_PER_UNIT * MIN ((UNITS_PER_WORD + 1) / 2, 2))
66 #endif
67
68 #ifndef INT_TYPE_SIZE
69 #define INT_TYPE_SIZE BITS_PER_WORD
70 #endif
71
72 #ifndef LONG_TYPE_SIZE
73 #define LONG_TYPE_SIZE BITS_PER_WORD
74 #endif
75
76 #ifndef LONG_LONG_TYPE_SIZE
77 #define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
78 #endif
79
80 #ifndef WCHAR_UNSIGNED
81 #define WCHAR_UNSIGNED 0
82 #endif
83
84 #ifndef FLOAT_TYPE_SIZE
85 #define FLOAT_TYPE_SIZE BITS_PER_WORD
86 #endif
87
88 #ifndef DOUBLE_TYPE_SIZE
89 #define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
90 #endif
91
92 #ifndef LONG_DOUBLE_TYPE_SIZE
93 #define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
94 #endif
95
96 /* We let tm.h override the types used here, to handle trivial differences
97 such as the choice of unsigned int or long unsigned int for size_t.
98 When machines start needing nontrivial differences in the size type,
99 it would be best to do something here to figure out automatically
100 from other information what type to use. */
101
102 #ifndef SIZE_TYPE
103 #define SIZE_TYPE "long unsigned int"
104 #endif
105
106 #ifndef PTRDIFF_TYPE
107 #define PTRDIFF_TYPE "long int"
108 #endif
109
110 #ifndef WCHAR_TYPE
111 #define WCHAR_TYPE "int"
112 #endif
113
114 #define builtin_function(NAME, TYPE, CODE, LIBNAME) \
115 define_function (NAME, TYPE, CODE, (void (*)())pushdecl, LIBNAME)
116 #define auto_function(NAME, TYPE, CODE) \
117 do { \
118 tree __name = NAME; \
119 tree __type = TYPE; \
120 define_function (IDENTIFIER_POINTER (__name), __type, CODE, \
121 (void (*)())push_overloaded_decl_1, \
122 IDENTIFIER_POINTER (build_decl_overload (__name, TYPE_ARG_TYPES (__type), 0)));\
123 } while (0)
124
125 static tree grokparms PROTO((tree, int));
126 static tree lookup_nested_type PROTO((tree, tree));
127 static char *redeclaration_error_message PROTO((tree, tree));
128 static void grok_op_properties PROTO((tree, int, int));
129
130 tree define_function
131 PROTO((char *, tree, enum built_in_function, void (*)(), char *));
132
133 /* a node which has tree code ERROR_MARK, and whose type is itself.
134 All erroneous expressions are replaced with this node. All functions
135 that accept nodes as arguments should avoid generating error messages
136 if this node is one of the arguments, since it is undesirable to get
137 multiple error messages from one error in the input. */
138
139 tree error_mark_node;
140
141 /* Erroneous argument lists can use this *IFF* they do not modify it. */
142 tree error_mark_list;
143
144 /* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */
145
146 tree short_integer_type_node;
147 tree integer_type_node;
148 tree long_integer_type_node;
149 tree long_long_integer_type_node;
150
151 tree short_unsigned_type_node;
152 tree unsigned_type_node;
153 tree long_unsigned_type_node;
154 tree long_long_unsigned_type_node;
155
156 tree ptrdiff_type_node;
157
158 tree unsigned_char_type_node;
159 tree signed_char_type_node;
160 tree char_type_node;
161 tree wchar_type_node;
162 tree signed_wchar_type_node;
163 tree unsigned_wchar_type_node;
164
165 tree float_type_node;
166 tree double_type_node;
167 tree long_double_type_node;
168
169 tree intQI_type_node;
170 tree intHI_type_node;
171 tree intSI_type_node;
172 tree intDI_type_node;
173
174 tree unsigned_intQI_type_node;
175 tree unsigned_intHI_type_node;
176 tree unsigned_intSI_type_node;
177 tree unsigned_intDI_type_node;
178
179 /* a VOID_TYPE node, and the same, packaged in a TREE_LIST. */
180
181 tree void_type_node, void_list_node;
182 tree void_zero_node;
183
184 /* Nodes for types `void *' and `const void *'. */
185
186 tree ptr_type_node, const_ptr_type_node;
187
188 /* Nodes for types `char *' and `const char *'. */
189
190 tree string_type_node, const_string_type_node;
191
192 /* Type `char[256]' or something like it.
193 Used when an array of char is needed and the size is irrelevant. */
194
195 tree char_array_type_node;
196
197 /* Type `int[256]' or something like it.
198 Used when an array of int needed and the size is irrelevant. */
199
200 tree int_array_type_node;
201
202 /* Type `wchar_t[256]' or something like it.
203 Used when a wide string literal is created. */
204
205 tree wchar_array_type_node;
206
207 /* The bool data type, and constants */
208 tree bool_type_node, true_node, false_node;
209
210 /* type `int ()' -- used for implicit declaration of functions. */
211
212 tree default_function_type;
213
214 /* function types `double (double)' and `double (double, double)', etc. */
215
216 tree double_ftype_double, double_ftype_double_double;
217 tree int_ftype_int, long_ftype_long;
218
219 /* Function type `void (void *, void *, int)' and similar ones. */
220
221 tree void_ftype_ptr_ptr_int, int_ftype_ptr_ptr_int, void_ftype_ptr_int_int;
222
223 /* Function type `char *(char *, char *)' and similar ones */
224 tree string_ftype_ptr_ptr, int_ftype_string_string;
225
226 /* Function type `size_t (const char *)' */
227 tree sizet_ftype_string;
228
229 /* Function type `int (const void *, const void *, size_t)' */
230 tree int_ftype_cptr_cptr_sizet;
231
232 /* C++ extensions */
233 tree vtable_entry_type;
234 tree delta_type_node;
235 tree __t_desc_type_node, __i_desc_type_node, __m_desc_type_node;
236 tree __t_desc_array_type, __i_desc_array_type, __m_desc_array_type;
237 tree class_star_type_node;
238 tree class_type_node, record_type_node, union_type_node, enum_type_node;
239 tree exception_type_node, unknown_type_node;
240 tree opaque_type_node, signature_type_node;
241 tree sigtable_entry_type;
242 tree maybe_gc_cleanup;
243
244 /* Array type `vtable_entry_type[]' */
245 tree vtbl_type_node;
246
247 /* In a destructor, the point at which all derived class destroying
248 has been done, just before any base class destroying will be done. */
249
250 tree dtor_label;
251
252 /* In a constructor, the point at which we are ready to return
253 the pointer to the initialized object. */
254
255 tree ctor_label;
256
257 /* A FUNCTION_DECL which can call `abort'. Not necessarily the
258 one that the user will declare, but sufficient to be called
259 by routines that want to abort the program. */
260
261 tree abort_fndecl;
262
263 extern rtx cleanup_label, return_label;
264
265 /* If original DECL_RESULT of current function was a register,
266 but due to being an addressable named return value, would up
267 on the stack, this variable holds the named return value's
268 original location. */
269 rtx original_result_rtx;
270
271 /* Sequence of insns which represents base initialization. */
272 rtx base_init_insns;
273
274 /* C++: Keep these around to reduce calls to `get_identifier'.
275 Identifiers for `this' in member functions and the auto-delete
276 parameter for destructors. */
277 tree this_identifier, in_charge_identifier;
278 /* Used in pointer to member functions, and in vtables. */
279 tree pfn_identifier, index_identifier, delta_identifier, delta2_identifier;
280 tree pfn_or_delta2_identifier;
281
282 /* A list (chain of TREE_LIST nodes) of named label uses.
283 The TREE_PURPOSE field is the list of variables defined
284 the the label's scope defined at the point of use.
285 The TREE_VALUE field is the LABEL_DECL used.
286 The TREE_TYPE field holds `current_binding_level' at the
287 point of the label's use.
288
289 Used only for jumps to as-yet undefined labels, since
290 jumps to defined labels can have their validity checked
291 by stmt.c. */
292
293 static tree named_label_uses;
294
295 /* A list of objects which have constructors or destructors
296 which reside in the global scope. The decl is stored in
297 the TREE_VALUE slot and the initializer is stored
298 in the TREE_PURPOSE slot. */
299 tree static_aggregates;
300
301 /* -- end of C++ */
302
303 /* Two expressions that are constants with value zero.
304 The first is of type `int', the second of type `void *'. */
305
306 tree integer_zero_node;
307 tree null_pointer_node;
308
309 /* A node for the integer constants 1, 2, and 3. */
310
311 tree integer_one_node, integer_two_node, integer_three_node;
312
313 /* Nonzero if we have seen an invalid cross reference
314 to a struct, union, or enum, but not yet printed the message. */
315
316 tree pending_invalid_xref;
317 /* File and line to appear in the eventual error message. */
318 char *pending_invalid_xref_file;
319 int pending_invalid_xref_line;
320
321 /* While defining an enum type, this is 1 plus the last enumerator
322 constant value. */
323
324 static tree enum_next_value;
325
326 /* Nonzero means that there was overflow computing enum_next_value. */
327
328 static int enum_overflow;
329
330 /* Parsing a function declarator leaves a list of parameter names
331 or a chain or parameter decls here. */
332
333 tree last_function_parms;
334
335 /* Parsing a function declarator leaves here a chain of structure
336 and enum types declared in the parmlist. */
337
338 static tree last_function_parm_tags;
339
340 /* After parsing the declarator that starts a function definition,
341 `start_function' puts here the list of parameter names or chain of decls.
342 `store_parm_decls' finds it here. */
343
344 static tree current_function_parms;
345
346 /* Similar, for last_function_parm_tags. */
347 static tree current_function_parm_tags;
348
349 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
350 that have names. Here so we can clear out their names' definitions
351 at the end of the function. */
352
353 static tree named_labels;
354
355 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
356
357 static tree shadowed_labels;
358
359 #if 0 /* Not needed by C++ */
360 /* Nonzero when store_parm_decls is called indicates a varargs function.
361 Value not meaningful after store_parm_decls. */
362
363 static int c_function_varargs;
364 #endif
365
366 /* The FUNCTION_DECL for the function currently being compiled,
367 or 0 if between functions. */
368 tree current_function_decl;
369
370 /* Set to 0 at beginning of a function definition, set to 1 if
371 a return statement that specifies a return value is seen. */
372
373 int current_function_returns_value;
374
375 /* Set to 0 at beginning of a function definition, set to 1 if
376 a return statement with no argument is seen. */
377
378 int current_function_returns_null;
379
380 /* Set to 0 at beginning of a function definition, and whenever
381 a label (case or named) is defined. Set to value of expression
382 returned from function when that value can be transformed into
383 a named return value. */
384
385 tree current_function_return_value;
386
387 /* Set to nonzero by `grokdeclarator' for a function
388 whose return type is defaulted, if warnings for this are desired. */
389
390 static int warn_about_return_type;
391
392 /* Nonzero when starting a function declared `extern inline'. */
393
394 static int current_extern_inline;
395
396 /* Nonzero means give `double' the same size as `float'. */
397
398 extern int flag_short_double;
399
400 /* Nonzero means don't recognize any builtin functions. */
401
402 extern int flag_no_builtin;
403
404 /* Nonzero means disable GNU extensions. */
405
406 extern int flag_ansi;
407
408 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
409 objects. */
410 extern int flag_huge_objects;
411
412 /* Nonzero if we want to conserve space in the .o files. We do this
413 by putting uninitialized data and runtime initialized data into
414 .common instead of .data at the expense of not flaging multiple
415 definitions. */
416 extern int flag_conserve_space;
417
418 /* Pointers to the base and current top of the language name stack. */
419
420 extern tree *current_lang_base, *current_lang_stack;
421 \f
422 /* C and C++ flags are in decl2.c. */
423
424 /* Set to 0 at beginning of a constructor, set to 1
425 if that function does an allocation before referencing its
426 instance variable. */
427 int current_function_assigns_this;
428 int current_function_just_assigned_this;
429
430 /* Set to 0 at beginning of a function. Set non-zero when
431 store_parm_decls is called. Don't call store_parm_decls
432 if this flag is non-zero! */
433 int current_function_parms_stored;
434
435 /* Current end of entries in the gc obstack for stack pointer variables. */
436
437 int current_function_obstack_index;
438
439 /* Flag saying whether we have used the obstack in this function or not. */
440
441 int current_function_obstack_usage;
442
443 /* Flag used when debugging spew.c */
444
445 extern int spew_debug;
446
447 /* This is a copy of the class_shadowed list of the previous class binding
448 contour when at global scope. It's used to reset IDENTIFIER_CLASS_VALUEs
449 when entering another class scope (i.e. a cache miss). */
450 extern tree previous_class_values;
451
452 \f
453 /* Allocate a level of searching. */
454 struct stack_level *
455 push_decl_level (stack, obstack)
456 struct stack_level *stack;
457 struct obstack *obstack;
458 {
459 struct stack_level tem;
460 tem.prev = stack;
461
462 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
463 }
464 \f
465 /* For each binding contour we allocate a binding_level structure
466 * which records the names defined in that contour.
467 * Contours include:
468 * 0) the global one
469 * 1) one for each function definition,
470 * where internal declarations of the parameters appear.
471 * 2) one for each compound statement,
472 * to record its declarations.
473 *
474 * The current meaning of a name can be found by searching the levels from
475 * the current one out to the global one.
476 *
477 * Off to the side, may be the class_binding_level. This exists
478 * only to catch class-local declarations. It is otherwise
479 * nonexistent.
480 *
481 * Also there may be binding levels that catch cleanups that
482 * must be run when exceptions occur.
483 */
484
485 /* Note that the information in the `names' component of the global contour
486 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
487
488 struct binding_level
489 {
490 /* A chain of _DECL nodes for all variables, constants, functions,
491 * and typedef types. These are in the reverse of the order supplied.
492 */
493 tree names;
494
495 /* A list of structure, union and enum definitions,
496 * for looking up tag names.
497 * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
498 * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
499 * or ENUMERAL_TYPE node.
500 *
501 * C++: the TREE_VALUE nodes can be simple types for component_bindings.
502 *
503 */
504 tree tags;
505
506 /* For each level, a list of shadowed outer-level local definitions
507 to be restored when this level is popped.
508 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
509 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
510 tree shadowed;
511
512 /* Same, for IDENTIFIER_CLASS_VALUE. */
513 tree class_shadowed;
514
515 /* Same, for IDENTIFIER_TYPE_VALUE. */
516 tree type_shadowed;
517
518 /* For each level (except not the global one),
519 a chain of BLOCK nodes for all the levels
520 that were entered and exited one level down. */
521 tree blocks;
522
523 /* The BLOCK node for this level, if one has been preallocated.
524 If 0, the BLOCK is allocated (if needed) when the level is popped. */
525 tree this_block;
526
527 /* The binding level which this one is contained in (inherits from). */
528 struct binding_level *level_chain;
529
530 /* Number of decls in `names' that have incomplete
531 structure or union types. */
532 unsigned short n_incomplete;
533
534 /* 1 for the level that holds the parameters of a function.
535 2 for the level that holds a class declaration.
536 3 for levels that hold parameter declarations. */
537 unsigned parm_flag : 4;
538
539 /* 1 means make a BLOCK for this level regardless of all else.
540 2 for temporary binding contours created by the compiler. */
541 unsigned keep : 3;
542
543 /* Nonzero if this level "doesn't exist" for tags. */
544 unsigned tag_transparent : 1;
545
546 /* Nonzero if this level can safely have additional
547 cleanup-needing variables added to it. */
548 unsigned more_cleanups_ok : 1;
549 unsigned have_cleanups : 1;
550
551 /* Nonzero if we should accept any name as an identifier in
552 this scope. This happens in some template definitions. */
553 unsigned accept_any : 1;
554
555 /* Nonzero if this level is for completing a template class definition
556 inside a binding level that temporarily binds the parameters. This
557 means that definitions here should not be popped off when unwinding
558 this binding level. (Not actually implemented this way,
559 unfortunately.) */
560 unsigned pseudo_global : 1;
561
562 /* Two bits left for this word. */
563
564 #if defined(DEBUG_CP_BINDING_LEVELS)
565 /* Binding depth at which this level began. */
566 unsigned binding_depth;
567 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
568 };
569
570 #define NULL_BINDING_LEVEL ((struct binding_level *) NULL)
571
572 /* The (non-class) binding level currently in effect. */
573
574 static struct binding_level *current_binding_level;
575
576 /* The binding level of the current class, if any. */
577
578 static struct binding_level *class_binding_level;
579
580 /* The current (class or non-class) binding level currently in effect. */
581
582 #define inner_binding_level \
583 (class_binding_level ? class_binding_level : current_binding_level)
584
585 /* A chain of binding_level structures awaiting reuse. */
586
587 static struct binding_level *free_binding_level;
588
589 /* The outermost binding level, for names of file scope.
590 This is created when the compiler is started and exists
591 through the entire run. */
592
593 static struct binding_level *global_binding_level;
594
595 /* Binding level structures are initialized by copying this one. */
596
597 static struct binding_level clear_binding_level;
598
599 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
600
601 static int keep_next_level_flag;
602
603 #if defined(DEBUG_CP_BINDING_LEVELS)
604 static int binding_depth = 0;
605 static int is_class_level = 0;
606
607 static void
608 indent ()
609 {
610 register unsigned i;
611
612 for (i = 0; i < binding_depth*2; i++)
613 putc (' ', stderr);
614 }
615 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
616
617 static tree pushdecl_with_scope PROTO((tree, struct binding_level *));
618
619 static void
620 push_binding_level (newlevel, tag_transparent, keep)
621 struct binding_level *newlevel;
622 int tag_transparent, keep;
623 {
624 /* Add this level to the front of the chain (stack) of levels that
625 are active. */
626 *newlevel = clear_binding_level;
627 if (class_binding_level)
628 {
629 newlevel->level_chain = class_binding_level;
630 class_binding_level = (struct binding_level *)0;
631 }
632 else
633 {
634 newlevel->level_chain = current_binding_level;
635 }
636 current_binding_level = newlevel;
637 newlevel->tag_transparent = tag_transparent;
638 newlevel->more_cleanups_ok = 1;
639 newlevel->keep = keep;
640 #if defined(DEBUG_CP_BINDING_LEVELS)
641 newlevel->binding_depth = binding_depth;
642 indent ();
643 fprintf (stderr, "push %s level 0x%08x line %d\n",
644 (is_class_level) ? "class" : "block", newlevel, lineno);
645 is_class_level = 0;
646 binding_depth++;
647 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
648 }
649
650 static void
651 pop_binding_level ()
652 {
653 if (class_binding_level)
654 current_binding_level = class_binding_level;
655
656 if (global_binding_level)
657 {
658 /* cannot pop a level, if there are none left to pop. */
659 if (current_binding_level == global_binding_level)
660 my_friendly_abort (123);
661 }
662 /* Pop the current level, and free the structure for reuse. */
663 #if defined(DEBUG_CP_BINDING_LEVELS)
664 binding_depth--;
665 indent ();
666 fprintf (stderr, "pop %s level 0x%08x line %d\n",
667 (is_class_level) ? "class" : "block",
668 current_binding_level, lineno);
669 if (is_class_level != (current_binding_level == class_binding_level))
670 #if 0 /* XXX Don't abort when we're watching how things are being managed. */
671 abort ();
672 #else
673 {
674 indent ();
675 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
676 }
677 #endif
678 is_class_level = 0;
679 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
680 {
681 register struct binding_level *level = current_binding_level;
682 current_binding_level = current_binding_level->level_chain;
683 level->level_chain = free_binding_level;
684 #if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */
685 if (level->binding_depth != binding_depth)
686 abort ();
687 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
688 free_binding_level = level;
689
690 class_binding_level = current_binding_level;
691 if (class_binding_level->parm_flag != 2)
692 class_binding_level = 0;
693 while (current_binding_level->parm_flag == 2)
694 current_binding_level = current_binding_level->level_chain;
695 }
696 }
697 \f
698 /* Nonzero if we are currently in the global binding level. */
699
700 int
701 global_bindings_p ()
702 {
703 return current_binding_level == global_binding_level;
704 }
705
706 void
707 keep_next_level ()
708 {
709 keep_next_level_flag = 1;
710 }
711
712 /* Nonzero if the current level needs to have a BLOCK made. */
713
714 int
715 kept_level_p ()
716 {
717 return (current_binding_level->blocks != NULL_TREE
718 || current_binding_level->keep
719 || current_binding_level->names != NULL_TREE
720 || (current_binding_level->tags != NULL_TREE
721 && !current_binding_level->tag_transparent));
722 }
723
724 /* Identify this binding level as a level of parameters. */
725
726 void
727 declare_parm_level ()
728 {
729 current_binding_level->parm_flag = 1;
730 }
731
732 void
733 declare_uninstantiated_type_level ()
734 {
735 current_binding_level->accept_any = 1;
736 }
737
738 int
739 uninstantiated_type_level_p ()
740 {
741 return current_binding_level->accept_any;
742 }
743
744 void
745 declare_pseudo_global_level ()
746 {
747 current_binding_level->pseudo_global = 1;
748 }
749
750 int
751 pseudo_global_level_p ()
752 {
753 return current_binding_level->pseudo_global;
754 }
755
756 void
757 set_class_shadows (shadows)
758 tree shadows;
759 {
760 class_binding_level->class_shadowed = shadows;
761 }
762
763 /* Enter a new binding level.
764 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
765 not for that of tags. */
766
767 void
768 pushlevel (tag_transparent)
769 int tag_transparent;
770 {
771 register struct binding_level *newlevel = NULL_BINDING_LEVEL;
772
773 /* If this is the top level of a function,
774 just make sure that NAMED_LABELS is 0.
775 They should have been set to 0 at the end of the previous function. */
776
777 if (current_binding_level == global_binding_level)
778 my_friendly_assert (named_labels == NULL_TREE, 134);
779
780 /* Reuse or create a struct for this binding level. */
781
782 #if defined(DEBUG_CP_BINDING_LEVELS)
783 if (0)
784 #else /* !defined(DEBUG_CP_BINDING_LEVELS) */
785 if (free_binding_level)
786 #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
787 {
788 newlevel = free_binding_level;
789 free_binding_level = free_binding_level->level_chain;
790 }
791 else
792 {
793 /* Create a new `struct binding_level'. */
794 newlevel = (struct binding_level *) xmalloc (sizeof (struct binding_level));
795 }
796 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
797 GNU_xref_start_scope ((HOST_WIDE_INT) newlevel);
798 keep_next_level_flag = 0;
799 }
800
801 void
802 pushlevel_temporary (tag_transparent)
803 int tag_transparent;
804 {
805 pushlevel (tag_transparent);
806 current_binding_level->keep = 2;
807 clear_last_expr ();
808
809 /* Note we don't call push_momentary() here. Otherwise, it would cause
810 cleanups to be allocated on the momentary obstack, and they will be
811 overwritten by the next statement. */
812
813 expand_start_bindings (0);
814 }
815
816 /* Exit a binding level.
817 Pop the level off, and restore the state of the identifier-decl mappings
818 that were in effect when this level was entered.
819
820 If KEEP == 1, this level had explicit declarations, so
821 and create a "block" (a BLOCK node) for the level
822 to record its declarations and subblocks for symbol table output.
823
824 If KEEP == 2, this level's subblocks go to the front,
825 not the back of the current binding level. This happens,
826 for instance, when code for constructors and destructors
827 need to generate code at the end of a function which must
828 be moved up to the front of the function.
829
830 If FUNCTIONBODY is nonzero, this level is the body of a function,
831 so create a block as if KEEP were set and also clear out all
832 label names.
833
834 If REVERSE is nonzero, reverse the order of decls before putting
835 them into the BLOCK. */
836
837 tree
838 poplevel (keep, reverse, functionbody)
839 int keep;
840 int reverse;
841 int functionbody;
842 {
843 register tree link;
844 /* The chain of decls was accumulated in reverse order.
845 Put it into forward order, just for cleanliness. */
846 tree decls;
847 int tmp = functionbody;
848 int implicit_try_block = current_binding_level->parm_flag == 3;
849 int real_functionbody = current_binding_level->keep == 2
850 ? ((functionbody = 0), tmp) : functionbody;
851 tree tags = functionbody >= 0 ? current_binding_level->tags : 0;
852 tree subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
853 tree block = NULL_TREE;
854 tree decl;
855 int block_previously_created;
856
857 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
858 (HOST_WIDE_INT) current_binding_level->level_chain,
859 current_binding_level->parm_flag,
860 current_binding_level->keep,
861 current_binding_level->tag_transparent);
862
863 if (current_binding_level->keep == 1)
864 keep = 1;
865
866 /* This warning is turned off because it causes warnings for
867 declarations like `extern struct foo *x'. */
868 #if 0
869 /* Warn about incomplete structure types in this level. */
870 for (link = tags; link; link = TREE_CHAIN (link))
871 if (TYPE_SIZE (TREE_VALUE (link)) == NULL_TREE)
872 {
873 tree type = TREE_VALUE (link);
874 char *errmsg;
875 switch (TREE_CODE (type))
876 {
877 case RECORD_TYPE:
878 errmsg = "`struct %s' incomplete in scope ending here";
879 break;
880 case UNION_TYPE:
881 errmsg = "`union %s' incomplete in scope ending here";
882 break;
883 case ENUMERAL_TYPE:
884 errmsg = "`enum %s' incomplete in scope ending here";
885 break;
886 }
887 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
888 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
889 else
890 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
891 error (errmsg, TYPE_NAME_STRING (type));
892 }
893 #endif /* 0 */
894
895 /* Get the decls in the order they were written.
896 Usually current_binding_level->names is in reverse order.
897 But parameter decls were previously put in forward order. */
898
899 if (reverse)
900 current_binding_level->names
901 = decls = nreverse (current_binding_level->names);
902 else
903 decls = current_binding_level->names;
904
905 /* Output any nested inline functions within this block
906 if they weren't already output. */
907
908 for (decl = decls; decl; decl = TREE_CHAIN (decl))
909 if (TREE_CODE (decl) == FUNCTION_DECL
910 && ! TREE_ASM_WRITTEN (decl)
911 && DECL_INITIAL (decl) != NULL_TREE
912 && TREE_ADDRESSABLE (decl))
913 {
914 /* If this decl was copied from a file-scope decl
915 on account of a block-scope extern decl,
916 propagate TREE_ADDRESSABLE to the file-scope decl. */
917 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
918 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
919 else
920 {
921 push_function_context ();
922 output_inline_function (decl);
923 pop_function_context ();
924 }
925 }
926
927 /* If there were any declarations or structure tags in that level,
928 or if this level is a function body,
929 create a BLOCK to record them for the life of this function. */
930
931 block = NULL_TREE;
932 block_previously_created = (current_binding_level->this_block != NULL_TREE);
933 if (block_previously_created)
934 block = current_binding_level->this_block;
935 else if (keep == 1 || functionbody)
936 block = make_node (BLOCK);
937 if (block != NULL_TREE)
938 {
939 BLOCK_VARS (block) = decls;
940 BLOCK_TYPE_TAGS (block) = tags;
941 BLOCK_SUBBLOCKS (block) = subblocks;
942 /* If we created the block earlier on, and we are just diddling it now,
943 then it already should have a proper BLOCK_END_NOTE value associated
944 with it, so avoid trashing that. Otherwise, for a new block, install
945 a new BLOCK_END_NOTE value. */
946 if (! block_previously_created)
947 remember_end_note (block);
948 }
949
950 /* In each subblock, record that this is its superior. */
951
952 if (keep >= 0)
953 for (link = subblocks; link; link = TREE_CHAIN (link))
954 BLOCK_SUPERCONTEXT (link) = block;
955
956 /* Clear out the meanings of the local variables of this level. */
957
958 for (link = decls; link; link = TREE_CHAIN (link))
959 {
960 if (DECL_NAME (link) != NULL_TREE)
961 {
962 /* If the ident. was used or addressed via a local extern decl,
963 don't forget that fact. */
964 if (DECL_EXTERNAL (link))
965 {
966 if (TREE_USED (link))
967 TREE_USED (DECL_ASSEMBLER_NAME (link)) = 1;
968 if (TREE_ADDRESSABLE (link))
969 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
970 }
971 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = NULL_TREE;
972 }
973 }
974
975 /* Restore all name-meanings of the outer levels
976 that were shadowed by this level. */
977
978 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
979 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
980 for (link = current_binding_level->class_shadowed;
981 link; link = TREE_CHAIN (link))
982 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
983 for (link = current_binding_level->type_shadowed;
984 link; link = TREE_CHAIN (link))
985 IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
986
987 /* If the level being exited is the top level of a function,
988 check over all the labels. */
989
990 if (functionbody)
991 {
992 /* If this is the top level block of a function,
993 the vars are the function's parameters.
994 Don't leave them in the BLOCK because they are
995 found in the FUNCTION_DECL instead. */
996
997 BLOCK_VARS (block) = 0;
998
999 /* Clear out the definitions of all label names,
1000 since their scopes end here. */
1001
1002 for (link = named_labels; link; link = TREE_CHAIN (link))
1003 {
1004 register tree label = TREE_VALUE (link);
1005
1006 if (DECL_INITIAL (label) == NULL_TREE)
1007 {
1008 cp_error_at ("label `%D' used but not defined", label);
1009 /* Avoid crashing later. */
1010 define_label (input_filename, 1, DECL_NAME (label));
1011 }
1012 else if (warn_unused && !TREE_USED (label))
1013 cp_warning_at ("label `%D' defined but not used", label);
1014 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), NULL_TREE);
1015
1016 /* Put the labels into the "variables" of the
1017 top-level block, so debugger can see them. */
1018 TREE_CHAIN (label) = BLOCK_VARS (block);
1019 BLOCK_VARS (block) = label;
1020 }
1021
1022 named_labels = NULL_TREE;
1023 }
1024
1025 /* Any uses of undefined labels now operate under constraints
1026 of next binding contour. */
1027 {
1028 struct binding_level *level_chain;
1029 level_chain = current_binding_level->level_chain;
1030 if (level_chain)
1031 {
1032 tree labels;
1033 for (labels = named_label_uses; labels; labels = TREE_CHAIN (labels))
1034 if (TREE_TYPE (labels) == (tree)current_binding_level)
1035 {
1036 TREE_TYPE (labels) = (tree)level_chain;
1037 TREE_PURPOSE (labels) = level_chain->names;
1038 }
1039 }
1040 }
1041
1042 tmp = current_binding_level->keep;
1043
1044 pop_binding_level ();
1045 if (functionbody)
1046 DECL_INITIAL (current_function_decl) = block;
1047 else if (block)
1048 {
1049 if (!block_previously_created)
1050 current_binding_level->blocks
1051 = chainon (current_binding_level->blocks, block);
1052 }
1053 /* If we did not make a block for the level just exited,
1054 any blocks made for inner levels
1055 (since they cannot be recorded as subblocks in that level)
1056 must be carried forward so they will later become subblocks
1057 of something else. */
1058 else if (subblocks)
1059 {
1060 if (keep == 2)
1061 current_binding_level->blocks
1062 = chainon (subblocks, current_binding_level->blocks);
1063 else
1064 current_binding_level->blocks
1065 = chainon (current_binding_level->blocks, subblocks);
1066 }
1067
1068 /* Take care of compiler's internal binding structures. */
1069 if (tmp == 2)
1070 {
1071 #if 0
1072 /* We did not call push_momentary for this
1073 binding contour, so there is nothing to pop. */
1074 pop_momentary ();
1075 #endif
1076 expand_end_bindings (getdecls (), keep, 1);
1077 /* Each and every BLOCK node created here in `poplevel' is important
1078 (e.g. for proper debugging information) so if we created one
1079 earlier, mark it as "used". */
1080 if (block)
1081 TREE_USED (block) = 1;
1082 block = poplevel (keep, reverse, real_functionbody);
1083 }
1084
1085 /* Each and every BLOCK node created here in `poplevel' is important
1086 (e.g. for proper debugging information) so if we created one
1087 earlier, mark it as "used". */
1088 if (block)
1089 TREE_USED (block) = 1;
1090 return block;
1091 }
1092
1093 /* Delete the node BLOCK from the current binding level.
1094 This is used for the block inside a stmt expr ({...})
1095 so that the block can be reinserted where appropriate. */
1096
1097 void
1098 delete_block (block)
1099 tree block;
1100 {
1101 tree t;
1102 if (current_binding_level->blocks == block)
1103 current_binding_level->blocks = TREE_CHAIN (block);
1104 for (t = current_binding_level->blocks; t;)
1105 {
1106 if (TREE_CHAIN (t) == block)
1107 TREE_CHAIN (t) = TREE_CHAIN (block);
1108 else
1109 t = TREE_CHAIN (t);
1110 }
1111 TREE_CHAIN (block) = NULL_TREE;
1112 /* Clear TREE_USED which is always set by poplevel.
1113 The flag is set again if insert_block is called. */
1114 TREE_USED (block) = 0;
1115 }
1116
1117 /* Insert BLOCK at the end of the list of subblocks of the
1118 current binding level. This is used when a BIND_EXPR is expanded,
1119 to handle the BLOCK node inside the BIND_EXPR. */
1120
1121 void
1122 insert_block (block)
1123 tree block;
1124 {
1125 TREE_USED (block) = 1;
1126 current_binding_level->blocks
1127 = chainon (current_binding_level->blocks, block);
1128 }
1129
1130 /* Add BLOCK to the current list of blocks for this binding contour. */
1131 void
1132 add_block_current_level (block)
1133 tree block;
1134 {
1135 current_binding_level->blocks
1136 = chainon (current_binding_level->blocks, block);
1137 }
1138
1139 /* Set the BLOCK node for the innermost scope
1140 (the one we are currently in). */
1141
1142 void
1143 set_block (block)
1144 register tree block;
1145 {
1146 current_binding_level->this_block = block;
1147 }
1148
1149 /* Do a pushlevel for class declarations. */
1150 void
1151 pushlevel_class ()
1152 {
1153 register struct binding_level *newlevel;
1154
1155 /* Reuse or create a struct for this binding level. */
1156 #if defined(DEBUG_CP_BINDING_LEVELS)
1157 if (0)
1158 #else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1159 if (free_binding_level)
1160 #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1161 {
1162 newlevel = free_binding_level;
1163 free_binding_level = free_binding_level->level_chain;
1164 }
1165 else
1166 {
1167 /* Create a new `struct binding_level'. */
1168 newlevel = (struct binding_level *) xmalloc (sizeof (struct binding_level));
1169 }
1170
1171 #if defined(DEBUG_CP_BINDING_LEVELS)
1172 is_class_level = 1;
1173 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1174
1175 push_binding_level (newlevel, 0, 0);
1176
1177 decl_stack = push_decl_level (decl_stack, &decl_obstack);
1178 class_binding_level = current_binding_level;
1179 class_binding_level->parm_flag = 2;
1180 /* We have just pushed into a new binding level. Now, fake out the rest
1181 of the compiler. Set the `current_binding_level' back to point to
1182 the most closely containing non-class binding level. */
1183 do
1184 {
1185 current_binding_level = current_binding_level->level_chain;
1186 }
1187 while (current_binding_level->parm_flag == 2);
1188 }
1189
1190 /* ...and a poplevel for class declarations. FORCE is used to force
1191 clearing out of CLASS_VALUEs after a class definition. */
1192 tree
1193 poplevel_class (force)
1194 int force;
1195 {
1196 register struct binding_level *level = class_binding_level;
1197 tree block = NULL_TREE;
1198 tree shadowed;
1199
1200 my_friendly_assert (level != 0, 354);
1201
1202 decl_stack = pop_stack_level (decl_stack);
1203 for (shadowed = level->shadowed; shadowed; shadowed = TREE_CHAIN (shadowed))
1204 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (shadowed)) = TREE_VALUE (shadowed);
1205 /* If we're leaving a toplevel class, don't bother to do the setting
1206 of IDENTIFER_CLASS_VALUE to NULL_TREE, since first of all this slot
1207 shouldn't even be used when current_class_type isn't set, and second,
1208 if we don't touch it here, we're able to use the caching effect if the
1209 next time we're entering a class scope, it is the same class. */
1210 if (current_class_depth != 1 || force)
1211 for (shadowed = level->class_shadowed;
1212 shadowed;
1213 shadowed = TREE_CHAIN (shadowed))
1214 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = TREE_VALUE (shadowed);
1215 else
1216 /* Remember to save what IDENTIFIER's were bound in this scope so we
1217 can recover from cache misses. */
1218 previous_class_values = class_binding_level->class_shadowed;
1219 for (shadowed = level->type_shadowed;
1220 shadowed;
1221 shadowed = TREE_CHAIN (shadowed))
1222 IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed)) = TREE_VALUE (shadowed);
1223
1224 GNU_xref_end_scope ((HOST_WIDE_INT) class_binding_level,
1225 (HOST_WIDE_INT) class_binding_level->level_chain,
1226 class_binding_level->parm_flag,
1227 class_binding_level->keep,
1228 class_binding_level->tag_transparent);
1229
1230 if (class_binding_level->parm_flag != 2)
1231 class_binding_level = (struct binding_level *)0;
1232
1233 /* Now, pop out of the the binding level which we created up in the
1234 `pushlevel_class' routine. */
1235 #if defined(DEBUG_CP_BINDING_LEVELS)
1236 is_class_level = 1;
1237 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1238
1239 pop_binding_level ();
1240
1241 return block;
1242 }
1243 \f
1244 /* For debugging. */
1245 int no_print_functions = 0;
1246 int no_print_builtins = 0;
1247
1248 void
1249 print_binding_level (lvl)
1250 struct binding_level *lvl;
1251 {
1252 tree t;
1253 int i = 0, len;
1254 fprintf (stderr, " blocks=");
1255 fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1256 fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d",
1257 lvl->n_incomplete, lvl->parm_flag, lvl->keep);
1258 if (lvl->tag_transparent)
1259 fprintf (stderr, " tag-transparent");
1260 if (lvl->more_cleanups_ok)
1261 fprintf (stderr, " more-cleanups-ok");
1262 if (lvl->have_cleanups)
1263 fprintf (stderr, " have-cleanups");
1264 fprintf (stderr, "\n");
1265 if (lvl->names)
1266 {
1267 fprintf (stderr, " names:\t");
1268 /* We can probably fit 3 names to a line? */
1269 for (t = lvl->names; t; t = TREE_CHAIN (t))
1270 {
1271 if (no_print_functions && (TREE_CODE(t) == FUNCTION_DECL))
1272 continue;
1273 if (no_print_builtins
1274 && (TREE_CODE(t) == TYPE_DECL)
1275 && (!strcmp(DECL_SOURCE_FILE(t),"<built-in>")))
1276 continue;
1277
1278 /* Function decls tend to have longer names. */
1279 if (TREE_CODE (t) == FUNCTION_DECL)
1280 len = 3;
1281 else
1282 len = 2;
1283 i += len;
1284 if (i > 6)
1285 {
1286 fprintf (stderr, "\n\t");
1287 i = len;
1288 }
1289 print_node_brief (stderr, "", t, 0);
1290 if (TREE_CODE (t) == ERROR_MARK)
1291 break;
1292 }
1293 if (i)
1294 fprintf (stderr, "\n");
1295 }
1296 if (lvl->tags)
1297 {
1298 fprintf (stderr, " tags:\t");
1299 i = 0;
1300 for (t = lvl->tags; t; t = TREE_CHAIN (t))
1301 {
1302 if (TREE_PURPOSE (t) == NULL_TREE)
1303 len = 3;
1304 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1305 len = 2;
1306 else
1307 len = 4;
1308 i += len;
1309 if (i > 5)
1310 {
1311 fprintf (stderr, "\n\t");
1312 i = len;
1313 }
1314 if (TREE_PURPOSE (t) == NULL_TREE)
1315 {
1316 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1317 fprintf (stderr, ">");
1318 }
1319 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1320 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1321 else
1322 {
1323 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1324 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1325 fprintf (stderr, ">");
1326 }
1327 }
1328 if (i)
1329 fprintf (stderr, "\n");
1330 }
1331 if (lvl->shadowed)
1332 {
1333 fprintf (stderr, " shadowed:");
1334 for (t = lvl->shadowed; t; t = TREE_CHAIN (t))
1335 {
1336 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1337 }
1338 fprintf (stderr, "\n");
1339 }
1340 if (lvl->class_shadowed)
1341 {
1342 fprintf (stderr, " class-shadowed:");
1343 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1344 {
1345 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1346 }
1347 fprintf (stderr, "\n");
1348 }
1349 if (lvl->type_shadowed)
1350 {
1351 fprintf (stderr, " type-shadowed:");
1352 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1353 {
1354 #if 0
1355 fprintf (stderr, "\n\t");
1356 print_node_brief (stderr, "<", TREE_PURPOSE (t), 0);
1357 if (TREE_VALUE (t))
1358 print_node_brief (stderr, " ", TREE_VALUE (t), 0);
1359 else
1360 fprintf (stderr, " (none)");
1361 fprintf (stderr, ">");
1362 #else
1363 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1364 #endif
1365 }
1366 fprintf (stderr, "\n");
1367 }
1368 }
1369
1370 void
1371 print_other_binding_stack (stack)
1372 struct binding_level *stack;
1373 {
1374 struct binding_level *level;
1375 for (level = stack; level != global_binding_level; level = level->level_chain)
1376 {
1377 fprintf (stderr, "binding level ");
1378 fprintf (stderr, HOST_PTR_PRINTF, level);
1379 fprintf (stderr, "\n");
1380 print_binding_level (level);
1381 }
1382 }
1383
1384 void
1385 print_binding_stack ()
1386 {
1387 struct binding_level *b;
1388 fprintf (stderr, "current_binding_level=");
1389 fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
1390 fprintf (stderr, "\nclass_binding_level=");
1391 fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
1392 fprintf (stderr, "\nglobal_binding_level=");
1393 fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
1394 fprintf (stderr, "\n");
1395 if (class_binding_level)
1396 {
1397 for (b = class_binding_level; b; b = b->level_chain)
1398 if (b == current_binding_level)
1399 break;
1400 if (b)
1401 b = class_binding_level;
1402 else
1403 b = current_binding_level;
1404 }
1405 else
1406 b = current_binding_level;
1407 print_other_binding_stack (b);
1408 fprintf (stderr, "global:\n");
1409 print_binding_level (global_binding_level);
1410 }
1411 \f
1412 /* Subroutines for reverting temporarily to top-level for instantiation
1413 of templates and such. We actually need to clear out the class- and
1414 local-value slots of all identifiers, so that only the global values
1415 are at all visible. Simply setting current_binding_level to the global
1416 scope isn't enough, because more binding levels may be pushed. */
1417 struct saved_scope {
1418 struct binding_level *old_binding_level;
1419 tree old_bindings;
1420 struct saved_scope *prev;
1421 tree class_name, class_type, class_decl, function_decl;
1422 struct binding_level *class_bindings;
1423 tree previous_class_type;
1424 tree *lang_base, *lang_stack, lang_name;
1425 int lang_stacksize;
1426 tree named_labels;
1427 };
1428 static struct saved_scope *current_saved_scope;
1429 extern tree prev_class_type;
1430
1431 void
1432 push_to_top_level ()
1433 {
1434 extern int current_lang_stacksize;
1435 struct saved_scope *s =
1436 (struct saved_scope *) xmalloc (sizeof (struct saved_scope));
1437 struct binding_level *b = current_binding_level;
1438 tree old_bindings = NULL_TREE;
1439
1440 /* Have to include global_binding_level, because class-level decls
1441 aren't listed anywhere useful. */
1442 for (; b; b = b->level_chain)
1443 {
1444 tree t;
1445
1446 if (b == global_binding_level)
1447 continue;
1448
1449 for (t = b->names; t; t = TREE_CHAIN (t))
1450 {
1451 tree binding, t1, t2 = t;
1452 tree id = DECL_ASSEMBLER_NAME (t2);
1453
1454 if (!id
1455 || (!IDENTIFIER_LOCAL_VALUE (id)
1456 && !IDENTIFIER_CLASS_VALUE (id)))
1457 continue;
1458
1459 for (t1 = old_bindings; t1; t1 = TREE_CHAIN (t1))
1460 if (TREE_VEC_ELT (t1, 0) == id)
1461 goto skip_it;
1462
1463 binding = make_tree_vec (4);
1464 if (id)
1465 {
1466 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
1467 TREE_VEC_ELT (binding, 0) = id;
1468 TREE_VEC_ELT (binding, 1) = IDENTIFIER_TYPE_VALUE (id);
1469 TREE_VEC_ELT (binding, 2) = IDENTIFIER_LOCAL_VALUE (id);
1470 TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
1471 IDENTIFIER_LOCAL_VALUE (id) = NULL_TREE;
1472 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
1473 }
1474 TREE_CHAIN (binding) = old_bindings;
1475 old_bindings = binding;
1476 skip_it:
1477 ;
1478 }
1479 /* Unwind type-value slots back to top level. */
1480 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
1481 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
1482 }
1483 /* Clear out class-level bindings cache. */
1484 if (current_binding_level == global_binding_level
1485 && previous_class_type != NULL_TREE)
1486 {
1487 popclass (-1);
1488 previous_class_type = NULL_TREE;
1489 }
1490
1491 s->old_binding_level = current_binding_level;
1492 current_binding_level = global_binding_level;
1493
1494 s->class_name = current_class_name;
1495 s->class_type = current_class_type;
1496 s->class_decl = current_class_decl;
1497 s->function_decl = current_function_decl;
1498 s->class_bindings = class_binding_level;
1499 s->previous_class_type = previous_class_type;
1500 s->lang_stack = current_lang_stack;
1501 s->lang_base = current_lang_base;
1502 s->lang_stacksize = current_lang_stacksize;
1503 s->lang_name = current_lang_name;
1504 s->named_labels = named_labels;
1505 current_class_name = current_class_type = current_class_decl = NULL_TREE;
1506 current_function_decl = NULL_TREE;
1507 class_binding_level = (struct binding_level *)0;
1508 previous_class_type = NULL_TREE;
1509 current_lang_stacksize = 10;
1510 current_lang_stack = current_lang_base
1511 = (tree *) xmalloc (current_lang_stacksize * sizeof (tree));
1512 current_lang_name = lang_name_cplusplus;
1513 strict_prototype = strict_prototypes_lang_cplusplus;
1514 named_labels = NULL_TREE;
1515
1516 s->prev = current_saved_scope;
1517 s->old_bindings = old_bindings;
1518 current_saved_scope = s;
1519 }
1520
1521 void
1522 pop_from_top_level ()
1523 {
1524 extern int current_lang_stacksize;
1525 struct saved_scope *s = current_saved_scope;
1526 tree t;
1527
1528 if (previous_class_type)
1529 previous_class_type = NULL_TREE;
1530
1531 current_binding_level = s->old_binding_level;
1532 current_saved_scope = s->prev;
1533 for (t = s->old_bindings; t; t = TREE_CHAIN (t))
1534 {
1535 tree id = TREE_VEC_ELT (t, 0);
1536 if (id)
1537 {
1538 IDENTIFIER_TYPE_VALUE (id) = TREE_VEC_ELT (t, 1);
1539 IDENTIFIER_LOCAL_VALUE (id) = TREE_VEC_ELT (t, 2);
1540 IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
1541 }
1542 }
1543 current_class_name = s->class_name;
1544 current_class_type = s->class_type;
1545 current_class_decl = s->class_decl;
1546 if (current_class_type)
1547 C_C_D = CLASSTYPE_INST_VAR (current_class_type);
1548 else
1549 C_C_D = NULL_TREE;
1550 current_function_decl = s->function_decl;
1551 class_binding_level = s->class_bindings;
1552 previous_class_type = s->previous_class_type;
1553 free (current_lang_base);
1554 current_lang_base = s->lang_base;
1555 current_lang_stack = s->lang_stack;
1556 current_lang_name = s->lang_name;
1557 current_lang_stacksize = s->lang_stacksize;
1558 if (current_lang_name == lang_name_cplusplus)
1559 strict_prototype = strict_prototypes_lang_cplusplus;
1560 else if (current_lang_name == lang_name_c)
1561 strict_prototype = strict_prototypes_lang_c;
1562 named_labels = s->named_labels;
1563
1564 free (s);
1565 }
1566 \f
1567 /* Push a definition of struct, union or enum tag "name".
1568 into binding_level "b". "type" should be the type node,
1569 We assume that the tag "name" is not already defined.
1570
1571 Note that the definition may really be just a forward reference.
1572 In that case, the TYPE_SIZE will be a NULL_TREE.
1573
1574 C++ gratuitously puts all these tags in the name space. */
1575
1576 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
1577 record the shadowed value for this binding contour. TYPE is
1578 the type that ID maps to. */
1579
1580 static void
1581 set_identifier_type_value_with_scope (id, type, b)
1582 tree id;
1583 tree type;
1584 struct binding_level *b;
1585 {
1586 if (b != global_binding_level)
1587 {
1588 tree old_type_value = IDENTIFIER_TYPE_VALUE (id);
1589 b->type_shadowed
1590 = tree_cons (id, old_type_value, b->type_shadowed);
1591 }
1592 SET_IDENTIFIER_TYPE_VALUE (id, type);
1593 }
1594
1595 /* As set_identifier_type_value_with_scope, but using inner_binding_level. */
1596
1597 void
1598 set_identifier_type_value (id, type)
1599 tree id;
1600 tree type;
1601 {
1602 set_identifier_type_value_with_scope (id, type, inner_binding_level);
1603 }
1604
1605 /* Subroutine "set_nested_typename" builds the nested-typename of
1606 the type decl in question. (Argument CLASSNAME can actually be
1607 a function as well, if that's the smallest containing scope.) */
1608
1609 static void
1610 set_nested_typename (decl, classname, name, type)
1611 tree decl, classname, name, type;
1612 {
1613 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 136);
1614 if (classname != NULL_TREE)
1615 {
1616 char *buf;
1617 my_friendly_assert (TREE_CODE (classname) == IDENTIFIER_NODE, 137);
1618 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 138);
1619 buf = (char *) alloca (4 + IDENTIFIER_LENGTH (classname)
1620 + IDENTIFIER_LENGTH (name));
1621 sprintf (buf, "%s::%s", IDENTIFIER_POINTER (classname),
1622 IDENTIFIER_POINTER (name));
1623 DECL_NESTED_TYPENAME (decl) = get_identifier (buf);
1624 TREE_MANGLED (DECL_NESTED_TYPENAME (decl)) = 1;
1625
1626 /* This is a special usage of IDENTIFIER_TYPE_VALUE which have no
1627 correspondence in any binding_level. This is ok since the
1628 DECL_NESTED_TYPENAME is just a convenience identifier whose
1629 IDENTIFIER_TYPE_VALUE will remain constant from now on. */
1630 SET_IDENTIFIER_TYPE_VALUE (DECL_NESTED_TYPENAME (decl), type);
1631 }
1632 else
1633 DECL_NESTED_TYPENAME (decl) = name;
1634 }
1635
1636 /* Pop off extraneous binding levels left over due to syntax errors. */
1637 void
1638 pop_everything ()
1639 {
1640 #ifdef DEBUG_CP_BINDING_LEVELS
1641 fprintf (stderr, "XXX entering pop_everything ()\n");
1642 #endif
1643 while (current_binding_level != global_binding_level
1644 && ! current_binding_level->pseudo_global)
1645 {
1646 if (class_binding_level)
1647 pop_nested_class (1);
1648 else
1649 poplevel (0, 0, 0);
1650 }
1651 #ifdef DEBUG_CP_BINDING_LEVELS
1652 fprintf (stderr, "XXX leaving pop_everything ()\n");
1653 #endif
1654 }
1655
1656 #if 0 /* not yet, should get fixed properly later */
1657 /* Create a TYPE_DECL node with the correct DECL_ASSEMBLER_NAME.
1658 Other routines shouldn't use build_decl directly; they'll produce
1659 incorrect results with `-g' unless they duplicate this code.
1660
1661 This is currently needed mainly for dbxout.c, but we can make
1662 use of it in method.c later as well. */
1663 tree
1664 make_type_decl (name, type)
1665 tree name, type;
1666 {
1667 tree decl, id;
1668 decl = build_decl (TYPE_DECL, name, type);
1669 if (TYPE_NAME (type) == name)
1670 /* Class/union/enum definition, or a redundant typedef for same. */
1671 {
1672 id = get_identifier (build_overload_name (type, 1, 1));
1673 DECL_ASSEMBLER_NAME (decl) = id;
1674 }
1675 else if (TYPE_NAME (type) != NULL_TREE)
1676 /* Explicit typedef, or implicit typedef for template expansion. */
1677 DECL_ASSEMBLER_NAME (decl) = DECL_ASSEMBLER_NAME (TYPE_NAME (type));
1678 else
1679 {
1680 /* XXX: Typedef for unnamed struct; some other situations.
1681 TYPE_NAME is null; what's right here? */
1682 }
1683 return decl;
1684 }
1685 #endif
1686
1687 /* Push a tag name NAME for struct/class/union/enum type TYPE.
1688 Normally put into into the inner-most non-tag-tranparent scope,
1689 but if GLOBALIZE is true, put it in the inner-most non-class scope.
1690 The latter is needed for implicit declarations. */
1691
1692 void
1693 pushtag (name, type, globalize)
1694 tree name, type;
1695 int globalize;
1696 {
1697 register struct binding_level *b;
1698 tree context = 0;
1699 tree c_decl = 0;
1700
1701 b = inner_binding_level;
1702 while (b->tag_transparent
1703 || (globalize && b->parm_flag == 2))
1704 b = b->level_chain;
1705
1706 if (b == global_binding_level)
1707 b->tags = perm_tree_cons (name, type, b->tags);
1708 else
1709 b->tags = saveable_tree_cons (name, type, b->tags);
1710
1711 if (name)
1712 {
1713 context = type ? TYPE_CONTEXT (type) : NULL_TREE;
1714 if (! context && ! globalize)
1715 context = current_scope ();
1716 if (context)
1717 c_decl = TREE_CODE (context) == FUNCTION_DECL
1718 ? context : TYPE_NAME (context);
1719
1720 /* Record the identifier as the type's name if it has none. */
1721 if (TYPE_NAME (type) == NULL_TREE)
1722 TYPE_NAME (type) = name;
1723
1724 /* Do C++ gratuitous typedefing. */
1725 if (IDENTIFIER_TYPE_VALUE (name) != type
1726 && (TREE_CODE (type) != RECORD_TYPE
1727 || b->parm_flag != 2
1728 || !CLASSTYPE_DECLARED_EXCEPTION (type)))
1729 {
1730 register tree d;
1731 int newdecl = 0;
1732
1733 if (b->parm_flag != 2
1734 || TYPE_SIZE (current_class_type) != NULL_TREE)
1735 {
1736 d = lookup_nested_type (type, c_decl);
1737
1738 if (d == NULL_TREE)
1739 {
1740 newdecl = 1;
1741 #if 0 /* not yet, should get fixed properly later */
1742 d = make_type_decl (name, type);
1743 #else
1744 d = build_decl (TYPE_DECL, name, type);
1745 #endif
1746 #ifdef DWARF_DEBUGGING_INFO
1747 if (write_symbols == DWARF_DEBUG)
1748 {
1749 /* Mark the TYPE_DECL node we created just above as an
1750 gratuitous one. We need to do this so that dwarfout.c
1751 will understand that it is not supposed to output a
1752 TAG_typedef DIE for it. */
1753 DECL_IGNORED_P (d) = 1;
1754 }
1755 #endif /* DWARF_DEBUGGING_INFO */
1756 set_identifier_type_value_with_scope (name, type, b);
1757 }
1758 else
1759 d = TYPE_NAME (d);
1760
1761 /* If it is anonymous, then we are called from pushdecl,
1762 and we don't want to infinitely recurse. Also, if the
1763 name is already in scope, we don't want to push it
1764 again--pushdecl is only for pushing new decls. */
1765 if (! ANON_AGGRNAME_P (name)
1766 && TYPE_NAME (type)
1767 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
1768 || lookup_name (name, 1) != TYPE_NAME (type)))
1769 {
1770 if (b->parm_flag == 2)
1771 d = pushdecl_class_level (d);
1772 else
1773 d = pushdecl_with_scope (d, b);
1774 }
1775 }
1776 else
1777 {
1778 /* Make nested declarations go into class-level scope. */
1779 newdecl = 1;
1780 d = build_decl (TYPE_DECL, name, type);
1781 #ifdef DWARF_DEBUGGING_INFO
1782 if (write_symbols == DWARF_DEBUG)
1783 {
1784 /* Mark the TYPE_DECL node we created just above as an
1785 gratuitous one. We need to do this so that dwarfout.c
1786 will understand that it is not supposed to output a
1787 TAG_typedef DIE for it. */
1788 DECL_IGNORED_P (d) = 1;
1789 }
1790 #endif /* DWARF_DEBUGGING_INFO */
1791 /* Make sure we're in this type's scope when we push the
1792 decl for a template, otherwise class_binding_level will
1793 be NULL and we'll end up dying inside of
1794 push_class_level_binding. */
1795 if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
1796 pushclass (type, 0);
1797 d = pushdecl_class_level (d);
1798 if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
1799 popclass (0);
1800 }
1801 if (write_symbols != DWARF_DEBUG)
1802 {
1803 if (ANON_AGGRNAME_P (name))
1804 DECL_IGNORED_P (d) = 1;
1805 }
1806 TYPE_NAME (type) = d;
1807
1808 if (context == NULL_TREE)
1809 /* Non-nested class. */
1810 DECL_NESTED_TYPENAME (d) = name;
1811 else if (context && TREE_CODE (context) == FUNCTION_DECL)
1812 {
1813 /* Function-nested class. */
1814 set_nested_typename (d, DECL_ASSEMBLER_NAME (c_decl),
1815 name, type);
1816 /* This builds the links for classes nested in fn scope. */
1817 DECL_CONTEXT (d) = context;
1818 }
1819 /* else if (TYPE_SIZE (current_class_type) == NULL_TREE)
1820 */
1821 else if (context && IS_AGGR_TYPE (context))
1822 {
1823 /* Class-nested class. */
1824 set_nested_typename (d, DECL_NESTED_TYPENAME (c_decl),
1825 name, type);
1826 /* This builds the links for classes nested in type scope. */
1827 DECL_CONTEXT (d) = context;
1828 }
1829 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
1830 if (newdecl)
1831 DECL_ASSEMBLER_NAME (d)
1832 = get_identifier (build_overload_name (type, 1, 1));
1833 }
1834 if (b->parm_flag == 2)
1835 {
1836 TREE_NONLOCAL_FLAG (type) = 1;
1837 if (TYPE_SIZE (current_class_type) == NULL_TREE)
1838 CLASSTYPE_TAGS (current_class_type) = b->tags;
1839 }
1840 }
1841
1842 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
1843 /* Use the canonical TYPE_DECL for this node. */
1844 TYPE_STUB_DECL (type) = TYPE_NAME (type);
1845 else
1846 {
1847 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
1848 will be the tagged type we just added to the current
1849 binding level. This fake NULL-named TYPE_DECL node helps
1850 dwarfout.c to know when it needs to output a
1851 representation of a tagged type, and it also gives us a
1852 convenient place to record the "scope start" address for
1853 the tagged type. */
1854
1855 #if 0 /* not yet, should get fixed properly later */
1856 tree d = make_type_decl (NULL_TREE, type);
1857 #else
1858 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
1859 #endif
1860 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
1861 }
1862 }
1863
1864 /* Counter used to create anonymous type names. */
1865 static int anon_cnt = 0;
1866
1867 /* Return an IDENTIFIER which can be used as a name for
1868 anonymous structs and unions. */
1869 tree
1870 make_anon_name ()
1871 {
1872 char buf[32];
1873
1874 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1875 return get_identifier (buf);
1876 }
1877
1878 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
1879 This keeps dbxout from getting confused. */
1880 void
1881 clear_anon_tags ()
1882 {
1883 register struct binding_level *b;
1884 register tree tags;
1885 static int last_cnt = 0;
1886
1887 /* Fast out if no new anon names were declared. */
1888 if (last_cnt == anon_cnt)
1889 return;
1890
1891 b = current_binding_level;
1892 while (b->tag_transparent)
1893 b = b->level_chain;
1894 tags = b->tags;
1895 while (tags)
1896 {
1897 /* A NULL purpose means we have already processed all tags
1898 from here to the end of the list. */
1899 if (TREE_PURPOSE (tags) == NULL_TREE)
1900 break;
1901 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
1902 TREE_PURPOSE (tags) = NULL_TREE;
1903 tags = TREE_CHAIN (tags);
1904 }
1905 last_cnt = anon_cnt;
1906 }
1907 \f
1908 /* Subroutine of duplicate_decls: return truthvalue of whether
1909 or not types of these decls match.
1910
1911 For C++, we must compare the parameter list so that `int' can match
1912 `int&' in a parameter position, but `int&' is not confused with
1913 `const int&'. */
1914 static int
1915 decls_match (newdecl, olddecl)
1916 tree newdecl, olddecl;
1917 {
1918 int types_match;
1919
1920 if (TREE_CODE (newdecl) == FUNCTION_DECL
1921 && TREE_CODE (olddecl) == FUNCTION_DECL)
1922 {
1923 tree f1 = TREE_TYPE (newdecl);
1924 tree f2 = TREE_TYPE (olddecl);
1925 tree p1 = TYPE_ARG_TYPES (f1);
1926 tree p2 = TYPE_ARG_TYPES (f2);
1927
1928 /* When we parse a static member function definition,
1929 we put together a FUNCTION_DECL which thinks its type
1930 is METHOD_TYPE. Change that to FUNCTION_TYPE, and
1931 proceed. */
1932 if (TREE_CODE (f1) == METHOD_TYPE && DECL_STATIC_FUNCTION_P (olddecl))
1933 revert_static_member_fn (&newdecl, &f1, &p1);
1934 else if (TREE_CODE (f2) == METHOD_TYPE
1935 && DECL_STATIC_FUNCTION_P (newdecl))
1936 revert_static_member_fn (&olddecl, &f2, &p2);
1937
1938 /* Here we must take care of the case where new default
1939 parameters are specified. Also, warn if an old
1940 declaration becomes ambiguous because default
1941 parameters may cause the two to be ambiguous. */
1942 if (TREE_CODE (f1) != TREE_CODE (f2))
1943 {
1944 if (TREE_CODE (f1) == OFFSET_TYPE)
1945 cp_compiler_error ("`%D' redeclared as member function", newdecl);
1946 else
1947 cp_compiler_error ("`%D' redeclared as non-member function", newdecl);
1948 return 0;
1949 }
1950
1951 if (comptypes (TREE_TYPE (f1), TREE_TYPE (f2), 1))
1952 {
1953 if (! strict_prototypes_lang_c && DECL_LANGUAGE (olddecl) == lang_c
1954 && p2 == NULL_TREE)
1955 {
1956 types_match = self_promoting_args_p (p1);
1957 if (p1 == void_list_node)
1958 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1959 }
1960 else if (!strict_prototypes_lang_c && DECL_LANGUAGE (olddecl)==lang_c
1961 && DECL_LANGUAGE (newdecl) == lang_c && p1 == NULL_TREE)
1962 {
1963 types_match = self_promoting_args_p (p2);
1964 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1965 }
1966 else
1967 types_match = compparms (p1, p2, 3);
1968 }
1969 else
1970 types_match = 0;
1971 }
1972 else if (TREE_CODE (newdecl) == TEMPLATE_DECL
1973 && TREE_CODE (olddecl) == TEMPLATE_DECL)
1974 {
1975 tree newargs = DECL_TEMPLATE_PARMS (newdecl);
1976 tree oldargs = DECL_TEMPLATE_PARMS (olddecl);
1977 int i, len = TREE_VEC_LENGTH (newargs);
1978
1979 if (TREE_VEC_LENGTH (oldargs) != len)
1980 return 0;
1981
1982 for (i = 0; i < len; i++)
1983 {
1984 tree newarg = TREE_VEC_ELT (newargs, i);
1985 tree oldarg = TREE_VEC_ELT (oldargs, i);
1986 if (TREE_CODE (newarg) != TREE_CODE (oldarg))
1987 return 0;
1988 else if (TREE_CODE (newarg) == IDENTIFIER_NODE)
1989 /* continue */;
1990 else if (! comptypes (TREE_TYPE (newarg), TREE_TYPE (oldarg), 1))
1991 return 0;
1992 }
1993
1994 if (DECL_TEMPLATE_IS_CLASS (newdecl)
1995 != DECL_TEMPLATE_IS_CLASS (olddecl))
1996 types_match = 0;
1997 else if (DECL_TEMPLATE_IS_CLASS (newdecl))
1998 types_match = 1;
1999 else
2000 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2001 DECL_TEMPLATE_RESULT (newdecl));
2002 }
2003 else
2004 {
2005 if (TREE_TYPE (newdecl) == error_mark_node)
2006 types_match = TREE_TYPE (olddecl) == error_mark_node;
2007 else if (TREE_TYPE (olddecl) == NULL_TREE)
2008 types_match = TREE_TYPE (newdecl) == NULL_TREE;
2009 else if (TREE_TYPE (newdecl) == NULL_TREE)
2010 types_match = 0;
2011 else
2012 types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl), 1);
2013 }
2014
2015 return types_match;
2016 }
2017
2018 /* If NEWDECL is `static' and an `extern' was seen previously,
2019 warn about it. (OLDDECL may be NULL_TREE; NAME contains
2020 information about previous usage as an `extern'.)
2021
2022 Note that this does not apply to the C++ case of declaring
2023 a variable `extern const' and then later `const'.
2024
2025 Don't complain if -traditional, since traditional compilers
2026 don't complain.
2027
2028 Don't complain about built-in functions, since they are beyond
2029 the user's control. */
2030
2031 static void
2032 warn_extern_redeclared_static (newdecl, olddecl)
2033 tree newdecl, olddecl;
2034 {
2035 tree name;
2036
2037 static char *explicit_extern_static_warning
2038 = "`%D' was declared `extern' and later `static'";
2039 static char *implicit_extern_static_warning
2040 = "`%D' was declared implicitly `extern' and later `static'";
2041
2042 if (flag_traditional
2043 || TREE_CODE (newdecl) == TYPE_DECL
2044 || (! warn_extern_inline
2045 && DECL_INLINE (newdecl)))
2046 return;
2047
2048 name = DECL_ASSEMBLER_NAME (newdecl);
2049 if (TREE_PUBLIC (name) && ! TREE_PUBLIC (newdecl))
2050 {
2051 /* It's okay to redeclare an ANSI built-in function as static,
2052 or to declare a non-ANSI built-in function as anything. */
2053 if (! (TREE_CODE (newdecl) == FUNCTION_DECL
2054 && olddecl != NULL_TREE
2055 && TREE_CODE (olddecl) == FUNCTION_DECL
2056 && (DECL_BUILT_IN (olddecl)
2057 || DECL_BUILT_IN_NONANSI (olddecl))))
2058 {
2059 cp_warning (IDENTIFIER_IMPLICIT_DECL (name)
2060 ? implicit_extern_static_warning
2061 : explicit_extern_static_warning, newdecl);
2062 if (olddecl != NULL_TREE)
2063 cp_warning_at ("previous declaration of `%D'", olddecl);
2064 }
2065 }
2066 }
2067
2068 /* Handle when a new declaration NEWDECL has the same name as an old
2069 one OLDDECL in the same binding contour. Prints an error message
2070 if appropriate.
2071
2072 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2073 Otherwise, return 0. */
2074
2075 int
2076 duplicate_decls (newdecl, olddecl)
2077 register tree newdecl, olddecl;
2078 {
2079 extern struct obstack permanent_obstack;
2080 unsigned olddecl_uid = DECL_UID (olddecl);
2081 int olddecl_friend = 0, types_match = 0;
2082 int new_defines_function;
2083 tree previous_c_decl = NULL_TREE;
2084
2085 types_match = decls_match (newdecl, olddecl);
2086
2087 if (TREE_CODE (olddecl) != TREE_LIST)
2088 olddecl_friend = DECL_LANG_SPECIFIC (olddecl) && DECL_FRIEND_P (olddecl);
2089
2090 /* If either the type of the new decl or the type of the old decl is an
2091 error_mark_node, then that implies that we have already issued an
2092 error (earlier) for some bogus type specification, and in that case,
2093 it is rather pointless to harass the user with yet more error message
2094 about the same declaration, so well just pretent the types match here. */
2095 if ((TREE_TYPE (newdecl)
2096 && TREE_CODE (TREE_TYPE (newdecl)) == ERROR_MARK)
2097 || (TREE_TYPE (olddecl)
2098 && TREE_CODE (TREE_TYPE (olddecl)) == ERROR_MARK))
2099 types_match = 1;
2100
2101 if (flag_traditional && TREE_CODE (newdecl) == FUNCTION_DECL
2102 && IDENTIFIER_IMPLICIT_DECL (DECL_ASSEMBLER_NAME (newdecl)) == olddecl)
2103 /* If -traditional, avoid error for redeclaring fcn
2104 after implicit decl. */
2105 ;
2106 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2107 && (DECL_BUILT_IN (olddecl) || DECL_BUILT_IN_NONANSI (olddecl)))
2108 {
2109 /* If you declare a built-in or predefined function name as static,
2110 the old definition is overridden, but optionally warn this was a
2111 bad choice of name. Ditto for overloads. */
2112 if (! TREE_PUBLIC (newdecl)
2113 || (TREE_CODE (newdecl) == FUNCTION_DECL
2114 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)))
2115 {
2116 if (warn_shadow)
2117 cp_warning ("shadowing %s function `%#D'",
2118 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2119 olddecl);
2120 /* Discard the old built-in function. */
2121 return 0;
2122 }
2123 else if (! types_match)
2124 {
2125 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2126 {
2127 /* If the built-in is not ansi, then programs can override
2128 it even globally without an error. */
2129 if (! DECL_BUILT_IN (olddecl))
2130 cp_warning ("library function `%#D' redeclared as non-function `%#D'",
2131 olddecl, newdecl);
2132 else
2133 {
2134 cp_error ("declaration of `%#D'", newdecl);
2135 cp_error ("conflicts with built-in declaration `%#D'",
2136 olddecl);
2137 }
2138 return 0;
2139 }
2140
2141 cp_warning ("declaration of `%#D'", newdecl);
2142 cp_warning ("conflicts with built-in declaration `%#D'",
2143 olddecl);
2144 }
2145 }
2146 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2147 {
2148 if ((TREE_CODE (newdecl) == FUNCTION_DECL
2149 && TREE_CODE (olddecl) == TEMPLATE_DECL
2150 && ! DECL_TEMPLATE_IS_CLASS (olddecl))
2151 || (TREE_CODE (olddecl) == FUNCTION_DECL
2152 && TREE_CODE (newdecl) == TEMPLATE_DECL
2153 && ! DECL_TEMPLATE_IS_CLASS (newdecl)))
2154 return 0;
2155
2156 cp_error ("`%#D' redeclared as different kind of symbol", newdecl);
2157 if (TREE_CODE (olddecl) == TREE_LIST)
2158 olddecl = TREE_VALUE (olddecl);
2159 cp_error_at ("previous declaration of `%#D'", olddecl);
2160
2161 /* New decl is completely inconsistent with the old one =>
2162 tell caller to replace the old one. */
2163
2164 return 0;
2165 }
2166 else if (!types_match)
2167 {
2168 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2169 {
2170 /* The name of a class template may not be declared to refer to
2171 any other template, class, function, object, namespace, value,
2172 or type in the same scope. */
2173 if (DECL_TEMPLATE_IS_CLASS (olddecl)
2174 || DECL_TEMPLATE_IS_CLASS (newdecl))
2175 {
2176 cp_error ("declaration of template `%#D'", newdecl);
2177 cp_error_at ("conflicts with previous declaration `%#D'",
2178 olddecl);
2179 }
2180 return 0;
2181 }
2182 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2183 {
2184 if (DECL_LANGUAGE (newdecl) == lang_c
2185 && DECL_LANGUAGE (olddecl) == lang_c)
2186 {
2187 cp_error ("declaration of C function `%#D' conflicts with",
2188 newdecl);
2189 cp_error_at ("previous declaration `%#D' here", olddecl);
2190 }
2191
2192 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2193 TYPE_ARG_TYPES (TREE_TYPE (olddecl)), 2))
2194 {
2195 cp_error ("new declaration `%#D'", newdecl);
2196 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
2197 }
2198 else
2199 return 0;
2200 }
2201
2202 /* Already complained about this, so don't do so again. */
2203 else if (current_class_type == NULL_TREE
2204 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
2205 {
2206 /* Since we're doing this before finish_struct can set the
2207 line number on NEWDECL, we just do a regular error here. */
2208 if (DECL_SOURCE_LINE (newdecl) == 0)
2209 cp_error ("conflicting types for `%#D'", newdecl);
2210 else
2211 cp_error_at ("conflicting types for `%#D'", newdecl);
2212 cp_error_at ("previous declaration as `%#D'", olddecl);
2213 }
2214 }
2215 else
2216 {
2217 char *errmsg = redeclaration_error_message (newdecl, olddecl);
2218 if (errmsg)
2219 {
2220 cp_error (errmsg, newdecl);
2221 if (DECL_NAME (olddecl) != NULL_TREE)
2222 cp_error_at ((DECL_INITIAL (olddecl)
2223 && current_binding_level == global_binding_level)
2224 ? "`%#D' previously defined here"
2225 : "`%#D' previously declared here", olddecl);
2226 }
2227 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2228 && DECL_INITIAL (olddecl) != NULL_TREE
2229 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
2230 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
2231 {
2232 /* Prototype decl follows defn w/o prototype. */
2233 cp_warning_at ("prototype for `%#D'", newdecl);
2234 cp_warning_at ("follows non-prototype definition here", olddecl);
2235 }
2236 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2237 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2238 {
2239 /* extern "C" int foo ();
2240 int foo () { bar (); }
2241 is OK. */
2242 if (current_lang_stack == current_lang_base)
2243 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
2244 else
2245 {
2246 cp_error_at ("previous declaration of `%#D' with %L linkage",
2247 olddecl, DECL_LANGUAGE (olddecl));
2248 cp_error ("conflicts with new declaration with %L linkage",
2249 DECL_LANGUAGE (newdecl));
2250 }
2251 }
2252
2253 /* These bits are logically part of the type. */
2254 if (pedantic
2255 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
2256 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
2257 cp_error_at ("type qualifiers for `%D' conflict with previous decl",
2258 newdecl);
2259 }
2260
2261 /* If new decl is `static' and an `extern' was seen previously,
2262 warn about it. */
2263 warn_extern_redeclared_static (newdecl, olddecl);
2264
2265 /* We have committed to returning 1 at this point. */
2266 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2267 {
2268 /* Now that functions must hold information normally held
2269 by field decls, there is extra work to do so that
2270 declaration information does not get destroyed during
2271 definition. */
2272 if (DECL_VINDEX (olddecl))
2273 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2274 if (DECL_CONTEXT (olddecl))
2275 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2276 if (DECL_CLASS_CONTEXT (olddecl))
2277 DECL_CLASS_CONTEXT (newdecl) = DECL_CLASS_CONTEXT (olddecl);
2278 if (DECL_CHAIN (newdecl) == NULL_TREE)
2279 DECL_CHAIN (newdecl) = DECL_CHAIN (olddecl);
2280 if (DECL_NEXT_METHOD (newdecl) == NULL_TREE)
2281 DECL_NEXT_METHOD (newdecl) = DECL_NEXT_METHOD (olddecl);
2282 if (DECL_PENDING_INLINE_INFO (newdecl) == (struct pending_inline *)0)
2283 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
2284 }
2285
2286 /* Deal with C++: must preserve virtual function table size. */
2287 if (TREE_CODE (olddecl) == TYPE_DECL)
2288 {
2289 register tree newtype = TREE_TYPE (newdecl);
2290 register tree oldtype = TREE_TYPE (olddecl);
2291
2292 if (newtype != error_mark_node && oldtype != error_mark_node
2293 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2294 {
2295 CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
2296 CLASSTYPE_FRIEND_CLASSES (newtype)
2297 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2298 }
2299 #if 0
2300 /* why assert here? Just because debugging information is
2301 messed up? (mrs) */
2302 /* it happens on something like:
2303 typedef struct Thing {
2304 Thing();
2305 int x;
2306 } Thing;
2307 */
2308 my_friendly_assert (DECL_IGNORED_P (olddecl) == DECL_IGNORED_P (newdecl),
2309 139);
2310 #endif
2311 }
2312
2313 /* Special handling ensues if new decl is a function definition. */
2314 new_defines_function = (TREE_CODE (newdecl) == FUNCTION_DECL
2315 && DECL_INITIAL (newdecl) != NULL_TREE);
2316
2317 /* Optionally warn about more than one declaration for the same name,
2318 but don't warn about a function declaration followed by a definition. */
2319 if (warn_redundant_decls
2320 && ! DECL_ARTIFICIAL (olddecl)
2321 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2322 /* Don't warn about extern decl followed by (tentative) definition. */
2323 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
2324 {
2325 cp_warning ("redundant redeclaration of `%D' in same scope", newdecl);
2326 cp_warning ("previous declaration of `%D'", olddecl);
2327 }
2328
2329 /* Copy all the DECL_... slots specified in the new decl
2330 except for any that we copy here from the old type. */
2331
2332 if (types_match)
2333 {
2334 /* Automatically handles default parameters. */
2335 tree oldtype = TREE_TYPE (olddecl);
2336 /* Merge the data types specified in the two decls. */
2337 tree newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2338
2339 /* Make sure we put the new type in the same obstack as the old ones.
2340 If the old types are not both in the same obstack, use the permanent
2341 one. */
2342 if (oldtype && TYPE_OBSTACK (oldtype) == TYPE_OBSTACK (newtype))
2343 push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
2344 else
2345 {
2346 push_obstacks_nochange ();
2347 end_temporary_allocation ();
2348 }
2349
2350 if (TREE_CODE (newdecl) == VAR_DECL)
2351 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2352 /* Do this after calling `common_type' so that default
2353 parameters don't confuse us. */
2354 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2355 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
2356 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
2357 {
2358 tree ctype = NULL_TREE;
2359 ctype = DECL_CLASS_CONTEXT (newdecl);
2360 TREE_TYPE (newdecl) = build_exception_variant (ctype, newtype,
2361 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
2362 TREE_TYPE (olddecl) = build_exception_variant (ctype, newtype,
2363 TYPE_RAISES_EXCEPTIONS (oldtype));
2364
2365 if (! compexcepttypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl), 0))
2366 {
2367 cp_error ("declaration of `%D' raises different exceptions...",
2368 newdecl);
2369 cp_error_at ("...from previous declaration here", olddecl);
2370 }
2371 }
2372 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2373
2374 /* Lay the type out, unless already done. */
2375 if (oldtype != TREE_TYPE (newdecl))
2376 {
2377 if (TREE_TYPE (newdecl) != error_mark_node)
2378 layout_type (TREE_TYPE (newdecl));
2379 if (TREE_CODE (newdecl) != FUNCTION_DECL
2380 && TREE_CODE (newdecl) != TYPE_DECL
2381 && TREE_CODE (newdecl) != CONST_DECL
2382 && TREE_CODE (newdecl) != TEMPLATE_DECL)
2383 layout_decl (newdecl, 0);
2384 }
2385 else
2386 {
2387 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2388 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2389 }
2390
2391 /* Merge the type qualifiers. */
2392 if (TREE_READONLY (newdecl))
2393 TREE_READONLY (olddecl) = 1;
2394 if (TREE_THIS_VOLATILE (newdecl))
2395 TREE_THIS_VOLATILE (olddecl) = 1;
2396
2397 /* Merge the initialization information. */
2398 if (DECL_INITIAL (newdecl) == NULL_TREE
2399 && DECL_INITIAL (olddecl) != NULL_TREE)
2400 {
2401 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2402 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
2403 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
2404 }
2405
2406 /* Merge the section attribute.
2407 We want to issue an error if the sections conflict but that must be
2408 done later in decl_attributes since we are called before attributes
2409 are assigned. */
2410 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
2411 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
2412
2413 /* Keep the old rtl since we can safely use it, unless it's the
2414 call to abort() used for abstract virtuals. */
2415 if ((DECL_LANG_SPECIFIC (olddecl)
2416 && !DECL_ABSTRACT_VIRTUAL_P (olddecl))
2417 || DECL_RTL (olddecl) != DECL_RTL (abort_fndecl))
2418 DECL_RTL (newdecl) = DECL_RTL (olddecl);
2419
2420 pop_obstacks ();
2421 }
2422 /* If cannot merge, then use the new type and qualifiers,
2423 and don't preserve the old rtl. */
2424 else
2425 {
2426 /* Clean out any memory we had of the old declaration. */
2427 tree oldstatic = value_member (olddecl, static_aggregates);
2428 if (oldstatic)
2429 TREE_VALUE (oldstatic) = error_mark_node;
2430
2431 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2432 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2433 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2434 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2435 }
2436
2437 /* Merge the storage class information. */
2438 if (DECL_EXTERNAL (newdecl))
2439 {
2440 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2441 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2442
2443 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2444 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2445 }
2446 else
2447 {
2448 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2449 /* A `const' which was not declared `extern' and is
2450 in static storage is invisible. */
2451 if (TREE_CODE (newdecl) == VAR_DECL
2452 && TREE_READONLY (newdecl) && TREE_STATIC (newdecl)
2453 && ! DECL_THIS_EXTERN (newdecl))
2454 TREE_PUBLIC (newdecl) = 0;
2455 else if (TREE_CODE (newdecl) != FUNCTION_DECL)
2456 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2457 }
2458
2459 /* For functions, static overrides non-static. */
2460 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2461 {
2462 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2463 /* This is since we don't automatically
2464 copy the attributes of NEWDECL into OLDDECL. */
2465 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2466 /* If this clears `static', clear it in the identifier too. */
2467 if (! TREE_PUBLIC (olddecl))
2468 TREE_PUBLIC (DECL_ASSEMBLER_NAME (olddecl)) = 0;
2469 }
2470
2471 /* If either decl says `inline', this fn is inline,
2472 unless its definition was passed already. */
2473 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
2474 DECL_INLINE (olddecl) = 1;
2475 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
2476
2477 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2478 {
2479 if (! types_match)
2480 {
2481 DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
2482 DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
2483 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2484 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2485 DECL_RTL (olddecl) = DECL_RTL (newdecl);
2486 }
2487 if (new_defines_function)
2488 /* If defining a function declared with other language
2489 linkage, use the previously declared language linkage. */
2490 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
2491 else
2492 {
2493 /* If redeclaring a builtin function, and not a definition,
2494 it stays built in. */
2495 if (DECL_BUILT_IN (olddecl))
2496 {
2497 DECL_BUILT_IN (newdecl) = 1;
2498 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2499 /* If we're keeping the built-in definition, keep the rtl,
2500 regardless of declaration matches. */
2501 DECL_RTL (newdecl) = DECL_RTL (olddecl);
2502 }
2503 else
2504 DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
2505
2506 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2507 if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl)))
2508 /* Previously saved insns go together with
2509 the function's previous definition. */
2510 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2511 /* Don't clear out the arguments if we're redefining a function. */
2512 if (DECL_ARGUMENTS (olddecl))
2513 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2514 }
2515 }
2516
2517 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2518 {
2519 if (DECL_TEMPLATE_INFO (olddecl)->length)
2520 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2521 DECL_TEMPLATE_MEMBERS (newdecl) = DECL_TEMPLATE_MEMBERS (olddecl);
2522 DECL_TEMPLATE_INSTANTIATIONS (newdecl)
2523 = DECL_TEMPLATE_INSTANTIATIONS (olddecl);
2524 }
2525
2526 /* Now preserve various other info from the definition. */
2527 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2528 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2529 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2530
2531 /* Don't really know how much of the language-specific
2532 values we should copy from old to new. */
2533 if (DECL_LANG_SPECIFIC (olddecl))
2534 {
2535 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2536 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2537 }
2538
2539 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2540 {
2541 int function_size;
2542 struct lang_decl *ol = DECL_LANG_SPECIFIC (olddecl);
2543 struct lang_decl *nl = DECL_LANG_SPECIFIC (newdecl);
2544
2545 function_size = sizeof (struct tree_decl);
2546
2547 bcopy ((char *) newdecl + sizeof (struct tree_common),
2548 (char *) olddecl + sizeof (struct tree_common),
2549 function_size - sizeof (struct tree_common));
2550
2551 /* Can we safely free the storage used by newdecl? */
2552
2553 #define ROUND(x) ((x + obstack_alignment_mask (&permanent_obstack)) \
2554 & ~ obstack_alignment_mask (&permanent_obstack))
2555
2556 if ((char *)newdecl + ROUND (function_size)
2557 + ROUND (sizeof (struct lang_decl))
2558 == obstack_next_free (&permanent_obstack))
2559 {
2560 DECL_MAIN_VARIANT (newdecl) = olddecl;
2561 DECL_LANG_SPECIFIC (olddecl) = ol;
2562 bcopy ((char *)nl, (char *)ol, sizeof (struct lang_decl));
2563
2564 obstack_free (&permanent_obstack, newdecl);
2565 }
2566 else if (LANG_DECL_PERMANENT (ol))
2567 {
2568 if (DECL_MAIN_VARIANT (olddecl) == olddecl)
2569 {
2570 /* Save these lang_decls that would otherwise be lost. */
2571 extern tree free_lang_decl_chain;
2572 tree free_lang_decl = (tree) ol;
2573 TREE_CHAIN (free_lang_decl) = free_lang_decl_chain;
2574 free_lang_decl_chain = free_lang_decl;
2575 }
2576 else
2577 {
2578 /* Storage leak. */
2579 }
2580 }
2581 }
2582 else
2583 {
2584 bcopy ((char *) newdecl + sizeof (struct tree_common),
2585 (char *) olddecl + sizeof (struct tree_common),
2586 sizeof (struct tree_decl) - sizeof (struct tree_common)
2587 + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *));
2588 }
2589
2590 DECL_UID (olddecl) = olddecl_uid;
2591 if (olddecl_friend)
2592 DECL_FRIEND_P (olddecl) = 1;
2593
2594 return 1;
2595 }
2596
2597 /* Record a decl-node X as belonging to the current lexical scope.
2598 Check for errors (such as an incompatible declaration for the same
2599 name already seen in the same scope).
2600
2601 Returns either X or an old decl for the same name.
2602 If an old decl is returned, it may have been smashed
2603 to agree with what X says. */
2604
2605 tree
2606 pushdecl (x)
2607 tree x;
2608 {
2609 register tree t;
2610 #if 0 /* not yet, should get fixed properly later */
2611 register tree name;
2612 #else
2613 register tree name = DECL_ASSEMBLER_NAME (x);
2614 #endif
2615 register struct binding_level *b = current_binding_level;
2616
2617 #if 0
2618 static int nglobals; int len;
2619
2620 len = list_length (global_binding_level->names);
2621 if (len < nglobals)
2622 my_friendly_abort (8);
2623 else if (len > nglobals)
2624 nglobals = len;
2625 #endif
2626
2627 if (x != current_function_decl
2628 /* Don't change DECL_CONTEXT of virtual methods. */
2629 && (TREE_CODE (x) != FUNCTION_DECL || !DECL_VIRTUAL_P (x))
2630 && ! DECL_CONTEXT (x))
2631 DECL_CONTEXT (x) = current_function_decl;
2632 /* A local declaration for a function doesn't constitute nesting. */
2633 if (TREE_CODE (x) == FUNCTION_DECL && DECL_INITIAL (x) == 0)
2634 DECL_CONTEXT (x) = 0;
2635
2636 #if 0 /* not yet, should get fixed properly later */
2637 /* For functions and class static data, we currently look up the encoded
2638 form of the name. For types, we want the real name. The former will
2639 probably be changed soon, according to MDT. */
2640 if (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
2641 name = DECL_ASSEMBLER_NAME (x);
2642 else
2643 name = DECL_NAME (x);
2644 #else
2645 /* Type are looked up using the DECL_NAME, as that is what the rest of the
2646 compiler wants to use. */
2647 if (TREE_CODE (x) == TYPE_DECL)
2648 name = DECL_NAME (x);
2649 #endif
2650
2651 if (name)
2652 {
2653 char *file;
2654 int line;
2655
2656 t = lookup_name_current_level (name);
2657 if (t == error_mark_node)
2658 {
2659 /* error_mark_node is 0 for a while during initialization! */
2660 t = NULL_TREE;
2661 cp_error_at ("`%#D' used prior to declaration", x);
2662 }
2663
2664 else if (t != NULL_TREE)
2665 {
2666 if (TREE_CODE (t) == PARM_DECL)
2667 {
2668 if (DECL_CONTEXT (t) == NULL_TREE)
2669 fatal ("parse errors have confused me too much");
2670 }
2671 file = DECL_SOURCE_FILE (t);
2672 line = DECL_SOURCE_LINE (t);
2673
2674 if (((TREE_CODE (x) == FUNCTION_DECL && DECL_LANGUAGE (x) == lang_c)
2675 || (TREE_CODE (x) == TEMPLATE_DECL
2676 && ! DECL_TEMPLATE_IS_CLASS (x)))
2677 && is_overloaded_fn (t))
2678 /* don't do anything just yet */;
2679 else if (TREE_CODE (t) != TREE_CODE (x))
2680 {
2681 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (x) == TYPE_DECL)
2682 {
2683 /* We do nothing special here, because C++ does such nasty
2684 things with TYPE_DECLs. Instead, just let the TYPE_DECL
2685 get shadowed, and know that if we need to find a TYPE_DECL
2686 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
2687 slot of the identifier. */
2688 ;
2689 }
2690 else if (duplicate_decls (x, t))
2691 return t;
2692 }
2693 else if (duplicate_decls (x, t))
2694 {
2695 #if 0
2696 /* This is turned off until I have time to do it right (bpk). */
2697
2698 /* Also warn if they did a prototype with `static' on it, but
2699 then later left the `static' off. */
2700 if (! TREE_PUBLIC (name) && TREE_PUBLIC (x))
2701 {
2702 if (DECL_LANG_SPECIFIC (t) && DECL_FRIEND_P (t))
2703 return t;
2704
2705 if (extra_warnings)
2706 {
2707 cp_warning ("`static' missing from declaration of `%D'",
2708 t);
2709 warning_with_file_and_line (file, line,
2710 "previous declaration of `%s'",
2711 decl_as_string (t, 0));
2712 }
2713
2714 /* Now fix things so it'll do what they expect. */
2715 if (current_function_decl)
2716 TREE_PUBLIC (current_function_decl) = 0;
2717 }
2718 /* Due to interference in memory reclamation (X may be
2719 obstack-deallocated at this point), we must guard against
2720 one really special case. [jason: This should be handled
2721 by start_function] */
2722 if (current_function_decl == x)
2723 current_function_decl = t;
2724 #endif
2725 if (TREE_CODE (t) == TYPE_DECL)
2726 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
2727
2728 return t;
2729 }
2730 }
2731
2732 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_FUNCTION_MEMBER_P (x))
2733 {
2734 t = push_overloaded_decl (x, 1);
2735 if (t != x || DECL_LANGUAGE (x) == lang_c)
2736 return t;
2737 }
2738 else if (TREE_CODE (x) == TEMPLATE_DECL && ! DECL_TEMPLATE_IS_CLASS (x))
2739 return push_overloaded_decl (x, 0);
2740
2741 /* If declaring a type as a typedef, and the type has no known
2742 typedef name, install this TYPE_DECL as its typedef name. */
2743 if (TREE_CODE (x) == TYPE_DECL)
2744 {
2745 tree type = TREE_TYPE (x);
2746 tree name = (type != error_mark_node) ? TYPE_NAME (type) : x;
2747
2748 if (name == NULL_TREE || TREE_CODE (name) != TYPE_DECL)
2749 {
2750 /* If these are different names, and we're at the global
2751 binding level, make two equivalent definitions. */
2752 name = x;
2753 if (global_bindings_p ())
2754 TYPE_NAME (type) = x;
2755 }
2756 else
2757 {
2758 tree tname = DECL_NAME (name);
2759
2760 if (global_bindings_p () && ANON_AGGRNAME_P (tname))
2761 {
2762 /* do gratuitous C++ typedefing, and make sure that
2763 we access this type either through TREE_TYPE field
2764 or via the tags list. */
2765 TYPE_NAME (TREE_TYPE (x)) = x;
2766 pushtag (tname, TREE_TYPE (x), 0);
2767 }
2768 }
2769 my_friendly_assert (TREE_CODE (name) == TYPE_DECL, 140);
2770
2771 if (DECL_NAME (name) && !DECL_NESTED_TYPENAME (name))
2772 set_nested_typename (x, current_class_name,
2773 DECL_NAME (name), type);
2774
2775 if (type != error_mark_node
2776 && TYPE_NAME (type)
2777 && TYPE_IDENTIFIER (type))
2778 set_identifier_type_value_with_scope (DECL_NAME (x), type, b);
2779 }
2780
2781 /* Multiple external decls of the same identifier ought to match.
2782
2783 We get warnings about inline functions where they are defined.
2784 We get warnings about other functions from push_overloaded_decl.
2785
2786 Avoid duplicate warnings where they are used. */
2787 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
2788 {
2789 tree decl;
2790
2791 if (IDENTIFIER_GLOBAL_VALUE (name) != NULL_TREE
2792 && (DECL_EXTERNAL (IDENTIFIER_GLOBAL_VALUE (name))
2793 || TREE_PUBLIC (IDENTIFIER_GLOBAL_VALUE (name))))
2794 decl = IDENTIFIER_GLOBAL_VALUE (name);
2795 else
2796 decl = NULL_TREE;
2797
2798 if (decl
2799 /* If different sort of thing, we already gave an error. */
2800 && TREE_CODE (decl) == TREE_CODE (x)
2801 && ! comptypes (TREE_TYPE (x), TREE_TYPE (decl), 1))
2802 {
2803 cp_pedwarn ("type mismatch with previous external decl", x);
2804 cp_pedwarn_at ("previous external decl of `%#D'", decl);
2805 }
2806 }
2807
2808 /* In PCC-compatibility mode, extern decls of vars with no current decl
2809 take effect at top level no matter where they are. */
2810 if (flag_traditional && DECL_EXTERNAL (x)
2811 && lookup_name (name, 0) == NULL_TREE)
2812 b = global_binding_level;
2813
2814 /* This name is new in its binding level.
2815 Install the new declaration and return it. */
2816 if (b == global_binding_level)
2817 {
2818 /* Install a global value. */
2819
2820 /* Rule for VAR_DECLs, but not for other kinds of _DECLs:
2821 A `const' which was not declared `extern' is invisible. */
2822 if (TREE_CODE (x) == VAR_DECL
2823 && TREE_READONLY (x) && ! DECL_THIS_EXTERN (x))
2824 TREE_PUBLIC (x) = 0;
2825
2826 /* If the first global decl has external linkage,
2827 warn if we later see static one. */
2828 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
2829 TREE_PUBLIC (name) = 1;
2830
2831 /* Don't install a TYPE_DECL if we already have another
2832 sort of _DECL with that name. */
2833 if (TREE_CODE (x) != TYPE_DECL
2834 || t == NULL_TREE
2835 || TREE_CODE (t) == TYPE_DECL)
2836 IDENTIFIER_GLOBAL_VALUE (name) = x;
2837
2838 /* Don't forget if the function was used via an implicit decl. */
2839 if (IDENTIFIER_IMPLICIT_DECL (name)
2840 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
2841 TREE_USED (x) = 1;
2842
2843 /* Don't forget if its address was taken in that way. */
2844 if (IDENTIFIER_IMPLICIT_DECL (name)
2845 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
2846 TREE_ADDRESSABLE (x) = 1;
2847
2848 /* Warn about mismatches against previous implicit decl. */
2849 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
2850 /* If this real decl matches the implicit, don't complain. */
2851 && ! (TREE_CODE (x) == FUNCTION_DECL
2852 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
2853 cp_warning
2854 ("`%D' was previously implicitly declared to return `int'", x);
2855
2856 /* If new decl is `static' and an `extern' was seen previously,
2857 warn about it. */
2858 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
2859 warn_extern_redeclared_static (x, t);
2860 }
2861 else
2862 {
2863 /* Here to install a non-global value. */
2864 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
2865 tree oldglobal = IDENTIFIER_GLOBAL_VALUE (name);
2866
2867 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
2868 IDENTIFIER_LOCAL_VALUE (name) = x;
2869
2870 /* If this is a TYPE_DECL, push it into the type value slot. */
2871 if (TREE_CODE (x) == TYPE_DECL)
2872 set_identifier_type_value_with_scope (name, TREE_TYPE (x), b);
2873
2874 /* If this is an extern function declaration, see if we
2875 have a global definition or declaration for the function. */
2876 if (oldlocal == NULL_TREE
2877 && DECL_EXTERNAL (x) && !DECL_INLINE (x)
2878 && oldglobal != NULL_TREE
2879 && TREE_CODE (x) == FUNCTION_DECL
2880 && TREE_CODE (oldglobal) == FUNCTION_DECL)
2881 {
2882 /* We have one. Their types must agree. */
2883 if (! comptypes (TREE_TYPE (x), TREE_TYPE (oldglobal), 1))
2884 {
2885 cp_warning ("extern declaration of `%#D' doesn't match", x);
2886 cp_warning_at ("global declaration `%#D'", oldglobal);
2887 }
2888 else
2889 {
2890 /* Inner extern decl is inline if global one is.
2891 Copy enough to really inline it. */
2892 if (DECL_INLINE (oldglobal))
2893 {
2894 DECL_INLINE (x) = DECL_INLINE (oldglobal);
2895 DECL_INITIAL (x) = (current_function_decl == oldglobal
2896 ? NULL_TREE : DECL_INITIAL (oldglobal));
2897 DECL_SAVED_INSNS (x) = DECL_SAVED_INSNS (oldglobal);
2898 DECL_FRAME_SIZE (x) = DECL_FRAME_SIZE (oldglobal);
2899 DECL_ARGUMENTS (x) = DECL_ARGUMENTS (oldglobal);
2900 DECL_RESULT (x) = DECL_RESULT (oldglobal);
2901 TREE_ASM_WRITTEN (x) = TREE_ASM_WRITTEN (oldglobal);
2902 DECL_ABSTRACT_ORIGIN (x) = oldglobal;
2903 }
2904 /* Inner extern decl is built-in if global one is. */
2905 if (DECL_BUILT_IN (oldglobal))
2906 {
2907 DECL_BUILT_IN (x) = DECL_BUILT_IN (oldglobal);
2908 DECL_FUNCTION_CODE (x) = DECL_FUNCTION_CODE (oldglobal);
2909 }
2910 /* Keep the arg types from a file-scope fcn defn. */
2911 if (TYPE_ARG_TYPES (TREE_TYPE (oldglobal)) != NULL_TREE
2912 && DECL_INITIAL (oldglobal)
2913 && TYPE_ARG_TYPES (TREE_TYPE (x)) == NULL_TREE)
2914 TREE_TYPE (x) = TREE_TYPE (oldglobal);
2915 }
2916 }
2917 /* If we have a local external declaration,
2918 and no file-scope declaration has yet been seen,
2919 then if we later have a file-scope decl it must not be static. */
2920 if (oldlocal == NULL_TREE
2921 && oldglobal == NULL_TREE
2922 && DECL_EXTERNAL (x)
2923 && TREE_PUBLIC (x))
2924 {
2925 TREE_PUBLIC (name) = 1;
2926 }
2927
2928 if (DECL_FROM_INLINE (x))
2929 /* Inline decls shadow nothing. */;
2930
2931 /* Warn if shadowing an argument at the top level of the body. */
2932 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
2933 && TREE_CODE (oldlocal) == PARM_DECL
2934 && TREE_CODE (x) != PARM_DECL)
2935 {
2936 /* Go to where the parms should be and see if we
2937 find them there. */
2938 struct binding_level *b = current_binding_level->level_chain;
2939
2940 if (cleanup_label)
2941 b = b->level_chain;
2942
2943 /* ARM $8.3 */
2944 if (b->parm_flag == 1)
2945 cp_error ("declaration of `%#D' shadows a parameter", name);
2946 }
2947 /* Maybe warn if shadowing something else. */
2948 else if (warn_shadow && !DECL_EXTERNAL (x)
2949 /* No shadow warnings for internally generated vars. */
2950 && ! DECL_ARTIFICIAL (x)
2951 /* No shadow warnings for vars made for inlining. */
2952 && ! DECL_FROM_INLINE (x))
2953 {
2954 char *warnstring = NULL;
2955
2956 if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
2957 warnstring = "declaration of `%s' shadows a parameter";
2958 else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
2959 && !TREE_STATIC (name))
2960 warnstring = "declaration of `%s' shadows a member of `this'";
2961 else if (oldlocal != NULL_TREE)
2962 warnstring = "declaration of `%s' shadows previous local";
2963 else if (oldglobal != NULL_TREE)
2964 warnstring = "declaration of `%s' shadows global declaration";
2965
2966 if (warnstring)
2967 warning (warnstring, IDENTIFIER_POINTER (name));
2968 }
2969
2970 /* If storing a local value, there may already be one (inherited).
2971 If so, record it for restoration when this binding level ends. */
2972 if (oldlocal != NULL_TREE)
2973 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
2974 }
2975
2976 /* Keep count of variables in this level with incomplete type. */
2977 if (TREE_CODE (x) != TEMPLATE_DECL
2978 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
2979 && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE))
2980 {
2981 if (++b->n_incomplete == 0)
2982 error ("too many incomplete variables at this point");
2983 }
2984 }
2985
2986 if (TREE_CODE (x) == TYPE_DECL && name != NULL_TREE)
2987 {
2988 if (current_class_name)
2989 {
2990 if (! TREE_MANGLED (name))
2991 set_nested_typename (x, current_class_name, DECL_NAME (x),
2992 TREE_TYPE (x));
2993 }
2994 }
2995
2996 /* Put decls on list in reverse order.
2997 We will reverse them later if necessary. */
2998 TREE_CHAIN (x) = b->names;
2999 b->names = x;
3000 if (! (b != global_binding_level || TREE_PERMANENT (x)))
3001 my_friendly_abort (124);
3002
3003 return x;
3004 }
3005
3006 /* Same as pushdecl, but define X in binding-level LEVEL. */
3007
3008 static tree
3009 pushdecl_with_scope (x, level)
3010 tree x;
3011 struct binding_level *level;
3012 {
3013 register struct binding_level *b = current_binding_level;
3014
3015 current_binding_level = level;
3016 x = pushdecl (x);
3017 current_binding_level = b;
3018 return x;
3019 }
3020
3021 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
3022 if appropriate. */
3023 tree
3024 pushdecl_top_level (x)
3025 tree x;
3026 {
3027 register struct binding_level *b = inner_binding_level;
3028 register tree t = pushdecl_with_scope (x, global_binding_level);
3029
3030 /* Now, the type_shadowed stack may screw us. Munge it so it does
3031 what we want. */
3032 if (TREE_CODE (x) == TYPE_DECL)
3033 {
3034 tree name = DECL_NAME (x);
3035 tree newval;
3036 tree *ptr = (tree *)0;
3037 for (; b != global_binding_level; b = b->level_chain)
3038 {
3039 tree shadowed = b->type_shadowed;
3040 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3041 if (TREE_PURPOSE (shadowed) == name)
3042 {
3043 ptr = &TREE_VALUE (shadowed);
3044 /* Can't break out of the loop here because sometimes
3045 a binding level will have duplicate bindings for
3046 PT names. It's gross, but I haven't time to fix it. */
3047 }
3048 }
3049 newval = TREE_TYPE (x);
3050 if (ptr == (tree *)0)
3051 {
3052 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3053 up here if this is changed to an assertion. --KR */
3054 SET_IDENTIFIER_TYPE_VALUE (name, newval);
3055 }
3056 else
3057 {
3058 #if 0
3059 /* Disabled this 11/10/92, since there are many cases which
3060 behave just fine when *ptr doesn't satisfy either of these.
3061 For example, nested classes declared as friends of their enclosing
3062 class will not meet this criteria. (bpk) */
3063 my_friendly_assert (*ptr == NULL_TREE || *ptr == newval, 141);
3064 #endif
3065 *ptr = newval;
3066 }
3067 }
3068 return t;
3069 }
3070
3071 /* Like push_overloaded_decl, only it places X in GLOBAL_BINDING_LEVEL,
3072 if appropriate. */
3073 void
3074 push_overloaded_decl_top_level (x, forget)
3075 tree x;
3076 int forget;
3077 {
3078 struct binding_level *b = current_binding_level;
3079
3080 current_binding_level = global_binding_level;
3081 push_overloaded_decl (x, forget);
3082 current_binding_level = b;
3083 }
3084
3085 /* Make the declaration of X appear in CLASS scope. */
3086 tree
3087 pushdecl_class_level (x)
3088 tree x;
3089 {
3090 /* Don't use DECL_ASSEMBLER_NAME here! Everything that looks in class
3091 scope looks for the pre-mangled name. */
3092 register tree name = DECL_NAME (x);
3093
3094 if (name)
3095 {
3096 if (TYPE_BEING_DEFINED (current_class_type))
3097 {
3098 /* Check for inconsistent use of this name in the class body.
3099 Types, enums, and static vars are checked here; other
3100 members are checked in finish_struct. */
3101 tree icv = IDENTIFIER_CLASS_VALUE (name);
3102
3103 if (icv
3104 /* Don't complain about inherited names. */
3105 && id_in_current_class (name)
3106 /* Or shadowed tags. */
3107 && !(TREE_CODE (icv) == TYPE_DECL
3108 && DECL_CONTEXT (icv) == current_class_type))
3109 {
3110 cp_error ("declaration of identifier `%D' as `%#D'", name, x);
3111 cp_error_at ("conflicts with previous use in class as `%#D'",
3112 icv);
3113 }
3114 }
3115
3116 push_class_level_binding (name, x);
3117 if (TREE_CODE (x) == TYPE_DECL)
3118 {
3119 set_identifier_type_value (name, TREE_TYPE (x));
3120 if (!DECL_NESTED_TYPENAME (x))
3121 set_nested_typename (x, current_class_name, name, TREE_TYPE (x));
3122 }
3123 }
3124 return x;
3125 }
3126
3127 /* This function is used to push the mangled decls for nested types into
3128 the appropriate scope. Previously pushdecl_top_level was used, but that
3129 is incorrect for members of local classes. */
3130 tree
3131 pushdecl_nonclass_level (x)
3132 tree x;
3133 {
3134 struct binding_level *b = current_binding_level;
3135
3136 #if 0
3137 /* Get out of class scope -- this isn't necessary, because class scope
3138 doesn't make it into current_binding_level. */
3139 while (b->parm_flag == 2)
3140 b = b->level_chain;
3141 #else
3142 my_friendly_assert (b->parm_flag != 2, 180);
3143 #endif
3144
3145 /* Get out of template binding levels */
3146 while (b->pseudo_global)
3147 b = b->level_chain;
3148
3149 pushdecl_with_scope (x, b);
3150 }
3151
3152 /* Make the declaration(s) of X appear in CLASS scope
3153 under the name NAME. */
3154 void
3155 push_class_level_binding (name, x)
3156 tree name;
3157 tree x;
3158 {
3159 maybe_push_cache_obstack ();
3160 class_binding_level->class_shadowed
3161 = tree_cons (name, IDENTIFIER_CLASS_VALUE (name),
3162 class_binding_level->class_shadowed);
3163 pop_obstacks ();
3164 IDENTIFIER_CLASS_VALUE (name) = x;
3165 obstack_ptr_grow (&decl_obstack, x);
3166 }
3167
3168 /* Tell caller how to interpret a TREE_LIST which contains
3169 chains of FUNCTION_DECLS. */
3170 int
3171 overloaded_globals_p (list)
3172 tree list;
3173 {
3174 my_friendly_assert (TREE_CODE (list) == TREE_LIST, 142);
3175
3176 /* Don't commit caller to seeing them as globals. */
3177 if (TREE_NONLOCAL_FLAG (list))
3178 return -1;
3179 /* Do commit caller to seeing them as globals. */
3180 if (TREE_CODE (TREE_PURPOSE (list)) == IDENTIFIER_NODE)
3181 return 1;
3182 /* Do commit caller to not seeing them as globals. */
3183 return 0;
3184 }
3185
3186 /* DECL is a FUNCTION_DECL which may have other definitions already in
3187 place. We get around this by making the value of the identifier point
3188 to a list of all the things that want to be referenced by that name. It
3189 is then up to the users of that name to decide what to do with that
3190 list.
3191
3192 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its DECL_RESULT
3193 slot. It is dealt with the same way.
3194
3195 The value returned may be a previous declaration if we guessed wrong
3196 about what language DECL should belong to (C or C++). Otherwise,
3197 it's always DECL (and never something that's not a _DECL). */
3198 tree
3199 push_overloaded_decl (decl, forgettable)
3200 tree decl;
3201 int forgettable;
3202 {
3203 tree orig_name = DECL_NAME (decl);
3204 tree old;
3205 int doing_global = (global_bindings_p () || ! forgettable
3206 || flag_traditional || pseudo_global_level_p ());
3207
3208 if (doing_global)
3209 {
3210 old = IDENTIFIER_GLOBAL_VALUE (orig_name);
3211 if (old && TREE_CODE (old) == FUNCTION_DECL
3212 && DECL_ARTIFICIAL (old)
3213 && (DECL_BUILT_IN (old) || DECL_BUILT_IN_NONANSI (old)))
3214 {
3215 if (duplicate_decls (decl, old))
3216 return old;
3217 old = NULL_TREE;
3218 }
3219 }
3220 else
3221 {
3222 old = IDENTIFIER_LOCAL_VALUE (orig_name);
3223
3224 if (! purpose_member (orig_name, current_binding_level->shadowed))
3225 {
3226 current_binding_level->shadowed
3227 = tree_cons (orig_name, old, current_binding_level->shadowed);
3228 old = NULL_TREE;
3229 }
3230 }
3231
3232 if (old)
3233 {
3234 #if 0
3235 /* We cache the value of builtin functions as ADDR_EXPRs
3236 in the name space. Convert it to some kind of _DECL after
3237 remembering what to forget. */
3238 if (TREE_CODE (old) == ADDR_EXPR)
3239 old = TREE_OPERAND (old, 0);
3240 else
3241 #endif
3242 if (TREE_CODE (old) == VAR_DECL)
3243 {
3244 cp_error_at ("previous non-function declaration `%#D'", old);
3245 cp_error ("conflicts with function declaration `%#D'", decl);
3246 return error_mark_node;
3247 }
3248 else if (TREE_CODE (old) == TYPE_DECL)
3249 {
3250 tree t = TREE_TYPE (old);
3251 if (IS_AGGR_TYPE (t) && warn_shadow)
3252 cp_warning ("`%#D' hides constructor for `%#T'", decl, t);
3253 old = NULL_TREE;
3254 }
3255 else if (is_overloaded_fn (old))
3256 {
3257 tree tmp;
3258
3259 for (tmp = get_first_fn (old); tmp; tmp = DECL_CHAIN (tmp))
3260 if (decl == tmp || duplicate_decls (decl, tmp))
3261 return tmp;
3262 }
3263 }
3264
3265 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
3266 {
3267 if (old && is_overloaded_fn (old))
3268 DECL_CHAIN (decl) = get_first_fn (old);
3269 else
3270 DECL_CHAIN (decl) = NULL_TREE;
3271 old = tree_cons (orig_name, decl, NULL_TREE);
3272 TREE_TYPE (old) = unknown_type_node;
3273 }
3274 else
3275 /* orig_name is not ambiguous. */
3276 old = decl;
3277
3278 if (doing_global)
3279 IDENTIFIER_GLOBAL_VALUE (orig_name) = old;
3280 else
3281 IDENTIFIER_LOCAL_VALUE (orig_name) = old;
3282
3283 return decl;
3284 }
3285 \f
3286 /* Generate an implicit declaration for identifier FUNCTIONID
3287 as a function of type int (). Print a warning if appropriate. */
3288
3289 tree
3290 implicitly_declare (functionid)
3291 tree functionid;
3292 {
3293 register tree decl;
3294 int temp = allocation_temporary_p ();
3295
3296 push_obstacks_nochange ();
3297
3298 /* Save the decl permanently so we can warn if definition follows.
3299 In ANSI C, warn_implicit is usually false, so the saves little space.
3300 But in C++, it's usually true, hence the extra code. */
3301 if (temp && (flag_traditional || !warn_implicit
3302 || current_binding_level == global_binding_level))
3303 end_temporary_allocation ();
3304
3305 /* We used to reuse an old implicit decl here,
3306 but this loses with inline functions because it can clobber
3307 the saved decl chains. */
3308 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
3309
3310 DECL_EXTERNAL (decl) = 1;
3311 TREE_PUBLIC (decl) = 1;
3312
3313 /* ANSI standard says implicit declarations are in the innermost block.
3314 So we record the decl in the standard fashion.
3315 If flag_traditional is set, pushdecl does it top-level. */
3316 pushdecl (decl);
3317 rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
3318
3319 if (warn_implicit
3320 /* Only one warning per identifier. */
3321 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
3322 {
3323 cp_pedwarn ("implicit declaration of function `%#D'", decl);
3324 }
3325
3326 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
3327
3328 pop_obstacks ();
3329
3330 return decl;
3331 }
3332
3333 /* Return zero if the declaration NEWDECL is valid
3334 when the declaration OLDDECL (assumed to be for the same name)
3335 has already been seen.
3336 Otherwise return an error message format string with a %s
3337 where the identifier should go. */
3338
3339 static char *
3340 redeclaration_error_message (newdecl, olddecl)
3341 tree newdecl, olddecl;
3342 {
3343 if (TREE_CODE (newdecl) == TYPE_DECL)
3344 {
3345 /* Because C++ can put things into name space for free,
3346 constructs like "typedef struct foo { ... } foo"
3347 would look like an erroneous redeclaration. */
3348 if (comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl), 0))
3349 return 0;
3350 else
3351 return "redefinition of `%#D'";
3352 }
3353 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3354 {
3355 /* If this is a pure function, its olddecl will actually be
3356 the original initialization to `0' (which we force to call
3357 abort()). Don't complain about redefinition in this case. */
3358 if (DECL_LANG_SPECIFIC (olddecl) && DECL_ABSTRACT_VIRTUAL_P (olddecl))
3359 return 0;
3360
3361 /* Declarations of functions can insist on internal linkage
3362 but they can't be inconsistent with internal linkage,
3363 so there can be no error on that account.
3364 However defining the same name twice is no good. */
3365 if (DECL_INITIAL (olddecl) != NULL_TREE
3366 && DECL_INITIAL (newdecl) != NULL_TREE
3367 /* However, defining once as extern inline and a second
3368 time in another way is ok. */
3369 && !(DECL_INLINE (olddecl) && DECL_EXTERNAL (olddecl)
3370 && !(DECL_INLINE (newdecl) && DECL_EXTERNAL (newdecl))))
3371 {
3372 if (DECL_NAME (olddecl) == NULL_TREE)
3373 return "`%#D' not declared in class";
3374 else
3375 return "redefinition of `%#D'";
3376 }
3377
3378 {
3379 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3380 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3381
3382 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3383 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3384
3385 for (; t1; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
3386 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3387 return "duplicate default arguments given for `%#D'";
3388 }
3389 return 0;
3390 }
3391 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3392 {
3393 if (DECL_INITIAL (olddecl) && DECL_INITIAL (newdecl))
3394 return "redefinition of `%#D'";
3395 return 0;
3396 }
3397 else if (current_binding_level == global_binding_level)
3398 {
3399 /* Objects declared at top level: */
3400 /* If at least one is a reference, it's ok. */
3401 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3402 return 0;
3403 /* Now we have two tentative defs, or one tentative and one real def. */
3404 /* Insist that the linkage match. */
3405 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
3406 return "conflicting declarations of `%#D'";
3407 /* Reject two definitions. */
3408 return "redefinition of `%#D'";
3409 }
3410 else
3411 {
3412 /* Objects declared with block scope: */
3413 /* Reject two definitions, and reject a definition
3414 together with an external reference. */
3415 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3416 return "redeclaration of `%#D'";
3417 return 0;
3418 }
3419 }
3420 \f
3421 /* Get the LABEL_DECL corresponding to identifier ID as a label.
3422 Create one if none exists so far for the current function.
3423 This function is called for both label definitions and label references. */
3424
3425 tree
3426 lookup_label (id)
3427 tree id;
3428 {
3429 register tree decl = IDENTIFIER_LABEL_VALUE (id);
3430
3431 if (current_function_decl == NULL_TREE)
3432 {
3433 error ("label `%s' referenced outside of any function",
3434 IDENTIFIER_POINTER (id));
3435 return NULL_TREE;
3436 }
3437
3438 if ((decl == NULL_TREE
3439 || DECL_SOURCE_LINE (decl) == 0)
3440 && (named_label_uses == NULL_TREE
3441 || TREE_PURPOSE (named_label_uses) != current_binding_level->names
3442 || TREE_VALUE (named_label_uses) != decl))
3443 {
3444 named_label_uses
3445 = tree_cons (current_binding_level->names, decl, named_label_uses);
3446 TREE_TYPE (named_label_uses) = (tree)current_binding_level;
3447 }
3448
3449 /* Use a label already defined or ref'd with this name. */
3450 if (decl != NULL_TREE)
3451 {
3452 /* But not if it is inherited and wasn't declared to be inheritable. */
3453 if (DECL_CONTEXT (decl) != current_function_decl
3454 && ! C_DECLARED_LABEL_FLAG (decl))
3455 return shadow_label (id);
3456 return decl;
3457 }
3458
3459 decl = build_decl (LABEL_DECL, id, void_type_node);
3460
3461 /* A label not explicitly declared must be local to where it's ref'd. */
3462 DECL_CONTEXT (decl) = current_function_decl;
3463
3464 DECL_MODE (decl) = VOIDmode;
3465
3466 /* Say where one reference is to the label,
3467 for the sake of the error if it is not defined. */
3468 DECL_SOURCE_LINE (decl) = lineno;
3469 DECL_SOURCE_FILE (decl) = input_filename;
3470
3471 SET_IDENTIFIER_LABEL_VALUE (id, decl);
3472
3473 named_labels = tree_cons (NULL_TREE, decl, named_labels);
3474 TREE_VALUE (named_label_uses) = decl;
3475
3476 return decl;
3477 }
3478
3479 /* Make a label named NAME in the current function,
3480 shadowing silently any that may be inherited from containing functions
3481 or containing scopes.
3482
3483 Note that valid use, if the label being shadowed
3484 comes from another scope in the same function,
3485 requires calling declare_nonlocal_label right away. */
3486
3487 tree
3488 shadow_label (name)
3489 tree name;
3490 {
3491 register tree decl = IDENTIFIER_LABEL_VALUE (name);
3492
3493 if (decl != NULL_TREE)
3494 {
3495 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
3496 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
3497 SET_IDENTIFIER_LABEL_VALUE (decl, NULL_TREE);
3498 }
3499
3500 return lookup_label (name);
3501 }
3502
3503 /* Define a label, specifying the location in the source file.
3504 Return the LABEL_DECL node for the label, if the definition is valid.
3505 Otherwise return 0. */
3506
3507 tree
3508 define_label (filename, line, name)
3509 char *filename;
3510 int line;
3511 tree name;
3512 {
3513 tree decl = lookup_label (name);
3514
3515 /* After labels, make any new cleanups go into their
3516 own new (temporary) binding contour. */
3517 current_binding_level->more_cleanups_ok = 0;
3518
3519 /* If label with this name is known from an outer context, shadow it. */
3520 if (decl != NULL_TREE && DECL_CONTEXT (decl) != current_function_decl)
3521 {
3522 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
3523 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
3524 decl = lookup_label (name);
3525 }
3526
3527 if (DECL_INITIAL (decl) != NULL_TREE)
3528 {
3529 cp_error ("duplicate label `%D'", decl);
3530 return 0;
3531 }
3532 else
3533 {
3534 tree uses, prev;
3535
3536 /* Mark label as having been defined. */
3537 DECL_INITIAL (decl) = error_mark_node;
3538 /* Say where in the source. */
3539 DECL_SOURCE_FILE (decl) = filename;
3540 DECL_SOURCE_LINE (decl) = line;
3541
3542 for (prev = NULL_TREE, uses = named_label_uses;
3543 uses;
3544 prev = uses, uses = TREE_CHAIN (uses))
3545 if (TREE_VALUE (uses) == decl)
3546 {
3547 struct binding_level *b = current_binding_level;
3548 while (b)
3549 {
3550 tree new_decls = b->names;
3551 tree old_decls = ((tree)b == TREE_TYPE (uses)
3552 ? TREE_PURPOSE (uses) : NULL_TREE);
3553 while (new_decls != old_decls)
3554 {
3555 if (TREE_CODE (new_decls) == VAR_DECL
3556 /* Don't complain about crossing initialization
3557 of internal entities. They can't be accessed,
3558 and they should be cleaned up
3559 by the time we get to the label. */
3560 && ! DECL_ARTIFICIAL (new_decls)
3561 && ((DECL_INITIAL (new_decls) != NULL_TREE
3562 && DECL_INITIAL (new_decls) != error_mark_node)
3563 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
3564 {
3565 if (IDENTIFIER_ERROR_LOCUS (decl) == NULL_TREE)
3566 cp_error ("invalid jump to label `%D'", decl);
3567 SET_IDENTIFIER_ERROR_LOCUS (decl, current_function_decl);
3568 cp_error ("crosses initialization of `%D'", new_decls);
3569 }
3570 new_decls = TREE_CHAIN (new_decls);
3571 }
3572 if ((tree)b == TREE_TYPE (uses))
3573 break;
3574 b = b->level_chain;
3575 }
3576
3577 if (prev)
3578 TREE_CHAIN (prev) = TREE_CHAIN (uses);
3579 else
3580 named_label_uses = TREE_CHAIN (uses);
3581 }
3582 current_function_return_value = NULL_TREE;
3583 return decl;
3584 }
3585 }
3586
3587 /* Same, but for CASE labels. If DECL is NULL_TREE, it's the default. */
3588 /* XXX Note decl is never actually used. (bpk) */
3589 void
3590 define_case_label (decl)
3591 tree decl;
3592 {
3593 tree cleanup = last_cleanup_this_contour ();
3594 if (cleanup)
3595 {
3596 static int explained = 0;
3597 cp_error_at ("destructor needed for `%#D'", TREE_PURPOSE (cleanup));
3598 error ("where case label appears here");
3599 if (!explained)
3600 {
3601 error ("(enclose actions of previous case statements requiring");
3602 error ("destructors in their own binding contours.)");
3603 explained = 1;
3604 }
3605 }
3606
3607 /* After labels, make any new cleanups go into their
3608 own new (temporary) binding contour. */
3609
3610 current_binding_level->more_cleanups_ok = 0;
3611 current_function_return_value = NULL_TREE;
3612 }
3613 \f
3614 /* Return the list of declarations of the current level.
3615 Note that this list is in reverse order unless/until
3616 you nreverse it; and when you do nreverse it, you must
3617 store the result back using `storedecls' or you will lose. */
3618
3619 tree
3620 getdecls ()
3621 {
3622 return current_binding_level->names;
3623 }
3624
3625 /* Return the list of type-tags (for structs, etc) of the current level. */
3626
3627 tree
3628 gettags ()
3629 {
3630 return current_binding_level->tags;
3631 }
3632
3633 /* Store the list of declarations of the current level.
3634 This is done for the parameter declarations of a function being defined,
3635 after they are modified in the light of any missing parameters. */
3636
3637 static void
3638 storedecls (decls)
3639 tree decls;
3640 {
3641 current_binding_level->names = decls;
3642 }
3643
3644 /* Similarly, store the list of tags of the current level. */
3645
3646 static void
3647 storetags (tags)
3648 tree tags;
3649 {
3650 current_binding_level->tags = tags;
3651 }
3652 \f
3653 /* Given NAME, an IDENTIFIER_NODE,
3654 return the structure (or union or enum) definition for that name.
3655 Searches binding levels from BINDING_LEVEL up to the global level.
3656 If THISLEVEL_ONLY is nonzero, searches only the specified context
3657 (but skips any tag-transparent contexts to find one that is
3658 meaningful for tags).
3659 FORM says which kind of type the caller wants;
3660 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3661 If the wrong kind of type is found, and it's not a template, an error is
3662 reported. */
3663
3664 static tree
3665 lookup_tag (form, name, binding_level, thislevel_only)
3666 enum tree_code form;
3667 struct binding_level *binding_level;
3668 tree name;
3669 int thislevel_only;
3670 {
3671 register struct binding_level *level;
3672
3673 for (level = binding_level; level; level = level->level_chain)
3674 {
3675 register tree tail;
3676 if (ANON_AGGRNAME_P (name))
3677 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
3678 {
3679 /* There's no need for error checking here, because
3680 anon names are unique throughout the compilation. */
3681 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
3682 return TREE_VALUE (tail);
3683 }
3684 else
3685 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
3686 {
3687 if (TREE_PURPOSE (tail) == name)
3688 {
3689 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
3690 /* Should tighten this up; it'll probably permit
3691 UNION_TYPE and a struct template, for example. */
3692 if (code != form
3693 && !(form != ENUMERAL_TYPE
3694 && (code == TEMPLATE_DECL
3695 || code == UNINSTANTIATED_P_TYPE)))
3696
3697 {
3698 /* Definition isn't the kind we were looking for. */
3699 cp_error ("`%#D' redeclared as %C", TREE_VALUE (tail),
3700 form);
3701 }
3702 return TREE_VALUE (tail);
3703 }
3704 }
3705 if (thislevel_only && ! level->tag_transparent)
3706 return NULL_TREE;
3707 if (current_class_type && level->level_chain == global_binding_level)
3708 {
3709 /* Try looking in this class's tags before heading into
3710 global binding level. */
3711 tree context = current_class_type;
3712 while (context)
3713 {
3714 switch (TREE_CODE_CLASS (TREE_CODE (context)))
3715 {
3716 tree these_tags;
3717 case 't':
3718 these_tags = CLASSTYPE_TAGS (context);
3719 if (ANON_AGGRNAME_P (name))
3720 while (these_tags)
3721 {
3722 if (TYPE_IDENTIFIER (TREE_VALUE (these_tags))
3723 == name)
3724 return TREE_VALUE (tail);
3725 these_tags = TREE_CHAIN (these_tags);
3726 }
3727 else
3728 while (these_tags)
3729 {
3730 if (TREE_PURPOSE (these_tags) == name)
3731 {
3732 if (TREE_CODE (TREE_VALUE (these_tags)) != form)
3733 {
3734 cp_error ("`%#D' redeclared as %C in class scope",
3735 TREE_VALUE (tail), form);
3736 }
3737 return TREE_VALUE (tail);
3738 }
3739 these_tags = TREE_CHAIN (these_tags);
3740 }
3741 /* If this type is not yet complete, then don't
3742 look at its context. */
3743 if (TYPE_SIZE (context) == NULL_TREE)
3744 goto no_context;
3745 /* Go to next enclosing type, if any. */
3746 context = DECL_CONTEXT (TYPE_NAME (context));
3747 break;
3748 case 'd':
3749 context = DECL_CONTEXT (context);
3750 break;
3751 default:
3752 my_friendly_abort (10);
3753 }
3754 continue;
3755 no_context:
3756 break;
3757 }
3758 }
3759 }
3760 return NULL_TREE;
3761 }
3762
3763 void
3764 set_current_level_tags_transparency (tags_transparent)
3765 int tags_transparent;
3766 {
3767 current_binding_level->tag_transparent = tags_transparent;
3768 }
3769
3770 /* Given a type, find the tag that was defined for it and return the tag name.
3771 Otherwise return 0. However, the value can never be 0
3772 in the cases in which this is used.
3773
3774 C++: If NAME is non-zero, this is the new name to install. This is
3775 done when replacing anonymous tags with real tag names. */
3776
3777 static tree
3778 lookup_tag_reverse (type, name)
3779 tree type;
3780 tree name;
3781 {
3782 register struct binding_level *level;
3783
3784 for (level = current_binding_level; level; level = level->level_chain)
3785 {
3786 register tree tail;
3787 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
3788 {
3789 if (TREE_VALUE (tail) == type)
3790 {
3791 if (name)
3792 TREE_PURPOSE (tail) = name;
3793 return TREE_PURPOSE (tail);
3794 }
3795 }
3796 }
3797 return NULL_TREE;
3798 }
3799
3800 /* Given type TYPE which was not declared in C++ language context,
3801 attempt to find a name by which it is referred. */
3802 tree
3803 typedecl_for_tag (tag)
3804 tree tag;
3805 {
3806 struct binding_level *b = current_binding_level;
3807
3808 if (TREE_CODE (TYPE_NAME (tag)) == TYPE_DECL)
3809 return TYPE_NAME (tag);
3810
3811 while (b)
3812 {
3813 tree decls = b->names;
3814 while (decls)
3815 {
3816 if (TREE_CODE (decls) == TYPE_DECL && TREE_TYPE (decls) == tag)
3817 break;
3818 decls = TREE_CHAIN (decls);
3819 }
3820 if (decls)
3821 return decls;
3822 b = b->level_chain;
3823 }
3824 return NULL_TREE;
3825 }
3826 \f
3827 /* Lookup TYPE in CONTEXT (a chain of nested types or a FUNCTION_DECL).
3828 Return the type value, or NULL_TREE if not found. */
3829 static tree
3830 lookup_nested_type (type, context)
3831 tree type;
3832 tree context;
3833 {
3834 if (context == NULL_TREE)
3835 return NULL_TREE;
3836 while (context)
3837 {
3838 switch (TREE_CODE (context))
3839 {
3840 case TYPE_DECL:
3841 {
3842 tree ctype = TREE_TYPE (context);
3843 tree match = value_member (type, CLASSTYPE_TAGS (ctype));
3844 if (match)
3845 return TREE_VALUE (match);
3846 context = DECL_CONTEXT (context);
3847
3848 /* When we have a nested class whose member functions have
3849 local types (e.g., a set of enums), we'll arrive here
3850 with the DECL_CONTEXT as the actual RECORD_TYPE node for
3851 the enclosing class. Instead, we want to make sure we
3852 come back in here with the TYPE_DECL, not the RECORD_TYPE. */
3853 if (context && TREE_CODE (context) == RECORD_TYPE)
3854 context = TREE_CHAIN (context);
3855 }
3856 break;
3857 case FUNCTION_DECL:
3858 return TYPE_IDENTIFIER (type) ?
3859 lookup_name (TYPE_IDENTIFIER (type), 1) : NULL_TREE;
3860 break;
3861 default:
3862 my_friendly_abort (12);
3863 }
3864 }
3865 return NULL_TREE;
3866 }
3867
3868 /* Look up NAME in the current binding level and its superiors in the
3869 namespace of variables, functions and typedefs. Return a ..._DECL
3870 node of some kind representing its definition if there is only one
3871 such declaration, or return a TREE_LIST with all the overloaded
3872 definitions if there are many, or return 0 if it is undefined.
3873
3874 If PREFER_TYPE is > 0, we prefer TYPE_DECLs.
3875 If PREFER_TYPE is -2, we're being called from yylex(). (UGLY)
3876 Otherwise we prefer non-TYPE_DECLs. */
3877
3878 tree
3879 lookup_name_real (name, prefer_type, nonclass)
3880 tree name;
3881 int prefer_type, nonclass;
3882 {
3883 register tree val;
3884 int yylex = 0;
3885
3886 if (prefer_type == -2)
3887 {
3888 extern int looking_for_typename;
3889
3890 yylex = 1;
3891 prefer_type = looking_for_typename;
3892
3893 if (got_scope != NULL_TREE)
3894 {
3895 if (got_scope == void_type_node)
3896 val = IDENTIFIER_GLOBAL_VALUE (name);
3897 else if (TREE_CODE (got_scope) == TEMPLATE_TYPE_PARM
3898 /* TFIXME -- don't do this for UPTs in new model. */
3899 || TREE_CODE (got_scope) == UNINSTANTIATED_P_TYPE)
3900 {
3901 if (prefer_type > 0)
3902 val = create_nested_upt (got_scope, name);
3903 else
3904 val = NULL_TREE;
3905 }
3906 else if (! IS_AGGR_TYPE (got_scope))
3907 /* Someone else will give an error about this if needed. */
3908 val = NULL_TREE;
3909 else if (TYPE_BEING_DEFINED (got_scope))
3910 {
3911 val = IDENTIFIER_CLASS_VALUE (name);
3912 if (val && DECL_CONTEXT (val) != got_scope)
3913 {
3914 struct binding_level *b = class_binding_level;
3915 for (val = NULL_TREE; b; b = b->level_chain)
3916 {
3917 tree t = purpose_member (name, b->class_shadowed);
3918 if (t && TREE_VALUE (t)
3919 && DECL_CONTEXT (TREE_VALUE (t)) == got_scope)
3920 {
3921 val = TREE_VALUE (t);
3922 break;
3923 }
3924 }
3925 }
3926 if (val == NULL_TREE
3927 && CLASSTYPE_LOCAL_TYPEDECLS (got_scope))
3928 val = lookup_field (got_scope, name, 0, 1);
3929 }
3930 else if (got_scope == current_class_type)
3931 val = IDENTIFIER_CLASS_VALUE (name);
3932 else
3933 val = lookup_field (got_scope, name, 0, 0);
3934
3935 goto done;
3936 }
3937 }
3938
3939 if (current_binding_level != global_binding_level
3940 && IDENTIFIER_LOCAL_VALUE (name))
3941 val = IDENTIFIER_LOCAL_VALUE (name);
3942 /* In C++ class fields are between local and global scope,
3943 just before the global scope. */
3944 else if (current_class_type && ! nonclass)
3945 {
3946 val = IDENTIFIER_CLASS_VALUE (name);
3947 if (val == NULL_TREE
3948 && TYPE_BEING_DEFINED (current_class_type)
3949 && CLASSTYPE_LOCAL_TYPEDECLS (current_class_type))
3950 /* Try to find values from base classes if we are presently
3951 defining a type. We are presently only interested in
3952 TYPE_DECLs. */
3953 val = lookup_field (current_class_type, name, 0, 1);
3954
3955 /* yylex() calls this with -2, since we should never start digging for
3956 the nested name at the point where we haven't even, for example,
3957 created the COMPONENT_REF or anything like that. */
3958 if (val == NULL_TREE)
3959 val = lookup_nested_field (name, ! yylex);
3960
3961 if (val == NULL_TREE)
3962 val = IDENTIFIER_GLOBAL_VALUE (name);
3963 }
3964 else
3965 val = IDENTIFIER_GLOBAL_VALUE (name);
3966
3967 done:
3968 if (val)
3969 {
3970 if ((TREE_CODE (val) == TEMPLATE_DECL && looking_for_template)
3971 || TREE_CODE (val) == TYPE_DECL || prefer_type <= 0)
3972 return val;
3973
3974 if (IDENTIFIER_HAS_TYPE_VALUE (name))
3975 return TYPE_NAME (IDENTIFIER_TYPE_VALUE (name));
3976
3977 if (TREE_TYPE (val) == error_mark_node)
3978 return error_mark_node;
3979 }
3980
3981 return val;
3982 }
3983
3984 tree
3985 lookup_name_nonclass (name)
3986 tree name;
3987 {
3988 return lookup_name_real (name, 0, 1);
3989 }
3990
3991 tree
3992 lookup_name (name, prefer_type)
3993 tree name;
3994 int prefer_type;
3995 {
3996 return lookup_name_real (name, prefer_type, 0);
3997 }
3998
3999 /* Similar to `lookup_name' but look only at current binding level. */
4000
4001 tree
4002 lookup_name_current_level (name)
4003 tree name;
4004 {
4005 register tree t = NULL_TREE;
4006
4007 if (current_binding_level == global_binding_level)
4008 {
4009 t = IDENTIFIER_GLOBAL_VALUE (name);
4010
4011 /* extern "C" function() */
4012 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4013 t = TREE_VALUE (t);
4014 }
4015 else if (IDENTIFIER_LOCAL_VALUE (name) != NULL_TREE)
4016 {
4017 struct binding_level *b = current_binding_level;
4018 while (1)
4019 {
4020 for (t = b->names; t; t = TREE_CHAIN (t))
4021 if (DECL_NAME (t) == name)
4022 goto out;
4023 if (b->keep == 2)
4024 b = b->level_chain;
4025 else
4026 break;
4027 }
4028 out:
4029 ;
4030 }
4031
4032 return t;
4033 }
4034 \f
4035 /* Arrange for the user to get a source line number, even when the
4036 compiler is going down in flames, so that she at least has a
4037 chance of working around problems in the compiler. We used to
4038 call error(), but that let the segmentation fault continue
4039 through; now, it's much more passive by asking them to send the
4040 maintainers mail about the problem. */
4041
4042 static void
4043 signal_catch (sig)
4044 int sig;
4045 {
4046 signal (SIGSEGV, SIG_DFL);
4047 #ifdef SIGIOT
4048 signal (SIGIOT, SIG_DFL);
4049 #endif
4050 #ifdef SIGILL
4051 signal (SIGILL, SIG_DFL);
4052 #endif
4053 #ifdef SIGABRT
4054 signal (SIGABRT, SIG_DFL);
4055 #endif
4056 #ifdef SIGBUS
4057 signal (SIGBUS, SIG_DFL);
4058 #endif
4059 my_friendly_abort (0);
4060 }
4061
4062 /* Array for holding types considered "built-in". These types
4063 are output in the module in which `main' is defined. */
4064 static tree *builtin_type_tdescs_arr;
4065 static int builtin_type_tdescs_len, builtin_type_tdescs_max;
4066
4067 /* Push the declarations of builtin types into the namespace.
4068 RID_INDEX, if < RID_MAX is the index of the builtin type
4069 in the array RID_POINTERS. NAME is the name used when looking
4070 up the builtin type. TYPE is the _TYPE node for the builtin type. */
4071
4072 static void
4073 record_builtin_type (rid_index, name, type)
4074 enum rid rid_index;
4075 char *name;
4076 tree type;
4077 {
4078 tree rname = NULL_TREE, tname = NULL_TREE;
4079 tree tdecl;
4080
4081 if ((int) rid_index < (int) RID_MAX)
4082 rname = ridpointers[(int) rid_index];
4083 if (name)
4084 tname = get_identifier (name);
4085
4086 TYPE_BUILT_IN (type) = 1;
4087
4088 if (tname)
4089 {
4090 #if 0 /* not yet, should get fixed properly later */
4091 tdecl = pushdecl (make_type_decl (tname, type));
4092 #else
4093 tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
4094 #endif
4095 set_identifier_type_value (tname, NULL_TREE);
4096 if ((int) rid_index < (int) RID_MAX)
4097 IDENTIFIER_GLOBAL_VALUE (tname) = tdecl;
4098 }
4099 if (rname != NULL_TREE)
4100 {
4101 if (tname != NULL_TREE)
4102 {
4103 set_identifier_type_value (rname, NULL_TREE);
4104 IDENTIFIER_GLOBAL_VALUE (rname) = tdecl;
4105 }
4106 else
4107 {
4108 #if 0 /* not yet, should get fixed properly later */
4109 tdecl = pushdecl (make_type_decl (rname, type));
4110 #else
4111 tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
4112 #endif
4113 set_identifier_type_value (rname, NULL_TREE);
4114 }
4115 }
4116
4117 if (flag_dossier)
4118 {
4119 if (builtin_type_tdescs_len+5 >= builtin_type_tdescs_max)
4120 {
4121 builtin_type_tdescs_max *= 2;
4122 builtin_type_tdescs_arr
4123 = (tree *)xrealloc (builtin_type_tdescs_arr,
4124 builtin_type_tdescs_max * sizeof (tree));
4125 }
4126 builtin_type_tdescs_arr[builtin_type_tdescs_len++] = type;
4127 if (TREE_CODE (type) != POINTER_TYPE)
4128 {
4129 builtin_type_tdescs_arr[builtin_type_tdescs_len++]
4130 = build_pointer_type (type);
4131 builtin_type_tdescs_arr[builtin_type_tdescs_len++]
4132 = build_pointer_type (build_type_variant (type, 1, 0));
4133 }
4134 if (TREE_CODE (type) != VOID_TYPE)
4135 {
4136 builtin_type_tdescs_arr[builtin_type_tdescs_len++]
4137 = build_reference_type (type);
4138 builtin_type_tdescs_arr[builtin_type_tdescs_len++]
4139 = build_reference_type (build_type_variant (type, 1, 0));
4140 }
4141 }
4142 }
4143
4144 static void
4145 output_builtin_tdesc_entries ()
4146 {
4147 extern struct obstack permanent_obstack;
4148
4149 /* If there's more than one main in this file, don't crash. */
4150 if (builtin_type_tdescs_arr == 0)
4151 return;
4152
4153 push_obstacks (&permanent_obstack, &permanent_obstack);
4154 while (builtin_type_tdescs_len > 0)
4155 {
4156 tree type = builtin_type_tdescs_arr[--builtin_type_tdescs_len];
4157 tree tdesc = build_t_desc (type, 0);
4158 TREE_ASM_WRITTEN (tdesc) = 0;
4159 build_t_desc (type, 2);
4160 }
4161 free (builtin_type_tdescs_arr);
4162 builtin_type_tdescs_arr = 0;
4163 pop_obstacks ();
4164 }
4165
4166 /* Push overloaded decl, in global scope, with one argument so it
4167 can be used as a callback from define_function. */
4168 static void
4169 push_overloaded_decl_1 (x)
4170 tree x;
4171 {
4172 push_overloaded_decl (x, 0);
4173 }
4174
4175 /* Create the predefined scalar types of C,
4176 and some nodes representing standard constants (0, 1, (void *)0).
4177 Initialize the global binding level.
4178 Make definitions for built-in primitive functions. */
4179
4180 void
4181 init_decl_processing ()
4182 {
4183 tree decl;
4184 register tree endlink, int_endlink, double_endlink, ptr_endlink;
4185 tree fields[20];
4186 /* Either char* or void*. */
4187 tree traditional_ptr_type_node;
4188 /* Data type of memcpy. */
4189 tree memcpy_ftype;
4190 #if 0 /* Not yet. */
4191 /* Data type of strncpy. */
4192 tree strncpy_ftype;
4193 #endif
4194 int wchar_type_size;
4195 tree temp;
4196 tree array_domain_type;
4197
4198 /* Have to make these distinct before we try using them. */
4199 lang_name_cplusplus = get_identifier ("C++");
4200 lang_name_c = get_identifier ("C");
4201
4202 if (flag_ansi || pedantic)
4203 strict_prototypes_lang_c = strict_prototypes_lang_cplusplus;
4204
4205 /* Initially, C. */
4206 current_lang_name = lang_name_c;
4207
4208 current_function_decl = NULL_TREE;
4209 named_labels = NULL_TREE;
4210 named_label_uses = NULL_TREE;
4211 current_binding_level = NULL_BINDING_LEVEL;
4212 free_binding_level = NULL_BINDING_LEVEL;
4213
4214 /* Because most segmentation signals can be traced back into user
4215 code, catch them and at least give the user a chance of working
4216 around compiler bugs. */
4217 signal (SIGSEGV, signal_catch);
4218
4219 /* We will also catch aborts in the back-end through signal_catch and
4220 give the user a chance to see where the error might be, and to defeat
4221 aborts in the back-end when there have been errors previously in their
4222 code. */
4223 #ifdef SIGIOT
4224 signal (SIGIOT, signal_catch);
4225 #endif
4226 #ifdef SIGILL
4227 signal (SIGILL, signal_catch);
4228 #endif
4229 #ifdef SIGABRT
4230 signal (SIGABRT, signal_catch);
4231 #endif
4232 #ifdef SIGBUS
4233 signal (SIGBUS, signal_catch);
4234 #endif
4235
4236 gcc_obstack_init (&decl_obstack);
4237 if (flag_dossier)
4238 {
4239 builtin_type_tdescs_max = 100;
4240 builtin_type_tdescs_arr = (tree *)xmalloc (100 * sizeof (tree));
4241 }
4242
4243 /* Must lay these out before anything else gets laid out. */
4244 error_mark_node = make_node (ERROR_MARK);
4245 TREE_PERMANENT (error_mark_node) = 1;
4246 TREE_TYPE (error_mark_node) = error_mark_node;
4247 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
4248 TREE_TYPE (error_mark_list) = error_mark_node;
4249
4250 /* Make the binding_level structure for global names. */
4251 pushlevel (0);
4252 global_binding_level = current_binding_level;
4253
4254 this_identifier = get_identifier (THIS_NAME);
4255 in_charge_identifier = get_identifier (IN_CHARGE_NAME);
4256 pfn_identifier = get_identifier (VTABLE_PFN_NAME);
4257 index_identifier = get_identifier (VTABLE_INDEX_NAME);
4258 delta_identifier = get_identifier (VTABLE_DELTA_NAME);
4259 delta2_identifier = get_identifier (VTABLE_DELTA2_NAME);
4260 pfn_or_delta2_identifier = get_identifier ("__pfn_or_delta2");
4261
4262 /* Define `int' and `char' first so that dbx will output them first. */
4263
4264 integer_type_node = make_signed_type (INT_TYPE_SIZE);
4265 record_builtin_type (RID_INT, NULL_PTR, integer_type_node);
4266
4267 /* Define `char', which is like either `signed char' or `unsigned char'
4268 but not the same as either. */
4269
4270 char_type_node =
4271 (flag_signed_char
4272 ? make_signed_type (CHAR_TYPE_SIZE)
4273 : make_unsigned_type (CHAR_TYPE_SIZE));
4274 record_builtin_type (RID_CHAR, "char", char_type_node);
4275
4276 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
4277 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4278
4279 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
4280 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4281
4282 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
4283 record_builtin_type (RID_MAX, "long unsigned int", long_unsigned_type_node);
4284 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4285
4286 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
4287 record_builtin_type (RID_MAX, "long long int", long_long_integer_type_node);
4288
4289 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
4290 record_builtin_type (RID_MAX, "long long unsigned int",
4291 long_long_unsigned_type_node);
4292 record_builtin_type (RID_MAX, "long long unsigned",
4293 long_long_unsigned_type_node);
4294
4295 /* `unsigned long' is the standard type for sizeof.
4296 Traditionally, use a signed type.
4297 Note that stddef.h uses `unsigned long',
4298 and this must agree, even of long and int are the same size. */
4299 sizetype
4300 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE)));
4301 if (flag_traditional && TREE_UNSIGNED (sizetype))
4302 sizetype = signed_type (sizetype);
4303
4304 ptrdiff_type_node
4305 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE)));
4306
4307 TREE_TYPE (TYPE_SIZE (integer_type_node)) = sizetype;
4308 TREE_TYPE (TYPE_SIZE (char_type_node)) = sizetype;
4309 TREE_TYPE (TYPE_SIZE (unsigned_type_node)) = sizetype;
4310 TREE_TYPE (TYPE_SIZE (long_unsigned_type_node)) = sizetype;
4311 TREE_TYPE (TYPE_SIZE (long_integer_type_node)) = sizetype;
4312 TREE_TYPE (TYPE_SIZE (long_long_integer_type_node)) = sizetype;
4313 TREE_TYPE (TYPE_SIZE (long_long_unsigned_type_node)) = sizetype;
4314
4315 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
4316 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4317 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
4318 record_builtin_type (RID_MAX, "short unsigned int", short_unsigned_type_node);
4319 record_builtin_type (RID_MAX, "unsigned short", short_unsigned_type_node);
4320
4321 /* Define both `signed char' and `unsigned char'. */
4322 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
4323 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4324 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
4325 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4326
4327 /* These are types that type_for_size and type_for_mode use. */
4328 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
4329 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
4330 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
4331 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
4332 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
4333 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
4334 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
4335 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
4336 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
4337 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
4338 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
4339 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
4340 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
4341 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
4342 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
4343 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
4344
4345 float_type_node = make_node (REAL_TYPE);
4346 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
4347 record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node);
4348 layout_type (float_type_node);
4349
4350 double_type_node = make_node (REAL_TYPE);
4351 if (flag_short_double)
4352 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
4353 else
4354 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
4355 record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node);
4356 layout_type (double_type_node);
4357
4358 long_double_type_node = make_node (REAL_TYPE);
4359 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
4360 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4361 layout_type (long_double_type_node);
4362
4363 integer_zero_node = build_int_2 (0, 0);
4364 TREE_TYPE (integer_zero_node) = integer_type_node;
4365 integer_one_node = build_int_2 (1, 0);
4366 TREE_TYPE (integer_one_node) = integer_type_node;
4367 integer_two_node = build_int_2 (2, 0);
4368 TREE_TYPE (integer_two_node) = integer_type_node;
4369 integer_three_node = build_int_2 (3, 0);
4370 TREE_TYPE (integer_three_node) = integer_type_node;
4371
4372 bool_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
4373 TREE_SET_CODE (bool_type_node, BOOLEAN_TYPE);
4374 record_builtin_type (RID_BOOL, "bool", bool_type_node);
4375 false_node = build_int_2 (0, 0);
4376 TREE_TYPE (false_node) = bool_type_node;
4377 true_node = build_int_2 (1, 0);
4378 TREE_TYPE (true_node) = bool_type_node;
4379
4380 /* These are needed by stor-layout.c. */
4381 size_zero_node = size_int (0);
4382 size_one_node = size_int (1);
4383
4384 void_type_node = make_node (VOID_TYPE);
4385 record_builtin_type (RID_VOID, NULL_PTR, void_type_node);
4386 layout_type (void_type_node); /* Uses integer_zero_node. */
4387 void_list_node = build_tree_list (NULL_TREE, void_type_node);
4388 TREE_PARMLIST (void_list_node) = 1;
4389
4390 null_pointer_node = build_int_2 (0, 0);
4391 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
4392 layout_type (TREE_TYPE (null_pointer_node));
4393
4394 /* Used for expressions that do nothing, but are not errors. */
4395 void_zero_node = build_int_2 (0, 0);
4396 TREE_TYPE (void_zero_node) = void_type_node;
4397
4398 string_type_node = build_pointer_type (char_type_node);
4399 const_string_type_node =
4400 build_pointer_type (build_type_variant (char_type_node, 1, 0));
4401 record_builtin_type (RID_MAX, NULL_PTR, string_type_node);
4402
4403 /* Make a type to be the domain of a few array types
4404 whose domains don't really matter.
4405 200 is small enough that it always fits in size_t
4406 and large enough that it can hold most function names for the
4407 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4408 array_domain_type = build_index_type (build_int_2 (200, 0));
4409
4410 /* make a type for arrays of characters.
4411 With luck nothing will ever really depend on the length of this
4412 array type. */
4413 char_array_type_node
4414 = build_array_type (char_type_node, array_domain_type);
4415 /* Likewise for arrays of ints. */
4416 int_array_type_node
4417 = build_array_type (integer_type_node, array_domain_type);
4418
4419 /* This is just some anonymous class type. Nobody should ever
4420 need to look inside this envelope. */
4421 class_star_type_node = build_pointer_type (make_lang_type (RECORD_TYPE));
4422
4423 default_function_type
4424 = build_function_type (integer_type_node, NULL_TREE);
4425 build_pointer_type (default_function_type);
4426
4427 ptr_type_node = build_pointer_type (void_type_node);
4428 const_ptr_type_node =
4429 build_pointer_type (build_type_variant (void_type_node, 1, 0));
4430 record_builtin_type (RID_MAX, NULL_PTR, ptr_type_node);
4431 endlink = void_list_node;
4432 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
4433 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
4434 ptr_endlink = tree_cons (NULL_TREE, ptr_type_node, endlink);
4435
4436 double_ftype_double
4437 = build_function_type (double_type_node, double_endlink);
4438
4439 double_ftype_double_double
4440 = build_function_type (double_type_node,
4441 tree_cons (NULL_TREE, double_type_node,
4442 double_endlink));
4443
4444 int_ftype_int
4445 = build_function_type (integer_type_node, int_endlink);
4446
4447 long_ftype_long
4448 = build_function_type (long_integer_type_node,
4449 tree_cons (NULL_TREE, long_integer_type_node,
4450 endlink));
4451
4452 void_ftype_ptr_ptr_int
4453 = build_function_type (void_type_node,
4454 tree_cons (NULL_TREE, ptr_type_node,
4455 tree_cons (NULL_TREE, ptr_type_node,
4456 int_endlink)));
4457
4458 int_ftype_cptr_cptr_sizet
4459 = build_function_type (integer_type_node,
4460 tree_cons (NULL_TREE, const_ptr_type_node,
4461 tree_cons (NULL_TREE, const_ptr_type_node,
4462 tree_cons (NULL_TREE,
4463 sizetype,
4464 endlink))));
4465
4466 void_ftype_ptr_int_int
4467 = build_function_type (void_type_node,
4468 tree_cons (NULL_TREE, ptr_type_node,
4469 tree_cons (NULL_TREE, integer_type_node,
4470 int_endlink)));
4471
4472 string_ftype_ptr_ptr /* strcpy prototype */
4473 = build_function_type (string_type_node,
4474 tree_cons (NULL_TREE, string_type_node,
4475 tree_cons (NULL_TREE,
4476 const_string_type_node,
4477 endlink)));
4478
4479 #if 0
4480 /* Not yet. */
4481 strncpy_ftype /* strncpy prototype */
4482 = build_function_type (string_type_node,
4483 tree_cons (NULL_TREE, string_type_node,
4484 tree_cons (NULL_TREE, const_string_type_node,
4485 tree_cons (NULL_TREE,
4486 sizetype,
4487 endlink))));
4488 #endif
4489
4490 int_ftype_string_string /* strcmp prototype */
4491 = build_function_type (integer_type_node,
4492 tree_cons (NULL_TREE, const_string_type_node,
4493 tree_cons (NULL_TREE,
4494 const_string_type_node,
4495 endlink)));
4496
4497 sizet_ftype_string /* strlen prototype */
4498 = build_function_type (sizetype,
4499 tree_cons (NULL_TREE, const_string_type_node,
4500 endlink));
4501
4502 traditional_ptr_type_node
4503 = (flag_traditional ? string_type_node : ptr_type_node);
4504
4505 memcpy_ftype /* memcpy prototype */
4506 = build_function_type (traditional_ptr_type_node,
4507 tree_cons (NULL_TREE, ptr_type_node,
4508 tree_cons (NULL_TREE, const_ptr_type_node,
4509 tree_cons (NULL_TREE,
4510 sizetype,
4511 endlink))));
4512
4513 if (flag_huge_objects)
4514 delta_type_node = long_integer_type_node;
4515 else
4516 delta_type_node = short_integer_type_node;
4517
4518 builtin_function ("__builtin_constant_p", int_ftype_int,
4519 BUILT_IN_CONSTANT_P, NULL_PTR);
4520
4521 builtin_return_address_fndecl =
4522 builtin_function ("__builtin_return_address",
4523 build_function_type (ptr_type_node,
4524 tree_cons (NULL_TREE,
4525 unsigned_type_node,
4526 endlink)),
4527 BUILT_IN_RETURN_ADDRESS, NULL_PTR);
4528
4529 builtin_function ("__builtin_frame_address",
4530 build_function_type (ptr_type_node,
4531 tree_cons (NULL_TREE,
4532 unsigned_type_node,
4533 endlink)),
4534 BUILT_IN_FRAME_ADDRESS, NULL_PTR);
4535
4536
4537 builtin_function ("__builtin_alloca",
4538 build_function_type (ptr_type_node,
4539 tree_cons (NULL_TREE,
4540 sizetype,
4541 endlink)),
4542 BUILT_IN_ALLOCA, "alloca");
4543 #if 0
4544 builtin_function ("alloca",
4545 build_function_type (ptr_type_node,
4546 tree_cons (NULL_TREE,
4547 sizetype,
4548 endlink)),
4549 BUILT_IN_ALLOCA, NULL_PTR);
4550 #endif
4551
4552 builtin_function ("__builtin_abs", int_ftype_int,
4553 BUILT_IN_ABS, NULL_PTR);
4554 builtin_function ("__builtin_fabs", double_ftype_double,
4555 BUILT_IN_FABS, NULL_PTR);
4556 builtin_function ("__builtin_labs", long_ftype_long,
4557 BUILT_IN_LABS, NULL_PTR);
4558 builtin_function ("__builtin_ffs", int_ftype_int,
4559 BUILT_IN_FFS, NULL_PTR);
4560 builtin_function ("__builtin_fsqrt", double_ftype_double,
4561 BUILT_IN_FSQRT, NULL_PTR);
4562 builtin_function ("__builtin_sin", double_ftype_double,
4563 BUILT_IN_SIN, "sin");
4564 builtin_function ("__builtin_cos", double_ftype_double,
4565 BUILT_IN_COS, "cos");
4566 builtin_function ("__builtin_saveregs",
4567 build_function_type (ptr_type_node, NULL_TREE),
4568 BUILT_IN_SAVEREGS, NULL_PTR);
4569 /* EXPAND_BUILTIN_VARARGS is obsolete. */
4570 #if 0
4571 builtin_function ("__builtin_varargs",
4572 build_function_type (ptr_type_node,
4573 tree_cons (NULL_TREE,
4574 integer_type_node,
4575 endlink)),
4576 BUILT_IN_VARARGS, NULL_PTR);
4577 #endif
4578 builtin_function ("__builtin_classify_type", default_function_type,
4579 BUILT_IN_CLASSIFY_TYPE, NULL_PTR);
4580 builtin_function ("__builtin_next_arg",
4581 build_function_type (ptr_type_node, NULL_TREE),
4582 BUILT_IN_NEXT_ARG, NULL_PTR);
4583 builtin_function ("__builtin_args_info",
4584 build_function_type (integer_type_node,
4585 tree_cons (NULL_TREE,
4586 integer_type_node,
4587 endlink)),
4588 BUILT_IN_ARGS_INFO, NULL_PTR);
4589
4590 /* Untyped call and return. */
4591 builtin_function ("__builtin_apply_args",
4592 build_function_type (ptr_type_node, NULL_TREE),
4593 BUILT_IN_APPLY_ARGS, NULL_PTR);
4594
4595 temp = tree_cons (NULL_TREE,
4596 build_pointer_type (build_function_type (void_type_node,
4597 NULL_TREE)),
4598 tree_cons (NULL_TREE,
4599 ptr_type_node,
4600 tree_cons (NULL_TREE,
4601 sizetype,
4602 endlink)));
4603 builtin_function ("__builtin_apply",
4604 build_function_type (ptr_type_node, temp),
4605 BUILT_IN_APPLY, NULL_PTR);
4606 builtin_function ("__builtin_return",
4607 build_function_type (void_type_node,
4608 tree_cons (NULL_TREE,
4609 ptr_type_node,
4610 endlink)),
4611 BUILT_IN_RETURN, NULL_PTR);
4612
4613 /* Currently under experimentation. */
4614 builtin_function ("__builtin_memcpy", memcpy_ftype,
4615 BUILT_IN_MEMCPY, "memcpy");
4616 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
4617 BUILT_IN_MEMCMP, "memcmp");
4618 builtin_function ("__builtin_strcmp", int_ftype_string_string,
4619 BUILT_IN_STRCMP, "strcmp");
4620 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
4621 BUILT_IN_STRCPY, "strcpy");
4622 #if 0
4623 /* Not yet. */
4624 builtin_function ("__builtin_strncpy", strncpy_ftype,
4625 BUILT_IN_STRNCPY, "strncpy");
4626 #endif
4627 builtin_function ("__builtin_strlen", sizet_ftype_string,
4628 BUILT_IN_STRLEN, "strlen");
4629
4630 if (!flag_no_builtin)
4631 {
4632 #if 0 /* These do not work well with libg++. */
4633 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
4634 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS, NULL_PTR);
4635 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS, NULL_PTR);
4636 #endif
4637 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY, NULL_PTR);
4638 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
4639 NULL_PTR);
4640 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP, NULL_PTR);
4641 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
4642 NULL_PTR);
4643 #if 0
4644 /* Not yet. */
4645 builtin_function ("strncpy", strncpy_ftype, BUILT_IN_STRNCPY, NULL_PTR);
4646 #endif
4647 builtin_function ("strlen", sizet_ftype_string, BUILT_IN_STRLEN, NULL_PTR);
4648 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN, NULL_PTR);
4649 builtin_function ("cos", double_ftype_double, BUILT_IN_COS, NULL_PTR);
4650 }
4651
4652 #if 0
4653 /* Support for these has not been written in either expand_builtin
4654 or build_function_call. */
4655 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV, 0);
4656 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV, 0);
4657 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
4658 0);
4659 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL, 0);
4660 builtin_function ("__builtin_fmod", double_ftype_double_double,
4661 BUILT_IN_FMOD, 0);
4662 builtin_function ("__builtin_frem", double_ftype_double_double,
4663 BUILT_IN_FREM, 0);
4664 builtin_function ("__builtin_memset", ptr_ftype_ptr_int_int, BUILT_IN_MEMSET,
4665 0);
4666 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
4667 0);
4668 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
4669 0);
4670 #endif
4671
4672 /* C++ extensions */
4673
4674 unknown_type_node = make_node (UNKNOWN_TYPE);
4675 #if 0 /* not yet, should get fixed properly later */
4676 pushdecl (make_type_decl (get_identifier ("unknown type"),
4677 unknown_type_node));
4678 #else
4679 decl = pushdecl (build_decl (TYPE_DECL, get_identifier ("unknown type"),
4680 unknown_type_node));
4681 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4682 DECL_IGNORED_P (decl) = 1;
4683 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4684 #endif
4685 TYPE_SIZE (unknown_type_node) = TYPE_SIZE (void_type_node);
4686 TYPE_ALIGN (unknown_type_node) = 1;
4687 TYPE_MODE (unknown_type_node) = TYPE_MODE (void_type_node);
4688 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4689 TREE_TYPE (unknown_type_node) = unknown_type_node;
4690 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same result. */
4691 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4692 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4693
4694 /* This is for handling opaque types in signatures. */
4695 opaque_type_node = copy_node (ptr_type_node);
4696 TYPE_MAIN_VARIANT (opaque_type_node) = opaque_type_node;
4697 record_builtin_type (RID_MAX, 0, opaque_type_node);
4698
4699 /* This is special for C++ so functions can be overloaded. */
4700 wchar_type_node
4701 = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (WCHAR_TYPE)));
4702 wchar_type_size = TYPE_PRECISION (wchar_type_node);
4703 signed_wchar_type_node = make_signed_type (wchar_type_size);
4704 unsigned_wchar_type_node = make_unsigned_type (wchar_type_size);
4705 wchar_type_node
4706 = TREE_UNSIGNED (wchar_type_node)
4707 ? unsigned_wchar_type_node
4708 : signed_wchar_type_node;
4709 record_builtin_type (RID_WCHAR, "__wchar_t", wchar_type_node);
4710
4711 /* This is for wide string constants. */
4712 wchar_array_type_node
4713 = build_array_type (wchar_type_node, array_domain_type);
4714
4715 /* This is a hack that should go away when we deliver the
4716 real gc code. */
4717 if (flag_gc)
4718 {
4719 builtin_function ("__gc_main", default_function_type, NOT_BUILT_IN, 0);
4720 pushdecl (lookup_name (get_identifier ("__gc_main"), 0));
4721 }
4722
4723 if (flag_vtable_thunks)
4724 {
4725 /* Make sure we get a unique function type, so we can give
4726 its pointer type a name. (This wins for gdb.) */
4727 tree vfunc_type = make_node (FUNCTION_TYPE);
4728 TREE_TYPE (vfunc_type) = integer_type_node;
4729 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4730 layout_type (vfunc_type);
4731
4732 vtable_entry_type = build_pointer_type (vfunc_type);
4733 }
4734 else
4735 {
4736 vtable_entry_type = make_lang_type (RECORD_TYPE);
4737 fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier,
4738 delta_type_node);
4739 fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier,
4740 delta_type_node);
4741 fields[2] = build_lang_field_decl (FIELD_DECL, pfn_identifier,
4742 ptr_type_node);
4743 finish_builtin_type (vtable_entry_type, VTBL_PTR_TYPE, fields, 2,
4744 double_type_node);
4745
4746 /* Make this part of an invisible union. */
4747 fields[3] = copy_node (fields[2]);
4748 TREE_TYPE (fields[3]) = delta_type_node;
4749 DECL_NAME (fields[3]) = delta2_identifier;
4750 DECL_MODE (fields[3]) = TYPE_MODE (delta_type_node);
4751 DECL_SIZE (fields[3]) = TYPE_SIZE (delta_type_node);
4752 TREE_UNSIGNED (fields[3]) = 0;
4753 TREE_CHAIN (fields[2]) = fields[3];
4754 vtable_entry_type = build_type_variant (vtable_entry_type, 1, 0);
4755 }
4756 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
4757
4758 vtbl_type_node
4759 = build_array_type (vtable_entry_type, NULL_TREE);
4760 layout_type (vtbl_type_node);
4761 vtbl_type_node = c_build_type_variant (vtbl_type_node, 1, 0);
4762 record_builtin_type (RID_MAX, NULL_PTR, vtbl_type_node);
4763
4764 /* Simplify life by making a "sigtable_entry_type". Give its
4765 fields names so that the debugger can use them. */
4766
4767 if (flag_handle_signatures)
4768 {
4769 sigtable_entry_type = make_lang_type (RECORD_TYPE);
4770 fields[0] = build_lang_field_decl (FIELD_DECL,
4771 get_identifier (SIGTABLE_CODE_NAME),
4772 short_integer_type_node);
4773 fields[1] = build_lang_field_decl (FIELD_DECL,
4774 get_identifier (SIGTABLE_OFFSET_NAME),
4775 short_integer_type_node);
4776 fields[2] = build_lang_field_decl (FIELD_DECL,
4777 get_identifier (SIGTABLE_PFN_NAME),
4778 ptr_type_node);
4779 finish_builtin_type (sigtable_entry_type, SIGTABLE_PTR_TYPE, fields, 2,
4780 double_type_node);
4781 sigtable_entry_type = build_type_variant (sigtable_entry_type, 1, 0);
4782 record_builtin_type (RID_MAX, SIGTABLE_PTR_TYPE, sigtable_entry_type);
4783 }
4784
4785 if (flag_dossier)
4786 {
4787 /* Must build __t_desc type. Currently, type descriptors look like this:
4788
4789 struct __t_desc
4790 {
4791 const char *name;
4792 int size;
4793 int bits;
4794 struct __t_desc *points_to;
4795 int ivars_count, meths_count;
4796 struct __i_desc *ivars[];
4797 struct __m_desc *meths[];
4798 struct __t_desc *parents[];
4799 struct __t_desc *vbases[];
4800 int offsets[];
4801 };
4802
4803 ...as per Linton's paper. */
4804
4805 __t_desc_type_node = make_lang_type (RECORD_TYPE);
4806 __i_desc_type_node = make_lang_type (RECORD_TYPE);
4807 __m_desc_type_node = make_lang_type (RECORD_TYPE);
4808 __t_desc_array_type =
4809 build_array_type (TYPE_POINTER_TO (__t_desc_type_node), NULL_TREE);
4810 __i_desc_array_type =
4811 build_array_type (TYPE_POINTER_TO (__i_desc_type_node), NULL_TREE);
4812 __m_desc_array_type =
4813 build_array_type (TYPE_POINTER_TO (__m_desc_type_node), NULL_TREE);
4814
4815 fields[0] = build_lang_field_decl (FIELD_DECL, get_identifier ("name"),
4816 string_type_node);
4817 fields[1] = build_lang_field_decl (FIELD_DECL, get_identifier ("size"),
4818 unsigned_type_node);
4819 fields[2] = build_lang_field_decl (FIELD_DECL, get_identifier ("bits"),
4820 unsigned_type_node);
4821 fields[3] = build_lang_field_decl (FIELD_DECL,
4822 get_identifier ("points_to"),
4823 TYPE_POINTER_TO (__t_desc_type_node));
4824 fields[4] = build_lang_field_decl (FIELD_DECL,
4825 get_identifier ("ivars_count"),
4826 integer_type_node);
4827 fields[5] = build_lang_field_decl (FIELD_DECL,
4828 get_identifier ("meths_count"),
4829 integer_type_node);
4830 fields[6] = build_lang_field_decl (FIELD_DECL, get_identifier ("ivars"),
4831 build_pointer_type (__i_desc_array_type));
4832 fields[7] = build_lang_field_decl (FIELD_DECL, get_identifier ("meths"),
4833 build_pointer_type (__m_desc_array_type));
4834 fields[8] = build_lang_field_decl (FIELD_DECL, get_identifier ("parents"),
4835 build_pointer_type (__t_desc_array_type));
4836 fields[9] = build_lang_field_decl (FIELD_DECL, get_identifier ("vbases"),
4837 build_pointer_type (__t_desc_array_type));
4838 fields[10] = build_lang_field_decl (FIELD_DECL, get_identifier ("offsets"),
4839 build_pointer_type (integer_type_node));
4840 finish_builtin_type (__t_desc_type_node, "__t_desc", fields, 10, integer_type_node);
4841
4842 /* ivar descriptors look like this:
4843
4844 struct __i_desc
4845 {
4846 const char *name;
4847 int offset;
4848 struct __t_desc *type;
4849 };
4850 */
4851
4852 fields[0] = build_lang_field_decl (FIELD_DECL, get_identifier ("name"),
4853 string_type_node);
4854 fields[1] = build_lang_field_decl (FIELD_DECL, get_identifier ("offset"),
4855 integer_type_node);
4856 fields[2] = build_lang_field_decl (FIELD_DECL, get_identifier ("type"),
4857 TYPE_POINTER_TO (__t_desc_type_node));
4858 finish_builtin_type (__i_desc_type_node, "__i_desc", fields, 2,
4859 integer_type_node);
4860
4861 /* method descriptors look like this:
4862
4863 struct __m_desc
4864 {
4865 const char *name;
4866 int vindex;
4867 struct __t_desc *vcontext;
4868 struct __t_desc *return_type;
4869 void (*address)();
4870 short parm_count;
4871 short required_parms;
4872 struct __t_desc *parm_types[];
4873 };
4874 */
4875
4876 fields[0] = build_lang_field_decl (FIELD_DECL, get_identifier ("name"),
4877 string_type_node);
4878 fields[1] = build_lang_field_decl (FIELD_DECL, get_identifier ("vindex"),
4879 integer_type_node);
4880 fields[2] = build_lang_field_decl (FIELD_DECL, get_identifier ("vcontext"),
4881 TYPE_POINTER_TO (__t_desc_type_node));
4882 fields[3] = build_lang_field_decl (FIELD_DECL, get_identifier ("return_type"),
4883 TYPE_POINTER_TO (__t_desc_type_node));
4884 fields[4] = build_lang_field_decl (FIELD_DECL, get_identifier ("address"),
4885 build_pointer_type (default_function_type));
4886 fields[5] = build_lang_field_decl (FIELD_DECL, get_identifier ("parm_count"),
4887 short_integer_type_node);
4888 fields[6] = build_lang_field_decl (FIELD_DECL, get_identifier ("required_parms"),
4889 short_integer_type_node);
4890 fields[7] = build_lang_field_decl (FIELD_DECL, get_identifier ("parm_types"),
4891 build_pointer_type (build_array_type (TYPE_POINTER_TO (__t_desc_type_node), NULL_TREE)));
4892 finish_builtin_type (__m_desc_type_node, "__m_desc", fields, 7,
4893 integer_type_node);
4894 }
4895
4896 /* Now, C++. */
4897 current_lang_name = lang_name_cplusplus;
4898 if (flag_dossier)
4899 {
4900 int i = builtin_type_tdescs_len;
4901 while (i > 0)
4902 {
4903 tree tdesc = build_t_desc (builtin_type_tdescs_arr[--i], 0);
4904 TREE_ASM_WRITTEN (tdesc) = 1;
4905 TREE_PUBLIC (TREE_OPERAND (tdesc, 0)) = 1;
4906 }
4907 }
4908
4909 auto_function (ansi_opname[(int) NEW_EXPR],
4910 build_function_type (ptr_type_node,
4911 tree_cons (NULL_TREE, sizetype,
4912 void_list_node)),
4913 NOT_BUILT_IN);
4914 auto_function (ansi_opname[(int) VEC_NEW_EXPR],
4915 build_function_type (ptr_type_node,
4916 tree_cons (NULL_TREE, sizetype,
4917 void_list_node)),
4918 NOT_BUILT_IN);
4919 auto_function (ansi_opname[(int) DELETE_EXPR],
4920 build_function_type (void_type_node,
4921 tree_cons (NULL_TREE, ptr_type_node,
4922 void_list_node)),
4923 NOT_BUILT_IN);
4924 auto_function (ansi_opname[(int) VEC_DELETE_EXPR],
4925 build_function_type (void_type_node,
4926 tree_cons (NULL_TREE, ptr_type_node,
4927 void_list_node)),
4928 NOT_BUILT_IN);
4929
4930 abort_fndecl
4931 = define_function ("abort",
4932 build_function_type (void_type_node, void_list_node),
4933 NOT_BUILT_IN, 0, 0);
4934
4935 /* Perform other language dependent initializations. */
4936 init_class_processing ();
4937 init_init_processing ();
4938 init_search_processing ();
4939
4940 if (flag_handle_exceptions)
4941 init_exception_processing ();
4942 if (flag_gc)
4943 init_gc_processing ();
4944 if (flag_no_inline)
4945 {
4946 flag_inline_functions = 0;
4947 #if 0
4948 /* This causes uneccessary emission of inline functions. */
4949 flag_default_inline = 0;
4950 #endif
4951 }
4952 if (flag_cadillac)
4953 init_cadillac ();
4954
4955 /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
4956 declare_function_name ();
4957
4958 /* Prepare to check format strings against argument lists. */
4959 init_function_format_info ();
4960 }
4961
4962 /* Make a definition for a builtin function named NAME and whose data type
4963 is TYPE. TYPE should be a function type with argument types.
4964 FUNCTION_CODE tells later passes how to compile calls to this function.
4965 See tree.h for its possible values.
4966
4967 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
4968 the name to be called if we can't opencode the function. */
4969
4970 tree
4971 define_function (name, type, function_code, pfn, library_name)
4972 char *name;
4973 tree type;
4974 enum built_in_function function_code;
4975 void (*pfn)();
4976 char *library_name;
4977 {
4978 tree decl = build_lang_decl (FUNCTION_DECL, get_identifier (name), type);
4979 DECL_EXTERNAL (decl) = 1;
4980 TREE_PUBLIC (decl) = 1;
4981
4982 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
4983 we cannot change DECL_ASSEMBLER_NAME until we have installed this
4984 function in the namespace. */
4985 if (pfn) (*pfn) (decl);
4986 if (library_name)
4987 DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
4988 make_function_rtl (decl);
4989 if (function_code != NOT_BUILT_IN)
4990 {
4991 DECL_BUILT_IN (decl) = 1;
4992 DECL_FUNCTION_CODE (decl) = function_code;
4993 }
4994 return decl;
4995 }
4996 \f
4997 /* Called when a declaration is seen that contains no names to declare.
4998 If its type is a reference to a structure, union or enum inherited
4999 from a containing scope, shadow that tag name for the current scope
5000 with a forward reference.
5001 If its type defines a new named structure or union
5002 or defines an enum, it is valid but we need not do anything here.
5003 Otherwise, it is an error.
5004
5005 C++: may have to grok the declspecs to learn about static,
5006 complain for anonymous unions. */
5007
5008 void
5009 shadow_tag (declspecs)
5010 tree declspecs;
5011 {
5012 int found_tag = 0;
5013 int warned = 0;
5014 tree ob_modifier = NULL_TREE;
5015 register tree link;
5016 register enum tree_code code, ok_code = ERROR_MARK;
5017 register tree t = NULL_TREE;
5018
5019 for (link = declspecs; link; link = TREE_CHAIN (link))
5020 {
5021 register tree value = TREE_VALUE (link);
5022
5023 code = TREE_CODE (value);
5024 if (IS_AGGR_TYPE_CODE (code) || code == ENUMERAL_TYPE)
5025 {
5026 register tree name = TYPE_NAME (value);
5027
5028 if (code == ENUMERAL_TYPE && TYPE_SIZE (value) == 0)
5029 cp_error ("forward declaration of `%#T'", value);
5030
5031 if (name == NULL_TREE)
5032 name = lookup_tag_reverse (value, NULL_TREE);
5033
5034 if (name && TREE_CODE (name) == TYPE_DECL)
5035 name = DECL_NAME (name);
5036
5037 t = lookup_tag (code, name, inner_binding_level, 1);
5038
5039 if (t == NULL_TREE)
5040 {
5041 push_obstacks (&permanent_obstack, &permanent_obstack);
5042 if (IS_AGGR_TYPE_CODE (code))
5043 t = make_lang_type (code);
5044 else
5045 t = make_node (code);
5046 pushtag (name, t, 0);
5047 pop_obstacks ();
5048 ok_code = code;
5049 }
5050 else if (name != NULL_TREE || code == ENUMERAL_TYPE)
5051 ok_code = code;
5052
5053 if (ok_code != ERROR_MARK)
5054 found_tag++;
5055 else
5056 {
5057 if (!warned)
5058 pedwarn ("useless keyword or type name in declaration");
5059 warned = 1;
5060 }
5061 }
5062 else if (value == ridpointers[(int) RID_STATIC]
5063 || value == ridpointers[(int) RID_EXTERN]
5064 || value == ridpointers[(int) RID_AUTO]
5065 || value == ridpointers[(int) RID_REGISTER])
5066 ob_modifier = value;
5067 }
5068
5069 /* This is where the variables in an anonymous union are
5070 declared. An anonymous union declaration looks like:
5071 union { ... } ;
5072 because there is no declarator after the union, the parser
5073 sends that declaration here. */
5074 if (ok_code == UNION_TYPE
5075 && t != NULL_TREE
5076 && ((TREE_CODE (TYPE_NAME (t)) == IDENTIFIER_NODE
5077 && ANON_AGGRNAME_P (TYPE_NAME (t)))
5078 || (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
5079 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))))
5080 {
5081 /* ANSI C++ June 5 1992 WP 9.5.3. Anonymous unions may not have
5082 function members. */
5083 if (TYPE_FIELDS (t))
5084 {
5085 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
5086 NULL_TREE);
5087 finish_anon_union (decl);
5088 }
5089 else
5090 error ("anonymous union cannot have a function member");
5091 }
5092 else
5093 {
5094 /* Anonymous unions are objects, that's why we only check for
5095 inappropriate specifiers in this branch. */
5096 if (ob_modifier)
5097 cp_error ("`%D' can only be specified for objects and functions",
5098 ob_modifier);
5099
5100 if (ok_code == RECORD_TYPE
5101 && found_tag == 1
5102 && TYPE_LANG_SPECIFIC (t)
5103 && CLASSTYPE_DECLARED_EXCEPTION (t))
5104 {
5105 if (TYPE_SIZE (t))
5106 cp_error ("redeclaration of exception `%T'", t);
5107 else
5108 {
5109 tree ename, decl;
5110
5111 push_obstacks (&permanent_obstack, &permanent_obstack);
5112
5113 pushclass (t, 0);
5114
5115 ename = TYPE_NAME (t);
5116 if (TREE_CODE (ename) == TYPE_DECL)
5117 ename = DECL_NAME (ename);
5118 decl = build_lang_field_decl (VAR_DECL, ename, t);
5119
5120 pop_obstacks ();
5121 }
5122 }
5123 else if (found_tag == 0)
5124 pedwarn ("abstract declarator used as declaration");
5125 else if (!warned && found_tag > 1)
5126 pedwarn ("multiple types in one declaration");
5127 }
5128 }
5129 \f
5130 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5131
5132 tree
5133 groktypename (typename)
5134 tree typename;
5135 {
5136 if (TREE_CODE (typename) != TREE_LIST)
5137 return typename;
5138 return grokdeclarator (TREE_VALUE (typename),
5139 TREE_PURPOSE (typename),
5140 TYPENAME, 0, NULL_TREE);
5141 }
5142
5143 /* Decode a declarator in an ordinary declaration or data definition.
5144 This is called as soon as the type information and variable name
5145 have been parsed, before parsing the initializer if any.
5146 Here we create the ..._DECL node, fill in its type,
5147 and put it on the list of decls for the current context.
5148 The ..._DECL node is returned as the value.
5149
5150 Exception: for arrays where the length is not specified,
5151 the type is left null, to be filled in by `finish_decl'.
5152
5153 Function definitions do not come here; they go to start_function
5154 instead. However, external and forward declarations of functions
5155 do go through here. Structure field declarations are done by
5156 grokfield and not through here. */
5157
5158 /* Set this to zero to debug not using the temporary obstack
5159 to parse initializers. */
5160 int debug_temp_inits = 1;
5161
5162 tree
5163 start_decl (declarator, declspecs, initialized, raises)
5164 tree declarator, declspecs;
5165 int initialized;
5166 tree raises;
5167 {
5168 register tree decl;
5169 register tree type, tem;
5170 tree context;
5171 extern int have_extern_spec;
5172 extern int used_extern_spec;
5173
5174 int init_written = initialized;
5175
5176 /* This should only be done once on the top most decl. */
5177 if (have_extern_spec && !used_extern_spec)
5178 {
5179 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"),
5180 declspecs);
5181 used_extern_spec = 1;
5182 }
5183
5184 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, raises);
5185 if (decl == NULL_TREE || decl == void_type_node)
5186 return NULL_TREE;
5187
5188 type = TREE_TYPE (decl);
5189
5190 /* Don't lose if destructors must be executed at file-level. */
5191 if (TREE_STATIC (decl)
5192 && TYPE_NEEDS_DESTRUCTOR (type)
5193 && !TREE_PERMANENT (decl))
5194 {
5195 push_obstacks (&permanent_obstack, &permanent_obstack);
5196 decl = copy_node (decl);
5197 if (TREE_CODE (type) == ARRAY_TYPE)
5198 {
5199 tree itype = TYPE_DOMAIN (type);
5200 if (itype && ! TREE_PERMANENT (itype))
5201 {
5202 itype = build_index_type (copy_to_permanent (TYPE_MAX_VALUE (itype)));
5203 type = build_cplus_array_type (TREE_TYPE (type), itype);
5204 TREE_TYPE (decl) = type;
5205 }
5206 }
5207 pop_obstacks ();
5208 }
5209
5210 /* Interesting work for this is done in `finish_exception_decl'. */
5211 if (TREE_CODE (type) == RECORD_TYPE
5212 && CLASSTYPE_DECLARED_EXCEPTION (type))
5213 return decl;
5214
5215 /* Corresponding pop_obstacks is done in `finish_decl'. */
5216 push_obstacks_nochange ();
5217
5218 context
5219 = (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
5220 ? DECL_CLASS_CONTEXT (decl)
5221 : DECL_CONTEXT (decl);
5222
5223 if (processing_template_decl)
5224 {
5225 tree d;
5226 if (TREE_CODE (decl) == FUNCTION_DECL)
5227 {
5228 /* Declarator is a call_expr; extract arguments from it, since
5229 grokdeclarator didn't do it. */
5230 tree args;
5231 args = copy_to_permanent (last_function_parms);
5232 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
5233 {
5234 tree t = TREE_TYPE (decl);
5235
5236 t = TYPE_METHOD_BASETYPE (t); /* type method belongs to */
5237 if (TREE_CODE (t) != UNINSTANTIATED_P_TYPE)
5238 {
5239 t = build_pointer_type (t); /* base type of `this' */
5240 #if 1
5241 /* I suspect this is wrong. */
5242 t = build_type_variant (t, flag_this_is_variable <= 0,
5243 0); /* type of `this' */
5244 #else
5245 t = build_type_variant (t, 0, 0); /* type of `this' */
5246 #endif
5247 t = build (PARM_DECL, t, this_identifier);
5248 TREE_CHAIN (t) = args;
5249 args = t;
5250 }
5251 }
5252 DECL_ARGUMENTS (decl) = args;
5253 }
5254 d = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), TREE_TYPE (decl));
5255 if (interface_unknown && flag_external_templates
5256 && ! DECL_IN_SYSTEM_HEADER (decl))
5257 warn_if_unknown_interface ();
5258 TREE_PUBLIC (d) = TREE_PUBLIC (decl) =
5259 flag_external_templates && !interface_unknown;
5260 TREE_STATIC (d) = TREE_STATIC (decl);
5261 DECL_EXTERNAL (d) = (DECL_EXTERNAL (decl)
5262 && !(context && !DECL_THIS_EXTERN (decl)));
5263 DECL_TEMPLATE_RESULT (d) = decl;
5264 decl = d;
5265 }
5266
5267 /* If this type of object needs a cleanup, and control may
5268 jump past it, make a new binding level so that it is cleaned
5269 up only when it is initialized first. */
5270 if (TYPE_NEEDS_DESTRUCTOR (type)
5271 && current_binding_level->more_cleanups_ok == 0)
5272 pushlevel_temporary (1);
5273
5274 if (initialized)
5275 /* Is it valid for this decl to have an initializer at all?
5276 If not, set INITIALIZED to zero, which will indirectly
5277 tell `finish_decl' to ignore the initializer once it is parsed. */
5278 switch (TREE_CODE (decl))
5279 {
5280 case TYPE_DECL:
5281 /* typedef foo = bar means give foo the same type as bar.
5282 We haven't parsed bar yet, so `finish_decl' will fix that up.
5283 Any other case of an initialization in a TYPE_DECL is an error. */
5284 if (pedantic || list_length (declspecs) > 1)
5285 {
5286 cp_error ("typedef `%D' is initialized", decl);
5287 initialized = 0;
5288 }
5289 break;
5290
5291 case FUNCTION_DECL:
5292 cp_error ("function `%#D' is initialized like a variable", decl);
5293 initialized = 0;
5294 break;
5295
5296 default:
5297 /* Don't allow initializations for incomplete types except for
5298 arrays which might be completed by the initialization. */
5299 if (type == error_mark_node)
5300 ; /* Don't complain again. */
5301 else if (TYPE_SIZE (type) != NULL_TREE)
5302 ; /* A complete type is ok. */
5303 else if (TREE_CODE (type) != ARRAY_TYPE)
5304 {
5305 cp_error ("variable `%#D' has initializer but incomplete type",
5306 decl);
5307 initialized = 0;
5308 }
5309 else if (TYPE_SIZE (TREE_TYPE (type)) == NULL_TREE)
5310 {
5311 cp_error ("elements of array `%#D' have incomplete type", decl);
5312 initialized = 0;
5313 }
5314 }
5315
5316 if (!initialized
5317 && TREE_CODE (decl) != TYPE_DECL
5318 && TREE_CODE (decl) != TEMPLATE_DECL
5319 && IS_AGGR_TYPE (type) && ! DECL_EXTERNAL (decl))
5320 {
5321 if (TYPE_SIZE (type) == NULL_TREE)
5322 {
5323 cp_error ("aggregate `%#D' has incomplete type and cannot be initialized",
5324 decl);
5325 /* Change the type so that assemble_variable will give
5326 DECL an rtl we can live with: (mem (const_int 0)). */
5327 TREE_TYPE (decl) = error_mark_node;
5328 type = error_mark_node;
5329 }
5330 else
5331 {
5332 /* If any base type in the hierarchy of TYPE needs a constructor,
5333 then we set initialized to 1. This way any nodes which are
5334 created for the purposes of initializing this aggregate
5335 will live as long as it does. This is necessary for global
5336 aggregates which do not have their initializers processed until
5337 the end of the file. */
5338 initialized = TYPE_NEEDS_CONSTRUCTING (type);
5339 }
5340 }
5341
5342 if (initialized)
5343 {
5344 if (current_binding_level != global_binding_level
5345 && DECL_EXTERNAL (decl))
5346 cp_warning ("declaration of `%#D' has `extern' and is initialized",
5347 decl);
5348 DECL_EXTERNAL (decl) = 0;
5349 if (current_binding_level == global_binding_level)
5350 TREE_STATIC (decl) = 1;
5351
5352 /* Tell `pushdecl' this is an initialized decl
5353 even though we don't yet have the initializer expression.
5354 Also tell `finish_decl' it may store the real initializer. */
5355 DECL_INITIAL (decl) = error_mark_node;
5356 }
5357
5358 if (context && TYPE_SIZE (context) != NULL_TREE)
5359 {
5360 if (TREE_CODE (decl) == VAR_DECL)
5361 {
5362 tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
5363 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
5364 cp_error ("`%#D' is not a static member of `%#T'", decl, context);
5365 else if (duplicate_decls (decl, field))
5366 decl = field;
5367 }
5368
5369 /* If it was not explicitly declared `extern',
5370 revoke any previous claims of DECL_EXTERNAL. */
5371 if (DECL_THIS_EXTERN (decl) == 0)
5372 DECL_EXTERNAL (decl) = 0;
5373 if (DECL_LANG_SPECIFIC (decl))
5374 DECL_IN_AGGR_P (decl) = 0;
5375 pushclass (context, 2);
5376 }
5377
5378 /* Add this decl to the current binding level, but not if it
5379 comes from another scope, e.g. a static member variable.
5380 TEM may equal DECL or it may be a previous decl of the same name. */
5381
5382 if ((TREE_CODE (decl) != PARM_DECL && DECL_CONTEXT (decl) != NULL_TREE)
5383 || (TREE_CODE (decl) == TEMPLATE_DECL && !global_bindings_p ())
5384 || TREE_CODE (type) == LANG_TYPE)
5385 tem = decl;
5386 else
5387 tem = pushdecl (decl);
5388
5389 /* Tell the back-end to use or not use .common as appropriate. If we say
5390 -fconserve-space, we want this to save space, at the expense of wrong
5391 semantics. If we say -fno-conserve-space, we want this to produce
5392 errors about redefs; to do this we force variables into the data
5393 segment. Common storage is okay for non-public uninitialized data;
5394 the linker can't match it with storage from other files, and we may
5395 save some disk space. */
5396 DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem);
5397
5398 #if 0
5399 /* We don't do this yet for GNU C++. */
5400 /* For a local variable, define the RTL now. */
5401 if (current_binding_level != global_binding_level
5402 /* But not if this is a duplicate decl
5403 and we preserved the rtl from the previous one
5404 (which may or may not happen). */
5405 && DECL_RTL (tem) == NULL_RTX)
5406 {
5407 if (TYPE_SIZE (TREE_TYPE (tem)) != NULL_TREE)
5408 expand_decl (tem);
5409 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
5410 && DECL_INITIAL (tem) != NULL_TREE)
5411 expand_decl (tem);
5412 }
5413 #endif
5414
5415 if (TREE_CODE (decl) == TEMPLATE_DECL)
5416 {
5417 tree result = DECL_TEMPLATE_RESULT (decl);
5418 if (DECL_CONTEXT (result) != NULL_TREE)
5419 {
5420 tree type;
5421 type = DECL_CONTEXT (result);
5422
5423 if (TREE_CODE (type) != UNINSTANTIATED_P_TYPE)
5424 {
5425 cp_error ("declaration of `%D' in non-template type `%T'",
5426 decl, type);
5427 return NULL_TREE;
5428 }
5429
5430 if (TREE_CODE (result) == FUNCTION_DECL)
5431 return tem;
5432 else if (TREE_CODE (result) == VAR_DECL)
5433 {
5434 #if 0
5435 tree tmpl = UPT_TEMPLATE (type);
5436
5437 fprintf (stderr, "%s:%d: adding ", __FILE__, __LINE__);
5438 print_node_brief (stderr, "", DECL_NAME (tem), 0);
5439 fprintf (stderr, " to class %s\n",
5440 IDENTIFIER_POINTER (DECL_NAME (tmpl)));
5441 DECL_TEMPLATE_MEMBERS (tmpl)
5442 = perm_tree_cons (DECL_NAME (tem), tem,
5443 DECL_TEMPLATE_MEMBERS (tmpl));
5444 return tem;
5445 #else
5446 sorry ("static data member templates");
5447 return NULL_TREE;
5448 #endif
5449 }
5450 else
5451 my_friendly_abort (13);
5452 }
5453 else if (TREE_CODE (result) == FUNCTION_DECL)
5454 /*tem = push_overloaded_decl (tem, 0)*/;
5455 else if (TREE_CODE (result) == VAR_DECL)
5456 {
5457 cp_error ("data template `%#D' must be member of a class template",
5458 result);
5459 return NULL_TREE;
5460 }
5461 else if (TREE_CODE (result) == TYPE_DECL)
5462 {
5463 cp_error ("invalid template `%#D'", result);
5464 return NULL_TREE;
5465 }
5466 else
5467 my_friendly_abort (14);
5468 }
5469
5470 if (init_written
5471 && ! (TREE_CODE (tem) == PARM_DECL
5472 || (TREE_READONLY (tem)
5473 && (TREE_CODE (tem) == VAR_DECL
5474 || TREE_CODE (tem) == FIELD_DECL))))
5475 {
5476 /* When parsing and digesting the initializer,
5477 use temporary storage. Do this even if we will ignore the value. */
5478 if (current_binding_level == global_binding_level && debug_temp_inits)
5479 {
5480 if (TYPE_NEEDS_CONSTRUCTING (type)
5481 || TREE_CODE (type) == REFERENCE_TYPE)
5482 /* In this case, the initializer must lay down in permanent
5483 storage, since it will be saved until `finish_file' is run. */
5484 ;
5485 else
5486 temporary_allocation ();
5487 }
5488 }
5489
5490 if (flag_cadillac)
5491 cadillac_start_decl (tem);
5492
5493 return tem;
5494 }
5495
5496 #if 0 /* unused */
5497 static void
5498 make_temporary_for_reference (decl, ctor_call, init, cleanupp)
5499 tree decl, ctor_call, init;
5500 tree *cleanupp;
5501 {
5502 tree type = TREE_TYPE (decl);
5503 tree target_type = TREE_TYPE (type);
5504 tree tmp, tmp_addr;
5505
5506 if (ctor_call)
5507 {
5508 tmp_addr = TREE_VALUE (TREE_OPERAND (ctor_call, 1));
5509 if (TREE_CODE (tmp_addr) == NOP_EXPR)
5510 tmp_addr = TREE_OPERAND (tmp_addr, 0);
5511 my_friendly_assert (TREE_CODE (tmp_addr) == ADDR_EXPR, 146);
5512 tmp = TREE_OPERAND (tmp_addr, 0);
5513 }
5514 else
5515 {
5516 tmp = get_temp_name (target_type,
5517 current_binding_level == global_binding_level);
5518 tmp_addr = build_unary_op (ADDR_EXPR, tmp, 0);
5519 }
5520
5521 TREE_TYPE (tmp_addr) = build_pointer_type (target_type);
5522 DECL_INITIAL (decl) = convert (TYPE_POINTER_TO (target_type), tmp_addr);
5523 TREE_TYPE (DECL_INITIAL (decl)) = type;
5524 if (TYPE_NEEDS_CONSTRUCTING (target_type))
5525 {
5526 if (current_binding_level == global_binding_level)
5527 {
5528 /* lay this variable out now. Otherwise `output_addressed_constants'
5529 gets confused by its initializer. */
5530 make_decl_rtl (tmp, NULL_PTR, 1);
5531 static_aggregates = perm_tree_cons (init, tmp, static_aggregates);
5532 }
5533 else
5534 {
5535 if (ctor_call != NULL_TREE)
5536 init = ctor_call;
5537 else
5538 init = build_method_call (tmp, constructor_name_full (target_type),
5539 build_tree_list (NULL_TREE, init),
5540 NULL_TREE, LOOKUP_NORMAL);
5541 DECL_INITIAL (decl) = build (COMPOUND_EXPR, type, init,
5542 DECL_INITIAL (decl));
5543 *cleanupp = maybe_build_cleanup (tmp);
5544 }
5545 }
5546 else
5547 {
5548 DECL_INITIAL (tmp) = init;
5549 TREE_STATIC (tmp) = current_binding_level == global_binding_level;
5550 finish_decl (tmp, init, 0, 0);
5551 }
5552 if (TREE_STATIC (tmp))
5553 preserve_initializer ();
5554 }
5555 #endif
5556
5557 /* Handle initialization of references.
5558 These three arguments from from `finish_decl', and have the
5559 same meaning here that they do there. */
5560 /* quotes on semantics can be found in ARM 8.4.3. */
5561 static void
5562 grok_reference_init (decl, type, init, cleanupp)
5563 tree decl, type, init;
5564 tree *cleanupp;
5565 {
5566 tree tmp;
5567
5568 if (init == NULL_TREE)
5569 {
5570 if (DECL_LANG_SPECIFIC (decl) == 0
5571 || DECL_IN_AGGR_P (decl) == 0)
5572 {
5573 cp_error ("`%D' declared as reference but not initialized", decl);
5574 if (TREE_CODE (decl) == VAR_DECL)
5575 SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
5576 }
5577 return;
5578 }
5579
5580 if (init == error_mark_node)
5581 return;
5582
5583 if (TREE_CODE (type) == REFERENCE_TYPE
5584 && TREE_CODE (init) == CONSTRUCTOR)
5585 {
5586 cp_error ("ANSI C++ forbids use of initializer list to initialize reference `%D'", decl);
5587 return;
5588 }
5589
5590 if (TREE_CODE (init) == TREE_LIST)
5591 init = build_compound_expr (init);
5592
5593 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
5594 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5595 {
5596 /* Note: default conversion is only called in very special cases. */
5597 init = default_conversion (init);
5598 }
5599
5600 tmp = convert_to_reference
5601 (type, init, CONV_IMPLICIT, LOOKUP_SPECULATIVELY|LOOKUP_NORMAL, decl);
5602
5603 if (tmp == error_mark_node)
5604 goto fail;
5605 else if (tmp != NULL_TREE)
5606 {
5607 tree subtype = TREE_TYPE (type);
5608 init = tmp;
5609
5610 /* Associate the cleanup with the reference so that we
5611 don't get burned by "aggressive" cleanup policy. */
5612 if (TYPE_NEEDS_DESTRUCTOR (subtype))
5613 {
5614 if (TREE_CODE (init) == WITH_CLEANUP_EXPR)
5615 {
5616 *cleanupp = TREE_OPERAND (init, 2);
5617 TREE_OPERAND (init, 2) = error_mark_node;
5618 }
5619 else
5620 {
5621 if (TREE_CODE (tmp) == ADDR_EXPR)
5622 tmp = TREE_OPERAND (tmp, 0);
5623 if (TREE_CODE (tmp) == TARGET_EXPR)
5624 {
5625 *cleanupp = build_delete
5626 (TYPE_POINTER_TO (subtype),
5627 build_unary_op (ADDR_EXPR, TREE_OPERAND (tmp, 0), 0),
5628 integer_two_node, LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
5629 TREE_OPERAND (tmp, 2) = error_mark_node;
5630 }
5631 }
5632 }
5633
5634 if (TREE_SIDE_EFFECTS (init))
5635 DECL_INITIAL (decl) = save_expr (init);
5636 else
5637 DECL_INITIAL (decl) = init;
5638 }
5639 else
5640 {
5641 cp_error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
5642 goto fail;
5643 }
5644
5645 /* ?? Can this be optimized in some cases to
5646 hand back the DECL_INITIAL slot?? */
5647 if (TYPE_SIZE (TREE_TYPE (type)))
5648 {
5649 init = convert_from_reference (decl);
5650 if (TREE_PERMANENT (decl))
5651 init = copy_to_permanent (init);
5652 SET_DECL_REFERENCE_SLOT (decl, init);
5653 }
5654
5655 if (TREE_STATIC (decl) && ! TREE_CONSTANT (DECL_INITIAL (decl)))
5656 {
5657 expand_static_init (decl, DECL_INITIAL (decl));
5658 DECL_INITIAL (decl) = NULL_TREE;
5659 }
5660 return;
5661
5662 fail:
5663 if (TREE_CODE (decl) == VAR_DECL)
5664 SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
5665 return;
5666 }
5667
5668 /* Finish processing of a declaration;
5669 install its line number and initial value.
5670 If the length of an array type is not known before,
5671 it must be determined now, from the initial value, or it is an error.
5672
5673 Call `pop_obstacks' iff NEED_POP is nonzero.
5674
5675 For C++, `finish_decl' must be fairly evasive: it must keep initializers
5676 for aggregates that have constructors alive on the permanent obstack,
5677 so that the global initializing functions can be written at the end.
5678
5679 INIT0 holds the value of an initializer that should be allowed to escape
5680 the normal rules.
5681
5682 For functions that take default parameters, DECL points to its
5683 "maximal" instantiation. `finish_decl' must then also declared its
5684 subsequently lower and lower forms of instantiation, checking for
5685 ambiguity as it goes. This can be sped up later. */
5686
5687 void
5688 finish_decl (decl, init, asmspec_tree, need_pop)
5689 tree decl, init;
5690 tree asmspec_tree;
5691 int need_pop;
5692 {
5693 register tree type;
5694 tree cleanup = NULL_TREE, ttype;
5695 int was_incomplete;
5696 int temporary = allocation_temporary_p ();
5697 char *asmspec = NULL;
5698 int was_readonly = 0;
5699
5700 /* If this is 0, then we did not change obstacks. */
5701 if (! decl)
5702 {
5703 if (init)
5704 error ("assignment (not initialization) in declaration");
5705 return;
5706 }
5707
5708 /* If a name was specified, get the string. */
5709 if (asmspec_tree)
5710 asmspec = TREE_STRING_POINTER (asmspec_tree);
5711
5712 /* If the type of the thing we are declaring either has
5713 a constructor, or has a virtual function table pointer,
5714 AND its initialization was accepted by `start_decl',
5715 then we stayed on the permanent obstack through the
5716 declaration, otherwise, changed obstacks as GCC would. */
5717
5718 type = TREE_TYPE (decl);
5719
5720 was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5721
5722 /* Take care of TYPE_DECLs up front. */
5723 if (TREE_CODE (decl) == TYPE_DECL)
5724 {
5725 if (init && DECL_INITIAL (decl))
5726 {
5727 /* typedef foo = bar; store the type of bar as the type of foo. */
5728 TREE_TYPE (decl) = type = TREE_TYPE (init);
5729 DECL_INITIAL (decl) = init = NULL_TREE;
5730 }
5731 if (type != error_mark_node
5732 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
5733 {
5734 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
5735 cp_warning ("shadowing previous type declaration of `%#D'", decl);
5736 set_identifier_type_value (DECL_NAME (decl), type);
5737 CLASSTYPE_GOT_SEMICOLON (type) = 1;
5738 }
5739 GNU_xref_decl (current_function_decl, decl);
5740 rest_of_decl_compilation (decl, NULL_PTR,
5741 DECL_CONTEXT (decl) == NULL_TREE, 0);
5742 goto finish_end;
5743 }
5744 if (type != error_mark_node && IS_AGGR_TYPE (type)
5745 && CLASSTYPE_DECLARED_EXCEPTION (type))
5746 {
5747 CLASSTYPE_GOT_SEMICOLON (type) = 1;
5748 goto finish_end;
5749 }
5750 if (TREE_CODE (decl) != FUNCTION_DECL)
5751 {
5752 ttype = target_type (type);
5753 #if 0 /* WTF? -KR
5754 Leave this out until we can figure out why it was
5755 needed/desirable in the first place. Then put a comment
5756 here explaining why. Or just delete the code if no ill
5757 effects arise. */
5758 if (TYPE_NAME (ttype)
5759 && TREE_CODE (TYPE_NAME (ttype)) == TYPE_DECL
5760 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (ttype)))
5761 {
5762 tree old_id = TYPE_IDENTIFIER (ttype);
5763 char *newname = (char *)alloca (IDENTIFIER_LENGTH (old_id) + 2);
5764 /* Need to preserve template data for UPT nodes. */
5765 tree old_template = IDENTIFIER_TEMPLATE (old_id);
5766 newname[0] = '_';
5767 bcopy (IDENTIFIER_POINTER (old_id), newname + 1,
5768 IDENTIFIER_LENGTH (old_id) + 1);
5769 old_id = get_identifier (newname);
5770 lookup_tag_reverse (ttype, old_id);
5771 TYPE_IDENTIFIER (ttype) = old_id;
5772 IDENTIFIER_TEMPLATE (old_id) = old_template;
5773 }
5774 #endif
5775 }
5776
5777 if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
5778 && TYPE_NEEDS_CONSTRUCTING (type))
5779 {
5780
5781 /* Currently, GNU C++ puts constants in text space, making them
5782 impossible to initialize. In the future, one would hope for
5783 an operating system which understood the difference between
5784 initialization and the running of a program. */
5785 was_readonly = 1;
5786 TREE_READONLY (decl) = 0;
5787 }
5788
5789 if (TREE_CODE (decl) == FIELD_DECL)
5790 {
5791 if (init && init != error_mark_node)
5792 my_friendly_assert (TREE_PERMANENT (init), 147);
5793
5794 if (asmspec)
5795 {
5796 /* This must override the asm specifier which was placed
5797 by grokclassfn. Lay this out fresh.
5798
5799 @@ Should emit an error if this redefines an asm-specified
5800 @@ name, or if we have already used the function's name. */
5801 DECL_RTL (TREE_TYPE (decl)) = NULL_RTX;
5802 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
5803 make_decl_rtl (decl, asmspec, 0);
5804 }
5805 }
5806 /* If `start_decl' didn't like having an initialization, ignore it now. */
5807 else if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5808 init = NULL_TREE;
5809 else if (DECL_EXTERNAL (decl))
5810 ;
5811 else if (TREE_CODE (type) == REFERENCE_TYPE
5812 || (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE_REFERENCE (type)))
5813 {
5814 grok_reference_init (decl, type, init, &cleanup);
5815 init = NULL_TREE;
5816 }
5817
5818 GNU_xref_decl (current_function_decl, decl);
5819
5820 if (TREE_CODE (decl) == FIELD_DECL)
5821 ;
5822 else if (TREE_CODE (decl) == CONST_DECL)
5823 {
5824 my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
5825
5826 DECL_INITIAL (decl) = init;
5827
5828 /* This will keep us from needing to worry about our obstacks. */
5829 my_friendly_assert (init != NULL_TREE, 149);
5830 init = NULL_TREE;
5831 }
5832 else if (init)
5833 {
5834 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
5835 {
5836 if (TREE_CODE (type) == ARRAY_TYPE)
5837 init = digest_init (type, init, (tree *) 0);
5838 else if (TREE_CODE (init) == CONSTRUCTOR)
5839 {
5840 if (TYPE_NEEDS_CONSTRUCTING (type))
5841 {
5842 cp_error ("`%D' must be initialized by constructor, not by `{...}'",
5843 decl);
5844 init = error_mark_node;
5845 }
5846 else
5847 goto dont_use_constructor;
5848 }
5849 #if 0
5850 /* fix this in `build_functional_cast' instead.
5851 Here's the trigger code:
5852
5853 struct ostream
5854 {
5855 ostream ();
5856 ostream (int, char *);
5857 ostream (char *);
5858 operator char *();
5859 ostream (void *);
5860 operator void *();
5861 operator << (int);
5862 };
5863 int buf_size = 1024;
5864 static char buf[buf_size];
5865 const char *debug(int i) {
5866 char *b = &buf[0];
5867 ostream o = ostream(buf_size, b);
5868 o << i;
5869 return buf;
5870 }
5871 */
5872
5873 else if (TREE_CODE (init) == TARGET_EXPR
5874 && TREE_CODE (TREE_OPERAND (init, 1) == NEW_EXPR))
5875 {
5876 /* User wrote something like `foo x = foo (args)' */
5877 my_friendly_assert (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL, 150);
5878 my_friendly_assert (DECL_NAME (TREE_OPERAND (init, 0)) == NULL_TREE, 151);
5879
5880 /* User wrote exactly `foo x = foo (args)' */
5881 if (TYPE_MAIN_VARIANT (type) == TREE_TYPE (init))
5882 {
5883 init = build (CALL_EXPR, TREE_TYPE (init),
5884 TREE_OPERAND (TREE_OPERAND (init, 1), 0),
5885 TREE_OPERAND (TREE_OPERAND (init, 1), 1), 0);
5886 TREE_SIDE_EFFECTS (init) = 1;
5887 }
5888 }
5889 #endif
5890
5891 /* We must hide the initializer so that expand_decl
5892 won't try to do something it does not understand. */
5893 if (current_binding_level == global_binding_level)
5894 {
5895 tree value;
5896 if (DECL_COMMON (decl))
5897 /* Should this be a NULL_TREE? */
5898 value = error_mark_node;
5899 else
5900 value = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
5901 DECL_INITIAL (decl) = value;
5902 }
5903 else
5904 DECL_INITIAL (decl) = error_mark_node;
5905 }
5906 else
5907 {
5908 dont_use_constructor:
5909 if (TREE_CODE (init) != TREE_VEC)
5910 init = store_init_value (decl, init);
5911
5912 /* Don't let anyone try to initialize this variable
5913 until we are ready to do so. */
5914 if (init)
5915 {
5916 tree value;
5917 if (DECL_COMMON (decl))
5918 value = error_mark_node;
5919 else
5920 value = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
5921 DECL_INITIAL (decl) = value;
5922 }
5923 }
5924 }
5925 else if (DECL_EXTERNAL (decl))
5926 ;
5927 else if (TREE_CODE_CLASS (TREE_CODE (type)) == 't'
5928 && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type)))
5929 {
5930 tree ctype = type;
5931 while (TREE_CODE (ctype) == ARRAY_TYPE)
5932 ctype = TREE_TYPE (ctype);
5933 if (! TYPE_NEEDS_CONSTRUCTING (ctype))
5934 {
5935 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (ctype))
5936 cp_error ("structure `%D' with uninitialized const members", decl);
5937 if (CLASSTYPE_REF_FIELDS_NEED_INIT (ctype))
5938 cp_error ("structure `%D' with uninitialized reference members",
5939 decl);
5940 }
5941
5942 if (TREE_CODE (decl) == VAR_DECL
5943 && !DECL_INITIAL (decl)
5944 && !TYPE_NEEDS_CONSTRUCTING (type)
5945 && (TYPE_READONLY (type) || TREE_READONLY (decl)))
5946 cp_error ("uninitialized const `%D'", decl);
5947
5948 /* Initialize variables in need of static initialization with
5949 an empty CONSTRUCTOR to keep assemble_variable from putting them in
5950 the wrong program space. */
5951 if (flag_pic == 0
5952 && TREE_STATIC (decl)
5953 && TREE_PUBLIC (decl)
5954 && ! DECL_EXTERNAL (decl)
5955 && TREE_CODE (decl) == VAR_DECL
5956 && TYPE_NEEDS_CONSTRUCTING (type)
5957 && (DECL_INITIAL (decl) == NULL_TREE
5958 || DECL_INITIAL (decl) == error_mark_node)
5959 && ! DECL_COMMON (decl))
5960 DECL_INITIAL (decl) = build (CONSTRUCTOR, type, NULL_TREE,
5961 NULL_TREE);
5962 }
5963 else if (TREE_CODE (decl) == VAR_DECL
5964 && TREE_CODE (type) != REFERENCE_TYPE
5965 && (TYPE_READONLY (type) || TREE_READONLY (decl)))
5966 {
5967 /* ``Unless explicitly declared extern, a const object does not have
5968 external linkage and must be initialized. ($8.4; $12.1)'' ARM 7.1.6
5969 However, if it's `const int foo = 1; const int foo;', don't complain
5970 about the second decl, since it does have an initializer before.
5971 We deliberately don't complain about arrays, because they're
5972 supposed to be initialized by a constructor. */
5973 if (! DECL_INITIAL (decl)
5974 && TREE_CODE (type) != ARRAY_TYPE
5975 && (!pedantic || !current_class_type))
5976 cp_error ("uninitialized const `%#D'", decl);
5977 }
5978
5979 /* For top-level declaration, the initial value was read in
5980 the temporary obstack. MAXINDEX, rtl, etc. to be made below
5981 must go in the permanent obstack; but don't discard the
5982 temporary data yet. */
5983
5984 if (current_binding_level == global_binding_level && temporary)
5985 end_temporary_allocation ();
5986
5987 /* Deduce size of array from initialization, if not already known. */
5988
5989 if (TREE_CODE (type) == ARRAY_TYPE
5990 && TYPE_DOMAIN (type) == NULL_TREE
5991 && TREE_CODE (decl) != TYPE_DECL)
5992 {
5993 int do_default
5994 = (TREE_STATIC (decl)
5995 /* Even if pedantic, an external linkage array
5996 may have incomplete type at first. */
5997 ? pedantic && ! DECL_EXTERNAL (decl)
5998 : !DECL_EXTERNAL (decl));
5999 tree initializer = init ? init : DECL_INITIAL (decl);
6000 int failure = complete_array_type (type, initializer, do_default);
6001
6002 if (failure == 1)
6003 cp_error ("initializer fails to determine size of `%D'", decl);
6004
6005 if (failure == 2)
6006 {
6007 if (do_default)
6008 cp_error ("array size missing in `%D'", decl);
6009 /* If a `static' var's size isn't known, make it extern as
6010 well as static, so it does not get allocated. If it's not
6011 `static', then don't mark it extern; finish_incomplete_decl
6012 will give it a default size and it will get allocated. */
6013 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6014 DECL_EXTERNAL (decl) = 1;
6015 }
6016
6017 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
6018 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
6019 integer_zero_node))
6020 cp_error ("zero-size array `%D'", decl);
6021
6022 layout_decl (decl, 0);
6023 }
6024
6025 if (TREE_CODE (decl) == VAR_DECL)
6026 {
6027 if (DECL_SIZE (decl) == NULL_TREE
6028 && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE)
6029 layout_decl (decl, 0);
6030
6031 if (TREE_STATIC (decl) && DECL_SIZE (decl) == NULL_TREE)
6032 {
6033 /* A static variable with an incomplete type:
6034 that is an error if it is initialized.
6035 Otherwise, let it through, but if it is not `extern'
6036 then it may cause an error message later. */
6037 if (DECL_INITIAL (decl) != NULL_TREE)
6038 cp_error ("storage size of `%D' isn't known", decl);
6039 init = NULL_TREE;
6040 }
6041 else if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6042 {
6043 /* An automatic variable with an incomplete type: that is an error.
6044 Don't talk about array types here, since we took care of that
6045 message in grokdeclarator. */
6046 cp_error ("storage size of `%D' isn't known", decl);
6047 TREE_TYPE (decl) = error_mark_node;
6048 }
6049 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
6050 /* Let debugger know it should output info for this type. */
6051 note_debug_info_needed (ttype);
6052
6053 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6054 && DECL_SIZE (decl) != NULL_TREE
6055 && ! TREE_CONSTANT (DECL_SIZE (decl)))
6056 {
6057 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
6058 constant_expression_warning (DECL_SIZE (decl));
6059 else
6060 cp_error ("storage size of `%D' isn't constant", decl);
6061 }
6062
6063 if (!DECL_EXTERNAL (decl) && TYPE_NEEDS_DESTRUCTOR (type))
6064 {
6065 int yes = suspend_momentary ();
6066
6067 /* If INIT comes from a functional cast, use the cleanup
6068 we built for that. Otherwise, make our own cleanup. */
6069 if (init && TREE_CODE (init) == WITH_CLEANUP_EXPR
6070 && comptypes (TREE_TYPE (decl), TREE_TYPE (init), 1))
6071 {
6072 cleanup = TREE_OPERAND (init, 2);
6073 init = TREE_OPERAND (init, 0);
6074 current_binding_level->have_cleanups = 1;
6075 }
6076 else
6077 cleanup = maybe_build_cleanup (decl);
6078 resume_momentary (yes);
6079 }
6080 }
6081 /* PARM_DECLs get cleanups, too. */
6082 else if (TREE_CODE (decl) == PARM_DECL && TYPE_NEEDS_DESTRUCTOR (type))
6083 {
6084 if (temporary)
6085 end_temporary_allocation ();
6086 cleanup = maybe_build_cleanup (decl);
6087 if (temporary)
6088 resume_temporary_allocation ();
6089 }
6090
6091 /* Output the assembler code and/or RTL code for variables and functions,
6092 unless the type is an undefined structure or union.
6093 If not, it will get done when the type is completed. */
6094
6095 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
6096 || TREE_CODE (decl) == RESULT_DECL)
6097 {
6098 /* ??? FIXME: What about nested classes? */
6099 int toplev = (current_binding_level == global_binding_level
6100 || pseudo_global_level_p ());
6101 int was_temp
6102 = ((flag_traditional
6103 || (TREE_STATIC (decl) && TYPE_NEEDS_DESTRUCTOR (type)))
6104 && allocation_temporary_p ());
6105
6106 if (was_temp)
6107 end_temporary_allocation ();
6108
6109 if (TREE_CODE (decl) == VAR_DECL
6110 && current_binding_level != global_binding_level
6111 && ! TREE_STATIC (decl)
6112 && type_needs_gc_entry (type))
6113 DECL_GC_OFFSET (decl) = size_int (++current_function_obstack_index);
6114
6115 if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
6116 make_decl_rtl (decl, NULL_PTR, toplev);
6117 else if (TREE_CODE (decl) == VAR_DECL
6118 && TREE_READONLY (decl)
6119 && DECL_INITIAL (decl) != NULL_TREE
6120 && DECL_INITIAL (decl) != error_mark_node
6121 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
6122 {
6123 DECL_INITIAL (decl) = save_expr (DECL_INITIAL (decl));
6124
6125 if (asmspec)
6126 DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
6127
6128 if (! toplev
6129 && TREE_STATIC (decl)
6130 && ! TREE_SIDE_EFFECTS (decl)
6131 && ! TREE_PUBLIC (decl)
6132 && ! DECL_EXTERNAL (decl)
6133 && ! TYPE_NEEDS_DESTRUCTOR (type)
6134 && DECL_MODE (decl) != BLKmode)
6135 {
6136 /* If this variable is really a constant, then fill its DECL_RTL
6137 slot with something which won't take up storage.
6138 If something later should take its address, we can always give
6139 it legitimate RTL at that time. */
6140 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
6141 store_expr (DECL_INITIAL (decl), DECL_RTL (decl), 0);
6142 TREE_ASM_WRITTEN (decl) = 1;
6143 }
6144 else if (toplev && ! TREE_PUBLIC (decl))
6145 {
6146 /* If this is a static const, change its apparent linkage
6147 if it belongs to a #pragma interface. */
6148 if (!interface_unknown)
6149 {
6150 TREE_PUBLIC (decl) = 1;
6151 DECL_EXTERNAL (decl) = interface_only;
6152 }
6153 make_decl_rtl (decl, asmspec, toplev);
6154 }
6155 else
6156 rest_of_decl_compilation (decl, asmspec, toplev, 0);
6157 }
6158 else if (TREE_CODE (decl) == VAR_DECL
6159 && DECL_LANG_SPECIFIC (decl)
6160 && DECL_IN_AGGR_P (decl))
6161 {
6162 if (TREE_STATIC (decl))
6163 {
6164 if (init == NULL_TREE
6165 #ifdef DEFAULT_STATIC_DEFS
6166 /* If this code is dead, then users must
6167 explicitly declare static member variables
6168 outside the class def'n as well. */
6169 && TYPE_NEEDS_CONSTRUCTING (type)
6170 #endif
6171 )
6172 {
6173 DECL_EXTERNAL (decl) = 1;
6174 make_decl_rtl (decl, asmspec, 1);
6175 }
6176 else
6177 rest_of_decl_compilation (decl, asmspec, toplev, 0);
6178 }
6179 else
6180 /* Just a constant field. Should not need any rtl. */
6181 goto finish_end0;
6182 }
6183 else
6184 rest_of_decl_compilation (decl, asmspec, toplev, 0);
6185
6186 if (was_temp)
6187 resume_temporary_allocation ();
6188
6189 if (type != error_mark_node
6190 && TYPE_LANG_SPECIFIC (type)
6191 && CLASSTYPE_ABSTRACT_VIRTUALS (type))
6192 abstract_virtuals_error (decl, type);
6193 else if ((TREE_CODE (type) == FUNCTION_TYPE
6194 || TREE_CODE (type) == METHOD_TYPE)
6195 && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
6196 && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (type)))
6197 abstract_virtuals_error (decl, TREE_TYPE (type));
6198
6199 if (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE (type))
6200 signature_error (decl, type);
6201 else if ((TREE_CODE (type) == FUNCTION_TYPE
6202 || TREE_CODE (type) == METHOD_TYPE)
6203 && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
6204 && IS_SIGNATURE (TREE_TYPE (type)))
6205 signature_error (decl, TREE_TYPE (type));
6206
6207 if (TREE_CODE (decl) == FUNCTION_DECL)
6208 {
6209 #if 0
6210 /* C++: Handle overloaded functions with default parameters. */
6211 if (DECL_OVERLOADED (decl))
6212 {
6213 tree parmtypes = TYPE_ARG_TYPES (type);
6214 tree prev = NULL_TREE;
6215 tree original_name = DECL_NAME (decl);
6216 struct lang_decl *tmp_lang_decl = DECL_LANG_SPECIFIC (decl);
6217 /* All variants will share an uncollectible lang_decl. */
6218 copy_decl_lang_specific (decl);
6219
6220 while (parmtypes && parmtypes != void_list_node)
6221 {
6222 /* The default value for the parameter in parmtypes is
6223 stored in the TREE_PURPOSE of the TREE_LIST. */
6224 if (TREE_PURPOSE (parmtypes))
6225 {
6226 tree fnname, fndecl;
6227 tree *argp;
6228
6229 argp = prev ? & TREE_CHAIN (prev)
6230 : & TYPE_ARG_TYPES (type);
6231
6232 *argp = NULL_TREE;
6233 fnname = build_decl_overload (original_name,
6234 TYPE_ARG_TYPES (type), 0);
6235 *argp = parmtypes;
6236 fndecl = build_decl (FUNCTION_DECL, fnname, type);
6237 DECL_EXTERNAL (fndecl) = DECL_EXTERNAL (decl);
6238 TREE_PUBLIC (fndecl) = TREE_PUBLIC (decl);
6239 DECL_INLINE (fndecl) = DECL_INLINE (decl);
6240 /* Keep G++ from thinking this function is unused.
6241 It is only used to speed up search in name space. */
6242 TREE_USED (fndecl) = 1;
6243 TREE_ASM_WRITTEN (fndecl) = 1;
6244 DECL_INITIAL (fndecl) = NULL_TREE;
6245 DECL_LANG_SPECIFIC (fndecl) = DECL_LANG_SPECIFIC (decl);
6246 fndecl = pushdecl (fndecl);
6247 DECL_INITIAL (fndecl) = error_mark_node;
6248 DECL_RTL (fndecl) = DECL_RTL (decl);
6249 }
6250 prev = parmtypes;
6251 parmtypes = TREE_CHAIN (parmtypes);
6252 }
6253 DECL_LANG_SPECIFIC (decl) = tmp_lang_decl;
6254 }
6255 #endif
6256 }
6257 else if (DECL_EXTERNAL (decl))
6258 ;
6259 else if (TREE_STATIC (decl) && type != error_mark_node)
6260 {
6261 /* Cleanups for static variables are handled by `finish_file'. */
6262 if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE)
6263 expand_static_init (decl, init);
6264 else if (TYPE_NEEDS_DESTRUCTOR (type))
6265 static_aggregates = perm_tree_cons (NULL_TREE, decl,
6266 static_aggregates);
6267
6268 /* Make entry in appropriate vector. */
6269 if (flag_gc && type_needs_gc_entry (type))
6270 build_static_gc_entry (decl, type);
6271 }
6272 else if (! toplev)
6273 {
6274 tree old_cleanups = cleanups_this_call;
6275 /* This is a declared decl which must live until the
6276 end of the binding contour. It may need a cleanup. */
6277
6278 /* Recompute the RTL of a local array now
6279 if it used to be an incomplete type. */
6280 if (was_incomplete && ! TREE_STATIC (decl))
6281 {
6282 /* If we used it already as memory, it must stay in memory. */
6283 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6284 /* If it's still incomplete now, no init will save it. */
6285 if (DECL_SIZE (decl) == NULL_TREE)
6286 DECL_INITIAL (decl) = NULL_TREE;
6287 expand_decl (decl);
6288 }
6289 else if (! TREE_ASM_WRITTEN (decl)
6290 && (TYPE_SIZE (type) != NULL_TREE
6291 || TREE_CODE (type) == ARRAY_TYPE))
6292 {
6293 /* Do this here, because we did not expand this decl's
6294 rtl in start_decl. */
6295 if (DECL_RTL (decl) == NULL_RTX)
6296 expand_decl (decl);
6297 else if (cleanup)
6298 {
6299 /* XXX: Why don't we use decl here? */
6300 /* Ans: Because it was already expanded? */
6301 if (! expand_decl_cleanup (NULL_TREE, cleanup))
6302 cp_error ("parser lost in parsing declaration of `%D'",
6303 decl);
6304 /* Cleanup used up here. */
6305 cleanup = NULL_TREE;
6306 }
6307 }
6308
6309 if (DECL_SIZE (decl) && type != error_mark_node)
6310 {
6311 /* Compute and store the initial value. */
6312 expand_decl_init (decl);
6313
6314 if (init || TYPE_NEEDS_CONSTRUCTING (type))
6315 {
6316 emit_line_note (DECL_SOURCE_FILE (decl),
6317 DECL_SOURCE_LINE (decl));
6318 expand_aggr_init (decl, init, 0);
6319 }
6320
6321 /* Set this to 0 so we can tell whether an aggregate
6322 which was initialized was ever used. */
6323 if (TYPE_NEEDS_CONSTRUCTING (type))
6324 TREE_USED (decl) = 0;
6325
6326 /* Store the cleanup, if there was one. */
6327 if (cleanup)
6328 {
6329 if (! expand_decl_cleanup (decl, cleanup))
6330 cp_error ("parser lost in parsing declaration of `%D'",
6331 decl);
6332 }
6333 }
6334 /* Cleanup any temporaries needed for the initial value. */
6335 expand_cleanups_to (old_cleanups);
6336 }
6337 finish_end0:
6338
6339 /* Undo call to `pushclass' that was done in `start_decl'
6340 due to initialization of qualified member variable.
6341 I.e., Foo::x = 10; */
6342 {
6343 tree context = DECL_CONTEXT (decl);
6344 if (context
6345 && TREE_CODE_CLASS (TREE_CODE (context)) == 't'
6346 && (TREE_CODE (decl) == VAR_DECL
6347 /* We also have a pushclass done that we need to undo here
6348 if we're at top level and declare a method. */
6349 || (TREE_CODE (decl) == FUNCTION_DECL
6350 /* If size hasn't been set, we're still defining it,
6351 and therefore inside the class body; don't pop
6352 the binding level.. */
6353 && TYPE_SIZE (context) != NULL_TREE
6354 /* The binding level gets popped elsewhere for a
6355 friend declaration inside another class. */
6356 /*
6357 && TYPE_IDENTIFIER (context) == current_class_name
6358 */
6359 && context == current_class_type
6360 )))
6361 popclass (1);
6362 }
6363 }
6364
6365 finish_end:
6366
6367 /* If requested, warn about definitions of large data objects. */
6368
6369 if (warn_larger_than
6370 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
6371 && !DECL_EXTERNAL (decl))
6372 {
6373 register tree decl_size = DECL_SIZE (decl);
6374
6375 if (decl_size && TREE_CODE (decl_size) == INTEGER_CST)
6376 {
6377 unsigned units = TREE_INT_CST_LOW (decl_size) / BITS_PER_UNIT;
6378
6379 if (units > larger_than_size)
6380 warning_with_decl (decl, "size of `%s' is %u bytes", units);
6381 }
6382 }
6383
6384 if (need_pop)
6385 {
6386 /* Resume permanent allocation, if not within a function. */
6387 /* The corresponding push_obstacks_nochange is in start_decl,
6388 start_method, groktypename, and in grokfield. */
6389 pop_obstacks ();
6390 }
6391
6392 if (was_readonly)
6393 TREE_READONLY (decl) = 1;
6394
6395 if (flag_cadillac)
6396 cadillac_finish_decl (decl);
6397 }
6398
6399 void
6400 expand_static_init (decl, init)
6401 tree decl;
6402 tree init;
6403 {
6404 tree oldstatic = value_member (decl, static_aggregates);
6405 tree old_cleanups;
6406
6407 if (oldstatic)
6408 {
6409 if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
6410 cp_error ("multiple initializations given for `%D'", decl);
6411 }
6412 else if (current_binding_level != global_binding_level
6413 && current_binding_level->pseudo_global == 0)
6414 {
6415 /* Emit code to perform this initialization but once. */
6416 tree temp;
6417
6418 /* Remember this information until end of file. */
6419 push_obstacks (&permanent_obstack, &permanent_obstack);
6420
6421 /* Emit code to perform this initialization but once. */
6422 temp = get_temp_name (integer_type_node, 1);
6423 rest_of_decl_compilation (temp, NULL_PTR, 0, 0);
6424 expand_start_cond (build_binary_op (EQ_EXPR, temp,
6425 integer_zero_node, 1), 0);
6426 old_cleanups = cleanups_this_call;
6427 expand_assignment (temp, integer_one_node, 0, 0);
6428 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
6429 {
6430 expand_aggr_init (decl, init, 0);
6431 do_pending_stack_adjust ();
6432 }
6433 else
6434 expand_assignment (decl, init, 0, 0);
6435 /* Cleanup any temporaries needed for the initial value. */
6436 expand_cleanups_to (old_cleanups);
6437 expand_end_cond ();
6438 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
6439 {
6440 static_aggregates = perm_tree_cons (temp, decl, static_aggregates);
6441 TREE_STATIC (static_aggregates) = 1;
6442 }
6443
6444 /* Resume old (possibly temporary) allocation. */
6445 pop_obstacks ();
6446 }
6447 else
6448 {
6449 /* This code takes into account memory allocation
6450 policy of `start_decl'. Namely, if TYPE_NEEDS_CONSTRUCTING
6451 does not hold for this object, then we must make permanent
6452 the storage currently in the temporary obstack. */
6453 if (! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
6454 preserve_initializer ();
6455 static_aggregates = perm_tree_cons (init, decl, static_aggregates);
6456 }
6457 }
6458 \f
6459 /* Make TYPE a complete type based on INITIAL_VALUE.
6460 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6461 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
6462
6463 int
6464 complete_array_type (type, initial_value, do_default)
6465 tree type, initial_value;
6466 int do_default;
6467 {
6468 register tree maxindex = NULL_TREE;
6469 int value = 0;
6470
6471 if (initial_value)
6472 {
6473 /* Note MAXINDEX is really the maximum index,
6474 one less than the size. */
6475 if (TREE_CODE (initial_value) == STRING_CST)
6476 maxindex = build_int_2 (TREE_STRING_LENGTH (initial_value) - 1, 0);
6477 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
6478 {
6479 register int nelts
6480 = list_length (CONSTRUCTOR_ELTS (initial_value));
6481 maxindex = build_int_2 (nelts - 1, - (nelts == 0));
6482 }
6483 else
6484 {
6485 /* Make an error message unless that happened already. */
6486 if (initial_value != error_mark_node)
6487 value = 1;
6488
6489 /* Prevent further error messages. */
6490 maxindex = build_int_2 (0, 0);
6491 }
6492 }
6493
6494 if (!maxindex)
6495 {
6496 if (do_default)
6497 maxindex = build_int_2 (0, 0);
6498 value = 2;
6499 }
6500
6501 if (maxindex)
6502 {
6503 tree itype;
6504
6505 TYPE_DOMAIN (type) = build_index_type (maxindex);
6506 if (!TREE_TYPE (maxindex))
6507 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
6508 if (initial_value)
6509 itype = TREE_TYPE (initial_value);
6510 else
6511 itype = NULL;
6512 if (itype && !TYPE_DOMAIN (itype))
6513 TYPE_DOMAIN (itype) = TYPE_DOMAIN (type);
6514 }
6515
6516 /* Lay out the type now that we can get the real answer. */
6517
6518 layout_type (type);
6519
6520 return value;
6521 }
6522 \f
6523 /* Return zero if something is declared to be a member of type
6524 CTYPE when in the context of CUR_TYPE. STRING is the error
6525 message to print in that case. Otherwise, quietly return 1. */
6526 static int
6527 member_function_or_else (ctype, cur_type, string)
6528 tree ctype, cur_type;
6529 char *string;
6530 {
6531 if (ctype && ctype != cur_type)
6532 {
6533 error (string, TYPE_NAME_STRING (ctype));
6534 return 0;
6535 }
6536 return 1;
6537 }
6538 \f
6539 /* Subroutine of `grokdeclarator'. */
6540
6541 /* Generate errors possibly applicable for a given set of specifiers.
6542 This is for ARM $7.1.2. */
6543 static void
6544 bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises)
6545 tree object;
6546 char *type;
6547 int virtualp, quals, friendp, raises, inlinep;
6548 {
6549 if (virtualp)
6550 cp_error ("`%D' declared as a `virtual' %s", object, type);
6551 if (inlinep)
6552 cp_error ("`%D' declared as an `inline' %s", object, type);
6553 if (quals)
6554 cp_error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
6555 object, type);
6556 if (friendp)
6557 cp_error_at ("invalid friend declaration", object);
6558 if (raises)
6559 cp_error_at ("invalid raises declaration", object);
6560 }
6561
6562 /* CTYPE is class type, or null if non-class.
6563 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
6564 or METHOD_TYPE.
6565 DECLARATOR is the function's name.
6566 VIRTUALP is truthvalue of whether the function is virtual or not.
6567 FLAGS are to be passed through to `grokclassfn'.
6568 QUALS are qualifiers indicating whether the function is `const'
6569 or `volatile'.
6570 RAISES is a list of exceptions that this function can raise.
6571 CHECK is 1 if we must find this method in CTYPE, 0 if we should
6572 not look, and -1 if we should not call `grokclassfn' at all. */
6573 static tree
6574 grokfndecl (ctype, type, declarator, virtualp, flags, quals,
6575 raises, check, publicp)
6576 tree ctype, type;
6577 tree declarator;
6578 int virtualp;
6579 enum overload_flags flags;
6580 tree quals, raises;
6581 int check, publicp;
6582 {
6583 tree cname, decl;
6584 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
6585
6586 if (ctype)
6587 cname = TREE_CODE (TYPE_NAME (ctype)) == TYPE_DECL
6588 ? TYPE_IDENTIFIER (ctype) : TYPE_NAME (ctype);
6589 else
6590 cname = NULL_TREE;
6591
6592 if (raises)
6593 {
6594 type = build_exception_variant (ctype, type, raises);
6595 raises = TYPE_RAISES_EXCEPTIONS (type);
6596 }
6597 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
6598 /* propagate volatile out from type to decl */
6599 if (TYPE_VOLATILE (type))
6600 TREE_THIS_VOLATILE (decl) = 1;
6601
6602 /* Should probably propagate const out from type to decl I bet (mrs). */
6603 if (staticp)
6604 {
6605 DECL_STATIC_FUNCTION_P (decl) = 1;
6606 DECL_CONTEXT (decl) = ctype;
6607 DECL_CLASS_CONTEXT (decl) = ctype;
6608 }
6609
6610 if (publicp)
6611 TREE_PUBLIC (decl) = 1;
6612
6613 DECL_EXTERNAL (decl) = 1;
6614 if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
6615 {
6616 cp_error ("%smember function `%D' cannot have `%T' method qualifier",
6617 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
6618 quals = NULL_TREE;
6619 }
6620
6621 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
6622 grok_op_properties (decl, virtualp, check < 0);
6623
6624 /* Caller will do the rest of this. */
6625 if (check < 0)
6626 return decl;
6627
6628 if (flags == NO_SPECIAL && ctype && constructor_name (cname) == declarator)
6629 {
6630 tree tmp;
6631 /* Just handle constructors here. We could do this
6632 inside the following if stmt, but I think
6633 that the code is more legible by breaking this
6634 case out. See comments below for what each of
6635 the following calls is supposed to do. */
6636 DECL_CONSTRUCTOR_P (decl) = 1;
6637
6638 grokclassfn (ctype, declarator, decl, flags, quals);
6639 if (check)
6640 check_classfn (ctype, declarator, decl);
6641 if (! grok_ctor_properties (ctype, decl))
6642 return NULL_TREE;
6643
6644 if (check == 0 && ! current_function_decl)
6645 {
6646 /* FIXME: this should only need to look at
6647 IDENTIFIER_GLOBAL_VALUE. */
6648 tmp = lookup_name (DECL_ASSEMBLER_NAME (decl), 0);
6649 if (tmp == NULL_TREE)
6650 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (decl)) = decl;
6651 else if (TREE_CODE (tmp) != TREE_CODE (decl))
6652 cp_error ("inconsistent declarations for `%D'", decl);
6653 else
6654 {
6655 duplicate_decls (decl, tmp);
6656 decl = tmp;
6657 /* avoid creating circularities. */
6658 DECL_CHAIN (decl) = NULL_TREE;
6659 }
6660 make_decl_rtl (decl, NULL_PTR, 1);
6661 }
6662 }
6663 else
6664 {
6665 tree tmp;
6666
6667 /* Function gets the ugly name, field gets the nice one.
6668 This call may change the type of the function (because
6669 of default parameters)! */
6670 if (ctype != NULL_TREE)
6671 grokclassfn (ctype, cname, decl, flags, quals);
6672
6673 if (ctype != NULL_TREE && check)
6674 check_classfn (ctype, cname, decl);
6675
6676 if (ctype == NULL_TREE || check)
6677 return decl;
6678
6679 /* Now install the declaration of this function so that others may
6680 find it (esp. its DECL_FRIENDLIST). Don't do this for local class
6681 methods, though. */
6682 if (! current_function_decl)
6683 {
6684 /* FIXME: this should only need to look at
6685 IDENTIFIER_GLOBAL_VALUE. */
6686 tmp = lookup_name (DECL_ASSEMBLER_NAME (decl), 0);
6687 if (tmp == NULL_TREE)
6688 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (decl)) = decl;
6689 else if (TREE_CODE (tmp) != TREE_CODE (decl))
6690 cp_error ("inconsistent declarations for `%D'", decl);
6691 else
6692 {
6693 duplicate_decls (decl, tmp);
6694 decl = tmp;
6695 /* avoid creating circularities. */
6696 DECL_CHAIN (decl) = NULL_TREE;
6697 }
6698 make_decl_rtl (decl, NULL_PTR, 1);
6699 }
6700
6701 /* If this declaration supersedes the declaration of
6702 a method declared virtual in the base class, then
6703 mark this field as being virtual as well. */
6704 {
6705 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
6706 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
6707
6708 for (i = 0; i < n_baselinks; i++)
6709 {
6710 tree base_binfo = TREE_VEC_ELT (binfos, i);
6711 if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo))
6712 || flag_all_virtual == 1)
6713 {
6714 tmp = get_matching_virtual (base_binfo, decl,
6715 flags == DTOR_FLAG);
6716 if (tmp)
6717 {
6718 /* If this function overrides some virtual in some base
6719 class, then the function itself is also necessarily
6720 virtual, even if the user didn't explicitly say so. */
6721 DECL_VIRTUAL_P (decl) = 1;
6722
6723 /* The TMP we really want is the one from the deepest
6724 baseclass on this path, taking care not to
6725 duplicate if we have already found it (via another
6726 path to its virtual baseclass. */
6727 if (staticp)
6728 {
6729 cp_error ("method `%D' may not be declared static",
6730 decl);
6731 cp_error_at ("(since `%D' declared virtual in base class.)",
6732 tmp);
6733 break;
6734 }
6735 virtualp = 1;
6736
6737 {
6738 /* The argument types may have changed... */
6739 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
6740 tree base_variant = TREE_TYPE (TREE_VALUE (argtypes));
6741
6742 argtypes = commonparms (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (tmp))),
6743 TREE_CHAIN (argtypes));
6744 /* But the return type has not. */
6745 type = build_cplus_method_type (base_variant, TREE_TYPE (type), argtypes);
6746 if (raises)
6747 {
6748 type = build_exception_variant (ctype, type, raises);
6749 raises = TYPE_RAISES_EXCEPTIONS (type);
6750 }
6751 TREE_TYPE (decl) = type;
6752 DECL_VINDEX (decl)
6753 = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl));
6754 }
6755 break;
6756 }
6757 }
6758 }
6759 }
6760 if (virtualp)
6761 {
6762 if (DECL_VINDEX (decl) == NULL_TREE)
6763 DECL_VINDEX (decl) = error_mark_node;
6764 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
6765 if (ctype && CLASSTYPE_VTABLE_NEEDS_WRITING (ctype)
6766 /* If this function is derived from a template, don't
6767 make it public. This shouldn't be here, but there's
6768 no good way to override the interface pragmas for one
6769 function or class only. Bletch. */
6770 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (ctype)) == NULL_TREE
6771 && (write_virtuals == 2
6772 || (write_virtuals == 3
6773 && CLASSTYPE_INTERFACE_KNOWN (ctype))))
6774 TREE_PUBLIC (decl) = 1;
6775 }
6776 }
6777 return decl;
6778 }
6779
6780 static tree
6781 grokvardecl (type, declarator, specbits, initialized)
6782 tree type;
6783 tree declarator;
6784 RID_BIT_TYPE specbits;
6785 int initialized;
6786 {
6787 tree decl;
6788
6789 if (TREE_CODE (type) == OFFSET_TYPE)
6790 {
6791 /* If you declare a static member so that it
6792 can be initialized, the code will reach here. */
6793 tree basetype = TYPE_OFFSET_BASETYPE (type);
6794 type = TREE_TYPE (type);
6795 decl = build_lang_field_decl (VAR_DECL, declarator, type);
6796 DECL_CONTEXT (decl) = basetype;
6797 DECL_CLASS_CONTEXT (decl) = basetype;
6798 }
6799 else
6800 decl = build_decl (VAR_DECL, declarator, type);
6801
6802 if (RIDBIT_SETP (RID_EXTERN, specbits))
6803 {
6804 DECL_THIS_EXTERN (decl) = 1;
6805 DECL_EXTERNAL (decl) = !initialized;
6806 }
6807
6808 /* In class context, static means one per class,
6809 public access, and static storage. */
6810 if (DECL_FIELD_CONTEXT (decl) != NULL_TREE
6811 && IS_AGGR_TYPE (DECL_FIELD_CONTEXT (decl)))
6812 {
6813 TREE_PUBLIC (decl) = 1;
6814 TREE_STATIC (decl) = 1;
6815 DECL_EXTERNAL (decl) = 0;
6816 }
6817 /* At top level, either `static' or no s.c. makes a definition
6818 (perhaps tentative), and absence of `static' makes it public. */
6819 else if (current_binding_level == global_binding_level)
6820 {
6821 TREE_PUBLIC (decl) = RIDBIT_NOTSETP (RID_STATIC, specbits);
6822 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
6823 }
6824 /* Not at top level, only `static' makes a static definition. */
6825 else
6826 {
6827 TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
6828 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
6829 }
6830 return decl;
6831 }
6832
6833 /* Create a canonical pointer to member function type. */
6834
6835 tree
6836 build_ptrmemfunc_type (type)
6837 tree type;
6838 {
6839 tree fields[4];
6840 tree t;
6841 tree u;
6842
6843 /* If a canonical type already exists for this type, use it. We use
6844 this method instead of type_hash_canon, because it only does a
6845 simple equality check on the list of field members. */
6846
6847 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
6848 return t;
6849
6850 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
6851
6852 u = make_lang_type (UNION_TYPE);
6853 fields[0] = build_lang_field_decl (FIELD_DECL, pfn_identifier, type);
6854 fields[1] = build_lang_field_decl (FIELD_DECL, delta2_identifier,
6855 delta_type_node);
6856 finish_builtin_type (u, "__ptrmemfunc_type", fields, 1, ptr_type_node);
6857 TYPE_NAME (u) = NULL_TREE;
6858
6859 t = make_lang_type (RECORD_TYPE);
6860
6861 /* Let the front-end know this is a pointer to member function. */
6862 TYPE_PTRMEMFUNC_FLAG(t) = 1;
6863
6864 fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier,
6865 delta_type_node);
6866 fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier,
6867 delta_type_node);
6868 fields[2] = build_lang_field_decl (FIELD_DECL, pfn_or_delta2_identifier, u);
6869 finish_builtin_type (t, "__ptrmemfunc_type", fields, 2, ptr_type_node);
6870
6871 pop_obstacks ();
6872
6873 /* Zap out the name so that the back-end will give us the debugging
6874 information for this anonymous RECORD_TYPE. */
6875 TYPE_NAME (t) = NULL_TREE;
6876
6877 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
6878
6879 /* Seems to be wanted. */
6880 CLASSTYPE_GOT_SEMICOLON (t) = 1;
6881 return t;
6882 }
6883
6884 /* Given declspecs and a declarator,
6885 determine the name and type of the object declared
6886 and construct a ..._DECL node for it.
6887 (In one case we can return a ..._TYPE node instead.
6888 For invalid input we sometimes return 0.)
6889
6890 DECLSPECS is a chain of tree_list nodes whose value fields
6891 are the storage classes and type specifiers.
6892
6893 DECL_CONTEXT says which syntactic context this declaration is in:
6894 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6895 FUNCDEF for a function definition. Like NORMAL but a few different
6896 error messages in each case. Return value may be zero meaning
6897 this definition is too screwy to try to parse.
6898 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
6899 handle member functions (which have FIELD context).
6900 Return value may be zero meaning this definition is too screwy to
6901 try to parse.
6902 PARM for a parameter declaration (either within a function prototype
6903 or before a function body). Make a PARM_DECL, or return void_type_node.
6904 TYPENAME if for a typename (in a cast or sizeof).
6905 Don't make a DECL node; just return the ..._TYPE node.
6906 FIELD for a struct or union field; make a FIELD_DECL.
6907 BITFIELD for a field with specified width.
6908 INITIALIZED is 1 if the decl has an initializer.
6909
6910 In the TYPENAME case, DECLARATOR is really an absolute declarator.
6911 It may also be so in the PARM case, for a prototype where the
6912 argument type is specified but not the name.
6913
6914 This function is where the complicated C meanings of `static'
6915 and `extern' are interpreted.
6916
6917 For C++, if there is any monkey business to do, the function which
6918 calls this one must do it, i.e., prepending instance variables,
6919 renaming overloaded function names, etc.
6920
6921 Note that for this C++, it is an error to define a method within a class
6922 which does not belong to that class.
6923
6924 Except in the case where SCOPE_REFs are implicitly known (such as
6925 methods within a class being redundantly qualified),
6926 declarations which involve SCOPE_REFs are returned as SCOPE_REFs
6927 (class_name::decl_name). The caller must also deal with this.
6928
6929 If a constructor or destructor is seen, and the context is FIELD,
6930 then the type gains the attribute TREE_HAS_x. If such a declaration
6931 is erroneous, NULL_TREE is returned.
6932
6933 QUALS is used only for FUNCDEF and MEMFUNCDEF cases. For a member
6934 function, these are the qualifiers to give to the `this' pointer.
6935
6936 May return void_type_node if the declarator turned out to be a friend.
6937 See grokfield for details. */
6938
6939 enum return_types { return_normal, return_ctor, return_dtor, return_conversion };
6940
6941 tree
6942 grokdeclarator (declarator, declspecs, decl_context, initialized, raises)
6943 tree declspecs;
6944 tree declarator;
6945 enum decl_context decl_context;
6946 int initialized;
6947 tree raises;
6948 {
6949 RID_BIT_TYPE specbits;
6950 int nclasses = 0;
6951 tree spec;
6952 tree type = NULL_TREE;
6953 int longlong = 0;
6954 int constp;
6955 int volatilep;
6956 int virtualp, friendp, inlinep, staticp;
6957 int explicit_int = 0;
6958 int explicit_char = 0;
6959 int opaque_typedef = 0;
6960 tree typedef_decl = NULL_TREE;
6961 char *name;
6962 tree typedef_type = NULL_TREE;
6963 int funcdef_flag = 0;
6964 enum tree_code innermost_code = ERROR_MARK;
6965 int bitfield = 0;
6966 int size_varies = 0;
6967 /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
6968 All FIELD_DECLs we build here have `init' put into their DECL_INITIAL. */
6969 tree init = NULL_TREE;
6970
6971 /* Keep track of what sort of function is being processed
6972 so that we can warn about default return values, or explicit
6973 return values which do not match prescribed defaults. */
6974 enum return_types return_type = return_normal;
6975
6976 tree dname = NULL_TREE;
6977 tree ctype = current_class_type;
6978 tree ctor_return_type = NULL_TREE;
6979 enum overload_flags flags = NO_SPECIAL;
6980 tree quals = NULL_TREE;
6981
6982 RIDBIT_RESET_ALL (specbits);
6983 if (decl_context == FUNCDEF)
6984 funcdef_flag = 1, decl_context = NORMAL;
6985 else if (decl_context == MEMFUNCDEF)
6986 funcdef_flag = -1, decl_context = FIELD;
6987 else if (decl_context == BITFIELD)
6988 bitfield = 1, decl_context = FIELD;
6989
6990 if (flag_traditional && allocation_temporary_p ())
6991 end_temporary_allocation ();
6992
6993 /* Look inside a declarator for the name being declared
6994 and get it as a string, for an error message. */
6995 {
6996 tree last = NULL_TREE;
6997 register tree decl = declarator;
6998 name = NULL;
6999
7000 while (decl)
7001 switch (TREE_CODE (decl))
7002 {
7003 case COND_EXPR:
7004 ctype = NULL_TREE;
7005 decl = TREE_OPERAND (decl, 0);
7006 break;
7007
7008 case BIT_NOT_EXPR: /* for C++ destructors! */
7009 {
7010 tree name = TREE_OPERAND (decl, 0);
7011 tree rename = NULL_TREE;
7012
7013 my_friendly_assert (flags == NO_SPECIAL, 152);
7014 flags = DTOR_FLAG;
7015 return_type = return_dtor;
7016 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
7017 if (ctype == NULL_TREE)
7018 {
7019 if (current_class_type == NULL_TREE)
7020 {
7021 error ("destructors must be member functions");
7022 flags = NO_SPECIAL;
7023 }
7024 else
7025 {
7026 tree t = constructor_name (current_class_name);
7027 if (t != name)
7028 rename = t;
7029 }
7030 }
7031 else
7032 {
7033 tree t = constructor_name (ctype);
7034 if (t != name)
7035 rename = t;
7036 }
7037
7038 if (rename)
7039 {
7040 error ("destructor `%s' must match class name `%s'",
7041 IDENTIFIER_POINTER (name),
7042 IDENTIFIER_POINTER (rename));
7043 TREE_OPERAND (decl, 0) = rename;
7044 }
7045 decl = name;
7046 }
7047 break;
7048
7049 case ADDR_EXPR: /* C++ reference declaration */
7050 /* fall through */
7051 case ARRAY_REF:
7052 case INDIRECT_REF:
7053 ctype = NULL_TREE;
7054 innermost_code = TREE_CODE (decl);
7055 last = decl;
7056 decl = TREE_OPERAND (decl, 0);
7057 break;
7058
7059 case CALL_EXPR:
7060 if (parmlist_is_exprlist (TREE_OPERAND (decl, 1)))
7061 {
7062 /* This is actually a variable declaration using constructor
7063 syntax. We need to call start_decl and finish_decl so we
7064 can get the variable initialized... */
7065
7066 if (last)
7067 /* We need to insinuate ourselves into the declarator in place
7068 of the CALL_EXPR. */
7069 TREE_OPERAND (last, 0) = TREE_OPERAND (decl, 0);
7070 else
7071 declarator = TREE_OPERAND (decl, 0);
7072
7073 init = TREE_OPERAND (decl, 1);
7074
7075 decl = start_decl (declarator, declspecs, 1, NULL_TREE);
7076 finish_decl (decl, init, NULL_TREE, 1);
7077 return 0;
7078 }
7079 innermost_code = TREE_CODE (decl);
7080 decl = TREE_OPERAND (decl, 0);
7081 if (decl_context == FIELD && ctype == NULL_TREE)
7082 ctype = current_class_type;
7083 if (ctype != NULL_TREE
7084 && decl != NULL_TREE && flags != DTOR_FLAG
7085 && decl == constructor_name (ctype))
7086 {
7087 return_type = return_ctor;
7088 ctor_return_type = ctype;
7089 }
7090 ctype = NULL_TREE;
7091 break;
7092
7093 case IDENTIFIER_NODE:
7094 dname = decl;
7095 decl = NULL_TREE;
7096
7097 if (IDENTIFIER_OPNAME_P (dname))
7098 {
7099 if (IDENTIFIER_TYPENAME_P (dname))
7100 {
7101 my_friendly_assert (flags == NO_SPECIAL, 154);
7102 flags = TYPENAME_FLAG;
7103 ctor_return_type = TREE_TYPE (dname);
7104 return_type = return_conversion;
7105 }
7106 name = operator_name_string (dname);
7107 }
7108 else
7109 name = IDENTIFIER_POINTER (dname);
7110 break;
7111
7112 case RECORD_TYPE:
7113 case UNION_TYPE:
7114 case ENUMERAL_TYPE:
7115 /* Parse error puts this typespec where
7116 a declarator should go. */
7117 error ("declarator name missing");
7118 dname = TYPE_NAME (decl);
7119 if (dname && TREE_CODE (dname) == TYPE_DECL)
7120 dname = DECL_NAME (dname);
7121 name = dname ? IDENTIFIER_POINTER (dname) : "<nameless>";
7122 declspecs = temp_tree_cons (NULL_TREE, decl, declspecs);
7123 decl = NULL_TREE;
7124 break;
7125
7126 /* C++ extension */
7127 case SCOPE_REF:
7128 {
7129 /* Perform error checking, and convert class names to types.
7130 We may call grokdeclarator multiple times for the same
7131 tree structure, so only do the conversion once. In this
7132 case, we have exactly what we want for `ctype'. */
7133 tree cname = TREE_OPERAND (decl, 0);
7134 if (cname == NULL_TREE)
7135 ctype = NULL_TREE;
7136 /* Can't use IS_AGGR_TYPE because CNAME might not be a type. */
7137 else if (IS_AGGR_TYPE_CODE (TREE_CODE (cname))
7138 || TREE_CODE (cname) == UNINSTANTIATED_P_TYPE)
7139 ctype = cname;
7140 else if (! is_aggr_typedef (cname, 1))
7141 {
7142 TREE_OPERAND (decl, 0) = NULL_TREE;
7143 }
7144 /* Must test TREE_OPERAND (decl, 1), in case user gives
7145 us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */
7146 else if (TREE_OPERAND (decl, 1)
7147 && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
7148 {
7149 TREE_OPERAND (decl, 0) = IDENTIFIER_TYPE_VALUE (cname);
7150 }
7151 else if (ctype == NULL_TREE)
7152 {
7153 ctype = IDENTIFIER_TYPE_VALUE (cname);
7154 TREE_OPERAND (decl, 0) = ctype;
7155 }
7156 else if (TREE_COMPLEXITY (decl) == current_class_depth)
7157 TREE_OPERAND (decl, 0) = ctype;
7158 else
7159 {
7160 if (! UNIQUELY_DERIVED_FROM_P (IDENTIFIER_TYPE_VALUE (cname),
7161 ctype))
7162 {
7163 cp_error ("type `%T' is not derived from type `%T'",
7164 IDENTIFIER_TYPE_VALUE (cname), ctype);
7165 TREE_OPERAND (decl, 0) = NULL_TREE;
7166 }
7167 else
7168 {
7169 ctype = IDENTIFIER_TYPE_VALUE (cname);
7170 TREE_OPERAND (decl, 0) = ctype;
7171 }
7172 }
7173
7174 decl = TREE_OPERAND (decl, 1);
7175 if (ctype)
7176 {
7177 if (TREE_CODE (decl) == IDENTIFIER_NODE
7178 && constructor_name (ctype) == decl)
7179 {
7180 return_type = return_ctor;
7181 ctor_return_type = ctype;
7182 }
7183 else if (TREE_CODE (decl) == BIT_NOT_EXPR
7184 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
7185 && constructor_name (ctype) == TREE_OPERAND (decl, 0))
7186 {
7187 return_type = return_dtor;
7188 ctor_return_type = ctype;
7189 flags = DTOR_FLAG;
7190 decl = TREE_OPERAND (decl, 0);
7191 }
7192 }
7193 }
7194 break;
7195
7196 case ERROR_MARK:
7197 decl = NULL_TREE;
7198 break;
7199
7200 default:
7201 return 0; /* We used to do a 155 abort here. */
7202 }
7203 if (name == NULL)
7204 name = "type name";
7205 }
7206
7207 /* A function definition's declarator must have the form of
7208 a function declarator. */
7209
7210 if (funcdef_flag && innermost_code != CALL_EXPR)
7211 return 0;
7212
7213 /* Anything declared one level down from the top level
7214 must be one of the parameters of a function
7215 (because the body is at least two levels down). */
7216
7217 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
7218 by not allowing C++ class definitions to specify their parameters
7219 with xdecls (must be spec.d in the parmlist).
7220
7221 Since we now wait to push a class scope until we are sure that
7222 we are in a legitimate method context, we must set oldcname
7223 explicitly (since current_class_name is not yet alive). */
7224
7225 if (decl_context == NORMAL
7226 && current_binding_level->level_chain == global_binding_level)
7227 decl_context = PARM;
7228
7229 /* Look through the decl specs and record which ones appear.
7230 Some typespecs are defined as built-in typenames.
7231 Others, the ones that are modifiers of other types,
7232 are represented by bits in SPECBITS: set the bits for
7233 the modifiers that appear. Storage class keywords are also in SPECBITS.
7234
7235 If there is a typedef name or a type, store the type in TYPE.
7236 This includes builtin typedefs such as `int'.
7237
7238 Set EXPLICIT_INT if the type is `int' or `char' and did not
7239 come from a user typedef.
7240
7241 Set LONGLONG if `long' is mentioned twice.
7242
7243 For C++, constructors and destructors have their own fast treatment. */
7244
7245 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
7246 {
7247 register int i;
7248 register tree id;
7249
7250 /* Certain parse errors slip through. For example,
7251 `int class;' is not caught by the parser. Try
7252 weakly to recover here. */
7253 if (TREE_CODE (spec) != TREE_LIST)
7254 return 0;
7255
7256 id = TREE_VALUE (spec);
7257
7258 if (TREE_CODE (id) == IDENTIFIER_NODE)
7259 {
7260 if (id == ridpointers[(int) RID_INT]
7261 || id == ridpointers[(int) RID_CHAR]
7262 || id == ridpointers[(int) RID_BOOL]
7263 || id == ridpointers[(int) RID_WCHAR])
7264 {
7265 if (type)
7266 error ("extraneous `%T' ignored", id);
7267 else
7268 {
7269 if (id == ridpointers[(int) RID_INT])
7270 explicit_int = 1;
7271 else if (id == ridpointers[(int) RID_CHAR])
7272 explicit_char = 1;
7273 type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
7274 }
7275 goto found;
7276 }
7277 /* C++ aggregate types. */
7278 if (IDENTIFIER_HAS_TYPE_VALUE (id))
7279 {
7280 if (type)
7281 cp_error ("multiple declarations `%T' and `%T'", type, id);
7282 else
7283 type = IDENTIFIER_TYPE_VALUE (id);
7284 goto found;
7285 }
7286
7287 for (i = (int) RID_FIRST_MODIFIER; i < (int) RID_MAX; i++)
7288 {
7289 if (ridpointers[i] == id)
7290 {
7291 if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
7292 {
7293 if (pedantic && flag_ansi)
7294 pedwarn ("duplicate `long'");
7295 else if (longlong)
7296 error ("`long long long' is too long for GCC");
7297 else
7298 longlong = 1;
7299 }
7300 else if (RIDBIT_SETP (i, specbits))
7301 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
7302 RIDBIT_SET (i, specbits);
7303 goto found;
7304 }
7305 }
7306 }
7307 if (type)
7308 error ("two or more data types in declaration of `%s'", name);
7309 else if (TREE_CODE (id) == IDENTIFIER_NODE)
7310 {
7311 register tree t = lookup_name (id, 1);
7312 if (!t || TREE_CODE (t) != TYPE_DECL)
7313 error ("`%s' fails to be a typedef or built in type",
7314 IDENTIFIER_POINTER (id));
7315 else
7316 {
7317 type = TREE_TYPE (t);
7318 typedef_decl = t;
7319 }
7320 }
7321 else if (TREE_CODE (id) != ERROR_MARK)
7322 /* Can't change CLASS nodes into RECORD nodes here! */
7323 type = id;
7324
7325 found: ;
7326 }
7327
7328 typedef_type = type;
7329
7330 /* No type at all: default to `int', and set EXPLICIT_INT
7331 because it was not a user-defined typedef.
7332 Except when we have a `typedef' inside a signature, in
7333 which case the type defaults to `unknown type' and is
7334 instantiated when assigning to a signature pointer or ref. */
7335
7336 if (type == NULL_TREE
7337 && (RIDBIT_SETP (RID_SIGNED, specbits)
7338 || RIDBIT_SETP (RID_UNSIGNED, specbits)
7339 || RIDBIT_SETP (RID_LONG, specbits)
7340 || RIDBIT_SETP (RID_SHORT, specbits)))
7341 {
7342 /* These imply 'int'. */
7343 type = integer_type_node;
7344 explicit_int = 1;
7345 }
7346
7347 if (type == NULL_TREE)
7348 {
7349 explicit_int = -1;
7350 if (return_type == return_dtor)
7351 type = void_type_node;
7352 else if (return_type == return_ctor)
7353 type = TYPE_POINTER_TO (ctor_return_type);
7354 else if (return_type == return_conversion)
7355 type = ctor_return_type;
7356 else if (current_class_type
7357 && IS_SIGNATURE (current_class_type)
7358 && (RIDBIT_SETP (RID_TYPEDEF, specbits)
7359 || SIGNATURE_GROKKING_TYPEDEF (current_class_type))
7360 && (decl_context == FIELD || decl_context == NORMAL))
7361 {
7362 explicit_int = 0;
7363 opaque_typedef = 1;
7364 type = copy_node (opaque_type_node);
7365 }
7366 /* access declaration */
7367 else if (decl_context == FIELD && declarator
7368 && TREE_CODE (declarator) == SCOPE_REF)
7369 type = void_type_node;
7370 else
7371 {
7372 if (funcdef_flag)
7373 {
7374 if (warn_return_type
7375 && return_type == return_normal)
7376 /* Save warning until we know what is really going on. */
7377 warn_about_return_type = 1;
7378 }
7379 else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
7380 pedwarn ("ANSI C++ forbids typedef which does not specify a type");
7381 else if (declspecs == NULL_TREE &&
7382 (innermost_code != CALL_EXPR || pedantic))
7383 cp_pedwarn ("ANSI C++ forbids declaration `%D' with no type or storage class",
7384 dname);
7385 type = integer_type_node;
7386 }
7387 }
7388 else if (return_type == return_dtor)
7389 {
7390 error ("return type specification for destructor invalid");
7391 type = void_type_node;
7392 }
7393 else if (return_type == return_ctor)
7394 {
7395 error ("return type specification for constructor invalid");
7396 type = TYPE_POINTER_TO (ctor_return_type);
7397 }
7398 else if (return_type == return_conversion)
7399 {
7400 if (comp_target_types (type, ctor_return_type, 1) == 0)
7401 cp_error ("operator `%T' declared to return `%T'",
7402 ctor_return_type, type);
7403 else
7404 cp_pedwarn ("return type specified for `operator %T'",
7405 ctor_return_type);
7406
7407 type = ctor_return_type;
7408 }
7409 /* Catch typedefs that only specify a type, like 'typedef int;'. */
7410 else if (RIDBIT_SETP (RID_TYPEDEF, specbits) && declarator == NULL_TREE)
7411 {
7412 /* Template "this is a type" syntax; just ignore for now. */
7413 if (processing_template_defn)
7414 return void_type_node;
7415 }
7416
7417 ctype = NULL_TREE;
7418
7419 /* Now process the modifiers that were specified
7420 and check for invalid combinations. */
7421
7422 /* Long double is a special combination. */
7423
7424 if (RIDBIT_SETP (RID_LONG, specbits)
7425 && TYPE_MAIN_VARIANT (type) == double_type_node)
7426 {
7427 RIDBIT_RESET (RID_LONG, specbits);
7428 type = build_type_variant (long_double_type_node, TYPE_READONLY (type),
7429 TYPE_VOLATILE (type));
7430 }
7431
7432 /* Check all other uses of type modifiers. */
7433
7434 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
7435 || RIDBIT_SETP (RID_SIGNED, specbits)
7436 || RIDBIT_SETP (RID_LONG, specbits)
7437 || RIDBIT_SETP (RID_SHORT, specbits))
7438 {
7439 int ok = 0;
7440
7441 if (TREE_CODE (type) == REAL_TYPE)
7442 error ("short, signed or unsigned invalid for `%s'", name);
7443 else if (TREE_CODE (type) != INTEGER_TYPE || type == wchar_type_node)
7444 error ("long, short, signed or unsigned invalid for `%s'", name);
7445 else if (RIDBIT_SETP (RID_LONG, specbits)
7446 && RIDBIT_SETP (RID_SHORT, specbits))
7447 error ("long and short specified together for `%s'", name);
7448 else if ((RIDBIT_SETP (RID_LONG, specbits)
7449 || RIDBIT_SETP (RID_SHORT, specbits))
7450 && explicit_char)
7451 error ("long or short specified with char for `%s'", name);
7452 else if ((RIDBIT_SETP (RID_LONG, specbits)
7453 || RIDBIT_SETP (RID_SHORT, specbits))
7454 && TREE_CODE (type) == REAL_TYPE)
7455 error ("long or short specified with floating type for `%s'", name);
7456 else if (RIDBIT_SETP (RID_SIGNED, specbits)
7457 && RIDBIT_SETP (RID_UNSIGNED, specbits))
7458 error ("signed and unsigned given together for `%s'", name);
7459 else
7460 {
7461 ok = 1;
7462 if (!explicit_int && !explicit_char && pedantic)
7463 {
7464 pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
7465 name);
7466 if (flag_pedantic_errors)
7467 ok = 0;
7468 }
7469 }
7470
7471 /* Discard the type modifiers if they are invalid. */
7472 if (! ok)
7473 {
7474 RIDBIT_RESET (RID_UNSIGNED, specbits);
7475 RIDBIT_RESET (RID_SIGNED, specbits);
7476 RIDBIT_RESET (RID_LONG, specbits);
7477 RIDBIT_RESET (RID_SHORT, specbits);
7478 longlong = 0;
7479 }
7480 }
7481
7482 /* Decide whether an integer type is signed or not.
7483 Optionally treat bitfields as signed by default. */
7484 if (RIDBIT_SETP (RID_UNSIGNED, specbits)
7485 /* Traditionally, all bitfields are unsigned. */
7486 || (bitfield && flag_traditional)
7487 || (bitfield && ! flag_signed_bitfields
7488 && (explicit_int || explicit_char
7489 /* A typedef for plain `int' without `signed'
7490 can be controlled just like plain `int'. */
7491 || ! (typedef_decl != NULL_TREE
7492 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
7493 && TREE_CODE (type) != ENUMERAL_TYPE
7494 && RIDBIT_NOTSETP (RID_SIGNED, specbits)))
7495 {
7496 if (longlong)
7497 type = long_long_unsigned_type_node;
7498 else if (RIDBIT_SETP (RID_LONG, specbits))
7499 type = long_unsigned_type_node;
7500 else if (RIDBIT_SETP (RID_SHORT, specbits))
7501 type = short_unsigned_type_node;
7502 else if (type == char_type_node)
7503 type = unsigned_char_type_node;
7504 else if (typedef_decl)
7505 type = unsigned_type (type);
7506 else
7507 type = unsigned_type_node;
7508 }
7509 else if (RIDBIT_SETP (RID_SIGNED, specbits)
7510 && type == char_type_node)
7511 type = signed_char_type_node;
7512 else if (longlong)
7513 type = long_long_integer_type_node;
7514 else if (RIDBIT_SETP (RID_LONG, specbits))
7515 type = long_integer_type_node;
7516 else if (RIDBIT_SETP (RID_SHORT, specbits))
7517 type = short_integer_type_node;
7518
7519 /* Set CONSTP if this declaration is `const', whether by
7520 explicit specification or via a typedef.
7521 Likewise for VOLATILEP. */
7522
7523 constp = !! RIDBIT_SETP (RID_CONST, specbits) + TYPE_READONLY (type);
7524 volatilep = !! RIDBIT_SETP (RID_VOLATILE, specbits) + TYPE_VOLATILE (type);
7525 staticp = 0;
7526 inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
7527 if (constp > 1)
7528 warning ("duplicate `const'");
7529 if (volatilep > 1)
7530 warning ("duplicate `volatile'");
7531 virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
7532
7533 if (RIDBIT_SETP (RID_STATIC, specbits))
7534 staticp = 1 + (decl_context == FIELD);
7535
7536 if (virtualp && staticp == 2)
7537 {
7538 cp_error ("member `%D' cannot be declared both virtual and static",
7539 dname);
7540 staticp = 0;
7541 }
7542 friendp = RIDBIT_SETP (RID_FRIEND, specbits);
7543 RIDBIT_RESET (RID_VIRTUAL, specbits);
7544 RIDBIT_RESET (RID_FRIEND, specbits);
7545
7546 if (RIDBIT_SETP (RID_MUTABLE, specbits))
7547 {
7548 if (decl_context == PARM)
7549 {
7550 error ("non-member `%s' cannot be declared mutable", name);
7551 RIDBIT_RESET (RID_MUTABLE, specbits);
7552 }
7553 else if (friendp || decl_context == TYPENAME)
7554 {
7555 error ("non-object member `%s' cannot be declared mutable", name);
7556 RIDBIT_RESET (RID_MUTABLE, specbits);
7557 }
7558 else if (staticp)
7559 {
7560 error ("static `%s' cannot be declared mutable", name);
7561 RIDBIT_RESET (RID_MUTABLE, specbits);
7562 }
7563 #if 0
7564 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
7565 {
7566 error ("non-object member `%s' cannot be declared mutable", name);
7567 RIDBIT_RESET (RID_MUTABLE, specbits);
7568 }
7569 /* Because local typedefs are parsed twice, we don't want this
7570 message here. */
7571 else if (decl_context != FIELD)
7572 {
7573 error ("non-member `%s' cannot be declared mutable", name);
7574 RIDBIT_RESET (RID_MUTABLE, specbits);
7575 }
7576 #endif
7577 }
7578
7579 /* Warn if two storage classes are given. Default to `auto'. */
7580
7581 if (RIDBIT_ANY_SET (specbits))
7582 {
7583 if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
7584 if (RIDBIT_SETP (RID_EXTERN, specbits)) nclasses++;
7585 if (decl_context == PARM && nclasses > 0)
7586 error ("storage class specifiers invalid in parameter declarations");
7587 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
7588 {
7589 if (decl_context == PARM)
7590 error ("typedef declaration invalid in parameter declaration");
7591 nclasses++;
7592 }
7593 if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
7594 if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
7595 }
7596
7597 /* Give error if `virtual' is used outside of class declaration. */
7598 if (virtualp && current_class_name == NULL_TREE)
7599 {
7600 error ("virtual outside class declaration");
7601 virtualp = 0;
7602 }
7603 if (current_class_name == NULL_TREE && RIDBIT_SETP (RID_MUTABLE, specbits))
7604 {
7605 error ("only members can be declared mutable");
7606 RIDBIT_RESET (RID_MUTABLE, specbits);
7607 }
7608
7609 /* Static anonymous unions are dealt with here. */
7610 if (staticp && decl_context == TYPENAME
7611 && TREE_CODE (declspecs) == TREE_LIST
7612 && TREE_CODE (TREE_VALUE (declspecs)) == UNION_TYPE
7613 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (TREE_VALUE (declspecs))))
7614 decl_context = FIELD;
7615
7616 /* Give error if `const,' `volatile,' `inline,' `friend,' or `virtual'
7617 is used in a signature member function declaration. */
7618 if (decl_context == FIELD
7619 && IS_SIGNATURE (current_class_type)
7620 && RIDBIT_NOTSETP(RID_TYPEDEF, specbits)
7621 && !SIGNATURE_GROKKING_TYPEDEF (current_class_type))
7622 {
7623 if (constp)
7624 {
7625 error ("`const' specified for signature member function `%s'", name);
7626 constp = 0;
7627 }
7628 if (volatilep)
7629 {
7630 error ("`volatile' specified for signature member function `%s'",
7631 name);
7632 volatilep = 0;
7633 }
7634 if (inlinep)
7635 {
7636 error ("`inline' specified for signature member function `%s'", name);
7637 /* Later, we'll make signature member functions inline. */
7638 inlinep = 0;
7639 }
7640 if (friendp)
7641 {
7642 error ("`friend' declaration in signature definition");
7643 friendp = 0;
7644 }
7645 if (virtualp)
7646 {
7647 error ("`virtual' specified for signature member function `%s'",
7648 name);
7649 /* Later, we'll make signature member functions virtual. */
7650 virtualp = 0;
7651 }
7652 }
7653
7654 /* Warn about storage classes that are invalid for certain
7655 kinds of declarations (parameters, typenames, etc.). */
7656
7657 if (nclasses > 1)
7658 error ("multiple storage classes in declaration of `%s'", name);
7659 else if (decl_context != NORMAL && nclasses > 0)
7660 {
7661 if (decl_context == PARM
7662 && (RIDBIT_SETP (RID_REGISTER, specbits)
7663 || RIDBIT_SETP (RID_AUTO, specbits)))
7664 ;
7665 else if (decl_context == FIELD
7666 && RIDBIT_SETP (RID_TYPEDEF, specbits))
7667 {
7668 /* Processing a typedef declaration nested within a class type
7669 definition. */
7670 register tree scanner;
7671 register tree previous_declspec;
7672 tree loc_typedecl;
7673
7674 if (initialized)
7675 error ("typedef declaration includes an initializer");
7676
7677 /* To process a class-local typedef declaration, we descend down
7678 the chain of declspecs looking for the `typedef' spec. When
7679 we find it, we replace it with `static', and then recursively
7680 call `grokdeclarator' with the original declarator and with
7681 the newly adjusted declspecs. This call should return a
7682 FIELD_DECL node with the TREE_TYPE (and other parts) set
7683 appropriately. We can then just change the TREE_CODE on that
7684 from FIELD_DECL to TYPE_DECL and we're done. */
7685
7686 for (previous_declspec = NULL_TREE, scanner = declspecs;
7687 scanner;
7688 previous_declspec = scanner, scanner = TREE_CHAIN (scanner))
7689 {
7690 if (TREE_VALUE (scanner) == ridpointers[(int) RID_TYPEDEF])
7691 break;
7692 }
7693
7694 if (scanner == IDENTIFIER_AS_LIST (ridpointers [(int) RID_TYPEDEF]))
7695 {
7696 if (previous_declspec)
7697 TREE_CHAIN (previous_declspec)
7698 = IDENTIFIER_AS_LIST (ridpointers [(int) RID_STATIC]);
7699 else
7700 declspecs
7701 = IDENTIFIER_AS_LIST (ridpointers [(int) RID_STATIC]);
7702 }
7703 else
7704 TREE_VALUE (scanner) = ridpointers[(int) RID_STATIC];
7705
7706 /* In the recursive call to grokdeclarator we need to know
7707 whether we are working on a signature-local typedef. */
7708 if (IS_SIGNATURE (current_class_type))
7709 SIGNATURE_GROKKING_TYPEDEF (current_class_type) = 1;
7710
7711 loc_typedecl =
7712 grokdeclarator (declarator, declspecs, FIELD, 0, NULL_TREE);
7713
7714 if (loc_typedecl != error_mark_node)
7715 {
7716 register int i = sizeof (struct lang_decl_flags) / sizeof (int);
7717 register int *pi;
7718
7719 TREE_SET_CODE (loc_typedecl, TYPE_DECL);
7720
7721 pi = (int *) permalloc (sizeof (struct lang_decl_flags));
7722 while (i > 0)
7723 pi[--i] = 0;
7724 DECL_LANG_SPECIFIC (loc_typedecl) = (struct lang_decl *) pi;
7725 }
7726
7727 if (IS_SIGNATURE (current_class_type))
7728 {
7729 SIGNATURE_GROKKING_TYPEDEF (current_class_type) = 0;
7730 if (loc_typedecl != error_mark_node && opaque_typedef)
7731 SIGNATURE_HAS_OPAQUE_TYPEDECLS (current_class_type) = 1;
7732 }
7733
7734 return loc_typedecl;
7735 }
7736 else if (decl_context == FIELD
7737 && (! IS_SIGNATURE (current_class_type)
7738 || SIGNATURE_GROKKING_TYPEDEF (current_class_type))
7739 /* C++ allows static class elements */
7740 && RIDBIT_SETP (RID_STATIC, specbits))
7741 /* C++ also allows inlines and signed and unsigned elements,
7742 but in those cases we don't come in here. */
7743 ;
7744 else
7745 {
7746 if (decl_context == FIELD)
7747 {
7748 tree tmp = TREE_OPERAND (declarator, 0);
7749 register int op = IDENTIFIER_OPNAME_P (tmp);
7750 error ("storage class specified for %s `%s'",
7751 IS_SIGNATURE (current_class_type)
7752 ? (op
7753 ? "signature member operator"
7754 : "signature member function")
7755 : (op ? "member operator" : "structure field"),
7756 op ? operator_name_string (tmp) : name);
7757 }
7758 else
7759 error ((decl_context == PARM
7760 ? "storage class specified for parameter `%s'"
7761 : "storage class specified for typename"), name);
7762 RIDBIT_RESET (RID_REGISTER, specbits);
7763 RIDBIT_RESET (RID_AUTO, specbits);
7764 RIDBIT_RESET (RID_EXTERN, specbits);
7765
7766 if (decl_context == FIELD && IS_SIGNATURE (current_class_type))
7767 {
7768 RIDBIT_RESET (RID_STATIC, specbits);
7769 staticp = 0;
7770 }
7771 }
7772 }
7773 else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
7774 {
7775 if (current_binding_level == global_binding_level)
7776 {
7777 /* It's common practice (and completely legal) to have a const
7778 be initialized and declared extern. */
7779 if (! constp)
7780 warning ("`%s' initialized and declared `extern'", name);
7781 }
7782 else
7783 error ("`%s' has both `extern' and initializer", name);
7784 }
7785 else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
7786 && current_binding_level != global_binding_level)
7787 error ("nested function `%s' declared `extern'", name);
7788 else if (current_binding_level == global_binding_level)
7789 {
7790 if (RIDBIT_SETP (RID_AUTO, specbits))
7791 error ("top-level declaration of `%s' specifies `auto'", name);
7792 #if 0
7793 if (RIDBIT_SETP (RID_REGISTER, specbits))
7794 error ("top-level declaration of `%s' specifies `register'", name);
7795 #endif
7796 #if 0
7797 /* I'm not sure under what circumstances we should turn
7798 on the extern bit, and under what circumstances we should
7799 warn if other bits are turned on. */
7800 if (decl_context == NORMAL
7801 && RIDBIT_NOSETP (RID_EXTERN, specbits)
7802 && ! root_lang_context_p ())
7803 {
7804 RIDBIT_SET (RID_EXTERN, specbits);
7805 }
7806 #endif
7807 }
7808
7809 /* Now figure out the structure of the declarator proper.
7810 Descend through it, creating more complex types, until we reach
7811 the declared identifier (or NULL_TREE, in an absolute declarator). */
7812
7813 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
7814 {
7815 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
7816 an INDIRECT_REF (for *...),
7817 a CALL_EXPR (for ...(...)),
7818 an identifier (for the name being declared)
7819 or a null pointer (for the place in an absolute declarator
7820 where the name was omitted).
7821 For the last two cases, we have just exited the loop.
7822
7823 For C++ it could also be
7824 a SCOPE_REF (for class :: ...). In this case, we have converted
7825 sensible names to types, and those are the values we use to
7826 qualify the member name.
7827 an ADDR_EXPR (for &...),
7828 a BIT_NOT_EXPR (for destructors)
7829
7830 At this point, TYPE is the type of elements of an array,
7831 or for a function to return, or for a pointer to point to.
7832 After this sequence of ifs, TYPE is the type of the
7833 array or function or pointer, and DECLARATOR has had its
7834 outermost layer removed. */
7835
7836 if (TREE_CODE (type) == ERROR_MARK)
7837 {
7838 if (TREE_CODE (declarator) == SCOPE_REF)
7839 declarator = TREE_OPERAND (declarator, 1);
7840 else
7841 declarator = TREE_OPERAND (declarator, 0);
7842 continue;
7843 }
7844 if (quals != NULL_TREE
7845 && (declarator == NULL_TREE
7846 || TREE_CODE (declarator) != SCOPE_REF))
7847 {
7848 if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
7849 ctype = TYPE_METHOD_BASETYPE (type);
7850 if (ctype != NULL_TREE)
7851 {
7852 #if 0 /* not yet, should get fixed properly later */
7853 tree dummy = make_type_decl (NULL_TREE, type);
7854 #else
7855 tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
7856 #endif
7857 ctype = grok_method_quals (ctype, dummy, quals);
7858 type = TREE_TYPE (dummy);
7859 quals = NULL_TREE;
7860 }
7861 }
7862 switch (TREE_CODE (declarator))
7863 {
7864 case ARRAY_REF:
7865 {
7866 register tree itype = NULL_TREE;
7867 register tree size = TREE_OPERAND (declarator, 1);
7868
7869 declarator = TREE_OPERAND (declarator, 0);
7870
7871 /* Check for some types that there cannot be arrays of. */
7872
7873 if (TYPE_MAIN_VARIANT (type) == void_type_node)
7874 {
7875 cp_error ("declaration of `%D' as array of voids", dname);
7876 type = error_mark_node;
7877 }
7878
7879 if (TREE_CODE (type) == FUNCTION_TYPE)
7880 {
7881 cp_error ("declaration of `%D' as array of functions", dname);
7882 type = error_mark_node;
7883 }
7884
7885 /* ARM $8.4.3: Since you can't have a pointer to a reference,
7886 you can't have arrays of references. If we allowed them,
7887 then we'd be saying x[i] is legal for an array x, but
7888 then you'd have to ask: what does `*(x + i)' mean? */
7889 if (TREE_CODE (type) == REFERENCE_TYPE)
7890 {
7891 if (decl_context == TYPENAME)
7892 cp_error ("cannot make arrays of references");
7893 else
7894 cp_error ("declaration of `%D' as array of references",
7895 dname);
7896 type = error_mark_node;
7897 }
7898
7899 if (TREE_CODE (type) == OFFSET_TYPE)
7900 {
7901 cp_error ("declaration of `%D' as array of data members",
7902 dname);
7903 type = error_mark_node;
7904 }
7905
7906 if (TREE_CODE (type) == METHOD_TYPE)
7907 {
7908 cp_error ("declaration of `%D' as array of function members",
7909 dname);
7910 type = error_mark_node;
7911 }
7912
7913 if (size == error_mark_node)
7914 type = error_mark_node;
7915
7916 if (type == error_mark_node)
7917 continue;
7918
7919 if (size)
7920 {
7921 /* Must suspend_momentary here because the index
7922 type may need to live until the end of the function.
7923 For example, it is used in the declaration of a
7924 variable which requires destructing at the end of
7925 the function; then build_vec_delete will need this
7926 value. */
7927 int yes = suspend_momentary ();
7928 /* might be a cast */
7929 if (TREE_CODE (size) == NOP_EXPR
7930 && TREE_TYPE (size) == TREE_TYPE (TREE_OPERAND (size, 0)))
7931 size = TREE_OPERAND (size, 0);
7932
7933 /* If this is a template parameter, it'll be constant, but
7934 we don't know what the value is yet. */
7935 if (TREE_CODE (size) == TEMPLATE_CONST_PARM)
7936 goto dont_grok_size;
7937
7938 if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
7939 && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE)
7940 {
7941 cp_error ("size of array `%D' has non-integer type",
7942 dname);
7943 size = integer_one_node;
7944 }
7945 if (TREE_READONLY_DECL_P (size))
7946 size = decl_constant_value (size);
7947 if (flag_ansi && integer_zerop (size))
7948 cp_pedwarn ("ANSI C++ forbids zero-size array `%D'", dname);
7949 if (TREE_CONSTANT (size))
7950 {
7951 constant_expression_warning (size);
7952 if (INT_CST_LT (size, integer_zero_node))
7953 {
7954 cp_error ("size of array `%D' is negative", dname);
7955 size = integer_one_node;
7956 }
7957 itype = build_index_type (size_binop (MINUS_EXPR, size,
7958 integer_one_node));
7959 }
7960 else
7961 {
7962 if (flag_ansi)
7963 {
7964 if (dname)
7965 cp_pedwarn ("ANSI C++ forbids variable-size array `%D'",
7966 dname);
7967 else
7968 cp_pedwarn ("ANSI C++ forbids variable-size array");
7969 }
7970 dont_grok_size:
7971 itype =
7972 build_binary_op (MINUS_EXPR, size, integer_one_node, 1);
7973 /* Make sure the array size remains visibly nonconstant
7974 even if it is (eg) a const variable with known value. */
7975 size_varies = 1;
7976 itype = variable_size (itype);
7977 itype = build_index_type (itype);
7978 }
7979 resume_momentary (yes);
7980 }
7981
7982 /* Build the array type itself, then merge any constancy or
7983 volatility into the target type. We must do it in this order
7984 to ensure that the TYPE_MAIN_VARIANT field of the array type
7985 is set correctly. */
7986
7987 type = build_cplus_array_type (type, itype);
7988 if (constp || volatilep)
7989 type = c_build_type_variant (type, constp, volatilep);
7990
7991 ctype = NULL_TREE;
7992 }
7993 break;
7994
7995 case CALL_EXPR:
7996 {
7997 tree arg_types;
7998
7999 /* Declaring a function type.
8000 Make sure we have a valid type for the function to return. */
8001 #if 0
8002 /* Is this an error? Should they be merged into TYPE here? */
8003 if (pedantic && (constp || volatilep))
8004 pedwarn ("function declared to return const or volatile result");
8005 #else
8006 /* Merge any constancy or volatility into the function return
8007 type. */
8008
8009 if (constp || volatilep)
8010 {
8011 type = c_build_type_variant (type, constp, volatilep);
8012 if (IS_AGGR_TYPE (type))
8013 build_pointer_type (type);
8014 constp = 0;
8015 volatilep = 0;
8016 }
8017 #endif
8018
8019 /* Warn about some types functions can't return. */
8020
8021 if (TREE_CODE (type) == FUNCTION_TYPE)
8022 {
8023 error ("`%s' declared as function returning a function", name);
8024 type = integer_type_node;
8025 }
8026 if (TREE_CODE (type) == ARRAY_TYPE)
8027 {
8028 error ("`%s' declared as function returning an array", name);
8029 type = integer_type_node;
8030 }
8031
8032 if (ctype == NULL_TREE
8033 && decl_context == FIELD
8034 && (friendp == 0 || dname == current_class_name))
8035 ctype = current_class_type;
8036
8037 if (ctype && return_type == return_conversion)
8038 TYPE_HAS_CONVERSION (ctype) = 1;
8039 if (ctype && constructor_name (ctype) == dname)
8040 {
8041 /* We are within a class's scope. If our declarator name
8042 is the same as the class name, and we are defining
8043 a function, then it is a constructor/destructor, and
8044 therefore returns a void type. */
8045
8046 if (flags == DTOR_FLAG)
8047 {
8048 /* ANSI C++ June 5 1992 WP 12.4.1. A destructor may
8049 not be declared const or volatile. A destructor
8050 may not be static. */
8051 if (staticp == 2)
8052 error ("destructor cannot be static member function");
8053 if (TYPE_READONLY (type))
8054 {
8055 error ("destructors cannot be declared `const'");
8056 return void_type_node;
8057 }
8058 if (TYPE_VOLATILE (type))
8059 {
8060 error ("destructors cannot be declared `volatile'");
8061 return void_type_node;
8062 }
8063 if (decl_context == FIELD)
8064 {
8065 if (! member_function_or_else (ctype, current_class_type,
8066 "destructor for alien class `%s' cannot be a member"))
8067 return void_type_node;
8068 }
8069 }
8070 else /* it's a constructor. */
8071 {
8072 /* ANSI C++ June 5 1992 WP 12.1.2. A constructor may
8073 not be declared const or volatile. A constructor may
8074 not be virtual. A constructor may not be static. */
8075 if (staticp == 2)
8076 error ("constructor cannot be static member function");
8077 if (virtualp)
8078 {
8079 pedwarn ("constructors cannot be declared virtual");
8080 virtualp = 0;
8081 }
8082 if (TYPE_READONLY (type))
8083 {
8084 error ("constructors cannot be declared `const'");
8085 return void_type_node;
8086 }
8087 if (TYPE_VOLATILE (type))
8088 {
8089 error ("constructors cannot be declared `volatile'");
8090 return void_type_node;
8091 }
8092 {
8093 RID_BIT_TYPE tmp_bits;
8094 bcopy ((void*)&specbits, (void*)&tmp_bits, sizeof(RID_BIT_TYPE));
8095 RIDBIT_RESET (RID_INLINE, tmp_bits);
8096 RIDBIT_RESET (RID_STATIC, tmp_bits);
8097 if (RIDBIT_ANY_SET (tmp_bits))
8098 error ("return value type specifier for constructor ignored");
8099 }
8100 type = TYPE_POINTER_TO (ctype);
8101 if (decl_context == FIELD &&
8102 IS_SIGNATURE (current_class_type))
8103 {
8104 error ("constructor not allowed in signature");
8105 return void_type_node;
8106 }
8107 else if (decl_context == FIELD)
8108 {
8109 if (! member_function_or_else (ctype, current_class_type,
8110 "constructor for alien class `%s' cannot be member"))
8111 return void_type_node;
8112 TYPE_HAS_CONSTRUCTOR (ctype) = 1;
8113 if (return_type != return_ctor)
8114 return NULL_TREE;
8115 }
8116 }
8117 if (decl_context == FIELD)
8118 staticp = 0;
8119 }
8120 else if (friendp && virtualp)
8121 {
8122 /* Cannot be both friend and virtual. */
8123 error ("virtual functions cannot be friends");
8124 RIDBIT_RESET (RID_FRIEND, specbits);
8125 friendp = 0;
8126 }
8127
8128 if (decl_context == NORMAL && friendp)
8129 error ("friend declaration not in class definition");
8130
8131 /* Pick up type qualifiers which should be applied to `this'. */
8132 quals = TREE_OPERAND (declarator, 2);
8133
8134 /* Traditionally, declaring return type float means double. */
8135
8136 if (flag_traditional
8137 && TYPE_MAIN_VARIANT (type) == float_type_node)
8138 {
8139 type = build_type_variant (double_type_node,
8140 TYPE_READONLY (type),
8141 TYPE_VOLATILE (type));
8142 }
8143
8144 /* Construct the function type and go to the next
8145 inner layer of declarator. */
8146
8147 {
8148 int funcdef_p;
8149 tree inner_parms = TREE_OPERAND (declarator, 1);
8150 tree inner_decl = TREE_OPERAND (declarator, 0);
8151
8152 declarator = TREE_OPERAND (declarator, 0);
8153
8154 if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
8155 inner_decl = TREE_OPERAND (inner_decl, 1);
8156
8157 /* Say it's a definition only for the CALL_EXPR
8158 closest to the identifier. */
8159 funcdef_p =
8160 (inner_decl && TREE_CODE (inner_decl) == IDENTIFIER_NODE)
8161 ? funcdef_flag : 0;
8162
8163 /* FIXME: This is where default args should be fully
8164 processed. */
8165
8166 arg_types = grokparms (inner_parms, funcdef_p);
8167 }
8168
8169 if (declarator)
8170 {
8171 /* Get past destructors, etc.
8172 We know we have one because FLAGS will be non-zero.
8173
8174 Complain about improper parameter lists here. */
8175 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
8176 {
8177 declarator = TREE_OPERAND (declarator, 0);
8178
8179 if (strict_prototype == 0 && arg_types == NULL_TREE)
8180 arg_types = void_list_node;
8181 else if (arg_types == NULL_TREE
8182 || arg_types != void_list_node)
8183 {
8184 error ("destructors cannot be specified with parameters");
8185 arg_types = void_list_node;
8186 }
8187 }
8188 }
8189
8190 /* ANSI seems to say that `const int foo ();'
8191 does not make the function foo const. */
8192 type = build_function_type (type,
8193 flag_traditional ? 0 : arg_types);
8194 }
8195 break;
8196
8197 case ADDR_EXPR:
8198 case INDIRECT_REF:
8199 /* Filter out pointers-to-references and references-to-references.
8200 We can get these if a TYPE_DECL is used. */
8201
8202 if (TREE_CODE (type) == REFERENCE_TYPE)
8203 {
8204 error ("cannot declare %s to references",
8205 TREE_CODE (declarator) == ADDR_EXPR
8206 ? "references" : "pointers");
8207 declarator = TREE_OPERAND (declarator, 0);
8208 continue;
8209 }
8210
8211 /* Merge any constancy or volatility into the target type
8212 for the pointer. */
8213
8214 if (constp || volatilep)
8215 {
8216 /* A const or volatile signature pointer/reference is
8217 pointing to a const or volatile object, i.e., the
8218 `optr' is const or volatile, respectively, not the
8219 signature pointer/reference itself. */
8220 if (! IS_SIGNATURE (type))
8221 {
8222 type = c_build_type_variant (type, constp, volatilep);
8223 if (IS_AGGR_TYPE (type))
8224 build_pointer_type (type);
8225 constp = 0;
8226 volatilep = 0;
8227 }
8228 }
8229
8230 if (IS_SIGNATURE (type))
8231 {
8232 if (TREE_CODE (declarator) == ADDR_EXPR)
8233 {
8234 if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE
8235 && TYPE_SIZE (type))
8236 cp_warning ("empty signature `%T' used in signature reference declaration",
8237 type);
8238 #if 0
8239 type = build_signature_reference_type (type,
8240 constp, volatilep);
8241 #else
8242 sorry ("signature reference");
8243 return NULL_TREE;
8244 #endif
8245 }
8246 else
8247 {
8248 if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE
8249 && TYPE_SIZE (type))
8250 cp_warning ("empty signature `%T' used in signature pointer declaration",
8251 type);
8252 type = build_signature_pointer_type (type,
8253 constp, volatilep);
8254 }
8255 constp = 0;
8256 volatilep = 0;
8257 }
8258 else if (TREE_CODE (declarator) == ADDR_EXPR)
8259 {
8260 if (TREE_CODE (type) == FUNCTION_TYPE)
8261 {
8262 error ("cannot declare references to functions; use pointer to function instead");
8263 type = build_pointer_type (type);
8264 }
8265 else
8266 {
8267 if (TYPE_MAIN_VARIANT (type) == void_type_node)
8268 error ("invalid type: `void &'");
8269 else
8270 type = build_reference_type (type);
8271 }
8272 }
8273 else if (TREE_CODE (type) == METHOD_TYPE)
8274 {
8275 type = build_ptrmemfunc_type (build_pointer_type (type));
8276 }
8277 else
8278 type = build_pointer_type (type);
8279
8280 /* Process a list of type modifier keywords (such as
8281 const or volatile) that were given inside the `*' or `&'. */
8282
8283 if (TREE_TYPE (declarator))
8284 {
8285 register tree typemodlist;
8286 int erred = 0;
8287 for (typemodlist = TREE_TYPE (declarator); typemodlist;
8288 typemodlist = TREE_CHAIN (typemodlist))
8289 {
8290 if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_CONST])
8291 constp++;
8292 else if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_VOLATILE])
8293 volatilep++;
8294 else if (!erred)
8295 {
8296 erred = 1;
8297 error ("invalid type modifier within %s declarator",
8298 TREE_CODE (declarator) == ADDR_EXPR
8299 ? "reference" : "pointer");
8300 }
8301 }
8302 if (constp > 1)
8303 pedwarn ("duplicate `const'");
8304 if (volatilep > 1)
8305 pedwarn ("duplicate `volatile'");
8306 if (TREE_CODE (declarator) == ADDR_EXPR
8307 && (constp || volatilep))
8308 {
8309 if (constp)
8310 warning ("discarding `const' applied to a reference");
8311 if (volatilep)
8312 warning ("discarding `volatile' applied to a reference");
8313 constp = volatilep = 0;
8314 }
8315 }
8316 declarator = TREE_OPERAND (declarator, 0);
8317 ctype = NULL_TREE;
8318 break;
8319
8320 case SCOPE_REF:
8321 {
8322 /* We have converted type names to NULL_TREE if the
8323 name was bogus, or to a _TYPE node, if not.
8324
8325 The variable CTYPE holds the type we will ultimately
8326 resolve to. The code here just needs to build
8327 up appropriate member types. */
8328 tree sname = TREE_OPERAND (declarator, 1);
8329 /* Destructors can have their visibilities changed as well. */
8330 if (TREE_CODE (sname) == BIT_NOT_EXPR)
8331 sname = TREE_OPERAND (sname, 0);
8332
8333 if (TREE_COMPLEXITY (declarator) == 0)
8334 /* This needs to be here, in case we are called
8335 multiple times. */ ;
8336 else if (friendp && (TREE_COMPLEXITY (declarator) < 2))
8337 /* don't fall out into global scope. Hides real bug? --eichin */ ;
8338 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
8339 {
8340 /* This pop_nested_class corresponds to the
8341 push_nested_class used to push into class scope for
8342 parsing the argument list of a function decl, in
8343 qualified_id. */
8344 pop_nested_class (1);
8345 TREE_COMPLEXITY (declarator) = current_class_depth;
8346 }
8347 else
8348 my_friendly_abort (16);
8349
8350 if (TREE_OPERAND (declarator, 0) == NULL_TREE)
8351 {
8352 /* We had a reference to a global decl, or
8353 perhaps we were given a non-aggregate typedef,
8354 in which case we cleared this out, and should just
8355 keep going as though it wasn't there. */
8356 declarator = sname;
8357 continue;
8358 }
8359 ctype = TREE_OPERAND (declarator, 0);
8360
8361 if (sname == NULL_TREE)
8362 goto done_scoping;
8363
8364 if (TREE_CODE (sname) == IDENTIFIER_NODE)
8365 {
8366 /* This is the `standard' use of the scoping operator:
8367 basetype :: member . */
8368
8369 if (TREE_CODE (type) == FUNCTION_TYPE)
8370 {
8371 if (current_class_type == NULL_TREE
8372 || TYPE_MAIN_VARIANT (ctype) == current_class_type
8373 || friendp)
8374 type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep),
8375 TREE_TYPE (type), TYPE_ARG_TYPES (type));
8376 else
8377 {
8378 cp_error ("cannot declare member function `%T::%s' within `%T'",
8379 ctype, name, current_class_type);
8380 return void_type_node;
8381 }
8382 }
8383 else if (TYPE_MAIN_VARIANT (ctype) == current_class_type)
8384 {
8385 if (extra_warnings)
8386 cp_warning ("redundant qualification `%T' on member `%s' ignored",
8387 ctype, name);
8388 type = build_offset_type (ctype, type);
8389 }
8390 else if (TYPE_SIZE (ctype) != NULL_TREE
8391 || (RIDBIT_SETP (RID_TYPEDEF, specbits)))
8392 {
8393 tree t;
8394 /* have to move this code elsewhere in this function.
8395 this code is used for i.e., typedef int A::M; M *pm; */
8396
8397 if (explicit_int == -1 && decl_context == FIELD
8398 && funcdef_flag == 0)
8399 {
8400 /* The code in here should only be used to build
8401 stuff that will be grokked as access decls. */
8402 t = lookup_field (ctype, sname, 0, 0);
8403 if (t)
8404 {
8405 t = build_lang_field_decl (FIELD_DECL, build_nt (SCOPE_REF, ctype, t), type);
8406 DECL_INITIAL (t) = init;
8407 return t;
8408 }
8409 /* No such field, try member functions. */
8410 t = lookup_fnfields (TYPE_BINFO (ctype), sname, 0);
8411 if (t)
8412 {
8413 if (flags == DTOR_FLAG)
8414 t = TREE_VALUE (t);
8415 else if (CLASSTYPE_METHOD_VEC (ctype)
8416 && TREE_VALUE (t) == TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), 0))
8417 {
8418 /* Don't include destructor with constructors. */
8419 t = DECL_CHAIN (TREE_VALUE (t));
8420 if (t == NULL_TREE)
8421 error ("class `%s' does not have any constructors", IDENTIFIER_POINTER (sname));
8422 t = build_tree_list (NULL_TREE, t);
8423 }
8424 t = build_lang_field_decl (FIELD_DECL, build_nt (SCOPE_REF, ctype, t), type);
8425 DECL_INITIAL (t) = init;
8426 return t;
8427 }
8428
8429 cp_error
8430 ("field `%D' is not a member of structure `%T'",
8431 sname, ctype);
8432 }
8433
8434 if (current_class_type)
8435 {
8436 if (TYPE_MAIN_VARIANT (ctype) != current_class_type)
8437 {
8438 cp_error ("cannot declare member `%T::%s' within `%T'",
8439 ctype, name, current_class_type);
8440 return void_type_node;
8441 }
8442 else if (extra_warnings)
8443 cp_warning ("extra qualification `%T' on member `%s' ignored",
8444 ctype, name);
8445 }
8446 type = build_offset_type (ctype, type);
8447 }
8448 else if (uses_template_parms (ctype))
8449 {
8450 enum tree_code c;
8451 if (TREE_CODE (type) == FUNCTION_TYPE)
8452 {
8453 type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep),
8454 TREE_TYPE (type),
8455 TYPE_ARG_TYPES (type));
8456 c = FUNCTION_DECL;
8457 }
8458 }
8459 else
8460 {
8461 cp_error ("structure `%T' not yet defined", ctype);
8462 return error_mark_node;
8463 }
8464
8465 declarator = sname;
8466 }
8467 else if (TREE_CODE (sname) == SCOPE_REF)
8468 my_friendly_abort (17);
8469 else
8470 {
8471 done_scoping:
8472 declarator = TREE_OPERAND (declarator, 1);
8473 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
8474 /* In this case, we will deal with it later. */
8475 ;
8476 else
8477 {
8478 if (TREE_CODE (type) == FUNCTION_TYPE)
8479 type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep), TREE_TYPE (type), TYPE_ARG_TYPES (type));
8480 else
8481 type = build_offset_type (ctype, type);
8482 }
8483 }
8484 }
8485 break;
8486
8487 case BIT_NOT_EXPR:
8488 declarator = TREE_OPERAND (declarator, 0);
8489 break;
8490
8491 case RECORD_TYPE:
8492 case UNION_TYPE:
8493 case ENUMERAL_TYPE:
8494 declarator = NULL_TREE;
8495 break;
8496
8497 case ERROR_MARK:
8498 declarator = NULL_TREE;
8499 break;
8500
8501 default:
8502 my_friendly_abort (158);
8503 }
8504 }
8505
8506 /* Now TYPE has the actual type. */
8507
8508 /* If this is declaring a typedef name, return a TYPE_DECL. */
8509
8510 if (RIDBIT_SETP (RID_TYPEDEF, specbits))
8511 {
8512 tree decl;
8513
8514 /* Note that the grammar rejects storage classes
8515 in typenames, fields or parameters. */
8516 if (constp || volatilep)
8517 type = c_build_type_variant (type, constp, volatilep);
8518
8519 /* If the user declares "struct {...} foo" then `foo' will have
8520 an anonymous name. Fill that name in now. Nothing can
8521 refer to it, so nothing needs know about the name change.
8522 The TYPE_NAME field was filled in by build_struct_xref. */
8523 if (type != error_mark_node
8524 && TYPE_NAME (type)
8525 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8526 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (type)))
8527 {
8528 /* replace the anonymous name with the real name everywhere. */
8529 lookup_tag_reverse (type, declarator);
8530 TYPE_IDENTIFIER (type) = declarator;
8531
8532 if (TYPE_LANG_SPECIFIC (type))
8533 TYPE_WAS_ANONYMOUS (type) = 1;
8534
8535 {
8536 tree d = TYPE_NAME (type), c = DECL_CONTEXT (d);
8537
8538 if (!c)
8539 set_nested_typename (d, 0, declarator, type);
8540 else if (TREE_CODE (c) == FUNCTION_DECL)
8541 set_nested_typename (d, DECL_ASSEMBLER_NAME (c),
8542 declarator, type);
8543 else
8544 set_nested_typename (d, TYPE_NESTED_NAME (c), declarator, type);
8545 }
8546 }
8547
8548 #if 0 /* not yet, should get fixed properly later */
8549 decl = make_type_decl (declarator, type);
8550 #else
8551 decl = build_decl (TYPE_DECL, declarator, type);
8552 #endif
8553 if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE)
8554 {
8555 cp_error_at ("typedef name may not be class-qualified", decl);
8556 return NULL_TREE;
8557 }
8558 else if (quals)
8559 {
8560 if (ctype == NULL_TREE)
8561 {
8562 if (TREE_CODE (type) != METHOD_TYPE)
8563 cp_error_at ("invalid type qualifier for non-method type", decl);
8564 else
8565 ctype = TYPE_METHOD_BASETYPE (type);
8566 }
8567 if (ctype != NULL_TREE)
8568 grok_method_quals (ctype, decl, quals);
8569 }
8570
8571 if (RIDBIT_SETP (RID_SIGNED, specbits)
8572 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
8573 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
8574
8575 if (RIDBIT_SETP (RID_MUTABLE, specbits))
8576 {
8577 error ("non-object member `%s' cannot be declared mutable", name);
8578 }
8579
8580 return decl;
8581 }
8582
8583 /* Detect the case of an array type of unspecified size
8584 which came, as such, direct from a typedef name.
8585 We must copy the type, so that each identifier gets
8586 a distinct type, so that each identifier's size can be
8587 controlled separately by its own initializer. */
8588
8589 if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE
8590 && TYPE_DOMAIN (type) == NULL_TREE)
8591 {
8592 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
8593 }
8594
8595 /* If this is a type name (such as, in a cast or sizeof),
8596 compute the type and return it now. */
8597
8598 if (decl_context == TYPENAME)
8599 {
8600 /* Note that the grammar rejects storage classes
8601 in typenames, fields or parameters. */
8602 if (constp || volatilep)
8603 if (IS_SIGNATURE (type))
8604 error ("`const' or `volatile' specified with signature type");
8605 else
8606 type = c_build_type_variant (type, constp, volatilep);
8607
8608 /* Special case: "friend class foo" looks like a TYPENAME context. */
8609 if (friendp)
8610 {
8611 /* A friendly class? */
8612 if (current_class_type)
8613 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
8614 else
8615 error("trying to make class `%s' a friend of global scope",
8616 TYPE_NAME_STRING (type));
8617 type = void_type_node;
8618 }
8619 else if (quals)
8620 {
8621 #if 0 /* not yet, should get fixed properly later */
8622 tree dummy = make_type_decl (declarator, type);
8623 #else
8624 tree dummy = build_decl (TYPE_DECL, declarator, type);
8625 #endif
8626 if (ctype == NULL_TREE)
8627 {
8628 my_friendly_assert (TREE_CODE (type) == METHOD_TYPE, 159);
8629 ctype = TYPE_METHOD_BASETYPE (type);
8630 }
8631 grok_method_quals (ctype, dummy, quals);
8632 type = TREE_TYPE (dummy);
8633 }
8634
8635 return type;
8636 }
8637 else if (declarator == NULL_TREE && decl_context != PARM
8638 && TREE_CODE (type) != UNION_TYPE
8639 && ! bitfield)
8640 {
8641 cp_error ("abstract declarator `%T' used as declaration", type);
8642 declarator = make_anon_name ();
8643 }
8644
8645 /* `void' at top level (not within pointer)
8646 is allowed only in typedefs or type names.
8647 We don't complain about parms either, but that is because
8648 a better error message can be made later. */
8649
8650 if (TYPE_MAIN_VARIANT (type) == void_type_node && decl_context != PARM)
8651 {
8652 if (TREE_CODE (declarator) == IDENTIFIER_NODE)
8653 {
8654 if (IDENTIFIER_OPNAME_P (declarator))
8655 #if 0 /* How could this happen? */
8656 error ("operator `%s' declared void",
8657 operator_name_string (declarator));
8658 #else
8659 my_friendly_abort (356);
8660 #endif
8661 else
8662 error ("variable or field `%s' declared void", name);
8663 }
8664 else
8665 error ("variable or field declared void");
8666 type = integer_type_node;
8667 }
8668
8669 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
8670 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
8671
8672 {
8673 register tree decl;
8674
8675 if (decl_context == PARM)
8676 {
8677 if (ctype)
8678 error ("cannot use `::' in parameter declaration");
8679
8680 /* A parameter declared as an array of T is really a pointer to T.
8681 One declared as a function is really a pointer to a function.
8682 One declared as a member is really a pointer to member. */
8683
8684 if (TREE_CODE (type) == ARRAY_TYPE)
8685 {
8686 /* Transfer const-ness of array into that of type pointed to. */
8687 type = build_pointer_type
8688 (c_build_type_variant (TREE_TYPE (type), constp, volatilep));
8689 volatilep = constp = 0;
8690 }
8691 else if (TREE_CODE (type) == FUNCTION_TYPE)
8692 type = build_pointer_type (type);
8693 else if (TREE_CODE (type) == OFFSET_TYPE)
8694 type = build_pointer_type (type);
8695
8696 decl = build_decl (PARM_DECL, declarator, type);
8697
8698 bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
8699 inlinep, friendp, raises != NULL_TREE);
8700 if (current_class_type
8701 && IS_SIGNATURE (current_class_type))
8702 {
8703 if (inlinep)
8704 error ("parameter of signature member function declared `inline'");
8705 if (RIDBIT_SETP (RID_AUTO, specbits))
8706 error ("parameter of signature member function declared `auto'");
8707 if (RIDBIT_SETP (RID_REGISTER, specbits))
8708 error ("parameter of signature member function declared `register'");
8709 }
8710
8711 /* Compute the type actually passed in the parmlist,
8712 for the case where there is no prototype.
8713 (For example, shorts and chars are passed as ints.)
8714 When there is a prototype, this is overridden later. */
8715
8716 DECL_ARG_TYPE (decl) = type_promotes_to (type);
8717 }
8718 else if (decl_context == FIELD)
8719 {
8720 if (type == error_mark_node)
8721 {
8722 /* Happens when declaring arrays of sizes which
8723 are error_mark_node, for example. */
8724 decl = NULL_TREE;
8725 }
8726 else if (TREE_CODE (type) == FUNCTION_TYPE)
8727 {
8728 int publicp = 0;
8729
8730 if (friendp == 0)
8731 {
8732 if (ctype == NULL_TREE)
8733 ctype = current_class_type;
8734
8735 if (ctype == NULL_TREE)
8736 {
8737 cp_error ("can't make `%D' into a method -- not in a class",
8738 declarator);
8739 return void_type_node;
8740 }
8741
8742 /* ``A union may [ ... ] not [ have ] virtual functions.''
8743 ARM 9.5 */
8744 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
8745 {
8746 cp_error ("function `%D' declared virtual inside a union",
8747 declarator);
8748 return void_type_node;
8749 }
8750
8751 if (declarator == ansi_opname[(int) NEW_EXPR]
8752 || declarator == ansi_opname[(int) VEC_NEW_EXPR]
8753 || declarator == ansi_opname[(int) DELETE_EXPR]
8754 || declarator == ansi_opname[(int) VEC_DELETE_EXPR])
8755 {
8756 if (virtualp)
8757 {
8758 cp_error ("`%D' cannot be declared virtual, since it is always static",
8759 declarator);
8760 virtualp = 0;
8761 }
8762 }
8763 else if (staticp < 2)
8764 type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep),
8765 TREE_TYPE (type), TYPE_ARG_TYPES (type));
8766 }
8767
8768 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
8769 publicp = (RIDBIT_SETP (RID_EXTERN, specbits)
8770 || (ctype != NULL_TREE
8771 && funcdef_flag >= 0
8772 && RIDBIT_NOTSETP (RID_INLINE, specbits))
8773 || (friendp
8774 && ! funcdef_flag
8775 && RIDBIT_NOTSETP (RID_STATIC, specbits)
8776 && RIDBIT_NOTSETP (RID_INLINE, specbits)));
8777 decl = grokfndecl (ctype, type, declarator,
8778 virtualp, flags, quals,
8779 raises, friendp ? -1 : 0, publicp);
8780 if (decl == NULL_TREE)
8781 return NULL_TREE;
8782
8783 DECL_INLINE (decl) = inlinep;
8784 }
8785 else if (TREE_CODE (type) == METHOD_TYPE)
8786 {
8787 /* All method decls are public, so tell grokfndecl to set
8788 TREE_PUBLIC, also. */
8789 decl = grokfndecl (ctype, type, declarator,
8790 virtualp, flags, quals,
8791 raises, friendp ? -1 : 0, 1);
8792 if (decl == NULL_TREE)
8793 return NULL_TREE;
8794
8795 DECL_INLINE (decl) = inlinep;
8796 }
8797 else if (TREE_CODE (type) == RECORD_TYPE
8798 && CLASSTYPE_DECLARED_EXCEPTION (type))
8799 {
8800 /* Handle a class-local exception declaration. */
8801 decl = build_lang_field_decl (VAR_DECL, declarator, type);
8802 if (ctype == NULL_TREE)
8803 ctype = current_class_type;
8804 return void_type_node;
8805 }
8806 else if (TYPE_SIZE (type) == NULL_TREE && !staticp
8807 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
8808 {
8809 error ("field `%s' has incomplete type",
8810 IDENTIFIER_POINTER (declarator));
8811
8812 /* If we're instantiating a template, tell them which
8813 instantiation made the field's type be incomplete. */
8814 if (current_class_type
8815 && TYPE_NAME (current_class_type)
8816 && IDENTIFIER_TEMPLATE (DECL_NAME (TYPE_NAME (current_class_type)))
8817 && declspecs && TREE_VALUE (declspecs)
8818 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
8819 error (" in instantiation of template `%s'",
8820 TYPE_NAME_STRING (current_class_type));
8821
8822 type = error_mark_node;
8823 decl = NULL_TREE;
8824 }
8825 else
8826 {
8827 if (friendp)
8828 {
8829 error ("`%s' is neither function nor method; cannot be declared friend",
8830 IDENTIFIER_POINTER (declarator));
8831 friendp = 0;
8832 }
8833 decl = NULL_TREE;
8834 }
8835
8836 if (friendp)
8837 {
8838 /* Friends are treated specially. */
8839 if (ctype == current_class_type)
8840 warning ("member functions are implicitly friends of their class");
8841 else
8842 {
8843 tree t = NULL_TREE;
8844 if (decl && DECL_NAME (decl))
8845 t = do_friend (ctype, declarator, decl,
8846 last_function_parms, flags, quals);
8847 if (t && funcdef_flag)
8848 return t;
8849
8850 return void_type_node;
8851 }
8852 }
8853
8854 /* Structure field. It may not be a function, except for C++ */
8855
8856 if (decl == NULL_TREE)
8857 {
8858 if (initialized)
8859 {
8860 /* Motion 10 at San Diego: If a static const integral data
8861 member is initialized with an integral constant
8862 expression, the initializer may appear either in the
8863 declaration (within the class), or in the definition,
8864 but not both. If it appears in the class, the member is
8865 a member constant. The file-scope definition is always
8866 required. */
8867 if (staticp)
8868 {
8869 if (pedantic)
8870 {
8871 if (! constp)
8872 cp_pedwarn ("ANSI C++ forbids in-class initialization of non-const static member `%D'",
8873 declarator);
8874
8875 else if (! INTEGRAL_TYPE_P (type))
8876 cp_pedwarn ("ANSI C++ forbids member constant `%D' of non-integral type `%T'", declarator, type);
8877 }
8878 }
8879
8880 /* Note that initialization of const members is prohibited
8881 by the draft ANSI standard, though it appears to be in
8882 common practice. 12.6.2: The argument list is used to
8883 initialize the named nonstatic member.... This (or an
8884 initializer list) is the only way to initialize
8885 nonstatic const and reference members. */
8886 else if (flag_ansi || ! constp)
8887 cp_pedwarn ("ANSI C++ forbids initialization of %s `%D'",
8888 constp ? "const member" : "member", declarator);
8889 }
8890
8891 if (staticp || (constp && initialized))
8892 {
8893 /* C++ allows static class members.
8894 All other work for this is done by grokfield.
8895 This VAR_DECL is built by build_lang_field_decl.
8896 All other VAR_DECLs are built by build_decl. */
8897 decl = build_lang_field_decl (VAR_DECL, declarator, type);
8898 TREE_STATIC (decl) = 1;
8899 /* In class context, 'static' means public access. */
8900 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = !!staticp;
8901 }
8902 else
8903 {
8904 decl = build_lang_field_decl (FIELD_DECL, declarator, type);
8905 if (RIDBIT_SETP (RID_MUTABLE, specbits))
8906 {
8907 DECL_MUTABLE_P (decl) = 1;
8908 RIDBIT_RESET (RID_MUTABLE, specbits);
8909 }
8910 }
8911
8912 bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
8913 inlinep, friendp, raises != NULL_TREE);
8914 }
8915 }
8916 else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
8917 {
8918 tree original_name = declarator;
8919 int publicp = 0;
8920
8921 if (! declarator)
8922 return NULL_TREE;
8923
8924 if (RIDBIT_SETP (RID_AUTO, specbits))
8925 error ("storage class `auto' invalid for function `%s'", name);
8926 else if (RIDBIT_SETP (RID_REGISTER, specbits))
8927 error ("storage class `register' invalid for function `%s'", name);
8928
8929 /* Function declaration not at top level.
8930 Storage classes other than `extern' are not allowed
8931 and `extern' makes no difference. */
8932 if (current_binding_level != global_binding_level
8933 && ! processing_template_decl
8934 && (RIDBIT_SETP (RID_STATIC, specbits)
8935 || RIDBIT_SETP (RID_INLINE, specbits))
8936 && pedantic)
8937 {
8938 if (RIDBIT_SETP (RID_STATIC, specbits))
8939 pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
8940 else
8941 pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
8942 }
8943
8944 if (ctype == NULL_TREE)
8945 {
8946 if (virtualp)
8947 {
8948 error ("virtual non-class function `%s'", name);
8949 virtualp = 0;
8950 }
8951
8952 if (current_lang_name == lang_name_cplusplus
8953 && ! (IDENTIFIER_LENGTH (original_name) == 4
8954 && IDENTIFIER_POINTER (original_name)[0] == 'm'
8955 && strcmp (IDENTIFIER_POINTER (original_name), "main") == 0)
8956 && ! (IDENTIFIER_LENGTH (original_name) > 10
8957 && IDENTIFIER_POINTER (original_name)[0] == '_'
8958 && IDENTIFIER_POINTER (original_name)[1] == '_'
8959 && strncmp (IDENTIFIER_POINTER (original_name)+2, "builtin_", 8) == 0))
8960 /* Plain overloading: will not be grok'd by grokclassfn. */
8961 declarator = build_decl_overload (dname, TYPE_ARG_TYPES (type), 0);
8962 }
8963 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
8964 type = build_cplus_method_type (build_type_variant (ctype, constp, volatilep),
8965 TREE_TYPE (type), TYPE_ARG_TYPES (type));
8966
8967 /* Record presence of `static'. In C++, `inline' is like `static'.
8968 Methods of classes should be public, unless we're dropping them
8969 into some other file, so we don't clear TREE_PUBLIC for them. */
8970 publicp
8971 = ((ctype
8972 && CLASSTYPE_INTERFACE_KNOWN (ctype))
8973 || !(RIDBIT_SETP (RID_STATIC, specbits)
8974 || RIDBIT_SETP (RID_INLINE, specbits)));
8975
8976 decl = grokfndecl (ctype, type, original_name,
8977 virtualp, flags, quals,
8978 raises,
8979 processing_template_decl ? 0 : friendp ? 2 : 1,
8980 publicp);
8981 if (decl == NULL_TREE)
8982 return NULL_TREE;
8983
8984 if (ctype == NULL_TREE && DECL_LANGUAGE (decl) != lang_c)
8985 DECL_ASSEMBLER_NAME (decl) = declarator;
8986
8987 if (staticp == 1)
8988 {
8989 int illegal_static = 0;
8990
8991 /* Don't allow a static member function in a class, and forbid
8992 declaring main to be static. */
8993 if (TREE_CODE (type) == METHOD_TYPE)
8994 {
8995 cp_error_at ("cannot declare member function `%D' to have static linkage", decl);
8996 illegal_static = 1;
8997 }
8998 else if (! ctype
8999 && IDENTIFIER_LENGTH (original_name) == 4
9000 && IDENTIFIER_POINTER (original_name)[0] == 'm'
9001 && ! strcmp (IDENTIFIER_POINTER (original_name), "main"))
9002 {
9003 error ("cannot declare function `main' to have static linkage");
9004 illegal_static = 1;
9005 }
9006 else if (current_function_decl)
9007 {
9008 /* FIXME need arm citation */
9009 error ("cannot declare static function inside another function");
9010 illegal_static = 1;
9011 }
9012
9013 if (illegal_static)
9014 {
9015 staticp = 0;
9016 RIDBIT_RESET (RID_STATIC, specbits);
9017 }
9018 }
9019
9020 /* Record presence of `inline', if it is reasonable. */
9021 if (inlinep)
9022 {
9023 tree last = tree_last (TYPE_ARG_TYPES (type));
9024
9025 if (! ctype
9026 && ! strcmp (IDENTIFIER_POINTER (original_name), "main"))
9027 error ("cannot inline function `main'");
9028 else if (last && last != void_list_node)
9029 cp_warning ("cannot inline function `%D' which takes `...'", original_name);
9030 else
9031 /* Assume that otherwise the function can be inlined. */
9032 DECL_INLINE (decl) = 1;
9033
9034 if (RIDBIT_SETP (RID_EXTERN, specbits))
9035 {
9036 current_extern_inline = 1;
9037 if (flag_ansi)
9038 pedwarn ("ANSI C++ does not permit `extern inline'");
9039 }
9040 }
9041 }
9042 else
9043 {
9044 /* It's a variable. */
9045
9046 /* An uninitialized decl with `extern' is a reference. */
9047 decl = grokvardecl (type, declarator, specbits, initialized);
9048 bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
9049 inlinep, friendp, raises != NULL_TREE);
9050
9051 if (ctype)
9052 {
9053 DECL_CONTEXT (decl) = ctype;
9054 if (staticp == 1)
9055 {
9056 cp_error ("static member `%D' re-declared as static",
9057 decl);
9058 staticp = 0;
9059 RIDBIT_RESET (RID_STATIC, specbits);
9060 }
9061 if (RIDBIT_SETP (RID_EXTERN, specbits))
9062 {
9063 cp_error ("cannot explicitly declare member `%#D' to have extern linkage",
9064 decl);
9065 RIDBIT_RESET (RID_EXTERN, specbits);
9066 }
9067 }
9068 }
9069
9070 if (RIDBIT_SETP (RID_MUTABLE, specbits))
9071 {
9072 error ("`%s' cannot be declared mutable", name);
9073 }
9074
9075 /* Record `register' declaration for warnings on &
9076 and in case doing stupid register allocation. */
9077
9078 if (RIDBIT_SETP (RID_REGISTER, specbits))
9079 DECL_REGISTER (decl) = 1;
9080
9081 if (RIDBIT_SETP (RID_EXTERN, specbits))
9082 DECL_THIS_EXTERN (decl) = 1;
9083
9084 /* Record constancy and volatility. */
9085
9086 if (constp)
9087 TREE_READONLY (decl) = TREE_CODE (type) != REFERENCE_TYPE;
9088 if (volatilep)
9089 {
9090 TREE_SIDE_EFFECTS (decl) = 1;
9091 TREE_THIS_VOLATILE (decl) = 1;
9092 }
9093
9094 return decl;
9095 }
9096 }
9097 \f
9098 /* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
9099 An empty exprlist is a parmlist. An exprlist which
9100 contains only identifiers at the global level
9101 is a parmlist. Otherwise, it is an exprlist. */
9102 int
9103 parmlist_is_exprlist (exprs)
9104 tree exprs;
9105 {
9106 if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
9107 return 0;
9108
9109 if (current_binding_level == global_binding_level)
9110 {
9111 /* At the global level, if these are all identifiers,
9112 then it is a parmlist. */
9113 while (exprs)
9114 {
9115 if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
9116 return 1;
9117 exprs = TREE_CHAIN (exprs);
9118 }
9119 return 0;
9120 }
9121 return 1;
9122 }
9123
9124 /* Subroutine of `grokparms'. In a fcn definition, arg types must
9125 be complete.
9126
9127 C++: also subroutine of `start_function'. */
9128 static void
9129 require_complete_types_for_parms (parms)
9130 tree parms;
9131 {
9132 while (parms)
9133 {
9134 tree type = TREE_TYPE (parms);
9135 if (TYPE_SIZE (type) == NULL_TREE)
9136 {
9137 if (DECL_NAME (parms))
9138 error ("parameter `%s' has incomplete type",
9139 IDENTIFIER_POINTER (DECL_NAME (parms)));
9140 else
9141 error ("parameter has incomplete type");
9142 TREE_TYPE (parms) = error_mark_node;
9143 }
9144 #if 0
9145 /* If the arg types are incomplete in a declaration,
9146 they must include undefined tags.
9147 These tags can never be defined in the scope of the declaration,
9148 so the types can never be completed,
9149 and no call can be compiled successfully. */
9150 /* This is not the right behavior for C++, but not having
9151 it is also probably wrong. */
9152 else
9153 {
9154 /* Now warn if is a pointer to an incomplete type. */
9155 while (TREE_CODE (type) == POINTER_TYPE
9156 || TREE_CODE (type) == REFERENCE_TYPE)
9157 type = TREE_TYPE (type);
9158 type = TYPE_MAIN_VARIANT (type);
9159 if (TYPE_SIZE (type) == NULL_TREE)
9160 {
9161 if (DECL_NAME (parm) != NULL_TREE)
9162 warning ("parameter `%s' points to incomplete type",
9163 IDENTIFIER_POINTER (DECL_NAME (parm)));
9164 else
9165 warning ("parameter points to incomplete type");
9166 }
9167 }
9168 #endif
9169 parms = TREE_CHAIN (parms);
9170 }
9171 }
9172
9173 /* Decode the list of parameter types for a function type.
9174 Given the list of things declared inside the parens,
9175 return a list of types.
9176
9177 The list we receive can have three kinds of elements:
9178 an IDENTIFIER_NODE for names given without types,
9179 a TREE_LIST node for arguments given as typespecs or names with typespecs,
9180 or void_type_node, to mark the end of an argument list
9181 when additional arguments are not permitted (... was not used).
9182
9183 FUNCDEF_FLAG is nonzero for a function definition, 0 for
9184 a mere declaration. A nonempty identifier-list gets an error message
9185 when FUNCDEF_FLAG is zero.
9186 If FUNCDEF_FLAG is 1, then parameter types must be complete.
9187 If FUNCDEF_FLAG is -1, then parameter types may be incomplete.
9188
9189 If all elements of the input list contain types,
9190 we return a list of the types.
9191 If all elements contain no type (except perhaps a void_type_node
9192 at the end), we return a null list.
9193 If some have types and some do not, it is an error, and we
9194 return a null list.
9195
9196 Also set last_function_parms to either
9197 a list of names (IDENTIFIER_NODEs) or a chain of PARM_DECLs.
9198 A list of names is converted to a chain of PARM_DECLs
9199 by store_parm_decls so that ultimately it is always a chain of decls.
9200
9201 Note that in C++, parameters can take default values. These default
9202 values are in the TREE_PURPOSE field of the TREE_LIST. It is
9203 an error to specify default values which are followed by parameters
9204 that have no default values, or an ELLIPSES. For simplicities sake,
9205 only parameters which are specified with their types can take on
9206 default values. */
9207
9208 static tree
9209 grokparms (first_parm, funcdef_flag)
9210 tree first_parm;
9211 int funcdef_flag;
9212 {
9213 tree result = NULL_TREE;
9214 tree decls = NULL_TREE;
9215
9216 if (first_parm != NULL_TREE
9217 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
9218 {
9219 if (! funcdef_flag)
9220 pedwarn ("parameter names (without types) in function declaration");
9221 last_function_parms = first_parm;
9222 return NULL_TREE;
9223 }
9224 else if (first_parm != NULL_TREE
9225 && TREE_CODE (TREE_VALUE (first_parm)) != TREE_LIST
9226 && TREE_VALUE (first_parm) != void_type_node)
9227 my_friendly_abort (145);
9228 else
9229 {
9230 /* Types were specified. This is a list of declarators
9231 each represented as a TREE_LIST node. */
9232 register tree parm, chain;
9233 int any_init = 0, any_error = 0, saw_void = 0;
9234
9235 if (first_parm != NULL_TREE)
9236 {
9237 tree last_result = NULL_TREE;
9238 tree last_decl = NULL_TREE;
9239
9240 for (parm = first_parm; parm != NULL_TREE; parm = chain)
9241 {
9242 tree type, list_node = parm;
9243 register tree decl = TREE_VALUE (parm);
9244 tree init = TREE_PURPOSE (parm);
9245
9246 chain = TREE_CHAIN (parm);
9247 /* @@ weak defense against parse errors. */
9248 if (decl != void_type_node && TREE_CODE (decl) != TREE_LIST)
9249 {
9250 /* Give various messages as the need arises. */
9251 if (TREE_CODE (decl) == STRING_CST)
9252 error ("invalid string constant `%s'",
9253 TREE_STRING_POINTER (decl));
9254 else if (TREE_CODE (decl) == INTEGER_CST)
9255 error ("invalid integer constant in parameter list, did you forget to give parameter name?");
9256 continue;
9257 }
9258
9259 if (decl != void_type_node)
9260 {
9261 /* @@ May need to fetch out a `raises' here. */
9262 decl = grokdeclarator (TREE_VALUE (decl),
9263 TREE_PURPOSE (decl),
9264 PARM, init != NULL_TREE, NULL_TREE);
9265 if (! decl)
9266 continue;
9267 type = TREE_TYPE (decl);
9268 if (TYPE_MAIN_VARIANT (type) == void_type_node)
9269 decl = void_type_node;
9270 else if (TREE_CODE (type) == METHOD_TYPE)
9271 {
9272 if (DECL_NAME (decl))
9273 /* Cannot use `error_with_decl' here because
9274 we don't have DECL_CONTEXT set up yet. */
9275 error ("parameter `%s' invalidly declared method type",
9276 IDENTIFIER_POINTER (DECL_NAME (decl)));
9277 else
9278 error ("parameter invalidly declared method type");
9279 type = build_pointer_type (type);
9280 TREE_TYPE (decl) = type;
9281 }
9282 else if (TREE_CODE (type) == OFFSET_TYPE)
9283 {
9284 if (DECL_NAME (decl))
9285 error ("parameter `%s' invalidly declared offset type",
9286 IDENTIFIER_POINTER (DECL_NAME (decl)));
9287 else
9288 error ("parameter invalidly declared offset type");
9289 type = build_pointer_type (type);
9290 TREE_TYPE (decl) = type;
9291 }
9292 else if (TREE_CODE (type) == RECORD_TYPE
9293 && TYPE_LANG_SPECIFIC (type)
9294 && CLASSTYPE_ABSTRACT_VIRTUALS (type))
9295 {
9296 abstract_virtuals_error (decl, type);
9297 any_error = 1; /* seems like a good idea */
9298 }
9299 else if (TREE_CODE (type) == RECORD_TYPE
9300 && TYPE_LANG_SPECIFIC (type)
9301 && IS_SIGNATURE (type))
9302 {
9303 signature_error (decl, type);
9304 any_error = 1; /* seems like a good idea */
9305 }
9306 }
9307
9308 if (decl == void_type_node)
9309 {
9310 if (result == NULL_TREE)
9311 {
9312 result = void_list_node;
9313 last_result = result;
9314 }
9315 else
9316 {
9317 TREE_CHAIN (last_result) = void_list_node;
9318 last_result = void_list_node;
9319 }
9320 saw_void = 1;
9321 if (chain
9322 && (chain != void_list_node || TREE_CHAIN (chain)))
9323 error ("`void' in parameter list must be entire list");
9324 break;
9325 }
9326
9327 /* Since there is a prototype, args are passed in their own types. */
9328 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9329 #ifdef PROMOTE_PROTOTYPES
9330 if ((TREE_CODE (type) == INTEGER_TYPE
9331 || TREE_CODE (type) == ENUMERAL_TYPE)
9332 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9333 DECL_ARG_TYPE (decl) = integer_type_node;
9334 #endif
9335 if (!any_error)
9336 {
9337 if (init)
9338 {
9339 any_init++;
9340 if (TREE_CODE (init) == SAVE_EXPR)
9341 PARM_DECL_EXPR (init) = 1;
9342 else if (TREE_CODE (init) == VAR_DECL)
9343 {
9344 if (IDENTIFIER_LOCAL_VALUE (DECL_NAME (init)))
9345 {
9346 /* ``Local variables may not be used in default
9347 argument expressions.'' dpANSI C++ 8.2.6 */
9348 /* If extern int i; within a function is not
9349 considered a local variable, then this code is
9350 wrong. */
9351 cp_error ("local variable `%D' may not be used as a default argument", init);
9352 any_error = 1;
9353 }
9354 else if (TREE_READONLY_DECL_P (init))
9355 init = decl_constant_value (init);
9356 }
9357 else
9358 init = require_instantiated_type (type, init, integer_zero_node);
9359 }
9360 else if (any_init)
9361 {
9362 error ("all trailing parameters must have default arguments");
9363 any_error = 1;
9364 }
9365 }
9366 else
9367 init = NULL_TREE;
9368
9369 if (decls == NULL_TREE)
9370 {
9371 decls = decl;
9372 last_decl = decls;
9373 }
9374 else
9375 {
9376 TREE_CHAIN (last_decl) = decl;
9377 last_decl = decl;
9378 }
9379 if (TREE_PERMANENT (list_node))
9380 {
9381 TREE_PURPOSE (list_node) = init;
9382 TREE_VALUE (list_node) = type;
9383 TREE_CHAIN (list_node) = NULL_TREE;
9384 }
9385 else
9386 list_node = saveable_tree_cons (init, type, NULL_TREE);
9387 if (result == NULL_TREE)
9388 {
9389 result = list_node;
9390 last_result = result;
9391 }
9392 else
9393 {
9394 TREE_CHAIN (last_result) = list_node;
9395 last_result = list_node;
9396 }
9397 }
9398 if (last_result)
9399 TREE_CHAIN (last_result) = NULL_TREE;
9400 /* If there are no parameters, and the function does not end
9401 with `...', then last_decl will be NULL_TREE. */
9402 if (last_decl != NULL_TREE)
9403 TREE_CHAIN (last_decl) = NULL_TREE;
9404 }
9405 }
9406
9407 last_function_parms = decls;
9408
9409 /* In a fcn definition, arg types must be complete. */
9410 if (funcdef_flag > 0)
9411 require_complete_types_for_parms (last_function_parms);
9412
9413 return result;
9414 }
9415 \f
9416 /* These memoizing functions keep track of special properties which
9417 a class may have. `grok_ctor_properties' notices whether a class
9418 has a constructor of the form X(X&), and also complains
9419 if the class has a constructor of the form X(X).
9420 `grok_op_properties' takes notice of the various forms of
9421 operator= which are defined, as well as what sorts of type conversion
9422 may apply. Both functions take a FUNCTION_DECL as an argument. */
9423 int
9424 grok_ctor_properties (ctype, decl)
9425 tree ctype, decl;
9426 {
9427 tree parmtypes = FUNCTION_ARG_CHAIN (decl);
9428 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
9429
9430 /* When a type has virtual baseclasses, a magical first int argument is
9431 added to any ctor so we can tell if the class has been initialized
9432 yet. This could screw things up in this function, so we deliberately
9433 ignore the leading int if we're in that situation. */
9434 if (parmtypes
9435 && TREE_VALUE (parmtypes) == integer_type_node
9436 && TYPE_USES_VIRTUAL_BASECLASSES (ctype))
9437 {
9438 parmtypes = TREE_CHAIN (parmtypes);
9439 parmtype = TREE_VALUE (parmtypes);
9440 }
9441
9442 if (TREE_CODE (parmtype) == REFERENCE_TYPE
9443 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == ctype)
9444 {
9445 if (TREE_CHAIN (parmtypes) == NULL_TREE
9446 || TREE_CHAIN (parmtypes) == void_list_node
9447 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
9448 {
9449 TYPE_HAS_INIT_REF (ctype) = 1;
9450 if (TYPE_READONLY (TREE_TYPE (parmtype)))
9451 TYPE_HAS_CONST_INIT_REF (ctype) = 1;
9452 }
9453 else
9454 TYPE_GETS_INIT_AGGR (ctype) = 1;
9455 }
9456 else if (TYPE_MAIN_VARIANT (parmtype) == ctype)
9457 {
9458 if (TREE_CHAIN (parmtypes) != NULL_TREE
9459 && TREE_CHAIN (parmtypes) == void_list_node)
9460 {
9461 cp_error ("invalid constructor; you probably meant `%T (%T&)'",
9462 ctype, ctype);
9463 SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
9464
9465 return 0;
9466 }
9467 else
9468 TYPE_GETS_INIT_AGGR (ctype) = 1;
9469 }
9470 else if (TREE_CODE (parmtype) == VOID_TYPE
9471 || TREE_PURPOSE (parmtypes) != NULL_TREE)
9472 TYPE_HAS_DEFAULT_CONSTRUCTOR (ctype) = 1;
9473
9474 return 1;
9475 }
9476
9477 /* An operator with this name can be either unary or binary. */
9478 static int
9479 ambi_op_p (name)
9480 tree name;
9481 {
9482 return (name == ansi_opname [(int) INDIRECT_REF]
9483 || name == ansi_opname [(int) ADDR_EXPR]
9484 || name == ansi_opname [(int) NEGATE_EXPR]
9485 || name == ansi_opname[(int) POSTINCREMENT_EXPR]
9486 || name == ansi_opname[(int) POSTDECREMENT_EXPR]
9487 || name == ansi_opname [(int) CONVERT_EXPR]);
9488 }
9489
9490 /* An operator with this name can only be unary. */
9491 static int
9492 unary_op_p (name)
9493 tree name;
9494 {
9495 return (name == ansi_opname [(int) TRUTH_NOT_EXPR]
9496 || name == ansi_opname [(int) BIT_NOT_EXPR]
9497 || name == ansi_opname [(int) COMPONENT_REF]
9498 || OPERATOR_TYPENAME_P (name));
9499 }
9500
9501 /* Do a little sanity-checking on how they declared their operator. */
9502 static void
9503 grok_op_properties (decl, virtualp, friendp)
9504 tree decl;
9505 int virtualp, friendp;
9506 {
9507 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9508 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
9509 tree name = DECL_NAME (decl);
9510
9511 if (current_class_type == NULL_TREE)
9512 friendp = 1;
9513
9514 if (! friendp)
9515 {
9516 if (name == ansi_opname[(int) MODIFY_EXPR])
9517 TYPE_HAS_ASSIGNMENT (current_class_type) = 1;
9518 else if (name == ansi_opname[(int) CALL_EXPR])
9519 TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
9520 else if (name == ansi_opname[(int) ARRAY_REF])
9521 TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
9522 else if (name == ansi_opname[(int) COMPONENT_REF]
9523 || name == ansi_opname[(int) MEMBER_REF])
9524 TYPE_OVERLOADS_ARROW (current_class_type) = 1;
9525 else if (name == ansi_opname[(int) NEW_EXPR])
9526 TYPE_GETS_NEW (current_class_type) |= 1;
9527 else if (name == ansi_opname[(int) DELETE_EXPR])
9528 TYPE_GETS_DELETE (current_class_type) |= 1;
9529 else if (name == ansi_opname[(int) VEC_NEW_EXPR])
9530 TYPE_GETS_NEW (current_class_type) |= 2;
9531 else if (name == ansi_opname[(int) VEC_DELETE_EXPR])
9532 TYPE_GETS_DELETE (current_class_type) |= 2;
9533 }
9534
9535 if (name == ansi_opname[(int) NEW_EXPR]
9536 || name == ansi_opname[(int) VEC_NEW_EXPR])
9537 {
9538 /* When the compiler encounters the definition of A::operator new, it
9539 doesn't look at the class declaration to find out if it's static. */
9540 if (methodp)
9541 revert_static_member_fn (&decl, NULL, NULL);
9542
9543 /* Take care of function decl if we had syntax errors. */
9544 if (argtypes == NULL_TREE)
9545 TREE_TYPE (decl) =
9546 build_function_type (ptr_type_node,
9547 hash_tree_chain (integer_type_node,
9548 void_list_node));
9549 else
9550 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
9551 }
9552 else if (name == ansi_opname[(int) DELETE_EXPR]
9553 || name == ansi_opname[(int) VEC_DELETE_EXPR])
9554 {
9555 if (methodp)
9556 revert_static_member_fn (&decl, NULL, NULL);
9557
9558 if (argtypes == NULL_TREE)
9559 TREE_TYPE (decl) =
9560 build_function_type (void_type_node,
9561 hash_tree_chain (ptr_type_node,
9562 void_list_node));
9563 else
9564 {
9565 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
9566
9567 if (! friendp && name == ansi_opname[(int) VEC_DELETE_EXPR]
9568 && (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
9569 != void_list_node))
9570 TYPE_VEC_DELETE_TAKES_SIZE (current_class_type) = 1;
9571 }
9572 }
9573 else
9574 {
9575 /* An operator function must either be a non-static member function
9576 or have at least one parameter of a class, a reference to a class,
9577 an enumeration, or a reference to an enumeration. 13.4.0.6 */
9578 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
9579 {
9580 if (OPERATOR_TYPENAME_P (name)
9581 || name == ansi_opname[(int) CALL_EXPR]
9582 || name == ansi_opname[(int) MODIFY_EXPR]
9583 || name == ansi_opname[(int) COMPONENT_REF]
9584 || name == ansi_opname[(int) ARRAY_REF])
9585 cp_error ("`%D' must be a nonstatic member function", decl);
9586 else
9587 {
9588 tree p = argtypes;
9589
9590 if (DECL_STATIC_FUNCTION_P (decl))
9591 cp_error ("`%D' must be either a non-static member function or a non-member function", decl);
9592
9593 if (p)
9594 for (; TREE_VALUE (p) != void_type_node ; p = TREE_CHAIN (p))
9595 {
9596 tree arg = TREE_VALUE (p);
9597 if (TREE_CODE (arg) == REFERENCE_TYPE)
9598 arg = TREE_TYPE (arg);
9599
9600 /* This lets bad template code slip through. */
9601 if (IS_AGGR_TYPE (arg)
9602 || TREE_CODE (arg) == ENUMERAL_TYPE
9603 || TREE_CODE (arg) == TEMPLATE_TYPE_PARM)
9604 goto foundaggr;
9605 }
9606 cp_error
9607 ("`%D' must have an argument of class or enumerated type",
9608 decl);
9609 foundaggr:
9610 ;
9611 }
9612 }
9613
9614 if (name == ansi_opname[(int) CALL_EXPR]
9615 || name == ansi_opname[(int) METHOD_CALL_EXPR])
9616 return; /* no restrictions on args */
9617
9618 if (IDENTIFIER_TYPENAME_P (name))
9619 {
9620 tree t = TREE_TYPE (name);
9621 if (TREE_CODE (t) == VOID_TYPE)
9622 pedwarn ("void is not a valid type conversion operator");
9623 else if (! friendp)
9624 {
9625 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
9626 char *what = 0;
9627 if (ref)
9628 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
9629
9630 if (t == current_class_type)
9631 what = "the same type";
9632 else if (IS_AGGR_TYPE (t)
9633 && DERIVED_FROM_P (t, current_class_type))
9634 what = "a base class";
9635
9636 if (what)
9637 warning ("conversion to %s%s will never use a type conversion operator",
9638 ref ? "a reference to " : "", what);
9639 }
9640 }
9641
9642 if (name == ansi_opname[(int) MODIFY_EXPR])
9643 {
9644 tree parmtype;
9645
9646 if (list_length (argtypes) != 3 && methodp)
9647 {
9648 cp_error ("`%D' must take exactly one argument", decl);
9649 return;
9650 }
9651 parmtype = TREE_VALUE (TREE_CHAIN (argtypes));
9652
9653 if (copy_assignment_arg_p (parmtype, virtualp)
9654 && ! friendp)
9655 {
9656 TYPE_HAS_ASSIGN_REF (current_class_type) = 1;
9657 if (TREE_CODE (parmtype) != REFERENCE_TYPE
9658 || TYPE_READONLY (TREE_TYPE (parmtype)))
9659 TYPE_HAS_CONST_ASSIGN_REF (current_class_type) = 1;
9660 #if 0 /* Too soon; done in grok_function_init */
9661 if (DECL_ABSTRACT_VIRTUAL_P (decl))
9662 TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type) = 1;
9663 #endif
9664 }
9665 }
9666 else if (name == ansi_opname[(int) COND_EXPR])
9667 {
9668 /* 13.4.0.3 */
9669 pedwarn ("ANSI C++ prohibits overloading operator ?:");
9670 if (list_length (argtypes) != 4)
9671 cp_error ("`%D' must take exactly three arguments", decl);
9672 }
9673 else if (ambi_op_p (name))
9674 {
9675 if (list_length (argtypes) == 2)
9676 /* prefix */;
9677 else if (list_length (argtypes) == 3)
9678 {
9679 if ((name == ansi_opname[(int) POSTINCREMENT_EXPR]
9680 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
9681 && TREE_VALUE (TREE_CHAIN (argtypes)) != integer_type_node)
9682 {
9683 if (methodp)
9684 cp_error ("postfix `%D' must take `int' as its argument",
9685 decl);
9686 else
9687 cp_error
9688 ("postfix `%D' must take `int' as its second argument",
9689 decl);
9690 }
9691 }
9692 else
9693 {
9694 if (methodp)
9695 cp_error ("`%D' must take either zero or one argument", decl);
9696 else
9697 cp_error ("`%D' must take either one or two arguments", decl);
9698 }
9699 }
9700 else if (unary_op_p (name))
9701 {
9702 if (list_length (argtypes) != 2)
9703 {
9704 if (methodp)
9705 cp_error ("`%D' must take `void'", decl);
9706 else
9707 cp_error ("`%D' must take exactly one argument", decl);
9708 }
9709 }
9710 else /* if (binary_op_p (name)) */
9711 {
9712 if (list_length (argtypes) != 3)
9713 {
9714 if (methodp)
9715 cp_error ("`%D' must take exactly one argument", decl);
9716 else
9717 cp_error ("`%D' must take exactly two arguments", decl);
9718 }
9719 }
9720
9721 /* 13.4.0.8 */
9722 if (argtypes)
9723 for (; argtypes != void_list_node ; argtypes = TREE_CHAIN (argtypes))
9724 if (TREE_PURPOSE (argtypes))
9725 {
9726 TREE_PURPOSE (argtypes) = NULL_TREE;
9727 if (name == ansi_opname[(int) POSTINCREMENT_EXPR]
9728 || name == ansi_opname[(int) POSTDECREMENT_EXPR])
9729 {
9730 if (pedantic)
9731 cp_pedwarn ("`%D' cannot have default arguments", decl);
9732 }
9733 else
9734 cp_error ("`%D' cannot have default arguments", decl);
9735 }
9736 }
9737 }
9738 \f
9739 /* Get the struct, enum or union (CODE says which) with tag NAME.
9740 Define the tag as a forward-reference if it is not defined.
9741
9742 C++: If a class derivation is given, process it here, and report
9743 an error if multiple derivation declarations are not identical.
9744
9745 If this is a definition, come in through xref_tag and only look in
9746 the current frame for the name (since C++ allows new names in any
9747 scope.) */
9748
9749 /* avoid rewriting all callers of xref_tag */
9750 static int xref_next_defn = 0;
9751
9752 tree
9753 xref_defn_tag (code_type_node, name, binfo)
9754 tree code_type_node;
9755 tree name, binfo;
9756 {
9757 tree rv, ncp;
9758 xref_next_defn = 1;
9759
9760 if (class_binding_level)
9761 {
9762 tree n1;
9763 char *buf;
9764 /* we need to build a new IDENTIFIER_NODE for name which nukes
9765 * the pieces... */
9766 /*
9767 n1 = IDENTIFIER_LOCAL_VALUE (current_class_name);
9768 if (n1)
9769 n1 = DECL_NAME (n1);
9770 else
9771 n1 = current_class_name;
9772 */
9773 n1 = TYPE_NAME (current_class_type);
9774 if (n1)
9775 n1 = DECL_NESTED_TYPENAME(n1);
9776 else
9777 n1 = current_class_name;
9778
9779 buf = (char *) alloca (4 + IDENTIFIER_LENGTH (n1)
9780 + IDENTIFIER_LENGTH (name));
9781
9782 sprintf (buf, "%s::%s", IDENTIFIER_POINTER (n1),
9783 IDENTIFIER_POINTER (name));
9784 ncp = get_identifier (buf);
9785 #ifdef SPEW_DEBUG
9786 if (spew_debug)
9787 printf("*** %s ***\n", IDENTIFIER_POINTER (ncp));
9788 #endif
9789 #if 0
9790 IDENTIFIER_LOCAL_VALUE (name) =
9791 build_decl (TYPE_DECL, ncp, NULL_TREE);
9792 #endif
9793 rv = xref_tag (code_type_node, name, binfo, 0);
9794 if (! ANON_AGGRNAME_P (name))
9795 {
9796 register tree type_decl = build_decl (TYPE_DECL, ncp, rv);
9797 #ifdef DWARF_DEBUGGING_INFO
9798 /* Mark the TYPE_DECL node created just above as a gratuitous one
9799 so that dwarfout.c will know not to generate a TAG_typedef DIE
9800 for it. */
9801 if (write_symbols == DWARF_DEBUG)
9802 DECL_IGNORED_P (type_decl) = 1;
9803 #endif /* DWARF_DEBUGGING_INFO */
9804 pushdecl_nonclass_level (type_decl);
9805 }
9806 }
9807 else
9808 {
9809 rv = xref_tag (code_type_node, name, binfo, 0);
9810 }
9811 xref_next_defn = 0;
9812 return rv;
9813 }
9814
9815 tree
9816 xref_tag (code_type_node, name, binfo, globalize)
9817 tree code_type_node;
9818 tree name, binfo;
9819 int globalize;
9820 {
9821 enum tag_types tag_code;
9822 enum tree_code code;
9823 int temp = 0;
9824 int i, len;
9825 register tree ref, t;
9826 struct binding_level *b = inner_binding_level;
9827
9828 tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
9829 switch (tag_code)
9830 {
9831 case record_type:
9832 case class_type:
9833 case exception_type:
9834 case signature_type:
9835 code = RECORD_TYPE;
9836 len = list_length (binfo);
9837 break;
9838 case union_type:
9839 code = UNION_TYPE;
9840 if (binfo)
9841 {
9842 cp_error ("derived union `%T' invalid", name);
9843 binfo = NULL_TREE;
9844 }
9845 len = 0;
9846 break;
9847 case enum_type:
9848 code = ENUMERAL_TYPE;
9849 break;
9850 default:
9851 my_friendly_abort (18);
9852 }
9853
9854 /* If a cross reference is requested, look up the type
9855 already defined for this tag and return it. */
9856 t = IDENTIFIER_TYPE_VALUE (name);
9857 if (t && TREE_CODE (t) != code)
9858 t = NULL_TREE;
9859
9860 if (xref_next_defn)
9861 {
9862 /* If we know we are defining this tag, only look it up in this scope
9863 * and don't try to find it as a type. */
9864 xref_next_defn = 0;
9865 if (t && TYPE_CONTEXT(t))
9866 {
9867 if (TREE_MANGLED (name))
9868 ref = t;
9869 else
9870 ref = lookup_tag (code, name, b, 1);
9871 }
9872 else
9873 ref = lookup_tag (code, name, b, 1);
9874 }
9875 else
9876 {
9877 if (t)
9878 ref = t;
9879 else
9880 ref = lookup_tag (code, name, b, 0);
9881
9882 if (! ref)
9883 {
9884 /* Try finding it as a type declaration. If that wins, use it. */
9885 ref = lookup_name (name, 1);
9886 if (ref && TREE_CODE (ref) == TYPE_DECL
9887 && TREE_CODE (TREE_TYPE (ref)) == code)
9888 ref = TREE_TYPE (ref);
9889 else
9890 ref = NULL_TREE;
9891 }
9892 }
9893
9894 push_obstacks_nochange ();
9895
9896 if (! ref)
9897 {
9898 /* If no such tag is yet defined, create a forward-reference node
9899 and record it as the "definition".
9900 When a real declaration of this type is found,
9901 the forward-reference will be altered into a real type. */
9902
9903 /* In C++, since these migrate into the global scope, we must
9904 build them on the permanent obstack. */
9905
9906 temp = allocation_temporary_p ();
9907 if (temp)
9908 end_temporary_allocation ();
9909
9910 if (code == ENUMERAL_TYPE)
9911 {
9912 ref = make_node (ENUMERAL_TYPE);
9913
9914 /* Give the type a default layout like unsigned int
9915 to avoid crashing if it does not get defined. */
9916 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
9917 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
9918 TREE_UNSIGNED (ref) = 1;
9919 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
9920 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
9921 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
9922
9923 /* Enable us to recognize when a type is created in class context.
9924 To do nested classes correctly, this should probably be cleared
9925 out when we leave this classes scope. Currently this in only
9926 done in `start_enum'. */
9927
9928 pushtag (name, ref, globalize);
9929 if (flag_cadillac)
9930 cadillac_start_enum (ref);
9931 }
9932 else if (tag_code == exception_type)
9933 {
9934 ref = make_lang_type (code);
9935 /* Enable us to recognize when an exception type is created in
9936 class context. To do nested classes correctly, this should
9937 probably be cleared out when we leave this class's scope. */
9938 CLASSTYPE_DECLARED_EXCEPTION (ref) = 1;
9939 pushtag (name, ref, globalize);
9940 if (flag_cadillac)
9941 cadillac_start_struct (ref);
9942 }
9943 else
9944 {
9945 extern tree pending_vtables;
9946 struct binding_level *old_b = class_binding_level;
9947 int needs_writing;
9948
9949 ref = make_lang_type (code);
9950
9951 /* A signature type will contain the fields of the signature
9952 table. Therefore, it's not only an interface. */
9953 if (tag_code == signature_type)
9954 {
9955 SET_SIGNATURE (ref);
9956 CLASSTYPE_INTERFACE_ONLY (ref) = 0;
9957 CLASSTYPE_INTERFACE_UNKNOWN (ref) = 0;
9958 }
9959
9960 /* Record how to set the access of this class's
9961 virtual functions. If write_virtuals == 2 or 3, then
9962 inline virtuals are ``extern inline''. */
9963 switch (write_virtuals)
9964 {
9965 case 0:
9966 case 1:
9967 needs_writing = 1;
9968 break;
9969 case 2:
9970 needs_writing = !! value_member (name, pending_vtables);
9971 break;
9972 case 3:
9973 needs_writing = ! CLASSTYPE_INTERFACE_ONLY (ref)
9974 && CLASSTYPE_INTERFACE_KNOWN (ref);
9975 break;
9976 default:
9977 needs_writing = 0;
9978 }
9979
9980 /* Signatures don't have a vtable. As long as we don't have default
9981 implementations, they behave as if `write_virtuals' were 3. */
9982 if (tag_code == signature_type)
9983 CLASSTYPE_VTABLE_NEEDS_WRITING (ref) = 0;
9984 else
9985 CLASSTYPE_VTABLE_NEEDS_WRITING (ref) = needs_writing;
9986
9987 #ifdef NONNESTED_CLASSES
9988 /* Class types don't nest the way enums do. */
9989 class_binding_level = (struct binding_level *)0;
9990 #endif
9991 pushtag (name, ref, globalize);
9992 class_binding_level = old_b;
9993
9994 if (flag_cadillac)
9995 cadillac_start_struct (ref);
9996 }
9997 }
9998 else
9999 {
10000 if (IS_AGGR_TYPE_CODE (code))
10001 {
10002 if (IS_AGGR_TYPE (ref)
10003 && ((tag_code == exception_type)
10004 != (CLASSTYPE_DECLARED_EXCEPTION (ref) == 1)))
10005 {
10006 cp_error ("type `%T' is both exception and aggregate type", ref);
10007 CLASSTYPE_DECLARED_EXCEPTION (ref) = (tag_code == exception_type);
10008 }
10009 }
10010
10011 /* If it no longer looks like a nested type, make sure it's
10012 in global scope. */
10013 if (b == global_binding_level && !class_binding_level
10014 && IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE)
10015 IDENTIFIER_GLOBAL_VALUE (name) = TYPE_NAME (ref);
10016
10017 if (binfo)
10018 {
10019 tree tt1 = binfo;
10020 tree tt2 = TYPE_BINFO_BASETYPES (ref);
10021
10022 if (TYPE_BINFO_BASETYPES (ref))
10023 for (i = 0; tt1; i++, tt1 = TREE_CHAIN (tt1))
10024 if (TREE_VALUE (tt1) != TYPE_IDENTIFIER (BINFO_TYPE (TREE_VEC_ELT (tt2, i))))
10025 {
10026 cp_error ("redeclaration of derivation chain of type `%#T'",
10027 ref);
10028 break;
10029 }
10030
10031 if (tt1 == NULL_TREE)
10032 /* The user told us something we already knew. */
10033 goto just_return;
10034
10035 /* In C++, since these migrate into the global scope, we must
10036 build them on the permanent obstack. */
10037 end_temporary_allocation ();
10038 }
10039 }
10040
10041 if (binfo)
10042 {
10043 /* In the declaration `A : X, Y, ... Z' we mark all the types
10044 (A, X, Y, ..., Z) so we can check for duplicates. */
10045 tree binfos;
10046
10047 SET_CLASSTYPE_MARKED (ref);
10048 BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
10049
10050 for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
10051 {
10052 /* The base of a derived struct is public by default. */
10053 int via_public
10054 = (TREE_PURPOSE (binfo) == (tree)access_public
10055 || TREE_PURPOSE (binfo) == (tree)access_public_virtual
10056 || (tag_code != class_type
10057 && (TREE_PURPOSE (binfo) == (tree)access_default
10058 || TREE_PURPOSE (binfo) == (tree)access_default_virtual)));
10059 int via_protected = TREE_PURPOSE (binfo) == (tree)access_protected;
10060 int via_virtual
10061 = (TREE_PURPOSE (binfo) == (tree)access_private_virtual
10062 || TREE_PURPOSE (binfo) == (tree)access_public_virtual
10063 || TREE_PURPOSE (binfo) == (tree)access_default_virtual);
10064 tree basetype = TREE_TYPE (TREE_VALUE (binfo));
10065 tree base_binfo;
10066
10067 GNU_xref_hier (IDENTIFIER_POINTER (name),
10068 IDENTIFIER_POINTER (TREE_VALUE (binfo)),
10069 via_public, via_virtual, 0);
10070
10071 if (basetype && TREE_CODE (basetype) == TYPE_DECL)
10072 basetype = TREE_TYPE (basetype);
10073 if (!basetype || TREE_CODE (basetype) != RECORD_TYPE)
10074 {
10075 error ("base type `%s' fails to be a struct or class type",
10076 IDENTIFIER_POINTER (TREE_VALUE (binfo)));
10077 continue;
10078 }
10079 #if 1
10080 /* This code replaces similar code in layout_basetypes. */
10081 else if (TYPE_SIZE (basetype) == NULL_TREE)
10082 {
10083 cp_error ("base class `%T' has incomplete type", basetype);
10084 continue;
10085 }
10086 #endif
10087 else
10088 {
10089 if (CLASSTYPE_MARKED (basetype))
10090 {
10091 if (basetype == ref)
10092 cp_error ("recursive type `%T' undefined", basetype);
10093 else
10094 cp_error ("duplicate base type `%T' invalid", basetype);
10095 continue;
10096 }
10097
10098 /* Note that the BINFO records which describe individual
10099 inheritances are *not* shared in the lattice! They
10100 cannot be shared because a given baseclass may be
10101 inherited with different `accessibility' by different
10102 derived classes. (Each BINFO record describing an
10103 individual inheritance contains flags which say what
10104 the `accessibility' of that particular inheritance is.) */
10105
10106 base_binfo = make_binfo (integer_zero_node, basetype,
10107 TYPE_BINFO_VTABLE (basetype),
10108 TYPE_BINFO_VIRTUALS (basetype), NULL_TREE);
10109
10110 TREE_VEC_ELT (binfos, i) = base_binfo;
10111 TREE_VIA_PUBLIC (base_binfo) = via_public;
10112 TREE_VIA_PROTECTED (base_binfo) = via_protected;
10113 TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
10114
10115 SET_CLASSTYPE_MARKED (basetype);
10116 #if 0
10117 /* XYZZY TEST VIRTUAL BASECLASSES */
10118 if (CLASSTYPE_N_BASECLASSES (basetype) == NULL_TREE
10119 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
10120 && via_virtual == 0)
10121 {
10122 warning ("making type `%s' a virtual baseclass",
10123 TYPE_NAME_STRING (basetype));
10124 via_virtual = 1;
10125 }
10126 #endif
10127 /* We are free to modify these bits because they are meaningless
10128 at top level, and BASETYPE is a top-level type. */
10129 if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
10130 {
10131 TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
10132 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
10133 }
10134
10135 TYPE_OVERLOADS_METHOD_CALL_EXPR (ref) |= TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype);
10136 TYPE_GETS_NEW (ref) |= TYPE_GETS_NEW (basetype);
10137 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
10138 CLASSTYPE_LOCAL_TYPEDECLS (ref) |= CLASSTYPE_LOCAL_TYPEDECLS (basetype);
10139 i += 1;
10140 }
10141 }
10142 if (i)
10143 TREE_VEC_LENGTH (binfos) = i;
10144 else
10145 BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
10146
10147 if (i > 1)
10148 TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
10149 else if (i == 1)
10150 TYPE_USES_MULTIPLE_INHERITANCE (ref)
10151 = TYPE_USES_MULTIPLE_INHERITANCE (BINFO_TYPE (TREE_VEC_ELT (binfos, 0)));
10152 if (TYPE_USES_MULTIPLE_INHERITANCE (ref))
10153 TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
10154
10155 /* Unmark all the types. */
10156 while (--i >= 0)
10157 CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
10158 CLEAR_CLASSTYPE_MARKED (ref);
10159 }
10160
10161 just_return:
10162
10163 /* Until the type is defined, tentatively accept whatever
10164 structure tag the user hands us. */
10165 if (TYPE_SIZE (ref) == NULL_TREE
10166 && ref != current_class_type
10167 /* Have to check this, in case we have contradictory tag info. */
10168 && IS_AGGR_TYPE_CODE (TREE_CODE (ref)))
10169 {
10170 if (tag_code == class_type)
10171 CLASSTYPE_DECLARED_CLASS (ref) = 1;
10172 else if (tag_code == record_type || tag_code == signature_type)
10173 CLASSTYPE_DECLARED_CLASS (ref) = 0;
10174 }
10175
10176 pop_obstacks ();
10177
10178 return ref;
10179 }
10180 \f
10181 static tree current_local_enum = NULL_TREE;
10182
10183 /* Begin compiling the definition of an enumeration type.
10184 NAME is its name (or null if anonymous).
10185 Returns the type object, as yet incomplete.
10186 Also records info about it so that build_enumerator
10187 may be used to declare the individual values as they are read. */
10188
10189 tree
10190 start_enum (name)
10191 tree name;
10192 {
10193 register tree enumtype = NULL_TREE;
10194 struct binding_level *b = inner_binding_level;
10195
10196 /* If this is the real definition for a previous forward reference,
10197 fill in the contents in the same object that used to be the
10198 forward reference. */
10199
10200 if (name != NULL_TREE)
10201 enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
10202
10203 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
10204 cp_error ("multiple definition of enum `%T'", enumtype);
10205 else
10206 {
10207 enumtype = make_node (ENUMERAL_TYPE);
10208 pushtag (name, enumtype, 0);
10209 }
10210
10211 if (current_class_type)
10212 TREE_ADDRESSABLE (b->tags) = 1;
10213 current_local_enum = NULL_TREE;
10214
10215 if (TYPE_VALUES (enumtype) != NULL_TREE)
10216 /* Completely replace its old definition.
10217 The old enumerators remain defined, however. */
10218 TYPE_VALUES (enumtype) = NULL_TREE;
10219
10220 /* Initially, set up this enum as like `int'
10221 so that we can create the enumerators' declarations and values.
10222 Later on, the precision of the type may be changed and
10223 it may be laid out again. */
10224
10225 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
10226 TYPE_SIZE (enumtype) = NULL_TREE;
10227 fixup_unsigned_type (enumtype);
10228
10229 /* We copy this value because enumerated type constants
10230 are really of the type of the enumerator, not integer_type_node. */
10231 enum_next_value = copy_node (integer_zero_node);
10232 enum_overflow = 0;
10233
10234 GNU_xref_decl (current_function_decl, enumtype);
10235 return enumtype;
10236 }
10237
10238 /* After processing and defining all the values of an enumeration type,
10239 install their decls in the enumeration type and finish it off.
10240 ENUMTYPE is the type object and VALUES a list of name-value pairs.
10241 Returns ENUMTYPE. */
10242
10243 tree
10244 finish_enum (enumtype, values)
10245 register tree enumtype, values;
10246 {
10247 register tree pair, tem;
10248 register HOST_WIDE_INT maxvalue = 0;
10249 register HOST_WIDE_INT minvalue = 0;
10250 register HOST_WIDE_INT i;
10251
10252 /* Calculate the maximum value of any enumerator in this type. */
10253
10254 if (values)
10255 {
10256 /* Speed up the main loop by performing some precalculations */
10257
10258 HOST_WIDE_INT value;
10259 TREE_TYPE (TREE_VALUE (values)) = enumtype;
10260 TREE_TYPE (DECL_INITIAL (TREE_VALUE (values))) = enumtype;
10261 TREE_VALUE (values) = DECL_INITIAL (TREE_VALUE (values));
10262 value = TREE_INT_CST_LOW (TREE_VALUE (values));
10263 minvalue = maxvalue = value;
10264
10265 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
10266 {
10267 TREE_TYPE (TREE_VALUE (pair)) = enumtype;
10268 TREE_TYPE (DECL_INITIAL (TREE_VALUE (pair))) = enumtype;
10269 TREE_VALUE (pair) = DECL_INITIAL (TREE_VALUE (pair));
10270 value = TREE_INT_CST_LOW (TREE_VALUE (pair));
10271 if (value > maxvalue)
10272 maxvalue = value;
10273 else if (value < minvalue)
10274 minvalue = value;
10275 }
10276 }
10277
10278 TYPE_VALUES (enumtype) = values;
10279
10280 if (flag_short_enums)
10281 {
10282 /* Determine the precision this type needs, lay it out, and define
10283 it. */
10284
10285 /* First reset precision */
10286 TYPE_PRECISION (enumtype) = 0;
10287
10288 for (i = maxvalue; i; i >>= 1)
10289 TYPE_PRECISION (enumtype)++;
10290
10291 if (!TYPE_PRECISION (enumtype))
10292 TYPE_PRECISION (enumtype) = 1;
10293
10294 /* Cancel the laying out previously done for the enum type,
10295 so that fixup_unsigned_type will do it over. */
10296 TYPE_SIZE (enumtype) = NULL_TREE;
10297
10298 fixup_unsigned_type (enumtype);
10299 }
10300
10301 TREE_INT_CST_LOW (TYPE_MAX_VALUE (enumtype)) = maxvalue;
10302
10303 /* An enum can have some negative values; then it is signed. */
10304 if (minvalue < 0)
10305 {
10306 TREE_INT_CST_LOW (TYPE_MIN_VALUE (enumtype)) = minvalue;
10307 TREE_INT_CST_HIGH (TYPE_MIN_VALUE (enumtype)) = -1;
10308 TREE_UNSIGNED (enumtype) = 0;
10309 }
10310 if (flag_cadillac)
10311 cadillac_finish_enum (enumtype);
10312
10313 /* Fix up all variant types of this enum type. */
10314 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
10315 {
10316 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
10317 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
10318 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
10319 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
10320 TYPE_MODE (tem) = TYPE_MODE (enumtype);
10321 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
10322 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
10323 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
10324 }
10325
10326 /* Finish debugging output for this type. */
10327 #if 0
10328 /* @@ Do we ever generate generate ENUMERAL_TYPE nodes for which debugging
10329 information should *not* be generated? I think not. */
10330 if (! DECL_IGNORED_P (TYPE_NAME (enumtype)))
10331 #endif
10332 rest_of_type_compilation (enumtype, global_bindings_p ());
10333
10334 return enumtype;
10335 }
10336
10337 /* Build and install a CONST_DECL for one value of the
10338 current enumeration type (one that was begun with start_enum).
10339 Return a tree-list containing the name and its value.
10340 Assignment of sequential values by default is handled here. */
10341
10342 tree
10343 build_enumerator (name, value)
10344 tree name, value;
10345 {
10346 tree decl, result;
10347 /* Change this to zero if we find VALUE is not shareable. */
10348 int shareable = 1;
10349
10350 /* Remove no-op casts from the value. */
10351 if (value)
10352 STRIP_TYPE_NOPS (value);
10353
10354 /* Validate and default VALUE. */
10355 if (value != NULL_TREE)
10356 {
10357 if (TREE_READONLY_DECL_P (value))
10358 {
10359 value = decl_constant_value (value);
10360 shareable = 0;
10361 }
10362
10363 if (TREE_CODE (value) == INTEGER_CST)
10364 {
10365 value = default_conversion (value);
10366 constant_expression_warning (value);
10367 }
10368 else
10369 {
10370 cp_error ("enumerator value for `%D' not integer constant", name);
10371 value = NULL_TREE;
10372 }
10373 }
10374
10375 /* The order of things is reversed here so that we
10376 can check for possible sharing of enum values,
10377 to keep that from happening. */
10378 /* Default based on previous value. */
10379 if (value == NULL_TREE)
10380 {
10381 value = enum_next_value;
10382 if (enum_overflow)
10383 cp_error ("overflow in enumeration values at `%D'", name);
10384 }
10385
10386 /* Remove no-op casts from the value. */
10387 if (value)
10388 STRIP_TYPE_NOPS (value);
10389
10390 /* Make up for hacks in lex.c. */
10391 if (value == integer_zero_node)
10392 value = build_int_2 (0, 0);
10393 else if (value == integer_one_node)
10394 value = build_int_2 (1, 0);
10395 else if (TREE_CODE (value) == INTEGER_CST
10396 && (shareable == 0
10397 || TREE_CODE (TREE_TYPE (value)) == ENUMERAL_TYPE))
10398 {
10399 value = copy_node (value);
10400 TREE_TYPE (value) = integer_type_node;
10401 }
10402
10403 /* C++ associates enums with global, function, or class declarations. */
10404
10405 decl = current_scope ();
10406 if (decl && decl == current_class_type)
10407 {
10408 /* This enum declaration is local to the class, so we must put
10409 it in that class's list of decls. */
10410 decl = build_lang_field_decl (CONST_DECL, name, integer_type_node);
10411 DECL_INITIAL (decl) = value;
10412 TREE_READONLY (decl) = 1;
10413 pushdecl_class_level (decl);
10414 TREE_CHAIN (decl) = current_local_enum;
10415 current_local_enum = decl;
10416 }
10417 else
10418 {
10419 /* It's a global enum, or it's local to a function. (Note local to
10420 a function could mean local to a class method. */
10421 decl = build_decl (CONST_DECL, name, integer_type_node);
10422 DECL_INITIAL (decl) = value;
10423
10424 pushdecl (decl);
10425 GNU_xref_decl (current_function_decl, decl);
10426 }
10427
10428 /* Set basis for default for next value. */
10429 enum_next_value = build_binary_op_nodefault (PLUS_EXPR, value,
10430 integer_one_node, PLUS_EXPR);
10431 enum_overflow = tree_int_cst_lt (enum_next_value, value);
10432
10433 if (enum_next_value == integer_one_node)
10434 enum_next_value = copy_node (enum_next_value);
10435
10436 result = saveable_tree_cons (name, decl, NULL_TREE);
10437 return result;
10438 }
10439
10440 tree
10441 grok_enum_decls (type, decl)
10442 tree type, decl;
10443 {
10444 tree d = current_local_enum;
10445
10446 if (d == NULL_TREE)
10447 return decl;
10448
10449 while (1)
10450 {
10451 TREE_TYPE (d) = type;
10452 if (TREE_CHAIN (d) == NULL_TREE)
10453 {
10454 TREE_CHAIN (d) = decl;
10455 break;
10456 }
10457 d = TREE_CHAIN (d);
10458 }
10459
10460 decl = current_local_enum;
10461 current_local_enum = NULL_TREE;
10462
10463 return decl;
10464 }
10465 \f
10466 /* Create the FUNCTION_DECL for a function definition.
10467 DECLSPECS and DECLARATOR are the parts of the declaration;
10468 they describe the function's name and the type it returns,
10469 but twisted together in a fashion that parallels the syntax of C.
10470
10471 This function creates a binding context for the function body
10472 as well as setting up the FUNCTION_DECL in current_function_decl.
10473
10474 Returns 1 on success. If the DECLARATOR is not suitable for a function
10475 (it defines a datum instead), we return 0, which tells
10476 yyparse to report a parse error.
10477
10478 For C++, we must first check whether that datum makes any sense.
10479 For example, "class A local_a(1,2);" means that variable local_a
10480 is an aggregate of type A, which should have a constructor
10481 applied to it with the argument list [1, 2].
10482
10483 @@ There is currently no way to retrieve the storage
10484 @@ allocated to FUNCTION (or all of its parms) if we return
10485 @@ something we had previously. */
10486
10487 int
10488 start_function (declspecs, declarator, raises, pre_parsed_p)
10489 tree declarator, declspecs, raises;
10490 int pre_parsed_p;
10491 {
10492 tree decl1, olddecl;
10493 tree ctype = NULL_TREE;
10494 tree fntype;
10495 tree restype;
10496 extern int have_extern_spec;
10497 extern int used_extern_spec;
10498 int doing_friend = 0;
10499
10500 /* Sanity check. */
10501 my_friendly_assert (TREE_VALUE (void_list_node) == void_type_node, 160);
10502 my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
10503
10504 /* Assume, until we see it does. */
10505 current_function_returns_value = 0;
10506 current_function_returns_null = 0;
10507 warn_about_return_type = 0;
10508 current_extern_inline = 0;
10509 current_function_assigns_this = 0;
10510 current_function_just_assigned_this = 0;
10511 current_function_parms_stored = 0;
10512 original_result_rtx = NULL_RTX;
10513 current_function_obstack_index = 0;
10514 current_function_obstack_usage = 0;
10515
10516 clear_temp_name ();
10517
10518 /* This should only be done once on the top most decl. */
10519 if (have_extern_spec && !used_extern_spec)
10520 {
10521 declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
10522 used_extern_spec = 1;
10523 }
10524
10525 if (pre_parsed_p)
10526 {
10527 decl1 = declarator;
10528
10529 if (! DECL_ARGUMENTS (decl1)
10530 && !DECL_STATIC_FUNCTION_P (decl1)
10531 && DECL_CONTEXT (decl1)
10532 && DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl1)))
10533 && IDENTIFIER_TEMPLATE (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl1)))))
10534 {
10535 cp_error ("redeclaration of `%#D'", decl1);
10536 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)))
10537 cp_error_at ("previous declaration here", IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)));
10538 else if (IDENTIFIER_GLOBAL_VALUE (DECL_NAME (decl1)))
10539 cp_error_at ("previous declaration here", IDENTIFIER_GLOBAL_VALUE (DECL_NAME (decl1)));
10540 }
10541
10542 last_function_parms = DECL_ARGUMENTS (decl1);
10543 last_function_parm_tags = NULL_TREE;
10544 fntype = TREE_TYPE (decl1);
10545 if (TREE_CODE (fntype) == METHOD_TYPE)
10546 ctype = TYPE_METHOD_BASETYPE (fntype);
10547
10548 /* ANSI C++ June 5 1992 WP 11.4.5. A friend function defined in a
10549 class is in the (lexical) scope of the class in which it is
10550 defined. */
10551 if (!ctype && DECL_FRIEND_P (decl1))
10552 {
10553 ctype = DECL_CLASS_CONTEXT (decl1);
10554
10555 /* CTYPE could be null here if we're dealing with a template;
10556 for example, `inline friend float foo()' inside a template
10557 will have no CTYPE set. */
10558 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
10559 ctype = NULL_TREE;
10560 else
10561 doing_friend = 1;
10562 }
10563
10564 if ( !(DECL_VINDEX (decl1)
10565 && write_virtuals >= 2
10566 && CLASSTYPE_VTABLE_NEEDS_WRITING (ctype)))
10567 current_extern_inline = DECL_THIS_EXTERN (decl1) && DECL_INLINE (decl1);
10568
10569 raises = TYPE_RAISES_EXCEPTIONS (fntype);
10570
10571 /* In a fcn definition, arg types must be complete. */
10572 require_complete_types_for_parms (last_function_parms);
10573 }
10574 else
10575 {
10576 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, raises);
10577 /* If the declarator is not suitable for a function definition,
10578 cause a syntax error. */
10579 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) return 0;
10580
10581 fntype = TREE_TYPE (decl1);
10582
10583 restype = TREE_TYPE (fntype);
10584 if (IS_AGGR_TYPE (restype) && ! TYPE_PTRMEMFUNC_P (restype)
10585 && ! CLASSTYPE_GOT_SEMICOLON (restype))
10586 {
10587 cp_error ("semicolon missing after declaration of `%#T'", restype);
10588 shadow_tag (build_tree_list (NULL_TREE, restype));
10589 CLASSTYPE_GOT_SEMICOLON (restype) = 1;
10590 if (TREE_CODE (fntype) == FUNCTION_TYPE)
10591 fntype = build_function_type (integer_type_node,
10592 TYPE_ARG_TYPES (fntype));
10593 else
10594 fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
10595 integer_type_node,
10596 TYPE_ARG_TYPES (fntype));
10597 TREE_TYPE (decl1) = fntype;
10598 }
10599
10600 if (TREE_CODE (fntype) == METHOD_TYPE)
10601 ctype = TYPE_METHOD_BASETYPE (fntype);
10602 else if (IDENTIFIER_LENGTH (DECL_NAME (decl1)) == 4
10603 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (decl1)), "main")
10604 && DECL_CONTEXT (decl1) == NULL_TREE)
10605 {
10606 /* If this doesn't return integer_type, complain. */
10607 if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
10608 {
10609 if (pedantic || warn_return_type)
10610 warning ("return type for `main' changed to integer type");
10611 TREE_TYPE (decl1) = fntype = default_function_type;
10612 }
10613 warn_about_return_type = 0;
10614 }
10615 }
10616
10617 /* Warn if function was previously implicitly declared
10618 (but not if we warned then). */
10619 if (! warn_implicit
10620 && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
10621 cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
10622
10623 current_function_decl = decl1;
10624
10625 if (flag_cadillac)
10626 cadillac_start_function (decl1);
10627 else
10628 announce_function (decl1);
10629
10630 if (TYPE_SIZE (TREE_TYPE (fntype)) == NULL_TREE)
10631 {
10632 if (IS_AGGR_TYPE (TREE_TYPE (fntype)))
10633 error_with_aggr_type (TREE_TYPE (fntype),
10634 "return-type `%s' is an incomplete type");
10635 else
10636 error ("return-type is an incomplete type");
10637
10638 /* Make it return void instead, but don't change the
10639 type of the DECL_RESULT, in case we have a named return value. */
10640 if (ctype)
10641 TREE_TYPE (decl1)
10642 = build_cplus_method_type (build_type_variant (ctype,
10643 TREE_READONLY (decl1),
10644 TREE_SIDE_EFFECTS (decl1)),
10645 void_type_node,
10646 FUNCTION_ARG_CHAIN (decl1));
10647 else
10648 TREE_TYPE (decl1)
10649 = build_function_type (void_type_node,
10650 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
10651 DECL_RESULT (decl1) = build_decl (RESULT_DECL, 0, TREE_TYPE (fntype));
10652 }
10653
10654 if (warn_about_return_type)
10655 warning ("return-type defaults to `int'");
10656
10657 /* Make the init_value nonzero so pushdecl knows this is not tentative.
10658 error_mark_node is replaced below (in poplevel) with the BLOCK. */
10659 DECL_INITIAL (decl1) = error_mark_node;
10660
10661 /* Didn't get anything from C. */
10662 olddecl = NULL_TREE;
10663
10664 /* This function exists in static storage.
10665 (This does not mean `static' in the C sense!) */
10666 TREE_STATIC (decl1) = 1;
10667
10668 /* Record the decl so that the function name is defined.
10669 If we already have a decl for this name, and it is a FUNCTION_DECL,
10670 use the old decl. */
10671
10672 if (pre_parsed_p == 0)
10673 {
10674 current_function_decl = decl1 = pushdecl (decl1);
10675 DECL_MAIN_VARIANT (decl1) = decl1;
10676 fntype = TREE_TYPE (decl1);
10677 }
10678 else
10679 current_function_decl = decl1;
10680
10681 /* If this function belongs to an interface, it is public.
10682 If it belongs to someone else's interface, it is also external.
10683 It doesn't matter whether it's inline or not. */
10684 if (interface_unknown == 0)
10685 {
10686 TREE_PUBLIC (decl1) = 1;
10687 DECL_EXTERNAL (decl1)
10688 = (interface_only
10689 || (DECL_INLINE (decl1) && ! flag_implement_inlines));
10690 }
10691 else if (DECL_EXPLICIT_INSTANTIATION (decl1))
10692 /* PUBLIC and EXTERNAL set by do_*_instantiation */;
10693 else
10694 {
10695 /* This is a definition, not a reference.
10696 So normally clear DECL_EXTERNAL.
10697 However, `extern inline' acts like a declaration except for
10698 defining how to inline. So set DECL_EXTERNAL in that case. */
10699 DECL_EXTERNAL (decl1) = current_extern_inline;
10700
10701 DECL_DEFER_OUTPUT (decl1) = DECL_INLINE (decl1);
10702 }
10703
10704 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1))
10705 {
10706 if (TREE_CODE (fntype) == METHOD_TYPE)
10707 TREE_TYPE (decl1) = fntype
10708 = build_function_type (TREE_TYPE (fntype),
10709 TREE_CHAIN (TYPE_ARG_TYPES (fntype)));
10710 last_function_parms = TREE_CHAIN (last_function_parms);
10711 DECL_ARGUMENTS (decl1) = last_function_parms;
10712 ctype = NULL_TREE;
10713 }
10714 restype = TREE_TYPE (fntype);
10715
10716 pushlevel (0);
10717 current_binding_level->parm_flag = 1;
10718
10719 /* Save the parm names or decls from this function's declarator
10720 where store_parm_decls will find them. */
10721 current_function_parms = last_function_parms;
10722 current_function_parm_tags = last_function_parm_tags;
10723
10724 GNU_xref_function (decl1, current_function_parms);
10725
10726 make_function_rtl (decl1);
10727
10728 if (ctype)
10729 {
10730 push_nested_class (ctype, 1);
10731
10732 /* If we're compiling a friend function, neither of the variables
10733 current_class_decl nor current_class_type will have values. */
10734 if (! doing_friend)
10735 {
10736 /* We know that this was set up by `grokclassfn'.
10737 We do not wait until `store_parm_decls', since evil
10738 parse errors may never get us to that point. Here
10739 we keep the consistency between `current_class_type'
10740 and `current_class_decl'. */
10741 current_class_decl = last_function_parms;
10742 my_friendly_assert (current_class_decl != NULL_TREE
10743 && TREE_CODE (current_class_decl) == PARM_DECL, 162);
10744 if (TREE_CODE (TREE_TYPE (current_class_decl)) == POINTER_TYPE)
10745 {
10746 tree variant = TREE_TYPE (TREE_TYPE (current_class_decl));
10747 if (CLASSTYPE_INST_VAR (ctype) == NULL_TREE)
10748 {
10749 /* Can't call build_indirect_ref here, because it has special
10750 logic to return C_C_D given this argument. */
10751 C_C_D = build1 (INDIRECT_REF, current_class_type, current_class_decl);
10752 CLASSTYPE_INST_VAR (ctype) = C_C_D;
10753 }
10754 else
10755 {
10756 C_C_D = CLASSTYPE_INST_VAR (ctype);
10757 /* `current_class_decl' is different for every
10758 function we compile. */
10759 TREE_OPERAND (C_C_D, 0) = current_class_decl;
10760 }
10761 TREE_READONLY (C_C_D) = TYPE_READONLY (variant);
10762 TREE_SIDE_EFFECTS (C_C_D) = TYPE_VOLATILE (variant);
10763 TREE_THIS_VOLATILE (C_C_D) = TYPE_VOLATILE (variant);
10764 }
10765 else
10766 C_C_D = current_class_decl;
10767 }
10768 }
10769 else
10770 {
10771 if (DECL_STATIC_FUNCTION_P (decl1))
10772 push_nested_class (DECL_CONTEXT (decl1), 2);
10773 else
10774 push_memoized_context (0, 1);
10775 }
10776
10777 /* Allocate further tree nodes temporarily during compilation
10778 of this function only. Tiemann moved up here from bottom of fn. */
10779 temporary_allocation ();
10780
10781 /* Promote the value to int before returning it. */
10782 if (C_PROMOTING_INTEGER_TYPE_P (restype))
10783 {
10784 /* It retains unsignedness if traditional or if it isn't
10785 really getting wider. */
10786 if (TREE_UNSIGNED (restype)
10787 && (flag_traditional
10788 || TYPE_PRECISION (restype)
10789 == TYPE_PRECISION (integer_type_node)))
10790 restype = unsigned_type_node;
10791 else
10792 restype = integer_type_node;
10793 }
10794 if (DECL_RESULT (decl1) == NULL_TREE)
10795 DECL_RESULT (decl1) = build_decl (RESULT_DECL, 0, restype);
10796
10797 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl1)))
10798 {
10799 dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
10800 ctor_label = NULL_TREE;
10801 }
10802 else
10803 {
10804 dtor_label = NULL_TREE;
10805 if (DECL_CONSTRUCTOR_P (decl1))
10806 ctor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
10807 }
10808
10809 /* If this fcn was already referenced via a block-scope `extern' decl
10810 (or an implicit decl), propagate certain information about the usage. */
10811 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (decl1)))
10812 TREE_ADDRESSABLE (decl1) = 1;
10813
10814 return 1;
10815 }
10816 \f
10817 /* Store the parameter declarations into the current function declaration.
10818 This is called after parsing the parameter declarations, before
10819 digesting the body of the function.
10820
10821 Also install to binding contour return value identifier, if any. */
10822
10823 void
10824 store_parm_decls ()
10825 {
10826 register tree fndecl = current_function_decl;
10827 register tree parm;
10828 int parms_have_cleanups = 0;
10829
10830 /* This is either a chain of PARM_DECLs (when a prototype is used). */
10831 tree specparms = current_function_parms;
10832
10833 /* This is a list of types declared among parms in a prototype. */
10834 tree parmtags = current_function_parm_tags;
10835
10836 /* This is a chain of any other decls that came in among the parm
10837 declarations. If a parm is declared with enum {foo, bar} x;
10838 then CONST_DECLs for foo and bar are put here. */
10839 tree nonparms = NULL_TREE;
10840
10841 if (current_binding_level == global_binding_level)
10842 fatal ("parse errors have confused me too much");
10843
10844 /* Initialize RTL machinery. */
10845 init_function_start (fndecl, input_filename, lineno);
10846
10847 /* Declare __FUNCTION__ and __PRETTY_FUNCTION__ for this function. */
10848 declare_function_name ();
10849
10850 /* Create a binding level for the parms. */
10851 expand_start_bindings (0);
10852
10853 if (specparms != NULL_TREE)
10854 {
10855 /* This case is when the function was defined with an ANSI prototype.
10856 The parms already have decls, so we need not do anything here
10857 except record them as in effect
10858 and complain if any redundant old-style parm decls were written. */
10859
10860 register tree next;
10861
10862 /* Must clear this because it might contain TYPE_DECLs declared
10863 at class level. */
10864 storedecls (NULL_TREE);
10865 for (parm = nreverse (specparms); parm; parm = next)
10866 {
10867 next = TREE_CHAIN (parm);
10868 if (TREE_CODE (parm) == PARM_DECL)
10869 {
10870 tree cleanup = maybe_build_cleanup (parm);
10871 if (DECL_NAME (parm) == NULL_TREE)
10872 {
10873 #if 0
10874 cp_error_at ("parameter name omitted", parm);
10875 #else
10876 /* for C++, this is not an error. */
10877 pushdecl (parm);
10878 #endif
10879 }
10880 else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
10881 cp_error ("parameter `%D' declared void", parm);
10882 else
10883 {
10884 /* Now fill in DECL_REFERENCE_SLOT for any of the parm decls.
10885 A parameter is assumed not to have any side effects.
10886 If this should change for any reason, then this
10887 will have to wrap the bashed reference type in a save_expr.
10888
10889 Also, if the parameter type is declared to be an X
10890 and there is an X(X&) constructor, we cannot lay it
10891 into the stack (any more), so we make this parameter
10892 look like it is really of reference type. Functions
10893 which pass parameters to this function will know to
10894 create a temporary in their frame, and pass a reference
10895 to that. */
10896
10897 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
10898 && TYPE_SIZE (TREE_TYPE (TREE_TYPE (parm))))
10899 SET_DECL_REFERENCE_SLOT (parm, convert_from_reference (parm));
10900
10901 pushdecl (parm);
10902 }
10903 if (cleanup)
10904 {
10905 expand_decl (parm);
10906 if (! expand_decl_cleanup (parm, cleanup))
10907 cp_error ("parser lost in parsing declaration of `%D'",
10908 parm);
10909 parms_have_cleanups = 1;
10910 }
10911 }
10912 else
10913 {
10914 /* If we find an enum constant or a type tag,
10915 put it aside for the moment. */
10916 TREE_CHAIN (parm) = NULL_TREE;
10917 nonparms = chainon (nonparms, parm);
10918 }
10919 }
10920
10921 /* Get the decls in their original chain order
10922 and record in the function. This is all and only the
10923 PARM_DECLs that were pushed into scope by the loop above. */
10924 DECL_ARGUMENTS (fndecl) = getdecls ();
10925
10926 storetags (chainon (parmtags, gettags ()));
10927 }
10928 else
10929 DECL_ARGUMENTS (fndecl) = NULL_TREE;
10930
10931 /* Now store the final chain of decls for the arguments
10932 as the decl-chain of the current lexical scope.
10933 Put the enumerators in as well, at the front so that
10934 DECL_ARGUMENTS is not modified. */
10935
10936 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
10937
10938 /* Initialize the RTL code for the function. */
10939 DECL_SAVED_INSNS (fndecl) = NULL_RTX;
10940 expand_function_start (fndecl, parms_have_cleanups);
10941
10942 /* Create a binding contour which can be used to catch
10943 cleanup-generated temporaries. Also, if the return value needs or
10944 has initialization, deal with that now. */
10945 if (parms_have_cleanups)
10946 {
10947 pushlevel (0);
10948 expand_start_bindings (0);
10949 }
10950
10951 current_function_parms_stored = 1;
10952
10953 if (flag_gc)
10954 {
10955 maybe_gc_cleanup = build_tree_list (NULL_TREE, error_mark_node);
10956 if (! expand_decl_cleanup (NULL_TREE, maybe_gc_cleanup))
10957 cp_error ("parser lost in parsing declaration of `%D'", fndecl);
10958 }
10959
10960 /* If this function is `main', emit a call to `__main'
10961 to run global initializers, etc. */
10962 if (DECL_NAME (fndecl)
10963 && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 4
10964 && strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main") == 0
10965 && DECL_CONTEXT (fndecl) == NULL_TREE)
10966 {
10967 expand_main_function ();
10968
10969 if (flag_gc)
10970 expand_expr (build_function_call (lookup_name (get_identifier ("__gc_main"), 0), NULL_TREE),
10971 0, VOIDmode, 0);
10972
10973 if (flag_dossier)
10974 output_builtin_tdesc_entries ();
10975 }
10976 }
10977
10978 /* Bind a name and initialization to the return value of
10979 the current function. */
10980 void
10981 store_return_init (return_id, init)
10982 tree return_id, init;
10983 {
10984 tree decl = DECL_RESULT (current_function_decl);
10985
10986 if (flag_ansi)
10987 /* Give this error as many times as there are occurrences,
10988 so that users can use Emacs compilation buffers to find
10989 and fix all such places. */
10990 pedwarn ("ANSI C++ does not permit named return values");
10991
10992 if (return_id != NULL_TREE)
10993 {
10994 if (DECL_NAME (decl) == NULL_TREE)
10995 {
10996 DECL_NAME (decl) = return_id;
10997 DECL_ASSEMBLER_NAME (decl) = return_id;
10998 }
10999 else
11000 error ("return identifier `%s' already in place",
11001 IDENTIFIER_POINTER (DECL_NAME (decl)));
11002 }
11003
11004 /* Can't let this happen for constructors. */
11005 if (DECL_CONSTRUCTOR_P (current_function_decl))
11006 {
11007 error ("can't redefine default return value for constructors");
11008 return;
11009 }
11010
11011 /* If we have a named return value, put that in our scope as well. */
11012 if (DECL_NAME (decl) != NULL_TREE)
11013 {
11014 /* If this named return value comes in a register,
11015 put it in a pseudo-register. */
11016 if (DECL_REGISTER (decl))
11017 {
11018 original_result_rtx = DECL_RTL (decl);
11019 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
11020 }
11021
11022 /* Let `finish_decl' know that this initializer is ok. */
11023 DECL_INITIAL (decl) = init;
11024 pushdecl (decl);
11025 finish_decl (decl, init, 0, 0);
11026 }
11027 }
11028
11029 #if 0
11030 /* Generate code for default X() constructor. */
11031 static void
11032 build_default_constructor (fndecl)
11033 tree fndecl;
11034 {
11035 int i = CLASSTYPE_N_BASECLASSES (current_class_type);
11036 tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
11037 tree fields = TYPE_FIELDS (current_class_type);
11038 tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
11039
11040 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
11041 parm = TREE_CHAIN (parm);
11042 parm = DECL_REFERENCE_SLOT (parm);
11043
11044 while (--i >= 0)
11045 {
11046 tree basetype = TREE_VEC_ELT (binfos, i);
11047 if (TYPE_HAS_INIT_REF (basetype))
11048 {
11049 tree name = TYPE_NAME (basetype);
11050 if (TREE_CODE (name) == TYPE_DECL)
11051 name = DECL_NAME (name);
11052 current_base_init_list = tree_cons (name, parm, current_base_init_list);
11053 }
11054 }
11055 for (; fields; fields = TREE_CHAIN (fields))
11056 {
11057 tree name, init;
11058 if (TREE_STATIC (fields))
11059 continue;
11060 if (TREE_CODE (fields) != FIELD_DECL)
11061 continue;
11062 if (DECL_NAME (fields))
11063 {
11064 if (VFIELD_NAME_P (DECL_NAME (fields)))
11065 continue;
11066 if (VBASE_NAME_P (DECL_NAME (fields)))
11067 continue;
11068
11069 /* True for duplicate members. */
11070 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (fields)) != fields)
11071 continue;
11072 }
11073
11074 init = build (COMPONENT_REF, TREE_TYPE (fields), parm, fields);
11075 init = build_tree_list (NULL_TREE, init);
11076
11077 current_member_init_list
11078 = tree_cons (DECL_NAME (fields), init, current_member_init_list);
11079 }
11080 }
11081 #endif
11082 \f
11083 /* Finish up a function declaration and compile that function
11084 all the way to assembler language output. The free the storage
11085 for the function definition.
11086
11087 This is called after parsing the body of the function definition.
11088 LINENO is the current line number.
11089
11090 C++: CALL_POPLEVEL is non-zero if an extra call to poplevel
11091 (and expand_end_bindings) must be made to take care of the binding
11092 contour for the base initializers. This is only relevant for
11093 constructors. */
11094
11095 void
11096 finish_function (lineno, call_poplevel)
11097 int lineno;
11098 int call_poplevel;
11099 {
11100 register tree fndecl = current_function_decl;
11101 tree fntype, ctype = NULL_TREE;
11102 rtx head, last_parm_insn, mark;
11103 /* Label to use if this function is supposed to return a value. */
11104 tree no_return_label = NULL_TREE;
11105 tree decls = NULL_TREE;
11106
11107 /* When we get some parse errors, we can end up without a
11108 current_function_decl, so cope. */
11109 if (fndecl == NULL_TREE)
11110 return;
11111
11112 fntype = TREE_TYPE (fndecl);
11113
11114 /* TREE_READONLY (fndecl) = 1;
11115 This caused &foo to be of type ptr-to-const-function
11116 which then got a warning when stored in a ptr-to-function variable. */
11117
11118 /* This happens on strange parse errors. */
11119 if (! current_function_parms_stored)
11120 {
11121 call_poplevel = 0;
11122 store_parm_decls ();
11123 }
11124
11125 if (write_symbols != NO_DEBUG && TREE_CODE (fntype) != METHOD_TYPE)
11126 {
11127 tree ttype = target_type (fntype);
11128 tree parmdecl;
11129
11130 if (IS_AGGR_TYPE (ttype))
11131 /* Let debugger know it should output info for this type. */
11132 note_debug_info_needed (ttype);
11133
11134 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
11135 {
11136 ttype = target_type (TREE_TYPE (parmdecl));
11137 if (IS_AGGR_TYPE (ttype))
11138 /* Let debugger know it should output info for this type. */
11139 note_debug_info_needed (ttype);
11140 }
11141 }
11142
11143 /* Clean house because we will need to reorder insns here. */
11144 do_pending_stack_adjust ();
11145
11146 if (dtor_label)
11147 {
11148 tree binfo = TYPE_BINFO (current_class_type);
11149 tree cond = integer_one_node;
11150 tree exprstmt, vfields;
11151 tree in_charge_node = lookup_name (in_charge_identifier, 0);
11152 tree virtual_size;
11153 int ok_to_optimize_dtor = 0;
11154
11155 if (current_function_assigns_this)
11156 cond = build (NE_EXPR, integer_type_node,
11157 current_class_decl, integer_zero_node);
11158 else
11159 {
11160 int n_baseclasses = CLASSTYPE_N_BASECLASSES (current_class_type);
11161
11162 /* If this destructor is empty, then we don't need to check
11163 whether `this' is NULL in some cases. */
11164 mark = get_last_insn ();
11165 last_parm_insn = get_first_nonparm_insn ();
11166
11167 if ((flag_this_is_variable & 1) == 0)
11168 ok_to_optimize_dtor = 1;
11169 else if (mark == last_parm_insn)
11170 ok_to_optimize_dtor
11171 = (n_baseclasses == 0
11172 || (n_baseclasses == 1
11173 && TYPE_HAS_DESTRUCTOR (TYPE_BINFO_BASETYPE (current_class_type, 0))));
11174 }
11175
11176 /* These initializations might go inline. Protect
11177 the binding level of the parms. */
11178 pushlevel (0);
11179 expand_start_bindings (0);
11180
11181 if (current_function_assigns_this)
11182 {
11183 current_function_assigns_this = 0;
11184 current_function_just_assigned_this = 0;
11185 }
11186
11187 /* Generate the code to call destructor on base class.
11188 If this destructor belongs to a class with virtual
11189 functions, then set the virtual function table
11190 pointer to represent the type of our base class. */
11191
11192 /* This side-effect makes call to `build_delete' generate the
11193 code we have to have at the end of this destructor. */
11194 TYPE_HAS_DESTRUCTOR (current_class_type) = 0;
11195
11196 /* These are two cases where we cannot delegate deletion. */
11197 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)
11198 || TYPE_GETS_REG_DELETE (current_class_type))
11199 exprstmt = build_delete (current_class_type, C_C_D, integer_zero_node,
11200 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
11201 else
11202 exprstmt = build_delete (current_class_type, C_C_D, in_charge_node,
11203 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
11204
11205 /* If we did not assign to this, then `this' is non-zero at
11206 the end of a destructor. As a special optimization, don't
11207 emit test if this is an empty destructor. If it does nothing,
11208 it does nothing. If it calls a base destructor, the base
11209 destructor will perform the test. */
11210
11211 if (exprstmt != error_mark_node
11212 && (TREE_CODE (exprstmt) != NOP_EXPR
11213 || TREE_OPERAND (exprstmt, 0) != integer_zero_node
11214 || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)))
11215 {
11216 expand_label (dtor_label);
11217 if (cond != integer_one_node)
11218 expand_start_cond (cond, 0);
11219 if (exprstmt != void_zero_node)
11220 /* Don't call `expand_expr_stmt' if we're not going to do
11221 anything, since -Wall will give a diagnostic. */
11222 expand_expr_stmt (exprstmt);
11223
11224 /* Run destructor on all virtual baseclasses. */
11225 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
11226 {
11227 tree vbases = nreverse (copy_list (CLASSTYPE_VBASECLASSES (current_class_type)));
11228 expand_start_cond (build (BIT_AND_EXPR, integer_type_node,
11229 in_charge_node, integer_two_node), 0);
11230 while (vbases)
11231 {
11232 if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (vbases)))
11233 {
11234 tree ptr = convert_pointer_to_vbase (vbases, current_class_decl);
11235 expand_expr_stmt (build_delete (TYPE_POINTER_TO (BINFO_TYPE (vbases)),
11236 ptr, integer_zero_node,
11237 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_HAS_IN_CHARGE, 0));
11238 }
11239 vbases = TREE_CHAIN (vbases);
11240 }
11241 expand_end_cond ();
11242 }
11243
11244 do_pending_stack_adjust ();
11245 if (cond != integer_one_node)
11246 expand_end_cond ();
11247 }
11248
11249 TYPE_HAS_DESTRUCTOR (current_class_type) = 1;
11250
11251 virtual_size = c_sizeof (current_class_type);
11252
11253 /* At the end, call delete if that's what's requested. */
11254 if (TYPE_GETS_REG_DELETE (current_class_type))
11255 /* This NOP_EXPR means we are in a static call context. */
11256 exprstmt =
11257 build_method_call
11258 (build_indirect_ref
11259 (build1 (NOP_EXPR, TYPE_POINTER_TO (current_class_type),
11260 error_mark_node),
11261 NULL_PTR),
11262 ansi_opname[(int) DELETE_EXPR],
11263 tree_cons (NULL_TREE, current_class_decl,
11264 build_tree_list (NULL_TREE, virtual_size)),
11265 NULL_TREE, LOOKUP_NORMAL);
11266 else if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
11267 exprstmt = build_x_delete (ptr_type_node, current_class_decl, 0,
11268 virtual_size);
11269 else
11270 exprstmt = NULL_TREE;
11271
11272 if (exprstmt)
11273 {
11274 cond = build (BIT_AND_EXPR, integer_type_node,
11275 in_charge_node, integer_one_node);
11276 expand_start_cond (cond, 0);
11277 expand_expr_stmt (exprstmt);
11278 expand_end_cond ();
11279 }
11280
11281 /* End of destructor. */
11282 expand_end_bindings (NULL_TREE, getdecls() != NULL_TREE, 0);
11283 poplevel (2, 0, 0); /* XXX change to 1 */
11284
11285 /* Back to the top of destructor. */
11286 /* Dont execute destructor code if `this' is NULL. */
11287 mark = get_last_insn ();
11288 last_parm_insn = get_first_nonparm_insn ();
11289 if (last_parm_insn == NULL_RTX)
11290 last_parm_insn = mark;
11291 else
11292 last_parm_insn = previous_insn (last_parm_insn);
11293
11294 /* Make all virtual function table pointers in non-virtual base
11295 classes point to CURRENT_CLASS_TYPE's virtual function
11296 tables. */
11297 expand_direct_vtbls_init (binfo, binfo, 1, 0, current_class_decl);
11298 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
11299 expand_indirect_vtbls_init (binfo, C_C_D, current_class_decl, 0);
11300 if (! ok_to_optimize_dtor)
11301 {
11302 cond = build_binary_op (NE_EXPR,
11303 current_class_decl, integer_zero_node, 1);
11304 expand_start_cond (cond, 0);
11305 }
11306 if (mark != get_last_insn ())
11307 reorder_insns (next_insn (mark), get_last_insn (), last_parm_insn);
11308 if (! ok_to_optimize_dtor)
11309 expand_end_cond ();
11310 }
11311 else if (current_function_assigns_this)
11312 {
11313 /* Does not need to call emit_base_init, because
11314 that is done (if needed) just after assignment to this
11315 is seen. */
11316
11317 if (DECL_CONSTRUCTOR_P (current_function_decl))
11318 {
11319 expand_label (ctor_label);
11320 ctor_label = NULL_TREE;
11321
11322 if (call_poplevel)
11323 {
11324 decls = getdecls ();
11325 expand_end_bindings (decls, decls != NULL_TREE, 0);
11326 poplevel (decls != NULL_TREE, 0, 0);
11327 }
11328 c_expand_return (current_class_decl);
11329 }
11330 else if (TYPE_MAIN_VARIANT (TREE_TYPE (
11331 DECL_RESULT (current_function_decl))) != void_type_node
11332 && return_label != NULL_RTX)
11333 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
11334
11335 current_function_assigns_this = 0;
11336 current_function_just_assigned_this = 0;
11337 base_init_insns = NULL_RTX;
11338 }
11339 else if (DECL_CONSTRUCTOR_P (fndecl))
11340 {
11341 tree allocated_this;
11342 tree cond, thenclause;
11343 /* Allow constructor for a type to get a new instance of the object
11344 using `build_new'. */
11345 tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type);
11346 CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = NULL_TREE;
11347
11348 DECL_RETURNS_FIRST_ARG (fndecl) = 1;
11349
11350 if (flag_this_is_variable > 0)
11351 {
11352 cond = build_binary_op (EQ_EXPR,
11353 current_class_decl, integer_zero_node, 1);
11354 thenclause = build_modify_expr (current_class_decl, NOP_EXPR,
11355 build_new (NULL_TREE, current_class_type, void_type_node, 0));
11356 }
11357
11358 CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = abstract_virtuals;
11359
11360 /* must keep the first insn safe. */
11361 head = get_insns ();
11362
11363 /* this note will come up to the top with us. */
11364 mark = get_last_insn ();
11365
11366 if (flag_this_is_variable > 0)
11367 {
11368 expand_start_cond (cond, 0);
11369 expand_expr_stmt (thenclause);
11370 expand_end_cond ();
11371 }
11372
11373 #if 0
11374 if (DECL_NAME (fndecl) == NULL_TREE
11375 && TREE_CHAIN (DECL_ARGUMENTS (fndecl)) != NULL_TREE)
11376 build_default_constructor (fndecl);
11377 #endif
11378
11379 /* Emit insns from `emit_base_init' which sets up virtual
11380 function table pointer(s). */
11381 emit_insns (base_init_insns);
11382 base_init_insns = NULL_RTX;
11383
11384 /* This is where the body of the constructor begins.
11385 If there were no insns in this function body, then the
11386 last_parm_insn is also the last insn.
11387
11388 If optimization is enabled, last_parm_insn may move, so
11389 we don't hold on to it (across emit_base_init). */
11390 last_parm_insn = get_first_nonparm_insn ();
11391 if (last_parm_insn == NULL_RTX)
11392 last_parm_insn = mark;
11393 else
11394 last_parm_insn = previous_insn (last_parm_insn);
11395
11396 if (mark != get_last_insn ())
11397 reorder_insns (next_insn (mark), get_last_insn (), last_parm_insn);
11398
11399 /* This is where the body of the constructor ends. */
11400 expand_label (ctor_label);
11401 ctor_label = NULL_TREE;
11402
11403 if (call_poplevel)
11404 {
11405 expand_end_bindings (decls = getdecls (), decls != NULL_TREE, 0);
11406 poplevel (decls != NULL_TREE, 1, 0);
11407 }
11408
11409 c_expand_return (current_class_decl);
11410
11411 current_function_assigns_this = 0;
11412 current_function_just_assigned_this = 0;
11413 }
11414 else if (IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 4
11415 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main")
11416 && DECL_CONTEXT (fndecl) == NULL_TREE)
11417 {
11418 /* Make it so that `main' always returns 0 by default. */
11419 #ifdef VMS
11420 c_expand_return (integer_one_node);
11421 #else
11422 c_expand_return (integer_zero_node);
11423 #endif
11424 }
11425 else if (return_label != NULL_RTX
11426 && current_function_return_value == NULL_TREE
11427 && ! DECL_NAME (DECL_RESULT (current_function_decl)))
11428 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
11429
11430 if (flag_gc)
11431 expand_gc_prologue_and_epilogue ();
11432
11433 /* That's the end of the vtable decl's life. Need to mark it such
11434 if doing stupid register allocation.
11435
11436 Note that current_vtable_decl is really an INDIRECT_REF
11437 on top of a VAR_DECL here. */
11438 if (obey_regdecls && current_vtable_decl)
11439 use_variable (DECL_RTL (TREE_OPERAND (current_vtable_decl, 0)));
11440
11441 /* If this function is supposed to return a value, ensure that
11442 we do not fall into the cleanups by mistake. The end of our
11443 function will look like this:
11444
11445 user code (may have return stmt somewhere)
11446 goto no_return_label
11447 cleanup_label:
11448 cleanups
11449 goto return_label
11450 no_return_label:
11451 NOTE_INSN_FUNCTION_END
11452 return_label:
11453 things for return
11454
11455 If the user omits a return stmt in the USER CODE section, we
11456 will have a control path which reaches NOTE_INSN_FUNCTION_END.
11457 Otherwise, we won't. */
11458 if (no_return_label)
11459 {
11460 DECL_CONTEXT (no_return_label) = fndecl;
11461 DECL_INITIAL (no_return_label) = error_mark_node;
11462 DECL_SOURCE_FILE (no_return_label) = input_filename;
11463 DECL_SOURCE_LINE (no_return_label) = lineno;
11464 expand_goto (no_return_label);
11465 }
11466
11467 if (cleanup_label)
11468 {
11469 /* remove the binding contour which is used
11470 to catch cleanup-generated temporaries. */
11471 expand_end_bindings (0, 0, 0);
11472 poplevel (0, 0, 0);
11473 }
11474
11475 if (cleanup_label)
11476 /* Emit label at beginning of cleanup code for parameters. */
11477 emit_label (cleanup_label);
11478
11479 /* Get return value into register if that's where it's supposed to be. */
11480 if (original_result_rtx)
11481 fixup_result_decl (DECL_RESULT (fndecl), original_result_rtx);
11482
11483 /* Finish building code that will trigger warnings if users forget
11484 to make their functions return values. */
11485 if (no_return_label || cleanup_label)
11486 emit_jump (return_label);
11487 if (no_return_label)
11488 {
11489 /* We don't need to call `expand_*_return' here because we
11490 don't need any cleanups here--this path of code is only
11491 for error checking purposes. */
11492 expand_label (no_return_label);
11493 }
11494
11495 /* reset scope for C++: if we were in the scope of a class,
11496 then when we finish this function, we are not longer so.
11497 This cannot be done until we know for sure that no more
11498 class members will ever be referenced in this function
11499 (i.e., calls to destructors). */
11500 if (current_class_name)
11501 {
11502 ctype = current_class_type;
11503 pop_nested_class (1);
11504 }
11505 else
11506 pop_memoized_context (1);
11507
11508 /* Generate rtl for function exit. */
11509 expand_function_end (input_filename, lineno, 1);
11510
11511 if (flag_handle_exceptions)
11512 expand_exception_blocks();
11513
11514 /* This must come after expand_function_end because cleanups might
11515 have declarations (from inline functions) that need to go into
11516 this function's blocks. */
11517 if (current_binding_level->parm_flag != 1)
11518 my_friendly_abort (122);
11519 poplevel (1, 0, 1);
11520
11521 /* Must mark the RESULT_DECL as being in this function. */
11522 DECL_CONTEXT (DECL_RESULT (fndecl)) = DECL_INITIAL (fndecl);
11523
11524 /* Obey `register' declarations if `setjmp' is called in this fn. */
11525 if (flag_traditional && current_function_calls_setjmp)
11526 setjmp_protect (DECL_INITIAL (fndecl));
11527
11528 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
11529 to the FUNCTION_DECL node itself. */
11530 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11531
11532 /* So we can tell if jump_optimize sets it to 1. */
11533 can_reach_end = 0;
11534
11535 /* ??? Compensate for Sun brain damage in dealing with data segments
11536 of PIC code. */
11537 if (flag_pic
11538 && (DECL_CONSTRUCTOR_P (fndecl)
11539 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
11540 && CLASSTYPE_NEEDS_VIRTUAL_REINIT (TYPE_METHOD_BASETYPE (fntype)))
11541 DECL_INLINE (fndecl) = 0;
11542
11543 if (DECL_EXTERNAL (fndecl)
11544 /* This function is just along for the ride. If we can make
11545 it inline, that's great. Otherwise, just punt it. */
11546 && (DECL_INLINE (fndecl) == 0
11547 || flag_no_inline
11548 || function_cannot_inline_p (fndecl)))
11549 {
11550 extern int rtl_dump_and_exit;
11551 int old_rtl_dump_and_exit = rtl_dump_and_exit;
11552 int inline_spec = DECL_INLINE (fndecl);
11553
11554 /* This throws away the code for FNDECL. */
11555 rtl_dump_and_exit = 1;
11556 /* This throws away the memory of the code for FNDECL. */
11557 if (flag_no_inline)
11558 DECL_INLINE (fndecl) = 0;
11559 rest_of_compilation (fndecl);
11560 rtl_dump_and_exit = old_rtl_dump_and_exit;
11561 DECL_INLINE (fndecl) = inline_spec;
11562 }
11563 else
11564 {
11565 /* Run the optimizers and output the assembler code for this
11566 function. */
11567 rest_of_compilation (fndecl);
11568 }
11569
11570 if (DECL_INLINE (fndecl)
11571 && !TREE_ASM_WRITTEN (fndecl) && DECL_FUNCTION_MEMBER_P (fndecl))
11572 {
11573 mark_inline_for_output (fndecl);
11574 }
11575
11576 if (ctype && TREE_ASM_WRITTEN (fndecl))
11577 note_debug_info_needed (ctype);
11578
11579 current_function_returns_null |= can_reach_end;
11580
11581 /* Since we don't normally go through c_expand_return for constructors,
11582 this normally gets the wrong value.
11583 Also, named return values have their return codes emitted after
11584 NOTE_INSN_FUNCTION_END, confusing jump.c. */
11585 if (DECL_CONSTRUCTOR_P (fndecl)
11586 || DECL_NAME (DECL_RESULT (fndecl)) != NULL_TREE)
11587 current_function_returns_null = 0;
11588
11589 if (TREE_THIS_VOLATILE (fndecl) && current_function_returns_null)
11590 cp_warning ("`noreturn' function `%D' does return", fndecl);
11591 else if ((warn_return_type || pedantic)
11592 && current_function_returns_null
11593 && TYPE_MAIN_VARIANT (TREE_TYPE (fntype)) != void_type_node)
11594 {
11595 /* If this function returns non-void and control can drop through,
11596 complain. */
11597 cp_pedwarn ("control reaches end of non-void function `%D'", fndecl);
11598 }
11599 /* With just -W, complain only if function returns both with
11600 and without a value. */
11601 else if (extra_warnings
11602 && current_function_returns_value && current_function_returns_null)
11603 warning ("this function may return with or without a value");
11604
11605 /* Free all the tree nodes making up this function. */
11606 /* Switch back to allocating nodes permanently
11607 until we start another function. */
11608 permanent_allocation (1);
11609
11610 if (flag_cadillac)
11611 cadillac_finish_function (fndecl);
11612
11613 if (DECL_SAVED_INSNS (fndecl) == NULL_RTX)
11614 {
11615 /* Stop pointing to the local nodes about to be freed. */
11616 /* But DECL_INITIAL must remain nonzero so we know this
11617 was an actual function definition. */
11618 DECL_INITIAL (fndecl) = error_mark_node;
11619 if (! DECL_CONSTRUCTOR_P (fndecl)
11620 || !TYPE_USES_VIRTUAL_BASECLASSES (TYPE_METHOD_BASETYPE (fntype)))
11621 DECL_ARGUMENTS (fndecl) = NULL_TREE;
11622 }
11623
11624 /* Let the error reporting routines know that we're outside a function. */
11625 current_function_decl = NULL_TREE;
11626 named_label_uses = NULL_TREE;
11627 }
11628 \f
11629 /* Create the FUNCTION_DECL for a function definition.
11630 LINE1 is the line number that the definition absolutely begins on.
11631 LINE2 is the line number that the name of the function appears on.
11632 DECLSPECS and DECLARATOR are the parts of the declaration;
11633 they describe the return type and the name of the function,
11634 but twisted together in a fashion that parallels the syntax of C.
11635
11636 This function creates a binding context for the function body
11637 as well as setting up the FUNCTION_DECL in current_function_decl.
11638
11639 Returns a FUNCTION_DECL on success.
11640
11641 If the DECLARATOR is not suitable for a function (it defines a datum
11642 instead), we return 0, which tells yyparse to report a parse error.
11643
11644 May return void_type_node indicating that this method is actually
11645 a friend. See grokfield for more details.
11646
11647 Came here with a `.pushlevel' .
11648
11649 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
11650 CHANGES TO CODE IN `grokfield'. */
11651 tree
11652 start_method (declspecs, declarator, raises)
11653 tree declarator, declspecs, raises;
11654 {
11655 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0, raises);
11656
11657 /* Something too ugly to handle. */
11658 if (fndecl == NULL_TREE)
11659 return NULL_TREE;
11660
11661 /* Pass friends other than inline friend functions back. */
11662 if (TYPE_MAIN_VARIANT (fndecl) == void_type_node)
11663 return fndecl;
11664
11665 if (TREE_CODE (fndecl) != FUNCTION_DECL)
11666 /* Not a function, tell parser to report parse error. */
11667 return NULL_TREE;
11668
11669 if (IS_SIGNATURE (current_class_type))
11670 {
11671 IS_DEFAULT_IMPLEMENTATION (fndecl) = 1;
11672 /* In case we need this info later. */
11673 HAS_DEFAULT_IMPLEMENTATION (current_class_type) = 1;
11674 }
11675
11676 if (DECL_IN_AGGR_P (fndecl))
11677 {
11678 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
11679 {
11680 if (DECL_CONTEXT (fndecl))
11681 cp_error ("`%D' is already defined in class %s", fndecl,
11682 TYPE_NAME_STRING (DECL_CONTEXT (fndecl)));
11683 }
11684 return void_type_node;
11685 }
11686
11687 if (flag_default_inline)
11688 DECL_INLINE (fndecl) = 1;
11689
11690 if (processing_template_defn)
11691 SET_DECL_IMPLICIT_INSTANTIATION (fndecl);
11692
11693 /* We read in the parameters on the maybepermanent_obstack,
11694 but we won't be getting back to them until after we
11695 may have clobbered them. So the call to preserve_data
11696 will keep them safe. */
11697 preserve_data ();
11698
11699 if (! DECL_FRIEND_P (fndecl))
11700 {
11701 if (DECL_CHAIN (fndecl) != NULL_TREE)
11702 {
11703 /* Need a fresh node here so that we don't get circularity
11704 when we link these together. If FNDECL was a friend, then
11705 `pushdecl' does the right thing, which is nothing wrt its
11706 current value of DECL_CHAIN. */
11707 fndecl = copy_node (fndecl);
11708 }
11709 if (TREE_CHAIN (fndecl))
11710 {
11711 fndecl = copy_node (fndecl);
11712 TREE_CHAIN (fndecl) = NULL_TREE;
11713 }
11714
11715 if (DECL_CONSTRUCTOR_P (fndecl))
11716 {
11717 if (! grok_ctor_properties (current_class_type, fndecl))
11718 return void_type_node;
11719 }
11720 else if (IDENTIFIER_OPNAME_P (DECL_NAME (fndecl)))
11721 grok_op_properties (fndecl, DECL_VIRTUAL_P (fndecl), 0);
11722 }
11723
11724 finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
11725
11726 /* Make a place for the parms */
11727 pushlevel (0);
11728 current_binding_level->parm_flag = 1;
11729
11730 DECL_IN_AGGR_P (fndecl) = 1;
11731 return fndecl;
11732 }
11733
11734 /* Go through the motions of finishing a function definition.
11735 We don't compile this method until after the whole class has
11736 been processed.
11737
11738 FINISH_METHOD must return something that looks as though it
11739 came from GROKFIELD (since we are defining a method, after all).
11740
11741 This is called after parsing the body of the function definition.
11742 STMTS is the chain of statements that makes up the function body.
11743
11744 DECL is the ..._DECL that `start_method' provided. */
11745
11746 tree
11747 finish_method (decl)
11748 tree decl;
11749 {
11750 register tree fndecl = decl;
11751 tree old_initial;
11752
11753 register tree link;
11754
11755 if (TYPE_MAIN_VARIANT (decl) == void_type_node)
11756 return decl;
11757
11758 old_initial = DECL_INITIAL (fndecl);
11759
11760 /* Undo the level for the parms (from start_method).
11761 This is like poplevel, but it causes nothing to be
11762 saved. Saving information here confuses symbol-table
11763 output routines. Besides, this information will
11764 be correctly output when this method is actually
11765 compiled. */
11766
11767 /* Clear out the meanings of the local variables of this level;
11768 also record in each decl which block it belongs to. */
11769
11770 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
11771 {
11772 if (DECL_NAME (link) != NULL_TREE)
11773 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
11774 my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
11775 DECL_CONTEXT (link) = NULL_TREE;
11776 }
11777
11778 /* Restore all name-meanings of the outer levels
11779 that were shadowed by this level. */
11780
11781 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
11782 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
11783 for (link = current_binding_level->class_shadowed;
11784 link; link = TREE_CHAIN (link))
11785 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
11786 for (link = current_binding_level->type_shadowed;
11787 link; link = TREE_CHAIN (link))
11788 IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
11789
11790 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
11791 (HOST_WIDE_INT) current_binding_level->level_chain,
11792 current_binding_level->parm_flag,
11793 current_binding_level->keep,
11794 current_binding_level->tag_transparent);
11795
11796 poplevel (0, 0, 0);
11797
11798 DECL_INITIAL (fndecl) = old_initial;
11799
11800 /* We used to check if the context of FNDECL was different from
11801 current_class_type as another way to get inside here. This didn't work
11802 for String.cc in libg++. */
11803 if (DECL_FRIEND_P (fndecl))
11804 {
11805 CLASSTYPE_INLINE_FRIENDS (current_class_type)
11806 = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
11807 decl = void_type_node;
11808 }
11809
11810 return decl;
11811 }
11812 \f
11813 /* Called when a new struct TYPE is defined.
11814 If this structure or union completes the type of any previous
11815 variable declaration, lay it out and output its rtl. */
11816
11817 void
11818 hack_incomplete_structures (type)
11819 tree type;
11820 {
11821 tree decl;
11822
11823 if (current_binding_level->n_incomplete == 0)
11824 return;
11825
11826 if (!type) /* Don't do this for class templates. */
11827 return;
11828
11829 for (decl = current_binding_level->names; decl; decl = TREE_CHAIN (decl))
11830 if (TREE_TYPE (decl) == type
11831 || (TREE_TYPE (decl)
11832 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
11833 && TREE_TYPE (TREE_TYPE (decl)) == type))
11834 {
11835 if (TREE_CODE (decl) == TYPE_DECL)
11836 layout_type (TREE_TYPE (decl));
11837 else
11838 {
11839 int toplevel = global_binding_level == current_binding_level;
11840 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
11841 && TREE_TYPE (TREE_TYPE (decl)) == type)
11842 layout_type (TREE_TYPE (decl));
11843 layout_decl (decl, 0);
11844 rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0);
11845 if (! toplevel)
11846 {
11847 tree cleanup;
11848 expand_decl (decl);
11849 cleanup = maybe_build_cleanup (decl);
11850 expand_decl_init (decl);
11851 if (! expand_decl_cleanup (decl, cleanup))
11852 cp_error ("parser lost in parsing declaration of `%D'",
11853 decl);
11854 }
11855 }
11856 my_friendly_assert (current_binding_level->n_incomplete > 0, 164);
11857 --current_binding_level->n_incomplete;
11858 }
11859 }
11860
11861 /* Nonzero if presently building a cleanup. Needed because
11862 SAVE_EXPRs are not the right things to use inside of cleanups.
11863 They are only ever evaluated once, where the cleanup
11864 might be evaluated several times. In this case, a later evaluation
11865 of the cleanup might fill in the SAVE_EXPR_RTL, and it will
11866 not be valid for an earlier cleanup. */
11867
11868 int building_cleanup;
11869
11870 /* If DECL is of a type which needs a cleanup, build that cleanup here.
11871 We don't build cleanups if just going for syntax checking, since
11872 fixup_cleanups does not know how to not handle them.
11873
11874 Don't build these on the momentary obstack; they must live
11875 the life of the binding contour. */
11876 tree
11877 maybe_build_cleanup (decl)
11878 tree decl;
11879 {
11880 tree type = TREE_TYPE (decl);
11881 if (TYPE_NEEDS_DESTRUCTOR (type))
11882 {
11883 int temp = 0, flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
11884 tree rval;
11885 int old_building_cleanup = building_cleanup;
11886 building_cleanup = 1;
11887
11888 if (TREE_CODE (decl) != PARM_DECL)
11889 temp = suspend_momentary ();
11890
11891 if (TREE_CODE (type) == ARRAY_TYPE)
11892 rval = decl;
11893 else
11894 {
11895 mark_addressable (decl);
11896 rval = build_unary_op (ADDR_EXPR, decl, 0);
11897 }
11898
11899 /* Optimize for space over speed here. */
11900 if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
11901 || flag_expensive_optimizations)
11902 flags |= LOOKUP_NONVIRTUAL;
11903
11904 /* Use TYPE_MAIN_VARIANT so we don't get a warning about
11905 calling delete on a `const' variable. */
11906 if (TYPE_READONLY (TREE_TYPE (TREE_TYPE (rval))))
11907 rval = build1 (NOP_EXPR, TYPE_POINTER_TO (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (rval)))), rval);
11908
11909 rval = build_delete (TREE_TYPE (rval), rval, integer_two_node, flags, 0);
11910
11911 if (TYPE_USES_VIRTUAL_BASECLASSES (type)
11912 && ! TYPE_HAS_DESTRUCTOR (type))
11913 rval = build_compound_expr (tree_cons (NULL_TREE, rval,
11914 build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
11915
11916 if (TREE_CODE (decl) != PARM_DECL)
11917 resume_momentary (temp);
11918
11919 building_cleanup = old_building_cleanup;
11920
11921 return rval;
11922 }
11923 return 0;
11924 }
11925 \f
11926 /* Expand a C++ expression at the statement level.
11927 This is needed to ferret out nodes which have UNKNOWN_TYPE.
11928 The C++ type checker should get all of these out when
11929 expressions are combined with other, type-providing, expressions,
11930 leaving only orphan expressions, such as:
11931
11932 &class::bar; / / takes its address, but does nothing with it.
11933
11934 */
11935 void
11936 cplus_expand_expr_stmt (exp)
11937 tree exp;
11938 {
11939 if (TREE_TYPE (exp) == unknown_type_node)
11940 {
11941 if (TREE_CODE (exp) == ADDR_EXPR || TREE_CODE (exp) == TREE_LIST)
11942 error ("address of overloaded function with no contextual type information");
11943 else if (TREE_CODE (exp) == COMPONENT_REF)
11944 warning ("useless reference to a member function name, did you forget the ()?");
11945 }
11946 else
11947 {
11948 int remove_implicit_immediately = 0;
11949
11950 if (TREE_CODE (exp) == FUNCTION_DECL)
11951 {
11952 cp_warning ("reference, not call, to function `%D'", exp);
11953 warning ("at this point in file");
11954 }
11955
11956 exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
11957 expand_expr_stmt (break_out_cleanups (exp));
11958 }
11959
11960 /* Clean up any pending cleanups. This happens when a function call
11961 returns a cleanup-needing value that nobody uses. */
11962 expand_cleanups_to (NULL_TREE);
11963 }
11964
11965 /* When a stmt has been parsed, this function is called.
11966
11967 Currently, this function only does something within a
11968 constructor's scope: if a stmt has just assigned to this,
11969 and we are in a derived class, we call `emit_base_init'. */
11970
11971 void
11972 finish_stmt ()
11973 {
11974 extern struct nesting *cond_stack, *loop_stack, *case_stack;
11975
11976
11977 if (current_function_assigns_this
11978 || ! current_function_just_assigned_this)
11979 return;
11980 if (DECL_CONSTRUCTOR_P (current_function_decl))
11981 {
11982 /* Constructors must wait until we are out of control
11983 zones before calling base constructors. */
11984 if (cond_stack || loop_stack || case_stack)
11985 return;
11986 emit_insns (base_init_insns);
11987 check_base_init (current_class_type);
11988 }
11989 current_function_assigns_this = 1;
11990
11991 if (flag_cadillac)
11992 cadillac_finish_stmt ();
11993 }
11994
11995 /* Change a static member function definition into a FUNCTION_TYPE, instead
11996 of the METHOD_TYPE that we create when it's originally parsed.
11997
11998 WARNING: DO NOT pass &TREE_TYPE (decl) to FN or &TYPE_ARG_TYPES
11999 (TREE_TYPE (decl)) to ARGTYPES, as doing so will corrupt the types of
12000 other decls. Either pass the addresses of local variables or NULL. */
12001
12002 void
12003 revert_static_member_fn (decl, fn, argtypes)
12004 tree *decl, *fn, *argtypes;
12005 {
12006 tree tmp;
12007 tree function = fn ? *fn : TREE_TYPE (*decl);
12008 tree args = argtypes ? *argtypes : TYPE_ARG_TYPES (function);
12009
12010 args = TREE_CHAIN (args);
12011 tmp = build_function_type (TREE_TYPE (function), args);
12012 tmp = build_type_variant (tmp, TYPE_READONLY (function),
12013 TYPE_VOLATILE (function));
12014 tmp = build_exception_variant (TYPE_METHOD_BASETYPE (function), tmp,
12015 TYPE_RAISES_EXCEPTIONS (function));
12016 TREE_TYPE (*decl) = tmp;
12017 DECL_STATIC_FUNCTION_P (*decl) = 1;
12018 if (fn)
12019 *fn = tmp;
12020 if (argtypes)
12021 *argtypes = args;
12022 }
12023
12024 int
12025 id_in_current_class (id)
12026 tree id;
12027 {
12028 return !!purpose_member (id, class_binding_level->class_shadowed);
12029 }
This page took 0.564768 seconds and 4 git commands to generate.