OSDN Git Service

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