impl.carbon 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/impl/impl.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/impl/impl.carbon
  12. // --- impl.carbon
  13. library "[[@TEST_NAME]]";
  14. interface I {
  15. fn F[self: Self]() -> i32;
  16. }
  17. class A {
  18. var n: i32;
  19. }
  20. impl A as I {
  21. fn F[self: A]() -> i32 {
  22. return self.n;
  23. }
  24. }
  25. fn Call(a: A) -> i32 {
  26. return a.(I.F)();
  27. }
  28. // --- generic_impls.carbon
  29. library "[[@TEST_NAME]]";
  30. class A {}
  31. class B {}
  32. class C {}
  33. interface I(T:! type) {
  34. fn F[self: Self]() -> T;
  35. }
  36. impl C as I(A) {
  37. fn F[self: C]() -> A { return {}; }
  38. }
  39. impl C as I(B) {
  40. fn F[self: C]() -> B { return {}; }
  41. }
  42. fn F1(x: C, y: C) {
  43. var a: A = x.(I(A).F)();
  44. var b: B = y.(I(B).F)();
  45. }
  46. fn F2[T:! I(A), U:! I(B)](x: T, y: U) {
  47. var a: A = x.F();
  48. var b: B = y.F();
  49. }
  50. // --- generic_class_impls.carbon
  51. library "[[@TEST_NAME]]";
  52. class A {}
  53. class B {}
  54. class C(T:! type) {}
  55. interface I {
  56. fn F();
  57. }
  58. impl C(A) as I {
  59. fn F() {}
  60. }
  61. impl C(B) as I {
  62. fn F() {}
  63. }
  64. // CHECK:STDOUT: ; ModuleID = 'impl.carbon'
  65. // CHECK:STDOUT: source_filename = "impl.carbon"
  66. // CHECK:STDOUT:
  67. // CHECK:STDOUT: ; Function Attrs: nounwind
  68. // CHECK:STDOUT: define i32 @"_CF.A.Main:I.Main"(ptr %self) #0 !dbg !4 {
  69. // CHECK:STDOUT: entry:
  70. // CHECK:STDOUT: %.loc14_16.1.n = getelementptr inbounds nuw { i32 }, ptr %self, i32 0, i32 0, !dbg !11
  71. // CHECK:STDOUT: %.loc14_16.2 = load i32, ptr %.loc14_16.1.n, align 4, !dbg !11
  72. // CHECK:STDOUT: ret i32 %.loc14_16.2, !dbg !12
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: ; Function Attrs: nounwind
  76. // CHECK:STDOUT: define i32 @_CCall.Main(ptr %a) #0 !dbg !13 {
  77. // CHECK:STDOUT: entry:
  78. // CHECK:STDOUT: %A.as.I.impl.F.call = call i32 @"_CF.A.Main:I.Main"(ptr %a), !dbg !16
  79. // CHECK:STDOUT: ret i32 %A.as.I.impl.F.call, !dbg !17
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: attributes #0 = { nounwind }
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  85. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  88. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  89. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  90. // CHECK:STDOUT: !3 = !DIFile(filename: "impl.carbon", directory: "")
  91. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "_CF.A.Main:I.Main", scope: null, file: !3, line: 13, type: !5, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !9)
  92. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  93. // CHECK:STDOUT: !6 = !{!7, !8}
  94. // CHECK:STDOUT: !7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
  95. // CHECK:STDOUT: !8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 8)
  96. // CHECK:STDOUT: !9 = !{!10}
  97. // CHECK:STDOUT: !10 = !DILocalVariable(arg: 1, scope: !4, type: !8)
  98. // CHECK:STDOUT: !11 = !DILocation(line: 14, column: 12, scope: !4)
  99. // CHECK:STDOUT: !12 = !DILocation(line: 14, column: 5, scope: !4)
  100. // CHECK:STDOUT: !13 = distinct !DISubprogram(name: "Call", linkageName: "_CCall.Main", scope: null, file: !3, line: 18, type: !5, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !14)
  101. // CHECK:STDOUT: !14 = !{!15}
  102. // CHECK:STDOUT: !15 = !DILocalVariable(arg: 1, scope: !13, type: !8)
  103. // CHECK:STDOUT: !16 = !DILocation(line: 19, column: 10, scope: !13)
  104. // CHECK:STDOUT: !17 = !DILocation(line: 19, column: 3, scope: !13)
  105. // CHECK:STDOUT: ; ModuleID = 'generic_impls.carbon'
  106. // CHECK:STDOUT: source_filename = "generic_impls.carbon"
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: @A.val.loc13_35 = internal constant {} zeroinitializer
  109. // CHECK:STDOUT: @B.val.loc17_35 = internal constant {} zeroinitializer
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: ; Function Attrs: nounwind
  112. // CHECK:STDOUT: define void @"_CF.C.Main:I.96947bccc1c5e61b.Main"(ptr sret({}) %return, ptr %self) #0 !dbg !4 {
  113. // CHECK:STDOUT: entry:
  114. // CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %return, ptr align 1 @A.val.loc13_35, i64 0, i1 false), !dbg !10
  115. // CHECK:STDOUT: ret void, !dbg !10
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: ; Function Attrs: nounwind
  119. // CHECK:STDOUT: define void @"_CF.C.Main:I.707dbe5ae2414d29.Main"(ptr sret({}) %return, ptr %self) #0 !dbg !11 {
  120. // CHECK:STDOUT: entry:
  121. // CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %return, ptr align 1 @B.val.loc17_35, i64 0, i1 false), !dbg !14
  122. // CHECK:STDOUT: ret void, !dbg !14
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: ; Function Attrs: nounwind
  126. // CHECK:STDOUT: define void @_CF1.Main(ptr %x, ptr %y) #0 !dbg !15 {
  127. // CHECK:STDOUT: entry:
  128. // CHECK:STDOUT: %a.var = alloca {}, align 8, !dbg !21
  129. // CHECK:STDOUT: %b.var = alloca {}, align 8, !dbg !22
  130. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %a.var), !dbg !21
  131. // CHECK:STDOUT: call void @"_CF.C.Main:I.96947bccc1c5e61b.Main"(ptr %a.var, ptr %x), !dbg !23
  132. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %b.var), !dbg !22
  133. // CHECK:STDOUT: call void @"_CF.C.Main:I.707dbe5ae2414d29.Main"(ptr %b.var, ptr %y), !dbg !24
  134. // CHECK:STDOUT: ret void, !dbg !25
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
  138. // CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias writeonly captures(none), ptr noalias readonly captures(none), i64, i1 immarg) #1
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
  141. // CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #2
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: ; uselistorder directives
  144. // CHECK:STDOUT: uselistorder ptr @llvm.memcpy.p0.p0.i64, { 1, 0 }
  145. // CHECK:STDOUT: uselistorder ptr @llvm.lifetime.start.p0, { 1, 0 }
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: attributes #0 = { nounwind }
  148. // CHECK:STDOUT: attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
  149. // CHECK:STDOUT: attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  152. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  155. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  156. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  157. // CHECK:STDOUT: !3 = !DIFile(filename: "generic_impls.carbon", directory: "")
  158. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "_CF.C.Main:I.96947bccc1c5e61b.Main", scope: null, file: !3, line: 13, type: !5, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !8)
  159. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  160. // CHECK:STDOUT: !6 = !{!7, !7}
  161. // CHECK:STDOUT: !7 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 8)
  162. // CHECK:STDOUT: !8 = !{!9}
  163. // CHECK:STDOUT: !9 = !DILocalVariable(arg: 1, scope: !4, type: !7)
  164. // CHECK:STDOUT: !10 = !DILocation(line: 13, column: 26, scope: !4)
  165. // CHECK:STDOUT: !11 = distinct !DISubprogram(name: "F", linkageName: "_CF.C.Main:I.707dbe5ae2414d29.Main", scope: null, file: !3, line: 17, type: !5, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !12)
  166. // CHECK:STDOUT: !12 = !{!13}
  167. // CHECK:STDOUT: !13 = !DILocalVariable(arg: 1, scope: !11, type: !7)
  168. // CHECK:STDOUT: !14 = !DILocation(line: 17, column: 26, scope: !11)
  169. // CHECK:STDOUT: !15 = distinct !DISubprogram(name: "F1", linkageName: "_CF1.Main", scope: null, file: !3, line: 20, type: !16, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !18)
  170. // CHECK:STDOUT: !16 = !DISubroutineType(types: !17)
  171. // CHECK:STDOUT: !17 = !{null, !7, !7}
  172. // CHECK:STDOUT: !18 = !{!19, !20}
  173. // CHECK:STDOUT: !19 = !DILocalVariable(arg: 1, scope: !15, type: !7)
  174. // CHECK:STDOUT: !20 = !DILocalVariable(arg: 2, scope: !15, type: !7)
  175. // CHECK:STDOUT: !21 = !DILocation(line: 21, column: 3, scope: !15)
  176. // CHECK:STDOUT: !22 = !DILocation(line: 22, column: 3, scope: !15)
  177. // CHECK:STDOUT: !23 = !DILocation(line: 21, column: 14, scope: !15)
  178. // CHECK:STDOUT: !24 = !DILocation(line: 22, column: 14, scope: !15)
  179. // CHECK:STDOUT: !25 = !DILocation(line: 20, column: 1, scope: !15)
  180. // CHECK:STDOUT: ; ModuleID = 'generic_class_impls.carbon'
  181. // CHECK:STDOUT: source_filename = "generic_class_impls.carbon"
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: ; Function Attrs: nounwind
  184. // CHECK:STDOUT: define void @"_CF.C.96947bccc1c5e61b.Main:I.Main"() #0 !dbg !4 {
  185. // CHECK:STDOUT: entry:
  186. // CHECK:STDOUT: ret void, !dbg !7
  187. // CHECK:STDOUT: }
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: ; Function Attrs: nounwind
  190. // CHECK:STDOUT: define void @"_CF.C.707dbe5ae2414d29.Main:I.Main"() #0 !dbg !8 {
  191. // CHECK:STDOUT: entry:
  192. // CHECK:STDOUT: ret void, !dbg !9
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: attributes #0 = { nounwind }
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  198. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  199. // CHECK:STDOUT:
  200. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  201. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  202. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  203. // CHECK:STDOUT: !3 = !DIFile(filename: "generic_class_impls.carbon", directory: "")
  204. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "_CF.C.96947bccc1c5e61b.Main:I.Main", scope: null, file: !3, line: 14, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  205. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  206. // CHECK:STDOUT: !6 = !{null}
  207. // CHECK:STDOUT: !7 = !DILocation(line: 14, column: 3, scope: !4)
  208. // CHECK:STDOUT: !8 = distinct !DISubprogram(name: "F", linkageName: "_CF.C.707dbe5ae2414d29.Main:I.Main", scope: null, file: !3, line: 18, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  209. // CHECK:STDOUT: !9 = !DILocation(line: 18, column: 3, scope: !8)