class.h 3.1 KB

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