scope.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  7. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  8. //
  9. // AUTOUPDATE
  10. // TIP: To test this file alone, run:
  11. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/scope.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/scope.carbon
  14. class Class {
  15. fn F() -> i32 {
  16. return 1;
  17. }
  18. fn G() -> i32 {
  19. return F();
  20. }
  21. }
  22. fn F() -> i32 {
  23. return 2;
  24. }
  25. fn Run() {
  26. var a: i32 = F();
  27. var b: i32 = Class.F();
  28. }
  29. // CHECK:STDOUT: --- scope.carbon
  30. // CHECK:STDOUT:
  31. // CHECK:STDOUT: constants {
  32. // CHECK:STDOUT: %Class: type = class_type @Class [concrete]
  33. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  34. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  35. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  36. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  37. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  38. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  39. // CHECK:STDOUT: %F.type.f1b: type = fn_type @F.1 [concrete]
  40. // CHECK:STDOUT: %F.1f2: %F.type.f1b = struct_value () [concrete]
  41. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  42. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  43. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  44. // CHECK:STDOUT: %complete_type.357: <witness> = complete_type_witness %empty_struct_type [concrete]
  45. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [concrete]
  46. // CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete]
  47. // CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete]
  48. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  49. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [concrete]
  50. // CHECK:STDOUT: %To: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic]
  51. // CHECK:STDOUT: %Convert.type.0f9: type = fn_type @Convert.2, @impl.4f9(%To) [symbolic]
  52. // CHECK:STDOUT: %Convert.f06: %Convert.type.0f9 = struct_value () [symbolic]
  53. // CHECK:STDOUT: %ImplicitAs.impl_witness.c75: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.a2f, @impl.4f9(%int_32) [concrete]
  54. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.4f9(%int_32) [concrete]
  55. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [concrete]
  56. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.c75) [concrete]
  57. // CHECK:STDOUT: %.9c3: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [concrete]
  58. // CHECK:STDOUT: %Convert.bound.ab5: <bound method> = bound_method %int_1.5b8, %Convert.956 [concrete]
  59. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.956, @Convert.2(%int_32) [concrete]
  60. // CHECK:STDOUT: %bound_method.9a1: <bound method> = bound_method %int_1.5b8, %Convert.specific_fn [concrete]
  61. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [concrete]
  62. // CHECK:STDOUT: %F.type.b25: type = fn_type @F.2 [concrete]
  63. // CHECK:STDOUT: %F.c41: %F.type.b25 = struct_value () [concrete]
  64. // CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [concrete]
  65. // CHECK:STDOUT: %Convert.bound.ef9: <bound method> = bound_method %int_2.ecc, %Convert.956 [concrete]
  66. // CHECK:STDOUT: %bound_method.b92: <bound method> = bound_method %int_2.ecc, %Convert.specific_fn [concrete]
  67. // CHECK:STDOUT: %int_2.ef8: %i32 = int_value 2 [concrete]
  68. // CHECK:STDOUT: %Run.type: type = fn_type @Run [concrete]
  69. // CHECK:STDOUT: %Run: %Run.type = struct_value () [concrete]
  70. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  71. // CHECK:STDOUT: %Op.type.bae: type = fn_type @Op.2 [concrete]
  72. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic]
  73. // CHECK:STDOUT: %Op.type.bc9: type = fn_type @Op.3, @impl.49c(%T) [symbolic]
  74. // CHECK:STDOUT: %Op.46f: %Op.type.bc9 = struct_value () [symbolic]
  75. // CHECK:STDOUT: %Destroy.impl_witness.64e: <witness> = impl_witness imports.%Destroy.impl_witness_table, @impl.49c(%i32) [concrete]
  76. // CHECK:STDOUT: %Op.type.a17: type = fn_type @Op.3, @impl.49c(%i32) [concrete]
  77. // CHECK:STDOUT: %Op.e6a: %Op.type.a17 = struct_value () [concrete]
  78. // CHECK:STDOUT: %Destroy.facet: %Destroy.type = facet_value %i32, (%Destroy.impl_witness.64e) [concrete]
  79. // CHECK:STDOUT: %.ab7: type = fn_type_with_self_type %Op.type.bae, %Destroy.facet [concrete]
  80. // CHECK:STDOUT: %Op.specific_fn: <specific function> = specific_function %Op.e6a, @Op.3(%i32) [concrete]
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: imports {
  84. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  85. // CHECK:STDOUT: .Int = %Core.Int
  86. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  87. // CHECK:STDOUT: .Destroy = %Core.Destroy
  88. // CHECK:STDOUT: import Core//prelude
  89. // CHECK:STDOUT: import Core//prelude/...
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  92. // CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/parts/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic]
  93. // CHECK:STDOUT: %Core.import_ref.a5b: @impl.4f9.%Convert.type (%Convert.type.0f9) = import_ref Core//prelude/parts/int, loc16_39, loaded [symbolic = @impl.4f9.%Convert (constants.%Convert.f06)]
  94. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.a2f = impl_witness_table (%Core.import_ref.a5b), @impl.4f9 [concrete]
  95. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  96. // CHECK:STDOUT: %Core.import_ref.0b9: @impl.49c.%Op.type (%Op.type.bc9) = import_ref Core//prelude/parts/destroy, loc8_23, loaded [symbolic = @impl.49c.%Op (constants.%Op.46f)]
  97. // CHECK:STDOUT: %Destroy.impl_witness_table = impl_witness_table (%Core.import_ref.0b9), @impl.49c [concrete]
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: file {
  101. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  102. // CHECK:STDOUT: .Core = imports.%Core
  103. // CHECK:STDOUT: .Class = %Class.decl
  104. // CHECK:STDOUT: .F = %F.decl
  105. // CHECK:STDOUT: .Run = %Run.decl
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT: %Core.import = import Core
  108. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [concrete = constants.%Class] {} {}
  109. // CHECK:STDOUT: %F.decl: %F.type.b25 = fn_decl @F.2 [concrete = constants.%F.c41] {
  110. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  111. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param0 [concrete]
  112. // CHECK:STDOUT: } {
  113. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  114. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  115. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param0
  116. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [concrete = constants.%Run] {} {}
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: class @Class {
  122. // CHECK:STDOUT: %F.decl: %F.type.f1b = fn_decl @F.1 [concrete = constants.%F.1f2] {
  123. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  124. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param0 [concrete]
  125. // CHECK:STDOUT: } {
  126. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  127. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  128. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param0
  129. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {
  132. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  133. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param0 [concrete]
  134. // CHECK:STDOUT: } {
  135. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  136. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  137. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param0
  138. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete = constants.%empty_struct_type]
  141. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type.357]
  142. // CHECK:STDOUT: complete_type_witness = %complete_type
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: !members:
  145. // CHECK:STDOUT: .Self = constants.%Class
  146. // CHECK:STDOUT: .F = %F.decl
  147. // CHECK:STDOUT: .G = %G.decl
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: fn @F.1() -> %i32 {
  151. // CHECK:STDOUT: !entry:
  152. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8]
  153. // CHECK:STDOUT: %impl.elem0: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Convert.956]
  154. // CHECK:STDOUT: %bound_method.loc17_13.1: <bound method> = bound_method %int_1, %impl.elem0 [concrete = constants.%Convert.bound.ab5]
  155. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  156. // CHECK:STDOUT: %bound_method.loc17_13.2: <bound method> = bound_method %int_1, %specific_fn [concrete = constants.%bound_method.9a1]
  157. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %bound_method.loc17_13.2(%int_1) [concrete = constants.%int_1.5d2]
  158. // CHECK:STDOUT: %.loc17_13.1: %i32 = value_of_initializer %int.convert_checked [concrete = constants.%int_1.5d2]
  159. // CHECK:STDOUT: %.loc17_13.2: %i32 = converted %int_1, %.loc17_13.1 [concrete = constants.%int_1.5d2]
  160. // CHECK:STDOUT: return %.loc17_13.2
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: fn @G() -> %i32 {
  164. // CHECK:STDOUT: !entry:
  165. // CHECK:STDOUT: %F.ref: %F.type.f1b = name_ref F, @Class.%F.decl [concrete = constants.%F.1f2]
  166. // CHECK:STDOUT: %F.call: init %i32 = call %F.ref()
  167. // CHECK:STDOUT: %.loc21_15.1: %i32 = value_of_initializer %F.call
  168. // CHECK:STDOUT: %.loc21_15.2: %i32 = converted %F.call, %.loc21_15.1
  169. // CHECK:STDOUT: return %.loc21_15.2
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: fn @F.2() -> %i32 {
  173. // CHECK:STDOUT: !entry:
  174. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2.ecc]
  175. // CHECK:STDOUT: %impl.elem0: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Convert.956]
  176. // CHECK:STDOUT: %bound_method.loc26_11.1: <bound method> = bound_method %int_2, %impl.elem0 [concrete = constants.%Convert.bound.ef9]
  177. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  178. // CHECK:STDOUT: %bound_method.loc26_11.2: <bound method> = bound_method %int_2, %specific_fn [concrete = constants.%bound_method.b92]
  179. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %bound_method.loc26_11.2(%int_2) [concrete = constants.%int_2.ef8]
  180. // CHECK:STDOUT: %.loc26_11.1: %i32 = value_of_initializer %int.convert_checked [concrete = constants.%int_2.ef8]
  181. // CHECK:STDOUT: %.loc26_11.2: %i32 = converted %int_2, %.loc26_11.1 [concrete = constants.%int_2.ef8]
  182. // CHECK:STDOUT: return %.loc26_11.2
  183. // CHECK:STDOUT: }
  184. // CHECK:STDOUT:
  185. // CHECK:STDOUT: fn @Run() {
  186. // CHECK:STDOUT: !entry:
  187. // CHECK:STDOUT: name_binding_decl {
  188. // CHECK:STDOUT: %a.patt: %pattern_type.7ce = binding_pattern a [concrete]
  189. // CHECK:STDOUT: %a.var_patt: %pattern_type.7ce = var_pattern %a.patt [concrete]
  190. // CHECK:STDOUT: }
  191. // CHECK:STDOUT: %a.var: ref %i32 = var %a.var_patt
  192. // CHECK:STDOUT: %F.ref.loc30: %F.type.b25 = name_ref F, file.%F.decl [concrete = constants.%F.c41]
  193. // CHECK:STDOUT: %F.call.loc30: init %i32 = call %F.ref.loc30()
  194. // CHECK:STDOUT: assign %a.var, %F.call.loc30
  195. // CHECK:STDOUT: %.loc30_10: type = splice_block %i32.loc30 [concrete = constants.%i32] {
  196. // CHECK:STDOUT: %int_32.loc30: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  197. // CHECK:STDOUT: %i32.loc30: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  198. // CHECK:STDOUT: }
  199. // CHECK:STDOUT: %a: ref %i32 = bind_name a, %a.var
  200. // CHECK:STDOUT: name_binding_decl {
  201. // CHECK:STDOUT: %b.patt: %pattern_type.7ce = binding_pattern b [concrete]
  202. // CHECK:STDOUT: %b.var_patt: %pattern_type.7ce = var_pattern %b.patt [concrete]
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT: %b.var: ref %i32 = var %b.var_patt
  205. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [concrete = constants.%Class]
  206. // CHECK:STDOUT: %F.ref.loc31: %F.type.f1b = name_ref F, @Class.%F.decl [concrete = constants.%F.1f2]
  207. // CHECK:STDOUT: %F.call.loc31: init %i32 = call %F.ref.loc31()
  208. // CHECK:STDOUT: assign %b.var, %F.call.loc31
  209. // CHECK:STDOUT: %.loc31_10: type = splice_block %i32.loc31 [concrete = constants.%i32] {
  210. // CHECK:STDOUT: %int_32.loc31: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  211. // CHECK:STDOUT: %i32.loc31: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  212. // CHECK:STDOUT: }
  213. // CHECK:STDOUT: %b: ref %i32 = bind_name b, %b.var
  214. // CHECK:STDOUT: %impl.elem0.loc31: %.ab7 = impl_witness_access constants.%Destroy.impl_witness.64e, element0 [concrete = constants.%Op.e6a]
  215. // CHECK:STDOUT: %bound_method.loc31_3.1: <bound method> = bound_method %b.var, %impl.elem0.loc31
  216. // CHECK:STDOUT: %specific_fn.loc31: <specific function> = specific_function %impl.elem0.loc31, @Op.3(constants.%i32) [concrete = constants.%Op.specific_fn]
  217. // CHECK:STDOUT: %bound_method.loc31_3.2: <bound method> = bound_method %b.var, %specific_fn.loc31
  218. // CHECK:STDOUT: %.loc31_3: %i32 = bind_value %b.var
  219. // CHECK:STDOUT: %no_op.loc31: init %empty_tuple.type = call %bound_method.loc31_3.2(%.loc31_3)
  220. // CHECK:STDOUT: %impl.elem0.loc30: %.ab7 = impl_witness_access constants.%Destroy.impl_witness.64e, element0 [concrete = constants.%Op.e6a]
  221. // CHECK:STDOUT: %bound_method.loc30_3.1: <bound method> = bound_method %a.var, %impl.elem0.loc30
  222. // CHECK:STDOUT: %specific_fn.loc30: <specific function> = specific_function %impl.elem0.loc30, @Op.3(constants.%i32) [concrete = constants.%Op.specific_fn]
  223. // CHECK:STDOUT: %bound_method.loc30_3.2: <bound method> = bound_method %a.var, %specific_fn.loc30
  224. // CHECK:STDOUT: %.loc30_3: %i32 = bind_value %a.var
  225. // CHECK:STDOUT: %no_op.loc30: init %empty_tuple.type = call %bound_method.loc30_3.2(%.loc30_3)
  226. // CHECK:STDOUT: return
  227. // CHECK:STDOUT: }
  228. // CHECK:STDOUT: