import.carbon 8.2 KB

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