struct.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/struct.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/if_expr/struct.carbon
  10. fn G(s: {.a: i32, .b: i32});
  11. fn F(cond: bool) {
  12. var a: {.a: i32, .b: i32} = {.a = 1, .b = 2};
  13. G(if cond then a else a);
  14. }
  15. // CHECK:STDOUT: --- struct.carbon
  16. // CHECK:STDOUT:
  17. // CHECK:STDOUT: constants {
  18. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  19. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  20. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  21. // CHECK:STDOUT: %struct_type.a.b.501: type = struct_type {.a: %i32, .b: %i32} [template]
  22. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  23. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  24. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [template]
  25. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [template]
  26. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  27. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  28. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [template]
  29. // CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [template]
  30. // CHECK:STDOUT: %struct_type.a.b.cfd: type = struct_type {.a: Core.IntLiteral, .b: Core.IntLiteral} [template]
  31. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [template]
  32. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  33. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.1(%int_32) [template]
  34. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  35. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [template]
  36. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, %impl_witness.d39 [template]
  37. // CHECK:STDOUT: %.a0b: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [template]
  38. // CHECK:STDOUT: %Convert.bound.ab5: <bound method> = bound_method %int_1.5b8, %Convert.956 [template]
  39. // CHECK:STDOUT: %Convert.specific_fn.70c: <specific function> = specific_function %Convert.bound.ab5, @Convert.2(%int_32) [template]
  40. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [template]
  41. // CHECK:STDOUT: %Convert.bound.ef9: <bound method> = bound_method %int_2.ecc, %Convert.956 [template]
  42. // CHECK:STDOUT: %Convert.specific_fn.787: <specific function> = specific_function %Convert.bound.ef9, @Convert.2(%int_32) [template]
  43. // CHECK:STDOUT: %int_2.ef8: %i32 = int_value 2 [template]
  44. // CHECK:STDOUT: %struct: %struct_type.a.b.501 = struct_value (%int_1.5d2, %int_2.ef8) [template]
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT:
  47. // CHECK:STDOUT: imports {
  48. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  49. // CHECK:STDOUT: .Int = %Core.Int
  50. // CHECK:STDOUT: .Bool = %Core.Bool
  51. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  52. // CHECK:STDOUT: import Core//prelude
  53. // CHECK:STDOUT: import Core//prelude/...
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT:
  57. // CHECK:STDOUT: file {
  58. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  59. // CHECK:STDOUT: .Core = imports.%Core
  60. // CHECK:STDOUT: .G = %G.decl
  61. // CHECK:STDOUT: .F = %F.decl
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: %Core.import = import Core
  64. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  65. // CHECK:STDOUT: %s.patt: %struct_type.a.b.501 = binding_pattern s
  66. // CHECK:STDOUT: %s.param_patt: %struct_type.a.b.501 = value_param_pattern %s.patt, runtime_param0
  67. // CHECK:STDOUT: } {
  68. // CHECK:STDOUT: %s.param: %struct_type.a.b.501 = value_param runtime_param0
  69. // CHECK:STDOUT: %.loc11: type = splice_block %struct_type.a.b [template = constants.%struct_type.a.b.501] {
  70. // CHECK:STDOUT: %int_32.loc11_14: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  71. // CHECK:STDOUT: %i32.loc11_14: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  72. // CHECK:STDOUT: %int_32.loc11_23: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  73. // CHECK:STDOUT: %i32.loc11_23: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  74. // CHECK:STDOUT: %struct_type.a.b: type = struct_type {.a: %i32, .b: %i32} [template = constants.%struct_type.a.b.501]
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT: %s: %struct_type.a.b.501 = bind_name s, %s.param
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  79. // CHECK:STDOUT: %cond.patt: bool = binding_pattern cond
  80. // CHECK:STDOUT: %cond.param_patt: bool = value_param_pattern %cond.patt, runtime_param0
  81. // CHECK:STDOUT: } {
  82. // CHECK:STDOUT: %cond.param: bool = value_param runtime_param0
  83. // CHECK:STDOUT: %.loc13_12.1: type = splice_block %.loc13_12.3 [template = bool] {
  84. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  85. // CHECK:STDOUT: %.loc13_12.2: type = value_of_initializer %bool.make_type [template = bool]
  86. // CHECK:STDOUT: %.loc13_12.3: type = converted %bool.make_type, %.loc13_12.2 [template = bool]
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT: %cond: bool = bind_name cond, %cond.param
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: fn @G(%s.param_patt: %struct_type.a.b.501);
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: fn @F(%cond.param_patt: bool) {
  95. // CHECK:STDOUT: !entry:
  96. // CHECK:STDOUT: name_binding_decl {
  97. // CHECK:STDOUT: %a.patt: %struct_type.a.b.501 = binding_pattern a
  98. // CHECK:STDOUT: %.loc14_3.1: %struct_type.a.b.501 = var_pattern %a.patt
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT: %a.var: ref %struct_type.a.b.501 = var a
  101. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  102. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template = constants.%int_2.ecc]
  103. // CHECK:STDOUT: %.loc14_46.1: %struct_type.a.b.cfd = struct_literal (%int_1, %int_2)
  104. // CHECK:STDOUT: %impl.elem0.loc14_46.1: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  105. // CHECK:STDOUT: %bound_method.loc14_46.1: <bound method> = bound_method %int_1, %impl.elem0.loc14_46.1 [template = constants.%Convert.bound.ab5]
  106. // CHECK:STDOUT: %specific_fn.loc14_46.1: <specific function> = specific_function %bound_method.loc14_46.1, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.70c]
  107. // CHECK:STDOUT: %int.convert_checked.loc14_46.1: init %i32 = call %specific_fn.loc14_46.1(%int_1) [template = constants.%int_1.5d2]
  108. // CHECK:STDOUT: %.loc14_46.2: init %i32 = converted %int_1, %int.convert_checked.loc14_46.1 [template = constants.%int_1.5d2]
  109. // CHECK:STDOUT: %.loc14_46.3: ref %i32 = struct_access %a.var, element0
  110. // CHECK:STDOUT: %.loc14_46.4: init %i32 = initialize_from %.loc14_46.2 to %.loc14_46.3 [template = constants.%int_1.5d2]
  111. // CHECK:STDOUT: %impl.elem0.loc14_46.2: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  112. // CHECK:STDOUT: %bound_method.loc14_46.2: <bound method> = bound_method %int_2, %impl.elem0.loc14_46.2 [template = constants.%Convert.bound.ef9]
  113. // CHECK:STDOUT: %specific_fn.loc14_46.2: <specific function> = specific_function %bound_method.loc14_46.2, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.787]
  114. // CHECK:STDOUT: %int.convert_checked.loc14_46.2: init %i32 = call %specific_fn.loc14_46.2(%int_2) [template = constants.%int_2.ef8]
  115. // CHECK:STDOUT: %.loc14_46.5: init %i32 = converted %int_2, %int.convert_checked.loc14_46.2 [template = constants.%int_2.ef8]
  116. // CHECK:STDOUT: %.loc14_46.6: ref %i32 = struct_access %a.var, element1
  117. // CHECK:STDOUT: %.loc14_46.7: init %i32 = initialize_from %.loc14_46.5 to %.loc14_46.6 [template = constants.%int_2.ef8]
  118. // CHECK:STDOUT: %.loc14_46.8: init %struct_type.a.b.501 = struct_init (%.loc14_46.4, %.loc14_46.7) to %a.var [template = constants.%struct]
  119. // CHECK:STDOUT: %.loc14_3.2: init %struct_type.a.b.501 = converted %.loc14_46.1, %.loc14_46.8 [template = constants.%struct]
  120. // CHECK:STDOUT: assign %a.var, %.loc14_3.2
  121. // CHECK:STDOUT: %.loc14_27: type = splice_block %struct_type.a.b [template = constants.%struct_type.a.b.501] {
  122. // CHECK:STDOUT: %int_32.loc14_15: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  123. // CHECK:STDOUT: %i32.loc14_15: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  124. // CHECK:STDOUT: %int_32.loc14_24: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  125. // CHECK:STDOUT: %i32.loc14_24: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  126. // CHECK:STDOUT: %struct_type.a.b: type = struct_type {.a: %i32, .b: %i32} [template = constants.%struct_type.a.b.501]
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT: %a: ref %struct_type.a.b.501 = bind_name a, %a.var
  129. // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [template = constants.%G]
  130. // CHECK:STDOUT: %cond.ref: bool = name_ref cond, %cond
  131. // CHECK:STDOUT: if %cond.ref br !if.expr.then else br !if.expr.else
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: !if.expr.then:
  134. // CHECK:STDOUT: %a.ref.loc15_18: ref %struct_type.a.b.501 = name_ref a, %a
  135. // CHECK:STDOUT: %.loc15_18.1: ref %i32 = struct_access %a.ref.loc15_18, element0
  136. // CHECK:STDOUT: %.loc15_18.2: %i32 = bind_value %.loc15_18.1
  137. // CHECK:STDOUT: %.loc15_18.3: ref %i32 = struct_access %a.ref.loc15_18, element1
  138. // CHECK:STDOUT: %.loc15_18.4: %i32 = bind_value %.loc15_18.3
  139. // CHECK:STDOUT: %struct.loc15_18: %struct_type.a.b.501 = struct_value (%.loc15_18.2, %.loc15_18.4)
  140. // CHECK:STDOUT: %.loc15_18.5: %struct_type.a.b.501 = converted %a.ref.loc15_18, %struct.loc15_18
  141. // CHECK:STDOUT: br !if.expr.result(%.loc15_18.5)
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: !if.expr.else:
  144. // CHECK:STDOUT: %a.ref.loc15_25: ref %struct_type.a.b.501 = name_ref a, %a
  145. // CHECK:STDOUT: %.loc15_25.1: ref %i32 = struct_access %a.ref.loc15_25, element0
  146. // CHECK:STDOUT: %.loc15_25.2: %i32 = bind_value %.loc15_25.1
  147. // CHECK:STDOUT: %.loc15_25.3: ref %i32 = struct_access %a.ref.loc15_25, element1
  148. // CHECK:STDOUT: %.loc15_25.4: %i32 = bind_value %.loc15_25.3
  149. // CHECK:STDOUT: %struct.loc15_25: %struct_type.a.b.501 = struct_value (%.loc15_25.2, %.loc15_25.4)
  150. // CHECK:STDOUT: %.loc15_20: %struct_type.a.b.501 = converted %a.ref.loc15_25, %struct.loc15_25
  151. // CHECK:STDOUT: br !if.expr.result(%.loc15_20)
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: !if.expr.result:
  154. // CHECK:STDOUT: %.loc15_5: %struct_type.a.b.501 = block_arg !if.expr.result
  155. // CHECK:STDOUT: %G.call: init %empty_tuple.type = call %G.ref(%.loc15_5)
  156. // CHECK:STDOUT: return
  157. // CHECK:STDOUT: }
  158. // CHECK:STDOUT: