class.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // The following members are set at the `{` of the class definition.
  27. // The class scope.
  28. NameScopeId scope_id = NameScopeId::Invalid;
  29. // The first block of the class body.
  30. // TODO: Handle control flow in the class body, such as if-expressions.
  31. InstBlockId body_block_id = InstBlockId::Invalid;
  32. // The following members are accumulated throughout the class definition.
  33. // The adapted type declaration, if any. Invalid if the class is not an
  34. // adapter. This is an AdaptDecl instruction.
  35. // TODO: Consider sharing the storage for `adapt_id` and `base_id`. A class
  36. // can't have both.
  37. InstId adapt_id = InstId::Invalid;
  38. // The base class declaration. Invalid if the class has no base class. This is
  39. // a BaseDecl instruction.
  40. InstId base_id = InstId::Invalid;
  41. // The following members are set at the `}` of the class definition.
  42. // A `CompleteTypeWitness` instruction witnessing that this class type is
  43. // complete, and tracking its object representation. This is valid once the
  44. // class is defined. For an adapter, the object representation is the
  45. // non-adapter type that this class directly or transitively adapts.
  46. InstId complete_type_witness_id = InstId::Invalid;
  47. };
  48. // A class. See EntityWithParamsBase regarding the inheritance here.
  49. struct Class : public EntityWithParamsBase,
  50. public ClassFields,
  51. public Printable<Class> {
  52. auto Print(llvm::raw_ostream& out) const -> void {
  53. out << "{";
  54. PrintBaseFields(out);
  55. out << "}";
  56. }
  57. // Determines whether this class has been fully defined. This is false until
  58. // we reach the `}` of the class definition.
  59. auto is_defined() const -> bool {
  60. return complete_type_witness_id.is_valid();
  61. }
  62. // Gets the object representation for this class. Returns Invalid if the class
  63. // is not yet defined.
  64. auto GetObjectRepr(const File& file, SpecificId specific_id) const -> TypeId;
  65. };
  66. } // namespace Carbon::SemIR
  67. #endif // CARBON_TOOLCHAIN_SEM_IR_CLASS_H_