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 : Peter A. Buhr |
---|
12 | // Last Modified On : Tue Dec 25 14:31:48 2018 |
---|
13 | // Update Count : 11 |
---|
14 | // |
---|
15 | |
---|
16 | #include <fstream.hfa> |
---|
17 | |
---|
18 | struct Y { int i; }; |
---|
19 | void ?{}( Y & y ) { sout | "Default constructing a Y"; } |
---|
20 | void ?{}( Y & y, Y other ) { sout | "Copy constructing a Y"; } |
---|
21 | void ^?{}( Y & y ) { sout | "Destructing a Y"; } |
---|
22 | Y ?=?( Y & y, Y other ) { sout | "Assigning a Y"; return y; } |
---|
23 | void ?{}( Y & y, int i ) { sout | "Value constructing a Y" | i; y.i = i; } |
---|
24 | |
---|
25 | struct X { Y & r; Y y; }; |
---|
26 | void ?{}( X & x ) { |
---|
27 | // ensure that r is not implicitly constructed |
---|
28 | } |
---|
29 | void ?{}( X & x, X other ) { |
---|
30 | // ensure that r is not implicitly constructed |
---|
31 | } |
---|
32 | void ^?{}( X & x ) { |
---|
33 | // ensure that r is not implicitly destructed |
---|
34 | } |
---|
35 | X ?=?( X & x, X other ) { return x; } |
---|
36 | |
---|
37 | // ensure that generated functions do not implicitly operate on references |
---|
38 | struct Z { Y & r; Y y; }; |
---|
39 | |
---|
40 | // test user-defined reference-returning function |
---|
41 | int & toref( int * p ) { return *p; } |
---|
42 | // test user-defined reference-parameter function |
---|
43 | int * toptr( int & r ) { return &r; } |
---|
44 | |
---|
45 | void changeRef( int & r ) { |
---|
46 | r++; |
---|
47 | } |
---|
48 | |
---|
49 | int main() { |
---|
50 | int x = 123456, x2 = 789, *p1 = &x, **p2 = &p1, ***p3 = &p2, |
---|
51 | &r1 = x, &&r2 = r1, &&&r3 = r2; |
---|
52 | ***p3 = 3; // change x |
---|
53 | **p3 = &x; // change p1 |
---|
54 | *p3 = &p1; // change p2 |
---|
55 | int y = 0, z = 11, & ar[3] = { x, y, z }; // initialize array of references |
---|
56 | // &ar[1] = &z; // change reference array element |
---|
57 | // typeof( ar[1] ) p = 3; // is int, i.e., the type of referenced object |
---|
58 | // typeof( &ar[1] ) q = &x; // is int *, i.e., the type of pointer |
---|
59 | // _Static_assert( sizeof( ar[1] ) == sizeof( int ), "Array type should be int." ); // is true, i.e., the size of referenced object |
---|
60 | // _Static_assert( sizeof( &ar[1] ) == sizeof( int *), "Address of array should be int *." ); // is true, i.e., the size of a reference |
---|
61 | |
---|
62 | ((int*&)&r3) = &x; // change r1, (&*)**r3 |
---|
63 | x = 3; |
---|
64 | // test that basic reference properties are true - r1 should be an alias for x |
---|
65 | sout | x | r1 | &x == &r1; |
---|
66 | r1 = 12; |
---|
67 | sout | x | r1 | &x == &r1; |
---|
68 | |
---|
69 | // test that functions using basic references work |
---|
70 | sout | toref( &x ) | toref( p1 ) | toptr( r1 ) == toptr( x ) | toptr( r1 ) == &x; |
---|
71 | |
---|
72 | changeRef( x ); |
---|
73 | changeRef( y ); |
---|
74 | changeRef( z ); |
---|
75 | sout | x | y | z; |
---|
76 | changeRef( r1 ); |
---|
77 | sout | r1 | x; |
---|
78 | |
---|
79 | r3 = 6; // change x, ***r3 |
---|
80 | sout | "x = " | x | " ; x2 = " | x2; // check that x was changed |
---|
81 | &r3 = &x2; // change r1 to refer to x2, (&*)**r3 |
---|
82 | r3 = 999; // modify x2 |
---|
83 | sout | "x = " | x | " ; x2 = " | x2; // check that x2 was changed |
---|
84 | ((int**&)&&r3) = p2; // change r2, (&(&*)*)*r3, ensure explicit cast to reference works |
---|
85 | r3 = 12345; // modify x |
---|
86 | sout | "x = " | x | " ; x2 = " | x2; // check that x was changed |
---|
87 | &&&r3 = p3; // change r3 to p3, (&(&(&*)*)*)r3 |
---|
88 | ((int&)r3) = 22222; // modify x, ensure explicit cast to reference works |
---|
89 | sout | "x = " | x | " ; x2 = " | x2; // check that x was changed |
---|
90 | |
---|
91 | // test that reference members are not implicitly constructed/destructed/assigned |
---|
92 | X x1, x2 = x1; |
---|
93 | x1 = x2; |
---|
94 | |
---|
95 | Z z1, z2 = z1; |
---|
96 | Y z1r = 56, z2r = 78; |
---|
97 | &z1.r = &z1r; |
---|
98 | &z2.r = &z2r; |
---|
99 | |
---|
100 | z1 = z2; |
---|
101 | |
---|
102 | // test rvalue-to-reference conversion |
---|
103 | { |
---|
104 | struct S { double x, y; }; |
---|
105 | void f( int & i, int & j, S & s, int v[] ) { |
---|
106 | sout | i | j | "{ " | s.[x, y] | " }," | "[" | v[0] | "," | v[1] | "," | v[2] | "]"; |
---|
107 | } |
---|
108 | void g(int & i) { sout | i; } |
---|
109 | void h(int &&& i) { sout | i; } |
---|
110 | |
---|
111 | int &&& r = 3; // rvalue to reference |
---|
112 | int i = r; |
---|
113 | sout | i | r; // both 3 |
---|
114 | |
---|
115 | g( 3 ); // rvalue to reference |
---|
116 | h( (int &&&)3 ); // rvalue to reference |
---|
117 | |
---|
118 | int a = 5, b = 4; |
---|
119 | f( 3, a + b, (S){ 1.0, 7.0 }, (int [3]){ 1, 2, 3 } ); // two rvalue to reference |
---|
120 | } |
---|
121 | |
---|
122 | { |
---|
123 | int a = 3; |
---|
124 | int *p = &a; |
---|
125 | asm ( |
---|
126 | "incl %[p]\n\t" |
---|
127 | : [p] "+m" (*p) |
---|
128 | ); |
---|
129 | printf("%d\n", a); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | // Local Variables: // |
---|
134 | // tab-width: 4 // |
---|
135 | // End: // |
---|