greater.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // --- int_greater.carbon
  7. fn Greater(a: i32, b: i32) -> bool = "int.greater";
  8. fn Negate(a: i32) -> i32 = "int.negate";
  9. class True {}
  10. class False {}
  11. fn F(true_: True, false_: False) {
  12. false_ as (if Greater(1, 2) then True else False);
  13. false_ as (if Greater(1, 1) then True else False);
  14. true_ as (if Greater(1, 0) then True else False);
  15. false_ as (if Greater(Negate(1), 0) then True else False);
  16. true_ as (if Greater(0, Negate(1)) then True else False);
  17. }
  18. fn RuntimeCall(a: i32, b: i32) -> bool {
  19. return Greater(a, b);
  20. }
  21. // CHECK:STDOUT: --- int_greater.carbon
  22. // CHECK:STDOUT:
  23. // CHECK:STDOUT: constants {
  24. // CHECK:STDOUT: %True: type = class_type @True [template]
  25. // CHECK:STDOUT: %.1: type = struct_type {} [template]
  26. // CHECK:STDOUT: %False: type = class_type @False [template]
  27. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  28. // CHECK:STDOUT: %.3: type = ptr_type {} [template]
  29. // CHECK:STDOUT: %.4: i32 = int_literal 1 [template]
  30. // CHECK:STDOUT: %.5: i32 = int_literal 2 [template]
  31. // CHECK:STDOUT: %.6: bool = bool_literal false [template]
  32. // CHECK:STDOUT: %.7: i32 = int_literal 0 [template]
  33. // CHECK:STDOUT: %.8: bool = bool_literal true [template]
  34. // CHECK:STDOUT: %.9: i32 = int_literal -1 [template]
  35. // CHECK:STDOUT: }
  36. // CHECK:STDOUT:
  37. // CHECK:STDOUT: file {
  38. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  39. // CHECK:STDOUT: .Greater = %Greater
  40. // CHECK:STDOUT: .Negate = %Negate
  41. // CHECK:STDOUT: .True = %True.decl
  42. // CHECK:STDOUT: .False = %False.decl
  43. // CHECK:STDOUT: .F = %F
  44. // CHECK:STDOUT: .RuntimeCall = %RuntimeCall
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT: %Greater: <function> = fn_decl @Greater [template] {
  47. // CHECK:STDOUT: %a.loc2_12.1: i32 = param a
  48. // CHECK:STDOUT: @Greater.%a: i32 = bind_name a, %a.loc2_12.1
  49. // CHECK:STDOUT: %b.loc2_20.1: i32 = param b
  50. // CHECK:STDOUT: @Greater.%b: i32 = bind_name b, %b.loc2_20.1
  51. // CHECK:STDOUT: %return.var.loc2: ref bool = var <return slot>
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: %Negate: <function> = fn_decl @Negate [template] {
  54. // CHECK:STDOUT: %a.loc3_11.1: i32 = param a
  55. // CHECK:STDOUT: @Negate.%a: i32 = bind_name a, %a.loc3_11.1
  56. // CHECK:STDOUT: %return.var.loc3: ref i32 = var <return slot>
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT: %True.decl: type = class_decl @True [template = constants.%True] {}
  59. // CHECK:STDOUT: %False.decl: type = class_decl @False [template = constants.%False] {}
  60. // CHECK:STDOUT: %F: <function> = fn_decl @F [template] {
  61. // CHECK:STDOUT: %True.ref: type = name_ref True, %True.decl [template = constants.%True]
  62. // CHECK:STDOUT: %true_.loc8_6.1: True = param true_
  63. // CHECK:STDOUT: @F.%true_: True = bind_name true_, %true_.loc8_6.1
  64. // CHECK:STDOUT: %False.ref: type = name_ref False, %False.decl [template = constants.%False]
  65. // CHECK:STDOUT: %false_.loc8_19.1: False = param false_
  66. // CHECK:STDOUT: @F.%false_: False = bind_name false_, %false_.loc8_19.1
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT: %RuntimeCall: <function> = fn_decl @RuntimeCall [template] {
  69. // CHECK:STDOUT: %a.loc16_16.1: i32 = param a
  70. // CHECK:STDOUT: @RuntimeCall.%a: i32 = bind_name a, %a.loc16_16.1
  71. // CHECK:STDOUT: %b.loc16_24.1: i32 = param b
  72. // CHECK:STDOUT: @RuntimeCall.%b: i32 = bind_name b, %b.loc16_24.1
  73. // CHECK:STDOUT: %return.var.loc16: ref bool = var <return slot>
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: class @True {
  78. // CHECK:STDOUT: !members:
  79. // CHECK:STDOUT: .Self = constants.%True
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: class @False {
  83. // CHECK:STDOUT: !members:
  84. // CHECK:STDOUT: .Self = constants.%False
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: fn @Greater(%a: i32, %b: i32) -> bool = "int.greater";
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: fn @Negate(%a: i32) -> i32 = "int.negate";
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: fn @F(%true_: True, %false_: False) {
  92. // CHECK:STDOUT: !entry:
  93. // CHECK:STDOUT: %false_.ref.loc9: False = name_ref false_, %false_
  94. // CHECK:STDOUT: %Greater.ref.loc9: <function> = name_ref Greater, file.%Greater [template = file.%Greater]
  95. // CHECK:STDOUT: %.loc9_25: i32 = int_literal 1 [template = constants.%.4]
  96. // CHECK:STDOUT: %.loc9_28: i32 = int_literal 2 [template = constants.%.5]
  97. // CHECK:STDOUT: %.loc9_24.1: init bool = call %Greater.ref.loc9(%.loc9_25, %.loc9_28) [template = constants.%.6]
  98. // CHECK:STDOUT: %.loc9_14.1: bool = value_of_initializer %.loc9_24.1 [template = constants.%.6]
  99. // CHECK:STDOUT: %.loc9_24.2: bool = converted %.loc9_24.1, %.loc9_14.1 [template = constants.%.6]
  100. // CHECK:STDOUT: if %.loc9_24.2 br !if.expr.then.loc9 else br !if.expr.else.loc9
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: !if.expr.then.loc9:
  103. // CHECK:STDOUT: %True.ref.loc9: type = name_ref True, file.%True.decl [template = constants.%True]
  104. // CHECK:STDOUT: br !if.expr.result.loc9(%True.ref.loc9)
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: !if.expr.else.loc9:
  107. // CHECK:STDOUT: %False.ref.loc9: type = name_ref False, file.%False.decl [template = constants.%False]
  108. // CHECK:STDOUT: br !if.expr.result.loc9(%False.ref.loc9)
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: !if.expr.result.loc9:
  111. // CHECK:STDOUT: %.loc9_14.2: type = block_arg !if.expr.result.loc9 [template = constants.%False]
  112. // CHECK:STDOUT: %false_.ref.loc10: False = name_ref false_, %false_
  113. // CHECK:STDOUT: %Greater.ref.loc10: <function> = name_ref Greater, file.%Greater [template = file.%Greater]
  114. // CHECK:STDOUT: %.loc10_25: i32 = int_literal 1 [template = constants.%.4]
  115. // CHECK:STDOUT: %.loc10_28: i32 = int_literal 1 [template = constants.%.4]
  116. // CHECK:STDOUT: %.loc10_24.1: init bool = call %Greater.ref.loc10(%.loc10_25, %.loc10_28) [template = constants.%.6]
  117. // CHECK:STDOUT: %.loc10_14.1: bool = value_of_initializer %.loc10_24.1 [template = constants.%.6]
  118. // CHECK:STDOUT: %.loc10_24.2: bool = converted %.loc10_24.1, %.loc10_14.1 [template = constants.%.6]
  119. // CHECK:STDOUT: if %.loc10_24.2 br !if.expr.then.loc10 else br !if.expr.else.loc10
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: !if.expr.then.loc10:
  122. // CHECK:STDOUT: %True.ref.loc10: type = name_ref True, file.%True.decl [template = constants.%True]
  123. // CHECK:STDOUT: br !if.expr.result.loc10(%True.ref.loc10)
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: !if.expr.else.loc10:
  126. // CHECK:STDOUT: %False.ref.loc10: type = name_ref False, file.%False.decl [template = constants.%False]
  127. // CHECK:STDOUT: br !if.expr.result.loc10(%False.ref.loc10)
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: !if.expr.result.loc10:
  130. // CHECK:STDOUT: %.loc10_14.2: type = block_arg !if.expr.result.loc10 [template = constants.%False]
  131. // CHECK:STDOUT: %true_.ref.loc11: True = name_ref true_, %true_
  132. // CHECK:STDOUT: %Greater.ref.loc11: <function> = name_ref Greater, file.%Greater [template = file.%Greater]
  133. // CHECK:STDOUT: %.loc11_24: i32 = int_literal 1 [template = constants.%.4]
  134. // CHECK:STDOUT: %.loc11_27: i32 = int_literal 0 [template = constants.%.7]
  135. // CHECK:STDOUT: %.loc11_23.1: init bool = call %Greater.ref.loc11(%.loc11_24, %.loc11_27) [template = constants.%.8]
  136. // CHECK:STDOUT: %.loc11_13.1: bool = value_of_initializer %.loc11_23.1 [template = constants.%.8]
  137. // CHECK:STDOUT: %.loc11_23.2: bool = converted %.loc11_23.1, %.loc11_13.1 [template = constants.%.8]
  138. // CHECK:STDOUT: if %.loc11_23.2 br !if.expr.then.loc11 else br !if.expr.else.loc11
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: !if.expr.then.loc11:
  141. // CHECK:STDOUT: %True.ref.loc11: type = name_ref True, file.%True.decl [template = constants.%True]
  142. // CHECK:STDOUT: br !if.expr.result.loc11(%True.ref.loc11)
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: !if.expr.else.loc11:
  145. // CHECK:STDOUT: %False.ref.loc11: type = name_ref False, file.%False.decl [template = constants.%False]
  146. // CHECK:STDOUT: br !if.expr.result.loc11(%False.ref.loc11)
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: !if.expr.result.loc11:
  149. // CHECK:STDOUT: %.loc11_13.2: type = block_arg !if.expr.result.loc11 [template = constants.%True]
  150. // CHECK:STDOUT: %false_.ref.loc12: False = name_ref false_, %false_
  151. // CHECK:STDOUT: %Greater.ref.loc12: <function> = name_ref Greater, file.%Greater [template = file.%Greater]
  152. // CHECK:STDOUT: %Negate.ref.loc12: <function> = name_ref Negate, file.%Negate [template = file.%Negate]
  153. // CHECK:STDOUT: %.loc12_32: i32 = int_literal 1 [template = constants.%.4]
  154. // CHECK:STDOUT: %.loc12_31.1: init i32 = call %Negate.ref.loc12(%.loc12_32) [template = constants.%.9]
  155. // CHECK:STDOUT: %.loc12_36: i32 = int_literal 0 [template = constants.%.7]
  156. // CHECK:STDOUT: %.loc12_24.1: i32 = value_of_initializer %.loc12_31.1 [template = constants.%.9]
  157. // CHECK:STDOUT: %.loc12_31.2: i32 = converted %.loc12_31.1, %.loc12_24.1 [template = constants.%.9]
  158. // CHECK:STDOUT: %.loc12_24.2: init bool = call %Greater.ref.loc12(%.loc12_31.2, %.loc12_36) [template = constants.%.6]
  159. // CHECK:STDOUT: %.loc12_14.1: bool = value_of_initializer %.loc12_24.2 [template = constants.%.6]
  160. // CHECK:STDOUT: %.loc12_24.3: bool = converted %.loc12_24.2, %.loc12_14.1 [template = constants.%.6]
  161. // CHECK:STDOUT: if %.loc12_24.3 br !if.expr.then.loc12 else br !if.expr.else.loc12
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: !if.expr.then.loc12:
  164. // CHECK:STDOUT: %True.ref.loc12: type = name_ref True, file.%True.decl [template = constants.%True]
  165. // CHECK:STDOUT: br !if.expr.result.loc12(%True.ref.loc12)
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: !if.expr.else.loc12:
  168. // CHECK:STDOUT: %False.ref.loc12: type = name_ref False, file.%False.decl [template = constants.%False]
  169. // CHECK:STDOUT: br !if.expr.result.loc12(%False.ref.loc12)
  170. // CHECK:STDOUT:
  171. // CHECK:STDOUT: !if.expr.result.loc12:
  172. // CHECK:STDOUT: %.loc12_14.2: type = block_arg !if.expr.result.loc12 [template = constants.%False]
  173. // CHECK:STDOUT: %true_.ref.loc13: True = name_ref true_, %true_
  174. // CHECK:STDOUT: %Greater.ref.loc13: <function> = name_ref Greater, file.%Greater [template = file.%Greater]
  175. // CHECK:STDOUT: %.loc13_24: i32 = int_literal 0 [template = constants.%.7]
  176. // CHECK:STDOUT: %Negate.ref.loc13: <function> = name_ref Negate, file.%Negate [template = file.%Negate]
  177. // CHECK:STDOUT: %.loc13_34: i32 = int_literal 1 [template = constants.%.4]
  178. // CHECK:STDOUT: %.loc13_33.1: init i32 = call %Negate.ref.loc13(%.loc13_34) [template = constants.%.9]
  179. // CHECK:STDOUT: %.loc13_23.1: i32 = value_of_initializer %.loc13_33.1 [template = constants.%.9]
  180. // CHECK:STDOUT: %.loc13_33.2: i32 = converted %.loc13_33.1, %.loc13_23.1 [template = constants.%.9]
  181. // CHECK:STDOUT: %.loc13_23.2: init bool = call %Greater.ref.loc13(%.loc13_24, %.loc13_33.2) [template = constants.%.8]
  182. // CHECK:STDOUT: %.loc13_13.1: bool = value_of_initializer %.loc13_23.2 [template = constants.%.8]
  183. // CHECK:STDOUT: %.loc13_23.3: bool = converted %.loc13_23.2, %.loc13_13.1 [template = constants.%.8]
  184. // CHECK:STDOUT: if %.loc13_23.3 br !if.expr.then.loc13 else br !if.expr.else.loc13
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: !if.expr.then.loc13:
  187. // CHECK:STDOUT: %True.ref.loc13: type = name_ref True, file.%True.decl [template = constants.%True]
  188. // CHECK:STDOUT: br !if.expr.result.loc13(%True.ref.loc13)
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: !if.expr.else.loc13:
  191. // CHECK:STDOUT: %False.ref.loc13: type = name_ref False, file.%False.decl [template = constants.%False]
  192. // CHECK:STDOUT: br !if.expr.result.loc13(%False.ref.loc13)
  193. // CHECK:STDOUT:
  194. // CHECK:STDOUT: !if.expr.result.loc13:
  195. // CHECK:STDOUT: %.loc13_13.2: type = block_arg !if.expr.result.loc13 [template = constants.%True]
  196. // CHECK:STDOUT: return
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: fn @RuntimeCall(%a: i32, %b: i32) -> bool {
  200. // CHECK:STDOUT: !entry:
  201. // CHECK:STDOUT: %Greater.ref: <function> = name_ref Greater, file.%Greater [template = file.%Greater]
  202. // CHECK:STDOUT: %a.ref: i32 = name_ref a, %a
  203. // CHECK:STDOUT: %b.ref: i32 = name_ref b, %b
  204. // CHECK:STDOUT: %.loc17_17.1: init bool = call %Greater.ref(%a.ref, %b.ref)
  205. // CHECK:STDOUT: %.loc17_23: bool = value_of_initializer %.loc17_17.1
  206. // CHECK:STDOUT: %.loc17_17.2: bool = converted %.loc17_17.1, %.loc17_23
  207. // CHECK:STDOUT: return %.loc17_17.2
  208. // CHECK:STDOUT: }
  209. // CHECK:STDOUT: