base_method.carbon 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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: %Int32.type: type = fn_type @Int32 [template]
  28. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  29. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  30. // CHECK:STDOUT: %.2: type = unbound_element_type %Base, i32 [template]
  31. // CHECK:STDOUT: %.3: type = ptr_type %Base [template]
  32. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  33. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  34. // CHECK:STDOUT: %.4: type = struct_type {.a: i32} [template]
  35. // CHECK:STDOUT: %.5: <witness> = complete_type_witness %.4 [template]
  36. // CHECK:STDOUT: %.6: type = ptr_type %.4 [template]
  37. // CHECK:STDOUT: %.7: i32 = int_value 1 [template]
  38. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  39. // CHECK:STDOUT: %.8: type = unbound_element_type %Derived, %Base [template]
  40. // CHECK:STDOUT: %.9: type = struct_type {.base: %Base} [template]
  41. // CHECK:STDOUT: %.10: <witness> = complete_type_witness %.9 [template]
  42. // CHECK:STDOUT: %.11: type = ptr_type %Derived [template]
  43. // CHECK:STDOUT: %Call.type: type = fn_type @Call [template]
  44. // CHECK:STDOUT: %Call: %Call.type = struct_value () [template]
  45. // CHECK:STDOUT: %.12: type = struct_type {.base: %.6} [template]
  46. // CHECK:STDOUT: %.13: type = ptr_type %.9 [template]
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT:
  49. // CHECK:STDOUT: imports {
  50. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  51. // CHECK:STDOUT: .Int32 = %import_ref
  52. // CHECK:STDOUT: import Core//prelude
  53. // CHECK:STDOUT: import Core//prelude/...
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+15, loaded [template = constants.%Int32]
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: file {
  59. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  60. // CHECK:STDOUT: .Core = imports.%Core
  61. // CHECK:STDOUT: .Base = %Base.decl
  62. // CHECK:STDOUT: .Derived = %Derived.decl
  63. // CHECK:STDOUT: .Call = %Call.decl
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT: %Core.import = import Core
  66. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {} {}
  67. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  68. // CHECK:STDOUT: %self.patt: %.3 = binding_pattern self
  69. // CHECK:STDOUT: %self.param_patt: %.3 = value_param_pattern %self.patt, runtime_param0
  70. // CHECK:STDOUT: %.loc17_11: auto = addr_pattern %self.param_patt
  71. // CHECK:STDOUT: } {
  72. // CHECK:STDOUT: %Self.ref.loc17: type = name_ref Self, constants.%Base [template = constants.%Base]
  73. // CHECK:STDOUT: %.loc17_26: type = ptr_type %Base [template = constants.%.3]
  74. // CHECK:STDOUT: %self.param.loc17: %.3 = value_param runtime_param0
  75. // CHECK:STDOUT: %self.loc17: %.3 = bind_name self, %self.param.loc17
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {} {}
  78. // CHECK:STDOUT: %Call.decl: %Call.type = fn_decl @Call [template = constants.%Call] {
  79. // CHECK:STDOUT: %p.patt: %.11 = binding_pattern p
  80. // CHECK:STDOUT: %p.param_patt: %.11 = value_param_pattern %p.patt, runtime_param0
  81. // CHECK:STDOUT: } {
  82. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl [template = constants.%Derived]
  83. // CHECK:STDOUT: %.loc25: type = ptr_type %Derived [template = constants.%.11]
  84. // CHECK:STDOUT: %p.param: %.11 = value_param runtime_param0
  85. // CHECK:STDOUT: %p: %.11 = bind_name p, %p.param
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: class @Base {
  90. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  91. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  92. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32, %.loc12_10.1 [template = i32]
  93. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl a, element0 [template]
  94. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  95. // CHECK:STDOUT: %self.patt: %.3 = binding_pattern self
  96. // CHECK:STDOUT: %self.param_patt: %.3 = value_param_pattern %self.patt, runtime_param0
  97. // CHECK:STDOUT: %.loc17_11: auto = addr_pattern %self.param_patt
  98. // CHECK:STDOUT: } {
  99. // CHECK:STDOUT: %Self.ref.loc14: type = name_ref Self, constants.%Base [template = constants.%Base]
  100. // CHECK:STDOUT: %.loc14: type = ptr_type %Base [template = constants.%.3]
  101. // CHECK:STDOUT: %self.param.loc14: %.3 = value_param runtime_param0
  102. // CHECK:STDOUT: %self.loc14: %.3 = bind_name self, %self.param.loc14
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %.loc15: <witness> = complete_type_witness %.4 [template = constants.%.5]
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: !members:
  107. // CHECK:STDOUT: .Self = constants.%Base
  108. // CHECK:STDOUT: .a = %.loc12_8
  109. // CHECK:STDOUT: .F = %F.decl
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: class @Derived {
  113. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  114. // CHECK:STDOUT: %.loc22: %.8 = base_decl %Base, element0 [template]
  115. // CHECK:STDOUT: %.loc23: <witness> = complete_type_witness %.9 [template = constants.%.10]
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: !members:
  118. // CHECK:STDOUT: .Self = constants.%Derived
  119. // CHECK:STDOUT: .base = %.loc22
  120. // CHECK:STDOUT: extend name_scope2
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: fn @F[addr %self.param_patt: %.3]() {
  126. // CHECK:STDOUT: !entry:
  127. // CHECK:STDOUT: %self.ref: %.3 = name_ref self, %self.loc17
  128. // CHECK:STDOUT: %.loc18_4: ref %Base = deref %self.ref
  129. // CHECK:STDOUT: %a.ref: %.2 = name_ref a, @Base.%.loc12_8 [template = @Base.%.loc12_8]
  130. // CHECK:STDOUT: %.loc18_10: ref i32 = class_element_access %.loc18_4, element0
  131. // CHECK:STDOUT: %.loc18_15: i32 = int_value 1 [template = constants.%.7]
  132. // CHECK:STDOUT: assign %.loc18_10, %.loc18_15
  133. // CHECK:STDOUT: return
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: fn @Call(%p.param_patt: %.11) {
  137. // CHECK:STDOUT: !entry:
  138. // CHECK:STDOUT: %p.ref: %.11 = name_ref p, %p
  139. // CHECK:STDOUT: %.loc26_4.1: ref %Derived = deref %p.ref
  140. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, @Base.%F.decl [template = constants.%F]
  141. // CHECK:STDOUT: %.loc26_7: <bound method> = bound_method %.loc26_4.1, %F.ref
  142. // CHECK:STDOUT: %.loc26_4.2: %.11 = addr_of %.loc26_4.1
  143. // CHECK:STDOUT: %.loc26_4.3: ref %Derived = deref %.loc26_4.2
  144. // CHECK:STDOUT: %.loc26_4.4: ref %Base = class_element_access %.loc26_4.3, element0
  145. // CHECK:STDOUT: %.loc26_4.5: %.3 = addr_of %.loc26_4.4
  146. // CHECK:STDOUT: %.loc26_4.6: %.3 = converted %.loc26_4.2, %.loc26_4.5
  147. // CHECK:STDOUT: %F.call: init %.1 = call %.loc26_7(%.loc26_4.6)
  148. // CHECK:STDOUT: return
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT: