basic.carbon 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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: %Bool: %Bool.type = struct_value () [template]
  19. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  20. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  21. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  22. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  23. // CHECK:STDOUT: %.1: i32 = int_value 1 [template]
  24. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(Core.IntLiteral) [template]
  25. // CHECK:STDOUT: %Convert.type.15: type = fn_type @Convert.11 [template]
  26. // CHECK:STDOUT: %Convert.15: %Convert.type.15 = struct_value () [template]
  27. // CHECK:STDOUT: %.25: <witness> = interface_witness (%Convert.15) [template]
  28. // CHECK:STDOUT: %.26: <bound method> = bound_method %.1, %Convert.15 [template]
  29. // CHECK:STDOUT: %.27: Core.IntLiteral = int_value 1 [template]
  30. // CHECK:STDOUT: %.28: type = array_type %.27, i32 [template]
  31. // CHECK:STDOUT: %.30: i32 = int_value 0 [template]
  32. // CHECK:STDOUT: %tuple.type: type = tuple_type (i32) [template]
  33. // CHECK:STDOUT: %array: %.28 = tuple_value (%.30) [template]
  34. // CHECK:STDOUT: }
  35. // CHECK:STDOUT:
  36. // CHECK:STDOUT: imports {
  37. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  38. // CHECK:STDOUT: .Bool = %import_ref.1
  39. // CHECK:STDOUT: .Int32 = %import_ref.2
  40. // CHECK:STDOUT: .ImplicitAs = %import_ref.3
  41. // CHECK:STDOUT: import Core//prelude
  42. // CHECK:STDOUT: import Core//prelude/...
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT:
  46. // CHECK:STDOUT: file {
  47. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  48. // CHECK:STDOUT: .Core = imports.%Core
  49. // CHECK:STDOUT: .F = %F.decl
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT: %Core.import = import Core
  52. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  53. // CHECK:STDOUT: %b.patt: bool = binding_pattern b
  54. // CHECK:STDOUT: %b.param_patt: bool = value_param_pattern %b.patt, runtime_param0
  55. // CHECK:STDOUT: %n.patt: i32 = binding_pattern n
  56. // CHECK:STDOUT: %n.param_patt: i32 = value_param_pattern %n.patt, runtime_param1
  57. // CHECK:STDOUT: %m.patt: i32 = binding_pattern m
  58. // CHECK:STDOUT: %m.param_patt: i32 = value_param_pattern %m.patt, runtime_param2
  59. // CHECK:STDOUT: %return.patt: i32 = return_slot_pattern
  60. // CHECK:STDOUT: %return.param_patt: i32 = out_param_pattern %return.patt, runtime_param3
  61. // CHECK:STDOUT: } {
  62. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  63. // CHECK:STDOUT: %.loc11_9.1: type = value_of_initializer %bool.make_type [template = bool]
  64. // CHECK:STDOUT: %.loc11_9.2: type = converted %bool.make_type, %.loc11_9.1 [template = bool]
  65. // CHECK:STDOUT: %int.make_type_32.loc11_18: init type = call constants.%Int32() [template = i32]
  66. // CHECK:STDOUT: %.loc11_18.1: type = value_of_initializer %int.make_type_32.loc11_18 [template = i32]
  67. // CHECK:STDOUT: %.loc11_18.2: type = converted %int.make_type_32.loc11_18, %.loc11_18.1 [template = i32]
  68. // CHECK:STDOUT: %int.make_type_32.loc11_26: init type = call constants.%Int32() [template = i32]
  69. // CHECK:STDOUT: %.loc11_26.1: type = value_of_initializer %int.make_type_32.loc11_26 [template = i32]
  70. // CHECK:STDOUT: %.loc11_26.2: type = converted %int.make_type_32.loc11_26, %.loc11_26.1 [template = i32]
  71. // CHECK:STDOUT: %int.make_type_32.loc11_34: init type = call constants.%Int32() [template = i32]
  72. // CHECK:STDOUT: %.loc11_34.1: type = value_of_initializer %int.make_type_32.loc11_34 [template = i32]
  73. // CHECK:STDOUT: %.loc11_34.2: type = converted %int.make_type_32.loc11_34, %.loc11_34.1 [template = i32]
  74. // CHECK:STDOUT: %b.param: bool = value_param runtime_param0
  75. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  76. // CHECK:STDOUT: %n.param: i32 = value_param runtime_param1
  77. // CHECK:STDOUT: %n: i32 = bind_name n, %n.param
  78. // CHECK:STDOUT: %m.param: i32 = value_param runtime_param2
  79. // CHECK:STDOUT: %m: i32 = bind_name m, %m.param
  80. // CHECK:STDOUT: %return.param: ref i32 = out_param runtime_param3
  81. // CHECK:STDOUT: %return: ref i32 = return_slot %return.param
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: fn @F(%b.param_patt: bool, %n.param_patt: i32, %m.param_patt: i32) -> i32 {
  86. // CHECK:STDOUT: !entry:
  87. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  88. // CHECK:STDOUT: %.loc12_16.1: i32 = int_value 1 [template = constants.%.1]
  89. // CHECK:STDOUT: %.loc12_11.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  90. // CHECK:STDOUT: %.loc12_11.2: type = converted %int.make_type_32.loc12, %.loc12_11.1 [template = i32]
  91. // CHECK:STDOUT: %.loc12_16.2: %Convert.type.2 = interface_witness_access constants.%.25, element0 [template = constants.%Convert.15]
  92. // CHECK:STDOUT: %.loc12_16.3: <bound method> = bound_method %.loc12_16.1, %.loc12_16.2 [template = constants.%.26]
  93. // CHECK:STDOUT: %int.convert_checked: init Core.IntLiteral = call %.loc12_16.3(%.loc12_16.1) [template = constants.%.27]
  94. // CHECK:STDOUT: %.loc12_16.4: Core.IntLiteral = value_of_initializer %int.convert_checked [template = constants.%.27]
  95. // CHECK:STDOUT: %.loc12_16.5: Core.IntLiteral = converted %.loc12_16.1, %.loc12_16.4 [template = constants.%.27]
  96. // CHECK:STDOUT: %.loc12_17: type = array_type %.loc12_16.5, i32 [template = constants.%.28]
  97. // CHECK:STDOUT: %x.var: ref %.28 = var x
  98. // CHECK:STDOUT: %x: ref %.28 = bind_name x, %x.var
  99. // CHECK:STDOUT: %.loc12_22: i32 = int_value 0 [template = constants.%.30]
  100. // CHECK:STDOUT: %.loc12_24.1: %tuple.type = tuple_literal (%.loc12_22)
  101. // CHECK:STDOUT: %.loc12_24.2: i32 = int_value 0 [template = constants.%.30]
  102. // CHECK:STDOUT: %.loc12_24.3: ref i32 = array_index %x.var, %.loc12_24.2
  103. // CHECK:STDOUT: %.loc12_24.4: init i32 = initialize_from %.loc12_22 to %.loc12_24.3 [template = constants.%.30]
  104. // CHECK:STDOUT: %.loc12_24.5: init %.28 = array_init (%.loc12_24.4) to %x.var [template = constants.%array]
  105. // CHECK:STDOUT: %.loc12_25: init %.28 = converted %.loc12_24.1, %.loc12_24.5 [template = constants.%array]
  106. // CHECK:STDOUT: assign %x.var, %.loc12_25
  107. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  108. // CHECK:STDOUT: if %b.ref br !if.expr.then else br !if.expr.else
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: !if.expr.then:
  111. // CHECK:STDOUT: %x.ref.loc13_20: ref %.28 = name_ref x, %x
  112. // CHECK:STDOUT: %m.ref: i32 = name_ref m, %m
  113. // CHECK:STDOUT: %.loc13_23.1: ref i32 = array_index %x.ref.loc13_20, %m.ref
  114. // CHECK:STDOUT: %.loc13_23.2: i32 = bind_value %.loc13_23.1
  115. // CHECK:STDOUT: br !if.expr.result(%.loc13_23.2)
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: !if.expr.else:
  118. // CHECK:STDOUT: %x.ref.loc13_30: ref %.28 = name_ref x, %x
  119. // CHECK:STDOUT: %n.ref: i32 = name_ref n, %n
  120. // CHECK:STDOUT: %.loc13_33.1: ref i32 = array_index %x.ref.loc13_30, %n.ref
  121. // CHECK:STDOUT: %.loc13_33.2: i32 = bind_value %.loc13_33.1
  122. // CHECK:STDOUT: br !if.expr.result(%.loc13_33.2)
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: !if.expr.result:
  125. // CHECK:STDOUT: %.loc13_10: i32 = block_arg !if.expr.result
  126. // CHECK:STDOUT: return %.loc13_10
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT: