mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Implement thunking in terms of constant evaluation (#7332)
The bulk of this change is changing most pattern insts to be `Always` rather than `AlwaysUnique` constants, so that they can be wrapped in `SpecificConstant`s to perform substitution. That then lets thunking rely much more on `SpecificConstant` wrappers instead of deep-copying the inst tree with modified types. This approach to thunking should scale better, particularly as things like form generics make function signatures more complex, because we can leverage the existing support for constant evaluation and substitution. Unfortunately, applying this approach to binding patterns will require more work; see the TODO near the top of `thunk.cpp` for details. --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
co-authored by
Richard Smith
parent
40d2d8f68c
commit
e023f75254
+32
-15
@@ -36,12 +36,19 @@ class DeductionWorklist {
|
||||
SemIR::InstId param;
|
||||
SemIR::InstId arg;
|
||||
bool want_value;
|
||||
SemIR::LocId param_loc_id;
|
||||
};
|
||||
|
||||
// Adds a single (param, arg) deduction.
|
||||
auto Add(SemIR::InstId param, SemIR::InstId arg, bool want_value) -> void {
|
||||
deductions_.push_back(
|
||||
{.param = param, .arg = arg, .want_value = want_value});
|
||||
auto Add(SemIR::InstId param, SemIR::InstId arg, bool want_value,
|
||||
SemIR::LocId param_loc_id = SemIR::LocId::None) -> void {
|
||||
if (!param_loc_id.has_value()) {
|
||||
param_loc_id = SemIR::LocId(param);
|
||||
}
|
||||
deductions_.push_back({.param = param,
|
||||
.arg = arg,
|
||||
.want_value = want_value,
|
||||
.param_loc_id = param_loc_id});
|
||||
}
|
||||
|
||||
// Adds a single (param, arg) deduction of a specific.
|
||||
@@ -163,11 +170,13 @@ class DeductionContext {
|
||||
|
||||
auto context() const -> Context& { return *context_; }
|
||||
|
||||
// Adds a pending deduction of `param` from `arg`. `needs_substitution`
|
||||
// indicates whether we need to substitute known generic parameters into
|
||||
// `param`.
|
||||
auto Add(SemIR::InstId param, SemIR::InstId arg, bool want_value) -> void {
|
||||
worklist_.Add(param, arg, want_value);
|
||||
// Adds a pending deduction of `param` from `arg`. If `param_loc_id` is not
|
||||
// `None`, it is used in place of `LocId(param)` in diagnostics.
|
||||
// `needs_substitution` indicates whether we need to substitute known generic
|
||||
// parameters into `param`.
|
||||
auto Add(SemIR::InstId param, SemIR::InstId arg, bool want_value,
|
||||
SemIR::LocId param_loc_id = SemIR::LocId::None) -> void {
|
||||
worklist_.Add(param, arg, want_value, param_loc_id);
|
||||
}
|
||||
|
||||
template <typename ParamT, typename ArgT>
|
||||
@@ -205,13 +214,14 @@ class DeductionContext {
|
||||
auto MakeSpecific() -> SemIR::SpecificId;
|
||||
|
||||
private:
|
||||
auto NoteInitializingParam(SemIR::InstId param_id, auto& builder) -> void {
|
||||
auto NoteInitializingParam(SemIR::InstId param_id, SemIR::LocId loc_id,
|
||||
auto& builder) -> void {
|
||||
if (auto param = context().insts().TryGetAs<SemIR::SymbolicBindingPattern>(
|
||||
param_id)) {
|
||||
CARBON_DIAGNOSTIC(InitializingGenericParam, Note,
|
||||
"initializing generic parameter `{0}` declared here",
|
||||
SemIR::NameId);
|
||||
builder.Note(param_id, InitializingGenericParam,
|
||||
builder.Note(loc_id, InitializingGenericParam,
|
||||
context().entity_names().Get(param->entity_name_id).name_id);
|
||||
} else {
|
||||
NoteGenericHere(context(), generic_id_, builder);
|
||||
@@ -285,13 +295,19 @@ DeductionContext::DeductionContext(Context* context, SemIR::LocId loc_id,
|
||||
|
||||
auto DeductionContext::Deduce() -> bool {
|
||||
while (!worklist_.Done()) {
|
||||
auto [param_id, arg_id, want_value] = worklist_.PopNext();
|
||||
auto [param_id, arg_id, want_value, param_loc_id] = worklist_.PopNext();
|
||||
|
||||
if (param_id == SemIR::ErrorInst::InstId) {
|
||||
return false;
|
||||
}
|
||||
// TODO: Bail out if there's nothing to deduce: if we're not in a pattern
|
||||
// and the parameter doesn't have a symbolic constant value.
|
||||
|
||||
auto param = context().insts().Get(param_id);
|
||||
auto param_type_id = param.type_id();
|
||||
if (param_type_id == SemIR::ErrorInst::TypeId) {
|
||||
return false;
|
||||
}
|
||||
if (context().types().Is<SemIR::PatternType>(param_type_id)) {
|
||||
param_type_id =
|
||||
SemIR::ExtractScrutineeType(context().sem_ir(), param_type_id);
|
||||
@@ -324,7 +340,7 @@ auto DeductionContext::Deduce() -> bool {
|
||||
Diagnostics::AnnotationScope annotate_diagnostics(
|
||||
&context().emitter(), [&](auto& builder) {
|
||||
if (diagnose_) {
|
||||
NoteInitializingParam(param_id, builder);
|
||||
NoteInitializingParam(param_id, param_loc_id, builder);
|
||||
}
|
||||
});
|
||||
// `want_value` is only set within types and the arguments for symbolic
|
||||
@@ -373,7 +389,7 @@ auto DeductionContext::Deduce() -> bool {
|
||||
"compile-time constant");
|
||||
auto diag =
|
||||
context().emitter().Build(loc_id_, CompTimeArgumentNotConstant);
|
||||
NoteInitializingParam(param_id, diag);
|
||||
NoteInitializingParam(param_id, param_loc_id, diag);
|
||||
diag.Emit();
|
||||
}
|
||||
return false;
|
||||
@@ -456,7 +472,7 @@ auto DeductionContext::Deduce() -> bool {
|
||||
auto param_const_inst_id =
|
||||
context().constant_values().GetInstId(param_const_id);
|
||||
if (param_const_inst_id != param_id) {
|
||||
Add(param_const_inst_id, arg_id, want_value);
|
||||
Add(param_const_inst_id, arg_id, want_value, param_loc_id);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -537,7 +553,8 @@ auto DeductionContext::CheckDeductionIsComplete() -> bool {
|
||||
Diagnostics::AnnotationScope annotate_diagnostics(
|
||||
&context().emitter(), [&](auto& builder) {
|
||||
if (diagnose_) {
|
||||
NoteInitializingParam(binding_id, builder);
|
||||
NoteInitializingParam(binding_id, SemIR::LocId(binding_id),
|
||||
builder);
|
||||
}
|
||||
});
|
||||
auto converted_arg_id =
|
||||
|
||||
Reference in New Issue
Block a user