| [a8555c5] | 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 | // references.c -- | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // Author           : Rob Schluntz | 
|---|
|  | 10 | // Created On       : Wed Aug 23 16:11:50 2017 | 
|---|
|  | 11 | // Last Modified By : Rob Schluntz | 
|---|
|  | 12 | // Last Modified On : Wed Aug 23 16:12:03 | 
|---|
|  | 13 | // Update Count     : 2 | 
|---|
|  | 14 | // | 
|---|
|  | 15 |  | 
|---|
|  | 16 | struct Y { int i; }; | 
|---|
|  | 17 | void ?{}(Y & y) { printf("Default constructing a Y\n"); } | 
|---|
|  | 18 | void ?{}(Y & y, Y other) { printf("Copy constructing a Y\n"); } | 
|---|
|  | 19 | void ^?{}(Y & y) { printf("Destructing a Y\n"); } | 
|---|
|  | 20 | Y ?=?(Y & y, Y other) { printf("Assigning a Y\n"); return y; } | 
|---|
| [12536d3] | 21 | void ?{}(Y & y, int i) { printf("Value constructing a Y %d\n", i); y.i = i; } | 
|---|
| [a8555c5] | 22 |  | 
|---|
|  | 23 | struct X { Y & r; Y y; }; | 
|---|
|  | 24 | void ?{}(X & x) { | 
|---|
|  | 25 | // ensure that r is not implicitly constructed | 
|---|
|  | 26 | } | 
|---|
|  | 27 | void ?{}(X & x, X other) { | 
|---|
|  | 28 | // ensure that r is not implicitly constructed | 
|---|
|  | 29 | } | 
|---|
|  | 30 | void ^?{}(X & x) { | 
|---|
|  | 31 | // ensure that r is not implicitly destructed | 
|---|
|  | 32 | } | 
|---|
|  | 33 | X ?=?(X & x, X other) { return x; } | 
|---|
|  | 34 |  | 
|---|
| [12536d3] | 35 | // ensure that generated functions do not implicitly operate on references | 
|---|
|  | 36 | struct Z { Y & r; Y y; }; | 
|---|
|  | 37 |  | 
|---|
| [a8555c5] | 38 | // test user-defined reference-returning function | 
|---|
|  | 39 | int & toref( int * p ) { return *p; } | 
|---|
|  | 40 | // test user-defined reference-parameter function | 
|---|
|  | 41 | int * toptr( int & r ) { return &r; } | 
|---|
|  | 42 |  | 
|---|
| [b05a4eb] | 43 | void changeRef( int & r ) { | 
|---|
|  | 44 | r++; | 
|---|
|  | 45 | } | 
|---|
|  | 46 |  | 
|---|
| [a8555c5] | 47 | int main() { | 
|---|
|  | 48 | int x = 123456, *p1 = &x, **p2 = &p1, ***p3 = &p2, | 
|---|
|  | 49 | &r1 = x,    &&r2 = r1,   &&&r3 = r2; | 
|---|
|  | 50 | ***p3 = 3;                          // change x | 
|---|
|  | 51 | **p3 = &x;                          // change p1 | 
|---|
|  | 52 | *p3 = &p1;                          // change p2 | 
|---|
| [b05a4eb] | 53 | int y = 0, z = 11, & ar[3] = { x, y, z };    // initialize array of references | 
|---|
| [a8555c5] | 54 |  | 
|---|
|  | 55 | // test that basic reference properties are true - r1 should be an alias for x | 
|---|
|  | 56 | printf("%d %d %d\n", x, r1, &x == &r1); | 
|---|
|  | 57 | r1 = 12; | 
|---|
|  | 58 | printf("%d %d %d\n", x, r1, &x == &r1); | 
|---|
|  | 59 |  | 
|---|
|  | 60 | // test that functions using basic references work | 
|---|
|  | 61 | printf("%d %d %d %d\n", toref(&x), toref(p1), toptr(r1) == toptr(x), toptr(r1) == &x); | 
|---|
|  | 62 |  | 
|---|
| [b05a4eb] | 63 | changeRef( x ); | 
|---|
|  | 64 | changeRef( y ); | 
|---|
|  | 65 | changeRef( z ); | 
|---|
|  | 66 | printf("%d %d %d\n", x, y, z); | 
|---|
|  | 67 | changeRef( r1 ); | 
|---|
|  | 68 | printf("%d %d\n", r1, x); | 
|---|
|  | 69 |  | 
|---|
| [a8555c5] | 70 | // test that reference members are not implicitly constructed/destructed/assigned | 
|---|
|  | 71 | X x1, x2 = x1; | 
|---|
|  | 72 | x1 = x2; | 
|---|
| [12536d3] | 73 |  | 
|---|
|  | 74 | Z z1, z2 = z1; | 
|---|
|  | 75 | Y z1r = 56, z2r = 78; | 
|---|
|  | 76 | &z1.r = &z1r; | 
|---|
|  | 77 | &z2.r = &z2r; | 
|---|
|  | 78 | z1 = z2; | 
|---|
| [a8555c5] | 79 | } | 
|---|
|  | 80 |  | 
|---|
|  | 81 | // Local Variables: // | 
|---|
|  | 82 | // tab-width: 4 // | 
|---|
|  | 83 | // End: // | 
|---|
|  | 84 |  | 
|---|