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