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