Implement support for parsing pseudo-destructor expression with a nested-name-specifier, e.g.,
typedef int Int;
int *p;
p->Int::~Int();
This weakens the invariant that the only types in nested-name-specifiers are tag types (restricted to class types in C++98/03). However, we weaken this invariant as little as possible, accepting arbitrary types in nested-name-specifiers only when we're in a member access expression that looks like a pseudo-destructor expression.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@96743 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index 25ca3d1..4ea3793 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -2198,7 +2198,8 @@
virtual CXXScopeTy *ActOnCXXGlobalScopeSpecifier(Scope *S,
SourceLocation CCLoc);
- bool isAcceptableNestedNameSpecifier(NamedDecl *SD);
+ bool isAcceptableNestedNameSpecifier(NamedDecl *SD,
+ bool MayBePseudoDestructor = false);
NamedDecl *FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS);
@@ -2207,6 +2208,7 @@
SourceLocation IdLoc,
SourceLocation CCLoc,
IdentifierInfo &II,
+ bool MayBePseudoDestructor,
QualType ObjectType,
NamedDecl *ScopeLookupResult,
bool EnteringContext,
@@ -2217,12 +2219,14 @@
SourceLocation IdLoc,
SourceLocation CCLoc,
IdentifierInfo &II,
+ bool MayBePseudoDestructor,
TypeTy *ObjectType,
bool EnteringContext);
virtual bool IsInvalidUnlessNestedName(Scope *S,
const CXXScopeSpec &SS,
IdentifierInfo &II,
+ bool MayBePseudoDestructor,
TypeTy *ObjectType,
bool EnteringContext);
@@ -2238,7 +2242,8 @@
const CXXScopeSpec &SS,
TypeTy *Type,
SourceRange TypeRange,
- SourceLocation CCLoc);
+ SourceLocation CCLoc,
+ bool MayBePseudoDestructor);
virtual bool ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS);
diff --git a/lib/Sema/SemaCXXScopeSpec.cpp b/lib/Sema/SemaCXXScopeSpec.cpp
index 52e9e9b..315938d 100644
--- a/lib/Sema/SemaCXXScopeSpec.cpp
+++ b/lib/Sema/SemaCXXScopeSpec.cpp
@@ -176,16 +176,15 @@
case NestedNameSpecifier::TypeSpec:
case NestedNameSpecifier::TypeSpecWithTemplate: {
- const TagType *Tag = NNS->getAsType()->getAs<TagType>();
- assert(Tag && "Non-tag type in nested-name-specifier");
- return Tag->getDecl();
- } break;
+ if (const TagType *Tag = NNS->getAsType()->getAs<TagType>())
+ return Tag->getDecl();
+ break;
+ }
case NestedNameSpecifier::Global:
return Context.getTranslationUnitDecl();
}
- // Required to silence a GCC warning.
return 0;
}
@@ -270,7 +269,8 @@
/// \brief Determines whether the given declaration is an valid acceptable
/// result for name lookup of a nested-name-specifier.
-bool Sema::isAcceptableNestedNameSpecifier(NamedDecl *SD) {
+bool Sema::isAcceptableNestedNameSpecifier(NamedDecl *SD,
+ bool MayBePseudoDestructor) {
if (!SD)
return false;
@@ -281,6 +281,11 @@
if (!isa<TypeDecl>(SD))
return false;
+ // If this may be part of a pseudo-destructor expression, we'll
+ // accept any type.
+ if (MayBePseudoDestructor)
+ return true;
+
// Determine whether we have a class (or, in C++0x, an enum) or
// a typedef thereof. If so, build the nested-name-specifier.
QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
@@ -321,7 +326,7 @@
return 0;
NamedDecl *Result = Found.getFoundDecl();
- if (isAcceptableNestedNameSpecifier(Result))
+ if (isAcceptableNestedNameSpecifier(Result, true))
return Result;
return 0;
@@ -345,6 +350,7 @@
SourceLocation IdLoc,
SourceLocation CCLoc,
IdentifierInfo &II,
+ bool MayBePseudoDestructor,
QualType ObjectType,
NamedDecl *ScopeLookupResult,
bool EnteringContext,
@@ -439,7 +445,8 @@
DeclarationName Name = Found.getLookupName();
if (CorrectTypo(Found, S, &SS, LookupCtx, EnteringContext) &&
Found.isSingleResult() &&
- isAcceptableNestedNameSpecifier(Found.getAsSingle<NamedDecl>())) {
+ isAcceptableNestedNameSpecifier(Found.getAsSingle<NamedDecl>(),
+ MayBePseudoDestructor)) {
if (LookupCtx)
Diag(Found.getNameLoc(), diag::err_no_member_suggest)
<< Name << LookupCtx << Found.getLookupName() << SS.getRange()
@@ -459,7 +466,7 @@
}
NamedDecl *SD = Found.getAsSingle<NamedDecl>();
- if (isAcceptableNestedNameSpecifier(SD)) {
+ if (isAcceptableNestedNameSpecifier(SD, MayBePseudoDestructor)) {
if (!ObjectType.isNull() && !ObjectTypeSearchedInScope) {
// C++ [basic.lookup.classref]p4:
// [...] If the name is found in both contexts, the
@@ -471,13 +478,14 @@
// scope, reconstruct the result from the template instantiation itself.
NamedDecl *OuterDecl;
if (S) {
- LookupResult FoundOuter(*this, &II, IdLoc, LookupNestedNameSpecifierName);
+ LookupResult FoundOuter(*this, &II, IdLoc,
+ LookupNestedNameSpecifierName);
LookupName(FoundOuter, S);
OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
} else
OuterDecl = ScopeLookupResult;
- if (isAcceptableNestedNameSpecifier(OuterDecl) &&
+ if (isAcceptableNestedNameSpecifier(OuterDecl, MayBePseudoDestructor) &&
OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
(!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
!Context.hasSameType(
@@ -554,9 +562,11 @@
SourceLocation IdLoc,
SourceLocation CCLoc,
IdentifierInfo &II,
+ bool MayBePseudoDestructor,
TypeTy *ObjectTypePtr,
bool EnteringContext) {
return BuildCXXNestedNameSpecifier(S, SS, IdLoc, CCLoc, II,
+ MayBePseudoDestructor,
QualType::getFromOpaquePtr(ObjectTypePtr),
/*ScopeLookupResult=*/0, EnteringContext,
false);
@@ -569,10 +579,13 @@
///
/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
bool Sema::IsInvalidUnlessNestedName(Scope *S, const CXXScopeSpec &SS,
- IdentifierInfo &II, TypeTy *ObjectType,
+ IdentifierInfo &II,
+ bool MayBePseudoDestructor,
+ TypeTy *ObjectType,
bool EnteringContext) {
return BuildCXXNestedNameSpecifier(S, SS, SourceLocation(), SourceLocation(),
- II, QualType::getFromOpaquePtr(ObjectType),
+ II, MayBePseudoDestructor,
+ QualType::getFromOpaquePtr(ObjectType),
/*ScopeLookupResult=*/0, EnteringContext,
true);
}
@@ -581,7 +594,8 @@
const CXXScopeSpec &SS,
TypeTy *Ty,
SourceRange TypeRange,
- SourceLocation CCLoc) {
+ SourceLocation CCLoc,
+ bool MayBePseudoDestructor) {
NestedNameSpecifier *Prefix
= static_cast<NestedNameSpecifier *>(SS.getScopeRep());
QualType T = GetTypeFromParser(Ty);
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index 4cb58d8..7e32f17 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -85,10 +85,11 @@
isDependent = SearchType->isDependentType();
} else {
LookupCtx = computeDeclContext(SS, EnteringContext);
- isDependent = LookupCtx->isDependentContext();
+ if (LookupCtx)
+ isDependent = LookupCtx->isDependentContext();
}
- LookInScope = false;
+ LookInScope = (LookupCtx == 0) && !isDependent;
} else if (ObjectTypePtr) {
// C++ [basic.lookup.classref]p3:
// If the unqualified-id is ~type-name, the type-name is looked up
diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp
index c98f88f..0d5c50a 100644
--- a/lib/Sema/SemaTemplate.cpp
+++ b/lib/Sema/SemaTemplate.cpp
@@ -4925,6 +4925,7 @@
NestedNameSpecifier *NNS
= TransformNestedNameSpecifier(T->getQualifier(),
/*FIXME:*/SourceRange(getBaseLocation()),
+ false,
ObjectType);
if (!NNS)
return QualType();
diff --git a/lib/Sema/SemaTemplateInstantiate.cpp b/lib/Sema/SemaTemplateInstantiate.cpp
index f12c559..b6ddc46 100644
--- a/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1486,7 +1486,7 @@
const MultiLevelTemplateArgumentList &TemplateArgs) {
TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
DeclarationName());
- return Instantiator.TransformNestedNameSpecifier(NNS, Range);
+ return Instantiator.TransformNestedNameSpecifier(NNS, Range, false);
}
TemplateName
diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h
index a596086..11c19eb 100644
--- a/lib/Sema/TreeTransform.h
+++ b/lib/Sema/TreeTransform.h
@@ -265,6 +265,7 @@
/// alternate behavior.
NestedNameSpecifier *TransformNestedNameSpecifier(NestedNameSpecifier *NNS,
SourceRange Range,
+ bool MayBePseudoDestructor,
QualType ObjectType = QualType(),
NamedDecl *FirstQualifierInScope = 0);
@@ -555,6 +556,7 @@
NestedNameSpecifier *RebuildNestedNameSpecifier(NestedNameSpecifier *Prefix,
SourceRange Range,
IdentifierInfo &II,
+ bool MayBePseudoDestructor,
QualType ObjectType,
NamedDecl *FirstQualifierInScope);
@@ -577,7 +579,8 @@
NestedNameSpecifier *RebuildNestedNameSpecifier(NestedNameSpecifier *Prefix,
SourceRange Range,
bool TemplateKW,
- QualType T);
+ QualType T,
+ bool MayBePseudoDestructor);
/// \brief Build a new template name given a nested name specifier, a flag
/// indicating whether the "template" keyword was provided, and the template
@@ -1725,6 +1728,7 @@
NestedNameSpecifier *
TreeTransform<Derived>::TransformNestedNameSpecifier(NestedNameSpecifier *NNS,
SourceRange Range,
+ bool MayBePseudoDestructor,
QualType ObjectType,
NamedDecl *FirstQualifierInScope) {
if (!NNS)
@@ -1734,6 +1738,7 @@
NestedNameSpecifier *Prefix = NNS->getPrefix();
if (Prefix) {
Prefix = getDerived().TransformNestedNameSpecifier(Prefix, Range,
+ false,
ObjectType,
FirstQualifierInScope);
if (!Prefix)
@@ -1755,6 +1760,7 @@
return getDerived().RebuildNestedNameSpecifier(Prefix, Range,
*NNS->getAsIdentifier(),
+ MayBePseudoDestructor,
ObjectType,
FirstQualifierInScope);
@@ -1790,7 +1796,8 @@
return getDerived().RebuildNestedNameSpecifier(Prefix, Range,
NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
- T);
+ T,
+ MayBePseudoDestructor);
}
}
@@ -1842,6 +1849,7 @@
NestedNameSpecifier *NNS
= getDerived().TransformNestedNameSpecifier(QTN->getQualifier(),
/*FIXME:*/SourceRange(getDerived().getBaseLocation()),
+ false,
ObjectType);
if (!NNS)
return TemplateName();
@@ -1869,6 +1877,7 @@
NestedNameSpecifier *NNS
= getDerived().TransformNestedNameSpecifier(DTN->getQualifier(),
/*FIXME:*/SourceRange(getDerived().getBaseLocation()),
+ false,
ObjectType);
if (!NNS && DTN->getQualifier())
return TemplateName();
@@ -2928,6 +2937,7 @@
NestedNameSpecifier *NNS
= getDerived().TransformNestedNameSpecifier(T->getQualifier(),
SourceRange(),
+ false,
ObjectType);
if (!NNS)
return QualType();
@@ -2962,7 +2972,7 @@
NestedNameSpecifier *NNS
= getDerived().TransformNestedNameSpecifier(T->getQualifier(), SR,
- ObjectType);
+ false, ObjectType);
if (!NNS)
return QualType();
@@ -3598,7 +3608,8 @@
NestedNameSpecifier *Qualifier = 0;
if (E->getQualifier()) {
Qualifier = getDerived().TransformNestedNameSpecifier(E->getQualifier(),
- E->getQualifierRange());
+ E->getQualifierRange(),
+ false);
if (!Qualifier)
return SemaRef.ExprError();
}
@@ -3807,7 +3818,8 @@
if (E->hasQualifier()) {
Qualifier
= getDerived().TransformNestedNameSpecifier(E->getQualifier(),
- E->getQualifierRange());
+ E->getQualifierRange(),
+ false);
if (Qualifier == 0)
return SemaRef.ExprError();
}
@@ -4677,7 +4689,8 @@
NestedNameSpecifier *Qualifier
= getDerived().TransformNestedNameSpecifier(E->getQualifier(),
- E->getQualifierRange());
+ E->getQualifierRange(),
+ true);
if (E->getQualifier() && !Qualifier)
return SemaRef.ExprError();
@@ -4747,7 +4760,8 @@
NestedNameSpecifier *Qualifier = 0;
if (Old->getQualifier()) {
Qualifier = getDerived().TransformNestedNameSpecifier(Old->getQualifier(),
- Old->getQualifierRange());
+ Old->getQualifierRange(),
+ false);
if (!Qualifier)
return SemaRef.ExprError();
@@ -4803,7 +4817,8 @@
DependentScopeDeclRefExpr *E) {
NestedNameSpecifier *NNS
= getDerived().TransformNestedNameSpecifier(E->getQualifier(),
- E->getQualifierRange());
+ E->getQualifierRange(),
+ false);
if (!NNS)
return SemaRef.ExprError();
@@ -5050,8 +5065,11 @@
NestedNameSpecifier *Qualifier = 0;
if (E->getQualifier()) {
+ bool MayBePseudoDestructor
+ = E->getMember().getNameKind() == DeclarationName::CXXDestructorName;
Qualifier = getDerived().TransformNestedNameSpecifier(E->getQualifier(),
E->getQualifierRange(),
+ MayBePseudoDestructor,
ObjectType,
FirstQualifierInScope);
if (!Qualifier)
@@ -5126,7 +5144,8 @@
if (Old->getQualifier()) {
Qualifier
= getDerived().TransformNestedNameSpecifier(Old->getQualifier(),
- Old->getQualifierRange());
+ Old->getQualifierRange(),
+ false);
if (Qualifier == 0)
return SemaRef.ExprError();
}
@@ -5551,6 +5570,7 @@
TreeTransform<Derived>::RebuildNestedNameSpecifier(NestedNameSpecifier *Prefix,
SourceRange Range,
IdentifierInfo &II,
+ bool MayBePseudoDestructor,
QualType ObjectType,
NamedDecl *FirstQualifierInScope) {
CXXScopeSpec SS;
@@ -5560,6 +5580,7 @@
return static_cast<NestedNameSpecifier *>(
SemaRef.BuildCXXNestedNameSpecifier(0, SS, Range.getEnd(),
Range.getEnd(), II,
+ MayBePseudoDestructor,
ObjectType,
FirstQualifierInScope,
false, false));
@@ -5578,8 +5599,9 @@
TreeTransform<Derived>::RebuildNestedNameSpecifier(NestedNameSpecifier *Prefix,
SourceRange Range,
bool TemplateKW,
- QualType T) {
- if (T->isDependentType() || T->isRecordType() ||
+ QualType T,
+ bool MayBePseudoDestructor) {
+ if (MayBePseudoDestructor || T->isDependentType() || T->isRecordType() ||
(SemaRef.getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
assert(!T.hasLocalQualifiers() && "Can't get cv-qualifiers here");
return NestedNameSpecifier::Create(SemaRef.Context, Prefix, TemplateKW,