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

Code before:
      lookup, ast->getASTContext().getTranslationUnitDecl());

  if (lookup.isClassLookup()) {
    // TODO: To support class lookup, also return the AccessKind for storage.
    context.TODO(loc_id, "Unsupported: Lookup in Class");
    return std::nullopt;
  }

  if (!found) {
    return std::nullopt;
  }

  return lookup;
}

// Returns the return type of the given function declaration.
// Currently only void and 32-bit int are supported.
// TODO: Support more return types.
static auto GetReturnType(Context& context,
                          const clang::FunctionDecl* clang_decl)
    -> SemIR::InstId {
  auto ret_type = clang_decl->getReturnType().getCanonicalType();
  if (ret_type->isVoidType()) {
    return SemIR::InstId::None;
  }
  const int supported_int_width = 32;
  if (ret_type->isSignedIntegerType() &&
      clang_decl->getASTContext().getTypeSize(ret_type) ==
          supported_int_width &&
      clang_decl->getASTContext().getTargetInfo().getIntWidth() ==
          supported_int_width) {
    auto size_id = context.ints().Add(supported_int_width);
    auto type_id = MakeIntType(context, Parse::NodeId::None,
                               SemIR::IntKind::Signed, size_id);
    auto type_inst_id = MakeIntTypeLiteral(context, Parse::NodeId::None,
                                           SemIR::IntKind::Signed, size_id);

    auto return_slot_pattern_id = AddInstInNoBlock(
        // TODO: have a location for the return type once available.
        context, SemIR::LocIdAndInst::NoLoc(SemIR::ReturnSlotPattern(
                     {.type_id = type_id, .type_inst_id = type_inst_id})));
    auto param_pattern_id = AddInstInNoBlock(
        // TODO: same here, have a location for the return type once available.
        context, SemIR::LocIdAndInst::NoLoc(SemIR::OutParamPattern(
                     {.type_id = type_id,
                      .subpattern_id = return_slot_pattern_id,
                      .index = SemIR::CallParamIndex::None})));
    return param_pattern_id;
  }
  return SemIR::ErrorInst::SingletonInstId;
}

Review comment (suggestion):
Had you considered marking this `constexpr` instead of just `const`? This has some different effects, including different naming checks for clang-tidy since it's a stronger guarantee.

```suggestion
  constexpr int SupportedIntWidth = 32;
```

Code after:
          clang::DeclarationName(
              sema.getPreprocessor().getIdentifierInfo(*name)),
          clang::SourceLocation()),
      clang::Sema::LookupNameKind::LookupOrdinaryName);

  bool found = sema.LookupQualifiedName(
      lookup, context.name_scopes().Get(scope_id).cpp_decl_context());

  if (lookup.isClassLookup()) {
    // TODO: To support class lookup, also return the AccessKind for storage.
    context.TODO(loc_id, "Unsupported: Lookup in Class");
    return std::nullopt;
  }

  if (!found) {
    return std::nullopt;
  }

  return lookup;
}

// Returns the return type of the given function declaration.
// Currently only void and 32-bit int are supported.
// TODO: Support more return types.
static auto GetReturnType(Context& context, SemIRLoc loc_id,
                          const clang::FunctionDecl* clang_decl)
    -> SemIR::InstId {
  clang::QualType ret_type = clang_decl->getReturnType().getCanonicalType();
  if (ret_type->isVoidType()) {
    return SemIR::InstId::None;
  }
  if (const auto* builtin_type = dyn_cast<clang::BuiltinType>(ret_type);
      builtin_type && builtin_type->getKind() == clang::BuiltinType::Int) {
    constexpr int SupportedIntWidth = 32;
    uint64_t int_size = context.ast_context().getTypeSize(ret_type);
    if (int_size != SupportedIntWidth) {
      // TODO: Add tests for this case.
      context.TODO(loc_id,
                   llvm::formatv("Unsupported: return type: {0}, size: {1}",
                                 ret_type.getAsString(), int_size));
      return SemIR::ErrorInst::SingletonInstId;
    }
    IntId size_id = context.ints().Add(int_size);
    // TODO: Fill in a location for the type once available.
    SemIR::TypeId type_id = MakeIntType(context, Parse::NodeId::None,
                                        SemIR::IntKind::Signed, size_id);
    // TODO: Fill in a location for the type once available.
    SemIR::InstId type_inst_id = MakeIntTypeLiteral(
        context, Parse::NodeId::None, SemIR::IntKind::Signed, size_id);

    SemIR::InstId return_slot_pattern_id = AddInstInNoBlock(