thunk.carbon 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/impl/thunk.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/impl/thunk.carbon
  10. class A { var a: i32; }
  11. class B { var b: i32; }
  12. class C { var c: i32; }
  13. impl A as Core.ImplicitAs(B) {
  14. fn Convert[self: A]() -> B { return {.b = self.a}; }
  15. }
  16. impl B as Core.ImplicitAs(C) {
  17. fn Convert[self: B]() -> C { return {.c = self.b}; }
  18. }
  19. interface I {
  20. fn F(a: A) -> C;
  21. }
  22. impl () as I {
  23. fn F(b: B) -> B { return {.b = b.b}; }
  24. }
  25. fn Test(a: A) -> C {
  26. return ().(I.F)(a);
  27. }
  28. // CHECK:STDOUT: ; ModuleID = 'thunk.carbon'
  29. // CHECK:STDOUT: source_filename = "thunk.carbon"
  30. // CHECK:STDOUT:
  31. // CHECK:STDOUT: define void @"_CConvert.A.Main:ImplicitAs.Core"(ptr sret({ i32 }) %return, ptr %self) !dbg !4 {
  32. // CHECK:STDOUT: entry:
  33. // CHECK:STDOUT: %.loc16_49.1.a = getelementptr inbounds nuw { i32 }, ptr %self, i32 0, i32 0, !dbg !7
  34. // CHECK:STDOUT: %.loc16_49.2 = load i32, ptr %.loc16_49.1.a, align 4, !dbg !7
  35. // CHECK:STDOUT: %.loc16_51.2.b = getelementptr inbounds nuw { i32 }, ptr %return, i32 0, i32 0, !dbg !8
  36. // CHECK:STDOUT: store i32 %.loc16_49.2, ptr %.loc16_51.2.b, align 4, !dbg !8
  37. // CHECK:STDOUT: ret void, !dbg !9
  38. // CHECK:STDOUT: }
  39. // CHECK:STDOUT:
  40. // CHECK:STDOUT: define void @"_CConvert.B.Main:ImplicitAs.Core"(ptr sret({ i32 }) %return, ptr %self) !dbg !10 {
  41. // CHECK:STDOUT: entry:
  42. // CHECK:STDOUT: %.loc19_49.1.b = getelementptr inbounds nuw { i32 }, ptr %self, i32 0, i32 0, !dbg !11
  43. // CHECK:STDOUT: %.loc19_49.2 = load i32, ptr %.loc19_49.1.b, align 4, !dbg !11
  44. // CHECK:STDOUT: %.loc19_51.2.c = getelementptr inbounds nuw { i32 }, ptr %return, i32 0, i32 0, !dbg !12
  45. // CHECK:STDOUT: store i32 %.loc19_49.2, ptr %.loc19_51.2.c, align 4, !dbg !12
  46. // CHECK:STDOUT: ret void, !dbg !13
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT:
  49. // CHECK:STDOUT: define void @"_CF.61ea2aba74ab3bf1:I.Main"(ptr sret({ i32 }) %return, ptr %b) !dbg !14 {
  50. // CHECK:STDOUT: entry:
  51. // CHECK:STDOUT: %.loc27_35.1.b = getelementptr inbounds nuw { i32 }, ptr %b, i32 0, i32 0, !dbg !15
  52. // CHECK:STDOUT: %.loc27_35.2 = load i32, ptr %.loc27_35.1.b, align 4, !dbg !15
  53. // CHECK:STDOUT: %.loc27_37.2.b = getelementptr inbounds nuw { i32 }, ptr %return, i32 0, i32 0, !dbg !16
  54. // CHECK:STDOUT: store i32 %.loc27_35.2, ptr %.loc27_37.2.b, align 4, !dbg !16
  55. // CHECK:STDOUT: ret void, !dbg !17
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: define void @"_CF:thunk.61ea2aba74ab3bf1:I.Main"(ptr sret({ i32 }) %return, ptr %a) !dbg !18 {
  59. // CHECK:STDOUT: entry:
  60. // CHECK:STDOUT: %.loc27_19.1.temp = alloca { i32 }, align 8, !dbg !19
  61. // CHECK:STDOUT: %.loc23_9.1.temp = alloca { i32 }, align 8, !dbg !20
  62. // CHECK:STDOUT: call void @"_CConvert.A.Main:ImplicitAs.Core"(ptr %.loc23_9.1.temp, ptr %a), !dbg !20
  63. // CHECK:STDOUT: call void @"_CF.61ea2aba74ab3bf1:I.Main"(ptr %.loc27_19.1.temp, ptr %.loc23_9.1.temp), !dbg !19
  64. // CHECK:STDOUT: %.loc27_19.2.temp = alloca { i32 }, align 8, !dbg !19
  65. // CHECK:STDOUT: call void @"_CConvert.B.Main:ImplicitAs.Core"(ptr %.loc27_19.2.temp, ptr %.loc27_19.1.temp), !dbg !19
  66. // CHECK:STDOUT: ret void, !dbg !19
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: define void @_CTest.Main(ptr sret({ i32 }) %return, ptr %a) !dbg !21 {
  70. // CHECK:STDOUT: entry:
  71. // CHECK:STDOUT: call void @"_CF:thunk.61ea2aba74ab3bf1:I.Main"(ptr %return, ptr %a), !dbg !22
  72. // CHECK:STDOUT: ret void, !dbg !23
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  76. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  79. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  80. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  81. // CHECK:STDOUT: !3 = !DIFile(filename: "thunk.carbon", directory: "")
  82. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Convert", linkageName: "_CConvert.A.Main:ImplicitAs.Core", scope: null, file: !3, line: 16, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  83. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  84. // CHECK:STDOUT: !6 = !{}
  85. // CHECK:STDOUT: !7 = !DILocation(line: 16, column: 45, scope: !4)
  86. // CHECK:STDOUT: !8 = !DILocation(line: 16, column: 39, scope: !4)
  87. // CHECK:STDOUT: !9 = !DILocation(line: 16, column: 32, scope: !4)
  88. // CHECK:STDOUT: !10 = distinct !DISubprogram(name: "Convert", linkageName: "_CConvert.B.Main:ImplicitAs.Core", scope: null, file: !3, line: 19, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  89. // CHECK:STDOUT: !11 = !DILocation(line: 19, column: 45, scope: !10)
  90. // CHECK:STDOUT: !12 = !DILocation(line: 19, column: 39, scope: !10)
  91. // CHECK:STDOUT: !13 = !DILocation(line: 19, column: 32, scope: !10)
  92. // CHECK:STDOUT: !14 = distinct !DISubprogram(name: "F", linkageName: "_CF.61ea2aba74ab3bf1:I.Main", scope: null, file: !3, line: 27, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  93. // CHECK:STDOUT: !15 = !DILocation(line: 27, column: 34, scope: !14)
  94. // CHECK:STDOUT: !16 = !DILocation(line: 27, column: 28, scope: !14)
  95. // CHECK:STDOUT: !17 = !DILocation(line: 27, column: 21, scope: !14)
  96. // CHECK:STDOUT: !18 = distinct !DISubprogram(name: "F", linkageName: "_CF:thunk.61ea2aba74ab3bf1:I.Main", scope: null, file: !3, line: 27, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  97. // CHECK:STDOUT: !19 = !DILocation(line: 27, column: 3, scope: !18)
  98. // CHECK:STDOUT: !20 = !DILocation(line: 23, column: 8, scope: !18)
  99. // CHECK:STDOUT: !21 = distinct !DISubprogram(name: "Test", linkageName: "_CTest.Main", scope: null, file: !3, line: 30, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  100. // CHECK:STDOUT: !22 = !DILocation(line: 31, column: 10, scope: !21)
  101. // CHECK:STDOUT: !23 = !DILocation(line: 31, column: 3, scope: !21)