basic.carbon 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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: [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 [template]
  18. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  19. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [template]
  20. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  21. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  22. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  23. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  24. // CHECK:STDOUT: %.2: i32 = int_literal 1 [template]
  25. // CHECK:STDOUT: %.3: type = array_type %.2, i32 [template]
  26. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  27. // CHECK:STDOUT: %.5: i32 = int_literal 0 [template]
  28. // CHECK:STDOUT: %.6: type = tuple_type (i32) [template]
  29. // CHECK:STDOUT: %array: %.3 = tuple_value (%.5) [template]
  30. // CHECK:STDOUT: }
  31. // CHECK:STDOUT:
  32. // CHECK:STDOUT: imports {
  33. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  34. // CHECK:STDOUT: .Bool = %import_ref.1
  35. // CHECK:STDOUT: .Int32 = %import_ref.2
  36. // CHECK:STDOUT: import Core//prelude
  37. // CHECK:STDOUT: import Core//prelude/operators
  38. // CHECK:STDOUT: import Core//prelude/types
  39. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  40. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  41. // CHECK:STDOUT: import Core//prelude/operators/comparison
  42. // CHECK:STDOUT: import Core//prelude/types/bool
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT: %import_ref.1: %Bool.type = import_ref Core//prelude/types/bool, inst+2, loaded [template = constants.%Bool]
  45. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: file {
  49. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  50. // CHECK:STDOUT: .Core = imports.%Core
  51. // CHECK:STDOUT: .F = %F.decl
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: %Core.import = import Core
  54. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  55. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  56. // CHECK:STDOUT: %.loc11_9.1: type = value_of_initializer %bool.make_type [template = bool]
  57. // CHECK:STDOUT: %.loc11_9.2: type = converted %bool.make_type, %.loc11_9.1 [template = bool]
  58. // CHECK:STDOUT: %b.loc11_6.1: bool = param b
  59. // CHECK:STDOUT: @F.%b: bool = bind_name b, %b.loc11_6.1
  60. // CHECK:STDOUT: %int.make_type_32.loc11_18: init type = call constants.%Int32() [template = i32]
  61. // CHECK:STDOUT: %.loc11_18.1: type = value_of_initializer %int.make_type_32.loc11_18 [template = i32]
  62. // CHECK:STDOUT: %.loc11_18.2: type = converted %int.make_type_32.loc11_18, %.loc11_18.1 [template = i32]
  63. // CHECK:STDOUT: %n.loc11_15.1: i32 = param n
  64. // CHECK:STDOUT: @F.%n: i32 = bind_name n, %n.loc11_15.1
  65. // CHECK:STDOUT: %int.make_type_32.loc11_26: init type = call constants.%Int32() [template = i32]
  66. // CHECK:STDOUT: %.loc11_26.1: type = value_of_initializer %int.make_type_32.loc11_26 [template = i32]
  67. // CHECK:STDOUT: %.loc11_26.2: type = converted %int.make_type_32.loc11_26, %.loc11_26.1 [template = i32]
  68. // CHECK:STDOUT: %m.loc11_23.1: i32 = param m
  69. // CHECK:STDOUT: @F.%m: i32 = bind_name m, %m.loc11_23.1
  70. // CHECK:STDOUT: %int.make_type_32.loc11_34: init type = call constants.%Int32() [template = i32]
  71. // CHECK:STDOUT: %.loc11_34.1: type = value_of_initializer %int.make_type_32.loc11_34 [template = i32]
  72. // CHECK:STDOUT: %.loc11_34.2: type = converted %int.make_type_32.loc11_34, %.loc11_34.1 [template = i32]
  73. // CHECK:STDOUT: @F.%return: ref i32 = var <return slot>
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: fn @Bool() -> type = "bool.make_type";
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: fn @F(%b: bool, %n: i32, %m: i32) -> i32 {
  82. // CHECK:STDOUT: !entry:
  83. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  84. // CHECK:STDOUT: %.loc12_16: i32 = int_literal 1 [template = constants.%.2]
  85. // CHECK:STDOUT: %.loc12_11.1: type = value_of_initializer %int.make_type_32 [template = i32]
  86. // CHECK:STDOUT: %.loc12_11.2: type = converted %int.make_type_32, %.loc12_11.1 [template = i32]
  87. // CHECK:STDOUT: %.loc12_17: type = array_type %.loc12_16, i32 [template = constants.%.3]
  88. // CHECK:STDOUT: %x.var: ref %.3 = var x
  89. // CHECK:STDOUT: %x: ref %.3 = bind_name x, %x.var
  90. // CHECK:STDOUT: %.loc12_22: i32 = int_literal 0 [template = constants.%.5]
  91. // CHECK:STDOUT: %.loc12_24.1: %.6 = tuple_literal (%.loc12_22)
  92. // CHECK:STDOUT: %.loc12_24.2: i32 = int_literal 0 [template = constants.%.5]
  93. // CHECK:STDOUT: %.loc12_24.3: ref i32 = array_index %x.var, %.loc12_24.2
  94. // CHECK:STDOUT: %.loc12_24.4: init i32 = initialize_from %.loc12_22 to %.loc12_24.3 [template = constants.%.5]
  95. // CHECK:STDOUT: %.loc12_24.5: init %.3 = array_init (%.loc12_24.4) to %x.var [template = constants.%array]
  96. // CHECK:STDOUT: %.loc12_25: init %.3 = converted %.loc12_24.1, %.loc12_24.5 [template = constants.%array]
  97. // CHECK:STDOUT: assign %x.var, %.loc12_25
  98. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  99. // CHECK:STDOUT: if %b.ref br !if.expr.then else br !if.expr.else
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: !if.expr.then:
  102. // CHECK:STDOUT: %x.ref.loc13_20: ref %.3 = name_ref x, %x
  103. // CHECK:STDOUT: %m.ref: i32 = name_ref m, %m
  104. // CHECK:STDOUT: %.loc13_23.1: ref i32 = array_index %x.ref.loc13_20, %m.ref
  105. // CHECK:STDOUT: %.loc13_23.2: i32 = bind_value %.loc13_23.1
  106. // CHECK:STDOUT: br !if.expr.result(%.loc13_23.2)
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: !if.expr.else:
  109. // CHECK:STDOUT: %x.ref.loc13_30: ref %.3 = name_ref x, %x
  110. // CHECK:STDOUT: %n.ref: i32 = name_ref n, %n
  111. // CHECK:STDOUT: %.loc13_33.1: ref i32 = array_index %x.ref.loc13_30, %n.ref
  112. // CHECK:STDOUT: %.loc13_33.2: i32 = bind_value %.loc13_33.1
  113. // CHECK:STDOUT: br !if.expr.result(%.loc13_33.2)
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: !if.expr.result:
  116. // CHECK:STDOUT: %.loc13_10: i32 = block_arg !if.expr.result
  117. // CHECK:STDOUT: return %.loc13_10
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT: