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