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