OSDN Git Service

86th Cygnus<->FSF quick merge
[pf3gnuchains/gcc-fork.git] / gcc / cp / decl2.c
1 /* Process declarations and variables for C compiler.
2    Copyright (C) 1988, 92, 93, 94, 95, 1996 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 /* Process declarations and symbol lookup for C front end.
24    Also constructs types; the standard scalar types at initialization,
25    and structure, union, array and enum types when they are declared.  */
26
27 /* ??? not all decl nodes are given the most useful possible
28    line numbers.  For example, the CONST_DECLs for enum values.  */
29
30 #include "config.h"
31 #include <stdio.h>
32 #include "tree.h"
33 #include "rtl.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "decl.h"
37 #include "lex.h"
38 #include "output.h"
39
40 #ifndef SUPPORTS_ONE_ONLY
41 #define SUPPORTS_ONE_ONLY 0
42 #endif
43
44 extern tree get_file_function_name ();
45 extern tree cleanups_this_call;
46 static void grok_function_init PROTO((tree, tree));
47 void import_export_decl ();
48 extern int current_class_depth;
49
50 /* A list of virtual function tables we must make sure to write out.  */
51 tree pending_vtables;
52
53 /* A list of static class variables.  This is needed, because a
54    static class variable can be declared inside the class without
55    an initializer, and then initialized, staticly, outside the class.  */
56 tree pending_statics;
57
58 /* A list of functions which were declared inline, but which we
59    may need to emit outline anyway. */
60 static tree saved_inlines;
61
62 /* Used to help generate temporary names which are unique within
63    a function.  Reset to 0 by start_function.  */
64
65 int temp_name_counter;
66
67 /* Same, but not reset.  Local temp variables and global temp variables
68    can have the same name.  */
69 static int global_temp_name_counter;
70
71 /* Flag used when debugging spew.c */
72
73 extern int spew_debug;
74
75 /* Nonzero if we're done parsing and into end-of-file activities.  */
76
77 int at_eof;
78
79 /* Functions called along with real static constructors and destructors.  */
80
81 tree static_ctors, static_dtors;
82 \f
83 /* C (and C++) language-specific option variables.  */
84
85 /* Nonzero means allow type mismatches in conditional expressions;
86    just make their values `void'.   */
87
88 int flag_cond_mismatch;
89
90 /* Nonzero means give `double' the same size as `float'.  */
91
92 int flag_short_double;
93
94 /* Nonzero means don't recognize the keyword `asm'.  */
95
96 int flag_no_asm;
97
98 /* Nonzero means don't recognize any extension keywords.  */
99
100 int flag_no_gnu_keywords;
101
102 /* Nonzero means don't recognize the non-ANSI builtin functions.  */
103
104 int flag_no_builtin;
105
106 /* Nonzero means don't recognize the non-ANSI builtin functions.
107    -ansi sets this.  */
108
109 int flag_no_nonansi_builtin;
110
111 /* Nonzero means do some things the same way PCC does.  */
112
113 int flag_traditional;
114
115 /* Nonzero means to treat bitfields as unsigned unless they say `signed'.  */
116
117 int flag_signed_bitfields = 1;
118
119 /* Nonzero means handle `#ident' directives.  0 means ignore them.  */
120
121 int flag_no_ident;
122
123 /* Nonzero means enable obscure ANSI features and disable GNU extensions
124    that might cause ANSI-compliant code to be miscompiled.  */
125
126 int flag_ansi;
127
128 /* Nonzero means do emit exported implementations of functions even if
129    they can be inlined.  */
130
131 int flag_implement_inlines = 1;
132
133 /* Nonzero means do emit exported implementations of templates, instead of
134    multiple static copies in each file that needs a definition. */
135
136 int flag_external_templates;
137
138 /* Nonzero means that the decision to emit or not emit the implementation of a
139    template depends on where the template is instantiated, rather than where
140    it is defined.  */
141
142 int flag_alt_external_templates;
143
144 /* Nonzero means that implicit instantiations will be emitted if needed.  */
145
146 int flag_implicit_templates = 1;
147
148 /* Nonzero means warn about implicit declarations.  */
149
150 int warn_implicit = 1;
151
152 /* Nonzero means warn when all ctors or dtors are private, and the class
153    has no friends.  */
154
155 int warn_ctor_dtor_privacy = 1;
156
157 /* True if we want to implement vtables using "thunks".
158    The default is off now, but will be on later. */
159
160 int flag_vtable_thunks;
161
162 /* True if we want to deal with repository information.  */
163
164 int flag_use_repository;
165
166 /* Nonzero means give string constants the type `const char *'
167    to get extra warnings from them.  These warnings will be too numerous
168    to be useful, except in thoroughly ANSIfied programs.  */
169
170 int warn_write_strings;
171
172 /* Nonzero means warn about pointer casts that can drop a type qualifier
173    from the pointer target type.  */
174
175 int warn_cast_qual;
176
177 /* Nonzero means warn that dbx info for template class methods isn't fully
178    supported yet.  */
179
180 int warn_template_debugging;
181
182 /* Warn about traditional constructs whose meanings changed in ANSI C.  */
183
184 int warn_traditional;
185
186 /* Nonzero means warn about sizeof(function) or addition/subtraction
187    of function pointers.  */
188
189 int warn_pointer_arith = 1;
190
191 /* Nonzero means warn for non-prototype function decls
192    or non-prototyped defs without previous prototype.  */
193
194 int warn_strict_prototypes;
195
196 /* Nonzero means warn for any function def without prototype decl.  */
197
198 int warn_missing_prototypes;
199
200 /* Nonzero means warn about multiple (redundant) decls for the same single
201    variable or function.  */
202
203 int warn_redundant_decls;
204
205 /* Warn if initializer is not completely bracketed.  */
206
207 int warn_missing_braces;
208
209 /* Warn about comparison of signed and unsigned values.  */
210
211 int warn_sign_compare;
212
213 /* Warn about *printf or *scanf format/argument anomalies. */
214
215 int warn_format;
216
217 /* Warn about a subscript that has type char.  */
218
219 int warn_char_subscripts;
220
221 /* Warn if a type conversion is done that might have confusing results.  */
222
223 int warn_conversion;
224
225 /* Warn if adding () is suggested.  */
226
227 int warn_parentheses;
228
229 /* Non-zero means warn in function declared in derived class has the
230    same name as a virtual in the base class, but fails to match the
231    type signature of any virtual function in the base class.  */
232 int warn_overloaded_virtual;
233
234 /* Non-zero means warn when declaring a class that has a non virtual
235    destructor, when it really ought to have a virtual one. */
236 int warn_nonvdtor;
237
238 /* Non-zero means warn when a function is declared extern and later inline.  */
239 int warn_extern_inline;
240
241 /* Non-zero means warn when the compiler will reorder code.  */
242 int warn_reorder;
243
244 /* Non-zero means warn when synthesis behavior differs from Cfront's.  */
245 int warn_synth;
246
247 /* Nonzero means `$' can be in an identifier.
248    See cccp.c for reasons why this breaks some obscure ANSI C programs.  */
249
250 #ifndef DOLLARS_IN_IDENTIFIERS
251 #define DOLLARS_IN_IDENTIFIERS 1
252 #endif
253 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
254
255 /* Nonzero for -fno-strict-prototype switch: do not consider empty
256    argument prototype to mean function takes no arguments.  */
257
258 int flag_strict_prototype = 2;
259 int strict_prototype = 1;
260 int strict_prototypes_lang_c, strict_prototypes_lang_cplusplus = 1;
261
262 /* Nonzero means that labels can be used as first-class objects */
263
264 int flag_labels_ok;
265
266 /* Non-zero means to collect statistics which might be expensive
267    and to print them when we are done.  */
268 int flag_detailed_statistics;
269
270 /* C++ specific flags.  */   
271 /* Nonzero for -fall-virtual: make every member function (except
272    constructors) lay down in the virtual function table.  Calls
273    can then either go through the virtual function table or not,
274    depending.  */
275
276 int flag_all_virtual;
277
278 /* Zero means that `this' is a *const.  This gives nice behavior in the
279    2.0 world.  1 gives 1.2-compatible behavior.  2 gives Spring behavior.
280    -2 means we're constructing an object and it has fixed type.  */
281
282 int flag_this_is_variable;
283
284 /* Nonzero means memoize our member lookups.  */
285
286 int flag_memoize_lookups; int flag_save_memoized_contexts;
287
288 /* 3 means write out only virtuals function tables `defined'
289    in this implementation file.
290    2 means write out only specific virtual function tables
291    and give them (C) public access.
292    1 means write out virtual function tables and give them
293    (C) public access.
294    0 means write out virtual function tables and give them
295    (C) static access (default).
296    -1 means declare virtual function tables extern.  */
297
298 int write_virtuals;
299
300 /* Nonzero means we should attempt to elide constructors when possible.  */
301
302 int flag_elide_constructors;
303
304 /* Nonzero means recognize and handle exception handling constructs.
305    Use ansi syntax and semantics.  WORK IN PROGRESS!  */
306
307 int flag_handle_exceptions;
308
309 /* Nonzero means recognize and handle signature language constructs.  */
310
311 int flag_handle_signatures;
312
313 /* Nonzero means that member functions defined in class scope are
314    inline by default.  */
315
316 int flag_default_inline = 1;
317
318 /* Controls whether enums and ints freely convert.
319    1 means with complete freedom.
320    0 means enums can convert to ints, but not vice-versa.  */
321 int flag_int_enum_equivalence;
322
323 /* Controls whether compiler generates 'type descriptor' that give
324    run-time type information.  */
325 int flag_rtti = 1;
326
327 /* Nonzero if we wish to output cross-referencing information
328    for the GNU class browser.  */
329 extern int flag_gnu_xref;
330
331 /* Nonzero if compiler can make `reasonable' assumptions about
332    references and objects.  For example, the compiler must be
333    conservative about the following and not assume that `a' is nonnull:
334
335    obj &a = g ();
336    a.f (2);
337
338    In general, it is `reasonable' to assume that for many programs,
339    and better code can be generated in that case.  */
340
341 int flag_assume_nonnull_objects = 1;
342
343 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
344    objects. */
345
346 int flag_huge_objects;
347
348 /* Nonzero if we want to conserve space in the .o files.  We do this
349    by putting uninitialized data and runtime initialized data into
350    .common instead of .data at the expense of not flagging multiple
351    definitions.  */
352
353 int flag_conserve_space;
354
355 /* Nonzero if we want to obey access control semantics.  */
356
357 int flag_access_control = 1;
358
359 /* Nonzero if we want to understand the operator names, i.e. 'bitand'.  */
360
361 int flag_operator_names;
362
363 /* Nonzero if we want to check the return value of new and avoid calling
364    constructors if it is a null pointer.  */
365
366 int flag_check_new;
367
368 /* Nonzero if we want the new ANSI rules for pushing a new scope for `for'
369    initialization variables.
370    0: Old rules, set by -fno-for-scope.
371    2: New ANSI rules, set by -ffor-scope.
372    1: Try to implement new ANSI rules, but with backup compatibility
373    (and warnings).  This is the default, for now.  */
374
375 int flag_new_for_scope = 1;
376
377 /* Nonzero if we want to emit defined symbols with common-like linkage as
378    weak symbols where possible, in order to conform to C++ semantics.
379    Otherwise, emit them as local symbols.  */
380
381 int flag_weak = 1;
382
383 /* Table of language-dependent -f options.
384    STRING is the option name.  VARIABLE is the address of the variable.
385    ON_VALUE is the value to store in VARIABLE
386     if `-fSTRING' is seen as an option.
387    (If `-fno-STRING' is seen as an option, the opposite value is stored.)  */
388
389 static struct { char *string; int *variable; int on_value;} lang_f_options[] =
390 {
391   {"signed-char", &flag_signed_char, 1},
392   {"unsigned-char", &flag_signed_char, 0},
393   {"signed-bitfields", &flag_signed_bitfields, 1},
394   {"unsigned-bitfields", &flag_signed_bitfields, 0},
395   {"short-enums", &flag_short_enums, 1},
396   {"short-double", &flag_short_double, 1},
397   {"cond-mismatch", &flag_cond_mismatch, 1},
398   {"asm", &flag_no_asm, 0},
399   {"builtin", &flag_no_builtin, 0},
400   {"ident", &flag_no_ident, 0},
401   {"labels-ok", &flag_labels_ok, 1},
402   {"stats", &flag_detailed_statistics, 1},
403   {"this-is-variable", &flag_this_is_variable, 1},
404   {"strict-prototype", &flag_strict_prototype, 1},
405   {"all-virtual", &flag_all_virtual, 1},
406   {"memoize-lookups", &flag_memoize_lookups, 1},
407   {"elide-constructors", &flag_elide_constructors, 1},
408   {"handle-exceptions", &flag_handle_exceptions, 1},
409   {"handle-signatures", &flag_handle_signatures, 1},
410   {"default-inline", &flag_default_inline, 1},
411   {"dollars-in-identifiers", &dollars_in_ident, 1},
412   {"enum-int-equiv", &flag_int_enum_equivalence, 1},
413   {"rtti", &flag_rtti, 1},
414   {"xref", &flag_gnu_xref, 1},
415   {"nonnull-objects", &flag_assume_nonnull_objects, 1},
416   {"implement-inlines", &flag_implement_inlines, 1},
417   {"external-templates", &flag_external_templates, 1},
418   {"implicit-templates", &flag_implicit_templates, 1},
419   {"huge-objects", &flag_huge_objects, 1},
420   {"conserve-space", &flag_conserve_space, 1},
421   {"vtable-thunks", &flag_vtable_thunks, 1},
422   {"short-temps", &flag_short_temps, 1},
423   {"access-control", &flag_access_control, 1},
424   {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
425   {"gnu-keywords", &flag_no_gnu_keywords, 0},
426   {"operator-names", &flag_operator_names, 1},
427   {"check-new", &flag_check_new, 1},
428   {"repo", &flag_use_repository, 1},
429   {"for-scope", &flag_new_for_scope, 2},
430   {"weak", &flag_weak, 1}
431 };
432
433 /* Decode the string P as a language-specific option.
434    Return 1 if it is recognized (and handle it);
435    return 0 if not recognized.  */
436
437 int   
438 lang_decode_option (p)
439      char *p;
440 {
441   if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
442     flag_traditional = 1, dollars_in_ident = 1, flag_writable_strings = 1,
443     flag_this_is_variable = 1, flag_new_for_scope = 0;
444   /* The +e options are for cfront compatibility.  They come in as
445      `-+eN', to kludge around gcc.c's argument handling.  */
446   else if (p[0] == '-' && p[1] == '+' && p[2] == 'e')
447     {
448       int old_write_virtuals = write_virtuals;
449       if (p[3] == '1')
450         write_virtuals = 1;
451       else if (p[3] == '0')
452         write_virtuals = -1;
453       else if (p[3] == '2')
454         write_virtuals = 2;
455       else error ("invalid +e option");
456       if (old_write_virtuals != 0
457           && write_virtuals != old_write_virtuals)
458         error ("conflicting +e options given");
459     }
460   else if (p[0] == '-' && p[1] == 'f')
461     {
462       /* Some kind of -f option.
463          P's value is the option sans `-f'.
464          Search for it in the table of options.  */
465       int found = 0, j;
466
467       p += 2;
468       /* Try special -f options.  */
469
470       if (!strcmp (p, "save-memoized"))
471         {
472           flag_memoize_lookups = 1;
473           flag_save_memoized_contexts = 1;
474           found = 1;
475         }
476       if (!strcmp (p, "no-save-memoized"))
477         {
478           flag_memoize_lookups = 0;
479           flag_save_memoized_contexts = 0;
480           found = 1;
481         }
482       else if (! strcmp (p, "alt-external-templates"))
483         {
484           flag_external_templates = 1;
485           flag_alt_external_templates = 1;
486           found = 1;
487         }
488       else if (! strcmp (p, "no-alt-external-templates"))
489         {
490           flag_alt_external_templates = 0;
491           found = 1;
492         }
493       else if (!strcmp (p, "ansi-overloading"))
494         {
495           warning ("-fansi-overloading is no longer meaningful");
496           found = 1;
497         }
498       else if (!strcmp (p, "repo"))
499         {
500           flag_use_repository = 1;
501           flag_implicit_templates = 0;
502           found = 1;
503         }
504       else for (j = 0;
505                 !found && j < sizeof (lang_f_options) / sizeof (lang_f_options[0]);
506                 j++)
507         {
508           if (!strcmp (p, lang_f_options[j].string))
509             {
510               *lang_f_options[j].variable = lang_f_options[j].on_value;
511               /* A goto here would be cleaner,
512                  but breaks the vax pcc.  */
513               found = 1;
514             }
515           if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
516               && ! strcmp (p+3, lang_f_options[j].string))
517             {
518               *lang_f_options[j].variable = ! lang_f_options[j].on_value;
519               found = 1;
520             }
521         }
522       return found;
523     }
524   else if (p[0] == '-' && p[1] == 'W')
525     {
526       int setting = 1;
527
528       /* The -W options control the warning behavior of the compiler.  */
529       p += 2;
530
531       if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
532         setting = 0, p += 3;
533
534       if (!strcmp (p, "implicit"))
535         warn_implicit = setting;
536       else if (!strcmp (p, "return-type"))
537         warn_return_type = setting;
538       else if (!strcmp (p, "ctor-dtor-privacy"))
539         warn_ctor_dtor_privacy = setting;
540       else if (!strcmp (p, "write-strings"))
541         warn_write_strings = setting;
542       else if (!strcmp (p, "cast-qual"))
543         warn_cast_qual = setting;
544       else if (!strcmp (p, "traditional"))
545         warn_traditional = setting;
546       else if (!strcmp (p, "char-subscripts"))
547         warn_char_subscripts = setting;
548       else if (!strcmp (p, "pointer-arith"))
549         warn_pointer_arith = setting;
550       else if (!strcmp (p, "strict-prototypes"))
551         warn_strict_prototypes = setting;
552       else if (!strcmp (p, "missing-prototypes"))
553         warn_missing_prototypes = setting;
554       else if (!strcmp (p, "redundant-decls"))
555         warn_redundant_decls = setting;
556       else if (!strcmp (p, "missing-braces"))
557         warn_missing_braces = setting;
558       else if (!strcmp (p, "sign-compare"))
559         warn_sign_compare = setting;
560       else if (!strcmp (p, "format"))
561         warn_format = setting;
562       else if (!strcmp (p, "conversion"))
563         warn_conversion = setting;
564       else if (!strcmp (p, "parentheses"))
565         warn_parentheses = setting;
566       else if (!strcmp (p, "non-virtual-dtor"))
567         warn_nonvdtor = setting;
568       else if (!strcmp (p, "extern-inline"))
569         warn_extern_inline = setting;
570       else if (!strcmp (p, "reorder"))
571         warn_reorder = setting;
572       else if (!strcmp (p, "synth"))
573         warn_synth = setting;
574       else if (!strcmp (p, "comment"))
575         ;                       /* cpp handles this one.  */
576       else if (!strcmp (p, "comments"))
577         ;                       /* cpp handles this one.  */
578       else if (!strcmp (p, "trigraphs"))
579         ;                       /* cpp handles this one.  */
580       else if (!strcmp (p, "import"))
581         ;                       /* cpp handles this one.  */
582       else if (!strcmp (p, "all"))
583         {
584           warn_return_type = setting;
585           warn_unused = setting;
586           warn_implicit = setting;
587           warn_ctor_dtor_privacy = setting;
588           warn_switch = setting;
589           warn_format = setting;
590           warn_parentheses = setting;
591           warn_missing_braces = setting;
592           warn_sign_compare = setting;
593           warn_extern_inline = setting;
594           warn_nonvdtor = setting;
595           /* We save the value of warn_uninitialized, since if they put
596              -Wuninitialized on the command line, we need to generate a
597              warning about not using it without also specifying -O.  */
598           if (warn_uninitialized != 1)
599             warn_uninitialized = (setting ? 2 : 0);
600           warn_template_debugging = setting;
601           warn_reorder = setting;
602         }
603
604       else if (!strcmp (p, "overloaded-virtual"))
605         warn_overloaded_virtual = setting;
606       else return 0;
607     }
608   else if (!strcmp (p, "-ansi"))
609     dollars_in_ident = 0, flag_no_nonansi_builtin = 1, flag_ansi = 1,
610     flag_no_gnu_keywords = 1, flag_operator_names = 1;
611 #ifdef SPEW_DEBUG
612   /* Undocumented, only ever used when you're invoking cc1plus by hand, since
613      it's probably safe to assume no sane person would ever want to use this
614      under normal circumstances.  */
615   else if (!strcmp (p, "-spew-debug"))
616     spew_debug = 1;
617 #endif
618   else
619     return 0;
620
621   return 1;
622 }
623 \f
624 /* Incorporate `const' and `volatile' qualifiers for member functions.
625    FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
626    QUALS is a list of qualifiers.  */
627 tree
628 grok_method_quals (ctype, function, quals)
629      tree ctype, function, quals;
630 {
631   tree fntype = TREE_TYPE (function);
632   tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
633
634   do
635     {
636       extern tree ridpointers[];
637
638       if (TREE_VALUE (quals) == ridpointers[(int)RID_CONST])
639         {
640           if (TYPE_READONLY (ctype))
641             error ("duplicate `%s' %s",
642                    IDENTIFIER_POINTER (TREE_VALUE (quals)),
643                    (TREE_CODE (function) == FUNCTION_DECL
644                     ? "for member function" : "in type declaration"));
645           ctype = build_type_variant (ctype, 1, TYPE_VOLATILE (ctype));
646           build_pointer_type (ctype);
647         }
648       else if (TREE_VALUE (quals) == ridpointers[(int)RID_VOLATILE])
649         {
650           if (TYPE_VOLATILE (ctype))
651             error ("duplicate `%s' %s",
652                    IDENTIFIER_POINTER (TREE_VALUE (quals)),
653                    (TREE_CODE (function) == FUNCTION_DECL
654                     ? "for member function" : "in type declaration"));
655           ctype = build_type_variant (ctype, TYPE_READONLY (ctype), 1);
656           build_pointer_type (ctype);
657         }
658       else
659         my_friendly_abort (20);
660       quals = TREE_CHAIN (quals);
661     }
662   while (quals);
663   fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
664                                     (TREE_CODE (fntype) == METHOD_TYPE
665                                      ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
666                                      : TYPE_ARG_TYPES (fntype)));
667   if (raises)
668     fntype = build_exception_variant (fntype, raises);
669
670   TREE_TYPE (function) = fntype;
671   return ctype;
672 }
673
674 #if 0                           /* Not used. */
675 /* This routine replaces cryptic DECL_NAMEs with readable DECL_NAMEs.
676    It leaves DECL_ASSEMBLER_NAMEs with the correct value.  */
677 /* This does not yet work with user defined conversion operators
678    It should.  */
679 static void
680 substitute_nice_name (decl)
681      tree decl;
682 {
683   if (DECL_NAME (decl) && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
684     {
685       char *n = decl_as_string (DECL_NAME (decl), 1);
686       if (n[strlen (n) - 1] == ' ')
687         n[strlen (n) - 1] = 0;
688       DECL_NAME (decl) = get_identifier (n);
689     }
690 }
691 #endif
692
693 /* Warn when -fexternal-templates is used and #pragma
694    interface/implementation is not used all the times it should be,
695    inform the user.  */
696 void
697 warn_if_unknown_interface (decl)
698      tree decl;
699 {
700   static int already_warned = 0;
701   if (already_warned++)
702     return;
703
704   if (flag_alt_external_templates)
705     {
706       struct tinst_level *til = tinst_for_decl ();
707       int sl = lineno;
708       char *sf = input_filename;
709
710       if (til)
711         {
712           lineno = til->line;
713           input_filename = til->file;
714         }
715       cp_warning ("template `%#D' instantiated in file without #pragma interface",
716                   decl);
717       lineno = sl;
718       input_filename = sf;
719     }
720   else
721     cp_warning_at ("template `%#D' defined in file without #pragma interface",
722                    decl);
723 }
724
725 /* A subroutine of the parser, to handle a component list.  */
726 tree
727 grok_x_components (specs, components)
728      tree specs, components;
729 {
730   register tree t, x, tcode;
731
732   /* We just got some friends.  They have been recorded elsewhere.  */
733   if (components == void_type_node)
734     return NULL_TREE;
735
736   if (components == NULL_TREE)
737     {
738       t = groktypename (build_decl_list (specs, NULL_TREE));
739
740       if (t == NULL_TREE)
741         {
742           error ("error in component specification");
743           return NULL_TREE;
744         }
745
746       switch (TREE_CODE (t))
747         {
748         case VAR_DECL:
749           /* Static anonymous unions come out as VAR_DECLs.  */
750           if (TREE_CODE (TREE_TYPE (t)) == UNION_TYPE
751               && ANON_AGGRNAME_P (TYPE_IDENTIFIER (TREE_TYPE (t))))
752             return t;
753
754           /* We return SPECS here, because in the parser it was ending
755              up with not doing anything to $$, which is what SPECS
756              represents.  */
757           return specs;
758           break;
759
760         case RECORD_TYPE:
761           /* This code may be needed for UNION_TYPEs as
762              well.  */
763           tcode = record_type_node;
764           if (CLASSTYPE_DECLARED_CLASS (t))
765             tcode = class_type_node;
766           else if (IS_SIGNATURE (t))
767             tcode = signature_type_node;
768           
769           t = xref_tag (tcode, TYPE_IDENTIFIER (t), NULL_TREE, 0);
770           if (TYPE_CONTEXT (t))
771             CLASSTYPE_NO_GLOBALIZE (t) = 1;
772           return NULL_TREE;
773           break;
774
775         case UNION_TYPE:
776         case ENUMERAL_TYPE:
777           if (TREE_CODE (t) == UNION_TYPE)
778             tcode = union_type_node;
779           else
780             tcode = enum_type_node;
781
782           t = xref_tag (tcode, TYPE_IDENTIFIER (t), NULL_TREE, 0);
783           if (TREE_CODE (t) == UNION_TYPE && TYPE_CONTEXT (t))
784             CLASSTYPE_NO_GLOBALIZE (t) = 1;
785           if (TREE_CODE (t) == UNION_TYPE
786               && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
787             {
788               struct pending_inline **p;
789               x = build_lang_field_decl (FIELD_DECL, NULL_TREE, t);
790
791               /* Wipe out memory of synthesized methods */
792               TYPE_HAS_CONSTRUCTOR (t) = 0;
793               TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
794               TYPE_HAS_INIT_REF (t) = 0;
795               TYPE_HAS_CONST_INIT_REF (t) = 0;
796               TYPE_HAS_ASSIGN_REF (t) = 0;
797               TYPE_HAS_ASSIGNMENT (t) = 0;
798               TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
799
800               p = &pending_inlines;
801               for (; *p; *p = (*p)->next)
802                 if (DECL_CONTEXT ((*p)->fndecl) != t)
803                   break;
804             }
805           else if (TREE_CODE (t) == ENUMERAL_TYPE)
806             x = grok_enum_decls (t, NULL_TREE);
807           else
808             x = NULL_TREE;
809           return x;
810           break;
811
812         default:
813           if (t != void_type_node)
814             error ("empty component declaration");
815           return NULL_TREE;
816         }
817     }
818   else
819     {
820       t = TREE_TYPE (components);
821       if (TREE_CODE (t) == ENUMERAL_TYPE && TREE_NONLOCAL_FLAG (t))
822         return grok_enum_decls (t, components);
823       else
824         return components;
825     }
826 }
827
828 /* Classes overload their constituent function names automatically.
829    When a function name is declared in a record structure,
830    its name is changed to it overloaded name.  Since names for
831    constructors and destructors can conflict, we place a leading
832    '$' for destructors.
833
834    CNAME is the name of the class we are grokking for.
835
836    FUNCTION is a FUNCTION_DECL.  It was created by `grokdeclarator'.
837
838    FLAGS contains bits saying what's special about today's
839    arguments.  1 == DESTRUCTOR.  2 == OPERATOR.
840
841    If FUNCTION is a destructor, then we must add the `auto-delete' field
842    as a second parameter.  There is some hair associated with the fact
843    that we must "declare" this variable in the manner consistent with the
844    way the rest of the arguments were declared.
845
846    QUALS are the qualifiers for the this pointer.  */
847
848 void
849 grokclassfn (ctype, cname, function, flags, quals)
850      tree ctype, cname, function;
851      enum overload_flags flags;
852      tree quals;
853 {
854   tree fn_name = DECL_NAME (function);
855   tree arg_types;
856   tree parm;
857   tree qualtype;
858   tree fntype = TREE_TYPE (function);
859   tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
860
861   if (fn_name == NULL_TREE)
862     {
863       error ("name missing for member function");
864       fn_name = get_identifier ("<anonymous>");
865       DECL_NAME (function) = fn_name;
866     }
867
868   if (quals)
869     qualtype = grok_method_quals (ctype, function, quals);
870   else
871     qualtype = ctype;
872
873   arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
874   if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
875     {
876       /* Must add the class instance variable up front.  */
877       /* Right now we just make this a pointer.  But later
878          we may wish to make it special.  */
879       tree type = TREE_VALUE (arg_types);
880       int constp = 1;
881
882       if ((flag_this_is_variable > 0)
883           && (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function)))
884         constp = 0;
885
886       if (DECL_CONSTRUCTOR_P (function))
887         {
888           if (TYPE_USES_VIRTUAL_BASECLASSES (ctype))
889             {
890               DECL_CONSTRUCTOR_FOR_VBASE_P (function) = 1;
891               /* In this case we need "in-charge" flag saying whether
892                  this constructor is responsible for initialization
893                  of virtual baseclasses or not.  */
894               parm = build_decl (PARM_DECL, in_charge_identifier, integer_type_node);
895               /* Mark the artificial `__in_chrg' parameter as "artificial".  */
896               SET_DECL_ARTIFICIAL (parm);
897               DECL_ARG_TYPE (parm) = integer_type_node;
898               TREE_READONLY (parm) = 1;
899               TREE_CHAIN (parm) = last_function_parms;
900               last_function_parms = parm;
901             }
902         }
903
904       parm = build_decl (PARM_DECL, this_identifier, type);
905       /* Mark the artificial `this' parameter as "artificial".  */
906       SET_DECL_ARTIFICIAL (parm);
907       DECL_ARG_TYPE (parm) = type;
908       /* We can make this a register, so long as we don't
909          accidentally complain if someone tries to take its address.  */
910       DECL_REGISTER (parm) = 1;
911       if (constp)
912         TREE_READONLY (parm) = 1;
913       TREE_CHAIN (parm) = last_function_parms;
914       last_function_parms = parm;
915     }
916
917   if (flags == DTOR_FLAG)
918     {
919       char *buf, *dbuf;
920       tree const_integer_type = build_type_variant (integer_type_node, 1, 0);
921       int len = sizeof (DESTRUCTOR_DECL_PREFIX)-1;
922
923       arg_types = hash_tree_chain (const_integer_type, void_list_node);
924       TREE_SIDE_EFFECTS (arg_types) = 1;
925       /* Build the overload name.  It will look like `7Example'.  */
926       if (IDENTIFIER_TYPE_VALUE (cname))
927         dbuf = build_overload_name (IDENTIFIER_TYPE_VALUE (cname), 1, 1);
928       else if (IDENTIFIER_LOCAL_VALUE (cname))
929         dbuf = build_overload_name (TREE_TYPE (IDENTIFIER_LOCAL_VALUE (cname)), 1, 1);
930       else
931       /* Using ctype fixes the `X::Y::~Y()' crash.  The cname has no type when
932          it's defined out of the class definition, since poplevel_class wipes
933          it out.  This used to be internal error 346.  */
934         dbuf = build_overload_name (ctype, 1, 1);
935       buf = (char *) alloca (strlen (dbuf) + sizeof (DESTRUCTOR_DECL_PREFIX));
936       bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
937       buf[len] = '\0';
938       strcat (buf, dbuf);
939       DECL_ASSEMBLER_NAME (function) = get_identifier (buf);
940       parm = build_decl (PARM_DECL, in_charge_identifier, integer_type_node);
941       /* Mark the artificial `__in_chrg' parameter as "artificial".  */
942       SET_DECL_ARTIFICIAL (parm);
943       TREE_READONLY (parm) = 1;
944       DECL_ARG_TYPE (parm) = integer_type_node;
945       /* This is the same chain as DECL_ARGUMENTS (...).  */
946       TREE_CHAIN (last_function_parms) = parm;
947
948       fntype = build_cplus_method_type (qualtype, void_type_node,
949                                         arg_types);
950       if (raises)
951         {
952           fntype = build_exception_variant (fntype, raises);
953         }
954       TREE_TYPE (function) = fntype;
955       TYPE_HAS_DESTRUCTOR (ctype) = 1;
956     }
957   else
958     {
959       tree these_arg_types;
960
961       if (DECL_CONSTRUCTOR_FOR_VBASE_P (function))
962         {
963           arg_types = hash_tree_chain (integer_type_node,
964                                        TREE_CHAIN (arg_types));
965           fntype = build_cplus_method_type (qualtype,
966                                             TREE_TYPE (TREE_TYPE (function)),
967                                             arg_types);
968           if (raises)
969             {
970               fntype = build_exception_variant (fntype, raises);
971             }
972           TREE_TYPE (function) = fntype;
973           arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
974         }
975
976       these_arg_types = arg_types;
977
978       if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
979         /* Only true for static member functions.  */
980         these_arg_types = hash_tree_chain (build_pointer_type (qualtype),
981                                            arg_types);
982
983       DECL_ASSEMBLER_NAME (function)
984         = build_decl_overload (fn_name, these_arg_types,
985                                1 + DECL_CONSTRUCTOR_P (function));
986
987 #if 0
988       /* This code is going into the compiler, but currently, it makes
989          libg++/src/Integer.cc not compile.  The problem is that the nice name
990          winds up going into the symbol table, and conversion operations look
991          for the manged name.  */
992       substitute_nice_name (function);
993 #endif
994     }
995
996   DECL_ARGUMENTS (function) = last_function_parms;
997   /* First approximations.  */
998   DECL_CONTEXT (function) = ctype;
999   DECL_CLASS_CONTEXT (function) = ctype;
1000 }
1001
1002 /* Work on the expr used by alignof (this is only called by the parser).  */
1003 tree
1004 grok_alignof (expr)
1005      tree expr;
1006 {
1007   tree best, t;
1008   int bestalign;
1009
1010   if (TREE_CODE (expr) == COMPONENT_REF
1011       && DECL_BIT_FIELD (TREE_OPERAND (expr, 1)))
1012     error ("`__alignof__' applied to a bit-field");
1013
1014   if (TREE_CODE (expr) == INDIRECT_REF)
1015     {
1016       best = t = TREE_OPERAND (expr, 0);
1017       bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1018
1019       while (TREE_CODE (t) == NOP_EXPR
1020              && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
1021         {
1022           int thisalign;
1023           t = TREE_OPERAND (t, 0);
1024           thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1025           if (thisalign > bestalign)
1026             best = t, bestalign = thisalign;
1027         }
1028       return c_alignof (TREE_TYPE (TREE_TYPE (best)));
1029     }
1030   else
1031     {
1032       /* ANSI says arrays and fns are converted inside comma.
1033          But we can't convert them in build_compound_expr
1034          because that would break commas in lvalues.
1035          So do the conversion here if operand was a comma.  */
1036       if (TREE_CODE (expr) == COMPOUND_EXPR
1037           && (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
1038               || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
1039         expr = default_conversion (expr);
1040       return c_alignof (TREE_TYPE (expr));
1041     }
1042 }
1043
1044 /* Create an ARRAY_REF, checking for the user doing things backwards
1045    along the way.  */
1046 tree
1047 grok_array_decl (array_expr, index_exp)
1048      tree array_expr, index_exp;
1049 {
1050   tree type = TREE_TYPE (array_expr);
1051   tree p1, p2, i1, i2;
1052
1053   if (type == error_mark_node || index_exp == error_mark_node)
1054     return error_mark_node;
1055   if (current_template_parms)
1056     return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1057                       array_expr, index_exp);
1058
1059   if (type == NULL_TREE)
1060     {
1061       /* Something has gone very wrong.  Assume we are mistakenly reducing
1062          an expression instead of a declaration.  */
1063       error ("parser may be lost: is there a '{' missing somewhere?");
1064       return NULL_TREE;
1065     }
1066
1067   if (TREE_CODE (type) == OFFSET_TYPE
1068       || TREE_CODE (type) == REFERENCE_TYPE)
1069     type = TREE_TYPE (type);
1070
1071   /* If they have an `operator[]', use that.  */
1072   if (TYPE_LANG_SPECIFIC (type)
1073       && TYPE_OVERLOADS_ARRAY_REF (complete_type (type)))
1074     return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1075                            array_expr, index_exp, NULL_TREE);
1076
1077   /* Otherwise, create an ARRAY_REF for a pointer or array type.  */
1078
1079   if (TREE_CODE (type) == ARRAY_TYPE)
1080     p1 = array_expr;
1081   else
1082     p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1083
1084   if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1085     p2 = index_exp;
1086   else
1087     p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1088
1089   i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1090   i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1091
1092   if ((p1 && i2) && (i1 && p2))
1093     error ("ambiguous conversion for array subscript");
1094
1095   if (p1 && i2)
1096     array_expr = p1, index_exp = i2;
1097   else if (i1 && p2)
1098     array_expr = p2, index_exp = i1;
1099   else
1100     {
1101       cp_error ("invalid types `%T[%T]' for array subscript",
1102                 type, TREE_TYPE (index_exp));
1103       return error_mark_node;
1104     }
1105
1106   if (array_expr == error_mark_node || index_exp == error_mark_node)
1107     error ("ambiguous conversion for array subscript");
1108
1109   return build_array_ref (array_expr, index_exp);
1110 }
1111
1112 /* Given the cast expression EXP, checking out its validity.   Either return
1113    an error_mark_node if there was an unavoidable error, return a cast to
1114    void for trying to delete a pointer w/ the value 0, or return the
1115    call to delete.  If DOING_VEC is 1, we handle things differently
1116    for doing an array delete.  If DOING_VEC is 2, they gave us the
1117    array size as an argument to delete.
1118    Implements ARM $5.3.4.  This is called from the parser.  */
1119 tree
1120 delete_sanity (exp, size, doing_vec, use_global_delete)
1121      tree exp, size;
1122      int doing_vec, use_global_delete;
1123 {
1124   tree t;
1125   tree type;
1126   enum tree_code code;
1127   /* For a regular vector delete (aka, no size argument) we will pass
1128      this down as a NULL_TREE into build_vec_delete.  */
1129   tree maxindex = NULL_TREE;
1130
1131   if (exp == error_mark_node)
1132     return exp;
1133
1134   if (current_template_parms)
1135     {
1136       t = build_min (DELETE_EXPR, void_type_node, exp, size);
1137       DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1138       DELETE_EXPR_USE_VEC (t) = doing_vec;
1139       return t;
1140     }
1141
1142   t = stabilize_reference (convert_from_reference (exp));
1143   type = TREE_TYPE (t);
1144   code = TREE_CODE (type);
1145
1146   switch (doing_vec)
1147     {
1148     case 2:
1149       maxindex = build_binary_op (MINUS_EXPR, size, integer_one_node, 1);
1150       if (! flag_traditional)
1151         pedwarn ("anachronistic use of array size in vector delete");
1152       /* Fall through.  */
1153     case 1:
1154       break;
1155     default:
1156       if (code != POINTER_TYPE)
1157         {
1158           cp_error ("type `%#T' argument given to `delete', expected pointer",
1159                     type);
1160           return error_mark_node;
1161         }
1162
1163       /* Deleting a pointer with the value zero is valid and has no effect.  */
1164       if (integer_zerop (t))
1165         return build1 (NOP_EXPR, void_type_node, t);
1166     }
1167
1168   if (code == POINTER_TYPE)
1169     {
1170 #if 0
1171       /* As of Valley Forge, you can delete a pointer to constant.  */
1172       /* You can't delete a pointer to constant.  */
1173       if (TREE_READONLY (TREE_TYPE (type)))
1174         {
1175           error ("`const *' cannot be deleted");
1176           return error_mark_node;
1177         }
1178 #endif
1179       /* You also can't delete functions.  */
1180       if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1181         {
1182           error ("cannot delete a function");
1183           return error_mark_node;
1184         }
1185     }
1186
1187 #if 0
1188   /* If the type has no destructor, then we should build a regular
1189      delete, instead of a vector delete.  Otherwise, we would end
1190      up passing a bogus offset into __builtin_delete, which is
1191      not expecting it.  */ 
1192   if (doing_vec
1193       && TREE_CODE (type) == POINTER_TYPE
1194       && !TYPE_HAS_DESTRUCTOR (TREE_TYPE (type)))
1195     {
1196       doing_vec = 0;
1197       use_global_delete = 1;
1198     }
1199 #endif
1200
1201   if (doing_vec)
1202     return build_vec_delete (t, maxindex, integer_one_node,
1203                              integer_two_node, use_global_delete);
1204   else
1205     {
1206       if (IS_AGGR_TYPE (TREE_TYPE (type))
1207           && TYPE_GETS_REG_DELETE (TREE_TYPE (type)))
1208         {
1209           /* Only do access checking here; we'll be calling op delete
1210              from the destructor.  */
1211           tree tmp = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, t,
1212                                      size_zero_node, NULL_TREE);
1213           if (tmp == error_mark_node)
1214             return error_mark_node;
1215         }
1216
1217       return build_delete (type, t, integer_three_node,
1218                            LOOKUP_NORMAL|LOOKUP_HAS_IN_CHARGE,
1219                            use_global_delete);
1220     }
1221 }
1222
1223 /* Sanity check: report error if this function FUNCTION is not
1224    really a member of the class (CTYPE) it is supposed to belong to.
1225    CNAME is the same here as it is for grokclassfn above.  */
1226
1227 tree
1228 check_classfn (ctype, function)
1229      tree ctype, function;
1230 {
1231   tree fn_name = DECL_NAME (function);
1232   tree fndecl;
1233   tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1234   tree *methods = 0;
1235   tree *end = 0;
1236
1237   if (method_vec != 0)
1238     {
1239       methods = &TREE_VEC_ELT (method_vec, 0);
1240       end = TREE_VEC_END (method_vec);
1241
1242       /* First suss out ctors and dtors.  */
1243       if (*methods && fn_name == DECL_NAME (*methods)
1244           && DECL_CONSTRUCTOR_P (function))
1245         goto got_it;
1246       if (*++methods && fn_name == DECL_NAME (*methods)
1247           && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function)))
1248         goto got_it;
1249
1250       while (++methods != end)
1251         {
1252           if (fn_name == DECL_NAME (*methods))
1253             {
1254             got_it:
1255               fndecl = *methods;
1256               while (fndecl)
1257                 {
1258                   if (DECL_ASSEMBLER_NAME (function) == DECL_ASSEMBLER_NAME (fndecl))
1259                     return fndecl;
1260 #if 0
1261                   /* This doesn't work for static member functions that are
1262                      pretending to be methods. */
1263                   /* We have to do more extensive argument checking here, as
1264                      the name may have been changed by asm("new_name"). */
1265                   if (decls_match (function, fndecl))
1266                     return fndecl;
1267 #else
1268                   if (DECL_NAME (function) == DECL_NAME (fndecl))
1269                     {
1270                       tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1271                       tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1272
1273                       /* Get rid of the this parameter on functions that become
1274                          static. */
1275                       if (DECL_STATIC_FUNCTION_P (fndecl)
1276                           && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1277                         p1 = TREE_CHAIN (p1);
1278
1279                       if (comptypes (TREE_TYPE (TREE_TYPE (function)),
1280                                      TREE_TYPE (TREE_TYPE (fndecl)), 1)
1281                           && compparms (p1, p2, 3))
1282                         return fndecl;
1283                     }
1284 #endif
1285                   fndecl = DECL_CHAIN (fndecl);
1286                 }
1287               break;            /* loser */
1288             }
1289         }
1290     }
1291
1292   if (methods != end)
1293     {
1294       tree fndecl = *methods;
1295       cp_error ("prototype for `%#D' does not match any in class `%T'",
1296                 function, ctype);
1297       cp_error_at ("candidate%s: %+#D", DECL_CHAIN (fndecl) ? "s are" : " is",
1298                    fndecl);
1299       while (fndecl = DECL_CHAIN (fndecl), fndecl)
1300         cp_error_at ("                %#D", fndecl);
1301     }
1302   else
1303     {
1304       methods = 0;
1305       cp_error ("no `%#D' member function declared in class `%T'",
1306                 function, ctype);
1307     }
1308
1309   /* If we did not find the method in the class, add it to avoid
1310      spurious errors.  */
1311   add_method (ctype, methods, function);
1312   return NULL_TREE;
1313 }
1314
1315 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1316    of a structure component, returning a FIELD_DECL node.
1317    QUALS is a list of type qualifiers for this decl (such as for declaring
1318    const member functions).
1319
1320    This is done during the parsing of the struct declaration.
1321    The FIELD_DECL nodes are chained together and the lot of them
1322    are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1323
1324    C++:
1325
1326    If class A defines that certain functions in class B are friends, then
1327    the way I have set things up, it is B who is interested in permission
1328    granted by A.  However, it is in A's context that these declarations
1329    are parsed.  By returning a void_type_node, class A does not attempt
1330    to incorporate the declarations of the friends within its structure.
1331
1332    DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1333    CHANGES TO CODE IN `start_method'.  */
1334
1335 tree
1336 grokfield (declarator, declspecs, raises, init, asmspec_tree, attrlist)
1337      tree declarator, declspecs, raises, init, asmspec_tree, attrlist;
1338 {
1339   register tree value;
1340   char *asmspec = 0;
1341   int flags = LOOKUP_ONLYCONVERTING;
1342
1343   /* Convert () initializers to = initializers.  */
1344   if (init == NULL_TREE && declarator != NULL_TREE
1345       && TREE_CODE (declarator) == CALL_EXPR
1346       && TREE_OPERAND (declarator, 0)
1347       && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1348           || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1349       && parmlist_is_exprlist (TREE_OPERAND (declarator, 1)))
1350     {
1351       init = TREE_OPERAND (declarator, 1);
1352       declarator = TREE_OPERAND (declarator, 0);
1353       flags = 0;
1354     }
1355
1356   if (declspecs == NULL_TREE
1357       && TREE_CODE (declarator) == SCOPE_REF)
1358     {
1359       /* Access declaration */
1360       if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1361         ;
1362       else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1363         pop_nested_class (1);
1364       return do_class_using_decl (declarator);
1365     }
1366
1367   if (init
1368       && TREE_CODE (init) == TREE_LIST
1369       && TREE_VALUE (init) == error_mark_node
1370       && TREE_CHAIN (init) == NULL_TREE)
1371     init = NULL_TREE;
1372
1373   value = grokdeclarator (declarator, declspecs, FIELD, init != 0,
1374                           raises, NULL_TREE);
1375   if (! value)
1376     return value; /* friend or constructor went bad.  */
1377
1378   /* Pass friendly classes back.  */
1379   if (TREE_CODE (value) == VOID_TYPE)
1380     return void_type_node;
1381
1382   if (DECL_NAME (value) != NULL_TREE
1383       && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1384       && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1385     cp_error ("member `%D' conflicts with virtual function table field name", value);
1386
1387   /* Stash away type declarations.  */
1388   if (TREE_CODE (value) == TYPE_DECL)
1389     {
1390       DECL_NONLOCAL (value) = 1;
1391       DECL_CONTEXT (value) = current_class_type;
1392       DECL_CLASS_CONTEXT (value) = current_class_type;
1393       CLASSTYPE_LOCAL_TYPEDECLS (current_class_type) = 1;
1394
1395       pushdecl_class_level (value);
1396       return value;
1397     }
1398
1399   if (IS_SIGNATURE (current_class_type)
1400       && TREE_CODE (value) != FUNCTION_DECL)
1401     {
1402       error ("field declaration not allowed in signature");
1403       return void_type_node;
1404     }
1405
1406   if (DECL_IN_AGGR_P (value))
1407     {
1408       cp_error ("`%D' is already defined in the class %T", value,
1409                   DECL_CONTEXT (value));
1410       return void_type_node;
1411     }
1412
1413   if (asmspec_tree)
1414     asmspec = TREE_STRING_POINTER (asmspec_tree);
1415
1416   if (init)
1417     {
1418       if (IS_SIGNATURE (current_class_type)
1419           && TREE_CODE (value) == FUNCTION_DECL)
1420         {
1421           error ("function declarations cannot have initializers in signature");
1422           init = NULL_TREE;
1423         }
1424       else if (TREE_CODE (value) == FUNCTION_DECL)
1425         {
1426           grok_function_init (value, init);
1427           init = NULL_TREE;
1428         }
1429       else if (pedantic && TREE_CODE (value) != VAR_DECL)
1430         /* Already complained in grokdeclarator.  */
1431         init = NULL_TREE;
1432       else
1433         {
1434           /* We allow initializers to become parameters to base
1435              initializers.  */
1436           if (TREE_CODE (init) == TREE_LIST)
1437             {
1438               if (TREE_CHAIN (init) == NULL_TREE)
1439                 init = TREE_VALUE (init);
1440               else
1441                 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1442             }
1443           
1444           if (TREE_CODE (init) == CONST_DECL)
1445             init = DECL_INITIAL (init);
1446           else if (TREE_READONLY_DECL_P (init))
1447             init = decl_constant_value (init);
1448           else if (TREE_CODE (init) == CONSTRUCTOR)
1449             init = digest_init (TREE_TYPE (value), init, (tree *)0);
1450           my_friendly_assert (TREE_PERMANENT (init), 192);
1451           if (init == error_mark_node)
1452             /* We must make this look different than `error_mark_node'
1453                because `decl_const_value' would mis-interpret it
1454                as only meaning that this VAR_DECL is defined.  */
1455             init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1456           else if (current_template_parms)
1457             ;
1458           else if (! TREE_CONSTANT (init))
1459             {
1460               /* We can allow references to things that are effectively
1461                  static, since references are initialized with the address.  */
1462               if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1463                   || (TREE_STATIC (init) == 0
1464                       && (TREE_CODE_CLASS (TREE_CODE (init)) != 'd'
1465                           || DECL_EXTERNAL (init) == 0)))
1466                 {
1467                   error ("field initializer is not constant");
1468                   init = error_mark_node;
1469                 }
1470             }
1471         }
1472     }
1473
1474   /* The corresponding pop_obstacks is in cp_finish_decl.  */
1475   push_obstacks_nochange ();
1476
1477   if (current_template_parms && ! current_function_decl
1478       && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1479     push_template_decl (value);
1480
1481   if (attrlist)
1482     cplus_decl_attributes (value, TREE_PURPOSE (attrlist),
1483                            TREE_VALUE (attrlist));
1484
1485   if (TREE_CODE (value) == VAR_DECL)
1486     {
1487       /* We cannot call pushdecl here, because that would
1488          fill in the value of our TREE_CHAIN.  Instead, we
1489          modify cp_finish_decl to do the right thing, namely, to
1490          put this decl out straight away.  */
1491       if (TREE_PUBLIC (value))
1492         {
1493           /* current_class_type can be NULL_TREE in case of error.  */
1494           if (asmspec == 0 && current_class_type)
1495             {
1496               TREE_PUBLIC (value) = 1;
1497               DECL_INITIAL (value) = error_mark_node;
1498               DECL_ASSEMBLER_NAME (value)
1499                 = build_static_name (current_class_type, DECL_NAME (value));
1500             }
1501           if (! current_template_parms)
1502             pending_statics = perm_tree_cons (NULL_TREE, value, pending_statics);
1503
1504           /* Static consts need not be initialized in the class definition.  */
1505           if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (value)))
1506             {
1507               static int explanation = 0;
1508
1509               error ("initializer invalid for static member with constructor");
1510               if (explanation++ == 0)
1511                 error ("(you really want to initialize it separately)");
1512               init = 0;
1513             }
1514           /* Force the compiler to know when an uninitialized static
1515              const member is being used.  */
1516           if (TYPE_READONLY (value) && init == 0)
1517             TREE_USED (value) = 1;
1518         }
1519       DECL_INITIAL (value) = init;
1520       DECL_IN_AGGR_P (value) = 1;
1521       DECL_CONTEXT (value) = current_class_type;
1522       DECL_CLASS_CONTEXT (value) = current_class_type;
1523
1524       cp_finish_decl (value, init, asmspec_tree, 1, flags);
1525       pushdecl_class_level (value);
1526       return value;
1527     }
1528   if (TREE_CODE (value) == FIELD_DECL)
1529     {
1530       if (asmspec)
1531         {
1532           /* This must override the asm specifier which was placed
1533              by grokclassfn.  Lay this out fresh.  */
1534           DECL_RTL (value) = NULL_RTX;
1535           DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1536         }
1537       if (DECL_INITIAL (value) == error_mark_node)
1538         init = error_mark_node;
1539       cp_finish_decl (value, init, asmspec_tree, 1, flags);
1540       DECL_INITIAL (value) = init;
1541       DECL_IN_AGGR_P (value) = 1;
1542       return value;
1543     }
1544   if (TREE_CODE (value) == FUNCTION_DECL)
1545     {
1546       check_default_args (value);
1547       if (DECL_CHAIN (value) != NULL_TREE)
1548         {
1549           /* Need a fresh node here so that we don't get circularity
1550              when we link these together.  */
1551           value = copy_node (value);
1552           /* When does this happen?  */
1553           my_friendly_assert (init == NULL_TREE, 193);
1554         }
1555       if (asmspec)
1556         {
1557           /* This must override the asm specifier which was placed
1558              by grokclassfn.  Lay this out fresh.  */
1559           DECL_RTL (value) = NULL_RTX;
1560           DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1561         }
1562       cp_finish_decl (value, init, asmspec_tree, 1, flags);
1563
1564       /* Pass friends back this way.  */
1565       if (DECL_FRIEND_P (value))
1566         return void_type_node;
1567
1568 #if 0 /* Just because a fn is declared doesn't mean we'll try to define it.  */
1569       if (current_function_decl && ! IS_SIGNATURE (current_class_type))
1570         cp_error ("method `%#D' of local class must be defined in class body",
1571                   value);
1572 #endif
1573
1574       DECL_IN_AGGR_P (value) = 1;
1575       return value;
1576     }
1577   my_friendly_abort (21);
1578   /* NOTREACHED */
1579   return NULL_TREE;
1580 }
1581
1582 /* Like `grokfield', but for bitfields.
1583    WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.  */
1584
1585 tree
1586 grokbitfield (declarator, declspecs, width)
1587      tree declarator, declspecs, width;
1588 {
1589   register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1590                                         0, NULL_TREE, NULL_TREE);
1591
1592   if (! value) return NULL_TREE; /* friends went bad.  */
1593
1594   /* Pass friendly classes back.  */
1595   if (TREE_CODE (value) == VOID_TYPE)
1596     return void_type_node;
1597
1598   if (TREE_CODE (value) == TYPE_DECL)
1599     {
1600       cp_error ("cannot declare `%D' to be a bitfield type", value);
1601       return NULL_TREE;
1602     }
1603
1604   if (IS_SIGNATURE (current_class_type))
1605     {
1606       error ("field declaration not allowed in signature");
1607       return void_type_node;
1608     }
1609
1610   if (DECL_IN_AGGR_P (value))
1611     {
1612       cp_error ("`%D' is already defined in the class %T", value,
1613                   DECL_CONTEXT (value));
1614       return void_type_node;
1615     }
1616
1617   GNU_xref_member (current_class_name, value);
1618
1619   if (TREE_STATIC (value))
1620     {
1621       cp_error ("static member `%D' cannot be a bitfield", value);
1622       return NULL_TREE;
1623     }
1624   cp_finish_decl (value, NULL_TREE, NULL_TREE, 0, 0);
1625
1626   if (width != error_mark_node)
1627     {
1628       constant_expression_warning (width);
1629       DECL_INITIAL (value) = width;
1630       DECL_BIT_FIELD (value) = 1;
1631     }
1632
1633   DECL_IN_AGGR_P (value) = 1;
1634   return value;
1635 }
1636
1637 #if 0
1638 /* Like GROKFIELD, except that the declarator has been
1639    buried in DECLSPECS.  Find the declarator, and
1640    return something that looks like it came from
1641    GROKFIELD.  */
1642 tree
1643 groktypefield (declspecs, parmlist)
1644      tree declspecs;
1645      tree parmlist;
1646 {
1647   tree spec = declspecs;
1648   tree prev = NULL_TREE;
1649
1650   tree type_id = NULL_TREE;
1651   tree quals = NULL_TREE;
1652   tree lengths = NULL_TREE;
1653   tree decl = NULL_TREE;
1654
1655   while (spec)
1656     {
1657       register tree id = TREE_VALUE (spec);
1658
1659       if (TREE_CODE (spec) != TREE_LIST)
1660         /* Certain parse errors slip through.  For example,
1661            `int class ();' is not caught by the parser. Try
1662            weakly to recover here.  */
1663         return NULL_TREE;
1664
1665       if (TREE_CODE (id) == TYPE_DECL
1666           || (TREE_CODE (id) == IDENTIFIER_NODE && TREE_TYPE (id)))
1667         {
1668           /* We have a constructor/destructor or
1669              conversion operator.  Use it.  */
1670           if (prev)
1671             TREE_CHAIN (prev) = TREE_CHAIN (spec);
1672           else
1673             declspecs = TREE_CHAIN (spec);
1674
1675           type_id = id;
1676           goto found;
1677         }
1678       prev = spec;
1679       spec = TREE_CHAIN (spec);
1680     }
1681
1682   /* Nope, we have a conversion operator to a scalar type or something
1683      else, that includes things like constructor declarations for
1684      templates.  */
1685   spec = declspecs;
1686   while (spec)
1687     {
1688       tree id = TREE_VALUE (spec);
1689
1690       if (TREE_CODE (id) == IDENTIFIER_NODE)
1691         {
1692           if (id == ridpointers[(int)RID_INT]
1693               || id == ridpointers[(int)RID_DOUBLE]
1694               || id == ridpointers[(int)RID_FLOAT]
1695               || id == ridpointers[(int)RID_WCHAR])
1696             {
1697               if (type_id)
1698                 error ("extra `%s' ignored",
1699                        IDENTIFIER_POINTER (id));
1700               else
1701                 type_id = id;
1702             }
1703           else if (id == ridpointers[(int)RID_LONG]
1704                    || id == ridpointers[(int)RID_SHORT]
1705                    || id == ridpointers[(int)RID_CHAR])
1706             {
1707               lengths = tree_cons (NULL_TREE, id, lengths);
1708             }
1709           else if (id == ridpointers[(int)RID_VOID])
1710             {
1711               if (type_id)
1712                 error ("spurious `void' type ignored");
1713               else
1714                 error ("conversion to `void' type invalid");
1715             }
1716           else if (id == ridpointers[(int)RID_AUTO]
1717                    || id == ridpointers[(int)RID_REGISTER]
1718                    || id == ridpointers[(int)RID_TYPEDEF]
1719                    || id == ridpointers[(int)RID_CONST]
1720                    || id == ridpointers[(int)RID_VOLATILE])
1721             {
1722               error ("type specifier `%s' used invalidly",
1723                      IDENTIFIER_POINTER (id));
1724             }
1725           else if (id == ridpointers[(int)RID_FRIEND]
1726                    || id == ridpointers[(int)RID_VIRTUAL]
1727                    || id == ridpointers[(int)RID_INLINE]
1728                    || id == ridpointers[(int)RID_UNSIGNED]
1729                    || id == ridpointers[(int)RID_SIGNED]
1730                    || id == ridpointers[(int)RID_STATIC]
1731                    || id == ridpointers[(int)RID_EXTERN])
1732             {
1733               quals = tree_cons (NULL_TREE, id, quals);
1734             }
1735           else
1736             {
1737               /* Happens when we have a global typedef
1738                  and a class-local member function with
1739                  the same name.  */
1740               type_id = id;
1741               goto found;
1742             }
1743         }
1744       else if (TREE_CODE (id) == RECORD_TYPE)
1745         {
1746           type_id = TYPE_NAME (id);
1747           if (TREE_CODE (type_id) == TYPE_DECL)
1748             type_id = DECL_NAME (type_id);
1749           if (type_id == NULL_TREE)
1750             error ("identifier for aggregate type conversion omitted");
1751         }
1752       else if (TREE_CODE_CLASS (TREE_CODE (id)) == 't')
1753         error ("`operator' missing on conversion operator or tag missing from type");
1754       else
1755         my_friendly_abort (194);
1756       spec = TREE_CHAIN (spec);
1757     }
1758
1759   if (type_id)
1760     declspecs = chainon (lengths, quals);
1761   else if (lengths)
1762     {
1763       if (TREE_CHAIN (lengths))
1764         error ("multiple length specifiers");
1765       type_id = ridpointers[(int)RID_INT];
1766       declspecs = chainon (lengths, quals);
1767     }
1768   else if (quals)
1769     {
1770       error ("no type given, defaulting to `operator int ...'");
1771       type_id = ridpointers[(int)RID_INT];
1772       declspecs = quals;
1773     }
1774   else
1775     return NULL_TREE;
1776
1777  found:
1778   decl = grokdeclarator (build_parse_node (CALL_EXPR, type_id, parmlist, NULL_TREE),
1779                          declspecs, FIELD, 0, NULL_TREE, NULL_TREE);
1780   if (decl == NULL_TREE)
1781     return NULL_TREE;
1782
1783   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CHAIN (decl) != NULL_TREE)
1784     {
1785       /* Need a fresh node here so that we don't get circularity
1786          when we link these together.  */
1787       decl = copy_node (decl);
1788     }
1789
1790   if (decl == void_type_node
1791       || (TREE_CODE (decl) == FUNCTION_DECL
1792           && TREE_CODE (TREE_TYPE (decl)) != METHOD_TYPE))
1793     /* bunch of friends.  */
1794     return decl;
1795
1796   if (DECL_IN_AGGR_P (decl))
1797     {
1798       cp_error ("`%D' already defined in the class ", decl);
1799       return void_type_node;
1800     }
1801
1802   cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0, 0);
1803
1804   /* If this declaration is common to another declaration
1805      complain about such redundancy, and return NULL_TREE
1806      so that we don't build a circular list.  */
1807   if (DECL_CHAIN (decl))
1808     {
1809       cp_error ("function `%D' declared twice in class %T", decl,
1810                   DECL_CONTEXT (decl));
1811       return NULL_TREE;
1812     }
1813   DECL_IN_AGGR_P (decl) = 1;
1814   return decl;
1815 }
1816 #endif
1817
1818 tree
1819 grokoptypename (declspecs, declarator)
1820      tree declspecs, declarator;
1821 {
1822   tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0,
1823                            NULL_TREE, NULL_TREE);
1824   return build_typename_overload (t);
1825 }
1826
1827 /* When a function is declared with an initializer,
1828    do the right thing.  Currently, there are two possibilities:
1829
1830    class B
1831    {
1832     public:
1833      // initialization possibility #1.
1834      virtual void f () = 0;
1835      int g ();
1836    };
1837    
1838    class D1 : B
1839    {
1840     public:
1841      int d1;
1842      // error, no f ();
1843    };
1844    
1845    class D2 : B
1846    {
1847     public:
1848      int d2;
1849      void f ();
1850    };
1851    
1852    class D3 : B
1853    {
1854     public:
1855      int d3;
1856      // initialization possibility #2
1857      void f () = B::f;
1858    };
1859
1860 */
1861
1862 int
1863 copy_assignment_arg_p (parmtype, virtualp)
1864      tree parmtype;
1865      int virtualp;
1866 {
1867   if (TREE_CODE (parmtype) == REFERENCE_TYPE)
1868     parmtype = TREE_TYPE (parmtype);
1869
1870   if ((TYPE_MAIN_VARIANT (parmtype) == current_class_type)
1871       || (virtualp && DERIVED_FROM_P (parmtype, current_class_type)))
1872     return 1;
1873
1874   return 0;
1875 }
1876
1877 static void
1878 grok_function_init (decl, init)
1879      tree decl;
1880      tree init;
1881 {
1882   /* An initializer for a function tells how this function should
1883      be inherited.  */
1884   tree type = TREE_TYPE (decl);
1885
1886   if (TREE_CODE (type) == FUNCTION_TYPE)
1887     cp_error ("initializer specified for non-member function `%D'", decl);
1888 #if 0
1889   /* We'll check for this in finish_struct_1.  */
1890   else if (DECL_VINDEX (decl) == NULL_TREE)
1891     cp_error ("initializer specified for non-virtual method `%D'", decl);
1892 #endif
1893   else if (integer_zerop (init))
1894     {
1895 #if 0
1896       /* Mark this function as being "defined".  */
1897       DECL_INITIAL (decl) = error_mark_node;
1898       /* pure virtual destructors must be defined. */
1899       /* pure virtual needs to be defined (as abort) only when put in 
1900          vtbl. For wellformed call, it should be itself. pr4737 */
1901       if (!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)))
1902         {
1903           extern tree abort_fndecl;
1904           /* Give this node rtl from `abort'.  */
1905           DECL_RTL (decl) = DECL_RTL (abort_fndecl);
1906         }
1907 #endif
1908       DECL_ABSTRACT_VIRTUAL_P (decl) = 1;
1909       if (DECL_NAME (decl) == ansi_opname [(int) MODIFY_EXPR])
1910         {
1911           tree parmtype
1912             = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))));
1913
1914           if (copy_assignment_arg_p (parmtype, 1))
1915             TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type) = 1;
1916         }
1917     }
1918   else if (TREE_CODE (init) == OFFSET_REF
1919            && TREE_OPERAND (init, 0) == NULL_TREE
1920            && TREE_CODE (TREE_TYPE (init)) == METHOD_TYPE)
1921     {
1922       tree basetype = DECL_CLASS_CONTEXT (init);
1923       tree basefn = TREE_OPERAND (init, 1);
1924       if (TREE_CODE (basefn) != FUNCTION_DECL)
1925         cp_error ("non-method initializer invalid for method `%D'", decl);
1926       else if (! BINFO_OFFSET_ZEROP (TYPE_BINFO (DECL_CLASS_CONTEXT (basefn))))
1927         sorry ("base member function from other than first base class");
1928       else
1929         {
1930           tree binfo = get_binfo (basetype, TYPE_METHOD_BASETYPE (type), 1);
1931           if (binfo == error_mark_node)
1932             ;
1933           else if (binfo == 0)
1934             error_not_base_type (TYPE_METHOD_BASETYPE (TREE_TYPE (init)),
1935                                  TYPE_METHOD_BASETYPE (type));
1936           else
1937             {
1938               /* Mark this function as being defined,
1939                  and give it new rtl.  */
1940               DECL_INITIAL (decl) = error_mark_node;
1941               DECL_RTL (decl) = DECL_RTL (basefn);
1942             }
1943         }
1944     }
1945   else
1946     cp_error ("invalid initializer for virtual method `%D'", decl);
1947 }
1948 \f
1949 void
1950 cplus_decl_attributes (decl, attributes, prefix_attributes)
1951      tree decl, attributes, prefix_attributes;
1952 {
1953   if (decl == NULL_TREE || decl == void_type_node)
1954     return;
1955
1956   if (TREE_CODE (decl) == TEMPLATE_DECL)
1957     decl = DECL_TEMPLATE_RESULT (decl);
1958
1959   decl_attributes (decl, attributes, prefix_attributes);
1960
1961   if (TREE_CODE (decl) == TYPE_DECL)
1962     SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (decl), TREE_TYPE (decl));
1963 }
1964 \f
1965 /* CONSTRUCTOR_NAME:
1966    Return the name for the constructor (or destructor) for the
1967    specified class.  Argument can be RECORD_TYPE, TYPE_DECL, or
1968    IDENTIFIER_NODE.  When given a template, this routine doesn't
1969    lose the specialization.  */
1970 tree
1971 constructor_name_full (thing)
1972      tree thing;
1973 {
1974   if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM)
1975     thing = TYPE_NAME (thing);
1976   else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
1977     {
1978       if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
1979         thing = DECL_NAME (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0));
1980       else
1981         thing = TYPE_NAME (thing);
1982     }
1983   if (TREE_CODE (thing) == TYPE_DECL
1984       || (TREE_CODE (thing) == TEMPLATE_DECL
1985           && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
1986     thing = DECL_NAME (thing);
1987   my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
1988   return thing;
1989 }
1990
1991 /* CONSTRUCTOR_NAME:
1992    Return the name for the constructor (or destructor) for the
1993    specified class.  Argument can be RECORD_TYPE, TYPE_DECL, or
1994    IDENTIFIER_NODE.  When given a template, return the plain
1995    unspecialized name.  */
1996 tree
1997 constructor_name (thing)
1998      tree thing;
1999 {
2000   tree t;
2001   thing = constructor_name_full (thing);
2002   t = IDENTIFIER_TEMPLATE (thing);
2003   if (!t)
2004     return thing;
2005   return t;
2006 }
2007 \f
2008 /* Cache the value of this class's main virtual function table pointer
2009    in a register variable.  This will save one indirection if a
2010    more than one virtual function call is made this function.  */
2011 void
2012 setup_vtbl_ptr ()
2013 {
2014   extern tree base_init_expr;
2015
2016   if (base_init_expr == 0
2017       && DECL_CONSTRUCTOR_P (current_function_decl))
2018     {
2019       if (current_template_parms)
2020         add_tree (build_min_nt
2021                   (CTOR_INITIALIZER,
2022                    current_member_init_list, current_base_init_list));
2023       else
2024         emit_base_init (current_class_type, 0);
2025     }
2026 }
2027
2028 /* Record the existence of an addressable inline function.  */
2029 void
2030 mark_inline_for_output (decl)
2031      tree decl;
2032 {
2033   decl = DECL_MAIN_VARIANT (decl);
2034   if (DECL_SAVED_INLINE (decl))
2035     return;
2036   my_friendly_assert (TREE_PERMANENT (decl), 363);
2037   DECL_SAVED_INLINE (decl) = 1;
2038 #if 0
2039   if (DECL_PENDING_INLINE_INFO (decl) != 0
2040       && ! DECL_PENDING_INLINE_INFO (decl)->deja_vu)
2041     {
2042       struct pending_inline *t = pending_inlines;
2043       my_friendly_assert (DECL_SAVED_INSNS (decl) == 0, 198);
2044       while (t)
2045         {
2046           if (t == DECL_PENDING_INLINE_INFO (decl))
2047             break;
2048           t = t->next;
2049         }
2050       if (t == 0)
2051         {
2052           t = DECL_PENDING_INLINE_INFO (decl);
2053           t->next = pending_inlines;
2054           pending_inlines = t;
2055         }
2056       DECL_PENDING_INLINE_INFO (decl) = 0;
2057     }
2058 #endif
2059   saved_inlines = perm_tree_cons (NULL_TREE, decl, saved_inlines);
2060 }
2061
2062 void
2063 clear_temp_name ()
2064 {
2065   temp_name_counter = 0;
2066 }
2067
2068 /* Hand off a unique name which can be used for variable we don't really
2069    want to know about anyway, for example, the anonymous variables which
2070    are needed to make references work.  Declare this thing so we can use it.
2071    The variable created will be of type TYPE.
2072
2073    STATICP is nonzero if this variable should be static.  */
2074
2075 tree
2076 get_temp_name (type, staticp)
2077      tree type;
2078      int staticp;
2079 {
2080   char buf[sizeof (AUTO_TEMP_FORMAT) + 20];
2081   tree decl;
2082   int toplev = toplevel_bindings_p ();
2083
2084   push_obstacks_nochange ();
2085   if (toplev || staticp)
2086     {
2087       end_temporary_allocation ();
2088       sprintf (buf, AUTO_TEMP_FORMAT, global_temp_name_counter++);
2089       decl = pushdecl_top_level (build_decl (VAR_DECL, get_identifier (buf), type));
2090     }
2091   else
2092     {
2093       sprintf (buf, AUTO_TEMP_FORMAT, temp_name_counter++);
2094       decl = pushdecl (build_decl (VAR_DECL, get_identifier (buf), type));
2095     }
2096   TREE_USED (decl) = 1;
2097   TREE_STATIC (decl) = staticp;
2098   DECL_ARTIFICIAL (decl) = 1;
2099
2100   /* If this is a local variable, then lay out its rtl now.
2101      Otherwise, callers of this function are responsible for dealing
2102      with this variable's rtl.  */
2103   if (! toplev)
2104     {
2105       expand_decl (decl);
2106       expand_decl_init (decl);
2107     }
2108   pop_obstacks ();
2109
2110   return decl;
2111 }
2112
2113 /* Get a variable which we can use for multiple assignments.
2114    It is not entered into current_binding_level, because
2115    that breaks things when it comes time to do final cleanups
2116    (which take place "outside" the binding contour of the function).  */
2117 tree
2118 get_temp_regvar (type, init)
2119      tree type, init;
2120 {
2121   static char buf[sizeof (AUTO_TEMP_FORMAT) + 20] = { '_' };
2122   tree decl;
2123
2124   sprintf (buf+1, AUTO_TEMP_FORMAT, temp_name_counter++);
2125   decl = build_decl (VAR_DECL, get_identifier (buf), type);
2126   TREE_USED (decl) = 1;
2127   DECL_REGISTER (decl) = 1;
2128
2129   if (init)
2130     store_init_value (decl, init);
2131
2132   /* We can expand these without fear, since they cannot need
2133      constructors or destructors.  */
2134   expand_decl (decl);
2135   expand_decl_init (decl);
2136
2137   return decl;
2138 }
2139
2140 /* Make the macro TEMP_NAME_P available to units which do not
2141    include c-tree.h.  */
2142 int
2143 temp_name_p (decl)
2144      tree decl;
2145 {
2146   return TEMP_NAME_P (decl);
2147 }
2148
2149 /* Finish off the processing of a UNION_TYPE structure.
2150    If there are static members, then all members are
2151    static, and must be laid out together.  If the
2152    union is an anonymous union, we arrange for that
2153    as well.  PUBLIC_P is nonzero if this union is
2154    not declared static.  */
2155 void
2156 finish_anon_union (anon_union_decl)
2157      tree anon_union_decl;
2158 {
2159   tree type = TREE_TYPE (anon_union_decl);
2160   tree field, main_decl = NULL_TREE;
2161   tree elems = NULL_TREE;
2162   int public_p = TREE_PUBLIC (anon_union_decl);
2163   int static_p = TREE_STATIC (anon_union_decl);
2164   int external_p = DECL_EXTERNAL (anon_union_decl);
2165
2166   if ((field = TYPE_FIELDS (type)) == NULL_TREE)
2167     return;
2168
2169   if (public_p)
2170     {
2171       error ("global anonymous unions must be declared static");
2172       return;
2173     }
2174
2175   for (; field; field = TREE_CHAIN (field))
2176     {
2177       tree decl;
2178       if (TREE_CODE (field) != FIELD_DECL)
2179         continue;
2180
2181       if (TREE_PRIVATE (field))
2182         cp_pedwarn_at ("private member `%#D' in anonymous union", field);
2183       else if (TREE_PROTECTED (field))
2184         cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
2185
2186       decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
2187       /* tell `pushdecl' that this is not tentative.  */
2188       DECL_INITIAL (decl) = error_mark_node;
2189       TREE_PUBLIC (decl) = public_p;
2190       TREE_STATIC (decl) = static_p;
2191       DECL_EXTERNAL (decl) = external_p;
2192       decl = pushdecl (decl);
2193
2194       /* Only write out one anon union element--choose the one that
2195          can hold them all.  */
2196       if (main_decl == NULL_TREE
2197           && 1 == simple_cst_equal (DECL_SIZE (decl),
2198                                     DECL_SIZE (anon_union_decl)))
2199         {
2200           main_decl = decl;
2201         }
2202       else
2203         {
2204           /* ??? This causes there to be no debug info written out
2205              about this decl.  */
2206           TREE_ASM_WRITTEN (decl) = 1;
2207         }
2208
2209       DECL_INITIAL (decl) = NULL_TREE;
2210       /* If there's a cleanup to do, it belongs in the
2211          TREE_PURPOSE of the following TREE_LIST.  */
2212       elems = tree_cons (NULL_TREE, decl, elems);
2213       TREE_TYPE (elems) = type;
2214     }
2215   if (static_p)
2216     {
2217       if (main_decl)
2218         {
2219           make_decl_rtl (main_decl, 0, toplevel_bindings_p ());
2220           DECL_RTL (anon_union_decl) = DECL_RTL (main_decl);
2221         }
2222       else
2223         {
2224           warning ("anonymous union with no members");
2225           return;
2226         }
2227     }
2228
2229   /* The following call assumes that there are never any cleanups
2230      for anonymous unions--a reasonable assumption.  */
2231   expand_anon_union_decl (anon_union_decl, NULL_TREE, elems);
2232 }
2233
2234 /* Finish and output a table which is generated by the compiler.
2235    NAME is the name to give the table.
2236    TYPE is the type of the table entry.
2237    INIT is all the elements in the table.
2238    PUBLICP is non-zero if this table should be given external access.  */
2239 tree
2240 finish_table (name, type, init, publicp)
2241      tree name, type, init;
2242      int publicp;
2243 {
2244   tree itype, atype, decl;
2245   static tree empty_table;
2246   int is_empty = 0;
2247   tree asmspec;
2248
2249   itype = build_index_type (size_int (list_length (init) - 1));
2250   atype = build_cplus_array_type (type, itype);
2251   layout_type (atype);
2252
2253   if (TREE_VALUE (init) == integer_zero_node
2254       && TREE_CHAIN (init) == NULL_TREE)
2255     {
2256 #if 0
2257       if (empty_table == NULL_TREE)
2258 #endif
2259         {
2260           empty_table = get_temp_name (atype, 1);
2261           init = build (CONSTRUCTOR, atype, NULL_TREE, init);
2262           TREE_CONSTANT (init) = 1;
2263           TREE_STATIC (init) = 1;
2264           DECL_INITIAL (empty_table) = init;
2265           asmspec = build_string (IDENTIFIER_LENGTH (DECL_NAME (empty_table)),
2266                                   IDENTIFIER_POINTER (DECL_NAME (empty_table)));
2267           cp_finish_decl (empty_table, NULL_TREE, asmspec, 0, 0);
2268         }
2269       is_empty = 1;
2270     }
2271
2272   if (name == NULL_TREE)
2273     {
2274       if (is_empty)
2275         return empty_table;
2276       decl = get_temp_name (atype, 1);
2277     }
2278   else
2279     {
2280       decl = build_decl (VAR_DECL, name, atype);
2281       decl = pushdecl (decl);
2282       TREE_STATIC (decl) = 1;
2283     }
2284
2285   if (is_empty == 0)
2286     {
2287       TREE_PUBLIC (decl) = publicp;
2288       init = build (CONSTRUCTOR, atype, NULL_TREE, init);
2289       TREE_CONSTANT (init) = 1;
2290       TREE_STATIC (init) = 1;
2291       DECL_INITIAL (decl) = init;
2292       asmspec = build_string (IDENTIFIER_LENGTH (DECL_NAME (decl)),
2293                               IDENTIFIER_POINTER (DECL_NAME (decl)));
2294     }
2295   else
2296     {
2297       /* This will cause DECL to point to EMPTY_TABLE in rtl-land.  */
2298       DECL_EXTERNAL (decl) = 1;
2299       TREE_STATIC (decl) = 0;
2300       init = 0;
2301       asmspec = build_string (IDENTIFIER_LENGTH (DECL_NAME (empty_table)),
2302                               IDENTIFIER_POINTER (DECL_NAME (empty_table)));
2303     }
2304
2305   cp_finish_decl (decl, NULL_TREE, asmspec, 0, 0);
2306   return decl;
2307 }
2308
2309 /* Finish processing a builtin type TYPE.  It's name is NAME,
2310    its fields are in the array FIELDS.  LEN is the number of elements
2311    in FIELDS minus one, or put another way, it is the maximum subscript
2312    used in FIELDS.
2313
2314    It is given the same alignment as ALIGN_TYPE.  */
2315 void
2316 finish_builtin_type (type, name, fields, len, align_type)
2317      tree type;
2318      char *name;
2319      tree fields[];
2320      int len;
2321      tree align_type;
2322 {
2323   register int i;
2324
2325   TYPE_FIELDS (type) = fields[0];
2326   for (i = 0; i < len; i++)
2327     {
2328       layout_type (TREE_TYPE (fields[i]));
2329       DECL_FIELD_CONTEXT (fields[i]) = type;
2330       TREE_CHAIN (fields[i]) = fields[i+1];
2331     }
2332   DECL_FIELD_CONTEXT (fields[i]) = type;
2333   DECL_CLASS_CONTEXT (fields[i]) = type;
2334   TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2335   layout_type (type);
2336 #if 0 /* not yet, should get fixed properly later */
2337   TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2338 #else
2339   TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2340 #endif
2341   layout_decl (TYPE_NAME (type), 0);
2342 }
2343 \f
2344 /* Auxiliary functions to make type signatures for
2345    `operator new' and `operator delete' correspond to
2346    what compiler will be expecting.  */
2347
2348 extern tree sizetype;
2349
2350 tree
2351 coerce_new_type (type)
2352      tree type;
2353 {
2354   int e1 = 0, e2 = 0;
2355
2356   if (TREE_CODE (type) == METHOD_TYPE)
2357     type = build_function_type (TREE_TYPE (type), TREE_CHAIN (TYPE_ARG_TYPES (type)));
2358   if (TREE_TYPE (type) != ptr_type_node)
2359     e1 = 1, error ("`operator new' must return type `void *'");
2360
2361   /* Technically the type must be `size_t', but we may not know
2362      what that is.  */
2363   if (TYPE_ARG_TYPES (type) == NULL_TREE)
2364     e1 = 1, error ("`operator new' takes type `size_t' parameter");
2365   else if (TREE_CODE (TREE_VALUE (TYPE_ARG_TYPES (type))) != INTEGER_TYPE
2366            || TYPE_PRECISION (TREE_VALUE (TYPE_ARG_TYPES (type))) != TYPE_PRECISION (sizetype))
2367     e2 = 1, error ("`operator new' takes type `size_t' as first parameter");
2368   if (e2)
2369     type = build_function_type (ptr_type_node, tree_cons (NULL_TREE, sizetype, TREE_CHAIN (TYPE_ARG_TYPES (type))));
2370   else if (e1)
2371     type = build_function_type (ptr_type_node, TYPE_ARG_TYPES (type));
2372   return type;
2373 }
2374
2375 tree
2376 coerce_delete_type (type)
2377      tree type;
2378 {
2379   int e1 = 0, e2 = 0, e3 = 0;
2380   tree arg_types = TYPE_ARG_TYPES (type);
2381
2382   if (TREE_CODE (type) == METHOD_TYPE)
2383     {
2384       type = build_function_type (TREE_TYPE (type), TREE_CHAIN (arg_types));
2385       arg_types = TREE_CHAIN (arg_types);
2386     }
2387   if (TREE_TYPE (type) != void_type_node)
2388     e1 = 1, error ("`operator delete' must return type `void'");
2389   if (arg_types == NULL_TREE
2390       || TREE_VALUE (arg_types) != ptr_type_node)
2391     e2 = 1, error ("`operator delete' takes type `void *' as first parameter");
2392
2393   if (arg_types
2394       && TREE_CHAIN (arg_types)
2395       && TREE_CHAIN (arg_types) != void_list_node)
2396     {
2397       /* Again, technically this argument must be `size_t', but again
2398          we may not know what that is.  */
2399       tree t2 = TREE_VALUE (TREE_CHAIN (arg_types));
2400       if (TREE_CODE (t2) != INTEGER_TYPE
2401           || TYPE_PRECISION (t2) != TYPE_PRECISION (sizetype))
2402         e3 = 1, error ("second argument to `operator delete' must be of type `size_t'");
2403       else if (TREE_CHAIN (TREE_CHAIN (arg_types)) != void_list_node)
2404         {
2405           e3 = 1;
2406           if (TREE_CHAIN (TREE_CHAIN (arg_types)))
2407             error ("too many arguments in declaration of `operator delete'");
2408           else
2409             error ("`...' invalid in specification of `operator delete'");
2410         }
2411     }
2412   if (e3)
2413     arg_types = tree_cons (NULL_TREE, ptr_type_node, build_tree_list (NULL_TREE, sizetype));
2414   else if (e3 |= e2)
2415     {
2416       if (arg_types == NULL_TREE)
2417         arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
2418       else
2419         arg_types = tree_cons (NULL_TREE, ptr_type_node, TREE_CHAIN (arg_types));
2420     }
2421   else e3 |= e1;
2422
2423   if (e3)
2424     type = build_function_type (void_type_node, arg_types);
2425
2426   return type;
2427 }
2428 \f
2429 extern tree abort_fndecl;
2430
2431 static void
2432 mark_vtable_entries (decl)
2433      tree decl;
2434 {
2435   tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2436
2437   if (flag_rtti)
2438     {
2439       tree fnaddr = (flag_vtable_thunks ? TREE_VALUE (TREE_CHAIN (entries))
2440                      : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries)));
2441       tree fn = TREE_OPERAND (fnaddr, 0);
2442       TREE_ADDRESSABLE (fn) = 1;
2443       mark_used (fn);
2444     }
2445   skip_rtti_stuff (&entries);
2446
2447   for (; entries; entries = TREE_CHAIN (entries))
2448     {
2449       tree fnaddr = (flag_vtable_thunks ? TREE_VALUE (entries) 
2450                      : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries)));
2451       tree fn = TREE_OPERAND (fnaddr, 0);
2452       TREE_ADDRESSABLE (fn) = 1;
2453       if (DECL_LANG_SPECIFIC (fn) && DECL_ABSTRACT_VIRTUAL_P (fn))
2454         TREE_OPERAND (fnaddr, 0) = fn = abort_fndecl;
2455       if (TREE_CODE (fn) == THUNK_DECL && DECL_EXTERNAL (fn))
2456         {
2457           DECL_EXTERNAL (fn) = 0;
2458           emit_thunk (fn);
2459         }
2460       mark_used (fn);
2461     }
2462 }
2463
2464 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2465    based on TYPE and other static flags.
2466
2467    Note that anything public is tagged TREE_PUBLIC, whether
2468    it's public in this file or in another one.  */
2469
2470 void
2471 import_export_vtable (decl, type, final)
2472      tree decl, type;
2473      int final;
2474 {
2475   if (DECL_INTERFACE_KNOWN (decl))
2476     return;
2477
2478   /* +e0 or +e1 */
2479   if (write_virtuals < 2 && write_virtuals != 0)
2480     {
2481       TREE_PUBLIC (decl) = 1;
2482       if (write_virtuals < 0)
2483         DECL_EXTERNAL (decl) = 1;
2484       DECL_INTERFACE_KNOWN (decl) = 1;
2485     }
2486   else if (CLASSTYPE_INTERFACE_KNOWN (type))
2487     {
2488       TREE_PUBLIC (decl) = 1;
2489       DECL_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
2490       DECL_INTERFACE_KNOWN (decl) = 1;
2491     }
2492   else
2493     {
2494       /* We can only wait to decide if we have real non-inline virtual
2495          functions in our class, or if we come from a template.  */
2496
2497       int found = CLASSTYPE_TEMPLATE_INSTANTIATION (type);
2498
2499       if (! found && ! final)
2500         {
2501           tree method;
2502           for (method = TYPE_METHODS (type); method != NULL_TREE;
2503                method = TREE_CHAIN (method))
2504             if (DECL_VINDEX (method) != NULL_TREE
2505                 && ! DECL_THIS_INLINE (method)
2506                 && ! DECL_ABSTRACT_VIRTUAL_P (method))
2507               {
2508                 found = 1;
2509                 break;
2510               }
2511         }
2512
2513       if (final || ! found)
2514         {
2515 #ifdef DECL_ONE_ONLY
2516           if (SUPPORTS_ONE_ONLY)
2517             {
2518               TREE_PUBLIC (decl) = 1;
2519               DECL_ONE_ONLY (decl) = 1;
2520             }
2521           else
2522 #endif
2523           if (flag_weak)
2524             {
2525               TREE_PUBLIC (decl) = 1;
2526               DECL_WEAK (decl) = 1;
2527             }
2528 #ifdef ASSEMBLE_EXTERNAL
2529           else if (TREE_PUBLIC (decl))
2530             cp_error ("all virtual functions redeclared inline");
2531 #endif
2532           else
2533             TREE_PUBLIC (decl) = 0;
2534           DECL_EXTERNAL (decl) = 0;
2535         }
2536       else
2537         {
2538           TREE_PUBLIC (decl) = 1;
2539           DECL_EXTERNAL (decl) = 1;
2540         }
2541     }
2542 }
2543
2544 static void
2545 import_export_template (type)
2546      tree type;
2547 {
2548   if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
2549       && ! flag_implicit_templates
2550       && CLASSTYPE_INTERFACE_UNKNOWN (type))
2551     {
2552       SET_CLASSTYPE_INTERFACE_KNOWN (type);
2553       CLASSTYPE_INTERFACE_ONLY (type) = 1;
2554       CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 0;
2555     }
2556 }
2557     
2558 static int
2559 finish_prevtable_vardecl (prev, vars)
2560      tree prev, vars;
2561 {
2562   tree ctype = DECL_CONTEXT (vars);
2563   import_export_template (ctype);
2564
2565   if (CLASSTYPE_INTERFACE_UNKNOWN (ctype) && TYPE_VIRTUAL_P (ctype)
2566       && ! (SUPPORTS_ONE_ONLY > 1)
2567       && ! CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2568     {
2569       tree method;
2570       for (method = TYPE_METHODS (ctype); method != NULL_TREE;
2571            method = TREE_CHAIN (method))
2572         {
2573           if (DECL_VINDEX (method) != NULL_TREE
2574               && !DECL_THIS_INLINE (method)
2575               && !DECL_ABSTRACT_VIRTUAL_P (method))
2576             {
2577               SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2578               CLASSTYPE_VTABLE_NEEDS_WRITING (ctype) = ! DECL_EXTERNAL (method);
2579               CLASSTYPE_INTERFACE_ONLY (ctype) = DECL_EXTERNAL (method);
2580               break;
2581             }
2582         }
2583     }
2584
2585   import_export_vtable (vars, ctype, 1);
2586   return 1;
2587 }
2588     
2589 static int
2590 finish_vtable_vardecl (prev, vars)
2591      tree prev, vars;
2592 {
2593   if (write_virtuals >= 0
2594       && ! DECL_EXTERNAL (vars) && (TREE_PUBLIC (vars) || TREE_USED (vars))
2595       && ! TREE_ASM_WRITTEN (vars))
2596     {
2597       /* Write it out.  */
2598       mark_vtable_entries (vars);
2599       if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2600         store_init_value (vars, DECL_INITIAL (vars));
2601
2602 #ifdef DWARF_DEBUGGING_INFO
2603       if (write_symbols == DWARF_DEBUG)
2604         {
2605           /* Mark the VAR_DECL node representing the vtable itself as a
2606              "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2607              It is rather important that such things be ignored because
2608              any effort to actually generate DWARF for them will run
2609              into trouble when/if we encounter code like:
2610
2611                 #pragma interface
2612                 struct S { virtual void member (); };
2613
2614               because the artificial declaration of the vtable itself (as
2615               manufactured by the g++ front end) will say that the vtable
2616               is a static member of `S' but only *after* the debug output
2617               for the definition of `S' has already been output.  This causes
2618               grief because the DWARF entry for the definition of the vtable
2619               will try to refer back to an earlier *declaration* of the
2620               vtable as a static member of `S' and there won't be one.
2621               We might be able to arrange to have the "vtable static member"
2622               attached to the member list for `S' before the debug info for
2623               `S' get written (which would solve the problem) but that would
2624               require more intrusive changes to the g++ front end.  */
2625
2626           DECL_IGNORED_P (vars) = 1;
2627         }
2628 #endif /* DWARF_DEBUGGING_INFO */
2629
2630       rest_of_decl_compilation (vars, NULL_PTR, 1, 1);
2631       return 1;
2632     }
2633   else if (! TREE_USED (vars))
2634     /* We don't know what to do with this one yet.  */
2635     return 0;
2636
2637   /* We know that PREV must be non-zero here.  */
2638   TREE_CHAIN (prev) = TREE_CHAIN (vars);
2639   return 0;
2640 }
2641
2642 static int
2643 prune_vtable_vardecl (prev, vars)
2644      tree prev, vars;
2645 {
2646   /* We know that PREV must be non-zero here.  */
2647   TREE_CHAIN (prev) = TREE_CHAIN (vars);
2648   return 1;
2649 }
2650
2651 int
2652 walk_vtables (typedecl_fn, vardecl_fn)
2653      register void (*typedecl_fn)();
2654      register int (*vardecl_fn)();
2655 {
2656   tree prev, vars;
2657   int flag = 0;
2658
2659   for (prev = 0, vars = getdecls (); vars; vars = TREE_CHAIN (vars))
2660     {
2661       register tree type = TREE_TYPE (vars);
2662
2663       if (TREE_CODE (vars) == VAR_DECL && DECL_VIRTUAL_P (vars))
2664         {
2665           if (vardecl_fn)
2666             flag |= (*vardecl_fn) (prev, vars);
2667
2668           if (prev && TREE_CHAIN (prev) != vars)
2669             continue;
2670         }
2671       else if (TREE_CODE (vars) == TYPE_DECL
2672                && type != error_mark_node
2673                && TYPE_LANG_SPECIFIC (type)
2674                && CLASSTYPE_VSIZE (type))
2675         {
2676           if (typedecl_fn) (*typedecl_fn) (prev, vars);
2677         }
2678
2679       prev = vars;
2680     }
2681
2682   return flag;
2683 }
2684
2685 static void
2686 finish_sigtable_vardecl (prev, vars)
2687      tree prev, vars;
2688 {
2689   /* We don't need to mark sigtable entries as addressable here as is done
2690      for vtables.  Since sigtables, unlike vtables, are always written out,
2691      that was already done in build_signature_table_constructor.  */
2692
2693   rest_of_decl_compilation (vars, NULL_PTR, 1, 1);
2694
2695   /* We know that PREV must be non-zero here.  */
2696   TREE_CHAIN (prev) = TREE_CHAIN (vars);
2697 }
2698
2699 void
2700 walk_sigtables (typedecl_fn, vardecl_fn)
2701      register void (*typedecl_fn)();
2702      register void (*vardecl_fn)();
2703 {
2704   tree prev, vars;
2705
2706   for (prev = 0, vars = getdecls (); vars; vars = TREE_CHAIN (vars))
2707     {
2708       register tree type = TREE_TYPE (vars);
2709
2710       if (TREE_CODE (vars) == TYPE_DECL
2711           && type != error_mark_node
2712           && IS_SIGNATURE (type))
2713         {
2714           if (typedecl_fn) (*typedecl_fn) (prev, vars);
2715         }
2716       else if (TREE_CODE (vars) == VAR_DECL
2717                && TREE_TYPE (vars) != error_mark_node
2718                && IS_SIGNATURE (TREE_TYPE (vars)))
2719         {
2720           if (vardecl_fn) (*vardecl_fn) (prev, vars);
2721         }
2722       else
2723         prev = vars;
2724     }
2725 }
2726
2727 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2728    inline function or template instantiation at end-of-file.  */
2729
2730 void
2731 import_export_decl (decl)
2732      tree decl;
2733 {
2734   if (DECL_INTERFACE_KNOWN (decl))
2735     return;
2736
2737   if (DECL_TEMPLATE_INSTANTIATION (decl))
2738     {
2739       DECL_NOT_REALLY_EXTERN (decl) = 1;
2740       if (DECL_IMPLICIT_INSTANTIATION (decl) && flag_implicit_templates)
2741         {
2742           if (TREE_CODE (decl) == FUNCTION_DECL)
2743             {
2744 #ifdef DECL_ONE_ONLY
2745               if (SUPPORTS_ONE_ONLY)
2746                 DECL_ONE_ONLY (decl) = 1;
2747               else
2748 #endif
2749               if (flag_weak)
2750                 DECL_WEAK (decl) = 1;
2751               else
2752                 TREE_PUBLIC (decl) = 0;
2753             }
2754           /* Dynamically initialized vars go into common.  */
2755           else if (DECL_INITIAL (decl) == NULL_TREE
2756                    || DECL_INITIAL (decl) == error_mark_node)
2757             DECL_COMMON (decl) = 1;
2758           else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2759             {
2760               DECL_COMMON (decl) = 1;
2761               DECL_INITIAL (decl) = error_mark_node;
2762             }
2763 #ifdef DECL_ONE_ONLY
2764           else if (SUPPORTS_ONE_ONLY)
2765             DECL_ONE_ONLY (decl) = 1;
2766 #endif
2767           /* Statically initialized vars are weak or comdat, if supported.  */
2768           else if (flag_weak)
2769             DECL_WEAK (decl) = 1;
2770           /* else leave vars public so multiple defs will break.  */
2771         }
2772       else
2773         DECL_NOT_REALLY_EXTERN (decl) = 0;
2774     }
2775   else if (DECL_FUNCTION_MEMBER_P (decl))
2776     {
2777       tree ctype = DECL_CLASS_CONTEXT (decl);
2778       if (CLASSTYPE_INTERFACE_KNOWN (ctype) && ! DECL_ARTIFICIAL (decl))
2779         {
2780           DECL_NOT_REALLY_EXTERN (decl)
2781             = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2782                  || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines));
2783         }
2784 #ifdef DECL_ONE_ONLY
2785       else if (SUPPORTS_ONE_ONLY)
2786         DECL_ONE_ONLY (decl) = 1;
2787 #endif
2788       else if (flag_weak)
2789         DECL_WEAK (decl) = 1;
2790       else
2791         TREE_PUBLIC (decl) = 0;
2792     }
2793   /* tinfo function */
2794   else if (DECL_ARTIFICIAL (decl) && DECL_MUTABLE_P (decl))
2795     {
2796       tree ctype = TREE_TYPE (DECL_NAME (decl));
2797       if (IS_AGGR_TYPE (ctype) && CLASSTYPE_INTERFACE_KNOWN (ctype))
2798         {
2799           DECL_NOT_REALLY_EXTERN (decl)
2800             = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2801                  || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines));
2802         }
2803       else if (TYPE_BUILT_IN (ctype) && ctype == TYPE_MAIN_VARIANT (ctype))
2804         DECL_NOT_REALLY_EXTERN (decl) = 0;
2805 #ifdef DECL_ONE_ONLY
2806       else if (SUPPORTS_ONE_ONLY)
2807         DECL_ONE_ONLY (decl) = 1;
2808 #endif
2809       else if (flag_weak)
2810         DECL_WEAK (decl) = 1;
2811       else
2812         TREE_PUBLIC (decl) = 0;
2813     } 
2814   else if (DECL_C_STATIC (decl))
2815     TREE_PUBLIC (decl) = 0;
2816 #ifdef DECL_ONE_ONLY
2817   else if (SUPPORTS_ONE_ONLY)
2818     DECL_ONE_ONLY (decl) = 1;
2819 #endif
2820   else if (flag_weak)
2821     DECL_WEAK (decl) = 1;
2822   else
2823     TREE_PUBLIC (decl) = 0;
2824
2825   DECL_INTERFACE_KNOWN (decl) = 1;
2826 }
2827
2828 tree
2829 build_cleanup (decl)
2830      tree decl;
2831 {
2832   tree temp;
2833   tree type = TREE_TYPE (decl);
2834
2835   if (TREE_CODE (type) == ARRAY_TYPE)
2836     temp = decl;
2837   else
2838     {
2839       mark_addressable (decl);
2840       temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2841     }
2842   temp = build_delete (TREE_TYPE (temp), temp,
2843                        integer_two_node,
2844                        LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2845   return temp;
2846 }
2847
2848 extern int parse_time, varconst_time;
2849 extern tree pending_templates;
2850
2851 #define TIMEVAR(VAR, BODY)    \
2852 do { int otime = get_run_time (); BODY; VAR += get_run_time () - otime; } while (0)
2853
2854 extern struct obstack permanent_obstack;
2855 extern tree get_id_2 ();
2856
2857 tree
2858 get_sentry (base)
2859      tree base;
2860 {
2861   tree sname = get_id_2 ("__sn", base);
2862   tree sentry = IDENTIFIER_GLOBAL_VALUE (sname);
2863   if (! sentry)
2864     {
2865       push_obstacks (&permanent_obstack, &permanent_obstack);
2866       sentry = build_decl (VAR_DECL, sname, integer_type_node);
2867       TREE_PUBLIC (sentry) = 1;
2868       DECL_ARTIFICIAL (sentry) = 1;
2869       TREE_STATIC (sentry) = 1;
2870       TREE_USED (sentry) = 1;
2871       DECL_COMMON (sentry) = 1;
2872       pushdecl_top_level (sentry);
2873       cp_finish_decl (sentry, NULL_TREE, NULL_TREE, 0, 0);
2874       pop_obstacks ();
2875     }
2876   return sentry;
2877 }
2878
2879 /* This routine is called from the last rule in yyparse ().
2880    Its job is to create all the code needed to initialize and
2881    destroy the global aggregates.  We do the destruction
2882    first, since that way we only need to reverse the decls once.  */
2883
2884 void
2885 finish_file ()
2886 {
2887   extern int lineno;
2888   int start_time, this_time;
2889
2890   tree fnname;
2891   tree vars;
2892   int needs_cleaning = 0, needs_messing_up = 0;
2893
2894   at_eof = 1;
2895
2896   if (flag_detailed_statistics)
2897     dump_tree_statistics ();
2898
2899   /* Bad parse errors.  Just forget about it.  */
2900   if (! global_bindings_p () || current_class_type)
2901     return;
2902
2903   start_time = get_run_time ();
2904
2905   /* Otherwise, GDB can get confused, because in only knows
2906      about source for LINENO-1 lines.  */
2907   lineno -= 1;
2908
2909   interface_unknown = 1;
2910   interface_only = 0;
2911
2912   for (fnname = pending_templates; fnname; fnname = TREE_CHAIN (fnname))
2913     {
2914       tree decl = TREE_VALUE (fnname);
2915       instantiate_decl (decl);
2916     }
2917
2918   /* Push into C language context, because that's all
2919      we'll need here.  */
2920   push_lang_context (lang_name_c);
2921
2922 #if 1
2923   /* The reason for pushing garbage onto the global_binding_level is to
2924      ensure that we can slice out _DECLs which pertain to virtual function
2925      tables.  If the last thing pushed onto the global_binding_level was a
2926      virtual function table, then slicing it out would slice away all the
2927      decls (i.e., we lose the head of the chain).
2928
2929      There are several ways of getting the same effect, from changing the
2930      way that iterators over the chain treat the elements that pertain to
2931      virtual function tables, moving the implementation of this code to
2932      decl.c (where we can manipulate global_binding_level directly),
2933      popping the garbage after pushing it and slicing away the vtable
2934      stuff, or just leaving it alone. */
2935
2936   /* Make last thing in global scope not be a virtual function table.  */
2937 #if 0 /* not yet, should get fixed properly later */
2938   vars = make_type_decl (get_identifier (" @%$#@!"), integer_type_node);
2939 #else
2940   vars = build_decl (TYPE_DECL, get_identifier (" @%$#@!"), integer_type_node);
2941 #endif
2942   DECL_IGNORED_P (vars) = 1;
2943   SET_DECL_ARTIFICIAL (vars);
2944   pushdecl (vars);
2945 #endif
2946
2947   /* Walk to mark the inline functions we need, then output them so
2948      that we can pick up any other tdecls that those routines need. */
2949   walk_vtables ((void (*)())0, finish_prevtable_vardecl);
2950
2951   for (vars = pending_statics; vars; vars = TREE_CHAIN (vars))
2952     {
2953       tree decl = TREE_VALUE (vars);
2954
2955       if (DECL_TEMPLATE_INSTANTIATION (decl)
2956           && ! DECL_IN_AGGR_P (decl))
2957         {
2958           import_export_decl (decl);
2959           DECL_EXTERNAL (decl) = ! DECL_NOT_REALLY_EXTERN (decl);
2960         }
2961     }
2962
2963   vars = static_aggregates;
2964
2965   if (static_ctors || vars || might_have_exceptions_p ())
2966     needs_messing_up = 1;
2967   if (static_dtors)
2968     needs_cleaning = 1;
2969
2970   /* See if we really need the hassle.  */
2971   while (vars && needs_cleaning == 0)
2972     {
2973       tree decl = TREE_VALUE (vars);
2974       tree type = TREE_TYPE (decl);
2975       if (TYPE_NEEDS_DESTRUCTOR (type) && ! TREE_STATIC (vars))
2976         {
2977           needs_cleaning = 1;
2978           break;
2979         }
2980
2981       vars = TREE_CHAIN (vars);
2982     }
2983
2984   if (needs_cleaning == 0)
2985     goto mess_up;
2986
2987   fnname = get_file_function_name ('D');
2988   start_function (void_list_node,
2989                   build_parse_node (CALL_EXPR, fnname, void_list_node,
2990                                     NULL_TREE),
2991                   NULL_TREE, NULL_TREE, 0);
2992   fnname = DECL_ASSEMBLER_NAME (current_function_decl);
2993   store_parm_decls ();
2994
2995   pushlevel (0);
2996   clear_last_expr ();
2997   push_momentary ();
2998   expand_start_bindings (0);
2999
3000   /* These must be done in backward order to destroy,
3001      in which they happen to be!  */
3002   for (vars = static_aggregates; vars; vars = TREE_CHAIN (vars))
3003     {
3004       tree decl = TREE_VALUE (vars);
3005       tree type = TREE_TYPE (decl);
3006       tree temp = TREE_PURPOSE (vars);
3007
3008       if (TYPE_NEEDS_DESTRUCTOR (type) && ! TREE_STATIC (vars)
3009           && ! DECL_EXTERNAL (decl))
3010         {
3011           int protect = (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3012 #ifdef DECL_ONE_ONLY
3013                                                 || DECL_ONE_ONLY (decl)
3014 #endif
3015                                                 || DECL_WEAK (decl)));
3016
3017           temp = build_cleanup (decl);
3018
3019           if (protect)
3020             {
3021               tree sentry = get_sentry (DECL_ASSEMBLER_NAME (decl));
3022               sentry = build_unary_op (PREDECREMENT_EXPR, sentry, 0);
3023               sentry = build_binary_op (EQ_EXPR, sentry, integer_zero_node, 1);
3024               expand_start_cond (sentry, 0);
3025             }
3026
3027           expand_expr_stmt (temp);
3028
3029           if (protect)
3030             expand_end_cond ();
3031         }
3032     }
3033
3034   for (; static_dtors; static_dtors = TREE_CHAIN (static_dtors))
3035     expand_expr_stmt (build_function_call (TREE_VALUE (static_dtors),
3036                                            NULL_TREE));
3037       
3038   expand_end_bindings (getdecls (), 1, 0);
3039   poplevel (1, 0, 0);
3040   pop_momentary ();
3041
3042   finish_function (lineno, 0, 0);
3043
3044   assemble_destructor (IDENTIFIER_POINTER (fnname));
3045
3046   /* if it needed cleaning, then it will need messing up: drop through  */
3047
3048  mess_up:
3049   /* Must do this while we think we are at the top level.  */
3050   vars = nreverse (static_aggregates);
3051   if (needs_messing_up)
3052     {
3053       fnname = get_file_function_name ('I');
3054       start_function (void_list_node,
3055                       build_parse_node (CALL_EXPR, fnname,
3056                                         void_list_node, NULL_TREE),
3057                       NULL_TREE, NULL_TREE, 0);
3058       fnname = DECL_ASSEMBLER_NAME (current_function_decl);
3059       store_parm_decls ();
3060
3061       pushlevel (0);
3062       clear_last_expr ();
3063       push_momentary ();
3064       expand_start_bindings (0);
3065
3066       if (might_have_exceptions_p ())
3067         register_exception_table ();
3068
3069       while (vars)
3070         {
3071           extern int temp_slot_level;
3072           extern int target_temp_slot_level; 
3073           tree decl = TREE_VALUE (vars);
3074           tree init = TREE_PURPOSE (vars);
3075           tree old_cleanups = cleanups_this_call;
3076           int old_temp_level = target_temp_slot_level;
3077           push_temp_slots ();
3078           push_temp_slots ();
3079           target_temp_slot_level = temp_slot_level;
3080
3081           /* If this was a static attribute within some function's scope,
3082              then don't initialize it here.  Also, don't bother
3083              with initializers that contain errors.  */
3084           if (TREE_STATIC (vars)
3085               || DECL_EXTERNAL (decl)
3086               || (init && TREE_CODE (init) == TREE_LIST
3087                   && value_member (error_mark_node, init)))
3088             goto next_mess;
3089
3090           if (TREE_CODE (decl) == VAR_DECL)
3091             {
3092           int protect = (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3093 #ifdef DECL_ONE_ONLY
3094                                                 || DECL_ONE_ONLY (decl)
3095 #endif
3096                                                 || DECL_WEAK (decl)));
3097
3098               /* Set these global variables so that GDB at least puts
3099                  us near the declaration which required the initialization.  */
3100               input_filename = DECL_SOURCE_FILE (decl);
3101               lineno = DECL_SOURCE_LINE (decl);
3102               emit_note (input_filename, lineno);
3103
3104               /* 9.5p5: The initializer of a static member of a class has
3105                  the same access rights as a member function.  */
3106               DECL_CLASS_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3107               DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3108
3109               if (protect)
3110                 {
3111                   tree sentry = get_sentry (DECL_ASSEMBLER_NAME (decl));
3112                   sentry = build_unary_op (PREINCREMENT_EXPR, sentry, 0);
3113                   sentry = build_binary_op
3114                     (EQ_EXPR, sentry, integer_one_node, 1);
3115                   expand_start_cond (sentry, 0);
3116                 }
3117
3118               if (IS_AGGR_TYPE (TREE_TYPE (decl))
3119                   || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3120                 expand_aggr_init (decl, init, 0, 0);
3121               else if (TREE_CODE (init) == TREE_VEC)
3122                 {
3123                   expand_expr (expand_vec_init (decl, TREE_VEC_ELT (init, 0),
3124                                                 TREE_VEC_ELT (init, 1),
3125                                                 TREE_VEC_ELT (init, 2), 0),
3126                                const0_rtx, VOIDmode, 0);
3127                 }
3128               else
3129                 expand_assignment (decl, init, 0, 0);
3130
3131               if (protect)
3132                 expand_end_cond ();
3133
3134               DECL_CLASS_CONTEXT (current_function_decl) = NULL_TREE;
3135               DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3136             }
3137           else if (decl == error_mark_node)
3138             ;
3139           else my_friendly_abort (22);
3140
3141           /* Cleanup any temporaries needed for the initial value.  */
3142           expand_cleanups_to (old_cleanups);
3143         next_mess:
3144           pop_temp_slots ();
3145           pop_temp_slots ();
3146           target_temp_slot_level = old_temp_level;
3147
3148           vars = TREE_CHAIN (vars);
3149         }
3150
3151       for (; static_ctors; static_ctors = TREE_CHAIN (static_ctors))
3152         expand_expr_stmt (build_function_call (TREE_VALUE (static_ctors),
3153                                                NULL_TREE));
3154       
3155       expand_end_bindings (getdecls (), 1, 0);
3156       poplevel (1, 0, 0);
3157       pop_momentary ();
3158
3159       finish_function (lineno, 0, 0);
3160       assemble_constructor (IDENTIFIER_POINTER (fnname));
3161     }
3162
3163   expand_builtin_throw ();
3164
3165   permanent_allocation (1);
3166
3167   /* Done with C language context needs.  */
3168   pop_lang_context ();
3169
3170   /* Now write out any static class variables (which may have since
3171      learned how to be initialized).  */
3172   while (pending_statics)
3173     {
3174       tree decl = TREE_VALUE (pending_statics);
3175
3176       if (TREE_USED (decl) == 1
3177           || TREE_READONLY (decl) == 0
3178           || DECL_INITIAL (decl) == 0)
3179         {
3180           DECL_DEFER_OUTPUT (decl) = 0;
3181           rest_of_decl_compilation (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), 1, 1);
3182         }
3183       pending_statics = TREE_CHAIN (pending_statics);
3184     }
3185
3186   this_time = get_run_time ();
3187   parse_time -= this_time - start_time;
3188   varconst_time += this_time - start_time;
3189
3190   start_time = get_run_time ();
3191
3192   if (flag_handle_signatures)
3193     walk_sigtables ((void (*)())0, finish_sigtable_vardecl);
3194
3195   for (fnname = saved_inlines; fnname; fnname = TREE_CHAIN (fnname))
3196     {
3197       tree decl = TREE_VALUE (fnname);
3198       import_export_decl (decl);
3199     }
3200
3201   /* Now write out inline functions which had their addresses taken and
3202      which were not declared virtual and which were not declared `extern
3203      inline'.  */
3204   {
3205     int reconsider = 1;         /* More may be referenced; check again */
3206
3207     while (reconsider)
3208       {
3209         tree *p = &saved_inlines;
3210         reconsider = 0;
3211
3212         /* We need to do this each time so that newly completed template
3213            types don't wind up at the front of the list.  Sigh.  */
3214         vars = build_decl (TYPE_DECL, make_anon_name (), integer_type_node);
3215         DECL_IGNORED_P (vars) = 1;
3216         SET_DECL_ARTIFICIAL (vars);
3217         pushdecl (vars);
3218
3219         reconsider |= walk_vtables ((void (*)())0, finish_vtable_vardecl);
3220
3221         while (*p)
3222           {
3223             tree decl = TREE_VALUE (*p);
3224
3225             if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3226                 && TREE_USED (decl)
3227                 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3228               {
3229                 if (DECL_MUTABLE_P (decl))
3230                   synthesize_tinfo_fn (decl);
3231                 else
3232                   synthesize_method (decl);
3233                 reconsider = 1;
3234               }
3235
3236             if (decl != TREE_VALUE (*p))
3237               ;
3238             else if (TREE_ASM_WRITTEN (decl)
3239                      || (DECL_SAVED_INSNS (decl) == 0
3240                          && ! DECL_ARTIFICIAL (decl)))
3241               *p = TREE_CHAIN (*p);
3242             else
3243               p = &TREE_CHAIN (*p);
3244           }
3245       }
3246
3247     reconsider = 1;             /* More may be referenced; check again */
3248     while (reconsider)
3249       {
3250         tree *p = &saved_inlines;
3251         reconsider = 0;
3252
3253         while (*p)
3254           {
3255             tree decl = TREE_VALUE (*p);
3256
3257             if (TREE_ASM_WRITTEN (decl) || DECL_SAVED_INSNS (decl) == 0)
3258               *p = TREE_CHAIN (*p);
3259             else if ((TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
3260                      || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
3261                      || flag_keep_inline_functions)
3262               {
3263                 if (DECL_NOT_REALLY_EXTERN (decl))
3264                   {
3265                     DECL_EXTERNAL (decl) = 0;
3266                     reconsider = 1;
3267                     temporary_allocation ();
3268                     output_inline_function (decl);
3269                     permanent_allocation (1);
3270                   }
3271
3272                 *p = TREE_CHAIN (*p);
3273               }
3274             else
3275               p = &TREE_CHAIN (*p);
3276           }
3277       }
3278   }
3279
3280   /* Now delete from the chain of variables all virtual function tables.
3281      We output them all ourselves, because each will be treated specially.  */
3282
3283   walk_vtables ((void (*)())0, prune_vtable_vardecl);
3284
3285   for (vars = getdecls (); vars; vars = TREE_CHAIN (vars))
3286     {
3287       if (TREE_CODE (vars) == FUNCTION_DECL
3288           && ! DECL_INTERFACE_KNOWN (vars)
3289           && DECL_C_STATIC (vars))
3290         TREE_PUBLIC (vars) = 0;
3291     }
3292
3293   if (might_have_exceptions_p ())
3294     emit_exception_table ();
3295
3296   if (write_virtuals == 2)
3297     {
3298       /* Now complain about an virtual function tables promised
3299          but not delivered.  */
3300       while (pending_vtables)
3301         {
3302           if (TREE_PURPOSE (pending_vtables) == NULL_TREE)
3303             error ("virtual function table for `%s' not defined",
3304                    IDENTIFIER_POINTER (TREE_VALUE (pending_vtables)));
3305           pending_vtables = TREE_CHAIN (pending_vtables);
3306         }
3307     }
3308
3309   finish_repo ();
3310
3311   this_time = get_run_time ();
3312   parse_time -= this_time - start_time;
3313   varconst_time += this_time - start_time;
3314
3315   if (flag_detailed_statistics)
3316     dump_time_statistics ();
3317 }
3318
3319 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3320    expr.  Since it was parsed like a type, we need to wade through and fix
3321    that.  Unfortunately, since operator() is left-associative, we can't use
3322    tail recursion.  In the above example, TYPE is `A', and DECL is
3323    `()()()()()'.
3324
3325    Maybe this shouldn't be recursive, but how often will it actually be
3326    used?  (jason) */
3327 tree
3328 reparse_absdcl_as_expr (type, decl)
3329      tree type, decl;
3330 {
3331   /* do build_functional_cast (type, NULL_TREE) at bottom */
3332   if (TREE_OPERAND (decl, 0) == NULL_TREE)
3333     return build_functional_cast (type, NULL_TREE);
3334
3335   /* recurse */
3336   decl = reparse_decl_as_expr (type, TREE_OPERAND (decl, 0));
3337
3338   decl = build_x_function_call (decl, NULL_TREE, current_class_decl);
3339
3340   if (TREE_CODE (decl) == CALL_EXPR && TREE_TYPE (decl) != void_type_node)
3341     decl = require_complete_type (decl);
3342
3343   return decl;
3344 }
3345
3346 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3347    out to be an expr.  Since it was parsed like a type, we need to wade
3348    through and fix that.  Since casts are right-associative, we are
3349    reversing the order, so we don't have to recurse.
3350
3351    In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3352    `1'.  */
3353 tree
3354 reparse_absdcl_as_casts (decl, expr)
3355      tree decl, expr;
3356 {
3357   tree type;
3358   
3359   if (TREE_CODE (expr) == CONSTRUCTOR)
3360     {
3361       type = groktypename (TREE_VALUE (TREE_OPERAND (decl, 1)));
3362       decl = TREE_OPERAND (decl, 0);
3363
3364       if (IS_SIGNATURE (type))
3365         {
3366           error ("cast specifies signature type");
3367           return error_mark_node;
3368         }
3369
3370       expr = digest_init (type, expr, (tree *) 0);
3371       if (TREE_CODE (type) == ARRAY_TYPE && TYPE_SIZE (type) == 0)
3372         {
3373           int failure = complete_array_type (type, expr, 1);
3374           if (failure)
3375             my_friendly_abort (78);
3376         }
3377     }
3378
3379   while (decl)
3380     {
3381       type = groktypename (TREE_VALUE (TREE_OPERAND (decl, 1)));
3382       decl = TREE_OPERAND (decl, 0);
3383       expr = build_c_cast (type, expr, 0);
3384     }
3385
3386   return expr;
3387 }
3388
3389 /* Given plain tree nodes for an expression, build up the full semantics. */
3390
3391 tree
3392 build_expr_from_tree (t)
3393      tree t;
3394 {
3395   if (t == NULL_TREE || t == error_mark_node)
3396     return t;
3397
3398   switch (TREE_CODE (t))
3399     {
3400     case IDENTIFIER_NODE:
3401       return do_identifier (t, 0);
3402
3403     case LOOKUP_EXPR:
3404       if (LOOKUP_EXPR_GLOBAL (t))
3405         return do_scoped_id (TREE_OPERAND (t, 0), 0);
3406       else
3407         return do_identifier (TREE_OPERAND (t, 0), 0);
3408
3409     case INDIRECT_REF:
3410       return build_x_indirect_ref
3411         (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3412
3413     case CAST_EXPR:
3414       return build_functional_cast
3415         (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3416
3417     case REINTERPRET_CAST_EXPR:
3418       return build_reinterpret_cast
3419         (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3420
3421     case PREDECREMENT_EXPR:
3422     case PREINCREMENT_EXPR:
3423     case POSTDECREMENT_EXPR:
3424     case POSTINCREMENT_EXPR:
3425     case NEGATE_EXPR:
3426     case BIT_NOT_EXPR:
3427     case ABS_EXPR:
3428     case TRUTH_NOT_EXPR:
3429     case ADDR_EXPR:
3430     case CONVERT_EXPR:      /* Unary + */
3431       if (TREE_TYPE (t))
3432         return t;
3433       return build_x_unary_op (TREE_CODE (t),
3434                                build_expr_from_tree (TREE_OPERAND (t, 0)));
3435
3436     case PLUS_EXPR:
3437     case MINUS_EXPR:
3438     case MULT_EXPR:
3439     case TRUNC_DIV_EXPR:
3440     case CEIL_DIV_EXPR:
3441     case FLOOR_DIV_EXPR:
3442     case ROUND_DIV_EXPR:
3443     case EXACT_DIV_EXPR:
3444     case BIT_AND_EXPR:
3445     case BIT_ANDTC_EXPR:
3446     case BIT_IOR_EXPR:
3447     case BIT_XOR_EXPR:
3448     case TRUNC_MOD_EXPR:
3449     case FLOOR_MOD_EXPR:
3450     case TRUTH_ANDIF_EXPR:
3451     case TRUTH_ORIF_EXPR:
3452     case TRUTH_AND_EXPR:
3453     case TRUTH_OR_EXPR:
3454     case RSHIFT_EXPR:
3455     case LSHIFT_EXPR:
3456     case RROTATE_EXPR:
3457     case LROTATE_EXPR:
3458     case EQ_EXPR:
3459     case NE_EXPR:
3460     case MAX_EXPR:
3461     case MIN_EXPR:
3462     case LE_EXPR:
3463     case GE_EXPR:
3464     case LT_EXPR:
3465     case GT_EXPR:
3466     case MEMBER_REF:
3467       return build_x_binary_op
3468         (TREE_CODE (t), 
3469          build_expr_from_tree (TREE_OPERAND (t, 0)),
3470          build_expr_from_tree (TREE_OPERAND (t, 1)));
3471
3472     case DOTSTAR_EXPR:
3473       return build_m_component_ref
3474         (build_expr_from_tree (TREE_OPERAND (t, 0)),
3475          build_expr_from_tree (TREE_OPERAND (t, 1)));
3476
3477     case SCOPE_REF:
3478       return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3479
3480     case ARRAY_REF:
3481       if (TREE_OPERAND (t, 0) == NULL_TREE)
3482         /* new-type-id */
3483         return build_parse_node (ARRAY_REF, NULL_TREE,
3484                                  build_expr_from_tree (TREE_OPERAND (t, 1)));
3485       return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3486                               build_expr_from_tree (TREE_OPERAND (t, 1)));
3487
3488     case SIZEOF_EXPR:
3489       {
3490         tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3491         if (TREE_CODE_CLASS (TREE_CODE (r)) != 't')
3492           r = TREE_TYPE (r);
3493         return c_sizeof (r);
3494       }
3495
3496     case MODOP_EXPR:
3497       return build_x_modify_expr
3498         (build_expr_from_tree (TREE_OPERAND (t, 0)),
3499          TREE_CODE (TREE_OPERAND (t, 1)),
3500          build_expr_from_tree (TREE_OPERAND (t, 2)));
3501
3502     case ARROW_EXPR:
3503       return build_x_arrow
3504         (build_expr_from_tree (TREE_OPERAND (t, 0)));
3505
3506     case NEW_EXPR:
3507       return build_new
3508         (build_expr_from_tree (TREE_OPERAND (t, 0)),
3509          build_expr_from_tree (TREE_OPERAND (t, 1)),
3510          build_expr_from_tree (TREE_OPERAND (t, 2)),
3511          NEW_EXPR_USE_GLOBAL (t));
3512
3513     case DELETE_EXPR:
3514       return delete_sanity
3515         (build_expr_from_tree (TREE_OPERAND (t, 0)),
3516          build_expr_from_tree (TREE_OPERAND (t, 1)),
3517          DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3518
3519     case COMPOUND_EXPR:
3520       if (TREE_OPERAND (t, 1) == NULL_TREE)
3521         return build_x_compound_expr
3522           (build_expr_from_tree (TREE_OPERAND (t, 0)));
3523       else
3524         my_friendly_abort (42);
3525
3526     case METHOD_CALL_EXPR:
3527       if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3528         {
3529           tree ref = TREE_OPERAND (t, 0);
3530           return build_scoped_method_call
3531             (build_expr_from_tree (TREE_OPERAND (t, 1)),
3532              build_expr_from_tree (TREE_OPERAND (ref, 0)),
3533              TREE_OPERAND (ref, 1),
3534              build_expr_from_tree (TREE_OPERAND (t, 2)));
3535         }
3536       return build_method_call
3537         (build_expr_from_tree (TREE_OPERAND (t, 1)),
3538          TREE_OPERAND (t, 0),
3539          build_expr_from_tree (TREE_OPERAND (t, 2)),
3540          NULL_TREE, LOOKUP_NORMAL);
3541
3542     case CALL_EXPR:
3543       if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3544         {
3545           tree ref = TREE_OPERAND (t, 0);
3546           return build_member_call
3547             (build_expr_from_tree (TREE_OPERAND (ref, 0)),
3548              TREE_OPERAND (ref, 1),
3549              build_expr_from_tree (TREE_OPERAND (t, 1)));
3550         }
3551       else
3552         {
3553           tree name = TREE_OPERAND (t, 0);
3554           if (! really_overloaded_fn (name))
3555             name = build_expr_from_tree (name);
3556           return build_x_function_call
3557             (name, build_expr_from_tree (TREE_OPERAND (t, 1)),
3558              current_class_decl);
3559         }
3560
3561     case COND_EXPR:
3562       return build_x_conditional_expr
3563         (build_expr_from_tree (TREE_OPERAND (t, 0)),
3564          build_expr_from_tree (TREE_OPERAND (t, 1)),
3565          build_expr_from_tree (TREE_OPERAND (t, 2)));
3566
3567     case TREE_LIST:
3568       {
3569         tree purpose, value, chain;
3570
3571         if (t == void_list_node)
3572           return t;
3573
3574         purpose = TREE_PURPOSE (t);
3575         if (purpose)
3576           purpose = build_expr_from_tree (purpose);
3577         value = TREE_VALUE (t);
3578         if (value)
3579           value = build_expr_from_tree (value);
3580         chain = TREE_CHAIN (t);
3581         if (chain && chain != void_type_node)
3582           chain = build_expr_from_tree (chain);
3583         return tree_cons (purpose, value, chain);
3584       }
3585
3586     case COMPONENT_REF:
3587       return build_component_ref
3588         (build_expr_from_tree (TREE_OPERAND (t, 0)),
3589          TREE_OPERAND (t, 1), NULL_TREE, 1);
3590
3591     case THROW_EXPR:
3592       return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3593
3594     default:
3595       return t;
3596     }
3597 }
3598
3599 /* This is something of the form `int (*a)++' that has turned out to be an
3600    expr.  It was only converted into parse nodes, so we need to go through
3601    and build up the semantics.  Most of the work is done by
3602    build_expr_from_tree, above.
3603
3604    In the above example, TYPE is `int' and DECL is `*a'.  */
3605 tree
3606 reparse_decl_as_expr (type, decl)
3607      tree type, decl;
3608 {
3609   decl = build_expr_from_tree (decl);
3610   if (type)
3611     return build_functional_cast (type, build_tree_list (NULL_TREE, decl));
3612   else
3613     return decl;
3614 }
3615
3616 /* This is something of the form `int (*a)' that has turned out to be a
3617    decl.  It was only converted into parse nodes, so we need to do the
3618    checking that make_{pointer,reference}_declarator do. */
3619
3620 tree
3621 finish_decl_parsing (decl)
3622      tree decl;
3623 {
3624   extern int current_class_depth;
3625   
3626   switch (TREE_CODE (decl))
3627     {
3628     case IDENTIFIER_NODE:
3629       return decl;
3630     case INDIRECT_REF:
3631       return make_pointer_declarator
3632         (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
3633     case ADDR_EXPR:
3634       return make_reference_declarator
3635         (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
3636     case BIT_NOT_EXPR:
3637       TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
3638       return decl;
3639     case SCOPE_REF:
3640       push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
3641       TREE_COMPLEXITY (decl) = current_class_depth;
3642       return decl;
3643     case ARRAY_REF:
3644       TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
3645       return decl;
3646     default:
3647       my_friendly_abort (5);
3648       return NULL_TREE;
3649     }
3650 }
3651
3652 tree
3653 check_cp_case_value (value)
3654      tree value;
3655 {
3656   if (value == NULL_TREE)
3657     return value;
3658
3659   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
3660   STRIP_TYPE_NOPS (value);
3661
3662   if (TREE_READONLY_DECL_P (value))
3663     {
3664       value = decl_constant_value (value);
3665       STRIP_TYPE_NOPS (value);
3666     }
3667   value = fold (value);
3668
3669   if (TREE_CODE (value) != INTEGER_CST
3670       && value != error_mark_node)
3671     {
3672       cp_error ("case label `%E' does not reduce to an integer constant",
3673                 value);
3674       value = error_mark_node;
3675     }
3676   else
3677     /* Promote char or short to int.  */
3678     value = default_conversion (value);
3679
3680   constant_expression_warning (value);
3681
3682   return value;
3683 }
3684
3685 tree current_namespace;
3686
3687 /* Get the inner part of a namespace id.  It doesn't have any prefix, nor
3688    postfix.  Returns 0 if in global namespace.  */
3689 tree
3690 get_namespace_id ()
3691 {
3692   tree x = current_namespace;
3693   if (x)
3694     x = TREE_PURPOSE (x);
3695   return x;
3696 }
3697
3698 /* Build up a DECL_ASSEMBLER_NAME for NAME in the current namespace. */
3699 tree
3700 current_namespace_id (name)
3701      tree name;
3702 {
3703   tree old_id = get_namespace_id ();
3704   char *buf;
3705
3706   /* Global names retain old encoding. */
3707   if (! old_id)
3708     return name;
3709
3710   buf = (char *) alloca (8 + IDENTIFIER_LENGTH (old_id)
3711                          + IDENTIFIER_LENGTH (name));
3712   sprintf (buf, "__ns_%s_%s", IDENTIFIER_POINTER (old_id),
3713            IDENTIFIER_POINTER (name));
3714   return get_identifier (buf);
3715 }
3716
3717 void
3718 do_namespace_alias (alias, namespace)
3719      tree alias, namespace;
3720 {
3721   sorry ("namespace alias");
3722 }
3723
3724 void
3725 do_toplevel_using_decl (decl)
3726      tree decl;
3727 {
3728   if (decl == NULL_TREE || decl == error_mark_node)
3729     return;
3730
3731   if (TREE_CODE (decl) == SCOPE_REF)
3732     decl = resolve_scope_to_name (NULL_TREE, decl);
3733
3734   /* Is this the right way to do an id list? */
3735   if (TREE_CODE (decl) != TREE_LIST)
3736     {
3737       pushdecl (decl);
3738     }
3739   else
3740     while (decl)
3741       {
3742         pushdecl (TREE_VALUE (decl));
3743         decl = TREE_CHAIN (decl);
3744       }
3745 }
3746
3747 tree
3748 do_class_using_decl (decl)
3749      tree decl;
3750 {
3751   tree name, value;
3752
3753   if (TREE_CODE (decl) != SCOPE_REF)
3754     {
3755       cp_error ("using-declaration for non-member at class scope");
3756       return NULL_TREE;
3757     }
3758   name = TREE_OPERAND (decl, 1);
3759   if (TREE_CODE (name) == BIT_NOT_EXPR)
3760     {
3761       cp_error ("using-declaration for destructor");
3762       return NULL_TREE;
3763     }
3764
3765   value = build_lang_field_decl (USING_DECL, name, void_type_node);
3766   DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
3767   return value;
3768 }
3769
3770 void
3771 do_using_directive (namespace)
3772      tree namespace;
3773 {
3774   sorry ("using directive");
3775 }
3776
3777 void
3778 check_default_args (x)
3779      tree x;
3780 {
3781   tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
3782   int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
3783   for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
3784     {
3785       if (TREE_PURPOSE (arg))
3786         saw_def = 1;
3787       else if (saw_def)
3788         {
3789           cp_error ("default argument missing for parameter %P of `%#D'",
3790                     i, x);
3791           break;
3792         }
3793     }
3794 }
3795
3796 void
3797 mark_used (decl)
3798      tree decl;
3799 {
3800   TREE_USED (decl) = 1;
3801   if (current_template_parms)
3802     return;
3803   assemble_external (decl);
3804   if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
3805     instantiate_decl (decl);
3806 }