source: tests/exceptions/virtual-poly.cfa @ eba9d27

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since eba9d27 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.2 KB
Line 
1// Test virtual casts with polymorphic types.
2
3/* IMPORTANT: The virtual system has not been finalized. However the
4 * exception system does depend on the work-in-progress version currently
5 * supported. That is also why the tests under the exception directory.
6 */
7
8#include <assert.h>
9
10
11struct __cfatid_struct_mono_base {
12    __cfavir_type_info const * parent;
13};
14
15__attribute__(( cfa_linkonce ))
16struct __cfatid_struct_mono_base __cfatid_mono_base = {
17    (__cfavir_type_info *)0,
18};
19
20struct mono_base_vtable {
21        __cfatid_struct_mono_base const * const __cfavir_typeid;
22};
23
24struct mono_base {
25        mono_base_vtable const * virtual_table;
26};
27
28forall(T)
29struct __cfatid_struct_mono_child {
30    __cfatid_struct_mono_base const * parent;
31};
32
33forall(T)
34struct mono_child_vtable {
35        __cfatid_struct_mono_child(T) const * const __cfavir_typeid;
36};
37
38forall(T)
39struct mono_child {
40        mono_child_vtable(T) const * virtual_table;
41};
42
43__cfatid_struct_mono_child(int) __cfatid_mono_child @= {
44        &__cfatid_mono_base,
45};
46
47mono_child_vtable(int) _mono_child_vtable_instance @= {
48        &__cfatid_mono_child,
49};
50
51void mono_poly_test(void) {
52        mono_child(int) child = { &_mono_child_vtable_instance };
53        mono_base * base = (virtual mono_base *)&child;
54        assert(base);
55}
56
57
58forall(U)
59struct __cfatid_struct_poly_base {
60    __cfavir_type_info const * parent;
61};
62
63forall(U)
64struct poly_base_vtable {
65        __cfatid_struct_poly_base(U) const * const __cfavir_typeid;
66};
67
68forall(U)
69struct poly_base {
70        poly_base_vtable(U) const * virtual_table;
71};
72
73forall(V)
74struct __cfatid_struct_poly_child {
75    __cfatid_struct_poly_base(V) const * parent;
76};
77
78forall(V)
79struct poly_child_vtable {
80        __cfatid_struct_poly_child(V) const * const __cfavir_typeid;
81};
82
83forall(V)
84struct poly_child {
85        poly_child_vtable(V) const * virtual_table;
86};
87
88__cfatid_struct_poly_base(int) __cfatid_poly_base @= {
89        (__cfavir_type_info *)0,
90};
91__cfatid_struct_poly_child(int) __cfatid_poly_child = {
92    &__cfatid_poly_base,
93};
94poly_child_vtable(int) _poly_child_vtable_instance @= {
95        &__cfatid_poly_child,
96};
97
98void poly_poly_test() {
99        poly_child(int) child = { &_poly_child_vtable_instance };
100        poly_base(int) * base = (virtual poly_base(int) *)&child;
101        assert(base);
102}
103
104int main(void) {
105        mono_poly_test();
106        poly_poly_test();
107        printf( "done\n" );
108}
Note: See TracBrowser for help on using the repository browser.