source: tests/raii/memberCtors.c @ 90152a4

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 90152a4 was 90152a4, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Merge branch 'master' into cleanup-dtors

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