call_from_operator.carbon 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // --- core.carbon
  7. package Core api;
  8. interface Add {
  9. fn Op[self: Self](other: Self) -> Self;
  10. }
  11. // --- user.carbon
  12. import Core;
  13. // TODO: This should be in `Core`, but currently impl lookup only looks in the
  14. // current file.
  15. impl i32 as Core.Add {
  16. fn Op[self: Self](other: Self) -> Self = "int.sadd";
  17. }
  18. var arr: [i32; 1 + 2] = (3, 4, 3 + 4);
  19. // CHECK:STDOUT: --- core.carbon
  20. // CHECK:STDOUT:
  21. // CHECK:STDOUT: constants {
  22. // CHECK:STDOUT: %.1: type = interface_type @Add [template]
  23. // CHECK:STDOUT: %Self: Add = bind_symbolic_name Self 0 [symbolic]
  24. // CHECK:STDOUT: %.2: type = assoc_entity_type @Add, <function> [template]
  25. // CHECK:STDOUT: %.3: <associated <function> in Add> = assoc_entity element0, @Add.%Op [template]
  26. // CHECK:STDOUT: }
  27. // CHECK:STDOUT:
  28. // CHECK:STDOUT: file {
  29. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  30. // CHECK:STDOUT: .Add = %Add.decl
  31. // CHECK:STDOUT: }
  32. // CHECK:STDOUT: %Add.decl: type = interface_decl @Add [template = constants.%.1] {}
  33. // CHECK:STDOUT: }
  34. // CHECK:STDOUT:
  35. // CHECK:STDOUT: interface @Add {
  36. // CHECK:STDOUT: %Self: Add = bind_symbolic_name Self 0 [symbolic = constants.%Self]
  37. // CHECK:STDOUT: %Op: <function> = fn_decl @Op [template] {
  38. // CHECK:STDOUT: %Self.ref.loc5_15: Add = name_ref Self, %Self [symbolic = constants.%Self]
  39. // CHECK:STDOUT: %.loc5_15.1: type = facet_type_access %Self.ref.loc5_15 [symbolic = constants.%Self]
  40. // CHECK:STDOUT: %.loc5_15.2: type = converted %Self.ref.loc5_15, %.loc5_15.1 [symbolic = constants.%Self]
  41. // CHECK:STDOUT: %self.loc5_9.1: Self = param self
  42. // CHECK:STDOUT: %self.loc5_9.2: Self = bind_name self, %self.loc5_9.1
  43. // CHECK:STDOUT: %Self.ref.loc5_28: Add = name_ref Self, %Self [symbolic = constants.%Self]
  44. // CHECK:STDOUT: %.loc5_28.1: type = facet_type_access %Self.ref.loc5_28 [symbolic = constants.%Self]
  45. // CHECK:STDOUT: %.loc5_28.2: type = converted %Self.ref.loc5_28, %.loc5_28.1 [symbolic = constants.%Self]
  46. // CHECK:STDOUT: %other.loc5_21.1: Self = param other
  47. // CHECK:STDOUT: %other.loc5_21.2: Self = bind_name other, %other.loc5_21.1
  48. // CHECK:STDOUT: %Self.ref.loc5_37: Add = name_ref Self, %Self [symbolic = constants.%Self]
  49. // CHECK:STDOUT: %.loc5_37.1: type = facet_type_access %Self.ref.loc5_37 [symbolic = constants.%Self]
  50. // CHECK:STDOUT: %.loc5_37.2: type = converted %Self.ref.loc5_37, %.loc5_37.1 [symbolic = constants.%Self]
  51. // CHECK:STDOUT: %return.var: ref Self = var <return slot>
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: %.loc5_41: <associated <function> in Add> = assoc_entity element0, %Op [template = constants.%.3]
  54. // CHECK:STDOUT:
  55. // CHECK:STDOUT: !members:
  56. // CHECK:STDOUT: .Self = %Self
  57. // CHECK:STDOUT: .Op = %.loc5_41
  58. // CHECK:STDOUT: witness = (%Op)
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: fn @Op[@Add.%self.loc5_9.2: Self](@Add.%other.loc5_21.2: Self) -> Self;
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: --- user.carbon
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: constants {
  66. // CHECK:STDOUT: %.1: type = interface_type @Add [template]
  67. // CHECK:STDOUT: %Self: Add = bind_symbolic_name Self 0 [symbolic]
  68. // CHECK:STDOUT: %.2: <witness> = interface_witness (@impl.%Op) [template]
  69. // CHECK:STDOUT: %.3: i32 = int_literal 1 [template]
  70. // CHECK:STDOUT: %.4: i32 = int_literal 2 [template]
  71. // CHECK:STDOUT: %.5: type = assoc_entity_type @Add, <function> [template]
  72. // CHECK:STDOUT: %.6: <associated <function> in Add> = assoc_entity element0, file.%import_ref.6 [template]
  73. // CHECK:STDOUT: %.7: <bound method> = bound_method %.3, @impl.%Op [template]
  74. // CHECK:STDOUT: %.8: i32 = int_literal 3 [template]
  75. // CHECK:STDOUT: %.9: type = array_type %.8, i32 [template]
  76. // CHECK:STDOUT: %.10: type = ptr_type [i32; 3] [template]
  77. // CHECK:STDOUT: %.11: i32 = int_literal 4 [template]
  78. // CHECK:STDOUT: %.12: <bound method> = bound_method %.8, @impl.%Op [template]
  79. // CHECK:STDOUT: %.13: i32 = int_literal 7 [template]
  80. // CHECK:STDOUT: %.14: type = tuple_type (i32, i32, i32) [template]
  81. // CHECK:STDOUT: %.15: i32 = int_literal 0 [template]
  82. // CHECK:STDOUT: %array: [i32; 3] = tuple_value (%.8, %.11, %.13) [template]
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: file {
  86. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  87. // CHECK:STDOUT: .Core = %Core
  88. // CHECK:STDOUT: .arr = %arr
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  91. // CHECK:STDOUT: %import_ref.1: type = import_ref ir2, inst+1, loc_9 [template = constants.%.1]
  92. // CHECK:STDOUT: %import_ref.2: <associated <function> in Add> = import_ref ir2, inst+21, loc_36 [template = constants.%.6]
  93. // CHECK:STDOUT: %import_ref.3 = import_ref ir2, inst+3, unloaded
  94. // CHECK:STDOUT: %import_ref.4: <function> = import_ref ir2, inst+19, loc_10 [template = imports.%Op]
  95. // CHECK:STDOUT: impl_decl @impl {
  96. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, %Core [template = %Core]
  97. // CHECK:STDOUT: %Add.decl: type = interface_decl @Add [template = constants.%.1] {}
  98. // CHECK:STDOUT: %Add.ref: type = name_ref Add, %import_ref.1 [template = constants.%.1]
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT: %.loc10_16: i32 = int_literal 1 [template = constants.%.3]
  101. // CHECK:STDOUT: %.loc10_20: i32 = int_literal 2 [template = constants.%.4]
  102. // CHECK:STDOUT: %import_ref.5: type = import_ref ir2, inst+1, loc_36 [template = constants.%.1]
  103. // CHECK:STDOUT: %import_ref.6 = import_ref ir2, inst+19, unloaded
  104. // CHECK:STDOUT: %.1: <function> = interface_witness_access @impl.%.1, element0 [template = @impl.%Op]
  105. // CHECK:STDOUT: %.loc10_18: <bound method> = bound_method %.loc10_16, %.1 [template = constants.%.7]
  106. // CHECK:STDOUT: %int.sadd: init i32 = call %.loc10_18(%.loc10_16, %.loc10_20) [template = constants.%.8]
  107. // CHECK:STDOUT: %.loc10_21: type = array_type %int.sadd, i32 [template = constants.%.9]
  108. // CHECK:STDOUT: %arr.var: ref [i32; 3] = var arr
  109. // CHECK:STDOUT: %arr: ref [i32; 3] = bind_name arr, %arr.var
  110. // CHECK:STDOUT: %import_ref.7: type = import_ref ir2, inst+1, loc_47 [template = constants.%.1]
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: interface @Add {
  114. // CHECK:STDOUT: !members:
  115. // CHECK:STDOUT: .Op = file.%import_ref.2
  116. // CHECK:STDOUT: .Self = file.%import_ref.3
  117. // CHECK:STDOUT: witness = (file.%import_ref.4)
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: impl @impl: i32 as Add {
  121. // CHECK:STDOUT: %Op: <function> = fn_decl @Op.1 [template] {
  122. // CHECK:STDOUT: %Self.ref.loc7_15: type = name_ref Self, i32 [template = i32]
  123. // CHECK:STDOUT: %self.loc7_9.1: i32 = param self
  124. // CHECK:STDOUT: %self.loc7_9.2: i32 = bind_name self, %self.loc7_9.1
  125. // CHECK:STDOUT: %Self.ref.loc7_28: type = name_ref Self, i32 [template = i32]
  126. // CHECK:STDOUT: %other.loc7_21.1: i32 = param other
  127. // CHECK:STDOUT: %other.loc7_21.2: i32 = bind_name other, %other.loc7_21.1
  128. // CHECK:STDOUT: %Self.ref.loc7_37: type = name_ref Self, i32 [template = i32]
  129. // CHECK:STDOUT: %return.var: ref i32 = var <return slot>
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT: %.1: <witness> = interface_witness (%Op) [template = constants.%.2]
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: !members:
  134. // CHECK:STDOUT: .Op = %Op
  135. // CHECK:STDOUT: witness = %.1
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: fn @Op.1[@impl.%self.loc7_9.2: i32](@impl.%other.loc7_21.2: i32) -> i32 = "int.sadd";
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: fn @Op.2[%self: Self](%other: Self) -> Self;
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: fn @__global_init() {
  143. // CHECK:STDOUT: !entry:
  144. // CHECK:STDOUT: %.loc10_26: i32 = int_literal 3 [template = constants.%.8]
  145. // CHECK:STDOUT: %.loc10_29: i32 = int_literal 4 [template = constants.%.11]
  146. // CHECK:STDOUT: %.loc10_32: i32 = int_literal 3 [template = constants.%.8]
  147. // CHECK:STDOUT: %.loc10_36: i32 = int_literal 4 [template = constants.%.11]
  148. // CHECK:STDOUT: %.1: <function> = interface_witness_access @impl.%.1, element0 [template = @impl.%Op]
  149. // CHECK:STDOUT: %.loc10_34: <bound method> = bound_method %.loc10_32, %.1 [template = constants.%.12]
  150. // CHECK:STDOUT: %int.sadd: init i32 = call %.loc10_34(%.loc10_32, %.loc10_36) [template = constants.%.13]
  151. // CHECK:STDOUT: %.loc10_37.1: (i32, i32, i32) = tuple_literal (%.loc10_26, %.loc10_29, %int.sadd)
  152. // CHECK:STDOUT: %.loc10_37.2: i32 = int_literal 0 [template = constants.%.15]
  153. // CHECK:STDOUT: %.loc10_37.3: ref i32 = array_index file.%arr.var, %.loc10_37.2
  154. // CHECK:STDOUT: %.loc10_37.4: init i32 = initialize_from %.loc10_26 to %.loc10_37.3 [template = constants.%.8]
  155. // CHECK:STDOUT: %.loc10_37.5: i32 = int_literal 1 [template = constants.%.3]
  156. // CHECK:STDOUT: %.loc10_37.6: ref i32 = array_index file.%arr.var, %.loc10_37.5
  157. // CHECK:STDOUT: %.loc10_37.7: init i32 = initialize_from %.loc10_29 to %.loc10_37.6 [template = constants.%.11]
  158. // CHECK:STDOUT: %.loc10_37.8: i32 = int_literal 2 [template = constants.%.4]
  159. // CHECK:STDOUT: %.loc10_37.9: ref i32 = array_index file.%arr.var, %.loc10_37.8
  160. // CHECK:STDOUT: %.loc10_37.10: init i32 = initialize_from %int.sadd to %.loc10_37.9 [template = constants.%.13]
  161. // CHECK:STDOUT: %.loc10_37.11: init [i32; 3] = array_init (%.loc10_37.4, %.loc10_37.7, %.loc10_37.10) to file.%arr.var [template = constants.%array]
  162. // CHECK:STDOUT: %.loc10_38: init [i32; 3] = converted %.loc10_37.1, %.loc10_37.11 [template = constants.%array]
  163. // CHECK:STDOUT: assign file.%arr.var, %.loc10_38
  164. // CHECK:STDOUT: return
  165. // CHECK:STDOUT: }
  166. // CHECK:STDOUT: