constant_condition.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/constant_condition.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/if_expr/constant_condition.carbon
  10. fn A() -> i32 { return 1; }
  11. fn B() -> i32 { return 2; }
  12. fn F() -> i32 {
  13. return if true then A() else B();
  14. }
  15. fn G() -> i32 {
  16. return if false then A() else B();
  17. }
  18. fn Constant() -> i32 {
  19. var v: if true then i32 else i32* = 1;
  20. var w: if false then i32 else i32* = &v;
  21. return *w;
  22. }
  23. fn PartiallyConstant(t: type) -> i32 {
  24. var v: if true then i32 else t = 1;
  25. var w: if false then t else i32* = &v;
  26. return *w;
  27. }
  28. // CHECK:STDOUT: --- constant_condition.carbon
  29. // CHECK:STDOUT:
  30. // CHECK:STDOUT: constants {
  31. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  32. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  33. // CHECK:STDOUT: %A.type: type = fn_type @A [template]
  34. // CHECK:STDOUT: %A: %A.type = struct_value () [template]
  35. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [template]
  36. // CHECK:STDOUT: %Convert.type.cd1: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  37. // CHECK:STDOUT: %impl_witness.5b0: <witness> = impl_witness (imports.%import_ref.723), @impl.1(%int_32) [template]
  38. // CHECK:STDOUT: %Convert.type.466: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  39. // CHECK:STDOUT: %Convert.925: %Convert.type.466 = struct_value () [template]
  40. // CHECK:STDOUT: %Convert.bound.afd: <bound method> = bound_method %int_1.5b8, %Convert.925 [template]
  41. // CHECK:STDOUT: %Convert.specific_fn.b73: <specific function> = specific_function %Convert.bound.afd, @Convert.2(%int_32) [template]
  42. // CHECK:STDOUT: %int_1.c60: %i32 = int_value 1 [template]
  43. // CHECK:STDOUT: %B.type: type = fn_type @B [template]
  44. // CHECK:STDOUT: %B: %B.type = struct_value () [template]
  45. // CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [template]
  46. // CHECK:STDOUT: %Convert.bound.f0b: <bound method> = bound_method %int_2.ecc, %Convert.925 [template]
  47. // CHECK:STDOUT: %Convert.specific_fn.20e: <specific function> = specific_function %Convert.bound.f0b, @Convert.2(%int_32) [template]
  48. // CHECK:STDOUT: %int_2.166: %i32 = int_value 2 [template]
  49. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  50. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  51. // CHECK:STDOUT: %true: bool = bool_literal true [template]
  52. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  53. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  54. // CHECK:STDOUT: %false: bool = bool_literal false [template]
  55. // CHECK:STDOUT: %Constant.type: type = fn_type @Constant [template]
  56. // CHECK:STDOUT: %Constant: %Constant.type = struct_value () [template]
  57. // CHECK:STDOUT: %ptr: type = ptr_type %i32 [template]
  58. // CHECK:STDOUT: %PartiallyConstant.type: type = fn_type @PartiallyConstant [template]
  59. // CHECK:STDOUT: %PartiallyConstant: %PartiallyConstant.type = struct_value () [template]
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT:
  62. // CHECK:STDOUT: imports {
  63. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  64. // CHECK:STDOUT: .Int = %import_ref.187
  65. // CHECK:STDOUT: .ImplicitAs = %import_ref.a69
  66. // CHECK:STDOUT: import Core//prelude
  67. // CHECK:STDOUT: import Core//prelude/...
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: file {
  72. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  73. // CHECK:STDOUT: .Core = imports.%Core
  74. // CHECK:STDOUT: .A = %A.decl
  75. // CHECK:STDOUT: .B = %B.decl
  76. // CHECK:STDOUT: .F = %F.decl
  77. // CHECK:STDOUT: .G = %G.decl
  78. // CHECK:STDOUT: .Constant = %Constant.decl
  79. // CHECK:STDOUT: .PartiallyConstant = %PartiallyConstant.decl
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT: %Core.import = import Core
  82. // CHECK:STDOUT: %A.decl: %A.type = fn_decl @A [template = constants.%A] {
  83. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  84. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param0
  85. // CHECK:STDOUT: } {
  86. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  87. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  88. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param0
  89. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %B.decl: %B.type = fn_decl @B [template = constants.%B] {
  92. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  93. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param0
  94. // CHECK:STDOUT: } {
  95. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  96. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  97. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param0
  98. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  101. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  102. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param0
  103. // CHECK:STDOUT: } {
  104. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  105. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  106. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param0
  107. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  110. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  111. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param0
  112. // CHECK:STDOUT: } {
  113. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  114. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  115. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param0
  116. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT: %Constant.decl: %Constant.type = fn_decl @Constant [template = constants.%Constant] {
  119. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  120. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param0
  121. // CHECK:STDOUT: } {
  122. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  123. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  124. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param0
  125. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT: %PartiallyConstant.decl: %PartiallyConstant.type = fn_decl @PartiallyConstant [template = constants.%PartiallyConstant] {
  128. // CHECK:STDOUT: %t.patt: type = binding_pattern t
  129. // CHECK:STDOUT: %t.param_patt: type = value_param_pattern %t.patt, runtime_param0
  130. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  131. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  132. // CHECK:STDOUT: } {
  133. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  134. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  135. // CHECK:STDOUT: %t.param: type = value_param runtime_param0
  136. // CHECK:STDOUT: %t: type = bind_name t, %t.param
  137. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  138. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: fn @A() -> %i32 {
  143. // CHECK:STDOUT: !entry:
  144. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  145. // CHECK:STDOUT: %impl.elem0: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  146. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_1, %impl.elem0 [template = constants.%Convert.bound.afd]
  147. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.b73]
  148. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_1) [template = constants.%int_1.c60]
  149. // CHECK:STDOUT: %.loc11_25.1: %i32 = value_of_initializer %int.convert_checked [template = constants.%int_1.c60]
  150. // CHECK:STDOUT: %.loc11_25.2: %i32 = converted %int_1, %.loc11_25.1 [template = constants.%int_1.c60]
  151. // CHECK:STDOUT: return %.loc11_25.2
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: fn @B() -> %i32 {
  155. // CHECK:STDOUT: !entry:
  156. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template = constants.%int_2.ecc]
  157. // CHECK:STDOUT: %impl.elem0: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  158. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_2, %impl.elem0 [template = constants.%Convert.bound.f0b]
  159. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.20e]
  160. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_2) [template = constants.%int_2.166]
  161. // CHECK:STDOUT: %.loc12_25.1: %i32 = value_of_initializer %int.convert_checked [template = constants.%int_2.166]
  162. // CHECK:STDOUT: %.loc12_25.2: %i32 = converted %int_2, %.loc12_25.1 [template = constants.%int_2.166]
  163. // CHECK:STDOUT: return %.loc12_25.2
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: fn @F() -> %i32 {
  167. // CHECK:STDOUT: !entry:
  168. // CHECK:STDOUT: %true: bool = bool_literal true [template = constants.%true]
  169. // CHECK:STDOUT: if %true br !if.expr.then else br !if.expr.else
  170. // CHECK:STDOUT:
  171. // CHECK:STDOUT: !if.expr.then:
  172. // CHECK:STDOUT: %A.ref: %A.type = name_ref A, file.%A.decl [template = constants.%A]
  173. // CHECK:STDOUT: %A.call: init %i32 = call %A.ref()
  174. // CHECK:STDOUT: %.loc15_25.1: %i32 = value_of_initializer %A.call
  175. // CHECK:STDOUT: %.loc15_25.2: %i32 = converted %A.call, %.loc15_25.1
  176. // CHECK:STDOUT: br !if.expr.result(%.loc15_25.2)
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: !if.expr.else:
  179. // CHECK:STDOUT: %B.ref: %B.type = name_ref B, file.%B.decl [template = constants.%B]
  180. // CHECK:STDOUT: %B.call: init %i32 = call %B.ref()
  181. // CHECK:STDOUT: %.loc15_27.1: %i32 = value_of_initializer %B.call
  182. // CHECK:STDOUT: %.loc15_27.2: %i32 = converted %B.call, %.loc15_27.1
  183. // CHECK:STDOUT: br !if.expr.result(%.loc15_27.2)
  184. // CHECK:STDOUT:
  185. // CHECK:STDOUT: !if.expr.result:
  186. // CHECK:STDOUT: %.loc15_10: %i32 = block_arg !if.expr.result
  187. // CHECK:STDOUT: return %.loc15_10
  188. // CHECK:STDOUT: }
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: fn @G() -> %i32 {
  191. // CHECK:STDOUT: !entry:
  192. // CHECK:STDOUT: %false: bool = bool_literal false [template = constants.%false]
  193. // CHECK:STDOUT: if %false br !if.expr.then else br !if.expr.else
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: !if.expr.then:
  196. // CHECK:STDOUT: %A.ref: %A.type = name_ref A, file.%A.decl [template = constants.%A]
  197. // CHECK:STDOUT: %A.call: init %i32 = call %A.ref()
  198. // CHECK:STDOUT: %.loc19_26.1: %i32 = value_of_initializer %A.call
  199. // CHECK:STDOUT: %.loc19_26.2: %i32 = converted %A.call, %.loc19_26.1
  200. // CHECK:STDOUT: br !if.expr.result(%.loc19_26.2)
  201. // CHECK:STDOUT:
  202. // CHECK:STDOUT: !if.expr.else:
  203. // CHECK:STDOUT: %B.ref: %B.type = name_ref B, file.%B.decl [template = constants.%B]
  204. // CHECK:STDOUT: %B.call: init %i32 = call %B.ref()
  205. // CHECK:STDOUT: %.loc19_28.1: %i32 = value_of_initializer %B.call
  206. // CHECK:STDOUT: %.loc19_28.2: %i32 = converted %B.call, %.loc19_28.1
  207. // CHECK:STDOUT: br !if.expr.result(%.loc19_28.2)
  208. // CHECK:STDOUT:
  209. // CHECK:STDOUT: !if.expr.result:
  210. // CHECK:STDOUT: %.loc19_10: %i32 = block_arg !if.expr.result
  211. // CHECK:STDOUT: return %.loc19_10
  212. // CHECK:STDOUT: }
  213. // CHECK:STDOUT:
  214. // CHECK:STDOUT: fn @Constant() -> %i32 {
  215. // CHECK:STDOUT: !entry:
  216. // CHECK:STDOUT: %v.var: ref %i32 = var v
  217. // CHECK:STDOUT: %v: ref %i32 = bind_name v, %v.var
  218. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  219. // CHECK:STDOUT: %impl.elem0: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  220. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_1, %impl.elem0 [template = constants.%Convert.bound.afd]
  221. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.b73]
  222. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_1) [template = constants.%int_1.c60]
  223. // CHECK:STDOUT: %.loc23: init %i32 = converted %int_1, %int.convert_checked [template = constants.%int_1.c60]
  224. // CHECK:STDOUT: assign %v.var, %.loc23
  225. // CHECK:STDOUT: %w.var: ref %ptr = var w
  226. // CHECK:STDOUT: %w: ref %ptr = bind_name w, %w.var
  227. // CHECK:STDOUT: %v.ref: ref %i32 = name_ref v, %v
  228. // CHECK:STDOUT: %addr: %ptr = addr_of %v.ref
  229. // CHECK:STDOUT: assign %w.var, %addr
  230. // CHECK:STDOUT: %w.ref: ref %ptr = name_ref w, %w
  231. // CHECK:STDOUT: %.loc25_11: %ptr = bind_value %w.ref
  232. // CHECK:STDOUT: %.loc25_10.1: ref %i32 = deref %.loc25_11
  233. // CHECK:STDOUT: %.loc25_10.2: %i32 = bind_value %.loc25_10.1
  234. // CHECK:STDOUT: return %.loc25_10.2
  235. // CHECK:STDOUT: }
  236. // CHECK:STDOUT:
  237. // CHECK:STDOUT: fn @PartiallyConstant(%t.param_patt: type) -> %i32 {
  238. // CHECK:STDOUT: !entry:
  239. // CHECK:STDOUT: %v.var: ref %i32 = var v
  240. // CHECK:STDOUT: %v: ref %i32 = bind_name v, %v.var
  241. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  242. // CHECK:STDOUT: %impl.elem0: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  243. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_1, %impl.elem0 [template = constants.%Convert.bound.afd]
  244. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.b73]
  245. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_1) [template = constants.%int_1.c60]
  246. // CHECK:STDOUT: %.loc29: init %i32 = converted %int_1, %int.convert_checked [template = constants.%int_1.c60]
  247. // CHECK:STDOUT: assign %v.var, %.loc29
  248. // CHECK:STDOUT: %w.var: ref %ptr = var w
  249. // CHECK:STDOUT: %w: ref %ptr = bind_name w, %w.var
  250. // CHECK:STDOUT: %v.ref: ref %i32 = name_ref v, %v
  251. // CHECK:STDOUT: %addr: %ptr = addr_of %v.ref
  252. // CHECK:STDOUT: assign %w.var, %addr
  253. // CHECK:STDOUT: %w.ref: ref %ptr = name_ref w, %w
  254. // CHECK:STDOUT: %.loc31_11: %ptr = bind_value %w.ref
  255. // CHECK:STDOUT: %.loc31_10.1: ref %i32 = deref %.loc31_11
  256. // CHECK:STDOUT: %.loc31_10.2: %i32 = bind_value %.loc31_10.1
  257. // CHECK:STDOUT: return %.loc31_10.2
  258. // CHECK:STDOUT: }
  259. // CHECK:STDOUT: