| 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 | // forall.cfa -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Peter A. Buhr | 
|---|
| 10 | // Created On       : Wed May  9 08:48:15 2018 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Thu Feb 23 20:29:59 2023 | 
|---|
| 13 | // Update Count     : 91 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include <fstream.hfa> | 
|---|
| 17 |  | 
|---|
| 18 | void g1() { | 
|---|
| 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(); }; | 
|---|
| 23 |  | 
|---|
| 24 | int x = 1; | 
|---|
| 25 | void (*y)(void) = g; | 
|---|
| 26 | char z = 'a'; | 
|---|
| 27 | float w = 3.5; | 
|---|
| 28 |  | 
|---|
| 29 | f( x ); | 
|---|
| 30 | f( y ); | 
|---|
| 31 | f( z ); | 
|---|
| 32 | f( w ); | 
|---|
| 33 | h( y ); | 
|---|
| 34 | f( y ); | 
|---|
| 35 | h( f( y ) ); | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | void g2() { | 
|---|
| 39 | forall( T ) void f( T, T ) { sout | "fT"; } | 
|---|
| 40 | forall( T, U ) void f( T, U ) { sout | "fTU"; } | 
|---|
| 41 |  | 
|---|
| 42 | int x; | 
|---|
| 43 | float y; | 
|---|
| 44 | int * z; | 
|---|
| 45 | float * w; | 
|---|
| 46 |  | 
|---|
| 47 | f( x, x ); | 
|---|
| 48 | f( y, y ); | 
|---|
| 49 | f( w, w ); | 
|---|
| 50 | f( x, y ); | 
|---|
| 51 | f( z, w ); | 
|---|
| 52 | f( x, z ); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 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 ); | 
|---|
| 58 |  | 
|---|
| 59 | forall( T ) | 
|---|
| 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 ]; | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | forall( T ) trait sumable { | 
|---|
| 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 |  | 
|---|
| 79 | forall( T | sumable( T ) )                                                              // use trait | 
|---|
| 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 + | 
|---|
| 84 | return total; | 
|---|
| 85 | } // sum | 
|---|
| 86 |  | 
|---|
| 87 | forall( T | { T ?+?( T, T ); T ?++( T & ); [T] ?+=?( T &, T ); } ) | 
|---|
| 88 | T twice( T t ) { | 
|---|
| 89 | return t + t; | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | forall( T | { int ?<?(T, T); } ) | 
|---|
| 93 | T min( T t1, T t2 ) { | 
|---|
| 94 | return t1 < t2 ? t1 : t2; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | void fred() { | 
|---|
| 98 | int x = 1, y = 2, a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; | 
|---|
| 99 | float f; | 
|---|
| 100 |  | 
|---|
| 101 | sout | x | y; | 
|---|
| 102 | swap( x, y ); | 
|---|
| 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 ); | 
|---|
| 107 | f = min( 4.0, 3.0 ); | 
|---|
| 108 | sout | f | min( 4.0, 3.0 ); | 
|---|
| 109 | sout | sum( 10, a ); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | // Multiple forall | 
|---|
| 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 ); | 
|---|
| 117 |  | 
|---|
| 118 | // Distribution | 
|---|
| 119 | struct P { int i; }; | 
|---|
| 120 | forall( T ) struct Q { T i; }; | 
|---|
| 121 | forall( T ) struct { int i; }; | 
|---|
| 122 | struct KK { int i; }; | 
|---|
| 123 | inline static { | 
|---|
| 124 | void RT1() {} | 
|---|
| 125 | } | 
|---|
| 126 | forall( T ) { | 
|---|
| 127 | T RT2( T ) { | 
|---|
| 128 | typedef int TD1; | 
|---|
| 129 | struct S1 { T t; }; | 
|---|
| 130 | } | 
|---|
| 131 | forall( X ) { | 
|---|
| 132 | typedef int TD2; | 
|---|
| 133 | struct S2 {}; | 
|---|
| 134 | X RT2( T, X ) { | 
|---|
| 135 | int TD2; | 
|---|
| 136 | } | 
|---|
| 137 | } | 
|---|
| 138 | extern "C" { | 
|---|
| 139 | forall( W ) { | 
|---|
| 140 | W RT3( W ) {} | 
|---|
| 141 | struct S3 {}; | 
|---|
| 142 | } | 
|---|
| 143 | } | 
|---|
| 144 | void RT4() { | 
|---|
| 145 | forall( W ) struct S4 {}; | 
|---|
| 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 { | 
|---|
| 169 | forall( T ) { | 
|---|
| 170 | int RT6( T p ); | 
|---|
| 171 | } | 
|---|
| 172 | forall( T, U ) { | 
|---|
| 173 | int RT7( T, U ); | 
|---|
| 174 | } | 
|---|
| 175 | } | 
|---|
| 176 | static forall( T ) { | 
|---|
| 177 | int RT8( T ); | 
|---|
| 178 | } | 
|---|
| 179 | forall( T ) inline static { | 
|---|
| 180 | int RT9( T ) { T t; return 3; } | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 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; } | 
|---|
| 187 | struct W { T t; } (int,int) ww; | 
|---|
| 188 | struct P pp; | 
|---|
| 189 | } | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | forall( T | { T ?+?( T, T ); } ) forall( S | { T ?+?( T, S ); } ) | 
|---|
| 193 | struct XW { T t; }; | 
|---|
| 194 | XW(int,int) xww; | 
|---|
| 195 |  | 
|---|
| 196 | forall( T ) struct S { T t; } (int) x, y, z; | 
|---|
| 197 | static forall( T ) struct { T t; } (int) a, b, c; | 
|---|
| 198 |  | 
|---|
| 199 | forall( T ) static forall( S ) { | 
|---|
| 200 | forall( X ) struct U { | 
|---|
| 201 | T x; | 
|---|
| 202 | }; | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | forall( T ) { | 
|---|
| 206 | //      extern "C" { | 
|---|
| 207 | struct SS { T t; }; | 
|---|
| 208 | T foo( T p ) { return p; } | 
|---|
| 209 | //      } | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | SS(int) s; | 
|---|
| 213 | W(int,int) w; | 
|---|
| 214 |  | 
|---|
| 215 | void jane() { | 
|---|
| 216 | //      int j = bar( 3, 4 ); | 
|---|
| 217 | int k = baz( 3, 4, 5 ); | 
|---|
| 218 | int i = foo( 3 ); | 
|---|
| 219 | sout | k | i; | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 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; | 
|---|
| 232 | sout | &tr | tp; | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 263 | int main( void ) { | 
|---|
| 264 | g1(); | 
|---|
| 265 | g2(); | 
|---|
| 266 | fred(); | 
|---|
| 267 | jane(); | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | // Local Variables: // | 
|---|
| 271 | // tab-width: 4 // | 
|---|
| 272 | // compile-command: "cfa forall.cfa" // | 
|---|
| 273 | // End: // | 
|---|