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