Skip to content

Commit 75fd8c2

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

File tree

4 files changed

+85
-23
lines changed

4 files changed

+85
-23
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/cpp_type_mapping.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ static auto TryMapBuiltinType(Context& context, SemIR::InstId inst_id,
7878
true);
7979
}
8080
case CARBON_KIND(SemIR::IntType int_type): {
81-
auto bit_width_inst = context.sem_ir().insts().TryGetAs<SemIR::IntValue>(
81+
auto bit_width_inst = context.sem_ir().insts().GetAs<SemIR::IntValue>(
8282
int_type.bit_width_id);
8383
return context.ast_context().getIntTypeForBitwidth(
84-
bit_width_inst->int_id.AsValue(), int_type.int_kind.is_signed());
84+
bit_width_inst.int_id.AsValue(), int_type.int_kind.is_signed());
8585
}
8686
// TODO: What if the value doesn't fit to f64?
8787
case SemIR::FloatLiteralType::Kind: {

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);

toolchain/check/testdata/interop/cpp/import.carbon

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,63 @@ library "[[@TEST_NAME]]";
3030

3131
import library "api";
3232

33-
// TODO: Fix this test as a follow-up of https://github.com/carbon-language/carbon-lang/pull/5891.
3433
fn F() {
3534
//@dump-sem-ir-begin
36-
// FooShort(8 as i16);
35+
// CHECK:STDERR: import_api.carbon:[[@LINE+7]]:3: error: semantics TODO: `Unsupported: parameter type: short` [SemanticsTodo]
36+
// CHECK:STDERR: FooShort(8 as i16);
37+
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
38+
// CHECK:STDERR: import_api.carbon:[[@LINE+4]]:3: note: in call to Cpp function here [InCallToCppFunction]
39+
// CHECK:STDERR: FooShort(8 as i16);
40+
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
41+
// CHECK:STDERR:
42+
FooShort(8 as i16);
3743
// FooInt(9);
3844
//@dump-sem-ir-end
3945
}
4046

4147
// CHECK:STDOUT: --- import_api.carbon
4248
// CHECK:STDOUT:
4349
// CHECK:STDOUT: constants {
50+
// CHECK:STDOUT: %.033: type = cpp_overload_set_type @As.Convert [concrete]
51+
// CHECK:STDOUT: %empty_struct: %.033 = struct_value () [concrete]
52+
// CHECK:STDOUT: %int_8.b85: Core.IntLiteral = int_value 8 [concrete]
53+
// CHECK:STDOUT: %int_16: Core.IntLiteral = int_value 16 [concrete]
54+
// CHECK:STDOUT: %i16: type = class_type @Int, @Int(%int_16) [concrete]
55+
// CHECK:STDOUT: %As.type.771: type = facet_type <@As, @As(%i16)> [concrete]
56+
// CHECK:STDOUT: %As.Convert.type.be5: type = fn_type @As.Convert, @As(%i16) [concrete]
57+
// CHECK:STDOUT: %To: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic]
58+
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.type.565: type = fn_type @Core.IntLiteral.as.As.impl.Convert, @Core.IntLiteral.as.As.impl(%To) [symbolic]
59+
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.d2c: %Core.IntLiteral.as.As.impl.Convert.type.565 = struct_value () [symbolic]
60+
// CHECK:STDOUT: %As.impl_witness.2d2: <witness> = impl_witness imports.%As.impl_witness_table.5ad, @Core.IntLiteral.as.As.impl(%int_16) [concrete]
61+
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.type.38a: type = fn_type @Core.IntLiteral.as.As.impl.Convert, @Core.IntLiteral.as.As.impl(%int_16) [concrete]
62+
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.97a: %Core.IntLiteral.as.As.impl.Convert.type.38a = struct_value () [concrete]
63+
// CHECK:STDOUT: %As.facet: %As.type.771 = facet_value Core.IntLiteral, (%As.impl_witness.2d2) [concrete]
64+
// CHECK:STDOUT: %.026: type = fn_type_with_self_type %As.Convert.type.be5, %As.facet [concrete]
65+
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.bound: <bound method> = bound_method %int_8.b85, %Core.IntLiteral.as.As.impl.Convert.97a [concrete]
66+
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.As.impl.Convert.97a, @Core.IntLiteral.as.As.impl.Convert(%int_16) [concrete]
67+
// CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_8.b85, %Core.IntLiteral.as.As.impl.Convert.specific_fn [concrete]
68+
// CHECK:STDOUT: %int_8.823: %i16 = int_value 8 [concrete]
4469
// CHECK:STDOUT: }
4570
// CHECK:STDOUT:
4671
// CHECK:STDOUT: imports {
72+
// CHECK:STDOUT: %Main.FooShort: %.033 = import_ref Main//api, FooShort, loaded [concrete = constants.%empty_struct]
73+
// CHECK:STDOUT: %Core.import_ref.99c: @Core.IntLiteral.as.As.impl.%Core.IntLiteral.as.As.impl.Convert.type (%Core.IntLiteral.as.As.impl.Convert.type.565) = import_ref Core//prelude/parts/int, loc32_39, loaded [symbolic = @Core.IntLiteral.as.As.impl.%Core.IntLiteral.as.As.impl.Convert (constants.%Core.IntLiteral.as.As.impl.Convert.d2c)]
74+
// CHECK:STDOUT: %As.impl_witness_table.5ad = impl_witness_table (%Core.import_ref.99c), @Core.IntLiteral.as.As.impl [concrete]
4775
// CHECK:STDOUT: }
4876
// CHECK:STDOUT:
4977
// CHECK:STDOUT: fn @F() {
5078
// CHECK:STDOUT: !entry:
79+
// CHECK:STDOUT: %FooShort.ref: %.033 = name_ref FooShort, imports.%Main.FooShort [concrete = constants.%empty_struct]
80+
// CHECK:STDOUT: %int_8: Core.IntLiteral = int_value 8 [concrete = constants.%int_8.b85]
81+
// CHECK:STDOUT: %int_16: Core.IntLiteral = int_value 16 [concrete = constants.%int_16]
82+
// CHECK:STDOUT: %i16: type = class_type @Int, @Int(constants.%int_16) [concrete = constants.%i16]
83+
// CHECK:STDOUT: %impl.elem0: %.026 = impl_witness_access constants.%As.impl_witness.2d2, element0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.97a]
84+
// CHECK:STDOUT: %bound_method.loc17_14.1: <bound method> = bound_method %int_8, %impl.elem0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.bound]
85+
// CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Core.IntLiteral.as.As.impl.Convert(constants.%int_16) [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.specific_fn]
86+
// CHECK:STDOUT: %bound_method.loc17_14.2: <bound method> = bound_method %int_8, %specific_fn [concrete = constants.%bound_method]
87+
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.call: init %i16 = call %bound_method.loc17_14.2(%int_8) [concrete = constants.%int_8.823]
88+
// CHECK:STDOUT: %.loc17_14.1: %i16 = value_of_initializer %Core.IntLiteral.as.As.impl.Convert.call [concrete = constants.%int_8.823]
89+
// CHECK:STDOUT: %.loc17_14.2: %i16 = converted %int_8, %.loc17_14.1 [concrete = constants.%int_8.823]
5190
// CHECK:STDOUT: <elided>
5291
// CHECK:STDOUT: }
5392
// CHECK:STDOUT:

0 commit comments

Comments
 (0)