1 | // Test virtual casts with polymorphic types.
|
---|
2 |
|
---|
3 | /* IMPORTANT: The virtual system has not been finalized. However the
|
---|
4 | * exception system does depend on the work-in-progress version currently
|
---|
5 | * supported. That is also why the tests under the exception directory.
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include <assert.h>
|
---|
9 |
|
---|
10 |
|
---|
11 | struct __cfatid_struct_mono_base {
|
---|
12 | __cfavir_type_info const * parent;
|
---|
13 | };
|
---|
14 |
|
---|
15 | __attribute__(( cfa_linkonce ))
|
---|
16 | struct __cfatid_struct_mono_base __cfatid_mono_base = {
|
---|
17 | (__cfavir_type_info *)0,
|
---|
18 | };
|
---|
19 |
|
---|
20 | struct mono_base_vtable {
|
---|
21 | __cfatid_struct_mono_base const * const __cfavir_typeid;
|
---|
22 | };
|
---|
23 |
|
---|
24 | struct mono_base {
|
---|
25 | mono_base_vtable const * virtual_table;
|
---|
26 | };
|
---|
27 |
|
---|
28 | forall(T)
|
---|
29 | struct __cfatid_struct_mono_child {
|
---|
30 | __cfatid_struct_mono_base const * parent;
|
---|
31 | };
|
---|
32 |
|
---|
33 | forall(T)
|
---|
34 | struct mono_child_vtable {
|
---|
35 | __cfatid_struct_mono_child(T) const * const __cfavir_typeid;
|
---|
36 | };
|
---|
37 |
|
---|
38 | forall(T)
|
---|
39 | struct mono_child {
|
---|
40 | mono_child_vtable(T) const * virtual_table;
|
---|
41 | };
|
---|
42 |
|
---|
43 | __cfatid_struct_mono_child(int) __cfatid_mono_child @= {
|
---|
44 | &__cfatid_mono_base,
|
---|
45 | };
|
---|
46 |
|
---|
47 | mono_child_vtable(int) _mono_child_vtable_instance @= {
|
---|
48 | &__cfatid_mono_child,
|
---|
49 | };
|
---|
50 |
|
---|
51 | void mono_poly_test(void) {
|
---|
52 | mono_child(int) child = { &_mono_child_vtable_instance };
|
---|
53 | mono_base * base = (virtual mono_base *)&child;
|
---|
54 | assert(base);
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | forall(U)
|
---|
59 | struct __cfatid_struct_poly_base {
|
---|
60 | __cfavir_type_info const * parent;
|
---|
61 | };
|
---|
62 |
|
---|
63 | forall(U)
|
---|
64 | struct poly_base_vtable {
|
---|
65 | __cfatid_struct_poly_base(U) const * const __cfavir_typeid;
|
---|
66 | };
|
---|
67 |
|
---|
68 | forall(U)
|
---|
69 | struct poly_base {
|
---|
70 | poly_base_vtable(U) const * virtual_table;
|
---|
71 | };
|
---|
72 |
|
---|
73 | forall(V)
|
---|
74 | struct __cfatid_struct_poly_child {
|
---|
75 | __cfatid_struct_poly_base(V) const * parent;
|
---|
76 | };
|
---|
77 |
|
---|
78 | forall(V)
|
---|
79 | struct poly_child_vtable {
|
---|
80 | __cfatid_struct_poly_child(V) const * const __cfavir_typeid;
|
---|
81 | };
|
---|
82 |
|
---|
83 | forall(V)
|
---|
84 | struct poly_child {
|
---|
85 | poly_child_vtable(V) const * virtual_table;
|
---|
86 | };
|
---|
87 |
|
---|
88 | __cfatid_struct_poly_base(int) __cfatid_poly_base @= {
|
---|
89 | (__cfavir_type_info *)0,
|
---|
90 | };
|
---|
91 | __cfatid_struct_poly_child(int) __cfatid_poly_child = {
|
---|
92 | &__cfatid_poly_base,
|
---|
93 | };
|
---|
94 | poly_child_vtable(int) _poly_child_vtable_instance @= {
|
---|
95 | &__cfatid_poly_child,
|
---|
96 | };
|
---|
97 |
|
---|
98 | void poly_poly_test() {
|
---|
99 | poly_child(int) child = { &_poly_child_vtable_instance };
|
---|
100 | poly_base(int) * base = (virtual poly_base(int) *)&child;
|
---|
101 | assert(base);
|
---|
102 | }
|
---|
103 |
|
---|
104 | int main(void) {
|
---|
105 | mono_poly_test();
|
---|
106 | poly_poly_test();
|
---|
107 | printf( "done\n" );
|
---|
108 | }
|
---|