named_constraint.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_NAMED_CONSTRAINT_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_NAMED_CONSTRAINT_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. // Named constraint-specific fields.
  11. struct NamedConstraintFields {
  12. // The following members are set at the `{` of the constraint definition.
  13. // The constraint scopes. The scope-without-self contains the symbolic `Self`
  14. // entity, which is then a generic binding of the generic-with-self. The
  15. // scope-with-self contains the rest of the entities in the constraint.
  16. NameScopeId scope_without_self_id = NameScopeId::None;
  17. NameScopeId scope_with_self_id = NameScopeId::None;
  18. // The block of instructions outside the constraint-with-self. This is where
  19. // the `Self` instruction can be constructed.
  20. InstBlockId body_block_without_self_id = InstBlockId::None;
  21. // The constraint-with-self generic, where the `Self` is a parameter to the
  22. // generic. This generic contains all the associated entities of the
  23. // interface.
  24. GenericId generic_with_self_id = GenericId::None;
  25. // The first block of the constraint-with-self body.
  26. InstBlockId body_block_with_self_id = InstBlockId::None;
  27. // The implicit `Self` parameter. This is a SymbolicBinding instruction.
  28. InstId self_param_id = InstId::None;
  29. // The following members are set at the `}` of the constraint definition.
  30. RequireImplsBlockId require_impls_block_id = RequireImplsBlockId::None;
  31. bool complete = false;
  32. };
  33. // A named constraint. See EntityWithParamsBase regarding the inheritance here.
  34. struct NamedConstraint : public EntityWithParamsBase,
  35. public NamedConstraintFields,
  36. public Printable<NamedConstraint> {
  37. auto Print(llvm::raw_ostream& out) const -> void {
  38. out << "{";
  39. PrintBaseFields(out);
  40. out << ", require_impls_block_id: " << require_impls_block_id;
  41. out << "}";
  42. }
  43. // This is false until we reach the `}` of the constraint definition.
  44. auto is_complete() const -> bool { return complete; }
  45. // Determines whether we're currently defining the constraint. This is true
  46. // between the braces of the constraint.
  47. auto is_being_defined() const -> bool {
  48. return has_definition_started() && !is_complete();
  49. }
  50. };
  51. using NamedConstraintStore =
  52. ValueStore<NamedConstraintId, NamedConstraint, Tag<CheckIRId>>;
  53. } // namespace Carbon::SemIR
  54. #endif // CARBON_TOOLCHAIN_SEM_IR_NAMED_CONSTRAINT_H_