Skip to content

Commit abefb0c

Browse files
committed
WIP: [clang] Store in exprs the conveed arguments for function calls.
WIP - Not ready for review This keeps around in expressions the sugared converted arguments. This is a pre-requisite for adding some additional users of the resugaring transform. Differential Revision: https://reviews.llvm.org/D134115
1 parent 9d5d428 commit abefb0c

29 files changed

+578
-343
lines changed

clang/include/clang/AST/ASTImporter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,13 @@ class TypeSourceInfo;
489489
/// error.
490490
llvm::Expected<APValue> Import(const APValue &FromValue);
491491

492+
/// Import the given C++ TemplateArgumentList from the "from"
493+
/// context into the "to" context.
494+
///
495+
/// \returns The equivalent initializer in the "to" context, or the import
496+
/// error.
497+
llvm::Expected<TemplateArgumentList *> Import(const TemplateArgumentList *);
498+
492499
/// Import the definition of the given declaration, including all of
493500
/// the declarations it contains.
494501
[[nodiscard]] llvm::Error ImportDefinition(Decl *From);

clang/include/clang/AST/DeclTemplate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ class TemplateArgumentList final
265265

266266
/// Create a new template argument list that copies the given set of
267267
/// template arguments.
268-
static TemplateArgumentList *CreateCopy(ASTContext &Context,
268+
static TemplateArgumentList *CreateCopy(const ASTContext &Context,
269269
ArrayRef<TemplateArgument> Args);
270270

271271
/// Retrieve the template argument at a given index.

clang/include/clang/AST/Expr.h

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,8 @@ class DeclRefExpr final
12741274
/// The declaration that we are referencing.
12751275
ValueDecl *D;
12761276

1277+
const TemplateArgumentList *ConvertedArgs;
1278+
12771279
/// Provides source/type location info for the declaration name
12781280
/// embedded in D.
12791281
DeclarationNameLoc DNLoc;
@@ -1298,7 +1300,8 @@ class DeclRefExpr final
12981300
SourceLocation TemplateKWLoc, ValueDecl *D,
12991301
bool RefersToEnclosingVariableOrCapture,
13001302
const DeclarationNameInfo &NameInfo, NamedDecl *FoundD,
1301-
const TemplateArgumentListInfo *TemplateArgs, QualType T,
1303+
const TemplateArgumentListInfo *TemplateArgs,
1304+
const TemplateArgumentList *ConvertedArgs, QualType T,
13021305
ExprValueKind VK, NonOdrUseReason NOUR);
13031306

13041307
/// Construct an empty declaration reference expression.
@@ -1317,6 +1320,7 @@ class DeclRefExpr final
13171320
bool RefersToEnclosingVariableOrCapture, SourceLocation NameLoc,
13181321
QualType T, ExprValueKind VK, NamedDecl *FoundD = nullptr,
13191322
const TemplateArgumentListInfo *TemplateArgs = nullptr,
1323+
const TemplateArgumentList *ConvertedArgs = nullptr,
13201324
NonOdrUseReason NOUR = NOUR_None);
13211325

13221326
static DeclRefExpr *
@@ -1326,6 +1330,7 @@ class DeclRefExpr final
13261330
const DeclarationNameInfo &NameInfo, QualType T, ExprValueKind VK,
13271331
NamedDecl *FoundD = nullptr,
13281332
const TemplateArgumentListInfo *TemplateArgs = nullptr,
1333+
const TemplateArgumentList *ConvertedArgs = nullptr,
13291334
NonOdrUseReason NOUR = NOUR_None);
13301335

13311336
/// Construct an empty declaration reference expression.
@@ -1433,6 +1438,8 @@ class DeclRefExpr final
14331438
return getTrailingObjects<TemplateArgumentLoc>();
14341439
}
14351440

