base_method.carbon 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/class/base_method.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/base_method.carbon
  10. base class Base {
  11. var a: i32;
  12. fn F[addr self: Self*]();
  13. }
  14. fn Base.F[addr self: Self*]() {
  15. (*self).a = 1;
  16. }
  17. class Derived {
  18. extend base: Base;
  19. }
  20. fn Call(p: Derived*) {
  21. (*p).F();
  22. }
  23. // CHECK:STDOUT: --- base_method.carbon
  24. // CHECK:STDOUT:
  25. // CHECK:STDOUT: constants {
  26. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  27. // CHECK:STDOUT: %.1: Core.IntLiteral = int_value 32 [template]
  28. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  29. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  30. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  31. // CHECK:STDOUT: %i32: type = int_type signed, %.1 [template]
  32. // CHECK:STDOUT: %.2: type = unbound_element_type %Base, %i32 [template]
  33. // CHECK:STDOUT: %.3: type = ptr_type %Base [template]
  34. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  35. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  36. // CHECK:STDOUT: %.4: type = struct_type {.a: %i32} [template]
  37. // CHECK:STDOUT: %.5: <witness> = complete_type_witness %.4 [template]
  38. // CHECK:STDOUT: %.7: Core.IntLiteral = int_value 1 [template]
  39. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  40. // CHECK:STDOUT: %Convert.type.14: type = fn_type @Convert.2, @impl.1(%.1) [template]
  41. // CHECK:STDOUT: %Convert.14: %Convert.type.14 = struct_value () [template]
  42. // CHECK:STDOUT: %.31: <witness> = interface_witness (%Convert.14) [template]
  43. // CHECK:STDOUT: %.32: <bound method> = bound_method %.7, %Convert.14 [template]
  44. // CHECK:STDOUT: %.33: <specific function> = specific_function %.32, @Convert.2(%.1) [template]
  45. // CHECK:STDOUT: %.34: %i32 = int_value 1 [template]
  46. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  47. // CHECK:STDOUT: %.35: type = unbound_element_type %Derived, %Base [template]
  48. // CHECK:STDOUT: %.36: type = struct_type {.base: %Base} [template]
  49. // CHECK:STDOUT: %.37: <witness> = complete_type_witness %.36 [template]
  50. // CHECK:STDOUT: %.38: type = ptr_type %Derived [template]
  51. // CHECK:STDOUT: %Call.type: type = fn_type @Call [template]
  52. // CHECK:STDOUT: %Call: %Call.type = struct_value () [template]
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT:
  55. // CHECK:STDOUT: imports {
  56. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  57. // CHECK:STDOUT: .Int = %import_ref.1
  58. // CHECK:STDOUT: .ImplicitAs = %import_ref.2
  59. // CHECK:STDOUT: import Core//prelude
  60. // CHECK:STDOUT: import Core//prelude/...
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: file {
  65. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  66. // CHECK:STDOUT: .Core = imports.%Core
  67. // CHECK:STDOUT: .Base = %Base.decl
  68. // CHECK:STDOUT: .Derived = %Derived.decl
  69. // CHECK:STDOUT: .Call = %Call.decl
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: %Core.import = import Core
  72. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {} {}
  73. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  74. // CHECK:STDOUT: %self.patt: %.3 = binding_pattern self
  75. // CHECK:STDOUT: %self.param_patt: %.3 = value_param_pattern %self.patt, runtime_param0
  76. // CHECK:STDOUT: %.loc17_11: auto = addr_pattern %self.param_patt
  77. // CHECK:STDOUT: } {
  78. // CHECK:STDOUT: %Self.ref.loc17: type = name_ref Self, constants.%Base [template = constants.%Base]
  79. // CHECK:STDOUT: %.loc17_26: type = ptr_type %Base [template = constants.%.3]
  80. // CHECK:STDOUT: %self.param.loc17: %.3 = value_param runtime_param0
  81. // CHECK:STDOUT: %self.loc17: %.3 = bind_name self, %self.param.loc17
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {} {}
  84. // CHECK:STDOUT: %Call.decl: %Call.type = fn_decl @Call [template = constants.%Call] {
  85. // CHECK:STDOUT: %p.patt: %.38 = binding_pattern p
  86. // CHECK:STDOUT: %p.param_patt: %.38 = value_param_pattern %p.patt, runtime_param0
  87. // CHECK:STDOUT: } {
  88. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl [template = constants.%Derived]
  89. // CHECK:STDOUT: %.loc25: type = ptr_type %Derived [template = constants.%.38]
  90. // CHECK:STDOUT: %p.param: %.38 = value_param runtime_param0
  91. // CHECK:STDOUT: %p: %.38 = bind_name p, %p.param
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: class @Base {
  96. // CHECK:STDOUT: %.loc12_10.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  97. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc12_10.1) [template = constants.%i32]
  98. // CHECK:STDOUT: %.loc12_10.2: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  99. // CHECK:STDOUT: %.loc12_10.3: type = converted %int.make_type_signed, %.loc12_10.2 [template = constants.%i32]
  100. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl a, element0 [template]
  101. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  102. // CHECK:STDOUT: %self.patt: %.3 = binding_pattern self
  103. // CHECK:STDOUT: %self.param_patt: %.3 = value_param_pattern %self.patt, runtime_param0
  104. // CHECK:STDOUT: %.loc17_11: auto = addr_pattern %self.param_patt
  105. // CHECK:STDOUT: } {
  106. // CHECK:STDOUT: %Self.ref.loc14: type = name_ref Self, constants.%Base [template = constants.%Base]
  107. // CHECK:STDOUT: %.loc14: type = ptr_type %Base [template = constants.%.3]
  108. // CHECK:STDOUT: %self.param.loc14: %.3 = value_param runtime_param0
  109. // CHECK:STDOUT: %self.loc14: %.3 = bind_name self, %self.param.loc14
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT: %.loc15: <witness> = complete_type_witness %.4 [template = constants.%.5]
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: !members:
  114. // CHECK:STDOUT: .Self = constants.%Base
  115. // CHECK:STDOUT: .a = %.loc12_8
  116. // CHECK:STDOUT: .F = %F.decl
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: class @Derived {
  120. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  121. // CHECK:STDOUT: %.loc22: %.35 = base_decl %Base.ref, element0 [template]
  122. // CHECK:STDOUT: %.loc23: <witness> = complete_type_witness %.36 [template = constants.%.37]
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: !members:
  125. // CHECK:STDOUT: .Self = constants.%Derived
  126. // CHECK:STDOUT: .base = %.loc22
  127. // CHECK:STDOUT: extend %Base.ref
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: fn @F[addr %self.param_patt: %.3]() {
  131. // CHECK:STDOUT: !entry:
  132. // CHECK:STDOUT: %self.ref: %.3 = name_ref self, %self.loc17
  133. // CHECK:STDOUT: %.loc18_4: ref %Base = deref %self.ref
  134. // CHECK:STDOUT: %a.ref: %.2 = name_ref a, @Base.%.loc12_8 [template = @Base.%.loc12_8]
  135. // CHECK:STDOUT: %.loc18_10: ref %i32 = class_element_access %.loc18_4, element0
  136. // CHECK:STDOUT: %.loc18_15: Core.IntLiteral = int_value 1 [template = constants.%.7]
  137. // CHECK:STDOUT: %.loc18_13.1: %Convert.type.2 = interface_witness_access constants.%.31, element0 [template = constants.%Convert.14]
  138. // CHECK:STDOUT: %.loc18_13.2: <bound method> = bound_method %.loc18_15, %.loc18_13.1 [template = constants.%.32]
  139. // CHECK:STDOUT: %.loc18_13.3: <specific function> = specific_function %.loc18_13.2, @Convert.2(constants.%.1) [template = constants.%.33]
  140. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %.loc18_13.3(%.loc18_15) [template = constants.%.34]
  141. // CHECK:STDOUT: %.loc18_13.4: init %i32 = converted %.loc18_15, %int.convert_checked [template = constants.%.34]
  142. // CHECK:STDOUT: assign %.loc18_10, %.loc18_13.4
  143. // CHECK:STDOUT: return
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: fn @Call(%p.param_patt: %.38) {
  147. // CHECK:STDOUT: !entry:
  148. // CHECK:STDOUT: %p.ref: %.38 = name_ref p, %p
  149. // CHECK:STDOUT: %.loc26_4.1: ref %Derived = deref %p.ref
  150. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, @Base.%F.decl [template = constants.%F]
  151. // CHECK:STDOUT: %.loc26_7: <bound method> = bound_method %.loc26_4.1, %F.ref
  152. // CHECK:STDOUT: %.loc26_4.2: %.38 = addr_of %.loc26_4.1
  153. // CHECK:STDOUT: %.loc26_4.3: ref %Derived = deref %.loc26_4.2
  154. // CHECK:STDOUT: %.loc26_4.4: ref %Base = class_element_access %.loc26_4.3, element0
  155. // CHECK:STDOUT: %.loc26_4.5: %.3 = addr_of %.loc26_4.4
  156. // CHECK:STDOUT: %.loc26_4.6: %.3 = converted %.loc26_4.2, %.loc26_4.5
  157. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %.loc26_7(%.loc26_4.6)
  158. // CHECK:STDOUT: return
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT: