blob: 49b119b8e05c5ae4cd741d4e914ceb73d1149a03 [file] [log] [blame]
Douglas Gregore4e5b052009-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 Gregor14aba762011-02-24 02:36:08 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000018#include "clang/AST/PrettyPrinter.h"
Douglas Gregore4e5b052009-03-19 00:18:19 +000019#include "clang/AST/Type.h"
Douglas Gregorc34348a2011-02-24 17:54:50 +000020#include "clang/AST/TypeLoc.h"
Richard Smith47467042012-08-15 01:41:43 +000021#include "llvm/Support/AlignOf.h"
Douglas Gregorbad35182009-03-19 03:51:16 +000022#include "llvm/Support/raw_ostream.h"
Douglas Gregorab452ba2009-03-26 23:50:42 +000023#include <cassert>
Douglas Gregorbad35182009-03-19 03:51:16 +000024
Douglas Gregore4e5b052009-03-19 00:18:19 +000025using namespace clang;
26
Douglas Gregorab452ba2009-03-26 23:50:42 +000027NestedNameSpecifier *
Jay Foad4ba2a172011-01-12 09:06:06 +000028NestedNameSpecifier::FindOrInsert(const ASTContext &Context,
Douglas Gregorab452ba2009-03-26 23:50:42 +000029 const NestedNameSpecifier &Mockup) {
30 llvm::FoldingSetNodeID ID;
31 Mockup.Profile(ID);
Douglas Gregore4e5b052009-03-19 00:18:19 +000032
Douglas Gregorab452ba2009-03-26 23:50:42 +000033 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000034 NestedNameSpecifier *NNS
Douglas Gregorab452ba2009-03-26 23:50:42 +000035 = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
36 if (!NNS) {
Richard Smith47467042012-08-15 01:41:43 +000037 NNS = new (Context, llvm::alignOf<NestedNameSpecifier>())
38 NestedNameSpecifier(Mockup);
Douglas Gregorab452ba2009-03-26 23:50:42 +000039 Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
40 }
Douglas Gregore4e5b052009-03-19 00:18:19 +000041
Douglas Gregorab452ba2009-03-26 23:50:42 +000042 return NNS;
Douglas Gregore4e5b052009-03-19 00:18:19 +000043}
Douglas Gregorbad35182009-03-19 03:51:16 +000044
Douglas Gregorab452ba2009-03-26 23:50:42 +000045NestedNameSpecifier *
Jay Foad4ba2a172011-01-12 09:06:06 +000046NestedNameSpecifier::Create(const ASTContext &Context,
47 NestedNameSpecifier *Prefix, IdentifierInfo *II) {
Douglas Gregorab452ba2009-03-26 23:50:42 +000048 assert(II && "Identifier cannot be NULL");
Douglas Gregor3b6afbb2009-09-09 00:23:06 +000049 assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent");
Douglas Gregorbad35182009-03-19 03:51:16 +000050
Douglas Gregorab452ba2009-03-26 23:50:42 +000051 NestedNameSpecifier Mockup;
Douglas Gregor17343172009-04-01 00:28:59 +000052 Mockup.Prefix.setPointer(Prefix);
Douglas Gregor14aba762011-02-24 02:36:08 +000053 Mockup.Prefix.setInt(StoredIdentifier);
Douglas Gregor17343172009-04-01 00:28:59 +000054 Mockup.Specifier = II;
Douglas Gregorab452ba2009-03-26 23:50:42 +000055 return FindOrInsert(Context, Mockup);
56}
Douglas Gregorbad35182009-03-19 03:51:16 +000057
Douglas Gregorab452ba2009-03-26 23:50:42 +000058NestedNameSpecifier *
Jay Foad4ba2a172011-01-12 09:06:06 +000059NestedNameSpecifier::Create(const ASTContext &Context,
60 NestedNameSpecifier *Prefix, NamespaceDecl *NS) {
Douglas Gregorab452ba2009-03-26 23:50:42 +000061 assert(NS && "Namespace cannot be NULL");
Mike Stump1eb44332009-09-09 15:08:12 +000062 assert((!Prefix ||
Douglas Gregorab452ba2009-03-26 23:50:42 +000063 (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) &&
64 "Broken nested name specifier");
65 NestedNameSpecifier Mockup;
Douglas Gregor17343172009-04-01 00:28:59 +000066 Mockup.Prefix.setPointer(Prefix);
Douglas Gregor14aba762011-02-24 02:36:08 +000067 Mockup.Prefix.setInt(StoredNamespaceOrAlias);
Douglas Gregor17343172009-04-01 00:28:59 +000068 Mockup.Specifier = NS;
Douglas Gregorab452ba2009-03-26 23:50:42 +000069 return FindOrInsert(Context, Mockup);
70}
71
72NestedNameSpecifier *
Jay Foad4ba2a172011-01-12 09:06:06 +000073NestedNameSpecifier::Create(const ASTContext &Context,
Douglas Gregor14aba762011-02-24 02:36:08 +000074 NestedNameSpecifier *Prefix,
75 NamespaceAliasDecl *Alias) {
76 assert(Alias && "Namespace alias cannot be NULL");
77 assert((!Prefix ||
78 (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) &&
79 "Broken nested name specifier");
80 NestedNameSpecifier Mockup;
81 Mockup.Prefix.setPointer(Prefix);
82 Mockup.Prefix.setInt(StoredNamespaceOrAlias);
83 Mockup.Specifier = Alias;
84 return FindOrInsert(Context, Mockup);
85}
86
87NestedNameSpecifier *
88NestedNameSpecifier::Create(const ASTContext &Context,
Jay Foad4ba2a172011-01-12 09:06:06 +000089 NestedNameSpecifier *Prefix,
John McCallf4c73712011-01-19 06:33:43 +000090 bool Template, const Type *T) {
Douglas Gregorab452ba2009-03-26 23:50:42 +000091 assert(T && "Type cannot be NULL");
92 NestedNameSpecifier Mockup;
Douglas Gregor17343172009-04-01 00:28:59 +000093 Mockup.Prefix.setPointer(Prefix);
Douglas Gregor14aba762011-02-24 02:36:08 +000094 Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec);
John McCallf4c73712011-01-19 06:33:43 +000095 Mockup.Specifier = const_cast<Type*>(T);
Douglas Gregorab452ba2009-03-26 23:50:42 +000096 return FindOrInsert(Context, Mockup);
97}
Douglas Gregor2700dcd2009-09-02 23:58:38 +000098
99NestedNameSpecifier *
Jay Foad4ba2a172011-01-12 09:06:06 +0000100NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) {
Douglas Gregor2700dcd2009-09-02 23:58:38 +0000101 assert(II && "Identifier cannot be NULL");
102 NestedNameSpecifier Mockup;
103 Mockup.Prefix.setPointer(0);
Douglas Gregor14aba762011-02-24 02:36:08 +0000104 Mockup.Prefix.setInt(StoredIdentifier);
Douglas Gregor2700dcd2009-09-02 23:58:38 +0000105 Mockup.Specifier = II;
106 return FindOrInsert(Context, Mockup);
107}
108
Jay Foad4ba2a172011-01-12 09:06:06 +0000109NestedNameSpecifier *
110NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
Douglas Gregorab452ba2009-03-26 23:50:42 +0000111 if (!Context.GlobalNestedNameSpecifier)
Richard Smith47467042012-08-15 01:41:43 +0000112 Context.GlobalNestedNameSpecifier =
113 new (Context, llvm::alignOf<NestedNameSpecifier>())
114 NestedNameSpecifier();
Douglas Gregorab452ba2009-03-26 23:50:42 +0000115 return Context.GlobalNestedNameSpecifier;
116}
117
Douglas Gregor14aba762011-02-24 02:36:08 +0000118NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const {
119 if (Specifier == 0)
120 return Global;
121
122 switch (Prefix.getInt()) {
123 case StoredIdentifier:
124 return Identifier;
125
126 case StoredNamespaceOrAlias:
127 return isa<NamespaceDecl>(static_cast<NamedDecl *>(Specifier))? Namespace
128 : NamespaceAlias;
129
130 case StoredTypeSpec:
131 return TypeSpec;
132
133 case StoredTypeSpecWithTemplate:
134 return TypeSpecWithTemplate;
135 }
136
David Blaikie30263482012-01-20 21:50:17 +0000137 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor14aba762011-02-24 02:36:08 +0000138}
139
140/// \brief Retrieve the namespace stored in this nested name
141/// specifier.
142NamespaceDecl *NestedNameSpecifier::getAsNamespace() const {
143 if (Prefix.getInt() == StoredNamespaceOrAlias)
144 return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier));
145
146 return 0;
147}
148
149/// \brief Retrieve the namespace alias stored in this nested name
150/// specifier.
151NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const {
152 if (Prefix.getInt() == StoredNamespaceOrAlias)
153 return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier));
154
155 return 0;
156}
157
158
Douglas Gregorab452ba2009-03-26 23:50:42 +0000159/// \brief Whether this nested name specifier refers to a dependent
160/// type or not.
161bool NestedNameSpecifier::isDependent() const {
162 switch (getKind()) {
163 case Identifier:
164 // Identifier specifiers always represent dependent types
165 return true;
166
167 case Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +0000168 case NamespaceAlias:
Douglas Gregorab452ba2009-03-26 23:50:42 +0000169 case Global:
170 return false;
171
172 case TypeSpec:
173 case TypeSpecWithTemplate:
174 return getAsType()->isDependentType();
Douglas Gregorbad35182009-03-19 03:51:16 +0000175 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000176
David Blaikie30263482012-01-20 21:50:17 +0000177 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregorab452ba2009-03-26 23:50:42 +0000178}
179
Douglas Gregor561f8122011-07-01 01:22:09 +0000180/// \brief Whether this nested name specifier refers to a dependent
181/// type or not.
182bool NestedNameSpecifier::isInstantiationDependent() const {
183 switch (getKind()) {
184 case Identifier:
185 // Identifier specifiers always represent dependent types
186 return true;
187
188 case Namespace:
189 case NamespaceAlias:
190 case Global:
191 return false;
192
193 case TypeSpec:
194 case TypeSpecWithTemplate:
195 return getAsType()->isInstantiationDependentType();
196 }
David Blaikie30263482012-01-20 21:50:17 +0000197
198 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregor561f8122011-07-01 01:22:09 +0000199}
200
Douglas Gregord0937222010-12-13 22:49:22 +0000201bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
202 switch (getKind()) {
203 case Identifier:
204 return getPrefix() && getPrefix()->containsUnexpandedParameterPack();
205
206 case Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +0000207 case NamespaceAlias:
Douglas Gregord0937222010-12-13 22:49:22 +0000208 case Global:
209 return false;
210
211 case TypeSpec:
212 case TypeSpecWithTemplate:
213 return getAsType()->containsUnexpandedParameterPack();
214 }
215
David Blaikie30263482012-01-20 21:50:17 +0000216 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregord0937222010-12-13 22:49:22 +0000217}
218
Douglas Gregorab452ba2009-03-26 23:50:42 +0000219/// \brief Print this nested name specifier to the given output
220/// stream.
Mike Stump1eb44332009-09-09 15:08:12 +0000221void
Chris Lattner5f9e2722011-07-23 10:55:15 +0000222NestedNameSpecifier::print(raw_ostream &OS,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000223 const PrintingPolicy &Policy) const {
Douglas Gregor17343172009-04-01 00:28:59 +0000224 if (getPrefix())
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000225 getPrefix()->print(OS, Policy);
Douglas Gregorab452ba2009-03-26 23:50:42 +0000226
227 switch (getKind()) {
228 case Identifier:
229 OS << getAsIdentifier()->getName();
230 break;
231
232 case Namespace:
Douglas Gregor25270b62011-11-03 00:16:13 +0000233 if (getAsNamespace()->isAnonymousNamespace())
234 return;
235
Douglas Gregor14aba762011-02-24 02:36:08 +0000236 OS << getAsNamespace()->getName();
237 break;
238
239 case NamespaceAlias:
240 OS << getAsNamespaceAlias()->getName();
Douglas Gregorab452ba2009-03-26 23:50:42 +0000241 break;
242
243 case Global:
244 break;
245
246 case TypeSpecWithTemplate:
247 OS << "template ";
248 // Fall through to print the type.
249
250 case TypeSpec: {
251 std::string TypeStr;
John McCallf4c73712011-01-19 06:33:43 +0000252 const Type *T = getAsType();
Douglas Gregorab452ba2009-03-26 23:50:42 +0000253
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000254 PrintingPolicy InnerPolicy(Policy);
John McCall2191b202009-09-05 06:31:47 +0000255 InnerPolicy.SuppressScope = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Douglas Gregordacd4342009-08-26 00:04:55 +0000257 // Nested-name-specifiers are intended to contain minimally-qualified
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000258 // types. An actual ElaboratedType will not occur, since we'll store
Douglas Gregordacd4342009-08-26 00:04:55 +0000259 // just the type that is referred to in the nested-name-specifier (e.g.,
260 // a TypedefType, TagType, etc.). However, when we are dealing with
Mike Stump1eb44332009-09-09 15:08:12 +0000261 // dependent template-id types (e.g., Outer<T>::template Inner<U>),
Douglas Gregordacd4342009-08-26 00:04:55 +0000262 // the type requires its own nested-name-specifier for uniqueness, so we
263 // suppress that nested-name-specifier during printing.
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000264 assert(!isa<ElaboratedType>(T) &&
265 "Elaborated type in nested-name-specifier");
Douglas Gregordacd4342009-08-26 00:04:55 +0000266 if (const TemplateSpecializationType *SpecType
267 = dyn_cast<TemplateSpecializationType>(T)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000268 // Print the template name without its corresponding
Douglas Gregordacd4342009-08-26 00:04:55 +0000269 // nested-name-specifier.
270 SpecType->getTemplateName().print(OS, InnerPolicy, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Douglas Gregordacd4342009-08-26 00:04:55 +0000272 // Print the template argument list.
273 TypeStr = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000274 SpecType->getArgs(),
275 SpecType->getNumArgs(),
Douglas Gregordacd4342009-08-26 00:04:55 +0000276 InnerPolicy);
277 } else {
278 // Print the type normally
Douglas Gregorfee8a3c2009-11-10 00:39:07 +0000279 TypeStr = QualType(T, 0).getAsString(InnerPolicy);
Douglas Gregordacd4342009-08-26 00:04:55 +0000280 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000281 OS << TypeStr;
282 break;
283 }
284 }
285
286 OS << "::";
287}
288
Chris Lattnere4f21422009-06-30 01:26:17 +0000289void NestedNameSpecifier::dump(const LangOptions &LO) {
290 print(llvm::errs(), PrintingPolicy(LO));
Douglas Gregord57959a2009-03-27 23:10:48 +0000291}
Douglas Gregorc34348a2011-02-24 17:54:50 +0000292
293unsigned
294NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
295 assert(Qualifier && "Expected a non-NULL qualifier");
296
297 // Location of the trailing '::'.
298 unsigned Length = sizeof(unsigned);
299
300 switch (Qualifier->getKind()) {
301 case NestedNameSpecifier::Global:
302 // Nothing more to add.
303 break;
304
305 case NestedNameSpecifier::Identifier:
306 case NestedNameSpecifier::Namespace:
307 case NestedNameSpecifier::NamespaceAlias:
308 // The location of the identifier or namespace name.
309 Length += sizeof(unsigned);
310 break;
311
312 case NestedNameSpecifier::TypeSpecWithTemplate:
313 case NestedNameSpecifier::TypeSpec:
314 // The "void*" that points at the TypeLoc data.
315 // Note: the 'template' keyword is part of the TypeLoc.
316 Length += sizeof(void *);
317 break;
318 }
319
320 return Length;
321}
322
323unsigned
324NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
325 unsigned Length = 0;
326 for (; Qualifier; Qualifier = Qualifier->getPrefix())
327 Length += getLocalDataLength(Qualifier);
328 return Length;
329}
330
331namespace {
332 /// \brief Load a (possibly unaligned) source location from a given address
333 /// and offset.
334 SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
335 unsigned Raw;
336 memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned));
337 return SourceLocation::getFromRawEncoding(Raw);
338 }
339
340 /// \brief Load a (possibly unaligned) pointer from a given address and
341 /// offset.
342 void *LoadPointer(void *Data, unsigned Offset) {
343 void *Result;
344 memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
345 return Result;
346 }
347}
348
Douglas Gregordc355712011-02-25 00:36:19 +0000349SourceRange NestedNameSpecifierLoc::getSourceRange() const {
Douglas Gregordb992412011-02-25 16:33:46 +0000350 if (!Qualifier)
351 return SourceRange();
352
Douglas Gregorc34348a2011-02-24 17:54:50 +0000353 NestedNameSpecifierLoc First = *this;
Douglas Gregordb992412011-02-25 16:33:46 +0000354 while (NestedNameSpecifierLoc Prefix = First.getPrefix())
Douglas Gregorc34348a2011-02-24 17:54:50 +0000355 First = Prefix;
356
357 return SourceRange(First.getLocalSourceRange().getBegin(),
358 getLocalSourceRange().getEnd());
359}
360
Douglas Gregordc355712011-02-25 00:36:19 +0000361SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
Douglas Gregordb992412011-02-25 16:33:46 +0000362 if (!Qualifier)
363 return SourceRange();
364
Douglas Gregorc34348a2011-02-24 17:54:50 +0000365 unsigned Offset = getDataLength(Qualifier->getPrefix());
366 switch (Qualifier->getKind()) {
367 case NestedNameSpecifier::Global:
368 return LoadSourceLocation(Data, Offset);
369
370 case NestedNameSpecifier::Identifier:
371 case NestedNameSpecifier::Namespace:
372 case NestedNameSpecifier::NamespaceAlias:
373 return SourceRange(LoadSourceLocation(Data, Offset),
374 LoadSourceLocation(Data, Offset + sizeof(unsigned)));
375
376 case NestedNameSpecifier::TypeSpecWithTemplate:
377 case NestedNameSpecifier::TypeSpec: {
378 // The "void*" that points at the TypeLoc data.
379 // Note: the 'template' keyword is part of the TypeLoc.
380 void *TypeData = LoadPointer(Data, Offset);
381 TypeLoc TL(Qualifier->getAsType(), TypeData);
382 return SourceRange(TL.getBeginLoc(),
383 LoadSourceLocation(Data, Offset + sizeof(void*)));
384 }
385 }
David Blaikie30263482012-01-20 21:50:17 +0000386
387 llvm_unreachable("Invalid NNS Kind!");
Douglas Gregorc34348a2011-02-24 17:54:50 +0000388}
Douglas Gregordc355712011-02-25 00:36:19 +0000389
390TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
391 assert((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||
392 Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) &&
393 "Nested-name-specifier location is not a type");
394
395 // The "void*" that points at the TypeLoc data.
396 unsigned Offset = getDataLength(Qualifier->getPrefix());
397 void *TypeData = LoadPointer(Data, Offset);
398 return TypeLoc(Qualifier->getAsType(), TypeData);
399}
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000400
401namespace {
402 void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
403 unsigned &BufferCapacity) {
404 if (BufferSize + (End - Start) > BufferCapacity) {
405 // Reallocate the buffer.
406 unsigned NewCapacity
407 = std::max((unsigned)(BufferCapacity? BufferCapacity * 2
408 : sizeof(void*) * 2),
409 (unsigned)(BufferSize + (End - Start)));
410 char *NewBuffer = static_cast<char *>(malloc(NewCapacity));
411 memcpy(NewBuffer, Buffer, BufferSize);
412
413 if (BufferCapacity)
414 free(Buffer);
415 Buffer = NewBuffer;
416 BufferCapacity = NewCapacity;
417 }
418
419 memcpy(Buffer + BufferSize, Start, End - Start);
420 BufferSize += End-Start;
421 }
422
423 /// \brief Save a source location to the given buffer.
424 void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
425 unsigned &BufferSize, unsigned &BufferCapacity) {
426 unsigned Raw = Loc.getRawEncoding();
427 Append(reinterpret_cast<char *>(&Raw),
428 reinterpret_cast<char *>(&Raw) + sizeof(unsigned),
429 Buffer, BufferSize, BufferCapacity);
430 }
431
432 /// \brief Save a pointer to the given buffer.
433 void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
434 unsigned &BufferCapacity) {
435 Append(reinterpret_cast<char *>(&Ptr),
436 reinterpret_cast<char *>(&Ptr) + sizeof(void *),
437 Buffer, BufferSize, BufferCapacity);
438 }
439}
440
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000441NestedNameSpecifierLocBuilder::
442NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
443 : Representation(Other.Representation), Buffer(0),
444 BufferSize(0), BufferCapacity(0)
445{
446 if (!Other.Buffer)
447 return;
448
449 if (Other.BufferCapacity == 0) {
450 // Shallow copy is okay.
451 Buffer = Other.Buffer;
452 BufferSize = Other.BufferSize;
453 return;
454 }
455
456 // Deep copy
457 BufferSize = Other.BufferSize;
458 BufferCapacity = Other.BufferSize;
459 Buffer = static_cast<char *>(malloc(BufferCapacity));
460 memcpy(Buffer, Other.Buffer, BufferSize);
461}
462
463NestedNameSpecifierLocBuilder &
464NestedNameSpecifierLocBuilder::
465operator=(const NestedNameSpecifierLocBuilder &Other) {
466 Representation = Other.Representation;
467
468 if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) {
469 // Re-use our storage.
470 BufferSize = Other.BufferSize;
471 memcpy(Buffer, Other.Buffer, BufferSize);
472 return *this;
473 }
474
475 // Free our storage, if we have any.
476 if (BufferCapacity) {
477 free(Buffer);
478 BufferCapacity = 0;
479 }
480
481 if (!Other.Buffer) {
482 // Empty.
483 Buffer = 0;
484 BufferSize = 0;
485 return *this;
486 }
487
488 if (Other.BufferCapacity == 0) {
489 // Shallow copy is okay.
490 Buffer = Other.Buffer;
491 BufferSize = Other.BufferSize;
492 return *this;
493 }
494
495 // Deep copy.
496 BufferSize = Other.BufferSize;
497 BufferCapacity = BufferSize;
498 Buffer = static_cast<char *>(malloc(BufferSize));
499 memcpy(Buffer, Other.Buffer, BufferSize);
500 return *this;
501}
502
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000503void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
504 SourceLocation TemplateKWLoc,
505 TypeLoc TL,
506 SourceLocation ColonColonLoc) {
507 Representation = NestedNameSpecifier::Create(Context, Representation,
508 TemplateKWLoc.isValid(),
509 TL.getTypePtr());
510
511 // Push source-location info into the buffer.
512 SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
513 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
514}
515
516void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
517 IdentifierInfo *Identifier,
518 SourceLocation IdentifierLoc,
519 SourceLocation ColonColonLoc) {
520 Representation = NestedNameSpecifier::Create(Context, Representation,
521 Identifier);
522
523 // Push source-location info into the buffer.
524 SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
525 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
526}
527
528void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
529 NamespaceDecl *Namespace,
530 SourceLocation NamespaceLoc,
531 SourceLocation ColonColonLoc) {
532 Representation = NestedNameSpecifier::Create(Context, Representation,
533 Namespace);
534
535 // Push source-location info into the buffer.
536 SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
537 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
538}
539
540void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
541 NamespaceAliasDecl *Alias,
542 SourceLocation AliasLoc,
543 SourceLocation ColonColonLoc) {
544 Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
545
546 // Push source-location info into the buffer.
547 SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
548 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
549}
550
551void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
552 SourceLocation ColonColonLoc) {
553 assert(!Representation && "Already have a nested-name-specifier!?");
554 Representation = NestedNameSpecifier::GlobalSpecifier(Context);
555
556 // Push source-location info into the buffer.
557 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
558}
559
560void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
561 NestedNameSpecifier *Qualifier,
562 SourceRange R) {
563 Representation = Qualifier;
564
565 // Construct bogus (but well-formed) source information for the
566 // nested-name-specifier.
567 BufferSize = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000568 SmallVector<NestedNameSpecifier *, 4> Stack;
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000569 for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix())
570 Stack.push_back(NNS);
571 while (!Stack.empty()) {
572 NestedNameSpecifier *NNS = Stack.back();
573 Stack.pop_back();
574 switch (NNS->getKind()) {
575 case NestedNameSpecifier::Identifier:
576 case NestedNameSpecifier::Namespace:
577 case NestedNameSpecifier::NamespaceAlias:
578 SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
579 break;
580
581 case NestedNameSpecifier::TypeSpec:
582 case NestedNameSpecifier::TypeSpecWithTemplate: {
583 TypeSourceInfo *TSInfo
584 = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
585 R.getBegin());
586 SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
587 BufferCapacity);
588 break;
589 }
590
591 case NestedNameSpecifier::Global:
592 break;
593 }
594
595 // Save the location of the '::'.
596 SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(),
597 Buffer, BufferSize, BufferCapacity);
598 }
599}
600
601void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
602 if (BufferCapacity)
603 free(Buffer);
604
605 if (!Other) {
606 Representation = 0;
607 BufferSize = 0;
608 return;
609 }
610
611 // Rather than copying the data (which is wasteful), "adopt" the
612 // pointer (which points into the ASTContext) but set the capacity to zero to
613 // indicate that we don't own it.
614 Representation = Other.getNestedNameSpecifier();
615 Buffer = static_cast<char *>(Other.getOpaqueData());
616 BufferSize = Other.getDataLength();
617 BufferCapacity = 0;
618}
619
620NestedNameSpecifierLoc
621NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
622 if (!Representation)
623 return NestedNameSpecifierLoc();
624
625 // If we adopted our data pointer from elsewhere in the AST context, there's
626 // no need to copy the memory.
627 if (BufferCapacity == 0)
628 return NestedNameSpecifierLoc(Representation, Buffer);
629
630 // FIXME: After copying the source-location information, should we free
631 // our (temporary) buffer and adopt the ASTContext-allocated memory?
632 // Doing so would optimize repeated calls to getWithLocInContext().
633 void *Mem = Context.Allocate(BufferSize, llvm::alignOf<void *>());
634 memcpy(Mem, Buffer, BufferSize);
635 return NestedNameSpecifierLoc(Representation, Mem);
636}