blob: bca75c0b2fb5acf355793a99d40929eb28aaefdd [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002class X {
3public:
4 operator bool();
5 operator int() const;
Douglas Gregor2def4832008-11-17 20:34:05 +00006
7 bool f() {
8 return operator bool();
9 }
10
11 float g() {
John McCall578b69b2009-12-16 08:11:27 +000012 return operator float(); // expected-error{{use of undeclared 'operator float'}}
Douglas Gregor2def4832008-11-17 20:34:05 +000013 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +000014};
15
16operator int(); // expected-error{{conversion function must be a non-static member function}}
17
Douglas Gregor10bd3682008-11-17 22:58:34 +000018operator int; // expected-error{{'operator int' cannot be the name of a variable or data member}}
19
Douglas Gregor2f1bc522008-11-07 20:08:42 +000020typedef int func_type(int);
21typedef int array_type[10];
22
23class Y {
24public:
25 void operator bool(int, ...) const; // expected-error{{conversion function cannot have a return type}} \
Chris Lattner6e475012009-04-25 08:35:12 +000026 // expected-error{{conversion function cannot have any parameters}}
27
28 operator float(...) const; // expected-error{{conversion function cannot be variadic}}
29
30
Douglas Gregor2f1bc522008-11-07 20:08:42 +000031 operator func_type(); // expected-error{{conversion function cannot convert to a function type}}
32 operator array_type(); // expected-error{{conversion function cannot convert to an array type}}
33};
34
35
36typedef int INT;
37typedef INT* INT_PTR;
38
39class Z {
Chris Lattner5f4a6822008-11-23 23:12:31 +000040 operator int(); // expected-note {{previous declaration is here}}
41 operator int**(); // expected-note {{previous declaration is here}}
Douglas Gregor2f1bc522008-11-07 20:08:42 +000042
43 operator INT(); // expected-error{{conversion function cannot be redeclared}}
44 operator INT_PTR*(); // expected-error{{conversion function cannot be redeclared}}
45};
46
47
48class A { };
49
50class B : public A {
51public:
John McCall7c2342d2010-03-10 11:27:22 +000052 operator A&() const; // expected-warning{{conversion function converting 'B' to its base class 'A' will never be used}}
53 operator const void() const; // expected-warning{{conversion function converting 'B' to 'void const' will never be used}}
54 operator const B(); // expected-warning{{conversion function converting 'B' to itself will never be used}}
Douglas Gregor2f1bc522008-11-07 20:08:42 +000055};
Sebastian Redl3201f6b2009-04-16 17:51:27 +000056
57// This used to crash Clang.
58struct Flip;
John McCallb1622a12010-01-06 09:43:14 +000059struct Flop { // expected-note{{candidate is the implicit copy constructor}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +000060 Flop();
John McCallb1622a12010-01-06 09:43:14 +000061 Flop(const Flip&); // expected-note{{candidate constructor}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +000062};
63struct Flip {
Douglas Gregor7abfbdb2009-12-19 03:01:41 +000064 operator Flop() const; // expected-note{{candidate function}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +000065};
John McCall7c2342d2010-03-10 11:27:22 +000066Flop flop = Flip(); // expected-error {{conversion from 'Flip' to 'Flop' is ambiguous}}
Anders Carlsson2c59d3c2009-09-13 21:33:06 +000067
68// This tests that we don't add the second conversion declaration to the list of user conversions
69struct C {
70 operator const char *() const;
71};
72
73C::operator const char*() const { return 0; }
74
75void f(const C& c) {
76 const char* v = c;
77}
Fariborz Jahanianb191e2d2009-09-14 20:41:01 +000078
79// Test. Conversion in base class is visible in derived class.
80class XB {
81public:
Fariborz Jahanian78cf9a22009-09-15 00:10:11 +000082 operator int(); // expected-note {{candidate function}}
Fariborz Jahanianb191e2d2009-09-14 20:41:01 +000083};
84
85class Yb : public XB {
86public:
Fariborz Jahanian78cf9a22009-09-15 00:10:11 +000087 operator char(); // expected-note {{candidate function}}
Fariborz Jahanianb191e2d2009-09-14 20:41:01 +000088};
89
90void f(Yb& a) {
John McCall7c2342d2010-03-10 11:27:22 +000091 if (a) { } // expected-error {{conversion from 'Yb' to 'bool' is ambiguous}}
Fariborz Jahanianb191e2d2009-09-14 20:41:01 +000092 int i = a; // OK. calls XB::operator int();
93 char ch = a; // OK. calls Yb::operator char();
94}
95
Douglas Gregor79b680e2009-11-13 18:44:21 +000096// Test conversion + copy construction.
97class AutoPtrRef { };
98
99class AutoPtr {
100 // FIXME: Using 'unavailable' since we do not have access control yet.
101 // FIXME: The error message isn't so good.
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000102 AutoPtr(AutoPtr &) __attribute__((unavailable)); // expected-note{{explicitly marked}}
Douglas Gregor79b680e2009-11-13 18:44:21 +0000103
104public:
105 AutoPtr();
106 AutoPtr(AutoPtrRef);
107
108 operator AutoPtrRef();
109};
110
111AutoPtr make_auto_ptr();
112
113AutoPtr test_auto_ptr(bool Cond) {
114 AutoPtr p1( make_auto_ptr() );
115
116 AutoPtr p;
117 if (Cond)
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000118 return p; // expected-error{{call to deleted constructor}}
Douglas Gregor79b680e2009-11-13 18:44:21 +0000119
120 return AutoPtr();
121}
122
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000123struct A1 {
124 A1(const char *);
125 ~A1();
Douglas Gregor79b680e2009-11-13 18:44:21 +0000126
Douglas Gregor18ef5e22009-12-18 05:02:21 +0000127private:
128 A1(const A1&) __attribute__((unavailable)); // expected-note{{here}}
129};
130
131A1 f() {
132 return "Hello"; // expected-error{{invokes deleted copy constructor}}
133}