base_method.carbon 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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: Base*]() {
  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: type = ptr_type %.4 [template]
  36. // CHECK:STDOUT: %.6: i32 = int_literal 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: type = ptr_type %Derived [template]
  41. // CHECK:STDOUT: %Call.type: type = fn_type @Call [template]
  42. // CHECK:STDOUT: %Call: %Call.type = struct_value () [template]
  43. // CHECK:STDOUT: %.10: type = struct_type {.base: %.5} [template]
  44. // CHECK:STDOUT: %.11: type = ptr_type %.8 [template]
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT:
  47. // CHECK:STDOUT: file {
  48. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  49. // CHECK:STDOUT: .Core = %Core
  50. // CHECK:STDOUT: .Base = %Base.decl
  51. // CHECK:STDOUT: .Derived = %Derived.decl
  52. // CHECK:STDOUT: .Call = %Call.decl
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT: %Core.import = import Core
  55. // CHECK:STDOUT: %Core: <namespace> = namespace %Core.import, [template] {}
  56. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {}
  57. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref ir3, inst+4, loaded [template = constants.%Int32]
  58. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  59. // CHECK:STDOUT: %Base.ref: type = name_ref Base, %Base.decl [template = constants.%Base]
  60. // CHECK:STDOUT: %.loc17_26: type = ptr_type %Base [template = constants.%.3]
  61. // CHECK:STDOUT: %self.loc17_16.1: %.3 = param self
  62. // CHECK:STDOUT: @F.%self: %.3 = bind_name self, %self.loc17_16.1
  63. // CHECK:STDOUT: @F.%.loc17: %.3 = addr_pattern @F.%self
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {}
  66. // CHECK:STDOUT: %Call.decl: %Call.type = fn_decl @Call [template = constants.%Call] {
  67. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, %Derived.decl [template = constants.%Derived]
  68. // CHECK:STDOUT: %.loc25: type = ptr_type %Derived [template = constants.%.9]
  69. // CHECK:STDOUT: %p.loc25_9.1: %.9 = param p
  70. // CHECK:STDOUT: @Call.%p: %.9 = bind_name p, %p.loc25_9.1
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: class @Base {
  75. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  76. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  77. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32, %.loc12_10.1 [template = i32]
  78. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl a, element0 [template]
  79. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  80. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Base [template = constants.%Base]
  81. // CHECK:STDOUT: %.loc14_23: type = ptr_type %Base [template = constants.%.3]
  82. // CHECK:STDOUT: %self.loc14_13.1: %.3 = param self
  83. // CHECK:STDOUT: %self.loc14_13.3: %.3 = bind_name self, %self.loc14_13.1
  84. // CHECK:STDOUT: %.loc14_8: %.3 = addr_pattern %self.loc14_13.3
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: !members:
  88. // CHECK:STDOUT: .Self = constants.%Base
  89. // CHECK:STDOUT: .a = %.loc12_8
  90. // CHECK:STDOUT: .F = %F.decl
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: class @Derived {
  94. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  95. // CHECK:STDOUT: %.loc22: %.7 = base_decl %Base, element0 [template]
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: !members:
  98. // CHECK:STDOUT: .Self = constants.%Derived
  99. // CHECK:STDOUT: .base = %.loc22
  100. // CHECK:STDOUT: extend name_scope2
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: fn @F[addr %self: %.3]() {
  106. // CHECK:STDOUT: !entry:
  107. // CHECK:STDOUT: %self.ref: %.3 = name_ref self, %self
  108. // CHECK:STDOUT: %.loc18_4: ref %Base = deref %self.ref
  109. // CHECK:STDOUT: %a.ref: %.2 = name_ref a, @Base.%.loc12_8 [template = @Base.%.loc12_8]
  110. // CHECK:STDOUT: %.loc18_10: ref i32 = class_element_access %.loc18_4, element0
  111. // CHECK:STDOUT: %.loc18_15: i32 = int_literal 1 [template = constants.%.6]
  112. // CHECK:STDOUT: assign %.loc18_10, %.loc18_15
  113. // CHECK:STDOUT: return
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: fn @Call(%p: %.9) {
  117. // CHECK:STDOUT: !entry:
  118. // CHECK:STDOUT: %p.ref: %.9 = name_ref p, %p
  119. // CHECK:STDOUT: %.loc26_4.1: ref %Derived = deref %p.ref
  120. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, @Base.%F.decl [template = constants.%F]
  121. // CHECK:STDOUT: %.loc26_7: <bound method> = bound_method %.loc26_4.1, %F.ref
  122. // CHECK:STDOUT: %.loc26_4.2: %.9 = addr_of %.loc26_4.1
  123. // CHECK:STDOUT: %.loc26_9.1: ref %Derived = deref %.loc26_4.2
  124. // CHECK:STDOUT: %.loc26_9.2: ref %Base = class_element_access %.loc26_9.1, element0
  125. // CHECK:STDOUT: %.loc26_9.3: %.3 = addr_of %.loc26_9.2
  126. // CHECK:STDOUT: %.loc26_9.4: %.3 = converted %.loc26_4.2, %.loc26_9.3
  127. // CHECK:STDOUT: %F.call: init %.1 = call %.loc26_7(%.loc26_9.4)
  128. // CHECK:STDOUT: return
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT: