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