OSDN Git Service

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