import.carbon 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/var/import.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/var/import.carbon
  12. // --- simple.carbon
  13. library "[[@TEST_NAME]]";
  14. var v: i32 = 42;
  15. // --- import_simple.carbon
  16. library "[[@TEST_NAME]]";
  17. import library "simple";
  18. fn Run() -> i32 {
  19. return v;
  20. }
  21. // --- tuple_pattern.carbon
  22. library "[[@TEST_NAME]]";
  23. var (v: i32, w: i32) = (1, 2);
  24. let (x: i32, var y: i32) = (3, 4);
  25. var (_: i32, z: i32) = (5, 6);
  26. // --- import_tuple_pattern.carbon
  27. library "[[@TEST_NAME]]";
  28. import library "tuple_pattern";
  29. fn V() -> i32 { return v; }
  30. fn W() -> i32 { return w; }
  31. // TODO: Also test `x`. Right now, lowering a reference to an imported `let` asserts.
  32. fn Y() -> i32 { return y; }
  33. fn Z() -> i32 { return z; }
  34. // CHECK:STDOUT: ; ModuleID = 'simple.carbon'
  35. // CHECK:STDOUT: source_filename = "simple.carbon"
  36. // CHECK:STDOUT:
  37. // CHECK:STDOUT: @_Cv.Main = global i32 0
  38. // CHECK:STDOUT: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @_C__global_init.Main, ptr null }]
  39. // CHECK:STDOUT:
  40. // CHECK:STDOUT: ; Function Attrs: nounwind
  41. // CHECK:STDOUT: define internal void @_C__global_init.Main() #0 !dbg !4 {
  42. // CHECK:STDOUT: entry:
  43. // CHECK:STDOUT: store i32 42, ptr @_Cv.Main, align 4, !dbg !7
  44. // CHECK:STDOUT: ret void, !dbg !8
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT:
  47. // CHECK:STDOUT: attributes #0 = { nounwind }
  48. // CHECK:STDOUT:
  49. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  50. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  51. // CHECK:STDOUT:
  52. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  53. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  54. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  55. // CHECK:STDOUT: !3 = !DIFile(filename: "simple.carbon", directory: "")
  56. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "__global_init", linkageName: "_C__global_init.Main", scope: null, file: !3, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  57. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  58. // CHECK:STDOUT: !6 = !{null}
  59. // CHECK:STDOUT: !7 = !DILocation(line: 4, column: 1, scope: !4)
  60. // CHECK:STDOUT: !8 = !DILocation(line: 0, scope: !4)
  61. // CHECK:STDOUT: ; ModuleID = 'import_simple.carbon'
  62. // CHECK:STDOUT: source_filename = "import_simple.carbon"
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: @_Cv.Main = external global i32
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: ; Function Attrs: nounwind
  67. // CHECK:STDOUT: define i32 @main() #0 !dbg !4 {
  68. // CHECK:STDOUT: entry:
  69. // CHECK:STDOUT: %.loc7 = load i32, ptr @_Cv.Main, align 4, !dbg !8
  70. // CHECK:STDOUT: ret i32 %.loc7, !dbg !9
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: attributes #0 = { nounwind }
  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_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  81. // CHECK:STDOUT: !3 = !DIFile(filename: "import_simple.carbon", directory: "")
  82. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Run", linkageName: "main", scope: null, file: !3, line: 6, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  83. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  84. // CHECK:STDOUT: !6 = !{!7}
  85. // CHECK:STDOUT: !7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
  86. // CHECK:STDOUT: !8 = !DILocation(line: 7, column: 10, scope: !4)
  87. // CHECK:STDOUT: !9 = !DILocation(line: 7, column: 3, scope: !4)
  88. // CHECK:STDOUT: ; ModuleID = 'tuple_pattern.carbon'
  89. // CHECK:STDOUT: source_filename = "tuple_pattern.carbon"
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: @_Cv.Main = global { i32, i32 } zeroinitializer
  92. // CHECK:STDOUT: @_Cy.Main = global i32 0
  93. // CHECK:STDOUT: @_Cz.Main = global { i32, i32 } zeroinitializer
  94. // CHECK:STDOUT: @tuple.21c.loc4_1 = internal constant { i32, i32 } { i32 1, i32 2 }
  95. // CHECK:STDOUT: @tuple.092.loc8_1 = internal constant { i32, i32 } { i32 5, i32 6 }
  96. // CHECK:STDOUT: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @_C__global_init.Main, ptr null }]
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: ; Function Attrs: nounwind
  99. // CHECK:STDOUT: define internal void @_C__global_init.Main() #0 !dbg !4 {
  100. // CHECK:STDOUT: entry:
  101. // CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 @_Cv.Main, ptr align 4 @tuple.21c.loc4_1, i64 8, i1 false), !dbg !7
  102. // CHECK:STDOUT: store i32 4, ptr @_Cy.Main, align 4, !dbg !8
  103. // CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 @_Cz.Main, ptr align 4 @tuple.092.loc8_1, i64 8, i1 false), !dbg !9
  104. // CHECK:STDOUT: ret void, !dbg !10
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
  108. // CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias writeonly captures(none), ptr noalias readonly captures(none), i64, i1 immarg) #1
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: ; uselistorder directives
  111. // CHECK:STDOUT: uselistorder ptr @llvm.memcpy.p0.p0.i64, { 1, 0 }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: attributes #0 = { nounwind }
  114. // CHECK:STDOUT: attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  117. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  120. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  121. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  122. // CHECK:STDOUT: !3 = !DIFile(filename: "tuple_pattern.carbon", directory: "")
  123. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "__global_init", linkageName: "_C__global_init.Main", scope: null, file: !3, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  124. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  125. // CHECK:STDOUT: !6 = !{null}
  126. // CHECK:STDOUT: !7 = !DILocation(line: 4, column: 1, scope: !4)
  127. // CHECK:STDOUT: !8 = !DILocation(line: 6, column: 14, scope: !4)
  128. // CHECK:STDOUT: !9 = !DILocation(line: 8, column: 1, scope: !4)
  129. // CHECK:STDOUT: !10 = !DILocation(line: 0, scope: !4)
  130. // CHECK:STDOUT: ; ModuleID = 'import_tuple_pattern.carbon'
  131. // CHECK:STDOUT: source_filename = "import_tuple_pattern.carbon"
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: @_Cv.Main = external global { i32, i32 }
  134. // CHECK:STDOUT: @_Cy.Main = external global i32
  135. // CHECK:STDOUT: @_Cz.Main = external global { i32, i32 }
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: ; Function Attrs: nounwind
  138. // CHECK:STDOUT: define i32 @_CV.Main() #0 !dbg !4 {
  139. // CHECK:STDOUT: entry:
  140. // CHECK:STDOUT: %.loc6_24 = load i32, ptr @_Cv.Main, align 4, !dbg !8
  141. // CHECK:STDOUT: ret i32 %.loc6_24, !dbg !9
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: ; Function Attrs: nounwind
  145. // CHECK:STDOUT: define i32 @_CW.Main() #0 !dbg !10 {
  146. // CHECK:STDOUT: entry:
  147. // CHECK:STDOUT: %.loc7_24 = load i32, ptr getelementptr inbounds nuw ({ i32, i32 }, ptr @_Cv.Main, i32 0, i32 1), align 4, !dbg !11
  148. // CHECK:STDOUT: ret i32 %.loc7_24, !dbg !12
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: ; Function Attrs: nounwind
  152. // CHECK:STDOUT: define i32 @_CY.Main() #0 !dbg !13 {
  153. // CHECK:STDOUT: entry:
  154. // CHECK:STDOUT: %.loc9_24 = load i32, ptr @_Cy.Main, align 4, !dbg !14
  155. // CHECK:STDOUT: ret i32 %.loc9_24, !dbg !15
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: ; Function Attrs: nounwind
  159. // CHECK:STDOUT: define i32 @_CZ.Main() #0 !dbg !16 {
  160. // CHECK:STDOUT: entry:
  161. // CHECK:STDOUT: %.loc10_24 = load i32, ptr getelementptr inbounds nuw ({ i32, i32 }, ptr @_Cz.Main, i32 0, i32 1), align 4, !dbg !17
  162. // CHECK:STDOUT: ret i32 %.loc10_24, !dbg !18
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: ; uselistorder directives
  166. // CHECK:STDOUT: uselistorder ptr @_Cv.Main, { 1, 0 }
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: attributes #0 = { nounwind }
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  171. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  174. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  175. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  176. // CHECK:STDOUT: !3 = !DIFile(filename: "import_tuple_pattern.carbon", directory: "")
  177. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "V", linkageName: "_CV.Main", scope: null, file: !3, line: 6, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  178. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  179. // CHECK:STDOUT: !6 = !{!7}
  180. // CHECK:STDOUT: !7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
  181. // CHECK:STDOUT: !8 = !DILocation(line: 6, column: 24, scope: !4)
  182. // CHECK:STDOUT: !9 = !DILocation(line: 6, column: 17, scope: !4)
  183. // CHECK:STDOUT: !10 = distinct !DISubprogram(name: "W", linkageName: "_CW.Main", scope: null, file: !3, line: 7, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  184. // CHECK:STDOUT: !11 = !DILocation(line: 7, column: 24, scope: !10)
  185. // CHECK:STDOUT: !12 = !DILocation(line: 7, column: 17, scope: !10)
  186. // CHECK:STDOUT: !13 = distinct !DISubprogram(name: "Y", linkageName: "_CY.Main", scope: null, file: !3, line: 9, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  187. // CHECK:STDOUT: !14 = !DILocation(line: 9, column: 24, scope: !13)
  188. // CHECK:STDOUT: !15 = !DILocation(line: 9, column: 17, scope: !13)
  189. // CHECK:STDOUT: !16 = distinct !DISubprogram(name: "Z", linkageName: "_CZ.Main", scope: null, file: !3, line: 10, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  190. // CHECK:STDOUT: !17 = !DILocation(line: 10, column: 24, scope: !16)
  191. // CHECK:STDOUT: !18 = !DILocation(line: 10, column: 17, scope: !16)