eval.h 2.8 KB

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