| [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 |  | 
|---|
|  | 10 | struct mono_base_vtable { | 
|---|
|  | 11 | mono_base_vtable const * const parent; | 
|---|
|  | 12 | }; | 
|---|
|  | 13 |  | 
|---|
|  | 14 | struct mono_base { | 
|---|
|  | 15 | mono_base_vtable const * virtual_table; | 
|---|
|  | 16 | }; | 
|---|
|  | 17 |  | 
|---|
|  | 18 | forall(otype T) | 
|---|
|  | 19 | struct mono_child_vtable { | 
|---|
|  | 20 | mono_base_vtable const * const parent; | 
|---|
|  | 21 | }; | 
|---|
|  | 22 |  | 
|---|
|  | 23 | forall(otype T) | 
|---|
|  | 24 | struct mono_child { | 
|---|
|  | 25 | mono_child_vtable(T) const * virtual_table; | 
|---|
|  | 26 | }; | 
|---|
|  | 27 |  | 
|---|
|  | 28 | mono_base_vtable _mono_base_vtable_instance @= { 0 }; | 
|---|
|  | 29 | mono_child_vtable(int) _mono_child_vtable_instance @= { | 
|---|
|  | 30 | &_mono_base_vtable_instance | 
|---|
|  | 31 | }; | 
|---|
|  | 32 |  | 
|---|
|  | 33 | void mono_poly_test(void) { | 
|---|
|  | 34 | mono_child(int) child = { &_mono_child_vtable_instance }; | 
|---|
|  | 35 | mono_base * base = (virtual mono_base *)&child; | 
|---|
|  | 36 | assert(base); | 
|---|
|  | 37 | } | 
|---|
|  | 38 |  | 
|---|
|  | 39 | forall(otype U) | 
|---|
|  | 40 | struct poly_base_vtable { | 
|---|
|  | 41 | poly_base_vtable(U) const * const parent; | 
|---|
|  | 42 | }; | 
|---|
|  | 43 |  | 
|---|
|  | 44 | forall(otype U) | 
|---|
|  | 45 | struct poly_base { | 
|---|
|  | 46 | poly_base_vtable(U) const * virtual_table; | 
|---|
|  | 47 | }; | 
|---|
|  | 48 |  | 
|---|
|  | 49 | forall(otype V) | 
|---|
|  | 50 | struct poly_child_vtable { | 
|---|
|  | 51 | poly_base_vtable(V) const * const parent; | 
|---|
|  | 52 | }; | 
|---|
|  | 53 |  | 
|---|
|  | 54 | forall(otype V) | 
|---|
|  | 55 | struct poly_child { | 
|---|
|  | 56 | poly_child_vtable(V) const * virtual_table; | 
|---|
|  | 57 | }; | 
|---|
|  | 58 |  | 
|---|
|  | 59 | poly_base_vtable(int) _poly_base_vtable_instance @= { 0 }; | 
|---|
|  | 60 | poly_child_vtable(int) _poly_child_vtable_instance @= { | 
|---|
|  | 61 | &_poly_base_vtable_instance | 
|---|
|  | 62 | }; | 
|---|
|  | 63 | /* Resolver bug keeps me from adding these. | 
|---|
|  | 64 | poly_base_vtable(char) _poly_base_vtable_instance @= { 0 }; | 
|---|
|  | 65 | poly_child_vtable(char) _poly_child_vtable_instance @= { | 
|---|
|  | 66 | &_poly_base_vtable_instance | 
|---|
|  | 67 | }; | 
|---|
|  | 68 | */ | 
|---|
|  | 69 |  | 
|---|
|  | 70 | void poly_poly_test() { | 
|---|
|  | 71 | poly_child(int) child = { &_poly_child_vtable_instance }; | 
|---|
|  | 72 | poly_base(int) * base = (virtual poly_base(int) *)&child; | 
|---|
|  | 73 | assert(base); | 
|---|
|  | 74 | } | 
|---|
|  | 75 |  | 
|---|
|  | 76 | int main(void) { | 
|---|
|  | 77 | mono_poly_test(); | 
|---|
|  | 78 | poly_poly_test(); | 
|---|
|  | 79 | } | 
|---|