[a5f0529] | 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 |
|
---|
[73abe95] | 11 | #include <stdlib.hfa>
|
---|
[a5f0529] | 12 | #include <assert.h>
|
---|
| 13 |
|
---|
[ecfd758] | 14 |
|
---|
| 15 |
|
---|
| 16 | // Hand defined alpha virtual type:
|
---|
| 17 | struct __cfatid_struct_alpha {
|
---|
[8f910430] | 18 | __cfavir_type_info parent;
|
---|
[ecfd758] | 19 | };
|
---|
| 20 |
|
---|
[8f910430] | 21 | __attribute__(( cfa_linkonce ))
|
---|
[ecfd758] | 22 | struct __cfatid_struct_alpha __cfatid_alpha = {
|
---|
[8f910430] | 23 | (__cfavir_type_info *)0,
|
---|
[ecfd758] | 24 | };
|
---|
| 25 |
|
---|
[a5f0529] | 26 | struct alpha_vtable {
|
---|
[ecfd758] | 27 | struct __cfatid_struct_alpha const * const __cfavir_typeid;
|
---|
[a5f0529] | 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 |
|
---|
[ecfd758] | 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 |
|
---|
[a5f0529] | 51 | struct beta_vtable {
|
---|
[ecfd758] | 52 | struct __cfatid_struct_beta const * const __cfavir_typeid;
|
---|
[a5f0529] | 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 |
|
---|
[ecfd758] | 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 |
|
---|
[a5f0529] | 76 | struct gamma_vtable {
|
---|
[ecfd758] | 77 | struct __cfatid_struct_gamma const * const __cfavir_typeid;
|
---|
[a5f0529] | 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" {
|
---|
[ecfd758] | 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 };
|
---|
[a5f0529] | 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);
|
---|
[66812dd] | 108 | printf( "done\n" ); // non-empty .expect file
|
---|
[a5f0529] | 109 | }
|
---|