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