| 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 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Thu May 18 18:05:12 2017 | 
|---|
| 13 | // Update Count     : 4 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | // packed is needed so that structs are not passed with the same alignment as function arguments | 
|---|
| 17 | __attribute__((packed)) struct A { | 
|---|
| 18 | double x; | 
|---|
| 19 | char y; | 
|---|
| 20 | double z; | 
|---|
| 21 | }; | 
|---|
| 22 |  | 
|---|
| 23 | __attribute__((packed)) struct B { | 
|---|
| 24 | long long x; | 
|---|
| 25 | char y; | 
|---|
| 26 | long long z; | 
|---|
| 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; } | 
|---|
| 31 | forall(T, U | { T f(T, U, U); }) | 
|---|
| 32 | void g(T x, U y) { f(x, y, y); } | 
|---|
| 33 |  | 
|---|
| 34 | // add two triples | 
|---|
| 35 | forall(T | { T ?+?(T, T); }) | 
|---|
| 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() { | 
|---|
| 41 | int x1 = 123, x3 = 456; | 
|---|
| 42 | double x2 = 999.123; | 
|---|
| 43 |  | 
|---|
| 44 | int i1 = 111, i3 = 222; | 
|---|
| 45 | double i2 = 333; | 
|---|
| 46 |  | 
|---|
| 47 | int d1 = 555, d3 = 444; | 
|---|
| 48 | double d2 = 666; | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 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); | 
|---|
| 55 |  | 
|---|
| 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); | 
|---|
| 61 |  | 
|---|
| 62 | // ensure non-matching assertions are specialized correctly | 
|---|
| 63 | g((A){ 1.21, 'x', 10.21}, (B){ 1111LL, 'v', 54385938LL }); | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | forall(T) | 
|---|
| 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: // | 
|---|