123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648 |
- //===--- NestedNameSpecifier.cpp - C++ nested name specifiers -----*- C++ -*-=//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is distributed under the University of Illinois Open Source
- // License. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- //
- // This file defines the NestedNameSpecifier class, which represents
- // a C++ nested-name-specifier.
- //
- //===----------------------------------------------------------------------===//
- #include "clang/AST/NestedNameSpecifier.h"
- #include "clang/AST/ASTContext.h"
- #include "clang/AST/Decl.h"
- #include "clang/AST/DeclCXX.h"
- #include "clang/AST/PrettyPrinter.h"
- #include "clang/AST/Type.h"
- #include "clang/AST/TypeLoc.h"
- #include "clang/Basic/Diagnostic.h"
- #include "llvm/Support/raw_ostream.h"
- #include <cassert>
- using namespace clang;
- NestedNameSpecifier *
- NestedNameSpecifier::FindOrInsert(const ASTContext &Context,
- const NestedNameSpecifier &Mockup) {
- llvm::FoldingSetNodeID ID;
- Mockup.Profile(ID);
- void *InsertPos = 0;
- NestedNameSpecifier *NNS
- = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
- if (!NNS) {
- NNS = new (Context, 4) NestedNameSpecifier(Mockup);
- Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
- }
- return NNS;
- }
- NestedNameSpecifier *
- NestedNameSpecifier::Create(const ASTContext &Context,
- NestedNameSpecifier *Prefix, IdentifierInfo *II) {
- assert(II && "Identifier cannot be NULL");
- assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent");
- NestedNameSpecifier Mockup;
- Mockup.Prefix.setPointer(Prefix);
- Mockup.Prefix.setInt(StoredIdentifier);
- Mockup.Specifier = II;
- return FindOrInsert(Context, Mockup);
- }
- NestedNameSpecifier *
- NestedNameSpecifier::Create(const ASTContext &Context,
- NestedNameSpecifier *Prefix, NamespaceDecl *NS) {
- assert(NS && "Namespace cannot be NULL");
- assert((!Prefix ||
- (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) &&
- "Broken nested name specifier");
- NestedNameSpecifier Mockup;
- Mockup.Prefix.setPointer(Prefix);
- Mockup.Prefix.setInt(StoredNamespaceOrAlias);
- Mockup.Specifier = NS;
- return FindOrInsert(Context, Mockup);
- }
- NestedNameSpecifier *
- NestedNameSpecifier::Create(const ASTContext &Context,
- NestedNameSpecifier *Prefix,
- NamespaceAliasDecl *Alias) {
- assert(Alias && "Namespace alias cannot be NULL");
- assert((!Prefix ||
- (Prefix->getAsType() == 0 && Prefix->getAsIdentifier() == 0)) &&
- "Broken nested name specifier");
- NestedNameSpecifier Mockup;
- Mockup.Prefix.setPointer(Prefix);
- Mockup.Prefix.setInt(StoredNamespaceOrAlias);
- Mockup.Specifier = Alias;
- return FindOrInsert(Context, Mockup);
- }
- NestedNameSpecifier *
- NestedNameSpecifier::Create(const ASTContext &Context,
- NestedNameSpecifier *Prefix,
- bool Template, const Type *T) {
- assert(T && "Type cannot be NULL");
- NestedNameSpecifier Mockup;
- Mockup.Prefix.setPointer(Prefix);
- Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec);
- Mockup.Specifier = const_cast<Type*>(T);
- return FindOrInsert(Context, Mockup);
- }
- NestedNameSpecifier *
- NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) {
- assert(II && "Identifier cannot be NULL");
- NestedNameSpecifier Mockup;
- Mockup.Prefix.setPointer(0);
- Mockup.Prefix.setInt(StoredIdentifier);
- Mockup.Specifier = II;
- return FindOrInsert(Context, Mockup);
- }
- NestedNameSpecifier *
- NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
- if (!Context.GlobalNestedNameSpecifier)
- Context.GlobalNestedNameSpecifier = new (Context, 4) NestedNameSpecifier();
- return Context.GlobalNestedNameSpecifier;
- }
- NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const {
- if (Specifier == 0)
- return Global;
- switch (Prefix.getInt()) {
- case StoredIdentifier:
- return Identifier;
- case StoredNamespaceOrAlias:
- return isa<NamespaceDecl>(static_cast<NamedDecl *>(Specifier))? Namespace
- : NamespaceAlias;
- case StoredTypeSpec:
- return TypeSpec;
- case StoredTypeSpecWithTemplate:
- return TypeSpecWithTemplate;
- }
- llvm_unreachable("Invalid NNS Kind!");
- }
- /// \brief Retrieve the namespace stored in this nested name
- /// specifier.
- NamespaceDecl *NestedNameSpecifier::getAsNamespace() const {
- if (Prefix.getInt() == StoredNamespaceOrAlias)
- return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier));
- return 0;
- }
- /// \brief Retrieve the namespace alias stored in this nested name
- /// specifier.
- NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const {
- if (Prefix.getInt() == StoredNamespaceOrAlias)
- return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier));
- return 0;
- }
- /// \brief Whether this nested name specifier refers to a dependent
- /// type or not.
- bool NestedNameSpecifier::isDependent() const {
- switch (getKind()) {
- case Identifier:
- // Identifier specifiers always represent dependent types
- return true;
- case Namespace:
- case NamespaceAlias:
- case Global:
- return false;
- case TypeSpec:
- case TypeSpecWithTemplate:
- return getAsType()->isDependentType();
- }
- llvm_unreachable("Invalid NNS Kind!");
- }
- /// \brief Whether this nested name specifier refers to a dependent
- /// type or not.
- bool NestedNameSpecifier::isInstantiationDependent() const {
- switch (getKind()) {
- case Identifier:
- // Identifier specifiers always represent dependent types
- return true;
-
- case Namespace:
- case NamespaceAlias:
- case Global:
- return false;
-
- case TypeSpec:
- case TypeSpecWithTemplate:
- return getAsType()->isInstantiationDependentType();
- }
- llvm_unreachable("Invalid NNS Kind!");
- }
- bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
- switch (getKind()) {
- case Identifier:
- return getPrefix() && getPrefix()->containsUnexpandedParameterPack();
- case Namespace:
- case NamespaceAlias:
- case Global:
- return false;
- case TypeSpec:
- case TypeSpecWithTemplate:
- return getAsType()->containsUnexpandedParameterPack();
- }
- llvm_unreachable("Invalid NNS Kind!");
- }
- /// \brief Print this nested name specifier to the given output
- /// stream.
- void
- NestedNameSpecifier::print(raw_ostream &OS,
- const PrintingPolicy &Policy) const {
- if (getPrefix())
- getPrefix()->print(OS, Policy);
- switch (getKind()) {
- case Identifier:
- OS << getAsIdentifier()->getName();
- break;
- case Namespace:
- if (getAsNamespace()->isAnonymousNamespace())
- return;
-
- OS << getAsNamespace()->getName();
- break;
- case NamespaceAlias:
- OS << getAsNamespaceAlias()->getName();
- break;
- case Global:
- break;
- case TypeSpecWithTemplate:
- OS << "template ";
- // Fall through to print the type.
- case TypeSpec: {
- std::string TypeStr;
- const Type *T = getAsType();
- PrintingPolicy InnerPolicy(Policy);
- InnerPolicy.SuppressScope = true;
- // Nested-name-specifiers are intended to contain minimally-qualified
- // types. An actual ElaboratedType will not occur, since we'll store
- // just the type that is referred to in the nested-name-specifier (e.g.,
- // a TypedefType, TagType, etc.). However, when we are dealing with
- // dependent template-id types (e.g., Outer<T>::template Inner<U>),
- // the type requires its own nested-name-specifier for uniqueness, so we
- // suppress that nested-name-specifier during printing.
- assert(!isa<ElaboratedType>(T) &&
- "Elaborated type in nested-name-specifier");
- if (const TemplateSpecializationType *SpecType
- = dyn_cast<TemplateSpecializationType>(T)) {
- // Print the template name without its corresponding
- // nested-name-specifier.
- SpecType->getTemplateName().print(OS, InnerPolicy, true);
- // Print the template argument list.
- TypeStr = TemplateSpecializationType::PrintTemplateArgumentList(
- SpecType->getArgs(),
- SpecType->getNumArgs(),
- InnerPolicy);
- } else {
- // Print the type normally
- TypeStr = QualType(T, 0).getAsString(InnerPolicy);
- }
- OS << TypeStr;
- break;
- }
- }
- OS << "::";
- }
- void NestedNameSpecifier::dump(const LangOptions &LO) {
- print(llvm::errs(), PrintingPolicy(LO));
- }
- unsigned
- NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
- assert(Qualifier && "Expected a non-NULL qualifier");
- // Location of the trailing '::'.
- unsigned Length = sizeof(unsigned);
- switch (Qualifier->getKind()) {
- case NestedNameSpecifier::Global:
- // Nothing more to add.
- break;
- case NestedNameSpecifier::Identifier:
- case NestedNameSpecifier::Namespace:
- case NestedNameSpecifier::NamespaceAlias:
- // The location of the identifier or namespace name.
- Length += sizeof(unsigned);
- break;
- case NestedNameSpecifier::TypeSpecWithTemplate:
- case NestedNameSpecifier::TypeSpec:
- // The "void*" that points at the TypeLoc data.
- // Note: the 'template' keyword is part of the TypeLoc.
- Length += sizeof(void *);
- break;
- }
- return Length;
- }
- unsigned
- NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
- unsigned Length = 0;
- for (; Qualifier; Qualifier = Qualifier->getPrefix())
- Length += getLocalDataLength(Qualifier);
- return Length;
- }
- namespace {
- /// \brief Load a (possibly unaligned) source location from a given address
- /// and offset.
- SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
- unsigned Raw;
- memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned));
- return SourceLocation::getFromRawEncoding(Raw);
- }
-
- /// \brief Load a (possibly unaligned) pointer from a given address and
- /// offset.
- void *LoadPointer(void *Data, unsigned Offset) {
- void *Result;
- memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
- return Result;
- }
- }
- SourceRange NestedNameSpecifierLoc::getSourceRange() const {
- if (!Qualifier)
- return SourceRange();
-
- NestedNameSpecifierLoc First = *this;
- while (NestedNameSpecifierLoc Prefix = First.getPrefix())
- First = Prefix;
-
- return SourceRange(First.getLocalSourceRange().getBegin(),
- getLocalSourceRange().getEnd());
- }
- SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
- if (!Qualifier)
- return SourceRange();
-
- unsigned Offset = getDataLength(Qualifier->getPrefix());
- switch (Qualifier->getKind()) {
- case NestedNameSpecifier::Global:
- return LoadSourceLocation(Data, Offset);
- case NestedNameSpecifier::Identifier:
- case NestedNameSpecifier::Namespace:
- case NestedNameSpecifier::NamespaceAlias:
- return SourceRange(LoadSourceLocation(Data, Offset),
- LoadSourceLocation(Data, Offset + sizeof(unsigned)));
- case NestedNameSpecifier::TypeSpecWithTemplate:
- case NestedNameSpecifier::TypeSpec: {
- // The "void*" that points at the TypeLoc data.
- // Note: the 'template' keyword is part of the TypeLoc.
- void *TypeData = LoadPointer(Data, Offset);
- TypeLoc TL(Qualifier->getAsType(), TypeData);
- return SourceRange(TL.getBeginLoc(),
- LoadSourceLocation(Data, Offset + sizeof(void*)));
- }
- }
- llvm_unreachable("Invalid NNS Kind!");
- }
- TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
- assert((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||
- Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) &&
- "Nested-name-specifier location is not a type");
- // The "void*" that points at the TypeLoc data.
- unsigned Offset = getDataLength(Qualifier->getPrefix());
- void *TypeData = LoadPointer(Data, Offset);
- return TypeLoc(Qualifier->getAsType(), TypeData);
- }
- namespace {
- void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
- unsigned &BufferCapacity) {
- if (BufferSize + (End - Start) > BufferCapacity) {
- // Reallocate the buffer.
- unsigned NewCapacity
- = std::max((unsigned)(BufferCapacity? BufferCapacity * 2
- : sizeof(void*) * 2),
- (unsigned)(BufferSize + (End - Start)));
- char *NewBuffer = static_cast<char *>(malloc(NewCapacity));
- memcpy(NewBuffer, Buffer, BufferSize);
-
- if (BufferCapacity)
- free(Buffer);
- Buffer = NewBuffer;
- BufferCapacity = NewCapacity;
- }
-
- memcpy(Buffer + BufferSize, Start, End - Start);
- BufferSize += End-Start;
- }
-
- /// \brief Save a source location to the given buffer.
- void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
- unsigned &BufferSize, unsigned &BufferCapacity) {
- unsigned Raw = Loc.getRawEncoding();
- Append(reinterpret_cast<char *>(&Raw),
- reinterpret_cast<char *>(&Raw) + sizeof(unsigned),
- Buffer, BufferSize, BufferCapacity);
- }
-
- /// \brief Save a pointer to the given buffer.
- void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
- unsigned &BufferCapacity) {
- Append(reinterpret_cast<char *>(&Ptr),
- reinterpret_cast<char *>(&Ptr) + sizeof(void *),
- Buffer, BufferSize, BufferCapacity);
- }
- }
- NestedNameSpecifierLocBuilder::NestedNameSpecifierLocBuilder()
- : Representation(0), Buffer(0), BufferSize(0), BufferCapacity(0) { }
- NestedNameSpecifierLocBuilder::
- NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
- : Representation(Other.Representation), Buffer(0),
- BufferSize(0), BufferCapacity(0)
- {
- if (!Other.Buffer)
- return;
-
- if (Other.BufferCapacity == 0) {
- // Shallow copy is okay.
- Buffer = Other.Buffer;
- BufferSize = Other.BufferSize;
- return;
- }
-
- // Deep copy
- BufferSize = Other.BufferSize;
- BufferCapacity = Other.BufferSize;
- Buffer = static_cast<char *>(malloc(BufferCapacity));
- memcpy(Buffer, Other.Buffer, BufferSize);
- }
- NestedNameSpecifierLocBuilder &
- NestedNameSpecifierLocBuilder::
- operator=(const NestedNameSpecifierLocBuilder &Other) {
- Representation = Other.Representation;
-
- if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) {
- // Re-use our storage.
- BufferSize = Other.BufferSize;
- memcpy(Buffer, Other.Buffer, BufferSize);
- return *this;
- }
-
- // Free our storage, if we have any.
- if (BufferCapacity) {
- free(Buffer);
- BufferCapacity = 0;
- }
-
- if (!Other.Buffer) {
- // Empty.
- Buffer = 0;
- BufferSize = 0;
- return *this;
- }
-
- if (Other.BufferCapacity == 0) {
- // Shallow copy is okay.
- Buffer = Other.Buffer;
- BufferSize = Other.BufferSize;
- return *this;
- }
-
- // Deep copy.
- BufferSize = Other.BufferSize;
- BufferCapacity = BufferSize;
- Buffer = static_cast<char *>(malloc(BufferSize));
- memcpy(Buffer, Other.Buffer, BufferSize);
- return *this;
- }
- NestedNameSpecifierLocBuilder::~NestedNameSpecifierLocBuilder() {
- if (BufferCapacity)
- free(Buffer);
- }
- void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
- SourceLocation TemplateKWLoc,
- TypeLoc TL,
- SourceLocation ColonColonLoc) {
- Representation = NestedNameSpecifier::Create(Context, Representation,
- TemplateKWLoc.isValid(),
- TL.getTypePtr());
-
- // Push source-location info into the buffer.
- SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
- SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
- }
- void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
- IdentifierInfo *Identifier,
- SourceLocation IdentifierLoc,
- SourceLocation ColonColonLoc) {
- Representation = NestedNameSpecifier::Create(Context, Representation,
- Identifier);
-
- // Push source-location info into the buffer.
- SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
- SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
- }
- void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
- NamespaceDecl *Namespace,
- SourceLocation NamespaceLoc,
- SourceLocation ColonColonLoc) {
- Representation = NestedNameSpecifier::Create(Context, Representation,
- Namespace);
-
- // Push source-location info into the buffer.
- SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
- SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
- }
- void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
- NamespaceAliasDecl *Alias,
- SourceLocation AliasLoc,
- SourceLocation ColonColonLoc) {
- Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
-
- // Push source-location info into the buffer.
- SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
- SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
- }
- void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
- SourceLocation ColonColonLoc) {
- assert(!Representation && "Already have a nested-name-specifier!?");
- Representation = NestedNameSpecifier::GlobalSpecifier(Context);
-
- // Push source-location info into the buffer.
- SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
- }
- void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
- NestedNameSpecifier *Qualifier,
- SourceRange R) {
- Representation = Qualifier;
-
- // Construct bogus (but well-formed) source information for the
- // nested-name-specifier.
- BufferSize = 0;
- SmallVector<NestedNameSpecifier *, 4> Stack;
- for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix())
- Stack.push_back(NNS);
- while (!Stack.empty()) {
- NestedNameSpecifier *NNS = Stack.back();
- Stack.pop_back();
- switch (NNS->getKind()) {
- case NestedNameSpecifier::Identifier:
- case NestedNameSpecifier::Namespace:
- case NestedNameSpecifier::NamespaceAlias:
- SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
- break;
-
- case NestedNameSpecifier::TypeSpec:
- case NestedNameSpecifier::TypeSpecWithTemplate: {
- TypeSourceInfo *TSInfo
- = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
- R.getBegin());
- SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
- BufferCapacity);
- break;
- }
-
- case NestedNameSpecifier::Global:
- break;
- }
-
- // Save the location of the '::'.
- SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(),
- Buffer, BufferSize, BufferCapacity);
- }
- }
- void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
- if (BufferCapacity)
- free(Buffer);
- if (!Other) {
- Representation = 0;
- BufferSize = 0;
- return;
- }
-
- // Rather than copying the data (which is wasteful), "adopt" the
- // pointer (which points into the ASTContext) but set the capacity to zero to
- // indicate that we don't own it.
- Representation = Other.getNestedNameSpecifier();
- Buffer = static_cast<char *>(Other.getOpaqueData());
- BufferSize = Other.getDataLength();
- BufferCapacity = 0;
- }
- NestedNameSpecifierLoc
- NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
- if (!Representation)
- return NestedNameSpecifierLoc();
-
- // If we adopted our data pointer from elsewhere in the AST context, there's
- // no need to copy the memory.
- if (BufferCapacity == 0)
- return NestedNameSpecifierLoc(Representation, Buffer);
-
- // FIXME: After copying the source-location information, should we free
- // our (temporary) buffer and adopt the ASTContext-allocated memory?
- // Doing so would optimize repeated calls to getWithLocInContext().
- void *Mem = Context.Allocate(BufferSize, llvm::alignOf<void *>());
- memcpy(Mem, Buffer, BufferSize);
- return NestedNameSpecifierLoc(Representation, Mem);
- }
- const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
- NestedNameSpecifier *NNS) {
- DB.AddTaggedVal(reinterpret_cast<intptr_t>(NNS),
- DiagnosticsEngine::ak_nestednamespec);
- return DB;
- }
|