Skip to content

Commit ae54406

Browse files
committed
Show the problem of importing overload set
1 parent 170237b commit ae54406

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

toolchain/check/context.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ class Context {
279279
return sem_ir().import_ir_insts();
280280
}
281281
auto ast_context() -> clang::ASTContext& {
282-
return sem_ir().clang_ast_unit()->getASTContext();
282+
clang::ASTUnit* ast_unit = sem_ir().clang_ast_unit();
283+
CARBON_CHECK(ast_unit);
284+
return ast_unit->getASTContext();
283285
}
284286
auto names() -> SemIR::NameStoreWrapper { return sem_ir().names(); }
285287
auto name_scopes() -> SemIR::NameScopeStore& {

toolchain/check/import_ref.cpp

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "toolchain/check/type_completion.h"
2323
#include "toolchain/parse/node_ids.h"
2424
#include "toolchain/sem_ir/constant.h"
25+
#include "toolchain/sem_ir/cpp_overload_set.h"
2526
#include "toolchain/sem_ir/file.h"
2627
#include "toolchain/sem_ir/ids.h"
2728
#include "toolchain/sem_ir/import_ir.h"
@@ -235,6 +236,9 @@ class ImportContext {
235236
return import_ir().facet_types();
236237
}
237238
auto import_functions() -> decltype(auto) { return import_ir().functions(); }
239+
auto import_cpp_overload_sets() -> decltype(auto) {
240+
return import_ir().cpp_overload_sets();
241+
}
238242
auto import_generics() -> decltype(auto) { return import_ir().generics(); }
239243
auto import_identifiers() -> decltype(auto) {
240244
return import_ir().identifiers();
@@ -295,6 +299,9 @@ class ImportContext {
295299
return local_ir().facet_types();
296300
}
297301
auto local_functions() -> decltype(auto) { return local_ir().functions(); }
302+
auto local_cpp_overload_sets() -> decltype(auto) {
303+
return local_ir().cpp_overload_sets();
304+
}
298305
auto local_generics() -> decltype(auto) { return local_ir().generics(); }
299306
auto local_identifiers() -> decltype(auto) {
300307
return local_ir().identifiers();
@@ -1837,26 +1844,40 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver, InstT inst)
18371844
resolver, {.type_id = SemIR::TypeType::TypeId, .inner_id = inner_id});
18381845
}
18391846

1840-
// TODO: This is a WIP attempt to solve the failing test
1841-
// https://github.com/carbon-language/carbon-lang/blob/508a88e2a995c9f3342b019cee6948c162004b68/toolchain/check/testdata/interop/cpp/import.carbon.
1842-
// Adding this method solves the failure `TryResolveInst on unsupported
1843-
// instruction kind CppOverloadSetType`. However there is a new failure
1844-
// `./toolchain/base/value_store.h:111: id.index < size_: inst27` and this still
1845-
// remains a WIP.
1847+
static auto GetLocalCppOverloadSet(ImportRefResolver& resolver,
1848+
SemIR::CppOverloadSetId cpp_overload_set_id)
1849+
-> SemIR::CppOverloadSet {
1850+
CARBON_CHECK(cpp_overload_set_id.has_value());
1851+
1852+
const auto& import_cpp_overload_set =
1853+
resolver.import_cpp_overload_sets().Get(cpp_overload_set_id);
1854+
return SemIR::CppOverloadSet{
1855+
.name_id = GetLocalNameId(resolver, import_cpp_overload_set.name_id),
1856+
.parent_scope_id = GetLocalNameScopeId(
1857+
resolver, import_cpp_overload_set.parent_scope_id),
1858+
// THIS IS A PROBLEM.
1859+
.candidate_functions = import_cpp_overload_set.candidate_functions};
1860+
}
1861+
18461862
static auto TryResolveTypedInst(ImportRefResolver& resolver,
1847-
SemIR::CppOverloadSetType inst,
1848-
SemIR::InstId inst_id, SemIR::Inst untyped_inst)
1863+
SemIR::CppOverloadSetType inst)
18491864
-> ResolveResult {
1850-
resolver.local_context().TODO(SemIR::LocId::None,
1851-
"Unsupported: Importing C++ functions that "
1852-
"require thunks indirectly called here");
1853-
auto inst_constant_id = resolver.import_constant_values().Get(inst_id);
1854-
if (!inst_constant_id.is_constant()) {
1855-
CARBON_CHECK(untyped_inst.Is<SemIR::BindName>(),
1856-
"TryResolveInst on non-constant instruction {0}", inst);
1857-
return ResolveResult::Done(SemIR::ConstantId::NotConstant);
1865+
auto type_const_id = GetLocalConstantId(resolver, inst.type_id);
1866+
auto cpp_overload_set =
1867+
GetLocalCppOverloadSet(resolver, inst.overload_set_id);
1868+
auto specific_data = GetLocalSpecificData(resolver, inst.specific_id);
1869+
if (resolver.HasNewWork()) {
1870+
return ResolveResult::Retry();
18581871
}
1859-
return ResolveResult::Done(inst_constant_id);
1872+
1873+
return ResolveAsDeduplicated<SemIR::CppOverloadSetType>(
1874+
resolver,
1875+
{.type_id = resolver.local_context().types().GetTypeIdForTypeConstantId(
1876+
type_const_id),
1877+
.overload_set_id =
1878+
resolver.local_cpp_overload_sets().Add(cpp_overload_set),
1879+
.specific_id =
1880+
GetOrAddLocalSpecific(resolver, inst.specific_id, specific_data)});
18601881
}
18611882

18621883
static auto TryResolveTypedInst(ImportRefResolver& resolver,
@@ -3182,7 +3203,7 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
31823203
return TryResolveTypedInst(resolver, inst);
31833204
}
31843205
case CARBON_KIND(SemIR::CppOverloadSetType inst): {
3185-
return TryResolveTypedInst(resolver, inst, inst_id, untyped_inst);
3206+
return TryResolveTypedInst(resolver, inst);
31863207
}
31873208
case CARBON_KIND(SemIR::ExportDecl inst): {
31883209
return TryResolveTypedInst(resolver, inst);

0 commit comments

Comments
 (0)