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 <fstream.hfa> |
---|
12 | #include <stdlib.hfa> |
---|
13 | #include <assert.h> |
---|
14 | |
---|
15 | // Hand defined alpha virtual type: |
---|
16 | struct __cfatid_struct_alpha { |
---|
17 | __cfavir_type_info parent; |
---|
18 | }; |
---|
19 | |
---|
20 | __attribute__(( cfa_linkonce )) |
---|
21 | struct __cfatid_struct_alpha __cfatid_alpha = { |
---|
22 | (__cfavir_type_info *)0, |
---|
23 | }; |
---|
24 | |
---|
25 | struct alpha_vtable { |
---|
26 | struct __cfatid_struct_alpha const * const __cfavir_typeid; |
---|
27 | char (*code)(void); |
---|
28 | }; |
---|
29 | |
---|
30 | struct alpha { |
---|
31 | alpha_vtable const * virtual_table; |
---|
32 | }; |
---|
33 | |
---|
34 | char ret_a(void) { |
---|
35 | return 'a'; |
---|
36 | } |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | // Hand defined beta virtual type: |
---|
41 | struct __cfatid_struct_beta { |
---|
42 | __cfatid_struct_alpha const * parent; |
---|
43 | }; |
---|
44 | |
---|
45 | __attribute__(( section(".gnu.linkonce.__cfatid_beta") )) |
---|
46 | struct __cfatid_struct_beta __cfatid_beta = { |
---|
47 | &__cfatid_alpha, |
---|
48 | }; |
---|
49 | |
---|
50 | struct beta_vtable { |
---|
51 | struct __cfatid_struct_beta const * const __cfavir_typeid; |
---|
52 | char (*code)(void); |
---|
53 | }; |
---|
54 | |
---|
55 | struct beta { |
---|
56 | beta_vtable const * virtual_table; |
---|
57 | }; |
---|
58 | |
---|
59 | char ret_b(void) { |
---|
60 | return 'b'; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | // Hand defined gamma virtual type: |
---|
66 | struct __cfatid_struct_gamma { |
---|
67 | __cfatid_struct_beta const * parent; |
---|
68 | }; |
---|
69 | |
---|
70 | __attribute__(( section(".gnu.linkonce.__cfatid_gamma") )) |
---|
71 | struct __cfatid_struct_gamma __cfatid_gamma = { |
---|
72 | &__cfatid_beta, |
---|
73 | }; |
---|
74 | |
---|
75 | struct gamma_vtable { |
---|
76 | struct __cfatid_struct_gamma const * const __cfavir_typeid; |
---|
77 | char (*code)(void); |
---|
78 | }; |
---|
79 | |
---|
80 | struct gamma { |
---|
81 | gamma_vtable const * virtual_table; |
---|
82 | }; |
---|
83 | |
---|
84 | char ret_g(void) { |
---|
85 | return 'g'; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | extern "C" { |
---|
90 | alpha_vtable _alpha_vtable_instance = { &__cfatid_alpha, ret_a }; |
---|
91 | beta_vtable _beta_vtable_instance = { &__cfatid_beta, ret_b }; |
---|
92 | gamma_vtable _gamma_vtable_instance = { &__cfatid_gamma, ret_g }; |
---|
93 | } |
---|
94 | |
---|
95 | int main (int argc, char * argv[]) { |
---|
96 | |
---|
97 | gamma * tri = malloc(); tri->virtual_table = &_gamma_vtable_instance; |
---|
98 | beta * mid = (virtual beta *)tri; |
---|
99 | assert( 'g' == mid->virtual_table->code() ); |
---|
100 | |
---|
101 | alpha * top = malloc(); top->virtual_table = &_alpha_vtable_instance; |
---|
102 | mid = (virtual beta *)top; |
---|
103 | assert( ! mid ); |
---|
104 | |
---|
105 | free(tri); |
---|
106 | free(top); |
---|
107 | sout | "done"; // non-empty .expect file |
---|
108 | } |
---|