init.carbon 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/init.carbon
  10. class Class {
  11. var n: i32;
  12. var next: Class*;
  13. }
  14. fn Make(n: i32, next: Class*) -> Class {
  15. return {.n = n, .next = next};
  16. }
  17. fn MakeReorder(n: i32, next: Class*) -> Class {
  18. return {.next = next, .n = n};
  19. }
  20. // CHECK:STDOUT: --- init.carbon
  21. // CHECK:STDOUT:
  22. // CHECK:STDOUT: constants {
  23. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  24. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  25. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  26. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  27. // CHECK:STDOUT: %.2: type = unbound_element_type %Class, i32 [template]
  28. // CHECK:STDOUT: %.3: type = ptr_type %Class [template]
  29. // CHECK:STDOUT: %.4: type = unbound_element_type %Class, %.3 [template]
  30. // CHECK:STDOUT: %.5: type = struct_type {.n: i32, .next: %.3} [template]
  31. // CHECK:STDOUT: %Make.type: type = fn_type @Make [template]
  32. // CHECK:STDOUT: %Make: %Make.type = struct_value () [template]
  33. // CHECK:STDOUT: %.6: type = ptr_type %.5 [template]
  34. // CHECK:STDOUT: %MakeReorder.type: type = fn_type @MakeReorder [template]
  35. // CHECK:STDOUT: %MakeReorder: %MakeReorder.type = struct_value () [template]
  36. // CHECK:STDOUT: %.7: type = struct_type {.next: %.3, .n: i32} [template]
  37. // CHECK:STDOUT: }
  38. // CHECK:STDOUT:
  39. // CHECK:STDOUT: file {
  40. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  41. // CHECK:STDOUT: .Core = %Core
  42. // CHECK:STDOUT: .Class = %Class.decl
  43. // CHECK:STDOUT: .Make = %Make.decl
  44. // CHECK:STDOUT: .MakeReorder = %MakeReorder.decl
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  47. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {}
  48. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  49. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  50. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [template = constants.%Make] {
  51. // CHECK:STDOUT: %int.make_type_32.loc16: init type = call constants.%Int32() [template = i32]
  52. // CHECK:STDOUT: %.loc16_12.1: type = value_of_initializer %int.make_type_32.loc16 [template = i32]
  53. // CHECK:STDOUT: %.loc16_12.2: type = converted %int.make_type_32.loc16, %.loc16_12.1 [template = i32]
  54. // CHECK:STDOUT: %n.loc16_9.1: i32 = param n
  55. // CHECK:STDOUT: @Make.%n: i32 = bind_name n, %n.loc16_9.1
  56. // CHECK:STDOUT: %Class.ref.loc16_23: type = name_ref Class, %Class.decl [template = constants.%Class]
  57. // CHECK:STDOUT: %.loc16_28: type = ptr_type %Class [template = constants.%.3]
  58. // CHECK:STDOUT: %next.loc16_17.1: %.3 = param next
  59. // CHECK:STDOUT: @Make.%next: %.3 = bind_name next, %next.loc16_17.1
  60. // CHECK:STDOUT: %Class.ref.loc16_34: type = name_ref Class, %Class.decl [template = constants.%Class]
  61. // CHECK:STDOUT: @Make.%return: ref %Class = var <return slot>
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  64. // CHECK:STDOUT: %MakeReorder.decl: %MakeReorder.type = fn_decl @MakeReorder [template = constants.%MakeReorder] {
  65. // CHECK:STDOUT: %int.make_type_32.loc20: init type = call constants.%Int32() [template = i32]
  66. // CHECK:STDOUT: %.loc20_19.1: type = value_of_initializer %int.make_type_32.loc20 [template = i32]
  67. // CHECK:STDOUT: %.loc20_19.2: type = converted %int.make_type_32.loc20, %.loc20_19.1 [template = i32]
  68. // CHECK:STDOUT: %n.loc20_16.1: i32 = param n
  69. // CHECK:STDOUT: @MakeReorder.%n: i32 = bind_name n, %n.loc20_16.1
  70. // CHECK:STDOUT: %Class.ref.loc20_30: type = name_ref Class, %Class.decl [template = constants.%Class]
  71. // CHECK:STDOUT: %.loc20_35: type = ptr_type %Class [template = constants.%.3]
  72. // CHECK:STDOUT: %next.loc20_24.1: %.3 = param next
  73. // CHECK:STDOUT: @MakeReorder.%next: %.3 = bind_name next, %next.loc20_24.1
  74. // CHECK:STDOUT: %Class.ref.loc20_41: type = name_ref Class, %Class.decl [template = constants.%Class]
  75. // CHECK:STDOUT: @MakeReorder.%return: ref %Class = var <return slot>
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: class @Class {
  80. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  81. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  82. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32, %.loc12_10.1 [template = i32]
  83. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl n, element0 [template]
  84. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  85. // CHECK:STDOUT: %.loc13_18: type = ptr_type %Class [template = constants.%.3]
  86. // CHECK:STDOUT: %.loc13_11: %.4 = field_decl next, element1 [template]
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: !members:
  89. // CHECK:STDOUT: .Self = constants.%Class
  90. // CHECK:STDOUT: .n = %.loc12_8
  91. // CHECK:STDOUT: .next = %.loc13_11
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: fn @Make(%n: i32, %next: %.3) -> %return: %Class {
  97. // CHECK:STDOUT: !entry:
  98. // CHECK:STDOUT: %n.ref: i32 = name_ref n, %n
  99. // CHECK:STDOUT: %next.ref: %.3 = name_ref next, %next
  100. // CHECK:STDOUT: %.loc17_31.1: %.5 = struct_literal (%n.ref, %next.ref)
  101. // CHECK:STDOUT: %.loc17_31.2: ref i32 = class_element_access %return, element0
  102. // CHECK:STDOUT: %.loc17_31.3: init i32 = initialize_from %n.ref to %.loc17_31.2
  103. // CHECK:STDOUT: %.loc17_31.4: ref %.3 = class_element_access %return, element1
  104. // CHECK:STDOUT: %.loc17_31.5: init %.3 = initialize_from %next.ref to %.loc17_31.4
  105. // CHECK:STDOUT: %.loc17_31.6: init %Class = class_init (%.loc17_31.3, %.loc17_31.5), %return
  106. // CHECK:STDOUT: %.loc17_32: init %Class = converted %.loc17_31.1, %.loc17_31.6
  107. // CHECK:STDOUT: return %.loc17_32 to %return
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: fn @MakeReorder(%n: i32, %next: %.3) -> %return: %Class {
  111. // CHECK:STDOUT: !entry:
  112. // CHECK:STDOUT: %next.ref: %.3 = name_ref next, %next
  113. // CHECK:STDOUT: %n.ref: i32 = name_ref n, %n
  114. // CHECK:STDOUT: %.loc21_31.1: %.7 = struct_literal (%next.ref, %n.ref)
  115. // CHECK:STDOUT: %.loc21_31.2: ref i32 = class_element_access %return, element1
  116. // CHECK:STDOUT: %.loc21_31.3: init i32 = initialize_from %n.ref to %.loc21_31.2
  117. // CHECK:STDOUT: %.loc21_31.4: ref %.3 = class_element_access %return, element0
  118. // CHECK:STDOUT: %.loc21_31.5: init %.3 = initialize_from %next.ref to %.loc21_31.4
  119. // CHECK:STDOUT: %.loc21_31.6: init %Class = class_init (%.loc21_31.3, %.loc21_31.5), %return
  120. // CHECK:STDOUT: %.loc21_32: init %Class = converted %.loc21_31.1, %.loc21_31.6
  121. // CHECK:STDOUT: return %.loc21_32 to %return
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT: