control_flow.carbon 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/control_flow.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/if_expr/control_flow.carbon
  10. fn A() -> i32 { return 1; }
  11. fn B() -> i32 { return 2; }
  12. fn F(b: bool) -> i32 {
  13. return if b then A() else B();
  14. }
  15. // CHECK:STDOUT: --- control_flow.carbon
  16. // CHECK:STDOUT:
  17. // CHECK:STDOUT: constants {
  18. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  19. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  20. // CHECK:STDOUT: %A.type: type = fn_type @A [template]
  21. // CHECK:STDOUT: %A: %A.type = struct_value () [template]
  22. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [template]
  23. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [template]
  24. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  25. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.1(%int_32) [template]
  26. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  27. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [template]
  28. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, %impl_witness.d39 [template]
  29. // CHECK:STDOUT: %.a0b: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [template]
  30. // CHECK:STDOUT: %Convert.bound.ab5: <bound method> = bound_method %int_1.5b8, %Convert.956 [template]
  31. // CHECK:STDOUT: %Convert.specific_fn.70c: <specific function> = specific_function %Convert.bound.ab5, @Convert.2(%int_32) [template]
  32. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [template]
  33. // CHECK:STDOUT: %B.type: type = fn_type @B [template]
  34. // CHECK:STDOUT: %B: %B.type = struct_value () [template]
  35. // CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [template]
  36. // CHECK:STDOUT: %Convert.bound.ef9: <bound method> = bound_method %int_2.ecc, %Convert.956 [template]
  37. // CHECK:STDOUT: %Convert.specific_fn.787: <specific function> = specific_function %Convert.bound.ef9, @Convert.2(%int_32) [template]
  38. // CHECK:STDOUT: %int_2.ef8: %i32 = int_value 2 [template]
  39. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [template]
  40. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [template]
  41. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  42. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT:
  45. // CHECK:STDOUT: imports {
  46. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  47. // CHECK:STDOUT: .Int = %Core.Int
  48. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  49. // CHECK:STDOUT: .Bool = %Core.Bool
  50. // CHECK:STDOUT: import Core//prelude
  51. // CHECK:STDOUT: import Core//prelude/...
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT:
  55. // CHECK:STDOUT: file {
  56. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  57. // CHECK:STDOUT: .Core = imports.%Core
  58. // CHECK:STDOUT: .A = %A.decl
  59. // CHECK:STDOUT: .B = %B.decl
  60. // CHECK:STDOUT: .F = %F.decl
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: %Core.import = import Core
  63. // CHECK:STDOUT: %A.decl: %A.type = fn_decl @A [template = constants.%A] {
  64. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  65. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param0
  66. // CHECK:STDOUT: } {
  67. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  68. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  69. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param0
  70. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %B.decl: %B.type = fn_decl @B [template = constants.%B] {
  73. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  74. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param0
  75. // CHECK:STDOUT: } {
  76. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  77. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  78. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param0
  79. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  82. // CHECK:STDOUT: %b.patt: bool = binding_pattern b
  83. // CHECK:STDOUT: %b.param_patt: bool = value_param_pattern %b.patt, runtime_param0
  84. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  85. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  86. // CHECK:STDOUT: } {
  87. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  88. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  89. // CHECK:STDOUT: %b.param: bool = value_param runtime_param0
  90. // CHECK:STDOUT: %.loc14_9.1: type = splice_block %.loc14_9.3 [template = bool] {
  91. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  92. // CHECK:STDOUT: %.loc14_9.2: type = value_of_initializer %bool.make_type [template = bool]
  93. // CHECK:STDOUT: %.loc14_9.3: type = converted %bool.make_type, %.loc14_9.2 [template = bool]
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  96. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  97. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: fn @A() -> %i32 {
  102. // CHECK:STDOUT: !entry:
  103. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  104. // CHECK:STDOUT: %impl.elem0: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  105. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_1, %impl.elem0 [template = constants.%Convert.bound.ab5]
  106. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %bound_method, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.70c]
  107. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %specific_fn(%int_1) [template = constants.%int_1.5d2]
  108. // CHECK:STDOUT: %.loc11_25.1: %i32 = value_of_initializer %int.convert_checked [template = constants.%int_1.5d2]
  109. // CHECK:STDOUT: %.loc11_25.2: %i32 = converted %int_1, %.loc11_25.1 [template = constants.%int_1.5d2]
  110. // CHECK:STDOUT: return %.loc11_25.2
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: fn @B() -> %i32 {
  114. // CHECK:STDOUT: !entry:
  115. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template = constants.%int_2.ecc]
  116. // CHECK:STDOUT: %impl.elem0: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  117. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_2, %impl.elem0 [template = constants.%Convert.bound.ef9]
  118. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %bound_method, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.787]
  119. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %specific_fn(%int_2) [template = constants.%int_2.ef8]
  120. // CHECK:STDOUT: %.loc12_25.1: %i32 = value_of_initializer %int.convert_checked [template = constants.%int_2.ef8]
  121. // CHECK:STDOUT: %.loc12_25.2: %i32 = converted %int_2, %.loc12_25.1 [template = constants.%int_2.ef8]
  122. // CHECK:STDOUT: return %.loc12_25.2
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: fn @F(%b.param_patt: bool) -> %i32 {
  126. // CHECK:STDOUT: !entry:
  127. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  128. // CHECK:STDOUT: if %b.ref br !if.expr.then else br !if.expr.else
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: !if.expr.then:
  131. // CHECK:STDOUT: %A.ref: %A.type = name_ref A, file.%A.decl [template = constants.%A]
  132. // CHECK:STDOUT: %A.call: init %i32 = call %A.ref()
  133. // CHECK:STDOUT: %.loc15_22.1: %i32 = value_of_initializer %A.call
  134. // CHECK:STDOUT: %.loc15_22.2: %i32 = converted %A.call, %.loc15_22.1
  135. // CHECK:STDOUT: br !if.expr.result(%.loc15_22.2)
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: !if.expr.else:
  138. // CHECK:STDOUT: %B.ref: %B.type = name_ref B, file.%B.decl [template = constants.%B]
  139. // CHECK:STDOUT: %B.call: init %i32 = call %B.ref()
  140. // CHECK:STDOUT: %.loc15_24.1: %i32 = value_of_initializer %B.call
  141. // CHECK:STDOUT: %.loc15_24.2: %i32 = converted %B.call, %.loc15_24.1
  142. // CHECK:STDOUT: br !if.expr.result(%.loc15_24.2)
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: !if.expr.result:
  145. // CHECK:STDOUT: %.loc15_10: %i32 = block_arg !if.expr.result
  146. // CHECK:STDOUT: return %.loc15_10
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT: