Skip to content

Commit dfbe2bc

Browse files
authored
[clang][NFC] Refactor Sema::TemplateDeductionResult (llvm#81398)
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.
1 parent a8b4c11 commit dfbe2bc

File tree

10 files changed

+678
-606
lines changed

10 files changed

+678
-606
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,72 @@ class PreferredTypeBuilder {
353353
llvm::function_ref<QualType()> ComputeType;
354354
};
355355

356+
/// Describes the result of template argument deduction.
357+
///
358+
/// The TemplateDeductionResult enumeration describes the result of
359+
/// template argument deduction, as returned from
360+
/// DeduceTemplateArguments(). The separate TemplateDeductionInfo
361+
/// structure provides additional information about the results of
362+
/// template argument deduction, e.g., the deduced template argument
363+
/// list (if successful) or the specific template parameters or
364+
/// deduced arguments that were involved in the failure.
365+
enum class TemplateDeductionResult {
366+
/// Template argument deduction was successful.
367+
Success = 0,
368+
/// The declaration was invalid; do nothing.
369+
Invalid,
370+
/// Template argument deduction exceeded the maximum template
371+
/// instantiation depth (which has already been diagnosed).
372+
InstantiationDepth,
373+
/// Template argument deduction did not deduce a value
374+
/// for every template parameter.
375+
Incomplete,
376+
/// Template argument deduction did not deduce a value for every
377+
/// expansion of an expanded template parameter pack.
378+
IncompletePack,
379+
/// Template argument deduction produced inconsistent
380+
/// deduced values for the given template parameter.
381+
Inconsistent,
382+
/// Template argument deduction failed due to inconsistent
383+
/// cv-qualifiers on a template parameter type that would
384+
/// otherwise be deduced, e.g., we tried to deduce T in "const T"
385+
/// but were given a non-const "X".
386+
Underqualified,
387+
/// Substitution of the deduced template argument values
388+
/// resulted in an error.
389+
SubstitutionFailure,
390+
/// After substituting deduced template arguments, a dependent
391+
/// parameter type did not match the corresponding argument.
392+
DeducedMismatch,
393+
/// After substituting deduced template arguments, an element of
394+
/// a dependent parameter type did not match the corresponding element
395+
/// of the corresponding argument (when deducing from an initializer list).
396+
DeducedMismatchNested,
397+
/// A non-depnedent component of the parameter did not match the
398+
/// corresponding component of the argument.
399+
NonDeducedMismatch,
400+
/// When performing template argument deduction for a function
401+
/// template, there were too many call arguments.
402+
TooManyArguments,
403+
/// When performing template argument deduction for a function
404+
/// template, there were too few call arguments.
405+
TooFewArguments,
406+
/// The explicitly-specified template arguments were not valid
407+
/// template arguments for the given template.
408+
InvalidExplicitArguments,
409+
/// Checking non-dependent argument conversions failed.
410+
NonDependentConversionFailure,
411+
/// The deduced arguments did not satisfy the constraints associated
412+
/// with the template.
413+
ConstraintsNotSatisfied,
414+
/// Deduction failed; that's all we know.
415+
MiscellaneousDeductionFailure,
416+
/// CUDA Target attributes do not match.
417+
CUDATargetMismatch,
418+
/// Some error which was already diagnosed.
419+
AlreadyDiagnosed
420+
};
421+
356422
/// Sema - This implements semantic analysis and AST building for C.
357423
class Sema final {
358424
Sema(const Sema &) = delete;
@@ -9261,72 +9327,6 @@ class Sema final {
92619327
QualType adjustCCAndNoReturn(QualType ArgFunctionType, QualType FunctionType,
92629328
bool AdjustExceptionSpec = false);
92639329

9264-
/// Describes the result of template argument deduction.
9265-
///
9266-
/// The TemplateDeductionResult enumeration describes the result of
9267-
/// template argument deduction, as returned from
9268-
/// DeduceTemplateArguments(). The separate TemplateDeductionInfo
9269-
/// structure provides additional information about the results of
9270-
/// template argument deduction, e.g., the deduced template argument
9271-
/// list (if successful) or the specific template parameters or
9272-
/// deduced arguments that were involved in the failure.
9273-
enum TemplateDeductionResult {
9274-
/// Template argument deduction was successful.
9275-
TDK_Success = 0,
9276-
/// The declaration was invalid; do nothing.
9277-
TDK_Invalid,
9278-
/// Template argument deduction exceeded the maximum template
9279-
/// instantiation depth (which has already been diagnosed).
9280-
TDK_InstantiationDepth,
9281-
/// Template argument deduction did not deduce a value
9282-
/// for every template parameter.
9283-
TDK_Incomplete,
9284-
/// Template argument deduction did not deduce a value for every
9285-
/// expansion of an expanded template parameter pack.
9286-
TDK_IncompletePack,
9287-
/// Template argument deduction produced inconsistent
9288-
/// deduced values for the given template parameter.
9289-
TDK_Inconsistent,
9290-
/// Template argument deduction failed due to inconsistent
9291-
/// cv-qualifiers on a template parameter type that would
9292-
/// otherwise be deduced, e.g., we tried to deduce T in "const T"
9293-
/// but were given a non-const "X".
9294-
TDK_Underqualified,
9295-
/// Substitution of the deduced template argument values
9296-
/// resulted in an error.
9297-
TDK_SubstitutionFailure,
9298-
/// After substituting deduced template arguments, a dependent
9299-
/// parameter type did not match the corresponding argument.
9300-
TDK_DeducedMismatch,
9301-
/// After substituting deduced template arguments, an element of
9302-
/// a dependent parameter type did not match the corresponding element
9303-
/// of the corresponding argument (when deducing from an initializer list).
9304-
TDK_DeducedMismatchNested,
9305-
/// A non-depnedent component of the parameter did not match the
9306-
/// corresponding component of the argument.
9307-
TDK_NonDeducedMismatch,
9308-
/// When performing template argument deduction for a function
9309-
/// template, there were too many call arguments.
9310-
TDK_TooManyArguments,
9311-
/// When performing template argument deduction for a function
9312-
/// template, there were too few call arguments.
9313-
TDK_TooFewArguments,
9314-
/// The explicitly-specified template arguments were not valid
9315-
/// template arguments for the given template.
9316-
TDK_InvalidExplicitArguments,
9317-
/// Checking non-dependent argument conversions failed.
9318-
TDK_NonDependentConversionFailure,
9319-
/// The deduced arguments did not satisfy the constraints associated
9320-
/// with the template.
9321-
TDK_ConstraintsNotSatisfied,
9322-
/// Deduction failed; that's all we know.
9323-
TDK_MiscellaneousDeductionFailure,
9324-
/// CUDA Target attributes do not match.
9325-
TDK_CUDATargetMismatch,
9326-
/// Some error which was already diagnosed.
9327-
TDK_AlreadyDiagnosed
9328-
};
9329-
93309330
TemplateDeductionResult
93319331
DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
93329332
ArrayRef<TemplateArgument> TemplateArgs,
@@ -14444,7 +14444,7 @@ class Sema final {
1444414444
};
1444514445

1444614446
DeductionFailureInfo
14447-
MakeDeductionFailureInfo(ASTContext &Context, Sema::TemplateDeductionResult TDK,
14447+
MakeDeductionFailureInfo(ASTContext &Context, TemplateDeductionResult TDK,
1444814448
sema::TemplateDeductionInfo &Info);
1444914449

1445014450
/// Contains a late templated function.

clang/include/clang/Sema/TemplateDeduction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace clang {
3333
class Decl;
3434
struct DeducedPack;
3535
class Sema;
36+
enum class TemplateDeductionResult;
3637

3738
namespace sema {
3839

@@ -295,6 +296,10 @@ struct DeductionFailureInfo {
295296

296297
/// Free any memory associated with this deduction failure.
297298
void Destroy();
299+
300+
TemplateDeductionResult getResult() const {
301+
return static_cast<TemplateDeductionResult>(Result);
302+
}
298303
};
299304

300305
/// TemplateSpecCandidate - This is a generalization of OverloadCandidate

clang/lib/Sema/SemaDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13070,7 +13070,8 @@ QualType Sema::deduceVarTypeFromInitializer(VarDecl *VDecl,
1307013070
TemplateDeductionInfo Info(DeduceInit->getExprLoc());
1307113071
TemplateDeductionResult Result =
1307213072
DeduceAutoType(TSI->getTypeLoc(), DeduceInit, DeducedType, Info);
13073-
if (Result != TDK_Success && Result != TDK_AlreadyDiagnosed) {
13073+
if (Result != TemplateDeductionResult::Success &&
13074+
Result != TemplateDeductionResult::AlreadyDiagnosed) {
1307413075
if (!IsInitCapture)
1307513076
DiagnoseAutoDeductionFailure(VDecl, DeduceInit);
1307613077
else if (isa<InitListExpr>(Init))

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,12 +1554,13 @@ Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo,
15541554
TemplateDeductionInfo Info(Deduce->getExprLoc());
15551555
TemplateDeductionResult Result =
15561556
DeduceAutoType(TInfo->getTypeLoc(), Deduce, DeducedType, Info);
1557-
if (Result != TDK_Success && Result != TDK_AlreadyDiagnosed)
1557+
if (Result != TemplateDeductionResult::Success &&
1558+
Result != TemplateDeductionResult::AlreadyDiagnosed)
15581559
return ExprError(Diag(TyBeginLoc, diag::err_auto_expr_deduction_failure)
15591560
<< Ty << Deduce->getType() << FullRange
15601561
<< Deduce->getSourceRange());
15611562
if (DeducedType.isNull()) {
1562-
assert(Result == TDK_AlreadyDiagnosed);
1563+
assert(Result == TemplateDeductionResult::AlreadyDiagnosed);
15631564
return ExprError();
15641565
}
15651566

@@ -2098,12 +2099,13 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
20982099
TemplateDeductionInfo Info(Deduce->getExprLoc());
20992100
TemplateDeductionResult Result =
21002101
DeduceAutoType(AllocTypeInfo->getTypeLoc(), Deduce, DeducedType, Info);
2101-
if (Result != TDK_Success && Result != TDK_AlreadyDiagnosed)
2102+
if (Result != TemplateDeductionResult::Success &&
2103+
Result != TemplateDeductionResult::AlreadyDiagnosed)
21022104
return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure)
21032105
<< AllocType << Deduce->getType() << TypeRange
21042106
<< Deduce->getSourceRange());
21052107
if (DeducedType.isNull()) {
2106-
assert(Result == TDK_AlreadyDiagnosed);
2108+
assert(Result == TemplateDeductionResult::AlreadyDiagnosed);
21072109
return ExprError();
21082110
}
21092111
AllocType = DeducedType;
@@ -2883,7 +2885,7 @@ bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range,
28832885
// expected function type.
28842886
TemplateDeductionInfo Info(StartLoc);
28852887
if (DeduceTemplateArguments(FnTmpl, nullptr, ExpectedFunctionType, Fn,
2886-
Info))
2888+
Info) != TemplateDeductionResult::Success)
28872889
continue;
28882890
} else
28892891
Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl());

clang/lib/Sema/SemaLookup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,8 +1200,8 @@ static bool LookupDirect(Sema &S, LookupResult &R, const DeclContext *DC) {
12001200
// Perform template argument deduction against the type that we would
12011201
// expect the function to have.
12021202
if (R.getSema().DeduceTemplateArguments(ConvTemplate, nullptr, ExpectedType,
1203-
Specialization, Info)
1204-
== Sema::TDK_Success) {
1203+
Specialization, Info) ==
1204+
TemplateDeductionResult::Success) {
12051205
R.addDecl(Specialization);
12061206
Found = true;
12071207
}

0 commit comments

Comments
 (0)