xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/virtual-member-functions.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify %s
2*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple %ms_abi_triple -DMSABI -fsyntax-only -verify %s
3f4a2713aSLionel Sambuc 
4f4a2713aSLionel Sambuc namespace PR5557 {
5f4a2713aSLionel Sambuc template <class T> struct A {
6*0a6a1f1dSLionel Sambuc   A(); // expected-note{{instantiation}}
7f4a2713aSLionel Sambuc   virtual int a(T x);
8f4a2713aSLionel Sambuc };
A()9f4a2713aSLionel Sambuc template<class T> A<T>::A() {}
10f4a2713aSLionel Sambuc 
a(T x)11f4a2713aSLionel Sambuc template<class T> int A<T>::a(T x) {
12f4a2713aSLionel Sambuc   return *x; // expected-error{{requires pointer operand}}
13f4a2713aSLionel Sambuc }
14f4a2713aSLionel Sambuc 
f()15*0a6a1f1dSLionel Sambuc void f() {
16*0a6a1f1dSLionel Sambuc   A<int> x; // expected-note{{instantiation}}
17f4a2713aSLionel Sambuc }
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc template<typename T>
20f4a2713aSLionel Sambuc struct X {
21f4a2713aSLionel Sambuc   virtual void f();
22f4a2713aSLionel Sambuc };
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc template<>
f()25f4a2713aSLionel Sambuc void X<int>::f() { }
26f4a2713aSLionel Sambuc }
27f4a2713aSLionel Sambuc 
28*0a6a1f1dSLionel Sambuc // Like PR5557, but with a defined destructor instead of a defined constructor.
29*0a6a1f1dSLionel Sambuc namespace PR5557_dtor {
30*0a6a1f1dSLionel Sambuc template <class T> struct A {
31*0a6a1f1dSLionel Sambuc   A(); // Don't have an implicit constructor.
32*0a6a1f1dSLionel Sambuc   ~A(); // expected-note{{instantiation}}
33*0a6a1f1dSLionel Sambuc   virtual int a(T x);
34*0a6a1f1dSLionel Sambuc };
~A()35*0a6a1f1dSLionel Sambuc template<class T> A<T>::~A() {}
36*0a6a1f1dSLionel Sambuc 
a(T x)37*0a6a1f1dSLionel Sambuc template<class T> int A<T>::a(T x) {
38*0a6a1f1dSLionel Sambuc   return *x; // expected-error{{requires pointer operand}}
39*0a6a1f1dSLionel Sambuc }
40*0a6a1f1dSLionel Sambuc 
f()41*0a6a1f1dSLionel Sambuc void f() {
42*0a6a1f1dSLionel Sambuc   A<int> x; // expected-note{{instantiation}}
43*0a6a1f1dSLionel Sambuc }
44*0a6a1f1dSLionel Sambuc }
45*0a6a1f1dSLionel Sambuc 
46f4a2713aSLionel Sambuc template<typename T>
47f4a2713aSLionel Sambuc struct Base {
~BaseBase48f4a2713aSLionel Sambuc   virtual ~Base() {
49f4a2713aSLionel Sambuc     int *ptr = 0;
50f4a2713aSLionel Sambuc     T t = ptr; // expected-error{{cannot initialize}}
51f4a2713aSLionel Sambuc   }
52f4a2713aSLionel Sambuc };
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc template<typename T>
55f4a2713aSLionel Sambuc struct Derived : Base<T> {
fooDerived56f4a2713aSLionel Sambuc   virtual void foo() { }
57f4a2713aSLionel Sambuc };
58f4a2713aSLionel Sambuc 
59f4a2713aSLionel Sambuc template struct Derived<int>; // expected-note {{in instantiation of member function 'Base<int>::~Base' requested here}}
60f4a2713aSLionel Sambuc 
61f4a2713aSLionel Sambuc template<typename T>
62f4a2713aSLionel Sambuc struct HasOutOfLineKey {
HasOutOfLineKeyHasOutOfLineKey63*0a6a1f1dSLionel Sambuc   HasOutOfLineKey() { } // expected-note{{in instantiation of member function 'HasOutOfLineKey<int>::f' requested here}}
64f4a2713aSLionel Sambuc   virtual T *f(float *fp);
65f4a2713aSLionel Sambuc };
66f4a2713aSLionel Sambuc 
67f4a2713aSLionel Sambuc template<typename T>
f(float * fp)68f4a2713aSLionel Sambuc T *HasOutOfLineKey<T>::f(float *fp) {
69f4a2713aSLionel Sambuc   return fp; // expected-error{{cannot initialize return object of type 'int *' with an lvalue of type 'float *'}}
70f4a2713aSLionel Sambuc }
71f4a2713aSLionel Sambuc 
72*0a6a1f1dSLionel Sambuc HasOutOfLineKey<int> out_of_line; // expected-note{{in instantiation of member function 'HasOutOfLineKey<int>::HasOutOfLineKey' requested here}}
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc namespace std {
75f4a2713aSLionel Sambuc   class type_info;
76f4a2713aSLionel Sambuc }
77f4a2713aSLionel Sambuc 
78f4a2713aSLionel Sambuc namespace PR7114 {
79f4a2713aSLionel Sambuc   class A { virtual ~A(); }; // expected-note{{declared private here}}
80f4a2713aSLionel Sambuc 
81f4a2713aSLionel Sambuc   template<typename T>
82f4a2713aSLionel Sambuc   class B {
83f4a2713aSLionel Sambuc   public:
84f4a2713aSLionel Sambuc     class Inner : public A { }; // expected-error{{base class 'PR7114::A' has private destructor}}
85f4a2713aSLionel Sambuc     static Inner i;
86f4a2713aSLionel Sambuc     static const unsigned value = sizeof(i) == 4;
87f4a2713aSLionel Sambuc   };
88f4a2713aSLionel Sambuc 
f()89f4a2713aSLionel Sambuc   int f() { return B<int>::value; }
90f4a2713aSLionel Sambuc 
91*0a6a1f1dSLionel Sambuc #ifdef MSABI
test_typeid(B<float>::Inner bfi)92*0a6a1f1dSLionel Sambuc   void test_typeid(B<float>::Inner bfi) { // expected-note{{implicit destructor}}
93*0a6a1f1dSLionel Sambuc     (void)typeid(bfi);
94*0a6a1f1dSLionel Sambuc #else
95f4a2713aSLionel Sambuc   void test_typeid(B<float>::Inner bfi) {
96f4a2713aSLionel Sambuc     (void)typeid(bfi); // expected-note{{implicit destructor}}
97*0a6a1f1dSLionel Sambuc #endif
98f4a2713aSLionel Sambuc   }
99f4a2713aSLionel Sambuc 
100f4a2713aSLionel Sambuc   template<typename T>
101f4a2713aSLionel Sambuc   struct X : A {
102f4a2713aSLionel Sambuc     void f() { }
103f4a2713aSLionel Sambuc   };
104f4a2713aSLionel Sambuc 
105*0a6a1f1dSLionel Sambuc   void test_X(X<int> &xi, X<float> &xf) {
106f4a2713aSLionel Sambuc     xi.f();
107f4a2713aSLionel Sambuc   }
108f4a2713aSLionel Sambuc }
109f4a2713aSLionel Sambuc 
110f4a2713aSLionel Sambuc namespace DynamicCast {
111f4a2713aSLionel Sambuc   struct Y {};
112f4a2713aSLionel Sambuc   template<typename T> struct X : virtual Y {
fooDynamicCast::X113f4a2713aSLionel Sambuc     virtual void foo() { T x; } // expected-error {{variable has incomplete type 'void'}}
114f4a2713aSLionel Sambuc   };
115f4a2713aSLionel Sambuc   template<typename T> struct X2 : virtual Y {
fooDynamicCast::X2116f4a2713aSLionel Sambuc     virtual void foo() { T x; }
117f4a2713aSLionel Sambuc   };
f(X<void> * x)118f4a2713aSLionel Sambuc   Y* f(X<void>* x) { return dynamic_cast<Y*>(x); } // expected-note {{in instantiation of member function 'DynamicCast::X<void>::foo' requested here}}
f2(X<void> * x)119f4a2713aSLionel Sambuc   Y* f2(X<void>* x) { return dynamic_cast<Y*>(x); }
120f4a2713aSLionel Sambuc }
121*0a6a1f1dSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc namespace avoid_using_vtable {
123*0a6a1f1dSLionel Sambuc // We shouldn't emit the vtable for this code, in any ABI.  If we emit the
124*0a6a1f1dSLionel Sambuc // vtable, we emit an implicit virtual dtor, which calls ~RefPtr, which requires
125*0a6a1f1dSLionel Sambuc // a complete type for DeclaredOnly.
126*0a6a1f1dSLionel Sambuc //
127*0a6a1f1dSLionel Sambuc // Previously we would reference the vtable in the MS C++ ABI, even though we
128*0a6a1f1dSLionel Sambuc // don't need to emit either the ctor or the dtor.  In the Itanium C++ ABI, the
129*0a6a1f1dSLionel Sambuc // 'trace' method is the key function, so even though we use the vtable, we
130*0a6a1f1dSLionel Sambuc // don't emit it.
131*0a6a1f1dSLionel Sambuc 
132*0a6a1f1dSLionel Sambuc template <typename T>
133*0a6a1f1dSLionel Sambuc struct RefPtr {
134*0a6a1f1dSLionel Sambuc   T *m_ptr;
~RefPtravoid_using_vtable::RefPtr135*0a6a1f1dSLionel Sambuc   ~RefPtr() { m_ptr->deref(); }
136*0a6a1f1dSLionel Sambuc };
137*0a6a1f1dSLionel Sambuc struct DeclaredOnly;
138*0a6a1f1dSLionel Sambuc struct Base {
139*0a6a1f1dSLionel Sambuc   virtual ~Base();
140*0a6a1f1dSLionel Sambuc };
141*0a6a1f1dSLionel Sambuc 
142*0a6a1f1dSLionel Sambuc struct AvoidVTable : Base {
143*0a6a1f1dSLionel Sambuc   RefPtr<DeclaredOnly> m_insertionStyle;
144*0a6a1f1dSLionel Sambuc   virtual void trace();
145*0a6a1f1dSLionel Sambuc   AvoidVTable();
146*0a6a1f1dSLionel Sambuc };
147*0a6a1f1dSLionel Sambuc // Don't call the dtor, because that will emit an implicit dtor, and require a
148*0a6a1f1dSLionel Sambuc // complete type for DeclaredOnly.
foo()149*0a6a1f1dSLionel Sambuc void foo() { new AvoidVTable; }
150*0a6a1f1dSLionel Sambuc }
151*0a6a1f1dSLionel Sambuc 
152*0a6a1f1dSLionel Sambuc namespace vtable_uses_incomplete {
153*0a6a1f1dSLionel Sambuc // Opposite of the previous test that avoids a vtable, this one tests that we
154*0a6a1f1dSLionel Sambuc // use the vtable when the ctor is defined inline.
155*0a6a1f1dSLionel Sambuc template <typename T>
156*0a6a1f1dSLionel Sambuc struct RefPtr {
157*0a6a1f1dSLionel Sambuc   T *m_ptr;
~RefPtrvtable_uses_incomplete::RefPtr158*0a6a1f1dSLionel Sambuc   ~RefPtr() { m_ptr->deref(); }  // expected-error {{member access into incomplete type 'vtable_uses_incomplete::DeclaredOnly'}}
159*0a6a1f1dSLionel Sambuc };
160*0a6a1f1dSLionel Sambuc struct DeclaredOnly; // expected-note {{forward declaration of 'vtable_uses_incomplete::DeclaredOnly'}}
161*0a6a1f1dSLionel Sambuc struct Base {
162*0a6a1f1dSLionel Sambuc   virtual ~Base();
163*0a6a1f1dSLionel Sambuc };
164*0a6a1f1dSLionel Sambuc 
165*0a6a1f1dSLionel Sambuc struct UsesVTable : Base {
166*0a6a1f1dSLionel Sambuc   RefPtr<DeclaredOnly> m_insertionStyle;
167*0a6a1f1dSLionel Sambuc   virtual void trace();
UsesVTablevtable_uses_incomplete::UsesVTable168*0a6a1f1dSLionel Sambuc   UsesVTable() {} // expected-note {{in instantiation of member function 'vtable_uses_incomplete::RefPtr<vtable_uses_incomplete::DeclaredOnly>::~RefPtr' requested here}}
169*0a6a1f1dSLionel Sambuc };
170*0a6a1f1dSLionel Sambuc }
171