generic.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. static auto GetConstantInSpecific(const File& sem_ir, SpecificId specific_id,
  45. ConstantId const_id) -> ConstantId {
  46. if (!const_id.is_symbolic()) {
  47. // The constant 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. // The constant is an unattached 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 this as a request
  58. // for the value that should be used within the generic itself, which is the
  59. // unattached constant.
  60. return sem_ir.constant_values().Get(symbolic.inst_id);
  61. }
  62. const auto& specific = sem_ir.specifics().Get(specific_id);
  63. CARBON_CHECK(specific.generic_id == symbolic.generic_id,
  64. "Given a specific for the wrong generic");
  65. auto value_block_id = specific.GetValueBlock(symbolic.index.region());
  66. if (!value_block_id.has_value()) {
  67. // For the self specific, we can see queries before the definition is
  68. // resolved. Return the unattached constant value.
  69. CARBON_CHECK(
  70. sem_ir.generics().GetSelfSpecific(symbolic.generic_id) == specific_id,
  71. "Queried {0} in {1} for {2} before it was resolved.", symbolic.index,
  72. specific_id,
  73. sem_ir.insts().Get(sem_ir.generics().Get(specific.generic_id).decl_id));
  74. // TODO: Make sure this is the same value that we put in the self specific
  75. // when it's resolved. Consider not building value blocks for a self
  76. // specific.
  77. return sem_ir.constant_values().Get(symbolic.inst_id);
  78. }
  79. return sem_ir.constant_values().Get(
  80. sem_ir.inst_blocks().Get(value_block_id)[symbolic.index.index()]);
  81. }
  82. auto GetConstantValueInSpecific(const File& sem_ir, SpecificId specific_id,
  83. InstId inst_id) -> ConstantId {
  84. return GetConstantInSpecific(sem_ir, specific_id,
  85. sem_ir.constant_values().GetAttached(inst_id));
  86. }
  87. auto GetTypeOfInstInSpecific(const File& sem_ir, SpecificId specific_id,
  88. InstId inst_id) -> TypeId {
  89. auto type_id = sem_ir.insts().GetAttachedType(inst_id);
  90. auto const_id = sem_ir.types().GetConstantId(type_id);
  91. auto specific_const_id = GetConstantInSpecific(sem_ir, specific_id, const_id);
  92. return TypeId::ForTypeConstant(specific_const_id);
  93. }
  94. } // namespace Carbon::SemIR