Repository: carbon-language/carbon-lang
File: toolchain/sem_ir/formatter.cpp (C++)

Code before:
  out_ << " ";
  FormatArg(inst.callee_id);

  if (!inst.args_id.has_value()) {
    out_ << "(<none>)";
    return;
  }

  llvm::ArrayRef<InstId> args = sem_ir_->inst_blocks().Get(inst.args_id);

  // If there's a return argument, don't print it here, because it's printed on
  // the LHS.
  int return_arg_index = -1;
  // TODO: Should GetCalleeAsFunction handle errors better?
  if (sem_ir_->constant_values().Get(inst.callee_id) !=
      SemIR::ErrorInst::ConstantId) {
    auto callee_function = SemIR::GetCalleeAsFunction(*sem_ir_, inst.callee_id);
    auto function = sem_ir_->functions().Get(callee_function.function_id);
    auto return_form_id = function.GetDeclaredReturnForm(
        *sem_ir_, callee_function.resolved_specific_id);
    if (return_form_id.has_value()) {
      if (auto init_form =
              sem_ir_->insts().TryGetAs<SemIR::InitForm>(return_form_id)) {
        auto type_id = sem_ir_->types().GetTypeIdForTypeInstId(
            init_form->type_component_inst_id);
        if (SemIR::InitRepr::ForType(*sem_ir_, type_id).MightBeInPlace()) {
          return_arg_index = init_form->index.index;
        }
      }
    }
  }

  llvm::ListSeparator sep;
  out_ << '(';
  for (auto [i, inst_id] : llvm::enumerate(args)) {
    if (static_cast<int>(i) == return_arg_index) {
      continue;
    }
    out_ << sep;
    FormatArg(inst_id);
  }
  out_ << ')';
}

auto Formatter::FormatImportCppDeclRhs() -> void {
  out_ << " ";
  OpenBrace();
  for (const Parse::Tree::PackagingNames& import :
       sem_ir_->parse_tree().imports()) {
    if (import.package_id != PackageNameId::Cpp) {
      continue;

Review comment (suggestion):
This file is in `SemIR`

```suggestion
      ErrorInst::ConstantId) {
    auto callee_function = GetCalleeAsFunction(*sem_ir_, inst.callee_id);
    auto function = sem_ir_->functions().Get(callee_function.function_id);
    auto return_form_id = function.GetDeclaredReturnForm(
        *sem_ir_, callee_function.resolved_specific_id);
    if (return_form_id.has_value()) {
      if (auto init_form =
              sem_ir_->insts().TryGetAs<InitForm>(return_form_id)) {
        auto type_id = sem_ir_->types().GetTypeIdForTypeInstId(
            init_form->type_component_inst_id);
        if (InitRepr::ForType(*sem_ir_, type_id).MightBeInPlace()) {
```

Code after:
  out_ << " ";
  FormatArg(inst.callee_id);

  if (!inst.args_id.has_value()) {
    out_ << "(<none>)";
    return;
  }

  llvm::ArrayRef<InstId> args = sem_ir_->inst_blocks().Get(inst.args_id);

  // If there's a return argument, don't print it here, because it's printed on
  // the LHS.
  int return_arg_index = -1;
  auto callee = GetCallee(*sem_ir_, inst.callee_id);
  if (auto* callee_function = std::get_if<CalleeFunction>(&callee)) {
    auto function = sem_ir_->functions().Get(callee_function->function_id);
    auto return_form_id = function.GetDeclaredReturnForm(
        *sem_ir_, callee_function->resolved_specific_id);
    if (return_form_id.has_value()) {
      if (auto init_form =
              sem_ir_->insts().TryGetAs<InitForm>(return_form_id)) {
        auto type_id = sem_ir_->types().GetTypeIdForTypeInstId(
            init_form->type_component_inst_id);
        if (InitRepr::ForType(*sem_ir_, type_id).MightBeInPlace()) {
          return_arg_index = init_form->index.index;
        }
      }
    }
  }

  llvm::ListSeparator sep;
  out_ << '(';
  for (auto [i, inst_id] : llvm::enumerate(args)) {
    if (static_cast<int>(i) == return_arg_index) {
      continue;
    }
    out_ << sep;
    FormatArg(inst_id);
  }
  out_ << ')';
}

auto Formatter::FormatImportCppDeclRhs() -> void {
  out_ << " ";
  OpenBrace();
  for (const Parse::Tree::PackagingNames& import :
       sem_ir_->parse_tree().imports()) {
    if (import.package_id != PackageNameId::Cpp) {
      continue;
    }