[f7e749f] | 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 : Tue Nov 16 10:38:00 2016 |
---|
[17e0dc9] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Thu May 18 18:05:12 2017 |
---|
| 13 | // Update Count : 4 |
---|
[f7e749f] | 14 | // |
---|
| 15 | |
---|
[ebb5ed9] | 16 | // packed is needed so that structs are not passed with the same alignment as function arguments |
---|
| 17 | __attribute__((packed)) struct A { |
---|
[17e0dc9] | 18 | double x; |
---|
| 19 | char y; |
---|
| 20 | double z; |
---|
[ebb5ed9] | 21 | }; |
---|
| 22 | |
---|
| 23 | __attribute__((packed)) struct B { |
---|
[17e0dc9] | 24 | long long x; |
---|
| 25 | char y; |
---|
| 26 | long long z; |
---|
[ebb5ed9] | 27 | }; |
---|
| 28 | |
---|
| 29 | // ensure that f is a viable candidate for g, even though its parameter structure does not exactly match |
---|
| 30 | [A] f([A, B] x, B y) { printf("%g %c %g %lld %c %lld %lld %c %lld\n", x.0.[x,y,z], x.1.[x,y,z], y.[x,y,z]); return x.0; } |
---|
[fd54fef] | 31 | forall(T, U | { T f(T, U, U); }) |
---|
[ebb5ed9] | 32 | void g(T x, U y) { f(x, y, y); } |
---|
| 33 | |
---|
| 34 | // add two triples |
---|
[fd54fef] | 35 | forall(T | { T ?+?(T, T); }) |
---|
[f7e749f] | 36 | [T, T, T] ?+?([T, T, T] x, [T, T, T] y) { |
---|
| 37 | return [x.0+y.0, x.1+y.1, x.2+y.2]; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | int main() { |
---|
[17e0dc9] | 41 | int x1 = 123, x3 = 456; |
---|
| 42 | double x2 = 999.123; |
---|
[f7e749f] | 43 | |
---|
[17e0dc9] | 44 | int i1 = 111, i3 = 222; |
---|
| 45 | double i2 = 333; |
---|
[f7e749f] | 46 | |
---|
[17e0dc9] | 47 | int d1 = 555, d3 = 444; |
---|
| 48 | double d2 = 666; |
---|
[f7e749f] | 49 | |
---|
| 50 | |
---|
[17e0dc9] | 51 | [i1, i2, i3] = ([x1, (int)x2, x3]) + ([9, 2, 3]); |
---|
| 52 | [d1, d2, d3] = ([x1, x2, x3]) + ([9, 2, 3]); |
---|
| 53 | printf("%d %g %d\n", i1, i2, i3); |
---|
| 54 | printf("%d %g %d\n", d1, d2, d3); |
---|
[f7e749f] | 55 | |
---|
[17e0dc9] | 56 | [double, double, double] zzz; |
---|
| 57 | zzz = [x1, x2, x3]; |
---|
| 58 | printf("%g %g %g\n", zzz); |
---|
| 59 | [x1, x2, x3] = zzz+zzz; |
---|
| 60 | printf("%d %g %d\n", x1, x2, x3); |
---|
[ebb5ed9] | 61 | |
---|
[17e0dc9] | 62 | // ensure non-matching assertions are specialized correctly |
---|
| 63 | g((A){ 1.21, 'x', 10.21}, (B){ 1111LL, 'v', 54385938LL }); |
---|
[f7e749f] | 64 | } |
---|
| 65 | |
---|
[fd54fef] | 66 | forall(T) |
---|
[f7e749f] | 67 | [T, T] foo([T, T] y) { |
---|
| 68 | [T, T] x; |
---|
| 69 | return x; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | // Local Variables: // |
---|
| 73 | // tab-width: 4 // |
---|
| 74 | // End: // |
---|