source: tests/exceptions/virtual-poly.cfa @ 65ef0cd

Last change on this file since 65ef0cd was 3bf9d10, checked in by Peter A. Buhr <pabuhr@…>, 17 months ago

change printf to sout

  • 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 <fstream.hfa>
9#include <assert.h>
10
11
12struct __cfatid_struct_mono_base {
13    __cfavir_type_info const * parent;
14};
15
16__attribute__(( cfa_linkonce ))
17struct __cfatid_struct_mono_base __cfatid_mono_base = {
18    (__cfavir_type_info *)0,
19};
20
21struct mono_base_vtable {
22        __cfatid_struct_mono_base const * const __cfavir_typeid;
23};
24
25struct mono_base {
26        mono_base_vtable const * virtual_table;
27};
28
29forall(T)
30struct __cfatid_struct_mono_child {
31    __cfatid_struct_mono_base const * parent;
32};
33
34forall(T)
35struct mono_child_vtable {
36        __cfatid_struct_mono_child(T) const * const __cfavir_typeid;
37};
38
39forall(T)
40struct mono_child {
41        mono_child_vtable(T) const * virtual_table;
42};
43
44__cfatid_struct_mono_child(int) __cfatid_mono_child @= {
45        &__cfatid_mono_base,
46};
47
48mono_child_vtable(int) _mono_child_vtable_instance @= {
49        &__cfatid_mono_child,
50};
51
52void mono_poly_test(void) {
53        mono_child(int) child = { &_mono_child_vtable_instance };
54        mono_base * base = (virtual mono_base *)&child;
55        assert(base);
56}
57
58
59forall(U)
60struct __cfatid_struct_poly_base {
61    __cfavir_type_info const * parent;
62};
63
64forall(U)
65struct poly_base_vtable {
66        __cfatid_struct_poly_base(U) const * const __cfavir_typeid;
67};
68
69forall(U)
70struct poly_base {
71        poly_base_vtable(U) const * virtual_table;
72};
73
74forall(V)
75struct __cfatid_struct_poly_child {
76    __cfatid_struct_poly_base(V) const * parent;
77};
78
79forall(V)
80struct poly_child_vtable {
81        __cfatid_struct_poly_child(V) const * const __cfavir_typeid;
82};
83
84forall(V)
85struct poly_child {
86        poly_child_vtable(V) const * virtual_table;
87};
88
89__cfatid_struct_poly_base(int) __cfatid_poly_base @= {
90        (__cfavir_type_info *)0,
91};
92__cfatid_struct_poly_child(int) __cfatid_poly_child = {
93    &__cfatid_poly_base,
94};
95poly_child_vtable(int) _poly_child_vtable_instance @= {
96        &__cfatid_poly_child,
97};
98
99void poly_poly_test() {
100        poly_child(int) child = { &_poly_child_vtable_instance };
101        poly_base(int) * base = (virtual poly_base(int) *)&child;
102        assert(base);
103}
104
105int main(void) {
106        mono_poly_test();
107        poly_poly_test();
108        sout | "done";
109}
Note: See TracBrowser for help on using the repository browser.