xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/instantiate-complete.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // Tests various places where requiring a complete type involves
4*f4a2713aSLionel Sambuc // instantiation of that type.
5*f4a2713aSLionel Sambuc 
6*f4a2713aSLionel Sambuc template<typename T>
7*f4a2713aSLionel Sambuc struct X {
8*f4a2713aSLionel Sambuc   X(T);
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc   T f; // expected-error{{data member instantiated with function type 'float (int)'}} \
11*f4a2713aSLionel Sambuc        // expected-error{{data member instantiated with function type 'int (int)'}} \
12*f4a2713aSLionel Sambuc        // expected-error{{data member instantiated with function type 'char (char)'}} \
13*f4a2713aSLionel Sambuc        // expected-error{{data member instantiated with function type 'short (short)'}} \
14*f4a2713aSLionel Sambuc        // expected-error{{data member instantiated with function type 'float (float)'}}
15*f4a2713aSLionel Sambuc };
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc X<int> f() { return 0; }
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc struct XField {
20*f4a2713aSLionel Sambuc   X<float(int)> xf; // expected-note{{in instantiation of template class 'X<float (int)>' requested here}}
21*f4a2713aSLionel Sambuc };
22*f4a2713aSLionel Sambuc 
23*f4a2713aSLionel Sambuc void test_subscript(X<double> *ptr1, X<int(int)> *ptr2, int i) {
24*f4a2713aSLionel Sambuc   (void)ptr1[i];
25*f4a2713aSLionel Sambuc   (void)ptr2[i]; // expected-note{{in instantiation of template class 'X<int (int)>' requested here}}
26*f4a2713aSLionel Sambuc }
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc void test_arith(X<signed char> *ptr1, X<unsigned char> *ptr2,
29*f4a2713aSLionel Sambuc                 X<char(char)> *ptr3, X<short(short)> *ptr4) {
30*f4a2713aSLionel Sambuc   (void)(ptr1 + 5);
31*f4a2713aSLionel Sambuc   (void)(5 + ptr2);
32*f4a2713aSLionel Sambuc   (void)(ptr3 + 5); // expected-note{{in instantiation of template class 'X<char (char)>' requested here}}
33*f4a2713aSLionel Sambuc   (void)(5 + ptr4); // expected-note{{in instantiation of template class 'X<short (short)>' requested here}}
34*f4a2713aSLionel Sambuc }
35*f4a2713aSLionel Sambuc 
36*f4a2713aSLionel Sambuc void test_new() {
37*f4a2713aSLionel Sambuc   (void)new X<float>(0);
38*f4a2713aSLionel Sambuc   (void)new X<float(float)>; // expected-note{{in instantiation of template class 'X<float (float)>' requested here}}
39*f4a2713aSLionel Sambuc }
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc void test_memptr(X<long> *p1, long X<long>::*pm1,
42*f4a2713aSLionel Sambuc                  X<long(long)> *p2,
43*f4a2713aSLionel Sambuc                  long (X<long(long)>::*pm2)(long)) {
44*f4a2713aSLionel Sambuc   (void)(p1->*pm1);
45*f4a2713aSLionel Sambuc }
46*f4a2713aSLionel Sambuc 
47*f4a2713aSLionel Sambuc // Reference binding to a base
48*f4a2713aSLionel Sambuc template<typename T>
49*f4a2713aSLionel Sambuc struct X1 { };
50*f4a2713aSLionel Sambuc 
51*f4a2713aSLionel Sambuc template<typename T>
52*f4a2713aSLionel Sambuc struct X2 : public T { };
53*f4a2713aSLionel Sambuc 
54*f4a2713aSLionel Sambuc void refbind_base(X2<X1<int> > &x2) {
55*f4a2713aSLionel Sambuc   X1<int> &x1 = x2;
56*f4a2713aSLionel Sambuc }
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc // Enumerate constructors for user-defined conversion.
59*f4a2713aSLionel Sambuc template<typename T>
60*f4a2713aSLionel Sambuc struct X3 {
61*f4a2713aSLionel Sambuc   X3(T);
62*f4a2713aSLionel Sambuc };
63*f4a2713aSLionel Sambuc 
64*f4a2713aSLionel Sambuc void enum_constructors(X1<float> &x1) {
65*f4a2713aSLionel Sambuc   X3<X1<float> > x3 = x1;
66*f4a2713aSLionel Sambuc }
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc namespace PR6376 {
69*f4a2713aSLionel Sambuc   template<typename T, typename U> struct W { };
70*f4a2713aSLionel Sambuc 
71*f4a2713aSLionel Sambuc   template<typename T>
72*f4a2713aSLionel Sambuc   struct X {
73*f4a2713aSLionel Sambuc     template<typename U>
74*f4a2713aSLionel Sambuc     struct apply {
75*f4a2713aSLionel Sambuc       typedef W<T, U> type;
76*f4a2713aSLionel Sambuc     };
77*f4a2713aSLionel Sambuc   };
78*f4a2713aSLionel Sambuc 
79*f4a2713aSLionel Sambuc   template<typename T, typename U>
80*f4a2713aSLionel Sambuc   struct Y : public X<T>::template apply<U>::type { };
81*f4a2713aSLionel Sambuc 
82*f4a2713aSLionel Sambuc   template struct Y<int, float>;
83*f4a2713aSLionel Sambuc }
84*f4a2713aSLionel Sambuc 
85*f4a2713aSLionel Sambuc namespace TemporaryObjectCopy {
86*f4a2713aSLionel Sambuc   // Make sure we instantiate classes when we create a temporary copy.
87*f4a2713aSLionel Sambuc   template<typename T>
88*f4a2713aSLionel Sambuc   struct X {
89*f4a2713aSLionel Sambuc     X(T);
90*f4a2713aSLionel Sambuc   };
91*f4a2713aSLionel Sambuc 
92*f4a2713aSLionel Sambuc   template<typename T>
93*f4a2713aSLionel Sambuc   void f(T t) {
94*f4a2713aSLionel Sambuc     const X<int> &x = X<int>(t);
95*f4a2713aSLionel Sambuc   }
96*f4a2713aSLionel Sambuc 
97*f4a2713aSLionel Sambuc   template void f(int);
98*f4a2713aSLionel Sambuc }
99*f4a2713aSLionel Sambuc 
100*f4a2713aSLionel Sambuc namespace PR7080 {
101*f4a2713aSLionel Sambuc   template <class T, class U>
102*f4a2713aSLionel Sambuc   class X
103*f4a2713aSLionel Sambuc   {
104*f4a2713aSLionel Sambuc     typedef char true_t;
105*f4a2713aSLionel Sambuc     class false_t { char dummy[2]; };
106*f4a2713aSLionel Sambuc     static true_t dispatch(U);
107*f4a2713aSLionel Sambuc     static false_t dispatch(...);
108*f4a2713aSLionel Sambuc     static T trigger();
109*f4a2713aSLionel Sambuc   public:
110*f4a2713aSLionel Sambuc     enum { value = sizeof(dispatch(trigger())) == sizeof(true_t) };
111*f4a2713aSLionel Sambuc   };
112*f4a2713aSLionel Sambuc 
113*f4a2713aSLionel Sambuc   template <class T>
114*f4a2713aSLionel Sambuc   class rv : public T
115*f4a2713aSLionel Sambuc   { };
116*f4a2713aSLionel Sambuc 
117*f4a2713aSLionel Sambuc   bool x = X<int, rv<int>&>::value;
118*f4a2713aSLionel Sambuc }
119*f4a2713aSLionel Sambuc 
120*f4a2713aSLionel Sambuc namespace pr7199 {
121*f4a2713aSLionel Sambuc   template <class T> class A; // expected-note {{template is declared here}}
122*f4a2713aSLionel Sambuc   template <class T> class B {
123*f4a2713aSLionel Sambuc     class A<T>::C field; // expected-error {{implicit instantiation of undefined template 'pr7199::A<int>'}}
124*f4a2713aSLionel Sambuc   };
125*f4a2713aSLionel Sambuc 
126*f4a2713aSLionel Sambuc   template class B<int>; // expected-note {{in instantiation}}
127*f4a2713aSLionel Sambuc }
128*f4a2713aSLionel Sambuc 
129*f4a2713aSLionel Sambuc namespace PR8425 {
130*f4a2713aSLionel Sambuc   template <typename T>
131*f4a2713aSLionel Sambuc   class BaseT {};
132*f4a2713aSLionel Sambuc 
133*f4a2713aSLionel Sambuc   template <typename T>
134*f4a2713aSLionel Sambuc   class DerivedT : public BaseT<T> {};
135*f4a2713aSLionel Sambuc 
136*f4a2713aSLionel Sambuc   template <typename T>
137*f4a2713aSLionel Sambuc   class FromT {
138*f4a2713aSLionel Sambuc   public:
139*f4a2713aSLionel Sambuc     operator DerivedT<T>() const { return DerivedT<T>(); }
140*f4a2713aSLionel Sambuc   };
141*f4a2713aSLionel Sambuc 
142*f4a2713aSLionel Sambuc   void test() {
143*f4a2713aSLionel Sambuc     FromT<int> ft;
144*f4a2713aSLionel Sambuc     BaseT<int> bt(ft);
145*f4a2713aSLionel Sambuc   }
146*f4a2713aSLionel Sambuc }
147