OSDN Git Service

* doc/extend.texi: Document optional priority argument to
[pf3gnuchains/gcc-fork.git] / gcc / cp / decl2.c
1 /* Process declarations and variables for C++ compiler.
2    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005  Free Software Foundation, Inc.
4    Hacked by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, 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 "output.h"
42 #include "except.h"
43 #include "toplev.h"
44 #include "timevar.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "c-common.h"
48 #include "tree-mudflap.h"
49 #include "cgraph.h"
50 #include "tree-inline.h"
51 #include "c-pragma.h"
52 #include "tree-dump.h"
53 #include "intl.h"
54
55 extern cpp_reader *parse_in;
56
57 /* This structure contains information about the initializations
58    and/or destructions required for a particular priority level.  */
59 typedef struct priority_info_s {
60   /* Nonzero if there have been any initializations at this priority
61      throughout the translation unit.  */
62   int initializations_p;
63   /* Nonzero if there have been any destructions at this priority
64      throughout the translation unit.  */
65   int destructions_p;
66 } *priority_info;
67
68 static void mark_vtable_entries (tree);
69 static bool maybe_emit_vtables (tree);
70 static bool acceptable_java_type (tree);
71 static tree start_objects (int, int);
72 static void finish_objects (int, int, tree);
73 static tree start_static_storage_duration_function (unsigned);
74 static void finish_static_storage_duration_function (tree);
75 static priority_info get_priority_info (int);
76 static void do_static_initialization_or_destruction (tree, bool);
77 static void one_static_initialization_or_destruction (tree, tree, bool);
78 static void generate_ctor_or_dtor_function (bool, int, location_t *);
79 static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
80                                                           void *);
81 static tree prune_vars_needing_no_initialization (tree *);
82 static void write_out_vars (tree);
83 static void import_export_class (tree);
84 static tree get_guard_bits (tree);
85 static void determine_visibility_from_class (tree, tree);
86
87 /* A list of static class variables.  This is needed, because a
88    static class variable can be declared inside the class without
89    an initializer, and then initialized, statically, outside the class.  */
90 static GTY(()) VEC(tree,gc) *pending_statics;
91
92 /* A list of functions which were declared inline, but which we
93    may need to emit outline anyway.  */
94 static GTY(()) VEC(tree,gc) *deferred_fns;
95
96 /* Nonzero if we're done parsing and into end-of-file activities.  */
97
98 int at_eof;
99
100 \f
101
102 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
103    FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
104    that apply to the function).  */
105
106 tree
107 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals)
108 {
109   tree raises;
110   int type_quals;
111
112   if (fntype == error_mark_node || ctype == error_mark_node)
113     return error_mark_node;
114
115   type_quals = quals & ~TYPE_QUAL_RESTRICT;
116   ctype = cp_build_qualified_type (ctype, type_quals);
117   fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
118                                        (TREE_CODE (fntype) == METHOD_TYPE
119                                         ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
120                                         : TYPE_ARG_TYPES (fntype)));
121   raises = TYPE_RAISES_EXCEPTIONS (fntype);
122   if (raises)
123     fntype = build_exception_variant (fntype, raises);
124
125   return fntype;
126 }
127
128 /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
129    appropriately.  */
130
131 tree
132 cp_build_parm_decl (tree name, tree type)
133 {
134   tree parm = build_decl (PARM_DECL, name, type);
135   /* DECL_ARG_TYPE is only used by the back end and the back end never
136      sees templates.  */
137   if (!processing_template_decl)
138     DECL_ARG_TYPE (parm) = type_passed_as (type);
139   return parm;
140 }
141
142 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
143    indicated NAME.  */
144
145 tree
146 build_artificial_parm (tree name, tree type)
147 {
148   tree parm = cp_build_parm_decl (name, type);
149   DECL_ARTIFICIAL (parm) = 1;
150   /* All our artificial parms are implicitly `const'; they cannot be
151      assigned to.  */
152   TREE_READONLY (parm) = 1;
153   return parm;
154 }
155
156 /* Constructors for types with virtual baseclasses need an "in-charge" flag
157    saying whether this constructor is responsible for initialization of
158    virtual baseclasses or not.  All destructors also need this "in-charge"
159    flag, which additionally determines whether or not the destructor should
160    free the memory for the object.
161
162    This function adds the "in-charge" flag to member function FN if
163    appropriate.  It is called from grokclassfn and tsubst.
164    FN must be either a constructor or destructor.
165
166    The in-charge flag follows the 'this' parameter, and is followed by the
167    VTT parm (if any), then the user-written parms.  */
168
169 void
170 maybe_retrofit_in_chrg (tree fn)
171 {
172   tree basetype, arg_types, parms, parm, fntype;
173
174   /* If we've already add the in-charge parameter don't do it again.  */
175   if (DECL_HAS_IN_CHARGE_PARM_P (fn))
176     return;
177
178   /* When processing templates we can't know, in general, whether or
179      not we're going to have virtual baseclasses.  */
180   if (processing_template_decl)
181     return;
182
183   /* We don't need an in-charge parameter for constructors that don't
184      have virtual bases.  */
185   if (DECL_CONSTRUCTOR_P (fn)
186       && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
187     return;
188
189   arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
190   basetype = TREE_TYPE (TREE_VALUE (arg_types));
191   arg_types = TREE_CHAIN (arg_types);
192
193   parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
194
195   /* If this is a subobject constructor or destructor, our caller will
196      pass us a pointer to our VTT.  */
197   if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
198     {
199       parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
200
201       /* First add it to DECL_ARGUMENTS between 'this' and the real args...  */
202       TREE_CHAIN (parm) = parms;
203       parms = parm;
204
205       /* ...and then to TYPE_ARG_TYPES.  */
206       arg_types = hash_tree_chain (vtt_parm_type, arg_types);
207
208       DECL_HAS_VTT_PARM_P (fn) = 1;
209     }
210
211   /* Then add the in-charge parm (before the VTT parm).  */
212   parm = build_artificial_parm (in_charge_identifier, integer_type_node);
213   TREE_CHAIN (parm) = parms;
214   parms = parm;
215   arg_types = hash_tree_chain (integer_type_node, arg_types);
216
217   /* Insert our new parameter(s) into the list.  */
218   TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
219
220   /* And rebuild the function type.  */
221   fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
222                                        arg_types);
223   if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
224     fntype = build_exception_variant (fntype,
225                                       TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
226   TREE_TYPE (fn) = fntype;
227
228   /* Now we've got the in-charge parameter.  */
229   DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
230 }
231
232 /* Classes overload their constituent function names automatically.
233    When a function name is declared in a record structure,
234    its name is changed to it overloaded name.  Since names for
235    constructors and destructors can conflict, we place a leading
236    '$' for destructors.
237
238    CNAME is the name of the class we are grokking for.
239
240    FUNCTION is a FUNCTION_DECL.  It was created by `grokdeclarator'.
241
242    FLAGS contains bits saying what's special about today's
243    arguments.  1 == DESTRUCTOR.  2 == OPERATOR.
244
245    If FUNCTION is a destructor, then we must add the `auto-delete' field
246    as a second parameter.  There is some hair associated with the fact
247    that we must "declare" this variable in the manner consistent with the
248    way the rest of the arguments were declared.
249
250    QUALS are the qualifiers for the this pointer.  */
251
252 void
253 grokclassfn (tree ctype, tree function, enum overload_flags flags)
254 {
255   tree fn_name = DECL_NAME (function);
256
257   /* Even within an `extern "C"' block, members get C++ linkage.  See
258      [dcl.link] for details.  */
259   SET_DECL_LANGUAGE (function, lang_cplusplus);
260
261   if (fn_name == NULL_TREE)
262     {
263       error ("name missing for member function");
264       fn_name = get_identifier ("<anonymous>");
265       DECL_NAME (function) = fn_name;
266     }
267
268   DECL_CONTEXT (function) = ctype;
269
270   if (flags == DTOR_FLAG)
271     DECL_DESTRUCTOR_P (function) = 1;
272
273   if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
274     maybe_retrofit_in_chrg (function);
275 }
276
277 /* Create an ARRAY_REF, checking for the user doing things backwards
278    along the way.  */
279
280 tree
281 grok_array_decl (tree array_expr, tree index_exp)
282 {
283   tree type;
284   tree expr;
285   tree orig_array_expr = array_expr;
286   tree orig_index_exp = index_exp;
287
288   if (error_operand_p (array_expr) || error_operand_p (index_exp))
289     return error_mark_node;
290
291   if (processing_template_decl)
292     {
293       if (type_dependent_expression_p (array_expr)
294           || type_dependent_expression_p (index_exp))
295         return build_min_nt (ARRAY_REF, array_expr, index_exp,
296                              NULL_TREE, NULL_TREE);
297       array_expr = build_non_dependent_expr (array_expr);
298       index_exp = build_non_dependent_expr (index_exp);
299     }
300
301   type = TREE_TYPE (array_expr);
302   gcc_assert (type);
303   type = non_reference (type);
304
305   /* If they have an `operator[]', use that.  */
306   if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
307     expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL,
308                          array_expr, index_exp, NULL_TREE,
309                          /*overloaded_p=*/NULL);
310   else
311     {
312       tree p1, p2, i1, i2;
313
314       /* Otherwise, create an ARRAY_REF for a pointer or array type.
315          It is a little-known fact that, if `a' is an array and `i' is
316          an int, you can write `i[a]', which means the same thing as
317          `a[i]'.  */
318       if (TREE_CODE (type) == ARRAY_TYPE)
319         p1 = array_expr;
320       else
321         p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
322
323       if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
324         p2 = index_exp;
325       else
326         p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
327
328       i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
329                                        false);
330       i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
331                                        false);
332
333       if ((p1 && i2) && (i1 && p2))
334         error ("ambiguous conversion for array subscript");
335
336       if (p1 && i2)
337         array_expr = p1, index_exp = i2;
338       else if (i1 && p2)
339         array_expr = p2, index_exp = i1;
340       else
341         {
342           error ("invalid types %<%T[%T]%> for array subscript",
343                  type, TREE_TYPE (index_exp));
344           return error_mark_node;
345         }
346
347       if (array_expr == error_mark_node || index_exp == error_mark_node)
348         error ("ambiguous conversion for array subscript");
349
350       expr = build_array_ref (array_expr, index_exp);
351     }
352   if (processing_template_decl && expr != error_mark_node)
353     return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
354                               NULL_TREE, NULL_TREE);
355   return expr;
356 }
357
358 /* Given the cast expression EXP, checking out its validity.   Either return
359    an error_mark_node if there was an unavoidable error, return a cast to
360    void for trying to delete a pointer w/ the value 0, or return the
361    call to delete.  If DOING_VEC is true, we handle things differently
362    for doing an array delete.
363    Implements ARM $5.3.4.  This is called from the parser.  */
364
365 tree
366 delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete)
367 {
368   tree t, type;
369
370   if (exp == error_mark_node)
371     return exp;
372
373   if (processing_template_decl)
374     {
375       t = build_min (DELETE_EXPR, void_type_node, exp, size);
376       DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
377       DELETE_EXPR_USE_VEC (t) = doing_vec;
378       TREE_SIDE_EFFECTS (t) = 1;
379       return t;
380     }
381
382   /* An array can't have been allocated by new, so complain.  */
383   if (TREE_CODE (exp) == VAR_DECL
384       && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
385     warning (0, "deleting array %q#D", exp);
386
387   t = build_expr_type_conversion (WANT_POINTER, exp, true);
388
389   if (t == NULL_TREE || t == error_mark_node)
390     {
391       error ("type %q#T argument given to %<delete%>, expected pointer",
392              TREE_TYPE (exp));
393       return error_mark_node;
394     }
395
396   type = TREE_TYPE (t);
397
398   /* As of Valley Forge, you can delete a pointer to const.  */
399
400   /* You can't delete functions.  */
401   if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
402     {
403       error ("cannot delete a function.  Only pointer-to-objects are "
404              "valid arguments to %<delete%>");
405       return error_mark_node;
406     }
407
408   /* Deleting ptr to void is undefined behavior [expr.delete/3].  */
409   if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
410     {
411       warning (0, "deleting %qT is undefined", type);
412       doing_vec = 0;
413     }
414
415   /* Deleting a pointer with the value zero is valid and has no effect.  */
416   if (integer_zerop (t))
417     return build1 (NOP_EXPR, void_type_node, t);
418
419   if (doing_vec)
420     return build_vec_delete (t, /*maxindex=*/NULL_TREE,
421                              sfk_deleting_destructor,
422                              use_global_delete);
423   else
424     return build_delete (type, t, sfk_deleting_destructor,
425                          LOOKUP_NORMAL, use_global_delete);
426 }
427
428 /* Report an error if the indicated template declaration is not the
429    sort of thing that should be a member template.  */
430
431 void
432 check_member_template (tree tmpl)
433 {
434   tree decl;
435
436   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
437   decl = DECL_TEMPLATE_RESULT (tmpl);
438
439   if (TREE_CODE (decl) == FUNCTION_DECL
440       || (TREE_CODE (decl) == TYPE_DECL
441           && IS_AGGR_TYPE (TREE_TYPE (decl))))
442     {
443       /* The parser rejects template declarations in local classes.  */
444       gcc_assert (!current_function_decl);
445       /* The parser rejects any use of virtual in a function template.  */
446       gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
447                     && DECL_VIRTUAL_P (decl)));
448
449       /* The debug-information generating code doesn't know what to do
450          with member templates.  */
451       DECL_IGNORED_P (tmpl) = 1;
452     }
453   else
454     error ("template declaration of %q#D", decl);
455 }
456
457 /* Return true iff TYPE is a valid Java parameter or return type.  */
458
459 static bool
460 acceptable_java_type (tree type)
461 {
462   if (type == error_mark_node)
463     return false;
464
465   if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
466     return true;
467   if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
468     {
469       type = TREE_TYPE (type);
470       if (TREE_CODE (type) == RECORD_TYPE)
471         {
472           tree args;  int i;
473           if (! TYPE_FOR_JAVA (type))
474             return false;
475           if (! CLASSTYPE_TEMPLATE_INFO (type))
476             return true;
477           args = CLASSTYPE_TI_ARGS (type);
478           i = TREE_VEC_LENGTH (args);
479           while (--i >= 0)
480             {
481               type = TREE_VEC_ELT (args, i);
482               if (TREE_CODE (type) == POINTER_TYPE)
483                 type = TREE_TYPE (type);
484               if (! TYPE_FOR_JAVA (type))
485                 return false;
486             }
487           return true;
488         }
489     }
490   return false;
491 }
492
493 /* For a METHOD in a Java class CTYPE, return true if
494    the parameter and return types are valid Java types.
495    Otherwise, print appropriate error messages, and return false.  */
496
497 bool
498 check_java_method (tree method)
499 {
500   bool jerr = false;
501   tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
502   tree ret_type = TREE_TYPE (TREE_TYPE (method));
503
504   if (!acceptable_java_type (ret_type))
505     {
506       error ("Java method %qD has non-Java return type %qT",
507              method, ret_type);
508       jerr = true;
509     }
510
511   arg_types = TREE_CHAIN (arg_types);
512   if (DECL_HAS_IN_CHARGE_PARM_P (method))
513     arg_types = TREE_CHAIN (arg_types);
514   if (DECL_HAS_VTT_PARM_P (method))
515     arg_types = TREE_CHAIN (arg_types);
516
517   for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
518     {
519       tree type = TREE_VALUE (arg_types);
520       if (!acceptable_java_type (type))
521         {
522           if (type != error_mark_node)
523             error ("Java method %qD has non-Java parameter type %qT",
524                    method, type);
525           jerr = true;
526         }
527     }
528   return !jerr;
529 }
530
531 /* Sanity check: report error if this function FUNCTION is not
532    really a member of the class (CTYPE) it is supposed to belong to.
533    TEMPLATE_PARMS is used to specify the template parameters of a member
534    template passed as FUNCTION_DECL. If the member template is passed as a
535    TEMPLATE_DECL, it can be NULL since the parameters can be extracted
536    from the declaration. If the function is not a function template, it
537    must be NULL.
538    It returns the original declaration for the function, or NULL_TREE
539    if no declaration was found (and an error was emitted).  */
540
541 tree
542 check_classfn (tree ctype, tree function, tree template_parms)
543 {
544   int ix;
545   bool is_template;
546   tree pushed_scope;
547   
548   if (DECL_USE_TEMPLATE (function)
549       && !(TREE_CODE (function) == TEMPLATE_DECL
550            && DECL_TEMPLATE_SPECIALIZATION (function))
551       && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
552     /* Since this is a specialization of a member template,
553        we're not going to find the declaration in the class.
554        For example, in:
555
556          struct S { template <typename T> void f(T); };
557          template <> void S::f(int);
558
559        we're not going to find `S::f(int)', but there's no
560        reason we should, either.  We let our callers know we didn't
561        find the method, but we don't complain.  */
562     return NULL_TREE;
563
564   /* Basic sanity check: for a template function, the template parameters
565      either were not passed, or they are the same of DECL_TEMPLATE_PARMS.  */
566   if (TREE_CODE (function) == TEMPLATE_DECL)
567     {
568       gcc_assert (!template_parms
569                   || comp_template_parms (template_parms,
570                                           DECL_TEMPLATE_PARMS (function)));
571       template_parms = DECL_TEMPLATE_PARMS (function);
572     }
573
574   /* OK, is this a definition of a member template?  */
575   is_template = (template_parms != NULL_TREE);
576
577   /* We must enter the scope here, because conversion operators are
578      named by target type, and type equivalence relies on typenames
579      resolving within the scope of CTYPE.  */
580   pushed_scope = push_scope (ctype);
581   ix = class_method_index_for_fn (complete_type (ctype), function);
582   if (ix >= 0)
583     {
584       VEC(tree,gc) *methods = CLASSTYPE_METHOD_VEC (ctype);
585       tree fndecls, fndecl = 0;
586       bool is_conv_op;
587       const char *format = NULL;
588
589       for (fndecls = VEC_index (tree, methods, ix);
590            fndecls; fndecls = OVL_NEXT (fndecls))
591         {
592           tree p1, p2;
593
594           fndecl = OVL_CURRENT (fndecls);
595           p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
596           p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
597
598           /* We cannot simply call decls_match because this doesn't
599              work for static member functions that are pretending to
600              be methods, and because the name may have been changed by
601              asm("new_name").  */
602
603            /* Get rid of the this parameter on functions that become
604               static.  */
605           if (DECL_STATIC_FUNCTION_P (fndecl)
606               && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
607             p1 = TREE_CHAIN (p1);
608
609           /* A member template definition only matches a member template
610              declaration.  */
611           if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
612             continue;
613
614           if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
615                            TREE_TYPE (TREE_TYPE (fndecl)))
616               && compparms (p1, p2)
617               && (!is_template
618                   || comp_template_parms (template_parms,
619                                           DECL_TEMPLATE_PARMS (fndecl)))
620               && (DECL_TEMPLATE_SPECIALIZATION (function)
621                   == DECL_TEMPLATE_SPECIALIZATION (fndecl))
622               && (!DECL_TEMPLATE_SPECIALIZATION (function)
623                   || (DECL_TI_TEMPLATE (function)
624                       == DECL_TI_TEMPLATE (fndecl))))
625             break;
626         }
627       if (fndecls)
628         {
629           if (pushed_scope)
630             pop_scope (pushed_scope);
631           return OVL_CURRENT (fndecls);
632         }
633       
634       error ("prototype for %q#D does not match any in class %qT",
635              function, ctype);
636       is_conv_op = DECL_CONV_FN_P (fndecl);
637
638       if (is_conv_op)
639         ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
640       fndecls = VEC_index (tree, methods, ix);
641       while (fndecls)
642         {
643           fndecl = OVL_CURRENT (fndecls);
644           fndecls = OVL_NEXT (fndecls);
645
646           if (!fndecls && is_conv_op)
647             {
648               if (VEC_length (tree, methods) > (size_t) ++ix)
649                 {
650                   fndecls = VEC_index (tree, methods, ix);
651                   if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
652                     {
653                       fndecls = NULL_TREE;
654                       is_conv_op = false;
655                     }
656                 }
657               else
658                 is_conv_op = false;
659             }
660           if (format)
661             format = "                %+#D";
662           else if (fndecls)
663             format = N_("candidates are: %+#D");
664           else
665             format = N_("candidate is: %+#D");
666           error (format, fndecl);
667         }
668     }
669   else if (!COMPLETE_TYPE_P (ctype))
670     cxx_incomplete_type_error (function, ctype);
671   else
672     error ("no %q#D member function declared in class %qT",
673            function, ctype);
674
675   /* If we did not find the method in the class, add it to avoid
676      spurious errors (unless the CTYPE is not yet defined, in which
677      case we'll only confuse ourselves when the function is declared
678      properly within the class.  */
679   if (COMPLETE_TYPE_P (ctype))
680     add_method (ctype, function, NULL_TREE);
681   
682   if (pushed_scope)
683     pop_scope (pushed_scope);
684   return NULL_TREE;
685 }
686
687 /* DECL is a function with vague linkage.  Remember it so that at the
688    end of the translation unit we can decide whether or not to emit
689    it.  */
690
691 void
692 note_vague_linkage_fn (tree decl)
693 {
694   if (!DECL_DEFERRED_FN (decl))
695     {
696       DECL_DEFERRED_FN (decl) = 1;
697       DECL_DEFER_OUTPUT (decl) = 1;
698       VEC_safe_push (tree, gc, deferred_fns, decl);
699     }
700 }
701
702 /* We have just processed the DECL, which is a static data member.
703    The other parameters are as for cp_finish_decl.  */
704
705 void
706 finish_static_data_member_decl (tree decl,
707                                 tree init, bool init_const_expr_p,
708                                 tree asmspec_tree,
709                                 int flags)
710 {
711   DECL_CONTEXT (decl) = current_class_type;
712
713   /* We cannot call pushdecl here, because that would fill in the
714      TREE_CHAIN of our decl.  Instead, we modify cp_finish_decl to do
715      the right thing, namely, to put this decl out straight away.  */
716
717   if (! processing_template_decl)
718     VEC_safe_push (tree, gc, pending_statics, decl);
719
720   if (LOCAL_CLASS_P (current_class_type))
721     pedwarn ("local class %q#T shall not have static data member %q#D",
722              current_class_type, decl);
723
724   /* Static consts need not be initialized in the class definition.  */
725   if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
726     {
727       static int explained = 0;
728
729       error ("initializer invalid for static member with constructor");
730       if (!explained)
731         {
732           error ("(an out of class initialization is required)");
733           explained = 1;
734         }
735       init = NULL_TREE;
736     }
737   /* Force the compiler to know when an uninitialized static const
738      member is being used.  */
739   if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
740     TREE_USED (decl) = 1;
741   DECL_INITIAL (decl) = init;
742   DECL_IN_AGGR_P (decl) = 1;
743
744   cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
745 }
746
747 /* DECLARATOR and DECLSPECS correspond to a class member.  The other
748    parameters are as for cp_finish_decl.  Return the DECL for the
749    class member declared.  */
750
751 tree
752 grokfield (const cp_declarator *declarator,
753            cp_decl_specifier_seq *declspecs,
754            tree init, bool init_const_expr_p,
755            tree asmspec_tree,
756            tree attrlist)
757 {
758   tree value;
759   const char *asmspec = 0;
760   int flags = LOOKUP_ONLYCONVERTING;
761
762   if (init
763       && TREE_CODE (init) == TREE_LIST
764       && TREE_VALUE (init) == error_mark_node
765       && TREE_CHAIN (init) == NULL_TREE)
766     init = NULL_TREE;
767
768   value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
769   if (! value || error_operand_p (value))
770     /* friend or constructor went bad.  */
771     return error_mark_node;
772
773   if (TREE_CODE (value) == TYPE_DECL && init)
774     {
775       error ("typedef %qD is initialized (use __typeof__ instead)", value);
776       init = NULL_TREE;
777     }
778
779   /* Pass friendly classes back.  */
780   if (value == void_type_node)
781     return value;
782
783   /* Pass friend decls back.  */
784   if ((TREE_CODE (value) == FUNCTION_DECL
785        || TREE_CODE (value) == TEMPLATE_DECL)
786       && DECL_CONTEXT (value) != current_class_type)
787     return value;
788
789   if (DECL_NAME (value) != NULL_TREE
790       && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
791       && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
792     error ("member %qD conflicts with virtual function table field name",
793            value);
794
795   /* Stash away type declarations.  */
796   if (TREE_CODE (value) == TYPE_DECL)
797     {
798       DECL_NONLOCAL (value) = 1;
799       DECL_CONTEXT (value) = current_class_type;
800
801       if (processing_template_decl)
802         value = push_template_decl (value);
803
804       if (attrlist)
805         {
806           /* Avoid storing attributes in template parameters:
807              tsubst is not ready to handle them.  */
808           tree type = TREE_TYPE (value);
809           if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
810               || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
811             sorry ("applying attributes to template parameters is not implemented");
812           else
813             cplus_decl_attributes (&value, attrlist, 0);
814         }
815
816       return value;
817     }
818
819   if (DECL_IN_AGGR_P (value))
820     {
821       error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
822       return void_type_node;
823     }
824
825   if (asmspec_tree && asmspec_tree != error_mark_node)
826     asmspec = TREE_STRING_POINTER (asmspec_tree);
827
828   if (init)
829     {
830       if (TREE_CODE (value) == FUNCTION_DECL)
831         {
832           /* Initializers for functions are rejected early in the parser.
833              If we get here, it must be a pure specifier for a method.  */
834           if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
835             {
836               gcc_assert (error_operand_p (init) || integer_zerop (init));
837               DECL_PURE_VIRTUAL_P (value) = 1;
838             }
839           else
840             {
841               gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
842               error ("initializer specified for static member function %qD",
843                      value);
844             }
845         }
846       else if (pedantic && TREE_CODE (value) != VAR_DECL)
847         /* Already complained in grokdeclarator.  */
848         init = NULL_TREE;
849       else if (!processing_template_decl)
850         {
851           if (TREE_CODE (init) == CONSTRUCTOR)
852             init = digest_init (TREE_TYPE (value), init);
853           else
854             init = integral_constant_value (init);
855
856           if (init != error_mark_node && !TREE_CONSTANT (init))
857             {
858               /* We can allow references to things that are effectively
859                  static, since references are initialized with the
860                  address.  */
861               if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
862                   || (TREE_STATIC (init) == 0
863                       && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
864                 {
865                   error ("field initializer is not constant");
866                   init = error_mark_node;
867                 }
868             }
869         }
870     }
871
872   if (processing_template_decl
873       && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
874     {
875       value = push_template_decl (value);
876       if (error_operand_p (value))
877         return error_mark_node;
878     }
879
880   if (attrlist)
881     cplus_decl_attributes (&value, attrlist, 0);
882
883   switch (TREE_CODE (value))
884     {
885     case VAR_DECL:
886       finish_static_data_member_decl (value, init, init_const_expr_p,
887                                       asmspec_tree, flags);
888       return value;
889
890     case FIELD_DECL:
891       if (asmspec)
892         error ("%<asm%> specifiers are not permitted on non-static data members");
893       if (DECL_INITIAL (value) == error_mark_node)
894         init = error_mark_node;
895       cp_finish_decl (value, init, /*init_const_expr_p=*/false,
896                       NULL_TREE, flags);
897       DECL_INITIAL (value) = init;
898       DECL_IN_AGGR_P (value) = 1;
899       return value;
900
901     case  FUNCTION_DECL:
902       if (asmspec)
903         set_user_assembler_name (value, asmspec);
904
905       cp_finish_decl (value,
906                       /*init=*/NULL_TREE,
907                       /*init_const_expr_p=*/false,
908                       asmspec_tree, flags);
909
910       /* Pass friends back this way.  */
911       if (DECL_FRIEND_P (value))
912         return void_type_node;
913
914       DECL_IN_AGGR_P (value) = 1;
915       return value;
916
917     default:
918       gcc_unreachable ();
919     }
920   return NULL_TREE;
921 }
922
923 /* Like `grokfield', but for bitfields.
924    WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.  */
925
926 tree
927 grokbitfield (const cp_declarator *declarator,
928               cp_decl_specifier_seq *declspecs, tree width)
929 {
930   tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, NULL);
931
932   if (value == error_mark_node) 
933     return NULL_TREE; /* friends went bad.  */
934
935   /* Pass friendly classes back.  */
936   if (TREE_CODE (value) == VOID_TYPE)
937     return void_type_node;
938
939   if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
940       && (POINTER_TYPE_P (value)
941           || !dependent_type_p (TREE_TYPE (value))))
942     {
943       error ("bit-field %qD with non-integral type", value);
944       return error_mark_node;
945     }
946
947   if (TREE_CODE (value) == TYPE_DECL)
948     {
949       error ("cannot declare %qD to be a bit-field type", value);
950       return NULL_TREE;
951     }
952
953   /* Usually, finish_struct_1 catches bitfields with invalid types.
954      But, in the case of bitfields with function type, we confuse
955      ourselves into thinking they are member functions, so we must
956      check here.  */
957   if (TREE_CODE (value) == FUNCTION_DECL)
958     {
959       error ("cannot declare bit-field %qD with function type",
960              DECL_NAME (value));
961       return NULL_TREE;
962     }
963
964   if (DECL_IN_AGGR_P (value))
965     {
966       error ("%qD is already defined in the class %qT", value,
967              DECL_CONTEXT (value));
968       return void_type_node;
969     }
970
971   if (TREE_STATIC (value))
972     {
973       error ("static member %qD cannot be a bit-field", value);
974       return NULL_TREE;
975     }
976   finish_decl (value, NULL_TREE, NULL_TREE);
977
978   if (width != error_mark_node)
979     {
980       constant_expression_warning (width);
981       DECL_INITIAL (value) = width;
982       SET_DECL_C_BIT_FIELD (value);
983     }
984
985   DECL_IN_AGGR_P (value) = 1;
986   return value;
987 }
988
989 \f
990 void
991 cplus_decl_attributes (tree *decl, tree attributes, int flags)
992 {
993   if (*decl == NULL_TREE || *decl == void_type_node
994       || *decl == error_mark_node)
995     return;
996
997   if (TREE_CODE (*decl) == TEMPLATE_DECL)
998     decl = &DECL_TEMPLATE_RESULT (*decl);
999
1000   decl_attributes (decl, attributes, flags);
1001
1002   if (TREE_CODE (*decl) == TYPE_DECL)
1003     SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1004 }
1005 \f
1006 /* Walks through the namespace- or function-scope anonymous union
1007    OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1008    Returns one of the fields for use in the mangled name.  */
1009
1010 static tree
1011 build_anon_union_vars (tree type, tree object)
1012 {
1013   tree main_decl = NULL_TREE;
1014   tree field;
1015
1016   /* Rather than write the code to handle the non-union case,
1017      just give an error.  */
1018   if (TREE_CODE (type) != UNION_TYPE)
1019     error ("anonymous struct not inside named type");
1020
1021   for (field = TYPE_FIELDS (type);
1022        field != NULL_TREE;
1023        field = TREE_CHAIN (field))
1024     {
1025       tree decl;
1026       tree ref;
1027
1028       if (DECL_ARTIFICIAL (field))
1029         continue;
1030       if (TREE_CODE (field) != FIELD_DECL)
1031         {
1032           pedwarn ("%q+#D invalid; an anonymous union can only "
1033                    "have non-static data members", field);
1034           continue;
1035         }
1036
1037       if (TREE_PRIVATE (field))
1038         pedwarn ("private member %q+#D in anonymous union", field);
1039       else if (TREE_PROTECTED (field))
1040         pedwarn ("protected member %q+#D in anonymous union", field);
1041
1042       if (processing_template_decl)
1043         ref = build_min_nt (COMPONENT_REF, object,
1044                             DECL_NAME (field), NULL_TREE);
1045       else
1046         ref = build_class_member_access_expr (object, field, NULL_TREE,
1047                                               false);
1048
1049       if (DECL_NAME (field))
1050         {
1051           tree base;
1052
1053           decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1054
1055           base = get_base_address (object);
1056           TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1057           TREE_STATIC (decl) = TREE_STATIC (base);
1058           DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1059
1060           SET_DECL_VALUE_EXPR (decl, ref);
1061           DECL_HAS_VALUE_EXPR_P (decl) = 1;
1062
1063           decl = pushdecl (decl);
1064         }
1065       else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1066         decl = build_anon_union_vars (TREE_TYPE (field), ref);
1067       else
1068         decl = 0;
1069
1070       if (main_decl == NULL_TREE)
1071         main_decl = decl;
1072     }
1073
1074   return main_decl;
1075 }
1076
1077 /* Finish off the processing of a UNION_TYPE structure.  If the union is an
1078    anonymous union, then all members must be laid out together.  PUBLIC_P
1079    is nonzero if this union is not declared static.  */
1080
1081 void
1082 finish_anon_union (tree anon_union_decl)
1083 {
1084   tree type;
1085   tree main_decl;
1086   bool public_p;
1087
1088   if (anon_union_decl == error_mark_node)
1089     return;
1090
1091   type = TREE_TYPE (anon_union_decl);
1092   public_p = TREE_PUBLIC (anon_union_decl);
1093
1094   /* The VAR_DECL's context is the same as the TYPE's context.  */
1095   DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1096
1097   if (TYPE_FIELDS (type) == NULL_TREE)
1098     return;
1099
1100   if (public_p)
1101     {
1102       error ("namespace-scope anonymous aggregates must be static");
1103       return;
1104     }
1105
1106   main_decl = build_anon_union_vars (type, anon_union_decl);
1107   if (main_decl == error_mark_node)
1108     return;
1109   if (main_decl == NULL_TREE)
1110     {
1111       warning (0, "anonymous union with no members");
1112       return;
1113     }
1114
1115   if (!processing_template_decl)
1116     {
1117       /* Use main_decl to set the mangled name.  */
1118       DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1119       mangle_decl (anon_union_decl);
1120       DECL_NAME (anon_union_decl) = NULL_TREE;
1121     }
1122
1123   pushdecl (anon_union_decl);
1124   if (building_stmt_tree ()
1125       && at_function_scope_p ())
1126     add_decl_expr (anon_union_decl);
1127   else if (!processing_template_decl)
1128     rest_of_decl_compilation (anon_union_decl,
1129                               toplevel_bindings_p (), at_eof);
1130 }
1131 \f
1132 /* Auxiliary functions to make type signatures for
1133    `operator new' and `operator delete' correspond to
1134    what compiler will be expecting.  */
1135
1136 tree
1137 coerce_new_type (tree type)
1138 {
1139   int e = 0;
1140   tree args = TYPE_ARG_TYPES (type);
1141
1142   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1143
1144   if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1145     {
1146       e = 1;
1147       error ("%<operator new%> must return type %qT", ptr_type_node);
1148     }
1149
1150   if (!args || args == void_list_node
1151       || !same_type_p (TREE_VALUE (args), size_type_node))
1152     {
1153       e = 2;
1154       if (args && args != void_list_node)
1155         args = TREE_CHAIN (args);
1156       pedwarn ("%<operator new%> takes type %<size_t%> (%qT) "
1157                "as first parameter", size_type_node);
1158     }
1159   switch (e)
1160   {
1161     case 2:
1162       args = tree_cons (NULL_TREE, size_type_node, args);
1163       /* Fall through.  */
1164     case 1:
1165       type = build_exception_variant
1166               (build_function_type (ptr_type_node, args),
1167                TYPE_RAISES_EXCEPTIONS (type));
1168       /* Fall through.  */
1169     default:;
1170   }
1171   return type;
1172 }
1173
1174 tree
1175 coerce_delete_type (tree type)
1176 {
1177   int e = 0;
1178   tree args = TYPE_ARG_TYPES (type);
1179
1180   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1181
1182   if (!same_type_p (TREE_TYPE (type), void_type_node))
1183     {
1184       e = 1;
1185       error ("%<operator delete%> must return type %qT", void_type_node);
1186     }
1187
1188   if (!args || args == void_list_node
1189       || !same_type_p (TREE_VALUE (args), ptr_type_node))
1190     {
1191       e = 2;
1192       if (args && args != void_list_node)
1193         args = TREE_CHAIN (args);
1194       error ("%<operator delete%> takes type %qT as first parameter",
1195              ptr_type_node);
1196     }
1197   switch (e)
1198   {
1199     case 2:
1200       args = tree_cons (NULL_TREE, ptr_type_node, args);
1201       /* Fall through.  */
1202     case 1:
1203       type = build_exception_variant
1204               (build_function_type (void_type_node, args),
1205                TYPE_RAISES_EXCEPTIONS (type));
1206       /* Fall through.  */
1207     default:;
1208   }
1209
1210   return type;
1211 }
1212 \f
1213 static void
1214 mark_vtable_entries (tree decl)
1215 {
1216   tree fnaddr;
1217   unsigned HOST_WIDE_INT idx;
1218
1219   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1220                               idx, fnaddr)
1221     {
1222       tree fn;
1223
1224       STRIP_NOPS (fnaddr);
1225
1226       if (TREE_CODE (fnaddr) != ADDR_EXPR
1227           && TREE_CODE (fnaddr) != FDESC_EXPR)
1228         /* This entry is an offset: a virtual base class offset, a
1229            virtual call offset, an RTTI offset, etc.  */
1230         continue;
1231
1232       fn = TREE_OPERAND (fnaddr, 0);
1233       TREE_ADDRESSABLE (fn) = 1;
1234       /* When we don't have vcall offsets, we output thunks whenever
1235          we output the vtables that contain them.  With vcall offsets,
1236          we know all the thunks we'll need when we emit a virtual
1237          function, so we emit the thunks there instead.  */
1238       if (DECL_THUNK_P (fn))
1239         use_thunk (fn, /*emit_p=*/0);
1240       mark_used (fn);
1241     }
1242 }
1243
1244 /* Set DECL up to have the closest approximation of "initialized common"
1245    linkage available.  */
1246
1247 void
1248 comdat_linkage (tree decl)
1249 {
1250   if (flag_weak)
1251     make_decl_one_only (decl);
1252   else if (TREE_CODE (decl) == FUNCTION_DECL
1253            || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
1254     /* We can just emit function and compiler-generated variables
1255        statically; having multiple copies is (for the most part) only
1256        a waste of space.
1257
1258        There are two correctness issues, however: the address of a
1259        template instantiation with external linkage should be the
1260        same, independent of what translation unit asks for the
1261        address, and this will not hold when we emit multiple copies of
1262        the function.  However, there's little else we can do.
1263
1264        Also, by default, the typeinfo implementation assumes that
1265        there will be only one copy of the string used as the name for
1266        each type.  Therefore, if weak symbols are unavailable, the
1267        run-time library should perform a more conservative check; it
1268        should perform a string comparison, rather than an address
1269        comparison.  */
1270     TREE_PUBLIC (decl) = 0;
1271   else
1272     {
1273       /* Static data member template instantiations, however, cannot
1274          have multiple copies.  */
1275       if (DECL_INITIAL (decl) == 0
1276           || DECL_INITIAL (decl) == error_mark_node)
1277         DECL_COMMON (decl) = 1;
1278       else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1279         {
1280           DECL_COMMON (decl) = 1;
1281           DECL_INITIAL (decl) = error_mark_node;
1282         }
1283       else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1284         {
1285           /* We can't do anything useful; leave vars for explicit
1286              instantiation.  */
1287           DECL_EXTERNAL (decl) = 1;
1288           DECL_NOT_REALLY_EXTERN (decl) = 0;
1289         }
1290     }
1291
1292   if (DECL_LANG_SPECIFIC (decl))
1293     DECL_COMDAT (decl) = 1;
1294 }
1295
1296 /* For win32 we also want to put explicit instantiations in
1297    linkonce sections, so that they will be merged with implicit
1298    instantiations; otherwise we get duplicate symbol errors.
1299    For Darwin we do not want explicit instantiations to be
1300    linkonce.  */
1301
1302 void
1303 maybe_make_one_only (tree decl)
1304 {
1305   /* We used to say that this was not necessary on targets that support weak
1306      symbols, because the implicit instantiations will defer to the explicit
1307      one.  However, that's not actually the case in SVR4; a strong definition
1308      after a weak one is an error.  Also, not making explicit
1309      instantiations one_only means that we can end up with two copies of
1310      some template instantiations.  */
1311   if (! flag_weak)
1312     return;
1313
1314   /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1315      we can get away with not emitting them if they aren't used.  We need
1316      to for variables so that cp_finish_decl will update their linkage,
1317      because their DECL_INITIAL may not have been set properly yet.  */
1318
1319   if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1320       || (! DECL_EXPLICIT_INSTANTIATION (decl)
1321           && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1322     {
1323       make_decl_one_only (decl);
1324
1325       if (TREE_CODE (decl) == VAR_DECL)
1326         {
1327           DECL_COMDAT (decl) = 1;
1328           /* Mark it needed so we don't forget to emit it.  */
1329           mark_decl_referenced (decl);
1330         }
1331     }
1332 }
1333
1334 /* Determine whether or not we want to specifically import or export CTYPE,
1335    using various heuristics.  */
1336
1337 static void
1338 import_export_class (tree ctype)
1339 {
1340   /* -1 for imported, 1 for exported.  */
1341   int import_export = 0;
1342
1343   /* It only makes sense to call this function at EOF.  The reason is
1344      that this function looks at whether or not the first non-inline
1345      non-abstract virtual member function has been defined in this
1346      translation unit.  But, we can't possibly know that until we've
1347      seen the entire translation unit.  */
1348   gcc_assert (at_eof);
1349
1350   if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1351     return;
1352
1353   /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1354      we will have CLASSTYPE_INTERFACE_ONLY set but not
1355      CLASSTYPE_INTERFACE_KNOWN.  In that case, we don't want to use this
1356      heuristic because someone will supply a #pragma implementation
1357      elsewhere, and deducing it here would produce a conflict.  */
1358   if (CLASSTYPE_INTERFACE_ONLY (ctype))
1359     return;
1360
1361   if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1362     import_export = -1;
1363   else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1364     import_export = 1;
1365   else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1366            && !flag_implicit_templates)
1367     /* For a template class, without -fimplicit-templates, check the
1368        repository.  If the virtual table is assigned to this
1369        translation unit, then export the class; otherwise, import
1370        it.  */
1371       import_export = repo_export_class_p (ctype) ? 1 : -1;
1372   else if (TYPE_POLYMORPHIC_P (ctype))
1373     {
1374       /* The ABI specifies that the virtual table and associated
1375          information are emitted with the key method, if any.  */
1376       tree method = CLASSTYPE_KEY_METHOD (ctype);
1377       /* If weak symbol support is not available, then we must be
1378          careful not to emit the vtable when the key function is
1379          inline.  An inline function can be defined in multiple
1380          translation units.  If we were to emit the vtable in each
1381          translation unit containing a definition, we would get
1382          multiple definition errors at link-time.  */
1383       if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1384         import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1385     }
1386
1387   /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1388      a definition anywhere else.  */
1389   if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1390     import_export = 0;
1391
1392   /* Allow back ends the chance to overrule the decision.  */
1393   if (targetm.cxx.import_export_class)
1394     import_export = targetm.cxx.import_export_class (ctype, import_export);
1395
1396   if (import_export)
1397     {
1398       SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1399       CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1400     }
1401 }
1402
1403 /* Return true if VAR has already been provided to the back end; in that
1404    case VAR should not be modified further by the front end.  */
1405 static bool
1406 var_finalized_p (tree var)
1407 {
1408   return varpool_node (var)->finalized;
1409 }
1410
1411 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1412    must be emitted in this translation unit.  Mark it as such.  */
1413
1414 void
1415 mark_needed (tree decl)
1416 {
1417   /* It's possible that we no longer need to set
1418      TREE_SYMBOL_REFERENCED here directly, but doing so is
1419      harmless.  */
1420   TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
1421   mark_decl_referenced (decl);
1422 }
1423
1424 /* DECL is either a FUNCTION_DECL or a VAR_DECL.  This function
1425    returns true if a definition of this entity should be provided in
1426    this object file.  Callers use this function to determine whether
1427    or not to let the back end know that a definition of DECL is
1428    available in this translation unit.  */
1429
1430 bool
1431 decl_needed_p (tree decl)
1432 {
1433   gcc_assert (TREE_CODE (decl) == VAR_DECL
1434               || TREE_CODE (decl) == FUNCTION_DECL);
1435   /* This function should only be called at the end of the translation
1436      unit.  We cannot be sure of whether or not something will be
1437      COMDAT until that point.  */
1438   gcc_assert (at_eof);
1439
1440   /* All entities with external linkage that are not COMDAT should be
1441      emitted; they may be referred to from other object files.  */
1442   if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1443     return true;
1444   /* If this entity was used, let the back end see it; it will decide
1445      whether or not to emit it into the object file.  */
1446   if (TREE_USED (decl)
1447       || (DECL_ASSEMBLER_NAME_SET_P (decl)
1448           && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
1449       return true;
1450   /* Otherwise, DECL does not need to be emitted -- yet.  A subsequent
1451      reference to DECL might cause it to be emitted later.  */
1452   return false;
1453 }
1454
1455 /* If necessary, write out the vtables for the dynamic class CTYPE.
1456    Returns true if any vtables were emitted.  */
1457
1458 static bool
1459 maybe_emit_vtables (tree ctype)
1460 {
1461   tree vtbl;
1462   tree primary_vtbl;
1463   int needed = 0;
1464
1465   /* If the vtables for this class have already been emitted there is
1466      nothing more to do.  */
1467   primary_vtbl = CLASSTYPE_VTABLES (ctype);
1468   if (var_finalized_p (primary_vtbl))
1469     return false;
1470   /* Ignore dummy vtables made by get_vtable_decl.  */
1471   if (TREE_TYPE (primary_vtbl) == void_type_node)
1472     return false;
1473
1474   /* On some targets, we cannot determine the key method until the end
1475      of the translation unit -- which is when this function is
1476      called.  */
1477   if (!targetm.cxx.key_method_may_be_inline ())
1478     determine_key_method (ctype);
1479
1480   /* See if any of the vtables are needed.  */
1481   for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1482     {
1483       import_export_decl (vtbl);
1484       if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
1485         needed = 1;
1486     }
1487   if (!needed)
1488     {
1489       /* If the references to this class' vtables are optimized away,
1490          still emit the appropriate debugging information.  See
1491          dfs_debug_mark.  */
1492       if (DECL_COMDAT (primary_vtbl)
1493           && CLASSTYPE_DEBUG_REQUESTED (ctype))
1494         note_debug_info_needed (ctype);
1495       return false;
1496     }
1497
1498   /* The ABI requires that we emit all of the vtables if we emit any
1499      of them.  */
1500   for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl))
1501     {
1502       /* Mark entities references from the virtual table as used.  */
1503       mark_vtable_entries (vtbl);
1504
1505       if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1506         {
1507           tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl));
1508
1509           /* It had better be all done at compile-time.  */
1510           gcc_assert (!expr);
1511         }
1512
1513       /* Write it out.  */
1514       DECL_EXTERNAL (vtbl) = 0;
1515       rest_of_decl_compilation (vtbl, 1, 1);
1516
1517       /* Because we're only doing syntax-checking, we'll never end up
1518          actually marking the variable as written.  */
1519       if (flag_syntax_only)
1520         TREE_ASM_WRITTEN (vtbl) = 1;
1521     }
1522
1523   /* Since we're writing out the vtable here, also write the debug
1524      info.  */
1525   note_debug_info_needed (ctype);
1526
1527   return true;
1528 }
1529
1530 /* A special return value from type_visibility meaning internal
1531    linkage.  */
1532
1533 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
1534
1535 /* walk_tree helper function for type_visibility.  */
1536
1537 static tree
1538 min_vis_r (tree *tp, int *walk_subtrees, void *data)
1539 {
1540   int *vis_p = (int *)data;
1541   if (! TYPE_P (*tp))
1542     {
1543       *walk_subtrees = 0;
1544     }
1545   else if (CLASS_TYPE_P (*tp))
1546     {
1547       if (!TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
1548         {
1549           *vis_p = VISIBILITY_ANON;
1550           return *tp;
1551         }
1552       else if (CLASSTYPE_VISIBILITY (*tp) > *vis_p)
1553         *vis_p = CLASSTYPE_VISIBILITY (*tp);
1554     }
1555   return NULL;
1556 }
1557
1558 /* Returns the visibility of TYPE, which is the minimum visibility of its
1559    component types.  */
1560
1561 static int
1562 type_visibility (tree type)
1563 {
1564   int vis = VISIBILITY_DEFAULT;
1565   walk_tree_without_duplicates (&type, min_vis_r, &vis);
1566   return vis;
1567 }
1568
1569 /* Limit the visibility of DECL to VISIBILITY, if not explicitly
1570    specified (or if VISIBILITY is static).  */
1571
1572 static bool
1573 constrain_visibility (tree decl, int visibility)
1574 {
1575   if (visibility == VISIBILITY_ANON)
1576     {
1577       /* extern "C" declarations aren't affected by the anonymous
1578          namespace.  */
1579       if (!DECL_EXTERN_C_P (decl))
1580         {
1581           TREE_PUBLIC (decl) = 0;
1582           DECL_INTERFACE_KNOWN (decl) = 1;
1583           if (DECL_LANG_SPECIFIC (decl))
1584             DECL_NOT_REALLY_EXTERN (decl) = 1;
1585         }
1586     }
1587   else if (visibility > DECL_VISIBILITY (decl)
1588            && !DECL_VISIBILITY_SPECIFIED (decl))
1589     {
1590       DECL_VISIBILITY (decl) = visibility;
1591       return true;
1592     }
1593   return false;
1594 }
1595
1596 /* Constrain the visibility of DECL based on the visibility of its template
1597    arguments.  */
1598
1599 static void
1600 constrain_visibility_for_template (tree decl, tree targs)
1601 {
1602   /* If this is a template instantiation, check the innermost
1603      template args for visibility constraints.  The outer template
1604      args are covered by the class check.  */
1605   tree args = INNERMOST_TEMPLATE_ARGS (targs);
1606   int i;
1607   for (i = TREE_VEC_LENGTH (args); i > 0; --i)
1608     {
1609       int vis = 0;
1610
1611       tree arg = TREE_VEC_ELT (args, i-1);
1612       if (TYPE_P (arg))
1613         vis = type_visibility (arg);
1614       else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
1615         {
1616           STRIP_NOPS (arg);
1617           if (TREE_CODE (arg) == ADDR_EXPR)
1618             arg = TREE_OPERAND (arg, 0);
1619           if (TREE_CODE (arg) == VAR_DECL
1620               || TREE_CODE (arg) == FUNCTION_DECL)
1621             {
1622               if (! TREE_PUBLIC (arg))
1623                 vis = VISIBILITY_ANON;
1624               else
1625                 vis = DECL_VISIBILITY (arg);
1626             }
1627         }
1628       if (vis)
1629         constrain_visibility (decl, vis);
1630     }
1631 }
1632
1633 /* Like c_determine_visibility, but with additional C++-specific
1634    behavior.
1635
1636    Function-scope entities can rely on the function's visibility because
1637    it is set in start_preparsed_function.
1638
1639    Class-scope entities cannot rely on the class's visibility until the end
1640    of the enclosing class definition.
1641
1642    Note that because namespaces have multiple independent definitions,
1643    namespace visibility is handled elsewhere using the #pragma visibility
1644    machinery rather than by decorating the namespace declaration.
1645
1646    The goal is for constraints from the type to give a diagnostic, and
1647    other constraints to be applied silently.  */
1648
1649 void
1650 determine_visibility (tree decl)
1651 {
1652   tree class_type = NULL_TREE;
1653   bool use_template;
1654
1655   /* Remember that all decls get VISIBILITY_DEFAULT when built.  */
1656
1657   /* Only relevant for names with external linkage.  */
1658   if (!TREE_PUBLIC (decl))
1659     return;
1660
1661   /* Cloned constructors and destructors get the same visibility as
1662      the underlying function.  That should be set up in
1663      maybe_clone_body.  */
1664   gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
1665
1666   if (TREE_CODE (decl) == TYPE_DECL)
1667     {
1668       if (CLASS_TYPE_P (TREE_TYPE (decl)))
1669         use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
1670       else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
1671         use_template = 1;
1672       else
1673         use_template = 0;
1674     }
1675   else if (DECL_LANG_SPECIFIC (decl))
1676     use_template = DECL_USE_TEMPLATE (decl);
1677   else
1678     use_template = 0;
1679
1680   /* Anything that is exported must have default visibility.  */
1681   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
1682       && lookup_attribute ("dllexport",
1683                            TREE_CODE (decl) == TYPE_DECL
1684                            ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
1685                            : DECL_ATTRIBUTES (decl)))
1686     {
1687       DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1688       DECL_VISIBILITY_SPECIFIED (decl) = 1;
1689     }
1690
1691   /* If DECL is a member of a class, visibility specifiers on the
1692      class can influence the visibility of the DECL.  */
1693   if (DECL_CLASS_SCOPE_P (decl))
1694     class_type = DECL_CONTEXT (decl);
1695   else if (TREE_CODE (decl) == VAR_DECL
1696            && DECL_TINFO_P (decl)
1697            && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl))))
1698     class_type = TREE_TYPE (DECL_NAME (decl));
1699   else
1700     {
1701       /* Not a class member.  */
1702
1703       /* Virtual tables have DECL_CONTEXT set to their associated class,
1704          so they are automatically handled above.  */
1705       gcc_assert (TREE_CODE (decl) != VAR_DECL
1706                   || !DECL_VTABLE_OR_VTT_P (decl));
1707
1708       if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
1709         {
1710           /* Local statics and classes get the visibility of their
1711              containing function by default, except that
1712              -fvisibility-inlines-hidden doesn't affect them.  */
1713           tree fn = DECL_CONTEXT (decl);
1714           if (DECL_VISIBILITY_SPECIFIED (fn) || ! DECL_CLASS_SCOPE_P (fn))
1715             {
1716               DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
1717               DECL_VISIBILITY_SPECIFIED (decl) = 
1718                 DECL_VISIBILITY_SPECIFIED (fn);
1719             }
1720           else
1721             determine_visibility_from_class (decl, DECL_CONTEXT (fn));
1722
1723           /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
1724              but have no TEMPLATE_INFO, so don't try to check it.  */
1725           use_template = 0;
1726         }
1727       else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
1728         {
1729           /* tinfo visibility is based on the type it's for.  */
1730           constrain_visibility
1731             (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))));
1732         }
1733       else if (use_template)
1734         /* Template instantiations and specializations get visibility based
1735            on their template unless they override it with an attribute.  */;
1736       else if (! DECL_VISIBILITY_SPECIFIED (decl))
1737         {
1738           /* Set default visibility to whatever the user supplied with
1739              #pragma GCC visibility or a namespace visibility attribute.  */
1740           DECL_VISIBILITY (decl) = default_visibility;
1741           DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
1742         }
1743     }
1744
1745   if (use_template)
1746     {
1747       /* If the specialization doesn't specify visibility, use the
1748          visibility from the template.  */
1749       tree tinfo = (TREE_CODE (decl) == TYPE_DECL
1750                     ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
1751                     : DECL_TEMPLATE_INFO (decl));
1752       tree args = TI_ARGS (tinfo);
1753       
1754       if (args != error_mark_node)
1755         {
1756           int depth = TMPL_ARGS_DEPTH (args);
1757           tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
1758
1759           if (!DECL_VISIBILITY_SPECIFIED (decl))
1760             {
1761               DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
1762               DECL_VISIBILITY_SPECIFIED (decl)
1763                 = DECL_VISIBILITY_SPECIFIED (pattern);
1764             }
1765
1766           /* FIXME should TMPL_ARGS_DEPTH really return 1 for null input? */
1767           if (args && depth > template_class_depth (class_type))
1768             /* Limit visibility based on its template arguments.  */
1769             constrain_visibility_for_template (decl, args);
1770         }
1771     }
1772
1773   if (class_type)
1774     determine_visibility_from_class (decl, class_type);
1775
1776   if (decl_anon_ns_mem_p (decl))
1777     /* Names in an anonymous namespace get internal linkage.
1778        This might change once we implement export.  */
1779     constrain_visibility (decl, VISIBILITY_ANON);
1780   else if (TREE_CODE (decl) != TYPE_DECL)
1781     {
1782       /* Propagate anonymity from type to decl.  */
1783       int tvis = type_visibility (TREE_TYPE (decl));
1784       if (tvis == VISIBILITY_ANON)
1785         constrain_visibility (decl, tvis);
1786     }
1787 }
1788
1789 /* By default, static data members and function members receive
1790    the visibility of their containing class.  */
1791
1792 static void
1793 determine_visibility_from_class (tree decl, tree class_type)
1794 {
1795   if (visibility_options.inlines_hidden
1796       /* Don't do this for inline templates; specializations might not be
1797          inline, and we don't want them to inherit the hidden
1798          visibility.  We'll set it here for all inline instantiations.  */
1799       && !processing_template_decl
1800       && ! DECL_VISIBILITY_SPECIFIED (decl)
1801       && TREE_CODE (decl) == FUNCTION_DECL
1802       && DECL_DECLARED_INLINE_P (decl)
1803       && (! DECL_LANG_SPECIFIC (decl)
1804           || ! DECL_EXPLICIT_INSTANTIATION (decl)))
1805     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
1806   else if (!DECL_VISIBILITY_SPECIFIED (decl))
1807     {
1808       /* Default to the class visibility.  */
1809       DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
1810       DECL_VISIBILITY_SPECIFIED (decl)
1811         = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
1812     }
1813
1814   /* Give the target a chance to override the visibility associated
1815      with DECL.  */
1816   if (TREE_CODE (decl) == VAR_DECL
1817       && (DECL_TINFO_P (decl)
1818           || (DECL_VTABLE_OR_VTT_P (decl)
1819               /* Construction virtual tables are not exported because
1820                  they cannot be referred to from other object files;
1821                  their name is not standardized by the ABI.  */
1822               && !DECL_CONSTRUCTION_VTABLE_P (decl)))
1823       && TREE_PUBLIC (decl)
1824       && !DECL_REALLY_EXTERN (decl)
1825       && !DECL_VISIBILITY_SPECIFIED (decl)
1826       && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
1827     targetm.cxx.determine_class_data_visibility (decl);
1828 }
1829
1830 /* Constrain the visibility of a class TYPE based on the visibility of its
1831    field types.  Warn if any fields require lesser visibility.  */
1832
1833 void
1834 constrain_class_visibility (tree type)
1835 {
1836   tree binfo;
1837   tree t;
1838   int i;
1839
1840   int vis = type_visibility (type);
1841
1842   if (vis == VISIBILITY_ANON
1843       || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
1844     return;
1845
1846   /* Don't warn about visibility if the class has explicit visibility.  */
1847   if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
1848     vis = VISIBILITY_INTERNAL;
1849
1850   for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
1851     if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
1852       {
1853         tree ftype = strip_array_types (TREE_TYPE (t));
1854         int subvis = type_visibility (ftype);
1855
1856         if (subvis == VISIBILITY_ANON)
1857           warning (0, "\
1858 %qT has a field %qD whose type uses the anonymous namespace",
1859                    type, t);
1860         else if (IS_AGGR_TYPE (ftype)
1861                  && vis < VISIBILITY_HIDDEN
1862                  && subvis >= VISIBILITY_HIDDEN)
1863           warning (OPT_Wattributes, "\
1864 %qT declared with greater visibility than the type of its field %qD",
1865                    type, t);
1866       }
1867
1868   binfo = TYPE_BINFO (type);
1869   for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
1870     {
1871       int subvis = type_visibility (TREE_TYPE (t));
1872
1873       if (subvis == VISIBILITY_ANON)
1874         warning (0, "\
1875 %qT has a base %qT whose type uses the anonymous namespace",
1876                  type, TREE_TYPE (t));
1877       else if (vis < VISIBILITY_HIDDEN
1878                && subvis >= VISIBILITY_HIDDEN)
1879         warning (OPT_Wattributes, "\
1880 %qT declared with greater visibility than its base %qT",
1881                  type, TREE_TYPE (t));
1882     }
1883 }
1884
1885 /* DECL is a FUNCTION_DECL or VAR_DECL.  If the object file linkage
1886    for DECL has not already been determined, do so now by setting
1887    DECL_EXTERNAL, DECL_COMDAT and other related flags.  Until this
1888    function is called entities with vague linkage whose definitions
1889    are available must have TREE_PUBLIC set.
1890
1891    If this function decides to place DECL in COMDAT, it will set
1892    appropriate flags -- but will not clear DECL_EXTERNAL.  It is up to
1893    the caller to decide whether or not to clear DECL_EXTERNAL.  Some
1894    callers defer that decision until it is clear that DECL is actually
1895    required.  */
1896
1897 void
1898 import_export_decl (tree decl)
1899 {
1900   int emit_p;
1901   bool comdat_p;
1902   bool import_p;
1903   tree class_type = NULL_TREE;
1904
1905   if (DECL_INTERFACE_KNOWN (decl))
1906     return;
1907
1908   /* We cannot determine what linkage to give to an entity with vague
1909      linkage until the end of the file.  For example, a virtual table
1910      for a class will be defined if and only if the key method is
1911      defined in this translation unit.  As a further example, consider
1912      that when compiling a translation unit that uses PCH file with
1913      "-frepo" it would be incorrect to make decisions about what
1914      entities to emit when building the PCH; those decisions must be
1915      delayed until the repository information has been processed.  */
1916   gcc_assert (at_eof);
1917   /* Object file linkage for explicit instantiations is handled in
1918      mark_decl_instantiated.  For static variables in functions with
1919      vague linkage, maybe_commonize_var is used.
1920
1921      Therefore, the only declarations that should be provided to this
1922      function are those with external linkage that are:
1923
1924      * implicit instantiations of function templates
1925
1926      * inline function
1927
1928      * implicit instantiations of static data members of class
1929        templates
1930
1931      * virtual tables
1932
1933      * typeinfo objects
1934
1935      Furthermore, all entities that reach this point must have a
1936      definition available in this translation unit.
1937
1938      The following assertions check these conditions.  */
1939   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1940               || TREE_CODE (decl) == VAR_DECL);
1941   /* Any code that creates entities with TREE_PUBLIC cleared should
1942      also set DECL_INTERFACE_KNOWN.  */
1943   gcc_assert (TREE_PUBLIC (decl));
1944   if (TREE_CODE (decl) == FUNCTION_DECL)
1945     gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
1946                 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
1947                 || DECL_DECLARED_INLINE_P (decl));
1948   else
1949     gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
1950                 || DECL_VTABLE_OR_VTT_P (decl)
1951                 || DECL_TINFO_P (decl));
1952   /* Check that a definition of DECL is available in this translation
1953      unit.  */
1954   gcc_assert (!DECL_REALLY_EXTERN (decl));
1955
1956   /* Assume that DECL will not have COMDAT linkage.  */
1957   comdat_p = false;
1958   /* Assume that DECL will not be imported into this translation
1959      unit.  */
1960   import_p = false;
1961
1962   /* See if the repository tells us whether or not to emit DECL in
1963      this translation unit.  */
1964   emit_p = repo_emit_p (decl);
1965   if (emit_p == 0)
1966     import_p = true;
1967   else if (emit_p == 1)
1968     {
1969       /* The repository indicates that this entity should be defined
1970          here.  Make sure the back end honors that request.  */
1971       if (TREE_CODE (decl) == VAR_DECL)
1972         mark_needed (decl);
1973       else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
1974                || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
1975         {
1976           tree clone;
1977           FOR_EACH_CLONE (clone, decl)
1978             mark_needed (clone);
1979         }
1980       else
1981         mark_needed (decl);
1982       /* Output the definition as an ordinary strong definition.  */
1983       DECL_EXTERNAL (decl) = 0;
1984       DECL_INTERFACE_KNOWN (decl) = 1;
1985       return;
1986     }
1987
1988   if (import_p)
1989     /* We have already decided what to do with this DECL; there is no
1990        need to check anything further.  */
1991     ;
1992   else if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
1993     {
1994       class_type = DECL_CONTEXT (decl);
1995       import_export_class (class_type);
1996       if (TYPE_FOR_JAVA (class_type))
1997         import_p = true;
1998       else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
1999                && CLASSTYPE_INTERFACE_ONLY (class_type))
2000         import_p = true;
2001       else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2002                && !CLASSTYPE_USE_TEMPLATE (class_type)
2003                && CLASSTYPE_KEY_METHOD (class_type)
2004                && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2005         /* The ABI requires that all virtual tables be emitted with
2006            COMDAT linkage.  However, on systems where COMDAT symbols
2007            don't show up in the table of contents for a static
2008            archive, or on systems without weak symbols (where we
2009            approximate COMDAT linkage by using internal linkage), the
2010            linker will report errors about undefined symbols because
2011            it will not see the virtual table definition.  Therefore,
2012            in the case that we know that the virtual table will be
2013            emitted in only one translation unit, we make the virtual
2014            table an ordinary definition with external linkage.  */
2015         DECL_EXTERNAL (decl) = 0;
2016       else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2017         {
2018           /* CLASS_TYPE is being exported from this translation unit,
2019              so DECL should be defined here.  */
2020           if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2021             /* If a class is declared in a header with the "extern
2022                template" extension, then it will not be instantiated,
2023                even in translation units that would normally require
2024                it.  Often such classes are explicitly instantiated in
2025                one translation unit.  Therefore, the explicit
2026                instantiation must be made visible to other translation
2027                units.  */
2028             DECL_EXTERNAL (decl) = 0;
2029           else
2030             {
2031               /* The generic C++ ABI says that class data is always
2032                  COMDAT, even if there is a key function.  Some
2033                  variants (e.g., the ARM EABI) says that class data
2034                  only has COMDAT linkage if the class data might be
2035                  emitted in more than one translation unit.  When the
2036                  key method can be inline and is inline, we still have
2037                  to arrange for comdat even though
2038                  class_data_always_comdat is false.  */
2039               if (!CLASSTYPE_KEY_METHOD (class_type)
2040                   || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2041                   || targetm.cxx.class_data_always_comdat ())
2042                 {
2043                   /* The ABI requires COMDAT linkage.  Normally, we
2044                      only emit COMDAT things when they are needed;
2045                      make sure that we realize that this entity is
2046                      indeed needed.  */
2047                   comdat_p = true;
2048                   mark_needed (decl);
2049                 }
2050             }
2051         }
2052       else if (!flag_implicit_templates
2053                && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2054         import_p = true;
2055       else
2056         comdat_p = true;
2057     }
2058   else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
2059     {
2060       tree type = TREE_TYPE (DECL_NAME (decl));
2061       if (CLASS_TYPE_P (type))
2062         {
2063           class_type = type;
2064           import_export_class (type);
2065           if (CLASSTYPE_INTERFACE_KNOWN (type)
2066               && TYPE_POLYMORPHIC_P (type)
2067               && CLASSTYPE_INTERFACE_ONLY (type)
2068               /* If -fno-rtti was specified, then we cannot be sure
2069                  that RTTI information will be emitted with the
2070                  virtual table of the class, so we must emit it
2071                  wherever it is used.  */
2072               && flag_rtti)
2073             import_p = true;
2074           else
2075             {
2076               if (CLASSTYPE_INTERFACE_KNOWN (type)
2077                   && !CLASSTYPE_INTERFACE_ONLY (type))
2078                 {
2079                   comdat_p = (targetm.cxx.class_data_always_comdat ()
2080                               || (CLASSTYPE_KEY_METHOD (type)
2081                                   && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2082                   mark_needed (decl);
2083                   if (!flag_weak)
2084                     {
2085                       comdat_p = false;
2086                       DECL_EXTERNAL (decl) = 0;
2087                     }
2088                 }
2089               else
2090                 comdat_p = true;
2091             }
2092         }
2093       else
2094         comdat_p = true;
2095     }
2096   else if (DECL_TEMPLATE_INSTANTIATION (decl)
2097            || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2098     {
2099       /* DECL is an implicit instantiation of a function or static
2100          data member.  */
2101       if (flag_implicit_templates
2102           || (flag_implicit_inline_templates
2103               && TREE_CODE (decl) == FUNCTION_DECL
2104               && DECL_DECLARED_INLINE_P (decl)))
2105         comdat_p = true;
2106       else
2107         /* If we are not implicitly generating templates, then mark
2108            this entity as undefined in this translation unit.  */
2109         import_p = true;
2110     }
2111   else if (DECL_FUNCTION_MEMBER_P (decl))
2112     {
2113       if (!DECL_DECLARED_INLINE_P (decl))
2114         {
2115           tree ctype = DECL_CONTEXT (decl);
2116           import_export_class (ctype);
2117           if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2118             {
2119               DECL_NOT_REALLY_EXTERN (decl)
2120                 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2121                      || (DECL_DECLARED_INLINE_P (decl)
2122                          && ! flag_implement_inlines
2123                          && !DECL_VINDEX (decl)));
2124
2125               if (!DECL_NOT_REALLY_EXTERN (decl))
2126                 DECL_EXTERNAL (decl) = 1;
2127
2128               /* Always make artificials weak.  */
2129               if (DECL_ARTIFICIAL (decl) && flag_weak)
2130                 comdat_p = true;
2131               else
2132                 maybe_make_one_only (decl);
2133             }
2134         }
2135       else
2136         comdat_p = true;
2137     }
2138   else
2139     comdat_p = true;
2140
2141   if (import_p)
2142     {
2143       /* If we are importing DECL into this translation unit, mark is
2144          an undefined here.  */
2145       DECL_EXTERNAL (decl) = 1;
2146       DECL_NOT_REALLY_EXTERN (decl) = 0;
2147     }
2148   else if (comdat_p)
2149     {
2150       /* If we decided to put DECL in COMDAT, mark it accordingly at
2151          this point.  */
2152       comdat_linkage (decl);
2153     }
2154
2155   DECL_INTERFACE_KNOWN (decl) = 1;
2156 }
2157
2158 /* Return an expression that performs the destruction of DECL, which
2159    must be a VAR_DECL whose type has a non-trivial destructor, or is
2160    an array whose (innermost) elements have a non-trivial destructor.  */
2161
2162 tree
2163 build_cleanup (tree decl)
2164 {
2165   tree temp;
2166   tree type = TREE_TYPE (decl);
2167
2168   /* This function should only be called for declarations that really
2169      require cleanups.  */
2170   gcc_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type));
2171
2172   /* Treat all objects with destructors as used; the destructor may do
2173      something substantive.  */
2174   mark_used (decl);
2175
2176   if (TREE_CODE (type) == ARRAY_TYPE)
2177     temp = decl;
2178   else
2179     {
2180       cxx_mark_addressable (decl);
2181       temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2182     }
2183   temp = build_delete (TREE_TYPE (temp), temp,
2184                        sfk_complete_destructor,
2185                        LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2186   return temp;
2187 }
2188
2189 /* Returns the initialization guard variable for the variable DECL,
2190    which has static storage duration.  */
2191
2192 tree
2193 get_guard (tree decl)
2194 {
2195   tree sname;
2196   tree guard;
2197
2198   sname = mangle_guard_variable (decl);
2199   guard = IDENTIFIER_GLOBAL_VALUE (sname);
2200   if (! guard)
2201     {
2202       tree guard_type;
2203
2204       /* We use a type that is big enough to contain a mutex as well
2205          as an integer counter.  */
2206       guard_type = targetm.cxx.guard_type ();
2207       guard = build_decl (VAR_DECL, sname, guard_type);
2208
2209       /* The guard should have the same linkage as what it guards.  */
2210       TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2211       TREE_STATIC (guard) = TREE_STATIC (decl);
2212       DECL_COMMON (guard) = DECL_COMMON (decl);
2213       DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
2214       if (TREE_PUBLIC (decl))
2215         DECL_WEAK (guard) = DECL_WEAK (decl);
2216
2217       DECL_ARTIFICIAL (guard) = 1;
2218       DECL_IGNORED_P (guard) = 1;
2219       TREE_USED (guard) = 1;
2220       pushdecl_top_level_and_finish (guard, NULL_TREE);
2221     }
2222   return guard;
2223 }
2224
2225 /* Return those bits of the GUARD variable that should be set when the
2226    guarded entity is actually initialized.  */
2227
2228 static tree
2229 get_guard_bits (tree guard)
2230 {
2231   if (!targetm.cxx.guard_mask_bit ())
2232     {
2233       /* We only set the first byte of the guard, in order to leave room
2234          for a mutex in the high-order bits.  */
2235       guard = build1 (ADDR_EXPR,
2236                       build_pointer_type (TREE_TYPE (guard)),
2237                       guard);
2238       guard = build1 (NOP_EXPR,
2239                       build_pointer_type (char_type_node),
2240                       guard);
2241       guard = build1 (INDIRECT_REF, char_type_node, guard);
2242     }
2243
2244   return guard;
2245 }
2246
2247 /* Return an expression which determines whether or not the GUARD
2248    variable has already been initialized.  */
2249
2250 tree
2251 get_guard_cond (tree guard)
2252 {
2253   tree guard_value;
2254
2255   /* Check to see if the GUARD is zero.  */
2256   guard = get_guard_bits (guard);
2257
2258   /* Mask off all but the low bit.  */
2259   if (targetm.cxx.guard_mask_bit ())
2260     {
2261       guard_value = integer_one_node;
2262       if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2263         guard_value = convert (TREE_TYPE (guard), guard_value);
2264         guard = cp_build_binary_op (BIT_AND_EXPR, guard, guard_value);
2265     }
2266
2267   guard_value = integer_zero_node;
2268   if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2269     guard_value = convert (TREE_TYPE (guard), guard_value);
2270   return cp_build_binary_op (EQ_EXPR, guard, guard_value);
2271 }
2272
2273 /* Return an expression which sets the GUARD variable, indicating that
2274    the variable being guarded has been initialized.  */
2275
2276 tree
2277 set_guard (tree guard)
2278 {
2279   tree guard_init;
2280
2281   /* Set the GUARD to one.  */
2282   guard = get_guard_bits (guard);
2283   guard_init = integer_one_node;
2284   if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2285     guard_init = convert (TREE_TYPE (guard), guard_init);
2286   return build_modify_expr (guard, NOP_EXPR, guard_init);
2287 }
2288
2289 /* Start the process of running a particular set of global constructors
2290    or destructors.  Subroutine of do_[cd]tors.  */
2291
2292 static tree
2293 start_objects (int method_type, int initp)
2294 {
2295   tree body;
2296   tree fndecl;
2297   char type[10];
2298
2299   /* Make ctor or dtor function.  METHOD_TYPE may be 'I' or 'D'.  */
2300
2301   if (initp != DEFAULT_INIT_PRIORITY)
2302     {
2303       char joiner;
2304
2305 #ifdef JOINER
2306       joiner = JOINER;
2307 #else
2308       joiner = '_';
2309 #endif
2310
2311       sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2312     }
2313   else
2314     sprintf (type, "%c", method_type);
2315
2316   fndecl = build_lang_decl (FUNCTION_DECL,
2317                             get_file_function_name (type),
2318                             build_function_type (void_type_node,
2319                                                  void_list_node));
2320   start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
2321
2322   /* It can be a static function as long as collect2 does not have
2323      to scan the object file to find its ctor/dtor routine.  */
2324   TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors;
2325
2326   /* Mark this declaration as used to avoid spurious warnings.  */
2327   TREE_USED (current_function_decl) = 1;
2328
2329   /* Mark this function as a global constructor or destructor.  */
2330   if (method_type == 'I')
2331     DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2332   else
2333     DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2334   DECL_LANG_SPECIFIC (current_function_decl)->decl_flags.u2sel = 1;
2335
2336   body = begin_compound_stmt (BCS_FN_BODY);
2337
2338   return body;
2339 }
2340
2341 /* Finish the process of running a particular set of global constructors
2342    or destructors.  Subroutine of do_[cd]tors.  */
2343
2344 static void
2345 finish_objects (int method_type, int initp, tree body)
2346 {
2347   tree fn;
2348
2349   /* Finish up.  */
2350   finish_compound_stmt (body);
2351   fn = finish_function (0);
2352   expand_or_defer_fn (fn);
2353
2354   /* When only doing semantic analysis, and no RTL generation, we
2355      can't call functions that directly emit assembly code; there is
2356      no assembly file in which to put the code.  */
2357   if (flag_syntax_only)
2358     return;
2359
2360   if (targetm.have_ctors_dtors)
2361     {
2362       rtx fnsym = XEXP (DECL_RTL (fn), 0);
2363       cgraph_mark_needed_node (cgraph_node (fn));
2364       if (method_type == 'I')
2365         (* targetm.asm_out.constructor) (fnsym, initp);
2366       else
2367         (* targetm.asm_out.destructor) (fnsym, initp);
2368     }
2369 }
2370
2371 /* The names of the parameters to the function created to handle
2372    initializations and destructions for objects with static storage
2373    duration.  */
2374 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2375 #define PRIORITY_IDENTIFIER "__priority"
2376
2377 /* The name of the function we create to handle initializations and
2378    destructions for objects with static storage duration.  */
2379 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2380
2381 /* The declaration for the __INITIALIZE_P argument.  */
2382 static GTY(()) tree initialize_p_decl;
2383
2384 /* The declaration for the __PRIORITY argument.  */
2385 static GTY(()) tree priority_decl;
2386
2387 /* The declaration for the static storage duration function.  */
2388 static GTY(()) tree ssdf_decl;
2389
2390 /* All the static storage duration functions created in this
2391    translation unit.  */
2392 static GTY(()) VEC(tree,gc) *ssdf_decls;
2393
2394 /* A map from priority levels to information about that priority
2395    level.  There may be many such levels, so efficient lookup is
2396    important.  */
2397 static splay_tree priority_info_map;
2398
2399 /* Begins the generation of the function that will handle all
2400    initialization and destruction of objects with static storage
2401    duration.  The function generated takes two parameters of type
2402    `int': __INITIALIZE_P and __PRIORITY.  If __INITIALIZE_P is
2403    nonzero, it performs initializations.  Otherwise, it performs
2404    destructions.  It only performs those initializations or
2405    destructions with the indicated __PRIORITY.  The generated function
2406    returns no value.
2407
2408    It is assumed that this function will only be called once per
2409    translation unit.  */
2410
2411 static tree
2412 start_static_storage_duration_function (unsigned count)
2413 {
2414   tree parm_types;
2415   tree type;
2416   tree body;
2417   char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2418
2419   /* Create the identifier for this function.  It will be of the form
2420      SSDF_IDENTIFIER_<number>.  */
2421   sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
2422
2423   /* Create the parameters.  */
2424   parm_types = void_list_node;
2425   parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2426   parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2427   type = build_function_type (void_type_node, parm_types);
2428
2429   /* Create the FUNCTION_DECL itself.  */
2430   ssdf_decl = build_lang_decl (FUNCTION_DECL,
2431                                get_identifier (id),
2432                                type);
2433   TREE_PUBLIC (ssdf_decl) = 0;
2434   DECL_ARTIFICIAL (ssdf_decl) = 1;
2435   DECL_INLINE (ssdf_decl) = 1;
2436
2437   /* Put this function in the list of functions to be called from the
2438      static constructors and destructors.  */
2439   if (!ssdf_decls)
2440     {
2441       ssdf_decls = VEC_alloc (tree, gc, 32);
2442
2443       /* Take this opportunity to initialize the map from priority
2444          numbers to information about that priority level.  */
2445       priority_info_map = splay_tree_new (splay_tree_compare_ints,
2446                                           /*delete_key_fn=*/0,
2447                                           /*delete_value_fn=*/
2448                                           (splay_tree_delete_value_fn) &free);
2449
2450       /* We always need to generate functions for the
2451          DEFAULT_INIT_PRIORITY so enter it now.  That way when we walk
2452          priorities later, we'll be sure to find the
2453          DEFAULT_INIT_PRIORITY.  */
2454       get_priority_info (DEFAULT_INIT_PRIORITY);
2455     }
2456
2457   VEC_safe_push (tree, gc, ssdf_decls, ssdf_decl);
2458
2459   /* Create the argument list.  */
2460   initialize_p_decl = cp_build_parm_decl
2461     (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
2462   DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2463   TREE_USED (initialize_p_decl) = 1;
2464   priority_decl = cp_build_parm_decl
2465     (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
2466   DECL_CONTEXT (priority_decl) = ssdf_decl;
2467   TREE_USED (priority_decl) = 1;
2468
2469   TREE_CHAIN (initialize_p_decl) = priority_decl;
2470   DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2471
2472   /* Put the function in the global scope.  */
2473   pushdecl (ssdf_decl);
2474
2475   /* Start the function itself.  This is equivalent to declaring the
2476      function as:
2477
2478        static void __ssdf (int __initialize_p, init __priority_p);
2479
2480      It is static because we only need to call this function from the
2481      various constructor and destructor functions for this module.  */
2482   start_preparsed_function (ssdf_decl,
2483                             /*attrs=*/NULL_TREE,
2484                             SF_PRE_PARSED);
2485
2486   /* Set up the scope of the outermost block in the function.  */
2487   body = begin_compound_stmt (BCS_FN_BODY);
2488
2489   return body;
2490 }
2491
2492 /* Finish the generation of the function which performs initialization
2493    and destruction of objects with static storage duration.  After
2494    this point, no more such objects can be created.  */
2495
2496 static void
2497 finish_static_storage_duration_function (tree body)
2498 {
2499   /* Close out the function.  */
2500   finish_compound_stmt (body);
2501   expand_or_defer_fn (finish_function (0));
2502 }
2503
2504 /* Return the information about the indicated PRIORITY level.  If no
2505    code to handle this level has yet been generated, generate the
2506    appropriate prologue.  */
2507
2508 static priority_info
2509 get_priority_info (int priority)
2510 {
2511   priority_info pi;
2512   splay_tree_node n;
2513
2514   n = splay_tree_lookup (priority_info_map,
2515                          (splay_tree_key) priority);
2516   if (!n)
2517     {
2518       /* Create a new priority information structure, and insert it
2519          into the map.  */
2520       pi = XNEW (struct priority_info_s);
2521       pi->initializations_p = 0;
2522       pi->destructions_p = 0;
2523       splay_tree_insert (priority_info_map,
2524                          (splay_tree_key) priority,
2525                          (splay_tree_value) pi);
2526     }
2527   else
2528     pi = (priority_info) n->value;
2529
2530   return pi;
2531 }
2532
2533 /* The effective initialization priority of a DECL.  */
2534
2535 #define DECL_EFFECTIVE_INIT_PRIORITY(decl)                                    \
2536         ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
2537          ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
2538
2539 /* Whether a DECL needs a guard to protect it against multiple
2540    initialization.  */
2541
2542 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl)      \
2543                                                     || DECL_ONE_ONLY (decl) \
2544                                                     || DECL_WEAK (decl)))
2545
2546 /* Set up to handle the initialization or destruction of DECL.  If
2547    INITP is nonzero, we are initializing the variable.  Otherwise, we
2548    are destroying it.  */
2549
2550 static void
2551 one_static_initialization_or_destruction (tree decl, tree init, bool initp)
2552 {
2553   tree guard_if_stmt = NULL_TREE;
2554   tree guard;
2555
2556   /* If we are supposed to destruct and there's a trivial destructor,
2557      nothing has to be done.  */
2558   if (!initp
2559       && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2560     return;
2561
2562   /* Trick the compiler into thinking we are at the file and line
2563      where DECL was declared so that error-messages make sense, and so
2564      that the debugger will show somewhat sensible file and line
2565      information.  */
2566   input_location = DECL_SOURCE_LOCATION (decl);
2567
2568   /* Because of:
2569
2570        [class.access.spec]
2571
2572        Access control for implicit calls to the constructors,
2573        the conversion functions, or the destructor called to
2574        create and destroy a static data member is performed as
2575        if these calls appeared in the scope of the member's
2576        class.
2577
2578      we pretend we are in a static member function of the class of
2579      which the DECL is a member.  */
2580   if (member_p (decl))
2581     {
2582       DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
2583       DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
2584     }
2585
2586   /* Assume we don't need a guard.  */
2587   guard = NULL_TREE;
2588   /* We need a guard if this is an object with external linkage that
2589      might be initialized in more than one place.  (For example, a
2590      static data member of a template, when the data member requires
2591      construction.)  */
2592   if (NEEDS_GUARD_P (decl))
2593     {
2594       tree guard_cond;
2595
2596       guard = get_guard (decl);
2597
2598       /* When using __cxa_atexit, we just check the GUARD as we would
2599          for a local static.  */
2600       if (flag_use_cxa_atexit)
2601         {
2602           /* When using __cxa_atexit, we never try to destroy
2603              anything from a static destructor.  */
2604           gcc_assert (initp);
2605           guard_cond = get_guard_cond (guard);
2606         }
2607       /* If we don't have __cxa_atexit, then we will be running
2608          destructors from .fini sections, or their equivalents.  So,
2609          we need to know how many times we've tried to initialize this
2610          object.  We do initializations only if the GUARD is zero,
2611          i.e., if we are the first to initialize the variable.  We do
2612          destructions only if the GUARD is one, i.e., if we are the
2613          last to destroy the variable.  */
2614       else if (initp)
2615         guard_cond
2616           = cp_build_binary_op (EQ_EXPR,
2617                                 build_unary_op (PREINCREMENT_EXPR,
2618                                                 guard,
2619                                                 /*noconvert=*/1),
2620                                 integer_one_node);
2621       else
2622         guard_cond
2623           = cp_build_binary_op (EQ_EXPR,
2624                                 build_unary_op (PREDECREMENT_EXPR,
2625                                                 guard,
2626                                                 /*noconvert=*/1),
2627                                 integer_zero_node);
2628
2629       guard_if_stmt = begin_if_stmt ();
2630       finish_if_stmt_cond (guard_cond, guard_if_stmt);
2631     }
2632
2633
2634   /* If we're using __cxa_atexit, we have not already set the GUARD,
2635      so we must do so now.  */
2636   if (guard && initp && flag_use_cxa_atexit)
2637     finish_expr_stmt (set_guard (guard));
2638
2639   /* Perform the initialization or destruction.  */
2640   if (initp)
2641     {
2642       if (init)
2643         finish_expr_stmt (init);
2644
2645       /* If we're using __cxa_atexit, register a function that calls the
2646          destructor for the object.  */
2647       if (flag_use_cxa_atexit)
2648         finish_expr_stmt (register_dtor_fn (decl));
2649     }
2650   else
2651     finish_expr_stmt (build_cleanup (decl));
2652
2653   /* Finish the guard if-stmt, if necessary.  */
2654   if (guard)
2655     {
2656       finish_then_clause (guard_if_stmt);
2657       finish_if_stmt (guard_if_stmt);
2658     }
2659
2660   /* Now that we're done with DECL we don't need to pretend to be a
2661      member of its class any longer.  */
2662   DECL_CONTEXT (current_function_decl) = NULL_TREE;
2663   DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
2664 }
2665
2666 /* Generate code to do the initialization or destruction of the decls in VARS,
2667    a TREE_LIST of VAR_DECL with static storage duration.
2668    Whether initialization or destruction is performed is specified by INITP.  */
2669
2670 static void
2671 do_static_initialization_or_destruction (tree vars, bool initp)
2672 {
2673   tree node, init_if_stmt, cond;
2674
2675   /* Build the outer if-stmt to check for initialization or destruction.  */
2676   init_if_stmt = begin_if_stmt ();
2677   cond = initp ? integer_one_node : integer_zero_node;
2678   cond = cp_build_binary_op (EQ_EXPR,
2679                                   initialize_p_decl,
2680                                   cond);
2681   finish_if_stmt_cond (cond, init_if_stmt);
2682
2683   node = vars;
2684   do {
2685     tree decl = TREE_VALUE (node);
2686     tree priority_if_stmt;
2687     int priority;
2688     priority_info pi;
2689
2690     /* If we don't need a destructor, there's nothing to do.  Avoid
2691        creating a possibly empty if-stmt.  */
2692     if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2693       {
2694         node = TREE_CHAIN (node);
2695         continue;
2696       }
2697
2698     /* Remember that we had an initialization or finalization at this
2699        priority.  */
2700     priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
2701     pi = get_priority_info (priority);
2702     if (initp)
2703       pi->initializations_p = 1;
2704     else
2705       pi->destructions_p = 1;
2706
2707     /* Conditionalize this initialization on being in the right priority
2708        and being initializing/finalizing appropriately.  */
2709     priority_if_stmt = begin_if_stmt ();
2710     cond = cp_build_binary_op (EQ_EXPR,
2711                                priority_decl,
2712                                build_int_cst (NULL_TREE, priority));
2713     finish_if_stmt_cond (cond, priority_if_stmt);
2714
2715     /* Process initializers with same priority.  */
2716     for (; node
2717            && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
2718          node = TREE_CHAIN (node))
2719       /* Do one initialization or destruction.  */
2720       one_static_initialization_or_destruction (TREE_VALUE (node),
2721                                                 TREE_PURPOSE (node), initp);
2722
2723     /* Finish up the priority if-stmt body.  */
2724     finish_then_clause (priority_if_stmt);
2725     finish_if_stmt (priority_if_stmt);
2726
2727   } while (node);
2728
2729   /* Finish up the init/destruct if-stmt body.  */
2730   finish_then_clause (init_if_stmt);
2731   finish_if_stmt (init_if_stmt);
2732 }
2733
2734 /* VARS is a list of variables with static storage duration which may
2735    need initialization and/or finalization.  Remove those variables
2736    that don't really need to be initialized or finalized, and return
2737    the resulting list.  The order in which the variables appear in
2738    VARS is in reverse order of the order in which they should actually
2739    be initialized.  The list we return is in the unreversed order;
2740    i.e., the first variable should be initialized first.  */
2741
2742 static tree
2743 prune_vars_needing_no_initialization (tree *vars)
2744 {
2745   tree *var = vars;
2746   tree result = NULL_TREE;
2747
2748   while (*var)
2749     {
2750       tree t = *var;
2751       tree decl = TREE_VALUE (t);
2752       tree init = TREE_PURPOSE (t);
2753
2754       /* Deal gracefully with error.  */
2755       if (decl == error_mark_node)
2756         {
2757           var = &TREE_CHAIN (t);
2758           continue;
2759         }
2760
2761       /* The only things that can be initialized are variables.  */
2762       gcc_assert (TREE_CODE (decl) == VAR_DECL);
2763
2764       /* If this object is not defined, we don't need to do anything
2765          here.  */
2766       if (DECL_EXTERNAL (decl))
2767         {
2768           var = &TREE_CHAIN (t);
2769           continue;
2770         }
2771
2772       /* Also, if the initializer already contains errors, we can bail
2773          out now.  */
2774       if (init && TREE_CODE (init) == TREE_LIST
2775           && value_member (error_mark_node, init))
2776         {
2777           var = &TREE_CHAIN (t);
2778           continue;
2779         }
2780
2781       /* This variable is going to need initialization and/or
2782          finalization, so we add it to the list.  */
2783       *var = TREE_CHAIN (t);
2784       TREE_CHAIN (t) = result;
2785       result = t;
2786     }
2787
2788   return result;
2789 }
2790
2791 /* Make sure we have told the back end about all the variables in
2792    VARS.  */
2793
2794 static void
2795 write_out_vars (tree vars)
2796 {
2797   tree v;
2798
2799   for (v = vars; v; v = TREE_CHAIN (v))
2800     {
2801       tree var = TREE_VALUE (v);
2802       if (!var_finalized_p (var))
2803         {
2804           import_export_decl (var);
2805           rest_of_decl_compilation (var, 1, 1);
2806         }
2807     }
2808 }
2809
2810 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
2811    (otherwise) that will initialize all global objects with static
2812    storage duration having the indicated PRIORITY.  */
2813
2814 static void
2815 generate_ctor_or_dtor_function (bool constructor_p, int priority,
2816                                 location_t *locus)
2817 {
2818   char function_key;
2819   tree arguments;
2820   tree fndecl;
2821   tree body;
2822   size_t i;
2823
2824   input_location = *locus;
2825 #ifdef USE_MAPPED_LOCATION
2826   /* ??? */
2827 #else
2828   locus->line++;
2829 #endif
2830
2831   /* We use `I' to indicate initialization and `D' to indicate
2832      destruction.  */
2833   function_key = constructor_p ? 'I' : 'D';
2834
2835   /* We emit the function lazily, to avoid generating empty
2836      global constructors and destructors.  */
2837   body = NULL_TREE;
2838
2839   /* For Objective-C++, we may need to initialize metadata found in this module.
2840      This must be done _before_ any other static initializations.  */
2841   if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
2842       && constructor_p && objc_static_init_needed_p ())
2843     {
2844       body = start_objects (function_key, priority);
2845       objc_generate_static_init_call (NULL_TREE);
2846     }
2847
2848   /* Call the static storage duration function with appropriate
2849      arguments.  */
2850   for (i = 0; VEC_iterate (tree, ssdf_decls, i, fndecl); ++i)
2851     {
2852       /* Calls to pure or const functions will expand to nothing.  */
2853       if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
2854         {
2855           if (! body)
2856             body = start_objects (function_key, priority);
2857
2858           arguments = tree_cons (NULL_TREE,
2859                                  build_int_cst (NULL_TREE, priority),
2860                                  NULL_TREE);
2861           arguments = tree_cons (NULL_TREE,
2862                                  build_int_cst (NULL_TREE, constructor_p),
2863                                  arguments);
2864           finish_expr_stmt (build_function_call (fndecl, arguments));
2865         }
2866     }
2867
2868   /* Close out the function.  */
2869   if (body)
2870     finish_objects (function_key, priority, body);
2871 }
2872
2873 /* Generate constructor and destructor functions for the priority
2874    indicated by N.  */
2875
2876 static int
2877 generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
2878 {
2879   location_t *locus = (location_t *) data;
2880   int priority = (int) n->key;
2881   priority_info pi = (priority_info) n->value;
2882
2883   /* Generate the functions themselves, but only if they are really
2884      needed.  */
2885   if (pi->initializations_p)
2886     generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
2887   if (pi->destructions_p)
2888     generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
2889
2890   /* Keep iterating.  */
2891   return 0;
2892 }
2893
2894 /* Called via LANGHOOK_CALLGRAPH_ANALYZE_EXPR.  It is supposed to mark
2895    decls referenced from front-end specific constructs; it will be called
2896    only for language-specific tree nodes.
2897
2898    Here we must deal with member pointers.  */
2899
2900 tree
2901 cxx_callgraph_analyze_expr (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2902                             tree from ATTRIBUTE_UNUSED)
2903 {
2904   tree t = *tp;
2905
2906   switch (TREE_CODE (t))
2907     {
2908     case PTRMEM_CST:
2909       if (TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2910         cgraph_mark_needed_node (cgraph_node (PTRMEM_CST_MEMBER (t)));
2911       break;
2912     case BASELINK:
2913       if (TREE_CODE (BASELINK_FUNCTIONS (t)) == FUNCTION_DECL)
2914         cgraph_mark_needed_node (cgraph_node (BASELINK_FUNCTIONS (t)));
2915       break;
2916     case VAR_DECL:
2917       if (DECL_VTABLE_OR_VTT_P (t))
2918         {
2919           /* The ABI requires that all virtual tables be emitted
2920              whenever one of them is.  */
2921           tree vtbl;
2922           for (vtbl = CLASSTYPE_VTABLES (DECL_CONTEXT (t));
2923                vtbl;
2924                vtbl = TREE_CHAIN (vtbl))
2925             mark_decl_referenced (vtbl);
2926         }
2927       else if (DECL_CONTEXT (t)
2928                && TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)
2929         /* If we need a static variable in a function, then we
2930            need the containing function.  */
2931         mark_decl_referenced (DECL_CONTEXT (t));
2932       break;
2933     default:
2934       break;
2935     }
2936
2937   return NULL;
2938 }
2939
2940 /* Java requires that we be able to reference a local address for a
2941    method, and not be confused by PLT entries.  If hidden aliases are
2942    supported, emit one for each java function that we've emitted.  */
2943
2944 static void
2945 build_java_method_aliases (void)
2946 {
2947   struct cgraph_node *node;
2948
2949 #ifndef HAVE_GAS_HIDDEN
2950   return;
2951 #endif
2952
2953   for (node = cgraph_nodes; node ; node = node->next)
2954     {
2955       tree fndecl = node->decl;
2956
2957       if (TREE_ASM_WRITTEN (fndecl)
2958           && DECL_CONTEXT (fndecl)
2959           && TYPE_P (DECL_CONTEXT (fndecl))
2960           && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
2961           && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
2962         {
2963           /* Mangle the name in a predictable way; we need to reference
2964              this from a java compiled object file.  */
2965           tree oid, nid, alias;
2966           const char *oname;
2967           char *nname;
2968
2969           oid = DECL_ASSEMBLER_NAME (fndecl);
2970           oname = IDENTIFIER_POINTER (oid);
2971           gcc_assert (oname[0] == '_' && oname[1] == 'Z');
2972           nname = ACONCAT (("_ZGA", oname+2, NULL));
2973           nid = get_identifier (nname);
2974
2975           alias = make_alias_for (fndecl, nid);
2976           TREE_PUBLIC (alias) = 1;
2977           DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
2978
2979           assemble_alias (alias, oid);
2980         }
2981     }
2982 }
2983
2984 /* This routine is called at the end of compilation.
2985    Its job is to create all the code needed to initialize and
2986    destroy the global aggregates.  We do the destruction
2987    first, since that way we only need to reverse the decls once.  */
2988
2989 void
2990 cp_write_global_declarations (void)
2991 {
2992   tree vars;
2993   bool reconsider;
2994   size_t i;
2995   location_t locus;
2996   unsigned ssdf_count = 0;
2997   int retries = 0;
2998   tree decl;
2999
3000   locus = input_location;
3001   at_eof = 1;
3002
3003   /* Bad parse errors.  Just forget about it.  */
3004   if (! global_bindings_p () || current_class_type || decl_namespace_list)
3005     return;
3006
3007   if (pch_file)
3008     c_common_write_pch ();
3009
3010 #ifdef USE_MAPPED_LOCATION
3011   /* FIXME - huh? */
3012 #else
3013   /* Otherwise, GDB can get confused, because in only knows
3014      about source for LINENO-1 lines.  */
3015   input_line -= 1;
3016 #endif
3017
3018   /* We now have to write out all the stuff we put off writing out.
3019      These include:
3020
3021        o Template specializations that we have not yet instantiated,
3022          but which are needed.
3023        o Initialization and destruction for non-local objects with
3024          static storage duration.  (Local objects with static storage
3025          duration are initialized when their scope is first entered,
3026          and are cleaned up via atexit.)
3027        o Virtual function tables.
3028
3029      All of these may cause others to be needed.  For example,
3030      instantiating one function may cause another to be needed, and
3031      generating the initializer for an object may cause templates to be
3032      instantiated, etc., etc.  */
3033
3034   timevar_push (TV_VARCONST);
3035
3036   emit_support_tinfos ();
3037
3038   do
3039     {
3040       tree t;
3041       tree decl;
3042
3043       reconsider = false;
3044
3045       /* If there are templates that we've put off instantiating, do
3046          them now.  */
3047       instantiate_pending_templates (retries);
3048       ggc_collect ();
3049
3050       /* Write out virtual tables as required.  Note that writing out
3051          the virtual table for a template class may cause the
3052          instantiation of members of that class.  If we write out
3053          vtables then we remove the class from our list so we don't
3054          have to look at it again.  */
3055
3056       while (keyed_classes != NULL_TREE
3057              && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
3058         {
3059           reconsider = true;
3060           keyed_classes = TREE_CHAIN (keyed_classes);
3061         }
3062
3063       t = keyed_classes;
3064       if (t != NULL_TREE)
3065         {
3066           tree next = TREE_CHAIN (t);
3067
3068           while (next)
3069             {
3070               if (maybe_emit_vtables (TREE_VALUE (next)))
3071                 {
3072                   reconsider = true;
3073                   TREE_CHAIN (t) = TREE_CHAIN (next);
3074                 }
3075               else
3076                 t = next;
3077
3078               next = TREE_CHAIN (t);
3079             }
3080         }
3081
3082       /* Write out needed type info variables.  We have to be careful
3083          looping through unemitted decls, because emit_tinfo_decl may
3084          cause other variables to be needed. New elements will be
3085          appended, and we remove from the vector those that actually
3086          get emitted.  */
3087       for (i = VEC_length (tree, unemitted_tinfo_decls);
3088            VEC_iterate (tree, unemitted_tinfo_decls, --i, t);)
3089         if (emit_tinfo_decl (t))
3090           {
3091             reconsider = true;
3092             VEC_unordered_remove (tree, unemitted_tinfo_decls, i);
3093           }
3094
3095       /* The list of objects with static storage duration is built up
3096          in reverse order.  We clear STATIC_AGGREGATES so that any new
3097          aggregates added during the initialization of these will be
3098          initialized in the correct order when we next come around the
3099          loop.  */
3100       vars = prune_vars_needing_no_initialization (&static_aggregates);
3101
3102       if (vars)
3103         {
3104           /* We need to start a new initialization function each time
3105              through the loop.  That's because we need to know which
3106              vtables have been referenced, and TREE_SYMBOL_REFERENCED
3107              isn't computed until a function is finished, and written
3108              out.  That's a deficiency in the back end.  When this is
3109              fixed, these initialization functions could all become
3110              inline, with resulting performance improvements.  */
3111           tree ssdf_body;
3112
3113           /* Set the line and file, so that it is obviously not from
3114              the source file.  */
3115           input_location = locus;
3116           ssdf_body = start_static_storage_duration_function (ssdf_count);
3117
3118           /* Make sure the back end knows about all the variables.  */
3119           write_out_vars (vars);
3120
3121           /* First generate code to do all the initializations.  */
3122           if (vars)
3123             do_static_initialization_or_destruction (vars, /*initp=*/true);
3124
3125           /* Then, generate code to do all the destructions.  Do these
3126              in reverse order so that the most recently constructed
3127              variable is the first destroyed.  If we're using
3128              __cxa_atexit, then we don't need to do this; functions
3129              were registered at initialization time to destroy the
3130              local statics.  */
3131           if (!flag_use_cxa_atexit && vars)
3132             {
3133               vars = nreverse (vars);
3134               do_static_initialization_or_destruction (vars, /*initp=*/false);
3135             }
3136           else
3137             vars = NULL_TREE;
3138
3139           /* Finish up the static storage duration function for this
3140              round.  */
3141           input_location = locus;
3142           finish_static_storage_duration_function (ssdf_body);
3143
3144           /* All those initializations and finalizations might cause
3145              us to need more inline functions, more template
3146              instantiations, etc.  */
3147           reconsider = true;
3148           ssdf_count++;
3149 #ifdef USE_MAPPED_LOCATION
3150           /* ??? */
3151 #else
3152           locus.line++;
3153 #endif
3154         }
3155
3156       /* Go through the set of inline functions whose bodies have not
3157          been emitted yet.  If out-of-line copies of these functions
3158          are required, emit them.  */
3159       for (i = 0; VEC_iterate (tree, deferred_fns, i, decl); ++i)
3160         {
3161           /* Does it need synthesizing?  */
3162           if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3163               && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3164             {
3165               /* Even though we're already at the top-level, we push
3166                  there again.  That way, when we pop back a few lines
3167                  hence, all of our state is restored.  Otherwise,
3168                  finish_function doesn't clean things up, and we end
3169                  up with CURRENT_FUNCTION_DECL set.  */
3170               push_to_top_level ();
3171               /* The decl's location will mark where it was first
3172                  needed.  Save that so synthesize method can indicate
3173                  where it was needed from, in case of error  */
3174               input_location = DECL_SOURCE_LOCATION (decl);
3175               synthesize_method (decl);
3176               pop_from_top_level ();
3177               reconsider = true;
3178             }
3179
3180           if (!DECL_SAVED_TREE (decl))
3181             continue;
3182
3183           /* We lie to the back end, pretending that some functions
3184              are not defined when they really are.  This keeps these
3185              functions from being put out unnecessarily.  But, we must
3186              stop lying when the functions are referenced, or if they
3187              are not comdat since they need to be put out now.  If
3188              DECL_INTERFACE_KNOWN, then we have already set
3189              DECL_EXTERNAL appropriately, so there's no need to check
3190              again, and we do not want to clear DECL_EXTERNAL if a
3191              previous call to import_export_decl set it.
3192
3193              This is done in a separate for cycle, because if some
3194              deferred function is contained in another deferred
3195              function later in deferred_fns varray,
3196              rest_of_compilation would skip this function and we
3197              really cannot expand the same function twice.  */
3198           import_export_decl (decl);
3199           if (DECL_NOT_REALLY_EXTERN (decl)
3200               && DECL_INITIAL (decl)
3201               && decl_needed_p (decl))
3202             DECL_EXTERNAL (decl) = 0;
3203
3204           /* If we're going to need to write this function out, and
3205              there's already a body for it, create RTL for it now.
3206              (There might be no body if this is a method we haven't
3207              gotten around to synthesizing yet.)  */
3208           if (!DECL_EXTERNAL (decl)
3209               && decl_needed_p (decl)
3210               && !TREE_ASM_WRITTEN (decl)
3211               && !cgraph_node (decl)->local.finalized)
3212             {
3213               /* We will output the function; no longer consider it in this
3214                  loop.  */
3215               DECL_DEFER_OUTPUT (decl) = 0;
3216               /* Generate RTL for this function now that we know we
3217                  need it.  */
3218               expand_or_defer_fn (decl);
3219               /* If we're compiling -fsyntax-only pretend that this
3220                  function has been written out so that we don't try to
3221                  expand it again.  */
3222               if (flag_syntax_only)
3223                 TREE_ASM_WRITTEN (decl) = 1;
3224               reconsider = true;
3225             }
3226         }
3227
3228       if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3229         reconsider = true;
3230
3231       /* Static data members are just like namespace-scope globals.  */
3232       for (i = 0; VEC_iterate (tree, pending_statics, i, decl); ++i)
3233         {
3234           if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl))
3235             continue;
3236           import_export_decl (decl);
3237           /* If this static data member is needed, provide it to the
3238              back end.  */
3239           if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
3240             DECL_EXTERNAL (decl) = 0;
3241         }
3242       if (VEC_length (tree, pending_statics) != 0
3243           && wrapup_global_declarations (VEC_address (tree, pending_statics),
3244                                          VEC_length (tree, pending_statics)))
3245         reconsider = true;
3246
3247       retries++;
3248     }
3249   while (reconsider);
3250
3251   /* All used inline functions must have a definition at this point.  */
3252   for (i = 0; VEC_iterate (tree, deferred_fns, i, decl); ++i)
3253     {
3254       if (/* Check online inline functions that were actually used.  */
3255           TREE_USED (decl) && DECL_DECLARED_INLINE_P (decl)
3256           /* If the definition actually was available here, then the
3257              fact that the function was not defined merely represents
3258              that for some reason (use of a template repository,
3259              #pragma interface, etc.) we decided not to emit the
3260              definition here.  */
3261           && !DECL_INITIAL (decl)
3262           /* An explicit instantiation can be used to specify
3263              that the body is in another unit. It will have
3264              already verified there was a definition.  */
3265           && !DECL_EXPLICIT_INSTANTIATION (decl))
3266         {
3267           warning (0, "inline function %q+D used but never defined", decl);
3268           /* Avoid a duplicate warning from check_global_declaration_1.  */
3269           TREE_NO_WARNING (decl) = 1;
3270         }
3271     }
3272
3273   /* We give C linkage to static constructors and destructors.  */
3274   push_lang_context (lang_name_c);
3275
3276   /* Generate initialization and destruction functions for all
3277      priorities for which they are required.  */
3278   if (priority_info_map)
3279     splay_tree_foreach (priority_info_map,
3280                         generate_ctor_and_dtor_functions_for_priority,
3281                         /*data=*/&locus);
3282   else if (c_dialect_objc () && objc_static_init_needed_p ())
3283     /* If this is obj-c++ and we need a static init, call
3284        generate_ctor_or_dtor_function.  */
3285     generate_ctor_or_dtor_function (/*constructor_p=*/true,
3286                                     DEFAULT_INIT_PRIORITY, &locus);
3287
3288   /* We're done with the splay-tree now.  */
3289   if (priority_info_map)
3290     splay_tree_delete (priority_info_map);
3291
3292   /* Generate any missing aliases.  */
3293   maybe_apply_pending_pragma_weaks ();
3294
3295   /* We're done with static constructors, so we can go back to "C++"
3296      linkage now.  */
3297   pop_lang_context ();
3298
3299   cgraph_finalize_compilation_unit ();
3300   cgraph_optimize ();
3301
3302   /* Now, issue warnings about static, but not defined, functions,
3303      etc., and emit debugging information.  */
3304   walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3305   if (VEC_length (tree, pending_statics) != 0)
3306     {
3307       check_global_declarations (VEC_address (tree, pending_statics),
3308                                  VEC_length (tree, pending_statics));
3309       emit_debug_global_declarations (VEC_address (tree, pending_statics),
3310                                       VEC_length (tree, pending_statics));
3311     }
3312
3313   /* Generate hidden aliases for Java.  */
3314   build_java_method_aliases ();
3315
3316   finish_repo ();
3317
3318   /* The entire file is now complete.  If requested, dump everything
3319      to a file.  */
3320   {
3321     int flags;
3322     FILE *stream = dump_begin (TDI_tu, &flags);
3323
3324     if (stream)
3325       {
3326         dump_node (global_namespace, flags & ~TDF_SLIM, stream);
3327         dump_end (TDI_tu, stream);
3328       }
3329   }
3330
3331   timevar_pop (TV_VARCONST);
3332
3333   if (flag_detailed_statistics)
3334     {
3335       dump_tree_statistics ();
3336       dump_time_statistics ();
3337     }
3338   input_location = locus;
3339
3340 #ifdef ENABLE_CHECKING
3341   validate_conversion_obstack ();
3342 #endif /* ENABLE_CHECKING */
3343 }
3344
3345 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
3346    function to call in parse-tree form; it has not yet been
3347    semantically analyzed.  ARGS are the arguments to the function.
3348    They have already been semantically analyzed.  */
3349
3350 tree
3351 build_offset_ref_call_from_tree (tree fn, tree args)
3352 {
3353   tree orig_fn;
3354   tree orig_args;
3355   tree expr;
3356   tree object;
3357
3358   orig_fn = fn;
3359   orig_args = args;
3360   object = TREE_OPERAND (fn, 0);
3361
3362   if (processing_template_decl)
3363     {
3364       gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
3365                   || TREE_CODE (fn) == MEMBER_REF);
3366       if (type_dependent_expression_p (fn)
3367           || any_type_dependent_arguments_p (args))
3368         return build_nt_call_list (fn, args);
3369
3370       /* Transform the arguments and add the implicit "this"
3371          parameter.  That must be done before the FN is transformed
3372          because we depend on the form of FN.  */
3373       args = build_non_dependent_args (args);
3374       if (TREE_CODE (fn) == DOTSTAR_EXPR)
3375         object = build_unary_op (ADDR_EXPR, object, 0);
3376       object = build_non_dependent_expr (object);
3377       args = tree_cons (NULL_TREE, object, args);
3378       /* Now that the arguments are done, transform FN.  */
3379       fn = build_non_dependent_expr (fn);
3380     }
3381
3382   /* A qualified name corresponding to a bound pointer-to-member is
3383      represented as an OFFSET_REF:
3384
3385         struct B { void g(); };
3386         void (B::*p)();
3387         void B::g() { (this->*p)(); }  */
3388   if (TREE_CODE (fn) == OFFSET_REF)
3389     {
3390       tree object_addr = build_unary_op (ADDR_EXPR, object, 0);
3391       fn = TREE_OPERAND (fn, 1);
3392       fn = get_member_function_from_ptrfunc (&object_addr, fn);
3393       args = tree_cons (NULL_TREE, object_addr, args);
3394     }
3395
3396   expr = build_function_call (fn, args);
3397   if (processing_template_decl && expr != error_mark_node)
3398     return build_min_non_dep_call_list (expr, orig_fn, orig_args);
3399   return expr;
3400 }
3401
3402
3403 void
3404 check_default_args (tree x)
3405 {
3406   tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
3407   bool saw_def = false;
3408   int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
3409   for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
3410     {
3411       if (TREE_PURPOSE (arg))
3412         saw_def = true;
3413       else if (saw_def)
3414         {
3415           error ("default argument missing for parameter %P of %q+#D", i, x);
3416           TREE_PURPOSE (arg) = error_mark_node;
3417         }
3418     }
3419 }
3420
3421 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
3422    If DECL is a specialization or implicitly declared class member,
3423    generate the actual definition.  */
3424
3425 void
3426 mark_used (tree decl)
3427 {
3428   HOST_WIDE_INT saved_processing_template_decl = 0;
3429
3430   /* If DECL is a BASELINK for a single function, then treat it just
3431      like the DECL for the function.  Otherwise, if the BASELINK is
3432      for an overloaded function, we don't know which function was
3433      actually used until after overload resolution.  */
3434   if (TREE_CODE (decl) == BASELINK)
3435     {
3436       decl = BASELINK_FUNCTIONS (decl);
3437       if (really_overloaded_fn (decl))
3438         return;
3439       decl = OVL_CURRENT (decl);
3440     }
3441
3442   TREE_USED (decl) = 1;
3443   if (DECL_CLONED_FUNCTION_P (decl))
3444     TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
3445   /* If we don't need a value, then we don't need to synthesize DECL.  */
3446   if (skip_evaluation)
3447     return;
3448   /* Normally, we can wait until instantiation-time to synthesize
3449      DECL.  However, if DECL is a static data member initialized with
3450      a constant, we need the value right now because a reference to
3451      such a data member is not value-dependent.  */
3452   if (TREE_CODE (decl) == VAR_DECL
3453       && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)
3454       && DECL_CLASS_SCOPE_P (decl))
3455     {
3456       /* Don't try to instantiate members of dependent types.  We
3457          cannot just use dependent_type_p here because this function
3458          may be called from fold_non_dependent_expr, and then we may
3459          see dependent types, even though processing_template_decl
3460          will not be set.  */
3461       if (CLASSTYPE_TEMPLATE_INFO ((DECL_CONTEXT (decl)))
3462           && uses_template_parms (CLASSTYPE_TI_ARGS (DECL_CONTEXT (decl))))
3463         return;
3464       /* Pretend that we are not in a template, even if we are, so
3465          that the static data member initializer will be processed.  */
3466       saved_processing_template_decl = processing_template_decl;
3467       processing_template_decl = 0;
3468     }
3469
3470   if (processing_template_decl)
3471     return;
3472
3473   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
3474       && !TREE_ASM_WRITTEN (decl))
3475     /* Remember it, so we can check it was defined.  */
3476     {
3477       if (DECL_DEFERRED_FN (decl))
3478         return;
3479
3480       /* Remember the current location for a function we will end up
3481          synthesizing.  Then we can inform the user where it was
3482          required in the case of error.  */
3483       if (DECL_ARTIFICIAL (decl) && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
3484           && !DECL_THUNK_P (decl))
3485         DECL_SOURCE_LOCATION (decl) = input_location;
3486
3487       note_vague_linkage_fn (decl);
3488     }
3489
3490   assemble_external (decl);
3491
3492   /* Is it a synthesized method that needs to be synthesized?  */
3493   if (TREE_CODE (decl) == FUNCTION_DECL
3494       && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
3495       && DECL_ARTIFICIAL (decl)
3496       && !DECL_THUNK_P (decl)
3497       && ! DECL_INITIAL (decl)
3498       /* Kludge: don't synthesize for default args.  Unfortunately this
3499          rules out initializers of namespace-scoped objects too, but
3500          it's sort-of ok if the implicit ctor or dtor decl keeps
3501          pointing to the class location.  */
3502       && current_function_decl)
3503     {
3504       synthesize_method (decl);
3505       /* If we've already synthesized the method we don't need to
3506          do the instantiation test below.  */
3507     }
3508   else if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
3509            && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3510            && (!DECL_EXPLICIT_INSTANTIATION (decl)
3511                || (TREE_CODE (decl) == FUNCTION_DECL
3512                    && DECL_INLINE (DECL_TEMPLATE_RESULT
3513                                    (template_for_substitution (decl))))
3514                /* We need to instantiate static data members so that there
3515                   initializers are available in integral constant
3516                   expressions.  */
3517                || (TREE_CODE (decl) == VAR_DECL
3518                    && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))))
3519     /* If this is a function or variable that is an instance of some
3520        template, we now know that we will need to actually do the
3521        instantiation. We check that DECL is not an explicit
3522        instantiation because that is not checked in instantiate_decl.
3523
3524        We put off instantiating functions in order to improve compile
3525        times.  Maintaining a stack of active functions is expensive,
3526        and the inliner knows to instantiate any functions it might
3527        need.  Therefore, we always try to defer instantiation.  */
3528     instantiate_decl (decl, /*defer_ok=*/true,
3529                       /*expl_inst_class_mem_p=*/false);
3530
3531   processing_template_decl = saved_processing_template_decl;
3532 }
3533
3534 #include "gt-cp-decl2.h"