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 if (!BufferCapacity) { 468 char *NewBuffer = static_cast<char *>(llvm::safe_malloc(NewCapacity)); 469 if (Buffer) 470 memcpy(NewBuffer, Buffer, BufferSize); 471 Buffer = NewBuffer; 472 } else { 473 Buffer = static_cast<char *>(llvm::safe_realloc(Buffer, NewCapacity)); 474 } 475 BufferCapacity = NewCapacity; 476 } 477 assert(Buffer && Start && End && End > Start && "Illegal memory buffer copy"); 478 memcpy(Buffer + BufferSize, Start, End - Start); 479 BufferSize += End - Start; 480 } 481 482 /// Save a source location to the given buffer. 483 static void SaveSourceLocation(SourceLocation Loc, char *&Buffer, 484 unsigned &BufferSize, unsigned &BufferCapacity) { 485 unsigned Raw = Loc.getRawEncoding(); 486 Append(reinterpret_cast<char *>(&Raw), 487 reinterpret_cast<char *>(&Raw) + sizeof(unsigned), 488 Buffer, BufferSize, BufferCapacity); 489 } 490 491 /// Save a pointer to the given buffer. 492 static void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize, 493 unsigned &BufferCapacity) { 494 Append(reinterpret_cast<char *>(&Ptr), 495 reinterpret_cast<char *>(&Ptr) + sizeof(void *), 496 Buffer, BufferSize, BufferCapacity); 497 } 498 499 NestedNameSpecifierLocBuilder:: 500 NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other) 501 : Representation(Other.Representation) { 502 if (!Other.Buffer) 503 return; 504 505 if (Other.BufferCapacity == 0) { 506 // Shallow copy is okay. 507 Buffer = Other.Buffer; 508 BufferSize = Other.BufferSize; 509 return; 510 } 511 512 // Deep copy 513 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize, 514 BufferCapacity); 515 } 516 517 NestedNameSpecifierLocBuilder & 518 NestedNameSpecifierLocBuilder:: 519 operator=(const NestedNameSpecifierLocBuilder &Other) { 520 Representation = Other.Representation; 521 522 if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) { 523 // Re-use our storage. 524 BufferSize = Other.BufferSize; 525 memcpy(Buffer, Other.Buffer, BufferSize); 526 return *this; 527 } 528 529 // Free our storage, if we have any. 530 if (BufferCapacity) { 531 free(Buffer); 532 BufferCapacity = 0; 533 } 534 535 if (!Other.Buffer) { 536 // Empty. 537 Buffer = nullptr; 538 BufferSize = 0; 539 return *this; 540 } 541 542 if (Other.BufferCapacity == 0) { 543 // Shallow copy is okay. 544 Buffer = Other.Buffer; 545 BufferSize = Other.BufferSize; 546 return *this; 547 } 548 549 // Deep copy. 550 BufferSize = 0; 551 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize, 552 BufferCapacity); 553 return *this; 554 } 555 556 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context, 557 SourceLocation TemplateKWLoc, 558 TypeLoc TL, 559 SourceLocation ColonColonLoc) { 560 Representation = NestedNameSpecifier::Create(Context, Representation, 561 TemplateKWLoc.isValid(), 562 TL.getTypePtr()); 563 564 // Push source-location info into the buffer. 565 SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity); 566 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); 567 } 568 569 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context, 570 IdentifierInfo *Identifier, 571 SourceLocation IdentifierLoc, 572 SourceLocation ColonColonLoc) { 573 Representation = NestedNameSpecifier::Create(Context, Representation, 574 Identifier); 575 576 // Push source-location info into the buffer. 577 SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity); 578 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); 579 } 580 581 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context, 582 NamespaceDecl *Namespace, 583 SourceLocation NamespaceLoc, 584 SourceLocation ColonColonLoc) { 585 Representation = NestedNameSpecifier::Create(Context, Representation, 586 Namespace); 587 588 // Push source-location info into the buffer. 589 SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity); 590 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); 591 } 592 593 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context, 594 NamespaceAliasDecl *Alias, 595 SourceLocation AliasLoc, 596 SourceLocation ColonColonLoc) { 597 Representation = NestedNameSpecifier::Create(Context, Representation, Alias); 598 599 // Push source-location info into the buffer. 600 SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity); 601 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); 602 } 603 604 void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context, 605 SourceLocation ColonColonLoc) { 606 assert(!Representation && "Already have a nested-name-specifier!?"); 607 Representation = NestedNameSpecifier::GlobalSpecifier(Context); 608 609 // Push source-location info into the buffer. 610 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); 611 } 612 613 void NestedNameSpecifierLocBuilder::MakeSuper(ASTContext &Context, 614 CXXRecordDecl *RD, 615 SourceLocation SuperLoc, 616 SourceLocation ColonColonLoc) { 617 Representation = NestedNameSpecifier::SuperSpecifier(Context, RD); 618 619 // Push source-location info into the buffer. 620 SaveSourceLocation(SuperLoc, Buffer, BufferSize, BufferCapacity); 621 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity); 622 } 623 624 void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context, 625 NestedNameSpecifier *Qualifier, 626 SourceRange R) { 627 Representation = Qualifier; 628 629 // Construct bogus (but well-formed) source information for the 630 // nested-name-specifier. 631 BufferSize = 0; 632 SmallVector<NestedNameSpecifier *, 4> Stack; 633 for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix()) 634 Stack.push_back(NNS); 635 while (!Stack.empty()) { 636 NestedNameSpecifier *NNS = Stack.pop_back_val(); 637 switch (NNS->getKind()) { 638 case NestedNameSpecifier::Identifier: 639 case NestedNameSpecifier::Namespace: 640 case NestedNameSpecifier::NamespaceAlias: 641 SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity); 642 break; 643 644 case NestedNameSpecifier::TypeSpec: 645 case NestedNameSpecifier::TypeSpecWithTemplate: { 646 TypeSourceInfo *TSInfo 647 = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0), 648 R.getBegin()); 649 SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize, 650 BufferCapacity); 651 break; 652 } 653 654 case NestedNameSpecifier::Global: 655 case NestedNameSpecifier::Super: 656 break; 657 } 658 659 // Save the location of the '::'. 660 SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(), 661 Buffer, BufferSize, BufferCapacity); 662 } 663 } 664 665 void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) { 666 if (BufferCapacity) 667 free(Buffer); 668 669 if (!Other) { 670 Representation = nullptr; 671 BufferSize = 0; 672 return; 673 } 674 675 // Rather than copying the data (which is wasteful), "adopt" the 676 // pointer (which points into the ASTContext) but set the capacity to zero to 677 // indicate that we don't own it. 678 Representation = Other.getNestedNameSpecifier(); 679 Buffer = static_cast<char *>(Other.getOpaqueData()); 680 BufferSize = Other.getDataLength(); 681 BufferCapacity = 0; 682 } 683 684 NestedNameSpecifierLoc 685 NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const { 686 if (!Representation) 687 return NestedNameSpecifierLoc(); 688 689 // If we adopted our data pointer from elsewhere in the AST context, there's 690 // no need to copy the memory. 691 if (BufferCapacity == 0) 692 return NestedNameSpecifierLoc(Representation, Buffer); 693 694 // FIXME: After copying the source-location information, should we free 695 // our (temporary) buffer and adopt the ASTContext-allocated memory? 696 // Doing so would optimize repeated calls to getWithLocInContext(). 697 void *Mem = Context.Allocate(BufferSize, alignof(void *)); 698 memcpy(Mem, Buffer, BufferSize); 699 return NestedNameSpecifierLoc(Representation, Mem); 700 } 701