1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -S %s -o %t-64.s
2*f4a2713aSLionel Sambuc // RUN: FileCheck -check-prefix CHECK-LP64 --input-file=%t-64.s %s
3*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple i386-apple-darwin -std=c++11 -S %s -o %t-32.s
4*f4a2713aSLionel Sambuc // RUN: FileCheck -check-prefix CHECK-LP32 --input-file=%t-32.s %s
5*f4a2713aSLionel Sambuc // XFAIL: *
6*f4a2713aSLionel Sambuc extern "C" int printf(...);
7*f4a2713aSLionel Sambuc struct S {
8*f4a2713aSLionel Sambuc operator int();
9*f4a2713aSLionel Sambuc };
10*f4a2713aSLionel Sambuc
operator int()11*f4a2713aSLionel Sambuc S::operator int() {
12*f4a2713aSLionel Sambuc return 10;
13*f4a2713aSLionel Sambuc }
14*f4a2713aSLionel Sambuc
f(S s)15*f4a2713aSLionel Sambuc int f(S s) {
16*f4a2713aSLionel Sambuc return s;
17*f4a2713aSLionel Sambuc }
18*f4a2713aSLionel Sambuc
19*f4a2713aSLionel Sambuc class X { // ...
operator int()20*f4a2713aSLionel Sambuc public: operator int() { printf("operator int()\n"); return iX; }
operator float()21*f4a2713aSLionel Sambuc public: operator float() { printf("operator float()\n"); return fX; }
X()22*f4a2713aSLionel Sambuc X() : iX(100), fX(1.234) {}
23*f4a2713aSLionel Sambuc int iX;
24*f4a2713aSLionel Sambuc float fX;
25*f4a2713aSLionel Sambuc };
26*f4a2713aSLionel Sambuc
27*f4a2713aSLionel Sambuc X x;
28*f4a2713aSLionel Sambuc
29*f4a2713aSLionel Sambuc struct Z {
operator XZ30*f4a2713aSLionel Sambuc operator X() { printf("perator X()\n"); x.iX += iZ; x.fX += fZ; return x; }
31*f4a2713aSLionel Sambuc int iZ;
32*f4a2713aSLionel Sambuc float fZ;
ZZ33*f4a2713aSLionel Sambuc Z() : iZ(1), fZ(1.00) {}
34*f4a2713aSLionel Sambuc };
35*f4a2713aSLionel Sambuc
36*f4a2713aSLionel Sambuc Z z;
37*f4a2713aSLionel Sambuc
38*f4a2713aSLionel Sambuc class Y { // ...
operator Z()39*f4a2713aSLionel Sambuc public: operator Z(){printf("perator Z()\n"); return z; }
40*f4a2713aSLionel Sambuc };
41*f4a2713aSLionel Sambuc
42*f4a2713aSLionel Sambuc Y y;
43*f4a2713aSLionel Sambuc
44*f4a2713aSLionel Sambuc int count=0;
45*f4a2713aSLionel Sambuc class O { // ...
46*f4a2713aSLionel Sambuc public:
operator int()47*f4a2713aSLionel Sambuc operator int(){ return ++iO; }
O()48*f4a2713aSLionel Sambuc O() : iO(count++) {}
49*f4a2713aSLionel Sambuc int iO;
50*f4a2713aSLionel Sambuc };
51*f4a2713aSLionel Sambuc
g(O a,O b)52*f4a2713aSLionel Sambuc void g(O a, O b) {
53*f4a2713aSLionel Sambuc int i = (a) ? 1+a : 0;
54*f4a2713aSLionel Sambuc int j = (a&&b) ? a+b : i;
55*f4a2713aSLionel Sambuc if (a) { }
56*f4a2713aSLionel Sambuc printf("i = %d j = %d a.iO = %d b.iO = %d\n", i, j, a.iO, b.iO);
57*f4a2713aSLionel Sambuc }
58*f4a2713aSLionel Sambuc
main()59*f4a2713aSLionel Sambuc int main() {
60*f4a2713aSLionel Sambuc int c = X(Z(y)); // OK: y.operator Z().operator X().operator int()
61*f4a2713aSLionel Sambuc printf("c = %d\n", c);
62*f4a2713aSLionel Sambuc float f = X(Z(y));
63*f4a2713aSLionel Sambuc printf("f = %f\n", f);
64*f4a2713aSLionel Sambuc int i = x;
65*f4a2713aSLionel Sambuc printf("i = %d float = %f\n", i, float(x));
66*f4a2713aSLionel Sambuc i = int(X(Z(y)));
67*f4a2713aSLionel Sambuc f = float(X(Z(y)));
68*f4a2713aSLionel Sambuc printf("i = %d float = %f\n", i,f);
69*f4a2713aSLionel Sambuc f = (float)x;
70*f4a2713aSLionel Sambuc i = (int)x;
71*f4a2713aSLionel Sambuc printf("i = %d float = %f\n", i,f);
72*f4a2713aSLionel Sambuc
73*f4a2713aSLionel Sambuc int d = (X)((Z)y);
74*f4a2713aSLionel Sambuc printf("d = %d\n", d);
75*f4a2713aSLionel Sambuc
76*f4a2713aSLionel Sambuc int e = (int)((X)((Z)y));
77*f4a2713aSLionel Sambuc printf("e = %d\n", e);
78*f4a2713aSLionel Sambuc O o1, o2;
79*f4a2713aSLionel Sambuc g(o1, o2);
80*f4a2713aSLionel Sambuc }
81*f4a2713aSLionel Sambuc
82*f4a2713aSLionel Sambuc // Test. Conversion in base class is visible in derived class.
83*f4a2713aSLionel Sambuc class XB {
84*f4a2713aSLionel Sambuc int a;
85*f4a2713aSLionel Sambuc public:
86*f4a2713aSLionel Sambuc operator int();
87*f4a2713aSLionel Sambuc };
88*f4a2713aSLionel Sambuc
89*f4a2713aSLionel Sambuc class Yb : public XB {
90*f4a2713aSLionel Sambuc double b;
91*f4a2713aSLionel Sambuc public:
92*f4a2713aSLionel Sambuc operator char();
93*f4a2713aSLionel Sambuc };
94*f4a2713aSLionel Sambuc
f(Yb & a)95*f4a2713aSLionel Sambuc void f(Yb& a) {
96*f4a2713aSLionel Sambuc int i = a; // OK. calls XB::operator int();
97*f4a2713aSLionel Sambuc char ch = a; // OK. calls Yb::operator char();
98*f4a2713aSLionel Sambuc }
99*f4a2713aSLionel Sambuc
100*f4a2713aSLionel Sambuc struct A {
101*f4a2713aSLionel Sambuc operator int() const;
102*f4a2713aSLionel Sambuc };
103*f4a2713aSLionel Sambuc
104*f4a2713aSLionel Sambuc // CHECK-LP64: .globl __ZN1ScviEv
105*f4a2713aSLionel Sambuc // CHECK-LP64-NEXT: __ZN1ScviEv:
106*f4a2713aSLionel Sambuc // CHECK-LP64: callq __ZN1Ycv1ZEv
107*f4a2713aSLionel Sambuc // CHECK-LP64: callq __ZN1Zcv1XEv
108*f4a2713aSLionel Sambuc // CHECK-LP64: callq __ZN1XcviEv
109*f4a2713aSLionel Sambuc // CHECK-LP64: callq __ZN1XcvfEv
110*f4a2713aSLionel Sambuc // CHECK-LP64: callq __ZN2XBcviEv
111*f4a2713aSLionel Sambuc // CHECK-LP64: callq __ZN2YbcvcEv
112*f4a2713aSLionel Sambuc
113*f4a2713aSLionel Sambuc // CHECK-LP32: .globl __ZN1ScviEv
114*f4a2713aSLionel Sambuc // CHECK-LP32-NEXT: __ZN1ScviEv:
115*f4a2713aSLionel Sambuc // CHECK-LP32: call L__ZN1Ycv1ZEv
116*f4a2713aSLionel Sambuc // CHECK-LP32: call L__ZN1Zcv1XEv
117*f4a2713aSLionel Sambuc // CHECK-LP32: call L__ZN1XcviEv
118*f4a2713aSLionel Sambuc // CHECK-LP32: call L__ZN1XcvfEv
119*f4a2713aSLionel Sambuc // CHECK-LP32: call L__ZN2XBcviEv
120*f4a2713aSLionel Sambuc // CHECK-LP32: call L__ZN2YbcvcEv
121