class.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // The object representation type to use for this class. This is valid once
  43. // the class is defined. For an adapter, this is the non-adapter type that
  44. // this class directly or transitively adapts.
  45. TypeId object_repr_id = TypeId::Invalid;
  46. };
  47. // A class. See EntityWithParamsBase regarding the inheritance here.
  48. struct Class : public EntityWithParamsBase,
  49. public ClassFields,
  50. public Printable<Class> {
  51. auto Print(llvm::raw_ostream& out) const -> void {
  52. out << "{";
  53. PrintBaseFields(out);
  54. out << "}";
  55. }
  56. // Determines whether this class has been fully defined. This is false until
  57. // we reach the `}` of the class definition.
  58. auto is_defined() const -> bool { return object_repr_id.is_valid(); }
  59. };
  60. } // namespace Carbon::SemIR
  61. #endif // CARBON_TOOLCHAIN_SEM_IR_CLASS_H_