Debian Codegraph @

libphobos/libdruntime/rt/cast_.d

raw
1 /**
2 * Implementation of array assignment support routines.
3 *
4 * Copyright: Copyright Digital Mars 2004 - 2010.
5 * License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 * Authors: Walter Bright, Sean Kelly
7 * Source: $(DRUNTIMESRC rt/_cast_.d)
8 */
9
10 /* Copyright Digital Mars 2004 - 2010.
11 * Distributed under the Boost Software License, Version 1.0.
12 * (See accompanying file LICENSE or copy at
13 * http://www.boost.org/LICENSE_1_0.txt)
14 */
15 module rt.cast_;
16
17 debug(cast_) import core.stdc.stdio : printf;
18
19 extern (C):
20 @nogc:
21 nothrow:
22 pure:
23
24 // Needed because ClassInfo.opEquals(Object) does a dynamic cast,
25 // but we are trying to implement dynamic cast.
26 extern (D) private bool areClassInfosEqual(scope const ClassInfo a, scope const ClassInfo b) @safe
27 {
28 // same class if signatures match, works with potential duplicates across binaries
29 if (a is b)
30 return true;
31
32 // new fast way
33 if (a.m_flags & TypeInfo_Class.ClassFlags.hasNameSig)
34 return a.nameSig[0] == b.nameSig[0]
35 && a.nameSig[1] == b.nameSig[1]
36 && a.nameSig[2] == b.nameSig[2]
37 && a.nameSig[3] == b.nameSig[3];
38
39 // old slow way for temporary binary compatibility
40 return a.name == b.name;
41 }
42
43 /******************************************
44 * Given a pointer:
45 * If it is an Object, return that Object.
46 * If it is an interface, return the Object implementing the interface.
47 * If it is null, return null.
48 * Else, undefined crash
49 */
50 Object _d_toObject(return scope void* p)
51 {
52 if (!p)
53 return null;
54
55 Object o = cast(Object) p;
56 ClassInfo oc = typeid(o);
57 Interface* pi = **cast(Interface***) p;
58
59 /* Interface.offset lines up with ClassInfo.name.ptr,
60 * so we rely on pointers never being less than 64K,
61 * and Objects never being greater.
62 */
63 if (pi.offset < 0x10000)
64 {
65 debug(cast_) printf("\tpi.offset = %zd\n", pi.offset);
66 return cast(Object)(p - pi.offset);
67 }
68 return o;
69 }
70
71 /*************************************
72 * Attempts to cast interface Object o to class c.
73 * Returns o if successful, null if not.
74 */
75 void* _d_interface_cast(void* p, ClassInfo c)
76 {
77 debug(cast_) printf("_d_interface_cast(p = %p, c = '%.*s')\n", p, cast(int) c.name.length, c.name.ptr);
78 if (!p)
79 return null;
80
81 Interface* pi = **cast(Interface***) p;
82
83 debug(cast_) printf("\tpi.offset = %zd\n", pi.offset);
84 Object o2 = cast(Object)(p - pi.offset);
85 void* res = null;
86 size_t offset = 0;
87 if (o2 && _d_isbaseof2(typeid(o2), c, offset))
88 {
89 debug(cast_) printf("\toffset = %zd\n", offset);
90 res = cast(void*) o2 + offset;
91 }
92 debug(cast_) printf("\tresult = %p\n", res);
93 return res;
94 }
95
96 /*****
97 * Dynamic cast from a class object `o` to class or interface `c`, where `c` is a subtype of `o`.
98 * Params:
99 * o = instance of class
100 * c = a subclass of o
101 * Returns:
102 * null if o is null or c is not a subclass of o. Otherwise, return o.
103 */
104 void* _d_dynamic_cast(Object o, ClassInfo c)
105 {
106 debug(cast_) printf("_d_dynamic_cast(o = %p, c = '%.*s')\n", o, cast(int) c.name.length, c.name.ptr);
107
108 void* res = null;
109 size_t offset = 0;
110 if (o && _d_isbaseof2(typeid(o), c, offset))
111 {
112 debug(cast_) printf("\toffset = %zd\n", offset);
113 res = cast(void*) o + offset;
114 }
115 debug(cast_) printf("\tresult = %p\n", res);
116 return res;
117 }
118
119 /*****
120 * Dynamic cast from a class object o to class c, where c is a subclass of o.
121 * Params:
122 * o = instance of class
123 * c = a subclass of o
124 * Returns:
125 * null if o is null or c is not a subclass of o. Otherwise, return o.
126 */
127 void* _d_class_cast(Object o, ClassInfo c)
128 {
129 debug(cast_) printf("_d_cast_cast(o = %p, c = '%.*s')\n", o, cast(int) c.name.length, c.name.ptr);
130
131 if (!o)
132 return null;
133
134 ClassInfo oc = typeid(o);
135 int delta = oc.depth;
136
137 if (delta && c.depth)
138 {
139 delta -= c.depth;
140 if (delta < 0)
141 return null;
142
143 while (delta--)
144 oc = oc.base;
145 if (areClassInfosEqual(oc, c))
146 return cast(void*)o;
147 return null;
148 }
149
150 // no depth data - support the old way
151 do
152 {
153 if (areClassInfosEqual(oc, c))
154 return cast(void*)o;
155 oc = oc.base;
156 } while (oc);
157 return null;
158 }
159
160 /**
161 * Dynamic cast `o` to final class `c` only one level down
162 * Params:
163 * o = object that is instance of a class
164 * c = class to cast it to
165 * Returns:
166 * o if it succeeds, null if it fails
167 */
168 void* _d_paint_cast(Object o, ClassInfo c)
169 {
170 /* If o is really an instance of c, just do a paint
171 */
172 auto p = o && cast(void*)(areClassInfosEqual(typeid(o), c)) ? o : null;
173 debug assert(cast(void*)p is cast(void*)_d_dynamic_cast(o, c));
174 return cast(void*)p;
175 }
176
177 int _d_isbaseof2(scope ClassInfo oc, scope const ClassInfo c, scope ref size_t offset) @safe
178 {
179 if (areClassInfosEqual(oc, c))
180 return true;
181
182 do
183 {
184 if (oc.base && areClassInfosEqual(oc.base, c))
185 return true;
186
187 // Bugzilla 2013: Use depth-first search to calculate offset
188 // from the derived (oc) to the base (c).
189 foreach (iface; oc.interfaces)
190 {
191 if (areClassInfosEqual(iface.classinfo, c) || _d_isbaseof2(iface.classinfo, c, offset))
192 {
193 offset += iface.offset;
194 return true;
195 }
196 }
197
198 oc = oc.base;
199 } while (oc);
200
201 return false;
202 }
203
204 int _d_isbaseof(scope ClassInfo oc, scope const ClassInfo c) @safe
205 {
206 size_t offset = 0;
207 return _d_isbaseof2(oc, c, offset);
208 }