generic.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. namespace Carbon::SemIR {
  7. class SpecificStore::KeyContext : public TranslatingKeyContext<KeyContext> {
  8. public:
  9. // A lookup key for a specific.
  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<Specific> specifics)
  16. : specifics_(specifics) {}
  17. auto TranslateKey(SpecificId id) const -> Key {
  18. const auto& specific = specifics_[id.index];
  19. return {.generic_id = specific.generic_id, .args_id = specific.args_id};
  20. }
  21. private:
  22. llvm::ArrayRef<Specific> specifics_;
  23. };
  24. auto SpecificStore::GetOrAdd(GenericId generic_id, InstBlockId args_id)
  25. -> SpecificId {
  26. return lookup_table_
  27. .Insert(
  28. KeyContext::Key{.generic_id = generic_id, .args_id = args_id},
  29. [&] {
  30. return specifics_.Add(
  31. {.generic_id = generic_id, .args_id = args_id});
  32. },
  33. KeyContext(specifics_.array_ref()))
  34. .key();
  35. }
  36. auto SpecificStore::CollectMemUsage(MemUsage& mem_usage,
  37. llvm::StringRef label) const -> void {
  38. mem_usage.Collect(MemUsage::ConcatLabel(label, "specifics_"), specifics_);
  39. mem_usage.Add(MemUsage::ConcatLabel(label, "lookup_table_"), lookup_table_,
  40. KeyContext(specifics_.array_ref()));
  41. }
  42. auto GetConstantInSpecific(const File& sem_ir, SpecificId specific_id,
  43. ConstantId const_id) -> ConstantId {
  44. if (!const_id.is_symbolic()) {
  45. // Type does not depend on a generic parameter.
  46. return const_id;
  47. }
  48. const auto& symbolic = sem_ir.constant_values().GetSymbolicConstant(const_id);
  49. if (!symbolic.generic_id.is_valid()) {
  50. // Constant is an abstract symbolic constant, not associated with some
  51. // particular generic.
  52. return const_id;
  53. }
  54. if (!specific_id.is_valid()) {
  55. // TODO: We have a generic constant but no specific. Investigate whether we
  56. // can CHECK-fail here. For now, produce the canonical value of the
  57. // constant.
  58. return sem_ir.constant_values().Get(symbolic.inst_id);
  59. }
  60. const auto& specific = sem_ir.specifics().Get(specific_id);
  61. if (specific.generic_id != symbolic.generic_id) {
  62. // TODO: Given an specific for the wrong generic. If the symbolic constant
  63. // is from an enclosing generic, take the value from the corresponding
  64. // specific. Otherwise, CHECK-fail.
  65. return sem_ir.constant_values().Get(symbolic.inst_id);
  66. }
  67. auto value_block_id = specific.GetValueBlock(symbolic.index.region());
  68. CARBON_CHECK(value_block_id.is_valid())
  69. << "Queried region of " << specific_id << " before it was resolved.";
  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 GetTypeInSpecific(const File& sem_ir, SpecificId specific_id,
  79. TypeId type_id) -> TypeId {
  80. return TypeId::ForTypeConstant(GetConstantInSpecific(
  81. sem_ir, specific_id, sem_ir.types().GetConstantId(type_id)));
  82. }
  83. } // namespace Carbon::SemIR