import.carbon 9.3 KB

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