init_as.carbon 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/class/init_as.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/init_as.carbon
  10. class Class {
  11. var a: i32;
  12. var b: i32;
  13. }
  14. fn F() -> i32 {
  15. return ({.a = 1, .b = 2} as Class).a;
  16. }
  17. // CHECK:STDOUT: --- init_as.carbon
  18. // CHECK:STDOUT:
  19. // CHECK:STDOUT: constants {
  20. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  21. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  22. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  23. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  24. // CHECK:STDOUT: %.2: type = unbound_element_type %Class, i32 [template]
  25. // CHECK:STDOUT: %.3: type = struct_type {.a: i32, .b: i32} [template]
  26. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  27. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  28. // CHECK:STDOUT: %.4: i32 = int_literal 1 [template]
  29. // CHECK:STDOUT: %.5: i32 = int_literal 2 [template]
  30. // CHECK:STDOUT: %.6: type = ptr_type %.3 [template]
  31. // CHECK:STDOUT: %struct: %Class = struct_value (%.4, %.5) [template]
  32. // CHECK:STDOUT: }
  33. // CHECK:STDOUT:
  34. // CHECK:STDOUT: file {
  35. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  36. // CHECK:STDOUT: .Core = %Core
  37. // CHECK:STDOUT: .Class = %Class.decl
  38. // CHECK:STDOUT: .F = %F.decl
  39. // CHECK:STDOUT: }
  40. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  41. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {}
  42. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  43. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  44. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  45. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  46. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  47. // CHECK:STDOUT: %.loc16_11.1: type = value_of_initializer %int.make_type_32 [template = i32]
  48. // CHECK:STDOUT: %.loc16_11.2: type = converted %int.make_type_32, %.loc16_11.1 [template = i32]
  49. // CHECK:STDOUT: @F.%return: ref i32 = var <return slot>
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: class @Class {
  54. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  55. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  56. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32.loc12, %.loc12_10.1 [template = i32]
  57. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl a, element0 [template]
  58. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  59. // CHECK:STDOUT: %.loc13_10.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  60. // CHECK:STDOUT: %.loc13_10.2: type = converted %int.make_type_32.loc13, %.loc13_10.1 [template = i32]
  61. // CHECK:STDOUT: %.loc13_8: %.2 = field_decl b, element1 [template]
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: !members:
  64. // CHECK:STDOUT: .Self = constants.%Class
  65. // CHECK:STDOUT: .a = %.loc12_8
  66. // CHECK:STDOUT: .b = %.loc13_8
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: fn @F() -> i32 {
  72. // CHECK:STDOUT: !entry:
  73. // CHECK:STDOUT: %.loc17_17: i32 = int_literal 1 [template = constants.%.4]
  74. // CHECK:STDOUT: %.loc17_25: i32 = int_literal 2 [template = constants.%.5]
  75. // CHECK:STDOUT: %.loc17_26.1: %.3 = struct_literal (%.loc17_17, %.loc17_25)
  76. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  77. // CHECK:STDOUT: %.loc17_26.2: ref %Class = temporary_storage
  78. // CHECK:STDOUT: %.loc17_26.3: ref i32 = class_element_access %.loc17_26.2, element0
  79. // CHECK:STDOUT: %.loc17_26.4: init i32 = initialize_from %.loc17_17 to %.loc17_26.3 [template = constants.%.4]
  80. // CHECK:STDOUT: %.loc17_26.5: ref i32 = class_element_access %.loc17_26.2, element1
  81. // CHECK:STDOUT: %.loc17_26.6: init i32 = initialize_from %.loc17_25 to %.loc17_26.5 [template = constants.%.5]
  82. // CHECK:STDOUT: %.loc17_26.7: init %Class = class_init (%.loc17_26.4, %.loc17_26.6), %.loc17_26.2 [template = constants.%struct]
  83. // CHECK:STDOUT: %.loc17_26.8: ref %Class = temporary %.loc17_26.2, %.loc17_26.7
  84. // CHECK:STDOUT: %.loc17_28: ref %Class = converted %.loc17_26.1, %.loc17_26.8
  85. // CHECK:STDOUT: %a.ref: %.2 = name_ref a, @Class.%.loc12_8 [template = @Class.%.loc12_8]
  86. // CHECK:STDOUT: %.loc17_37.1: ref i32 = class_element_access %.loc17_28, element0
  87. // CHECK:STDOUT: %.loc17_37.2: i32 = bind_value %.loc17_37.1
  88. // CHECK:STDOUT: return %.loc17_37.2
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT: