blob: e2f8f7e17b930a07ba6cc53f79471dafa6d0b479 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++0x -S %s -o %t-64.s
Daniel Dunbar4fcfde42009-11-08 01:45:36 +00002// RUN: FileCheck -check-prefix LP64 --input-file=%t-64.s %s
Daniel Dunbara5728872009-12-15 20:14:24 +00003// RUN: %clang_cc1 -triple i386-apple-darwin -std=c++0x -S %s -o %t-32.s
Daniel Dunbar4fcfde42009-11-08 01:45:36 +00004// RUN: FileCheck -check-prefix LP32 --input-file=%t-32.s %s
Anders Carlsson798345c2010-01-27 03:06:00 +00005// XFAIL: *
Fariborz Jahaniand8417782009-08-28 16:23:54 +00006extern "C" int printf(...);
Anders Carlsson293361a2009-08-25 13:14:46 +00007struct S {
Mike Stump1eb44332009-09-09 15:08:12 +00008 operator int();
Anders Carlsson293361a2009-08-25 13:14:46 +00009};
10
Anders Carlsson293361a2009-08-25 13:14:46 +000011S::operator int() {
Mike Stump1eb44332009-09-09 15:08:12 +000012 return 10;
Anders Carlsson293361a2009-08-25 13:14:46 +000013}
Fariborz Jahaniand8417782009-08-28 16:23:54 +000014
Anders Carlsson0ddb6f72009-12-18 14:42:03 +000015int f(S s) {
16 return s;
17}
Fariborz Jahaniand8417782009-08-28 16:23:54 +000018
19class X { // ...
20 public: operator int() { printf("operator int()\n"); return iX; }
21 public: operator float() { printf("operator float()\n"); return fX; }
22 X() : iX(100), fX(1.234) {}
23 int iX;
24 float fX;
25};
26
27X x;
28
29struct Z {
30 operator X() { printf("perator X()\n"); x.iX += iZ; x.fX += fZ; return x; }
31 int iZ;
32 float fZ;
33 Z() : iZ(1), fZ(1.00) {}
34};
35
36Z z;
37
38class Y { // ...
39 public: operator Z(){printf("perator Z()\n"); return z; }
40};
41
42Y y;
43
Fariborz Jahanian383d2982009-08-29 20:33:32 +000044int count=0;
45class O { // ...
46public:
Mike Stump1eb44332009-09-09 15:08:12 +000047 operator int(){ return ++iO; }
48 O() : iO(count++) {}
49 int iO;
Fariborz Jahanian383d2982009-08-29 20:33:32 +000050};
51
Mike Stump1eb44332009-09-09 15:08:12 +000052void g(O a, O b) {
53 int i = (a) ? 1+a : 0;
54 int j = (a&&b) ? a+b : i;
55 if (a) { }
56 printf("i = %d j = %d a.iO = %d b.iO = %d\n", i, j, a.iO, b.iO);
Fariborz Jahanian383d2982009-08-29 20:33:32 +000057}
58
Fariborz Jahaniand8417782009-08-28 16:23:54 +000059int main() {
Mike Stump1eb44332009-09-09 15:08:12 +000060 int c = X(Z(y)); // OK: y.operator Z().operator X().operator int()
61 printf("c = %d\n", c);
62 float f = X(Z(y));
63 printf("f = %f\n", f);
64 int i = x;
65 printf("i = %d float = %f\n", i, float(x));
66 i = int(X(Z(y)));
67 f = float(X(Z(y)));
68 printf("i = %d float = %f\n", i,f);
69 f = (float)x;
70 i = (int)x;
71 printf("i = %d float = %f\n", i,f);
Fariborz Jahanian31976592009-08-29 19:15:16 +000072
Mike Stump1eb44332009-09-09 15:08:12 +000073 int d = (X)((Z)y);
74 printf("d = %d\n", d);
Fariborz Jahanian31976592009-08-29 19:15:16 +000075
Mike Stump1eb44332009-09-09 15:08:12 +000076 int e = (int)((X)((Z)y));
77 printf("e = %d\n", e);
78 O o1, o2;
79 g(o1, o2);
Fariborz Jahaniand8417782009-08-28 16:23:54 +000080}
Mike Stump1eb44332009-09-09 15:08:12 +000081
Fariborz Jahanianb191e2d2009-09-14 20:41:01 +000082// Test. Conversion in base class is visible in derived class.
83class XB {
Anders Carlssonaac6e3a2009-09-15 07:42:44 +000084 int a;
Fariborz Jahanianb191e2d2009-09-14 20:41:01 +000085public:
86 operator int();
87};
88
89class Yb : public XB {
Anders Carlssonaac6e3a2009-09-15 07:42:44 +000090 double b;
Fariborz Jahanianb191e2d2009-09-14 20:41:01 +000091public:
92 operator char();
93};
94
95void f(Yb& a) {
96 int i = a; // OK. calls XB::operator int();
97 char ch = a; // OK. calls Yb::operator char();
98}
99
Anders Carlsson0ddb6f72009-12-18 14:42:03 +0000100struct A {
101 operator int() const;
102};
Fariborz Jahanianb191e2d2009-09-14 20:41:01 +0000103
Mike Stump1eb44332009-09-09 15:08:12 +0000104// CHECK-LP64: .globl __ZN1ScviEv
Fariborz Jahaniand8417782009-08-28 16:23:54 +0000105// CHECK-LP64-NEXT: __ZN1ScviEv:
Sean Callanan3b299012009-12-18 00:04:09 +0000106// CHECK-LP64: callq __ZN1Ycv1ZEv
107// CHECK-LP64: callq __ZN1Zcv1XEv
108// CHECK-LP64: callq __ZN1XcviEv
109// CHECK-LP64: callq __ZN1XcvfEv
110// CHECK-LP64: callq __ZN2XBcviEv
111// CHECK-LP64: callq __ZN2YbcvcEv
Fariborz Jahaniand8417782009-08-28 16:23:54 +0000112
113// CHECK-LP32: .globl __ZN1ScviEv
114// CHECK-LP32-NEXT: __ZN1ScviEv:
Mike Stump1eb44332009-09-09 15:08:12 +0000115// CHECK-LP32: call L__ZN1Ycv1ZEv
116// CHECK-LP32: call L__ZN1Zcv1XEv
117// CHECK-LP32: call L__ZN1XcviEv
118// CHECK-LP32: call L__ZN1XcvfEv
Fariborz Jahanianb191e2d2009-09-14 20:41:01 +0000119// CHECK-LP32: call L__ZN2XBcviEv
120// CHECK-LP32: call L__ZN2YbcvcEv