class.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/interop/cpp/reverse/class.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/interop/cpp/reverse/class.carbon
  12. // --- pointer.carbon
  13. library "[[@TEST_NAME]]";
  14. import Cpp;
  15. class A {
  16. var x: i32;
  17. var y: i32;
  18. }
  19. inline Cpp '''
  20. Carbon::A *pass(Carbon::A *p) { return p; }
  21. void call() { pass(nullptr); }
  22. ''';
  23. fn DoIt() { Cpp.call(); }
  24. // --- layout.carbon
  25. library "[[@TEST_NAME]]";
  26. import Cpp;
  27. base class A {
  28. var x: ()*;
  29. var y: i32;
  30. }
  31. class B {
  32. var a: A;
  33. var b: i32;
  34. }
  35. class C {
  36. extend base: A;
  37. var c: i32;
  38. }
  39. fn CallA(a: A*);
  40. fn CallB(b: B*);
  41. fn CallC(b: C*);
  42. inline Cpp '''
  43. void f() {
  44. Carbon::A a;
  45. Carbon::CallA(&a);
  46. }
  47. void g() {
  48. Carbon::B b;
  49. Carbon::CallB(&b);
  50. }
  51. void h() {
  52. Carbon::C c;
  53. Carbon::CallC(&c);
  54. Carbon::CallA(&c);
  55. }
  56. ''';
  57. // CHECK:STDOUT: ; ModuleID = 'pointer.carbon'
  58. // CHECK:STDOUT: source_filename = "pointer.carbon"
  59. // CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
  60. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  61. // CHECK:STDOUT:
  62. // CHECK:STDOUT: ; Function Attrs: mustprogress nounwind uwtable
  63. // CHECK:STDOUT: define dso_local noundef ptr @_Z4passPN6Carbon1AE(ptr noundef %p) #0 {
  64. // CHECK:STDOUT: entry:
  65. // CHECK:STDOUT: %p.addr = alloca ptr, align 8
  66. // CHECK:STDOUT: store ptr %p, ptr %p.addr, align 8, !tbaa !11
  67. // CHECK:STDOUT: %0 = load ptr, ptr %p.addr, align 8, !tbaa !11
  68. // CHECK:STDOUT: ret ptr %0
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: ; Function Attrs: mustprogress nounwind uwtable
  72. // CHECK:STDOUT: define dso_local void @_Z4callv() #0 {
  73. // CHECK:STDOUT: entry:
  74. // CHECK:STDOUT: %call = call noundef ptr @_Z4passPN6Carbon1AE(ptr noundef null)
  75. // CHECK:STDOUT: ret void
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: ; Function Attrs: nounwind
  79. // CHECK:STDOUT: define void @_CDoIt.Main() #1 !dbg !14 {
  80. // CHECK:STDOUT: entry:
  81. // CHECK:STDOUT: call void @_Z4callv(), !dbg !17
  82. // CHECK:STDOUT: ret void, !dbg !18
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: attributes #0 = { mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
  86. // CHECK:STDOUT: attributes #1 = { nounwind }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  89. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  90. // CHECK:STDOUT: !llvm.errno.tbaa = !{!7}
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  93. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  94. // CHECK:STDOUT: !2 = !{i32 8, !"PIC Level", i32 2}
  95. // CHECK:STDOUT: !3 = !{i32 7, !"PIE Level", i32 2}
  96. // CHECK:STDOUT: !4 = !{i32 7, !"uwtable", i32 2}
  97. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  98. // CHECK:STDOUT: !6 = !DIFile(filename: "pointer.carbon", directory: "")
  99. // CHECK:STDOUT: !7 = !{!8, !8, i64 0}
  100. // CHECK:STDOUT: !8 = !{!"int", !9, i64 0}
  101. // CHECK:STDOUT: !9 = !{!"omnipotent char", !10, i64 0}
  102. // CHECK:STDOUT: !10 = !{!"Simple C++ TBAA"}
  103. // CHECK:STDOUT: !11 = !{!12, !12, i64 0}
  104. // CHECK:STDOUT: !12 = !{!"p1 _ZTSN6Carbon1AE", !13, i64 0}
  105. // CHECK:STDOUT: !13 = !{!"any pointer", !9, i64 0}
  106. // CHECK:STDOUT: !14 = distinct !DISubprogram(name: "DoIt", linkageName: "_CDoIt.Main", scope: null, file: !6, line: 15, type: !15, spFlags: DISPFlagDefinition, unit: !5)
  107. // CHECK:STDOUT: !15 = !DISubroutineType(types: !16)
  108. // CHECK:STDOUT: !16 = !{null}
  109. // CHECK:STDOUT: !17 = !DILocation(line: 15, column: 13, scope: !14)
  110. // CHECK:STDOUT: !18 = !DILocation(line: 15, column: 1, scope: !14)
  111. // CHECK:STDOUT: ; ModuleID = 'layout.carbon'
  112. // CHECK:STDOUT: source_filename = "layout.carbon"
  113. // CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
  114. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: %"class.Carbon::A" = type { [12 x i8] }
  117. // CHECK:STDOUT: %"class.Carbon::B" = type { [16 x i8] }
  118. // CHECK:STDOUT: %"class.Carbon::C" = type { [16 x i8] }
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: ; Function Attrs: mustprogress uwtable
  121. // CHECK:STDOUT: define dso_local void @_Z1fv() #0 {
  122. // CHECK:STDOUT: entry:
  123. // CHECK:STDOUT: %a = alloca %"class.Carbon::A", align 8
  124. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %a) #3
  125. // CHECK:STDOUT: call void @_ZN6CarbonL5CallAEPNS_1AE(ptr noundef %a)
  126. // CHECK:STDOUT: call void @llvm.lifetime.end.p0(ptr %a) #3
  127. // CHECK:STDOUT: ret void
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
  131. // CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #1
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: ; Function Attrs: alwaysinline mustprogress nounwind uwtable
  134. // CHECK:STDOUT: define internal void @_ZN6CarbonL5CallAEPNS_1AE(ptr noundef %0) #2 {
  135. // CHECK:STDOUT: entry:
  136. // CHECK:STDOUT: %.addr = alloca ptr, align 8
  137. // CHECK:STDOUT: store ptr %0, ptr %.addr, align 8, !tbaa !11
  138. // CHECK:STDOUT: call void @_CCallA__carbon_thunk.Main(ptr noundef nonnull align 8 dereferenceable(8) %.addr)
  139. // CHECK:STDOUT: ret void
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
  143. // CHECK:STDOUT: declare void @llvm.lifetime.end.p0(ptr captures(none)) #1
  144. // CHECK:STDOUT:
  145. // CHECK:STDOUT: ; Function Attrs: mustprogress uwtable
  146. // CHECK:STDOUT: define dso_local void @_Z1gv() #0 {
  147. // CHECK:STDOUT: entry:
  148. // CHECK:STDOUT: %b = alloca %"class.Carbon::B", align 8
  149. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %b) #3
  150. // CHECK:STDOUT: call void @_ZN6CarbonL5CallBEPNS_1BE(ptr noundef %b)
  151. // CHECK:STDOUT: call void @llvm.lifetime.end.p0(ptr %b) #3
  152. // CHECK:STDOUT: ret void
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: ; Function Attrs: alwaysinline mustprogress nounwind uwtable
  156. // CHECK:STDOUT: define internal void @_ZN6CarbonL5CallBEPNS_1BE(ptr noundef %0) #2 {
  157. // CHECK:STDOUT: entry:
  158. // CHECK:STDOUT: %.addr = alloca ptr, align 8
  159. // CHECK:STDOUT: store ptr %0, ptr %.addr, align 8, !tbaa !14
  160. // CHECK:STDOUT: call void @_CCallB__carbon_thunk.Main(ptr noundef nonnull align 8 dereferenceable(8) %.addr)
  161. // CHECK:STDOUT: ret void
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: ; Function Attrs: mustprogress uwtable
  165. // CHECK:STDOUT: define dso_local void @_Z1hv() #0 {
  166. // CHECK:STDOUT: entry:
  167. // CHECK:STDOUT: %c = alloca %"class.Carbon::C", align 8
  168. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %c) #3
  169. // CHECK:STDOUT: call void @_ZN6CarbonL5CallCEPNS_1CE(ptr noundef %c)
  170. // CHECK:STDOUT: call void @_ZN6CarbonL5CallAEPNS_1AE(ptr noundef %c)
  171. // CHECK:STDOUT: call void @llvm.lifetime.end.p0(ptr %c) #3
  172. // CHECK:STDOUT: ret void
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT:
  175. // CHECK:STDOUT: ; Function Attrs: alwaysinline mustprogress nounwind uwtable
  176. // CHECK:STDOUT: define internal void @_ZN6CarbonL5CallCEPNS_1CE(ptr noundef %0) #2 {
  177. // CHECK:STDOUT: entry:
  178. // CHECK:STDOUT: %.addr = alloca ptr, align 8
  179. // CHECK:STDOUT: store ptr %0, ptr %.addr, align 8, !tbaa !16
  180. // CHECK:STDOUT: call void @_CCallC__carbon_thunk.Main(ptr noundef nonnull align 8 dereferenceable(8) %.addr)
  181. // CHECK:STDOUT: ret void
  182. // CHECK:STDOUT: }
  183. // CHECK:STDOUT:
  184. // CHECK:STDOUT: declare void @_CCallA.Main(ptr)
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: declare void @_CCallB.Main(ptr)
  187. // CHECK:STDOUT:
  188. // CHECK:STDOUT: declare void @_CCallC.Main(ptr)
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: ; Function Attrs: nounwind
  191. // CHECK:STDOUT: define void @_CCallA__carbon_thunk.Main(ptr %_) #3 !dbg !18 {
  192. // CHECK:STDOUT: entry:
  193. // CHECK:STDOUT: %.loc21 = load ptr, ptr %_, align 8, !dbg !24
  194. // CHECK:STDOUT: call void @_CCallA.Main(ptr %.loc21), !dbg !24
  195. // CHECK:STDOUT: ret void, !dbg !24
  196. // CHECK:STDOUT: }
  197. // CHECK:STDOUT:
  198. // CHECK:STDOUT: ; Function Attrs: nounwind
  199. // CHECK:STDOUT: define void @_CCallB__carbon_thunk.Main(ptr %_) #3 !dbg !25 {
  200. // CHECK:STDOUT: entry:
  201. // CHECK:STDOUT: %.loc22 = load ptr, ptr %_, align 8, !dbg !28
  202. // CHECK:STDOUT: call void @_CCallB.Main(ptr %.loc22), !dbg !28
  203. // CHECK:STDOUT: ret void, !dbg !28
  204. // CHECK:STDOUT: }
  205. // CHECK:STDOUT:
  206. // CHECK:STDOUT: ; Function Attrs: nounwind
  207. // CHECK:STDOUT: define void @_CCallC__carbon_thunk.Main(ptr %_) #3 !dbg !29 {
  208. // CHECK:STDOUT: entry:
  209. // CHECK:STDOUT: %.loc23 = load ptr, ptr %_, align 8, !dbg !32
  210. // CHECK:STDOUT: call void @_CCallC.Main(ptr %.loc23), !dbg !32
  211. // CHECK:STDOUT: ret void, !dbg !32
  212. // CHECK:STDOUT: }
  213. // CHECK:STDOUT:
  214. // CHECK:STDOUT: attributes #0 = { mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
  215. // CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
  216. // CHECK:STDOUT: attributes #2 = { alwaysinline mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
  217. // CHECK:STDOUT: attributes #3 = { nounwind }
  218. // CHECK:STDOUT:
  219. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  220. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  221. // CHECK:STDOUT: !llvm.errno.tbaa = !{!7}
  222. // CHECK:STDOUT:
  223. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  224. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  225. // CHECK:STDOUT: !2 = !{i32 8, !"PIC Level", i32 2}
  226. // CHECK:STDOUT: !3 = !{i32 7, !"PIE Level", i32 2}
  227. // CHECK:STDOUT: !4 = !{i32 7, !"uwtable", i32 2}
  228. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  229. // CHECK:STDOUT: !6 = !DIFile(filename: "layout.carbon", directory: "")
  230. // CHECK:STDOUT: !7 = !{!8, !8, i64 0}
  231. // CHECK:STDOUT: !8 = !{!"int", !9, i64 0}
  232. // CHECK:STDOUT: !9 = !{!"omnipotent char", !10, i64 0}
  233. // CHECK:STDOUT: !10 = !{!"Simple C++ TBAA"}
  234. // CHECK:STDOUT: !11 = !{!12, !12, i64 0}
  235. // CHECK:STDOUT: !12 = !{!"p1 _ZTSN6Carbon1AE", !13, i64 0}
  236. // CHECK:STDOUT: !13 = !{!"any pointer", !9, i64 0}
  237. // CHECK:STDOUT: !14 = !{!15, !15, i64 0}
  238. // CHECK:STDOUT: !15 = !{!"p1 _ZTSN6Carbon1BE", !13, i64 0}
  239. // CHECK:STDOUT: !16 = !{!17, !17, i64 0}
  240. // CHECK:STDOUT: !17 = !{!"p1 _ZTSN6Carbon1CE", !13, i64 0}
  241. // CHECK:STDOUT: !18 = distinct !DISubprogram(name: "CallA__carbon_thunk", linkageName: "_CCallA__carbon_thunk.Main", scope: null, file: !6, line: 21, type: !19, spFlags: DISPFlagDefinition, unit: !5, retainedNodes: !22)
  242. // CHECK:STDOUT: !19 = !DISubroutineType(types: !20)
  243. // CHECK:STDOUT: !20 = !{null, !21}
  244. // CHECK:STDOUT: !21 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
  245. // CHECK:STDOUT: !22 = !{!23}
  246. // CHECK:STDOUT: !23 = !DILocalVariable(arg: 1, scope: !18, type: !21)
  247. // CHECK:STDOUT: !24 = !DILocation(line: 21, column: 1, scope: !18)
  248. // CHECK:STDOUT: !25 = distinct !DISubprogram(name: "CallB__carbon_thunk", linkageName: "_CCallB__carbon_thunk.Main", scope: null, file: !6, line: 22, type: !19, spFlags: DISPFlagDefinition, unit: !5, retainedNodes: !26)
  249. // CHECK:STDOUT: !26 = !{!27}
  250. // CHECK:STDOUT: !27 = !DILocalVariable(arg: 1, scope: !25, type: !21)
  251. // CHECK:STDOUT: !28 = !DILocation(line: 22, column: 1, scope: !25)
  252. // CHECK:STDOUT: !29 = distinct !DISubprogram(name: "CallC__carbon_thunk", linkageName: "_CCallC__carbon_thunk.Main", scope: null, file: !6, line: 23, type: !19, spFlags: DISPFlagDefinition, unit: !5, retainedNodes: !30)
  253. // CHECK:STDOUT: !30 = !{!31}
  254. // CHECK:STDOUT: !31 = !DILocalVariable(arg: 1, scope: !29, type: !21)
  255. // CHECK:STDOUT: !32 = !DILocation(line: 23, column: 1, scope: !29)