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