eval.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #ifndef CARBON_TOOLCHAIN_CHECK_EVAL_H_
  5. #define CARBON_TOOLCHAIN_CHECK_EVAL_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. #include "toolchain/sem_ir/inst.h"
  9. #include "toolchain/sem_ir/inst_kind.h"
  10. namespace Carbon::Check {
  11. // Adds a `ConstantId` for a constant that has been imported from another IR.
  12. // Does not evaluate the instruction, instead trusting that it is already in a
  13. // suitable form, but does canonicalize the operands if necessary.
  14. // TODO: Rely on import to canonicalize the operands to avoid this work.
  15. auto AddImportedConstant(Context& context, SemIR::Inst inst)
  16. -> SemIR::ConstantId;
  17. // Evaluates the instruction `inst`. If `inst_id` is specified, it is the ID of
  18. // the instruction; otherwise, evaluation of the instruction must not require an
  19. // `InstId` to be provided.
  20. auto TryEvalInstUnsafe(Context& context, SemIR::InstId inst_id,
  21. SemIR::Inst inst) -> SemIR::ConstantId;
  22. // Determines the phase of the instruction `inst_id`, and returns its constant
  23. // value if it has constant phase. If it has runtime phase, returns
  24. // `SemIR::ConstantId::NotConstant`.
  25. inline auto TryEvalInst(Context& context, SemIR::InstId inst_id)
  26. -> SemIR::ConstantId {
  27. return TryEvalInstUnsafe(context, inst_id, context.insts().Get(inst_id));
  28. }
  29. // Same, but for a typed instruction that doesn't have an InstId assigned yet,
  30. // in the case where evaluation doesn't need an InstId. This can be used to
  31. // avoid allocating an instruction in the case where you just want a constant
  32. // value and the instruction is known to not matter. However, even then care
  33. // should be taken: if the produced constant is symbolic, you may still need an
  34. // instruction to associate the constant with the enclosing generic.
  35. //
  36. // To evaluate an instruction and add it to SemIR only if necessary, use
  37. // EvalOrAddInst instead.
  38. template <typename InstT>
  39. requires(InstT::Kind.constant_needs_inst_id() ==
  40. SemIR::InstConstantNeedsInstIdKind::No)
  41. auto TryEvalInst(Context& context, InstT inst) -> SemIR::ConstantId {
  42. return TryEvalInstUnsafe(context, SemIR::InstId::None, inst);
  43. }
  44. // Evaluates the eval block for a region of a specific. Produces a block
  45. // containing the evaluated constant values of the instructions in the eval
  46. // block.
  47. auto TryEvalBlockForSpecific(Context& context, SemIR::LocId loc_id,
  48. SemIR::SpecificId specific_id,
  49. SemIR::GenericInstIndex::Region region)
  50. -> SemIR::InstBlockId;
  51. } // namespace Carbon::Check
  52. #endif // CARBON_TOOLCHAIN_CHECK_EVAL_H_