p4.cpp revision b05b5f35f114505182b076aa70002843c0669beb
1c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall// RUN: %clang_cc1 -fsyntax-only -faccess-control -verify %s
2c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall
3c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall// C++0x [class.access]p4:
4c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall
5c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall//   Access control is applied uniformly to all names, whether the
6c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall//   names are referred to from declarations or expressions.  In the
7c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall//   case of overloaded function names, access control is applied to
8c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall//   the function selected by overload resolution.
9c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall
10c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallclass Public {} PublicInst;
11c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallclass Protected {} ProtectedInst;
12c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallclass Private {} PrivateInst;
13c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall
14c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallnamespace test0 {
15c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  class A {
16c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  public:
17c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    void foo(Public&);
18c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  protected:
19c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    void foo(Protected&); // expected-note 2 {{declared protected here}}
20c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  private:
21c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    void foo(Private&); // expected-note 2 {{declared private here}}
22c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  };
23c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall
24c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  void test(A *op) {
25c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    op->foo(PublicInst);
266b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    op->foo(ProtectedInst); // expected-error {{'foo' is a protected member}}
276b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    op->foo(PrivateInst); // expected-error {{'foo' is a private member}}
28c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall
29c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    void (A::*a)(Public&) = &A::foo;
306b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    void (A::*b)(Protected&) = &A::foo; // expected-error {{'foo' is a protected member}}
316b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    void (A::*c)(Private&) = &A::foo; // expected-error {{'foo' is a private member}}
32c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  }
33c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall}
345357b615364c17ea024c757354c58ae2a520d216John McCall
355357b615364c17ea024c757354c58ae2a520d216John McCall// Member operators.
365357b615364c17ea024c757354c58ae2a520d216John McCallnamespace test1 {
375357b615364c17ea024c757354c58ae2a520d216John McCall  class A {
385357b615364c17ea024c757354c58ae2a520d216John McCall  public:
395357b615364c17ea024c757354c58ae2a520d216John McCall    void operator+(Public&);
405357b615364c17ea024c757354c58ae2a520d216John McCall    void operator[](Public&);
4141d8903731782ee85ee2b19734008b006e01c76fJohn McCall    void operator()(Public&);
42233a6419097ed97b67ff8efcacef9af613262ca3John McCall    typedef void (*PublicSurrogate)(Public&);
43233a6419097ed97b67ff8efcacef9af613262ca3John McCall    operator PublicSurrogate() const;
445357b615364c17ea024c757354c58ae2a520d216John McCall  protected:
455357b615364c17ea024c757354c58ae2a520d216John McCall    void operator+(Protected&); // expected-note {{declared protected here}}
465357b615364c17ea024c757354c58ae2a520d216John McCall    void operator[](Protected&); // expected-note {{declared protected here}}
4741d8903731782ee85ee2b19734008b006e01c76fJohn McCall    void operator()(Protected&); // expected-note {{declared protected here}}
48233a6419097ed97b67ff8efcacef9af613262ca3John McCall    typedef void (*ProtectedSurrogate)(Protected&);
49233a6419097ed97b67ff8efcacef9af613262ca3John McCall    operator ProtectedSurrogate() const; // expected-note {{declared protected here}}
505357b615364c17ea024c757354c58ae2a520d216John McCall  private:
515357b615364c17ea024c757354c58ae2a520d216John McCall    void operator+(Private&); // expected-note {{declared private here}}
525357b615364c17ea024c757354c58ae2a520d216John McCall    void operator[](Private&); // expected-note {{declared private here}}
5341d8903731782ee85ee2b19734008b006e01c76fJohn McCall    void operator()(Private&); // expected-note {{declared private here}}
545357b615364c17ea024c757354c58ae2a520d216John McCall    void operator-(); // expected-note {{declared private here}}
55233a6419097ed97b67ff8efcacef9af613262ca3John McCall    typedef void (*PrivateSurrogate)(Private&);
56233a6419097ed97b67ff8efcacef9af613262ca3John McCall    operator PrivateSurrogate() const; // expected-note {{declared private here}}
575357b615364c17ea024c757354c58ae2a520d216John McCall  };
585357b615364c17ea024c757354c58ae2a520d216John McCall  void operator+(const A &, Public&);
595357b615364c17ea024c757354c58ae2a520d216John McCall  void operator+(const A &, Protected&);
605357b615364c17ea024c757354c58ae2a520d216John McCall  void operator+(const A &, Private&);
615357b615364c17ea024c757354c58ae2a520d216John McCall  void operator-(const A &);
625357b615364c17ea024c757354c58ae2a520d216John McCall
635357b615364c17ea024c757354c58ae2a520d216John McCall  void test(A &a, Public &pub, Protected &prot, Private &priv) {
645357b615364c17ea024c757354c58ae2a520d216John McCall    a + pub;
656b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    a + prot; // expected-error {{'operator+' is a protected member}}
666b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    a + priv; // expected-error {{'operator+' is a private member}}
675357b615364c17ea024c757354c58ae2a520d216John McCall    a[pub];
686b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    a[prot]; // expected-error {{'operator[]' is a protected member}}
696b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    a[priv]; // expected-error {{'operator[]' is a private member}}
7041d8903731782ee85ee2b19734008b006e01c76fJohn McCall    a(pub);
716b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    a(prot); // expected-error {{'operator()' is a protected member}}
726b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    a(priv); // expected-error {{'operator()' is a private member}}
736b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    -a;       // expected-error {{'operator-' is a private member}}
745357b615364c17ea024c757354c58ae2a520d216John McCall
755357b615364c17ea024c757354c58ae2a520d216John McCall    const A &ca = a;
765357b615364c17ea024c757354c58ae2a520d216John McCall    ca + pub;
775357b615364c17ea024c757354c58ae2a520d216John McCall    ca + prot;
785357b615364c17ea024c757354c58ae2a520d216John McCall    ca + priv;
795357b615364c17ea024c757354c58ae2a520d216John McCall    -ca;
80233a6419097ed97b67ff8efcacef9af613262ca3John McCall    // These are all surrogate calls
81233a6419097ed97b67ff8efcacef9af613262ca3John McCall    ca(pub);
826b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    ca(prot); // expected-error {{'operator void (*)(class Protected &)' is a protected member}}
836b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    ca(priv); // expected-error {{'operator void (*)(class Private &)' is a private member}}
845357b615364c17ea024c757354c58ae2a520d216John McCall  }
855357b615364c17ea024c757354c58ae2a520d216John McCall}
864f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall
874f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall// Implicit constructor calls.
884f9506a27cb6b865bf38beea48eadfa9dc93f510John McCallnamespace test2 {
894f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  class A {
904f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  private:
914f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall    A(); // expected-note {{declared private here}}
924f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall
934f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall    static A foo;
944f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  };
954f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall
966b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall  A a; // expected-error {{calling a private constructor}}
974f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  A A::foo; // okay
984f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall}
994f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall
1004f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall// Implicit destructor calls.
1014f9506a27cb6b865bf38beea48eadfa9dc93f510John McCallnamespace test3 {
1024f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  class A{
1034f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  private:
1044f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall    ~A(); // expected-note 3 {{declared private here}}
1054f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall    static A foo;
1064f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  };
1074f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall
1086b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall  A a; // expected-error {{'~A' is a private member}}
1094f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  A A::foo;
1104f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall
1116b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall  void foo(A param) { // expected-error {{'~A' is a private member}}
1126b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    A local; // expected-error {{'~A' is a private member}}
1134f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  }
1144f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall}
115b05b5f35f114505182b076aa70002843c0669bebJohn McCall
116b05b5f35f114505182b076aa70002843c0669bebJohn McCall// Conversion functions.
117b05b5f35f114505182b076aa70002843c0669bebJohn McCallnamespace test4 {
118b05b5f35f114505182b076aa70002843c0669bebJohn McCall  class Base {
119b05b5f35f114505182b076aa70002843c0669bebJohn McCall  private:
120b05b5f35f114505182b076aa70002843c0669bebJohn McCall    operator Private(); // expected-note 4 {{declared private here}}
121b05b5f35f114505182b076aa70002843c0669bebJohn McCall  public:
122b05b5f35f114505182b076aa70002843c0669bebJohn McCall    operator Public();
123b05b5f35f114505182b076aa70002843c0669bebJohn McCall  };
124b05b5f35f114505182b076aa70002843c0669bebJohn McCall
125b05b5f35f114505182b076aa70002843c0669bebJohn McCall  class Derived1 : private Base { // expected-note 2 {{declared private here}} \
126b05b5f35f114505182b076aa70002843c0669bebJohn McCall                                  // expected-note {{constrained by private inheritance}}
127b05b5f35f114505182b076aa70002843c0669bebJohn McCall    Private test1() { return *this; } // expected-error {{'operator Private' is a private member}}
128b05b5f35f114505182b076aa70002843c0669bebJohn McCall    Public test2() { return *this; }
129b05b5f35f114505182b076aa70002843c0669bebJohn McCall  };
130b05b5f35f114505182b076aa70002843c0669bebJohn McCall  Private test1(Derived1 &d) { return d; } // expected-error {{'operator Private' is a private member}} \
131b05b5f35f114505182b076aa70002843c0669bebJohn McCall                                           // expected-error {{cannot cast 'test4::Derived1' to its private base class}}
132b05b5f35f114505182b076aa70002843c0669bebJohn McCall  Public test2(Derived1 &d) { return d; } // expected-error {{cannot cast 'test4::Derived1' to its private base class}} \
133b05b5f35f114505182b076aa70002843c0669bebJohn McCall                                          // expected-error {{'operator Public' is a private member}}
134b05b5f35f114505182b076aa70002843c0669bebJohn McCall
135b05b5f35f114505182b076aa70002843c0669bebJohn McCall
136b05b5f35f114505182b076aa70002843c0669bebJohn McCall  class Derived2 : public Base {
137b05b5f35f114505182b076aa70002843c0669bebJohn McCall    Private test1() { return *this; } // expected-error {{'operator Private' is a private member}}
138b05b5f35f114505182b076aa70002843c0669bebJohn McCall    Public test2() { return *this; }
139b05b5f35f114505182b076aa70002843c0669bebJohn McCall  };
140b05b5f35f114505182b076aa70002843c0669bebJohn McCall  Private test1(Derived2 &d) { return d; } // expected-error {{'operator Private' is a private member}}
141b05b5f35f114505182b076aa70002843c0669bebJohn McCall  Public test2(Derived2 &d) { return d; }
142b05b5f35f114505182b076aa70002843c0669bebJohn McCall
143b05b5f35f114505182b076aa70002843c0669bebJohn McCall  class Derived3 : private Base { // expected-note {{constrained by private inheritance here}} \
144b05b5f35f114505182b076aa70002843c0669bebJohn McCall                                  // expected-note {{declared private here}}
145b05b5f35f114505182b076aa70002843c0669bebJohn McCall  public:
146b05b5f35f114505182b076aa70002843c0669bebJohn McCall    operator Private();
147b05b5f35f114505182b076aa70002843c0669bebJohn McCall  };
148b05b5f35f114505182b076aa70002843c0669bebJohn McCall  Private test1(Derived3 &d) { return d; }
149b05b5f35f114505182b076aa70002843c0669bebJohn McCall  Public test2(Derived3 &d) { return d; } // expected-error {{'operator Public' is a private member of 'test4::Base'}} \
150b05b5f35f114505182b076aa70002843c0669bebJohn McCall                                          // expected-error {{cannot cast 'test4::Derived3' to its private base class}}
151b05b5f35f114505182b076aa70002843c0669bebJohn McCall
152b05b5f35f114505182b076aa70002843c0669bebJohn McCall  class Derived4 : public Base {
153b05b5f35f114505182b076aa70002843c0669bebJohn McCall  public:
154b05b5f35f114505182b076aa70002843c0669bebJohn McCall    operator Private();
155b05b5f35f114505182b076aa70002843c0669bebJohn McCall  };
156b05b5f35f114505182b076aa70002843c0669bebJohn McCall  Private test1(Derived4 &d) { return d; }
157b05b5f35f114505182b076aa70002843c0669bebJohn McCall  Public test2(Derived4 &d) { return d; }
158b05b5f35f114505182b076aa70002843c0669bebJohn McCall}
159