class.h 4.2 KB

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