[6da49249] | 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 | // |
---|
[dc8511c] | 7 | // forall.cfa -- |
---|
[6da49249] | 8 | // |
---|
| 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Wed May 9 08:48:15 2018 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[53692b3] | 12 | // Last Modified On : Sat Jun 5 10:06:08 2021 |
---|
| 13 | // Update Count : 36 |
---|
[6da49249] | 14 | // |
---|
[8a95629] | 15 | |
---|
| 16 | void g1() { |
---|
[fd54fef] | 17 | forall( T ) T f( T ) {}; |
---|
[6da49249] | 18 | void f( int ) {}; |
---|
| 19 | void h( void (*p)(void) ) {}; |
---|
[e5b96bf] | 20 | |
---|
[8a95629] | 21 | int x; |
---|
| 22 | void (*y)(void); |
---|
| 23 | char z; |
---|
| 24 | float w; |
---|
[e5b96bf] | 25 | |
---|
[8a95629] | 26 | f( x ); |
---|
| 27 | f( y ); |
---|
| 28 | f( z ); |
---|
| 29 | f( w ); |
---|
| 30 | h( f( y ) ); |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | void g2() { |
---|
[fd54fef] | 34 | forall( T ) void f( T, T ) {} |
---|
| 35 | forall( T, U ) void f( T, U ) {} |
---|
[e5b96bf] | 36 | |
---|
[8a95629] | 37 | int x; |
---|
| 38 | float y; |
---|
| 39 | int *z; |
---|
| 40 | float *w; |
---|
[e5b96bf] | 41 | |
---|
[8a95629] | 42 | f( x, y ); |
---|
| 43 | f( z, w ); |
---|
| 44 | f( x, z ); |
---|
| 45 | } |
---|
| 46 | |
---|
[fd54fef] | 47 | typedef forall ( T ) int (* f)( int ); |
---|
[51b7345] | 48 | |
---|
[fd54fef] | 49 | forall( T ) |
---|
[a65d92e] | 50 | void swap( T left, T right ) { |
---|
[51b7345] | 51 | T temp = left; |
---|
| 52 | left = right; |
---|
| 53 | right = temp; |
---|
[a65d92e] | 54 | } |
---|
[51b7345] | 55 | |
---|
[fd54fef] | 56 | trait sumable( T ) { |
---|
[6da49249] | 57 | void ?{}( T &, zero_t ); // 0 literal constructor |
---|
| 58 | T ?+?( T, T ); // assortment of additions |
---|
| 59 | T ?+=?( T &, T ); |
---|
| 60 | T ++?( T & ); |
---|
| 61 | T ?++( T & ); |
---|
| 62 | }; // sumable |
---|
| 63 | |
---|
[fd54fef] | 64 | forall( T | sumable( T ) ) // use trait |
---|
[6da49249] | 65 | T sum( size_t size, T a[] ) { |
---|
| 66 | T total = 0; // initialize by 0 constructor |
---|
| 67 | for ( size_t i = 0; i < size; i += 1 ) |
---|
| 68 | total = total + a[i]; // select appropriate + |
---|
[51b7345] | 69 | return total; |
---|
[6da49249] | 70 | } // sum |
---|
[51b7345] | 71 | |
---|
[fd54fef] | 72 | forall( T | { T ?+?( T, T ); T ?++( T & ); [T] ?+=?( T &,T ); } ) |
---|
[a65d92e] | 73 | T twice( T t ) { |
---|
[51b7345] | 74 | return t + t; |
---|
[a65d92e] | 75 | } |
---|
[51b7345] | 76 | |
---|
[fd54fef] | 77 | forall( T | { int ?<?(T, T); } ) |
---|
[0b2961f] | 78 | T min( T t1, T t2 ) { |
---|
| 79 | return t1 < t2 ? t1 : t2; |
---|
| 80 | } |
---|
| 81 | |
---|
[6da49249] | 82 | int fred() { |
---|
[0b2961f] | 83 | int x = 1, y = 2, a[10]; |
---|
| 84 | float f; |
---|
[51b7345] | 85 | |
---|
[0b2961f] | 86 | swap( x, y ); |
---|
| 87 | twice( x ); |
---|
| 88 | f = min( 4.0, 3.0 ); |
---|
| 89 | sum( 10, a ); |
---|
[51b7345] | 90 | } |
---|
[a65d92e] | 91 | |
---|
[6da49249] | 92 | // Multiple forall |
---|
[fd54fef] | 93 | forall( T ) forall( S ) struct { int i; }; |
---|
| 94 | forall( T ) struct { int i; } forall( S ); |
---|
| 95 | struct { int i; } forall( T ) forall( S ); |
---|
| 96 | forall( W ) struct { int i; } forall( T ) forall( S ); |
---|
[6da49249] | 97 | |
---|
| 98 | // Distribution |
---|
| 99 | struct P { int i; }; |
---|
[fd54fef] | 100 | forall( T ) struct Q { T i; }; |
---|
| 101 | forall( T ) struct { int i; }; |
---|
[6da49249] | 102 | struct KK { int i; }; |
---|
| 103 | inline static { |
---|
| 104 | void RT1() {} |
---|
| 105 | } |
---|
[fd54fef] | 106 | forall( T ) { |
---|
[6da49249] | 107 | T RT2( T ) { |
---|
| 108 | typedef int TD1; |
---|
| 109 | struct S1 { T t; }; |
---|
| 110 | } |
---|
[fd54fef] | 111 | forall( X ) { |
---|
[6da49249] | 112 | typedef int TD2; |
---|
| 113 | struct S2 {}; |
---|
| 114 | X RT2( T, X ) { |
---|
| 115 | int TD2; |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | extern "C" { |
---|
[fd54fef] | 119 | forall( W ) { |
---|
[6da49249] | 120 | W RT3( W ) {} |
---|
| 121 | struct S3 {}; |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | void RT4() { |
---|
[fd54fef] | 125 | forall( W ) struct S4 {}; |
---|
[6da49249] | 126 | typedef int TD3; |
---|
| 127 | } |
---|
| 128 | static { |
---|
| 129 | struct S5 {}; |
---|
| 130 | void RT5( T ) { |
---|
| 131 | struct S6 {}; |
---|
| 132 | int TD2; |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | struct S7 {}; |
---|
| 136 | typedef int TD4; |
---|
| 137 | } |
---|
| 138 | TD2 t2; |
---|
| 139 | TD4 t4; |
---|
| 140 | struct Q( int ) t; |
---|
| 141 | struct S2( int, int ) t; |
---|
| 142 | struct S5( int ) t; |
---|
| 143 | struct S7( int ) t; |
---|
| 144 | |
---|
| 145 | int i = RT2( 3 ); |
---|
| 146 | double j = RT2( 3, 4.5 ); |
---|
| 147 | |
---|
| 148 | static inline { |
---|
[fd54fef] | 149 | forall( T ) { |
---|
[6da49249] | 150 | int RT6( T p ); |
---|
| 151 | } |
---|
[fd54fef] | 152 | forall( T, U ) { |
---|
[6da49249] | 153 | int RT7( T, U ); |
---|
| 154 | } |
---|
| 155 | } |
---|
[fd54fef] | 156 | static forall( T ) { |
---|
[6da49249] | 157 | int RT8( T ); |
---|
| 158 | } |
---|
[fd54fef] | 159 | forall( T ) inline static { |
---|
[66812dd] | 160 | int RT9( T ) { T t; return 3; } |
---|
[6da49249] | 161 | } |
---|
| 162 | |
---|
[fd54fef] | 163 | forall( T | { T ?+?( T, T ); } ) { |
---|
| 164 | forall( S | { T ?+?( T, S ); } ) { |
---|
| 165 | forall( W ) T bar( T t, S s ) { return t + s; } |
---|
| 166 | forall( W | { W ?+?( T, W ); } ) W baz( T t, S s, W w ) { return t + s + w; } |
---|
[6da49249] | 167 | struct W { T t; } (int,int) ww; |
---|
| 168 | struct P pp; |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
[fd54fef] | 172 | forall( T | { T ?+?( T, T ); } ) forall( S | { T ?+?( T, S ); } ) |
---|
[6da49249] | 173 | struct XW { T t; }; |
---|
| 174 | XW(int,int) xww; |
---|
| 175 | |
---|
[fd54fef] | 176 | forall( T ) struct S { T t; } (int) x, y, z; |
---|
| 177 | forall( T ) struct { T t; } (int) a, b, c; |
---|
[6da49249] | 178 | |
---|
[fd54fef] | 179 | forall( T ) static forall( S ) { |
---|
| 180 | forall( X ) struct U { |
---|
[6da49249] | 181 | T x; |
---|
| 182 | }; |
---|
| 183 | } |
---|
| 184 | |
---|
[fd54fef] | 185 | forall( T ) { |
---|
[6da49249] | 186 | extern "C" { |
---|
| 187 | struct SS { T t; }; |
---|
| 188 | T foo( T ) {} |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | SS(int) s; |
---|
| 193 | W(int,int) w; |
---|
| 194 | |
---|
| 195 | int jane() { |
---|
| 196 | // int j = bar( 3, 4 ); |
---|
| 197 | int k = baz( 3, 4, 5 ); |
---|
| 198 | int i = foo( 3 ); |
---|
| 199 | } |
---|
| 200 | |
---|
[68b52b0] | 201 | forall( T ) void check_otype() { |
---|
| 202 | T & tr = *0p; |
---|
| 203 | T * tp = 0p; |
---|
| 204 | |
---|
| 205 | &tr += 1; |
---|
| 206 | tp += 1; |
---|
| 207 | T & tx = tp[1]; |
---|
| 208 | |
---|
| 209 | T t; |
---|
| 210 | T t2 = t; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | forall( T * ) void check_dstype() { |
---|
| 214 | T & tr = *0p; |
---|
| 215 | T * tp = 0p; |
---|
| 216 | |
---|
| 217 | &tr += 1; |
---|
| 218 | tp += 1; |
---|
| 219 | T & tx = tp[1]; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | forall( T & ) void check_dtype() { |
---|
| 223 | T & tr = *0p; |
---|
| 224 | T * tp = 0p; |
---|
| 225 | } |
---|
| 226 | |
---|
[6da49249] | 227 | //otype T1 | { void xxx( T1 ); }; |
---|
| 228 | |
---|
| 229 | // otype T1 | { void ?{}( T1 &, zero_t ); T1 ?+?(T1, T1); T1 ?++(T1); [T1] ?+=?(T1,T1); }, |
---|
| 230 | // T2(otype P1, otype P2 ), |
---|
| 231 | // T3 | sumable(T3); |
---|
| 232 | |
---|
| 233 | //otype T2(otype P1, otype P2) | sumable( T2( P1,P2 ) ) = struct { P1 i; P2 j; }; |
---|
| 234 | |
---|
| 235 | // T2(int, int) w1; |
---|
| 236 | // typedef T2(int, int) w2; |
---|
| 237 | // w2 g2; |
---|
| 238 | // otype w3 = T2(int, int); |
---|
| 239 | // w3 g3; |
---|
| 240 | |
---|
[66812dd] | 241 | int main( void ) { |
---|
[53692b3] | 242 | #pragma GCC warning "Compiled" // force non-empty .expect file, NO TABS!!! |
---|
[66812dd] | 243 | } |
---|
[6da49249] | 244 | |
---|
[a65d92e] | 245 | // Local Variables: // |
---|
| 246 | // tab-width: 4 // |
---|
[dc8511c] | 247 | // compile-command: "cfa forall.cfa" // |
---|
[a65d92e] | 248 | // End: // |
---|