13573b2c84372d9484296fa658f5276f6c09acb92Daniel Dunbar// RUN: %clang_cc1 -fsyntax-only -verify %s
287d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
387d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
5fc8ba61c279f72d3bac5df8754348037df5f76f3Argiris Kirtzidisstruct Base { };
6ec29a8136449036f485e28f6e1e21ae3fb0289a0John McCallstruct Derived : Base { }; // expected-note{{candidate constructor (the implicit copy constructor) not viable}}
787d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar#if __cplusplus >= 201103L // C++11 or later
887d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar// expected-note@-2 {{candidate constructor (the implicit move constructor) not viable}}
987d948ecccffea9e9e37d0d053b246e2d6d6c47bPirama Arumuga Nainar#endif
10da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct Unrelated { };
11da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct Derived2 : Base { };
12da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct Diamond : Derived, Derived2 { };
13da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
14da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToBaseRef {
15da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  operator Base&() const;
16da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor};
17da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
18da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToDerivedRef {
19da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  operator Derived&() const;
20da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor};
21da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
22da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToBothDerivedRef {
23da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  operator Derived&(); // expected-note{{candidate function}}
24da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  operator Derived2&(); // expected-note{{candidate function}}
25da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor};
26da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
27da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToIntRef {
28da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  operator int&();
29da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor};
30da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
31da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToBase {
32da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  operator Base() const;
33da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor};
34da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
35da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToDerived {
36da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  operator Derived() const;
37da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor};
38da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
39da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToBothDerived {
40da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  operator Derived(); // expected-note{{candidate function}}
41da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  operator Derived2(); // expected-note{{candidate function}}
42da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor};
43da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
44da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToInt {
45da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  operator int();
46da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor};
47da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
48da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregortemplate<typename T> T create();
49da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
50da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor// First bullet: lvalue references binding to lvalues (the simple cases).
51da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_lvalue_to_lvalue(Base b, Derived d,
52da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor                           const Base bc, const Derived dc,
53da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor                           Diamond diamond,
54da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor                           int i) {
55da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  // Reference-compatible
56da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  Base &br1 = b;
57da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  Base &br2 = d;
58da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  Derived &dr1 = d;
5998624359b74071fdf2575b5b1afb5d3a3a86b4a3John McCall  Derived &dr2 = b; // expected-error{{non-const lvalue reference to type 'Derived' cannot bind to a value of unrelated type 'Base'}}
60b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  Base &br3 = bc; // expected-error{{drops 'const' qualifier}}
61b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  Base &br4 = dc; // expected-error{{drops 'const' qualifier}}
6298624359b74071fdf2575b5b1afb5d3a3a86b4a3John McCall  Base &br5 = diamond; // expected-error{{ambiguous conversion from derived class 'Diamond' to base class 'Base':}}
63da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  int &ir = i;
64da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  long &lr = i; // expected-error{{non-const lvalue reference to type 'long' cannot bind to a value of unrelated type 'int'}}
65da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}
66da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
67da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_lvalue_quals(volatile Base b, volatile Derived d,
68da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor                       volatile const Base bvc, volatile const Derived dvc,
69da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor                       volatile const int ivc) {
70da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  volatile Base &bvr1 = b;
71da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  volatile Base &bvr2 = d;
72b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  volatile Base &bvr3 = bvc; // expected-error{{binding value of type 'const volatile Base' to reference to type 'volatile Base' drops 'const' qualifier}}
73b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  volatile Base &bvr4 = dvc; // expected-error{{binding value of type 'const volatile Derived' to reference to type 'volatile Base' drops 'const' qualifier}}
74da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
75b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  volatile int &ir = ivc; // expected-error{{binding value of type 'const volatile int' to reference to type 'volatile int' drops 'const' qualifier}}
76cc845d86b09df95440626e0f5c9c4ce361fc8fffDouglas Gregor
77cc845d86b09df95440626e0f5c9c4ce361fc8fffDouglas Gregor  const volatile Base &bcvr1 = b;
78cc845d86b09df95440626e0f5c9c4ce361fc8fffDouglas Gregor  const volatile Base &bcvr2 = d;
79da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}
80da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
81da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_lvalue_to_rvalue() {
8298624359b74071fdf2575b5b1afb5d3a3a86b4a3John McCall  Base &br1 = Base(); // expected-error{{non-const lvalue reference to type 'Base' cannot bind to a temporary of type 'Base'}}
8398624359b74071fdf2575b5b1afb5d3a3a86b4a3John McCall  Base &br2 = Derived(); // expected-error{{non-const lvalue reference to type 'Base' cannot bind to a temporary of type 'Derived'}}
84da004db50cc2f5917b3a94d5cc54b4a6e85fdb05Chris Lattner  const volatile Base &br3 = Base(); // expected-error{{volatile lvalue reference to type 'const volatile Base' cannot bind to a temporary of type 'Base'}}
85da004db50cc2f5917b3a94d5cc54b4a6e85fdb05Chris Lattner  const volatile Base &br4 = Derived(); // expected-error{{volatile lvalue reference to type 'const volatile Base' cannot bind to a temporary of type 'Derived'}}
86da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
87da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  int &ir = 17; // expected-error{{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
88da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}
89da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
90da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_lvalue_to_unrelated(Unrelated ur) {
9198624359b74071fdf2575b5b1afb5d3a3a86b4a3John McCall  Base &br1 = ur; // expected-error{{non-const lvalue reference to type 'Base' cannot bind to a value of unrelated type 'Unrelated'}}
92da004db50cc2f5917b3a94d5cc54b4a6e85fdb05Chris Lattner  const volatile Base &br2 = ur; // expected-error{{volatile lvalue reference to type 'const volatile Base' cannot bind to a value of unrelated type 'Unrelated'}}
93da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}
94da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
95da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_lvalue_to_conv_lvalue() {
96da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  // Not reference-related, but convertible
97da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  Base &nbr1 = ConvertibleToBaseRef();
98da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  Base &nbr2 = ConvertibleToDerivedRef();
99da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  Derived &ndr1 = ConvertibleToDerivedRef();
100da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  int &ir = ConvertibleToIntRef();
101da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}
102da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
103da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_lvalue_to_conv_lvalue_ambig(ConvertibleToBothDerivedRef both) {
104da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  Derived &dr1 = both;
10598624359b74071fdf2575b5b1afb5d3a3a86b4a3John McCall  Base &br1 = both; // expected-error{{reference initialization of type 'Base &' with initializer of type 'ConvertibleToBothDerivedRef' is ambiguous}}
106da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}
107da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
108da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct IntBitfield {
109da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  int i : 17; // expected-note{{bit-field is declared here}}
110da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor};
111da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
112da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid test_bitfield(IntBitfield ib) {
113da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  int & ir1 = (ib.i); // expected-error{{non-const reference cannot bind to bit-field 'i'}}
114da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}
115da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
116da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor// Second bullet: const lvalue reference binding to an rvalue with
117da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor// similar type (both of which are class types).
118da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_const_lvalue_to_rvalue() {
119da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  const Base &br1 = create<Base>();
120da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  const Base &br2 = create<Derived>();
121da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  const Derived &dr1 = create<Base>(); // expected-error{{no viable conversion}}
122da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
123da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  const Base &br3 = create<const Base>();
124da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  const Base &br4 = create<const Derived>();
125da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
126b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  const Base &br5 = create<const volatile Base>(); // expected-error{{binding value of type 'const volatile Base' to reference to type 'const Base' drops 'volatile' qualifier}}
127b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  const Base &br6 = create<const volatile Derived>(); // expected-error{{binding value of type 'const volatile Derived' to reference to type 'const Base' drops 'volatile' qualifier}}
128da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
129da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  const int &ir = create<int>();
130da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}
131da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor
132da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor// Second bullet: const lvalue reference binds to the result of a conversion.
133da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_const_lvalue_to_class_conv_temporary() {
134da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  const Base &br1 = ConvertibleToBase();
135da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  const Base &br2 = ConvertibleToDerived();
136da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}
137da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_lvalue_to_conv_rvalue_ambig(ConvertibleToBothDerived both) {
138da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor  const Derived &dr1 = both;
139da004db50cc2f5917b3a94d5cc54b4a6e85fdb05Chris Lattner  const Base &br1 = both; // expected-error{{reference initialization of type 'const Base &' with initializer of type 'ConvertibleToBothDerived' is ambiguous}}
140da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}
141