interface.h 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_CHECK_INTERFACE_H_
  5. #define CARBON_TOOLCHAIN_CHECK_INTERFACE_H_
  6. #include <optional>
  7. #include "toolchain/check/context.h"
  8. #include "toolchain/check/decl_name_stack.h"
  9. #include "toolchain/check/name_component.h"
  10. #include "toolchain/parse/node_ids.h"
  11. #include "toolchain/sem_ir/entity_with_params_base.h"
  12. #include "toolchain/sem_ir/ids.h"
  13. #include "toolchain/sem_ir/name_scope.h"
  14. #include "toolchain/sem_ir/typed_insts.h"
  15. namespace Carbon::Check {
  16. // Builds and returns an associated entity for `interface_id` corresponding to
  17. // the declaration `decl_id`, which can be an associated function or an
  18. // associated constant. Registers the associated entity in the list for the
  19. // interface.
  20. auto BuildAssociatedEntity(Context& context, SemIR::InterfaceId interface_id,
  21. SemIR::InstId decl_id) -> SemIR::InstId;
  22. // Construct a facet value that can be used for the `Self` binding of entities
  23. // inside an interface.
  24. //
  25. // The `interface_specific_id` is the specific of the interface around the
  26. // `Self`. The `generic_id` is for member of the interface that the `Self` value
  27. // will be for. The `self_witness_id` is an impl witness for the interface that
  28. // the `self_type_id` implements that interface. It should come from an impl
  29. // definition with the given self-type and the interface as its constraint.
  30. //
  31. // The returned facet value can be used as the `Self` value in a specific for
  32. // the generic member of the interface, and can appear in its specific. As such,
  33. // this is a building block of GetSelfSpecificForInterfaceMemberWithSelfType.
  34. auto GetSelfFacetValueForInterfaceMemberSpecific(
  35. Context& context, SemIR::SpecificId interface_specific_id,
  36. SemIR::GenericId generic_id, SemIR::TypeId self_type_id,
  37. SemIR::InstId self_witness_id) -> SemIR::InstId;
  38. // Gets the self specific of a generic declaration that is an interface member,
  39. // given a specific for the interface plus a type to use as `Self`.
  40. auto GetSelfSpecificForInterfaceMemberWithSelfType(
  41. Context& context, SemIR::LocId loc_id,
  42. SemIR::SpecificId interface_specific_id, SemIR::GenericId generic_id,
  43. SemIR::SpecificId enclosing_specific_id, SemIR::TypeId self_type_id,
  44. SemIR::InstId witness_inst_id) -> SemIR::SpecificId;
  45. // Gets the type of the specified associated entity, given the specific for the
  46. // interface and the type of `Self`.
  47. auto GetTypeForSpecificAssociatedEntity(Context& context, SemIR::LocId loc_id,
  48. SemIR::SpecificId interface_specific_id,
  49. SemIR::InstId decl_id,
  50. SemIR::TypeId self_type_id,
  51. SemIR::InstId self_witness_id)
  52. -> SemIR::TypeId;
  53. // Creates a symbolic binding for `Self` of type `type_id` in the scope of
  54. // `scope_id`, and add the name `Self` for the compile time binding.
  55. //
  56. // Returns the symbolic binding instruction.
  57. auto AddSelfGenericParameter(Context& context, SemIR::LocId definition_loc_id,
  58. SemIR::TypeId type_id, SemIR::NameScopeId scope_id,
  59. bool is_template) -> SemIR::InstId;
  60. // Given a search result `lookup_result` for `name`, returns the previous valid
  61. // declaration of `name` if there is one. The `entity` is a new decl of the same
  62. // `name`, and the existing decl need to be of the same entity type. Otherwise,
  63. // produces diagnostics if needed and returns nullopt.
  64. template <typename EntityT>
  65. requires SameAsOneOf<EntityT, SemIR::Interface, SemIR::NamedConstraint>
  66. auto TryGetExistingDecl(Context& context, const NameComponent& name,
  67. SemIR::ScopeLookupResult lookup_result,
  68. const EntityT& entity, bool is_definition)
  69. -> std::optional<SemIR::Inst>;
  70. } // namespace Carbon::Check
  71. #endif // CARBON_TOOLCHAIN_CHECK_INTERFACE_H_