xref: /minix3/external/bsd/llvm/dist/clang/test/CXX/class.access/p4.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc // C++0x [class.access]p4:
4f4a2713aSLionel Sambuc 
5f4a2713aSLionel Sambuc //   Access control is applied uniformly to all names, whether the
6f4a2713aSLionel Sambuc //   names are referred to from declarations or expressions.  In the
7f4a2713aSLionel Sambuc //   case of overloaded function names, access control is applied to
8f4a2713aSLionel Sambuc //   the function selected by overload resolution.
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc class Public {} PublicInst;
11f4a2713aSLionel Sambuc class Protected {} ProtectedInst;
12f4a2713aSLionel Sambuc class Private {} PrivateInst;
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc namespace test0 {
15f4a2713aSLionel Sambuc   class A {
16f4a2713aSLionel Sambuc   public:
17f4a2713aSLionel Sambuc     void foo(Public&);
18f4a2713aSLionel Sambuc   protected:
19f4a2713aSLionel Sambuc     void foo(Protected&); // expected-note 2 {{declared protected here}}
20f4a2713aSLionel Sambuc   private:
21f4a2713aSLionel Sambuc     void foo(Private&); // expected-note 2 {{declared private here}}
22f4a2713aSLionel Sambuc   };
23f4a2713aSLionel Sambuc 
test(A * op)24f4a2713aSLionel Sambuc   void test(A *op) {
25f4a2713aSLionel Sambuc     op->foo(PublicInst);
26f4a2713aSLionel Sambuc     op->foo(ProtectedInst); // expected-error {{'foo' is a protected member}}
27f4a2713aSLionel Sambuc     op->foo(PrivateInst); // expected-error {{'foo' is a private member}}
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc     void (A::*a)(Public&) = &A::foo;
30f4a2713aSLionel Sambuc     void (A::*b)(Protected&) = &A::foo; // expected-error {{'foo' is a protected member}}
31f4a2713aSLionel Sambuc     void (A::*c)(Private&) = &A::foo; // expected-error {{'foo' is a private member}}
32f4a2713aSLionel Sambuc   }
33f4a2713aSLionel Sambuc }
34f4a2713aSLionel Sambuc 
35f4a2713aSLionel Sambuc // Member operators.
36f4a2713aSLionel Sambuc namespace test1 {
37f4a2713aSLionel Sambuc   class A {
38f4a2713aSLionel Sambuc   public:
39f4a2713aSLionel Sambuc     void operator+(Public&);
40f4a2713aSLionel Sambuc     void operator[](Public&);
41f4a2713aSLionel Sambuc     void operator()(Public&);
42f4a2713aSLionel Sambuc     typedef void (*PublicSurrogate)(Public&);
43f4a2713aSLionel Sambuc     operator PublicSurrogate() const;
44f4a2713aSLionel Sambuc   protected:
45f4a2713aSLionel Sambuc     void operator+(Protected&); // expected-note {{declared protected here}}
46f4a2713aSLionel Sambuc     void operator[](Protected&); // expected-note {{declared protected here}}
47f4a2713aSLionel Sambuc     void operator()(Protected&); // expected-note {{declared protected here}}
48f4a2713aSLionel Sambuc     typedef void (*ProtectedSurrogate)(Protected&);
49f4a2713aSLionel Sambuc     operator ProtectedSurrogate() const; // expected-note {{declared protected here}}
50f4a2713aSLionel Sambuc   private:
51f4a2713aSLionel Sambuc     void operator+(Private&); // expected-note {{declared private here}}
52f4a2713aSLionel Sambuc     void operator[](Private&); // expected-note {{declared private here}}
53f4a2713aSLionel Sambuc     void operator()(Private&); // expected-note {{declared private here}}
54f4a2713aSLionel Sambuc     void operator-(); // expected-note {{declared private here}}
55f4a2713aSLionel Sambuc     typedef void (*PrivateSurrogate)(Private&);
56f4a2713aSLionel Sambuc     operator PrivateSurrogate() const; // expected-note {{declared private here}}
57f4a2713aSLionel Sambuc   };
58f4a2713aSLionel Sambuc   void operator+(const A &, Public&);
59f4a2713aSLionel Sambuc   void operator+(const A &, Protected&);
60f4a2713aSLionel Sambuc   void operator+(const A &, Private&);
61f4a2713aSLionel Sambuc   void operator-(const A &);
62f4a2713aSLionel Sambuc 
test(A & a,Public & pub,Protected & prot,Private & priv)63f4a2713aSLionel Sambuc   void test(A &a, Public &pub, Protected &prot, Private &priv) {
64f4a2713aSLionel Sambuc     a + pub;
65f4a2713aSLionel Sambuc     a + prot; // expected-error {{'operator+' is a protected member}}
66f4a2713aSLionel Sambuc     a + priv; // expected-error {{'operator+' is a private member}}
67f4a2713aSLionel Sambuc     a[pub];
68f4a2713aSLionel Sambuc     a[prot]; // expected-error {{'operator[]' is a protected member}}
69f4a2713aSLionel Sambuc     a[priv]; // expected-error {{'operator[]' is a private member}}
70f4a2713aSLionel Sambuc     a(pub);
71f4a2713aSLionel Sambuc     a(prot); // expected-error {{'operator()' is a protected member}}
72f4a2713aSLionel Sambuc     a(priv); // expected-error {{'operator()' is a private member}}
73f4a2713aSLionel Sambuc     -a;       // expected-error {{'operator-' is a private member}}
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc     const A &ca = a;
76f4a2713aSLionel Sambuc     ca + pub;
77f4a2713aSLionel Sambuc     ca + prot;
78f4a2713aSLionel Sambuc     ca + priv;
79f4a2713aSLionel Sambuc     -ca;
80f4a2713aSLionel Sambuc     // These are all surrogate calls
81f4a2713aSLionel Sambuc     ca(pub);
82*0a6a1f1dSLionel Sambuc     ca(prot); // expected-error {{'operator void (*)(Protected &)' is a protected member}}
83*0a6a1f1dSLionel Sambuc     ca(priv); // expected-error {{'operator void (*)(Private &)' is a private member}}
84f4a2713aSLionel Sambuc   }
85f4a2713aSLionel Sambuc }
86f4a2713aSLionel Sambuc 
87f4a2713aSLionel Sambuc // Implicit constructor calls.
88f4a2713aSLionel Sambuc namespace test2 {
89f4a2713aSLionel Sambuc   class A {
90f4a2713aSLionel Sambuc   private:
91f4a2713aSLionel Sambuc     A(); // expected-note 3 {{declared private here}}
92f4a2713aSLionel Sambuc 
93f4a2713aSLionel Sambuc     static A foo;
94f4a2713aSLionel Sambuc   };
95f4a2713aSLionel Sambuc 
96f4a2713aSLionel Sambuc   A a; // expected-error {{calling a private constructor}}
97f4a2713aSLionel Sambuc   A A::foo; // okay
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc   class B : A { }; // expected-error {{base class 'test2::A' has private default constructor}}
100f4a2713aSLionel Sambuc   B b; // expected-note{{implicit default constructor}}
101f4a2713aSLionel Sambuc 
102f4a2713aSLionel Sambuc   class C : virtual A {
103f4a2713aSLionel Sambuc   public:
104f4a2713aSLionel Sambuc     C();
105f4a2713aSLionel Sambuc   };
106f4a2713aSLionel Sambuc 
107f4a2713aSLionel Sambuc   class D : C { }; // expected-error {{inherited virtual base class 'test2::A' has private default constructor}}
108f4a2713aSLionel Sambuc   D d; // expected-note{{implicit default constructor}}
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc // Implicit destructor calls.
112f4a2713aSLionel Sambuc namespace test3 {
113f4a2713aSLionel Sambuc   class A {
114f4a2713aSLionel Sambuc   private:
115f4a2713aSLionel Sambuc     ~A(); // expected-note 2 {{declared private here}}
116f4a2713aSLionel Sambuc     static A foo;
117f4a2713aSLionel Sambuc   };
118f4a2713aSLionel Sambuc 
119f4a2713aSLionel Sambuc   A a; // expected-error {{variable of type 'test3::A' has private destructor}}
120f4a2713aSLionel Sambuc   A A::foo;
121f4a2713aSLionel Sambuc 
foo(A param)122f4a2713aSLionel Sambuc   void foo(A param) { // okay
123f4a2713aSLionel Sambuc     A local; // expected-error {{variable of type 'test3::A' has private destructor}}
124f4a2713aSLionel Sambuc   }
125f4a2713aSLionel Sambuc 
126f4a2713aSLionel Sambuc   template <unsigned N> class Base { ~Base(); }; // expected-note 14 {{declared private here}}
127f4a2713aSLionel Sambuc   class Base2 : virtual Base<2> { ~Base2(); }; // expected-note 3 {{declared private here}} \
128f4a2713aSLionel Sambuc                                                // expected-error {{base class 'Base<2>' has private destructor}}
129f4a2713aSLionel Sambuc   class Base3 : virtual Base<3> { public: ~Base3(); }; // expected-error {{base class 'Base<3>' has private destructor}}
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc   // These don't cause diagnostics because we don't need the destructor.
132f4a2713aSLionel Sambuc   class Derived0 : Base<0> { ~Derived0(); };
133f4a2713aSLionel Sambuc   class Derived1 : Base<1> { };
134f4a2713aSLionel Sambuc 
135f4a2713aSLionel Sambuc   class Derived2 : // expected-error {{inherited virtual base class 'Base<2>' has private destructor}} \
136f4a2713aSLionel Sambuc                    // expected-error {{inherited virtual base class 'Base<3>' has private destructor}}
137f4a2713aSLionel Sambuc     Base<0>,  // expected-error {{base class 'Base<0>' has private destructor}}
138f4a2713aSLionel Sambuc     virtual Base<1>, // expected-error {{base class 'Base<1>' has private destructor}}
139f4a2713aSLionel Sambuc     Base2, // expected-error {{base class 'test3::Base2' has private destructor}}
140f4a2713aSLionel Sambuc     virtual Base3
141f4a2713aSLionel Sambuc   {
~Derived2()142f4a2713aSLionel Sambuc     ~Derived2() {}
143f4a2713aSLionel Sambuc   };
144f4a2713aSLionel Sambuc 
145f4a2713aSLionel Sambuc   class Derived3 : // expected-error 2 {{inherited virtual base class 'Base<2>' has private destructor}} \
146f4a2713aSLionel Sambuc                    // expected-error 2 {{inherited virtual base class 'Base<3>' has private destructor}} \
147f4a2713aSLionel Sambuc     // expected-note 2{{implicit default constructor}}
148f4a2713aSLionel Sambuc     Base<0>,  // expected-error 2 {{base class 'Base<0>' has private destructor}}
149f4a2713aSLionel Sambuc     virtual Base<1>, // expected-error 2 {{base class 'Base<1>' has private destructor}}
150f4a2713aSLionel Sambuc     Base2, // expected-error 2 {{base class 'test3::Base2' has private destructor}}
151f4a2713aSLionel Sambuc     virtual Base3
152f4a2713aSLionel Sambuc   {};
153f4a2713aSLionel Sambuc   Derived3 d3; // expected-note {{implicit default constructor}}\
154f4a2713aSLionel Sambuc                // expected-note{{implicit destructor}}}
155f4a2713aSLionel Sambuc }
156f4a2713aSLionel Sambuc 
157f4a2713aSLionel Sambuc // Conversion functions.
158f4a2713aSLionel Sambuc namespace test4 {
159f4a2713aSLionel Sambuc   class Base {
160f4a2713aSLionel Sambuc   private:
161f4a2713aSLionel Sambuc     operator Private(); // expected-note 4 {{declared private here}}
162f4a2713aSLionel Sambuc   public:
163f4a2713aSLionel Sambuc     operator Public(); // expected-note 2{{member is declared here}}
164f4a2713aSLionel Sambuc   };
165f4a2713aSLionel Sambuc 
166f4a2713aSLionel Sambuc   class Derived1 : private Base { // expected-note 2 {{declared private here}} \
167f4a2713aSLionel Sambuc                                   // expected-note {{constrained by private inheritance}}
test1()168f4a2713aSLionel Sambuc     Private test1() { return *this; } // expected-error {{'operator Private' is a private member}}
test2()169f4a2713aSLionel Sambuc     Public test2() { return *this; }
170f4a2713aSLionel Sambuc   };
test1(Derived1 & d)171f4a2713aSLionel Sambuc   Private test1(Derived1 &d) { return d; } // expected-error {{'operator Private' is a private member}} \
172f4a2713aSLionel Sambuc                                            // expected-error {{cannot cast 'test4::Derived1' to its private base class}}
test2(Derived1 & d)173f4a2713aSLionel Sambuc   Public test2(Derived1 &d) { return d; } // expected-error {{cannot cast 'test4::Derived1' to its private base class}} \
174f4a2713aSLionel Sambuc                                           // expected-error {{'operator Public' is a private member}}
175f4a2713aSLionel Sambuc 
176f4a2713aSLionel Sambuc 
177f4a2713aSLionel Sambuc   class Derived2 : public Base {
test1()178f4a2713aSLionel Sambuc     Private test1() { return *this; } // expected-error {{'operator Private' is a private member}}
test2()179f4a2713aSLionel Sambuc     Public test2() { return *this; }
180f4a2713aSLionel Sambuc   };
test1(Derived2 & d)181f4a2713aSLionel Sambuc   Private test1(Derived2 &d) { return d; } // expected-error {{'operator Private' is a private member}}
test2(Derived2 & d)182f4a2713aSLionel Sambuc   Public test2(Derived2 &d) { return d; }
183f4a2713aSLionel Sambuc 
184f4a2713aSLionel Sambuc   class Derived3 : private Base { // expected-note {{constrained by private inheritance here}} \
185f4a2713aSLionel Sambuc                                   // expected-note {{declared private here}}
186f4a2713aSLionel Sambuc   public:
187f4a2713aSLionel Sambuc     operator Private();
188f4a2713aSLionel Sambuc   };
test1(Derived3 & d)189f4a2713aSLionel Sambuc   Private test1(Derived3 &d) { return d; }
test2(Derived3 & d)190f4a2713aSLionel Sambuc   Public test2(Derived3 &d) { return d; } // expected-error {{'operator Public' is a private member of 'test4::Base'}} \
191f4a2713aSLionel Sambuc                                           // expected-error {{cannot cast 'test4::Derived3' to its private base class}}
192f4a2713aSLionel Sambuc 
193f4a2713aSLionel Sambuc   class Derived4 : public Base {
194f4a2713aSLionel Sambuc   public:
195f4a2713aSLionel Sambuc     operator Private();
196f4a2713aSLionel Sambuc   };
test1(Derived4 & d)197f4a2713aSLionel Sambuc   Private test1(Derived4 &d) { return d; }
test2(Derived4 & d)198f4a2713aSLionel Sambuc   Public test2(Derived4 &d) { return d; }
199f4a2713aSLionel Sambuc }
200f4a2713aSLionel Sambuc 
201f4a2713aSLionel Sambuc // Implicit copy assignment operator uses.
202f4a2713aSLionel Sambuc namespace test5 {
203f4a2713aSLionel Sambuc   class A {
204f4a2713aSLionel Sambuc     void operator=(const A &); // expected-note 2 {{implicitly declared private here}}
205f4a2713aSLionel Sambuc   };
206f4a2713aSLionel Sambuc 
207f4a2713aSLionel Sambuc   class Test1 { A a; }; // expected-error {{private member}}
test1()208f4a2713aSLionel Sambuc   void test1() {
209f4a2713aSLionel Sambuc     Test1 a;
210f4a2713aSLionel Sambuc     a = Test1(); // expected-note{{implicit copy}}
211f4a2713aSLionel Sambuc   }
212f4a2713aSLionel Sambuc 
213f4a2713aSLionel Sambuc   class Test2 : A {}; // expected-error {{private member}}
test2()214f4a2713aSLionel Sambuc   void test2() {
215f4a2713aSLionel Sambuc     Test2 a;
216f4a2713aSLionel Sambuc     a = Test2(); // expected-note{{implicit copy}}
217f4a2713aSLionel Sambuc   }
218f4a2713aSLionel Sambuc }
219f4a2713aSLionel Sambuc 
220f4a2713aSLionel Sambuc // Implicit copy constructor uses.
221f4a2713aSLionel Sambuc namespace test6 {
222f4a2713aSLionel Sambuc   class A {
223f4a2713aSLionel Sambuc     public: A();
224f4a2713aSLionel Sambuc     private: A(const A &); // expected-note 2 {{declared private here}}
225f4a2713aSLionel Sambuc   };
226f4a2713aSLionel Sambuc 
227f4a2713aSLionel Sambuc   class Test1 { A a; }; // expected-error {{field of type 'test6::A' has private copy constructor}}
test1(const Test1 & t)228f4a2713aSLionel Sambuc   void test1(const Test1 &t) {
229f4a2713aSLionel Sambuc     Test1 a = t; // expected-note{{implicit copy}}
230f4a2713aSLionel Sambuc   }
231f4a2713aSLionel Sambuc 
232f4a2713aSLionel Sambuc   class Test2 : A {}; // expected-error {{base class 'test6::A' has private copy constructor}}
test2(const Test2 & t)233f4a2713aSLionel Sambuc   void test2(const Test2 &t) {
234f4a2713aSLionel Sambuc     Test2 a = t; // expected-note{{implicit copy}}
235f4a2713aSLionel Sambuc   }
236f4a2713aSLionel Sambuc }
237f4a2713aSLionel Sambuc 
238f4a2713aSLionel Sambuc // Redeclaration lookups are not accesses.
239f4a2713aSLionel Sambuc namespace test7 {
240f4a2713aSLionel Sambuc   class A {
241f4a2713aSLionel Sambuc     int private_member;
242f4a2713aSLionel Sambuc   };
243f4a2713aSLionel Sambuc   class B : A {
foo(int private_member)244f4a2713aSLionel Sambuc     int foo(int private_member) {
245f4a2713aSLionel Sambuc       return 0;
246f4a2713aSLionel Sambuc     }
247f4a2713aSLionel Sambuc   };
248f4a2713aSLionel Sambuc }
249f4a2713aSLionel Sambuc 
250f4a2713aSLionel Sambuc // Ignored operator new and delete overloads are not
251f4a2713aSLionel Sambuc namespace test8 {
252f4a2713aSLionel Sambuc   typedef __typeof__(sizeof(int)) size_t;
253f4a2713aSLionel Sambuc 
254f4a2713aSLionel Sambuc   class A {
255f4a2713aSLionel Sambuc     void *operator new(size_t s);
256f4a2713aSLionel Sambuc     void operator delete(void *p);
257f4a2713aSLionel Sambuc   public:
258f4a2713aSLionel Sambuc     void *operator new(size_t s, int n);
259f4a2713aSLionel Sambuc     void operator delete(void *p, int n);
260f4a2713aSLionel Sambuc   };
261f4a2713aSLionel Sambuc 
test()262f4a2713aSLionel Sambuc   void test() {
263f4a2713aSLionel Sambuc     new (2) A();
264f4a2713aSLionel Sambuc   }
265f4a2713aSLionel Sambuc }
266f4a2713aSLionel Sambuc 
267f4a2713aSLionel Sambuc // Don't silently upgrade forbidden-access paths to private.
268f4a2713aSLionel Sambuc namespace test9 {
269f4a2713aSLionel Sambuc   class A {
270f4a2713aSLionel Sambuc   public: static int x; // expected-note {{member is declared here}}
271f4a2713aSLionel Sambuc   };
272f4a2713aSLionel Sambuc   class B : private A { // expected-note {{constrained by private inheritance here}}
273f4a2713aSLionel Sambuc   };
274f4a2713aSLionel Sambuc   class C : public B {
getX()275f4a2713aSLionel Sambuc     static int getX() { return x; } // expected-error {{'x' is a private member of 'test9::A'}}
276f4a2713aSLionel Sambuc   };
277f4a2713aSLionel Sambuc }
278f4a2713aSLionel Sambuc 
279f4a2713aSLionel Sambuc namespace test10 {
280f4a2713aSLionel Sambuc   class A {
281f4a2713aSLionel Sambuc     enum {
282f4a2713aSLionel Sambuc       value = 10 // expected-note {{declared private here}}
283f4a2713aSLionel Sambuc     };
284f4a2713aSLionel Sambuc     friend class C;
285f4a2713aSLionel Sambuc   };
286f4a2713aSLionel Sambuc 
287f4a2713aSLionel Sambuc   class B {
288f4a2713aSLionel Sambuc     enum {
289f4a2713aSLionel Sambuc       value = A::value // expected-error {{'value' is a private member of 'test10::A'}}
290f4a2713aSLionel Sambuc     };
291f4a2713aSLionel Sambuc   };
292f4a2713aSLionel Sambuc 
293f4a2713aSLionel Sambuc   class C {
294f4a2713aSLionel Sambuc     enum {
295f4a2713aSLionel Sambuc       value = A::value
296f4a2713aSLionel Sambuc     };
297f4a2713aSLionel Sambuc   };
298f4a2713aSLionel Sambuc }
299f4a2713aSLionel Sambuc 
300f4a2713aSLionel Sambuc namespace test11 {
301f4a2713aSLionel Sambuc   class A {
302f4a2713aSLionel Sambuc     protected: virtual ~A();
303f4a2713aSLionel Sambuc   };
304f4a2713aSLionel Sambuc 
305f4a2713aSLionel Sambuc   class B : public A {
306f4a2713aSLionel Sambuc     ~B();
307f4a2713aSLionel Sambuc   };
308f4a2713aSLionel Sambuc 
~B()309f4a2713aSLionel Sambuc   B::~B() {};
310f4a2713aSLionel Sambuc }
311f4a2713aSLionel Sambuc 
312f4a2713aSLionel Sambuc namespace test12 {
313f4a2713aSLionel Sambuc   class A {
314f4a2713aSLionel Sambuc     int x;
315f4a2713aSLionel Sambuc 
foo()316f4a2713aSLionel Sambuc     void foo() {
317f4a2713aSLionel Sambuc       class Local {
318f4a2713aSLionel Sambuc         int foo(A *a) {
319f4a2713aSLionel Sambuc           return a->x;
320f4a2713aSLionel Sambuc         }
321f4a2713aSLionel Sambuc       };
322f4a2713aSLionel Sambuc     }
323f4a2713aSLionel Sambuc   };
324f4a2713aSLionel Sambuc }
325f4a2713aSLionel Sambuc 
326f4a2713aSLionel Sambuc namespace test13 {
327f4a2713aSLionel Sambuc   struct A {
328f4a2713aSLionel Sambuc     int x;
329f4a2713aSLionel Sambuc     unsigned foo() const;
330f4a2713aSLionel Sambuc   };
331f4a2713aSLionel Sambuc 
332f4a2713aSLionel Sambuc   struct B : protected A {
333f4a2713aSLionel Sambuc     using A::foo;
334f4a2713aSLionel Sambuc     using A::x;
335f4a2713aSLionel Sambuc   };
336f4a2713aSLionel Sambuc 
test()337f4a2713aSLionel Sambuc   void test() {
338f4a2713aSLionel Sambuc     A *d;
339f4a2713aSLionel Sambuc     d->foo();
340f4a2713aSLionel Sambuc     (void) d->x;
341f4a2713aSLionel Sambuc   }
342f4a2713aSLionel Sambuc }
343f4a2713aSLionel Sambuc 
344f4a2713aSLionel Sambuc // Destructors for temporaries.
345f4a2713aSLionel Sambuc namespace test14 {
346f4a2713aSLionel Sambuc   class A {
347f4a2713aSLionel Sambuc   private: ~A(); // expected-note {{declared private here}}
348f4a2713aSLionel Sambuc   };
349f4a2713aSLionel Sambuc   A foo();
350f4a2713aSLionel Sambuc 
test()351f4a2713aSLionel Sambuc   void test() {
352f4a2713aSLionel Sambuc     foo(); // expected-error {{temporary of type 'test14::A' has private destructor}}
353f4a2713aSLionel Sambuc   }
354f4a2713aSLionel Sambuc 
355f4a2713aSLionel Sambuc   class X {
356f4a2713aSLionel Sambuc     ~X(); // expected-note {{declared private here}}
357f4a2713aSLionel Sambuc   };
358f4a2713aSLionel Sambuc 
359f4a2713aSLionel Sambuc   struct Y1 {
360f4a2713aSLionel Sambuc     operator X();
361f4a2713aSLionel Sambuc   };
362f4a2713aSLionel Sambuc 
g()363f4a2713aSLionel Sambuc   void g() {
364f4a2713aSLionel Sambuc     const X &xr = Y1(); // expected-error{{temporary of type 'test14::X' has private destructor}}
365f4a2713aSLionel Sambuc   }
366f4a2713aSLionel Sambuc }
367f4a2713aSLionel Sambuc 
368f4a2713aSLionel Sambuc // PR 7024
369f4a2713aSLionel Sambuc namespace test15 {
370f4a2713aSLionel Sambuc   template <class T> class A {
371f4a2713aSLionel Sambuc   private:
372f4a2713aSLionel Sambuc     int private_foo; // expected-note {{declared private here}}
373f4a2713aSLionel Sambuc     static int private_sfoo; // expected-note {{declared private here}}
374f4a2713aSLionel Sambuc   protected:
375f4a2713aSLionel Sambuc     int protected_foo; // expected-note 3 {{declared protected here}} // expected-note {{can only access this member on an object of type 'test15::B<int>'}}
376f4a2713aSLionel Sambuc     static int protected_sfoo; // expected-note 3 {{declared protected here}}
377f4a2713aSLionel Sambuc 
test1(A<int> & a)378f4a2713aSLionel Sambuc     int test1(A<int> &a) {
379f4a2713aSLionel Sambuc       return a.private_foo; // expected-error {{private member}}
380f4a2713aSLionel Sambuc     }
381f4a2713aSLionel Sambuc 
test2(A<int> & a)382f4a2713aSLionel Sambuc     int test2(A<int> &a) {
383f4a2713aSLionel Sambuc       return a.private_sfoo; // expected-error {{private member}}
384f4a2713aSLionel Sambuc     }
385f4a2713aSLionel Sambuc 
test3(A<int> & a)386f4a2713aSLionel Sambuc     int test3(A<int> &a) {
387f4a2713aSLionel Sambuc       return a.protected_foo; // expected-error {{protected member}}
388f4a2713aSLionel Sambuc     }
389f4a2713aSLionel Sambuc 
test4(A<int> & a)390f4a2713aSLionel Sambuc     int test4(A<int> &a) {
391f4a2713aSLionel Sambuc       return a.protected_sfoo; // expected-error {{protected member}}
392f4a2713aSLionel Sambuc     }
393f4a2713aSLionel Sambuc   };
394f4a2713aSLionel Sambuc 
395f4a2713aSLionel Sambuc   template class A<int>;
396f4a2713aSLionel Sambuc   template class A<long>; // expected-note 4 {{in instantiation}}
397f4a2713aSLionel Sambuc 
398f4a2713aSLionel Sambuc   template <class T> class B : public A<T> {
399f4a2713aSLionel Sambuc     // TODO: These first two accesses can be detected as ill-formed at
400f4a2713aSLionel Sambuc     // definition time because they're member accesses and A<int> can't
401f4a2713aSLionel Sambuc     // be a subclass of B<T> for any T.
402f4a2713aSLionel Sambuc 
test1(A<int> & a)403f4a2713aSLionel Sambuc     int test1(A<int> &a) {
404f4a2713aSLionel Sambuc       return a.protected_foo; // expected-error 2 {{protected member}}
405f4a2713aSLionel Sambuc     }
406f4a2713aSLionel Sambuc 
test2(A<int> & a)407f4a2713aSLionel Sambuc     int test2(A<int> &a) {
408f4a2713aSLionel Sambuc       return a.protected_sfoo; // expected-error {{protected member}}
409f4a2713aSLionel Sambuc     }
410f4a2713aSLionel Sambuc 
test3(B<int> & b)411f4a2713aSLionel Sambuc     int test3(B<int> &b) {
412f4a2713aSLionel Sambuc       return b.protected_foo; // expected-error {{protected member}}
413f4a2713aSLionel Sambuc     }
414f4a2713aSLionel Sambuc 
test4(B<int> & b)415f4a2713aSLionel Sambuc     int test4(B<int> &b) {
416f4a2713aSLionel Sambuc       return b.protected_sfoo; // expected-error {{protected member}}
417f4a2713aSLionel Sambuc     }
418f4a2713aSLionel Sambuc   };
419f4a2713aSLionel Sambuc 
420f4a2713aSLionel Sambuc   template class B<int>;  // expected-note {{in instantiation}}
421f4a2713aSLionel Sambuc   template class B<long>; // expected-note 4 {{in instantiation}}
422f4a2713aSLionel Sambuc }
423f4a2713aSLionel Sambuc 
424f4a2713aSLionel Sambuc // PR7281
425f4a2713aSLionel Sambuc namespace test16 {
426f4a2713aSLionel Sambuc   class A { ~A(); }; // expected-note 2{{declared private here}}
b()427f4a2713aSLionel Sambuc   void b() { throw A(); } // expected-error{{temporary of type 'test16::A' has private destructor}} \
428f4a2713aSLionel Sambuc   // expected-error{{exception object of type 'test16::A' has private destructor}}
429f4a2713aSLionel Sambuc }
430f4a2713aSLionel Sambuc 
431f4a2713aSLionel Sambuc // rdar://problem/8146294
432f4a2713aSLionel Sambuc namespace test17 {
433f4a2713aSLionel Sambuc   class A {
434f4a2713aSLionel Sambuc     template <typename T> class Inner { }; // expected-note {{declared private here}}
435f4a2713aSLionel Sambuc   };
436f4a2713aSLionel Sambuc 
437f4a2713aSLionel Sambuc   A::Inner<int> s; // expected-error {{'Inner' is a private member of 'test17::A'}}
438f4a2713aSLionel Sambuc }
439f4a2713aSLionel Sambuc 
440f4a2713aSLionel Sambuc namespace test18 {
441f4a2713aSLionel Sambuc   template <class T> class A {};
442f4a2713aSLionel Sambuc   class B : A<int> {
443f4a2713aSLionel Sambuc     A<int> member;
444f4a2713aSLionel Sambuc   };
445f4a2713aSLionel Sambuc 
446f4a2713aSLionel Sambuc   // FIXME: this access to A should be forbidden (because C++ is dumb),
447f4a2713aSLionel Sambuc   // but LookupResult can't express the necessary information to do
448f4a2713aSLionel Sambuc   // the check, so we aggressively suppress access control.
449f4a2713aSLionel Sambuc   class C : B {
450f4a2713aSLionel Sambuc     A<int> member;
451f4a2713aSLionel Sambuc   };
452f4a2713aSLionel Sambuc }
453f4a2713aSLionel Sambuc 
454f4a2713aSLionel Sambuc // PR8325
455f4a2713aSLionel Sambuc namespace test19 {
456f4a2713aSLionel Sambuc   class A { ~A(); };
457f4a2713aSLionel Sambuc   // The destructor is not implicitly referenced here.  Contrast to test16,
458f4a2713aSLionel Sambuc   // testing PR7281, earlier in this file.
b(A * x)459f4a2713aSLionel Sambuc   void b(A* x) { throw x; }
460f4a2713aSLionel Sambuc }
461f4a2713aSLionel Sambuc 
462f4a2713aSLionel Sambuc // PR7930
463f4a2713aSLionel Sambuc namespace test20 {
464f4a2713aSLionel Sambuc   class Foo {
465f4a2713aSLionel Sambuc     Foo(); // expected-note {{implicitly declared private here}}
466f4a2713aSLionel Sambuc   };
Foo()467f4a2713aSLionel Sambuc   Foo::Foo() {}
468f4a2713aSLionel Sambuc 
test()469f4a2713aSLionel Sambuc   void test() {
470f4a2713aSLionel Sambuc     Foo a; // expected-error {{calling a private constructor}}
471f4a2713aSLionel Sambuc   }
472f4a2713aSLionel Sambuc }
473f4a2713aSLionel Sambuc 
474f4a2713aSLionel Sambuc namespace test21 {
475f4a2713aSLionel Sambuc   template <class T> class A {
476f4a2713aSLionel Sambuc     void foo();
477f4a2713aSLionel Sambuc     void bar();
478f4a2713aSLionel Sambuc     class Inner; // expected-note {{implicitly declared private here}}
479f4a2713aSLionel Sambuc   public:
480f4a2713aSLionel Sambuc     void baz();
481f4a2713aSLionel Sambuc   };
482f4a2713aSLionel Sambuc   template <class T> class A<T>::Inner {};
483f4a2713aSLionel Sambuc   class B {
484f4a2713aSLionel Sambuc     template <class T> class A<T>::Inner; // expected-error{{non-friend class member 'Inner' cannot have a qualified name}}
485f4a2713aSLionel Sambuc   };
486f4a2713aSLionel Sambuc 
test()487f4a2713aSLionel Sambuc   void test() {
488f4a2713aSLionel Sambuc     A<int>::Inner i; // expected-error {{'Inner' is a private member}}
489f4a2713aSLionel Sambuc   }
490f4a2713aSLionel Sambuc }
491f4a2713aSLionel Sambuc 
492f4a2713aSLionel Sambuc namespace rdar8876150 {
493f4a2713aSLionel Sambuc   struct A { operator bool(); };
494f4a2713aSLionel Sambuc   struct B : private A { using A::operator bool; };
495f4a2713aSLionel Sambuc 
f()496f4a2713aSLionel Sambuc   bool f() {
497f4a2713aSLionel Sambuc     B b;
498f4a2713aSLionel Sambuc     return !b;
499f4a2713aSLionel Sambuc   }
500f4a2713aSLionel Sambuc }
501f4a2713aSLionel Sambuc 
502f4a2713aSLionel Sambuc namespace test23 {
503f4a2713aSLionel Sambuc   template <typename T> class A {
504f4a2713aSLionel Sambuc     A();
505f4a2713aSLionel Sambuc     static A instance;
506f4a2713aSLionel Sambuc   };
507f4a2713aSLionel Sambuc 
508f4a2713aSLionel Sambuc   template <typename T> A<T> A<T>::instance;
509f4a2713aSLionel Sambuc   template class A<int>;
510f4a2713aSLionel Sambuc }
511