method.carbon 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. class Class {
  7. fn F[self: Class]() -> i32;
  8. fn G[addr self: Class*]() -> i32;
  9. var k: i32;
  10. }
  11. fn Class.F[self: Class]() -> i32 {
  12. return self.k;
  13. }
  14. fn Call(c: Class) -> i32 {
  15. // TODO: The sem-ir for this call doesn't distinguish the `self` argument from
  16. // the explicit arguments.
  17. return c.F();
  18. }
  19. fn CallOnConstBoundMethod() -> i32 {
  20. return ({.k = 1} as Class).F();
  21. }
  22. fn CallWithAddr() -> i32 {
  23. var c: Class;
  24. return c.G();
  25. }
  26. fn CallFThroughPointer(p: Class*) -> i32 {
  27. return (*p).F();
  28. }
  29. fn CallGThroughPointer(p: Class*) -> i32 {
  30. return (*p).G();
  31. }
  32. fn Make() -> Class;
  33. fn CallFOnInitializingExpr() -> i32 {
  34. return Make().F();
  35. }
  36. fn CallGOnInitializingExpr() -> i32 {
  37. return Make().G();
  38. }
  39. // CHECK:STDOUT: --- method.carbon
  40. // CHECK:STDOUT:
  41. // CHECK:STDOUT: constants {
  42. // CHECK:STDOUT: %.loc12: type = struct_type {.k: i32}, const
  43. // CHECK:STDOUT: %.loc7: type = ptr_type {.k: i32}, const
  44. // CHECK:STDOUT: %.loc25: i32 = int_literal 1, const
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT:
  47. // CHECK:STDOUT: file {
  48. // CHECK:STDOUT: package: <namespace> = namespace package, {.Class = %Class.decl, .Call = %Call, .CallOnConstBoundMethod = %CallOnConstBoundMethod, .CallWithAddr = %CallWithAddr, .CallFThroughPointer = %CallFThroughPointer, .CallGThroughPointer = %CallGThroughPointer, .Make = %Make, .CallFOnInitializingExpr = %CallFOnInitializingExpr, .CallGOnInitializingExpr = %CallGOnInitializingExpr}
  49. // CHECK:STDOUT: %Class.decl = class_decl @Class, ()
  50. // CHECK:STDOUT: %Class: type = class_type @Class, const
  51. // CHECK:STDOUT: %F: <function> = fn_decl @F, const
  52. // CHECK:STDOUT: %Call: <function> = fn_decl @Call, const
  53. // CHECK:STDOUT: %CallOnConstBoundMethod: <function> = fn_decl @CallOnConstBoundMethod, const
  54. // CHECK:STDOUT: %CallWithAddr: <function> = fn_decl @CallWithAddr, const
  55. // CHECK:STDOUT: %CallFThroughPointer: <function> = fn_decl @CallFThroughPointer, const
  56. // CHECK:STDOUT: %CallGThroughPointer: <function> = fn_decl @CallGThroughPointer, const
  57. // CHECK:STDOUT: %Make: <function> = fn_decl @Make, const
  58. // CHECK:STDOUT: %CallFOnInitializingExpr: <function> = fn_decl @CallFOnInitializingExpr, const
  59. // CHECK:STDOUT: %CallGOnInitializingExpr: <function> = fn_decl @CallGOnInitializingExpr, const
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT:
  62. // CHECK:STDOUT: class @Class {
  63. // CHECK:STDOUT: %F: <function> = fn_decl @F, const
  64. // CHECK:STDOUT: %G: <function> = fn_decl @G, const
  65. // CHECK:STDOUT: %.loc11_8.1: type = unbound_element_type Class, i32, const
  66. // CHECK:STDOUT: %.loc11_8.2: <unbound element of class Class> = field_decl k, element0, const
  67. // CHECK:STDOUT: %k: <unbound element of class Class> = bind_name k, %.loc11_8.2, const = %.loc11_8.2
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: !members:
  70. // CHECK:STDOUT: .F = %F
  71. // CHECK:STDOUT: .G = %G
  72. // CHECK:STDOUT: .k = %k
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: fn @F[%self: Class]() -> i32 {
  76. // CHECK:STDOUT: !entry:
  77. // CHECK:STDOUT: %self.ref: Class = name_ref self, %self
  78. // CHECK:STDOUT: %.loc15_14.1: ref i32 = class_element_access %self.ref, element0
  79. // CHECK:STDOUT: %.loc15_14.2: i32 = bind_value %.loc15_14.1
  80. // CHECK:STDOUT: return %.loc15_14.2
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: fn @G[addr %self: Class*]() -> i32;
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: fn @Call(%c: Class) -> i32 {
  86. // CHECK:STDOUT: !entry:
  87. // CHECK:STDOUT: %c.ref: Class = name_ref c, %c
  88. // CHECK:STDOUT: %.loc21_11: <bound method> = bound_method %c.ref, @Class.%F
  89. // CHECK:STDOUT: %.loc21_13: init i32 = call %.loc21_11(%c.ref)
  90. // CHECK:STDOUT: %.loc21_15.1: i32 = value_of_initializer %.loc21_13
  91. // CHECK:STDOUT: %.loc21_15.2: i32 = converted %.loc21_13, %.loc21_15.1
  92. // CHECK:STDOUT: return %.loc21_15.2
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: fn @CallOnConstBoundMethod() -> i32 {
  96. // CHECK:STDOUT: !entry:
  97. // CHECK:STDOUT: %.loc25_17: i32 = int_literal 1, const = constants.%.loc25
  98. // CHECK:STDOUT: %.loc25_18.1: {.k: i32} = struct_literal (%.loc25_17)
  99. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class, const = file.%Class
  100. // CHECK:STDOUT: %.loc25_18.2: ref Class = temporary_storage
  101. // CHECK:STDOUT: %.loc25_18.3: ref i32 = class_element_access %.loc25_18.2, element0
  102. // CHECK:STDOUT: %.loc25_18.4: init i32 = initialize_from %.loc25_17 to %.loc25_18.3
  103. // CHECK:STDOUT: %.loc25_18.5: init Class = class_init (%.loc25_18.4), %.loc25_18.2
  104. // CHECK:STDOUT: %.loc25_18.6: ref Class = temporary %.loc25_18.2, %.loc25_18.5
  105. // CHECK:STDOUT: %.loc25_18.7: ref Class = converted %.loc25_18.1, %.loc25_18.6
  106. // CHECK:STDOUT: %.loc25_29: <bound method> = bound_method %.loc25_18.7, @Class.%F
  107. // CHECK:STDOUT: %.loc25_18.8: Class = bind_value %.loc25_18.7
  108. // CHECK:STDOUT: %.loc25_31: init i32 = call %.loc25_29(%.loc25_18.8)
  109. // CHECK:STDOUT: %.loc25_33.1: i32 = value_of_initializer %.loc25_31
  110. // CHECK:STDOUT: %.loc25_33.2: i32 = converted %.loc25_31, %.loc25_33.1
  111. // CHECK:STDOUT: return %.loc25_33.2
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: fn @CallWithAddr() -> i32 {
  115. // CHECK:STDOUT: !entry:
  116. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class, const = file.%Class
  117. // CHECK:STDOUT: %c.var: ref Class = var c
  118. // CHECK:STDOUT: %c: ref Class = bind_name c, %c.var
  119. // CHECK:STDOUT: %c.ref: ref Class = name_ref c, %c
  120. // CHECK:STDOUT: %.loc30_11: <bound method> = bound_method %c.ref, @Class.%G
  121. // CHECK:STDOUT: %.loc30_10: Class* = addr_of %c.ref
  122. // CHECK:STDOUT: %.loc30_13: init i32 = call %.loc30_11(%.loc30_10)
  123. // CHECK:STDOUT: %.loc30_15.1: i32 = value_of_initializer %.loc30_13
  124. // CHECK:STDOUT: %.loc30_15.2: i32 = converted %.loc30_13, %.loc30_15.1
  125. // CHECK:STDOUT: return %.loc30_15.2
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: fn @CallFThroughPointer(%p: Class*) -> i32 {
  129. // CHECK:STDOUT: !entry:
  130. // CHECK:STDOUT: %p.ref: Class* = name_ref p, %p
  131. // CHECK:STDOUT: %.loc34_11.1: ref Class = deref %p.ref
  132. // CHECK:STDOUT: %.loc34_14: <bound method> = bound_method %.loc34_11.1, @Class.%F
  133. // CHECK:STDOUT: %.loc34_11.2: Class = bind_value %.loc34_11.1
  134. // CHECK:STDOUT: %.loc34_16: init i32 = call %.loc34_14(%.loc34_11.2)
  135. // CHECK:STDOUT: %.loc34_18.1: i32 = value_of_initializer %.loc34_16
  136. // CHECK:STDOUT: %.loc34_18.2: i32 = converted %.loc34_16, %.loc34_18.1
  137. // CHECK:STDOUT: return %.loc34_18.2
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: fn @CallGThroughPointer(%p: Class*) -> i32 {
  141. // CHECK:STDOUT: !entry:
  142. // CHECK:STDOUT: %p.ref: Class* = name_ref p, %p
  143. // CHECK:STDOUT: %.loc38_11.1: ref Class = deref %p.ref
  144. // CHECK:STDOUT: %.loc38_14: <bound method> = bound_method %.loc38_11.1, @Class.%G
  145. // CHECK:STDOUT: %.loc38_11.2: Class* = addr_of %.loc38_11.1
  146. // CHECK:STDOUT: %.loc38_16: init i32 = call %.loc38_14(%.loc38_11.2)
  147. // CHECK:STDOUT: %.loc38_18.1: i32 = value_of_initializer %.loc38_16
  148. // CHECK:STDOUT: %.loc38_18.2: i32 = converted %.loc38_16, %.loc38_18.1
  149. // CHECK:STDOUT: return %.loc38_18.2
  150. // CHECK:STDOUT: }
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: fn @Make() -> %return: Class;
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: fn @CallFOnInitializingExpr() -> i32 {
  155. // CHECK:STDOUT: !entry:
  156. // CHECK:STDOUT: %Make.ref: <function> = name_ref Make, file.%Make, const = file.%Make
  157. // CHECK:STDOUT: %.loc44_14.1: ref Class = temporary_storage
  158. // CHECK:STDOUT: %.loc44_14.2: init Class = call %Make.ref() to %.loc44_14.1
  159. // CHECK:STDOUT: %.loc44_14.3: ref Class = temporary %.loc44_14.1, %.loc44_14.2
  160. // CHECK:STDOUT: %.loc44_16: <bound method> = bound_method %.loc44_14.3, @Class.%F
  161. // CHECK:STDOUT: %.loc44_14.4: Class = bind_value %.loc44_14.3
  162. // CHECK:STDOUT: %.loc44_18: init i32 = call %.loc44_16(%.loc44_14.4)
  163. // CHECK:STDOUT: %.loc44_20.1: i32 = value_of_initializer %.loc44_18
  164. // CHECK:STDOUT: %.loc44_20.2: i32 = converted %.loc44_18, %.loc44_20.1
  165. // CHECK:STDOUT: return %.loc44_20.2
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: fn @CallGOnInitializingExpr() -> i32 {
  169. // CHECK:STDOUT: !entry:
  170. // CHECK:STDOUT: %Make.ref: <function> = name_ref Make, file.%Make, const = file.%Make
  171. // CHECK:STDOUT: %.loc48_14.1: ref Class = temporary_storage
  172. // CHECK:STDOUT: %.loc48_14.2: init Class = call %Make.ref() to %.loc48_14.1
  173. // CHECK:STDOUT: %.loc48_14.3: ref Class = temporary %.loc48_14.1, %.loc48_14.2
  174. // CHECK:STDOUT: %.loc48_16: <bound method> = bound_method %.loc48_14.3, @Class.%G
  175. // CHECK:STDOUT: %.loc48_14.4: Class* = addr_of %.loc48_14.3
  176. // CHECK:STDOUT: %.loc48_18: init i32 = call %.loc48_16(%.loc48_14.4)
  177. // CHECK:STDOUT: %.loc48_20.1: i32 = value_of_initializer %.loc48_18
  178. // CHECK:STDOUT: %.loc48_20.2: i32 = converted %.loc48_18, %.loc48_20.1
  179. // CHECK:STDOUT: return %.loc48_20.2
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT: