annotate gcc/cp/rtti.c @ 145:1830386684a0

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