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