source:
tests/withStatement.cfa
@
6a9d4b4
Last change on this file since 6a9d4b4 was d5b2ac8, checked in by , 6 years ago | |
---|---|
|
|
File size: 2.0 KB |
Rev | Line | |
---|---|---|
[866f560] | 1 | // |
2 | // Cforall Version 1.0.0 Copyright (C) 2017 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 | // tupleFunction.c -- | |
8 | // | |
9 | // Author : Rob Schluntz | |
10 | // Created On : Mon Dec 04 17:41:45 2017 | |
[d5b2ac8] | 11 | // Last Modified By : Peter A. Buhr |
12 | // Last Modified On : Mon Dec 24 19:08:18 2018 | |
13 | // Update Count : 5 | |
[866f560] | 14 | // |
15 | ||
[d5b2ac8] | 16 | #include <fstream.hfa> |
17 | ||
[866f560] | 18 | struct S { |
[d5b2ac8] | 19 | int i; |
20 | // dynamically allocated member ensures ctor/dtors are called correctly on temporaries | |
21 | int * ptr; | |
[866f560] | 22 | }; |
23 | ||
24 | // with clause on reference parameter | |
[d5b2ac8] | 25 | void ?{}( S & this, int n ) with( this ) { |
26 | i = n; | |
27 | ptr = (int *)malloc( sizeof(int) ); | |
[866f560] | 28 | } |
29 | ||
[d5b2ac8] | 30 | void ?{}( S & this ) { |
31 | this{ 0 }; | |
[866f560] | 32 | } |
33 | ||
[d5b2ac8] | 34 | void ?{}( S & this, S other ) { |
35 | this{ other.i }; | |
[866f560] | 36 | } |
37 | ||
[d5b2ac8] | 38 | S ?=?( S & this, S other ) with( this ) { |
39 | i = other.i; | |
40 | *ptr = *other.ptr; | |
41 | return this; | |
[866f560] | 42 | } |
43 | ||
[d5b2ac8] | 44 | void ^?{}( S & this ) with( this ) { |
45 | free( ptr ); | |
[866f560] | 46 | } |
47 | ||
48 | struct S2 { | |
[d5b2ac8] | 49 | S s; |
[866f560] | 50 | }; |
51 | ||
[d5b2ac8] | 52 | void ?{}( S2 & this, int n ) { |
53 | (this.s){ n }; | |
[866f560] | 54 | } |
55 | ||
[d5b2ac8] | 56 | forall( otype T ) |
[866f560] | 57 | struct Box { |
[d5b2ac8] | 58 | T x; |
[866f560] | 59 | }; |
60 | ||
[d5b2ac8] | 61 | forall( otype T ) |
62 | void ?{}( Box(T) & this ) with( this ) { // with clause in polymorphic function | |
63 | x{}; | |
[866f560] | 64 | } |
65 | ||
[d5b2ac8] | 66 | void print( int i ) { sout | i; } |
[866f560] | 67 | |
[d5b2ac8] | 68 | forall( otype T | { void print( T ); }) |
69 | void foo( T t ) { | |
70 | Box( T ) b = { t }; | |
71 | with( b ) { // with statement in polymorphic function | |
72 | print( x ); | |
73 | } | |
[866f560] | 74 | } |
75 | ||
76 | // ensure with-statement temporary generation works correctly | |
77 | S mk() { | |
[d5b2ac8] | 78 | sout | "called mk"; |
79 | return (S){ 444 }; | |
[866f560] | 80 | } |
81 | ||
82 | // ensure with-statement temporary generation with reference-returning functions works correctly | |
83 | S & ref() { | |
[d5b2ac8] | 84 | static S var = { 123456789 }; |
85 | return var; | |
[866f560] | 86 | } |
87 | ||
88 | int main() { | |
[d5b2ac8] | 89 | S2 s2 = { 12345 }; |
90 | with ( s2) { | |
91 | with( s ) { // with s2.s | |
92 | sout | i | s.i | s2.s.i; | |
93 | foo( i ); // s.i | |
94 | with( mk()) { | |
95 | sout | i | i | i; | |
96 | with( ref()) { | |
97 | sout | i | i | i; | |
98 | } // with ref() | |
99 | with( ref()) { | |
100 | sout | i | i | i; | |
101 | } // with ref() | |
102 | } // with mk() | |
103 | } // with s | |
104 | } // with s2 | |
[866f560] | 105 | } |
106 | ||
107 | // Local Variables: // | |
108 | // tab-width: 4 // | |
109 | // End: // |
Note: See TracBrowser
for help on using the repository browser.