constant.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  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/constant.h"
  5. #include "toolchain/sem_ir/file.h"
  6. namespace Carbon::SemIR {
  7. auto ConstantStore::GetOrAdd(Inst inst, bool is_symbolic) -> ConstantId {
  8. auto result = map_.Insert(inst, [&] {
  9. auto inst_id = sem_ir_.insts().AddInNoBlock(LocIdAndInst::NoLoc(inst));
  10. ConstantId const_id = ConstantId::Invalid;
  11. if (is_symbolic) {
  12. // The instruction in the constants store is an abstract symbolic
  13. // constant, not associated with any particular generic.
  14. auto symbolic_constant =
  15. SymbolicConstant{.inst_id = inst_id,
  16. .generic_id = GenericId::Invalid,
  17. .index = GenericInstIndex::Invalid};
  18. const_id =
  19. sem_ir_.constant_values().AddSymbolicConstant(symbolic_constant);
  20. } else {
  21. const_id = SemIR::ConstantId::ForTemplateConstant(inst_id);
  22. }
  23. sem_ir_.constant_values().Set(inst_id, const_id);
  24. constants_.push_back(inst_id);
  25. return const_id;
  26. });
  27. CARBON_CHECK(result.value() != ConstantId::Invalid);
  28. CARBON_CHECK(result.value().is_symbolic() == is_symbolic);
  29. return result.value();
  30. }
  31. } // namespace Carbon::SemIR