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