init.carbon 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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: %.6: <witness> = complete_type_witness %.5 [template]
  32. // CHECK:STDOUT: %Make.type: type = fn_type @Make [template]
  33. // CHECK:STDOUT: %Make: %Make.type = struct_value () [template]
  34. // CHECK:STDOUT: %.7: type = ptr_type %.5 [template]
  35. // CHECK:STDOUT: %MakeReorder.type: type = fn_type @MakeReorder [template]
  36. // CHECK:STDOUT: %MakeReorder: %MakeReorder.type = struct_value () [template]
  37. // CHECK:STDOUT: %.8: type = struct_type {.next: %.3, .n: i32} [template]
  38. // CHECK:STDOUT: }
  39. // CHECK:STDOUT:
  40. // CHECK:STDOUT: imports {
  41. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  42. // CHECK:STDOUT: .Int32 = %import_ref
  43. // CHECK:STDOUT: import Core//prelude
  44. // CHECK:STDOUT: import Core//prelude/operators
  45. // CHECK:STDOUT: import Core//prelude/types
  46. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  47. // CHECK:STDOUT: import Core//prelude/operators/as
  48. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  49. // CHECK:STDOUT: import Core//prelude/operators/comparison
  50. // CHECK:STDOUT: import Core//prelude/types/bool
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT:
  55. // CHECK:STDOUT: file {
  56. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  57. // CHECK:STDOUT: .Core = imports.%Core
  58. // CHECK:STDOUT: .Class = %Class.decl
  59. // CHECK:STDOUT: .Make = %Make.decl
  60. // CHECK:STDOUT: .MakeReorder = %MakeReorder.decl
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: %Core.import = import Core
  63. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  64. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [template = constants.%Make] {
  65. // CHECK:STDOUT: %n.patt: i32 = binding_pattern n
  66. // CHECK:STDOUT: %next.patt: %.3 = binding_pattern next
  67. // CHECK:STDOUT: } {
  68. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  69. // CHECK:STDOUT: %.loc16_12.1: type = value_of_initializer %int.make_type_32 [template = i32]
  70. // CHECK:STDOUT: %.loc16_12.2: type = converted %int.make_type_32, %.loc16_12.1 [template = i32]
  71. // CHECK:STDOUT: %n.param: i32 = param n, runtime_param0
  72. // CHECK:STDOUT: %n: i32 = bind_name n, %n.param
  73. // CHECK:STDOUT: %Class.ref.loc16_23: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  74. // CHECK:STDOUT: %.loc16_28: type = ptr_type %Class [template = constants.%.3]
  75. // CHECK:STDOUT: %next.param: %.3 = param next, runtime_param1
  76. // CHECK:STDOUT: %next: %.3 = bind_name next, %next.param
  77. // CHECK:STDOUT: %Class.ref.loc16_34: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  78. // CHECK:STDOUT: %return: ref %Class = var <return slot>
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: %MakeReorder.decl: %MakeReorder.type = fn_decl @MakeReorder [template = constants.%MakeReorder] {
  81. // CHECK:STDOUT: %n.patt: i32 = binding_pattern n
  82. // CHECK:STDOUT: %next.patt: %.3 = binding_pattern next
  83. // CHECK:STDOUT: } {
  84. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  85. // CHECK:STDOUT: %.loc20_19.1: type = value_of_initializer %int.make_type_32 [template = i32]
  86. // CHECK:STDOUT: %.loc20_19.2: type = converted %int.make_type_32, %.loc20_19.1 [template = i32]
  87. // CHECK:STDOUT: %n.param: i32 = param n, runtime_param0
  88. // CHECK:STDOUT: %n: i32 = bind_name n, %n.param
  89. // CHECK:STDOUT: %Class.ref.loc20_30: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  90. // CHECK:STDOUT: %.loc20_35: type = ptr_type %Class [template = constants.%.3]
  91. // CHECK:STDOUT: %next.param: %.3 = param next, runtime_param1
  92. // CHECK:STDOUT: %next: %.3 = bind_name next, %next.param
  93. // CHECK:STDOUT: %Class.ref.loc20_41: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  94. // CHECK:STDOUT: %return: ref %Class = var <return slot>
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: class @Class {
  99. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  100. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  101. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32, %.loc12_10.1 [template = i32]
  102. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl n, element0 [template]
  103. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  104. // CHECK:STDOUT: %.loc13_18: type = ptr_type %Class [template = constants.%.3]
  105. // CHECK:STDOUT: %.loc13_11: %.4 = field_decl next, element1 [template]
  106. // CHECK:STDOUT: %.loc14: <witness> = complete_type_witness %.5 [template = constants.%.6]
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: !members:
  109. // CHECK:STDOUT: .Self = constants.%Class
  110. // CHECK:STDOUT: .n = %.loc12_8
  111. // CHECK:STDOUT: .next = %.loc13_11
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: fn @Make(%n: i32, %next: %.3) -> %return: %Class {
  117. // CHECK:STDOUT: !entry:
  118. // CHECK:STDOUT: %n.ref: i32 = name_ref n, %n
  119. // CHECK:STDOUT: %next.ref: %.3 = name_ref next, %next
  120. // CHECK:STDOUT: %.loc17_31.1: %.5 = struct_literal (%n.ref, %next.ref)
  121. // CHECK:STDOUT: %.loc17_31.2: ref i32 = class_element_access %return, element0
  122. // CHECK:STDOUT: %.loc17_31.3: init i32 = initialize_from %n.ref to %.loc17_31.2
  123. // CHECK:STDOUT: %.loc17_31.4: ref %.3 = class_element_access %return, element1
  124. // CHECK:STDOUT: %.loc17_31.5: init %.3 = initialize_from %next.ref to %.loc17_31.4
  125. // CHECK:STDOUT: %.loc17_31.6: init %Class = class_init (%.loc17_31.3, %.loc17_31.5), %return
  126. // CHECK:STDOUT: %.loc17_32: init %Class = converted %.loc17_31.1, %.loc17_31.6
  127. // CHECK:STDOUT: return %.loc17_32 to %return
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: fn @MakeReorder(%n: i32, %next: %.3) -> %return: %Class {
  131. // CHECK:STDOUT: !entry:
  132. // CHECK:STDOUT: %next.ref: %.3 = name_ref next, %next
  133. // CHECK:STDOUT: %n.ref: i32 = name_ref n, %n
  134. // CHECK:STDOUT: %.loc21_31.1: %.8 = struct_literal (%next.ref, %n.ref)
  135. // CHECK:STDOUT: %.loc21_31.2: ref i32 = class_element_access %return, element1
  136. // CHECK:STDOUT: %.loc21_31.3: init i32 = initialize_from %n.ref to %.loc21_31.2
  137. // CHECK:STDOUT: %.loc21_31.4: ref %.3 = class_element_access %return, element0
  138. // CHECK:STDOUT: %.loc21_31.5: init %.3 = initialize_from %next.ref to %.loc21_31.4
  139. // CHECK:STDOUT: %.loc21_31.6: init %Class = class_init (%.loc21_31.3, %.loc21_31.5), %return
  140. // CHECK:STDOUT: %.loc21_32: init %Class = converted %.loc21_31.1, %.loc21_31.6
  141. // CHECK:STDOUT: return %.loc21_32 to %return
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT: