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