| 1 | // Testing the virtual cast, as part of strict inheritance.
 | 
|---|
| 2 | 
 | 
|---|
| 3 | /* IMPORTANT: This test does not repersent the final feature set.
 | 
|---|
| 4 |  * We are missing a number of important aspects such as:
 | 
|---|
| 5 |  * + vtable type generation.
 | 
|---|
| 6 |  * + vtable instance generation, that might use different resolution rules.
 | 
|---|
| 7 |  * + Virtual syntax to force said generation on structures and traits.
 | 
|---|
| 8 |  * + Trait references/pointers that do the virtual_table lookup.
 | 
|---|
| 9 |  */
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #include <stdlib.hfa>
 | 
|---|
| 12 | #include <assert.h>
 | 
|---|
| 13 | 
 | 
|---|
| 14 | 
 | 
|---|
| 15 | 
 | 
|---|
| 16 | // Hand defined alpha virtual type:
 | 
|---|
| 17 | struct __cfatid_struct_alpha {
 | 
|---|
| 18 |         __cfavir_type_info parent;
 | 
|---|
| 19 | };
 | 
|---|
| 20 | 
 | 
|---|
| 21 | __attribute__(( cfa_linkonce ))
 | 
|---|
| 22 | struct __cfatid_struct_alpha __cfatid_alpha = {
 | 
|---|
| 23 |         (__cfavir_type_info *)0,
 | 
|---|
| 24 | };
 | 
|---|
| 25 | 
 | 
|---|
| 26 | struct alpha_vtable {
 | 
|---|
| 27 |         struct __cfatid_struct_alpha const * const __cfavir_typeid;
 | 
|---|
| 28 |         char (*code)(void);
 | 
|---|
| 29 | };
 | 
|---|
| 30 | 
 | 
|---|
| 31 | struct alpha {
 | 
|---|
| 32 |         alpha_vtable const * virtual_table;
 | 
|---|
| 33 | };
 | 
|---|
| 34 | 
 | 
|---|
| 35 | char ret_a(void) {
 | 
|---|
| 36 |         return 'a';
 | 
|---|
| 37 | }
 | 
|---|
| 38 | 
 | 
|---|
| 39 | 
 | 
|---|
| 40 | 
 | 
|---|
| 41 | // Hand defined beta virtual type:
 | 
|---|
| 42 | struct __cfatid_struct_beta {
 | 
|---|
| 43 |         __cfatid_struct_alpha const * parent;
 | 
|---|
| 44 | };
 | 
|---|
| 45 | 
 | 
|---|
| 46 | __attribute__(( section(".gnu.linkonce.__cfatid_beta") ))
 | 
|---|
| 47 | struct __cfatid_struct_beta __cfatid_beta = {
 | 
|---|
| 48 |         &__cfatid_alpha,
 | 
|---|
| 49 | };
 | 
|---|
| 50 | 
 | 
|---|
| 51 | struct beta_vtable {
 | 
|---|
| 52 |         struct __cfatid_struct_beta const * const __cfavir_typeid;
 | 
|---|
| 53 |         char (*code)(void);
 | 
|---|
| 54 | };
 | 
|---|
| 55 | 
 | 
|---|
| 56 | struct beta {
 | 
|---|
| 57 |         beta_vtable const * virtual_table;
 | 
|---|
| 58 | };
 | 
|---|
| 59 | 
 | 
|---|
| 60 | char ret_b(void) {
 | 
|---|
| 61 |         return 'b';
 | 
|---|
| 62 | }
 | 
|---|
| 63 | 
 | 
|---|
| 64 | 
 | 
|---|
| 65 | 
 | 
|---|
| 66 | // Hand defined gamma virtual type:
 | 
|---|
| 67 | struct __cfatid_struct_gamma {
 | 
|---|
| 68 |         __cfatid_struct_beta const * parent;
 | 
|---|
| 69 | };
 | 
|---|
| 70 | 
 | 
|---|
| 71 | __attribute__(( section(".gnu.linkonce.__cfatid_gamma") ))
 | 
|---|
| 72 | struct __cfatid_struct_gamma __cfatid_gamma = {
 | 
|---|
| 73 |         &__cfatid_beta,
 | 
|---|
| 74 | };
 | 
|---|
| 75 | 
 | 
|---|
| 76 | struct gamma_vtable {
 | 
|---|
| 77 |         struct __cfatid_struct_gamma const * const __cfavir_typeid;
 | 
|---|
| 78 |         char (*code)(void);
 | 
|---|
| 79 | };
 | 
|---|
| 80 | 
 | 
|---|
| 81 | struct gamma {
 | 
|---|
| 82 |         gamma_vtable const * virtual_table;
 | 
|---|
| 83 | };
 | 
|---|
| 84 | 
 | 
|---|
| 85 | char ret_g(void) {
 | 
|---|
| 86 |         return 'g';
 | 
|---|
| 87 | }
 | 
|---|
| 88 | 
 | 
|---|
| 89 | 
 | 
|---|
| 90 | extern "C" {
 | 
|---|
| 91 |         alpha_vtable _alpha_vtable_instance = { &__cfatid_alpha, ret_a };
 | 
|---|
| 92 |         beta_vtable _beta_vtable_instance = { &__cfatid_beta, ret_b };
 | 
|---|
| 93 |         gamma_vtable _gamma_vtable_instance = { &__cfatid_gamma, ret_g };
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | int main (int argc, char * argv[]) {
 | 
|---|
| 97 | 
 | 
|---|
| 98 |         gamma * tri = malloc(); tri->virtual_table = &_gamma_vtable_instance;
 | 
|---|
| 99 |         beta * mid = (virtual beta *)tri;
 | 
|---|
| 100 |         assert( 'g' == mid->virtual_table->code() );
 | 
|---|
| 101 | 
 | 
|---|
| 102 |         alpha * top = malloc(); top->virtual_table = &_alpha_vtable_instance;
 | 
|---|
| 103 |         mid = (virtual beta *)top;
 | 
|---|
| 104 |         assert( ! mid );
 | 
|---|
| 105 | 
 | 
|---|
| 106 |         free(tri);
 | 
|---|
| 107 |         free(top);
 | 
|---|
| 108 |         printf( "done\n" );                             // non-empty .expect file
 | 
|---|
| 109 | }
 | 
|---|