interface.h 2.9 KB

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