import of gcc-2.8
[official-gcc.git] / gcc / cp / tinfo.cc
blobd82aaaf04315dd35f00046c2289682d7e378c38c
1 // Methods for type_info for -*- C++ -*- Run Time Type Identification.
2 // Copyright (C) 1994, 1996 Free Software Foundation
4 // This file is part of GNU CC.
6 // GNU CC is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // GNU CC is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with GNU CC; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330,
19 // Boston, MA 02111-1307, USA.
21 // As a special exception, if you link this library with other files,
22 // some of which are compiled with GCC, to produce an executable,
23 // this library does not by itself cause the resulting executable
24 // to be covered by the GNU General Public License.
25 // This exception does not however invalidate any other reasons why
26 // the executable file might be covered by the GNU General Public License.
28 #include <stddef.h>
29 #include "tinfo.h"
30 #include "new" // for placement new
32 // This file contains the minimal working set necessary to link with code
33 // that uses virtual functions and -frtti but does not actually use RTTI
34 // functionality.
36 type_info::
37 ~type_info ()
38 { }
40 extern "C" void
41 __rtti_class (void *addr, const char *name,
42 const __class_type_info::base_info *bl, size_t bn)
43 { new (addr) __class_type_info (name, bl, bn); }
45 extern "C" void
46 __rtti_si (void *addr, const char *n, const type_info *ti)
48 new (addr) __si_type_info
49 (n, static_cast <const __user_type_info &> (*ti));
52 extern "C" void
53 __rtti_user (void *addr, const char *name)
54 { new (addr) __user_type_info (name); }
56 // dynamic_cast helper methods.
57 // Returns a pointer to the desired sub-object or 0.
59 void * __user_type_info::
60 dcast (const type_info& to, int, void *addr, const type_info *, void *) const
61 { return (*this == to) ? addr : 0; }
63 void * __si_type_info::
64 dcast (const type_info& to, int require_public, void *addr,
65 const type_info *sub, void *subptr) const
67 if (*this == to)
68 return addr;
69 return base.dcast (to, require_public, addr, sub, subptr);
72 void* __class_type_info::
73 dcast (const type_info& desired, int is_public, void *objptr,
74 const type_info *sub, void *subptr) const
76 if (*this == desired)
77 return objptr;
79 void *match_found = 0;
80 for (int i = 0; i < n_bases; i++)
82 if (is_public && base_list[i].access != PUBLIC)
83 continue;
85 void *p = (char *)objptr + base_list[i].offset;
86 if (base_list[i].is_virtual)
87 p = *(void **)p;
88 p = base_list[i].base->dcast (desired, is_public, p, sub, subptr);
89 if (p)
91 if (match_found == 0)
92 match_found = p;
93 else if (match_found != p)
95 if (sub)
97 // Perhaps we're downcasting from *sub to desired; see if
98 // subptr is a subobject of exactly one of {match_found,p}.
100 const __user_type_info &d =
101 static_cast <const __user_type_info &> (desired);
103 void *os = d.dcast (*sub, 1, match_found);
104 void *ns = d.dcast (*sub, 1, p);
106 if (os == ns)
107 /* ambiguous -- subptr is a virtual base */;
108 else if (os == subptr)
109 continue;
110 else if (ns == subptr)
112 match_found = p;
113 continue;
117 // base found at two different pointers,
118 // conversion is not unique
119 return 0;
124 return match_found;