reinterpret-cast.cpp revision 2f639b9f3c6b081f076d2ac6d75115ce44bfa249
12f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor// RUN: clang -fsyntax-only -verify %s
22f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor
32f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregorenum test { testval = 1 };
42f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregorstruct structure { int m; };
52f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregortypedef void (*fnptr)();
62f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor
72f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor// Test the conversion to self.
82f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregorvoid self_conversion()
92f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor{
102f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  // T*->T* is allowed, T->T in general not.
112f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  int i = 0;
122f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  (void)reinterpret_cast<int>(i); // expected-error {{reinterpret_cast from 'int' to 'int' is not allowed}}
132f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  structure s;
142f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  (void)reinterpret_cast<structure>(s); // expected-error {{reinterpret_cast from 'struct structure' to 'struct structure' is not allowed}}
152f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  int *pi = 0;
162f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  (void)reinterpret_cast<int*>(pi);
172f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor}
182f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor
192f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor// Test conversion between pointer and integral types, as in /3 and /4.
202f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregorvoid integral_conversion()
212f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor{
222f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  void *vp = reinterpret_cast<void*>(testval);
232f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  long l = reinterpret_cast<long>(vp);
242f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  (void)reinterpret_cast<float*>(l);
252f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  fnptr fnp = reinterpret_cast<fnptr>(l);
262f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  (void)reinterpret_cast<char>(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}}
272f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  (void)reinterpret_cast<long>(fnp);
282f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor}
292f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor
302f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregorvoid pointer_conversion()
312f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor{
322f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  int *p1 = 0;
332f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  float *p2 = reinterpret_cast<float*>(p1);
342f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  structure *p3 = reinterpret_cast<structure*>(p2);
352f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  typedef int **ppint;
362f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  ppint *deep = reinterpret_cast<ppint*>(p3);
372f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  (void)reinterpret_cast<fnptr*>(deep);
382f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor}
392f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor
402f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregorvoid constness()
412f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor{
422f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  int ***const ipppc = 0;
432f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  // Valid: T1* -> T2 const*
442f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  int const *icp = reinterpret_cast<int const*>(ipppc);
452f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  // Invalid: T1 const* -> T2*
462f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  (void)reinterpret_cast<int*>(icp); // expected-error {{reinterpret_cast from 'int const *' to 'int *' casts away constness}}
472f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  // Invalid: T1*** -> T2 const* const**
482f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  int const *const **icpcpp = reinterpret_cast<int const* const**>(ipppc); // expected-error {{reinterpret_cast from 'int ***const' to 'int const *const **' casts away constness}}
492f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  // Valid: T1* -> T2*
502f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  int *ip = reinterpret_cast<int*>(icpcpp);
512f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  // Valid: T* -> T const*
522f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  (void)reinterpret_cast<int const*>(ip);
532f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  // Valid: T*** -> T2 const* const* const*
542f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  (void)reinterpret_cast<int const* const* const*>(ipppc);
552f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor}
562f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor
572f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregorvoid fnptrs()
582f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor{
592f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  typedef int (*fnptr2)(int);
602f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  fnptr fp = 0;
612f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  (void)reinterpret_cast<fnptr2>(fp);
622f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  void *vp = reinterpret_cast<void*>(fp);
632f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  (void)reinterpret_cast<fnptr>(vp);
642f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor}
652f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor
662f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregorvoid refs()
672f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor{
682f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  long l = 0;
692f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  char &c = reinterpret_cast<char&>(l);
702f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  // Bad: from rvalue
712f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor  (void)reinterpret_cast<int&>(&c); // expected-error {{reinterpret_cast from rvalue to reference type 'int &'}}
722f639b9f3c6b081f076d2ac6d75115ce44bfa249Douglas Gregor}
73