source: src/tests/virtualCast.c @ 630bcb5

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 630bcb5 was a5f0529, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Virtual casts have been added. They still require a lot of hand coded support to work but for simple cases it should be enough.

  • Property mode set to 100644
File size: 1.5 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>
12#include <assert.h>
13
14struct alpha_vtable {
15        alpha_vtable const * const parent;
16        char (*code)(void);
17};
18
19struct alpha {
20        alpha_vtable const * virtual_table;
21};
22
23char ret_a(void) {
24        return 'a';
25}
26
27
28
29struct beta_vtable {
30        alpha_vtable const * const parent;
31        char (*code)(void);
32};
33
34struct beta {
35        beta_vtable const * virtual_table;
36};
37
38char ret_b(void) {
39        return 'b';
40}
41
42
43
44struct gamma_vtable {
45        beta_vtable const * const parent;
46        char (*code)(void);
47};
48
49struct gamma {
50        gamma_vtable const * virtual_table;
51};
52
53char ret_g(void) {
54        return 'g';
55}
56
57
58extern "C" {
59        alpha_vtable _alpha_vtable_instance = { 0, ret_a };
60        beta_vtable _beta_vtable_instance = { &_alpha_vtable_instance, ret_b };
61        gamma_vtable _gamma_vtable_instance = { &_beta_vtable_instance, ret_g };
62}
63
64int main (int argc, char * argv[]) {
65
66        gamma * tri = malloc(); tri->virtual_table = &_gamma_vtable_instance;
67        beta * mid = (virtual beta *)tri;
68        assert( 'g' == mid->virtual_table->code() );
69
70        alpha * top = malloc(); top->virtual_table = &_alpha_vtable_instance;
71        mid = (virtual beta *)top;
72        assert( ! mid );
73
74        free(tri);
75        free(top);
76}
Note: See TracBrowser for help on using the repository browser.