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