source: tests/exceptions/virtual-cast.cfa @ 091011a

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 091011a was ecfd758, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Major exception update, seperating type-ids from virtual tables. The major interface changes are done. There is a regression of ?Cancelled(T) to Some?Cancelled. There is some bits of code for the new verion of the ?Cancelled(T) interface already there. Not connected yet but I just reached the limit of what I wanted to do in one commit and then spent over a day cleaning up, so it will replace Some?Cancelled in a future commit.

  • Property mode set to 100644
File size: 2.3 KB
RevLine 
[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:
17struct __cfatid_struct_alpha {
18        __cfa__parent_vtable const * parent;
19};
20
21__attribute__(( section(".gnu.linkonce.__cfatid_alpha") ))
22struct __cfatid_struct_alpha __cfatid_alpha = {
23        (__cfa__parent_vtable *)0,
24};
25
[a5f0529]26struct alpha_vtable {
[ecfd758]27        struct __cfatid_struct_alpha const * const __cfavir_typeid;
[a5f0529]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
[ecfd758]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
[a5f0529]51struct beta_vtable {
[ecfd758]52        struct __cfatid_struct_beta const * const __cfavir_typeid;
[a5f0529]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
[ecfd758]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
[a5f0529]76struct gamma_vtable {
[ecfd758]77        struct __cfatid_struct_gamma const * const __cfavir_typeid;
[a5f0529]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" {
[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
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);
[66812dd]108        printf( "done\n" );                             // non-empty .expect file
[a5f0529]109}
Note: See TracBrowser for help on using the repository browser.