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