Fariborz Jahanian | d841778 | 2009-08-28 16:23:54 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -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-cc -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 | // RUN: true |
| 6 | |
| 7 | extern "C" int printf(...); |
Anders Carlsson | 293361a | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 8 | struct S { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9 | operator int(); |
Anders Carlsson | 293361a | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 10 | }; |
| 11 | |
Anders Carlsson | 293361a | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 12 | S::operator int() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13 | return 10; |
Anders Carlsson | 293361a | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 14 | } |
Fariborz Jahanian | d841778 | 2009-08-28 16:23:54 +0000 | [diff] [blame] | 15 | |
| 16 | |
| 17 | class X { // ... |
| 18 | public: operator int() { printf("operator int()\n"); return iX; } |
| 19 | public: operator float() { printf("operator float()\n"); return fX; } |
| 20 | X() : iX(100), fX(1.234) {} |
| 21 | int iX; |
| 22 | float fX; |
| 23 | }; |
| 24 | |
| 25 | X x; |
| 26 | |
| 27 | struct Z { |
| 28 | operator X() { printf("perator X()\n"); x.iX += iZ; x.fX += fZ; return x; } |
| 29 | int iZ; |
| 30 | float fZ; |
| 31 | Z() : iZ(1), fZ(1.00) {} |
| 32 | }; |
| 33 | |
| 34 | Z z; |
| 35 | |
| 36 | class Y { // ... |
| 37 | public: operator Z(){printf("perator Z()\n"); return z; } |
| 38 | }; |
| 39 | |
| 40 | Y y; |
| 41 | |
Fariborz Jahanian | 383d298 | 2009-08-29 20:33:32 +0000 | [diff] [blame] | 42 | int count=0; |
| 43 | class O { // ... |
| 44 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | operator int(){ return ++iO; } |
| 46 | O() : iO(count++) {} |
| 47 | int iO; |
Fariborz Jahanian | 383d298 | 2009-08-29 20:33:32 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | void g(O a, O b) { |
| 51 | int i = (a) ? 1+a : 0; |
| 52 | int j = (a&&b) ? a+b : i; |
| 53 | if (a) { } |
| 54 | printf("i = %d j = %d a.iO = %d b.iO = %d\n", i, j, a.iO, b.iO); |
Fariborz Jahanian | 383d298 | 2009-08-29 20:33:32 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Fariborz Jahanian | d841778 | 2009-08-28 16:23:54 +0000 | [diff] [blame] | 57 | int main() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | int c = X(Z(y)); // OK: y.operator Z().operator X().operator int() |
| 59 | printf("c = %d\n", c); |
| 60 | float f = X(Z(y)); |
| 61 | printf("f = %f\n", f); |
| 62 | int i = x; |
| 63 | printf("i = %d float = %f\n", i, float(x)); |
| 64 | i = int(X(Z(y))); |
| 65 | f = float(X(Z(y))); |
| 66 | printf("i = %d float = %f\n", i,f); |
| 67 | f = (float)x; |
| 68 | i = (int)x; |
| 69 | printf("i = %d float = %f\n", i,f); |
Fariborz Jahanian | 3197659 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 70 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | int d = (X)((Z)y); |
| 72 | printf("d = %d\n", d); |
Fariborz Jahanian | 3197659 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 73 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | int e = (int)((X)((Z)y)); |
| 75 | printf("e = %d\n", e); |
| 76 | O o1, o2; |
| 77 | g(o1, o2); |
Fariborz Jahanian | d841778 | 2009-08-28 16:23:54 +0000 | [diff] [blame] | 78 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame^] | 80 | // Test. Conversion in base class is visible in derived class. |
| 81 | class XB { |
| 82 | public: |
| 83 | operator int(); |
| 84 | }; |
| 85 | |
| 86 | class Yb : public XB { |
| 87 | public: |
| 88 | operator char(); |
| 89 | }; |
| 90 | |
| 91 | void f(Yb& a) { |
| 92 | int i = a; // OK. calls XB::operator int(); |
| 93 | char ch = a; // OK. calls Yb::operator char(); |
| 94 | } |
| 95 | |
| 96 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | // CHECK-LP64: .globl __ZN1ScviEv |
Fariborz Jahanian | d841778 | 2009-08-28 16:23:54 +0000 | [diff] [blame] | 98 | // CHECK-LP64-NEXT: __ZN1ScviEv: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | // CHECK-LP64: call __ZN1Ycv1ZEv |
| 100 | // CHECK-LP64: call __ZN1Zcv1XEv |
| 101 | // CHECK-LP64: call __ZN1XcviEv |
| 102 | // CHECK-LP64: call __ZN1XcvfEv |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame^] | 103 | // CHECK-LP64: call __ZN2XBcviEv |
| 104 | // CHECK-LP64: call __ZN2YbcvcEv |
Fariborz Jahanian | d841778 | 2009-08-28 16:23:54 +0000 | [diff] [blame] | 105 | |
| 106 | // CHECK-LP32: .globl __ZN1ScviEv |
| 107 | // CHECK-LP32-NEXT: __ZN1ScviEv: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | // CHECK-LP32: call L__ZN1Ycv1ZEv |
| 109 | // CHECK-LP32: call L__ZN1Zcv1XEv |
| 110 | // CHECK-LP32: call L__ZN1XcviEv |
| 111 | // CHECK-LP32: call L__ZN1XcvfEv |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame^] | 112 | // CHECK-LP32: call L__ZN2XBcviEv |
| 113 | // CHECK-LP32: call L__ZN2YbcvcEv |