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