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