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
|
---|
11 | // Last Modified By : Rob Schluntz
|
---|
12 | // Last Modified On : Mon Dec 04 17:45:07 2017
|
---|
13 | // Update Count : 2
|
---|
14 | //
|
---|
15 |
|
---|
16 | struct S {
|
---|
17 | int i;
|
---|
18 | // dynamically allocated member ensures ctor/dtors are called correctly on temporaries
|
---|
19 | int * ptr;
|
---|
20 | };
|
---|
21 |
|
---|
22 | // with clause on reference parameter
|
---|
23 | void ?{}(S & this, int n) with(this) {
|
---|
24 | i = n;
|
---|
25 | ptr = (int *)malloc(sizeof(int));
|
---|
26 | }
|
---|
27 |
|
---|
28 | void ?{}(S & this) {
|
---|
29 | this{ 0 };
|
---|
30 | }
|
---|
31 |
|
---|
32 | void ?{}(S & this, S other) {
|
---|
33 | this{ other.i };
|
---|
34 | }
|
---|
35 |
|
---|
36 | S ?=?(S & this, S other) with(this) {
|
---|
37 | i = other.i;
|
---|
38 | *ptr = *other.ptr;
|
---|
39 | return this;
|
---|
40 | }
|
---|
41 |
|
---|
42 | void ^?{}(S & this) with(this) {
|
---|
43 | free(ptr);
|
---|
44 | }
|
---|
45 |
|
---|
46 | struct S2 {
|
---|
47 | S s;
|
---|
48 | };
|
---|
49 |
|
---|
50 | void ?{}(S2 & this, int n) {
|
---|
51 | (this.s){ n };
|
---|
52 | }
|
---|
53 |
|
---|
54 | forall(otype T)
|
---|
55 | struct Box {
|
---|
56 | T x;
|
---|
57 | };
|
---|
58 |
|
---|
59 | forall(otype T)
|
---|
60 | void ?{}(Box(T) & this) with(this) { // with clause in polymorphic function
|
---|
61 | x{};
|
---|
62 | }
|
---|
63 |
|
---|
64 | void print(int i) { printf("%d", i); }
|
---|
65 |
|
---|
66 | forall(otype T | { void print(T); })
|
---|
67 | void foo(T t) {
|
---|
68 | Box(T) b = { t };
|
---|
69 | with(b) { // with statement in polymorphic function
|
---|
70 | print(x);
|
---|
71 | printf("\n");
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | // ensure with-statement temporary generation works correctly
|
---|
76 | S mk() {
|
---|
77 | printf("called mk\n");
|
---|
78 | return (S) { 444 };
|
---|
79 | }
|
---|
80 |
|
---|
81 | // ensure with-statement temporary generation with reference-returning functions works correctly
|
---|
82 | S & ref() {
|
---|
83 | static S var = { 123456789 };
|
---|
84 | return var;
|
---|
85 | }
|
---|
86 |
|
---|
87 | int main() {
|
---|
88 | S2 s2 = { 12345 };
|
---|
89 | with (s2) {
|
---|
90 | with(s) { // with s2.s
|
---|
91 | printf("%d %d %d\n", i, s.i, s2.s.i);
|
---|
92 | foo(i); // s.i
|
---|
93 | with(mk()) {
|
---|
94 | printf("%d %d %d\n", i, i, i);
|
---|
95 | with(ref()) {
|
---|
96 | printf("%d %d %d\n", i, i, i);
|
---|
97 | } // with ref()
|
---|
98 | with(ref()) {
|
---|
99 | printf("%d %d %d\n", i, i, i);
|
---|
100 | } // with ref()
|
---|
101 | } // with mk()
|
---|
102 | } // with s
|
---|
103 | } // with s2
|
---|
104 | }
|
---|
105 |
|
---|
106 | // Local Variables: //
|
---|
107 | // tab-width: 4 //
|
---|
108 | // End: //
|
---|