unary_op.carbon 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/operators/builtin/unary_op.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/operators/builtin/unary_op.carbon
  10. fn Not(b: bool) -> bool {
  11. return not b;
  12. }
  13. let not_true: bool = not true;
  14. let not_false: bool = not false;
  15. fn Constant() {
  16. var a: if not true then bool else () = ();
  17. var b: if not false then bool else () = true;
  18. }
  19. // CHECK:STDOUT: --- unary_op.carbon
  20. // CHECK:STDOUT:
  21. // CHECK:STDOUT: constants {
  22. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [template]
  23. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  24. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [template]
  25. // CHECK:STDOUT: %Not.type: type = fn_type @Not [template]
  26. // CHECK:STDOUT: %Not: %Not.type = struct_value () [template]
  27. // CHECK:STDOUT: %.2: bool = bool_literal true [template]
  28. // CHECK:STDOUT: %.3: bool = bool_literal false [template]
  29. // CHECK:STDOUT: %Constant.type: type = fn_type @Constant [template]
  30. // CHECK:STDOUT: %Constant: %Constant.type = struct_value () [template]
  31. // CHECK:STDOUT: %tuple: %.1 = tuple_value () [template]
  32. // CHECK:STDOUT: }
  33. // CHECK:STDOUT:
  34. // CHECK:STDOUT: imports {
  35. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  36. // CHECK:STDOUT: .Bool = %import_ref
  37. // CHECK:STDOUT: import Core//prelude
  38. // CHECK:STDOUT: import Core//prelude/...
  39. // CHECK:STDOUT: }
  40. // CHECK:STDOUT: %import_ref: %Bool.type = import_ref Core//prelude/types/bool, inst+5, loaded [template = constants.%Bool]
  41. // CHECK:STDOUT: }
  42. // CHECK:STDOUT:
  43. // CHECK:STDOUT: file {
  44. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  45. // CHECK:STDOUT: .Core = imports.%Core
  46. // CHECK:STDOUT: .Not = %Not.decl
  47. // CHECK:STDOUT: .not_true = @__global_init.%not_true
  48. // CHECK:STDOUT: .not_false = @__global_init.%not_false
  49. // CHECK:STDOUT: .Constant = %Constant.decl
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT: %Core.import = import Core
  52. // CHECK:STDOUT: %Not.decl: %Not.type = fn_decl @Not [template = constants.%Not] {
  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: %return.patt: bool = return_slot_pattern
  56. // CHECK:STDOUT: %return.param_patt: bool = out_param_pattern %return.patt, runtime_param1
  57. // CHECK:STDOUT: } {
  58. // CHECK:STDOUT: %bool.make_type.loc11_11: init type = call constants.%Bool() [template = bool]
  59. // CHECK:STDOUT: %.loc11_11.1: type = value_of_initializer %bool.make_type.loc11_11 [template = bool]
  60. // CHECK:STDOUT: %.loc11_11.2: type = converted %bool.make_type.loc11_11, %.loc11_11.1 [template = bool]
  61. // CHECK:STDOUT: %bool.make_type.loc11_20: init type = call constants.%Bool() [template = bool]
  62. // CHECK:STDOUT: %.loc11_20.1: type = value_of_initializer %bool.make_type.loc11_20 [template = bool]
  63. // CHECK:STDOUT: %.loc11_20.2: type = converted %bool.make_type.loc11_20, %.loc11_20.1 [template = bool]
  64. // CHECK:STDOUT: %b.param: bool = value_param runtime_param0
  65. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  66. // CHECK:STDOUT: %return.param: ref bool = out_param runtime_param1
  67. // CHECK:STDOUT: %return: ref bool = return_slot %return.param
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: %bool.make_type.loc15: init type = call constants.%Bool() [template = bool]
  70. // CHECK:STDOUT: %.loc15_15.1: type = value_of_initializer %bool.make_type.loc15 [template = bool]
  71. // CHECK:STDOUT: %.loc15_15.2: type = converted %bool.make_type.loc15, %.loc15_15.1 [template = bool]
  72. // CHECK:STDOUT: %bool.make_type.loc16: init type = call constants.%Bool() [template = bool]
  73. // CHECK:STDOUT: %.loc16_16.1: type = value_of_initializer %bool.make_type.loc16 [template = bool]
  74. // CHECK:STDOUT: %.loc16_16.2: type = converted %bool.make_type.loc16, %.loc16_16.1 [template = bool]
  75. // CHECK:STDOUT: %Constant.decl: %Constant.type = fn_decl @Constant [template = constants.%Constant] {} {}
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: fn @Bool() -> type = "bool.make_type";
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: fn @Not(%b.param_patt: bool) -> bool {
  81. // CHECK:STDOUT: !entry:
  82. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  83. // CHECK:STDOUT: %.loc12: bool = not %b.ref
  84. // CHECK:STDOUT: return %.loc12
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: fn @Constant() {
  88. // CHECK:STDOUT: !entry:
  89. // CHECK:STDOUT: %.loc19_17: bool = bool_literal true [template = constants.%.2]
  90. // CHECK:STDOUT: %.loc19_13: bool = not %.loc19_17 [template = constants.%.3]
  91. // CHECK:STDOUT: if %.loc19_13 br !if.expr.then.loc19 else br !if.expr.else.loc19
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: !if.expr.then.loc19:
  94. // CHECK:STDOUT: %bool.make_type.loc19: init type = call constants.%Bool() [template = bool]
  95. // CHECK:STDOUT: %.loc19_27.1: type = value_of_initializer %bool.make_type.loc19 [template = bool]
  96. // CHECK:STDOUT: %.loc19_27.2: type = converted %bool.make_type.loc19, %.loc19_27.1 [template = bool]
  97. // CHECK:STDOUT: br !if.expr.result.loc19(%.loc19_27.2)
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: !if.expr.else.loc19:
  100. // CHECK:STDOUT: %.loc19_38: %.1 = tuple_literal ()
  101. // CHECK:STDOUT: %.loc19_32: type = converted %.loc19_38, constants.%.1 [template = constants.%.1]
  102. // CHECK:STDOUT: br !if.expr.result.loc19(%.loc19_32)
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: !if.expr.result.loc19:
  105. // CHECK:STDOUT: %.loc19_10: type = block_arg !if.expr.result.loc19 [template = constants.%.1]
  106. // CHECK:STDOUT: %a.var: ref %.1 = var a
  107. // CHECK:STDOUT: %a: ref %.1 = bind_name a, %a.var
  108. // CHECK:STDOUT: %.loc19_43.1: %.1 = tuple_literal ()
  109. // CHECK:STDOUT: %.loc19_43.2: init %.1 = tuple_init () to %a.var [template = constants.%tuple]
  110. // CHECK:STDOUT: %.loc19_44: init %.1 = converted %.loc19_43.1, %.loc19_43.2 [template = constants.%tuple]
  111. // CHECK:STDOUT: assign %a.var, %.loc19_44
  112. // CHECK:STDOUT: %.loc20_17: bool = bool_literal false [template = constants.%.3]
  113. // CHECK:STDOUT: %.loc20_13: bool = not %.loc20_17 [template = constants.%.2]
  114. // CHECK:STDOUT: if %.loc20_13 br !if.expr.then.loc20 else br !if.expr.else.loc20
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: !if.expr.then.loc20:
  117. // CHECK:STDOUT: %bool.make_type.loc20: init type = call constants.%Bool() [template = bool]
  118. // CHECK:STDOUT: %.loc20_28.1: type = value_of_initializer %bool.make_type.loc20 [template = bool]
  119. // CHECK:STDOUT: %.loc20_28.2: type = converted %bool.make_type.loc20, %.loc20_28.1 [template = bool]
  120. // CHECK:STDOUT: br !if.expr.result.loc20(%.loc20_28.2)
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: !if.expr.else.loc20:
  123. // CHECK:STDOUT: %.loc20_39: %.1 = tuple_literal ()
  124. // CHECK:STDOUT: %.loc20_33: type = converted %.loc20_39, constants.%.1 [template = constants.%.1]
  125. // CHECK:STDOUT: br !if.expr.result.loc20(%.loc20_33)
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: !if.expr.result.loc20:
  128. // CHECK:STDOUT: %.loc20_10: type = block_arg !if.expr.result.loc20 [template = bool]
  129. // CHECK:STDOUT: %b.var: ref bool = var b
  130. // CHECK:STDOUT: %b: ref bool = bind_name b, %b.var
  131. // CHECK:STDOUT: %.loc20_43: bool = bool_literal true [template = constants.%.2]
  132. // CHECK:STDOUT: assign %b.var, %.loc20_43
  133. // CHECK:STDOUT: return
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: fn @__global_init() {
  137. // CHECK:STDOUT: !entry:
  138. // CHECK:STDOUT: %.loc15_26: bool = bool_literal true [template = constants.%.2]
  139. // CHECK:STDOUT: %.loc15_22: bool = not %.loc15_26 [template = constants.%.3]
  140. // CHECK:STDOUT: %not_true: bool = bind_name not_true, %.loc15_22
  141. // CHECK:STDOUT: %.loc16_27: bool = bool_literal false [template = constants.%.3]
  142. // CHECK:STDOUT: %.loc16_23: bool = not %.loc16_27 [template = constants.%.2]
  143. // CHECK:STDOUT: %not_false: bool = bind_name not_false, %.loc16_23
  144. // CHECK:STDOUT: return
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT: