address-spaces.cpp revision a459cc26f6c895ae48742a36203f753cea8b3e91
1a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor// RUN: %clang_cc1 -fsyntax-only -verify %s
2a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor
3a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregortemplate<typename T, typename U>
4a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregorstruct is_same {
5a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  static const bool value = false;
6a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor};
7a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor
8a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregortemplate<typename T>
9a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregorstruct is_same<T, T> {
10a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  static const bool value = true;
11a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor};
12a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor
13a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregortypedef int __attribute__((address_space(1))) int_1;;
14a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregortypedef int __attribute__((address_space(2))) int_2;;
15a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregortypedef int __attribute__((address_space(1))) *int_1_ptr;
16a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregortypedef int_2 *int_2_ptr;
17a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor
18a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor// Check that we maintain address spaces through template argument
19a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor// deduction from a type.
20a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregortemplate<typename T>
21a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregorstruct remove_pointer {
22a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  typedef T type;
23a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor};
24a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor
25a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregortemplate<typename T>
26a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregorstruct remove_pointer<T *> {
27a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  typedef T type;
28a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor};
29a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor
30a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregorint check_remove0[is_same<remove_pointer<int_1_ptr>::type, int_1>::value? 1 : -1];
31a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregorint check_remove1[is_same<remove_pointer<int_2_ptr>::type, int_2>::value? 1 : -1];
32a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregorint check_remove2[is_same<remove_pointer<int_2_ptr>::type, int>::value? -1 : 1];
33a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregorint check_remove3[is_same<remove_pointer<int_2_ptr>::type, int_1>::value? -1 : 1];
34a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor
35a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor// Check that we maintain address spaces through template argument
36a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor// deduction for a call.
37a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregortemplate<typename T>
38a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregorvoid accept_any_pointer(T*) {
39a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  T *x = 1; // expected-error{{cannot initialize a variable of type '__attribute__((address_space(1))) int *' with an rvalue of type 'int'}} \
40a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  // expected-error{{cannot initialize a variable of type '__attribute__((address_space(3))) int *' with an rvalue of type 'int'}}
41a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor}
42a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor
43a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregorvoid test_accept_any_pointer(int_1_ptr ip1, int_2_ptr ip2) {
44a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  static __attribute__((address_space(3))) int array[17];
45a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  accept_any_pointer(ip1); // expected-note{{in instantiation of}}
46a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  accept_any_pointer(array); // expected-note{{in instantiation of}}
47a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor}
48a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor
49