Implement appropriate semantics for C++ casting and conversion when
dealing with address-space- and GC-qualified pointers. Previously,
these qualifiers were being treated just like cvr-qualifiers (in some
cases) or were completely ignored, leading to uneven behavior. For
example, const_cast would allow conversion between pointers to
different address spaces.
The new semantics are fairly simple: reinterpret_cast can be used to
explicitly cast between pointers to different address spaces
(including adding/removing addresss spaces), while
static_cast/dynamic_cast/const_cast do not tolerate any changes in the
address space. C-style casts can add/remove/change address spaces
through the reinterpret_cast mechanism. Other non-CVR qualifiers
(e.g., Objective-C GC qualifiers) work similarly.
As part of this change, I tweaked the "casts away constness"
diagnostic to use the term "casts away qualifiers". The term
"constness" actually comes from the C++ standard, despite the fact
that removing "volatile" also falls under that category. In Clang, we
also have restrict, address spaces, ObjC GC attributes, etc., so the
more general "qualifiers" is clearer.
llvm-svn: 129583
2011-04-16 01:59:54 +08:00
// 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 ,
2019-03-08 01:06:30 +08:00
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}}
Implement appropriate semantics for C++ casting and conversion when
dealing with address-space- and GC-qualified pointers. Previously,
these qualifiers were being treated just like cvr-qualifiers (in some
cases) or were completely ignored, leading to uneven behavior. For
example, const_cast would allow conversion between pointers to
different address spaces.
The new semantics are fairly simple: reinterpret_cast can be used to
explicitly cast between pointers to different address spaces
(including adding/removing addresss spaces), while
static_cast/dynamic_cast/const_cast do not tolerate any changes in the
address space. C-style casts can add/remove/change address spaces
through the reinterpret_cast mechanism. Other non-CVR qualifiers
(e.g., Objective-C GC qualifiers) work similarly.
As part of this change, I tweaked the "casts away constness"
diagnostic to use the term "casts away qualifiers". The term
"constness" actually comes from the C++ standard, despite the fact
that removing "volatile" also falls under that category. In Clang, we
also have restrict, address spaces, ObjC GC attributes, etc., so the
more general "qualifiers" is clearer.
llvm-svn: 129583
2011-04-16 01:59:54 +08:00
( void ) reinterpret_cast < A_ptr > ( bp ) ;
2019-03-08 01:06:30 +08:00
( 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}}
Implement appropriate semantics for C++ casting and conversion when
dealing with address-space- and GC-qualified pointers. Previously,
these qualifiers were being treated just like cvr-qualifiers (in some
cases) or were completely ignored, leading to uneven behavior. For
example, const_cast would allow conversion between pointers to
different address spaces.
The new semantics are fairly simple: reinterpret_cast can be used to
explicitly cast between pointers to different address spaces
(including adding/removing addresss spaces), while
static_cast/dynamic_cast/const_cast do not tolerate any changes in the
address space. C-style casts can add/remove/change address spaces
through the reinterpret_cast mechanism. Other non-CVR qualifiers
(e.g., Objective-C GC qualifiers) work similarly.
As part of this change, I tweaked the "casts away constness"
diagnostic to use the term "casts away qualifiers". The term
"constness" actually comes from the C++ standard, despite the fact
that removing "volatile" also falls under that category. In Clang, we
also have restrict, address spaces, ObjC GC attributes, etc., so the
more general "qualifiers" is clearer.
llvm-svn: 129583
2011-04-16 01:59:54 +08:00
( void ) reinterpret_cast < A_ptr > ( vp ) ;
2019-03-08 01:06:30 +08:00
( 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}}
Implement appropriate semantics for C++ casting and conversion when
dealing with address-space- and GC-qualified pointers. Previously,
these qualifiers were being treated just like cvr-qualifiers (in some
cases) or were completely ignored, leading to uneven behavior. For
example, const_cast would allow conversion between pointers to
different address spaces.
The new semantics are fairly simple: reinterpret_cast can be used to
explicitly cast between pointers to different address spaces
(including adding/removing addresss spaces), while
static_cast/dynamic_cast/const_cast do not tolerate any changes in the
address space. C-style casts can add/remove/change address spaces
through the reinterpret_cast mechanism. Other non-CVR qualifiers
(e.g., Objective-C GC qualifiers) work similarly.
As part of this change, I tweaked the "casts away constness"
diagnostic to use the term "casts away qualifiers". The term
"constness" actually comes from the C++ standard, despite the fact
that removing "volatile" also falls under that category. In Clang, we
also have restrict, address spaces, ObjC GC attributes, etc., so the
more general "qualifiers" is clearer.
llvm-svn: 129583
2011-04-16 01:59:54 +08:00
( void ) reinterpret_cast < A_ptr_1 > ( bp1 ) ;
2019-03-08 01:06:30 +08:00
( 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}}
Implement appropriate semantics for C++ casting and conversion when
dealing with address-space- and GC-qualified pointers. Previously,
these qualifiers were being treated just like cvr-qualifiers (in some
cases) or were completely ignored, leading to uneven behavior. For
example, const_cast would allow conversion between pointers to
different address spaces.
The new semantics are fairly simple: reinterpret_cast can be used to
explicitly cast between pointers to different address spaces
(including adding/removing addresss spaces), while
static_cast/dynamic_cast/const_cast do not tolerate any changes in the
address space. C-style casts can add/remove/change address spaces
through the reinterpret_cast mechanism. Other non-CVR qualifiers
(e.g., Objective-C GC qualifiers) work similarly.
As part of this change, I tweaked the "casts away constness"
diagnostic to use the term "casts away qualifiers". The term
"constness" actually comes from the C++ standard, despite the fact
that removing "volatile" also falls under that category. In Clang, we
also have restrict, address spaces, ObjC GC attributes, etc., so the
more general "qualifiers" is clearer.
llvm-svn: 129583
2011-04-16 01:59:54 +08:00
( void ) reinterpret_cast < A_ptr_1 > ( vp1 ) ;
2019-03-08 01:06:30 +08:00
( 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}}
Implement appropriate semantics for C++ casting and conversion when
dealing with address-space- and GC-qualified pointers. Previously,
these qualifiers were being treated just like cvr-qualifiers (in some
cases) or were completely ignored, leading to uneven behavior. For
example, const_cast would allow conversion between pointers to
different address spaces.
The new semantics are fairly simple: reinterpret_cast can be used to
explicitly cast between pointers to different address spaces
(including adding/removing addresss spaces), while
static_cast/dynamic_cast/const_cast do not tolerate any changes in the
address space. C-style casts can add/remove/change address spaces
through the reinterpret_cast mechanism. Other non-CVR qualifiers
(e.g., Objective-C GC qualifiers) work similarly.
As part of this change, I tweaked the "casts away constness"
diagnostic to use the term "casts away qualifiers". The term
"constness" actually comes from the C++ standard, despite the fact
that removing "volatile" also falls under that category. In Clang, we
also have restrict, address spaces, ObjC GC attributes, etc., so the
more general "qualifiers" is clearer.
llvm-svn: 129583
2011-04-16 01:59:54 +08:00
// ... 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}}
}