[da6dec23] | 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 | |
---|
[1e3d5b6] | 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 | |
---|
[2afec66] | 31 | forall( dtype T, ttype Params | sized(T) | { void ?{}(T &, Params); } ) |
---|
[627f585] | 32 | T * new(Params p); |
---|
[da6dec23] | 33 | |
---|
| 34 | struct array { |
---|
| 35 | int * data; |
---|
| 36 | int size; |
---|
| 37 | }; |
---|
| 38 | |
---|
| 39 | // xxx - eventually this will be collapsed...x |
---|
[2afec66] | 40 | void ?{}(array & a) { |
---|
| 41 | a.size = 0; |
---|
| 42 | a.data = 0; |
---|
[da6dec23] | 43 | printf("called ?{} with no a\n"); |
---|
| 44 | } |
---|
| 45 | |
---|
[2afec66] | 46 | void ?{}(array & a, int a0) { |
---|
| 47 | a.size = 1; |
---|
| 48 | a.data = (int*)malloc(sizeof(int)*a.size); |
---|
| 49 | a.data[0] = a0; |
---|
[da6dec23] | 50 | printf("called ?{} with a: %d\n", a0); |
---|
| 51 | } |
---|
| 52 | |
---|
[2afec66] | 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; |
---|
[da6dec23] | 58 | printf("called ?{} with a: %d %d\n", a0, a1); |
---|
| 59 | } |
---|
| 60 | |
---|
[2afec66] | 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; |
---|
[da6dec23] | 67 | printf("called ?{} with a: %d %d %d\n", a0, a1, a2); |
---|
| 68 | } |
---|
| 69 | |
---|
[64eae56] | 70 | // test use of a tuple argument |
---|
[2afec66] | 71 | [void] ?{}(array & a, [int, int, int, int] args) { |
---|
[64eae56] | 72 | int a0, a1, a2, a3; |
---|
| 73 | [a0, a1, a2, a3] = args; |
---|
[2afec66] | 74 | a.size = 4; |
---|
[cdbfab0] | 75 | a.data = (int *)malloc(sizeof(int)*a.size); |
---|
[2afec66] | 76 | a.data[0] = a0; |
---|
| 77 | a.data[1] = a1; |
---|
| 78 | a.data[2] = a2; |
---|
| 79 | a.data[3] = a3; |
---|
[da6dec23] | 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 | |
---|
[dd0c97b] | 91 | forall(otype T) |
---|
| 92 | T * copy(T x) { |
---|
| 93 | // test calling new inside a polymorphic function |
---|
| 94 | return new(x); |
---|
| 95 | } |
---|
| 96 | |
---|
[da6dec23] | 97 | int main() { |
---|
[0c286cf] | 98 | array * x0 = new(); |
---|
| 99 | print(x0); |
---|
[da6dec23] | 100 | |
---|
[0c286cf] | 101 | array * x1 = new(999); |
---|
| 102 | print(x1); |
---|
[da6dec23] | 103 | |
---|
| 104 | array * x2 = new(123, 456); |
---|
| 105 | print(x2); |
---|
| 106 | |
---|
| 107 | array * x3 = new(100, 200, 300); |
---|
| 108 | print(x3); |
---|
| 109 | |
---|
| 110 | array * x4 = new(10, 2, 3, 4); |
---|
| 111 | print(x4); |
---|
[dd0c97b] | 112 | |
---|
| 113 | int * ptr = copy(111111); |
---|
| 114 | printf("copy=%d\n", *ptr); |
---|
| 115 | |
---|
[1e3d5b6] | 116 | printf("calling func\n"); |
---|
| 117 | func(3, 2.0, 111, 4.145); |
---|
| 118 | printf("finished func\n"); |
---|
[da6dec23] | 119 | } |
---|
| 120 | |
---|
| 121 | // Local Variables: // |
---|
| 122 | // tab-width: 4 // |
---|
| 123 | // End: // |
---|
| 124 | |
---|