instance_method.carbon 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/impl/lookup/instance_method.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/lookup/instance_method.carbon
  10. class C;
  11. interface I {
  12. fn F[self: Self]() -> i32;
  13. }
  14. class C {
  15. extend impl as I {
  16. fn F[self: Self]() -> i32;
  17. }
  18. }
  19. fn F(c: C) -> i32 {
  20. return c.F();
  21. }
  22. // CHECK:STDOUT: --- instance_method.carbon
  23. // CHECK:STDOUT:
  24. // CHECK:STDOUT: constants {
  25. // CHECK:STDOUT: %C: type = class_type @C [template]
  26. // CHECK:STDOUT: %.1: type = interface_type @I [template]
  27. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic]
  28. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  29. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  30. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  31. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  32. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  33. // CHECK:STDOUT: %.3: type = assoc_entity_type @I, %F.type.1 [template]
  34. // CHECK:STDOUT: %.4: %.3 = assoc_entity element0, @I.%F.decl [template]
  35. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  36. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  37. // CHECK:STDOUT: %.5: <witness> = interface_witness (%F.2) [template]
  38. // CHECK:STDOUT: %.6: type = struct_type {} [template]
  39. // CHECK:STDOUT: %F.type.3: type = fn_type @F.3 [template]
  40. // CHECK:STDOUT: %F.3: %F.type.3 = struct_value () [template]
  41. // CHECK:STDOUT: %.7: type = ptr_type %.6 [template]
  42. // CHECK:STDOUT: }
  43. // CHECK:STDOUT:
  44. // CHECK:STDOUT: file {
  45. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  46. // CHECK:STDOUT: .Core = %Core
  47. // CHECK:STDOUT: .C = %C.decl.loc11
  48. // CHECK:STDOUT: .I = %I.decl
  49. // CHECK:STDOUT: .F = %F.decl
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  52. // CHECK:STDOUT: %C.decl.loc11: type = class_decl @C [template = constants.%C] {}
  53. // CHECK:STDOUT: %I.decl: type = interface_decl @I [template = constants.%.1] {}
  54. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  55. // CHECK:STDOUT: %C.decl.loc17: type = class_decl @C [template = constants.%C] {}
  56. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  57. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  58. // CHECK:STDOUT: %F.decl: %F.type.3 = fn_decl @F.3 [template = constants.%F.3] {
  59. // CHECK:STDOUT: %C.ref: type = name_ref C, %C.decl.loc11 [template = constants.%C]
  60. // CHECK:STDOUT: %c.loc23_6.1: %C = param c
  61. // CHECK:STDOUT: @F.3.%c: %C = bind_name c, %c.loc23_6.1
  62. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  63. // CHECK:STDOUT: %.loc23_15.1: type = value_of_initializer %int.make_type_32 [template = i32]
  64. // CHECK:STDOUT: %.loc23_15.2: type = converted %int.make_type_32, %.loc23_15.1 [template = i32]
  65. // CHECK:STDOUT: @F.3.%return: ref i32 = var <return slot>
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: interface @I {
  70. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = constants.%Self]
  71. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  72. // CHECK:STDOUT: %Self.ref: %.1 = name_ref Self, %Self [symbolic = constants.%Self]
  73. // CHECK:STDOUT: %.loc14_14.1: type = facet_type_access %Self.ref [symbolic = constants.%Self]
  74. // CHECK:STDOUT: %.loc14_14.2: type = converted %Self.ref, %.loc14_14.1 [symbolic = constants.%Self]
  75. // CHECK:STDOUT: %self.loc14_8.1: %Self = param self
  76. // CHECK:STDOUT: %self.loc14_8.2: %Self = bind_name self, %self.loc14_8.1
  77. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  78. // CHECK:STDOUT: %.loc14_25.1: type = value_of_initializer %int.make_type_32 [template = i32]
  79. // CHECK:STDOUT: %.loc14_25.2: type = converted %int.make_type_32, %.loc14_25.1 [template = i32]
  80. // CHECK:STDOUT: %return.var: ref i32 = var <return slot>
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %.loc14_28: %.3 = assoc_entity element0, %F.decl [template = constants.%.4]
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: !members:
  85. // CHECK:STDOUT: .Self = %Self
  86. // CHECK:STDOUT: .F = %.loc14_28
  87. // CHECK:STDOUT: witness = (%F.decl)
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: impl @impl: %C as %.1 {
  91. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  92. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [template = constants.%C]
  93. // CHECK:STDOUT: %self.loc19_10.1: %C = param self
  94. // CHECK:STDOUT: %self.loc19_10.2: %C = bind_name self, %self.loc19_10.1
  95. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  96. // CHECK:STDOUT: %.loc19_27.1: type = value_of_initializer %int.make_type_32 [template = i32]
  97. // CHECK:STDOUT: %.loc19_27.2: type = converted %int.make_type_32, %.loc19_27.1 [template = i32]
  98. // CHECK:STDOUT: %return.var: ref i32 = var <return slot>
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT: %.1: <witness> = interface_witness (%F.decl) [template = constants.%.5]
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: !members:
  103. // CHECK:STDOUT: .F = %F.decl
  104. // CHECK:STDOUT: witness = %.1
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: class @C {
  108. // CHECK:STDOUT: impl_decl @impl {
  109. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [template = constants.%.1]
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: !members:
  113. // CHECK:STDOUT: .Self = constants.%C
  114. // CHECK:STDOUT: extend name_scope2
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: fn @F.1[@I.%self.loc14_8.2: %Self]() -> i32
  120. // CHECK:STDOUT: generic [@I.%Self: %.1];
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: fn @F.2[@impl.%self.loc19_10.2: %C]() -> i32;
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: fn @F.3(%c: %C) -> i32 {
  125. // CHECK:STDOUT: !entry:
  126. // CHECK:STDOUT: %c.ref: %C = name_ref c, %c
  127. // CHECK:STDOUT: %F.ref: %.3 = name_ref F, @I.%.loc14_28 [template = constants.%.4]
  128. // CHECK:STDOUT: %.1: %F.type.1 = interface_witness_access @impl.%.1, element0 [template = constants.%F.2]
  129. // CHECK:STDOUT: %.loc24_11: <bound method> = bound_method %c.ref, %.1
  130. // CHECK:STDOUT: %F.call: init i32 = call %.loc24_11(%c.ref)
  131. // CHECK:STDOUT: %.loc24_15.1: i32 = value_of_initializer %F.call
  132. // CHECK:STDOUT: %.loc24_15.2: i32 = converted %F.call, %.loc24_15.1
  133. // CHECK:STDOUT: return %.loc24_15.2
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT: