libstdc++: Remove UB from month and weekday additions and subtractions.
[official-gcc.git] / libphobos / libdruntime / rt / cast_.d
blob925599e01592867ac0441285cb0fec6768063bd5
1 /**
2 * Implementation of array assignment support routines.
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 */
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)
15 module rt.cast_;
17 extern (C):
18 @nogc:
19 nothrow:
20 pure:
22 // Needed because ClassInfo.opEquals(Object) does a dynamic cast,
23 // but we are trying to implement dynamic cast.
24 extern (D) private bool areClassInfosEqual(scope const ClassInfo a, scope const ClassInfo b) @safe
26 if (a is b)
27 return true;
28 // take care of potential duplicates across binaries
29 return a.name == b.name;
32 /******************************************
33 * Given a pointer:
34 * If it is an Object, return that Object.
35 * If it is an interface, return the Object implementing the interface.
36 * If it is null, return null.
37 * Else, undefined crash
39 Object _d_toObject(return scope void* p)
41 if (!p)
42 return null;
44 Object o = cast(Object) p;
45 ClassInfo oc = typeid(o);
46 Interface* pi = **cast(Interface***) p;
48 /* Interface.offset lines up with ClassInfo.name.ptr,
49 * so we rely on pointers never being less than 64K,
50 * and Objects never being greater.
52 if (pi.offset < 0x10000)
54 debug(cast_) printf("\tpi.offset = %d\n", pi.offset);
55 return cast(Object)(p - pi.offset);
57 return o;
60 /*************************************
61 * Attempts to cast Object o to class c.
62 * Returns o if successful, null if not.
64 void* _d_interface_cast(void* p, ClassInfo c)
66 debug(cast_) printf("_d_interface_cast(p = %p, c = '%.*s')\n", p, c.name);
67 if (!p)
68 return null;
70 Interface* pi = **cast(Interface***) p;
72 debug(cast_) printf("\tpi.offset = %d\n", pi.offset);
73 return _d_dynamic_cast(cast(Object)(p - pi.offset), c);
76 void* _d_dynamic_cast(Object o, ClassInfo c)
78 debug(cast_) printf("_d_dynamic_cast(o = %p, c = '%.*s')\n", o, c.name);
80 void* res = null;
81 size_t offset = 0;
82 if (o && _d_isbaseof2(typeid(o), c, offset))
84 debug(cast_) printf("\toffset = %d\n", offset);
85 res = cast(void*) o + offset;
87 debug(cast_) printf("\tresult = %p\n", res);
88 return res;
91 int _d_isbaseof2(scope ClassInfo oc, scope const ClassInfo c, scope ref size_t offset) @safe
93 if (areClassInfosEqual(oc, c))
94 return true;
98 if (oc.base && areClassInfosEqual(oc.base, c))
99 return true;
101 // Bugzilla 2013: Use depth-first search to calculate offset
102 // from the derived (oc) to the base (c).
103 foreach (iface; oc.interfaces)
105 if (areClassInfosEqual(iface.classinfo, c) || _d_isbaseof2(iface.classinfo, c, offset))
107 offset += iface.offset;
108 return true;
112 oc = oc.base;
113 } while (oc);
115 return false;
118 int _d_isbaseof(scope ClassInfo oc, scope const ClassInfo c) @safe
120 size_t offset = 0;
121 return _d_isbaseof2(oc, c, offset);