OSDN Git Service

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