constant.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. auto GetInstWithConstantValue(const File& file, ConstantId const_id) -> InstId {
  35. if (!const_id.has_value() || !const_id.is_constant()) {
  36. return InstId::None;
  37. }
  38. // For concrete constants, the corresponding instruction has the desired
  39. // constant value.
  40. if (!const_id.is_symbolic()) {
  41. return file.constant_values().GetInstId(const_id);
  42. }
  43. // For unattached symbolic constants, the corresponding instruction has the
  44. // desired constant value.
  45. const auto& symbolic_const =
  46. file.constant_values().GetSymbolicConstant(const_id);
  47. if (!symbolic_const.generic_id.has_value()) {
  48. return file.constant_values().GetInstId(const_id);
  49. }
  50. // For attached symbolic constants, pick the corresponding instruction out of
  51. // the eval block for the generic.
  52. const auto& generic = file.generics().Get(symbolic_const.generic_id);
  53. auto block = generic.GetEvalBlock(symbolic_const.index.region());
  54. return file.inst_blocks().Get(block)[symbolic_const.index.index()];
  55. }
  56. } // namespace Carbon::SemIR