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

Code before:
                         SemIR::EntityNameId entity_name_id =
                             SemIR::EntityNameId::None) -> SemIR::InstId {
  return AddImportRef(context.local_context(),
                      {.ir_id = context.import_ir_id(), .inst_id = inst_id},
                      entity_name_id);
}

static auto AddLoadedImportRef(ImportContext& context, SemIR::TypeId type_id,
                               SemIR::InstId inst_id,
                               SemIR::ConstantId const_id) -> SemIR::InstId {
  return AddLoadedImportRef(
      context.local_context(), type_id,
      {.ir_id = context.import_ir_id(), .inst_id = inst_id}, const_id);
}

static auto AddImportIRInst(ImportContext& context, SemIR::InstId inst_id)
    -> SemIR::ImportIRInstId {
  return context.local_import_ir_insts().Add(
      {.ir_id = context.import_ir_id(), .inst_id = inst_id});
}

// Sets the constant value for an instruction and returns it. The constant value
// is computed if not explicitly specified.
static auto ComputeOrSetConstantValue(
    Context& context, SemIR::InstId inst_id, SemIR::Inst inst,
    std::optional<SemIR::ConstantId> const_id = std::nullopt)
    -> SemIR::ConstantId {
  if (!const_id) {
    const_id = TryEvalInstUnsafe(context, inst_id, inst);
  }
  if (const_id->has_value()) {
    CARBON_VLOG_TO(context.vlog_stream(), "Constant: {0} -> {1}\n", inst,
                   context.constant_values().GetInstId(*const_id));
  }
  context.constant_values().Set(inst_id, *const_id);
  return *const_id;
}

// Adds an imported instruction. The constant value of the instruction can be
// specified if it is known, otherwise it will be computed.
//
// Normally `AddImportedConstant` should be used to create the result of
// resolving a constant, for example by calling `ResolveAs*`. However, we
// sometimes need to create a new instruction, not just obtain a constant value,
// such as when importing a declaration or a pattern. In that case, this
// function should be used to create it.
//
// Do not use `AddInst*`, as it will add symbolic constants to the eval block of
// whatever generic the import happens within.
template <typename InstT>
static auto AddImportedInst(

Review comment (suggestion):
Unused default?

```suggestion
    std::optional<SemIR::ConstantId> const_id)
```

Code after:
                         SemIR::EntityNameId entity_name_id =
                             SemIR::EntityNameId::None) -> SemIR::InstId {
  return AddImportRef(context.local_context(),
                      {.ir_id = context.import_ir_id(), .inst_id = inst_id},
                      entity_name_id);
}

static auto AddLoadedImportRef(ImportContext& context, SemIR::TypeId type_id,
                               SemIR::InstId inst_id,
                               SemIR::ConstantId const_id) -> SemIR::InstId {
  return AddLoadedImportRef(
      context.local_context(), type_id,
      {.ir_id = context.import_ir_id(), .inst_id = inst_id}, const_id);
}

static auto AddImportIRInst(ImportContext& context, SemIR::InstId inst_id)
    -> SemIR::ImportIRInstId {
  return context.local_import_ir_insts().Add(
      {.ir_id = context.import_ir_id(), .inst_id = inst_id});
}

// Computes, sets, and returns the constant value for an instruction.
static auto SetConstantValue(Context& context, SemIR::InstId inst_id,
                             SemIR::Inst inst) -> SemIR::ConstantId {
  auto const_id = TryEvalInstUnsafe(context, inst_id, inst);
  if (const_id.is_constant()) {
    CARBON_VLOG_TO(context.vlog_stream(), "Constant: {0} -> {1}\n", inst,
                   context.constant_values().GetInstId(const_id));
  }
  context.constant_values().Set(inst_id, const_id);
  return const_id;
}

// Adds an imported instruction without setting its constant value. The
// instruction should later be updated by either `SetConstantValue` or
// `ReplaceImportedInstAndSetConstantValue`.
template <typename InstT>
static auto AddPlaceholderImportedInst(ImportContext& context,
                                       SemIR::InstId import_inst_id, InstT inst)
    -> SemIR::InstId {
  auto inst_id = context.local_insts().AddInNoBlock(MakeImportedLocIdAndInst(
      context.local_context(), AddImportIRInst(context, import_inst_id), inst));
  CARBON_VLOG_TO(context.local_context().vlog_stream(),
                 "AddImportedInst: {0}\n", static_cast<SemIR::Inst>(inst));
  return inst_id;
}

// Adds an imported instruction. The constant value of the instruction will be
// computed.
//
// Normally `AddImportedConstant` should be used to create the result of