source: tests/exceptions/virtual-cast.cfa @ 1d66a91

Last change on this file since 1d66a91 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
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
[3bf9d10]11#include <fstream.hfa>
[73abe95]12#include <stdlib.hfa>
[a5f0529]13#include <assert.h>
14
[ecfd758]15// Hand defined alpha virtual type:
16struct __cfatid_struct_alpha {
[8f910430]17        __cfavir_type_info parent;
[ecfd758]18};
19
[8f910430]20__attribute__(( cfa_linkonce ))
[ecfd758]21struct __cfatid_struct_alpha __cfatid_alpha = {
[8f910430]22        (__cfavir_type_info *)0,
[ecfd758]23};
24
[a5f0529]25struct alpha_vtable {
[ecfd758]26        struct __cfatid_struct_alpha const * const __cfavir_typeid;
[a5f0529]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
[ecfd758]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
[a5f0529]50struct beta_vtable {
[ecfd758]51        struct __cfatid_struct_beta const * const __cfavir_typeid;
[a5f0529]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
[ecfd758]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
[a5f0529]75struct gamma_vtable {
[ecfd758]76        struct __cfatid_struct_gamma const * const __cfavir_typeid;
[a5f0529]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" {
[ecfd758]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 };
[a5f0529]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);
[3bf9d10]107        sout | "done";                                                                          // non-empty .expect file
[a5f0529]108}
Note: See TracBrowser for help on using the repository browser.