self.carbon 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/generic/self.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/generic/self.carbon
  10. class Class(T:! type) {
  11. // `Self` is the same as `Class(T)` here.
  12. // TODO: Find a better way to test two types are the same.
  13. fn MakeSelf() -> Self;
  14. fn MakeClass() -> Class(T);
  15. fn F() {
  16. var c: Class(T) = MakeSelf();
  17. var s: Self = MakeClass();
  18. }
  19. }
  20. // CHECK:STDOUT: --- self.carbon
  21. // CHECK:STDOUT:
  22. // CHECK:STDOUT: constants {
  23. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic]
  24. // CHECK:STDOUT: %Class.type: type = generic_class_type @Class [template]
  25. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  26. // CHECK:STDOUT: %Class.1: %Class.type = struct_value () [template]
  27. // CHECK:STDOUT: %Class.2: type = class_type @Class, (%T) [symbolic]
  28. // CHECK:STDOUT: %MakeSelf.type: type = fn_type @MakeSelf [template]
  29. // CHECK:STDOUT: %MakeSelf: %MakeSelf.type = struct_value () [template]
  30. // CHECK:STDOUT: %MakeClass.type: type = fn_type @MakeClass [template]
  31. // CHECK:STDOUT: %MakeClass: %MakeClass.type = struct_value () [template]
  32. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  33. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  34. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  35. // CHECK:STDOUT: %.3: type = ptr_type %.2 [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: %Class.type = class_decl @Class [template = constants.%Class.1] {
  45. // CHECK:STDOUT: %T.loc11_13.1: type = param T
  46. // CHECK:STDOUT: %T.loc11_13.2: type = bind_symbolic_name T 0, %T.loc11_13.1 [symbolic = constants.%T]
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: class @Class
  51. // CHECK:STDOUT: generic [file.%T.loc11_13.2: type] {
  52. // CHECK:STDOUT: %MakeSelf.decl: %MakeSelf.type = fn_decl @MakeSelf [template = constants.%MakeSelf] {
  53. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Class.2 [symbolic = constants.%Class.2]
  54. // CHECK:STDOUT: %return.var.loc14: ref %Class.2 = var <return slot>
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT: %MakeClass.decl: %MakeClass.type = fn_decl @MakeClass [template = constants.%MakeClass] {
  57. // CHECK:STDOUT: %Class.ref: %Class.type = name_ref Class, file.%Class.decl [template = constants.%Class.1]
  58. // CHECK:STDOUT: %T.ref: type = name_ref T, file.%T.loc11_13.2 [symbolic = constants.%T]
  59. // CHECK:STDOUT: %.loc15_26: init type = call %Class.ref(%T.ref) [symbolic = constants.%Class.2]
  60. // CHECK:STDOUT: %.loc15_28.1: type = value_of_initializer %.loc15_26 [symbolic = constants.%Class.2]
  61. // CHECK:STDOUT: %.loc15_28.2: type = converted %.loc15_26, %.loc15_28.1 [symbolic = constants.%Class.2]
  62. // CHECK:STDOUT: %return.var.loc15: ref %Class.2 = var <return slot>
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {}
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: !members:
  67. // CHECK:STDOUT: .Self = constants.%Class.2
  68. // CHECK:STDOUT: .MakeSelf = %MakeSelf.decl
  69. // CHECK:STDOUT: .MakeClass = %MakeClass.decl
  70. // CHECK:STDOUT: .F = %F.decl
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: fn @MakeSelf() -> %Class.2
  74. // CHECK:STDOUT: generic [file.%T.loc11_13.2: type];
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: fn @MakeClass() -> %Class.2
  77. // CHECK:STDOUT: generic [file.%T.loc11_13.2: type];
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: fn @F()
  80. // CHECK:STDOUT: generic [file.%T.loc11_13.2: type] {
  81. // CHECK:STDOUT: !entry:
  82. // CHECK:STDOUT: %Class.ref: %Class.type = name_ref Class, file.%Class.decl [template = constants.%Class.1]
  83. // CHECK:STDOUT: %T.ref: type = name_ref T, file.%T.loc11_13.2 [symbolic = constants.%T]
  84. // CHECK:STDOUT: %.loc17_17: init type = call %Class.ref(%T.ref) [symbolic = constants.%Class.2]
  85. // CHECK:STDOUT: %.loc17_19.1: type = value_of_initializer %.loc17_17 [symbolic = constants.%Class.2]
  86. // CHECK:STDOUT: %.loc17_19.2: type = converted %.loc17_17, %.loc17_19.1 [symbolic = constants.%Class.2]
  87. // CHECK:STDOUT: %c.var: ref %Class.2 = var c
  88. // CHECK:STDOUT: %c: ref %Class.2 = bind_name c, %c.var
  89. // CHECK:STDOUT: %MakeSelf.ref: %MakeSelf.type = name_ref MakeSelf, @Class.%MakeSelf.decl [template = constants.%MakeSelf]
  90. // CHECK:STDOUT: %.loc17_9: ref %Class.2 = splice_block %c.var {}
  91. // CHECK:STDOUT: %MakeSelf.call: init %Class.2 = call %MakeSelf.ref() to %.loc17_9
  92. // CHECK:STDOUT: assign %c.var, %MakeSelf.call
  93. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Class.2 [symbolic = constants.%Class.2]
  94. // CHECK:STDOUT: %s.var: ref %Class.2 = var s
  95. // CHECK:STDOUT: %s: ref %Class.2 = bind_name s, %s.var
  96. // CHECK:STDOUT: %MakeClass.ref: %MakeClass.type = name_ref MakeClass, @Class.%MakeClass.decl [template = constants.%MakeClass]
  97. // CHECK:STDOUT: %.loc18: ref %Class.2 = splice_block %s.var {}
  98. // CHECK:STDOUT: %MakeClass.call: init %Class.2 = call %MakeClass.ref() to %.loc18
  99. // CHECK:STDOUT: assign %s.var, %MakeClass.call
  100. // CHECK:STDOUT: return
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT: