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