1 //===--- NestedNameSpecifier.cpp - C++ nested name specifiers -----*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the NestedNameSpecifier class, which represents
11 // a C++ nested-name-specifier.
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 "llvm/Support/raw_ostream.h"
24 using namespace clang;
27 NestedNameSpecifier::FindOrInsert(const ASTContext &Context,
28 const NestedNameSpecifier &Mockup) {
29 llvm::FoldingSetNodeID ID;
33 NestedNameSpecifier *NNS
34 = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
36 NNS = new (Context, 4) NestedNameSpecifier(Mockup);
37 Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
44 NestedNameSpecifier::Create(const ASTContext &Context,
45 NestedNameSpecifier *Prefix, IdentifierInfo *II) {
46 assert(II && "Identifier cannot be NULL");
47 assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent");
49 NestedNameSpecifier Mockup;
50 Mockup.Prefix.setPointer(Prefix);
51 Mockup.Prefix.setInt(StoredIdentifier);
52 Mockup.Specifier = II;
53 return FindOrInsert(Context, Mockup);
57 NestedNameSpecifier::Create(const ASTContext &Context,
58 NestedNameSpecifier *Prefix, NamespaceDecl *NS) {
59 assert(NS && "Namespace cannot be NULL");
61 (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) &&
62 "Broken nested name specifier");
63 NestedNameSpecifier Mockup;
64 Mockup.Prefix.setPointer(Prefix);
65 Mockup.Prefix.setInt(StoredNamespaceOrAlias);
66 Mockup.Specifier = NS;
67 return FindOrInsert(Context, Mockup);
71 NestedNameSpecifier::Create(const ASTContext &Context,
72 NestedNameSpecifier *Prefix,
73 NamespaceAliasDecl *Alias) {
74 assert(Alias && "Namespace alias cannot be NULL");
76 (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) &&
77 "Broken nested name specifier");
78 NestedNameSpecifier Mockup;
79 Mockup.Prefix.setPointer(Prefix);
80 Mockup.Prefix.setInt(StoredNamespaceOrAlias);
81 Mockup.Specifier = Alias;
82 return FindOrInsert(Context, Mockup);
86 NestedNameSpecifier::Create(const ASTContext &Context,
87 NestedNameSpecifier *Prefix,
88 bool Template, const Type *T) {
89 assert(T && "Type cannot be NULL");
90 NestedNameSpecifier Mockup;
91 Mockup.Prefix.setPointer(Prefix);
92 Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec);
93 Mockup.Specifier = const_cast<Type*>(T);
94 return FindOrInsert(Context, Mockup);
98 NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) {
99 assert(II && "Identifier cannot be NULL");
100 NestedNameSpecifier Mockup;
101 Mockup.Prefix.setPointer(0);
102 Mockup.Prefix.setInt(StoredIdentifier);
103 Mockup.Specifier = II;
104 return FindOrInsert(Context, Mockup);
107 NestedNameSpecifier *
108 NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
109 if (!Context.GlobalNestedNameSpecifier)
110 Context.GlobalNestedNameSpecifier = new (Context, 4) NestedNameSpecifier();
111 return Context.GlobalNestedNameSpecifier;
114 NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const {
118 switch (Prefix.getInt()) {
119 case StoredIdentifier:
122 case StoredNamespaceOrAlias:
123 return isa<NamespaceDecl>(static_cast<NamedDecl *>(Specifier))? Namespace
129 case StoredTypeSpecWithTemplate:
130 return TypeSpecWithTemplate;
133 llvm_unreachable("Invalid NNS Kind!");
136 /// \brief Retrieve the namespace stored in this nested name
138 NamespaceDecl *NestedNameSpecifier::getAsNamespace() const {
139 if (Prefix.getInt() == StoredNamespaceOrAlias)
140 return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier));
145 /// \brief Retrieve the namespace alias stored in this nested name
147 NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const {
148 if (Prefix.getInt() == StoredNamespaceOrAlias)
149 return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier));
155 /// \brief Whether this nested name specifier refers to a dependent
157 bool NestedNameSpecifier::isDependent() const {
160 // Identifier specifiers always represent dependent types
169 case TypeSpecWithTemplate:
170 return getAsType()->isDependentType();
173 llvm_unreachable("Invalid NNS Kind!");
176 /// \brief Whether this nested name specifier refers to a dependent
178 bool NestedNameSpecifier::isInstantiationDependent() const {
181 // Identifier specifiers always represent dependent types
190 case TypeSpecWithTemplate:
191 return getAsType()->isInstantiationDependentType();
194 llvm_unreachable("Invalid NNS Kind!");
197 bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
200 return getPrefix() && getPrefix()->containsUnexpandedParameterPack();
208 case TypeSpecWithTemplate:
209 return getAsType()->containsUnexpandedParameterPack();
212 llvm_unreachable("Invalid NNS Kind!");
215 /// \brief Print this nested name specifier to the given output
218 NestedNameSpecifier::print(raw_ostream &OS,
219 const PrintingPolicy &Policy) const {
221 getPrefix()->print(OS, Policy);
225 OS << getAsIdentifier()->getName();
229 if (getAsNamespace()->isAnonymousNamespace())
232 OS << getAsNamespace()->getName();
236 OS << getAsNamespaceAlias()->getName();
242 case TypeSpecWithTemplate:
244 // Fall through to print the type.
248 const Type *T = getAsType();
250 PrintingPolicy InnerPolicy(Policy);
251 InnerPolicy.SuppressScope = true;
253 // Nested-name-specifiers are intended to contain minimally-qualified
254 // types. An actual ElaboratedType will not occur, since we'll store
255 // just the type that is referred to in the nested-name-specifier (e.g.,
256 // a TypedefType, TagType, etc.). However, when we are dealing with
257 // dependent template-id types (e.g., Outer<T>::template Inner<U>),
258 // the type requires its own nested-name-specifier for uniqueness, so we
259 // suppress that nested-name-specifier during printing.
260 assert(!isa<ElaboratedType>(T) &&
261 "Elaborated type in nested-name-specifier");
262 if (const TemplateSpecializationType *SpecType
263 = dyn_cast<TemplateSpecializationType>(T)) {
264 // Print the template name without its corresponding
265 // nested-name-specifier.
266 SpecType->getTemplateName().print(OS, InnerPolicy, true);
268 // Print the template argument list.
269 TypeStr = TemplateSpecializationType::PrintTemplateArgumentList(
271 SpecType->getNumArgs(),
274 // Print the type normally
275 TypeStr = QualType(T, 0).getAsString(InnerPolicy);
285 void NestedNameSpecifier::dump(const LangOptions &LO) {
286 print(llvm::errs(), PrintingPolicy(LO));
290 NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
291 assert(Qualifier && "Expected a non-NULL qualifier");
293 // Location of the trailing '::'.
294 unsigned Length = sizeof(unsigned);
296 switch (Qualifier->getKind()) {
297 case NestedNameSpecifier::Global:
298 // Nothing more to add.
301 case NestedNameSpecifier::Identifier:
302 case NestedNameSpecifier::Namespace:
303 case NestedNameSpecifier::NamespaceAlias:
304 // The location of the identifier or namespace name.
305 Length += sizeof(unsigned);
308 case NestedNameSpecifier::TypeSpecWithTemplate:
309 case NestedNameSpecifier::TypeSpec:
310 // The "void*" that points at the TypeLoc data.
311 // Note: the 'template' keyword is part of the TypeLoc.
312 Length += sizeof(void *);
320 NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
322 for (; Qualifier; Qualifier = Qualifier->getPrefix())
323 Length += getLocalDataLength(Qualifier);
328 /// \brief Load a (possibly unaligned) source location from a given address
330 SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
332 memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned));
333 return SourceLocation::getFromRawEncoding(Raw);
336 /// \brief Load a (possibly unaligned) pointer from a given address and
338 void *LoadPointer(void *Data, unsigned Offset) {
340 memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
345 SourceRange NestedNameSpecifierLoc::getSourceRange() const {
347 return SourceRange();
349 NestedNameSpecifierLoc First = *this;
350 while (NestedNameSpecifierLoc Prefix = First.getPrefix())
353 return SourceRange(First.getLocalSourceRange().getBegin(),
354 getLocalSourceRange().getEnd());
357 SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
359 return SourceRange();
361 unsigned Offset = getDataLength(Qualifier->getPrefix());
362 switch (Qualifier->getKind()) {
363 case NestedNameSpecifier::Global:
364 return LoadSourceLocation(Data, Offset);
366 case NestedNameSpecifier::Identifier:
367 case NestedNameSpecifier::Namespace:
368 case NestedNameSpecifier::NamespaceAlias:
369 return SourceRange(LoadSourceLocation(Data, Offset),
370 LoadSourceLocation(Data, Offset + sizeof(unsigned)));
372 case NestedNameSpecifier::TypeSpecWithTemplate:
373 case NestedNameSpecifier::TypeSpec: {
374 // The "void*" that points at the TypeLoc data.
375 // Note: the 'template' keyword is part of the TypeLoc.
376 void *TypeData = LoadPointer(Data, Offset);
377 TypeLoc TL(Qualifier->getAsType(), TypeData);
378 return SourceRange(TL.getBeginLoc(),
379 LoadSourceLocation(Data, Offset + sizeof(void*)));
383 llvm_unreachable("Invalid NNS Kind!");
386 TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
387 assert((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||
388 Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) &&
389 "Nested-name-specifier location is not a type");
391 // The "void*" that points at the TypeLoc data.
392 unsigned Offset = getDataLength(Qualifier->getPrefix());
393 void *TypeData = LoadPointer(Data, Offset);
394 return TypeLoc(Qualifier->getAsType(), TypeData);
398 void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
399 unsigned &BufferCapacity) {
400 if (BufferSize + (End - Start) > BufferCapacity) {
401 // Reallocate the buffer.
403 = std::max((unsigned)(BufferCapacity? BufferCapacity * 2
404 : sizeof(void*) * 2),
405 (unsigned)(BufferSize + (End - Start)));
406 char *NewBuffer = static_cast<char *>(malloc(NewCapacity));
407 memcpy(NewBuffer, Buffer, BufferSize);
412 BufferCapacity = NewCapacity;
415 memcpy(Buffer + BufferSize, Start, End - Start);
416 BufferSize += End-Start;
419 /// \brief Save a source location to the given buffer.
420 void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
421 unsigned &BufferSize, unsigned &BufferCapacity) {
422 unsigned Raw = Loc.getRawEncoding();
423 Append(reinterpret_cast<char *>(&Raw),
424 reinterpret_cast<char *>(&Raw) + sizeof(unsigned),
425 Buffer, BufferSize, BufferCapacity);
428 /// \brief Save a pointer to the given buffer.
429 void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
430 unsigned &BufferCapacity) {
431 Append(reinterpret_cast<char *>(&Ptr),
432 reinterpret_cast<char *>(&Ptr) + sizeof(void *),
433 Buffer, BufferSize, BufferCapacity);
437 NestedNameSpecifierLocBuilder::
438 NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
439 : Representation(Other.Representation), Buffer(0),
440 BufferSize(0), BufferCapacity(0)
445 if (Other.BufferCapacity == 0) {
446 // Shallow copy is okay.
447 Buffer = Other.Buffer;
448 BufferSize = Other.BufferSize;
453 BufferSize = Other.BufferSize;
454 BufferCapacity = Other.BufferSize;
455 Buffer = static_cast<char *>(malloc(BufferCapacity));
456 memcpy(Buffer, Other.Buffer, BufferSize);
459 NestedNameSpecifierLocBuilder &
460 NestedNameSpecifierLocBuilder::
461 operator=(const NestedNameSpecifierLocBuilder &Other) {
462 Representation = Other.Representation;
464 if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) {
465 // Re-use our storage.
466 BufferSize = Other.BufferSize;
467 memcpy(Buffer, Other.Buffer, BufferSize);
471 // Free our storage, if we have any.
472 if (BufferCapacity) {
484 if (Other.BufferCapacity == 0) {
485 // Shallow copy is okay.
486 Buffer = Other.Buffer;
487 BufferSize = Other.BufferSize;
492 BufferSize = Other.BufferSize;
493 BufferCapacity = BufferSize;
494 Buffer = static_cast<char *>(malloc(BufferSize));
495 memcpy(Buffer, Other.Buffer, BufferSize);
499 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
500 SourceLocation TemplateKWLoc,
502 SourceLocation ColonColonLoc) {
503 Representation = NestedNameSpecifier::Create(Context, Representation,
504 TemplateKWLoc.isValid(),
507 // Push source-location info into the buffer.
508 SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
509 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
512 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
513 IdentifierInfo *Identifier,
514 SourceLocation IdentifierLoc,
515 SourceLocation ColonColonLoc) {
516 Representation = NestedNameSpecifier::Create(Context, Representation,
519 // Push source-location info into the buffer.
520 SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
521 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
524 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
525 NamespaceDecl *Namespace,
526 SourceLocation NamespaceLoc,
527 SourceLocation ColonColonLoc) {
528 Representation = NestedNameSpecifier::Create(Context, Representation,
531 // Push source-location info into the buffer.
532 SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
533 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
536 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
537 NamespaceAliasDecl *Alias,
538 SourceLocation AliasLoc,
539 SourceLocation ColonColonLoc) {
540 Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
542 // Push source-location info into the buffer.
543 SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
544 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
547 void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
548 SourceLocation ColonColonLoc) {
549 assert(!Representation && "Already have a nested-name-specifier!?");
550 Representation = NestedNameSpecifier::GlobalSpecifier(Context);
552 // Push source-location info into the buffer.
553 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
556 void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
557 NestedNameSpecifier *Qualifier,
559 Representation = Qualifier;
561 // Construct bogus (but well-formed) source information for the
562 // nested-name-specifier.
564 SmallVector<NestedNameSpecifier *, 4> Stack;
565 for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix())
566 Stack.push_back(NNS);
567 while (!Stack.empty()) {
568 NestedNameSpecifier *NNS = Stack.back();
570 switch (NNS->getKind()) {
571 case NestedNameSpecifier::Identifier:
572 case NestedNameSpecifier::Namespace:
573 case NestedNameSpecifier::NamespaceAlias:
574 SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
577 case NestedNameSpecifier::TypeSpec:
578 case NestedNameSpecifier::TypeSpecWithTemplate: {
579 TypeSourceInfo *TSInfo
580 = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
582 SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
587 case NestedNameSpecifier::Global:
591 // Save the location of the '::'.
592 SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(),
593 Buffer, BufferSize, BufferCapacity);
597 void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
607 // Rather than copying the data (which is wasteful), "adopt" the
608 // pointer (which points into the ASTContext) but set the capacity to zero to
609 // indicate that we don't own it.
610 Representation = Other.getNestedNameSpecifier();
611 Buffer = static_cast<char *>(Other.getOpaqueData());
612 BufferSize = Other.getDataLength();
616 NestedNameSpecifierLoc
617 NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
619 return NestedNameSpecifierLoc();
621 // If we adopted our data pointer from elsewhere in the AST context, there's
622 // no need to copy the memory.
623 if (BufferCapacity == 0)
624 return NestedNameSpecifierLoc(Representation, Buffer);
626 // FIXME: After copying the source-location information, should we free
627 // our (temporary) buffer and adopt the ASTContext-allocated memory?
628 // Doing so would optimize repeated calls to getWithLocInContext().
629 void *Mem = Context.Allocate(BufferSize, llvm::alignOf<void *>());
630 memcpy(Mem, Buffer, BufferSize);
631 return NestedNameSpecifierLoc(Representation, Mem);