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

Code before:
      clang::TemplateName name(cast<clang::TemplateDecl>(
          context.clang_decls().Get(template_name_type->decl_id).key.decl));
      return clang::TemplateArgumentLoc(
          context.ast_context(), clang::TemplateArgument(name),
          /*TemplateKWLoc=*/clang::SourceLocation(),
          clang::NestedNameSpecifierLoc(),
          GetCppLocation(context, SemIR::LocId(arg_id)));
    }

    // TODO: Eventually we should also support passing Carbon generics as
    // template template arguments.
    return MakePlaceholderTemplateArg(context, arg_id);
  }

  if (const auto* non_type =
          dyn_cast<clang::NonTypeTemplateParmDecl>(param_decl)) {
    auto param_type = non_type->getType();

    // Handle integer parameters.
    if (param_type->isIntegerType()) {
      // Get the Carbon type corresponding to the parameter's Clang type.
      const TypeExpr type_expr =
          ImportCppType(context, SemIR::LocId(arg_id), param_type);

      // Try to convert the argument to the parameter type.
      const SemIR::InstId converted_inst_id =
          Convert(context, SemIR::LocId(arg_id), arg_id,
                  {
                      .kind = ConversionTarget::Value,
                      .type_id = type_expr.type_id,
                  });

      if (converted_inst_id == SemIR::ErrorInst::InstId) {
        return std::nullopt;
      }

      auto const_inst_id =
          context.constant_values().GetConstantInstId(converted_inst_id);
      if (const_inst_id.has_value()) {
        if (auto int_value =
                context.insts().TryGetAs<SemIR::IntValue>(const_inst_id)) {
          const llvm::APInt& ap_int = context.ints().Get(int_value->int_id);
          const bool is_unsigned =
              !param_type->isSignedIntegerOrEnumerationType();
          auto aps_int =
              llvm::APSInt(ap_int, is_unsigned)
                  .extOrTrunc(context.ast_context().getIntWidth(param_type));
          auto template_arg = clang::TemplateArgument(context.ast_context(),
                                                      aps_int, param_type);
          // TODO: provide a better location.
          auto loc = clang::TemplateArgumentLocInfo();

Review comment (suggestion):
We'd normally use `auto` in cases like this (see [style guide](https://docs.carbon-lang.dev/docs/project/cpp_style_guide.html#naming-variable-types-and-the-use-of-auto)).

```suggestion
      const auto type_expr =
          ImportCppType(context, SemIR::LocId(arg_id), param_type);

      // Try to convert the argument to the parameter type.
      const auto converted_inst_id =
```

Code after:
      clang::TemplateName name(cast<clang::TemplateDecl>(
          context.clang_decls().Get(template_name_type->decl_id).key.decl));
      return clang::TemplateArgumentLoc(
          context.ast_context(), clang::TemplateArgument(name),
          /*TemplateKWLoc=*/clang::SourceLocation(),
          clang::NestedNameSpecifierLoc(),
          GetCppLocation(context, SemIR::LocId(arg_id)));
    }

    // TODO: Eventually we should also support passing Carbon generics as
    // template template arguments.
    return MakePlaceholderTemplateArg(context, arg_id);
  }

  if (const auto* non_type =
          dyn_cast<clang::NonTypeTemplateParmDecl>(param_decl)) {
    auto param_type = non_type->getType();

    // Handle integer parameters.
    if (param_type->isIntegerType()) {
      // Get the Carbon type corresponding to the parameter's Clang type.
      const auto type_expr =
          ImportCppType(context, SemIR::LocId(arg_id), param_type);

      // Try to convert the argument to the parameter type.
      const auto converted_inst_id =
          Convert(context, SemIR::LocId(arg_id), arg_id,
                  {
                      .kind = ConversionTarget::Value,
                      .type_id = type_expr.type_id,
                  });

      if (converted_inst_id == SemIR::ErrorInst::InstId) {
        return std::nullopt;
      }

      auto const_inst_id =
          context.constant_values().GetConstantInstId(converted_inst_id);
      if (const_inst_id.has_value()) {
        if (auto int_value =
                context.insts().TryGetAs<SemIR::IntValue>(const_inst_id)) {
          const auto& ap_int = context.ints().Get(int_value->int_id);
          const bool is_unsigned =
              !param_type->isSignedIntegerOrEnumerationType();
          auto aps_int =
              llvm::APSInt(ap_int, is_unsigned)
                  .extOrTrunc(context.ast_context().getIntWidth(param_type));
          auto template_arg = clang::TemplateArgument(context.ast_context(),
                                                      aps_int, param_type);
          // TODO: provide a better location.
          auto loc = clang::TemplateArgumentLocInfo();