1441+
const TemplateArgumentList *getConvertedArgs() const { return ConvertedArgs; }
1442+
14361443
/// Retrieve the number of template arguments provided as part of this
14371444
/// template-id.
14381445
unsigned getNumTemplateArgs() const {
@@ -3248,6 +3255,8 @@ class MemberExpr final
32483255
/// In X.F, this is the decl referenced by F.
32493256
ValueDecl *MemberDecl;
32503257

3258+
const TemplateArgumentList *Deduced;
3259+
32513260
/// MemberDNLoc - Provides source/type location info for the
32523261
/// declaration name embedded in MemberDecl.
32533262
DeclarationNameLoc MemberDNLoc;
@@ -3277,21 +3286,21 @@ class MemberExpr final
32773286
NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
32783287
ValueDecl *MemberDecl, DeclAccessPair FoundDecl,
32793288
const DeclarationNameInfo &NameInfo,
3280-
const TemplateArgumentListInfo *TemplateArgs, QualType T,
3281-
ExprValueKind VK, ExprObjectKind OK, NonOdrUseReason NOUR);
3289+
const TemplateArgumentListInfo *TemplateArgs,
3290+
const TemplateArgumentList *Deduced, QualType T, ExprValueKind VK,
3291+
ExprObjectKind OK, NonOdrUseReason NOUR);
32823292
MemberExpr(EmptyShell Empty)
32833293
: Expr(MemberExprClass, Empty), Base(), MemberDecl() {}
32843294

32853295
public:
3286-
static MemberExpr *Create(const ASTContext &C, Expr *Base, bool IsArrow,
3287-
SourceLocation OperatorLoc,
3288-
NestedNameSpecifierLoc QualifierLoc,
3289-
SourceLocation TemplateKWLoc, ValueDecl *MemberDecl,
3290-
DeclAccessPair FoundDecl,
3291-
DeclarationNameInfo MemberNameInfo,
3292-
const TemplateArgumentListInfo *TemplateArgs,
3293-
QualType T, ExprValueKind VK, ExprObjectKind OK,
3294-
NonOdrUseReason NOUR);
3296+
static MemberExpr *
3297+
Create(const ASTContext &C, Expr *Base, bool IsArrow,
3298+
SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
3299+
SourceLocation TemplateKWLoc, ValueDecl *MemberDecl,
3300+
DeclAccessPair FoundDecl, DeclarationNameInfo MemberNameInfo,
3301+
const TemplateArgumentListInfo *TemplateArgs,
3302+
const TemplateArgumentList *Deduced, QualType T, ExprValueKind VK,
3303+
ExprObjectKind OK, NonOdrUseReason NOUR);
32953304

32963305
/// Create an implicit MemberExpr, with no location, qualifier, template
32973306
/// arguments, and so on. Suitable only for non-static member access.
@@ -3302,7 +3311,8 @@ class MemberExpr final
33023311
return Create(C, Base, IsArrow, SourceLocation(), NestedNameSpecifierLoc(),
33033312
SourceLocation(), MemberDecl,
33043313
DeclAccessPair::make(MemberDecl, MemberDecl->getAccess()),
3305-
DeclarationNameInfo(), nullptr, T, VK, OK, NOUR_None);
3314+
DeclarationNameInfo(), nullptr, /*Deduced=*/{}, T, VK, OK,
3315+
NOUR_None);
33063316
}
33073317

33083318
static MemberExpr *CreateEmpty(const ASTContext &Context, bool HasQualifier,
@@ -3328,6 +3338,8 @@ class MemberExpr final
33283338
return *getTrailingObjects<DeclAccessPair>();
33293339
}
33303340

3341+
const TemplateArgumentList *getDeduced() const { return Deduced; }
3342+
33313343
/// Determines whether this member expression actually had
33323344
/// a C++ nested-name-specifier prior to the name of the member, e.g.,
33333345
/// x->Base::foo.

clang/include/clang/Sema/Initialization.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ class InitializationSequence {
957957
bool HadMultipleCandidates;
958958
FunctionDecl *Function;
959959
DeclAccessPair FoundDecl;
960+
const TemplateArgumentList *ConvertedArgs;
960961
};
961962

962963
union {
@@ -1262,9 +1263,10 @@ class InitializationSequence {
12621263
///
12631264
/// \param Function the function to which the overloaded function reference
12641265
/// resolves.
1265-
void AddAddressOverloadResolutionStep(FunctionDecl *Function,
1266-
DeclAccessPair Found,
1267-
bool HadMultipleCandidates);
1266+
void
1267+
AddAddressOverloadResolutionStep(FunctionDecl *Function, DeclAccessPair Found,
1268+
const TemplateArgumentList *ConvertedArgs,
1269+
bool HadMultipleCandidates);
12681270

12691271
/// Add a new step in the initialization that performs a derived-to-
12701272
/// base cast.

clang/include/clang/Sema/Overload.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,9 @@ class Sema;
961961
StandardConversionSequence FinalConversion;
962962
};
963963

964+
/// Deduced Arguments for Function Templates.
965+
const TemplateArgumentList *Deduced;
966+
964967
/// Get RewriteKind value in OverloadCandidateRewriteKind type (This
965968
/// function is to workaround the spurious GCC bitfield enum warning)
966969
OverloadCandidateRewriteKind getRewriteKind() const {

clang/include/clang/Sema/Sema.h

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6856,7 +6856,8 @@ class Sema final : public SemaBase {
68566856
const CXXScopeSpec *SS = nullptr,
68576857
NamedDecl *FoundD = nullptr,
68586858
SourceLocation TemplateKWLoc = SourceLocation(),
6859-
const TemplateArgumentListInfo *TemplateArgs = nullptr);
6859+
const TemplateArgumentListInfo *TemplateArgs = nullptr,
6860+
const TemplateArgumentList *ConvertArgs = nullptr);
68606861

68616862
/// BuildDeclRefExpr - Build an expression that references a
68626863
/// declaration that does not require a closure capture.
@@ -6865,7 +6866,8 @@ class Sema final : public SemaBase {
68656866
const DeclarationNameInfo &NameInfo,
68666867
NestedNameSpecifierLoc NNS, NamedDecl *FoundD = nullptr,
68676868
SourceLocation TemplateKWLoc = SourceLocation(),
6868-
const TemplateArgumentListInfo *TemplateArgs = nullptr);
6869+
const TemplateArgumentListInfo *TemplateArgs = nullptr,
6870+
const TemplateArgumentList *ConvertArgs = nullptr);
68696871

68706872
bool UseArgumentDependentLookup(const CXXScopeSpec &SS, const LookupResult &R,
68716873
bool HasTrailingLParen);
@@ -6887,6 +6889,7 @@ class Sema final : public SemaBase {
68876889
const CXXScopeSpec &SS, const DeclarationNameInfo &NameInfo, NamedDecl *D,
68886890
NamedDecl *FoundD = nullptr,
68896891
const TemplateArgumentListInfo *TemplateArgs = nullptr,
6892+
const TemplateArgumentList *ConvertedArgs = nullptr,
68906893
bool AcceptInvalidDecl = false);
68916894

68926895
// ExpandFunctionLocalPredefinedMacros - Returns a new vector of Tokens,
@@ -8700,14 +8703,13 @@ class Sema final : public SemaBase {
87008703
SourceLocation TemplateKWLoc,
87018704
UnqualifiedId &Member, Decl *ObjCImpDecl);
87028705

8703-
MemberExpr *
8704-
BuildMemberExpr(Expr *Base, bool IsArrow, SourceLocation OpLoc,
8705-
NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc,
8706-
ValueDecl *Member, DeclAccessPair FoundDecl,
8707-
bool HadMultipleCandidates,
8708-
const DeclarationNameInfo &MemberNameInfo, QualType Ty,
8709-
ExprValueKind VK, ExprObjectKind OK,
8710-
const TemplateArgumentListInfo *TemplateArgs = nullptr);
8706+
MemberExpr *BuildMemberExpr(
8707+
Expr *Base, bool IsArrow, SourceLocation OpLoc,
8708+
NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc,
8709+
ValueDecl *Member, DeclAccessPair FoundDecl, bool HadMultipleCandidates,
8710+
const DeclarationNameInfo &MemberNameInfo, QualType Ty, ExprValueKind VK,
8711+
ExprObjectKind OK, const TemplateArgumentListInfo *TemplateArgs = nullptr,
8712+
const TemplateArgumentList *Deduced = nullptr);
87118713

87128714
// Check whether the declarations we found through a nested-name
87138715
// specifier in a member expression are actually members of the base
@@ -10296,6 +10298,7 @@ class Sema final : public SemaBase {
1029610298
ADLCallKind IsADLCandidate = ADLCallKind::NotADL,
1029710299
ConversionSequenceList EarlyConversions = {},
1029810300
OverloadCandidateParamOrder PO = {},
10301+
const TemplateArgumentList *Deduced = nullptr,
1029910302
bool AggregateCandidateDeduction = false, bool StrictPackMatch = false);
1030010303

1030110304
/// Add all of the function declarations in the given function set to
@@ -10332,6 +10335,7 @@ class Sema final : public SemaBase {
1033210335
bool PartialOverloading = false,
1033310336
ConversionSequenceList EarlyConversions = {},
1033410337
OverloadCandidateParamOrder PO = {},
10338+
const TemplateArgumentList *Deduced = nullptr,
1033510339
bool StrictPackMatch = false);
1033610340

1033710341
/// Add a C++ member function template as a candidate to the candidate
@@ -10540,6 +10544,7 @@ class Sema final : public SemaBase {
1054010544
FunctionDecl *
1054110545
ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, QualType TargetType,
1054210546
bool Complain, DeclAccessPair &Found,
10547+
const TemplateArgumentList *&ConvertedArgs,
1054310548
bool *pHadMultipleCandidates = nullptr);
1054410549

1054510550
/// Given an expression that refers to an overloaded function, try to
@@ -10573,7 +10578,9 @@ class Sema final : public SemaBase {
1057310578
/// If no template-ids are found, no diagnostics are emitted and NULL is
1057410579
/// returned.
1057510580
FunctionDecl *ResolveSingleFunctionTemplateSpecialization(
10576-
OverloadExpr *ovl, bool Complain = false, DeclAccessPair *Found = nullptr,
10581+
OverloadExpr *ovl, TemplateArgumentListInfo &ExplicitTemplateArgs,
10582+
const TemplateArgumentList *&ConvertedArgs, bool Complain = false,
10583+
DeclAccessPair *Found = nullptr,
1057710584
TemplateSpecCandidateSet *FailedTSC = nullptr,
1057810585
bool ForTypeDeduction = false);
1057910586

@@ -10758,11 +10765,14 @@ class Sema final : public SemaBase {
1075810765
/// perhaps a '&' around it). We have resolved the overloaded function
1075910766
/// to the function declaration Fn, so patch up the expression E to
1076010767
/// refer (possibly indirectly) to Fn. Returns the new expr.
10761-
ExprResult FixOverloadedFunctionReference(Expr *E, DeclAccessPair FoundDecl,
10762-
FunctionDecl *Fn);
10763-
ExprResult FixOverloadedFunctionReference(ExprResult,
10764-
DeclAccessPair FoundDecl,
10765-
FunctionDecl *Fn);
10768+
ExprResult
10769+
FixOverloadedFunctionReference(Expr *E, DeclAccessPair FoundDecl,
10770+
FunctionDecl *Fn,
10771+
const TemplateArgumentList *Deduced);
10772+
ExprResult
10773+
FixOverloadedFunctionReference(ExprResult, DeclAccessPair FoundDecl,
10774+
FunctionDecl *Fn,
10775+
const TemplateArgumentList *Deduced);
1076610776

1076710777
/// - Returns a selector which best matches given argument list or
1076810778
/// nullptr if none could be found
@@ -11542,7 +11552,8 @@ class Sema final : public SemaBase {
1154211552
DeclResult CheckVarTemplateId(VarTemplateDecl *Template,
1154311553
SourceLocation TemplateLoc,
1154411554
SourceLocation TemplateNameLoc,
11545-
const TemplateArgumentListInfo &TemplateArgs);
11555+
const TemplateArgumentListInfo &TemplateArgs,
11556+
const TemplateArgumentList *&ConvertedArgs);
1154611557

1154711558
/// Form a reference to the specialization of the given variable template
1154811559
/// corresponding to the specified argument list, or a null-but-valid result

clang/include/clang/Sema/SemaCUDA.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ class SemaCUDA : public SemaBase {
229229
/// calling priority.
230230
void EraseUnwantedMatches(
231231
const FunctionDecl *Caller,
232-
llvm::SmallVectorImpl<std::pair<DeclAccessPair, FunctionDecl *>>
233-
&Matches);
232+
llvm::SmallVectorImpl<std::tuple<DeclAccessPair, FunctionDecl *,
233+
const TemplateArgumentList *>> &Matches);
234234

235235
/// Given a implicit special member, infer its CUDA target from the
236236
/// calls it needs to make to underlying base/field special members.

clang/lib/AST/ASTImporter.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7493,6 +7493,7 @@ ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
74937493
auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc());
74947494
auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc());
74957495
auto ToDecl = importChecked(Err, E->getDecl());
7496+
auto ToConvertedArgs = importChecked(Err, E->getConvertedArgs());
74967497
auto ToLocation = importChecked(Err, E->getLocation());
74977498
auto ToType = importChecked(Err, E->getType());
74987499
if (Err)
@@ -7519,7 +7520,8 @@ ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
75197520
auto *ToE = DeclRefExpr::Create(
75207521
Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
75217522
E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
7522-
E->getValueKind(), ToFoundD, ToResInfo, E->isNonOdrUse());
7523+
E->getValueKind(), ToFoundD, ToResInfo, ToConvertedArgs,
7524+
E->isNonOdrUse());
75237525
if (E->hadMultipleCandidates())
75247526
ToE->setHadMultipleCandidates(true);
75257527
ToE->setIsImmediateEscalating(E->isImmediateEscalating());
@@ -8444,6 +8446,7 @@ ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
84448446
auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc());
84458447
auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc());
84468448
auto ToMemberDecl = importChecked(Err, E->getMemberDecl());
8449+
auto ToDeduced = importChecked(Err, E->getDeduced());
84478450
auto ToType = importChecked(Err, E->getType());
84488451
auto ToDecl = importChecked(Err, E->getFoundDecl().getDecl());
84498452
auto ToName = importChecked(Err, E->getMemberNameInfo().getName());
@@ -8468,7 +8471,7 @@ ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
84688471
return MemberExpr::Create(Importer.getToContext(), ToBase, E->isArrow(),
84698472
ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
84708473
ToMemberDecl, ToFoundDecl, ToMemberNameInfo,
8471-
ResInfo, ToType, E->getValueKind(),
8474+
ResInfo, ToDeduced, ToType, E->getValueKind(),
84728475
E->getObjectKind(), E->isNonOdrUse());
84738476
}
84748477

