local.carbon 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/class/local.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/local.carbon
  10. class A {
  11. fn F() -> i32 {
  12. class B {
  13. fn Make() -> Self {
  14. returned var b: Self = {.n = 1};
  15. return var;
  16. }
  17. var n: i32;
  18. }
  19. return B.Make().n;
  20. }
  21. }
  22. // CHECK:STDOUT: --- local.carbon
  23. // CHECK:STDOUT:
  24. // CHECK:STDOUT: constants {
  25. // CHECK:STDOUT: %A: type = class_type @A [concrete]
  26. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  27. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  28. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  29. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  30. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  31. // CHECK:STDOUT: %complete_type.357: <witness> = complete_type_witness %empty_struct_type [concrete]
  32. // CHECK:STDOUT: %B: type = class_type @B [concrete]
  33. // CHECK:STDOUT: %Make.type: type = fn_type @Make [concrete]
  34. // CHECK:STDOUT: %Make: %Make.type = struct_value () [concrete]
  35. // CHECK:STDOUT: %B.elem: type = unbound_element_type %B, %i32 [concrete]
  36. // CHECK:STDOUT: %struct_type.n.033: type = struct_type {.n: %i32} [concrete]
  37. // CHECK:STDOUT: %complete_type.54b: <witness> = complete_type_witness %struct_type.n.033 [concrete]
  38. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [concrete]
  39. // CHECK:STDOUT: %struct_type.n.44a: type = struct_type {.n: Core.IntLiteral} [concrete]
  40. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  41. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [concrete]
  42. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.4f9(%int_32) [concrete]
  43. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.4f9(%int_32) [concrete]
  44. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [concrete]
  45. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, (%impl_witness.d39) [concrete]
  46. // CHECK:STDOUT: %.be7: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [concrete]
  47. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_1.5b8, %Convert.956 [concrete]
  48. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.956, @Convert.2(%int_32) [concrete]
  49. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_1.5b8, %Convert.specific_fn [concrete]
  50. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [concrete]
  51. // CHECK:STDOUT: %B.val: %B = struct_value (%int_1.5d2) [concrete]
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT:
  54. // CHECK:STDOUT: imports {
  55. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  56. // CHECK:STDOUT: .Int = %Core.Int
  57. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  58. // CHECK:STDOUT: import Core//prelude
  59. // CHECK:STDOUT: import Core//prelude/...
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: file {
  64. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  65. // CHECK:STDOUT: .Core = imports.%Core
  66. // CHECK:STDOUT: .A = %A.decl
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT: %Core.import = import Core
  69. // CHECK:STDOUT: %A.decl: type = class_decl @A [concrete = constants.%A] {} {}
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: class @A {
  73. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  74. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  75. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, call_param0
  76. // CHECK:STDOUT: } {
  77. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  78. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  79. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param0
  80. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type.357]
  83. // CHECK:STDOUT: complete_type_witness = %complete_type
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: !members:
  86. // CHECK:STDOUT: .Self = constants.%A
  87. // CHECK:STDOUT: .F = %F.decl
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: class @B {
  91. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [concrete = constants.%Make] {
  92. // CHECK:STDOUT: %return.patt: %B = return_slot_pattern
  93. // CHECK:STDOUT: %return.param_patt: %B = out_param_pattern %return.patt, call_param0
  94. // CHECK:STDOUT: } {
  95. // CHECK:STDOUT: %Self.ref.loc14: type = name_ref Self, constants.%B [concrete = constants.%B]
  96. // CHECK:STDOUT: %return.param: ref %B = out_param call_param0
  97. // CHECK:STDOUT: %return: ref %B = return_slot %return.param
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: %.loc19_12: %B.elem = field_decl n, element0 [concrete]
  100. // CHECK:STDOUT: name_binding_decl {
  101. // CHECK:STDOUT: %.loc19_7: %B.elem = var_pattern %.loc19_12
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: %.var: ref %B.elem = var <none>
  104. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.n.033 [concrete = constants.%complete_type.54b]
  105. // CHECK:STDOUT: complete_type_witness = %complete_type
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: !members:
  108. // CHECK:STDOUT: .Self = constants.%B
  109. // CHECK:STDOUT: .Make = %Make.decl
  110. // CHECK:STDOUT: .n = %.loc19_12
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: fn @F() -> %i32 {
  114. // CHECK:STDOUT: !entry:
  115. // CHECK:STDOUT: %B.decl: type = class_decl @B [concrete = constants.%B] {} {}
  116. // CHECK:STDOUT: %B.ref: type = name_ref B, %B.decl [concrete = constants.%B]
  117. // CHECK:STDOUT: %Make.ref: %Make.type = name_ref Make, @B.%Make.decl [concrete = constants.%Make]
  118. // CHECK:STDOUT: %.loc22_19.1: ref %B = temporary_storage
  119. // CHECK:STDOUT: %Make.call: init %B = call %Make.ref() to %.loc22_19.1
  120. // CHECK:STDOUT: %.loc22_19.2: ref %B = temporary %.loc22_19.1, %Make.call
  121. // CHECK:STDOUT: %n.ref: %B.elem = name_ref n, @B.%.loc19_12 [concrete = @B.%.loc19_12]
  122. // CHECK:STDOUT: %.loc22_20.1: ref %i32 = class_element_access %.loc22_19.2, element0
  123. // CHECK:STDOUT: %.loc22_20.2: %i32 = bind_value %.loc22_20.1
  124. // CHECK:STDOUT: return %.loc22_20.2
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: fn @Make() -> %return.param_patt: %B {
  128. // CHECK:STDOUT: !entry:
  129. // CHECK:STDOUT: name_binding_decl {
  130. // CHECK:STDOUT: %b.patt: %B = binding_pattern b
  131. // CHECK:STDOUT: %.loc15_18.1: %B = var_pattern %b.patt
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8]
  134. // CHECK:STDOUT: %.loc15_39.1: %struct_type.n.44a = struct_literal (%int_1)
  135. // CHECK:STDOUT: %impl.elem0: %.be7 = impl_witness_access constants.%impl_witness.d39, element0 [concrete = constants.%Convert.956]
  136. // CHECK:STDOUT: %bound_method.loc15_39.1: <bound method> = bound_method %int_1, %impl.elem0 [concrete = constants.%Convert.bound]
  137. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  138. // CHECK:STDOUT: %bound_method.loc15_39.2: <bound method> = bound_method %int_1, %specific_fn [concrete = constants.%bound_method]
  139. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %bound_method.loc15_39.2(%int_1) [concrete = constants.%int_1.5d2]
  140. // CHECK:STDOUT: %.loc15_39.2: init %i32 = converted %int_1, %int.convert_checked [concrete = constants.%int_1.5d2]
  141. // CHECK:STDOUT: %.loc15_39.3: ref %i32 = class_element_access %return, element0
  142. // CHECK:STDOUT: %.loc15_39.4: init %i32 = initialize_from %.loc15_39.2 to %.loc15_39.3 [concrete = constants.%int_1.5d2]
  143. // CHECK:STDOUT: %.loc15_39.5: init %B = class_init (%.loc15_39.4), %return [concrete = constants.%B.val]
  144. // CHECK:STDOUT: %.loc15_18.2: init %B = converted %.loc15_39.1, %.loc15_39.5 [concrete = constants.%B.val]
  145. // CHECK:STDOUT: assign %return, %.loc15_18.2
  146. // CHECK:STDOUT: %Self.ref.loc15: type = name_ref Self, constants.%B [concrete = constants.%B]
  147. // CHECK:STDOUT: %b: ref %B = bind_name b, %return
  148. // CHECK:STDOUT: return %b to %return
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT: