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 #include "llvm/Support/Compiler.h" 21 22 namespace clang { 23 24 class ASTContext; 25 class NamespaceAliasDecl; 26 class NamespaceDecl; 27 class IdentifierInfo; 28 struct PrintingPolicy; 29 class Type; 30 class TypeLoc; 31 class LangOptions; 32 33 /// \brief Represents a C++ nested name specifier, such as 34 /// "\::std::vector<int>::". 35 /// 36 /// C++ nested name specifiers are the prefixes to qualified 37 /// namespaces. For example, "foo::" in "foo::x" is a nested name 38 /// specifier. Nested name specifiers are made up of a sequence of 39 /// specifiers, each of which can be a namespace, type, identifier 40 /// (for dependent names), decltype specifier, or the global specifier ('::'). 41 /// The last two specifiers can only appear at the start of a 42 /// nested-namespace-specifier. 43 class NestedNameSpecifier : public llvm::FoldingSetNode { 44 45 /// \brief Enumeration describing 46 enum StoredSpecifierKind { 47 StoredIdentifier = 0, 48 StoredNamespaceOrAlias = 1, 49 StoredTypeSpec = 2, 50 StoredTypeSpecWithTemplate = 3 51 }; 52 53 /// \brief The nested name specifier that precedes this nested name 54 /// specifier. 55 /// 56 /// The pointer is the nested-name-specifier that precedes this 57 /// one. The integer stores one of the first four values of type 58 /// SpecifierKind. 59 llvm::PointerIntPair<NestedNameSpecifier *, 2, StoredSpecifierKind> Prefix; 60 61 /// \brief The last component in the nested name specifier, which 62 /// can be an identifier, a declaration, or a type. 63 /// 64 /// When the pointer is NULL, this specifier represents the global 65 /// specifier '::'. Otherwise, the pointer is one of 66 /// IdentifierInfo*, Namespace*, or Type*, depending on the kind of 67 /// specifier as encoded within the prefix. 68 void* Specifier; 69 70 public: 71 /// \brief The kind of specifier that completes this nested name 72 /// specifier. 73 enum SpecifierKind { 74 /// \brief An identifier, stored as an IdentifierInfo*. 75 Identifier, 76 /// \brief A namespace, stored as a NamespaceDecl*. 77 Namespace, 78 /// \brief A namespace alias, stored as a NamespaceAliasDecl*. 79 NamespaceAlias, 80 /// \brief A type, stored as a Type*. 81 TypeSpec, 82 /// \brief A type that was preceded by the 'template' keyword, 83 /// stored as a Type*. 84 TypeSpecWithTemplate, 85 /// \brief The global specifier '::'. There is no stored value. 86 Global 87 }; 88 89 private: 90 /// \brief Builds the global specifier. NestedNameSpecifier()91 NestedNameSpecifier() : Prefix(0, StoredIdentifier), Specifier(0) { } 92 93 /// \brief Copy constructor used internally to clone nested name 94 /// specifiers. NestedNameSpecifier(const NestedNameSpecifier & Other)95 NestedNameSpecifier(const NestedNameSpecifier &Other) 96 : llvm::FoldingSetNode(Other), Prefix(Other.Prefix), 97 Specifier(Other.Specifier) { 98 } 99 100 void operator=(const NestedNameSpecifier &) LLVM_DELETED_FUNCTION; 101 102 /// \brief Either find or insert the given nested name specifier 103 /// mockup in the given context. 104 static NestedNameSpecifier *FindOrInsert(const ASTContext &Context, 105 const NestedNameSpecifier &Mockup); 106 107 public: 108 /// \brief Builds a specifier combining a prefix and an identifier. 109 /// 110 /// The prefix must be dependent, since nested name specifiers 111 /// referencing an identifier are only permitted when the identifier 112 /// cannot be resolved. 113 static NestedNameSpecifier *Create(const ASTContext &Context, 114 NestedNameSpecifier *Prefix, 115 IdentifierInfo *II); 116 117 /// \brief Builds a nested name specifier that names a namespace. 118 static NestedNameSpecifier *Create(const ASTContext &Context, 119 NestedNameSpecifier *Prefix, 120 const NamespaceDecl *NS); 121 122 /// \brief Builds a nested name specifier that names a namespace alias. 123 static NestedNameSpecifier *Create(const ASTContext &Context, 124 NestedNameSpecifier *Prefix, 125 NamespaceAliasDecl *Alias); 126 127 /// \brief Builds a nested name specifier that names a type. 128 static NestedNameSpecifier *Create(const ASTContext &Context, 129 NestedNameSpecifier *Prefix, 130 bool Template, const Type *T); 131 132 /// \brief Builds a specifier that consists of just an identifier. 133 /// 134 /// The nested-name-specifier is assumed to be dependent, but has no 135 /// prefix because the prefix is implied by something outside of the 136 /// nested name specifier, e.g., in "x->Base::f", the "x" has a dependent 137 /// type. 138 static NestedNameSpecifier *Create(const ASTContext &Context, 139 IdentifierInfo *II); 140 141 /// \brief Returns the nested name specifier representing the global 142 /// scope. 143 static NestedNameSpecifier *GlobalSpecifier(const ASTContext &Context); 144 145 /// \brief Return the prefix of this nested name specifier. 146 /// 147 /// The prefix contains all of the parts of the nested name 148 /// specifier that preced this current specifier. For example, for a 149 /// nested name specifier that represents "foo::bar::", the current 150 /// specifier will contain "bar::" and the prefix will contain 151 /// "foo::". getPrefix()152 NestedNameSpecifier *getPrefix() const { return Prefix.getPointer(); } 153 154 /// \brief Determine what kind of nested name specifier is stored. 155 SpecifierKind getKind() const; 156 157 /// \brief Retrieve the identifier stored in this nested name 158 /// specifier. getAsIdentifier()159 IdentifierInfo *getAsIdentifier() const { 160 if (Prefix.getInt() == StoredIdentifier) 161 return (IdentifierInfo *)Specifier; 162 163 return 0; 164 } 165 166 /// \brief Retrieve the namespace stored in this nested name 167 /// specifier. 168 NamespaceDecl *getAsNamespace() const; 169 170 /// \brief Retrieve the namespace alias stored in this nested name 171 /// specifier. 172 NamespaceAliasDecl *getAsNamespaceAlias() const; 173 174 /// \brief Retrieve the type stored in this nested name specifier. getAsType()175 const Type *getAsType() const { 176 if (Prefix.getInt() == StoredTypeSpec || 177 Prefix.getInt() == StoredTypeSpecWithTemplate) 178 return (const Type *)Specifier; 179 180 return 0; 181 } 182 183 /// \brief Whether this nested name specifier refers to a dependent 184 /// type or not. 185 bool isDependent() const; 186 187 /// \brief Whether this nested name specifier involves a template 188 /// parameter. 189 bool isInstantiationDependent() const; 190 191 /// \brief Whether this nested-name-specifier contains an unexpanded 192 /// parameter pack (for C++11 variadic templates). 193 bool containsUnexpandedParameterPack() const; 194 195 /// \brief Print this nested name specifier to the given output 196 /// stream. 197 void print(raw_ostream &OS, const PrintingPolicy &Policy) const; 198 Profile(llvm::FoldingSetNodeID & ID)199 void Profile(llvm::FoldingSetNodeID &ID) const { 200 ID.AddPointer(Prefix.getOpaqueValue()); 201 ID.AddPointer(Specifier); 202 } 203 204 /// \brief Dump the nested name specifier to standard output to aid 205 /// in debugging. 206 void dump(const LangOptions &LO); 207 }; 208 209 /// \brief A C++ nested-name-specifier augmented with source location 210 /// information. 211 class NestedNameSpecifierLoc { 212 NestedNameSpecifier *Qualifier; 213 void *Data; 214 215 /// \brief Determines the data length for the last component in the 216 /// given nested-name-specifier. 217 static unsigned getLocalDataLength(NestedNameSpecifier *Qualifier); 218 219 /// \brief Determines the data length for the entire 220 /// nested-name-specifier. 221 static unsigned getDataLength(NestedNameSpecifier *Qualifier); 222 223 public: 224 /// \brief Construct an empty nested-name-specifier. NestedNameSpecifierLoc()225 NestedNameSpecifierLoc() : Qualifier(0), Data(0) { } 226 227 /// \brief Construct a nested-name-specifier with source location information 228 /// from NestedNameSpecifierLoc(NestedNameSpecifier * Qualifier,void * Data)229 NestedNameSpecifierLoc(NestedNameSpecifier *Qualifier, void *Data) 230 : Qualifier(Qualifier), Data(Data) { } 231 232 /// \brief Evalutes true when this nested-name-specifier location is 233 /// non-empty. 234 operator bool() const { return Qualifier; } 235 236 /// \brief Retrieve the nested-name-specifier to which this instance 237 /// refers. getNestedNameSpecifier()238 NestedNameSpecifier *getNestedNameSpecifier() const { 239 return Qualifier; 240 } 241 242 /// \brief Retrieve the opaque pointer that refers to source-location data. getOpaqueData()243 void *getOpaqueData() const { return Data; } 244 245 /// \brief Retrieve the source range covering the entirety of this 246 /// nested-name-specifier. 247 /// 248 /// For example, if this instance refers to a nested-name-specifier 249 /// \c \::std::vector<int>::, the returned source range would cover 250 /// from the initial '::' to the last '::'. 251 SourceRange getSourceRange() const LLVM_READONLY; 252 253 /// \brief Retrieve the source range covering just the last part of 254 /// this nested-name-specifier, not including the prefix. 255 /// 256 /// For example, if this instance refers to a nested-name-specifier 257 /// \c \::std::vector<int>::, the returned source range would cover 258 /// from "vector" to the last '::'. 259 SourceRange getLocalSourceRange() const; 260 261 /// \brief Retrieve the location of the beginning of this 262 /// nested-name-specifier. getBeginLoc()263 SourceLocation getBeginLoc() const { 264 return getSourceRange().getBegin(); 265 } 266 267 /// \brief Retrieve the location of the end of this 268 /// nested-name-specifier. getEndLoc()269 SourceLocation getEndLoc() const { 270 return getSourceRange().getEnd(); 271 } 272 273 /// \brief Retrieve the location of the beginning of this 274 /// component of the nested-name-specifier. getLocalBeginLoc()275 SourceLocation getLocalBeginLoc() const { 276 return getLocalSourceRange().getBegin(); 277 } 278 279 /// \brief Retrieve the location of the end of this component of the 280 /// nested-name-specifier. getLocalEndLoc()281 SourceLocation getLocalEndLoc() const { 282 return getLocalSourceRange().getEnd(); 283 } 284 285 /// \brief Return the prefix of this nested-name-specifier. 286 /// 287 /// For example, if this instance refers to a nested-name-specifier 288 /// \c \::std::vector<int>::, the prefix is \c \::std::. Note that the 289 /// returned prefix may be empty, if this is the first component of 290 /// the nested-name-specifier. getPrefix()291 NestedNameSpecifierLoc getPrefix() const { 292 if (!Qualifier) 293 return *this; 294 295 return NestedNameSpecifierLoc(Qualifier->getPrefix(), Data); 296 } 297 298 /// \brief For a nested-name-specifier that refers to a type, 299 /// retrieve the type with source-location information. 300 TypeLoc getTypeLoc() const; 301 302 /// \brief Determines the data length for the entire 303 /// nested-name-specifier. getDataLength()304 unsigned getDataLength() const { return getDataLength(Qualifier); } 305 306 friend bool operator==(NestedNameSpecifierLoc X, 307 NestedNameSpecifierLoc Y) { 308 return X.Qualifier == Y.Qualifier && X.Data == Y.Data; 309 } 310 311 friend bool operator!=(NestedNameSpecifierLoc X, 312 NestedNameSpecifierLoc Y) { 313 return !(X == Y); 314 } 315 }; 316 317 /// \brief Class that aids in the construction of nested-name-specifiers along 318 /// with source-location information for all of the components of the 319 /// nested-name-specifier. 320 class NestedNameSpecifierLocBuilder { 321 /// \brief The current representation of the nested-name-specifier we're 322 /// building. 323 NestedNameSpecifier *Representation; 324 325 /// \brief Buffer used to store source-location information for the 326 /// nested-name-specifier. 327 /// 328 /// Note that we explicitly manage the buffer (rather than using a 329 /// SmallVector) because \c Declarator expects it to be possible to memcpy() 330 /// a \c CXXScopeSpec, and CXXScopeSpec uses a NestedNameSpecifierLocBuilder. 331 char *Buffer; 332 333 /// \brief The size of the buffer used to store source-location information 334 /// for the nested-name-specifier. 335 unsigned BufferSize; 336 337 /// \brief The capacity of the buffer used to store source-location 338 /// information for the nested-name-specifier. 339 unsigned BufferCapacity; 340 341 public: NestedNameSpecifierLocBuilder()342 NestedNameSpecifierLocBuilder() 343 : Representation(0), Buffer(0), BufferSize(0), BufferCapacity(0) { } 344 345 NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other); 346 347 NestedNameSpecifierLocBuilder & 348 operator=(const NestedNameSpecifierLocBuilder &Other); 349 ~NestedNameSpecifierLocBuilder()350 ~NestedNameSpecifierLocBuilder() { 351 if (BufferCapacity) 352 free(Buffer); 353 } 354 355 /// \brief Retrieve the representation of the nested-name-specifier. getRepresentation()356 NestedNameSpecifier *getRepresentation() const { return Representation; } 357 358 /// \brief Extend the current nested-name-specifier by another 359 /// nested-name-specifier component of the form 'type::'. 360 /// 361 /// \param Context The AST context in which this nested-name-specifier 362 /// resides. 363 /// 364 /// \param TemplateKWLoc The location of the 'template' keyword, if present. 365 /// 366 /// \param TL The TypeLoc that describes the type preceding the '::'. 367 /// 368 /// \param ColonColonLoc The location of the trailing '::'. 369 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL, 370 SourceLocation ColonColonLoc); 371 372 /// \brief Extend the current nested-name-specifier by another 373 /// nested-name-specifier component of the form 'identifier::'. 374 /// 375 /// \param Context The AST context in which this nested-name-specifier 376 /// resides. 377 /// 378 /// \param Identifier The identifier. 379 /// 380 /// \param IdentifierLoc The location of the identifier. 381 /// 382 /// \param ColonColonLoc The location of the trailing '::'. 383 void Extend(ASTContext &Context, IdentifierInfo *Identifier, 384 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc); 385 386 /// \brief Extend the current nested-name-specifier by another 387 /// nested-name-specifier component of the form 'namespace::'. 388 /// 389 /// \param Context The AST context in which this nested-name-specifier 390 /// resides. 391 /// 392 /// \param Namespace The namespace. 393 /// 394 /// \param NamespaceLoc The location of the namespace name. 395 /// 396 /// \param ColonColonLoc The location of the trailing '::'. 397 void Extend(ASTContext &Context, NamespaceDecl *Namespace, 398 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc); 399 400 /// \brief Extend the current nested-name-specifier by another 401 /// nested-name-specifier component of the form 'namespace-alias::'. 402 /// 403 /// \param Context The AST context in which this nested-name-specifier 404 /// resides. 405 /// 406 /// \param Alias The namespace alias. 407 /// 408 /// \param AliasLoc The location of the namespace alias 409 /// name. 410 /// 411 /// \param ColonColonLoc The location of the trailing '::'. 412 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias, 413 SourceLocation AliasLoc, SourceLocation ColonColonLoc); 414 415 /// \brief Turn this (empty) nested-name-specifier into the global 416 /// nested-name-specifier '::'. 417 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc); 418 419 /// \brief Make a new nested-name-specifier from incomplete source-location 420 /// information. 421 /// 422 /// This routine should be used very, very rarely, in cases where we 423 /// need to synthesize a nested-name-specifier. Most code should instead use 424 /// \c Adopt() with a proper \c NestedNameSpecifierLoc. 425 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier, 426 SourceRange R); 427 428 /// \brief Adopt an existing nested-name-specifier (with source-range 429 /// information). 430 void Adopt(NestedNameSpecifierLoc Other); 431 432 /// \brief Retrieve the source range covered by this nested-name-specifier. getSourceRange()433 SourceRange getSourceRange() const LLVM_READONLY { 434 return NestedNameSpecifierLoc(Representation, Buffer).getSourceRange(); 435 } 436 437 /// \brief Retrieve a nested-name-specifier with location information, 438 /// copied into the given AST context. 439 /// 440 /// \param Context The context into which this nested-name-specifier will be 441 /// copied. 442 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const; 443 444 /// \brief Retrieve a nested-name-specifier with location 445 /// information based on the information in this builder. 446 /// 447 /// This loc will contain references to the builder's internal data and may 448 /// be invalidated by any change to the builder. getTemporary()449 NestedNameSpecifierLoc getTemporary() const { 450 return NestedNameSpecifierLoc(Representation, Buffer); 451 } 452 453 /// \brief Clear out this builder, and prepare it to build another 454 /// nested-name-specifier with source-location information. Clear()455 void Clear() { 456 Representation = 0; 457 BufferSize = 0; 458 } 459 460 /// \brief Retrieve the underlying buffer. 461 /// 462 /// \returns A pair containing a pointer to the buffer of source-location 463 /// data and the size of the source-location data that resides in that 464 /// buffer. getBuffer()465 std::pair<char *, unsigned> getBuffer() const { 466 return std::make_pair(Buffer, BufferSize); 467 } 468 }; 469 470 /// Insertion operator for diagnostics. This allows sending 471 /// NestedNameSpecifiers into a diagnostic with <<. 472 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 473 NestedNameSpecifier *NNS) { 474 DB.AddTaggedVal(reinterpret_cast<intptr_t>(NNS), 475 DiagnosticsEngine::ak_nestednamespec); 476 return DB; 477 } 478 479 } 480 481 #endif 482