mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-23 18:02:40 +00:00
Add support for default values for non-generic parameters of generic functions. (#8011)
A first step in expanding support for default values for parameters in generic functions. Adds code to parameter type deduction to include default values for absent call arguments. In support of that, this PR also generalizes a function from `export.cpp` that is useful to extract a default `ConstantId` value from an arbitrary function paramater pattern.
This commit is contained in:
1 parent
04f456dddd
commit
503f09037d
5 files changed
+604
-46
No files matched your search
@@ -9,6 +9,7 @@
|
||||
#include "toolchain/check/context.h"
|
||||
#include "toolchain/check/convert.h"
|
||||
#include "toolchain/check/generic.h"
|
||||
#include "toolchain/check/pattern.h"
|
||||
#include "toolchain/check/subst.h"
|
||||
#include "toolchain/check/type.h"
|
||||
#include "toolchain/diagnostics/diagnostic.h"
|
||||
@@ -622,6 +623,7 @@ auto DeduceGenericCallArguments(
|
||||
enclosing_specific_id,
|
||||
/*diagnose=*/true);
|
||||
|
||||
auto argument_count = arg_ids.size();
|
||||
// Prepare to perform deduction of the parameters against the explicit
|
||||
// arguments. When `self` is provided as the method-call receiver it is
|
||||
// prepended to the explicit argument list to match the leading `self`
|
||||
@@ -629,11 +631,32 @@ auto DeduceGenericCallArguments(
|
||||
llvm::ArrayRef<SemIR::InstId> self_refs = {};
|
||||
if (self_id.has_value()) {
|
||||
self_refs = self_id;
|
||||
++argument_count;
|
||||
}
|
||||
deduction.AddAll(context.inst_blocks().GetOrEmpty(param_patterns_id),
|
||||
llvm::ArrayRef<SemIR::InstId>(llvm::to_vector<8>(
|
||||
llvm::concat<const SemIR::InstId>(self_refs, arg_ids))),
|
||||
/*want_value=*/false);
|
||||
|
||||
auto param_patterns_block =
|
||||
context.inst_blocks().GetOrEmpty(param_patterns_id);
|
||||
|
||||
// If we are short on arguments, check if we can append default values for
|
||||
// deduction.
|
||||
llvm::SmallVector<SemIR::InstId> default_values;
|
||||
for (auto i = argument_count; i < param_patterns_block.size(); ++i) {
|
||||
auto default_value =
|
||||
ExtractParameterDefault(context, param_patterns_block[i]);
|
||||
if (!default_value.has_value()) {
|
||||
break;
|
||||
}
|
||||
default_values.push_back(
|
||||
context.constant_values().GetInstId(default_value));
|
||||
}
|
||||
|
||||
deduction.AddAll(
|
||||
param_patterns_block,
|
||||
llvm::ArrayRef<SemIR::InstId>(
|
||||
llvm::to_vector<8>(llvm::concat<const SemIR::InstId>(
|
||||
llvm::concat<const SemIR::InstId>(self_refs, arg_ids),
|
||||
default_values))),
|
||||
/*want_value=*/false);
|
||||
|
||||
if (!deduction.Deduce() || !deduction.CheckDeductionIsComplete()) {
|
||||
return SemIR::SpecificId::None;
|
||||
|
||||
Reference in new issue
Block a user