Compiler side new abi rtti (not enabled).
[gcc.git] / gcc / cp / rtti.c
1 /* RunTime Type Identification
2 Copyright (C) 1995, 96-97, 1998, 1999, 2000 Free Software Foundation, Inc.
3 Mostly written by Jason Merrill (jason@cygnus.com).
4
5 This file is part of GNU CC.
6
7 GNU CC 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 2, or (at your option)
10 any later version.
11
12 GNU CC 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 GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "tree.h"
26 #include "cp-tree.h"
27 #include "flags.h"
28 #include "output.h"
29 #include "assert.h"
30 #include "toplev.h"
31
32 #ifndef INT_TYPE_SIZE
33 #define INT_TYPE_SIZE BITS_PER_WORD
34 #endif
35
36 /* Accessors for the type_info objects. We need to remember several things
37 about each of the type_info types. The global tree nodes such as
38 bltn_desc_type_node are TREE_LISTs, and these macros are used to access
39 the required information. */
40 /* The RECORD_TYPE of a type_info derived class. */
41 #define TINFO_PSEUDO_TYPE(NODE) TREE_TYPE (NODE)
42 /* The VAR_DECL of the vtable for the type_info derived class. */
43 #define TINFO_VTABLE_DECL(NODE) TREE_VALUE (NODE)
44
45 extern struct obstack permanent_obstack;
46
47 static tree build_runtime_decl PARAMS((const char *, tree));
48 static tree build_headof_sub PARAMS((tree));
49 static tree build_headof PARAMS((tree));
50 static tree get_tinfo_var PARAMS((tree));
51 static tree ifnonnull PARAMS((tree, tree));
52 static tree tinfo_name PARAMS((tree));
53 static tree get_base_offset PARAMS((tree, tree));
54 static tree build_dynamic_cast_1 PARAMS((tree, tree));
55 static void expand_si_desc PARAMS((tree, tree));
56 static void expand_class_desc PARAMS((tree, tree));
57 static void expand_attr_desc PARAMS((tree, tree));
58 static void expand_ptr_desc PARAMS((tree, tree));
59 static void expand_generic_desc PARAMS((tree, tree, const char *));
60 static tree throw_bad_cast PARAMS((void));
61 static tree throw_bad_typeid PARAMS((void));
62 static tree get_tinfo_decl_dynamic PARAMS((tree));
63 static tree tinfo_from_decl PARAMS((tree));
64 static int qualifier_flags PARAMS((tree));
65 static tree tinfo_base_init PARAMS((tree, tree));
66 static tree generic_initializer PARAMS((tree, tree));
67 static tree ptr_ref_initializer PARAMS((tree, tree));
68 static tree ptmd_initializer PARAMS((tree, tree));
69 static int class_hint_flags PARAMS((tree));
70 static tree class_initializer PARAMS((tree, tree, tree));
71 static tree synthesize_tinfo_var PARAMS((tree, tree));
72 static tree create_real_tinfo_var PARAMS((tree, tree, tree));
73 static tree create_pseudo_type_info PARAMS((const char *, int, ...));
74 static tree get_vmi_pseudo_type_info PARAMS((int));
75 static void create_tinfo_types PARAMS((void));
76
77 static int doing_runtime = 0;
78 \f
79 void
80 init_rtti_processing ()
81 {
82 if (flag_honor_std)
83 push_namespace (get_identifier ("std"));
84 type_info_type_node = xref_tag
85 (class_type_node, get_identifier ("type_info"), 1);
86 if (flag_honor_std)
87 pop_namespace ();
88 if (!new_abi_rtti_p ())
89 {
90 tinfo_decl_id = get_identifier ("__tf");
91 tinfo_decl_type = build_function_type
92 (build_reference_type
93 (build_qualified_type
94 (type_info_type_node, TYPE_QUAL_CONST)),
95 void_list_node);
96 }
97 else
98 {
99 tinfo_decl_id = get_identifier ("__ti");
100 tinfo_decl_type = build_qualified_type
101 (type_info_type_node, TYPE_QUAL_CONST);
102 }
103 tinfo_var_id = get_identifier ("__ti");
104 }
105
106 /* Given a pointer to an object with at least one virtual table
107 pointer somewhere, return a pointer to a possible sub-object that
108 has a virtual table pointer in it that is the vtable parent for
109 that sub-object. */
110
111 static tree
112 build_headof_sub (exp)
113 tree exp;
114 {
115 tree type = TREE_TYPE (TREE_TYPE (exp));
116 tree basetype = CLASSTYPE_RTTI (type);
117 tree binfo = get_binfo (basetype, type, 0);
118
119 exp = convert_pointer_to_real (binfo, exp);
120 return exp;
121 }
122
123 /* Given the expression EXP of type `class *', return the head of the
124 object pointed to by EXP with type cv void*, if the class has any
125 virtual functions (TYPE_POLYMORPHIC_P), else just return the
126 expression. */
127
128 static tree
129 build_headof (exp)
130 tree exp;
131 {
132 tree type = TREE_TYPE (exp);
133 tree aref;
134 tree offset;
135
136 my_friendly_assert (TREE_CODE (type) == POINTER_TYPE, 20000112);
137 type = TREE_TYPE (type);
138
139 if (!TYPE_POLYMORPHIC_P (type))
140 return exp;
141 if (CLASSTYPE_COM_INTERFACE (type))
142 {
143 cp_error ("RTTI not supported for COM interface type `%T'", type);
144 return error_mark_node;
145 }
146
147 /* If we don't have rtti stuff, get to a sub-object that does. */
148 if (!CLASSTYPE_VFIELDS (TREE_TYPE (TREE_TYPE (exp))))
149 exp = build_headof_sub (exp);
150
151 /* We use this a couple of times below, protect it. */
152 exp = save_expr (exp);
153
154 aref = build_vtbl_ref (build_indirect_ref (exp, NULL_PTR), integer_zero_node);
155
156 if (flag_vtable_thunks)
157 offset = aref;
158 else
159 offset = build_component_ref (aref, delta_identifier, NULL_TREE, 0);
160
161 type = build_qualified_type (ptr_type_node,
162 CP_TYPE_QUALS (TREE_TYPE (exp)));
163 return build (PLUS_EXPR, type, exp,
164 cp_convert (ptrdiff_type_node, offset));
165 }
166
167 /* Build a decl to a runtime entry point taking void and returning TYPE.
168 Although the entry point may never return, making its return type
169 consistent is necessary. */
170
171 static tree
172 build_runtime_decl (name, type)
173 const char *name;
174 tree type;
175 {
176 tree d = get_identifier (name);
177
178 if (IDENTIFIER_GLOBAL_VALUE (d))
179 d = IDENTIFIER_GLOBAL_VALUE (d);
180 else
181 {
182 type = build_function_type (type, void_list_node);
183 d = build_lang_decl (FUNCTION_DECL, d, type);
184 DECL_EXTERNAL (d) = 1;
185 TREE_PUBLIC (d) = 1;
186 DECL_ARTIFICIAL (d) = 1;
187 pushdecl_top_level (d);
188 make_function_rtl (d);
189 }
190
191 mark_used (d);
192 return d;
193 }
194
195 /* Get a bad_cast node for the program to throw...
196
197 See libstdc++/exception.cc for __throw_bad_cast */
198
199 static tree
200 throw_bad_cast ()
201 {
202 if (!throw_bad_cast_node)
203 throw_bad_cast_node = build_runtime_decl
204 ("__throw_bad_cast", ptr_type_node);
205
206 return build_call (throw_bad_cast_node,
207 TREE_TYPE (TREE_TYPE (throw_bad_cast_node)),
208 NULL_TREE);
209 }
210
211 static tree
212 throw_bad_typeid ()
213 {
214 if (!throw_bad_typeid_node)
215 throw_bad_typeid_node = build_runtime_decl
216 ("__throw_bad_typeid",
217 build_reference_type
218 (build_qualified_type
219 (type_info_type_node, TYPE_QUAL_CONST)));
220
221 return build_call (throw_bad_typeid_node,
222 TREE_TYPE (TREE_TYPE (throw_bad_typeid_node)),
223 NULL_TREE);
224 }
225 \f
226 /* Return a pointer to type_info function associated with the expression EXP.
227 If EXP is a reference to a polymorphic class, return the dynamic type;
228 otherwise return the static type of the expression. */
229
230 static tree
231 get_tinfo_decl_dynamic (exp)
232 tree exp;
233 {
234 tree type;
235
236 if (exp == error_mark_node)
237 return error_mark_node;
238
239 type = TREE_TYPE (exp);
240
241 /* peel back references, so they match. */
242 if (TREE_CODE (type) == REFERENCE_TYPE)
243 type = TREE_TYPE (type);
244
245 /* Peel off cv qualifiers. */
246 type = TYPE_MAIN_VARIANT (type);
247
248 if (type != void_type_node)
249 type = complete_type_or_else (type, exp);
250
251 if (!type)
252 return error_mark_node;
253
254 /* If exp is a reference to polymorphic type, get the real type_info. */
255 if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
256 {
257 /* build reference to type_info from vtable. */
258 tree t;
259
260 if (! flag_rtti)
261 error ("taking dynamic typeid of object with -fno-rtti");
262 if (CLASSTYPE_COM_INTERFACE (type))
263 {
264 cp_error ("RTTI not supported for COM interface type `%T'", type);
265 return error_mark_node;
266 }
267
268 /* If we don't have rtti stuff, get to a sub-object that does. */
269 if (! CLASSTYPE_VFIELDS (type))
270 {
271 exp = build_unary_op (ADDR_EXPR, exp, 0);
272 exp = build_headof_sub (exp);
273 exp = build_indirect_ref (exp, NULL_PTR);
274 }
275
276 if (flag_vtable_thunks)
277 t = build_vfn_ref ((tree *) 0, exp, integer_one_node);
278 else
279 t = build_vfn_ref ((tree *) 0, exp, integer_zero_node);
280 TREE_TYPE (t) = build_pointer_type (tinfo_decl_type);
281 return t;
282 }
283
284 /* otherwise return the type_info for the static type of the expr. */
285 exp = get_tinfo_decl (TYPE_MAIN_VARIANT (type));
286 return build_unary_op (ADDR_EXPR, exp, 0);
287 }
288
289 tree
290 build_typeid (exp)
291 tree exp;
292 {
293 tree cond = NULL_TREE;
294 int nonnull = 0;
295
296 if (! flag_rtti)
297 {
298 error ("cannot use typeid with -fno-rtti");
299 return error_mark_node;
300 }
301
302 if (TYPE_SIZE (type_info_type_node) == NULL_TREE)
303 {
304 error ("must #include <typeinfo> before using typeid");
305 return error_mark_node;
306 }
307
308 if (processing_template_decl)
309 return build_min_nt (TYPEID_EXPR, exp);
310
311 if (TREE_CODE (exp) == INDIRECT_REF
312 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
313 && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
314 && ! resolves_to_fixed_type_p (exp, &nonnull)
315 && ! nonnull)
316 {
317 exp = stabilize_reference (exp);
318 cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0));
319 }
320
321 exp = get_tinfo_decl_dynamic (exp);
322
323 if (exp == error_mark_node)
324 return error_mark_node;
325
326 exp = tinfo_from_decl (exp);
327
328 if (cond)
329 {
330 tree bad = throw_bad_typeid ();
331
332 exp = build (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
333 }
334
335 return convert_from_reference (exp);
336 }
337
338 static tree
339 get_tinfo_var (type)
340 tree type;
341 {
342 tree tname = build_overload_with_type (tinfo_var_id, type);
343 tree arrtype;
344 int size;
345
346 my_friendly_assert (!new_abi_rtti_p (), 20000118);
347 if (IDENTIFIER_GLOBAL_VALUE (tname))
348 return IDENTIFIER_GLOBAL_VALUE (tname);
349
350 /* Figure out how much space we need to allocate for the type_info object.
351 If our struct layout or the type_info classes are changed, this will
352 need to be modified. */
353 if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
354 size = 3 * POINTER_SIZE + INT_TYPE_SIZE;
355 else if (TREE_CODE (type) == POINTER_TYPE
356 && ! (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE
357 || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE))
358 size = 3 * POINTER_SIZE;
359 else if (IS_AGGR_TYPE (type))
360 {
361 if (CLASSTYPE_N_BASECLASSES (type) == 0)
362 size = 2 * POINTER_SIZE;
363 else if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
364 && (TREE_VIA_PUBLIC
365 (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), 0))))
366 size = 3 * POINTER_SIZE;
367 else
368 size = 3 * POINTER_SIZE + TYPE_PRECISION (sizetype);
369 }
370 else
371 size = 2 * POINTER_SIZE;
372
373 /* The type for a character array of the appropriate size. */
374 arrtype = build_cplus_array_type
375 (unsigned_char_type_node,
376 build_index_type (size_int (size / BITS_PER_UNIT - 1)));
377
378 return declare_global_var (tname, arrtype);
379 }
380
381 /* Generate the NTBS name of a type. */
382 static tree
383 tinfo_name (type)
384 tree type;
385 {
386 const char *name = build_overload_name (type, 1, 1);
387 tree name_string = combine_strings (build_string (strlen (name) + 1, name));
388 return name_string;
389 }
390
391 /* Returns a decl for a function or variable which can be used to obtain a
392 type_info object for TYPE. The old-abi uses functions, the new-abi
393 uses the type_info object directly. You can take the address of the
394 returned decl, to save the decl. To use the decl call
395 tinfo_from_decl. You must arrange that the decl is mark_used, if
396 actually use it --- decls in vtables are only used if the vtable is
397 output. */
398
399 tree
400 get_tinfo_decl (type)
401 tree type;
402 {
403 tree name;
404 tree d;
405
406 if (TREE_CODE (type) == OFFSET_TYPE)
407 type = TREE_TYPE (type);
408 if (TREE_CODE (type) == METHOD_TYPE)
409 type = build_function_type (TREE_TYPE (type),
410 TREE_CHAIN (TYPE_ARG_TYPES (type)));
411
412 name = build_overload_with_type (tinfo_decl_id, type);
413
414 d = IDENTIFIER_GLOBAL_VALUE (name);
415 if (d)
416 /* OK */;
417 else if (!new_abi_rtti_p ())
418 {
419 /* The tinfo decl is a function returning a reference to the type_info
420 object. */
421 d = build_lang_decl (FUNCTION_DECL, name, tinfo_decl_type);
422 DECL_EXTERNAL (d) = 1;
423 TREE_PUBLIC (d) = 1;
424 DECL_ARTIFICIAL (d) = 1;
425 DECL_NOT_REALLY_EXTERN (d) = 1;
426 SET_DECL_TINFO_FN_P (d);
427 TREE_TYPE (name) = type;
428
429 pushdecl_top_level (d);
430 make_function_rtl (d);
431 mark_inline_for_output (d);
432 }
433 else
434 {
435 /* The tinfo decl is the type_info object itself. We make all
436 tinfo objects look as type_info, even though they will end up
437 being a subclass of that when emitted. This means the we'll
438 erroneously think we know the dynamic type -- be careful in the
439 runtime. */
440 d = build_lang_decl (VAR_DECL, name, tinfo_decl_type);
441
442 DECL_ARTIFICIAL (d) = 1;
443 DECL_ALIGN (d) = TYPE_ALIGN (ptr_type_node);
444 TREE_READONLY (d) = 1;
445 TREE_STATIC (d) = 1;
446 DECL_EXTERNAL (d) = 1;
447 TREE_PUBLIC (d) = 1;
448 DECL_ASSEMBLER_NAME (d) = DECL_NAME (d);
449 cp_finish_decl (d, NULL_TREE, NULL_TREE, 0);
450
451 pushdecl_top_level (d);
452 /* Remember the type it is for. */
453 TREE_TYPE (name) = type;
454 }
455 return d;
456 }
457
458 /* Given an expr produced by get_tinfo_decl, return an expr which
459 produces a reference to the type_info object. */
460
461 static tree
462 tinfo_from_decl (expr)
463 tree expr;
464 {
465 tree t;
466
467 if (!new_abi_rtti_p ())
468 t = build_call (expr, TREE_TYPE (tinfo_decl_type), NULL_TREE);
469 else if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
470 t = build_indirect_ref (expr, NULL);
471 else
472 t = expr;
473
474 return t;
475 }
476
477 tree
478 get_typeid_1 (type)
479 tree type;
480 {
481 tree t;
482
483 t = get_tinfo_decl (type);
484 t = tinfo_from_decl (t);
485 return convert_from_reference (t);
486 }
487
488 /* Return the type_info object for TYPE. */
489
490 tree
491 get_typeid (type)
492 tree type;
493 {
494 if (type == error_mark_node)
495 return error_mark_node;
496
497 if (TYPE_SIZE (type_info_type_node) == NULL_TREE)
498 {
499 error ("must #include <typeinfo> before using typeid");
500 return error_mark_node;
501 }
502
503 if (processing_template_decl)
504 return build_min_nt (TYPEID_EXPR, type);
505
506 /* If the type of the type-id is a reference type, the result of the
507 typeid expression refers to a type_info object representing the
508 referenced type. */
509 if (TREE_CODE (type) == REFERENCE_TYPE)
510 type = TREE_TYPE (type);
511
512 /* The top-level cv-qualifiers of the lvalue expression or the type-id
513 that is the operand of typeid are always ignored. */
514 type = TYPE_MAIN_VARIANT (type);
515
516 if (type != void_type_node)
517 type = complete_type_or_else (type, NULL_TREE);
518
519 if (!type)
520 return error_mark_node;
521
522 return get_typeid_1 (type);
523 }
524
525 /* Check whether TEST is null before returning RESULT. If TEST is used in
526 RESULT, it must have previously had a save_expr applied to it. */
527
528 static tree
529 ifnonnull (test, result)
530 tree test, result;
531 {
532 return build (COND_EXPR, TREE_TYPE (result),
533 build (EQ_EXPR, boolean_type_node, test, integer_zero_node),
534 cp_convert (TREE_TYPE (result), integer_zero_node),
535 result);
536 }
537
538 /* Generate the constant expression describing where direct base BINFO
539 appears within the PARENT. How to interpret this expression depends on
540 details of the ABI, which the runtime must be aware of. */
541
542 static tree
543 get_base_offset (binfo, parent)
544 tree binfo;
545 tree parent;
546 {
547 tree offset;
548
549 if (!TREE_VIA_VIRTUAL (binfo))
550 offset = BINFO_OFFSET (binfo);
551 else if (!vbase_offsets_in_vtable_p ())
552 {
553 tree t = BINFO_TYPE (binfo);
554 const char *name;
555 tree field;
556
557 FORMAT_VBASE_NAME (name, t);
558 field = lookup_field (parent, get_identifier (name), 0, 0);
559 offset = size_binop (FLOOR_DIV_EXPR,
560 DECL_FIELD_BITPOS (field),
561 size_int (BITS_PER_UNIT));
562 offset = convert (sizetype, offset);
563 }
564 else
565 {
566 /* Under the new ABI, we store the vtable offset at which
567 the virtual base offset can be found. */
568 tree vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), parent);
569 offset = convert (sizetype, BINFO_VPTR_FIELD (vbase));
570 }
571 return offset;
572 }
573
574 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
575 paper. */
576
577 static tree
578 build_dynamic_cast_1 (type, expr)
579 tree type, expr;
580 {
581 enum tree_code tc = TREE_CODE (type);
582 tree exprtype;
583 enum tree_code ec;
584 tree dcast_fn;
585 tree old_expr = expr;
586
587 if (TREE_CODE (expr) == OFFSET_REF)
588 expr = resolve_offset_ref (expr);
589
590 exprtype = TREE_TYPE (expr);
591 assert (exprtype != NULL_TREE);
592 ec = TREE_CODE (exprtype);
593
594 switch (tc)
595 {
596 case POINTER_TYPE:
597 if (ec == REFERENCE_TYPE)
598 {
599 expr = convert_from_reference (expr);
600 exprtype = TREE_TYPE (expr);
601 ec = TREE_CODE (exprtype);
602 }
603 if (ec != POINTER_TYPE)
604 goto fail;
605 if (TREE_CODE (TREE_TYPE (exprtype)) != RECORD_TYPE)
606 goto fail;
607 if (TYPE_SIZE (complete_type (TREE_TYPE (exprtype))) == NULL_TREE)
608 goto fail;
609 if (!at_least_as_qualified_p (TREE_TYPE (type),
610 TREE_TYPE (exprtype)))
611 goto fail;
612 if (TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)
613 break;
614 /* else fall through */
615 case REFERENCE_TYPE:
616 if (TREE_CODE (TREE_TYPE (type)) != RECORD_TYPE)
617 goto fail;
618 if (TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
619 goto fail;
620 break;
621 /* else fall through */
622 default:
623 goto fail;
624 }
625
626 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
627 if (ec == RECORD_TYPE)
628 {
629 exprtype = build_reference_type (exprtype);
630 expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
631 LOOKUP_NORMAL, NULL_TREE);
632 ec = REFERENCE_TYPE;
633 }
634
635 if (tc == REFERENCE_TYPE)
636 {
637 if (ec != REFERENCE_TYPE)
638 goto fail;
639 if (TREE_CODE (TREE_TYPE (exprtype)) != RECORD_TYPE)
640 goto fail;
641 if (TYPE_SIZE (complete_type (TREE_TYPE (exprtype))) == NULL_TREE)
642 goto fail;
643 if (!at_least_as_qualified_p (TREE_TYPE (type),
644 TREE_TYPE (exprtype)))
645 goto fail;
646 }
647
648 /* If *type is an unambiguous accessible base class of *exprtype,
649 convert statically. */
650 {
651 int distance;
652 tree path;
653
654 distance = get_base_distance (TREE_TYPE (type), TREE_TYPE (exprtype), 1,
655 &path);
656
657 if (distance == -2)
658 {
659 cp_error ("dynamic_cast from `%T' to ambiguous base class `%T'",
660 TREE_TYPE (exprtype), TREE_TYPE (type));
661 return error_mark_node;
662 }
663 if (distance == -3)
664 {
665 cp_error ("dynamic_cast from `%T' to private base class `%T'",
666 TREE_TYPE (exprtype), TREE_TYPE (type));
667 return error_mark_node;
668 }
669
670 if (distance >= 0)
671 return build_vbase_path (PLUS_EXPR, type, expr, path, 0);
672 }
673
674 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
675 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
676 {
677 tree expr1;
678 /* if TYPE is `void *', return pointer to complete object. */
679 if (tc == POINTER_TYPE
680 && TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)
681 {
682 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
683 if (TREE_CODE (expr) == ADDR_EXPR
684 && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
685 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
686 return build1 (NOP_EXPR, type, expr);
687
688 /* Since expr is used twice below, save it. */
689 expr = save_expr (expr);
690
691 expr1 = build_headof (expr);
692 if (TREE_TYPE (expr1) != type)
693 expr1 = build1 (NOP_EXPR, type, expr1);
694 return ifnonnull (expr, expr1);
695 }
696 else
697 {
698 tree retval;
699 tree result, td2, td3, elems;
700 tree static_type, target_type, boff;
701
702 /* If we got here, we can't convert statically. Therefore,
703 dynamic_cast<D&>(b) (b an object) cannot succeed. */
704 if (ec == REFERENCE_TYPE)
705 {
706 if (TREE_CODE (old_expr) == VAR_DECL
707 && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
708 {
709 tree expr = throw_bad_cast ();
710 cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
711 old_expr, type);
712 /* Bash it to the expected type. */
713 TREE_TYPE (expr) = type;
714 return expr;
715 }
716 }
717 /* Ditto for dynamic_cast<D*>(&b). */
718 else if (TREE_CODE (expr) == ADDR_EXPR)
719 {
720 tree op = TREE_OPERAND (expr, 0);
721 if (TREE_CODE (op) == VAR_DECL
722 && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
723 {
724 cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
725 op, type);
726 retval = build_int_2 (0, 0);
727 TREE_TYPE (retval) = type;
728 return retval;
729 }
730 }
731
732 target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
733 static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
734 td2 = build_unary_op (ADDR_EXPR, get_tinfo_decl (target_type), 0);
735 td3 = build_unary_op (ADDR_EXPR, get_tinfo_decl (static_type), 0);
736
737 /* Determine how T and V are related. */
738 boff = get_dynamic_cast_base_type (static_type, target_type);
739
740 /* Since expr is used twice below, save it. */
741 expr = save_expr (expr);
742
743 expr1 = expr;
744 if (tc == REFERENCE_TYPE)
745 expr1 = build_unary_op (ADDR_EXPR, expr1, 0);
746
747 if (!new_abi_rtti_p ())
748 {
749 tree expr2 = build_headof (expr1);
750 tree td1 = expr;
751
752 if (ec == POINTER_TYPE)
753 td1 = build_indirect_ref (td1, NULL_PTR);
754 td1 = get_tinfo_decl_dynamic (td1);
755
756 elems = tree_cons
757 (NULL_TREE, td1, tree_cons
758 (NULL_TREE, td2, tree_cons
759 (NULL_TREE, boff, tree_cons
760 (NULL_TREE, expr2, tree_cons
761 (NULL_TREE, td3, tree_cons
762 (NULL_TREE, expr1, NULL_TREE))))));
763 }
764 else
765 elems = tree_cons
766 (NULL_TREE, expr1, tree_cons
767 (NULL_TREE, td3, tree_cons
768 (NULL_TREE, td2, tree_cons
769 (NULL_TREE, boff, NULL_TREE))));
770
771 dcast_fn = dynamic_cast_node;
772 if (!dcast_fn)
773 {
774 tree tmp;
775 tree tinfo_ptr;
776 tree ns = global_namespace;
777 const char *name;
778
779 push_nested_namespace (ns);
780 if (!new_abi_rtti_p ())
781 {
782 tinfo_ptr = build_pointer_type (tinfo_decl_type);
783 name = "__dynamic_cast_2";
784 tmp = tree_cons
785 (NULL_TREE, tinfo_ptr, tree_cons
786 (NULL_TREE, tinfo_ptr, tree_cons
787 (NULL_TREE, integer_type_node, tree_cons
788 (NULL_TREE, ptr_type_node, tree_cons
789 (NULL_TREE, tinfo_ptr, tree_cons
790 (NULL_TREE, ptr_type_node, void_list_node))))));
791 }
792 else
793 {
794 if (flag_honor_std)
795 {
796 push_namespace (get_identifier ("std"));
797 ns = current_namespace;
798 }
799 tinfo_ptr = xref_tag (class_type_node,
800 get_identifier ("__class_type_info"),
801 1);
802
803 tinfo_ptr = build_pointer_type
804 (build_qualified_type
805 (tinfo_ptr, TYPE_QUAL_CONST));
806 name = "__dynamic_cast";
807 tmp = tree_cons
808 (NULL_TREE, const_ptr_type_node, tree_cons
809 (NULL_TREE, tinfo_ptr, tree_cons
810 (NULL_TREE, tinfo_ptr, tree_cons
811 (NULL_TREE, ptrdiff_type_node, void_list_node))));
812 }
813 tmp = build_function_type (ptr_type_node, tmp);
814 dcast_fn = build_lang_decl (FUNCTION_DECL,
815 get_identifier (name),
816 tmp);
817 DECL_EXTERNAL (dcast_fn) = 1;
818 TREE_PUBLIC (dcast_fn) = 1;
819 DECL_ARTIFICIAL (dcast_fn) = 1;
820 pushdecl (dcast_fn);
821 if (new_abi_rtti_p ())
822 /* We want it's name mangling. */
823 set_mangled_name_for_decl (dcast_fn);
824 make_function_rtl (dcast_fn);
825 pop_nested_namespace (ns);
826 dynamic_cast_node = dcast_fn;
827 }
828 mark_used (dcast_fn);
829 result = build_call
830 (dcast_fn, TREE_TYPE (TREE_TYPE (dcast_fn)), elems);
831
832 if (tc == REFERENCE_TYPE)
833 {
834 tree bad = throw_bad_cast ();
835
836 result = save_expr (result);
837 return build (COND_EXPR, type, result, result, bad);
838 }
839
840 /* Now back to the type we want from a void*. */
841 result = cp_convert (type, result);
842 return ifnonnull (expr, result);
843 }
844 }
845
846 cp_error ("dynamic_cast from non-polymorphic type `%#T'", exprtype);
847 return error_mark_node;
848
849 fail:
850 cp_error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T'",
851 expr, exprtype, type);
852 return error_mark_node;
853 }
854
855 tree
856 build_dynamic_cast (type, expr)
857 tree type, expr;
858 {
859 if (type == error_mark_node || expr == error_mark_node)
860 return error_mark_node;
861
862 if (processing_template_decl)
863 return build_min (DYNAMIC_CAST_EXPR, type, expr);
864
865 return convert_from_reference (build_dynamic_cast_1 (type, expr));
866 }
867 \f
868 /* Build and initialize various sorts of descriptors. Every descriptor
869 node has a name associated with it (the name created by mangling).
870 For this reason, we use the identifier as our access to the __*_desc
871 nodes, instead of sticking them directly in the types. Otherwise we
872 would burden all built-in types (and pointer types) with slots that
873 we don't necessarily want to use.
874
875 For each descriptor we build, we build a variable that contains
876 the descriptor's information. When we need this info at runtime,
877 all we need is access to these variables.
878
879 Note: these constructors always return the address of the descriptor
880 info, since that is simplest for their mutual interaction. */
881
882 /* Build an initializer for a __si_type_info node. */
883
884 static void
885 expand_si_desc (tdecl, type)
886 tree tdecl;
887 tree type;
888 {
889 tree t, elems, fn;
890 tree name_string = tinfo_name (type);
891
892 type = BINFO_TYPE (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), 0));
893 finish_expr_stmt (get_typeid_1 (type));
894 t = decay_conversion (get_tinfo_var (type));
895 elems = tree_cons
896 (NULL_TREE, decay_conversion (tdecl), tree_cons
897 (NULL_TREE, decay_conversion (name_string), tree_cons
898 (NULL_TREE, t, NULL_TREE)));
899
900 fn = get_identifier ("__rtti_si");
901 if (IDENTIFIER_GLOBAL_VALUE (fn))
902 fn = IDENTIFIER_GLOBAL_VALUE (fn);
903 else
904 {
905 tree tmp;
906 tmp = tree_cons
907 (NULL_TREE, ptr_type_node, tree_cons
908 (NULL_TREE, const_string_type_node, tree_cons
909 (NULL_TREE, build_pointer_type (type_info_type_node),
910 void_list_node)));
911 tmp = build_function_type (void_type_node, tmp);
912
913 fn = build_lang_decl (FUNCTION_DECL, fn, tmp);
914 DECL_EXTERNAL (fn) = 1;
915 TREE_PUBLIC (fn) = 1;
916 DECL_ARTIFICIAL (fn) = 1;
917 pushdecl_top_level (fn);
918 make_function_rtl (fn);
919 }
920
921 mark_used (fn);
922 fn = build_call (fn, TREE_TYPE (TREE_TYPE (fn)), elems);
923 finish_expr_stmt (fn);
924 }
925
926 /* Build an initializer for a __class_type_info node. */
927
928 static void
929 expand_class_desc (tdecl, type)
930 tree tdecl;
931 tree type;
932 {
933 tree name_string;
934 tree fn, tmp;
935
936 int i = CLASSTYPE_N_BASECLASSES (type);
937 int base_cnt = 0;
938 tree binfos = TYPE_BINFO_BASETYPES (type);
939 #if 0
940 /* See code below that used these. */
941 tree vb = CLASSTYPE_VBASECLASSES (type);
942 int n_base = i;
943 #endif
944 tree base, elems, access, offset, isvir;
945 tree elt, elts = NULL_TREE;
946
947 if (base_desc_type_node == NULL_TREE)
948 {
949 tree fields [4];
950
951 /* A reasonably close approximation of __class_type_info::base_info */
952
953 base_desc_type_node = make_aggr_type (RECORD_TYPE);
954
955 /* Actually const __user_type_info * */
956 fields [0] = build_lang_decl
957 (FIELD_DECL, NULL_TREE,
958 build_pointer_type (build_qualified_type
959 (type_info_type_node,
960 TYPE_QUAL_CONST)));
961 fields [1] = build_lang_decl
962 (FIELD_DECL, NULL_TREE,
963 flag_new_abi ? intSI_type_node : unsigned_intSI_type_node);
964 DECL_BIT_FIELD (fields[1]) = 1;
965 DECL_FIELD_SIZE (fields[1]) = 29;
966
967 fields [2] = build_lang_decl
968 (FIELD_DECL, NULL_TREE, boolean_type_node);
969 DECL_BIT_FIELD (fields[2]) = 1;
970 DECL_FIELD_SIZE (fields[2]) = 1;
971
972 /* Actually enum access */
973 fields [3] = build_lang_decl
974 (FIELD_DECL, NULL_TREE, integer_type_node);
975 DECL_BIT_FIELD (fields[3]) = 1;
976 DECL_FIELD_SIZE (fields[3]) = 2;
977
978 finish_builtin_type (base_desc_type_node, "__base_info", fields,
979 3, ptr_type_node);
980 }
981
982 while (--i >= 0)
983 {
984 tree binfo = TREE_VEC_ELT (binfos, i);
985
986 finish_expr_stmt (get_typeid_1 (BINFO_TYPE (binfo)));
987 base = decay_conversion (get_tinfo_var (BINFO_TYPE (binfo)));
988 offset = get_base_offset (binfo, type);
989
990 if (TREE_VIA_PUBLIC (binfo))
991 access = access_public_node;
992 else if (TREE_VIA_PROTECTED (binfo))
993 access = access_protected_node;
994 else
995 access = access_private_node;
996 if (TREE_VIA_VIRTUAL (binfo))
997 isvir = boolean_true_node;
998 else
999 isvir = boolean_false_node;
1000
1001 elt = build
1002 (CONSTRUCTOR, base_desc_type_node, NULL_TREE, tree_cons
1003 (NULL_TREE, base, tree_cons
1004 (NULL_TREE, offset, tree_cons
1005 (NULL_TREE, isvir, tree_cons
1006 (NULL_TREE, access, NULL_TREE)))));
1007 TREE_HAS_CONSTRUCTOR (elt) = TREE_CONSTANT (elt) = TREE_STATIC (elt) = 1;
1008 elts = tree_cons (NULL_TREE, elt, elts);
1009 base_cnt++;
1010 }
1011 #if 0
1012 i = n_base;
1013 while (vb)
1014 {
1015 tree b;
1016 access = access_public_node;
1017 while (--i >= 0)
1018 {
1019 b = TREE_VEC_ELT (binfos, i);
1020 if (BINFO_TYPE (vb) == BINFO_TYPE (b) && TREE_VIA_VIRTUAL (b))
1021 {
1022 if (TREE_VIA_PUBLIC (b))
1023 access = access_public_node;
1024 else if (TREE_VIA_PROTECTED (b))
1025 access = access_protected_node;
1026 else
1027 access = access_private_node;
1028 break;
1029 }
1030 }
1031 base = build_t_desc (BINFO_TYPE (vb), 1);
1032 offset = BINFO_OFFSET (vb);
1033 isvir = build_int_2 (1, 0);
1034
1035 base_list = tree_cons (NULL_TREE, base, base_list);
1036 isvir_list = tree_cons (NULL_TREE, isvir, isvir_list);
1037 acc_list = tree_cons (NULL_TREE, access, acc_list);
1038 off_list = tree_cons (NULL_TREE, offset, off_list);
1039
1040 base_cnt++;
1041 vb = TREE_CHAIN (vb);
1042 }
1043 #endif
1044
1045 name_string = tinfo_name (type);
1046
1047 {
1048 tree arrtype = build_array_type (base_desc_type_node, NULL_TREE);
1049 elts = build (CONSTRUCTOR, arrtype, NULL_TREE, elts);
1050 TREE_HAS_CONSTRUCTOR (elts) = TREE_CONSTANT (elts)
1051 = TREE_STATIC (elts) = 1;
1052 complete_array_type (arrtype, elts, 1);
1053 }
1054
1055 elems = tree_cons
1056 (NULL_TREE, decay_conversion (tdecl), tree_cons
1057 (NULL_TREE, decay_conversion (name_string), tree_cons
1058 (NULL_TREE, decay_conversion (elts), tree_cons
1059 (NULL_TREE, cp_convert (sizetype, build_int_2 (base_cnt, 0)),
1060 NULL_TREE))));
1061
1062 fn = get_identifier ("__rtti_class");
1063 if (IDENTIFIER_GLOBAL_VALUE (fn))
1064 fn = IDENTIFIER_GLOBAL_VALUE (fn);
1065 else
1066 {
1067 tmp = tree_cons
1068 (NULL_TREE, ptr_type_node, tree_cons
1069 (NULL_TREE, const_string_type_node, tree_cons
1070 (NULL_TREE, build_pointer_type (base_desc_type_node), tree_cons
1071 (NULL_TREE, sizetype, void_list_node))));
1072 tmp = build_function_type (void_type_node, tmp);
1073
1074 fn = build_lang_decl (FUNCTION_DECL, fn, tmp);
1075 DECL_EXTERNAL (fn) = 1;
1076 TREE_PUBLIC (fn) = 1;
1077 DECL_ARTIFICIAL (fn) = 1;
1078 pushdecl_top_level (fn);
1079 make_function_rtl (fn);
1080 }
1081
1082 mark_used (fn);
1083 fn = build_call (fn, TREE_TYPE (TREE_TYPE (fn)), elems);
1084 finish_expr_stmt (fn);
1085 }
1086
1087 /* Build an initializer for a __pointer_type_info node. */
1088
1089 static void
1090 expand_ptr_desc (tdecl, type)
1091 tree tdecl;
1092 tree type;
1093 {
1094 tree t, elems, fn;
1095 tree name_string = tinfo_name (type);
1096
1097 type = TREE_TYPE (type);
1098 finish_expr_stmt (get_typeid_1 (type));
1099 t = decay_conversion (get_tinfo_var (type));
1100 elems = tree_cons
1101 (NULL_TREE, decay_conversion (tdecl), tree_cons
1102 (NULL_TREE, decay_conversion (name_string), tree_cons
1103 (NULL_TREE, t, NULL_TREE)));
1104
1105 fn = get_identifier ("__rtti_ptr");
1106 if (IDENTIFIER_GLOBAL_VALUE (fn))
1107 fn = IDENTIFIER_GLOBAL_VALUE (fn);
1108 else
1109 {
1110 tree tmp;
1111 tmp = tree_cons
1112 (NULL_TREE, ptr_type_node, tree_cons
1113 (NULL_TREE, const_string_type_node, tree_cons
1114 (NULL_TREE, build_pointer_type (type_info_type_node),
1115 void_list_node)));
1116 tmp = build_function_type (void_type_node, tmp);
1117
1118 fn = build_lang_decl (FUNCTION_DECL, fn, tmp);
1119 DECL_EXTERNAL (fn) = 1;
1120 TREE_PUBLIC (fn) = 1;
1121 DECL_ARTIFICIAL (fn) = 1;
1122 pushdecl_top_level (fn);
1123 make_function_rtl (fn);
1124 }
1125
1126 mark_used (fn);
1127 fn = build_call (fn, TREE_TYPE (TREE_TYPE (fn)), elems);
1128 finish_expr_stmt (fn);
1129 }
1130
1131 /* Build an initializer for a __attr_type_info node. */
1132
1133 static void
1134 expand_attr_desc (tdecl, type)
1135 tree tdecl;
1136 tree type;
1137 {
1138 tree elems, t, fn;
1139 tree name_string = tinfo_name (type);
1140 tree attrval = build_int_2 (TYPE_QUALS (type), 0);
1141
1142 finish_expr_stmt (get_typeid_1 (TYPE_MAIN_VARIANT (type)));
1143 t = decay_conversion (get_tinfo_var (TYPE_MAIN_VARIANT (type)));
1144 elems = tree_cons
1145 (NULL_TREE, decay_conversion (tdecl), tree_cons
1146 (NULL_TREE, decay_conversion (name_string), tree_cons
1147 (NULL_TREE, attrval, tree_cons (NULL_TREE, t, NULL_TREE))));
1148
1149 fn = get_identifier ("__rtti_attr");
1150 if (IDENTIFIER_GLOBAL_VALUE (fn))
1151 fn = IDENTIFIER_GLOBAL_VALUE (fn);
1152 else
1153 {
1154 tree tmp;
1155 tmp = tree_cons
1156 (NULL_TREE, ptr_type_node, tree_cons
1157 (NULL_TREE, const_string_type_node, tree_cons
1158 (NULL_TREE, integer_type_node, tree_cons
1159 (NULL_TREE, build_pointer_type (type_info_type_node),
1160 void_list_node))));
1161 tmp = build_function_type (void_type_node, tmp);
1162
1163 fn = build_lang_decl (FUNCTION_DECL, fn, tmp);
1164 DECL_EXTERNAL (fn) = 1;
1165 TREE_PUBLIC (fn) = 1;
1166 DECL_ARTIFICIAL (fn) = 1;
1167 pushdecl_top_level (fn);
1168 make_function_rtl (fn);
1169 }
1170
1171 mark_used (fn);
1172 fn = build_call (fn, TREE_TYPE (TREE_TYPE (fn)), elems);
1173 finish_expr_stmt (fn);
1174 }
1175
1176 /* Build an initializer for a type_info node that just has a name. */
1177
1178 static void
1179 expand_generic_desc (tdecl, type, fnname)
1180 tree tdecl;
1181 tree type;
1182 const char *fnname;
1183 {
1184 tree name_string = tinfo_name (type);
1185 tree elems = tree_cons
1186 (NULL_TREE, decay_conversion (tdecl), tree_cons
1187 (NULL_TREE, decay_conversion (name_string), NULL_TREE));
1188
1189 tree fn = get_identifier (fnname);
1190 if (IDENTIFIER_GLOBAL_VALUE (fn))
1191 fn = IDENTIFIER_GLOBAL_VALUE (fn);
1192 else
1193 {
1194 tree tmp;
1195 tmp = tree_cons
1196 (NULL_TREE, ptr_type_node, tree_cons
1197 (NULL_TREE, const_string_type_node, void_list_node));
1198 tmp = build_function_type (void_type_node, tmp);
1199
1200 fn = build_lang_decl (FUNCTION_DECL, fn, tmp);
1201 DECL_EXTERNAL (fn) = 1;
1202 TREE_PUBLIC (fn) = 1;
1203 DECL_ARTIFICIAL (fn) = 1;
1204 pushdecl_top_level (fn);
1205 make_function_rtl (fn);
1206 }
1207
1208 mark_used (fn);
1209 fn = build_call (fn, TREE_TYPE (TREE_TYPE (fn)), elems);
1210 finish_expr_stmt (fn);
1211 }
1212
1213 /* Generate the code for a type_info initialization function.
1214 Note that we take advantage of the passage
1215
1216 5.2.7 Type identification [expr.typeid]
1217
1218 Whether or not the destructor is called for the type_info object at the
1219 end of the program is unspecified.
1220
1221 and don't bother to arrange for these objects to be destroyed. It
1222 doesn't matter, anyway, since the destructors don't do anything.
1223
1224 This must only be called from toplevel (i.e. from finish_file)! */
1225
1226 void
1227 synthesize_tinfo_fn (fndecl)
1228 tree fndecl;
1229 {
1230 tree type = TREE_TYPE (DECL_NAME (fndecl));
1231 tree tmp, addr, tdecl;
1232 tree compound_stmt;
1233 tree if_stmt;
1234 tree then_clause;
1235
1236 my_friendly_assert (!new_abi_rtti_p (), 20000118);
1237 if (at_eof)
1238 {
1239 import_export_decl (fndecl);
1240 if (DECL_REALLY_EXTERN (fndecl))
1241 return;
1242 }
1243
1244 /* Declare the static typeinfo variable. */
1245 tdecl = get_tinfo_var (type);
1246 DECL_EXTERNAL (tdecl) = 0;
1247 TREE_STATIC (tdecl) = 1;
1248 DECL_COMMON (tdecl) = 1;
1249 TREE_USED (tdecl) = 1;
1250 DECL_ALIGN (tdecl) = TYPE_ALIGN (ptr_type_node);
1251 cp_finish_decl (tdecl, NULL_TREE, NULL_TREE, 0);
1252
1253 /* Begin processing the function. */
1254 start_function (NULL_TREE, fndecl, NULL_TREE,
1255 SF_DEFAULT | SF_PRE_PARSED);
1256 DECL_DEFER_OUTPUT (fndecl) = 1;
1257 store_parm_decls ();
1258 clear_last_expr ();
1259
1260 /* Begin the body of the function. */
1261 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
1262
1263 /* For convenience, we save away the address of the static
1264 variable. */
1265 addr = decay_conversion (tdecl);
1266
1267 /* If the first word of the array (the vtable) is non-zero, we've already
1268 initialized the object, so don't do it again. */
1269 if_stmt = begin_if_stmt ();
1270 tmp = cp_convert (build_pointer_type (ptr_type_node), addr);
1271 tmp = build_indirect_ref (tmp, 0);
1272 tmp = build_binary_op (EQ_EXPR, tmp, integer_zero_node);
1273 finish_if_stmt_cond (tmp, if_stmt);
1274 then_clause = begin_compound_stmt (/*has_no_scope=*/0);
1275
1276 if (TREE_CODE (type) == FUNCTION_TYPE)
1277 expand_generic_desc (tdecl, type, "__rtti_func");
1278 else if (TREE_CODE (type) == ARRAY_TYPE)
1279 expand_generic_desc (tdecl, type, "__rtti_array");
1280 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
1281 expand_attr_desc (tdecl, type);
1282 else if (TREE_CODE (type) == POINTER_TYPE)
1283 {
1284 if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE)
1285 expand_generic_desc (tdecl, type, "__rtti_ptmd");
1286 else if (TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
1287 expand_generic_desc (tdecl, type, "__rtti_ptmf");
1288 else
1289 expand_ptr_desc (tdecl, type);
1290 }
1291 else if (TYPE_PTRMEMFUNC_P (type))
1292 expand_generic_desc (tdecl, type, "__rtti_ptmf");
1293 else if (IS_AGGR_TYPE (type))
1294 {
1295 if (CLASSTYPE_N_BASECLASSES (type) == 0)
1296 expand_generic_desc (tdecl, type, "__rtti_user");
1297 else if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
1298 && (TREE_VIA_PUBLIC
1299 (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), 0))))
1300 expand_si_desc (tdecl, type);
1301 else
1302 expand_class_desc (tdecl, type);
1303 }
1304 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1305 expand_generic_desc (tdecl, type, "__rtti_user");
1306 else
1307 my_friendly_abort (252);
1308
1309 finish_compound_stmt (/*has_no_scope=*/0, then_clause);
1310 finish_then_clause (if_stmt);
1311 finish_if_stmt ();
1312
1313 /* OK, now return the type_info object. */
1314 tmp = cp_convert (build_pointer_type (type_info_type_node), addr);
1315 tmp = build_indirect_ref (tmp, 0);
1316 finish_return_stmt (tmp);
1317 /* Finish the function body. */
1318 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
1319 expand_body (finish_function (lineno, 0));
1320 }
1321
1322 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
1323
1324 static int
1325 qualifier_flags (type)
1326 tree type;
1327 {
1328 int flags = 0;
1329 /* we want the qualifiers on this type, not any array core, it might have */
1330 int quals = TYPE_QUALS (type);
1331
1332 if (quals & TYPE_QUAL_CONST)
1333 flags |= 1;
1334 if (quals & TYPE_QUAL_VOLATILE)
1335 flags |= 2;
1336 return flags;
1337 }
1338
1339 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
1340 is the vtable pointer and NTBS name. */
1341
1342 static tree
1343 tinfo_base_init (desc, target)
1344 tree desc;
1345 tree target;
1346 {
1347 tree name_string = tinfo_name (target);
1348 tree init = NULL_TREE;
1349
1350 if (TINFO_VTABLE_DECL (desc))
1351 {
1352 tree vtbl_ptr = build_unary_op (ADDR_EXPR, TINFO_VTABLE_DECL (desc), 0);
1353
1354 init = tree_cons (NULL_TREE, vtbl_ptr, init);
1355 }
1356
1357 init = tree_cons (NULL_TREE, decay_conversion (name_string), init);
1358
1359 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse(init));
1360 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
1361 init = tree_cons (NULL_TREE, init, NULL_TREE);
1362
1363 return init;
1364 }
1365
1366 /* Return the CONSTRUCTOR expr for a type_info of TYPE. DESC provides the
1367 information about the particular type_info derivation, which adds no
1368 additional fields to the type_info base. */
1369
1370 static tree
1371 generic_initializer (desc, target)
1372 tree desc;
1373 tree target;
1374 {
1375 tree init = tinfo_base_init (desc, target);
1376
1377 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, init);
1378 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
1379 return init;
1380 }
1381
1382 /* Return the CONSTRUCTOR expr for a type_info of pointer or reference TYPE.
1383 DESC provides information about the particular type_info derivation,
1384 which adds target type and qualifier flags members to the type_info base. */
1385
1386 static tree
1387 ptr_ref_initializer (desc, target)
1388 tree desc;
1389 tree target;
1390 {
1391 tree init = tinfo_base_init (desc, target);
1392 tree to = TREE_TYPE (target);
1393 int flags = qualifier_flags (to);
1394
1395 init = tree_cons (NULL_TREE, build_int_2 (flags, 0), init);
1396 init = tree_cons (NULL_TREE,
1397 build_unary_op (ADDR_EXPR,
1398 get_tinfo_decl (TYPE_MAIN_VARIANT (to)), 0),
1399 init);
1400
1401 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
1402 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
1403 return init;
1404 }
1405
1406 /* Return the CONSTRUCTOR expr for a type_info of pointer or reference TYPE.
1407 DESC provides information about the particular type_info derivation,
1408 which adds target type and qualifier flags members to the type_info base. */
1409
1410 static tree
1411 ptmd_initializer (desc, target)
1412 tree desc;
1413 tree target;
1414 {
1415 tree init = tinfo_base_init (desc, target);
1416 tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
1417 tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
1418 int flags = qualifier_flags (to);
1419
1420 init = tree_cons (NULL_TREE,
1421 build_unary_op (ADDR_EXPR, get_tinfo_decl (klass), 0),
1422 init);
1423 init = tree_cons (NULL_TREE,
1424 build_unary_op (ADDR_EXPR,
1425 get_tinfo_decl (TYPE_MAIN_VARIANT (to)), 0),
1426 init);
1427 init = tree_cons (NULL_TREE, build_int_2 (flags, 0), init);
1428
1429 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
1430 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
1431 return init;
1432 }
1433
1434 /* Determine the hint flags describing the features of a class's heirarchy.
1435 FIXME: better set the hint_flags here! For now set them
1436 to safe 'don't know' values. The specification is under
1437 review. Don't forget to check the runtime dynamic_cast and
1438 catch machinery if these change. */
1439
1440 static int
1441 class_hint_flags (type)
1442 tree type;
1443 {
1444 int hint_flags = 0;
1445 hint_flags |= 0x1; /* contains multiply inherited sub object */
1446 hint_flags |= 0x4; /* has virtual bases */
1447 hint_flags |= 0x8; /* has private base */
1448 if (TYPE_POLYMORPHIC_P (type))
1449 hint_flags |= 0x2;
1450
1451 return hint_flags;
1452 }
1453
1454 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
1455 DESC provides information about the particular __class_type_info derivation,
1456 which adds hint flags and TRAIL initializers to the type_info base. */
1457
1458 static tree
1459 class_initializer (desc, target, trail)
1460 tree desc;
1461 tree target;
1462 tree trail;
1463 {
1464 tree init = tinfo_base_init (desc, target);
1465 int flags = class_hint_flags (target);
1466
1467 trail = tree_cons (NULL_TREE, build_int_2 (flags, 0), trail);
1468 TREE_CHAIN (init) = trail;
1469 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, init);
1470 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
1471 return init;
1472 }
1473
1474 /* Generate a pseudo_type_info VAR_DECL suitable for the supplied
1475 TARGET_TYPE and given the REAL_NAME. This is the structure expected by
1476 the runtime, and therefore has additional fields. If we need not emit a
1477 definition (because the runtime must contain it), return NULL_TREE,
1478 otherwise return the VAR_DECL. */
1479
1480 static tree
1481 synthesize_tinfo_var (target_type, real_name)
1482 tree target_type;
1483 tree real_name;
1484 {
1485 tree var_init = NULL_TREE;
1486 tree var_type = NULL_TREE;
1487
1488 my_friendly_assert (new_abi_rtti_p (), 20000118);
1489
1490 switch (TREE_CODE (target_type))
1491 {
1492 case POINTER_TYPE:
1493 if (TYPE_PTRMEM_P (target_type))
1494 {
1495 var_type = ptmd_desc_type_node;
1496 var_init = ptmd_initializer (var_type, target_type);
1497 }
1498 else
1499 {
1500 int code = TREE_CODE (TREE_TYPE (target_type));
1501
1502 if ((CP_TYPE_QUALS (TREE_TYPE (target_type)) | TYPE_QUAL_CONST)
1503 == TYPE_QUAL_CONST
1504 && (code == INTEGER_TYPE || code == BOOLEAN_TYPE
1505 || code == CHAR_TYPE || code == REAL_TYPE
1506 || code == VOID_TYPE)
1507 && !doing_runtime)
1508 /* These are in the runtime. */
1509 return NULL_TREE;
1510 var_type = ptr_desc_type_node;
1511 var_init = ptr_ref_initializer (var_type, target_type);
1512 }
1513 break;
1514 case REFERENCE_TYPE:
1515 var_type = ref_desc_type_node;
1516 var_init = ptr_ref_initializer (var_type, target_type);
1517 break;
1518 case ENUMERAL_TYPE:
1519 var_type = enum_desc_type_node;
1520 var_init = generic_initializer (var_type, target_type);
1521 break;
1522 case FUNCTION_TYPE:
1523 var_type = func_desc_type_node;
1524 var_init = generic_initializer (var_type, target_type);
1525 break;
1526 case ARRAY_TYPE:
1527 var_type = ary_desc_type_node;
1528 var_init = generic_initializer (var_type, target_type);
1529 break;
1530 case UNION_TYPE:
1531 case RECORD_TYPE:
1532 if (!TYPE_SIZE (target_type))
1533 {
1534 /* FIXME: incomplete type. Awaiting specification. */
1535 return NULL_TREE;
1536 }
1537 else if (!CLASSTYPE_N_BASECLASSES (target_type))
1538 {
1539 var_type = class_desc_type_node;
1540 var_init = class_initializer (var_type, target_type, NULL_TREE);
1541 }
1542 else
1543 {
1544 /* if this has a single public non-virtual base, it's easier */
1545 tree binfo = TYPE_BINFO (target_type);
1546 int nbases = BINFO_N_BASETYPES (binfo);
1547 tree base_binfos = BINFO_BASETYPES (binfo);
1548 tree base_inits = NULL_TREE;
1549 int is_simple = nbases == 1;
1550 int ix;
1551
1552 /* Generate the base information initializer. */
1553 for (ix = nbases; ix--;)
1554 {
1555 tree base_binfo = TREE_VEC_ELT (base_binfos, ix);
1556 tree base_init = NULL_TREE;
1557 int flags = 0;
1558 tree tinfo;
1559 tree offset;
1560
1561 if (TREE_VIA_VIRTUAL (base_binfo))
1562 flags |= 1;
1563 if (TREE_PUBLIC (base_binfo))
1564 flags |= 2;
1565 tinfo = get_tinfo_decl (BINFO_TYPE (base_binfo));
1566 tinfo = build_unary_op (ADDR_EXPR, tinfo, 0);
1567 offset = get_base_offset (base_binfo, target_type);
1568
1569 /* is it a single public inheritance? */
1570 if (is_simple && flags == 2 && integer_zerop (offset))
1571 {
1572 base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1573 break;
1574 }
1575 is_simple = 0;
1576
1577 base_init = tree_cons
1578 (NULL_TREE, build_int_2 (flags, 0), base_init);
1579 base_init = tree_cons (NULL_TREE, offset, base_init);
1580 base_init = tree_cons (NULL_TREE, tinfo, base_init);
1581 base_init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, base_init);
1582 base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1583 }
1584
1585 if (is_simple)
1586 var_type = si_class_desc_type_node;
1587 else
1588 {
1589 /* Prepend the number of bases. */
1590 base_inits = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, base_inits);
1591 base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1592 base_inits = tree_cons (NULL_TREE,
1593 build_int_2 (nbases, 0), base_inits);
1594
1595 var_type = get_vmi_pseudo_type_info (nbases);
1596 }
1597 var_init = class_initializer (var_type, target_type, base_inits);
1598 }
1599 break;
1600 case INTEGER_TYPE:
1601 case BOOLEAN_TYPE:
1602 case CHAR_TYPE:
1603 case REAL_TYPE:
1604 case VOID_TYPE:
1605 if (!doing_runtime)
1606 /* These are guaranteed to be in the runtime. */
1607 return NULL_TREE;
1608 var_type = bltn_desc_type_node;
1609 var_init = generic_initializer (var_type, target_type);
1610 break;
1611 default:
1612 my_friendly_abort (20000117);
1613 }
1614
1615 return create_real_tinfo_var (real_name, TINFO_PSEUDO_TYPE (var_type), var_init);
1616 }
1617
1618 /* Create the real typeinfo variable. */
1619
1620 static tree
1621 create_real_tinfo_var (name, type, init)
1622 tree name;
1623 tree type;
1624 tree init;
1625 {
1626 tree decl;
1627
1628 decl = build_lang_decl (VAR_DECL, name,
1629 build_qualified_type (type, TYPE_QUAL_CONST));
1630 DECL_ARTIFICIAL (decl) = 1;
1631 TREE_READONLY (decl) = 1;
1632 TREE_STATIC (decl) = 1;
1633 TREE_PUBLIC (decl) = 1;
1634 DECL_EXTERNAL (decl) = 0;
1635
1636 comdat_linkage (decl);
1637 DECL_ASSEMBLER_NAME (decl) = name;
1638 DECL_INITIAL (decl) = init;
1639 cp_finish_decl (decl, init, NULL_TREE, 0);
1640
1641 return decl;
1642 }
1643
1644 /* Generate the RECORD_TYPE containing the data layout of a type_info
1645 derivative as used by the runtime. This layout must be consistent with
1646 that defined in the runtime support. Also generate the VAR_DECL for the
1647 type's vtable. We explicitly manage the vtable member, and name it for
1648 real type as used in the runtime. The RECORD type has a different name,
1649 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1650 is the generated type and TINFO_VTABLE_DECL is the vtable decl.
1651
1652 REAL_NAME is the runtime's name of the type. Trailing arguments are
1653 additional FIELD_DECL's for the structure. The final argument must be
1654 NULL. */
1655
1656 static tree
1657 create_pseudo_type_info VPARAMS((const char *real_name, int ident, ...))
1658 {
1659 #ifndef ANSI_PROTOTYPES
1660 char const *real_name;
1661 int ident;
1662 #endif
1663 va_list ap;
1664 tree real_type, pseudo_type;
1665 char *pseudo_name;
1666 tree vtable_decl;
1667 int ix;
1668 tree fields[10];
1669 tree field_decl;
1670 tree result;
1671
1672 VA_START (ap, ident);
1673 #ifndef ANSI_PROTOTYPES
1674 real_name = va_arg (ap, char const *);
1675 ident = va_arg (app, int);
1676 #endif
1677
1678 /* Generate the pseudo type name. */
1679 pseudo_name = (char *)alloca (strlen (real_name) + 30);
1680 strcpy (pseudo_name, real_name);
1681 strcat (pseudo_name, "_pseudo");
1682 if (ident)
1683 sprintf (pseudo_name + strlen (pseudo_name), "%d", ident);
1684
1685 /* Get the vtable decl. */
1686 real_type = xref_tag (class_type_node, get_identifier (real_name), 1);
1687 vtable_decl = get_vtable_decl (real_type, /*complete=*/1);
1688
1689 /* First field is the pseudo type_info base class. */
1690 fields[0] = build_lang_decl (FIELD_DECL, NULL_TREE, ti_desc_type_node);
1691
1692 /* Now add the derived fields. */
1693 for (ix = 0; (field_decl = va_arg (ap, tree));)
1694 fields[++ix] = field_decl;
1695
1696 /* Create the pseudo type. */
1697 pseudo_type = make_aggr_type (RECORD_TYPE);
1698 finish_builtin_type (pseudo_type, pseudo_name, fields, ix, ptr_type_node);
1699 TYPE_HAS_CONSTRUCTOR (pseudo_type) = 1;
1700 va_end (ap);
1701
1702 result = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
1703 TINFO_VTABLE_DECL (result) = vtable_decl;
1704 TINFO_PSEUDO_TYPE (result) = pseudo_type;
1705
1706 return result;
1707 }
1708
1709 /* Return a descriptor for a vmi type with NUM_BASES bases. */
1710
1711 static tree
1712 get_vmi_pseudo_type_info (num_bases)
1713 int num_bases;
1714 {
1715 tree desc;
1716 tree array_domain, base_array;
1717
1718 if (TREE_VEC_LENGTH (vmi_class_desc_type_node) <= num_bases)
1719 {
1720 int ix;
1721 tree extend = make_tree_vec (num_bases + 5);
1722
1723 for (ix = TREE_VEC_LENGTH (vmi_class_desc_type_node); ix--;)
1724 TREE_VEC_ELT (extend, ix) = TREE_VEC_ELT (vmi_class_desc_type_node, ix);
1725 vmi_class_desc_type_node = extend;
1726 }
1727 desc = TREE_VEC_ELT (vmi_class_desc_type_node, num_bases);
1728
1729 if (desc)
1730 return desc;
1731
1732 /* Add number of bases and trailing array of base_class_type_info. */
1733 array_domain = build_index_type (build_int_2 (num_bases, 0));
1734 base_array = build_array_type (base_desc_type_node, array_domain);
1735
1736 desc = create_pseudo_type_info
1737 ("__vmi_class_type_info", num_bases,
1738 build_lang_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1739 build_lang_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1740 build_lang_decl (FIELD_DECL, NULL_TREE, base_array),
1741 NULL);
1742 TREE_VEC_ELT (vmi_class_desc_type_node, num_bases) = desc;
1743 return desc;
1744 }
1745
1746 /* Make sure the required builtin types exist for generating the type_info
1747 varable definitions. */
1748
1749 static void
1750 create_tinfo_types ()
1751 {
1752 tree ptr_type_info;
1753
1754 if (bltn_desc_type_node)
1755 return;
1756 if (flag_honor_std)
1757 push_namespace (get_identifier ("std"));
1758
1759 ptr_type_info = build_pointer_type
1760 (build_qualified_type
1761 (type_info_type_node, TYPE_QUAL_CONST));
1762
1763 /* Create the internal type_info structure. This is used as a base for
1764 the other structures. */
1765 {
1766 tree fields[2];
1767
1768 ti_desc_type_node = make_aggr_type (RECORD_TYPE);
1769 fields[0] = build_lang_decl (FIELD_DECL, NULL_TREE, const_ptr_type_node);
1770 fields[1] = build_lang_decl (FIELD_DECL, NULL_TREE, const_string_type_node);
1771 finish_builtin_type (ti_desc_type_node, "__type_info_pseudo",
1772 fields, 1, ptr_type_node);
1773 TYPE_HAS_CONSTRUCTOR (ti_desc_type_node) = 1;
1774 }
1775
1776 /* Fundamental type_info */
1777 bltn_desc_type_node = create_pseudo_type_info
1778 ("__fundamental_type_info", 0,
1779 NULL);
1780
1781 /* Pointer and reference type_info. These two fields, qualification mask
1782 and pointer to the pointed to (referenced) type. */
1783 ptr_desc_type_node = create_pseudo_type_info
1784 ("__pointer_type_info", 0,
1785 build_lang_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1786 build_lang_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1787 NULL);
1788 ref_desc_type_node = create_pseudo_type_info
1789 ("__reference_type_info", 0,
1790 build_lang_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1791 build_lang_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1792 NULL);
1793
1794 /* Array, function and enum type_info. No additional fields. */
1795 ary_desc_type_node = create_pseudo_type_info
1796 ("__array_type_info", 0,
1797 NULL);
1798 func_desc_type_node = create_pseudo_type_info
1799 ("__function_type_info", 0,
1800 NULL);
1801 enum_desc_type_node = create_pseudo_type_info
1802 ("__enum_type_info", 0,
1803 NULL);
1804
1805 /* Class type_info. Add a flags field. */
1806 class_desc_type_node = create_pseudo_type_info
1807 ("__class_type_info", 0,
1808 build_lang_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1809 NULL);
1810
1811 /* Single public non-virtual base class. Add pointer to base class. */
1812 si_class_desc_type_node = create_pseudo_type_info
1813 ("__si_class_type_info", 0,
1814 build_lang_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1815 build_lang_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1816 NULL);
1817
1818 /* Base class internal helper. Pointer to base type, offset to base,
1819 flags. */
1820 {
1821 tree fields[3];
1822
1823 fields[0] = build_lang_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1824 fields[1] = build_lang_decl (FIELD_DECL, NULL_TREE, ptrdiff_type_node),
1825 fields[2] = build_lang_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1826 base_desc_type_node = make_aggr_type (RECORD_TYPE);
1827 finish_builtin_type (base_desc_type_node, "__base_class_type_info_pseudo",
1828 fields, 2, ptr_type_node);
1829 TYPE_HAS_CONSTRUCTOR (base_desc_type_node) = 1;
1830 }
1831
1832 /* General heirarchy is created as necessary in this vector. */
1833 vmi_class_desc_type_node = make_tree_vec (10);
1834
1835 /* Pointer to member data type_info. Add pointer to the class, pointer
1836 to the member's type info and qualifications flags. */
1837 ptmd_desc_type_node = create_pseudo_type_info
1838 ("__ptr_to_member_type_info", 0,
1839 build_lang_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1840 build_lang_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1841 build_lang_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1842 NULL);
1843
1844 if (flag_honor_std)
1845 pop_namespace ();
1846 }
1847
1848 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1849 support. Generating them here guarantees consistency with the other
1850 structures. We use the following heuristic to determine when the runtime
1851 is being generated. If std::__fundamental_type_info is defined, and it's
1852 destructor is defined, then the runtime is being built. */
1853
1854 void
1855 emit_support_tinfos ()
1856 {
1857 static tree *const fundamentals[] =
1858 {
1859 &void_type_node,
1860 &boolean_type_node,
1861 &wchar_type_node,
1862 #if 0
1863 &signed_wchar_type_node, &unsigned_wchar_type_node,
1864 #endif
1865 &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1866 &short_integer_type_node, &short_unsigned_type_node,
1867 &integer_type_node, &unsigned_type_node,
1868 &long_integer_type_node, &long_unsigned_type_node,
1869 &long_long_integer_type_node, &long_long_unsigned_type_node,
1870 &float_type_node, &double_type_node, &long_double_type_node,
1871
1872 /* GCC extension types */
1873 #if 0
1874 &complex_integer_type_node,
1875 &complex_float_type_node, &complex_double_type_node,
1876 &complex_long_double_type_node,
1877 #endif
1878
1879 0
1880 };
1881 int ix;
1882 tree bltn_type, dtor;
1883
1884 if (flag_honor_std)
1885 push_namespace (get_identifier ("std"));
1886 bltn_type = xref_tag (class_type_node,
1887 get_identifier ("__fundamental_type_info"), 1);
1888 if (flag_honor_std)
1889 pop_namespace ();
1890 if (!TYPE_SIZE (bltn_type))
1891 return;
1892 dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (bltn_type), 1);
1893 if (DECL_EXTERNAL (dtor))
1894 return;
1895 doing_runtime = 1;
1896 for (ix = 0; fundamentals[ix]; ix++)
1897 {
1898 tree bltn = *fundamentals[ix];
1899 tree bltn_ptr = build_pointer_type (bltn);
1900 tree bltn_const_ptr = build_pointer_type
1901 (build_qualified_type (bltn, TYPE_QUAL_CONST));
1902 tree tinfo;
1903
1904 tinfo = get_tinfo_decl (bltn);
1905 TREE_USED (tinfo) = 1;
1906 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1907
1908 tinfo = get_tinfo_decl (bltn_ptr);
1909 TREE_USED (tinfo) = 1;
1910 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1911
1912 tinfo = get_tinfo_decl (bltn_const_ptr);
1913 TREE_USED (tinfo) = 1;
1914 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1915 }
1916 }
1917
1918 /* Return non-zero, iff T is a type_info variable which has not had a
1919 definition emitted for it. */
1920
1921 int
1922 tinfo_decl_p (t, data)
1923 tree t;
1924 void *data ATTRIBUTE_UNUSED;
1925 {
1926 return TREE_CODE (t) == VAR_DECL
1927 && IDENTIFIER_GLOBAL_VALUE (DECL_NAME (t)) == (t)
1928 && TREE_TYPE (t) == tinfo_decl_type
1929 && TREE_TYPE (DECL_NAME (t));
1930 }
1931
1932 /* Emit a suitable type_info definition for the type_info decl pointed to by
1933 DECL_PTR. We emit a completely new variable, of the correct type for the
1934 actual type this is describing. The DECL_ASSEMBLER_NAME of the generated
1935 definition is set to that of the supplied decl, so that they can be tied
1936 up. Mark the supplied decl as having been dealt with. Emitting one
1937 definitions might cause other declarations to be emitted.
1938
1939 We need to do things this way, because we're trying to do something like
1940
1941 struct B : A {
1942 ...
1943 };
1944
1945 extern const A tinfo_var;
1946
1947 const B tinfo_var = {...};
1948
1949 which is not permitted. Also, we've not necessarily seen the definition of B.
1950 So we do something like the following,
1951
1952 extern const A tinfo_var;
1953
1954 struct pseudo_A {
1955 const void *vtable_ptr;
1956 const char *name;
1957 };
1958 struct pseudo_B {
1959 pseudo_A base;
1960 ...
1961 };
1962
1963 const pseudo_B proxy_tinfo_var attribute((assembler_name="tinfo_var")) =
1964 {
1965 {&B::vtable, "..."},
1966 ...
1967 };
1968
1969 pseudo_A and pseudo_B must be layout equivalent to the real definitions in
1970 the runtime. */
1971
1972 int
1973 emit_tinfo_decl (decl_ptr, data)
1974 tree *decl_ptr;
1975 void *data ATTRIBUTE_UNUSED;
1976 {
1977 tree tinfo_decl = *decl_ptr;
1978 tree tinfo_type, decl;
1979
1980 my_friendly_assert (TREE_TYPE (tinfo_decl) == tinfo_decl_type, 20000121);
1981 tinfo_type = TREE_TYPE (DECL_NAME (tinfo_decl));
1982 my_friendly_assert (tinfo_type != NULL_TREE, 20000120);
1983
1984 /* Say we've dealt with it. */
1985 TREE_TYPE (DECL_NAME (tinfo_decl)) = NULL_TREE;
1986
1987 if (!DECL_NEEDED_P (tinfo_decl))
1988 return 0;
1989 create_tinfo_types ();
1990 decl = synthesize_tinfo_var (tinfo_type, DECL_ASSEMBLER_NAME (tinfo_decl));
1991
1992 return decl != 0;
1993 }