struct.carbon 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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: %.1: Core.IntLiteral = int_value 32 [template]
  19. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  20. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  21. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  22. // CHECK:STDOUT: %i32: type = int_type signed, %.1 [template]
  23. // CHECK:STDOUT: %.2: type = struct_type {.a: %i32, .b: %i32} [template]
  24. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  25. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  26. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [template]
  27. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [template]
  28. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  29. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  30. // CHECK:STDOUT: %.4: Core.IntLiteral = int_value 1 [template]
  31. // CHECK:STDOUT: %.5: Core.IntLiteral = int_value 2 [template]
  32. // CHECK:STDOUT: %.6: type = struct_type {.a: Core.IntLiteral, .b: Core.IntLiteral} [template]
  33. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  34. // CHECK:STDOUT: %Convert.type.14: type = fn_type @Convert.2, @impl.1(%.1) [template]
  35. // CHECK:STDOUT: %Convert.14: %Convert.type.14 = struct_value () [template]
  36. // CHECK:STDOUT: %.30: <witness> = interface_witness (%Convert.14) [template]
  37. // CHECK:STDOUT: %.31: <bound method> = bound_method %.4, %Convert.14 [template]
  38. // CHECK:STDOUT: %.32: <specific function> = specific_function %.31, @Convert.2(%.1) [template]
  39. // CHECK:STDOUT: %.33: %i32 = int_value 1 [template]
  40. // CHECK:STDOUT: %.34: <bound method> = bound_method %.5, %Convert.14 [template]
  41. // CHECK:STDOUT: %.35: <specific function> = specific_function %.34, @Convert.2(%.1) [template]
  42. // CHECK:STDOUT: %.36: %i32 = int_value 2 [template]
  43. // CHECK:STDOUT: %struct: %.2 = struct_value (%.33, %.36) [template]
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT:
  46. // CHECK:STDOUT: imports {
  47. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  48. // CHECK:STDOUT: .Int = %import_ref.1
  49. // CHECK:STDOUT: .Bool = %import_ref.2
  50. // CHECK:STDOUT: .ImplicitAs = %import_ref.3
  51. // CHECK:STDOUT: import Core//prelude
  52. // CHECK:STDOUT: import Core//prelude/...
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: file {
  57. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  58. // CHECK:STDOUT: .Core = imports.%Core
  59. // CHECK:STDOUT: .G = %G.decl
  60. // CHECK:STDOUT: .F = %F.decl
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: %Core.import = import Core
  63. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  64. // CHECK:STDOUT: %s.patt: %.2 = binding_pattern s
  65. // CHECK:STDOUT: %s.param_patt: %.2 = value_param_pattern %s.patt, runtime_param0
  66. // CHECK:STDOUT: } {
  67. // CHECK:STDOUT: %.loc11_14.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  68. // CHECK:STDOUT: %int.make_type_signed.loc11_14: init type = call constants.%Int(%.loc11_14.1) [template = constants.%i32]
  69. // CHECK:STDOUT: %.loc11_14.2: type = value_of_initializer %int.make_type_signed.loc11_14 [template = constants.%i32]
  70. // CHECK:STDOUT: %.loc11_14.3: type = converted %int.make_type_signed.loc11_14, %.loc11_14.2 [template = constants.%i32]
  71. // CHECK:STDOUT: %.loc11_23.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  72. // CHECK:STDOUT: %int.make_type_signed.loc11_23: init type = call constants.%Int(%.loc11_23.1) [template = constants.%i32]
  73. // CHECK:STDOUT: %.loc11_23.2: type = value_of_initializer %int.make_type_signed.loc11_23 [template = constants.%i32]
  74. // CHECK:STDOUT: %.loc11_23.3: type = converted %int.make_type_signed.loc11_23, %.loc11_23.2 [template = constants.%i32]
  75. // CHECK:STDOUT: %.loc11_26: type = struct_type {.a: %i32, .b: %i32} [template = constants.%.2]
  76. // CHECK:STDOUT: %s.param: %.2 = value_param runtime_param0
  77. // CHECK:STDOUT: %s: %.2 = bind_name s, %s.param
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  80. // CHECK:STDOUT: %cond.patt: bool = binding_pattern cond
  81. // CHECK:STDOUT: %cond.param_patt: bool = value_param_pattern %cond.patt, runtime_param0
  82. // CHECK:STDOUT: } {
  83. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  84. // CHECK:STDOUT: %.loc13_12.1: type = value_of_initializer %bool.make_type [template = bool]
  85. // CHECK:STDOUT: %.loc13_12.2: type = converted %bool.make_type, %.loc13_12.1 [template = bool]
  86. // CHECK:STDOUT: %cond.param: bool = value_param runtime_param0
  87. // CHECK:STDOUT: %cond: bool = bind_name cond, %cond.param
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: fn @G(%s.param_patt: %.2);
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: fn @F(%cond.param_patt: bool) {
  94. // CHECK:STDOUT: !entry:
  95. // CHECK:STDOUT: %.loc14_15.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  96. // CHECK:STDOUT: %int.make_type_signed.loc14_15: init type = call constants.%Int(%.loc14_15.1) [template = constants.%i32]
  97. // CHECK:STDOUT: %.loc14_15.2: type = value_of_initializer %int.make_type_signed.loc14_15 [template = constants.%i32]
  98. // CHECK:STDOUT: %.loc14_15.3: type = converted %int.make_type_signed.loc14_15, %.loc14_15.2 [template = constants.%i32]
  99. // CHECK:STDOUT: %.loc14_24.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  100. // CHECK:STDOUT: %int.make_type_signed.loc14_24: init type = call constants.%Int(%.loc14_24.1) [template = constants.%i32]
  101. // CHECK:STDOUT: %.loc14_24.2: type = value_of_initializer %int.make_type_signed.loc14_24 [template = constants.%i32]
  102. // CHECK:STDOUT: %.loc14_24.3: type = converted %int.make_type_signed.loc14_24, %.loc14_24.2 [template = constants.%i32]
  103. // CHECK:STDOUT: %.loc14_27: type = struct_type {.a: %i32, .b: %i32} [template = constants.%.2]
  104. // CHECK:STDOUT: %a.var: ref %.2 = var a
  105. // CHECK:STDOUT: %a: ref %.2 = bind_name a, %a.var
  106. // CHECK:STDOUT: %.loc14_37: Core.IntLiteral = int_value 1 [template = constants.%.4]
  107. // CHECK:STDOUT: %.loc14_45: Core.IntLiteral = int_value 2 [template = constants.%.5]
  108. // CHECK:STDOUT: %.loc14_46.1: %.6 = struct_literal (%.loc14_37, %.loc14_45)
  109. // CHECK:STDOUT: %.loc14_46.2: %Convert.type.2 = interface_witness_access constants.%.30, element0 [template = constants.%Convert.14]
  110. // CHECK:STDOUT: %.loc14_46.3: <bound method> = bound_method %.loc14_37, %.loc14_46.2 [template = constants.%.31]
  111. // CHECK:STDOUT: %.loc14_46.4: <specific function> = specific_function %.loc14_46.3, @Convert.2(constants.%.1) [template = constants.%.32]
  112. // CHECK:STDOUT: %int.convert_checked.loc14_46.1: init %i32 = call %.loc14_46.4(%.loc14_37) [template = constants.%.33]
  113. // CHECK:STDOUT: %.loc14_46.5: init %i32 = converted %.loc14_37, %int.convert_checked.loc14_46.1 [template = constants.%.33]
  114. // CHECK:STDOUT: %.loc14_46.6: ref %i32 = struct_access %a.var, element0
  115. // CHECK:STDOUT: %.loc14_46.7: init %i32 = initialize_from %.loc14_46.5 to %.loc14_46.6 [template = constants.%.33]
  116. // CHECK:STDOUT: %.loc14_46.8: %Convert.type.2 = interface_witness_access constants.%.30, element0 [template = constants.%Convert.14]
  117. // CHECK:STDOUT: %.loc14_46.9: <bound method> = bound_method %.loc14_45, %.loc14_46.8 [template = constants.%.34]
  118. // CHECK:STDOUT: %.loc14_46.10: <specific function> = specific_function %.loc14_46.9, @Convert.2(constants.%.1) [template = constants.%.35]
  119. // CHECK:STDOUT: %int.convert_checked.loc14_46.2: init %i32 = call %.loc14_46.10(%.loc14_45) [template = constants.%.36]
  120. // CHECK:STDOUT: %.loc14_46.11: init %i32 = converted %.loc14_45, %int.convert_checked.loc14_46.2 [template = constants.%.36]
  121. // CHECK:STDOUT: %.loc14_46.12: ref %i32 = struct_access %a.var, element1
  122. // CHECK:STDOUT: %.loc14_46.13: init %i32 = initialize_from %.loc14_46.11 to %.loc14_46.12 [template = constants.%.36]
  123. // CHECK:STDOUT: %.loc14_46.14: init %.2 = struct_init (%.loc14_46.7, %.loc14_46.13) to %a.var [template = constants.%struct]
  124. // CHECK:STDOUT: %.loc14_47: init %.2 = converted %.loc14_46.1, %.loc14_46.14 [template = constants.%struct]
  125. // CHECK:STDOUT: assign %a.var, %.loc14_47
  126. // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [template = constants.%G]
  127. // CHECK:STDOUT: %cond.ref: bool = name_ref cond, %cond
  128. // CHECK:STDOUT: if %cond.ref br !if.expr.then else br !if.expr.else
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: !if.expr.then:
  131. // CHECK:STDOUT: %a.ref.loc15_18: ref %.2 = name_ref a, %a
  132. // CHECK:STDOUT: %.loc15_18.1: ref %i32 = struct_access %a.ref.loc15_18, element0
  133. // CHECK:STDOUT: %.loc15_18.2: %i32 = bind_value %.loc15_18.1
  134. // CHECK:STDOUT: %.loc15_18.3: ref %i32 = struct_access %a.ref.loc15_18, element1
  135. // CHECK:STDOUT: %.loc15_18.4: %i32 = bind_value %.loc15_18.3
  136. // CHECK:STDOUT: %struct.loc15_18: %.2 = struct_value (%.loc15_18.2, %.loc15_18.4)
  137. // CHECK:STDOUT: %.loc15_18.5: %.2 = converted %a.ref.loc15_18, %struct.loc15_18
  138. // CHECK:STDOUT: br !if.expr.result(%.loc15_18.5)
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: !if.expr.else:
  141. // CHECK:STDOUT: %a.ref.loc15_25: ref %.2 = name_ref a, %a
  142. // CHECK:STDOUT: %.loc15_25.1: ref %i32 = struct_access %a.ref.loc15_25, element0
  143. // CHECK:STDOUT: %.loc15_25.2: %i32 = bind_value %.loc15_25.1
  144. // CHECK:STDOUT: %.loc15_25.3: ref %i32 = struct_access %a.ref.loc15_25, element1
  145. // CHECK:STDOUT: %.loc15_25.4: %i32 = bind_value %.loc15_25.3
  146. // CHECK:STDOUT: %struct.loc15_25: %.2 = struct_value (%.loc15_25.2, %.loc15_25.4)
  147. // CHECK:STDOUT: %.loc15_20: %.2 = converted %a.ref.loc15_25, %struct.loc15_25
  148. // CHECK:STDOUT: br !if.expr.result(%.loc15_20)
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: !if.expr.result:
  151. // CHECK:STDOUT: %.loc15_5: %.2 = block_arg !if.expr.result
  152. // CHECK:STDOUT: %G.call: init %empty_tuple.type = call %G.ref(%.loc15_5)
  153. // CHECK:STDOUT: return
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT: