Skip to content

Commit 5c24847

Browse files
committed
Revert "[clang] Track function template instantiation from definition (#112241)"
This reverts commit 07a0e2b. This change broke compiling Qt; see #112241 for details.
1 parent 956cfa6 commit 5c24847

File tree

11 files changed

+28
-154
lines changed

11 files changed

+28
-154
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ Bug Fixes to Attribute Support
128128
Bug Fixes to C++ Support
129129
^^^^^^^^^^^^^^^^^^^^^^^^
130130

131-
- Clang is now better at keeping track of friend function template instance contexts. (#GH55509)
132-
133131
Bug Fixes to AST Handling
134132
^^^^^^^^^^^^^^^^^^^^^^^^^
135133

clang/include/clang/AST/Decl.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,13 +2298,6 @@ class FunctionDecl : public DeclaratorDecl,
22982298
FunctionDeclBits.IsLateTemplateParsed = ILT;
22992299
}
23002300

2301-
bool isInstantiatedFromMemberTemplate() const {
2302-
return FunctionDeclBits.IsInstantiatedFromMemberTemplate;
2303-
}
2304-
void setInstantiatedFromMemberTemplate(bool Val = true) {
2305-
FunctionDeclBits.IsInstantiatedFromMemberTemplate = Val;
2306-
}
2307-
23082301
/// Whether this function is "trivial" in some specialized C++ senses.
23092302
/// Can only be true for default constructors, copy constructors,
23102303
/// copy assignment operators, and destructors. Not meaningful until

clang/include/clang/AST/DeclBase.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,8 +1777,6 @@ class DeclContext {
17771777
uint64_t HasImplicitReturnZero : 1;
17781778
LLVM_PREFERRED_TYPE(bool)
17791779
uint64_t IsLateTemplateParsed : 1;
1780-
LLVM_PREFERRED_TYPE(bool)
1781-
uint64_t IsInstantiatedFromMemberTemplate : 1;
17821780

17831781
/// Kind of contexpr specifier as defined by ConstexprSpecKind.
17841782
LLVM_PREFERRED_TYPE(ConstexprSpecKind)
@@ -1829,7 +1827,7 @@ class DeclContext {
18291827
};
18301828

18311829
/// Number of inherited and non-inherited bits in FunctionDeclBitfields.
1832-
enum { NumFunctionDeclBits = NumDeclContextBits + 32 };
1830+
enum { NumFunctionDeclBits = NumDeclContextBits + 31 };
18331831

18341832
/// Stores the bits used by CXXConstructorDecl. If modified
18351833
/// NumCXXConstructorDeclBits and the accessor
@@ -1840,12 +1838,12 @@ class DeclContext {
18401838
LLVM_PREFERRED_TYPE(FunctionDeclBitfields)
18411839
uint64_t : NumFunctionDeclBits;
18421840

1843-
/// 19 bits to fit in the remaining available space.
1841+
/// 20 bits to fit in the remaining available space.
18441842
/// Note that this makes CXXConstructorDeclBitfields take
18451843
/// exactly 64 bits and thus the width of NumCtorInitializers
18461844
/// will need to be shrunk if some bit is added to NumDeclContextBitfields,
18471845
/// NumFunctionDeclBitfields or CXXConstructorDeclBitfields.
1848-
uint64_t NumCtorInitializers : 16;
1846+
uint64_t NumCtorInitializers : 17;
18491847
LLVM_PREFERRED_TYPE(bool)
18501848
uint64_t IsInheritingConstructor : 1;
18511849

@@ -1859,7 +1857,7 @@ class DeclContext {
18591857
};
18601858

18611859
/// Number of inherited and non-inherited bits in CXXConstructorDeclBitfields.
1862-
enum { NumCXXConstructorDeclBits = NumFunctionDeclBits + 19 };
1860+
enum { NumCXXConstructorDeclBits = NumFunctionDeclBits + 20 };
18631861

18641862
/// Stores the bits used by ObjCMethodDecl.
18651863
/// If modified NumObjCMethodDeclBits and the accessor

clang/include/clang/AST/DeclTemplate.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,15 +1011,6 @@ class FunctionTemplateDecl : public RedeclarableTemplateDecl {
10111011
return getTemplatedDecl()->isThisDeclarationADefinition();
10121012
}
10131013

1014-
bool isCompatibleWithDefinition() const {
1015-
return getTemplatedDecl()->isInstantiatedFromMemberTemplate() ||
1016-
isThisDeclarationADefinition();
1017-
}
1018-
void setInstantiatedFromMemberTemplate(FunctionTemplateDecl *D) {
1019-
getTemplatedDecl()->setInstantiatedFromMemberTemplate();
1020-
RedeclarableTemplateDecl::setInstantiatedFromMemberTemplate(D);
1021-
}
1022-
10231014
/// Return the specialization with the provided arguments if it exists,
10241015
/// otherwise return the insertion point.
10251016
FunctionDecl *findSpecialization(ArrayRef<TemplateArgument> Args,

clang/lib/AST/Decl.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3065,7 +3065,6 @@ FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC,
30653065
FunctionDeclBits.IsIneligibleOrNotSelected = false;
30663066
FunctionDeclBits.HasImplicitReturnZero = false;
30673067
FunctionDeclBits.IsLateTemplateParsed = false;
3068-
FunctionDeclBits.IsInstantiatedFromMemberTemplate = false;
30693068
FunctionDeclBits.ConstexprKind = static_cast<uint64_t>(ConstexprKind);
30703069
FunctionDeclBits.BodyContainsImmediateEscalatingExpression = false;
30713070
FunctionDeclBits.InstantiationIsPending = false;

clang/lib/Sema/SemaTemplateDeduction.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4074,7 +4074,22 @@ TemplateDeductionResult Sema::FinishTemplateArgumentDeduction(
40744074
if (FunctionTemplate->getFriendObjectKind())
40754075
Owner = FunctionTemplate->getLexicalDeclContext();
40764076
FunctionDecl *FD = FunctionTemplate->getTemplatedDecl();
4077-
4077+
// additional check for inline friend,
4078+
// ```
4079+
// template <class F1> int foo(F1 X);
4080+
// template <int A1> struct A {
4081+
// template <class F1> friend int foo(F1 X) { return A1; }
4082+
// };
4083+
// template struct A<1>;
4084+
// int a = foo(1.0);
4085+
// ```
4086+
const FunctionDecl *FDFriend;
4087+
if (FD->getFriendObjectKind() == Decl::FriendObjectKind::FOK_None &&
4088+
FD->isDefined(FDFriend, /*CheckForPendingFriendDefinition*/ true) &&
4089+
FDFriend->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None) {
4090+
FD = const_cast<FunctionDecl *>(FDFriend);
4091+
Owner = FD->getLexicalDeclContext();
4092+
}
40784093
MultiLevelTemplateArgumentList SubstArgs(
40794094
FunctionTemplate, CanonicalDeducedArgumentList->asArray(),
40804095
/*Final=*/false);

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ MultiLevelTemplateArgumentList Sema::getTemplateInstantiationArgs(
479479
using namespace TemplateInstArgsHelpers;
480480
const Decl *CurDecl = ND;
481481

482+
if (!CurDecl)
483+
CurDecl = Decl::castFromDeclContext(DC);
484+
482485
if (Innermost) {
483486
Result.addOuterTemplateArguments(const_cast<NamedDecl *>(ND), *Innermost,
484487
Final);
@@ -492,10 +495,8 @@ MultiLevelTemplateArgumentList Sema::getTemplateInstantiationArgs(
492495
// has a depth of 0.
493496
if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(CurDecl))
494497
HandleDefaultTempArgIntoTempTempParam(TTP, Result);
495-
CurDecl = DC ? Decl::castFromDeclContext(DC)
496-
: Response::UseNextDecl(CurDecl).NextDecl;
497-
} else if (!CurDecl)
498-
CurDecl = Decl::castFromDeclContext(DC);
498+
CurDecl = Response::UseNextDecl(CurDecl).NextDecl;
499+
}
499500

500501
while (!CurDecl->isFileContextDecl()) {
501502
Response R;

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "TreeTransform.h"
1313
#include "clang/AST/ASTConsumer.h"
1414
#include "clang/AST/ASTContext.h"
15-
#include "clang/AST/ASTLambda.h"
1615
#include "clang/AST/ASTMutationListener.h"
1716
#include "clang/AST/DeclTemplate.h"
1817
#include "clang/AST/DependentDiagnostic.h"
@@ -5277,26 +5276,9 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
52775276
RebuildTypeSourceInfoForDefaultSpecialMembers();
52785277
SetDeclDefaulted(Function, PatternDecl->getLocation());
52795278
} else {
5280-
NamedDecl *ND = Function;
5281-
DeclContext *DC = ND->getLexicalDeclContext();
5282-
std::optional<ArrayRef<TemplateArgument>> Innermost;
5283-
if (auto *Primary = Function->getPrimaryTemplate();
5284-
Primary &&
5285-
!isGenericLambdaCallOperatorOrStaticInvokerSpecialization(Function) &&
5286-
Function->getTemplateSpecializationKind() !=
5287-
TSK_ExplicitSpecialization) {
5288-
auto It = llvm::find_if(Primary->redecls(),
5289-
[](const RedeclarableTemplateDecl *RTD) {
5290-
return cast<FunctionTemplateDecl>(RTD)
5291-
->isCompatibleWithDefinition();
5292-
});
5293-
assert(It != Primary->redecls().end() &&
5294-
"Should't get here without a definition");
5295-
DC = (*It)->getLexicalDeclContext();
5296-
Innermost.emplace(Function->getTemplateSpecializationArgs()->asArray());
5297-
}
52985279
MultiLevelTemplateArgumentList TemplateArgs = getTemplateInstantiationArgs(
5299-
Function, DC, /*Final=*/false, Innermost, false, PatternDecl);
5280+
Function, Function->getLexicalDeclContext(), /*Final=*/false,
5281+
/*Innermost=*/std::nullopt, false, PatternDecl);
53005282

53015283
// Substitute into the qualifier; we can get a substitution failure here
53025284
// through evil use of alias templates.

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,6 @@ void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
10641064
FD->setHasImplicitReturnZero(FunctionDeclBits.getNextBit());
10651065
FD->setIsMultiVersion(FunctionDeclBits.getNextBit());
10661066
FD->setLateTemplateParsed(FunctionDeclBits.getNextBit());
1067-
FD->setInstantiatedFromMemberTemplate(FunctionDeclBits.getNextBit());
10681067
FD->setFriendConstraintRefersToEnclosingTemplate(
10691068
FunctionDeclBits.getNextBit());
10701069
FD->setUsesSEHTry(FunctionDeclBits.getNextBit());

clang/lib/Serialization/ASTWriterDecl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
679679
}
680680

681681
void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
682-
static_assert(DeclContext::NumFunctionDeclBits == 45,
682+
static_assert(DeclContext::NumFunctionDeclBits == 44,
683683
"You need to update the serializer after you change the "
684684
"FunctionDeclBits");
685685

@@ -785,7 +785,6 @@ void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
785785
FunctionDeclBits.addBit(D->hasImplicitReturnZero());
786786
FunctionDeclBits.addBit(D->isMultiVersion());
787787
FunctionDeclBits.addBit(D->isLateTemplateParsed());
788-
FunctionDeclBits.addBit(D->isInstantiatedFromMemberTemplate());
789788
FunctionDeclBits.addBit(D->FriendConstraintRefersToEnclosingTemplate());
790789
FunctionDeclBits.addBit(D->usesSEHTry());
791790
Record.push_back(FunctionDeclBits);

clang/test/SemaTemplate/GH55509.cpp

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)