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;
33 void *InsertPos = nullptr;
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() == nullptr &&
65 Prefix->getAsIdentifier() == nullptr)) &&
66 "Broken nested name specifier");
67 NestedNameSpecifier Mockup;
68 Mockup.Prefix.setPointer(Prefix);
69 Mockup.Prefix.setInt(StoredNamespaceOrAlias);
70 Mockup.Specifier = const_cast<NamespaceDecl *>(NS);
71 return FindOrInsert(Context, Mockup);
75 NestedNameSpecifier::Create(const ASTContext &Context,
76 NestedNameSpecifier *Prefix,
77 NamespaceAliasDecl *Alias) {
78 assert(Alias && "Namespace alias cannot be NULL");
80 (Prefix->getAsType() == nullptr &&
81 Prefix->getAsIdentifier() == nullptr)) &&
82 "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);
91 NestedNameSpecifier::Create(const ASTContext &Context,
92 NestedNameSpecifier *Prefix,
93 bool Template, const Type *T) {
94 assert(T && "Type cannot be NULL");
95 NestedNameSpecifier Mockup;
96 Mockup.Prefix.setPointer(Prefix);
97 Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec);
98 Mockup.Specifier = const_cast<Type*>(T);
99 return FindOrInsert(Context, Mockup);
102 NestedNameSpecifier *
103 NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) {
104 assert(II && "Identifier cannot be NULL");
105 NestedNameSpecifier Mockup;
106 Mockup.Prefix.setPointer(nullptr);
107 Mockup.Prefix.setInt(StoredIdentifier);
108 Mockup.Specifier = II;
109 return FindOrInsert(Context, Mockup);
112 NestedNameSpecifier *
113 NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
114 if (!Context.GlobalNestedNameSpecifier)
115 Context.GlobalNestedNameSpecifier =
116 new (Context, llvm::alignOf<NestedNameSpecifier>())
117 NestedNameSpecifier();
118 return Context.GlobalNestedNameSpecifier;
121 NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const {
125 switch (Prefix.getInt()) {
126 case StoredIdentifier:
129 case StoredNamespaceOrAlias:
130 return isa<NamespaceDecl>(static_cast<NamedDecl *>(Specifier))? Namespace
136 case StoredTypeSpecWithTemplate:
137 return TypeSpecWithTemplate;
140 llvm_unreachable("Invalid NNS Kind!");
143 /// \brief Retrieve the namespace stored in this nested name
145 NamespaceDecl *NestedNameSpecifier::getAsNamespace() const {
146 if (Prefix.getInt() == StoredNamespaceOrAlias)
147 return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier));
152 /// \brief Retrieve the namespace alias stored in this nested name
154 NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const {
155 if (Prefix.getInt() == StoredNamespaceOrAlias)
156 return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier));
162 /// \brief Whether this nested name specifier refers to a dependent
164 bool NestedNameSpecifier::isDependent() const {
167 // Identifier specifiers always represent dependent types
176 case TypeSpecWithTemplate:
177 return getAsType()->isDependentType();
180 llvm_unreachable("Invalid NNS Kind!");
183 /// \brief Whether this nested name specifier refers to a dependent
185 bool NestedNameSpecifier::isInstantiationDependent() const {
188 // Identifier specifiers always represent dependent types
197 case TypeSpecWithTemplate:
198 return getAsType()->isInstantiationDependentType();
201 llvm_unreachable("Invalid NNS Kind!");
204 bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
207 return getPrefix() && getPrefix()->containsUnexpandedParameterPack();
215 case TypeSpecWithTemplate:
216 return getAsType()->containsUnexpandedParameterPack();
219 llvm_unreachable("Invalid NNS Kind!");
222 /// \brief Print this nested name specifier to the given output
225 NestedNameSpecifier::print(raw_ostream &OS,
226 const PrintingPolicy &Policy) const {
228 getPrefix()->print(OS, Policy);
232 OS << getAsIdentifier()->getName();
236 if (getAsNamespace()->isAnonymousNamespace())
239 OS << getAsNamespace()->getName();
243 OS << getAsNamespaceAlias()->getName();
249 case TypeSpecWithTemplate:
251 // Fall through to print the type.
254 const Type *T = getAsType();
256 PrintingPolicy InnerPolicy(Policy);
257 InnerPolicy.SuppressScope = true;
259 // Nested-name-specifiers are intended to contain minimally-qualified
260 // types. An actual ElaboratedType will not occur, since we'll store
261 // 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
263 // dependent template-id types (e.g., Outer<T>::template Inner<U>),
264 // the type requires its own nested-name-specifier for uniqueness, so we
265 // suppress that nested-name-specifier during printing.
266 assert(!isa<ElaboratedType>(T) &&
267 "Elaborated type in nested-name-specifier");
268 if (const TemplateSpecializationType *SpecType
269 = dyn_cast<TemplateSpecializationType>(T)) {
270 // Print the template name without its corresponding
271 // nested-name-specifier.
272 SpecType->getTemplateName().print(OS, InnerPolicy, true);
274 // Print the template argument list.
275 TemplateSpecializationType::PrintTemplateArgumentList(
276 OS, SpecType->getArgs(), SpecType->getNumArgs(), InnerPolicy);
278 // Print the type normally
279 QualType(T, 0).print(OS, InnerPolicy);
288 void NestedNameSpecifier::dump(const LangOptions &LO) {
289 print(llvm::errs(), PrintingPolicy(LO));
293 NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
294 assert(Qualifier && "Expected a non-NULL qualifier");
296 // Location of the trailing '::'.
297 unsigned Length = sizeof(unsigned);
299 switch (Qualifier->getKind()) {
300 case NestedNameSpecifier::Global:
301 // Nothing more to add.
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);
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 *);
323 NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
325 for (; Qualifier; Qualifier = Qualifier->getPrefix())
326 Length += getLocalDataLength(Qualifier);
331 /// \brief Load a (possibly unaligned) source location from a given address
333 SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
335 memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned));
336 return SourceLocation::getFromRawEncoding(Raw);
339 /// \brief Load a (possibly unaligned) pointer from a given address and
341 void *LoadPointer(void *Data, unsigned Offset) {
343 memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
348 SourceRange NestedNameSpecifierLoc::getSourceRange() const {
350 return SourceRange();
352 NestedNameSpecifierLoc First = *this;
353 while (NestedNameSpecifierLoc Prefix = First.getPrefix())
356 return SourceRange(First.getLocalSourceRange().getBegin(),
357 getLocalSourceRange().getEnd());
360 SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
362 return SourceRange();
364 unsigned Offset = getDataLength(Qualifier->getPrefix());
365 switch (Qualifier->getKind()) {
366 case NestedNameSpecifier::Global:
367 return LoadSourceLocation(Data, Offset);
369 case NestedNameSpecifier::Identifier:
370 case NestedNameSpecifier::Namespace:
371 case NestedNameSpecifier::NamespaceAlias:
372 return SourceRange(LoadSourceLocation(Data, Offset),
373 LoadSourceLocation(Data, Offset + sizeof(unsigned)));
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*)));
386 llvm_unreachable("Invalid NNS Kind!");
389 TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
390 assert((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||
391 Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) &&
392 "Nested-name-specifier location is not a type");
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);
401 void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
402 unsigned &BufferCapacity) {
403 if (BufferSize + (End - Start) > BufferCapacity) {
404 // Reallocate the buffer.
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);
415 BufferCapacity = NewCapacity;
418 memcpy(Buffer + BufferSize, Start, End - Start);
419 BufferSize += End-Start;
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);
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);
440 NestedNameSpecifierLocBuilder::
441 NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
442 : Representation(Other.Representation), Buffer(nullptr),
443 BufferSize(0), BufferCapacity(0)
448 if (Other.BufferCapacity == 0) {
449 // Shallow copy is okay.
450 Buffer = Other.Buffer;
451 BufferSize = Other.BufferSize;
456 Append(Other.Buffer, Other.Buffer + Other.BufferSize, 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 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
498 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
499 SourceLocation TemplateKWLoc,
501 SourceLocation ColonColonLoc) {
502 Representation = NestedNameSpecifier::Create(Context, Representation,
503 TemplateKWLoc.isValid(),
506 // Push source-location info into the buffer.
507 SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
508 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
511 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
512 IdentifierInfo *Identifier,
513 SourceLocation IdentifierLoc,
514 SourceLocation ColonColonLoc) {
515 Representation = NestedNameSpecifier::Create(Context, Representation,
518 // Push source-location info into the buffer.
519 SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
520 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
523 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
524 NamespaceDecl *Namespace,
525 SourceLocation NamespaceLoc,
526 SourceLocation ColonColonLoc) {
527 Representation = NestedNameSpecifier::Create(Context, Representation,
530 // Push source-location info into the buffer.
531 SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
532 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
535 void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
536 NamespaceAliasDecl *Alias,
537 SourceLocation AliasLoc,
538 SourceLocation ColonColonLoc) {
539 Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
541 // Push source-location info into the buffer.
542 SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
543 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
546 void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
547 SourceLocation ColonColonLoc) {
548 assert(!Representation && "Already have a nested-name-specifier!?");
549 Representation = NestedNameSpecifier::GlobalSpecifier(Context);
551 // Push source-location info into the buffer.
552 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
555 void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
556 NestedNameSpecifier *Qualifier,
558 Representation = Qualifier;
560 // Construct bogus (but well-formed) source information for the
561 // nested-name-specifier.
563 SmallVector<NestedNameSpecifier *, 4> Stack;
564 for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix())
565 Stack.push_back(NNS);
566 while (!Stack.empty()) {
567 NestedNameSpecifier *NNS = Stack.pop_back_val();
568 switch (NNS->getKind()) {
569 case NestedNameSpecifier::Identifier:
570 case NestedNameSpecifier::Namespace:
571 case NestedNameSpecifier::NamespaceAlias:
572 SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
575 case NestedNameSpecifier::TypeSpec:
576 case NestedNameSpecifier::TypeSpecWithTemplate: {
577 TypeSourceInfo *TSInfo
578 = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
580 SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
585 case NestedNameSpecifier::Global:
589 // Save the location of the '::'.
590 SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(),
591 Buffer, BufferSize, BufferCapacity);
595 void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
600 Representation = nullptr;
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();
614 NestedNameSpecifierLoc
615 NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
617 return NestedNameSpecifierLoc();
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);
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);