]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/CodeGenCXX/conversion-function.cpp
Vendor import of clang 3.9.0 release r280324:
[FreeBSD/FreeBSD.git] / test / CodeGenCXX / conversion-function.cpp
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -S %s -o %t-64.s
2 // RUN: FileCheck -check-prefix CHECK-LP64 --input-file=%t-64.s %s
3 // RUN: %clang_cc1 -triple i386-apple-darwin -std=c++11 -S %s -o %t-32.s
4 // RUN: FileCheck -check-prefix CHECK-LP32 --input-file=%t-32.s %s
5 // XFAIL: *
6 extern "C" int printf(...);
7 struct S {
8   operator int();
9 };
10
11 S::operator int() {
12   return 10;
13 }
14
15 int f(S s) {
16   return s;
17 }
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
44 int count=0;
45 class O { // ...
46 public: 
47   operator int(){ return ++iO; }
48   O() : iO(count++) {}
49   int iO;
50 };
51
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);
57 }
58
59 int main() {
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);
72
73   int d = (X)((Z)y);
74   printf("d = %d\n", d);
75
76   int e = (int)((X)((Z)y));
77   printf("e = %d\n", e);
78   O o1, o2;
79   g(o1, o2);
80 }
81
82 // Test. Conversion in base class is visible in derived class.
83 class XB {
84   int a;
85 public:
86   operator int();
87 };
88
89 class Yb : public XB {
90   double b;
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
100 struct A {
101   operator int() const;
102 };
103
104 // CHECK-LP64: .globl __ZN1ScviEv
105 // CHECK-LP64-NEXT: __ZN1ScviEv:
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
112
113 // CHECK-LP32: .globl  __ZN1ScviEv
114 // CHECK-LP32-NEXT: __ZN1ScviEv:
115 // CHECK-LP32: call L__ZN1Ycv1ZEv
116 // CHECK-LP32: call L__ZN1Zcv1XEv
117 // CHECK-LP32: call L__ZN1XcviEv
118 // CHECK-LP32: call L__ZN1XcvfEv
119 // CHECK-LP32: call L__ZN2XBcviEv
120 // CHECK-LP32: call L__ZN2YbcvcEv