2009-02-15 04:20:19 +08:00
|
|
|
//===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements C++ semantic analysis for scope specifiers.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "TypeLocBuilder.h"
|
2009-02-15 04:20:19 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-05-12 03:58:34 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2009-08-06 11:17:00 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
Introduce a representation for types that we referred to via a
qualified name, e.g.,
foo::x
so that we retain the nested-name-specifier as written in the source
code and can reproduce that qualified name when printing the types
back (e.g., in diagnostics). This is PR3493, which won't be complete
until finished the other tasks mentioned near the end of this commit.
The parser's representation of nested-name-specifiers, CXXScopeSpec,
is now a bit fatter, because it needs to contain the scopes that
precede each '::' and keep track of whether the global scoping
operator '::' was at the beginning. For example, we need to keep track
of the leading '::', 'foo', and 'bar' in
::foo::bar::x
The Action's CXXScopeTy * is no longer a DeclContext *. It's now the
opaque version of the new NestedNameSpecifier, which contains a single
component of a nested-name-specifier (either a DeclContext * or a Type
*, bitmangled).
The new sugar type QualifiedNameType composes a sequence of
NestedNameSpecifiers with a representation of the type we're actually
referring to. At present, we only build QualifiedNameType nodes within
Sema::getTypeName. This will be extended to other type-constructing
actions (e.g., ActOnClassTemplateId).
Also on the way: QualifiedDeclRefExprs will also store a sequence of
NestedNameSpecifiers, so that we can print out the property
nested-name-specifier. I expect to also use this for handling
dependent names like Fibonacci<I - 1>::value.
llvm-svn: 67265
2009-03-19 08:18:19 +08:00
|
|
|
#include "clang/AST/NestedNameSpecifier.h"
|
2009-08-27 07:45:07 +08:00
|
|
|
#include "clang/Basic/PartialDiagnostic.h"
|
2010-08-21 02:27:03 +08:00
|
|
|
#include "clang/Sema/DeclSpec.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Sema/Lookup.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Sema/SemaInternal.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Sema/Template.h"
|
2009-02-15 04:20:19 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
using namespace clang;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Find the current instantiation that associated with the given type.
|
2012-11-22 08:24:47 +08:00
|
|
|
static CXXRecordDecl *getCurrentInstantiationOf(QualType T,
|
2011-02-20 03:24:40 +08:00
|
|
|
DeclContext *CurContext) {
|
2009-11-05 06:49:18 +08:00
|
|
|
if (T.isNull())
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-04-27 08:57:59 +08:00
|
|
|
|
|
|
|
const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
|
2011-02-20 03:24:40 +08:00
|
|
|
if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
|
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
|
2012-11-22 08:24:47 +08:00
|
|
|
if (!Record->isDependentContext() ||
|
|
|
|
Record->isCurrentInstantiation(CurContext))
|
2011-02-20 03:24:40 +08:00
|
|
|
return Record;
|
|
|
|
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2011-02-20 03:24:40 +08:00
|
|
|
} else if (isa<InjectedClassNameType>(Ty))
|
2010-04-27 08:57:59 +08:00
|
|
|
return cast<InjectedClassNameType>(Ty)->getDecl();
|
|
|
|
else
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2009-11-05 06:49:18 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Compute the DeclContext that is associated with the given type.
|
2009-09-03 06:59:36 +08:00
|
|
|
///
|
|
|
|
/// \param T the type for which we are attempting to find a DeclContext.
|
|
|
|
///
|
2009-09-09 23:08:12 +08:00
|
|
|
/// \returns the declaration context represented by the type T,
|
2009-09-03 06:59:36 +08:00
|
|
|
/// or NULL if the declaration context cannot be computed (e.g., because it is
|
|
|
|
/// dependent and not the current instantiation).
|
|
|
|
DeclContext *Sema::computeDeclContext(QualType T) {
|
2011-02-20 03:24:40 +08:00
|
|
|
if (!T->isDependentType())
|
|
|
|
if (const TagType *Tag = T->getAs<TagType>())
|
|
|
|
return Tag->getDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-20 03:24:40 +08:00
|
|
|
return ::getCurrentInstantiationOf(T, CurContext);
|
2009-09-03 06:59:36 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Compute the DeclContext that is associated with the given
|
Introduce a representation for types that we referred to via a
qualified name, e.g.,
foo::x
so that we retain the nested-name-specifier as written in the source
code and can reproduce that qualified name when printing the types
back (e.g., in diagnostics). This is PR3493, which won't be complete
until finished the other tasks mentioned near the end of this commit.
The parser's representation of nested-name-specifiers, CXXScopeSpec,
is now a bit fatter, because it needs to contain the scopes that
precede each '::' and keep track of whether the global scoping
operator '::' was at the beginning. For example, we need to keep track
of the leading '::', 'foo', and 'bar' in
::foo::bar::x
The Action's CXXScopeTy * is no longer a DeclContext *. It's now the
opaque version of the new NestedNameSpecifier, which contains a single
component of a nested-name-specifier (either a DeclContext * or a Type
*, bitmangled).
The new sugar type QualifiedNameType composes a sequence of
NestedNameSpecifiers with a representation of the type we're actually
referring to. At present, we only build QualifiedNameType nodes within
Sema::getTypeName. This will be extended to other type-constructing
actions (e.g., ActOnClassTemplateId).
Also on the way: QualifiedDeclRefExprs will also store a sequence of
NestedNameSpecifiers, so that we can print out the property
nested-name-specifier. I expect to also use this for handling
dependent names like Fibonacci<I - 1>::value.
llvm-svn: 67265
2009-03-19 08:18:19 +08:00
|
|
|
/// scope specifier.
|
2009-07-22 07:53:31 +08:00
|
|
|
///
|
|
|
|
/// \param SS the C++ scope specifier as it appears in the source
|
|
|
|
///
|
|
|
|
/// \param EnteringContext when true, we will be entering the context of
|
|
|
|
/// this scope specifier, so we can retrieve the declaration context of a
|
|
|
|
/// class template or class template partial specialization even if it is
|
|
|
|
/// not the current instantiation.
|
|
|
|
///
|
|
|
|
/// \returns the declaration context represented by the scope specifier @p SS,
|
|
|
|
/// or NULL if the declaration context cannot be computed (e.g., because it is
|
|
|
|
/// dependent and not the current instantiation).
|
|
|
|
DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS,
|
|
|
|
bool EnteringContext) {
|
Introduce a representation for types that we referred to via a
qualified name, e.g.,
foo::x
so that we retain the nested-name-specifier as written in the source
code and can reproduce that qualified name when printing the types
back (e.g., in diagnostics). This is PR3493, which won't be complete
until finished the other tasks mentioned near the end of this commit.
The parser's representation of nested-name-specifiers, CXXScopeSpec,
is now a bit fatter, because it needs to contain the scopes that
precede each '::' and keep track of whether the global scoping
operator '::' was at the beginning. For example, we need to keep track
of the leading '::', 'foo', and 'bar' in
::foo::bar::x
The Action's CXXScopeTy * is no longer a DeclContext *. It's now the
opaque version of the new NestedNameSpecifier, which contains a single
component of a nested-name-specifier (either a DeclContext * or a Type
*, bitmangled).
The new sugar type QualifiedNameType composes a sequence of
NestedNameSpecifiers with a representation of the type we're actually
referring to. At present, we only build QualifiedNameType nodes within
Sema::getTypeName. This will be extended to other type-constructing
actions (e.g., ActOnClassTemplateId).
Also on the way: QualifiedDeclRefExprs will also store a sequence of
NestedNameSpecifiers, so that we can print out the property
nested-name-specifier. I expect to also use this for handling
dependent names like Fibonacci<I - 1>::value.
llvm-svn: 67265
2009-03-19 08:18:19 +08:00
|
|
|
if (!SS.isSet() || SS.isInvalid())
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2009-03-18 08:36:05 +08:00
|
|
|
|
2013-03-26 08:54:11 +08:00
|
|
|
NestedNameSpecifier *NNS = SS.getScopeRep();
|
2009-05-12 03:58:34 +08:00
|
|
|
if (NNS->isDependent()) {
|
|
|
|
// If this nested-name-specifier refers to the current
|
|
|
|
// instantiation, return its DeclContext.
|
|
|
|
if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS))
|
|
|
|
return Record;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 07:53:31 +08:00
|
|
|
if (EnteringContext) {
|
2010-03-10 11:28:59 +08:00
|
|
|
const Type *NNSType = NNS->getAsType();
|
|
|
|
if (!NNSType) {
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2011-05-06 05:57:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look through type alias templates, per C++0x [temp.dep.type]p1.
|
|
|
|
NNSType = Context.getCanonicalType(NNSType);
|
|
|
|
if (const TemplateSpecializationType *SpecType
|
|
|
|
= NNSType->getAs<TemplateSpecializationType>()) {
|
Improve support for out-of-line definitions of nested templates and
their members, including member class template, member function
templates, and member classes and functions of member templates.
To actually parse the nested-name-specifiers that qualify the name of
an out-of-line definition of a member template, e.g.,
template<typename X> template<typename Y>
X Outer<X>::Inner1<Y>::foo(Y) {
return X();
}
we need to look for the template names (e.g., "Inner1") as a member of
the current instantiation (Outer<X>), even before we have entered the
scope of the current instantiation. Since we can't do this in general
(i.e., we should not be looking into all dependent
nested-name-specifiers as if they were the current instantiation), we
rely on the parser to tell us when it is parsing a declaration
specifier sequence, and, therefore, when we should consider the
current scope specifier to be a current instantiation.
Printing of complicated, dependent nested-name-specifiers may be
somewhat broken by this commit; I'll add tests for this issue and fix
the problem (if it still exists) in a subsequent commit.
llvm-svn: 80044
2009-08-26 06:51:20 +08:00
|
|
|
// We are entering the context of the nested name specifier, so try to
|
|
|
|
// match the nested name specifier to either a primary class template
|
|
|
|
// or a class template partial specialization.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (ClassTemplateDecl *ClassTemplate
|
2009-07-22 07:53:31 +08:00
|
|
|
= dyn_cast_or_null<ClassTemplateDecl>(
|
|
|
|
SpecType->getTemplateName().getAsTemplateDecl())) {
|
2009-07-31 01:40:51 +08:00
|
|
|
QualType ContextType
|
|
|
|
= Context.getCanonicalType(QualType(SpecType, 0));
|
|
|
|
|
2009-07-22 07:53:31 +08:00
|
|
|
// If the type of the nested name specifier is the same as the
|
|
|
|
// injected class name of the named class template, we're entering
|
|
|
|
// into that class template definition.
|
2010-03-10 11:28:59 +08:00
|
|
|
QualType Injected
|
2010-07-09 02:37:38 +08:00
|
|
|
= ClassTemplate->getInjectedClassNameSpecialization();
|
2009-07-31 01:40:51 +08:00
|
|
|
if (Context.hasSameType(Injected, ContextType))
|
2009-07-22 07:53:31 +08:00
|
|
|
return ClassTemplate->getTemplatedDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-31 01:40:51 +08:00
|
|
|
// If the type of the nested name specifier is the same as the
|
|
|
|
// type of one of the class template's class template partial
|
|
|
|
// specializations, we're entering into the definition of that
|
|
|
|
// class template partial specialization.
|
|
|
|
if (ClassTemplatePartialSpecializationDecl *PartialSpec
|
2016-05-05 08:56:12 +08:00
|
|
|
= ClassTemplate->findPartialSpecialization(ContextType)) {
|
|
|
|
// A declaration of the partial specialization must be visible.
|
|
|
|
// We can always recover here, because this only happens when we're
|
|
|
|
// entering the context, and that can't happen in a SFINAE context.
|
|
|
|
assert(!isSFINAEContext() &&
|
|
|
|
"partial specialization scope specifier in SFINAE context?");
|
|
|
|
if (!hasVisibleDeclaration(PartialSpec))
|
|
|
|
diagnoseMissingImport(SS.getLastQualifierNameLoc(), PartialSpec,
|
|
|
|
MissingImportKind::PartialSpecialization,
|
|
|
|
/*Recover*/true);
|
2009-07-31 01:40:51 +08:00
|
|
|
return PartialSpec;
|
2016-05-05 08:56:12 +08:00
|
|
|
}
|
2009-07-22 07:53:31 +08:00
|
|
|
}
|
2010-03-10 11:28:59 +08:00
|
|
|
} else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) {
|
Improve support for out-of-line definitions of nested templates and
their members, including member class template, member function
templates, and member classes and functions of member templates.
To actually parse the nested-name-specifiers that qualify the name of
an out-of-line definition of a member template, e.g.,
template<typename X> template<typename Y>
X Outer<X>::Inner1<Y>::foo(Y) {
return X();
}
we need to look for the template names (e.g., "Inner1") as a member of
the current instantiation (Outer<X>), even before we have entered the
scope of the current instantiation. Since we can't do this in general
(i.e., we should not be looking into all dependent
nested-name-specifiers as if they were the current instantiation), we
rely on the parser to tell us when it is parsing a declaration
specifier sequence, and, therefore, when we should consider the
current scope specifier to be a current instantiation.
Printing of complicated, dependent nested-name-specifiers may be
somewhat broken by this commit; I'll add tests for this issue and fix
the problem (if it still exists) in a subsequent commit.
llvm-svn: 80044
2009-08-26 06:51:20 +08:00
|
|
|
// The nested name specifier refers to a member of a class template.
|
|
|
|
return RecordT->getDecl();
|
2009-07-22 07:53:31 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2009-05-12 03:58:34 +08:00
|
|
|
}
|
2009-03-27 07:50:42 +08:00
|
|
|
|
|
|
|
switch (NNS->getKind()) {
|
|
|
|
case NestedNameSpecifier::Identifier:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Dependent nested-name-specifier has no DeclContext");
|
2009-03-27 07:50:42 +08:00
|
|
|
|
|
|
|
case NestedNameSpecifier::Namespace:
|
|
|
|
return NNS->getAsNamespace();
|
|
|
|
|
2011-02-24 10:36:08 +08:00
|
|
|
case NestedNameSpecifier::NamespaceAlias:
|
|
|
|
return NNS->getAsNamespaceAlias()->getNamespace();
|
|
|
|
|
2009-03-27 07:50:42 +08:00
|
|
|
case NestedNameSpecifier::TypeSpec:
|
|
|
|
case NestedNameSpecifier::TypeSpecWithTemplate: {
|
2010-02-25 12:46:04 +08:00
|
|
|
const TagType *Tag = NNS->getAsType()->getAs<TagType>();
|
|
|
|
assert(Tag && "Non-tag type in nested-name-specifier");
|
|
|
|
return Tag->getDecl();
|
2012-01-17 14:56:22 +08:00
|
|
|
}
|
2009-03-27 07:50:42 +08:00
|
|
|
|
|
|
|
case NestedNameSpecifier::Global:
|
|
|
|
return Context.getTranslationUnitDecl();
|
2014-09-26 08:28:20 +08:00
|
|
|
|
|
|
|
case NestedNameSpecifier::Super:
|
|
|
|
return NNS->getAsRecordDecl();
|
2009-03-27 07:50:42 +08:00
|
|
|
}
|
|
|
|
|
2012-01-17 14:56:22 +08:00
|
|
|
llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
|
2009-03-18 08:36:05 +08:00
|
|
|
}
|
|
|
|
|
Introduce a new expression type, UnresolvedDeclRefExpr, that describes
dependent qualified-ids such as
Fibonacci<N - 1>::value
where N is a template parameter. These references are "unresolved"
because the name is dependent and, therefore, cannot be resolved to a
declaration node (as we would do for a DeclRefExpr or
QualifiedDeclRefExpr). UnresolvedDeclRefExprs instantiate to
DeclRefExprs, QualifiedDeclRefExprs, etc.
Also, be a bit more careful about keeping only a single set of
specializations for a class template, and instantiating from the
definition of that template rather than a previous declaration. In
general, we need a better solution for this for all TagDecls, because
it's too easy to accidentally look at a declaration that isn't the
definition.
We can now process a simple Fibonacci computation described as a
template metaprogram.
llvm-svn: 67308
2009-03-20 01:26:29 +08:00
|
|
|
bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) {
|
|
|
|
if (!SS.isSet() || SS.isInvalid())
|
|
|
|
return false;
|
|
|
|
|
2013-03-26 08:54:11 +08:00
|
|
|
return SS.getScopeRep()->isDependent();
|
Introduce a new expression type, UnresolvedDeclRefExpr, that describes
dependent qualified-ids such as
Fibonacci<N - 1>::value
where N is a template parameter. These references are "unresolved"
because the name is dependent and, therefore, cannot be resolved to a
declaration node (as we would do for a DeclRefExpr or
QualifiedDeclRefExpr). UnresolvedDeclRefExprs instantiate to
DeclRefExprs, QualifiedDeclRefExprs, etc.
Also, be a bit more careful about keeping only a single set of
specializations for a class template, and instantiating from the
definition of that template rather than a previous declaration. In
general, we need a better solution for this for all TagDecls, because
it's too easy to accidentally look at a declaration that isn't the
definition.
We can now process a simple Fibonacci computation described as a
template metaprogram.
llvm-svn: 67308
2009-03-20 01:26:29 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If the given nested name specifier refers to the current
|
2009-05-12 03:58:34 +08:00
|
|
|
/// instantiation, return the declaration that corresponds to that
|
|
|
|
/// current instantiation (C++0x [temp.dep.type]p1).
|
|
|
|
///
|
|
|
|
/// \param NNS a dependent nested name specifier.
|
|
|
|
CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) {
|
2012-03-11 15:00:24 +08:00
|
|
|
assert(getLangOpts().CPlusPlus && "Only callable in C++");
|
2009-05-12 03:58:34 +08:00
|
|
|
assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed");
|
|
|
|
|
2009-07-22 07:53:31 +08:00
|
|
|
if (!NNS->getAsType())
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 02:32:42 +08:00
|
|
|
QualType T = QualType(NNS->getAsType(), 0);
|
2011-02-20 03:24:40 +08:00
|
|
|
return ::getCurrentInstantiationOf(T, CurContext);
|
2009-05-12 03:58:34 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Require that the context specified by SS be complete.
|
2009-03-12 00:48:53 +08:00
|
|
|
///
|
|
|
|
/// If SS refers to a type, this routine checks whether the type is
|
|
|
|
/// complete enough (or can be made complete enough) for name lookup
|
|
|
|
/// into the DeclContext. A type that is not yet completed can be
|
|
|
|
/// considered "complete enough" if it is a class/struct/union/enum
|
|
|
|
/// that is currently being defined. Or, if we have a type that names
|
|
|
|
/// a class template specialization that is not a complete type, we
|
|
|
|
/// will attempt to instantiate that class template.
|
2010-05-01 08:40:08 +08:00
|
|
|
bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS,
|
|
|
|
DeclContext *DC) {
|
2014-05-26 14:22:03 +08:00
|
|
|
assert(DC && "given null context");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-03-15 07:13:10 +08:00
|
|
|
TagDecl *tag = dyn_cast<TagDecl>(DC);
|
2011-07-06 14:57:57 +08:00
|
|
|
|
2012-03-15 07:13:10 +08:00
|
|
|
// If this is a dependent type, then we consider it complete.
|
2016-05-05 08:56:12 +08:00
|
|
|
// FIXME: This is wrong; we should require a (visible) definition to
|
|
|
|
// exist in this case too.
|
2012-03-15 07:13:10 +08:00
|
|
|
if (!tag || tag->isDependentContext())
|
|
|
|
return false;
|
|
|
|
|
2018-08-08 05:35:41 +08:00
|
|
|
// Grab the tag definition, if there is one.
|
|
|
|
QualType type = Context.getTypeDeclType(tag);
|
|
|
|
tag = type->getAsTagDecl();
|
|
|
|
|
2012-03-15 07:13:10 +08:00
|
|
|
// If we're currently defining this type, then lookup into the
|
|
|
|
// type is okay: don't complain that it isn't complete yet.
|
2018-08-08 05:35:41 +08:00
|
|
|
if (tag->isBeingDefined())
|
2012-03-15 07:13:10 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
SourceLocation loc = SS.getLastQualifierNameLoc();
|
|
|
|
if (loc.isInvalid()) loc = SS.getRange().getBegin();
|
|
|
|
|
|
|
|
// The type must be complete.
|
2012-05-05 00:32:21 +08:00
|
|
|
if (RequireCompleteType(loc, type, diag::err_incomplete_nested_name_spec,
|
|
|
|
SS.getRange())) {
|
2012-03-15 07:13:10 +08:00
|
|
|
SS.SetInvalid(SS.getRange());
|
|
|
|
return true;
|
2009-03-12 00:48:53 +08:00
|
|
|
}
|
|
|
|
|
2012-03-15 07:13:10 +08:00
|
|
|
// Fixed enum types are complete, but they aren't valid as scopes
|
|
|
|
// until we see a definition, so awkwardly pull out this special
|
|
|
|
// case.
|
2018-08-08 05:35:41 +08:00
|
|
|
auto *EnumD = dyn_cast<EnumDecl>(tag);
|
|
|
|
if (!EnumD)
|
2012-03-15 07:13:10 +08:00
|
|
|
return false;
|
2018-08-08 05:35:41 +08:00
|
|
|
if (EnumD->isCompleteDefinition()) {
|
2016-05-05 08:56:12 +08:00
|
|
|
// If we know about the definition but it is not visible, complain.
|
|
|
|
NamedDecl *SuggestedDef = nullptr;
|
2018-08-08 05:35:41 +08:00
|
|
|
if (!hasVisibleDefinition(EnumD, &SuggestedDef,
|
2016-05-05 08:56:12 +08:00
|
|
|
/*OnlyNeedComplete*/false)) {
|
|
|
|
// If the user is going to see an error here, recover by making the
|
|
|
|
// definition visible.
|
|
|
|
bool TreatAsComplete = !isSFINAEContext();
|
|
|
|
diagnoseMissingImport(loc, SuggestedDef, MissingImportKind::Definition,
|
|
|
|
/*Recover*/TreatAsComplete);
|
|
|
|
return !TreatAsComplete;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-15 07:13:10 +08:00
|
|
|
|
|
|
|
// Try to instantiate the definition, if this is a specialization of an
|
|
|
|
// enumeration temploid.
|
2018-08-08 05:35:41 +08:00
|
|
|
if (EnumDecl *Pattern = EnumD->getInstantiatedFromMemberEnum()) {
|
|
|
|
MemberSpecializationInfo *MSI = EnumD->getMemberSpecializationInfo();
|
2012-03-23 11:33:32 +08:00
|
|
|
if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) {
|
2018-08-08 05:35:41 +08:00
|
|
|
if (InstantiateEnum(loc, EnumD, Pattern,
|
|
|
|
getTemplateInstantiationArgs(EnumD),
|
2012-03-23 11:33:32 +08:00
|
|
|
TSK_ImplicitInstantiation)) {
|
|
|
|
SS.SetInvalid(SS.getRange());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-15 07:13:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Diag(loc, diag::err_incomplete_nested_name_spec)
|
|
|
|
<< type << SS.getRange();
|
|
|
|
SS.SetInvalid(SS.getRange());
|
|
|
|
return true;
|
2009-03-12 00:48:53 +08:00
|
|
|
}
|
2009-02-15 04:20:19 +08:00
|
|
|
|
2014-09-26 08:28:20 +08:00
|
|
|
bool Sema::ActOnCXXGlobalScopeSpecifier(SourceLocation CCLoc,
|
2011-02-24 08:17:56 +08:00
|
|
|
CXXScopeSpec &SS) {
|
|
|
|
SS.MakeGlobal(Context, CCLoc);
|
|
|
|
return false;
|
2009-02-15 04:20:19 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 08:28:20 +08:00
|
|
|
bool Sema::ActOnSuperScopeSpecifier(SourceLocation SuperLoc,
|
|
|
|
SourceLocation ColonColonLoc,
|
|
|
|
CXXScopeSpec &SS) {
|
|
|
|
CXXRecordDecl *RD = nullptr;
|
|
|
|
for (Scope *S = getCurScope(); S; S = S->getParent()) {
|
|
|
|
if (S->isFunctionScope()) {
|
|
|
|
if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(S->getEntity()))
|
|
|
|
RD = MD->getParent();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (S->isClassScope()) {
|
|
|
|
RD = cast<CXXRecordDecl>(S->getEntity());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!RD) {
|
|
|
|
Diag(SuperLoc, diag::err_invalid_super_scope);
|
|
|
|
return true;
|
|
|
|
} else if (RD->isLambda()) {
|
|
|
|
Diag(SuperLoc, diag::err_super_in_lambda_unsupported);
|
|
|
|
return true;
|
|
|
|
} else if (RD->getNumBases() == 0) {
|
|
|
|
Diag(SuperLoc, diag::err_no_base_classes) << RD->getName();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SS.MakeSuper(Context, RD, SuperLoc, ColonColonLoc);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Determines whether the given declaration is an valid acceptable
|
2009-09-03 06:59:36 +08:00
|
|
|
/// result for name lookup of a nested-name-specifier.
|
2015-01-19 04:04:35 +08:00
|
|
|
/// \param SD Declaration checked for nested-name-specifier.
|
|
|
|
/// \param IsExtension If not null and the declaration is accepted as an
|
|
|
|
/// extension, the pointed variable is assigned true.
|
|
|
|
bool Sema::isAcceptableNestedNameSpecifier(const NamedDecl *SD,
|
|
|
|
bool *IsExtension) {
|
2009-09-03 06:59:36 +08:00
|
|
|
if (!SD)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-12-30 07:34:32 +08:00
|
|
|
SD = SD->getUnderlyingDecl();
|
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
// Namespace and namespace aliases are fine.
|
2015-12-30 07:34:32 +08:00
|
|
|
if (isa<NamespaceDecl>(SD))
|
2009-09-03 06:59:36 +08:00
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
if (!isa<TypeDecl>(SD))
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-10-19 05:39:00 +08:00
|
|
|
// Determine whether we have a class (or, in C++11, an enum) or
|
2009-09-03 06:59:36 +08:00
|
|
|
// a typedef thereof. If so, build the nested-name-specifier.
|
|
|
|
QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
|
|
|
|
if (T->isDependentType())
|
|
|
|
return true;
|
2015-01-19 04:04:35 +08:00
|
|
|
if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
|
|
|
|
if (TD->getUnderlyingType()->isRecordType())
|
2009-09-03 06:59:36 +08:00
|
|
|
return true;
|
2015-01-19 04:04:35 +08:00
|
|
|
if (TD->getUnderlyingType()->isEnumeralType()) {
|
|
|
|
if (Context.getLangOpts().CPlusPlus11)
|
|
|
|
return true;
|
|
|
|
if (IsExtension)
|
|
|
|
*IsExtension = true;
|
|
|
|
}
|
|
|
|
} else if (isa<RecordDecl>(SD)) {
|
2009-09-03 06:59:36 +08:00
|
|
|
return true;
|
2015-01-19 04:04:35 +08:00
|
|
|
} else if (isa<EnumDecl>(SD)) {
|
|
|
|
if (Context.getLangOpts().CPlusPlus11)
|
|
|
|
return true;
|
|
|
|
if (IsExtension)
|
|
|
|
*IsExtension = true;
|
|
|
|
}
|
2009-09-03 06:59:36 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// If the given nested-name-specifier begins with a bare identifier
|
2009-09-09 23:08:12 +08:00
|
|
|
/// (e.g., Base::), perform name lookup for that identifier as a
|
2009-09-04 05:38:09 +08:00
|
|
|
/// nested-name-specifier within the given scope, and return the result of that
|
|
|
|
/// name lookup.
|
|
|
|
NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) {
|
|
|
|
if (!S || !NNS)
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-04 05:38:09 +08:00
|
|
|
while (NNS->getPrefix())
|
|
|
|
NNS = NNS->getPrefix();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-04 05:38:09 +08:00
|
|
|
if (NNS->getKind() != NestedNameSpecifier::Identifier)
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-17 10:14:36 +08:00
|
|
|
LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(),
|
|
|
|
LookupNestedNameSpecifierName);
|
|
|
|
LookupName(Found, S);
|
2009-09-04 05:38:09 +08:00
|
|
|
assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet");
|
|
|
|
|
2009-12-02 16:25:40 +08:00
|
|
|
if (!Found.isSingleResult())
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2009-12-02 16:25:40 +08:00
|
|
|
|
|
|
|
NamedDecl *Result = Found.getFoundDecl();
|
2010-02-25 12:46:04 +08:00
|
|
|
if (isAcceptableNestedNameSpecifier(Result))
|
2009-09-04 05:38:09 +08:00
|
|
|
return Result;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2009-09-04 05:38:09 +08:00
|
|
|
}
|
|
|
|
|
2010-04-09 00:38:48 +08:00
|
|
|
bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS,
|
2016-08-08 12:02:15 +08:00
|
|
|
NestedNameSpecInfo &IdInfo) {
|
|
|
|
QualType ObjectType = GetTypeFromParser(IdInfo.ObjectType);
|
|
|
|
LookupResult Found(*this, IdInfo.Identifier, IdInfo.IdentifierLoc,
|
|
|
|
LookupNestedNameSpecifierName);
|
|
|
|
|
2010-02-25 05:29:12 +08:00
|
|
|
// Determine where to perform name lookup
|
2014-05-26 14:22:03 +08:00
|
|
|
DeclContext *LookupCtx = nullptr;
|
2010-02-25 05:29:12 +08:00
|
|
|
bool isDependent = false;
|
|
|
|
if (!ObjectType.isNull()) {
|
|
|
|
// This nested-name-specifier occurs in a member access expression, e.g.,
|
|
|
|
// x->B::f, and we are looking into the type of the object.
|
|
|
|
assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
|
|
|
|
LookupCtx = computeDeclContext(ObjectType);
|
|
|
|
isDependent = ObjectType->isDependentType();
|
|
|
|
} else if (SS.isSet()) {
|
|
|
|
// This nested-name-specifier occurs after another nested-name-specifier,
|
|
|
|
// so long into the context associated with the prior nested-name-specifier.
|
|
|
|
LookupCtx = computeDeclContext(SS, false);
|
|
|
|
isDependent = isDependentScopeSpecifier(SS);
|
|
|
|
Found.setContextRange(SS.getRange());
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-02-25 05:29:12 +08:00
|
|
|
if (LookupCtx) {
|
|
|
|
// Perform "qualified" name lookup into the declaration context we
|
|
|
|
// computed, which is either the type of the base of a member access
|
|
|
|
// expression or the declaration context associated with a prior
|
|
|
|
// nested-name-specifier.
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-02-25 05:29:12 +08:00
|
|
|
// The declaration context must be complete.
|
2010-05-01 08:40:08 +08:00
|
|
|
if (!LookupCtx->isDependentContext() &&
|
|
|
|
RequireCompleteDeclContext(SS, LookupCtx))
|
2010-02-25 05:29:12 +08:00
|
|
|
return false;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-02-25 05:29:12 +08:00
|
|
|
LookupQualifiedName(Found, LookupCtx);
|
|
|
|
} else if (isDependent) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
LookupName(Found, S);
|
|
|
|
}
|
|
|
|
Found.suppressDiagnostics();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2015-12-30 07:34:32 +08:00
|
|
|
return Found.getAsSingle<NamespaceDecl>();
|
2010-02-25 05:29:12 +08:00
|
|
|
}
|
|
|
|
|
2012-01-13 06:32:39 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Callback to only accept typo corrections that can be a valid C++ member
|
|
|
|
// intializer: either a non-static field member or a base class.
|
|
|
|
class NestedNameSpecifierValidatorCCC : public CorrectionCandidateCallback {
|
|
|
|
public:
|
|
|
|
explicit NestedNameSpecifierValidatorCCC(Sema &SRef)
|
|
|
|
: SRef(SRef) {}
|
|
|
|
|
2014-03-12 12:55:44 +08:00
|
|
|
bool ValidateCandidate(const TypoCorrection &candidate) override {
|
2012-01-13 06:32:39 +08:00
|
|
|
return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Sema &SRef;
|
|
|
|
};
|
|
|
|
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2012-01-13 06:32:39 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Build a new nested-name-specifier for "identifier::", as described
|
2009-09-04 05:38:09 +08:00
|
|
|
/// by ActOnCXXNestedNameSpecifier.
|
|
|
|
///
|
2014-04-14 00:52:03 +08:00
|
|
|
/// \param S Scope in which the nested-name-specifier occurs.
|
2016-08-08 12:02:15 +08:00
|
|
|
/// \param IdInfo Parser information about an identifier in the
|
|
|
|
/// nested-name-spec.
|
2014-04-14 00:52:03 +08:00
|
|
|
/// \param EnteringContext If true, enter the context specified by the
|
|
|
|
/// nested-name-specifier.
|
|
|
|
/// \param SS Optional nested name specifier preceding the identifier.
|
|
|
|
/// \param ScopeLookupResult Provides the result of name lookup within the
|
|
|
|
/// scope of the nested-name-specifier that was computed at template
|
|
|
|
/// definition time.
|
|
|
|
/// \param ErrorRecoveryLookup Specifies if the method is called to improve
|
|
|
|
/// error recovery and what kind of recovery is performed.
|
|
|
|
/// \param IsCorrectedToColon If not null, suggestion of replace '::' -> ':'
|
|
|
|
/// are allowed. The bool value pointed by this parameter is set to
|
|
|
|
/// 'true' if the identifier is treated as if it was followed by ':',
|
|
|
|
/// not '::'.
|
2017-03-18 05:41:20 +08:00
|
|
|
/// \param OnlyNamespace If true, only considers namespaces in lookup.
|
2014-04-14 00:52:03 +08:00
|
|
|
///
|
|
|
|
/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
|
2009-09-04 05:38:09 +08:00
|
|
|
/// that it contains an extra parameter \p ScopeLookupResult, which provides
|
|
|
|
/// the result of name lookup within the scope of the nested-name-specifier
|
2009-12-31 00:01:52 +08:00
|
|
|
/// that was computed at template definition time.
|
2009-12-07 09:36:53 +08:00
|
|
|
///
|
|
|
|
/// If ErrorRecoveryLookup is true, then this call is used to improve error
|
|
|
|
/// recovery. This means that it should not emit diagnostics, it should
|
2011-02-24 08:17:56 +08:00
|
|
|
/// just return true on failure. It also means it should only return a valid
|
2009-12-07 09:36:53 +08:00
|
|
|
/// scope if it *knows* that the result is correct. It should not return in a
|
2011-02-24 08:17:56 +08:00
|
|
|
/// dependent context, for example. Nor will it extend \p SS with the scope
|
|
|
|
/// specifier.
|
2017-03-18 05:41:20 +08:00
|
|
|
bool Sema::BuildCXXNestedNameSpecifier(Scope *S, NestedNameSpecInfo &IdInfo,
|
|
|
|
bool EnteringContext, CXXScopeSpec &SS,
|
2011-02-24 08:17:56 +08:00
|
|
|
NamedDecl *ScopeLookupResult,
|
2014-04-14 00:52:03 +08:00
|
|
|
bool ErrorRecoveryLookup,
|
2017-03-18 05:41:20 +08:00
|
|
|
bool *IsCorrectedToColon,
|
|
|
|
bool OnlyNamespace) {
|
2017-04-19 16:58:56 +08:00
|
|
|
if (IdInfo.Identifier->isEditorPlaceholder())
|
|
|
|
return true;
|
2016-08-08 12:02:15 +08:00
|
|
|
LookupResult Found(*this, IdInfo.Identifier, IdInfo.IdentifierLoc,
|
2017-03-18 05:41:20 +08:00
|
|
|
OnlyNamespace ? LookupNamespaceName
|
|
|
|
: LookupNestedNameSpecifierName);
|
2016-08-08 12:02:15 +08:00
|
|
|
QualType ObjectType = GetTypeFromParser(IdInfo.ObjectType);
|
2009-11-17 10:14:36 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
// Determine where to perform name lookup
|
2014-05-26 14:22:03 +08:00
|
|
|
DeclContext *LookupCtx = nullptr;
|
2009-09-03 06:59:36 +08:00
|
|
|
bool isDependent = false;
|
2014-04-14 00:52:03 +08:00
|
|
|
if (IsCorrectedToColon)
|
|
|
|
*IsCorrectedToColon = false;
|
2009-09-04 05:38:09 +08:00
|
|
|
if (!ObjectType.isNull()) {
|
2009-09-03 06:59:36 +08:00
|
|
|
// This nested-name-specifier occurs in a member access expression, e.g.,
|
|
|
|
// x->B::f, and we are looking into the type of the object.
|
|
|
|
assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
|
|
|
|
LookupCtx = computeDeclContext(ObjectType);
|
|
|
|
isDependent = ObjectType->isDependentType();
|
|
|
|
} else if (SS.isSet()) {
|
|
|
|
// This nested-name-specifier occurs after another nested-name-specifier,
|
2011-05-06 05:57:07 +08:00
|
|
|
// so look into the context associated with the prior nested-name-specifier.
|
2009-09-03 06:59:36 +08:00
|
|
|
LookupCtx = computeDeclContext(SS, EnteringContext);
|
|
|
|
isDependent = isDependentScopeSpecifier(SS);
|
2009-11-17 10:14:36 +08:00
|
|
|
Found.setContextRange(SS.getRange());
|
2009-09-03 06:59:36 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
bool ObjectTypeSearchedInScope = false;
|
|
|
|
if (LookupCtx) {
|
2009-09-09 23:08:12 +08:00
|
|
|
// Perform "qualified" name lookup into the declaration context we
|
2009-09-03 06:59:36 +08:00
|
|
|
// computed, which is either the type of the base of a member access
|
2009-09-09 23:08:12 +08:00
|
|
|
// expression or the declaration context associated with a prior
|
2009-09-03 06:59:36 +08:00
|
|
|
// nested-name-specifier.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
// The declaration context must be complete.
|
2010-05-01 08:40:08 +08:00
|
|
|
if (!LookupCtx->isDependentContext() &&
|
|
|
|
RequireCompleteDeclContext(SS, LookupCtx))
|
2011-02-24 08:17:56 +08:00
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-17 10:14:36 +08:00
|
|
|
LookupQualifiedName(Found, LookupCtx);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-17 10:14:36 +08:00
|
|
|
if (!ObjectType.isNull() && Found.empty()) {
|
2009-09-03 06:59:36 +08:00
|
|
|
// C++ [basic.lookup.classref]p4:
|
|
|
|
// If the id-expression in a class member access is a qualified-id of
|
2009-09-09 23:08:12 +08:00
|
|
|
// the form
|
2009-09-03 06:59:36 +08:00
|
|
|
//
|
|
|
|
// class-name-or-namespace-name::...
|
|
|
|
//
|
2009-09-09 23:08:12 +08:00
|
|
|
// the class-name-or-namespace-name following the . or -> operator is
|
|
|
|
// looked up both in the context of the entire postfix-expression and in
|
2009-09-03 06:59:36 +08:00
|
|
|
// the scope of the class of the object expression. If the name is found
|
2009-09-09 23:08:12 +08:00
|
|
|
// only in the scope of the class of the object expression, the name
|
|
|
|
// shall refer to a class-name. If the name is found only in the
|
2009-09-03 06:59:36 +08:00
|
|
|
// context of the entire postfix-expression, the name shall refer to a
|
|
|
|
// class-name or namespace-name. [...]
|
|
|
|
//
|
|
|
|
// Qualified name lookup into a class will not find a namespace-name,
|
2011-05-16 01:27:27 +08:00
|
|
|
// so we do not need to diagnose that case specifically. However,
|
2009-09-03 06:59:36 +08:00
|
|
|
// this qualified name lookup may find nothing. In that case, perform
|
2009-09-09 23:08:12 +08:00
|
|
|
// unqualified name lookup in the given scope (if available) or
|
2009-09-04 05:38:09 +08:00
|
|
|
// reconstruct the result from when name lookup was performed at template
|
|
|
|
// definition time.
|
|
|
|
if (S)
|
2009-11-17 10:14:36 +08:00
|
|
|
LookupName(Found, S);
|
2009-10-10 05:13:30 +08:00
|
|
|
else if (ScopeLookupResult)
|
|
|
|
Found.addDecl(ScopeLookupResult);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
ObjectTypeSearchedInScope = true;
|
|
|
|
}
|
2010-07-28 22:49:07 +08:00
|
|
|
} else if (!isDependent) {
|
|
|
|
// Perform unqualified name lookup in the current scope.
|
|
|
|
LookupName(Found, S);
|
|
|
|
}
|
|
|
|
|
2015-11-13 06:40:09 +08:00
|
|
|
if (Found.isAmbiguous())
|
|
|
|
return true;
|
|
|
|
|
2010-07-28 22:49:07 +08:00
|
|
|
// If we performed lookup into a dependent context and did not find anything,
|
|
|
|
// that's fine: just build a dependent nested-name-specifier.
|
|
|
|
if (Found.empty() && isDependent &&
|
|
|
|
!(LookupCtx && LookupCtx->isRecord() &&
|
|
|
|
(!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
|
|
|
|
!cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) {
|
2009-12-07 09:36:53 +08:00
|
|
|
// Don't speculate if we're just trying to improve error recovery.
|
|
|
|
if (ErrorRecoveryLookup)
|
2011-02-24 08:17:56 +08:00
|
|
|
return true;
|
2014-04-14 00:52:03 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
// We were not able to compute the declaration context for a dependent
|
2009-09-09 23:08:12 +08:00
|
|
|
// base object type or prior nested-name-specifier, so this
|
2009-09-03 06:59:36 +08:00
|
|
|
// nested-name-specifier refers to an unknown specialization. Just build
|
|
|
|
// a dependent nested-name-specifier.
|
2016-08-08 12:02:15 +08:00
|
|
|
SS.Extend(Context, IdInfo.Identifier, IdInfo.IdentifierLoc, IdInfo.CCLoc);
|
2011-02-24 08:17:56 +08:00
|
|
|
return false;
|
2014-04-14 00:52:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Found.empty() && !ErrorRecoveryLookup) {
|
|
|
|
// If identifier is not found as class-name-or-namespace-name, but is found
|
|
|
|
// as other entity, don't look for typos.
|
|
|
|
LookupResult R(*this, Found.getLookupNameInfo(), LookupOrdinaryName);
|
|
|
|
if (LookupCtx)
|
|
|
|
LookupQualifiedName(R, LookupCtx);
|
|
|
|
else if (S && !isDependent)
|
|
|
|
LookupName(R, S);
|
|
|
|
if (!R.empty()) {
|
2015-11-13 06:40:09 +08:00
|
|
|
// Don't diagnose problems with this speculative lookup.
|
|
|
|
R.suppressDiagnostics();
|
2014-04-14 00:52:03 +08:00
|
|
|
// The identifier is found in ordinary lookup. If correction to colon is
|
|
|
|
// allowed, suggest replacement to ':'.
|
|
|
|
if (IsCorrectedToColon) {
|
|
|
|
*IsCorrectedToColon = true;
|
2016-08-08 12:02:15 +08:00
|
|
|
Diag(IdInfo.CCLoc, diag::err_nested_name_spec_is_not_class)
|
|
|
|
<< IdInfo.Identifier << getLangOpts().CPlusPlus
|
|
|
|
<< FixItHint::CreateReplacement(IdInfo.CCLoc, ":");
|
2014-04-14 00:52:03 +08:00
|
|
|
if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
|
|
|
|
Diag(ND->getLocation(), diag::note_declared_at);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Replacement '::' -> ':' is not allowed, just issue respective error.
|
2017-03-18 05:41:20 +08:00
|
|
|
Diag(R.getNameLoc(), OnlyNamespace
|
2017-03-18 06:19:20 +08:00
|
|
|
? unsigned(diag::err_expected_namespace_name)
|
|
|
|
: unsigned(diag::err_expected_class_or_namespace))
|
2016-08-08 12:02:15 +08:00
|
|
|
<< IdInfo.Identifier << getLangOpts().CPlusPlus;
|
2014-04-14 00:52:03 +08:00
|
|
|
if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
|
2016-08-08 12:02:15 +08:00
|
|
|
Diag(ND->getLocation(), diag::note_entity_declared_at)
|
|
|
|
<< IdInfo.Identifier;
|
2014-04-14 00:52:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 20:51:41 +08:00
|
|
|
if (Found.empty() && !ErrorRecoveryLookup && !getLangOpts().MSVCCompat) {
|
2009-12-31 16:26:35 +08:00
|
|
|
// We haven't found anything, and we're not recovering from a
|
|
|
|
// different kind of error, so look for typos.
|
|
|
|
DeclarationName Name = Found.getLookupName();
|
2011-06-29 00:20:02 +08:00
|
|
|
Found.clear();
|
2014-10-28 02:07:29 +08:00
|
|
|
if (TypoCorrection Corrected = CorrectTypo(
|
|
|
|
Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS,
|
|
|
|
llvm::make_unique<NestedNameSpecifierValidatorCCC>(*this),
|
|
|
|
CTK_ErrorRecovery, LookupCtx, EnteringContext)) {
|
2013-08-17 08:46:16 +08:00
|
|
|
if (LookupCtx) {
|
|
|
|
bool DroppedSpecifier =
|
|
|
|
Corrected.WillReplaceSpecifier() &&
|
|
|
|
Name.getAsString() == Corrected.getAsString(getLangOpts());
|
2013-12-17 03:19:18 +08:00
|
|
|
if (DroppedSpecifier)
|
|
|
|
SS.clear();
|
2013-08-17 08:46:16 +08:00
|
|
|
diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest)
|
|
|
|
<< Name << LookupCtx << DroppedSpecifier
|
|
|
|
<< SS.getRange());
|
|
|
|
} else
|
|
|
|
diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
|
|
|
|
<< Name);
|
|
|
|
|
2016-02-17 03:16:20 +08:00
|
|
|
if (Corrected.getCorrectionSpecifier())
|
|
|
|
SS.MakeTrivial(Context, Corrected.getCorrectionSpecifier(),
|
|
|
|
SourceRange(Found.getNameLoc()));
|
|
|
|
|
2015-12-30 03:43:10 +08:00
|
|
|
if (NamedDecl *ND = Corrected.getFoundDecl())
|
2011-06-29 00:20:02 +08:00
|
|
|
Found.addDecl(ND);
|
|
|
|
Found.setLookupName(Corrected.getCorrection());
|
2010-06-30 03:27:42 +08:00
|
|
|
} else {
|
2016-08-08 12:02:15 +08:00
|
|
|
Found.setLookupName(IdInfo.Identifier);
|
2010-06-30 03:27:42 +08:00
|
|
|
}
|
2009-12-31 16:26:35 +08:00
|
|
|
}
|
|
|
|
|
2015-12-30 07:34:32 +08:00
|
|
|
NamedDecl *SD =
|
|
|
|
Found.isSingleResult() ? Found.getRepresentativeDecl() : nullptr;
|
2015-01-19 04:04:35 +08:00
|
|
|
bool IsExtension = false;
|
|
|
|
bool AcceptSpec = isAcceptableNestedNameSpecifier(SD, &IsExtension);
|
|
|
|
if (!AcceptSpec && IsExtension) {
|
|
|
|
AcceptSpec = true;
|
2016-08-08 12:02:15 +08:00
|
|
|
Diag(IdInfo.IdentifierLoc, diag::ext_nested_name_spec_is_enum);
|
2015-01-19 04:04:35 +08:00
|
|
|
}
|
|
|
|
if (AcceptSpec) {
|
2012-05-02 04:23:02 +08:00
|
|
|
if (!ObjectType.isNull() && !ObjectTypeSearchedInScope &&
|
2013-01-02 19:42:31 +08:00
|
|
|
!getLangOpts().CPlusPlus11) {
|
2012-05-02 04:23:02 +08:00
|
|
|
// C++03 [basic.lookup.classref]p4:
|
2009-09-09 23:08:12 +08:00
|
|
|
// [...] If the name is found in both contexts, the
|
2009-09-03 06:59:36 +08:00
|
|
|
// class-name-or-namespace-name shall refer to the same entity.
|
|
|
|
//
|
|
|
|
// We already found the name in the scope of the object. Now, look
|
|
|
|
// into the current scope (the scope of the postfix-expression) to
|
2009-09-04 05:38:09 +08:00
|
|
|
// see if we can find the same name there. As above, if there is no
|
|
|
|
// scope, reconstruct the result from the template instantiation itself.
|
2012-05-02 04:23:02 +08:00
|
|
|
//
|
|
|
|
// Note that C++11 does *not* perform this redundant lookup.
|
2009-10-10 05:13:30 +08:00
|
|
|
NamedDecl *OuterDecl;
|
|
|
|
if (S) {
|
2016-08-08 12:02:15 +08:00
|
|
|
LookupResult FoundOuter(*this, IdInfo.Identifier, IdInfo.IdentifierLoc,
|
2011-02-24 08:17:56 +08:00
|
|
|
LookupNestedNameSpecifierName);
|
2009-11-17 10:14:36 +08:00
|
|
|
LookupName(FoundOuter, S);
|
2009-12-02 16:25:40 +08:00
|
|
|
OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
|
2009-10-10 05:13:30 +08:00
|
|
|
} else
|
|
|
|
OuterDecl = ScopeLookupResult;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-25 12:46:04 +08:00
|
|
|
if (isAcceptableNestedNameSpecifier(OuterDecl) &&
|
2009-09-03 06:59:36 +08:00
|
|
|
OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
|
|
|
|
(!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
|
|
|
|
!Context.hasSameType(
|
2009-09-04 05:38:09 +08:00
|
|
|
Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
|
2009-09-03 06:59:36 +08:00
|
|
|
Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
|
2014-04-14 00:52:03 +08:00
|
|
|
if (ErrorRecoveryLookup)
|
|
|
|
return true;
|
2011-02-24 08:17:56 +08:00
|
|
|
|
2016-08-08 12:02:15 +08:00
|
|
|
Diag(IdInfo.IdentifierLoc,
|
2011-02-24 08:17:56 +08:00
|
|
|
diag::err_nested_name_member_ref_lookup_ambiguous)
|
2016-08-08 12:02:15 +08:00
|
|
|
<< IdInfo.Identifier;
|
2011-02-24 08:17:56 +08:00
|
|
|
Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type)
|
|
|
|
<< ObjectType;
|
|
|
|
Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope);
|
|
|
|
|
|
|
|
// Fall through so that we'll pick the name we found in the object
|
|
|
|
// type, since that's probably what the user wanted anyway.
|
|
|
|
}
|
2009-09-03 06:59:36 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Add -Wunused-local-typedef, a warning that finds unused local typedefs.
The warning warns on TypedefNameDecls -- typedefs and C++11 using aliases --
that are !isReferenced(). Since the isReferenced() bit on TypedefNameDecls
wasn't used for anything before this warning it wasn't always set correctly,
so this patch also adds a few missing MarkAnyDeclReferenced() calls in
various places for TypedefNameDecls.
This is made a bit complicated due to local typedefs possibly being used only
after their local scope has closed. Consider:
template <class T>
void template_fun(T t) {
typename T::Foo s3foo; // YYY
(void)s3foo;
}
void template_fun_user() {
struct Local {
typedef int Foo; // XXX
} p;
template_fun(p);
}
Here the typedef in XXX is only used at end-of-translation unit, when YYY in
template_fun() gets instantiated. To handle this, typedefs that are unused when
their scope exits are added to a set of potentially unused typedefs, and that
set gets checked at end-of-TU. Typedefs that are still unused at that point then
get warned on. There's also serialization code for this set, so that the
warning works with precompiled headers and modules. For modules, the warning
is emitted when the module is built, for precompiled headers each time the
header gets used.
Finally, consider a function using C++14 auto return types to return a local
type defined in a header:
auto f() {
struct S { typedef int a; };
return S();
}
Here, the typedef escapes its local scope and could be used by only some
translation units including the header. To not warn on this, add a
RecursiveASTVisitor that marks all delcs on local types returned from auto
functions as referenced. (Except if it's a function with internal linkage, or
the decls are private and the local type has no friends -- in these cases, it
_is_ safe to warn.)
Several of the included testcases (most of the interesting ones) were provided
by Richard Smith.
(gcc's spelling -Wunused-local-typedefs is supported as an alias for this
warning.)
llvm-svn: 217298
2014-09-06 09:25:55 +08:00
|
|
|
if (auto *TD = dyn_cast_or_null<TypedefNameDecl>(SD))
|
|
|
|
MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
|
|
|
|
|
2014-04-14 00:52:03 +08:00
|
|
|
// If we're just performing this lookup for error-recovery purposes,
|
2011-02-24 08:17:56 +08:00
|
|
|
// don't extend the nested-name-specifier. Just return now.
|
|
|
|
if (ErrorRecoveryLookup)
|
|
|
|
return false;
|
2014-11-15 06:34:56 +08:00
|
|
|
|
|
|
|
// The use of a nested name specifier may trigger deprecation warnings.
|
2016-08-08 12:02:15 +08:00
|
|
|
DiagnoseUseOfDecl(SD, IdInfo.CCLoc);
|
2014-11-15 06:34:56 +08:00
|
|
|
|
2011-02-24 08:17:56 +08:00
|
|
|
if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) {
|
2016-08-08 12:02:15 +08:00
|
|
|
SS.Extend(Context, Namespace, IdInfo.IdentifierLoc, IdInfo.CCLoc);
|
2011-02-24 08:17:56 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-24 08:17:56 +08:00
|
|
|
if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) {
|
2016-08-08 12:02:15 +08:00
|
|
|
SS.Extend(Context, Alias, IdInfo.IdentifierLoc, IdInfo.CCLoc);
|
2011-02-24 08:17:56 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-12-30 07:34:32 +08:00
|
|
|
QualType T =
|
|
|
|
Context.getTypeDeclType(cast<TypeDecl>(SD->getUnderlyingDecl()));
|
2011-02-24 08:17:56 +08:00
|
|
|
TypeLocBuilder TLB;
|
|
|
|
if (isa<InjectedClassNameType>(T)) {
|
|
|
|
InjectedClassNameTypeLoc InjectedTL
|
|
|
|
= TLB.push<InjectedClassNameTypeLoc>(T);
|
2016-08-08 12:02:15 +08:00
|
|
|
InjectedTL.setNameLoc(IdInfo.IdentifierLoc);
|
2011-05-05 07:05:40 +08:00
|
|
|
} else if (isa<RecordType>(T)) {
|
2011-02-24 08:17:56 +08:00
|
|
|
RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T);
|
2016-08-08 12:02:15 +08:00
|
|
|
RecordTL.setNameLoc(IdInfo.IdentifierLoc);
|
2011-05-05 07:05:40 +08:00
|
|
|
} else if (isa<TypedefType>(T)) {
|
2011-02-24 08:17:56 +08:00
|
|
|
TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T);
|
2016-08-08 12:02:15 +08:00
|
|
|
TypedefTL.setNameLoc(IdInfo.IdentifierLoc);
|
2011-05-05 07:05:40 +08:00
|
|
|
} else if (isa<EnumType>(T)) {
|
2011-02-24 08:17:56 +08:00
|
|
|
EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T);
|
2016-08-08 12:02:15 +08:00
|
|
|
EnumTL.setNameLoc(IdInfo.IdentifierLoc);
|
2011-05-05 07:05:40 +08:00
|
|
|
} else if (isa<TemplateTypeParmType>(T)) {
|
2011-02-24 08:17:56 +08:00
|
|
|
TemplateTypeParmTypeLoc TemplateTypeTL
|
|
|
|
= TLB.push<TemplateTypeParmTypeLoc>(T);
|
2016-08-08 12:02:15 +08:00
|
|
|
TemplateTypeTL.setNameLoc(IdInfo.IdentifierLoc);
|
2011-05-05 07:05:40 +08:00
|
|
|
} else if (isa<UnresolvedUsingType>(T)) {
|
2011-02-24 08:17:56 +08:00
|
|
|
UnresolvedUsingTypeLoc UnresolvedTL
|
|
|
|
= TLB.push<UnresolvedUsingTypeLoc>(T);
|
2016-08-08 12:02:15 +08:00
|
|
|
UnresolvedTL.setNameLoc(IdInfo.IdentifierLoc);
|
2011-05-05 07:05:40 +08:00
|
|
|
} else if (isa<SubstTemplateTypeParmType>(T)) {
|
2018-07-31 03:24:48 +08:00
|
|
|
SubstTemplateTypeParmTypeLoc TL
|
2011-05-05 07:05:40 +08:00
|
|
|
= TLB.push<SubstTemplateTypeParmTypeLoc>(T);
|
2016-08-08 12:02:15 +08:00
|
|
|
TL.setNameLoc(IdInfo.IdentifierLoc);
|
2011-05-05 07:05:40 +08:00
|
|
|
} else if (isa<SubstTemplateTypeParmPackType>(T)) {
|
|
|
|
SubstTemplateTypeParmPackTypeLoc TL
|
|
|
|
= TLB.push<SubstTemplateTypeParmPackTypeLoc>(T);
|
2016-08-08 12:02:15 +08:00
|
|
|
TL.setNameLoc(IdInfo.IdentifierLoc);
|
2011-05-05 07:05:40 +08:00
|
|
|
} else {
|
|
|
|
llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier");
|
2011-02-24 08:17:56 +08:00
|
|
|
}
|
|
|
|
|
2011-10-20 11:28:47 +08:00
|
|
|
if (T->isEnumeralType())
|
2016-08-08 12:02:15 +08:00
|
|
|
Diag(IdInfo.IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec);
|
2011-10-20 11:28:47 +08:00
|
|
|
|
2011-02-24 08:17:56 +08:00
|
|
|
SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
|
2016-08-08 12:02:15 +08:00
|
|
|
IdInfo.CCLoc);
|
2011-02-24 08:17:56 +08:00
|
|
|
return false;
|
2009-09-03 06:59:36 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-07 09:36:53 +08:00
|
|
|
// Otherwise, we have an error case. If we don't want diagnostics, just
|
|
|
|
// return an error now.
|
|
|
|
if (ErrorRecoveryLookup)
|
2011-02-24 08:17:56 +08:00
|
|
|
return true;
|
2009-12-07 09:36:53 +08:00
|
|
|
|
2009-02-15 04:20:19 +08:00
|
|
|
// If we didn't find anything during our lookup, try again with
|
|
|
|
// ordinary name lookup, which can help us produce better error
|
|
|
|
// messages.
|
2009-12-02 16:25:40 +08:00
|
|
|
if (Found.empty()) {
|
2009-11-17 10:14:36 +08:00
|
|
|
Found.clear(LookupOrdinaryName);
|
|
|
|
LookupName(Found, S);
|
2009-10-10 05:13:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-27 09:05:24 +08:00
|
|
|
// In Microsoft mode, if we are within a templated function and we can't
|
2018-07-31 03:24:48 +08:00
|
|
|
// resolve Identifier, then extend the SS with Identifier. This will have
|
|
|
|
// the effect of resolving Identifier during template instantiation.
|
2011-07-27 09:05:24 +08:00
|
|
|
// The goal is to be able to resolve a function call whose
|
|
|
|
// nested-name-specifier is located inside a dependent base class.
|
2018-07-31 03:24:48 +08:00
|
|
|
// Example:
|
2011-07-27 09:05:24 +08:00
|
|
|
//
|
|
|
|
// class C {
|
|
|
|
// public:
|
|
|
|
// static void foo2() { }
|
|
|
|
// };
|
|
|
|
// template <class T> class A { public: typedef C D; };
|
|
|
|
//
|
|
|
|
// template <class T> class B : public A<T> {
|
|
|
|
// public:
|
|
|
|
// void foo() { D::foo2(); }
|
|
|
|
// };
|
2014-01-14 20:51:41 +08:00
|
|
|
if (getLangOpts().MSVCCompat) {
|
2011-07-27 09:05:24 +08:00
|
|
|
DeclContext *DC = LookupCtx ? LookupCtx : CurContext;
|
|
|
|
if (DC->isDependentContext() && DC->isFunctionOrMethod()) {
|
2014-08-15 07:34:52 +08:00
|
|
|
CXXRecordDecl *ContainingClass = dyn_cast<CXXRecordDecl>(DC->getParent());
|
|
|
|
if (ContainingClass && ContainingClass->hasAnyDependentBases()) {
|
2016-08-08 12:02:15 +08:00
|
|
|
Diag(IdInfo.IdentifierLoc,
|
|
|
|
diag::ext_undeclared_unqual_id_with_dependent_base)
|
|
|
|
<< IdInfo.Identifier << ContainingClass;
|
|
|
|
SS.Extend(Context, IdInfo.Identifier, IdInfo.IdentifierLoc,
|
|
|
|
IdInfo.CCLoc);
|
2014-08-15 07:34:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
2011-07-27 09:05:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 14:54:23 +08:00
|
|
|
if (!Found.empty()) {
|
|
|
|
if (TypeDecl *TD = Found.getAsSingle<TypeDecl>())
|
2016-08-08 12:02:15 +08:00
|
|
|
Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace)
|
2016-10-28 08:15:24 +08:00
|
|
|
<< Context.getTypeDeclType(TD) << getLangOpts().CPlusPlus;
|
2014-02-09 14:54:23 +08:00
|
|
|
else {
|
2016-08-08 12:02:15 +08:00
|
|
|
Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace)
|
|
|
|
<< IdInfo.Identifier << getLangOpts().CPlusPlus;
|
2014-02-09 14:54:23 +08:00
|
|
|
if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
|
2016-08-08 12:02:15 +08:00
|
|
|
Diag(ND->getLocation(), diag::note_entity_declared_at)
|
|
|
|
<< IdInfo.Identifier;
|
2014-02-09 14:54:23 +08:00
|
|
|
}
|
|
|
|
} else if (SS.isSet())
|
2016-08-08 12:02:15 +08:00
|
|
|
Diag(IdInfo.IdentifierLoc, diag::err_no_member) << IdInfo.Identifier
|
|
|
|
<< LookupCtx << SS.getRange();
|
2009-02-15 04:20:19 +08:00
|
|
|
else
|
2016-08-08 12:02:15 +08:00
|
|
|
Diag(IdInfo.IdentifierLoc, diag::err_undeclared_var_use)
|
|
|
|
<< IdInfo.Identifier;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-24 08:17:56 +08:00
|
|
|
return true;
|
2009-02-15 04:20:19 +08:00
|
|
|
}
|
|
|
|
|
2017-03-18 05:41:20 +08:00
|
|
|
bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, NestedNameSpecInfo &IdInfo,
|
|
|
|
bool EnteringContext, CXXScopeSpec &SS,
|
2014-04-14 00:52:03 +08:00
|
|
|
bool ErrorRecoveryLookup,
|
2017-03-18 05:41:20 +08:00
|
|
|
bool *IsCorrectedToColon,
|
|
|
|
bool OnlyNamespace) {
|
2011-02-24 08:17:56 +08:00
|
|
|
if (SS.isInvalid())
|
|
|
|
return true;
|
2014-04-14 00:52:03 +08:00
|
|
|
|
2017-03-18 05:41:20 +08:00
|
|
|
return BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS,
|
2014-05-26 14:22:03 +08:00
|
|
|
/*ScopeLookupResult=*/nullptr, false,
|
2017-03-18 05:41:20 +08:00
|
|
|
IsCorrectedToColon, OnlyNamespace);
|
2009-12-07 09:36:53 +08:00
|
|
|
}
|
|
|
|
|
2011-12-04 13:04:18 +08:00
|
|
|
bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS,
|
|
|
|
const DeclSpec &DS,
|
|
|
|
SourceLocation ColonColonLoc) {
|
|
|
|
if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
assert(DS.getTypeSpecType() == DeclSpec::TST_decltype);
|
|
|
|
|
|
|
|
QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
|
2018-07-07 08:17:25 +08:00
|
|
|
if (T.isNull())
|
|
|
|
return true;
|
|
|
|
|
2011-12-04 13:04:18 +08:00
|
|
|
if (!T->isDependentType() && !T->getAs<TagType>()) {
|
2018-07-31 03:24:48 +08:00
|
|
|
Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class_or_namespace)
|
2012-03-11 15:00:24 +08:00
|
|
|
<< T << getLangOpts().CPlusPlus;
|
2011-12-04 13:04:18 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeLocBuilder TLB;
|
|
|
|
DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T);
|
|
|
|
DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc());
|
|
|
|
SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
|
|
|
|
ColonColonLoc);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-07 09:36:53 +08:00
|
|
|
/// IsInvalidUnlessNestedName - This method is used for error recovery
|
|
|
|
/// purposes to determine whether the specified identifier is only valid as
|
|
|
|
/// a nested name specifier, for example a namespace name. It is
|
|
|
|
/// conservatively correct to always return false from this method.
|
|
|
|
///
|
|
|
|
/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
|
2010-04-09 00:38:48 +08:00
|
|
|
bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
|
2016-08-08 12:02:15 +08:00
|
|
|
NestedNameSpecInfo &IdInfo,
|
2009-12-07 09:36:53 +08:00
|
|
|
bool EnteringContext) {
|
2011-02-24 08:17:56 +08:00
|
|
|
if (SS.isInvalid())
|
|
|
|
return false;
|
2014-05-26 14:22:03 +08:00
|
|
|
|
2016-08-08 12:02:15 +08:00
|
|
|
return !BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS,
|
2014-05-26 14:22:03 +08:00
|
|
|
/*ScopeLookupResult=*/nullptr, true);
|
2009-09-04 05:38:09 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 08:17:56 +08:00
|
|
|
bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
|
2012-01-27 17:46:47 +08:00
|
|
|
CXXScopeSpec &SS,
|
|
|
|
SourceLocation TemplateKWLoc,
|
2011-02-28 08:04:36 +08:00
|
|
|
TemplateTy Template,
|
|
|
|
SourceLocation TemplateNameLoc,
|
|
|
|
SourceLocation LAngleLoc,
|
|
|
|
ASTTemplateArgsPtr TemplateArgsIn,
|
|
|
|
SourceLocation RAngleLoc,
|
2011-02-24 08:17:56 +08:00
|
|
|
SourceLocation CCLoc,
|
2011-02-28 08:04:36 +08:00
|
|
|
bool EnteringContext) {
|
2011-02-24 08:17:56 +08:00
|
|
|
if (SS.isInvalid())
|
|
|
|
return true;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-02-28 08:04:36 +08:00
|
|
|
// Translate the parser's template argument list in our AST format.
|
|
|
|
TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
|
|
|
|
translateTemplateArguments(TemplateArgsIn, TemplateArgs);
|
|
|
|
|
2013-12-04 08:28:23 +08:00
|
|
|
DependentTemplateName *DTN = Template.get().getAsDependentTemplateName();
|
|
|
|
if (DTN && DTN->isIdentifier()) {
|
2011-02-28 08:04:36 +08:00
|
|
|
// Handle a dependent template specialization for which we cannot resolve
|
|
|
|
// the template name.
|
2013-03-26 08:54:11 +08:00
|
|
|
assert(DTN->getQualifier() == SS.getScopeRep());
|
2011-02-28 08:04:36 +08:00
|
|
|
QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
|
2011-03-02 04:11:18 +08:00
|
|
|
DTN->getQualifier(),
|
|
|
|
DTN->getIdentifier(),
|
2011-02-28 08:04:36 +08:00
|
|
|
TemplateArgs);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-02-28 08:04:36 +08:00
|
|
|
// Create source-location information for this type.
|
|
|
|
TypeLocBuilder Builder;
|
2012-02-06 22:41:24 +08:00
|
|
|
DependentTemplateSpecializationTypeLoc SpecTL
|
2011-02-28 08:04:36 +08:00
|
|
|
= Builder.push<DependentTemplateSpecializationTypeLoc>(T);
|
2012-02-06 22:41:24 +08:00
|
|
|
SpecTL.setElaboratedKeywordLoc(SourceLocation());
|
|
|
|
SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
|
2012-02-07 06:45:07 +08:00
|
|
|
SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
|
2012-02-06 22:41:24 +08:00
|
|
|
SpecTL.setTemplateNameLoc(TemplateNameLoc);
|
2011-02-28 08:04:36 +08:00
|
|
|
SpecTL.setLAngleLoc(LAngleLoc);
|
|
|
|
SpecTL.setRAngleLoc(RAngleLoc);
|
|
|
|
for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
|
|
|
|
SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2012-01-27 17:46:47 +08:00
|
|
|
SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
|
2011-02-28 08:04:36 +08:00
|
|
|
CCLoc);
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-04 08:28:23 +08:00
|
|
|
|
2013-12-04 08:47:45 +08:00
|
|
|
TemplateDecl *TD = Template.get().getAsTemplateDecl();
|
2013-12-04 08:28:23 +08:00
|
|
|
if (Template.get().getAsOverloadedTemplate() || DTN ||
|
2013-12-04 08:47:45 +08:00
|
|
|
isa<FunctionTemplateDecl>(TD) || isa<VarTemplateDecl>(TD)) {
|
2011-03-05 05:37:14 +08:00
|
|
|
SourceRange R(TemplateNameLoc, RAngleLoc);
|
|
|
|
if (SS.getRange().isValid())
|
|
|
|
R.setBegin(SS.getRange().getBegin());
|
2013-12-04 08:28:23 +08:00
|
|
|
|
2011-03-05 05:37:14 +08:00
|
|
|
Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier)
|
2013-12-04 08:47:45 +08:00
|
|
|
<< (TD && isa<VarTemplateDecl>(TD)) << Template.get() << R;
|
2011-03-05 05:37:14 +08:00
|
|
|
NoteAllFoundTemplates(Template.get());
|
|
|
|
return true;
|
|
|
|
}
|
2013-12-04 08:28:23 +08:00
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
// We were able to resolve the template name to an actual template.
|
2011-02-28 08:04:36 +08:00
|
|
|
// Build an appropriate nested-name-specifier.
|
2017-01-20 05:00:13 +08:00
|
|
|
QualType T =
|
|
|
|
CheckTemplateIdType(Template.get(), TemplateNameLoc, TemplateArgs);
|
2011-02-24 08:17:56 +08:00
|
|
|
if (T.isNull())
|
|
|
|
return true;
|
|
|
|
|
2011-05-06 05:57:07 +08:00
|
|
|
// Alias template specializations can produce types which are not valid
|
|
|
|
// nested name specifiers.
|
|
|
|
if (!T->isDependentType() && !T->getAs<TagType>()) {
|
|
|
|
Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T;
|
|
|
|
NoteAllFoundTemplates(Template.get());
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-28 08:04:36 +08:00
|
|
|
|
2012-02-06 22:41:24 +08:00
|
|
|
// Provide source-location information for the template specialization type.
|
2011-02-28 08:04:36 +08:00
|
|
|
TypeLocBuilder Builder;
|
2012-02-06 22:41:24 +08:00
|
|
|
TemplateSpecializationTypeLoc SpecTL
|
2011-02-28 08:04:36 +08:00
|
|
|
= Builder.push<TemplateSpecializationTypeLoc>(T);
|
2012-02-06 22:41:24 +08:00
|
|
|
SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
|
|
|
|
SpecTL.setTemplateNameLoc(TemplateNameLoc);
|
2011-02-28 08:04:36 +08:00
|
|
|
SpecTL.setLAngleLoc(LAngleLoc);
|
|
|
|
SpecTL.setRAngleLoc(RAngleLoc);
|
|
|
|
for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
|
|
|
|
SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
|
|
|
|
|
|
|
|
|
2012-01-27 17:46:47 +08:00
|
|
|
SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
|
2011-02-28 08:04:36 +08:00
|
|
|
CCLoc);
|
2011-02-24 08:17:56 +08:00
|
|
|
return false;
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
}
|
|
|
|
|
2011-02-25 01:54:50 +08:00
|
|
|
namespace {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A structure that stores a nested-name-specifier annotation,
|
2018-07-31 03:24:48 +08:00
|
|
|
/// including both the nested-name-specifier
|
2011-02-25 01:54:50 +08:00
|
|
|
struct NestedNameSpecifierAnnotation {
|
|
|
|
NestedNameSpecifier *NNS;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) {
|
|
|
|
if (SS.isEmpty() || SS.isInvalid())
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2016-10-20 22:27:22 +08:00
|
|
|
void *Mem = Context.Allocate(
|
|
|
|
(sizeof(NestedNameSpecifierAnnotation) + SS.location_size()),
|
|
|
|
alignof(NestedNameSpecifierAnnotation));
|
2011-02-25 01:54:50 +08:00
|
|
|
NestedNameSpecifierAnnotation *Annotation
|
|
|
|
= new (Mem) NestedNameSpecifierAnnotation;
|
|
|
|
Annotation->NNS = SS.getScopeRep();
|
|
|
|
memcpy(Annotation + 1, SS.location_data(), SS.location_size());
|
|
|
|
return Annotation;
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
|
2011-02-25 01:54:50 +08:00
|
|
|
SourceRange AnnotationRange,
|
|
|
|
CXXScopeSpec &SS) {
|
|
|
|
if (!AnnotationPtr) {
|
|
|
|
SS.SetInvalid(AnnotationRange);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-02-25 01:54:50 +08:00
|
|
|
NestedNameSpecifierAnnotation *Annotation
|
|
|
|
= static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr);
|
|
|
|
SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1));
|
|
|
|
}
|
|
|
|
|
2009-12-12 04:04:54 +08:00
|
|
|
bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
|
|
|
|
assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
|
|
|
|
|
2016-12-07 18:24:44 +08:00
|
|
|
// Don't enter a declarator context when the current context is an Objective-C
|
|
|
|
// declaration.
|
|
|
|
if (isa<ObjCContainerDecl>(CurContext) || isa<ObjCMethodDecl>(CurContext))
|
|
|
|
return false;
|
|
|
|
|
2013-03-26 08:54:11 +08:00
|
|
|
NestedNameSpecifier *Qualifier = SS.getScopeRep();
|
2009-12-12 04:04:54 +08:00
|
|
|
|
|
|
|
// There are only two places a well-formed program may qualify a
|
|
|
|
// declarator: first, when defining a namespace or class member
|
|
|
|
// out-of-line, and second, when naming an explicitly-qualified
|
|
|
|
// friend function. The latter case is governed by
|
|
|
|
// C++03 [basic.lookup.unqual]p10:
|
|
|
|
// In a friend declaration naming a member function, a name used
|
|
|
|
// in the function declarator and not part of a template-argument
|
|
|
|
// in a template-id is first looked up in the scope of the member
|
|
|
|
// function's class. If it is not found, or if the name is part of
|
|
|
|
// a template-argument in a template-id, the look up is as
|
|
|
|
// described for unqualified names in the definition of the class
|
|
|
|
// granting friendship.
|
|
|
|
// i.e. we don't push a scope unless it's a class member.
|
|
|
|
|
|
|
|
switch (Qualifier->getKind()) {
|
|
|
|
case NestedNameSpecifier::Global:
|
|
|
|
case NestedNameSpecifier::Namespace:
|
2011-02-24 10:36:08 +08:00
|
|
|
case NestedNameSpecifier::NamespaceAlias:
|
2009-12-12 04:04:54 +08:00
|
|
|
// These are always namespace scopes. We never want to enter a
|
|
|
|
// namespace scope from anything but a file context.
|
2010-08-31 08:36:30 +08:00
|
|
|
return CurContext->getRedeclContext()->isFileContext();
|
2009-12-12 04:04:54 +08:00
|
|
|
|
|
|
|
case NestedNameSpecifier::Identifier:
|
|
|
|
case NestedNameSpecifier::TypeSpec:
|
|
|
|
case NestedNameSpecifier::TypeSpecWithTemplate:
|
2014-09-26 08:28:20 +08:00
|
|
|
case NestedNameSpecifier::Super:
|
2009-12-12 04:04:54 +08:00
|
|
|
// These are never namespace scopes.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-17 14:56:22 +08:00
|
|
|
llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
|
2009-12-12 04:04:54 +08:00
|
|
|
}
|
|
|
|
|
2009-02-15 04:20:19 +08:00
|
|
|
/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
|
|
|
|
/// scope or nested-name-specifier) is parsed, part of a declarator-id.
|
|
|
|
/// After this method is called, according to [C++ 3.4.3p3], names should be
|
|
|
|
/// looked up in the declarator-id's scope, until the declarator is parsed and
|
|
|
|
/// ActOnCXXExitDeclaratorScope is called.
|
|
|
|
/// The 'SS' should be a non-empty valid CXXScopeSpec.
|
2010-04-09 00:38:48 +08:00
|
|
|
bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
|
2009-02-15 04:20:19 +08:00
|
|
|
assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
|
2009-12-19 18:49:29 +08:00
|
|
|
|
|
|
|
if (SS.isInvalid()) return true;
|
|
|
|
|
|
|
|
DeclContext *DC = computeDeclContext(SS, true);
|
|
|
|
if (!DC) return true;
|
|
|
|
|
|
|
|
// Before we enter a declarator's context, we need to make sure that
|
|
|
|
// it is a complete declaration context.
|
2010-05-01 08:40:08 +08:00
|
|
|
if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC))
|
2009-12-19 18:49:29 +08:00
|
|
|
return true;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2009-12-19 18:49:29 +08:00
|
|
|
EnterDeclaratorContext(S, DC);
|
2010-04-27 08:57:59 +08:00
|
|
|
|
|
|
|
// Rebuild the nested name specifier for the new scope.
|
|
|
|
if (DC->isDependentContext())
|
|
|
|
RebuildNestedNameSpecifierInCurrentInstantiation(SS);
|
|
|
|
|
2009-09-25 07:39:01 +08:00
|
|
|
return false;
|
2009-02-15 04:20:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
|
|
|
|
/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
|
|
|
|
/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
|
|
|
|
/// Used to indicate that names should revert to being looked up in the
|
|
|
|
/// defining scope.
|
|
|
|
void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
|
|
|
|
assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
|
2009-08-26 08:04:55 +08:00
|
|
|
if (SS.isInvalid())
|
|
|
|
return;
|
2009-12-19 18:49:29 +08:00
|
|
|
assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
|
|
|
|
"exiting declarator scope we never really entered");
|
|
|
|
ExitDeclaratorContext(S);
|
2009-02-15 04:20:19 +08:00
|
|
|
}
|