source: src/tests/with-statement.c @ 1629965

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 1629965 was 866f560, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Add with-statement test

  • Property mode set to 100644
File size: 2.0 KB
Line 
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
16struct 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
23void ?{}(S & this, int n) with(this) {
24  i = n;
25  ptr = (int *)malloc(sizeof(int));
26}
27
28void ?{}(S & this) {
29  this{ 0 };
30}
31
32void ?{}(S & this, S other) {
33  this{ other.i };
34}
35
36S ?=?(S & this, S other) with(this) {
37  i = other.i;
38  *ptr = *other.ptr;
39  return this;
40}
41
42void ^?{}(S & this) with(this) {
43  free(ptr);
44}
45
46struct S2 {
47  S s;
48};
49
50void ?{}(S2 & this, int n) {
51  (this.s){ n };
52}
53
54forall(otype T)
55struct Box {
56  T x;
57};
58
59forall(otype T)
60void ?{}(Box(T) & this) with(this) { // with clause in polymorphic function
61  x{};
62}
63
64void print(int i) { printf("%d", i); }
65
66forall(otype T | { void print(T); })
67void 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
76S mk() {
77  printf("called mk\n");
78  return (S) { 444 };
79}
80
81// ensure with-statement temporary generation with reference-returning functions works correctly
82S & ref() {
83  static S var = { 123456789 };
84  return var;
85}
86
87int 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: //
Note: See TracBrowser for help on using the repository browser.