class.h 4.8 KB

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