DeclBase.h revision b8547e800f9553a72c2f3635ae2371692bd4411a
1//===-- DeclBase.h - Base Classes for representing declarations *- 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 Decl and DeclContext interfaces. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_DECLBASE_H 15#define LLVM_CLANG_AST_DECLBASE_H 16 17#include "clang/AST/Attr.h" 18#include "clang/AST/Type.h" 19// FIXME: Layering violation 20#include "clang/Parse/AccessSpecifier.h" 21#include "llvm/Support/PrettyStackTrace.h" 22 23namespace clang { 24class DeclContext; 25class TranslationUnitDecl; 26class NamespaceDecl; 27class UsingDirectiveDecl; 28class NamedDecl; 29class FunctionDecl; 30class CXXRecordDecl; 31class EnumDecl; 32class ObjCMethodDecl; 33class ObjCContainerDecl; 34class ObjCInterfaceDecl; 35class ObjCCategoryDecl; 36class ObjCProtocolDecl; 37class ObjCImplementationDecl; 38class ObjCCategoryImplDecl; 39class LinkageSpecDecl; 40class BlockDecl; 41class DeclarationName; 42class CompoundStmt; 43 44/// Decl - This represents one declaration (or definition), e.g. a variable, 45/// typedef, function, struct, etc. 46/// 47class Decl { 48public: 49 /// \brief Lists the kind of concrete classes of Decl. 50 enum Kind { 51#define DECL(Derived, Base) Derived, 52#define DECL_RANGE(CommonBase, Start, End) \ 53 CommonBase##First = Start, CommonBase##Last = End, 54#define LAST_DECL_RANGE(CommonBase, Start, End) \ 55 CommonBase##First = Start, CommonBase##Last = End 56#include "clang/AST/DeclNodes.def" 57 }; 58 59 /// IdentifierNamespace - According to C99 6.2.3, there are four namespaces, 60 /// labels, tags, members and ordinary identifiers. These are meant 61 /// as bitmasks, so that searches in C++ can look into the "tag" namespace 62 /// during ordinary lookup. 63 enum IdentifierNamespace { 64 IDNS_Label = 0x1, 65 IDNS_Tag = 0x2, 66 IDNS_Member = 0x4, 67 IDNS_Ordinary = 0x8, 68 IDNS_Protocol = 0x10 69 }; 70 71 /// ObjCDeclQualifier - Qualifier used on types in method declarations 72 /// for remote messaging. They are meant for the arguments though and 73 /// applied to the Decls (ObjCMethodDecl and ParmVarDecl). 74 enum ObjCDeclQualifier { 75 OBJC_TQ_None = 0x0, 76 OBJC_TQ_In = 0x1, 77 OBJC_TQ_Inout = 0x2, 78 OBJC_TQ_Out = 0x4, 79 OBJC_TQ_Bycopy = 0x8, 80 OBJC_TQ_Byref = 0x10, 81 OBJC_TQ_Oneway = 0x20 82 }; 83 84private: 85 /// NextDeclarator - If this decl was part of a multi-declarator declaration, 86 /// such as "int X, Y, *Z;" this indicates Decl for the next declarator. 87 Decl *NextDeclarator; 88 89 /// NextDeclInScope - The next declaration within the same lexical 90 /// DeclContext. These pointers form the linked list that is 91 /// traversed via DeclContext's decls_begin()/decls_end(). 92 /// FIXME: If NextDeclarator is non-NULL, will it always be the same 93 /// as NextDeclInScope? If so, we can use a 94 /// PointerIntPair<Decl*, 1> to make Decl smaller. 95 Decl *NextDeclInScope; 96 97 friend class DeclContext; 98 99 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*. 100 /// For declarations that don't contain C++ scope specifiers, it contains 101 /// the DeclContext where the Decl was declared. 102 /// For declarations with C++ scope specifiers, it contains a MultipleDC* 103 /// with the context where it semantically belongs (SemanticDC) and the 104 /// context where it was lexically declared (LexicalDC). 105 /// e.g.: 106 /// 107 /// namespace A { 108 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 109 /// } 110 /// void A::f(); // SemanticDC == namespace 'A' 111 /// // LexicalDC == global namespace 112 uintptr_t DeclCtx; 113 114 struct MultipleDC { 115 DeclContext *SemanticDC; 116 DeclContext *LexicalDC; 117 }; 118 119 inline bool isInSemaDC() const { return (DeclCtx & 0x1) == 0; } 120 inline bool isOutOfSemaDC() const { return (DeclCtx & 0x1) != 0; } 121 inline MultipleDC *getMultipleDC() const { 122 return reinterpret_cast<MultipleDC*>(DeclCtx & ~0x1); 123 } 124 125 /// Loc - The location that this decl. 126 SourceLocation Loc; 127 128 /// DeclKind - This indicates which class this is. 129 Kind DeclKind : 8; 130 131 /// InvalidDecl - This indicates a semantic error occurred. 132 unsigned int InvalidDecl : 1; 133 134 /// HasAttrs - This indicates whether the decl has attributes or not. 135 unsigned int HasAttrs : 1; 136 137 /// Implicit - Whether this declaration was implicitly generated by 138 /// the implementation rather than explicitly written by the user. 139 bool Implicit : 1; 140 141protected: 142 /// Access - Used by C++ decls for the access specifier. 143 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum 144 unsigned Access : 2; 145 friend class CXXClassMemberWrapper; 146 147 Decl(Kind DK, DeclContext *DC, SourceLocation L) 148 : NextDeclarator(0), NextDeclInScope(0), 149 DeclCtx(reinterpret_cast<uintptr_t>(DC)), 150 Loc(L), DeclKind(DK), InvalidDecl(0), 151 HasAttrs(false), Implicit(false) { 152 if (Decl::CollectingStats()) addDeclKind(DK); 153 } 154 155 virtual ~Decl(); 156 157 /// setDeclContext - Set both the semantic and lexical DeclContext 158 /// to DC. 159 void setDeclContext(DeclContext *DC); 160 161public: 162 SourceLocation getLocation() const { return Loc; } 163 void setLocation(SourceLocation L) { Loc = L; } 164 165 Kind getKind() const { return DeclKind; } 166 const char *getDeclKindName() const; 167 168 const DeclContext *getDeclContext() const { 169 if (isInSemaDC()) 170 return reinterpret_cast<DeclContext*>(DeclCtx); 171 return getMultipleDC()->SemanticDC; 172 } 173 DeclContext *getDeclContext() { 174 return const_cast<DeclContext*>( 175 const_cast<const Decl*>(this)->getDeclContext()); 176 } 177 178 void setAccess(AccessSpecifier AS) { 179 assert(AS != AS_none && "Can't set access to none"); 180 Access = AS; 181 } 182 AccessSpecifier getAccess() const { return AccessSpecifier(Access); } 183 184 bool hasAttrs() const { return HasAttrs; } 185 void addAttr(Attr *attr); 186 const Attr *getAttrs() const { 187 if (!HasAttrs) return 0; // common case, no attributes. 188 return getAttrsImpl(); // Uncommon case, out of line hash lookup. 189 } 190 void swapAttrs(Decl *D); 191 void invalidateAttrs(); 192 193 template<typename T> const T *getAttr() const { 194 for (const Attr *attr = getAttrs(); attr; attr = attr->getNext()) 195 if (const T *V = dyn_cast<T>(attr)) 196 return V; 197 return 0; 198 } 199 200 /// setInvalidDecl - Indicates the Decl had a semantic error. This 201 /// allows for graceful error recovery. 202 void setInvalidDecl() { InvalidDecl = 1; } 203 bool isInvalidDecl() const { return (bool) InvalidDecl; } 204 205 /// isImplicit - Indicates whether the declaration was implicitly 206 /// generated by the implementation. If false, this declaration 207 /// was written explicitly in the source code. 208 bool isImplicit() const { return Implicit; } 209 void setImplicit(bool I = true) { Implicit = I; } 210 211 unsigned getIdentifierNamespace() const { 212 switch (DeclKind) { 213 default: 214 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast) 215 return IDNS_Ordinary; 216 assert(0 && "Unknown decl kind!"); 217 case OverloadedFunction: 218 case Typedef: 219 case EnumConstant: 220 case Var: 221 case ImplicitParam: 222 case ParmVar: 223 case OriginalParmVar: 224 case NonTypeTemplateParm: 225 case ObjCMethod: 226 case ObjCContainer: 227 case ObjCCategory: 228 case ObjCInterface: 229 case ObjCCategoryImpl: 230 case ObjCProperty: 231 case ObjCCompatibleAlias: 232 return IDNS_Ordinary; 233 234 case ObjCProtocol: 235 return IDNS_Protocol; 236 237 case Field: 238 case ObjCAtDefsField: 239 case ObjCIvar: 240 return IDNS_Member; 241 242 case Record: 243 case CXXRecord: 244 case Enum: 245 case TemplateTypeParm: 246 return IDNS_Tag; 247 248 case Namespace: 249 case Template: 250 case FunctionTemplate: 251 case ClassTemplate: 252 case TemplateTemplateParm: 253 return IDNS_Tag | IDNS_Ordinary; 254 255 case ClassTemplateSpecialization: 256 return 0; 257 } 258 } 259 260 bool isInIdentifierNamespace(unsigned NS) const { 261 return getIdentifierNamespace() & NS; 262 } 263 264 /// getLexicalDeclContext - The declaration context where this Decl was 265 /// lexically declared (LexicalDC). May be different from 266 /// getDeclContext() (SemanticDC). 267 /// e.g.: 268 /// 269 /// namespace A { 270 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 271 /// } 272 /// void A::f(); // SemanticDC == namespace 'A' 273 /// // LexicalDC == global namespace 274 const DeclContext *getLexicalDeclContext() const { 275 if (isInSemaDC()) 276 return reinterpret_cast<DeclContext*>(DeclCtx); 277 return getMultipleDC()->LexicalDC; 278 } 279 DeclContext *getLexicalDeclContext() { 280 return const_cast<DeclContext*>( 281 const_cast<const Decl*>(this)->getLexicalDeclContext()); 282 } 283 284 void setLexicalDeclContext(DeclContext *DC); 285 286 /// getNextDeclarator - If this decl was part of a multi-declarator 287 /// declaration, such as "int X, Y, *Z;" this returns the decl for the next 288 /// declarator. Otherwise it returns null. 289 Decl *getNextDeclarator() { return NextDeclarator; } 290 const Decl *getNextDeclarator() const { return NextDeclarator; } 291 void setNextDeclarator(Decl *N) { NextDeclarator = N; } 292 293 // isDefinedOutsideFunctionOrMethod - This predicate returns true if this 294 // scoped decl is defined outside the current function or method. This is 295 // roughly global variables and functions, but also handles enums (which could 296 // be defined inside or outside a function etc). 297 bool isDefinedOutsideFunctionOrMethod() const; 298 299 // getBody - If this Decl represents a declaration for a body of code, 300 // such as a function or method definition, this method returns the top-level 301 // Stmt* of that body. Otherwise this method returns null. 302 virtual CompoundStmt* getBody() const { return 0; } 303 304 // global temp stats (until we have a per-module visitor) 305 static void addDeclKind(Kind k); 306 static bool CollectingStats(bool Enable = false); 307 static void PrintStats(); 308 309 /// isTemplateParameter - Determines whether this declartion is a 310 /// template parameter. 311 bool isTemplateParameter() const; 312 313 // Implement isa/cast/dyncast/etc. 314 static bool classof(const Decl *) { return true; } 315 static DeclContext *castToDeclContext(const Decl *); 316 static Decl *castFromDeclContext(const DeclContext *); 317 318 /// Emit - Serialize this Decl to Bitcode. 319 void Emit(llvm::Serializer& S) const; 320 321 /// Create - Deserialize a Decl from Bitcode. 322 static Decl* Create(llvm::Deserializer& D, ASTContext& C); 323 324 /// Destroy - Call destructors and release memory. 325 virtual void Destroy(ASTContext& C); 326 327protected: 328 /// EmitImpl - Provides the subclass-specific serialization logic for 329 /// serializing out a decl. 330 virtual void EmitImpl(llvm::Serializer& S) const { 331 // FIXME: This will eventually be a pure virtual function. 332 assert (false && "Not implemented."); 333 } 334private: 335 const Attr *getAttrsImpl() const; 336 337}; 338 339/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when 340/// doing something to a specific decl. 341class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry { 342 Decl *TheDecl; 343 SourceLocation Loc; 344 SourceManager &SM; 345 const char *Message; 346public: 347 PrettyStackTraceDecl(Decl *theDecl, SourceLocation L, 348 SourceManager &sm, const char *Msg) 349 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {} 350 351 virtual void print(llvm::raw_ostream &OS) const; 352}; 353 354 355/// DeclContext - This is used only as base class of specific decl types that 356/// can act as declaration contexts. These decls are (only the top classes 357/// that directly derive from DeclContext are mentioned, not their subclasses): 358/// 359/// TranslationUnitDecl 360/// NamespaceDecl 361/// FunctionDecl 362/// TagDecl 363/// ObjCMethodDecl 364/// ObjCContainerDecl 365/// ObjCCategoryImplDecl 366/// ObjCImplementationDecl 367/// LinkageSpecDecl 368/// BlockDecl 369/// 370class DeclContext { 371 /// DeclKind - This indicates which class this is. 372 Decl::Kind DeclKind : 8; 373 374 /// LookupPtrKind - Describes what kind of pointer LookupPtr 375 /// actually is. 376 enum LookupPtrKind { 377 /// LookupIsMap - Indicates that LookupPtr is actually a map. 378 LookupIsMap = 7 379 }; 380 381 /// LookupPtr - Pointer to a data structure used to lookup 382 /// declarations within this context. If the context contains fewer 383 /// than seven declarations, the number of declarations is provided 384 /// in the 3 lowest-order bits and the upper bits are treated as a 385 /// pointer to an array of NamedDecl pointers. If the context 386 /// contains seven or more declarations, the upper bits are treated 387 /// as a pointer to a DenseMap<DeclarationName, std::vector<NamedDecl*>>. 388 /// FIXME: We need a better data structure for this. 389 llvm::PointerIntPair<void*, 3> LookupPtr; 390 391 /// FirstDecl - The first declaration stored within this declaration 392 /// context. 393 Decl *FirstDecl; 394 395 /// LastDecl - The last declaration stored within this declaration 396 /// context. FIXME: We could probably cache this value somewhere 397 /// outside of the DeclContext, to reduce the size of DeclContext by 398 /// another pointer. 399 Decl *LastDecl; 400 401 /// isLookupMap - Determine if the lookup structure is a 402 /// DenseMap. Othewise, it is an array. 403 bool isLookupMap() const { return LookupPtr.getInt() == LookupIsMap; } 404 405 static Decl *getNextDeclInScope(Decl *D) { return D->NextDeclInScope; } 406 407protected: 408 DeclContext(Decl::Kind K) 409 : DeclKind(K), LookupPtr(), FirstDecl(0), LastDecl(0) { } 410 411 void DestroyDecls(ASTContext &C); 412 413public: 414 ~DeclContext(); 415 416 Decl::Kind getDeclKind() const { 417 return DeclKind; 418 } 419 const char *getDeclKindName() const; 420 421 /// getParent - Returns the containing DeclContext. 422 const DeclContext *getParent() const { 423 return cast<Decl>(this)->getDeclContext(); 424 } 425 DeclContext *getParent() { 426 return const_cast<DeclContext*>( 427 const_cast<const DeclContext*>(this)->getParent()); 428 } 429 430 /// getLexicalParent - Returns the containing lexical DeclContext. May be 431 /// different from getParent, e.g.: 432 /// 433 /// namespace A { 434 /// struct S; 435 /// } 436 /// struct A::S {}; // getParent() == namespace 'A' 437 /// // getLexicalParent() == translation unit 438 /// 439 const DeclContext *getLexicalParent() const { 440 return cast<Decl>(this)->getLexicalDeclContext(); 441 } 442 DeclContext *getLexicalParent() { 443 return const_cast<DeclContext*>( 444 const_cast<const DeclContext*>(this)->getLexicalParent()); 445 } 446 447 bool isFunctionOrMethod() const { 448 switch (DeclKind) { 449 case Decl::Block: 450 case Decl::ObjCMethod: 451 return true; 452 453 default: 454 if (DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast) 455 return true; 456 return false; 457 } 458 } 459 460 bool isFileContext() const { 461 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; 462 } 463 464 bool isTranslationUnit() const { 465 return DeclKind == Decl::TranslationUnit; 466 } 467 468 bool isRecord() const { 469 return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast; 470 } 471 472 bool isNamespace() const { 473 return DeclKind == Decl::Namespace; 474 } 475 476 /// isTransparentContext - Determines whether this context is a 477 /// "transparent" context, meaning that the members declared in this 478 /// context are semantically declared in the nearest enclosing 479 /// non-transparent (opaque) context but are lexically declared in 480 /// this context. For example, consider the enumerators of an 481 /// enumeration type: 482 /// @code 483 /// enum E { 484 /// Val1 485 /// }; 486 /// @endcode 487 /// Here, E is a transparent context, so its enumerator (Val1) will 488 /// appear (semantically) that it is in the same context of E. 489 /// Examples of transparent contexts include: enumerations (except for 490 /// C++0x scoped enums), C++ linkage specifications, and C++0x 491 /// inline namespaces. 492 bool isTransparentContext() const; 493 494 bool Encloses(DeclContext *DC) const { 495 for (; DC; DC = DC->getParent()) 496 if (DC == this) 497 return true; 498 return false; 499 } 500 501 /// getPrimaryContext - There may be many different 502 /// declarations of the same entity (including forward declarations 503 /// of classes, multiple definitions of namespaces, etc.), each with 504 /// a different set of declarations. This routine returns the 505 /// "primary" DeclContext structure, which will contain the 506 /// information needed to perform name lookup into this context. 507 DeclContext *getPrimaryContext(); 508 509 /// getLookupContext - Retrieve the innermost non-transparent 510 /// context of this context, which corresponds to the innermost 511 /// location from which name lookup can find the entities in this 512 /// context. 513 DeclContext *getLookupContext() { 514 return const_cast<DeclContext *>( 515 const_cast<const DeclContext *>(this)->getLookupContext()); 516 } 517 const DeclContext *getLookupContext() const; 518 519 /// \brief Retrieve the nearest enclosing namespace context. 520 DeclContext *getEnclosingNamespaceContext(); 521 const DeclContext *getEnclosingNamespaceContext() const { 522 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext(); 523 } 524 525 /// getNextContext - If this is a DeclContext that may have other 526 /// DeclContexts that are semantically connected but syntactically 527 /// different, such as C++ namespaces, this routine retrieves the 528 /// next DeclContext in the link. Iteration through the chain of 529 /// DeclContexts should begin at the primary DeclContext and 530 /// continue until this function returns NULL. For example, given: 531 /// @code 532 /// namespace N { 533 /// int x; 534 /// } 535 /// namespace N { 536 /// int y; 537 /// } 538 /// @endcode 539 /// The first occurrence of namespace N will be the primary 540 /// DeclContext. Its getNextContext will return the second 541 /// occurrence of namespace N. 542 DeclContext *getNextContext(); 543 544 /// decl_iterator - Iterates through the declarations stored 545 /// within this context. 546 class decl_iterator { 547 /// Current - The current declaration. 548 Decl *Current; 549 550 public: 551 typedef Decl* value_type; 552 typedef Decl* reference; 553 typedef Decl* pointer; 554 typedef std::forward_iterator_tag iterator_category; 555 typedef std::ptrdiff_t difference_type; 556 557 decl_iterator() : Current(0) { } 558 explicit decl_iterator(Decl *C) : Current(C) { } 559 560 reference operator*() const { return Current; } 561 pointer operator->() const { return Current; } 562 563 decl_iterator& operator++(); 564 565 decl_iterator operator++(int) { 566 decl_iterator tmp(*this); 567 ++(*this); 568 return tmp; 569 } 570 571 friend bool operator==(decl_iterator x, decl_iterator y) { 572 return x.Current == y.Current; 573 } 574 friend bool operator!=(decl_iterator x, decl_iterator y) { 575 return x.Current != y.Current; 576 } 577 }; 578 579 /// decls_begin/decls_end - Iterate over the declarations stored in 580 /// this context. 581 decl_iterator decls_begin() const { return decl_iterator(FirstDecl); } 582 decl_iterator decls_end() const { return decl_iterator(); } 583 584 /// specific_decl_iterator - Iterates over a subrange of 585 /// declarations stored in a DeclContext, providing only those that 586 /// are of type SpecificDecl (or a class derived from it). This 587 /// iterator is used, for example, to provide iteration over just 588 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl). 589 template<typename SpecificDecl> 590 class specific_decl_iterator { 591 /// Current - The current, underlying declaration iterator, which 592 /// will either be NULL or will point to a declaration of 593 /// type SpecificDecl. 594 DeclContext::decl_iterator Current; 595 596 /// SkipToNextDecl - Advances the current position up to the next 597 /// declaration of type SpecificDecl that also meets the criteria 598 /// required by Acceptable. 599 void SkipToNextDecl() { 600 while (*Current && !isa<SpecificDecl>(*Current)) 601 ++Current; 602 } 603 604 public: 605 typedef SpecificDecl* value_type; 606 typedef SpecificDecl* reference; 607 typedef SpecificDecl* pointer; 608 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 609 difference_type; 610 typedef std::forward_iterator_tag iterator_category; 611 612 specific_decl_iterator() : Current() { } 613 614 /// specific_decl_iterator - Construct a new iterator over a 615 /// subset of the declarations the range [C, 616 /// end-of-declarations). If A is non-NULL, it is a pointer to a 617 /// member function of SpecificDecl that should return true for 618 /// all of the SpecificDecl instances that will be in the subset 619 /// of iterators. For example, if you want Objective-C instance 620 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 621 /// &ObjCMethodDecl::isInstanceMethod. 622 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 623 SkipToNextDecl(); 624 } 625 626 reference operator*() const { return cast<SpecificDecl>(*Current); } 627 pointer operator->() const { return cast<SpecificDecl>(*Current); } 628 629 specific_decl_iterator& operator++() { 630 ++Current; 631 SkipToNextDecl(); 632 return *this; 633 } 634 635 specific_decl_iterator operator++(int) { 636 specific_decl_iterator tmp(*this); 637 ++(*this); 638 return tmp; 639 } 640 641 friend bool 642 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) { 643 return x.Current == y.Current; 644 } 645 646 friend bool 647 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) { 648 return x.Current != y.Current; 649 } 650 }; 651 652 /// \brief Iterates over a filtered subrange of declarations stored 653 /// in a DeclContext. 654 /// 655 /// This iterator visits only those declarations that are of type 656 /// SpecificDecl (or a class derived from it) and that meet some 657 /// additional run-time criteria. This iterator is used, for 658 /// example, to provide access to the instance methods within an 659 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and 660 /// Acceptable = ObjCMethodDecl::isInstanceMethod). 661 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const> 662 class filtered_decl_iterator { 663 /// Current - The current, underlying declaration iterator, which 664 /// will either be NULL or will point to a declaration of 665 /// type SpecificDecl. 666 DeclContext::decl_iterator Current; 667 668 /// SkipToNextDecl - Advances the current position up to the next 669 /// declaration of type SpecificDecl that also meets the criteria 670 /// required by Acceptable. 671 void SkipToNextDecl() { 672 while (*Current && 673 (!isa<SpecificDecl>(*Current) || 674 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) 675 ++Current; 676 } 677 678 public: 679 typedef SpecificDecl* value_type; 680 typedef SpecificDecl* reference; 681 typedef SpecificDecl* pointer; 682 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 683 difference_type; 684 typedef std::forward_iterator_tag iterator_category; 685 686 filtered_decl_iterator() : Current() { } 687 688 /// specific_decl_iterator - Construct a new iterator over a 689 /// subset of the declarations the range [C, 690 /// end-of-declarations). If A is non-NULL, it is a pointer to a 691 /// member function of SpecificDecl that should return true for 692 /// all of the SpecificDecl instances that will be in the subset 693 /// of iterators. For example, if you want Objective-C instance 694 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 695 /// &ObjCMethodDecl::isInstanceMethod. 696 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 697 SkipToNextDecl(); 698 } 699 700 reference operator*() const { return cast<SpecificDecl>(*Current); } 701 pointer operator->() const { return cast<SpecificDecl>(*Current); } 702 703 filtered_decl_iterator& operator++() { 704 ++Current; 705 SkipToNextDecl(); 706 return *this; 707 } 708 709 filtered_decl_iterator operator++(int) { 710 filtered_decl_iterator tmp(*this); 711 ++(*this); 712 return tmp; 713 } 714 715 friend bool 716 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { 717 return x.Current == y.Current; 718 } 719 720 friend bool 721 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { 722 return x.Current != y.Current; 723 } 724 }; 725 726 /// @brief Add the declaration D into this context. 727 /// 728 /// This routine should be invoked when the declaration D has first 729 /// been declared, to place D into the context where it was 730 /// (lexically) defined. Every declaration must be added to one 731 /// (and only one!) context, where it can be visited via 732 /// [decls_begin(), decls_end()). Once a declaration has been added 733 /// to its lexical context, the corresponding DeclContext owns the 734 /// declaration. 735 /// 736 /// If D is also a NamedDecl, it will be made visible within its 737 /// semantic context via makeDeclVisibleInContext. 738 void addDecl(Decl *D); 739 740 /// lookup_iterator - An iterator that provides access to the results 741 /// of looking up a name within this context. 742 typedef NamedDecl **lookup_iterator; 743 744 /// lookup_const_iterator - An iterator that provides non-mutable 745 /// access to the results of lookup up a name within this context. 746 typedef NamedDecl * const * lookup_const_iterator; 747 748 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result; 749 typedef std::pair<lookup_const_iterator, lookup_const_iterator> 750 lookup_const_result; 751 752 /// lookup - Find the declarations (if any) with the given Name in 753 /// this context. Returns a range of iterators that contains all of 754 /// the declarations with this name, with object, function, member, 755 /// and enumerator names preceding any tag name. Note that this 756 /// routine will not look into parent contexts. 757 lookup_result lookup(DeclarationName Name); 758 lookup_const_result lookup(DeclarationName Name) const; 759 760 /// @brief Makes a declaration visible within this context. 761 /// 762 /// This routine makes the declaration D visible to name lookup 763 /// within this context and, if this is a transparent context, 764 /// within its parent contexts up to the first enclosing 765 /// non-transparent context. Making a declaration visible within a 766 /// context does not transfer ownership of a declaration, and a 767 /// declaration can be visible in many contexts that aren't its 768 /// lexical context. 769 /// 770 /// If D is a redeclaration of an existing declaration that is 771 /// visible from this context, as determined by 772 /// NamedDecl::declarationReplaces, the previous declaration will be 773 /// replaced with D. 774 void makeDeclVisibleInContext(NamedDecl *D); 775 776 /// udir_iterator - Iterates through the using-directives stored 777 /// within this context. 778 typedef UsingDirectiveDecl * const * udir_iterator; 779 780 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range; 781 782 udir_iterator_range getUsingDirectives() const; 783 784 udir_iterator using_directives_begin() const { 785 return getUsingDirectives().first; 786 } 787 788 udir_iterator using_directives_end() const { 789 return getUsingDirectives().second; 790 } 791 792 static bool classof(const Decl *D); 793 static bool classof(const DeclContext *D) { return true; } 794#define DECL_CONTEXT(Name) \ 795 static bool classof(const Name##Decl *D) { return true; } 796#include "clang/AST/DeclNodes.def" 797 798private: 799 void buildLookup(DeclContext *DCtx); 800 void makeDeclVisibleInContextImpl(NamedDecl *D); 801 802 void EmitOutRec(llvm::Serializer& S) const; 803 void ReadOutRec(llvm::Deserializer& D, ASTContext& C); 804 805 friend class Decl; 806}; 807 808inline bool Decl::isTemplateParameter() const { 809 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm; 810} 811 812inline bool Decl::isDefinedOutsideFunctionOrMethod() const { 813 if (getDeclContext()) 814 return !getDeclContext()->getLookupContext()->isFunctionOrMethod(); 815 else 816 return true; 817} 818 819inline DeclContext::decl_iterator& DeclContext::decl_iterator::operator++() { 820 Current = getNextDeclInScope(Current); 821 return *this; 822} 823 824} // end clang. 825 826namespace llvm { 827 828/// Implement a isa_impl_wrap specialization to check whether a DeclContext is 829/// a specific Decl. 830template<class ToTy> 831struct isa_impl_wrap<ToTy, 832 const ::clang::DeclContext,const ::clang::DeclContext> { 833 static bool doit(const ::clang::DeclContext &Val) { 834 return ToTy::classof(::clang::Decl::castFromDeclContext(&Val)); 835 } 836}; 837template<class ToTy> 838struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext> 839 : public isa_impl_wrap<ToTy, 840 const ::clang::DeclContext,const ::clang::DeclContext> {}; 841 842/// Implement cast_convert_val for Decl -> DeclContext conversions. 843template<class FromTy> 844struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { 845 static ::clang::DeclContext &doit(const FromTy &Val) { 846 return *FromTy::castToDeclContext(&Val); 847 } 848}; 849 850template<class FromTy> 851struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { 852 static ::clang::DeclContext *doit(const FromTy *Val) { 853 return FromTy::castToDeclContext(Val); 854 } 855}; 856 857template<class FromTy> 858struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { 859 static const ::clang::DeclContext &doit(const FromTy &Val) { 860 return *FromTy::castToDeclContext(&Val); 861 } 862}; 863 864template<class FromTy> 865struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { 866 static const ::clang::DeclContext *doit(const FromTy *Val) { 867 return FromTy::castToDeclContext(Val); 868 } 869}; 870 871/// Implement cast_convert_val for DeclContext -> Decl conversions. 872template<class ToTy> 873struct cast_convert_val<ToTy, 874 const ::clang::DeclContext,const ::clang::DeclContext> { 875 static ToTy &doit(const ::clang::DeclContext &Val) { 876 return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val)); 877 } 878}; 879template<class ToTy> 880struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> 881 : public cast_convert_val<ToTy, 882 const ::clang::DeclContext,const ::clang::DeclContext> {}; 883 884template<class ToTy> 885struct cast_convert_val<ToTy, 886 const ::clang::DeclContext*, const ::clang::DeclContext*> { 887 static ToTy *doit(const ::clang::DeclContext *Val) { 888 return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val)); 889 } 890}; 891template<class ToTy> 892struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> 893 : public cast_convert_val<ToTy, 894 const ::clang::DeclContext*,const ::clang::DeclContext*> {}; 895 896} // end namespace llvm 897 898#endif 899