OSDN Git Service

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