class.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #ifndef CARBON_TOOLCHAIN_SEM_IR_CLASS_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_CLASS_H_
  6. #include "toolchain/sem_ir/entity_with_params_base.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::SemIR {
  9. // Class-specific fields.
  10. struct ClassFields {
  11. enum InheritanceKind : int8_t {
  12. // `abstract class`
  13. Abstract,
  14. // `base class`
  15. Base,
  16. // `class`
  17. Final,
  18. };
  19. // The following members always have values, and do not change throughout the
  20. // lifetime of the class.
  21. // The class type, which is the type of `Self` in the class definition.
  22. TypeId self_type_id;
  23. // The kind of inheritance that this class supports.
  24. // TODO: The rules here are not yet decided. See #3384.
  25. InheritanceKind inheritance_kind;
  26. // Whether this class or any base class has at least one virtual function.
  27. bool is_dynamic = false;
  28. // The following members are set at the `{` of the class definition.
  29. // The class scope.
  30. NameScopeId scope_id = NameScopeId::None;
  31. // The first block of the class body.
  32. // TODO: Handle control flow in the class body, such as if-expressions.
  33. InstBlockId body_block_id = InstBlockId::None;
  34. // The following members are accumulated throughout the class definition.
  35. // The adapted type declaration, if any. `None` if the class is not an
  36. // adapter. This is an AdaptDecl instruction.
  37. // TODO: Consider sharing the storage for `adapt_id` and `base_id`. A class
  38. // can't have both.
  39. InstId adapt_id = InstId::None;
  40. // The base class declaration. `None` if the class has no base class. This is
  41. // a BaseDecl instruction.
  42. InstId base_id = InstId::None;
  43. // The following members are set at the `}` of the class definition.
  44. // A `CompleteTypeWitness` instruction witnessing that this class type is
  45. // complete, and tracking its object representation. This has a value once the
  46. // class is defined. For an adapter, the object representation is the
  47. // non-adapter type that this class directly or transitively adapts.
  48. InstId complete_type_witness_id = InstId::None;
  49. // The virtual function table. `None` if the class has no (direct or
  50. // inherited) virtual functions.
  51. InstId vtable_id = InstId::None;
  52. auto PrintClassFields(llvm::raw_ostream& out) const -> void {
  53. out << "self_type_id: " << self_type_id << ", inheritance_kind: ";
  54. switch (inheritance_kind) {
  55. case Abstract:
  56. out << "Abstract";
  57. break;
  58. case Base:
  59. out << "Base";
  60. break;
  61. case Final:
  62. out << "Final";
  63. break;
  64. }
  65. out << ", is_dynamic: " << is_dynamic << ", scope_id: " << scope_id
  66. << ", body_block_id: " << body_block_id << ", adapt_id: " << adapt_id
  67. << ", base_id: " << base_id
  68. << ", complete_type_witness_id: " << complete_type_witness_id
  69. << ", vtable_id: " << vtable_id << "}";
  70. }
  71. };
  72. // A class. See EntityWithParamsBase regarding the inheritance here.
  73. struct Class : public EntityWithParamsBase,
  74. public ClassFields,
  75. public Printable<Class> {
  76. auto Print(llvm::raw_ostream& out) const -> void {
  77. out << "{";
  78. PrintBaseFields(out);
  79. out << ", ";
  80. PrintClassFields(out);
  81. out << "}";
  82. }
  83. // This is false until we reach the `}` of the class definition.
  84. auto is_complete() const -> bool {
  85. return complete_type_witness_id.has_value();
  86. }
  87. // Gets the type that this class type adapts. Returns `None` if there is no
  88. // such type, or if the class is not yet defined.
  89. auto GetAdaptedType(const File& file, SpecificId specific_id) const -> TypeId;
  90. // Gets the base class for this class type. Returns `None` if there is no
  91. // such type, or if the class is not yet defined.
  92. auto GetBaseType(const File& file, SpecificId specific_id) const -> TypeId;
  93. // Gets the object representation for this class. Returns `None` if the class
  94. // is not yet defined.
  95. auto GetObjectRepr(const File& file, SpecificId specific_id) const -> TypeId;
  96. };
  97. } // namespace Carbon::SemIR
  98. #endif // CARBON_TOOLCHAIN_SEM_IR_CLASS_H_