summaryrefslogtreecommitdiff
path: root/src/private_typeinfo.cpp
blob: 04dc3ac2d4d27be1c323e1a5bcf519b50728463e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//===----------------------- private_typeinfo.cpp -------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "private_typeinfo.h"

// temporary headers
#include <iostream>
#include <cassert>

namespace std
{

type_info::~type_info()
{
}

}  // std

namespace __cxxabiv1
{

// __fundamental_type_info

// This miraculously (compiler magic) emits the type_info's for:
//   1. all of the fundamental types
//   2. pointers to all of the fundamental types
//   3. pointers to all of the const fundamental types
__fundamental_type_info::~__fundamental_type_info()
{
}

// __array_type_info

__array_type_info::~__array_type_info()
{
}

// __function_type_info

__function_type_info::~__function_type_info()
{
}

// __enum_type_info

__enum_type_info::~__enum_type_info()
{
}

// __class_type_info

__class_type_info::~__class_type_info()
{
}

// __dynamic_cast notes:
// Up or above refers to base classes and base objects.
// Down or below refers to derived classes/objects.

// search1 is a search algorithm used by __dynamic_cast.
// If a static_type is found
//     If dynamic_ptr == static_ptr
//         Record the path to get here.
//         if the path to get here is public
//             stop search
//     Otherwise continue search only below this node
// Else
//     Continue search above and below this node.

// __class_type_info::search1
// There are no nodes to search above this node
// Returns:  1 if search should be continued, otherwise 0
int
__class_type_info::search1(__dynamic_cast_info* info, const void* dynamic_ptr,
                           int path_below) const
{
    if (this == info->static_type)
    {
        if (dynamic_ptr == info->static_ptr)
        {
            if (path_below == public_path)
            {
                info->path_dynamic_ptr_to_static_ptr = public_path;
                return 0;
            }
            info->path_dynamic_ptr_to_static_ptr = not_public_path;
        }
    }
    return 1;
}

// search2 is a search algorithm used by __dynamic_cast.
// if this is a dst_type
//     if this has already been classified then
//        If the path to get here is public, overwrite existing path_dynamic_ptr_to_dst_ptr.
//     else we haven't been to this dst_type before.
//        Record the path to get here in path_dynamic_ptr_to_dst_ptr.
//        Is (static_ptr, static_type) above this dst_type?
//           Yes:
//             Record it as dst_ptr_leading_to_static_ptr and increment the
//                number of such recordings.
//             If this is not the first of such recordings, then stop searching.
//             Otherwise continue searching both above and below this node.
//           No:
//             record it as dst_ptr_not_leading_to_static_ptr and increment
//                 the number of such recordings.
// else if this is a static_type
//     if this is *our* static_type
//        if we found it above a dst_type, record the path from the dst_type
//        else record the path from the dynamic_type being careful not to overwrite a
//           previous public path in this latter case.
//        Record that we found our static_type.
//     Continue searching only below this node
// else 
//     Continue searching above and below this node.
int
__class_type_info::search2(__dynamic_cast_info* info, const void* dynamic_ptr,
                           int path_below) const
{
    if (this == info->dst_type)
    {
        if (dynamic_ptr == info->dst_ptr_not_leading_to_static_ptr)
        {
            if (path_below == public_path)
                info->path_dynamic_ptr_to_dst_ptr = public_path;
        }
        else
        {
            info->path_dynamic_ptr_to_dst_ptr = path_below;
            info->dst_ptr_not_leading_to_static_ptr = dynamic_ptr;
            info->number_to_dst_ptr += 1;
        }
    }
    else if (this == info->static_type && dynamic_ptr == info->static_ptr)
    {
        if (info->above_dst_ptr)
            info->path_dst_ptr_to_static_ptr = path_below;
        else if (info->path_dynamic_ptr_to_static_ptr != public_path)
            info->path_dynamic_ptr_to_static_ptr = path_below;
        info->found_static_ptr = true;
    }
    return 1;
}

void
__class_type_info::display(const void* obj) const
{
    std::cout << "\n__class_type_info::this = " << obj << "  " << name() << '\n';
}

// __si_class_type_info

__si_class_type_info::~__si_class_type_info()
{
}

int
__si_class_type_info::search1(__dynamic_cast_info* info, const void* dynamic_ptr,
                              int path_below) const
{
    if (this == info->static_type)
    {
        if (dynamic_ptr == info->static_ptr)
        {
            if (path_below == public_path)
            {
                info->path_dynamic_ptr_to_static_ptr = public_path;
                return 0;
            }
        }
        return 1;
    }
    return __base_type->search1(info, dynamic_ptr, path_below);
}

int
__si_class_type_info::search2(__dynamic_cast_info* info, const void* dynamic_ptr,
                              int path_below) const
{
    if (this == info->dst_type)
    {
        if (dynamic_ptr == info->dst_ptr_leading_to_static_ptr ||
            dynamic_ptr == info->dst_ptr_not_leading_to_static_ptr)
        {
            if (path_below == public_path)
                info->path_dynamic_ptr_to_dst_ptr = public_path;
        }
        else
        {
            info->path_dynamic_ptr_to_dst_ptr = path_below;
            info->above_dst_ptr = true;
            (void)__base_type->search2(info, dynamic_ptr, public_path);
            info->above_dst_ptr = false;
            if (info->found_static_ptr)
            {
                info->found_static_ptr = false;
                info->dst_ptr_leading_to_static_ptr = dynamic_ptr;
                info->number_to_static_ptr += 1;
                return info->number_to_static_ptr == 1;
            }
            info->dst_ptr_not_leading_to_static_ptr = dynamic_ptr;
            info->number_to_dst_ptr += 1;
        }
        return 1;
    }
    else if (this == info->static_type)
    {
        if (dynamic_ptr == info->static_ptr)
        {
            if (info->above_dst_ptr)
                info->path_dst_ptr_to_static_ptr = path_below;
            else if (info->path_dynamic_ptr_to_static_ptr != public_path)
                info->path_dynamic_ptr_to_static_ptr = path_below;
            info->found_static_ptr = true;
        }
        return 1;
    }
    return __base_type->search2(info, dynamic_ptr, path_below);
}

void
__si_class_type_info::display(const void* obj) const
{
    std::cout << "\n__si_class_type_info::this = " << obj << "  " << name() << '\n';
    __base_type->display(obj);
}

// __vmi_class_type_info

__vmi_class_type_info::~__vmi_class_type_info()
{
}

int
__vmi_class_type_info::search1(__dynamic_cast_info* info, const void* dynamic_ptr,
                               int path_below) const
{
    typedef const __base_class_type_info* Iter;
    if (this == info->static_type)
    {
        if (dynamic_ptr == info->static_ptr)
        {
            if (path_below == public_path)
            {
                info->path_dynamic_ptr_to_static_ptr = public_path;
                return 0;
            }
        }
        return 1;
    }
    for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
    {
        int r = p->search1(info, dynamic_ptr, path_below);
        if (r == 0)
            return 0;
    }
    return 1;
}

int
__base_class_type_info::search1(__dynamic_cast_info* info, const void* dynamic_ptr,
                                int path_below) const
{
    ptrdiff_t offset_to_base = __offset_flags >> __offset_shift;
    if (__offset_flags & __virtual_mask)
    {
        char* vtable = *(char**)dynamic_ptr;
        offset_to_base = (ptrdiff_t)vtable[offset_to_base];
    }
    return __base_type->search1(info, (char*)dynamic_ptr + offset_to_base,
                                (__offset_flags & __public_mask) ? path_below :
                                                                   not_public_path);
}

int
__vmi_class_type_info::search2(__dynamic_cast_info* info, const void* dynamic_ptr,
                               int path_below) const
{
    typedef const __base_class_type_info* Iter;
    if (this == info->dst_type)
    {
        if (dynamic_ptr == info->dst_ptr_leading_to_static_ptr ||
            dynamic_ptr == info->dst_ptr_not_leading_to_static_ptr)
        {
            if (path_below == public_path)
                info->path_dynamic_ptr_to_dst_ptr = public_path;
        }
        else
        {
            info->path_dynamic_ptr_to_dst_ptr = path_below;
            for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
            {
                info->above_dst_ptr = true;
                int r = p->search2(info, dynamic_ptr, public_path);
                info->above_dst_ptr = false;
                if (r == 0)
                    return 0;
                if (info->found_static_ptr)
                {
                    info->found_static_ptr = false;
                    info->dst_ptr_leading_to_static_ptr = dynamic_ptr;
                    info->number_to_static_ptr += 1;
                    if (info->number_to_static_ptr != 1)
                        return 0;
                }
                else
                {
                    info->dst_ptr_not_leading_to_static_ptr = dynamic_ptr;
                    info->number_to_dst_ptr += 1;
                }
            }
        }
        return 1;
    }
    else if (this == info->static_type)
    {
        if (dynamic_ptr == info->static_ptr)
        {
            if (info->above_dst_ptr)
                info->path_dst_ptr_to_static_ptr = path_below;
            else if (info->path_dynamic_ptr_to_static_ptr != public_path)
                info->path_dynamic_ptr_to_static_ptr = path_below;
            info->found_static_ptr = true;
        }
        return 1;
    }
    for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p)
    {
        int r = p->search2(info, dynamic_ptr, path_below);
        if (r == 0)
            return 0;
    }
    return 1;
}

int
__base_class_type_info::search2(__dynamic_cast_info* info, const void* dynamic_ptr,
                                int path_below) const
{
    ptrdiff_t offset_to_base = __offset_flags >> __offset_shift;
    if (__offset_flags & __virtual_mask)
    {
        char* vtable = *(char**)dynamic_ptr;
        offset_to_base = (ptrdiff_t)vtable[offset_to_base];
    }
    return __base_type->search2(info, (char*)dynamic_ptr + offset_to_base,
                                (__offset_flags & __public_mask) ? path_below :
                                                                   not_public_path);
}

void
__vmi_class_type_info::display(const void* obj) const
{
    std::cout << "\n__vmi_class_type_info::this = " << obj << "  " << name() << '\n';
    if (__flags & __non_diamond_repeat_mask)
        std::cout << "__non_diamond_repeat_mask\n";
    if (__flags & __diamond_shaped_mask)
        std::cout << "__diamond_shaped_mask\n";
    std::cout << "__base_count = " << __base_count << '\n';
    for (const __base_class_type_info* p = __base_info; p < __base_info + __base_count; ++p)
        p->display(obj);
}

void
__base_class_type_info::display(const void* obj) const
{
    if (__offset_flags & __virtual_mask)
        std::cout << "__virtual_mask\n";
    if (__offset_flags & __public_mask)
        std::cout << "__public_mask\n";
    ptrdiff_t offset_to_base = __offset_flags >> __offset_shift;
    if (__offset_flags & __virtual_mask)
    {
        char* vtable = *(char**)obj;
        offset_to_base = (ptrdiff_t)vtable[offset_to_base];
    }
    __base_type->display((char*)obj + offset_to_base);
}

