local.carbon 8.3 KB

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