p4.cpp revision 233a6419097ed97b67ff8efcacef9af613262ca3
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);
26c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    op->foo(ProtectedInst); // expected-error {{access to protected member outside any class}}
27c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    op->foo(PrivateInst); // expected-error {{access to private member outside any class}}
28c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall
29c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    void (A::*a)(Public&) = &A::foo;
30c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    void (A::*b)(Protected&) = &A::foo; // expected-error {{access to protected member outside any class}}
31c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    void (A::*c)(Private&) = &A::foo; // expected-error {{access to private member outside any class}}
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;
655357b615364c17ea024c757354c58ae2a520d216John McCall    a + prot; // expected-error {{access to protected member}}
665357b615364c17ea024c757354c58ae2a520d216John McCall    a + priv; // expected-error {{access to private member}}
675357b615364c17ea024c757354c58ae2a520d216John McCall    a[pub];
685357b615364c17ea024c757354c58ae2a520d216John McCall    a[prot]; // expected-error {{access to protected member}}
695357b615364c17ea024c757354c58ae2a520d216John McCall    a[priv]; // expected-error {{access to private member}}
7041d8903731782ee85ee2b19734008b006e01c76fJohn McCall    a(pub);
7141d8903731782ee85ee2b19734008b006e01c76fJohn McCall    a(prot); // expected-error {{access to protected member}}
7241d8903731782ee85ee2b19734008b006e01c76fJohn McCall    a(priv); // expected-error {{access to private member}}
735357b615364c17ea024c757354c58ae2a520d216John McCall    -a;       // expected-error {{access to 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);
82233a6419097ed97b67ff8efcacef9af613262ca3John McCall    ca(prot); // expected-error {{access to protected member}}
83233a6419097ed97b67ff8efcacef9af613262ca3John McCall    ca(priv); // expected-error {{access to private member}}
845357b615364c17ea024c757354c58ae2a520d216John McCall  }
855357b615364c17ea024c757354c58ae2a520d216John McCall}
86