DeclBase.h revision 482b77d1cb4ca08391d1f749436f092a4cc24427
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#include "clang/Basic/SourceLocation.h" 20#include "llvm/ADT/PointerIntPair.h" 21 22namespace clang { 23class DeclContext; 24class TranslationUnitDecl; 25class NamespaceDecl; 26class NamedDecl; 27class ScopedDecl; 28class FunctionDecl; 29class CXXRecordDecl; 30class EnumDecl; 31class ObjCMethodDecl; 32class ObjCInterfaceDecl; 33class ObjCCategoryDecl; 34class ObjCProtocolDecl; 35class ObjCImplementationDecl; 36class ObjCCategoryImplDecl; 37class LinkageSpecDecl; 38class BlockDecl; 39class DeclarationName; 40 41/// Decl - This represents one declaration (or definition), e.g. a variable, 42/// typedef, function, struct, etc. 43/// 44class Decl { 45public: 46 enum Kind { 47 // This lists the concrete classes of Decl in order of the inheritance 48 // hierarchy. This allows us to do efficient classof tests based on the 49 // enums below. The commented out names are abstract class names. 50 // [DeclContext] indicates that the class also inherits from DeclContext. 51 52 // Decl 53 TranslationUnit, // [DeclContext] 54 // NamedDecl 55 OverloadedFunction, 56 // ScopedDecl 57 Field, 58 ObjCIvar, 59 ObjCAtDefsField, 60 Namespace, // [DeclContext] 61 // TypeDecl 62 Typedef, 63 // TagDecl // [DeclContext] 64 Enum, 65 Record, 66 CXXRecord, 67 TemplateTypeParm, 68 // ValueDecl 69 EnumConstant, 70 Function, // [DeclContext] 71 CXXMethod, 72 CXXConstructor, 73 CXXDestructor, 74 CXXConversion, 75 Var, 76 ImplicitParam, 77 CXXClassVar, 78 ParmVar, 79 OriginalParmVar, 80 NonTypeTemplateParm, 81 LinkageSpec, // [DeclContext] 82 ObjCMethod, // [DeclContext] 83 ObjCContainer, // [DeclContext] 84 ObjCCategory, 85 ObjCProtocol, 86 ObjCInterface, 87 ObjCCategoryImpl, // [DeclContext] 88 ObjCImplementation, // [DeclContext] 89 ObjCProperty, 90 ObjCCompatibleAlias, 91 ObjCClass, 92 ObjCForwardProtocol, 93 ObjCPropertyImpl, 94 FileScopeAsm, 95 Block, // [DeclContext] 96 97 // For each non-leaf class, we now define a mapping to the first/last member 98 // of the class, to allow efficient classof. 99 NamedFirst = OverloadedFunction, NamedLast = NonTypeTemplateParm, 100 ObjCContainerFirst = ObjCContainer, ObjCContainerLast = ObjCInterface, 101 FieldFirst = Field , FieldLast = ObjCAtDefsField, 102 ScopedFirst = Field , ScopedLast = ObjCPropertyImpl, 103 TypeFirst = Typedef , TypeLast = TemplateTypeParm, 104 TagFirst = Enum , TagLast = CXXRecord, 105 RecordFirst = Record , RecordLast = CXXRecord, 106 ValueFirst = EnumConstant , ValueLast = NonTypeTemplateParm, 107 FunctionFirst = Function , FunctionLast = CXXConversion, 108 VarFirst = Var , VarLast = NonTypeTemplateParm 109 }; 110 111 /// IdentifierNamespace - According to C99 6.2.3, there are four namespaces, 112 /// labels, tags, members and ordinary identifiers. These are meant 113 /// as bitmasks, so that searches in C++ can look into the "tag" namespace 114 /// during ordinary lookup. 115 enum IdentifierNamespace { 116 IDNS_Label = 0x1, 117 IDNS_Tag = 0x2, 118 IDNS_Member = 0x4, 119 IDNS_Ordinary = 0x8 120 }; 121 122 /// ObjCDeclQualifier - Qualifier used on types in method declarations 123 /// for remote messaging. They are meant for the arguments though and 124 /// applied to the Decls (ObjCMethodDecl and ParmVarDecl). 125 enum ObjCDeclQualifier { 126 OBJC_TQ_None = 0x0, 127 OBJC_TQ_In = 0x1, 128 OBJC_TQ_Inout = 0x2, 129 OBJC_TQ_Out = 0x4, 130 OBJC_TQ_Bycopy = 0x8, 131 OBJC_TQ_Byref = 0x10, 132 OBJC_TQ_Oneway = 0x20 133 }; 134 135private: 136 /// Loc - The location that this decl. 137 SourceLocation Loc; 138 139 /// DeclKind - This indicates which class this is. 140 Kind DeclKind : 8; 141 142 /// InvalidDecl - This indicates a semantic error occurred. 143 unsigned int InvalidDecl : 1; 144 145 /// HasAttrs - This indicates whether the decl has attributes or not. 146 unsigned int HasAttrs : 1; 147 148 /// Implicit - Whether this declaration was implicitly generated by 149 /// the implementation rather than explicitly written by the user. 150 bool Implicit : 1; 151 152 protected: 153 /// Access - Used by C++ decls for the access specifier. 154 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum 155 unsigned Access : 2; 156 friend class CXXClassMemberWrapper; 157 158 Decl(Kind DK, SourceLocation L) : Loc(L), DeclKind(DK), InvalidDecl(0), 159 HasAttrs(false), Implicit(false) { 160 if (Decl::CollectingStats()) addDeclKind(DK); 161 } 162 163 virtual ~Decl(); 164 165public: 166 SourceLocation getLocation() const { return Loc; } 167 void setLocation(SourceLocation L) { Loc = L; } 168 169 Kind getKind() const { return DeclKind; } 170 const char *getDeclKindName() const; 171 172 void addAttr(Attr *attr); 173 const Attr *getAttrs() const; 174 void swapAttrs(Decl *D); 175 void invalidateAttrs(); 176 177 template<typename T> const T *getAttr() const { 178 for (const Attr *attr = getAttrs(); attr; attr = attr->getNext()) 179 if (const T *V = dyn_cast<T>(attr)) 180 return V; 181 182 return 0; 183 } 184 185 /// setInvalidDecl - Indicates the Decl had a semantic error. This 186 /// allows for graceful error recovery. 187 void setInvalidDecl() { InvalidDecl = 1; } 188 bool isInvalidDecl() const { return (bool) InvalidDecl; } 189 190 /// isImplicit - Indicates whether the declaration was implicitly 191 /// generated by the implementation. If false, this declaration 192 /// was written explicitly in the source code. 193 bool isImplicit() const { return Implicit; } 194 void setImplicit(bool I = true) { Implicit = I; } 195 196 IdentifierNamespace getIdentifierNamespace() const { 197 switch (DeclKind) { 198 default: 199 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast) 200 return IDNS_Ordinary; 201 assert(0 && "Unknown decl kind!"); 202 case ImplicitParam: 203 case Typedef: 204 case Var: 205 case ParmVar: 206 case OriginalParmVar: 207 case EnumConstant: 208 case NonTypeTemplateParm: 209 case ObjCInterface: 210 case ObjCCompatibleAlias: 211 case OverloadedFunction: 212 case CXXMethod: 213 case CXXConversion: 214 case CXXClassVar: 215 return IDNS_Ordinary; 216 217 case Field: 218 case ObjCAtDefsField: 219 case ObjCIvar: 220 return IDNS_Member; 221 222 case Record: 223 case CXXRecord: 224 case TemplateTypeParm: 225 case Enum: 226 return IDNS_Tag; 227 case Namespace: 228 return IdentifierNamespace(IDNS_Tag | IDNS_Ordinary); 229 } 230 } 231 232 bool isInIdentifierNamespace(unsigned NS) const { 233 return getIdentifierNamespace() & NS; 234 } 235 236 // getBody - If this Decl represents a declaration for a body of code, 237 // such as a function or method definition, this method returns the top-level 238 // Stmt* of that body. Otherwise this method returns null. 239 virtual Stmt* getBody() const { return 0; } 240 241 // global temp stats (until we have a per-module visitor) 242 static void addDeclKind(Kind k); 243 static bool CollectingStats(bool Enable = false); 244 static void PrintStats(); 245 246 /// isTemplateParameter - Determines whether this declartion is a 247 /// template parameter. 248 bool isTemplateParameter() const; 249 250 // Implement isa/cast/dyncast/etc. 251 static bool classof(const Decl *) { return true; } 252 static DeclContext *castToDeclContext(const Decl *); 253 static Decl *castFromDeclContext(const DeclContext *); 254 255 /// Emit - Serialize this Decl to Bitcode. 256 void Emit(llvm::Serializer& S) const; 257 258 /// Create - Deserialize a Decl from Bitcode. 259 static Decl* Create(llvm::Deserializer& D, ASTContext& C); 260 261 /// Destroy - Call destructors and release memory. 262 virtual void Destroy(ASTContext& C); 263 264protected: 265 /// EmitImpl - Provides the subclass-specific serialization logic for 266 /// serializing out a decl. 267 virtual void EmitImpl(llvm::Serializer& S) const { 268 // FIXME: This will eventually be a pure virtual function. 269 assert (false && "Not implemented."); 270 } 271 272 void EmitInRec(llvm::Serializer& S) const; 273 void ReadInRec(llvm::Deserializer& D, ASTContext& C); 274}; 275 276/// DeclContext - This is used only as base class of specific decl types that 277/// can act as declaration contexts. These decls are: 278/// 279/// TranslationUnitDecl 280/// NamespaceDecl 281/// FunctionDecl 282/// RecordDecl/CXXRecordDecl 283/// EnumDecl 284/// ObjCMethodDecl 285/// ObjCInterfaceDecl 286/// LinkageSpecDecl 287/// BlockDecl 288class DeclContext { 289 /// DeclKind - This indicates which class this is. 290 Decl::Kind DeclKind : 8; 291 292 /// LookupPtrKind - Describes what kind of pointer LookupPtr 293 /// actually is. 294 enum LookupPtrKind { 295 /// LookupIsMap - Indicates that LookupPtr is actually a map. 296 LookupIsMap = 7 297 }; 298 299 /// LookupPtr - Pointer to a data structure used to lookup 300 /// declarations within this context. If the context contains fewer 301 /// than seven declarations, the number of declarations is provided 302 /// in the 3 lowest-order bits and the upper bits are treated as a 303 /// pointer to an array of ScopedDecl pointers. If the context 304 /// contains seven or more declarations, the upper bits are treated 305 /// as a pointer to a DenseMap<DeclarationName, std::vector<ScopedDecl>>. 306 /// FIXME: We need a better data structure for this. 307 llvm::PointerIntPair<void*, 3> LookupPtr; 308 309 /// FirstDecl - The first declaration stored within this declaration 310 /// context. 311 ScopedDecl *FirstDecl; 312 313 /// LastDecl - The last declaration stored within this declaration 314 /// context. FIXME: We could probably cache this value somewhere 315 /// outside of the DeclContext, to reduce the size of DeclContext by 316 /// another pointer. 317 ScopedDecl *LastDecl; 318 319 // Used in the CastTo template to get the DeclKind 320 // from a Decl or a DeclContext. DeclContext doesn't have a getKind() method 321 // to avoid 'ambiguous access' compiler errors. 322 template<typename T> struct KindTrait { 323 static Decl::Kind getKind(const T *D) { return D->getKind(); } 324 }; 325 326 // Used only by the ToDecl and FromDecl methods 327 template<typename To, typename From> 328 static To *CastTo(const From *D) { 329 Decl::Kind DK = KindTrait<From>::getKind(D); 330 switch(DK) { 331 case Decl::TranslationUnit: 332 return static_cast<TranslationUnitDecl*>(const_cast<From*>(D)); 333 case Decl::Namespace: 334 return static_cast<NamespaceDecl*>(const_cast<From*>(D)); 335 case Decl::Enum: 336 return static_cast<EnumDecl*>(const_cast<From*>(D)); 337 case Decl::Record: 338 return static_cast<RecordDecl*>(const_cast<From*>(D)); 339 case Decl::CXXRecord: 340 return static_cast<CXXRecordDecl*>(const_cast<From*>(D)); 341 case Decl::ObjCMethod: 342 return static_cast<ObjCMethodDecl*>(const_cast<From*>(D)); 343 case Decl::ObjCInterface: 344 return static_cast<ObjCInterfaceDecl*>(const_cast<From*>(D)); 345 case Decl::ObjCCategory: 346 return static_cast<ObjCCategoryDecl*>(const_cast<From*>(D)); 347 case Decl::ObjCProtocol: 348 return static_cast<ObjCProtocolDecl*>(const_cast<From*>(D)); 349 case Decl::ObjCImplementation: 350 return static_cast<ObjCImplementationDecl*>(const_cast<From*>(D)); 351 case Decl::ObjCCategoryImpl: 352 return static_cast<ObjCCategoryImplDecl*>(const_cast<From*>(D)); 353 case Decl::LinkageSpec: 354 return static_cast<LinkageSpecDecl*>(const_cast<From*>(D)); 355 case Decl::Block: 356 return static_cast<BlockDecl*>(const_cast<From*>(D)); 357 default: 358 if (DK >= Decl::FunctionFirst && DK <= Decl::FunctionLast) 359 return static_cast<FunctionDecl*>(const_cast<From*>(D)); 360 361 assert(false && "a decl that inherits DeclContext isn't handled"); 362 return 0; 363 } 364 } 365 366 /// isLookupMap - Determine if the lookup structure is a 367 /// DenseMap. Othewise, it is an array. 368 bool isLookupMap() const { return LookupPtr.getInt() == LookupIsMap; } 369 370protected: 371 DeclContext(Decl::Kind K) 372 : DeclKind(K), LookupPtr(), FirstDecl(0), LastDecl(0) { } 373 374 void DestroyDecls(ASTContext &C); 375 376public: 377 ~DeclContext(); 378 379 /// getParent - Returns the containing DeclContext if this is a ScopedDecl, 380 /// else returns NULL. 381 const DeclContext *getParent() const; 382 DeclContext *getParent() { 383 return const_cast<DeclContext*>( 384 const_cast<const DeclContext*>(this)->getParent()); 385 } 386 387 /// getLexicalParent - Returns the containing lexical DeclContext. May be 388 /// different from getParent, e.g.: 389 /// 390 /// namespace A { 391 /// struct S; 392 /// } 393 /// struct A::S {}; // getParent() == namespace 'A' 394 /// // getLexicalParent() == translation unit 395 /// 396 const DeclContext *getLexicalParent() const; 397 DeclContext *getLexicalParent() { 398 return const_cast<DeclContext*>( 399 const_cast<const DeclContext*>(this)->getLexicalParent()); 400 } 401 402 bool isFunctionOrMethod() const { 403 switch (DeclKind) { 404 case Decl::Block: 405 case Decl::ObjCMethod: 406 return true; 407 408 default: 409 if (DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast) 410 return true; 411 return false; 412 } 413 } 414 415 bool isFileContext() const { 416 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; 417 } 418 419 bool isRecord() const { 420 return DeclKind == Decl::Record || DeclKind == Decl::CXXRecord; 421 } 422 423 bool isNamespace() const { 424 return DeclKind == Decl::Namespace; 425 } 426 427 /// isTransparentContext - Determines whether this context is a 428 /// "transparent" context, meaning that the members declared in this 429 /// context are semantically declared in the nearest enclosing 430 /// non-transparent (opaque) context but are lexically declared in 431 /// this context. For example, consider the enumerators of an 432 /// enumeration type: 433 /// @code 434 /// enum E { 435 /// Val1 436 /// }; 437 /// @endcode 438 /// Here, E is a transparent context, so its enumerator (Val1) will 439 /// appear (semantically) that it is in the same context of E. 440 /// Examples of transparent contexts include: enumerations (except for 441 /// C++0x scoped enums), C++ linkage specifications, and C++0x 442 /// inline namespaces. 443 bool isTransparentContext() const; 444 445 bool Encloses(DeclContext *DC) const { 446 for (; DC; DC = DC->getParent()) 447 if (DC == this) 448 return true; 449 return false; 450 } 451 452 /// getPrimaryContext - There may be many different 453 /// declarations of the same entity (including forward declarations 454 /// of classes, multiple definitions of namespaces, etc.), each with 455 /// a different set of declarations. This routine returns the 456 /// "primary" DeclContext structure, which will contain the 457 /// information needed to perform name lookup into this context. 458 DeclContext *getPrimaryContext(); 459 460 /// getLookupContext - Retrieve the innermost non-transparent 461 /// context of this context, which corresponds to the innermost 462 /// location from which name lookup can find the entities in this 463 /// context. 464 DeclContext *getLookupContext() { 465 return const_cast<DeclContext *>( 466 const_cast<const DeclContext *>(this)->getLookupContext()); 467 } 468 const DeclContext *getLookupContext() const; 469 470 /// getNextContext - If this is a DeclContext that may have other 471 /// DeclContexts that are semantically connected but syntactically 472 /// different, such as C++ namespaces, this routine retrieves the 473 /// next DeclContext in the link. Iteration through the chain of 474 /// DeclContexts should begin at the primary DeclContext and 475 /// continue until this function returns NULL. For example, given: 476 /// @code 477 /// namespace N { 478 /// int x; 479 /// } 480 /// namespace N { 481 /// int y; 482 /// } 483 /// @endcode 484 /// The first occurrence of namespace N will be the primary 485 /// DeclContext. Its getNextContext will return the second 486 /// occurrence of namespace N. 487 DeclContext *getNextContext(); 488 489 /// decl_iterator - Iterates through the declarations stored 490 /// within this context. 491 class decl_iterator { 492 /// Current - The current declaration. 493 ScopedDecl *Current; 494 495 public: 496 typedef ScopedDecl* value_type; 497 typedef ScopedDecl* reference; 498 typedef ScopedDecl* pointer; 499 typedef std::forward_iterator_tag iterator_category; 500 typedef std::ptrdiff_t difference_type; 501 502 decl_iterator() : Current(0) { } 503 explicit decl_iterator(ScopedDecl *C) : Current(C) { } 504 505 reference operator*() const { return Current; } 506 pointer operator->() const { return Current; } 507 508 decl_iterator& operator++(); 509 510 decl_iterator operator++(int) { 511 decl_iterator tmp(*this); 512 ++(*this); 513 return tmp; 514 } 515 516 friend bool operator==(decl_iterator x, decl_iterator y) { 517 return x.Current == y.Current; 518 } 519 friend bool operator!=(decl_iterator x, decl_iterator y) { 520 return x.Current != y.Current; 521 } 522 }; 523 524 /// decls_begin/decls_end - Iterate over the declarations stored in 525 /// this context. 526 decl_iterator decls_begin() const { return decl_iterator(FirstDecl); } 527 decl_iterator decls_end() const { return decl_iterator(); } 528 529 /// specific_decl_iterator - Iterates over a subrange of 530 /// declarations stored in a DeclContext, providing only those that 531 /// are of type SpecificDecl (or a class derived from it) and, 532 /// optionally, that meet some additional run-time criteria. This 533 /// iterator is used, for example, to provide iteration over just 534 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl) 535 /// or the instance methods within an Objective-C interface (with 536 /// SpecificDecl = ObjCMethodDecl and using 537 /// ObjCMethodDecl::isInstanceMethod as the run-time criteria). 538 template<typename SpecificDecl> 539 class specific_decl_iterator { 540 /// Current - The current, underlying declaration iterator, which 541 /// will either be the same as End or will point to a declaration of 542 /// type SpecificDecl. 543 DeclContext::decl_iterator Current; 544 545 /// End - One past the last declaration within the DeclContext. 546 DeclContext::decl_iterator End; 547 548 /// Acceptable - If non-NULL, points to a member function that 549 /// will determine if a particular declaration of type 550 /// SpecificDecl should be visited by the iteration. 551 bool (SpecificDecl::*Acceptable)() const; 552 553 /// SkipToNextDecl - Advances the current position up to the next 554 /// declaration of type SpecificDecl that also meets the criteria 555 /// required by Acceptable. 556 void SkipToNextDecl() { 557 while (Current != End && 558 (!isa<SpecificDecl>(*Current) || 559 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) 560 ++Current; 561 } 562 563 public: 564 typedef SpecificDecl* value_type; 565 typedef SpecificDecl* reference; 566 typedef SpecificDecl* pointer; 567 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 568 difference_type; 569 typedef std::forward_iterator_tag iterator_category; 570 571 specific_decl_iterator() : Current(), End(), Acceptable(0) { } 572 573 /// specific_decl_iterator - Construct a new iterator over a 574 /// subset of the declarations in [C, E). If A is non-NULL, it is 575 /// a pointer to a member function of SpecificDecl that should 576 /// return true for all of the SpecificDecl instances that will be 577 /// in the subset of iterators. For example, if you want 578 /// Objective-C instance methods, SpecificDecl will be 579 /// ObjCMethodDecl and A will be &ObjCMethodDecl::isInstanceMethod. 580 specific_decl_iterator(DeclContext::decl_iterator C, 581 DeclContext::decl_iterator E, 582 bool (SpecificDecl::*A)() const = 0) 583 : Current(C), End(E), Acceptable(A) { 584 SkipToNextDecl(); 585 } 586 587 reference operator*() const { return cast<SpecificDecl>(*Current); } 588 pointer operator->() const { return cast<SpecificDecl>(*Current); } 589 590 specific_decl_iterator& operator++() { 591 ++Current; 592 SkipToNextDecl(); 593 return *this; 594 } 595 596 specific_decl_iterator operator++(int) { 597 specific_decl_iterator tmp(*this); 598 ++(*this); 599 return tmp; 600 } 601 602 friend bool 603 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) { 604 return x.Current == y.Current; 605 } 606 607 friend bool 608 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) { 609 return x.Current != y.Current; 610 } 611 }; 612 613 /// addDecl - Add the declaration D to this scope, and into data structure 614 /// for name lookup. 615 void addDecl(ScopedDecl *D); 616 617 void buildLookup(DeclContext *DCtx); 618 619 /// lookup_iterator - An iterator that provides access to the results 620 /// of looking up a name within this context. 621 typedef ScopedDecl **lookup_iterator; 622 623 /// lookup_const_iterator - An iterator that provides non-mutable 624 /// access to the results of lookup up a name within this context. 625 typedef ScopedDecl * const * lookup_const_iterator; 626 627 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result; 628 typedef std::pair<lookup_const_iterator, lookup_const_iterator> 629 lookup_const_result; 630 631 /// lookup - Find the declarations (if any) with the given Name in 632 /// this context. Returns a range of iterators that contains all of 633 /// the declarations with this name (which may be 0, 1, or more 634 /// declarations). If two declarations are returned, the declaration 635 /// in the "ordinary" identifier namespace will precede the 636 /// declaration in the "tag" identifier namespace (e.g., values 637 /// before types). Note that this routine will not look into parent 638 /// contexts. 639 lookup_result lookup(DeclarationName Name); 640 lookup_const_result lookup(DeclarationName Name) const; 641 642 /// insert - Insert the declaration D into this context. Up to two 643 /// declarations with the same name can be inserted into a single 644 /// declaration context, one in the "tag" namespace (e.g., for 645 /// classes and enums) and one in the "ordinary" namespaces (e.g., 646 /// for variables, functions, and other values). Note that, if there 647 /// is already a declaration with the same name and identifier 648 /// namespace, D will replace it. It is up to the caller to ensure 649 /// that this replacement is semantically correct, e.g., that 650 /// declarations are only replaced by later declarations of the same 651 /// entity and not a declaration of some other kind of entity. 652 void insert(ScopedDecl *D); 653 654 static bool classof(const Decl *D) { 655 switch (D->getKind()) { 656 case Decl::TranslationUnit: 657 case Decl::Namespace: 658 case Decl::Enum: 659 case Decl::Record: 660 case Decl::CXXRecord: 661 case Decl::ObjCMethod: 662 case Decl::ObjCInterface: 663 case Decl::ObjCCategory: 664 case Decl::ObjCProtocol: 665 case Decl::ObjCImplementation: 666 case Decl::ObjCCategoryImpl: 667 case Decl::LinkageSpec: 668 case Decl::Block: 669 return true; 670 default: 671 if (D->getKind() >= Decl::FunctionFirst && 672 D->getKind() <= Decl::FunctionLast) 673 return true; 674 return false; 675 } 676 } 677 static bool classof(const DeclContext *D) { return true; } 678 static bool classof(const TranslationUnitDecl *D) { return true; } 679 static bool classof(const NamespaceDecl *D) { return true; } 680 static bool classof(const FunctionDecl *D) { return true; } 681 static bool classof(const RecordDecl *D) { return true; } 682 static bool classof(const CXXRecordDecl *D) { return true; } 683 static bool classof(const EnumDecl *D) { return true; } 684 static bool classof(const ObjCMethodDecl *D) { return true; } 685 static bool classof(const ObjCInterfaceDecl *D) { return true; } 686 static bool classof(const ObjCCategoryDecl *D) { return true; } 687 static bool classof(const ObjCProtocolDecl *D) { return true; } 688 static bool classof(const ObjCImplementationDecl *D) { return true; } 689 static bool classof(const ObjCCategoryImplDecl *D) { return true; } 690 static bool classof(const LinkageSpecDecl *D) { return true; } 691 static bool classof(const BlockDecl *D) { return true; } 692 693private: 694 void insertImpl(ScopedDecl *D); 695 696 void EmitOutRec(llvm::Serializer& S) const; 697 void ReadOutRec(llvm::Deserializer& D, ASTContext& C); 698 699 friend class Decl; 700}; 701 702template<> struct DeclContext::KindTrait<DeclContext> { 703 static Decl::Kind getKind(const DeclContext *D) { return D->DeclKind; } 704}; 705 706inline bool Decl::isTemplateParameter() const { 707 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm; 708} 709 710} // end clang. 711 712namespace llvm { 713 714/// Implement a isa_impl_wrap specialization to check whether a DeclContext is 715/// a specific Decl. 716template<class ToTy> 717struct isa_impl_wrap<ToTy, 718 const ::clang::DeclContext,const ::clang::DeclContext> { 719 static bool doit(const ::clang::DeclContext &Val) { 720 return ToTy::classof(::clang::Decl::castFromDeclContext(&Val)); 721 } 722}; 723template<class ToTy> 724struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext> 725 : public isa_impl_wrap<ToTy, 726 const ::clang::DeclContext,const ::clang::DeclContext> {}; 727 728/// Implement cast_convert_val for Decl -> DeclContext conversions. 729template<class FromTy> 730struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { 731 static ::clang::DeclContext &doit(const FromTy &Val) { 732 return *FromTy::castToDeclContext(&Val); 733 } 734}; 735 736template<class FromTy> 737struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { 738 static ::clang::DeclContext *doit(const FromTy *Val) { 739 return FromTy::castToDeclContext(Val); 740 } 741}; 742 743template<class FromTy> 744struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { 745 static const ::clang::DeclContext &doit(const FromTy &Val) { 746 return *FromTy::castToDeclContext(&Val); 747 } 748}; 749 750template<class FromTy> 751struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { 752 static const ::clang::DeclContext *doit(const FromTy *Val) { 753 return FromTy::castToDeclContext(Val); 754 } 755}; 756 757/// Implement cast_convert_val for DeclContext -> Decl conversions. 758template<class ToTy> 759struct cast_convert_val<ToTy, 760 const ::clang::DeclContext,const ::clang::DeclContext> { 761 static ToTy &doit(const ::clang::DeclContext &Val) { 762 return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val)); 763 } 764}; 765template<class ToTy> 766struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> 767 : public cast_convert_val<ToTy, 768 const ::clang::DeclContext,const ::clang::DeclContext> {}; 769 770template<class ToTy> 771struct cast_convert_val<ToTy, 772 const ::clang::DeclContext*, const ::clang::DeclContext*> { 773 static ToTy *doit(const ::clang::DeclContext *Val) { 774 return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val)); 775 } 776}; 777template<class ToTy> 778struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> 779 : public cast_convert_val<ToTy, 780 const ::clang::DeclContext*,const ::clang::DeclContext*> {}; 781 782} // end namespace llvm 783 784#endif 785