Skip to content

Commit

Permalink
[clang] Changes to produce sugared converted template arguments
Browse files Browse the repository at this point in the history
Makes CheckTemplateArgumentList and the template deduction functions
produce a sugared converted argument list in addition to the canonical one.

This is mostly NFC except that we hook this up to a few diagnostics in
SemaOverload.

The infrastructure here will be used in subsequent patches
where we perform a finalized sugared substitution for entities
which we do not unique per specializations on canonical arguments,
and later on will be used for template specialization resugaring.

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D133874
  • Loading branch information
mizvekov committed Oct 26, 2022
1 parent 8acddef commit 11ce789
Show file tree
Hide file tree
Showing 10 changed files with 580 additions and 436 deletions.
43 changes: 22 additions & 21 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -8224,14 +8224,13 @@ class Sema final {
CTAK_DeducedFromArrayBound
};

bool CheckTemplateArgument(NamedDecl *Param,
TemplateArgumentLoc &Arg,
NamedDecl *Template,
SourceLocation TemplateLoc,
SourceLocation RAngleLoc,
unsigned ArgumentPackIndex,
SmallVectorImpl<TemplateArgument> &Converted,
CheckTemplateArgumentKind CTAK = CTAK_Specified);
bool
CheckTemplateArgument(NamedDecl *Param, TemplateArgumentLoc &Arg,
NamedDecl *Template, SourceLocation TemplateLoc,
SourceLocation RAngleLoc, unsigned ArgumentPackIndex,
SmallVectorImpl<TemplateArgument> &SugaredConverted,
SmallVectorImpl<TemplateArgument> &CanonicalConverted,
CheckTemplateArgumentKind CTAK);

/// Check that the given template arguments can be be provided to
/// the given template, converting the arguments along the way.
Expand Down Expand Up @@ -8262,23 +8261,25 @@ class Sema final {
/// the template not being satisfied by the template arguments.
///
/// \returns true if an error occurred, false otherwise.
bool CheckTemplateArgumentList(TemplateDecl *Template,
SourceLocation TemplateLoc,
TemplateArgumentListInfo &TemplateArgs,
bool PartialTemplateArgs,
SmallVectorImpl<TemplateArgument> &Converted,
bool UpdateArgsWithConversions = true,
bool *ConstraintsNotSatisfied = nullptr);

bool CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
TemplateArgumentLoc &Arg,
SmallVectorImpl<TemplateArgument> &Converted);
bool CheckTemplateArgumentList(
TemplateDecl *Template, SourceLocation TemplateLoc,
TemplateArgumentListInfo &TemplateArgs, bool PartialTemplateArgs,
SmallVectorImpl<TemplateArgument> &SugaredConverted,
SmallVectorImpl<TemplateArgument> &CanonicalConverted,
bool UpdateArgsWithConversions = true,
bool *ConstraintsNotSatisfied = nullptr);

bool CheckTemplateTypeArgument(
TemplateTypeParmDecl *Param, TemplateArgumentLoc &Arg,
SmallVectorImpl<TemplateArgument> &SugaredConverted,
SmallVectorImpl<TemplateArgument> &CanonicalConverted);

