impl.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. #include "toolchain/sem_ir/impl.h"
  5. #include "toolchain/base/kind_switch.h"
  6. #include "toolchain/sem_ir/file.h"
  7. #include "toolchain/sem_ir/specific_interface.h"
  8. #include "toolchain/sem_ir/specific_named_constraint.h"
  9. namespace Carbon::SemIR {
  10. ImplStore::ImplStore(File& sem_ir)
  11. : sem_ir_(sem_ir), values_(sem_ir.check_ir_id()) {}
  12. auto ImplStore::GetOrAddLookupBucket(const Impl& impl) -> LookupBucketRef {
  13. auto self_id = sem_ir_.constant_values().GetConstantInstId(impl.self_id);
  14. InterfaceId interface_id = InterfaceId::None;
  15. SpecificId specific_id = SpecificId::None;
  16. auto facet_type_id = TypeId::ForTypeConstant(
  17. sem_ir_.constant_values().Get(impl.constraint_id));
  18. if (auto facet_type = sem_ir_.types().TryGetAs<FacetType>(facet_type_id)) {
  19. const FacetTypeInfo& facet_type_info =
  20. sem_ir_.facet_types().Get(facet_type->facet_type_id);
  21. if (auto single = facet_type_info.TryAsSingleExtend()) {
  22. CARBON_KIND_SWITCH(*single) {
  23. case CARBON_KIND(SemIR::SpecificInterface interface): {
  24. interface_id = interface.interface_id;
  25. specific_id = interface.specific_id;
  26. break;
  27. }
  28. case CARBON_KIND(SemIR::SpecificNamedConstraint _): {
  29. // TODO: Handle named constraints which resolve to a single interface
  30. // in the IdentifiedFacetType.
  31. break;
  32. }
  33. }
  34. }
  35. }
  36. return LookupBucketRef(
  37. *this, lookup_
  38. .Insert(std::tuple{self_id, interface_id, specific_id},
  39. [] { return ImplOrLookupBucketId::None; })
  40. .value());
  41. }
  42. } // namespace Carbon::SemIR