| 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/Template.h" |
| 22 | #include "clang/Sema/TypoCorrection.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 | DeclContext *CurContext) { |
| 29 | if (T.isNull()) |
| 30 | return nullptr; |
| 31 | |
| 32 | const TagType *TagTy = dyn_cast<TagType>(Val: T->getCanonicalTypeInternal()); |
| 33 | if (!isa_and_present<RecordType, InjectedClassNameType>(Val: TagTy)) |
| 34 | return nullptr; |
| 35 | auto *RD = cast<CXXRecordDecl>(Val: TagTy->getDecl())->getDefinitionOrSelf(); |
| 36 | if (isa<InjectedClassNameType>(Val: TagTy) || |
| 37 | RD->isCurrentInstantiation(CurContext)) |
| 38 | return RD; |
| 39 | return nullptr; |
| 40 | } |
| 41 | |
| 42 | DeclContext *Sema::computeDeclContext(QualType T) { |
| 43 | if (!T->isDependentType()) |
| 44 | if (auto *D = T->getAsRecordDecl()) |
| 45 | return D; |
| 46 | return ::getCurrentInstantiationOf(T, CurContext); |
| 47 | } |
| 48 | |
| 49 | DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS, |
| 50 | bool EnteringContext) { |
| 51 | NestedNameSpecifier NNS = SS.getScopeRep(); |
| 52 | if (!NNS.isDependent()) { |
| 53 | switch (NNS.getKind()) { |
| 54 | case NestedNameSpecifier::Kind::Namespace: |
| 55 | return const_cast<NamespaceDecl *>( |
| 56 | NNS.getAsNamespaceAndPrefix().Namespace->getNamespace()); |
| 57 | |
| 58 | case NestedNameSpecifier::Kind::Type: |
| 59 | return NNS.getAsType()->castAsTagDecl(); |
| 60 | |
| 61 | case NestedNameSpecifier::Kind::Global: |
| 62 | return Context.getTranslationUnitDecl(); |
| 63 | |
| 64 | case NestedNameSpecifier::Kind::MicrosoftSuper: |
| 65 | return NNS.getAsMicrosoftSuper(); |
| 66 | |
| 67 | case NestedNameSpecifier::Kind::Null: |
| 68 | return nullptr; |
| 69 | } |
| 70 | |
| 71 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!" ); |
| 72 | } |
| 73 | |
| 74 | // If this nested-name-specifier refers to the current |
| 75 | // instantiation, return its DeclContext. |
| 76 | if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS)) |
| 77 | return Record; |
| 78 | |
| 79 | if (!EnteringContext || NNS.getKind() != NestedNameSpecifier::Kind::Type) |
| 80 | return nullptr; |
| 81 | const Type *NNSType = NNS.getAsType(); |
| 82 | |
| 83 | // Look through type alias templates, per C++0x [temp.dep.type]p1. |
| 84 | NNSType = Context.getCanonicalType(T: NNSType); |
| 85 | if (const auto *SpecType = dyn_cast<TemplateSpecializationType>(Val: NNSType)) { |
| 86 | // We are entering the context of the nested name specifier, so try to |
| 87 | // match the nested name specifier to either a primary class template |
| 88 | // or a class template partial specialization. |
| 89 | ClassTemplateDecl *ClassTemplate = dyn_cast_or_null<ClassTemplateDecl>( |
| 90 | Val: SpecType->getTemplateName().getAsTemplateDecl()); |
| 91 | if (!ClassTemplate) |
| 92 | return nullptr; |
| 93 | ClassTemplatePartialSpecializationDecl *PartialSpec = nullptr; |
| 94 | ArrayRef<TemplateParameterList *> TemplateParamLists = |
| 95 | SS.getTemplateParamLists(); |
| 96 | if (!TemplateParamLists.empty()) { |
| 97 | unsigned Depth = ClassTemplate->getTemplateParameters()->getDepth(); |
| 98 | auto L = llvm::find_if(Range&: TemplateParamLists, |
| 99 | P: [Depth](TemplateParameterList *TPL) { |
| 100 | return TPL->getDepth() == Depth; |
| 101 | }); |
| 102 | if (L != TemplateParamLists.end()) { |
| 103 | void *Pos = nullptr; |
| 104 | PartialSpec = ClassTemplate->findPartialSpecialization( |
| 105 | Args: SpecType->template_arguments(), TPL: *L, InsertPos&: Pos); |
| 106 | } |
| 107 | } else { |
| 108 | // FIXME: The fallback on the search of partial |
| 109 | // specialization using ContextType should be eventually removed since |
| 110 | // it doesn't handle the case of constrained template parameters |
| 111 | // correctly. Currently removing this fallback would change the |
| 112 | // diagnostic output for invalid code in a number of tests. |
| 113 | PartialSpec = |
| 114 | ClassTemplate->findPartialSpecialization(T: QualType(SpecType, 0)); |
| 115 | } |
| 116 | |
| 117 | if (PartialSpec) { |
| 118 | // A declaration of the partial specialization must be visible. |
| 119 | // We can always recover here, because this only happens when we're |
| 120 | // entering the context, and that can't happen in a SFINAE context. |
| 121 | assert(!isSFINAEContext() && "partial specialization scope " |
| 122 | "specifier in SFINAE context?" ); |
| 123 | if (PartialSpec->hasDefinition() && !hasReachableDefinition(D: PartialSpec)) |
| 124 | diagnoseMissingImport(Loc: SS.getLastQualifierNameLoc(), Decl: PartialSpec, |
| 125 | MIK: MissingImportKind::PartialSpecialization, Recover: true); |
| 126 | return PartialSpec; |
| 127 | } |
| 128 | |
| 129 | // If the type of the nested name specifier is the same as the |
| 130 | // injected class name of the named class template, we're entering |
| 131 | // into that class template definition. |
| 132 | CanQualType Injected = |
| 133 | ClassTemplate->getCanonicalInjectedSpecializationType(Ctx: Context); |
| 134 | if (Context.hasSameType(T1: Injected, T2: QualType(SpecType, 0))) |
| 135 | return ClassTemplate->getTemplatedDecl(); |
| 136 | return nullptr; |
| 137 | } |
| 138 | if (const auto *RecordT = dyn_cast<RecordType>(Val: NNSType)) { |
| 139 | // The nested name specifier refers to a member of a class template. |
| 140 | return RecordT->getDecl()->getDefinitionOrSelf(); |
| 141 | } |
| 142 | |
| 143 | return nullptr; |
| 144 | } |
| 145 | |
| 146 | bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) { |
| 147 | if (!SS.isSet() || SS.isInvalid()) |
| 148 | return false; |
| 149 | |
| 150 | return SS.getScopeRep().isDependent(); |
| 151 | } |
| 152 | |
| 153 | CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier NNS) { |
| 154 | assert(getLangOpts().CPlusPlus && "Only callable in C++" ); |
| 155 | assert(NNS.isDependent() && "Only dependent nested-name-specifier allowed" ); |
| 156 | |
| 157 | if (NNS.getKind() != NestedNameSpecifier::Kind::Type) |
| 158 | return nullptr; |
| 159 | |
| 160 | QualType T = QualType(NNS.getAsType(), 0); |
| 161 | return ::getCurrentInstantiationOf(T, CurContext); |
| 162 | } |
| 163 | |
| 164 | /// Require that the context specified by SS be complete. |
| 165 | /// |
| 166 | /// If SS refers to a type, this routine checks whether the type is |
| 167 | /// complete enough (or can be made complete enough) for name lookup |
| 168 | /// into the DeclContext. A type that is not yet completed can be |
| 169 | /// considered "complete enough" if it is a class/struct/union/enum |
| 170 | /// that is currently being defined. Or, if we have a type that names |
| 171 | /// a class template specialization that is not a complete type, we |
| 172 | /// will attempt to instantiate that class template. |
| 173 | bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS, |
| 174 | DeclContext *DC) { |
| 175 | assert(DC && "given null context" ); |
| 176 | |
| 177 | TagDecl *tag = dyn_cast<TagDecl>(Val: DC); |
| 178 | |
| 179 | // If this is a dependent type, then we consider it complete. |
| 180 | // FIXME: This is wrong; we should require a (visible) definition to |
| 181 | // exist in this case too. |
| 182 | if (!tag || tag->isDependentContext()) |
| 183 | return false; |
| 184 | |
| 185 | // Grab the tag definition, if there is one. |
| 186 | tag = tag->getDefinitionOrSelf(); |
| 187 | |
| 188 | // If we're currently defining this type, then lookup into the |
| 189 | // type is okay: don't complain that it isn't complete yet. |
| 190 | if (tag->isBeingDefined()) |
| 191 | return false; |
| 192 | |
| 193 | SourceLocation loc = SS.getLastQualifierNameLoc(); |
| 194 | if (loc.isInvalid()) loc = SS.getRange().getBegin(); |
| 195 | |
| 196 | // The type must be complete. |
| 197 | if (RequireCompleteType(Loc: loc, T: Context.getCanonicalTagType(TD: tag), |
| 198 | DiagID: diag::err_incomplete_nested_name_spec, |
| 199 | Args: SS.getRange())) { |
| 200 | SS.SetInvalid(SS.getRange()); |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | if (auto *EnumD = dyn_cast<EnumDecl>(Val: tag)) |
| 205 | // Fixed enum types and scoped enum instantiations are complete, but they |
| 206 | // aren't valid as scopes until we see or instantiate their definition. |
| 207 | return RequireCompleteEnumDecl(D: EnumD, L: loc, SS: &SS); |
| 208 | |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | /// Require that the EnumDecl is completed with its enumerators defined or |
| 213 | /// instantiated. SS, if provided, is the ScopeRef parsed. |
| 214 | /// |
| 215 | bool Sema::RequireCompleteEnumDecl(EnumDecl *EnumD, SourceLocation L, |
| 216 | CXXScopeSpec *SS) { |
| 217 | if (EnumDecl *Def = EnumD->getDefinition(); |
| 218 | Def && Def->isCompleteDefinition()) { |
| 219 | // If we know about the definition but it is not visible, complain. |
| 220 | NamedDecl *SuggestedDef = nullptr; |
| 221 | if (!hasReachableDefinition(D: Def, Suggested: &SuggestedDef, |
| 222 | /*OnlyNeedComplete*/ false)) { |
| 223 | // If the user is going to see an error here, recover by making the |
| 224 | // definition visible. |
| 225 | bool TreatAsComplete = !isSFINAEContext(); |
| 226 | diagnoseMissingImport(Loc: L, Decl: SuggestedDef, MIK: MissingImportKind::Definition, |
| 227 | /*Recover*/ TreatAsComplete); |
| 228 | return !TreatAsComplete; |
| 229 | } |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | // Try to instantiate the definition, if this is a specialization of an |
| 234 | // enumeration temploid. |
| 235 | if (EnumDecl *Pattern = EnumD->getInstantiatedFromMemberEnum()) { |
| 236 | MemberSpecializationInfo *MSI = EnumD->getMemberSpecializationInfo(); |
| 237 | if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) { |
| 238 | if (InstantiateEnum(PointOfInstantiation: L, Instantiation: EnumD, Pattern, |
| 239 | TemplateArgs: getTemplateInstantiationArgs(D: EnumD), |
| 240 | TSK: TSK_ImplicitInstantiation)) { |
| 241 | if (SS) |
| 242 | SS->SetInvalid(SS->getRange()); |
| 243 | return true; |
| 244 | } |
| 245 | return false; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | if (SS) { |
| 250 | Diag(Loc: L, DiagID: diag::err_incomplete_nested_name_spec) |
| 251 | << Context.getCanonicalTagType(TD: EnumD) << SS->getRange(); |
| 252 | SS->SetInvalid(SS->getRange()); |
| 253 | } else { |
| 254 | Diag(Loc: L, DiagID: diag::err_incomplete_enum) << Context.getCanonicalTagType(TD: EnumD); |
| 255 | Diag(Loc: EnumD->getLocation(), DiagID: diag::note_declared_at); |
| 256 | } |
| 257 | |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | bool Sema::ActOnCXXGlobalScopeSpecifier(SourceLocation CCLoc, |
| 262 | CXXScopeSpec &SS) { |
| 263 | SS.MakeGlobal(Context, ColonColonLoc: CCLoc); |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | bool Sema::ActOnSuperScopeSpecifier(SourceLocation SuperLoc, |
| 268 | SourceLocation ColonColonLoc, |
| 269 | CXXScopeSpec &SS) { |
| 270 | if (getCurLambda()) { |
| 271 | Diag(Loc: SuperLoc, DiagID: diag::err_super_in_lambda_unsupported); |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | CXXRecordDecl *RD = nullptr; |
| 276 | for (Scope *S = getCurScope(); S; S = S->getParent()) { |
| 277 | if (S->isFunctionScope()) { |
| 278 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: S->getEntity())) |
| 279 | RD = MD->getParent(); |
| 280 | break; |
| 281 | } |
| 282 | if (S->isClassScope()) { |
| 283 | RD = cast<CXXRecordDecl>(Val: S->getEntity()); |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if (!RD) { |
| 289 | Diag(Loc: SuperLoc, DiagID: diag::err_invalid_super_scope); |
| 290 | return true; |
| 291 | } else if (RD->getNumBases() == 0) { |
| 292 | Diag(Loc: SuperLoc, DiagID: diag::err_no_base_classes) << RD->getName(); |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | SS.MakeMicrosoftSuper(Context, RD, SuperLoc, ColonColonLoc); |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | bool Sema::isAcceptableNestedNameSpecifier(const NamedDecl *SD, |
| 301 | bool *IsExtension) { |
| 302 | if (!SD) |
| 303 | return false; |
| 304 | |
| 305 | SD = SD->getUnderlyingDecl(); |
| 306 | |
| 307 | // Namespace and namespace aliases are fine. |
| 308 | if (isa<NamespaceDecl>(Val: SD)) |
| 309 | return true; |
| 310 | |
| 311 | if (!isa<TypeDecl>(Val: SD)) |
| 312 | return false; |
| 313 | |
| 314 | // Determine whether we have a class (or, in C++11, an enum) or |
| 315 | // a typedef thereof. If so, build the nested-name-specifier. |
| 316 | if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(Val: SD)) { |
| 317 | if (TD->getUnderlyingType()->isRecordType()) |
| 318 | return true; |
| 319 | if (TD->getUnderlyingType()->isEnumeralType()) { |
| 320 | if (Context.getLangOpts().CPlusPlus11) |
| 321 | return true; |
| 322 | if (IsExtension) |
| 323 | *IsExtension = true; |
| 324 | } |
| 325 | } else if (isa<RecordDecl>(Val: SD)) { |
| 326 | return true; |
| 327 | } else if (isa<EnumDecl>(Val: SD)) { |
| 328 | if (Context.getLangOpts().CPlusPlus11) |
| 329 | return true; |
| 330 | if (IsExtension) |
| 331 | *IsExtension = true; |
| 332 | } |
| 333 | if (auto *TD = dyn_cast<TagDecl>(Val: SD)) { |
| 334 | if (TD->isDependentType()) |
| 335 | return true; |
| 336 | } else if (Context.getCanonicalTypeDeclType(TD: cast<TypeDecl>(Val: SD)) |
| 337 | ->isDependentType()) { |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier NNS) { |
| 345 | if (!S) |
| 346 | return nullptr; |
| 347 | |
| 348 | while (NNS.getKind() == NestedNameSpecifier::Kind::Type) { |
| 349 | const Type *T = NNS.getAsType(); |
| 350 | if ((NNS = T->getPrefix())) |
| 351 | continue; |
| 352 | |
| 353 | const auto *DNT = dyn_cast<DependentNameType>(Val: T); |
| 354 | if (!DNT) |
| 355 | break; |
| 356 | |
| 357 | LookupResult Found(*this, DNT->getIdentifier(), SourceLocation(), |
| 358 | LookupNestedNameSpecifierName); |
| 359 | LookupName(R&: Found, S); |
| 360 | assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet" ); |
| 361 | |
| 362 | if (!Found.isSingleResult()) |
| 363 | return nullptr; |
| 364 | |
| 365 | NamedDecl *Result = Found.getFoundDecl(); |
| 366 | if (isAcceptableNestedNameSpecifier(SD: Result)) |
| 367 | return Result; |
| 368 | } |
| 369 | return nullptr; |
| 370 | } |
| 371 | |
| 372 | namespace { |
| 373 | |
| 374 | // Callback to only accept typo corrections that can be a valid C++ member |
| 375 | // initializer: either a non-static field member or a base class. |
| 376 | class NestedNameSpecifierValidatorCCC final |
| 377 | : public QualifiedLookupValidatorCCC { |
| 378 | public: |
| 379 | explicit NestedNameSpecifierValidatorCCC(Sema &SRef, bool HasQualifier) |
| 380 | : QualifiedLookupValidatorCCC(HasQualifier), SRef(SRef) {} |
| 381 | |
| 382 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| 383 | if (!QualifiedLookupValidatorCCC::ValidateCandidate(Candidate: candidate)) |
| 384 | return false; |
| 385 | const NamedDecl *ND = candidate.getCorrectionDecl(); |
| 386 | if (!SRef.isAcceptableNestedNameSpecifier(SD: ND)) |
| 387 | return false; |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| 392 | return std::make_unique<NestedNameSpecifierValidatorCCC>(args&: *this); |
| 393 | } |
| 394 | |
| 395 | private: |
| 396 | Sema &SRef; |
| 397 | }; |
| 398 | } |
| 399 | |
| 400 | [[nodiscard]] static bool ExtendNestedNameSpecifier(Sema &S, CXXScopeSpec &SS, |
| 401 | const NamedDecl *ND, |
| 402 | SourceLocation NameLoc, |
| 403 | SourceLocation CCLoc) { |
| 404 | TypeLocBuilder TLB; |
| 405 | QualType T; |
| 406 | if (const auto *USD = dyn_cast<UsingShadowDecl>(Val: ND)) { |
| 407 | T = S.Context.getUsingType(Keyword: ElaboratedTypeKeyword::None, Qualifier: SS.getScopeRep(), |
| 408 | D: USD); |
| 409 | TLB.push<UsingTypeLoc>(T).set(/*ElaboratedKeywordLoc=*/SourceLocation(), |
| 410 | QualifierLoc: SS.getWithLocInContext(Context&: S.Context), NameLoc); |
| 411 | } else if (const auto *TD = dyn_cast<TypeDecl>(Val: ND)) { |
| 412 | T = S.Context.getTypeDeclType(Keyword: ElaboratedTypeKeyword::None, Qualifier: SS.getScopeRep(), |
| 413 | Decl: TD); |
| 414 | switch (T->getTypeClass()) { |
| 415 | case Type::Record: |
| 416 | case Type::InjectedClassName: |
| 417 | case Type::Enum: { |
| 418 | auto TTL = TLB.push<TagTypeLoc>(T); |
| 419 | TTL.setElaboratedKeywordLoc(SourceLocation()); |
| 420 | TTL.setQualifierLoc(SS.getWithLocInContext(Context&: S.Context)); |
| 421 | TTL.setNameLoc(NameLoc); |
| 422 | break; |
| 423 | } |
| 424 | case Type::Typedef: |
| 425 | TLB.push<TypedefTypeLoc>(T).set(/*ElaboratedKeywordLoc=*/SourceLocation(), |
| 426 | QualifierLoc: SS.getWithLocInContext(Context&: S.Context), |
| 427 | NameLoc); |
| 428 | break; |
| 429 | case Type::UnresolvedUsing: |
| 430 | TLB.push<UnresolvedUsingTypeLoc>(T).set( |
| 431 | /*ElaboratedKeywordLoc=*/SourceLocation(), |
| 432 | QualifierLoc: SS.getWithLocInContext(Context&: S.Context), NameLoc); |
| 433 | break; |
| 434 | default: |
| 435 | assert(SS.isEmpty()); |
| 436 | T = S.Context.getTypeDeclType(Decl: TD); |
| 437 | TLB.pushTypeSpec(T).setNameLoc(NameLoc); |
| 438 | break; |
| 439 | } |
| 440 | } else { |
| 441 | return false; |
| 442 | } |
| 443 | SS.clear(); |
| 444 | SS.Make(Context&: S.Context, TL: TLB.getTypeLocInContext(Context&: S.Context, T), ColonColonLoc: CCLoc); |
| 445 | return true; |
| 446 | } |
| 447 | |
| 448 | bool Sema::BuildCXXNestedNameSpecifier(Scope *S, NestedNameSpecInfo &IdInfo, |
| 449 | bool EnteringContext, CXXScopeSpec &SS, |
| 450 | NamedDecl *ScopeLookupResult, |
| 451 | bool ErrorRecoveryLookup, |
| 452 | bool *IsCorrectedToColon, |
| 453 | bool OnlyNamespace) { |
| 454 | if (IdInfo.Identifier->isEditorPlaceholder()) |
| 455 | return true; |
| 456 | LookupResult Found(*this, IdInfo.Identifier, IdInfo.IdentifierLoc, |
| 457 | OnlyNamespace ? LookupNamespaceName |
| 458 | : LookupNestedNameSpecifierName); |
| 459 | QualType ObjectType = GetTypeFromParser(Ty: IdInfo.ObjectType); |
| 460 | |
| 461 | // Determine where to perform name lookup |
| 462 | DeclContext *LookupCtx = nullptr; |
| 463 | bool isDependent = false; |
| 464 | if (IsCorrectedToColon) |
| 465 | *IsCorrectedToColon = false; |
| 466 | if (!ObjectType.isNull()) { |
| 467 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 468 | // x->B::f, and we are looking into the type of the object. |
| 469 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist" ); |
| 470 | LookupCtx = computeDeclContext(T: ObjectType); |
| 471 | isDependent = ObjectType->isDependentType(); |
| 472 | } else if (SS.isSet()) { |
| 473 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 474 | // so look into the context associated with the prior nested-name-specifier. |
| 475 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 476 | isDependent = isDependentScopeSpecifier(SS); |
| 477 | Found.setContextRange(SS.getRange()); |
| 478 | } |
| 479 | |
| 480 | bool ObjectTypeSearchedInScope = false; |
| 481 | if (LookupCtx) { |
| 482 | // Perform "qualified" name lookup into the declaration context we |
| 483 | // computed, which is either the type of the base of a member access |
| 484 | // expression or the declaration context associated with a prior |
| 485 | // nested-name-specifier. |
| 486 | |
| 487 | // The declaration context must be complete. |
| 488 | if (!LookupCtx->isDependentContext() && |
| 489 | RequireCompleteDeclContext(SS, DC: LookupCtx)) |
| 490 | return true; |
| 491 | |
| 492 | LookupQualifiedName(R&: Found, LookupCtx); |
| 493 | |
| 494 | if (!ObjectType.isNull() && Found.empty()) { |
| 495 | // C++ [basic.lookup.classref]p4: |
| 496 | // If the id-expression in a class member access is a qualified-id of |
| 497 | // the form |
| 498 | // |
| 499 | // class-name-or-namespace-name::... |
| 500 | // |
| 501 | // the class-name-or-namespace-name following the . or -> operator is |
| 502 | // looked up both in the context of the entire postfix-expression and in |
| 503 | // the scope of the class of the object expression. If the name is found |
| 504 | // only in the scope of the class of the object expression, the name |
| 505 | // shall refer to a class-name. If the name is found only in the |
| 506 | // context of the entire postfix-expression, the name shall refer to a |
| 507 | // class-name or namespace-name. [...] |
| 508 | // |
| 509 | // Qualified name lookup into a class will not find a namespace-name, |
| 510 | // so we do not need to diagnose that case specifically. However, |
| 511 | // this qualified name lookup may find nothing. In that case, perform |
| 512 | // unqualified name lookup in the given scope (if available) or |
| 513 | // reconstruct the result from when name lookup was performed at template |
| 514 | // definition time. |
| 515 | if (S) |
| 516 | LookupName(R&: Found, S); |
| 517 | else if (ScopeLookupResult) |
| 518 | Found.addDecl(D: ScopeLookupResult); |
| 519 | |
| 520 | ObjectTypeSearchedInScope = true; |
| 521 | } |
| 522 | } else if (!isDependent) { |
| 523 | // Perform unqualified name lookup in the current scope. |
| 524 | LookupName(R&: Found, S); |
| 525 | } |
| 526 | |
| 527 | if (Found.isAmbiguous()) |
| 528 | return true; |
| 529 | |
| 530 | // If we performed lookup into a dependent context and did not find anything, |
| 531 | // that's fine: just build a dependent nested-name-specifier. |
| 532 | if (Found.empty() && isDependent && |
| 533 | !(LookupCtx && LookupCtx->isRecord() && |
| 534 | (!cast<CXXRecordDecl>(Val: LookupCtx)->hasDefinition() || |
| 535 | !cast<CXXRecordDecl>(Val: LookupCtx)->hasAnyDependentBases()))) { |
| 536 | // Don't speculate if we're just trying to improve error recovery. |
| 537 | if (ErrorRecoveryLookup) |
| 538 | return true; |
| 539 | |
| 540 | // We were not able to compute the declaration context for a dependent |
| 541 | // base object type or prior nested-name-specifier, so this |
| 542 | // nested-name-specifier refers to an unknown specialization. Just build |
| 543 | // a dependent nested-name-specifier. |
| 544 | |
| 545 | TypeLocBuilder TLB; |
| 546 | |
| 547 | QualType DTN = Context.getDependentNameType( |
| 548 | Keyword: ElaboratedTypeKeyword::None, NNS: SS.getScopeRep(), Name: IdInfo.Identifier); |
| 549 | auto DTNL = TLB.push<DependentNameTypeLoc>(T: DTN); |
| 550 | DTNL.setElaboratedKeywordLoc(SourceLocation()); |
| 551 | DTNL.setNameLoc(IdInfo.IdentifierLoc); |
| 552 | DTNL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 553 | |
| 554 | SS.clear(); |
| 555 | SS.Make(Context, TL: TLB.getTypeLocInContext(Context, T: DTN), ColonColonLoc: IdInfo.CCLoc); |
| 556 | return false; |
| 557 | } |
| 558 | |
| 559 | if (Found.empty() && !ErrorRecoveryLookup) { |
| 560 | // If identifier is not found as class-name-or-namespace-name, but is found |
| 561 | // as other entity, don't look for typos. |
| 562 | LookupResult R(*this, Found.getLookupNameInfo(), LookupOrdinaryName); |
| 563 | if (LookupCtx) |
| 564 | LookupQualifiedName(R, LookupCtx); |
| 565 | else if (S && !isDependent) |
| 566 | LookupName(R, S); |
| 567 | if (!R.empty()) { |
| 568 | // Don't diagnose problems with this speculative lookup. |
| 569 | R.suppressDiagnostics(); |
| 570 | // The identifier is found in ordinary lookup. If correction to colon is |
| 571 | // allowed, suggest replacement to ':'. |
| 572 | if (IsCorrectedToColon) { |
| 573 | *IsCorrectedToColon = true; |
| 574 | Diag(Loc: IdInfo.CCLoc, DiagID: diag::err_nested_name_spec_is_not_class) |
| 575 | << IdInfo.Identifier << getLangOpts().CPlusPlus |
| 576 | << FixItHint::CreateReplacement(RemoveRange: IdInfo.CCLoc, Code: ":" ); |
| 577 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 578 | Diag(Loc: ND->getLocation(), DiagID: diag::note_declared_at); |
| 579 | return true; |
| 580 | } |
| 581 | // Replacement '::' -> ':' is not allowed, just issue respective error. |
| 582 | Diag(Loc: R.getNameLoc(), DiagID: OnlyNamespace |
| 583 | ? unsigned(diag::err_expected_namespace_name) |
| 584 | : unsigned(diag::err_expected_class_or_namespace)) |
| 585 | << IdInfo.Identifier << getLangOpts().CPlusPlus; |
| 586 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 587 | Diag(Loc: ND->getLocation(), DiagID: diag::note_entity_declared_at) |
| 588 | << IdInfo.Identifier; |
| 589 | return true; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | if (Found.empty() && !ErrorRecoveryLookup && !getLangOpts().MSVCCompat) { |
| 594 | // We haven't found anything, and we're not recovering from a |
| 595 | // different kind of error, so look for typos. |
| 596 | DeclarationName Name = Found.getLookupName(); |
| 597 | Found.clear(); |
| 598 | NestedNameSpecifierValidatorCCC CCC(*this, /*HasQualifier=*/!SS.isEmpty()); |
| 599 | if (TypoCorrection Corrected = CorrectTypo( |
| 600 | Typo: Found.getLookupNameInfo(), LookupKind: Found.getLookupKind(), S, SS: &SS, CCC, |
| 601 | Mode: CorrectTypoKind::ErrorRecovery, MemberContext: LookupCtx, EnteringContext)) { |
| 602 | if (LookupCtx) { |
| 603 | bool DroppedSpecifier = |
| 604 | Corrected.WillReplaceSpecifier() && |
| 605 | Name.getAsString() == Corrected.getAsString(LO: getLangOpts()); |
| 606 | if (DroppedSpecifier) |
| 607 | SS.clear(); |
| 608 | diagnoseTypo(Correction: Corrected, TypoDiag: PDiag(DiagID: diag::err_no_member_suggest) |
| 609 | << Name << LookupCtx << DroppedSpecifier |
| 610 | << SS.getRange()); |
| 611 | } else |
| 612 | diagnoseTypo(Correction: Corrected, TypoDiag: PDiag(DiagID: diag::err_undeclared_var_use_suggest) |
| 613 | << Name); |
| 614 | |
| 615 | if (Corrected.getCorrectionSpecifier()) |
| 616 | SS.MakeTrivial(Context, Qualifier: Corrected.getCorrectionSpecifier(), |
| 617 | R: SourceRange(Found.getNameLoc())); |
| 618 | |
| 619 | if (NamedDecl *ND = Corrected.getFoundDecl()) |
| 620 | Found.addDecl(D: ND); |
| 621 | Found.setLookupName(Corrected.getCorrection()); |
| 622 | } else { |
| 623 | Found.setLookupName(IdInfo.Identifier); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | NamedDecl *SD = |
| 628 | Found.isSingleResult() ? Found.getRepresentativeDecl() : nullptr; |
| 629 | bool IsExtension = false; |
| 630 | bool AcceptSpec = isAcceptableNestedNameSpecifier(SD, IsExtension: &IsExtension); |
| 631 | if (!AcceptSpec && IsExtension) { |
| 632 | AcceptSpec = true; |
| 633 | Diag(Loc: IdInfo.IdentifierLoc, DiagID: diag::ext_nested_name_spec_is_enum); |
| 634 | } |
| 635 | if (AcceptSpec) { |
| 636 | if (!ObjectType.isNull() && !ObjectTypeSearchedInScope && |
| 637 | !getLangOpts().CPlusPlus11) { |
| 638 | // C++03 [basic.lookup.classref]p4: |
| 639 | // [...] If the name is found in both contexts, the |
| 640 | // class-name-or-namespace-name shall refer to the same entity. |
| 641 | // |
| 642 | // We already found the name in the scope of the object. Now, look |
| 643 | // into the current scope (the scope of the postfix-expression) to |
| 644 | // see if we can find the same name there. As above, if there is no |
| 645 | // scope, reconstruct the result from the template instantiation itself. |
| 646 | // |
| 647 | // Note that C++11 does *not* perform this redundant lookup. |
| 648 | NamedDecl *OuterDecl; |
| 649 | if (S) { |
| 650 | LookupResult FoundOuter(*this, IdInfo.Identifier, IdInfo.IdentifierLoc, |
| 651 | LookupNestedNameSpecifierName); |
| 652 | LookupName(R&: FoundOuter, S); |
| 653 | OuterDecl = FoundOuter.getAsSingle<NamedDecl>(); |
| 654 | } else |
| 655 | OuterDecl = ScopeLookupResult; |
| 656 | |
| 657 | if (isAcceptableNestedNameSpecifier(SD: OuterDecl) && |
| 658 | OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() && |
| 659 | (!isa<TypeDecl>(Val: OuterDecl) || !isa<TypeDecl>(Val: SD) || |
| 660 | !Context.hasSameType( |
| 661 | T1: Context.getCanonicalTypeDeclType(TD: cast<TypeDecl>(Val: OuterDecl)), |
| 662 | T2: Context.getCanonicalTypeDeclType(TD: cast<TypeDecl>(Val: SD))))) { |
| 663 | if (ErrorRecoveryLookup) |
| 664 | return true; |
| 665 | |
| 666 | Diag(Loc: IdInfo.IdentifierLoc, |
| 667 | DiagID: diag::err_nested_name_member_ref_lookup_ambiguous) |
| 668 | << IdInfo.Identifier; |
| 669 | Diag(Loc: SD->getLocation(), DiagID: diag::note_ambig_member_ref_object_type) |
| 670 | << ObjectType; |
| 671 | Diag(Loc: OuterDecl->getLocation(), DiagID: diag::note_ambig_member_ref_scope); |
| 672 | |
| 673 | // Fall through so that we'll pick the name we found in the object |
| 674 | // type, since that's probably what the user wanted anyway. |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | MarkAnyDeclReferenced(Loc: SD->getLocation(), D: SD, /*OdrUse=*/MightBeOdrUse: false); |
| 679 | |
| 680 | // If we're just performing this lookup for error-recovery purposes, |
| 681 | // don't extend the nested-name-specifier. Just return now. |
| 682 | if (ErrorRecoveryLookup) |
| 683 | return false; |
| 684 | |
| 685 | // The use of a nested name specifier may trigger deprecation warnings. |
| 686 | DiagnoseUseOfDecl(D: SD, Locs: IdInfo.CCLoc); |
| 687 | |
| 688 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Val: SD)) { |
| 689 | SS.Extend(Context, Namespace, NamespaceLoc: IdInfo.IdentifierLoc, ColonColonLoc: IdInfo.CCLoc); |
| 690 | return false; |
| 691 | } |
| 692 | |
| 693 | if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(Val: SD)) { |
| 694 | SS.Extend(Context, Namespace: Alias, NamespaceLoc: IdInfo.IdentifierLoc, ColonColonLoc: IdInfo.CCLoc); |
| 695 | return false; |
| 696 | } |
| 697 | |
| 698 | const auto *TD = cast<TypeDecl>(Val: SD->getUnderlyingDecl()); |
| 699 | if (isa<EnumDecl>(Val: TD)) |
| 700 | Diag(Loc: IdInfo.IdentifierLoc, DiagID: diag::warn_cxx98_compat_enum_nested_name_spec); |
| 701 | |
| 702 | [[maybe_unused]] bool IsType = ::ExtendNestedNameSpecifier( |
| 703 | S&: *this, SS, ND: SD, NameLoc: IdInfo.IdentifierLoc, CCLoc: IdInfo.CCLoc); |
| 704 | assert(IsType && "unhandled declaration kind" ); |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | // Otherwise, we have an error case. If we don't want diagnostics, just |
| 709 | // return an error now. |
| 710 | if (ErrorRecoveryLookup) |
| 711 | return true; |
| 712 | |
| 713 | // If we didn't find anything during our lookup, try again with |
| 714 | // ordinary name lookup, which can help us produce better error |
| 715 | // messages. |
| 716 | if (Found.empty()) { |
| 717 | Found.clear(Kind: LookupOrdinaryName); |
| 718 | LookupName(R&: Found, S); |
| 719 | } |
| 720 | |
| 721 | // In Microsoft mode, if we are within a templated function and we can't |
| 722 | // resolve Identifier, then extend the SS with Identifier. This will have |
| 723 | // the effect of resolving Identifier during template instantiation. |
| 724 | // The goal is to be able to resolve a function call whose |
| 725 | // nested-name-specifier is located inside a dependent base class. |
| 726 | // Example: |
| 727 | // |
| 728 | // class C { |
| 729 | // public: |
| 730 | // static void foo2() { } |
| 731 | // }; |
| 732 | // template <class T> class A { public: typedef C D; }; |
| 733 | // |
| 734 | // template <class T> class B : public A<T> { |
| 735 | // public: |
| 736 | // void foo() { D::foo2(); } |
| 737 | // }; |
| 738 | if (getLangOpts().MSVCCompat) { |
| 739 | DeclContext *DC = LookupCtx ? LookupCtx : CurContext; |
| 740 | if (DC->isDependentContext() && DC->isFunctionOrMethod()) { |
| 741 | CXXRecordDecl *ContainingClass = dyn_cast<CXXRecordDecl>(Val: DC->getParent()); |
| 742 | if (ContainingClass && ContainingClass->hasAnyDependentBases()) { |
| 743 | Diag(Loc: IdInfo.IdentifierLoc, |
| 744 | DiagID: diag::ext_undeclared_unqual_id_with_dependent_base) |
| 745 | << IdInfo.Identifier << ContainingClass; |
| 746 | |
| 747 | TypeLocBuilder TLB; |
| 748 | |
| 749 | // Fake up a nested-name-specifier that starts with the |
| 750 | // injected-class-name of the enclosing class. |
| 751 | // FIXME: This should be done as part of an adjustment, so that this |
| 752 | // doesn't get confused with something written in source. |
| 753 | QualType Result = |
| 754 | Context.getTagType(Keyword: ElaboratedTypeKeyword::None, Qualifier: SS.getScopeRep(), |
| 755 | TD: ContainingClass, /*OwnsTag=*/false); |
| 756 | auto TTL = TLB.push<TagTypeLoc>(T: Result); |
| 757 | TTL.setElaboratedKeywordLoc(SourceLocation()); |
| 758 | TTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 759 | TTL.setNameLoc(IdInfo.IdentifierLoc); |
| 760 | SS.Make(Context, TL: TLB.getTypeLocInContext(Context, T: Result), |
| 761 | ColonColonLoc: SourceLocation()); |
| 762 | |
| 763 | TLB.clear(); |
| 764 | |
| 765 | // Form a DependentNameType. |
| 766 | QualType DTN = Context.getDependentNameType( |
| 767 | Keyword: ElaboratedTypeKeyword::None, NNS: SS.getScopeRep(), Name: IdInfo.Identifier); |
| 768 | auto DTNL = TLB.push<DependentNameTypeLoc>(T: DTN); |
| 769 | DTNL.setElaboratedKeywordLoc(SourceLocation()); |
| 770 | DTNL.setNameLoc(IdInfo.IdentifierLoc); |
| 771 | DTNL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 772 | SS.clear(); |
| 773 | SS.Make(Context, TL: TLB.getTypeLocInContext(Context, T: DTN), ColonColonLoc: IdInfo.CCLoc); |
| 774 | return false; |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | if (!Found.empty()) { |
| 780 | const auto *ND = Found.getAsSingle<NamedDecl>(); |
| 781 | if (!ND) { |
| 782 | Diag(Loc: IdInfo.IdentifierLoc, DiagID: diag::err_expected_class_or_namespace) |
| 783 | << IdInfo.Identifier << getLangOpts().CPlusPlus; |
| 784 | return true; |
| 785 | } |
| 786 | if (Found.getLookupKind() == LookupNestedNameSpecifierName && |
| 787 | ::ExtendNestedNameSpecifier(S&: *this, SS, ND, NameLoc: IdInfo.IdentifierLoc, |
| 788 | CCLoc: IdInfo.CCLoc)) { |
| 789 | const Type *T = SS.getScopeRep().getAsType(); |
| 790 | Diag(Loc: IdInfo.IdentifierLoc, DiagID: diag::err_expected_class_or_namespace) |
| 791 | << QualType(T, 0) << getLangOpts().CPlusPlus; |
| 792 | // Recover with this type if it would be a valid nested name specifier. |
| 793 | return !T->getAsCanonical<TagType>(); |
| 794 | } |
| 795 | if (isa<TemplateDecl>(Val: ND)) { |
| 796 | ParsedType SuggestedType; |
| 797 | DiagnoseUnknownTypeName(II&: IdInfo.Identifier, IILoc: IdInfo.IdentifierLoc, S, SS: &SS, |
| 798 | SuggestedType); |
| 799 | } else { |
| 800 | Diag(Loc: IdInfo.IdentifierLoc, DiagID: diag::err_expected_class_or_namespace) |
| 801 | << IdInfo.Identifier << getLangOpts().CPlusPlus; |
| 802 | if (NamedDecl *ND = Found.getAsSingle<NamedDecl>()) |
| 803 | Diag(Loc: ND->getLocation(), DiagID: diag::note_entity_declared_at) |
| 804 | << IdInfo.Identifier; |
| 805 | } |
| 806 | } else if (SS.isSet()) |
| 807 | Diag(Loc: IdInfo.IdentifierLoc, DiagID: diag::err_no_member) << IdInfo.Identifier |
| 808 | << LookupCtx << SS.getRange(); |
| 809 | else |
| 810 | Diag(Loc: IdInfo.IdentifierLoc, DiagID: diag::err_undeclared_var_use) |
| 811 | << IdInfo.Identifier; |
| 812 | |
| 813 | return true; |
| 814 | } |
| 815 | |
| 816 | bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, NestedNameSpecInfo &IdInfo, |
| 817 | bool EnteringContext, CXXScopeSpec &SS, |
| 818 | bool *IsCorrectedToColon, |
| 819 | bool OnlyNamespace) { |
| 820 | if (SS.isInvalid()) |
| 821 | return true; |
| 822 | |
| 823 | return BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS, |
| 824 | /*ScopeLookupResult=*/nullptr, ErrorRecoveryLookup: false, |
| 825 | IsCorrectedToColon, OnlyNamespace); |
| 826 | } |
| 827 | |
| 828 | bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS, |
| 829 | const DeclSpec &DS, |
| 830 | SourceLocation ColonColonLoc) { |
| 831 | if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error) |
| 832 | return true; |
| 833 | |
| 834 | assert(DS.getTypeSpecType() == DeclSpec::TST_decltype); |
| 835 | |
| 836 | QualType T = BuildDecltypeType(E: DS.getRepAsExpr()); |
| 837 | if (T.isNull()) |
| 838 | return true; |
| 839 | |
| 840 | if (!T->isDependentType() && !isa<TagType>(Val: T.getCanonicalType())) { |
| 841 | Diag(Loc: DS.getTypeSpecTypeLoc(), DiagID: diag::err_expected_class_or_namespace) |
| 842 | << T << getLangOpts().CPlusPlus; |
| 843 | return true; |
| 844 | } |
| 845 | |
| 846 | assert(SS.isEmpty()); |
| 847 | |
| 848 | TypeLocBuilder TLB; |
| 849 | DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T); |
| 850 | DecltypeTL.setDecltypeLoc(DS.getTypeSpecTypeLoc()); |
| 851 | DecltypeTL.setRParenLoc(DS.getTypeofParensRange().getEnd()); |
| 852 | SS.Make(Context, TL: TLB.getTypeLocInContext(Context, T), ColonColonLoc); |
| 853 | return false; |
| 854 | } |
| 855 | |
| 856 | bool Sema::ActOnCXXNestedNameSpecifierIndexedPack(CXXScopeSpec &SS, |
| 857 | const DeclSpec &DS, |
| 858 | SourceLocation ColonColonLoc, |
| 859 | QualType Type) { |
| 860 | if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error) |
| 861 | return true; |
| 862 | |
| 863 | assert(DS.getTypeSpecType() == DeclSpec::TST_typename_pack_indexing); |
| 864 | |
| 865 | if (Type.isNull()) |
| 866 | return true; |
| 867 | |
| 868 | assert(SS.isEmpty()); |
| 869 | |
| 870 | TypeLocBuilder TLB; |
| 871 | TLB.pushTrivial(Context&: getASTContext(), |
| 872 | T: cast<PackIndexingType>(Val: Type.getTypePtr())->getPattern(), |
| 873 | Loc: DS.getBeginLoc()); |
| 874 | PackIndexingTypeLoc PIT = TLB.push<PackIndexingTypeLoc>(T: Type); |
| 875 | PIT.setEllipsisLoc(DS.getEllipsisLoc()); |
| 876 | SS.Make(Context, TL: TLB.getTypeLocInContext(Context, T: Type), ColonColonLoc); |
| 877 | return false; |
| 878 | } |
| 879 | |
| 880 | bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS, |
| 881 | NestedNameSpecInfo &IdInfo, |
| 882 | bool EnteringContext) { |
| 883 | if (SS.isInvalid()) |
| 884 | return false; |
| 885 | |
| 886 | return !BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS, |
| 887 | /*ScopeLookupResult=*/nullptr, ErrorRecoveryLookup: true); |
| 888 | } |
| 889 | |
| 890 | bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, |
| 891 | CXXScopeSpec &SS, |
| 892 | SourceLocation TemplateKWLoc, |
| 893 | TemplateTy OpaqueTemplate, |
| 894 | SourceLocation TemplateNameLoc, |
| 895 | SourceLocation LAngleLoc, |
| 896 | ASTTemplateArgsPtr TemplateArgsIn, |
| 897 | SourceLocation RAngleLoc, |
| 898 | SourceLocation CCLoc, |
| 899 | bool EnteringContext) { |
| 900 | if (SS.isInvalid()) |
| 901 | return true; |
| 902 | |
| 903 | // Translate the parser's template argument list in our AST format. |
| 904 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 905 | translateTemplateArguments(In: TemplateArgsIn, Out&: TemplateArgs); |
| 906 | |
| 907 | // We were able to resolve the template name to an actual template. |
| 908 | // Build an appropriate nested-name-specifier. |
| 909 | QualType T = CheckTemplateIdType( |
| 910 | Keyword: ElaboratedTypeKeyword::None, Template: OpaqueTemplate.get(), TemplateLoc: TemplateNameLoc, |
| 911 | TemplateArgs, /*Scope=*/S, /*ForNestedNameSpecifier=*/true); |
| 912 | if (T.isNull()) |
| 913 | return true; |
| 914 | |
| 915 | // Alias template specializations can produce types which are not valid |
| 916 | // nested name specifiers. |
| 917 | if (!T->isDependentType() && !isa<TagType>(Val: T.getCanonicalType())) { |
| 918 | Diag(Loc: TemplateNameLoc, DiagID: diag::err_nested_name_spec_non_tag) << T; |
| 919 | NoteAllFoundTemplates(Name: OpaqueTemplate.get()); |
| 920 | return true; |
| 921 | } |
| 922 | |
| 923 | // Provide source-location information for the template specialization type. |
| 924 | TypeLocBuilder TLB; |
| 925 | TLB.push<TemplateSpecializationTypeLoc>(T).set( |
| 926 | /*ElaboratedKeywordLoc=*/SourceLocation(), |
| 927 | QualifierLoc: SS.getWithLocInContext(Context), TemplateKeywordLoc: TemplateKWLoc, NameLoc: TemplateNameLoc, |
| 928 | TAL: TemplateArgs); |
| 929 | |
| 930 | SS.clear(); |
| 931 | SS.Make(Context, TL: TLB.getTypeLocInContext(Context, T), ColonColonLoc: CCLoc); |
| 932 | return false; |
| 933 | } |
| 934 | |
| 935 | namespace { |
| 936 | /// A structure that stores a nested-name-specifier annotation, |
| 937 | /// including both the nested-name-specifier |
| 938 | struct NestedNameSpecifierAnnotation { |
| 939 | NestedNameSpecifier NNS = std::nullopt; |
| 940 | }; |
| 941 | } |
| 942 | |
| 943 | void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) { |
| 944 | if (SS.isEmpty() || SS.isInvalid()) |
| 945 | return nullptr; |
| 946 | |
| 947 | void *Mem = Context.Allocate( |
| 948 | Size: (sizeof(NestedNameSpecifierAnnotation) + SS.location_size()), |
| 949 | Align: alignof(NestedNameSpecifierAnnotation)); |
| 950 | NestedNameSpecifierAnnotation *Annotation |
| 951 | = new (Mem) NestedNameSpecifierAnnotation; |
| 952 | Annotation->NNS = SS.getScopeRep(); |
| 953 | memcpy(dest: Annotation + 1, src: SS.location_data(), n: SS.location_size()); |
| 954 | return Annotation; |
| 955 | } |
| 956 | |
| 957 | void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr, |
| 958 | SourceRange AnnotationRange, |
| 959 | CXXScopeSpec &SS) { |
| 960 | if (!AnnotationPtr) { |
| 961 | SS.SetInvalid(AnnotationRange); |
| 962 | return; |
| 963 | } |
| 964 | |
| 965 | NestedNameSpecifierAnnotation *Annotation |
| 966 | = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr); |
| 967 | SS.Adopt(Other: NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1)); |
| 968 | } |
| 969 | |
| 970 | bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { |
| 971 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec." ); |
| 972 | |
| 973 | // Don't enter a declarator context when the current context is an Objective-C |
| 974 | // declaration. |
| 975 | if (isa<ObjCContainerDecl>(Val: CurContext) || isa<ObjCMethodDecl>(Val: CurContext)) |
| 976 | return false; |
| 977 | |
| 978 | // There are only two places a well-formed program may qualify a |
| 979 | // declarator: first, when defining a namespace or class member |
| 980 | // out-of-line, and second, when naming an explicitly-qualified |
| 981 | // friend function. The latter case is governed by |
| 982 | // C++03 [basic.lookup.unqual]p10: |
| 983 | // In a friend declaration naming a member function, a name used |
| 984 | // in the function declarator and not part of a template-argument |
| 985 | // in a template-id is first looked up in the scope of the member |
| 986 | // function's class. If it is not found, or if the name is part of |
| 987 | // a template-argument in a template-id, the look up is as |
| 988 | // described for unqualified names in the definition of the class |
| 989 | // granting friendship. |
| 990 | // i.e. we don't push a scope unless it's a class member. |
| 991 | |
| 992 | switch (SS.getScopeRep().getKind()) { |
| 993 | case NestedNameSpecifier::Kind::Global: |
| 994 | case NestedNameSpecifier::Kind::Namespace: |
| 995 | // These are always namespace scopes. We never want to enter a |
| 996 | // namespace scope from anything but a file context. |
| 997 | return CurContext->getRedeclContext()->isFileContext(); |
| 998 | |
| 999 | case NestedNameSpecifier::Kind::Type: |
| 1000 | case NestedNameSpecifier::Kind::MicrosoftSuper: |
| 1001 | // These are never namespace scopes. |
| 1002 | return true; |
| 1003 | |
| 1004 | case NestedNameSpecifier::Kind::Null: |
| 1005 | llvm_unreachable("unexpected null nested name specifier" ); |
| 1006 | } |
| 1007 | |
| 1008 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!" ); |
| 1009 | } |
| 1010 | |
| 1011 | bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) { |
| 1012 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec." ); |
| 1013 | |
| 1014 | if (SS.isInvalid()) return true; |
| 1015 | |
| 1016 | DeclContext *DC = computeDeclContext(SS, EnteringContext: true); |
| 1017 | if (!DC) return true; |
| 1018 | |
| 1019 | // Before we enter a declarator's context, we need to make sure that |
| 1020 | // it is a complete declaration context. |
| 1021 | if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC)) |
| 1022 | return true; |
| 1023 | |
| 1024 | EnterDeclaratorContext(S, DC); |
| 1025 | |
| 1026 | // Rebuild the nested name specifier for the new scope. |
| 1027 | if (DC->isDependentContext()) |
| 1028 | RebuildNestedNameSpecifierInCurrentInstantiation(SS); |
| 1029 | |
| 1030 | return false; |
| 1031 | } |
| 1032 | |
| 1033 | void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { |
| 1034 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec." ); |
| 1035 | if (SS.isInvalid()) |
| 1036 | return; |
| 1037 | assert(!SS.isInvalid() && computeDeclContext(SS, true) && |
| 1038 | "exiting declarator scope we never really entered" ); |
| 1039 | ExitDeclaratorContext(S); |
| 1040 | } |
| 1041 | |