basic.carbon 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/if_expr/basic.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/if_expr/basic.carbon
  10. fn F(b: bool, n: i32, m: i32) -> i32 {
  11. var x: array(i32, 1) = (0,);
  12. return if b then x[m] else x[n];
  13. }
  14. // CHECK:STDOUT: --- basic.carbon
  15. // CHECK:STDOUT:
  16. // CHECK:STDOUT: constants {
  17. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [concrete]
  18. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [concrete]
  19. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  20. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  21. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  22. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  23. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
  24. // CHECK:STDOUT: %array_type: type = array_type %int_1, %i32 [concrete]
  25. // CHECK:STDOUT: %int_0.5c6: Core.IntLiteral = int_value 0 [concrete]
  26. // CHECK:STDOUT: %tuple.type: type = tuple_type (Core.IntLiteral) [concrete]
  27. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  28. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [concrete]
  29. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.4f9(%int_32) [concrete]
  30. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.4f9(%int_32) [concrete]
  31. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [concrete]
  32. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, (%impl_witness.d39) [concrete]
  33. // CHECK:STDOUT: %.be7: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [concrete]
  34. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_0.5c6, %Convert.956 [concrete]
  35. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.956, @Convert.2(%int_32) [concrete]
  36. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_0.5c6, %Convert.specific_fn [concrete]
  37. // CHECK:STDOUT: %int_0.6a9: %i32 = int_value 0 [concrete]
  38. // CHECK:STDOUT: %array: %array_type = tuple_value (%int_0.6a9) [concrete]
  39. // CHECK:STDOUT: }
  40. // CHECK:STDOUT:
  41. // CHECK:STDOUT: imports {
  42. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  43. // CHECK:STDOUT: .Bool = %Core.Bool
  44. // CHECK:STDOUT: .Int = %Core.Int
  45. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  46. // CHECK:STDOUT: import Core//prelude
  47. // CHECK:STDOUT: import Core//prelude/...
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT: }
  50. // CHECK:STDOUT:
  51. // CHECK:STDOUT: file {
  52. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  53. // CHECK:STDOUT: .Core = imports.%Core
  54. // CHECK:STDOUT: .F = %F.decl
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT: %Core.import = import Core
  57. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  58. // CHECK:STDOUT: %b.patt: bool = binding_pattern b
  59. // CHECK:STDOUT: %b.param_patt: bool = value_param_pattern %b.patt, call_param0
  60. // CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
  61. // CHECK:STDOUT: %n.param_patt: %i32 = value_param_pattern %n.patt, call_param1
  62. // CHECK:STDOUT: %m.patt: %i32 = binding_pattern m
  63. // CHECK:STDOUT: %m.param_patt: %i32 = value_param_pattern %m.patt, call_param2
  64. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  65. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, call_param3
  66. // CHECK:STDOUT: } {
  67. // CHECK:STDOUT: %int_32.loc11_34: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  68. // CHECK:STDOUT: %i32.loc11_34: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  69. // CHECK:STDOUT: %b.param: bool = value_param call_param0
  70. // CHECK:STDOUT: %.loc11_9.1: type = splice_block %.loc11_9.3 [concrete = bool] {
  71. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [concrete = bool]
  72. // CHECK:STDOUT: %.loc11_9.2: type = value_of_initializer %bool.make_type [concrete = bool]
  73. // CHECK:STDOUT: %.loc11_9.3: type = converted %bool.make_type, %.loc11_9.2 [concrete = bool]
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  76. // CHECK:STDOUT: %n.param: %i32 = value_param call_param1
  77. // CHECK:STDOUT: %.loc11_18: type = splice_block %i32.loc11_18 [concrete = constants.%i32] {
  78. // CHECK:STDOUT: %int_32.loc11_18: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  79. // CHECK:STDOUT: %i32.loc11_18: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT: %n: %i32 = bind_name n, %n.param
  82. // CHECK:STDOUT: %m.param: %i32 = value_param call_param2
  83. // CHECK:STDOUT: %.loc11_26: type = splice_block %i32.loc11_26 [concrete = constants.%i32] {
  84. // CHECK:STDOUT: %int_32.loc11_26: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  85. // CHECK:STDOUT: %i32.loc11_26: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT: %m: %i32 = bind_name m, %m.param
  88. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param3
  89. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: fn @F(%b.param_patt: bool, %n.param_patt: %i32, %m.param_patt: %i32) -> %i32 {
  94. // CHECK:STDOUT: !entry:
  95. // CHECK:STDOUT: name_binding_decl {
  96. // CHECK:STDOUT: %x.patt: %array_type = binding_pattern x
  97. // CHECK:STDOUT: %.loc12_3.1: %array_type = var_pattern %x.patt
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: %x.var: ref %array_type = var x
  100. // CHECK:STDOUT: %int_0.loc12_27: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  101. // CHECK:STDOUT: %.loc12_29.1: %tuple.type = tuple_literal (%int_0.loc12_27)
  102. // CHECK:STDOUT: %impl.elem0: %.be7 = impl_witness_access constants.%impl_witness.d39, element0 [concrete = constants.%Convert.956]
  103. // CHECK:STDOUT: %bound_method.loc12_29.1: <bound method> = bound_method %int_0.loc12_27, %impl.elem0 [concrete = constants.%Convert.bound]
  104. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  105. // CHECK:STDOUT: %bound_method.loc12_29.2: <bound method> = bound_method %int_0.loc12_27, %specific_fn [concrete = constants.%bound_method]
  106. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %bound_method.loc12_29.2(%int_0.loc12_27) [concrete = constants.%int_0.6a9]
  107. // CHECK:STDOUT: %.loc12_29.2: init %i32 = converted %int_0.loc12_27, %int.convert_checked [concrete = constants.%int_0.6a9]
  108. // CHECK:STDOUT: %int_0.loc12_29: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  109. // CHECK:STDOUT: %.loc12_29.3: ref %i32 = array_index %x.var, %int_0.loc12_29
  110. // CHECK:STDOUT: %.loc12_29.4: init %i32 = initialize_from %.loc12_29.2 to %.loc12_29.3 [concrete = constants.%int_0.6a9]
  111. // CHECK:STDOUT: %.loc12_29.5: init %array_type = array_init (%.loc12_29.4) to %x.var [concrete = constants.%array]
  112. // CHECK:STDOUT: %.loc12_3.2: init %array_type = converted %.loc12_29.1, %.loc12_29.5 [concrete = constants.%array]
  113. // CHECK:STDOUT: assign %x.var, %.loc12_3.2
  114. // CHECK:STDOUT: %.loc12_22: type = splice_block %array_type [concrete = constants.%array_type] {
  115. // CHECK:STDOUT: %int_32.loc12: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  116. // CHECK:STDOUT: %i32.loc12: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  117. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  118. // CHECK:STDOUT: %array_type: type = array_type %int_1, %i32.loc12 [concrete = constants.%array_type]
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT: %x: ref %array_type = bind_name x, %x.var
  121. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  122. // CHECK:STDOUT: if %b.ref br !if.expr.then else br !if.expr.else
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: !if.expr.then:
  125. // CHECK:STDOUT: %x.ref.loc13_20: ref %array_type = name_ref x, %x
  126. // CHECK:STDOUT: %m.ref: %i32 = name_ref m, %m
  127. // CHECK:STDOUT: %int_32.loc13_23: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  128. // CHECK:STDOUT: %i32.loc13_23: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  129. // CHECK:STDOUT: %.loc13_23.1: ref %i32 = array_index %x.ref.loc13_20, %m.ref
  130. // CHECK:STDOUT: %.loc13_23.2: %i32 = bind_value %.loc13_23.1
  131. // CHECK:STDOUT: br !if.expr.result(%.loc13_23.2)
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: !if.expr.else:
  134. // CHECK:STDOUT: %x.ref.loc13_30: ref %array_type = name_ref x, %x
  135. // CHECK:STDOUT: %n.ref: %i32 = name_ref n, %n
  136. // CHECK:STDOUT: %int_32.loc13_33: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  137. // CHECK:STDOUT: %i32.loc13_33: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  138. // CHECK:STDOUT: %.loc13_33.1: ref %i32 = array_index %x.ref.loc13_30, %n.ref
  139. // CHECK:STDOUT: %.loc13_33.2: %i32 = bind_value %.loc13_33.1
  140. // CHECK:STDOUT: br !if.expr.result(%.loc13_33.2)
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: !if.expr.result:
  143. // CHECK:STDOUT: %.loc13_10: %i32 = block_arg !if.expr.result
  144. // CHECK:STDOUT: return %.loc13_10
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT: