1 //===- NestedNameSpecifier.cpp - C++ nested name 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 defines the NestedNameSpecifier class, which represents 10 // a C++ nested-name-specifier. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/NestedNameSpecifier.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/Decl.h" 17 #include "clang/AST/DeclCXX.h" 18 #include "clang/AST/DeclTemplate.h" 19 #include "clang/AST/DependenceFlags.h" 20 #include "clang/AST/PrettyPrinter.h" 21 #include "clang/AST/TemplateName.h" 22 #include "clang/AST/Type.h" 23 #include "clang/AST/TypeLoc.h" 24 #include "clang/Basic/LLVM.h" 25 #include "clang/Basic/LangOptions.h" 26 #include "clang/Basic/SourceLocation.h" 27 #include "llvm/ADT/FoldingSet.h" 28 #include "llvm/ADT/SmallVector.h" 29 #include "llvm/Support/Casting.h" 30 #include "llvm/Support/Compiler.h" 31 #include "llvm/Support/ErrorHandling.h" 32 #include "llvm/Support/raw_ostream.h" 33 #include <algorithm> 34 #include <cassert> 35 #include <cstdlib> 36 #include <cstring> 37 38 using namespace clang; 39 40 NestedNameSpecifier * 41 NestedNameSpecifier::FindOrInsert(const ASTContext &Context, 42 const NestedNameSpecifier &Mockup) { 43 llvm::FoldingSetNodeID ID; 44 Mockup.Profile(ID); 45 46 void *InsertPos = nullptr; 47 NestedNameSpecifier *NNS 48 = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos); 49 if (!NNS) { 50 NNS = 51 new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier(Mockup); 52 Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos); 53 } 54 55 return NNS; 56 } 57 58 NestedNameSpecifier * 59 NestedNameSpecifier::Create(const ASTContext &Context, 60 NestedNameSpecifier *Prefix, IdentifierInfo *II) { 61 assert(II && "Identifier cannot be NULL"); 62 assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent"); 63 64 NestedNameSpecifier Mockup; 65 Mockup.Prefix.setPointer(Prefix); 66 Mockup.Prefix.setInt(StoredIdentifier); 67 Mockup.Specifier = II; 68 return FindOrInsert(Context, Mockup); 69 } 70 71 NestedNameSpecifier * 72 NestedNameSpecifier::Create(const ASTContext &Context, 73 NestedNameSpecifier *Prefix, 74 const NamespaceDecl *NS) { 75 assert(NS && "Namespace cannot be NULL"); 76 assert((!Prefix || 77 (Prefix->getAsType() == nullptr && 78 Prefix->getAsIdentifier() == nullptr)) && 79 "Broken nested name specifier"); 80 NestedNameSpecifier Mockup; 81 Mockup.Prefix.setPointer(Prefix); 82 Mockup.Prefix.setInt(StoredDecl); 83 Mockup.Specifier = const_cast<NamespaceDecl *>(NS); 84 return FindOrInsert(Context, Mockup); 85 } 86 87 NestedNameSpecifier * 88 NestedNameSpecifier::Create(const ASTContext &Context, 89 NestedNameSpecifier *Prefix, 90 NamespaceAliasDecl *Alias) { 91 assert(Alias && "Namespace alias cannot be NULL"); 92 assert((!Prefix || 93 (Prefix->getAsType() == nullptr && 94 Prefix->getAsIdentifier() == nullptr)) && 95 "Broken nested name specifier"); 96 NestedNameSpecifier Mockup; 97 Mockup.Prefix.setPointer(Prefix); 98 Mockup.Prefix.setInt(StoredDecl); 99 Mockup.Specifier = Alias; 100 return FindOrInsert(Context, Mockup); 101 } 102 103 NestedNameSpecifier * 104 NestedNameSpecifier::Create(const ASTContext &Context, 105 NestedNameSpecifier *Prefix, 106 bool Template, const Type *T) { 107 assert(T && "Type cannot be NULL"); 108 NestedNameSpecifier Mockup; 109 Mockup.Prefix.setPointer(Prefix); 110 Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec); 111 Mockup.Specifier = const_cast<Type*>(T); 112 return FindOrInsert(Context, Mockup); 113 } 114 115 NestedNameSpecifier * 116 NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) { 117 assert(II && "Identifier cannot be NULL"); 118 NestedNameSpecifier Mockup; 119 Mockup.Prefix.setPointer(nullptr); 120 Mockup.Prefix.setInt(StoredIdentifier); 121 Mockup.Specifier = II; 122 return FindOrInsert(Context, Mockup); 123 } 124 125 NestedNameSpecifier * 126 NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) { 127 if (!Context.GlobalNestedNameSpecifier) 128 Context.GlobalNestedNameSpecifier = 129 new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier(); 130 return Context.GlobalNestedNameSpecifier; 131 } 132 133 NestedNameSpecifier * 134 NestedNameSpecifier::SuperSpecifier(const ASTContext &Context, 135 CXXRecordDecl *RD) { 136 NestedNameSpecifier Mockup; 137 Mockup.Prefix.setPointer(nullptr); 138 Mockup.Prefix.setInt(StoredDecl); 139 Mockup.Specifier = RD; 140 return FindOrInsert(Context, Mockup); 141 } 142 143 NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const { 144 if (!Specifier) 145 return Global; 146 147 switch (Prefix.getInt()) { 148 case StoredIdentifier: 149 return Identifier; 150 151 case StoredDecl: { 152 NamedDecl *ND = static_cast<NamedDecl *>(Specifier); 153 if (isa<CXXRecordDecl>(ND)) 154 return Super; 155 return isa<NamespaceDecl>(ND) ? Namespace : NamespaceAlias; 156 } 157 158 case StoredTypeSpec: 159 return TypeSpec; 160 161 case StoredTypeSpecWithTemplate: 162 return TypeSpecWithTemplate; 163 } 164 165 llvm_unreachable("Invalid NNS Kind!"); 166 } 167 168 /// Retrieve the namespace stored in this nested name specifier. 169 NamespaceDecl *NestedNameSpecifier::getAsNamespace() const { 170 if (Prefix.getInt() == StoredDecl) 171 return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier)); 172 173 return nullptr; 174 } 175 176 /// Retrieve the namespace alias stored in this nested name specifier. 177 NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const { 178 if (Prefix.getInt() == StoredDecl) 179 return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier)); 180 181 return nullptr; 182 } 183 184 /// Retrieve the record declaration stored in this nested name specifier. 185 CXXRecordDecl *NestedNameSpecifier::getAsRecordDecl() const { 186 switch (Prefix.getInt()) { 187 case StoredIdentifier: 188 return nullptr; 189 190 case StoredDecl: 191 return dyn_cast<CXXRecordDecl>(static_cast<NamedDecl *>(Specifier)); 192 193 case StoredTypeSpec: 194 case StoredTypeSpecWithTemplate: 195 return getAsType()->getAsCXXRecordDecl(); 196 } 197 198 llvm_unreachable("Invalid NNS Kind!"); 199 } 200 201 NestedNameSpecifierDependence NestedNameSpecifier::getDependence() const { 202 switch (getKind()) { 203 case Identifier: { 204 // Identifier specifiers always represent dependent types 205 auto F = NestedNameSpecifierDependence::Dependent | 206 NestedNameSpecifierDependence::Instantiation; 207 // Prefix can contain unexpanded template parameters. 208 if (getPrefix()) 209 return F | getPrefix()->getDependence(); 210 return F; 211 } 212 213 case Namespace: 214 case NamespaceAlias: 215 case Global: 216 return NestedNameSpecifierDependence::None; 217 218 case Super: { 219 CXXRecordDecl *RD = static_cast<CXXRecordDecl *>(Specifier); 220 for (const auto &Base : RD->bases()) 221 if (Base.getType()->isDependentType()) 222 // FIXME: must also be instantiation-dependent. 223 return NestedNameSpecifierDependence::Dependent; 224 return NestedNameSpecifierDependence::None; 225 } 226 227 case TypeSpec: 228 case TypeSpecWithTemplate: 229 return toNestedNameSpecifierDependendence(getAsType()->getDependence()); 230 } 231 llvm_unreachable("Invalid NNS Kind!"); 232 } 233 234 bool NestedNameSpecifier::isDependent() const { 235 return getDependence() & NestedNameSpecifierDependence::Dependent; 236 } 237 238 bool NestedNameSpecifier::isInstantiationDependent() const { 239 return getDependence() & NestedNameSpecifierDependence::Instantiation; 240 } 241 242 bool NestedNameSpecifier::containsUnexpandedParameterPack() const { 243 return getDependence() & NestedNameSpecifierDependence::UnexpandedPack; 244 } 245 246 /// Print this nested name specifier to the given output 247 /// stream. 248 void NestedNameSpecifier::print(raw_ostream &OS, const PrintingPolicy &Policy, 249 bool ResolveTemplateArguments) const { 250 if (getPrefix()) 251 getPrefix()->print(OS, Policy); 252 253 switch (getKind()) { 254 case Identifier: 255 OS << getAsIdentifier()->getName(); 256 break; 257 258 case Namespace: 259 if (getAsNamespace()->isAnonymousNamespace()) 260 return; 261 262 OS << getAsNamespace()->getName(); 263 break; 264 265 case NamespaceAlias: 266 OS << getAsNamespaceAlias()->getName(); 267 break; 268 269 case Global: 270 break; 271 272 case Super: 273 OS << "__super"; 274 break; 275 276 case TypeSpecWithTemplate: 277 OS << "template "; 278 // Fall through to print the type. 279 LLVM_FALLTHROUGH; 280 281 case TypeSpec: { 282 const auto *Record = 283 dyn_cast_or_null<ClassTemplateSpecializationDecl>(getAsRecordDecl()); 284 if (ResolveTemplateArguments && Record) { 285 // Print the type trait with resolved template parameters. 286 Record->printName(OS); 287 printTemplateArgumentList(OS, Record->getTemplateArgs().asArray(), 288 Policy); 289 break; 290 } 291 const Type *T = getAsType(); 292 293 PrintingPolicy InnerPolicy(Policy); 294 InnerPolicy.SuppressScope = true; 295 296 // Nested-name-specifiers are intended to contain minimally-qualified 297 // types. An actual ElaboratedType will not occur, since we'll store 298 // just the type that is referred to in the nested-name-specifier (e.g., 299 // a TypedefType, TagType, etc.). However, when we are dealing with 300 // dependent template-id types (e.g., Outer<T>::template Inner<U>), 301 // the type requires its own nested-name-specifier for uniqueness, so we 302 // suppress that nested-name-specifier during printing. 303 assert(!isa<ElaboratedType>(T) && 304 "Elaborated type in nested-name-specifier"); 305 if (const TemplateSpecializationType *SpecType 306 = dyn_cast<TemplateSpecializationType>(T)) { 307 // Print the template name without its corresponding 308 // nested-name-specifier. 309 SpecType->getTemplateName().print(OS, InnerPolicy, true); 310 311 // Print the template argument list. 312 printTemplateArgumentList(OS, SpecType->template_arguments(), 313 InnerPolicy); 314 } else if (const auto *DepSpecType = 315 dyn_cast<DependentTemplateSpecializationType>(T)) { 316 // Print the template name without its corresponding 317 // nested-name-specifier. 318 OS << DepSpecType->getIdentifier()->getName(); 319 // Print the template argument list. 320 printTemplateArgumentList(OS, DepSpecType->template_arguments(), 321 InnerPolicy); 322 } else { 323 // Print the type normally 324 QualType(T, 0).print(OS, InnerPolicy); 325 } 326 break; 327 } 328 } 329 330 OS << "::"; 331 } 332 333 LLVM_DUMP_METHOD void NestedNameSpecifier::dump(const LangOptions &LO) const { 334 dump(llvm::errs(), LO); 335 } 336 337 LLVM_DUMP_METHOD void NestedNameSpecifier::dump() const { dump(llvm::errs()); } 338 339 LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream &OS) const { 340 LangOptions LO; 341 dump(OS, LO); 342 } 343 344 LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream &OS, 345 const LangOptions &LO) const { 346 print(OS, PrintingPolicy(LO)); 347 } 348 349 unsigned 350 NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) { 351 assert(Qualifier && "Expected a non-NULL qualifier"); 352 353 // Location of the trailing '::'. 354 unsigned Length = sizeof(unsigned); 355 356 switch (Qualifier->getKind()) { 357 case NestedNameSpecifier::Global: 358 // Nothing more to add. 359 break; 360 361 case NestedNameSpecifier::Identifier: 362 case NestedNameSpecifier::Namespace: 363 case NestedNameSpecifier::NamespaceAlias: 364 case NestedNameSpecifier::Super: 365 // The location of the identifier or namespace name. 366 Length += sizeof(unsigned); 367 break; 368 369 case NestedNameSpecifier::TypeSpecWithTemplate: 370 case NestedNameSpecifier::TypeSpec: 371 // The "void*" that points at the TypeLoc data. 372 // Note: the 'template' keyword is part of the TypeLoc. 373 Length += sizeof(void *); 374 break; 375 } 376 377 return Length; 378 } 379 380 unsigned 381 NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) { 382 unsigned Length = 0; 383 for (; Qualifier; Qualifier = Qualifier->getPrefix()) 384 Length += getLocalDataLength(Qualifier); 385 return Length; 386 } 387 388 /// Load a (possibly unaligned) source location from a given address 389 /// and offset. 390 static SourceLocation LoadSourceLocation(void *Data, unsigned Offset) { 391 unsigned Raw; 392 memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned)); 393 return SourceLocation::getFromRawEncoding(Raw); 394 } 395 396 /// Load a (possibly unaligned) pointer from a given address and 397 /// offset. 398 static void *LoadPointer(void *Data, unsigned Offset) { 399 void *Result; 400 memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*)); 401 return Result; 402 } 403 404 SourceRange NestedNameSpecifierLoc::getSourceRange() const { 405 if (!Qualifier) 406 return SourceRange(); 407 408 NestedNameSpecifierLoc First = *this; 409 while (NestedNameSpecifierLoc Prefix = First.getPrefix()) 410 First = Prefix; 411 412 return SourceRange(First.getLocalSourceRange().getBegin(), 413 getLocalSourceRange().getEnd()); 414 } 415 416 SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const { 417 if (!Qualifier) 418 return SourceRange(); 419 420 unsigned Offset = getDataLength(Qualifier->getPrefix()); 421 switch (Qualifier->getKind()) { 422 case NestedNameSpecifier::Global: 423 return LoadSourceLocation(Data, Offset); 424 425 case NestedNameSpecifier::Identifier: 426 case NestedNameSpecifier::Namespace: 427 case NestedNameSpecifier::NamespaceAlias: 428 case NestedNameSpecifier::Super: 429 return SourceRange(LoadSourceLocation(Data, Offset), 430 LoadSourceLocation(Data, Offset + sizeof(unsigned))); 431 432 case NestedNameSpecifier::TypeSpecWithTemplate: 433 case NestedNameSpecifier::TypeSpec: { 434 // The "void*" that points at the TypeLoc data. 435 // Note: the 'template' keyword is part of the TypeLoc. 436 void *TypeData = LoadPointer(Data, Offset); 437 TypeLoc TL(Qualifier->getAsType(), TypeData); 438 return SourceRange(TL.getBeginLoc(), 439 LoadSourceLocation(Data, Offset + sizeof(void*))); 440 } 441 } 442 443 llvm_unreachable("Invalid NNS Kind!"); 444 } 445 446 TypeLoc NestedNameSpecifierLoc::getTypeLoc() const { 447 if (Qualifier->getKind() != NestedNameSpecifier::TypeSpec && 448 Qualifier->getKind() != NestedNameSpecifier::TypeSpecWithTemplate) 449 return TypeLoc(); 450 451 // The "void*" that points at the TypeLoc data. 452 unsigned Offset = getDataLength(Qualifier->getPrefix()); 453 void *TypeData = LoadPointer(Data, Offset); 454 return TypeLoc(Qualifier->getAsType(), TypeData); 455 } 456 457 static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize, 458 unsigned &BufferCapacity) { 459 if (Start == End) 460 return; 461 462 if (BufferSize + (End - Start) > BufferCapacity) { 463 // Reallocate the buffer. 464 unsigned NewCapacity = std::max( 465 (unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2), 466 (unsigned)(BufferSize + (End - Start))); 467 char *NewBuffer = static_cast<char *>(llvm::safe_malloc(NewCapacity)); 468 if (Buffer) { 469 memcpy(NewBuffer, Buffer, BufferSize); 470 if (BufferCapacity) 471 free(Buffer); 472 } 473 Buffer = NewBuffer; 474 BufferCapacity = NewCapacity; 475 } 476 assert(Buffer && Start && End && End > Start && "Illegal memory buffer copy"); 477 memcpy(Buffer + BufferSize, Start, End - Start); 478 BufferSize += End - Start; 479 } 480 481 /// Save a source location to the given buffer. 482 static void SaveSourceLocation(SourceLocation Loc, char *&Buffer, 483 unsigned &BufferSize, unsigned &BufferCapacity) { 484 unsigned Raw = Loc.getRawEncoding(); 485 Append(reinterpret_cast<char *>(&Raw), 486 reinterpret_cast<char *>(&Raw) + sizeof(unsigned), 487 Buffer, BufferSize, BufferCapacity); 488 } 489 490 /// Save a pointer to the given buffer. 491 static void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize, 492 unsigned &BufferCapacity) { 493 Append(reinterpret_cast<char *>(&Ptr), 494 reinterpret_cast<char *>(&Ptr) + sizeof(void *), 495 Buffer, BufferSize, BufferCapacity); 496 } 497 498 NestedNameSpecifierLocBuilder:: 499 NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other) 500 : Representation(Other.Representation) { 501 if (!Other.Buffer) 502 return; 503 504 if (Other.BufferCapacity == 0) { 505 // Shallow copy is okay. 506 Buffer = Other.Buffer; 507 BufferSize = Other.BufferSize; 508 return; 509 } 510 511 // Deep copy 512 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize, 513 BufferCapacity); 514 } 515 516 NestedNameSpecifierLocBuilder & 517 NestedNameSpecifierLocBuilder:: 518 operator=(const NestedNameSpecifierLocBuilder &Other) { 519 Representation = Other.Representation; 520 521 if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) { 522 // Re-use our storage. 523 BufferSize = Other.BufferSize; 524 memcpy(Buffer, Other.Buffer, BufferSize); 525 return *this; 526 } 527 528 // Free our storage, if we have any. 529 if (BufferCapacity) { 530 free(Buffer); 531 BufferCapacity = 0; 532 } 533 534 if (!Other.Buffer) { 535 // Empty. 536 Buffer = nullptr; 537 BufferSize = 0; 538 return *this; 539 } 540 541 if (Other.BufferCapacity == 0) { 542 // Shallow copy is okay. 543 Buffer = Other.Buffer; 544 BufferSize = Other.BufferSize; 545 return *this; 546 } 547 548 // Deep copy. 549 BufferSize = 0; 550 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize, 551 BufferCapacity); 552 return *this; 553 } 554 555 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context, 556 SourceLocation TemplateKWLoc, 557 TypeLoc TL, 558 SourceLocation ColonColonLoc) { 559 Representation = NestedNameSpecifier::Create(Context, Representation, 560 TemplateKWLoc.isValid(), 561 TL.getTypePtr()); 562 563 // Push source-location info into the buffer. 564 SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity); 565 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); 566 } 567 568 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context, 569 IdentifierInfo *Identifier, 570 SourceLocation IdentifierLoc, 571 SourceLocation ColonColonLoc) { 572 Representation = NestedNameSpecifier::Create(Context, Representation, 573 Identifier); 574 575 // Push source-location info into the buffer. 576 SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity); 577 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); 578 } 579 580 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context, 581 NamespaceDecl *Namespace, 582 SourceLocation NamespaceLoc, 583 SourceLocation ColonColonLoc) { 584 Representation = NestedNameSpecifier::Create(Context, Representation, 585 Namespace); 586 587 // Push source-location info into the buffer. 588 SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity); 589 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); 590 } 591 592 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context, 593 NamespaceAliasDecl *Alias, 594 SourceLocation AliasLoc, 595 SourceLocation ColonColonLoc) { 596 Representation = NestedNameSpecifier::Create(Context, Representation, Alias); 597 598 // Push source-location info into the buffer. 599 SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity); 600 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); 601 } 602 603 void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context, 604 SourceLocation ColonColonLoc) { 605 assert(!Representation && "Already have a nested-name-specifier!?"); 606 Representation = NestedNameSpecifier::GlobalSpecifier(Context); 607 608 // Push source-location info into the buffer. 609 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); 610 } 611 612 void NestedNameSpecifierLocBuilder::MakeSuper(ASTContext &Context, 613 CXXRecordDecl *RD, 614 SourceLocation SuperLoc, 615 SourceLocation ColonColonLoc) { 616 Representation = NestedNameSpecifier::SuperSpecifier(Context, RD); 617 618 // Push source-location info into the buffer. 619 SaveSourceLocation(SuperLoc, Buffer, BufferSize, BufferCapacity); 620 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); 621 } 622 623 void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context, 624 NestedNameSpecifier *Qualifier, 625 SourceRange R) { 626 Representation = Qualifier; 627 628 // Construct bogus (but well-formed) source information for the 629 // nested-name-specifier. 630 BufferSize = 0; 631 SmallVector<NestedNameSpecifier *, 4> Stack; 632 for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix()) 633 Stack.push_back(NNS); 634 while (!Stack.empty()) { 635 NestedNameSpecifier *NNS = Stack.pop_back_val(); 636 switch (NNS->getKind()) { 637 case NestedNameSpecifier::Identifier: 638 case NestedNameSpecifier::Namespace: 639 case NestedNameSpecifier::NamespaceAlias: 640 SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity); 641 break; 642 643 case NestedNameSpecifier::TypeSpec: 644 case NestedNameSpecifier::TypeSpecWithTemplate: { 645 TypeSourceInfo *TSInfo 646 = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0), 647 R.getBegin()); 648 SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize, 649 BufferCapacity); 650 break; 651 } 652 653 case NestedNameSpecifier::Global: 654 case NestedNameSpecifier::Super: 655 break; 656 } 657 658 // Save the location of the '::'. 659 SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(), 660 Buffer, BufferSize, BufferCapacity); 661 } 662 } 663 664 void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) { 665 if (BufferCapacity) 666 free(Buffer); 667 668 if (!Other) { 669 Representation = nullptr; 670 BufferSize = 0; 671 return; 672 } 673 674 // Rather than copying the data (which is wasteful), "adopt" the 675 // pointer (which points into the ASTContext) but set the capacity to zero to 676 // indicate that we don't own it. 677 Representation = Other.getNestedNameSpecifier(); 678 Buffer = static_cast<char *>(Other.getOpaqueData()); 679 BufferSize = Other.getDataLength(); 680 BufferCapacity = 0; 681 } 682 683 NestedNameSpecifierLoc 684 NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const { 685 if (!Representation) 686 return NestedNameSpecifierLoc(); 687 688 // If we adopted our data pointer from elsewhere in the AST context, there's 689 // no need to copy the memory. 690 if (BufferCapacity == 0) 691 return NestedNameSpecifierLoc(Representation, Buffer); 692 693 // FIXME: After copying the source-location information, should we free 694 // our (temporary) buffer and adopt the ASTContext-allocated memory? 695 // Doing so would optimize repeated calls to getWithLocInContext(). 696 void *Mem = Context.Allocate(BufferSize, alignof(void *)); 697 memcpy(Mem, Buffer, BufferSize); 698 return NestedNameSpecifierLoc(Representation, Mem); 699 } 700