OSDN Git Service

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