1f4a2713aSLionel Sambuc //===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements C++ semantic analysis for scope specifiers.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h"
15f4a2713aSLionel Sambuc #include "TypeLocBuilder.h"
16f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
17f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
18f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
19f4a2713aSLionel Sambuc #include "clang/AST/NestedNameSpecifier.h"
20f4a2713aSLionel Sambuc #include "clang/Basic/PartialDiagnostic.h"
21f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h"
22f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h"
23f4a2713aSLionel Sambuc #include "clang/Sema/Template.h"
24f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
25f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
26f4a2713aSLionel Sambuc using namespace clang;
27f4a2713aSLionel Sambuc
28f4a2713aSLionel Sambuc /// \brief Find the current instantiation that associated with the given type.
getCurrentInstantiationOf(QualType T,DeclContext * CurContext)29f4a2713aSLionel Sambuc static CXXRecordDecl *getCurrentInstantiationOf(QualType T,
30f4a2713aSLionel Sambuc DeclContext *CurContext) {
31f4a2713aSLionel Sambuc if (T.isNull())
32*0a6a1f1dSLionel Sambuc return nullptr;
33f4a2713aSLionel Sambuc
34f4a2713aSLionel Sambuc const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
35f4a2713aSLionel Sambuc if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
36f4a2713aSLionel Sambuc CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
37f4a2713aSLionel Sambuc if (!Record->isDependentContext() ||
38f4a2713aSLionel Sambuc Record->isCurrentInstantiation(CurContext))
39f4a2713aSLionel Sambuc return Record;
40f4a2713aSLionel Sambuc
41*0a6a1f1dSLionel Sambuc return nullptr;
42f4a2713aSLionel Sambuc } else if (isa<InjectedClassNameType>(Ty))
43f4a2713aSLionel Sambuc return cast<InjectedClassNameType>(Ty)->getDecl();
44f4a2713aSLionel Sambuc else
45*0a6a1f1dSLionel Sambuc return nullptr;
46f4a2713aSLionel Sambuc }
47f4a2713aSLionel Sambuc
48f4a2713aSLionel Sambuc /// \brief Compute the DeclContext that is associated with the given type.
49f4a2713aSLionel Sambuc ///
50f4a2713aSLionel Sambuc /// \param T the type for which we are attempting to find a DeclContext.
51f4a2713aSLionel Sambuc ///
52f4a2713aSLionel Sambuc /// \returns the declaration context represented by the type T,
53f4a2713aSLionel Sambuc /// or NULL if the declaration context cannot be computed (e.g., because it is
54f4a2713aSLionel Sambuc /// dependent and not the current instantiation).
computeDeclContext(QualType T)55f4a2713aSLionel Sambuc DeclContext *Sema::computeDeclContext(QualType T) {
56f4a2713aSLionel Sambuc if (!T->isDependentType())
57f4a2713aSLionel Sambuc if (const TagType *Tag = T->getAs<TagType>())
58f4a2713aSLionel Sambuc return Tag->getDecl();
59f4a2713aSLionel Sambuc
60f4a2713aSLionel Sambuc return ::getCurrentInstantiationOf(T, CurContext);
61f4a2713aSLionel Sambuc }
62f4a2713aSLionel Sambuc
63f4a2713aSLionel Sambuc /// \brief Compute the DeclContext that is associated with the given
64f4a2713aSLionel Sambuc /// scope specifier.
65f4a2713aSLionel Sambuc ///
66f4a2713aSLionel Sambuc /// \param SS the C++ scope specifier as it appears in the source
67f4a2713aSLionel Sambuc ///
68f4a2713aSLionel Sambuc /// \param EnteringContext when true, we will be entering the context of
69f4a2713aSLionel Sambuc /// this scope specifier, so we can retrieve the declaration context of a
70f4a2713aSLionel Sambuc /// class template or class template partial specialization even if it is
71f4a2713aSLionel Sambuc /// not the current instantiation.
72f4a2713aSLionel Sambuc ///
73f4a2713aSLionel Sambuc /// \returns the declaration context represented by the scope specifier @p SS,
74f4a2713aSLionel Sambuc /// or NULL if the declaration context cannot be computed (e.g., because it is
75f4a2713aSLionel Sambuc /// dependent and not the current instantiation).
computeDeclContext(const CXXScopeSpec & SS,bool EnteringContext)76f4a2713aSLionel Sambuc DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS,
77f4a2713aSLionel Sambuc bool EnteringContext) {
78f4a2713aSLionel Sambuc if (!SS.isSet() || SS.isInvalid())
79*0a6a1f1dSLionel Sambuc return nullptr;
80f4a2713aSLionel Sambuc
81f4a2713aSLionel Sambuc NestedNameSpecifier *NNS = SS.getScopeRep();
82f4a2713aSLionel Sambuc if (NNS->isDependent()) {
83f4a2713aSLionel Sambuc // If this nested-name-specifier refers to the current
84f4a2713aSLionel Sambuc // instantiation, return its DeclContext.
85f4a2713aSLionel Sambuc if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS))
86f4a2713aSLionel Sambuc return Record;
87f4a2713aSLionel Sambuc
88f4a2713aSLionel Sambuc if (EnteringContext) {
89f4a2713aSLionel Sambuc const Type *NNSType = NNS->getAsType();
90f4a2713aSLionel Sambuc if (!NNSType) {
91*0a6a1f1dSLionel Sambuc return nullptr;
92f4a2713aSLionel Sambuc }
93f4a2713aSLionel Sambuc
94f4a2713aSLionel Sambuc // Look through type alias templates, per C++0x [temp.dep.type]p1.
95f4a2713aSLionel Sambuc NNSType = Context.getCanonicalType(NNSType);
96f4a2713aSLionel Sambuc if (const TemplateSpecializationType *SpecType
97f4a2713aSLionel Sambuc = NNSType->getAs<TemplateSpecializationType>()) {
98f4a2713aSLionel Sambuc // We are entering the context of the nested name specifier, so try to
99f4a2713aSLionel Sambuc // match the nested name specifier to either a primary class template
100f4a2713aSLionel Sambuc // or a class template partial specialization.
101f4a2713aSLionel Sambuc if (ClassTemplateDecl *ClassTemplate
102f4a2713aSLionel Sambuc = dyn_cast_or_null<ClassTemplateDecl>(
103f4a2713aSLionel Sambuc SpecType->getTemplateName().getAsTemplateDecl())) {
104f4a2713aSLionel Sambuc QualType ContextType
105f4a2713aSLionel Sambuc = Context.getCanonicalType(QualType(SpecType, 0));
106f4a2713aSLionel Sambuc
107f4a2713aSLionel Sambuc // If the type of the nested name specifier is the same as the
108f4a2713aSLionel Sambuc // injected class name of the named class template, we're entering
109f4a2713aSLionel Sambuc // into that class template definition.
110f4a2713aSLionel Sambuc QualType Injected
111f4a2713aSLionel Sambuc = ClassTemplate->getInjectedClassNameSpecialization();
112f4a2713aSLionel Sambuc if (Context.hasSameType(Injected, ContextType))
113f4a2713aSLionel Sambuc return ClassTemplate->getTemplatedDecl();
114f4a2713aSLionel Sambuc
115f4a2713aSLionel Sambuc // If the type of the nested name specifier is the same as the
116f4a2713aSLionel Sambuc // type of one of the class template's class template partial
117f4a2713aSLionel Sambuc // specializations, we're entering into the definition of that
118f4a2713aSLionel Sambuc // class template partial specialization.
119f4a2713aSLionel Sambuc if (ClassTemplatePartialSpecializationDecl *PartialSpec
120f4a2713aSLionel Sambuc = ClassTemplate->findPartialSpecialization(ContextType))
121f4a2713aSLionel Sambuc return PartialSpec;
122f4a2713aSLionel Sambuc }
123f4a2713aSLionel Sambuc } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) {
124f4a2713aSLionel Sambuc // The nested name specifier refers to a member of a class template.
125f4a2713aSLionel Sambuc return RecordT->getDecl();
126f4a2713aSLionel Sambuc }
127f4a2713aSLionel Sambuc }
128f4a2713aSLionel Sambuc
129*0a6a1f1dSLionel Sambuc return nullptr;
130f4a2713aSLionel Sambuc }
131f4a2713aSLionel Sambuc
132f4a2713aSLionel Sambuc switch (NNS->getKind()) {
133f4a2713aSLionel Sambuc case NestedNameSpecifier::Identifier:
134f4a2713aSLionel Sambuc llvm_unreachable("Dependent nested-name-specifier has no DeclContext");
135f4a2713aSLionel Sambuc
136f4a2713aSLionel Sambuc case NestedNameSpecifier::Namespace:
137f4a2713aSLionel Sambuc return NNS->getAsNamespace();
138f4a2713aSLionel Sambuc
139f4a2713aSLionel Sambuc case NestedNameSpecifier::NamespaceAlias:
140f4a2713aSLionel Sambuc return NNS->getAsNamespaceAlias()->getNamespace();
141f4a2713aSLionel Sambuc
142f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpec:
143f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpecWithTemplate: {
144f4a2713aSLionel Sambuc const TagType *Tag = NNS->getAsType()->getAs<TagType>();
145f4a2713aSLionel Sambuc assert(Tag && "Non-tag type in nested-name-specifier");
146f4a2713aSLionel Sambuc return Tag->getDecl();
147f4a2713aSLionel Sambuc }
148f4a2713aSLionel Sambuc
149f4a2713aSLionel Sambuc case NestedNameSpecifier::Global:
150f4a2713aSLionel Sambuc return Context.getTranslationUnitDecl();
151*0a6a1f1dSLionel Sambuc
152*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::Super:
153*0a6a1f1dSLionel Sambuc return NNS->getAsRecordDecl();
154f4a2713aSLionel Sambuc }
155f4a2713aSLionel Sambuc
156f4a2713aSLionel Sambuc llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
157f4a2713aSLionel Sambuc }
158f4a2713aSLionel Sambuc
isDependentScopeSpecifier(const CXXScopeSpec & SS)159f4a2713aSLionel Sambuc bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) {
160f4a2713aSLionel Sambuc if (!SS.isSet() || SS.isInvalid())
161f4a2713aSLionel Sambuc return false;
162f4a2713aSLionel Sambuc
163f4a2713aSLionel Sambuc return SS.getScopeRep()->isDependent();
164f4a2713aSLionel Sambuc }
165f4a2713aSLionel Sambuc
166f4a2713aSLionel Sambuc /// \brief If the given nested name specifier refers to the current
167f4a2713aSLionel Sambuc /// instantiation, return the declaration that corresponds to that
168f4a2713aSLionel Sambuc /// current instantiation (C++0x [temp.dep.type]p1).
169f4a2713aSLionel Sambuc ///
170f4a2713aSLionel Sambuc /// \param NNS a dependent nested name specifier.
getCurrentInstantiationOf(NestedNameSpecifier * NNS)171f4a2713aSLionel Sambuc CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) {
172f4a2713aSLionel Sambuc assert(getLangOpts().CPlusPlus && "Only callable in C++");
173f4a2713aSLionel Sambuc assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed");
174f4a2713aSLionel Sambuc
175f4a2713aSLionel Sambuc if (!NNS->getAsType())
176*0a6a1f1dSLionel Sambuc return nullptr;
177f4a2713aSLionel Sambuc
178f4a2713aSLionel Sambuc QualType T = QualType(NNS->getAsType(), 0);
179f4a2713aSLionel Sambuc return ::getCurrentInstantiationOf(T, CurContext);
180f4a2713aSLionel Sambuc }
181f4a2713aSLionel Sambuc
182f4a2713aSLionel Sambuc /// \brief Require that the context specified by SS be complete.
183f4a2713aSLionel Sambuc ///
184f4a2713aSLionel Sambuc /// If SS refers to a type, this routine checks whether the type is
185f4a2713aSLionel Sambuc /// complete enough (or can be made complete enough) for name lookup
186f4a2713aSLionel Sambuc /// into the DeclContext. A type that is not yet completed can be
187f4a2713aSLionel Sambuc /// considered "complete enough" if it is a class/struct/union/enum
188f4a2713aSLionel Sambuc /// that is currently being defined. Or, if we have a type that names
189f4a2713aSLionel Sambuc /// a class template specialization that is not a complete type, we
190f4a2713aSLionel Sambuc /// will attempt to instantiate that class template.
RequireCompleteDeclContext(CXXScopeSpec & SS,DeclContext * DC)191f4a2713aSLionel Sambuc bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS,
192f4a2713aSLionel Sambuc DeclContext *DC) {
193*0a6a1f1dSLionel Sambuc assert(DC && "given null context");
194f4a2713aSLionel Sambuc
195f4a2713aSLionel Sambuc TagDecl *tag = dyn_cast<TagDecl>(DC);
196f4a2713aSLionel Sambuc
197f4a2713aSLionel Sambuc // If this is a dependent type, then we consider it complete.
198f4a2713aSLionel Sambuc if (!tag || tag->isDependentContext())
199f4a2713aSLionel Sambuc return false;
200f4a2713aSLionel Sambuc
201f4a2713aSLionel Sambuc // If we're currently defining this type, then lookup into the
202f4a2713aSLionel Sambuc // type is okay: don't complain that it isn't complete yet.
203f4a2713aSLionel Sambuc QualType type = Context.getTypeDeclType(tag);
204f4a2713aSLionel Sambuc const TagType *tagType = type->getAs<TagType>();
205f4a2713aSLionel Sambuc if (tagType && tagType->isBeingDefined())
206f4a2713aSLionel Sambuc return false;
207f4a2713aSLionel Sambuc
208f4a2713aSLionel Sambuc SourceLocation loc = SS.getLastQualifierNameLoc();
209f4a2713aSLionel Sambuc if (loc.isInvalid()) loc = SS.getRange().getBegin();
210f4a2713aSLionel Sambuc
211f4a2713aSLionel Sambuc // The type must be complete.
212f4a2713aSLionel Sambuc if (RequireCompleteType(loc, type, diag::err_incomplete_nested_name_spec,
213f4a2713aSLionel Sambuc SS.getRange())) {
214f4a2713aSLionel Sambuc SS.SetInvalid(SS.getRange());
215f4a2713aSLionel Sambuc return true;
216f4a2713aSLionel Sambuc }
217f4a2713aSLionel Sambuc
218f4a2713aSLionel Sambuc // Fixed enum types are complete, but they aren't valid as scopes
219f4a2713aSLionel Sambuc // until we see a definition, so awkwardly pull out this special
220f4a2713aSLionel Sambuc // case.
221f4a2713aSLionel Sambuc const EnumType *enumType = dyn_cast_or_null<EnumType>(tagType);
222f4a2713aSLionel Sambuc if (!enumType || enumType->getDecl()->isCompleteDefinition())
223f4a2713aSLionel Sambuc return false;
224f4a2713aSLionel Sambuc
225f4a2713aSLionel Sambuc // Try to instantiate the definition, if this is a specialization of an
226f4a2713aSLionel Sambuc // enumeration temploid.
227f4a2713aSLionel Sambuc EnumDecl *ED = enumType->getDecl();
228f4a2713aSLionel Sambuc if (EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
229f4a2713aSLionel Sambuc MemberSpecializationInfo *MSI = ED->getMemberSpecializationInfo();
230f4a2713aSLionel Sambuc if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) {
231f4a2713aSLionel Sambuc if (InstantiateEnum(loc, ED, Pattern, getTemplateInstantiationArgs(ED),
232f4a2713aSLionel Sambuc TSK_ImplicitInstantiation)) {
233f4a2713aSLionel Sambuc SS.SetInvalid(SS.getRange());
234f4a2713aSLionel Sambuc return true;
235f4a2713aSLionel Sambuc }
236f4a2713aSLionel Sambuc return false;
237f4a2713aSLionel Sambuc }
238f4a2713aSLionel Sambuc }
239f4a2713aSLionel Sambuc
240f4a2713aSLionel Sambuc Diag(loc, diag::err_incomplete_nested_name_spec)
241f4a2713aSLionel Sambuc << type << SS.getRange();
242f4a2713aSLionel Sambuc SS.SetInvalid(SS.getRange());
243f4a2713aSLionel Sambuc return true;
244f4a2713aSLionel Sambuc }
245f4a2713aSLionel Sambuc
ActOnCXXGlobalScopeSpecifier(SourceLocation CCLoc,CXXScopeSpec & SS)246*0a6a1f1dSLionel Sambuc bool Sema::ActOnCXXGlobalScopeSpecifier(SourceLocation CCLoc,
247f4a2713aSLionel Sambuc CXXScopeSpec &SS) {
248f4a2713aSLionel Sambuc SS.MakeGlobal(Context, CCLoc);
249f4a2713aSLionel Sambuc return false;
250f4a2713aSLionel Sambuc }
251f4a2713aSLionel Sambuc
ActOnSuperScopeSpecifier(SourceLocation SuperLoc,SourceLocation ColonColonLoc,CXXScopeSpec & SS)252*0a6a1f1dSLionel Sambuc bool Sema::ActOnSuperScopeSpecifier(SourceLocation SuperLoc,
253*0a6a1f1dSLionel Sambuc SourceLocation ColonColonLoc,
254*0a6a1f1dSLionel Sambuc CXXScopeSpec &SS) {
255*0a6a1f1dSLionel Sambuc CXXRecordDecl *RD = nullptr;
256*0a6a1f1dSLionel Sambuc for (Scope *S = getCurScope(); S; S = S->getParent()) {
257*0a6a1f1dSLionel Sambuc if (S->isFunctionScope()) {
258*0a6a1f1dSLionel Sambuc if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(S->getEntity()))
259*0a6a1f1dSLionel Sambuc RD = MD->getParent();
260*0a6a1f1dSLionel Sambuc break;
261*0a6a1f1dSLionel Sambuc }
262*0a6a1f1dSLionel Sambuc if (S->isClassScope()) {
263*0a6a1f1dSLionel Sambuc RD = cast<CXXRecordDecl>(S->getEntity());
264*0a6a1f1dSLionel Sambuc break;
265*0a6a1f1dSLionel Sambuc }
266*0a6a1f1dSLionel Sambuc }
267*0a6a1f1dSLionel Sambuc
268*0a6a1f1dSLionel Sambuc if (!RD) {
269*0a6a1f1dSLionel Sambuc Diag(SuperLoc, diag::err_invalid_super_scope);
270*0a6a1f1dSLionel Sambuc return true;
271*0a6a1f1dSLionel Sambuc } else if (RD->isLambda()) {
272*0a6a1f1dSLionel Sambuc Diag(SuperLoc, diag::err_super_in_lambda_unsupported);
273*0a6a1f1dSLionel Sambuc return true;
274*0a6a1f1dSLionel Sambuc } else if (RD->getNumBases() == 0) {
275*0a6a1f1dSLionel Sambuc Diag(SuperLoc, diag::err_no_base_classes) << RD->getName();
276*0a6a1f1dSLionel Sambuc return true;
277*0a6a1f1dSLionel Sambuc }
278*0a6a1f1dSLionel Sambuc
279*0a6a1f1dSLionel Sambuc SS.MakeSuper(Context, RD, SuperLoc, ColonColonLoc);
280*0a6a1f1dSLionel Sambuc return false;
281*0a6a1f1dSLionel Sambuc }
282*0a6a1f1dSLionel Sambuc
283f4a2713aSLionel Sambuc /// \brief Determines whether the given declaration is an valid acceptable
284f4a2713aSLionel Sambuc /// result for name lookup of a nested-name-specifier.
isAcceptableNestedNameSpecifier(const NamedDecl * SD)285f4a2713aSLionel Sambuc bool Sema::isAcceptableNestedNameSpecifier(const NamedDecl *SD) {
286f4a2713aSLionel Sambuc if (!SD)
287f4a2713aSLionel Sambuc return false;
288f4a2713aSLionel Sambuc
289f4a2713aSLionel Sambuc // Namespace and namespace aliases are fine.
290f4a2713aSLionel Sambuc if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD))
291f4a2713aSLionel Sambuc return true;
292f4a2713aSLionel Sambuc
293f4a2713aSLionel Sambuc if (!isa<TypeDecl>(SD))
294f4a2713aSLionel Sambuc return false;
295f4a2713aSLionel Sambuc
296f4a2713aSLionel Sambuc // Determine whether we have a class (or, in C++11, an enum) or
297f4a2713aSLionel Sambuc // a typedef thereof. If so, build the nested-name-specifier.
298f4a2713aSLionel Sambuc QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
299f4a2713aSLionel Sambuc if (T->isDependentType())
300f4a2713aSLionel Sambuc return true;
301f4a2713aSLionel Sambuc else if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
302f4a2713aSLionel Sambuc if (TD->getUnderlyingType()->isRecordType() ||
303f4a2713aSLionel Sambuc (Context.getLangOpts().CPlusPlus11 &&
304f4a2713aSLionel Sambuc TD->getUnderlyingType()->isEnumeralType()))
305f4a2713aSLionel Sambuc return true;
306f4a2713aSLionel Sambuc } else if (isa<RecordDecl>(SD) ||
307f4a2713aSLionel Sambuc (Context.getLangOpts().CPlusPlus11 && isa<EnumDecl>(SD)))
308f4a2713aSLionel Sambuc return true;
309f4a2713aSLionel Sambuc
310f4a2713aSLionel Sambuc return false;
311f4a2713aSLionel Sambuc }
312f4a2713aSLionel Sambuc
313f4a2713aSLionel Sambuc /// \brief If the given nested-name-specifier begins with a bare identifier
314f4a2713aSLionel Sambuc /// (e.g., Base::), perform name lookup for that identifier as a
315f4a2713aSLionel Sambuc /// nested-name-specifier within the given scope, and return the result of that
316f4a2713aSLionel Sambuc /// name lookup.
FindFirstQualifierInScope(Scope * S,NestedNameSpecifier * NNS)317f4a2713aSLionel Sambuc NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) {
318f4a2713aSLionel Sambuc if (!S || !NNS)
319*0a6a1f1dSLionel Sambuc return nullptr;
320f4a2713aSLionel Sambuc
321f4a2713aSLionel Sambuc while (NNS->getPrefix())
322f4a2713aSLionel Sambuc NNS = NNS->getPrefix();
323f4a2713aSLionel Sambuc
324f4a2713aSLionel Sambuc if (NNS->getKind() != NestedNameSpecifier::Identifier)
325*0a6a1f1dSLionel Sambuc return nullptr;
326f4a2713aSLionel Sambuc
327f4a2713aSLionel Sambuc LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(),
328f4a2713aSLionel Sambuc LookupNestedNameSpecifierName);
329f4a2713aSLionel Sambuc LookupName(Found, S);
330f4a2713aSLionel Sambuc assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet");
331f4a2713aSLionel Sambuc
332f4a2713aSLionel Sambuc if (!Found.isSingleResult())
333*0a6a1f1dSLionel Sambuc return nullptr;
334f4a2713aSLionel Sambuc
335f4a2713aSLionel Sambuc NamedDecl *Result = Found.getFoundDecl();
336f4a2713aSLionel Sambuc if (isAcceptableNestedNameSpecifier(Result))
337f4a2713aSLionel Sambuc return Result;
338f4a2713aSLionel Sambuc
339*0a6a1f1dSLionel Sambuc return nullptr;
340f4a2713aSLionel Sambuc }
341f4a2713aSLionel Sambuc
isNonTypeNestedNameSpecifier(Scope * S,CXXScopeSpec & SS,SourceLocation IdLoc,IdentifierInfo & II,ParsedType ObjectTypePtr)342f4a2713aSLionel Sambuc bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS,
343f4a2713aSLionel Sambuc SourceLocation IdLoc,
344f4a2713aSLionel Sambuc IdentifierInfo &II,
345f4a2713aSLionel Sambuc ParsedType ObjectTypePtr) {
346f4a2713aSLionel Sambuc QualType ObjectType = GetTypeFromParser(ObjectTypePtr);
347f4a2713aSLionel Sambuc LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName);
348f4a2713aSLionel Sambuc
349f4a2713aSLionel Sambuc // Determine where to perform name lookup
350*0a6a1f1dSLionel Sambuc DeclContext *LookupCtx = nullptr;
351f4a2713aSLionel Sambuc bool isDependent = false;
352f4a2713aSLionel Sambuc if (!ObjectType.isNull()) {
353f4a2713aSLionel Sambuc // This nested-name-specifier occurs in a member access expression, e.g.,
354f4a2713aSLionel Sambuc // x->B::f, and we are looking into the type of the object.
355f4a2713aSLionel Sambuc assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
356f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(ObjectType);
357f4a2713aSLionel Sambuc isDependent = ObjectType->isDependentType();
358f4a2713aSLionel Sambuc } else if (SS.isSet()) {
359f4a2713aSLionel Sambuc // This nested-name-specifier occurs after another nested-name-specifier,
360f4a2713aSLionel Sambuc // so long into the context associated with the prior nested-name-specifier.
361f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(SS, false);
362f4a2713aSLionel Sambuc isDependent = isDependentScopeSpecifier(SS);
363f4a2713aSLionel Sambuc Found.setContextRange(SS.getRange());
364f4a2713aSLionel Sambuc }
365f4a2713aSLionel Sambuc
366f4a2713aSLionel Sambuc if (LookupCtx) {
367f4a2713aSLionel Sambuc // Perform "qualified" name lookup into the declaration context we
368f4a2713aSLionel Sambuc // computed, which is either the type of the base of a member access
369f4a2713aSLionel Sambuc // expression or the declaration context associated with a prior
370f4a2713aSLionel Sambuc // nested-name-specifier.
371f4a2713aSLionel Sambuc
372f4a2713aSLionel Sambuc // The declaration context must be complete.
373f4a2713aSLionel Sambuc if (!LookupCtx->isDependentContext() &&
374f4a2713aSLionel Sambuc RequireCompleteDeclContext(SS, LookupCtx))
375f4a2713aSLionel Sambuc return false;
376f4a2713aSLionel Sambuc
377f4a2713aSLionel Sambuc LookupQualifiedName(Found, LookupCtx);
378f4a2713aSLionel Sambuc } else if (isDependent) {
379f4a2713aSLionel Sambuc return false;
380f4a2713aSLionel Sambuc } else {
381f4a2713aSLionel Sambuc LookupName(Found, S);
382f4a2713aSLionel Sambuc }
383f4a2713aSLionel Sambuc Found.suppressDiagnostics();
384f4a2713aSLionel Sambuc
385f4a2713aSLionel Sambuc if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
386f4a2713aSLionel Sambuc return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
387f4a2713aSLionel Sambuc
388f4a2713aSLionel Sambuc return false;
389f4a2713aSLionel Sambuc }
390f4a2713aSLionel Sambuc
391f4a2713aSLionel Sambuc namespace {
392f4a2713aSLionel Sambuc
393f4a2713aSLionel Sambuc // Callback to only accept typo corrections that can be a valid C++ member
394f4a2713aSLionel Sambuc // intializer: either a non-static field member or a base class.
395f4a2713aSLionel Sambuc class NestedNameSpecifierValidatorCCC : public CorrectionCandidateCallback {
396f4a2713aSLionel Sambuc public:
NestedNameSpecifierValidatorCCC(Sema & SRef)397f4a2713aSLionel Sambuc explicit NestedNameSpecifierValidatorCCC(Sema &SRef)
398f4a2713aSLionel Sambuc : SRef(SRef) {}
399f4a2713aSLionel Sambuc
ValidateCandidate(const TypoCorrection & candidate)400*0a6a1f1dSLionel Sambuc bool ValidateCandidate(const TypoCorrection &candidate) override {
401f4a2713aSLionel Sambuc return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl());
402f4a2713aSLionel Sambuc }
403f4a2713aSLionel Sambuc
404f4a2713aSLionel Sambuc private:
405f4a2713aSLionel Sambuc Sema &SRef;
406f4a2713aSLionel Sambuc };
407f4a2713aSLionel Sambuc
408f4a2713aSLionel Sambuc }
409f4a2713aSLionel Sambuc
410f4a2713aSLionel Sambuc /// \brief Build a new nested-name-specifier for "identifier::", as described
411f4a2713aSLionel Sambuc /// by ActOnCXXNestedNameSpecifier.
412f4a2713aSLionel Sambuc ///
413*0a6a1f1dSLionel Sambuc /// \param S Scope in which the nested-name-specifier occurs.
414*0a6a1f1dSLionel Sambuc /// \param Identifier Identifier in the sequence "identifier" "::".
415*0a6a1f1dSLionel Sambuc /// \param IdentifierLoc Location of the \p Identifier.
416*0a6a1f1dSLionel Sambuc /// \param CCLoc Location of "::" following Identifier.
417*0a6a1f1dSLionel Sambuc /// \param ObjectType Type of postfix expression if the nested-name-specifier
418*0a6a1f1dSLionel Sambuc /// occurs in construct like: <tt>ptr->nns::f</tt>.
419*0a6a1f1dSLionel Sambuc /// \param EnteringContext If true, enter the context specified by the
420*0a6a1f1dSLionel Sambuc /// nested-name-specifier.
421*0a6a1f1dSLionel Sambuc /// \param SS Optional nested name specifier preceding the identifier.
422*0a6a1f1dSLionel Sambuc /// \param ScopeLookupResult Provides the result of name lookup within the
423*0a6a1f1dSLionel Sambuc /// scope of the nested-name-specifier that was computed at template
424*0a6a1f1dSLionel Sambuc /// definition time.
425*0a6a1f1dSLionel Sambuc /// \param ErrorRecoveryLookup Specifies if the method is called to improve
426*0a6a1f1dSLionel Sambuc /// error recovery and what kind of recovery is performed.
427*0a6a1f1dSLionel Sambuc /// \param IsCorrectedToColon If not null, suggestion of replace '::' -> ':'
428*0a6a1f1dSLionel Sambuc /// are allowed. The bool value pointed by this parameter is set to
429*0a6a1f1dSLionel Sambuc /// 'true' if the identifier is treated as if it was followed by ':',
430*0a6a1f1dSLionel Sambuc /// not '::'.
431*0a6a1f1dSLionel Sambuc ///
432f4a2713aSLionel Sambuc /// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
433f4a2713aSLionel Sambuc /// that it contains an extra parameter \p ScopeLookupResult, which provides
434f4a2713aSLionel Sambuc /// the result of name lookup within the scope of the nested-name-specifier
435f4a2713aSLionel Sambuc /// that was computed at template definition time.
436f4a2713aSLionel Sambuc ///
437f4a2713aSLionel Sambuc /// If ErrorRecoveryLookup is true, then this call is used to improve error
438f4a2713aSLionel Sambuc /// recovery. This means that it should not emit diagnostics, it should
439f4a2713aSLionel Sambuc /// just return true on failure. It also means it should only return a valid
440f4a2713aSLionel Sambuc /// scope if it *knows* that the result is correct. It should not return in a
441f4a2713aSLionel Sambuc /// dependent context, for example. Nor will it extend \p SS with the scope
442f4a2713aSLionel Sambuc /// specifier.
BuildCXXNestedNameSpecifier(Scope * S,IdentifierInfo & Identifier,SourceLocation IdentifierLoc,SourceLocation CCLoc,QualType ObjectType,bool EnteringContext,CXXScopeSpec & SS,NamedDecl * ScopeLookupResult,bool ErrorRecoveryLookup,bool * IsCorrectedToColon)443f4a2713aSLionel Sambuc bool Sema::BuildCXXNestedNameSpecifier(Scope *S,
444f4a2713aSLionel Sambuc IdentifierInfo &Identifier,
445f4a2713aSLionel Sambuc SourceLocation IdentifierLoc,
446f4a2713aSLionel Sambuc SourceLocation CCLoc,
447f4a2713aSLionel Sambuc QualType ObjectType,
448f4a2713aSLionel Sambuc bool EnteringContext,
449f4a2713aSLionel Sambuc CXXScopeSpec &SS,
450f4a2713aSLionel Sambuc NamedDecl *ScopeLookupResult,
451*0a6a1f1dSLionel Sambuc bool ErrorRecoveryLookup,
452*0a6a1f1dSLionel Sambuc bool *IsCorrectedToColon) {
453f4a2713aSLionel Sambuc LookupResult Found(*this, &Identifier, IdentifierLoc,
454f4a2713aSLionel Sambuc LookupNestedNameSpecifierName);
455f4a2713aSLionel Sambuc
456f4a2713aSLionel Sambuc // Determine where to perform name lookup
457*0a6a1f1dSLionel Sambuc DeclContext *LookupCtx = nullptr;
458f4a2713aSLionel Sambuc bool isDependent = false;
459*0a6a1f1dSLionel Sambuc if (IsCorrectedToColon)
460*0a6a1f1dSLionel Sambuc *IsCorrectedToColon = false;
461f4a2713aSLionel Sambuc if (!ObjectType.isNull()) {
462f4a2713aSLionel Sambuc // This nested-name-specifier occurs in a member access expression, e.g.,
463f4a2713aSLionel Sambuc // x->B::f, and we are looking into the type of the object.
464f4a2713aSLionel Sambuc assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
465f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(ObjectType);
466f4a2713aSLionel Sambuc isDependent = ObjectType->isDependentType();
467f4a2713aSLionel Sambuc } else if (SS.isSet()) {
468f4a2713aSLionel Sambuc // This nested-name-specifier occurs after another nested-name-specifier,
469f4a2713aSLionel Sambuc // so look into the context associated with the prior nested-name-specifier.
470f4a2713aSLionel Sambuc LookupCtx = computeDeclContext(SS, EnteringContext);
471f4a2713aSLionel Sambuc isDependent = isDependentScopeSpecifier(SS);
472f4a2713aSLionel Sambuc Found.setContextRange(SS.getRange());
473f4a2713aSLionel Sambuc }
474f4a2713aSLionel Sambuc
475f4a2713aSLionel Sambuc bool ObjectTypeSearchedInScope = false;
476f4a2713aSLionel Sambuc if (LookupCtx) {
477f4a2713aSLionel Sambuc // Perform "qualified" name lookup into the declaration context we
478f4a2713aSLionel Sambuc // computed, which is either the type of the base of a member access
479f4a2713aSLionel Sambuc // expression or the declaration context associated with a prior
480f4a2713aSLionel Sambuc // nested-name-specifier.
481f4a2713aSLionel Sambuc
482f4a2713aSLionel Sambuc // The declaration context must be complete.
483f4a2713aSLionel Sambuc if (!LookupCtx->isDependentContext() &&
484f4a2713aSLionel Sambuc RequireCompleteDeclContext(SS, LookupCtx))
485f4a2713aSLionel Sambuc return true;
486f4a2713aSLionel Sambuc
487f4a2713aSLionel Sambuc LookupQualifiedName(Found, LookupCtx);
488f4a2713aSLionel Sambuc
489f4a2713aSLionel Sambuc if (!ObjectType.isNull() && Found.empty()) {
490f4a2713aSLionel Sambuc // C++ [basic.lookup.classref]p4:
491f4a2713aSLionel Sambuc // If the id-expression in a class member access is a qualified-id of
492f4a2713aSLionel Sambuc // the form
493f4a2713aSLionel Sambuc //
494f4a2713aSLionel Sambuc // class-name-or-namespace-name::...
495f4a2713aSLionel Sambuc //
496f4a2713aSLionel Sambuc // the class-name-or-namespace-name following the . or -> operator is
497f4a2713aSLionel Sambuc // looked up both in the context of the entire postfix-expression and in
498f4a2713aSLionel Sambuc // the scope of the class of the object expression. If the name is found
499f4a2713aSLionel Sambuc // only in the scope of the class of the object expression, the name
500f4a2713aSLionel Sambuc // shall refer to a class-name. If the name is found only in the
501f4a2713aSLionel Sambuc // context of the entire postfix-expression, the name shall refer to a
502f4a2713aSLionel Sambuc // class-name or namespace-name. [...]
503f4a2713aSLionel Sambuc //
504f4a2713aSLionel Sambuc // Qualified name lookup into a class will not find a namespace-name,
505f4a2713aSLionel Sambuc // so we do not need to diagnose that case specifically. However,
506f4a2713aSLionel Sambuc // this qualified name lookup may find nothing. In that case, perform
507f4a2713aSLionel Sambuc // unqualified name lookup in the given scope (if available) or
508f4a2713aSLionel Sambuc // reconstruct the result from when name lookup was performed at template
509f4a2713aSLionel Sambuc // definition time.
510f4a2713aSLionel Sambuc if (S)
511f4a2713aSLionel Sambuc LookupName(Found, S);
512f4a2713aSLionel Sambuc else if (ScopeLookupResult)
513f4a2713aSLionel Sambuc Found.addDecl(ScopeLookupResult);
514f4a2713aSLionel Sambuc
515f4a2713aSLionel Sambuc ObjectTypeSearchedInScope = true;
516f4a2713aSLionel Sambuc }
517f4a2713aSLionel Sambuc } else if (!isDependent) {
518f4a2713aSLionel Sambuc // Perform unqualified name lookup in the current scope.
519f4a2713aSLionel Sambuc LookupName(Found, S);
520f4a2713aSLionel Sambuc }
521f4a2713aSLionel Sambuc
522f4a2713aSLionel Sambuc // If we performed lookup into a dependent context and did not find anything,
523f4a2713aSLionel Sambuc // that's fine: just build a dependent nested-name-specifier.
524f4a2713aSLionel Sambuc if (Found.empty() && isDependent &&
525f4a2713aSLionel Sambuc !(LookupCtx && LookupCtx->isRecord() &&
526f4a2713aSLionel Sambuc (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
527f4a2713aSLionel Sambuc !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) {
528f4a2713aSLionel Sambuc // Don't speculate if we're just trying to improve error recovery.
529f4a2713aSLionel Sambuc if (ErrorRecoveryLookup)
530f4a2713aSLionel Sambuc return true;
531f4a2713aSLionel Sambuc
532f4a2713aSLionel Sambuc // We were not able to compute the declaration context for a dependent
533f4a2713aSLionel Sambuc // base object type or prior nested-name-specifier, so this
534f4a2713aSLionel Sambuc // nested-name-specifier refers to an unknown specialization. Just build
535f4a2713aSLionel Sambuc // a dependent nested-name-specifier.
536f4a2713aSLionel Sambuc SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
537f4a2713aSLionel Sambuc return false;
538f4a2713aSLionel Sambuc }
539f4a2713aSLionel Sambuc
540f4a2713aSLionel Sambuc // FIXME: Deal with ambiguities cleanly.
541f4a2713aSLionel Sambuc
542*0a6a1f1dSLionel Sambuc if (Found.empty() && !ErrorRecoveryLookup) {
543*0a6a1f1dSLionel Sambuc // If identifier is not found as class-name-or-namespace-name, but is found
544*0a6a1f1dSLionel Sambuc // as other entity, don't look for typos.
545*0a6a1f1dSLionel Sambuc LookupResult R(*this, Found.getLookupNameInfo(), LookupOrdinaryName);
546*0a6a1f1dSLionel Sambuc if (LookupCtx)
547*0a6a1f1dSLionel Sambuc LookupQualifiedName(R, LookupCtx);
548*0a6a1f1dSLionel Sambuc else if (S && !isDependent)
549*0a6a1f1dSLionel Sambuc LookupName(R, S);
550*0a6a1f1dSLionel Sambuc if (!R.empty()) {
551*0a6a1f1dSLionel Sambuc // The identifier is found in ordinary lookup. If correction to colon is
552*0a6a1f1dSLionel Sambuc // allowed, suggest replacement to ':'.
553*0a6a1f1dSLionel Sambuc if (IsCorrectedToColon) {
554*0a6a1f1dSLionel Sambuc *IsCorrectedToColon = true;
555*0a6a1f1dSLionel Sambuc Diag(CCLoc, diag::err_nested_name_spec_is_not_class)
556*0a6a1f1dSLionel Sambuc << &Identifier << getLangOpts().CPlusPlus
557*0a6a1f1dSLionel Sambuc << FixItHint::CreateReplacement(CCLoc, ":");
558*0a6a1f1dSLionel Sambuc if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
559*0a6a1f1dSLionel Sambuc Diag(ND->getLocation(), diag::note_declared_at);
560*0a6a1f1dSLionel Sambuc return true;
561*0a6a1f1dSLionel Sambuc }
562*0a6a1f1dSLionel Sambuc // Replacement '::' -> ':' is not allowed, just issue respective error.
563*0a6a1f1dSLionel Sambuc Diag(R.getNameLoc(), diag::err_expected_class_or_namespace)
564*0a6a1f1dSLionel Sambuc << &Identifier << getLangOpts().CPlusPlus;
565*0a6a1f1dSLionel Sambuc if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
566*0a6a1f1dSLionel Sambuc Diag(ND->getLocation(), diag::note_entity_declared_at) << &Identifier;
567*0a6a1f1dSLionel Sambuc return true;
568*0a6a1f1dSLionel Sambuc }
569*0a6a1f1dSLionel Sambuc }
570*0a6a1f1dSLionel Sambuc
571*0a6a1f1dSLionel Sambuc if (Found.empty() && !ErrorRecoveryLookup && !getLangOpts().MSVCCompat) {
572f4a2713aSLionel Sambuc // We haven't found anything, and we're not recovering from a
573f4a2713aSLionel Sambuc // different kind of error, so look for typos.
574f4a2713aSLionel Sambuc DeclarationName Name = Found.getLookupName();
575f4a2713aSLionel Sambuc Found.clear();
576*0a6a1f1dSLionel Sambuc if (TypoCorrection Corrected = CorrectTypo(
577*0a6a1f1dSLionel Sambuc Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS,
578*0a6a1f1dSLionel Sambuc llvm::make_unique<NestedNameSpecifierValidatorCCC>(*this),
579*0a6a1f1dSLionel Sambuc CTK_ErrorRecovery, LookupCtx, EnteringContext)) {
580f4a2713aSLionel Sambuc if (LookupCtx) {
581f4a2713aSLionel Sambuc bool DroppedSpecifier =
582f4a2713aSLionel Sambuc Corrected.WillReplaceSpecifier() &&
583f4a2713aSLionel Sambuc Name.getAsString() == Corrected.getAsString(getLangOpts());
584*0a6a1f1dSLionel Sambuc if (DroppedSpecifier)
585*0a6a1f1dSLionel Sambuc SS.clear();
586f4a2713aSLionel Sambuc diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest)
587f4a2713aSLionel Sambuc << Name << LookupCtx << DroppedSpecifier
588f4a2713aSLionel Sambuc << SS.getRange());
589f4a2713aSLionel Sambuc } else
590f4a2713aSLionel Sambuc diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
591f4a2713aSLionel Sambuc << Name);
592f4a2713aSLionel Sambuc
593f4a2713aSLionel Sambuc if (NamedDecl *ND = Corrected.getCorrectionDecl())
594f4a2713aSLionel Sambuc Found.addDecl(ND);
595f4a2713aSLionel Sambuc Found.setLookupName(Corrected.getCorrection());
596f4a2713aSLionel Sambuc } else {
597f4a2713aSLionel Sambuc Found.setLookupName(&Identifier);
598f4a2713aSLionel Sambuc }
599f4a2713aSLionel Sambuc }
600f4a2713aSLionel Sambuc
601f4a2713aSLionel Sambuc NamedDecl *SD = Found.getAsSingle<NamedDecl>();
602f4a2713aSLionel Sambuc if (isAcceptableNestedNameSpecifier(SD)) {
603f4a2713aSLionel Sambuc if (!ObjectType.isNull() && !ObjectTypeSearchedInScope &&
604f4a2713aSLionel Sambuc !getLangOpts().CPlusPlus11) {
605f4a2713aSLionel Sambuc // C++03 [basic.lookup.classref]p4:
606f4a2713aSLionel Sambuc // [...] If the name is found in both contexts, the
607f4a2713aSLionel Sambuc // class-name-or-namespace-name shall refer to the same entity.
608f4a2713aSLionel Sambuc //
609f4a2713aSLionel Sambuc // We already found the name in the scope of the object. Now, look
610f4a2713aSLionel Sambuc // into the current scope (the scope of the postfix-expression) to
611f4a2713aSLionel Sambuc // see if we can find the same name there. As above, if there is no
612f4a2713aSLionel Sambuc // scope, reconstruct the result from the template instantiation itself.
613f4a2713aSLionel Sambuc //
614f4a2713aSLionel Sambuc // Note that C++11 does *not* perform this redundant lookup.
615f4a2713aSLionel Sambuc NamedDecl *OuterDecl;
616f4a2713aSLionel Sambuc if (S) {
617f4a2713aSLionel Sambuc LookupResult FoundOuter(*this, &Identifier, IdentifierLoc,
618f4a2713aSLionel Sambuc LookupNestedNameSpecifierName);
619f4a2713aSLionel Sambuc LookupName(FoundOuter, S);
620f4a2713aSLionel Sambuc OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
621f4a2713aSLionel Sambuc } else
622f4a2713aSLionel Sambuc OuterDecl = ScopeLookupResult;
623f4a2713aSLionel Sambuc
624f4a2713aSLionel Sambuc if (isAcceptableNestedNameSpecifier(OuterDecl) &&
625f4a2713aSLionel Sambuc OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
626f4a2713aSLionel Sambuc (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
627f4a2713aSLionel Sambuc !Context.hasSameType(
628f4a2713aSLionel Sambuc Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
629f4a2713aSLionel Sambuc Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
630f4a2713aSLionel Sambuc if (ErrorRecoveryLookup)
631f4a2713aSLionel Sambuc return true;
632f4a2713aSLionel Sambuc
633f4a2713aSLionel Sambuc Diag(IdentifierLoc,
634f4a2713aSLionel Sambuc diag::err_nested_name_member_ref_lookup_ambiguous)
635f4a2713aSLionel Sambuc << &Identifier;
636f4a2713aSLionel Sambuc Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type)
637f4a2713aSLionel Sambuc << ObjectType;
638f4a2713aSLionel Sambuc Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope);
639f4a2713aSLionel Sambuc
640f4a2713aSLionel Sambuc // Fall through so that we'll pick the name we found in the object
641f4a2713aSLionel Sambuc // type, since that's probably what the user wanted anyway.
642f4a2713aSLionel Sambuc }
643f4a2713aSLionel Sambuc }
644f4a2713aSLionel Sambuc
645*0a6a1f1dSLionel Sambuc if (auto *TD = dyn_cast_or_null<TypedefNameDecl>(SD))
646*0a6a1f1dSLionel Sambuc MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
647*0a6a1f1dSLionel Sambuc
648f4a2713aSLionel Sambuc // If we're just performing this lookup for error-recovery purposes,
649f4a2713aSLionel Sambuc // don't extend the nested-name-specifier. Just return now.
650f4a2713aSLionel Sambuc if (ErrorRecoveryLookup)
651f4a2713aSLionel Sambuc return false;
652f4a2713aSLionel Sambuc
653*0a6a1f1dSLionel Sambuc // The use of a nested name specifier may trigger deprecation warnings.
654*0a6a1f1dSLionel Sambuc DiagnoseUseOfDecl(SD, CCLoc);
655*0a6a1f1dSLionel Sambuc
656*0a6a1f1dSLionel Sambuc
657f4a2713aSLionel Sambuc if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) {
658f4a2713aSLionel Sambuc SS.Extend(Context, Namespace, IdentifierLoc, CCLoc);
659f4a2713aSLionel Sambuc return false;
660f4a2713aSLionel Sambuc }
661f4a2713aSLionel Sambuc
662f4a2713aSLionel Sambuc if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) {
663f4a2713aSLionel Sambuc SS.Extend(Context, Alias, IdentifierLoc, CCLoc);
664f4a2713aSLionel Sambuc return false;
665f4a2713aSLionel Sambuc }
666f4a2713aSLionel Sambuc
667f4a2713aSLionel Sambuc QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
668f4a2713aSLionel Sambuc TypeLocBuilder TLB;
669f4a2713aSLionel Sambuc if (isa<InjectedClassNameType>(T)) {
670f4a2713aSLionel Sambuc InjectedClassNameTypeLoc InjectedTL
671f4a2713aSLionel Sambuc = TLB.push<InjectedClassNameTypeLoc>(T);
672f4a2713aSLionel Sambuc InjectedTL.setNameLoc(IdentifierLoc);
673f4a2713aSLionel Sambuc } else if (isa<RecordType>(T)) {
674f4a2713aSLionel Sambuc RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T);
675f4a2713aSLionel Sambuc RecordTL.setNameLoc(IdentifierLoc);
676f4a2713aSLionel Sambuc } else if (isa<TypedefType>(T)) {
677f4a2713aSLionel Sambuc TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T);
678f4a2713aSLionel Sambuc TypedefTL.setNameLoc(IdentifierLoc);
679f4a2713aSLionel Sambuc } else if (isa<EnumType>(T)) {
680f4a2713aSLionel Sambuc EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T);
681f4a2713aSLionel Sambuc EnumTL.setNameLoc(IdentifierLoc);
682f4a2713aSLionel Sambuc } else if (isa<TemplateTypeParmType>(T)) {
683f4a2713aSLionel Sambuc TemplateTypeParmTypeLoc TemplateTypeTL
684f4a2713aSLionel Sambuc = TLB.push<TemplateTypeParmTypeLoc>(T);
685f4a2713aSLionel Sambuc TemplateTypeTL.setNameLoc(IdentifierLoc);
686f4a2713aSLionel Sambuc } else if (isa<UnresolvedUsingType>(T)) {
687f4a2713aSLionel Sambuc UnresolvedUsingTypeLoc UnresolvedTL
688f4a2713aSLionel Sambuc = TLB.push<UnresolvedUsingTypeLoc>(T);
689f4a2713aSLionel Sambuc UnresolvedTL.setNameLoc(IdentifierLoc);
690f4a2713aSLionel Sambuc } else if (isa<SubstTemplateTypeParmType>(T)) {
691f4a2713aSLionel Sambuc SubstTemplateTypeParmTypeLoc TL
692f4a2713aSLionel Sambuc = TLB.push<SubstTemplateTypeParmTypeLoc>(T);
693f4a2713aSLionel Sambuc TL.setNameLoc(IdentifierLoc);
694f4a2713aSLionel Sambuc } else if (isa<SubstTemplateTypeParmPackType>(T)) {
695f4a2713aSLionel Sambuc SubstTemplateTypeParmPackTypeLoc TL
696f4a2713aSLionel Sambuc = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T);
697f4a2713aSLionel Sambuc TL.setNameLoc(IdentifierLoc);
698f4a2713aSLionel Sambuc } else {
699f4a2713aSLionel Sambuc llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier");
700f4a2713aSLionel Sambuc }
701f4a2713aSLionel Sambuc
702f4a2713aSLionel Sambuc if (T->isEnumeralType())
703f4a2713aSLionel Sambuc Diag(IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec);
704f4a2713aSLionel Sambuc
705f4a2713aSLionel Sambuc SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
706f4a2713aSLionel Sambuc CCLoc);
707f4a2713aSLionel Sambuc return false;
708f4a2713aSLionel Sambuc }
709f4a2713aSLionel Sambuc
710f4a2713aSLionel Sambuc // Otherwise, we have an error case. If we don't want diagnostics, just
711f4a2713aSLionel Sambuc // return an error now.
712f4a2713aSLionel Sambuc if (ErrorRecoveryLookup)
713f4a2713aSLionel Sambuc return true;
714f4a2713aSLionel Sambuc
715f4a2713aSLionel Sambuc // If we didn't find anything during our lookup, try again with
716f4a2713aSLionel Sambuc // ordinary name lookup, which can help us produce better error
717f4a2713aSLionel Sambuc // messages.
718f4a2713aSLionel Sambuc if (Found.empty()) {
719f4a2713aSLionel Sambuc Found.clear(LookupOrdinaryName);
720f4a2713aSLionel Sambuc LookupName(Found, S);
721f4a2713aSLionel Sambuc }
722f4a2713aSLionel Sambuc
723f4a2713aSLionel Sambuc // In Microsoft mode, if we are within a templated function and we can't
724f4a2713aSLionel Sambuc // resolve Identifier, then extend the SS with Identifier. This will have
725f4a2713aSLionel Sambuc // the effect of resolving Identifier during template instantiation.
726f4a2713aSLionel Sambuc // The goal is to be able to resolve a function call whose
727f4a2713aSLionel Sambuc // nested-name-specifier is located inside a dependent base class.
728f4a2713aSLionel Sambuc // Example:
729f4a2713aSLionel Sambuc //
730f4a2713aSLionel Sambuc // class C {
731f4a2713aSLionel Sambuc // public:
732f4a2713aSLionel Sambuc // static void foo2() { }
733f4a2713aSLionel Sambuc // };
734f4a2713aSLionel Sambuc // template <class T> class A { public: typedef C D; };
735f4a2713aSLionel Sambuc //
736f4a2713aSLionel Sambuc // template <class T> class B : public A<T> {
737f4a2713aSLionel Sambuc // public:
738f4a2713aSLionel Sambuc // void foo() { D::foo2(); }
739f4a2713aSLionel Sambuc // };
740*0a6a1f1dSLionel Sambuc if (getLangOpts().MSVCCompat) {
741f4a2713aSLionel Sambuc DeclContext *DC = LookupCtx ? LookupCtx : CurContext;
742f4a2713aSLionel Sambuc if (DC->isDependentContext() && DC->isFunctionOrMethod()) {
743*0a6a1f1dSLionel Sambuc CXXRecordDecl *ContainingClass = dyn_cast<CXXRecordDecl>(DC->getParent());
744*0a6a1f1dSLionel Sambuc if (ContainingClass && ContainingClass->hasAnyDependentBases()) {
745*0a6a1f1dSLionel Sambuc Diag(IdentifierLoc, diag::ext_undeclared_unqual_id_with_dependent_base)
746*0a6a1f1dSLionel Sambuc << &Identifier << ContainingClass;
747f4a2713aSLionel Sambuc SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
748f4a2713aSLionel Sambuc return false;
749f4a2713aSLionel Sambuc }
750f4a2713aSLionel Sambuc }
751*0a6a1f1dSLionel Sambuc }
752f4a2713aSLionel Sambuc
753*0a6a1f1dSLionel Sambuc if (!Found.empty()) {
754*0a6a1f1dSLionel Sambuc if (TypeDecl *TD = Found.getAsSingle<TypeDecl>())
755*0a6a1f1dSLionel Sambuc Diag(IdentifierLoc, diag::err_expected_class_or_namespace)
756*0a6a1f1dSLionel Sambuc << QualType(TD->getTypeForDecl(), 0) << getLangOpts().CPlusPlus;
757*0a6a1f1dSLionel Sambuc else {
758*0a6a1f1dSLionel Sambuc Diag(IdentifierLoc, diag::err_expected_class_or_namespace)
759*0a6a1f1dSLionel Sambuc << &Identifier << getLangOpts().CPlusPlus;
760*0a6a1f1dSLionel Sambuc if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
761*0a6a1f1dSLionel Sambuc Diag(ND->getLocation(), diag::note_entity_declared_at) << &Identifier;
762*0a6a1f1dSLionel Sambuc }
763*0a6a1f1dSLionel Sambuc } else if (SS.isSet())
764*0a6a1f1dSLionel Sambuc Diag(IdentifierLoc, diag::err_no_member) << &Identifier << LookupCtx
765*0a6a1f1dSLionel Sambuc << SS.getRange();
766f4a2713aSLionel Sambuc else
767*0a6a1f1dSLionel Sambuc Diag(IdentifierLoc, diag::err_undeclared_var_use) << &Identifier;
768f4a2713aSLionel Sambuc
769f4a2713aSLionel Sambuc return true;
770f4a2713aSLionel Sambuc }
771f4a2713aSLionel Sambuc
ActOnCXXNestedNameSpecifier(Scope * S,IdentifierInfo & Identifier,SourceLocation IdentifierLoc,SourceLocation CCLoc,ParsedType ObjectType,bool EnteringContext,CXXScopeSpec & SS,bool ErrorRecoveryLookup,bool * IsCorrectedToColon)772f4a2713aSLionel Sambuc bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
773f4a2713aSLionel Sambuc IdentifierInfo &Identifier,
774f4a2713aSLionel Sambuc SourceLocation IdentifierLoc,
775f4a2713aSLionel Sambuc SourceLocation CCLoc,
776f4a2713aSLionel Sambuc ParsedType ObjectType,
777f4a2713aSLionel Sambuc bool EnteringContext,
778*0a6a1f1dSLionel Sambuc CXXScopeSpec &SS,
779*0a6a1f1dSLionel Sambuc bool ErrorRecoveryLookup,
780*0a6a1f1dSLionel Sambuc bool *IsCorrectedToColon) {
781f4a2713aSLionel Sambuc if (SS.isInvalid())
782f4a2713aSLionel Sambuc return true;
783f4a2713aSLionel Sambuc
784f4a2713aSLionel Sambuc return BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, CCLoc,
785f4a2713aSLionel Sambuc GetTypeFromParser(ObjectType),
786f4a2713aSLionel Sambuc EnteringContext, SS,
787*0a6a1f1dSLionel Sambuc /*ScopeLookupResult=*/nullptr, false,
788*0a6a1f1dSLionel Sambuc IsCorrectedToColon);
789f4a2713aSLionel Sambuc }
790f4a2713aSLionel Sambuc
ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec & SS,const DeclSpec & DS,SourceLocation ColonColonLoc)791f4a2713aSLionel Sambuc bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS,
792f4a2713aSLionel Sambuc const DeclSpec &DS,
793f4a2713aSLionel Sambuc SourceLocation ColonColonLoc) {
794f4a2713aSLionel Sambuc if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error)
795f4a2713aSLionel Sambuc return true;
796f4a2713aSLionel Sambuc
797f4a2713aSLionel Sambuc assert(DS.getTypeSpecType() == DeclSpec::TST_decltype);
798f4a2713aSLionel Sambuc
799f4a2713aSLionel Sambuc QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
800f4a2713aSLionel Sambuc if (!T->isDependentType() && !T->getAs<TagType>()) {
801*0a6a1f1dSLionel Sambuc Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class_or_namespace)
802f4a2713aSLionel Sambuc << T << getLangOpts().CPlusPlus;
803f4a2713aSLionel Sambuc return true;
804f4a2713aSLionel Sambuc }
805f4a2713aSLionel Sambuc
806f4a2713aSLionel Sambuc TypeLocBuilder TLB;
807f4a2713aSLionel Sambuc DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T);
808f4a2713aSLionel Sambuc DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc());
809f4a2713aSLionel Sambuc SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
810f4a2713aSLionel Sambuc ColonColonLoc);
811f4a2713aSLionel Sambuc return false;
812f4a2713aSLionel Sambuc }
813f4a2713aSLionel Sambuc
814f4a2713aSLionel Sambuc /// IsInvalidUnlessNestedName - This method is used for error recovery
815f4a2713aSLionel Sambuc /// purposes to determine whether the specified identifier is only valid as
816f4a2713aSLionel Sambuc /// a nested name specifier, for example a namespace name. It is
817f4a2713aSLionel Sambuc /// conservatively correct to always return false from this method.
818f4a2713aSLionel Sambuc ///
819f4a2713aSLionel Sambuc /// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
IsInvalidUnlessNestedName(Scope * S,CXXScopeSpec & SS,IdentifierInfo & Identifier,SourceLocation IdentifierLoc,SourceLocation ColonLoc,ParsedType ObjectType,bool EnteringContext)820f4a2713aSLionel Sambuc bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
821f4a2713aSLionel Sambuc IdentifierInfo &Identifier,
822f4a2713aSLionel Sambuc SourceLocation IdentifierLoc,
823f4a2713aSLionel Sambuc SourceLocation ColonLoc,
824f4a2713aSLionel Sambuc ParsedType ObjectType,
825f4a2713aSLionel Sambuc bool EnteringContext) {
826f4a2713aSLionel Sambuc if (SS.isInvalid())
827f4a2713aSLionel Sambuc return false;
828f4a2713aSLionel Sambuc
829f4a2713aSLionel Sambuc return !BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, ColonLoc,
830f4a2713aSLionel Sambuc GetTypeFromParser(ObjectType),
831f4a2713aSLionel Sambuc EnteringContext, SS,
832*0a6a1f1dSLionel Sambuc /*ScopeLookupResult=*/nullptr, true);
833f4a2713aSLionel Sambuc }
834f4a2713aSLionel Sambuc
ActOnCXXNestedNameSpecifier(Scope * S,CXXScopeSpec & SS,SourceLocation TemplateKWLoc,TemplateTy Template,SourceLocation TemplateNameLoc,SourceLocation LAngleLoc,ASTTemplateArgsPtr TemplateArgsIn,SourceLocation RAngleLoc,SourceLocation CCLoc,bool EnteringContext)835f4a2713aSLionel Sambuc bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
836f4a2713aSLionel Sambuc CXXScopeSpec &SS,
837f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc,
838f4a2713aSLionel Sambuc TemplateTy Template,
839f4a2713aSLionel Sambuc SourceLocation TemplateNameLoc,
840f4a2713aSLionel Sambuc SourceLocation LAngleLoc,
841f4a2713aSLionel Sambuc ASTTemplateArgsPtr TemplateArgsIn,
842f4a2713aSLionel Sambuc SourceLocation RAngleLoc,
843f4a2713aSLionel Sambuc SourceLocation CCLoc,
844f4a2713aSLionel Sambuc bool EnteringContext) {
845f4a2713aSLionel Sambuc if (SS.isInvalid())
846f4a2713aSLionel Sambuc return true;
847f4a2713aSLionel Sambuc
848f4a2713aSLionel Sambuc // Translate the parser's template argument list in our AST format.
849f4a2713aSLionel Sambuc TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
850f4a2713aSLionel Sambuc translateTemplateArguments(TemplateArgsIn, TemplateArgs);
851f4a2713aSLionel Sambuc
852*0a6a1f1dSLionel Sambuc DependentTemplateName *DTN = Template.get().getAsDependentTemplateName();
853*0a6a1f1dSLionel Sambuc if (DTN && DTN->isIdentifier()) {
854f4a2713aSLionel Sambuc // Handle a dependent template specialization for which we cannot resolve
855f4a2713aSLionel Sambuc // the template name.
856f4a2713aSLionel Sambuc assert(DTN->getQualifier() == SS.getScopeRep());
857f4a2713aSLionel Sambuc QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
858f4a2713aSLionel Sambuc DTN->getQualifier(),
859f4a2713aSLionel Sambuc DTN->getIdentifier(),
860f4a2713aSLionel Sambuc TemplateArgs);
861f4a2713aSLionel Sambuc
862f4a2713aSLionel Sambuc // Create source-location information for this type.
863f4a2713aSLionel Sambuc TypeLocBuilder Builder;
864f4a2713aSLionel Sambuc DependentTemplateSpecializationTypeLoc SpecTL
865f4a2713aSLionel Sambuc = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
866f4a2713aSLionel Sambuc SpecTL.setElaboratedKeywordLoc(SourceLocation());
867f4a2713aSLionel Sambuc SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
868f4a2713aSLionel Sambuc SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
869f4a2713aSLionel Sambuc SpecTL.setTemplateNameLoc(TemplateNameLoc);
870f4a2713aSLionel Sambuc SpecTL.setLAngleLoc(LAngleLoc);
871f4a2713aSLionel Sambuc SpecTL.setRAngleLoc(RAngleLoc);
872f4a2713aSLionel Sambuc for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
873f4a2713aSLionel Sambuc SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
874f4a2713aSLionel Sambuc
875f4a2713aSLionel Sambuc SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
876f4a2713aSLionel Sambuc CCLoc);
877f4a2713aSLionel Sambuc return false;
878f4a2713aSLionel Sambuc }
879f4a2713aSLionel Sambuc
880*0a6a1f1dSLionel Sambuc TemplateDecl *TD = Template.get().getAsTemplateDecl();
881*0a6a1f1dSLionel Sambuc if (Template.get().getAsOverloadedTemplate() || DTN ||
882*0a6a1f1dSLionel Sambuc isa<FunctionTemplateDecl>(TD) || isa<VarTemplateDecl>(TD)) {
883f4a2713aSLionel Sambuc SourceRange R(TemplateNameLoc, RAngleLoc);
884f4a2713aSLionel Sambuc if (SS.getRange().isValid())
885f4a2713aSLionel Sambuc R.setBegin(SS.getRange().getBegin());
886f4a2713aSLionel Sambuc
887f4a2713aSLionel Sambuc Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier)
888*0a6a1f1dSLionel Sambuc << (TD && isa<VarTemplateDecl>(TD)) << Template.get() << R;
889f4a2713aSLionel Sambuc NoteAllFoundTemplates(Template.get());
890f4a2713aSLionel Sambuc return true;
891f4a2713aSLionel Sambuc }
892f4a2713aSLionel Sambuc
893f4a2713aSLionel Sambuc // We were able to resolve the template name to an actual template.
894f4a2713aSLionel Sambuc // Build an appropriate nested-name-specifier.
895f4a2713aSLionel Sambuc QualType T = CheckTemplateIdType(Template.get(), TemplateNameLoc,
896f4a2713aSLionel Sambuc TemplateArgs);
897f4a2713aSLionel Sambuc if (T.isNull())
898f4a2713aSLionel Sambuc return true;
899f4a2713aSLionel Sambuc
900f4a2713aSLionel Sambuc // Alias template specializations can produce types which are not valid
901f4a2713aSLionel Sambuc // nested name specifiers.
902f4a2713aSLionel Sambuc if (!T->isDependentType() && !T->getAs<TagType>()) {
903f4a2713aSLionel Sambuc Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T;
904f4a2713aSLionel Sambuc NoteAllFoundTemplates(Template.get());
905f4a2713aSLionel Sambuc return true;
906f4a2713aSLionel Sambuc }
907f4a2713aSLionel Sambuc
908f4a2713aSLionel Sambuc // Provide source-location information for the template specialization type.
909f4a2713aSLionel Sambuc TypeLocBuilder Builder;
910f4a2713aSLionel Sambuc TemplateSpecializationTypeLoc SpecTL
911f4a2713aSLionel Sambuc = Builder.push<TemplateSpecializationTypeLoc>(T);
912f4a2713aSLionel Sambuc SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
913f4a2713aSLionel Sambuc SpecTL.setTemplateNameLoc(TemplateNameLoc);
914f4a2713aSLionel Sambuc SpecTL.setLAngleLoc(LAngleLoc);
915f4a2713aSLionel Sambuc SpecTL.setRAngleLoc(RAngleLoc);
916f4a2713aSLionel Sambuc for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
917f4a2713aSLionel Sambuc SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
918f4a2713aSLionel Sambuc
919f4a2713aSLionel Sambuc
920f4a2713aSLionel Sambuc SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
921f4a2713aSLionel Sambuc CCLoc);
922f4a2713aSLionel Sambuc return false;
923f4a2713aSLionel Sambuc }
924f4a2713aSLionel Sambuc
925f4a2713aSLionel Sambuc namespace {
926f4a2713aSLionel Sambuc /// \brief A structure that stores a nested-name-specifier annotation,
927f4a2713aSLionel Sambuc /// including both the nested-name-specifier
928f4a2713aSLionel Sambuc struct NestedNameSpecifierAnnotation {
929f4a2713aSLionel Sambuc NestedNameSpecifier *NNS;
930f4a2713aSLionel Sambuc };
931f4a2713aSLionel Sambuc }
932f4a2713aSLionel Sambuc
SaveNestedNameSpecifierAnnotation(CXXScopeSpec & SS)933f4a2713aSLionel Sambuc void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) {
934f4a2713aSLionel Sambuc if (SS.isEmpty() || SS.isInvalid())
935*0a6a1f1dSLionel Sambuc return nullptr;
936f4a2713aSLionel Sambuc
937f4a2713aSLionel Sambuc void *Mem = Context.Allocate((sizeof(NestedNameSpecifierAnnotation) +
938f4a2713aSLionel Sambuc SS.location_size()),
939f4a2713aSLionel Sambuc llvm::alignOf<NestedNameSpecifierAnnotation>());
940f4a2713aSLionel Sambuc NestedNameSpecifierAnnotation *Annotation
941f4a2713aSLionel Sambuc = new (Mem) NestedNameSpecifierAnnotation;
942f4a2713aSLionel Sambuc Annotation->NNS = SS.getScopeRep();
943f4a2713aSLionel Sambuc memcpy(Annotation + 1, SS.location_data(), SS.location_size());
944f4a2713aSLionel Sambuc return Annotation;
945f4a2713aSLionel Sambuc }
946f4a2713aSLionel Sambuc
RestoreNestedNameSpecifierAnnotation(void * AnnotationPtr,SourceRange AnnotationRange,CXXScopeSpec & SS)947f4a2713aSLionel Sambuc void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
948f4a2713aSLionel Sambuc SourceRange AnnotationRange,
949f4a2713aSLionel Sambuc CXXScopeSpec &SS) {
950f4a2713aSLionel Sambuc if (!AnnotationPtr) {
951f4a2713aSLionel Sambuc SS.SetInvalid(AnnotationRange);
952f4a2713aSLionel Sambuc return;
953f4a2713aSLionel Sambuc }
954f4a2713aSLionel Sambuc
955f4a2713aSLionel Sambuc NestedNameSpecifierAnnotation *Annotation
956f4a2713aSLionel Sambuc = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr);
957f4a2713aSLionel Sambuc SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1));
958f4a2713aSLionel Sambuc }
959f4a2713aSLionel Sambuc
ShouldEnterDeclaratorScope(Scope * S,const CXXScopeSpec & SS)960f4a2713aSLionel Sambuc bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
961f4a2713aSLionel Sambuc assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
962f4a2713aSLionel Sambuc
963f4a2713aSLionel Sambuc NestedNameSpecifier *Qualifier = SS.getScopeRep();
964f4a2713aSLionel Sambuc
965f4a2713aSLionel Sambuc // There are only two places a well-formed program may qualify a
966f4a2713aSLionel Sambuc // declarator: first, when defining a namespace or class member
967f4a2713aSLionel Sambuc // out-of-line, and second, when naming an explicitly-qualified
968f4a2713aSLionel Sambuc // friend function. The latter case is governed by
969f4a2713aSLionel Sambuc // C++03 [basic.lookup.unqual]p10:
970f4a2713aSLionel Sambuc // In a friend declaration naming a member function, a name used
971f4a2713aSLionel Sambuc // in the function declarator and not part of a template-argument
972f4a2713aSLionel Sambuc // in a template-id is first looked up in the scope of the member
973f4a2713aSLionel Sambuc // function's class. If it is not found, or if the name is part of
974f4a2713aSLionel Sambuc // a template-argument in a template-id, the look up is as
975f4a2713aSLionel Sambuc // described for unqualified names in the definition of the class
976f4a2713aSLionel Sambuc // granting friendship.
977f4a2713aSLionel Sambuc // i.e. we don't push a scope unless it's a class member.
978f4a2713aSLionel Sambuc
979f4a2713aSLionel Sambuc switch (Qualifier->getKind()) {
980f4a2713aSLionel Sambuc case NestedNameSpecifier::Global:
981f4a2713aSLionel Sambuc case NestedNameSpecifier::Namespace:
982f4a2713aSLionel Sambuc case NestedNameSpecifier::NamespaceAlias:
983f4a2713aSLionel Sambuc // These are always namespace scopes. We never want to enter a
984f4a2713aSLionel Sambuc // namespace scope from anything but a file context.
985f4a2713aSLionel Sambuc return CurContext->getRedeclContext()->isFileContext();
986f4a2713aSLionel Sambuc
987f4a2713aSLionel Sambuc case NestedNameSpecifier::Identifier:
988f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpec:
989f4a2713aSLionel Sambuc case NestedNameSpecifier::TypeSpecWithTemplate:
990*0a6a1f1dSLionel Sambuc case NestedNameSpecifier::Super:
991f4a2713aSLionel Sambuc // These are never namespace scopes.
992f4a2713aSLionel Sambuc return true;
993f4a2713aSLionel Sambuc }
994f4a2713aSLionel Sambuc
995f4a2713aSLionel Sambuc llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
996f4a2713aSLionel Sambuc }
997f4a2713aSLionel Sambuc
998f4a2713aSLionel Sambuc /// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
999f4a2713aSLionel Sambuc /// scope or nested-name-specifier) is parsed, part of a declarator-id.
1000f4a2713aSLionel Sambuc /// After this method is called, according to [C++ 3.4.3p3], names should be
1001f4a2713aSLionel Sambuc /// looked up in the declarator-id's scope, until the declarator is parsed and
1002f4a2713aSLionel Sambuc /// ActOnCXXExitDeclaratorScope is called.
1003f4a2713aSLionel Sambuc /// The 'SS' should be a non-empty valid CXXScopeSpec.
ActOnCXXEnterDeclaratorScope(Scope * S,CXXScopeSpec & SS)1004f4a2713aSLionel Sambuc bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
1005f4a2713aSLionel Sambuc assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
1006f4a2713aSLionel Sambuc
1007f4a2713aSLionel Sambuc if (SS.isInvalid()) return true;
1008f4a2713aSLionel Sambuc
1009f4a2713aSLionel Sambuc DeclContext *DC = computeDeclContext(SS, true);
1010f4a2713aSLionel Sambuc if (!DC) return true;
1011f4a2713aSLionel Sambuc
1012f4a2713aSLionel Sambuc // Before we enter a declarator's context, we need to make sure that
1013f4a2713aSLionel Sambuc // it is a complete declaration context.
1014f4a2713aSLionel Sambuc if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC))
1015f4a2713aSLionel Sambuc return true;
1016f4a2713aSLionel Sambuc
1017f4a2713aSLionel Sambuc EnterDeclaratorContext(S, DC);
1018f4a2713aSLionel Sambuc
1019f4a2713aSLionel Sambuc // Rebuild the nested name specifier for the new scope.
1020f4a2713aSLionel Sambuc if (DC->isDependentContext())
1021f4a2713aSLionel Sambuc RebuildNestedNameSpecifierInCurrentInstantiation(SS);
1022f4a2713aSLionel Sambuc
1023f4a2713aSLionel Sambuc return false;
1024f4a2713aSLionel Sambuc }
1025f4a2713aSLionel Sambuc
1026f4a2713aSLionel Sambuc /// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
1027f4a2713aSLionel Sambuc /// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
1028f4a2713aSLionel Sambuc /// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
1029f4a2713aSLionel Sambuc /// Used to indicate that names should revert to being looked up in the
1030f4a2713aSLionel Sambuc /// defining scope.
ActOnCXXExitDeclaratorScope(Scope * S,const CXXScopeSpec & SS)1031f4a2713aSLionel Sambuc void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
1032f4a2713aSLionel Sambuc assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
1033f4a2713aSLionel Sambuc if (SS.isInvalid())
1034f4a2713aSLionel Sambuc return;
1035f4a2713aSLionel Sambuc assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
1036f4a2713aSLionel Sambuc "exiting declarator scope we never really entered");
1037f4a2713aSLionel Sambuc ExitDeclaratorContext(S);
1038f4a2713aSLionel Sambuc }
1039