OSDN Git Service

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