@@ -10174,6 +10177,17 @@ llvm::Expected<APValue> ASTImporter::Import(const APValue &FromValue) {
1017410177
return Importer.ImportAPValue(FromValue);
1017510178
}
1017610179

10180+
llvm::Expected<TemplateArgumentList *>
10181+
ASTImporter::Import(const TemplateArgumentList *ArgList) {
10182+
ASTNodeImporter Importer(*this);
10183+
if (!ArgList)
10184+
return nullptr;
10185+
SmallVector<TemplateArgument, 4> ToArgs(ArgList->size());
10186+
if (auto Res = Importer.ImportTemplateArguments(ArgList->asArray(), ToArgs))
10187+
return std::move(Res);
10188+
return TemplateArgumentList::CreateCopy(ToContext, ToArgs);
10189+
}
10190+
1017710191
Error ASTImporter::ImportDefinition(Decl *From) {
1017810192
ExpectedDecl ToOrErr = Import(From);
1017910193
if (!ToOrErr)

clang/lib/AST/DeclTemplate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ TemplateArgumentList::TemplateArgumentList(ArrayRef<TemplateArgument> Args)
950950
}
951951

952952
TemplateArgumentList *
953-
TemplateArgumentList::CreateCopy(ASTContext &Context,
953+
TemplateArgumentList::CreateCopy(const ASTContext &Context,
954954
ArrayRef<TemplateArgument> Args) {
955955
void *Mem = Context.Allocate(totalSizeToAlloc<TemplateArgument>(Args.size()));
956956
return new (Mem) TemplateArgumentList(Args);

0 commit comments

Comments
 (0)