DeclBase.h revision 305ec42f971a2b3e114eb35256c23e955c497a1c
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 /// isLookupMap - Determine if the lookup structure is a 375 /// DenseMap. Othewise, it is an array. 376 bool isLookupMap() const { return LookupPtr.getInt() == LookupIsMap; } 377 378 static Decl *getNextDeclInScope(Decl *D) { return D->NextDeclInScope; } 379 380protected: 381 DeclContext(Decl::Kind K) 382 : DeclKind(K), LookupPtr(), FirstDecl(0), LastDecl(0) { } 383 384 void DestroyDecls(ASTContext &C); 385 386public: 387 ~DeclContext(); 388 389 Decl::Kind getDeclKind() const { 390 return DeclKind; 391 } 392 const char *getDeclKindName() const; 393 394 /// getParent - Returns the containing DeclContext. 395 const DeclContext *getParent() const { 396 return cast<Decl>(this)->getDeclContext(); 397 } 398 DeclContext *getParent() { 399 return const_cast<DeclContext*>( 400 const_cast<const DeclContext*>(this)->getParent()); 401 } 402 403 /// getLexicalParent - Returns the containing lexical DeclContext. May be 404 /// different from getParent, e.g.: 405 /// 406 /// namespace A { 407 /// struct S; 408 /// } 409 /// struct A::S {}; // getParent() == namespace 'A' 410 /// // getLexicalParent() == translation unit 411 /// 412 const DeclContext *getLexicalParent() const { 413 return cast<Decl>(this)->getLexicalDeclContext(); 414 } 415 DeclContext *getLexicalParent() { 416 return const_cast<DeclContext*>( 417 const_cast<const DeclContext*>(this)->getLexicalParent()); 418 } 419 420 bool isFunctionOrMethod() const { 421 switch (DeclKind) { 422 case Decl::Block: 423 case Decl::ObjCMethod: 424 return true; 425 426 default: 427 if (DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast) 428 return true; 429 return false; 430 } 431 } 432 433 bool isFileContext() const { 434 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; 435 } 436 437 bool isTranslationUnit() const { 438 return DeclKind == Decl::TranslationUnit; 439 } 440 441 bool isRecord() const { 442 return DeclKind == Decl::Record || DeclKind == Decl::CXXRecord; 443 } 444 445 bool isNamespace() const { 446 return DeclKind == Decl::Namespace; 447 } 448 449 /// isTransparentContext - Determines whether this context is a 450 /// "transparent" context, meaning that the members declared in this 451 /// context are semantically declared in the nearest enclosing 452 /// non-transparent (opaque) context but are lexically declared in 453 /// this context. For example, consider the enumerators of an 454 /// enumeration type: 455 /// @code 456 /// enum E { 457 /// Val1 458 /// }; 459 /// @endcode 460 /// Here, E is a transparent context, so its enumerator (Val1) will 461 /// appear (semantically) that it is in the same context of E. 462 /// Examples of transparent contexts include: enumerations (except for 463 /// C++0x scoped enums), C++ linkage specifications, and C++0x 464 /// inline namespaces. 465 bool isTransparentContext() const; 466 467 bool Encloses(DeclContext *DC) const { 468 for (; DC; DC = DC->getParent()) 469 if (DC == this) 470 return true; 471 return false; 472 } 473 474 /// getPrimaryContext - There may be many different 475 /// declarations of the same entity (including forward declarations 476 /// of classes, multiple definitions of namespaces, etc.), each with 477 /// a different set of declarations. This routine returns the 478 /// "primary" DeclContext structure, which will contain the 479 /// information needed to perform name lookup into this context. 480 DeclContext *getPrimaryContext(); 481 482 /// getLookupContext - Retrieve the innermost non-transparent 483 /// context of this context, which corresponds to the innermost 484 /// location from which name lookup can find the entities in this 485 /// context. 486 DeclContext *getLookupContext() { 487 return const_cast<DeclContext *>( 488 const_cast<const DeclContext *>(this)->getLookupContext()); 489 } 490 const DeclContext *getLookupContext() const; 491 492 /// getNextContext - If this is a DeclContext that may have other 493 /// DeclContexts that are semantically connected but syntactically 494 /// different, such as C++ namespaces, this routine retrieves the 495 /// next DeclContext in the link. Iteration through the chain of 496 /// DeclContexts should begin at the primary DeclContext and 497 /// continue until this function returns NULL. For example, given: 498 /// @code 499 /// namespace N { 500 /// int x; 501 /// } 502 /// namespace N { 503 /// int y; 504 /// } 505 /// @endcode 506 /// The first occurrence of namespace N will be the primary 507 /// DeclContext. Its getNextContext will return the second 508 /// occurrence of namespace N. 509 DeclContext *getNextContext(); 510 511 /// decl_iterator - Iterates through the declarations stored 512 /// within this context. 513 class decl_iterator { 514 /// Current - The current declaration. 515 Decl *Current; 516 517 public: 518 typedef Decl* value_type; 519 typedef Decl* reference; 520 typedef Decl* pointer; 521 typedef std::forward_iterator_tag iterator_category; 522 typedef std::ptrdiff_t difference_type; 523 524 decl_iterator() : Current(0) { } 525 explicit decl_iterator(Decl *C) : Current(C) { } 526 527 reference operator*() const { return Current; } 528 pointer operator->() const { return Current; } 529 530 decl_iterator& operator++(); 531 532 decl_iterator operator++(int) { 533 decl_iterator tmp(*this); 534 ++(*this); 535 return tmp; 536 } 537 538 friend bool operator==(decl_iterator x, decl_iterator y) { 539 return x.Current == y.Current; 540 } 541 friend bool operator!=(decl_iterator x, decl_iterator y) { 542 return x.Current != y.Current; 543 } 544 }; 545 546 /// decls_begin/decls_end - Iterate over the declarations stored in 547 /// this context. 548 decl_iterator decls_begin() const { return decl_iterator(FirstDecl); } 549 decl_iterator decls_end() const { return decl_iterator(); } 550 551 /// specific_decl_iterator - Iterates over a subrange of 552 /// declarations stored in a DeclContext, providing only those that 553 /// are of type SpecificDecl (or a class derived from it). This 554 /// iterator is used, for example, to provide iteration over just 555 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl). 556 template<typename SpecificDecl> 557 class specific_decl_iterator { 558 /// Current - The current, underlying declaration iterator, which 559 /// will either be NULL or will point to a declaration of 560 /// type SpecificDecl. 561 DeclContext::decl_iterator Current; 562 563 /// SkipToNextDecl - Advances the current position up to the next 564 /// declaration of type SpecificDecl that also meets the criteria 565 /// required by Acceptable. 566 void SkipToNextDecl() { 567 while (*Current && !isa<SpecificDecl>(*Current)) 568 ++Current; 569 } 570 571 public: 572 typedef SpecificDecl* value_type; 573 typedef SpecificDecl* reference; 574 typedef SpecificDecl* pointer; 575 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 576 difference_type; 577 typedef std::forward_iterator_tag iterator_category; 578 579 specific_decl_iterator() : Current() { } 580 581 /// specific_decl_iterator - Construct a new iterator over a 582 /// subset of the declarations the range [C, 583 /// end-of-declarations). If A is non-NULL, it is a pointer to a 584 /// member function of SpecificDecl that should return true for 585 /// all of the SpecificDecl instances that will be in the subset 586 /// of iterators. For example, if you want Objective-C instance 587 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 588 /// &ObjCMethodDecl::isInstanceMethod. 589 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 590 SkipToNextDecl(); 591 } 592 593 reference operator*() const { return cast<SpecificDecl>(*Current); } 594 pointer operator->() const { return cast<SpecificDecl>(*Current); } 595 596 specific_decl_iterator& operator++() { 597 ++Current; 598 SkipToNextDecl(); 599 return *this; 600 } 601 602 specific_decl_iterator operator++(int) { 603 specific_decl_iterator tmp(*this); 604 ++(*this); 605 return tmp; 606 } 607 608 friend bool 609 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) { 610 return x.Current == y.Current; 611 } 612 613 friend bool 614 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) { 615 return x.Current != y.Current; 616 } 617 }; 618 619 /// \brief Iterates over a filtered subrange of declarations stored 620 /// in a DeclContext. 621 /// 622 /// This iterator visits only those declarations that are of type 623 /// SpecificDecl (or a class derived from it) and that meet some 624 /// additional run-time criteria. This iterator is used, for 625 /// example, to provide access to the instance methods within an 626 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and 627 /// Acceptable = ObjCMethodDecl::isInstanceMethod). 628 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const> 629 class filtered_decl_iterator { 630 /// Current - The current, underlying declaration iterator, which 631 /// will either be NULL or will point to a declaration of 632 /// type SpecificDecl. 633 DeclContext::decl_iterator Current; 634 635 /// SkipToNextDecl - Advances the current position up to the next 636 /// declaration of type SpecificDecl that also meets the criteria 637 /// required by Acceptable. 638 void SkipToNextDecl() { 639 while (*Current && 640 (!isa<SpecificDecl>(*Current) || 641 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) 642 ++Current; 643 } 644 645 public: 646 typedef SpecificDecl* value_type; 647 typedef SpecificDecl* reference; 648 typedef SpecificDecl* pointer; 649 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 650 difference_type; 651 typedef std::forward_iterator_tag iterator_category; 652 653 filtered_decl_iterator() : Current() { } 654 655 /// specific_decl_iterator - Construct a new iterator over a 656 /// subset of the declarations the range [C, 657 /// end-of-declarations). If A is non-NULL, it is a pointer to a 658 /// member function of SpecificDecl that should return true for 659 /// all of the SpecificDecl instances that will be in the subset 660 /// of iterators. For example, if you want Objective-C instance 661 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 662 /// &ObjCMethodDecl::isInstanceMethod. 663 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 664 SkipToNextDecl(); 665 } 666 667 reference operator*() const { return cast<SpecificDecl>(*Current); } 668 pointer operator->() const { return cast<SpecificDecl>(*Current); } 669 670 filtered_decl_iterator& operator++() { 671 ++Current; 672 SkipToNextDecl(); 673 return *this; 674 } 675 676 filtered_decl_iterator operator++(int) { 677 filtered_decl_iterator tmp(*this); 678 ++(*this); 679 return tmp; 680 } 681 682 friend bool 683 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { 684 return x.Current == y.Current; 685 } 686 687 friend bool 688 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { 689 return x.Current != y.Current; 690 } 691 }; 692 693 /// @brief Add the declaration D into this context. 694 /// 695 /// This routine should be invoked when the declaration D has first 696 /// been declared, to place D into the context where it was 697 /// (lexically) defined. Every declaration must be added to one 698 /// (and only one!) context, where it can be visited via 699 /// [decls_begin(), decls_end()). Once a declaration has been added 700 /// to its lexical context, the corresponding DeclContext owns the 701 /// declaration. 702 /// 703 /// If D is also a NamedDecl, it will be made visible within its 704 /// semantic context via makeDeclVisibleInContext. 705 void addDecl(Decl *D); 706 707 /// lookup_iterator - An iterator that provides access to the results 708 /// of looking up a name within this context. 709 typedef NamedDecl **lookup_iterator; 710 711 /// lookup_const_iterator - An iterator that provides non-mutable 712 /// access to the results of lookup up a name within this context. 713 typedef NamedDecl * const * lookup_const_iterator; 714 715 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result; 716 typedef std::pair<lookup_const_iterator, lookup_const_iterator> 717 lookup_const_result; 718 719 /// lookup - Find the declarations (if any) with the given Name in 720 /// this context. Returns a range of iterators that contains all of 721 /// the declarations with this name, with object, function, member, 722 /// and enumerator names preceding any tag name. Note that this 723 /// routine will not look into parent contexts. 724 lookup_result lookup(DeclarationName Name); 725 lookup_const_result lookup(DeclarationName Name) const; 726 727 /// @brief Makes a declaration visible within this context. 728 /// 729 /// This routine makes the declaration D visible to name lookup 730 /// within this context and, if this is a transparent context, 731 /// within its parent contexts up to the first enclosing 732 /// non-transparent context. Making a declaration visible within a 733 /// context does not transfer ownership of a declaration, and a 734 /// declaration can be visible in many contexts that aren't its 735 /// lexical context. 736 /// 737 /// If D is a redeclaration of an existing declaration that is 738 /// visible from this context, as determined by 739 /// NamedDecl::declarationReplaces, the previous declaration will be 740 /// replaced with D. 741 void makeDeclVisibleInContext(NamedDecl *D); 742 743 /// udir_iterator - Iterates through the using-directives stored 744 /// within this context. 745 typedef UsingDirectiveDecl * const * udir_iterator; 746 747 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range; 748 749 udir_iterator_range getUsingDirectives() const; 750 751 udir_iterator using_directives_begin() const { 752 return getUsingDirectives().first; 753 } 754 755 udir_iterator using_directives_end() const { 756 return getUsingDirectives().second; 757 } 758 759 static bool classof(const Decl *D); 760 static bool classof(const DeclContext *D) { return true; } 761#define DECL_CONTEXT(Name) \ 762 static bool classof(const Name##Decl *D) { return true; } 763#include "clang/AST/DeclNodes.def" 764 765private: 766 void buildLookup(DeclContext *DCtx); 767 void makeDeclVisibleInContextImpl(NamedDecl *D); 768 769 void EmitOutRec(llvm::Serializer& S) const; 770 void ReadOutRec(llvm::Deserializer& D, ASTContext& C); 771 772 friend class Decl; 773}; 774 775inline bool Decl::isTemplateParameter() const { 776 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm; 777} 778 779inline bool Decl::isDefinedOutsideFunctionOrMethod() const { 780 if (getDeclContext()) 781 return !getDeclContext()->getLookupContext()->isFunctionOrMethod(); 782 else 783 return true; 784} 785 786inline DeclContext::decl_iterator& DeclContext::decl_iterator::operator++() { 787 Current = getNextDeclInScope(Current); 788 return *this; 789} 790 791} // end clang. 792 793namespace llvm { 794 795/// Implement a isa_impl_wrap specialization to check whether a DeclContext is 796/// a specific Decl. 797template<class ToTy> 798struct isa_impl_wrap<ToTy, 799 const ::clang::DeclContext,const ::clang::DeclContext> { 800 static bool doit(const ::clang::DeclContext &Val) { 801 return ToTy::classof(::clang::Decl::castFromDeclContext(&Val)); 802 } 803}; 804template<class ToTy> 805struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext> 806 : public isa_impl_wrap<ToTy, 807 const ::clang::DeclContext,const ::clang::DeclContext> {}; 808 809/// Implement cast_convert_val for Decl -> DeclContext conversions. 810template<class FromTy> 811struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { 812 static ::clang::DeclContext &doit(const FromTy &Val) { 813 return *FromTy::castToDeclContext(&Val); 814 } 815}; 816 817template<class FromTy> 818struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { 819 static ::clang::DeclContext *doit(const FromTy *Val) { 820 return FromTy::castToDeclContext(Val); 821 } 822}; 823 824template<class FromTy> 825struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { 826 static const ::clang::DeclContext &doit(const FromTy &Val) { 827 return *FromTy::castToDeclContext(&Val); 828 } 829}; 830 831template<class FromTy> 832struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { 833 static const ::clang::DeclContext *doit(const FromTy *Val) { 834 return FromTy::castToDeclContext(Val); 835 } 836}; 837 838/// Implement cast_convert_val for DeclContext -> Decl conversions. 839template<class ToTy> 840struct cast_convert_val<ToTy, 841 const ::clang::DeclContext,const ::clang::DeclContext> { 842 static ToTy &doit(const ::clang::DeclContext &Val) { 843 return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val)); 844 } 845}; 846template<class ToTy> 847struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> 848 : public cast_convert_val<ToTy, 849 const ::clang::DeclContext,const ::clang::DeclContext> {}; 850 851template<class ToTy> 852struct cast_convert_val<ToTy, 853 const ::clang::DeclContext*, const ::clang::DeclContext*> { 854 static ToTy *doit(const ::clang::DeclContext *Val) { 855 return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val)); 856 } 857}; 858template<class ToTy> 859struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> 860 : public cast_convert_val<ToTy, 861 const ::clang::DeclContext*,const ::clang::DeclContext*> {}; 862 863} // end namespace llvm 864 865#endif 866