Skip to content

Commit

Permalink
Rename RAII objects for performing eager instantiation to have names …
Browse files Browse the repository at this point in the history
…that

describe what they're for, not how they do it, and factor out a bit more
common code into them.

llvm-svn: 303479
  • Loading branch information
zygoloid committed May 20, 2017
1 parent a451953 commit 4f3e381
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 62 deletions.
23 changes: 16 additions & 7 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -7383,17 +7383,24 @@ class Sema {
/// but have not yet been performed.
std::deque<PendingImplicitInstantiation> PendingInstantiations;

class SavePendingInstantiationsAndVTableUsesRAII {
class GlobalEagerInstantiationScope {
public:
SavePendingInstantiationsAndVTableUsesRAII(Sema &S, bool Enabled)
GlobalEagerInstantiationScope(Sema &S, bool Enabled)
: S(S), Enabled(Enabled) {
if (!Enabled) return;

SavedPendingInstantiations.swap(S.PendingInstantiations);
SavedVTableUses.swap(S.VTableUses);
}

~SavePendingInstantiationsAndVTableUsesRAII() {
void perform() {
if (Enabled) {
S.DefineUsedVTables();
S.PerformPendingInstantiations();
}
}

~GlobalEagerInstantiationScope() {
if (!Enabled) return;

// Restore the set of pending vtables.
Expand Down Expand Up @@ -7423,14 +7430,16 @@ class Sema {
/// types, static variables, enumerators, etc.
std::deque<PendingImplicitInstantiation> PendingLocalImplicitInstantiations;

class SavePendingLocalImplicitInstantiationsRAII {
class LocalEagerInstantiationScope {
public:
SavePendingLocalImplicitInstantiationsRAII(Sema &S): S(S) {
LocalEagerInstantiationScope(Sema &S) : S(S) {
SavedPendingLocalImplicitInstantiations.swap(
S.PendingLocalImplicitInstantiations);
}

~SavePendingLocalImplicitInstantiationsRAII() {
void perform() { S.PerformPendingInstantiations(/*LocalOnly=*/true); }

~LocalEagerInstantiationScope() {
assert(S.PendingLocalImplicitInstantiations.empty() &&
"there shouldn't be any pending local implicit instantiations");
SavedPendingLocalImplicitInstantiations.swap(
Expand All @@ -7440,7 +7449,7 @@ class Sema {
private:
Sema &S;
std::deque<PendingImplicitInstantiation>
SavedPendingLocalImplicitInstantiations;
SavedPendingLocalImplicitInstantiations;
};

/// A helper class for building up ExtParameterInfos.
Expand Down
73 changes: 18 additions & 55 deletions clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1540,8 +1540,7 @@ Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
// DR1484 clarifies that the members of a local class are instantiated as part
// of the instantiation of their enclosing entity.
if (D->isCompleteDefinition() && D->isLocalClass()) {
Sema::SavePendingLocalImplicitInstantiationsRAII
SavedPendingLocalImplicitInstantiations(SemaRef);
Sema::LocalEagerInstantiationScope LocalInstantiations(SemaRef);

SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs,
TSK_ImplicitInstantiation,
Expand All @@ -1555,7 +1554,7 @@ Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {

// This class may have local implicit instantiations that need to be
// performed within this scope.
SemaRef.PerformPendingInstantiations(/*LocalOnly=*/true);
LocalInstantiations.perform();
}

SemaRef.DiagnoseUnusedNestedTypedefs(Record);
Expand Down Expand Up @@ -3812,10 +3811,9 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
// while we're still within our own instantiation context.
// This has to happen before LateTemplateParser below is called, so that
// it marks vtables used in late parsed templates as used.
SavePendingLocalImplicitInstantiationsRAII
SavedPendingLocalImplicitInstantiations(*this);
SavePendingInstantiationsAndVTableUsesRAII
SavePendingInstantiationsAndVTableUses(*this, /*Enabled=*/Recursive);
GlobalEagerInstantiationScope GlobalInstantiations(*this,
/*Enabled=*/Recursive);
LocalEagerInstantiationScope LocalInstantiations(*this);

// Call the LateTemplateParser callback if there is a need to late parse
// a templated function definition.
Expand Down Expand Up @@ -3942,20 +3940,9 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,

// This class may have local implicit instantiations that need to be
// instantiation within this scope.
PerformPendingInstantiations(/*LocalOnly=*/true);
LocalInstantiations.perform();
Scope.Exit();

if (Recursive) {
// Define any pending vtables.
DefineUsedVTables();

// Instantiate any pending implicit instantiations found during the
// instantiation of this template.
PerformPendingInstantiations();

// PendingInstantiations and VTableUses are restored through
// SavePendingInstantiationsAndVTableUses's destructor.
}
GlobalInstantiations.perform();
}

VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation(
Expand Down Expand Up @@ -4287,10 +4274,10 @@ void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,
// If we're performing recursive template instantiation, create our own
// queue of pending implicit instantiations that we will instantiate
// later, while we're still within our own instantiation context.
SavePendingInstantiationsAndVTableUsesRAII
SavePendingInstantiationsAndVTableUses(*this, /*Enabled=*/Recursive);

GlobalEagerInstantiationScope GlobalInstantiations(*this,
/*Enabled=*/Recursive);
LocalInstantiationScope Local(*this);
LocalEagerInstantiationScope LocalInstantiations(*this);

// Enter the scope of this instantiation. We don't use
// PushDeclContext because we don't have a scope.
Expand All @@ -4303,21 +4290,9 @@ void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,

// This variable may have local implicit instantiations that need to be
// instantiated within this scope.
PerformPendingInstantiations(/*LocalOnly=*/true);

LocalInstantiations.perform();
Local.Exit();

if (Recursive) {
// Define any newly required vtables.
DefineUsedVTables();

// Instantiate any pending implicit instantiations found during the
// instantiation of this template.
PerformPendingInstantiations();

// PendingInstantiations and VTableUses are restored through
// SavePendingInstantiationsAndVTableUses's destructor.
}
GlobalInstantiations.perform();
}

// Find actual definition
Expand Down Expand Up @@ -4408,16 +4383,16 @@ void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,
// If we're performing recursive template instantiation, create our own
// queue of pending implicit instantiations that we will instantiate later,
// while we're still within our own instantiation context.
SavePendingLocalImplicitInstantiationsRAII
SavedPendingLocalImplicitInstantiations(*this);
SavePendingInstantiationsAndVTableUsesRAII
SavePendingInstantiationsAndVTableUses(*this, /*Enabled=*/Recursive);
GlobalEagerInstantiationScope GlobalInstantiations(*this,
/*Enabled=*/Recursive);

// Enter the scope of this instantiation. We don't use
// PushDeclContext because we don't have a scope.
ContextRAII PreviousContext(*this, Var->getDeclContext());
LocalInstantiationScope Local(*this);

LocalEagerInstantiationScope LocalInstantiations(*this);

VarDecl *OldVar = Var;
if (Def->isStaticDataMember() && !Def->isOutOfLine()) {
// We're instantiating an inline static data member whose definition was
Expand Down Expand Up @@ -4470,21 +4445,9 @@ void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,

// This variable may have local implicit instantiations that need to be
// instantiated within this scope.
PerformPendingInstantiations(/*LocalOnly=*/true);

LocalInstantiations.perform();
Local.Exit();

if (Recursive) {
// Define any newly required vtables.
DefineUsedVTables();

// Instantiate any pending implicit instantiations found during the
// instantiation of this template.
PerformPendingInstantiations();

// PendingInstantiations and VTableUses are restored through
// SavePendingInstantiationsAndVTableUses's destructor.
}
GlobalInstantiations.perform();
}

void
Expand Down

0 comments on commit 4f3e381

Please sign in to comment.