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