Move *-*-gnu* pattern below *-*-linux*.
[official-gcc.git] / gcc / cp / tinfo.cc
blobfe22e8de0745c0d6e3b9f0ffc186f159845a5036
1 // Methods for type_info for -*- C++ -*- Run Time Type Identification.
2 // Copyright (C) 1994, 1996, 1998, 1999 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 #pragma implementation "typeinfo"
30 #include <stddef.h>
31 #include "tinfo.h"
32 #include "new" // for placement new
34 // This file contains the minimal working set necessary to link with code
35 // that uses virtual functions and -frtti but does not actually use RTTI
36 // functionality.
38 std::type_info::
39 ~type_info ()
40 { }
42 // We can't rely on common symbols being shared between shared objects.
43 bool std::type_info::
44 operator== (const std::type_info& arg) const
46 return (&arg == this) || (strcmp (name (), arg.name ()) == 0);
49 extern "C" void
50 __rtti_class (void *addr, const char *name,
51 const __class_type_info::base_info *bl, size_t bn)
52 { new (addr) __class_type_info (name, bl, bn); }
54 extern "C" void
55 __rtti_si (void *addr, const char *n, const std::type_info *ti)
57 new (addr) __si_type_info
58 (n, static_cast <const __user_type_info &> (*ti));
61 extern "C" void
62 __rtti_user (void *addr, const char *name)
63 { new (addr) __user_type_info (name); }
65 // dynamic_cast helper methods.
66 // Returns 1 if the cast succeeds, 0 otherwise. Stores the adjusted value
67 // in VALP.
69 int __user_type_info::
70 dcast (const type_info& to, int, void *addr, void **valp,
71 const type_info *, void *) const
73 *valp = addr;
74 return (*this == to);
77 int __si_type_info::
78 dcast (const type_info& to, int require_public, void *addr, void **valp,
79 const type_info *sub, void *subptr) const
81 if (*this == to)
83 *valp = addr;
84 return 1;
86 return base.dcast (to, require_public, addr, valp, sub, subptr);
89 int __class_type_info::
90 dcast (const type_info& desired, int is_public, void *objptr, void **valp,
91 const type_info *sub, void *subptr) const
93 *valp = objptr;
95 if (*this == desired)
96 return 1;
98 int match_found = 0;
99 void *match = 0;
101 for (size_t i = 0; i < n_bases; i++)
103 if (is_public && base_list[i].access != PUBLIC)
104 continue;
106 void *p;
108 if (objptr)
110 p = (char *)objptr + base_list[i].offset;
111 if (base_list[i].is_virtual)
112 p = *(void **)p;
114 else
115 /* Preserve null pointer. */
116 p = objptr;
118 if (base_list[i].base->dcast (desired, is_public, p, &p, sub, subptr))
120 if (! match_found)
122 match_found = 1;
123 match = p;
125 else if (match != p)
127 if (sub)
129 // Perhaps we're downcasting from *sub to desired; see if
130 // subptr is a subobject of exactly one of {match_found,p}.
132 const __user_type_info &d =
133 static_cast <const __user_type_info &> (desired);
135 void *os;
136 d.dcast (*sub, 1, match, &os);
137 void *ns;
138 d.dcast (*sub, 1, p, &ns);
140 if (os == ns)
141 // Both have the same subobject, so we can't disambiguate;
142 // i.e. subptr is a virtual base.
143 return 0;
144 else if (os == subptr)
145 continue;
146 else if (ns == subptr)
148 match = p;
149 continue;
152 else
153 // We're not downcasting, so we can't disambiguate.
154 return 0;
159 *valp = match;
160 return match_found;