class.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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::Invalid;
  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::Invalid;
  34. // The following members are accumulated throughout the class definition.
  35. // The adapted type declaration, if any. Invalid 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::Invalid;
  40. // The base class declaration. Invalid if the class has no base class. This is
  41. // a BaseDecl instruction.
  42. InstId base_id = InstId::Invalid;
  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 is valid 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::Invalid;
  49. };
  50. // A class. See EntityWithParamsBase regarding the inheritance here.
  51. struct Class : public EntityWithParamsBase,
  52. public ClassFields,
  53. public Printable<Class> {
  54. auto Print(llvm::raw_ostream& out) const -> void {
  55. out << "{";
  56. PrintBaseFields(out);
  57. out << "}";
  58. }
  59. // Determines whether this class has been fully defined. This is false until
  60. // we reach the `}` of the class definition.
  61. auto is_defined() const -> bool {
  62. return complete_type_witness_id.is_valid();
  63. }
  64. // Gets the object representation for this class. Returns Invalid if the class
  65. // is not yet defined.
  66. auto GetObjectRepr(const File& file, SpecificId specific_id) const -> TypeId;
  67. };
  68. } // namespace Carbon::SemIR
  69. #endif // CARBON_TOOLCHAIN_SEM_IR_CLASS_H_