basic.carbon 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/basic.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/generic/basic.carbon
  10. class Class(T:! type) {
  11. fn GetAddr[addr self: Self*]() -> T* {
  12. return &self->k;
  13. }
  14. // TODO: Should this work? T is not necessarily copyable.
  15. fn GetValue[self: Self]() -> T {
  16. return self.k;
  17. }
  18. var k: T;
  19. }
  20. // CHECK:STDOUT: --- basic.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: %.2: type = ptr_type %Class.2 [symbolic]
  29. // CHECK:STDOUT: %.3: type = ptr_type %T [symbolic]
  30. // CHECK:STDOUT: %GetAddr.type: type = fn_type @GetAddr [template]
  31. // CHECK:STDOUT: %GetAddr: %GetAddr.type = struct_value () [template]
  32. // CHECK:STDOUT: %GetValue.type: type = fn_type @GetValue [template]
  33. // CHECK:STDOUT: %GetValue: %GetValue.type = struct_value () [template]
  34. // CHECK:STDOUT: %.4: type = unbound_element_type %Class.2, %T [symbolic]
  35. // CHECK:STDOUT: %.5: type = struct_type {.k: %T} [symbolic]
  36. // CHECK:STDOUT: %.6: type = ptr_type %.5 [symbolic]
  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: }
  44. // CHECK:STDOUT: %Core.import = import Core
  45. // CHECK:STDOUT: %Core: <namespace> = namespace %Core.import, [template] {}
  46. // CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [template = constants.%Class.1] {
  47. // CHECK:STDOUT: %T.loc11_13.1: type = param T
  48. // CHECK:STDOUT: %T.loc11_13.2: type = bind_symbolic_name T 0, %T.loc11_13.1 [symbolic = %T.loc11_13.2 (constants.%T)]
  49. // CHECK:STDOUT: }
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT:
  52. // CHECK:STDOUT: class @Class
  53. // CHECK:STDOUT: generic [file.%T.loc11_13.2: type] {
  54. // CHECK:STDOUT: %GetAddr.decl: %GetAddr.type = fn_decl @GetAddr [template = constants.%GetAddr] {
  55. // CHECK:STDOUT: %.loc12_25: type = specific_constant constants.%Class.2, (constants.%T) [symbolic = %.loc12_25 (constants.%Class.2)]
  56. // CHECK:STDOUT: %Self.ref.loc12: type = name_ref Self, %.loc12_25 [symbolic = %.loc12_25 (constants.%Class.2)]
  57. // CHECK:STDOUT: %.loc12_29: type = ptr_type %Class.2 [symbolic = %.loc12_29 (constants.%.2)]
  58. // CHECK:STDOUT: %self.loc12_19.1: @Class.%.loc12_29 (%.2) = param self
  59. // CHECK:STDOUT: %self.loc12_19.3: @Class.%.loc12_29 (%.2) = bind_name self, %self.loc12_19.1
  60. // CHECK:STDOUT: %.loc12_14: @Class.%.loc12_29 (%.2) = addr_pattern %self.loc12_19.3
  61. // CHECK:STDOUT: %T.ref.loc12: type = name_ref T, file.%T.loc11_13.2 [symbolic = %T.ref.loc12 (constants.%T)]
  62. // CHECK:STDOUT: %.loc12_38: type = ptr_type %T [symbolic = %.loc12_38 (constants.%.3)]
  63. // CHECK:STDOUT: %return.var.loc12: ref %.3 = var <return slot>
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT: %GetValue.decl: %GetValue.type = fn_decl @GetValue [template = constants.%GetValue] {
  66. // CHECK:STDOUT: %.loc17: type = specific_constant constants.%Class.2, (constants.%T) [symbolic = %.loc17 (constants.%Class.2)]
  67. // CHECK:STDOUT: %Self.ref.loc17: type = name_ref Self, %.loc17 [symbolic = %.loc17 (constants.%Class.2)]
  68. // CHECK:STDOUT: %self.loc17_15.1: @Class.%.loc17 (%Class.2) = param self
  69. // CHECK:STDOUT: %self.loc17_15.2: @Class.%.loc17 (%Class.2) = bind_name self, %self.loc17_15.1
  70. // CHECK:STDOUT: %T.ref.loc17: type = name_ref T, file.%T.loc11_13.2 [symbolic = %T.ref.loc17 (constants.%T)]
  71. // CHECK:STDOUT: %return.var.loc17: ref %T = var <return slot>
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %T.ref.loc21: type = name_ref T, file.%T.loc11_13.2 [symbolic = constants.%T]
  74. // CHECK:STDOUT: %.loc21: %.4 = field_decl k, element0 [template]
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: !members:
  77. // CHECK:STDOUT: .Self = constants.%Class.2
  78. // CHECK:STDOUT: .GetAddr = %GetAddr.decl
  79. // CHECK:STDOUT: .GetValue = %GetValue.decl
  80. // CHECK:STDOUT: .k = %.loc21
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: fn @GetAddr[addr @Class.%self.loc12_19.3: @Class.%.loc12_29 (%.2)]() -> %.3
  84. // CHECK:STDOUT: generic [file.%T.loc11_13.2: type] {
  85. // CHECK:STDOUT: !entry:
  86. // CHECK:STDOUT: %self.ref: %.2 = name_ref self, @Class.%self.loc12_19.3
  87. // CHECK:STDOUT: %.loc13_17.1: ref %Class.2 = deref %self.ref
  88. // CHECK:STDOUT: %k.ref: %.4 = name_ref k, @Class.%.loc21 [template = @Class.%.loc21]
  89. // CHECK:STDOUT: %.loc13_17.2: ref %T = class_element_access %.loc13_17.1, element0
  90. // CHECK:STDOUT: %.loc13_12: %.3 = addr_of %.loc13_17.2
  91. // CHECK:STDOUT: return %.loc13_12
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: fn @GetValue[@Class.%self.loc17_15.2: @Class.%.loc17 (%Class.2)]() -> %T
  95. // CHECK:STDOUT: generic [file.%T.loc11_13.2: type] {
  96. // CHECK:STDOUT: !entry:
  97. // CHECK:STDOUT: %self.ref: %Class.2 = name_ref self, @Class.%self.loc17_15.2
  98. // CHECK:STDOUT: %k.ref: %.4 = name_ref k, @Class.%.loc21 [template = @Class.%.loc21]
  99. // CHECK:STDOUT: %.loc18_16.1: ref %T = class_element_access %self.ref, element0
  100. // CHECK:STDOUT: %.loc18_16.2: %T = bind_value %.loc18_16.1
  101. // CHECK:STDOUT: return %.loc18_16.2
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: