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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
|
// Methods for type_info for -*- C++ -*- Run Time Type Identification.
// Copyright (C) 1994, 1996, 1998, 1999, 2000 Free Software Foundation
// This file is part of GNU CC.
// GNU CC is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// GNU CC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with GNU CC; see the file COPYING. If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
// As a special exception, if you link this library with other files,
// some of which are compiled with GCC, to produce an executable,
// this library does not by itself cause the resulting executable
// to be covered by the GNU General Public License.
// This exception does not however invalidate any other reasons why
// the executable file might be covered by the GNU General Public License.
#pragma implementation "typeinfo"
#include <stddef.h>
#include "tinfo.h"
#include "new" // for placement new
// This file contains the minimal working set necessary to link with code
// that uses virtual functions and -frtti but does not actually use RTTI
// functionality.
std::type_info::
~type_info ()
{ }
#if !defined(__GXX_ABI_VERSION) || __GXX_ABI_VERSION < 100
// original (old) abi
namespace
{
// ADDR is a pointer to an object. Convert it to a pointer to a base,
// using OFFSET.
inline void*
convert_to_base (void *addr, bool is_virtual, myint32 offset)
{
if (!addr)
return NULL;
if (!is_virtual)
return (char *) addr + offset;
// Under the old ABI, the offset gives us the address of a pointer
// to the virtual base.
return *((void **) ((char *) addr + offset));
}
}
// We can't rely on common symbols being shared between shared objects.
bool std::type_info::
operator== (const std::type_info& arg) const
{
return (&arg == this) || (strcmp (name (), arg.name ()) == 0);
}
extern "C" void
__rtti_class (void *addr, const char *name,
const __class_type_info::base_info *bl, size_t bn)
{ new (addr) __class_type_info (name, bl, bn); }
extern "C" void
__rtti_si (void *addr, const char *n, const std::type_info *ti)
{
new (addr) __si_type_info
(n, static_cast <const __user_type_info &> (*ti));
}
extern "C" void
__rtti_user (void *addr, const char *name)
{ new (addr) __user_type_info (name); }
// Upcast for catch checking. OBJPTR points to the thrown object and might be
// NULL. Return 0 on failure, non-zero on success. Set *ADJPTR to adjusted
// object pointer.
int __user_type_info::
upcast (const type_info &target, void *objptr,
void **adjptr) const
{
upcast_result result;
if (do_upcast (contained_public, target, objptr, result))
return 0;
*adjptr = result.target_obj;
return contained_public_p (result.whole2target);
}
// Down or cross cast for dynamic_cast. OBJPTR points to the most derrived
// object, SUBPTR points to the static base object. Both must not be NULL.
// TARGET specifies the desired target type, SUBTYPE specifies the static
// type. Both must be defined. Returns adjusted object pointer on success,
// NULL on failure. [expr.dynamic.cast]/8 says 'unambiguous public base'. This
// itself is an ambiguous statement. We choose it to mean the base must be
// separately unambiguous and public, rather than unambiguous considering only
// public bases.
void *__user_type_info::
dyncast (int boff,
const type_info &target, void *objptr,
const type_info &subtype, void *subptr) const
{
dyncast_result result;
do_dyncast (boff, contained_public,
target, objptr, subtype, subptr, result);
if (!result.target_obj)
return NULL;
if (contained_public_p (result.target2sub))
return result.target_obj;
if (contained_public_p (sub_kind (result.whole2sub & result.whole2target)))
// Found a valid cross cast
return result.target_obj;
if (contained_nonvirtual_p (result.whole2sub))
// Found an invalid cross cast, which cannot also be a down cast
return NULL;
if (result.target2sub == unknown)
result.target2sub = static_cast <const __user_type_info &> (target)
.find_public_subobj (boff, subtype,
result.target_obj, subptr);
if (contained_public_p (result.target2sub))
// Found a valid down cast
return result.target_obj;
// Must be an invalid down cast, or the cross cast wasn't bettered
return NULL;
}
// Catch cast helper. ACCESS_PATH is the access from the complete thrown
// object to this base. TARGET is the desired type we want to catch. OBJPTR
// points to this base within the throw object, it might be NULL. Fill in
// RESULT with what we find. Return true, should we determine catch must fail.
bool __user_type_info::
do_upcast (sub_kind access_path,
const type_info &target, void *objptr,
upcast_result &__restrict result) const
{
if (*this == target)
{
result.target_obj = objptr;
result.base_type = nonvirtual_base_type;
result.whole2target = access_path;
return contained_nonpublic_p (access_path);
}
return false;
}
// dynamic cast helper. ACCESS_PATH gives the access from the most derived
// object to this base. TARGET indicates the desired type we want. OBJPTR
// points to this base within the object. SUBTYPE indicates the static type
// started from and SUBPTR points to that base within the most derived object.
// Fill in RESULT with what we find. Return true if we have located an
// ambiguous match.
bool __user_type_info::
do_dyncast (int, sub_kind access_path,
const type_info &target, void *objptr,
const type_info &subtype, void *subptr,
dyncast_result &__restrict result) const
{
if (objptr == subptr && *this == subtype)
{
// The subobject we started from. Indicate how we are accessible from
// the most derived object.
result.whole2sub = access_path;
return false;
}
if (*this == target)
{
result.target_obj = objptr;
result.whole2target = access_path;
result.target2sub = not_contained;
return false;
}
return false;
}
// find_public_subobj helper. Return contained_public if we are the desired
// subtype. OBJPTR points to this base type, SUBPTR points to the desired base
// object.
__user_type_info::sub_kind __user_type_info::
do_find_public_subobj (int, const type_info &, void *objptr, void *subptr) const
{
if (subptr == objptr)
// Must be our type, as the pointers match.
return contained_public;
return not_contained;
}
// catch helper for single public inheritance types. See
// __user_type_info::do_upcast for semantics.
bool __si_type_info::
do_upcast (sub_kind access_path,
const type_info &target, void *objptr,
upcast_result &__restrict result) const
{
if (*this == target)
{
result.target_obj = objptr;
result.base_type = nonvirtual_base_type;
result.whole2target = access_path;
return contained_nonpublic_p (access_path);
}
return base.do_upcast (access_path, target, objptr, result);
}
// dynamic cast helper for single public inheritance types. See
// __user_type_info::do_dyncast for semantics. BOFF indicates how SUBTYPE
// types are inherited by TARGET types.
bool __si_type_info::
do_dyncast (int boff, sub_kind access_path,
const type_info &target, void *objptr,
const type_info &subtype, void *subptr,
dyncast_result &__restrict result) const
{
if (objptr == subptr && *this == subtype)
{
// The subobject we started from. Indicate how we are accessible from
// the most derived object.
result.whole2sub = access_path;
return false;
}
if (*this == target)
{
result.target_obj = objptr;
result.whole2target = access_path;
if (boff >= 0)
result.target2sub = ((char *)subptr - (char *)objptr) == boff
? contained_public : not_contained;
else if (boff == -2)
result.target2sub = not_contained;
return false;
}
return base.do_dyncast (boff, access_path,
target, objptr, subtype, subptr, result);
}
// find_public_subobj helper. See __user_type_info::do_find_public_subobj or
// semantics. BOFF indicates how SUBTYPE types are inherited by the original
// target object.
__user_type_info::sub_kind __si_type_info::
do_find_public_subobj (int boff, const type_info &subtype, void *objptr, void *subptr) const
{
if (subptr == objptr && subtype == *this)
return contained_public;
return base.do_find_public_subobj (boff, subtype, objptr, subptr);
}
// catch helper for multiple or non-public inheritance types. See
// __user_type_info::do_upcast for semantics.
bool __class_type_info::
do_upcast (sub_kind access_path,
const type_info &target, void *objptr,
upcast_result &__restrict result) const
{
if (*this == target)
{
result.target_obj = objptr;
result.base_type = nonvirtual_base_type;
result.whole2target = access_path;
return contained_nonpublic_p (access_path);
}
for (size_t i = n_bases; i--;)
{
upcast_result result2;
void *p = objptr;
sub_kind sub_access = access_path;
p = convert_to_base (p,
base_list[i].is_virtual,
base_list[i].offset);
if (base_list[i].is_virtual)
sub_access = sub_kind (sub_access | contained_virtual_mask);
if (base_list[i].access != PUBLIC)
sub_access = sub_kind (sub_access & ~contained_public_mask);
if (base_list[i].base->do_upcast (sub_access, target, p, result2))
return true; // must fail
if (result2.base_type)
{
if (result2.base_type == nonvirtual_base_type
&& base_list[i].is_virtual)
result2.base_type = base_list[i].base;
if (!result.base_type)
result = result2;
else if (result.target_obj != result2.target_obj)
{
// Found an ambiguity.
result.target_obj = NULL;
result.whole2target = contained_ambig;
return true;
}
else if (result.target_obj)
{
// Ok, found real object via a virtual path.
result.whole2target
= sub_kind (result.whole2target | result2.whole2target);
}
else
{
// Dealing with a null pointer, need to check vbase
// containing each of the two choices.
if (result2.base_type == nonvirtual_base_type
|| result.base_type == nonvirtual_base_type
|| !(*result2.base_type == *result.base_type))
{
// Already ambiguous, not virtual or via different virtuals.
// Cannot match.
result.whole2target = contained_ambig;
return true;
}
}
}
}
return false;
}
// dynamic cast helper for non-public or multiple inheritance types. See
// __user_type_info::do_dyncast for overall semantics.
// This is a big hairy function. Although the run-time behaviour of
// dynamic_cast is simple to describe, it gives rise to some non-obvious
// behaviour. We also desire to determine as early as possible any definite
// answer we can get. Because it is unknown what the run-time ratio of
// succeeding to failing dynamic casts is, we do not know in which direction
// to bias any optimizations. To that end we make no particular effort towards
// early fail answers or early success answers. Instead we try to minimize
// work by filling in things lazily (when we know we need the information),
// and opportunisticly take early success or failure results.
bool __class_type_info::
do_dyncast (int boff, sub_kind access_path,
const type_info &target, void *objptr,
const type_info &subtype, void *subptr,
dyncast_result &__restrict result) const
{
if (objptr == subptr && *this == subtype)
{
// The subobject we started from. Indicate how we are accessible from
// the most derived object.
result.whole2sub = access_path;
return false;
}
if (*this == target)
{
result.target_obj = objptr;
result.whole2target = access_path;
if (boff >= 0)
result.target2sub = ((char *)subptr - (char *)objptr) == boff
? contained_public : not_contained;
else if (boff == -2)
result.target2sub = not_contained;
return false;
}
bool result_ambig = false;
for (size_t i = n_bases; i--;)
{
dyncast_result result2;
void *p;
sub_kind sub_access = access_path;
p = convert_to_base (objptr,
base_list[i].is_virtual,
base_list[i].offset);
if (base_list[i].is_virtual)
sub_access = sub_kind (sub_access | contained_virtual_mask);
if (base_list[i].access != PUBLIC)
sub_access = sub_kind (sub_access & ~contained_public_mask);
bool result2_ambig
= base_list[i].base->do_dyncast (boff, sub_access,
target, p, subtype, subptr, result2);
result.whole2sub = sub_kind (result.whole2sub | result2.whole2sub);
if (result2.target2sub == contained_public
|| result2.target2sub == contained_ambig)
{
result.target_obj = result2.target_obj;
result.whole2target = result2.whole2target;
result.target2sub = result2.target2sub;
// Found a downcast which can't be bettered or an ambiguous downcast
// which can't be disambiguated
return result2_ambig;
}
if (!result_ambig && !result.target_obj)
{
// Not found anything yet.
result.target_obj = result2.target_obj;
result.whole2target = result2.whole2target;
result_ambig = result2_ambig;
}
else if (result.target_obj && result.target_obj == result2.target_obj)
{
// Found at same address, must be via virtual. Pick the most
// accessible path.
result.whole2target =
sub_kind (result.whole2target | result2.whole2target);
}
else if ((result.target_obj && result2.target_obj)
|| (result_ambig && result2.target_obj)
|| (result2_ambig && result.target_obj))
{
// Found two different TARGET bases, or a valid one and a set of
// ambiguous ones, must disambiguate. See whether SUBOBJ is
// contained publicly within one of the non-ambiguous choices.
// If it is in only one, then that's the choice. If it is in
// both, then we're ambiguous and fail. If it is in neither,
// we're ambiguous, but don't yet fail as we might later find a
// third base which does contain SUBPTR.
sub_kind new_sub_kind = result2.target2sub;
sub_kind old_sub_kind = result.target2sub;
if (contained_nonvirtual_p (result.whole2sub))
{
// We already found SUBOBJ as a non-virtual base of most
// derived. Therefore if it is in either choice, it can only be
// in one of them, and we will already know.
if (old_sub_kind == unknown)
old_sub_kind = not_contained;
if (new_sub_kind == unknown)
new_sub_kind = not_contained;
}
else
{
const __user_type_info &t =
static_cast <const __user_type_info &> (target);
if (old_sub_kind >= not_contained)
;// already calculated
else if (contained_nonvirtual_p (new_sub_kind))
// Already found non-virtually inside the other choice,
// cannot be in this.
old_sub_kind = not_contained;
else
old_sub_kind = t.find_public_subobj (boff, subtype,
result.target_obj, subptr);
if (new_sub_kind >= not_contained)
;// already calculated
else if (contained_nonvirtual_p (old_sub_kind))
// Already found non-virtually inside the other choice,
// cannot be in this.
new_sub_kind = not_contained;
else
new_sub_kind = t.find_public_subobj (boff, subtype,
result2.target_obj, subptr);
}
// Neither sub_kind can be contained_ambig -- we bail out early
// when we find those.
if (contained_p (sub_kind (new_sub_kind ^ old_sub_kind)))
{
// Only on one choice, not ambiguous.
if (contained_p (new_sub_kind))
{
// Only in new.
result.target_obj = result2.target_obj;
result.whole2target = result2.whole2target;
result_ambig = false;
old_sub_kind = new_sub_kind;
}
result.target2sub = old_sub_kind;
if (result.target2sub == contained_public)
return false; // Can't be an ambiguating downcast for later discovery.
}
else if (contained_p (sub_kind (new_sub_kind & old_sub_kind)))
{
// In both.
result.target_obj = NULL;
result.target2sub = contained_ambig;
return true; // Fail.
}
else
{
// In neither publicly, ambiguous for the moment, but keep
// looking. It is possible that it was private in one or
// both and therefore we should fail, but that's just tough.
result.target_obj = NULL;
result.target2sub = not_contained;
result_ambig = true;
}
}
if (result.whole2sub == contained_private)
// We found SUBOBJ as a private non-virtual base, therefore all
// cross casts will fail. We have already found a down cast, if
// there is one.
return result_ambig;
}
return result_ambig;
}
// find_public_subobj helper for non-public or multiple inheritance types. See
// __user_type_info::do_find_public_subobj for semantics. We make use of BOFF
// to prune the base class walk.
__user_type_info::sub_kind __class_type_info::
do_find_public_subobj (int boff, const type_info &subtype, void *objptr, void *subptr) const
{
if (objptr == subptr && subtype == *this)
return contained_public;
for (size_t i = n_bases; i--;)
{
if (base_list[i].access != PUBLIC)
continue; // Not public, can't be here.
void *p;
if (base_list[i].is_virtual && boff == -3)
// Not a virtual base, so can't be here.
continue;
p = convert_to_base (objptr,
base_list[i].is_virtual,
base_list[i].offset);
sub_kind base_kind = base_list[i].base->do_find_public_subobj
(boff, subtype, p, subptr);
if (contained_p (base_kind))
{
if (base_list[i].is_virtual)
base_kind = sub_kind (base_kind | contained_virtual_mask);
return base_kind;
}
}
return not_contained;
}
#else
// new abi
namespace std {
// return true if this is a type_info for a pointer type
bool type_info::
is_pointer_p () const
{
return false;
}
// return true if this is a type_info for a function type
bool type_info::
is_function_p () const
{
return false;
}
// try and catch a thrown object.
bool type_info::
do_catch (const type_info *thr_type, void **, unsigned) const
{
return *this == *thr_type;
}
// upcast from this type to the target. __class_type_info will override
bool type_info::
do_upcast (const abi::__class_type_info *, void **) const
{
return false;
}
};
namespace {
using namespace std;
using namespace abi;
// initial part of a vtable, this structure is used with offsetof, so we don't
// have to keep alignments consistent manually.
struct vtable_prefix {
ptrdiff_t whole_object; // offset to most derived object
const __class_type_info *whole_type; // pointer to most derived type_info
const void *origin; // what a class's vptr points to
};
template <typename T>
inline const T *
adjust_pointer (const void *base, ptrdiff_t offset)
{
return reinterpret_cast <const T *>
(reinterpret_cast <const char *> (base) + offset);
}
// some predicate functions for __class_type_info::sub_kind
inline bool contained_p (__class_type_info::sub_kind access_path)
{
return access_path >= __class_type_info::contained_mask;
}
inline bool public_p (__class_type_info::sub_kind access_path)
{
return access_path & __class_type_info::contained_public_mask;
}
inline bool virtual_p (__class_type_info::sub_kind access_path)
{
return (access_path & __class_type_info::contained_virtual_mask);
}
inline bool contained_public_p (__class_type_info::sub_kind access_path)
{
return (access_path & __class_type_info::contained_public) == __class_type_info::contained_public;
}
inline bool contained_nonpublic_p (__class_type_info::sub_kind access_path)
{
return (access_path & __class_type_info::contained_public) == __class_type_info::contained_mask;
}
inline bool contained_nonvirtual_p (__class_type_info::sub_kind access_path)
{
return (access_path & (__class_type_info::contained_mask | __class_type_info::contained_virtual_mask))
== __class_type_info::contained_mask;
}
static const __class_type_info *const nonvirtual_base_type =
static_cast <const __class_type_info *> (0) + 1;
}; // namespace
namespace __cxxabiv1
{
__class_type_info::
~__class_type_info ()
{}
__si_class_type_info::
~__si_class_type_info ()
{}
__vmi_class_type_info::
~__vmi_class_type_info ()
{}
bool __class_type_info::
do_catch (const type_info *thr_type, void **thr_obj,
unsigned outer) const
{
if (*this == *thr_type)
return true;
if (outer >= 4)
// Neither `A' nor `A *'.
return false;
return thr_type->do_upcast (this, thr_obj);
}
bool __class_type_info::
do_upcast (const __class_type_info *dst_type, void **obj_ptr) const
{
upcast_result result (details);
if (do_upcast (contained_public, dst_type, *obj_ptr, result))
return false;
*obj_ptr = const_cast <void *> (result.dst_ptr);
return contained_public_p (result.whole2dst);
}
inline __class_type_info::sub_kind __class_type_info::
find_public_src (ptrdiff_t src2dst,
const void *obj_ptr,
const __class_type_info *src_type,
const void *src_ptr) const
{
if (src2dst >= 0)
return adjust_pointer <void> (obj_ptr, src2dst) == src_ptr
? contained_public : not_contained;
if (src2dst == -2)
return not_contained;
return do_find_public_src (src2dst, obj_ptr, src_type, src_ptr);
}
__class_type_info::sub_kind __class_type_info::
do_find_public_src (ptrdiff_t,
const void *obj_ptr,
const __class_type_info *,
const void *src_ptr) const
{
if (src_ptr == obj_ptr)
// Must be our type, as the pointers match.
return contained_public;
return not_contained;
}
__class_type_info::sub_kind __si_class_type_info::
do_find_public_src (ptrdiff_t src2dst,
const void *obj_ptr,
const __class_type_info *src_type,
const void *src_ptr) const
{
if (src_ptr == obj_ptr && *this == *src_type)
return contained_public;
return base->do_find_public_src (src2dst, obj_ptr, src_type, src_ptr);
}
__class_type_info::sub_kind __vmi_class_type_info::
do_find_public_src (ptrdiff_t src2dst,
const void *obj_ptr,
const __class_type_info *src_type,
const void *src_ptr) const
{
if (obj_ptr == src_ptr && *this == *src_type)
return contained_public;
for (size_t i = n_bases; i--;)
{
if (!base_list[i].is_public_p ())
continue; // Not public, can't be here.
const void *base = obj_ptr;
ptrdiff_t offset = base_list[i].offset;
if (base_list[i].is_virtual_p ())
{
if (src2dst == -3)
continue; // Not a virtual base, so can't be here.
const ptrdiff_t *vtable = *static_cast <const ptrdiff_t *const *> (base);
offset = vtable[offset];
}
base = adjust_pointer <void> (base, offset);
sub_kind base_kind = base_list[i].base->do_find_public_src
(src2dst, base, src_type, src_ptr);
if (contained_p (base_kind))
{
if (base_list[i].is_virtual_p ())
base_kind = sub_kind (base_kind | contained_virtual_mask);
return base_kind;
}
}
return not_contained;
}
bool __class_type_info::
do_dyncast (ptrdiff_t,
sub_kind access_path,
const __class_type_info *dst_type,
const void *obj_ptr,
const __class_type_info *src_type,
const void *src_ptr,
dyncast_result &__restrict result) const
{
if (obj_ptr == src_ptr && *this == *src_type)
{
// The src object we started from. Indicate how we are accessible from
// the most derived object.
result.whole2src = access_path;
return false;
}
if (*this == *dst_type)
{
result.dst_ptr = obj_ptr;
result.whole2dst = access_path;
result.dst2src = not_contained;
return false;
}
return false;
}
bool __si_class_type_info::
do_dyncast (ptrdiff_t src2dst,
sub_kind access_path,
const __class_type_info *dst_type,
const void *obj_ptr,
const __class_type_info *src_type,
const void *src_ptr,
dyncast_result &__restrict result) const
{
if (*this == *dst_type)
{
result.dst_ptr = obj_ptr;
result.whole2dst = access_path;
if (src2dst >= 0)
result.dst2src = adjust_pointer <void> (obj_ptr, src2dst) == src_ptr
? contained_public : not_contained;
else if (src2dst == -2)
result.dst2src = not_contained;
return false;
}
if (obj_ptr == src_ptr && *this == *src_type)
{
// The src object we started from. Indicate how we are accessible from
// the most derived object.
result.whole2src = access_path;
return false;
}
return base->do_dyncast (src2dst, access_path, dst_type, obj_ptr,
src_type, src_ptr, result);
}
// This is a big hairy function. Although the run-time behaviour of
// dynamic_cast is simple to describe, it gives rise to some non-obvious
// behaviour. We also desire to determine as early as possible any definite
// answer we can get. Because it is unknown what the run-time ratio of
// succeeding to failing dynamic casts is, we do not know in which direction
// to bias any optimizations. To that end we make no particular effort towards
// early fail answers or early success answers. Instead we try to minimize
// work by filling in things lazily (when we know we need the information),
// and opportunisticly take early success or failure results.
bool __vmi_class_type_info::
do_dyncast (ptrdiff_t src2dst,
sub_kind access_path,
const __class_type_info *dst_type,
const void *obj_ptr,
const __class_type_info *src_type,
const void *src_ptr,
dyncast_result &__restrict result) const
{
if (obj_ptr == src_ptr && *this == *src_type)
{
// The src object we started from. Indicate how we are accessible from
// the most derived object.
result.whole2src = access_path;
return false;
}
if (*this == *dst_type)
{
result.dst_ptr = obj_ptr;
result.whole2dst = access_path;
if (src2dst >= 0)
result.dst2src = adjust_pointer <void> (obj_ptr, src2dst) == src_ptr
? contained_public : not_contained;
else if (src2dst == -2)
result.dst2src = not_contained;
return false;
}
bool result_ambig = false;
for (size_t i = n_bases; i--;)
{
dyncast_result result2;
void const *base = obj_ptr;
sub_kind base_access = access_path;
ptrdiff_t offset = base_list[i].offset;
if (base_list[i].is_virtual_p ())
{
base_access = sub_kind (base_access | contained_virtual_mask);
const ptrdiff_t *vtable = *static_cast <const ptrdiff_t *const *> (base);
offset = vtable[offset];
}
base = adjust_pointer <void> (base, offset);
if (!base_list[i].is_public_p ())
base_access = sub_kind (base_access & ~contained_public_mask);
bool result2_ambig
= base_list[i].base->do_dyncast (src2dst, base_access,
dst_type, base,
src_type, src_ptr, result2);
result.whole2src = sub_kind (result.whole2src | result2.whole2src);
if (result2.dst2src == contained_public
|| result2.dst2src == contained_ambig)
{
result.dst_ptr = result2.dst_ptr;
result.whole2dst = result2.whole2dst;
result.dst2src = result2.dst2src;
// Found a downcast which can't be bettered or an ambiguous downcast
// which can't be disambiguated
return result2_ambig;
}
if (!result_ambig && !result.dst_ptr)
{
// Not found anything yet.
result.dst_ptr = result2.dst_ptr;
result.whole2dst = result2.whole2dst;
result_ambig = result2_ambig;
}
else if (result.dst_ptr && result.dst_ptr == result2.dst_ptr)
{
// Found at same address, must be via virtual. Pick the most
// accessible path.
result.whole2dst =
sub_kind (result.whole2dst | result2.whole2dst);
}
else if ((result.dst_ptr && result2.dst_ptr)
|| (result_ambig && result2.dst_ptr)
|| (result2_ambig && result.dst_ptr))
{
// Found two different DST_TYPE bases, or a valid one and a set of
// ambiguous ones, must disambiguate. See whether SRC_PTR is
// contained publicly within one of the non-ambiguous choices. If it
// is in only one, then that's the choice. If it is in both, then
// we're ambiguous and fail. If it is in neither, we're ambiguous,
// but don't yet fail as we might later find a third base which does
// contain SRC_PTR.
sub_kind new_sub_kind = result2.dst2src;
sub_kind old_sub_kind = result.dst2src;
if (contained_nonvirtual_p (result.whole2src))
{
// We already found SRC_PTR as a non-virtual base of most
// derived. Therefore if it is in either choice, it can only be
// in one of them, and we will already know.
if (old_sub_kind == unknown)
old_sub_kind = not_contained;
if (new_sub_kind == unknown)
new_sub_kind = not_contained;
}
else
{
if (old_sub_kind >= not_contained)
;// already calculated
else if (contained_nonvirtual_p (new_sub_kind))
// Already found non-virtually inside the other choice,
// cannot be in this.
old_sub_kind = not_contained;
else
old_sub_kind = dst_type->find_public_src
(src2dst, result.dst_ptr, src_type, src_ptr);
if (new_sub_kind >= not_contained)
;// already calculated
else if (contained_nonvirtual_p (old_sub_kind))
// Already found non-virtually inside the other choice,
// cannot be in this.
new_sub_kind = not_contained;
else
new_sub_kind = dst_type->find_public_src
(src2dst, result2.dst_ptr, src_type, src_ptr);
}
// Neither sub_kind can be contained_ambig -- we bail out early
// when we find those.
if (contained_p (sub_kind (new_sub_kind ^ old_sub_kind)))
{
// Only on one choice, not ambiguous.
if (contained_p (new_sub_kind))
{
// Only in new.
result.dst_ptr = result2.dst_ptr;
result.whole2dst = result2.whole2dst;
result_ambig = false;
old_sub_kind = new_sub_kind;
}
result.dst2src = old_sub_kind;
if (public_p (result.dst2src))
return false; // Can't be an ambiguating downcast for later discovery.
if (!virtual_p (result.dst2src))
return false; // Found non-virtually can't be bettered
}
else if (contained_p (sub_kind (new_sub_kind & old_sub_kind)))
{
// In both.
result.dst_ptr = NULL;
result.dst2src = contained_ambig;
return true; // Fail.
}
else
{
// In neither publicly, ambiguous for the moment, but keep
// looking. It is possible that it was private in one or
// both and therefore we should fail, but that's just tough.
result.dst_ptr = NULL;
result.dst2src = not_contained;
result_ambig = true;
}
}
if (result.whole2src == contained_private)
// We found SRC_PTR as a private non-virtual base, therefore all
// cross casts will fail. We have already found a down cast, if
// there is one.
return result_ambig;
}
return result_ambig;
}
bool __class_type_info::
do_upcast (sub_kind access_path,
const __class_type_info *dst, const void *obj,
upcast_result &__restrict result) const
{
if (*this == *dst)
{
result.dst_ptr = obj;
result.base_type = nonvirtual_base_type;
result.whole2dst = access_path;
return contained_nonpublic_p (access_path);
}
return false;
}
bool __si_class_type_info::
do_upcast (sub_kind access_path,
const __class_type_info *dst, const void *obj_ptr,
upcast_result &__restrict result) const
{
if (*this == *dst)
{
result.dst_ptr = obj_ptr;
result.base_type = nonvirtual_base_type;
result.whole2dst = access_path;
return contained_nonpublic_p (access_path);
}
return base->do_upcast (access_path, dst, obj_ptr, result);
}
bool __vmi_class_type_info::
do_upcast (sub_kind access_path,
const __class_type_info *dst, const void *obj_ptr,
upcast_result &__restrict result) const
{
if (*this == *dst)
{
result.dst_ptr = obj_ptr;
result.base_type = nonvirtual_base_type;
result.whole2dst = access_path;
return contained_nonpublic_p (access_path);
}
for (size_t i = n_bases; i--;)
{
upcast_result result2 (result.src_details);
const void *base = obj_ptr;
sub_kind sub_access = access_path;
ptrdiff_t offset = base_list[i].offset;
if (!base_list[i].is_public_p ())
{
if (!(result.src_details & multiple_base_mask))
// original cannot have an ambiguous base
continue;
sub_access = sub_kind (sub_access & ~contained_public_mask);
}
if (base_list[i].is_virtual_p ())
{
sub_access = sub_kind (sub_access | contained_virtual_mask);
if (base)
{
const ptrdiff_t *vtable = *static_cast <const ptrdiff_t *const *> (base);
offset = vtable[offset];
}
}
if (base)
base = adjust_pointer <void> (base, offset);
if (base_list[i].base->do_upcast (sub_access, dst, base, result2))
return true; // must fail
if (result2.base_type)
{
if (result2.base_type == nonvirtual_base_type
&& base_list[i].is_virtual_p ())
result2.base_type = base_list[i].base;
if (!result.base_type)
{
result = result2;
if (!(details & multiple_base_mask))
// cannot have an ambiguous other base
return false;
}
else if (result.dst_ptr != result2.dst_ptr)
{
// Found an ambiguity.
result.dst_ptr = NULL;
result.whole2dst = contained_ambig;
return true;
}
else if (result.dst_ptr)
{
// Ok, found real object via a virtual path.
result.whole2dst
= sub_kind (result.whole2dst | result2.whole2dst);
}
else
{
// Dealing with a null pointer, need to check vbase
// containing each of the two choices.
if (result2.base_type == nonvirtual_base_type
|| result.base_type == nonvirtual_base_type
|| !(*result2.base_type == *result.base_type))
{
// Already ambiguous, not virtual or via different virtuals.
// Cannot match.
result.whole2dst = contained_ambig;
return true;
}
}
}
}
return false;
}
// this is the external interface to the dynamic cast machinery
void *
__dynamic_cast (const void *src_ptr, // object started from
const __class_type_info *src_type, // type of the starting object
const __class_type_info *dst_type, // desired target type
ptrdiff_t src2dst) // how src and dst are related
{
const void *vtable = *static_cast <const void *const *> (src_ptr);
const vtable_prefix *prefix =
adjust_pointer <vtable_prefix> (vtable, 0);
// FIXME: the above offset should be -offsetof (vtable_prefix, origin));
// but we don't currently layout vtables correctly.
const void *whole_ptr =
adjust_pointer <void> (src_ptr, prefix->whole_object);
const __class_type_info *whole_type = prefix->whole_type;
__class_type_info::dyncast_result result;
whole_type->do_dyncast (src2dst, __class_type_info::contained_public,
dst_type, whole_ptr, src_type, src_ptr, result);
if (!result.dst_ptr)
return NULL;
if (contained_public_p (result.dst2src))
return const_cast <void *> (result.dst_ptr);
if (contained_public_p (__class_type_info::sub_kind (result.whole2src & result.whole2dst)))
// Found a valid cross cast
return const_cast <void *> (result.dst_ptr);
if (contained_nonvirtual_p (result.whole2src))
// Found an invalid cross cast, which cannot also be a down cast
return NULL;
if (!(whole_type->details & __class_type_info::private_base_mask))
// whole type has no private bases
return const_cast <void *> (result.dst_ptr);
if (result.dst2src == __class_type_info::unknown)
result.dst2src = dst_type->find_public_src (src2dst, result.dst_ptr,
src_type, src_ptr);
if (contained_public_p (result.dst2src))
// Found a valid down cast
return const_cast <void *> (result.dst_ptr);
// Must be an invalid down cast, or the cross cast wasn't bettered
return NULL;
}
}; // namespace __cxxabiv1
#endif
|