source: tests/raii/memberCtors.cfa @ ded6c2a6

ast-experimental
Last change on this file since ded6c2a6 was cde3891, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Merge branch 'master' into cleanup-dtors

  • Property mode set to 100644
File size: 2.7 KB
Line 
1struct WrappedInt {
2  int x;
3  int id;
4};
5int intID = 0;
6
7void ?{}(WrappedInt & this) {
8  this.id = intID++;
9  printf("constructing int id: %d\n", this.id);
10  this.x = 0;
11}
12
13void ?{}(WrappedInt & this, WrappedInt other) {
14  this.id = intID++;
15  printf("copy constructing int: %d id: %d\n", other.x, this.id);
16  this.x = other.x;
17}
18
19void ?{}(WrappedInt & this, int x) {
20  this.id = intID++;
21  printf("constructing int: %d id: %d\n", x, this.id);
22  this.x = x;
23}
24
25void ^?{}(WrappedInt & this) {
26  printf("destructing int: %d id: %d\n", this.x, this.id);
27}
28
29/* WrappedInt */ void ?=?(WrappedInt & this, int x) {
30  printf("assigning int: %d %d id: %d\n", this.x, x, this.id);
31  this.x = x;
32  // return this;
33}
34
35// WrappedInt ?=?(WrappedInt & this, WrappedInt other) {
36//   printf("assigning int: %d %d\n", this.x, other.x);
37//   this.x = other.x;
38//   return this;
39// }
40
41struct A {
42  WrappedInt x, y, z;
43  int id;
44};
45int AID = 0;
46
47void ?{}(A & a) {
48  // currently must define default ctor, since there's no "= default" syntax
49  a.id = AID++;
50  printf("default construct A %d\n", a.id);
51}
52
53void ?{}(A & a, int x) {
54  a.id = AID++;
55  printf("begin construct A id: %d\n", a.id);
56  printf("construct a.x\n");
57  (a.x){ x+999 };
58  printf("assign a.y\n");
59  a.y = 0; // not a constructor - default constructor will be inserted
60  printf("end construct A\n");
61} // z never constructed - will be automatically default constructed
62
63void ?{}(A & this, A other) {
64  this.id = AID++;
65  printf("begin copy construct A id: %d\n", this.id);
66  printf("copy construct this.x\n");
67  (this.x){ other.x };
68  printf("assign this.y\n");
69  this.y = other.y; // not a constructor - copy constructor will be inserted
70  printf("end copy construct A\n");
71} // z never constructed - will be automatically copy constructed
72
73A ?=?(A & this, A other) {
74  printf("begin ?=? A id: %d\n", this.id);
75  this.x = other.x;
76  this.y = other.y;
77  this.z = other.z;
78  printf("end ?=? A\n");
79  return this;
80}
81
82struct B {
83  A a1, a2, a3;
84  int id;
85};
86int BID = 0;
87
88void ?{}(B & b) {
89  b.id = BID++;
90  printf("begin construct B id: %d\n", b.id);
91  printf("assign b.a2\n");
92  b.a2 = (A) { 2 };
93  printf("construct b.a1\n");
94  (b.a1){ 1 };
95#ifdef ERR1
96  (b.a2){ b.a3 }; // error, b->a2 was used previously but is explicitly constructed
97#endif
98  printf("end construct B\n");
99} // a2, a3 never constructed - will be automatically default constructed
100
101void ^?{}(B & b) {
102  b.id = BID++;
103  printf("begin destruct B id: %d\n", b.id);
104  b.a2 = (A) { 0 };
105  ^(b.a1){};
106  printf("end destruct B\n");
107} // a2, a3 never destructed - will be automatically destructed
108
109int main() {
110  printf("Before declaration of b1\n");
111  B b1;  // b1 = { { 1000, 0, 0 }, { 1001, 0, 0 }, { 0, 0, 0 } }
112  printf("Before declaration of b2\n");
113  B b2 = b1;
114  printf("End of main\n");
115}
Note: See TracBrowser for help on using the repository browser.