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/AlignOf.h"
22 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
28 NestedNameSpecifier::FindOrInsert(const ASTContext &Context,
29 const NestedNameSpecifier &Mockup) {
30 llvm::FoldingSetNodeID ID;
34 NestedNameSpecifier *NNS
35 = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
37 NNS = new (Context, llvm::alignOf<NestedNameSpecifier>())
38 NestedNameSpecifier(Mockup);
39 Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
46 NestedNameSpecifier::Create(const ASTContext &Context,
47 NestedNameSpecifier *Prefix, IdentifierInfo *II) {
48 assert(II && "Identifier cannot be NULL");
49 assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent");
51 NestedNameSpecifier Mockup;
52 Mockup.Prefix.setPointer(Prefix);
53 Mockup.Prefix.setInt(StoredIdentifier);
54 Mockup.Specifier = II;
55 return FindOrInsert(Context, Mockup);
59 NestedNameSpecifier::Create(const ASTContext &Context,
60 NestedNameSpecifier *Prefix,
61 const NamespaceDecl *NS) {
62 assert(NS && "Namespace cannot be NULL");
64 (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) &&
65 "Broken nested name specifier");
66 NestedNameSpecifier Mockup;
67 Mockup.Prefix.setPointer(Prefix);
68 Mockup.Prefix.setInt(StoredNamespaceOrAlias);
69 Mockup.Specifier = const_cast<NamespaceDecl *>(NS);
70 return FindOrInsert(Context, Mockup);
74 NestedNameSpecifier::Create(const ASTContext &Context,
75 NestedNameSpecifier *Prefix,
76 NamespaceAliasDecl *Alias) {
77 assert(Alias && "Namespace alias cannot be NULL");
79 (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) &&
80 "Broken nested name specifier");
81 NestedNameSpecifier Mockup;
82 Mockup.Prefix.setPointer(Prefix);
83 Mockup.Prefix.setInt(StoredNamespaceOrAlias);
84 Mockup.Specifier = Alias;
85 return FindOrInsert(Context, Mockup);
89 NestedNameSpecifier::Create(const ASTContext &Context,
90 NestedNameSpecifier *Prefix,
91 bool Template, const Type *T) {
92 assert(T && "Type cannot be NULL");
93 NestedNameSpecifier Mockup;
94 Mockup.Prefix.setPointer(Prefix);
95 Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec);
96 Mockup.Specifier = const_cast<Type*>(T);
97 return FindOrInsert(Context, Mockup);
100 NestedNameSpecifier *
101 NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) {
102 assert(II && "Identifier cannot be NULL");
103 NestedNameSpecifier Mockup;
104 Mockup.Prefix.setPointer(0);
105 Mockup.Prefix.setInt(StoredIdentifier);
106 Mockup.Specifier = II;
107 return FindOrInsert(Context, Mockup);
110 NestedNameSpecifier *
111 NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
112 if (!Context.GlobalNestedNameSpecifier)
113 Context.GlobalNestedNameSpecifier =
114 new (Context, llvm::alignOf<NestedNameSpecifier>())
115 NestedNameSpecifier();
116 return Context.GlobalNestedNameSpecifier;
119 NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const {
123 switch (Prefix.getInt()) {
124 case StoredIdentifier:
127 case StoredNamespaceOrAlias:
128 return isa<NamespaceDecl>(static_cast<NamedDecl *>(Specifier))? Namespace
134 case StoredTypeSpecWithTemplate:
135 return TypeSpecWithTemplate;
138 llvm_unreachable("Invalid NNS Kind!");
141 /// \brief Retrieve the namespace stored in this nested name
143 NamespaceDecl *NestedNameSpecifier::getAsNamespace() const {
144 if (Prefix.getInt() == StoredNamespaceOrAlias)
145 return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier));
150 /// \brief Retrieve the namespace alias stored in this nested name
152 NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const {
153 if (Prefix.getInt() == StoredNamespaceOrAlias)
154 return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier));
160 /// \brief Whether this nested name specifier refers to a dependent
162 bool NestedNameSpecifier::isDependent() const {
165 // Identifier specifiers always represent dependent types
174 case TypeSpecWithTemplate:
175 return getAsType()->isDependentType();
178 llvm_unreachable("Invalid NNS Kind!");
181 /// \brief Whether this nested name specifier refers to a dependent
183 bool NestedNameSpecifier::isInstantiationDependent() const {
186 // Identifier specifiers always represent dependent types
195 case TypeSpecWithTemplate:
196 return getAsType()->isInstantiationDependentType();
199 llvm_unreachable("Invalid NNS Kind!");
202 bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
205 return getPrefix() && getPrefix()->containsUnexpandedParameterPack();
213 case TypeSpecWithTemplate:
214 return getAsType()->containsUnexpandedParameterPack();
217 llvm_unreachable("Invalid NNS Kind!");
220 /// \brief Print this nested name specifier to the given output
223 NestedNameSpecifier::print(raw_ostream &OS,
224 const PrintingPolicy &Policy) const {
226 getPrefix()->print(OS, Policy);
230 OS << getAsIdentifier()->getName();
234 if (getAsNamespace()->isAnonymousNamespace())
237 OS << getAsNamespace()->getName();
241 OS << getAsNamespaceAlias()->getName();
247 case TypeSpecWithTemplate:
249 // Fall through to print the type.
252 const Type *T = getAsType();
254 PrintingPolicy InnerPolicy(Policy);
255 InnerPolicy.SuppressScope = true;
257 // Nested-name-specifiers are intended to contain minimally-qualified
258 // types. An actual ElaboratedType will not occur, since we'll store
259 // 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
261 // dependent template-id types (e.g., Outer<T>::template Inner<U>),
262 // the type requires its own nested-name-specifier for uniqueness, so we
263 // suppress that nested-name-specifier during printing.
264 assert(!isa<ElaboratedType>(T) &&
265 "Elaborated type in nested-name-specifier");
266 if (const TemplateSpecializationType *SpecType
267 = dyn_cast<TemplateSpecializationType>(T)) {
268 // Print the template name without its corresponding
269 // nested-name-specifier.
270 SpecType->getTemplateName().print(OS, InnerPolicy, true);
272 // Print the template argument list.
273 TemplateSpecializationType::PrintTemplateArgumentList(
274 OS, SpecType->getArgs(), SpecType->getNumArgs(), InnerPolicy);
276 // Print the type normally
277 QualType(T, 0).print(OS, InnerPolicy);
286 void NestedNameSpecifier::dump(const LangOptions &LO) {
287 print(llvm::errs(), PrintingPolicy(LO));
291 NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
292 assert(Qualifier && "Expected a non-NULL qualifier");
294 // Location of the trailing '::'.
295 unsigned Length = sizeof(unsigned);
297 switch (Qualifier->getKind()) {
298 case NestedNameSpecifier::Global:
299 // Nothing more to add.
302 case NestedNameSpecifier::Identifier:
303 case NestedNameSpecifier::Namespace:
304 case NestedNameSpecifier::NamespaceAlias:
305 // The location of the identifier or namespace name.
306 Length += sizeof(unsigned);
309 case NestedNameSpecifier::TypeSpecWithTemplate:
310 case NestedNameSpecifier::TypeSpec:
311 // The "void*" that points at the TypeLoc data.
312 // Note: the 'template' keyword is part of the TypeLoc.
313 Length += sizeof(void *);
321 NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
323 for (; Qualifier; Qualifier = Qualifier->getPrefix())
324 Length += getLocalDataLength(Qualifier);
329 /// \brief Load a (possibly unaligned) source location from a given address
331 SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
333 memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned));
334 return SourceLocation::getFromRawEncoding(Raw);
337 /// \brief Load a (possibly unaligned) pointer from a given address and
339 void *LoadPointer(void *Data, unsigned Offset) {
341 memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
346 SourceRange NestedNameSpecifierLoc::getSourceRange() const {
348 return SourceRange();
350 NestedNameSpecifierLoc First = *this;
351 while (NestedNameSpecifierLoc Prefix = First.getPrefix())
354 return SourceRange(First.getLocalSourceRange().getBegin(),
355 getLocalSourceRange().getEnd());
358 SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
360 return SourceRange();
362 unsigned Offset = getDataLength(Qualifier->getPrefix());
363 switch (Qualifier->getKind()) {
364 case NestedNameSpecifier::Global:
365 return LoadSourceLocation(Data, Offset);
367 case NestedNameSpecifier::Identifier:
368 case NestedNameSpecifier::Namespace:
369 case NestedNameSpecifier::NamespaceAlias:
370 return SourceRange(LoadSourceLocation(Data, Offset),
371 LoadSourceLocation(Data, Offset + sizeof(unsigned)));
373 case NestedNameSpecifier::TypeSpecWithTemplate:
374 case NestedNameSpecifier::TypeSpec: {
375 // The "void*" that points at the TypeLoc data.
376 // Note: the 'template' keyword is part of the TypeLoc.
377 void *TypeData = LoadPointer(Data, Offset);
378 TypeLoc TL(Qualifier->getAsType(), TypeData);
379 return SourceRange(TL.getBeginLoc(),
380 LoadSourceLocation(Data, Offset + sizeof(void*)));
384 llvm_unreachable("Invalid NNS Kind!");
387 TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
388 assert((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||
389 Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) &&
390 "Nested-name-specifier location is not a type");
392 // The "void*" that points at the TypeLoc data.
393 unsigned Offset = getDataLength(Qualifier->getPrefix());
394 void *TypeData = LoadPointer(Data, Offset);
395 return TypeLoc(Qualifier->getAsType(), TypeData);
399 void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
400 unsigned &BufferCapacity) {
401 if (BufferSize + (End - Start) > BufferCapacity) {
402 // Reallocate the buffer.
404 = std::max((unsigned)(BufferCapacity? BufferCapacity * 2
405 : sizeof(void*) * 2),
406 (unsigned)(BufferSize + (End - Start)));
407 char *NewBuffer = static_cast<char *>(malloc(NewCapacity));
408 memcpy(NewBuffer, Buffer, BufferSize);
413 BufferCapacity = NewCapacity;
416 memcpy(Buffer + BufferSize, Start, End - Start);
417 BufferSize += End-Start;
420 /// \brief Save a source location to the given buffer.
421 void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
422 unsigned &BufferSize, unsigned &BufferCapacity) {
423 unsigned Raw = Loc.getRawEncoding();
424 Append(reinterpret_cast<char *>(&Raw),
425 reinterpret_cast<char *>(&Raw) + sizeof(unsigned),
426 Buffer, BufferSize, BufferCapacity);
429 /// \brief Save a pointer to the given buffer.
430 void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
431 unsigned &BufferCapacity) {
432 Append(reinterpret_cast<char *>(&Ptr),
433 reinterpret_cast<char *>(&Ptr) + sizeof(void *),
434 Buffer, BufferSize, BufferCapacity);
438 NestedNameSpecifierLocBuilder::
439 NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
440 : Representation(Other.Representation), Buffer(0),
441 BufferSize(0), BufferCapacity(0)
446 if (Other.BufferCapacity == 0) {
447 // Shallow copy is okay.
448 Buffer = Other.Buffer;
449 BufferSize = Other.BufferSize;
454 BufferSize = Other.BufferSize;
455 BufferCapacity = Other.BufferSize;
456 Buffer = static_cast<char *>(malloc(BufferCapacity));
457 memcpy(Buffer, Other.Buffer, BufferSize);
460 NestedNameSpecifierLocBuilder &
461 NestedNameSpecifierLocBuilder::
462 operator=(const NestedNameSpecifierLocBuilder &Other) {
463 Representation = Other.Representation;
465 if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) {
466 // Re-use our storage.
467 BufferSize = Other.BufferSize;
468 memcpy(Buffer, Other.Buffer, BufferSize);
472 // Free our storage, if we have any.
473 if (BufferCapacity) {
485 if (Other.BufferCapacity == 0) {
486 // Shallow copy is okay.
487 Buffer = Other.Buffer;
488 BufferSize = Other.BufferSize;
493 BufferSize = Other.BufferSize;
494 BufferCapacity = BufferSize;
495 Buffer = static_cast<char *>(malloc(BufferSize));
496 memcpy(Buffer, Other.Buffer, BufferSize);
500 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
501 SourceLocation TemplateKWLoc,
503 SourceLocation ColonColonLoc) {
504 Representation = NestedNameSpecifier::Create(Context, Representation,
505 TemplateKWLoc.isValid(),
508 // Push source-location info into the buffer.
509 SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
510 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
513 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
514 IdentifierInfo *Identifier,
515 SourceLocation IdentifierLoc,
516 SourceLocation ColonColonLoc) {
517 Representation = NestedNameSpecifier::Create(Context, Representation,
520 // Push source-location info into the buffer.
521 SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
522 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
525 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
526 NamespaceDecl *Namespace,
527 SourceLocation NamespaceLoc,
528 SourceLocation ColonColonLoc) {
529 Representation = NestedNameSpecifier::Create(Context, Representation,
532 // Push source-location info into the buffer.
533 SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
534 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
537 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
538 NamespaceAliasDecl *Alias,
539 SourceLocation AliasLoc,
540 SourceLocation ColonColonLoc) {
541 Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
543 // Push source-location info into the buffer.
544 SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
545 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
548 void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
549 SourceLocation ColonColonLoc) {
550 assert(!Representation && "Already have a nested-name-specifier!?");
551 Representation = NestedNameSpecifier::GlobalSpecifier(Context);
553 // Push source-location info into the buffer.
554 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
557 void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
558 NestedNameSpecifier *Qualifier,
560 Representation = Qualifier;
562 // Construct bogus (but well-formed) source information for the
563 // nested-name-specifier.
565 SmallVector<NestedNameSpecifier *, 4> Stack;
566 for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix())
567 Stack.push_back(NNS);
568 while (!Stack.empty()) {
569 NestedNameSpecifier *NNS = Stack.back();
571 switch (NNS->getKind()) {
572 case NestedNameSpecifier::Identifier:
573 case NestedNameSpecifier::Namespace:
574 case NestedNameSpecifier::NamespaceAlias:
575 SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
578 case NestedNameSpecifier::TypeSpec:
579 case NestedNameSpecifier::TypeSpecWithTemplate: {
580 TypeSourceInfo *TSInfo
581 = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
583 SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
588 case NestedNameSpecifier::Global:
592 // Save the location of the '::'.
593 SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(),
594 Buffer, BufferSize, BufferCapacity);
598 void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
608 // Rather than copying the data (which is wasteful), "adopt" the
609 // pointer (which points into the ASTContext) but set the capacity to zero to
610 // indicate that we don't own it.
611 Representation = Other.getNestedNameSpecifier();
612 Buffer = static_cast<char *>(Other.getOpaqueData());
613 BufferSize = Other.getDataLength();
617 NestedNameSpecifierLoc
618 NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
620 return NestedNameSpecifierLoc();
622 // If we adopted our data pointer from elsewhere in the AST context, there's
623 // no need to copy the memory.
624 if (BufferCapacity == 0)
625 return NestedNameSpecifierLoc(Representation, Buffer);
627 // FIXME: After copying the source-location information, should we free
628 // our (temporary) buffer and adopt the ASTContext-allocated memory?
629 // Doing so would optimize repeated calls to getWithLocInContext().
630 void *Mem = Context.Allocate(BufferSize, llvm::alignOf<void *>());
631 memcpy(Mem, Buffer, BufferSize);
632 return NestedNameSpecifierLoc(Representation, Mem);