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/ids.h"
  7. namespace Carbon::SemIR {
  8. // A class.
  9. struct Class : public Printable<Class> {
  10. enum InheritanceKind : int8_t {
  11. // `abstract class`
  12. Abstract,
  13. // `base class`
  14. Base,
  15. // `class`
  16. Final,
  17. };
  18. auto Print(llvm::raw_ostream& out) const -> void {
  19. out << "{name: " << name_id << ", enclosing_scope: " << enclosing_scope_id
  20. << "}";
  21. }
  22. // Determines whether this class has been fully defined. This is false until
  23. // we reach the `}` of the class definition.
  24. auto is_defined() const -> bool { return object_repr_id.is_valid(); }
  25. // Determines whether this is a generic class.
  26. auto is_generic() const -> bool {
  27. return implicit_param_refs_id.is_valid() || param_refs_id.is_valid();
  28. }
  29. // The following members always have values, and do not change throughout the
  30. // lifetime of the class.
  31. // The class name.
  32. NameId name_id;
  33. // The enclosing scope.
  34. NameScopeId enclosing_scope_id;
  35. // A block containing a single reference instruction per implicit parameter.
  36. InstBlockId implicit_param_refs_id;
  37. // A block containing a single reference instruction per parameter.
  38. InstBlockId param_refs_id;
  39. // The class type, which is the type of `Self` in the class definition.
  40. TypeId self_type_id;
  41. // The first declaration of the class. This is a ClassDecl.
  42. InstId decl_id = InstId::Invalid;
  43. // The kind of inheritance that this class supports.
  44. // TODO: The rules here are not yet decided. See #3384.
  45. InheritanceKind inheritance_kind;
  46. // The following members are set at the `{` of the class definition.
  47. // The definition of the class. This is a ClassDecl.
  48. InstId definition_id = InstId::Invalid;
  49. // The class scope.
  50. NameScopeId scope_id = NameScopeId::Invalid;
  51. // The first block of the class body.
  52. // TODO: Handle control flow in the class body, such as if-expressions.
  53. InstBlockId body_block_id = InstBlockId::Invalid;
  54. // The following members are accumulated throughout the class definition.
  55. // The adapted type declaration, if any. Invalid if the class is not an
  56. // adapter. This is an AdaptDecl instruction.
  57. // TODO: Consider sharing the storage for `adapt_id` and `base_id`. A class
  58. // can't have both.
  59. InstId adapt_id = InstId::Invalid;
  60. // The base class declaration. Invalid if the class has no base class. This is
  61. // a BaseDecl instruction.
  62. InstId base_id = InstId::Invalid;
  63. // The following members are set at the `}` of the class definition.
  64. // The object representation type to use for this class. This is valid once
  65. // the class is defined. For an adapter, this is the non-adapter type that
  66. // this class directly or transitively adapts.
  67. TypeId object_repr_id = TypeId::Invalid;
  68. };
  69. } // namespace Carbon::SemIR
  70. #endif // CARBON_TOOLCHAIN_SEM_IR_CLASS_H_