init.carbon 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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: %int_32: Core.IntLiteral = int_value 32 [template]
  25. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  26. // CHECK:STDOUT: %Class.elem.c91: type = unbound_element_type %Class, %i32 [template]
  27. // CHECK:STDOUT: %ptr.e71: type = ptr_type %Class [template]
  28. // CHECK:STDOUT: %Class.elem.0c0: type = unbound_element_type %Class, %ptr.e71 [template]
  29. // CHECK:STDOUT: %struct_type.n.next: type = struct_type {.n: %i32, .next: %ptr.e71} [template]
  30. // CHECK:STDOUT: %complete_type.78f: <witness> = complete_type_witness %struct_type.n.next [template]
  31. // CHECK:STDOUT: %Make.type: type = fn_type @Make [template]
  32. // CHECK:STDOUT: %Make: %Make.type = struct_value () [template]
  33. // CHECK:STDOUT: %MakeReorder.type: type = fn_type @MakeReorder [template]
  34. // CHECK:STDOUT: %MakeReorder: %MakeReorder.type = struct_value () [template]
  35. // CHECK:STDOUT: %struct_type.next.n: type = struct_type {.next: %ptr.e71, .n: %i32} [template]
  36. // CHECK:STDOUT: }
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: imports {
  39. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  40. // CHECK:STDOUT: .Int = %Core.Int
  41. // CHECK:STDOUT: import Core//prelude
  42. // CHECK:STDOUT: import Core//prelude/...
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT:
  46. // CHECK:STDOUT: file {
  47. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  48. // CHECK:STDOUT: .Core = imports.%Core
  49. // CHECK:STDOUT: .Class = %Class.decl
  50. // CHECK:STDOUT: .Make = %Make.decl
  51. // CHECK:STDOUT: .MakeReorder = %MakeReorder.decl
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: %Core.import = import Core
  54. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  55. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [template = constants.%Make] {
  56. // CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
  57. // CHECK:STDOUT: %n.param_patt: %i32 = value_param_pattern %n.patt, runtime_param0
  58. // CHECK:STDOUT: %next.patt: %ptr.e71 = binding_pattern next
  59. // CHECK:STDOUT: %next.param_patt: %ptr.e71 = value_param_pattern %next.patt, runtime_param1
  60. // CHECK:STDOUT: %return.patt: %Class = return_slot_pattern
  61. // CHECK:STDOUT: %return.param_patt: %Class = out_param_pattern %return.patt, runtime_param2
  62. // CHECK:STDOUT: } {
  63. // CHECK:STDOUT: %Class.ref.loc16_34: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  64. // CHECK:STDOUT: %n.param: %i32 = value_param runtime_param0
  65. // CHECK:STDOUT: %.loc16_12: type = splice_block %i32 [template = constants.%i32] {
  66. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  67. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: %n: %i32 = bind_name n, %n.param
  70. // CHECK:STDOUT: %next.param: %ptr.e71 = value_param runtime_param1
  71. // CHECK:STDOUT: %.loc16_28: type = splice_block %ptr [template = constants.%ptr.e71] {
  72. // CHECK:STDOUT: %Class.ref.loc16_23: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  73. // CHECK:STDOUT: %ptr: type = ptr_type %Class [template = constants.%ptr.e71]
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: %next: %ptr.e71 = bind_name next, %next.param
  76. // CHECK:STDOUT: %return.param: ref %Class = out_param runtime_param2
  77. // CHECK:STDOUT: %return: ref %Class = return_slot %return.param
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT: %MakeReorder.decl: %MakeReorder.type = fn_decl @MakeReorder [template = constants.%MakeReorder] {
  80. // CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
  81. // CHECK:STDOUT: %n.param_patt: %i32 = value_param_pattern %n.patt, runtime_param0
  82. // CHECK:STDOUT: %next.patt: %ptr.e71 = binding_pattern next
  83. // CHECK:STDOUT: %next.param_patt: %ptr.e71 = value_param_pattern %next.patt, runtime_param1
  84. // CHECK:STDOUT: %return.patt: %Class = return_slot_pattern
  85. // CHECK:STDOUT: %return.param_patt: %Class = out_param_pattern %return.patt, runtime_param2
  86. // CHECK:STDOUT: } {
  87. // CHECK:STDOUT: %Class.ref.loc20_41: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  88. // CHECK:STDOUT: %n.param: %i32 = value_param runtime_param0
  89. // CHECK:STDOUT: %.loc20_19: type = splice_block %i32 [template = constants.%i32] {
  90. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  91. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT: %n: %i32 = bind_name n, %n.param
  94. // CHECK:STDOUT: %next.param: %ptr.e71 = value_param runtime_param1
  95. // CHECK:STDOUT: %.loc20_35: type = splice_block %ptr [template = constants.%ptr.e71] {
  96. // CHECK:STDOUT: %Class.ref.loc20_30: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  97. // CHECK:STDOUT: %ptr: type = ptr_type %Class [template = constants.%ptr.e71]
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: %next: %ptr.e71 = bind_name next, %next.param
  100. // CHECK:STDOUT: %return.param: ref %Class = out_param runtime_param2
  101. // CHECK:STDOUT: %return: ref %Class = return_slot %return.param
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: class @Class {
  106. // CHECK:STDOUT: %.loc12_8: %Class.elem.c91 = field_decl n, element0 [template]
  107. // CHECK:STDOUT: name_binding_decl {
  108. // CHECK:STDOUT: %.loc12_3: %Class.elem.c91 = var_pattern %.loc12_8
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT: %.var.loc12: ref %Class.elem.c91 = var <none>
  111. // CHECK:STDOUT: %.loc13_11: %Class.elem.0c0 = field_decl next, element1 [template]
  112. // CHECK:STDOUT: name_binding_decl {
  113. // CHECK:STDOUT: %.loc13_3: %Class.elem.0c0 = var_pattern %.loc13_11
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: %.var.loc13: ref %Class.elem.0c0 = var <none>
  116. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.n.next [template = constants.%complete_type.78f]
  117. // CHECK:STDOUT: complete_type_witness = %complete_type
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: !members:
  120. // CHECK:STDOUT: .Self = constants.%Class
  121. // CHECK:STDOUT: .n = %.loc12_8
  122. // CHECK:STDOUT: .next = %.loc13_11
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: fn @Make(%n.param_patt: %i32, %next.param_patt: %ptr.e71) -> %return.param_patt: %Class {
  126. // CHECK:STDOUT: !entry:
  127. // CHECK:STDOUT: %n.ref: %i32 = name_ref n, %n
  128. // CHECK:STDOUT: %next.ref: %ptr.e71 = name_ref next, %next
  129. // CHECK:STDOUT: %.loc17_31.1: %struct_type.n.next = struct_literal (%n.ref, %next.ref)
  130. // CHECK:STDOUT: %.loc17_31.2: ref %i32 = class_element_access %return, element0
  131. // CHECK:STDOUT: %.loc17_31.3: init %i32 = initialize_from %n.ref to %.loc17_31.2
  132. // CHECK:STDOUT: %.loc17_31.4: ref %ptr.e71 = class_element_access %return, element1
  133. // CHECK:STDOUT: %.loc17_31.5: init %ptr.e71 = initialize_from %next.ref to %.loc17_31.4
  134. // CHECK:STDOUT: %.loc17_31.6: init %Class = class_init (%.loc17_31.3, %.loc17_31.5), %return
  135. // CHECK:STDOUT: %.loc17_32: init %Class = converted %.loc17_31.1, %.loc17_31.6
  136. // CHECK:STDOUT: return %.loc17_32 to %return
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: fn @MakeReorder(%n.param_patt: %i32, %next.param_patt: %ptr.e71) -> %return.param_patt: %Class {
  140. // CHECK:STDOUT: !entry:
  141. // CHECK:STDOUT: %next.ref: %ptr.e71 = name_ref next, %next
  142. // CHECK:STDOUT: %n.ref: %i32 = name_ref n, %n
  143. // CHECK:STDOUT: %.loc21_31.1: %struct_type.next.n = struct_literal (%next.ref, %n.ref)
  144. // CHECK:STDOUT: %.loc21_31.2: ref %i32 = class_element_access %return, element1
  145. // CHECK:STDOUT: %.loc21_31.3: init %i32 = initialize_from %n.ref to %.loc21_31.2
  146. // CHECK:STDOUT: %.loc21_31.4: ref %ptr.e71 = class_element_access %return, element0
  147. // CHECK:STDOUT: %.loc21_31.5: init %ptr.e71 = initialize_from %next.ref to %.loc21_31.4
  148. // CHECK:STDOUT: %.loc21_31.6: init %Class = class_init (%.loc21_31.3, %.loc21_31.5), %return
  149. // CHECK:STDOUT: %.loc21_32: init %Class = converted %.loc21_31.1, %.loc21_31.6
  150. // CHECK:STDOUT: return %.loc21_32 to %return
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT: