[f19fbbc] | 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 |
|
---|
[ecfd758] | 10 |
|
---|
| 11 | struct __cfatid_struct_mono_base {
|
---|
[8f910430] | 12 | __cfavir_type_info const * parent;
|
---|
[ecfd758] | 13 | };
|
---|
| 14 |
|
---|
[8f910430] | 15 | __attribute__(( cfa_linkonce ))
|
---|
[ecfd758] | 16 | struct __cfatid_struct_mono_base __cfatid_mono_base = {
|
---|
[8f910430] | 17 | (__cfavir_type_info *)0,
|
---|
[ecfd758] | 18 | };
|
---|
| 19 |
|
---|
[f19fbbc] | 20 | struct mono_base_vtable {
|
---|
[ecfd758] | 21 | __cfatid_struct_mono_base const * const __cfavir_typeid;
|
---|
[f19fbbc] | 22 | };
|
---|
| 23 |
|
---|
| 24 | struct mono_base {
|
---|
| 25 | mono_base_vtable const * virtual_table;
|
---|
| 26 | };
|
---|
| 27 |
|
---|
[ecfd758] | 28 | forall(T)
|
---|
| 29 | struct __cfatid_struct_mono_child {
|
---|
| 30 | __cfatid_struct_mono_base const * parent;
|
---|
| 31 | };
|
---|
| 32 |
|
---|
[fd54fef] | 33 | forall(T)
|
---|
[f19fbbc] | 34 | struct mono_child_vtable {
|
---|
[ecfd758] | 35 | __cfatid_struct_mono_child(T) const * const __cfavir_typeid;
|
---|
[f19fbbc] | 36 | };
|
---|
| 37 |
|
---|
[fd54fef] | 38 | forall(T)
|
---|
[f19fbbc] | 39 | struct mono_child {
|
---|
| 40 | mono_child_vtable(T) const * virtual_table;
|
---|
| 41 | };
|
---|
| 42 |
|
---|
[ecfd758] | 43 | __cfatid_struct_mono_child(int) __cfatid_mono_child @= {
|
---|
| 44 | &__cfatid_mono_base,
|
---|
| 45 | };
|
---|
| 46 |
|
---|
[f19fbbc] | 47 | mono_child_vtable(int) _mono_child_vtable_instance @= {
|
---|
[ecfd758] | 48 | &__cfatid_mono_child,
|
---|
[f19fbbc] | 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 |
|
---|
[ecfd758] | 57 |
|
---|
| 58 | forall(U)
|
---|
| 59 | struct __cfatid_struct_poly_base {
|
---|
[8f910430] | 60 | __cfavir_type_info const * parent;
|
---|
[ecfd758] | 61 | };
|
---|
| 62 |
|
---|
[fd54fef] | 63 | forall(U)
|
---|
[f19fbbc] | 64 | struct poly_base_vtable {
|
---|
[ecfd758] | 65 | __cfatid_struct_poly_base(U) const * const __cfavir_typeid;
|
---|
[f19fbbc] | 66 | };
|
---|
| 67 |
|
---|
[fd54fef] | 68 | forall(U)
|
---|
[f19fbbc] | 69 | struct poly_base {
|
---|
| 70 | poly_base_vtable(U) const * virtual_table;
|
---|
| 71 | };
|
---|
| 72 |
|
---|
[ecfd758] | 73 | forall(V)
|
---|
| 74 | struct __cfatid_struct_poly_child {
|
---|
| 75 | __cfatid_struct_poly_base(V) const * parent;
|
---|
| 76 | };
|
---|
| 77 |
|
---|
[fd54fef] | 78 | forall(V)
|
---|
[f19fbbc] | 79 | struct poly_child_vtable {
|
---|
[ecfd758] | 80 | __cfatid_struct_poly_child(V) const * const __cfavir_typeid;
|
---|
[f19fbbc] | 81 | };
|
---|
| 82 |
|
---|
[fd54fef] | 83 | forall(V)
|
---|
[f19fbbc] | 84 | struct poly_child {
|
---|
| 85 | poly_child_vtable(V) const * virtual_table;
|
---|
| 86 | };
|
---|
| 87 |
|
---|
[ecfd758] | 88 | __cfatid_struct_poly_base(int) __cfatid_poly_base @= {
|
---|
[8f910430] | 89 | (__cfavir_type_info *)0,
|
---|
[f19fbbc] | 90 | };
|
---|
[ecfd758] | 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,
|
---|
[f19fbbc] | 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();
|
---|
[ecfd758] | 107 | printf( "done\n" );
|
---|
[f19fbbc] | 108 | }
|
---|