self_conversion.carbon 7.6 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. base class Base {
  7. var a: i32;
  8. }
  9. class Derived {
  10. extend base: Base;
  11. fn SelfBase[self: Base]() -> i32;
  12. fn AddrSelfBase[addr self: Base*]();
  13. }
  14. fn Derived.SelfBase[self: Base]() -> i32 {
  15. return self.a;
  16. }
  17. fn Derived.AddrSelfBase[addr self: Base*]() {
  18. (*self).a = 1;
  19. }
  20. fn Call(p: Derived*) -> i32 {
  21. (*p).AddrSelfBase();
  22. return (*p).SelfBase();
  23. }
  24. // CHECK:STDOUT: --- self_conversion.carbon
  25. // CHECK:STDOUT:
  26. // CHECK:STDOUT: constants {
  27. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  28. // CHECK:STDOUT: %.1: type = unbound_element_type Base, i32 [template]
  29. // CHECK:STDOUT: %.2: type = struct_type {.a: i32} [template]
  30. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  31. // CHECK:STDOUT: %.3: type = ptr_type {.a: i32} [template]
  32. // CHECK:STDOUT: %.4: type = unbound_element_type Derived, Base [template]
  33. // CHECK:STDOUT: %.5: type = ptr_type Base [template]
  34. // CHECK:STDOUT: %.6: type = struct_type {.base: Base} [template]
  35. // CHECK:STDOUT: %.7: i32 = int_literal 1 [template]
  36. // CHECK:STDOUT: %.8: type = ptr_type Derived [template]
  37. // CHECK:STDOUT: %.9: type = struct_type {.base: {.a: i32}*} [template]
  38. // CHECK:STDOUT: %.10: type = ptr_type {.base: Base} [template]
  39. // CHECK:STDOUT: %.11: type = tuple_type () [template]
  40. // CHECK:STDOUT: }
  41. // CHECK:STDOUT:
  42. // CHECK:STDOUT: file {
  43. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  44. // CHECK:STDOUT: .Base = %Base.decl
  45. // CHECK:STDOUT: .Derived = %Derived.decl
  46. // CHECK:STDOUT: .Call = %Call
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {}
  49. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {}
  50. // CHECK:STDOUT: %SelfBase: <function> = fn_decl @SelfBase [template] {
  51. // CHECK:STDOUT: %Base.ref.loc18: type = name_ref Base, %Base.decl [template = constants.%Base]
  52. // CHECK:STDOUT: %self.loc18_21.1: Base = param self
  53. // CHECK:STDOUT: @SelfBase.%self: Base = bind_name self, %self.loc18_21.1
  54. // CHECK:STDOUT: %return.var.loc18: ref i32 = var <return slot>
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT: %AddrSelfBase: <function> = fn_decl @AddrSelfBase [template] {
  57. // CHECK:STDOUT: %Base.ref.loc22: type = name_ref Base, %Base.decl [template = constants.%Base]
  58. // CHECK:STDOUT: %.loc22_40: type = ptr_type Base [template = constants.%.5]
  59. // CHECK:STDOUT: %self.loc22_30.1: Base* = param self
  60. // CHECK:STDOUT: @AddrSelfBase.%self: Base* = bind_name self, %self.loc22_30.1
  61. // CHECK:STDOUT: @AddrSelfBase.%.loc22: Base* = addr_pattern @AddrSelfBase.%self
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: %Call: <function> = fn_decl @Call [template] {
  64. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, %Derived.decl [template = constants.%Derived]
  65. // CHECK:STDOUT: %.loc26: type = ptr_type Derived [template = constants.%.8]
  66. // CHECK:STDOUT: %p.loc26_9.1: Derived* = param p
  67. // CHECK:STDOUT: @Call.%p: Derived* = bind_name p, %p.loc26_9.1
  68. // CHECK:STDOUT: %return.var.loc26: ref i32 = var <return slot>
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: class @Base {
  73. // CHECK:STDOUT: %.loc8: <unbound element of class Base> = field_decl a, element0 [template]
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: !members:
  76. // CHECK:STDOUT: .Self = constants.%Base
  77. // CHECK:STDOUT: .a = %.loc8
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: class @Derived {
  81. // CHECK:STDOUT: %Base.ref.loc12: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  82. // CHECK:STDOUT: %.loc12: <unbound element of class Derived> = base_decl Base, element0 [template]
  83. // CHECK:STDOUT: %SelfBase: <function> = fn_decl @SelfBase [template] {
  84. // CHECK:STDOUT: %Base.ref.loc14: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  85. // CHECK:STDOUT: %self.loc14_15.1: Base = param self
  86. // CHECK:STDOUT: %self.loc14_15.2: Base = bind_name self, %self.loc14_15.1
  87. // CHECK:STDOUT: %return.var: ref i32 = var <return slot>
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT: %AddrSelfBase: <function> = fn_decl @AddrSelfBase [template] {
  90. // CHECK:STDOUT: %Base.ref.loc15: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  91. // CHECK:STDOUT: %.loc15_34: type = ptr_type Base [template = constants.%.5]
  92. // CHECK:STDOUT: %self.loc15_24.1: Base* = param self
  93. // CHECK:STDOUT: %self.loc15_24.3: Base* = bind_name self, %self.loc15_24.1
  94. // CHECK:STDOUT: %.loc15_19: Base* = addr_pattern %self.loc15_24.3
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: !members:
  98. // CHECK:STDOUT: .Self = constants.%Derived
  99. // CHECK:STDOUT: .base = %.loc12
  100. // CHECK:STDOUT: .SelfBase = %SelfBase
  101. // CHECK:STDOUT: .AddrSelfBase = %AddrSelfBase
  102. // CHECK:STDOUT: extend name_scope1
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: fn @SelfBase[%self: Base]() -> i32 {
  106. // CHECK:STDOUT: !entry:
  107. // CHECK:STDOUT: %self.ref: Base = name_ref self, %self
  108. // CHECK:STDOUT: %a.ref: <unbound element of class Base> = name_ref a, @Base.%.loc8 [template = @Base.%.loc8]
  109. // CHECK:STDOUT: %.loc19_14.1: ref i32 = class_element_access %self.ref, element0
  110. // CHECK:STDOUT: %.loc19_14.2: i32 = bind_value %.loc19_14.1
  111. // CHECK:STDOUT: return %.loc19_14.2
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: fn @AddrSelfBase[addr %self: Base*]() {
  115. // CHECK:STDOUT: !entry:
  116. // CHECK:STDOUT: %self.ref: Base* = name_ref self, %self
  117. // CHECK:STDOUT: %.loc23_4: ref Base = deref %self.ref
  118. // CHECK:STDOUT: %a.ref: <unbound element of class Base> = name_ref a, @Base.%.loc8 [template = @Base.%.loc8]
  119. // CHECK:STDOUT: %.loc23_10: ref i32 = class_element_access %.loc23_4, element0
  120. // CHECK:STDOUT: %.loc23_15: i32 = int_literal 1 [template = constants.%.7]
  121. // CHECK:STDOUT: assign %.loc23_10, %.loc23_15
  122. // CHECK:STDOUT: return
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: fn @Call(%p: Derived*) -> i32 {
  126. // CHECK:STDOUT: !entry:
  127. // CHECK:STDOUT: %p.ref.loc27: Derived* = name_ref p, %p
  128. // CHECK:STDOUT: %.loc27_4.1: ref Derived = deref %p.ref.loc27
  129. // CHECK:STDOUT: %AddrSelfBase.ref: <function> = name_ref AddrSelfBase, @Derived.%AddrSelfBase [template = @Derived.%AddrSelfBase]
  130. // CHECK:STDOUT: %.loc27_7: <bound method> = bound_method %.loc27_4.1, %AddrSelfBase.ref
  131. // CHECK:STDOUT: %.loc27_4.2: Derived* = addr_of %.loc27_4.1
  132. // CHECK:STDOUT: %.loc27_20.1: ref Derived = deref %.loc27_4.2
  133. // CHECK:STDOUT: %.loc27_20.2: ref Base = class_element_access %.loc27_20.1, element0
  134. // CHECK:STDOUT: %.loc27_20.3: Base* = addr_of %.loc27_20.2
  135. // CHECK:STDOUT: %.loc27_4.3: Base* = converted %.loc27_4.2, %.loc27_20.3
  136. // CHECK:STDOUT: %.loc27_20.4: init () = call %.loc27_7(%.loc27_4.3)
  137. // CHECK:STDOUT: %p.ref.loc28: Derived* = name_ref p, %p
  138. // CHECK:STDOUT: %.loc28_11.1: ref Derived = deref %p.ref.loc28
  139. // CHECK:STDOUT: %SelfBase.ref: <function> = name_ref SelfBase, @Derived.%SelfBase [template = @Derived.%SelfBase]
  140. // CHECK:STDOUT: %.loc28_14: <bound method> = bound_method %.loc28_11.1, %SelfBase.ref
  141. // CHECK:STDOUT: %.loc28_23.1: ref Base = class_element_access %.loc28_11.1, element0
  142. // CHECK:STDOUT: %.loc28_11.2: ref Base = converted %.loc28_11.1, %.loc28_23.1
  143. // CHECK:STDOUT: %.loc28_11.3: Base = bind_value %.loc28_11.2
  144. // CHECK:STDOUT: %.loc28_23.2: init i32 = call %.loc28_14(%.loc28_11.3)
  145. // CHECK:STDOUT: %.loc28_25: i32 = value_of_initializer %.loc28_23.2
  146. // CHECK:STDOUT: %.loc28_23.3: i32 = converted %.loc28_23.2, %.loc28_25
  147. // CHECK:STDOUT: return %.loc28_23.3
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT: