NestedNameSpecifier.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. //===--- NestedNameSpecifier.cpp - C++ nested name specifiers -----*- C++ -*-=//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the NestedNameSpecifier class, which represents
  11. // a C++ nested-name-specifier.
  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/PrettyPrinter.h"
  19. #include "clang/AST/Type.h"
  20. #include "clang/AST/TypeLoc.h"
  21. #include "clang/Basic/Diagnostic.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include <cassert>
  24. using namespace clang;
  25. NestedNameSpecifier *
  26. NestedNameSpecifier::FindOrInsert(const ASTContext &Context,
  27. const NestedNameSpecifier &Mockup) {
  28. llvm::FoldingSetNodeID ID;
  29. Mockup.Profile(ID);
  30. void *InsertPos = 0;
  31. NestedNameSpecifier *NNS
  32. = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
  33. if (!NNS) {
  34. NNS = new (Context, 4) NestedNameSpecifier(Mockup);
  35. Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
  36. }
  37. return NNS;
  38. }
  39. NestedNameSpecifier *
  40. NestedNameSpecifier::Create(const ASTContext &Context,
  41. NestedNameSpecifier *Prefix, IdentifierInfo *II) {
  42. assert(II && "Identifier cannot be NULL");
  43. assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent");
  44. NestedNameSpecifier Mockup;
  45. Mockup.Prefix.setPointer(Prefix);
  46. Mockup.Prefix.setInt(StoredIdentifier);
  47. Mockup.Specifier = II;
  48. return FindOrInsert(Context, Mockup);
  49. }
  50. NestedNameSpecifier *
  51. NestedNameSpecifier::Create(const ASTContext &Context,
  52. NestedNameSpecifier *Prefix, NamespaceDecl *NS) {
  53. assert(NS && "Namespace cannot be NULL");
  54. assert((!Prefix ||
  55. (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) &&
  56. "Broken nested name specifier");
  57. NestedNameSpecifier Mockup;
  58. Mockup.Prefix.setPointer(Prefix);
  59. Mockup.Prefix.setInt(StoredNamespaceOrAlias);
  60. Mockup.Specifier = NS;
  61. return FindOrInsert(Context, Mockup);
  62. }
  63. NestedNameSpecifier *
  64. NestedNameSpecifier::Create(const ASTContext &Context,
  65. NestedNameSpecifier *Prefix,
  66. NamespaceAliasDecl *Alias) {
  67. assert(Alias && "Namespace alias cannot be NULL");
  68. assert((!Prefix ||
  69. (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) &&
  70. "Broken nested name specifier");
  71. NestedNameSpecifier Mockup;
  72. Mockup.Prefix.setPointer(Prefix);
  73. Mockup.Prefix.setInt(StoredNamespaceOrAlias);
  74. Mockup.Specifier = Alias;
  75. return FindOrInsert(Context, Mockup);
  76. }
  77. NestedNameSpecifier *
  78. NestedNameSpecifier::Create(const ASTContext &Context,
  79. NestedNameSpecifier *Prefix,
  80. bool Template, const Type *T) {
  81. assert(T && "Type cannot be NULL");
  82. NestedNameSpecifier Mockup;
  83. Mockup.Prefix.setPointer(Prefix);
  84. Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec);
  85. Mockup.Specifier = const_cast<Type*>(T);
  86. return FindOrInsert(Context, Mockup);
  87. }
  88. NestedNameSpecifier *
  89. NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) {
  90. assert(II && "Identifier cannot be NULL");
  91. NestedNameSpecifier Mockup;
  92. Mockup.Prefix.setPointer(0);
  93. Mockup.Prefix.setInt(StoredIdentifier);
  94. Mockup.Specifier = II;
  95. return FindOrInsert(Context, Mockup);
  96. }
  97. NestedNameSpecifier *
  98. NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
  99. if (!Context.GlobalNestedNameSpecifier)
  100. Context.GlobalNestedNameSpecifier = new (Context, 4) NestedNameSpecifier();
  101. return Context.GlobalNestedNameSpecifier;
  102. }
  103. NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const {
  104. if (Specifier == 0)
  105. return Global;
  106. switch (Prefix.getInt()) {
  107. case StoredIdentifier:
  108. return Identifier;
  109. case StoredNamespaceOrAlias:
  110. return isa<NamespaceDecl>(static_cast<NamedDecl *>(Specifier))? Namespace
  111. : NamespaceAlias;
  112. case StoredTypeSpec:
  113. return TypeSpec;
  114. case StoredTypeSpecWithTemplate:
  115. return TypeSpecWithTemplate;
  116. }
  117. llvm_unreachable("Invalid NNS Kind!");
  118. }
  119. /// \brief Retrieve the namespace stored in this nested name
  120. /// specifier.
  121. NamespaceDecl *NestedNameSpecifier::getAsNamespace() const {
  122. if (Prefix.getInt() == StoredNamespaceOrAlias)
  123. return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier));
  124. return 0;
  125. }
  126. /// \brief Retrieve the namespace alias stored in this nested name
  127. /// specifier.
  128. NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const {
  129. if (Prefix.getInt() == StoredNamespaceOrAlias)
  130. return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier));
  131. return 0;
  132. }
  133. /// \brief Whether this nested name specifier refers to a dependent
  134. /// type or not.
  135. bool NestedNameSpecifier::isDependent() const {
  136. switch (getKind()) {
  137. case Identifier:
  138. // Identifier specifiers always represent dependent types
  139. return true;
  140. case Namespace:
  141. case NamespaceAlias:
  142. case Global:
  143. return false;
  144. case TypeSpec:
  145. case TypeSpecWithTemplate:
  146. return getAsType()->isDependentType();
  147. }
  148. llvm_unreachable("Invalid NNS Kind!");
  149. }
  150. /// \brief Whether this nested name specifier refers to a dependent
  151. /// type or not.
  152. bool NestedNameSpecifier::isInstantiationDependent() const {
  153. switch (getKind()) {
  154. case Identifier:
  155. // Identifier specifiers always represent dependent types
  156. return true;
  157. case Namespace:
  158. case NamespaceAlias:
  159. case Global:
  160. return false;
  161. case TypeSpec:
  162. case TypeSpecWithTemplate:
  163. return getAsType()->isInstantiationDependentType();
  164. }
  165. llvm_unreachable("Invalid NNS Kind!");
  166. }
  167. bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
  168. switch (getKind()) {
  169. case Identifier:
  170. return getPrefix() && getPrefix()->containsUnexpandedParameterPack();
  171. case Namespace:
  172. case NamespaceAlias:
  173. case Global:
  174. return false;
  175. case TypeSpec:
  176. case TypeSpecWithTemplate:
  177. return getAsType()->containsUnexpandedParameterPack();
  178. }
  179. llvm_unreachable("Invalid NNS Kind!");
  180. }
  181. /// \brief Print this nested name specifier to the given output
  182. /// stream.
  183. void
  184. NestedNameSpecifier::print(raw_ostream &OS,
  185. const PrintingPolicy &Policy) const {
  186. if (getPrefix())
  187. getPrefix()->print(OS, Policy);
  188. switch (getKind()) {
  189. case Identifier:
  190. OS << getAsIdentifier()->getName();
  191. break;
  192. case Namespace:
  193. if (getAsNamespace()->isAnonymousNamespace())
  194. return;
  195. OS << getAsNamespace()->getName();
  196. break;
  197. case NamespaceAlias:
  198. OS << getAsNamespaceAlias()->getName();
  199. break;
  200. case Global:
  201. break;
  202. case TypeSpecWithTemplate:
  203. OS << "template ";
  204. // Fall through to print the type.
  205. case TypeSpec: {
  206. std::string TypeStr;
  207. const Type *T = getAsType();
  208. PrintingPolicy InnerPolicy(Policy);
  209. InnerPolicy.SuppressScope = true;
  210. // Nested-name-specifiers are intended to contain minimally-qualified
  211. // types. An actual ElaboratedType will not occur, since we'll store
  212. // just the type that is referred to in the nested-name-specifier (e.g.,
  213. // a TypedefType, TagType, etc.). However, when we are dealing with
  214. // dependent template-id types (e.g., Outer<T>::template Inner<U>),
  215. // the type requires its own nested-name-specifier for uniqueness, so we
  216. // suppress that nested-name-specifier during printing.
  217. assert(!isa<ElaboratedType>(T) &&
  218. "Elaborated type in nested-name-specifier");
  219. if (const TemplateSpecializationType *SpecType
  220. = dyn_cast<TemplateSpecializationType>(T)) {
  221. // Print the template name without its corresponding
  222. // nested-name-specifier.
  223. SpecType->getTemplateName().print(OS, InnerPolicy, true);
  224. // Print the template argument list.
  225. TypeStr = TemplateSpecializationType::PrintTemplateArgumentList(
  226. SpecType->getArgs(),
  227. SpecType->getNumArgs(),
  228. InnerPolicy);
  229. } else {
  230. // Print the type normally
  231. TypeStr = QualType(T, 0).getAsString(InnerPolicy);
  232. }
  233. OS << TypeStr;
  234. break;
  235. }
  236. }
  237. OS << "::";
  238. }
  239. void NestedNameSpecifier::dump(const LangOptions &LO) {
  240. print(llvm::errs(), PrintingPolicy(LO));
  241. }
  242. unsigned
  243. NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
  244. assert(Qualifier && "Expected a non-NULL qualifier");
  245. // Location of the trailing '::'.
  246. unsigned Length = sizeof(unsigned);
  247. switch (Qualifier->getKind()) {
  248. case NestedNameSpecifier::Global:
  249. // Nothing more to add.
  250. break;
  251. case NestedNameSpecifier::Identifier:
  252. case NestedNameSpecifier::Namespace:
  253. case NestedNameSpecifier::NamespaceAlias:
  254. // The location of the identifier or namespace name.
  255. Length += sizeof(unsigned);
  256. break;
  257. case NestedNameSpecifier::TypeSpecWithTemplate:
  258. case NestedNameSpecifier::TypeSpec:
  259. // The "void*" that points at the TypeLoc data.
  260. // Note: the 'template' keyword is part of the TypeLoc.
  261. Length += sizeof(void *);
  262. break;
  263. }
  264. return Length;
  265. }
  266. unsigned
  267. NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
  268. unsigned Length = 0;
  269. for (; Qualifier; Qualifier = Qualifier->getPrefix())
  270. Length += getLocalDataLength(Qualifier);
  271. return Length;
  272. }
  273. namespace {
  274. /// \brief Load a (possibly unaligned) source location from a given address
  275. /// and offset.
  276. SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
  277. unsigned Raw;
  278. memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned));
  279. return SourceLocation::getFromRawEncoding(Raw);
  280. }
  281. /// \brief Load a (possibly unaligned) pointer from a given address and
  282. /// offset.
  283. void *LoadPointer(void *Data, unsigned Offset) {
  284. void *Result;
  285. memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
  286. return Result;
  287. }
  288. }
  289. SourceRange NestedNameSpecifierLoc::getSourceRange() const {
  290. if (!Qualifier)
  291. return SourceRange();
  292. NestedNameSpecifierLoc First = *this;
  293. while (NestedNameSpecifierLoc Prefix = First.getPrefix())
  294. First = Prefix;
  295. return SourceRange(First.getLocalSourceRange().getBegin(),
  296. getLocalSourceRange().getEnd());
  297. }
  298. SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
  299. if (!Qualifier)
  300. return SourceRange();
  301. unsigned Offset = getDataLength(Qualifier->getPrefix());
  302. switch (Qualifier->getKind()) {
  303. case NestedNameSpecifier::Global:
  304. return LoadSourceLocation(Data, Offset);
  305. case NestedNameSpecifier::Identifier:
  306. case NestedNameSpecifier::Namespace:
  307. case NestedNameSpecifier::NamespaceAlias:
  308. return SourceRange(LoadSourceLocation(Data, Offset),
  309. LoadSourceLocation(Data, Offset + sizeof(unsigned)));
  310. case NestedNameSpecifier::TypeSpecWithTemplate:
  311. case NestedNameSpecifier::TypeSpec: {
  312. // The "void*" that points at the TypeLoc data.
  313. // Note: the 'template' keyword is part of the TypeLoc.
  314. void *TypeData = LoadPointer(Data, Offset);
  315. TypeLoc TL(Qualifier->getAsType(), TypeData);
  316. return SourceRange(TL.getBeginLoc(),
  317. LoadSourceLocation(Data, Offset + sizeof(void*)));
  318. }
  319. }
  320. llvm_unreachable("Invalid NNS Kind!");
  321. }
  322. TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
  323. assert((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||
  324. Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) &&
  325. "Nested-name-specifier location is not a type");
  326. // The "void*" that points at the TypeLoc data.
  327. unsigned Offset = getDataLength(Qualifier->getPrefix());
  328. void *TypeData = LoadPointer(Data, Offset);
  329. return TypeLoc(Qualifier->getAsType(), TypeData);
  330. }
  331. namespace {
  332. void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
  333. unsigned &BufferCapacity) {
  334. if (BufferSize + (End - Start) > BufferCapacity) {
  335. // Reallocate the buffer.
  336. unsigned NewCapacity
  337. = std::max((unsigned)(BufferCapacity? BufferCapacity * 2
  338. : sizeof(void*) * 2),
  339. (unsigned)(BufferSize + (End - Start)));
  340. char *NewBuffer = static_cast<char *>(malloc(NewCapacity));
  341. memcpy(NewBuffer, Buffer, BufferSize);
  342. if (BufferCapacity)
  343. free(Buffer);
  344. Buffer = NewBuffer;
  345. BufferCapacity = NewCapacity;
  346. }
  347. memcpy(Buffer + BufferSize, Start, End - Start);
  348. BufferSize += End-Start;
  349. }
  350. /// \brief Save a source location to the given buffer.
  351. void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
  352. unsigned &BufferSize, unsigned &BufferCapacity) {
  353. unsigned Raw = Loc.getRawEncoding();
  354. Append(reinterpret_cast<char *>(&Raw),
  355. reinterpret_cast<char *>(&Raw) + sizeof(unsigned),
  356. Buffer, BufferSize, BufferCapacity);
  357. }
  358. /// \brief Save a pointer to the given buffer.
  359. void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
  360. unsigned &BufferCapacity) {
  361. Append(reinterpret_cast<char *>(&Ptr),
  362. reinterpret_cast<char *>(&Ptr) + sizeof(void *),
  363. Buffer, BufferSize, BufferCapacity);
  364. }
  365. }
  366. NestedNameSpecifierLocBuilder::NestedNameSpecifierLocBuilder()
  367. : Representation(0), Buffer(0), BufferSize(0), BufferCapacity(0) { }
  368. NestedNameSpecifierLocBuilder::
  369. NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
  370. : Representation(Other.Representation), Buffer(0),
  371. BufferSize(0), BufferCapacity(0)
  372. {
  373. if (!Other.Buffer)
  374. return;
  375. if (Other.BufferCapacity == 0) {
  376. // Shallow copy is okay.
  377. Buffer = Other.Buffer;
  378. BufferSize = Other.BufferSize;
  379. return;
  380. }
  381. // Deep copy
  382. BufferSize = Other.BufferSize;
  383. BufferCapacity = Other.BufferSize;
  384. Buffer = static_cast<char *>(malloc(BufferCapacity));
  385. memcpy(Buffer, Other.Buffer, BufferSize);
  386. }
  387. NestedNameSpecifierLocBuilder &
  388. NestedNameSpecifierLocBuilder::
  389. operator=(const NestedNameSpecifierLocBuilder &Other) {
  390. Representation = Other.Representation;
  391. if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) {
  392. // Re-use our storage.
  393. BufferSize = Other.BufferSize;
  394. memcpy(Buffer, Other.Buffer, BufferSize);
  395. return *this;
  396. }
  397. // Free our storage, if we have any.
  398. if (BufferCapacity) {
  399. free(Buffer);
  400. BufferCapacity = 0;
  401. }
  402. if (!Other.Buffer) {
  403. // Empty.
  404. Buffer = 0;
  405. BufferSize = 0;
  406. return *this;
  407. }
  408. if (Other.BufferCapacity == 0) {
  409. // Shallow copy is okay.
  410. Buffer = Other.Buffer;
  411. BufferSize = Other.BufferSize;
  412. return *this;
  413. }
  414. // Deep copy.
  415. BufferSize = Other.BufferSize;
  416. BufferCapacity = BufferSize;
  417. Buffer = static_cast<char *>(malloc(BufferSize));
  418. memcpy(Buffer, Other.Buffer, BufferSize);
  419. return *this;
  420. }
  421. NestedNameSpecifierLocBuilder::~NestedNameSpecifierLocBuilder() {
  422. if (BufferCapacity)
  423. free(Buffer);
  424. }
  425. void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
  426. SourceLocation TemplateKWLoc,
  427. TypeLoc TL,
  428. SourceLocation ColonColonLoc) {
  429. Representation = NestedNameSpecifier::Create(Context, Representation,
  430. TemplateKWLoc.isValid(),
  431. TL.getTypePtr());
  432. // Push source-location info into the buffer.
  433. SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
  434. SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
  435. }
  436. void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
  437. IdentifierInfo *Identifier,
  438. SourceLocation IdentifierLoc,
  439. SourceLocation ColonColonLoc) {
  440. Representation = NestedNameSpecifier::Create(Context, Representation,
  441. Identifier);
  442. // Push source-location info into the buffer.
  443. SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
  444. SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
  445. }
  446. void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
  447. NamespaceDecl *Namespace,
  448. SourceLocation NamespaceLoc,
  449. SourceLocation ColonColonLoc) {
  450. Representation = NestedNameSpecifier::Create(Context, Representation,
  451. Namespace);
  452. // Push source-location info into the buffer.
  453. SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
  454. SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
  455. }
  456. void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
  457. NamespaceAliasDecl *Alias,
  458. SourceLocation AliasLoc,
  459. SourceLocation ColonColonLoc) {
  460. Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
  461. // Push source-location info into the buffer.
  462. SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
  463. SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
  464. }
  465. void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
  466. SourceLocation ColonColonLoc) {
  467. assert(!Representation && "Already have a nested-name-specifier!?");
  468. Representation = NestedNameSpecifier::GlobalSpecifier(Context);
  469. // Push source-location info into the buffer.
  470. SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
  471. }
  472. void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
  473. NestedNameSpecifier *Qualifier,
  474. SourceRange R) {
  475. Representation = Qualifier;
  476. // Construct bogus (but well-formed) source information for the
  477. // nested-name-specifier.
  478. BufferSize = 0;
  479. SmallVector<NestedNameSpecifier *, 4> Stack;
  480. for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix())
  481. Stack.push_back(NNS);
  482. while (!Stack.empty()) {
  483. NestedNameSpecifier *NNS = Stack.back();
  484. Stack.pop_back();
  485. switch (NNS->getKind()) {
  486. case NestedNameSpecifier::Identifier:
  487. case NestedNameSpecifier::Namespace:
  488. case NestedNameSpecifier::NamespaceAlias:
  489. SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
  490. break;
  491. case NestedNameSpecifier::TypeSpec:
  492. case NestedNameSpecifier::TypeSpecWithTemplate: {
  493. TypeSourceInfo *TSInfo
  494. = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
  495. R.getBegin());
  496. SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
  497. BufferCapacity);
  498. break;
  499. }
  500. case NestedNameSpecifier::Global:
  501. break;
  502. }
  503. // Save the location of the '::'.
  504. SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(),
  505. Buffer, BufferSize, BufferCapacity);
  506. }
  507. }
  508. void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
  509. if (BufferCapacity)
  510. free(Buffer);
  511. if (!Other) {
  512. Representation = 0;
  513. BufferSize = 0;
  514. return;
  515. }
  516. // Rather than copying the data (which is wasteful), "adopt" the
  517. // pointer (which points into the ASTContext) but set the capacity to zero to
  518. // indicate that we don't own it.
  519. Representation = Other.getNestedNameSpecifier();
  520. Buffer = static_cast<char *>(Other.getOpaqueData());
  521. BufferSize = Other.getDataLength();
  522. BufferCapacity = 0;
  523. }
  524. NestedNameSpecifierLoc
  525. NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
  526. if (!Representation)
  527. return NestedNameSpecifierLoc();
  528. // If we adopted our data pointer from elsewhere in the AST context, there's
  529. // no need to copy the memory.
  530. if (BufferCapacity == 0)
  531. return NestedNameSpecifierLoc(Representation, Buffer);
  532. // FIXME: After copying the source-location information, should we free
  533. // our (temporary) buffer and adopt the ASTContext-allocated memory?
  534. // Doing so would optimize repeated calls to getWithLocInContext().
  535. void *Mem = Context.Allocate(BufferSize, llvm::alignOf<void *>());
  536. memcpy(Mem, Buffer, BufferSize);
  537. return NestedNameSpecifierLoc(Representation, Mem);
  538. }
  539. const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
  540. NestedNameSpecifier *NNS) {
  541. DB.AddTaggedVal(reinterpret_cast<intptr_t>(NNS),
  542. DiagnosticsEngine::ak_nestednamespec);
  543. return DB;
  544. }