blob: 7aa614cd8b1a20ea1d5bf971e9e3b112cfade0be [file] [log] [blame]
John McCallc373d482010-01-27 01:50:18 +00001// RUN: %clang_cc1 -fsyntax-only -faccess-control -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);
John McCall6b2accb2010-02-10 09:31:12 +000026 op->foo(ProtectedInst); // expected-error {{'foo' is a protected member}}
27 op->foo(PrivateInst); // expected-error {{'foo' is a private member}}
John McCallc373d482010-01-27 01:50:18 +000028
29 void (A::*a)(Public&) = &A::foo;
John McCall6b2accb2010-02-10 09:31:12 +000030 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}}
John McCallc373d482010-01-27 01:50:18 +000032 }
33}
John McCall5357b612010-01-28 01:42:12 +000034
35// Member operators.
36namespace test1 {
37 class A {
38 public:
39 void operator+(Public&);
40 void operator[](Public&);
John McCall41d89032010-01-28 01:54:34 +000041 void operator()(Public&);
John McCall233a6412010-01-28 07:38:46 +000042 typedef void (*PublicSurrogate)(Public&);
43 operator PublicSurrogate() const;
John McCall5357b612010-01-28 01:42:12 +000044 protected:
45 void operator+(Protected&); // expected-note {{declared protected here}}
46 void operator[](Protected&); // expected-note {{declared protected here}}
John McCall41d89032010-01-28 01:54:34 +000047 void operator()(Protected&); // expected-note {{declared protected here}}
John McCall233a6412010-01-28 07:38:46 +000048 typedef void (*ProtectedSurrogate)(Protected&);
49 operator ProtectedSurrogate() const; // expected-note {{declared protected here}}
John McCall5357b612010-01-28 01:42:12 +000050 private:
51 void operator+(Private&); // expected-note {{declared private here}}
52 void operator[](Private&); // expected-note {{declared private here}}
John McCall41d89032010-01-28 01:54:34 +000053 void operator()(Private&); // expected-note {{declared private here}}
John McCall5357b612010-01-28 01:42:12 +000054 void operator-(); // expected-note {{declared private here}}
John McCall233a6412010-01-28 07:38:46 +000055 typedef void (*PrivateSurrogate)(Private&);
56 operator PrivateSurrogate() const; // expected-note {{declared private here}}
John McCall5357b612010-01-28 01:42:12 +000057 };
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;
John McCall6b2accb2010-02-10 09:31:12 +000065 a + prot; // expected-error {{'operator+' is a protected member}}
66 a + priv; // expected-error {{'operator+' is a private member}}
John McCall5357b612010-01-28 01:42:12 +000067 a[pub];
John McCall6b2accb2010-02-10 09:31:12 +000068 a[prot]; // expected-error {{'operator[]' is a protected member}}
69 a[priv]; // expected-error {{'operator[]' is a private member}}
John McCall41d89032010-01-28 01:54:34 +000070 a(pub);
John McCall6b2accb2010-02-10 09:31:12 +000071 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}}
John McCall5357b612010-01-28 01:42:12 +000074
75 const A &ca = a;
76 ca + pub;
77 ca + prot;
78 ca + priv;
79 -ca;
John McCall233a6412010-01-28 07:38:46 +000080 // These are all surrogate calls
81 ca(pub);
John McCall6b2accb2010-02-10 09:31:12 +000082 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}}
John McCall5357b612010-01-28 01:42:12 +000084 }
85}
John McCall4f9506a2010-02-02 08:45:54 +000086
87// Implicit constructor calls.
88namespace test2 {
89 class A {
90 private:
91 A(); // expected-note {{declared private here}}
92
93 static A foo;
94 };
95
John McCall6b2accb2010-02-10 09:31:12 +000096 A a; // expected-error {{calling a private constructor}}
John McCall4f9506a2010-02-02 08:45:54 +000097 A A::foo; // okay
98}
99
100// Implicit destructor calls.
101namespace test3 {
102 class A{
103 private:
104 ~A(); // expected-note 3 {{declared private here}}
105 static A foo;
106 };
107
John McCall6b2accb2010-02-10 09:31:12 +0000108 A a; // expected-error {{'~A' is a private member}}
John McCall4f9506a2010-02-02 08:45:54 +0000109 A A::foo;
110
John McCall6b2accb2010-02-10 09:31:12 +0000111 void foo(A param) { // expected-error {{'~A' is a private member}}
112 A local; // expected-error {{'~A' is a private member}}
John McCall4f9506a2010-02-02 08:45:54 +0000113 }
114}