p4.cpp revision 9a68a67c6ae4982001815cc04f69b8781058263a
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2
3// C++0x [class.access]p4:
4
5//   Access control is applied uniformly to all names, whether the
6//   names are referred to from declarations or expressions.  In the
7//   case of overloaded function names, access control is applied to
8//   the function selected by overload resolution.
9
10class Public {} PublicInst;
11class Protected {} ProtectedInst;
12class Private {} PrivateInst;
13
14namespace test0 {
15  class A {
16  public:
17    void foo(Public&);
18  protected:
19    void foo(Protected&); // expected-note 2 {{declared protected here}}
20  private:
21    void foo(Private&); // expected-note 2 {{declared private here}}
22  };
23
24  void test(A *op) {
25    op->foo(PublicInst);
26    op->foo(ProtectedInst); // expected-error {{'foo' is a protected member}}
27    op->foo(PrivateInst); // expected-error {{'foo' is a private member}}
28
29    void (A::*a)(Public&) = &A::foo;
30    void (A::*b)(Protected&) = &A::foo; // expected-error {{'foo' is a protected member}}
31    void (A::*c)(Private&) = &A::foo; // expected-error {{'foo' is a private member}}
32  }
33}
34
35// Member operators.
36namespace test1 {
37  class A {
38  public:
39    void operator+(Public&);
40    void operator[](Public&);
41    void operator()(Public&);
42    typedef void (*PublicSurrogate)(Public&);
43    operator PublicSurrogate() const;
44  protected:
45    void operator+(Protected&); // expected-note {{declared protected here}}
46    void operator[](Protected&); // expected-note {{declared protected here}}
47    void operator()(Protected&); // expected-note {{declared protected here}}
48    typedef void (*ProtectedSurrogate)(Protected&);
49    operator ProtectedSurrogate() const; // expected-note {{declared protected here}}
50  private:
51    void operator+(Private&); // expected-note {{declared private here}}
52    void operator[](Private&); // expected-note {{declared private here}}
53    void operator()(Private&); // expected-note {{declared private here}}
54    void operator-(); // expected-note {{declared private here}}
55    typedef void (*PrivateSurrogate)(Private&);
56    operator PrivateSurrogate() const; // expected-note {{declared private here}}
57  };
58  void operator+(const A &, Public&);
59  void operator+(const A &, Protected&);
60  void operator+(const A &, Private&);
61  void operator-(const A &);
62
63  void test(A &a, Public &pub, Protected &prot, Private &priv) {
64    a + pub;
65    a + prot; // expected-error {{'operator+' is a protected member}}
66    a + priv; // expected-error {{'operator+' is a private member}}
67    a[pub];
68    a[prot]; // expected-error {{'operator[]' is a protected member}}
69    a[priv]; // expected-error {{'operator[]' is a private member}}
70    a(pub);
71    a(prot); // expected-error {{'operator()' is a protected member}}
72    a(priv); // expected-error {{'operator()' is a private member}}
73    -a;       // expected-error {{'operator-' is a private member}}
74
75    const A &ca = a;
76    ca + pub;
77    ca + prot;
78    ca + priv;
79    -ca;
80    // These are all surrogate calls
81    ca(pub);
82    ca(prot); // expected-error {{'operator void (*)(class Protected &)' is a protected member}}
83    ca(priv); // expected-error {{'operator void (*)(class Private &)' is a private member}}
84  }
85}
86
87// Implicit constructor calls.
88namespace test2 {
89  class A {
90  private:
91    A(); // expected-note 3 {{declared private here}}
92
93    static A foo;
94  };
95
96  A a; // expected-error {{calling a private constructor}}
97  A A::foo; // okay
98
99  class B : A { }; // expected-error {{base class 'test2::A' has private constructor}}
100  B b;
101
102  class C : virtual A {
103  public:
104    C();
105  };
106
107  // FIXME: It would be better if this said something about A being an inherited virtual base.
108  class D : C { }; // expected-error {{base class 'test2::A' has private constructor}}
109  D d;
110}
111
112// Implicit destructor calls.
113namespace test3 {
114  class A {
115  private:
116    ~A(); // expected-note 2 {{declared private here}}
117    static A foo;
118  };
119
120  A a; // expected-error {{variable of type 'test3::A' has private destructor}}
121  A A::foo;
122
123  void foo(A param) { // okay
124    A local; // expected-error {{variable of type 'test3::A' has private destructor}}
125  }
126
127  template <unsigned N> class Base { ~Base(); }; // expected-note 14 {{declared private here}}
128  class Base2 : virtual Base<2> { ~Base2(); }; // expected-note 3 {{declared private here}} \
129                                               // expected-error {{base class 'Base<2>' has private destructor}}
130  class Base3 : virtual Base<3> { public: ~Base3(); }; // expected-error {{base class 'Base<3>' has private destructor}}
131
132  // These don't cause diagnostics because we don't need the destructor.
133  class Derived0 : Base<0> { ~Derived0(); };
134  class Derived1 : Base<1> { };
135
136  class Derived2 : // expected-error {{inherited virtual base class 'Base<2>' has private destructor}} \
137                   // expected-error {{inherited virtual base class 'Base<3>' has private destructor}}
138    Base<0>,  // expected-error {{base class 'Base<0>' has private destructor}}
139    virtual Base<1>, // expected-error {{base class 'Base<1>' has private destructor}}
140    Base2, // expected-error {{base class 'test3::Base2' has private destructor}}
141    virtual Base3
142  {
143    ~Derived2() {}
144  };
145
146  class Derived3 : // expected-error 2 {{inherited virtual base class 'Base<2>' has private destructor}} \
147                   // expected-error 2 {{inherited virtual base class 'Base<3>' has private destructor}}
148    Base<0>,  // expected-error 2 {{base class 'Base<0>' has private destructor}}
149    virtual Base<1>, // expected-error 2 {{base class 'Base<1>' has private destructor}}
150    Base2, // expected-error 2 {{base class 'test3::Base2' has private destructor}}
151    virtual Base3
152  {};
153  Derived3 d3;
154}
155
156// Conversion functions.
157namespace test4 {
158  class Base {
159  private:
160    operator Private(); // expected-note 4 {{declared private here}}
161  public:
162    operator Public();
163  };
164
165  class Derived1 : private Base { // expected-note 2 {{declared private here}} \
166                                  // expected-note {{constrained by private inheritance}}
167    Private test1() { return *this; } // expected-error {{'operator Private' is a private member}}
168    Public test2() { return *this; }
169  };
170  Private test1(Derived1 &d) { return d; } // expected-error {{'operator Private' is a private member}} \
171                                           // expected-error {{cannot cast 'test4::Derived1' to its private base class}}
172  Public test2(Derived1 &d) { return d; } // expected-error {{cannot cast 'test4::Derived1' to its private base class}} \
173                                          // expected-error {{'operator Public' is a private member}}
174
175
176  class Derived2 : public Base {
177    Private test1() { return *this; } // expected-error {{'operator Private' is a private member}}
178    Public test2() { return *this; }
179  };
180  Private test1(Derived2 &d) { return d; } // expected-error {{'operator Private' is a private member}}
181  Public test2(Derived2 &d) { return d; }
182
183  class Derived3 : private Base { // expected-note {{constrained by private inheritance here}} \
184                                  // expected-note {{declared private here}}
185  public:
186    operator Private();
187  };
188  Private test1(Derived3 &d) { return d; }
189  Public test2(Derived3 &d) { return d; } // expected-error {{'operator Public' is a private member of 'test4::Base'}} \
190                                          // expected-error {{cannot cast 'test4::Derived3' to its private base class}}
191
192  class Derived4 : public Base {
193  public:
194    operator Private();
195  };
196  Private test1(Derived4 &d) { return d; }
197  Public test2(Derived4 &d) { return d; }
198}
199
200// Implicit copy assignment operator uses.
201namespace test5 {
202  class A {
203    void operator=(const A &); // expected-note 2 {{declared private here}}
204  };
205
206  class Test1 { A a; }; // expected-error {{field of type 'test5::A' has private copy assignment operator}}
207  void test1() {
208    Test1 a;
209    a = Test1();
210  }
211
212  class Test2 : A {}; // expected-error {{base class 'test5::A' has private copy assignment operator}}
213  void test2() {
214    Test2 a;
215    a = Test2();
216  }
217}
218
219// Implicit copy constructor uses.
220namespace test6 {
221  class A {
222    public: A();
223    private: A(const A &); // expected-note 2 {{declared private here}}
224  };
225
226  class Test1 { A a; }; // expected-error {{field of type 'test6::A' has private copy constructor}}
227  void test1(const Test1 &t) {
228    Test1 a = t;
229  }
230
231  class Test2 : A {}; // expected-error {{base class 'test6::A' has private copy constructor}}
232  void test2(const Test2 &t) {
233    Test2 a = t;
234  }
235}
236
237// Redeclaration lookups are not accesses.
238namespace test7 {
239  class A {
240    int private_member;
241  };
242  class B : A {
243    int foo(int private_member) {
244      return 0;
245    }
246  };
247}
248
249// Ignored operator new and delete overloads are not
250namespace test8 {
251  typedef __typeof__(sizeof(int)) size_t;
252
253  class A {
254    void *operator new(size_t s);
255    void operator delete(void *p);
256  public:
257    void *operator new(size_t s, int n);
258    void operator delete(void *p, int n);
259  };
260
261  void test() {
262    new (2) A();
263  }
264}
265
266// Don't silently upgrade forbidden-access paths to private.
267namespace test9 {
268  class A {
269    public: static int x;
270  };
271  class B : private A { // expected-note {{constrained by private inheritance here}}
272  };
273  class C : public B {
274    static int getX() { return x; } // expected-error {{'x' is a private member of 'test9::A'}}
275  };
276}
277
278namespace test10 {
279  class A {
280    enum {
281      value = 10 // expected-note {{declared private here}}
282    };
283    friend class C;
284  };
285
286  class B {
287    enum {
288      value = A::value // expected-error {{'value' is a private member of 'test10::A'}}
289    };
290  };
291
292  class C {
293    enum {
294      value = A::value
295    };
296  };
297}
298
299namespace test11 {
300  class A {
301    protected: virtual ~A();
302  };
303
304  class B : public A {
305    ~B();
306  };
307
308  B::~B() {};
309}
310
311namespace test12 {
312  class A {
313    int x;
314
315    void foo() {
316      class Local {
317        int foo(A *a) {
318          return a->x;
319        }
320      };
321    }
322  };
323}
324
325namespace test13 {
326  struct A {
327    int x;
328    unsigned foo() const;
329  };
330
331  struct B : protected A {
332    using A::foo;
333    using A::x;
334  };
335
336  void test() {
337    A *d;
338    d->foo();
339    (void) d->x;
340  }
341}
342
343// Destructors for temporaries.
344namespace test14 {
345  class A {
346  private: ~A(); // expected-note {{declared private here}}
347  };
348  A foo();
349
350  void test() {
351    foo(); // expected-error {{temporary of type 'test14::A' has private destructor}}
352  }
353}
354