address-spaces.cpp revision 61d0b6baf47cf411f6c0f6ddb4acffcfeec724f1
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];
3461d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
3561d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregortemplate<typename T>
3661d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregorstruct is_pointer_in_address_space_1 {
3761d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  static const bool value = false;
3861d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor};
3961d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
4061d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregortemplate<typename T>
4161d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregorstruct is_pointer_in_address_space_1<T __attribute__((address_space(1))) *> {
4261d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  static const bool value = true;
4361d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor};
44a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor
4561d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregorint check_ptr_in_as1[is_pointer_in_address_space_1<int_1_ptr>::value? 1 : -1];
4661d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregorint check_ptr_in_as2[is_pointer_in_address_space_1<int_2_ptr>::value? -1 : 1];
4761d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregorint check_ptr_in_as3[is_pointer_in_address_space_1<int*>::value? -1 : 1];
4861d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
49a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor// Check that we maintain address spaces through template argument
50a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor// deduction for a call.
51a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregortemplate<typename T>
52a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregorvoid accept_any_pointer(T*) {
53a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  T *x = 1; // expected-error{{cannot initialize a variable of type '__attribute__((address_space(1))) int *' with an rvalue of type 'int'}} \
54a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  // expected-error{{cannot initialize a variable of type '__attribute__((address_space(3))) int *' with an rvalue of type 'int'}}
55a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor}
56a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor
57a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregorvoid test_accept_any_pointer(int_1_ptr ip1, int_2_ptr ip2) {
58a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  static __attribute__((address_space(3))) int array[17];
59a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  accept_any_pointer(ip1); // expected-note{{in instantiation of}}
60a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  accept_any_pointer(array); // expected-note{{in instantiation of}}
61a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor}
62a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor
6361d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregortemplate<typename T> struct identity {};
6461d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
6561d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregortemplate<typename T>
6661d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregoridentity<T> accept_arg_in_address_space_1(__attribute__((address_space(1))) T &ir1);
6761d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
6861d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregortemplate<typename T>
6961d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregoridentity<T> accept_any_arg(T &ir1);
7061d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
7161d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregorvoid test_arg_in_address_space_1() {
7261d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  static int __attribute__((address_space(1))) int_1;
7361d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  identity<int> ii = accept_arg_in_address_space_1(int_1);
7461d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  identity<int __attribute__((address_space(1)))> ii2 = accept_any_arg(int_1);
7561d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor}
76