mod.carbon 15 KB

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