NestedNameSpecifier.h revision c22b5fff39a7520207f165fb16a27a34b944bd9c
1//===--- NestedNameSpecifier.h - 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#ifndef LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H 15#define LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H 16 17#include "clang/Basic/Diagnostic.h" 18#include "llvm/ADT/FoldingSet.h" 19#include "llvm/ADT/PointerIntPair.h" 20 21namespace llvm { 22 class raw_ostream; 23} 24 25namespace clang { 26 27class ASTContext; 28class NamespaceAliasDecl; 29class NamespaceDecl; 30class IdentifierInfo; 31struct PrintingPolicy; 32class Type; 33class TypeLoc; 34class LangOptions; 35 36/// \brief Represents a C++ nested name specifier, such as 37/// "::std::vector<int>::". 38/// 39/// C++ nested name specifiers are the prefixes to qualified 40/// namespaces. For example, "foo::" in "foo::x" is a nested name 41/// specifier. Nested name specifiers are made up of a sequence of 42/// specifiers, each of which can be a namespace, type, identifier 43/// (for dependent names), or the global specifier ('::', must be the 44/// first specifier). 45class NestedNameSpecifier : public llvm::FoldingSetNode { 46 47 /// \brief Enumeration describing 48 enum StoredSpecifierKind { 49 StoredIdentifier = 0, 50 StoredNamespaceOrAlias = 1, 51 StoredTypeSpec = 2, 52 StoredTypeSpecWithTemplate = 3 53 }; 54 55 /// \brief The nested name specifier that precedes this nested name 56 /// specifier. 57 /// 58 /// The pointer is the nested-name-specifier that precedes this 59 /// one. The integer stores one of the first four values of type 60 /// SpecifierKind. 61 llvm::PointerIntPair<NestedNameSpecifier *, 2, StoredSpecifierKind> Prefix; 62 63 /// \brief The last component in the nested name specifier, which 64 /// can be an identifier, a declaration, or a type. 65 /// 66 /// When the pointer is NULL, this specifier represents the global 67 /// specifier '::'. Otherwise, the pointer is one of 68 /// IdentifierInfo*, Namespace*, or Type*, depending on the kind of 69 /// specifier as encoded within the prefix. 70 void* Specifier; 71 72public: 73 /// \brief The kind of specifier that completes this nested name 74 /// specifier. 75 enum SpecifierKind { 76 /// \brief An identifier, stored as an IdentifierInfo*. 77 Identifier, 78 /// \brief A namespace, stored as a NamespaceDecl*. 79 Namespace, 80 /// \brief A namespace alias, stored as a NamespaceAliasDecl*. 81 NamespaceAlias, 82 /// \brief A type, stored as a Type*. 83 TypeSpec, 84 /// \brief A type that was preceded by the 'template' keyword, 85 /// stored as a Type*. 86 TypeSpecWithTemplate, 87 /// \brief The global specifier '::'. There is no stored value. 88 Global 89 }; 90 91private: 92 /// \brief Builds the global specifier. 93 NestedNameSpecifier() : Prefix(0, StoredIdentifier), Specifier(0) { } 94 95 /// \brief Copy constructor used internally to clone nested name 96 /// specifiers. 97 NestedNameSpecifier(const NestedNameSpecifier &Other) 98 : llvm::FoldingSetNode(Other), Prefix(Other.Prefix), 99 Specifier(Other.Specifier) { 100 } 101 102 NestedNameSpecifier &operator=(const NestedNameSpecifier &); // do not implement 103 104 /// \brief Either find or insert the given nested name specifier 105 /// mockup in the given context. 106 static NestedNameSpecifier *FindOrInsert(const ASTContext &Context, 107 const NestedNameSpecifier &Mockup); 108 109public: 110 /// \brief Builds a specifier combining a prefix and an identifier. 111 /// 112 /// The prefix must be dependent, since nested name specifiers 113 /// referencing an identifier are only permitted when the identifier 114 /// cannot be resolved. 115 static NestedNameSpecifier *Create(const ASTContext &Context, 116 NestedNameSpecifier *Prefix, 117 IdentifierInfo *II); 118 119 /// \brief Builds a nested name specifier that names a namespace. 120 static NestedNameSpecifier *Create(const ASTContext &Context, 121 NestedNameSpecifier *Prefix, 122 NamespaceDecl *NS); 123 124 /// \brief Builds a nested name specifier that names a namespace alias. 125 static NestedNameSpecifier *Create(const ASTContext &Context, 126 NestedNameSpecifier *Prefix, 127 NamespaceAliasDecl *Alias); 128 129 /// \brief Builds a nested name specifier that names a type. 130 static NestedNameSpecifier *Create(const ASTContext &Context, 131 NestedNameSpecifier *Prefix, 132 bool Template, const Type *T); 133 134 /// \brief Builds a specifier that consists of just an identifier. 135 /// 136 /// The nested-name-specifier is assumed to be dependent, but has no 137 /// prefix because the prefix is implied by something outside of the 138 /// nested name specifier, e.g., in "x->Base::f", the "x" has a dependent 139 /// type. 140 static NestedNameSpecifier *Create(const ASTContext &Context, 141 IdentifierInfo *II); 142 143 /// \brief Returns the nested name specifier representing the global 144 /// scope. 145 static NestedNameSpecifier *GlobalSpecifier(const ASTContext &Context); 146 147 /// \brief Return the prefix of this nested name specifier. 148 /// 149 /// The prefix contains all of the parts of the nested name 150 /// specifier that preced this current specifier. For example, for a 151 /// nested name specifier that represents "foo::bar::", the current 152 /// specifier will contain "bar::" and the prefix will contain 153 /// "foo::". 154 NestedNameSpecifier *getPrefix() const { return Prefix.getPointer(); } 155 156 /// \brief Determine what kind of nested name specifier is stored. 157 SpecifierKind getKind() const; 158 159 /// \brief Retrieve the identifier stored in this nested name 160 /// specifier. 161 IdentifierInfo *getAsIdentifier() const { 162 if (Prefix.getInt() == StoredIdentifier) 163 return (IdentifierInfo *)Specifier; 164 165 return 0; 166 } 167 168 /// \brief Retrieve the namespace stored in this nested name 169 /// specifier. 170 NamespaceDecl *getAsNamespace() const; 171 172 /// \brief Retrieve the namespace alias stored in this nested name 173 /// specifier. 174 NamespaceAliasDecl *getAsNamespaceAlias() const; 175 176 /// \brief Retrieve the type stored in this nested name specifier. 177 const Type *getAsType() const { 178 if (Prefix.getInt() == StoredTypeSpec || 179 Prefix.getInt() == StoredTypeSpecWithTemplate) 180 return (const Type *)Specifier; 181 182 return 0; 183 } 184 185 /// \brief Whether this nested name specifier refers to a dependent 186 /// type or not. 187 bool isDependent() const; 188 189 /// \brief Whether this nested-name-specifier contains an unexpanded 190 /// parameter pack (for C++0x variadic templates). 191 bool containsUnexpandedParameterPack() const; 192 193 /// \brief Print this nested name specifier to the given output 194 /// stream. 195 void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const; 196 197 void Profile(llvm::FoldingSetNodeID &ID) const { 198 ID.AddPointer(Prefix.getOpaqueValue()); 199 ID.AddPointer(Specifier); 200 } 201 202 /// \brief Dump the nested name specifier to standard output to aid 203 /// in debugging. 204 void dump(const LangOptions &LO); 205}; 206 207/// \brief A C++ nested-name-specifier augmented with source location 208/// information. 209class NestedNameSpecifierLoc { 210 NestedNameSpecifier *Qualifier; 211 void *Data; 212 213 /// \brief Determines the data length for the last component in the 214 /// given nested-name-specifier. 215 static unsigned getLocalDataLength(NestedNameSpecifier *Qualifier); 216 217 /// \brief Determines the data length for the entire 218 /// nested-name-specifier. 219 static unsigned getDataLength(NestedNameSpecifier *Qualifier); 220 221public: 222 /// \brief Construct an empty nested-name-specifier. 223 NestedNameSpecifierLoc() : Qualifier(0), Data(0) { } 224 225 /// \brief Construct a nested-name-specifier with source location information 226 /// from 227 NestedNameSpecifierLoc(NestedNameSpecifier *Qualifier, void *Data) 228 : Qualifier(Qualifier), Data(Data) { } 229 230 /// \brief Evalutes true when this nested-name-specifier location is 231 /// non-empty. 232 operator bool() const { return Qualifier; } 233 234 /// \brief Retrieve the nested-name-specifier to which this instance 235 /// refers. 236 NestedNameSpecifier *getNestedNameSpecifier() const { 237 return Qualifier; 238 } 239 240 /// \brief Retrieve the opaque pointer that refers to source-location data. 241 void *getOpaqueData() const { return Data; } 242 243 /// \brief Retrieve the source range covering the entirety of this 244 /// nested-name-specifier. 245 /// 246 /// For example, if this instance refers to a nested-name-specifier 247 /// \c ::std::vector<int>::, the returned source range would cover 248 /// from the initial '::' to the last '::'. 249 SourceRange getSourceRange() const; 250 251 /// \brief Retrieve the source range covering just the last part of 252 /// this nested-name-specifier, not including the prefix. 253 /// 254 /// For example, if this instance refers to a nested-name-specifier 255 /// \c ::std::vector<int>::, the returned source range would cover 256 /// from "vector" to the last '::'. 257 SourceRange getLocalSourceRange() const; 258 259 /// \brief Retrieve the location of the beginning of this 260 /// nested-name-specifier. 261 SourceLocation getBeginLoc() const { 262 return getSourceRange().getBegin(); 263 } 264 265 /// \brief Retrieve the location of the end of this 266 /// nested-name-specifier. 267 SourceLocation getEndLoc() const { 268 return getSourceRange().getEnd(); 269 } 270 271 /// \brief Retrieve the location of the beginning of this 272 /// component of the nested-name-specifier. 273 SourceLocation getLocalBeginLoc() const { 274 return getLocalSourceRange().getBegin(); 275 } 276 277 /// \brief Retrieve the location of the end of this component of the 278 /// nested-name-specifier. 279 SourceLocation getLocalEndLoc() const { 280 return getLocalSourceRange().getEnd(); 281 } 282 283 /// \brief Return the prefix of this nested-name-specifier. 284 /// 285 /// For example, if this instance refers to a nested-name-specifier 286 /// \c ::std::vector<int>::, the prefix is \c ::std::. Note that the 287 /// returned prefix may be empty, if this is the first component of 288 /// the nested-name-specifier. 289 NestedNameSpecifierLoc getPrefix() const { 290 if (!Qualifier) 291 return *this; 292 293 return NestedNameSpecifierLoc(Qualifier->getPrefix(), Data); 294 } 295 296 /// \brief For a nested-name-specifier that refers to a type, 297 /// retrieve the type with source-location information. 298 TypeLoc getTypeLoc() const; 299 300 /// \brief Determines the data length for the entire 301 /// nested-name-specifier. 302 unsigned getDataLength() const { return getDataLength(Qualifier); } 303}; 304 305/// Insertion operator for diagnostics. This allows sending NestedNameSpecifiers 306/// into a diagnostic with <<. 307inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 308 NestedNameSpecifier *NNS) { 309 DB.AddTaggedVal(reinterpret_cast<intptr_t>(NNS), 310 Diagnostic::ak_nestednamespec); 311 return DB; 312} 313 314} 315 316#endif 317