xref: /dragonfly/contrib/gcc-4.7/gcc/cp/decl2.c (revision 5ce9237c)
1e4b17023SJohn Marino /* Process declarations and variables for C++ compiler.
2e4b17023SJohn Marino    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3e4b17023SJohn Marino    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010,
4e4b17023SJohn Marino    2011 Free Software Foundation, Inc.
5e4b17023SJohn Marino    Hacked by Michael Tiemann (tiemann@cygnus.com)
6e4b17023SJohn Marino 
7e4b17023SJohn Marino This file is part of GCC.
8e4b17023SJohn Marino 
9e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify
10e4b17023SJohn Marino it under the terms of the GNU General Public License as published by
11e4b17023SJohn Marino the Free Software Foundation; either version 3, or (at your option)
12e4b17023SJohn Marino any later version.
13e4b17023SJohn Marino 
14e4b17023SJohn Marino GCC is distributed in the hope that it will be useful,
15e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
16e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17e4b17023SJohn Marino GNU General Public License for more details.
18e4b17023SJohn Marino 
19e4b17023SJohn Marino You should have received a copy of the GNU General Public License
20e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
21e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
22e4b17023SJohn Marino 
23e4b17023SJohn Marino 
24e4b17023SJohn Marino /* Process declarations and symbol lookup for C++ front end.
25e4b17023SJohn Marino    Also constructs types; the standard scalar types at initialization,
26e4b17023SJohn Marino    and structure, union, array and enum types when they are declared.  */
27e4b17023SJohn Marino 
28e4b17023SJohn Marino /* ??? not all decl nodes are given the most useful possible
29e4b17023SJohn Marino    line numbers.  For example, the CONST_DECLs for enum values.  */
30e4b17023SJohn Marino 
31e4b17023SJohn Marino #include "config.h"
32e4b17023SJohn Marino #include "system.h"
33e4b17023SJohn Marino #include "coretypes.h"
34e4b17023SJohn Marino #include "tm.h"
35e4b17023SJohn Marino #include "tree.h"
36e4b17023SJohn Marino #include "flags.h"
37e4b17023SJohn Marino #include "cp-tree.h"
38e4b17023SJohn Marino #include "decl.h"
39e4b17023SJohn Marino #include "output.h"
40e4b17023SJohn Marino #include "toplev.h"
41e4b17023SJohn Marino #include "timevar.h"
42e4b17023SJohn Marino #include "cpplib.h"
43e4b17023SJohn Marino #include "target.h"
44e4b17023SJohn Marino #include "c-family/c-common.h"
45e4b17023SJohn Marino #include "c-family/c-objc.h"
46e4b17023SJohn Marino #include "cgraph.h"
47e4b17023SJohn Marino #include "tree-inline.h"
48e4b17023SJohn Marino #include "c-family/c-pragma.h"
49e4b17023SJohn Marino #include "tree-dump.h"
50e4b17023SJohn Marino #include "intl.h"
51e4b17023SJohn Marino #include "gimple.h"
52e4b17023SJohn Marino #include "pointer-set.h"
53e4b17023SJohn Marino #include "splay-tree.h"
54e4b17023SJohn Marino #include "langhooks.h"
55e4b17023SJohn Marino #include "c-family/c-ada-spec.h"
56e4b17023SJohn Marino 
57e4b17023SJohn Marino extern cpp_reader *parse_in;
58e4b17023SJohn Marino 
59e4b17023SJohn Marino /* This structure contains information about the initializations
60e4b17023SJohn Marino    and/or destructions required for a particular priority level.  */
61e4b17023SJohn Marino typedef struct priority_info_s {
62e4b17023SJohn Marino   /* Nonzero if there have been any initializations at this priority
63e4b17023SJohn Marino      throughout the translation unit.  */
64e4b17023SJohn Marino   int initializations_p;
65e4b17023SJohn Marino   /* Nonzero if there have been any destructions at this priority
66e4b17023SJohn Marino      throughout the translation unit.  */
67e4b17023SJohn Marino   int destructions_p;
68e4b17023SJohn Marino } *priority_info;
69e4b17023SJohn Marino 
70e4b17023SJohn Marino static void mark_vtable_entries (tree);
71e4b17023SJohn Marino static bool maybe_emit_vtables (tree);
72e4b17023SJohn Marino static bool acceptable_java_type (tree);
73e4b17023SJohn Marino static tree start_objects (int, int);
74e4b17023SJohn Marino static void finish_objects (int, int, tree);
75e4b17023SJohn Marino static tree start_static_storage_duration_function (unsigned);
76e4b17023SJohn Marino static void finish_static_storage_duration_function (tree);
77e4b17023SJohn Marino static priority_info get_priority_info (int);
78e4b17023SJohn Marino static void do_static_initialization_or_destruction (tree, bool);
79e4b17023SJohn Marino static void one_static_initialization_or_destruction (tree, tree, bool);
80e4b17023SJohn Marino static void generate_ctor_or_dtor_function (bool, int, location_t *);
81e4b17023SJohn Marino static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
82e4b17023SJohn Marino 							  void *);
83e4b17023SJohn Marino static tree prune_vars_needing_no_initialization (tree *);
84e4b17023SJohn Marino static void write_out_vars (tree);
85e4b17023SJohn Marino static void import_export_class (tree);
86e4b17023SJohn Marino static tree get_guard_bits (tree);
87e4b17023SJohn Marino static void determine_visibility_from_class (tree, tree);
88e4b17023SJohn Marino static bool determine_hidden_inline (tree);
89e4b17023SJohn Marino static bool decl_defined_p (tree);
90e4b17023SJohn Marino 
91e4b17023SJohn Marino /* A list of static class variables.  This is needed, because a
92e4b17023SJohn Marino    static class variable can be declared inside the class without
93e4b17023SJohn Marino    an initializer, and then initialized, statically, outside the class.  */
94e4b17023SJohn Marino static GTY(()) VEC(tree,gc) *pending_statics;
95e4b17023SJohn Marino 
96e4b17023SJohn Marino /* A list of functions which were declared inline, but which we
97e4b17023SJohn Marino    may need to emit outline anyway.  */
98e4b17023SJohn Marino static GTY(()) VEC(tree,gc) *deferred_fns;
99e4b17023SJohn Marino 
100e4b17023SJohn Marino /* A list of decls that use types with no linkage, which we need to make
101e4b17023SJohn Marino    sure are defined.  */
102e4b17023SJohn Marino static GTY(()) VEC(tree,gc) *no_linkage_decls;
103e4b17023SJohn Marino 
104e4b17023SJohn Marino /* Nonzero if we're done parsing and into end-of-file activities.  */
105e4b17023SJohn Marino 
106e4b17023SJohn Marino int at_eof;
107e4b17023SJohn Marino 
108e4b17023SJohn Marino 
109e4b17023SJohn Marino 
110e4b17023SJohn Marino /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
111e4b17023SJohn Marino    FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
112e4b17023SJohn Marino    that apply to the function).  */
113e4b17023SJohn Marino 
114e4b17023SJohn Marino tree
build_memfn_type(tree fntype,tree ctype,cp_cv_quals quals)115e4b17023SJohn Marino build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals)
116e4b17023SJohn Marino {
117e4b17023SJohn Marino   tree raises;
118e4b17023SJohn Marino   tree attrs;
119e4b17023SJohn Marino   int type_quals;
120e4b17023SJohn Marino 
121e4b17023SJohn Marino   if (fntype == error_mark_node || ctype == error_mark_node)
122e4b17023SJohn Marino     return error_mark_node;
123e4b17023SJohn Marino 
124e4b17023SJohn Marino   gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
125e4b17023SJohn Marino 	      || TREE_CODE (fntype) == METHOD_TYPE);
126e4b17023SJohn Marino 
127e4b17023SJohn Marino   type_quals = quals & ~TYPE_QUAL_RESTRICT;
128e4b17023SJohn Marino   ctype = cp_build_qualified_type (ctype, type_quals);
129e4b17023SJohn Marino   raises = TYPE_RAISES_EXCEPTIONS (fntype);
130e4b17023SJohn Marino   attrs = TYPE_ATTRIBUTES (fntype);
131e4b17023SJohn Marino   fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
132e4b17023SJohn Marino 				       (TREE_CODE (fntype) == METHOD_TYPE
133e4b17023SJohn Marino 					? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
134e4b17023SJohn Marino 					: TYPE_ARG_TYPES (fntype)));
135e4b17023SJohn Marino   if (raises)
136e4b17023SJohn Marino     fntype = build_exception_variant (fntype, raises);
137e4b17023SJohn Marino   if (attrs)
138e4b17023SJohn Marino     fntype = cp_build_type_attribute_variant (fntype, attrs);
139e4b17023SJohn Marino 
140e4b17023SJohn Marino   return fntype;
141e4b17023SJohn Marino }
142e4b17023SJohn Marino 
143e4b17023SJohn Marino /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
144e4b17023SJohn Marino    return type changed to NEW_RET.  */
145e4b17023SJohn Marino 
146e4b17023SJohn Marino tree
change_return_type(tree new_ret,tree fntype)147e4b17023SJohn Marino change_return_type (tree new_ret, tree fntype)
148e4b17023SJohn Marino {
149e4b17023SJohn Marino   tree newtype;
150e4b17023SJohn Marino   tree args = TYPE_ARG_TYPES (fntype);
151e4b17023SJohn Marino   tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
152e4b17023SJohn Marino   tree attrs = TYPE_ATTRIBUTES (fntype);
153e4b17023SJohn Marino 
154e4b17023SJohn Marino   if (same_type_p (new_ret, TREE_TYPE (fntype)))
155e4b17023SJohn Marino     return fntype;
156e4b17023SJohn Marino 
157e4b17023SJohn Marino   if (TREE_CODE (fntype) == FUNCTION_TYPE)
158e4b17023SJohn Marino     {
159e4b17023SJohn Marino       newtype = build_function_type (new_ret, args);
160e4b17023SJohn Marino       newtype = apply_memfn_quals (newtype, type_memfn_quals (fntype));
161e4b17023SJohn Marino     }
162e4b17023SJohn Marino   else
163e4b17023SJohn Marino     newtype = build_method_type_directly
164e4b17023SJohn Marino       (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
165e4b17023SJohn Marino   if (raises)
166e4b17023SJohn Marino     newtype = build_exception_variant (newtype, raises);
167e4b17023SJohn Marino   if (attrs)
168e4b17023SJohn Marino     newtype = cp_build_type_attribute_variant (newtype, attrs);
169e4b17023SJohn Marino 
170e4b17023SJohn Marino   return newtype;
171e4b17023SJohn Marino }
172e4b17023SJohn Marino 
173e4b17023SJohn Marino /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
174e4b17023SJohn Marino    appropriately.  */
175e4b17023SJohn Marino 
176e4b17023SJohn Marino tree
cp_build_parm_decl(tree name,tree type)177e4b17023SJohn Marino cp_build_parm_decl (tree name, tree type)
178e4b17023SJohn Marino {
179e4b17023SJohn Marino   tree parm = build_decl (input_location,
180e4b17023SJohn Marino 			  PARM_DECL, name, type);
181e4b17023SJohn Marino   /* DECL_ARG_TYPE is only used by the back end and the back end never
182e4b17023SJohn Marino      sees templates.  */
183e4b17023SJohn Marino   if (!processing_template_decl)
184e4b17023SJohn Marino     DECL_ARG_TYPE (parm) = type_passed_as (type);
185e4b17023SJohn Marino 
186e4b17023SJohn Marino   /* If the type is a pack expansion, then we have a function
187e4b17023SJohn Marino      parameter pack. */
188e4b17023SJohn Marino   if (type && TREE_CODE (type) == TYPE_PACK_EXPANSION)
189e4b17023SJohn Marino     FUNCTION_PARAMETER_PACK_P (parm) = 1;
190e4b17023SJohn Marino 
191e4b17023SJohn Marino   return parm;
192e4b17023SJohn Marino }
193e4b17023SJohn Marino 
194e4b17023SJohn Marino /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
195e4b17023SJohn Marino    indicated NAME.  */
196e4b17023SJohn Marino 
197e4b17023SJohn Marino tree
build_artificial_parm(tree name,tree type)198e4b17023SJohn Marino build_artificial_parm (tree name, tree type)
199e4b17023SJohn Marino {
200e4b17023SJohn Marino   tree parm = cp_build_parm_decl (name, type);
201e4b17023SJohn Marino   DECL_ARTIFICIAL (parm) = 1;
202e4b17023SJohn Marino   /* All our artificial parms are implicitly `const'; they cannot be
203e4b17023SJohn Marino      assigned to.  */
204e4b17023SJohn Marino   TREE_READONLY (parm) = 1;
205e4b17023SJohn Marino   return parm;
206e4b17023SJohn Marino }
207e4b17023SJohn Marino 
208e4b17023SJohn Marino /* Constructors for types with virtual baseclasses need an "in-charge" flag
209e4b17023SJohn Marino    saying whether this constructor is responsible for initialization of
210e4b17023SJohn Marino    virtual baseclasses or not.  All destructors also need this "in-charge"
211e4b17023SJohn Marino    flag, which additionally determines whether or not the destructor should
212e4b17023SJohn Marino    free the memory for the object.
213e4b17023SJohn Marino 
214e4b17023SJohn Marino    This function adds the "in-charge" flag to member function FN if
215e4b17023SJohn Marino    appropriate.  It is called from grokclassfn and tsubst.
216e4b17023SJohn Marino    FN must be either a constructor or destructor.
217e4b17023SJohn Marino 
218e4b17023SJohn Marino    The in-charge flag follows the 'this' parameter, and is followed by the
219e4b17023SJohn Marino    VTT parm (if any), then the user-written parms.  */
220e4b17023SJohn Marino 
221e4b17023SJohn Marino void
maybe_retrofit_in_chrg(tree fn)222e4b17023SJohn Marino maybe_retrofit_in_chrg (tree fn)
223e4b17023SJohn Marino {
224e4b17023SJohn Marino   tree basetype, arg_types, parms, parm, fntype;
225e4b17023SJohn Marino 
226e4b17023SJohn Marino   /* If we've already add the in-charge parameter don't do it again.  */
227e4b17023SJohn Marino   if (DECL_HAS_IN_CHARGE_PARM_P (fn))
228e4b17023SJohn Marino     return;
229e4b17023SJohn Marino 
230e4b17023SJohn Marino   /* When processing templates we can't know, in general, whether or
231e4b17023SJohn Marino      not we're going to have virtual baseclasses.  */
232e4b17023SJohn Marino   if (processing_template_decl)
233e4b17023SJohn Marino     return;
234e4b17023SJohn Marino 
235e4b17023SJohn Marino   /* We don't need an in-charge parameter for constructors that don't
236e4b17023SJohn Marino      have virtual bases.  */
237e4b17023SJohn Marino   if (DECL_CONSTRUCTOR_P (fn)
238e4b17023SJohn Marino       && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
239e4b17023SJohn Marino     return;
240e4b17023SJohn Marino 
241e4b17023SJohn Marino   arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
242e4b17023SJohn Marino   basetype = TREE_TYPE (TREE_VALUE (arg_types));
243e4b17023SJohn Marino   arg_types = TREE_CHAIN (arg_types);
244e4b17023SJohn Marino 
245e4b17023SJohn Marino   parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
246e4b17023SJohn Marino 
247e4b17023SJohn Marino   /* If this is a subobject constructor or destructor, our caller will
248e4b17023SJohn Marino      pass us a pointer to our VTT.  */
249e4b17023SJohn Marino   if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
250e4b17023SJohn Marino     {
251e4b17023SJohn Marino       parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
252e4b17023SJohn Marino 
253e4b17023SJohn Marino       /* First add it to DECL_ARGUMENTS between 'this' and the real args...  */
254e4b17023SJohn Marino       DECL_CHAIN (parm) = parms;
255e4b17023SJohn Marino       parms = parm;
256e4b17023SJohn Marino 
257e4b17023SJohn Marino       /* ...and then to TYPE_ARG_TYPES.  */
258e4b17023SJohn Marino       arg_types = hash_tree_chain (vtt_parm_type, arg_types);
259e4b17023SJohn Marino 
260e4b17023SJohn Marino       DECL_HAS_VTT_PARM_P (fn) = 1;
261e4b17023SJohn Marino     }
262e4b17023SJohn Marino 
263e4b17023SJohn Marino   /* Then add the in-charge parm (before the VTT parm).  */
264e4b17023SJohn Marino   parm = build_artificial_parm (in_charge_identifier, integer_type_node);
265e4b17023SJohn Marino   DECL_CHAIN (parm) = parms;
266e4b17023SJohn Marino   parms = parm;
267e4b17023SJohn Marino   arg_types = hash_tree_chain (integer_type_node, arg_types);
268e4b17023SJohn Marino 
269e4b17023SJohn Marino   /* Insert our new parameter(s) into the list.  */
270e4b17023SJohn Marino   DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
271e4b17023SJohn Marino 
272e4b17023SJohn Marino   /* And rebuild the function type.  */
273e4b17023SJohn Marino   fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
274e4b17023SJohn Marino 				       arg_types);
275e4b17023SJohn Marino   if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
276e4b17023SJohn Marino     fntype = build_exception_variant (fntype,
277e4b17023SJohn Marino 				      TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
278e4b17023SJohn Marino   if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
279e4b17023SJohn Marino     fntype = (cp_build_type_attribute_variant
280e4b17023SJohn Marino 	      (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
281e4b17023SJohn Marino   TREE_TYPE (fn) = fntype;
282e4b17023SJohn Marino 
283e4b17023SJohn Marino   /* Now we've got the in-charge parameter.  */
284e4b17023SJohn Marino   DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
285e4b17023SJohn Marino }
286e4b17023SJohn Marino 
287e4b17023SJohn Marino /* Classes overload their constituent function names automatically.
288e4b17023SJohn Marino    When a function name is declared in a record structure,
289e4b17023SJohn Marino    its name is changed to it overloaded name.  Since names for
290e4b17023SJohn Marino    constructors and destructors can conflict, we place a leading
291e4b17023SJohn Marino    '$' for destructors.
292e4b17023SJohn Marino 
293e4b17023SJohn Marino    CNAME is the name of the class we are grokking for.
294e4b17023SJohn Marino 
295e4b17023SJohn Marino    FUNCTION is a FUNCTION_DECL.  It was created by `grokdeclarator'.
296e4b17023SJohn Marino 
297e4b17023SJohn Marino    FLAGS contains bits saying what's special about today's
298e4b17023SJohn Marino    arguments.  DTOR_FLAG == DESTRUCTOR.
299e4b17023SJohn Marino 
300e4b17023SJohn Marino    If FUNCTION is a destructor, then we must add the `auto-delete' field
301e4b17023SJohn Marino    as a second parameter.  There is some hair associated with the fact
302e4b17023SJohn Marino    that we must "declare" this variable in the manner consistent with the
303e4b17023SJohn Marino    way the rest of the arguments were declared.
304e4b17023SJohn Marino 
305e4b17023SJohn Marino    QUALS are the qualifiers for the this pointer.  */
306e4b17023SJohn Marino 
307e4b17023SJohn Marino void
grokclassfn(tree ctype,tree function,enum overload_flags flags)308e4b17023SJohn Marino grokclassfn (tree ctype, tree function, enum overload_flags flags)
309e4b17023SJohn Marino {
310e4b17023SJohn Marino   tree fn_name = DECL_NAME (function);
311e4b17023SJohn Marino 
312e4b17023SJohn Marino   /* Even within an `extern "C"' block, members get C++ linkage.  See
313e4b17023SJohn Marino      [dcl.link] for details.  */
314e4b17023SJohn Marino   SET_DECL_LANGUAGE (function, lang_cplusplus);
315e4b17023SJohn Marino 
316e4b17023SJohn Marino   if (fn_name == NULL_TREE)
317e4b17023SJohn Marino     {
318e4b17023SJohn Marino       error ("name missing for member function");
319e4b17023SJohn Marino       fn_name = get_identifier ("<anonymous>");
320e4b17023SJohn Marino       DECL_NAME (function) = fn_name;
321e4b17023SJohn Marino     }
322e4b17023SJohn Marino 
323e4b17023SJohn Marino   DECL_CONTEXT (function) = ctype;
324e4b17023SJohn Marino 
325e4b17023SJohn Marino   if (flags == DTOR_FLAG)
326e4b17023SJohn Marino     DECL_DESTRUCTOR_P (function) = 1;
327e4b17023SJohn Marino 
328e4b17023SJohn Marino   if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
329e4b17023SJohn Marino     maybe_retrofit_in_chrg (function);
330e4b17023SJohn Marino }
331e4b17023SJohn Marino 
332e4b17023SJohn Marino /* Create an ARRAY_REF, checking for the user doing things backwards
333e4b17023SJohn Marino    along the way.  */
334e4b17023SJohn Marino 
335e4b17023SJohn Marino tree
grok_array_decl(tree array_expr,tree index_exp)336e4b17023SJohn Marino grok_array_decl (tree array_expr, tree index_exp)
337e4b17023SJohn Marino {
338e4b17023SJohn Marino   tree type;
339e4b17023SJohn Marino   tree expr;
340e4b17023SJohn Marino   tree orig_array_expr = array_expr;
341e4b17023SJohn Marino   tree orig_index_exp = index_exp;
342e4b17023SJohn Marino 
343e4b17023SJohn Marino   if (error_operand_p (array_expr) || error_operand_p (index_exp))
344e4b17023SJohn Marino     return error_mark_node;
345e4b17023SJohn Marino 
346e4b17023SJohn Marino   if (processing_template_decl)
347e4b17023SJohn Marino     {
348e4b17023SJohn Marino       if (type_dependent_expression_p (array_expr)
349e4b17023SJohn Marino 	  || type_dependent_expression_p (index_exp))
350e4b17023SJohn Marino 	return build_min_nt (ARRAY_REF, array_expr, index_exp,
351e4b17023SJohn Marino 			     NULL_TREE, NULL_TREE);
352e4b17023SJohn Marino       array_expr = build_non_dependent_expr (array_expr);
353e4b17023SJohn Marino       index_exp = build_non_dependent_expr (index_exp);
354e4b17023SJohn Marino     }
355e4b17023SJohn Marino 
356e4b17023SJohn Marino   type = TREE_TYPE (array_expr);
357e4b17023SJohn Marino   gcc_assert (type);
358e4b17023SJohn Marino   type = non_reference (type);
359e4b17023SJohn Marino 
360e4b17023SJohn Marino   /* If they have an `operator[]', use that.  */
361e4b17023SJohn Marino   if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
362e4b17023SJohn Marino     expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL,
363e4b17023SJohn Marino 			 array_expr, index_exp, NULL_TREE,
364e4b17023SJohn Marino 			 /*overload=*/NULL, tf_warning_or_error);
365e4b17023SJohn Marino   else
366e4b17023SJohn Marino     {
367e4b17023SJohn Marino       tree p1, p2, i1, i2;
368e4b17023SJohn Marino 
369e4b17023SJohn Marino       /* Otherwise, create an ARRAY_REF for a pointer or array type.
370e4b17023SJohn Marino 	 It is a little-known fact that, if `a' is an array and `i' is
371e4b17023SJohn Marino 	 an int, you can write `i[a]', which means the same thing as
372e4b17023SJohn Marino 	 `a[i]'.  */
373e4b17023SJohn Marino       if (TREE_CODE (type) == ARRAY_TYPE)
374e4b17023SJohn Marino 	p1 = array_expr;
375e4b17023SJohn Marino       else
376e4b17023SJohn Marino 	p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
377e4b17023SJohn Marino 
378e4b17023SJohn Marino       if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
379e4b17023SJohn Marino 	p2 = index_exp;
380e4b17023SJohn Marino       else
381e4b17023SJohn Marino 	p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
382e4b17023SJohn Marino 
383e4b17023SJohn Marino       i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
384e4b17023SJohn Marino 				       false);
385e4b17023SJohn Marino       i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
386e4b17023SJohn Marino 				       false);
387e4b17023SJohn Marino 
388e4b17023SJohn Marino       if ((p1 && i2) && (i1 && p2))
389e4b17023SJohn Marino 	error ("ambiguous conversion for array subscript");
390e4b17023SJohn Marino 
391e4b17023SJohn Marino       if (p1 && i2)
392e4b17023SJohn Marino 	array_expr = p1, index_exp = i2;
393e4b17023SJohn Marino       else if (i1 && p2)
394e4b17023SJohn Marino 	array_expr = p2, index_exp = i1;
395e4b17023SJohn Marino       else
396e4b17023SJohn Marino 	{
397e4b17023SJohn Marino 	  error ("invalid types %<%T[%T]%> for array subscript",
398e4b17023SJohn Marino 		 type, TREE_TYPE (index_exp));
399e4b17023SJohn Marino 	  return error_mark_node;
400e4b17023SJohn Marino 	}
401e4b17023SJohn Marino 
402e4b17023SJohn Marino       if (array_expr == error_mark_node || index_exp == error_mark_node)
403e4b17023SJohn Marino 	error ("ambiguous conversion for array subscript");
404e4b17023SJohn Marino 
405e4b17023SJohn Marino       expr = build_array_ref (input_location, array_expr, index_exp);
406e4b17023SJohn Marino     }
407e4b17023SJohn Marino   if (processing_template_decl && expr != error_mark_node)
408e4b17023SJohn Marino     return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
409e4b17023SJohn Marino 			      NULL_TREE, NULL_TREE);
410e4b17023SJohn Marino   return expr;
411e4b17023SJohn Marino }
412e4b17023SJohn Marino 
413e4b17023SJohn Marino /* Given the cast expression EXP, checking out its validity.   Either return
414e4b17023SJohn Marino    an error_mark_node if there was an unavoidable error, return a cast to
415e4b17023SJohn Marino    void for trying to delete a pointer w/ the value 0, or return the
416e4b17023SJohn Marino    call to delete.  If DOING_VEC is true, we handle things differently
417e4b17023SJohn Marino    for doing an array delete.
418e4b17023SJohn Marino    Implements ARM $5.3.4.  This is called from the parser.  */
419e4b17023SJohn Marino 
420e4b17023SJohn Marino tree
delete_sanity(tree exp,tree size,bool doing_vec,int use_global_delete,tsubst_flags_t complain)421e4b17023SJohn Marino delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
422e4b17023SJohn Marino 	       tsubst_flags_t complain)
423e4b17023SJohn Marino {
424e4b17023SJohn Marino   tree t, type;
425e4b17023SJohn Marino 
426e4b17023SJohn Marino   if (exp == error_mark_node)
427e4b17023SJohn Marino     return exp;
428e4b17023SJohn Marino 
429e4b17023SJohn Marino   if (processing_template_decl)
430e4b17023SJohn Marino     {
431e4b17023SJohn Marino       t = build_min (DELETE_EXPR, void_type_node, exp, size);
432e4b17023SJohn Marino       DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
433e4b17023SJohn Marino       DELETE_EXPR_USE_VEC (t) = doing_vec;
434e4b17023SJohn Marino       TREE_SIDE_EFFECTS (t) = 1;
435e4b17023SJohn Marino       return t;
436e4b17023SJohn Marino     }
437e4b17023SJohn Marino 
438e4b17023SJohn Marino   /* An array can't have been allocated by new, so complain.  */
439e4b17023SJohn Marino   if (TREE_CODE (exp) == VAR_DECL
440e4b17023SJohn Marino       && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
441e4b17023SJohn Marino     warning (0, "deleting array %q#D", exp);
442e4b17023SJohn Marino 
443e4b17023SJohn Marino   t = build_expr_type_conversion (WANT_POINTER, exp, true);
444e4b17023SJohn Marino 
445e4b17023SJohn Marino   if (t == NULL_TREE || t == error_mark_node)
446e4b17023SJohn Marino     {
447e4b17023SJohn Marino       error ("type %q#T argument given to %<delete%>, expected pointer",
448e4b17023SJohn Marino 	     TREE_TYPE (exp));
449e4b17023SJohn Marino       return error_mark_node;
450e4b17023SJohn Marino     }
451e4b17023SJohn Marino 
452e4b17023SJohn Marino   type = TREE_TYPE (t);
453e4b17023SJohn Marino 
454e4b17023SJohn Marino   /* As of Valley Forge, you can delete a pointer to const.  */
455e4b17023SJohn Marino 
456e4b17023SJohn Marino   /* You can't delete functions.  */
457e4b17023SJohn Marino   if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
458e4b17023SJohn Marino     {
459e4b17023SJohn Marino       error ("cannot delete a function.  Only pointer-to-objects are "
460e4b17023SJohn Marino 	     "valid arguments to %<delete%>");
461e4b17023SJohn Marino       return error_mark_node;
462e4b17023SJohn Marino     }
463e4b17023SJohn Marino 
464e4b17023SJohn Marino   /* Deleting ptr to void is undefined behavior [expr.delete/3].  */
465e4b17023SJohn Marino   if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
466e4b17023SJohn Marino     {
467e4b17023SJohn Marino       warning (0, "deleting %qT is undefined", type);
468e4b17023SJohn Marino       doing_vec = 0;
469e4b17023SJohn Marino     }
470e4b17023SJohn Marino 
471e4b17023SJohn Marino   /* Deleting a pointer with the value zero is valid and has no effect.  */
472e4b17023SJohn Marino   if (integer_zerop (t))
473e4b17023SJohn Marino     return build1 (NOP_EXPR, void_type_node, t);
474e4b17023SJohn Marino 
475e4b17023SJohn Marino   if (doing_vec)
476e4b17023SJohn Marino     return build_vec_delete (t, /*maxindex=*/NULL_TREE,
477e4b17023SJohn Marino 			     sfk_deleting_destructor,
478e4b17023SJohn Marino 			     use_global_delete, complain);
479e4b17023SJohn Marino   else
480e4b17023SJohn Marino     return build_delete (type, t, sfk_deleting_destructor,
481e4b17023SJohn Marino 			 LOOKUP_NORMAL, use_global_delete,
482e4b17023SJohn Marino 			 complain);
483e4b17023SJohn Marino }
484e4b17023SJohn Marino 
485e4b17023SJohn Marino /* Report an error if the indicated template declaration is not the
486e4b17023SJohn Marino    sort of thing that should be a member template.  */
487e4b17023SJohn Marino 
488e4b17023SJohn Marino void
check_member_template(tree tmpl)489e4b17023SJohn Marino check_member_template (tree tmpl)
490e4b17023SJohn Marino {
491e4b17023SJohn Marino   tree decl;
492e4b17023SJohn Marino 
493e4b17023SJohn Marino   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
494e4b17023SJohn Marino   decl = DECL_TEMPLATE_RESULT (tmpl);
495e4b17023SJohn Marino 
496e4b17023SJohn Marino   if (TREE_CODE (decl) == FUNCTION_DECL
497e4b17023SJohn Marino       || DECL_ALIAS_TEMPLATE_P (tmpl)
498e4b17023SJohn Marino       || (TREE_CODE (decl) == TYPE_DECL
499e4b17023SJohn Marino 	  && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
500e4b17023SJohn Marino     {
501e4b17023SJohn Marino       /* The parser rejects template declarations in local classes.  */
502e4b17023SJohn Marino       gcc_assert (!current_function_decl);
503e4b17023SJohn Marino       /* The parser rejects any use of virtual in a function template.  */
504e4b17023SJohn Marino       gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
505e4b17023SJohn Marino 		    && DECL_VIRTUAL_P (decl)));
506e4b17023SJohn Marino 
507e4b17023SJohn Marino       /* The debug-information generating code doesn't know what to do
508e4b17023SJohn Marino 	 with member templates.  */
509e4b17023SJohn Marino       DECL_IGNORED_P (tmpl) = 1;
510e4b17023SJohn Marino     }
511e4b17023SJohn Marino   else
512e4b17023SJohn Marino     error ("template declaration of %q#D", decl);
513e4b17023SJohn Marino }
514e4b17023SJohn Marino 
515e4b17023SJohn Marino /* Return true iff TYPE is a valid Java parameter or return type.  */
516e4b17023SJohn Marino 
517e4b17023SJohn Marino static bool
acceptable_java_type(tree type)518e4b17023SJohn Marino acceptable_java_type (tree type)
519e4b17023SJohn Marino {
520e4b17023SJohn Marino   if (type == error_mark_node)
521e4b17023SJohn Marino     return false;
522e4b17023SJohn Marino 
523e4b17023SJohn Marino   if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
524e4b17023SJohn Marino     return true;
525e4b17023SJohn Marino   if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
526e4b17023SJohn Marino     {
527e4b17023SJohn Marino       type = TREE_TYPE (type);
528e4b17023SJohn Marino       if (TREE_CODE (type) == RECORD_TYPE)
529e4b17023SJohn Marino 	{
530e4b17023SJohn Marino 	  tree args;  int i;
531e4b17023SJohn Marino 	  if (! TYPE_FOR_JAVA (type))
532e4b17023SJohn Marino 	    return false;
533e4b17023SJohn Marino 	  if (! CLASSTYPE_TEMPLATE_INFO (type))
534e4b17023SJohn Marino 	    return true;
535e4b17023SJohn Marino 	  args = CLASSTYPE_TI_ARGS (type);
536e4b17023SJohn Marino 	  i = TREE_VEC_LENGTH (args);
537e4b17023SJohn Marino 	  while (--i >= 0)
538e4b17023SJohn Marino 	    {
539e4b17023SJohn Marino 	      type = TREE_VEC_ELT (args, i);
540e4b17023SJohn Marino 	      if (TREE_CODE (type) == POINTER_TYPE)
541e4b17023SJohn Marino 		type = TREE_TYPE (type);
542e4b17023SJohn Marino 	      if (! TYPE_FOR_JAVA (type))
543e4b17023SJohn Marino 		return false;
544e4b17023SJohn Marino 	    }
545e4b17023SJohn Marino 	  return true;
546e4b17023SJohn Marino 	}
547e4b17023SJohn Marino     }
548e4b17023SJohn Marino   return false;
549e4b17023SJohn Marino }
550e4b17023SJohn Marino 
551e4b17023SJohn Marino /* For a METHOD in a Java class CTYPE, return true if
552e4b17023SJohn Marino    the parameter and return types are valid Java types.
553e4b17023SJohn Marino    Otherwise, print appropriate error messages, and return false.  */
554e4b17023SJohn Marino 
555e4b17023SJohn Marino bool
check_java_method(tree method)556e4b17023SJohn Marino check_java_method (tree method)
557e4b17023SJohn Marino {
558e4b17023SJohn Marino   bool jerr = false;
559e4b17023SJohn Marino   tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
560e4b17023SJohn Marino   tree ret_type = TREE_TYPE (TREE_TYPE (method));
561e4b17023SJohn Marino 
562e4b17023SJohn Marino   if (!acceptable_java_type (ret_type))
563e4b17023SJohn Marino     {
564e4b17023SJohn Marino       error ("Java method %qD has non-Java return type %qT",
565e4b17023SJohn Marino 	     method, ret_type);
566e4b17023SJohn Marino       jerr = true;
567e4b17023SJohn Marino     }
568e4b17023SJohn Marino 
569e4b17023SJohn Marino   arg_types = TREE_CHAIN (arg_types);
570e4b17023SJohn Marino   if (DECL_HAS_IN_CHARGE_PARM_P (method))
571e4b17023SJohn Marino     arg_types = TREE_CHAIN (arg_types);
572e4b17023SJohn Marino   if (DECL_HAS_VTT_PARM_P (method))
573e4b17023SJohn Marino     arg_types = TREE_CHAIN (arg_types);
574e4b17023SJohn Marino 
575e4b17023SJohn Marino   for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
576e4b17023SJohn Marino     {
577e4b17023SJohn Marino       tree type = TREE_VALUE (arg_types);
578e4b17023SJohn Marino       if (!acceptable_java_type (type))
579e4b17023SJohn Marino 	{
580e4b17023SJohn Marino           if (type != error_mark_node)
581e4b17023SJohn Marino 	    error ("Java method %qD has non-Java parameter type %qT",
582e4b17023SJohn Marino 		   method, type);
583e4b17023SJohn Marino 	  jerr = true;
584e4b17023SJohn Marino 	}
585e4b17023SJohn Marino     }
586e4b17023SJohn Marino   return !jerr;
587e4b17023SJohn Marino }
588e4b17023SJohn Marino 
589e4b17023SJohn Marino /* Sanity check: report error if this function FUNCTION is not
590e4b17023SJohn Marino    really a member of the class (CTYPE) it is supposed to belong to.
591e4b17023SJohn Marino    TEMPLATE_PARMS is used to specify the template parameters of a member
592e4b17023SJohn Marino    template passed as FUNCTION_DECL. If the member template is passed as a
593e4b17023SJohn Marino    TEMPLATE_DECL, it can be NULL since the parameters can be extracted
594e4b17023SJohn Marino    from the declaration. If the function is not a function template, it
595e4b17023SJohn Marino    must be NULL.
596e4b17023SJohn Marino    It returns the original declaration for the function, NULL_TREE if
597e4b17023SJohn Marino    no declaration was found, error_mark_node if an error was emitted.  */
598e4b17023SJohn Marino 
599e4b17023SJohn Marino tree
check_classfn(tree ctype,tree function,tree template_parms)600e4b17023SJohn Marino check_classfn (tree ctype, tree function, tree template_parms)
601e4b17023SJohn Marino {
602e4b17023SJohn Marino   int ix;
603e4b17023SJohn Marino   bool is_template;
604e4b17023SJohn Marino   tree pushed_scope;
605e4b17023SJohn Marino 
606e4b17023SJohn Marino   if (DECL_USE_TEMPLATE (function)
607e4b17023SJohn Marino       && !(TREE_CODE (function) == TEMPLATE_DECL
608e4b17023SJohn Marino 	   && DECL_TEMPLATE_SPECIALIZATION (function))
609e4b17023SJohn Marino       && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
610e4b17023SJohn Marino     /* Since this is a specialization of a member template,
611e4b17023SJohn Marino        we're not going to find the declaration in the class.
612e4b17023SJohn Marino        For example, in:
613e4b17023SJohn Marino 
614e4b17023SJohn Marino 	 struct S { template <typename T> void f(T); };
615e4b17023SJohn Marino 	 template <> void S::f(int);
616e4b17023SJohn Marino 
617e4b17023SJohn Marino        we're not going to find `S::f(int)', but there's no
618e4b17023SJohn Marino        reason we should, either.  We let our callers know we didn't
619e4b17023SJohn Marino        find the method, but we don't complain.  */
620e4b17023SJohn Marino     return NULL_TREE;
621e4b17023SJohn Marino 
622e4b17023SJohn Marino   /* Basic sanity check: for a template function, the template parameters
623e4b17023SJohn Marino      either were not passed, or they are the same of DECL_TEMPLATE_PARMS.  */
624e4b17023SJohn Marino   if (TREE_CODE (function) == TEMPLATE_DECL)
625e4b17023SJohn Marino     {
626e4b17023SJohn Marino       if (template_parms
627e4b17023SJohn Marino 	  && !comp_template_parms (template_parms,
628e4b17023SJohn Marino 				   DECL_TEMPLATE_PARMS (function)))
629e4b17023SJohn Marino 	{
630e4b17023SJohn Marino 	  error ("template parameter lists provided don%'t match the "
631e4b17023SJohn Marino 		 "template parameters of %qD", function);
632e4b17023SJohn Marino 	  return error_mark_node;
633e4b17023SJohn Marino 	}
634e4b17023SJohn Marino       template_parms = DECL_TEMPLATE_PARMS (function);
635e4b17023SJohn Marino     }
636e4b17023SJohn Marino 
637e4b17023SJohn Marino   /* OK, is this a definition of a member template?  */
638e4b17023SJohn Marino   is_template = (template_parms != NULL_TREE);
639e4b17023SJohn Marino 
640e4b17023SJohn Marino   /* We must enter the scope here, because conversion operators are
641e4b17023SJohn Marino      named by target type, and type equivalence relies on typenames
642e4b17023SJohn Marino      resolving within the scope of CTYPE.  */
643e4b17023SJohn Marino   pushed_scope = push_scope (ctype);
644e4b17023SJohn Marino   ix = class_method_index_for_fn (complete_type (ctype), function);
645e4b17023SJohn Marino   if (ix >= 0)
646e4b17023SJohn Marino     {
647e4b17023SJohn Marino       VEC(tree,gc) *methods = CLASSTYPE_METHOD_VEC (ctype);
648e4b17023SJohn Marino       tree fndecls, fndecl = 0;
649e4b17023SJohn Marino       bool is_conv_op;
650e4b17023SJohn Marino       const char *format = NULL;
651e4b17023SJohn Marino 
652e4b17023SJohn Marino       for (fndecls = VEC_index (tree, methods, ix);
653e4b17023SJohn Marino 	   fndecls; fndecls = OVL_NEXT (fndecls))
654e4b17023SJohn Marino 	{
655e4b17023SJohn Marino 	  tree p1, p2;
656e4b17023SJohn Marino 
657e4b17023SJohn Marino 	  fndecl = OVL_CURRENT (fndecls);
658e4b17023SJohn Marino 	  p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
659e4b17023SJohn Marino 	  p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
660e4b17023SJohn Marino 
661e4b17023SJohn Marino 	  /* We cannot simply call decls_match because this doesn't
662e4b17023SJohn Marino 	     work for static member functions that are pretending to
663e4b17023SJohn Marino 	     be methods, and because the name may have been changed by
664e4b17023SJohn Marino 	     asm("new_name").  */
665e4b17023SJohn Marino 
666e4b17023SJohn Marino 	   /* Get rid of the this parameter on functions that become
667e4b17023SJohn Marino 	      static.  */
668e4b17023SJohn Marino 	  if (DECL_STATIC_FUNCTION_P (fndecl)
669e4b17023SJohn Marino 	      && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
670e4b17023SJohn Marino 	    p1 = TREE_CHAIN (p1);
671e4b17023SJohn Marino 
672e4b17023SJohn Marino 	  /* A member template definition only matches a member template
673e4b17023SJohn Marino 	     declaration.  */
674e4b17023SJohn Marino 	  if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
675e4b17023SJohn Marino 	    continue;
676e4b17023SJohn Marino 
677e4b17023SJohn Marino 	  if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
678e4b17023SJohn Marino 			   TREE_TYPE (TREE_TYPE (fndecl)))
679e4b17023SJohn Marino 	      && compparms (p1, p2)
680e4b17023SJohn Marino 	      && (!is_template
681e4b17023SJohn Marino 		  || comp_template_parms (template_parms,
682e4b17023SJohn Marino 					  DECL_TEMPLATE_PARMS (fndecl)))
683e4b17023SJohn Marino 	      && (DECL_TEMPLATE_SPECIALIZATION (function)
684e4b17023SJohn Marino 		  == DECL_TEMPLATE_SPECIALIZATION (fndecl))
685e4b17023SJohn Marino 	      && (!DECL_TEMPLATE_SPECIALIZATION (function)
686e4b17023SJohn Marino 		  || (DECL_TI_TEMPLATE (function)
687e4b17023SJohn Marino 		      == DECL_TI_TEMPLATE (fndecl))))
688e4b17023SJohn Marino 	    break;
689e4b17023SJohn Marino 	}
690e4b17023SJohn Marino       if (fndecls)
691e4b17023SJohn Marino 	{
692e4b17023SJohn Marino 	  if (pushed_scope)
693e4b17023SJohn Marino 	    pop_scope (pushed_scope);
694e4b17023SJohn Marino 	  return OVL_CURRENT (fndecls);
695e4b17023SJohn Marino 	}
696e4b17023SJohn Marino 
697e4b17023SJohn Marino       error_at (DECL_SOURCE_LOCATION (function),
698e4b17023SJohn Marino 		"prototype for %q#D does not match any in class %qT",
699e4b17023SJohn Marino 		function, ctype);
700e4b17023SJohn Marino       is_conv_op = DECL_CONV_FN_P (fndecl);
701e4b17023SJohn Marino 
702e4b17023SJohn Marino       if (is_conv_op)
703e4b17023SJohn Marino 	ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
704e4b17023SJohn Marino       fndecls = VEC_index (tree, methods, ix);
705e4b17023SJohn Marino       while (fndecls)
706e4b17023SJohn Marino 	{
707e4b17023SJohn Marino 	  fndecl = OVL_CURRENT (fndecls);
708e4b17023SJohn Marino 	  fndecls = OVL_NEXT (fndecls);
709e4b17023SJohn Marino 
710e4b17023SJohn Marino 	  if (!fndecls && is_conv_op)
711e4b17023SJohn Marino 	    {
712e4b17023SJohn Marino 	      if (VEC_length (tree, methods) > (size_t) ++ix)
713e4b17023SJohn Marino 		{
714e4b17023SJohn Marino 		  fndecls = VEC_index (tree, methods, ix);
715e4b17023SJohn Marino 		  if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
716e4b17023SJohn Marino 		    {
717e4b17023SJohn Marino 		      fndecls = NULL_TREE;
718e4b17023SJohn Marino 		      is_conv_op = false;
719e4b17023SJohn Marino 		    }
720e4b17023SJohn Marino 		}
721e4b17023SJohn Marino 	      else
722e4b17023SJohn Marino 		is_conv_op = false;
723e4b17023SJohn Marino 	    }
724e4b17023SJohn Marino 	  if (format)
725e4b17023SJohn Marino 	    format = "                %+#D";
726e4b17023SJohn Marino 	  else if (fndecls)
727e4b17023SJohn Marino 	    format = N_("candidates are: %+#D");
728e4b17023SJohn Marino 	  else
729e4b17023SJohn Marino 	    format = N_("candidate is: %+#D");
730e4b17023SJohn Marino 	  error (format, fndecl);
731e4b17023SJohn Marino 	}
732e4b17023SJohn Marino     }
733e4b17023SJohn Marino   else if (!COMPLETE_TYPE_P (ctype))
734e4b17023SJohn Marino     cxx_incomplete_type_error (function, ctype);
735e4b17023SJohn Marino   else
736e4b17023SJohn Marino     error ("no %q#D member function declared in class %qT",
737e4b17023SJohn Marino 	   function, ctype);
738e4b17023SJohn Marino 
739e4b17023SJohn Marino   if (pushed_scope)
740e4b17023SJohn Marino     pop_scope (pushed_scope);
741e4b17023SJohn Marino   return error_mark_node;
742e4b17023SJohn Marino }
743e4b17023SJohn Marino 
744e4b17023SJohn Marino /* DECL is a function with vague linkage.  Remember it so that at the
745e4b17023SJohn Marino    end of the translation unit we can decide whether or not to emit
746e4b17023SJohn Marino    it.  */
747e4b17023SJohn Marino 
748e4b17023SJohn Marino void
note_vague_linkage_fn(tree decl)749e4b17023SJohn Marino note_vague_linkage_fn (tree decl)
750e4b17023SJohn Marino {
751e4b17023SJohn Marino   DECL_DEFER_OUTPUT (decl) = 1;
752e4b17023SJohn Marino   VEC_safe_push (tree, gc, deferred_fns, decl);
753e4b17023SJohn Marino }
754e4b17023SJohn Marino 
755e4b17023SJohn Marino /* We have just processed the DECL, which is a static data member.
756e4b17023SJohn Marino    The other parameters are as for cp_finish_decl.  */
757e4b17023SJohn Marino 
758e4b17023SJohn Marino void
finish_static_data_member_decl(tree decl,tree init,bool init_const_expr_p,tree asmspec_tree,int flags)759e4b17023SJohn Marino finish_static_data_member_decl (tree decl,
760e4b17023SJohn Marino 				tree init, bool init_const_expr_p,
761e4b17023SJohn Marino 				tree asmspec_tree,
762e4b17023SJohn Marino 				int flags)
763e4b17023SJohn Marino {
764e4b17023SJohn Marino   DECL_CONTEXT (decl) = current_class_type;
765e4b17023SJohn Marino 
766e4b17023SJohn Marino   /* We cannot call pushdecl here, because that would fill in the
767e4b17023SJohn Marino      TREE_CHAIN of our decl.  Instead, we modify cp_finish_decl to do
768e4b17023SJohn Marino      the right thing, namely, to put this decl out straight away.  */
769e4b17023SJohn Marino 
770e4b17023SJohn Marino   if (! processing_template_decl)
771e4b17023SJohn Marino     VEC_safe_push (tree, gc, pending_statics, decl);
772e4b17023SJohn Marino 
773e4b17023SJohn Marino   if (LOCAL_CLASS_P (current_class_type))
774e4b17023SJohn Marino     permerror (input_location, "local class %q#T shall not have static data member %q#D",
775e4b17023SJohn Marino 	       current_class_type, decl);
776e4b17023SJohn Marino 
777e4b17023SJohn Marino   DECL_IN_AGGR_P (decl) = 1;
778e4b17023SJohn Marino 
779e4b17023SJohn Marino   if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
780e4b17023SJohn Marino       && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
781e4b17023SJohn Marino     SET_VAR_HAD_UNKNOWN_BOUND (decl);
782e4b17023SJohn Marino 
783e4b17023SJohn Marino   cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
784e4b17023SJohn Marino }
785e4b17023SJohn Marino 
786e4b17023SJohn Marino /* DECLARATOR and DECLSPECS correspond to a class member.  The other
787e4b17023SJohn Marino    parameters are as for cp_finish_decl.  Return the DECL for the
788e4b17023SJohn Marino    class member declared.  */
789e4b17023SJohn Marino 
790e4b17023SJohn Marino tree
grokfield(const cp_declarator * declarator,cp_decl_specifier_seq * declspecs,tree init,bool init_const_expr_p,tree asmspec_tree,tree attrlist)791e4b17023SJohn Marino grokfield (const cp_declarator *declarator,
792e4b17023SJohn Marino 	   cp_decl_specifier_seq *declspecs,
793e4b17023SJohn Marino 	   tree init, bool init_const_expr_p,
794e4b17023SJohn Marino 	   tree asmspec_tree,
795e4b17023SJohn Marino 	   tree attrlist)
796e4b17023SJohn Marino {
797e4b17023SJohn Marino   tree value;
798e4b17023SJohn Marino   const char *asmspec = 0;
799e4b17023SJohn Marino   int flags;
800e4b17023SJohn Marino   tree name;
801e4b17023SJohn Marino 
802e4b17023SJohn Marino   if (init
803e4b17023SJohn Marino       && TREE_CODE (init) == TREE_LIST
804e4b17023SJohn Marino       && TREE_VALUE (init) == error_mark_node
805e4b17023SJohn Marino       && TREE_CHAIN (init) == NULL_TREE)
806e4b17023SJohn Marino     init = NULL_TREE;
807e4b17023SJohn Marino 
808e4b17023SJohn Marino   value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
809e4b17023SJohn Marino   if (! value || error_operand_p (value))
810e4b17023SJohn Marino     /* friend or constructor went bad.  */
811e4b17023SJohn Marino     return error_mark_node;
812e4b17023SJohn Marino 
813e4b17023SJohn Marino   if (TREE_CODE (value) == TYPE_DECL && init)
814e4b17023SJohn Marino     {
815e4b17023SJohn Marino       error ("typedef %qD is initialized (use decltype instead)", value);
816e4b17023SJohn Marino       init = NULL_TREE;
817e4b17023SJohn Marino     }
818e4b17023SJohn Marino 
819e4b17023SJohn Marino   /* Pass friendly classes back.  */
820e4b17023SJohn Marino   if (value == void_type_node)
821e4b17023SJohn Marino     return value;
822e4b17023SJohn Marino 
823e4b17023SJohn Marino   /* Pass friend decls back.  */
824e4b17023SJohn Marino   if ((TREE_CODE (value) == FUNCTION_DECL
825e4b17023SJohn Marino        || TREE_CODE (value) == TEMPLATE_DECL)
826e4b17023SJohn Marino       && DECL_CONTEXT (value) != current_class_type)
827e4b17023SJohn Marino     return value;
828e4b17023SJohn Marino 
829e4b17023SJohn Marino   name = DECL_NAME (value);
830e4b17023SJohn Marino 
831e4b17023SJohn Marino   if (name != NULL_TREE)
832e4b17023SJohn Marino     {
833e4b17023SJohn Marino       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
834e4b17023SJohn Marino 	{
835e4b17023SJohn Marino 	  error ("explicit template argument list not allowed");
836e4b17023SJohn Marino 	  return error_mark_node;
837e4b17023SJohn Marino 	}
838e4b17023SJohn Marino 
839e4b17023SJohn Marino       if (IDENTIFIER_POINTER (name)[0] == '_'
840e4b17023SJohn Marino 	  && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
841e4b17023SJohn Marino 	error ("member %qD conflicts with virtual function table field name",
842e4b17023SJohn Marino 	       value);
843e4b17023SJohn Marino     }
844e4b17023SJohn Marino 
845e4b17023SJohn Marino   /* Stash away type declarations.  */
846e4b17023SJohn Marino   if (TREE_CODE (value) == TYPE_DECL)
847e4b17023SJohn Marino     {
848e4b17023SJohn Marino       DECL_NONLOCAL (value) = 1;
849e4b17023SJohn Marino       DECL_CONTEXT (value) = current_class_type;
850e4b17023SJohn Marino 
851e4b17023SJohn Marino       if (attrlist)
852e4b17023SJohn Marino 	{
853e4b17023SJohn Marino 	  int attrflags = 0;
854e4b17023SJohn Marino 
855e4b17023SJohn Marino 	  /* If this is a typedef that names the class for linkage purposes
856e4b17023SJohn Marino 	     (7.1.3p8), apply any attributes directly to the type.  */
857e4b17023SJohn Marino 	  if (TAGGED_TYPE_P (TREE_TYPE (value))
858e4b17023SJohn Marino 	      && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
859e4b17023SJohn Marino 	    attrflags = ATTR_FLAG_TYPE_IN_PLACE;
860e4b17023SJohn Marino 
861e4b17023SJohn Marino 	  cplus_decl_attributes (&value, attrlist, attrflags);
862e4b17023SJohn Marino 	}
863e4b17023SJohn Marino 
864e4b17023SJohn Marino       if (declspecs->specs[(int)ds_typedef]
865e4b17023SJohn Marino           && TREE_TYPE (value) != error_mark_node
866e4b17023SJohn Marino           && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
867e4b17023SJohn Marino 	set_underlying_type (value);
868e4b17023SJohn Marino 
869e4b17023SJohn Marino       /* It's important that push_template_decl below follows
870e4b17023SJohn Marino 	 set_underlying_type above so that the created template
871e4b17023SJohn Marino 	 carries the properly set type of VALUE.  */
872e4b17023SJohn Marino       if (processing_template_decl)
873e4b17023SJohn Marino 	value = push_template_decl (value);
874e4b17023SJohn Marino 
875e4b17023SJohn Marino       record_locally_defined_typedef (value);
876e4b17023SJohn Marino       return value;
877e4b17023SJohn Marino     }
878e4b17023SJohn Marino 
879e4b17023SJohn Marino   if (DECL_IN_AGGR_P (value))
880e4b17023SJohn Marino     {
881e4b17023SJohn Marino       error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
882e4b17023SJohn Marino       return void_type_node;
883e4b17023SJohn Marino     }
884e4b17023SJohn Marino 
885e4b17023SJohn Marino   if (asmspec_tree && asmspec_tree != error_mark_node)
886e4b17023SJohn Marino     asmspec = TREE_STRING_POINTER (asmspec_tree);
887e4b17023SJohn Marino 
888e4b17023SJohn Marino   if (init)
889e4b17023SJohn Marino     {
890e4b17023SJohn Marino       if (TREE_CODE (value) == FUNCTION_DECL)
891e4b17023SJohn Marino 	{
892e4b17023SJohn Marino 	  /* Initializers for functions are rejected early in the parser.
893e4b17023SJohn Marino 	     If we get here, it must be a pure specifier for a method.  */
894e4b17023SJohn Marino 	  if (init == ridpointers[(int)RID_DELETE])
895e4b17023SJohn Marino 	    {
896e4b17023SJohn Marino 	      DECL_DELETED_FN (value) = 1;
897e4b17023SJohn Marino 	      DECL_DECLARED_INLINE_P (value) = 1;
898e4b17023SJohn Marino 	      DECL_INITIAL (value) = error_mark_node;
899e4b17023SJohn Marino 	    }
900e4b17023SJohn Marino 	  else if (init == ridpointers[(int)RID_DEFAULT])
901e4b17023SJohn Marino 	    {
902e4b17023SJohn Marino 	      if (defaultable_fn_check (value))
903e4b17023SJohn Marino 		{
904e4b17023SJohn Marino 		  DECL_DEFAULTED_FN (value) = 1;
905e4b17023SJohn Marino 		  DECL_INITIALIZED_IN_CLASS_P (value) = 1;
906e4b17023SJohn Marino 		  DECL_DECLARED_INLINE_P (value) = 1;
907e4b17023SJohn Marino 		}
908e4b17023SJohn Marino 	    }
909e4b17023SJohn Marino 	  else if (TREE_CODE (init) == DEFAULT_ARG)
910e4b17023SJohn Marino 	    error ("invalid initializer for member function %qD", value);
911e4b17023SJohn Marino 	  else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
912e4b17023SJohn Marino 	    {
913e4b17023SJohn Marino 	      if (integer_zerop (init))
914e4b17023SJohn Marino 		DECL_PURE_VIRTUAL_P (value) = 1;
915e4b17023SJohn Marino 	      else if (error_operand_p (init))
916e4b17023SJohn Marino 		; /* An error has already been reported.  */
917e4b17023SJohn Marino 	      else
918e4b17023SJohn Marino 		error ("invalid initializer for member function %qD",
919e4b17023SJohn Marino 		       value);
920e4b17023SJohn Marino 	    }
921e4b17023SJohn Marino 	  else
922e4b17023SJohn Marino 	    {
923e4b17023SJohn Marino 	      gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
924e4b17023SJohn Marino 	      error ("initializer specified for static member function %qD",
925e4b17023SJohn Marino 		     value);
926e4b17023SJohn Marino 	    }
927e4b17023SJohn Marino 	}
928e4b17023SJohn Marino       else if (TREE_CODE (value) == FIELD_DECL)
929e4b17023SJohn Marino 	/* C++11 NSDMI, keep going.  */;
930e4b17023SJohn Marino       else if (TREE_CODE (value) != VAR_DECL)
931e4b17023SJohn Marino 	gcc_unreachable ();
932e4b17023SJohn Marino       else if (!processing_template_decl)
933e4b17023SJohn Marino 	{
934e4b17023SJohn Marino 	  if (TREE_CODE (init) == CONSTRUCTOR)
935e4b17023SJohn Marino 	    init = digest_init (TREE_TYPE (value), init, tf_warning_or_error);
936e4b17023SJohn Marino 	  init = maybe_constant_init (init);
937e4b17023SJohn Marino 
938e4b17023SJohn Marino 	  if (init != error_mark_node && !TREE_CONSTANT (init))
939e4b17023SJohn Marino 	    {
940e4b17023SJohn Marino 	      /* We can allow references to things that are effectively
941e4b17023SJohn Marino 		 static, since references are initialized with the
942e4b17023SJohn Marino 		 address.  */
943e4b17023SJohn Marino 	      if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
944e4b17023SJohn Marino 		  || (TREE_STATIC (init) == 0
945e4b17023SJohn Marino 		      && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
946e4b17023SJohn Marino 		{
947e4b17023SJohn Marino 		  error ("field initializer is not constant");
948e4b17023SJohn Marino 		  init = error_mark_node;
949e4b17023SJohn Marino 		}
950e4b17023SJohn Marino 	    }
951e4b17023SJohn Marino 	}
952e4b17023SJohn Marino     }
953e4b17023SJohn Marino 
954e4b17023SJohn Marino   if (processing_template_decl
955e4b17023SJohn Marino       && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
956e4b17023SJohn Marino     {
957e4b17023SJohn Marino       value = push_template_decl (value);
958e4b17023SJohn Marino       if (error_operand_p (value))
959e4b17023SJohn Marino 	return error_mark_node;
960e4b17023SJohn Marino     }
961e4b17023SJohn Marino 
962e4b17023SJohn Marino   if (attrlist)
963e4b17023SJohn Marino     cplus_decl_attributes (&value, attrlist, 0);
964e4b17023SJohn Marino 
965e4b17023SJohn Marino   if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
966e4b17023SJohn Marino       && CONSTRUCTOR_IS_DIRECT_INIT (init))
967e4b17023SJohn Marino     flags = LOOKUP_NORMAL;
968e4b17023SJohn Marino   else
969e4b17023SJohn Marino     flags = LOOKUP_IMPLICIT;
970e4b17023SJohn Marino 
971e4b17023SJohn Marino   switch (TREE_CODE (value))
972e4b17023SJohn Marino     {
973e4b17023SJohn Marino     case VAR_DECL:
974e4b17023SJohn Marino       finish_static_data_member_decl (value, init, init_const_expr_p,
975e4b17023SJohn Marino 				      asmspec_tree, flags);
976e4b17023SJohn Marino       return value;
977e4b17023SJohn Marino 
978e4b17023SJohn Marino     case FIELD_DECL:
979e4b17023SJohn Marino       if (asmspec)
980e4b17023SJohn Marino 	error ("%<asm%> specifiers are not permitted on non-static data members");
981e4b17023SJohn Marino       if (DECL_INITIAL (value) == error_mark_node)
982e4b17023SJohn Marino 	init = error_mark_node;
983e4b17023SJohn Marino       cp_finish_decl (value, init, /*init_const_expr_p=*/false,
984e4b17023SJohn Marino 		      NULL_TREE, flags);
985e4b17023SJohn Marino       DECL_IN_AGGR_P (value) = 1;
986e4b17023SJohn Marino       return value;
987e4b17023SJohn Marino 
988e4b17023SJohn Marino     case  FUNCTION_DECL:
989e4b17023SJohn Marino       if (asmspec)
990e4b17023SJohn Marino 	set_user_assembler_name (value, asmspec);
991e4b17023SJohn Marino 
992e4b17023SJohn Marino       cp_finish_decl (value,
993e4b17023SJohn Marino 		      /*init=*/NULL_TREE,
994e4b17023SJohn Marino 		      /*init_const_expr_p=*/false,
995e4b17023SJohn Marino 		      asmspec_tree, flags);
996e4b17023SJohn Marino 
997e4b17023SJohn Marino       /* Pass friends back this way.  */
998e4b17023SJohn Marino       if (DECL_FRIEND_P (value))
999e4b17023SJohn Marino 	return void_type_node;
1000e4b17023SJohn Marino 
1001e4b17023SJohn Marino       DECL_IN_AGGR_P (value) = 1;
1002e4b17023SJohn Marino       return value;
1003e4b17023SJohn Marino 
1004e4b17023SJohn Marino     default:
1005e4b17023SJohn Marino       gcc_unreachable ();
1006e4b17023SJohn Marino     }
1007e4b17023SJohn Marino   return NULL_TREE;
1008e4b17023SJohn Marino }
1009e4b17023SJohn Marino 
1010e4b17023SJohn Marino /* Like `grokfield', but for bitfields.
1011e4b17023SJohn Marino    WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.  */
1012e4b17023SJohn Marino 
1013e4b17023SJohn Marino tree
grokbitfield(const cp_declarator * declarator,cp_decl_specifier_seq * declspecs,tree width,tree attrlist)1014e4b17023SJohn Marino grokbitfield (const cp_declarator *declarator,
1015e4b17023SJohn Marino 	      cp_decl_specifier_seq *declspecs, tree width,
1016e4b17023SJohn Marino 	      tree attrlist)
1017e4b17023SJohn Marino {
1018e4b17023SJohn Marino   tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
1019e4b17023SJohn Marino 
1020e4b17023SJohn Marino   if (value == error_mark_node)
1021e4b17023SJohn Marino     return NULL_TREE; /* friends went bad.  */
1022e4b17023SJohn Marino 
1023e4b17023SJohn Marino   /* Pass friendly classes back.  */
1024e4b17023SJohn Marino   if (TREE_CODE (value) == VOID_TYPE)
1025e4b17023SJohn Marino     return void_type_node;
1026e4b17023SJohn Marino 
1027e4b17023SJohn Marino   if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))
1028e4b17023SJohn Marino       && (POINTER_TYPE_P (value)
1029e4b17023SJohn Marino           || !dependent_type_p (TREE_TYPE (value))))
1030e4b17023SJohn Marino     {
1031e4b17023SJohn Marino       error ("bit-field %qD with non-integral type", value);
1032e4b17023SJohn Marino       return error_mark_node;
1033e4b17023SJohn Marino     }
1034e4b17023SJohn Marino 
1035e4b17023SJohn Marino   if (TREE_CODE (value) == TYPE_DECL)
1036e4b17023SJohn Marino     {
1037e4b17023SJohn Marino       error ("cannot declare %qD to be a bit-field type", value);
1038e4b17023SJohn Marino       return NULL_TREE;
1039e4b17023SJohn Marino     }
1040e4b17023SJohn Marino 
1041e4b17023SJohn Marino   /* Usually, finish_struct_1 catches bitfields with invalid types.
1042e4b17023SJohn Marino      But, in the case of bitfields with function type, we confuse
1043e4b17023SJohn Marino      ourselves into thinking they are member functions, so we must
1044e4b17023SJohn Marino      check here.  */
1045e4b17023SJohn Marino   if (TREE_CODE (value) == FUNCTION_DECL)
1046e4b17023SJohn Marino     {
1047e4b17023SJohn Marino       error ("cannot declare bit-field %qD with function type",
1048e4b17023SJohn Marino 	     DECL_NAME (value));
1049e4b17023SJohn Marino       return NULL_TREE;
1050e4b17023SJohn Marino     }
1051e4b17023SJohn Marino 
1052e4b17023SJohn Marino   if (DECL_IN_AGGR_P (value))
1053e4b17023SJohn Marino     {
1054e4b17023SJohn Marino       error ("%qD is already defined in the class %qT", value,
1055e4b17023SJohn Marino 	     DECL_CONTEXT (value));
1056e4b17023SJohn Marino       return void_type_node;
1057e4b17023SJohn Marino     }
1058e4b17023SJohn Marino 
1059e4b17023SJohn Marino   if (TREE_STATIC (value))
1060e4b17023SJohn Marino     {
1061e4b17023SJohn Marino       error ("static member %qD cannot be a bit-field", value);
1062e4b17023SJohn Marino       return NULL_TREE;
1063e4b17023SJohn Marino     }
1064e4b17023SJohn Marino   cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
1065e4b17023SJohn Marino 
1066e4b17023SJohn Marino   if (width != error_mark_node)
1067e4b17023SJohn Marino     {
1068e4b17023SJohn Marino       /* The width must be an integer type.  */
1069e4b17023SJohn Marino       if (!type_dependent_expression_p (width)
1070e4b17023SJohn Marino 	  && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
1071e4b17023SJohn Marino 	error ("width of bit-field %qD has non-integral type %qT", value,
1072e4b17023SJohn Marino 	       TREE_TYPE (width));
1073e4b17023SJohn Marino       DECL_INITIAL (value) = width;
1074e4b17023SJohn Marino       SET_DECL_C_BIT_FIELD (value);
1075e4b17023SJohn Marino     }
1076e4b17023SJohn Marino 
1077e4b17023SJohn Marino   DECL_IN_AGGR_P (value) = 1;
1078e4b17023SJohn Marino 
1079e4b17023SJohn Marino   if (attrlist)
1080e4b17023SJohn Marino     cplus_decl_attributes (&value, attrlist, /*flags=*/0);
1081e4b17023SJohn Marino 
1082e4b17023SJohn Marino   return value;
1083e4b17023SJohn Marino }
1084e4b17023SJohn Marino 
1085e4b17023SJohn Marino 
1086e4b17023SJohn Marino /* Returns true iff ATTR is an attribute which needs to be applied at
1087e4b17023SJohn Marino    instantiation time rather than template definition time.  */
1088e4b17023SJohn Marino 
1089e4b17023SJohn Marino static bool
is_late_template_attribute(tree attr,tree decl)1090e4b17023SJohn Marino is_late_template_attribute (tree attr, tree decl)
1091e4b17023SJohn Marino {
1092e4b17023SJohn Marino   tree name = TREE_PURPOSE (attr);
1093e4b17023SJohn Marino   tree args = TREE_VALUE (attr);
1094e4b17023SJohn Marino   const struct attribute_spec *spec = lookup_attribute_spec (name);
1095e4b17023SJohn Marino   tree arg;
1096e4b17023SJohn Marino 
1097e4b17023SJohn Marino   if (!spec)
1098e4b17023SJohn Marino     /* Unknown attribute.  */
1099e4b17023SJohn Marino     return false;
1100e4b17023SJohn Marino 
1101e4b17023SJohn Marino   /* Attribute weak handling wants to write out assembly right away.  */
1102e4b17023SJohn Marino   if (is_attribute_p ("weak", name))
1103e4b17023SJohn Marino     return true;
1104e4b17023SJohn Marino 
1105e4b17023SJohn Marino   /* If any of the arguments are dependent expressions, we can't evaluate
1106e4b17023SJohn Marino      the attribute until instantiation time.  */
1107e4b17023SJohn Marino   for (arg = args; arg; arg = TREE_CHAIN (arg))
1108e4b17023SJohn Marino     {
1109e4b17023SJohn Marino       tree t = TREE_VALUE (arg);
1110e4b17023SJohn Marino 
1111e4b17023SJohn Marino       /* If the first attribute argument is an identifier, only consider
1112e4b17023SJohn Marino 	 second and following arguments.  Attributes like mode, format,
1113e4b17023SJohn Marino 	 cleanup and several target specific attributes aren't late
1114e4b17023SJohn Marino 	 just because they have an IDENTIFIER_NODE as first argument.  */
1115e4b17023SJohn Marino       if (arg == args && TREE_CODE (t) == IDENTIFIER_NODE)
1116e4b17023SJohn Marino 	continue;
1117e4b17023SJohn Marino 
1118e4b17023SJohn Marino       if (value_dependent_expression_p (t)
1119e4b17023SJohn Marino 	  || type_dependent_expression_p (t))
1120e4b17023SJohn Marino 	return true;
1121e4b17023SJohn Marino     }
1122e4b17023SJohn Marino 
1123e4b17023SJohn Marino   if (TREE_CODE (decl) == TYPE_DECL
1124e4b17023SJohn Marino       || TYPE_P (decl)
1125e4b17023SJohn Marino       || spec->type_required)
1126e4b17023SJohn Marino     {
1127e4b17023SJohn Marino       tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
1128e4b17023SJohn Marino 
1129e4b17023SJohn Marino       /* We can't apply any attributes to a completely unknown type until
1130e4b17023SJohn Marino 	 instantiation time.  */
1131e4b17023SJohn Marino       enum tree_code code = TREE_CODE (type);
1132e4b17023SJohn Marino       if (code == TEMPLATE_TYPE_PARM
1133e4b17023SJohn Marino 	  || code == BOUND_TEMPLATE_TEMPLATE_PARM
1134e4b17023SJohn Marino 	  || code == TYPENAME_TYPE)
1135e4b17023SJohn Marino 	return true;
1136e4b17023SJohn Marino       /* Also defer most attributes on dependent types.  This is not
1137e4b17023SJohn Marino 	 necessary in all cases, but is the better default.  */
1138e4b17023SJohn Marino       else if (dependent_type_p (type)
1139e4b17023SJohn Marino 	       /* But attribute visibility specifically works on
1140e4b17023SJohn Marino 		  templates.  */
1141e4b17023SJohn Marino 	       && !is_attribute_p ("visibility", name))
1142e4b17023SJohn Marino 	return true;
1143e4b17023SJohn Marino       else
1144e4b17023SJohn Marino 	return false;
1145e4b17023SJohn Marino     }
1146e4b17023SJohn Marino   else
1147e4b17023SJohn Marino     return false;
1148e4b17023SJohn Marino }
1149e4b17023SJohn Marino 
1150e4b17023SJohn Marino /* ATTR_P is a list of attributes.  Remove any attributes which need to be
1151e4b17023SJohn Marino    applied at instantiation time and return them.  If IS_DEPENDENT is true,
1152e4b17023SJohn Marino    the declaration itself is dependent, so all attributes should be applied
1153e4b17023SJohn Marino    at instantiation time.  */
1154e4b17023SJohn Marino 
1155e4b17023SJohn Marino static tree
splice_template_attributes(tree * attr_p,tree decl)1156e4b17023SJohn Marino splice_template_attributes (tree *attr_p, tree decl)
1157e4b17023SJohn Marino {
1158e4b17023SJohn Marino   tree *p = attr_p;
1159e4b17023SJohn Marino   tree late_attrs = NULL_TREE;
1160e4b17023SJohn Marino   tree *q = &late_attrs;
1161e4b17023SJohn Marino 
1162e4b17023SJohn Marino   if (!p)
1163e4b17023SJohn Marino     return NULL_TREE;
1164e4b17023SJohn Marino 
1165e4b17023SJohn Marino   for (; *p; )
1166e4b17023SJohn Marino     {
1167e4b17023SJohn Marino       if (is_late_template_attribute (*p, decl))
1168e4b17023SJohn Marino 	{
1169e4b17023SJohn Marino 	  ATTR_IS_DEPENDENT (*p) = 1;
1170e4b17023SJohn Marino 	  *q = *p;
1171e4b17023SJohn Marino 	  *p = TREE_CHAIN (*p);
1172e4b17023SJohn Marino 	  q = &TREE_CHAIN (*q);
1173e4b17023SJohn Marino 	  *q = NULL_TREE;
1174e4b17023SJohn Marino 	}
1175e4b17023SJohn Marino       else
1176e4b17023SJohn Marino 	p = &TREE_CHAIN (*p);
1177e4b17023SJohn Marino     }
1178e4b17023SJohn Marino 
1179e4b17023SJohn Marino   return late_attrs;
1180e4b17023SJohn Marino }
1181e4b17023SJohn Marino 
1182e4b17023SJohn Marino /* Remove any late attributes from the list in ATTR_P and attach them to
1183e4b17023SJohn Marino    DECL_P.  */
1184e4b17023SJohn Marino 
1185e4b17023SJohn Marino static void
save_template_attributes(tree * attr_p,tree * decl_p)1186e4b17023SJohn Marino save_template_attributes (tree *attr_p, tree *decl_p)
1187e4b17023SJohn Marino {
1188e4b17023SJohn Marino   tree late_attrs = splice_template_attributes (attr_p, *decl_p);
1189e4b17023SJohn Marino   tree *q;
1190e4b17023SJohn Marino   tree old_attrs = NULL_TREE;
1191e4b17023SJohn Marino 
1192e4b17023SJohn Marino   if (!late_attrs)
1193e4b17023SJohn Marino     return;
1194e4b17023SJohn Marino 
1195e4b17023SJohn Marino   if (DECL_P (*decl_p))
1196e4b17023SJohn Marino     q = &DECL_ATTRIBUTES (*decl_p);
1197e4b17023SJohn Marino   else
1198e4b17023SJohn Marino     q = &TYPE_ATTRIBUTES (*decl_p);
1199e4b17023SJohn Marino 
1200e4b17023SJohn Marino   old_attrs = *q;
1201e4b17023SJohn Marino 
1202e4b17023SJohn Marino   /* Merge the late attributes at the beginning with the attribute
1203e4b17023SJohn Marino      list.  */
1204e4b17023SJohn Marino   late_attrs = merge_attributes (late_attrs, *q);
1205e4b17023SJohn Marino   *q = late_attrs;
1206e4b17023SJohn Marino 
1207e4b17023SJohn Marino   if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
1208e4b17023SJohn Marino     {
1209e4b17023SJohn Marino       /* We've added new attributes directly to the main variant, so
1210e4b17023SJohn Marino 	 now we need to update all of the other variants to include
1211e4b17023SJohn Marino 	 these new attributes.  */
1212e4b17023SJohn Marino       tree variant;
1213e4b17023SJohn Marino       for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
1214e4b17023SJohn Marino 	   variant = TYPE_NEXT_VARIANT (variant))
1215e4b17023SJohn Marino 	{
1216e4b17023SJohn Marino 	  gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
1217e4b17023SJohn Marino 	  TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
1218e4b17023SJohn Marino 	}
1219e4b17023SJohn Marino     }
1220e4b17023SJohn Marino }
1221e4b17023SJohn Marino 
1222e4b17023SJohn Marino /* Like reconstruct_complex_type, but handle also template trees.  */
1223e4b17023SJohn Marino 
1224e4b17023SJohn Marino tree
cp_reconstruct_complex_type(tree type,tree bottom)1225e4b17023SJohn Marino cp_reconstruct_complex_type (tree type, tree bottom)
1226e4b17023SJohn Marino {
1227e4b17023SJohn Marino   tree inner, outer;
1228e4b17023SJohn Marino 
1229e4b17023SJohn Marino   if (TREE_CODE (type) == POINTER_TYPE)
1230e4b17023SJohn Marino     {
1231e4b17023SJohn Marino       inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1232e4b17023SJohn Marino       outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
1233e4b17023SJohn Marino 					   TYPE_REF_CAN_ALIAS_ALL (type));
1234e4b17023SJohn Marino     }
1235e4b17023SJohn Marino   else if (TREE_CODE (type) == REFERENCE_TYPE)
1236e4b17023SJohn Marino     {
1237e4b17023SJohn Marino       inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1238e4b17023SJohn Marino       outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
1239e4b17023SJohn Marino 					     TYPE_REF_CAN_ALIAS_ALL (type));
1240e4b17023SJohn Marino     }
1241e4b17023SJohn Marino   else if (TREE_CODE (type) == ARRAY_TYPE)
1242e4b17023SJohn Marino     {
1243e4b17023SJohn Marino       inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1244e4b17023SJohn Marino       outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
1245e4b17023SJohn Marino       /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
1246e4b17023SJohn Marino 	 element type qualification will be handled by the recursive
1247e4b17023SJohn Marino 	 cp_reconstruct_complex_type call and cp_build_qualified_type
1248e4b17023SJohn Marino 	 for ARRAY_TYPEs changes the element type.  */
1249e4b17023SJohn Marino       return outer;
1250e4b17023SJohn Marino     }
1251e4b17023SJohn Marino   else if (TREE_CODE (type) == FUNCTION_TYPE)
1252e4b17023SJohn Marino     {
1253e4b17023SJohn Marino       inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1254e4b17023SJohn Marino       outer = build_function_type (inner, TYPE_ARG_TYPES (type));
1255e4b17023SJohn Marino       outer = apply_memfn_quals (outer, type_memfn_quals (type));
1256e4b17023SJohn Marino     }
1257e4b17023SJohn Marino   else if (TREE_CODE (type) == METHOD_TYPE)
1258e4b17023SJohn Marino     {
1259e4b17023SJohn Marino       inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1260e4b17023SJohn Marino       /* The build_method_type_directly() routine prepends 'this' to argument list,
1261e4b17023SJohn Marino 	 so we must compensate by getting rid of it.  */
1262e4b17023SJohn Marino       outer
1263e4b17023SJohn Marino 	= build_method_type_directly
1264e4b17023SJohn Marino 	    (class_of_this_parm (type), inner,
1265e4b17023SJohn Marino 	     TREE_CHAIN (TYPE_ARG_TYPES (type)));
1266e4b17023SJohn Marino     }
1267e4b17023SJohn Marino   else if (TREE_CODE (type) == OFFSET_TYPE)
1268e4b17023SJohn Marino     {
1269e4b17023SJohn Marino       inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
1270e4b17023SJohn Marino       outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
1271e4b17023SJohn Marino     }
1272e4b17023SJohn Marino   else
1273e4b17023SJohn Marino     return bottom;
1274e4b17023SJohn Marino 
1275e4b17023SJohn Marino   if (TYPE_ATTRIBUTES (type))
1276e4b17023SJohn Marino     outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
1277e4b17023SJohn Marino   return cp_build_qualified_type (outer, cp_type_quals (type));
1278e4b17023SJohn Marino }
1279e4b17023SJohn Marino 
1280e4b17023SJohn Marino /* Replaces any constexpr expression that may be into the attributes
1281e4b17023SJohn Marino    arguments with their reduced value.  */
1282e4b17023SJohn Marino 
1283e4b17023SJohn Marino static void
cp_check_const_attributes(tree attributes)1284e4b17023SJohn Marino cp_check_const_attributes (tree attributes)
1285e4b17023SJohn Marino {
1286e4b17023SJohn Marino   tree attr;
1287e4b17023SJohn Marino   for (attr = attributes; attr; attr = TREE_CHAIN (attr))
1288e4b17023SJohn Marino     {
1289e4b17023SJohn Marino       tree arg;
1290e4b17023SJohn Marino       for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
1291e4b17023SJohn Marino 	{
1292e4b17023SJohn Marino 	  tree expr = TREE_VALUE (arg);
1293e4b17023SJohn Marino 	  if (EXPR_P (expr))
1294e4b17023SJohn Marino 	    TREE_VALUE (arg) = maybe_constant_value (expr);
1295e4b17023SJohn Marino 	}
1296e4b17023SJohn Marino     }
1297e4b17023SJohn Marino }
1298e4b17023SJohn Marino 
1299e4b17023SJohn Marino /* Like decl_attributes, but handle C++ complexity.  */
1300e4b17023SJohn Marino 
1301e4b17023SJohn Marino void
cplus_decl_attributes(tree * decl,tree attributes,int flags)1302e4b17023SJohn Marino cplus_decl_attributes (tree *decl, tree attributes, int flags)
1303e4b17023SJohn Marino {
1304e4b17023SJohn Marino   if (*decl == NULL_TREE || *decl == void_type_node
1305*5ce9237cSJohn Marino       || *decl == error_mark_node)
1306e4b17023SJohn Marino     return;
1307e4b17023SJohn Marino 
1308e4b17023SJohn Marino   if (processing_template_decl)
1309e4b17023SJohn Marino     {
1310e4b17023SJohn Marino       if (check_for_bare_parameter_packs (attributes))
1311e4b17023SJohn Marino 	return;
1312e4b17023SJohn Marino 
1313e4b17023SJohn Marino       save_template_attributes (&attributes, decl);
1314e4b17023SJohn Marino     }
1315e4b17023SJohn Marino 
1316e4b17023SJohn Marino   cp_check_const_attributes (attributes);
1317e4b17023SJohn Marino 
1318e4b17023SJohn Marino   if (TREE_CODE (*decl) == TEMPLATE_DECL)
1319e4b17023SJohn Marino     decl = &DECL_TEMPLATE_RESULT (*decl);
1320e4b17023SJohn Marino 
1321e4b17023SJohn Marino   decl_attributes (decl, attributes, flags);
1322e4b17023SJohn Marino 
1323e4b17023SJohn Marino   if (TREE_CODE (*decl) == TYPE_DECL)
1324e4b17023SJohn Marino     SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1325e4b17023SJohn Marino }
1326e4b17023SJohn Marino 
1327e4b17023SJohn Marino /* Walks through the namespace- or function-scope anonymous union
1328e4b17023SJohn Marino    OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
1329e4b17023SJohn Marino    Returns one of the fields for use in the mangled name.  */
1330e4b17023SJohn Marino 
1331e4b17023SJohn Marino static tree
build_anon_union_vars(tree type,tree object)1332e4b17023SJohn Marino build_anon_union_vars (tree type, tree object)
1333e4b17023SJohn Marino {
1334e4b17023SJohn Marino   tree main_decl = NULL_TREE;
1335e4b17023SJohn Marino   tree field;
1336e4b17023SJohn Marino 
1337e4b17023SJohn Marino   /* Rather than write the code to handle the non-union case,
1338e4b17023SJohn Marino      just give an error.  */
1339e4b17023SJohn Marino   if (TREE_CODE (type) != UNION_TYPE)
1340e4b17023SJohn Marino     {
1341e4b17023SJohn Marino       error ("anonymous struct not inside named type");
1342e4b17023SJohn Marino       return error_mark_node;
1343e4b17023SJohn Marino     }
1344e4b17023SJohn Marino 
1345e4b17023SJohn Marino   for (field = TYPE_FIELDS (type);
1346e4b17023SJohn Marino        field != NULL_TREE;
1347e4b17023SJohn Marino        field = DECL_CHAIN (field))
1348e4b17023SJohn Marino     {
1349e4b17023SJohn Marino       tree decl;
1350e4b17023SJohn Marino       tree ref;
1351e4b17023SJohn Marino 
1352e4b17023SJohn Marino       if (DECL_ARTIFICIAL (field))
1353e4b17023SJohn Marino 	continue;
1354e4b17023SJohn Marino       if (TREE_CODE (field) != FIELD_DECL)
1355e4b17023SJohn Marino 	{
1356e4b17023SJohn Marino 	  permerror (input_location, "%q+#D invalid; an anonymous union can only "
1357e4b17023SJohn Marino 		     "have non-static data members", field);
1358e4b17023SJohn Marino 	  continue;
1359e4b17023SJohn Marino 	}
1360e4b17023SJohn Marino 
1361e4b17023SJohn Marino       if (TREE_PRIVATE (field))
1362e4b17023SJohn Marino 	permerror (input_location, "private member %q+#D in anonymous union", field);
1363e4b17023SJohn Marino       else if (TREE_PROTECTED (field))
1364e4b17023SJohn Marino 	permerror (input_location, "protected member %q+#D in anonymous union", field);
1365e4b17023SJohn Marino 
1366e4b17023SJohn Marino       if (processing_template_decl)
1367e4b17023SJohn Marino 	ref = build_min_nt (COMPONENT_REF, object,
1368e4b17023SJohn Marino 			    DECL_NAME (field), NULL_TREE);
1369e4b17023SJohn Marino       else
1370e4b17023SJohn Marino 	ref = build_class_member_access_expr (object, field, NULL_TREE,
1371e4b17023SJohn Marino 					      false, tf_warning_or_error);
1372e4b17023SJohn Marino 
1373e4b17023SJohn Marino       if (DECL_NAME (field))
1374e4b17023SJohn Marino 	{
1375e4b17023SJohn Marino 	  tree base;
1376e4b17023SJohn Marino 
1377e4b17023SJohn Marino 	  decl = build_decl (input_location,
1378e4b17023SJohn Marino 			     VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1379e4b17023SJohn Marino 	  DECL_ANON_UNION_VAR_P (decl) = 1;
1380e4b17023SJohn Marino 	  DECL_ARTIFICIAL (decl) = 1;
1381e4b17023SJohn Marino 
1382e4b17023SJohn Marino 	  base = get_base_address (object);
1383e4b17023SJohn Marino 	  TREE_PUBLIC (decl) = TREE_PUBLIC (base);
1384e4b17023SJohn Marino 	  TREE_STATIC (decl) = TREE_STATIC (base);
1385e4b17023SJohn Marino 	  DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
1386e4b17023SJohn Marino 
1387e4b17023SJohn Marino 	  SET_DECL_VALUE_EXPR (decl, ref);
1388e4b17023SJohn Marino 	  DECL_HAS_VALUE_EXPR_P (decl) = 1;
1389e4b17023SJohn Marino 
1390e4b17023SJohn Marino 	  decl = pushdecl (decl);
1391e4b17023SJohn Marino 	}
1392e4b17023SJohn Marino       else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1393e4b17023SJohn Marino 	decl = build_anon_union_vars (TREE_TYPE (field), ref);
1394e4b17023SJohn Marino       else
1395e4b17023SJohn Marino 	decl = 0;
1396e4b17023SJohn Marino 
1397e4b17023SJohn Marino       if (main_decl == NULL_TREE)
1398e4b17023SJohn Marino 	main_decl = decl;
1399e4b17023SJohn Marino     }
1400e4b17023SJohn Marino 
1401e4b17023SJohn Marino   return main_decl;
1402e4b17023SJohn Marino }
1403e4b17023SJohn Marino 
1404e4b17023SJohn Marino /* Finish off the processing of a UNION_TYPE structure.  If the union is an
1405e4b17023SJohn Marino    anonymous union, then all members must be laid out together.  PUBLIC_P
1406e4b17023SJohn Marino    is nonzero if this union is not declared static.  */
1407e4b17023SJohn Marino 
1408e4b17023SJohn Marino void
finish_anon_union(tree anon_union_decl)1409e4b17023SJohn Marino finish_anon_union (tree anon_union_decl)
1410e4b17023SJohn Marino {
1411e4b17023SJohn Marino   tree type;
1412e4b17023SJohn Marino   tree main_decl;
1413e4b17023SJohn Marino   bool public_p;
1414e4b17023SJohn Marino 
1415e4b17023SJohn Marino   if (anon_union_decl == error_mark_node)
1416e4b17023SJohn Marino     return;
1417e4b17023SJohn Marino 
1418e4b17023SJohn Marino   type = TREE_TYPE (anon_union_decl);
1419e4b17023SJohn Marino   public_p = TREE_PUBLIC (anon_union_decl);
1420e4b17023SJohn Marino 
1421e4b17023SJohn Marino   /* The VAR_DECL's context is the same as the TYPE's context.  */
1422e4b17023SJohn Marino   DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1423e4b17023SJohn Marino 
1424e4b17023SJohn Marino   if (TYPE_FIELDS (type) == NULL_TREE)
1425e4b17023SJohn Marino     return;
1426e4b17023SJohn Marino 
1427e4b17023SJohn Marino   if (public_p)
1428e4b17023SJohn Marino     {
1429e4b17023SJohn Marino       error ("namespace-scope anonymous aggregates must be static");
1430e4b17023SJohn Marino       return;
1431e4b17023SJohn Marino     }
1432e4b17023SJohn Marino 
1433e4b17023SJohn Marino   main_decl = build_anon_union_vars (type, anon_union_decl);
1434e4b17023SJohn Marino   if (main_decl == error_mark_node)
1435e4b17023SJohn Marino     return;
1436e4b17023SJohn Marino   if (main_decl == NULL_TREE)
1437e4b17023SJohn Marino     {
1438e4b17023SJohn Marino       warning (0, "anonymous union with no members");
1439e4b17023SJohn Marino       return;
1440e4b17023SJohn Marino     }
1441e4b17023SJohn Marino 
1442e4b17023SJohn Marino   if (!processing_template_decl)
1443e4b17023SJohn Marino     {
1444e4b17023SJohn Marino       /* Use main_decl to set the mangled name.  */
1445e4b17023SJohn Marino       DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
1446e4b17023SJohn Marino       maybe_commonize_var (anon_union_decl);
1447e4b17023SJohn Marino       if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
1448e4b17023SJohn Marino 	mangle_decl (anon_union_decl);
1449e4b17023SJohn Marino       DECL_NAME (anon_union_decl) = NULL_TREE;
1450e4b17023SJohn Marino     }
1451e4b17023SJohn Marino 
1452e4b17023SJohn Marino   pushdecl (anon_union_decl);
1453e4b17023SJohn Marino   if (building_stmt_list_p ()
1454e4b17023SJohn Marino       && at_function_scope_p ())
1455e4b17023SJohn Marino     add_decl_expr (anon_union_decl);
1456e4b17023SJohn Marino   else if (!processing_template_decl)
1457e4b17023SJohn Marino     rest_of_decl_compilation (anon_union_decl,
1458e4b17023SJohn Marino 			      toplevel_bindings_p (), at_eof);
1459e4b17023SJohn Marino }
1460e4b17023SJohn Marino 
1461e4b17023SJohn Marino /* Auxiliary functions to make type signatures for
1462e4b17023SJohn Marino    `operator new' and `operator delete' correspond to
1463e4b17023SJohn Marino    what compiler will be expecting.  */
1464e4b17023SJohn Marino 
1465e4b17023SJohn Marino tree
coerce_new_type(tree type)1466e4b17023SJohn Marino coerce_new_type (tree type)
1467e4b17023SJohn Marino {
1468e4b17023SJohn Marino   int e = 0;
1469e4b17023SJohn Marino   tree args = TYPE_ARG_TYPES (type);
1470e4b17023SJohn Marino 
1471e4b17023SJohn Marino   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1472e4b17023SJohn Marino 
1473e4b17023SJohn Marino   if (!same_type_p (TREE_TYPE (type), ptr_type_node))
1474e4b17023SJohn Marino     {
1475e4b17023SJohn Marino       e = 1;
1476e4b17023SJohn Marino       error ("%<operator new%> must return type %qT", ptr_type_node);
1477e4b17023SJohn Marino     }
1478e4b17023SJohn Marino 
1479e4b17023SJohn Marino   if (args && args != void_list_node)
1480e4b17023SJohn Marino     {
1481e4b17023SJohn Marino       if (TREE_PURPOSE (args))
1482e4b17023SJohn Marino 	{
1483e4b17023SJohn Marino 	  /* [basic.stc.dynamic.allocation]
1484e4b17023SJohn Marino 
1485e4b17023SJohn Marino 	     The first parameter shall not have an associated default
1486e4b17023SJohn Marino 	     argument.  */
1487e4b17023SJohn Marino 	  error ("the first parameter of %<operator new%> cannot "
1488e4b17023SJohn Marino 		 "have a default argument");
1489e4b17023SJohn Marino 	  /* Throw away the default argument.  */
1490e4b17023SJohn Marino 	  TREE_PURPOSE (args) = NULL_TREE;
1491e4b17023SJohn Marino 	}
1492e4b17023SJohn Marino 
1493e4b17023SJohn Marino       if (!same_type_p (TREE_VALUE (args), size_type_node))
1494e4b17023SJohn Marino 	{
1495e4b17023SJohn Marino 	  e = 2;
1496e4b17023SJohn Marino 	  args = TREE_CHAIN (args);
1497e4b17023SJohn Marino 	}
1498e4b17023SJohn Marino     }
1499e4b17023SJohn Marino   else
1500e4b17023SJohn Marino     e = 2;
1501e4b17023SJohn Marino 
1502e4b17023SJohn Marino   if (e == 2)
1503e4b17023SJohn Marino     permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
1504e4b17023SJohn Marino 	       "as first parameter", size_type_node);
1505e4b17023SJohn Marino 
1506e4b17023SJohn Marino   switch (e)
1507e4b17023SJohn Marino   {
1508e4b17023SJohn Marino     case 2:
1509e4b17023SJohn Marino       args = tree_cons (NULL_TREE, size_type_node, args);
1510e4b17023SJohn Marino       /* Fall through.  */
1511e4b17023SJohn Marino     case 1:
1512e4b17023SJohn Marino       type = build_exception_variant
1513e4b17023SJohn Marino 	      (build_function_type (ptr_type_node, args),
1514e4b17023SJohn Marino 	       TYPE_RAISES_EXCEPTIONS (type));
1515e4b17023SJohn Marino       /* Fall through.  */
1516e4b17023SJohn Marino     default:;
1517e4b17023SJohn Marino   }
1518e4b17023SJohn Marino   return type;
1519e4b17023SJohn Marino }
1520e4b17023SJohn Marino 
1521e4b17023SJohn Marino tree
coerce_delete_type(tree type)1522e4b17023SJohn Marino coerce_delete_type (tree type)
1523e4b17023SJohn Marino {
1524e4b17023SJohn Marino   int e = 0;
1525e4b17023SJohn Marino   tree args = TYPE_ARG_TYPES (type);
1526e4b17023SJohn Marino 
1527e4b17023SJohn Marino   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
1528e4b17023SJohn Marino 
1529e4b17023SJohn Marino   if (!same_type_p (TREE_TYPE (type), void_type_node))
1530e4b17023SJohn Marino     {
1531e4b17023SJohn Marino       e = 1;
1532e4b17023SJohn Marino       error ("%<operator delete%> must return type %qT", void_type_node);
1533e4b17023SJohn Marino     }
1534e4b17023SJohn Marino 
1535e4b17023SJohn Marino   if (!args || args == void_list_node
1536e4b17023SJohn Marino       || !same_type_p (TREE_VALUE (args), ptr_type_node))
1537e4b17023SJohn Marino     {
1538e4b17023SJohn Marino       e = 2;
1539e4b17023SJohn Marino       if (args && args != void_list_node)
1540e4b17023SJohn Marino 	args = TREE_CHAIN (args);
1541e4b17023SJohn Marino       error ("%<operator delete%> takes type %qT as first parameter",
1542e4b17023SJohn Marino 	     ptr_type_node);
1543e4b17023SJohn Marino     }
1544e4b17023SJohn Marino   switch (e)
1545e4b17023SJohn Marino   {
1546e4b17023SJohn Marino     case 2:
1547e4b17023SJohn Marino       args = tree_cons (NULL_TREE, ptr_type_node, args);
1548e4b17023SJohn Marino       /* Fall through.  */
1549e4b17023SJohn Marino     case 1:
1550e4b17023SJohn Marino       type = build_exception_variant
1551e4b17023SJohn Marino 	      (build_function_type (void_type_node, args),
1552e4b17023SJohn Marino 	       TYPE_RAISES_EXCEPTIONS (type));
1553e4b17023SJohn Marino       /* Fall through.  */
1554e4b17023SJohn Marino     default:;
1555e4b17023SJohn Marino   }
1556e4b17023SJohn Marino 
1557e4b17023SJohn Marino   return type;
1558e4b17023SJohn Marino }
1559e4b17023SJohn Marino 
1560e4b17023SJohn Marino /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
1561e4b17023SJohn Marino    and mark them as needed.  */
1562e4b17023SJohn Marino 
1563e4b17023SJohn Marino static void
mark_vtable_entries(tree decl)1564e4b17023SJohn Marino mark_vtable_entries (tree decl)
1565e4b17023SJohn Marino {
1566e4b17023SJohn Marino   tree fnaddr;
1567e4b17023SJohn Marino   unsigned HOST_WIDE_INT idx;
1568e4b17023SJohn Marino 
1569e4b17023SJohn Marino   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
1570e4b17023SJohn Marino 			      idx, fnaddr)
1571e4b17023SJohn Marino     {
1572e4b17023SJohn Marino       tree fn;
1573e4b17023SJohn Marino 
1574e4b17023SJohn Marino       STRIP_NOPS (fnaddr);
1575e4b17023SJohn Marino 
1576e4b17023SJohn Marino       if (TREE_CODE (fnaddr) != ADDR_EXPR
1577e4b17023SJohn Marino 	  && TREE_CODE (fnaddr) != FDESC_EXPR)
1578e4b17023SJohn Marino 	/* This entry is an offset: a virtual base class offset, a
1579e4b17023SJohn Marino 	   virtual call offset, an RTTI offset, etc.  */
1580e4b17023SJohn Marino 	continue;
1581e4b17023SJohn Marino 
1582e4b17023SJohn Marino       fn = TREE_OPERAND (fnaddr, 0);
1583e4b17023SJohn Marino       TREE_ADDRESSABLE (fn) = 1;
1584e4b17023SJohn Marino       /* When we don't have vcall offsets, we output thunks whenever
1585e4b17023SJohn Marino 	 we output the vtables that contain them.  With vcall offsets,
1586e4b17023SJohn Marino 	 we know all the thunks we'll need when we emit a virtual
1587e4b17023SJohn Marino 	 function, so we emit the thunks there instead.  */
1588e4b17023SJohn Marino       if (DECL_THUNK_P (fn))
1589e4b17023SJohn Marino 	use_thunk (fn, /*emit_p=*/0);
1590e4b17023SJohn Marino       mark_used (fn);
1591e4b17023SJohn Marino     }
1592e4b17023SJohn Marino }
1593e4b17023SJohn Marino 
1594e4b17023SJohn Marino /* Set DECL up to have the closest approximation of "initialized common"
1595e4b17023SJohn Marino    linkage available.  */
1596e4b17023SJohn Marino 
1597e4b17023SJohn Marino void
comdat_linkage(tree decl)1598e4b17023SJohn Marino comdat_linkage (tree decl)
1599e4b17023SJohn Marino {
1600e4b17023SJohn Marino   if (flag_weak)
1601e4b17023SJohn Marino     make_decl_one_only (decl, cxx_comdat_group (decl));
1602e4b17023SJohn Marino   else if (TREE_CODE (decl) == FUNCTION_DECL
1603e4b17023SJohn Marino 	   || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
1604e4b17023SJohn Marino     /* We can just emit function and compiler-generated variables
1605e4b17023SJohn Marino        statically; having multiple copies is (for the most part) only
1606e4b17023SJohn Marino        a waste of space.
1607e4b17023SJohn Marino 
1608e4b17023SJohn Marino        There are two correctness issues, however: the address of a
1609e4b17023SJohn Marino        template instantiation with external linkage should be the
1610e4b17023SJohn Marino        same, independent of what translation unit asks for the
1611e4b17023SJohn Marino        address, and this will not hold when we emit multiple copies of
1612e4b17023SJohn Marino        the function.  However, there's little else we can do.
1613e4b17023SJohn Marino 
1614e4b17023SJohn Marino        Also, by default, the typeinfo implementation assumes that
1615e4b17023SJohn Marino        there will be only one copy of the string used as the name for
1616e4b17023SJohn Marino        each type.  Therefore, if weak symbols are unavailable, the
1617e4b17023SJohn Marino        run-time library should perform a more conservative check; it
1618e4b17023SJohn Marino        should perform a string comparison, rather than an address
1619e4b17023SJohn Marino        comparison.  */
1620e4b17023SJohn Marino     TREE_PUBLIC (decl) = 0;
1621e4b17023SJohn Marino   else
1622e4b17023SJohn Marino     {
1623e4b17023SJohn Marino       /* Static data member template instantiations, however, cannot
1624e4b17023SJohn Marino 	 have multiple copies.  */
1625e4b17023SJohn Marino       if (DECL_INITIAL (decl) == 0
1626e4b17023SJohn Marino 	  || DECL_INITIAL (decl) == error_mark_node)
1627e4b17023SJohn Marino 	DECL_COMMON (decl) = 1;
1628e4b17023SJohn Marino       else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
1629e4b17023SJohn Marino 	{
1630e4b17023SJohn Marino 	  DECL_COMMON (decl) = 1;
1631e4b17023SJohn Marino 	  DECL_INITIAL (decl) = error_mark_node;
1632e4b17023SJohn Marino 	}
1633e4b17023SJohn Marino       else if (!DECL_EXPLICIT_INSTANTIATION (decl))
1634e4b17023SJohn Marino 	{
1635e4b17023SJohn Marino 	  /* We can't do anything useful; leave vars for explicit
1636e4b17023SJohn Marino 	     instantiation.  */
1637e4b17023SJohn Marino 	  DECL_EXTERNAL (decl) = 1;
1638e4b17023SJohn Marino 	  DECL_NOT_REALLY_EXTERN (decl) = 0;
1639e4b17023SJohn Marino 	}
1640e4b17023SJohn Marino     }
1641e4b17023SJohn Marino 
1642e4b17023SJohn Marino   DECL_COMDAT (decl) = 1;
1643e4b17023SJohn Marino }
1644e4b17023SJohn Marino 
1645e4b17023SJohn Marino /* For win32 we also want to put explicit instantiations in
1646e4b17023SJohn Marino    linkonce sections, so that they will be merged with implicit
1647e4b17023SJohn Marino    instantiations; otherwise we get duplicate symbol errors.
1648e4b17023SJohn Marino    For Darwin we do not want explicit instantiations to be
1649e4b17023SJohn Marino    linkonce.  */
1650e4b17023SJohn Marino 
1651e4b17023SJohn Marino void
maybe_make_one_only(tree decl)1652e4b17023SJohn Marino maybe_make_one_only (tree decl)
1653e4b17023SJohn Marino {
1654e4b17023SJohn Marino   /* We used to say that this was not necessary on targets that support weak
1655e4b17023SJohn Marino      symbols, because the implicit instantiations will defer to the explicit
1656e4b17023SJohn Marino      one.  However, that's not actually the case in SVR4; a strong definition
1657e4b17023SJohn Marino      after a weak one is an error.  Also, not making explicit
1658e4b17023SJohn Marino      instantiations one_only means that we can end up with two copies of
1659e4b17023SJohn Marino      some template instantiations.  */
1660e4b17023SJohn Marino   if (! flag_weak)
1661e4b17023SJohn Marino     return;
1662e4b17023SJohn Marino 
1663e4b17023SJohn Marino   /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
1664e4b17023SJohn Marino      we can get away with not emitting them if they aren't used.  We need
1665e4b17023SJohn Marino      to for variables so that cp_finish_decl will update their linkage,
1666e4b17023SJohn Marino      because their DECL_INITIAL may not have been set properly yet.  */
1667e4b17023SJohn Marino 
1668e4b17023SJohn Marino   if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
1669e4b17023SJohn Marino       || (! DECL_EXPLICIT_INSTANTIATION (decl)
1670e4b17023SJohn Marino 	  && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
1671e4b17023SJohn Marino     {
1672e4b17023SJohn Marino       make_decl_one_only (decl, cxx_comdat_group (decl));
1673e4b17023SJohn Marino 
1674e4b17023SJohn Marino       if (TREE_CODE (decl) == VAR_DECL)
1675e4b17023SJohn Marino 	{
1676e4b17023SJohn Marino 	  DECL_COMDAT (decl) = 1;
1677e4b17023SJohn Marino 	  /* Mark it needed so we don't forget to emit it.  */
1678e4b17023SJohn Marino 	  mark_decl_referenced (decl);
1679e4b17023SJohn Marino 	}
1680e4b17023SJohn Marino     }
1681e4b17023SJohn Marino }
1682e4b17023SJohn Marino 
1683e4b17023SJohn Marino /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
1684e4b17023SJohn Marino    This predicate will give the right answer during parsing of the
1685e4b17023SJohn Marino    function, which other tests may not.  */
1686e4b17023SJohn Marino 
1687e4b17023SJohn Marino bool
vague_linkage_p(tree decl)1688e4b17023SJohn Marino vague_linkage_p (tree decl)
1689e4b17023SJohn Marino {
1690e4b17023SJohn Marino   /* Unfortunately, import_export_decl has not always been called
1691e4b17023SJohn Marino      before the function is processed, so we cannot simply check
1692e4b17023SJohn Marino      DECL_COMDAT.  */
1693e4b17023SJohn Marino   return (DECL_COMDAT (decl)
1694e4b17023SJohn Marino 	  || (((TREE_CODE (decl) == FUNCTION_DECL
1695e4b17023SJohn Marino 		&& DECL_DECLARED_INLINE_P (decl))
1696e4b17023SJohn Marino 	       || (DECL_LANG_SPECIFIC (decl)
1697e4b17023SJohn Marino 		   && DECL_TEMPLATE_INSTANTIATION (decl)))
1698e4b17023SJohn Marino 	      && TREE_PUBLIC (decl)));
1699e4b17023SJohn Marino }
1700e4b17023SJohn Marino 
1701e4b17023SJohn Marino /* Determine whether or not we want to specifically import or export CTYPE,
1702e4b17023SJohn Marino    using various heuristics.  */
1703e4b17023SJohn Marino 
1704e4b17023SJohn Marino static void
import_export_class(tree ctype)1705e4b17023SJohn Marino import_export_class (tree ctype)
1706e4b17023SJohn Marino {
1707e4b17023SJohn Marino   /* -1 for imported, 1 for exported.  */
1708e4b17023SJohn Marino   int import_export = 0;
1709e4b17023SJohn Marino 
1710e4b17023SJohn Marino   /* It only makes sense to call this function at EOF.  The reason is
1711e4b17023SJohn Marino      that this function looks at whether or not the first non-inline
1712e4b17023SJohn Marino      non-abstract virtual member function has been defined in this
1713e4b17023SJohn Marino      translation unit.  But, we can't possibly know that until we've
1714e4b17023SJohn Marino      seen the entire translation unit.  */
1715e4b17023SJohn Marino   gcc_assert (at_eof);
1716e4b17023SJohn Marino 
1717e4b17023SJohn Marino   if (CLASSTYPE_INTERFACE_KNOWN (ctype))
1718e4b17023SJohn Marino     return;
1719e4b17023SJohn Marino 
1720e4b17023SJohn Marino   /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
1721e4b17023SJohn Marino      we will have CLASSTYPE_INTERFACE_ONLY set but not
1722e4b17023SJohn Marino      CLASSTYPE_INTERFACE_KNOWN.  In that case, we don't want to use this
1723e4b17023SJohn Marino      heuristic because someone will supply a #pragma implementation
1724e4b17023SJohn Marino      elsewhere, and deducing it here would produce a conflict.  */
1725e4b17023SJohn Marino   if (CLASSTYPE_INTERFACE_ONLY (ctype))
1726e4b17023SJohn Marino     return;
1727e4b17023SJohn Marino 
1728e4b17023SJohn Marino   if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
1729e4b17023SJohn Marino     import_export = -1;
1730e4b17023SJohn Marino   else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
1731e4b17023SJohn Marino     import_export = 1;
1732e4b17023SJohn Marino   else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
1733e4b17023SJohn Marino 	   && !flag_implicit_templates)
1734e4b17023SJohn Marino     /* For a template class, without -fimplicit-templates, check the
1735e4b17023SJohn Marino        repository.  If the virtual table is assigned to this
1736e4b17023SJohn Marino        translation unit, then export the class; otherwise, import
1737e4b17023SJohn Marino        it.  */
1738e4b17023SJohn Marino       import_export = repo_export_class_p (ctype) ? 1 : -1;
1739e4b17023SJohn Marino   else if (TYPE_POLYMORPHIC_P (ctype))
1740e4b17023SJohn Marino     {
1741e4b17023SJohn Marino       /* The ABI specifies that the virtual table and associated
1742e4b17023SJohn Marino 	 information are emitted with the key method, if any.  */
1743e4b17023SJohn Marino       tree method = CLASSTYPE_KEY_METHOD (ctype);
1744e4b17023SJohn Marino       /* If weak symbol support is not available, then we must be
1745e4b17023SJohn Marino 	 careful not to emit the vtable when the key function is
1746e4b17023SJohn Marino 	 inline.  An inline function can be defined in multiple
1747e4b17023SJohn Marino 	 translation units.  If we were to emit the vtable in each
1748e4b17023SJohn Marino 	 translation unit containing a definition, we would get
1749e4b17023SJohn Marino 	 multiple definition errors at link-time.  */
1750e4b17023SJohn Marino       if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
1751e4b17023SJohn Marino 	import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
1752e4b17023SJohn Marino     }
1753e4b17023SJohn Marino 
1754e4b17023SJohn Marino   /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
1755e4b17023SJohn Marino      a definition anywhere else.  */
1756e4b17023SJohn Marino   if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
1757e4b17023SJohn Marino     import_export = 0;
1758e4b17023SJohn Marino 
1759e4b17023SJohn Marino   /* Allow back ends the chance to overrule the decision.  */
1760e4b17023SJohn Marino   if (targetm.cxx.import_export_class)
1761e4b17023SJohn Marino     import_export = targetm.cxx.import_export_class (ctype, import_export);
1762e4b17023SJohn Marino 
1763e4b17023SJohn Marino   if (import_export)
1764e4b17023SJohn Marino     {
1765e4b17023SJohn Marino       SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
1766e4b17023SJohn Marino       CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
1767e4b17023SJohn Marino     }
1768e4b17023SJohn Marino }
1769e4b17023SJohn Marino 
1770e4b17023SJohn Marino /* Return true if VAR has already been provided to the back end; in that
1771e4b17023SJohn Marino    case VAR should not be modified further by the front end.  */
1772e4b17023SJohn Marino static bool
var_finalized_p(tree var)1773e4b17023SJohn Marino var_finalized_p (tree var)
1774e4b17023SJohn Marino {
1775e4b17023SJohn Marino   return varpool_node (var)->finalized;
1776e4b17023SJohn Marino }
1777e4b17023SJohn Marino 
1778e4b17023SJohn Marino /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
1779e4b17023SJohn Marino    must be emitted in this translation unit.  Mark it as such.  */
1780e4b17023SJohn Marino 
1781e4b17023SJohn Marino void
mark_needed(tree decl)1782e4b17023SJohn Marino mark_needed (tree decl)
1783e4b17023SJohn Marino {
1784e4b17023SJohn Marino   /* It's possible that we no longer need to set
1785e4b17023SJohn Marino      TREE_SYMBOL_REFERENCED here directly, but doing so is
1786e4b17023SJohn Marino      harmless.  */
1787e4b17023SJohn Marino   TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
1788e4b17023SJohn Marino   mark_decl_referenced (decl);
1789e4b17023SJohn Marino }
1790e4b17023SJohn Marino 
1791e4b17023SJohn Marino /* DECL is either a FUNCTION_DECL or a VAR_DECL.  This function
1792e4b17023SJohn Marino    returns true if a definition of this entity should be provided in
1793e4b17023SJohn Marino    this object file.  Callers use this function to determine whether
1794e4b17023SJohn Marino    or not to let the back end know that a definition of DECL is
1795e4b17023SJohn Marino    available in this translation unit.  */
1796e4b17023SJohn Marino 
1797e4b17023SJohn Marino bool
decl_needed_p(tree decl)1798e4b17023SJohn Marino decl_needed_p (tree decl)
1799e4b17023SJohn Marino {
1800e4b17023SJohn Marino   gcc_assert (TREE_CODE (decl) == VAR_DECL
1801e4b17023SJohn Marino 	      || TREE_CODE (decl) == FUNCTION_DECL);
1802e4b17023SJohn Marino   /* This function should only be called at the end of the translation
1803e4b17023SJohn Marino      unit.  We cannot be sure of whether or not something will be
1804e4b17023SJohn Marino      COMDAT until that point.  */
1805e4b17023SJohn Marino   gcc_assert (at_eof);
1806e4b17023SJohn Marino 
1807e4b17023SJohn Marino   /* All entities with external linkage that are not COMDAT should be
1808e4b17023SJohn Marino      emitted; they may be referred to from other object files.  */
1809e4b17023SJohn Marino   if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
1810e4b17023SJohn Marino     return true;
1811e4b17023SJohn Marino   /* If this entity was used, let the back end see it; it will decide
1812e4b17023SJohn Marino      whether or not to emit it into the object file.  */
1813e4b17023SJohn Marino   if (TREE_USED (decl)
1814e4b17023SJohn Marino       || (DECL_ASSEMBLER_NAME_SET_P (decl)
1815e4b17023SJohn Marino 	  && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
1816e4b17023SJohn Marino       return true;
1817e4b17023SJohn Marino   /* Functions marked "dllexport" must be emitted so that they are
1818e4b17023SJohn Marino      visible to other DLLs.  */
1819e4b17023SJohn Marino   if (flag_keep_inline_dllexport
1820e4b17023SJohn Marino       && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
1821e4b17023SJohn Marino     return true;
1822e4b17023SJohn Marino   /* Otherwise, DECL does not need to be emitted -- yet.  A subsequent
1823e4b17023SJohn Marino      reference to DECL might cause it to be emitted later.  */
1824e4b17023SJohn Marino   return false;
1825e4b17023SJohn Marino }
1826e4b17023SJohn Marino 
1827e4b17023SJohn Marino /* If necessary, write out the vtables for the dynamic class CTYPE.
1828e4b17023SJohn Marino    Returns true if any vtables were emitted.  */
1829e4b17023SJohn Marino 
1830e4b17023SJohn Marino static bool
maybe_emit_vtables(tree ctype)1831e4b17023SJohn Marino maybe_emit_vtables (tree ctype)
1832e4b17023SJohn Marino {
1833e4b17023SJohn Marino   tree vtbl;
1834e4b17023SJohn Marino   tree primary_vtbl;
1835e4b17023SJohn Marino   int needed = 0;
1836e4b17023SJohn Marino   struct varpool_node *current = NULL, *last = NULL, *first = NULL;
1837e4b17023SJohn Marino 
1838e4b17023SJohn Marino   /* If the vtables for this class have already been emitted there is
1839e4b17023SJohn Marino      nothing more to do.  */
1840e4b17023SJohn Marino   primary_vtbl = CLASSTYPE_VTABLES (ctype);
1841e4b17023SJohn Marino   if (var_finalized_p (primary_vtbl))
1842e4b17023SJohn Marino     return false;
1843e4b17023SJohn Marino   /* Ignore dummy vtables made by get_vtable_decl.  */
1844e4b17023SJohn Marino   if (TREE_TYPE (primary_vtbl) == void_type_node)
1845e4b17023SJohn Marino     return false;
1846e4b17023SJohn Marino 
1847e4b17023SJohn Marino   /* On some targets, we cannot determine the key method until the end
1848e4b17023SJohn Marino      of the translation unit -- which is when this function is
1849e4b17023SJohn Marino      called.  */
1850e4b17023SJohn Marino   if (!targetm.cxx.key_method_may_be_inline ())
1851e4b17023SJohn Marino     determine_key_method (ctype);
1852e4b17023SJohn Marino 
1853e4b17023SJohn Marino   /* See if any of the vtables are needed.  */
1854e4b17023SJohn Marino   for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
1855e4b17023SJohn Marino     {
1856e4b17023SJohn Marino       import_export_decl (vtbl);
1857e4b17023SJohn Marino       if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
1858e4b17023SJohn Marino 	needed = 1;
1859e4b17023SJohn Marino     }
1860e4b17023SJohn Marino   if (!needed)
1861e4b17023SJohn Marino     {
1862e4b17023SJohn Marino       /* If the references to this class' vtables are optimized away,
1863e4b17023SJohn Marino 	 still emit the appropriate debugging information.  See
1864e4b17023SJohn Marino 	 dfs_debug_mark.  */
1865e4b17023SJohn Marino       if (DECL_COMDAT (primary_vtbl)
1866e4b17023SJohn Marino 	  && CLASSTYPE_DEBUG_REQUESTED (ctype))
1867e4b17023SJohn Marino 	note_debug_info_needed (ctype);
1868e4b17023SJohn Marino       return false;
1869e4b17023SJohn Marino     }
1870e4b17023SJohn Marino 
1871e4b17023SJohn Marino   /* The ABI requires that we emit all of the vtables if we emit any
1872e4b17023SJohn Marino      of them.  */
1873e4b17023SJohn Marino   for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
1874e4b17023SJohn Marino     {
1875e4b17023SJohn Marino       /* Mark entities references from the virtual table as used.  */
1876e4b17023SJohn Marino       mark_vtable_entries (vtbl);
1877e4b17023SJohn Marino 
1878e4b17023SJohn Marino       if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
1879e4b17023SJohn Marino 	{
1880e4b17023SJohn Marino 	  VEC(tree,gc)* cleanups = NULL;
1881e4b17023SJohn Marino 	  tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
1882e4b17023SJohn Marino 					LOOKUP_NORMAL);
1883e4b17023SJohn Marino 
1884e4b17023SJohn Marino 	  /* It had better be all done at compile-time.  */
1885e4b17023SJohn Marino 	  gcc_assert (!expr && !cleanups);
1886e4b17023SJohn Marino 	}
1887e4b17023SJohn Marino 
1888e4b17023SJohn Marino       /* Write it out.  */
1889e4b17023SJohn Marino       DECL_EXTERNAL (vtbl) = 0;
1890e4b17023SJohn Marino       rest_of_decl_compilation (vtbl, 1, 1);
1891e4b17023SJohn Marino 
1892e4b17023SJohn Marino       /* Because we're only doing syntax-checking, we'll never end up
1893e4b17023SJohn Marino 	 actually marking the variable as written.  */
1894e4b17023SJohn Marino       if (flag_syntax_only)
1895e4b17023SJohn Marino 	TREE_ASM_WRITTEN (vtbl) = 1;
1896e4b17023SJohn Marino       else if (DECL_COMDAT (vtbl))
1897e4b17023SJohn Marino 	{
1898e4b17023SJohn Marino 	  current = varpool_node (vtbl);
1899e4b17023SJohn Marino 	  if (last)
1900e4b17023SJohn Marino 	    last->same_comdat_group = current;
1901e4b17023SJohn Marino 	  last = current;
1902e4b17023SJohn Marino 	  if (!first)
1903e4b17023SJohn Marino 	    first = current;
1904e4b17023SJohn Marino 	}
1905e4b17023SJohn Marino     }
1906e4b17023SJohn Marino 
1907e4b17023SJohn Marino   if (first != last)
1908e4b17023SJohn Marino     last->same_comdat_group = first;
1909e4b17023SJohn Marino 
1910e4b17023SJohn Marino   /* Since we're writing out the vtable here, also write the debug
1911e4b17023SJohn Marino      info.  */
1912e4b17023SJohn Marino   note_debug_info_needed (ctype);
1913e4b17023SJohn Marino 
1914e4b17023SJohn Marino   return true;
1915e4b17023SJohn Marino }
1916e4b17023SJohn Marino 
1917e4b17023SJohn Marino /* A special return value from type_visibility meaning internal
1918e4b17023SJohn Marino    linkage.  */
1919e4b17023SJohn Marino 
1920e4b17023SJohn Marino enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
1921e4b17023SJohn Marino 
1922e4b17023SJohn Marino /* walk_tree helper function for type_visibility.  */
1923e4b17023SJohn Marino 
1924e4b17023SJohn Marino static tree
min_vis_r(tree * tp,int * walk_subtrees,void * data)1925e4b17023SJohn Marino min_vis_r (tree *tp, int *walk_subtrees, void *data)
1926e4b17023SJohn Marino {
1927e4b17023SJohn Marino   int *vis_p = (int *)data;
1928e4b17023SJohn Marino   if (! TYPE_P (*tp))
1929e4b17023SJohn Marino     {
1930e4b17023SJohn Marino       *walk_subtrees = 0;
1931e4b17023SJohn Marino     }
1932*5ce9237cSJohn Marino   else if (TAGGED_TYPE_P (*tp)
1933*5ce9237cSJohn Marino 	   && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
1934e4b17023SJohn Marino     {
1935e4b17023SJohn Marino       *vis_p = VISIBILITY_ANON;
1936e4b17023SJohn Marino       return *tp;
1937e4b17023SJohn Marino     }
1938*5ce9237cSJohn Marino   else if (CLASS_TYPE_P (*tp)
1939*5ce9237cSJohn Marino 	   && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
1940e4b17023SJohn Marino     *vis_p = CLASSTYPE_VISIBILITY (*tp);
1941e4b17023SJohn Marino   return NULL;
1942e4b17023SJohn Marino }
1943e4b17023SJohn Marino 
1944e4b17023SJohn Marino /* Returns the visibility of TYPE, which is the minimum visibility of its
1945e4b17023SJohn Marino    component types.  */
1946e4b17023SJohn Marino 
1947e4b17023SJohn Marino static int
type_visibility(tree type)1948e4b17023SJohn Marino type_visibility (tree type)
1949e4b17023SJohn Marino {
1950e4b17023SJohn Marino   int vis = VISIBILITY_DEFAULT;
1951e4b17023SJohn Marino   cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
1952e4b17023SJohn Marino   return vis;
1953e4b17023SJohn Marino }
1954e4b17023SJohn Marino 
1955e4b17023SJohn Marino /* Limit the visibility of DECL to VISIBILITY, if not explicitly
1956e4b17023SJohn Marino    specified (or if VISIBILITY is static).  If TMPL is true, this
1957e4b17023SJohn Marino    constraint is for a template argument, and takes precedence
1958e4b17023SJohn Marino    over explicitly-specified visibility on the template.  */
1959e4b17023SJohn Marino 
1960e4b17023SJohn Marino static void
constrain_visibility(tree decl,int visibility,bool tmpl)1961e4b17023SJohn Marino constrain_visibility (tree decl, int visibility, bool tmpl)
1962e4b17023SJohn Marino {
1963e4b17023SJohn Marino   if (visibility == VISIBILITY_ANON)
1964e4b17023SJohn Marino     {
1965e4b17023SJohn Marino       /* extern "C" declarations aren't affected by the anonymous
1966e4b17023SJohn Marino 	 namespace.  */
1967e4b17023SJohn Marino       if (!DECL_EXTERN_C_P (decl))
1968e4b17023SJohn Marino 	{
1969e4b17023SJohn Marino 	  TREE_PUBLIC (decl) = 0;
1970e4b17023SJohn Marino 	  DECL_WEAK (decl) = 0;
1971e4b17023SJohn Marino 	  DECL_COMMON (decl) = 0;
1972e4b17023SJohn Marino 	  DECL_COMDAT_GROUP (decl) = NULL_TREE;
1973e4b17023SJohn Marino 	  DECL_INTERFACE_KNOWN (decl) = 1;
1974e4b17023SJohn Marino 	  if (DECL_LANG_SPECIFIC (decl))
1975e4b17023SJohn Marino 	    DECL_NOT_REALLY_EXTERN (decl) = 1;
1976e4b17023SJohn Marino 	}
1977e4b17023SJohn Marino     }
1978e4b17023SJohn Marino   else if (visibility > DECL_VISIBILITY (decl)
1979e4b17023SJohn Marino 	   && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
1980e4b17023SJohn Marino     {
1981e4b17023SJohn Marino       DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
1982e4b17023SJohn Marino       /* This visibility was not specified.  */
1983e4b17023SJohn Marino       DECL_VISIBILITY_SPECIFIED (decl) = false;
1984e4b17023SJohn Marino     }
1985e4b17023SJohn Marino }
1986e4b17023SJohn Marino 
1987e4b17023SJohn Marino /* Constrain the visibility of DECL based on the visibility of its template
1988e4b17023SJohn Marino    arguments.  */
1989e4b17023SJohn Marino 
1990e4b17023SJohn Marino static void
constrain_visibility_for_template(tree decl,tree targs)1991e4b17023SJohn Marino constrain_visibility_for_template (tree decl, tree targs)
1992e4b17023SJohn Marino {
1993e4b17023SJohn Marino   /* If this is a template instantiation, check the innermost
1994e4b17023SJohn Marino      template args for visibility constraints.  The outer template
1995e4b17023SJohn Marino      args are covered by the class check.  */
1996e4b17023SJohn Marino   tree args = INNERMOST_TEMPLATE_ARGS (targs);
1997e4b17023SJohn Marino   int i;
1998e4b17023SJohn Marino   for (i = TREE_VEC_LENGTH (args); i > 0; --i)
1999e4b17023SJohn Marino     {
2000e4b17023SJohn Marino       int vis = 0;
2001e4b17023SJohn Marino 
2002e4b17023SJohn Marino       tree arg = TREE_VEC_ELT (args, i-1);
2003e4b17023SJohn Marino       if (TYPE_P (arg))
2004e4b17023SJohn Marino 	vis = type_visibility (arg);
2005e4b17023SJohn Marino       else if (TREE_TYPE (arg) && POINTER_TYPE_P (TREE_TYPE (arg)))
2006e4b17023SJohn Marino 	{
2007e4b17023SJohn Marino 	  STRIP_NOPS (arg);
2008e4b17023SJohn Marino 	  if (TREE_CODE (arg) == ADDR_EXPR)
2009e4b17023SJohn Marino 	    arg = TREE_OPERAND (arg, 0);
2010e4b17023SJohn Marino 	  if (TREE_CODE (arg) == VAR_DECL
2011e4b17023SJohn Marino 	      || TREE_CODE (arg) == FUNCTION_DECL)
2012e4b17023SJohn Marino 	    {
2013e4b17023SJohn Marino 	      if (! TREE_PUBLIC (arg))
2014e4b17023SJohn Marino 		vis = VISIBILITY_ANON;
2015e4b17023SJohn Marino 	      else
2016e4b17023SJohn Marino 		vis = DECL_VISIBILITY (arg);
2017e4b17023SJohn Marino 	    }
2018e4b17023SJohn Marino 	}
2019e4b17023SJohn Marino       if (vis)
2020e4b17023SJohn Marino 	constrain_visibility (decl, vis, true);
2021e4b17023SJohn Marino     }
2022e4b17023SJohn Marino }
2023e4b17023SJohn Marino 
2024e4b17023SJohn Marino /* Like c_determine_visibility, but with additional C++-specific
2025e4b17023SJohn Marino    behavior.
2026e4b17023SJohn Marino 
2027e4b17023SJohn Marino    Function-scope entities can rely on the function's visibility because
2028e4b17023SJohn Marino    it is set in start_preparsed_function.
2029e4b17023SJohn Marino 
2030e4b17023SJohn Marino    Class-scope entities cannot rely on the class's visibility until the end
2031e4b17023SJohn Marino    of the enclosing class definition.
2032e4b17023SJohn Marino 
2033e4b17023SJohn Marino    Note that because namespaces have multiple independent definitions,
2034e4b17023SJohn Marino    namespace visibility is handled elsewhere using the #pragma visibility
2035e4b17023SJohn Marino    machinery rather than by decorating the namespace declaration.
2036e4b17023SJohn Marino 
2037e4b17023SJohn Marino    The goal is for constraints from the type to give a diagnostic, and
2038e4b17023SJohn Marino    other constraints to be applied silently.  */
2039e4b17023SJohn Marino 
2040e4b17023SJohn Marino void
determine_visibility(tree decl)2041e4b17023SJohn Marino determine_visibility (tree decl)
2042e4b17023SJohn Marino {
2043e4b17023SJohn Marino   tree class_type = NULL_TREE;
2044e4b17023SJohn Marino   bool use_template;
2045e4b17023SJohn Marino   bool orig_visibility_specified;
2046e4b17023SJohn Marino   enum symbol_visibility orig_visibility;
2047e4b17023SJohn Marino 
2048e4b17023SJohn Marino   /* Remember that all decls get VISIBILITY_DEFAULT when built.  */
2049e4b17023SJohn Marino 
2050e4b17023SJohn Marino   /* Only relevant for names with external linkage.  */
2051e4b17023SJohn Marino   if (!TREE_PUBLIC (decl))
2052e4b17023SJohn Marino     return;
2053e4b17023SJohn Marino 
2054e4b17023SJohn Marino   /* Cloned constructors and destructors get the same visibility as
2055e4b17023SJohn Marino      the underlying function.  That should be set up in
2056e4b17023SJohn Marino      maybe_clone_body.  */
2057e4b17023SJohn Marino   gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
2058e4b17023SJohn Marino 
2059e4b17023SJohn Marino   orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
2060e4b17023SJohn Marino   orig_visibility = DECL_VISIBILITY (decl);
2061e4b17023SJohn Marino 
2062e4b17023SJohn Marino   if (TREE_CODE (decl) == TYPE_DECL)
2063e4b17023SJohn Marino     {
2064e4b17023SJohn Marino       if (CLASS_TYPE_P (TREE_TYPE (decl)))
2065e4b17023SJohn Marino 	use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
2066e4b17023SJohn Marino       else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2067e4b17023SJohn Marino 	use_template = 1;
2068e4b17023SJohn Marino       else
2069e4b17023SJohn Marino 	use_template = 0;
2070e4b17023SJohn Marino     }
2071e4b17023SJohn Marino   else if (DECL_LANG_SPECIFIC (decl))
2072e4b17023SJohn Marino     use_template = DECL_USE_TEMPLATE (decl);
2073e4b17023SJohn Marino   else
2074e4b17023SJohn Marino     use_template = 0;
2075e4b17023SJohn Marino 
2076e4b17023SJohn Marino   /* If DECL is a member of a class, visibility specifiers on the
2077e4b17023SJohn Marino      class can influence the visibility of the DECL.  */
2078e4b17023SJohn Marino   if (DECL_CLASS_SCOPE_P (decl))
2079e4b17023SJohn Marino     class_type = DECL_CONTEXT (decl);
2080e4b17023SJohn Marino   else
2081e4b17023SJohn Marino     {
2082e4b17023SJohn Marino       /* Not a class member.  */
2083e4b17023SJohn Marino 
2084e4b17023SJohn Marino       /* Virtual tables have DECL_CONTEXT set to their associated class,
2085e4b17023SJohn Marino 	 so they are automatically handled above.  */
2086e4b17023SJohn Marino       gcc_assert (TREE_CODE (decl) != VAR_DECL
2087e4b17023SJohn Marino 		  || !DECL_VTABLE_OR_VTT_P (decl));
2088e4b17023SJohn Marino 
2089e4b17023SJohn Marino       if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
2090e4b17023SJohn Marino 	{
2091e4b17023SJohn Marino 	  /* Local statics and classes get the visibility of their
2092e4b17023SJohn Marino 	     containing function by default, except that
2093e4b17023SJohn Marino 	     -fvisibility-inlines-hidden doesn't affect them.  */
2094e4b17023SJohn Marino 	  tree fn = DECL_CONTEXT (decl);
2095e4b17023SJohn Marino 	  if (DECL_VISIBILITY_SPECIFIED (fn))
2096e4b17023SJohn Marino 	    {
2097e4b17023SJohn Marino 	      DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2098e4b17023SJohn Marino 	      DECL_VISIBILITY_SPECIFIED (decl) =
2099e4b17023SJohn Marino 		DECL_VISIBILITY_SPECIFIED (fn);
2100e4b17023SJohn Marino 	    }
2101e4b17023SJohn Marino 	  else
2102e4b17023SJohn Marino 	    {
2103e4b17023SJohn Marino 	      if (DECL_CLASS_SCOPE_P (fn))
2104e4b17023SJohn Marino 		determine_visibility_from_class (decl, DECL_CONTEXT (fn));
2105e4b17023SJohn Marino 	      else if (determine_hidden_inline (fn))
2106e4b17023SJohn Marino 		{
2107e4b17023SJohn Marino 		  DECL_VISIBILITY (decl) = default_visibility;
2108e4b17023SJohn Marino 		  DECL_VISIBILITY_SPECIFIED (decl) =
2109e4b17023SJohn Marino 		    visibility_options.inpragma;
2110e4b17023SJohn Marino 		}
2111e4b17023SJohn Marino 	      else
2112e4b17023SJohn Marino 		{
2113e4b17023SJohn Marino 	          DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
2114e4b17023SJohn Marino 	          DECL_VISIBILITY_SPECIFIED (decl) =
2115e4b17023SJohn Marino 		    DECL_VISIBILITY_SPECIFIED (fn);
2116e4b17023SJohn Marino 		}
2117e4b17023SJohn Marino 	    }
2118e4b17023SJohn Marino 
2119e4b17023SJohn Marino 	  /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
2120e4b17023SJohn Marino 	     but have no TEMPLATE_INFO, so don't try to check it.  */
2121e4b17023SJohn Marino 	  use_template = 0;
2122e4b17023SJohn Marino 	}
2123e4b17023SJohn Marino       else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl)
2124e4b17023SJohn Marino 	       && flag_visibility_ms_compat)
2125e4b17023SJohn Marino 	{
2126e4b17023SJohn Marino 	  /* Under -fvisibility-ms-compat, types are visible by default,
2127e4b17023SJohn Marino 	     even though their contents aren't.  */
2128e4b17023SJohn Marino 	  tree underlying_type = TREE_TYPE (DECL_NAME (decl));
2129e4b17023SJohn Marino 	  int underlying_vis = type_visibility (underlying_type);
2130e4b17023SJohn Marino 	  if (underlying_vis == VISIBILITY_ANON
2131e4b17023SJohn Marino 	      || (CLASS_TYPE_P (underlying_type)
2132e4b17023SJohn Marino 		  && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
2133e4b17023SJohn Marino 	    constrain_visibility (decl, underlying_vis, false);
2134e4b17023SJohn Marino 	  else
2135e4b17023SJohn Marino 	    DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
2136e4b17023SJohn Marino 	}
2137e4b17023SJohn Marino       else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
2138e4b17023SJohn Marino 	{
2139e4b17023SJohn Marino 	  /* tinfo visibility is based on the type it's for.  */
2140e4b17023SJohn Marino 	  constrain_visibility
2141e4b17023SJohn Marino 	    (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
2142e4b17023SJohn Marino 
2143e4b17023SJohn Marino 	  /* Give the target a chance to override the visibility associated
2144e4b17023SJohn Marino 	     with DECL.  */
2145e4b17023SJohn Marino 	  if (TREE_PUBLIC (decl)
2146e4b17023SJohn Marino 	      && !DECL_REALLY_EXTERN (decl)
2147e4b17023SJohn Marino 	      && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
2148e4b17023SJohn Marino 	      && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
2149e4b17023SJohn Marino 	    targetm.cxx.determine_class_data_visibility (decl);
2150e4b17023SJohn Marino 	}
2151e4b17023SJohn Marino       else if (use_template)
2152e4b17023SJohn Marino 	/* Template instantiations and specializations get visibility based
2153e4b17023SJohn Marino 	   on their template unless they override it with an attribute.  */;
2154e4b17023SJohn Marino       else if (! DECL_VISIBILITY_SPECIFIED (decl))
2155e4b17023SJohn Marino 	{
2156e4b17023SJohn Marino           if (determine_hidden_inline (decl))
2157e4b17023SJohn Marino 	    DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2158e4b17023SJohn Marino 	  else
2159e4b17023SJohn Marino             {
2160e4b17023SJohn Marino 	      /* Set default visibility to whatever the user supplied with
2161e4b17023SJohn Marino 	         #pragma GCC visibility or a namespace visibility attribute.  */
2162e4b17023SJohn Marino 	      DECL_VISIBILITY (decl) = default_visibility;
2163e4b17023SJohn Marino 	      DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
2164e4b17023SJohn Marino             }
2165e4b17023SJohn Marino 	}
2166e4b17023SJohn Marino     }
2167e4b17023SJohn Marino 
2168e4b17023SJohn Marino   if (use_template)
2169e4b17023SJohn Marino     {
2170e4b17023SJohn Marino       /* If the specialization doesn't specify visibility, use the
2171e4b17023SJohn Marino 	 visibility from the template.  */
2172e4b17023SJohn Marino       tree tinfo = (TREE_CODE (decl) == TYPE_DECL
2173e4b17023SJohn Marino 		    ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2174e4b17023SJohn Marino 		    : DECL_TEMPLATE_INFO (decl));
2175e4b17023SJohn Marino       tree args = TI_ARGS (tinfo);
2176e4b17023SJohn Marino       tree attribs = (TREE_CODE (decl) == TYPE_DECL
2177e4b17023SJohn Marino 		      ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
2178e4b17023SJohn Marino 		      : DECL_ATTRIBUTES (decl));
2179e4b17023SJohn Marino 
2180e4b17023SJohn Marino       if (args != error_mark_node
2181e4b17023SJohn Marino 	  /* Template argument visibility outweighs #pragma or namespace
2182e4b17023SJohn Marino 	     visibility, but not an explicit attribute.  */
2183e4b17023SJohn Marino 	  && !lookup_attribute ("visibility", attribs))
2184e4b17023SJohn Marino 	{
2185e4b17023SJohn Marino 	  int depth = TMPL_ARGS_DEPTH (args);
2186e4b17023SJohn Marino 	  tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
2187e4b17023SJohn Marino 
2188e4b17023SJohn Marino 	  if (!DECL_VISIBILITY_SPECIFIED (decl))
2189e4b17023SJohn Marino 	    {
2190e4b17023SJohn Marino 	      if (!DECL_VISIBILITY_SPECIFIED (pattern)
2191e4b17023SJohn Marino 		  && determine_hidden_inline (decl))
2192e4b17023SJohn Marino 		DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2193e4b17023SJohn Marino 	      else
2194e4b17023SJohn Marino 		{
2195e4b17023SJohn Marino 	          DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
2196e4b17023SJohn Marino 	          DECL_VISIBILITY_SPECIFIED (decl)
2197e4b17023SJohn Marino 		    = DECL_VISIBILITY_SPECIFIED (pattern);
2198e4b17023SJohn Marino 		}
2199e4b17023SJohn Marino 	    }
2200e4b17023SJohn Marino 
2201e4b17023SJohn Marino 	  /* FIXME should TMPL_ARGS_DEPTH really return 1 for null input? */
2202e4b17023SJohn Marino 	  if (args && depth > template_class_depth (class_type))
2203e4b17023SJohn Marino 	    /* Limit visibility based on its template arguments.  */
2204e4b17023SJohn Marino 	    constrain_visibility_for_template (decl, args);
2205e4b17023SJohn Marino 	}
2206e4b17023SJohn Marino     }
2207e4b17023SJohn Marino 
2208e4b17023SJohn Marino   if (class_type)
2209e4b17023SJohn Marino     determine_visibility_from_class (decl, class_type);
2210e4b17023SJohn Marino 
2211e4b17023SJohn Marino   if (decl_anon_ns_mem_p (decl))
2212e4b17023SJohn Marino     /* Names in an anonymous namespace get internal linkage.
2213e4b17023SJohn Marino        This might change once we implement export.  */
2214e4b17023SJohn Marino     constrain_visibility (decl, VISIBILITY_ANON, false);
2215e4b17023SJohn Marino   else if (TREE_CODE (decl) != TYPE_DECL)
2216e4b17023SJohn Marino     {
2217e4b17023SJohn Marino       /* Propagate anonymity from type to decl.  */
2218e4b17023SJohn Marino       int tvis = type_visibility (TREE_TYPE (decl));
2219e4b17023SJohn Marino       if (tvis == VISIBILITY_ANON
2220e4b17023SJohn Marino 	  || ! DECL_VISIBILITY_SPECIFIED (decl))
2221e4b17023SJohn Marino 	constrain_visibility (decl, tvis, false);
2222e4b17023SJohn Marino     }
2223e4b17023SJohn Marino   else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
2224e4b17023SJohn Marino     /* DR 757: A type without linkage shall not be used as the type of a
2225e4b17023SJohn Marino        variable or function with linkage, unless
2226e4b17023SJohn Marino        o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
2227e4b17023SJohn Marino        o the variable or function is not used (3.2 [basic.def.odr]) or is
2228e4b17023SJohn Marino        defined in the same translation unit.
2229e4b17023SJohn Marino 
2230e4b17023SJohn Marino        Since non-extern "C" decls need to be defined in the same
2231e4b17023SJohn Marino        translation unit, we can make the type internal.  */
2232e4b17023SJohn Marino     constrain_visibility (decl, VISIBILITY_ANON, false);
2233e4b17023SJohn Marino 
2234e4b17023SJohn Marino   /* If visibility changed and DECL already has DECL_RTL, ensure
2235e4b17023SJohn Marino      symbol flags are updated.  */
2236e4b17023SJohn Marino   if ((DECL_VISIBILITY (decl) != orig_visibility
2237e4b17023SJohn Marino        || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
2238e4b17023SJohn Marino       && ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
2239e4b17023SJohn Marino 	  || TREE_CODE (decl) == FUNCTION_DECL)
2240e4b17023SJohn Marino       && DECL_RTL_SET_P (decl))
2241e4b17023SJohn Marino     make_decl_rtl (decl);
2242e4b17023SJohn Marino }
2243e4b17023SJohn Marino 
2244e4b17023SJohn Marino /* By default, static data members and function members receive
2245e4b17023SJohn Marino    the visibility of their containing class.  */
2246e4b17023SJohn Marino 
2247e4b17023SJohn Marino static void
determine_visibility_from_class(tree decl,tree class_type)2248e4b17023SJohn Marino determine_visibility_from_class (tree decl, tree class_type)
2249e4b17023SJohn Marino {
2250e4b17023SJohn Marino   if (DECL_VISIBILITY_SPECIFIED (decl))
2251e4b17023SJohn Marino     return;
2252e4b17023SJohn Marino 
2253e4b17023SJohn Marino   if (determine_hidden_inline (decl))
2254e4b17023SJohn Marino     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2255e4b17023SJohn Marino   else
2256e4b17023SJohn Marino     {
2257e4b17023SJohn Marino       /* Default to the class visibility.  */
2258e4b17023SJohn Marino       DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
2259e4b17023SJohn Marino       DECL_VISIBILITY_SPECIFIED (decl)
2260e4b17023SJohn Marino 	= CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
2261e4b17023SJohn Marino     }
2262e4b17023SJohn Marino 
2263e4b17023SJohn Marino   /* Give the target a chance to override the visibility associated
2264e4b17023SJohn Marino      with DECL.  */
2265e4b17023SJohn Marino   if (TREE_CODE (decl) == VAR_DECL
2266e4b17023SJohn Marino       && (DECL_TINFO_P (decl)
2267e4b17023SJohn Marino 	  || (DECL_VTABLE_OR_VTT_P (decl)
2268e4b17023SJohn Marino 	      /* Construction virtual tables are not exported because
2269e4b17023SJohn Marino 		 they cannot be referred to from other object files;
2270e4b17023SJohn Marino 		 their name is not standardized by the ABI.  */
2271e4b17023SJohn Marino 	      && !DECL_CONSTRUCTION_VTABLE_P (decl)))
2272e4b17023SJohn Marino       && TREE_PUBLIC (decl)
2273e4b17023SJohn Marino       && !DECL_REALLY_EXTERN (decl)
2274e4b17023SJohn Marino       && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
2275e4b17023SJohn Marino     targetm.cxx.determine_class_data_visibility (decl);
2276e4b17023SJohn Marino }
2277e4b17023SJohn Marino 
2278e4b17023SJohn Marino /* Returns true iff DECL is an inline that should get hidden visibility
2279e4b17023SJohn Marino    because of -fvisibility-inlines-hidden.  */
2280e4b17023SJohn Marino 
2281e4b17023SJohn Marino static bool
determine_hidden_inline(tree decl)2282e4b17023SJohn Marino determine_hidden_inline (tree decl)
2283e4b17023SJohn Marino {
2284e4b17023SJohn Marino   return (visibility_options.inlines_hidden
2285e4b17023SJohn Marino 	  /* Don't do this for inline templates; specializations might not be
2286e4b17023SJohn Marino 	     inline, and we don't want them to inherit the hidden
2287e4b17023SJohn Marino 	     visibility.  We'll set it here for all inline instantiations.  */
2288e4b17023SJohn Marino 	  && !processing_template_decl
2289e4b17023SJohn Marino 	  && TREE_CODE (decl) == FUNCTION_DECL
2290e4b17023SJohn Marino 	  && DECL_DECLARED_INLINE_P (decl)
2291e4b17023SJohn Marino 	  && (! DECL_LANG_SPECIFIC (decl)
2292e4b17023SJohn Marino 	      || ! DECL_EXPLICIT_INSTANTIATION (decl)));
2293e4b17023SJohn Marino }
2294e4b17023SJohn Marino 
2295e4b17023SJohn Marino /* Constrain the visibility of a class TYPE based on the visibility of its
2296e4b17023SJohn Marino    field types.  Warn if any fields require lesser visibility.  */
2297e4b17023SJohn Marino 
2298e4b17023SJohn Marino void
constrain_class_visibility(tree type)2299e4b17023SJohn Marino constrain_class_visibility (tree type)
2300e4b17023SJohn Marino {
2301e4b17023SJohn Marino   tree binfo;
2302e4b17023SJohn Marino   tree t;
2303e4b17023SJohn Marino   int i;
2304e4b17023SJohn Marino 
2305e4b17023SJohn Marino   int vis = type_visibility (type);
2306e4b17023SJohn Marino 
2307e4b17023SJohn Marino   if (vis == VISIBILITY_ANON
2308e4b17023SJohn Marino       || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
2309e4b17023SJohn Marino     return;
2310e4b17023SJohn Marino 
2311e4b17023SJohn Marino   /* Don't warn about visibility if the class has explicit visibility.  */
2312e4b17023SJohn Marino   if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
2313e4b17023SJohn Marino     vis = VISIBILITY_INTERNAL;
2314e4b17023SJohn Marino 
2315e4b17023SJohn Marino   for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
2316e4b17023SJohn Marino     if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
2317e4b17023SJohn Marino       {
2318e4b17023SJohn Marino 	tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
2319e4b17023SJohn Marino 	int subvis = type_visibility (ftype);
2320e4b17023SJohn Marino 
2321e4b17023SJohn Marino 	if (subvis == VISIBILITY_ANON)
2322e4b17023SJohn Marino 	  {
2323e4b17023SJohn Marino 	    if (!in_main_input_context ())
2324e4b17023SJohn Marino 	      warning (0, "\
2325e4b17023SJohn Marino %qT has a field %qD whose type uses the anonymous namespace",
2326e4b17023SJohn Marino 		       type, t);
2327e4b17023SJohn Marino 	  }
2328e4b17023SJohn Marino 	else if (MAYBE_CLASS_TYPE_P (ftype)
2329e4b17023SJohn Marino 		 && vis < VISIBILITY_HIDDEN
2330e4b17023SJohn Marino 		 && subvis >= VISIBILITY_HIDDEN)
2331e4b17023SJohn Marino 	  warning (OPT_Wattributes, "\
2332e4b17023SJohn Marino %qT declared with greater visibility than the type of its field %qD",
2333e4b17023SJohn Marino 		   type, t);
2334e4b17023SJohn Marino       }
2335e4b17023SJohn Marino 
2336e4b17023SJohn Marino   binfo = TYPE_BINFO (type);
2337e4b17023SJohn Marino   for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
2338e4b17023SJohn Marino     {
2339e4b17023SJohn Marino       int subvis = type_visibility (TREE_TYPE (t));
2340e4b17023SJohn Marino 
2341e4b17023SJohn Marino       if (subvis == VISIBILITY_ANON)
2342e4b17023SJohn Marino         {
2343e4b17023SJohn Marino 	  if (!in_main_input_context())
2344e4b17023SJohn Marino 	    warning (0, "\
2345e4b17023SJohn Marino %qT has a base %qT whose type uses the anonymous namespace",
2346e4b17023SJohn Marino 		     type, TREE_TYPE (t));
2347e4b17023SJohn Marino 	}
2348e4b17023SJohn Marino       else if (vis < VISIBILITY_HIDDEN
2349e4b17023SJohn Marino 	       && subvis >= VISIBILITY_HIDDEN)
2350e4b17023SJohn Marino 	warning (OPT_Wattributes, "\
2351e4b17023SJohn Marino %qT declared with greater visibility than its base %qT",
2352e4b17023SJohn Marino 		 type, TREE_TYPE (t));
2353e4b17023SJohn Marino     }
2354e4b17023SJohn Marino }
2355e4b17023SJohn Marino 
2356e4b17023SJohn Marino /* DECL is a FUNCTION_DECL or VAR_DECL.  If the object file linkage
2357e4b17023SJohn Marino    for DECL has not already been determined, do so now by setting
2358e4b17023SJohn Marino    DECL_EXTERNAL, DECL_COMDAT and other related flags.  Until this
2359e4b17023SJohn Marino    function is called entities with vague linkage whose definitions
2360e4b17023SJohn Marino    are available must have TREE_PUBLIC set.
2361e4b17023SJohn Marino 
2362e4b17023SJohn Marino    If this function decides to place DECL in COMDAT, it will set
2363e4b17023SJohn Marino    appropriate flags -- but will not clear DECL_EXTERNAL.  It is up to
2364e4b17023SJohn Marino    the caller to decide whether or not to clear DECL_EXTERNAL.  Some
2365e4b17023SJohn Marino    callers defer that decision until it is clear that DECL is actually
2366e4b17023SJohn Marino    required.  */
2367e4b17023SJohn Marino 
2368e4b17023SJohn Marino void
import_export_decl(tree decl)2369e4b17023SJohn Marino import_export_decl (tree decl)
2370e4b17023SJohn Marino {
2371e4b17023SJohn Marino   int emit_p;
2372e4b17023SJohn Marino   bool comdat_p;
2373e4b17023SJohn Marino   bool import_p;
2374e4b17023SJohn Marino   tree class_type = NULL_TREE;
2375e4b17023SJohn Marino 
2376e4b17023SJohn Marino   if (DECL_INTERFACE_KNOWN (decl))
2377e4b17023SJohn Marino     return;
2378e4b17023SJohn Marino 
2379e4b17023SJohn Marino   /* We cannot determine what linkage to give to an entity with vague
2380e4b17023SJohn Marino      linkage until the end of the file.  For example, a virtual table
2381e4b17023SJohn Marino      for a class will be defined if and only if the key method is
2382e4b17023SJohn Marino      defined in this translation unit.  As a further example, consider
2383e4b17023SJohn Marino      that when compiling a translation unit that uses PCH file with
2384e4b17023SJohn Marino      "-frepo" it would be incorrect to make decisions about what
2385e4b17023SJohn Marino      entities to emit when building the PCH; those decisions must be
2386e4b17023SJohn Marino      delayed until the repository information has been processed.  */
2387e4b17023SJohn Marino   gcc_assert (at_eof);
2388e4b17023SJohn Marino   /* Object file linkage for explicit instantiations is handled in
2389e4b17023SJohn Marino      mark_decl_instantiated.  For static variables in functions with
2390e4b17023SJohn Marino      vague linkage, maybe_commonize_var is used.
2391e4b17023SJohn Marino 
2392e4b17023SJohn Marino      Therefore, the only declarations that should be provided to this
2393e4b17023SJohn Marino      function are those with external linkage that are:
2394e4b17023SJohn Marino 
2395e4b17023SJohn Marino      * implicit instantiations of function templates
2396e4b17023SJohn Marino 
2397e4b17023SJohn Marino      * inline function
2398e4b17023SJohn Marino 
2399e4b17023SJohn Marino      * implicit instantiations of static data members of class
2400e4b17023SJohn Marino        templates
2401e4b17023SJohn Marino 
2402e4b17023SJohn Marino      * virtual tables
2403e4b17023SJohn Marino 
2404e4b17023SJohn Marino      * typeinfo objects
2405e4b17023SJohn Marino 
2406e4b17023SJohn Marino      Furthermore, all entities that reach this point must have a
2407e4b17023SJohn Marino      definition available in this translation unit.
2408e4b17023SJohn Marino 
2409e4b17023SJohn Marino      The following assertions check these conditions.  */
2410e4b17023SJohn Marino   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
2411e4b17023SJohn Marino 	      || TREE_CODE (decl) == VAR_DECL);
2412e4b17023SJohn Marino   /* Any code that creates entities with TREE_PUBLIC cleared should
2413e4b17023SJohn Marino      also set DECL_INTERFACE_KNOWN.  */
2414e4b17023SJohn Marino   gcc_assert (TREE_PUBLIC (decl));
2415e4b17023SJohn Marino   if (TREE_CODE (decl) == FUNCTION_DECL)
2416e4b17023SJohn Marino     gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2417e4b17023SJohn Marino 		|| DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
2418e4b17023SJohn Marino 		|| DECL_DECLARED_INLINE_P (decl));
2419e4b17023SJohn Marino   else
2420e4b17023SJohn Marino     gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
2421e4b17023SJohn Marino 		|| DECL_VTABLE_OR_VTT_P (decl)
2422e4b17023SJohn Marino 		|| DECL_TINFO_P (decl));
2423e4b17023SJohn Marino   /* Check that a definition of DECL is available in this translation
2424e4b17023SJohn Marino      unit.  */
2425e4b17023SJohn Marino   gcc_assert (!DECL_REALLY_EXTERN (decl));
2426e4b17023SJohn Marino 
2427e4b17023SJohn Marino   /* Assume that DECL will not have COMDAT linkage.  */
2428e4b17023SJohn Marino   comdat_p = false;
2429e4b17023SJohn Marino   /* Assume that DECL will not be imported into this translation
2430e4b17023SJohn Marino      unit.  */
2431e4b17023SJohn Marino   import_p = false;
2432e4b17023SJohn Marino 
2433e4b17023SJohn Marino   /* See if the repository tells us whether or not to emit DECL in
2434e4b17023SJohn Marino      this translation unit.  */
2435e4b17023SJohn Marino   emit_p = repo_emit_p (decl);
2436e4b17023SJohn Marino   if (emit_p == 0)
2437e4b17023SJohn Marino     import_p = true;
2438e4b17023SJohn Marino   else if (emit_p == 1)
2439e4b17023SJohn Marino     {
2440e4b17023SJohn Marino       /* The repository indicates that this entity should be defined
2441e4b17023SJohn Marino 	 here.  Make sure the back end honors that request.  */
2442e4b17023SJohn Marino       if (TREE_CODE (decl) == VAR_DECL)
2443e4b17023SJohn Marino 	mark_needed (decl);
2444e4b17023SJohn Marino       else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
2445e4b17023SJohn Marino 	       || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2446e4b17023SJohn Marino 	{
2447e4b17023SJohn Marino 	  tree clone;
2448e4b17023SJohn Marino 	  FOR_EACH_CLONE (clone, decl)
2449e4b17023SJohn Marino 	    mark_needed (clone);
2450e4b17023SJohn Marino 	}
2451e4b17023SJohn Marino       else
2452e4b17023SJohn Marino 	mark_needed (decl);
2453e4b17023SJohn Marino       /* Output the definition as an ordinary strong definition.  */
2454e4b17023SJohn Marino       DECL_EXTERNAL (decl) = 0;
2455e4b17023SJohn Marino       DECL_INTERFACE_KNOWN (decl) = 1;
2456e4b17023SJohn Marino       return;
2457e4b17023SJohn Marino     }
2458e4b17023SJohn Marino 
2459e4b17023SJohn Marino   if (import_p)
2460e4b17023SJohn Marino     /* We have already decided what to do with this DECL; there is no
2461e4b17023SJohn Marino        need to check anything further.  */
2462e4b17023SJohn Marino     ;
2463e4b17023SJohn Marino   else if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
2464e4b17023SJohn Marino     {
2465e4b17023SJohn Marino       class_type = DECL_CONTEXT (decl);
2466e4b17023SJohn Marino       import_export_class (class_type);
2467e4b17023SJohn Marino       if (TYPE_FOR_JAVA (class_type))
2468e4b17023SJohn Marino 	import_p = true;
2469e4b17023SJohn Marino       else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
2470e4b17023SJohn Marino 	       && CLASSTYPE_INTERFACE_ONLY (class_type))
2471e4b17023SJohn Marino 	import_p = true;
2472e4b17023SJohn Marino       else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
2473e4b17023SJohn Marino 	       && !CLASSTYPE_USE_TEMPLATE (class_type)
2474e4b17023SJohn Marino 	       && CLASSTYPE_KEY_METHOD (class_type)
2475e4b17023SJohn Marino 	       && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
2476e4b17023SJohn Marino 	/* The ABI requires that all virtual tables be emitted with
2477e4b17023SJohn Marino 	   COMDAT linkage.  However, on systems where COMDAT symbols
2478e4b17023SJohn Marino 	   don't show up in the table of contents for a static
2479e4b17023SJohn Marino 	   archive, or on systems without weak symbols (where we
2480e4b17023SJohn Marino 	   approximate COMDAT linkage by using internal linkage), the
2481e4b17023SJohn Marino 	   linker will report errors about undefined symbols because
2482e4b17023SJohn Marino 	   it will not see the virtual table definition.  Therefore,
2483e4b17023SJohn Marino 	   in the case that we know that the virtual table will be
2484e4b17023SJohn Marino 	   emitted in only one translation unit, we make the virtual
2485e4b17023SJohn Marino 	   table an ordinary definition with external linkage.  */
2486e4b17023SJohn Marino 	DECL_EXTERNAL (decl) = 0;
2487e4b17023SJohn Marino       else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
2488e4b17023SJohn Marino 	{
2489e4b17023SJohn Marino 	  /* CLASS_TYPE is being exported from this translation unit,
2490e4b17023SJohn Marino 	     so DECL should be defined here.  */
2491e4b17023SJohn Marino 	  if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
2492e4b17023SJohn Marino 	    /* If a class is declared in a header with the "extern
2493e4b17023SJohn Marino 	       template" extension, then it will not be instantiated,
2494e4b17023SJohn Marino 	       even in translation units that would normally require
2495e4b17023SJohn Marino 	       it.  Often such classes are explicitly instantiated in
2496e4b17023SJohn Marino 	       one translation unit.  Therefore, the explicit
2497e4b17023SJohn Marino 	       instantiation must be made visible to other translation
2498e4b17023SJohn Marino 	       units.  */
2499e4b17023SJohn Marino 	    DECL_EXTERNAL (decl) = 0;
2500e4b17023SJohn Marino 	  else
2501e4b17023SJohn Marino 	    {
2502e4b17023SJohn Marino 	      /* The generic C++ ABI says that class data is always
2503e4b17023SJohn Marino 		 COMDAT, even if there is a key function.  Some
2504e4b17023SJohn Marino 		 variants (e.g., the ARM EABI) says that class data
2505e4b17023SJohn Marino 		 only has COMDAT linkage if the class data might be
2506e4b17023SJohn Marino 		 emitted in more than one translation unit.  When the
2507e4b17023SJohn Marino 		 key method can be inline and is inline, we still have
2508e4b17023SJohn Marino 		 to arrange for comdat even though
2509e4b17023SJohn Marino 		 class_data_always_comdat is false.  */
2510e4b17023SJohn Marino 	      if (!CLASSTYPE_KEY_METHOD (class_type)
2511e4b17023SJohn Marino 		  || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
2512e4b17023SJohn Marino 		  || targetm.cxx.class_data_always_comdat ())
2513e4b17023SJohn Marino 		{
2514e4b17023SJohn Marino 		  /* The ABI requires COMDAT linkage.  Normally, we
2515e4b17023SJohn Marino 		     only emit COMDAT things when they are needed;
2516e4b17023SJohn Marino 		     make sure that we realize that this entity is
2517e4b17023SJohn Marino 		     indeed needed.  */
2518e4b17023SJohn Marino 		  comdat_p = true;
2519e4b17023SJohn Marino 		  mark_needed (decl);
2520e4b17023SJohn Marino 		}
2521e4b17023SJohn Marino 	    }
2522e4b17023SJohn Marino 	}
2523e4b17023SJohn Marino       else if (!flag_implicit_templates
2524e4b17023SJohn Marino 	       && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
2525e4b17023SJohn Marino 	import_p = true;
2526e4b17023SJohn Marino       else
2527e4b17023SJohn Marino 	comdat_p = true;
2528e4b17023SJohn Marino     }
2529e4b17023SJohn Marino   else if (TREE_CODE (decl) == VAR_DECL && DECL_TINFO_P (decl))
2530e4b17023SJohn Marino     {
2531e4b17023SJohn Marino       tree type = TREE_TYPE (DECL_NAME (decl));
2532e4b17023SJohn Marino       if (CLASS_TYPE_P (type))
2533e4b17023SJohn Marino 	{
2534e4b17023SJohn Marino 	  class_type = type;
2535e4b17023SJohn Marino 	  import_export_class (type);
2536e4b17023SJohn Marino 	  if (CLASSTYPE_INTERFACE_KNOWN (type)
2537e4b17023SJohn Marino 	      && TYPE_POLYMORPHIC_P (type)
2538e4b17023SJohn Marino 	      && CLASSTYPE_INTERFACE_ONLY (type)
2539e4b17023SJohn Marino 	      /* If -fno-rtti was specified, then we cannot be sure
2540e4b17023SJohn Marino 		 that RTTI information will be emitted with the
2541e4b17023SJohn Marino 		 virtual table of the class, so we must emit it
2542e4b17023SJohn Marino 		 wherever it is used.  */
2543e4b17023SJohn Marino 	      && flag_rtti)
2544e4b17023SJohn Marino 	    import_p = true;
2545e4b17023SJohn Marino 	  else
2546e4b17023SJohn Marino 	    {
2547e4b17023SJohn Marino 	      if (CLASSTYPE_INTERFACE_KNOWN (type)
2548e4b17023SJohn Marino 		  && !CLASSTYPE_INTERFACE_ONLY (type))
2549e4b17023SJohn Marino 		{
2550e4b17023SJohn Marino 		  comdat_p = (targetm.cxx.class_data_always_comdat ()
2551e4b17023SJohn Marino 			      || (CLASSTYPE_KEY_METHOD (type)
2552e4b17023SJohn Marino 				  && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
2553e4b17023SJohn Marino 		  mark_needed (decl);
2554e4b17023SJohn Marino 		  if (!flag_weak)
2555e4b17023SJohn Marino 		    {
2556e4b17023SJohn Marino 		      comdat_p = false;
2557e4b17023SJohn Marino 		      DECL_EXTERNAL (decl) = 0;
2558e4b17023SJohn Marino 		    }
2559e4b17023SJohn Marino 		}
2560e4b17023SJohn Marino 	      else
2561e4b17023SJohn Marino 		comdat_p = true;
2562e4b17023SJohn Marino 	    }
2563e4b17023SJohn Marino 	}
2564e4b17023SJohn Marino       else
2565e4b17023SJohn Marino 	comdat_p = true;
2566e4b17023SJohn Marino     }
2567e4b17023SJohn Marino   else if (DECL_TEMPLATE_INSTANTIATION (decl)
2568e4b17023SJohn Marino 	   || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2569e4b17023SJohn Marino     {
2570e4b17023SJohn Marino       /* DECL is an implicit instantiation of a function or static
2571e4b17023SJohn Marino 	 data member.  */
2572e4b17023SJohn Marino       if ((flag_implicit_templates
2573e4b17023SJohn Marino 	   && !flag_use_repository)
2574e4b17023SJohn Marino 	  || (flag_implicit_inline_templates
2575e4b17023SJohn Marino 	      && TREE_CODE (decl) == FUNCTION_DECL
2576e4b17023SJohn Marino 	      && DECL_DECLARED_INLINE_P (decl)))
2577e4b17023SJohn Marino 	comdat_p = true;
2578e4b17023SJohn Marino       else
2579e4b17023SJohn Marino 	/* If we are not implicitly generating templates, then mark
2580e4b17023SJohn Marino 	   this entity as undefined in this translation unit.  */
2581e4b17023SJohn Marino 	import_p = true;
2582e4b17023SJohn Marino     }
2583e4b17023SJohn Marino   else if (DECL_FUNCTION_MEMBER_P (decl))
2584e4b17023SJohn Marino     {
2585e4b17023SJohn Marino       if (!DECL_DECLARED_INLINE_P (decl))
2586e4b17023SJohn Marino 	{
2587e4b17023SJohn Marino 	  tree ctype = DECL_CONTEXT (decl);
2588e4b17023SJohn Marino 	  import_export_class (ctype);
2589e4b17023SJohn Marino 	  if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2590e4b17023SJohn Marino 	    {
2591e4b17023SJohn Marino 	      DECL_NOT_REALLY_EXTERN (decl)
2592e4b17023SJohn Marino 		= ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2593e4b17023SJohn Marino 		     || (DECL_DECLARED_INLINE_P (decl)
2594e4b17023SJohn Marino 			 && ! flag_implement_inlines
2595e4b17023SJohn Marino 			 && !DECL_VINDEX (decl)));
2596e4b17023SJohn Marino 
2597e4b17023SJohn Marino 	      if (!DECL_NOT_REALLY_EXTERN (decl))
2598e4b17023SJohn Marino 		DECL_EXTERNAL (decl) = 1;
2599e4b17023SJohn Marino 
2600e4b17023SJohn Marino 	      /* Always make artificials weak.  */
2601e4b17023SJohn Marino 	      if (DECL_ARTIFICIAL (decl) && flag_weak)
2602e4b17023SJohn Marino 		comdat_p = true;
2603e4b17023SJohn Marino 	      else
2604e4b17023SJohn Marino 		maybe_make_one_only (decl);
2605e4b17023SJohn Marino 	    }
2606e4b17023SJohn Marino 	}
2607e4b17023SJohn Marino       else
2608e4b17023SJohn Marino 	comdat_p = true;
2609e4b17023SJohn Marino     }
2610e4b17023SJohn Marino   else
2611e4b17023SJohn Marino     comdat_p = true;
2612e4b17023SJohn Marino 
2613e4b17023SJohn Marino   if (import_p)
2614e4b17023SJohn Marino     {
2615e4b17023SJohn Marino       /* If we are importing DECL into this translation unit, mark is
2616e4b17023SJohn Marino 	 an undefined here.  */
2617e4b17023SJohn Marino       DECL_EXTERNAL (decl) = 1;
2618e4b17023SJohn Marino       DECL_NOT_REALLY_EXTERN (decl) = 0;
2619e4b17023SJohn Marino     }
2620e4b17023SJohn Marino   else if (comdat_p)
2621e4b17023SJohn Marino     {
2622e4b17023SJohn Marino       /* If we decided to put DECL in COMDAT, mark it accordingly at
2623e4b17023SJohn Marino 	 this point.  */
2624e4b17023SJohn Marino       comdat_linkage (decl);
2625e4b17023SJohn Marino     }
2626e4b17023SJohn Marino 
2627e4b17023SJohn Marino   DECL_INTERFACE_KNOWN (decl) = 1;
2628e4b17023SJohn Marino }
2629e4b17023SJohn Marino 
2630e4b17023SJohn Marino /* Return an expression that performs the destruction of DECL, which
2631e4b17023SJohn Marino    must be a VAR_DECL whose type has a non-trivial destructor, or is
2632e4b17023SJohn Marino    an array whose (innermost) elements have a non-trivial destructor.  */
2633e4b17023SJohn Marino 
2634e4b17023SJohn Marino tree
build_cleanup(tree decl)2635e4b17023SJohn Marino build_cleanup (tree decl)
2636e4b17023SJohn Marino {
2637e4b17023SJohn Marino   tree temp;
2638e4b17023SJohn Marino   tree type = TREE_TYPE (decl);
2639e4b17023SJohn Marino 
2640e4b17023SJohn Marino   /* This function should only be called for declarations that really
2641e4b17023SJohn Marino      require cleanups.  */
2642e4b17023SJohn Marino   gcc_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type));
2643e4b17023SJohn Marino 
2644e4b17023SJohn Marino   /* Treat all objects with destructors as used; the destructor may do
2645e4b17023SJohn Marino      something substantive.  */
2646e4b17023SJohn Marino   mark_used (decl);
2647e4b17023SJohn Marino 
2648e4b17023SJohn Marino   if (TREE_CODE (type) == ARRAY_TYPE)
2649e4b17023SJohn Marino     temp = decl;
2650e4b17023SJohn Marino   else
2651e4b17023SJohn Marino     temp = build_address (decl);
2652e4b17023SJohn Marino   temp = build_delete (TREE_TYPE (temp), temp,
2653e4b17023SJohn Marino 		       sfk_complete_destructor,
2654e4b17023SJohn Marino 		       LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
2655e4b17023SJohn Marino 		       tf_warning_or_error);
2656e4b17023SJohn Marino   return temp;
2657e4b17023SJohn Marino }
2658e4b17023SJohn Marino 
2659e4b17023SJohn Marino /* Returns the initialization guard variable for the variable DECL,
2660e4b17023SJohn Marino    which has static storage duration.  */
2661e4b17023SJohn Marino 
2662e4b17023SJohn Marino tree
get_guard(tree decl)2663e4b17023SJohn Marino get_guard (tree decl)
2664e4b17023SJohn Marino {
2665e4b17023SJohn Marino   tree sname;
2666e4b17023SJohn Marino   tree guard;
2667e4b17023SJohn Marino 
2668e4b17023SJohn Marino   sname = mangle_guard_variable (decl);
2669e4b17023SJohn Marino   guard = IDENTIFIER_GLOBAL_VALUE (sname);
2670e4b17023SJohn Marino   if (! guard)
2671e4b17023SJohn Marino     {
2672e4b17023SJohn Marino       tree guard_type;
2673e4b17023SJohn Marino 
2674e4b17023SJohn Marino       /* We use a type that is big enough to contain a mutex as well
2675e4b17023SJohn Marino 	 as an integer counter.  */
2676e4b17023SJohn Marino       guard_type = targetm.cxx.guard_type ();
2677e4b17023SJohn Marino       guard = build_decl (DECL_SOURCE_LOCATION (decl),
2678e4b17023SJohn Marino 			  VAR_DECL, sname, guard_type);
2679e4b17023SJohn Marino 
2680e4b17023SJohn Marino       /* The guard should have the same linkage as what it guards.  */
2681e4b17023SJohn Marino       TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2682e4b17023SJohn Marino       TREE_STATIC (guard) = TREE_STATIC (decl);
2683e4b17023SJohn Marino       DECL_COMMON (guard) = DECL_COMMON (decl);
2684e4b17023SJohn Marino       DECL_COMDAT (guard) = DECL_COMDAT (decl);
2685e4b17023SJohn Marino       if (DECL_ONE_ONLY (decl))
2686e4b17023SJohn Marino 	make_decl_one_only (guard, cxx_comdat_group (guard));
2687e4b17023SJohn Marino       if (TREE_PUBLIC (decl))
2688e4b17023SJohn Marino 	DECL_WEAK (guard) = DECL_WEAK (decl);
2689e4b17023SJohn Marino       DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
2690e4b17023SJohn Marino       DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
2691e4b17023SJohn Marino 
2692e4b17023SJohn Marino       DECL_ARTIFICIAL (guard) = 1;
2693e4b17023SJohn Marino       DECL_IGNORED_P (guard) = 1;
2694e4b17023SJohn Marino       TREE_USED (guard) = 1;
2695e4b17023SJohn Marino       pushdecl_top_level_and_finish (guard, NULL_TREE);
2696e4b17023SJohn Marino     }
2697e4b17023SJohn Marino   return guard;
2698e4b17023SJohn Marino }
2699e4b17023SJohn Marino 
2700e4b17023SJohn Marino /* Return those bits of the GUARD variable that should be set when the
2701e4b17023SJohn Marino    guarded entity is actually initialized.  */
2702e4b17023SJohn Marino 
2703e4b17023SJohn Marino static tree
get_guard_bits(tree guard)2704e4b17023SJohn Marino get_guard_bits (tree guard)
2705e4b17023SJohn Marino {
2706e4b17023SJohn Marino   if (!targetm.cxx.guard_mask_bit ())
2707e4b17023SJohn Marino     {
2708e4b17023SJohn Marino       /* We only set the first byte of the guard, in order to leave room
2709e4b17023SJohn Marino 	 for a mutex in the high-order bits.  */
2710e4b17023SJohn Marino       guard = build1 (ADDR_EXPR,
2711e4b17023SJohn Marino 		      build_pointer_type (TREE_TYPE (guard)),
2712e4b17023SJohn Marino 		      guard);
2713e4b17023SJohn Marino       guard = build1 (NOP_EXPR,
2714e4b17023SJohn Marino 		      build_pointer_type (char_type_node),
2715e4b17023SJohn Marino 		      guard);
2716e4b17023SJohn Marino       guard = build1 (INDIRECT_REF, char_type_node, guard);
2717e4b17023SJohn Marino     }
2718e4b17023SJohn Marino 
2719e4b17023SJohn Marino   return guard;
2720e4b17023SJohn Marino }
2721e4b17023SJohn Marino 
2722e4b17023SJohn Marino /* Return an expression which determines whether or not the GUARD
2723e4b17023SJohn Marino    variable has already been initialized.  */
2724e4b17023SJohn Marino 
2725e4b17023SJohn Marino tree
get_guard_cond(tree guard)2726e4b17023SJohn Marino get_guard_cond (tree guard)
2727e4b17023SJohn Marino {
2728e4b17023SJohn Marino   tree guard_value;
2729e4b17023SJohn Marino 
2730e4b17023SJohn Marino   /* Check to see if the GUARD is zero.  */
2731e4b17023SJohn Marino   guard = get_guard_bits (guard);
2732e4b17023SJohn Marino 
2733e4b17023SJohn Marino   /* Mask off all but the low bit.  */
2734e4b17023SJohn Marino   if (targetm.cxx.guard_mask_bit ())
2735e4b17023SJohn Marino     {
2736e4b17023SJohn Marino       guard_value = integer_one_node;
2737e4b17023SJohn Marino       if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2738e4b17023SJohn Marino 	guard_value = convert (TREE_TYPE (guard), guard_value);
2739e4b17023SJohn Marino       guard = cp_build_binary_op (input_location,
2740e4b17023SJohn Marino 				  BIT_AND_EXPR, guard, guard_value,
2741e4b17023SJohn Marino 				  tf_warning_or_error);
2742e4b17023SJohn Marino     }
2743e4b17023SJohn Marino 
2744e4b17023SJohn Marino   guard_value = integer_zero_node;
2745e4b17023SJohn Marino   if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2746e4b17023SJohn Marino     guard_value = convert (TREE_TYPE (guard), guard_value);
2747e4b17023SJohn Marino   return cp_build_binary_op (input_location,
2748e4b17023SJohn Marino 			     EQ_EXPR, guard, guard_value,
2749e4b17023SJohn Marino 			     tf_warning_or_error);
2750e4b17023SJohn Marino }
2751e4b17023SJohn Marino 
2752e4b17023SJohn Marino /* Return an expression which sets the GUARD variable, indicating that
2753e4b17023SJohn Marino    the variable being guarded has been initialized.  */
2754e4b17023SJohn Marino 
2755e4b17023SJohn Marino tree
set_guard(tree guard)2756e4b17023SJohn Marino set_guard (tree guard)
2757e4b17023SJohn Marino {
2758e4b17023SJohn Marino   tree guard_init;
2759e4b17023SJohn Marino 
2760e4b17023SJohn Marino   /* Set the GUARD to one.  */
2761e4b17023SJohn Marino   guard = get_guard_bits (guard);
2762e4b17023SJohn Marino   guard_init = integer_one_node;
2763e4b17023SJohn Marino   if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2764e4b17023SJohn Marino     guard_init = convert (TREE_TYPE (guard), guard_init);
2765e4b17023SJohn Marino   return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
2766e4b17023SJohn Marino 			       tf_warning_or_error);
2767e4b17023SJohn Marino }
2768e4b17023SJohn Marino 
2769e4b17023SJohn Marino /* Start the process of running a particular set of global constructors
2770e4b17023SJohn Marino    or destructors.  Subroutine of do_[cd]tors.  */
2771e4b17023SJohn Marino 
2772e4b17023SJohn Marino static tree
start_objects(int method_type,int initp)2773e4b17023SJohn Marino start_objects (int method_type, int initp)
2774e4b17023SJohn Marino {
2775e4b17023SJohn Marino   tree body;
2776e4b17023SJohn Marino   tree fndecl;
2777e4b17023SJohn Marino   char type[14];
2778e4b17023SJohn Marino 
2779e4b17023SJohn Marino   /* Make ctor or dtor function.  METHOD_TYPE may be 'I' or 'D'.  */
2780e4b17023SJohn Marino 
2781e4b17023SJohn Marino   if (initp != DEFAULT_INIT_PRIORITY)
2782e4b17023SJohn Marino     {
2783e4b17023SJohn Marino       char joiner;
2784e4b17023SJohn Marino 
2785e4b17023SJohn Marino #ifdef JOINER
2786e4b17023SJohn Marino       joiner = JOINER;
2787e4b17023SJohn Marino #else
2788e4b17023SJohn Marino       joiner = '_';
2789e4b17023SJohn Marino #endif
2790e4b17023SJohn Marino 
2791e4b17023SJohn Marino       sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
2792e4b17023SJohn Marino     }
2793e4b17023SJohn Marino   else
2794e4b17023SJohn Marino     sprintf (type, "sub_%c", method_type);
2795e4b17023SJohn Marino 
2796e4b17023SJohn Marino   fndecl = build_lang_decl (FUNCTION_DECL,
2797e4b17023SJohn Marino 			    get_file_function_name (type),
2798e4b17023SJohn Marino 			    build_function_type_list (void_type_node,
2799e4b17023SJohn Marino 						      NULL_TREE));
2800e4b17023SJohn Marino   start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
2801e4b17023SJohn Marino 
2802e4b17023SJohn Marino   TREE_PUBLIC (current_function_decl) = 0;
2803e4b17023SJohn Marino 
2804e4b17023SJohn Marino   /* Mark as artificial because it's not explicitly in the user's
2805e4b17023SJohn Marino      source code.  */
2806e4b17023SJohn Marino   DECL_ARTIFICIAL (current_function_decl) = 1;
2807e4b17023SJohn Marino 
2808e4b17023SJohn Marino   /* Mark this declaration as used to avoid spurious warnings.  */
2809e4b17023SJohn Marino   TREE_USED (current_function_decl) = 1;
2810e4b17023SJohn Marino 
2811e4b17023SJohn Marino   /* Mark this function as a global constructor or destructor.  */
2812e4b17023SJohn Marino   if (method_type == 'I')
2813e4b17023SJohn Marino     DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2814e4b17023SJohn Marino   else
2815e4b17023SJohn Marino     DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2816e4b17023SJohn Marino 
2817e4b17023SJohn Marino   body = begin_compound_stmt (BCS_FN_BODY);
2818e4b17023SJohn Marino 
2819e4b17023SJohn Marino   return body;
2820e4b17023SJohn Marino }
2821e4b17023SJohn Marino 
2822e4b17023SJohn Marino /* Finish the process of running a particular set of global constructors
2823e4b17023SJohn Marino    or destructors.  Subroutine of do_[cd]tors.  */
2824e4b17023SJohn Marino 
2825e4b17023SJohn Marino static void
finish_objects(int method_type,int initp,tree body)2826e4b17023SJohn Marino finish_objects (int method_type, int initp, tree body)
2827e4b17023SJohn Marino {
2828e4b17023SJohn Marino   tree fn;
2829e4b17023SJohn Marino 
2830e4b17023SJohn Marino   /* Finish up.  */
2831e4b17023SJohn Marino   finish_compound_stmt (body);
2832e4b17023SJohn Marino   fn = finish_function (0);
2833e4b17023SJohn Marino 
2834e4b17023SJohn Marino   if (method_type == 'I')
2835e4b17023SJohn Marino     {
2836e4b17023SJohn Marino       DECL_STATIC_CONSTRUCTOR (fn) = 1;
2837e4b17023SJohn Marino       decl_init_priority_insert (fn, initp);
2838e4b17023SJohn Marino     }
2839e4b17023SJohn Marino   else
2840e4b17023SJohn Marino     {
2841e4b17023SJohn Marino       DECL_STATIC_DESTRUCTOR (fn) = 1;
2842e4b17023SJohn Marino       decl_fini_priority_insert (fn, initp);
2843e4b17023SJohn Marino     }
2844e4b17023SJohn Marino 
2845e4b17023SJohn Marino   expand_or_defer_fn (fn);
2846e4b17023SJohn Marino }
2847e4b17023SJohn Marino 
2848e4b17023SJohn Marino /* The names of the parameters to the function created to handle
2849e4b17023SJohn Marino    initializations and destructions for objects with static storage
2850e4b17023SJohn Marino    duration.  */
2851e4b17023SJohn Marino #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2852e4b17023SJohn Marino #define PRIORITY_IDENTIFIER "__priority"
2853e4b17023SJohn Marino 
2854e4b17023SJohn Marino /* The name of the function we create to handle initializations and
2855e4b17023SJohn Marino    destructions for objects with static storage duration.  */
2856e4b17023SJohn Marino #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2857e4b17023SJohn Marino 
2858e4b17023SJohn Marino /* The declaration for the __INITIALIZE_P argument.  */
2859e4b17023SJohn Marino static GTY(()) tree initialize_p_decl;
2860e4b17023SJohn Marino 
2861e4b17023SJohn Marino /* The declaration for the __PRIORITY argument.  */
2862e4b17023SJohn Marino static GTY(()) tree priority_decl;
2863e4b17023SJohn Marino 
2864e4b17023SJohn Marino /* The declaration for the static storage duration function.  */
2865e4b17023SJohn Marino static GTY(()) tree ssdf_decl;
2866e4b17023SJohn Marino 
2867e4b17023SJohn Marino /* All the static storage duration functions created in this
2868e4b17023SJohn Marino    translation unit.  */
2869e4b17023SJohn Marino static GTY(()) VEC(tree,gc) *ssdf_decls;
2870e4b17023SJohn Marino 
2871e4b17023SJohn Marino /* A map from priority levels to information about that priority
2872e4b17023SJohn Marino    level.  There may be many such levels, so efficient lookup is
2873e4b17023SJohn Marino    important.  */
2874e4b17023SJohn Marino static splay_tree priority_info_map;
2875e4b17023SJohn Marino 
2876e4b17023SJohn Marino /* Begins the generation of the function that will handle all
2877e4b17023SJohn Marino    initialization and destruction of objects with static storage
2878e4b17023SJohn Marino    duration.  The function generated takes two parameters of type
2879e4b17023SJohn Marino    `int': __INITIALIZE_P and __PRIORITY.  If __INITIALIZE_P is
2880e4b17023SJohn Marino    nonzero, it performs initializations.  Otherwise, it performs
2881e4b17023SJohn Marino    destructions.  It only performs those initializations or
2882e4b17023SJohn Marino    destructions with the indicated __PRIORITY.  The generated function
2883e4b17023SJohn Marino    returns no value.
2884e4b17023SJohn Marino 
2885e4b17023SJohn Marino    It is assumed that this function will only be called once per
2886e4b17023SJohn Marino    translation unit.  */
2887e4b17023SJohn Marino 
2888e4b17023SJohn Marino static tree
start_static_storage_duration_function(unsigned count)2889e4b17023SJohn Marino start_static_storage_duration_function (unsigned count)
2890e4b17023SJohn Marino {
2891e4b17023SJohn Marino   tree type;
2892e4b17023SJohn Marino   tree body;
2893e4b17023SJohn Marino   char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2894e4b17023SJohn Marino 
2895e4b17023SJohn Marino   /* Create the identifier for this function.  It will be of the form
2896e4b17023SJohn Marino      SSDF_IDENTIFIER_<number>.  */
2897e4b17023SJohn Marino   sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
2898e4b17023SJohn Marino 
2899e4b17023SJohn Marino   type = build_function_type_list (void_type_node,
2900e4b17023SJohn Marino 				   integer_type_node, integer_type_node,
2901e4b17023SJohn Marino 				   NULL_TREE);
2902e4b17023SJohn Marino 
2903e4b17023SJohn Marino   /* Create the FUNCTION_DECL itself.  */
2904e4b17023SJohn Marino   ssdf_decl = build_lang_decl (FUNCTION_DECL,
2905e4b17023SJohn Marino 			       get_identifier (id),
2906e4b17023SJohn Marino 			       type);
2907e4b17023SJohn Marino   TREE_PUBLIC (ssdf_decl) = 0;
2908e4b17023SJohn Marino   DECL_ARTIFICIAL (ssdf_decl) = 1;
2909e4b17023SJohn Marino 
2910e4b17023SJohn Marino   /* Put this function in the list of functions to be called from the
2911e4b17023SJohn Marino      static constructors and destructors.  */
2912e4b17023SJohn Marino   if (!ssdf_decls)
2913e4b17023SJohn Marino     {
2914e4b17023SJohn Marino       ssdf_decls = VEC_alloc (tree, gc, 32);
2915e4b17023SJohn Marino 
2916e4b17023SJohn Marino       /* Take this opportunity to initialize the map from priority
2917e4b17023SJohn Marino 	 numbers to information about that priority level.  */
2918e4b17023SJohn Marino       priority_info_map = splay_tree_new (splay_tree_compare_ints,
2919e4b17023SJohn Marino 					  /*delete_key_fn=*/0,
2920e4b17023SJohn Marino 					  /*delete_value_fn=*/
2921e4b17023SJohn Marino 					  (splay_tree_delete_value_fn) &free);
2922e4b17023SJohn Marino 
2923e4b17023SJohn Marino       /* We always need to generate functions for the
2924e4b17023SJohn Marino 	 DEFAULT_INIT_PRIORITY so enter it now.  That way when we walk
2925e4b17023SJohn Marino 	 priorities later, we'll be sure to find the
2926e4b17023SJohn Marino 	 DEFAULT_INIT_PRIORITY.  */
2927e4b17023SJohn Marino       get_priority_info (DEFAULT_INIT_PRIORITY);
2928e4b17023SJohn Marino     }
2929e4b17023SJohn Marino 
2930e4b17023SJohn Marino   VEC_safe_push (tree, gc, ssdf_decls, ssdf_decl);
2931e4b17023SJohn Marino 
2932e4b17023SJohn Marino   /* Create the argument list.  */
2933e4b17023SJohn Marino   initialize_p_decl = cp_build_parm_decl
2934e4b17023SJohn Marino     (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
2935e4b17023SJohn Marino   DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2936e4b17023SJohn Marino   TREE_USED (initialize_p_decl) = 1;
2937e4b17023SJohn Marino   priority_decl = cp_build_parm_decl
2938e4b17023SJohn Marino     (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
2939e4b17023SJohn Marino   DECL_CONTEXT (priority_decl) = ssdf_decl;
2940e4b17023SJohn Marino   TREE_USED (priority_decl) = 1;
2941e4b17023SJohn Marino 
2942e4b17023SJohn Marino   DECL_CHAIN (initialize_p_decl) = priority_decl;
2943e4b17023SJohn Marino   DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2944e4b17023SJohn Marino 
2945e4b17023SJohn Marino   /* Put the function in the global scope.  */
2946e4b17023SJohn Marino   pushdecl (ssdf_decl);
2947e4b17023SJohn Marino 
2948e4b17023SJohn Marino   /* Start the function itself.  This is equivalent to declaring the
2949e4b17023SJohn Marino      function as:
2950e4b17023SJohn Marino 
2951e4b17023SJohn Marino        static void __ssdf (int __initialize_p, init __priority_p);
2952e4b17023SJohn Marino 
2953e4b17023SJohn Marino      It is static because we only need to call this function from the
2954e4b17023SJohn Marino      various constructor and destructor functions for this module.  */
2955e4b17023SJohn Marino   start_preparsed_function (ssdf_decl,
2956e4b17023SJohn Marino 			    /*attrs=*/NULL_TREE,
2957e4b17023SJohn Marino 			    SF_PRE_PARSED);
2958e4b17023SJohn Marino 
2959e4b17023SJohn Marino   /* Set up the scope of the outermost block in the function.  */
2960e4b17023SJohn Marino   body = begin_compound_stmt (BCS_FN_BODY);
2961e4b17023SJohn Marino 
2962e4b17023SJohn Marino   return body;
2963e4b17023SJohn Marino }
2964e4b17023SJohn Marino 
2965e4b17023SJohn Marino /* Finish the generation of the function which performs initialization
2966e4b17023SJohn Marino    and destruction of objects with static storage duration.  After
2967e4b17023SJohn Marino    this point, no more such objects can be created.  */
2968e4b17023SJohn Marino 
2969e4b17023SJohn Marino static void
finish_static_storage_duration_function(tree body)2970e4b17023SJohn Marino finish_static_storage_duration_function (tree body)
2971e4b17023SJohn Marino {
2972e4b17023SJohn Marino   /* Close out the function.  */
2973e4b17023SJohn Marino   finish_compound_stmt (body);
2974e4b17023SJohn Marino   expand_or_defer_fn (finish_function (0));
2975e4b17023SJohn Marino }
2976e4b17023SJohn Marino 
2977e4b17023SJohn Marino /* Return the information about the indicated PRIORITY level.  If no
2978e4b17023SJohn Marino    code to handle this level has yet been generated, generate the
2979e4b17023SJohn Marino    appropriate prologue.  */
2980e4b17023SJohn Marino 
2981e4b17023SJohn Marino static priority_info
get_priority_info(int priority)2982e4b17023SJohn Marino get_priority_info (int priority)
2983e4b17023SJohn Marino {
2984e4b17023SJohn Marino   priority_info pi;
2985e4b17023SJohn Marino   splay_tree_node n;
2986e4b17023SJohn Marino 
2987e4b17023SJohn Marino   n = splay_tree_lookup (priority_info_map,
2988e4b17023SJohn Marino 			 (splay_tree_key) priority);
2989e4b17023SJohn Marino   if (!n)
2990e4b17023SJohn Marino     {
2991e4b17023SJohn Marino       /* Create a new priority information structure, and insert it
2992e4b17023SJohn Marino 	 into the map.  */
2993e4b17023SJohn Marino       pi = XNEW (struct priority_info_s);
2994e4b17023SJohn Marino       pi->initializations_p = 0;
2995e4b17023SJohn Marino       pi->destructions_p = 0;
2996e4b17023SJohn Marino       splay_tree_insert (priority_info_map,
2997e4b17023SJohn Marino 			 (splay_tree_key) priority,
2998e4b17023SJohn Marino 			 (splay_tree_value) pi);
2999e4b17023SJohn Marino     }
3000e4b17023SJohn Marino   else
3001e4b17023SJohn Marino     pi = (priority_info) n->value;
3002e4b17023SJohn Marino 
3003e4b17023SJohn Marino   return pi;
3004e4b17023SJohn Marino }
3005e4b17023SJohn Marino 
3006e4b17023SJohn Marino /* The effective initialization priority of a DECL.  */
3007e4b17023SJohn Marino 
3008e4b17023SJohn Marino #define DECL_EFFECTIVE_INIT_PRIORITY(decl)				      \
3009e4b17023SJohn Marino 	((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
3010e4b17023SJohn Marino 	 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
3011e4b17023SJohn Marino 
3012e4b17023SJohn Marino /* Whether a DECL needs a guard to protect it against multiple
3013e4b17023SJohn Marino    initialization.  */
3014e4b17023SJohn Marino 
3015e4b17023SJohn Marino #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl)      \
3016e4b17023SJohn Marino 						    || DECL_ONE_ONLY (decl) \
3017e4b17023SJohn Marino 						    || DECL_WEAK (decl)))
3018e4b17023SJohn Marino 
3019e4b17023SJohn Marino /* Called from one_static_initialization_or_destruction(),
3020e4b17023SJohn Marino    via walk_tree.
3021e4b17023SJohn Marino    Walks the initializer list of a global variable and looks for
3022e4b17023SJohn Marino    temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
3023e4b17023SJohn Marino    and that have their DECL_CONTEXT() == NULL.
3024e4b17023SJohn Marino    For each such temporary variable, set their DECL_CONTEXT() to
3025e4b17023SJohn Marino    the current function. This is necessary because otherwise
3026e4b17023SJohn Marino    some optimizers (enabled by -O2 -fprofile-arcs) might crash
3027e4b17023SJohn Marino    when trying to refer to a temporary variable that does not have
3028e4b17023SJohn Marino    it's DECL_CONTECT() properly set.  */
3029e4b17023SJohn Marino static tree
fix_temporary_vars_context_r(tree * node,int * unused ATTRIBUTE_UNUSED,void * unused1 ATTRIBUTE_UNUSED)3030e4b17023SJohn Marino fix_temporary_vars_context_r (tree *node,
3031e4b17023SJohn Marino 			      int  *unused ATTRIBUTE_UNUSED,
3032e4b17023SJohn Marino 			      void *unused1 ATTRIBUTE_UNUSED)
3033e4b17023SJohn Marino {
3034e4b17023SJohn Marino   gcc_assert (current_function_decl);
3035e4b17023SJohn Marino 
3036e4b17023SJohn Marino   if (TREE_CODE (*node) == BIND_EXPR)
3037e4b17023SJohn Marino     {
3038e4b17023SJohn Marino       tree var;
3039e4b17023SJohn Marino 
3040e4b17023SJohn Marino       for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
3041e4b17023SJohn Marino 	if (TREE_CODE (var) == VAR_DECL
3042e4b17023SJohn Marino 	  && !DECL_NAME (var)
3043e4b17023SJohn Marino 	  && DECL_ARTIFICIAL (var)
3044e4b17023SJohn Marino 	  && !DECL_CONTEXT (var))
3045e4b17023SJohn Marino 	  DECL_CONTEXT (var) = current_function_decl;
3046e4b17023SJohn Marino     }
3047e4b17023SJohn Marino 
3048e4b17023SJohn Marino   return NULL_TREE;
3049e4b17023SJohn Marino }
3050e4b17023SJohn Marino 
3051e4b17023SJohn Marino /* Set up to handle the initialization or destruction of DECL.  If
3052e4b17023SJohn Marino    INITP is nonzero, we are initializing the variable.  Otherwise, we
3053e4b17023SJohn Marino    are destroying it.  */
3054e4b17023SJohn Marino 
3055e4b17023SJohn Marino static void
one_static_initialization_or_destruction(tree decl,tree init,bool initp)3056e4b17023SJohn Marino one_static_initialization_or_destruction (tree decl, tree init, bool initp)
3057e4b17023SJohn Marino {
3058e4b17023SJohn Marino   tree guard_if_stmt = NULL_TREE;
3059e4b17023SJohn Marino   tree guard;
3060e4b17023SJohn Marino 
3061e4b17023SJohn Marino   /* If we are supposed to destruct and there's a trivial destructor,
3062e4b17023SJohn Marino      nothing has to be done.  */
3063e4b17023SJohn Marino   if (!initp
3064e4b17023SJohn Marino       && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3065e4b17023SJohn Marino     return;
3066e4b17023SJohn Marino 
3067e4b17023SJohn Marino   /* Trick the compiler into thinking we are at the file and line
3068e4b17023SJohn Marino      where DECL was declared so that error-messages make sense, and so
3069e4b17023SJohn Marino      that the debugger will show somewhat sensible file and line
3070e4b17023SJohn Marino      information.  */
3071e4b17023SJohn Marino   input_location = DECL_SOURCE_LOCATION (decl);
3072e4b17023SJohn Marino 
3073e4b17023SJohn Marino   /* Make sure temporary variables in the initialiser all have
3074e4b17023SJohn Marino      their DECL_CONTEXT() set to a value different from NULL_TREE.
3075e4b17023SJohn Marino      This can happen when global variables initialisers are built.
3076e4b17023SJohn Marino      In that case, the DECL_CONTEXT() of the global variables _AND_ of all
3077e4b17023SJohn Marino      the temporary variables that might have been generated in the
3078e4b17023SJohn Marino      accompagning initialisers is NULL_TREE, meaning the variables have been
3079e4b17023SJohn Marino      declared in the global namespace.
3080e4b17023SJohn Marino      What we want to do here is to fix that and make sure the DECL_CONTEXT()
3081e4b17023SJohn Marino      of the temporaries are set to the current function decl.  */
3082e4b17023SJohn Marino   cp_walk_tree_without_duplicates (&init,
3083e4b17023SJohn Marino 				   fix_temporary_vars_context_r,
3084e4b17023SJohn Marino 				   NULL);
3085e4b17023SJohn Marino 
3086e4b17023SJohn Marino   /* Because of:
3087e4b17023SJohn Marino 
3088e4b17023SJohn Marino        [class.access.spec]
3089e4b17023SJohn Marino 
3090e4b17023SJohn Marino        Access control for implicit calls to the constructors,
3091e4b17023SJohn Marino        the conversion functions, or the destructor called to
3092e4b17023SJohn Marino        create and destroy a static data member is performed as
3093e4b17023SJohn Marino        if these calls appeared in the scope of the member's
3094e4b17023SJohn Marino        class.
3095e4b17023SJohn Marino 
3096e4b17023SJohn Marino      we pretend we are in a static member function of the class of
3097e4b17023SJohn Marino      which the DECL is a member.  */
3098e4b17023SJohn Marino   if (member_p (decl))
3099e4b17023SJohn Marino     {
3100e4b17023SJohn Marino       DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3101e4b17023SJohn Marino       DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3102e4b17023SJohn Marino     }
3103e4b17023SJohn Marino 
3104e4b17023SJohn Marino   /* Assume we don't need a guard.  */
3105e4b17023SJohn Marino   guard = NULL_TREE;
3106e4b17023SJohn Marino   /* We need a guard if this is an object with external linkage that
3107e4b17023SJohn Marino      might be initialized in more than one place.  (For example, a
3108e4b17023SJohn Marino      static data member of a template, when the data member requires
3109e4b17023SJohn Marino      construction.)  */
3110e4b17023SJohn Marino   if (NEEDS_GUARD_P (decl))
3111e4b17023SJohn Marino     {
3112e4b17023SJohn Marino       tree guard_cond;
3113e4b17023SJohn Marino 
3114e4b17023SJohn Marino       guard = get_guard (decl);
3115e4b17023SJohn Marino 
3116e4b17023SJohn Marino       /* When using __cxa_atexit, we just check the GUARD as we would
3117e4b17023SJohn Marino 	 for a local static.  */
3118e4b17023SJohn Marino       if (flag_use_cxa_atexit)
3119e4b17023SJohn Marino 	{
3120e4b17023SJohn Marino 	  /* When using __cxa_atexit, we never try to destroy
3121e4b17023SJohn Marino 	     anything from a static destructor.  */
3122e4b17023SJohn Marino 	  gcc_assert (initp);
3123e4b17023SJohn Marino 	  guard_cond = get_guard_cond (guard);
3124e4b17023SJohn Marino 	}
3125e4b17023SJohn Marino       /* If we don't have __cxa_atexit, then we will be running
3126e4b17023SJohn Marino 	 destructors from .fini sections, or their equivalents.  So,
3127e4b17023SJohn Marino 	 we need to know how many times we've tried to initialize this
3128e4b17023SJohn Marino 	 object.  We do initializations only if the GUARD is zero,
3129e4b17023SJohn Marino 	 i.e., if we are the first to initialize the variable.  We do
3130e4b17023SJohn Marino 	 destructions only if the GUARD is one, i.e., if we are the
3131e4b17023SJohn Marino 	 last to destroy the variable.  */
3132e4b17023SJohn Marino       else if (initp)
3133e4b17023SJohn Marino 	guard_cond
3134e4b17023SJohn Marino 	  = cp_build_binary_op (input_location,
3135e4b17023SJohn Marino 				EQ_EXPR,
3136e4b17023SJohn Marino 				cp_build_unary_op (PREINCREMENT_EXPR,
3137e4b17023SJohn Marino 						   guard,
3138e4b17023SJohn Marino 						   /*noconvert=*/1,
3139e4b17023SJohn Marino 						   tf_warning_or_error),
3140e4b17023SJohn Marino 				integer_one_node,
3141e4b17023SJohn Marino 				tf_warning_or_error);
3142e4b17023SJohn Marino       else
3143e4b17023SJohn Marino 	guard_cond
3144e4b17023SJohn Marino 	  = cp_build_binary_op (input_location,
3145e4b17023SJohn Marino 				EQ_EXPR,
3146e4b17023SJohn Marino 				cp_build_unary_op (PREDECREMENT_EXPR,
3147e4b17023SJohn Marino 						   guard,
3148e4b17023SJohn Marino 						   /*noconvert=*/1,
3149e4b17023SJohn Marino 						   tf_warning_or_error),
3150e4b17023SJohn Marino 				integer_zero_node,
3151e4b17023SJohn Marino 				tf_warning_or_error);
3152e4b17023SJohn Marino 
3153e4b17023SJohn Marino       guard_if_stmt = begin_if_stmt ();
3154e4b17023SJohn Marino       finish_if_stmt_cond (guard_cond, guard_if_stmt);
3155e4b17023SJohn Marino     }
3156e4b17023SJohn Marino 
3157e4b17023SJohn Marino 
3158e4b17023SJohn Marino   /* If we're using __cxa_atexit, we have not already set the GUARD,
3159e4b17023SJohn Marino      so we must do so now.  */
3160e4b17023SJohn Marino   if (guard && initp && flag_use_cxa_atexit)
3161e4b17023SJohn Marino     finish_expr_stmt (set_guard (guard));
3162e4b17023SJohn Marino 
3163e4b17023SJohn Marino   /* Perform the initialization or destruction.  */
3164e4b17023SJohn Marino   if (initp)
3165e4b17023SJohn Marino     {
3166e4b17023SJohn Marino       if (init)
3167e4b17023SJohn Marino 	finish_expr_stmt (init);
3168e4b17023SJohn Marino 
3169e4b17023SJohn Marino       /* If we're using __cxa_atexit, register a function that calls the
3170e4b17023SJohn Marino 	 destructor for the object.  */
3171e4b17023SJohn Marino       if (flag_use_cxa_atexit)
3172e4b17023SJohn Marino 	finish_expr_stmt (register_dtor_fn (decl));
3173e4b17023SJohn Marino     }
3174e4b17023SJohn Marino   else
3175e4b17023SJohn Marino     finish_expr_stmt (build_cleanup (decl));
3176e4b17023SJohn Marino 
3177e4b17023SJohn Marino   /* Finish the guard if-stmt, if necessary.  */
3178e4b17023SJohn Marino   if (guard)
3179e4b17023SJohn Marino     {
3180e4b17023SJohn Marino       finish_then_clause (guard_if_stmt);
3181e4b17023SJohn Marino       finish_if_stmt (guard_if_stmt);
3182e4b17023SJohn Marino     }
3183e4b17023SJohn Marino 
3184e4b17023SJohn Marino   /* Now that we're done with DECL we don't need to pretend to be a
3185e4b17023SJohn Marino      member of its class any longer.  */
3186e4b17023SJohn Marino   DECL_CONTEXT (current_function_decl) = NULL_TREE;
3187e4b17023SJohn Marino   DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3188e4b17023SJohn Marino }
3189e4b17023SJohn Marino 
3190e4b17023SJohn Marino /* Generate code to do the initialization or destruction of the decls in VARS,
3191e4b17023SJohn Marino    a TREE_LIST of VAR_DECL with static storage duration.
3192e4b17023SJohn Marino    Whether initialization or destruction is performed is specified by INITP.  */
3193e4b17023SJohn Marino 
3194e4b17023SJohn Marino static void
do_static_initialization_or_destruction(tree vars,bool initp)3195e4b17023SJohn Marino do_static_initialization_or_destruction (tree vars, bool initp)
3196e4b17023SJohn Marino {
3197e4b17023SJohn Marino   tree node, init_if_stmt, cond;
3198e4b17023SJohn Marino 
3199e4b17023SJohn Marino   /* Build the outer if-stmt to check for initialization or destruction.  */
3200e4b17023SJohn Marino   init_if_stmt = begin_if_stmt ();
3201e4b17023SJohn Marino   cond = initp ? integer_one_node : integer_zero_node;
3202e4b17023SJohn Marino   cond = cp_build_binary_op (input_location,
3203e4b17023SJohn Marino 			     EQ_EXPR,
3204e4b17023SJohn Marino 			     initialize_p_decl,
3205e4b17023SJohn Marino 			     cond,
3206e4b17023SJohn Marino 			     tf_warning_or_error);
3207e4b17023SJohn Marino   finish_if_stmt_cond (cond, init_if_stmt);
3208e4b17023SJohn Marino 
3209e4b17023SJohn Marino   node = vars;
3210e4b17023SJohn Marino   do {
3211e4b17023SJohn Marino     tree decl = TREE_VALUE (node);
3212e4b17023SJohn Marino     tree priority_if_stmt;
3213e4b17023SJohn Marino     int priority;
3214e4b17023SJohn Marino     priority_info pi;
3215e4b17023SJohn Marino 
3216e4b17023SJohn Marino     /* If we don't need a destructor, there's nothing to do.  Avoid
3217e4b17023SJohn Marino        creating a possibly empty if-stmt.  */
3218e4b17023SJohn Marino     if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3219e4b17023SJohn Marino       {
3220e4b17023SJohn Marino 	node = TREE_CHAIN (node);
3221e4b17023SJohn Marino 	continue;
3222e4b17023SJohn Marino       }
3223e4b17023SJohn Marino 
3224e4b17023SJohn Marino     /* Remember that we had an initialization or finalization at this
3225e4b17023SJohn Marino        priority.  */
3226e4b17023SJohn Marino     priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
3227e4b17023SJohn Marino     pi = get_priority_info (priority);
3228e4b17023SJohn Marino     if (initp)
3229e4b17023SJohn Marino       pi->initializations_p = 1;
3230e4b17023SJohn Marino     else
3231e4b17023SJohn Marino       pi->destructions_p = 1;
3232e4b17023SJohn Marino 
3233e4b17023SJohn Marino     /* Conditionalize this initialization on being in the right priority
3234e4b17023SJohn Marino        and being initializing/finalizing appropriately.  */
3235e4b17023SJohn Marino     priority_if_stmt = begin_if_stmt ();
3236e4b17023SJohn Marino     cond = cp_build_binary_op (input_location,
3237e4b17023SJohn Marino 			       EQ_EXPR,
3238e4b17023SJohn Marino 			       priority_decl,
3239e4b17023SJohn Marino 			       build_int_cst (NULL_TREE, priority),
3240e4b17023SJohn Marino 			       tf_warning_or_error);
3241e4b17023SJohn Marino     finish_if_stmt_cond (cond, priority_if_stmt);
3242e4b17023SJohn Marino 
3243e4b17023SJohn Marino     /* Process initializers with same priority.  */
3244e4b17023SJohn Marino     for (; node
3245e4b17023SJohn Marino 	   && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
3246e4b17023SJohn Marino 	 node = TREE_CHAIN (node))
3247e4b17023SJohn Marino       /* Do one initialization or destruction.  */
3248e4b17023SJohn Marino       one_static_initialization_or_destruction (TREE_VALUE (node),
3249e4b17023SJohn Marino 						TREE_PURPOSE (node), initp);
3250e4b17023SJohn Marino 
3251e4b17023SJohn Marino     /* Finish up the priority if-stmt body.  */
3252e4b17023SJohn Marino     finish_then_clause (priority_if_stmt);
3253e4b17023SJohn Marino     finish_if_stmt (priority_if_stmt);
3254e4b17023SJohn Marino 
3255e4b17023SJohn Marino   } while (node);
3256e4b17023SJohn Marino 
3257e4b17023SJohn Marino   /* Finish up the init/destruct if-stmt body.  */
3258e4b17023SJohn Marino   finish_then_clause (init_if_stmt);
3259e4b17023SJohn Marino   finish_if_stmt (init_if_stmt);
3260e4b17023SJohn Marino }
3261e4b17023SJohn Marino 
3262e4b17023SJohn Marino /* VARS is a list of variables with static storage duration which may
3263e4b17023SJohn Marino    need initialization and/or finalization.  Remove those variables
3264e4b17023SJohn Marino    that don't really need to be initialized or finalized, and return
3265e4b17023SJohn Marino    the resulting list.  The order in which the variables appear in
3266e4b17023SJohn Marino    VARS is in reverse order of the order in which they should actually
3267e4b17023SJohn Marino    be initialized.  The list we return is in the unreversed order;
3268e4b17023SJohn Marino    i.e., the first variable should be initialized first.  */
3269e4b17023SJohn Marino 
3270e4b17023SJohn Marino static tree
prune_vars_needing_no_initialization(tree * vars)3271e4b17023SJohn Marino prune_vars_needing_no_initialization (tree *vars)
3272e4b17023SJohn Marino {
3273e4b17023SJohn Marino   tree *var = vars;
3274e4b17023SJohn Marino   tree result = NULL_TREE;
3275e4b17023SJohn Marino 
3276e4b17023SJohn Marino   while (*var)
3277e4b17023SJohn Marino     {
3278e4b17023SJohn Marino       tree t = *var;
3279e4b17023SJohn Marino       tree decl = TREE_VALUE (t);
3280e4b17023SJohn Marino       tree init = TREE_PURPOSE (t);
3281e4b17023SJohn Marino 
3282e4b17023SJohn Marino       /* Deal gracefully with error.  */
3283e4b17023SJohn Marino       if (decl == error_mark_node)
3284e4b17023SJohn Marino 	{
3285e4b17023SJohn Marino 	  var = &TREE_CHAIN (t);
3286e4b17023SJohn Marino 	  continue;
3287e4b17023SJohn Marino 	}
3288e4b17023SJohn Marino 
3289e4b17023SJohn Marino       /* The only things that can be initialized are variables.  */
3290e4b17023SJohn Marino       gcc_assert (TREE_CODE (decl) == VAR_DECL);
3291e4b17023SJohn Marino 
3292e4b17023SJohn Marino       /* If this object is not defined, we don't need to do anything
3293e4b17023SJohn Marino 	 here.  */
3294e4b17023SJohn Marino       if (DECL_EXTERNAL (decl))
3295e4b17023SJohn Marino 	{
3296e4b17023SJohn Marino 	  var = &TREE_CHAIN (t);
3297e4b17023SJohn Marino 	  continue;
3298e4b17023SJohn Marino 	}
3299e4b17023SJohn Marino 
3300e4b17023SJohn Marino       /* Also, if the initializer already contains errors, we can bail
3301e4b17023SJohn Marino 	 out now.  */
3302e4b17023SJohn Marino       if (init && TREE_CODE (init) == TREE_LIST
3303e4b17023SJohn Marino 	  && value_member (error_mark_node, init))
3304e4b17023SJohn Marino 	{
3305e4b17023SJohn Marino 	  var = &TREE_CHAIN (t);
3306e4b17023SJohn Marino 	  continue;
3307e4b17023SJohn Marino 	}
3308e4b17023SJohn Marino 
3309e4b17023SJohn Marino       /* This variable is going to need initialization and/or
3310e4b17023SJohn Marino 	 finalization, so we add it to the list.  */
3311e4b17023SJohn Marino       *var = TREE_CHAIN (t);
3312e4b17023SJohn Marino       TREE_CHAIN (t) = result;
3313e4b17023SJohn Marino       result = t;
3314e4b17023SJohn Marino     }
3315e4b17023SJohn Marino 
3316e4b17023SJohn Marino   return result;
3317e4b17023SJohn Marino }
3318e4b17023SJohn Marino 
3319e4b17023SJohn Marino /* Make sure we have told the back end about all the variables in
3320e4b17023SJohn Marino    VARS.  */
3321e4b17023SJohn Marino 
3322e4b17023SJohn Marino static void
write_out_vars(tree vars)3323e4b17023SJohn Marino write_out_vars (tree vars)
3324e4b17023SJohn Marino {
3325e4b17023SJohn Marino   tree v;
3326e4b17023SJohn Marino 
3327e4b17023SJohn Marino   for (v = vars; v; v = TREE_CHAIN (v))
3328e4b17023SJohn Marino     {
3329e4b17023SJohn Marino       tree var = TREE_VALUE (v);
3330e4b17023SJohn Marino       if (!var_finalized_p (var))
3331e4b17023SJohn Marino 	{
3332e4b17023SJohn Marino 	  import_export_decl (var);
3333e4b17023SJohn Marino 	  rest_of_decl_compilation (var, 1, 1);
3334e4b17023SJohn Marino 	}
3335e4b17023SJohn Marino     }
3336e4b17023SJohn Marino }
3337e4b17023SJohn Marino 
3338e4b17023SJohn Marino /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3339e4b17023SJohn Marino    (otherwise) that will initialize all global objects with static
3340e4b17023SJohn Marino    storage duration having the indicated PRIORITY.  */
3341e4b17023SJohn Marino 
3342e4b17023SJohn Marino static void
generate_ctor_or_dtor_function(bool constructor_p,int priority,location_t * locus)3343e4b17023SJohn Marino generate_ctor_or_dtor_function (bool constructor_p, int priority,
3344e4b17023SJohn Marino 				location_t *locus)
3345e4b17023SJohn Marino {
3346e4b17023SJohn Marino   char function_key;
3347e4b17023SJohn Marino   tree fndecl;
3348e4b17023SJohn Marino   tree body;
3349e4b17023SJohn Marino   size_t i;
3350e4b17023SJohn Marino 
3351e4b17023SJohn Marino   input_location = *locus;
3352e4b17023SJohn Marino   /* ??? */
3353e4b17023SJohn Marino   /* Was: locus->line++; */
3354e4b17023SJohn Marino 
3355e4b17023SJohn Marino   /* We use `I' to indicate initialization and `D' to indicate
3356e4b17023SJohn Marino      destruction.  */
3357e4b17023SJohn Marino   function_key = constructor_p ? 'I' : 'D';
3358e4b17023SJohn Marino 
3359e4b17023SJohn Marino   /* We emit the function lazily, to avoid generating empty
3360e4b17023SJohn Marino      global constructors and destructors.  */
3361e4b17023SJohn Marino   body = NULL_TREE;
3362e4b17023SJohn Marino 
3363e4b17023SJohn Marino   /* For Objective-C++, we may need to initialize metadata found in this module.
3364e4b17023SJohn Marino      This must be done _before_ any other static initializations.  */
3365e4b17023SJohn Marino   if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
3366e4b17023SJohn Marino       && constructor_p && objc_static_init_needed_p ())
3367e4b17023SJohn Marino     {
3368e4b17023SJohn Marino       body = start_objects (function_key, priority);
3369e4b17023SJohn Marino       objc_generate_static_init_call (NULL_TREE);
3370e4b17023SJohn Marino     }
3371e4b17023SJohn Marino 
3372e4b17023SJohn Marino   /* Call the static storage duration function with appropriate
3373e4b17023SJohn Marino      arguments.  */
3374e4b17023SJohn Marino   FOR_EACH_VEC_ELT (tree, ssdf_decls, i, fndecl)
3375e4b17023SJohn Marino     {
3376e4b17023SJohn Marino       /* Calls to pure or const functions will expand to nothing.  */
3377e4b17023SJohn Marino       if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
3378e4b17023SJohn Marino 	{
3379e4b17023SJohn Marino 	  tree call;
3380e4b17023SJohn Marino 
3381e4b17023SJohn Marino 	  if (! body)
3382e4b17023SJohn Marino 	    body = start_objects (function_key, priority);
3383e4b17023SJohn Marino 
3384e4b17023SJohn Marino 	  call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
3385e4b17023SJohn Marino 					      build_int_cst (NULL_TREE,
3386e4b17023SJohn Marino 							     constructor_p),
3387e4b17023SJohn Marino 					      build_int_cst (NULL_TREE,
3388e4b17023SJohn Marino 							     priority),
3389e4b17023SJohn Marino 					      NULL_TREE);
3390e4b17023SJohn Marino 	  finish_expr_stmt (call);
3391e4b17023SJohn Marino 	}
3392e4b17023SJohn Marino     }
3393e4b17023SJohn Marino 
3394e4b17023SJohn Marino   /* Close out the function.  */
3395e4b17023SJohn Marino   if (body)
3396e4b17023SJohn Marino     finish_objects (function_key, priority, body);
3397e4b17023SJohn Marino }
3398e4b17023SJohn Marino 
3399e4b17023SJohn Marino /* Generate constructor and destructor functions for the priority
3400e4b17023SJohn Marino    indicated by N.  */
3401e4b17023SJohn Marino 
3402e4b17023SJohn Marino static int
generate_ctor_and_dtor_functions_for_priority(splay_tree_node n,void * data)3403e4b17023SJohn Marino generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
3404e4b17023SJohn Marino {
3405e4b17023SJohn Marino   location_t *locus = (location_t *) data;
3406e4b17023SJohn Marino   int priority = (int) n->key;
3407e4b17023SJohn Marino   priority_info pi = (priority_info) n->value;
3408e4b17023SJohn Marino 
3409e4b17023SJohn Marino   /* Generate the functions themselves, but only if they are really
3410e4b17023SJohn Marino      needed.  */
3411e4b17023SJohn Marino   if (pi->initializations_p)
3412e4b17023SJohn Marino     generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
3413e4b17023SJohn Marino   if (pi->destructions_p)
3414e4b17023SJohn Marino     generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
3415e4b17023SJohn Marino 
3416e4b17023SJohn Marino   /* Keep iterating.  */
3417e4b17023SJohn Marino   return 0;
3418e4b17023SJohn Marino }
3419e4b17023SJohn Marino 
3420e4b17023SJohn Marino /* Called via LANGHOOK_CALLGRAPH_ANALYZE_EXPR.  It is supposed to mark
3421e4b17023SJohn Marino    decls referenced from front-end specific constructs; it will be called
3422e4b17023SJohn Marino    only for language-specific tree nodes.
3423e4b17023SJohn Marino 
3424e4b17023SJohn Marino    Here we must deal with member pointers.  */
3425e4b17023SJohn Marino 
3426e4b17023SJohn Marino tree
cxx_callgraph_analyze_expr(tree * tp,int * walk_subtrees ATTRIBUTE_UNUSED)3427e4b17023SJohn Marino cxx_callgraph_analyze_expr (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED)
3428e4b17023SJohn Marino {
3429e4b17023SJohn Marino   tree t = *tp;
3430e4b17023SJohn Marino 
3431e4b17023SJohn Marino   switch (TREE_CODE (t))
3432e4b17023SJohn Marino     {
3433e4b17023SJohn Marino     case PTRMEM_CST:
3434e4b17023SJohn Marino       if (TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
3435e4b17023SJohn Marino 	cgraph_mark_address_taken_node (
3436e4b17023SJohn Marino 			      cgraph_get_create_node (PTRMEM_CST_MEMBER (t)));
3437e4b17023SJohn Marino       break;
3438e4b17023SJohn Marino     case BASELINK:
3439e4b17023SJohn Marino       if (TREE_CODE (BASELINK_FUNCTIONS (t)) == FUNCTION_DECL)
3440e4b17023SJohn Marino 	cgraph_mark_address_taken_node (
3441e4b17023SJohn Marino 			      cgraph_get_create_node (BASELINK_FUNCTIONS (t)));
3442e4b17023SJohn Marino       break;
3443e4b17023SJohn Marino     case VAR_DECL:
3444e4b17023SJohn Marino       if (DECL_CONTEXT (t)
3445e4b17023SJohn Marino 	  && flag_use_repository
3446e4b17023SJohn Marino 	  && TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)
3447e4b17023SJohn Marino 	/* If we need a static variable in a function, then we
3448e4b17023SJohn Marino 	   need the containing function.  */
3449e4b17023SJohn Marino 	mark_decl_referenced (DECL_CONTEXT (t));
3450e4b17023SJohn Marino       break;
3451e4b17023SJohn Marino     default:
3452e4b17023SJohn Marino       break;
3453e4b17023SJohn Marino     }
3454e4b17023SJohn Marino 
3455e4b17023SJohn Marino   return NULL;
3456e4b17023SJohn Marino }
3457e4b17023SJohn Marino 
3458e4b17023SJohn Marino /* Java requires that we be able to reference a local address for a
3459e4b17023SJohn Marino    method, and not be confused by PLT entries.  If hidden aliases are
3460e4b17023SJohn Marino    supported, collect and return all the functions for which we should
3461e4b17023SJohn Marino    emit a hidden alias.  */
3462e4b17023SJohn Marino 
3463e4b17023SJohn Marino static struct pointer_set_t *
collect_candidates_for_java_method_aliases(void)3464e4b17023SJohn Marino collect_candidates_for_java_method_aliases (void)
3465e4b17023SJohn Marino {
3466e4b17023SJohn Marino   struct cgraph_node *node;
3467e4b17023SJohn Marino   struct pointer_set_t *candidates = NULL;
3468e4b17023SJohn Marino 
3469e4b17023SJohn Marino #ifndef HAVE_GAS_HIDDEN
3470e4b17023SJohn Marino   return candidates;
3471e4b17023SJohn Marino #endif
3472e4b17023SJohn Marino 
3473e4b17023SJohn Marino   for (node = cgraph_nodes; node ; node = node->next)
3474e4b17023SJohn Marino     {
3475e4b17023SJohn Marino       tree fndecl = node->decl;
3476e4b17023SJohn Marino 
3477e4b17023SJohn Marino       if (DECL_CONTEXT (fndecl)
3478e4b17023SJohn Marino 	  && TYPE_P (DECL_CONTEXT (fndecl))
3479e4b17023SJohn Marino 	  && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
3480e4b17023SJohn Marino 	  && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
3481e4b17023SJohn Marino 	{
3482e4b17023SJohn Marino 	  if (candidates == NULL)
3483e4b17023SJohn Marino 	    candidates = pointer_set_create ();
3484e4b17023SJohn Marino 	  pointer_set_insert (candidates, fndecl);
3485e4b17023SJohn Marino 	}
3486e4b17023SJohn Marino     }
3487e4b17023SJohn Marino 
3488e4b17023SJohn Marino   return candidates;
3489e4b17023SJohn Marino }
3490e4b17023SJohn Marino 
3491e4b17023SJohn Marino 
3492e4b17023SJohn Marino /* Java requires that we be able to reference a local address for a
3493e4b17023SJohn Marino    method, and not be confused by PLT entries.  If hidden aliases are
3494e4b17023SJohn Marino    supported, emit one for each java function that we've emitted.
3495e4b17023SJohn Marino    CANDIDATES is the set of FUNCTION_DECLs that were gathered
3496e4b17023SJohn Marino    by collect_candidates_for_java_method_aliases.  */
3497e4b17023SJohn Marino 
3498e4b17023SJohn Marino static void
build_java_method_aliases(struct pointer_set_t * candidates)3499e4b17023SJohn Marino build_java_method_aliases (struct pointer_set_t *candidates)
3500e4b17023SJohn Marino {
3501e4b17023SJohn Marino   struct cgraph_node *node;
3502e4b17023SJohn Marino 
3503e4b17023SJohn Marino #ifndef HAVE_GAS_HIDDEN
3504e4b17023SJohn Marino   return;
3505e4b17023SJohn Marino #endif
3506e4b17023SJohn Marino 
3507e4b17023SJohn Marino   for (node = cgraph_nodes; node ; node = node->next)
3508e4b17023SJohn Marino     {
3509e4b17023SJohn Marino       tree fndecl = node->decl;
3510e4b17023SJohn Marino 
3511e4b17023SJohn Marino       if (TREE_ASM_WRITTEN (fndecl)
3512e4b17023SJohn Marino 	  && pointer_set_contains (candidates, fndecl))
3513e4b17023SJohn Marino 	{
3514e4b17023SJohn Marino 	  /* Mangle the name in a predictable way; we need to reference
3515e4b17023SJohn Marino 	     this from a java compiled object file.  */
3516e4b17023SJohn Marino 	  tree oid, nid, alias;
3517e4b17023SJohn Marino 	  const char *oname;
3518e4b17023SJohn Marino 	  char *nname;
3519e4b17023SJohn Marino 
3520e4b17023SJohn Marino 	  oid = DECL_ASSEMBLER_NAME (fndecl);
3521e4b17023SJohn Marino 	  oname = IDENTIFIER_POINTER (oid);
3522e4b17023SJohn Marino 	  gcc_assert (oname[0] == '_' && oname[1] == 'Z');
3523e4b17023SJohn Marino 	  nname = ACONCAT (("_ZGA", oname+2, NULL));
3524e4b17023SJohn Marino 	  nid = get_identifier (nname);
3525e4b17023SJohn Marino 
3526e4b17023SJohn Marino 	  alias = make_alias_for (fndecl, nid);
3527e4b17023SJohn Marino 	  TREE_PUBLIC (alias) = 1;
3528e4b17023SJohn Marino 	  DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
3529e4b17023SJohn Marino 
3530e4b17023SJohn Marino 	  assemble_alias (alias, oid);
3531e4b17023SJohn Marino 	}
3532e4b17023SJohn Marino     }
3533e4b17023SJohn Marino }
3534e4b17023SJohn Marino 
3535e4b17023SJohn Marino /* Return C++ property of T, based on given operation OP.  */
3536e4b17023SJohn Marino 
3537e4b17023SJohn Marino static int
cpp_check(tree t,cpp_operation op)3538e4b17023SJohn Marino cpp_check (tree t, cpp_operation op)
3539e4b17023SJohn Marino {
3540e4b17023SJohn Marino   switch (op)
3541e4b17023SJohn Marino     {
3542e4b17023SJohn Marino       case IS_ABSTRACT:
3543e4b17023SJohn Marino 	return DECL_PURE_VIRTUAL_P (t);
3544e4b17023SJohn Marino       case IS_CONSTRUCTOR:
3545e4b17023SJohn Marino 	return DECL_CONSTRUCTOR_P (t);
3546e4b17023SJohn Marino       case IS_DESTRUCTOR:
3547e4b17023SJohn Marino 	return DECL_DESTRUCTOR_P (t);
3548e4b17023SJohn Marino       case IS_COPY_CONSTRUCTOR:
3549e4b17023SJohn Marino 	return DECL_COPY_CONSTRUCTOR_P (t);
3550e4b17023SJohn Marino       case IS_TEMPLATE:
3551e4b17023SJohn Marino 	return TREE_CODE (t) == TEMPLATE_DECL;
3552e4b17023SJohn Marino       default:
3553e4b17023SJohn Marino         return 0;
3554e4b17023SJohn Marino     }
3555e4b17023SJohn Marino }
3556e4b17023SJohn Marino 
3557e4b17023SJohn Marino /* Collect source file references recursively, starting from NAMESPC.  */
3558e4b17023SJohn Marino 
3559e4b17023SJohn Marino static void
collect_source_refs(tree namespc)3560e4b17023SJohn Marino collect_source_refs (tree namespc)
3561e4b17023SJohn Marino {
3562e4b17023SJohn Marino   tree t;
3563e4b17023SJohn Marino 
3564e4b17023SJohn Marino   if (!namespc)
3565e4b17023SJohn Marino     return;
3566e4b17023SJohn Marino 
3567e4b17023SJohn Marino   /* Iterate over names in this name space.  */
3568e4b17023SJohn Marino   for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
3569e4b17023SJohn Marino     if (!DECL_IS_BUILTIN (t) )
3570e4b17023SJohn Marino       collect_source_ref (DECL_SOURCE_FILE (t));
3571e4b17023SJohn Marino 
3572e4b17023SJohn Marino   /* Dump siblings, if any */
3573e4b17023SJohn Marino   collect_source_refs (TREE_CHAIN (namespc));
3574e4b17023SJohn Marino 
3575e4b17023SJohn Marino   /* Dump children, if any */
3576e4b17023SJohn Marino   collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
3577e4b17023SJohn Marino }
3578e4b17023SJohn Marino 
3579e4b17023SJohn Marino /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
3580e4b17023SJohn Marino    starting from NAMESPC.  */
3581e4b17023SJohn Marino 
3582e4b17023SJohn Marino static void
collect_ada_namespace(tree namespc,const char * source_file)3583e4b17023SJohn Marino collect_ada_namespace (tree namespc, const char *source_file)
3584e4b17023SJohn Marino {
3585e4b17023SJohn Marino   if (!namespc)
3586e4b17023SJohn Marino     return;
3587e4b17023SJohn Marino 
3588e4b17023SJohn Marino   /* Collect decls from this namespace */
3589e4b17023SJohn Marino   collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
3590e4b17023SJohn Marino 
3591e4b17023SJohn Marino   /* Collect siblings, if any */
3592e4b17023SJohn Marino   collect_ada_namespace (TREE_CHAIN (namespc), source_file);
3593e4b17023SJohn Marino 
3594e4b17023SJohn Marino   /* Collect children, if any */
3595e4b17023SJohn Marino   collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
3596e4b17023SJohn Marino }
3597e4b17023SJohn Marino 
3598e4b17023SJohn Marino /* Returns true iff there is a definition available for variable or
3599e4b17023SJohn Marino    function DECL.  */
3600e4b17023SJohn Marino 
3601e4b17023SJohn Marino static bool
decl_defined_p(tree decl)3602e4b17023SJohn Marino decl_defined_p (tree decl)
3603e4b17023SJohn Marino {
3604e4b17023SJohn Marino   if (TREE_CODE (decl) == FUNCTION_DECL)
3605e4b17023SJohn Marino     return (DECL_INITIAL (decl) != NULL_TREE);
3606e4b17023SJohn Marino   else
3607e4b17023SJohn Marino     {
3608e4b17023SJohn Marino       gcc_assert (TREE_CODE (decl) == VAR_DECL);
3609e4b17023SJohn Marino       return !DECL_EXTERNAL (decl);
3610e4b17023SJohn Marino     }
3611e4b17023SJohn Marino }
3612e4b17023SJohn Marino 
3613e4b17023SJohn Marino /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
3614e4b17023SJohn Marino 
3615e4b17023SJohn Marino       [expr.const]
3616e4b17023SJohn Marino 
3617e4b17023SJohn Marino       An integral constant-expression can only involve ... const
3618e4b17023SJohn Marino       variables of integral or enumeration types initialized with
3619e4b17023SJohn Marino       constant expressions ...
3620e4b17023SJohn Marino 
3621e4b17023SJohn Marino       C++0x also allows constexpr variables and temporaries initialized
3622e4b17023SJohn Marino       with constant expressions.  We handle the former here, but the latter
3623e4b17023SJohn Marino       are just folded away in cxx_eval_constant_expression.
3624e4b17023SJohn Marino 
3625e4b17023SJohn Marino    The standard does not require that the expression be non-volatile.
3626e4b17023SJohn Marino    G++ implements the proposed correction in DR 457.  */
3627e4b17023SJohn Marino 
3628e4b17023SJohn Marino bool
decl_constant_var_p(tree decl)3629e4b17023SJohn Marino decl_constant_var_p (tree decl)
3630e4b17023SJohn Marino {
3631e4b17023SJohn Marino   if (!decl_maybe_constant_var_p (decl))
3632e4b17023SJohn Marino     return false;
3633e4b17023SJohn Marino 
3634e4b17023SJohn Marino   /* We don't know if a template static data member is initialized with
3635e4b17023SJohn Marino      a constant expression until we instantiate its initializer.  Even
3636e4b17023SJohn Marino      in the case of a constexpr variable, we can't treat it as a
3637e4b17023SJohn Marino      constant until its initializer is complete in case it's used in
3638e4b17023SJohn Marino      its own initializer.  */
3639e4b17023SJohn Marino   mark_used (decl);
3640e4b17023SJohn Marino   return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
3641e4b17023SJohn Marino }
3642e4b17023SJohn Marino 
3643e4b17023SJohn Marino /* Returns true if DECL could be a symbolic constant variable, depending on
3644e4b17023SJohn Marino    its initializer.  */
3645e4b17023SJohn Marino 
3646e4b17023SJohn Marino bool
decl_maybe_constant_var_p(tree decl)3647e4b17023SJohn Marino decl_maybe_constant_var_p (tree decl)
3648e4b17023SJohn Marino {
3649e4b17023SJohn Marino   tree type = TREE_TYPE (decl);
3650e4b17023SJohn Marino   if (TREE_CODE (decl) != VAR_DECL)
3651e4b17023SJohn Marino     return false;
3652e4b17023SJohn Marino   if (DECL_DECLARED_CONSTEXPR_P (decl))
3653e4b17023SJohn Marino     return true;
3654e4b17023SJohn Marino   return (CP_TYPE_CONST_NON_VOLATILE_P (type)
3655e4b17023SJohn Marino 	  && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
3656e4b17023SJohn Marino }
3657e4b17023SJohn Marino 
3658e4b17023SJohn Marino /* Complain that DECL uses a type with no linkage but is never defined.  */
3659e4b17023SJohn Marino 
3660e4b17023SJohn Marino static void
no_linkage_error(tree decl)3661e4b17023SJohn Marino no_linkage_error (tree decl)
3662e4b17023SJohn Marino {
3663e4b17023SJohn Marino   tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
3664e4b17023SJohn Marino   if (TYPE_ANONYMOUS_P (t))
3665e4b17023SJohn Marino     {
3666e4b17023SJohn Marino       permerror (0, "%q+#D, declared using anonymous type, "
3667e4b17023SJohn Marino 		 "is used but never defined", decl);
3668e4b17023SJohn Marino       if (is_typedef_decl (TYPE_NAME (t)))
3669e4b17023SJohn Marino 	permerror (0, "%q+#D does not refer to the unqualified type, "
3670e4b17023SJohn Marino 		   "so it is not used for linkage", TYPE_NAME (t));
3671e4b17023SJohn Marino     }
3672e4b17023SJohn Marino   else
3673e4b17023SJohn Marino     permerror (0, "%q+#D, declared using local type %qT, "
3674e4b17023SJohn Marino 	       "is used but never defined", decl, t);
3675e4b17023SJohn Marino }
3676e4b17023SJohn Marino 
3677e4b17023SJohn Marino /* Collect declarations from all namespaces relevant to SOURCE_FILE.  */
3678e4b17023SJohn Marino 
3679e4b17023SJohn Marino static void
collect_all_refs(const char * source_file)3680e4b17023SJohn Marino collect_all_refs (const char *source_file)
3681e4b17023SJohn Marino {
3682e4b17023SJohn Marino   collect_ada_namespace (global_namespace, source_file);
3683e4b17023SJohn Marino }
3684e4b17023SJohn Marino 
3685e4b17023SJohn Marino /* Clear DECL_EXTERNAL for NODE.  */
3686e4b17023SJohn Marino 
3687e4b17023SJohn Marino static bool
clear_decl_external(struct cgraph_node * node,void * data ATTRIBUTE_UNUSED)3688e4b17023SJohn Marino clear_decl_external (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3689e4b17023SJohn Marino {
3690e4b17023SJohn Marino   DECL_EXTERNAL (node->decl) = 0;
3691e4b17023SJohn Marino   return false;
3692e4b17023SJohn Marino }
3693e4b17023SJohn Marino 
3694e4b17023SJohn Marino /* This routine is called at the end of compilation.
3695e4b17023SJohn Marino    Its job is to create all the code needed to initialize and
3696e4b17023SJohn Marino    destroy the global aggregates.  We do the destruction
3697e4b17023SJohn Marino    first, since that way we only need to reverse the decls once.  */
3698e4b17023SJohn Marino 
3699e4b17023SJohn Marino void
cp_write_global_declarations(void)3700e4b17023SJohn Marino cp_write_global_declarations (void)
3701e4b17023SJohn Marino {
3702e4b17023SJohn Marino   tree vars;
3703e4b17023SJohn Marino   bool reconsider;
3704e4b17023SJohn Marino   size_t i;
3705e4b17023SJohn Marino   location_t locus;
3706e4b17023SJohn Marino   unsigned ssdf_count = 0;
3707e4b17023SJohn Marino   int retries = 0;
3708e4b17023SJohn Marino   tree decl;
3709e4b17023SJohn Marino   struct pointer_set_t *candidates;
3710e4b17023SJohn Marino 
3711e4b17023SJohn Marino   locus = input_location;
3712e4b17023SJohn Marino   at_eof = 1;
3713e4b17023SJohn Marino 
3714e4b17023SJohn Marino   /* Bad parse errors.  Just forget about it.  */
3715e4b17023SJohn Marino   if (! global_bindings_p () || current_class_type
3716e4b17023SJohn Marino       || !VEC_empty (tree,decl_namespace_list))
3717e4b17023SJohn Marino     return;
3718e4b17023SJohn Marino 
3719e4b17023SJohn Marino   if (pch_file)
3720e4b17023SJohn Marino     c_common_write_pch ();
3721e4b17023SJohn Marino 
3722e4b17023SJohn Marino   cgraph_process_same_body_aliases ();
3723e4b17023SJohn Marino 
3724e4b17023SJohn Marino   /* Handle -fdump-ada-spec[-slim] */
3725e4b17023SJohn Marino   if (dump_enabled_p (TDI_ada))
3726e4b17023SJohn Marino     {
3727e4b17023SJohn Marino       if (get_dump_file_info (TDI_ada)->flags & TDF_SLIM)
3728e4b17023SJohn Marino 	collect_source_ref (main_input_filename);
3729e4b17023SJohn Marino       else
3730e4b17023SJohn Marino 	collect_source_refs (global_namespace);
3731e4b17023SJohn Marino 
3732e4b17023SJohn Marino       dump_ada_specs (collect_all_refs, cpp_check);
3733e4b17023SJohn Marino     }
3734e4b17023SJohn Marino 
3735e4b17023SJohn Marino   /* FIXME - huh?  was  input_line -= 1;*/
3736e4b17023SJohn Marino 
3737e4b17023SJohn Marino   timevar_start (TV_PHASE_DEFERRED);
3738e4b17023SJohn Marino 
3739e4b17023SJohn Marino   /* We now have to write out all the stuff we put off writing out.
3740e4b17023SJohn Marino      These include:
3741e4b17023SJohn Marino 
3742e4b17023SJohn Marino        o Template specializations that we have not yet instantiated,
3743e4b17023SJohn Marino 	 but which are needed.
3744e4b17023SJohn Marino        o Initialization and destruction for non-local objects with
3745e4b17023SJohn Marino 	 static storage duration.  (Local objects with static storage
3746e4b17023SJohn Marino 	 duration are initialized when their scope is first entered,
3747e4b17023SJohn Marino 	 and are cleaned up via atexit.)
3748e4b17023SJohn Marino        o Virtual function tables.
3749e4b17023SJohn Marino 
3750e4b17023SJohn Marino      All of these may cause others to be needed.  For example,
3751e4b17023SJohn Marino      instantiating one function may cause another to be needed, and
3752e4b17023SJohn Marino      generating the initializer for an object may cause templates to be
3753e4b17023SJohn Marino      instantiated, etc., etc.  */
3754e4b17023SJohn Marino 
3755e4b17023SJohn Marino   emit_support_tinfos ();
3756e4b17023SJohn Marino 
3757e4b17023SJohn Marino   do
3758e4b17023SJohn Marino     {
3759e4b17023SJohn Marino       tree t;
3760e4b17023SJohn Marino       tree decl;
3761e4b17023SJohn Marino 
3762e4b17023SJohn Marino       reconsider = false;
3763e4b17023SJohn Marino 
3764e4b17023SJohn Marino       /* If there are templates that we've put off instantiating, do
3765e4b17023SJohn Marino 	 them now.  */
3766e4b17023SJohn Marino       instantiate_pending_templates (retries);
3767e4b17023SJohn Marino       ggc_collect ();
3768e4b17023SJohn Marino 
3769e4b17023SJohn Marino       /* Write out virtual tables as required.  Note that writing out
3770e4b17023SJohn Marino 	 the virtual table for a template class may cause the
3771e4b17023SJohn Marino 	 instantiation of members of that class.  If we write out
3772e4b17023SJohn Marino 	 vtables then we remove the class from our list so we don't
3773e4b17023SJohn Marino 	 have to look at it again.  */
3774e4b17023SJohn Marino 
3775e4b17023SJohn Marino       while (keyed_classes != NULL_TREE
3776e4b17023SJohn Marino 	     && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
3777e4b17023SJohn Marino 	{
3778e4b17023SJohn Marino 	  reconsider = true;
3779e4b17023SJohn Marino 	  keyed_classes = TREE_CHAIN (keyed_classes);
3780e4b17023SJohn Marino 	}
3781e4b17023SJohn Marino 
3782e4b17023SJohn Marino       t = keyed_classes;
3783e4b17023SJohn Marino       if (t != NULL_TREE)
3784e4b17023SJohn Marino 	{
3785e4b17023SJohn Marino 	  tree next = TREE_CHAIN (t);
3786e4b17023SJohn Marino 
3787e4b17023SJohn Marino 	  while (next)
3788e4b17023SJohn Marino 	    {
3789e4b17023SJohn Marino 	      if (maybe_emit_vtables (TREE_VALUE (next)))
3790e4b17023SJohn Marino 		{
3791e4b17023SJohn Marino 		  reconsider = true;
3792e4b17023SJohn Marino 		  TREE_CHAIN (t) = TREE_CHAIN (next);
3793e4b17023SJohn Marino 		}
3794e4b17023SJohn Marino 	      else
3795e4b17023SJohn Marino 		t = next;
3796e4b17023SJohn Marino 
3797e4b17023SJohn Marino 	      next = TREE_CHAIN (t);
3798e4b17023SJohn Marino 	    }
3799e4b17023SJohn Marino 	}
3800e4b17023SJohn Marino 
3801e4b17023SJohn Marino       /* Write out needed type info variables.  We have to be careful
3802e4b17023SJohn Marino 	 looping through unemitted decls, because emit_tinfo_decl may
3803e4b17023SJohn Marino 	 cause other variables to be needed. New elements will be
3804e4b17023SJohn Marino 	 appended, and we remove from the vector those that actually
3805e4b17023SJohn Marino 	 get emitted.  */
3806e4b17023SJohn Marino       for (i = VEC_length (tree, unemitted_tinfo_decls);
3807e4b17023SJohn Marino 	   VEC_iterate (tree, unemitted_tinfo_decls, --i, t);)
3808e4b17023SJohn Marino 	if (emit_tinfo_decl (t))
3809e4b17023SJohn Marino 	  {
3810e4b17023SJohn Marino 	    reconsider = true;
3811e4b17023SJohn Marino 	    VEC_unordered_remove (tree, unemitted_tinfo_decls, i);
3812e4b17023SJohn Marino 	  }
3813e4b17023SJohn Marino 
3814e4b17023SJohn Marino       /* The list of objects with static storage duration is built up
3815e4b17023SJohn Marino 	 in reverse order.  We clear STATIC_AGGREGATES so that any new
3816e4b17023SJohn Marino 	 aggregates added during the initialization of these will be
3817e4b17023SJohn Marino 	 initialized in the correct order when we next come around the
3818e4b17023SJohn Marino 	 loop.  */
3819e4b17023SJohn Marino       vars = prune_vars_needing_no_initialization (&static_aggregates);
3820e4b17023SJohn Marino 
3821e4b17023SJohn Marino       if (vars)
3822e4b17023SJohn Marino 	{
3823e4b17023SJohn Marino 	  /* We need to start a new initialization function each time
3824e4b17023SJohn Marino 	     through the loop.  That's because we need to know which
3825e4b17023SJohn Marino 	     vtables have been referenced, and TREE_SYMBOL_REFERENCED
3826e4b17023SJohn Marino 	     isn't computed until a function is finished, and written
3827e4b17023SJohn Marino 	     out.  That's a deficiency in the back end.  When this is
3828e4b17023SJohn Marino 	     fixed, these initialization functions could all become
3829e4b17023SJohn Marino 	     inline, with resulting performance improvements.  */
3830e4b17023SJohn Marino 	  tree ssdf_body;
3831e4b17023SJohn Marino 
3832e4b17023SJohn Marino 	  /* Set the line and file, so that it is obviously not from
3833e4b17023SJohn Marino 	     the source file.  */
3834e4b17023SJohn Marino 	  input_location = locus;
3835e4b17023SJohn Marino 	  ssdf_body = start_static_storage_duration_function (ssdf_count);
3836e4b17023SJohn Marino 
3837e4b17023SJohn Marino 	  /* Make sure the back end knows about all the variables.  */
3838e4b17023SJohn Marino 	  write_out_vars (vars);
3839e4b17023SJohn Marino 
3840e4b17023SJohn Marino 	  /* First generate code to do all the initializations.  */
3841e4b17023SJohn Marino 	  if (vars)
3842e4b17023SJohn Marino 	    do_static_initialization_or_destruction (vars, /*initp=*/true);
3843e4b17023SJohn Marino 
3844e4b17023SJohn Marino 	  /* Then, generate code to do all the destructions.  Do these
3845e4b17023SJohn Marino 	     in reverse order so that the most recently constructed
3846e4b17023SJohn Marino 	     variable is the first destroyed.  If we're using
3847e4b17023SJohn Marino 	     __cxa_atexit, then we don't need to do this; functions
3848e4b17023SJohn Marino 	     were registered at initialization time to destroy the
3849e4b17023SJohn Marino 	     local statics.  */
3850e4b17023SJohn Marino 	  if (!flag_use_cxa_atexit && vars)
3851e4b17023SJohn Marino 	    {
3852e4b17023SJohn Marino 	      vars = nreverse (vars);
3853e4b17023SJohn Marino 	      do_static_initialization_or_destruction (vars, /*initp=*/false);
3854e4b17023SJohn Marino 	    }
3855e4b17023SJohn Marino 	  else
3856e4b17023SJohn Marino 	    vars = NULL_TREE;
3857e4b17023SJohn Marino 
3858e4b17023SJohn Marino 	  /* Finish up the static storage duration function for this
3859e4b17023SJohn Marino 	     round.  */
3860e4b17023SJohn Marino 	  input_location = locus;
3861e4b17023SJohn Marino 	  finish_static_storage_duration_function (ssdf_body);
3862e4b17023SJohn Marino 
3863e4b17023SJohn Marino 	  /* All those initializations and finalizations might cause
3864e4b17023SJohn Marino 	     us to need more inline functions, more template
3865e4b17023SJohn Marino 	     instantiations, etc.  */
3866e4b17023SJohn Marino 	  reconsider = true;
3867e4b17023SJohn Marino 	  ssdf_count++;
3868e4b17023SJohn Marino 	  /* ??? was:  locus.line++; */
3869e4b17023SJohn Marino 	}
3870e4b17023SJohn Marino 
3871e4b17023SJohn Marino       /* Go through the set of inline functions whose bodies have not
3872e4b17023SJohn Marino 	 been emitted yet.  If out-of-line copies of these functions
3873e4b17023SJohn Marino 	 are required, emit them.  */
3874e4b17023SJohn Marino       FOR_EACH_VEC_ELT (tree, deferred_fns, i, decl)
3875e4b17023SJohn Marino 	{
3876e4b17023SJohn Marino 	  /* Does it need synthesizing?  */
3877e4b17023SJohn Marino 	  if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
3878e4b17023SJohn Marino 	      && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
3879e4b17023SJohn Marino 	    {
3880e4b17023SJohn Marino 	      /* Even though we're already at the top-level, we push
3881e4b17023SJohn Marino 		 there again.  That way, when we pop back a few lines
3882e4b17023SJohn Marino 		 hence, all of our state is restored.  Otherwise,
3883e4b17023SJohn Marino 		 finish_function doesn't clean things up, and we end
3884e4b17023SJohn Marino 		 up with CURRENT_FUNCTION_DECL set.  */
3885e4b17023SJohn Marino 	      push_to_top_level ();
3886e4b17023SJohn Marino 	      /* The decl's location will mark where it was first
3887e4b17023SJohn Marino 		 needed.  Save that so synthesize method can indicate
3888e4b17023SJohn Marino 		 where it was needed from, in case of error  */
3889e4b17023SJohn Marino 	      input_location = DECL_SOURCE_LOCATION (decl);
3890e4b17023SJohn Marino 	      synthesize_method (decl);
3891e4b17023SJohn Marino 	      pop_from_top_level ();
3892e4b17023SJohn Marino 	      reconsider = true;
3893e4b17023SJohn Marino 	    }
3894e4b17023SJohn Marino 
3895e4b17023SJohn Marino 	  if (!DECL_SAVED_TREE (decl))
3896e4b17023SJohn Marino 	    continue;
3897e4b17023SJohn Marino 
3898e4b17023SJohn Marino 	  /* We lie to the back end, pretending that some functions
3899e4b17023SJohn Marino 	     are not defined when they really are.  This keeps these
3900e4b17023SJohn Marino 	     functions from being put out unnecessarily.  But, we must
3901e4b17023SJohn Marino 	     stop lying when the functions are referenced, or if they
3902e4b17023SJohn Marino 	     are not comdat since they need to be put out now.  If
3903e4b17023SJohn Marino 	     DECL_INTERFACE_KNOWN, then we have already set
3904e4b17023SJohn Marino 	     DECL_EXTERNAL appropriately, so there's no need to check
3905e4b17023SJohn Marino 	     again, and we do not want to clear DECL_EXTERNAL if a
3906e4b17023SJohn Marino 	     previous call to import_export_decl set it.
3907e4b17023SJohn Marino 
3908e4b17023SJohn Marino 	     This is done in a separate for cycle, because if some
3909e4b17023SJohn Marino 	     deferred function is contained in another deferred
3910e4b17023SJohn Marino 	     function later in deferred_fns varray,
3911e4b17023SJohn Marino 	     rest_of_compilation would skip this function and we
3912e4b17023SJohn Marino 	     really cannot expand the same function twice.  */
3913e4b17023SJohn Marino 	  import_export_decl (decl);
3914e4b17023SJohn Marino 	  if (DECL_NOT_REALLY_EXTERN (decl)
3915e4b17023SJohn Marino 	      && DECL_INITIAL (decl)
3916e4b17023SJohn Marino 	      && decl_needed_p (decl))
3917e4b17023SJohn Marino 	    {
3918e4b17023SJohn Marino 	      struct cgraph_node *node, *next;
3919e4b17023SJohn Marino 
3920e4b17023SJohn Marino 	      node = cgraph_get_node (decl);
3921e4b17023SJohn Marino 	      if (node->same_body_alias)
3922e4b17023SJohn Marino 		node = cgraph_alias_aliased_node (node);
3923e4b17023SJohn Marino 
3924e4b17023SJohn Marino 	      cgraph_for_node_and_aliases (node, clear_decl_external,
3925e4b17023SJohn Marino 					   NULL, true);
3926e4b17023SJohn Marino 	      /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
3927e4b17023SJohn Marino 		 group, we need to mark all symbols in the same comdat group
3928e4b17023SJohn Marino 		 that way.  */
3929e4b17023SJohn Marino 	      if (node->same_comdat_group)
3930e4b17023SJohn Marino 		for (next = node->same_comdat_group;
3931e4b17023SJohn Marino 		     next != node;
3932e4b17023SJohn Marino 		     next = next->same_comdat_group)
3933e4b17023SJohn Marino 	          cgraph_for_node_and_aliases (next, clear_decl_external,
3934e4b17023SJohn Marino 					       NULL, true);
3935e4b17023SJohn Marino 	    }
3936e4b17023SJohn Marino 
3937e4b17023SJohn Marino 	  /* If we're going to need to write this function out, and
3938e4b17023SJohn Marino 	     there's already a body for it, create RTL for it now.
3939e4b17023SJohn Marino 	     (There might be no body if this is a method we haven't
3940e4b17023SJohn Marino 	     gotten around to synthesizing yet.)  */
3941e4b17023SJohn Marino 	  if (!DECL_EXTERNAL (decl)
3942e4b17023SJohn Marino 	      && decl_needed_p (decl)
3943e4b17023SJohn Marino 	      && !TREE_ASM_WRITTEN (decl)
3944e4b17023SJohn Marino 	      && !cgraph_get_node (decl)->local.finalized)
3945e4b17023SJohn Marino 	    {
3946e4b17023SJohn Marino 	      /* We will output the function; no longer consider it in this
3947e4b17023SJohn Marino 		 loop.  */
3948e4b17023SJohn Marino 	      DECL_DEFER_OUTPUT (decl) = 0;
3949e4b17023SJohn Marino 	      /* Generate RTL for this function now that we know we
3950e4b17023SJohn Marino 		 need it.  */
3951e4b17023SJohn Marino 	      expand_or_defer_fn (decl);
3952e4b17023SJohn Marino 	      /* If we're compiling -fsyntax-only pretend that this
3953e4b17023SJohn Marino 		 function has been written out so that we don't try to
3954e4b17023SJohn Marino 		 expand it again.  */
3955e4b17023SJohn Marino 	      if (flag_syntax_only)
3956e4b17023SJohn Marino 		TREE_ASM_WRITTEN (decl) = 1;
3957e4b17023SJohn Marino 	      reconsider = true;
3958e4b17023SJohn Marino 	    }
3959e4b17023SJohn Marino 	}
3960e4b17023SJohn Marino 
3961e4b17023SJohn Marino       if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3962e4b17023SJohn Marino 	reconsider = true;
3963e4b17023SJohn Marino 
3964e4b17023SJohn Marino       /* Static data members are just like namespace-scope globals.  */
3965e4b17023SJohn Marino       FOR_EACH_VEC_ELT (tree, pending_statics, i, decl)
3966e4b17023SJohn Marino 	{
3967e4b17023SJohn Marino 	  if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
3968e4b17023SJohn Marino 	      /* Don't write it out if we haven't seen a definition.  */
3969e4b17023SJohn Marino 	      || DECL_IN_AGGR_P (decl))
3970e4b17023SJohn Marino 	    continue;
3971e4b17023SJohn Marino 	  import_export_decl (decl);
3972e4b17023SJohn Marino 	  /* If this static data member is needed, provide it to the
3973e4b17023SJohn Marino 	     back end.  */
3974e4b17023SJohn Marino 	  if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
3975e4b17023SJohn Marino 	    DECL_EXTERNAL (decl) = 0;
3976e4b17023SJohn Marino 	}
3977e4b17023SJohn Marino       if (VEC_length (tree, pending_statics) != 0
3978e4b17023SJohn Marino 	  && wrapup_global_declarations (VEC_address (tree, pending_statics),
3979e4b17023SJohn Marino 					 VEC_length (tree, pending_statics)))
3980e4b17023SJohn Marino 	reconsider = true;
3981e4b17023SJohn Marino 
3982e4b17023SJohn Marino       retries++;
3983e4b17023SJohn Marino     }
3984e4b17023SJohn Marino   while (reconsider);
3985e4b17023SJohn Marino 
3986e4b17023SJohn Marino   /* All used inline functions must have a definition at this point.  */
3987e4b17023SJohn Marino   FOR_EACH_VEC_ELT (tree, deferred_fns, i, decl)
3988e4b17023SJohn Marino     {
3989e4b17023SJohn Marino       if (/* Check online inline functions that were actually used.  */
3990e4b17023SJohn Marino 	  DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
3991e4b17023SJohn Marino 	  /* If the definition actually was available here, then the
3992e4b17023SJohn Marino 	     fact that the function was not defined merely represents
3993e4b17023SJohn Marino 	     that for some reason (use of a template repository,
3994e4b17023SJohn Marino 	     #pragma interface, etc.) we decided not to emit the
3995e4b17023SJohn Marino 	     definition here.  */
3996e4b17023SJohn Marino 	  && !DECL_INITIAL (decl)
3997e4b17023SJohn Marino 	  /* Don't complain if the template was defined.  */
3998e4b17023SJohn Marino 	  && !(DECL_TEMPLATE_INSTANTIATION (decl)
3999e4b17023SJohn Marino 	       && DECL_INITIAL (DECL_TEMPLATE_RESULT
4000e4b17023SJohn Marino 				(template_for_substitution (decl)))))
4001e4b17023SJohn Marino 	{
4002e4b17023SJohn Marino 	  warning (0, "inline function %q+D used but never defined", decl);
4003e4b17023SJohn Marino 	  /* Avoid a duplicate warning from check_global_declaration_1.  */
4004e4b17023SJohn Marino 	  TREE_NO_WARNING (decl) = 1;
4005e4b17023SJohn Marino 	}
4006e4b17023SJohn Marino     }
4007e4b17023SJohn Marino 
4008e4b17023SJohn Marino   /* So must decls that use a type with no linkage.  */
4009e4b17023SJohn Marino   FOR_EACH_VEC_ELT (tree, no_linkage_decls, i, decl)
4010e4b17023SJohn Marino     if (!decl_defined_p (decl))
4011e4b17023SJohn Marino       no_linkage_error (decl);
4012e4b17023SJohn Marino 
4013e4b17023SJohn Marino   /* Then, do the Objective-C stuff.  This is where all the
4014e4b17023SJohn Marino      Objective-C module stuff gets generated (symtab,
4015e4b17023SJohn Marino      class/protocol/selector lists etc).  This must be done after C++
4016e4b17023SJohn Marino      templates, destructors etc. so that selectors used in C++
4017e4b17023SJohn Marino      templates are properly allocated.  */
4018e4b17023SJohn Marino   if (c_dialect_objc ())
4019e4b17023SJohn Marino     objc_write_global_declarations ();
4020e4b17023SJohn Marino 
4021e4b17023SJohn Marino   /* We give C linkage to static constructors and destructors.  */
4022e4b17023SJohn Marino   push_lang_context (lang_name_c);
4023e4b17023SJohn Marino 
4024e4b17023SJohn Marino   /* Generate initialization and destruction functions for all
4025e4b17023SJohn Marino      priorities for which they are required.  */
4026e4b17023SJohn Marino   if (priority_info_map)
4027e4b17023SJohn Marino     splay_tree_foreach (priority_info_map,
4028e4b17023SJohn Marino 			generate_ctor_and_dtor_functions_for_priority,
4029e4b17023SJohn Marino 			/*data=*/&locus);
4030e4b17023SJohn Marino   else if (c_dialect_objc () && objc_static_init_needed_p ())
4031e4b17023SJohn Marino     /* If this is obj-c++ and we need a static init, call
4032e4b17023SJohn Marino        generate_ctor_or_dtor_function.  */
4033e4b17023SJohn Marino     generate_ctor_or_dtor_function (/*constructor_p=*/true,
4034e4b17023SJohn Marino 				    DEFAULT_INIT_PRIORITY, &locus);
4035e4b17023SJohn Marino 
4036e4b17023SJohn Marino   /* We're done with the splay-tree now.  */
4037e4b17023SJohn Marino   if (priority_info_map)
4038e4b17023SJohn Marino     splay_tree_delete (priority_info_map);
4039e4b17023SJohn Marino 
4040e4b17023SJohn Marino   /* Generate any missing aliases.  */
4041e4b17023SJohn Marino   maybe_apply_pending_pragma_weaks ();
4042e4b17023SJohn Marino 
4043e4b17023SJohn Marino   /* We're done with static constructors, so we can go back to "C++"
4044e4b17023SJohn Marino      linkage now.  */
4045e4b17023SJohn Marino   pop_lang_context ();
4046e4b17023SJohn Marino 
4047e4b17023SJohn Marino   /* Collect candidates for Java hidden aliases.  */
4048e4b17023SJohn Marino   candidates = collect_candidates_for_java_method_aliases ();
4049e4b17023SJohn Marino 
4050e4b17023SJohn Marino   timevar_stop (TV_PHASE_DEFERRED);
4051e4b17023SJohn Marino   timevar_start (TV_PHASE_CGRAPH);
4052e4b17023SJohn Marino 
4053e4b17023SJohn Marino   cgraph_finalize_compilation_unit ();
4054e4b17023SJohn Marino 
4055e4b17023SJohn Marino   timevar_stop (TV_PHASE_CGRAPH);
4056e4b17023SJohn Marino   timevar_start (TV_PHASE_CHECK_DBGINFO);
4057e4b17023SJohn Marino 
4058e4b17023SJohn Marino   /* Now, issue warnings about static, but not defined, functions,
4059e4b17023SJohn Marino      etc., and emit debugging information.  */
4060e4b17023SJohn Marino   walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
4061e4b17023SJohn Marino   if (VEC_length (tree, pending_statics) != 0)
4062e4b17023SJohn Marino     {
4063e4b17023SJohn Marino       check_global_declarations (VEC_address (tree, pending_statics),
4064e4b17023SJohn Marino 				 VEC_length (tree, pending_statics));
4065e4b17023SJohn Marino       emit_debug_global_declarations (VEC_address (tree, pending_statics),
4066e4b17023SJohn Marino 				      VEC_length (tree, pending_statics));
4067e4b17023SJohn Marino     }
4068e4b17023SJohn Marino 
4069e4b17023SJohn Marino   perform_deferred_noexcept_checks ();
4070e4b17023SJohn Marino 
4071e4b17023SJohn Marino   /* Generate hidden aliases for Java.  */
4072e4b17023SJohn Marino   if (candidates)
4073e4b17023SJohn Marino     {
4074e4b17023SJohn Marino       build_java_method_aliases (candidates);
4075e4b17023SJohn Marino       pointer_set_destroy (candidates);
4076e4b17023SJohn Marino     }
4077e4b17023SJohn Marino 
4078e4b17023SJohn Marino   finish_repo ();
4079e4b17023SJohn Marino 
4080e4b17023SJohn Marino   /* The entire file is now complete.  If requested, dump everything
4081e4b17023SJohn Marino      to a file.  */
4082e4b17023SJohn Marino   {
4083e4b17023SJohn Marino     int flags;
4084e4b17023SJohn Marino     FILE *stream = dump_begin (TDI_tu, &flags);
4085e4b17023SJohn Marino 
4086e4b17023SJohn Marino     if (stream)
4087e4b17023SJohn Marino       {
4088e4b17023SJohn Marino 	dump_node (global_namespace, flags & ~TDF_SLIM, stream);
4089e4b17023SJohn Marino 	dump_end (TDI_tu, stream);
4090e4b17023SJohn Marino       }
4091e4b17023SJohn Marino   }
4092e4b17023SJohn Marino 
4093e4b17023SJohn Marino   if (flag_detailed_statistics)
4094e4b17023SJohn Marino     {
4095e4b17023SJohn Marino       dump_tree_statistics ();
4096e4b17023SJohn Marino       dump_time_statistics ();
4097e4b17023SJohn Marino     }
4098e4b17023SJohn Marino   input_location = locus;
4099e4b17023SJohn Marino 
4100e4b17023SJohn Marino #ifdef ENABLE_CHECKING
4101e4b17023SJohn Marino   validate_conversion_obstack ();
4102e4b17023SJohn Marino #endif /* ENABLE_CHECKING */
4103e4b17023SJohn Marino 
4104e4b17023SJohn Marino   timevar_stop (TV_PHASE_CHECK_DBGINFO);
4105e4b17023SJohn Marino }
4106e4b17023SJohn Marino 
4107e4b17023SJohn Marino /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
4108e4b17023SJohn Marino    function to call in parse-tree form; it has not yet been
4109e4b17023SJohn Marino    semantically analyzed.  ARGS are the arguments to the function.
4110e4b17023SJohn Marino    They have already been semantically analyzed.  This may change
4111e4b17023SJohn Marino    ARGS.  */
4112e4b17023SJohn Marino 
4113e4b17023SJohn Marino tree
build_offset_ref_call_from_tree(tree fn,VEC (tree,gc)** args)4114e4b17023SJohn Marino build_offset_ref_call_from_tree (tree fn, VEC(tree,gc) **args)
4115e4b17023SJohn Marino {
4116e4b17023SJohn Marino   tree orig_fn;
4117e4b17023SJohn Marino   VEC(tree,gc) *orig_args = NULL;
4118e4b17023SJohn Marino   tree expr;
4119e4b17023SJohn Marino   tree object;
4120e4b17023SJohn Marino 
4121e4b17023SJohn Marino   orig_fn = fn;
4122e4b17023SJohn Marino   object = TREE_OPERAND (fn, 0);
4123e4b17023SJohn Marino 
4124e4b17023SJohn Marino   if (processing_template_decl)
4125e4b17023SJohn Marino     {
4126e4b17023SJohn Marino       gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
4127e4b17023SJohn Marino 		  || TREE_CODE (fn) == MEMBER_REF);
4128e4b17023SJohn Marino       if (type_dependent_expression_p (fn)
4129e4b17023SJohn Marino 	  || any_type_dependent_arguments_p (*args))
4130e4b17023SJohn Marino 	return build_nt_call_vec (fn, *args);
4131e4b17023SJohn Marino 
4132e4b17023SJohn Marino       orig_args = make_tree_vector_copy (*args);
4133e4b17023SJohn Marino 
4134e4b17023SJohn Marino       /* Transform the arguments and add the implicit "this"
4135e4b17023SJohn Marino 	 parameter.  That must be done before the FN is transformed
4136e4b17023SJohn Marino 	 because we depend on the form of FN.  */
4137e4b17023SJohn Marino       make_args_non_dependent (*args);
4138e4b17023SJohn Marino       object = build_non_dependent_expr (object);
4139e4b17023SJohn Marino       if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4140e4b17023SJohn Marino 	{
4141e4b17023SJohn Marino 	  if (TREE_CODE (fn) == DOTSTAR_EXPR)
4142e4b17023SJohn Marino 	    object = cp_build_addr_expr (object, tf_warning_or_error);
4143e4b17023SJohn Marino 	  VEC_safe_insert (tree, gc, *args, 0, object);
4144e4b17023SJohn Marino 	}
4145e4b17023SJohn Marino       /* Now that the arguments are done, transform FN.  */
4146e4b17023SJohn Marino       fn = build_non_dependent_expr (fn);
4147e4b17023SJohn Marino     }
4148e4b17023SJohn Marino 
4149e4b17023SJohn Marino   /* A qualified name corresponding to a bound pointer-to-member is
4150e4b17023SJohn Marino      represented as an OFFSET_REF:
4151e4b17023SJohn Marino 
4152e4b17023SJohn Marino 	struct B { void g(); };
4153e4b17023SJohn Marino 	void (B::*p)();
4154e4b17023SJohn Marino 	void B::g() { (this->*p)(); }  */
4155e4b17023SJohn Marino   if (TREE_CODE (fn) == OFFSET_REF)
4156e4b17023SJohn Marino     {
4157e4b17023SJohn Marino       tree object_addr = cp_build_addr_expr (object, tf_warning_or_error);
4158e4b17023SJohn Marino       fn = TREE_OPERAND (fn, 1);
4159e4b17023SJohn Marino       fn = get_member_function_from_ptrfunc (&object_addr, fn);
4160e4b17023SJohn Marino       VEC_safe_insert (tree, gc, *args, 0, object_addr);
4161e4b17023SJohn Marino     }
4162e4b17023SJohn Marino 
4163e4b17023SJohn Marino   if (CLASS_TYPE_P (TREE_TYPE (fn)))
4164e4b17023SJohn Marino     expr = build_op_call (fn, args, tf_warning_or_error);
4165e4b17023SJohn Marino   else
4166e4b17023SJohn Marino     expr = cp_build_function_call_vec (fn, args, tf_warning_or_error);
4167e4b17023SJohn Marino   if (processing_template_decl && expr != error_mark_node)
4168e4b17023SJohn Marino     expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
4169e4b17023SJohn Marino 
4170e4b17023SJohn Marino   if (orig_args != NULL)
4171e4b17023SJohn Marino     release_tree_vector (orig_args);
4172e4b17023SJohn Marino 
4173e4b17023SJohn Marino   return expr;
4174e4b17023SJohn Marino }
4175e4b17023SJohn Marino 
4176e4b17023SJohn Marino 
4177e4b17023SJohn Marino void
check_default_args(tree x)4178e4b17023SJohn Marino check_default_args (tree x)
4179e4b17023SJohn Marino {
4180e4b17023SJohn Marino   tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4181e4b17023SJohn Marino   bool saw_def = false;
4182e4b17023SJohn Marino   int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4183e4b17023SJohn Marino   for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4184e4b17023SJohn Marino     {
4185e4b17023SJohn Marino       if (TREE_PURPOSE (arg))
4186e4b17023SJohn Marino 	saw_def = true;
4187e4b17023SJohn Marino       else if (saw_def)
4188e4b17023SJohn Marino 	{
4189e4b17023SJohn Marino 	  error ("default argument missing for parameter %P of %q+#D", i, x);
4190e4b17023SJohn Marino 	  TREE_PURPOSE (arg) = error_mark_node;
4191e4b17023SJohn Marino 	}
4192e4b17023SJohn Marino     }
4193e4b17023SJohn Marino }
4194e4b17023SJohn Marino 
4195e4b17023SJohn Marino /* Return true if function DECL can be inlined.  This is used to force
4196e4b17023SJohn Marino    instantiation of methods that might be interesting for inlining.  */
4197e4b17023SJohn Marino bool
possibly_inlined_p(tree decl)4198e4b17023SJohn Marino possibly_inlined_p (tree decl)
4199e4b17023SJohn Marino {
4200e4b17023SJohn Marino   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4201e4b17023SJohn Marino   if (DECL_UNINLINABLE (decl))
4202e4b17023SJohn Marino     return false;
4203e4b17023SJohn Marino   if (!optimize || pragma_java_exceptions)
4204e4b17023SJohn Marino     return DECL_DECLARED_INLINE_P (decl);
4205e4b17023SJohn Marino   /* When optimizing, we might inline everything when flatten
4206e4b17023SJohn Marino      attribute or heuristics inlining for size or autoinlining
4207e4b17023SJohn Marino      is used.  */
4208e4b17023SJohn Marino   return true;
4209e4b17023SJohn Marino }
4210e4b17023SJohn Marino 
4211e4b17023SJohn Marino /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
4212e4b17023SJohn Marino    If DECL is a specialization or implicitly declared class member,
4213e4b17023SJohn Marino    generate the actual definition.  Return false if something goes
4214e4b17023SJohn Marino    wrong, true otherwise.  */
4215e4b17023SJohn Marino 
4216e4b17023SJohn Marino bool
mark_used(tree decl)4217e4b17023SJohn Marino mark_used (tree decl)
4218e4b17023SJohn Marino {
4219e4b17023SJohn Marino   /* If DECL is a BASELINK for a single function, then treat it just
4220e4b17023SJohn Marino      like the DECL for the function.  Otherwise, if the BASELINK is
4221e4b17023SJohn Marino      for an overloaded function, we don't know which function was
4222e4b17023SJohn Marino      actually used until after overload resolution.  */
4223e4b17023SJohn Marino   if (BASELINK_P (decl))
4224e4b17023SJohn Marino     {
4225e4b17023SJohn Marino       decl = BASELINK_FUNCTIONS (decl);
4226e4b17023SJohn Marino       if (really_overloaded_fn (decl))
4227e4b17023SJohn Marino 	return true;
4228e4b17023SJohn Marino       decl = OVL_CURRENT (decl);
4229e4b17023SJohn Marino     }
4230e4b17023SJohn Marino 
4231e4b17023SJohn Marino   /* Set TREE_USED for the benefit of -Wunused.  */
4232e4b17023SJohn Marino   TREE_USED (decl) = 1;
4233e4b17023SJohn Marino   if (DECL_CLONED_FUNCTION_P (decl))
4234e4b17023SJohn Marino     TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4235e4b17023SJohn Marino 
4236e4b17023SJohn Marino   if (TREE_CODE (decl) == FUNCTION_DECL
4237e4b17023SJohn Marino       && DECL_DELETED_FN (decl))
4238e4b17023SJohn Marino     {
4239e4b17023SJohn Marino       if (DECL_ARTIFICIAL (decl))
4240e4b17023SJohn Marino 	{
4241e4b17023SJohn Marino 	  if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
4242e4b17023SJohn Marino 	      && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
4243e4b17023SJohn Marino 	    {
4244e4b17023SJohn Marino 	      /* We mark a lambda conversion op as deleted if we can't
4245e4b17023SJohn Marino 		 generate it properly; see maybe_add_lambda_conv_op.  */
4246e4b17023SJohn Marino 	      sorry ("converting lambda which uses %<...%> to "
4247e4b17023SJohn Marino 		     "function pointer");
4248e4b17023SJohn Marino 	      return false;
4249e4b17023SJohn Marino 	    }
4250e4b17023SJohn Marino 	}
4251e4b17023SJohn Marino       error ("use of deleted function %qD", decl);
4252e4b17023SJohn Marino       if (!maybe_explain_implicit_delete (decl))
4253e4b17023SJohn Marino 	error_at (DECL_SOURCE_LOCATION (decl), "declared here");
4254e4b17023SJohn Marino       return false;
4255e4b17023SJohn Marino     }
4256e4b17023SJohn Marino 
4257e4b17023SJohn Marino   /* We can only check DECL_ODR_USED on variables or functions with
4258e4b17023SJohn Marino      DECL_LANG_SPECIFIC set, and these are also the only decls that we
4259e4b17023SJohn Marino      might need special handling for.  */
4260e4b17023SJohn Marino   if ((TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
4261e4b17023SJohn Marino       || DECL_LANG_SPECIFIC (decl) == NULL
4262e4b17023SJohn Marino       || DECL_THUNK_P (decl))
4263e4b17023SJohn Marino     return true;
4264e4b17023SJohn Marino 
4265e4b17023SJohn Marino   /* We only want to do this processing once.  We don't need to keep trying
4266e4b17023SJohn Marino      to instantiate inline templates, because unit-at-a-time will make sure
4267e4b17023SJohn Marino      we get them compiled before functions that want to inline them.  */
4268e4b17023SJohn Marino   if (DECL_ODR_USED (decl))
4269e4b17023SJohn Marino     return true;
4270e4b17023SJohn Marino 
4271e4b17023SJohn Marino   /* If within finish_function, defer the rest until that function
4272e4b17023SJohn Marino      finishes, otherwise it might recurse.  */
4273e4b17023SJohn Marino   if (defer_mark_used_calls)
4274e4b17023SJohn Marino     {
4275e4b17023SJohn Marino       VEC_safe_push (tree, gc, deferred_mark_used_calls, decl);
4276e4b17023SJohn Marino       return true;
4277e4b17023SJohn Marino     }
4278e4b17023SJohn Marino 
4279e4b17023SJohn Marino   if (TREE_CODE (decl) == FUNCTION_DECL)
4280e4b17023SJohn Marino     maybe_instantiate_noexcept (decl);
4281e4b17023SJohn Marino 
4282e4b17023SJohn Marino   /* Normally, we can wait until instantiation-time to synthesize DECL.
4283e4b17023SJohn Marino      However, if DECL is a static data member initialized with a constant
4284e4b17023SJohn Marino      or a constexpr function, we need it right now because a reference to
4285e4b17023SJohn Marino      such a data member or a call to such function is not value-dependent.  */
4286e4b17023SJohn Marino   if ((decl_maybe_constant_var_p (decl)
4287e4b17023SJohn Marino        || (TREE_CODE (decl) == FUNCTION_DECL
4288e4b17023SJohn Marino 	   && DECL_DECLARED_CONSTEXPR_P (decl)))
4289e4b17023SJohn Marino       && DECL_LANG_SPECIFIC (decl)
4290e4b17023SJohn Marino       && DECL_TEMPLATE_INFO (decl)
4291e4b17023SJohn Marino       && !uses_template_parms (DECL_TI_ARGS (decl)))
4292e4b17023SJohn Marino     {
4293e4b17023SJohn Marino       /* Instantiating a function will result in garbage collection.  We
4294e4b17023SJohn Marino 	 must treat this situation as if we were within the body of a
4295e4b17023SJohn Marino 	 function so as to avoid collecting live data only referenced from
4296e4b17023SJohn Marino 	 the stack (such as overload resolution candidates).  */
4297e4b17023SJohn Marino       ++function_depth;
4298e4b17023SJohn Marino       instantiate_decl (decl, /*defer_ok=*/false,
4299e4b17023SJohn Marino 			/*expl_inst_class_mem_p=*/false);
4300e4b17023SJohn Marino       --function_depth;
4301e4b17023SJohn Marino     }
4302e4b17023SJohn Marino 
4303e4b17023SJohn Marino   /* If we don't need a value, then we don't need to synthesize DECL.  */
4304e4b17023SJohn Marino   if (cp_unevaluated_operand != 0)
4305e4b17023SJohn Marino     return true;
4306e4b17023SJohn Marino 
4307e4b17023SJohn Marino   if (processing_template_decl)
4308e4b17023SJohn Marino     return true;
4309e4b17023SJohn Marino 
4310e4b17023SJohn Marino   /* Check this too in case we're within fold_non_dependent_expr.  */
4311e4b17023SJohn Marino   if (DECL_TEMPLATE_INFO (decl)
4312e4b17023SJohn Marino       && uses_template_parms (DECL_TI_ARGS (decl)))
4313e4b17023SJohn Marino     return true;
4314e4b17023SJohn Marino 
4315e4b17023SJohn Marino   DECL_ODR_USED (decl) = 1;
4316e4b17023SJohn Marino   if (DECL_CLONED_FUNCTION_P (decl))
4317e4b17023SJohn Marino     DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
4318e4b17023SJohn Marino 
4319e4b17023SJohn Marino   /* DR 757: A type without linkage shall not be used as the type of a
4320e4b17023SJohn Marino      variable or function with linkage, unless
4321e4b17023SJohn Marino    o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
4322e4b17023SJohn Marino    o the variable or function is not used (3.2 [basic.def.odr]) or is
4323e4b17023SJohn Marino    defined in the same translation unit.  */
4324e4b17023SJohn Marino   if (cxx_dialect > cxx98
4325e4b17023SJohn Marino       && decl_linkage (decl) != lk_none
4326e4b17023SJohn Marino       && !DECL_EXTERN_C_P (decl)
4327e4b17023SJohn Marino       && !DECL_ARTIFICIAL (decl)
4328e4b17023SJohn Marino       && !decl_defined_p (decl)
4329e4b17023SJohn Marino       && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
4330e4b17023SJohn Marino     {
4331e4b17023SJohn Marino       if (is_local_extern (decl))
4332e4b17023SJohn Marino 	/* There's no way to define a local extern, and adding it to
4333e4b17023SJohn Marino 	   the vector interferes with GC, so give an error now.  */
4334e4b17023SJohn Marino 	no_linkage_error (decl);
4335e4b17023SJohn Marino       else
4336e4b17023SJohn Marino 	VEC_safe_push (tree, gc, no_linkage_decls, decl);
4337e4b17023SJohn Marino     }
4338e4b17023SJohn Marino 
4339e4b17023SJohn Marino   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
4340e4b17023SJohn Marino       && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
4341e4b17023SJohn Marino     /* Remember it, so we can check it was defined.  */
4342e4b17023SJohn Marino     note_vague_linkage_fn (decl);
4343e4b17023SJohn Marino 
4344e4b17023SJohn Marino   /* Is it a synthesized method that needs to be synthesized?  */
4345e4b17023SJohn Marino   if (TREE_CODE (decl) == FUNCTION_DECL
4346e4b17023SJohn Marino       && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
4347e4b17023SJohn Marino       && DECL_DEFAULTED_FN (decl)
4348e4b17023SJohn Marino       /* A function defaulted outside the class is synthesized either by
4349e4b17023SJohn Marino 	 cp_finish_decl or instantiate_decl.  */
4350e4b17023SJohn Marino       && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
4351e4b17023SJohn Marino       && ! DECL_INITIAL (decl))
4352e4b17023SJohn Marino     {
4353e4b17023SJohn Marino       /* Defer virtual destructors so that thunks get the right
4354e4b17023SJohn Marino 	 linkage.  */
4355e4b17023SJohn Marino       if (DECL_VIRTUAL_P (decl) && !at_eof)
4356e4b17023SJohn Marino 	{
4357e4b17023SJohn Marino 	  note_vague_linkage_fn (decl);
4358e4b17023SJohn Marino 	  return true;
4359e4b17023SJohn Marino 	}
4360e4b17023SJohn Marino 
4361e4b17023SJohn Marino       /* Remember the current location for a function we will end up
4362e4b17023SJohn Marino 	 synthesizing.  Then we can inform the user where it was
4363e4b17023SJohn Marino 	 required in the case of error.  */
4364e4b17023SJohn Marino       DECL_SOURCE_LOCATION (decl) = input_location;
4365e4b17023SJohn Marino 
4366e4b17023SJohn Marino       /* Synthesizing an implicitly defined member function will result in
4367e4b17023SJohn Marino 	 garbage collection.  We must treat this situation as if we were
4368e4b17023SJohn Marino 	 within the body of a function so as to avoid collecting live data
4369e4b17023SJohn Marino 	 on the stack (such as overload resolution candidates).
4370e4b17023SJohn Marino 
4371e4b17023SJohn Marino          We could just let cp_write_global_declarations handle synthesizing
4372e4b17023SJohn Marino          this function by adding it to deferred_fns, but doing
4373e4b17023SJohn Marino          it at the use site produces better error messages.  */
4374e4b17023SJohn Marino       ++function_depth;
4375e4b17023SJohn Marino       synthesize_method (decl);
4376e4b17023SJohn Marino       --function_depth;
4377e4b17023SJohn Marino       /* If this is a synthesized method we don't need to
4378e4b17023SJohn Marino 	 do the instantiation test below.  */
4379e4b17023SJohn Marino     }
4380e4b17023SJohn Marino   else if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4381e4b17023SJohn Marino 	   && DECL_TEMPLATE_INFO (decl)
4382e4b17023SJohn Marino 	   && (!DECL_EXPLICIT_INSTANTIATION (decl)
4383e4b17023SJohn Marino 	       || always_instantiate_p (decl)))
4384e4b17023SJohn Marino     /* If this is a function or variable that is an instance of some
4385e4b17023SJohn Marino        template, we now know that we will need to actually do the
4386e4b17023SJohn Marino        instantiation. We check that DECL is not an explicit
4387e4b17023SJohn Marino        instantiation because that is not checked in instantiate_decl.
4388e4b17023SJohn Marino 
4389e4b17023SJohn Marino        We put off instantiating functions in order to improve compile
4390e4b17023SJohn Marino        times.  Maintaining a stack of active functions is expensive,
4391e4b17023SJohn Marino        and the inliner knows to instantiate any functions it might
4392e4b17023SJohn Marino        need.  Therefore, we always try to defer instantiation.  */
4393e4b17023SJohn Marino     {
4394e4b17023SJohn Marino       ++function_depth;
4395e4b17023SJohn Marino       instantiate_decl (decl, /*defer_ok=*/true,
4396e4b17023SJohn Marino 			/*expl_inst_class_mem_p=*/false);
4397e4b17023SJohn Marino       --function_depth;
4398e4b17023SJohn Marino     }
4399e4b17023SJohn Marino 
4400e4b17023SJohn Marino   return true;
4401e4b17023SJohn Marino }
4402e4b17023SJohn Marino 
4403e4b17023SJohn Marino #include "gt-cp-decl2.h"
4404