OSDN Git Service

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