raw_self.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/raw_self.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/raw_self.carbon
  10. class Class {
  11. fn F[addr self: Self*](r#self: i32);
  12. fn G[self: Self](r#self: i32) -> (i32, i32);
  13. var n: i32;
  14. }
  15. fn Class.F[addr self: Self*](r#self: i32) {
  16. (*self).n = r#self;
  17. }
  18. fn Class.G[self: Self](r#self: i32) -> (i32, i32) {
  19. return (self.n, r#self);
  20. }
  21. // CHECK:STDOUT: --- raw_self.carbon
  22. // CHECK:STDOUT:
  23. // CHECK:STDOUT: constants {
  24. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  25. // CHECK:STDOUT: %ptr.e71: type = ptr_type %Class [template]
  26. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  27. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  28. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  29. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  30. // CHECK:STDOUT: %tuple.type.24b: type = tuple_type (type, type) [template]
  31. // CHECK:STDOUT: %tuple.type.d07: type = tuple_type (%i32, %i32) [template]
  32. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  33. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  34. // CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class, %i32 [template]
  35. // CHECK:STDOUT: %struct_type.n: type = struct_type {.n: %i32} [template]
  36. // CHECK:STDOUT: %complete_type.54b: <witness> = complete_type_witness %struct_type.n [template]
  37. // CHECK:STDOUT: }
  38. // CHECK:STDOUT:
  39. // CHECK:STDOUT: imports {
  40. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  41. // CHECK:STDOUT: .Int = %Core.Int
  42. // CHECK:STDOUT: import Core//prelude
  43. // CHECK:STDOUT: import Core//prelude/...
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT:
  47. // CHECK:STDOUT: file {
  48. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  49. // CHECK:STDOUT: .Core = imports.%Core
  50. // CHECK:STDOUT: .Class = %Class.decl
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT: %Core.import = import Core
  53. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  54. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  55. // CHECK:STDOUT: %self.patt.loc17_17: %ptr.e71 = binding_pattern self
  56. // CHECK:STDOUT: %self.param_patt.loc17_21: %ptr.e71 = value_param_pattern %self.patt.loc17_17, runtime_param0
  57. // CHECK:STDOUT: %.loc17_12: auto = addr_pattern %self.param_patt.loc17_21
  58. // CHECK:STDOUT: %self.patt.loc17_30: %i32 = binding_pattern r#self
  59. // CHECK:STDOUT: %self.param_patt.loc17_36: %i32 = value_param_pattern %self.patt.loc17_30, runtime_param1
  60. // CHECK:STDOUT: } {
  61. // CHECK:STDOUT: %self.param.loc17_21: %ptr.e71 = value_param runtime_param0
  62. // CHECK:STDOUT: %.loc17_27: type = splice_block %ptr.loc17 [template = constants.%ptr.e71] {
  63. // CHECK:STDOUT: %Self.ref.loc17: type = name_ref Self, constants.%Class [template = constants.%Class]
  64. // CHECK:STDOUT: %ptr.loc17: type = ptr_type %Class [template = constants.%ptr.e71]
  65. // CHECK:STDOUT: }
  66. // CHECK:STDOUT: %self.loc17_17: %ptr.e71 = bind_name self, %self.param.loc17_21
  67. // CHECK:STDOUT: %self.param.loc17_36: %i32 = value_param runtime_param1
  68. // CHECK:STDOUT: %.loc17_38: type = splice_block %i32.loc17 [template = constants.%i32] {
  69. // CHECK:STDOUT: %int_32.loc17: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  70. // CHECK:STDOUT: %i32.loc17: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %self.loc17_30: %i32 = bind_name r#self, %self.param.loc17_36
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  75. // CHECK:STDOUT: %self.patt.loc21_12: %Class = binding_pattern self
  76. // CHECK:STDOUT: %self.param_patt.loc21_16: %Class = value_param_pattern %self.patt.loc21_12, runtime_param0
  77. // CHECK:STDOUT: %self.patt.loc21_24: %i32 = binding_pattern r#self
  78. // CHECK:STDOUT: %self.param_patt.loc21_30: %i32 = value_param_pattern %self.patt.loc21_24, runtime_param1
  79. // CHECK:STDOUT: %return.patt: %tuple.type.d07 = return_slot_pattern
  80. // CHECK:STDOUT: %return.param_patt: %tuple.type.d07 = out_param_pattern %return.patt, runtime_param2
  81. // CHECK:STDOUT: } {
  82. // CHECK:STDOUT: %int_32.loc21_41: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  83. // CHECK:STDOUT: %i32.loc21_41: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  84. // CHECK:STDOUT: %int_32.loc21_46: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  85. // CHECK:STDOUT: %i32.loc21_46: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  86. // CHECK:STDOUT: %.loc21_49.1: %tuple.type.24b = tuple_literal (%i32.loc21_41, %i32.loc21_46)
  87. // CHECK:STDOUT: %.loc21_49.2: type = converted %.loc21_49.1, constants.%tuple.type.d07 [template = constants.%tuple.type.d07]
  88. // CHECK:STDOUT: %self.param.loc21_16: %Class = value_param runtime_param0
  89. // CHECK:STDOUT: %Self.ref.loc21: type = name_ref Self, constants.%Class [template = constants.%Class]
  90. // CHECK:STDOUT: %self.loc21_12: %Class = bind_name self, %self.param.loc21_16
  91. // CHECK:STDOUT: %self.param.loc21_30: %i32 = value_param runtime_param1
  92. // CHECK:STDOUT: %.loc21_32: type = splice_block %i32.loc21_32 [template = constants.%i32] {
  93. // CHECK:STDOUT: %int_32.loc21_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  94. // CHECK:STDOUT: %i32.loc21_32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT: %self.loc21_24: %i32 = bind_name r#self, %self.param.loc21_30
  97. // CHECK:STDOUT: %return.param.loc21: ref %tuple.type.d07 = out_param runtime_param2
  98. // CHECK:STDOUT: %return.loc21: ref %tuple.type.d07 = return_slot %return.param.loc21
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: class @Class {
  103. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  104. // CHECK:STDOUT: %self.patt.loc17_17: %ptr.e71 = binding_pattern self
  105. // CHECK:STDOUT: %self.param_patt.loc17_21: %ptr.e71 = value_param_pattern %self.patt.loc17_17, runtime_param0
  106. // CHECK:STDOUT: %.loc17_12: auto = addr_pattern %self.param_patt.loc17_21
  107. // CHECK:STDOUT: %self.patt.loc17_30: %i32 = binding_pattern r#self
  108. // CHECK:STDOUT: %self.param_patt.loc17_36: %i32 = value_param_pattern %self.patt.loc17_30, runtime_param1
  109. // CHECK:STDOUT: } {
  110. // CHECK:STDOUT: %self.param.loc12_17: %ptr.e71 = value_param runtime_param0
  111. // CHECK:STDOUT: %.loc12_23: type = splice_block %ptr.loc12 [template = constants.%ptr.e71] {
  112. // CHECK:STDOUT: %Self.ref.loc12: type = name_ref Self, constants.%Class [template = constants.%Class]
  113. // CHECK:STDOUT: %ptr.loc12: type = ptr_type %Class [template = constants.%ptr.e71]
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: %self.loc12_13: %ptr.e71 = bind_name self, %self.param.loc12_17
  116. // CHECK:STDOUT: %self.param.loc12_32: %i32 = value_param runtime_param1
  117. // CHECK:STDOUT: %.loc12_34: type = splice_block %i32.loc12 [template = constants.%i32] {
  118. // CHECK:STDOUT: %int_32.loc12: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  119. // CHECK:STDOUT: %i32.loc12: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT: %self.loc12_26: %i32 = bind_name r#self, %self.param.loc12_32
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  124. // CHECK:STDOUT: %self.patt.loc21_12: %Class = binding_pattern self
  125. // CHECK:STDOUT: %self.param_patt.loc21_16: %Class = value_param_pattern %self.patt.loc21_12, runtime_param0
  126. // CHECK:STDOUT: %self.patt.loc21_24: %i32 = binding_pattern r#self
  127. // CHECK:STDOUT: %self.param_patt.loc21_30: %i32 = value_param_pattern %self.patt.loc21_24, runtime_param1
  128. // CHECK:STDOUT: %return.patt: %tuple.type.d07 = return_slot_pattern
  129. // CHECK:STDOUT: %return.param_patt: %tuple.type.d07 = out_param_pattern %return.patt, runtime_param2
  130. // CHECK:STDOUT: } {
  131. // CHECK:STDOUT: %int_32.loc13_37: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  132. // CHECK:STDOUT: %i32.loc13_37: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  133. // CHECK:STDOUT: %int_32.loc13_42: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  134. // CHECK:STDOUT: %i32.loc13_42: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  135. // CHECK:STDOUT: %.loc13_45.1: %tuple.type.24b = tuple_literal (%i32.loc13_37, %i32.loc13_42)
  136. // CHECK:STDOUT: %.loc13_45.2: type = converted %.loc13_45.1, constants.%tuple.type.d07 [template = constants.%tuple.type.d07]
  137. // CHECK:STDOUT: %self.param.loc13_12: %Class = value_param runtime_param0
  138. // CHECK:STDOUT: %Self.ref.loc13: type = name_ref Self, constants.%Class [template = constants.%Class]
  139. // CHECK:STDOUT: %self.loc13_8: %Class = bind_name self, %self.param.loc13_12
  140. // CHECK:STDOUT: %self.param.loc13_26: %i32 = value_param runtime_param1
  141. // CHECK:STDOUT: %.loc13_28: type = splice_block %i32.loc13_28 [template = constants.%i32] {
  142. // CHECK:STDOUT: %int_32.loc13_28: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  143. // CHECK:STDOUT: %i32.loc13_28: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT: %self.loc13_20: %i32 = bind_name r#self, %self.param.loc13_26
  146. // CHECK:STDOUT: %return.param.loc13: ref %tuple.type.d07 = out_param runtime_param2
  147. // CHECK:STDOUT: %return.loc13: ref %tuple.type.d07 = return_slot %return.param.loc13
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT: %.loc14_8: %Class.elem = field_decl n, element0 [template]
  150. // CHECK:STDOUT: name_binding_decl {
  151. // CHECK:STDOUT: %.loc14_3: %Class.elem = var_pattern %.loc14_8
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT: %.var: ref %Class.elem = var <none>
  154. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.n [template = constants.%complete_type.54b]
  155. // CHECK:STDOUT: complete_type_witness = %complete_type
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: !members:
  158. // CHECK:STDOUT: .Self = constants.%Class
  159. // CHECK:STDOUT: .F = %F.decl
  160. // CHECK:STDOUT: .G = %G.decl
  161. // CHECK:STDOUT: .n = %.loc14_8
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: fn @F[addr %self.param_patt.loc17_21: %ptr.e71](%self.param_patt.loc17_36: %i32) {
  165. // CHECK:STDOUT: !entry:
  166. // CHECK:STDOUT: %self.ref.loc18_5: %ptr.e71 = name_ref self, %self.loc17_17
  167. // CHECK:STDOUT: %.loc18_4: ref %Class = deref %self.ref.loc18_5
  168. // CHECK:STDOUT: %n.ref: %Class.elem = name_ref n, @Class.%.loc14_8 [template = @Class.%.loc14_8]
  169. // CHECK:STDOUT: %.loc18_10: ref %i32 = class_element_access %.loc18_4, element0
  170. // CHECK:STDOUT: %self.ref.loc18_15: %i32 = name_ref r#self, %self.loc17_30
  171. // CHECK:STDOUT: assign %.loc18_10, %self.ref.loc18_15
  172. // CHECK:STDOUT: return
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT:
  175. // CHECK:STDOUT: fn @G[%self.param_patt.loc21_16: %Class](%self.param_patt.loc21_30: %i32) -> %return.param_patt: %tuple.type.d07 {
  176. // CHECK:STDOUT: !entry:
  177. // CHECK:STDOUT: %self.ref.loc22_11: %Class = name_ref self, %self.loc21_12
  178. // CHECK:STDOUT: %n.ref: %Class.elem = name_ref n, @Class.%.loc14_8 [template = @Class.%.loc14_8]
  179. // CHECK:STDOUT: %.loc22_15.1: ref %i32 = class_element_access %self.ref.loc22_11, element0
  180. // CHECK:STDOUT: %.loc22_15.2: %i32 = bind_value %.loc22_15.1
  181. // CHECK:STDOUT: %self.ref.loc22_19: %i32 = name_ref r#self, %self.loc21_24
  182. // CHECK:STDOUT: %.loc22_25.1: %tuple.type.d07 = tuple_literal (%.loc22_15.2, %self.ref.loc22_19)
  183. // CHECK:STDOUT: %tuple.elem0: ref %i32 = tuple_access %return.loc21, element0
  184. // CHECK:STDOUT: %.loc22_25.2: init %i32 = initialize_from %.loc22_15.2 to %tuple.elem0
  185. // CHECK:STDOUT: %tuple.elem1: ref %i32 = tuple_access %return.loc21, element1
  186. // CHECK:STDOUT: %.loc22_25.3: init %i32 = initialize_from %self.ref.loc22_19 to %tuple.elem1
  187. // CHECK:STDOUT: %.loc22_25.4: init %tuple.type.d07 = tuple_init (%.loc22_25.2, %.loc22_25.3) to %return.loc21
  188. // CHECK:STDOUT: %.loc22_26: init %tuple.type.d07 = converted %.loc22_25.1, %.loc22_25.4
  189. // CHECK:STDOUT: return %.loc22_26 to %return.loc21
  190. // CHECK:STDOUT: }
  191. // CHECK:STDOUT: