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