Skip to content

Commit a24598f

Browse files
Lower CppOverloadSetValue (carbon-language#6101)
Following up on comments from [carbon-language#5891](carbon-language#5891) ([ 1](carbon-language#5891 (comment)), [2](carbon-language#5891 (comment))). Lowering `CppOverloadSetValue` as an empty struct value, using `context.GetLiteralAsValue()`. Also changed its constant kind to `InstConstantKind::Always`. Part of carbon-language#5915
1 parent 4a6376c commit a24598f

40 files changed

+838
-698
lines changed

toolchain/check/eval_inst.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,6 @@ auto EvalConstantInst(Context& context, SemIR::Converted inst)
180180
context.constant_values().Get(inst.result_id));
181181
}
182182

183-
// TODO: This should not be necessary since the constant kind is
184-
// WheneverPossible.
185-
auto EvalConstantInst(Context& /*context*/, SemIR::CppOverloadSetValue inst)
186-
-> ConstantEvalResult {
187-
return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
188-
.type_id = inst.type_id, .elements_id = SemIR::InstBlockId::Empty});
189-
}
190-
191183
auto EvalConstantInst(Context& /*context*/, SemIR::Deref /*inst*/)
192184
-> ConstantEvalResult {
193185
// TODO: Handle this.

toolchain/check/import_ref.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,24 +1845,33 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver, InstT inst)
18451845
resolver, {.type_id = SemIR::TypeType::TypeId, .inner_id = inner_id});
18461846
}
18471847

1848-
static auto TryResolveTypedInst(ImportRefResolver& resolver,
1849-
SemIR::CppOverloadSetType inst)
1850-
-> ResolveResult {
1848+
static auto HandleUnsupportedCppOverloadSet(ImportRefResolver& resolver,
1849+
SemIR::CppOverloadSetId id) {
18511850
// Supporting C++ overload resolution of imported functions is a large task,
18521851
// which might require serializing and deserializing AST for using decl ids,
18531852
// using modules and/or linking ASTs.
18541853
resolver.local_context().TODO(
18551854
SemIR::LocId::None,
1856-
llvm::formatv("Unsupported: Importing C++ function `{0}` indirectly",
1857-
resolver.import_ir().names().GetAsStringIfIdentifier(
1858-
resolver.import_ir()
1859-
.cpp_overload_sets()
1860-
.Get(inst.overload_set_id)
1861-
.name_id)));
1855+
llvm::formatv(
1856+
"Unsupported: Importing C++ function `{0}` indirectly",
1857+
resolver.import_ir().names().GetAsStringIfIdentifier(
1858+
resolver.import_ir().cpp_overload_sets().Get(id).name_id)));
18621859
return ResolveResult::Done(SemIR::ErrorInst::ConstantId,
18631860
SemIR::ErrorInst::InstId);
18641861
}
18651862

1863+
static auto TryResolveTypedInst(ImportRefResolver& resolver,
1864+
SemIR::CppOverloadSetType inst)
1865+
-> ResolveResult {
1866+
return HandleUnsupportedCppOverloadSet(resolver, inst.overload_set_id);
1867+
}
1868+
1869+
static auto TryResolveTypedInst(ImportRefResolver& resolver,
1870+
SemIR::CppOverloadSetValue inst)
1871+
-> ResolveResult {
1872+
return HandleUnsupportedCppOverloadSet(resolver, inst.overload_set_id);
1873+
}
1874+
18661875
static auto TryResolveTypedInst(ImportRefResolver& resolver,
18671876
SemIR::ExportDecl inst) -> ResolveResult {
18681877
auto value_id = GetLocalConstantId(resolver, inst.value_id);
@@ -3188,6 +3197,9 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
31883197
case CARBON_KIND(SemIR::CppOverloadSetType inst): {
31893198
return TryResolveTypedInst(resolver, inst);
31903199
}
3200+
case CARBON_KIND(SemIR::CppOverloadSetValue inst): {
3201+
return TryResolveTypedInst(resolver, inst);
3202+
}
31913203
case CARBON_KIND(SemIR::ExportDecl inst): {
31923204
return TryResolveTypedInst(resolver, inst);
31933205
}

toolchain/check/testdata/basics/raw_sem_ir/cpp_interop.carbon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn G(x: Cpp.X) {
135135
// CHECK:STDOUT: inst36: {kind: NameRef, arg0: name0, arg1: inst16, type: type(inst(NamespaceType))}
136136
// CHECK:STDOUT: inst37: {kind: CppOverloadSetType, arg0: cpp_overload_set0, arg1: specific<none>, type: type(TypeType)}
137137
// CHECK:STDOUT: inst38: {kind: CppOverloadSetValue, arg0: cpp_overload_set0, type: type(inst37)}
138-
// CHECK:STDOUT: inst39: {kind: StructValue, arg0: inst_block_empty, type: type(inst37)}
138+
// CHECK:STDOUT: inst39: {kind: CppOverloadSetValue, arg0: cpp_overload_set0, type: type(inst37)}
139139
// CHECK:STDOUT: inst40: {kind: NameRef, arg0: name4, arg1: inst38, type: type(inst37)}
140140
// CHECK:STDOUT: inst41: {kind: FunctionDecl, arg0: function1, arg1: inst_block_empty, type: type(inst42)}
141141
// CHECK:STDOUT: inst42: {kind: FunctionType, arg0: function1, arg1: specific<none>, type: type(TypeType)}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ fn F() {
550550
// CHECK:STDOUT: %u32: type = class_type @UInt, @UInt(%int_32) [concrete]
551551
// CHECK:STDOUT: %pattern_type.4a9: type = pattern_type %u32 [concrete]
552552
// CHECK:STDOUT: %.15a: type = cpp_overload_set_type @unsigned_int.foo [concrete]
553-
// CHECK:STDOUT: %empty_struct: %.15a = struct_value () [concrete]
553+
// CHECK:STDOUT: %.afb: %.15a = cpp_overload_set_value @unsigned_int.foo [concrete]
554554
// CHECK:STDOUT: %ptr.d47: type = ptr_type %unsigned_int [concrete]
555555
// CHECK:STDOUT: %unsigned_int.foo.type: type = fn_type @unsigned_int.foo [concrete]
556556
// CHECK:STDOUT: %unsigned_int.foo: %unsigned_int.foo.type = struct_value () [concrete]
@@ -566,7 +566,7 @@ fn F() {
566566
// CHECK:STDOUT: import Cpp//...
567567
// CHECK:STDOUT: }
568568
// CHECK:STDOUT: %unsigned_int.decl: type = class_decl @unsigned_int [concrete = constants.%unsigned_int] {} {}
569-
// CHECK:STDOUT: %.afb: %.15a = cpp_overload_set_value @unsigned_int.foo [concrete = constants.%empty_struct]
569+
// CHECK:STDOUT: %.afb: %.15a = cpp_overload_set_value @unsigned_int.foo [concrete = constants.%.afb]
570570
// CHECK:STDOUT: %unsigned_int.foo.decl: %unsigned_int.foo.type = fn_decl @unsigned_int.foo [concrete = constants.%unsigned_int.foo] {
571571
// CHECK:STDOUT: <elided>
572572
// CHECK:STDOUT: } {
@@ -590,7 +590,7 @@ fn F() {
590590
// CHECK:STDOUT: %x.patt: %pattern_type.4a9 = binding_pattern x [concrete]
591591
// CHECK:STDOUT: }
592592
// CHECK:STDOUT: %unsigned_int.ref.loc13: ref %unsigned_int = name_ref unsigned_int, %unsigned_int
593-
// CHECK:STDOUT: %foo.ref: %.15a = name_ref foo, imports.%.afb [concrete = constants.%empty_struct]
593+
// CHECK:STDOUT: %foo.ref: %.15a = name_ref foo, imports.%.afb [concrete = constants.%.afb]
594594
// CHECK:STDOUT: %bound_method.loc13: <bound method> = bound_method %unsigned_int.ref.loc13, %foo.ref
595595
// CHECK:STDOUT: %addr.loc13: %ptr.d47 = addr_of %unsigned_int.ref.loc13
596596
// CHECK:STDOUT: %unsigned_int.foo.call: init %u32 = call imports.%unsigned_int.foo.decl(%addr.loc13)

toolchain/check/testdata/interop/cpp/class/access.carbon

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -493,13 +493,13 @@ fn F() {
493493
// CHECK:STDOUT: %ptr.5c7: type = ptr_type %S [concrete]
494494
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
495495
// CHECK:STDOUT: %.177: type = cpp_overload_set_type @S.foo [concrete]
496-
// CHECK:STDOUT: %empty_struct: %.177 = struct_value () [concrete]
496+
// CHECK:STDOUT: %.dcb: %.177 = cpp_overload_set_value @S.foo [concrete]
497497
// CHECK:STDOUT: %S.foo.type: type = fn_type @S.foo [concrete]
498498
// CHECK:STDOUT: %S.foo: %S.foo.type = struct_value () [concrete]
499499
// CHECK:STDOUT: }
500500
// CHECK:STDOUT:
501501
// CHECK:STDOUT: imports {
502-
// CHECK:STDOUT: %.dcb: %.177 = cpp_overload_set_value @S.foo [concrete = constants.%empty_struct]
502+
// CHECK:STDOUT: %.dcb: %.177 = cpp_overload_set_value @S.foo [concrete = constants.%.dcb]
503503
// CHECK:STDOUT: %S.foo.decl: %S.foo.type = fn_decl @S.foo [concrete = constants.%S.foo] {
504504
// CHECK:STDOUT: <elided>
505505
// CHECK:STDOUT: } {
@@ -511,7 +511,7 @@ fn F() {
511511
// CHECK:STDOUT: !entry:
512512
// CHECK:STDOUT: %s.ref: %ptr.5c7 = name_ref s, %s
513513
// CHECK:STDOUT: %.loc8: ref %S = deref %s.ref
514-
// CHECK:STDOUT: %foo.ref: %.177 = name_ref foo, imports.%.dcb [concrete = constants.%empty_struct]
514+
// CHECK:STDOUT: %foo.ref: %.177 = name_ref foo, imports.%.dcb [concrete = constants.%.dcb]
515515
// CHECK:STDOUT: %bound_method: <bound method> = bound_method %.loc8, %foo.ref
516516
// CHECK:STDOUT: %addr: %ptr.5c7 = addr_of %.loc8
517517
// CHECK:STDOUT: %S.foo.call: init %empty_tuple.type = call imports.%S.foo.decl(%addr)
@@ -524,12 +524,12 @@ fn F() {
524524
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
525525
// CHECK:STDOUT: %C: type = class_type @C [concrete]
526526
// CHECK:STDOUT: %.d40: type = cpp_overload_set_type @C__carbon_thunk [concrete]
527-
// CHECK:STDOUT: %empty_struct.e73: %.d40 = struct_value () [concrete]
527+
// CHECK:STDOUT: %.40b: %.d40 = cpp_overload_set_value @C__carbon_thunk [concrete]
528528
// CHECK:STDOUT: %ptr.d9e: type = ptr_type %C [concrete]
529529
// CHECK:STDOUT: %C__carbon_thunk.type: type = fn_type @C__carbon_thunk [concrete]
530530
// CHECK:STDOUT: %C__carbon_thunk: %C__carbon_thunk.type = struct_value () [concrete]
531531
// CHECK:STDOUT: %.cd1: type = cpp_overload_set_type @C.foo [concrete]
532-
// CHECK:STDOUT: %empty_struct.b85: %.cd1 = struct_value () [concrete]
532+
// CHECK:STDOUT: %.e4a: %.cd1 = cpp_overload_set_value @C.foo [concrete]
533533
// CHECK:STDOUT: %C.foo.type: type = fn_type @C.foo [concrete]
534534
// CHECK:STDOUT: %C.foo: %C.foo.type = struct_value () [concrete]
535535
// CHECK:STDOUT: %type_where: type = facet_type <type where .Self impls <CanDestroy>> [concrete]
@@ -544,29 +544,29 @@ fn F() {
544544
// CHECK:STDOUT: import Cpp//...
545545
// CHECK:STDOUT: }
546546
// CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
547-
// CHECK:STDOUT: %.40b: %.d40 = cpp_overload_set_value @C__carbon_thunk [concrete = constants.%empty_struct.e73]
547+
// CHECK:STDOUT: %.40b: %.d40 = cpp_overload_set_value @C__carbon_thunk [concrete = constants.%.40b]
548548
// CHECK:STDOUT: %C__carbon_thunk.decl: %C__carbon_thunk.type = fn_decl @C__carbon_thunk [concrete = constants.%C__carbon_thunk] {
549549
// CHECK:STDOUT: <elided>
550550
// CHECK:STDOUT: } {
551551
// CHECK:STDOUT: <elided>
552552
// CHECK:STDOUT: }
553-
// CHECK:STDOUT: %.e4a: %.cd1 = cpp_overload_set_value @C.foo [concrete = constants.%empty_struct.b85]
553+
// CHECK:STDOUT: %.e4a: %.cd1 = cpp_overload_set_value @C.foo [concrete = constants.%.e4a]
554554
// CHECK:STDOUT: %C.foo.decl: %C.foo.type = fn_decl @C.foo [concrete = constants.%C.foo] {} {}
555555
// CHECK:STDOUT: }
556556
// CHECK:STDOUT:
557557
// CHECK:STDOUT: fn @F() {
558558
// CHECK:STDOUT: !entry:
559559
// CHECK:STDOUT: %Cpp.ref.loc8: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
560560
// CHECK:STDOUT: %C.ref.loc8_6: type = name_ref C, imports.%C.decl [concrete = constants.%C]
561-
// CHECK:STDOUT: %C.ref.loc8_8: %.d40 = name_ref C, imports.%.40b [concrete = constants.%empty_struct.e73]
561+
// CHECK:STDOUT: %C.ref.loc8_8: %.d40 = name_ref C, imports.%.40b [concrete = constants.%.40b]
562562
// CHECK:STDOUT: %.loc8_11.1: ref %C = temporary_storage
563563
// CHECK:STDOUT: %addr.loc8_11.1: %ptr.d9e = addr_of %.loc8_11.1
564564
// CHECK:STDOUT: %C__carbon_thunk.call: init %empty_tuple.type = call imports.%C__carbon_thunk.decl(%addr.loc8_11.1)
565565
// CHECK:STDOUT: %.loc8_11.2: init %C = in_place_init %C__carbon_thunk.call, %.loc8_11.1
566566
// CHECK:STDOUT: %.loc8_11.3: ref %C = temporary %.loc8_11.1, %.loc8_11.2
567567
// CHECK:STDOUT: %Cpp.ref.loc9: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
568568
// CHECK:STDOUT: %C.ref.loc9: type = name_ref C, imports.%C.decl [concrete = constants.%C]
569-
// CHECK:STDOUT: %foo.ref: %.cd1 = name_ref foo, imports.%.e4a [concrete = constants.%empty_struct.b85]
569+
// CHECK:STDOUT: %foo.ref: %.cd1 = name_ref foo, imports.%.e4a [concrete = constants.%.e4a]
570570
// CHECK:STDOUT: %C.foo.call: init %empty_tuple.type = call imports.%C.foo.decl()
571571
// CHECK:STDOUT: %facet_value: %type_where = facet_value constants.%C, () [concrete = constants.%facet_value]
572572
// CHECK:STDOUT: %.loc8_11.4: %type_where = converted constants.%C, %facet_value [concrete = constants.%facet_value]
@@ -584,7 +584,7 @@ fn F() {
584584
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
585585
// CHECK:STDOUT: %DerivedPublic: type = class_type @DerivedPublic [concrete]
586586
// CHECK:STDOUT: %.5a6: type = cpp_overload_set_type @Base.foo [concrete]
587-
// CHECK:STDOUT: %empty_struct: %.5a6 = struct_value () [concrete]
587+
// CHECK:STDOUT: %.607: %.5a6 = cpp_overload_set_value @Base.foo [concrete]
588588
// CHECK:STDOUT: %Base.foo.type: type = fn_type @Base.foo [concrete]
589589
// CHECK:STDOUT: %Base.foo: %Base.foo.type = struct_value () [concrete]
590590
// CHECK:STDOUT: }
@@ -595,15 +595,15 @@ fn F() {
595595
// CHECK:STDOUT: import Cpp//...
596596
// CHECK:STDOUT: }
597597
// CHECK:STDOUT: %DerivedPublic.decl: type = class_decl @DerivedPublic [concrete = constants.%DerivedPublic] {} {}
598-
// CHECK:STDOUT: %.607: %.5a6 = cpp_overload_set_value @Base.foo [concrete = constants.%empty_struct]
598+
// CHECK:STDOUT: %.607: %.5a6 = cpp_overload_set_value @Base.foo [concrete = constants.%.607]
599599
// CHECK:STDOUT: %Base.foo.decl: %Base.foo.type = fn_decl @Base.foo [concrete = constants.%Base.foo] {} {}
600600
// CHECK:STDOUT: }
601601
// CHECK:STDOUT:
602602
// CHECK:STDOUT: fn @F() {
603603
// CHECK:STDOUT: !entry:
604604
// CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
605605
// CHECK:STDOUT: %DerivedPublic.ref: type = name_ref DerivedPublic, imports.%DerivedPublic.decl [concrete = constants.%DerivedPublic]
606-
// CHECK:STDOUT: %foo.ref: %.5a6 = name_ref foo, imports.%.607 [concrete = constants.%empty_struct]
606+
// CHECK:STDOUT: %foo.ref: %.5a6 = name_ref foo, imports.%.607 [concrete = constants.%.607]
607607
// CHECK:STDOUT: %Base.foo.call: init %empty_tuple.type = call imports.%Base.foo.decl()
608608
// CHECK:STDOUT: <elided>
609609
// CHECK:STDOUT: }

0 commit comments

Comments
 (0)