OSDN Git Service

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