or.carbon 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/or.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/operators/builtin/or.carbon
  10. fn F() -> bool { return true; }
  11. fn G() -> bool { return true; }
  12. fn Or() -> bool {
  13. return F() or G();
  14. }
  15. fn Constant() {
  16. var a: if true or true then bool else () = true;
  17. var b: if true or false then bool else () = true;
  18. var c: if false or true then bool else () = true;
  19. var d: if false or false then bool else () = ();
  20. }
  21. fn PartialConstant(x: bool) {
  22. var a: if true or x then bool else () = true;
  23. }
  24. // CHECK:STDOUT: --- or.carbon
  25. // CHECK:STDOUT:
  26. // CHECK:STDOUT: constants {
  27. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [template]
  28. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  29. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [template]
  30. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  31. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  32. // CHECK:STDOUT: %true: bool = bool_literal true [template]
  33. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  34. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  35. // CHECK:STDOUT: %Or.type: type = fn_type @Or [template]
  36. // CHECK:STDOUT: %Or: %Or.type = struct_value () [template]
  37. // CHECK:STDOUT: %Constant.type: type = fn_type @Constant [template]
  38. // CHECK:STDOUT: %Constant: %Constant.type = struct_value () [template]
  39. // CHECK:STDOUT: %empty_tuple: %empty_tuple.type = tuple_value () [template]
  40. // CHECK:STDOUT: %PartialConstant.type: type = fn_type @PartialConstant [template]
  41. // CHECK:STDOUT: %PartialConstant: %PartialConstant.type = struct_value () [template]
  42. // CHECK:STDOUT: }
  43. // CHECK:STDOUT:
  44. // CHECK:STDOUT: imports {
  45. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  46. // CHECK:STDOUT: .Bool = %import_ref
  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: .F = %F.decl
  56. // CHECK:STDOUT: .G = %G.decl
  57. // CHECK:STDOUT: .Or = %Or.decl
  58. // CHECK:STDOUT: .Constant = %Constant.decl
  59. // CHECK:STDOUT: .PartialConstant = %PartialConstant.decl
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %Core.import = import Core
  62. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  63. // CHECK:STDOUT: %return.patt: bool = return_slot_pattern
  64. // CHECK:STDOUT: %return.param_patt: bool = out_param_pattern %return.patt, runtime_param0
  65. // CHECK:STDOUT: } {
  66. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  67. // CHECK:STDOUT: %.loc11_11.1: type = value_of_initializer %bool.make_type [template = bool]
  68. // CHECK:STDOUT: %.loc11_11.2: type = converted %bool.make_type, %.loc11_11.1 [template = bool]
  69. // CHECK:STDOUT: %return.param: ref bool = out_param runtime_param0
  70. // CHECK:STDOUT: %return: ref bool = return_slot %return.param
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  73. // CHECK:STDOUT: %return.patt: bool = return_slot_pattern
  74. // CHECK:STDOUT: %return.param_patt: bool = out_param_pattern %return.patt, runtime_param0
  75. // CHECK:STDOUT: } {
  76. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  77. // CHECK:STDOUT: %.loc12_11.1: type = value_of_initializer %bool.make_type [template = bool]
  78. // CHECK:STDOUT: %.loc12_11.2: type = converted %bool.make_type, %.loc12_11.1 [template = bool]
  79. // CHECK:STDOUT: %return.param: ref bool = out_param runtime_param0
  80. // CHECK:STDOUT: %return: ref bool = return_slot %return.param
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %Or.decl: %Or.type = fn_decl @Or [template = constants.%Or] {
  83. // CHECK:STDOUT: %return.patt: bool = return_slot_pattern
  84. // CHECK:STDOUT: %return.param_patt: bool = out_param_pattern %return.patt, runtime_param0
  85. // CHECK:STDOUT: } {
  86. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  87. // CHECK:STDOUT: %.loc14_12.1: type = value_of_initializer %bool.make_type [template = bool]
  88. // CHECK:STDOUT: %.loc14_12.2: type = converted %bool.make_type, %.loc14_12.1 [template = bool]
  89. // CHECK:STDOUT: %return.param: ref bool = out_param runtime_param0
  90. // CHECK:STDOUT: %return: ref bool = return_slot %return.param
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: %Constant.decl: %Constant.type = fn_decl @Constant [template = constants.%Constant] {} {}
  93. // CHECK:STDOUT: %PartialConstant.decl: %PartialConstant.type = fn_decl @PartialConstant [template = constants.%PartialConstant] {
  94. // CHECK:STDOUT: %x.patt: bool = binding_pattern x
  95. // CHECK:STDOUT: %x.param_patt: bool = value_param_pattern %x.patt, runtime_param0
  96. // CHECK:STDOUT: } {
  97. // CHECK:STDOUT: %x.param: bool = value_param runtime_param0
  98. // CHECK:STDOUT: %.loc25_23.1: type = splice_block %.loc25_23.3 [template = bool] {
  99. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  100. // CHECK:STDOUT: %.loc25_23.2: type = value_of_initializer %bool.make_type [template = bool]
  101. // CHECK:STDOUT: %.loc25_23.3: type = converted %bool.make_type, %.loc25_23.2 [template = bool]
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: %x: bool = bind_name x, %x.param
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: fn @F() -> bool {
  108. // CHECK:STDOUT: !entry:
  109. // CHECK:STDOUT: %true: bool = bool_literal true [template = constants.%true]
  110. // CHECK:STDOUT: return %true
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: fn @G() -> bool {
  114. // CHECK:STDOUT: !entry:
  115. // CHECK:STDOUT: %true: bool = bool_literal true [template = constants.%true]
  116. // CHECK:STDOUT: return %true
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: fn @Or() -> bool {
  120. // CHECK:STDOUT: !entry:
  121. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [template = constants.%F]
  122. // CHECK:STDOUT: %F.call: init bool = call %F.ref()
  123. // CHECK:STDOUT: %.loc15_14.1: bool = value_of_initializer %F.call
  124. // CHECK:STDOUT: %.loc15_14.2: bool = converted %F.call, %.loc15_14.1
  125. // CHECK:STDOUT: %.loc15_14.3: bool = not %.loc15_14.2
  126. // CHECK:STDOUT: %true: bool = bool_literal true [template = constants.%true]
  127. // CHECK:STDOUT: if %.loc15_14.3 br !or.rhs else br !or.result(%true)
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: !or.rhs:
  130. // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [template = constants.%G]
  131. // CHECK:STDOUT: %G.call: init bool = call %G.ref()
  132. // CHECK:STDOUT: %.loc15_14.4: bool = value_of_initializer %G.call
  133. // CHECK:STDOUT: %.loc15_14.5: bool = converted %G.call, %.loc15_14.4
  134. // CHECK:STDOUT: br !or.result(%.loc15_14.5)
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: !or.result:
  137. // CHECK:STDOUT: %.loc15_14.6: bool = block_arg !or.result
  138. // CHECK:STDOUT: return %.loc15_14.6
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: fn @Constant() {
  142. // CHECK:STDOUT: !entry:
  143. // CHECK:STDOUT: %a.var: ref bool = var a
  144. // CHECK:STDOUT: %a: ref bool = bind_name a, %a.var
  145. // CHECK:STDOUT: %true.loc19: bool = bool_literal true [template = constants.%true]
  146. // CHECK:STDOUT: assign %a.var, %true.loc19
  147. // CHECK:STDOUT: %b.var: ref bool = var b
  148. // CHECK:STDOUT: %b: ref bool = bind_name b, %b.var
  149. // CHECK:STDOUT: %true.loc20: bool = bool_literal true [template = constants.%true]
  150. // CHECK:STDOUT: assign %b.var, %true.loc20
  151. // CHECK:STDOUT: %c.var: ref bool = var c
  152. // CHECK:STDOUT: %c: ref bool = bind_name c, %c.var
  153. // CHECK:STDOUT: %true.loc21: bool = bool_literal true [template = constants.%true]
  154. // CHECK:STDOUT: assign %c.var, %true.loc21
  155. // CHECK:STDOUT: %d.var: ref %empty_tuple.type = var d
  156. // CHECK:STDOUT: %d: ref %empty_tuple.type = bind_name d, %d.var
  157. // CHECK:STDOUT: %.loc22_49.1: %empty_tuple.type = tuple_literal ()
  158. // CHECK:STDOUT: %.loc22_49.2: init %empty_tuple.type = tuple_init () to %d.var [template = constants.%empty_tuple]
  159. // CHECK:STDOUT: %.loc22_50: init %empty_tuple.type = converted %.loc22_49.1, %.loc22_49.2 [template = constants.%empty_tuple]
  160. // CHECK:STDOUT: assign %d.var, %.loc22_50
  161. // CHECK:STDOUT: return
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: fn @PartialConstant(%x.param_patt: bool) {
  165. // CHECK:STDOUT: !entry:
  166. // CHECK:STDOUT: %a.var: ref bool = var a
  167. // CHECK:STDOUT: %a: ref bool = bind_name a, %a.var
  168. // CHECK:STDOUT: %true: bool = bool_literal true [template = constants.%true]
  169. // CHECK:STDOUT: assign %a.var, %true
  170. // CHECK:STDOUT: return
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT: