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