Branch data Line data Source code
1 : : /* RunTime Type Identification
2 : : Copyright (C) 1995-2024 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 : 101073 : init_rtti_processing (void)
148 : : {
149 : 101073 : vec_alloc (unemitted_tinfo_decls, 124);
150 : :
151 : 101073 : create_tinfo_types ();
152 : 101073 : }
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 : 690 : build_headof (tree exp)
161 : : {
162 : 690 : tree type = TREE_TYPE (exp);
163 : 690 : tree offset;
164 : 690 : tree index;
165 : :
166 : 690 : gcc_assert (TYPE_PTR_P (type));
167 : 690 : type = TREE_TYPE (type);
168 : :
169 : 690 : if (!TYPE_POLYMORPHIC_P (type))
170 : : return exp;
171 : :
172 : : /* We use this a couple of times below, protect it. */
173 : 661 : exp = save_expr (exp);
174 : :
175 : : /* The offset-to-top field is at index -2 from the vptr. */
176 : 1322 : index = build_int_cst (NULL_TREE,
177 : 661 : -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
178 : :
179 : 661 : offset = build_vtbl_ref (cp_build_fold_indirect_ref (exp),
180 : : index);
181 : :
182 : 661 : cp_build_qualified_type (ptr_type_node,
183 : 661 : cp_type_quals (TREE_TYPE (exp)));
184 : 661 : return fold_build_pointer_plus (exp, offset);
185 : : }
186 : :
187 : : /* Get a bad_cast node for the program to throw...
188 : :
189 : : See libstdc++/exception.cc for __throw_bad_cast */
190 : :
191 : : static tree
192 : 112 : throw_bad_cast (void)
193 : : {
194 : 112 : static tree fn;
195 : 112 : if (!fn)
196 : : {
197 : 58 : tree name = get_identifier ("__cxa_bad_cast");
198 : 58 : fn = get_global_binding (name);
199 : 58 : if (!fn)
200 : 58 : fn = push_throw_library_fn
201 : 58 : (name, build_function_type_list (ptr_type_node, NULL_TREE));
202 : : }
203 : :
204 : 112 : return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
205 : : }
206 : :
207 : : /* Return an expression for "__cxa_bad_typeid()". The expression
208 : : returned is an lvalue of type "const std::type_info". */
209 : :
210 : : static tree
211 : 155 : throw_bad_typeid (void)
212 : : {
213 : 155 : static tree fn;
214 : 155 : if (!fn)
215 : : {
216 : 52 : tree name = get_identifier ("__cxa_bad_typeid");
217 : 52 : fn = get_global_binding (name);
218 : 52 : if (!fn)
219 : : {
220 : 52 : tree t = build_reference_type (const_type_info_type_node);
221 : 52 : t = build_function_type_list (t, NULL_TREE);
222 : 52 : fn = push_throw_library_fn (name, t);
223 : : }
224 : : }
225 : :
226 : 155 : return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
227 : : }
228 : :
229 : : /* const type_info*. */
230 : :
231 : : inline tree
232 : 83262 : type_info_ptr_type ()
233 : : {
234 : 83082 : return build_pointer_type (const_type_info_type_node);
235 : : }
236 : :
237 : : /* Return a pointer to a type_info object describing TYPE, suitably
238 : : cast to the language defined type (for typeid) or void (for building
239 : : up the descriptors). */
240 : :
241 : : static tree
242 : 364163 : get_tinfo_ptr (tree type, bool voidp = false)
243 : : {
244 : 364163 : tree decl = get_tinfo_decl (type);
245 : 364163 : mark_used (decl);
246 : :
247 : 364163 : tree ptype = voidp ? const_ptr_type_node : type_info_ptr_type ();
248 : 364163 : return build_nop (ptype, build_address (decl));
249 : : }
250 : : static inline tree
251 : 281081 : get_void_tinfo_ptr (tree type)
252 : : {
253 : 281081 : return get_tinfo_ptr (type, true);
254 : : }
255 : :
256 : : /* Return an lvalue expression whose type is "const std::type_info"
257 : : and whose value indicates the type of the expression EXP. If EXP
258 : : is a reference to a polymorphic class, return the dynamic type;
259 : : otherwise return the static type of the expression. */
260 : :
261 : : static tree
262 : 857 : get_tinfo_decl_dynamic (tree exp, tsubst_flags_t complain)
263 : : {
264 : 857 : tree type;
265 : 857 : tree t;
266 : :
267 : 857 : if (error_operand_p (exp))
268 : 4 : return error_mark_node;
269 : :
270 : 853 : exp = resolve_nondeduced_context (exp, complain);
271 : :
272 : : /* Peel back references, so they match. */
273 : 853 : type = non_reference (unlowered_expr_type (exp));
274 : :
275 : : /* Peel off cv qualifiers. */
276 : 853 : type = cv_unqualified (type);
277 : :
278 : : /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
279 : 853 : if (CLASS_TYPE_P (type) || type == unknown_type_node
280 : 1295 : || type == init_list_type_node)
281 : 411 : type = complete_type_or_maybe_complain (type, exp, complain);
282 : :
283 : 853 : if (!type)
284 : 16 : return error_mark_node;
285 : :
286 : : /* If exp is a reference to polymorphic type, get the real type_info. */
287 : 837 : if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
288 : : {
289 : : /* build reference to type_info from vtable. */
290 : 180 : tree index;
291 : :
292 : : /* The RTTI information is at index -1. */
293 : 360 : index = build_int_cst (NULL_TREE,
294 : 180 : -1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
295 : 180 : t = build_vtbl_ref (exp, index);
296 : 180 : t = convert (type_info_ptr_type (), t);
297 : : }
298 : : else
299 : : /* Otherwise return the type_info for the static type of the expr. */
300 : 657 : t = get_tinfo_ptr (type);
301 : :
302 : 837 : return cp_build_fold_indirect_ref (t);
303 : : }
304 : :
305 : : static bool
306 : 201776 : typeid_ok_p (void)
307 : : {
308 : 201776 : if (! flag_rtti)
309 : : {
310 : 4 : error ("cannot use %<typeid%> with %<-fno-rtti%>");
311 : 4 : return false;
312 : : }
313 : :
314 : 201772 : if (!const_type_info_type_node)
315 : : {
316 : 11491 : tree name = get_identifier ("type_info");
317 : 11491 : tree decl = lookup_qualified_name (std_node, name);
318 : 11491 : if (TREE_CODE (decl) != TYPE_DECL)
319 : : {
320 : 4 : gcc_rich_location richloc (input_location);
321 : 4 : maybe_add_include_fixit (&richloc, "<typeinfo>", false);
322 : 4 : error_at (&richloc,
323 : : "must %<#include <typeinfo>%> before using"
324 : : " %<typeid%>");
325 : :
326 : 4 : return false;
327 : 4 : }
328 : 11487 : const_type_info_type_node
329 : 11487 : = cp_build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_CONST);
330 : : }
331 : :
332 : 201768 : tree pseudo = TYPE_MAIN_VARIANT (get_tinfo_desc (TK_TYPE_INFO_TYPE)->type);
333 : 201768 : tree real = TYPE_MAIN_VARIANT (const_type_info_type_node);
334 : :
335 : : /* Make sure abi::__type_info_pseudo has the same alias set
336 : : as std::type_info. */
337 : 201768 : if (! TYPE_ALIAS_SET_KNOWN_P (pseudo))
338 : 11487 : TYPE_ALIAS_SET (pseudo) = get_alias_set (real);
339 : : else
340 : 190281 : gcc_assert (TYPE_ALIAS_SET (pseudo) == get_alias_set (real));
341 : :
342 : : return true;
343 : : }
344 : :
345 : : /* Return an expression for "typeid(EXP)". The expression returned is
346 : : an lvalue of type "const std::type_info". */
347 : :
348 : : tree
349 : 2434 : build_typeid (tree exp, tsubst_flags_t complain)
350 : : {
351 : 2434 : tree cond = NULL_TREE, initial_expr = exp;
352 : 2434 : int nonnull = 0;
353 : :
354 : 2434 : if (exp == error_mark_node || !typeid_ok_p ())
355 : 28 : return error_mark_node;
356 : :
357 : 2406 : if (processing_template_decl)
358 : 1549 : return build_min (TYPEID_EXPR, const_type_info_type_node, exp);
359 : :
360 : 857 : if (TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
361 : 210 : && ! resolves_to_fixed_type_p (exp, &nonnull)
362 : 1037 : && ! nonnull)
363 : : {
364 : : /* So we need to look into the vtable of the type of exp.
365 : : Make sure it isn't a null lvalue. */
366 : 155 : exp = cp_build_addr_expr (exp, complain);
367 : 155 : exp = save_expr (exp);
368 : 155 : cond = cp_convert (boolean_type_node, exp, complain);
369 : 155 : exp = cp_build_fold_indirect_ref (exp);
370 : : }
371 : :
372 : 857 : exp = get_tinfo_decl_dynamic (exp, complain);
373 : :
374 : 857 : if (exp == error_mark_node)
375 : : return error_mark_node;
376 : :
377 : 829 : if (cond)
378 : : {
379 : 155 : tree bad = throw_bad_typeid ();
380 : :
381 : 155 : exp = build3 (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
382 : : }
383 : : else
384 : 674 : mark_type_use (initial_expr);
385 : :
386 : : return exp;
387 : : }
388 : :
389 : : /* Generate the NTBS name of a type. If MARK_PRIVATE, put a '*' in front so that
390 : : comparisons will be done by pointer rather than string comparison. */
391 : : static tree
392 : 347348 : tinfo_name (tree type, bool mark_private)
393 : : {
394 : 347348 : const char *name;
395 : 347348 : int length;
396 : 347348 : tree name_string;
397 : :
398 : 347348 : name = mangle_type_string (type);
399 : 347348 : length = strlen (name);
400 : :
401 : 347348 : if (mark_private)
402 : : {
403 : : /* Inject '*' at beginning of name to force pointer comparison. */
404 : 963 : char* buf = (char*) XALLOCAVEC (char, length + 2);
405 : 963 : buf[0] = '*';
406 : 963 : memcpy (buf + 1, name, length + 1);
407 : 963 : name_string = build_string (length + 2, buf);
408 : : }
409 : : else
410 : 346385 : name_string = build_string (length + 1, name);
411 : :
412 : 347348 : return fix_string_type (name_string);
413 : : }
414 : :
415 : : /* Return a VAR_DECL for the internal ABI defined type_info object for
416 : : TYPE. You must arrange that the decl is mark_used, if actually use
417 : : it --- decls in vtables are only used if the vtable is output. */
418 : :
419 : : tree
420 : 2906146 : get_tinfo_decl (tree type)
421 : : {
422 : 2906146 : if (variably_modified_type_p (type, /*fn=*/NULL_TREE))
423 : : {
424 : 12 : error ("cannot create type information for type %qT because "
425 : : "it involves types of variable size",
426 : : type);
427 : 12 : return error_mark_node;
428 : : }
429 : :
430 : 2906134 : if (TREE_CODE (type) == METHOD_TYPE)
431 : 52 : type = build_function_type (TREE_TYPE (type),
432 : 52 : TREE_CHAIN (TYPE_ARG_TYPES (type)));
433 : :
434 : 2906134 : return get_tinfo_decl_direct (type, NULL, -1);
435 : : }
436 : :
437 : : /* Get or create a tinfo VAR_DECL directly from the provided information.
438 : : The caller must have already checked it is valid to do so. */
439 : :
440 : : tree
441 : 2911210 : get_tinfo_decl_direct (tree type, tree name, int pseudo_ix)
442 : : {
443 : : /* For a class type, the variable is cached in the type node
444 : : itself. */
445 : 2911210 : tree d = NULL_TREE;
446 : :
447 : 2911210 : gcc_checking_assert (TREE_CODE (type) != METHOD_TYPE);
448 : :
449 : 2911210 : if (CLASS_TYPE_P (type))
450 : 2892063 : d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type));
451 : :
452 : 2911210 : if (!name)
453 : 2906134 : name = mangle_typeinfo_for_type (type);
454 : :
455 : 2911210 : if (!CLASS_TYPE_P (type) || TYPE_TRANSPARENT_AGGR (type))
456 : 19196 : d = get_global_binding (name);
457 : :
458 : 2911210 : if (!d)
459 : : {
460 : : /* Create it. */
461 : 1665417 : if (pseudo_ix < 0)
462 : 1663499 : pseudo_ix = get_pseudo_ti_index (type);
463 : :
464 : 1665417 : const tinfo_s *ti = get_tinfo_desc (pseudo_ix);
465 : :
466 : 1665417 : d = build_lang_decl (VAR_DECL, name, ti->type);
467 : 1665417 : SET_DECL_ASSEMBLER_NAME (d, name);
468 : : /* Remember the type it is for. */
469 : 1665417 : TREE_TYPE (name) = type;
470 : 1665417 : DECL_TINFO_P (d) = 1;
471 : 1665417 : DECL_ARTIFICIAL (d) = 1;
472 : 1665417 : DECL_IGNORED_P (d) = 1;
473 : 1665417 : TREE_READONLY (d) = 1;
474 : 1665417 : TREE_STATIC (d) = 1;
475 : : /* Tell equal_address_to that different tinfo decls never
476 : : overlap. */
477 : 1665417 : if (vec_safe_is_empty (unemitted_tinfo_decls))
478 : 21585 : DECL_ATTRIBUTES (d)
479 : 43170 : = build_tree_list (get_identifier ("non overlapping"),
480 : : NULL_TREE);
481 : : else
482 : 3287664 : DECL_ATTRIBUTES (d)
483 : 1643832 : = 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 : 1665417 : DECL_EXTERNAL (d) = 1;
488 : 1665417 : DECL_NOT_REALLY_EXTERN (d) = 1;
489 : 1665417 : set_linkage_according_to_type (type, d);
490 : :
491 : 1665417 : d = pushdecl_top_level_and_finish (d, NULL_TREE);
492 : 1665417 : if (CLASS_TYPE_P (type))
493 : 1651830 : CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d;
494 : :
495 : : /* Add decl to the global array of tinfo decls. */
496 : 1665417 : vec_safe_push (unemitted_tinfo_decls, d);
497 : : }
498 : :
499 : 2911210 : return d;
500 : : }
501 : :
502 : : /* Return the type_info object for TYPE. */
503 : :
504 : : tree
505 : 199374 : get_typeid (tree type, tsubst_flags_t complain)
506 : : {
507 : 199374 : if (type == error_mark_node || !typeid_ok_p ())
508 : 12 : return error_mark_node;
509 : :
510 : 199362 : if (processing_template_decl)
511 : 116933 : 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 : 82429 : type = non_reference (type);
517 : :
518 : : /* This is not one of the uses of a qualified function type in 8.3.5. */
519 : 82429 : if (TREE_CODE (type) == FUNCTION_TYPE
520 : 82429 : && (type_memfn_quals (type) != TYPE_UNQUALIFIED
521 : 10 : || type_memfn_rqual (type) != REF_QUAL_NONE))
522 : : {
523 : 4 : if (complain & tf_error)
524 : 4 : error ("%<typeid%> of qualified function type %qT", type);
525 : 4 : 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 : 82425 : type = cv_unqualified (type);
531 : :
532 : : /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
533 : 82425 : if (CLASS_TYPE_P (type) || type == unknown_type_node
534 : 92462 : || type == init_list_type_node)
535 : 72388 : type = complete_type_or_maybe_complain (type, NULL_TREE, complain);
536 : :
537 : 82425 : if (!type)
538 : 0 : return error_mark_node;
539 : :
540 : 82425 : 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 : 9865 : build_if_nonnull (tree test, tree result, tsubst_flags_t complain)
548 : : {
549 : 9865 : tree null_ptr = cp_convert (TREE_TYPE (test), nullptr_node, complain);
550 : 9865 : 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 : 9865 : suppress_warning (cond, OPT_Wnonnull);
555 : :
556 : 9865 : null_ptr = cp_convert (TREE_TYPE (result), nullptr_node, complain);
557 : 9865 : 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 : 9865 : suppress_warning (cond, OPT_Wnonnull);
562 : 9865 : 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 : 8251 : build_dynamic_cast_1 (location_t loc, tree type, tree expr,
570 : : tsubst_flags_t complain)
571 : : {
572 : 8251 : enum tree_code tc = TREE_CODE (type);
573 : 8251 : tree exprtype;
574 : 8251 : tree dcast_fn;
575 : 8251 : tree old_expr = expr;
576 : 8251 : const char *errstr = NULL;
577 : :
578 : : /* Save casted types in the function's used types hash table. */
579 : 8251 : 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 : 8251 : switch (tc)
584 : : {
585 : 7995 : case POINTER_TYPE:
586 : 7995 : if (VOID_TYPE_P (TREE_TYPE (type)))
587 : : break;
588 : : /* Fall through. */
589 : 7578 : case REFERENCE_TYPE:
590 : 7578 : 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 : 7578 : 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 : 16 : default:
603 : 16 : errstr = _("target is not pointer or reference");
604 : 16 : goto fail;
605 : : }
606 : :
607 : 8235 : if (tc == POINTER_TYPE)
608 : : {
609 : 7995 : expr = decay_conversion (expr, complain);
610 : 7995 : 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 : 7995 : expr = mark_rvalue_use (expr);
616 : :
617 : 7995 : if (!TYPE_PTR_P (exprtype))
618 : : {
619 : 0 : errstr = _("source is not a pointer");
620 : 0 : goto fail;
621 : : }
622 : 7995 : if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
623 : : {
624 : 4 : errstr = _("source is not a pointer to class");
625 : 4 : goto fail;
626 : : }
627 : 7991 : 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 : 240 : expr = mark_lvalue_use (expr);
636 : 240 : 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 : 240 : if (! MAYBE_CLASS_TYPE_P (exprtype))
641 : : {
642 : 4 : errstr = _("source is not of class type");
643 : 4 : goto fail;
644 : : }
645 : 236 : if (!COMPLETE_TYPE_P (complete_type (exprtype)))
646 : : {
647 : 0 : errstr = _("source is of incomplete class type");
648 : 0 : goto fail;
649 : : }
650 : :
651 : 236 : exprtype = cp_build_reference_type (exprtype, !lvalue_p (expr));
652 : : }
653 : :
654 : : /* The dynamic_cast operator shall not cast away constness. */
655 : 8227 : if (!at_least_as_qualified_p (TREE_TYPE (type),
656 : 8227 : TREE_TYPE (exprtype)))
657 : : {
658 : 16 : errstr = _("conversion casts away constness");
659 : 16 : goto fail;
660 : : }
661 : :
662 : : /* If *type is an unambiguous accessible base class of *exprtype,
663 : : convert statically. */
664 : 8211 : {
665 : 8211 : tree binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
666 : : ba_check, NULL, complain);
667 : 8211 : if (binfo)
668 : 486 : return build_static_cast (loc, type, expr, complain);
669 : : }
670 : :
671 : : /* Apply trivial conversion T -> T& for dereferenced ptrs. */
672 : 7725 : if (tc == REFERENCE_TYPE)
673 : 112 : 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 : 7725 : if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
678 : : {
679 : 7721 : tree expr1;
680 : : /* if TYPE is `void *', return pointer to complete object. */
681 : 7721 : if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
682 : : {
683 : : /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
684 : 645 : if (TREE_CODE (expr) == ADDR_EXPR
685 : 0 : && VAR_P (TREE_OPERAND (expr, 0))
686 : 645 : && 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 : 645 : expr = save_expr (expr);
691 : :
692 : 645 : expr1 = build_headof (expr);
693 : 645 : if (TREE_TYPE (expr1) != type)
694 : 645 : expr1 = build1 (NOP_EXPR, type, expr1);
695 : 645 : return build_if_nonnull (expr, expr1, complain);
696 : : }
697 : : else
698 : : {
699 : 7076 : tree retval;
700 : 7076 : tree result, td2, td3;
701 : 7076 : tree elems[4];
702 : 7076 : 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 : 7076 : if (tc == REFERENCE_TYPE)
707 : : {
708 : 112 : if (VAR_P (old_expr)
709 : 112 : && 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 : 6964 : else if (TREE_CODE (expr) == ADDR_EXPR)
723 : : {
724 : 4 : tree op = TREE_OPERAND (expr, 0);
725 : 4 : if (VAR_P (op)
726 : 4 : && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
727 : : {
728 : 4 : if (complain & tf_warning)
729 : 4 : warning_at (loc, 0,
730 : : "%<dynamic_cast<%#T>(%#D)%> can never succeed",
731 : : type, op);
732 : 4 : retval = build_int_cst (type, 0);
733 : 4 : return retval;
734 : : }
735 : : }
736 : :
737 : : /* Use of dynamic_cast when -fno-rtti is prohibited. */
738 : 7072 : if (!flag_rtti)
739 : : {
740 : 4 : if (complain & tf_error)
741 : 4 : error_at (loc,
742 : : "%<dynamic_cast%> not permitted with %<-fno-rtti%>");
743 : 4 : return error_mark_node;
744 : : }
745 : :
746 : 7068 : target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
747 : 7068 : static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
748 : 7068 : td2 = get_tinfo_decl (target_type);
749 : 7068 : if (!mark_used (td2, complain) && !(complain & tf_error))
750 : 0 : return error_mark_node;
751 : 7068 : td2 = cp_build_addr_expr (td2, complain);
752 : 7068 : td3 = get_tinfo_decl (static_type);
753 : 7068 : if (!mark_used (td3, complain) && !(complain & tf_error))
754 : 0 : return error_mark_node;
755 : 7068 : td3 = cp_build_addr_expr (td3, complain);
756 : :
757 : : /* Determine how T and V are related. */
758 : 7068 : boff = dcast_base_hint (static_type, target_type);
759 : :
760 : : /* Since expr is used twice below, save it. */
761 : 7068 : expr = save_expr (expr);
762 : :
763 : 7068 : expr1 = expr;
764 : 7068 : if (tc == REFERENCE_TYPE)
765 : 112 : expr1 = cp_build_addr_expr (expr1, complain);
766 : :
767 : 7068 : elems[0] = expr1;
768 : 7068 : elems[1] = td3;
769 : 7068 : elems[2] = td2;
770 : 7068 : elems[3] = boff;
771 : :
772 : 7068 : dcast_fn = dynamic_cast_node;
773 : 7068 : if (!dcast_fn)
774 : : {
775 : 3438 : unsigned flags = push_abi_namespace ();
776 : 3438 : tree tinfo_ptr = xref_tag (class_type,
777 : : get_identifier ("__class_type_info"));
778 : 3438 : tinfo_ptr = cp_build_qualified_type (tinfo_ptr, TYPE_QUAL_CONST);
779 : 3438 : tinfo_ptr = build_pointer_type (tinfo_ptr);
780 : :
781 : 3438 : const char *fn_name = "__dynamic_cast";
782 : : /* void *() (void const *, __class_type_info const *,
783 : : __class_type_info const *, ptrdiff_t) */
784 : 3438 : tree fn_type = (build_function_type_list
785 : 3438 : (ptr_type_node, const_ptr_type_node,
786 : : tinfo_ptr, tinfo_ptr, ptrdiff_type_node,
787 : : NULL_TREE));
788 : 3438 : dcast_fn = (build_library_fn_ptr
789 : 3438 : (fn_name, fn_type, ECF_LEAF | ECF_PURE | ECF_NOTHROW));
790 : : /* As with __cxa_atexit in get_atexit_node. */
791 : 3438 : DECL_CONTEXT (dcast_fn) = FROB_CONTEXT (current_namespace);
792 : 3438 : DECL_SOURCE_LOCATION (dcast_fn) = BUILTINS_LOCATION;
793 : 3438 : dcast_fn = pushdecl (dcast_fn, /*hiding=*/true);
794 : 3438 : pop_abi_namespace (flags);
795 : 3438 : dynamic_cast_node = dcast_fn;
796 : : }
797 : 7068 : result = build_cxx_call (dcast_fn, 4, elems, complain);
798 : 7068 : SET_EXPR_LOCATION (result, loc);
799 : :
800 : 7068 : if (tc == REFERENCE_TYPE)
801 : : {
802 : 112 : tree bad = throw_bad_cast ();
803 : 112 : tree neq;
804 : :
805 : 112 : result = save_expr (result);
806 : 112 : neq = cp_truthvalue_conversion (result, complain);
807 : 112 : return cp_convert (type,
808 : 112 : build3 (COND_EXPR, TREE_TYPE (result),
809 : 112 : neq, result, bad), complain);
810 : : }
811 : :
812 : : /* Now back to the type we want from a void*. */
813 : 6956 : result = cp_convert (type, result, complain);
814 : 6956 : return build_if_nonnull (expr, result, complain);
815 : : }
816 : : }
817 : : else
818 : 4 : errstr = _("source type is not polymorphic");
819 : :
820 : 44 : fail:
821 : 44 : if (complain & tf_error)
822 : 24 : error_at (loc, "cannot %<dynamic_cast%> %qE (of type %q#T) "
823 : : "to type %q#T (%s)",
824 : 24 : old_expr, TREE_TYPE (old_expr), type, errstr);
825 : 44 : return error_mark_node;
826 : : }
827 : :
828 : : tree
829 : 54261 : build_dynamic_cast (location_t loc, tree type, tree expr,
830 : : tsubst_flags_t complain)
831 : : {
832 : 54261 : tree r;
833 : :
834 : 54261 : if (type == error_mark_node || expr == error_mark_node)
835 : : return error_mark_node;
836 : :
837 : 54257 : if (processing_template_decl)
838 : : {
839 : 46006 : expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
840 : 46006 : TREE_SIDE_EFFECTS (expr) = 1;
841 : 46006 : r = convert_from_reference (expr);
842 : 46006 : protected_set_expr_location (r, loc);
843 : 46006 : return r;
844 : : }
845 : :
846 : 8251 : r = convert_from_reference (build_dynamic_cast_1 (loc, type, expr,
847 : : complain));
848 : 8251 : if (r != error_mark_node)
849 : 8197 : maybe_warn_about_useless_cast (loc, type, expr, complain);
850 : 8251 : protected_set_expr_location (r, loc);
851 : 8251 : return r;
852 : : }
853 : :
854 : : /* Return the runtime bit mask encoding the qualifiers of TYPE. */
855 : :
856 : : static int
857 : 1260 : qualifier_flags (tree type)
858 : : {
859 : 1260 : int flags = 0;
860 : 1260 : int quals = cp_type_quals (type);
861 : :
862 : 1260 : if (quals & TYPE_QUAL_CONST)
863 : 270 : flags |= 1;
864 : 1260 : if (quals & TYPE_QUAL_VOLATILE)
865 : 8 : flags |= 2;
866 : 1260 : if (quals & TYPE_QUAL_RESTRICT)
867 : 8 : flags |= 4;
868 : 1260 : return flags;
869 : : }
870 : :
871 : : /* Return true, if the pointer chain TYPE ends at an incomplete type, or
872 : : contains a pointer to member of an incomplete class. */
873 : :
874 : : static bool
875 : 2116 : target_incomplete_p (tree type)
876 : : {
877 : 2348 : while (true)
878 : 2348 : if (TYPE_PTRDATAMEM_P (type))
879 : : {
880 : 20 : if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
881 : : return true;
882 : 12 : type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
883 : : }
884 : 2328 : else if (TYPE_PTR_P (type))
885 : 220 : type = TREE_TYPE (type);
886 : : else
887 : 2108 : return !COMPLETE_OR_VOID_TYPE_P (type);
888 : : }
889 : :
890 : : /* Returns true if TYPE involves an incomplete class type; in that
891 : : case, typeinfo variables for TYPE should be emitted with internal
892 : : linkage. */
893 : :
894 : : static bool
895 : 5395123 : involves_incomplete_p (tree type)
896 : : {
897 : 5395123 : switch (TREE_CODE (type))
898 : : {
899 : 750 : case POINTER_TYPE:
900 : 750 : return target_incomplete_p (TREE_TYPE (type));
901 : :
902 : 106 : case OFFSET_TYPE:
903 : 106 : ptrmem:
904 : 106 : return
905 : 106 : (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type))
906 : 106 : || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)));
907 : :
908 : 5394138 : case RECORD_TYPE:
909 : 5394138 : if (TYPE_PTRMEMFUNC_P (type))
910 : 52 : goto ptrmem;
911 : : /* Fall through. */
912 : 5394086 : case UNION_TYPE:
913 : 5394086 : if (!COMPLETE_TYPE_P (type))
914 : : return true;
915 : : /* Fall through. */
916 : : default:
917 : : /* All other types do not involve incomplete class types. */
918 : : return false;
919 : : }
920 : : }
921 : :
922 : : /* Return a CONSTRUCTOR for the common part of the type_info objects. This
923 : : is the vtable pointer and NTBS name. The NTBS name is emitted as a
924 : : comdat const char array, so it becomes a unique key for the type. Generate
925 : : and emit that VAR_DECL here. (We can't always emit the type_info itself
926 : : as comdat, because of pointers to incomplete.) */
927 : :
928 : : static tree
929 : 347348 : tinfo_base_init (tinfo_s *ti, tree target)
930 : : {
931 : 347348 : tree init;
932 : 347348 : tree name_decl;
933 : 347348 : tree vtable_ptr;
934 : 347348 : vec<constructor_elt, va_gc> *v;
935 : :
936 : 347348 : {
937 : 347348 : tree name_name, name_string;
938 : :
939 : : /* Generate the NTBS array variable. */
940 : 347348 : tree name_type = build_cplus_array_type
941 : 347348 : (cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST),
942 : : NULL_TREE);
943 : :
944 : : /* Determine the name of the variable -- and remember with which
945 : : type it is associated. */
946 : 347348 : name_name = mangle_typeinfo_string_for_type (target);
947 : 347348 : TREE_TYPE (name_name) = target;
948 : :
949 : 347348 : name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
950 : 347348 : SET_DECL_ASSEMBLER_NAME (name_decl, name_name);
951 : 347348 : DECL_ARTIFICIAL (name_decl) = 1;
952 : 347348 : DECL_IGNORED_P (name_decl) = 1;
953 : 347348 : TREE_READONLY (name_decl) = 1;
954 : 347348 : TREE_STATIC (name_decl) = 1;
955 : 347348 : DECL_EXTERNAL (name_decl) = 0;
956 : 347348 : DECL_TINFO_P (name_decl) = 1;
957 : 347348 : set_linkage_according_to_type (target, name_decl);
958 : 347348 : import_export_decl (name_decl);
959 : 347348 : name_string = tinfo_name (target, !TREE_PUBLIC (name_decl));
960 : 347348 : DECL_INITIAL (name_decl) = name_string;
961 : 347348 : mark_used (name_decl);
962 : 347348 : pushdecl_top_level_and_finish (name_decl, name_string);
963 : : }
964 : :
965 : 347348 : vtable_ptr = ti->vtable;
966 : 347348 : if (!vtable_ptr)
967 : : {
968 : 50327 : int flags = push_abi_namespace ();
969 : 50327 : tree real_type = xref_tag (class_type, ti->name);
970 : 50327 : tree real_decl = TYPE_NAME (real_type);
971 : 50327 : DECL_SOURCE_LOCATION (real_decl) = BUILTINS_LOCATION;
972 : 50327 : pop_abi_namespace (flags);
973 : :
974 : 50327 : if (!COMPLETE_TYPE_P (real_type))
975 : : {
976 : : /* We never saw a definition of this type, so we need to
977 : : tell the compiler that this is an exported class, as
978 : : indeed all of the __*_type_info classes are. */
979 : 49822 : SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
980 : 49822 : CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
981 : : }
982 : :
983 : 50327 : vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
984 : 50327 : vtable_ptr = cp_build_addr_expr (vtable_ptr, tf_warning_or_error);
985 : :
986 : : /* We need to point into the middle of the vtable. */
987 : 50327 : vtable_ptr = fold_build_pointer_plus
988 : : (vtable_ptr,
989 : : size_binop (MULT_EXPR,
990 : : size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
991 : : TYPE_SIZE_UNIT (vtable_entry_type)));
992 : :
993 : 50327 : ti->vtable = vtable_ptr;
994 : : }
995 : :
996 : 347348 : vec_alloc (v, 2);
997 : 347348 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, vtable_ptr);
998 : 347348 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
999 : : decay_conversion (name_decl, tf_warning_or_error));
1000 : :
1001 : 347348 : init = build_constructor (init_list_type_node, v);
1002 : 347348 : TREE_CONSTANT (init) = 1;
1003 : 347348 : TREE_STATIC (init) = 1;
1004 : :
1005 : 347348 : return init;
1006 : : }
1007 : :
1008 : : /* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the
1009 : : information about the particular type_info derivation, which adds no
1010 : : additional fields to the type_info base. */
1011 : :
1012 : : static tree
1013 : 383 : generic_initializer (tinfo_s *ti, tree target)
1014 : : {
1015 : 383 : tree init = tinfo_base_init (ti, target);
1016 : :
1017 : 383 : init = build_constructor_single (init_list_type_node, NULL_TREE, init);
1018 : 383 : TREE_CONSTANT (init) = 1;
1019 : 383 : TREE_STATIC (init) = 1;
1020 : 383 : return init;
1021 : : }
1022 : :
1023 : : /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
1024 : : TI provides information about the particular type_info derivation,
1025 : : which adds target type and qualifier flags members to the type_info base. */
1026 : :
1027 : : static tree
1028 : 1154 : ptr_initializer (tinfo_s *ti, tree target)
1029 : : {
1030 : 1154 : tree init = tinfo_base_init (ti, target);
1031 : 1154 : tree to = TREE_TYPE (target);
1032 : 1154 : int flags = qualifier_flags (to);
1033 : 1154 : bool incomplete = target_incomplete_p (to);
1034 : 1154 : vec<constructor_elt, va_gc> *v;
1035 : 1154 : vec_alloc (v, 3);
1036 : :
1037 : 1154 : if (incomplete)
1038 : 83 : flags |= 8;
1039 : 1154 : if (tx_safe_fn_type_p (to))
1040 : : {
1041 : 2 : flags |= 0x20;
1042 : 2 : to = tx_unsafe_fn_variant (to);
1043 : : }
1044 : 1154 : if (flag_noexcept_type
1045 : 691 : && FUNC_OR_METHOD_TYPE_P (to)
1046 : 1223 : && TYPE_NOTHROW_P (to))
1047 : : {
1048 : 6 : flags |= 0x40;
1049 : 6 : to = build_exception_variant (to, NULL_TREE);
1050 : : }
1051 : 1154 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
1052 : 1154 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, flags));
1053 : 1154 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
1054 : : get_void_tinfo_ptr (TYPE_MAIN_VARIANT (to)));
1055 : :
1056 : 1154 : init = build_constructor (init_list_type_node, v);
1057 : 1154 : TREE_CONSTANT (init) = 1;
1058 : 1154 : TREE_STATIC (init) = 1;
1059 : 1154 : return init;
1060 : : }
1061 : :
1062 : : /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
1063 : : TI provides information about the particular type_info derivation,
1064 : : which adds class, target type and qualifier flags members to the type_info
1065 : : base. */
1066 : :
1067 : : static tree
1068 : 106 : ptm_initializer (tinfo_s *ti, tree target)
1069 : : {
1070 : 106 : tree init = tinfo_base_init (ti, target);
1071 : 106 : tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
1072 : 106 : tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
1073 : 106 : int flags = qualifier_flags (to);
1074 : 106 : bool incomplete = target_incomplete_p (to);
1075 : 106 : vec<constructor_elt, va_gc> *v;
1076 : 106 : vec_alloc (v, 4);
1077 : :
1078 : 106 : if (incomplete)
1079 : 8 : flags |= 0x8;
1080 : 106 : if (!COMPLETE_TYPE_P (klass))
1081 : 8 : flags |= 0x10;
1082 : 106 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
1083 : 106 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, flags));
1084 : 106 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
1085 : : get_void_tinfo_ptr (TYPE_MAIN_VARIANT (to)));
1086 : 106 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, get_void_tinfo_ptr (klass));
1087 : :
1088 : 106 : init = build_constructor (init_list_type_node, v);
1089 : 106 : TREE_CONSTANT (init) = 1;
1090 : 106 : TREE_STATIC (init) = 1;
1091 : 106 : return init;
1092 : : }
1093 : :
1094 : : /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
1095 : : TI provides information about the particular __class_type_info derivation,
1096 : : which adds hint flags and N extra initializers to the type_info base. */
1097 : :
1098 : : static tree
1099 : 345705 : class_initializer (tinfo_s *ti, tree target, unsigned n, ...)
1100 : : {
1101 : 345705 : tree init = tinfo_base_init (ti, target);
1102 : 345705 : va_list extra_inits;
1103 : 345705 : unsigned i;
1104 : 345705 : vec<constructor_elt, va_gc> *v;
1105 : 345705 : vec_alloc (v, n+1);
1106 : :
1107 : 345705 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
1108 : 345705 : va_start (extra_inits, n);
1109 : 676528 : for (i = 0; i < n; i++)
1110 : 330823 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, va_arg (extra_inits, tree));
1111 : 345705 : va_end (extra_inits);
1112 : :
1113 : 345705 : init = build_constructor (init_list_type_node, v);
1114 : 345705 : TREE_CONSTANT (init) = 1;
1115 : 345705 : TREE_STATIC (init) = 1;
1116 : 345705 : return init;
1117 : : }
1118 : :
1119 : : /* Returns true if the typeinfo for type should be placed in
1120 : : the runtime library. */
1121 : :
1122 : : static bool
1123 : 5436983 : typeinfo_in_lib_p (tree type)
1124 : : {
1125 : : /* The typeinfo objects for `T*' and `const T*' are in the runtime
1126 : : library for simple types T. */
1127 : 5436983 : if (TYPE_PTR_P (type)
1128 : 5436983 : && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
1129 : 1310 : || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
1130 : 1948 : type = TREE_TYPE (type);
1131 : :
1132 : 5436983 : switch (TREE_CODE (type))
1133 : : {
1134 : : case INTEGER_TYPE:
1135 : : case BOOLEAN_TYPE:
1136 : : case REAL_TYPE:
1137 : : case VOID_TYPE:
1138 : : case NULLPTR_TYPE:
1139 : : return true;
1140 : :
1141 : 5395123 : case LANG_TYPE:
1142 : : /* fall through. */
1143 : :
1144 : 5395123 : default:
1145 : 5395123 : return false;
1146 : : }
1147 : : }
1148 : :
1149 : : /* Generate the initializer for the type info describing TYPE. TK_INDEX is
1150 : : the index of the descriptor in the tinfo_desc vector. */
1151 : :
1152 : : static tree
1153 : 347348 : get_pseudo_ti_init (tree type, unsigned tk_index)
1154 : : {
1155 : 347348 : tinfo_s *ti = get_tinfo_desc (tk_index);
1156 : :
1157 : 347348 : gcc_assert (at_eof);
1158 : 347348 : switch (tk_index)
1159 : : {
1160 : 106 : case TK_POINTER_MEMBER_TYPE:
1161 : 106 : return ptm_initializer (ti, type);
1162 : :
1163 : 1154 : case TK_POINTER_TYPE:
1164 : 1154 : return ptr_initializer (ti, type);
1165 : :
1166 : 383 : case TK_BUILTIN_TYPE:
1167 : 383 : case TK_ENUMERAL_TYPE:
1168 : 383 : case TK_FUNCTION_TYPE:
1169 : 383 : case TK_ARRAY_TYPE:
1170 : 383 : return generic_initializer (ti, type);
1171 : :
1172 : 94450 : case TK_CLASS_TYPE:
1173 : 94450 : return class_initializer (ti, type, 0);
1174 : :
1175 : 211471 : case TK_SI_CLASS_TYPE:
1176 : 211471 : {
1177 : 211471 : tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
1178 : 211471 : tree tinfo = get_void_tinfo_ptr (BINFO_TYPE (base_binfo));
1179 : :
1180 : : /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1181 : 211471 : ti = &(*tinfo_descs)[tk_index];
1182 : 211471 : return class_initializer (ti, type, 1, tinfo);
1183 : : }
1184 : :
1185 : 39784 : default:
1186 : 39784 : {
1187 : 39784 : int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0)
1188 : 39784 : | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1));
1189 : 39784 : tree binfo = TYPE_BINFO (type);
1190 : 39784 : unsigned nbases = BINFO_N_BASE_BINFOS (binfo);
1191 : 39784 : vec<tree, va_gc> *base_accesses = BINFO_BASE_ACCESSES (binfo);
1192 : 39901 : tree offset_type = LONGPTR_T;
1193 : 39784 : vec<constructor_elt, va_gc> *init_vec = NULL;
1194 : :
1195 : 39784 : gcc_assert (tk_index - TK_VMI_CLASS_TYPES + 1 == nbases);
1196 : :
1197 : 39784 : vec_safe_grow (init_vec, nbases, true);
1198 : : /* Generate the base information initializer. */
1199 : 108028 : for (unsigned ix = nbases; ix--;)
1200 : : {
1201 : 68244 : tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
1202 : 68244 : int flags = 0;
1203 : 68244 : tree tinfo;
1204 : 68244 : tree offset;
1205 : 68244 : vec<constructor_elt, va_gc> *v;
1206 : :
1207 : 68244 : if ((*base_accesses)[ix] == access_public_node)
1208 : 67316 : flags |= 2;
1209 : 68244 : tinfo = get_void_tinfo_ptr (BINFO_TYPE (base_binfo));
1210 : 68244 : if (BINFO_VIRTUAL_P (base_binfo))
1211 : : {
1212 : : /* We store the vtable offset at which the virtual
1213 : : base offset can be found. */
1214 : 4458 : offset = BINFO_VPTR_FIELD (base_binfo);
1215 : 4458 : flags |= 1;
1216 : : }
1217 : : else
1218 : 63786 : offset = BINFO_OFFSET (base_binfo);
1219 : :
1220 : : /* Combine offset and flags into one field. */
1221 : 68244 : offset = fold_convert (offset_type, offset);
1222 : 68244 : offset = fold_build2_loc (input_location,
1223 : : LSHIFT_EXPR, offset_type, offset,
1224 : 68244 : build_int_cst (offset_type, 8));
1225 : 68244 : offset = fold_build2_loc (input_location,
1226 : : BIT_IOR_EXPR, offset_type, offset,
1227 : : build_int_cst (offset_type, flags));
1228 : 68244 : vec_alloc (v, 2);
1229 : 68244 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tinfo);
1230 : 68244 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, offset);
1231 : 68244 : tree base_init = build_constructor (init_list_type_node, v);
1232 : 68244 : constructor_elt *e = &(*init_vec)[ix];
1233 : 68244 : e->index = NULL_TREE;
1234 : 68244 : e->value = base_init;
1235 : : }
1236 : 39784 : tree base_inits = build_constructor (init_list_type_node, init_vec);
1237 : :
1238 : : /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1239 : 39784 : ti = &(*tinfo_descs)[tk_index];
1240 : 39784 : return class_initializer (ti, type, 3,
1241 : : build_int_cst (NULL_TREE, hint),
1242 : : build_int_cst (NULL_TREE, nbases),
1243 : : base_inits);
1244 : : }
1245 : : }
1246 : : }
1247 : :
1248 : : /* Return the index of a pseudo type info type node used to describe
1249 : : TYPE. TYPE must be a complete type (or cv void), except at the end
1250 : : of the translation unit. */
1251 : :
1252 : : static unsigned
1253 : 2010847 : get_pseudo_ti_index (tree type)
1254 : : {
1255 : 2010847 : unsigned ix;
1256 : :
1257 : 2010847 : switch (TREE_CODE (type))
1258 : : {
1259 : : case OFFSET_TYPE:
1260 : : ix = TK_POINTER_MEMBER_TYPE;
1261 : : break;
1262 : :
1263 : : case POINTER_TYPE:
1264 : 2010847 : ix = TK_POINTER_TYPE;
1265 : : break;
1266 : :
1267 : 32 : case ENUMERAL_TYPE:
1268 : 32 : ix = TK_ENUMERAL_TYPE;
1269 : 32 : break;
1270 : :
1271 : 244 : case FUNCTION_TYPE:
1272 : 244 : ix = TK_FUNCTION_TYPE;
1273 : 244 : break;
1274 : :
1275 : 72 : case ARRAY_TYPE:
1276 : 72 : ix = TK_ARRAY_TYPE;
1277 : 72 : break;
1278 : :
1279 : 1995731 : case UNION_TYPE:
1280 : 1995731 : case RECORD_TYPE:
1281 : 1995731 : if (TYPE_PTRMEMFUNC_P (type))
1282 : : ix = TK_POINTER_MEMBER_TYPE;
1283 : 1995627 : else if (!COMPLETE_TYPE_P (type))
1284 : : {
1285 : 70 : if (!at_eof)
1286 : 2 : cxx_incomplete_type_error (NULL_TREE, type);
1287 : : ix = TK_CLASS_TYPE;
1288 : : }
1289 : 1995557 : else if (!TYPE_BINFO (type)
1290 : 1995557 : || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
1291 : : ix = TK_CLASS_TYPE;
1292 : : else
1293 : : {
1294 : 1670816 : tree binfo = TYPE_BINFO (type);
1295 : 1670816 : vec<tree, va_gc> *base_accesses = BINFO_BASE_ACCESSES (binfo);
1296 : 1670816 : tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
1297 : 1670816 : int num_bases = BINFO_N_BASE_BINFOS (binfo);
1298 : :
1299 : 1670816 : if (num_bases == 1
1300 : 1474985 : && (*base_accesses)[0] == access_public_node
1301 : 1473405 : && !BINFO_VIRTUAL_P (base_binfo)
1302 : 3098176 : && integer_zerop (BINFO_OFFSET (base_binfo)))
1303 : : /* single non-virtual public. */
1304 : : ix = TK_SI_CLASS_TYPE;
1305 : : else
1306 : 262501 : ix = TK_VMI_CLASS_TYPES + num_bases - 1;
1307 : : }
1308 : : break;
1309 : :
1310 : 12039 : default:
1311 : 12039 : ix = TK_BUILTIN_TYPE;
1312 : 12039 : break;
1313 : : }
1314 : 2010847 : return ix;
1315 : : }
1316 : :
1317 : : /* Return pointer to tinfo descriptor. Possibly creating the tinfo
1318 : : descriptor in the first place. */
1319 : :
1320 : : static tinfo_s *
1321 : 2663268 : get_tinfo_desc (unsigned ix)
1322 : : {
1323 : 2663268 : unsigned len = tinfo_descs->length ();
1324 : :
1325 : 2663268 : if (len <= ix)
1326 : : {
1327 : : /* too short, extend. */
1328 : 57811 : len = ix + 1 - len;
1329 : 57811 : vec_safe_reserve (tinfo_descs, len);
1330 : 57811 : tinfo_s elt;
1331 : 57811 : elt.type = elt.vtable = elt.name = NULL_TREE;
1332 : 287096 : while (len--)
1333 : 229285 : tinfo_descs->quick_push (elt);
1334 : : }
1335 : :
1336 : 2663268 : tinfo_s *res = &(*tinfo_descs)[ix];
1337 : :
1338 : 2663268 : if (res->type)
1339 : : return res;
1340 : :
1341 : : /* Ok, we have to create it. This layout must be consistent with
1342 : : that defined in the runtime support. We explicitly manage the
1343 : : vtable member, and name it for real type as used in the runtime.
1344 : : The RECORD type has a different name, to avoid collisions. We
1345 : : have to delay generating the VAR_DECL of the vtable until the end
1346 : : of the translation, when we'll have seen the library definition,
1347 : : if there was one. */
1348 : :
1349 : : /* Fields to add, chained in reverse order. */
1350 : 108216 : tree fields = NULL_TREE;
1351 : :
1352 : 108216 : if (ix >= TK_DERIVED_TYPES)
1353 : : {
1354 : : /* First field is the pseudo type_info base class. */
1355 : 72667 : tree fld_base = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1356 : 72667 : get_tinfo_desc (TK_TYPE_INFO_TYPE)->type);
1357 : :
1358 : 72667 : DECL_CHAIN (fld_base) = fields;
1359 : 72667 : fields = fld_base;
1360 : : }
1361 : :
1362 : 108216 : switch (ix)
1363 : : {
1364 : 21569 : case TK_TYPE_INFO_TYPE:
1365 : 21569 : {
1366 : 21569 : tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1367 : : NULL_TREE, const_ptr_type_node);
1368 : 21569 : fields = fld_ptr;
1369 : :
1370 : 21569 : tree fld_str = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1371 : : NULL_TREE, const_string_type_node);
1372 : 21569 : DECL_CHAIN (fld_str) = fields;
1373 : 21569 : fields = fld_str;
1374 : 21569 : break;
1375 : : }
1376 : :
1377 : 13980 : case TK_BASE_TYPE:
1378 : 13980 : {
1379 : : /* Base class internal helper. Pointer to base type, offset to
1380 : : base, flags. */
1381 : 13980 : tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1382 : : NULL_TREE, const_ptr_type_node);
1383 : 13980 : DECL_CHAIN (fld_ptr) = fields;
1384 : 13980 : fields = fld_ptr;
1385 : :
1386 : 13980 : tree fld_flag = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1387 : 14070 : NULL_TREE, LONGPTR_T);
1388 : 13980 : DECL_CHAIN (fld_flag) = fields;
1389 : 13980 : fields = fld_flag;
1390 : 13980 : break;
1391 : : }
1392 : :
1393 : : case TK_BUILTIN_TYPE:
1394 : : /* Fundamental type_info */
1395 : : break;
1396 : :
1397 : : case TK_ARRAY_TYPE:
1398 : : break;
1399 : :
1400 : : case TK_FUNCTION_TYPE:
1401 : : break;
1402 : :
1403 : : case TK_ENUMERAL_TYPE:
1404 : : break;
1405 : :
1406 : 510 : case TK_POINTER_TYPE:
1407 : 510 : case TK_POINTER_MEMBER_TYPE:
1408 : 510 : {
1409 : : /* Pointer type_info. Adds two fields, qualification mask and
1410 : : pointer to the pointed to type. This is really a
1411 : : descendant of __pbase_type_info. */
1412 : 510 : tree fld_mask = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1413 : : NULL_TREE, integer_type_node);
1414 : 510 : DECL_CHAIN (fld_mask) = fields;
1415 : 510 : fields = fld_mask;
1416 : :
1417 : 510 : tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1418 : : NULL_TREE, const_ptr_type_node);
1419 : 510 : DECL_CHAIN (fld_ptr) = fields;
1420 : 510 : fields = fld_ptr;
1421 : :
1422 : 510 : if (ix == TK_POINTER_MEMBER_TYPE)
1423 : : {
1424 : : /* Add a pointer to the class too. */
1425 : 48 : tree fld_cls = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1426 : : NULL_TREE, const_ptr_type_node);
1427 : 48 : DECL_CHAIN (fld_cls) = fields;
1428 : 48 : fields = fld_cls;
1429 : : }
1430 : : break;
1431 : : }
1432 : :
1433 : : case TK_CLASS_TYPE:
1434 : : /* Class type_info. No additional fields. */
1435 : : break;
1436 : :
1437 : 15992 : case TK_SI_CLASS_TYPE:
1438 : 15992 : {
1439 : : /* Single public non-virtual base class. Add pointer to base
1440 : : class. This is really a descendant of
1441 : : __class_type_info. */
1442 : 15992 : tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1443 : : NULL_TREE, const_ptr_type_node);
1444 : 15992 : DECL_CHAIN (fld_ptr) = fields;
1445 : 15992 : fields = fld_ptr;
1446 : 15992 : break;
1447 : : }
1448 : :
1449 : 25348 : default: /* Multiple inheritance. */
1450 : 25348 : {
1451 : 25348 : unsigned num_bases = ix - TK_VMI_CLASS_TYPES + 1;
1452 : :
1453 : 25348 : tree fld_flg = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1454 : : NULL_TREE, integer_type_node);
1455 : 25348 : DECL_CHAIN (fld_flg) = fields;
1456 : 25348 : fields = fld_flg;
1457 : :
1458 : 25348 : tree fld_cnt = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1459 : : NULL_TREE, integer_type_node);
1460 : 25348 : DECL_CHAIN (fld_cnt) = fields;
1461 : 25348 : fields = fld_cnt;
1462 : :
1463 : : /* Create the array of __base_class_type_info entries. */
1464 : 25348 : tree domain = build_index_type (size_int (num_bases - 1));
1465 : 25348 : tree array = build_array_type (get_tinfo_desc (TK_BASE_TYPE)->type,
1466 : : domain);
1467 : 25348 : tree fld_ary = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1468 : : NULL_TREE, array);
1469 : 25348 : DECL_CHAIN (fld_ary) = fields;
1470 : 25348 : fields = fld_ary;
1471 : 25348 : break;
1472 : : }
1473 : : }
1474 : :
1475 : : /* Generate the pseudo type name. */
1476 : 108216 : const char *real_name = tinfo_names[ix < TK_VMI_CLASS_TYPES
1477 : 108216 : ? ix : unsigned (TK_VMI_CLASS_TYPES)];
1478 : 108216 : size_t name_len = strlen (real_name);
1479 : 108216 : char *pseudo_name = (char *) alloca (name_len + 30);
1480 : 108216 : memcpy (pseudo_name, real_name, name_len);
1481 : : /* Those >= TK_VMI_CLASS_TYPES need a discriminator, may as well
1482 : : apply it to all. See get_peudo_tinfo_index where we make use of
1483 : : this. */
1484 : 108216 : sprintf (pseudo_name + name_len, "_pseudo_%d", ix);
1485 : :
1486 : : /* Create the pseudo type. */
1487 : 108216 : tree pseudo_type = make_class_type (RECORD_TYPE);
1488 : : /* Pass the fields chained in reverse. */
1489 : 108216 : finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
1490 : 108216 : CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
1491 : 108216 : DECL_CONTEXT (TYPE_NAME (pseudo_type)) = FROB_CONTEXT (global_namespace);
1492 : 108216 : DECL_TINFO_P (TYPE_NAME (pseudo_type)) = true;
1493 : 108216 : xref_basetypes (pseudo_type, /*bases=*/NULL_TREE);
1494 : :
1495 : 108216 : res->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1496 : 108216 : res->name = get_identifier (real_name);
1497 : :
1498 : : /* Pretend this is public so determine_visibility doesn't give vtables
1499 : : internal linkage. */
1500 : 108216 : TREE_PUBLIC (TYPE_MAIN_DECL (res->type)) = 1;
1501 : :
1502 : 108216 : return res;
1503 : : }
1504 : :
1505 : : /* Return an identifying index for the pseudo type_info TYPE.
1506 : : We wrote the index at the end of the name, so just scan it from
1507 : : there. This isn't critical, as it's only on the first use of this
1508 : : type during module stream out. */
1509 : :
1510 : : unsigned
1511 : 17371 : get_pseudo_tinfo_index (tree type)
1512 : : {
1513 : 17371 : tree name = DECL_NAME (TYPE_NAME (type));
1514 : 17371 : unsigned ix = 0, scale = 1;
1515 : 17371 : size_t len = IDENTIFIER_LENGTH (name);
1516 : 17371 : const char *ptr = IDENTIFIER_POINTER (name) + len;
1517 : :
1518 : 38280 : for (; *--ptr != '_'; scale *= 10)
1519 : : {
1520 : 20909 : len--;
1521 : 20909 : gcc_checking_assert (len && ISDIGIT (*ptr));
1522 : 20909 : ix += (*ptr - '0') * scale;
1523 : : }
1524 : :
1525 : 17371 : gcc_assert (len != IDENTIFIER_LENGTH (name));
1526 : 17371 : return ix;
1527 : : }
1528 : :
1529 : : tree
1530 : 3372 : get_pseudo_tinfo_type (unsigned ix)
1531 : : {
1532 : 3372 : return get_tinfo_desc (ix)->type;
1533 : : }
1534 : :
1535 : : /* We lazily create the type info types. */
1536 : :
1537 : : static void
1538 : 101073 : create_tinfo_types (void)
1539 : : {
1540 : 101073 : gcc_assert (!tinfo_descs);
1541 : :
1542 : 101073 : vec_alloc (tinfo_descs, TK_MAX + 20);
1543 : 101073 : }
1544 : :
1545 : : /* Helper for emit_support_tinfos. Emits the type_info descriptor of
1546 : : a single type. */
1547 : :
1548 : : void
1549 : 220 : emit_support_tinfo_1 (tree bltn)
1550 : : {
1551 : 220 : tree types[3];
1552 : :
1553 : 220 : if (bltn == NULL_TREE)
1554 : 6 : return;
1555 : 214 : types[0] = bltn;
1556 : 214 : types[1] = build_pointer_type (bltn);
1557 : 214 : types[2] = build_pointer_type (cp_build_qualified_type (bltn,
1558 : : TYPE_QUAL_CONST));
1559 : :
1560 : 856 : for (int i = 0; i < 3; ++i)
1561 : : {
1562 : 642 : tree tinfo = get_tinfo_decl (types[i]);
1563 : 642 : TREE_USED (tinfo) = 1;
1564 : 642 : mark_needed (tinfo);
1565 : : /* The C++ ABI requires that these objects be COMDAT. But,
1566 : : On systems without weak symbols, initialized COMDAT
1567 : : objects are emitted with internal linkage. (See
1568 : : comdat_linkage for details.) Since we want these objects
1569 : : to have external linkage so that copies do not have to be
1570 : : emitted in code outside the runtime library, we make them
1571 : : non-COMDAT here.
1572 : :
1573 : : It might also not be necessary to follow this detail of the
1574 : : ABI. */
1575 : 642 : if (!flag_weak || ! targetm.cxx.library_rtti_comdat ())
1576 : : {
1577 : 0 : gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
1578 : 0 : DECL_INTERFACE_KNOWN (tinfo) = 1;
1579 : : }
1580 : :
1581 : : /* Emit it right away if not emitted already. */
1582 : 642 : if (DECL_INITIAL (tinfo) == NULL_TREE)
1583 : : {
1584 : 606 : bool ok = emit_tinfo_decl (tinfo);
1585 : 606 : gcc_assert (ok);
1586 : : /* When compiling libsupc++.a (fundamental_type_info.o),
1587 : : unemitted_tinfo_decls->last () will be tinfo, so pop it
1588 : : from the vector as it is emitted now. If one uses typeid
1589 : : etc. in the same TU as the definition of
1590 : : ~fundamental_type_info (), the tinfo might be emitted
1591 : : already earlier, in such case keep it in the vector
1592 : : (as otherwise we'd need to walk the whole vector) and
1593 : : let c_parse_final_cleanups ignore it when it will have
1594 : : non-NULL DECL_INITIAL. */
1595 : 606 : if (unemitted_tinfo_decls->last () == tinfo)
1596 : 602 : unemitted_tinfo_decls->pop ();
1597 : : }
1598 : : }
1599 : : }
1600 : :
1601 : : /* Emit the type_info descriptors which are guaranteed to be in the runtime
1602 : : support. Generating them here guarantees consistency with the other
1603 : : structures. We use the following heuristic to determine when the runtime
1604 : : is being generated. If std::__fundamental_type_info is defined, and its
1605 : : destructor is defined, then the runtime is being built. */
1606 : :
1607 : : void
1608 : 99673 : emit_support_tinfos (void)
1609 : : {
1610 : : /* Dummy static variable so we can put nullptr in the array; it will be
1611 : : set before we actually start to walk the array. */
1612 : 99673 : static tree *const fundamentals[] =
1613 : : {
1614 : : &void_type_node,
1615 : : &boolean_type_node,
1616 : : &wchar_type_node, &char8_type_node, &char16_type_node, &char32_type_node,
1617 : : &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1618 : : &short_integer_type_node, &short_unsigned_type_node,
1619 : : &integer_type_node, &unsigned_type_node,
1620 : : &long_integer_type_node, &long_unsigned_type_node,
1621 : : &long_long_integer_type_node, &long_long_unsigned_type_node,
1622 : : &float_type_node, &double_type_node, &long_double_type_node,
1623 : : &bfloat16_type_node, &float16_type_node, &float32_type_node,
1624 : : &float64_type_node, &float128_type_node, &float32x_type_node,
1625 : : &float64x_type_node, &float128x_type_node, &nullptr_type_node,
1626 : : 0
1627 : : };
1628 : : /* Similar, but for floating point types only which should get type info
1629 : : regardless whether they are non-NULL or NULL. */
1630 : 99673 : static tree *const fundamentals_with_fallback[] =
1631 : : {
1632 : : &dfloat32_type_node, &dfloat64_type_node, &dfloat128_type_node,
1633 : : 0
1634 : : };
1635 : 99673 : int ix;
1636 : :
1637 : : /* Look for a defined class. */
1638 : 99673 : tree bltn_type = lookup_qualified_name
1639 : 99673 : (abi_node, "__fundamental_type_info", LOOK_want::TYPE, false);
1640 : 99673 : if (TREE_CODE (bltn_type) != TYPE_DECL)
1641 : : return;
1642 : :
1643 : 246 : bltn_type = TREE_TYPE (bltn_type);
1644 : 246 : if (!COMPLETE_TYPE_P (bltn_type))
1645 : : return;
1646 : 246 : tree dtor = CLASSTYPE_DESTRUCTOR (bltn_type);
1647 : 246 : if (!dtor || DECL_EXTERNAL (dtor))
1648 : : return;
1649 : :
1650 : : /* All these are really builtins. So set the location. */
1651 : 6 : location_t saved_loc = input_location;
1652 : 6 : input_location = BUILTINS_LOCATION;
1653 : 6 : doing_runtime = 1;
1654 : 6 : tree fallback = NULL_TREE;
1655 : 180 : for (ix = 0; fundamentals[ix]; ix++)
1656 : 174 : emit_support_tinfo_1 (*fundamentals[ix]);
1657 : 24 : for (ix = 0; fundamentals_with_fallback[ix]; ix++)
1658 : 18 : if (*fundamentals_with_fallback[ix])
1659 : 18 : emit_support_tinfo_1 (*fundamentals_with_fallback[ix]);
1660 : : else
1661 : : {
1662 : 0 : if (fallback == NULL_TREE)
1663 : 0 : fallback = make_node (REAL_TYPE);
1664 : 0 : *fundamentals_with_fallback[ix] = fallback;
1665 : 0 : emit_support_tinfo_1 (fallback);
1666 : 0 : *fundamentals_with_fallback[ix] = NULL_TREE;
1667 : : }
1668 : 12 : for (ix = 0; ix < NUM_INT_N_ENTS; ix ++)
1669 : 6 : if (int_n_enabled_p[ix])
1670 : : {
1671 : 5 : emit_support_tinfo_1 (int_n_trees[ix].signed_type);
1672 : 5 : emit_support_tinfo_1 (int_n_trees[ix].unsigned_type);
1673 : : }
1674 : 24 : for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
1675 : 18 : emit_support_tinfo_1 (TREE_VALUE (t));
1676 : :
1677 : : /* Emit additional typeinfos as requested by target. */
1678 : 6 : targetm.emit_support_tinfos (emit_support_tinfo_1);
1679 : :
1680 : 6 : input_location = saved_loc;
1681 : : }
1682 : :
1683 : : /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1684 : : tinfo decl. Determine whether it needs emitting, and if so
1685 : : generate the initializer. */
1686 : :
1687 : : bool
1688 : 5436983 : emit_tinfo_decl (tree decl)
1689 : : {
1690 : 5436983 : gcc_assert (DECL_TINFO_P (decl));
1691 : :
1692 : 5436983 : tree type = TREE_TYPE (DECL_NAME (decl));
1693 : 5436983 : if (typeinfo_in_lib_p (type))
1694 : : {
1695 : 41860 : if (doing_runtime)
1696 : 606 : DECL_EXTERNAL (decl) = 0;
1697 : : else
1698 : : {
1699 : : /* If we're not in the runtime, then DECL (which is already
1700 : : DECL_EXTERNAL) will not be defined here. */
1701 : 41254 : DECL_INTERFACE_KNOWN (decl) = 1;
1702 : 41254 : return false;
1703 : : }
1704 : : }
1705 : 5395123 : else if (involves_incomplete_p (type))
1706 : : {
1707 : 128 : if (!decl_needed_p (decl))
1708 : : return false;
1709 : : /* If TYPE involves an incomplete class type, then the typeinfo
1710 : : object will be emitted with internal linkage. There is no
1711 : : way to know whether or not types are incomplete until the end
1712 : : of the compilation, so this determination must be deferred
1713 : : until this point. */
1714 : 128 : TREE_PUBLIC (decl) = 0;
1715 : 128 : DECL_EXTERNAL (decl) = 0;
1716 : 128 : DECL_INTERFACE_KNOWN (decl) = 1;
1717 : : }
1718 : :
1719 : 5395729 : import_export_decl (decl);
1720 : 5395729 : if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
1721 : : {
1722 : 347348 : tree init;
1723 : :
1724 : 347348 : DECL_EXTERNAL (decl) = 0;
1725 : 347348 : int pseudo_ix = get_pseudo_ti_index (type);
1726 : 347348 : const tinfo_s *ti = get_tinfo_desc (pseudo_ix);
1727 : 347348 : if (TREE_TYPE (decl) != ti->type)
1728 : : {
1729 : : /* If the class became complete since we first called get_tinfo_decl,
1730 : : its type_info descriptor may have switched from __class_type_info
1731 : : to e.g. __si_class_type_info. */
1732 : 4 : TREE_TYPE (decl) = ti->type;
1733 : 4 : relayout_decl (decl);
1734 : : }
1735 : 347348 : init = get_pseudo_ti_init (type, pseudo_ix);
1736 : 347348 : DECL_INITIAL (decl) = init;
1737 : 347348 : mark_used (decl);
1738 : 347348 : cp_finish_decl (decl, init, false, NULL_TREE, 0);
1739 : : /* Avoid targets optionally bumping up the alignment to improve
1740 : : vector instruction accesses, tinfo are never accessed this way. */
1741 : : #ifdef DATA_ABI_ALIGNMENT
1742 : 347348 : SET_DECL_ALIGN (decl, DATA_ABI_ALIGNMENT (decl, TYPE_ALIGN (TREE_TYPE (decl))));
1743 : 347348 : DECL_USER_ALIGN (decl) = true;
1744 : : #endif
1745 : 347348 : return true;
1746 : : }
1747 : : else
1748 : 5048381 : return false;
1749 : : }
1750 : :
1751 : : #include "gt-cp-rtti.h"
|