class.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. };
  53. // A class. See EntityWithParamsBase regarding the inheritance here.
  54. struct Class : public EntityWithParamsBase,
  55. public ClassFields,
  56. public Printable<Class> {
  57. auto Print(llvm::raw_ostream& out) const -> void {
  58. out << "{";
  59. PrintBaseFields(out);
  60. out << "}";
  61. }
  62. // Determines whether this class has been fully defined. This is false until
  63. // we reach the `}` of the class definition.
  64. auto is_defined() const -> bool {
  65. return complete_type_witness_id.has_value();
  66. }
  67. // Gets the type that this class type adapts. Returns `None` if there is no
  68. // such type, or if the class is not yet defined.
  69. auto GetAdaptedType(const File& file, SpecificId specific_id) const -> TypeId;
  70. // Gets the base class for this class type. Returns `None` if there is no
  71. // such type, or if the class is not yet defined.
  72. auto GetBaseType(const File& file, SpecificId specific_id) const -> TypeId;
  73. // Gets the object representation for this class. Returns `None` if the class
  74. // is not yet defined.
  75. auto GetObjectRepr(const File& file, SpecificId specific_id) const -> TypeId;
  76. };
  77. } // namespace Carbon::SemIR
  78. #endif // CARBON_TOOLCHAIN_SEM_IR_CLASS_H_