sub.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/operators/overloaded/sub.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/operators/overloaded/sub.carbon
  10. // This file was generated from binary_op.carbon.tmpl. Run make_tests.sh to regenerate.
  11. package User;
  12. class C {};
  13. impl C as Core.Sub {
  14. fn Op[self: C](other: C) -> C {
  15. return {};
  16. }
  17. }
  18. impl C as Core.SubAssign {
  19. fn Op[addr self: C*](other: C) {}
  20. }
  21. fn TestOp(a: C, b: C) -> C {
  22. return a - b;
  23. }
  24. fn TestAssign(a: C*, b: C) {
  25. *a -= b;
  26. }
  27. // CHECK:STDOUT: --- sub.carbon
  28. // CHECK:STDOUT:
  29. // CHECK:STDOUT: constants {
  30. // CHECK:STDOUT: %C: type = class_type @C [template]
  31. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  32. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  33. // CHECK:STDOUT: %Sub.type: type = facet_type <@Sub> [template]
  34. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  35. // CHECK:STDOUT: %Op.type.111: type = fn_type @Op.1 [template]
  36. // CHECK:STDOUT: %impl_witness.3b8: <witness> = impl_witness (@impl.1.%Op.decl) [template]
  37. // CHECK:STDOUT: %Op.type.c74: type = fn_type @Op.2 [template]
  38. // CHECK:STDOUT: %Op.151: %Op.type.c74 = struct_value () [template]
  39. // CHECK:STDOUT: %Sub.facet: %Sub.type = facet_value %C, %impl_witness.3b8 [template]
  40. // CHECK:STDOUT: %C.val: %C = struct_value () [template]
  41. // CHECK:STDOUT: %SubAssign.type: type = facet_type <@SubAssign> [template]
  42. // CHECK:STDOUT: %Op.type.f0d: type = fn_type @Op.3 [template]
  43. // CHECK:STDOUT: %impl_witness.91c: <witness> = impl_witness (@impl.2.%Op.decl) [template]
  44. // CHECK:STDOUT: %ptr.019: type = ptr_type %C [template]
  45. // CHECK:STDOUT: %Op.type.966: type = fn_type @Op.4 [template]
  46. // CHECK:STDOUT: %Op.a55: %Op.type.966 = struct_value () [template]
  47. // CHECK:STDOUT: %SubAssign.facet: %SubAssign.type = facet_value %C, %impl_witness.91c [template]
  48. // CHECK:STDOUT: %TestOp.type: type = fn_type @TestOp [template]
  49. // CHECK:STDOUT: %TestOp: %TestOp.type = struct_value () [template]
  50. // CHECK:STDOUT: %.f35: type = fn_type_with_self_type %Op.type.111, %Sub.facet [template]
  51. // CHECK:STDOUT: %TestAssign.type: type = fn_type @TestAssign [template]
  52. // CHECK:STDOUT: %TestAssign: %TestAssign.type = struct_value () [template]
  53. // CHECK:STDOUT: %.3d8: type = fn_type_with_self_type %Op.type.f0d, %SubAssign.facet [template]
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: imports {
  57. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  58. // CHECK:STDOUT: .Sub = %Core.Sub
  59. // CHECK:STDOUT: .SubAssign = %Core.SubAssign
  60. // CHECK:STDOUT: import Core//prelude
  61. // CHECK:STDOUT: import Core//prelude/...
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: %Core.Sub: type = import_ref Core//prelude/operators/arithmetic, Sub, loaded [template = constants.%Sub.type]
  64. // CHECK:STDOUT: %Core.SubAssign: type = import_ref Core//prelude/operators/arithmetic, SubAssign, loaded [template = constants.%SubAssign.type]
  65. // CHECK:STDOUT: }
  66. // CHECK:STDOUT:
  67. // CHECK:STDOUT: file {
  68. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  69. // CHECK:STDOUT: .Core = imports.%Core
  70. // CHECK:STDOUT: .C = %C.decl
  71. // CHECK:STDOUT: .TestOp = %TestOp.decl
  72. // CHECK:STDOUT: .TestAssign = %TestAssign.decl
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT: %Core.import = import Core
  75. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {} {}
  76. // CHECK:STDOUT: impl_decl @impl.1 [template] {} {
  77. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  78. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  79. // CHECK:STDOUT: %Sub.ref: type = name_ref Sub, imports.%Core.Sub [template = constants.%Sub.type]
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT: %impl_witness.loc17: <witness> = impl_witness (@impl.1.%Op.decl) [template = constants.%impl_witness.3b8]
  82. // CHECK:STDOUT: impl_decl @impl.2 [template] {} {
  83. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  84. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  85. // CHECK:STDOUT: %SubAssign.ref: type = name_ref SubAssign, imports.%Core.SubAssign [template = constants.%SubAssign.type]
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT: %impl_witness.loc22: <witness> = impl_witness (@impl.2.%Op.decl) [template = constants.%impl_witness.91c]
  88. // CHECK:STDOUT: %TestOp.decl: %TestOp.type = fn_decl @TestOp [template = constants.%TestOp] {
  89. // CHECK:STDOUT: %a.patt: %C = binding_pattern a
  90. // CHECK:STDOUT: %a.param_patt: %C = value_param_pattern %a.patt, runtime_param0
  91. // CHECK:STDOUT: %b.patt: %C = binding_pattern b
  92. // CHECK:STDOUT: %b.param_patt: %C = value_param_pattern %b.patt, runtime_param1
  93. // CHECK:STDOUT: %return.patt: %C = return_slot_pattern
  94. // CHECK:STDOUT: %return.param_patt: %C = out_param_pattern %return.patt, runtime_param2
  95. // CHECK:STDOUT: } {
  96. // CHECK:STDOUT: %C.ref.loc26_26: type = name_ref C, file.%C.decl [template = constants.%C]
  97. // CHECK:STDOUT: %a.param: %C = value_param runtime_param0
  98. // CHECK:STDOUT: %C.ref.loc26_14: type = name_ref C, file.%C.decl [template = constants.%C]
  99. // CHECK:STDOUT: %a: %C = bind_name a, %a.param
  100. // CHECK:STDOUT: %b.param: %C = value_param runtime_param1
  101. // CHECK:STDOUT: %C.ref.loc26_20: type = name_ref C, file.%C.decl [template = constants.%C]
  102. // CHECK:STDOUT: %b: %C = bind_name b, %b.param
  103. // CHECK:STDOUT: %return.param: ref %C = out_param runtime_param2
  104. // CHECK:STDOUT: %return: ref %C = return_slot %return.param
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT: %TestAssign.decl: %TestAssign.type = fn_decl @TestAssign [template = constants.%TestAssign] {
  107. // CHECK:STDOUT: %a.patt: %ptr.019 = binding_pattern a
  108. // CHECK:STDOUT: %a.param_patt: %ptr.019 = value_param_pattern %a.patt, runtime_param0
  109. // CHECK:STDOUT: %b.patt: %C = binding_pattern b
  110. // CHECK:STDOUT: %b.param_patt: %C = value_param_pattern %b.patt, runtime_param1
  111. // CHECK:STDOUT: } {
  112. // CHECK:STDOUT: %a.param: %ptr.019 = value_param runtime_param0
  113. // CHECK:STDOUT: %.loc30: type = splice_block %ptr [template = constants.%ptr.019] {
  114. // CHECK:STDOUT: %C.ref.loc30_18: type = name_ref C, file.%C.decl [template = constants.%C]
  115. // CHECK:STDOUT: %ptr: type = ptr_type %C [template = constants.%ptr.019]
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT: %a: %ptr.019 = bind_name a, %a.param
  118. // CHECK:STDOUT: %b.param: %C = value_param runtime_param1
  119. // CHECK:STDOUT: %C.ref.loc30_25: type = name_ref C, file.%C.decl [template = constants.%C]
  120. // CHECK:STDOUT: %b: %C = bind_name b, %b.param
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: impl @impl.1: %C.ref as %Sub.ref {
  125. // CHECK:STDOUT: %Op.decl: %Op.type.c74 = fn_decl @Op.2 [template = constants.%Op.151] {
  126. // CHECK:STDOUT: %self.patt: %C = binding_pattern self
  127. // CHECK:STDOUT: %self.param_patt: %C = value_param_pattern %self.patt, runtime_param0
  128. // CHECK:STDOUT: %other.patt: %C = binding_pattern other
  129. // CHECK:STDOUT: %other.param_patt: %C = value_param_pattern %other.patt, runtime_param1
  130. // CHECK:STDOUT: %return.patt: %C = return_slot_pattern
  131. // CHECK:STDOUT: %return.param_patt: %C = out_param_pattern %return.patt, runtime_param2
  132. // CHECK:STDOUT: } {
  133. // CHECK:STDOUT: %C.ref.loc18_31: type = name_ref C, file.%C.decl [template = constants.%C]
  134. // CHECK:STDOUT: %self.param: %C = value_param runtime_param0
  135. // CHECK:STDOUT: %C.ref.loc18_15: type = name_ref C, file.%C.decl [template = constants.%C]
  136. // CHECK:STDOUT: %self: %C = bind_name self, %self.param
  137. // CHECK:STDOUT: %other.param: %C = value_param runtime_param1
  138. // CHECK:STDOUT: %C.ref.loc18_25: type = name_ref C, file.%C.decl [template = constants.%C]
  139. // CHECK:STDOUT: %other: %C = bind_name other, %other.param
  140. // CHECK:STDOUT: %return.param: ref %C = out_param runtime_param2
  141. // CHECK:STDOUT: %return: ref %C = return_slot %return.param
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: !members:
  145. // CHECK:STDOUT: .Op = %Op.decl
  146. // CHECK:STDOUT: witness = file.%impl_witness.loc17
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: impl @impl.2: %C.ref as %SubAssign.ref {
  150. // CHECK:STDOUT: %Op.decl: %Op.type.966 = fn_decl @Op.4 [template = constants.%Op.a55] {
  151. // CHECK:STDOUT: %self.patt: %ptr.019 = binding_pattern self
  152. // CHECK:STDOUT: %self.param_patt: %ptr.019 = value_param_pattern %self.patt, runtime_param0
  153. // CHECK:STDOUT: %.loc23_9: auto = addr_pattern %self.param_patt
  154. // CHECK:STDOUT: %other.patt: %C = binding_pattern other
  155. // CHECK:STDOUT: %other.param_patt: %C = value_param_pattern %other.patt, runtime_param1
  156. // CHECK:STDOUT: } {
  157. // CHECK:STDOUT: %self.param: %ptr.019 = value_param runtime_param0
  158. // CHECK:STDOUT: %.loc23_21: type = splice_block %ptr [template = constants.%ptr.019] {
  159. // CHECK:STDOUT: %C.ref.loc23_20: type = name_ref C, file.%C.decl [template = constants.%C]
  160. // CHECK:STDOUT: %ptr: type = ptr_type %C [template = constants.%ptr.019]
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT: %self: %ptr.019 = bind_name self, %self.param
  163. // CHECK:STDOUT: %other.param: %C = value_param runtime_param1
  164. // CHECK:STDOUT: %C.ref.loc23_31: type = name_ref C, file.%C.decl [template = constants.%C]
  165. // CHECK:STDOUT: %other: %C = bind_name other, %other.param
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: !members:
  169. // CHECK:STDOUT: .Op = %Op.decl
  170. // CHECK:STDOUT: witness = file.%impl_witness.loc22
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: class @C {
  174. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  175. // CHECK:STDOUT: complete_type_witness = %complete_type
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: !members:
  178. // CHECK:STDOUT: .Self = constants.%C
  179. // CHECK:STDOUT: }
  180. // CHECK:STDOUT:
  181. // CHECK:STDOUT: fn @Op.2[%self.param_patt: %C](%other.param_patt: %C) -> %return.param_patt: %C {
  182. // CHECK:STDOUT: !entry:
  183. // CHECK:STDOUT: %.loc19_13.1: %empty_struct_type = struct_literal ()
  184. // CHECK:STDOUT: %.loc19_13.2: init %C = class_init (), %return [template = constants.%C.val]
  185. // CHECK:STDOUT: %.loc19_14: init %C = converted %.loc19_13.1, %.loc19_13.2 [template = constants.%C.val]
  186. // CHECK:STDOUT: return %.loc19_14 to %return
  187. // CHECK:STDOUT: }
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: fn @Op.4[addr %self.param_patt: %ptr.019](%other.param_patt: %C) {
  190. // CHECK:STDOUT: !entry:
  191. // CHECK:STDOUT: return
  192. // CHECK:STDOUT: }
  193. // CHECK:STDOUT:
  194. // CHECK:STDOUT: fn @TestOp(%a.param_patt: %C, %b.param_patt: %C) -> %return.param_patt: %C {
  195. // CHECK:STDOUT: !entry:
  196. // CHECK:STDOUT: %a.ref: %C = name_ref a, %a
  197. // CHECK:STDOUT: %b.ref: %C = name_ref b, %b
  198. // CHECK:STDOUT: %impl.elem0: %.f35 = impl_witness_access constants.%impl_witness.3b8, element0 [template = constants.%Op.151]
  199. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %a.ref, %impl.elem0
  200. // CHECK:STDOUT: %.loc26: ref %C = splice_block %return {}
  201. // CHECK:STDOUT: %Op.call: init %C = call %bound_method(%a.ref, %b.ref) to %.loc26
  202. // CHECK:STDOUT: return %Op.call to %return
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT:
  205. // CHECK:STDOUT: fn @TestAssign(%a.param_patt: %ptr.019, %b.param_patt: %C) {
  206. // CHECK:STDOUT: !entry:
  207. // CHECK:STDOUT: %a.ref: %ptr.019 = name_ref a, %a
  208. // CHECK:STDOUT: %.loc31: ref %C = deref %a.ref
  209. // CHECK:STDOUT: %b.ref: %C = name_ref b, %b
  210. // CHECK:STDOUT: %impl.elem0: %.3d8 = impl_witness_access constants.%impl_witness.91c, element0 [template = constants.%Op.a55]
  211. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %.loc31, %impl.elem0
  212. // CHECK:STDOUT: %addr: %ptr.019 = addr_of %.loc31
  213. // CHECK:STDOUT: %Op.call: init %empty_tuple.type = call %bound_method(%addr, %b.ref)
  214. // CHECK:STDOUT: return
  215. // CHECK:STDOUT: }
  216. // CHECK:STDOUT: