xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/instantiate-method.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2f4a2713aSLionel Sambuc template<typename T>
3f4a2713aSLionel Sambuc class X {
4f4a2713aSLionel Sambuc public:
5f4a2713aSLionel Sambuc   void f(T x); // expected-error{{argument may not have 'void' type}}
6f4a2713aSLionel Sambuc   void g(T*);
7f4a2713aSLionel Sambuc 
8f4a2713aSLionel Sambuc   static int h(T, T); // expected-error {{argument may not have 'void' type}}
9f4a2713aSLionel Sambuc };
10f4a2713aSLionel Sambuc 
identity(int x)11f4a2713aSLionel Sambuc int identity(int x) { return x; }
12f4a2713aSLionel Sambuc 
test(X<int> * xi,int * ip,X<int (int)> * xf)13f4a2713aSLionel Sambuc void test(X<int> *xi, int *ip, X<int(int)> *xf) {
14f4a2713aSLionel Sambuc   xi->f(17);
15f4a2713aSLionel Sambuc   xi->g(ip);
16f4a2713aSLionel Sambuc   xf->f(&identity);
17f4a2713aSLionel Sambuc   xf->g(identity);
18f4a2713aSLionel Sambuc   X<int>::h(17, 25);
19f4a2713aSLionel Sambuc   X<int(int)>::h(identity, &identity);
20f4a2713aSLionel Sambuc }
21f4a2713aSLionel Sambuc 
test_bad()22f4a2713aSLionel Sambuc void test_bad() {
23f4a2713aSLionel Sambuc   X<void> xv; // expected-note{{in instantiation of template class 'X<void>' requested here}}
24f4a2713aSLionel Sambuc }
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc template<typename T, typename U>
27f4a2713aSLionel Sambuc class Overloading {
28f4a2713aSLionel Sambuc public:
29f4a2713aSLionel Sambuc   int& f(T, T); // expected-note{{previous declaration is here}}
30f4a2713aSLionel Sambuc   float& f(T, U); // expected-error{{functions that differ only in their return type cannot be overloaded}}
31f4a2713aSLionel Sambuc };
32f4a2713aSLionel Sambuc 
test_ovl(Overloading<int,long> * oil,int i,long l)33f4a2713aSLionel Sambuc void test_ovl(Overloading<int, long> *oil, int i, long l) {
34f4a2713aSLionel Sambuc   int &ir = oil->f(i, i);
35f4a2713aSLionel Sambuc   float &fr = oil->f(i, l);
36f4a2713aSLionel Sambuc }
37f4a2713aSLionel Sambuc 
test_ovl_bad()38f4a2713aSLionel Sambuc void test_ovl_bad() {
39f4a2713aSLionel Sambuc   Overloading<float, float> off; // expected-note{{in instantiation of template class 'Overloading<float, float>' requested here}}
40f4a2713aSLionel Sambuc }
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc template<typename T>
43f4a2713aSLionel Sambuc class HasDestructor {
44f4a2713aSLionel Sambuc public:
45f4a2713aSLionel Sambuc   virtual ~HasDestructor() = 0;
46f4a2713aSLionel Sambuc };
47f4a2713aSLionel Sambuc 
48f4a2713aSLionel Sambuc int i = sizeof(HasDestructor<int>); // FIXME: forces instantiation, but
49f4a2713aSLionel Sambuc                 // the code below should probably instantiate by itself.
50f4a2713aSLionel Sambuc int abstract_destructor[__is_abstract(HasDestructor<int>)? 1 : -1];
51f4a2713aSLionel Sambuc 
52f4a2713aSLionel Sambuc 
53f4a2713aSLionel Sambuc template<typename T>
54f4a2713aSLionel Sambuc class Constructors {
55f4a2713aSLionel Sambuc public:
56f4a2713aSLionel Sambuc   Constructors(const T&);
57f4a2713aSLionel Sambuc   Constructors(const Constructors &other);
58f4a2713aSLionel Sambuc };
59f4a2713aSLionel Sambuc 
test_constructors()60f4a2713aSLionel Sambuc void test_constructors() {
61f4a2713aSLionel Sambuc   Constructors<int> ci1(17);
62f4a2713aSLionel Sambuc   Constructors<int> ci2 = ci1;
63f4a2713aSLionel Sambuc }
64f4a2713aSLionel Sambuc 
65f4a2713aSLionel Sambuc 
66f4a2713aSLionel Sambuc template<typename T>
67f4a2713aSLionel Sambuc struct ConvertsTo {
68f4a2713aSLionel Sambuc   operator T();
69f4a2713aSLionel Sambuc };
70f4a2713aSLionel Sambuc 
test_converts_to(ConvertsTo<int> ci,ConvertsTo<int * > cip)71f4a2713aSLionel Sambuc void test_converts_to(ConvertsTo<int> ci, ConvertsTo<int *> cip) {
72f4a2713aSLionel Sambuc   int i = ci;
73f4a2713aSLionel Sambuc   int *ip = cip;
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc 
76f4a2713aSLionel Sambuc // PR4660
77f4a2713aSLionel Sambuc template<class T> struct A0 { operator T*(); };
78f4a2713aSLionel Sambuc template<class T> struct A1;
79f4a2713aSLionel Sambuc 
a(A0<int> & x0,A1<int> & x1)80f4a2713aSLionel Sambuc int *a(A0<int> &x0, A1<int> &x1) {
81f4a2713aSLionel Sambuc   int *y0 = x0;
82f4a2713aSLionel Sambuc   int *y1 = x1; // expected-error{{no viable conversion}}
83f4a2713aSLionel Sambuc }
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc struct X0Base {
86f4a2713aSLionel Sambuc   int &f();
87f4a2713aSLionel Sambuc   int& g(int);
88f4a2713aSLionel Sambuc   static double &g(double);
89f4a2713aSLionel Sambuc };
90f4a2713aSLionel Sambuc 
91f4a2713aSLionel Sambuc template<typename T>
92f4a2713aSLionel Sambuc struct X0 : X0Base {
93f4a2713aSLionel Sambuc };
94f4a2713aSLionel Sambuc 
95f4a2713aSLionel Sambuc template<typename U>
96f4a2713aSLionel Sambuc struct X1 : X0<U> {
f2X197f4a2713aSLionel Sambuc   int &f2() {
98f4a2713aSLionel Sambuc     return X0Base::f();
99f4a2713aSLionel Sambuc   }
100f4a2713aSLionel Sambuc };
101f4a2713aSLionel Sambuc 
test_X1(X1<int> x1i)102f4a2713aSLionel Sambuc void test_X1(X1<int> x1i) {
103f4a2713aSLionel Sambuc   int &ir = x1i.f2();
104f4a2713aSLionel Sambuc }
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc template<typename U>
107f4a2713aSLionel Sambuc struct X2 : X0Base, U {
f2X2108f4a2713aSLionel Sambuc   int &f2() { return X0Base::f(); }
109f4a2713aSLionel Sambuc };
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc template<typename T>
112f4a2713aSLionel Sambuc struct X3 {
testX3113f4a2713aSLionel Sambuc   void test(T x) {
114f4a2713aSLionel Sambuc     double& d1 = X0Base::g(x);
115f4a2713aSLionel Sambuc   }
116f4a2713aSLionel Sambuc };
117f4a2713aSLionel Sambuc 
118f4a2713aSLionel Sambuc 
119f4a2713aSLionel Sambuc template struct X3<double>;
120f4a2713aSLionel Sambuc 
121f4a2713aSLionel Sambuc // Don't try to instantiate this, it's invalid.
122f4a2713aSLionel Sambuc namespace test1 {
123f4a2713aSLionel Sambuc   template <class T> class A {};
124f4a2713aSLionel Sambuc   template <class T> class B {
foo(A<test1::Undeclared> & a)125f4a2713aSLionel Sambuc     void foo(A<test1::Undeclared> &a) // expected-error {{no member named 'Undeclared' in namespace 'test1'}}
126f4a2713aSLionel Sambuc     {}
127f4a2713aSLionel Sambuc   };
128f4a2713aSLionel Sambuc   template class B<int>;
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc namespace PR6947 {
132f4a2713aSLionel Sambuc   template< class T >
133f4a2713aSLionel Sambuc   struct X {
f0PR6947::X134f4a2713aSLionel Sambuc     int f0( )
135f4a2713aSLionel Sambuc     {
136f4a2713aSLionel Sambuc       typedef void ( X::*impl_fun_ptr )( );
137f4a2713aSLionel Sambuc       impl_fun_ptr pImpl = &X::template
138f4a2713aSLionel Sambuc         f0_impl1<int>;
139f4a2713aSLionel Sambuc     }
140f4a2713aSLionel Sambuc   private:
f1PR6947::X141f4a2713aSLionel Sambuc     int f1() {
142f4a2713aSLionel Sambuc     }
143f4a2713aSLionel Sambuc     template< class Processor>
f0_impl1PR6947::X144f4a2713aSLionel Sambuc     void f0_impl1( )
145f4a2713aSLionel Sambuc     {
146f4a2713aSLionel Sambuc     }
147f4a2713aSLionel Sambuc   };
148f4a2713aSLionel Sambuc 
g0()149f4a2713aSLionel Sambuc   char g0() {
150f4a2713aSLionel Sambuc     X<int> pc;
151f4a2713aSLionel Sambuc     pc.f0();
152f4a2713aSLionel Sambuc   }
153f4a2713aSLionel Sambuc 
154f4a2713aSLionel Sambuc }
155f4a2713aSLionel Sambuc 
156f4a2713aSLionel Sambuc namespace PR7022 {
157f4a2713aSLionel Sambuc   template <typename >
158f4a2713aSLionel Sambuc   struct X1
159f4a2713aSLionel Sambuc   {
160f4a2713aSLionel Sambuc     typedef int state_t( );
161f4a2713aSLionel Sambuc     state_t g ;
162f4a2713aSLionel Sambuc   };
163f4a2713aSLionel Sambuc 
164f4a2713aSLionel Sambuc   template <  typename U = X1<int> > struct X2
165f4a2713aSLionel Sambuc   {
X2PR7022::X2166f4a2713aSLionel Sambuc     X2( U = U())
167f4a2713aSLionel Sambuc     {
168f4a2713aSLionel Sambuc     }
169f4a2713aSLionel Sambuc   };
170f4a2713aSLionel Sambuc 
m(void)171f4a2713aSLionel Sambuc   void m(void)
172f4a2713aSLionel Sambuc   {
173f4a2713aSLionel Sambuc     typedef X2<> X2_type;
174f4a2713aSLionel Sambuc     X2_type c;
175f4a2713aSLionel Sambuc   }
176f4a2713aSLionel Sambuc }
177f4a2713aSLionel Sambuc 
178f4a2713aSLionel Sambuc namespace SameSignatureAfterInstantiation {
179f4a2713aSLionel Sambuc   template<typename T> struct S {
180f4a2713aSLionel Sambuc     void f(T *); // expected-note {{previous}}
181*0a6a1f1dSLionel Sambuc     void f(const T*); // expected-error-re {{multiple overloads of 'f' instantiate to the same signature 'void (const int *){{( __attribute__\(\(thiscall\)\))?}}'}}
182f4a2713aSLionel Sambuc   };
183f4a2713aSLionel Sambuc   S<const int> s; // expected-note {{instantiation}}
184f4a2713aSLionel Sambuc }
185*0a6a1f1dSLionel Sambuc 
186*0a6a1f1dSLionel Sambuc namespace PR22040 {
187*0a6a1f1dSLionel Sambuc   template <typename T> struct Foobar {
bazquxPR22040::Foobar188*0a6a1f1dSLionel Sambuc     template <> void bazqux(typename T::type) {}  // expected-error {{cannot specialize a function 'bazqux' within class scope}} expected-error 2{{cannot be used prior to '::' because it has no members}}
189*0a6a1f1dSLionel Sambuc   };
190*0a6a1f1dSLionel Sambuc 
test()191*0a6a1f1dSLionel Sambuc   void test() {
192*0a6a1f1dSLionel Sambuc     // FIXME: we should suppress the "no member" errors
193*0a6a1f1dSLionel Sambuc     Foobar<void>::bazqux();  // expected-error{{no member named 'bazqux' in }}  expected-note{{in instantiation of template class }}
194*0a6a1f1dSLionel Sambuc     Foobar<int>::bazqux();  // expected-error{{no member named 'bazqux' in }}  expected-note{{in instantiation of template class }}
195*0a6a1f1dSLionel Sambuc     Foobar<int>::bazqux(3);  // expected-error{{no member named 'bazqux' in }}
196*0a6a1f1dSLionel Sambuc   }
197*0a6a1f1dSLionel Sambuc }
198*0a6a1f1dSLionel Sambuc 
199*0a6a1f1dSLionel Sambuc template <typename>
200*0a6a1f1dSLionel Sambuc struct SpecializationOfGlobalFnInClassScope {
201*0a6a1f1dSLionel Sambuc   template <>
202*0a6a1f1dSLionel Sambuc   void ::Fn(); // expected-error{{cannot have a qualified name}}
203*0a6a1f1dSLionel Sambuc };
204*0a6a1f1dSLionel Sambuc 
205*0a6a1f1dSLionel Sambuc class AbstractClassWithGlobalFn {
206*0a6a1f1dSLionel Sambuc   template <typename>
207*0a6a1f1dSLionel Sambuc   void ::f(); // expected-error{{cannot have a qualified name}}
208*0a6a1f1dSLionel Sambuc   virtual void f1() = 0;
209*0a6a1f1dSLionel Sambuc };
210