impl.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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/facet_type_info.h"
  7. #include "toolchain/sem_ir/file.h"
  8. #include "toolchain/sem_ir/specific_interface.h"
  9. #include "toolchain/sem_ir/specific_named_constraint.h"
  10. namespace Carbon::SemIR {
  11. ImplStore::ImplStore(File& sem_ir)
  12. : sem_ir_(sem_ir), values_(sem_ir.check_ir_id()) {}
  13. auto ImplStore::GetOrAddLookupBucket(const Impl& impl) -> LookupBucketRef {
  14. auto self_const_id = sem_ir_.constant_values().Get(impl.self_id);
  15. auto impl_as_interface = SpecificInterface::None;
  16. auto facet_type_type_id = TypeId::ForTypeConstant(
  17. sem_ir_.constant_values().Get(impl.constraint_id));
  18. if (auto facet_type =
  19. sem_ir_.types().TryGetAs<FacetType>(facet_type_type_id)) {
  20. auto identified_id = sem_ir_.identified_facet_types().Lookup(
  21. {facet_type->facet_type_id, self_const_id});
  22. if (identified_id.has_value()) {
  23. const auto& identified =
  24. sem_ir_.identified_facet_types().Get(identified_id);
  25. if (identified.is_valid_impl_as_target()) {
  26. impl_as_interface = identified.impl_as_target_interface();
  27. }
  28. }
  29. }
  30. return LookupBucketRef(
  31. *this, lookup_
  32. .Insert(std::pair{self_const_id, impl_as_interface},
  33. [] { return ImplOrLookupBucketId::None; })
  34. .value());
  35. }
  36. } // namespace Carbon::SemIR