blob: 1f041aa495420d3551d5f6f5ce989f9ffb2e3404 [file] [log] [blame]
Douglas Gregor52537682009-03-19 00:18:19 +00001//===--- 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"
Douglas Gregor7b26ff92011-02-24 02:36:08 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000018#include "clang/AST/PrettyPrinter.h"
Douglas Gregor52537682009-03-19 00:18:19 +000019#include "clang/AST/Type.h"
Douglas Gregor869ad452011-02-24 17:54:50 +000020#include "clang/AST/TypeLoc.h"
Richard Smithbeb386a2012-08-15 01:41:43 +000021#include "llvm/Support/AlignOf.h"
Douglas Gregor18353912009-03-19 03:51:16 +000022#include "llvm/Support/raw_ostream.h"
Douglas Gregorf21eb492009-03-26 23:50:42 +000023#include <cassert>
Douglas Gregor18353912009-03-19 03:51:16 +000024
Douglas Gregor52537682009-03-19 00:18:19 +000025using namespace clang;
26
Douglas Gregorf21eb492009-03-26 23:50:42 +000027NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +000028NestedNameSpecifier::FindOrInsert(const ASTContext &Context,
Douglas Gregorf21eb492009-03-26 23:50:42 +000029 const NestedNameSpecifier &Mockup) {
30 llvm::FoldingSetNodeID ID;
31 Mockup.Profile(ID);
Douglas Gregor52537682009-03-19 00:18:19 +000032
Craig Topper36250ad2014-05-12 05:36:57 +000033 void *InsertPos = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +000034 NestedNameSpecifier *NNS
Douglas Gregorf21eb492009-03-26 23:50:42 +000035 = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
36 if (!NNS) {
Richard Smithbeb386a2012-08-15 01:41:43 +000037 NNS = new (Context, llvm::alignOf<NestedNameSpecifier>())
38 NestedNameSpecifier(Mockup);
Douglas Gregorf21eb492009-03-26 23:50:42 +000039 Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
40 }
Douglas Gregor52537682009-03-19 00:18:19 +000041
Douglas Gregorf21eb492009-03-26 23:50:42 +000042 return NNS;
Douglas Gregor52537682009-03-19 00:18:19 +000043}
Douglas Gregor18353912009-03-19 03:51:16 +000044
Douglas Gregorf21eb492009-03-26 23:50:42 +000045NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +000046NestedNameSpecifier::Create(const ASTContext &Context,
47 NestedNameSpecifier *Prefix, IdentifierInfo *II) {
Douglas Gregorf21eb492009-03-26 23:50:42 +000048 assert(II && "Identifier cannot be NULL");
Douglas Gregor308047d2009-09-09 00:23:06 +000049 assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent");
Douglas Gregor18353912009-03-19 03:51:16 +000050
Douglas Gregorf21eb492009-03-26 23:50:42 +000051 NestedNameSpecifier Mockup;
Douglas Gregordce2b622009-04-01 00:28:59 +000052 Mockup.Prefix.setPointer(Prefix);
Douglas Gregor7b26ff92011-02-24 02:36:08 +000053 Mockup.Prefix.setInt(StoredIdentifier);
Douglas Gregordce2b622009-04-01 00:28:59 +000054 Mockup.Specifier = II;
Douglas Gregorf21eb492009-03-26 23:50:42 +000055 return FindOrInsert(Context, Mockup);
56}
Douglas Gregor18353912009-03-19 03:51:16 +000057
Douglas Gregorf21eb492009-03-26 23:50:42 +000058NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +000059NestedNameSpecifier::Create(const ASTContext &Context,
Dmitri Gribenkoaeeca772013-01-23 17:06:56 +000060 NestedNameSpecifier *Prefix,
61 const NamespaceDecl *NS) {
Douglas Gregorf21eb492009-03-26 23:50:42 +000062 assert(NS && "Namespace cannot be NULL");
Mike Stump11289f42009-09-09 15:08:12 +000063 assert((!Prefix ||
Craig Topper36250ad2014-05-12 05:36:57 +000064 (Prefix->getAsType() == nullptr &&
65 Prefix->getAsIdentifier() == nullptr)) &&
Douglas Gregorf21eb492009-03-26 23:50:42 +000066 "Broken nested name specifier");
67 NestedNameSpecifier Mockup;
Douglas Gregordce2b622009-04-01 00:28:59 +000068 Mockup.Prefix.setPointer(Prefix);
Douglas Gregor7b26ff92011-02-24 02:36:08 +000069 Mockup.Prefix.setInt(StoredNamespaceOrAlias);
Dmitri Gribenkoaeeca772013-01-23 17:06:56 +000070 Mockup.Specifier = const_cast<NamespaceDecl *>(NS);
Douglas Gregorf21eb492009-03-26 23:50:42 +000071 return FindOrInsert(Context, Mockup);
72}
73
74NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +000075NestedNameSpecifier::Create(const ASTContext &Context,
Douglas Gregor7b26ff92011-02-24 02:36:08 +000076 NestedNameSpecifier *Prefix,
77 NamespaceAliasDecl *Alias) {
78 assert(Alias && "Namespace alias cannot be NULL");
79 assert((!Prefix ||
Craig Topper36250ad2014-05-12 05:36:57 +000080 (Prefix->getAsType() == nullptr &&
81 Prefix->getAsIdentifier() == nullptr)) &&
Douglas Gregor7b26ff92011-02-24 02:36:08 +000082 "Broken nested name specifier");
83 NestedNameSpecifier Mockup;
84 Mockup.Prefix.setPointer(Prefix);
85 Mockup.Prefix.setInt(StoredNamespaceOrAlias);
86 Mockup.Specifier = Alias;
87 return FindOrInsert(Context, Mockup);
88}
89
90NestedNameSpecifier *
91NestedNameSpecifier::Create(const ASTContext &Context,
Jay Foad39c79802011-01-12 09:06:06 +000092 NestedNameSpecifier *Prefix,
John McCall424cec92011-01-19 06:33:43 +000093 bool Template, const Type *T) {
Douglas Gregorf21eb492009-03-26 23:50:42 +000094 assert(T && "Type cannot be NULL");
95 NestedNameSpecifier Mockup;
Douglas Gregordce2b622009-04-01 00:28:59 +000096 Mockup.Prefix.setPointer(Prefix);
Douglas Gregor7b26ff92011-02-24 02:36:08 +000097 Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec);
John McCall424cec92011-01-19 06:33:43 +000098 Mockup.Specifier = const_cast<Type*>(T);
Douglas Gregorf21eb492009-03-26 23:50:42 +000099 return FindOrInsert(Context, Mockup);
100}
Douglas Gregor64792e02009-09-02 23:58:38 +0000101
102NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +0000103NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) {
Douglas Gregor64792e02009-09-02 23:58:38 +0000104 assert(II && "Identifier cannot be NULL");
105 NestedNameSpecifier Mockup;
Craig Topper36250ad2014-05-12 05:36:57 +0000106 Mockup.Prefix.setPointer(nullptr);
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000107 Mockup.Prefix.setInt(StoredIdentifier);
Douglas Gregor64792e02009-09-02 23:58:38 +0000108 Mockup.Specifier = II;
109 return FindOrInsert(Context, Mockup);
110}
111
Jay Foad39c79802011-01-12 09:06:06 +0000112NestedNameSpecifier *
113NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
Douglas Gregorf21eb492009-03-26 23:50:42 +0000114 if (!Context.GlobalNestedNameSpecifier)
Richard Smithbeb386a2012-08-15 01:41:43 +0000115 Context.GlobalNestedNameSpecifier =
116 new (Context, llvm::alignOf<NestedNameSpecifier>())
117 NestedNameSpecifier();
Douglas Gregorf21eb492009-03-26 23:50:42 +0000118 return Context.GlobalNestedNameSpecifier;
119}
120
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000121NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const {
Craig Topper36250ad2014-05-12 05:36:57 +0000122 if (!Specifier)
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000123 return Global;
124
125 switch (Prefix.getInt()) {
126 case StoredIdentifier:
127 return Identifier;
128
129 case StoredNamespaceOrAlias:
130 return isa<NamespaceDecl>(static_cast<NamedDecl *>(Specifier))? Namespace
131 : NamespaceAlias;
132
133 case StoredTypeSpec:
134 return TypeSpec;
135
136 case StoredTypeSpecWithTemplate:
137 return TypeSpecWithTemplate;
138 }
139
David Blaikiee4d798f2012-01-20 21:50:17 +0000140 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000141}
142
143/// \brief Retrieve the namespace stored in this nested name
144/// specifier.
145NamespaceDecl *NestedNameSpecifier::getAsNamespace() const {
146 if (Prefix.getInt() == StoredNamespaceOrAlias)
147 return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier));
148
Craig Topper36250ad2014-05-12 05:36:57 +0000149 return nullptr;
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000150}
151
152/// \brief Retrieve the namespace alias stored in this nested name
153/// specifier.
154NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const {
155 if (Prefix.getInt() == StoredNamespaceOrAlias)
156 return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier));
157
Craig Topper36250ad2014-05-12 05:36:57 +0000158 return nullptr;
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000159}
160
161
Douglas Gregorf21eb492009-03-26 23:50:42 +0000162/// \brief Whether this nested name specifier refers to a dependent
163/// type or not.
164bool NestedNameSpecifier::isDependent() const {
165 switch (getKind()) {
166 case Identifier:
167 // Identifier specifiers always represent dependent types
168 return true;
169
170 case Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000171 case NamespaceAlias:
Douglas Gregorf21eb492009-03-26 23:50:42 +0000172 case Global:
173 return false;
174
175 case TypeSpec:
176 case TypeSpecWithTemplate:
177 return getAsType()->isDependentType();
Douglas Gregor18353912009-03-19 03:51:16 +0000178 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000179
David Blaikiee4d798f2012-01-20 21:50:17 +0000180 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregorf21eb492009-03-26 23:50:42 +0000181}
182
Douglas Gregor678d76c2011-07-01 01:22:09 +0000183/// \brief Whether this nested name specifier refers to a dependent
184/// type or not.
185bool NestedNameSpecifier::isInstantiationDependent() const {
186 switch (getKind()) {
187 case Identifier:
188 // Identifier specifiers always represent dependent types
189 return true;
190
191 case Namespace:
192 case NamespaceAlias:
193 case Global:
194 return false;
195
196 case TypeSpec:
197 case TypeSpecWithTemplate:
198 return getAsType()->isInstantiationDependentType();
199 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000200
201 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor678d76c2011-07-01 01:22:09 +0000202}
203
Douglas Gregor506bd562010-12-13 22:49:22 +0000204bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
205 switch (getKind()) {
206 case Identifier:
207 return getPrefix() && getPrefix()->containsUnexpandedParameterPack();
208
209 case Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000210 case NamespaceAlias:
Douglas Gregor506bd562010-12-13 22:49:22 +0000211 case Global:
212 return false;
213
214 case TypeSpec:
215 case TypeSpecWithTemplate:
216 return getAsType()->containsUnexpandedParameterPack();
217 }
218
David Blaikiee4d798f2012-01-20 21:50:17 +0000219 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor506bd562010-12-13 22:49:22 +0000220}
221
Douglas Gregorf21eb492009-03-26 23:50:42 +0000222/// \brief Print this nested name specifier to the given output
223/// stream.
Mike Stump11289f42009-09-09 15:08:12 +0000224void
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000225NestedNameSpecifier::print(raw_ostream &OS,
Douglas Gregor7de59662009-05-29 20:38:28 +0000226 const PrintingPolicy &Policy) const {
Douglas Gregordce2b622009-04-01 00:28:59 +0000227 if (getPrefix())
Douglas Gregor7de59662009-05-29 20:38:28 +0000228 getPrefix()->print(OS, Policy);
Douglas Gregorf21eb492009-03-26 23:50:42 +0000229
230 switch (getKind()) {
231 case Identifier:
232 OS << getAsIdentifier()->getName();
233 break;
234
235 case Namespace:
Douglas Gregor2e10cf92011-11-03 00:16:13 +0000236 if (getAsNamespace()->isAnonymousNamespace())
237 return;
238
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000239 OS << getAsNamespace()->getName();
240 break;
241
242 case NamespaceAlias:
243 OS << getAsNamespaceAlias()->getName();
Douglas Gregorf21eb492009-03-26 23:50:42 +0000244 break;
245
246 case Global:
247 break;
248
249 case TypeSpecWithTemplate:
250 OS << "template ";
251 // Fall through to print the type.
252
253 case TypeSpec: {
John McCall424cec92011-01-19 06:33:43 +0000254 const Type *T = getAsType();
Douglas Gregorf21eb492009-03-26 23:50:42 +0000255
Douglas Gregor7de59662009-05-29 20:38:28 +0000256 PrintingPolicy InnerPolicy(Policy);
John McCallb2e195a2009-09-05 06:31:47 +0000257 InnerPolicy.SuppressScope = true;
Mike Stump11289f42009-09-09 15:08:12 +0000258
Douglas Gregor053f6912009-08-26 00:04:55 +0000259 // Nested-name-specifiers are intended to contain minimally-qualified
Abramo Bagnara6150c882010-05-11 21:36:43 +0000260 // types. An actual ElaboratedType will not occur, since we'll store
Douglas Gregor053f6912009-08-26 00:04:55 +0000261 // just the type that is referred to in the nested-name-specifier (e.g.,
262 // a TypedefType, TagType, etc.). However, when we are dealing with
Mike Stump11289f42009-09-09 15:08:12 +0000263 // dependent template-id types (e.g., Outer<T>::template Inner<U>),
Douglas Gregor053f6912009-08-26 00:04:55 +0000264 // the type requires its own nested-name-specifier for uniqueness, so we
265 // suppress that nested-name-specifier during printing.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000266 assert(!isa<ElaboratedType>(T) &&
267 "Elaborated type in nested-name-specifier");
Douglas Gregor053f6912009-08-26 00:04:55 +0000268 if (const TemplateSpecializationType *SpecType
269 = dyn_cast<TemplateSpecializationType>(T)) {
Mike Stump11289f42009-09-09 15:08:12 +0000270 // Print the template name without its corresponding
Douglas Gregor053f6912009-08-26 00:04:55 +0000271 // nested-name-specifier.
272 SpecType->getTemplateName().print(OS, InnerPolicy, true);
Mike Stump11289f42009-09-09 15:08:12 +0000273
Douglas Gregor053f6912009-08-26 00:04:55 +0000274 // Print the template argument list.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000275 TemplateSpecializationType::PrintTemplateArgumentList(
276 OS, SpecType->getArgs(), SpecType->getNumArgs(), InnerPolicy);
Douglas Gregor053f6912009-08-26 00:04:55 +0000277 } else {
278 // Print the type normally
Benjamin Kramer9170e912013-02-22 15:46:01 +0000279 QualType(T, 0).print(OS, InnerPolicy);
Douglas Gregor053f6912009-08-26 00:04:55 +0000280 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000281 break;
282 }
283 }
284
285 OS << "::";
286}
287
Chris Lattnerc61089a2009-06-30 01:26:17 +0000288void NestedNameSpecifier::dump(const LangOptions &LO) {
289 print(llvm::errs(), PrintingPolicy(LO));
Douglas Gregor333489b2009-03-27 23:10:48 +0000290}
Douglas Gregor869ad452011-02-24 17:54:50 +0000291
292unsigned
293NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
294 assert(Qualifier && "Expected a non-NULL qualifier");
295
296 // Location of the trailing '::'.
297 unsigned Length = sizeof(unsigned);
298
299 switch (Qualifier->getKind()) {
300 case NestedNameSpecifier::Global:
301 // Nothing more to add.
302 break;
303
304 case NestedNameSpecifier::Identifier:
305 case NestedNameSpecifier::Namespace:
306 case NestedNameSpecifier::NamespaceAlias:
307 // The location of the identifier or namespace name.
308 Length += sizeof(unsigned);
309 break;
310
311 case NestedNameSpecifier::TypeSpecWithTemplate:
312 case NestedNameSpecifier::TypeSpec:
313 // The "void*" that points at the TypeLoc data.
314 // Note: the 'template' keyword is part of the TypeLoc.
315 Length += sizeof(void *);
316 break;
317 }
318
319 return Length;
320}
321
322unsigned
323NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
324 unsigned Length = 0;
325 for (; Qualifier; Qualifier = Qualifier->getPrefix())
326 Length += getLocalDataLength(Qualifier);
327 return Length;
328}
329
330namespace {
331 /// \brief Load a (possibly unaligned) source location from a given address
332 /// and offset.
333 SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
334 unsigned Raw;
335 memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned));
336 return SourceLocation::getFromRawEncoding(Raw);
337 }
338
339 /// \brief Load a (possibly unaligned) pointer from a given address and
340 /// offset.
341 void *LoadPointer(void *Data, unsigned Offset) {
342 void *Result;
343 memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
344 return Result;
345 }
346}
347
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000348SourceRange NestedNameSpecifierLoc::getSourceRange() const {
Douglas Gregor12441b32011-02-25 16:33:46 +0000349 if (!Qualifier)
350 return SourceRange();
351
Douglas Gregor869ad452011-02-24 17:54:50 +0000352 NestedNameSpecifierLoc First = *this;
Douglas Gregor12441b32011-02-25 16:33:46 +0000353 while (NestedNameSpecifierLoc Prefix = First.getPrefix())
Douglas Gregor869ad452011-02-24 17:54:50 +0000354 First = Prefix;
355
356 return SourceRange(First.getLocalSourceRange().getBegin(),
357 getLocalSourceRange().getEnd());
358}
359
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000360SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
Douglas Gregor12441b32011-02-25 16:33:46 +0000361 if (!Qualifier)
362 return SourceRange();
363
Douglas Gregor869ad452011-02-24 17:54:50 +0000364 unsigned Offset = getDataLength(Qualifier->getPrefix());
365 switch (Qualifier->getKind()) {
366 case NestedNameSpecifier::Global:
367 return LoadSourceLocation(Data, Offset);
368
369 case NestedNameSpecifier::Identifier:
370 case NestedNameSpecifier::Namespace:
371 case NestedNameSpecifier::NamespaceAlias:
372 return SourceRange(LoadSourceLocation(Data, Offset),
373 LoadSourceLocation(Data, Offset + sizeof(unsigned)));
374
375 case NestedNameSpecifier::TypeSpecWithTemplate:
376 case NestedNameSpecifier::TypeSpec: {
377 // The "void*" that points at the TypeLoc data.
378 // Note: the 'template' keyword is part of the TypeLoc.
379 void *TypeData = LoadPointer(Data, Offset);
380 TypeLoc TL(Qualifier->getAsType(), TypeData);
381 return SourceRange(TL.getBeginLoc(),
382 LoadSourceLocation(Data, Offset + sizeof(void*)));
383 }
384 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000385
386 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor869ad452011-02-24 17:54:50 +0000387}
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000388
389TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
390 assert((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||
391 Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) &&
392 "Nested-name-specifier location is not a type");
393
394 // The "void*" that points at the TypeLoc data.
395 unsigned Offset = getDataLength(Qualifier->getPrefix());
396 void *TypeData = LoadPointer(Data, Offset);
397 return TypeLoc(Qualifier->getAsType(), TypeData);
398}
Douglas Gregor9b272512011-02-28 23:58:31 +0000399
400namespace {
401 void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
402 unsigned &BufferCapacity) {
403 if (BufferSize + (End - Start) > BufferCapacity) {
404 // Reallocate the buffer.
405 unsigned NewCapacity
406 = std::max((unsigned)(BufferCapacity? BufferCapacity * 2
407 : sizeof(void*) * 2),
408 (unsigned)(BufferSize + (End - Start)));
409 char *NewBuffer = static_cast<char *>(malloc(NewCapacity));
410 memcpy(NewBuffer, Buffer, BufferSize);
411
412 if (BufferCapacity)
413 free(Buffer);
414 Buffer = NewBuffer;
415 BufferCapacity = NewCapacity;
416 }
417
418 memcpy(Buffer + BufferSize, Start, End - Start);
419 BufferSize += End-Start;
420 }
421
422 /// \brief Save a source location to the given buffer.
423 void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
424 unsigned &BufferSize, unsigned &BufferCapacity) {
425 unsigned Raw = Loc.getRawEncoding();
426 Append(reinterpret_cast<char *>(&Raw),
427 reinterpret_cast<char *>(&Raw) + sizeof(unsigned),
428 Buffer, BufferSize, BufferCapacity);
429 }
430
431 /// \brief Save a pointer to the given buffer.
432 void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
433 unsigned &BufferCapacity) {
434 Append(reinterpret_cast<char *>(&Ptr),
435 reinterpret_cast<char *>(&Ptr) + sizeof(void *),
436 Buffer, BufferSize, BufferCapacity);
437 }
438}
439
Douglas Gregor9b272512011-02-28 23:58:31 +0000440NestedNameSpecifierLocBuilder::
441NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
Craig Topper36250ad2014-05-12 05:36:57 +0000442 : Representation(Other.Representation), Buffer(nullptr),
Douglas Gregor9b272512011-02-28 23:58:31 +0000443 BufferSize(0), BufferCapacity(0)
444{
445 if (!Other.Buffer)
446 return;
447
448 if (Other.BufferCapacity == 0) {
449 // Shallow copy is okay.
450 Buffer = Other.Buffer;
451 BufferSize = Other.BufferSize;
452 return;
453 }
454
455 // Deep copy
Serge Pavlov9f81d6a2014-07-16 18:18:13 +0000456 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
457 BufferCapacity);
Douglas Gregor9b272512011-02-28 23:58:31 +0000458}
459
460NestedNameSpecifierLocBuilder &
461NestedNameSpecifierLocBuilder::
462operator=(const NestedNameSpecifierLocBuilder &Other) {
463 Representation = Other.Representation;
464
465 if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) {
466 // Re-use our storage.
467 BufferSize = Other.BufferSize;
468 memcpy(Buffer, Other.Buffer, BufferSize);
469 return *this;
470 }
471
472 // Free our storage, if we have any.
473 if (BufferCapacity) {
474 free(Buffer);
475 BufferCapacity = 0;
476 }
477
478 if (!Other.Buffer) {
479 // Empty.
Craig Topper36250ad2014-05-12 05:36:57 +0000480 Buffer = nullptr;
Douglas Gregor9b272512011-02-28 23:58:31 +0000481 BufferSize = 0;
482 return *this;
483 }
484
485 if (Other.BufferCapacity == 0) {
486 // Shallow copy is okay.
487 Buffer = Other.Buffer;
488 BufferSize = Other.BufferSize;
489 return *this;
490 }
491
492 // Deep copy.
Serge Pavlov9f81d6a2014-07-16 18:18:13 +0000493 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
494 BufferCapacity);
Douglas Gregor9b272512011-02-28 23:58:31 +0000495 return *this;
496}
497
Douglas Gregor9b272512011-02-28 23:58:31 +0000498void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
499 SourceLocation TemplateKWLoc,
500 TypeLoc TL,
501 SourceLocation ColonColonLoc) {
502 Representation = NestedNameSpecifier::Create(Context, Representation,
503 TemplateKWLoc.isValid(),
504 TL.getTypePtr());
505
506 // Push source-location info into the buffer.
507 SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
508 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
509}
510
511void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
512 IdentifierInfo *Identifier,
513 SourceLocation IdentifierLoc,
514 SourceLocation ColonColonLoc) {
515 Representation = NestedNameSpecifier::Create(Context, Representation,
516 Identifier);
517
518 // Push source-location info into the buffer.
519 SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
520 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
521}
522
523void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
524 NamespaceDecl *Namespace,
525 SourceLocation NamespaceLoc,
526 SourceLocation ColonColonLoc) {
527 Representation = NestedNameSpecifier::Create(Context, Representation,
528 Namespace);
529
530 // Push source-location info into the buffer.
531 SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
532 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
533}
534
535void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
536 NamespaceAliasDecl *Alias,
537 SourceLocation AliasLoc,
538 SourceLocation ColonColonLoc) {
539 Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
540
541 // Push source-location info into the buffer.
542 SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
543 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
544}
545
546void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
547 SourceLocation ColonColonLoc) {
548 assert(!Representation && "Already have a nested-name-specifier!?");
549 Representation = NestedNameSpecifier::GlobalSpecifier(Context);
550
551 // Push source-location info into the buffer.
552 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
553}
554
555void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
556 NestedNameSpecifier *Qualifier,
557 SourceRange R) {
558 Representation = Qualifier;
559
560 // Construct bogus (but well-formed) source information for the
561 // nested-name-specifier.
562 BufferSize = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000563 SmallVector<NestedNameSpecifier *, 4> Stack;
Douglas Gregor9b272512011-02-28 23:58:31 +0000564 for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix())
565 Stack.push_back(NNS);
566 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000567 NestedNameSpecifier *NNS = Stack.pop_back_val();
Douglas Gregor9b272512011-02-28 23:58:31 +0000568 switch (NNS->getKind()) {
569 case NestedNameSpecifier::Identifier:
570 case NestedNameSpecifier::Namespace:
571 case NestedNameSpecifier::NamespaceAlias:
572 SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
573 break;
574
575 case NestedNameSpecifier::TypeSpec:
576 case NestedNameSpecifier::TypeSpecWithTemplate: {
577 TypeSourceInfo *TSInfo
578 = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
579 R.getBegin());
580 SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
581 BufferCapacity);
582 break;
583 }
584
585 case NestedNameSpecifier::Global:
586 break;
587 }
588
589 // Save the location of the '::'.
590 SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(),
591 Buffer, BufferSize, BufferCapacity);
592 }
593}
594
595void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
596 if (BufferCapacity)
597 free(Buffer);
598
599 if (!Other) {
Craig Topper36250ad2014-05-12 05:36:57 +0000600 Representation = nullptr;
Douglas Gregor9b272512011-02-28 23:58:31 +0000601 BufferSize = 0;
602 return;
603 }
604
605 // Rather than copying the data (which is wasteful), "adopt" the
606 // pointer (which points into the ASTContext) but set the capacity to zero to
607 // indicate that we don't own it.
608 Representation = Other.getNestedNameSpecifier();
609 Buffer = static_cast<char *>(Other.getOpaqueData());
610 BufferSize = Other.getDataLength();
611 BufferCapacity = 0;
612}
613
614NestedNameSpecifierLoc
615NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
616 if (!Representation)
617 return NestedNameSpecifierLoc();
618
619 // If we adopted our data pointer from elsewhere in the AST context, there's
620 // no need to copy the memory.
621 if (BufferCapacity == 0)
622 return NestedNameSpecifierLoc(Representation, Buffer);
623
624 // FIXME: After copying the source-location information, should we free
625 // our (temporary) buffer and adopt the ASTContext-allocated memory?
626 // Doing so would optimize repeated calls to getWithLocInContext().
627 void *Mem = Context.Allocate(BufferSize, llvm::alignOf<void *>());
628 memcpy(Mem, Buffer, BufferSize);
629 return NestedNameSpecifierLoc(Representation, Mem);
630}