[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 | |
---|
[ec776c4] | 47 | // --- temporary code needed to make array of references subscript work. |
---|
| 48 | extern "C" { |
---|
| 49 | void ** __index(__attribute__ ((unused)) size_t sizeof_T, __attribute__ ((unused)) size_t alignof_T, void **x, ptrdiff_t y) { |
---|
| 50 | return (void **)((char *)x+y*sizeof(void *)); |
---|
| 51 | } |
---|
| 52 | void __ctor(void ***dst, void **src) { |
---|
| 53 | *dst = src; |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | __attribute__((alias("__index"))) forall( dtype T | sized(T) ) T && ?[?]( T & * x, ptrdiff_t y ); |
---|
| 57 | __attribute__((alias("__ctor"))) forall( dtype DT ) void ?{}( DT & * & dst, DT & * src); |
---|
| 58 | forall( dtype DT ) void ^?{}( DT & * & ) {} |
---|
| 59 | // --- end of temporary code |
---|
| 60 | |
---|
[a8555c5] | 61 | int main() { |
---|
[101a4d2] | 62 | int x = 123456, x2 = 789, *p1 = &x, **p2 = &p1, ***p3 = &p2, |
---|
[a8555c5] | 63 | &r1 = x, &&r2 = r1, &&&r3 = r2; |
---|
| 64 | ***p3 = 3; // change x |
---|
| 65 | **p3 = &x; // change p1 |
---|
| 66 | *p3 = &p1; // change p2 |
---|
[b05a4eb] | 67 | int y = 0, z = 11, & ar[3] = { x, y, z }; // initialize array of references |
---|
[ec776c4] | 68 | &ar[1] = &z; // change reference array element |
---|
| 69 | typeof( ar[1] ) p = 3; // is int, i.e., the type of referenced object |
---|
| 70 | typeof( &ar[1] ) q = &x; // is int *, i.e., the type of pointer |
---|
| 71 | _Static_assert( sizeof( ar[1] ) == sizeof( int ), "Array type should be int." ); // is true, i.e., the size of referenced object |
---|
| 72 | _Static_assert( sizeof( &ar[1] ) == sizeof( int *), "Address of array should be int *." ); // is true, i.e., the size of a reference |
---|
[a8555c5] | 73 | |
---|
[101a4d2] | 74 | ((int*&)&r3) = &x; // change r1, (&*)**r3 |
---|
| 75 | x = 3; |
---|
[a8555c5] | 76 | // test that basic reference properties are true - r1 should be an alias for x |
---|
| 77 | printf("%d %d %d\n", x, r1, &x == &r1); |
---|
| 78 | r1 = 12; |
---|
| 79 | printf("%d %d %d\n", x, r1, &x == &r1); |
---|
| 80 | |
---|
| 81 | // test that functions using basic references work |
---|
| 82 | printf("%d %d %d %d\n", toref(&x), toref(p1), toptr(r1) == toptr(x), toptr(r1) == &x); |
---|
| 83 | |
---|
[b05a4eb] | 84 | changeRef( x ); |
---|
| 85 | changeRef( y ); |
---|
| 86 | changeRef( z ); |
---|
| 87 | printf("%d %d %d\n", x, y, z); |
---|
| 88 | changeRef( r1 ); |
---|
| 89 | printf("%d %d\n", r1, x); |
---|
| 90 | |
---|
[9d5fb67] | 91 | r3 = 6; // change x, ***r3 |
---|
[101a4d2] | 92 | printf("x = %d ; x2 = %d\n", x, x2); // check that x was changed |
---|
[9d5fb67] | 93 | &r3 = &x2; // change r1 to refer to x2, (&*)**r3 |
---|
| 94 | r3 = 999; // modify x2 |
---|
[101a4d2] | 95 | printf("x = %d ; x2 = %d\n", x, x2); // check that x2 was changed |
---|
[9d5fb67] | 96 | ((int**&)&&r3) = p2; // change r2, (&(&*)*)*r3, ensure explicit cast to reference works |
---|
| 97 | r3 = 12345; // modify x |
---|
[101a4d2] | 98 | printf("x = %d ; x2 = %d\n", x, x2); // check that x was changed |
---|
[9d5fb67] | 99 | &&&r3 = p3; // change r3 to p3, (&(&(&*)*)*)r3 |
---|
| 100 | ((int&)r3) = 22222; // modify x, ensure explicit cast to reference works |
---|
[101a4d2] | 101 | printf("x = %d ; x2 = %d\n", x, x2); // check that x was changed |
---|
| 102 | |
---|
[a8555c5] | 103 | // test that reference members are not implicitly constructed/destructed/assigned |
---|
| 104 | X x1, x2 = x1; |
---|
| 105 | x1 = x2; |
---|
[12536d3] | 106 | |
---|
| 107 | Z z1, z2 = z1; |
---|
| 108 | Y z1r = 56, z2r = 78; |
---|
| 109 | &z1.r = &z1r; |
---|
| 110 | &z2.r = &z2r; |
---|
[0415e24] | 111 | |
---|
[12536d3] | 112 | z1 = z2; |
---|
[12db9e4] | 113 | |
---|
| 114 | // test rvalue-to-reference conversion |
---|
| 115 | { |
---|
| 116 | struct S { double x, y; }; |
---|
| 117 | void f( int & i, int & j, S & s, int v[] ) { |
---|
| 118 | printf("%d %d { %g, %g }, [%d, %d, %d]\n", i, j, s.[x, y], v[0], v[1], v[2]); |
---|
| 119 | } |
---|
| 120 | void g(int & i) { printf("%d\n", i); } |
---|
| 121 | void h(int &&& i) { printf("%d\n", i); } |
---|
| 122 | |
---|
| 123 | int &&& r = 3; // rvalue to reference |
---|
| 124 | int i = r; |
---|
| 125 | printf("%d %d\n", i, r); // both 3 |
---|
| 126 | |
---|
| 127 | g( 3 ); // rvalue to reference |
---|
| 128 | h( (int &&&)3 ); // rvalue to reference |
---|
| 129 | |
---|
| 130 | int a = 5, b = 4; |
---|
| 131 | f( 3, a + b, (S){ 1.0, 7.0 }, (int [3]){ 1, 2, 3 } ); // two rvalue to reference |
---|
| 132 | } |
---|
[a8555c5] | 133 | } |
---|
| 134 | |
---|
| 135 | // Local Variables: // |
---|
| 136 | // tab-width: 4 // |
---|
| 137 | // End: // |
---|
| 138 | |
---|