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 | // tuplePolymorphism.c -- |
---|
8 | // |
---|
9 | // Author : Rob Schluntz |
---|
10 | // Created On : Fri Dec 16 10:25:35 2016 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Fri Dec 21 14:42:48 2016 |
---|
13 | // Update Count : 2 |
---|
14 | // |
---|
15 | |
---|
16 | void func(void) { |
---|
17 | printf("called func(void)\n"); |
---|
18 | } |
---|
19 | forall(otype T, ttype Params | { void process(T); void func(Params); }) |
---|
20 | void func(T arg1, Params p) { |
---|
21 | process(arg1); |
---|
22 | func(p); |
---|
23 | } |
---|
24 | void process(int x) { |
---|
25 | printf("called process(int) %d\n", x); |
---|
26 | } |
---|
27 | void process(double x) { |
---|
28 | printf("called process(double) %g\n", x); |
---|
29 | } |
---|
30 | |
---|
31 | forall( dtype T, ttype Params | sized(T) | { void ?{}(T &, Params); } ) |
---|
32 | T * new(Params p); |
---|
33 | |
---|
34 | struct array { |
---|
35 | int * data; |
---|
36 | int size; |
---|
37 | }; |
---|
38 | |
---|
39 | // xxx - eventually this will be collapsed...x |
---|
40 | void ?{}(array & a) { |
---|
41 | a.size = 0; |
---|
42 | a.data = 0; |
---|
43 | printf("called ?{} with no a\n"); |
---|
44 | } |
---|
45 | |
---|
46 | void ?{}(array & a, int a0) { |
---|
47 | a.size = 1; |
---|
48 | a.data = (int*)malloc(sizeof(int)*a.size); |
---|
49 | a.data[0] = a0; |
---|
50 | printf("called ?{} with a: %d\n", a0); |
---|
51 | } |
---|
52 | |
---|
53 | void ?{}(array & a, int a0, int a1) { |
---|
54 | a.size = 2; |
---|
55 | a.data = (int*)malloc(sizeof(int)*a.size); |
---|
56 | a.data[0] = a0; |
---|
57 | a.data[1] = a1; |
---|
58 | printf("called ?{} with a: %d %d\n", a0, a1); |
---|
59 | } |
---|
60 | |
---|
61 | void ?{}(array & a, int a0, int a1, int a2) { |
---|
62 | a.size = 3; |
---|
63 | a.data = (int*)malloc(sizeof(int)*a.size); |
---|
64 | a.data[0] = a0; |
---|
65 | a.data[1] = a1; |
---|
66 | a.data[2] = a2; |
---|
67 | printf("called ?{} with a: %d %d %d\n", a0, a1, a2); |
---|
68 | } |
---|
69 | |
---|
70 | // test use of a tuple argument |
---|
71 | [void] ?{}(array & a, [int, int, int, int] args) { |
---|
72 | int a0, a1, a2, a3; |
---|
73 | [a0, a1, a2, a3] = args; |
---|
74 | a.size = 4; |
---|
75 | a.data = (int *)malloc(sizeof(int)*a.size); |
---|
76 | a.data[0] = a0; |
---|
77 | a.data[1] = a1; |
---|
78 | a.data[2] = a2; |
---|
79 | a.data[3] = a3; |
---|
80 | printf("called ?{} with a: %d %d %d %d\n", a0, a1, a2, a3); |
---|
81 | } |
---|
82 | |
---|
83 | void print(array * x) { |
---|
84 | printf("array = { "); |
---|
85 | for (int i = 0; i < x->size; ++i) { |
---|
86 | printf("%d, ", x->data[i]); |
---|
87 | } |
---|
88 | printf("}\n"); |
---|
89 | } |
---|
90 | |
---|
91 | forall(otype T) |
---|
92 | T * copy(T x) { |
---|
93 | // test calling new inside a polymorphic function |
---|
94 | return new(x); |
---|
95 | } |
---|
96 | |
---|
97 | forall(ttype T | { void foo(T); }) void bar(T x) {} |
---|
98 | void foo(int) {} |
---|
99 | |
---|
100 | int main() { |
---|
101 | array * x0 = new(); |
---|
102 | print(x0); |
---|
103 | |
---|
104 | array * x1 = new(999); |
---|
105 | print(x1); |
---|
106 | |
---|
107 | array * x2 = new(123, 456); |
---|
108 | print(x2); |
---|
109 | |
---|
110 | array * x3 = new(100, 200, 300); |
---|
111 | print(x3); |
---|
112 | |
---|
113 | array * x4 = new(10, 2, 3, 4); |
---|
114 | print(x4); |
---|
115 | |
---|
116 | int * ptr = copy(111111); |
---|
117 | printf("copy=%d\n", *ptr); |
---|
118 | |
---|
119 | printf("calling func\n"); |
---|
120 | func(3, 2.0, 111, 4.145); |
---|
121 | printf("finished func\n"); |
---|
122 | |
---|
123 | { |
---|
124 | // T = [const int] -- this ensures that void(*)(int) satisfies void(*)(const int) |
---|
125 | const int x; |
---|
126 | bar(x); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | // Local Variables: // |
---|
131 | // tab-width: 4 // |
---|
132 | // End: // |
---|
133 | |
---|