address-spaces.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. template<typename T, typename U>
  3. struct is_same {
  4. static const bool value = false;
  5. };
  6. template<typename T>
  7. struct is_same<T, T> {
  8. static const bool value = true;
  9. };
  10. typedef int __attribute__((address_space(1))) int_1;;
  11. typedef int __attribute__((address_space(2))) int_2;;
  12. typedef int __attribute__((address_space(1))) *int_1_ptr;
  13. typedef int_2 *int_2_ptr;
  14. // Check that we maintain address spaces through template argument
  15. // deduction from a type.
  16. template<typename T>
  17. struct remove_pointer {
  18. typedef T type;
  19. };
  20. template<typename T>
  21. struct remove_pointer<T *> {
  22. typedef T type;
  23. };
  24. int check_remove0[is_same<remove_pointer<int_1_ptr>::type, int_1>::value? 1 : -1];
  25. int check_remove1[is_same<remove_pointer<int_2_ptr>::type, int_2>::value? 1 : -1];
  26. int check_remove2[is_same<remove_pointer<int_2_ptr>::type, int>::value? -1 : 1];
  27. int check_remove3[is_same<remove_pointer<int_2_ptr>::type, int_1>::value? -1 : 1];
  28. template<typename T>
  29. struct is_pointer_in_address_space_1 {
  30. static const bool value = false;
  31. };
  32. template<typename T>
  33. struct is_pointer_in_address_space_1<T __attribute__((address_space(1))) *> {
  34. static const bool value = true;
  35. };
  36. int check_ptr_in_as1[is_pointer_in_address_space_1<int_1_ptr>::value? 1 : -1];
  37. int check_ptr_in_as2[is_pointer_in_address_space_1<int_2_ptr>::value? -1 : 1];
  38. int check_ptr_in_as3[is_pointer_in_address_space_1<int*>::value? -1 : 1];
  39. // Check that we maintain address spaces through template argument
  40. // deduction for a call.
  41. template<typename T>
  42. void accept_any_pointer(T*) {
  43. T *x = 1; // expected-error{{cannot initialize a variable of type '__attribute__((address_space(1))) int *' with an rvalue of type 'int'}} \
  44. // expected-error{{cannot initialize a variable of type '__attribute__((address_space(3))) int *' with an rvalue of type 'int'}}
  45. }
  46. void test_accept_any_pointer(int_1_ptr ip1, int_2_ptr ip2) {
  47. static __attribute__((address_space(3))) int array[17];
  48. accept_any_pointer(ip1); // expected-note{{in instantiation of}}
  49. accept_any_pointer(array); // expected-note{{in instantiation of}}
  50. }
  51. template<typename T> struct identity {};
  52. template<typename T>
  53. identity<T> accept_arg_in_address_space_1(__attribute__((address_space(1))) T &ir1);
  54. template<typename T>
  55. identity<T> accept_any_arg(T &ir1);
  56. void test_arg_in_address_space_1() {
  57. static int __attribute__((address_space(1))) int_1;
  58. identity<int> ii = accept_arg_in_address_space_1(int_1);
  59. identity<int __attribute__((address_space(1)))> ii2 = accept_any_arg(int_1);
  60. }
  61. // Partial ordering
  62. template<typename T> int &order1(__attribute__((address_space(1))) T&);
  63. template<typename T> float &order1(T&);
  64. void test_order1() {
  65. static __attribute__((address_space(1))) int i1;
  66. int i;
  67. int &ir = order1(i1);
  68. float &fr = order1(i);
  69. }