Repository: carbon-language/carbon-lang
File: toolchain/check/inst.cpp (C++)

Code before:
      return context.constant_values().Get(inst_id);
    }

    case SemIR::InstConstantNeedsInstIdKind::Permanent: {
      // Evaluation needs a permanent InstId. Add the instruction.
      auto inst_id = AddInst(context, loc_id_and_inst);
      return context.constant_values().Get(inst_id);
    }
  }
}

auto AddPlaceholderInstInNoBlock(Context& context,
                                 SemIR::LocIdAndInst loc_id_and_inst)
    -> SemIR::InstId {
  auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  CARBON_VLOG_TO(context.vlog_stream(), "AddPlaceholderInst: {0}\n",
                 loc_id_and_inst.inst);
  context.constant_values().Set(inst_id, SemIR::ConstantId::None);
  return inst_id;
}

auto AddPlaceholderInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
    -> SemIR::InstId {
  auto inst_id = AddPlaceholderInstInNoBlock(context, loc_id_and_inst);
  context.inst_block_stack().AddInstId(inst_id);
  return inst_id;
}

auto ReplaceLocIdAndInstBeforeConstantUse(Context& context,
                                          SemIR::InstId inst_id,
                                          SemIR::LocIdAndInst loc_id_and_inst)
    -> void {
  context.sem_ir().insts().SetLocIdAndInst(inst_id, loc_id_and_inst);
  CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
                 loc_id_and_inst.inst);
  FinishInst(context, inst_id, loc_id_and_inst.inst);
}

auto ReplaceInstBeforeConstantUse(Context& context, SemIR::InstId inst_id,
                                  SemIR::Inst inst) -> void {
  context.sem_ir().insts().Set(inst_id, inst);
  CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
                 inst);
  FinishInst(context, inst_id, inst);
}

auto ReplaceInstPreservingConstantValue(Context& context, SemIR::InstId inst_id,
                                        SemIR::Inst inst) -> void {
  // Check that the type didn't change: a change of type will change the
  // constant value. Replace the type with the attached type.
  auto old_type_id = context.insts().GetAttachedType(inst_id);

Review comment (suggestion):
I wonder if we should drop the `InNoBlock` from this function's name, since we're putting the instruction in the imports "block" (that's not exactly a block). But maybe it's a helpful reminder that it doesn't go in the current inst block? Just a thought, I don't feel strongly either way.

Code after:
      return context.constant_values().Get(inst_id);
    }

    case SemIR::InstConstantNeedsInstIdKind::Permanent: {
      // Evaluation needs a permanent InstId. Add the instruction.
      auto inst_id = AddInst(context, loc_id_and_inst);
      return context.constant_values().Get(inst_id);
    }
  }
}

auto AddPlaceholderInstInNoBlock(Context& context,
                                 SemIR::LocIdAndInst loc_id_and_inst)
    -> SemIR::InstId {
  auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  CARBON_VLOG_TO(context.vlog_stream(), "AddPlaceholderInst: {0}\n",
                 loc_id_and_inst.inst);
  context.constant_values().Set(inst_id, SemIR::ConstantId::None);
  return inst_id;
}

auto AddPlaceholderImportedInstInNoBlock(Context& context,
                                         SemIR::LocIdAndInst loc_id_and_inst)
    -> SemIR::InstId {
  auto inst_id = AddPlaceholderInstInNoBlock(context, loc_id_and_inst);
  context.imports().push_back(inst_id);
  return inst_id;
}

auto AddPlaceholderInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
    -> SemIR::InstId {
  auto inst_id = AddPlaceholderInstInNoBlock(context, loc_id_and_inst);
  context.inst_block_stack().AddInstId(inst_id);
  return inst_id;
}

auto ReplaceLocIdAndInstBeforeConstantUse(Context& context,
                                          SemIR::InstId inst_id,
                                          SemIR::LocIdAndInst loc_id_and_inst)
    -> void {
  context.sem_ir().insts().SetLocIdAndInst(inst_id, loc_id_and_inst);
  CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
                 loc_id_and_inst.inst);
  FinishInst(context, inst_id, loc_id_and_inst.inst);
}

auto ReplaceInstBeforeConstantUse(Context& context, SemIR::InstId inst_id,
                                  SemIR::Inst inst) -> void {
  context.sem_ir().insts().Set(inst_id, inst);
  CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
                 inst);