import.carbon 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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: %int_32: Core.IntLiteral = int_value 32 [concrete]
  25. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  26. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  27. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  28. // CHECK:STDOUT: %const: type = const_type %i32 [concrete]
  29. // CHECK:STDOUT: %pattern_type.a65: type = pattern_type %const [concrete]
  30. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  31. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  32. // CHECK:STDOUT: %ptr: type = ptr_type %const [concrete]
  33. // CHECK:STDOUT: %pattern_type.bff: type = pattern_type %ptr [concrete]
  34. // CHECK:STDOUT: }
  35. // CHECK:STDOUT:
  36. // CHECK:STDOUT: imports {
  37. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  38. // CHECK:STDOUT: .Int = %Core.Int
  39. // CHECK:STDOUT: import Core//prelude
  40. // CHECK:STDOUT: import Core//prelude/...
  41. // CHECK:STDOUT: }
  42. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/types/int, Int, loaded [concrete = constants.%Int.generic]
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT:
  45. // CHECK:STDOUT: file {
  46. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  47. // CHECK:STDOUT: .Core = imports.%Core
  48. // CHECK:STDOUT: .F = %F.decl
  49. // CHECK:STDOUT: .a_ref = %a_ref
  50. // CHECK:STDOUT: .a_ptr_ref = %a_ptr_ref
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT: %Core.import = import Core
  53. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  54. // CHECK:STDOUT: %return.patt: %pattern_type.a65 = return_slot_pattern [concrete]
  55. // CHECK:STDOUT: %return.param_patt: %pattern_type.a65 = out_param_pattern %return.patt, call_param0 [concrete]
  56. // CHECK:STDOUT: } {
  57. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  58. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  59. // CHECK:STDOUT: %const: type = const_type %i32 [concrete = constants.%const]
  60. // CHECK:STDOUT: %return.param: ref %const = out_param call_param0
  61. // CHECK:STDOUT: %return: ref %const = return_slot %return.param
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: name_binding_decl {
  64. // CHECK:STDOUT: %a_ref.patt: %pattern_type.a65 = binding_pattern a_ref [concrete]
  65. // CHECK:STDOUT: %.loc6_1: %pattern_type.a65 = var_pattern %a_ref.patt [concrete]
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: %a_ref.var: ref %const = var a_ref
  68. // CHECK:STDOUT: %.loc6_12: type = splice_block %const.loc6 [concrete = constants.%const] {
  69. // CHECK:STDOUT: %int_32.loc6: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  70. // CHECK:STDOUT: %i32.loc6: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  71. // CHECK:STDOUT: %const.loc6: type = const_type %i32.loc6 [concrete = constants.%const]
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %a_ref: ref %const = bind_name a_ref, %a_ref.var
  74. // CHECK:STDOUT: name_binding_decl {
  75. // CHECK:STDOUT: %a_ptr_ref.patt: %pattern_type.bff = binding_pattern a_ptr_ref [concrete]
  76. // CHECK:STDOUT: %.loc7_1: %pattern_type.bff = var_pattern %a_ptr_ref.patt [concrete]
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %a_ptr_ref.var: ref %ptr = var a_ptr_ref
  79. // CHECK:STDOUT: %.loc7_25: type = splice_block %ptr [concrete = constants.%ptr] {
  80. // CHECK:STDOUT: %int_32.loc7: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  81. // CHECK:STDOUT: %i32.loc7: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  82. // CHECK:STDOUT: %const.loc7: type = const_type %i32.loc7 [concrete = constants.%const]
  83. // CHECK:STDOUT: %ptr: type = ptr_type %const.loc7 [concrete = constants.%ptr]
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT: %a_ptr_ref: ref %ptr = bind_name a_ptr_ref, %a_ptr_ref.var
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: fn @F() -> %const;
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: fn @__global_init() {
  91. // CHECK:STDOUT: !entry:
  92. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  93. // CHECK:STDOUT: %F.call: init %const = call %F.ref()
  94. // CHECK:STDOUT: assign file.%a_ref.var, %F.call
  95. // CHECK:STDOUT: %a_ref.ref: ref %const = name_ref a_ref, file.%a_ref
  96. // CHECK:STDOUT: %addr: %ptr = addr_of %a_ref.ref
  97. // CHECK:STDOUT: assign file.%a_ptr_ref.var, %addr
  98. // CHECK:STDOUT: return
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: --- implicit.impl.carbon
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: constants {
  104. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  105. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  106. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  107. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  108. // CHECK:STDOUT: %const: type = const_type %i32 [concrete]
  109. // CHECK:STDOUT: %ptr: type = ptr_type %const [concrete]
  110. // CHECK:STDOUT: %pattern_type.bff: type = pattern_type %ptr [concrete]
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: imports {
  114. // CHECK:STDOUT: %Implicit.F = import_ref Implicit//default, F, unloaded
  115. // CHECK:STDOUT: %Implicit.a_ref: ref %const = import_ref Implicit//default, a_ref, loaded
  116. // CHECK:STDOUT: %Implicit.a_ptr_ref: ref %ptr = import_ref Implicit//default, a_ptr_ref, loaded
  117. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  118. // CHECK:STDOUT: .Int = %Core.Int
  119. // CHECK:STDOUT: import Core//prelude
  120. // CHECK:STDOUT: import Core//prelude/...
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/types/int, Int, loaded [concrete = constants.%Int.generic]
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: file {
  126. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  127. // CHECK:STDOUT: .F = imports.%Implicit.F
  128. // CHECK:STDOUT: .a_ref = imports.%Implicit.a_ref
  129. // CHECK:STDOUT: .a_ptr_ref = imports.%Implicit.a_ptr_ref
  130. // CHECK:STDOUT: .Core = imports.%Core
  131. // CHECK:STDOUT: .a = %a
  132. // CHECK:STDOUT: .a_ptr = %a_ptr
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT: %Implicit.import = import Implicit
  135. // CHECK:STDOUT: %default.import = import <none>
  136. // CHECK:STDOUT: %Core.import = import Core
  137. // CHECK:STDOUT: name_binding_decl {
  138. // CHECK:STDOUT: %a.patt: %pattern_type.bff = binding_pattern a [concrete]
  139. // CHECK:STDOUT: %.loc6_1: %pattern_type.bff = var_pattern %a.patt [concrete]
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT: %a.var: ref %ptr = var a
  142. // CHECK:STDOUT: %.loc6_17: type = splice_block %ptr.loc6 [concrete = constants.%ptr] {
  143. // CHECK:STDOUT: %int_32.loc6: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  144. // CHECK:STDOUT: %i32.loc6: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  145. // CHECK:STDOUT: %const.loc6: type = const_type %i32.loc6 [concrete = constants.%const]
  146. // CHECK:STDOUT: %ptr.loc6: type = ptr_type %const.loc6 [concrete = constants.%ptr]
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT: %a: ref %ptr = bind_name a, %a.var
  149. // CHECK:STDOUT: name_binding_decl {
  150. // CHECK:STDOUT: %a_ptr.patt: %pattern_type.bff = binding_pattern a_ptr [concrete]
  151. // CHECK:STDOUT: %.loc7_1: %pattern_type.bff = var_pattern %a_ptr.patt [concrete]
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT: %a_ptr.var: ref %ptr = var a_ptr
  154. // CHECK:STDOUT: %.loc7_21: type = splice_block %ptr.loc7 [concrete = constants.%ptr] {
  155. // CHECK:STDOUT: %int_32.loc7: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  156. // CHECK:STDOUT: %i32.loc7: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  157. // CHECK:STDOUT: %const.loc7: type = const_type %i32.loc7 [concrete = constants.%const]
  158. // CHECK:STDOUT: %ptr.loc7: type = ptr_type %const.loc7 [concrete = constants.%ptr]
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT: %a_ptr: ref %ptr = bind_name a_ptr, %a_ptr.var
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: fn @__global_init() {
  164. // CHECK:STDOUT: !entry:
  165. // CHECK:STDOUT: %a_ref.ref: ref %const = name_ref a_ref, imports.%Implicit.a_ref
  166. // CHECK:STDOUT: %addr: %ptr = addr_of %a_ref.ref
  167. // CHECK:STDOUT: assign file.%a.var, %addr
  168. // CHECK:STDOUT: %a_ptr_ref.ref: ref %ptr = name_ref a_ptr_ref, imports.%Implicit.a_ptr_ref
  169. // CHECK:STDOUT: %.loc7: %ptr = bind_value %a_ptr_ref.ref
  170. // CHECK:STDOUT: assign file.%a_ptr.var, %.loc7
  171. // CHECK:STDOUT: return
  172. // CHECK:STDOUT: }
  173. // CHECK:STDOUT: