OSDN Git Service

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