source: tests/exceptions/virtual-cast.cfa @ 01f78e0

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationpthread-emulationqualifiedEnum
Last change on this file since 01f78e0 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
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 {
[8f910430]18        __cfavir_type_info parent;
[ecfd758]19};
20
[8f910430]21__attribute__(( cfa_linkonce ))
[ecfd758]22struct __cfatid_struct_alpha __cfatid_alpha = {
[8f910430]23        (__cfavir_type_info *)0,
[ecfd758]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.