source: tests/raii/memberCtors.cfa @ 12a1013

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 12a1013 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
RevLine 
[4d4882a]1struct WrappedInt {
2  int x;
[a42a654]3  int id;
[4d4882a]4};
[a42a654]5int intID = 0;
[4d4882a]6
[2afec66]7void ?{}(WrappedInt & this) {
[a42a654]8  this.id = intID++;
9  printf("constructing int id: %d\n", this.id);
[2afec66]10  this.x = 0;
[4d4882a]11}
12
[2afec66]13void ?{}(WrappedInt & this, WrappedInt other) {
[a42a654]14  this.id = intID++;
15  printf("copy constructing int: %d id: %d\n", other.x, this.id);
[2afec66]16  this.x = other.x;
[4d4882a]17}
18
[2afec66]19void ?{}(WrappedInt & this, int x) {
[a42a654]20  this.id = intID++;
21  printf("constructing int: %d id: %d\n", x, this.id);
[2afec66]22  this.x = x;
[4d4882a]23}
24
[2afec66]25void ^?{}(WrappedInt & this) {
[a42a654]26  printf("destructing int: %d id: %d\n", this.x, this.id);
[4d4882a]27}
28
[72f85de]29/* WrappedInt */ void ?=?(WrappedInt & this, int x) {
[a42a654]30  printf("assigning int: %d %d id: %d\n", this.x, x, this.id);
[2afec66]31  this.x = x;
[72f85de]32  // return this;
[4d4882a]33}
34
[72f85de]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
[4d4882a]41struct A {
42  WrappedInt x, y, z;
[a42a654]43  int id;
[4d4882a]44};
[a42a654]45int AID = 0;
[4d4882a]46
[2afec66]47void ?{}(A & a) {
[235114f]48  // currently must define default ctor, since there's no "= default" syntax
[a42a654]49  a.id = AID++;
50  printf("default construct A %d\n", a.id);
[235114f]51}
52
[2afec66]53void ?{}(A & a, int x) {
[a42a654]54  a.id = AID++;
55  printf("begin construct A id: %d\n", a.id);
[2afec66]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
[f7e749f]60  printf("end construct A\n");
[4d4882a]61} // z never constructed - will be automatically default constructed
62
[2afec66]63void ?{}(A & this, A other) {
[a42a654]64  this.id = AID++;
65  printf("begin copy construct A id: %d\n", this.id);
[2afec66]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
[f7e749f]70  printf("end copy construct A\n");
[4d4882a]71} // z never constructed - will be automatically copy constructed
72
[2afec66]73A ?=?(A & this, A other) {
[a42a654]74  printf("begin ?=? A id: %d\n", this.id);
[2afec66]75  this.x = other.x;
76  this.y = other.y;
77  this.z = other.z;
[ddbde34]78  printf("end ?=? A\n");
[2afec66]79  return this;
[ddbde34]80}
81
[4d4882a]82struct B {
83  A a1, a2, a3;
[a42a654]84  int id;
[4d4882a]85};
[a42a654]86int BID = 0;
[4d4882a]87
[2afec66]88void ?{}(B & b) {
[a42a654]89  b.id = BID++;
90  printf("begin construct B id: %d\n", b.id);
[2afec66]91  printf("assign b.a2\n");
92  b.a2 = (A) { 2 };
93  printf("construct b.a1\n");
94  (b.a1){ 1 };
[4d4882a]95#ifdef ERR1
[2afec66]96  (b.a2){ b.a3 }; // error, b->a2 was used previously but is explicitly constructed
[4d4882a]97#endif
[f7e749f]98  printf("end construct B\n");
[4d4882a]99} // a2, a3 never constructed - will be automatically default constructed
100
[2afec66]101void ^?{}(B & b) {
[a42a654]102  b.id = BID++;
103  printf("begin destruct B id: %d\n", b.id);
[2afec66]104  b.a2 = (A) { 0 };
105  ^(b.a1){};
[72f85de]106  printf("end destruct B\n");
[4d4882a]107} // a2, a3 never destructed - will be automatically destructed
108
109int main() {
[44f6341]110  printf("Before declaration of b1\n");
[72f85de]111  B b1;  // b1 = { { 1000, 0, 0 }, { 1001, 0, 0 }, { 0, 0, 0 } }
[44f6341]112  printf("Before declaration of b2\n");
[4d4882a]113  B b2 = b1;
[44f6341]114  printf("End of main\n");
[4d4882a]115}
Note: See TracBrowser for help on using the repository browser.