bindings.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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: ; Function Attrs: nounwind
  37. // CHECK:STDOUT: define void @_CFor.Main() #0 !dbg !4 {
  38. // CHECK:STDOUT: entry:
  39. // CHECK:STDOUT: %r.var = alloca {}, align 8, !dbg !7
  40. // CHECK:STDOUT: %var = alloca {}, align 8, !dbg !8
  41. // CHECK:STDOUT: %.loc29_33.1.temp = alloca { { i32, i32 }, i1 }, align 8, !dbg !8
  42. // CHECK:STDOUT: %n.var = alloca i32, align 4, !dbg !9
  43. // CHECK:STDOUT: %.loc29_33.8.temp = alloca { i32, i32 }, align 8, !dbg !8
  44. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %r.var), !dbg !7
  45. // 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
  46. // CHECK:STDOUT: call void @"_CNewCursor.EmptyRange.Main:Iterate.Core.a862d5c8b748242e"(ptr %r.var), !dbg !8
  47. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %var), !dbg !8
  48. // CHECK:STDOUT: br label %for.next, !dbg !8
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: for.next: ; preds = %for.body, %entry
  51. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc29_33.1.temp), !dbg !8
  52. // CHECK:STDOUT: call void @"_CNext.EmptyRange.Main:Iterate.Core.a862d5c8b748242e"(ptr %.loc29_33.1.temp, ptr %r.var, ptr %var), !dbg !8
  53. // CHECK:STDOUT: %Optional.HasValue.call = call i1 @_CHasValue.Optional.Core.7a2fd2277130880e(ptr %.loc29_33.1.temp), !dbg !8
  54. // CHECK:STDOUT: br i1 %Optional.HasValue.call, label %for.body, label %for.done, !dbg !8
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: for.body: ; preds = %for.next
  57. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %n.var), !dbg !9
  58. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc29_33.8.temp), !dbg !8
  59. // CHECK:STDOUT: call void @_CGet.Optional.Core.7a2fd2277130880e(ptr %.loc29_33.8.temp, ptr %.loc29_33.1.temp), !dbg !8
  60. // CHECK:STDOUT: %tuple.elem0.tuple.elem = getelementptr inbounds nuw { i32, i32 }, ptr %.loc29_33.8.temp, i32 0, i32 0, !dbg !8
  61. // CHECK:STDOUT: %tuple.elem1.tuple.elem = getelementptr inbounds nuw { i32, i32 }, ptr %.loc29_33.8.temp, i32 0, i32 1, !dbg !8
  62. // CHECK:STDOUT: %.loc29_33.11 = load i32, ptr %tuple.elem0.tuple.elem, align 4, !dbg !8
  63. // CHECK:STDOUT: %.loc29_33.12 = load i32, ptr %tuple.elem1.tuple.elem, align 4, !dbg !8
  64. // CHECK:STDOUT: store i32 %.loc29_33.12, ptr %n.var, align 4, !dbg !9
  65. // CHECK:STDOUT: call void @_CF.Main(i32 %.loc29_33.11, ptr %n.var), !dbg !10
  66. // CHECK:STDOUT: br label %for.next, !dbg !11
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: for.done: ; preds = %for.next
  69. // CHECK:STDOUT: ret void, !dbg !12
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
  73. // CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #1
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
  76. // CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias writeonly captures(none), ptr noalias readonly captures(none), i64, i1 immarg) #2
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: ; Function Attrs: nounwind
  79. // CHECK:STDOUT: define linkonce_odr void @"_CNewCursor.EmptyRange.Main:Iterate.Core.a862d5c8b748242e"(ptr %self) #0 !dbg !13 {
  80. // CHECK:STDOUT: entry:
  81. // CHECK:STDOUT: ret void, !dbg !14
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: ; Function Attrs: nounwind
  85. // CHECK:STDOUT: define linkonce_odr void @"_CNext.EmptyRange.Main:Iterate.Core.a862d5c8b748242e"(ptr sret({ { i32, i32 }, i1 }) %return, ptr %self, ptr %cursor) #0 !dbg !15 {
  86. // CHECK:STDOUT: entry:
  87. // CHECK:STDOUT: call void @_CNone.Optional.Core.7a2fd2277130880e(ptr %return), !dbg !16
  88. // CHECK:STDOUT: ret void, !dbg !17
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: ; Function Attrs: nounwind
  92. // CHECK:STDOUT: define linkonce_odr i1 @_CHasValue.Optional.Core.7a2fd2277130880e(ptr %self) #0 !dbg !18 {
  93. // CHECK:STDOUT: %1 = call i1 @"_CHas.225258f1a45e9386:OptionalStorage.Core.a862d5c8b748242e"(ptr %self), !dbg !20
  94. // CHECK:STDOUT: ret i1 %1, !dbg !21
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: ; Function Attrs: nounwind
  98. // CHECK:STDOUT: define linkonce_odr void @_CGet.Optional.Core.7a2fd2277130880e(ptr sret({ i32, i32 }) %return, ptr %self) #0 !dbg !22 {
  99. // CHECK:STDOUT: call void @"_CGet.225258f1a45e9386:OptionalStorage.Core.a862d5c8b748242e"(ptr %return, ptr %self), !dbg !23
  100. // CHECK:STDOUT: ret void, !dbg !24
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: ; Function Attrs: nounwind
  104. // CHECK:STDOUT: define linkonce_odr void @_CNone.Optional.Core.7a2fd2277130880e(ptr sret({ { i32, i32 }, i1 }) %return) #0 !dbg !25 {
  105. // CHECK:STDOUT: call void @"_CNone.225258f1a45e9386:OptionalStorage.Core.a862d5c8b748242e"(ptr %return), !dbg !26
  106. // CHECK:STDOUT: ret void, !dbg !27
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: ; Function Attrs: nounwind
  110. // CHECK:STDOUT: define linkonce_odr i1 @"_CHas.225258f1a45e9386:OptionalStorage.Core.a862d5c8b748242e"(ptr %value) #0 !dbg !28 {
  111. // CHECK:STDOUT: %has_value = getelementptr inbounds nuw { { i32, i32 }, i1 }, ptr %value, i32 0, i32 1, !dbg !29
  112. // CHECK:STDOUT: %1 = load i8, ptr %has_value, align 1, !dbg !29
  113. // CHECK:STDOUT: %2 = trunc i8 %1 to i1, !dbg !29
  114. // CHECK:STDOUT: ret i1 %2, !dbg !30
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: ; Function Attrs: nounwind
  118. // CHECK:STDOUT: define linkonce_odr void @"_CGet.225258f1a45e9386:OptionalStorage.Core.a862d5c8b748242e"(ptr sret({ i32, i32 }) %return, ptr %value) #0 !dbg !31 {
  119. // CHECK:STDOUT: %value1 = getelementptr inbounds nuw { { i32, i32 }, i1 }, ptr %value, i32 0, i32 0, !dbg !32
  120. // CHECK:STDOUT: call void @"_COp.2b3ba83e01542f11:Copy.Core.4193728889277ad1"(ptr %return, ptr %value1), !dbg !32
  121. // CHECK:STDOUT: ret void, !dbg !33
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: ; Function Attrs: nounwind
  125. // CHECK:STDOUT: define linkonce_odr void @"_CNone.225258f1a45e9386:OptionalStorage.Core.a862d5c8b748242e"(ptr sret({ { i32, i32 }, i1 }) %return) #0 !dbg !34 {
  126. // CHECK:STDOUT: %has_value = getelementptr inbounds nuw { { i32, i32 }, i1 }, ptr %return, i32 0, i32 1, !dbg !35
  127. // CHECK:STDOUT: store i8 0, ptr %has_value, align 1, !dbg !35
  128. // CHECK:STDOUT: ret void, !dbg !36
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: ; Function Attrs: nounwind
  132. // CHECK:STDOUT: define linkonce_odr void @"_COp.2b3ba83e01542f11:Copy.Core.4193728889277ad1"(ptr sret({ i32, i32 }) %return, ptr %self) #0 !dbg !37 {
  133. // CHECK:STDOUT: %tuple.elem = getelementptr inbounds nuw { i32, i32 }, ptr %self, i32 0, i32 0, !dbg !39
  134. // CHECK:STDOUT: %tuple.elem.load = load i32, ptr %tuple.elem, align 4, !dbg !39
  135. // CHECK:STDOUT: %tuple.elem1 = getelementptr inbounds nuw { i32, i32 }, ptr %return, i32 0, i32 0, !dbg !40
  136. // CHECK:STDOUT: %tuple.elem2 = getelementptr inbounds nuw { i32, i32 }, ptr %self, i32 0, i32 1, !dbg !41
  137. // CHECK:STDOUT: %tuple.elem.load3 = load i32, ptr %tuple.elem2, align 4, !dbg !41
  138. // CHECK:STDOUT: %tuple.elem4 = getelementptr inbounds nuw { i32, i32 }, ptr %return, i32 0, i32 1, !dbg !40
  139. // CHECK:STDOUT: store i32 %tuple.elem.load, ptr %tuple.elem1, align 4, !dbg !40
  140. // CHECK:STDOUT: store i32 %tuple.elem.load3, ptr %tuple.elem4, align 4, !dbg !40
  141. // CHECK:STDOUT: ret void, !dbg !42
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: ; uselistorder directives
  145. // CHECK:STDOUT: uselistorder ptr @llvm.lifetime.start.p0, { 4, 3, 2, 1, 0 }
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: attributes #0 = { nounwind }
  148. // CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
  149. // CHECK:STDOUT: attributes #2 = { nocallback nofree 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, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  157. // CHECK:STDOUT: !3 = !DIFile(filename: "bindings.carbon", directory: "")
  158. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "For", linkageName: "_CFor.Main", scope: null, file: !3, line: 26, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  159. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  160. // CHECK:STDOUT: !6 = !{}
  161. // CHECK:STDOUT: !7 = !DILocation(line: 27, column: 3, scope: !4)
  162. // CHECK:STDOUT: !8 = !DILocation(line: 29, column: 7, scope: !4)
  163. // CHECK:STDOUT: !9 = !DILocation(line: 29, column: 17, scope: !4)
  164. // CHECK:STDOUT: !10 = !DILocation(line: 30, column: 5, scope: !4)
  165. // CHECK:STDOUT: !11 = !DILocation(line: 29, column: 3, scope: !4)
  166. // CHECK:STDOUT: !12 = !DILocation(line: 26, column: 1, scope: !4)
  167. // CHECK:STDOUT: !13 = distinct !DISubprogram(name: "NewCursor", linkageName: "_CNewCursor.EmptyRange.Main:Iterate.Core.a862d5c8b748242e", scope: null, file: !3, line: 15, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  168. // CHECK:STDOUT: !14 = !DILocation(line: 16, column: 7, scope: !13)
  169. // CHECK:STDOUT: !15 = distinct !DISubprogram(name: "Next", linkageName: "_CNext.EmptyRange.Main:Iterate.Core.a862d5c8b748242e", scope: null, file: !3, line: 18, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  170. // CHECK:STDOUT: !16 = !DILocation(line: 19, column: 14, scope: !15)
  171. // CHECK:STDOUT: !17 = !DILocation(line: 19, column: 7, scope: !15)
  172. // CHECK:STDOUT: !18 = distinct !DISubprogram(name: "HasValue", linkageName: "_CHasValue.Optional.Core.7a2fd2277130880e", scope: null, file: !19, line: 31, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  173. // CHECK:STDOUT: !19 = !DIFile(filename: "{{.*}}/prelude/types/optional.carbon", directory: "")
  174. // CHECK:STDOUT: !20 = !DILocation(line: 32, column: 12, scope: !18)
  175. // CHECK:STDOUT: !21 = !DILocation(line: 32, column: 5, scope: !18)
  176. // CHECK:STDOUT: !22 = distinct !DISubprogram(name: "Get", linkageName: "_CGet.Optional.Core.7a2fd2277130880e", scope: null, file: !19, line: 34, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  177. // CHECK:STDOUT: !23 = !DILocation(line: 35, column: 12, scope: !22)
  178. // CHECK:STDOUT: !24 = !DILocation(line: 35, column: 5, scope: !22)
  179. // CHECK:STDOUT: !25 = distinct !DISubprogram(name: "None", linkageName: "_CNone.Optional.Core.7a2fd2277130880e", scope: null, file: !19, line: 25, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  180. // CHECK:STDOUT: !26 = !DILocation(line: 26, column: 12, scope: !25)
  181. // CHECK:STDOUT: !27 = !DILocation(line: 26, column: 5, scope: !25)
  182. // CHECK:STDOUT: !28 = distinct !DISubprogram(name: "Has", linkageName: "_CHas.225258f1a45e9386:OptionalStorage.Core.a862d5c8b748242e", scope: null, file: !19, line: 73, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  183. // CHECK:STDOUT: !29 = !DILocation(line: 74, column: 12, scope: !28)
  184. // CHECK:STDOUT: !30 = !DILocation(line: 74, column: 5, scope: !28)
  185. // CHECK:STDOUT: !31 = distinct !DISubprogram(name: "Get", linkageName: "_CGet.225258f1a45e9386:OptionalStorage.Core.a862d5c8b748242e", scope: null, file: !19, line: 76, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  186. // CHECK:STDOUT: !32 = !DILocation(line: 77, column: 12, scope: !31)
  187. // CHECK:STDOUT: !33 = !DILocation(line: 77, column: 5, scope: !31)
  188. // CHECK:STDOUT: !34 = distinct !DISubprogram(name: "None", linkageName: "_CNone.225258f1a45e9386:OptionalStorage.Core.a862d5c8b748242e", scope: null, file: !19, line: 60, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  189. // CHECK:STDOUT: !35 = !DILocation(line: 62, column: 5, scope: !34)
  190. // CHECK:STDOUT: !36 = !DILocation(line: 63, column: 5, scope: !34)
  191. // CHECK:STDOUT: !37 = distinct !DISubprogram(name: "Op", linkageName: "_COp.2b3ba83e01542f11:Copy.Core.4193728889277ad1", scope: null, file: !38, line: 58, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  192. // CHECK:STDOUT: !38 = !DIFile(filename: "{{.*}}/prelude/copy.carbon", directory: "")
  193. // CHECK:STDOUT: !39 = !DILocation(line: 59, column: 13, scope: !37)
  194. // CHECK:STDOUT: !40 = !DILocation(line: 59, column: 12, scope: !37)
  195. // CHECK:STDOUT: !41 = !DILocation(line: 59, column: 26, scope: !37)
  196. // CHECK:STDOUT: !42 = !DILocation(line: 59, column: 5, scope: !37)