control_flow.carbon 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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: %Int32.type: type = fn_type @Int32 [template]
  19. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  20. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  21. // CHECK:STDOUT: %A.type: type = fn_type @A [template]
  22. // CHECK:STDOUT: %A: %A.type = struct_value () [template]
  23. // CHECK:STDOUT: %.2: i32 = int_literal 1 [template]
  24. // CHECK:STDOUT: %B.type: type = fn_type @B [template]
  25. // CHECK:STDOUT: %B: %B.type = struct_value () [template]
  26. // CHECK:STDOUT: %.3: i32 = int_literal 2 [template]
  27. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [template]
  28. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [template]
  29. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  30. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  31. // CHECK:STDOUT: }
  32. // CHECK:STDOUT:
  33. // CHECK:STDOUT: file {
  34. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  35. // CHECK:STDOUT: .Core = %Core
  36. // CHECK:STDOUT: .A = %A.decl
  37. // CHECK:STDOUT: .B = %B.decl
  38. // CHECK:STDOUT: .F = %F.decl
  39. // CHECK:STDOUT: }
  40. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  41. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  42. // CHECK:STDOUT: %A.decl: %A.type = fn_decl @A [template = constants.%A] {
  43. // CHECK:STDOUT: %int.make_type_32.loc11: init type = call constants.%Int32() [template = i32]
  44. // CHECK:STDOUT: %.loc11_11.1: type = value_of_initializer %int.make_type_32.loc11 [template = i32]
  45. // CHECK:STDOUT: %.loc11_11.2: type = converted %int.make_type_32.loc11, %.loc11_11.1 [template = i32]
  46. // CHECK:STDOUT: @A.%return: ref i32 = var <return slot>
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  49. // CHECK:STDOUT: %B.decl: %B.type = fn_decl @B [template = constants.%B] {
  50. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  51. // CHECK:STDOUT: %.loc12_11.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  52. // CHECK:STDOUT: %.loc12_11.2: type = converted %int.make_type_32.loc12, %.loc12_11.1 [template = i32]
  53. // CHECK:STDOUT: @B.%return: ref i32 = var <return slot>
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT: %import_ref.3: %Bool.type = import_ref ir7, inst+2, loaded [template = constants.%Bool]
  56. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  57. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  58. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  59. // CHECK:STDOUT: %.loc14_9.1: type = value_of_initializer %bool.make_type [template = bool]
  60. // CHECK:STDOUT: %.loc14_9.2: type = converted %bool.make_type, %.loc14_9.1 [template = bool]
  61. // CHECK:STDOUT: %b.loc14_6.1: bool = param b
  62. // CHECK:STDOUT: @F.%b: bool = bind_name b, %b.loc14_6.1
  63. // CHECK:STDOUT: %int.make_type_32.loc14: init type = call constants.%Int32() [template = i32]
  64. // CHECK:STDOUT: %.loc14_18.1: type = value_of_initializer %int.make_type_32.loc14 [template = i32]
  65. // CHECK:STDOUT: %.loc14_18.2: type = converted %int.make_type_32.loc14, %.loc14_18.1 [template = i32]
  66. // CHECK:STDOUT: @F.%return: ref i32 = var <return slot>
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT:
  70. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: fn @A() -> i32 {
  73. // CHECK:STDOUT: !entry:
  74. // CHECK:STDOUT: %.loc11: i32 = int_literal 1 [template = constants.%.2]
  75. // CHECK:STDOUT: return %.loc11
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: fn @B() -> i32 {
  79. // CHECK:STDOUT: !entry:
  80. // CHECK:STDOUT: %.loc12: i32 = int_literal 2 [template = constants.%.3]
  81. // CHECK:STDOUT: return %.loc12
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: fn @Bool() -> type = "bool.make_type";
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: fn @F(%b: bool) -> i32 {
  87. // CHECK:STDOUT: !entry:
  88. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  89. // CHECK:STDOUT: if %b.ref br !if.expr.then else br !if.expr.else
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: !if.expr.then:
  92. // CHECK:STDOUT: %A.ref: %A.type = name_ref A, file.%A.decl [template = constants.%A]
  93. // CHECK:STDOUT: %A.call: init i32 = call %A.ref()
  94. // CHECK:STDOUT: %.loc15_21.1: i32 = value_of_initializer %A.call
  95. // CHECK:STDOUT: %.loc15_21.2: i32 = converted %A.call, %.loc15_21.1
  96. // CHECK:STDOUT: br !if.expr.result(%.loc15_21.2)
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: !if.expr.else:
  99. // CHECK:STDOUT: %B.ref: %B.type = name_ref B, file.%B.decl [template = constants.%B]
  100. // CHECK:STDOUT: %B.call: init i32 = call %B.ref()
  101. // CHECK:STDOUT: %.loc15_24.1: i32 = value_of_initializer %B.call
  102. // CHECK:STDOUT: %.loc15_24.2: i32 = converted %B.call, %.loc15_24.1
  103. // CHECK:STDOUT: br !if.expr.result(%.loc15_24.2)
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: !if.expr.result:
  106. // CHECK:STDOUT: %.loc15_10: i32 = block_arg !if.expr.result
  107. // CHECK:STDOUT: return %.loc15_10
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT: