bindings.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 & Core.Destroy) {
  13. impl as Core.Iterate where .CursorType = {} and .ElementType = T {
  14. fn NewCursor[self: Self]() -> {} {
  15. return {};
  16. }
  17. fn Next[self: Self](_: {}*) -> 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.1ccc388edee5a786.Main:Iterate.Core.c6c37881d2ed7d9d"(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.1ccc388edee5a786.Main:Iterate.Core.c6c37881d2ed7d9d"(ptr %.loc29_33.1.temp, ptr %r.var, ptr %var), !dbg !8
  53. // CHECK:STDOUT: %Optional.HasValue.call = call i1 @_CHasValue.Optional.Core.eeebe1ada0072aea(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.eeebe1ada0072aea(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.1ccc388edee5a786.Main:Iterate.Core.c6c37881d2ed7d9d"(ptr %self) #0 !dbg !13 {
  80. // CHECK:STDOUT: entry:
  81. // CHECK:STDOUT: ret void, !dbg !19
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: ; Function Attrs: nounwind
  85. // CHECK:STDOUT: define linkonce_odr void @"_CNext.EmptyRange.1ccc388edee5a786.Main:Iterate.Core.c6c37881d2ed7d9d"(ptr sret({ { i32, i32 }, i1 }) %return, ptr %self, ptr %_) #0 !dbg !20 {
  86. // CHECK:STDOUT: entry:
  87. // CHECK:STDOUT: call void @_CNone.Optional.Core.eeebe1ada0072aea(ptr %return), !dbg !26
  88. // CHECK:STDOUT: ret void, !dbg !27
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: ; Function Attrs: nounwind
  92. // CHECK:STDOUT: define linkonce_odr i1 @_CHasValue.Optional.Core.eeebe1ada0072aea(ptr %self) #0 !dbg !28 {
  93. // CHECK:STDOUT: %1 = call i1 @"_CHas.3e8267224c5dc9c2:OptionalStorage.Core.c6c37881d2ed7d9d"(ptr %self), !dbg !34
  94. // CHECK:STDOUT: ret i1 %1, !dbg !35
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: ; Function Attrs: nounwind
  98. // CHECK:STDOUT: define linkonce_odr void @_CGet.Optional.Core.eeebe1ada0072aea(ptr sret({ i32, i32 }) %return, ptr %self) #0 !dbg !36 {
  99. // CHECK:STDOUT: call void @"_CGet.3e8267224c5dc9c2:OptionalStorage.Core.c6c37881d2ed7d9d"(ptr %return, ptr %self), !dbg !39
  100. // CHECK:STDOUT: ret void, !dbg !40
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: ; Function Attrs: nounwind
  104. // CHECK:STDOUT: define linkonce_odr void @_CNone.Optional.Core.eeebe1ada0072aea(ptr sret({ { i32, i32 }, i1 }) %return) #0 !dbg !41 {
  105. // CHECK:STDOUT: call void @"_CNone.3e8267224c5dc9c2:OptionalStorage.Core.c6c37881d2ed7d9d"(ptr %return), !dbg !44
  106. // CHECK:STDOUT: ret void, !dbg !45
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: ; Function Attrs: nounwind
  110. // CHECK:STDOUT: define linkonce_odr i1 @"_CHas.3e8267224c5dc9c2:OptionalStorage.Core.c6c37881d2ed7d9d"(ptr %value) #0 !dbg !46 {
  111. // CHECK:STDOUT: %has_value = getelementptr inbounds nuw { { i32, i32 }, i1 }, ptr %value, i32 0, i32 1, !dbg !49
  112. // CHECK:STDOUT: %1 = load i8, ptr %has_value, align 1, !dbg !49
  113. // CHECK:STDOUT: %2 = trunc i8 %1 to i1, !dbg !49
  114. // CHECK:STDOUT: ret i1 %2, !dbg !50
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: ; Function Attrs: nounwind
  118. // CHECK:STDOUT: define linkonce_odr void @"_CGet.3e8267224c5dc9c2:OptionalStorage.Core.c6c37881d2ed7d9d"(ptr sret({ i32, i32 }) %return, ptr %value) #0 !dbg !51 {
  119. // CHECK:STDOUT: %value1 = getelementptr inbounds nuw { { i32, i32 }, i1 }, ptr %value, i32 0, i32 0, !dbg !54
  120. // CHECK:STDOUT: call void @"_COp.e4daa70d026fdea2:Copy.Core.3e36085cb869f630"(ptr %return, ptr %value1), !dbg !54
  121. // CHECK:STDOUT: ret void, !dbg !55
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: ; Function Attrs: nounwind
  125. // CHECK:STDOUT: define linkonce_odr void @"_CNone.3e8267224c5dc9c2:OptionalStorage.Core.c6c37881d2ed7d9d"(ptr sret({ { i32, i32 }, i1 }) %return) #0 !dbg !56 {
  126. // CHECK:STDOUT: %has_value = getelementptr inbounds nuw { { i32, i32 }, i1 }, ptr %return, i32 0, i32 1, !dbg !57
  127. // CHECK:STDOUT: store i8 0, ptr %has_value, align 1, !dbg !57
  128. // CHECK:STDOUT: ret void, !dbg !58
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: ; Function Attrs: nounwind
  132. // CHECK:STDOUT: define linkonce_odr void @"_COp.e4daa70d026fdea2:Copy.Core.3e36085cb869f630"(ptr sret({ i32, i32 }) %return, ptr %self) #0 !dbg !59 {
  133. // CHECK:STDOUT: %tuple.elem = getelementptr inbounds nuw { i32, i32 }, ptr %self, i32 0, i32 0, !dbg !63
  134. // CHECK:STDOUT: %tuple.elem.load = load i32, ptr %tuple.elem, align 4, !dbg !63
  135. // CHECK:STDOUT: %tuple.elem1 = getelementptr inbounds nuw { i32, i32 }, ptr %return, i32 0, i32 0, !dbg !64
  136. // CHECK:STDOUT: %tuple.elem2 = getelementptr inbounds nuw { i32, i32 }, ptr %self, i32 0, i32 1, !dbg !65
  137. // CHECK:STDOUT: %tuple.elem.load3 = load i32, ptr %tuple.elem2, align 4, !dbg !65
  138. // CHECK:STDOUT: %tuple.elem4 = getelementptr inbounds nuw { i32, i32 }, ptr %return, i32 0, i32 1, !dbg !64
  139. // CHECK:STDOUT: store i32 %tuple.elem.load, ptr %tuple.elem1, align 4, !dbg !64
  140. // CHECK:STDOUT: store i32 %tuple.elem.load3, ptr %tuple.elem4, align 4, !dbg !64
  141. // CHECK:STDOUT: ret void, !dbg !66
  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_plus_plus, 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 = !{null}
  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.1ccc388edee5a786.Main:Iterate.Core.c6c37881d2ed7d9d", scope: null, file: !3, line: 15, type: !14, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !17)
  168. // CHECK:STDOUT: !14 = !DISubroutineType(types: !15)
  169. // CHECK:STDOUT: !15 = !{null, !16}
  170. // CHECK:STDOUT: !16 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 8)
  171. // CHECK:STDOUT: !17 = !{!18}
  172. // CHECK:STDOUT: !18 = !DILocalVariable(arg: 1, scope: !13, type: !16)
  173. // CHECK:STDOUT: !19 = !DILocation(line: 16, column: 7, scope: !13)
  174. // CHECK:STDOUT: !20 = distinct !DISubprogram(name: "Next", linkageName: "_CNext.EmptyRange.1ccc388edee5a786.Main:Iterate.Core.c6c37881d2ed7d9d", scope: null, file: !3, line: 18, type: !21, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !23)
  175. // CHECK:STDOUT: !21 = !DISubroutineType(types: !22)
  176. // CHECK:STDOUT: !22 = !{!16, !16, !16}
  177. // CHECK:STDOUT: !23 = !{!24, !25}
  178. // CHECK:STDOUT: !24 = !DILocalVariable(arg: 1, scope: !20, type: !16)
  179. // CHECK:STDOUT: !25 = !DILocalVariable(arg: 2, scope: !20, type: !16)
  180. // CHECK:STDOUT: !26 = !DILocation(line: 19, column: 14, scope: !20)
  181. // CHECK:STDOUT: !27 = !DILocation(line: 19, column: 7, scope: !20)
  182. // CHECK:STDOUT: !28 = distinct !DISubprogram(name: "HasValue", linkageName: "_CHasValue.Optional.Core.eeebe1ada0072aea", scope: null, file: !29, line: 32, type: !30, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !32)
  183. // CHECK:STDOUT: !29 = !DIFile(filename: "{{.*}}/prelude/types/optional.carbon", directory: "")
  184. // CHECK:STDOUT: !30 = !DISubroutineType(types: !31)
  185. // CHECK:STDOUT: !31 = !{!16, !16}
  186. // CHECK:STDOUT: !32 = !{!33}
  187. // CHECK:STDOUT: !33 = !DILocalVariable(arg: 1, scope: !28, type: !16)
  188. // CHECK:STDOUT: !34 = !DILocation(line: 33, column: 12, scope: !28)
  189. // CHECK:STDOUT: !35 = !DILocation(line: 33, column: 5, scope: !28)
  190. // CHECK:STDOUT: !36 = distinct !DISubprogram(name: "Get", linkageName: "_CGet.Optional.Core.eeebe1ada0072aea", scope: null, file: !29, line: 35, type: !30, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !37)
  191. // CHECK:STDOUT: !37 = !{!38}
  192. // CHECK:STDOUT: !38 = !DILocalVariable(arg: 1, scope: !36, type: !16)
  193. // CHECK:STDOUT: !39 = !DILocation(line: 36, column: 12, scope: !36)
  194. // CHECK:STDOUT: !40 = !DILocation(line: 36, column: 5, scope: !36)
  195. // CHECK:STDOUT: !41 = distinct !DISubprogram(name: "None", linkageName: "_CNone.Optional.Core.eeebe1ada0072aea", scope: null, file: !29, line: 26, type: !42, spFlags: DISPFlagDefinition, unit: !2)
  196. // CHECK:STDOUT: !42 = !DISubroutineType(types: !43)
  197. // CHECK:STDOUT: !43 = !{!16}
  198. // CHECK:STDOUT: !44 = !DILocation(line: 27, column: 12, scope: !41)
  199. // CHECK:STDOUT: !45 = !DILocation(line: 27, column: 5, scope: !41)
  200. // CHECK:STDOUT: !46 = distinct !DISubprogram(name: "Has", linkageName: "_CHas.3e8267224c5dc9c2:OptionalStorage.Core.c6c37881d2ed7d9d", scope: null, file: !29, line: 123, type: !30, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !47)
  201. // CHECK:STDOUT: !47 = !{!48}
  202. // CHECK:STDOUT: !48 = !DILocalVariable(arg: 1, scope: !46, type: !16)
  203. // CHECK:STDOUT: !49 = !DILocation(line: 124, column: 12, scope: !46)
  204. // CHECK:STDOUT: !50 = !DILocation(line: 124, column: 5, scope: !46)
  205. // CHECK:STDOUT: !51 = distinct !DISubprogram(name: "Get", linkageName: "_CGet.3e8267224c5dc9c2:OptionalStorage.Core.c6c37881d2ed7d9d", scope: null, file: !29, line: 126, type: !30, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !52)
  206. // CHECK:STDOUT: !52 = !{!53}
  207. // CHECK:STDOUT: !53 = !DILocalVariable(arg: 1, scope: !51, type: !16)
  208. // CHECK:STDOUT: !54 = !DILocation(line: 127, column: 12, scope: !51)
  209. // CHECK:STDOUT: !55 = !DILocation(line: 127, column: 5, scope: !51)
  210. // CHECK:STDOUT: !56 = distinct !DISubprogram(name: "None", linkageName: "_CNone.3e8267224c5dc9c2:OptionalStorage.Core.c6c37881d2ed7d9d", scope: null, file: !29, line: 110, type: !42, spFlags: DISPFlagDefinition, unit: !2)
  211. // CHECK:STDOUT: !57 = !DILocation(line: 112, column: 5, scope: !56)
  212. // CHECK:STDOUT: !58 = !DILocation(line: 113, column: 5, scope: !56)
  213. // CHECK:STDOUT: !59 = distinct !DISubprogram(name: "Op", linkageName: "_COp.e4daa70d026fdea2:Copy.Core.3e36085cb869f630", scope: null, file: !60, line: 58, type: !30, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !61)
  214. // CHECK:STDOUT: !60 = !DIFile(filename: "{{.*}}/prelude/copy.carbon", directory: "")
  215. // CHECK:STDOUT: !61 = !{!62}
  216. // CHECK:STDOUT: !62 = !DILocalVariable(arg: 1, scope: !59, type: !16)
  217. // CHECK:STDOUT: !63 = !DILocation(line: 59, column: 13, scope: !59)
  218. // CHECK:STDOUT: !64 = !DILocation(line: 59, column: 12, scope: !59)
  219. // CHECK:STDOUT: !65 = !DILocation(line: 59, column: 26, scope: !59)
  220. // CHECK:STDOUT: !66 = !DILocation(line: 59, column: 5, scope: !59)