xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/instantiate-function-1.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc template<typename T, typename U>
3*f4a2713aSLionel Sambuc struct X0 {
fX04*f4a2713aSLionel Sambuc   void f(T x, U y) {
5*f4a2713aSLionel Sambuc     (void)(x + y); // expected-error{{invalid operands}}
6*f4a2713aSLionel Sambuc   }
7*f4a2713aSLionel Sambuc };
8*f4a2713aSLionel Sambuc 
9*f4a2713aSLionel Sambuc struct X1 { };
10*f4a2713aSLionel Sambuc 
11*f4a2713aSLionel Sambuc template struct X0<int, float>;
12*f4a2713aSLionel Sambuc template struct X0<int*, int>;
13*f4a2713aSLionel Sambuc template struct X0<int X1::*, int>; // expected-note{{instantiation of}}
14*f4a2713aSLionel Sambuc 
15*f4a2713aSLionel Sambuc template<typename T>
16*f4a2713aSLionel Sambuc struct X2 {
17*f4a2713aSLionel Sambuc   void f(T);
18*f4a2713aSLionel Sambuc 
gX219*f4a2713aSLionel Sambuc   T g(T x, T y) {
20*f4a2713aSLionel Sambuc     /* DeclStmt */;
21*f4a2713aSLionel Sambuc     T *xp = &x, &yr = y; // expected-error{{pointer to a reference}}
22*f4a2713aSLionel Sambuc     /* NullStmt */;
23*f4a2713aSLionel Sambuc   }
24*f4a2713aSLionel Sambuc };
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc template struct X2<int>;
27*f4a2713aSLionel Sambuc template struct X2<int&>; // expected-note{{instantiation of}}
28*f4a2713aSLionel Sambuc 
29*f4a2713aSLionel Sambuc template<typename T>
30*f4a2713aSLionel Sambuc struct X3 {
fX331*f4a2713aSLionel Sambuc   void f(T) {
32*f4a2713aSLionel Sambuc     Label:
33*f4a2713aSLionel Sambuc     T x;
34*f4a2713aSLionel Sambuc     goto Label;
35*f4a2713aSLionel Sambuc   }
36*f4a2713aSLionel Sambuc };
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc template struct X3<int>;
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc template <typename T> struct X4 {
fX441*f4a2713aSLionel Sambuc   T f() const {
42*f4a2713aSLionel Sambuc     return; // expected-error{{non-void function 'f' should return a value}}
43*f4a2713aSLionel Sambuc   }
44*f4a2713aSLionel Sambuc 
gX445*f4a2713aSLionel Sambuc   T g() const {
46*f4a2713aSLionel Sambuc     return 1; // expected-error{{void function 'g' should not return a value}}
47*f4a2713aSLionel Sambuc   }
48*f4a2713aSLionel Sambuc };
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc template struct X4<void>; // expected-note{{in instantiation of}}
51*f4a2713aSLionel Sambuc template struct X4<int>; // expected-note{{in instantiation of}}
52*f4a2713aSLionel Sambuc 
53*f4a2713aSLionel Sambuc struct Incomplete; // expected-note 2{{forward declaration}}
54*f4a2713aSLionel Sambuc 
55*f4a2713aSLionel Sambuc template<typename T> struct X5 {
fX556*f4a2713aSLionel Sambuc   T f() { } // expected-error{{incomplete result type}}
57*f4a2713aSLionel Sambuc };
58*f4a2713aSLionel Sambuc void test_X5(X5<Incomplete> x5); // okay!
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc template struct X5<Incomplete>; // expected-note{{instantiation}}
61*f4a2713aSLionel Sambuc 
62*f4a2713aSLionel Sambuc template<typename T, typename U, typename V> struct X6 {
fX663*f4a2713aSLionel Sambuc   U f(T t, U u, V v) {
64*f4a2713aSLionel Sambuc     // IfStmt
65*f4a2713aSLionel Sambuc     if (t > 0)
66*f4a2713aSLionel Sambuc       return u;
67*f4a2713aSLionel Sambuc     else {
68*f4a2713aSLionel Sambuc       if (t < 0)
69*f4a2713aSLionel Sambuc         return v; // expected-error{{cannot initialize return object of type}}
70*f4a2713aSLionel Sambuc     }
71*f4a2713aSLionel Sambuc 
72*f4a2713aSLionel Sambuc     if (T x = t) {
73*f4a2713aSLionel Sambuc       t = x;
74*f4a2713aSLionel Sambuc     }
75*f4a2713aSLionel Sambuc     return v; // expected-error{{cannot initialize return object of type}}
76*f4a2713aSLionel Sambuc   }
77*f4a2713aSLionel Sambuc };
78*f4a2713aSLionel Sambuc 
79*f4a2713aSLionel Sambuc struct ConvertibleToInt {
80*f4a2713aSLionel Sambuc   operator int() const;
81*f4a2713aSLionel Sambuc };
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc template struct X6<ConvertibleToInt, float, char>;
84*f4a2713aSLionel Sambuc template struct X6<bool, int, int*>; // expected-note{{instantiation}}
85*f4a2713aSLionel Sambuc 
86*f4a2713aSLionel Sambuc template <typename T> struct X7 {
fX787*f4a2713aSLionel Sambuc   void f() {
88*f4a2713aSLionel Sambuc     void *v = this;
89*f4a2713aSLionel Sambuc   }
90*f4a2713aSLionel Sambuc };
91*f4a2713aSLionel Sambuc 
92*f4a2713aSLionel Sambuc template struct X7<int>;
93*f4a2713aSLionel Sambuc 
94*f4a2713aSLionel Sambuc template<typename T> struct While0 {
fWhile095*f4a2713aSLionel Sambuc   void f(T t) {
96*f4a2713aSLionel Sambuc     while (t) {
97*f4a2713aSLionel Sambuc     }
98*f4a2713aSLionel Sambuc 
99*f4a2713aSLionel Sambuc     while (T t2 = T()) ;
100*f4a2713aSLionel Sambuc   }
101*f4a2713aSLionel Sambuc };
102*f4a2713aSLionel Sambuc 
103*f4a2713aSLionel Sambuc template struct While0<float>;
104*f4a2713aSLionel Sambuc 
105*f4a2713aSLionel Sambuc template<typename T> struct Do0 {
fDo0106*f4a2713aSLionel Sambuc   void f(T t) {
107*f4a2713aSLionel Sambuc     do {
108*f4a2713aSLionel Sambuc     } while (t); // expected-error{{not contextually}}
109*f4a2713aSLionel Sambuc   }
110*f4a2713aSLionel Sambuc };
111*f4a2713aSLionel Sambuc 
112*f4a2713aSLionel Sambuc struct NotConvertibleToBool { };
113*f4a2713aSLionel Sambuc template struct Do0<ConvertibleToInt>;
114*f4a2713aSLionel Sambuc template struct Do0<NotConvertibleToBool>; // expected-note{{instantiation}}
115*f4a2713aSLionel Sambuc 
116*f4a2713aSLionel Sambuc template<typename T> struct For0 {
fFor0117*f4a2713aSLionel Sambuc   void f(T f, T l) {
118*f4a2713aSLionel Sambuc     for (; f != l; ++f) {
119*f4a2713aSLionel Sambuc       if (*f)
120*f4a2713aSLionel Sambuc         continue;
121*f4a2713aSLionel Sambuc       else if (*f == 17)
122*f4a2713aSLionel Sambuc         break;
123*f4a2713aSLionel Sambuc     }
124*f4a2713aSLionel Sambuc   }
125*f4a2713aSLionel Sambuc };
126*f4a2713aSLionel Sambuc 
127*f4a2713aSLionel Sambuc template struct For0<int*>;
128*f4a2713aSLionel Sambuc 
129*f4a2713aSLionel Sambuc template<typename T> struct Member0 {
fMember0130*f4a2713aSLionel Sambuc   void f(T t) {
131*f4a2713aSLionel Sambuc     t;
132*f4a2713aSLionel Sambuc     t.f;
133*f4a2713aSLionel Sambuc     t->f;
134*f4a2713aSLionel Sambuc 
135*f4a2713aSLionel Sambuc     T* tp;
136*f4a2713aSLionel Sambuc     tp.f; // expected-error{{member reference base type 'T *' is not a structure or union}}
137*f4a2713aSLionel Sambuc     tp->f;
138*f4a2713aSLionel Sambuc 
139*f4a2713aSLionel Sambuc     this->f;
140*f4a2713aSLionel Sambuc     this.f; // expected-error{{member reference base type 'Member0<T> *' is not a structure or union}}
141*f4a2713aSLionel Sambuc   }
142*f4a2713aSLionel Sambuc };
143*f4a2713aSLionel Sambuc 
144*f4a2713aSLionel Sambuc template<typename T, typename U> struct Switch0 {
fSwitch0145*f4a2713aSLionel Sambuc   U f(T value, U v0, U v1, U v2) {
146*f4a2713aSLionel Sambuc     switch (value) {
147*f4a2713aSLionel Sambuc     case 0: return v0;
148*f4a2713aSLionel Sambuc 
149*f4a2713aSLionel Sambuc     case 1: return v1;
150*f4a2713aSLionel Sambuc 
151*f4a2713aSLionel Sambuc     case 2: // fall through
152*f4a2713aSLionel Sambuc 
153*f4a2713aSLionel Sambuc     default:
154*f4a2713aSLionel Sambuc       return  v2;
155*f4a2713aSLionel Sambuc     }
156*f4a2713aSLionel Sambuc   }
157*f4a2713aSLionel Sambuc };
158*f4a2713aSLionel Sambuc 
159*f4a2713aSLionel Sambuc template struct Switch0<int, float>;
160*f4a2713aSLionel Sambuc 
161*f4a2713aSLionel Sambuc template<typename T, int I1, int I2> struct Switch1 {
fSwitch1162*f4a2713aSLionel Sambuc   T f(T x, T y, T z) {
163*f4a2713aSLionel Sambuc     switch (x) {
164*f4a2713aSLionel Sambuc     case I1: return y; // expected-note{{previous}}
165*f4a2713aSLionel Sambuc     case I2: return z; // expected-error{{duplicate}}
166*f4a2713aSLionel Sambuc     default: return x;
167*f4a2713aSLionel Sambuc     }
168*f4a2713aSLionel Sambuc   }
169*f4a2713aSLionel Sambuc };
170*f4a2713aSLionel Sambuc 
171*f4a2713aSLionel Sambuc template struct Switch1<int, 1, 2>;
172*f4a2713aSLionel Sambuc template struct Switch1<int, 2, 2>; // expected-note{{instantiation}}
173*f4a2713aSLionel Sambuc 
174*f4a2713aSLionel Sambuc template<typename T> struct IndirectGoto0 {
fIndirectGoto0175*f4a2713aSLionel Sambuc   void f(T x) {
176*f4a2713aSLionel Sambuc     // FIXME: crummy error message below
177*f4a2713aSLionel Sambuc     goto *x; // expected-error{{incompatible}}
178*f4a2713aSLionel Sambuc 
179*f4a2713aSLionel Sambuc   prior:
180*f4a2713aSLionel Sambuc     T prior_label;
181*f4a2713aSLionel Sambuc     prior_label = &&prior; // expected-error{{assigning to 'int'}}
182*f4a2713aSLionel Sambuc 
183*f4a2713aSLionel Sambuc     T later_label;
184*f4a2713aSLionel Sambuc     later_label = &&later; // expected-error{{assigning to 'int'}}
185*f4a2713aSLionel Sambuc 
186*f4a2713aSLionel Sambuc   later:
187*f4a2713aSLionel Sambuc     (void)(1+1);
188*f4a2713aSLionel Sambuc   }
189*f4a2713aSLionel Sambuc };
190*f4a2713aSLionel Sambuc 
191*f4a2713aSLionel Sambuc template struct IndirectGoto0<void*>;
192*f4a2713aSLionel Sambuc template struct IndirectGoto0<int>; // expected-note{{instantiation}}
193*f4a2713aSLionel Sambuc 
194*f4a2713aSLionel Sambuc template<typename T> struct TryCatch0 {
fTryCatch0195*f4a2713aSLionel Sambuc   void f() {
196*f4a2713aSLionel Sambuc     try {
197*f4a2713aSLionel Sambuc     } catch (T t) { // expected-error{{incomplete type}} \
198*f4a2713aSLionel Sambuc                     // expected-error{{abstract class}}
199*f4a2713aSLionel Sambuc     } catch (...) {
200*f4a2713aSLionel Sambuc     }
201*f4a2713aSLionel Sambuc   }
202*f4a2713aSLionel Sambuc };
203*f4a2713aSLionel Sambuc 
204*f4a2713aSLionel Sambuc struct Abstract {
205*f4a2713aSLionel Sambuc   virtual void foo() = 0; // expected-note{{pure virtual}}
206*f4a2713aSLionel Sambuc };
207*f4a2713aSLionel Sambuc 
208*f4a2713aSLionel Sambuc template struct TryCatch0<int>; // okay
209*f4a2713aSLionel Sambuc template struct TryCatch0<Incomplete*>; // expected-note{{instantiation}}
210*f4a2713aSLionel Sambuc template struct TryCatch0<Abstract>; // expected-note{{instantiation}}
211*f4a2713aSLionel Sambuc 
212*f4a2713aSLionel Sambuc // PR4383
213*f4a2713aSLionel Sambuc template<typename T> struct X;
214*f4a2713aSLionel Sambuc template<typename T> struct Y : public X<T> {
xY215*f4a2713aSLionel Sambuc   Y& x() { return *this; }
216*f4a2713aSLionel Sambuc };
217*f4a2713aSLionel Sambuc 
218*f4a2713aSLionel Sambuc // Make sure our assertions don't get too uppity.
219*f4a2713aSLionel Sambuc namespace test0 {
220*f4a2713aSLionel Sambuc   template <class T> class A { void foo(T array[10]); };
221*f4a2713aSLionel Sambuc   template class A<int>;
222*f4a2713aSLionel Sambuc }
223*f4a2713aSLionel Sambuc 
224*f4a2713aSLionel Sambuc namespace PR7016 {
f()225*f4a2713aSLionel Sambuc   template<typename T> void f() { T x = x; }
226*f4a2713aSLionel Sambuc   template void f<int>();
227*f4a2713aSLionel Sambuc }
228*f4a2713aSLionel Sambuc 
229*f4a2713aSLionel Sambuc namespace PR9880 {
230*f4a2713aSLionel Sambuc   struct lua_State;
231*f4a2713aSLionel Sambuc   struct no_tag { char a; };			// (A)
232*f4a2713aSLionel Sambuc   struct yes_tag { long a; long b; };	// (A)
233*f4a2713aSLionel Sambuc 
234*f4a2713aSLionel Sambuc   template <typename T>
235*f4a2713aSLionel Sambuc   struct HasIndexMetamethod {
236*f4a2713aSLionel Sambuc     template <typename U>
237*f4a2713aSLionel Sambuc     static no_tag check(...);
238*f4a2713aSLionel Sambuc     template <typename U>
239*f4a2713aSLionel Sambuc     static yes_tag check(char[sizeof(&U::luaIndex)]);
240*f4a2713aSLionel Sambuc     enum { value = sizeof(check<T>(0)) == sizeof(yes_tag) };
241*f4a2713aSLionel Sambuc   };
242*f4a2713aSLionel Sambuc 
243*f4a2713aSLionel Sambuc   class SomeClass {
244*f4a2713aSLionel Sambuc   public:
245*f4a2713aSLionel Sambuc     int luaIndex(lua_State* L);
246*f4a2713aSLionel Sambuc   };
247*f4a2713aSLionel Sambuc 
248*f4a2713aSLionel Sambuc   int i = HasIndexMetamethod<SomeClass>::value;
249*f4a2713aSLionel Sambuc }
250