Skip to content

Commit 8bf42cc

Browse files
committed
Merge branch 'trunk' into toolchain-format-completetypewitness
2 parents 47ef5c6 + d6ec885 commit 8bf42cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1546
-418
lines changed

toolchain/check/context.cpp

+20-22
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,19 @@ static auto DiagnoseInvalidQualifiedNameAccess(Context& context, SemIRLoc loc,
390390
}
391391

392392
// TODO: Support scoped entities other than just classes.
393-
auto class_info = context.classes().Get(class_type->class_id);
393+
const auto& class_info = context.classes().Get(class_type->class_id);
394394

395395
auto parent_type_id = class_info.self_type_id;
396396

397397
if (access_kind == SemIR::AccessKind::Private && is_parent_access) {
398-
if (auto base_decl = context.insts().TryGetAsIfValid<SemIR::BaseDecl>(
399-
class_info.base_id)) {
400-
parent_type_id = base_decl->base_type_id;
401-
} else if (auto adapt_decl =
402-
context.insts().TryGetAsIfValid<SemIR::AdaptDecl>(
403-
class_info.adapt_id)) {
404-
parent_type_id = adapt_decl->adapted_type_id;
398+
if (auto base_type_id =
399+
class_info.GetBaseType(context.sem_ir(), class_type->specific_id);
400+
base_type_id.is_valid()) {
401+
parent_type_id = base_type_id;
402+
} else if (auto adapted_type_id = class_info.GetAdaptedType(
403+
context.sem_ir(), class_type->specific_id);
404+
adapted_type_id.is_valid()) {
405+
parent_type_id = adapted_type_id;
405406
} else {
406407
CARBON_FATAL("Expected parent for parent access");
407408
}
@@ -957,7 +958,13 @@ class TypeCompleter {
957958
if (inst.specific_id.is_valid()) {
958959
ResolveSpecificDefinition(context_, inst.specific_id);
959960
}
960-
Push(class_info.GetObjectRepr(context_.sem_ir(), inst.specific_id));
961+
if (auto adapted_type_id =
962+
class_info.GetAdaptedType(context_.sem_ir(), inst.specific_id);
963+
adapted_type_id.is_valid()) {
964+
Push(adapted_type_id);
965+
} else {
966+
Push(class_info.GetObjectRepr(context_.sem_ir(), inst.specific_id));
967+
}
961968
break;
962969
}
963970
case CARBON_KIND(SemIR::ConstType inst): {
@@ -1127,12 +1134,10 @@ class TypeCompleter {
11271134
auto& class_info = context_.classes().Get(inst.class_id);
11281135
// The value representation of an adapter is the value representation of
11291136
// its adapted type.
1130-
if (class_info.adapt_id.is_valid()) {
1131-
return GetNestedValueRepr(SemIR::GetTypeInSpecific(
1132-
context_.sem_ir(), inst.specific_id,
1133-
context_.insts()
1134-
.GetAs<SemIR::AdaptDecl>(class_info.adapt_id)
1135-
.adapted_type_id));
1137+
if (auto adapted_type_id =
1138+
class_info.GetAdaptedType(context_.sem_ir(), inst.specific_id);
1139+
adapted_type_id.is_valid()) {
1140+
return GetNestedValueRepr(adapted_type_id);
11361141
}
11371142
// Otherwise, the value representation for a class is a pointer to the
11381143
// object representation.
@@ -1400,13 +1405,6 @@ auto Context::GetUnboundElementType(SemIR::TypeId class_type_id,
14001405
element_type_id);
14011406
}
14021407

1403-
auto Context::GetUnqualifiedType(SemIR::TypeId type_id) -> SemIR::TypeId {
1404-
if (auto const_type = types().TryGetAs<SemIR::ConstType>(type_id)) {
1405-
return const_type->inner_id;
1406-
}
1407-
return type_id;
1408-
}
1409-
14101408
auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
14111409
output << "Check::Context\n";
14121410

toolchain/check/context.h

-3
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,6 @@ class Context {
431431
auto GetUnboundElementType(SemIR::TypeId class_type_id,
432432
SemIR::TypeId element_type_id) -> SemIR::TypeId;
433433

434-
// Removes any top-level `const` qualifiers from a type.
435-
auto GetUnqualifiedType(SemIR::TypeId type_id) -> SemIR::TypeId;
436-
437434
// Adds an exported name.
438435
auto AddExport(SemIR::InstId inst_id) -> void { exports_.push_back(inst_id); }
439436

toolchain/check/convert.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -607,15 +607,12 @@ static auto ComputeInheritancePath(Context& context, SemIR::TypeId derived_id,
607607
break;
608608
}
609609
auto& derived_class = context.classes().Get(derived_class_type->class_id);
610-
if (!derived_class.base_id.is_valid()) {
610+
auto base_type_id = derived_class.GetBaseType(
611+
context.sem_ir(), derived_class_type->specific_id);
612+
if (!base_type_id.is_valid()) {
611613
result = std::nullopt;
612614
break;
613615
}
614-
auto base_decl =
615-
context.insts().GetAs<SemIR::BaseDecl>(derived_class.base_id);
616-
auto base_type_id = SemIR::GetTypeInSpecific(
617-
context.sem_ir(), derived_class_type->specific_id,
618-
base_decl.base_type_id);
619616
result->push_back({derived_class.base_id, base_type_id});
620617
derived_id = base_type_id;
621618
}
@@ -712,12 +709,15 @@ static auto GetCompatibleBaseType(Context& context, SemIR::TypeId type_id)
712709
-> SemIR::TypeId {
713710
// If the type is an adapter, its object representation type is its compatible
714711
// non-adapter type.
715-
if (auto class_type = context.types().TryGetAs<SemIR::ClassType>(type_id)) {
712+
while (auto class_type =
713+
context.types().TryGetAs<SemIR::ClassType>(type_id)) {
716714
auto& class_info = context.classes().Get(class_type->class_id);
717-
if (class_info.adapt_id.is_valid()) {
718-
return class_info.GetObjectRepr(context.sem_ir(),
719-
class_type->specific_id);
715+
auto adapted_type_id =
716+
class_info.GetAdaptedType(context.sem_ir(), class_type->specific_id);
717+
if (!adapted_type_id.is_valid()) {
718+
break;
720719
}
720+
type_id = adapted_type_id;
721721
}
722722

723723
// Otherwise, the type itself is a non-adapter type.

toolchain/check/eval.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ static auto PerformCheckedIntConvert(Context& context, SemIRLoc loc,
676676
auto arg_val = context.ints().Get(arg.int_id);
677677

678678
auto [is_signed, bit_width_id] =
679-
context.sem_ir().GetIntTypeInfo(dest_type_id);
679+
context.sem_ir().types().GetIntTypeInfo(dest_type_id);
680680
auto width = bit_width_id.is_valid()
681681
? context.ints().Get(bit_width_id).getZExtValue()
682682
: arg_val.getBitWidth();
@@ -718,7 +718,8 @@ static auto PerformBuiltinUnaryIntOp(Context& context, SemIRLoc loc,
718718
SemIR::InstId arg_id)
719719
-> SemIR::ConstantId {
720720
auto op = context.insts().GetAs<SemIR::IntValue>(arg_id);
721-
auto [is_signed, bit_width_id] = context.sem_ir().GetIntTypeInfo(op.type_id);
721+
auto [is_signed, bit_width_id] =
722+
context.sem_ir().types().GetIntTypeInfo(op.type_id);
722723
CARBON_CHECK(bit_width_id != IntId::Invalid,
723724
"Cannot evaluate a generic bit width integer: {0}", op);
724725
llvm::APInt op_val = context.ints().GetAtWidth(op.int_id, bit_width_id);
@@ -771,7 +772,7 @@ static auto PerformBuiltinBinaryIntOp(Context& context, SemIRLoc loc,
771772
}
772773

773774
auto [lhs_is_signed, lhs_bit_width_id] =
774-
context.sem_ir().GetIntTypeInfo(lhs.type_id);
775+
context.sem_ir().types().GetIntTypeInfo(lhs.type_id);
775776
llvm::APInt lhs_val = context.ints().GetAtWidth(lhs.int_id, lhs_bit_width_id);
776777

777778
llvm::APInt result_val;
@@ -916,7 +917,8 @@ static auto PerformBuiltinIntComparison(Context& context,
916917
CARBON_CHECK(lhs.type_id == rhs.type_id,
917918
"Builtin comparison with mismatched types!");
918919

919-
auto [is_signed, bit_width_id] = context.sem_ir().GetIntTypeInfo(lhs.type_id);
920+
auto [is_signed, bit_width_id] =
921+
context.sem_ir().types().GetIntTypeInfo(lhs.type_id);
920922
CARBON_CHECK(bit_width_id != IntId::Invalid,
921923
"Cannot evaluate a generic bit width integer: {0}", lhs);
922924
llvm::APInt lhs_val = context.ints().GetAtWidth(lhs.int_id, bit_width_id);
@@ -1497,6 +1499,7 @@ static auto TryEvalInstInContext(EvalContext& eval_context,
14971499
// TODO: This doesn't properly handle redeclarations. Consider adding a
14981500
// corresponding `Value` inst for each of these cases, or returning the
14991501
// first declaration.
1502+
case SemIR::AdaptDecl::Kind:
15001503
case SemIR::AssociatedConstantDecl::Kind:
15011504
case SemIR::BaseDecl::Kind:
15021505
case SemIR::FieldDecl::Kind:
@@ -1699,7 +1702,6 @@ static auto TryEvalInstInContext(EvalContext& eval_context,
16991702
}
17001703

17011704
// These cases are either not expressions or not constant.
1702-
case SemIR::AdaptDecl::Kind:
17031705
case SemIR::AddrPattern::Kind:
17041706
case SemIR::Assign::Kind:
17051707
case SemIR::BindName::Kind:

toolchain/check/handle_class.cpp

+26-34
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "toolchain/check/context.h"
77
#include "toolchain/check/convert.h"
88
#include "toolchain/check/decl_name_stack.h"
9+
#include "toolchain/check/diagnostic_helpers.h"
910
#include "toolchain/check/eval.h"
1011
#include "toolchain/check/generic.h"
1112
#include "toolchain/check/handle.h"
@@ -387,21 +388,23 @@ auto HandleParseNode(Context& context, Parse::AdaptDeclId node_id) -> bool {
387388
[&] {
388389
CARBON_DIAGNOSTIC(IncompleteTypeInAdaptDecl, Error,
389390
"adapted type {0} is an incomplete type",
390-
SemIR::TypeId);
391+
InstIdAsType);
391392
return context.emitter().Build(node_id, IncompleteTypeInAdaptDecl,
392-
adapted_type_id);
393+
adapted_inst_id);
393394
},
394395
[&] {
395396
CARBON_DIAGNOSTIC(AbstractTypeInAdaptDecl, Error,
396-
"adapted type {0} is an abstract type",
397-
SemIR::TypeId);
397+
"adapted type {0} is an abstract type", InstIdAsType);
398398
return context.emitter().Build(node_id, AbstractTypeInAdaptDecl,
399-
adapted_type_id);
399+
adapted_inst_id);
400400
});
401+
if (adapted_type_id == SemIR::TypeId::Error) {
402+
adapted_inst_id = SemIR::InstId::BuiltinErrorInst;
403+
}
401404

402405
// Build a SemIR representation for the declaration.
403406
class_info.adapt_id = context.AddInst<SemIR::AdaptDecl>(
404-
node_id, {.adapted_type_id = adapted_type_id});
407+
node_id, {.adapted_type_inst_id = adapted_inst_id});
405408

406409
// Extend the class scope with the adapted type's scope if requested.
407410
if (introducer.modifier_set.HasAnyOf(KeywordModifierSet::Extend)) {
@@ -432,9 +435,10 @@ struct BaseInfo {
432435
SemIR::NameScopeId scope_id;
433436
SemIR::InstId inst_id;
434437
};
435-
constexpr BaseInfo BaseInfo::Error = {.type_id = SemIR::TypeId::Error,
436-
.scope_id = SemIR::NameScopeId::Invalid,
437-
.inst_id = SemIR::InstId::Invalid};
438+
constexpr BaseInfo BaseInfo::Error = {
439+
.type_id = SemIR::TypeId::Error,
440+
.scope_id = SemIR::NameScopeId::Invalid,
441+
.inst_id = SemIR::InstId::BuiltinErrorInst};
438442
} // namespace
439443

440444
// Diagnoses an attempt to derive from a final type.
@@ -531,7 +535,7 @@ auto HandleParseNode(Context& context, Parse::BaseDeclId node_id) -> bool {
531535
context.GetUnboundElementType(class_info.self_type_id, base_info.type_id);
532536
class_info.base_id = context.AddInst<SemIR::BaseDecl>(
533537
node_id, {.type_id = field_type_id,
534-
.base_type_id = base_info.type_id,
538+
.base_type_inst_id = base_info.inst_id,
535539
.index = SemIR::ElementIndex::Invalid});
536540

537541
if (base_info.type_id != SemIR::TypeId::Error) {
@@ -607,27 +611,17 @@ static auto CheckCompleteAdapterClassType(Context& context,
607611
}
608612

609613
// The object representation of the adapter is the object representation
610-
// of the adapted type. This is the adapted type itself unless it's a class
611-
// type.
612-
//
613-
// TODO: The object representation of `const T` should also be the object
614-
// representation of `T`.
615-
auto adapted_type_id = context.insts()
616-
.GetAs<SemIR::AdaptDecl>(class_info.adapt_id)
617-
.adapted_type_id;
618-
if (auto adapted_class =
619-
context.types().TryGetAs<SemIR::ClassType>(adapted_type_id)) {
620-
auto& adapted_class_info = context.classes().Get(adapted_class->class_id);
621-
if (adapted_class_info.adapt_id.is_valid()) {
622-
return adapted_class_info.complete_type_witness_id;
623-
}
624-
}
614+
// of the adapted type.
615+
auto adapted_type_id =
616+
class_info.GetAdaptedType(context.sem_ir(), SemIR::SpecificId::Invalid);
617+
auto object_repr_id = context.types().GetObjectRepr(adapted_type_id);
625618

626619
return context.AddInst<SemIR::CompleteTypeWitness>(
627620
node_id,
628621
{.type_id = context.GetBuiltinType(SemIR::BuiltinInstKind::WitnessType),
629-
.object_repr_id = adapted_type_id});
622+
.object_repr_id = object_repr_id});
630623
}
624+
631625
static auto AddStructTypeFields(
632626
Context& context,
633627
llvm::SmallVector<SemIR::StructTypeField>& struct_type_fields)
@@ -664,11 +658,12 @@ static auto CheckCompleteClassType(Context& context, Parse::NodeId node_id,
664658
}
665659

666660
bool defining_vptr = class_info.is_dynamic;
667-
if (class_info.base_id.is_valid()) {
668-
auto base_info = context.insts().GetAs<SemIR::BaseDecl>(class_info.base_id);
661+
auto base_type_id =
662+
class_info.GetBaseType(context.sem_ir(), SemIR::SpecificId::Invalid);
663+
if (base_type_id.is_valid()) {
669664
// TODO: If the base class is template dependent, we will need to decide
670665
// whether to add a vptr as part of instantiation.
671-
if (auto* base_class_info = TryGetAsClass(context, base_info.base_type_id);
666+
if (auto* base_class_info = TryGetAsClass(context, base_type_id);
672667
base_class_info && base_class_info->is_dynamic) {
673668
defining_vptr = false;
674669
}
@@ -684,16 +679,13 @@ static auto CheckCompleteClassType(Context& context, Parse::NodeId node_id,
684679
.type_id = context.GetPointerType(
685680
context.GetBuiltinType(SemIR::BuiltinInstKind::VtableType))});
686681
}
687-
if (class_info.base_id.is_valid()) {
682+
if (base_type_id.is_valid()) {
688683
auto base_decl = context.insts().GetAs<SemIR::BaseDecl>(class_info.base_id);
689684
base_decl.index =
690685
SemIR::ElementIndex{static_cast<int>(struct_type_fields.size())};
691686
context.ReplaceInstPreservingConstantValue(class_info.base_id, base_decl);
692687
struct_type_fields.push_back(
693-
{.name_id = SemIR::NameId::Base,
694-
.type_id = context.insts()
695-
.GetAs<SemIR::BaseDecl>(class_info.base_id)
696-
.base_type_id});
688+
{.name_id = SemIR::NameId::Base, .type_id = base_type_id});
697689
}
698690

699691
return context.AddInst<SemIR::CompleteTypeWitness>(

0 commit comments

Comments
 (0)