method.carbon 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/function/builtin/method.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/function/builtin/method.carbon
  10. interface I {
  11. fn F[self: Self](other: Self) -> Self;
  12. }
  13. impl i32 as I {
  14. fn F[self: i32](other: i32) -> i32 = "int.sadd";
  15. }
  16. var arr: [i32; (1 as i32).(I.F)(2)];
  17. // CHECK:STDOUT: --- method.carbon
  18. // CHECK:STDOUT:
  19. // CHECK:STDOUT: constants {
  20. // CHECK:STDOUT: %I.type: type = facet_type <@I> [template]
  21. // CHECK:STDOUT: %Self.1: %I.type = bind_symbolic_name Self, 0 [symbolic]
  22. // CHECK:STDOUT: %Self.as_type.1: type = facet_access_type %Self.1 [symbolic]
  23. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  24. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  25. // CHECK:STDOUT: %F.assoc_type: type = assoc_entity_type %I.type, %F.type.1 [template]
  26. // CHECK:STDOUT: %assoc0.1: %F.assoc_type = assoc_entity element0, @I.%F.decl [template]
  27. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  28. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  29. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  30. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  31. // CHECK:STDOUT: %I.facet: %I.type = facet_value %i32, %i32 [template]
  32. // CHECK:STDOUT: %interface.1: <witness> = interface_witness (%F.2) [template]
  33. // CHECK:STDOUT: %int_3.2: Core.IntLiteral = int_value 3 [template]
  34. // CHECK:STDOUT: %array_type: type = array_type %int_3.2, %i32 [template]
  35. // CHECK:STDOUT: }
  36. // CHECK:STDOUT:
  37. // CHECK:STDOUT: imports {
  38. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  39. // CHECK:STDOUT: .Int = %import_ref.1
  40. // CHECK:STDOUT: .As = %import_ref.5
  41. // CHECK:STDOUT: .ImplicitAs = %import_ref.229
  42. // CHECK:STDOUT: import Core//prelude
  43. // CHECK:STDOUT: import Core//prelude/...
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT:
  47. // CHECK:STDOUT: file {
  48. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  49. // CHECK:STDOUT: .Core = imports.%Core
  50. // CHECK:STDOUT: .I = %I.decl
  51. // CHECK:STDOUT: .arr = %arr
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: %Core.import = import Core
  54. // CHECK:STDOUT: %I.decl: type = interface_decl @I [template = constants.%I.type] {} {}
  55. // CHECK:STDOUT: impl_decl @impl.1 [template] {} {
  56. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  57. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  58. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [template = constants.%I.type]
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT: %arr.var: ref %array_type = var arr
  61. // CHECK:STDOUT: %arr: ref %array_type = bind_name arr, %arr.var
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: interface @I {
  65. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self.1]
  66. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  67. // CHECK:STDOUT: %self.patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = binding_pattern self
  68. // CHECK:STDOUT: %self.param_patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = value_param_pattern %self.patt, runtime_param0
  69. // CHECK:STDOUT: %other.patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = binding_pattern other
  70. // CHECK:STDOUT: %other.param_patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = value_param_pattern %other.patt, runtime_param1
  71. // CHECK:STDOUT: %return.patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = return_slot_pattern
  72. // CHECK:STDOUT: %return.param_patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = out_param_pattern %return.patt, runtime_param2
  73. // CHECK:STDOUT: } {
  74. // CHECK:STDOUT: %Self.ref.loc12_36: %I.type = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self.1)]
  75. // CHECK:STDOUT: %Self.as_type.loc12_36: type = facet_access_type %Self.ref.loc12_36 [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)]
  76. // CHECK:STDOUT: %.loc12_36: type = converted %Self.ref.loc12_36, %Self.as_type.loc12_36 [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)]
  77. // CHECK:STDOUT: %self.param: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = value_param runtime_param0
  78. // CHECK:STDOUT: %.loc12_14.1: type = splice_block %.loc12_14.2 [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)] {
  79. // CHECK:STDOUT: %Self.ref.loc12_14: %I.type = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self.1)]
  80. // CHECK:STDOUT: %Self.as_type.loc12_14.2: type = facet_access_type %Self.ref.loc12_14 [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)]
  81. // CHECK:STDOUT: %.loc12_14.2: type = converted %Self.ref.loc12_14, %Self.as_type.loc12_14.2 [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)]
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT: %self: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = bind_name self, %self.param
  84. // CHECK:STDOUT: %other.param: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = value_param runtime_param1
  85. // CHECK:STDOUT: %.loc12_27.1: type = splice_block %.loc12_27.2 [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)] {
  86. // CHECK:STDOUT: %Self.ref.loc12_27: %I.type = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self.1)]
  87. // CHECK:STDOUT: %Self.as_type.loc12_27: type = facet_access_type %Self.ref.loc12_27 [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)]
  88. // CHECK:STDOUT: %.loc12_27.2: type = converted %Self.ref.loc12_27, %Self.as_type.loc12_27 [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)]
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT: %other: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = bind_name other, %other.param
  91. // CHECK:STDOUT: %return.param: ref @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = out_param runtime_param2
  92. // CHECK:STDOUT: %return: ref @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1) = return_slot %return.param
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT: %assoc0: %F.assoc_type = assoc_entity element0, %F.decl [template = constants.%assoc0.1]
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: !members:
  97. // CHECK:STDOUT: .Self = %Self
  98. // CHECK:STDOUT: .F = %assoc0
  99. // CHECK:STDOUT: witness = (%F.decl)
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: impl @impl.1: %i32 as %I.ref {
  103. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  104. // CHECK:STDOUT: %self.patt: %i32 = binding_pattern self
  105. // CHECK:STDOUT: %self.param_patt: %i32 = value_param_pattern %self.patt, runtime_param0
  106. // CHECK:STDOUT: %other.patt: %i32 = binding_pattern other
  107. // CHECK:STDOUT: %other.param_patt: %i32 = value_param_pattern %other.patt, runtime_param1
  108. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  109. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param2
  110. // CHECK:STDOUT: } {
  111. // CHECK:STDOUT: %int_32.loc16_34: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  112. // CHECK:STDOUT: %i32.loc16_34: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  113. // CHECK:STDOUT: %self.param: %i32 = value_param runtime_param0
  114. // CHECK:STDOUT: %.loc16_14: type = splice_block %i32.loc16_14 [template = constants.%i32] {
  115. // CHECK:STDOUT: %int_32.loc16_14: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  116. // CHECK:STDOUT: %i32.loc16_14: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT: %self: %i32 = bind_name self, %self.param
  119. // CHECK:STDOUT: %other.param: %i32 = value_param runtime_param1
  120. // CHECK:STDOUT: %.loc16_26: type = splice_block %i32.loc16_26 [template = constants.%i32] {
  121. // CHECK:STDOUT: %int_32.loc16_26: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  122. // CHECK:STDOUT: %i32.loc16_26: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT: %other: %i32 = bind_name other, %other.param
  125. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param2
  126. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT: %interface: <witness> = interface_witness (%F.decl) [template = constants.%interface.1]
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: !members:
  131. // CHECK:STDOUT: .F = %F.decl
  132. // CHECK:STDOUT: witness = %interface
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT:
  135. // CHECK:STDOUT: generic fn @F.1(@I.%Self: %I.type) {
  136. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = %Self (constants.%Self.1)]
  137. // CHECK:STDOUT: %Self.as_type.loc12_14.1: type = facet_access_type %Self [symbolic = %Self.as_type.loc12_14.1 (constants.%Self.as_type.1)]
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: fn[%self.param_patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1)](%other.param_patt: @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1)) -> @F.1.%Self.as_type.loc12_14.1 (%Self.as_type.1);
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: fn @F.2[%self.param_patt: %i32](%other.param_patt: %i32) -> %i32 = "int.sadd";
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: specific @F.1(constants.%Self.1) {
  145. // CHECK:STDOUT: %Self => constants.%Self.1
  146. // CHECK:STDOUT: %Self.as_type.loc12_14.1 => constants.%Self.as_type.1
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: specific @F.1(constants.%I.facet) {
  150. // CHECK:STDOUT: %Self => constants.%I.facet
  151. // CHECK:STDOUT: %Self.as_type.loc12_14.1 => constants.%i32
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT: