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