bool CheckTemplateArgument(TypeSourceInfo *Arg);
ExprResult CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
QualType InstantiatedParamType, Expr *Arg,
TemplateArgument &Converted,
CheckTemplateArgumentKind CTAK = CTAK_Specified);
TemplateArgument &SugaredConverted,
TemplateArgument &CanonicalConverted,
CheckTemplateArgumentKind CTAK);
bool CheckTemplateTemplateArgument(TemplateTemplateParmDecl *Param,
TemplateParameterList *Params,
TemplateArgumentLoc &Arg);
Expand Down
34 changes: 22 additions & 12 deletions clang/include/clang/Sema/TemplateDeduction.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace sema {
/// TemplateDeductionResult value.
class TemplateDeductionInfo {
/// The deduced template argument list.
TemplateArgumentList *Deduced = nullptr;
TemplateArgumentList *DeducedSugared = nullptr, *DeducedCanonical = nullptr;

/// The source location at which template argument
/// deduction is occurring.
Expand Down Expand Up @@ -71,8 +71,8 @@ class TemplateDeductionInfo {
/// Create temporary template deduction info for speculatively deducing
/// against a base class of an argument's type.
TemplateDeductionInfo(ForBaseTag, const TemplateDeductionInfo &Info)
: Deduced(Info.Deduced), Loc(Info.Loc), DeducedDepth(Info.DeducedDepth),
ExplicitArgs(Info.ExplicitArgs) {}
: DeducedSugared(Info.DeducedSugared), Loc(Info.Loc),
DeducedDepth(Info.DeducedDepth), ExplicitArgs(Info.ExplicitArgs) {}

/// Returns the location at which template argument is
/// occurring.
Expand All @@ -91,10 +91,15 @@ class TemplateDeductionInfo {
return ExplicitArgs;
}

/// Take ownership of the deduced template argument list.
TemplateArgumentList *take() {
TemplateArgumentList *Result = Deduced;
Deduced = nullptr;
/// Take ownership of the deduced template argument lists.
TemplateArgumentList *takeSugared() {
TemplateArgumentList *Result = DeducedSugared;
DeducedSugared = nullptr;
return Result;
}
TemplateArgumentList *takeCanonical() {
TemplateArgumentList *Result = DeducedCanonical;
DeducedCanonical = nullptr;
return Result;
}

Expand All @@ -120,15 +125,20 @@ class TemplateDeductionInfo {

/// Provide an initial template argument list that contains the
/// explicitly-specified arguments.
void setExplicitArgs(TemplateArgumentList *NewDeduced) {
Deduced = NewDeduced;
ExplicitArgs = Deduced->size();
void setExplicitArgs(TemplateArgumentList *NewDeducedSugared,
TemplateArgumentList *NewDeducedCanonical) {
assert(NewDeducedSugared->size() == NewDeducedCanonical->size());
DeducedSugared = NewDeducedSugared;
DeducedCanonical = NewDeducedCanonical;
ExplicitArgs = DeducedSugared->size();
}

/// Provide a new template argument list that contains the
/// results of template argument deduction.
void reset(TemplateArgumentList *NewDeduced) {
Deduced = NewDeduced;
void reset(TemplateArgumentList *NewDeducedSugared,
TemplateArgumentList *NewDeducedCanonical) {
DeducedSugared = NewDeducedSugared;
DeducedCanonical = NewDeducedCanonical;
}

/// Is a SFINAE diagnostic available?
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/Sema/SemaLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3694,11 +3694,11 @@ Sema::LookupLiteralOperator(Scope *S, LookupResult &R,
// is a well-formed template argument for the template parameter.
if (StringLit) {
SFINAETrap Trap(*this);
SmallVector<TemplateArgument, 1> Checked;
SmallVector<TemplateArgument, 1> SugaredChecked, CanonicalChecked;
TemplateArgumentLoc Arg(TemplateArgument(StringLit), StringLit);
if (CheckTemplateArgument(Params->getParam(0), Arg, FD,
R.getNameLoc(), R.getNameLoc(), 0,
Checked) ||
if (CheckTemplateArgument(
Params->getParam(0), Arg, FD, R.getNameLoc(), R.getNameLoc(),
0, SugaredChecked, CanonicalChecked, CTAK_Specified) ||
Trap.hasErrorOccurred())
IsTemplate = false;
}
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Sema/SemaOverload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ clang::MakeDeductionFailureInfo(ASTContext &Context,
auto *Saved = new (Context) DFIDeducedMismatchArgs;
Saved->FirstArg = Info.FirstArg;
Saved->SecondArg = Info.SecondArg;
Saved->TemplateArgs = Info.take();
Saved->TemplateArgs = Info.takeSugared();
Saved->CallArgIndex = Info.CallArgIndex;
Result.Data = Saved;
break;
Expand Down Expand Up @@ -669,7 +669,7 @@ clang::MakeDeductionFailureInfo(ASTContext &Context,
}

case Sema::TDK_SubstitutionFailure:
Result.Data = Info.take();
Result.Data = Info.takeSugared();
if (Info.hasSFINAEDiagnostic()) {
PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt(
SourceLocation(), PartialDiagnostic::NullDiagnostic());
Expand All @@ -680,7 +680,7 @@ clang::MakeDeductionFailureInfo(ASTContext &Context,

case Sema::TDK_ConstraintsNotSatisfied: {
CNSInfo *Saved = new (Context) CNSInfo;
Saved->TemplateArgs = Info.take();
Saved->TemplateArgs = Info.takeSugared();
Saved->Satisfaction = Info.AssociatedConstraintsSatisfaction;
Result.Data = Saved;
break;
Expand Down
Loading

0 comments on commit 11ce789

Please sign in to comment.