interface.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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_INTERFACE_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_INTERFACE_H_
  6. #include "toolchain/base/value_store.h"
  7. #include "toolchain/sem_ir/entity_with_params_base.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::SemIR {
  10. // Interface-specific fields.
  11. //
  12. // TODO: Factor out the shared fields between InterfaceFields and
  13. // NamedConstraintFields.
  14. struct InterfaceFields {
  15. // The following members are set at the `{` of the interface definition.
  16. // The interface scopes.
  17. NameScopeId scope_without_self_id = NameScopeId::None;
  18. NameScopeId scope_with_self_id = NameScopeId::None;
  19. // The block of instructions outside the interface-with-self. This is where
  20. // the `Self` instruction can be constructed.
  21. InstBlockId body_block_without_self_id = InstBlockId::None;
  22. // The interface-with-self generic, where the `Self` is a parameter to the
  23. // generic. This generic contains all the associated entities of the
  24. // interface.
  25. GenericId generic_with_self_id = GenericId::None;
  26. // The first block of the interface-with-self body.
  27. // TODO: Handle control flow in the interface body, such as if-expressions.
  28. InstBlockId body_block_with_self_id = InstBlockId::None;
  29. // The implicit `Self` parameter. This is a SymbolicBinding instruction.
  30. InstId self_param_id = InstId::None;
  31. // The following members are set at the `}` of the interface definition.
  32. RequireImplsBlockId require_impls_block_id = RequireImplsBlockId::None;
  33. InstBlockId associated_entities_id = InstBlockId::None;
  34. };
  35. // An interface. See EntityWithParamsBase regarding the inheritance here.
  36. struct Interface : public EntityWithParamsBase,
  37. public InterfaceFields,
  38. public Printable<Interface> {
  39. auto Print(llvm::raw_ostream& out) const -> void {
  40. out << "{";
  41. PrintBaseFields(out);
  42. out << ", require_impls_block_id: " << require_impls_block_id;
  43. out << "}";
  44. }
  45. // This is false until we reach the `}` of the interface definition.
  46. auto is_complete() const -> bool {
  47. return associated_entities_id.has_value();
  48. }
  49. // Determines whether we're currently defining the interface. This is true
  50. // between the braces of the interface.
  51. auto is_being_defined() const -> bool {
  52. return has_definition_started() && !is_complete();
  53. }
  54. };
  55. using InterfaceStore = ValueStore<InterfaceId, Interface, Tag<CheckIRId>>;
  56. } // namespace Carbon::SemIR
  57. #endif // CARBON_TOOLCHAIN_SEM_IR_INTERFACE_H_