// __pbase_type_info

__pbase_type_info::~__pbase_type_info()
{
}

// __pointer_type_info

__pointer_type_info::~__pointer_type_info()
{
}

// __pointer_to_member_type_info

__pointer_to_member_type_info::~__pointer_to_member_type_info()
{
}

// __dynamic_cast

// static_ptr: source address to be adjusted; nonnull, and since the
//   source object is polymorphic, *(void**)static_ptr is a virtual table pointer.
// static_type: static type of the source object.
// dst_type: destination type (the "T" in "dynamic_cast<T>(v)").
// src2dst_offset: a static hint about the location of the
//                 source subobject with respect to the complete object;
//                 special negative values are:
//                     -1: no hint
//                     -2: static_type is not a public base of dst_type
//                     -3: static_type is a multiple public base type but never a
//                         virtual base type
//                 otherwise, the static_type type is a unique public nonvirtual
//                 base type of dst_type at offset src2dst_offset from the
//                 origin of dst_type.
// Returns either one of:
//    1.  dynamic_ptr adjusted from static_ptr given a public path from
//        (static_ptr, static_type) to dst_type without ambiguity, or
//    2.  dynamic_ptr adjusted from static_ptr given a public path from
//        (static_ptr, static_type) to (dynamic_ptr, dynamic_type) and also
//        a public path from (dynamic_ptr, dynamic_type) to dst_type without
//        ambiguity, or
//    3.  nullptr
// Knowns:
//     (dynamic_ptr, dynamic_type) can be derived from static_ptr.
//     dynamic_ptr is a pointer to the complete run time object.
//     dynamic_type is the type of the complete run time object.
//     The type hierarchy is a DAG rooted at (dynamic_ptr, dynamic_type) and
//     traveling up.  Each node represents a distinct sub-object and is
//     uniquely identified by a (const void*, const __class_type_info*) pair.
//     __dynamic_cast is not called if dst_type == static_type, or if dst_type
//     appears as a node above (static_ptr, static_type), or if static_ptr is
//     nullptr.  In these cases the compiler already knows the answer.
//     So if dst_type appears in the DAG, it appears at the root
//     (dst_type == dynamic_type) or between the root and any static_types.
//     No type can appear above a node of the same type in the DAG.  Thus
//     there is only one node with type dynamic_type.
//     A node can appear above more than one node, as well as below more than
//     one node.
//     If dst_type == dynamic_type then there is only one dst_type in the
//        DAG and cases 1 and 2 collapse to the same degenerate case.  And
//        this degenerate case is easier/faster to search:
//        Returns dynamic_ptr if there exists a public path from
//           (dynamic_ptr, dynamic_type) to (static_ptr, static_type),
//           else returns nullptr.
//        This check is purely an optimization and does not impact correctness.
extern "C"
void*
__dynamic_cast(const void* static_ptr,
			   const __class_type_info* static_type,
			   const __class_type_info* dst_type,
			   std::ptrdiff_t src2dst_offset)
{
std::cout << "static_ptr = " << static_ptr << '\n';
std::cout << "static_type = " << static_type << '\n';
std::cout << "dst_type = " << dst_type << '\n';
std::cout << "src2dst_offset = " << src2dst_offset << '\n';
    void** vtable = *(void***)static_ptr;
    ptrdiff_t offset_to_derived = (ptrdiff_t)vtable[-2];
    const void* dynamic_ptr = (const char*)static_ptr + offset_to_derived;
    const __class_type_info* dynamic_type = (const __class_type_info*)vtable[-1];
std::cout << "dynamic_ptr = " << dynamic_ptr << '\n';
std::cout << "dynamic_type = " << dynamic_type << '\n';
dynamic_type->display(dynamic_ptr);

    const void* dst_ptr = 0;
    __dynamic_cast_info info = {dst_type, static_ptr, static_type, src2dst_offset, 0};
    if (dynamic_type == dst_type)
    {
        (void)dynamic_type->search1(&info, dynamic_ptr, public_path);
        if (info.path_dynamic_ptr_to_static_ptr == public_path)
            dst_ptr = dynamic_ptr;
    }
    else
    {
        (void)dynamic_type->search2(&info, dynamic_ptr, public_path);
        switch (info.number_to_static_ptr)
        {
        case 0:
            if (info.number_to_dst_ptr == 1 &&
                    info.path_dynamic_ptr_to_static_ptr == public_path &&
                    info.path_dynamic_ptr_to_dst_ptr == public_path)
                dst_ptr = info.dst_ptr_not_leading_to_static_ptr;
            break;
        case 1:
            if (info.path_dst_ptr_to_static_ptr == public_path)
                dst_ptr = info.dst_ptr_leading_to_static_ptr;
            break;
        }
    }
    return const_cast<void*>(dst_ptr);
}

}  // __cxxabiv1