xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/address-space-conversion.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // This test checks for the various conversions and casting operations
4*f4a2713aSLionel Sambuc // with address-space-qualified pointers.
5*f4a2713aSLionel Sambuc 
~AA6*f4a2713aSLionel Sambuc struct A { virtual ~A() {} };
7*f4a2713aSLionel Sambuc struct B : A { };
8*f4a2713aSLionel Sambuc 
9*f4a2713aSLionel Sambuc typedef void *void_ptr;
10*f4a2713aSLionel Sambuc typedef void __attribute__((address_space(1))) *void_ptr_1;
11*f4a2713aSLionel Sambuc typedef void __attribute__((address_space(2))) *void_ptr_2;
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc typedef int *int_ptr;
14*f4a2713aSLionel Sambuc typedef int __attribute__((address_space(1))) *int_ptr_1;
15*f4a2713aSLionel Sambuc typedef int __attribute__((address_space(2))) *int_ptr_2;
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc typedef A *A_ptr;
18*f4a2713aSLionel Sambuc typedef A __attribute__((address_space(1))) *A_ptr_1;
19*f4a2713aSLionel Sambuc typedef A __attribute__((address_space(2))) *A_ptr_2;
20*f4a2713aSLionel Sambuc 
21*f4a2713aSLionel Sambuc typedef B *B_ptr;
22*f4a2713aSLionel Sambuc typedef B __attribute__((address_space(1))) *B_ptr_1;
23*f4a2713aSLionel Sambuc typedef B __attribute__((address_space(2))) *B_ptr_2;
24*f4a2713aSLionel Sambuc 
test_const_cast(int_ptr ip,int_ptr_1 ip1,int_ptr_2 ip2,A_ptr ap,A_ptr_1 ap1,A_ptr_2 ap2,const int * cip,const int * cip1)25*f4a2713aSLionel Sambuc void test_const_cast(int_ptr ip, int_ptr_1 ip1, int_ptr_2 ip2,
26*f4a2713aSLionel Sambuc                      A_ptr ap, A_ptr_1 ap1, A_ptr_2 ap2,
27*f4a2713aSLionel Sambuc                      const int *cip,
28*f4a2713aSLionel Sambuc                      const int __attribute__((address_space(1))) *cip1) {
29*f4a2713aSLionel Sambuc   // Cannot use const_cast to cast between address spaces, add an
30*f4a2713aSLionel Sambuc   // address space, or remove an address space.
31*f4a2713aSLionel Sambuc   (void)const_cast<int_ptr>(ip1); // expected-error{{is not allowed}}
32*f4a2713aSLionel Sambuc   (void)const_cast<int_ptr>(ip2); // expected-error{{is not allowed}}
33*f4a2713aSLionel Sambuc   (void)const_cast<int_ptr_1>(ip); // expected-error{{is not allowed}}
34*f4a2713aSLionel Sambuc   (void)const_cast<int_ptr_1>(ip2); // expected-error{{is not allowed}}
35*f4a2713aSLionel Sambuc   (void)const_cast<int_ptr_2>(ip); // expected-error{{is not allowed}}
36*f4a2713aSLionel Sambuc   (void)const_cast<int_ptr_2>(ip1); // expected-error{{is not allowed}}
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc   (void)const_cast<A_ptr>(ap1); // expected-error{{is not allowed}}
39*f4a2713aSLionel Sambuc   (void)const_cast<A_ptr>(ap2); // expected-error{{is not allowed}}
40*f4a2713aSLionel Sambuc   (void)const_cast<A_ptr_1>(ap); // expected-error{{is not allowed}}
41*f4a2713aSLionel Sambuc   (void)const_cast<A_ptr_1>(ap2); // expected-error{{is not allowed}}
42*f4a2713aSLionel Sambuc   (void)const_cast<A_ptr_2>(ap); // expected-error{{is not allowed}}
43*f4a2713aSLionel Sambuc   (void)const_cast<A_ptr_2>(ap1); // expected-error{{is not allowed}}
44*f4a2713aSLionel Sambuc 
45*f4a2713aSLionel Sambuc   // It's acceptable to cast away constness.
46*f4a2713aSLionel Sambuc   (void)const_cast<int_ptr>(cip);
47*f4a2713aSLionel Sambuc   (void)const_cast<int_ptr_1>(cip1);
48*f4a2713aSLionel Sambuc }
49*f4a2713aSLionel Sambuc 
test_static_cast(void_ptr vp,void_ptr_1 vp1,void_ptr_2 vp2,A_ptr ap,A_ptr_1 ap1,A_ptr_2 ap2,B_ptr bp,B_ptr_1 bp1,B_ptr_2 bp2)50*f4a2713aSLionel Sambuc void test_static_cast(void_ptr vp, void_ptr_1 vp1, void_ptr_2 vp2,
51*f4a2713aSLionel Sambuc                       A_ptr ap, A_ptr_1 ap1, A_ptr_2 ap2,
52*f4a2713aSLionel Sambuc                       B_ptr bp, B_ptr_1 bp1, B_ptr_2 bp2) {
53*f4a2713aSLionel Sambuc   // Well-formed upcast
54*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr>(bp);
55*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr_1>(bp1);
56*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr_2>(bp2);
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc   // Well-formed downcast
59*f4a2713aSLionel Sambuc   (void)static_cast<B_ptr>(ap);
60*f4a2713aSLionel Sambuc   (void)static_cast<B_ptr_1>(ap1);
61*f4a2713aSLionel Sambuc   (void)static_cast<B_ptr_2>(ap2);
62*f4a2713aSLionel Sambuc 
63*f4a2713aSLionel Sambuc   // Well-formed cast to/from void
64*f4a2713aSLionel Sambuc   (void)static_cast<void_ptr>(ap);
65*f4a2713aSLionel Sambuc   (void)static_cast<void_ptr_1>(ap1);
66*f4a2713aSLionel Sambuc   (void)static_cast<void_ptr_2>(ap2);
67*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr>(vp);
68*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr_1>(vp1);
69*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr_2>(vp2);
70*f4a2713aSLionel Sambuc 
71*f4a2713aSLionel Sambuc   // Ill-formed upcasts
72*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr>(bp1); // expected-error{{is not allowed}}
73*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr>(bp2); // expected-error{{is not allowed}}
74*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr_1>(bp); // expected-error{{is not allowed}}
75*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr_1>(bp2); // expected-error{{is not allowed}}
76*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr_2>(bp); // expected-error{{is not allowed}}
77*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr_2>(bp1); // expected-error{{is not allowed}}
78*f4a2713aSLionel Sambuc 
79*f4a2713aSLionel Sambuc   // Ill-formed downcasts
80*f4a2713aSLionel Sambuc   (void)static_cast<B_ptr>(ap1); // expected-error{{casts away qualifiers}}
81*f4a2713aSLionel Sambuc   (void)static_cast<B_ptr>(ap2); // expected-error{{casts away qualifiers}}
82*f4a2713aSLionel Sambuc   (void)static_cast<B_ptr_1>(ap); // expected-error{{casts away qualifiers}}
83*f4a2713aSLionel Sambuc   (void)static_cast<B_ptr_1>(ap2); // expected-error{{casts away qualifiers}}
84*f4a2713aSLionel Sambuc   (void)static_cast<B_ptr_2>(ap); // expected-error{{casts away qualifiers}}
85*f4a2713aSLionel Sambuc   (void)static_cast<B_ptr_2>(ap1); // expected-error{{casts away qualifiers}}
86*f4a2713aSLionel Sambuc 
87*f4a2713aSLionel Sambuc   // Ill-formed cast to/from void
88*f4a2713aSLionel Sambuc   (void)static_cast<void_ptr>(ap1); // expected-error{{is not allowed}}
89*f4a2713aSLionel Sambuc   (void)static_cast<void_ptr>(ap2); // expected-error{{is not allowed}}
90*f4a2713aSLionel Sambuc   (void)static_cast<void_ptr_1>(ap); // expected-error{{is not allowed}}
91*f4a2713aSLionel Sambuc   (void)static_cast<void_ptr_1>(ap2); // expected-error{{is not allowed}}
92*f4a2713aSLionel Sambuc   (void)static_cast<void_ptr_2>(ap); // expected-error{{is not allowed}}
93*f4a2713aSLionel Sambuc   (void)static_cast<void_ptr_2>(ap1); // expected-error{{is not allowed}}
94*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr>(vp1); // expected-error{{casts away qualifiers}}
95*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr>(vp2); // expected-error{{casts away qualifiers}}
96*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr_1>(vp); // expected-error{{casts away qualifiers}}
97*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr_1>(vp2); // expected-error{{casts away qualifiers}}
98*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr_2>(vp); // expected-error{{casts away qualifiers}}
99*f4a2713aSLionel Sambuc   (void)static_cast<A_ptr_2>(vp1); // expected-error{{casts away qualifiers}}
100*f4a2713aSLionel Sambuc }
101*f4a2713aSLionel Sambuc 
test_dynamic_cast(A_ptr ap,A_ptr_1 ap1,A_ptr_2 ap2,B_ptr bp,B_ptr_1 bp1,B_ptr_2 bp2)102*f4a2713aSLionel Sambuc void test_dynamic_cast(A_ptr ap, A_ptr_1 ap1, A_ptr_2 ap2,
103*f4a2713aSLionel Sambuc                        B_ptr bp, B_ptr_1 bp1, B_ptr_2 bp2) {
104*f4a2713aSLionel Sambuc   // Well-formed upcast
105*f4a2713aSLionel Sambuc   (void)dynamic_cast<A_ptr>(bp);
106*f4a2713aSLionel Sambuc   (void)dynamic_cast<A_ptr_1>(bp1);
107*f4a2713aSLionel Sambuc   (void)dynamic_cast<A_ptr_2>(bp2);
108*f4a2713aSLionel Sambuc 
109*f4a2713aSLionel Sambuc   // Well-formed downcast
110*f4a2713aSLionel Sambuc   (void)dynamic_cast<B_ptr>(ap);
111*f4a2713aSLionel Sambuc   (void)dynamic_cast<B_ptr_1>(ap1);
112*f4a2713aSLionel Sambuc   (void)dynamic_cast<B_ptr_2>(ap2);
113*f4a2713aSLionel Sambuc 
114*f4a2713aSLionel Sambuc   // Ill-formed upcasts
115*f4a2713aSLionel Sambuc   (void)dynamic_cast<A_ptr>(bp1); // expected-error{{casts away qualifiers}}
116*f4a2713aSLionel Sambuc   (void)dynamic_cast<A_ptr>(bp2); // expected-error{{casts away qualifiers}}
117*f4a2713aSLionel Sambuc   (void)dynamic_cast<A_ptr_1>(bp); // expected-error{{casts away qualifiers}}
118*f4a2713aSLionel Sambuc   (void)dynamic_cast<A_ptr_1>(bp2); // expected-error{{casts away qualifiers}}
119*f4a2713aSLionel Sambuc   (void)dynamic_cast<A_ptr_2>(bp); // expected-error{{casts away qualifiers}}
120*f4a2713aSLionel Sambuc   (void)dynamic_cast<A_ptr_2>(bp1); // expected-error{{casts away qualifiers}}
121*f4a2713aSLionel Sambuc 
122*f4a2713aSLionel Sambuc   // Ill-formed downcasts
123*f4a2713aSLionel Sambuc   (void)dynamic_cast<B_ptr>(ap1); // expected-error{{casts away qualifiers}}
124*f4a2713aSLionel Sambuc   (void)dynamic_cast<B_ptr>(ap2); // expected-error{{casts away qualifiers}}
125*f4a2713aSLionel Sambuc   (void)dynamic_cast<B_ptr_1>(ap); // expected-error{{casts away qualifiers}}
126*f4a2713aSLionel Sambuc   (void)dynamic_cast<B_ptr_1>(ap2); // expected-error{{casts away qualifiers}}
127*f4a2713aSLionel Sambuc   (void)dynamic_cast<B_ptr_2>(ap); // expected-error{{casts away qualifiers}}
128*f4a2713aSLionel Sambuc   (void)dynamic_cast<B_ptr_2>(ap1); // expected-error{{casts away qualifiers}}
129*f4a2713aSLionel Sambuc }
130*f4a2713aSLionel Sambuc 
test_reinterpret_cast(void_ptr vp,void_ptr_1 vp1,void_ptr_2 vp2,A_ptr ap,A_ptr_1 ap1,A_ptr_2 ap2,B_ptr bp,B_ptr_1 bp1,B_ptr_2 bp2,const void * cvp1)131*f4a2713aSLionel Sambuc void test_reinterpret_cast(void_ptr vp, void_ptr_1 vp1, void_ptr_2 vp2,
132*f4a2713aSLionel Sambuc                            A_ptr ap, A_ptr_1 ap1, A_ptr_2 ap2,
133*f4a2713aSLionel Sambuc                            B_ptr bp, B_ptr_1 bp1, B_ptr_2 bp2,
134*f4a2713aSLionel Sambuc                            const void __attribute__((address_space(1))) *cvp1) {
135*f4a2713aSLionel Sambuc   // reinterpret_cast can be used to cast to a different address space.
136*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr>(ap1);
137*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr>(ap2);
138*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr>(bp);
139*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr>(bp1);
140*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr>(bp2);
141*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr>(vp);
142*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr>(vp1);
143*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr>(vp2);
144*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr_1>(ap);
145*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr_1>(ap2);
146*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr_1>(bp);
147*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr_1>(bp1);
148*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr_1>(bp2);
149*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr_1>(vp);
150*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr_1>(vp1);
151*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr_1>(vp2);
152*f4a2713aSLionel Sambuc 
153*f4a2713aSLionel Sambuc   // ... but don't try to cast away constness!
154*f4a2713aSLionel Sambuc   (void)reinterpret_cast<A_ptr_2>(cvp1); // expected-error{{casts away qualifiers}}
155*f4a2713aSLionel Sambuc }
156*f4a2713aSLionel Sambuc 
test_cstyle_cast(void_ptr vp,void_ptr_1 vp1,void_ptr_2 vp2,A_ptr ap,A_ptr_1 ap1,A_ptr_2 ap2,B_ptr bp,B_ptr_1 bp1,B_ptr_2 bp2,const void * cvp1)157*f4a2713aSLionel Sambuc void test_cstyle_cast(void_ptr vp, void_ptr_1 vp1, void_ptr_2 vp2,
158*f4a2713aSLionel Sambuc                       A_ptr ap, A_ptr_1 ap1, A_ptr_2 ap2,
159*f4a2713aSLionel Sambuc                       B_ptr bp, B_ptr_1 bp1, B_ptr_2 bp2,
160*f4a2713aSLionel Sambuc                       const void __attribute__((address_space(1))) *cvp1) {
161*f4a2713aSLionel Sambuc   // C-style casts are the wild west of casts.
162*f4a2713aSLionel Sambuc   (void)(A_ptr)(ap1);
163*f4a2713aSLionel Sambuc   (void)(A_ptr)(ap2);
164*f4a2713aSLionel Sambuc   (void)(A_ptr)(bp);
165*f4a2713aSLionel Sambuc   (void)(A_ptr)(bp1);
166*f4a2713aSLionel Sambuc   (void)(A_ptr)(bp2);
167*f4a2713aSLionel Sambuc   (void)(A_ptr)(vp);
168*f4a2713aSLionel Sambuc   (void)(A_ptr)(vp1);
169*f4a2713aSLionel Sambuc   (void)(A_ptr)(vp2);
170*f4a2713aSLionel Sambuc   (void)(A_ptr_1)(ap);
171*f4a2713aSLionel Sambuc   (void)(A_ptr_1)(ap2);
172*f4a2713aSLionel Sambuc   (void)(A_ptr_1)(bp);
173*f4a2713aSLionel Sambuc   (void)(A_ptr_1)(bp1);
174*f4a2713aSLionel Sambuc   (void)(A_ptr_1)(bp2);
175*f4a2713aSLionel Sambuc   (void)(A_ptr_1)(vp);
176*f4a2713aSLionel Sambuc   (void)(A_ptr_1)(vp1);
177*f4a2713aSLionel Sambuc   (void)(A_ptr_1)(vp2);
178*f4a2713aSLionel Sambuc   (void)(A_ptr_2)(cvp1);
179*f4a2713aSLionel Sambuc }
180*f4a2713aSLionel Sambuc 
test_implicit_conversion(void_ptr vp,void_ptr_1 vp1,void_ptr_2 vp2,A_ptr ap,A_ptr_1 ap1,A_ptr_2 ap2,B_ptr bp,B_ptr_1 bp1,B_ptr_2 bp2)181*f4a2713aSLionel Sambuc void test_implicit_conversion(void_ptr vp, void_ptr_1 vp1, void_ptr_2 vp2,
182*f4a2713aSLionel Sambuc                               A_ptr ap, A_ptr_1 ap1, A_ptr_2 ap2,
183*f4a2713aSLionel Sambuc                               B_ptr bp, B_ptr_1 bp1, B_ptr_2 bp2) {
184*f4a2713aSLionel Sambuc   // Well-formed conversions
185*f4a2713aSLionel Sambuc   void_ptr vpA = ap;
186*f4a2713aSLionel Sambuc   void_ptr_1 vp_1A = ap1;
187*f4a2713aSLionel Sambuc   void_ptr_2 vp_2A = ap2;
188*f4a2713aSLionel Sambuc   A_ptr ap_A = bp;
189*f4a2713aSLionel Sambuc   A_ptr_1 ap_A1 = bp1;
190*f4a2713aSLionel Sambuc   A_ptr_2 ap_A2 = bp2;
191*f4a2713aSLionel Sambuc 
192*f4a2713aSLionel Sambuc   // Ill-formed conversions
193*f4a2713aSLionel Sambuc   void_ptr vpB = ap1; // expected-error{{cannot initialize a variable of type}}
194*f4a2713aSLionel Sambuc   void_ptr_1 vp_1B = ap2; // expected-error{{cannot initialize a variable of type}}
195*f4a2713aSLionel Sambuc   A_ptr ap_B = bp1; // expected-error{{cannot initialize a variable of type}}
196*f4a2713aSLionel Sambuc   A_ptr_1 ap_B1 = bp2; // expected-error{{cannot initialize a variable of type}}
197*f4a2713aSLionel Sambuc }
198