xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/member-access-expr.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc template<typename T>
call_f0(T x)3*f4a2713aSLionel Sambuc void call_f0(T x) {
4*f4a2713aSLionel Sambuc   x.Base::f0();
5*f4a2713aSLionel Sambuc }
6*f4a2713aSLionel Sambuc 
7*f4a2713aSLionel Sambuc struct Base {
8*f4a2713aSLionel Sambuc   void f0();
9*f4a2713aSLionel Sambuc };
10*f4a2713aSLionel Sambuc 
11*f4a2713aSLionel Sambuc struct X0 : Base {
12*f4a2713aSLionel Sambuc   typedef Base CrazyBase;
13*f4a2713aSLionel Sambuc };
14*f4a2713aSLionel Sambuc 
test_f0(X0 x0)15*f4a2713aSLionel Sambuc void test_f0(X0 x0) {
16*f4a2713aSLionel Sambuc   call_f0(x0);
17*f4a2713aSLionel Sambuc }
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc template<typename TheBase, typename T>
call_f0_through_typedef(T x)20*f4a2713aSLionel Sambuc void call_f0_through_typedef(T x) {
21*f4a2713aSLionel Sambuc   typedef TheBase Base2;
22*f4a2713aSLionel Sambuc   x.Base2::f0();
23*f4a2713aSLionel Sambuc }
24*f4a2713aSLionel Sambuc 
test_f0_through_typedef(X0 x0)25*f4a2713aSLionel Sambuc void test_f0_through_typedef(X0 x0) {
26*f4a2713aSLionel Sambuc   call_f0_through_typedef<Base>(x0);
27*f4a2713aSLionel Sambuc }
28*f4a2713aSLionel Sambuc 
29*f4a2713aSLionel Sambuc template<typename TheBase, typename T>
call_f0_through_typedef2(T x)30*f4a2713aSLionel Sambuc void call_f0_through_typedef2(T x) {
31*f4a2713aSLionel Sambuc   typedef TheBase CrazyBase; // expected-note{{current scope}}
32*f4a2713aSLionel Sambuc   x.CrazyBase::f0(); // expected-error{{ambiguous}} \
33*f4a2713aSLionel Sambuc                      // expected-error 2{{no member named}}
34*f4a2713aSLionel Sambuc }
35*f4a2713aSLionel Sambuc 
36*f4a2713aSLionel Sambuc struct OtherBase { };
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc struct X1 : Base, OtherBase {
39*f4a2713aSLionel Sambuc   typedef OtherBase CrazyBase; // expected-note{{object type}}
40*f4a2713aSLionel Sambuc };
41*f4a2713aSLionel Sambuc 
test_f0_through_typedef2(X0 x0,X1 x1)42*f4a2713aSLionel Sambuc void test_f0_through_typedef2(X0 x0, X1 x1) {
43*f4a2713aSLionel Sambuc   call_f0_through_typedef2<Base>(x0);
44*f4a2713aSLionel Sambuc   call_f0_through_typedef2<OtherBase>(x1); // expected-note{{instantiation}}
45*f4a2713aSLionel Sambuc   call_f0_through_typedef2<Base>(x1); // expected-note{{instantiation}}
46*f4a2713aSLionel Sambuc }
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc struct X2 {
50*f4a2713aSLionel Sambuc   operator int() const;
51*f4a2713aSLionel Sambuc };
52*f4a2713aSLionel Sambuc 
53*f4a2713aSLionel Sambuc template<typename T, typename U>
convert(const U & value)54*f4a2713aSLionel Sambuc T convert(const U& value) {
55*f4a2713aSLionel Sambuc   return value.operator T(); // expected-error{{operator long}}
56*f4a2713aSLionel Sambuc }
57*f4a2713aSLionel Sambuc 
test_convert(X2 x2)58*f4a2713aSLionel Sambuc void test_convert(X2 x2) {
59*f4a2713aSLionel Sambuc   convert<int>(x2);
60*f4a2713aSLionel Sambuc   convert<long>(x2); // expected-note{{instantiation}}
61*f4a2713aSLionel Sambuc }
62*f4a2713aSLionel Sambuc 
63*f4a2713aSLionel Sambuc template<typename T>
destruct(T * ptr)64*f4a2713aSLionel Sambuc void destruct(T* ptr) {
65*f4a2713aSLionel Sambuc   ptr->~T();
66*f4a2713aSLionel Sambuc   ptr->T::~T();
67*f4a2713aSLionel Sambuc }
68*f4a2713aSLionel Sambuc 
69*f4a2713aSLionel Sambuc template<typename T>
destruct_intptr(int * ip)70*f4a2713aSLionel Sambuc void destruct_intptr(int *ip) {
71*f4a2713aSLionel Sambuc   ip->~T();
72*f4a2713aSLionel Sambuc   ip->T::~T();
73*f4a2713aSLionel Sambuc }
74*f4a2713aSLionel Sambuc 
test_destruct(X2 * x2p,int * ip)75*f4a2713aSLionel Sambuc void test_destruct(X2 *x2p, int *ip) {
76*f4a2713aSLionel Sambuc   destruct(x2p);
77*f4a2713aSLionel Sambuc   destruct(ip);
78*f4a2713aSLionel Sambuc   destruct_intptr<int>(ip);
79*f4a2713aSLionel Sambuc }
80*f4a2713aSLionel Sambuc 
81*f4a2713aSLionel Sambuc // PR5220
82*f4a2713aSLionel Sambuc class X3 {
83*f4a2713aSLionel Sambuc protected:
84*f4a2713aSLionel Sambuc   template <int> float* &f0();
85*f4a2713aSLionel Sambuc   template <int> const float* &f0() const;
f1()86*f4a2713aSLionel Sambuc   void f1() {
87*f4a2713aSLionel Sambuc     (void)static_cast<float*>(f0<0>());
88*f4a2713aSLionel Sambuc   }
f1() const89*f4a2713aSLionel Sambuc   void f1() const{
90*f4a2713aSLionel Sambuc     (void)f0<0>();
91*f4a2713aSLionel Sambuc   }
92*f4a2713aSLionel Sambuc };
93*f4a2713aSLionel Sambuc 
94*f4a2713aSLionel Sambuc // Fun with template instantiation and conversions
95*f4a2713aSLionel Sambuc struct X4 {
96*f4a2713aSLionel Sambuc   int& member();
97*f4a2713aSLionel Sambuc   float& member() const;
98*f4a2713aSLionel Sambuc };
99*f4a2713aSLionel Sambuc 
100*f4a2713aSLionel Sambuc template<typename T>
101*f4a2713aSLionel Sambuc struct X5 {
fX5102*f4a2713aSLionel Sambuc   void f(T* ptr) { int& ir = ptr->member(); }
gX5103*f4a2713aSLionel Sambuc   void g(T* ptr) { float& fr = ptr->member(); }
104*f4a2713aSLionel Sambuc };
105*f4a2713aSLionel Sambuc 
test_X5(X5<X4> x5,X5<const X4> x5c,X4 * xp,const X4 * cxp)106*f4a2713aSLionel Sambuc void test_X5(X5<X4> x5, X5<const X4> x5c, X4 *xp, const X4 *cxp) {
107*f4a2713aSLionel Sambuc   x5.f(xp);
108*f4a2713aSLionel Sambuc   x5c.g(cxp);
109*f4a2713aSLionel Sambuc }
110*f4a2713aSLionel Sambuc 
111*f4a2713aSLionel Sambuc // In theory we can do overload resolution at template-definition time on this.
112*f4a2713aSLionel Sambuc // We should at least not assert.
113*f4a2713aSLionel Sambuc namespace test4 {
114*f4a2713aSLionel Sambuc   struct Base {
footest4::Base115*f4a2713aSLionel Sambuc     template <class T> void foo() {}
116*f4a2713aSLionel Sambuc   };
117*f4a2713aSLionel Sambuc 
118*f4a2713aSLionel Sambuc   template <class T> struct Foo : Base {
testtest4::Foo119*f4a2713aSLionel Sambuc     void test() {
120*f4a2713aSLionel Sambuc       foo<int>();
121*f4a2713aSLionel Sambuc     }
122*f4a2713aSLionel Sambuc   };
123*f4a2713aSLionel Sambuc }
124*f4a2713aSLionel Sambuc 
125*f4a2713aSLionel Sambuc namespace test5 {
126*f4a2713aSLionel Sambuc   template<typename T>
127*f4a2713aSLionel Sambuc   struct X {
128*f4a2713aSLionel Sambuc     using T::value;
129*f4a2713aSLionel Sambuc 
getValuetest5::X130*f4a2713aSLionel Sambuc     T &getValue() {
131*f4a2713aSLionel Sambuc       return &value;
132*f4a2713aSLionel Sambuc     }
133*f4a2713aSLionel Sambuc   };
134*f4a2713aSLionel Sambuc }
135*f4a2713aSLionel Sambuc 
136*f4a2713aSLionel Sambuc // PR8739
137*f4a2713aSLionel Sambuc namespace test6 {
138*f4a2713aSLionel Sambuc   struct A {};
139*f4a2713aSLionel Sambuc   struct B {};
140*f4a2713aSLionel Sambuc   template <class T> class Base;
141*f4a2713aSLionel Sambuc   template <class T> class Derived : public Base<T> {
142*f4a2713aSLionel Sambuc     A *field;
get(B ** ptr)143*f4a2713aSLionel Sambuc     void get(B **ptr) {
144*f4a2713aSLionel Sambuc       // It's okay if at some point we figure out how to diagnose this
145*f4a2713aSLionel Sambuc       // at instantiation time.
146*f4a2713aSLionel Sambuc       *ptr = field;
147*f4a2713aSLionel Sambuc     }
148*f4a2713aSLionel Sambuc   };
149*f4a2713aSLionel Sambuc }
150