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