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