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