| 1 |
/* { dg-do run } */ |
| 2 |
/* { dg-additional-options "-fno-exceptions -fno-rtti" } */ |
| 3 |
|
| 4 |
extern "C" void free (void *); |
| 5 |
void fancy_abort () { __builtin_abort (); } |
| 6 |
struct vec_prefix |
| 7 |
{ |
| 8 |
unsigned m_alloc : 1; |
| 9 |
unsigned m_using_auto_storage : 1; |
| 10 |
unsigned m_num; |
| 11 |
}; |
| 12 |
struct vl_ptr |
| 13 |
; |
| 14 |
struct va_heap |
| 15 |
{ |
| 16 |
typedef vl_ptr default_layout; |
| 17 |
}; |
| 18 |
template<typename , |
| 19 |
typename A , |
| 20 |
typename = typename A::default_layout> |
| 21 |
struct vec |
| 22 |
; |
| 23 |
template<typename T, typename A> |
| 24 |
struct vec<T, A, int> |
| 25 |
{ |
| 26 |
T & operator[] (unsigned ix) |
| 27 |
{ |
| 28 |
int *__trans_tmp_2; |
| 29 |
!m_vecpfx.m_num ? fancy_abort (), 0 : 0; |
| 30 |
__trans_tmp_2 = reinterpret_cast <T *> (this + 1); |
| 31 |
return __trans_tmp_2[ix]; |
| 32 |
} |
| 33 |
bool iterate (unsigned , T *ptr) { |
| 34 |
*ptr = 0; |
| 35 |
return false; |
| 36 |
} |
| 37 |
void embedded_init (unsigned , unsigned num, unsigned aut) |
| 38 |
{ |
| 39 |
m_vecpfx.m_alloc = |
| 40 |
m_vecpfx.m_using_auto_storage = aut; |
| 41 |
m_vecpfx.m_num = num; |
| 42 |
} |
| 43 |
void quick_grow (unsigned len) |
| 44 |
{ |
| 45 |
!m_vecpfx.m_alloc ? fancy_abort (), 0 : m_vecpfx.m_num = len; |
| 46 |
} |
| 47 |
vec_prefix m_vecpfx; |
| 48 |
}; |
| 49 |
template<typename T, int N > |
| 50 |
struct auto_vec : vec<T, va_heap> |
| 51 |
{ |
| 52 |
auto_vec () |
| 53 |
{ |
| 54 |
m_auto.embedded_init (N, 0, 1); |
| 55 |
long off = (char *) &m_auto - (char *) this; |
| 56 |
this->m_vec = (vec<T, va_heap, int> *) ((char *) this + off); |
| 57 |
} |
| 58 |
~auto_vec () |
| 59 |
{ |
| 60 |
this->release (); |
| 61 |
} |
| 62 |
vec<T, va_heap, int> m_auto; |
| 63 |
char m_data[2 * sizeof (int)]; |
| 64 |
}; |
| 65 |
template<typename T> |
| 66 |
struct vec<T, va_heap> |
| 67 |
{ |
| 68 |
void |
| 69 |
release () |
| 70 |
{ |
| 71 |
bool __trans_tmp_1 = m_vec ? m_vec->m_vecpfx.m_using_auto_storage : false; |
| 72 |
if (__trans_tmp_1) |
| 73 |
return; |
| 74 |
vec<int, va_heap, int> *&v = m_vec; |
| 75 |
free (m_vec); |
| 76 |
v = nullptr; |
| 77 |
} |
| 78 |
T &operator[] (unsigned ix) { return (*m_vec)[ix]; } |
| 79 |
bool |
| 80 |
iterate (unsigned ix, T *ptr) { |
| 81 |
m_vec->iterate (ix, ptr); |
| 82 |
return false; |
| 83 |
} |
| 84 |
void |
| 85 |
quick_grow (unsigned len) |
| 86 |
{ |
| 87 |
m_vec->quick_grow (len); |
| 88 |
} |
| 89 |
vec<T, va_heap, int> *m_vec; |
| 90 |
}; |
| 91 |
void test_auto_alias () |
| 92 |
{ |
| 93 |
volatile int i = 1; |
| 94 |
auto_vec<int, 8> v; |
| 95 |
v.quick_grow (2); |
| 96 |
v[0] = 1; |
| 97 |
v[1] = 2; |
| 98 |
int val; |
| 99 |
for (int ix = i; v.iterate (ix, &val); ix++) |
| 100 |
if (val != 2) |
| 101 |
__builtin_abort (); |
| 102 |
if (val != 0) |
| 103 |
__builtin_abort (); |
| 104 |
} |
| 105 |
int main() |
| 106 |
{ |
| 107 |
test_auto_alias (); |
| 108 |
return 0; |
| 109 |
} |