OSDN Git Service

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