constant.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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, PhaseKind phase) -> ConstantId {
  8. auto result = map_.Insert(inst, [&] {
  9. auto inst_id = sem_ir_->insts().AddInNoBlock(LocIdAndInst::NoLoc(inst));
  10. ConstantId const_id = ConstantId::None;
  11. if (phase == IsTemplate) {
  12. const_id = SemIR::ConstantId::ForTemplateConstant(inst_id);
  13. } else {
  14. // The instruction in the constants store is an abstract symbolic
  15. // constant, not associated with any particular generic.
  16. SymbolicConstant symbolic_constant = {
  17. .inst_id = inst_id,
  18. .generic_id = GenericId::None,
  19. .index = GenericInstIndex::None,
  20. .period_self_only = (phase == IsPeriodSelfSymbolic)};
  21. const_id =
  22. sem_ir_->constant_values().AddSymbolicConstant(symbolic_constant);
  23. }
  24. sem_ir_->constant_values().Set(inst_id, const_id);
  25. constants_.push_back(inst_id);
  26. return const_id;
  27. });
  28. CARBON_CHECK(result.value() != ConstantId::None);
  29. CARBON_CHECK(
  30. result.value().is_symbolic() == (phase != IsTemplate),
  31. "Constant {0} registered as both symbolic and template constant.", inst);
  32. return result.value();
  33. }
  34. } // namespace Carbon::SemIR