1*404b540aSrobert /* RunTime Type Identification
2*404b540aSrobert Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3*404b540aSrobert 2005, 2006
4*404b540aSrobert Free Software Foundation, Inc.
5*404b540aSrobert Mostly written by Jason Merrill (jason@cygnus.com).
6*404b540aSrobert
7*404b540aSrobert This file is part of GCC.
8*404b540aSrobert
9*404b540aSrobert GCC is free software; you can redistribute it and/or modify
10*404b540aSrobert it under the terms of the GNU General Public License as published by
11*404b540aSrobert the Free Software Foundation; either version 2, or (at your option)
12*404b540aSrobert any later version.
13*404b540aSrobert
14*404b540aSrobert GCC is distributed in the hope that it will be useful,
15*404b540aSrobert but WITHOUT ANY WARRANTY; without even the implied warranty of
16*404b540aSrobert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17*404b540aSrobert GNU General Public License for more details.
18*404b540aSrobert
19*404b540aSrobert You should have received a copy of the GNU General Public License
20*404b540aSrobert along with GCC; see the file COPYING. If not, write to
21*404b540aSrobert the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22*404b540aSrobert Boston, MA 02110-1301, USA. */
23*404b540aSrobert
24*404b540aSrobert #include "config.h"
25*404b540aSrobert #include "system.h"
26*404b540aSrobert #include "coretypes.h"
27*404b540aSrobert #include "tm.h"
28*404b540aSrobert #include "tree.h"
29*404b540aSrobert #include "cp-tree.h"
30*404b540aSrobert #include "flags.h"
31*404b540aSrobert #include "output.h"
32*404b540aSrobert #include "assert.h"
33*404b540aSrobert #include "toplev.h"
34*404b540aSrobert #include "convert.h"
35*404b540aSrobert
36*404b540aSrobert /* C++ returns type information to the user in struct type_info
37*404b540aSrobert objects. We also use type information to implement dynamic_cast and
38*404b540aSrobert exception handlers. Type information for a particular type is
39*404b540aSrobert indicated with an ABI defined structure derived from type_info.
40*404b540aSrobert This would all be very straight forward, but for the fact that the
41*404b540aSrobert runtime library provides the definitions of the type_info structure
42*404b540aSrobert and the ABI defined derived classes. We cannot build declarations
43*404b540aSrobert of them directly in the compiler, but we need to layout objects of
44*404b540aSrobert their type. Somewhere we have to lie.
45*404b540aSrobert
46*404b540aSrobert We define layout compatible POD-structs with compiler-defined names
47*404b540aSrobert and generate the appropriate initializations for them (complete
48*404b540aSrobert with explicit mention of their vtable). When we have to provide a
49*404b540aSrobert type_info to the user we reinterpret_cast the internal compiler
50*404b540aSrobert type to type_info. A well formed program can only explicitly refer
51*404b540aSrobert to the type_infos of complete types (& cv void). However, we chain
52*404b540aSrobert pointer type_infos to the pointed-to-type, and that can be
53*404b540aSrobert incomplete. We only need the addresses of such incomplete
54*404b540aSrobert type_info objects for static initialization.
55*404b540aSrobert
56*404b540aSrobert The type information VAR_DECL of a type is held on the
57*404b540aSrobert IDENTIFIER_GLOBAL_VALUE of the type's mangled name. That VAR_DECL
58*404b540aSrobert will be the internal type. It will usually have the correct
59*404b540aSrobert internal type reflecting the kind of type it represents (pointer,
60*404b540aSrobert array, function, class, inherited class, etc). When the type it
61*404b540aSrobert represents is incomplete, it will have the internal type
62*404b540aSrobert corresponding to type_info. That will only happen at the end of
63*404b540aSrobert translation, when we are emitting the type info objects. */
64*404b540aSrobert
65*404b540aSrobert /* Auxiliary data we hold for each type_info derived object we need. */
66*404b540aSrobert typedef struct tinfo_s GTY (())
67*404b540aSrobert {
68*404b540aSrobert tree type; /* The RECORD_TYPE for this type_info object */
69*404b540aSrobert
70*404b540aSrobert tree vtable; /* The VAR_DECL of the vtable. Only filled at end of
71*404b540aSrobert translation. */
72*404b540aSrobert
73*404b540aSrobert tree name; /* IDENTIFIER_NODE for the ABI specified name of
74*404b540aSrobert the type_info derived type. */
75*404b540aSrobert } tinfo_s;
76*404b540aSrobert
77*404b540aSrobert DEF_VEC_O(tinfo_s);
78*404b540aSrobert DEF_VEC_ALLOC_O(tinfo_s,gc);
79*404b540aSrobert
80*404b540aSrobert typedef enum tinfo_kind
81*404b540aSrobert {
82*404b540aSrobert TK_TYPE_INFO_TYPE, /* std::type_info */
83*404b540aSrobert TK_BASE_TYPE, /* abi::__base_class_type_info */
84*404b540aSrobert TK_BUILTIN_TYPE, /* abi::__fundamental_type_info */
85*404b540aSrobert TK_ARRAY_TYPE, /* abi::__array_type_info */
86*404b540aSrobert TK_FUNCTION_TYPE, /* abi::__function_type_info */
87*404b540aSrobert TK_ENUMERAL_TYPE, /* abi::__enum_type_info */
88*404b540aSrobert TK_POINTER_TYPE, /* abi::__pointer_type_info */
89*404b540aSrobert TK_POINTER_MEMBER_TYPE, /* abi::__pointer_to_member_type_info */
90*404b540aSrobert TK_CLASS_TYPE, /* abi::__class_type_info */
91*404b540aSrobert TK_SI_CLASS_TYPE, /* abi::__si_class_type_info */
92*404b540aSrobert TK_FIXED /* end of fixed descriptors. */
93*404b540aSrobert /* ... abi::__vmi_type_info<I> */
94*404b540aSrobert } tinfo_kind;
95*404b540aSrobert
96*404b540aSrobert /* A vector of all tinfo decls that haven't yet been emitted. */
97*404b540aSrobert VEC(tree,gc) *unemitted_tinfo_decls;
98*404b540aSrobert
99*404b540aSrobert /* A vector of all type_info derived types we need. The first few are
100*404b540aSrobert fixed and created early. The remainder are for multiple inheritance
101*404b540aSrobert and are generated as needed. */
102*404b540aSrobert static GTY (()) VEC(tinfo_s,gc) *tinfo_descs;
103*404b540aSrobert
104*404b540aSrobert static tree build_headof (tree);
105*404b540aSrobert static tree ifnonnull (tree, tree);
106*404b540aSrobert static tree tinfo_name (tree);
107*404b540aSrobert static tree build_dynamic_cast_1 (tree, tree);
108*404b540aSrobert static tree throw_bad_cast (void);
109*404b540aSrobert static tree throw_bad_typeid (void);
110*404b540aSrobert static tree get_tinfo_decl_dynamic (tree);
111*404b540aSrobert static tree get_tinfo_ptr (tree);
112*404b540aSrobert static bool typeid_ok_p (void);
113*404b540aSrobert static int qualifier_flags (tree);
114*404b540aSrobert static bool target_incomplete_p (tree);
115*404b540aSrobert static tree tinfo_base_init (tinfo_s *, tree);
116*404b540aSrobert static tree generic_initializer (tinfo_s *, tree);
117*404b540aSrobert static tree ptr_initializer (tinfo_s *, tree);
118*404b540aSrobert static tree ptm_initializer (tinfo_s *, tree);
119*404b540aSrobert static tree class_initializer (tinfo_s *, tree, tree);
120*404b540aSrobert static void create_pseudo_type_info (int, const char *, ...);
121*404b540aSrobert static tree get_pseudo_ti_init (tree, unsigned);
122*404b540aSrobert static unsigned get_pseudo_ti_index (tree);
123*404b540aSrobert static void create_tinfo_types (void);
124*404b540aSrobert static bool typeinfo_in_lib_p (tree);
125*404b540aSrobert
126*404b540aSrobert static int doing_runtime = 0;
127*404b540aSrobert
128*404b540aSrobert
129*404b540aSrobert /* Declare language defined type_info type and a pointer to const
130*404b540aSrobert type_info. This is incomplete here, and will be completed when
131*404b540aSrobert the user #includes <typeinfo>. There are language defined
132*404b540aSrobert restrictions on what can be done until that is included. Create
133*404b540aSrobert the internal versions of the ABI types. */
134*404b540aSrobert
135*404b540aSrobert void
init_rtti_processing(void)136*404b540aSrobert init_rtti_processing (void)
137*404b540aSrobert {
138*404b540aSrobert tree type_info_type;
139*404b540aSrobert
140*404b540aSrobert push_namespace (std_identifier);
141*404b540aSrobert type_info_type = xref_tag (class_type, get_identifier ("type_info"),
142*404b540aSrobert /*tag_scope=*/ts_current, false);
143*404b540aSrobert pop_namespace ();
144*404b540aSrobert const_type_info_type_node
145*404b540aSrobert = build_qualified_type (type_info_type, TYPE_QUAL_CONST);
146*404b540aSrobert type_info_ptr_type = build_pointer_type (const_type_info_type_node);
147*404b540aSrobert
148*404b540aSrobert unemitted_tinfo_decls = VEC_alloc (tree, gc, 124);
149*404b540aSrobert
150*404b540aSrobert create_tinfo_types ();
151*404b540aSrobert }
152*404b540aSrobert
153*404b540aSrobert /* Given the expression EXP of type `class *', return the head of the
154*404b540aSrobert object pointed to by EXP with type cv void*, if the class has any
155*404b540aSrobert virtual functions (TYPE_POLYMORPHIC_P), else just return the
156*404b540aSrobert expression. */
157*404b540aSrobert
158*404b540aSrobert static tree
build_headof(tree exp)159*404b540aSrobert build_headof (tree exp)
160*404b540aSrobert {
161*404b540aSrobert tree type = TREE_TYPE (exp);
162*404b540aSrobert tree offset;
163*404b540aSrobert tree index;
164*404b540aSrobert
165*404b540aSrobert gcc_assert (TREE_CODE (type) == POINTER_TYPE);
166*404b540aSrobert type = TREE_TYPE (type);
167*404b540aSrobert
168*404b540aSrobert if (!TYPE_POLYMORPHIC_P (type))
169*404b540aSrobert return exp;
170*404b540aSrobert
171*404b540aSrobert /* We use this a couple of times below, protect it. */
172*404b540aSrobert exp = save_expr (exp);
173*404b540aSrobert
174*404b540aSrobert /* The offset-to-top field is at index -2 from the vptr. */
175*404b540aSrobert index = build_int_cst (NULL_TREE,
176*404b540aSrobert -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
177*404b540aSrobert
178*404b540aSrobert offset = build_vtbl_ref (build_indirect_ref (exp, NULL), index);
179*404b540aSrobert
180*404b540aSrobert type = build_qualified_type (ptr_type_node,
181*404b540aSrobert cp_type_quals (TREE_TYPE (exp)));
182*404b540aSrobert return build2 (PLUS_EXPR, type, exp,
183*404b540aSrobert convert_to_integer (ptrdiff_type_node, offset));
184*404b540aSrobert }
185*404b540aSrobert
186*404b540aSrobert /* Get a bad_cast node for the program to throw...
187*404b540aSrobert
188*404b540aSrobert See libstdc++/exception.cc for __throw_bad_cast */
189*404b540aSrobert
190*404b540aSrobert static tree
throw_bad_cast(void)191*404b540aSrobert throw_bad_cast (void)
192*404b540aSrobert {
193*404b540aSrobert tree fn = get_identifier ("__cxa_bad_cast");
194*404b540aSrobert if (!get_global_value_if_present (fn, &fn))
195*404b540aSrobert fn = push_throw_library_fn (fn, build_function_type (ptr_type_node,
196*404b540aSrobert void_list_node));
197*404b540aSrobert
198*404b540aSrobert return build_cxx_call (fn, NULL_TREE);
199*404b540aSrobert }
200*404b540aSrobert
201*404b540aSrobert /* Return an expression for "__cxa_bad_typeid()". The expression
202*404b540aSrobert returned is an lvalue of type "const std::type_info". */
203*404b540aSrobert
204*404b540aSrobert static tree
throw_bad_typeid(void)205*404b540aSrobert throw_bad_typeid (void)
206*404b540aSrobert {
207*404b540aSrobert tree fn = get_identifier ("__cxa_bad_typeid");
208*404b540aSrobert if (!get_global_value_if_present (fn, &fn))
209*404b540aSrobert {
210*404b540aSrobert tree t;
211*404b540aSrobert
212*404b540aSrobert t = build_reference_type (const_type_info_type_node);
213*404b540aSrobert t = build_function_type (t, void_list_node);
214*404b540aSrobert fn = push_throw_library_fn (fn, t);
215*404b540aSrobert }
216*404b540aSrobert
217*404b540aSrobert return build_cxx_call (fn, NULL_TREE);
218*404b540aSrobert }
219*404b540aSrobert
220*404b540aSrobert /* Return an lvalue expression whose type is "const std::type_info"
221*404b540aSrobert and whose value indicates the type of the expression EXP. If EXP
222*404b540aSrobert is a reference to a polymorphic class, return the dynamic type;
223*404b540aSrobert otherwise return the static type of the expression. */
224*404b540aSrobert
225*404b540aSrobert static tree
get_tinfo_decl_dynamic(tree exp)226*404b540aSrobert get_tinfo_decl_dynamic (tree exp)
227*404b540aSrobert {
228*404b540aSrobert tree type;
229*404b540aSrobert tree t;
230*404b540aSrobert
231*404b540aSrobert if (error_operand_p (exp))
232*404b540aSrobert return error_mark_node;
233*404b540aSrobert
234*404b540aSrobert /* peel back references, so they match. */
235*404b540aSrobert type = non_reference (TREE_TYPE (exp));
236*404b540aSrobert
237*404b540aSrobert /* Peel off cv qualifiers. */
238*404b540aSrobert type = TYPE_MAIN_VARIANT (type);
239*404b540aSrobert
240*404b540aSrobert if (!VOID_TYPE_P (type))
241*404b540aSrobert type = complete_type_or_else (type, exp);
242*404b540aSrobert
243*404b540aSrobert if (!type)
244*404b540aSrobert return error_mark_node;
245*404b540aSrobert
246*404b540aSrobert /* If exp is a reference to polymorphic type, get the real type_info. */
247*404b540aSrobert if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
248*404b540aSrobert {
249*404b540aSrobert /* build reference to type_info from vtable. */
250*404b540aSrobert tree index;
251*404b540aSrobert
252*404b540aSrobert /* The RTTI information is at index -1. */
253*404b540aSrobert index = build_int_cst (NULL_TREE,
254*404b540aSrobert -1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
255*404b540aSrobert t = build_vtbl_ref (exp, index);
256*404b540aSrobert t = convert (type_info_ptr_type, t);
257*404b540aSrobert }
258*404b540aSrobert else
259*404b540aSrobert /* Otherwise return the type_info for the static type of the expr. */
260*404b540aSrobert t = get_tinfo_ptr (TYPE_MAIN_VARIANT (type));
261*404b540aSrobert
262*404b540aSrobert return build_indirect_ref (t, NULL);
263*404b540aSrobert }
264*404b540aSrobert
265*404b540aSrobert static bool
typeid_ok_p(void)266*404b540aSrobert typeid_ok_p (void)
267*404b540aSrobert {
268*404b540aSrobert if (! flag_rtti)
269*404b540aSrobert {
270*404b540aSrobert error ("cannot use typeid with -fno-rtti");
271*404b540aSrobert return false;
272*404b540aSrobert }
273*404b540aSrobert
274*404b540aSrobert if (!COMPLETE_TYPE_P (const_type_info_type_node))
275*404b540aSrobert {
276*404b540aSrobert error ("must #include <typeinfo> before using typeid");
277*404b540aSrobert return false;
278*404b540aSrobert }
279*404b540aSrobert
280*404b540aSrobert return true;
281*404b540aSrobert }
282*404b540aSrobert
283*404b540aSrobert /* Return an expression for "typeid(EXP)". The expression returned is
284*404b540aSrobert an lvalue of type "const std::type_info". */
285*404b540aSrobert
286*404b540aSrobert tree
build_typeid(tree exp)287*404b540aSrobert build_typeid (tree exp)
288*404b540aSrobert {
289*404b540aSrobert tree cond = NULL_TREE;
290*404b540aSrobert int nonnull = 0;
291*404b540aSrobert
292*404b540aSrobert if (exp == error_mark_node || !typeid_ok_p ())
293*404b540aSrobert return error_mark_node;
294*404b540aSrobert
295*404b540aSrobert if (processing_template_decl)
296*404b540aSrobert return build_min (TYPEID_EXPR, const_type_info_type_node, exp);
297*404b540aSrobert
298*404b540aSrobert if (TREE_CODE (exp) == INDIRECT_REF
299*404b540aSrobert && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
300*404b540aSrobert && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
301*404b540aSrobert && ! resolves_to_fixed_type_p (exp, &nonnull)
302*404b540aSrobert && ! nonnull)
303*404b540aSrobert {
304*404b540aSrobert exp = stabilize_reference (exp);
305*404b540aSrobert cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0));
306*404b540aSrobert }
307*404b540aSrobert
308*404b540aSrobert exp = get_tinfo_decl_dynamic (exp);
309*404b540aSrobert
310*404b540aSrobert if (exp == error_mark_node)
311*404b540aSrobert return error_mark_node;
312*404b540aSrobert
313*404b540aSrobert if (cond)
314*404b540aSrobert {
315*404b540aSrobert tree bad = throw_bad_typeid ();
316*404b540aSrobert
317*404b540aSrobert exp = build3 (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
318*404b540aSrobert }
319*404b540aSrobert
320*404b540aSrobert return exp;
321*404b540aSrobert }
322*404b540aSrobert
323*404b540aSrobert /* Generate the NTBS name of a type. */
324*404b540aSrobert static tree
tinfo_name(tree type)325*404b540aSrobert tinfo_name (tree type)
326*404b540aSrobert {
327*404b540aSrobert const char *name;
328*404b540aSrobert tree name_string;
329*404b540aSrobert
330*404b540aSrobert name = mangle_type_string (type);
331*404b540aSrobert name_string = fix_string_type (build_string (strlen (name) + 1, name));
332*404b540aSrobert return name_string;
333*404b540aSrobert }
334*404b540aSrobert
335*404b540aSrobert /* Return a VAR_DECL for the internal ABI defined type_info object for
336*404b540aSrobert TYPE. You must arrange that the decl is mark_used, if actually use
337*404b540aSrobert it --- decls in vtables are only used if the vtable is output. */
338*404b540aSrobert
339*404b540aSrobert tree
get_tinfo_decl(tree type)340*404b540aSrobert get_tinfo_decl (tree type)
341*404b540aSrobert {
342*404b540aSrobert tree name;
343*404b540aSrobert tree d;
344*404b540aSrobert
345*404b540aSrobert if (variably_modified_type_p (type, /*fn=*/NULL_TREE))
346*404b540aSrobert {
347*404b540aSrobert error ("cannot create type information for type %qT because "
348*404b540aSrobert "it involves types of variable size",
349*404b540aSrobert type);
350*404b540aSrobert return error_mark_node;
351*404b540aSrobert }
352*404b540aSrobert
353*404b540aSrobert if (TREE_CODE (type) == METHOD_TYPE)
354*404b540aSrobert type = build_function_type (TREE_TYPE (type),
355*404b540aSrobert TREE_CHAIN (TYPE_ARG_TYPES (type)));
356*404b540aSrobert
357*404b540aSrobert /* For a class type, the variable is cached in the type node
358*404b540aSrobert itself. */
359*404b540aSrobert if (CLASS_TYPE_P (type))
360*404b540aSrobert {
361*404b540aSrobert d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type));
362*404b540aSrobert if (d)
363*404b540aSrobert return d;
364*404b540aSrobert }
365*404b540aSrobert
366*404b540aSrobert name = mangle_typeinfo_for_type (type);
367*404b540aSrobert
368*404b540aSrobert d = IDENTIFIER_GLOBAL_VALUE (name);
369*404b540aSrobert if (!d)
370*404b540aSrobert {
371*404b540aSrobert int ix = get_pseudo_ti_index (type);
372*404b540aSrobert tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, ix);
373*404b540aSrobert
374*404b540aSrobert d = build_lang_decl (VAR_DECL, name, ti->type);
375*404b540aSrobert SET_DECL_ASSEMBLER_NAME (d, name);
376*404b540aSrobert /* Remember the type it is for. */
377*404b540aSrobert TREE_TYPE (name) = type;
378*404b540aSrobert DECL_TINFO_P (d) = 1;
379*404b540aSrobert DECL_ARTIFICIAL (d) = 1;
380*404b540aSrobert DECL_IGNORED_P (d) = 1;
381*404b540aSrobert TREE_READONLY (d) = 1;
382*404b540aSrobert TREE_STATIC (d) = 1;
383*404b540aSrobert /* Mark the variable as undefined -- but remember that we can
384*404b540aSrobert define it later if we need to do so. */
385*404b540aSrobert DECL_EXTERNAL (d) = 1;
386*404b540aSrobert DECL_NOT_REALLY_EXTERN (d) = 1;
387*404b540aSrobert if (CLASS_TYPE_P (type))
388*404b540aSrobert CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d;
389*404b540aSrobert set_linkage_according_to_type (type, d);
390*404b540aSrobert pushdecl_top_level_and_finish (d, NULL_TREE);
391*404b540aSrobert
392*404b540aSrobert /* Add decl to the global array of tinfo decls. */
393*404b540aSrobert VEC_safe_push (tree, gc, unemitted_tinfo_decls, d);
394*404b540aSrobert }
395*404b540aSrobert
396*404b540aSrobert return d;
397*404b540aSrobert }
398*404b540aSrobert
399*404b540aSrobert /* Return a pointer to a type_info object describing TYPE, suitably
400*404b540aSrobert cast to the language defined type. */
401*404b540aSrobert
402*404b540aSrobert static tree
get_tinfo_ptr(tree type)403*404b540aSrobert get_tinfo_ptr (tree type)
404*404b540aSrobert {
405*404b540aSrobert tree decl = get_tinfo_decl (type);
406*404b540aSrobert
407*404b540aSrobert mark_used (decl);
408*404b540aSrobert return build_nop (type_info_ptr_type,
409*404b540aSrobert build_address (decl));
410*404b540aSrobert }
411*404b540aSrobert
412*404b540aSrobert /* Return the type_info object for TYPE. */
413*404b540aSrobert
414*404b540aSrobert tree
get_typeid(tree type)415*404b540aSrobert get_typeid (tree type)
416*404b540aSrobert {
417*404b540aSrobert if (type == error_mark_node || !typeid_ok_p ())
418*404b540aSrobert return error_mark_node;
419*404b540aSrobert
420*404b540aSrobert if (processing_template_decl)
421*404b540aSrobert return build_min (TYPEID_EXPR, const_type_info_type_node, type);
422*404b540aSrobert
423*404b540aSrobert /* If the type of the type-id is a reference type, the result of the
424*404b540aSrobert typeid expression refers to a type_info object representing the
425*404b540aSrobert referenced type. */
426*404b540aSrobert type = non_reference (type);
427*404b540aSrobert
428*404b540aSrobert /* The top-level cv-qualifiers of the lvalue expression or the type-id
429*404b540aSrobert that is the operand of typeid are always ignored. */
430*404b540aSrobert type = TYPE_MAIN_VARIANT (type);
431*404b540aSrobert
432*404b540aSrobert if (!VOID_TYPE_P (type))
433*404b540aSrobert type = complete_type_or_else (type, NULL_TREE);
434*404b540aSrobert
435*404b540aSrobert if (!type)
436*404b540aSrobert return error_mark_node;
437*404b540aSrobert
438*404b540aSrobert return build_indirect_ref (get_tinfo_ptr (type), NULL);
439*404b540aSrobert }
440*404b540aSrobert
441*404b540aSrobert /* Check whether TEST is null before returning RESULT. If TEST is used in
442*404b540aSrobert RESULT, it must have previously had a save_expr applied to it. */
443*404b540aSrobert
444*404b540aSrobert static tree
ifnonnull(tree test,tree result)445*404b540aSrobert ifnonnull (tree test, tree result)
446*404b540aSrobert {
447*404b540aSrobert return build3 (COND_EXPR, TREE_TYPE (result),
448*404b540aSrobert build2 (EQ_EXPR, boolean_type_node, test,
449*404b540aSrobert cp_convert (TREE_TYPE (test), integer_zero_node)),
450*404b540aSrobert cp_convert (TREE_TYPE (result), integer_zero_node),
451*404b540aSrobert result);
452*404b540aSrobert }
453*404b540aSrobert
454*404b540aSrobert /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
455*404b540aSrobert paper. */
456*404b540aSrobert
457*404b540aSrobert static tree
build_dynamic_cast_1(tree type,tree expr)458*404b540aSrobert build_dynamic_cast_1 (tree type, tree expr)
459*404b540aSrobert {
460*404b540aSrobert enum tree_code tc = TREE_CODE (type);
461*404b540aSrobert tree exprtype = TREE_TYPE (expr);
462*404b540aSrobert tree dcast_fn;
463*404b540aSrobert tree old_expr = expr;
464*404b540aSrobert const char *errstr = NULL;
465*404b540aSrobert
466*404b540aSrobert /* Save casted types in the function's used types hash table. */
467*404b540aSrobert used_types_insert (type);
468*404b540aSrobert
469*404b540aSrobert /* T shall be a pointer or reference to a complete class type, or
470*404b540aSrobert `pointer to cv void''. */
471*404b540aSrobert switch (tc)
472*404b540aSrobert {
473*404b540aSrobert case POINTER_TYPE:
474*404b540aSrobert if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
475*404b540aSrobert break;
476*404b540aSrobert /* Fall through. */
477*404b540aSrobert case REFERENCE_TYPE:
478*404b540aSrobert if (! IS_AGGR_TYPE (TREE_TYPE (type)))
479*404b540aSrobert {
480*404b540aSrobert errstr = "target is not pointer or reference to class";
481*404b540aSrobert goto fail;
482*404b540aSrobert }
483*404b540aSrobert if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
484*404b540aSrobert {
485*404b540aSrobert errstr = "target is not pointer or reference to complete type";
486*404b540aSrobert goto fail;
487*404b540aSrobert }
488*404b540aSrobert break;
489*404b540aSrobert
490*404b540aSrobert default:
491*404b540aSrobert errstr = "target is not pointer or reference";
492*404b540aSrobert goto fail;
493*404b540aSrobert }
494*404b540aSrobert
495*404b540aSrobert if (tc == POINTER_TYPE)
496*404b540aSrobert {
497*404b540aSrobert /* If T is a pointer type, v shall be an rvalue of a pointer to
498*404b540aSrobert complete class type, and the result is an rvalue of type T. */
499*404b540aSrobert
500*404b540aSrobert if (TREE_CODE (exprtype) != POINTER_TYPE)
501*404b540aSrobert {
502*404b540aSrobert errstr = "source is not a pointer";
503*404b540aSrobert goto fail;
504*404b540aSrobert }
505*404b540aSrobert if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
506*404b540aSrobert {
507*404b540aSrobert errstr = "source is not a pointer to class";
508*404b540aSrobert goto fail;
509*404b540aSrobert }
510*404b540aSrobert if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
511*404b540aSrobert {
512*404b540aSrobert errstr = "source is a pointer to incomplete type";
513*404b540aSrobert goto fail;
514*404b540aSrobert }
515*404b540aSrobert }
516*404b540aSrobert else
517*404b540aSrobert {
518*404b540aSrobert exprtype = build_reference_type (exprtype);
519*404b540aSrobert
520*404b540aSrobert /* T is a reference type, v shall be an lvalue of a complete class
521*404b540aSrobert type, and the result is an lvalue of the type referred to by T. */
522*404b540aSrobert
523*404b540aSrobert if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
524*404b540aSrobert {
525*404b540aSrobert errstr = "source is not of class type";
526*404b540aSrobert goto fail;
527*404b540aSrobert }
528*404b540aSrobert if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
529*404b540aSrobert {
530*404b540aSrobert errstr = "source is of incomplete class type";
531*404b540aSrobert goto fail;
532*404b540aSrobert }
533*404b540aSrobert
534*404b540aSrobert /* Apply trivial conversion T -> T& for dereferenced ptrs. */
535*404b540aSrobert expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
536*404b540aSrobert LOOKUP_NORMAL, NULL_TREE);
537*404b540aSrobert }
538*404b540aSrobert
539*404b540aSrobert /* The dynamic_cast operator shall not cast away constness. */
540*404b540aSrobert if (!at_least_as_qualified_p (TREE_TYPE (type),
541*404b540aSrobert TREE_TYPE (exprtype)))
542*404b540aSrobert {
543*404b540aSrobert errstr = "conversion casts away constness";
544*404b540aSrobert goto fail;
545*404b540aSrobert }
546*404b540aSrobert
547*404b540aSrobert /* If *type is an unambiguous accessible base class of *exprtype,
548*404b540aSrobert convert statically. */
549*404b540aSrobert {
550*404b540aSrobert tree binfo;
551*404b540aSrobert
552*404b540aSrobert binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
553*404b540aSrobert ba_check, NULL);
554*404b540aSrobert
555*404b540aSrobert if (binfo)
556*404b540aSrobert {
557*404b540aSrobert expr = build_base_path (PLUS_EXPR, convert_from_reference (expr),
558*404b540aSrobert binfo, 0);
559*404b540aSrobert if (TREE_CODE (exprtype) == POINTER_TYPE)
560*404b540aSrobert expr = rvalue (expr);
561*404b540aSrobert return expr;
562*404b540aSrobert }
563*404b540aSrobert }
564*404b540aSrobert
565*404b540aSrobert /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
566*404b540aSrobert if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
567*404b540aSrobert {
568*404b540aSrobert tree expr1;
569*404b540aSrobert /* if TYPE is `void *', return pointer to complete object. */
570*404b540aSrobert if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
571*404b540aSrobert {
572*404b540aSrobert /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
573*404b540aSrobert if (TREE_CODE (expr) == ADDR_EXPR
574*404b540aSrobert && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
575*404b540aSrobert && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
576*404b540aSrobert return build1 (NOP_EXPR, type, expr);
577*404b540aSrobert
578*404b540aSrobert /* Since expr is used twice below, save it. */
579*404b540aSrobert expr = save_expr (expr);
580*404b540aSrobert
581*404b540aSrobert expr1 = build_headof (expr);
582*404b540aSrobert if (TREE_TYPE (expr1) != type)
583*404b540aSrobert expr1 = build1 (NOP_EXPR, type, expr1);
584*404b540aSrobert return ifnonnull (expr, expr1);
585*404b540aSrobert }
586*404b540aSrobert else
587*404b540aSrobert {
588*404b540aSrobert tree retval;
589*404b540aSrobert tree result, td2, td3, elems;
590*404b540aSrobert tree static_type, target_type, boff;
591*404b540aSrobert
592*404b540aSrobert /* If we got here, we can't convert statically. Therefore,
593*404b540aSrobert dynamic_cast<D&>(b) (b an object) cannot succeed. */
594*404b540aSrobert if (tc == REFERENCE_TYPE)
595*404b540aSrobert {
596*404b540aSrobert if (TREE_CODE (old_expr) == VAR_DECL
597*404b540aSrobert && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
598*404b540aSrobert {
599*404b540aSrobert tree expr = throw_bad_cast ();
600*404b540aSrobert warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
601*404b540aSrobert old_expr, type);
602*404b540aSrobert /* Bash it to the expected type. */
603*404b540aSrobert TREE_TYPE (expr) = type;
604*404b540aSrobert return expr;
605*404b540aSrobert }
606*404b540aSrobert }
607*404b540aSrobert /* Ditto for dynamic_cast<D*>(&b). */
608*404b540aSrobert else if (TREE_CODE (expr) == ADDR_EXPR)
609*404b540aSrobert {
610*404b540aSrobert tree op = TREE_OPERAND (expr, 0);
611*404b540aSrobert if (TREE_CODE (op) == VAR_DECL
612*404b540aSrobert && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
613*404b540aSrobert {
614*404b540aSrobert warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
615*404b540aSrobert op, type);
616*404b540aSrobert retval = build_int_cst (type, 0);
617*404b540aSrobert return retval;
618*404b540aSrobert }
619*404b540aSrobert }
620*404b540aSrobert
621*404b540aSrobert /* Use of dynamic_cast when -fno-rtti is prohibited. */
622*404b540aSrobert if (!flag_rtti)
623*404b540aSrobert {
624*404b540aSrobert error ("%<dynamic_cast%> not permitted with -fno-rtti");
625*404b540aSrobert return error_mark_node;
626*404b540aSrobert }
627*404b540aSrobert
628*404b540aSrobert target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
629*404b540aSrobert static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
630*404b540aSrobert td2 = get_tinfo_decl (target_type);
631*404b540aSrobert mark_used (td2);
632*404b540aSrobert td2 = build_unary_op (ADDR_EXPR, td2, 0);
633*404b540aSrobert td3 = get_tinfo_decl (static_type);
634*404b540aSrobert mark_used (td3);
635*404b540aSrobert td3 = build_unary_op (ADDR_EXPR, td3, 0);
636*404b540aSrobert
637*404b540aSrobert /* Determine how T and V are related. */
638*404b540aSrobert boff = dcast_base_hint (static_type, target_type);
639*404b540aSrobert
640*404b540aSrobert /* Since expr is used twice below, save it. */
641*404b540aSrobert expr = save_expr (expr);
642*404b540aSrobert
643*404b540aSrobert expr1 = expr;
644*404b540aSrobert if (tc == REFERENCE_TYPE)
645*404b540aSrobert expr1 = build_unary_op (ADDR_EXPR, expr1, 0);
646*404b540aSrobert
647*404b540aSrobert elems = tree_cons
648*404b540aSrobert (NULL_TREE, expr1, tree_cons
649*404b540aSrobert (NULL_TREE, td3, tree_cons
650*404b540aSrobert (NULL_TREE, td2, tree_cons
651*404b540aSrobert (NULL_TREE, boff, NULL_TREE))));
652*404b540aSrobert
653*404b540aSrobert dcast_fn = dynamic_cast_node;
654*404b540aSrobert if (!dcast_fn)
655*404b540aSrobert {
656*404b540aSrobert tree tmp;
657*404b540aSrobert tree tinfo_ptr;
658*404b540aSrobert tree ns = abi_node;
659*404b540aSrobert const char *name;
660*404b540aSrobert
661*404b540aSrobert push_nested_namespace (ns);
662*404b540aSrobert tinfo_ptr = xref_tag (class_type,
663*404b540aSrobert get_identifier ("__class_type_info"),
664*404b540aSrobert /*tag_scope=*/ts_current, false);
665*404b540aSrobert
666*404b540aSrobert tinfo_ptr = build_pointer_type
667*404b540aSrobert (build_qualified_type
668*404b540aSrobert (tinfo_ptr, TYPE_QUAL_CONST));
669*404b540aSrobert name = "__dynamic_cast";
670*404b540aSrobert tmp = tree_cons
671*404b540aSrobert (NULL_TREE, const_ptr_type_node, tree_cons
672*404b540aSrobert (NULL_TREE, tinfo_ptr, tree_cons
673*404b540aSrobert (NULL_TREE, tinfo_ptr, tree_cons
674*404b540aSrobert (NULL_TREE, ptrdiff_type_node, void_list_node))));
675*404b540aSrobert tmp = build_function_type (ptr_type_node, tmp);
676*404b540aSrobert dcast_fn = build_library_fn_ptr (name, tmp);
677*404b540aSrobert DECL_IS_PURE (dcast_fn) = 1;
678*404b540aSrobert pop_nested_namespace (ns);
679*404b540aSrobert dynamic_cast_node = dcast_fn;
680*404b540aSrobert }
681*404b540aSrobert result = build_cxx_call (dcast_fn, elems);
682*404b540aSrobert
683*404b540aSrobert if (tc == REFERENCE_TYPE)
684*404b540aSrobert {
685*404b540aSrobert tree bad = throw_bad_cast ();
686*404b540aSrobert tree neq;
687*404b540aSrobert
688*404b540aSrobert result = save_expr (result);
689*404b540aSrobert neq = c_common_truthvalue_conversion (result);
690*404b540aSrobert return build3 (COND_EXPR, type, neq, result, bad);
691*404b540aSrobert }
692*404b540aSrobert
693*404b540aSrobert /* Now back to the type we want from a void*. */
694*404b540aSrobert result = cp_convert (type, result);
695*404b540aSrobert return ifnonnull (expr, result);
696*404b540aSrobert }
697*404b540aSrobert }
698*404b540aSrobert else
699*404b540aSrobert errstr = "source type is not polymorphic";
700*404b540aSrobert
701*404b540aSrobert fail:
702*404b540aSrobert error ("cannot dynamic_cast %qE (of type %q#T) to type %q#T (%s)",
703*404b540aSrobert expr, exprtype, type, errstr);
704*404b540aSrobert return error_mark_node;
705*404b540aSrobert }
706*404b540aSrobert
707*404b540aSrobert tree
build_dynamic_cast(tree type,tree expr)708*404b540aSrobert build_dynamic_cast (tree type, tree expr)
709*404b540aSrobert {
710*404b540aSrobert if (type == error_mark_node || expr == error_mark_node)
711*404b540aSrobert return error_mark_node;
712*404b540aSrobert
713*404b540aSrobert if (processing_template_decl)
714*404b540aSrobert {
715*404b540aSrobert expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
716*404b540aSrobert TREE_SIDE_EFFECTS (expr) = 1;
717*404b540aSrobert
718*404b540aSrobert return expr;
719*404b540aSrobert }
720*404b540aSrobert
721*404b540aSrobert return convert_from_reference (build_dynamic_cast_1 (type, expr));
722*404b540aSrobert }
723*404b540aSrobert
724*404b540aSrobert /* Return the runtime bit mask encoding the qualifiers of TYPE. */
725*404b540aSrobert
726*404b540aSrobert static int
qualifier_flags(tree type)727*404b540aSrobert qualifier_flags (tree type)
728*404b540aSrobert {
729*404b540aSrobert int flags = 0;
730*404b540aSrobert int quals = cp_type_quals (type);
731*404b540aSrobert
732*404b540aSrobert if (quals & TYPE_QUAL_CONST)
733*404b540aSrobert flags |= 1;
734*404b540aSrobert if (quals & TYPE_QUAL_VOLATILE)
735*404b540aSrobert flags |= 2;
736*404b540aSrobert if (quals & TYPE_QUAL_RESTRICT)
737*404b540aSrobert flags |= 4;
738*404b540aSrobert return flags;
739*404b540aSrobert }
740*404b540aSrobert
741*404b540aSrobert /* Return true, if the pointer chain TYPE ends at an incomplete type, or
742*404b540aSrobert contains a pointer to member of an incomplete class. */
743*404b540aSrobert
744*404b540aSrobert static bool
target_incomplete_p(tree type)745*404b540aSrobert target_incomplete_p (tree type)
746*404b540aSrobert {
747*404b540aSrobert while (true)
748*404b540aSrobert if (TYPE_PTRMEM_P (type))
749*404b540aSrobert {
750*404b540aSrobert if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
751*404b540aSrobert return true;
752*404b540aSrobert type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
753*404b540aSrobert }
754*404b540aSrobert else if (TREE_CODE (type) == POINTER_TYPE)
755*404b540aSrobert type = TREE_TYPE (type);
756*404b540aSrobert else
757*404b540aSrobert return !COMPLETE_OR_VOID_TYPE_P (type);
758*404b540aSrobert }
759*404b540aSrobert
760*404b540aSrobert /* Returns true if TYPE involves an incomplete class type; in that
761*404b540aSrobert case, typeinfo variables for TYPE should be emitted with internal
762*404b540aSrobert linkage. */
763*404b540aSrobert
764*404b540aSrobert static bool
involves_incomplete_p(tree type)765*404b540aSrobert involves_incomplete_p (tree type)
766*404b540aSrobert {
767*404b540aSrobert switch (TREE_CODE (type))
768*404b540aSrobert {
769*404b540aSrobert case POINTER_TYPE:
770*404b540aSrobert return target_incomplete_p (TREE_TYPE (type));
771*404b540aSrobert
772*404b540aSrobert case OFFSET_TYPE:
773*404b540aSrobert ptrmem:
774*404b540aSrobert return
775*404b540aSrobert (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type))
776*404b540aSrobert || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)));
777*404b540aSrobert
778*404b540aSrobert case RECORD_TYPE:
779*404b540aSrobert if (TYPE_PTRMEMFUNC_P (type))
780*404b540aSrobert goto ptrmem;
781*404b540aSrobert /* Fall through. */
782*404b540aSrobert case UNION_TYPE:
783*404b540aSrobert if (!COMPLETE_TYPE_P (type))
784*404b540aSrobert return true;
785*404b540aSrobert
786*404b540aSrobert default:
787*404b540aSrobert /* All other types do not involve incomplete class types. */
788*404b540aSrobert return false;
789*404b540aSrobert }
790*404b540aSrobert }
791*404b540aSrobert
792*404b540aSrobert /* Return a CONSTRUCTOR for the common part of the type_info objects. This
793*404b540aSrobert is the vtable pointer and NTBS name. The NTBS name is emitted as a
794*404b540aSrobert comdat const char array, so it becomes a unique key for the type. Generate
795*404b540aSrobert and emit that VAR_DECL here. (We can't always emit the type_info itself
796*404b540aSrobert as comdat, because of pointers to incomplete.) */
797*404b540aSrobert
798*404b540aSrobert static tree
tinfo_base_init(tinfo_s * ti,tree target)799*404b540aSrobert tinfo_base_init (tinfo_s *ti, tree target)
800*404b540aSrobert {
801*404b540aSrobert tree init = NULL_TREE;
802*404b540aSrobert tree name_decl;
803*404b540aSrobert tree vtable_ptr;
804*404b540aSrobert
805*404b540aSrobert {
806*404b540aSrobert tree name_name;
807*404b540aSrobert
808*404b540aSrobert /* Generate the NTBS array variable. */
809*404b540aSrobert tree name_type = build_cplus_array_type
810*404b540aSrobert (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
811*404b540aSrobert NULL_TREE);
812*404b540aSrobert tree name_string = tinfo_name (target);
813*404b540aSrobert
814*404b540aSrobert /* Determine the name of the variable -- and remember with which
815*404b540aSrobert type it is associated. */
816*404b540aSrobert name_name = mangle_typeinfo_string_for_type (target);
817*404b540aSrobert TREE_TYPE (name_name) = target;
818*404b540aSrobert
819*404b540aSrobert name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
820*404b540aSrobert SET_DECL_ASSEMBLER_NAME (name_decl, name_name);
821*404b540aSrobert DECL_ARTIFICIAL (name_decl) = 1;
822*404b540aSrobert DECL_IGNORED_P (name_decl) = 1;
823*404b540aSrobert TREE_READONLY (name_decl) = 1;
824*404b540aSrobert TREE_STATIC (name_decl) = 1;
825*404b540aSrobert DECL_EXTERNAL (name_decl) = 0;
826*404b540aSrobert DECL_TINFO_P (name_decl) = 1;
827*404b540aSrobert set_linkage_according_to_type (target, name_decl);
828*404b540aSrobert import_export_decl (name_decl);
829*404b540aSrobert DECL_INITIAL (name_decl) = name_string;
830*404b540aSrobert mark_used (name_decl);
831*404b540aSrobert pushdecl_top_level_and_finish (name_decl, name_string);
832*404b540aSrobert }
833*404b540aSrobert
834*404b540aSrobert vtable_ptr = ti->vtable;
835*404b540aSrobert if (!vtable_ptr)
836*404b540aSrobert {
837*404b540aSrobert tree real_type;
838*404b540aSrobert push_nested_namespace (abi_node);
839*404b540aSrobert real_type = xref_tag (class_type, ti->name,
840*404b540aSrobert /*tag_scope=*/ts_current, false);
841*404b540aSrobert pop_nested_namespace (abi_node);
842*404b540aSrobert
843*404b540aSrobert if (!COMPLETE_TYPE_P (real_type))
844*404b540aSrobert {
845*404b540aSrobert /* We never saw a definition of this type, so we need to
846*404b540aSrobert tell the compiler that this is an exported class, as
847*404b540aSrobert indeed all of the __*_type_info classes are. */
848*404b540aSrobert SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
849*404b540aSrobert CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
850*404b540aSrobert }
851*404b540aSrobert
852*404b540aSrobert vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
853*404b540aSrobert vtable_ptr = build_unary_op (ADDR_EXPR, vtable_ptr, 0);
854*404b540aSrobert
855*404b540aSrobert /* We need to point into the middle of the vtable. */
856*404b540aSrobert vtable_ptr = build2
857*404b540aSrobert (PLUS_EXPR, TREE_TYPE (vtable_ptr), vtable_ptr,
858*404b540aSrobert size_binop (MULT_EXPR,
859*404b540aSrobert size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
860*404b540aSrobert TYPE_SIZE_UNIT (vtable_entry_type)));
861*404b540aSrobert
862*404b540aSrobert ti->vtable = vtable_ptr;
863*404b540aSrobert }
864*404b540aSrobert
865*404b540aSrobert init = tree_cons (NULL_TREE, vtable_ptr, init);
866*404b540aSrobert
867*404b540aSrobert init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
868*404b540aSrobert
869*404b540aSrobert init = build_constructor_from_list (NULL_TREE, nreverse (init));
870*404b540aSrobert TREE_CONSTANT (init) = 1;
871*404b540aSrobert TREE_INVARIANT (init) = 1;
872*404b540aSrobert TREE_STATIC (init) = 1;
873*404b540aSrobert init = tree_cons (NULL_TREE, init, NULL_TREE);
874*404b540aSrobert
875*404b540aSrobert return init;
876*404b540aSrobert }
877*404b540aSrobert
878*404b540aSrobert /* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the
879*404b540aSrobert information about the particular type_info derivation, which adds no
880*404b540aSrobert additional fields to the type_info base. */
881*404b540aSrobert
882*404b540aSrobert static tree
generic_initializer(tinfo_s * ti,tree target)883*404b540aSrobert generic_initializer (tinfo_s *ti, tree target)
884*404b540aSrobert {
885*404b540aSrobert tree init = tinfo_base_init (ti, target);
886*404b540aSrobert
887*404b540aSrobert init = build_constructor_from_list (NULL_TREE, init);
888*404b540aSrobert TREE_CONSTANT (init) = 1;
889*404b540aSrobert TREE_INVARIANT (init) = 1;
890*404b540aSrobert TREE_STATIC (init) = 1;
891*404b540aSrobert return init;
892*404b540aSrobert }
893*404b540aSrobert
894*404b540aSrobert /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
895*404b540aSrobert TI provides information about the particular type_info derivation,
896*404b540aSrobert which adds target type and qualifier flags members to the type_info base. */
897*404b540aSrobert
898*404b540aSrobert static tree
ptr_initializer(tinfo_s * ti,tree target)899*404b540aSrobert ptr_initializer (tinfo_s *ti, tree target)
900*404b540aSrobert {
901*404b540aSrobert tree init = tinfo_base_init (ti, target);
902*404b540aSrobert tree to = TREE_TYPE (target);
903*404b540aSrobert int flags = qualifier_flags (to);
904*404b540aSrobert bool incomplete = target_incomplete_p (to);
905*404b540aSrobert
906*404b540aSrobert if (incomplete)
907*404b540aSrobert flags |= 8;
908*404b540aSrobert init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
909*404b540aSrobert init = tree_cons (NULL_TREE,
910*404b540aSrobert get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
911*404b540aSrobert init);
912*404b540aSrobert
913*404b540aSrobert init = build_constructor_from_list (NULL_TREE, nreverse (init));
914*404b540aSrobert TREE_CONSTANT (init) = 1;
915*404b540aSrobert TREE_INVARIANT (init) = 1;
916*404b540aSrobert TREE_STATIC (init) = 1;
917*404b540aSrobert return init;
918*404b540aSrobert }
919*404b540aSrobert
920*404b540aSrobert /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
921*404b540aSrobert TI provides information about the particular type_info derivation,
922*404b540aSrobert which adds class, target type and qualifier flags members to the type_info
923*404b540aSrobert base. */
924*404b540aSrobert
925*404b540aSrobert static tree
ptm_initializer(tinfo_s * ti,tree target)926*404b540aSrobert ptm_initializer (tinfo_s *ti, tree target)
927*404b540aSrobert {
928*404b540aSrobert tree init = tinfo_base_init (ti, target);
929*404b540aSrobert tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
930*404b540aSrobert tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
931*404b540aSrobert int flags = qualifier_flags (to);
932*404b540aSrobert bool incomplete = target_incomplete_p (to);
933*404b540aSrobert
934*404b540aSrobert if (incomplete)
935*404b540aSrobert flags |= 0x8;
936*404b540aSrobert if (!COMPLETE_TYPE_P (klass))
937*404b540aSrobert flags |= 0x10;
938*404b540aSrobert init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
939*404b540aSrobert init = tree_cons (NULL_TREE,
940*404b540aSrobert get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
941*404b540aSrobert init);
942*404b540aSrobert init = tree_cons (NULL_TREE,
943*404b540aSrobert get_tinfo_ptr (klass),
944*404b540aSrobert init);
945*404b540aSrobert
946*404b540aSrobert init = build_constructor_from_list (NULL_TREE, nreverse (init));
947*404b540aSrobert TREE_CONSTANT (init) = 1;
948*404b540aSrobert TREE_INVARIANT (init) = 1;
949*404b540aSrobert TREE_STATIC (init) = 1;
950*404b540aSrobert return init;
951*404b540aSrobert }
952*404b540aSrobert
953*404b540aSrobert /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
954*404b540aSrobert TI provides information about the particular __class_type_info derivation,
955*404b540aSrobert which adds hint flags and TRAIL initializers to the type_info base. */
956*404b540aSrobert
957*404b540aSrobert static tree
class_initializer(tinfo_s * ti,tree target,tree trail)958*404b540aSrobert class_initializer (tinfo_s *ti, tree target, tree trail)
959*404b540aSrobert {
960*404b540aSrobert tree init = tinfo_base_init (ti, target);
961*404b540aSrobert
962*404b540aSrobert TREE_CHAIN (init) = trail;
963*404b540aSrobert init = build_constructor_from_list (NULL_TREE, init);
964*404b540aSrobert TREE_CONSTANT (init) = 1;
965*404b540aSrobert TREE_INVARIANT (init) = 1;
966*404b540aSrobert TREE_STATIC (init) = 1;
967*404b540aSrobert return init;
968*404b540aSrobert }
969*404b540aSrobert
970*404b540aSrobert /* Returns true if the typeinfo for type should be placed in
971*404b540aSrobert the runtime library. */
972*404b540aSrobert
973*404b540aSrobert static bool
typeinfo_in_lib_p(tree type)974*404b540aSrobert typeinfo_in_lib_p (tree type)
975*404b540aSrobert {
976*404b540aSrobert /* The typeinfo objects for `T*' and `const T*' are in the runtime
977*404b540aSrobert library for simple types T. */
978*404b540aSrobert if (TREE_CODE (type) == POINTER_TYPE
979*404b540aSrobert && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
980*404b540aSrobert || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
981*404b540aSrobert type = TREE_TYPE (type);
982*404b540aSrobert
983*404b540aSrobert switch (TREE_CODE (type))
984*404b540aSrobert {
985*404b540aSrobert case INTEGER_TYPE:
986*404b540aSrobert case BOOLEAN_TYPE:
987*404b540aSrobert case REAL_TYPE:
988*404b540aSrobert case VOID_TYPE:
989*404b540aSrobert return true;
990*404b540aSrobert
991*404b540aSrobert default:
992*404b540aSrobert return false;
993*404b540aSrobert }
994*404b540aSrobert }
995*404b540aSrobert
996*404b540aSrobert /* Generate the initializer for the type info describing TYPE. TK_INDEX is
997*404b540aSrobert the index of the descriptor in the tinfo_desc vector. */
998*404b540aSrobert
999*404b540aSrobert static tree
get_pseudo_ti_init(tree type,unsigned tk_index)1000*404b540aSrobert get_pseudo_ti_init (tree type, unsigned tk_index)
1001*404b540aSrobert {
1002*404b540aSrobert tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1003*404b540aSrobert
1004*404b540aSrobert gcc_assert (at_eof);
1005*404b540aSrobert switch (tk_index)
1006*404b540aSrobert {
1007*404b540aSrobert case TK_POINTER_MEMBER_TYPE:
1008*404b540aSrobert return ptm_initializer (ti, type);
1009*404b540aSrobert
1010*404b540aSrobert case TK_POINTER_TYPE:
1011*404b540aSrobert return ptr_initializer (ti, type);
1012*404b540aSrobert
1013*404b540aSrobert case TK_BUILTIN_TYPE:
1014*404b540aSrobert case TK_ENUMERAL_TYPE:
1015*404b540aSrobert case TK_FUNCTION_TYPE:
1016*404b540aSrobert case TK_ARRAY_TYPE:
1017*404b540aSrobert return generic_initializer (ti, type);
1018*404b540aSrobert
1019*404b540aSrobert case TK_CLASS_TYPE:
1020*404b540aSrobert return class_initializer (ti, type, NULL_TREE);
1021*404b540aSrobert
1022*404b540aSrobert case TK_SI_CLASS_TYPE:
1023*404b540aSrobert {
1024*404b540aSrobert tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
1025*404b540aSrobert tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1026*404b540aSrobert tree base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1027*404b540aSrobert
1028*404b540aSrobert /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1029*404b540aSrobert ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1030*404b540aSrobert return class_initializer (ti, type, base_inits);
1031*404b540aSrobert }
1032*404b540aSrobert
1033*404b540aSrobert default:
1034*404b540aSrobert {
1035*404b540aSrobert int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0)
1036*404b540aSrobert | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1));
1037*404b540aSrobert tree binfo = TYPE_BINFO (type);
1038*404b540aSrobert int nbases = BINFO_N_BASE_BINFOS (binfo);
1039*404b540aSrobert VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1040*404b540aSrobert tree base_inits = NULL_TREE;
1041*404b540aSrobert int ix;
1042*404b540aSrobert
1043*404b540aSrobert gcc_assert (tk_index >= TK_FIXED);
1044*404b540aSrobert
1045*404b540aSrobert /* Generate the base information initializer. */
1046*404b540aSrobert for (ix = nbases; ix--;)
1047*404b540aSrobert {
1048*404b540aSrobert tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
1049*404b540aSrobert tree base_init = NULL_TREE;
1050*404b540aSrobert int flags = 0;
1051*404b540aSrobert tree tinfo;
1052*404b540aSrobert tree offset;
1053*404b540aSrobert
1054*404b540aSrobert if (VEC_index (tree, base_accesses, ix) == access_public_node)
1055*404b540aSrobert flags |= 2;
1056*404b540aSrobert tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1057*404b540aSrobert if (BINFO_VIRTUAL_P (base_binfo))
1058*404b540aSrobert {
1059*404b540aSrobert /* We store the vtable offset at which the virtual
1060*404b540aSrobert base offset can be found. */
1061*404b540aSrobert offset = BINFO_VPTR_FIELD (base_binfo);
1062*404b540aSrobert offset = convert (sizetype, offset);
1063*404b540aSrobert flags |= 1;
1064*404b540aSrobert }
1065*404b540aSrobert else
1066*404b540aSrobert offset = BINFO_OFFSET (base_binfo);
1067*404b540aSrobert
1068*404b540aSrobert /* Combine offset and flags into one field. */
1069*404b540aSrobert offset = cp_build_binary_op (LSHIFT_EXPR, offset,
1070*404b540aSrobert build_int_cst (NULL_TREE, 8));
1071*404b540aSrobert offset = cp_build_binary_op (BIT_IOR_EXPR, offset,
1072*404b540aSrobert build_int_cst (NULL_TREE, flags));
1073*404b540aSrobert base_init = tree_cons (NULL_TREE, offset, base_init);
1074*404b540aSrobert base_init = tree_cons (NULL_TREE, tinfo, base_init);
1075*404b540aSrobert base_init = build_constructor_from_list (NULL_TREE, base_init);
1076*404b540aSrobert base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1077*404b540aSrobert }
1078*404b540aSrobert base_inits = build_constructor_from_list (NULL_TREE, base_inits);
1079*404b540aSrobert base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1080*404b540aSrobert /* Prepend the number of bases. */
1081*404b540aSrobert base_inits = tree_cons (NULL_TREE,
1082*404b540aSrobert build_int_cst (NULL_TREE, nbases),
1083*404b540aSrobert base_inits);
1084*404b540aSrobert /* Prepend the hint flags. */
1085*404b540aSrobert base_inits = tree_cons (NULL_TREE,
1086*404b540aSrobert build_int_cst (NULL_TREE, hint),
1087*404b540aSrobert base_inits);
1088*404b540aSrobert
1089*404b540aSrobert /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1090*404b540aSrobert ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1091*404b540aSrobert return class_initializer (ti, type, base_inits);
1092*404b540aSrobert }
1093*404b540aSrobert }
1094*404b540aSrobert }
1095*404b540aSrobert
1096*404b540aSrobert /* Generate the RECORD_TYPE containing the data layout of a type_info
1097*404b540aSrobert derivative as used by the runtime. This layout must be consistent with
1098*404b540aSrobert that defined in the runtime support. Also generate the VAR_DECL for the
1099*404b540aSrobert type's vtable. We explicitly manage the vtable member, and name it for
1100*404b540aSrobert real type as used in the runtime. The RECORD type has a different name,
1101*404b540aSrobert to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1102*404b540aSrobert is the generated type and TINFO_VTABLE_NAME is the name of the
1103*404b540aSrobert vtable. We have to delay generating the VAR_DECL of the vtable
1104*404b540aSrobert until the end of the translation, when we'll have seen the library
1105*404b540aSrobert definition, if there was one.
1106*404b540aSrobert
1107*404b540aSrobert REAL_NAME is the runtime's name of the type. Trailing arguments are
1108*404b540aSrobert additional FIELD_DECL's for the structure. The final argument must be
1109*404b540aSrobert NULL. */
1110*404b540aSrobert
1111*404b540aSrobert static void
create_pseudo_type_info(int tk,const char * real_name,...)1112*404b540aSrobert create_pseudo_type_info (int tk, const char *real_name, ...)
1113*404b540aSrobert {
1114*404b540aSrobert tinfo_s *ti;
1115*404b540aSrobert tree pseudo_type;
1116*404b540aSrobert char *pseudo_name;
1117*404b540aSrobert tree fields;
1118*404b540aSrobert tree field_decl;
1119*404b540aSrobert va_list ap;
1120*404b540aSrobert
1121*404b540aSrobert va_start (ap, real_name);
1122*404b540aSrobert
1123*404b540aSrobert /* Generate the pseudo type name. */
1124*404b540aSrobert pseudo_name = (char *) alloca (strlen (real_name) + 30);
1125*404b540aSrobert strcpy (pseudo_name, real_name);
1126*404b540aSrobert strcat (pseudo_name, "_pseudo");
1127*404b540aSrobert if (tk >= TK_FIXED)
1128*404b540aSrobert sprintf (pseudo_name + strlen (pseudo_name), "%d", tk - TK_FIXED);
1129*404b540aSrobert
1130*404b540aSrobert /* First field is the pseudo type_info base class. */
1131*404b540aSrobert fields = build_decl (FIELD_DECL, NULL_TREE,
1132*404b540aSrobert VEC_index (tinfo_s, tinfo_descs,
1133*404b540aSrobert TK_TYPE_INFO_TYPE)->type);
1134*404b540aSrobert
1135*404b540aSrobert /* Now add the derived fields. */
1136*404b540aSrobert while ((field_decl = va_arg (ap, tree)))
1137*404b540aSrobert {
1138*404b540aSrobert TREE_CHAIN (field_decl) = fields;
1139*404b540aSrobert fields = field_decl;
1140*404b540aSrobert }
1141*404b540aSrobert
1142*404b540aSrobert /* Create the pseudo type. */
1143*404b540aSrobert pseudo_type = make_aggr_type (RECORD_TYPE);
1144*404b540aSrobert finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
1145*404b540aSrobert CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
1146*404b540aSrobert
1147*404b540aSrobert ti = VEC_index (tinfo_s, tinfo_descs, tk);
1148*404b540aSrobert ti->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1149*404b540aSrobert ti->name = get_identifier (real_name);
1150*404b540aSrobert ti->vtable = NULL_TREE;
1151*404b540aSrobert
1152*404b540aSrobert /* Pretend this is public so determine_visibility doesn't give vtables
1153*404b540aSrobert internal linkage. */
1154*404b540aSrobert TREE_PUBLIC (TYPE_MAIN_DECL (ti->type)) = 1;
1155*404b540aSrobert
1156*404b540aSrobert va_end (ap);
1157*404b540aSrobert }
1158*404b540aSrobert
1159*404b540aSrobert /* Return the index of a pseudo type info type node used to describe
1160*404b540aSrobert TYPE. TYPE must be a complete type (or cv void), except at the end
1161*404b540aSrobert of the translation unit. */
1162*404b540aSrobert
1163*404b540aSrobert static unsigned
get_pseudo_ti_index(tree type)1164*404b540aSrobert get_pseudo_ti_index (tree type)
1165*404b540aSrobert {
1166*404b540aSrobert unsigned ix;
1167*404b540aSrobert
1168*404b540aSrobert switch (TREE_CODE (type))
1169*404b540aSrobert {
1170*404b540aSrobert case OFFSET_TYPE:
1171*404b540aSrobert ix = TK_POINTER_MEMBER_TYPE;
1172*404b540aSrobert break;
1173*404b540aSrobert
1174*404b540aSrobert case POINTER_TYPE:
1175*404b540aSrobert ix = TK_POINTER_TYPE;
1176*404b540aSrobert break;
1177*404b540aSrobert
1178*404b540aSrobert case ENUMERAL_TYPE:
1179*404b540aSrobert ix = TK_ENUMERAL_TYPE;
1180*404b540aSrobert break;
1181*404b540aSrobert
1182*404b540aSrobert case FUNCTION_TYPE:
1183*404b540aSrobert ix = TK_FUNCTION_TYPE;
1184*404b540aSrobert break;
1185*404b540aSrobert
1186*404b540aSrobert case ARRAY_TYPE:
1187*404b540aSrobert ix = TK_ARRAY_TYPE;
1188*404b540aSrobert break;
1189*404b540aSrobert
1190*404b540aSrobert case UNION_TYPE:
1191*404b540aSrobert case RECORD_TYPE:
1192*404b540aSrobert if (TYPE_PTRMEMFUNC_P (type))
1193*404b540aSrobert {
1194*404b540aSrobert ix = TK_POINTER_MEMBER_TYPE;
1195*404b540aSrobert break;
1196*404b540aSrobert }
1197*404b540aSrobert else if (!COMPLETE_TYPE_P (type))
1198*404b540aSrobert {
1199*404b540aSrobert if (!at_eof)
1200*404b540aSrobert cxx_incomplete_type_error (NULL_TREE, type);
1201*404b540aSrobert ix = TK_CLASS_TYPE;
1202*404b540aSrobert break;
1203*404b540aSrobert }
1204*404b540aSrobert else if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
1205*404b540aSrobert {
1206*404b540aSrobert ix = TK_CLASS_TYPE;
1207*404b540aSrobert break;
1208*404b540aSrobert }
1209*404b540aSrobert else
1210*404b540aSrobert {
1211*404b540aSrobert tree binfo = TYPE_BINFO (type);
1212*404b540aSrobert VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1213*404b540aSrobert tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
1214*404b540aSrobert int num_bases = BINFO_N_BASE_BINFOS (binfo);
1215*404b540aSrobert
1216*404b540aSrobert if (num_bases == 1
1217*404b540aSrobert && VEC_index (tree, base_accesses, 0) == access_public_node
1218*404b540aSrobert && !BINFO_VIRTUAL_P (base_binfo)
1219*404b540aSrobert && integer_zerop (BINFO_OFFSET (base_binfo)))
1220*404b540aSrobert {
1221*404b540aSrobert /* single non-virtual public. */
1222*404b540aSrobert ix = TK_SI_CLASS_TYPE;
1223*404b540aSrobert break;
1224*404b540aSrobert }
1225*404b540aSrobert else
1226*404b540aSrobert {
1227*404b540aSrobert tinfo_s *ti;
1228*404b540aSrobert tree array_domain, base_array;
1229*404b540aSrobert
1230*404b540aSrobert ix = TK_FIXED + num_bases;
1231*404b540aSrobert if (VEC_length (tinfo_s, tinfo_descs) <= ix)
1232*404b540aSrobert {
1233*404b540aSrobert /* too short, extend. */
1234*404b540aSrobert unsigned len = VEC_length (tinfo_s, tinfo_descs);
1235*404b540aSrobert
1236*404b540aSrobert VEC_safe_grow (tinfo_s, gc, tinfo_descs, ix + 1);
1237*404b540aSrobert while (VEC_iterate (tinfo_s, tinfo_descs, len++, ti))
1238*404b540aSrobert ti->type = ti->vtable = ti->name = NULL_TREE;
1239*404b540aSrobert }
1240*404b540aSrobert else if (VEC_index (tinfo_s, tinfo_descs, ix)->type)
1241*404b540aSrobert /* already created. */
1242*404b540aSrobert break;
1243*404b540aSrobert
1244*404b540aSrobert /* Create the array of __base_class_type_info entries.
1245*404b540aSrobert G++ 3.2 allocated an array that had one too many
1246*404b540aSrobert entries, and then filled that extra entries with
1247*404b540aSrobert zeros. */
1248*404b540aSrobert if (abi_version_at_least (2))
1249*404b540aSrobert array_domain = build_index_type (size_int (num_bases - 1));
1250*404b540aSrobert else
1251*404b540aSrobert array_domain = build_index_type (size_int (num_bases));
1252*404b540aSrobert base_array =
1253*404b540aSrobert build_array_type (VEC_index (tinfo_s, tinfo_descs,
1254*404b540aSrobert TK_BASE_TYPE)->type,
1255*404b540aSrobert array_domain);
1256*404b540aSrobert
1257*404b540aSrobert push_nested_namespace (abi_node);
1258*404b540aSrobert create_pseudo_type_info
1259*404b540aSrobert (ix, "__vmi_class_type_info",
1260*404b540aSrobert build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1261*404b540aSrobert build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1262*404b540aSrobert build_decl (FIELD_DECL, NULL_TREE, base_array),
1263*404b540aSrobert NULL);
1264*404b540aSrobert pop_nested_namespace (abi_node);
1265*404b540aSrobert break;
1266*404b540aSrobert }
1267*404b540aSrobert }
1268*404b540aSrobert default:
1269*404b540aSrobert ix = TK_BUILTIN_TYPE;
1270*404b540aSrobert break;
1271*404b540aSrobert }
1272*404b540aSrobert return ix;
1273*404b540aSrobert }
1274*404b540aSrobert
1275*404b540aSrobert /* Make sure the required builtin types exist for generating the type_info
1276*404b540aSrobert variable definitions. */
1277*404b540aSrobert
1278*404b540aSrobert static void
create_tinfo_types(void)1279*404b540aSrobert create_tinfo_types (void)
1280*404b540aSrobert {
1281*404b540aSrobert tinfo_s *ti;
1282*404b540aSrobert
1283*404b540aSrobert gcc_assert (!tinfo_descs);
1284*404b540aSrobert
1285*404b540aSrobert VEC_safe_grow (tinfo_s, gc, tinfo_descs, TK_FIXED);
1286*404b540aSrobert
1287*404b540aSrobert push_nested_namespace (abi_node);
1288*404b540aSrobert
1289*404b540aSrobert /* Create the internal type_info structure. This is used as a base for
1290*404b540aSrobert the other structures. */
1291*404b540aSrobert {
1292*404b540aSrobert tree field, fields;
1293*404b540aSrobert
1294*404b540aSrobert field = build_decl (FIELD_DECL, NULL_TREE, const_ptr_type_node);
1295*404b540aSrobert fields = field;
1296*404b540aSrobert
1297*404b540aSrobert field = build_decl (FIELD_DECL, NULL_TREE, const_string_type_node);
1298*404b540aSrobert TREE_CHAIN (field) = fields;
1299*404b540aSrobert fields = field;
1300*404b540aSrobert
1301*404b540aSrobert ti = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE);
1302*404b540aSrobert ti->type = make_aggr_type (RECORD_TYPE);
1303*404b540aSrobert ti->vtable = NULL_TREE;
1304*404b540aSrobert ti->name = NULL_TREE;
1305*404b540aSrobert finish_builtin_struct (ti->type, "__type_info_pseudo",
1306*404b540aSrobert fields, NULL_TREE);
1307*404b540aSrobert TYPE_HAS_CONSTRUCTOR (ti->type) = 1;
1308*404b540aSrobert }
1309*404b540aSrobert
1310*404b540aSrobert /* Fundamental type_info */
1311*404b540aSrobert create_pseudo_type_info (TK_BUILTIN_TYPE, "__fundamental_type_info", NULL);
1312*404b540aSrobert
1313*404b540aSrobert /* Array, function and enum type_info. No additional fields. */
1314*404b540aSrobert create_pseudo_type_info (TK_ARRAY_TYPE, "__array_type_info", NULL);
1315*404b540aSrobert create_pseudo_type_info (TK_FUNCTION_TYPE, "__function_type_info", NULL);
1316*404b540aSrobert create_pseudo_type_info (TK_ENUMERAL_TYPE, "__enum_type_info", NULL);
1317*404b540aSrobert
1318*404b540aSrobert /* Class type_info. No additional fields. */
1319*404b540aSrobert create_pseudo_type_info (TK_CLASS_TYPE, "__class_type_info", NULL);
1320*404b540aSrobert
1321*404b540aSrobert /* Single public non-virtual base class. Add pointer to base class.
1322*404b540aSrobert This is really a descendant of __class_type_info. */
1323*404b540aSrobert create_pseudo_type_info (TK_SI_CLASS_TYPE, "__si_class_type_info",
1324*404b540aSrobert build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1325*404b540aSrobert NULL);
1326*404b540aSrobert
1327*404b540aSrobert /* Base class internal helper. Pointer to base type, offset to base,
1328*404b540aSrobert flags. */
1329*404b540aSrobert {
1330*404b540aSrobert tree field, fields;
1331*404b540aSrobert
1332*404b540aSrobert field = build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type);
1333*404b540aSrobert fields = field;
1334*404b540aSrobert
1335*404b540aSrobert field = build_decl (FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1336*404b540aSrobert TREE_CHAIN (field) = fields;
1337*404b540aSrobert fields = field;
1338*404b540aSrobert
1339*404b540aSrobert ti = VEC_index (tinfo_s, tinfo_descs, TK_BASE_TYPE);
1340*404b540aSrobert
1341*404b540aSrobert ti->type = make_aggr_type (RECORD_TYPE);
1342*404b540aSrobert ti->vtable = NULL_TREE;
1343*404b540aSrobert ti->name = NULL_TREE;
1344*404b540aSrobert finish_builtin_struct (ti->type, "__base_class_type_info_pseudo",
1345*404b540aSrobert fields, NULL_TREE);
1346*404b540aSrobert TYPE_HAS_CONSTRUCTOR (ti->type) = 1;
1347*404b540aSrobert }
1348*404b540aSrobert
1349*404b540aSrobert /* Pointer type_info. Adds two fields, qualification mask
1350*404b540aSrobert and pointer to the pointed to type. This is really a descendant of
1351*404b540aSrobert __pbase_type_info. */
1352*404b540aSrobert create_pseudo_type_info (TK_POINTER_TYPE, "__pointer_type_info",
1353*404b540aSrobert build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1354*404b540aSrobert build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1355*404b540aSrobert NULL);
1356*404b540aSrobert
1357*404b540aSrobert /* Pointer to member data type_info. Add qualifications flags,
1358*404b540aSrobert pointer to the member's type info and pointer to the class.
1359*404b540aSrobert This is really a descendant of __pbase_type_info. */
1360*404b540aSrobert create_pseudo_type_info (TK_POINTER_MEMBER_TYPE,
1361*404b540aSrobert "__pointer_to_member_type_info",
1362*404b540aSrobert build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1363*404b540aSrobert build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1364*404b540aSrobert build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1365*404b540aSrobert NULL);
1366*404b540aSrobert
1367*404b540aSrobert pop_nested_namespace (abi_node);
1368*404b540aSrobert }
1369*404b540aSrobert
1370*404b540aSrobert /* Emit the type_info descriptors which are guaranteed to be in the runtime
1371*404b540aSrobert support. Generating them here guarantees consistency with the other
1372*404b540aSrobert structures. We use the following heuristic to determine when the runtime
1373*404b540aSrobert is being generated. If std::__fundamental_type_info is defined, and its
1374*404b540aSrobert destructor is defined, then the runtime is being built. */
1375*404b540aSrobert
1376*404b540aSrobert void
emit_support_tinfos(void)1377*404b540aSrobert emit_support_tinfos (void)
1378*404b540aSrobert {
1379*404b540aSrobert static tree *const fundamentals[] =
1380*404b540aSrobert {
1381*404b540aSrobert &void_type_node,
1382*404b540aSrobert &boolean_type_node,
1383*404b540aSrobert &wchar_type_node,
1384*404b540aSrobert &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1385*404b540aSrobert &short_integer_type_node, &short_unsigned_type_node,
1386*404b540aSrobert &integer_type_node, &unsigned_type_node,
1387*404b540aSrobert &long_integer_type_node, &long_unsigned_type_node,
1388*404b540aSrobert &long_long_integer_type_node, &long_long_unsigned_type_node,
1389*404b540aSrobert &float_type_node, &double_type_node, &long_double_type_node,
1390*404b540aSrobert 0
1391*404b540aSrobert };
1392*404b540aSrobert int ix;
1393*404b540aSrobert tree bltn_type, dtor;
1394*404b540aSrobert
1395*404b540aSrobert push_nested_namespace (abi_node);
1396*404b540aSrobert bltn_type = xref_tag (class_type,
1397*404b540aSrobert get_identifier ("__fundamental_type_info"),
1398*404b540aSrobert /*tag_scope=*/ts_current, false);
1399*404b540aSrobert pop_nested_namespace (abi_node);
1400*404b540aSrobert if (!COMPLETE_TYPE_P (bltn_type))
1401*404b540aSrobert return;
1402*404b540aSrobert dtor = CLASSTYPE_DESTRUCTORS (bltn_type);
1403*404b540aSrobert if (!dtor || DECL_EXTERNAL (dtor))
1404*404b540aSrobert return;
1405*404b540aSrobert doing_runtime = 1;
1406*404b540aSrobert for (ix = 0; fundamentals[ix]; ix++)
1407*404b540aSrobert {
1408*404b540aSrobert tree bltn = *fundamentals[ix];
1409*404b540aSrobert tree types[3];
1410*404b540aSrobert int i;
1411*404b540aSrobert
1412*404b540aSrobert types[0] = bltn;
1413*404b540aSrobert types[1] = build_pointer_type (bltn);
1414*404b540aSrobert types[2] = build_pointer_type (build_qualified_type (bltn,
1415*404b540aSrobert TYPE_QUAL_CONST));
1416*404b540aSrobert
1417*404b540aSrobert for (i = 0; i < 3; ++i)
1418*404b540aSrobert {
1419*404b540aSrobert tree tinfo;
1420*404b540aSrobert
1421*404b540aSrobert tinfo = get_tinfo_decl (types[i]);
1422*404b540aSrobert TREE_USED (tinfo) = 1;
1423*404b540aSrobert mark_needed (tinfo);
1424*404b540aSrobert /* The C++ ABI requires that these objects be COMDAT. But,
1425*404b540aSrobert On systems without weak symbols, initialized COMDAT
1426*404b540aSrobert objects are emitted with internal linkage. (See
1427*404b540aSrobert comdat_linkage for details.) Since we want these objects
1428*404b540aSrobert to have external linkage so that copies do not have to be
1429*404b540aSrobert emitted in code outside the runtime library, we make them
1430*404b540aSrobert non-COMDAT here. */
1431*404b540aSrobert if (!flag_weak)
1432*404b540aSrobert {
1433*404b540aSrobert gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
1434*404b540aSrobert DECL_INTERFACE_KNOWN (tinfo) = 1;
1435*404b540aSrobert }
1436*404b540aSrobert }
1437*404b540aSrobert }
1438*404b540aSrobert }
1439*404b540aSrobert
1440*404b540aSrobert /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1441*404b540aSrobert tinfo decl. Determine whether it needs emitting, and if so
1442*404b540aSrobert generate the initializer. */
1443*404b540aSrobert
1444*404b540aSrobert bool
emit_tinfo_decl(tree decl)1445*404b540aSrobert emit_tinfo_decl (tree decl)
1446*404b540aSrobert {
1447*404b540aSrobert tree type = TREE_TYPE (DECL_NAME (decl));
1448*404b540aSrobert int in_library = typeinfo_in_lib_p (type);
1449*404b540aSrobert
1450*404b540aSrobert gcc_assert (DECL_TINFO_P (decl));
1451*404b540aSrobert
1452*404b540aSrobert if (in_library)
1453*404b540aSrobert {
1454*404b540aSrobert if (doing_runtime)
1455*404b540aSrobert DECL_EXTERNAL (decl) = 0;
1456*404b540aSrobert else
1457*404b540aSrobert {
1458*404b540aSrobert /* If we're not in the runtime, then DECL (which is already
1459*404b540aSrobert DECL_EXTERNAL) will not be defined here. */
1460*404b540aSrobert DECL_INTERFACE_KNOWN (decl) = 1;
1461*404b540aSrobert return false;
1462*404b540aSrobert }
1463*404b540aSrobert }
1464*404b540aSrobert else if (involves_incomplete_p (type))
1465*404b540aSrobert {
1466*404b540aSrobert if (!decl_needed_p (decl))
1467*404b540aSrobert return false;
1468*404b540aSrobert /* If TYPE involves an incomplete class type, then the typeinfo
1469*404b540aSrobert object will be emitted with internal linkage. There is no
1470*404b540aSrobert way to know whether or not types are incomplete until the end
1471*404b540aSrobert of the compilation, so this determination must be deferred
1472*404b540aSrobert until this point. */
1473*404b540aSrobert TREE_PUBLIC (decl) = 0;
1474*404b540aSrobert DECL_EXTERNAL (decl) = 0;
1475*404b540aSrobert DECL_INTERFACE_KNOWN (decl) = 1;
1476*404b540aSrobert }
1477*404b540aSrobert
1478*404b540aSrobert import_export_decl (decl);
1479*404b540aSrobert if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
1480*404b540aSrobert {
1481*404b540aSrobert tree init;
1482*404b540aSrobert
1483*404b540aSrobert DECL_EXTERNAL (decl) = 0;
1484*404b540aSrobert init = get_pseudo_ti_init (type, get_pseudo_ti_index (type));
1485*404b540aSrobert DECL_INITIAL (decl) = init;
1486*404b540aSrobert mark_used (decl);
1487*404b540aSrobert finish_decl (decl, init, NULL_TREE);
1488*404b540aSrobert return true;
1489*404b540aSrobert }
1490*404b540aSrobert else
1491*404b540aSrobert return false;
1492*404b540aSrobert }
1493*404b540aSrobert
1494*404b540aSrobert #include "gt-cp-rtti.h"
1495