self.carbon 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/self.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/self.carbon
  10. class Class {
  11. fn F[self: Self]() -> i32;
  12. fn G[addr self: Self*]() -> i32;
  13. var n: i32;
  14. }
  15. fn Class.F[self: Self]() -> i32 {
  16. return self.n;
  17. }
  18. fn Class.G[addr self: Self*]() -> i32 {
  19. return (*self).n;
  20. }
  21. // CHECK:STDOUT: --- self.carbon
  22. // CHECK:STDOUT:
  23. // CHECK:STDOUT: constants {
  24. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  25. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  26. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  27. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  28. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  29. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  30. // CHECK:STDOUT: %.2: type = ptr_type %Class [template]
  31. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  32. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  33. // CHECK:STDOUT: %.3: type = unbound_element_type %Class, i32 [template]
  34. // CHECK:STDOUT: %.4: type = struct_type {.n: i32} [template]
  35. // CHECK:STDOUT: %.5: type = ptr_type %.4 [template]
  36. // CHECK:STDOUT: }
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: file {
  39. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  40. // CHECK:STDOUT: .Core = %Core
  41. // CHECK:STDOUT: .Class = %Class.decl
  42. // CHECK:STDOUT: }
  43. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  44. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {}
  45. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  46. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  47. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  48. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  49. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  50. // CHECK:STDOUT: %Self.ref.loc18: type = name_ref Self, constants.%Class [template = constants.%Class]
  51. // CHECK:STDOUT: %self.loc18_12.1: %Class = param self
  52. // CHECK:STDOUT: @F.%self: %Class = bind_name self, %self.loc18_12.1
  53. // CHECK:STDOUT: %int.make_type_32.loc18: init type = call constants.%Int32() [template = i32]
  54. // CHECK:STDOUT: %.loc18_29.1: type = value_of_initializer %int.make_type_32.loc18 [template = i32]
  55. // CHECK:STDOUT: %.loc18_29.2: type = converted %int.make_type_32.loc18, %.loc18_29.1 [template = i32]
  56. // CHECK:STDOUT: @F.%return: ref i32 = var <return slot>
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT: %import_ref.5: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  59. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  60. // CHECK:STDOUT: %Self.ref.loc22: type = name_ref Self, constants.%Class [template = constants.%Class]
  61. // CHECK:STDOUT: %.loc22_27: type = ptr_type %Class [template = constants.%.2]
  62. // CHECK:STDOUT: %self.loc22_17.1: %.2 = param self
  63. // CHECK:STDOUT: @G.%self: %.2 = bind_name self, %self.loc22_17.1
  64. // CHECK:STDOUT: @G.%.loc22: %.2 = addr_pattern @G.%self
  65. // CHECK:STDOUT: %int.make_type_32.loc22: init type = call constants.%Int32() [template = i32]
  66. // CHECK:STDOUT: %.loc22_35.1: type = value_of_initializer %int.make_type_32.loc22 [template = i32]
  67. // CHECK:STDOUT: %.loc22_35.2: type = converted %int.make_type_32.loc22, %.loc22_35.1 [template = i32]
  68. // CHECK:STDOUT: @G.%return: ref i32 = var <return slot>
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: class @Class {
  73. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  74. // CHECK:STDOUT: %Self.ref.loc12: type = name_ref Self, constants.%Class [template = constants.%Class]
  75. // CHECK:STDOUT: %self.loc12_8.1: %Class = param self
  76. // CHECK:STDOUT: %self.loc12_8.2: %Class = bind_name self, %self.loc12_8.1
  77. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  78. // CHECK:STDOUT: %.loc12_25.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  79. // CHECK:STDOUT: %.loc12_25.2: type = converted %int.make_type_32.loc12, %.loc12_25.1 [template = i32]
  80. // CHECK:STDOUT: %return.var.loc12: ref i32 = var <return slot>
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  83. // CHECK:STDOUT: %Self.ref.loc13: type = name_ref Self, constants.%Class [template = constants.%Class]
  84. // CHECK:STDOUT: %.loc13_23: type = ptr_type %Class [template = constants.%.2]
  85. // CHECK:STDOUT: %self.loc13_13.1: %.2 = param self
  86. // CHECK:STDOUT: %self.loc13_13.3: %.2 = bind_name self, %self.loc13_13.1
  87. // CHECK:STDOUT: %.loc13_8: %.2 = addr_pattern %self.loc13_13.3
  88. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  89. // CHECK:STDOUT: %.loc13_31.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  90. // CHECK:STDOUT: %.loc13_31.2: type = converted %int.make_type_32.loc13, %.loc13_31.1 [template = i32]
  91. // CHECK:STDOUT: %return.var.loc13: ref i32 = var <return slot>
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT: %int.make_type_32.loc15: init type = call constants.%Int32() [template = i32]
  94. // CHECK:STDOUT: %.loc15_10.1: type = value_of_initializer %int.make_type_32.loc15 [template = i32]
  95. // CHECK:STDOUT: %.loc15_10.2: type = converted %int.make_type_32.loc15, %.loc15_10.1 [template = i32]
  96. // CHECK:STDOUT: %.loc15_8: %.3 = field_decl n, element0 [template]
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: !members:
  99. // CHECK:STDOUT: .Self = constants.%Class
  100. // CHECK:STDOUT: .F = %F.decl
  101. // CHECK:STDOUT: .G = %G.decl
  102. // CHECK:STDOUT: .n = %.loc15_8
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: fn @F[%self: %Class]() -> i32 {
  108. // CHECK:STDOUT: !entry:
  109. // CHECK:STDOUT: %self.ref: %Class = name_ref self, %self
  110. // CHECK:STDOUT: %n.ref: %.3 = name_ref n, @Class.%.loc15_8 [template = @Class.%.loc15_8]
  111. // CHECK:STDOUT: %.loc19_14.1: ref i32 = class_element_access %self.ref, element0
  112. // CHECK:STDOUT: %.loc19_14.2: i32 = bind_value %.loc19_14.1
  113. // CHECK:STDOUT: return %.loc19_14.2
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: fn @G[addr %self: %.2]() -> i32 {
  117. // CHECK:STDOUT: !entry:
  118. // CHECK:STDOUT: %self.ref: %.2 = name_ref self, %self
  119. // CHECK:STDOUT: %.loc23_11: ref %Class = deref %self.ref
  120. // CHECK:STDOUT: %n.ref: %.3 = name_ref n, @Class.%.loc15_8 [template = @Class.%.loc15_8]
  121. // CHECK:STDOUT: %.loc23_17.1: ref i32 = class_element_access %.loc23_11, element0
  122. // CHECK:STDOUT: %.loc23_17.2: i32 = bind_value %.loc23_17.1
  123. // CHECK:STDOUT: return %.loc23_17.2
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: