bindings.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/full.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/for/bindings.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/for/bindings.carbon
  12. class EmptyRange(T:! Core.Copy) {
  13. impl as Core.Iterate where .CursorType = {} and .ElementType = T {
  14. fn NewCursor[self: Self]() -> {} {
  15. return {};
  16. }
  17. fn Next[self: Self](cursor: {}*) -> Core.Optional(T) {
  18. return Core.Optional(T).None();
  19. }
  20. }
  21. }
  22. fn F(m: i32, n: i32*);
  23. fn For() {
  24. var r: EmptyRange((i32, i32)) = {};
  25. for ((m: i32, var n: i32) in r) {
  26. F(m, &n);
  27. }
  28. }
  29. // CHECK:STDOUT: ; ModuleID = 'bindings.carbon'
  30. // CHECK:STDOUT: source_filename = "bindings.carbon"
  31. // CHECK:STDOUT:
  32. // CHECK:STDOUT: @EmptyRange.val.loc27_3 = internal constant {} zeroinitializer
  33. // CHECK:STDOUT:
  34. // CHECK:STDOUT: declare void @_CF.Main(i32, ptr)
  35. // CHECK:STDOUT:
  36. // CHECK:STDOUT: define void @_CFor.Main() !dbg !4 {
  37. // CHECK:STDOUT: entry:
  38. // CHECK:STDOUT: %r.var = alloca {}, align 8, !dbg !7
  39. // CHECK:STDOUT: %var = alloca {}, align 8, !dbg !8
  40. // CHECK:STDOUT: %.loc29_33.1.temp = alloca { i1, { i32, i32 } }, align 8, !dbg !8
  41. // CHECK:STDOUT: %n.var = alloca i32, align 4, !dbg !9
  42. // CHECK:STDOUT: %.loc29_33.8.temp = alloca { i32, i32 }, align 8, !dbg !8
  43. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %r.var), !dbg !7
  44. // CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %r.var, ptr align 1 @EmptyRange.val.loc27_3, i64 0, i1 false), !dbg !7
  45. // CHECK:STDOUT: call void @"_CNewCursor.EmptyRange.Main:Iterate.Core.d9506acff356e5d2"(ptr %r.var), !dbg !8
  46. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %var), !dbg !8
  47. // CHECK:STDOUT: br label %for.next, !dbg !8
  48. // CHECK:STDOUT:
  49. // CHECK:STDOUT: for.next: ; preds = %for.body, %entry
  50. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc29_33.1.temp), !dbg !8
  51. // CHECK:STDOUT: call void @"_CNext.EmptyRange.Main:Iterate.Core.d9506acff356e5d2"(ptr %.loc29_33.1.temp, ptr %r.var, ptr %var), !dbg !8
  52. // CHECK:STDOUT: %Optional.HasValue.call = call i1 @_CHasValue.Optional.Core.d9506acff356e5d2(ptr %.loc29_33.1.temp), !dbg !8
  53. // CHECK:STDOUT: br i1 %Optional.HasValue.call, label %for.body, label %for.done, !dbg !8
  54. // CHECK:STDOUT:
  55. // CHECK:STDOUT: for.body: ; preds = %for.next
  56. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %n.var), !dbg !9
  57. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc29_33.8.temp), !dbg !8
  58. // CHECK:STDOUT: call void @_CGet.Optional.Core.d9506acff356e5d2(ptr %.loc29_33.8.temp, ptr %.loc29_33.1.temp), !dbg !8
  59. // CHECK:STDOUT: %tuple.elem0.tuple.elem = getelementptr inbounds nuw { i32, i32 }, ptr %.loc29_33.8.temp, i32 0, i32 0, !dbg !8
  60. // CHECK:STDOUT: %tuple.elem1.tuple.elem = getelementptr inbounds nuw { i32, i32 }, ptr %.loc29_33.8.temp, i32 0, i32 1, !dbg !8
  61. // CHECK:STDOUT: %.loc29_33.11 = load i32, ptr %tuple.elem0.tuple.elem, align 4, !dbg !8
  62. // CHECK:STDOUT: %.loc29_33.12 = load i32, ptr %tuple.elem1.tuple.elem, align 4, !dbg !8
  63. // CHECK:STDOUT: store i32 %.loc29_33.12, ptr %n.var, align 4, !dbg !9
  64. // CHECK:STDOUT: call void @_CF.Main(i32 %.loc29_33.11, ptr %n.var), !dbg !10
  65. // CHECK:STDOUT: br label %for.next, !dbg !11
  66. // CHECK:STDOUT:
  67. // CHECK:STDOUT: for.done: ; preds = %for.next
  68. // CHECK:STDOUT: ret void, !dbg !12
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
  72. // CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #0
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
  75. // CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias writeonly captures(none), ptr noalias readonly captures(none), i64, i1 immarg) #1
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: define linkonce_odr void @"_CNewCursor.EmptyRange.Main:Iterate.Core.d9506acff356e5d2"(ptr %self) !dbg !13 {
  78. // CHECK:STDOUT: entry:
  79. // CHECK:STDOUT: ret void, !dbg !14
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: define linkonce_odr void @"_CNext.EmptyRange.Main:Iterate.Core.d9506acff356e5d2"(ptr sret({ i1, { i32, i32 } }) %return, ptr %self, ptr %cursor) !dbg !15 {
  83. // CHECK:STDOUT: entry:
  84. // CHECK:STDOUT: call void @_CNone.Optional.Core.d9506acff356e5d2(ptr %return), !dbg !16
  85. // CHECK:STDOUT: ret void, !dbg !17
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: define linkonce_odr i1 @_CHasValue.Optional.Core.d9506acff356e5d2(ptr %self) !dbg !18 {
  89. // CHECK:STDOUT: %has_value = getelementptr inbounds nuw { i1, { i32, i32 } }, ptr %self, i32 0, i32 0, !dbg !20
  90. // CHECK:STDOUT: %1 = load i8, ptr %has_value, align 1, !dbg !20
  91. // CHECK:STDOUT: %2 = trunc i8 %1 to i1, !dbg !20
  92. // CHECK:STDOUT: ret i1 %2, !dbg !21
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: define linkonce_odr void @_CGet.Optional.Core.d9506acff356e5d2(ptr sret({ i32, i32 }) %return, ptr %self) !dbg !22 {
  96. // CHECK:STDOUT: %value = getelementptr inbounds nuw { i1, { i32, i32 } }, ptr %self, i32 0, i32 1, !dbg !23
  97. // CHECK:STDOUT: call void @"_COp.52304025ab0e7af9:Copy.Core.58c6957df82926a9"(ptr %return, ptr %value), !dbg !23
  98. // CHECK:STDOUT: ret void, !dbg !24
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: define linkonce_odr void @_CNone.Optional.Core.d9506acff356e5d2(ptr sret({ i1, { i32, i32 } }) %return) !dbg !25 {
  102. // CHECK:STDOUT: %has_value = getelementptr inbounds nuw { i1, { i32, i32 } }, ptr %return, i32 0, i32 0, !dbg !26
  103. // CHECK:STDOUT: store i8 0, ptr %has_value, align 1, !dbg !26
  104. // CHECK:STDOUT: ret void, !dbg !27
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: define linkonce_odr void @"_COp.52304025ab0e7af9:Copy.Core.58c6957df82926a9"(ptr sret({ i32, i32 }) %return, ptr %self) !dbg !28 {
  108. // CHECK:STDOUT: %tuple.elem = getelementptr inbounds nuw { i32, i32 }, ptr %self, i32 0, i32 0, !dbg !30
  109. // CHECK:STDOUT: %tuple.elem.load = load i32, ptr %tuple.elem, align 4, !dbg !30
  110. // CHECK:STDOUT: %tuple.elem1 = getelementptr inbounds nuw { i32, i32 }, ptr %return, i32 0, i32 0, !dbg !31
  111. // CHECK:STDOUT: %tuple.elem2 = getelementptr inbounds nuw { i32, i32 }, ptr %self, i32 0, i32 1, !dbg !32
  112. // CHECK:STDOUT: %tuple.elem.load3 = load i32, ptr %tuple.elem2, align 4, !dbg !32
  113. // CHECK:STDOUT: %tuple.elem4 = getelementptr inbounds nuw { i32, i32 }, ptr %return, i32 0, i32 1, !dbg !31
  114. // CHECK:STDOUT: store i32 %tuple.elem.load, ptr %tuple.elem1, align 4, !dbg !31
  115. // CHECK:STDOUT: store i32 %tuple.elem.load3, ptr %tuple.elem4, align 4, !dbg !31
  116. // CHECK:STDOUT: ret void, !dbg !33
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: ; uselistorder directives
  120. // CHECK:STDOUT: uselistorder ptr @llvm.lifetime.start.p0, { 4, 3, 2, 1, 0 }
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
  123. // CHECK:STDOUT: attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  126. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  129. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  130. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  131. // CHECK:STDOUT: !3 = !DIFile(filename: "bindings.carbon", directory: "")
  132. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "For", linkageName: "_CFor.Main", scope: null, file: !3, line: 26, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  133. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  134. // CHECK:STDOUT: !6 = !{}
  135. // CHECK:STDOUT: !7 = !DILocation(line: 27, column: 3, scope: !4)
  136. // CHECK:STDOUT: !8 = !DILocation(line: 29, column: 7, scope: !4)
  137. // CHECK:STDOUT: !9 = !DILocation(line: 29, column: 17, scope: !4)
  138. // CHECK:STDOUT: !10 = !DILocation(line: 30, column: 5, scope: !4)
  139. // CHECK:STDOUT: !11 = !DILocation(line: 29, column: 3, scope: !4)
  140. // CHECK:STDOUT: !12 = !DILocation(line: 26, column: 1, scope: !4)
  141. // CHECK:STDOUT: !13 = distinct !DISubprogram(name: "NewCursor", linkageName: "_CNewCursor.EmptyRange.Main:Iterate.Core.d9506acff356e5d2", scope: null, file: !3, line: 15, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  142. // CHECK:STDOUT: !14 = !DILocation(line: 16, column: 7, scope: !13)
  143. // CHECK:STDOUT: !15 = distinct !DISubprogram(name: "Next", linkageName: "_CNext.EmptyRange.Main:Iterate.Core.d9506acff356e5d2", scope: null, file: !3, line: 18, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  144. // CHECK:STDOUT: !16 = !DILocation(line: 19, column: 14, scope: !15)
  145. // CHECK:STDOUT: !17 = !DILocation(line: 19, column: 7, scope: !15)
  146. // CHECK:STDOUT: !18 = distinct !DISubprogram(name: "HasValue", linkageName: "_CHasValue.Optional.Core.d9506acff356e5d2", scope: null, file: !19, line: 30, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  147. // CHECK:STDOUT: !19 = !DIFile(filename: "{{.*}}/prelude/types/optional.carbon", directory: "")
  148. // CHECK:STDOUT: !20 = !DILocation(line: 30, column: 46, scope: !18)
  149. // CHECK:STDOUT: !21 = !DILocation(line: 30, column: 39, scope: !18)
  150. // CHECK:STDOUT: !22 = distinct !DISubprogram(name: "Get", linkageName: "_CGet.Optional.Core.d9506acff356e5d2", scope: null, file: !19, line: 31, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  151. // CHECK:STDOUT: !23 = !DILocation(line: 31, column: 38, scope: !22)
  152. // CHECK:STDOUT: !24 = !DILocation(line: 31, column: 31, scope: !22)
  153. // CHECK:STDOUT: !25 = distinct !DISubprogram(name: "None", linkageName: "_CNone.Optional.Core.d9506acff356e5d2", scope: null, file: !19, line: 20, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  154. // CHECK:STDOUT: !26 = !DILocation(line: 22, column: 5, scope: !25)
  155. // CHECK:STDOUT: !27 = !DILocation(line: 23, column: 5, scope: !25)
  156. // CHECK:STDOUT: !28 = distinct !DISubprogram(name: "Op", linkageName: "_COp.52304025ab0e7af9:Copy.Core.58c6957df82926a9", scope: null, file: !29, line: 58, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  157. // CHECK:STDOUT: !29 = !DIFile(filename: "{{.*}}/prelude/copy.carbon", directory: "")
  158. // CHECK:STDOUT: !30 = !DILocation(line: 59, column: 13, scope: !28)
  159. // CHECK:STDOUT: !31 = !DILocation(line: 59, column: 12, scope: !28)
  160. // CHECK:STDOUT: !32 = !DILocation(line: 59, column: 26, scope: !28)
  161. // CHECK:STDOUT: !33 = !DILocation(line: 59, column: 5, scope: !28)