generic.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include "toolchain/sem_ir/file.h"
  6. #include "toolchain/sem_ir/typed_insts.h"
  7. namespace Carbon::SemIR {
  8. class SpecificStore::KeyContext : public TranslatingKeyContext<KeyContext> {
  9. public:
  10. // A lookup key for a specific.
  11. struct Key {
  12. GenericId generic_id;
  13. InstBlockId args_id;
  14. friend auto operator==(const Key&, const Key&) -> bool = default;
  15. };
  16. explicit KeyContext(llvm::ArrayRef<Specific> specifics)
  17. : specifics_(specifics) {}
  18. auto TranslateKey(SpecificId id) const -> Key {
  19. const auto& specific = specifics_[id.index];
  20. return {.generic_id = specific.generic_id, .args_id = specific.args_id};
  21. }
  22. private:
  23. llvm::ArrayRef<Specific> specifics_;
  24. };
  25. auto SpecificStore::GetOrAdd(GenericId generic_id, InstBlockId args_id)
  26. -> SpecificId {
  27. CARBON_CHECK(generic_id.has_value());
  28. return lookup_table_
  29. .Insert(
  30. KeyContext::Key{.generic_id = generic_id, .args_id = args_id},
  31. [&] {
  32. return specifics_.Add(
  33. {.generic_id = generic_id, .args_id = args_id});
  34. },
  35. KeyContext(specifics_.array_ref()))
  36. .key();
  37. }
  38. auto SpecificStore::CollectMemUsage(MemUsage& mem_usage,
  39. llvm::StringRef label) const -> void {
  40. mem_usage.Collect(MemUsage::ConcatLabel(label, "specifics_"), specifics_);
  41. mem_usage.Collect(MemUsage::ConcatLabel(label, "lookup_table_"),
  42. lookup_table_, KeyContext(specifics_.array_ref()));
  43. }
  44. auto GetConstantInSpecific(const File& sem_ir, SpecificId specific_id,
  45. ConstantId const_id) -> ConstantId {
  46. if (!const_id.is_symbolic()) {
  47. // Type does not depend on a generic parameter.
  48. return const_id;
  49. }
  50. const auto& symbolic = sem_ir.constant_values().GetSymbolicConstant(const_id);
  51. if (!symbolic.generic_id.has_value()) {
  52. // Constant is an abstract symbolic constant, not associated with some
  53. // particular generic.
  54. return const_id;
  55. }
  56. if (!specific_id.has_value()) {
  57. // We have a generic constant but no specific. We treat as a request for the
  58. // canonical value of the constant.
  59. return sem_ir.constant_values().Get(symbolic.inst_id);
  60. }
  61. const auto& specific = sem_ir.specifics().Get(specific_id);
  62. CARBON_CHECK(specific.generic_id == symbolic.generic_id,
  63. "Given a specific for the wrong generic");
  64. auto value_block_id = specific.GetValueBlock(symbolic.index.region());
  65. CARBON_CHECK(
  66. value_block_id.has_value(),
  67. "Queried {0} in {1} for {2} before it was resolved.", symbolic.index,
  68. specific_id,
  69. sem_ir.insts().Get(sem_ir.generics().Get(specific.generic_id).decl_id));
  70. return sem_ir.constant_values().Get(
  71. sem_ir.inst_blocks().Get(value_block_id)[symbolic.index.index()]);
  72. }
  73. auto GetConstantValueInSpecific(const File& sem_ir, SpecificId specific_id,
  74. InstId inst_id) -> ConstantId {
  75. return GetConstantInSpecific(sem_ir, specific_id,
  76. sem_ir.constant_values().Get(inst_id));
  77. }
  78. auto GetTypeOfInstInSpecific(const File& sem_ir, SpecificId specific_id,
  79. InstId inst_id) -> TypeId {
  80. auto type_id = sem_ir.insts().Get(inst_id).type_id();
  81. auto const_id = sem_ir.types().GetConstantId(type_id);
  82. auto specific_const_id = GetConstantInSpecific(sem_ir, specific_id, const_id);
  83. return TypeId::ForTypeConstant(specific_const_id);
  84. }
  85. } // namespace Carbon::SemIR