method.carbon 9.0 KB

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