Rename FacetTypeInfo to DeclaredFacetType (#7528)

This resolves a TODO comment in code.
This commit is contained in:
Özgür T. Önsoy
2026-07-20 19:15:09 +00:00
committed by GitHub
parent 085e45093e
commit 40028098ce
49 changed files with 682 additions and 593 deletions
+1 -1
View File
@@ -82,7 +82,7 @@ Example usage:
"constraint": "SemIR::MakeNamedConstraintId",
"symbolic_constant": "SemIR::MakeSymbolicConstantId",
"entity_name": "SemIR::MakeEntityNameId",
"facet_type": "SemIR::MakeFacetTypeId",
"declared_facet_type": "SemIR::MakeDeclaredFacetTypeId",
"function": "SemIR::MakeFunctionId",
"generic": "SemIR::MakeGenericId",
"impl": "SemIR::MakeImplId",
+3 -3
View File
@@ -31,7 +31,7 @@
#include "toolchain/parse/node_ids.h"
#include "toolchain/parse/tree.h"
#include "toolchain/parse/tree_and_subtrees.h"
#include "toolchain/sem_ir/facet_type_info.h"
#include "toolchain/sem_ir/declared_facet_type.h"
#include "toolchain/sem_ir/file.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/import_ir.h"
@@ -384,8 +384,8 @@ class Context {
auto associated_constants() -> SemIR::AssociatedConstantStore& {
return sem_ir().associated_constants();
}
auto facet_types() -> SemIR::FacetTypeInfoStore& {
return sem_ir().facet_types();
auto declared_facet_types() -> SemIR::DeclaredFacetTypeStore& {
return sem_ir().declared_facet_types();
}
auto identified_facet_types() -> SemIR::IdentifiedFacetTypeStore& {
return sem_ir().identified_facet_types();
+3 -2
View File
@@ -188,8 +188,9 @@ static auto CanDestroyType(
// For facet values, look if the FacetType provides the same.
if (auto facet_type =
context.types().TryGetAs<SemIR::FacetType>(inst.type_id())) {
const auto& info = context.facet_types().Get(facet_type->facet_type_id);
for (auto interface : info.extend_constraints) {
const auto& declared_facet_type =
context.declared_facet_types().Get(facet_type->declared_facet_type_id);
for (auto interface : declared_facet_type.extend_constraints) {
if (interface.interface_id == destroy_interface_id) {
return DestroyFormat::Trivial;
}
+4 -2
View File
@@ -115,8 +115,10 @@ auto DiagnosticEmitter::ConvertArg(llvm::Any arg) const -> llvm::Any {
sem_ir_->types().GetTypeInstId(*type_id)) +
"`";
}
if (auto* facet_type_id = llvm::any_cast<SemIR::FacetTypeId>(&arg)) {
return "`" + StringifyFacetType(*sem_ir_, *facet_type_id) + "`";
if (auto* declared_facet_type_id =
llvm::any_cast<SemIR::DeclaredFacetTypeId>(&arg)) {
return "`" + StringifyDeclaredFacetType(*sem_ir_, *declared_facet_type_id) +
"`";
}
if (auto* specific_id = llvm::any_cast<SemIR::SpecificId>(&arg)) {
return "`" + StringifySpecific(*sem_ir_, *specific_id) + "`";
+3 -3
View File
@@ -63,10 +63,10 @@ LLVM_DUMP_METHOD static auto Dump(const Context& context,
return SemIR::Dump(context.sem_ir(), entity_name_id);
}
LLVM_DUMP_METHOD static auto Dump(const Context& context,
SemIR::FacetTypeId facet_type_id)
LLVM_DUMP_METHOD static auto Dump(
const Context& context, SemIR::DeclaredFacetTypeId declared_facet_type_id)
-> std::string {
return SemIR::Dump(context.sem_ir(), facet_type_id);
return SemIR::Dump(context.sem_ir(), declared_facet_type_id);
}
LLVM_DUMP_METHOD static auto Dump(const Context& context,
+137 -109
View File
@@ -31,7 +31,7 @@
#include "toolchain/diagnostics/format_providers.h"
#include "toolchain/sem_ir/builtin_function_kind.h"
#include "toolchain/sem_ir/constant.h"
#include "toolchain/sem_ir/facet_type_info.h"
#include "toolchain/sem_ir/declared_facet_type.h"
#include "toolchain/sem_ir/function.h"
#include "toolchain/sem_ir/generic.h"
#include "toolchain/sem_ir/id_kind.h"
@@ -244,8 +244,8 @@ class EvalContext {
auto specific_interfaces() -> SemIR::SpecificInterfaceStore& {
return sem_ir().specific_interfaces();
}
auto facet_types() -> SemIR::FacetTypeInfoStore& {
return sem_ir().facet_types();
auto declared_facet_types() -> SemIR::DeclaredFacetTypeStore& {
return sem_ir().declared_facet_types();
}
auto generics() -> const SemIR::GenericStore& { return sem_ir().generics(); }
auto specifics() -> const SemIR::SpecificStore& {
@@ -441,14 +441,16 @@ static auto MakeFloatResult(Context& context, SemIR::TypeId type_id,
}
// Creates a FacetType constant.
static auto MakeFacetTypeResult(Context& context,
const SemIR::FacetTypeInfo& info, Phase phase)
-> SemIR::ConstantId {
SemIR::FacetTypeId facet_type_id = context.facet_types().Add(info);
return MakeConstantResult(context,
SemIR::FacetType{.type_id = SemIR::TypeType::TypeId,
.facet_type_id = facet_type_id},
phase);
static auto MakeFacetTypeResult(
Context& context, const SemIR::DeclaredFacetType& declared_facet_type,
Phase phase) -> SemIR::ConstantId {
SemIR::DeclaredFacetTypeId declared_facet_type_id =
context.declared_facet_types().Add(declared_facet_type);
return MakeConstantResult(
context,
SemIR::FacetType{.type_id = SemIR::TypeType::TypeId,
.declared_facet_type_id = declared_facet_type_id},
phase);
}
// `GetConstantValue` checks to see whether the provided ID describes a value
@@ -723,57 +725,62 @@ static auto GetConstantValue(EvalContext& eval_context,
GetConstantValue(eval_context, interface.specific_id, phase)});
}
// Like `GetConstantValue` but for a `FacetTypeInfo`.
static auto GetConstantFacetTypeInfo(EvalContext& eval_context,
SemIR::LocId loc_id,
const SemIR::FacetTypeInfo& orig,
Phase* phase) -> SemIR::FacetTypeInfo {
SemIR::FacetTypeInfo info = {};
// Like `GetConstantValue` but for a `DeclaredFacetType`.
static auto GetConstantDeclaredFacetType(EvalContext& eval_context,
SemIR::LocId loc_id,
const SemIR::DeclaredFacetType& orig,
Phase* phase)
-> SemIR::DeclaredFacetType {
SemIR::DeclaredFacetType declared_facet_type = {};
// Phase of constraints whose `.Self` refers to the type constrained by this
// facet type.
Phase self_phase = Phase::Concrete;
info.extend_constraints.reserve(orig.extend_constraints.size());
declared_facet_type.extend_constraints.reserve(
orig.extend_constraints.size());
for (const auto& extend : orig.extend_constraints) {
// TODO: Add GetConstantValue for SpecificInterface.
info.extend_constraints.push_back(
declared_facet_type.extend_constraints.push_back(
{.interface_id = extend.interface_id,
.specific_id =
GetConstantValue(eval_context, extend.specific_id, phase)});
}
info.self_impls_constraints.reserve(orig.self_impls_constraints.size());
declared_facet_type.self_impls_constraints.reserve(
orig.self_impls_constraints.size());
for (const auto& self_impls : orig.self_impls_constraints) {
// TODO: Add GetConstantValue for SpecificInterface.
info.self_impls_constraints.push_back(
declared_facet_type.self_impls_constraints.push_back(
{.interface_id = self_impls.interface_id,
.specific_id = GetConstantValue(eval_context, self_impls.specific_id,
&self_phase)});
}
info.extend_named_constraints.reserve(orig.extend_named_constraints.size());
declared_facet_type.extend_named_constraints.reserve(
orig.extend_named_constraints.size());
for (const auto& extend : orig.extend_named_constraints) {
// TODO: Add GetConstantValue for SpecificNamedConstraint.
info.extend_named_constraints.push_back(
declared_facet_type.extend_named_constraints.push_back(
{.named_constraint_id = extend.named_constraint_id,
.specific_id =
GetConstantValue(eval_context, extend.specific_id, phase)});
}
info.self_impls_named_constraints.reserve(
declared_facet_type.self_impls_named_constraints.reserve(
orig.self_impls_named_constraints.size());
for (const auto& self_impls : orig.self_impls_named_constraints) {
// TODO: Add GetConstantValue for SpecificNamedConstraint.
info.self_impls_named_constraints.push_back(
declared_facet_type.self_impls_named_constraints.push_back(
{.named_constraint_id = self_impls.named_constraint_id,
.specific_id = GetConstantValue(eval_context, self_impls.specific_id,
&self_phase)});
}
info.type_impls_interfaces.reserve(orig.type_impls_interfaces.size());
declared_facet_type.type_impls_interfaces.reserve(
orig.type_impls_interfaces.size());
for (const auto& type_impls : orig.type_impls_interfaces) {
info.type_impls_interfaces.push_back(
declared_facet_type.type_impls_interfaces.push_back(
{.self_type =
GetConstantValue(eval_context, type_impls.self_type, &self_phase),
// TODO: Add GetConstantValue for SpecificInterface.
@@ -784,10 +791,10 @@ static auto GetConstantFacetTypeInfo(EvalContext& eval_context,
&self_phase)}});
}
info.type_impls_named_constraints.reserve(
declared_facet_type.type_impls_named_constraints.reserve(
orig.type_impls_named_constraints.size());
for (const auto& type_impls : orig.type_impls_named_constraints) {
info.type_impls_named_constraints.push_back(
declared_facet_type.type_impls_named_constraints.push_back(
{.self_type =
GetConstantValue(eval_context, type_impls.self_type, &self_phase),
// TODO: Add GetConstantValue for SpecificNamedConstraint.
@@ -810,13 +817,14 @@ static auto GetConstantFacetTypeInfo(EvalContext& eval_context,
// which must be handled gracefully during resolution. They will be replaced
// with the constant value of the `ImplWitnessAccess` below when they are
// substituted with a constant value.
info.rewrite_constraints = orig.rewrite_constraints;
if (!ResolveFacetTypeRewriteConstraints(eval_context.context(), loc_id,
info.rewrite_constraints)) {
declared_facet_type.rewrite_constraints = orig.rewrite_constraints;
if (!ResolveFacetTypeRewriteConstraints(
eval_context.context(), loc_id,
declared_facet_type.rewrite_constraints)) {
*phase = Phase::UnknownDueToError;
}
for (auto& rewrite : info.rewrite_constraints) {
for (auto& rewrite : declared_facet_type.rewrite_constraints) {
auto lhs_id =
RequireConstantValue(eval_context, rewrite.lhs_id, &self_phase);
auto rhs_id =
@@ -831,19 +839,19 @@ static auto GetConstantFacetTypeInfo(EvalContext& eval_context,
}
// TODO: Process other requirements.
info.other_requirements = orig.other_requirements;
declared_facet_type.other_requirements = orig.other_requirements;
info.Canonicalize();
return info;
declared_facet_type.Canonicalize();
return declared_facet_type;
}
static auto GetConstantValue(EvalContext& eval_context,
SemIR::FacetTypeId facet_type_id, Phase* phase)
-> SemIR::FacetTypeId {
SemIR::FacetTypeInfo info = GetConstantFacetTypeInfo(
SemIR::DeclaredFacetTypeId declared_facet_type_id,
Phase* phase) -> SemIR::DeclaredFacetTypeId {
SemIR::DeclaredFacetType declared_facet_type = GetConstantDeclaredFacetType(
eval_context, SemIR::LocId::None,
eval_context.facet_types().Get(facet_type_id), phase);
return eval_context.facet_types().Add(info);
eval_context.declared_facet_types().Get(declared_facet_type_id), phase);
return eval_context.declared_facet_types().Add(declared_facet_type);
}
static auto GetConstantValue(EvalContext& eval_context,
@@ -1002,27 +1010,30 @@ static auto ResolveSpecificDeclForSpecificId(EvalContext& eval_context,
specific_id);
}
static auto ResolveSpecificDeclForArg(EvalContext& eval_context,
SemIR::FacetTypeId facet_type_id)
-> void {
const auto& info = eval_context.context().facet_types().Get(facet_type_id);
for (const auto& interface : info.extend_constraints) {
static auto ResolveSpecificDeclForArg(
EvalContext& eval_context,
SemIR::DeclaredFacetTypeId declared_facet_type_id) -> void {
const auto& declared_facet_type =
eval_context.context().declared_facet_types().Get(declared_facet_type_id);
for (const auto& interface : declared_facet_type.extend_constraints) {
ResolveSpecificDeclForSpecificId(eval_context, interface.specific_id);
}
for (const auto& interface : info.self_impls_constraints) {
for (const auto& interface : declared_facet_type.self_impls_constraints) {
ResolveSpecificDeclForSpecificId(eval_context, interface.specific_id);
}
for (const auto& constraint : info.extend_named_constraints) {
for (const auto& constraint : declared_facet_type.extend_named_constraints) {
ResolveSpecificDeclForSpecificId(eval_context, constraint.specific_id);
}
for (const auto& constraint : info.self_impls_named_constraints) {
for (const auto& constraint :
declared_facet_type.self_impls_named_constraints) {
ResolveSpecificDeclForSpecificId(eval_context, constraint.specific_id);
}
for (const auto& type_impls : info.type_impls_interfaces) {
for (const auto& type_impls : declared_facet_type.type_impls_interfaces) {
ResolveSpecificDeclForSpecificId(eval_context,
type_impls.specific_interface.specific_id);
}
for (const auto& type_impls : info.type_impls_named_constraints) {
for (const auto& type_impls :
declared_facet_type.type_impls_named_constraints) {
ResolveSpecificDeclForSpecificId(
eval_context, type_impls.specific_named_constraint.specific_id);
}
@@ -2263,13 +2274,14 @@ static auto PerformBuiltinBoolComparison(
: lhs != rhs);
}
// Converts a call argument to a FacetTypeId.
// Converts a call argument to a DeclaredFacetTypeId.
static auto ArgToFacetTypeId(Context& context, SemIR::LocId loc_id,
SemIR::InstId arg_id) -> SemIR::FacetTypeId {
SemIR::InstId arg_id)
-> SemIR::DeclaredFacetTypeId {
auto type_arg_id = context.types().GetAsTypeInstId(arg_id);
if (auto facet_type =
context.insts().TryGetAs<SemIR::FacetType>(type_arg_id)) {
return facet_type->facet_type_id;
return facet_type->declared_facet_type_id;
}
CARBON_DIAGNOSTIC(FacetTypeRequiredForTypeAndOperator, Error,
"non-facet type {0} combined with `&` operator",
@@ -2280,7 +2292,7 @@ static auto ArgToFacetTypeId(Context& context, SemIR::LocId loc_id,
// The `arg_id` instruction has no location in it for some reason.
context.emitter().Emit(loc_id, FacetTypeRequiredForTypeAndOperator,
context.types().GetTypeIdForTypeInstId(type_arg_id));
return SemIR::FacetTypeId::None;
return SemIR::DeclaredFacetTypeId::None;
}
// Returns a constant for a call to a builtin function.
@@ -2390,29 +2402,33 @@ static auto MakeConstantForBuiltinCall(EvalContext& eval_context,
case SemIR::BuiltinFunctionKind::TypeAnd: {
CARBON_CHECK(arg_ids.size() == 2);
auto lhs_facet_type_id = ArgToFacetTypeId(context, loc_id, arg_ids[0]);
auto rhs_facet_type_id = ArgToFacetTypeId(context, loc_id, arg_ids[1]);
auto lhs_declared_facet_type_id =
ArgToFacetTypeId(context, loc_id, arg_ids[0]);
auto rhs_declared_facet_type_id =
ArgToFacetTypeId(context, loc_id, arg_ids[1]);
// Allow errors to be diagnosed for both sides of the operator before
// returning here if any error occurred on either side.
if (!lhs_facet_type_id.has_value() || !rhs_facet_type_id.has_value()) {
if (!lhs_declared_facet_type_id.has_value() ||
!rhs_declared_facet_type_id.has_value()) {
return SemIR::ErrorInst::ConstantId;
}
// Reuse one of the argument instructions if nothing has changed.
if (lhs_facet_type_id == rhs_facet_type_id) {
if (lhs_declared_facet_type_id == rhs_declared_facet_type_id) {
return context.types().GetConstantId(
context.types().GetTypeIdForTypeInstId(arg_ids[0]));
}
auto combined_info = SemIR::FacetTypeInfo::Combine(
context.facet_types().Get(lhs_facet_type_id),
context.facet_types().Get(rhs_facet_type_id));
auto combined_declared_facet_type = SemIR::DeclaredFacetType::Combine(
context.declared_facet_types().Get(lhs_declared_facet_type_id),
context.declared_facet_types().Get(rhs_declared_facet_type_id));
if (!ResolveFacetTypeRewriteConstraints(
eval_context.context(), loc_id,
combined_info.rewrite_constraints)) {
combined_declared_facet_type.rewrite_constraints)) {
phase = Phase::UnknownDueToError;
}
combined_info.Canonicalize();
return MakeFacetTypeResult(eval_context.context(), combined_info, phase);
combined_declared_facet_type.Canonicalize();
return MakeFacetTypeResult(eval_context.context(),
combined_declared_facet_type, phase);
}
case SemIR::BuiltinFunctionKind::CharLiteralMakeType: {
@@ -3020,8 +3036,8 @@ auto TryEvalTypedInst<SemIR::Temporary>(EvalContext& eval_context,
static auto AddRequirementBase(Context& context,
SemIR::RequirementBaseFacetType base,
SemIR::FacetTypeInfo* info, Phase* phase)
-> void {
SemIR::DeclaredFacetType* declared_facet_type,
Phase* phase) -> void {
auto base_type_inst_id =
context.constant_values().GetConstantTypeInstId(base.base_type_inst_id);
if (base_type_inst_id == SemIR::ErrorInst::TypeInstId) {
@@ -3031,25 +3047,31 @@ static auto AddRequirementBase(Context& context,
if (auto base_facet_type =
context.insts().TryGetAs<SemIR::FacetType>(base_type_inst_id)) {
const auto& base_info =
context.facet_types().Get(base_facet_type->facet_type_id);
info->extend_constraints.append(base_info.extend_constraints);
info->extend_named_constraints.append(base_info.extend_named_constraints);
info->self_impls_constraints.append(base_info.self_impls_constraints);
info->self_impls_named_constraints.append(
base_info.self_impls_named_constraints);
info->type_impls_interfaces.append(base_info.type_impls_interfaces);
info->type_impls_named_constraints.append(
base_info.type_impls_named_constraints);
info->rewrite_constraints.append(base_info.rewrite_constraints);
info->other_requirements |= base_info.other_requirements;
const auto& base_declared_facet_type = context.declared_facet_types().Get(
base_facet_type->declared_facet_type_id);
declared_facet_type->extend_constraints.append(
base_declared_facet_type.extend_constraints);
declared_facet_type->extend_named_constraints.append(
base_declared_facet_type.extend_named_constraints);
declared_facet_type->self_impls_constraints.append(
base_declared_facet_type.self_impls_constraints);
declared_facet_type->self_impls_named_constraints.append(
base_declared_facet_type.self_impls_named_constraints);
declared_facet_type->type_impls_interfaces.append(
base_declared_facet_type.type_impls_interfaces);
declared_facet_type->type_impls_named_constraints.append(
base_declared_facet_type.type_impls_named_constraints);
declared_facet_type->rewrite_constraints.append(
base_declared_facet_type.rewrite_constraints);
declared_facet_type->other_requirements |=
base_declared_facet_type.other_requirements;
}
}
static auto AddRequirementRewrite(Context& context,
SemIR::RequirementRewrite rewrite,
SemIR::FacetTypeInfo* info, Phase* phase)
-> void {
SemIR::DeclaredFacetType* declared_facet_type,
Phase* phase) -> void {
auto lhs_id = context.constant_values().GetConstantInstId(rewrite.lhs_id);
auto rhs_id = context.constant_values().GetConstantInstId(rewrite.rhs_id);
if (lhs_id == SemIR::ErrorInst::InstId ||
@@ -3065,9 +3087,9 @@ static auto AddRequirementRewrite(Context& context,
return;
}
// The FacetTypeInfo must hold canonical IDs for constant comparison, yet here
// we must insert the non-canonical IDs:
// * Rewrite constraints are resolved once the FacetTypeInfo is fully
// The DeclaredFacetType must hold canonical IDs for constant comparison, yet
// here we must insert the non-canonical IDs:
// * Rewrite constraints are resolved once the DeclaredFacetType is fully
// constructed in order to produce the constant value of the facet type.
// That resolution step needs the non-canonical insts to do its job
// correctly. For instance, the LHS may be a `ImplWitnessAccessSubstituted`
@@ -3083,13 +3105,13 @@ static auto AddRequirementRewrite(Context& context,
// values here. We only need to use canonical values if we need to observe
// the constant value, such as to determine in the RHS has a runtime value
// above.
info->rewrite_constraints.push_back(
declared_facet_type->rewrite_constraints.push_back(
{.lhs_id = rewrite.lhs_id, .rhs_id = rewrite.rhs_id});
}
static auto AddRequirementImpls(Context& context, SemIR::RequirementImpls impls,
SemIR::FacetTypeInfo* info, Phase* phase)
-> void {
SemIR::DeclaredFacetType* declared_facet_type,
Phase* phase) -> void {
auto lhs_id = context.constant_values().GetConstantInstId(impls.lhs_id);
auto rhs_id = context.constant_values().GetConstantInstId(impls.rhs_id);
if (lhs_id == SemIR::ErrorInst::InstId ||
@@ -3104,7 +3126,8 @@ static auto AddRequirementImpls(Context& context, SemIR::RequirementImpls impls,
}
auto facet_type = context.insts().GetAs<SemIR::FacetType>(rhs_id);
const auto& rhs = context.facet_types().Get(facet_type.facet_type_id);
const auto& rhs =
context.declared_facet_types().Get(facet_type.declared_facet_type_id);
// We forbid `where` on the RHS of another `where`, so non-extend constraints
// can't be part of a facet type on the RHS of `where ... impls`.
@@ -3120,36 +3143,36 @@ static auto AddRequirementImpls(Context& context, SemIR::RequirementImpls impls,
// type constrains for `.Self` gets forwarded to the output facet type to
// also constrain `.Self`. Nothing on the RHS of `impls` can extend the
// resulting facet type.
llvm::append_range(info->self_impls_constraints, rhs.extend_constraints);
llvm::append_range(info->self_impls_named_constraints,
llvm::append_range(declared_facet_type->self_impls_constraints,
rhs.extend_constraints);
llvm::append_range(declared_facet_type->self_impls_named_constraints,
rhs.extend_named_constraints);
} else {
auto lhs_facet_or_type = GetCanonicalFacetOrTypeValue(context, lhs_id);
auto extends_interface = [=](SemIR::SpecificInterface si)
-> SemIR::FacetTypeInfo::TypeImplsInterface {
-> SemIR::DeclaredFacetType::TypeImplsInterface {
return {lhs_facet_or_type, si};
};
auto extends_constraint = [=](SemIR::SpecificNamedConstraint sc)
-> SemIR::FacetTypeInfo::TypeImplsNamedConstraint {
-> SemIR::DeclaredFacetType::TypeImplsNamedConstraint {
return {lhs_facet_or_type, sc};
};
// Extend constraints are copied over without replacing anything, but are
// converted to type impls constraints so they apply to the LHS type.
llvm::append_range(
info->type_impls_interfaces,
declared_facet_type->type_impls_interfaces,
llvm::map_range(rhs.extend_constraints, extends_interface));
llvm::append_range(
info->type_impls_named_constraints,
declared_facet_type->type_impls_named_constraints,
llvm::map_range(rhs.extend_named_constraints, extends_constraint));
}
}
static auto AddRequirementEquivalent(Context& context,
SemIR::RequirementEquivalent equiv,
SemIR::FacetTypeInfo* info, Phase* phase)
-> void {
static auto AddRequirementEquivalent(
Context& context, SemIR::RequirementEquivalent equiv,
SemIR::DeclaredFacetType* declared_facet_type, Phase* phase) -> void {
auto lhs_id = context.constant_values().GetConstantInstId(equiv.lhs_id);
auto rhs_id = context.constant_values().GetConstantInstId(equiv.rhs_id);
if (lhs_id == SemIR::ErrorInst::InstId ||
@@ -3158,11 +3181,11 @@ static auto AddRequirementEquivalent(Context& context,
return;
}
// TODO: Handle equality requirements.
info->other_requirements = true;
declared_facet_type->other_requirements = true;
}
// Add the constraints from the WhereExpr instruction into a FacetTypeInfo in
// order to construct a FacetType constant value.
// Add the constraints from the WhereExpr instruction into a DeclaredFacetType
// in order to construct a FacetType constant value.
//
// TODO: Convert this to an EvalConstantInst function. This will require
// providing a `GetConstantValue` overload for a requirement block.
@@ -3173,7 +3196,7 @@ auto TryEvalTypedInst<SemIR::WhereExpr>(EvalContext& eval_context,
auto typed_inst = inst.As<SemIR::WhereExpr>();
Phase phase = Phase::Concrete;
SemIR::FacetTypeInfo info;
SemIR::DeclaredFacetType declared_facet_type;
if (inst.type_id() == SemIR::ErrorInst::TypeId) {
return SemIR::ErrorInst::ConstantId;
@@ -3192,19 +3215,23 @@ auto TryEvalTypedInst<SemIR::WhereExpr>(EvalContext& eval_context,
auto inst = eval_context.insts().Get(inst_id);
CARBON_KIND_SWITCH(inst) {
case CARBON_KIND(SemIR::RequirementBaseFacetType base): {
AddRequirementBase(eval_context.context(), base, &info, &phase);
AddRequirementBase(eval_context.context(), base, &declared_facet_type,
&phase);
break;
}
case CARBON_KIND(SemIR::RequirementRewrite rewrite): {
AddRequirementRewrite(eval_context.context(), rewrite, &info, &phase);
AddRequirementRewrite(eval_context.context(), rewrite,
&declared_facet_type, &phase);
break;
}
case CARBON_KIND(SemIR::RequirementImpls impls): {
AddRequirementImpls(eval_context.context(), impls, &info, &phase);
AddRequirementImpls(eval_context.context(), impls, &declared_facet_type,
&phase);
break;
}
case CARBON_KIND(SemIR::RequirementEquivalent equiv): {
AddRequirementEquivalent(eval_context.context(), equiv, &info, &phase);
AddRequirementEquivalent(eval_context.context(), equiv,
&declared_facet_type, &phase);
break;
}
default:
@@ -3213,9 +3240,10 @@ auto TryEvalTypedInst<SemIR::WhereExpr>(EvalContext& eval_context,
}
}
auto const_info = GetConstantFacetTypeInfo(
eval_context, SemIR::LocId(where_inst_id), info, &phase);
return MakeFacetTypeResult(eval_context.context(), const_info, phase);
auto const_declared_facet_type = GetConstantDeclaredFacetType(
eval_context, SemIR::LocId(where_inst_id), declared_facet_type, &phase);
return MakeFacetTypeResult(eval_context.context(), const_declared_facet_type,
phase);
}
// Implementation for `TryEvalInst`, wrapping `Context` with `EvalContext`.
+4 -4
View File
@@ -306,10 +306,10 @@ static auto TryFindValueInRewriteConstraints(
auto access_interface =
context.specific_interfaces().Get(specific_interface_id);
auto access_self_facet_type_id =
auto access_self_declared_facet_type_id =
context.types()
.GetAs<SemIR::FacetType>(access_self_type_id)
.facet_type_id;
.declared_facet_type_id;
// TODO: We could consider something better than linear search here, such as a
// map. However that would probably require heap allocations which may be
// worse overall since the number of rewrite constraints is generally low. If
@@ -317,8 +317,8 @@ static auto TryFindValueInRewriteConstraints(
// grouped together, as in ResolveFacetTypeRewriteConstraints(), and limited
// to just the `ImplWitnessAccess` entries, then a binary search may work
// here.
for (const auto& rewrite : context.facet_types()
.Get(access_self_facet_type_id)
for (const auto& rewrite : context.declared_facet_types()
.Get(access_self_declared_facet_type_id)
.rewrite_constraints) {
// Look at each rewrite constraint in the self facet's type. If the LHS is
// an `ImplWitnessAccess` into the same interface that `inst` is indexing
+20 -14
View File
@@ -19,22 +19,27 @@ namespace Carbon::Check {
auto FacetTypeFromInterface(Context& context, SemIR::InterfaceId interface_id,
SemIR::SpecificId specific_id) -> SemIR::FacetType {
auto info = SemIR::FacetTypeInfo{};
info.extend_constraints.push_back({interface_id, specific_id});
info.Canonicalize();
SemIR::FacetTypeId facet_type_id = context.facet_types().Add(info);
return {.type_id = SemIR::TypeType::TypeId, .facet_type_id = facet_type_id};
auto declared_facet_type = SemIR::DeclaredFacetType{};
declared_facet_type.extend_constraints.push_back({interface_id, specific_id});
declared_facet_type.Canonicalize();
SemIR::DeclaredFacetTypeId declared_facet_type_id =
context.declared_facet_types().Add(declared_facet_type);
return {.type_id = SemIR::TypeType::TypeId,
.declared_facet_type_id = declared_facet_type_id};
}
auto FacetTypeFromNamedConstraint(Context& context,
SemIR::NamedConstraintId named_constraint_id,
SemIR::SpecificId specific_id)
-> SemIR::FacetType {
auto info = SemIR::FacetTypeInfo{};
info.extend_named_constraints.push_back({named_constraint_id, specific_id});
info.Canonicalize();
SemIR::FacetTypeId facet_type_id = context.facet_types().Add(info);
return {.type_id = SemIR::TypeType::TypeId, .facet_type_id = facet_type_id};
auto declared_facet_type = SemIR::DeclaredFacetType{};
declared_facet_type.extend_named_constraints.push_back(
{named_constraint_id, specific_id});
declared_facet_type.Canonicalize();
SemIR::DeclaredFacetTypeId declared_facet_type_id =
context.declared_facet_types().Add(declared_facet_type);
return {.type_id = SemIR::TypeType::TypeId,
.declared_facet_type_id = declared_facet_type_id};
}
auto GetImplWitnessAccessWithoutSubstitution(Context& context,
@@ -314,7 +319,7 @@ class SubstImplWitnessAccessCallbacks : public SubstInstCallbacks {
auto ResolveFacetTypeRewriteConstraints(
Context& context, SemIR::LocId loc_id,
llvm::SmallVector<SemIR::FacetTypeInfo::RewriteConstraint>& rewrites)
llvm::SmallVector<SemIR::DeclaredFacetType::RewriteConstraint>& rewrites)
-> bool {
if (rewrites.empty()) {
return true;
@@ -426,11 +431,12 @@ auto ResolveFacetTypeRewriteConstraints(
}
auto GetEmptyFacetType(Context& context) -> SemIR::TypeId {
SemIR::FacetTypeId facet_type_id =
context.facet_types().Add(SemIR::FacetTypeInfo{});
SemIR::DeclaredFacetTypeId declared_facet_type_id =
context.declared_facet_types().Add(SemIR::DeclaredFacetType{});
auto const_id = EvalOrAddInst<SemIR::FacetType>(
context, SemIR::LocId::None,
{.type_id = SemIR::TypeType::TypeId, .facet_type_id = facet_type_id});
{.type_id = SemIR::TypeType::TypeId,
.declared_facet_type_id = declared_facet_type_id});
return context.types().GetTypeIdForTypeConstantId(const_id);
}
+1 -1
View File
@@ -55,7 +55,7 @@ auto GetImplWitnessAccessWithoutSubstitution(Context& context,
// value of the facet type should be an error constant.
auto ResolveFacetTypeRewriteConstraints(
Context& context, SemIR::LocId loc_id,
llvm::SmallVector<SemIR::FacetTypeInfo::RewriteConstraint>& rewrites)
llvm::SmallVector<SemIR::DeclaredFacetType::RewriteConstraint>& rewrites)
-> bool;
// Get a FacetType instruction for an empty FacetType. This is the facet
+7 -6
View File
@@ -211,21 +211,22 @@ static auto ValidateRequire(Context& context, SemIR::LocId full_require_loc_id,
if (auto named_constraint =
context.insts().TryGetAs<SemIR::NamedConstraintWithSelfDecl>(
scope_inst_id)) {
const auto& constraint_facet_type_info =
context.facet_types().Get(constraint_facet_type->facet_type_id);
const auto& constraint_declared_facet_type =
context.declared_facet_types().Get(
constraint_facet_type->declared_facet_type_id);
// TODO: Handle other impls named constraints for the
// RequireImplsReferenceCycle diagnostic.
if (constraint_facet_type_info.other_requirements) {
if (constraint_declared_facet_type.other_requirements) {
context.TODO(constraint_loc_id,
"facet type has constraints that we don't handle yet");
return std::nullopt;
}
auto named_constraints_from_type_impls = llvm::map_range(
constraint_facet_type_info.type_impls_named_constraints,
constraint_declared_facet_type.type_impls_named_constraints,
[](auto impls) { return impls.specific_named_constraint; });
auto named_constraints = llvm::concat<const SemIR::SpecificNamedConstraint>(
constraint_facet_type_info.extend_named_constraints,
constraint_facet_type_info.self_impls_named_constraints,
constraint_declared_facet_type.extend_named_constraints,
constraint_declared_facet_type.self_impls_named_constraints,
named_constraints_from_type_impls);
for (auto c : named_constraints) {
if (c.named_constraint_id == named_constraint->named_constraint_id) {
+27 -23
View File
@@ -13,7 +13,7 @@
#include "toolchain/check/subst.h"
#include "toolchain/check/type.h"
#include "toolchain/check/unused.h"
#include "toolchain/sem_ir/facet_type_info.h"
#include "toolchain/sem_ir/declared_facet_type.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/inst.h"
#include "toolchain/sem_ir/typed_insts.h"
@@ -23,10 +23,12 @@ namespace Carbon::Check {
static auto GetExtendedOnlyFacetType(Context& context,
const SemIR::FacetType& facet_type)
-> SemIR::TypeId {
const auto& info = context.facet_types().Get(facet_type.facet_type_id);
auto stripped_info = SemIR::FacetTypeInfo::ExtendedOnly(info);
stripped_info.Canonicalize();
return GetFacetType(context, stripped_info);
const auto& declared_facet_type =
context.declared_facet_types().Get(facet_type.declared_facet_type_id);
auto stripped_declared_facet_type =
SemIR::DeclaredFacetType::ExtendedOnly(declared_facet_type);
stripped_declared_facet_type.Canonicalize();
return GetFacetType(context, stripped_declared_facet_type);
}
static auto GetPeriodSelfType(Context& context,
@@ -106,15 +108,15 @@ auto HandleParseNode(Context& context, Parse::WhereOperandId node_id) -> bool {
if (auto self_facet_type = context.types().TryGetAs<SemIR::FacetType>(
self_with_constraints_type_id)) {
const auto& base_facet_type_info =
context.facet_types().Get(self_facet_type->facet_type_id);
const auto& base_declared_facet_type = context.declared_facet_types().Get(
self_facet_type->declared_facet_type_id);
// Make rewrite constraints from the self facet type available immediately
// to expressions in rewrite constraints for this `where` expression.
//
// Note that the where_stack rewrites need to be frozen. The rewrites in
// the base facet type will be thawed since their `WhereExpr` would have
// already been handled, so we need to freeze them again here.
for (const auto& rewrite : base_facet_type_info.rewrite_constraints) {
for (const auto& rewrite : base_declared_facet_type.rewrite_constraints) {
if (rewrite.lhs_id != SemIR::ErrorInst::InstId) {
auto const_id = context.constant_values().Get(
GetImplWitnessAccessWithoutSubstitution(context, rewrite.lhs_id));
@@ -132,7 +134,7 @@ auto HandleParseNode(Context& context, Parse::WhereOperandId node_id) -> bool {
// base facet type will be thawed since their `WhereExpr` would have already
// been handled, so we need to freeze them again here. Note that
// `period_self` is already frozen since it is created in that state.
for (const auto& impls : base_facet_type_info.self_impls_constraints) {
for (const auto& impls : base_declared_facet_type.self_impls_constraints) {
auto self_frozen_const_id = context.constant_values().Get(period_self);
auto type_const_id =
GetInterfaceType(context, impls.interface_id, impls.specific_id)
@@ -143,7 +145,7 @@ auto HandleParseNode(Context& context, Parse::WhereOperandId node_id) -> bool {
.facet_type_const_id = type_frozen_const_id});
}
for (const auto& impls :
base_facet_type_info.self_impls_named_constraints) {
base_declared_facet_type.self_impls_named_constraints) {
auto self_frozen_const_id = context.constant_values().Get(period_self);
auto type_const_id =
GetNamedConstraintType(context, impls.named_constraint_id,
@@ -154,7 +156,8 @@ auto HandleParseNode(Context& context, Parse::WhereOperandId node_id) -> bool {
{.self_const_id = self_frozen_const_id,
.facet_type_const_id = type_frozen_const_id});
}
for (const auto& type_impls : base_facet_type_info.type_impls_interfaces) {
for (const auto& type_impls :
base_declared_facet_type.type_impls_interfaces) {
auto self_const_id = context.constant_values().Get(type_impls.self_type);
auto self_frozen_const_id = FreezePeriodSelf(context, self_const_id);
auto type_const_id =
@@ -167,7 +170,7 @@ auto HandleParseNode(Context& context, Parse::WhereOperandId node_id) -> bool {
.facet_type_const_id = type_frozen_const_id});
}
for (const auto& type_impls :
base_facet_type_info.type_impls_named_constraints) {
base_declared_facet_type.type_impls_named_constraints) {
auto self_const_id = context.constant_values().Get(type_impls.self_type);
auto self_frozen_const_id = FreezePeriodSelf(context, self_const_id);
auto type_const_id =
@@ -359,20 +362,21 @@ static auto FindDesignatorInSpecific(Context& context,
static auto FindDesignatorInEveryExtendConstraint(Context& context,
SemIR::FacetType facet_type)
-> bool {
const auto& info = context.facet_types().Get(facet_type.facet_type_id);
const auto& declared_facet_type =
context.declared_facet_types().Get(facet_type.declared_facet_type_id);
for (const auto& extend : info.extend_constraints) {
for (const auto& extend : declared_facet_type.extend_constraints) {
if (!FindDesignatorInSpecific(context, extend.specific_id)) {
return false;
}
}
for (const auto& extend : info.extend_named_constraints) {
for (const auto& extend : declared_facet_type.extend_named_constraints) {
if (!FindDesignatorInSpecific(context, extend.specific_id)) {
return false;
}
}
return !info.extend_constraints.empty() ||
!info.extend_named_constraints.empty();
return !declared_facet_type.extend_constraints.empty() ||
!declared_facet_type.extend_named_constraints.empty();
}
auto HandleParseNode(Context& context, Parse::RequirementImplsId node_id)
@@ -442,9 +446,9 @@ auto HandleParseNode(Context& context, Parse::RequirementImplsId node_id)
if (IsPeriodSelfAccess(context, lhs_as_type.inst_id)) {
auto facet_type =
context.types().GetAs<SemIR::FacetType>(rhs_as_type.type_id);
const auto& facet_type_info =
context.facet_types().Get(facet_type.facet_type_id);
for (const auto& rewrite : facet_type_info.rewrite_constraints) {
const auto& declared_facet_type =
context.declared_facet_types().Get(facet_type.declared_facet_type_id);
for (const auto& rewrite : declared_facet_type.rewrite_constraints) {
auto lhs_id = SubstPeriodSelf(
context, rhs_node, context.constant_values().Get(rewrite.lhs_id),
context.constant_values().Get(lhs_as_type.inst_id));
@@ -489,9 +493,9 @@ static auto FindWhere(Context& context, SemIR::ConstantId const_id) -> bool {
if (auto facet_type =
context().insts().TryGetAs<SemIR::FacetType>(inst_id)) {
const auto& info =
context().facet_types().Get(facet_type->facet_type_id);
if (!info.IsExtendedOnly()) {
const auto& declared_facet_type = context().declared_facet_types().Get(
facet_type->declared_facet_type_id);
if (!declared_facet_type.IsExtendedOnly()) {
*found_ = true;
return FullySubstituted;
}
+6 -6
View File
@@ -419,15 +419,15 @@ auto AddImplWitnessForDeclaration(Context& context, SemIR::LocId loc_id,
context.types().GetTypeIdForTypeInstId(impl.constraint_id);
CARBON_CHECK(facet_type_id != SemIR::ErrorInst::TypeId);
auto facet_type = context.types().GetAs<SemIR::FacetType>(facet_type_id);
const auto& facet_type_info =
context.facet_types().Get(facet_type.facet_type_id);
const auto& declared_facet_type =
context.declared_facet_types().Get(facet_type.declared_facet_type_id);
// An iterator over the rewrite_constraints where the LHS of the rewrite names
// a member of the `impl.interface`. This filters out rewrites of names
// from other interfaces, as they do not set values in the witness table.
auto rewrites_into_interface_to_witness = llvm::make_filter_range(
facet_type_info.rewrite_constraints,
[&](const SemIR::FacetTypeInfo::RewriteConstraint& rewrite) {
declared_facet_type.rewrite_constraints,
[&](const SemIR::DeclaredFacetType::RewriteConstraint& rewrite) {
auto access = context.insts().GetAs<SemIR::ImplWitnessAccess>(
GetImplWitnessAccessWithoutSubstitution(context, rewrite.lhs_id));
return WitnessQueryMatchesInterface(context, loc_id, impl.self_id,
@@ -814,13 +814,13 @@ auto CheckRequireDeclsSatisfied(Context& context, SemIR::LocId loc_id,
"interface `{0}` being implemented requires that {1} "
"implements {2}",
SemIR::SpecificInterface, SemIR::TypeId,
SemIR::FacetTypeId);
SemIR::DeclaredFacetTypeId);
context.emitter().Emit(
loc_id, RequireImplsNotImplemented, impl.interface,
context.types().GetTypeIdForTypeConstantId(self_const_id),
context.constant_values()
.GetInstAs<SemIR::FacetType>(facet_type_const_id)
.facet_type_id);
.declared_facet_type_id);
}
}
if (!result.has_value() || result.has_error_value()) {
+15 -15
View File
@@ -24,7 +24,7 @@
#include "toolchain/check/type.h"
#include "toolchain/check/type_completion.h"
#include "toolchain/check/type_structure.h"
#include "toolchain/sem_ir/facet_type_info.h"
#include "toolchain/sem_ir/declared_facet_type.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/impl.h"
#include "toolchain/sem_ir/inst.h"
@@ -120,14 +120,14 @@ static auto FindAssociatedImportIRs(
add_entity(context.interfaces().Get(interface_id));
break;
}
case CARBON_KIND(SemIR::FacetTypeId facet_type_id): {
const auto& facet_type_info =
context.facet_types().Get(facet_type_id);
for (const auto& impl : facet_type_info.extend_constraints) {
case CARBON_KIND(SemIR::DeclaredFacetTypeId declared_facet_type_id): {
const auto& declared_facet_type =
context.declared_facet_types().Get(declared_facet_type_id);
for (const auto& impl : declared_facet_type.extend_constraints) {
add_entity(context.interfaces().Get(impl.interface_id));
push_args(impl.specific_id);
}
for (const auto& impl : facet_type_info.self_impls_constraints) {
for (const auto& impl : declared_facet_type.self_impls_constraints) {
add_entity(context.interfaces().Get(impl.interface_id));
push_args(impl.specific_id);
}
@@ -499,12 +499,12 @@ static auto VerifyQueryFacetTypeConstraints(
SemIR::ConstantId query_facet_type_const_id,
llvm::ArrayRef<SemIR::IdentifiedFacetType::RequiredImpl> req_impls,
llvm::ArrayRef<SemIR::InstId> witness_inst_ids) -> bool {
const auto& facet_type_info = context.facet_types().Get(
const auto& declared_facet_type = context.declared_facet_types().Get(
context.constant_values()
.GetInstAs<SemIR::FacetType>(query_facet_type_const_id)
.facet_type_id);
.declared_facet_type_id);
if (!facet_type_info.rewrite_constraints.empty()) {
if (!declared_facet_type.rewrite_constraints.empty()) {
auto rebuild = [&](SemIR::Inst new_inst) -> SemIR::InstId {
// When rebuilding a witness where `.Self` was replaced, use a witness we
// found in impl lookup instead of performing impl lookup again.
@@ -522,7 +522,7 @@ static auto VerifyQueryFacetTypeConstraints(
return SemIR::InstId::None;
};
for (const auto& rewrite : facet_type_info.rewrite_constraints) {
for (const auto& rewrite : declared_facet_type.rewrite_constraints) {
// Replace `.Self` in rewrite constraints with the query self in order to
// find the provided values of rewrite constraints from the query. This
// includes replacing `.Self` in LookupImplWitness instructions.
@@ -551,8 +551,8 @@ static auto VerifyQueryFacetTypeConstraints(
}
// TODO: Validate that the witnesses satisfy the other requirements in the
// `facet_type_info`.
if (facet_type_info.other_requirements) {
// `declared_facet_type`.
if (declared_facet_type.other_requirements) {
return false;
}
@@ -1158,9 +1158,9 @@ static auto FacetTypeIsSingleInterface(
Context& context, SemIR::TypeId type_id,
SemIR::SpecificInterface specific_interface) -> bool {
auto facet_type = context.types().GetAs<SemIR::FacetType>(type_id);
const auto& facet_type_info =
context.facet_types().Get(facet_type.facet_type_id);
if (auto single = facet_type_info.TryAsSingleExtend()) {
const auto& declared_facet_type =
context.declared_facet_types().Get(facet_type.declared_facet_type_id);
if (auto single = declared_facet_type.TryAsSingleExtend()) {
if (auto* si = std::get_if<SemIR::SpecificInterface>(&*single)) {
return *si == specific_interface;
}
+91 -89
View File
@@ -179,8 +179,8 @@ class ImportContext {
auto import_entity_names() -> const SemIR::EntityNameStore& {
return import_ir().entity_names();
}
auto import_facet_types() -> const SemIR::FacetTypeInfoStore& {
return import_ir().facet_types();
auto import_facet_types() -> const SemIR::DeclaredFacetTypeStore& {
return import_ir().declared_facet_types();
}
auto import_functions() -> const SemIR::FunctionStore& {
return import_ir().functions();
@@ -264,8 +264,8 @@ class ImportContext {
auto local_entity_names() -> SemIR::EntityNameStore& {
return local_ir().entity_names();
}
auto local_facet_types() -> SemIR::FacetTypeInfoStore& {
return local_ir().facet_types();
auto local_facet_types() -> SemIR::DeclaredFacetTypeStore& {
return local_ir().declared_facet_types();
}
auto local_functions() -> SemIR::FunctionStore& {
return local_ir().functions();
@@ -424,8 +424,8 @@ class ImportRefResolver : public ImportContext {
// Pushes a specific onto the work stack. This will only process when the
// current instruction is done, and does not count towards `HasNewWork`. We
// add specifics this way because some instructions (e.g. `FacetTypeInfo`) can
// add multiple specifics.
// add specifics this way because some instructions (e.g. `DeclaredFacetType`)
// can add multiple specifics.
//
// The insert may do extra work moving already-added work on the work stack,
// but that is expected to be okay because the common cases are 0 or 1
@@ -1256,10 +1256,10 @@ static auto GetLocalSpecificInterface(
resolver.local_insts().Get(resolver.local_constant_values().GetInstId(
interface_data.interface_const_id));
if (auto facet_type = interface_const_inst.TryAs<SemIR::FacetType>()) {
const SemIR::FacetTypeInfo& new_facet_type_info =
resolver.local_facet_types().Get(facet_type->facet_type_id);
const SemIR::DeclaredFacetType& new_declared_facet_type =
resolver.local_facet_types().Get(facet_type->declared_facet_type_id);
return std::get<SemIR::SpecificInterface>(
*new_facet_type_info.TryAsSingleExtend());
*new_declared_facet_type.TryAsSingleExtend());
} else {
auto generic_interface_type =
resolver.local_types().GetAs<SemIR::GenericInterfaceType>(
@@ -1310,10 +1310,10 @@ static auto GetLocalSpecificNamedConstraint(
resolver.local_insts().Get(resolver.local_constant_values().GetInstId(
constraint_data.constraint_const_id));
if (auto facet_type = constraint_const_inst.TryAs<SemIR::FacetType>()) {
const SemIR::FacetTypeInfo& new_facet_type_info =
resolver.local_facet_types().Get(facet_type->facet_type_id);
const SemIR::DeclaredFacetType& new_declared_facet_type =
resolver.local_facet_types().Get(facet_type->declared_facet_type_id);
return std::get<SemIR::SpecificNamedConstraint>(
*new_facet_type_info.TryAsSingleExtend());
*new_declared_facet_type.TryAsSingleExtend());
} else {
auto generic_constraint_type =
resolver.local_types().GetAs<SemIR::GenericNamedConstraintType>(
@@ -1342,9 +1342,9 @@ static auto GetLocalNameScopeIdImpl(ImportRefResolver& resolver,
return resolver.local_classes().Get(inst.class_id).scope_id;
}
case CARBON_KIND(SemIR::FacetType inst): {
const SemIR::FacetTypeInfo& facet_type_info =
resolver.local_facet_types().Get(inst.facet_type_id);
if (auto single = facet_type_info.TryAsSingleExtend()) {
const SemIR::DeclaredFacetType& declared_facet_type =
resolver.local_facet_types().Get(inst.declared_facet_type_id);
if (auto single = declared_facet_type.TryAsSingleExtend()) {
// This is the facet type produced by an interface or named constraint
// declaration.
CARBON_KIND_SWITCH(*single) {
@@ -3127,9 +3127,9 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
auto interface_const_inst = resolver.local_insts().Get(
resolver.local_constant_values().GetInstId(interface_const_id));
if (auto facet_type = interface_const_inst.TryAs<SemIR::FacetType>()) {
const SemIR::FacetTypeInfo& facet_type_info =
resolver.local_facet_types().Get(facet_type->facet_type_id);
auto single = facet_type_info.TryAsSingleExtend();
const SemIR::DeclaredFacetType& declared_facet_type =
resolver.local_facet_types().Get(facet_type->declared_facet_type_id);
auto single = declared_facet_type.TryAsSingleExtend();
CARBON_CHECK(single);
interface_id = std::get<SemIR::SpecificInterface>(*single).interface_id;
} else {
@@ -3237,9 +3237,9 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
auto local_facet_type =
resolver.local_constant_values().GetInstAs<SemIR::FacetType>(
interface_const_id);
const auto& local_facet_type_info =
resolver.local_facet_types().Get(local_facet_type.facet_type_id);
auto single_interface = *local_facet_type_info.TryAsSingleExtend();
const auto& local_declared_facet_type = resolver.local_facet_types().Get(
local_facet_type.declared_facet_type_id);
auto single_interface = *local_declared_facet_type.TryAsSingleExtend();
CARBON_KIND_SWITCH(single_interface) {
case CARBON_KIND(SemIR::SpecificInterface specific_interface): {
local_interface_id = specific_interface.interface_id;
@@ -3417,9 +3417,9 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
resolver.local_constant_values().GetInstId(named_constraint_const_id));
if (auto facet_type =
named_constraint_const_inst.TryAs<SemIR::FacetType>()) {
const SemIR::FacetTypeInfo& facet_type_info =
resolver.local_facet_types().Get(facet_type->facet_type_id);
auto single = facet_type_info.TryAsSingleExtend();
const SemIR::DeclaredFacetType& declared_facet_type =
resolver.local_facet_types().Get(facet_type->declared_facet_type_id);
auto single = declared_facet_type.TryAsSingleExtend();
CARBON_CHECK(single);
named_constraint_id =
std::get<SemIR::SpecificNamedConstraint>(*single).named_constraint_id;
@@ -3528,9 +3528,9 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
auto local_facet_type =
resolver.local_constant_values().GetInstAs<SemIR::FacetType>(
constraint_const_id);
const auto& local_facet_type_info =
resolver.local_facet_types().Get(local_facet_type.facet_type_id);
auto single_interface = *local_facet_type_info.TryAsSingleExtend();
const auto& local_declared_facet_type = resolver.local_facet_types().Get(
local_facet_type.declared_facet_type_id);
auto single_interface = *local_declared_facet_type.TryAsSingleExtend();
CARBON_KIND_SWITCH(single_interface) {
case CARBON_KIND(SemIR::SpecificNamedConstraint specific_constraint): {
local_constraint_id = specific_constraint.named_constraint_id;
@@ -3618,101 +3618,103 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
.facet_value_inst_id = facet_value_inst_id});
}
// Collects and assigns constants for a `FacetTypeInfo`. Discards constants when
// `local_facet_type_info` is null.
static auto ResolveFacetTypeInfo(
// Collects and assigns constants for a `DeclaredFacetType`. Discards constants
// when `local_declared_facet_type` is null.
static auto ResolveDeclaredFacetType(
ImportRefResolver& resolver,
const SemIR::FacetTypeInfo& import_facet_type_info,
SemIR::FacetTypeInfo* local_facet_type_info) -> void {
if (local_facet_type_info) {
local_facet_type_info->extend_constraints.reserve(
import_facet_type_info.extend_constraints.size());
const SemIR::DeclaredFacetType& import_declared_facet_type,
SemIR::DeclaredFacetType* local_declared_facet_type) -> void {
if (local_declared_facet_type) {
local_declared_facet_type->extend_constraints.reserve(
import_declared_facet_type.extend_constraints.size());
}
for (auto interface : import_facet_type_info.extend_constraints) {
for (auto interface : import_declared_facet_type.extend_constraints) {
auto data = GetLocalSpecificInterfaceData(resolver, interface);
if (local_facet_type_info) {
local_facet_type_info->extend_constraints.push_back(
if (local_declared_facet_type) {
local_declared_facet_type->extend_constraints.push_back(
GetLocalSpecificInterface(resolver, interface, data));
}
}
if (local_facet_type_info) {
local_facet_type_info->self_impls_constraints.reserve(
import_facet_type_info.self_impls_constraints.size());
if (local_declared_facet_type) {
local_declared_facet_type->self_impls_constraints.reserve(
import_declared_facet_type.self_impls_constraints.size());
}
for (auto interface : import_facet_type_info.self_impls_constraints) {
for (auto interface : import_declared_facet_type.self_impls_constraints) {
auto data = GetLocalSpecificInterfaceData(resolver, interface);
if (local_facet_type_info) {
local_facet_type_info->self_impls_constraints.push_back(
if (local_declared_facet_type) {
local_declared_facet_type->self_impls_constraints.push_back(
GetLocalSpecificInterface(resolver, interface, data));
}
}
if (local_facet_type_info) {
local_facet_type_info->extend_named_constraints.reserve(
import_facet_type_info.extend_named_constraints.size());
if (local_declared_facet_type) {
local_declared_facet_type->extend_named_constraints.reserve(
import_declared_facet_type.extend_named_constraints.size());
}
for (auto constraint : import_facet_type_info.extend_named_constraints) {
for (auto constraint : import_declared_facet_type.extend_named_constraints) {
auto data = GetLocalSpecificNamedConstraintData(resolver, constraint);
if (local_facet_type_info) {
local_facet_type_info->extend_named_constraints.push_back(
if (local_declared_facet_type) {
local_declared_facet_type->extend_named_constraints.push_back(
GetLocalSpecificNamedConstraint(resolver, constraint, data));
}
}
if (local_facet_type_info) {
local_facet_type_info->self_impls_named_constraints.reserve(
import_facet_type_info.self_impls_named_constraints.size());
if (local_declared_facet_type) {
local_declared_facet_type->self_impls_named_constraints.reserve(
import_declared_facet_type.self_impls_named_constraints.size());
}
for (auto constraint : import_facet_type_info.self_impls_named_constraints) {
for (auto constraint :
import_declared_facet_type.self_impls_named_constraints) {
auto data = GetLocalSpecificNamedConstraintData(resolver, constraint);
if (local_facet_type_info) {
local_facet_type_info->self_impls_named_constraints.push_back(
if (local_declared_facet_type) {
local_declared_facet_type->self_impls_named_constraints.push_back(
GetLocalSpecificNamedConstraint(resolver, constraint, data));
}
}
if (local_facet_type_info) {
local_facet_type_info->type_impls_interfaces.reserve(
import_facet_type_info.type_impls_interfaces.size());
if (local_declared_facet_type) {
local_declared_facet_type->type_impls_interfaces.reserve(
import_declared_facet_type.type_impls_interfaces.size());
}
for (const auto& type_impls : import_facet_type_info.type_impls_interfaces) {
for (const auto& type_impls :
import_declared_facet_type.type_impls_interfaces) {
auto self_type = GetLocalConstantInstId(resolver, type_impls.self_type);
auto data =
GetLocalSpecificInterfaceData(resolver, type_impls.specific_interface);
if (local_facet_type_info) {
local_facet_type_info->type_impls_interfaces.push_back(
if (local_declared_facet_type) {
local_declared_facet_type->type_impls_interfaces.push_back(
{self_type, GetLocalSpecificInterface(
resolver, type_impls.specific_interface, data)});
}
}
if (local_facet_type_info) {
local_facet_type_info->type_impls_named_constraints.reserve(
import_facet_type_info.type_impls_named_constraints.size());
if (local_declared_facet_type) {
local_declared_facet_type->type_impls_named_constraints.reserve(
import_declared_facet_type.type_impls_named_constraints.size());
}
for (const auto& type_impls :
import_facet_type_info.type_impls_named_constraints) {
import_declared_facet_type.type_impls_named_constraints) {
auto self_type = GetLocalConstantInstId(resolver, type_impls.self_type);
auto data = GetLocalSpecificNamedConstraintData(
resolver, type_impls.specific_named_constraint);
if (local_facet_type_info) {
local_facet_type_info->type_impls_named_constraints.push_back(
if (local_declared_facet_type) {
local_declared_facet_type->type_impls_named_constraints.push_back(
{self_type,
GetLocalSpecificNamedConstraint(
resolver, type_impls.specific_named_constraint, data)});
}
}
if (local_facet_type_info) {
local_facet_type_info->rewrite_constraints.reserve(
import_facet_type_info.rewrite_constraints.size());
if (local_declared_facet_type) {
local_declared_facet_type->rewrite_constraints.reserve(
import_declared_facet_type.rewrite_constraints.size());
}
for (auto rewrite : import_facet_type_info.rewrite_constraints) {
for (auto rewrite : import_declared_facet_type.rewrite_constraints) {
auto lhs_id = GetLocalConstantInstId(resolver, rewrite.lhs_id);
auto rhs_id = GetLocalConstantInstId(resolver, rewrite.rhs_id);
if (local_facet_type_info) {
local_facet_type_info->rewrite_constraints.push_back(
if (local_declared_facet_type) {
local_declared_facet_type->rewrite_constraints.push_back(
{.lhs_id = lhs_id, .rhs_id = rhs_id});
}
}
@@ -3722,27 +3724,27 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
SemIR::FacetType inst) -> ResolveResult {
CARBON_CHECK(inst.type_id == SemIR::TypeType::TypeId);
const SemIR::FacetTypeInfo& import_facet_type_info =
resolver.import_facet_types().Get(inst.facet_type_id);
const SemIR::DeclaredFacetType& import_declared_facet_type =
resolver.import_facet_types().Get(inst.declared_facet_type_id);
// Ensure values are imported, but discard them to avoid allocations.
ResolveFacetTypeInfo(resolver, import_facet_type_info,
/*local_facet_type_info=*/nullptr);
ResolveDeclaredFacetType(resolver, import_declared_facet_type,
/*local_declared_facet_type=*/nullptr);
if (resolver.HasNewWork()) {
return ResolveResult::Retry();
}
SemIR::FacetTypeInfo local_facet_type_info = {
SemIR::DeclaredFacetType local_declared_facet_type = {
// TODO: Also process the other requirements.
.other_requirements = import_facet_type_info.other_requirements};
// Re-resolve and add values to the local `FacetTypeInfo`.
ResolveFacetTypeInfo(resolver, import_facet_type_info,
&local_facet_type_info);
.other_requirements = import_declared_facet_type.other_requirements};
// Re-resolve and add values to the local `DeclaredFacetType`.
ResolveDeclaredFacetType(resolver, import_declared_facet_type,
&local_declared_facet_type);
SemIR::FacetTypeId facet_type_id =
resolver.local_facet_types().Add(std::move(local_facet_type_info));
SemIR::DeclaredFacetTypeId declared_facet_type_id =
resolver.local_facet_types().Add(std::move(local_declared_facet_type));
return ResolveResult::Deduplicated<SemIR::FacetType>(
resolver,
{.type_id = SemIR::TypeType::TypeId, .facet_type_id = facet_type_id});
resolver, {.type_id = SemIR::TypeType::TypeId,
.declared_facet_type_id = declared_facet_type_id});
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
@@ -4996,8 +4998,8 @@ auto ImportInterface(Context& context, SemIR::ImportIRId import_ir_id,
// A non-generic interface will import as a facet type for that single
// interface.
if (auto facet_type = local_inst.TryAs<SemIR::FacetType>()) {
auto single = context.facet_types()
.Get(facet_type->facet_type_id)
auto single = context.declared_facet_types()
.Get(facet_type->declared_facet_type_id)
.TryAsSingleExtend();
CARBON_CHECK(single,
"Importing an interface didn't produce a single interface");
+4 -3
View File
@@ -404,9 +404,10 @@ auto AppendLookupScopesForConstant(Context& context, SemIR::LocId loc_id,
}
}
auto facet_type_info = context.facet_types().Get(facet_type->facet_type_id);
auto declared_facet_type =
context.declared_facet_types().Get(facet_type->declared_facet_type_id);
// Name lookup into "extend" constraints but not "self impls" constraints.
for (const auto& extend : facet_type_info.extend_constraints) {
for (const auto& extend : declared_facet_type.extend_constraints) {
auto& interface = context.interfaces().Get(extend.interface_id);
// We need to build the inner interface-with-self specific. To do that
@@ -421,7 +422,7 @@ auto AppendLookupScopesForConstant(Context& context, SemIR::LocId loc_id,
.specific_id = interface_with_self_specific_id,
.self_const_id = self_type_const_id});
}
for (const auto& extend : facet_type_info.extend_named_constraints) {
for (const auto& extend : declared_facet_type.extend_named_constraints) {
auto& constraint =
context.named_constraints().Get(extend.named_constraint_id);
+37 -29
View File
@@ -309,8 +309,8 @@ auto SubstPeriodSelfInFacetType(Context& context, SemIR::LocId loc_id,
auto orig_facet_type =
context.insts().GetAs<SemIR::FacetType>(canon_facet_type_inst_id);
const auto& orig_info =
context.facet_types().Get(orig_facet_type.facet_type_id);
const auto& orig_declared_facet_type = context.declared_facet_types().Get(
orig_facet_type.declared_facet_type_id);
auto replace_interface = [&](SemIR::SpecificInterface si) {
return SubstPeriodSelf(context, loc_id, si, period_self_replacement_id);
@@ -319,8 +319,8 @@ auto SubstPeriodSelfInFacetType(Context& context, SemIR::LocId loc_id,
return SubstPeriodSelf(context, loc_id, sc, period_self_replacement_id);
};
auto replace_type_impls_interface =
[&](SemIR::FacetTypeInfo::TypeImplsInterface impls)
-> SemIR::FacetTypeInfo::TypeImplsInterface {
[&](SemIR::DeclaredFacetType::TypeImplsInterface impls)
-> SemIR::DeclaredFacetType::TypeImplsInterface {
auto self = SubstPeriodSelf(context, loc_id,
context.constant_values().Get(impls.self_type),
period_self_replacement_id);
@@ -329,8 +329,8 @@ auto SubstPeriodSelfInFacetType(Context& context, SemIR::LocId loc_id,
return {context.constant_values().GetInstId(self), interface};
};
auto replace_type_impls_constraint =
[&](SemIR::FacetTypeInfo::TypeImplsNamedConstraint impls)
-> SemIR::FacetTypeInfo::TypeImplsNamedConstraint {
[&](SemIR::DeclaredFacetType::TypeImplsNamedConstraint impls)
-> SemIR::DeclaredFacetType::TypeImplsNamedConstraint {
auto self = SubstPeriodSelf(context, loc_id,
context.constant_values().Get(impls.self_type),
period_self_replacement_id);
@@ -339,8 +339,8 @@ auto SubstPeriodSelfInFacetType(Context& context, SemIR::LocId loc_id,
period_self_replacement_id);
return {context.constant_values().GetInstId(self), constraint};
};
auto replace_rewrite = [&](SemIR::FacetTypeInfo::RewriteConstraint r)
-> SemIR::FacetTypeInfo::RewriteConstraint {
auto replace_rewrite = [&](SemIR::DeclaredFacetType::RewriteConstraint r)
-> SemIR::DeclaredFacetType::RewriteConstraint {
// The LHS access instruction is not substituted so it keeps its `.Self`.
// This avoids evaluation replacing it with a concrete value from a final
// impl, as that would drop the association with the associated constant
@@ -351,31 +351,38 @@ auto SubstPeriodSelfInFacetType(Context& context, SemIR::LocId loc_id,
return {r.lhs_id, context.constant_values().GetInstId(rhs)};
};
SemIR::FacetTypeInfo info;
SemIR::DeclaredFacetType declared_facet_type;
llvm::append_range(
info.extend_constraints,
llvm::map_range(orig_info.extend_constraints, replace_interface));
declared_facet_type.extend_constraints,
llvm::map_range(orig_declared_facet_type.extend_constraints,
replace_interface));
llvm::append_range(
info.extend_named_constraints,
llvm::map_range(orig_info.extend_named_constraints, replace_constraint));
declared_facet_type.extend_named_constraints,
llvm::map_range(orig_declared_facet_type.extend_named_constraints,
replace_constraint));
llvm::append_range(
info.self_impls_constraints,
llvm::map_range(orig_info.self_impls_constraints, replace_interface));
llvm::append_range(info.self_impls_named_constraints,
llvm::map_range(orig_info.self_impls_named_constraints,
replace_constraint));
llvm::append_range(info.type_impls_interfaces,
llvm::map_range(orig_info.type_impls_interfaces,
replace_type_impls_interface));
llvm::append_range(info.type_impls_named_constraints,
llvm::map_range(orig_info.type_impls_named_constraints,
replace_type_impls_constraint));
declared_facet_type.self_impls_constraints,
llvm::map_range(orig_declared_facet_type.self_impls_constraints,
replace_interface));
llvm::append_range(
info.rewrite_constraints,
llvm::map_range(orig_info.rewrite_constraints, replace_rewrite));
declared_facet_type.self_impls_named_constraints,
llvm::map_range(orig_declared_facet_type.self_impls_named_constraints,
replace_constraint));
llvm::append_range(
declared_facet_type.type_impls_interfaces,
llvm::map_range(orig_declared_facet_type.type_impls_interfaces,
replace_type_impls_interface));
llvm::append_range(
declared_facet_type.type_impls_named_constraints,
llvm::map_range(orig_declared_facet_type.type_impls_named_constraints,
replace_type_impls_constraint));
llvm::append_range(
declared_facet_type.rewrite_constraints,
llvm::map_range(orig_declared_facet_type.rewrite_constraints,
replace_rewrite));
info.Canonicalize();
if (info == orig_info) {
declared_facet_type.Canonicalize();
if (declared_facet_type == orig_declared_facet_type) {
// Nothing was substituted, keep the original instruction.
//
// It is noteworthy that we keep the non-canonical instruction here, since
@@ -388,7 +395,8 @@ auto SubstPeriodSelfInFacetType(Context& context, SemIR::LocId loc_id,
return AddTypeInst<SemIR::FacetType>(
context, loc_id,
{.type_id = SemIR::TypeType::TypeId,
.facet_type_id = context.facet_types().Add(info)});
.declared_facet_type_id =
context.declared_facet_types().Add(declared_facet_type)});
}
auto IsPeriodSelf(Context& context, SemIR::InstId inst_id, bool canonicalize)
+51 -47
View File
@@ -137,30 +137,31 @@ static auto PushOperand(Context& context, Worklist& worklist,
push_specific(interface.specific_id);
break;
}
case CARBON_KIND(SemIR::FacetTypeId facet_type_id): {
const auto& facet_type_info = context.facet_types().Get(facet_type_id);
for (auto extends : facet_type_info.extend_constraints) {
case CARBON_KIND(SemIR::DeclaredFacetTypeId declared_facet_type_id): {
const auto& declared_facet_type =
context.declared_facet_types().Get(declared_facet_type_id);
for (auto extends : declared_facet_type.extend_constraints) {
push_specific(extends.specific_id);
}
for (auto impls : facet_type_info.self_impls_constraints) {
for (auto impls : declared_facet_type.self_impls_constraints) {
push_specific(impls.specific_id);
}
for (auto extends : facet_type_info.extend_named_constraints) {
for (auto extends : declared_facet_type.extend_named_constraints) {
push_specific(extends.specific_id);
}
for (auto impls : facet_type_info.self_impls_named_constraints) {
for (auto impls : declared_facet_type.self_impls_named_constraints) {
push_specific(impls.specific_id);
}
for (const auto& type_impls : facet_type_info.type_impls_interfaces) {
for (const auto& type_impls : declared_facet_type.type_impls_interfaces) {
worklist.Push(type_impls.self_type);
push_specific(type_impls.specific_interface.specific_id);
}
for (const auto& type_impls :
facet_type_info.type_impls_named_constraints) {
declared_facet_type.type_impls_named_constraints) {
worklist.Push(type_impls.self_type);
push_specific(type_impls.specific_named_constraint.specific_id);
}
for (auto rewrite : facet_type_info.rewrite_constraints) {
for (auto rewrite : declared_facet_type.rewrite_constraints) {
worklist.Push(rewrite.lhs_id);
worklist.Push(rewrite.rhs_id);
}
@@ -247,28 +248,28 @@ static auto PopOperand(Context& context, Worklist& worklist,
})
.index;
}
case CARBON_KIND(SemIR::FacetTypeId facet_type_id): {
const auto& old_facet_type_info =
context.facet_types().Get(facet_type_id);
SemIR::FacetTypeInfo new_facet_type_info = {
.other_requirements = old_facet_type_info.other_requirements};
case CARBON_KIND(SemIR::DeclaredFacetTypeId declared_facet_type_id): {
const auto& old_declared_facet_type =
context.declared_facet_types().Get(declared_facet_type_id);
SemIR::DeclaredFacetType new_declared_facet_type = {
.other_requirements = old_declared_facet_type.other_requirements};
// Since these were added to a stack, we get them back in reverse order.
new_facet_type_info.rewrite_constraints.resize(
old_facet_type_info.rewrite_constraints.size(),
SemIR::FacetTypeInfo::RewriteConstraint::None);
new_declared_facet_type.rewrite_constraints.resize(
old_declared_facet_type.rewrite_constraints.size(),
SemIR::DeclaredFacetType::RewriteConstraint::None);
for (auto& new_constraint :
llvm::reverse(new_facet_type_info.rewrite_constraints)) {
llvm::reverse(new_declared_facet_type.rewrite_constraints)) {
auto rhs_id = worklist.Pop();
auto lhs_id = worklist.Pop();
new_constraint = {.lhs_id = lhs_id, .rhs_id = rhs_id};
}
new_facet_type_info.type_impls_named_constraints.resize(
old_facet_type_info.type_impls_named_constraints.size(),
new_declared_facet_type.type_impls_named_constraints.resize(
old_declared_facet_type.type_impls_named_constraints.size(),
{SemIR::InstId::None, SemIR::SpecificNamedConstraint::None});
for (auto [old_type_impls, new_type_impls] :
llvm::reverse(llvm::zip_equal(
old_facet_type_info.type_impls_named_constraints,
new_facet_type_info.type_impls_named_constraints))) {
old_declared_facet_type.type_impls_named_constraints,
new_declared_facet_type.type_impls_named_constraints))) {
auto specific_id =
pop_specific(old_type_impls.specific_named_constraint.specific_id);
auto self_type = worklist.Pop();
@@ -278,12 +279,13 @@ static auto PopOperand(Context& context, Worklist& worklist,
old_type_impls.specific_named_constraint.named_constraint_id,
specific_id}};
}
new_facet_type_info.type_impls_interfaces.resize(
old_facet_type_info.type_impls_interfaces.size(),
new_declared_facet_type.type_impls_interfaces.resize(
old_declared_facet_type.type_impls_interfaces.size(),
{SemIR::InstId::None, SemIR::SpecificInterface::None});
for (auto [old_type_impls, new_type_impls] : llvm::reverse(
llvm::zip_equal(old_facet_type_info.type_impls_interfaces,
new_facet_type_info.type_impls_interfaces))) {
for (auto [old_type_impls, new_type_impls] :
llvm::reverse(llvm::zip_equal(
old_declared_facet_type.type_impls_interfaces,
new_declared_facet_type.type_impls_interfaces))) {
auto specific_id =
pop_specific(old_type_impls.specific_interface.specific_id);
auto self_type = worklist.Pop();
@@ -292,49 +294,51 @@ static auto PopOperand(Context& context, Worklist& worklist,
.specific_interface = {
old_type_impls.specific_interface.interface_id, specific_id}};
}
new_facet_type_info.self_impls_named_constraints.resize(
old_facet_type_info.self_impls_named_constraints.size(),
new_declared_facet_type.self_impls_named_constraints.resize(
old_declared_facet_type.self_impls_named_constraints.size(),
SemIR::SpecificNamedConstraint::None);
for (auto [old_constraint, new_constraint] :
llvm::reverse(llvm::zip_equal(
old_facet_type_info.self_impls_named_constraints,
new_facet_type_info.self_impls_named_constraints))) {
old_declared_facet_type.self_impls_named_constraints,
new_declared_facet_type.self_impls_named_constraints))) {
new_constraint = {
.named_constraint_id = old_constraint.named_constraint_id,
.specific_id = pop_specific(old_constraint.specific_id)};
}
new_facet_type_info.extend_named_constraints.resize(
old_facet_type_info.extend_named_constraints.size(),
new_declared_facet_type.extend_named_constraints.resize(
old_declared_facet_type.extend_named_constraints.size(),
SemIR::SpecificNamedConstraint::None);
for (auto [old_constraint, new_constraint] : llvm::reverse(
llvm::zip_equal(old_facet_type_info.extend_named_constraints,
new_facet_type_info.extend_named_constraints))) {
for (auto [old_constraint, new_constraint] :
llvm::reverse(llvm::zip_equal(
old_declared_facet_type.extend_named_constraints,
new_declared_facet_type.extend_named_constraints))) {
new_constraint = {
.named_constraint_id = old_constraint.named_constraint_id,
.specific_id = pop_specific(old_constraint.specific_id)};
}
new_facet_type_info.self_impls_constraints.resize(
old_facet_type_info.self_impls_constraints.size(),
new_declared_facet_type.self_impls_constraints.resize(
old_declared_facet_type.self_impls_constraints.size(),
SemIR::SpecificInterface::None);
for (auto [old_constraint, new_constraint] : llvm::reverse(
llvm::zip_equal(old_facet_type_info.self_impls_constraints,
new_facet_type_info.self_impls_constraints))) {
for (auto [old_constraint, new_constraint] :
llvm::reverse(llvm::zip_equal(
old_declared_facet_type.self_impls_constraints,
new_declared_facet_type.self_impls_constraints))) {
new_constraint = {
.interface_id = old_constraint.interface_id,
.specific_id = pop_specific(old_constraint.specific_id)};
}
new_facet_type_info.extend_constraints.resize(
old_facet_type_info.extend_constraints.size(),
new_declared_facet_type.extend_constraints.resize(
old_declared_facet_type.extend_constraints.size(),
SemIR::SpecificInterface::None);
for (auto [old_constraint, new_constraint] : llvm::reverse(
llvm::zip_equal(old_facet_type_info.extend_constraints,
new_facet_type_info.extend_constraints))) {
llvm::zip_equal(old_declared_facet_type.extend_constraints,
new_declared_facet_type.extend_constraints))) {
new_constraint = {
.interface_id = old_constraint.interface_id,
.specific_id = pop_specific(old_constraint.specific_id)};
}
new_facet_type_info.Canonicalize();
return context.facet_types().Add(new_facet_type_info).index;
new_declared_facet_type.Canonicalize();
return context.declared_facet_types().Add(new_declared_facet_type).index;
}
default:
return arg.value();
+1 -1
View File
@@ -60,7 +60,7 @@
// CHECK:STDOUT: object_layout:
// CHECK:STDOUT: size: 0
// CHECK:STDOUT: alignment: 1
// CHECK:STDOUT: facet_types: {}
// CHECK:STDOUT: declared_facet_types: {}
// CHECK:STDOUT: insts:
// CHECK:STDOUT: 'inst(TypeType)': {kind: TypeType, type: type(TypeType)}
// CHECK:STDOUT: 'inst(AutoType)': {kind: AutoType, type: type(TypeType)}
+3 -3
View File
@@ -86,13 +86,13 @@ fn F(generic Form: Core.Form) ->? Form;
// CHECK:STDOUT: object_layout:
// CHECK:STDOUT: size: 0
// CHECK:STDOUT: alignment: 1
// CHECK:STDOUT: facet_types:
// CHECK:STDOUT: facet_type70000000: {}
// CHECK:STDOUT: declared_facet_types:
// CHECK:STDOUT: declared_facet_type70000000: {}
// CHECK:STDOUT: insts:
// CHECK:STDOUT: instF: {kind: Namespace, arg0: name_scope0, type: type(inst(NamespaceType))}
// CHECK:STDOUT: inst70000010: {kind: ImportDecl, arg0: name(Core)}
// CHECK:STDOUT: inst70000011: {kind: Namespace, arg0: name_scope70000001, type: type(inst(NamespaceType))}
// CHECK:STDOUT: inst70000012: {kind: FacetType, arg0: facet_type70000000, type: type(TypeType)}
// CHECK:STDOUT: inst70000012: {kind: FacetType, arg0: declared_facet_type70000000, type: type(TypeType)}
// CHECK:STDOUT: inst70000013: {kind: SymbolicBinding, arg0: entity_name70000000, arg1: inst<none>, type: type(inst70000012)}
// CHECK:STDOUT: inst70000014: {kind: SymbolicBinding, arg0: entity_name70000000, arg1: inst<none>, type: type(inst70000012)}
// CHECK:STDOUT: inst70000015: {kind: NameRef, arg0: name(Core), arg1: inst70000011, type: type(inst(NamespaceType))}
@@ -180,7 +180,7 @@ fn G(x: Cpp.X) {
// CHECK:STDOUT: object_layout:
// CHECK:STDOUT: size: 0
// CHECK:STDOUT: alignment: 1
// CHECK:STDOUT: facet_types: {}
// CHECK:STDOUT: declared_facet_types: {}
// CHECK:STDOUT: insts:
// CHECK:STDOUT: 'inst(TypeType)': {kind: TypeType, type: type(TypeType)}
// CHECK:STDOUT: 'inst(AutoType)': {kind: AutoType, type: type(TypeType)}
@@ -88,7 +88,7 @@ fn B() {
// CHECK:STDOUT: object_layout:
// CHECK:STDOUT: size: 0
// CHECK:STDOUT: alignment: 1
// CHECK:STDOUT: facet_types: {}
// CHECK:STDOUT: declared_facet_types: {}
// CHECK:STDOUT: insts:
// CHECK:STDOUT: instF: {kind: Namespace, arg0: name_scope0, type: type(inst(NamespaceType))}
// CHECK:STDOUT: inst50000010: {kind: FunctionDecl, arg0: function50000000, arg1: inst_block_empty, type: type(inst50000011)}
@@ -195,7 +195,7 @@ fn B() {
// CHECK:STDOUT: object_layout:
// CHECK:STDOUT: size: 0
// CHECK:STDOUT: alignment: 1
// CHECK:STDOUT: facet_types: {}
// CHECK:STDOUT: declared_facet_types: {}
// CHECK:STDOUT: insts:
// CHECK:STDOUT: instF: {kind: Namespace, arg0: name_scope0, type: type(inst(NamespaceType))}
// CHECK:STDOUT: inst70000010: {kind: ImportDecl, arg0: name1}
@@ -88,7 +88,7 @@ fn B() {
// CHECK:STDOUT: object_layout:
// CHECK:STDOUT: size: 0
// CHECK:STDOUT: alignment: 1
// CHECK:STDOUT: facet_types: {}
// CHECK:STDOUT: declared_facet_types: {}
// CHECK:STDOUT: insts:
// CHECK:STDOUT: instF: {kind: Namespace, arg0: name_scope0, type: type(inst(NamespaceType))}
// CHECK:STDOUT: inst50000010: {kind: FunctionDecl, arg0: function50000000, arg1: inst_block_empty, type: type(inst50000011)}
@@ -214,7 +214,7 @@ fn B() {
// CHECK:STDOUT: object_layout:
// CHECK:STDOUT: size: 0
// CHECK:STDOUT: alignment: 1
// CHECK:STDOUT: facet_types: {}
// CHECK:STDOUT: declared_facet_types: {}
// CHECK:STDOUT: insts:
// CHECK:STDOUT: instF: {kind: Namespace, arg0: name_scope0, type: type(inst(NamespaceType))}
// CHECK:STDOUT: inst70000010: {kind: ImportDecl, arg0: name1}
@@ -95,12 +95,12 @@ fn UseLocalCopy[T: Copy](_: T.T1, _: T.T2) {}
// CHECK:STDOUT: object_layout:
// CHECK:STDOUT: size: 0
// CHECK:STDOUT: alignment: 1
// CHECK:STDOUT: facet_types:
// CHECK:STDOUT: facet_type60000000: {extends interface: interface60000000}
// CHECK:STDOUT: declared_facet_types:
// CHECK:STDOUT: declared_facet_type60000000: {extends interface: interface60000000}
// CHECK:STDOUT: insts:
// CHECK:STDOUT: instF: {kind: Namespace, arg0: name_scope0, type: type(inst(NamespaceType))}
// CHECK:STDOUT: inst60000010: {kind: InterfaceDecl, arg0: interface60000000, arg1: inst_block_empty, type: type(TypeType)}
// CHECK:STDOUT: inst60000011: {kind: FacetType, arg0: facet_type60000000, type: type(TypeType)}
// CHECK:STDOUT: inst60000011: {kind: FacetType, arg0: declared_facet_type60000000, type: type(TypeType)}
// CHECK:STDOUT: inst60000012: {kind: SymbolicBinding, arg0: entity_name60000000, arg1: inst<none>, type: type(inst60000011)}
// CHECK:STDOUT: inst60000013: {kind: SymbolicBinding, arg0: entity_name60000000, arg1: inst<none>, type: type(inst60000011)}
// CHECK:STDOUT: inst60000014: {kind: SymbolicBinding, arg0: entity_name60000000, arg1: inst<none>, type: type(inst60000011)}
@@ -339,16 +339,16 @@ fn UseLocalCopy[T: Copy](_: T.T1, _: T.T2) {}
// CHECK:STDOUT: object_layout:
// CHECK:STDOUT: size: 0
// CHECK:STDOUT: alignment: 0
// CHECK:STDOUT: facet_types:
// CHECK:STDOUT: facet_type50000000: {extends interface: interface50000000}
// CHECK:STDOUT: facet_type50000001: {}
// CHECK:STDOUT: facet_type50000002: {extends interface: interface50000001}
// CHECK:STDOUT: declared_facet_types:
// CHECK:STDOUT: declared_facet_type50000000: {extends interface: interface50000000}
// CHECK:STDOUT: declared_facet_type50000001: {}
// CHECK:STDOUT: declared_facet_type50000002: {extends interface: interface50000001}
// CHECK:STDOUT: insts:
// CHECK:STDOUT: instF: {kind: Namespace, arg0: name_scope0, type: type(inst(NamespaceType))}
// CHECK:STDOUT: inst50000010: {kind: ImportDecl, arg0: name0}
// CHECK:STDOUT: inst50000011: {kind: Namespace, arg0: name_scope50000001, type: type(inst(NamespaceType))}
// CHECK:STDOUT: inst50000012: {kind: InterfaceDecl, arg0: interface50000000, arg1: inst_block_empty, type: type(TypeType)}
// CHECK:STDOUT: inst50000013: {kind: FacetType, arg0: facet_type50000000, type: type(TypeType)}
// CHECK:STDOUT: inst50000013: {kind: FacetType, arg0: declared_facet_type50000000, type: type(TypeType)}
// CHECK:STDOUT: inst50000014: {kind: SymbolicBinding, arg0: entity_name50000000, arg1: inst<none>, type: type(inst50000013)}
// CHECK:STDOUT: inst50000015: {kind: SymbolicBinding, arg0: entity_name50000000, arg1: inst<none>, type: type(inst50000013)}
// CHECK:STDOUT: inst50000016: {kind: SymbolicBinding, arg0: entity_name50000000, arg1: inst<none>, type: type(inst50000013)}
@@ -362,13 +362,13 @@ fn UseLocalCopy[T: Copy](_: T.T1, _: T.T2) {}
// CHECK:STDOUT: inst5000001E: {kind: AssociatedConstantDecl, arg0: assoc_const50000001, arg1: inst_block5000000C, type: type(TypeType)}
// CHECK:STDOUT: inst5000001F: {kind: AssociatedEntity, arg0: element1, arg1: inst5000001E, type: type(inst5000001A)}
// CHECK:STDOUT: inst50000020: {kind: AssociatedEntity, arg0: element1, arg1: inst5000001E, type: type(inst5000001A)}
// CHECK:STDOUT: inst50000021: {kind: FacetType, arg0: facet_type50000001, type: type(TypeType)}
// CHECK:STDOUT: inst50000021: {kind: FacetType, arg0: declared_facet_type50000001, type: type(TypeType)}
// CHECK:STDOUT: inst50000022: {kind: SymbolicBinding, arg0: entity_name50000001, arg1: inst<none>, type: type(inst50000021)}
// CHECK:STDOUT: inst50000023: {kind: SymbolicBinding, arg0: entity_name50000001, arg1: inst<none>, type: type(inst50000021)}
// CHECK:STDOUT: inst50000024: {kind: NameRef, arg0: name0, arg1: inst50000011, type: type(inst(NamespaceType))}
// CHECK:STDOUT: inst50000025: {kind: ImportRefLoaded, arg0: import_ir_inst0, arg1: entity_name50000002, type: type(TypeType)}
// CHECK:STDOUT: inst50000026: {kind: InterfaceDecl, arg0: interface50000001, arg1: inst_block_empty, type: type(TypeType)}
// CHECK:STDOUT: inst50000027: {kind: FacetType, arg0: facet_type50000002, type: type(TypeType)}
// CHECK:STDOUT: inst50000027: {kind: FacetType, arg0: declared_facet_type50000002, type: type(TypeType)}
// CHECK:STDOUT: inst50000028: {kind: InterfaceWithSelfDecl, arg0: interface50000001}
// CHECK:STDOUT: inst50000029: {kind: SymbolicBinding, arg0: entity_name50000003, arg1: inst<none>, type: type(inst50000027)}
// CHECK:STDOUT: inst5000002A: {kind: ImportRefLoaded, arg0: import_ir_inst3, arg1: entity_name<none>, type: type(inst5000003D)}
+5 -5
View File
@@ -595,14 +595,14 @@ fn Foo[T: type](p: T*) -> (T*, ()) {
// CHECK:STDOUT: object_layout:
// CHECK:STDOUT: size: 0
// CHECK:STDOUT: alignment: 1
// CHECK:STDOUT: facet_types:
// CHECK:STDOUT: facet_type78000000: {}
// CHECK:STDOUT: facet_type78000001: {extends interface: interface78000000}
// CHECK:STDOUT: declared_facet_types:
// CHECK:STDOUT: declared_facet_type78000000: {}
// CHECK:STDOUT: declared_facet_type78000001: {extends interface: interface78000000}
// CHECK:STDOUT: insts:
// CHECK:STDOUT: instF: {kind: Namespace, arg0: name_scope0, type: type(inst(NamespaceType))}
// CHECK:STDOUT: inst78000010: {kind: ImportDecl, arg0: name(Core)}
// CHECK:STDOUT: inst78000011: {kind: Namespace, arg0: name_scope78000001, type: type(inst(NamespaceType))}
// CHECK:STDOUT: inst78000012: {kind: FacetType, arg0: facet_type78000000, type: type(TypeType)}
// CHECK:STDOUT: inst78000012: {kind: FacetType, arg0: declared_facet_type78000000, type: type(TypeType)}
// CHECK:STDOUT: inst78000013: {kind: SymbolicBinding, arg0: entity_name78000000, arg1: inst<none>, type: type(inst78000012)}
// CHECK:STDOUT: inst78000014: {kind: SymbolicBinding, arg0: entity_name78000000, arg1: inst<none>, type: type(inst78000012)}
// CHECK:STDOUT: inst78000015: {kind: TypeLiteral, arg0: inst(TypeType), type: type(TypeType)}
@@ -674,7 +674,7 @@ fn Foo[T: type](p: T*) -> (T*, ()) {
// CHECK:STDOUT: inst78000057: {kind: RequireCompleteType, arg0: inst7800001F, type: type(inst(WitnessType))}
// CHECK:STDOUT: inst78000058: {kind: ImportRefLoaded, arg0: import_ir_inst0, arg1: entity_name78000003, type: type(TypeType)}
// CHECK:STDOUT: inst78000059: {kind: InterfaceDecl, arg0: interface78000000, arg1: inst_block_empty, type: type(TypeType)}
// CHECK:STDOUT: inst7800005A: {kind: FacetType, arg0: facet_type78000001, type: type(TypeType)}
// CHECK:STDOUT: inst7800005A: {kind: FacetType, arg0: declared_facet_type78000001, type: type(TypeType)}
// CHECK:STDOUT: inst7800005B: {kind: InterfaceWithSelfDecl, arg0: interface78000000}
// CHECK:STDOUT: inst7800005C: {kind: SymbolicBinding, arg0: entity_name78000004, arg1: inst<none>, type: type(inst7800005A)}
// CHECK:STDOUT: inst7800005D: {kind: ImportRefLoaded, arg0: import_ir_inst3, arg1: entity_name<none>, type: type(inst78000084)}
@@ -90,7 +90,7 @@ fn Foo(n: ()) -> ((), ()) {
// CHECK:STDOUT: object_layout:
// CHECK:STDOUT: size: 0
// CHECK:STDOUT: alignment: 1
// CHECK:STDOUT: facet_types: {}
// CHECK:STDOUT: declared_facet_types: {}
// CHECK:STDOUT: insts:
// CHECK:STDOUT: instF: {kind: Namespace, arg0: name_scope0, type: type(inst(NamespaceType))}
// CHECK:STDOUT: inst50000010: {kind: TupleType, arg0: inst_block_empty, type: type(TypeType)}
+8 -7
View File
@@ -8,7 +8,7 @@
#include "toolchain/check/facet_type.h"
#include "toolchain/check/inst.h"
#include "toolchain/check/type_completion.h"
#include "toolchain/sem_ir/facet_type_info.h"
#include "toolchain/sem_ir/declared_facet_type.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/typed_insts.h"
@@ -227,8 +227,8 @@ auto GetGenericNamedConstraintType(Context& context,
auto GetInterfaceType(Context& context, SemIR::InterfaceId interface_id,
SemIR::SpecificId specific_id) -> SemIR::TypeId {
return GetTypeImpl<SemIR::FacetType>(
context,
FacetTypeFromInterface(context, interface_id, specific_id).facet_type_id);
context, FacetTypeFromInterface(context, interface_id, specific_id)
.declared_facet_type_id);
}
auto GetNamedConstraintType(Context& context,
@@ -237,13 +237,14 @@ auto GetNamedConstraintType(Context& context,
return GetTypeImpl<SemIR::FacetType>(
context,
FacetTypeFromNamedConstraint(context, named_constraint_id, specific_id)
.facet_type_id);
.declared_facet_type_id);
}
auto GetFacetType(Context& context, const SemIR::FacetTypeInfo& info)
auto GetFacetType(Context& context,
const SemIR::DeclaredFacetType& declared_facet_type)
-> SemIR::TypeId {
return GetTypeImpl<SemIR::FacetType>(context,
context.facet_types().Add(info));
return GetTypeImpl<SemIR::FacetType>(
context, context.declared_facet_types().Add(declared_facet_type));
}
auto GetFacetAccessType(Context& context, SemIR::InstId facet_value_inst_id)
+3 -2
View File
@@ -100,8 +100,9 @@ auto GetNamedConstraintType(Context& context,
SemIR::NamedConstraintId named_constraint_id,
SemIR::SpecificId specific_id) -> SemIR::TypeId;
// Gets the facet type for the given `info`.
auto GetFacetType(Context& context, const SemIR::FacetTypeInfo& info)
// Gets the facet type for the given `declared_facet_type`.
auto GetFacetType(Context& context,
const SemIR::DeclaredFacetType& declared_facet_type)
-> SemIR::TypeId;
// Gets the type contained within the given facet value.
+41 -38
View File
@@ -18,7 +18,7 @@
#include "toolchain/diagnostics/emitter.h"
#include "toolchain/diagnostics/format_providers.h"
#include "toolchain/sem_ir/constant.h"
#include "toolchain/sem_ir/facet_type_info.h"
#include "toolchain/sem_ir/declared_facet_type.h"
#include "toolchain/sem_ir/generic.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/named_constraint.h"
@@ -116,10 +116,10 @@ static auto SpecificHasError(Context& context, SemIR::SpecificId specific_id)
static auto RequireCompleteFacetType(Context& context, SemIR::LocId loc_id,
const SemIR::FacetType& facet_type,
bool diagnose) -> bool {
const auto& facet_type_info =
context.facet_types().Get(facet_type.facet_type_id);
const auto& declared_facet_type =
context.declared_facet_types().Get(facet_type.declared_facet_type_id);
for (auto extends : facet_type_info.extend_constraints) {
for (auto extends : declared_facet_type.extend_constraints) {
auto interface_id = extends.interface_id;
const auto& interface = context.interfaces().Get(interface_id);
if (!interface.is_complete()) {
@@ -147,7 +147,7 @@ static auto RequireCompleteFacetType(Context& context, SemIR::LocId loc_id,
}
}
for (auto extends : facet_type_info.extend_named_constraints) {
for (auto extends : declared_facet_type.extend_named_constraints) {
auto named_constraint_id = extends.named_constraint_id;
const auto& constraint =
context.named_constraints().Get(named_constraint_id);
@@ -967,8 +967,9 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id,
SemIR::TypeInstId facet_type_inst_id,
bool allow_partially_identified, bool diagnose)
-> SemIR::IdentifiedFacetTypeId {
auto facet_type_id =
context.insts().GetAs<SemIR::FacetType>(facet_type_inst_id).facet_type_id;
auto declared_facet_type_id = context.insts()
.GetAs<SemIR::FacetType>(facet_type_inst_id)
.declared_facet_type_id;
// While partially identified facet types end up in the store of
// IdentifiedFacetTypes, we don't try to construct a key to look for them
@@ -978,7 +979,8 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id,
// work of identifying the facet type (though we could skip the mapping of
// constant values into specifics).
auto key = SemIR::IdentifiedFacetTypeKey{
.facet_type_id = facet_type_id, .self_const_id = initial_self_const_id};
.declared_facet_type_id = declared_facet_type_id,
.self_const_id = initial_self_const_id};
if (auto identified_id = context.identified_facet_types().Lookup(key);
identified_id.has_value()) {
return identified_id;
@@ -989,8 +991,9 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id,
context, loc_id,
context.constant_values().GetInstId(initial_self_const_id),
context.types().GetAsTypeInstId(facet_type_inst_id));
facet_type_id =
context.insts().GetAs<SemIR::FacetType>(subst_id).facet_type_id;
declared_facet_type_id = context.insts()
.GetAs<SemIR::FacetType>(subst_id)
.declared_facet_type_id;
}
struct SelfImplsFacetType {
@@ -998,12 +1001,12 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id,
// the resulting IdentifiedFacetType.
bool extend;
SemIR::ConstantId self;
SemIR::FacetTypeId facet_type;
SemIR::DeclaredFacetTypeId declared_facet_type;
};
// Work queue.
llvm::SmallVector<SelfImplsFacetType> work = {
{true, initial_self_const_id, facet_type_id}};
{true, initial_self_const_id, declared_facet_type_id}};
// Outputs for the IdentifiedFacetType.
bool partially_identified = false;
@@ -1014,15 +1017,15 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id,
SelfImplsFacetType next_impls = work.pop_back_val();
bool facet_type_extends = next_impls.extend;
auto self_const_id = GetCanonicalFacetOrTypeValue(context, next_impls.self);
const auto& facet_type_info =
context.facet_types().Get(next_impls.facet_type);
const auto& declared_facet_type =
context.declared_facet_types().Get(next_impls.declared_facet_type);
auto self_and_interface = [&](SemIR::SpecificInterface impls_interface)
-> SemIR::IdentifiedFacetType::RequiredImpl {
return {self_const_id, impls_interface};
};
auto type_and_interface =
[&](SemIR::FacetTypeInfo::TypeImplsInterface impls)
[&](SemIR::DeclaredFacetType::TypeImplsInterface impls)
-> SemIR::IdentifiedFacetType::RequiredImpl {
return {context.constant_values().Get(impls.self_type),
impls.specific_interface};
@@ -1030,23 +1033,23 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id,
if (facet_type_extends) {
llvm::append_range(extends,
llvm::map_range(facet_type_info.extend_constraints,
llvm::map_range(declared_facet_type.extend_constraints,
self_and_interface));
} else {
llvm::append_range(impls,
llvm::map_range(facet_type_info.extend_constraints,
llvm::map_range(declared_facet_type.extend_constraints,
self_and_interface));
}
llvm::append_range(impls,
llvm::map_range(facet_type_info.self_impls_constraints,
self_and_interface));
llvm::append_range(impls,
llvm::map_range(facet_type_info.type_impls_interfaces,
type_and_interface));
llvm::append_range(
impls, llvm::map_range(declared_facet_type.self_impls_constraints,
self_and_interface));
llvm::append_range(
impls, llvm::map_range(declared_facet_type.type_impls_interfaces,
type_and_interface));
if (facet_type_info.extend_named_constraints.empty() &&
facet_type_info.self_impls_named_constraints.empty() &&
facet_type_info.type_impls_named_constraints.empty()) {
if (declared_facet_type.extend_named_constraints.empty() &&
declared_facet_type.self_impls_named_constraints.empty() &&
declared_facet_type.type_impls_named_constraints.empty()) {
continue;
}
@@ -1055,7 +1058,7 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id,
// specific.
auto self_facet = GetSelfFacetValue(context, self_const_id);
for (auto extends : facet_type_info.extend_named_constraints) {
for (auto extends : declared_facet_type.extend_named_constraints) {
const auto& constraint =
context.named_constraints().Get(extends.named_constraint_id);
@@ -1104,16 +1107,16 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id,
return SemIR::IdentifiedFacetTypeId::None;
}
auto facet_type_id =
auto declared_facet_type_id =
context.constant_values()
.GetInstAs<SemIR::FacetType>(require_facet_type)
.facet_type_id;
.declared_facet_type_id;
bool extend = facet_type_extends && require.extend_self;
work.push_back({extend, require_self, facet_type_id});
work.push_back({extend, require_self, declared_facet_type_id});
}
}
for (auto impls : facet_type_info.self_impls_named_constraints) {
for (auto impls : declared_facet_type.self_impls_named_constraints) {
const auto& constraint =
context.named_constraints().Get(impls.named_constraint_id);
@@ -1161,16 +1164,16 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id,
return SemIR::IdentifiedFacetTypeId::None;
}
auto facet_type_id =
auto declared_facet_type_id =
context.constant_values()
.GetInstAs<SemIR::FacetType>(require_facet_type)
.facet_type_id;
work.push_back({false, require_self, facet_type_id});
.declared_facet_type_id;
work.push_back({false, require_self, declared_facet_type_id});
}
}
for (const auto& type_impls :
facet_type_info.type_impls_named_constraints) {
declared_facet_type.type_impls_named_constraints) {
auto [self_type_inst_id, impls] = type_impls;
const auto& constraint =
context.named_constraints().Get(impls.named_constraint_id);
@@ -1222,11 +1225,11 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id,
return SemIR::IdentifiedFacetTypeId::None;
}
auto facet_type_id =
auto declared_facet_type_id =
context.constant_values()
.GetInstAs<SemIR::FacetType>(require_facet_type)
.facet_type_id;
work.push_back({false, require_self, facet_type_id});
.declared_facet_type_id;
work.push_back({false, require_self, declared_facet_type_id});
}
}
}
+1 -1
View File
@@ -72,7 +72,7 @@
// CHECK:STDOUT: object_layout:
// CHECK:STDOUT: size: 0
// CHECK:STDOUT: alignment: 1
// CHECK:STDOUT: facet_types: {}
// CHECK:STDOUT: declared_facet_types: {}
// CHECK:STDOUT: insts:
// CHECK:STDOUT: instF: {kind: Namespace, arg0: name_scope0, type: type(inst(NamespaceType))}
// CHECK:STDOUT: bundles: {}
+2 -2
View File
@@ -92,8 +92,8 @@ cc_library(
"cpp_file.cpp",
"cpp_initializer_list.cpp",
"cpp_overload_set.cpp",
"declared_facet_type.cpp",
"entity_name.cpp",
"facet_type_info.cpp",
"field.cpp",
"file.cpp",
"function.cpp",
@@ -125,9 +125,9 @@ cc_library(
"core_interface.h",
"cpp_initializer_list.h",
"cpp_overload_set.h",
"declared_facet_type.h",
"entity_name.h",
"entity_with_params_base.h",
"facet_type_info.h",
"field.h",
"file.h",
"function.h",
@@ -2,7 +2,7 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/sem_ir/facet_type_info.h"
#include "toolchain/sem_ir/declared_facet_type.h"
#include <tuple>
@@ -34,8 +34,9 @@ static auto InterfaceLess(const SpecificInterface& lhs,
}
// Canonically ordered by the numerical ids.
static auto RewriteLess(const FacetTypeInfo::RewriteConstraint& lhs,
const FacetTypeInfo::RewriteConstraint& rhs) -> bool {
static auto RewriteLess(const DeclaredFacetType::RewriteConstraint& lhs,
const DeclaredFacetType::RewriteConstraint& rhs)
-> bool {
return std::tie(lhs.lhs_id.index, lhs.rhs_id.index) <
std::tie(rhs.lhs_id.index, rhs.rhs_id.index);
}
@@ -48,9 +49,9 @@ static auto NamedConstraintLess(const SpecificNamedConstraint& lhs,
}
// Canonically ordered by the numerical ids.
static auto TypeImplsInterfaceLess(const FacetTypeInfo::TypeImplsInterface& lhs,
const FacetTypeInfo::TypeImplsInterface& rhs)
-> bool {
static auto TypeImplsInterfaceLess(
const DeclaredFacetType::TypeImplsInterface& lhs,
const DeclaredFacetType::TypeImplsInterface& rhs) -> bool {
return std::tie(lhs.self_type.index,
lhs.specific_interface.interface_id.index,
lhs.specific_interface.specific_id.index) <
@@ -61,8 +62,8 @@ static auto TypeImplsInterfaceLess(const FacetTypeInfo::TypeImplsInterface& lhs,
// Canonically ordered by the numerical ids.
static auto TypeImplsNamedConstraintLess(
const FacetTypeInfo::TypeImplsNamedConstraint& lhs,
const FacetTypeInfo::TypeImplsNamedConstraint& rhs) -> bool {
const DeclaredFacetType::TypeImplsNamedConstraint& lhs,
const DeclaredFacetType::TypeImplsNamedConstraint& rhs) -> bool {
return std::tie(lhs.self_type.index,
lhs.specific_named_constraint.named_constraint_id.index,
lhs.specific_named_constraint.specific_id.index) <
@@ -145,35 +146,39 @@ static auto CombineVectors(VecT& vec, const VecT& lhs, const VecT& rhs) {
llvm::concat<const typename VecT::value_type>(lhs, rhs));
}
auto FacetTypeInfo::Combine(const FacetTypeInfo& lhs, const FacetTypeInfo& rhs)
-> FacetTypeInfo {
FacetTypeInfo info;
CombineVectors(info.extend_constraints, lhs.extend_constraints,
auto DeclaredFacetType::Combine(const DeclaredFacetType& lhs,
const DeclaredFacetType& rhs)
-> DeclaredFacetType {
DeclaredFacetType declared_facet_type;
CombineVectors(declared_facet_type.extend_constraints, lhs.extend_constraints,
rhs.extend_constraints);
CombineVectors(info.self_impls_constraints, lhs.self_impls_constraints,
rhs.self_impls_constraints);
CombineVectors(info.extend_named_constraints, lhs.extend_named_constraints,
rhs.extend_named_constraints);
CombineVectors(info.self_impls_named_constraints,
CombineVectors(declared_facet_type.self_impls_constraints,
lhs.self_impls_constraints, rhs.self_impls_constraints);
CombineVectors(declared_facet_type.extend_named_constraints,
lhs.extend_named_constraints, rhs.extend_named_constraints);
CombineVectors(declared_facet_type.self_impls_named_constraints,
lhs.self_impls_named_constraints,
rhs.self_impls_named_constraints);
CombineVectors(info.type_impls_interfaces, lhs.type_impls_interfaces,
rhs.type_impls_interfaces);
CombineVectors(info.type_impls_named_constraints,
CombineVectors(declared_facet_type.type_impls_interfaces,
lhs.type_impls_interfaces, rhs.type_impls_interfaces);
CombineVectors(declared_facet_type.type_impls_named_constraints,
lhs.type_impls_named_constraints,
rhs.type_impls_named_constraints);
CombineVectors(info.rewrite_constraints, lhs.rewrite_constraints,
rhs.rewrite_constraints);
info.other_requirements = lhs.other_requirements || rhs.other_requirements;
return info;
CombineVectors(declared_facet_type.rewrite_constraints,
lhs.rewrite_constraints, rhs.rewrite_constraints);
declared_facet_type.other_requirements =
lhs.other_requirements || rhs.other_requirements;
return declared_facet_type;
}
auto FacetTypeInfo::ExtendedOnly(const FacetTypeInfo& info) -> FacetTypeInfo {
return {.extend_constraints = info.extend_constraints,
.extend_named_constraints = info.extend_named_constraints};
auto DeclaredFacetType::ExtendedOnly(
const DeclaredFacetType& declared_facet_type) -> DeclaredFacetType {
return {
.extend_constraints = declared_facet_type.extend_constraints,
.extend_named_constraints = declared_facet_type.extend_named_constraints};
}
auto FacetTypeInfo::TryAsSingleExtend() const
auto DeclaredFacetType::TryAsSingleExtend() const
-> std::optional<SingleExtendFacetType> {
if (!IsExtendedOnly()) {
return std::nullopt;
@@ -187,12 +192,12 @@ auto FacetTypeInfo::TryAsSingleExtend() const
return std::nullopt;
}
auto FacetTypeInfo::HasNoConstraints() const -> bool {
auto DeclaredFacetType::HasNoConstraints() const -> bool {
return extend_constraints.empty() && extend_named_constraints.empty() &&
IsExtendedOnly();
}
auto FacetTypeInfo::IsExtendedOnly() const -> bool {
auto DeclaredFacetType::IsExtendedOnly() const -> bool {
return self_impls_constraints.empty() &&
self_impls_named_constraints.empty() &&
type_impls_interfaces.empty() &&
@@ -200,7 +205,7 @@ auto FacetTypeInfo::IsExtendedOnly() const -> bool {
!other_requirements;
}
auto FacetTypeInfo::Canonicalize() -> void {
auto DeclaredFacetType::Canonicalize() -> void {
SortAndDeduplicate(extend_constraints, InterfaceLess);
SortAndDeduplicate(self_impls_constraints, InterfaceLess);
SubtractSorted(self_impls_constraints, extend_constraints, InterfaceLess);
@@ -214,7 +219,7 @@ auto FacetTypeInfo::Canonicalize() -> void {
SortAndDeduplicate(rewrite_constraints, RewriteLess);
}
auto FacetTypeInfo::Print(llvm::raw_ostream& out) const -> void {
auto DeclaredFacetType::Print(llvm::raw_ostream& out) const -> void {
out << "{";
llvm::ListSeparator outer_sep("; ");
@@ -392,12 +397,13 @@ auto AddCanonicalWitnessesBlock(File& sem_ir,
} // namespace Carbon::SemIR
namespace Carbon {
template class CanonicalValueStore<SemIR::FacetTypeId, SemIR::FacetTypeInfo,
template class CanonicalValueStore<SemIR::DeclaredFacetTypeId,
SemIR::DeclaredFacetType,
Tag<SemIR::CheckIRId>>;
template class CanonicalValueStore<
SemIR::IdentifiedFacetTypeId, SemIR::IdentifiedFacetTypeKey,
Tag<SemIR::CheckIRId>, SemIR::IdentifiedFacetType>;
template class ValueStore<SemIR::FacetTypeId, SemIR::FacetTypeInfo,
template class ValueStore<SemIR::DeclaredFacetTypeId, SemIR::DeclaredFacetType,
Tag<SemIR::CheckIRId>>;
template class ValueStore<SemIR::IdentifiedFacetTypeId,
SemIR::IdentifiedFacetType, Tag<SemIR::CheckIRId>>;
@@ -2,8 +2,8 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_TOOLCHAIN_SEM_IR_FACET_TYPE_INFO_H_
#define CARBON_TOOLCHAIN_SEM_IR_FACET_TYPE_INFO_H_
#ifndef CARBON_TOOLCHAIN_SEM_IR_DECLARED_FACET_TYPE_H_
#define CARBON_TOOLCHAIN_SEM_IR_DECLARED_FACET_TYPE_H_
#include "common/enum_mask_base.h"
#include "common/hashing.h"
@@ -34,18 +34,17 @@ using SingleExtendFacetType =
//
// The flattening of the named constraints into interfaces is done by forming
// the IdentifiedFacetType for a specific Self type.
//
// TODO: Rename to DeclaredFacetType.
struct FacetTypeInfo : Printable<FacetTypeInfo> {
// Returns a FacetTypeInfo that combines `lhs` and `rhs`. It is not
struct DeclaredFacetType : Printable<DeclaredFacetType> {
// Returns a DeclaredFacetType that combines `lhs` and `rhs`. It is not
// canonicalized, so that it can be further modified by the caller if desired.
static auto Combine(const FacetTypeInfo& lhs, const FacetTypeInfo& rhs)
-> FacetTypeInfo;
static auto Combine(const DeclaredFacetType& lhs,
const DeclaredFacetType& rhs) -> DeclaredFacetType;
// Returns a FacetTypeInfo that only contains constraints that are extended by
// the facet type. It is not canonicalized, so that it can be further modified
// by the caller if desired.
static auto ExtendedOnly(const FacetTypeInfo& info) -> FacetTypeInfo;
// Returns a DeclaredFacetType that only contains constraints that are
// extended by the facet type. It is not canonicalized, so that it can be
// further modified by the caller if desired.
static auto ExtendedOnly(const DeclaredFacetType& declared_facet_type)
-> DeclaredFacetType;
// TODO: Need to switch to a processed, canonical form, that can support facet
// type equality as defined by
@@ -130,8 +129,8 @@ struct FacetTypeInfo : Printable<FacetTypeInfo> {
// by the facet type. If true, `ExtendedOnly()` would be a no-op.
auto IsExtendedOnly() const -> bool;
friend auto operator==(const FacetTypeInfo& lhs, const FacetTypeInfo& rhs)
-> bool {
friend auto operator==(const DeclaredFacetType& lhs,
const DeclaredFacetType& rhs) -> bool {
return lhs.extend_constraints == rhs.extend_constraints &&
lhs.self_impls_constraints == rhs.self_impls_constraints &&
lhs.extend_named_constraints == rhs.extend_named_constraints &&
@@ -145,15 +144,15 @@ struct FacetTypeInfo : Printable<FacetTypeInfo> {
}
};
constexpr FacetTypeInfo::RewriteConstraint
FacetTypeInfo::RewriteConstraint::None = {.lhs_id = InstId::None,
.rhs_id = InstId::None};
constexpr DeclaredFacetType::RewriteConstraint
DeclaredFacetType::RewriteConstraint::None = {.lhs_id = InstId::None,
.rhs_id = InstId::None};
using FacetTypeInfoStore =
CanonicalValueStore<FacetTypeId, FacetTypeInfo, Tag<CheckIRId>>;
using DeclaredFacetTypeStore =
CanonicalValueStore<DeclaredFacetTypeId, DeclaredFacetType, Tag<CheckIRId>>;
struct IdentifiedFacetTypeKey {
FacetTypeId facet_type_id;
DeclaredFacetTypeId declared_facet_type_id;
ConstantId self_const_id;
// Inside a named constraint, each identification of the `Self` facet type can
// be unique, as it can be modified by each require declaration seen so far.
@@ -244,7 +243,7 @@ using IdentifiedFacetTypeStore =
Tag<CheckIRId>, IdentifiedFacetType>;
// See common/hashing.h.
inline auto CarbonHashValue(const FacetTypeInfo& value, uint64_t seed)
inline auto CarbonHashValue(const DeclaredFacetType& value, uint64_t seed)
-> HashCode {
Hasher hasher(seed);
hasher.HashArray(llvm::ArrayRef(value.extend_constraints));
@@ -268,16 +267,18 @@ auto AddCanonicalWitnessesBlock(File& sem_ir,
} // namespace Carbon::SemIR
namespace Carbon {
extern template class CanonicalValueStore<
SemIR::FacetTypeId, SemIR::FacetTypeInfo, Tag<SemIR::CheckIRId>>;
extern template class CanonicalValueStore<SemIR::DeclaredFacetTypeId,
SemIR::DeclaredFacetType,
Tag<SemIR::CheckIRId>>;
extern template class CanonicalValueStore<
SemIR::IdentifiedFacetTypeId, SemIR::IdentifiedFacetTypeKey,
Tag<SemIR::CheckIRId>, SemIR::IdentifiedFacetType>;
extern template class ValueStore<SemIR::FacetTypeId, SemIR::FacetTypeInfo,
extern template class ValueStore<SemIR::DeclaredFacetTypeId,
SemIR::DeclaredFacetType,
Tag<SemIR::CheckIRId>>;
extern template class ValueStore<SemIR::IdentifiedFacetTypeId,
SemIR::IdentifiedFacetType,
Tag<SemIR::CheckIRId>>;
} // namespace Carbon
#endif // CARBON_TOOLCHAIN_SEM_IR_FACET_TYPE_INFO_H_
#endif // CARBON_TOOLCHAIN_SEM_IR_DECLARED_FACET_TYPE_H_
+9 -6
View File
@@ -240,15 +240,17 @@ LLVM_DUMP_METHOD auto Dump(const File& file, EntityNameId entity_name_id)
return out.TakeStr();
}
LLVM_DUMP_METHOD auto Dump(const File& file, FacetTypeId facet_type_id)
LLVM_DUMP_METHOD auto Dump(const File& file,
DeclaredFacetTypeId declared_facet_type_id)
-> std::string {
RawStringOstream out;
out << facet_type_id;
if (!facet_type_id.has_value()) {
out << declared_facet_type_id;
if (!declared_facet_type_id.has_value()) {
return out.TakeStr();
}
const auto& facet_type = file.facet_types().Get(facet_type_id);
const auto& facet_type =
file.declared_facet_types().Get(declared_facet_type_id);
out << ": " << facet_type;
for (auto impls : facet_type.extend_constraints) {
out << "\n - " << DumpInterfaceSummary(file, impls.interface_id);
@@ -626,8 +628,9 @@ LLVM_DUMP_METHOD auto MakeSymbolicConstantId(int id) -> ConstantId {
LLVM_DUMP_METHOD static auto MakeEntityNameId(int id) -> EntityNameId {
return EntityNameId(id);
}
LLVM_DUMP_METHOD static auto MakeFacetTypeId(int id) -> FacetTypeId {
return FacetTypeId(id);
LLVM_DUMP_METHOD static auto MakeDeclaredFacetTypeId(int id)
-> DeclaredFacetTypeId {
return DeclaredFacetTypeId(id);
}
LLVM_DUMP_METHOD static auto MakeFunctionId(int id) -> FunctionId {
return FunctionId(id);
+2 -1
View File
@@ -25,7 +25,8 @@ auto Dump(const File& file, RawBundleId bundle_id) -> std::string;
auto Dump(const File& file, ClassId class_id) -> std::string;
auto Dump(const File& file, ConstantId const_id) -> std::string;
auto Dump(const File& file, EntityNameId entity_name_id) -> std::string;
auto Dump(const File& file, FacetTypeId facet_type_id) -> std::string;
auto Dump(const File& file, DeclaredFacetTypeId declared_facet_type_id)
-> std::string;
auto Dump(const File& file, FieldId field_id) -> std::string;
auto Dump(const File& file, FunctionId function_id) -> std::string;
auto Dump(const File& file, GenericId generic_id) -> std::string;
+2 -2
View File
@@ -54,7 +54,7 @@ File::File(const Parse::Tree* parse_tree, CheckIRId check_ir_id,
// 1 reserved id for `ObserveBlockId::Empty`.
observe_blocks_(allocator_, check_ir_id, 1),
associated_constants_(check_ir_id),
facet_types_(check_ir_id),
declared_facet_types_(check_ir_id),
identified_facet_types_(check_ir_id),
impls_(*this),
specific_interfaces_(check_ir_id),
@@ -171,7 +171,7 @@ auto File::OutputYaml(bool include_singletons) const -> Yaml::OutputMapping {
map.Add("specific_interfaces", specific_interfaces_.OutputYaml());
map.Add("struct_type_fields", struct_type_fields_.OutputYaml());
map.Add("types", types_.OutputYaml());
map.Add("facet_types", facet_types_.OutputYaml());
map.Add("declared_facet_types", declared_facet_types_.OutputYaml());
map.Add("insts",
Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
for (auto [id, inst] : insts_.enumerate()) {
+9 -6
View File
@@ -24,8 +24,8 @@
#include "toolchain/sem_ir/constant.h"
#include "toolchain/sem_ir/cpp_file.h"
#include "toolchain/sem_ir/cpp_overload_set.h"
#include "toolchain/sem_ir/declared_facet_type.h"
#include "toolchain/sem_ir/entity_name.h"
#include "toolchain/sem_ir/facet_type_info.h"
#include "toolchain/sem_ir/field.h"
#include "toolchain/sem_ir/function.h"
#include "toolchain/sem_ir/generic.h"
@@ -209,9 +209,12 @@ class File : public Printable<File> {
auto associated_constants() const -> const AssociatedConstantStore& {
return associated_constants_;
}
// TODO: Rename these to `facet_type_infos`.
auto facet_types() -> FacetTypeInfoStore& { return facet_types_; }
auto facet_types() const -> const FacetTypeInfoStore& { return facet_types_; }
auto declared_facet_types() -> DeclaredFacetTypeStore& {
return declared_facet_types_;
}
auto declared_facet_types() const -> const DeclaredFacetTypeStore& {
return declared_facet_types_;
}
// If `class_id` is an imported C++ class, appends the Clang mangled name of
// its type to `out` and returns true. Otherwise returns false and leaves
@@ -391,8 +394,8 @@ class File : public Printable<File> {
// Storage for associated constants.
AssociatedConstantStore associated_constants_;
// Storage for facet types.
FacetTypeInfoStore facet_types_;
// Storage for declared facet types.
DeclaredFacetTypeStore declared_facet_types_;
// Storage for identified facet types.
IdentifiedFacetTypeStore identified_facet_types_;
+22 -19
View File
@@ -1548,10 +1548,10 @@ auto Formatter::FormatArg(EntityNameId id) -> void {
}
}
auto Formatter::FormatArg(FacetTypeId id) -> void {
const auto& info = sem_ir_->facet_types().Get(id);
auto Formatter::FormatArg(DeclaredFacetTypeId id) -> void {
const auto& declared_facet_type = sem_ir_->declared_facet_types().Get(id);
// Nothing output to indicate that this is a facet type since this is only
// used as the argument to a `facet_type` instruction.
// used as the argument to a `declared_facet_type` instruction.
out() << "<";
auto format_specific = [&](SemIR::SpecificId specific_id) {
@@ -1562,42 +1562,44 @@ auto Formatter::FormatArg(FacetTypeId id) -> void {
};
llvm::ListSeparator sep(" & ");
if (info.extend_constraints.empty() &&
info.extend_named_constraints.empty()) {
if (declared_facet_type.extend_constraints.empty() &&
declared_facet_type.extend_named_constraints.empty()) {
out() << "type";
} else {
for (auto extend : info.extend_constraints) {
for (auto extend : declared_facet_type.extend_constraints) {
out() << sep;
FormatName(extend.interface_id);
format_specific(extend.specific_id);
}
for (auto extend : info.extend_named_constraints) {
for (auto extend : declared_facet_type.extend_named_constraints) {
out() << sep;
FormatName(extend.named_constraint_id);
format_specific(extend.specific_id);
}
}
if (info.other_requirements || !info.self_impls_constraints.empty() ||
!info.type_impls_interfaces.empty() ||
!info.type_impls_named_constraints.empty() ||
!info.rewrite_constraints.empty()) {
if (declared_facet_type.other_requirements ||
!declared_facet_type.self_impls_constraints.empty() ||
!declared_facet_type.type_impls_interfaces.empty() ||
!declared_facet_type.type_impls_named_constraints.empty() ||
!declared_facet_type.rewrite_constraints.empty()) {
out() << " where ";
llvm::ListSeparator and_sep(" and ");
int num_self_impls = info.self_impls_constraints.size() +
info.self_impls_named_constraints.size();
int num_self_impls =
declared_facet_type.self_impls_constraints.size() +
declared_facet_type.self_impls_named_constraints.size();
if (num_self_impls > 0) {
out() << and_sep << ".Self impls ";
llvm::ListSeparator amp_sep(" & ");
if (num_self_impls > 1) {
out() << "(";
}
for (auto self_impls : info.self_impls_constraints) {
for (auto self_impls : declared_facet_type.self_impls_constraints) {
out() << amp_sep;
FormatName(self_impls.interface_id);
format_specific(self_impls.specific_id);
}
for (auto self_impls : info.self_impls_named_constraints) {
for (auto self_impls : declared_facet_type.self_impls_named_constraints) {
out() << amp_sep;
FormatName(self_impls.named_constraint_id);
format_specific(self_impls.specific_id);
@@ -1606,27 +1608,28 @@ auto Formatter::FormatArg(FacetTypeId id) -> void {
out() << ")";
}
}
for (const auto& type_impls : info.type_impls_interfaces) {
for (const auto& type_impls : declared_facet_type.type_impls_interfaces) {
out() << and_sep;
FormatName(type_impls.self_type);
out() << " impls ";
FormatName(type_impls.specific_interface.interface_id);
format_specific(type_impls.specific_interface.specific_id);
}
for (const auto& type_impls : info.type_impls_named_constraints) {
for (const auto& type_impls :
declared_facet_type.type_impls_named_constraints) {
out() << and_sep;
FormatName(type_impls.self_type);
out() << " impls ";
FormatName(type_impls.specific_named_constraint.named_constraint_id);
format_specific(type_impls.specific_named_constraint.specific_id);
}
for (auto rewrite : info.rewrite_constraints) {
for (auto rewrite : declared_facet_type.rewrite_constraints) {
out() << and_sep;
FormatArg(rewrite.lhs_id);
out() << " = ";
FormatArg(rewrite.rhs_id);
}
if (info.other_requirements) {
if (declared_facet_type.other_requirements) {
out() << and_sep << "TODO";
}
}
+1 -1
View File
@@ -272,7 +272,7 @@ class Formatter {
auto FormatArg(BoolValue v) -> void { out() << v; }
auto FormatArg(CharId c) -> void { out() << c; }
auto FormatArg(EntityNameId id) -> void;
auto FormatArg(FacetTypeId id) -> void;
auto FormatArg(DeclaredFacetTypeId id) -> void;
auto FormatArg(FieldId id) -> void;
auto FormatArg(IntKind k) -> void { k.Print(out()); }
auto FormatArg(FloatKind k) -> void { k.Print(out()); }
+1 -1
View File
@@ -42,12 +42,12 @@ using IdKind = TypeEnum<
ConstantId,
CppOverloadSetId,
CustomLayoutId,
DeclaredFacetTypeId,
DeclInstBlockId,
DestInstId,
ElementIndex,
EntityNameId,
ExprRegionId,
FacetTypeId,
FieldId,
FloatKind,
FunctionId,
+3 -3
View File
@@ -360,9 +360,9 @@ struct AssociatedConstantId : public IdBase<AssociatedConstantId> {
using IdBase::IdBase;
};
// The ID of a `FacetTypeInfo`.
struct FacetTypeId : public IdBase<FacetTypeId> {
static constexpr llvm::StringLiteral Label = "facet_type";
// The ID of a `DeclaredFacetType`.
struct DeclaredFacetTypeId : public IdBase<DeclaredFacetTypeId> {
static constexpr llvm::StringLiteral Label = "declared_facet_type";
using DiagnosticType = Diagnostics::TypeInfo<std::string>;
using IdBase::IdBase;
+1 -1
View File
@@ -6,7 +6,7 @@
#include "toolchain/base/kind_switch.h"
#include "toolchain/base/value_store_impl.h"
#include "toolchain/sem_ir/facet_type_info.h"
#include "toolchain/sem_ir/declared_facet_type.h"
#include "toolchain/sem_ir/file.h"
#include "toolchain/sem_ir/specific_interface.h"
#include "toolchain/sem_ir/specific_named_constraint.h"
+1 -1
View File
@@ -9,8 +9,8 @@
#include "common/map.h"
#include "toolchain/base/value_store.h"
#include "toolchain/sem_ir/declared_facet_type.h"
#include "toolchain/sem_ir/entity_with_params_base.h"
#include "toolchain/sem_ir/facet_type_info.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::SemIR {
+3 -2
View File
@@ -521,8 +521,9 @@ struct Worklist {
store->AddInteger(block_id.index);
}
auto Add(FacetTypeId facet_type_id) -> void {
const auto& facet_type = sem_ir->facet_types().Get(facet_type_id);
auto Add(DeclaredFacetTypeId declared_facet_type_id) -> void {
const auto& facet_type =
sem_ir->declared_facet_types().Get(declared_facet_type_id);
auto add_constraints = [&](auto constraints) {
store->AddInteger(constraints.size());
for (auto [first, second] : constraints) {
+21 -20
View File
@@ -1084,27 +1084,28 @@ auto InstNamer::NamingContext::NameInst() -> void {
return;
}
case CARBON_KIND(FacetType inst): {
const auto& facet_type_info =
sem_ir().facet_types().Get(inst.facet_type_id);
bool has_where = facet_type_info.other_requirements ||
!facet_type_info.self_impls_constraints.empty() ||
!facet_type_info.self_impls_named_constraints.empty() ||
!facet_type_info.type_impls_interfaces.empty() ||
!facet_type_info.type_impls_named_constraints.empty() ||
!facet_type_info.rewrite_constraints.empty();
if (facet_type_info.extend_constraints.size() == 1 &&
facet_type_info.extend_named_constraints.empty()) {
const auto& declared_facet_type =
sem_ir().declared_facet_types().Get(inst.declared_facet_type_id);
bool has_where =
declared_facet_type.other_requirements ||
!declared_facet_type.self_impls_constraints.empty() ||
!declared_facet_type.self_impls_named_constraints.empty() ||
!declared_facet_type.type_impls_interfaces.empty() ||
!declared_facet_type.type_impls_named_constraints.empty() ||
!declared_facet_type.rewrite_constraints.empty();
if (declared_facet_type.extend_constraints.size() == 1 &&
declared_facet_type.extend_named_constraints.empty()) {
AddEntityNameAndMaybePush(
facet_type_info.extend_constraints.front().interface_id,
declared_facet_type.extend_constraints.front().interface_id,
has_where ? "_where.type" : ".type");
} else if (facet_type_info.extend_named_constraints.size() == 1 &&
facet_type_info.extend_constraints.empty()) {
} else if (declared_facet_type.extend_named_constraints.size() == 1 &&
declared_facet_type.extend_constraints.empty()) {
AddEntityNameAndMaybePush(
facet_type_info.extend_named_constraints.front()
declared_facet_type.extend_named_constraints.front()
.named_constraint_id,
has_where ? "_where.type" : ".type");
} else if (facet_type_info.extend_constraints.empty() &&
facet_type_info.extend_named_constraints.empty()) {
} else if (declared_facet_type.extend_constraints.empty() &&
declared_facet_type.extend_named_constraints.empty()) {
AddInstName(has_where ? "type_where" : "type");
} else {
AddInstName("facet_type");
@@ -1114,9 +1115,9 @@ auto InstNamer::NamingContext::NameInst() -> void {
case CARBON_KIND(FacetValue inst): {
if (auto facet_type =
sem_ir().types().TryGetAs<FacetType>(inst.type_id)) {
const auto& facet_type_info =
sem_ir().facet_types().Get(facet_type->facet_type_id);
if (auto single = facet_type_info.TryAsSingleExtend()) {
const auto& declared_facet_type = sem_ir().declared_facet_types().Get(
facet_type->declared_facet_type_id);
if (auto single = declared_facet_type.TryAsSingleExtend()) {
CARBON_KIND_SWITCH(*single) {
case CARBON_KIND(SemIR::SpecificInterface interface): {
AddEntityNameAndMaybePush(interface.interface_id, ".facet");
@@ -1130,7 +1131,7 @@ auto InstNamer::NamingContext::NameInst() -> void {
}
return;
}
if (facet_type_info.HasNoConstraints()) {
if (declared_facet_type.HasNoConstraints()) {
if (auto class_ty =
sem_ir().insts().TryGetAs<ClassType>(inst.type_inst_id)) {
AddEntityNameAndMaybePush(class_ty->class_id, ".type.facet");
+29 -26
View File
@@ -12,8 +12,8 @@
#include "common/concepts.h"
#include "common/raw_string_ostream.h"
#include "toolchain/base/kind_switch.h"
#include "toolchain/sem_ir/declared_facet_type.h"
#include "toolchain/sem_ir/entity_with_params_base.h"
#include "toolchain/sem_ir/facet_type_info.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/inst_kind.h"
#include "toolchain/sem_ir/singleton_insts.h"
@@ -51,9 +51,9 @@ class StepStack {
// An individual step in the stack, which stringifies some component of a type
// name.
using Step =
std::variant<InstId, llvm::StringRef, NameId, ElementIndex, FacetTypeId,
StopQualifiedNames, ResumeQualifiedNames>;
using Step = std::variant<InstId, llvm::StringRef, NameId, ElementIndex,
DeclaredFacetTypeId, StopQualifiedNames,
ResumeQualifiedNames>;
// Support `Push` for a qualified name. e.g., `A.B.C`.
using QualifiedNameItem = std::pair<NameScopeId, NameId>;
@@ -79,8 +79,8 @@ class StepStack {
auto PushElementIndex(ElementIndex element_index) -> void {
steps_.push_back(element_index);
}
auto PushFacetType(FacetTypeId facet_type_id) -> void {
steps_.push_back(facet_type_id);
auto PushFacetType(DeclaredFacetTypeId declared_facet_type_id) -> void {
steps_.push_back(declared_facet_type_id);
}
auto PushResumeQualfiedNames() -> void {
steps_.push_back(ResumeQualifiedNames{});
@@ -375,7 +375,7 @@ class Stringifier {
}
auto StringifyInst(InstId /*inst_id*/, FacetType inst) -> void {
step_stack_->PushFacetType(inst.facet_type_id);
step_stack_->PushFacetType(inst.declared_facet_type_id);
}
auto StringifyInst(InstId /*inst_id*/, FacetValue inst) -> void {
@@ -713,40 +713,42 @@ class Stringifier {
*out_ << "<vtable ptr>";
}
auto StringifyFacetType(FacetTypeId facet_type_id) -> void {
const FacetTypeInfo& facet_type_info =
sem_ir_->facet_types().Get(facet_type_id);
auto StringifyFacetType(DeclaredFacetTypeId declared_facet_type_id) -> void {
const DeclaredFacetType& declared_facet_type =
sem_ir_->declared_facet_types().Get(declared_facet_type_id);
// Output `where` restrictions.
bool some_where = false;
if (facet_type_info.other_requirements) {
if (declared_facet_type.other_requirements) {
step_stack_->PushString("...");
some_where = true;
}
for (auto rewrite : llvm::reverse(facet_type_info.rewrite_constraints)) {
for (auto rewrite :
llvm::reverse(declared_facet_type.rewrite_constraints)) {
if (some_where) {
step_stack_->PushString(" and");
}
step_stack_->Push(" ", rewrite.lhs_id, " = ", rewrite.rhs_id);
some_where = true;
}
if (!facet_type_info.self_impls_constraints.empty() ||
!facet_type_info.self_impls_named_constraints.empty()) {
if (!declared_facet_type.self_impls_constraints.empty() ||
!declared_facet_type.self_impls_named_constraints.empty()) {
if (some_where) {
step_stack_->PushString(" and");
}
llvm::ListSeparator sep(" & ");
for (auto impls :
llvm::reverse(facet_type_info.self_impls_named_constraints)) {
llvm::reverse(declared_facet_type.self_impls_named_constraints)) {
step_stack_->Push(impls, &sep);
}
for (auto impls : llvm::reverse(facet_type_info.self_impls_constraints)) {
for (auto impls :
llvm::reverse(declared_facet_type.self_impls_constraints)) {
step_stack_->Push(impls, &sep);
}
step_stack_->PushString(" .Self impls ");
some_where = true;
}
for (const auto& type_impls :
llvm::reverse(facet_type_info.type_impls_interfaces)) {
llvm::reverse(declared_facet_type.type_impls_interfaces)) {
if (some_where) {
step_stack_->PushString(" and");
}
@@ -755,7 +757,7 @@ class Stringifier {
some_where = true;
}
for (const auto& type_impls :
llvm::reverse(facet_type_info.type_impls_named_constraints)) {
llvm::reverse(declared_facet_type.type_impls_named_constraints)) {
if (some_where) {
step_stack_->PushString(" and");
}
@@ -768,17 +770,17 @@ class Stringifier {
}
// Output extend interface and named constraint requirements.
if (facet_type_info.extend_constraints.empty() &&
facet_type_info.extend_named_constraints.empty()) {
if (declared_facet_type.extend_constraints.empty() &&
declared_facet_type.extend_named_constraints.empty()) {
step_stack_->PushString("type");
return;
}
llvm::ListSeparator sep(" & ");
for (auto extend :
llvm::reverse(facet_type_info.extend_named_constraints)) {
llvm::reverse(declared_facet_type.extend_named_constraints)) {
step_stack_->Push(extend, &sep);
}
for (auto extend : llvm::reverse(facet_type_info.extend_constraints)) {
for (auto extend : llvm::reverse(declared_facet_type.extend_constraints)) {
step_stack_->Push(extend, &sep);
}
}
@@ -827,8 +829,8 @@ static auto Stringify(const File& sem_ir, StepStack& step_stack)
out << element_index.index;
break;
}
case CARBON_KIND(FacetTypeId facet_type_id): {
stringifier.StringifyFacetType(facet_type_id);
case CARBON_KIND(DeclaredFacetTypeId declared_facet_type_id): {
stringifier.StringifyFacetType(declared_facet_type_id);
break;
}
case CARBON_KIND(StepStack::StopQualifiedNames _): {
@@ -936,10 +938,11 @@ auto StringifySpecificInterface(const File& sem_ir,
}
}
auto StringifyFacetType(const File& sem_ir, FacetTypeId facet_type_id)
auto StringifyDeclaredFacetType(const File& sem_ir,
DeclaredFacetTypeId declared_facet_type_id)
-> std::string {
StepStack step_stack(&sem_ir);
step_stack.PushFacetType(facet_type_id);
step_stack.PushFacetType(declared_facet_type_id);
return Stringify(sem_ir, step_stack);
}
+4 -3
View File
@@ -34,10 +34,11 @@ auto StringifySpecificInterface(const File& sem_ir,
SpecificInterface specific_interface)
-> std::string;
// Produces a string version of the facet type. This contains the name of the
// interfaces or named constraints that the facet type names, and any
// Produces a string version of the declared facet type. This contains the name
// of the interfaces or named constraints that the facet type names, and any
// requirements such as rewrites.
auto StringifyFacetType(const File& sem_ir, FacetTypeId facet_type_id)
auto StringifyDeclaredFacetType(const File& sem_ir,
DeclaredFacetTypeId declared_facet_type_id)
-> std::string;
} // namespace Carbon::SemIR
+12 -7
View File
@@ -27,26 +27,31 @@ auto TypeIterator::Next() -> Step {
return Step::End();
}
case CARBON_KIND(FacetType facet_type): {
const auto& info = sem_ir_->facet_types().Get(facet_type.facet_type_id);
for (const auto& extend : info.extend_constraints) {
const auto& declared_facet_type = sem_ir_->declared_facet_types().Get(
facet_type.declared_facet_type_id);
for (const auto& extend : declared_facet_type.extend_constraints) {
Push(SpecificInterface{extend.interface_id, extend.specific_id});
}
for (const auto& extend : info.extend_named_constraints) {
for (const auto& extend :
declared_facet_type.extend_named_constraints) {
Push(SpecificNamedConstraint{extend.named_constraint_id,
extend.specific_id});
}
for (const auto& impls : info.self_impls_constraints) {
for (const auto& impls : declared_facet_type.self_impls_constraints) {
Push(SpecificInterface{impls.interface_id, impls.specific_id});
}
for (const auto& impls : info.self_impls_named_constraints) {
for (const auto& impls :
declared_facet_type.self_impls_named_constraints) {
Push(SpecificNamedConstraint{impls.named_constraint_id,
impls.specific_id});
}
for (const auto& type_impls : info.type_impls_interfaces) {
for (const auto& type_impls :
declared_facet_type.type_impls_interfaces) {
PushInstId(type_impls.self_type);
Push(type_impls.specific_interface);
}
for (const auto& type_impls : info.type_impls_named_constraints) {
for (const auto& type_impls :
declared_facet_type.type_impls_named_constraints) {
PushInstId(type_impls.self_type);
Push(type_impls.specific_named_constraint);
}
+1 -2
View File
@@ -729,8 +729,7 @@ struct FacetType {
.constant_kind = InstConstantKind::Always});
TypeId type_id;
// TODO: Rename this to facet_type_info_id.
FacetTypeId facet_type_id;
DeclaredFacetTypeId declared_facet_type_id;
};
// A facet value is a general value of type FacetType. This consists of a type
+1 -1
View File
@@ -74,7 +74,7 @@ TEST(SemIRTest, Yaml) {
Pair("specific_interfaces", Yaml::Mapping(SizeIs(0))),
Pair("struct_type_fields", Yaml::Mapping(SizeIs(1))),
Pair("types", Yaml::Mapping(Each(type_builtin))),
Pair("facet_types", Yaml::Mapping(SizeIs(0))),
Pair("declared_facet_types", Yaml::Mapping(SizeIs(0))),
Pair("insts",
Yaml::Mapping(AllOf(
Each(Key(inst_id)),