generic.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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/generic.h"
  5. namespace Carbon::SemIR {
  6. class GenericInstanceStore::KeyContext
  7. : public TranslatingKeyContext<KeyContext> {
  8. public:
  9. // A lookup key for a generic instance.
  10. struct Key {
  11. GenericId generic_id;
  12. InstBlockId args_id;
  13. friend auto operator==(const Key&, const Key&) -> bool = default;
  14. };
  15. explicit KeyContext(llvm::ArrayRef<GenericInstance> instances)
  16. : instances_(instances) {}
  17. auto TranslateKey(GenericInstanceId id) const -> Key {
  18. const auto& instance = instances_[id.index];
  19. return {.generic_id = instance.generic_id, .args_id = instance.args_id};
  20. }
  21. private:
  22. llvm::ArrayRef<GenericInstance> instances_;
  23. };
  24. auto GenericInstanceStore::GetOrAdd(GenericId generic_id, InstBlockId args_id)
  25. -> GenericInstanceId {
  26. return lookup_table_
  27. .Insert(
  28. KeyContext::Key{.generic_id = generic_id, .args_id = args_id},
  29. [&] {
  30. return generic_instances_.Add(
  31. {.generic_id = generic_id, .args_id = args_id});
  32. },
  33. KeyContext(generic_instances_.array_ref()))
  34. .key();
  35. }
  36. } // namespace Carbon::SemIR