basic.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/basic.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/basic.carbon
  10. class Class {
  11. fn F(n: i32) -> i32 {
  12. return n;
  13. }
  14. fn G(n: i32) -> i32;
  15. var k: i32;
  16. }
  17. fn Class.G(n: i32) -> i32 {
  18. return n;
  19. }
  20. fn Run() -> i32 {
  21. return Class.F(4);
  22. }
  23. // CHECK:STDOUT: --- basic.carbon
  24. // CHECK:STDOUT:
  25. // CHECK:STDOUT: constants {
  26. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  27. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  28. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  29. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  30. // CHECK:STDOUT: %i32: type = int_type signed, %int_32 [template]
  31. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  32. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  33. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  34. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  35. // CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class, %i32 [template]
  36. // CHECK:STDOUT: %struct_type.k: type = struct_type {.k: %i32} [template]
  37. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.k [template]
  38. // CHECK:STDOUT: %Run.type: type = fn_type @Run [template]
  39. // CHECK:STDOUT: %Run: %Run.type = struct_value () [template]
  40. // CHECK:STDOUT: %int_4.1: Core.IntLiteral = int_value 4 [template]
  41. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  42. // CHECK:STDOUT: %Convert.type.14: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  43. // CHECK:STDOUT: %Convert.14: %Convert.type.14 = struct_value () [template]
  44. // CHECK:STDOUT: %interface.9: <witness> = interface_witness (%Convert.14) [template]
  45. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_4.1, %Convert.14 [template]
  46. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(%int_32) [template]
  47. // CHECK:STDOUT: %int_4.2: %i32 = int_value 4 [template]
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: imports {
  51. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  52. // CHECK:STDOUT: .Int = %import_ref.1
  53. // CHECK:STDOUT: .ImplicitAs = %import_ref.2
  54. // CHECK:STDOUT: import Core//prelude
  55. // CHECK:STDOUT: import Core//prelude/...
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: file {
  60. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  61. // CHECK:STDOUT: .Core = imports.%Core
  62. // CHECK:STDOUT: .Class = %Class.decl
  63. // CHECK:STDOUT: .Run = %Run.decl
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT: %Core.import = import Core
  66. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  67. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  68. // CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
  69. // CHECK:STDOUT: %n.param_patt: %i32 = value_param_pattern %n.patt, runtime_param0
  70. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  71. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  72. // CHECK:STDOUT: } {
  73. // CHECK:STDOUT: %int_32.loc21_15: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  74. // CHECK:STDOUT: %int.make_type_signed.loc21_15: init type = call constants.%Int(%int_32.loc21_15) [template = constants.%i32]
  75. // CHECK:STDOUT: %.loc21_15.1: type = value_of_initializer %int.make_type_signed.loc21_15 [template = constants.%i32]
  76. // CHECK:STDOUT: %.loc21_15.2: type = converted %int.make_type_signed.loc21_15, %.loc21_15.1 [template = constants.%i32]
  77. // CHECK:STDOUT: %int_32.loc21_23: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  78. // CHECK:STDOUT: %int.make_type_signed.loc21_23: init type = call constants.%Int(%int_32.loc21_23) [template = constants.%i32]
  79. // CHECK:STDOUT: %.loc21_23.1: type = value_of_initializer %int.make_type_signed.loc21_23 [template = constants.%i32]
  80. // CHECK:STDOUT: %.loc21_23.2: type = converted %int.make_type_signed.loc21_23, %.loc21_23.1 [template = constants.%i32]
  81. // CHECK:STDOUT: %n.param.loc21: %i32 = value_param runtime_param0
  82. // CHECK:STDOUT: %n.loc21: %i32 = bind_name n, %n.param.loc21
  83. // CHECK:STDOUT: %return.param.loc21: ref %i32 = out_param runtime_param1
  84. // CHECK:STDOUT: %return.loc21: ref %i32 = return_slot %return.param.loc21
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [template = constants.%Run] {
  87. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  88. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param0
  89. // CHECK:STDOUT: } {
  90. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  91. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%int_32) [template = constants.%i32]
  92. // CHECK:STDOUT: %.loc25_13.1: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  93. // CHECK:STDOUT: %.loc25_13.2: type = converted %int.make_type_signed, %.loc25_13.1 [template = constants.%i32]
  94. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param0
  95. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  98. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%int_32) [template = constants.%i32]
  99. // CHECK:STDOUT: %.loc25_17.1: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  100. // CHECK:STDOUT: %.loc25_17.2: type = converted %int.make_type_signed, %.loc25_17.1 [template = constants.%i32]
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: class @Class {
  104. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  105. // CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
  106. // CHECK:STDOUT: %n.param_patt: %i32 = value_param_pattern %n.patt, runtime_param0
  107. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  108. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  109. // CHECK:STDOUT: } {
  110. // CHECK:STDOUT: %int_32.loc12_11: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  111. // CHECK:STDOUT: %int.make_type_signed.loc12_11: init type = call constants.%Int(%int_32.loc12_11) [template = constants.%i32]
  112. // CHECK:STDOUT: %.loc12_11.1: type = value_of_initializer %int.make_type_signed.loc12_11 [template = constants.%i32]
  113. // CHECK:STDOUT: %.loc12_11.2: type = converted %int.make_type_signed.loc12_11, %.loc12_11.1 [template = constants.%i32]
  114. // CHECK:STDOUT: %int_32.loc12_19: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  115. // CHECK:STDOUT: %int.make_type_signed.loc12_19: init type = call constants.%Int(%int_32.loc12_19) [template = constants.%i32]
  116. // CHECK:STDOUT: %.loc12_19.1: type = value_of_initializer %int.make_type_signed.loc12_19 [template = constants.%i32]
  117. // CHECK:STDOUT: %.loc12_19.2: type = converted %int.make_type_signed.loc12_19, %.loc12_19.1 [template = constants.%i32]
  118. // CHECK:STDOUT: %n.param: %i32 = value_param runtime_param0
  119. // CHECK:STDOUT: %n: %i32 = bind_name n, %n.param
  120. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  121. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  124. // CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
  125. // CHECK:STDOUT: %n.param_patt: %i32 = value_param_pattern %n.patt, runtime_param0
  126. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  127. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  128. // CHECK:STDOUT: } {
  129. // CHECK:STDOUT: %int_32.loc16_11: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  130. // CHECK:STDOUT: %int.make_type_signed.loc16_11: init type = call constants.%Int(%int_32.loc16_11) [template = constants.%i32]
  131. // CHECK:STDOUT: %.loc16_11.1: type = value_of_initializer %int.make_type_signed.loc16_11 [template = constants.%i32]
  132. // CHECK:STDOUT: %.loc16_11.2: type = converted %int.make_type_signed.loc16_11, %.loc16_11.1 [template = constants.%i32]
  133. // CHECK:STDOUT: %int_32.loc16_19: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  134. // CHECK:STDOUT: %int.make_type_signed.loc16_19: init type = call constants.%Int(%int_32.loc16_19) [template = constants.%i32]
  135. // CHECK:STDOUT: %.loc16_19.1: type = value_of_initializer %int.make_type_signed.loc16_19 [template = constants.%i32]
  136. // CHECK:STDOUT: %.loc16_19.2: type = converted %int.make_type_signed.loc16_19, %.loc16_19.1 [template = constants.%i32]
  137. // CHECK:STDOUT: %n.param.loc16: %i32 = value_param runtime_param0
  138. // CHECK:STDOUT: %n.loc16: %i32 = bind_name n, %n.param.loc16
  139. // CHECK:STDOUT: %return.param.loc16: ref %i32 = out_param runtime_param1
  140. // CHECK:STDOUT: %return.loc16: ref %i32 = return_slot %return.param.loc16
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  143. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%int_32) [template = constants.%i32]
  144. // CHECK:STDOUT: %.loc18_10.1: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  145. // CHECK:STDOUT: %.loc18_10.2: type = converted %int.make_type_signed, %.loc18_10.1 [template = constants.%i32]
  146. // CHECK:STDOUT: %.loc18_8: %Class.elem = field_decl k, element0 [template]
  147. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.k [template = constants.%complete_type]
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: !members:
  150. // CHECK:STDOUT: .Self = constants.%Class
  151. // CHECK:STDOUT: .F = %F.decl
  152. // CHECK:STDOUT: .G = %G.decl
  153. // CHECK:STDOUT: .k = %.loc18_8
  154. // CHECK:STDOUT: complete_type_witness = %complete_type
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: fn @F(%n.param_patt: %i32) -> %i32 {
  158. // CHECK:STDOUT: !entry:
  159. // CHECK:STDOUT: %n.ref: %i32 = name_ref n, %n
  160. // CHECK:STDOUT: return %n.ref
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: fn @G(%n.param_patt: %i32) -> %i32 {
  164. // CHECK:STDOUT: !entry:
  165. // CHECK:STDOUT: %n.ref: %i32 = name_ref n, %n.loc21
  166. // CHECK:STDOUT: return %n.ref
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: fn @Run() -> %i32 {
  170. // CHECK:STDOUT: !entry:
  171. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  172. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, @Class.%F.decl [template = constants.%F]
  173. // CHECK:STDOUT: %int_4: Core.IntLiteral = int_value 4 [template = constants.%int_4.1]
  174. // CHECK:STDOUT: %impl.elem0: %Convert.type.2 = interface_witness_access constants.%interface.9, element0 [template = constants.%Convert.14]
  175. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_4, %impl.elem0 [template = constants.%Convert.bound]
  176. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn]
  177. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_4) [template = constants.%int_4.2]
  178. // CHECK:STDOUT: %.loc26_18.1: %i32 = value_of_initializer %int.convert_checked [template = constants.%int_4.2]
  179. // CHECK:STDOUT: %.loc26_18.2: %i32 = converted %int_4, %.loc26_18.1 [template = constants.%int_4.2]
  180. // CHECK:STDOUT: %F.call: init %i32 = call %F.ref(%.loc26_18.2)
  181. // CHECK:STDOUT: %.loc26_20.1: %i32 = value_of_initializer %F.call
  182. // CHECK:STDOUT: %.loc26_20.2: %i32 = converted %F.call, %.loc26_20.1
  183. // CHECK:STDOUT: return %.loc26_20.2
  184. // CHECK:STDOUT: }
  185. // CHECK:STDOUT: