import.carbon 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/check/testdata/const/import.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/const/import.carbon
  10. // --- implicit.carbon
  11. package Implicit;
  12. fn F() -> const i32;
  13. var a_ref: const i32 = F();
  14. var a_ptr_ref: const i32* = &a_ref;
  15. // --- implicit.impl.carbon
  16. impl package Implicit;
  17. // Take a reference to avoid unsupported copy logic. This still validates the
  18. // `const` is handled.
  19. var a: const i32* = &a_ref;
  20. var a_ptr: const i32* = a_ptr_ref;
  21. // CHECK:STDOUT: --- implicit.carbon
  22. // CHECK:STDOUT:
  23. // CHECK:STDOUT: constants {
  24. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  25. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  26. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  27. // CHECK:STDOUT: %.2: type = const_type i32 [template]
  28. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  29. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  30. // CHECK:STDOUT: %.3: type = ptr_type %.2 [template]
  31. // CHECK:STDOUT: }
  32. // CHECK:STDOUT:
  33. // CHECK:STDOUT: imports {
  34. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  35. // CHECK:STDOUT: .Int32 = %import_ref
  36. // CHECK:STDOUT: import Core//prelude
  37. // CHECK:STDOUT: import Core//prelude/operators
  38. // CHECK:STDOUT: import Core//prelude/types
  39. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  40. // CHECK:STDOUT: import Core//prelude/operators/as
  41. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  42. // CHECK:STDOUT: import Core//prelude/operators/comparison
  43. // CHECK:STDOUT: import Core//prelude/types/bool
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: file {
  49. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  50. // CHECK:STDOUT: .Core = imports.%Core
  51. // CHECK:STDOUT: .F = %F.decl
  52. // CHECK:STDOUT: .a_ref = %a_ref
  53. // CHECK:STDOUT: .a_ptr_ref = %a_ptr_ref
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT: %Core.import = import Core
  56. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {
  57. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  58. // CHECK:STDOUT: %.loc4_11.1: type = value_of_initializer %int.make_type_32 [template = i32]
  59. // CHECK:STDOUT: %.loc4_11.2: type = converted %int.make_type_32, %.loc4_11.1 [template = i32]
  60. // CHECK:STDOUT: %.loc4_11.3: type = const_type i32 [template = constants.%.2]
  61. // CHECK:STDOUT: %return: ref %.2 = var <return slot>
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: %int.make_type_32.loc6: init type = call constants.%Int32() [template = i32]
  64. // CHECK:STDOUT: %.loc6_12.1: type = value_of_initializer %int.make_type_32.loc6 [template = i32]
  65. // CHECK:STDOUT: %.loc6_12.2: type = converted %int.make_type_32.loc6, %.loc6_12.1 [template = i32]
  66. // CHECK:STDOUT: %.loc6_12.3: type = const_type i32 [template = constants.%.2]
  67. // CHECK:STDOUT: %a_ref.var: ref %.2 = var a_ref
  68. // CHECK:STDOUT: %a_ref: ref %.2 = bind_name a_ref, %a_ref.var
  69. // CHECK:STDOUT: %int.make_type_32.loc7: init type = call constants.%Int32() [template = i32]
  70. // CHECK:STDOUT: %.loc7_16.1: type = value_of_initializer %int.make_type_32.loc7 [template = i32]
  71. // CHECK:STDOUT: %.loc7_16.2: type = converted %int.make_type_32.loc7, %.loc7_16.1 [template = i32]
  72. // CHECK:STDOUT: %.loc7_16.3: type = const_type i32 [template = constants.%.2]
  73. // CHECK:STDOUT: %.loc7_25: type = ptr_type %.2 [template = constants.%.3]
  74. // CHECK:STDOUT: %a_ptr_ref.var: ref %.3 = var a_ptr_ref
  75. // CHECK:STDOUT: %a_ptr_ref: ref %.3 = bind_name a_ptr_ref, %a_ptr_ref.var
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: fn @F() -> %.2;
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: fn @__global_init() {
  83. // CHECK:STDOUT: !entry:
  84. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [template = constants.%F]
  85. // CHECK:STDOUT: %F.call: init %.2 = call %F.ref()
  86. // CHECK:STDOUT: assign file.%a_ref.var, %F.call
  87. // CHECK:STDOUT: %a_ref.ref: ref %.2 = name_ref a_ref, file.%a_ref
  88. // CHECK:STDOUT: %.loc7: %.3 = addr_of %a_ref.ref
  89. // CHECK:STDOUT: assign file.%a_ptr_ref.var, %.loc7
  90. // CHECK:STDOUT: return
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: --- implicit.impl.carbon
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: constants {
  96. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  97. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  98. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  99. // CHECK:STDOUT: %.2: type = const_type i32 [template]
  100. // CHECK:STDOUT: %.3: type = ptr_type %.2 [template]
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: imports {
  104. // CHECK:STDOUT: %import_ref.1 = import_ref Implicit//default, inst+15, unloaded
  105. // CHECK:STDOUT: %import_ref.2: ref %.2 = import_ref Implicit//default, inst+23, loaded
  106. // CHECK:STDOUT: %import_ref.3: ref %.3 = import_ref Implicit//default, inst+34, loaded
  107. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  108. // CHECK:STDOUT: .Int32 = %import_ref.4
  109. // CHECK:STDOUT: import Core//prelude
  110. // CHECK:STDOUT: import Core//prelude/operators
  111. // CHECK:STDOUT: import Core//prelude/types
  112. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  113. // CHECK:STDOUT: import Core//prelude/operators/as
  114. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  115. // CHECK:STDOUT: import Core//prelude/operators/comparison
  116. // CHECK:STDOUT: import Core//prelude/types/bool
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: file {
  122. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  123. // CHECK:STDOUT: .F = imports.%import_ref.1
  124. // CHECK:STDOUT: .a_ref = imports.%import_ref.2
  125. // CHECK:STDOUT: .a_ptr_ref = imports.%import_ref.3
  126. // CHECK:STDOUT: .Core = imports.%Core
  127. // CHECK:STDOUT: .a = %a
  128. // CHECK:STDOUT: .a_ptr = %a_ptr
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT: %Implicit.import = import Implicit
  131. // CHECK:STDOUT: %default.import = import <invalid>
  132. // CHECK:STDOUT: %Core.import = import Core
  133. // CHECK:STDOUT: %int.make_type_32.loc6: init type = call constants.%Int32() [template = i32]
  134. // CHECK:STDOUT: %.loc6_8.1: type = value_of_initializer %int.make_type_32.loc6 [template = i32]
  135. // CHECK:STDOUT: %.loc6_8.2: type = converted %int.make_type_32.loc6, %.loc6_8.1 [template = i32]
  136. // CHECK:STDOUT: %.loc6_8.3: type = const_type i32 [template = constants.%.2]
  137. // CHECK:STDOUT: %.loc6_17: type = ptr_type %.2 [template = constants.%.3]
  138. // CHECK:STDOUT: %a.var: ref %.3 = var a
  139. // CHECK:STDOUT: %a: ref %.3 = bind_name a, %a.var
  140. // CHECK:STDOUT: %int.make_type_32.loc7: init type = call constants.%Int32() [template = i32]
  141. // CHECK:STDOUT: %.loc7_12.1: type = value_of_initializer %int.make_type_32.loc7 [template = i32]
  142. // CHECK:STDOUT: %.loc7_12.2: type = converted %int.make_type_32.loc7, %.loc7_12.1 [template = i32]
  143. // CHECK:STDOUT: %.loc7_12.3: type = const_type i32 [template = constants.%.2]
  144. // CHECK:STDOUT: %.loc7_21: type = ptr_type %.2 [template = constants.%.3]
  145. // CHECK:STDOUT: %a_ptr.var: ref %.3 = var a_ptr
  146. // CHECK:STDOUT: %a_ptr: ref %.3 = bind_name a_ptr, %a_ptr.var
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: fn @__global_init() {
  152. // CHECK:STDOUT: !entry:
  153. // CHECK:STDOUT: %a_ref.ref: ref %.2 = name_ref a_ref, imports.%import_ref.2
  154. // CHECK:STDOUT: %.loc6: %.3 = addr_of %a_ref.ref
  155. // CHECK:STDOUT: assign file.%a.var, %.loc6
  156. // CHECK:STDOUT: %a_ptr_ref.ref: ref %.3 = name_ref a_ptr_ref, imports.%import_ref.3
  157. // CHECK:STDOUT: %.loc7: %.3 = bind_value %a_ptr_ref.ref
  158. // CHECK:STDOUT: assign file.%a_ptr.var, %.loc7
  159. // CHECK:STDOUT: return
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT: