struct.carbon 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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: %Int32.type: type = fn_type @Int32 [template]
  19. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  20. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  21. // CHECK:STDOUT: %.2: 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: %.3: type = ptr_type %.2 [template]
  29. // CHECK:STDOUT: %.4: i32 = int_literal 1 [template]
  30. // CHECK:STDOUT: %.5: i32 = int_literal 2 [template]
  31. // CHECK:STDOUT: %struct: %.2 = struct_value (%.4, %.5) [template]
  32. // CHECK:STDOUT: }
  33. // CHECK:STDOUT:
  34. // CHECK:STDOUT: imports {
  35. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  36. // CHECK:STDOUT: .Int32 = %import_ref.1
  37. // CHECK:STDOUT: .Bool = %import_ref.2
  38. // CHECK:STDOUT: import Core//prelude
  39. // CHECK:STDOUT: import Core//prelude/operators
  40. // CHECK:STDOUT: import Core//prelude/types
  41. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  42. // CHECK:STDOUT: import Core//prelude/operators/as
  43. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  44. // CHECK:STDOUT: import Core//prelude/operators/comparison
  45. // CHECK:STDOUT: import Core//prelude/types/bool
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  48. // CHECK:STDOUT: %import_ref.2: %Bool.type = import_ref Core//prelude/types/bool, inst+2, loaded [template = constants.%Bool]
  49. // CHECK:STDOUT: }
  50. // CHECK:STDOUT:
  51. // CHECK:STDOUT: file {
  52. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  53. // CHECK:STDOUT: .Core = imports.%Core
  54. // CHECK:STDOUT: .G = %G.decl
  55. // CHECK:STDOUT: .F = %F.decl
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT: %Core.import = import Core
  58. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  59. // CHECK:STDOUT: %s.patt: %.2 = binding_pattern s
  60. // CHECK:STDOUT: %s.param_patt: %.2 = param_pattern %s.patt, runtime_param0
  61. // CHECK:STDOUT: } {
  62. // CHECK:STDOUT: %int.make_type_32.loc11_14: init type = call constants.%Int32() [template = i32]
  63. // CHECK:STDOUT: %.loc11_14.1: type = value_of_initializer %int.make_type_32.loc11_14 [template = i32]
  64. // CHECK:STDOUT: %.loc11_14.2: type = converted %int.make_type_32.loc11_14, %.loc11_14.1 [template = i32]
  65. // CHECK:STDOUT: %int.make_type_32.loc11_23: init type = call constants.%Int32() [template = i32]
  66. // CHECK:STDOUT: %.loc11_23.1: type = value_of_initializer %int.make_type_32.loc11_23 [template = i32]
  67. // CHECK:STDOUT: %.loc11_23.2: type = converted %int.make_type_32.loc11_23, %.loc11_23.1 [template = i32]
  68. // CHECK:STDOUT: %.loc11_26: type = struct_type {.a: i32, .b: i32} [template = constants.%.2]
  69. // CHECK:STDOUT: %param: %.2 = param runtime_param0
  70. // CHECK:STDOUT: %s: %.2 = bind_name s, %param
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  73. // CHECK:STDOUT: %cond.patt: bool = binding_pattern cond
  74. // CHECK:STDOUT: %cond.param_patt: bool = param_pattern %cond.patt, runtime_param0
  75. // CHECK:STDOUT: } {
  76. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  77. // CHECK:STDOUT: %.loc13_12.1: type = value_of_initializer %bool.make_type [template = bool]
  78. // CHECK:STDOUT: %.loc13_12.2: type = converted %bool.make_type, %.loc13_12.1 [template = bool]
  79. // CHECK:STDOUT: %param: bool = param runtime_param0
  80. // CHECK:STDOUT: %cond: bool = bind_name cond, %param
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: fn @G(%s.param_patt: %.2);
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: fn @Bool() -> type = "bool.make_type";
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: fn @F(%cond.param_patt: bool) {
  91. // CHECK:STDOUT: !entry:
  92. // CHECK:STDOUT: %int.make_type_32.loc14_15: init type = call constants.%Int32() [template = i32]
  93. // CHECK:STDOUT: %.loc14_15.1: type = value_of_initializer %int.make_type_32.loc14_15 [template = i32]
  94. // CHECK:STDOUT: %.loc14_15.2: type = converted %int.make_type_32.loc14_15, %.loc14_15.1 [template = i32]
  95. // CHECK:STDOUT: %int.make_type_32.loc14_24: init type = call constants.%Int32() [template = i32]
  96. // CHECK:STDOUT: %.loc14_24.1: type = value_of_initializer %int.make_type_32.loc14_24 [template = i32]
  97. // CHECK:STDOUT: %.loc14_24.2: type = converted %int.make_type_32.loc14_24, %.loc14_24.1 [template = i32]
  98. // CHECK:STDOUT: %.loc14_27: type = struct_type {.a: i32, .b: i32} [template = constants.%.2]
  99. // CHECK:STDOUT: %a.var: ref %.2 = var a
  100. // CHECK:STDOUT: %a: ref %.2 = bind_name a, %a.var
  101. // CHECK:STDOUT: %.loc14_37: i32 = int_literal 1 [template = constants.%.4]
  102. // CHECK:STDOUT: %.loc14_45: i32 = int_literal 2 [template = constants.%.5]
  103. // CHECK:STDOUT: %.loc14_46.1: %.2 = struct_literal (%.loc14_37, %.loc14_45)
  104. // CHECK:STDOUT: %.loc14_46.2: ref i32 = struct_access %a.var, element0
  105. // CHECK:STDOUT: %.loc14_46.3: init i32 = initialize_from %.loc14_37 to %.loc14_46.2 [template = constants.%.4]
  106. // CHECK:STDOUT: %.loc14_46.4: ref i32 = struct_access %a.var, element1
  107. // CHECK:STDOUT: %.loc14_46.5: init i32 = initialize_from %.loc14_45 to %.loc14_46.4 [template = constants.%.5]
  108. // CHECK:STDOUT: %.loc14_46.6: init %.2 = struct_init (%.loc14_46.3, %.loc14_46.5) to %a.var [template = constants.%struct]
  109. // CHECK:STDOUT: %.loc14_47: init %.2 = converted %.loc14_46.1, %.loc14_46.6 [template = constants.%struct]
  110. // CHECK:STDOUT: assign %a.var, %.loc14_47
  111. // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [template = constants.%G]
  112. // CHECK:STDOUT: %cond.ref: bool = name_ref cond, %cond
  113. // CHECK:STDOUT: if %cond.ref br !if.expr.then else br !if.expr.else
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: !if.expr.then:
  116. // CHECK:STDOUT: %a.ref.loc15_18: ref %.2 = name_ref a, %a
  117. // CHECK:STDOUT: %.loc15_18.1: ref i32 = struct_access %a.ref.loc15_18, element0
  118. // CHECK:STDOUT: %.loc15_18.2: i32 = bind_value %.loc15_18.1
  119. // CHECK:STDOUT: %.loc15_18.3: ref i32 = struct_access %a.ref.loc15_18, element1
  120. // CHECK:STDOUT: %.loc15_18.4: i32 = bind_value %.loc15_18.3
  121. // CHECK:STDOUT: %struct.loc15_18: %.2 = struct_value (%.loc15_18.2, %.loc15_18.4)
  122. // CHECK:STDOUT: %.loc15_18.5: %.2 = converted %a.ref.loc15_18, %struct.loc15_18
  123. // CHECK:STDOUT: br !if.expr.result(%.loc15_18.5)
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: !if.expr.else:
  126. // CHECK:STDOUT: %a.ref.loc15_25: ref %.2 = name_ref a, %a
  127. // CHECK:STDOUT: %.loc15_25.1: ref i32 = struct_access %a.ref.loc15_25, element0
  128. // CHECK:STDOUT: %.loc15_25.2: i32 = bind_value %.loc15_25.1
  129. // CHECK:STDOUT: %.loc15_25.3: ref i32 = struct_access %a.ref.loc15_25, element1
  130. // CHECK:STDOUT: %.loc15_25.4: i32 = bind_value %.loc15_25.3
  131. // CHECK:STDOUT: %struct.loc15_25: %.2 = struct_value (%.loc15_25.2, %.loc15_25.4)
  132. // CHECK:STDOUT: %.loc15_20: %.2 = converted %a.ref.loc15_25, %struct.loc15_25
  133. // CHECK:STDOUT: br !if.expr.result(%.loc15_20)
  134. // CHECK:STDOUT:
  135. // CHECK:STDOUT: !if.expr.result:
  136. // CHECK:STDOUT: %.loc15_5: %.2 = block_arg !if.expr.result
  137. // CHECK:STDOUT: %G.call: init %.1 = call %G.ref(%.loc15_5)
  138. // CHECK:STDOUT: return
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT: