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