sub.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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: %.1: type = struct_type {} [template]
  32. // CHECK:STDOUT: %.2: type = interface_type @Sub [template]
  33. // CHECK:STDOUT: %Self.1: %.2 = bind_symbolic_name Self 0 [symbolic]
  34. // CHECK:STDOUT: %Op.type.1: type = fn_type @Op.1 [template]
  35. // CHECK:STDOUT: %.3: type = tuple_type () [template]
  36. // CHECK:STDOUT: %Op.1: %Op.type.1 = struct_value () [template]
  37. // CHECK:STDOUT: %Op.type.2: type = fn_type @Op.2 [template]
  38. // CHECK:STDOUT: %Op.2: %Op.type.2 = struct_value () [template]
  39. // CHECK:STDOUT: %.4: <witness> = interface_witness (%Op.1) [template]
  40. // CHECK:STDOUT: %.5: type = ptr_type %.1 [template]
  41. // CHECK:STDOUT: %struct: %C = struct_value () [template]
  42. // CHECK:STDOUT: %.6: type = interface_type @SubAssign [template]
  43. // CHECK:STDOUT: %Self.2: %.6 = bind_symbolic_name Self 0 [symbolic]
  44. // CHECK:STDOUT: %.7: type = ptr_type %C [template]
  45. // CHECK:STDOUT: %Op.type.3: type = fn_type @Op.3 [template]
  46. // CHECK:STDOUT: %Op.3: %Op.type.3 = struct_value () [template]
  47. // CHECK:STDOUT: %Op.type.4: type = fn_type @Op.4 [template]
  48. // CHECK:STDOUT: %Op.4: %Op.type.4 = struct_value () [template]
  49. // CHECK:STDOUT: %.8: type = ptr_type %Self.2 [symbolic]
  50. // CHECK:STDOUT: %.9: <witness> = interface_witness (%Op.3) [template]
  51. // CHECK:STDOUT: %TestOp.type: type = fn_type @TestOp [template]
  52. // CHECK:STDOUT: %TestOp: %TestOp.type = struct_value () [template]
  53. // CHECK:STDOUT: %.10: type = assoc_entity_type %.2, %Op.type.2 [template]
  54. // CHECK:STDOUT: %.11: %.10 = assoc_entity element0, imports.%import_ref.9 [template]
  55. // CHECK:STDOUT: %TestAssign.type: type = fn_type @TestAssign [template]
  56. // CHECK:STDOUT: %TestAssign: %TestAssign.type = struct_value () [template]
  57. // CHECK:STDOUT: %.12: type = assoc_entity_type %.6, %Op.type.4 [template]
  58. // CHECK:STDOUT: %.13: %.12 = assoc_entity element0, imports.%import_ref.10 [template]
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: imports {
  62. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  63. // CHECK:STDOUT: .Sub = %import_ref.1
  64. // CHECK:STDOUT: .SubAssign = %import_ref.5
  65. // CHECK:STDOUT: import Core//prelude
  66. // CHECK:STDOUT: import Core//prelude/operators
  67. // CHECK:STDOUT: import Core//prelude/types
  68. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  69. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  70. // CHECK:STDOUT: import Core//prelude/operators/comparison
  71. // CHECK:STDOUT: import Core//prelude/types/bool
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %import_ref.1: type = import_ref Core//prelude/operators/arithmetic, inst+92, loaded [template = constants.%.2]
  74. // CHECK:STDOUT: %import_ref.2 = import_ref Core//prelude/operators/arithmetic, inst+94, unloaded
  75. // CHECK:STDOUT: %import_ref.3: %.10 = import_ref Core//prelude/operators/arithmetic, inst+115, loaded [template = constants.%.11]
  76. // CHECK:STDOUT: %import_ref.4: %Op.type.2 = import_ref Core//prelude/operators/arithmetic, inst+110, loaded [template = constants.%Op.2]
  77. // CHECK:STDOUT: %import_ref.5: type = import_ref Core//prelude/operators/arithmetic, inst+117, loaded [template = constants.%.6]
  78. // CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/arithmetic, inst+119, unloaded
  79. // CHECK:STDOUT: %import_ref.7: %.12 = import_ref Core//prelude/operators/arithmetic, inst+140, loaded [template = constants.%.13]
  80. // CHECK:STDOUT: %import_ref.8: %Op.type.4 = import_ref Core//prelude/operators/arithmetic, inst+134, loaded [template = constants.%Op.4]
  81. // CHECK:STDOUT: %import_ref.9 = import_ref Core//prelude/operators/arithmetic, inst+110, unloaded
  82. // CHECK:STDOUT: %import_ref.10 = import_ref Core//prelude/operators/arithmetic, inst+134, unloaded
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: file {
  86. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  87. // CHECK:STDOUT: .Core = imports.%Core
  88. // CHECK:STDOUT: .C = %C.decl
  89. // CHECK:STDOUT: .TestOp = %TestOp.decl
  90. // CHECK:STDOUT: .TestAssign = %TestAssign.decl
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: %Core.import = import Core
  93. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {}
  94. // CHECK:STDOUT: impl_decl @impl.1 {
  95. // CHECK:STDOUT: %C.ref.loc17: type = name_ref C, %C.decl [template = constants.%C]
  96. // CHECK:STDOUT: %Core.ref.loc17: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  97. // CHECK:STDOUT: %Sub.ref: type = name_ref Sub, imports.%import_ref.1 [template = constants.%.2]
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: impl_decl @impl.2 {
  100. // CHECK:STDOUT: %C.ref.loc22: type = name_ref C, %C.decl [template = constants.%C]
  101. // CHECK:STDOUT: %Core.ref.loc22: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  102. // CHECK:STDOUT: %SubAssign.ref: type = name_ref SubAssign, imports.%import_ref.5 [template = constants.%.6]
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %TestOp.decl: %TestOp.type = fn_decl @TestOp [template = constants.%TestOp] {
  105. // CHECK:STDOUT: %C.ref.loc26_14: type = name_ref C, %C.decl [template = constants.%C]
  106. // CHECK:STDOUT: %a.loc26_11.1: %C = param a
  107. // CHECK:STDOUT: @TestOp.%a: %C = bind_name a, %a.loc26_11.1
  108. // CHECK:STDOUT: %C.ref.loc26_20: type = name_ref C, %C.decl [template = constants.%C]
  109. // CHECK:STDOUT: %b.loc26_17.1: %C = param b
  110. // CHECK:STDOUT: @TestOp.%b: %C = bind_name b, %b.loc26_17.1
  111. // CHECK:STDOUT: %C.ref.loc26_26: type = name_ref C, %C.decl [template = constants.%C]
  112. // CHECK:STDOUT: @TestOp.%return: ref %C = var <return slot>
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT: %TestAssign.decl: %TestAssign.type = fn_decl @TestAssign [template = constants.%TestAssign] {
  115. // CHECK:STDOUT: %C.ref.loc30_18: type = name_ref C, %C.decl [template = constants.%C]
  116. // CHECK:STDOUT: %.loc30: type = ptr_type %C [template = constants.%.7]
  117. // CHECK:STDOUT: %a.loc30_15.1: %.7 = param a
  118. // CHECK:STDOUT: @TestAssign.%a: %.7 = bind_name a, %a.loc30_15.1
  119. // CHECK:STDOUT: %C.ref.loc30_25: type = name_ref C, %C.decl [template = constants.%C]
  120. // CHECK:STDOUT: %b.loc30_22.1: %C = param b
  121. // CHECK:STDOUT: @TestAssign.%b: %C = bind_name b, %b.loc30_22.1
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: interface @Sub {
  126. // CHECK:STDOUT: !members:
  127. // CHECK:STDOUT: .Self = imports.%import_ref.2
  128. // CHECK:STDOUT: .Op = imports.%import_ref.3
  129. // CHECK:STDOUT: witness = (imports.%import_ref.4)
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: interface @SubAssign {
  133. // CHECK:STDOUT: !members:
  134. // CHECK:STDOUT: .Self = imports.%import_ref.6
  135. // CHECK:STDOUT: .Op = imports.%import_ref.7
  136. // CHECK:STDOUT: witness = (imports.%import_ref.8)
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: impl @impl.1: %C as %.2 {
  140. // CHECK:STDOUT: %Op.decl: %Op.type.1 = fn_decl @Op.1 [template = constants.%Op.1] {
  141. // CHECK:STDOUT: %C.ref.loc18_15: type = name_ref C, file.%C.decl [template = constants.%C]
  142. // CHECK:STDOUT: %self.loc18_9.1: %C = param self
  143. // CHECK:STDOUT: %self.loc18_9.2: %C = bind_name self, %self.loc18_9.1
  144. // CHECK:STDOUT: %C.ref.loc18_25: type = name_ref C, file.%C.decl [template = constants.%C]
  145. // CHECK:STDOUT: %other.loc18_18.1: %C = param other
  146. // CHECK:STDOUT: %other.loc18_18.2: %C = bind_name other, %other.loc18_18.1
  147. // CHECK:STDOUT: %C.ref.loc18_31: type = name_ref C, file.%C.decl [template = constants.%C]
  148. // CHECK:STDOUT: %return.var: ref %C = var <return slot>
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT: %.1: <witness> = interface_witness (%Op.decl) [template = constants.%.4]
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: !members:
  153. // CHECK:STDOUT: .Op = %Op.decl
  154. // CHECK:STDOUT: witness = %.1
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: impl @impl.2: %C as %.6 {
  158. // CHECK:STDOUT: %Op.decl: %Op.type.3 = fn_decl @Op.3 [template = constants.%Op.3] {
  159. // CHECK:STDOUT: %C.ref.loc23_20: type = name_ref C, file.%C.decl [template = constants.%C]
  160. // CHECK:STDOUT: %.loc23_21: type = ptr_type %C [template = constants.%.7]
  161. // CHECK:STDOUT: %self.loc23_14.1: %.7 = param self
  162. // CHECK:STDOUT: %self.loc23_14.3: %.7 = bind_name self, %self.loc23_14.1
  163. // CHECK:STDOUT: %.loc23_9: %.7 = addr_pattern %self.loc23_14.3
  164. // CHECK:STDOUT: %C.ref.loc23_31: type = name_ref C, file.%C.decl [template = constants.%C]
  165. // CHECK:STDOUT: %other.loc23_24.1: %C = param other
  166. // CHECK:STDOUT: %other.loc23_24.2: %C = bind_name other, %other.loc23_24.1
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT: %.1: <witness> = interface_witness (%Op.decl) [template = constants.%.9]
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: !members:
  171. // CHECK:STDOUT: .Op = %Op.decl
  172. // CHECK:STDOUT: witness = %.1
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT:
  175. // CHECK:STDOUT: class @C {
  176. // CHECK:STDOUT: !members:
  177. // CHECK:STDOUT: .Self = constants.%C
  178. // CHECK:STDOUT: }
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: fn @Op.1[@impl.1.%self.loc18_9.2: %C](@impl.1.%other.loc18_18.2: %C) -> @impl.1.%return.var: %C {
  181. // CHECK:STDOUT: !entry:
  182. // CHECK:STDOUT: %.loc19_13.1: %.1 = struct_literal ()
  183. // CHECK:STDOUT: %.loc19_13.2: init %C = class_init (), @impl.1.%return.var [template = constants.%struct]
  184. // CHECK:STDOUT: %.loc19_14: init %C = converted %.loc19_13.1, %.loc19_13.2 [template = constants.%struct]
  185. // CHECK:STDOUT: return %.loc19_14 to @impl.1.%return.var
  186. // CHECK:STDOUT: }
  187. // CHECK:STDOUT:
  188. // CHECK:STDOUT: generic fn @Op.2(constants.%Self.1: %.2) {
  189. // CHECK:STDOUT: %Self: %.2 = bind_symbolic_name Self 0 [symbolic = %Self (constants.%Self.1)]
  190. // CHECK:STDOUT:
  191. // CHECK:STDOUT: fn[%self: @Op.2.%Self (%Self.1)](%other: @Op.2.%Self (%Self.1)) -> @Op.2.%Self (%Self.1);
  192. // CHECK:STDOUT: }
  193. // CHECK:STDOUT:
  194. // CHECK:STDOUT: fn @Op.3[addr @impl.2.%self.loc23_14.3: %.7](@impl.2.%other.loc23_24.2: %C) {
  195. // CHECK:STDOUT: !entry:
  196. // CHECK:STDOUT: return
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: generic fn @Op.4(constants.%Self.2: %.6) {
  200. // CHECK:STDOUT: %Self: %.6 = bind_symbolic_name Self 0 [symbolic = %Self (constants.%Self.2)]
  201. // CHECK:STDOUT: %.2: type = ptr_type @Op.4.%Self (%Self.2) [symbolic = %.2 (constants.%.8)]
  202. // CHECK:STDOUT:
  203. // CHECK:STDOUT: fn[addr %self: @Op.4.%.2 (%.8)](%other: @Op.4.%Self (%Self.2));
  204. // CHECK:STDOUT: }
  205. // CHECK:STDOUT:
  206. // CHECK:STDOUT: fn @TestOp(%a: %C, %b: %C) -> %return: %C {
  207. // CHECK:STDOUT: !entry:
  208. // CHECK:STDOUT: %a.ref: %C = name_ref a, %a
  209. // CHECK:STDOUT: %b.ref: %C = name_ref b, %b
  210. // CHECK:STDOUT: %.1: %Op.type.2 = interface_witness_access @impl.1.%.1, element0 [template = constants.%Op.1]
  211. // CHECK:STDOUT: %.loc27: <bound method> = bound_method %a.ref, %.1
  212. // CHECK:STDOUT: %.loc26: ref %C = splice_block %return {}
  213. // CHECK:STDOUT: %Op.call: init %C = call %.loc27(%a.ref, %b.ref) to %.loc26
  214. // CHECK:STDOUT: return %Op.call to %return
  215. // CHECK:STDOUT: }
  216. // CHECK:STDOUT:
  217. // CHECK:STDOUT: fn @TestAssign(%a: %.7, %b: %C) {
  218. // CHECK:STDOUT: !entry:
  219. // CHECK:STDOUT: %a.ref: %.7 = name_ref a, %a
  220. // CHECK:STDOUT: %.loc31_3.1: ref %C = deref %a.ref
  221. // CHECK:STDOUT: %b.ref: %C = name_ref b, %b
  222. // CHECK:STDOUT: %.1: %Op.type.4 = interface_witness_access @impl.2.%.1, element0 [template = constants.%Op.3]
  223. // CHECK:STDOUT: %.loc31_6: <bound method> = bound_method %.loc31_3.1, %.1
  224. // CHECK:STDOUT: %.loc31_3.2: %.7 = addr_of %.loc31_3.1
  225. // CHECK:STDOUT: %Op.call: init %.3 = call %.loc31_6(%.loc31_3.2, %b.ref)
  226. // CHECK:STDOUT: return
  227. // CHECK:STDOUT: }
  228. // CHECK:STDOUT:
  229. // CHECK:STDOUT: specific @Op.2(constants.%Self.1) {
  230. // CHECK:STDOUT: %Self => constants.%Self.1
  231. // CHECK:STDOUT: }
  232. // CHECK:STDOUT:
  233. // CHECK:STDOUT: specific @Op.2(constants.%C) {
  234. // CHECK:STDOUT: %Self => constants.%C
  235. // CHECK:STDOUT: }
  236. // CHECK:STDOUT:
  237. // CHECK:STDOUT: specific @Op.4(constants.%Self.2) {
  238. // CHECK:STDOUT: %Self => constants.%Self.2
  239. // CHECK:STDOUT: %.2 => constants.%.8
  240. // CHECK:STDOUT: }
  241. // CHECK:STDOUT:
  242. // CHECK:STDOUT: specific @Op.4(constants.%C) {
  243. // CHECK:STDOUT: %Self => constants.%C
  244. // CHECK:STDOUT: %.2 => constants.%.7
  245. // CHECK:STDOUT: }
  246. // CHECK:STDOUT: