source: tests/exceptions/virtual-cast.cfa @ 006d4c4

Last change on this file since 006d4c4 was 3bf9d10, checked in by Peter A. Buhr <pabuhr@…>, 17 months ago

change printf to sout

  • Property mode set to 100644
File size: 2.3 KB
Line 
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:
16struct __cfatid_struct_alpha {
17        __cfavir_type_info parent;
18};
19
20__attribute__(( cfa_linkonce ))
21struct __cfatid_struct_alpha __cfatid_alpha = {
22        (__cfavir_type_info *)0,
23};
24
25struct alpha_vtable {
26        struct __cfatid_struct_alpha const * const __cfavir_typeid;
27        char (*code)(void);
28};
29
30struct alpha {
31        alpha_vtable const * virtual_table;
32};
33
34char ret_a(void) {
35        return 'a';
36}
37
38
39
40// Hand defined beta virtual type:
41struct __cfatid_struct_beta {
42        __cfatid_struct_alpha const * parent;
43};
44
45__attribute__(( section(".gnu.linkonce.__cfatid_beta") ))
46struct __cfatid_struct_beta __cfatid_beta = {
47        &__cfatid_alpha,
48};
49
50struct beta_vtable {
51        struct __cfatid_struct_beta const * const __cfavir_typeid;
52        char (*code)(void);
53};
54
55struct beta {
56        beta_vtable const * virtual_table;
57};
58
59char ret_b(void) {
60        return 'b';
61}
62
63
64
65// Hand defined gamma virtual type:
66struct __cfatid_struct_gamma {
67        __cfatid_struct_beta const * parent;
68};
69
70__attribute__(( section(".gnu.linkonce.__cfatid_gamma") ))
71struct __cfatid_struct_gamma __cfatid_gamma = {
72        &__cfatid_beta,
73};
74
75struct gamma_vtable {
76        struct __cfatid_struct_gamma const * const __cfavir_typeid;
77        char (*code)(void);
78};
79
80struct gamma {
81        gamma_vtable const * virtual_table;
82};
83
84char ret_g(void) {
85        return 'g';
86}
87
88
89extern "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
95int 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}
Note: See TracBrowser for help on using the repository browser.