Skip to content

Commit

Permalink
[clang][NFC] Refactor Sema::TemplateDeductionResult (#81398)
Browse files Browse the repository at this point in the history
This patch converts `Sema::TemplateDeductionResult` into a scoped enum
in namespace scope, making it eligible for forward declaring. This is
useful in certain contexts, such as `preferred_type` annotations on
bit-fields.
  • Loading branch information
Endilll committed Feb 12, 2024
1 parent a8b4c11 commit dfbe2bc
Show file tree
Hide file tree
Showing 10 changed files with 678 additions and 606 deletions.
134 changes: 67 additions & 67 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,72 @@ class PreferredTypeBuilder {
llvm::function_ref<QualType()> ComputeType;
};

/// Describes the result of template argument deduction.
///
/// The TemplateDeductionResult enumeration describes the result of
/// template argument deduction, as returned from
/// DeduceTemplateArguments(). The separate TemplateDeductionInfo
/// structure provides additional information about the results of
/// template argument deduction, e.g., the deduced template argument
/// list (if successful) or the specific template parameters or
/// deduced arguments that were involved in the failure.
enum class TemplateDeductionResult {
/// Template argument deduction was successful.
Success = 0,
/// The declaration was invalid; do nothing.
Invalid,
/// Template argument deduction exceeded the maximum template
/// instantiation depth (which has already been diagnosed).
InstantiationDepth,
/// Template argument deduction did not deduce a value
/// for every template parameter.
Incomplete,
/// Template argument deduction did not deduce a value for every
/// expansion of an expanded template parameter pack.
IncompletePack,
/// Template argument deduction produced inconsistent
/// deduced values for the given template parameter.
Inconsistent,
/// Template argument deduction failed due to inconsistent
/// cv-qualifiers on a template parameter type that would
/// otherwise be deduced, e.g., we tried to deduce T in "const T"
/// but were given a non-const "X".
Underqualified,
/// Substitution of the deduced template argument values
/// resulted in an error.
SubstitutionFailure,
/// After substituting deduced template arguments, a dependent
/// parameter type did not match the corresponding argument.
DeducedMismatch,
/// After substituting deduced template arguments, an element of
/// a dependent parameter type did not match the corresponding element
/// of the corresponding argument (when deducing from an initializer list).
DeducedMismatchNested,
/// A non-depnedent component of the parameter did not match the
/// corresponding component of the argument.
NonDeducedMismatch,
/// When performing template argument deduction for a function
/// template, there were too many call arguments.
TooManyArguments,
/// When performing template argument deduction for a function
/// template, there were too few call arguments.
TooFewArguments,
/// The explicitly-specified template arguments were not valid
/// template arguments for the given template.
InvalidExplicitArguments,
/// Checking non-dependent argument conversions failed.
NonDependentConversionFailure,
/// The deduced arguments did not satisfy the constraints associated
/// with the template.
ConstraintsNotSatisfied,
/// Deduction failed; that's all we know.
MiscellaneousDeductionFailure,
/// CUDA Target attributes do not match.
CUDATargetMismatch,
/// Some error which was already diagnosed.
AlreadyDiagnosed
};

/// Sema - This implements semantic analysis and AST building for C.
class Sema final {
Sema(const Sema &) = delete;
Expand Down Expand Up @@ -9261,72 +9327,6 @@ class Sema final {
QualType adjustCCAndNoReturn(QualType ArgFunctionType, QualType FunctionType,
bool AdjustExceptionSpec = false);

/// Describes the result of template argument deduction.
///
/// The TemplateDeductionResult enumeration describes the result of
/// template argument deduction, as returned from
/// DeduceTemplateArguments(). The separate TemplateDeductionInfo
/// structure provides additional information about the results of
/// template argument deduction, e.g., the deduced template argument
/// list (if successful) or the specific template parameters or
/// deduced arguments that were involved in the failure.
enum TemplateDeductionResult {
/// Template argument deduction was successful.
TDK_Success = 0,
/// The declaration was invalid; do nothing.
TDK_Invalid,
/// Template argument deduction exceeded the maximum template
/// instantiation depth (which has already been diagnosed).
TDK_InstantiationDepth,
/// Template argument deduction did not deduce a value
/// for every template parameter.
TDK_Incomplete,
/// Template argument deduction did not deduce a value for every
/// expansion of an expanded template parameter pack.
TDK_IncompletePack,
/// Template argument deduction produced inconsistent
/// deduced values for the given template parameter.
TDK_Inconsistent,
/// Template argument deduction failed due to inconsistent
/// cv-qualifiers on a template parameter type that would
/// otherwise be deduced, e.g., we tried to deduce T in "const T"
/// but were given a non-const "X".
TDK_Underqualified,
/// Substitution of the deduced template argument values
/// resulted in an error.
TDK_SubstitutionFailure,
/// After substituting deduced template arguments, a dependent
/// parameter type did not match the corresponding argument.
TDK_DeducedMismatch,
/// After substituting deduced template arguments, an element of
/// a dependent parameter type did not match the corresponding element
/// of the corresponding argument (when deducing from an initializer list).
TDK_DeducedMismatchNested,
/// A non-depnedent component of the parameter did not match the
/// corresponding component of the argument.
TDK_NonDeducedMismatch,
/// When performing template argument deduction for a function
/// template, there were too many call arguments.
TDK_TooManyArguments,
/// When performing template argument deduction for a function
/// template, there were too few call arguments.
TDK_TooFewArguments,
/// The explicitly-specified template arguments were not valid
/// template arguments for the given template.
TDK_InvalidExplicitArguments,
/// Checking non-dependent argument conversions failed.
TDK_NonDependentConversionFailure,
/// The deduced arguments did not satisfy the constraints associated
/// with the template.
TDK_ConstraintsNotSatisfied,
/// Deduction failed; that's all we know.
TDK_MiscellaneousDeductionFailure,
/// CUDA Target attributes do not match.
TDK_CUDATargetMismatch,
/// Some error which was already diagnosed.
TDK_AlreadyDiagnosed
};

TemplateDeductionResult
DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
ArrayRef<TemplateArgument> TemplateArgs,
Expand Down Expand Up @@ -14444,7 +14444,7 @@ class Sema final {
};

DeductionFailureInfo
MakeDeductionFailureInfo(ASTContext &Context, Sema::TemplateDeductionResult TDK,
MakeDeductionFailureInfo(ASTContext &Context, TemplateDeductionResult TDK,
sema::TemplateDeductionInfo &Info);

/// Contains a late templated function.
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Sema/TemplateDeduction.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace clang {
class Decl;
struct DeducedPack;
class Sema;
enum class TemplateDeductionResult;

namespace sema {

Expand Down Expand Up @@ -295,6 +296,10 @@ struct DeductionFailureInfo {

/// Free any memory associated with this deduction failure.
void Destroy();

TemplateDeductionResult getResult() const {
return static_cast<TemplateDeductionResult>(Result);
}
};

/// TemplateSpecCandidate - This is a generalization of OverloadCandidate
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13070,7 +13070,8 @@ QualType Sema::deduceVarTypeFromInitializer(VarDecl *VDecl,
TemplateDeductionInfo Info(DeduceInit->getExprLoc());
TemplateDeductionResult Result =
DeduceAutoType(TSI->getTypeLoc(), DeduceInit, DeducedType, Info);
if (Result != TDK_Success && Result != TDK_AlreadyDiagnosed) {
if (Result != TemplateDeductionResult::Success &&
Result != TemplateDeductionResult::AlreadyDiagnosed) {
if (!IsInitCapture)
DiagnoseAutoDeductionFailure(VDecl, DeduceInit);
else if (isa<InitListExpr>(Init))
Expand Down
12 changes: 7 additions & 5 deletions clang/lib/Sema/SemaExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1554,12 +1554,13 @@ Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo,
TemplateDeductionInfo Info(Deduce->getExprLoc());
TemplateDeductionResult Result =
DeduceAutoType(TInfo->getTypeLoc(), Deduce, DeducedType, Info);
if (Result != TDK_Success && Result != TDK_AlreadyDiagnosed)
if (Result != TemplateDeductionResult::Success &&
Result != TemplateDeductionResult::AlreadyDiagnosed)
return ExprError(Diag(TyBeginLoc, diag::err_auto_expr_deduction_failure)
<< Ty << Deduce->getType() << FullRange
<< Deduce->getSourceRange());
if (DeducedType.isNull()) {
assert(Result == TDK_AlreadyDiagnosed);
assert(Result == TemplateDeductionResult::AlreadyDiagnosed);
return ExprError();
}

Expand Down Expand Up @@ -2098,12 +2099,13 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
TemplateDeductionInfo Info(Deduce->getExprLoc());
TemplateDeductionResult Result =
DeduceAutoType(AllocTypeInfo->getTypeLoc(), Deduce, DeducedType, Info);
if (Result != TDK_Success && Result != TDK_AlreadyDiagnosed)
if (Result != TemplateDeductionResult::Success &&
Result != TemplateDeductionResult::AlreadyDiagnosed)
return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure)
<< AllocType << Deduce->getType() << TypeRange
<< Deduce->getSourceRange());
if (DeducedType.isNull()) {
assert(Result == TDK_AlreadyDiagnosed);
assert(Result == TemplateDeductionResult::AlreadyDiagnosed);
return ExprError();
}
AllocType = DeducedType;
Expand Down Expand Up @@ -2883,7 +2885,7 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range,
// expected function type.
TemplateDeductionInfo Info(StartLoc);
if (DeduceTemplateArguments(FnTmpl, nullptr, ExpectedFunctionType, Fn,
Info))
Info) != TemplateDeductionResult::Success)
continue;
} else
Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl());
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Sema/SemaLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1200,8 +1200,8 @@ static bool LookupDirect(Sema &S, LookupResult &R, const DeclContext *DC) {
// Perform template argument deduction against the type that we would
// expect the function to have.
if (R.getSema().DeduceTemplateArguments(ConvTemplate, nullptr, ExpectedType,
Specialization, Info)
== Sema::TDK_Success) {
Specialization, Info) ==
TemplateDeductionResult::Success) {
R.addDecl(Specialization);
Found = true;
}
Expand Down

0 comments on commit dfbe2bc

Please sign in to comment.