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

Code before:

static auto AddDependentActionSpliceImpl(Context& context,
                                         SemIR::LocIdAndInst action,
                                         SemIR::TypeId result_type_id)
    -> SemIR::InstId {
  auto inst_id = AddDependentActionInst(context, action);
  if (!result_type_id.has_value()) {
    auto type_inst_id = AddDependentActionInst(
        context, action.loc_id,
        SemIR::TypeOfInst{.type_id = SemIR::TypeType::SingletonTypeId,
                          .inst_id = inst_id});
    result_type_id = context.types().GetTypeIdForTypeInstId(type_inst_id);
  }
  return AddInst(
      context, action.loc_id,
      SemIR::SpliceInst{.type_id = result_type_id, .inst_id = inst_id});
}

// Refine one operand of an action. Given an argument from a template, this
// produces an argument that has the template-dependent parts replaced with
// their concrete values, so that the action doesn't need to know which specific
// it is operating on.
static auto RefineOperand(Context& context, SemIR::LocId loc_id,
                          SemIR::Inst::ArgAndKind arg) -> int32_t {
  if (arg.kind == SemIR::IdKind::For<SemIR::MetaInstId>) {
    auto inst_id = arg.As<SemIR::MetaInstId>();
    auto inst = context.insts().Get(inst_id);
    if (inst.Is<SemIR::SpliceInst>()) {
      // The argument will evaluate to the spliced instruction, which is already
      // refined.
      return arg.value;
    }

    // If the type of the action argument is dependent, refine to an instruction
    // with a concrete type.
    if (OperandIsDependent(context, inst.type_id())) {
      inst_id = AddDependentActionSpliceImpl(
          context,
          SemIR::LocIdAndInst(loc_id,
                              SemIR::RefineTypeAction{
                                  .type_id = SemIR::InstType::SingletonTypeId,
                                  .inst_id = inst_id,
                                  .inst_type_id = inst.type_id()}),
          inst.type_id());
    }

    // TODO: Handle the case where the constant value of the instruction is
    // template-dependent.

    return inst_id.index;
  }

Review comment (suggestion):
This suggests a `TryAs` function would be useful:
```suggestion
  if (auto inst_id = arg.TryAs<SemIR::MetaInstId>()) {
```

Code after:

static auto AddDependentActionSpliceImpl(Context& context,
                                         SemIR::LocIdAndInst action,
                                         SemIR::TypeId result_type_id)
    -> SemIR::InstId {
  auto inst_id = AddDependentActionInst(context, action);
  if (!result_type_id.has_value()) {
    auto type_inst_id = AddDependentActionInst(
        context, action.loc_id,
        SemIR::TypeOfInst{.type_id = SemIR::TypeType::SingletonTypeId,
                          .inst_id = inst_id});
    result_type_id = context.types().GetTypeIdForTypeInstId(type_inst_id);
  }
  return AddInst(
      context, action.loc_id,
      SemIR::SpliceInst{.type_id = result_type_id, .inst_id = inst_id});
}

// Refine one operand of an action. Given an argument from a template, this
// produces an argument that has the template-dependent parts replaced with
// their concrete values, so that the action doesn't need to know which specific
// it is operating on.
static auto RefineOperand(Context& context, SemIR::LocId loc_id,
                          SemIR::Inst::ArgAndKind arg) -> int32_t {
  if (auto inst_id = arg.TryAs<SemIR::MetaInstId>()) {
    auto inst = context.insts().Get(*inst_id);
    if (inst.Is<SemIR::SpliceInst>()) {
      // The argument will evaluate to the spliced instruction, which is already
      // refined.
      return arg.value;
    }

    // If the type of the action argument is dependent, refine to an instruction
    // with a concrete type.
    if (OperandIsDependent(context, inst.type_id())) {
      inst_id = AddDependentActionSpliceImpl(
          context,
          SemIR::LocIdAndInst(loc_id,
                              SemIR::RefineTypeAction{
                                  .type_id = SemIR::InstType::SingletonTypeId,
                                  .inst_id = *inst_id,
                                  .inst_type_id = inst.type_id()}),
          inst.type_id());
    }

    // TODO: Handle the case where the constant value of the instruction is
    // template-dependent.

    return inst_id->index;
  }