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

Code before:

  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;
  }
  const auto* builtin_type = dyn_cast<clang::BuiltinType>(ret_type);
  if (builtin_type && builtin_type->getKind() == clang::BuiltinType::Int) {
    // TODO: Detect when integer types aren't the expected sizes
    IntId size_id = context.ints().Add(32);
    // 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(
        // TODO: Fill in a location for the return type once available.
        context, SemIR::LocIdAndInst::NoLoc(SemIR::ReturnSlotPattern(
                     {.type_id = type_id, .type_inst_id = type_inst_id})));
    SemIR::InstId param_pattern_id = AddInstInNoBlock(
        // TODO: Fill in 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;
  }
  context.TODO(loc_id, llvm::formatv("Unsupported: return type: {0}",
                                     ret_type.getAsString()));
  return SemIR::ErrorInst::SingletonInstId;
}

Review comment (suggestion):
```suggestion
  if (const auto* builtin_type = dyn_cast<clang::BuiltinType>(ret_type); builtin_type && builtin_type->getKind() == clang::BuiltinType::Int) {
```

Code after:
              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(
        // TODO: Fill in a location for the return type once available.