base_method.carbon 7.2 KB

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