OSDN Git Service

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