xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/reference-cast.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -emit-llvm -triple x86_64-apple-darwin10 -o - %s | FileCheck %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // PR6024
4*f4a2713aSLionel Sambuc extern int i;
5*f4a2713aSLionel Sambuc 
6*f4a2713aSLionel Sambuc // CHECK: define i32* @_Z16lvalue_noop_castv() [[NUW:#[0-9]+]]
7*f4a2713aSLionel Sambuc const int &lvalue_noop_cast() {
8*f4a2713aSLionel Sambuc   if (i == 0)
9*f4a2713aSLionel Sambuc     // CHECK: store i32 17, i32*
10*f4a2713aSLionel Sambuc     return (const int&)17;
11*f4a2713aSLionel Sambuc   else if (i == 1)
12*f4a2713aSLionel Sambuc     // CHECK: store i32 17, i32*
13*f4a2713aSLionel Sambuc     return static_cast<const int&>(17);
14*f4a2713aSLionel Sambuc     // CHECK: store i32 17, i32*
15*f4a2713aSLionel Sambuc   return 17;
16*f4a2713aSLionel Sambuc }
17*f4a2713aSLionel Sambuc 
18*f4a2713aSLionel Sambuc // CHECK-LABEL: define i16* @_Z20lvalue_integral_castv()
19*f4a2713aSLionel Sambuc const short &lvalue_integral_cast() {
20*f4a2713aSLionel Sambuc   if (i == 0)
21*f4a2713aSLionel Sambuc     // CHECK: store i16 17, i16*
22*f4a2713aSLionel Sambuc     return (const short&)17;
23*f4a2713aSLionel Sambuc   else if (i == 1)
24*f4a2713aSLionel Sambuc     // CHECK: store i16 17, i16*
25*f4a2713aSLionel Sambuc     return static_cast<const short&>(17);
26*f4a2713aSLionel Sambuc   // CHECK: store i16 17, i16*
27*f4a2713aSLionel Sambuc   return 17;
28*f4a2713aSLionel Sambuc }
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc // CHECK-LABEL: define i16* @_Z29lvalue_floating_integral_castv()
31*f4a2713aSLionel Sambuc const short &lvalue_floating_integral_cast() {
32*f4a2713aSLionel Sambuc   if (i == 0)
33*f4a2713aSLionel Sambuc     // CHECK: store i16 17, i16*
34*f4a2713aSLionel Sambuc     return (const short&)17.5;
35*f4a2713aSLionel Sambuc   else if (i == 1)
36*f4a2713aSLionel Sambuc     // CHECK: store i16 17, i16*
37*f4a2713aSLionel Sambuc     return static_cast<const short&>(17.5);
38*f4a2713aSLionel Sambuc   // CHECK: store i16 17, i16*
39*f4a2713aSLionel Sambuc   return 17.5;
40*f4a2713aSLionel Sambuc }
41*f4a2713aSLionel Sambuc 
42*f4a2713aSLionel Sambuc // CHECK-LABEL: define float* @_Z29lvalue_integral_floating_castv()
43*f4a2713aSLionel Sambuc const float &lvalue_integral_floating_cast() {
44*f4a2713aSLionel Sambuc   if (i == 0)
45*f4a2713aSLionel Sambuc     // CHECK: store float 1.700000e+{{0*}}1, float*
46*f4a2713aSLionel Sambuc     return (const float&)17;
47*f4a2713aSLionel Sambuc   else if (i == 1)
48*f4a2713aSLionel Sambuc     // CHECK: store float 1.700000e+{{0*}}1, float*
49*f4a2713aSLionel Sambuc     return static_cast<const float&>(17);
50*f4a2713aSLionel Sambuc   // CHECK: store float 1.700000e+{{0*}}1, float*
51*f4a2713aSLionel Sambuc   return 17;
52*f4a2713aSLionel Sambuc }
53*f4a2713aSLionel Sambuc 
54*f4a2713aSLionel Sambuc // CHECK-LABEL: define float* @_Z20lvalue_floating_castv()
55*f4a2713aSLionel Sambuc const float &lvalue_floating_cast() {
56*f4a2713aSLionel Sambuc   if (i == 0)
57*f4a2713aSLionel Sambuc     // CHECK: store float 1.700000e+{{0*}}1, float*
58*f4a2713aSLionel Sambuc     return (const float&)17.0;
59*f4a2713aSLionel Sambuc   else if (i == 1)
60*f4a2713aSLionel Sambuc     // CHECK: store float 1.700000e+{{0*}}1, float*
61*f4a2713aSLionel Sambuc     return static_cast<const float&>(17.0);
62*f4a2713aSLionel Sambuc   // CHECK: store float 1.700000e+{{0*}}1, float*
63*f4a2713aSLionel Sambuc   return 17.0;
64*f4a2713aSLionel Sambuc }
65*f4a2713aSLionel Sambuc 
66*f4a2713aSLionel Sambuc int get_int();
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc // CHECK-LABEL: define i8* @_Z24lvalue_integer_bool_castv()
69*f4a2713aSLionel Sambuc const bool &lvalue_integer_bool_cast() {
70*f4a2713aSLionel Sambuc   if (i == 0)
71*f4a2713aSLionel Sambuc     // CHECK: call i32 @_Z7get_intv()
72*f4a2713aSLionel Sambuc     // CHECK: store i8
73*f4a2713aSLionel Sambuc     return (const bool&)get_int();
74*f4a2713aSLionel Sambuc   else if (i == 1)
75*f4a2713aSLionel Sambuc     // CHECK: call i32 @_Z7get_intv()
76*f4a2713aSLionel Sambuc     // CHECK: store i8
77*f4a2713aSLionel Sambuc     return static_cast<const bool&>(get_int());
78*f4a2713aSLionel Sambuc   // CHECK: call i32 @_Z7get_intv()
79*f4a2713aSLionel Sambuc   // CHECK: store i8
80*f4a2713aSLionel Sambuc   return get_int();
81*f4a2713aSLionel Sambuc }
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc float get_float();
84*f4a2713aSLionel Sambuc 
85*f4a2713aSLionel Sambuc // CHECK-LABEL: define i8* @_Z25lvalue_floating_bool_castv()
86*f4a2713aSLionel Sambuc const bool &lvalue_floating_bool_cast() {
87*f4a2713aSLionel Sambuc   if (i == 0)
88*f4a2713aSLionel Sambuc     // CHECK: call float @_Z9get_floatv()
89*f4a2713aSLionel Sambuc     // CHECK: fcmp une float
90*f4a2713aSLionel Sambuc     // CHECK: store i8
91*f4a2713aSLionel Sambuc     return (const bool&)get_float();
92*f4a2713aSLionel Sambuc   else if (i == 1)
93*f4a2713aSLionel Sambuc     // CHECK: call float @_Z9get_floatv()
94*f4a2713aSLionel Sambuc     // CHECK: fcmp une float
95*f4a2713aSLionel Sambuc     // CHECK: store i8
96*f4a2713aSLionel Sambuc     return static_cast<const bool&>(get_float());
97*f4a2713aSLionel Sambuc   // CHECK: call float @_Z9get_floatv()
98*f4a2713aSLionel Sambuc   // CHECK: fcmp une float
99*f4a2713aSLionel Sambuc   // CHECK: store i8
100*f4a2713aSLionel Sambuc   return get_float();
101*f4a2713aSLionel Sambuc }
102*f4a2713aSLionel Sambuc 
103*f4a2713aSLionel Sambuc struct X { };
104*f4a2713aSLionel Sambuc typedef int X::*pm;
105*f4a2713aSLionel Sambuc typedef int (X::*pmf)(int);
106*f4a2713aSLionel Sambuc 
107*f4a2713aSLionel Sambuc pm get_pointer_to_member_data();
108*f4a2713aSLionel Sambuc pmf get_pointer_to_member_function();
109*f4a2713aSLionel Sambuc 
110*f4a2713aSLionel Sambuc // CHECK-LABEL: define i8* @_Z26lvalue_ptrmem_to_bool_castv()
111*f4a2713aSLionel Sambuc const bool &lvalue_ptrmem_to_bool_cast() {
112*f4a2713aSLionel Sambuc   if (i == 0)
113*f4a2713aSLionel Sambuc     // CHECK: call i64 @_Z26get_pointer_to_member_datav()
114*f4a2713aSLionel Sambuc     // CHECK: store i8
115*f4a2713aSLionel Sambuc     // CHECK: store i8*
116*f4a2713aSLionel Sambuc     return (const bool&)get_pointer_to_member_data();
117*f4a2713aSLionel Sambuc   else if (i == 1)
118*f4a2713aSLionel Sambuc     // CHECK: call i64 @_Z26get_pointer_to_member_datav()
119*f4a2713aSLionel Sambuc     // CHECK: store i8
120*f4a2713aSLionel Sambuc     // CHECK: store i8*
121*f4a2713aSLionel Sambuc     return static_cast<const bool&>(get_pointer_to_member_data());
122*f4a2713aSLionel Sambuc   // CHECK: call i64 @_Z26get_pointer_to_member_datav()
123*f4a2713aSLionel Sambuc   // CHECK: store i8
124*f4a2713aSLionel Sambuc   // CHECK: store i8*
125*f4a2713aSLionel Sambuc   return get_pointer_to_member_data();
126*f4a2713aSLionel Sambuc }
127*f4a2713aSLionel Sambuc 
128*f4a2713aSLionel Sambuc // CHECK-LABEL: define i8* @_Z27lvalue_ptrmem_to_bool_cast2v
129*f4a2713aSLionel Sambuc const bool &lvalue_ptrmem_to_bool_cast2() {
130*f4a2713aSLionel Sambuc   if (i == 0)
131*f4a2713aSLionel Sambuc     // CHECK: {{call.*_Z30get_pointer_to_member_functionv}}
132*f4a2713aSLionel Sambuc     // CHECK: store i8
133*f4a2713aSLionel Sambuc     // CHECK: store i8*
134*f4a2713aSLionel Sambuc     return (const bool&)get_pointer_to_member_function();
135*f4a2713aSLionel Sambuc   else if (i == 1)
136*f4a2713aSLionel Sambuc     // CHECK: {{call.*_Z30get_pointer_to_member_functionv}}
137*f4a2713aSLionel Sambuc     // CHECK: store i8
138*f4a2713aSLionel Sambuc     // CHECK: store i8*
139*f4a2713aSLionel Sambuc     return static_cast<const bool&>(get_pointer_to_member_function());
140*f4a2713aSLionel Sambuc   // CHECK: {{call.*_Z30get_pointer_to_member_functionv}}
141*f4a2713aSLionel Sambuc   // CHECK: store i8
142*f4a2713aSLionel Sambuc   // CHECK: store i8*
143*f4a2713aSLionel Sambuc   return get_pointer_to_member_function();
144*f4a2713aSLionel Sambuc }
145*f4a2713aSLionel Sambuc 
146*f4a2713aSLionel Sambuc _Complex double get_complex_double();
147*f4a2713aSLionel Sambuc 
148*f4a2713aSLionel Sambuc // CHECK: {{define.*_Z2f1v}}
149*f4a2713aSLionel Sambuc const _Complex float &f1() {
150*f4a2713aSLionel Sambuc   if (i == 0)
151*f4a2713aSLionel Sambuc     // CHECK: {{call.*_Z18get_complex_doublev}}
152*f4a2713aSLionel Sambuc     // CHECK: fptrunc
153*f4a2713aSLionel Sambuc     // CHECK: fptrunc
154*f4a2713aSLionel Sambuc     // CHECK: store float
155*f4a2713aSLionel Sambuc     // CHECK: store float
156*f4a2713aSLionel Sambuc     return (const _Complex float&)get_complex_double();
157*f4a2713aSLionel Sambuc   else if (i == 1)
158*f4a2713aSLionel Sambuc     // CHECK: {{call.*_Z18get_complex_doublev}}
159*f4a2713aSLionel Sambuc     // CHECK: fptrunc
160*f4a2713aSLionel Sambuc     // CHECK: fptrunc
161*f4a2713aSLionel Sambuc     // CHECK: store float
162*f4a2713aSLionel Sambuc     // CHECK: store float
163*f4a2713aSLionel Sambuc     return static_cast<const _Complex float&>(get_complex_double());
164*f4a2713aSLionel Sambuc   // CHECK: {{call.*_Z18get_complex_doublev}}
165*f4a2713aSLionel Sambuc   // CHECK: fptrunc
166*f4a2713aSLionel Sambuc   // CHECK: fptrunc
167*f4a2713aSLionel Sambuc   // CHECK: store float
168*f4a2713aSLionel Sambuc   // CHECK: store float
169*f4a2713aSLionel Sambuc   return get_complex_double();
170*f4a2713aSLionel Sambuc }
171*f4a2713aSLionel Sambuc 
172*f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @_Z7pr10592RKi(i32*
173*f4a2713aSLionel Sambuc unsigned pr10592(const int &v) {
174*f4a2713aSLionel Sambuc   // CHECK: [[VADDR:%[a-zA-Z0-9.]+]] = alloca i32*
175*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[REFTMP:%[a-zA-Z0-9.]+]] = alloca i32
176*f4a2713aSLionel Sambuc   // CHECK-NEXT: store i32* [[V:%[a-zA-Z0-9.]+]], i32** [[VADDR]]
177*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[VADDR_1:%[a-zA-Z0-9.]+]] = load i32** [[VADDR]]
178*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[VVAL:%[a-zA-Z0-9.]+]] = load i32* [[VADDR_1]]
179*f4a2713aSLionel Sambuc   // CHECK-NEXT: store i32 [[VVAL]], i32* [[REFTMP]]
180*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[VVAL_I:%[a-zA-Z0-9.]+]] = load i32* [[REFTMP]]
181*f4a2713aSLionel Sambuc   // CHECK-NEXT: ret i32 [[VVAL_I]]
182*f4a2713aSLionel Sambuc   return static_cast<const unsigned &>(v);
183*f4a2713aSLionel Sambuc }
184*f4a2713aSLionel Sambuc 
185*f4a2713aSLionel Sambuc namespace PR10650 {
186*f4a2713aSLionel Sambuc   struct Helper {
187*f4a2713aSLionel Sambuc     unsigned long long id();
188*f4a2713aSLionel Sambuc   };
189*f4a2713aSLionel Sambuc   unsigned long long test(Helper *obj) {
190*f4a2713aSLionel Sambuc     return static_cast<const unsigned long long&>(obj->id());
191*f4a2713aSLionel Sambuc   }
192*f4a2713aSLionel Sambuc   // CHECK-LABEL: define i64 @_ZN7PR106504testEPNS_6HelperE
193*f4a2713aSLionel Sambuc   // CHECK: store i64
194*f4a2713aSLionel Sambuc }
195*f4a2713aSLionel Sambuc 
196*f4a2713aSLionel Sambuc // CHECK: attributes [[NUW]] = { nounwind{{.*}} }
197