Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++0x -S %s -o %t-64.s |
Daniel Dunbar | 4fcfde4 | 2009-11-08 01:45:36 +0000 | [diff] [blame] | 2 | // RUN: FileCheck -check-prefix LP64 --input-file=%t-64.s %s |
Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 3 | // RUN: %clang_cc1 -triple i386-apple-darwin -std=c++0x -S %s -o %t-32.s |
Daniel Dunbar | 4fcfde4 | 2009-11-08 01:45:36 +0000 | [diff] [blame] | 4 | // RUN: FileCheck -check-prefix LP32 --input-file=%t-32.s %s |
Anders Carlsson | 798345c | 2010-01-27 03:06:00 +0000 | [diff] [blame] | 5 | // XFAIL: * |
Fariborz Jahanian | d841778 | 2009-08-28 16:23:54 +0000 | [diff] [blame] | 6 | extern "C" int printf(...); |
Anders Carlsson | 293361a | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 7 | struct S { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8 | operator int(); |
Anders Carlsson | 293361a | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 9 | }; |
| 10 | |
Anders Carlsson | 293361a | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 11 | S::operator int() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12 | return 10; |
Anders Carlsson | 293361a | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 13 | } |
Fariborz Jahanian | d841778 | 2009-08-28 16:23:54 +0000 | [diff] [blame] | 14 | |
Anders Carlsson | 0ddb6f7 | 2009-12-18 14:42:03 +0000 | [diff] [blame] | 15 | int f(S s) { |
| 16 | return s; |
| 17 | } |
Fariborz Jahanian | d841778 | 2009-08-28 16:23:54 +0000 | [diff] [blame] | 18 | |
| 19 | class 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 | |
| 27 | X x; |
| 28 | |
| 29 | struct 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 | |
| 36 | Z z; |
| 37 | |
| 38 | class Y { // ... |
| 39 | public: operator Z(){printf("perator Z()\n"); return z; } |
| 40 | }; |
| 41 | |
| 42 | Y y; |
| 43 | |
Fariborz Jahanian | 383d298 | 2009-08-29 20:33:32 +0000 | [diff] [blame] | 44 | int count=0; |
| 45 | class O { // ... |
| 46 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | operator int(){ return ++iO; } |
| 48 | O() : iO(count++) {} |
| 49 | int iO; |
Fariborz Jahanian | 383d298 | 2009-08-29 20:33:32 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | void 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 Jahanian | 383d298 | 2009-08-29 20:33:32 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Fariborz Jahanian | d841778 | 2009-08-28 16:23:54 +0000 | [diff] [blame] | 59 | int main() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | 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 Jahanian | 3197659 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 72 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | int d = (X)((Z)y); |
| 74 | printf("d = %d\n", d); |
Fariborz Jahanian | 3197659 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 75 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | int e = (int)((X)((Z)y)); |
| 77 | printf("e = %d\n", e); |
| 78 | O o1, o2; |
| 79 | g(o1, o2); |
Fariborz Jahanian | d841778 | 2009-08-28 16:23:54 +0000 | [diff] [blame] | 80 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 82 | // Test. Conversion in base class is visible in derived class. |
| 83 | class XB { |
Anders Carlsson | aac6e3a | 2009-09-15 07:42:44 +0000 | [diff] [blame] | 84 | int a; |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 85 | public: |
| 86 | operator int(); |
| 87 | }; |
| 88 | |
| 89 | class Yb : public XB { |
Anders Carlsson | aac6e3a | 2009-09-15 07:42:44 +0000 | [diff] [blame] | 90 | double b; |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 91 | public: |
| 92 | operator char(); |
| 93 | }; |
| 94 | |
| 95 | void f(Yb& a) { |
| 96 | int i = a; // OK. calls XB::operator int(); |
| 97 | char ch = a; // OK. calls Yb::operator char(); |
| 98 | } |
| 99 | |
Anders Carlsson | 0ddb6f7 | 2009-12-18 14:42:03 +0000 | [diff] [blame] | 100 | struct A { |
| 101 | operator int() const; |
| 102 | }; |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 103 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | // CHECK-LP64: .globl __ZN1ScviEv |
Fariborz Jahanian | d841778 | 2009-08-28 16:23:54 +0000 | [diff] [blame] | 105 | // CHECK-LP64-NEXT: __ZN1ScviEv: |
Sean Callanan | 3b29901 | 2009-12-18 00:04:09 +0000 | [diff] [blame] | 106 | // 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 Jahanian | d841778 | 2009-08-28 16:23:54 +0000 | [diff] [blame] | 112 | |
| 113 | // CHECK-LP32: .globl __ZN1ScviEv |
| 114 | // CHECK-LP32-NEXT: __ZN1ScviEv: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | // CHECK-LP32: call L__ZN1Ycv1ZEv |
| 116 | // CHECK-LP32: call L__ZN1Zcv1XEv |
| 117 | // CHECK-LP32: call L__ZN1XcviEv |
| 118 | // CHECK-LP32: call L__ZN1XcvfEv |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 119 | // CHECK-LP32: call L__ZN2XBcviEv |
| 120 | // CHECK-LP32: call L__ZN2YbcvcEv |