1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // polymorphism.c -- |
---|
8 | // |
---|
9 | // Author : Rob Schluntz |
---|
10 | // Created On : Tue Oct 17 12:19:48 2017 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Tue Dec 25 14:40:24 2018 |
---|
13 | // Update Count : 3 |
---|
14 | // |
---|
15 | |
---|
16 | #include <assert.h> |
---|
17 | #include <inttypes.h> |
---|
18 | #include <fstream.hfa> |
---|
19 | |
---|
20 | forall(otype T) |
---|
21 | T f(T x, T y) { |
---|
22 | x = y; |
---|
23 | return x; |
---|
24 | } |
---|
25 | |
---|
26 | forall(otype T) T ident(T x) { |
---|
27 | return x; |
---|
28 | } |
---|
29 | |
---|
30 | forall( otype T, otype U ) |
---|
31 | size_t struct_size( T i, U j ) { |
---|
32 | struct S { T i; U j; }; |
---|
33 | return sizeof(S); |
---|
34 | } |
---|
35 | |
---|
36 | forall( otype T, otype U ) |
---|
37 | size_t union_size( T i, U j ) { |
---|
38 | union B { T i; U j; }; |
---|
39 | return sizeof(B); |
---|
40 | } |
---|
41 | |
---|
42 | // perform some simple operations on aggregates of T and U |
---|
43 | forall( otype T | { void print(T); int ?==?(T, T); }, otype U | { void print(U); U ?=?(U&, zero_t); } ) |
---|
44 | U foo(T i, U j) { |
---|
45 | struct S { T i; U j; }; |
---|
46 | union B { T i; U j; }; |
---|
47 | |
---|
48 | S s; |
---|
49 | s.i = i; |
---|
50 | assertf(s.i == i, "struct operation fails in polymorphic context."); |
---|
51 | |
---|
52 | B b; |
---|
53 | b.j = 0; |
---|
54 | b.i = s.i; |
---|
55 | return b.j; |
---|
56 | } |
---|
57 | |
---|
58 | int main() { |
---|
59 | { |
---|
60 | // ensure that x is not changed by the invocation of a polymorphic function |
---|
61 | int x = 123; |
---|
62 | int y = 456; |
---|
63 | int z = f(x, y); |
---|
64 | sout | x | y | z; |
---|
65 | } |
---|
66 | { |
---|
67 | // explicitly specialize function |
---|
68 | int (*f)(int) = ident; |
---|
69 | ((int(*)(int))ident); |
---|
70 | sout | f(5) | ((int(*)(int))ident)(5); |
---|
71 | } |
---|
72 | { |
---|
73 | // test aggregates with polymorphic members |
---|
74 | typedef __attribute__((aligned(8))) uint32_t x_type; |
---|
75 | typedef __attribute__((aligned(8))) uint64_t y_type; |
---|
76 | |
---|
77 | x_type x = 3; |
---|
78 | y_type y = 3; |
---|
79 | |
---|
80 | struct S { |
---|
81 | x_type f1; |
---|
82 | y_type f2; |
---|
83 | }; |
---|
84 | union U { |
---|
85 | x_type f1; |
---|
86 | y_type f2; |
---|
87 | }; |
---|
88 | // ensure that the size of aggregates with polymorphic members |
---|
89 | // matches the size of the aggregates in a monomorphic context |
---|
90 | size_t ssz = struct_size(x, y); |
---|
91 | size_t usz = union_size(x, y); |
---|
92 | assertf( ssz == sizeof(S), "struct size differs in polymorphic context: %zd / %zd", ssz, sizeof(S)); |
---|
93 | assertf( usz == sizeof(U), "union size differs in polymorphic context: %zd / %zd", usz, sizeof(U)); |
---|
94 | |
---|
95 | y_type ?=?(y_type & this, zero_t) { |
---|
96 | this = (int)0; |
---|
97 | return this; |
---|
98 | } |
---|
99 | |
---|
100 | void print(x_type x) { |
---|
101 | sout | x; |
---|
102 | } |
---|
103 | |
---|
104 | void print(y_type y) { |
---|
105 | sout | y; |
---|
106 | } |
---|
107 | |
---|
108 | y_type ret = foo(x, y); |
---|
109 | |
---|
110 | // duplicate logic from inside of foo to ensure the same results |
---|
111 | U u; |
---|
112 | u.f2 = 0; |
---|
113 | u.f1 = x; |
---|
114 | assertf(ret == u.f2, "union operation fails in polymorphic context."); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | // Local Variables: // |
---|
119 | // tab-width: 4 // |
---|
120 | // End: // |
---|