constant.cpp 1002 B

12345678910111213141516171819202122232425262728
  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 [it, added] = map_.insert({inst, ConstantId::Invalid});
  9. if (added) {
  10. auto inst_id = sem_ir_.insts().AddInNoBlock(LocIdAndInst::NoLoc(inst));
  11. auto const_id = is_symbolic
  12. ? SemIR::ConstantId::ForSymbolicConstant(inst_id)
  13. : SemIR::ConstantId::ForTemplateConstant(inst_id);
  14. it->second = const_id;
  15. sem_ir_.constant_values().Set(inst_id, const_id);
  16. constants_.push_back(inst_id);
  17. } else {
  18. CARBON_CHECK(it->second != ConstantId::Invalid);
  19. CARBON_CHECK(it->second.is_symbolic() == is_symbolic);
  20. }
  21. return it->second;
  22. }
  23. } // namespace Carbon::SemIR