and.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. fn F() -> bool { return true; }
  7. fn G() -> bool { return true; }
  8. fn And() -> bool {
  9. return F() and G();
  10. }
  11. fn Constant() {
  12. var a: if true and true then bool else () = true;
  13. var b: if true and false then bool else () = ();
  14. var c: if false and true then bool else () = ();
  15. var d: if false and false then bool else () = ();
  16. }
  17. fn PartialConstant(x: bool) {
  18. var a: if false and x then bool else () = ();
  19. }
  20. // CHECK:STDOUT: --- and.carbon
  21. // CHECK:STDOUT:
  22. // CHECK:STDOUT: constants {
  23. // CHECK:STDOUT: %F: type = fn_type @F [template]
  24. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  25. // CHECK:STDOUT: %struct.1: F = struct_value () [template]
  26. // CHECK:STDOUT: %.2: bool = bool_literal true [template]
  27. // CHECK:STDOUT: %G: type = fn_type @G [template]
  28. // CHECK:STDOUT: %struct.2: G = struct_value () [template]
  29. // CHECK:STDOUT: %And: type = fn_type @And [template]
  30. // CHECK:STDOUT: %struct.3: And = struct_value () [template]
  31. // CHECK:STDOUT: %.3: bool = bool_literal false [template]
  32. // CHECK:STDOUT: %Constant: type = fn_type @Constant [template]
  33. // CHECK:STDOUT: %struct.4: Constant = struct_value () [template]
  34. // CHECK:STDOUT: %tuple: () = tuple_value () [template]
  35. // CHECK:STDOUT: %PartialConstant: type = fn_type @PartialConstant [template]
  36. // CHECK:STDOUT: %struct.5: PartialConstant = struct_value () [template]
  37. // CHECK:STDOUT: }
  38. // CHECK:STDOUT:
  39. // CHECK:STDOUT: file {
  40. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  41. // CHECK:STDOUT: .Core = %Core
  42. // CHECK:STDOUT: .F = %F.decl
  43. // CHECK:STDOUT: .G = %G.decl
  44. // CHECK:STDOUT: .And = %And.decl
  45. // CHECK:STDOUT: .Constant = %Constant.decl
  46. // CHECK:STDOUT: .PartialConstant = %PartialConstant.decl
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  49. // CHECK:STDOUT: %F.decl: F = fn_decl @F [template = constants.%struct.1] {
  50. // CHECK:STDOUT: @F.%return: ref bool = var <return slot>
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT: %G.decl: G = fn_decl @G [template = constants.%struct.2] {
  53. // CHECK:STDOUT: @G.%return: ref bool = var <return slot>
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT: %And.decl: And = fn_decl @And [template = constants.%struct.3] {
  56. // CHECK:STDOUT: @And.%return: ref bool = var <return slot>
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT: %Constant.decl: Constant = fn_decl @Constant [template = constants.%struct.4] {}
  59. // CHECK:STDOUT: %PartialConstant.decl: PartialConstant = fn_decl @PartialConstant [template = constants.%struct.5] {
  60. // CHECK:STDOUT: %x.loc21_20.1: bool = param x
  61. // CHECK:STDOUT: @PartialConstant.%x: bool = bind_name x, %x.loc21_20.1
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: fn @F() -> bool {
  66. // CHECK:STDOUT: !entry:
  67. // CHECK:STDOUT: %.loc7: bool = bool_literal true [template = constants.%.2]
  68. // CHECK:STDOUT: return %.loc7
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: fn @G() -> bool {
  72. // CHECK:STDOUT: !entry:
  73. // CHECK:STDOUT: %.loc8: bool = bool_literal true [template = constants.%.2]
  74. // CHECK:STDOUT: return %.loc8
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: fn @And() -> bool {
  78. // CHECK:STDOUT: !entry:
  79. // CHECK:STDOUT: %F.ref: F = name_ref F, file.%F.decl [template = constants.%struct.1]
  80. // CHECK:STDOUT: %F.call: init bool = call %F.ref()
  81. // CHECK:STDOUT: %.loc11_14.1: bool = value_of_initializer %F.call
  82. // CHECK:STDOUT: %.loc11_14.2: bool = converted %F.call, %.loc11_14.1
  83. // CHECK:STDOUT: %.loc11_14.3: bool = bool_literal false [template = constants.%.3]
  84. // CHECK:STDOUT: if %.loc11_14.2 br !and.rhs else br !and.result(%.loc11_14.3)
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: !and.rhs:
  87. // CHECK:STDOUT: %G.ref: G = name_ref G, file.%G.decl [template = constants.%struct.2]
  88. // CHECK:STDOUT: %G.call: init bool = call %G.ref()
  89. // CHECK:STDOUT: %.loc11_14.4: bool = value_of_initializer %G.call
  90. // CHECK:STDOUT: %.loc11_14.5: bool = converted %G.call, %.loc11_14.4
  91. // CHECK:STDOUT: br !and.result(%.loc11_14.5)
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: !and.result:
  94. // CHECK:STDOUT: %.loc11_14.6: bool = block_arg !and.result
  95. // CHECK:STDOUT: return %.loc11_14.6
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: fn @Constant() {
  99. // CHECK:STDOUT: !entry:
  100. // CHECK:STDOUT: %.loc15_13: bool = bool_literal true [template = constants.%.2]
  101. // CHECK:STDOUT: %.loc15_18.1: bool = bool_literal false [template = constants.%.3]
  102. // CHECK:STDOUT: if %.loc15_13 br !and.rhs.loc15 else br !and.result.loc15(%.loc15_18.1)
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: !and.rhs.loc15:
  105. // CHECK:STDOUT: %.loc15_22: bool = bool_literal true [template = constants.%.2]
  106. // CHECK:STDOUT: br !and.result.loc15(%.loc15_22)
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: !and.result.loc15:
  109. // CHECK:STDOUT: %.loc15_18.2: bool = block_arg !and.result.loc15 [template = constants.%.2]
  110. // CHECK:STDOUT: if %.loc15_18.2 br !if.expr.then.loc15 else br !if.expr.else.loc15
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: !if.expr.then.loc15:
  113. // CHECK:STDOUT: br !if.expr.result.loc15(bool)
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: !if.expr.else.loc15:
  116. // CHECK:STDOUT: %.loc15_43: () = tuple_literal ()
  117. // CHECK:STDOUT: %.loc15_37: type = converted %.loc15_43, constants.%.1 [template = constants.%.1]
  118. // CHECK:STDOUT: br !if.expr.result.loc15(%.loc15_37)
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: !if.expr.result.loc15:
  121. // CHECK:STDOUT: %.loc15_10: type = block_arg !if.expr.result.loc15 [template = bool]
  122. // CHECK:STDOUT: %a.var: ref bool = var a
  123. // CHECK:STDOUT: %a: ref bool = bind_name a, %a.var
  124. // CHECK:STDOUT: %.loc15_47: bool = bool_literal true [template = constants.%.2]
  125. // CHECK:STDOUT: assign %a.var, %.loc15_47
  126. // CHECK:STDOUT: %.loc16_13: bool = bool_literal true [template = constants.%.2]
  127. // CHECK:STDOUT: %.loc16_18.1: bool = bool_literal false [template = constants.%.3]
  128. // CHECK:STDOUT: if %.loc16_13 br !and.rhs.loc16 else br !and.result.loc16(%.loc16_18.1)
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: !and.rhs.loc16:
  131. // CHECK:STDOUT: %.loc16_22: bool = bool_literal false [template = constants.%.3]
  132. // CHECK:STDOUT: br !and.result.loc16(%.loc16_22)
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: !and.result.loc16:
  135. // CHECK:STDOUT: %.loc16_18.2: bool = block_arg !and.result.loc16 [template = constants.%.3]
  136. // CHECK:STDOUT: if %.loc16_18.2 br !if.expr.then.loc16 else br !if.expr.else.loc16
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: !if.expr.then.loc16:
  139. // CHECK:STDOUT: br !if.expr.result.loc16(bool)
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: !if.expr.else.loc16:
  142. // CHECK:STDOUT: %.loc16_44: () = tuple_literal ()
  143. // CHECK:STDOUT: %.loc16_38: type = converted %.loc16_44, constants.%.1 [template = constants.%.1]
  144. // CHECK:STDOUT: br !if.expr.result.loc16(%.loc16_38)
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: !if.expr.result.loc16:
  147. // CHECK:STDOUT: %.loc16_10: type = block_arg !if.expr.result.loc16 [template = constants.%.1]
  148. // CHECK:STDOUT: %b.var: ref () = var b
  149. // CHECK:STDOUT: %b: ref () = bind_name b, %b.var
  150. // CHECK:STDOUT: %.loc16_49.1: () = tuple_literal ()
  151. // CHECK:STDOUT: %.loc16_49.2: init () = tuple_init () to %b.var [template = constants.%tuple]
  152. // CHECK:STDOUT: %.loc16_50: init () = converted %.loc16_49.1, %.loc16_49.2 [template = constants.%tuple]
  153. // CHECK:STDOUT: assign %b.var, %.loc16_50
  154. // CHECK:STDOUT: %.loc17_13: bool = bool_literal false [template = constants.%.3]
  155. // CHECK:STDOUT: %.loc17_19.1: bool = bool_literal false [template = constants.%.3]
  156. // CHECK:STDOUT: if %.loc17_13 br !and.rhs.loc17 else br !and.result.loc17(%.loc17_19.1)
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: !and.rhs.loc17:
  159. // CHECK:STDOUT: %.loc17_23: bool = bool_literal true [template = constants.%.2]
  160. // CHECK:STDOUT: br !and.result.loc17(%.loc17_23)
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: !and.result.loc17:
  163. // CHECK:STDOUT: %.loc17_19.2: bool = block_arg !and.result.loc17 [template = constants.%.3]
  164. // CHECK:STDOUT: if %.loc17_19.2 br !if.expr.then.loc17 else br !if.expr.else.loc17
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: !if.expr.then.loc17:
  167. // CHECK:STDOUT: br !if.expr.result.loc17(bool)
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: !if.expr.else.loc17:
  170. // CHECK:STDOUT: %.loc17_44: () = tuple_literal ()
  171. // CHECK:STDOUT: %.loc17_38: type = converted %.loc17_44, constants.%.1 [template = constants.%.1]
  172. // CHECK:STDOUT: br !if.expr.result.loc17(%.loc17_38)
  173. // CHECK:STDOUT:
  174. // CHECK:STDOUT: !if.expr.result.loc17:
  175. // CHECK:STDOUT: %.loc17_10: type = block_arg !if.expr.result.loc17 [template = constants.%.1]
  176. // CHECK:STDOUT: %c.var: ref () = var c
  177. // CHECK:STDOUT: %c: ref () = bind_name c, %c.var
  178. // CHECK:STDOUT: %.loc17_49.1: () = tuple_literal ()
  179. // CHECK:STDOUT: %.loc17_49.2: init () = tuple_init () to %c.var [template = constants.%tuple]
  180. // CHECK:STDOUT: %.loc17_50: init () = converted %.loc17_49.1, %.loc17_49.2 [template = constants.%tuple]
  181. // CHECK:STDOUT: assign %c.var, %.loc17_50
  182. // CHECK:STDOUT: %.loc18_13: bool = bool_literal false [template = constants.%.3]
  183. // CHECK:STDOUT: %.loc18_19.1: bool = bool_literal false [template = constants.%.3]
  184. // CHECK:STDOUT: if %.loc18_13 br !and.rhs.loc18 else br !and.result.loc18(%.loc18_19.1)
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: !and.rhs.loc18:
  187. // CHECK:STDOUT: %.loc18_23: bool = bool_literal false [template = constants.%.3]
  188. // CHECK:STDOUT: br !and.result.loc18(%.loc18_23)
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: !and.result.loc18:
  191. // CHECK:STDOUT: %.loc18_19.2: bool = block_arg !and.result.loc18 [template = constants.%.3]
  192. // CHECK:STDOUT: if %.loc18_19.2 br !if.expr.then.loc18 else br !if.expr.else.loc18
  193. // CHECK:STDOUT:
  194. // CHECK:STDOUT: !if.expr.then.loc18:
  195. // CHECK:STDOUT: br !if.expr.result.loc18(bool)
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: !if.expr.else.loc18:
  198. // CHECK:STDOUT: %.loc18_45: () = tuple_literal ()
  199. // CHECK:STDOUT: %.loc18_39: type = converted %.loc18_45, constants.%.1 [template = constants.%.1]
  200. // CHECK:STDOUT: br !if.expr.result.loc18(%.loc18_39)
  201. // CHECK:STDOUT:
  202. // CHECK:STDOUT: !if.expr.result.loc18:
  203. // CHECK:STDOUT: %.loc18_10: type = block_arg !if.expr.result.loc18 [template = constants.%.1]
  204. // CHECK:STDOUT: %d.var: ref () = var d
  205. // CHECK:STDOUT: %d: ref () = bind_name d, %d.var
  206. // CHECK:STDOUT: %.loc18_50.1: () = tuple_literal ()
  207. // CHECK:STDOUT: %.loc18_50.2: init () = tuple_init () to %d.var [template = constants.%tuple]
  208. // CHECK:STDOUT: %.loc18_51: init () = converted %.loc18_50.1, %.loc18_50.2 [template = constants.%tuple]
  209. // CHECK:STDOUT: assign %d.var, %.loc18_51
  210. // CHECK:STDOUT: return
  211. // CHECK:STDOUT: }
  212. // CHECK:STDOUT:
  213. // CHECK:STDOUT: fn @PartialConstant(%x: bool) {
  214. // CHECK:STDOUT: !entry:
  215. // CHECK:STDOUT: %.loc22_13: bool = bool_literal false [template = constants.%.3]
  216. // CHECK:STDOUT: %.loc22_19.1: bool = bool_literal false [template = constants.%.3]
  217. // CHECK:STDOUT: if %.loc22_13 br !and.rhs else br !and.result(%.loc22_19.1)
  218. // CHECK:STDOUT:
  219. // CHECK:STDOUT: !and.rhs:
  220. // CHECK:STDOUT: %x.ref: bool = name_ref x, %x
  221. // CHECK:STDOUT: br !and.result(%x.ref)
  222. // CHECK:STDOUT:
  223. // CHECK:STDOUT: !and.result:
  224. // CHECK:STDOUT: %.loc22_19.2: bool = block_arg !and.result [template = constants.%.3]
  225. // CHECK:STDOUT: if %.loc22_19.2 br !if.expr.then else br !if.expr.else
  226. // CHECK:STDOUT:
  227. // CHECK:STDOUT: !if.expr.then:
  228. // CHECK:STDOUT: br !if.expr.result(bool)
  229. // CHECK:STDOUT:
  230. // CHECK:STDOUT: !if.expr.else:
  231. // CHECK:STDOUT: %.loc22_41: () = tuple_literal ()
  232. // CHECK:STDOUT: %.loc22_35: type = converted %.loc22_41, constants.%.1 [template = constants.%.1]
  233. // CHECK:STDOUT: br !if.expr.result(%.loc22_35)
  234. // CHECK:STDOUT:
  235. // CHECK:STDOUT: !if.expr.result:
  236. // CHECK:STDOUT: %.loc22_10: type = block_arg !if.expr.result [template = constants.%.1]
  237. // CHECK:STDOUT: %a.var: ref () = var a
  238. // CHECK:STDOUT: %a: ref () = bind_name a, %a.var
  239. // CHECK:STDOUT: %.loc22_46.1: () = tuple_literal ()
  240. // CHECK:STDOUT: %.loc22_46.2: init () = tuple_init () to %a.var [template = constants.%tuple]
  241. // CHECK:STDOUT: %.loc22_47: init () = converted %.loc22_46.1, %.loc22_46.2 [template = constants.%tuple]
  242. // CHECK:STDOUT: assign %a.var, %.loc22_47
  243. // CHECK:STDOUT: return
  244. // CHECK:STDOUT: }
  245. // CHECK:STDOUT: