OSDN Git Service

cp:
[pf3gnuchains/gcc-fork.git] / gcc / cp / decl2.c
1 /* Process declarations and variables for C compiler.
2    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001 Free Software Foundation, Inc.
4    Hacked by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23
24 /* Process declarations and symbol lookup for C front end.
25    Also constructs types; the standard scalar types at initialization,
26    and structure, union, array and enum types when they are declared.  */
27
28 /* ??? not all decl nodes are given the most useful possible
29    line numbers.  For example, the CONST_DECLs for enum values.  */
30
31 #include "config.h"
32 #include "system.h"
33 #include "tree.h"
34 #include "rtl.h"
35 #include "expr.h"
36 #include "flags.h"
37 #include "cp-tree.h"
38 #include "decl.h"
39 #include "lex.h"
40 #include "output.h"
41 #include "except.h"
42 #include "toplev.h"
43 #include "dwarf2out.h"
44 #include "dwarfout.h"
45 #include "ggc.h"
46 #include "timevar.h"
47 #include "cpplib.h"
48 extern cpp_reader *parse_in;
49
50 /* This structure contains information about the initializations
51    and/or destructions required for a particular priority level.  */
52 typedef struct priority_info_s {
53   /* Non-zero if there have been any initializations at this priority
54      throughout the translation unit.  */
55   int initializations_p;
56   /* Non-zero if there have been any destructions at this priority
57      throughout the translation unit.  */
58   int destructions_p;
59 } *priority_info;
60
61 static void mark_vtable_entries PARAMS ((tree));
62 static void grok_function_init PARAMS ((tree, tree));
63 static int finish_vtable_vardecl PARAMS ((tree *, void *));
64 static int prune_vtable_vardecl PARAMS ((tree *, void *));
65 static int is_namespace_ancestor PARAMS ((tree, tree));
66 static void add_using_namespace PARAMS ((tree, tree, int));
67 static tree ambiguous_decl PARAMS ((tree, tree, tree,int));
68 static tree build_anon_union_vars PARAMS ((tree, tree*, int, int));
69 static int acceptable_java_type PARAMS ((tree));
70 static void output_vtable_inherit PARAMS ((tree));
71 static tree start_objects PARAMS ((int, int));
72 static void finish_objects PARAMS ((int, int, tree));
73 static tree merge_functions PARAMS ((tree, tree));
74 static tree decl_namespace PARAMS ((tree));
75 static tree validate_nonmember_using_decl PARAMS ((tree, tree *, tree *));
76 static void do_nonmember_using_decl PARAMS ((tree, tree, tree, tree,
77                                            tree *, tree *));
78 static tree start_static_storage_duration_function PARAMS ((void));
79 static void finish_static_storage_duration_function PARAMS ((tree));
80 static priority_info get_priority_info PARAMS ((int));
81 static void do_static_initialization PARAMS ((tree, tree));
82 static void do_static_destruction PARAMS ((tree));
83 static tree start_static_initialization_or_destruction PARAMS ((tree, int));
84 static void finish_static_initialization_or_destruction PARAMS ((tree));
85 static void generate_ctor_or_dtor_function PARAMS ((int, int));
86 static int generate_ctor_and_dtor_functions_for_priority
87                                   PARAMS ((splay_tree_node, void *));
88 static tree prune_vars_needing_no_initialization PARAMS ((tree));
89 static void write_out_vars PARAMS ((tree));
90 static void import_export_class PARAMS ((tree));
91 static tree key_method PARAMS ((tree));
92 static int compare_options PARAMS ((const PTR, const PTR));
93 static tree get_guard_bits PARAMS ((tree));
94
95 /* A list of static class variables.  This is needed, because a
96    static class variable can be declared inside the class without
97    an initializer, and then initialized, staticly, outside the class.  */
98 static varray_type pending_statics;
99 #define pending_statics_used \
100   (pending_statics ? pending_statics->elements_used : 0)
101
102 /* A list of functions which were declared inline, but which we
103    may need to emit outline anyway.  */
104 static varray_type deferred_fns;
105 #define deferred_fns_used \
106   (deferred_fns ? deferred_fns->elements_used : 0)
107
108 /* Same, but not reset.  Local temp variables and global temp variables
109    can have the same name.  */
110 static int global_temp_name_counter;
111
112 /* Flag used when debugging spew.c */
113
114 extern int spew_debug;
115
116 /* Nonzero if we're done parsing and into end-of-file activities.  */
117
118 int at_eof;
119
120 /* Functions called along with real static constructors and destructors.  */
121
122 tree static_ctors;
123 tree static_dtors;
124
125 /* The :: namespace. */
126
127 tree global_namespace;
128 \f
129 /* C (and C++) language-specific option variables.  */
130
131 /* Nonzero means don't recognize the keyword `asm'.  */
132
133 int flag_no_asm;
134
135 /* Nonzero means don't recognize any extension keywords.  */
136
137 int flag_no_gnu_keywords;
138
139 /* Nonzero means do some things the same way PCC does.  Only provided so
140    the compiler will link.  */
141
142 int flag_traditional;
143
144 /* Nonzero means to treat bitfields as unsigned unless they say `signed'.  */
145
146 int flag_signed_bitfields = 1;
147
148 /* Nonzero means enable obscure standard features and disable GNU
149    extensions that might cause standard-compliant code to be
150    miscompiled.  */
151
152 int flag_ansi;
153
154 /* Nonzero means do emit exported implementations of functions even if
155    they can be inlined.  */
156
157 int flag_implement_inlines = 1;
158
159 /* Nonzero means do emit exported implementations of templates, instead of
160    multiple static copies in each file that needs a definition.  */
161
162 int flag_external_templates;
163
164 /* Nonzero means that the decision to emit or not emit the implementation of a
165    template depends on where the template is instantiated, rather than where
166    it is defined.  */
167
168 int flag_alt_external_templates;
169
170 /* Nonzero means that implicit instantiations will be emitted if needed.  */
171
172 int flag_implicit_templates = 1;
173
174 /* Nonzero means that implicit instantiations of inline templates will be
175    emitted if needed, even if instantiations of non-inline templates
176    aren't.  */
177
178 int flag_implicit_inline_templates = 1;
179
180 /* Nonzero means warn about implicit declarations.  */
181
182 int warn_implicit = 1;
183
184 /* Nonzero means warn about usage of long long when `-pedantic'.  */
185
186 int warn_long_long = 1;
187
188 /* Nonzero means warn when all ctors or dtors are private, and the class
189    has no friends.  */
190
191 int warn_ctor_dtor_privacy = 1;
192
193 /* True if we want to implement vtables using "thunks".
194    The default is off.  */
195
196 int flag_vtable_thunks = 1;
197
198 /* Nonzero means generate separate instantiation control files and juggle
199    them at link time.  */
200
201 int flag_use_repository;
202
203 /* Nonzero if we want to issue diagnostics that the standard says are not
204    required.  */
205
206 int flag_optional_diags = 1;
207
208 /* Nonzero means give string constants the type `const char *', as mandated
209    by the standard.  */
210
211 int flag_const_strings = 1;
212
213 /* Nonzero means warn about deprecated conversion from string constant to
214    `char *'.  */
215
216 int warn_write_strings;
217
218 /* Nonzero means warn about pointer casts that can drop a type qualifier
219    from the pointer target type.  */
220
221 int warn_cast_qual;
222
223 /* Nonzero means warn about sizeof(function) or addition/subtraction
224    of function pointers.  */
225
226 int warn_pointer_arith = 1;
227
228 /* Nonzero means warn for any function def without prototype decl.  */
229
230 int warn_missing_prototypes;
231
232 /* Nonzero means warn about multiple (redundant) decls for the same single
233    variable or function.  */
234
235 int warn_redundant_decls;
236
237 /* Warn if initializer is not completely bracketed.  */
238
239 int warn_missing_braces;
240
241 /* Warn about comparison of signed and unsigned values.  */
242
243 int warn_sign_compare;
244
245 /* Warn about testing equality of floating point numbers. */
246
247 int warn_float_equal = 0;
248
249 /* Warn about functions which might be candidates for format attributes.  */
250
251 int warn_missing_format_attribute;
252
253 /* Warn about a subscript that has type char.  */
254
255 int warn_char_subscripts;
256
257 /* Warn if a type conversion is done that might have confusing results.  */
258
259 int warn_conversion;
260
261 /* Warn if adding () is suggested.  */
262
263 int warn_parentheses;
264
265 /* Non-zero means warn in function declared in derived class has the
266    same name as a virtual in the base class, but fails to match the
267    type signature of any virtual function in the base class.  */
268
269 int warn_overloaded_virtual;
270
271 /* Non-zero means warn when declaring a class that has a non virtual
272    destructor, when it really ought to have a virtual one.  */
273
274 int warn_nonvdtor;
275
276 /* Non-zero means warn when the compiler will reorder code.  */
277
278 int warn_reorder;
279
280 /* Non-zero means warn when synthesis behavior differs from Cfront's.  */
281
282 int warn_synth;
283
284 /* Non-zero means warn when we convert a pointer to member function
285    into a pointer to (void or function).  */
286
287 int warn_pmf2ptr = 1;
288
289 /* Nonzero means warn about violation of some Effective C++ style rules.  */
290
291 int warn_ecpp;
292
293 /* Nonzero means warn where overload resolution chooses a promotion from
294    unsigned to signed over a conversion to an unsigned of the same size.  */
295
296 int warn_sign_promo;
297
298 /* Nonzero means warn when an old-style cast is used.  */
299
300 int warn_old_style_cast;
301
302 /* Warn about #pragma directives that are not recognised.  */      
303
304 int warn_unknown_pragmas; /* Tri state variable.  */  
305
306 /* Nonzero means warn about use of multicharacter literals.  */
307
308 int warn_multichar = 1;
309
310 /* Nonzero means warn when non-templatized friend functions are
311    declared within a template */
312
313 int warn_nontemplate_friend = 1;
314
315 /* Nonzero means complain about deprecated features.  */
316
317 int warn_deprecated = 1;
318
319 /* Nonzero means `$' can be in an identifier.  */
320
321 #ifndef DOLLARS_IN_IDENTIFIERS
322 #define DOLLARS_IN_IDENTIFIERS 1
323 #endif
324 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
325
326 /* Nonzero means allow Microsoft extensions without a pedwarn.  */
327
328 int flag_ms_extensions;
329
330 /* C++ specific flags.  */   
331
332 /* Nonzero means we should attempt to elide constructors when possible.  */
333
334 int flag_elide_constructors = 1;
335
336 /* Nonzero means that member functions defined in class scope are
337    inline by default.  */
338
339 int flag_default_inline = 1;
340
341 /* Controls whether compiler generates 'type descriptor' that give
342    run-time type information.  */
343
344 int flag_rtti = 1;
345
346 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
347    objects.  */
348
349 int flag_huge_objects;
350
351 /* Nonzero if we want to conserve space in the .o files.  We do this
352    by putting uninitialized data and runtime initialized data into
353    .common instead of .data at the expense of not flagging multiple
354    definitions.  */
355
356 int flag_conserve_space;
357
358 /* Nonzero if we want to obey access control semantics.  */
359
360 int flag_access_control = 1;
361
362 /* Nonzero if we want to understand the operator names, i.e. 'bitand'.  */
363
364 int flag_operator_names = 1;
365
366 /* Nonzero if we want to check the return value of new and avoid calling
367    constructors if it is a null pointer.  */
368
369 int flag_check_new;
370
371 /* Nonnull if we want to dump class heirarchies.  */
372
373 const char *flag_dump_class_layout;
374
375 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
376    initialization variables.
377    0: Old rules, set by -fno-for-scope.
378    2: New ISO rules, set by -ffor-scope.
379    1: Try to implement new ISO rules, but with backup compatibility
380    (and warnings).  This is the default, for now.  */
381
382 int flag_new_for_scope = 1;
383
384 /* Nonzero if we want to emit defined symbols with common-like linkage as
385    weak symbols where possible, in order to conform to C++ semantics.
386    Otherwise, emit them as local symbols.  */
387
388 int flag_weak = 1;
389
390 /* Nonzero to use __cxa_atexit, rather than atexit, to register
391    destructors for local statics and global objects.  */
392
393 int flag_use_cxa_atexit;
394
395 /* Nonzero to not ignore namespace std. */
396
397 int flag_honor_std = 1;
398
399 /* Nonzero if we should expand functions calls inline at the tree
400    level, rather than at the RTL level.  */
401
402 int flag_inline_trees = 0;
403
404 /* Maximum template instantiation depth.  This limit is rather
405    arbitrary, but it exists to limit the time it takes to notice
406    infinite template instantiations.  */
407
408 int max_tinst_depth = 50;
409
410 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc.  */
411
412 int flag_vtable_gc;
413
414 /* Nonzero means make the default pedwarns warnings instead of errors.
415    The value of this flag is ignored if -pedantic is specified.  */
416
417 int flag_permissive;
418
419 /* Nonzero means to implement standard semantics for exception
420    specifications, calling unexpected if an exception is thrown that
421    doesn't match the specification.  Zero means to treat them as
422    assertions and optimize accordingly, but not check them.  */
423
424 int flag_enforce_eh_specs = 1;
425
426 /* The variant of the C language being processed.  */
427
428 c_language_kind c_language = clk_cplusplus;
429
430 /* Table of language-dependent -f options.
431    STRING is the option name.  VARIABLE is the address of the variable.
432    ON_VALUE is the value to store in VARIABLE
433     if `-fSTRING' is seen as an option.
434    (If `-fno-STRING' is seen as an option, the opposite value is stored.)  */
435
436 static struct { const char *string; int *variable; int on_value;}
437 lang_f_options[] =
438 {
439   /* C/C++ options.  */
440   {"signed-char", &flag_signed_char, 1},
441   {"unsigned-char", &flag_signed_char, 0},
442   {"signed-bitfields", &flag_signed_bitfields, 1},
443   {"unsigned-bitfields", &flag_signed_bitfields, 0},
444   {"short-enums", &flag_short_enums, 1},
445   {"short-double", &flag_short_double, 1},
446   {"short-wchar", &flag_short_wchar, 1},
447   {"asm", &flag_no_asm, 0},
448   {"builtin", &flag_no_builtin, 0},
449
450   /* C++-only options.  */
451   {"access-control", &flag_access_control, 1},
452   {"check-new", &flag_check_new, 1},
453   {"conserve-space", &flag_conserve_space, 1},
454   {"const-strings", &flag_const_strings, 1},
455   {"default-inline", &flag_default_inline, 1},
456   {"dollars-in-identifiers", &dollars_in_ident, 1},
457   {"elide-constructors", &flag_elide_constructors, 1},
458   {"enforce-eh-specs", &flag_enforce_eh_specs, 1},
459   {"external-templates", &flag_external_templates, 1},
460   {"for-scope", &flag_new_for_scope, 2},
461   {"gnu-keywords", &flag_no_gnu_keywords, 0},
462   {"handle-exceptions", &flag_exceptions, 1},
463   {"honor-std", &flag_honor_std, 1},
464   {"huge-objects", &flag_huge_objects, 1},
465   {"implement-inlines", &flag_implement_inlines, 1},
466   {"implicit-inline-templates", &flag_implicit_inline_templates, 1},
467   {"implicit-templates", &flag_implicit_templates, 1},
468   {"ms-extensions", &flag_ms_extensions, 1},
469   {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
470   {"operator-names", &flag_operator_names, 1},
471   {"optional-diags", &flag_optional_diags, 1},
472   {"permissive", &flag_permissive, 1},
473   {"repo", &flag_use_repository, 1},
474   {"rtti", &flag_rtti, 1},
475   {"stats", &flag_detailed_statistics, 1},
476   {"use-cxa-atexit", &flag_use_cxa_atexit, 1},
477   {"vtable-thunks", &flag_vtable_thunks, 1},
478   {"weak", &flag_weak, 1}
479 };
480
481 /* The list of `-f' options that we no longer support.  The `-f'
482    prefix is not given in this table.  The `-fno-' variants are not
483    listed here.  This table must be kept in alphabetical order.  */
484 static const char * const unsupported_options[] = {
485   "all-virtual",
486   "cond-mismatch",
487   "enum-int-equiv",
488   "guiding-decls",
489   "labels-ok",
490   "new-abi",
491   "nonnull-objects",
492   "squangle",
493   "strict-prototype",
494   "this-is-variable",
495   "vtable-gc",
496   "xref"
497 };
498
499 /* Compare two option strings, pointed two by P1 and P2, for use with
500    bsearch.  */
501
502 static int
503 compare_options (p1, p2)
504      const PTR p1;
505      const PTR p2;
506 {
507   return strcmp (*((const char *const *) p1), *((const char *const *) p2));
508 }
509
510 /* Decode the string P as a language-specific option.
511    Return the number of strings consumed for a valid option.
512    Otherwise return 0.  Should not complain if it does not
513    recognise the option.  */
514
515 int   
516 cxx_decode_option (argc, argv)
517      int argc;
518      char **argv;
519 {
520   int strings_processed;
521   const char *p = argv[0];
522
523   strings_processed = cpp_handle_option (parse_in, argc, argv);
524
525   if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
526     /* ignore */;
527   else if (p[0] == '-' && p[1] == 'f')
528     {
529       /* Some kind of -f option.
530          P's value is the option sans `-f'.
531          Search for it in the table of options.  */
532       const char *option_value = NULL;
533       const char *positive_option;
534       size_t j;
535
536       p += 2;
537       /* Try special -f options.  */
538
539       /* See if this is one of the options no longer supported.  We
540          used to support these options, so we continue to accept them,
541          with a warning.  */
542       if (strncmp (p, "no-", strlen ("no-")) == 0)
543         positive_option = p + strlen ("no-");
544       else
545         positive_option = p;
546
547       /* If the option is present, issue a warning.  Indicate to our
548          caller that the option was processed successfully.  */
549       if (bsearch (&positive_option, 
550                    unsupported_options, 
551                    (sizeof (unsupported_options) 
552                     / sizeof (unsupported_options[0])),
553                    sizeof (unsupported_options[0]),
554                    compare_options))
555         {
556           warning ("-f%s is no longer supported", p);
557           return 1;
558         }
559
560       if (!strcmp (p, "handle-exceptions")
561           || !strcmp (p, "no-handle-exceptions"))
562         warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
563       else if (! strcmp (p, "alt-external-templates"))
564         {
565           flag_external_templates = 1;
566           flag_alt_external_templates = 1;
567           cp_deprecated ("-falt-external-templates");
568         }
569       else if (! strcmp (p, "no-alt-external-templates"))
570         flag_alt_external_templates = 0;
571       else if (!strcmp (p, "repo"))
572         {
573           flag_use_repository = 1;
574           flag_implicit_templates = 0;
575         }
576       else if (!strcmp (p, "external-templates"))
577         {
578           flag_external_templates = 1;
579           cp_deprecated ("-fexternal-templates");
580         }
581       else if ((option_value
582                 = skip_leading_substring (p, "template-depth-")))
583         max_tinst_depth
584           = read_integral_parameter (option_value, p - 2, max_tinst_depth);
585       else if ((option_value
586                 = skip_leading_substring (p, "name-mangling-version-")))
587         {
588           warning ("-fname-mangling-version is no longer supported");
589           return 1;
590         }
591       else if ((option_value
592                 = skip_leading_substring (p, "dump-translation-unit=")))
593         {
594           if (!*option_value)
595             error ("no file specified with -fdump-translation-unit");
596           else
597             flag_dump_translation_unit = option_value;
598         }
599       else if ((option_value
600                 = skip_leading_substring (p, "dump-class-layout=")))
601         {
602           if (!*option_value)
603             error ("no file specified with -fdump-class-layout");
604           else
605             flag_dump_class_layout = option_value;
606         }
607       else if (!strcmp (p, "dump-class-layout"))
608         flag_dump_class_layout = ""; /* empty string for stderr */
609       else 
610         {
611           int found = 0;
612
613           for (j = 0;
614                !found && j < (sizeof (lang_f_options) 
615                               / sizeof (lang_f_options[0]));
616                j++)
617             {
618               if (!strcmp (p, lang_f_options[j].string))
619                 {
620                   *lang_f_options[j].variable = lang_f_options[j].on_value;
621                   /* A goto here would be cleaner,
622                      but breaks the vax pcc.  */
623                   found = 1;
624                 }
625               if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
626                   && ! strcmp (p+3, lang_f_options[j].string))
627                 {
628                   *lang_f_options[j].variable = ! lang_f_options[j].on_value;
629                   found = 1;
630                 }
631             }
632               
633           return found;
634         }
635     }
636   else if (p[0] == '-' && p[1] == 'W')
637     {
638       int setting = 1;
639
640       /* The -W options control the warning behavior of the compiler.  */
641       p += 2;
642
643       if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
644         setting = 0, p += 3;
645
646       if (!strcmp (p, "implicit"))
647         warn_implicit = setting;
648       else if (!strcmp (p, "long-long"))
649         warn_long_long = setting;
650       else if (!strcmp (p, "return-type"))
651         warn_return_type = setting;
652       else if (!strcmp (p, "ctor-dtor-privacy"))
653         warn_ctor_dtor_privacy = setting;
654       else if (!strcmp (p, "write-strings"))
655         warn_write_strings = setting;
656       else if (!strcmp (p, "cast-qual"))
657         warn_cast_qual = setting;
658       else if (!strcmp (p, "char-subscripts"))
659         warn_char_subscripts = setting;
660       else if (!strcmp (p, "pointer-arith"))
661         warn_pointer_arith = setting;
662       else if (!strcmp (p, "missing-prototypes"))
663         warn_missing_prototypes = setting;
664       else if (!strcmp (p, "strict-prototypes"))
665         {
666           if (setting == 0)
667             warning ("-Wno-strict-prototypes is not supported in C++");
668         }
669       else if (!strcmp (p, "redundant-decls"))
670         warn_redundant_decls = setting;
671       else if (!strcmp (p, "missing-braces"))
672         warn_missing_braces = setting;
673       else if (!strcmp (p, "sign-compare"))
674         warn_sign_compare = setting;
675       else if (!strcmp (p, "float-equal"))
676         warn_float_equal = setting;
677       else if (!strcmp (p, "format"))
678         set_Wformat (setting);
679       else if (!strcmp (p, "format=2"))
680         set_Wformat (2);
681       else if (!strcmp (p, "format-y2k"))
682         warn_format_y2k = setting;
683       else if (!strcmp (p, "format-extra-args"))
684         warn_format_extra_args = setting;
685       else if (!strcmp (p, "format-nonliteral"))
686         warn_format_nonliteral = setting;
687       else if (!strcmp (p, "format-security"))
688         warn_format_security = setting;
689       else if (!strcmp (p, "missing-format-attribute"))
690         warn_missing_format_attribute = setting;
691       else if (!strcmp (p, "conversion"))
692         warn_conversion = setting;
693       else if (!strcmp (p, "parentheses"))
694         warn_parentheses = setting;
695       else if (!strcmp (p, "non-virtual-dtor"))
696         warn_nonvdtor = setting;
697       else if (!strcmp (p, "reorder"))
698         warn_reorder = setting;
699       else if (!strcmp (p, "synth"))
700         warn_synth = setting;
701       else if (!strcmp (p, "pmf-conversions"))
702         warn_pmf2ptr = setting;
703       else if (!strcmp (p, "effc++"))
704         warn_ecpp = setting;
705       else if (!strcmp (p, "sign-promo"))
706         warn_sign_promo = setting;
707       else if (!strcmp (p, "old-style-cast"))
708         warn_old_style_cast = setting;
709       else if (!strcmp (p, "overloaded-virtual"))
710         warn_overloaded_virtual = setting;
711       else if (!strcmp (p, "multichar"))
712         warn_multichar = setting;
713       else if (!strcmp (p, "unknown-pragmas"))
714         /* Set to greater than 1, so that even unknown pragmas in
715            system headers will be warned about.  */  
716         warn_unknown_pragmas = setting * 2;
717       else if (!strcmp (p, "non-template-friend"))
718         warn_nontemplate_friend = setting;
719       else if (!strcmp (p, "deprecated"))
720         warn_deprecated = setting;
721       else if (!strcmp (p, "comment"))
722         ;                       /* cpp handles this one.  */
723       else if (!strcmp (p, "comments"))
724         ;                       /* cpp handles this one.  */
725       else if (!strcmp (p, "trigraphs"))
726         ;                       /* cpp handles this one.  */
727       else if (!strcmp (p, "import"))
728         ;                       /* cpp handles this one.  */
729       else if (!strcmp (p, "all"))
730         {
731           warn_return_type = setting;
732           set_Wunused (setting);
733           warn_implicit = setting;
734           warn_switch = setting;
735           set_Wformat (setting);
736           warn_parentheses = setting;
737           warn_missing_braces = setting;
738           warn_sign_compare = setting;
739           warn_multichar = setting;
740           /* We save the value of warn_uninitialized, since if they put
741              -Wuninitialized on the command line, we need to generate a
742              warning about not using it without also specifying -O.  */
743           if (warn_uninitialized != 1)
744             warn_uninitialized = (setting ? 2 : 0);
745           /* Only warn about unknown pragmas that are not in system
746              headers.  */                                        
747           warn_unknown_pragmas = 1;       
748
749           /* C++-specific warnings.  */
750           warn_ctor_dtor_privacy = setting;
751           warn_nonvdtor = setting;
752           warn_reorder = setting;
753           warn_nontemplate_friend = setting;           
754         }
755       else return strings_processed;
756     }
757   else if (!strcmp (p, "-ansi"))
758     flag_no_nonansi_builtin = 1, flag_ansi = 1,
759     flag_noniso_default_format_attributes = 0, flag_no_gnu_keywords = 1;
760 #ifdef SPEW_DEBUG
761   /* Undocumented, only ever used when you're invoking cc1plus by hand, since
762      it's probably safe to assume no sane person would ever want to use this
763      under normal circumstances.  */
764   else if (!strcmp (p, "-spew-debug"))
765     spew_debug = 1;
766 #endif
767   else
768     return strings_processed;
769
770   return 1;
771 }
772 \f
773 /* Incorporate `const' and `volatile' qualifiers for member functions.
774    FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
775    QUALS is a list of qualifiers.  Returns any explicit
776    top-level qualifiers of the method's this pointer, anything other than
777    TYPE_UNQUALIFIED will be an extension.  */
778
779 int
780 grok_method_quals (ctype, function, quals)
781      tree ctype, function, quals;
782 {
783   tree fntype = TREE_TYPE (function);
784   tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
785   int type_quals = TYPE_UNQUALIFIED;
786   int dup_quals = TYPE_UNQUALIFIED;
787   int this_quals = TYPE_UNQUALIFIED;
788
789   do
790     {
791       int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
792       
793       if ((type_quals | this_quals) & tq)
794         dup_quals |= tq;
795       else if (tq & TYPE_QUAL_RESTRICT)
796         this_quals |= tq;
797       else
798         type_quals |= tq;
799       quals = TREE_CHAIN (quals);
800     } 
801   while (quals);
802
803   if (dup_quals != TYPE_UNQUALIFIED)
804     cp_error ("duplicate type qualifiers in %s declaration",
805               TREE_CODE (function) == FUNCTION_DECL 
806               ? "member function" : "type");
807
808   ctype = cp_build_qualified_type (ctype, type_quals);
809   fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
810                                     (TREE_CODE (fntype) == METHOD_TYPE
811                                      ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
812                                      : TYPE_ARG_TYPES (fntype)));
813   if (raises)
814     fntype = build_exception_variant (fntype, raises);
815
816   TREE_TYPE (function) = fntype;
817   return this_quals;
818 }
819
820 /* Warn when -fexternal-templates is used and #pragma
821    interface/implementation is not used all the times it should be,
822    inform the user.  */
823
824 void
825 warn_if_unknown_interface (decl)
826      tree decl;
827 {
828   static int already_warned = 0;
829   if (already_warned++)
830     return;
831
832   if (flag_alt_external_templates)
833     {
834       tree til = tinst_for_decl ();
835       int sl = lineno;
836       const char *sf = input_filename;
837
838       if (til)
839         {
840           lineno = TINST_LINE (til);
841           input_filename = TINST_FILE (til);
842         }
843       cp_warning ("template `%#D' instantiated in file without #pragma interface",
844                   decl);
845       lineno = sl;
846       input_filename = sf;
847     }
848   else
849     cp_warning_at ("template `%#D' defined in file without #pragma interface",
850                    decl);
851 }
852
853 /* A subroutine of the parser, to handle a component list.  */
854
855 void
856 grok_x_components (specs)
857      tree specs;
858 {
859   tree t;
860
861   specs = strip_attrs (specs);
862
863   check_tag_decl (specs);
864   t = groktypename (build_tree_list (specs, NULL_TREE)); 
865
866   /* The only case where we need to do anything additional here is an
867      anonymous union field, e.g.: `struct S { union { int i; }; };'.  */
868   if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
869     return;
870
871   fixup_anonymous_aggr (t);
872   finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t)); 
873 }
874
875 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
876    indicated NAME.  */
877
878 tree
879 build_artificial_parm (name, type)
880      tree name;
881      tree type;
882 {
883   tree parm;
884
885   parm = build_decl (PARM_DECL, name, type);
886   DECL_ARTIFICIAL (parm) = 1;
887   /* All our artificial parms are implicitly `const'; they cannot be
888      assigned to.  */
889   TREE_READONLY (parm) = 1;
890   DECL_ARG_TYPE (parm) = type;
891   return parm;
892 }
893
894 /* Constructors for types with virtual baseclasses need an "in-charge" flag
895    saying whether this constructor is responsible for initialization of
896    virtual baseclasses or not.  All destructors also need this "in-charge"
897    flag, which additionally determines whether or not the destructor should
898    free the memory for the object.
899
900    This function adds the "in-charge" flag to member function FN if
901    appropriate.  It is called from grokclassfn and tsubst.
902    FN must be either a constructor or destructor.
903
904    The in-charge flag follows the 'this' parameter, and is followed by the
905    VTT parm (if any), then the user-written parms.  */
906
907 void
908 maybe_retrofit_in_chrg (fn)
909      tree fn;
910 {
911   tree basetype, arg_types, parms, parm, fntype;
912
913   /* If we've already add the in-charge parameter don't do it again.  */
914   if (DECL_HAS_IN_CHARGE_PARM_P (fn))
915     return;
916
917   /* When processing templates we can't know, in general, whether or
918      not we're going to have virtual baseclasses.  */
919   if (uses_template_parms (fn))
920     return;
921
922   /* We don't need an in-charge parameter for constructors that don't
923      have virtual bases.  */
924   if (DECL_CONSTRUCTOR_P (fn)
925       && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
926     return;
927
928   arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
929   basetype = TREE_TYPE (TREE_VALUE (arg_types));
930   arg_types = TREE_CHAIN (arg_types);
931
932   parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
933
934   /* If this is a subobject constructor or destructor, our caller will
935      pass us a pointer to our VTT.  */
936   if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
937     {
938       parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
939
940       /* First add it to DECL_ARGUMENTS between 'this' and the real args...  */
941       TREE_CHAIN (parm) = parms;
942       parms = parm;
943
944       /* ...and then to TYPE_ARG_TYPES.  */
945       arg_types = hash_tree_chain (vtt_parm_type, arg_types);
946
947       DECL_HAS_VTT_PARM_P (fn) = 1;
948     }
949
950   /* Then add the in-charge parm (before the VTT parm).  */
951   parm = build_artificial_parm (in_charge_identifier, integer_type_node);
952   TREE_CHAIN (parm) = parms;
953   parms = parm;
954   arg_types = hash_tree_chain (integer_type_node, arg_types);
955
956   /* Insert our new parameter(s) into the list.  */
957   TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
958
959   /* And rebuild the function type.  */
960   fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
961                                     arg_types);
962   if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
963     fntype = build_exception_variant (fntype,
964                                       TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
965   TREE_TYPE (fn) = fntype;
966
967   /* Now we've got the in-charge parameter.  */
968   DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
969 }
970
971 /* Classes overload their constituent function names automatically.
972    When a function name is declared in a record structure,
973    its name is changed to it overloaded name.  Since names for
974    constructors and destructors can conflict, we place a leading
975    '$' for destructors.
976
977    CNAME is the name of the class we are grokking for.
978
979    FUNCTION is a FUNCTION_DECL.  It was created by `grokdeclarator'.
980
981    FLAGS contains bits saying what's special about today's
982    arguments.  1 == DESTRUCTOR.  2 == OPERATOR.
983
984    If FUNCTION is a destructor, then we must add the `auto-delete' field
985    as a second parameter.  There is some hair associated with the fact
986    that we must "declare" this variable in the manner consistent with the
987    way the rest of the arguments were declared.
988
989    QUALS are the qualifiers for the this pointer.  */
990
991 void
992 grokclassfn (ctype, function, flags, quals)
993      tree ctype, function;
994      enum overload_flags flags;
995      tree quals;
996 {
997   tree fn_name = DECL_NAME (function);
998   int this_quals = TYPE_UNQUALIFIED;
999
1000   /* Even within an `extern "C"' block, members get C++ linkage.  See
1001      [dcl.link] for details.  */
1002   SET_DECL_LANGUAGE (function, lang_cplusplus);
1003
1004   if (fn_name == NULL_TREE)
1005     {
1006       error ("name missing for member function");
1007       fn_name = get_identifier ("<anonymous>");
1008       DECL_NAME (function) = fn_name;
1009     }
1010
1011   if (quals)
1012     this_quals = grok_method_quals (ctype, function, quals);
1013
1014   if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1015     {
1016       /* Must add the class instance variable up front.  */
1017       /* Right now we just make this a pointer.  But later
1018          we may wish to make it special.  */
1019       tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
1020       tree qual_type;
1021       tree parm;
1022
1023       /* The `this' parameter is implicitly `const'; it cannot be
1024          assigned to.  */
1025       this_quals |= TYPE_QUAL_CONST;
1026       qual_type = cp_build_qualified_type (type, this_quals);
1027       parm = build_artificial_parm (this_identifier, qual_type);
1028       c_apply_type_quals_to_decl (this_quals, parm);
1029
1030       /* We can make this a register, so long as we don't
1031          accidentally complain if someone tries to take its address.  */
1032       DECL_REGISTER (parm) = 1;
1033       TREE_CHAIN (parm) = last_function_parms;
1034       last_function_parms = parm;
1035     }
1036
1037   DECL_ARGUMENTS (function) = last_function_parms;
1038   DECL_CONTEXT (function) = ctype;
1039
1040   if (flags == DTOR_FLAG)
1041     DECL_DESTRUCTOR_P (function) = 1;
1042
1043   if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
1044     maybe_retrofit_in_chrg (function);
1045
1046   if (flags == DTOR_FLAG)
1047     {
1048       DECL_DESTRUCTOR_P (function) = 1;
1049       TYPE_HAS_DESTRUCTOR (ctype) = 1;
1050     }
1051 }
1052
1053 /* Work on the expr used by alignof (this is only called by the parser).  */
1054
1055 tree
1056 grok_alignof (expr)
1057      tree expr;
1058 {
1059   tree best, t;
1060   int bestalign;
1061
1062   if (processing_template_decl)
1063     return build_min (ALIGNOF_EXPR, sizetype, expr);
1064
1065   if (TREE_CODE (expr) == COMPONENT_REF
1066       && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1067     error ("`__alignof__' applied to a bit-field");
1068
1069   if (TREE_CODE (expr) == INDIRECT_REF)
1070     {
1071       best = t = TREE_OPERAND (expr, 0);
1072       bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1073
1074       while (TREE_CODE (t) == NOP_EXPR
1075              && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
1076         {
1077           int thisalign;
1078           t = TREE_OPERAND (t, 0);
1079           thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1080           if (thisalign > bestalign)
1081             best = t, bestalign = thisalign;
1082         }
1083       return c_alignof (TREE_TYPE (TREE_TYPE (best)));
1084     }
1085   else
1086     return c_alignof (TREE_TYPE (expr));
1087 }
1088
1089 /* Create an ARRAY_REF, checking for the user doing things backwards
1090    along the way.  */
1091
1092 tree
1093 grok_array_decl (array_expr, index_exp)
1094      tree array_expr, index_exp;
1095 {
1096   tree type = TREE_TYPE (array_expr);
1097   tree p1, p2, i1, i2;
1098
1099   if (type == error_mark_node || index_exp == error_mark_node)
1100     return error_mark_node;
1101   if (processing_template_decl)
1102     return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1103                       array_expr, index_exp);
1104
1105   if (type == NULL_TREE)
1106     {
1107       /* Something has gone very wrong.  Assume we are mistakenly reducing
1108          an expression instead of a declaration.  */
1109       error ("parser may be lost: is there a '{' missing somewhere?");
1110       return NULL_TREE;
1111     }
1112
1113   if (TREE_CODE (type) == OFFSET_TYPE
1114       || TREE_CODE (type) == REFERENCE_TYPE)
1115     type = TREE_TYPE (type);
1116
1117   /* If they have an `operator[]', use that.  */
1118   if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
1119     return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1120                            array_expr, index_exp, NULL_TREE);
1121
1122   /* Otherwise, create an ARRAY_REF for a pointer or array type.  It
1123      is a little-known fact that, if `a' is an array and `i' is an
1124      int, you can write `i[a]', which means the same thing as `a[i]'.  */
1125
1126   if (TREE_CODE (type) == ARRAY_TYPE)
1127     p1 = array_expr;
1128   else
1129     p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1130
1131   if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1132     p2 = index_exp;
1133   else
1134     p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1135
1136   i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1137   i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1138
1139   if ((p1 && i2) && (i1 && p2))
1140     error ("ambiguous conversion for array subscript");
1141
1142   if (p1 && i2)
1143     array_expr = p1, index_exp = i2;
1144   else if (i1 && p2)
1145     array_expr = p2, index_exp = i1;
1146   else
1147     {
1148       cp_error ("invalid types `%T[%T]' for array subscript",
1149                 type, TREE_TYPE (index_exp));
1150       return error_mark_node;
1151     }
1152
1153   if (array_expr == error_mark_node || index_exp == error_mark_node)
1154     error ("ambiguous conversion for array subscript");
1155
1156   return build_array_ref (array_expr, index_exp);
1157 }
1158
1159 /* Given the cast expression EXP, checking out its validity.   Either return
1160    an error_mark_node if there was an unavoidable error, return a cast to
1161    void for trying to delete a pointer w/ the value 0, or return the
1162    call to delete.  If DOING_VEC is 1, we handle things differently
1163    for doing an array delete.  If DOING_VEC is 2, they gave us the
1164    array size as an argument to delete.
1165    Implements ARM $5.3.4.  This is called from the parser.  */
1166
1167 tree
1168 delete_sanity (exp, size, doing_vec, use_global_delete)
1169      tree exp, size;
1170      int doing_vec, use_global_delete;
1171 {
1172   tree t, type;
1173   /* For a regular vector delete (aka, no size argument) we will pass
1174      this down as a NULL_TREE into build_vec_delete.  */
1175   tree maxindex = NULL_TREE;
1176
1177   if (exp == error_mark_node)
1178     return exp;
1179
1180   if (processing_template_decl)
1181     {
1182       t = build_min (DELETE_EXPR, void_type_node, exp, size);
1183       DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1184       DELETE_EXPR_USE_VEC (t) = doing_vec;
1185       return t;
1186     }
1187
1188   if (TREE_CODE (exp) == OFFSET_REF)
1189     exp = resolve_offset_ref (exp);
1190   exp = convert_from_reference (exp);
1191   t = stabilize_reference (exp);
1192   t = build_expr_type_conversion (WANT_POINTER, t, 1);
1193
1194   if (t == NULL_TREE || t == error_mark_node)
1195     {
1196       cp_error ("type `%#T' argument given to `delete', expected pointer",
1197                 TREE_TYPE (exp));
1198       return error_mark_node;
1199     }
1200
1201   if (doing_vec == 2)
1202     {
1203       maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
1204       pedwarn ("anachronistic use of array size in vector delete");
1205     }
1206
1207   type = TREE_TYPE (t);
1208
1209   /* As of Valley Forge, you can delete a pointer to const.  */
1210
1211   /* You can't delete functions.  */
1212   if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1213     {
1214       error ("cannot delete a function.  Only pointer-to-objects are valid arguments to `delete'");
1215       return error_mark_node;
1216     }
1217
1218   /* Deleting ptr to void is undefined behaviour [expr.delete/3].  */
1219   if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
1220     {
1221       cp_warning ("deleting `%T' is undefined", type);
1222       doing_vec = 0;
1223     }
1224
1225   /* An array can't have been allocated by new, so complain.  */
1226   if (TREE_CODE (t) == ADDR_EXPR
1227       && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
1228       && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
1229     cp_warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
1230
1231   /* Deleting a pointer with the value zero is valid and has no effect.  */
1232   if (integer_zerop (t))
1233     return build1 (NOP_EXPR, void_type_node, t);
1234
1235   if (doing_vec)
1236     return build_vec_delete (t, maxindex, sfk_deleting_destructor,
1237                              use_global_delete);
1238   else
1239     {
1240       if (IS_AGGR_TYPE (TREE_TYPE (type))
1241           && TYPE_GETS_REG_DELETE (TREE_TYPE (type)))
1242         {
1243           /* Only do access checking here; we'll be calling op delete
1244              from the destructor.  */
1245           tree tmp = build_op_delete_call (DELETE_EXPR, t, size_zero_node,
1246                                            LOOKUP_NORMAL, NULL_TREE);
1247           if (tmp == error_mark_node)
1248             return error_mark_node;
1249         }
1250
1251       return build_delete (type, t, sfk_deleting_destructor,
1252                            LOOKUP_NORMAL, use_global_delete);
1253     }
1254 }
1255
1256 /* Report an error if the indicated template declaration is not the
1257    sort of thing that should be a member template.  */
1258
1259 void
1260 check_member_template (tmpl)
1261      tree tmpl;
1262 {
1263   tree decl;
1264
1265   my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
1266   decl = DECL_TEMPLATE_RESULT (tmpl);
1267
1268   if (TREE_CODE (decl) == FUNCTION_DECL
1269       || (TREE_CODE (decl) == TYPE_DECL
1270           && IS_AGGR_TYPE (TREE_TYPE (decl))))
1271     {
1272       if (current_function_decl)
1273         /* 14.5.2.2 [temp.mem]
1274            
1275            A local class shall not have member templates. */
1276         cp_error ("invalid declaration of member template `%#D' in local class",
1277                   decl);
1278       
1279       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
1280         {
1281           /* 14.5.2.3 [temp.mem]
1282
1283              A member function template shall not be virtual.  */
1284           cp_error 
1285             ("invalid use of `virtual' in template declaration of `%#D'",
1286              decl);
1287           DECL_VIRTUAL_P (decl) = 0;
1288         }
1289
1290       /* The debug-information generating code doesn't know what to do
1291          with member templates.  */ 
1292       DECL_IGNORED_P (tmpl) = 1;
1293     } 
1294   else
1295     cp_error ("template declaration of `%#D'", decl);
1296 }
1297
1298 /* Return true iff TYPE is a valid Java parameter or return type. */
1299
1300 static int
1301 acceptable_java_type (type)
1302      tree type;
1303 {
1304   if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
1305     return 1;
1306   if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
1307     {
1308       type = TREE_TYPE (type);
1309       if (TREE_CODE (type) == RECORD_TYPE)
1310         {
1311           tree args;  int i;
1312           if (! TYPE_FOR_JAVA (type))
1313             return 0;
1314           if (! CLASSTYPE_TEMPLATE_INFO (type))
1315             return 1;
1316           args = CLASSTYPE_TI_ARGS (type);
1317           i = TREE_VEC_LENGTH (args);
1318           while (--i >= 0)
1319             {
1320               type = TREE_VEC_ELT (args, i);
1321               if (TREE_CODE (type) == POINTER_TYPE)
1322                 type = TREE_TYPE (type);
1323               if (! TYPE_FOR_JAVA (type))
1324                 return 0;
1325             }
1326           return 1;
1327         }
1328     }
1329   return 0;
1330 }
1331
1332 /* For a METHOD in a Java class CTYPE, return 1 if
1333    the parameter and return types are valid Java types.
1334    Otherwise, print appropriate error messages, and return 0.  */
1335
1336 int
1337 check_java_method (method)
1338      tree method;
1339 {
1340   int jerr = 0;
1341   tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
1342   tree ret_type = TREE_TYPE (TREE_TYPE (method));
1343   if (! acceptable_java_type (ret_type))
1344     {
1345       cp_error ("Java method '%D' has non-Java return type `%T'",
1346                 method, ret_type);
1347       jerr++;
1348     }
1349   for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
1350     {
1351       tree type = TREE_VALUE (arg_types);
1352       if (! acceptable_java_type (type))
1353         {
1354           cp_error ("Java method '%D' has non-Java parameter type `%T'",
1355                     method, type);
1356           jerr++;
1357         }
1358     }
1359   return jerr ? 0 : 1;
1360 }
1361
1362 /* Sanity check: report error if this function FUNCTION is not
1363    really a member of the class (CTYPE) it is supposed to belong to.
1364    CNAME is the same here as it is for grokclassfn above.  */
1365
1366 tree
1367 check_classfn (ctype, function)
1368      tree ctype, function;
1369 {
1370   tree fn_name = DECL_NAME (function);
1371   tree fndecl, fndecls;
1372   tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1373   tree *methods = 0;
1374   tree *end = 0;
1375   
1376   if (DECL_USE_TEMPLATE (function)
1377       && !(TREE_CODE (function) == TEMPLATE_DECL
1378            && DECL_TEMPLATE_SPECIALIZATION (function))
1379       && is_member_template (DECL_TI_TEMPLATE (function)))
1380     /* Since this is a specialization of a member template,
1381        we're not going to find the declaration in the class.
1382        For example, in:
1383        
1384          struct S { template <typename T> void f(T); };
1385          template <> void S::f(int);
1386        
1387        we're not going to find `S::f(int)', but there's no
1388        reason we should, either.  We let our callers know we didn't
1389        find the method, but we don't complain.  */
1390     return NULL_TREE;
1391       
1392   if (method_vec != 0)
1393     {
1394       methods = &TREE_VEC_ELT (method_vec, 0);
1395       end = TREE_VEC_END (method_vec);
1396
1397       /* First suss out ctors and dtors.  */
1398       if (*methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1399           && DECL_CONSTRUCTOR_P (function))
1400         goto got_it;
1401       if (*++methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1402           && DECL_DESTRUCTOR_P (function))
1403         goto got_it;
1404
1405       while (++methods != end && *methods)
1406         {
1407           fndecl = *methods;
1408           if (fn_name == DECL_NAME (OVL_CURRENT (*methods)))
1409             {
1410             got_it:
1411               for (fndecls = *methods; fndecls != NULL_TREE;
1412                    fndecls = OVL_NEXT (fndecls))
1413                 {
1414                   fndecl = OVL_CURRENT (fndecls);
1415
1416                   /* We cannot simply call decls_match because this
1417                      doesn't work for static member functions that are 
1418                      pretending to be methods, and because the name
1419                      may have been changed by asm("new_name").  */ 
1420                   if (DECL_NAME (function) == DECL_NAME (fndecl))
1421                     {
1422                       tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1423                       tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1424
1425                       /* Get rid of the this parameter on functions that become
1426                          static.  */
1427                       if (DECL_STATIC_FUNCTION_P (fndecl)
1428                           && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1429                         p1 = TREE_CHAIN (p1);
1430
1431                       if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
1432                                        TREE_TYPE (TREE_TYPE (fndecl)))
1433                           && compparms (p1, p2)
1434                           && (DECL_TEMPLATE_SPECIALIZATION (function)
1435                               == DECL_TEMPLATE_SPECIALIZATION (fndecl))
1436                           && (!DECL_TEMPLATE_SPECIALIZATION (function)
1437                               || (DECL_TI_TEMPLATE (function) 
1438                                   == DECL_TI_TEMPLATE (fndecl))))
1439                         return fndecl;
1440                     }
1441                 }
1442               break;            /* loser */
1443             }
1444         }
1445     }
1446
1447   if (methods != end && *methods)
1448     {
1449       tree fndecl = *methods;
1450       cp_error ("prototype for `%#D' does not match any in class `%T'",
1451                 function, ctype);
1452       cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl) ? "s are" : " is",
1453                    OVL_CURRENT (fndecl));
1454       while (fndecl = OVL_NEXT (fndecl), fndecl)
1455         cp_error_at ("                %#D", OVL_CURRENT(fndecl));
1456     }
1457   else
1458     {
1459       methods = 0;
1460       if (!COMPLETE_TYPE_P (ctype))
1461         incomplete_type_error (function, ctype);
1462       else
1463         cp_error ("no `%#D' member function declared in class `%T'",
1464                   function, ctype);
1465     }
1466
1467   /* If we did not find the method in the class, add it to avoid
1468      spurious errors (unless the CTYPE is not yet defined, in which
1469      case we'll only confuse ourselves when the function is declared
1470      properly within the class.  */
1471   if (COMPLETE_TYPE_P (ctype))
1472     add_method (ctype, function, /*error_p=*/1);
1473   return NULL_TREE;
1474 }
1475
1476 /* We have just processed the DECL, which is a static data member.
1477    Its initializer, if present, is INIT.  The ASMSPEC_TREE, if
1478    present, is the assembly-language name for the data member.
1479    FLAGS is as for cp_finish_decl.  */
1480
1481 void
1482 finish_static_data_member_decl (decl, init, asmspec_tree, flags)
1483      tree decl;
1484      tree init;
1485      tree asmspec_tree;
1486      int flags;
1487 {
1488   my_friendly_assert (TREE_PUBLIC (decl), 0);
1489
1490   DECL_CONTEXT (decl) = current_class_type;
1491
1492   /* We cannot call pushdecl here, because that would fill in the
1493      TREE_CHAIN of our decl.  Instead, we modify cp_finish_decl to do
1494      the right thing, namely, to put this decl out straight away.  */
1495   /* current_class_type can be NULL_TREE in case of error.  */
1496   if (!asmspec_tree && current_class_type)
1497     DECL_INITIAL (decl) = error_mark_node;
1498
1499   if (! processing_template_decl)
1500     {
1501       if (!pending_statics)
1502         VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
1503       VARRAY_PUSH_TREE (pending_statics, decl);
1504     }
1505
1506   /* Static consts need not be initialized in the class definition.  */
1507   if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
1508     {
1509       static int explained = 0;
1510           
1511       error ("initializer invalid for static member with constructor");
1512       if (!explained)
1513         {
1514           error ("(an out of class initialization is required)");
1515           explained = 1;
1516         }
1517       init = NULL_TREE;
1518     }
1519   /* Force the compiler to know when an uninitialized static const
1520      member is being used.  */
1521   if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
1522     TREE_USED (decl) = 1;
1523   DECL_INITIAL (decl) = init;
1524   DECL_IN_AGGR_P (decl) = 1;
1525
1526   cp_finish_decl (decl, init, asmspec_tree, flags);
1527 }
1528
1529 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1530    of a structure component, returning a _DECL node.
1531    QUALS is a list of type qualifiers for this decl (such as for declaring
1532    const member functions).
1533
1534    This is done during the parsing of the struct declaration.
1535    The _DECL nodes are chained together and the lot of them
1536    are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1537
1538    If class A defines that certain functions in class B are friends, then
1539    the way I have set things up, it is B who is interested in permission
1540    granted by A.  However, it is in A's context that these declarations
1541    are parsed.  By returning a void_type_node, class A does not attempt
1542    to incorporate the declarations of the friends within its structure.
1543
1544    DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1545    CHANGES TO CODE IN `start_method'.  */
1546
1547 tree
1548 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
1549      tree declarator, declspecs, init, asmspec_tree, attrlist;
1550 {
1551   register tree value;
1552   const char *asmspec = 0;
1553   int flags = LOOKUP_ONLYCONVERTING;
1554
1555   /* Convert () initializers to = initializers.  */
1556   if (init == NULL_TREE && declarator != NULL_TREE
1557       && TREE_CODE (declarator) == CALL_EXPR
1558       && TREE_OPERAND (declarator, 0)
1559       && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1560           || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1561       && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator)))
1562     {
1563       /* It's invalid to try to initialize a data member using a
1564          functional notation, e.g.:
1565          
1566             struct S {
1567               static int i (3);
1568             };
1569             
1570          Explain that to the user.  */
1571       static int explained;
1572
1573       cp_error ("invalid data member initialization");
1574       if (!explained)
1575         {
1576           cp_error ("(use `=' to initialize static data members)");
1577           explained = 1;
1578         }
1579
1580       declarator = TREE_OPERAND (declarator, 0);
1581       flags = 0;
1582     }
1583
1584   if (declspecs == NULL_TREE
1585       && TREE_CODE (declarator) == SCOPE_REF
1586       && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
1587     {
1588       /* Access declaration */
1589       if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1590         ;
1591       else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1592         pop_nested_class ();
1593       return do_class_using_decl (declarator);
1594     }
1595
1596   if (init
1597       && TREE_CODE (init) == TREE_LIST
1598       && TREE_VALUE (init) == error_mark_node
1599       && TREE_CHAIN (init) == NULL_TREE)
1600     init = NULL_TREE;
1601
1602   value = grokdeclarator (declarator, declspecs, FIELD, init != 0, attrlist);
1603   if (! value || value == error_mark_node)
1604     /* friend or constructor went bad.  */
1605     return value;
1606   if (TREE_TYPE (value) == error_mark_node)
1607     return error_mark_node;  
1608
1609   /* Pass friendly classes back.  */
1610   if (TREE_CODE (value) == VOID_TYPE)
1611     return void_type_node;
1612
1613   if (DECL_NAME (value) != NULL_TREE
1614       && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1615       && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1616     cp_error ("member `%D' conflicts with virtual function table field name",
1617               value);
1618
1619   /* Stash away type declarations.  */
1620   if (TREE_CODE (value) == TYPE_DECL)
1621     {
1622       DECL_NONLOCAL (value) = 1;
1623       DECL_CONTEXT (value) = current_class_type;
1624
1625       if (CLASS_TYPE_P (TREE_TYPE (value)))
1626         CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1;
1627       
1628       if (processing_template_decl)
1629         value = push_template_decl (value);
1630
1631       return value;
1632     }
1633
1634   if (DECL_IN_AGGR_P (value))
1635     {
1636       cp_error ("`%D' is already defined in `%T'", value,
1637                 DECL_CONTEXT (value));
1638       return void_type_node;
1639     }
1640
1641   if (asmspec_tree)
1642     asmspec = TREE_STRING_POINTER (asmspec_tree);
1643
1644   if (init)
1645     {
1646       if (TREE_CODE (value) == FUNCTION_DECL)
1647         {
1648           grok_function_init (value, init);
1649           init = NULL_TREE;
1650         }
1651       else if (pedantic && TREE_CODE (value) != VAR_DECL)
1652         /* Already complained in grokdeclarator.  */
1653         init = NULL_TREE;
1654       else
1655         {
1656           /* We allow initializers to become parameters to base
1657              initializers.  */
1658           if (TREE_CODE (init) == TREE_LIST)
1659             {
1660               if (TREE_CHAIN (init) == NULL_TREE)
1661                 init = TREE_VALUE (init);
1662               else
1663                 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1664             }
1665           
1666           if (TREE_CODE (init) == CONST_DECL)
1667             init = DECL_INITIAL (init);
1668           else if (TREE_READONLY_DECL_P (init))
1669             init = decl_constant_value (init);
1670           else if (TREE_CODE (init) == CONSTRUCTOR)
1671             init = digest_init (TREE_TYPE (value), init, (tree *)0);
1672           if (init == error_mark_node)
1673             /* We must make this look different than `error_mark_node'
1674                because `decl_const_value' would mis-interpret it
1675                as only meaning that this VAR_DECL is defined.  */
1676             init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1677           else if (processing_template_decl)
1678             ;
1679           else if (! TREE_CONSTANT (init))
1680             {
1681               /* We can allow references to things that are effectively
1682                  static, since references are initialized with the address.  */
1683               if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1684                   || (TREE_STATIC (init) == 0
1685                       && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
1686                 {
1687                   error ("field initializer is not constant");
1688                   init = error_mark_node;
1689                 }
1690             }
1691         }
1692     }
1693
1694   if (processing_template_decl && ! current_function_decl
1695       && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1696     value = push_template_decl (value);
1697
1698   if (attrlist)
1699     cplus_decl_attributes (value, TREE_PURPOSE (attrlist),
1700                            TREE_VALUE (attrlist));
1701
1702   if (TREE_CODE (value) == VAR_DECL)
1703     {
1704       finish_static_data_member_decl (value, init, asmspec_tree, 
1705                                       flags);
1706       return value;
1707     }
1708   if (TREE_CODE (value) == FIELD_DECL)
1709     {
1710       if (asmspec)
1711         cp_error ("`asm' specifiers are not permitted on non-static data members");
1712       if (DECL_INITIAL (value) == error_mark_node)
1713         init = error_mark_node;
1714       cp_finish_decl (value, init, NULL_TREE, flags);
1715       DECL_INITIAL (value) = init;
1716       DECL_IN_AGGR_P (value) = 1;
1717       return value;
1718     }
1719   if (TREE_CODE (value) == FUNCTION_DECL)
1720     {
1721       if (asmspec)
1722         {
1723           /* This must override the asm specifier which was placed
1724              by grokclassfn.  Lay this out fresh.  */
1725           SET_DECL_RTL (value, NULL_RTX);
1726           SET_DECL_ASSEMBLER_NAME (value, get_identifier (asmspec));
1727         }
1728       cp_finish_decl (value, init, asmspec_tree, flags);
1729
1730       /* Pass friends back this way.  */
1731       if (DECL_FRIEND_P (value))
1732         return void_type_node;
1733
1734       DECL_IN_AGGR_P (value) = 1;
1735       return value;
1736     }
1737   my_friendly_abort (21);
1738   /* NOTREACHED */
1739   return NULL_TREE;
1740 }
1741
1742 /* Like `grokfield', but for bitfields.
1743    WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.  */
1744
1745 tree
1746 grokbitfield (declarator, declspecs, width)
1747      tree declarator, declspecs, width;
1748 {
1749   register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1750                                         0, NULL_TREE);
1751
1752   if (! value) return NULL_TREE; /* friends went bad.  */
1753
1754   /* Pass friendly classes back.  */
1755   if (TREE_CODE (value) == VOID_TYPE)
1756     return void_type_node;
1757
1758   if (TREE_CODE (value) == TYPE_DECL)
1759     {
1760       cp_error ("cannot declare `%D' to be a bitfield type", value);
1761       return NULL_TREE;
1762     }
1763
1764   /* Usually, finish_struct_1 catches bitifields with invalid types.
1765      But, in the case of bitfields with function type, we confuse
1766      ourselves into thinking they are member functions, so we must
1767      check here.  */
1768   if (TREE_CODE (value) == FUNCTION_DECL)
1769     {
1770       cp_error ("cannot declare bitfield `%D' with funcion type",
1771                 DECL_NAME (value));
1772       return NULL_TREE;
1773     }
1774
1775   if (DECL_IN_AGGR_P (value))
1776     {
1777       cp_error ("`%D' is already defined in the class %T", value,
1778                   DECL_CONTEXT (value));
1779       return void_type_node;
1780     }
1781
1782   GNU_xref_member (current_class_name, value);
1783
1784   if (TREE_STATIC (value))
1785     {
1786       cp_error ("static member `%D' cannot be a bitfield", value);
1787       return NULL_TREE;
1788     }
1789   cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1790
1791   if (width != error_mark_node)
1792     {
1793       constant_expression_warning (width);
1794       DECL_INITIAL (value) = width;
1795       SET_DECL_C_BIT_FIELD (value);
1796     }
1797
1798   DECL_IN_AGGR_P (value) = 1;
1799   return value;
1800 }
1801
1802 tree
1803 grokoptypename (declspecs, declarator)
1804      tree declspecs, declarator;
1805 {
1806   tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL_TREE);
1807   return mangle_conv_op_name_for_type (t);
1808 }
1809
1810 /* When a function is declared with an initializer,
1811    do the right thing.  Currently, there are two possibilities:
1812
1813    class B
1814    {
1815     public:
1816      // initialization possibility #1.
1817      virtual void f () = 0;
1818      int g ();
1819    };
1820    
1821    class D1 : B
1822    {
1823     public:
1824      int d1;
1825      // error, no f ();
1826    };
1827    
1828    class D2 : B
1829    {
1830     public:
1831      int d2;
1832      void f ();
1833    };
1834    
1835    class D3 : B
1836    {
1837     public:
1838      int d3;
1839      // initialization possibility #2
1840      void f () = B::f;
1841    };
1842
1843 */
1844
1845 int
1846 copy_assignment_arg_p (parmtype, virtualp)
1847      tree parmtype;
1848      int virtualp ATTRIBUTE_UNUSED;
1849 {
1850   if (current_class_type == NULL_TREE)
1851     return 0;
1852
1853   if (TREE_CODE (parmtype) == REFERENCE_TYPE)
1854     parmtype = TREE_TYPE (parmtype);
1855
1856   if ((TYPE_MAIN_VARIANT (parmtype) == current_class_type)
1857 #if 0
1858       /* Non-standard hack to support old Booch components.  */
1859       || (! virtualp && DERIVED_FROM_P (parmtype, current_class_type))
1860 #endif
1861       )
1862     return 1;
1863
1864   return 0;
1865 }
1866
1867 static void
1868 grok_function_init (decl, init)
1869      tree decl;
1870      tree init;
1871 {
1872   /* An initializer for a function tells how this function should
1873      be inherited.  */
1874   tree type = TREE_TYPE (decl);
1875
1876   if (TREE_CODE (type) == FUNCTION_TYPE)
1877     cp_error ("initializer specified for non-member function `%D'", decl);
1878   else if (integer_zerop (init))
1879     {
1880       DECL_PURE_VIRTUAL_P (decl) = 1;
1881       if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
1882         {
1883           tree parmtype
1884             = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))));
1885
1886           if (copy_assignment_arg_p (parmtype, 1))
1887             TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type) = 1;
1888         }
1889     }
1890   else
1891     cp_error ("invalid initializer for virtual method `%D'", decl);
1892 }
1893 \f
1894 void
1895 cplus_decl_attributes (decl, attributes, prefix_attributes)
1896      tree decl, attributes, prefix_attributes;
1897 {
1898   if (decl == NULL_TREE || decl == void_type_node)
1899     return;
1900
1901   if (TREE_CODE (decl) == TEMPLATE_DECL)
1902     decl = DECL_TEMPLATE_RESULT (decl);
1903
1904   decl_attributes (decl, attributes, prefix_attributes);
1905
1906   if (TREE_CODE (decl) == TYPE_DECL)
1907     SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (decl), TREE_TYPE (decl));
1908 }
1909 \f
1910 /* CONSTRUCTOR_NAME:
1911    Return the name for the constructor (or destructor) for the
1912    specified class.  Argument can be RECORD_TYPE, TYPE_DECL, or
1913    IDENTIFIER_NODE.  When given a template, this routine doesn't
1914    lose the specialization.  */
1915
1916 tree
1917 constructor_name_full (thing)
1918      tree thing;
1919 {
1920   if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM
1921       || TREE_CODE (thing) == BOUND_TEMPLATE_TEMPLATE_PARM
1922       || TREE_CODE (thing) == TYPENAME_TYPE)
1923     thing = TYPE_NAME (thing);
1924   else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
1925     {
1926       if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
1927         thing = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0)));
1928       else
1929         thing = TYPE_NAME (thing);
1930     }
1931   if (TREE_CODE (thing) == TYPE_DECL
1932       || (TREE_CODE (thing) == TEMPLATE_DECL
1933           && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
1934     thing = DECL_NAME (thing);
1935   my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
1936   return thing;
1937 }
1938
1939 /* CONSTRUCTOR_NAME:
1940    Return the name for the constructor (or destructor) for the
1941    specified class.  Argument can be RECORD_TYPE, TYPE_DECL, or
1942    IDENTIFIER_NODE.  When given a template, return the plain
1943    unspecialized name.  */
1944
1945 tree
1946 constructor_name (thing)
1947      tree thing;
1948 {
1949   tree t;
1950   thing = constructor_name_full (thing);
1951   t = IDENTIFIER_TEMPLATE (thing);
1952   if (!t)
1953     return thing;
1954   return t;
1955 }
1956 \f
1957 /* Defer the compilation of the FN until the end of compilation.  */
1958
1959 void
1960 defer_fn (fn)
1961      tree fn;
1962 {
1963   if (DECL_DEFERRED_FN (fn))
1964     return;
1965   DECL_DEFERRED_FN (fn) = 1;
1966   if (!deferred_fns)
1967     VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
1968
1969   VARRAY_PUSH_TREE (deferred_fns, fn);
1970 }
1971
1972 /* Hand off a unique name which can be used for variable we don't really
1973    want to know about anyway, for example, the anonymous variables which
1974    are needed to make references work.  Declare this thing so we can use it.
1975    The variable created will be of type TYPE, and will have internal
1976    linkage.  */
1977
1978 tree
1979 get_temp_name (type)
1980      tree type;
1981 {
1982   char buf[sizeof (AUTO_TEMP_FORMAT) + 20];
1983   tree decl;
1984   int toplev = toplevel_bindings_p ();
1985
1986   sprintf (buf, AUTO_TEMP_FORMAT, global_temp_name_counter++);
1987   decl = build_decl (VAR_DECL, get_identifier (buf), type);
1988   DECL_ARTIFICIAL (decl) = 1;
1989   TREE_USED (decl) = 1;
1990   TREE_STATIC (decl) = 1;
1991   
1992   decl = pushdecl_top_level (decl);
1993
1994   /* If this is a local variable, then lay out its rtl now.
1995      Otherwise, callers of this function are responsible for dealing
1996      with this variable's rtl.  */
1997   if (! toplev)
1998     {
1999       expand_decl (decl);
2000       my_friendly_assert (DECL_INITIAL (decl) == NULL_TREE,
2001                           19990826);
2002     }
2003
2004   return decl;
2005 }
2006
2007 /* Hunts through the global anonymous union ANON_DECL, building
2008    appropriate VAR_DECLs.  Stores cleanups on the list of ELEMS, and
2009    returns a VAR_DECL whose size is the same as the size of the
2010    ANON_DECL, if one is available.  */
2011
2012 static tree 
2013 build_anon_union_vars (anon_decl, elems, static_p, external_p)
2014      tree anon_decl;
2015      tree* elems;
2016      int static_p;
2017      int external_p;
2018 {
2019   tree type = TREE_TYPE (anon_decl);
2020   tree main_decl = NULL_TREE;
2021   tree field;
2022
2023   /* Rather than write the code to handle the non-union case,
2024      just give an error.  */
2025   if (TREE_CODE (type) != UNION_TYPE)
2026     error ("anonymous struct not inside named type");
2027
2028   for (field = TYPE_FIELDS (type); 
2029        field != NULL_TREE; 
2030        field = TREE_CHAIN (field))
2031     {
2032       tree decl;
2033
2034       if (DECL_ARTIFICIAL (field))
2035         continue;
2036       if (TREE_CODE (field) != FIELD_DECL)
2037         {
2038           cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2039                          field);
2040           continue;
2041         }
2042
2043       if (TREE_PRIVATE (field))
2044         cp_pedwarn_at ("private member `%#D' in anonymous union", field);
2045       else if (TREE_PROTECTED (field))
2046         cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
2047
2048       if (DECL_NAME (field) == NULL_TREE
2049           && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2050         {
2051           decl = build_anon_union_vars (field, elems, static_p, external_p);
2052           if (!decl)
2053             continue;
2054         }
2055       else if (DECL_NAME (field) == NULL_TREE)
2056         continue;
2057       else
2058         {
2059           decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
2060           /* tell `pushdecl' that this is not tentative.  */
2061           DECL_INITIAL (decl) = error_mark_node;
2062           TREE_PUBLIC (decl) = 0;
2063           TREE_STATIC (decl) = static_p;
2064           DECL_EXTERNAL (decl) = external_p;
2065           decl = pushdecl (decl);
2066           DECL_INITIAL (decl) = NULL_TREE;
2067         }
2068
2069       /* Only write out one anon union element--choose the largest
2070          one.  We used to try to find one the same size as the union,
2071          but that fails if the ABI forces us to align the union more
2072          strictly.  */
2073       if (main_decl == NULL_TREE
2074           || tree_int_cst_lt (DECL_SIZE (main_decl), DECL_SIZE (decl)))
2075         {
2076           if (main_decl)
2077             TREE_ASM_WRITTEN (main_decl) = 1;
2078           main_decl = decl;
2079         }
2080       else 
2081         /* ??? This causes there to be no debug info written out
2082            about this decl.  */
2083         TREE_ASM_WRITTEN (decl) = 1;
2084       
2085       if (DECL_NAME (field) == NULL_TREE
2086           && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2087         /* The remainder of the processing was already done in the
2088            recursive call.  */
2089         continue;
2090
2091       /* If there's a cleanup to do, it belongs in the
2092          TREE_PURPOSE of the following TREE_LIST.  */
2093       *elems = tree_cons (NULL_TREE, decl, *elems);
2094       TREE_TYPE (*elems) = type;
2095     }
2096   
2097   return main_decl;
2098 }
2099
2100 /* Finish off the processing of a UNION_TYPE structure.  If the union is an
2101    anonymous union, then all members must be laid out together.  PUBLIC_P
2102    is nonzero if this union is not declared static.  */
2103
2104 void
2105 finish_anon_union (anon_union_decl)
2106      tree anon_union_decl;
2107 {
2108   tree type = TREE_TYPE (anon_union_decl);
2109   tree main_decl;
2110   int public_p = TREE_PUBLIC (anon_union_decl);
2111   int static_p = TREE_STATIC (anon_union_decl);
2112   int external_p = DECL_EXTERNAL (anon_union_decl);
2113
2114   if (TYPE_FIELDS (type) == NULL_TREE)
2115     return;
2116
2117   if (public_p)
2118     {
2119       error ("namespace-scope anonymous aggregates must be static");
2120       return;
2121     }
2122
2123   main_decl = build_anon_union_vars (anon_union_decl,
2124                                      &DECL_ANON_UNION_ELEMS (anon_union_decl),
2125                                      static_p, external_p);
2126
2127   if (main_decl == NULL_TREE)
2128     {
2129       warning ("anonymous aggregate with no members");
2130       return;
2131     }
2132
2133   if (static_p)
2134     {
2135       make_decl_rtl (main_decl, 0);
2136       COPY_DECL_RTL (main_decl, anon_union_decl);
2137       expand_anon_union_decl (anon_union_decl, 
2138                               NULL_TREE,
2139                               DECL_ANON_UNION_ELEMS (anon_union_decl));
2140     }
2141   else
2142     add_decl_stmt (anon_union_decl);
2143 }
2144
2145 /* Finish processing a builtin type TYPE.  It's name is NAME,
2146    its fields are in the array FIELDS.  LEN is the number of elements
2147    in FIELDS minus one, or put another way, it is the maximum subscript
2148    used in FIELDS.
2149
2150    It is given the same alignment as ALIGN_TYPE.  */
2151
2152 void
2153 finish_builtin_type (type, name, fields, len, align_type)
2154      tree type;
2155      const char *name;
2156      tree fields[];
2157      int len;
2158      tree align_type;
2159 {
2160   register int i;
2161
2162   TYPE_FIELDS (type) = fields[0];
2163   for (i = 0; i < len; i++)
2164     {
2165       layout_type (TREE_TYPE (fields[i]));
2166       DECL_FIELD_CONTEXT (fields[i]) = type;
2167       TREE_CHAIN (fields[i]) = fields[i+1];
2168     }
2169   DECL_FIELD_CONTEXT (fields[i]) = type;
2170   TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2171   TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
2172   layout_type (type);
2173 #if 0 /* not yet, should get fixed properly later */
2174   TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2175 #else
2176   TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2177 #endif
2178   TYPE_STUB_DECL (type) = TYPE_NAME (type);
2179   layout_decl (TYPE_NAME (type), 0);
2180 }
2181 \f
2182 /* Auxiliary functions to make type signatures for
2183    `operator new' and `operator delete' correspond to
2184    what compiler will be expecting.  */
2185
2186 tree
2187 coerce_new_type (type)
2188      tree type;
2189 {
2190   int e = 0;
2191   tree args = TYPE_ARG_TYPES (type);
2192
2193   my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2194   
2195   if (!same_type_p (TREE_TYPE (type), ptr_type_node))
2196     e = 1, cp_error ("`operator new' must return type `%T'", ptr_type_node);
2197
2198   if (!args || args == void_list_node
2199       || !same_type_p (TREE_VALUE (args), c_size_type_node))
2200     {
2201       e = 2;
2202       if (args && args != void_list_node)
2203         args = TREE_CHAIN (args);
2204       cp_error ("`operator new' takes type `size_t' (`%T') as first parameter", c_size_type_node);
2205     }
2206   switch (e)
2207   {
2208     case 2:
2209       args = tree_cons (NULL_TREE, c_size_type_node, args);
2210       /* FALLTHROUGH */
2211     case 1:
2212       type = build_exception_variant
2213               (build_function_type (ptr_type_node, args),
2214                TYPE_RAISES_EXCEPTIONS (type));
2215       /* FALLTHROUGH */
2216     default:;
2217   }
2218   return type;
2219 }
2220
2221 tree
2222 coerce_delete_type (type)
2223      tree type;
2224 {
2225   int e = 0;
2226   tree args = TYPE_ARG_TYPES (type);
2227   
2228   my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2229
2230   if (!same_type_p (TREE_TYPE (type), void_type_node))
2231     e = 1, cp_error ("`operator delete' must return type `%T'", void_type_node);
2232
2233   if (!args || args == void_list_node
2234       || !same_type_p (TREE_VALUE (args), ptr_type_node))
2235     {
2236       e = 2;
2237       if (args && args != void_list_node)
2238         args = TREE_CHAIN (args);
2239       cp_error ("`operator delete' takes type `%T' as first parameter", ptr_type_node);
2240     }
2241   switch (e)
2242   {
2243     case 2:
2244       args = tree_cons (NULL_TREE, ptr_type_node, args);
2245       /* FALLTHROUGH */
2246     case 1:
2247       type = build_exception_variant
2248               (build_function_type (void_type_node, args),
2249                TYPE_RAISES_EXCEPTIONS (type));
2250       /* FALLTHROUGH */
2251     default:;
2252   }
2253
2254   return type;
2255 }
2256 \f
2257 static void
2258 mark_vtable_entries (decl)
2259      tree decl;
2260 {
2261   tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2262
2263   for (; entries; entries = TREE_CHAIN (entries))
2264     {
2265       tree fnaddr;
2266       tree fn;
2267
2268       fnaddr = (flag_vtable_thunks ? TREE_VALUE (entries) 
2269                 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries)));
2270
2271       if (TREE_CODE (fnaddr) != ADDR_EXPR)
2272         /* This entry is an offset: a virtual base class offset, a
2273            virtual call offset, an RTTI offset, etc.  */
2274         continue;
2275
2276       fn = TREE_OPERAND (fnaddr, 0);
2277       TREE_ADDRESSABLE (fn) = 1;
2278       /* When we don't have vcall offsets, we output thunks whenever
2279          we output the vtables that contain them.  With vcall offsets,
2280          we know all the thunks we'll need when we emit a virtual
2281          function, so we emit the thunks there instead.  */
2282       if (DECL_THUNK_P (fn)) 
2283         use_thunk (fn, THUNK_GENERATE_WITH_VTABLE_P (fn));
2284       mark_used (fn);
2285     }
2286 }
2287
2288 /* Set DECL up to have the closest approximation of "initialized common"
2289    linkage available.  */
2290
2291 void
2292 comdat_linkage (decl)
2293      tree decl;
2294 {
2295   if (flag_weak)
2296     make_decl_one_only (decl);
2297   else if (TREE_CODE (decl) == FUNCTION_DECL 
2298            || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
2299     /* We can just emit function and compiler-generated variables
2300        statically; having multiple copies is (for the most part) only
2301        a waste of space.  
2302
2303        There are two correctness issues, however: the address of a
2304        template instantiation with external linkage should be the
2305        same, independent of what translation unit asks for the
2306        address, and this will not hold when we emit multiple copies of
2307        the function.  However, there's little else we can do.  
2308
2309        Also, by default, the typeinfo implementation for the new ABI
2310        assumes that there will be only one copy of the string used as
2311        the name for each type.  Therefore, if weak symbols are
2312        unavailable, the run-time library should perform a more
2313        conservative check; it should perform a string comparison,
2314        rather than an address comparison.  */
2315     TREE_PUBLIC (decl) = 0;
2316   else
2317     {
2318       /* Static data member template instantiations, however, cannot
2319          have multiple copies.  */
2320       if (DECL_INITIAL (decl) == 0
2321           || DECL_INITIAL (decl) == error_mark_node)
2322         DECL_COMMON (decl) = 1;
2323       else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2324         {
2325           DECL_COMMON (decl) = 1;
2326           DECL_INITIAL (decl) = error_mark_node;
2327         }
2328       else
2329         {
2330           /* We can't do anything useful; leave vars for explicit
2331              instantiation.  */
2332           DECL_EXTERNAL (decl) = 1;
2333           DECL_NOT_REALLY_EXTERN (decl) = 0;
2334         }
2335     }
2336
2337   if (DECL_LANG_SPECIFIC (decl))
2338     DECL_COMDAT (decl) = 1;
2339 }
2340
2341 /* For win32 we also want to put explicit instantiations in
2342    linkonce sections, so that they will be merged with implicit
2343    instantiations; otherwise we get duplicate symbol errors.  */
2344
2345 void
2346 maybe_make_one_only (decl)
2347      tree decl;
2348 {
2349   /* We used to say that this was not necessary on targets that support weak
2350      symbols, because the implicit instantiations will defer to the explicit
2351      one.  However, that's not actually the case in SVR4; a strong definition
2352      after a weak one is an error.  Also, not making explicit
2353      instantiations one_only means that we can end up with two copies of
2354      some template instantiations. */
2355   if (! supports_one_only ())
2356     return;
2357
2358   /* We can't set DECL_COMDAT on functions, or finish_file will think
2359      we can get away with not emitting them if they aren't used.  We need
2360      to for variables so that cp_finish_decl will update their linkage,
2361      because their DECL_INITIAL may not have been set properly yet.  */
2362
2363   make_decl_one_only (decl);
2364
2365   if (TREE_CODE (decl) == VAR_DECL && DECL_LANG_SPECIFIC (decl))
2366     DECL_COMDAT (decl) = 1;
2367 }
2368
2369 /* Returns the virtual function with which the vtable for TYPE is
2370    emitted, or NULL_TREE if that heuristic is not applicable to TYPE.  */
2371
2372 static tree
2373 key_method (type)
2374      tree type;
2375 {
2376   tree method;
2377
2378   if (TYPE_FOR_JAVA (type)
2379       || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2380       || CLASSTYPE_INTERFACE_KNOWN (type))
2381     return NULL_TREE;
2382
2383   for (method = TYPE_METHODS (type); method != NULL_TREE;
2384        method = TREE_CHAIN (method))
2385     if (DECL_VINDEX (method) != NULL_TREE
2386         && ! DECL_DECLARED_INLINE_P (method)
2387         && (! DECL_PURE_VIRTUAL_P (method) || DECL_DESTRUCTOR_P (method)))
2388       return method;
2389
2390   return NULL_TREE;
2391 }
2392
2393 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2394    based on TYPE and other static flags.
2395
2396    Note that anything public is tagged TREE_PUBLIC, whether
2397    it's public in this file or in another one.  */
2398
2399 void
2400 import_export_vtable (decl, type, final)
2401      tree decl, type;
2402      int final;
2403 {
2404   if (DECL_INTERFACE_KNOWN (decl))
2405     return;
2406
2407   if (TYPE_FOR_JAVA (type))
2408     {
2409       TREE_PUBLIC (decl) = 1;
2410       DECL_EXTERNAL (decl) = 1;
2411       DECL_INTERFACE_KNOWN (decl) = 1;
2412     }
2413   else if (CLASSTYPE_INTERFACE_KNOWN (type))
2414     {
2415       TREE_PUBLIC (decl) = 1;
2416       DECL_EXTERNAL (decl) = CLASSTYPE_INTERFACE_ONLY (type);
2417       DECL_INTERFACE_KNOWN (decl) = 1;
2418     }
2419   else
2420     {
2421       /* We can only wait to decide if we have real non-inline virtual
2422          functions in our class, or if we come from a template.  */
2423
2424       int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2425                    || key_method (type));
2426
2427       if (final || ! found)
2428         {
2429           comdat_linkage (decl);
2430           DECL_EXTERNAL (decl) = 0;
2431         }
2432       else
2433         {
2434           TREE_PUBLIC (decl) = 1;
2435           DECL_EXTERNAL (decl) = 1;
2436         }
2437     }
2438 }
2439
2440 /* Determine whether or not we want to specifically import or export CTYPE,
2441    using various heuristics.  */
2442
2443 static void
2444 import_export_class (ctype)
2445      tree ctype;
2446 {
2447   /* -1 for imported, 1 for exported.  */
2448   int import_export = 0;
2449
2450   /* It only makes sense to call this function at EOF.  The reason is
2451      that this function looks at whether or not the first non-inline
2452      non-abstract virtual member function has been defined in this
2453      translation unit.  But, we can't possibly know that until we've
2454      seen the entire translation unit.  */
2455   my_friendly_assert (at_eof, 20000226);
2456
2457   if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2458     return;
2459
2460   /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2461      we will have CLASSTYPE_INTERFACE_ONLY set but not
2462      CLASSTYPE_INTERFACE_KNOWN.  In that case, we don't want to use this
2463      heuristic because someone will supply a #pragma implementation
2464      elsewhere, and deducing it here would produce a conflict.  */
2465   if (CLASSTYPE_INTERFACE_ONLY (ctype))
2466     return;
2467
2468 #ifdef VALID_MACHINE_TYPE_ATTRIBUTE
2469   /* FIXME this should really use some sort of target-independent macro.  */
2470   if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2471     import_export = -1;
2472   else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2473     import_export = 1;
2474 #endif
2475
2476   /* If we got -fno-implicit-templates, we import template classes that
2477      weren't explicitly instantiated.  */
2478   if (import_export == 0
2479       && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2480       && ! flag_implicit_templates)
2481     import_export = -1;
2482
2483   /* Base our import/export status on that of the first non-inline,
2484      non-pure virtual function, if any.  */
2485   if (import_export == 0
2486       && TYPE_POLYMORPHIC_P (ctype))
2487     {
2488       tree method = key_method (ctype);
2489       if (method)
2490         import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2491     }
2492
2493 #ifdef MULTIPLE_SYMBOL_SPACES
2494   if (import_export == -1)
2495     import_export = 0;
2496 #endif
2497
2498   if (import_export)
2499     {
2500       SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2501       CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2502     }
2503 }
2504     
2505 /* We need to describe to the assembler the relationship between
2506    a vtable and the vtable of the parent class.  */
2507
2508 static void
2509 output_vtable_inherit (vars)
2510      tree vars;
2511 {
2512   tree parent;
2513   rtx op[2];
2514
2515   op[0] = XEXP (DECL_RTL (vars), 0);      /* strip the mem ref  */
2516
2517   parent = binfo_for_vtable (vars);
2518
2519   if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
2520     op[1] = const0_rtx;
2521   else if (parent)
2522     {
2523       parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent)));
2524       op[1] = XEXP (DECL_RTL (parent), 0);  /* strip the mem ref  */
2525     }
2526   else
2527     my_friendly_abort (980826);
2528
2529   output_asm_insn (".vtable_inherit %c0, %c1", op);
2530 }
2531
2532 static int
2533 finish_vtable_vardecl (t, data)
2534      tree *t;
2535      void *data ATTRIBUTE_UNUSED;
2536 {
2537   tree vars = *t;
2538   tree ctype = DECL_CONTEXT (vars);
2539   import_export_class (ctype);
2540   import_export_vtable (vars, ctype, 1);
2541
2542   if (! DECL_EXTERNAL (vars)
2543       && DECL_NEEDED_P (vars)
2544       && ! TREE_ASM_WRITTEN (vars))
2545     {
2546       if (TREE_TYPE (vars) == void_type_node)
2547         /* It is a dummy vtable made by get_vtable_decl. Ignore it.  */
2548         return 0;
2549       
2550       /* Write it out.  */
2551       mark_vtable_entries (vars);
2552       if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2553         store_init_value (vars, DECL_INITIAL (vars));
2554
2555       if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2556         {
2557           /* Mark the VAR_DECL node representing the vtable itself as a
2558              "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2559              It is rather important that such things be ignored because
2560              any effort to actually generate DWARF for them will run
2561              into trouble when/if we encounter code like:
2562
2563                 #pragma interface
2564                 struct S { virtual void member (); };
2565
2566               because the artificial declaration of the vtable itself (as
2567               manufactured by the g++ front end) will say that the vtable
2568               is a static member of `S' but only *after* the debug output
2569               for the definition of `S' has already been output.  This causes
2570               grief because the DWARF entry for the definition of the vtable
2571               will try to refer back to an earlier *declaration* of the
2572               vtable as a static member of `S' and there won't be one.
2573               We might be able to arrange to have the "vtable static member"
2574               attached to the member list for `S' before the debug info for
2575               `S' get written (which would solve the problem) but that would
2576               require more intrusive changes to the g++ front end.  */
2577
2578           DECL_IGNORED_P (vars) = 1;
2579         }
2580
2581       /* Always make vtables weak.  */
2582       if (flag_weak)
2583         comdat_linkage (vars);
2584
2585       rest_of_decl_compilation (vars, NULL, 1, 1);
2586
2587       if (flag_vtable_gc)
2588         output_vtable_inherit (vars);
2589
2590       /* Because we're only doing syntax-checking, we'll never end up
2591          actually marking the variable as written.  */
2592       if (flag_syntax_only)
2593         TREE_ASM_WRITTEN (vars) = 1;
2594
2595       /* Since we're writing out the vtable here, also write the debug 
2596          info.  */
2597       note_debug_info_needed (ctype);
2598
2599       return 1;
2600     }
2601
2602   /* If the references to this class' vtables were optimized away, still
2603      emit the appropriate debugging information.  See dfs_debug_mark.  */
2604   if (DECL_COMDAT (vars)
2605       && CLASSTYPE_DEBUG_REQUESTED (ctype))
2606     note_debug_info_needed (ctype);
2607
2608   return 0;
2609 }
2610
2611 static int
2612 prune_vtable_vardecl (t, data)
2613      tree *t;
2614      void *data ATTRIBUTE_UNUSED;
2615 {
2616   *t = TREE_CHAIN (*t);
2617   return 1;
2618 }
2619
2620 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2621    inline function or template instantiation at end-of-file.  */
2622
2623 void
2624 import_export_decl (decl)
2625      tree decl;
2626 {
2627   if (DECL_INTERFACE_KNOWN (decl))
2628     return;
2629
2630   if (DECL_TEMPLATE_INSTANTIATION (decl)
2631       || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2632     {
2633       DECL_NOT_REALLY_EXTERN (decl) = 1;
2634       if ((DECL_IMPLICIT_INSTANTIATION (decl)
2635            || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2636           && (flag_implicit_templates
2637               || (flag_implicit_inline_templates 
2638                   && DECL_DECLARED_INLINE_P (decl))))
2639         {
2640           if (!TREE_PUBLIC (decl))
2641             /* Templates are allowed to have internal linkage.  See 
2642                [basic.link].  */
2643             ;
2644           else
2645             comdat_linkage (decl);
2646         }
2647       else
2648         DECL_NOT_REALLY_EXTERN (decl) = 0;
2649     }
2650   else if (DECL_FUNCTION_MEMBER_P (decl))
2651     {
2652       if (!DECL_DECLARED_INLINE_P (decl))
2653         {
2654           tree ctype = DECL_CONTEXT (decl);
2655           import_export_class (ctype);
2656           if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2657             {
2658               DECL_NOT_REALLY_EXTERN (decl)
2659                 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2660                      || (DECL_DECLARED_INLINE_P (decl) 
2661                          && ! flag_implement_inlines
2662                          && !DECL_VINDEX (decl)));
2663
2664               /* Always make artificials weak.  */
2665               if (DECL_ARTIFICIAL (decl) && flag_weak)
2666                 comdat_linkage (decl);
2667               else
2668                 maybe_make_one_only (decl);
2669             }
2670         }
2671       else
2672         comdat_linkage (decl);
2673     }
2674   else if (tinfo_decl_p (decl, 0))
2675     {
2676       tree ctype = TREE_TYPE (DECL_NAME (decl));
2677
2678       if (IS_AGGR_TYPE (ctype))
2679         import_export_class (ctype);
2680
2681       if (IS_AGGR_TYPE (ctype) && CLASSTYPE_INTERFACE_KNOWN (ctype)
2682           && TYPE_POLYMORPHIC_P (ctype)
2683           /* If -fno-rtti, we're not necessarily emitting this stuff with
2684              the class, so go ahead and emit it now.  This can happen
2685              when a class is used in exception handling.  */
2686           && flag_rtti
2687           /* If the type is a cv-qualified variant of a type, then we
2688              must emit the tinfo function in this translation unit
2689              since it will not be emitted when the vtable for the type
2690              is output (which is when the unqualified version is
2691              generated).  */
2692           && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2693         {
2694           DECL_NOT_REALLY_EXTERN (decl)
2695             = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2696                  || (DECL_DECLARED_INLINE_P (decl) 
2697                      && ! flag_implement_inlines
2698                      && !DECL_VINDEX (decl)));
2699
2700           /* Always make artificials weak.  */
2701           if (flag_weak)
2702             comdat_linkage (decl);
2703         }
2704       else if (TYPE_BUILT_IN (ctype) 
2705                && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2706         DECL_NOT_REALLY_EXTERN (decl) = 0;
2707       else
2708         comdat_linkage (decl);
2709     } 
2710   else
2711     comdat_linkage (decl);
2712
2713   DECL_INTERFACE_KNOWN (decl) = 1;
2714 }
2715
2716 tree
2717 build_cleanup (decl)
2718      tree decl;
2719 {
2720   tree temp;
2721   tree type = TREE_TYPE (decl);
2722
2723   if (TREE_CODE (type) == ARRAY_TYPE)
2724     temp = decl;
2725   else
2726     {
2727       mark_addressable (decl);
2728       temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2729     }
2730   temp = build_delete (TREE_TYPE (temp), temp,
2731                        sfk_complete_destructor,
2732                        LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2733   return temp;
2734 }
2735
2736 /* Returns the initialization guard variable for the variable DECL,
2737    which has static storage duration.  */
2738
2739 tree
2740 get_guard (decl)
2741      tree decl;
2742 {
2743   tree sname;
2744   tree guard;
2745
2746   sname = mangle_guard_variable (decl);
2747   guard = IDENTIFIER_GLOBAL_VALUE (sname);
2748   if (! guard)
2749     {
2750       tree guard_type;
2751
2752       /* Under the new ABI, we use a type that is big enough to
2753          contain a mutex as well as an integer counter.  */
2754       guard_type = long_long_integer_type_node;
2755       guard = build_decl (VAR_DECL, sname, guard_type);
2756       
2757       /* The guard should have the same linkage as what it guards. */
2758       TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2759       TREE_STATIC (guard) = TREE_STATIC (decl);
2760       DECL_COMMON (guard) = DECL_COMMON (decl);
2761       DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
2762       if (TREE_PUBLIC (decl))
2763         DECL_WEAK (guard) = DECL_WEAK (decl);
2764       
2765       DECL_ARTIFICIAL (guard) = 1;
2766       TREE_USED (guard) = 1;
2767       pushdecl_top_level (guard);
2768       cp_finish_decl (guard, NULL_TREE, NULL_TREE, 0);
2769     }
2770   return guard;
2771 }
2772
2773 /* Return those bits of the GUARD variable that should be set when the
2774    guarded entity is actually initialized.  */
2775
2776 static tree
2777 get_guard_bits (guard)
2778      tree guard;
2779 {
2780   /* Under the new ABI, we only set the first byte of the guard,
2781      in order to leave room for a mutex in the high-order bits.  */
2782   guard = build1 (ADDR_EXPR, 
2783                   build_pointer_type (TREE_TYPE (guard)),
2784                   guard);
2785   guard = build1 (NOP_EXPR, 
2786                   build_pointer_type (char_type_node), 
2787                   guard);
2788   guard = build1 (INDIRECT_REF, char_type_node, guard);
2789
2790   return guard;
2791 }
2792
2793 /* Return an expression which determines whether or not the GUARD
2794    variable has already been initialized.  */
2795
2796 tree
2797 get_guard_cond (guard)
2798      tree guard;
2799 {
2800   tree guard_value;
2801
2802   /* Check to see if the GUARD is zero.  */
2803   guard = get_guard_bits (guard);
2804   guard_value = integer_zero_node;
2805   if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2806     guard_value = convert (TREE_TYPE (guard), guard_value);
2807   return cp_build_binary_op (EQ_EXPR, guard, guard_value);
2808 }
2809
2810 /* Return an expression which sets the GUARD variable, indicating that
2811    the variable being guarded has been initialized.  */
2812
2813 tree
2814 set_guard (guard)
2815      tree guard;
2816 {
2817   tree guard_init;
2818
2819   /* Set the GUARD to one.  */
2820   guard = get_guard_bits (guard);
2821   guard_init = integer_one_node;
2822   if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2823     guard_init = convert (TREE_TYPE (guard), guard_init);
2824   return build_modify_expr (guard, NOP_EXPR, guard_init);
2825 }
2826
2827 /* Start the process of running a particular set of global constructors
2828    or destructors.  Subroutine of do_[cd]tors.  */
2829
2830 static tree
2831 start_objects (method_type, initp)
2832      int method_type, initp;
2833 {
2834   tree fnname;
2835   tree body;
2836   char type[10];
2837
2838   /* Make ctor or dtor function.  METHOD_TYPE may be 'I' or 'D'.  */
2839
2840   if (initp != DEFAULT_INIT_PRIORITY)
2841     {
2842       char joiner;
2843
2844 #ifdef JOINER
2845       joiner = JOINER;
2846 #else
2847       joiner = '_';
2848 #endif
2849
2850       sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2851     }
2852   else
2853     sprintf (type, "%c", method_type);
2854
2855   fnname = get_file_function_name_long (type);
2856
2857   start_function (void_list_node,
2858                   make_call_declarator (fnname, void_list_node, NULL_TREE,
2859                                         NULL_TREE),
2860                   NULL_TREE, SF_DEFAULT);
2861
2862 #if defined(ASM_OUTPUT_CONSTRUCTOR) && defined(ASM_OUTPUT_DESTRUCTOR)
2863   /* It can be a static function as long as collect2 does not have
2864      to scan the object file to find its ctor/dtor routine.  */
2865   TREE_PUBLIC (current_function_decl) = 0;
2866 #endif
2867
2868   /* Mark this declaration as used to avoid spurious warnings.  */
2869   TREE_USED (current_function_decl) = 1;
2870
2871   /* Mark this function as a global constructor or destructor.  */
2872   if (method_type == 'I')
2873     DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2874   else
2875     DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2876   GLOBAL_INIT_PRIORITY (current_function_decl) = initp;
2877
2878   body = begin_compound_stmt (/*has_no_scope=*/0);
2879
2880   /* We cannot allow these functions to be elided, even if they do not
2881      have external linkage.  And, there's no point in deferring
2882      copmilation of thes functions; they're all going to have to be
2883      out anyhow.  */
2884   current_function_cannot_inline
2885     = "static constructors and destructors cannot be inlined";
2886
2887   return body;
2888 }
2889
2890 /* Finish the process of running a particular set of global constructors
2891    or destructors.  Subroutine of do_[cd]tors.  */
2892
2893 static void
2894 finish_objects (method_type, initp, body)
2895      int method_type, initp;
2896      tree body;
2897 {
2898   const char *fnname;
2899   tree fn;
2900
2901   /* Finish up.  */
2902   finish_compound_stmt (/*has_no_scope=*/0, body);
2903   fn = finish_function (0);
2904   expand_body (fn);
2905
2906   /* When only doing semantic analysis, and no RTL generation, we
2907      can't call functions that directly emit assembly code; there is
2908      no assembly file in which to put the code.  */
2909   if (flag_syntax_only)
2910     return;
2911
2912   fnname = XSTR (XEXP (DECL_RTL (fn), 0), 0);
2913   if (initp == DEFAULT_INIT_PRIORITY)
2914     {
2915       if (method_type == 'I')
2916         assemble_constructor (fnname);
2917       else
2918         assemble_destructor (fnname);
2919     }
2920 #if defined (ASM_OUTPUT_SECTION_NAME) && defined (ASM_OUTPUT_CONSTRUCTOR)
2921   /* If we're using init priority we can't use assemble_*tor, but on ELF
2922      targets we can stick the references into named sections for GNU ld
2923      to collect.  */
2924   else
2925     {
2926       char buf[15];
2927       sprintf (buf, ".%ctors.%.5u", method_type == 'I' ? 'c' : 'd',
2928                /* invert the numbering so the linker puts us in the proper
2929                   order; constructors are run from right to left, and the
2930                   linker sorts in increasing order.  */
2931                MAX_INIT_PRIORITY - initp);
2932       named_section (NULL_TREE, buf, 0);
2933       assemble_integer (gen_rtx_SYMBOL_REF (Pmode, fnname),
2934                         POINTER_SIZE / BITS_PER_UNIT, 1);
2935     }
2936 #endif
2937 }
2938
2939 /* The names of the parameters to the function created to handle
2940    initializations and destructions for objects with static storage
2941    duration.  */
2942 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2943 #define PRIORITY_IDENTIFIER "__priority"
2944
2945 /* The name of the function we create to handle initializations and
2946    destructions for objects with static storage duration.  */
2947 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2948
2949 /* The declaration for the __INITIALIZE_P argument.  */
2950 static tree initialize_p_decl;
2951
2952 /* The declaration for the __PRIORITY argument.  */
2953 static tree priority_decl;
2954
2955 /* The declaration for the static storage duration function.  */
2956 static tree ssdf_decl;
2957
2958 /* All the static storage duration functions created in this
2959    translation unit.  */
2960 static varray_type ssdf_decls;
2961
2962 /* A map from priority levels to information about that priority
2963    level.  There may be many such levels, so efficient lookup is
2964    important.  */
2965 static splay_tree priority_info_map;
2966
2967 /* Begins the generation of the function that will handle all
2968    initialization and destruction of objects with static storage
2969    duration.  The function generated takes two parameters of type
2970    `int': __INITIALIZE_P and __PRIORITY.  If __INITIALIZE_P is
2971    non-zero, it performs initializations.  Otherwise, it performs
2972    destructions.  It only performs those initializations or
2973    destructions with the indicated __PRIORITY.  The generated function
2974    returns no value.  
2975
2976    It is assumed that this function will only be called once per
2977    translation unit.  */
2978
2979 static tree
2980 start_static_storage_duration_function ()
2981 {
2982   static unsigned ssdf_number;
2983
2984   tree parm_types;
2985   tree type;
2986   tree body;
2987   char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2988
2989   /* Create the identifier for this function.  It will be of the form
2990      SSDF_IDENTIFIER_<number>.  */
2991   sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++);
2992   if (ssdf_number == 0)
2993     {
2994       /* Overflow occurred.  That means there are at least 4 billion
2995          initialization functions.  */
2996       sorry ("too many initialization functions required");
2997       my_friendly_abort (19990430);
2998     }
2999
3000   /* Create the parameters.  */
3001   parm_types = void_list_node;
3002   parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
3003   parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
3004   type = build_function_type (void_type_node, parm_types);
3005
3006   /* Create the FUNCTION_DECL itself.  */
3007   ssdf_decl = build_lang_decl (FUNCTION_DECL, 
3008                                get_identifier (id),
3009                                type);
3010   TREE_PUBLIC (ssdf_decl) = 0;
3011   DECL_ARTIFICIAL (ssdf_decl) = 1;
3012
3013   /* Put this function in the list of functions to be called from the
3014      static constructors and destructors.  */
3015   if (!ssdf_decls)
3016     {
3017       VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
3018
3019       /* Take this opportunity to initialize the map from priority
3020          numbers to information about that priority level. */
3021       priority_info_map = splay_tree_new (splay_tree_compare_ints,
3022                                           /*delete_key_fn=*/0,
3023                                           /*delete_value_fn=*/
3024                                           (splay_tree_delete_value_fn) &free);
3025
3026       /* We always need to generate functions for the
3027          DEFAULT_INIT_PRIORITY so enter it now.  That way when we walk
3028          priorities later, we'll be sure to find the
3029          DEFAULT_INIT_PRIORITY.  */
3030       get_priority_info (DEFAULT_INIT_PRIORITY);
3031     }
3032
3033   VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
3034
3035   /* Create the argument list.  */
3036   initialize_p_decl = build_decl (PARM_DECL,
3037                                   get_identifier (INITIALIZE_P_IDENTIFIER),
3038                                   integer_type_node);
3039   DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3040   DECL_ARG_TYPE (initialize_p_decl) = integer_type_node;
3041   TREE_USED (initialize_p_decl) = 1;
3042   priority_decl = build_decl (PARM_DECL, get_identifier (PRIORITY_IDENTIFIER),
3043                               integer_type_node);
3044   DECL_CONTEXT (priority_decl) = ssdf_decl;
3045   DECL_ARG_TYPE (priority_decl) = integer_type_node;
3046   TREE_USED (priority_decl) = 1;
3047
3048   TREE_CHAIN (initialize_p_decl) = priority_decl;
3049   DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3050
3051   /* Put the function in the global scope.  */
3052   pushdecl (ssdf_decl);
3053
3054   /* Start the function itself.  This is equivalent to declarating the
3055      function as:
3056
3057        static void __ssdf (int __initialize_p, init __priority_p);
3058        
3059      It is static because we only need to call this function from the
3060      various constructor and destructor functions for this module.  */
3061   start_function (/*specs=*/NULL_TREE, 
3062                   ssdf_decl,
3063                   /*attrs=*/NULL_TREE,
3064                   SF_PRE_PARSED);
3065
3066   /* Set up the scope of the outermost block in the function.  */
3067   body = begin_compound_stmt (/*has_no_scope=*/0);
3068
3069   /* This function must not be deferred because we are depending on
3070      its compilation to tell us what is TREE_SYMBOL_REFERENCED.  */
3071   current_function_cannot_inline 
3072     = "static storage duration functions cannot be inlined";
3073
3074   return body;
3075 }
3076
3077 /* Finish the generation of the function which performs initialization
3078    and destruction of objects with static storage duration.  After
3079    this point, no more such objects can be created.  */
3080
3081 static void
3082 finish_static_storage_duration_function (body)
3083      tree body;
3084 {
3085   /* Close out the function.  */
3086   finish_compound_stmt (/*has_no_scope=*/0, body);
3087   expand_body (finish_function (0));
3088 }
3089
3090 /* Return the information about the indicated PRIORITY level.  If no
3091    code to handle this level has yet been generated, generate the
3092    appropriate prologue.  */
3093
3094 static priority_info
3095 get_priority_info (priority)
3096      int priority;
3097 {
3098   priority_info pi;
3099   splay_tree_node n;
3100
3101   n = splay_tree_lookup (priority_info_map, 
3102                          (splay_tree_key) priority);
3103   if (!n)
3104     {
3105       /* Create a new priority information structure, and insert it
3106          into the map.  */
3107       pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
3108       pi->initializations_p = 0;
3109       pi->destructions_p = 0;
3110       splay_tree_insert (priority_info_map,
3111                          (splay_tree_key) priority,
3112                          (splay_tree_value) pi);
3113     }
3114   else
3115     pi = (priority_info) n->value;
3116
3117   return pi;
3118 }
3119
3120 /* Set up to handle the initialization or destruction of DECL.  If
3121    INITP is non-zero, we are initializing the variable.  Otherwise, we
3122    are destroying it.  */
3123
3124 static tree
3125 start_static_initialization_or_destruction (decl, initp)
3126      tree decl;
3127      int initp;
3128 {
3129   tree guard_if_stmt = NULL_TREE;
3130   int priority;
3131   tree cond;
3132   tree guard;
3133   tree init_cond;
3134   priority_info pi;
3135
3136   /* Figure out the priority for this declaration.  */
3137   priority = DECL_INIT_PRIORITY (decl);
3138   if (!priority)
3139     priority = DEFAULT_INIT_PRIORITY;
3140
3141   /* Remember that we had an initialization or finalization at this
3142      priority.  */
3143   pi = get_priority_info (priority);
3144   if (initp)
3145     pi->initializations_p = 1;
3146   else
3147     pi->destructions_p = 1;
3148
3149   /* Trick the compiler into thinking we are at the file and line
3150      where DECL was declared so that error-messages make sense, and so
3151      that the debugger will show somewhat sensible file and line
3152      information.  */
3153   input_filename = DECL_SOURCE_FILE (decl);
3154   lineno = DECL_SOURCE_LINE (decl);
3155
3156   /* Because of:
3157
3158        [class.access.spec]
3159
3160        Access control for implicit calls to the constructors,
3161        the conversion functions, or the destructor called to
3162        create and destroy a static data member is performed as
3163        if these calls appeared in the scope of the member's
3164        class.  
3165
3166      we pretend we are in a static member function of the class of
3167      which the DECL is a member.  */
3168   if (member_p (decl))
3169     {
3170       DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3171       DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3172     }
3173   
3174   /* Conditionalize this initialization on being in the right priority
3175      and being initializing/finalizing appropriately.  */
3176   guard_if_stmt = begin_if_stmt ();
3177   cond = cp_build_binary_op (EQ_EXPR,
3178                              priority_decl,
3179                              build_int_2 (priority, 0));
3180   init_cond = initp ? integer_one_node : integer_zero_node;
3181   init_cond = cp_build_binary_op (EQ_EXPR,
3182                                   initialize_p_decl,
3183                                   init_cond);
3184   cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
3185
3186   /* Assume we don't need a guard.  */
3187   guard = NULL_TREE;
3188   /* We need a guard if this is an object with external linkage that
3189      might be initialized in more than one place.  (For example, a
3190      static data member of a template, when the data member requires
3191      construction.)  */
3192   if (TREE_PUBLIC (decl) && (DECL_COMMON (decl) 
3193                              || DECL_ONE_ONLY (decl)
3194                              || DECL_WEAK (decl)))
3195     {
3196       tree guard_cond;
3197
3198       guard = get_guard (decl);
3199
3200       /* When using __cxa_atexit, we just check the GUARD as we would
3201          for a local static.  */
3202       if (flag_use_cxa_atexit)
3203         {
3204           /* When using __cxa_atexit, we never try to destroy
3205              anything from a static destructor.  */
3206           my_friendly_assert (initp, 20000629);
3207           guard_cond = get_guard_cond (guard);
3208         }
3209       /* If we don't have __cxa_atexit, then we will be running
3210          destructors from .fini sections, or their equivalents.  So,
3211          we need to know how many times we've tried to initialize this
3212          object.  We do initializations only if the GUARD is zero,
3213          i.e., if we are the first to initialize the variable.  We do
3214          destructions only if the GUARD is one, i.e., if we are the
3215          last to destroy the variable.  */
3216       else if (initp)
3217         guard_cond 
3218           = cp_build_binary_op (EQ_EXPR,
3219                                 build_unary_op (PREINCREMENT_EXPR,
3220                                                 guard,
3221                                                 /*noconvert=*/1),
3222                                 integer_one_node);
3223       else
3224         guard_cond 
3225           = cp_build_binary_op (EQ_EXPR,
3226                                 build_unary_op (PREDECREMENT_EXPR,
3227                                                 guard,
3228                                                 /*noconvert=*/1),
3229                                 integer_zero_node);
3230
3231       cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond);
3232     }
3233
3234   finish_if_stmt_cond (cond, guard_if_stmt);
3235
3236   /* If we're using __cxa_atexit, we have not already set the GUARD,
3237      so we must do so now.  */
3238   if (guard && initp && flag_use_cxa_atexit)
3239     finish_expr_stmt (set_guard (guard));
3240
3241   return guard_if_stmt;
3242 }
3243
3244 /* We've just finished generating code to do an initialization or
3245    finalization.  GUARD_IF_STMT is the if-statement we used to guard
3246    the initialization.  */
3247
3248 static void
3249 finish_static_initialization_or_destruction (guard_if_stmt)
3250      tree guard_if_stmt;
3251 {
3252   finish_then_clause (guard_if_stmt);
3253   finish_if_stmt ();
3254
3255   /* Now that we're done with DECL we don't need to pretend to be a
3256      member of its class any longer.  */
3257   DECL_CONTEXT (current_function_decl) = NULL_TREE;
3258   DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3259 }
3260
3261 /* Generate code to do the static initialization of DECL.  The
3262    initialization is INIT.  If DECL may be initialized more than once
3263    in different object files, GUARD is the guard variable to 
3264    check.  PRIORITY is the priority for the initialization.  */
3265
3266 static void
3267 do_static_initialization (decl, init)
3268      tree decl;
3269      tree init;
3270 {
3271   tree expr;
3272   tree guard_if_stmt;
3273
3274   /* Set up for the initialization.  */
3275   guard_if_stmt
3276     = start_static_initialization_or_destruction (decl,
3277                                                   /*initp=*/1);
3278   
3279   /* Do the initialization itself.  */
3280   if (IS_AGGR_TYPE (TREE_TYPE (decl))
3281       || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3282     expr = build_aggr_init (decl, init, 0);
3283   else
3284     {
3285       expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
3286       TREE_SIDE_EFFECTS (expr) = 1;
3287     }
3288   finish_expr_stmt (expr);
3289
3290   /* If we're using __cxa_atexit, register a a function that calls the
3291      destructor for the object.  */
3292   if (flag_use_cxa_atexit)
3293     register_dtor_fn (decl);
3294
3295   /* Finsh up.  */
3296   finish_static_initialization_or_destruction (guard_if_stmt);
3297 }
3298
3299 /* Generate code to do the static destruction of DECL.  If DECL may be
3300    initialized more than once in different object files, GUARD is the
3301    guard variable to check.  PRIORITY is the priority for the
3302    destruction.  */
3303
3304 static void
3305 do_static_destruction (decl)
3306      tree decl;
3307 {
3308   tree guard_if_stmt;
3309
3310   /* If we're using __cxa_atexit, then destructors are registered
3311      immediately after objects are initialized.  */
3312   my_friendly_assert (!flag_use_cxa_atexit, 20000121);
3313
3314   /* If we don't need a destructor, there's nothing to do.  */
3315   if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3316     return;
3317
3318   /* Actually do the destruction.  */
3319   guard_if_stmt = start_static_initialization_or_destruction (decl,
3320                                                                /*initp=*/0);
3321   finish_expr_stmt (build_cleanup (decl));
3322   finish_static_initialization_or_destruction (guard_if_stmt);
3323 }
3324
3325 /* VARS is a list of variables with static storage duration which may
3326    need initialization and/or finalization.  Remove those variables
3327    that don't really need to be initialized or finalized, and return
3328    the resulting list.  The order in which the variables appear in
3329    VARS is in reverse order of the order in which they should actually
3330    be initialized.  The list we return is in the unreversed order;
3331    i.e., the first variable should be initialized first.  */
3332
3333 static tree
3334 prune_vars_needing_no_initialization (vars)
3335      tree vars;
3336 {
3337   tree var;
3338   tree result;
3339
3340   for (var = vars, result = NULL_TREE;
3341        var;
3342        var = TREE_CHAIN (var))
3343     {
3344       tree decl = TREE_VALUE (var);
3345       tree init = TREE_PURPOSE (var);
3346
3347       /* Deal gracefully with error.  */
3348       if (decl == error_mark_node)
3349         continue;
3350
3351       /* The only things that can be initialized are variables.  */
3352       my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
3353
3354       /* If this object is not defined, we don't need to do anything
3355          here.  */
3356       if (DECL_EXTERNAL (decl))
3357         continue;
3358
3359       /* Also, if the initializer already contains errors, we can bail
3360          out now.  */
3361       if (init && TREE_CODE (init) == TREE_LIST 
3362           && value_member (error_mark_node, init))
3363         continue;
3364
3365       /* This variable is going to need initialization and/or
3366          finalization, so we add it to the list.  */
3367       result = tree_cons (init, decl, result);
3368     }
3369
3370   return result;
3371 }
3372
3373 /* Make sure we have told the back end about all the variables in
3374    VARS.  */
3375
3376 static void
3377 write_out_vars (vars)
3378      tree vars;
3379 {
3380   tree v;
3381
3382   for (v = vars; v; v = TREE_CHAIN (v))
3383     if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
3384       rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
3385 }
3386
3387 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3388    (otherwise) that will initialize all gobal objects with static
3389    storage duration having the indicated PRIORITY.  */
3390
3391 static void
3392 generate_ctor_or_dtor_function (constructor_p, priority)
3393      int constructor_p;
3394      int priority;
3395 {
3396   char function_key;
3397   tree arguments;
3398   tree body;
3399   size_t i;
3400
3401   /* We use `I' to indicate initialization and `D' to indicate
3402      destruction.  */
3403   if (constructor_p)
3404     function_key = 'I';
3405   else
3406     function_key = 'D';
3407
3408   /* Begin the function.  */
3409   body = start_objects (function_key, priority);
3410
3411   /* Call the static storage duration function with appropriate
3412      arguments.  */
3413   for (i = 0; i < ssdf_decls->elements_used; ++i) 
3414     {
3415       arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0), 
3416                              NULL_TREE);
3417       arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
3418                              arguments);
3419       finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i),
3420                                              arguments));
3421     }
3422
3423   /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
3424      calls to any functions marked with attributes indicating that
3425      they should be called at initialization- or destruction-time.  */
3426   if (priority == DEFAULT_INIT_PRIORITY)
3427     {
3428       tree fns;
3429       
3430       for (fns = constructor_p ? static_ctors : static_dtors; 
3431            fns;
3432            fns = TREE_CHAIN (fns))
3433         finish_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE));
3434     }
3435
3436   /* Close out the function.  */
3437   finish_objects (function_key, priority, body);
3438 }
3439
3440 /* Generate constructor and destructor functions for the priority
3441    indicated by N.  */
3442
3443 static int
3444 generate_ctor_and_dtor_functions_for_priority (n, data)
3445      splay_tree_node n;
3446      void *data ATTRIBUTE_UNUSED;
3447 {
3448   int priority = (int) n->key;
3449   priority_info pi = (priority_info) n->value;
3450
3451   /* Generate the functions themselves, but only if they are really
3452      needed.  */
3453   if (pi->initializations_p
3454       || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
3455     generate_ctor_or_dtor_function (/*constructor_p=*/1,
3456                                     priority);
3457   if (pi->destructions_p
3458       || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
3459     generate_ctor_or_dtor_function (/*constructor_p=*/0,
3460                                     priority);
3461
3462   /* Keep iterating.  */
3463   return 0;
3464 }
3465
3466 /* This routine is called from the last rule in yyparse ().
3467    Its job is to create all the code needed to initialize and
3468    destroy the global aggregates.  We do the destruction
3469    first, since that way we only need to reverse the decls once.  */
3470
3471 void
3472 finish_file ()
3473 {
3474   tree vars;
3475   int reconsider;
3476   size_t i;
3477
3478   at_eof = 1;
3479
3480   /* Bad parse errors.  Just forget about it.  */
3481   if (! global_bindings_p () || current_class_type || decl_namespace_list)
3482     return;
3483
3484   /* Otherwise, GDB can get confused, because in only knows
3485      about source for LINENO-1 lines.  */
3486   lineno -= 1;
3487
3488   interface_unknown = 1;
3489   interface_only = 0;
3490
3491   /* We now have to write out all the stuff we put off writing out.
3492      These include:
3493
3494        o Template specializations that we have not yet instantiated,
3495          but which are needed.
3496        o Initialization and destruction for non-local objects with
3497          static storage duration.  (Local objects with static storage
3498          duration are initialized when their scope is first entered,
3499          and are cleaned up via atexit.)
3500        o Virtual function tables.  
3501
3502      All of these may cause others to be needed.  For example,
3503      instantiating one function may cause another to be needed, and
3504      generating the intiailzer for an object may cause templates to be
3505      instantiated, etc., etc.  */
3506
3507   timevar_push (TV_VARCONST);
3508
3509   emit_support_tinfos ();
3510   
3511   do 
3512     {
3513       reconsider = 0;
3514
3515       /* If there are templates that we've put off instantiating, do
3516          them now.  */
3517       instantiate_pending_templates ();
3518
3519       /* Write out virtual tables as required.  Note that writing out
3520          the virtual table for a template class may cause the
3521          instantiation of members of that class.  */
3522       if (walk_globals (vtable_decl_p,
3523                         finish_vtable_vardecl,
3524                         /*data=*/0))
3525         reconsider = 1;
3526       
3527       /* Write out needed type info variables. Writing out one variable
3528          might cause others to be needed.  */
3529       if (walk_globals (tinfo_decl_p, emit_tinfo_decl, /*data=*/0))
3530         reconsider = 1;
3531
3532       /* The list of objects with static storage duration is built up
3533          in reverse order.  We clear STATIC_AGGREGATES so that any new
3534          aggregates added during the initialization of these will be
3535          initialized in the correct order when we next come around the
3536          loop.  */
3537       vars = prune_vars_needing_no_initialization (static_aggregates);
3538       static_aggregates = NULL_TREE;
3539
3540       if (vars)
3541         {
3542           tree v;
3543
3544           /* We need to start a new initialization function each time
3545              through the loop.  That's because we need to know which
3546              vtables have been referenced, and TREE_SYMBOL_REFERENCED
3547              isn't computed until a function is finished, and written
3548              out.  That's a deficiency in the back-end.  When this is
3549              fixed, these initialization functions could all become
3550              inline, with resulting performance improvements.  */
3551           tree ssdf_body = start_static_storage_duration_function ();
3552
3553           /* Make sure the back end knows about all the variables.  */
3554           write_out_vars (vars);
3555
3556           /* First generate code to do all the initializations.  */
3557           for (v = vars; v; v = TREE_CHAIN (v))
3558             do_static_initialization (TREE_VALUE (v),
3559                                       TREE_PURPOSE (v));
3560
3561           /* Then, generate code to do all the destructions.  Do these
3562              in reverse order so that the most recently constructed
3563              variable is the first destroyed.  If we're using
3564              __cxa_atexit, then we don't need to do this; functions
3565              were registered at initialization time to destroy the
3566              local statics.  */
3567           if (!flag_use_cxa_atexit)
3568             {
3569               vars = nreverse (vars);
3570               for (v = vars; v; v = TREE_CHAIN (v))
3571                 do_static_destruction (TREE_VALUE (v));
3572             }
3573           else
3574             vars = NULL_TREE;
3575
3576           /* Finish up the static storage duration function for this
3577              round.  */
3578           finish_static_storage_duration_function (ssdf_body);
3579
3580           /* All those initializations and finalizations might cause
3581              us to need more inline functions, more template
3582              instantiations, etc.  */
3583           reconsider = 1;
3584         }
3585       
3586       /* Go through the various inline functions, and see if any need
3587          synthesizing.  */
3588       for (i = 0; i < deferred_fns_used; ++i)
3589         {
3590           tree decl = VARRAY_TREE (deferred_fns, i);
3591           import_export_decl (decl);
3592           if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3593               && TREE_USED (decl)
3594               && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3595             {
3596               /* Even though we're already at the top-level, we push
3597                  there again.  That way, when we pop back a few lines
3598                  hence, all of our state is restored.  Otherwise,
3599                  finish_function doesn't clean things up, and we end
3600                  up with CURRENT_FUNCTION_DECL set.  */
3601               push_to_top_level ();
3602               synthesize_method (decl);
3603               pop_from_top_level ();
3604               reconsider = 1;
3605             }
3606         }
3607
3608       /* We lie to the back-end, pretending that some functions are
3609          not defined when they really are.  This keeps these functions
3610          from being put out unnecessarily.  But, we must stop lying
3611          when the functions are referenced, or if they are not comdat
3612          since they need to be put out now.  */
3613       for (i = 0; i < deferred_fns_used; ++i)
3614         {
3615           tree decl = VARRAY_TREE (deferred_fns, i);
3616       
3617           if (DECL_NOT_REALLY_EXTERN (decl)
3618               && DECL_INITIAL (decl)
3619               && DECL_NEEDED_P (decl))
3620             DECL_EXTERNAL (decl) = 0;
3621
3622           /* If we're going to need to write this function out, and
3623              there's already a body for it, create RTL for it now.
3624              (There might be no body if this is a method we haven't
3625              gotten around to synthesizing yet.)  */
3626           if (!DECL_EXTERNAL (decl)
3627               && DECL_NEEDED_P (decl)
3628               && DECL_SAVED_TREE (decl)
3629               && !DECL_SAVED_INSNS (decl)
3630               && !TREE_ASM_WRITTEN (decl))
3631             {
3632               int saved_not_really_extern;
3633
3634               /* When we call finish_function in expand_body, it will
3635                  try to reset DECL_NOT_REALLY_EXTERN so we save and
3636                  restore it here.  */
3637               saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl);
3638               /* Generate RTL for this function now that we know we
3639                  need it.  */
3640               expand_body (decl);
3641               /* Undo the damage done by finish_function.  */
3642               DECL_EXTERNAL (decl) = 0;
3643               DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern;
3644               /* If we're compiling -fsyntax-only pretend that this
3645                  function has been written out so that we don't try to
3646                  expand it again.  */
3647               if (flag_syntax_only)
3648                 TREE_ASM_WRITTEN (decl) = 1;
3649               reconsider = 1;
3650             }
3651         }
3652
3653       if (deferred_fns_used
3654           && wrapup_global_declarations (&VARRAY_TREE (deferred_fns, 0),
3655                                          deferred_fns_used))
3656         reconsider = 1;
3657       if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3658         reconsider = 1;
3659
3660       /* Static data members are just like namespace-scope globals.  */
3661       for (i = 0; i < pending_statics_used; ++i) 
3662         {
3663           tree decl = VARRAY_TREE (pending_statics, i);
3664           if (TREE_ASM_WRITTEN (decl))
3665             continue;
3666           import_export_decl (decl);
3667           if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
3668             DECL_EXTERNAL (decl) = 0;
3669         }
3670       if (pending_statics
3671           && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
3672                                          pending_statics_used))
3673         reconsider = 1;
3674     } 
3675   while (reconsider);
3676
3677   /* We give C linkage to static constructors and destructors.  */
3678   push_lang_context (lang_name_c);
3679
3680   /* Generate initialization and destruction functions for all
3681      priorities for which they are required.  */
3682   if (priority_info_map)
3683     splay_tree_foreach (priority_info_map, 
3684                         generate_ctor_and_dtor_functions_for_priority,
3685                         /*data=*/0);
3686
3687   /* We're done with the splay-tree now.  */
3688   if (priority_info_map)
3689     splay_tree_delete (priority_info_map);
3690
3691   /* We're done with static constructors, so we can go back to "C++"
3692      linkage now.  */
3693   pop_lang_context ();
3694
3695   /* Now delete from the chain of variables all virtual function tables.
3696      We output them all ourselves, because each will be treated
3697      specially.  We don't do this if we're just doing semantic
3698      analysis, and not code-generation.  */
3699   if (!flag_syntax_only)
3700     walk_globals (vtable_decl_p, prune_vtable_vardecl, /*data=*/0);
3701
3702   /* Now, issue warnings about static, but not defined, functions,
3703      etc., and emit debugging information.  */
3704   walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3705   if (pending_statics)
3706     check_global_declarations (&VARRAY_TREE (pending_statics, 0),
3707                                pending_statics_used);
3708
3709   finish_repo ();
3710
3711   /* The entire file is now complete.  If requested, dump everything
3712      to a file.   */
3713   if (flag_dump_translation_unit)
3714     dump_node_to_file (global_namespace, flag_dump_translation_unit);
3715
3716   /* If there's some tool that wants to examine the entire translation
3717      unit, let it do so now.  */
3718   if (back_end_hook)
3719     (*back_end_hook) (global_namespace);
3720
3721   timevar_pop (TV_VARCONST);
3722
3723   if (flag_detailed_statistics)
3724     {
3725       dump_tree_statistics ();
3726       dump_time_statistics ();
3727     }
3728 }
3729
3730 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3731    expr.  Since it was parsed like a type, we need to wade through and fix
3732    that.  Unfortunately, since operator() is left-associative, we can't use
3733    tail recursion.  In the above example, TYPE is `A', and DECL is
3734    `()()()()()'.
3735
3736    Maybe this shouldn't be recursive, but how often will it actually be
3737    used?  (jason) */
3738
3739 tree
3740 reparse_absdcl_as_expr (type, decl)
3741      tree type, decl;
3742 {
3743   /* do build_functional_cast (type, NULL_TREE) at bottom */
3744   if (TREE_OPERAND (decl, 0) == NULL_TREE)
3745     return build_functional_cast (type, NULL_TREE);
3746
3747   /* recurse */
3748   decl = reparse_absdcl_as_expr (type, TREE_OPERAND (decl, 0));
3749
3750   decl = build_x_function_call (decl, NULL_TREE, current_class_ref);
3751
3752   if (TREE_CODE (decl) == CALL_EXPR
3753       && (! TREE_TYPE (decl)
3754           || TREE_CODE (TREE_TYPE (decl)) != VOID_TYPE))
3755     decl = require_complete_type (decl);
3756
3757   return decl;
3758 }
3759
3760 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3761    out to be an expr.  Since it was parsed like a type, we need to wade
3762    through and fix that.  Since casts are right-associative, we are
3763    reversing the order, so we don't have to recurse.
3764
3765    In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3766    `1'.  */
3767
3768 tree
3769 reparse_absdcl_as_casts (decl, expr)
3770      tree decl, expr;
3771 {
3772   tree type;
3773   
3774   if (TREE_CODE (expr) == CONSTRUCTOR
3775       && TREE_TYPE (expr) == 0)
3776     {
3777       type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3778       decl = TREE_OPERAND (decl, 0);
3779
3780       expr = digest_init (type, expr, (tree *) 0);
3781       if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3782         {
3783           int failure = complete_array_type (type, expr, 1);
3784           if (failure)
3785             my_friendly_abort (78);
3786         }
3787     }
3788
3789   while (decl)
3790     {
3791       type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3792       decl = TREE_OPERAND (decl, 0);
3793       expr = build_c_cast (type, expr);
3794     }
3795
3796   if (warn_old_style_cast && ! in_system_header
3797       && current_lang_name != lang_name_c)
3798     warning ("use of old-style cast");
3799
3800   return expr;
3801 }
3802
3803 /* Given plain tree nodes for an expression, build up the full semantics.  */
3804
3805 tree
3806 build_expr_from_tree (t)
3807      tree t;
3808 {
3809   if (t == NULL_TREE || t == error_mark_node)
3810     return t;
3811
3812   switch (TREE_CODE (t))
3813     {
3814     case IDENTIFIER_NODE:
3815       return do_identifier (t, 0, NULL_TREE);
3816
3817     case LOOKUP_EXPR:
3818       if (LOOKUP_EXPR_GLOBAL (t))
3819         return do_scoped_id (TREE_OPERAND (t, 0), 0);
3820       else
3821         return do_identifier (TREE_OPERAND (t, 0), 0, NULL_TREE);
3822
3823     case TEMPLATE_ID_EXPR:
3824       return (lookup_template_function
3825               (build_expr_from_tree (TREE_OPERAND (t, 0)),
3826                build_expr_from_tree (TREE_OPERAND (t, 1))));
3827
3828     case INDIRECT_REF:
3829       return build_x_indirect_ref
3830         (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3831
3832     case CAST_EXPR:
3833       return build_functional_cast
3834         (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3835
3836     case REINTERPRET_CAST_EXPR:
3837       return build_reinterpret_cast
3838         (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3839
3840     case CONST_CAST_EXPR:
3841       return build_const_cast
3842         (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3843
3844     case DYNAMIC_CAST_EXPR:
3845       return build_dynamic_cast
3846         (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3847
3848     case STATIC_CAST_EXPR:
3849       return build_static_cast
3850         (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3851
3852     case PREDECREMENT_EXPR:
3853     case PREINCREMENT_EXPR:
3854     case POSTDECREMENT_EXPR:
3855     case POSTINCREMENT_EXPR:
3856     case NEGATE_EXPR:
3857     case BIT_NOT_EXPR:
3858     case ABS_EXPR:
3859     case TRUTH_NOT_EXPR:
3860     case ADDR_EXPR:
3861     case CONVERT_EXPR:      /* Unary + */
3862     case REALPART_EXPR:
3863     case IMAGPART_EXPR:
3864       if (TREE_TYPE (t))
3865         return t;
3866       return build_x_unary_op (TREE_CODE (t),
3867                                build_expr_from_tree (TREE_OPERAND (t, 0)));
3868
3869     case PLUS_EXPR:
3870     case MINUS_EXPR:
3871     case MULT_EXPR:
3872     case TRUNC_DIV_EXPR:
3873     case CEIL_DIV_EXPR:
3874     case FLOOR_DIV_EXPR:
3875     case ROUND_DIV_EXPR:
3876     case EXACT_DIV_EXPR:
3877     case BIT_AND_EXPR:
3878     case BIT_ANDTC_EXPR:
3879     case BIT_IOR_EXPR:
3880     case BIT_XOR_EXPR:
3881     case TRUNC_MOD_EXPR:
3882     case FLOOR_MOD_EXPR:
3883     case TRUTH_ANDIF_EXPR:
3884     case TRUTH_ORIF_EXPR:
3885     case TRUTH_AND_EXPR:
3886     case TRUTH_OR_EXPR:
3887     case RSHIFT_EXPR:
3888     case LSHIFT_EXPR:
3889     case RROTATE_EXPR:
3890     case LROTATE_EXPR:
3891     case EQ_EXPR:
3892     case NE_EXPR:
3893     case MAX_EXPR:
3894     case MIN_EXPR:
3895     case LE_EXPR:
3896     case GE_EXPR:
3897     case LT_EXPR:
3898     case GT_EXPR:
3899     case MEMBER_REF:
3900       return build_x_binary_op
3901         (TREE_CODE (t), 
3902          build_expr_from_tree (TREE_OPERAND (t, 0)),
3903          build_expr_from_tree (TREE_OPERAND (t, 1)));
3904
3905     case DOTSTAR_EXPR:
3906       return build_m_component_ref
3907         (build_expr_from_tree (TREE_OPERAND (t, 0)),
3908          build_expr_from_tree (TREE_OPERAND (t, 1)));
3909
3910     case SCOPE_REF:
3911       return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3912
3913     case ARRAY_REF:
3914       if (TREE_OPERAND (t, 0) == NULL_TREE)
3915         /* new-type-id */
3916         return build_nt (ARRAY_REF, NULL_TREE,
3917                          build_expr_from_tree (TREE_OPERAND (t, 1)));
3918       return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3919                               build_expr_from_tree (TREE_OPERAND (t, 1)));
3920
3921     case SIZEOF_EXPR:
3922     case ALIGNOF_EXPR:
3923       {
3924         tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3925         if (!TYPE_P (r))
3926           r = TREE_TYPE (r);
3927         return TREE_CODE (t) == SIZEOF_EXPR ? c_sizeof (r) : c_alignof (r);
3928       }
3929
3930     case MODOP_EXPR:
3931       return build_x_modify_expr
3932         (build_expr_from_tree (TREE_OPERAND (t, 0)),
3933          TREE_CODE (TREE_OPERAND (t, 1)),
3934          build_expr_from_tree (TREE_OPERAND (t, 2)));
3935
3936     case ARROW_EXPR:
3937       return build_x_arrow
3938         (build_expr_from_tree (TREE_OPERAND (t, 0)));
3939
3940     case NEW_EXPR:
3941       return build_new
3942         (build_expr_from_tree (TREE_OPERAND (t, 0)),
3943          build_expr_from_tree (TREE_OPERAND (t, 1)),
3944          build_expr_from_tree (TREE_OPERAND (t, 2)),
3945          NEW_EXPR_USE_GLOBAL (t));
3946
3947     case DELETE_EXPR:
3948       return delete_sanity
3949         (build_expr_from_tree (TREE_OPERAND (t, 0)),
3950          build_expr_from_tree (TREE_OPERAND (t, 1)),
3951          DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3952
3953     case COMPOUND_EXPR:
3954       if (TREE_OPERAND (t, 1) == NULL_TREE)
3955         return build_x_compound_expr
3956           (build_expr_from_tree (TREE_OPERAND (t, 0)));
3957       else
3958         my_friendly_abort (42);
3959
3960     case METHOD_CALL_EXPR:
3961       if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3962         {
3963           tree ref = TREE_OPERAND (t, 0);
3964           tree name = TREE_OPERAND (ref, 1);
3965           
3966           if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3967             name = build_nt (TEMPLATE_ID_EXPR,
3968                              TREE_OPERAND (name, 0),
3969                              build_expr_from_tree (TREE_OPERAND (name, 1)));
3970             
3971           return build_scoped_method_call
3972             (build_expr_from_tree (TREE_OPERAND (t, 1)),
3973              build_expr_from_tree (TREE_OPERAND (ref, 0)),
3974              name,
3975              build_expr_from_tree (TREE_OPERAND (t, 2)));
3976         }
3977       else 
3978         {
3979           tree fn = TREE_OPERAND (t, 0);
3980
3981           /* We can get a TEMPLATE_ID_EXPR here on code like:
3982
3983                x->f<2>();
3984               
3985              so we must resolve that.  However, we can also get things
3986              like a BIT_NOT_EXPR here, when referring to a destructor,
3987              and things like that are not correctly resolved by
3988              build_expr_from_tree.  So, just use build_expr_from_tree
3989              when we really need it.  */
3990           if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3991             fn = lookup_template_function
3992               (TREE_OPERAND (fn, 0),
3993                build_expr_from_tree (TREE_OPERAND (fn, 1)));
3994
3995           return build_method_call
3996             (build_expr_from_tree (TREE_OPERAND (t, 1)),
3997              fn,
3998              build_expr_from_tree (TREE_OPERAND (t, 2)),
3999              NULL_TREE, LOOKUP_NORMAL);
4000         }
4001
4002     case CALL_EXPR:
4003       if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
4004         {
4005           tree ref = TREE_OPERAND (t, 0);
4006           tree name = TREE_OPERAND (ref, 1);
4007           
4008           if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4009             name = build_nt (TEMPLATE_ID_EXPR,
4010                              TREE_OPERAND (name, 0),
4011                              build_expr_from_tree (TREE_OPERAND (name, 1)));
4012             
4013           return build_member_call
4014             (build_expr_from_tree (TREE_OPERAND (ref, 0)),
4015              name,
4016              build_expr_from_tree (TREE_OPERAND (t, 1)));
4017         }
4018       else
4019         {
4020           tree name = TREE_OPERAND (t, 0);
4021           tree id;
4022           tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
4023           if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
4024               && !LOOKUP_EXPR_GLOBAL (name)
4025               && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
4026               && (!current_class_type
4027                   || !lookup_member (current_class_type, id, 0, 0)))
4028             {
4029               /* Do Koenig lookup if there are no class members. */
4030               name = do_identifier (id, 0, args);
4031             }
4032           else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
4033               || ! really_overloaded_fn (name))
4034             name = build_expr_from_tree (name);
4035           return build_x_function_call (name, args, current_class_ref);
4036         }
4037
4038     case COND_EXPR:
4039       return build_x_conditional_expr
4040         (build_expr_from_tree (TREE_OPERAND (t, 0)),
4041          build_expr_from_tree (TREE_OPERAND (t, 1)),
4042          build_expr_from_tree (TREE_OPERAND (t, 2)));
4043
4044     case PSEUDO_DTOR_EXPR:
4045       return (finish_pseudo_destructor_call_expr 
4046               (build_expr_from_tree (TREE_OPERAND (t, 0)),
4047                build_expr_from_tree (TREE_OPERAND (t, 1)),
4048                build_expr_from_tree (TREE_OPERAND (t, 2))));
4049
4050     case TREE_LIST:
4051       {
4052         tree purpose, value, chain;
4053
4054         if (t == void_list_node)
4055           return t;
4056
4057         purpose = TREE_PURPOSE (t);
4058         if (purpose)
4059           purpose = build_expr_from_tree (purpose);
4060         value = TREE_VALUE (t);
4061         if (value)
4062           value = build_expr_from_tree (value);
4063         chain = TREE_CHAIN (t);
4064         if (chain && chain != void_type_node)
4065           chain = build_expr_from_tree (chain);
4066         return tree_cons (purpose, value, chain);
4067       }
4068
4069     case COMPONENT_REF:
4070       {
4071         tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
4072         tree field = TREE_OPERAND (t, 1);
4073         
4074         /* We use a COMPONENT_REF to indicate things of the form `x.b'
4075            and `x.A::b'.  We must distinguish between those cases
4076            here.  */
4077         if (TREE_CODE (field) == SCOPE_REF)
4078           return build_object_ref (object, 
4079                                    TREE_OPERAND (field, 0),
4080                                    TREE_OPERAND (field, 1));
4081         else
4082           return build_x_component_ref (object, field,
4083                                         NULL_TREE, 1);
4084       }
4085
4086     case THROW_EXPR:
4087       return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
4088
4089     case CONSTRUCTOR:
4090       {
4091         tree r;
4092
4093         /* digest_init will do the wrong thing if we let it.  */
4094         if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
4095           return t;
4096
4097         r = build_nt (CONSTRUCTOR, NULL_TREE,
4098                       build_expr_from_tree (CONSTRUCTOR_ELTS (t)));
4099         TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
4100
4101         if (TREE_TYPE (t))
4102           return digest_init (TREE_TYPE (t), r, 0);
4103         return r;
4104       }
4105
4106     case TYPEID_EXPR:
4107       if (TYPE_P (TREE_OPERAND (t, 0)))
4108         return get_typeid (TREE_OPERAND (t, 0));
4109       return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
4110
4111     case VAR_DECL:
4112       return convert_from_reference (t);
4113
4114     case VA_ARG_EXPR:
4115       return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
4116                            TREE_TYPE (t));
4117
4118     default:
4119       return t;
4120     }
4121 }
4122
4123 /* This is something of the form `int (*a)++' that has turned out to be an
4124    expr.  It was only converted into parse nodes, so we need to go through
4125    and build up the semantics.  Most of the work is done by
4126    build_expr_from_tree, above.
4127
4128    In the above example, TYPE is `int' and DECL is `*a'.  */
4129
4130 tree
4131 reparse_decl_as_expr (type, decl)
4132      tree type, decl;
4133 {
4134   decl = build_expr_from_tree (decl);
4135   if (type)
4136     return build_functional_cast (type, build_tree_list (NULL_TREE, decl));
4137   else
4138     return decl;
4139 }
4140
4141 /* This is something of the form `int (*a)' that has turned out to be a
4142    decl.  It was only converted into parse nodes, so we need to do the
4143    checking that make_{pointer,reference}_declarator do.  */
4144
4145 tree
4146 finish_decl_parsing (decl)
4147      tree decl;
4148 {
4149   switch (TREE_CODE (decl))
4150     {
4151     case IDENTIFIER_NODE:
4152       return decl;
4153     case INDIRECT_REF:
4154       return make_pointer_declarator
4155         (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4156     case ADDR_EXPR:
4157       return make_reference_declarator
4158         (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4159     case BIT_NOT_EXPR:
4160       TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4161       return decl;
4162     case SCOPE_REF:
4163       push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
4164       TREE_COMPLEXITY (decl) = current_class_depth;
4165       return decl;
4166     case ARRAY_REF:
4167       TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4168       return decl;
4169     case TREE_LIST:
4170       /* For attribute handling.  */
4171       TREE_VALUE (decl) = finish_decl_parsing (TREE_VALUE (decl));
4172       return decl;
4173     case TEMPLATE_ID_EXPR:
4174       return decl;
4175     default:
4176       my_friendly_abort (5);
4177       return NULL_TREE;
4178     }
4179 }
4180
4181 /* Return 1 if root encloses child. */
4182
4183 static int
4184 is_namespace_ancestor (root, child)
4185      tree root, child;
4186 {
4187   if (root == child)
4188     return 1;
4189   if (root == global_namespace)
4190     return 1;
4191   if (child == global_namespace)
4192     return 0;
4193   return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
4194 }
4195   
4196
4197 /* Return the namespace that is the common ancestor 
4198    of two given namespaces. */
4199
4200 tree
4201 namespace_ancestor (ns1, ns2)
4202      tree ns1, ns2;
4203 {
4204   if (is_namespace_ancestor (ns1, ns2))
4205     return ns1;
4206   return namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2);
4207 }
4208
4209 /* Insert used into the using list of user. Set indirect_flag if this
4210    directive is not directly from the source. Also find the common
4211    ancestor and let our users know about the new namespace */
4212 static void 
4213 add_using_namespace (user, used, indirect)
4214      tree user;
4215      tree used;
4216      int indirect;
4217 {
4218   tree t;
4219   /* Using oneself is a no-op. */
4220   if (user == used)
4221     return;
4222   my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
4223   my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
4224   /* Check if we already have this. */
4225   t = purpose_member (used, DECL_NAMESPACE_USING (user));
4226   if (t != NULL_TREE)
4227     {
4228       if (!indirect)
4229         /* Promote to direct usage. */
4230         TREE_INDIRECT_USING (t) = 0;
4231       return;
4232     }
4233
4234   /* Add used to the user's using list. */
4235   DECL_NAMESPACE_USING (user) 
4236     = tree_cons (used, namespace_ancestor (user, used), 
4237                  DECL_NAMESPACE_USING (user));
4238
4239   TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4240
4241   /* Add user to the used's users list. */
4242   DECL_NAMESPACE_USERS (used)
4243     = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4244
4245   /* Recursively add all namespaces used. */
4246   for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4247     /* indirect usage */
4248     add_using_namespace (user, TREE_PURPOSE (t), 1);
4249
4250   /* Tell everyone using us about the new used namespaces. */
4251   for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4252     add_using_namespace (TREE_PURPOSE (t), used, 1);
4253 }
4254
4255 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4256    duplicates.  The first list becomes the tail of the result.
4257
4258    The algorithm is O(n^2).  We could get this down to O(n log n) by
4259    doing a sort on the addresses of the functions, if that becomes
4260    necessary.  */
4261
4262 static tree
4263 merge_functions (s1, s2)
4264      tree s1;
4265      tree s2;
4266 {
4267   for (; s2; s2 = OVL_NEXT (s2))
4268     {
4269       tree fn = OVL_CURRENT (s2);
4270       if (! ovl_member (fn, s1))
4271         s1 = build_overload (fn, s1);
4272     }
4273   return s1;
4274 }
4275
4276 /* This should return an error not all definitions define functions.
4277    It is not an error if we find two functions with exactly the
4278    same signature, only if these are selected in overload resolution.
4279    old is the current set of bindings, new the freshly-found binding.
4280    XXX Do we want to give *all* candidates in case of ambiguity?
4281    XXX In what way should I treat extern declarations?
4282    XXX I don't want to repeat the entire duplicate_decls here */
4283
4284 static tree
4285 ambiguous_decl (name, old, new, flags)
4286      tree name;
4287      tree old;
4288      tree new;
4289      int flags;
4290 {
4291   tree val, type;
4292   my_friendly_assert (old != NULL_TREE, 393);
4293   /* Copy the value. */
4294   val = BINDING_VALUE (new);
4295   if (val)
4296     switch (TREE_CODE (val))
4297       {
4298       case TEMPLATE_DECL:
4299         /* If we expect types or namespaces, and not templates,
4300            or this is not a template class. */
4301         if (LOOKUP_QUALIFIERS_ONLY (flags)
4302             && !DECL_CLASS_TEMPLATE_P (val))
4303           val = NULL_TREE;
4304         break;
4305       case TYPE_DECL:
4306         if (LOOKUP_NAMESPACES_ONLY (flags))
4307           val = NULL_TREE;
4308         break;
4309       case NAMESPACE_DECL:
4310         if (LOOKUP_TYPES_ONLY (flags))
4311           val = NULL_TREE;
4312         break;
4313       default:
4314         if (LOOKUP_QUALIFIERS_ONLY (flags))
4315           val = NULL_TREE;
4316       }
4317         
4318   if (!BINDING_VALUE (old))
4319     BINDING_VALUE (old) = val;
4320   else if (val && val != BINDING_VALUE (old))
4321     {
4322       if (is_overloaded_fn (BINDING_VALUE (old)) 
4323           && is_overloaded_fn (val))
4324         {
4325           BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
4326                                                  val);
4327         }
4328       else
4329         {
4330           /* Some declarations are functions, some are not. */
4331           if (flags & LOOKUP_COMPLAIN)
4332             {
4333               /* If we've already given this error for this lookup,
4334                  BINDING_VALUE (old) is error_mark_node, so let's not
4335                  repeat ourselves.  */
4336               if (BINDING_VALUE (old) != error_mark_node)
4337                 {
4338                   cp_error ("use of `%D' is ambiguous", name);
4339                   cp_error_at ("  first declared as `%#D' here",
4340                                BINDING_VALUE (old));
4341                 }
4342               cp_error_at ("  also declared as `%#D' here", val);
4343             }
4344           BINDING_VALUE (old) = error_mark_node;
4345         }
4346     }
4347   /* ... and copy the type. */
4348   type = BINDING_TYPE (new);
4349   if (LOOKUP_NAMESPACES_ONLY (flags))
4350     type = NULL_TREE;
4351   if (!BINDING_TYPE (old))
4352     BINDING_TYPE (old) = type;
4353   else if (type && BINDING_TYPE (old) != type)
4354     {
4355       if (flags & LOOKUP_COMPLAIN)
4356         {
4357           cp_error ("`%D' denotes an ambiguous type",name);
4358           cp_error_at ("  first type here", BINDING_TYPE (old));
4359           cp_error_at ("  other type here", type);
4360         }
4361     }
4362   return old;
4363 }
4364
4365 /* Subroutine of unualified_namespace_lookup:
4366    Add the bindings of NAME in used namespaces to VAL.
4367    We are currently looking for names in namespace SCOPE, so we
4368    look through USINGS for using-directives of namespaces
4369    which have SCOPE as a common ancestor with the current scope.
4370    Returns zero on errors. */
4371
4372 int
4373 lookup_using_namespace (name, val, usings, scope, flags, spacesp)
4374      tree name, val, usings, scope;
4375      int flags;
4376      tree *spacesp;
4377 {
4378   tree iter;
4379   tree val1;
4380   /* Iterate over all used namespaces in current, searching for using
4381      directives of scope. */
4382   for (iter = usings; iter; iter = TREE_CHAIN (iter))
4383     if (TREE_VALUE (iter) == scope)
4384       {
4385         if (spacesp)
4386           *spacesp = tree_cons (TREE_PURPOSE (iter), NULL_TREE,
4387                                 *spacesp);
4388         val1 = binding_for_name (name, TREE_PURPOSE (iter));
4389         /* Resolve ambiguities. */
4390         val = ambiguous_decl (name, val, val1, flags);
4391       }
4392   return BINDING_VALUE (val) != error_mark_node;
4393 }
4394
4395 /* [namespace.qual]
4396    Accepts the NAME to lookup and its qualifying SCOPE.
4397    Returns the name/type pair found into the CPLUS_BINDING RESULT,
4398    or 0 on error. */
4399
4400 int
4401 qualified_lookup_using_namespace (name, scope, result, flags)
4402      tree name;
4403      tree scope;
4404      tree result;
4405      int flags;
4406 {
4407   /* Maintain a list of namespaces visited... */
4408   tree seen = NULL_TREE;
4409   /* ... and a list of namespace yet to see. */
4410   tree todo = NULL_TREE;
4411   tree usings;
4412   /* Look through namespace aliases.  */
4413   scope = ORIGINAL_NAMESPACE (scope);
4414   while (scope && (result != error_mark_node))
4415     {
4416       seen = tree_cons (scope, NULL_TREE, seen);
4417       result = ambiguous_decl (name, result,
4418                                binding_for_name (name, scope), flags);
4419       if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
4420         /* Consider using directives. */
4421         for (usings = DECL_NAMESPACE_USING (scope); usings;
4422              usings = TREE_CHAIN (usings))
4423           /* If this was a real directive, and we have not seen it. */
4424           if (!TREE_INDIRECT_USING (usings)
4425               && !purpose_member (TREE_PURPOSE (usings), seen))
4426             todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
4427       if (todo)
4428         {
4429           scope = TREE_PURPOSE (todo);
4430           todo = TREE_CHAIN (todo);
4431         }
4432       else
4433         scope = NULL_TREE; /* If there never was a todo list. */
4434     }
4435   return result != error_mark_node;
4436 }
4437
4438 /* [namespace.memdef]/2 */
4439
4440 /* Set the context of a declaration to scope. Complain if we are not
4441    outside scope. */
4442
4443 void
4444 set_decl_namespace (decl, scope, friendp)
4445      tree decl;
4446      tree scope;
4447      int friendp;
4448 {
4449   tree old;
4450   if (scope == fake_std_node)
4451     scope = global_namespace;
4452   /* Get rid of namespace aliases. */
4453   scope = ORIGINAL_NAMESPACE (scope);
4454   
4455   /* It is ok for friends to be qualified in parallel space.  */
4456   if (!friendp && !is_namespace_ancestor (current_namespace, scope))
4457     cp_error ("declaration of `%D' not in a namespace surrounding `%D'",
4458               decl, scope);
4459   DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4460   if (scope != current_namespace)
4461     {
4462       /* See whether this has been declared in the namespace. */
4463       old = namespace_binding (DECL_NAME (decl), scope);
4464       if (!old)
4465         /* No old declaration at all. */
4466         goto complain;
4467       /* A template can be explicitly specialized in any namespace.  */
4468       if (processing_explicit_instantiation)
4469         return;
4470       if (!is_overloaded_fn (decl))
4471         /* Don't compare non-function decls with decls_match here,
4472            since it can't check for the correct constness at this
4473            point. pushdecl will find those errors later.  */
4474         return;
4475       /* Since decl is a function, old should contain a function decl. */
4476       if (!is_overloaded_fn (old))
4477         goto complain;
4478       if (processing_template_decl || processing_specialization)
4479         /* We have not yet called push_template_decl to turn the
4480            FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
4481            won't match.  But, we'll check later, when we construct the
4482            template.  */
4483         return;
4484       for (; old; old = OVL_NEXT (old))
4485         if (decls_match (decl, OVL_CURRENT (old)))
4486           return;
4487     }
4488   else
4489     return;
4490  complain:
4491   cp_error ("`%D' should have been declared inside `%D'",
4492             decl, scope);
4493
4494
4495 /* Compute the namespace where a declaration is defined. */
4496
4497 static tree
4498 decl_namespace (decl)
4499      tree decl;
4500 {
4501   if (TYPE_P (decl))
4502     decl = TYPE_STUB_DECL (decl);
4503   while (DECL_CONTEXT (decl))
4504     {
4505       decl = DECL_CONTEXT (decl);
4506       if (TREE_CODE (decl) == NAMESPACE_DECL)
4507         return decl;
4508       if (TYPE_P (decl))
4509         decl = TYPE_STUB_DECL (decl);
4510       my_friendly_assert (DECL_P (decl), 390);
4511     }
4512
4513   return global_namespace;
4514 }
4515
4516 /* Return the namespace where the current declaration is declared. */
4517
4518 tree
4519 current_decl_namespace ()
4520 {
4521   tree result;
4522   /* If we have been pushed into a different namespace, use it. */
4523   if (decl_namespace_list)
4524     return TREE_PURPOSE (decl_namespace_list);
4525
4526   if (current_class_type)
4527     result = decl_namespace (TYPE_STUB_DECL (current_class_type));
4528   else if (current_function_decl)
4529     result = decl_namespace (current_function_decl);
4530   else 
4531     result = current_namespace;
4532   return result;
4533 }
4534
4535 /* Temporarily set the namespace for the current declaration. */
4536
4537 void
4538 push_decl_namespace (decl)
4539      tree decl;
4540 {
4541   if (TREE_CODE (decl) != NAMESPACE_DECL)
4542     decl = decl_namespace (decl);
4543   decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
4544                                    NULL_TREE, decl_namespace_list);
4545 }
4546
4547 void
4548 pop_decl_namespace ()
4549 {
4550   decl_namespace_list = TREE_CHAIN (decl_namespace_list);
4551 }
4552
4553 /* Enter a class or namespace scope. */
4554
4555 void
4556 push_scope (t)
4557      tree t;
4558 {
4559   if (TREE_CODE (t) == NAMESPACE_DECL)
4560     push_decl_namespace (t);
4561   else
4562     pushclass (t, 2);
4563 }
4564
4565 /* Leave scope pushed by push_scope. */
4566
4567 void
4568 pop_scope (t)
4569      tree t;
4570 {
4571   if (TREE_CODE (t) == NAMESPACE_DECL)
4572     pop_decl_namespace ();
4573   else
4574     popclass ();
4575 }
4576
4577 /* [basic.lookup.koenig] */
4578 /* A non-zero return value in the functions below indicates an error.
4579    All nodes allocated in the procedure are on the scratch obstack. */
4580
4581 struct arg_lookup
4582 {
4583   tree name;
4584   tree namespaces;
4585   tree classes;
4586   tree functions;
4587 };
4588
4589 static int arg_assoc         PARAMS ((struct arg_lookup*, tree));
4590 static int arg_assoc_args    PARAMS ((struct arg_lookup*, tree));
4591 static int arg_assoc_type    PARAMS ((struct arg_lookup*, tree));
4592 static int add_function      PARAMS ((struct arg_lookup *, tree));
4593 static int arg_assoc_namespace PARAMS ((struct arg_lookup *, tree));
4594 static int arg_assoc_class   PARAMS ((struct arg_lookup *, tree));
4595 static int arg_assoc_template_arg PARAMS ((struct arg_lookup*, tree));
4596
4597 /* Add a function to the lookup structure.
4598    Returns 1 on error.  */
4599
4600 static int
4601 add_function (k, fn)
4602      struct arg_lookup *k;
4603      tree fn;
4604 {
4605   /* We used to check here to see if the function was already in the list,
4606      but that's O(n^2), which is just too expensive for function lookup.
4607      Now we deal with the occasional duplicate in joust.  In doing this, we
4608      assume that the number of duplicates will be small compared to the
4609      total number of functions being compared, which should usually be the
4610      case.  */
4611
4612   /* We must find only functions, or exactly one non-function. */
4613   if (!k->functions) 
4614     k->functions = fn;
4615   else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4616     k->functions = build_overload (fn, k->functions);
4617   else
4618     {
4619       tree f1 = OVL_CURRENT (k->functions);
4620       tree f2 = fn;
4621       if (is_overloaded_fn (f1))
4622         {
4623           fn = f1; f1 = f2; f2 = fn;
4624         }
4625       cp_error_at ("`%D' is not a function,", f1);
4626       cp_error_at ("  conflict with `%D'", f2);
4627       cp_error ("  in call to `%D'", k->name);
4628       return 1;
4629     }
4630
4631   return 0;
4632 }
4633
4634 /* Add functions of a namespace to the lookup structure.
4635    Returns 1 on error.  */
4636
4637 static int
4638 arg_assoc_namespace (k, scope)
4639      struct arg_lookup *k;
4640      tree scope;
4641 {
4642   tree value;
4643
4644   if (purpose_member (scope, k->namespaces))
4645     return 0;
4646   k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4647   
4648   value = namespace_binding (k->name, scope);
4649   if (!value)
4650     return 0;
4651
4652   for (; value; value = OVL_NEXT (value))
4653     if (add_function (k, OVL_CURRENT (value)))
4654       return 1;
4655   
4656   return 0;
4657 }
4658
4659 /* Adds everything associated with a template argument to the lookup
4660    structure.  Returns 1 on error.  */
4661
4662 static int
4663 arg_assoc_template_arg (k, arg)
4664      struct arg_lookup* k;
4665      tree arg;
4666 {
4667   /* [basic.lookup.koenig]
4668
4669      If T is a template-id, its associated namespaces and classes are
4670      ... the namespaces and classes associated with the types of the
4671      template arguments provided for template type parameters
4672      (excluding template template parameters); the namespaces in which
4673      any template template arguments are defined; and the classes in
4674      which any member templates used as template template arguments
4675      are defined.  [Note: non-type template arguments do not
4676      contribute to the set of associated namespaces.  ]  */
4677
4678   /* Consider first template template arguments.  */
4679   if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
4680     return 0;
4681   else if (TREE_CODE (arg) == TEMPLATE_DECL)
4682     {
4683       tree ctx = CP_DECL_CONTEXT (arg);
4684
4685       /* It's not a member template.  */
4686       if (TREE_CODE (ctx) == NAMESPACE_DECL)
4687         return arg_assoc_namespace (k, ctx);
4688       /* Otherwise, it must be member template.  */
4689       else 
4690         return arg_assoc_class (k, ctx);
4691     }
4692   /* It's not a template template argument, but it is a type template
4693      argument.  */
4694   else if (TYPE_P (arg))
4695     return arg_assoc_type (k, arg);
4696   /* It's a non-type template argument.  */
4697   else
4698     return 0;
4699 }
4700
4701 /* Adds everything associated with class to the lookup structure.
4702    Returns 1 on error.  */
4703
4704 static int
4705 arg_assoc_class (k, type)
4706      struct arg_lookup* k;
4707      tree type;
4708 {
4709   tree list, friends, context;
4710   int i;
4711   
4712   /* Backend build structures, such as __builtin_va_list, aren't
4713      affected by all this.  */
4714   if (!CLASS_TYPE_P (type))
4715     return 0;
4716
4717   if (purpose_member (type, k->classes))
4718     return 0;
4719   k->classes = tree_cons (type, NULL_TREE, k->classes);
4720   
4721   context = decl_namespace (TYPE_MAIN_DECL (type));
4722   if (arg_assoc_namespace (k, context))
4723     return 1;
4724   
4725   /* Process baseclasses. */
4726   for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4727     if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4728       return 1;
4729   
4730   /* Process friends. */
4731   for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list; 
4732        list = TREE_CHAIN (list))
4733     if (k->name == TREE_PURPOSE (list))
4734       for (friends = TREE_VALUE (list); friends; 
4735            friends = TREE_CHAIN (friends))
4736         /* Only interested in global functions with potentially hidden
4737            (i.e. unqualified) declarations. */
4738         if (TREE_PURPOSE (friends) == error_mark_node && TREE_VALUE (friends)
4739             && decl_namespace (TREE_VALUE (friends)) == context)
4740           if (add_function (k, TREE_VALUE (friends)))
4741             return 1;
4742
4743   /* Process template arguments.  */
4744   if (CLASSTYPE_TEMPLATE_INFO (type))
4745     {
4746       list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4747       for (i = 0; i < TREE_VEC_LENGTH (list); ++i) 
4748         arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4749     }
4750
4751   return 0;
4752 }
4753
4754 /* Adds everything associated with a given type.
4755    Returns 1 on error.  */
4756
4757 static int
4758 arg_assoc_type (k, type)
4759      struct arg_lookup *k;
4760      tree type;
4761 {
4762   switch (TREE_CODE (type))
4763     {
4764     case VOID_TYPE:
4765     case INTEGER_TYPE:
4766     case REAL_TYPE:
4767     case COMPLEX_TYPE:
4768     case VECTOR_TYPE:
4769     case CHAR_TYPE:
4770     case BOOLEAN_TYPE:
4771       return 0;
4772     case RECORD_TYPE:
4773       if (TYPE_PTRMEMFUNC_P (type))
4774         return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4775       return arg_assoc_class (k, type);
4776     case POINTER_TYPE:
4777     case REFERENCE_TYPE:
4778     case ARRAY_TYPE:
4779       return arg_assoc_type (k, TREE_TYPE (type));
4780     case UNION_TYPE:
4781     case ENUMERAL_TYPE:
4782       return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4783     case OFFSET_TYPE:
4784       /* Pointer to member: associate class type and value type. */
4785       if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4786         return 1;
4787       return arg_assoc_type (k, TREE_TYPE (type));
4788     case METHOD_TYPE:
4789       /* The basetype is referenced in the first arg type, so just
4790          fall through.  */
4791     case FUNCTION_TYPE:
4792       /* Associate the parameter types. */
4793       if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4794         return 1;
4795       /* Associate the return type. */
4796       return arg_assoc_type (k, TREE_TYPE (type));
4797     case TEMPLATE_TYPE_PARM:
4798     case BOUND_TEMPLATE_TEMPLATE_PARM:
4799       return 0;
4800     case TYPENAME_TYPE:
4801       return 0;
4802     case LANG_TYPE:
4803       if (type == unknown_type_node)
4804         return 0;
4805       /* else fall through */
4806     default:
4807       my_friendly_abort (390);
4808     }
4809   return 0;
4810 }
4811
4812 /* Adds everything associated with arguments.  Returns 1 on error.  */
4813
4814 static int
4815 arg_assoc_args (k, args)
4816      struct arg_lookup* k;
4817      tree args;
4818 {
4819   for (; args; args = TREE_CHAIN (args))
4820     if (arg_assoc (k, TREE_VALUE (args)))
4821       return 1;
4822   return 0;
4823 }
4824
4825 /* Adds everything associated with a given tree_node.  Returns 1 on error.  */
4826
4827 static int
4828 arg_assoc (k, n)
4829      struct arg_lookup* k;
4830      tree n;
4831 {
4832   if (n == error_mark_node)
4833     return 0;
4834
4835   if (TYPE_P (n))
4836     return arg_assoc_type (k, n);
4837
4838   if (! type_unknown_p (n))
4839     return arg_assoc_type (k, TREE_TYPE (n));
4840
4841   if (TREE_CODE (n) == ADDR_EXPR)
4842     n = TREE_OPERAND (n, 0);
4843   if (TREE_CODE (n) == COMPONENT_REF)
4844     n = TREE_OPERAND (n, 1);
4845   if (TREE_CODE (n) == OFFSET_REF)
4846     n = TREE_OPERAND (n, 1);
4847   while (TREE_CODE (n) == TREE_LIST)
4848     n = TREE_VALUE (n);
4849
4850   if (TREE_CODE (n) == FUNCTION_DECL)
4851     return arg_assoc_type (k, TREE_TYPE (n));
4852   if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4853     {
4854       /* [basic.lookup.koenig]
4855
4856          If T is a template-id, its associated namespaces and classes
4857          are the namespace in which the template is defined; for
4858          member templates, the member template's class...  */
4859       tree template = TREE_OPERAND (n, 0);
4860       tree args = TREE_OPERAND (n, 1);
4861       tree ctx;
4862       tree arg;
4863
4864       if (TREE_CODE (template) == COMPONENT_REF)
4865         template = TREE_OPERAND (template, 1);
4866       
4867       /* First, the template.  There may actually be more than one if
4868          this is an overloaded function template.  But, in that case,
4869          we only need the first; all the functions will be in the same
4870          namespace.  */
4871       template = OVL_CURRENT (template);
4872
4873       ctx = CP_DECL_CONTEXT (template);
4874        
4875       if (TREE_CODE (ctx) == NAMESPACE_DECL)
4876         {
4877           if (arg_assoc_namespace (k, ctx) == 1)
4878             return 1;
4879         }
4880       /* It must be a member template.  */
4881       else if (arg_assoc_class (k, ctx) == 1)
4882         return 1;
4883
4884       /* Now the arguments.  */
4885       for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4886         if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
4887           return 1;
4888     }
4889   else
4890     {
4891       my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4892       
4893       for (; n; n = OVL_CHAIN (n))
4894         if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4895           return 1;
4896     }
4897
4898   return 0;
4899 }
4900
4901 /* Performs Koenig lookup depending on arguments, where fns
4902    are the functions found in normal lookup. */
4903
4904 tree
4905 lookup_arg_dependent (name, fns, args)
4906      tree name;
4907      tree fns;
4908      tree args;
4909 {
4910   struct arg_lookup k;
4911   tree fn = NULL_TREE;
4912
4913   k.name = name;
4914   k.functions = fns;
4915   k.classes = NULL_TREE;
4916
4917   /* Note that we've already looked at some namespaces during normal
4918      unqualified lookup, unless we found a decl in function scope.  */
4919   if (fns)
4920     fn = OVL_CURRENT (fns);
4921   if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
4922     k.namespaces = NULL_TREE;
4923   else
4924     unqualified_namespace_lookup (name, 0, &k.namespaces);
4925
4926   arg_assoc_args (&k, args);
4927   return k.functions;
4928 }
4929
4930 /* Process a namespace-alias declaration. */
4931
4932 void
4933 do_namespace_alias (alias, namespace)
4934      tree alias, namespace;
4935 {
4936   if (TREE_CODE (namespace) != NAMESPACE_DECL)
4937     {
4938       /* The parser did not find it, so it's not there. */
4939       cp_error ("unknown namespace `%D'", namespace);
4940       return;
4941     }
4942
4943   namespace = ORIGINAL_NAMESPACE (namespace);
4944
4945   /* Build the alias. */
4946   alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);     
4947   DECL_NAMESPACE_ALIAS (alias) = namespace;
4948   pushdecl (alias);
4949 }
4950
4951 /* Check a non-member using-declaration. Return the name and scope
4952    being used, and the USING_DECL, or NULL_TREE on failure. */
4953
4954 static tree
4955 validate_nonmember_using_decl (decl, scope, name)
4956      tree decl;
4957      tree *scope;
4958      tree *name;
4959 {
4960   if (TREE_CODE (decl) == SCOPE_REF
4961       && TREE_OPERAND (decl, 0) == fake_std_node)
4962     {
4963       *scope = global_namespace;
4964       *name = TREE_OPERAND (decl, 1);
4965     }
4966   else if (TREE_CODE (decl) == SCOPE_REF)
4967     {
4968       *scope = TREE_OPERAND (decl, 0);
4969       *name = TREE_OPERAND (decl, 1);
4970
4971       /* [namespace.udecl]
4972
4973          A using-declaration for a class member shall be a
4974          member-declaration.  */
4975       if (!processing_template_decl
4976           && TREE_CODE (*scope) != NAMESPACE_DECL)
4977         {
4978           if (TYPE_P (*scope))
4979             cp_error ("`%T' is not a namespace", *scope);
4980           else
4981             cp_error ("`%D' is not a namespace", *scope);
4982           return NULL_TREE;
4983         }
4984     }
4985   else if (TREE_CODE (decl) == IDENTIFIER_NODE
4986            || TREE_CODE (decl) == TYPE_DECL
4987            || TREE_CODE (decl) == TEMPLATE_DECL)
4988     {
4989       *scope = global_namespace;
4990       *name = decl;
4991     }
4992   else
4993     my_friendly_abort (382);
4994   if (DECL_P (*name))
4995     *name = DECL_NAME (*name);
4996   /* Make a USING_DECL. */
4997   return push_using_decl (*scope, *name);
4998 }
4999
5000 /* Process local and global using-declarations. */
5001
5002 static void
5003 do_nonmember_using_decl (scope, name, oldval, oldtype, newval, newtype)
5004      tree scope, name;
5005      tree oldval, oldtype;
5006      tree *newval, *newtype;
5007 {
5008   tree decls;
5009
5010   *newval = *newtype = NULL_TREE;
5011   decls = make_node (CPLUS_BINDING);
5012   if (!qualified_lookup_using_namespace (name, scope, decls, 0))
5013     /* Lookup error */
5014     return;
5015
5016   if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
5017     {
5018       cp_error ("`%D' not declared", name);
5019       return;
5020     }
5021
5022   /* Check for using functions. */
5023   if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
5024     {
5025       tree tmp, tmp1;
5026
5027       if (oldval && !is_overloaded_fn (oldval))
5028         {
5029           duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval);
5030           oldval = NULL_TREE;
5031         }
5032
5033       *newval = oldval;
5034       for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
5035         {
5036           tree new_fn = OVL_CURRENT (tmp);
5037
5038           /* [namespace.udecl]
5039
5040              If a function declaration in namespace scope or block
5041              scope has the same name and the same parameter types as a
5042              function introduced by a using declaration the program is
5043              ill-formed.  */
5044           for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
5045             {
5046               tree old_fn = OVL_CURRENT (tmp1);
5047
5048               if (new_fn == old_fn)
5049                 /* The function already exists in the current namespace.  */
5050                 break;
5051               else if (OVL_USED (tmp1))
5052                 continue; /* this is a using decl */
5053               else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
5054                                   TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
5055                 {
5056                   /* There was already a non-using declaration in
5057                      this scope with the same parameter types. If both
5058                      are the same extern "C" functions, that's ok.  */
5059                   if (!decls_match (new_fn, old_fn))
5060                     cp_error ("`%D' is already declared in this scope", name);
5061                   break;
5062                 }
5063             }
5064
5065           /* If we broke out of the loop, there's no reason to add
5066              this function to the using declarations for this
5067              scope.  */
5068           if (tmp1)
5069             continue;
5070             
5071           *newval = build_overload (OVL_CURRENT (tmp), *newval);
5072           if (TREE_CODE (*newval) != OVERLOAD)
5073             *newval = ovl_cons (*newval, NULL_TREE);
5074           OVL_USED (*newval) = 1;
5075         }
5076     }
5077   else 
5078     {
5079       *newval = BINDING_VALUE (decls);
5080       if (oldval)
5081         duplicate_decls (*newval, oldval);
5082     } 
5083
5084   *newtype = BINDING_TYPE (decls);
5085   if (oldtype && *newtype && oldtype != *newtype)
5086     {
5087       cp_error ("using directive `%D' introduced ambiguous type `%T'",
5088                 name, oldtype);
5089       return;
5090     }
5091 }
5092
5093 /* Process a using-declaration not appearing in class or local scope. */
5094
5095 void
5096 do_toplevel_using_decl (decl)
5097      tree decl;
5098 {
5099   tree scope, name, binding;
5100   tree oldval, oldtype, newval, newtype;
5101
5102   decl = validate_nonmember_using_decl (decl, &scope, &name);
5103   if (decl == NULL_TREE)
5104     return;
5105   
5106   binding = binding_for_name (name, current_namespace);
5107
5108   oldval = BINDING_VALUE (binding);
5109   oldtype = BINDING_TYPE (binding);
5110
5111   do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5112
5113   /* Copy declarations found. */
5114   if (newval)
5115     BINDING_VALUE (binding) = newval;
5116   if (newtype)
5117     BINDING_TYPE (binding) = newtype;
5118   return;
5119 }
5120
5121 /* Process a using-declaration at function scope.  */
5122
5123 void
5124 do_local_using_decl (decl)
5125      tree decl;
5126 {
5127   tree scope, name;
5128   tree oldval, oldtype, newval, newtype;
5129
5130   decl = validate_nonmember_using_decl (decl, &scope, &name);
5131   if (decl == NULL_TREE)
5132     return;
5133
5134   if (building_stmt_tree ()
5135       && at_function_scope_p ())
5136     add_decl_stmt (decl);
5137   if (processing_template_decl)
5138     return;
5139
5140   oldval = lookup_name_current_level (name);
5141   oldtype = lookup_type_current_level (name);
5142
5143   do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5144
5145   if (newval)
5146     {
5147       if (is_overloaded_fn (newval))
5148         {
5149           tree fn, term;
5150
5151           /* We only need to push declarations for those functions
5152              that were not already bound in the current level.
5153              The old value might be NULL_TREE, it might be a single
5154              function, or an OVERLOAD.  */
5155           if (oldval && TREE_CODE (oldval) == OVERLOAD)
5156             term = OVL_FUNCTION (oldval);
5157           else
5158             term = oldval;
5159           for (fn = newval; fn && OVL_CURRENT (fn) != term; 
5160                fn = OVL_NEXT (fn))
5161             push_overloaded_decl (OVL_CURRENT (fn), 
5162                                   PUSH_LOCAL | PUSH_USING);
5163         }
5164       else
5165         push_local_binding (name, newval, PUSH_USING);
5166     }
5167   if (newtype)
5168     set_identifier_type_value (name, newtype);
5169 }
5170
5171 tree
5172 do_class_using_decl (decl)
5173      tree decl;
5174 {
5175   tree name, value;
5176
5177   if (TREE_CODE (decl) != SCOPE_REF
5178       || !TYPE_P (TREE_OPERAND (decl, 0)))
5179     {
5180       cp_error ("using-declaration for non-member at class scope");
5181       return NULL_TREE;
5182     }
5183   name = TREE_OPERAND (decl, 1);
5184   if (TREE_CODE (name) == BIT_NOT_EXPR)
5185     {
5186       cp_error ("using-declaration for destructor");
5187       return NULL_TREE;
5188     }
5189   if (TREE_CODE (name) == TYPE_DECL)
5190     name = DECL_NAME (name);
5191
5192   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
5193
5194   value = build_lang_decl (USING_DECL, name, void_type_node);
5195   DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
5196   return value;
5197 }
5198
5199 /* Process a using-directive. */
5200
5201 void
5202 do_using_directive (namespace)
5203      tree namespace;
5204 {
5205   if (namespace == fake_std_node)
5206     return;
5207   
5208   /* using namespace A::B::C; */
5209   if (TREE_CODE (namespace) == SCOPE_REF)
5210       namespace = TREE_OPERAND (namespace, 1);
5211   if (TREE_CODE (namespace) == IDENTIFIER_NODE)
5212     {
5213       /* Lookup in lexer did not find a namespace. */
5214       if (!processing_template_decl)
5215         cp_error ("namespace `%T' undeclared", namespace);
5216       return;
5217     }
5218   if (TREE_CODE (namespace) != NAMESPACE_DECL)
5219     {
5220       if (!processing_template_decl)
5221         cp_error ("`%T' is not a namespace", namespace);
5222       return;
5223     }
5224   namespace = ORIGINAL_NAMESPACE (namespace);
5225   if (!toplevel_bindings_p ())
5226     push_using_directive (namespace);
5227   else
5228     /* direct usage */
5229     add_using_namespace (current_namespace, namespace, 0);
5230 }
5231
5232 void
5233 check_default_args (x)
5234      tree x;
5235 {
5236   tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5237   int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5238   for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5239     {
5240       if (TREE_PURPOSE (arg))
5241         saw_def = 1;
5242       else if (saw_def)
5243         {
5244           cp_error_at ("default argument missing for parameter %P of `%+#D'",
5245                        i, x);
5246           break;
5247         }
5248     }
5249 }
5250
5251 void
5252 mark_used (decl)
5253      tree decl;
5254 {
5255   TREE_USED (decl) = 1;
5256   if (processing_template_decl)
5257     return;
5258   assemble_external (decl);
5259
5260   /* Is it a synthesized method that needs to be synthesized?  */
5261   if (TREE_CODE (decl) == FUNCTION_DECL
5262       && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5263       && DECL_ARTIFICIAL (decl) 
5264       && ! DECL_INITIAL (decl)
5265       /* Kludge: don't synthesize for default args.  */
5266       && current_function_decl)
5267     {
5268       synthesize_method (decl);
5269       /* If we've already synthesized the method we don't need to
5270          instantiate it, so we can return right away.  */
5271       return;
5272     }
5273
5274   /* If this is a function or variable that is an instance of some
5275      template, we now know that we will need to actually do the
5276      instantiation. We check that DECL is not an explicit
5277      instantiation because that is not checked in instantiate_decl.  */
5278   if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
5279       && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
5280       && (!DECL_EXPLICIT_INSTANTIATION (decl)
5281           || (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))))
5282     instantiate_decl (decl, /*defer_ok=*/1);
5283 }
5284
5285 /* Helper function for named_class_head_sans_basetype nonterminal.  We
5286    have just seen something of the form `AGGR SCOPE::ID'.  Return a
5287    TYPE_DECL for the type declared by ID in SCOPE.  */
5288
5289 tree
5290 handle_class_head (aggr, scope, id)
5291      tree aggr, scope, id;
5292 {
5293   tree decl = NULL_TREE;
5294
5295   if (TREE_CODE (id) == TYPE_DECL)
5296     /* We must bash typedefs back to the main decl of the type. Otherwise
5297        we become confused about scopes.  */
5298     decl = TYPE_MAIN_DECL (TREE_TYPE (id));
5299   else if (DECL_CLASS_TEMPLATE_P (id))
5300     decl = DECL_TEMPLATE_RESULT (id);
5301   else 
5302     {
5303       tree current = current_scope ();
5304   
5305       if (current == NULL_TREE)
5306         current = current_namespace;
5307       if (scope == fake_std_node)
5308         scope = global_namespace;
5309       if (scope == NULL_TREE)
5310         scope = global_namespace;
5311
5312       if (TYPE_P (scope))
5313         {
5314           /* According to the suggested resolution of core issue 180,
5315              'typename' is assumed after a class-key.  */
5316           decl = make_typename_type (scope, id, 1);
5317           if (decl != error_mark_node)
5318             decl = TYPE_MAIN_DECL (decl);
5319           else
5320             decl = NULL_TREE;
5321         }
5322       else if (scope == current)
5323         {
5324           /* We've been given AGGR SCOPE::ID, when we're already inside SCOPE.
5325              Be nice about it.  */
5326           if (pedantic)
5327             cp_pedwarn ("extra qualification `%T::' on member `%D' ignored",
5328                         FROB_CONTEXT (scope), id);
5329         }
5330       else if (scope != global_namespace)
5331         cp_error ("`%T' does not have a nested type named `%D'", scope, id);
5332       else
5333         cp_error ("no file-scope type named `%D'", id);
5334       
5335       /* Inject it at the current scope.  */
5336       if (! decl)
5337         decl = TYPE_MAIN_DECL (xref_tag (aggr, id, 1));
5338     }
5339  
5340   /* Enter the SCOPE.  If this turns out not to be a definition, the
5341      parser must leave the scope.  */
5342   push_scope (CP_DECL_CONTEXT (decl));
5343
5344   /* If we see something like:
5345
5346        template <typename T> struct S::I ....
5347        
5348      we must create a TEMPLATE_DECL for the nested type.  */
5349   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
5350     decl = push_template_decl (decl);
5351
5352   return decl;
5353 }
5354
5355 /* Initialize decl2.c.  */
5356
5357 void
5358 init_decl2 ()
5359 {
5360   ggc_add_tree_varray_root (&deferred_fns, 1);
5361   ggc_add_tree_varray_root (&pending_statics, 1);
5362   ggc_add_tree_varray_root (&ssdf_decls, 1);
5363   ggc_add_tree_root (&ssdf_decl, 1);
5364   ggc_add_tree_root (&priority_decl, 1);
5365   ggc_add_tree_root (&initialize_p_decl, 1);
5366 }