constant.cpp 1.6 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, ConstantDependence dependence)
  8. -> ConstantId {
  9. auto result = map_.Insert(inst, [&] {
  10. auto inst_id = sem_ir_->insts().AddInNoBlock(LocIdAndInst::NoLoc(inst));
  11. ConstantId const_id = ConstantId::None;
  12. if (dependence == ConstantDependence::None) {
  13. const_id = ConstantId::ForConcreteConstant(inst_id);
  14. } else {
  15. // The instruction in the constants store is an abstract symbolic
  16. // constant, not associated with any particular generic.
  17. SymbolicConstant symbolic_constant = {.inst_id = inst_id,
  18. .generic_id = GenericId::None,
  19. .index = GenericInstIndex::None,
  20. .dependence = dependence};
  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() == (dependence != ConstantDependence::None),
  31. "Constant {0} registered as both symbolic and concrete constant.", inst);
  32. return result.value();
  33. }
  34. } // namespace Carbon::SemIR