source: tests/forall.cfa @ 17fa94f

Last change on this file since 17fa94f was a22e8b4, checked in by Andrew Beach <ajbeach@…>, 2 months ago

Removed the warnings from more tests. I think '(void)var;' might be working better than 'attribute((unused)) var;'.

  • Property mode set to 100644
File size: 5.1 KB
Line 
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
18void 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
38void g2() {
39        forall( T ) void f( T, T ) { sout | "fT"; }
40        forall( T, U ) void f( T, U ) { sout | "fTU"; }
41
42        int x = 0;
43        float y = 0;
44        int * z = 0;
45        float * w = 0;
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
59forall( T )
60void swap( T & left, T & right ) {                                              // by reference
61    T temp = left;
62    left = right;
63    right = temp;
64}
65
66forall( T )
67[ T, T ] swap( T i, T j ) {                                                             // by value
68    return [ j, i ];
69}
70
71forall( 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
79forall( T | sumable( T ) )                                                              // use trait
80T 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
87forall( T | { T ?+?( T, T ); T ?++( T & ); [T] ?+=?( T &, T ); } )
88T twice( T t ) {
89        return t + t;
90}
91
92forall( T | { int ?<?(T, T); } )
93T min( T t1, T t2 ) {
94        return t1 < t2 ? t1 : t2;
95}
96
97void 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
113forall( T ) forall( S ) struct { int i; };
114forall( T ) struct { int i; } forall( S );
115struct { int i; } forall( T ) forall( S );
116forall( W ) struct { int i; } forall( T ) forall( S );
117
118// Distribution
119struct P { int i; };
120forall( T ) struct Q { T i; };
121forall( T ) struct { int i; };
122struct KK { int i; };
123inline static {
124        void RT1() {}
125}
126forall( 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 x ) {
135                        int TD2; (void)TD2;
136                        return x;
137                }
138        }
139        extern "C" {
140                forall( W ) {
141                        W RT3( W ) {}
142                        struct S3 {};
143                }
144        }
145        void RT4() {
146                forall( W ) struct S4 {};
147                typedef int TD3;
148        }
149        struct S5 {};
150        inline {
151                void RT5( T ) {
152                        struct S6 {};
153                        int TD2; (void)TD2;
154                }
155        }
156        struct S7 {};
157        typedef int TD4;
158}
159TD2 t2;
160TD4 t4;
161struct Q( int ) t;
162struct S2( int, int ) t;
163struct S5( int ) t;
164struct S7( int ) t;
165
166int i = RT2( 3 );
167double j = RT2( 3, 4.5 );
168
169static inline {
170        forall( T ) {
171                int RT6( T ) { return 0; }
172        }
173        forall( T, U ) {
174                int RT7( T, U ) { return 0; }
175        }
176}
177inline forall( T ) {
178        int RT8( T ) { return 0; }
179}
180forall( T ) inline {
181        int RT9( T ) { T t; return 3; }
182}
183
184forall( T | { T ?+?( T, T ); } ) {
185        forall( S | { T ?+?( T, S ); } ) {
186                forall( W ) T bar( T t, S s ) { return t + s; }
187                forall( W | { W ?+?( T, W ); } ) W baz( T t, S s, W w ) { return t + s + w; }
188                struct W { T t; } (int,int) ww;
189                struct P pp;
190        }
191}
192
193forall( T | { T ?+?( T, T ); } ) forall( S | { T ?+?( T, S ); } )
194struct XW { T t; };
195XW(int,int) xww;
196
197forall( T ) struct S { T t; } (int) x, y, z;
198static forall( T ) struct { T t; } (int) a, b, c;
199
200forall( T ) static forall( S ) {
201    forall( X ) struct U {
202                T x;
203    };
204}
205
206forall( T ) {
207//      extern "C" {
208                struct SS { T t; };
209                T foo( T p ) { return p; }
210//      }
211}
212
213SS(int) s;
214W(int,int) w;
215
216void jane() {
217//      int j = bar( 3, 4 );
218        int k = baz( 3, 4, 5 );
219        int i = foo( 3 );
220        sout | k | i;
221}
222
223forall( T ) void check_otype() {
224        T & tr = *0p;
225        T * tp = 0p;
226
227        &tr += 1;
228        tp += 1;
229        T & tx = tp[1]; (void)tx;
230
231        T t;
232        T t2 = t;
233        sout | &tr | tp;
234}
235
236forall( T * ) void check_dstype() {
237        T & tr = *0p;
238        T * tp = 0p;
239
240        &tr += 1;
241        tp += 1;
242        T & tx = tp[1]; (void)tx;
243}
244
245forall( T & ) void check_dtype() {
246        T & tr = *0p; (void)tr;
247        T * tp = 0p; (void)tp;
248}
249
250//otype T1 | { void xxx( T1 ); };
251
252// otype T1 | { void ?{}( T1 &, zero_t ); T1 ?+?(T1, T1); T1 ?++(T1); [T1] ?+=?(T1,T1); },
253//      T2(otype P1, otype P2 ),
254//      T3 | sumable(T3);
255
256//otype T2(otype P1, otype P2) | sumable( T2( P1,P2 ) ) = struct { P1 i; P2 j; };
257
258// T2(int, int) w1;
259// typedef T2(int, int) w2;
260// w2 g2;
261// otype w3 = T2(int, int);
262// w3 g3;
263
264int main( void ) {
265        g1();
266        g2();
267        fred();
268        jane();
269}
270
271// Local Variables: //
272// tab-width: 4 //
273// compile-command: "cfa forall.cfa" //
274// End: //
Note: See TracBrowser for help on using the repository browser.