source: tests/exceptions/virtual-cast.cfa @ db6cdc0

ADTast-experimental
Last change on this file since db6cdc0 was 8f910430, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Updated the virtual module to prefix the names with the new convention and change the parent_vtable into type_info.

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