cp-tree.h (build_scoped_method_call): Remove.
[gcc.git] / gcc / cp / rtti.c
1 /* RunTime Type Identification
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Mostly written by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC 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 GCC 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 GCC; 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 "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "flags.h"
31 #include "output.h"
32 #include "assert.h"
33 #include "toplev.h"
34
35 /* C++ returns type information to the user in struct type_info
36 objects. We also use type information to implement dynamic_cast and
37 exception handlers. Type information for a particular type is
38 indicated with an ABI defined structure derived from type_info.
39 This would all be very straight forward, but for the fact that the
40 runtime library provides the definitions of the type_info structure
41 and the ABI defined derived classes. We cannot build declarations
42 of them directly in the compiler, but we need to layout objects of
43 their type. Somewhere we have to lie.
44
45 We define layout compatible POD-structs with compiler-defined names
46 and generate the appropriate initializations for them (complete
47 with explicit mention of their vtable). When we have to provide a
48 type_info to the user we reinterpret_cast the internal compiler
49 type to type_info. A well formed program can only explicitly refer
50 to the type_infos of complete types (& cv void). However, we chain
51 pointer type_infos to the pointed-to-type, and that can be
52 incomplete. We only need the addresses of such incomplete
53 type_info objects for static initialization.
54
55 The type information VAR_DECL of a type is held on the
56 IDENTIFIER_GLOBAL_VALUE of the type's mangled name. That VAR_DECL
57 will be the internal type. It will usually have the correct
58 internal type reflecting the kind of type it represents (pointer,
59 array, function, class, inherited class, etc). When the type it
60 represents is incomplete, it will have the internal type
61 corresponding to type_info. That will only happen at the end of
62 translation, when we are emitting the type info objects. */
63
64 /* Accessors for the type_info objects. We need to remember several things
65 about each of the type_info types. The global tree nodes such as
66 bltn_desc_type_node are TREE_LISTs, and these macros are used to access
67 the required information. */
68 /* The RECORD_TYPE of a type_info derived class. */
69 #define TINFO_PSEUDO_TYPE(NODE) TREE_TYPE (NODE)
70 /* The VAR_DECL of the vtable for the type_info derived class.
71 This is only filled in at the end of the translation. */
72 #define TINFO_VTABLE_DECL(NODE) TREE_VALUE (NODE)
73 /* The IDENTIFIER_NODE naming the real class. */
74 #define TINFO_REAL_NAME(NODE) TREE_PURPOSE (NODE)
75
76 /* A varray of all tinfo decls that haven't yet been emitted. */
77 varray_type unemitted_tinfo_decls;
78
79 static tree build_headof (tree);
80 static tree ifnonnull (tree, tree);
81 static tree tinfo_name (tree);
82 static tree build_dynamic_cast_1 (tree, tree);
83 static tree throw_bad_cast (void);
84 static tree throw_bad_typeid (void);
85 static tree get_tinfo_decl_dynamic (tree);
86 static tree get_tinfo_ptr (tree);
87 static bool typeid_ok_p (void);
88 static int qualifier_flags (tree);
89 static bool target_incomplete_p (tree);
90 static tree tinfo_base_init (tree, tree);
91 static tree generic_initializer (tree, tree);
92 static tree ptr_initializer (tree, tree, bool *);
93 static tree ptm_initializer (tree, tree, bool *);
94 static tree dfs_class_hint_mark (tree, void *);
95 static tree dfs_class_hint_unmark (tree, void *);
96 static int class_hint_flags (tree);
97 static tree class_initializer (tree, tree, tree);
98 static tree create_pseudo_type_info (const char *, int, ...);
99 static tree get_pseudo_ti_init (tree, tree, bool *);
100 static tree get_pseudo_ti_desc (tree);
101 static void create_tinfo_types (void);
102 static bool typeinfo_in_lib_p (tree);
103 static bool unemitted_tinfo_decl_p (tree);
104
105 static int doing_runtime = 0;
106 \f
107
108 /* Declare language defined type_info type and a pointer to const
109 type_info. This is incomplete here, and will be completed when
110 the user #includes <typeinfo>. There are language defined
111 restrictions on what can be done until that is included. Create
112 the internal versions of the ABI types. */
113
114 void
115 init_rtti_processing (void)
116 {
117 tree const_type_info_type;
118
119 push_namespace (std_identifier);
120 type_info_type_node
121 = xref_tag (class_type, get_identifier ("type_info"),
122 /*attributes=*/NULL_TREE, 1);
123 pop_namespace ();
124 const_type_info_type = build_qualified_type (type_info_type_node,
125 TYPE_QUAL_CONST);
126 type_info_ptr_type = build_pointer_type (const_type_info_type);
127 type_info_ref_type = build_reference_type (const_type_info_type);
128
129 VARRAY_TREE_INIT (unemitted_tinfo_decls, 10, "RTTI decls");
130
131 create_tinfo_types ();
132 }
133
134 /* Given the expression EXP of type `class *', return the head of the
135 object pointed to by EXP with type cv void*, if the class has any
136 virtual functions (TYPE_POLYMORPHIC_P), else just return the
137 expression. */
138
139 static tree
140 build_headof (tree exp)
141 {
142 tree type = TREE_TYPE (exp);
143 tree offset;
144 tree index;
145
146 my_friendly_assert (TREE_CODE (type) == POINTER_TYPE, 20000112);
147 type = TREE_TYPE (type);
148
149 if (!TYPE_POLYMORPHIC_P (type))
150 return exp;
151
152 /* We use this a couple of times below, protect it. */
153 exp = save_expr (exp);
154
155 /* The offset-to-top field is at index -2 from the vptr. */
156 index = build_int_2 (-2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE, -1);
157
158 offset = build_vtbl_ref (build_indirect_ref (exp, NULL), index);
159
160 type = build_qualified_type (ptr_type_node,
161 cp_type_quals (TREE_TYPE (exp)));
162 return build (PLUS_EXPR, type, exp,
163 cp_convert (ptrdiff_type_node, offset));
164 }
165
166 /* Get a bad_cast node for the program to throw...
167
168 See libstdc++/exception.cc for __throw_bad_cast */
169
170 static tree
171 throw_bad_cast (void)
172 {
173 tree fn = get_identifier ("__cxa_bad_cast");
174 if (IDENTIFIER_GLOBAL_VALUE (fn))
175 fn = IDENTIFIER_GLOBAL_VALUE (fn);
176 else
177 fn = push_throw_library_fn (fn, build_function_type (ptr_type_node,
178 void_list_node));
179
180 return build_cxx_call (fn, NULL_TREE, NULL_TREE);
181 }
182
183 /* Return an expression for "__cxa_bad_typeid()". The expression
184 returned is an lvalue of type "const std::type_info". */
185
186 static tree
187 throw_bad_typeid (void)
188 {
189 tree fn = get_identifier ("__cxa_bad_typeid");
190 if (IDENTIFIER_GLOBAL_VALUE (fn))
191 fn = IDENTIFIER_GLOBAL_VALUE (fn);
192 else
193 {
194 tree t = build_qualified_type (type_info_type_node, TYPE_QUAL_CONST);
195 t = build_function_type (build_reference_type (t), void_list_node);
196 fn = push_throw_library_fn (fn, t);
197 }
198
199 return convert_from_reference (build_cxx_call (fn, NULL_TREE, NULL_TREE));
200 }
201 \f
202 /* Return an lvalue expression whose type is "const std::type_info"
203 and whose value indicates the type of the expression EXP. If EXP
204 is a reference to a polymorphic class, return the dynamic type;
205 otherwise return the static type of the expression. */
206
207 static tree
208 get_tinfo_decl_dynamic (tree exp)
209 {
210 tree type;
211 tree t;
212
213 if (exp == error_mark_node)
214 return error_mark_node;
215
216 /* peel back references, so they match. */
217 type = non_reference (TREE_TYPE (exp));
218
219 /* Peel off cv qualifiers. */
220 type = TYPE_MAIN_VARIANT (type);
221
222 if (!VOID_TYPE_P (type))
223 type = complete_type_or_else (type, exp);
224
225 if (!type)
226 return error_mark_node;
227
228 /* If exp is a reference to polymorphic type, get the real type_info. */
229 if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
230 {
231 /* build reference to type_info from vtable. */
232 tree index;
233
234 /* The RTTI information is at index -1. */
235 index = build_int_2 (-1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE, -1);
236 t = build_vtbl_ref (exp, index);
237 TREE_TYPE (t) = type_info_ptr_type;
238 }
239 else
240 /* Otherwise return the type_info for the static type of the expr. */
241 t = get_tinfo_ptr (TYPE_MAIN_VARIANT (type));
242
243 return build_indirect_ref (t, NULL);
244 }
245
246 static bool
247 typeid_ok_p (void)
248 {
249 if (! flag_rtti)
250 {
251 error ("cannot use typeid with -fno-rtti");
252 return false;
253 }
254
255 if (!COMPLETE_TYPE_P (type_info_type_node))
256 {
257 error ("must #include <typeinfo> before using typeid");
258 return false;
259 }
260
261 return true;
262 }
263
264 /* Return an expression for "typeid(EXP)". The expression returned is
265 an lvalue of type "const std::type_info". */
266
267 tree
268 build_typeid (tree exp)
269 {
270 tree cond = NULL_TREE;
271 int nonnull = 0;
272
273 if (exp == error_mark_node || !typeid_ok_p ())
274 return error_mark_node;
275
276 if (processing_template_decl)
277 return build_min (TYPEID_EXPR, type_info_ref_type, exp);
278
279 if (TREE_CODE (exp) == INDIRECT_REF
280 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
281 && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
282 && ! resolves_to_fixed_type_p (exp, &nonnull)
283 && ! nonnull)
284 {
285 exp = stabilize_reference (exp);
286 cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0));
287 }
288
289 exp = get_tinfo_decl_dynamic (exp);
290
291 if (exp == error_mark_node)
292 return error_mark_node;
293
294 if (cond)
295 {
296 tree bad = throw_bad_typeid ();
297
298 exp = build (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
299 }
300
301 return exp;
302 }
303
304 /* Generate the NTBS name of a type. */
305 static tree
306 tinfo_name (tree type)
307 {
308 const char *name;
309 tree name_string;
310
311 name = mangle_type_string (type);
312 name_string = fix_string_type (build_string (strlen (name) + 1, name));
313 return name_string;
314 }
315
316 /* Return a VAR_DECL for the internal ABI defined type_info object for
317 TYPE. You must arrange that the decl is mark_used, if actually use
318 it --- decls in vtables are only used if the vtable is output. */
319
320 tree
321 get_tinfo_decl (tree type)
322 {
323 tree name;
324 tree d;
325
326 if (COMPLETE_TYPE_P (type)
327 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
328 {
329 error ("cannot create type information for type `%T' because its size is variable",
330 type);
331 return error_mark_node;
332 }
333
334 if (TREE_CODE (type) == OFFSET_TYPE)
335 type = TREE_TYPE (type);
336 if (TREE_CODE (type) == METHOD_TYPE)
337 type = build_function_type (TREE_TYPE (type),
338 TREE_CHAIN (TYPE_ARG_TYPES (type)));
339
340 /* For a class type, the variable is cached in the type node
341 itself. */
342 if (CLASS_TYPE_P (type))
343 {
344 d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type));
345 if (d)
346 return d;
347 }
348
349 name = mangle_typeinfo_for_type (type);
350
351 d = IDENTIFIER_GLOBAL_VALUE (name);
352 if (!d)
353 {
354 tree var_desc = get_pseudo_ti_desc (type);
355
356 d = build_lang_decl (VAR_DECL, name, TINFO_PSEUDO_TYPE (var_desc));
357
358 DECL_ARTIFICIAL (d) = 1;
359 TREE_READONLY (d) = 1;
360 TREE_STATIC (d) = 1;
361 DECL_EXTERNAL (d) = 1;
362 SET_DECL_ASSEMBLER_NAME (d, name);
363 DECL_COMDAT (d) = 1;
364
365 pushdecl_top_level_and_finish (d, NULL_TREE);
366
367 if (CLASS_TYPE_P (type))
368 CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d;
369
370 /* Remember the type it is for. */
371 TREE_TYPE (name) = type;
372
373 /* Add decl to the global array of tinfo decls. */
374 my_friendly_assert (unemitted_tinfo_decls != 0, 20030312);
375 VARRAY_PUSH_TREE (unemitted_tinfo_decls, d);
376 }
377
378 return d;
379 }
380
381 /* Return a pointer to a type_info object describing TYPE, suitably
382 cast to the language defined type. */
383
384 static tree
385 get_tinfo_ptr (tree type)
386 {
387 tree decl = get_tinfo_decl (type);
388
389 mark_used (decl);
390 return build_nop (type_info_ptr_type,
391 build_address (decl));
392 }
393
394 /* Return the type_info object for TYPE. */
395
396 tree
397 get_typeid (tree type)
398 {
399 if (type == error_mark_node || !typeid_ok_p ())
400 return error_mark_node;
401
402 if (processing_template_decl)
403 return build_min (TYPEID_EXPR, type_info_ref_type, type);
404
405 /* If the type of the type-id is a reference type, the result of the
406 typeid expression refers to a type_info object representing the
407 referenced type. */
408 type = non_reference (type);
409
410 /* The top-level cv-qualifiers of the lvalue expression or the type-id
411 that is the operand of typeid are always ignored. */
412 type = TYPE_MAIN_VARIANT (type);
413
414 if (!VOID_TYPE_P (type))
415 type = complete_type_or_else (type, NULL_TREE);
416
417 if (!type)
418 return error_mark_node;
419
420 return build_indirect_ref (get_tinfo_ptr (type), NULL);
421 }
422
423 /* Check whether TEST is null before returning RESULT. If TEST is used in
424 RESULT, it must have previously had a save_expr applied to it. */
425
426 static tree
427 ifnonnull (tree test, tree result)
428 {
429 return build (COND_EXPR, TREE_TYPE (result),
430 build (EQ_EXPR, boolean_type_node, test, integer_zero_node),
431 cp_convert (TREE_TYPE (result), integer_zero_node),
432 result);
433 }
434
435 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
436 paper. */
437
438 static tree
439 build_dynamic_cast_1 (tree type, tree expr)
440 {
441 enum tree_code tc = TREE_CODE (type);
442 tree exprtype = TREE_TYPE (expr);
443 tree dcast_fn;
444 tree old_expr = expr;
445 const char *errstr = NULL;
446
447 /* T shall be a pointer or reference to a complete class type, or
448 `pointer to cv void''. */
449 switch (tc)
450 {
451 case POINTER_TYPE:
452 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
453 break;
454 case REFERENCE_TYPE:
455 if (! IS_AGGR_TYPE (TREE_TYPE (type)))
456 {
457 errstr = "target is not pointer or reference to class";
458 goto fail;
459 }
460 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
461 {
462 errstr = "target is not pointer or reference to complete type";
463 goto fail;
464 }
465 break;
466
467 default:
468 errstr = "target is not pointer or reference";
469 goto fail;
470 }
471
472 if (tc == POINTER_TYPE)
473 expr = convert_from_reference (expr);
474 else if (TREE_CODE (exprtype) != REFERENCE_TYPE)
475 {
476 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
477 exprtype = build_reference_type (exprtype);
478 expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
479 LOOKUP_NORMAL, NULL_TREE);
480 }
481
482 exprtype = TREE_TYPE (expr);
483
484 if (tc == POINTER_TYPE)
485 {
486 /* If T is a pointer type, v shall be an rvalue of a pointer to
487 complete class type, and the result is an rvalue of type T. */
488
489 if (TREE_CODE (exprtype) != POINTER_TYPE)
490 {
491 errstr = "source is not a pointer";
492 goto fail;
493 }
494 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
495 {
496 errstr = "source is not a pointer to class";
497 goto fail;
498 }
499 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
500 {
501 errstr = "source is a pointer to incomplete type";
502 goto fail;
503 }
504 }
505 else
506 {
507 /* T is a reference type, v shall be an lvalue of a complete class
508 type, and the result is an lvalue of the type referred to by T. */
509
510 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
511 {
512 errstr = "source is not of class type";
513 goto fail;
514 }
515 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
516 {
517 errstr = "source is of incomplete class type";
518 goto fail;
519 }
520
521 }
522
523 /* The dynamic_cast operator shall not cast away constness. */
524 if (!at_least_as_qualified_p (TREE_TYPE (type),
525 TREE_TYPE (exprtype)))
526 {
527 errstr = "conversion casts away constness";
528 goto fail;
529 }
530
531 /* If *type is an unambiguous accessible base class of *exprtype,
532 convert statically. */
533 {
534 tree binfo;
535
536 binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
537 ba_not_special, NULL);
538
539 if (binfo)
540 {
541 expr = build_base_path (PLUS_EXPR, convert_from_reference (expr),
542 binfo, 0);
543 if (TREE_CODE (exprtype) == POINTER_TYPE)
544 expr = non_lvalue (expr);
545 return expr;
546 }
547 }
548
549 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
550 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
551 {
552 tree expr1;
553 /* if TYPE is `void *', return pointer to complete object. */
554 if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
555 {
556 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
557 if (TREE_CODE (expr) == ADDR_EXPR
558 && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
559 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
560 return build1 (NOP_EXPR, type, expr);
561
562 /* Since expr is used twice below, save it. */
563 expr = save_expr (expr);
564
565 expr1 = build_headof (expr);
566 if (TREE_TYPE (expr1) != type)
567 expr1 = build1 (NOP_EXPR, type, expr1);
568 return ifnonnull (expr, expr1);
569 }
570 else
571 {
572 tree retval;
573 tree result, td2, td3, elems;
574 tree static_type, target_type, boff;
575
576 /* If we got here, we can't convert statically. Therefore,
577 dynamic_cast<D&>(b) (b an object) cannot succeed. */
578 if (tc == REFERENCE_TYPE)
579 {
580 if (TREE_CODE (old_expr) == VAR_DECL
581 && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
582 {
583 tree expr = throw_bad_cast ();
584 warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
585 old_expr, type);
586 /* Bash it to the expected type. */
587 TREE_TYPE (expr) = type;
588 return expr;
589 }
590 }
591 /* Ditto for dynamic_cast<D*>(&b). */
592 else if (TREE_CODE (expr) == ADDR_EXPR)
593 {
594 tree op = TREE_OPERAND (expr, 0);
595 if (TREE_CODE (op) == VAR_DECL
596 && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
597 {
598 warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
599 op, type);
600 retval = build_int_2 (0, 0);
601 TREE_TYPE (retval) = type;
602 return retval;
603 }
604 }
605
606 target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
607 static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
608 td2 = get_tinfo_decl (target_type);
609 mark_used (td2);
610 td2 = build_unary_op (ADDR_EXPR, td2, 0);
611 td3 = get_tinfo_decl (static_type);
612 mark_used (td3);
613 td3 = build_unary_op (ADDR_EXPR, td3, 0);
614
615 /* Determine how T and V are related. */
616 boff = get_dynamic_cast_base_type (static_type, target_type);
617
618 /* Since expr is used twice below, save it. */
619 expr = save_expr (expr);
620
621 expr1 = expr;
622 if (tc == REFERENCE_TYPE)
623 expr1 = build_unary_op (ADDR_EXPR, expr1, 0);
624
625 elems = tree_cons
626 (NULL_TREE, expr1, tree_cons
627 (NULL_TREE, td3, tree_cons
628 (NULL_TREE, td2, tree_cons
629 (NULL_TREE, boff, NULL_TREE))));
630
631 dcast_fn = dynamic_cast_node;
632 if (!dcast_fn)
633 {
634 tree tmp;
635 tree tinfo_ptr;
636 tree ns = abi_node;
637 const char *name;
638
639 push_nested_namespace (ns);
640 tinfo_ptr = xref_tag (class_type,
641 get_identifier ("__class_type_info"),
642 /*attributes=*/NULL_TREE,
643 1);
644
645 tinfo_ptr = build_pointer_type
646 (build_qualified_type
647 (tinfo_ptr, TYPE_QUAL_CONST));
648 name = "__dynamic_cast";
649 tmp = tree_cons
650 (NULL_TREE, const_ptr_type_node, tree_cons
651 (NULL_TREE, tinfo_ptr, tree_cons
652 (NULL_TREE, tinfo_ptr, tree_cons
653 (NULL_TREE, ptrdiff_type_node, void_list_node))));
654 tmp = build_function_type (ptr_type_node, tmp);
655 dcast_fn = build_library_fn_ptr (name, tmp);
656 pop_nested_namespace (ns);
657 dynamic_cast_node = dcast_fn;
658 }
659 result = build_cxx_call (dcast_fn, elems, elems);
660
661 if (tc == REFERENCE_TYPE)
662 {
663 tree bad = throw_bad_cast ();
664
665 result = save_expr (result);
666 return build (COND_EXPR, type, result, result, bad);
667 }
668
669 /* Now back to the type we want from a void*. */
670 result = cp_convert (type, result);
671 return ifnonnull (expr, result);
672 }
673 }
674 else
675 errstr = "source type is not polymorphic";
676
677 fail:
678 error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T' (%s)",
679 expr, exprtype, type, errstr);
680 return error_mark_node;
681 }
682
683 tree
684 build_dynamic_cast (tree type, tree expr)
685 {
686 if (type == error_mark_node || expr == error_mark_node)
687 return error_mark_node;
688
689 if (processing_template_decl)
690 return build_min (DYNAMIC_CAST_EXPR, type, expr);
691
692 return convert_from_reference (build_dynamic_cast_1 (type, expr));
693 }
694 \f
695 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
696
697 static int
698 qualifier_flags (tree type)
699 {
700 int flags = 0;
701 int quals = cp_type_quals (type);
702
703 if (quals & TYPE_QUAL_CONST)
704 flags |= 1;
705 if (quals & TYPE_QUAL_VOLATILE)
706 flags |= 2;
707 if (quals & TYPE_QUAL_RESTRICT)
708 flags |= 4;
709 return flags;
710 }
711
712 /* Return true, if the pointer chain TYPE ends at an incomplete type, or
713 contains a pointer to member of an incomplete class. */
714
715 static bool
716 target_incomplete_p (tree type)
717 {
718 while (TREE_CODE (type) == POINTER_TYPE)
719 if (TYPE_PTRMEM_P (type))
720 {
721 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
722 return true;
723 type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
724 }
725 else
726 type = TREE_TYPE (type);
727 if (!COMPLETE_OR_VOID_TYPE_P (type))
728 return true;
729
730 return false;
731 }
732
733 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
734 is the vtable pointer and NTBS name. The NTBS name is emitted as a
735 comdat const char array, so it becomes a unique key for the type. Generate
736 and emit that VAR_DECL here. (We can't always emit the type_info itself
737 as comdat, because of pointers to incomplete.) */
738
739 static tree
740 tinfo_base_init (tree desc, tree target)
741 {
742 tree init = NULL_TREE;
743 tree name_decl;
744 tree vtable_ptr;
745
746 {
747 tree name_name;
748
749 /* Generate the NTBS array variable. */
750 tree name_type = build_cplus_array_type
751 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
752 NULL_TREE);
753 tree name_string = tinfo_name (target);
754
755 name_name = mangle_typeinfo_string_for_type (target);
756 name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
757
758 DECL_ARTIFICIAL (name_decl) = 1;
759 TREE_READONLY (name_decl) = 1;
760 TREE_STATIC (name_decl) = 1;
761 DECL_EXTERNAL (name_decl) = 0;
762 TREE_PUBLIC (name_decl) = 1;
763 comdat_linkage (name_decl);
764 /* External name of the string containing the type's name has a
765 special name. */
766 SET_DECL_ASSEMBLER_NAME (name_decl,
767 mangle_typeinfo_string_for_type (target));
768 DECL_INITIAL (name_decl) = name_string;
769 mark_used (name_decl);
770 pushdecl_top_level_and_finish (name_decl, name_string);
771 }
772
773 vtable_ptr = TINFO_VTABLE_DECL (desc);
774 if (!vtable_ptr)
775 {
776 tree real_type;
777
778 push_nested_namespace (abi_node);
779 real_type = xref_tag (class_type, TINFO_REAL_NAME (desc),
780 /*attributes=*/NULL_TREE, 1);
781 pop_nested_namespace (abi_node);
782
783 if (!COMPLETE_TYPE_P (real_type))
784 {
785 /* We never saw a definition of this type, so we need to
786 tell the compiler that this is an exported class, as
787 indeed all of the __*_type_info classes are. */
788 SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
789 CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
790 }
791
792 vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
793 vtable_ptr = build_unary_op (ADDR_EXPR, vtable_ptr, 0);
794
795 /* We need to point into the middle of the vtable. */
796 vtable_ptr = build
797 (PLUS_EXPR, TREE_TYPE (vtable_ptr), vtable_ptr,
798 size_binop (MULT_EXPR,
799 size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
800 TYPE_SIZE_UNIT (vtable_entry_type)));
801 TREE_CONSTANT (vtable_ptr) = 1;
802
803 TINFO_VTABLE_DECL (desc) = vtable_ptr;
804 }
805
806 init = tree_cons (NULL_TREE, vtable_ptr, init);
807
808 init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
809
810 init = build_constructor (NULL_TREE, nreverse (init));
811 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
812 init = tree_cons (NULL_TREE, init, NULL_TREE);
813
814 return init;
815 }
816
817 /* Return the CONSTRUCTOR expr for a type_info of TYPE. DESC provides the
818 information about the particular type_info derivation, which adds no
819 additional fields to the type_info base. */
820
821 static tree
822 generic_initializer (tree desc, tree target)
823 {
824 tree init = tinfo_base_init (desc, target);
825
826 init = build_constructor (NULL_TREE, init);
827 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
828 return init;
829 }
830
831 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
832 DESC provides information about the particular type_info derivation,
833 which adds target type and qualifier flags members to the type_info base. */
834
835 static tree
836 ptr_initializer (tree desc, tree target, bool *non_public_ptr)
837 {
838 tree init = tinfo_base_init (desc, target);
839 tree to = TREE_TYPE (target);
840 int flags = qualifier_flags (to);
841 bool incomplete = target_incomplete_p (to);
842
843 if (incomplete)
844 {
845 flags |= 8;
846 *non_public_ptr = true;
847 }
848 init = tree_cons (NULL_TREE, build_int_2 (flags, 0), init);
849 init = tree_cons (NULL_TREE,
850 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
851 init);
852
853 init = build_constructor (NULL_TREE, nreverse (init));
854 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
855 return init;
856 }
857
858 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
859 DESC provides information about the particular type_info derivation,
860 which adds class, target type and qualifier flags members to the type_info
861 base. */
862
863 static tree
864 ptm_initializer (tree desc, tree target, bool *non_public_ptr)
865 {
866 tree init = tinfo_base_init (desc, target);
867 tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
868 tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
869 int flags = qualifier_flags (to);
870 bool incomplete = target_incomplete_p (to);
871
872 if (incomplete)
873 {
874 flags |= 0x8;
875 *non_public_ptr = true;
876 }
877 if (!COMPLETE_TYPE_P (klass))
878 {
879 flags |= 0x10;
880 *non_public_ptr = true;
881 }
882 init = tree_cons (NULL_TREE, build_int_2 (flags, 0), init);
883 init = tree_cons (NULL_TREE,
884 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
885 init);
886 init = tree_cons (NULL_TREE,
887 get_tinfo_ptr (klass),
888 init);
889
890 init = build_constructor (NULL_TREE, nreverse (init));
891 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
892 return init;
893 }
894
895 /* Check base BINFO to set hint flags in *DATA, which is really an int.
896 We use CLASSTYPE_MARKED to tag types we've found as non-virtual bases and
897 CLASSTYPE_MARKED2 to tag those which are virtual bases. Remember it is
898 possible for a type to be both a virtual and non-virtual base. */
899
900 static tree
901 dfs_class_hint_mark (tree binfo, void *data)
902 {
903 tree basetype = BINFO_TYPE (binfo);
904 int *hint = (int *) data;
905
906 if (TREE_VIA_VIRTUAL (binfo))
907 {
908 if (CLASSTYPE_MARKED (basetype))
909 *hint |= 1;
910 if (CLASSTYPE_MARKED2 (basetype))
911 *hint |= 2;
912 SET_CLASSTYPE_MARKED2 (basetype);
913 }
914 else
915 {
916 if (CLASSTYPE_MARKED (basetype) || CLASSTYPE_MARKED2 (basetype))
917 *hint |= 1;
918 SET_CLASSTYPE_MARKED (basetype);
919 }
920 return NULL_TREE;
921 }
922
923 /* Clear the base's dfs marks, after searching for duplicate bases. */
924
925 static tree
926 dfs_class_hint_unmark (tree binfo, void *data ATTRIBUTE_UNUSED)
927 {
928 tree basetype = BINFO_TYPE (binfo);
929
930 CLEAR_CLASSTYPE_MARKED (basetype);
931 CLEAR_CLASSTYPE_MARKED2 (basetype);
932 return NULL_TREE;
933 }
934
935 /* Determine the hint flags describing the features of a class's hierarchy. */
936
937 static int
938 class_hint_flags (tree type)
939 {
940 int hint_flags = 0;
941
942 dfs_walk (TYPE_BINFO (type), dfs_class_hint_mark, NULL, &hint_flags);
943 dfs_walk (TYPE_BINFO (type), dfs_class_hint_unmark, NULL, NULL);
944
945 return hint_flags;
946 }
947
948 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
949 DESC provides information about the particular __class_type_info derivation,
950 which adds hint flags and TRAIL initializers to the type_info base. */
951
952 static tree
953 class_initializer (tree desc, tree target, tree trail)
954 {
955 tree init = tinfo_base_init (desc, target);
956
957 TREE_CHAIN (init) = trail;
958 init = build_constructor (NULL_TREE, init);
959 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
960 return init;
961 }
962
963 /* Returns true if the typeinfo for type should be placed in
964 the runtime library. */
965
966 static bool
967 typeinfo_in_lib_p (tree type)
968 {
969 /* The typeinfo objects for `T*' and `const T*' are in the runtime
970 library for simple types T. */
971 if (TREE_CODE (type) == POINTER_TYPE
972 && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
973 || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
974 type = TREE_TYPE (type);
975
976 switch (TREE_CODE (type))
977 {
978 case INTEGER_TYPE:
979 case BOOLEAN_TYPE:
980 case CHAR_TYPE:
981 case REAL_TYPE:
982 case VOID_TYPE:
983 return true;
984
985 default:
986 return false;
987 }
988 }
989
990 /* Generate the initializer for the type info describing
991 TYPE. VAR_DESC is a . NON_PUBLIC_P is set nonzero, if the VAR_DECL
992 should not be exported from this object file. This should only be
993 called at the end of translation, when we know that no further
994 types will be completed. */
995
996 static tree
997 get_pseudo_ti_init (tree type, tree var_desc, bool *non_public_p)
998 {
999 my_friendly_assert (at_eof, 20021120);
1000 switch (TREE_CODE (type))
1001 {
1002 case POINTER_TYPE:
1003 if (TYPE_PTRMEM_P (type))
1004 return ptm_initializer (var_desc, type, non_public_p);
1005 else
1006 return ptr_initializer (var_desc, type, non_public_p);
1007 break;
1008 case ENUMERAL_TYPE:
1009 return generic_initializer (var_desc, type);
1010 break;
1011 case FUNCTION_TYPE:
1012 return generic_initializer (var_desc, type);
1013 break;
1014 case ARRAY_TYPE:
1015 return generic_initializer (var_desc, type);
1016 break;
1017 case UNION_TYPE:
1018 case RECORD_TYPE:
1019 if (TYPE_PTRMEMFUNC_P (type))
1020 return ptm_initializer (var_desc, type, non_public_p);
1021 else if (var_desc == class_desc_type_node)
1022 {
1023 if (!COMPLETE_TYPE_P (type))
1024 /* Emit a non-public class_type_info. */
1025 *non_public_p = true;
1026 return class_initializer (var_desc, type, NULL_TREE);
1027 }
1028 else if (var_desc == si_class_desc_type_node)
1029 {
1030 tree base_binfos = BINFO_BASETYPES (TYPE_BINFO (type));
1031 tree base_binfo = TREE_VEC_ELT (base_binfos, 0);
1032 tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1033 tree base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1034
1035 return class_initializer (var_desc, type, base_inits);
1036 }
1037 else
1038 {
1039 int hint = class_hint_flags (type);
1040 tree binfo = TYPE_BINFO (type);
1041 int nbases = BINFO_N_BASETYPES (binfo);
1042 tree base_binfos = BINFO_BASETYPES (binfo);
1043 tree base_accesses = BINFO_BASEACCESSES (binfo);
1044 tree base_inits = NULL_TREE;
1045 int ix;
1046
1047 /* Generate the base information initializer. */
1048 for (ix = nbases; ix--;)
1049 {
1050 tree base_binfo = TREE_VEC_ELT (base_binfos, ix);
1051 tree base_init = NULL_TREE;
1052 int flags = 0;
1053 tree tinfo;
1054 tree offset;
1055
1056 if (TREE_VEC_ELT (base_accesses, ix) == access_public_node)
1057 flags |= 2;
1058 tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1059 if (TREE_VIA_VIRTUAL (base_binfo))
1060 {
1061 /* We store the vtable offset at which the virtual
1062 base offset can be found. */
1063 offset = BINFO_VPTR_FIELD (base_binfo);
1064 offset = convert (sizetype, offset);
1065 flags |= 1;
1066 }
1067 else
1068 offset = BINFO_OFFSET (base_binfo);
1069
1070 /* combine offset and flags into one field */
1071 offset = cp_build_binary_op (LSHIFT_EXPR, offset,
1072 build_int_2 (8, 0));
1073 offset = cp_build_binary_op (BIT_IOR_EXPR, offset,
1074 build_int_2 (flags, 0));
1075 base_init = tree_cons (NULL_TREE, offset, base_init);
1076 base_init = tree_cons (NULL_TREE, tinfo, base_init);
1077 base_init = build_constructor (NULL_TREE, base_init);
1078 TREE_HAS_CONSTRUCTOR (base_init) = 1;
1079 base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1080 }
1081 base_inits = build_constructor (NULL_TREE, base_inits);
1082 TREE_HAS_CONSTRUCTOR (base_inits) = 1;
1083 base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1084 /* Prepend the number of bases. */
1085 base_inits = tree_cons (NULL_TREE,
1086 build_int_2 (nbases, 0), base_inits);
1087 /* Prepend the hint flags. */
1088 base_inits = tree_cons (NULL_TREE,
1089 build_int_2 (hint, 0), base_inits);
1090
1091 return class_initializer (var_desc, type, base_inits);
1092 }
1093 break;
1094
1095 default:
1096 return generic_initializer (var_desc, type);
1097 }
1098 }
1099
1100 /* Generate the RECORD_TYPE containing the data layout of a type_info
1101 derivative as used by the runtime. This layout must be consistent with
1102 that defined in the runtime support. Also generate the VAR_DECL for the
1103 type's vtable. We explicitly manage the vtable member, and name it for
1104 real type as used in the runtime. The RECORD type has a different name,
1105 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1106 is the generated type and TINFO_VTABLE_NAME is the name of the
1107 vtable. We have to delay generating the VAR_DECL of the vtable
1108 until the end of the translation, when we'll have seen the library
1109 definition, if there was one.
1110
1111 REAL_NAME is the runtime's name of the type. Trailing arguments are
1112 additional FIELD_DECL's for the structure. The final argument must be
1113 NULL. */
1114
1115 static tree
1116 create_pseudo_type_info (const char *real_name, int ident, ...)
1117 {
1118 tree pseudo_type;
1119 char *pseudo_name;
1120 tree fields;
1121 tree field_decl;
1122 tree result;
1123 va_list ap;
1124
1125 va_start (ap, ident);
1126
1127 /* Generate the pseudo type name. */
1128 pseudo_name = (char *)alloca (strlen (real_name) + 30);
1129 strcpy (pseudo_name, real_name);
1130 strcat (pseudo_name, "_pseudo");
1131 if (ident)
1132 sprintf (pseudo_name + strlen (pseudo_name), "%d", ident);
1133
1134 /* First field is the pseudo type_info base class. */
1135 fields = build_decl (FIELD_DECL, NULL_TREE, ti_desc_type_node);
1136
1137 /* Now add the derived fields. */
1138 while ((field_decl = va_arg (ap, tree)))
1139 {
1140 TREE_CHAIN (field_decl) = fields;
1141 fields = field_decl;
1142 }
1143
1144 /* Create the pseudo type. */
1145 pseudo_type = make_aggr_type (RECORD_TYPE);
1146 finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
1147 CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
1148
1149 result = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
1150 TINFO_REAL_NAME (result) = get_identifier (real_name);
1151 TINFO_PSEUDO_TYPE (result) =
1152 cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1153
1154 va_end (ap);
1155 return result;
1156 }
1157
1158 /* Return a pseudo type info type node used to describe TYPE. TYPE
1159 must be a complete type (or cv void), except at the end of the
1160 translation unit. */
1161
1162 static tree
1163 get_pseudo_ti_desc (tree type)
1164 {
1165 switch (TREE_CODE (type))
1166 {
1167 case POINTER_TYPE:
1168 return TYPE_PTRMEM_P (type) ? ptm_desc_type_node : ptr_desc_type_node;
1169 case ENUMERAL_TYPE:
1170 return enum_desc_type_node;
1171 case FUNCTION_TYPE:
1172 return func_desc_type_node;
1173 case ARRAY_TYPE:
1174 return ary_desc_type_node;
1175 case UNION_TYPE:
1176 case RECORD_TYPE:
1177 if (TYPE_PTRMEMFUNC_P (type))
1178 return ptm_desc_type_node;
1179 else if (!COMPLETE_TYPE_P (type))
1180 {
1181 if (!at_eof)
1182 cxx_incomplete_type_error (NULL_TREE, type);
1183 return class_desc_type_node;
1184 }
1185 else if (!CLASSTYPE_N_BASECLASSES (type))
1186 return class_desc_type_node;
1187 else
1188 {
1189 tree binfo = TYPE_BINFO (type);
1190 tree base_binfos = BINFO_BASETYPES (binfo);
1191 tree base_accesses = BINFO_BASEACCESSES (binfo);
1192 tree base_binfo = TREE_VEC_ELT (base_binfos, 0);
1193 int num_bases = TREE_VEC_LENGTH (base_binfos);
1194
1195 if (num_bases == 1
1196 && TREE_VEC_ELT (base_accesses, 0) == access_public_node
1197 && !TREE_VIA_VIRTUAL (base_binfo)
1198 && integer_zerop (BINFO_OFFSET (base_binfo)))
1199 /* single non-virtual public. */
1200 return si_class_desc_type_node;
1201 else
1202 {
1203 tree var_desc;
1204 tree array_domain, base_array;
1205
1206 if (TREE_VEC_LENGTH (vmi_class_desc_type_node) <= num_bases)
1207 {
1208 int ix;
1209 tree extend = make_tree_vec (num_bases + 5);
1210
1211 for (ix = TREE_VEC_LENGTH (vmi_class_desc_type_node); ix--;)
1212 TREE_VEC_ELT (extend, ix)
1213 = TREE_VEC_ELT (vmi_class_desc_type_node, ix);
1214 vmi_class_desc_type_node = extend;
1215 }
1216 var_desc = TREE_VEC_ELT (vmi_class_desc_type_node, num_bases);
1217 if (var_desc)
1218 return var_desc;
1219
1220 /* Create the array of __base_class_type_info entries.
1221 G++ 3.2 allocated an array that had one too many
1222 entries, and then filled that extra entries with
1223 zeros. */
1224 if (abi_version_at_least (2))
1225 array_domain = build_index_type (size_int (num_bases - 1));
1226 else
1227 array_domain = build_index_type (size_int (num_bases));
1228 base_array =
1229 build_array_type (base_desc_type_node, array_domain);
1230
1231 push_nested_namespace (abi_node);
1232 var_desc = create_pseudo_type_info
1233 ("__vmi_class_type_info", num_bases,
1234 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1235 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1236 build_decl (FIELD_DECL, NULL_TREE, base_array),
1237 NULL);
1238 pop_nested_namespace (abi_node);
1239
1240 TREE_VEC_ELT (vmi_class_desc_type_node, num_bases) = var_desc;
1241 return var_desc;
1242 }
1243 }
1244 default:
1245 return bltn_desc_type_node;
1246 }
1247 }
1248
1249 /* Make sure the required builtin types exist for generating the type_info
1250 varable definitions. */
1251
1252 static void
1253 create_tinfo_types (void)
1254 {
1255 my_friendly_assert (!ti_desc_type_node, 20020609);
1256
1257 push_nested_namespace (abi_node);
1258
1259 /* Create the internal type_info structure. This is used as a base for
1260 the other structures. */
1261 {
1262 tree field, fields;
1263
1264 ti_desc_type_node = make_aggr_type (RECORD_TYPE);
1265 field = build_decl (FIELD_DECL, NULL_TREE, const_ptr_type_node);
1266 fields = field;
1267
1268 field = build_decl (FIELD_DECL, NULL_TREE, const_string_type_node);
1269 TREE_CHAIN (field) = fields;
1270 fields = field;
1271
1272 finish_builtin_struct (ti_desc_type_node, "__type_info_pseudo",
1273 fields, NULL_TREE);
1274 TYPE_HAS_CONSTRUCTOR (ti_desc_type_node) = 1;
1275 }
1276
1277 /* Fundamental type_info */
1278 bltn_desc_type_node = create_pseudo_type_info
1279 ("__fundamental_type_info", 0,
1280 NULL);
1281
1282 /* Array, function and enum type_info. No additional fields. */
1283 ary_desc_type_node = create_pseudo_type_info
1284 ("__array_type_info", 0,
1285 NULL);
1286 func_desc_type_node = create_pseudo_type_info
1287 ("__function_type_info", 0,
1288 NULL);
1289 enum_desc_type_node = create_pseudo_type_info
1290 ("__enum_type_info", 0,
1291 NULL);
1292
1293 /* Class type_info. Add a flags field. */
1294 class_desc_type_node = create_pseudo_type_info
1295 ("__class_type_info", 0,
1296 NULL);
1297
1298 /* Single public non-virtual base class. Add pointer to base class.
1299 This is really a descendant of __class_type_info. */
1300 si_class_desc_type_node = create_pseudo_type_info
1301 ("__si_class_type_info", 0,
1302 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1303 NULL);
1304
1305 /* Base class internal helper. Pointer to base type, offset to base,
1306 flags. */
1307 {
1308 tree field, fields;
1309
1310 field = build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type);
1311 fields = field;
1312
1313 field = build_decl (FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1314 TREE_CHAIN (field) = fields;
1315 fields = field;
1316
1317 base_desc_type_node = make_aggr_type (RECORD_TYPE);
1318 finish_builtin_struct (base_desc_type_node, "__base_class_type_info_pseudo",
1319 fields, NULL_TREE);
1320 TYPE_HAS_CONSTRUCTOR (base_desc_type_node) = 1;
1321 }
1322
1323 /* General hierarchy is created as necessary in this vector. */
1324 vmi_class_desc_type_node = make_tree_vec (10);
1325
1326 /* Pointer type_info. Adds two fields, qualification mask
1327 and pointer to the pointed to type. This is really a descendant of
1328 __pbase_type_info. */
1329 ptr_desc_type_node = create_pseudo_type_info
1330 ("__pointer_type_info", 0,
1331 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1332 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1333 NULL);
1334
1335 /* Pointer to member data type_info. Add qualifications flags,
1336 pointer to the member's type info and pointer to the class.
1337 This is really a descendant of __pbase_type_info. */
1338 ptm_desc_type_node = create_pseudo_type_info
1339 ("__pointer_to_member_type_info", 0,
1340 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1341 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1342 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1343 NULL);
1344
1345 pop_nested_namespace (abi_node);
1346 }
1347
1348 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1349 support. Generating them here guarantees consistency with the other
1350 structures. We use the following heuristic to determine when the runtime
1351 is being generated. If std::__fundamental_type_info is defined, and its
1352 destructor is defined, then the runtime is being built. */
1353
1354 void
1355 emit_support_tinfos (void)
1356 {
1357 static tree *const fundamentals[] =
1358 {
1359 &void_type_node,
1360 &boolean_type_node,
1361 &wchar_type_node,
1362 &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1363 &short_integer_type_node, &short_unsigned_type_node,
1364 &integer_type_node, &unsigned_type_node,
1365 &long_integer_type_node, &long_unsigned_type_node,
1366 &long_long_integer_type_node, &long_long_unsigned_type_node,
1367 &float_type_node, &double_type_node, &long_double_type_node,
1368 0
1369 };
1370 int ix;
1371 tree bltn_type, dtor;
1372
1373 push_nested_namespace (abi_node);
1374 bltn_type = xref_tag (class_type,
1375 get_identifier ("__fundamental_type_info"),
1376 /*attributes=*/NULL_TREE,
1377 1);
1378 pop_nested_namespace (abi_node);
1379 if (!COMPLETE_TYPE_P (bltn_type))
1380 return;
1381 dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (bltn_type), 1);
1382 if (DECL_EXTERNAL (dtor))
1383 return;
1384 doing_runtime = 1;
1385 for (ix = 0; fundamentals[ix]; ix++)
1386 {
1387 tree bltn = *fundamentals[ix];
1388 tree bltn_ptr = build_pointer_type (bltn);
1389 tree bltn_const_ptr = build_pointer_type
1390 (build_qualified_type (bltn, TYPE_QUAL_CONST));
1391 tree tinfo;
1392
1393 tinfo = get_tinfo_decl (bltn);
1394 TREE_USED (tinfo) = 1;
1395 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1396
1397 tinfo = get_tinfo_decl (bltn_ptr);
1398 TREE_USED (tinfo) = 1;
1399 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1400
1401 tinfo = get_tinfo_decl (bltn_const_ptr);
1402 TREE_USED (tinfo) = 1;
1403 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1404 }
1405 }
1406
1407 /* Return true, iff T is a type_info variable which has not had a
1408 definition emitted for it. */
1409
1410 static bool
1411 unemitted_tinfo_decl_p (tree t)
1412 {
1413 if (/* It's a var decl */
1414 TREE_CODE (t) == VAR_DECL
1415 /* which has a name */
1416 && DECL_NAME (t)
1417 /* whose name points back to itself */
1418 && IDENTIFIER_GLOBAL_VALUE (DECL_NAME (t)) == t
1419 /* whose name's type is non-null */
1420 && TREE_TYPE (DECL_NAME (t))
1421 /* and whose type is a struct */
1422 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1423 /* with a field */
1424 && TYPE_FIELDS (TREE_TYPE (t))
1425 /* which is our pseudo type info */
1426 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (t))) == ti_desc_type_node)
1427 return true;
1428 return false;
1429 }
1430
1431 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1432 tinfo decl. Determine whether it needs emitting, and if so
1433 generate the initializer. */
1434
1435 bool
1436 emit_tinfo_decl (tree decl)
1437 {
1438 tree type = TREE_TYPE (DECL_NAME (decl));
1439 bool non_public;
1440 int in_library = typeinfo_in_lib_p (type);
1441 tree var_desc, var_init;
1442
1443 my_friendly_assert (unemitted_tinfo_decl_p (decl), 20030307);
1444
1445 import_export_tinfo (decl, type, in_library);
1446 if (DECL_REALLY_EXTERN (decl) || !DECL_NEEDED_P (decl))
1447 return false;
1448
1449 if (!doing_runtime && in_library)
1450 return false;
1451
1452 non_public = false;
1453 var_desc = get_pseudo_ti_desc (type);
1454 var_init = get_pseudo_ti_init (type, var_desc, &non_public);
1455
1456 DECL_EXTERNAL (decl) = 0;
1457 TREE_PUBLIC (decl) = !non_public;
1458 if (non_public)
1459 DECL_COMDAT (decl) = 0;
1460
1461 DECL_INITIAL (decl) = var_init;
1462 mark_used (decl);
1463 cp_finish_decl (decl, var_init, NULL_TREE, 0);
1464 /* cp_finish_decl will have dealt with linkage. */
1465
1466 /* Say we've dealt with it. */
1467 TREE_TYPE (DECL_NAME (decl)) = NULL_TREE;
1468
1469 return true;
1470 }