DeclBase.h revision 074149e11baf5f7db12f84efd5c34ba6e35d5cdf
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 // getBody - If this Decl represents a declaration for a body of code, 215 // such as a function or method definition, this method returns the top-level 216 // Stmt* of that body. Otherwise this method returns null. 217 virtual Stmt* getBody() const { return 0; } 218 219 // global temp stats (until we have a per-module visitor) 220 static void addDeclKind(Kind k); 221 static bool CollectingStats(bool Enable = false); 222 static void PrintStats(); 223 224 /// isTemplateParameter - Determines whether this declartion is a 225 /// template parameter. 226 bool isTemplateParameter() const; 227 228 // Implement isa/cast/dyncast/etc. 229 static bool classof(const Decl *) { return true; } 230 static DeclContext *castToDeclContext(const Decl *); 231 static Decl *castFromDeclContext(const DeclContext *); 232 233 /// Emit - Serialize this Decl to Bitcode. 234 void Emit(llvm::Serializer& S) const; 235 236 /// Create - Deserialize a Decl from Bitcode. 237 static Decl* Create(llvm::Deserializer& D, ASTContext& C); 238 239 /// Destroy - Call destructors and release memory. 240 virtual void Destroy(ASTContext& C); 241 242protected: 243 /// EmitImpl - Provides the subclass-specific serialization logic for 244 /// serializing out a decl. 245 virtual void EmitImpl(llvm::Serializer& S) const { 246 // FIXME: This will eventually be a pure virtual function. 247 assert (false && "Not implemented."); 248 } 249 250 void EmitInRec(llvm::Serializer& S) const; 251 void ReadInRec(llvm::Deserializer& D, ASTContext& C); 252}; 253 254/// DeclContext - This is used only as base class of specific decl types that 255/// can act as declaration contexts. These decls are: 256/// 257/// TranslationUnitDecl 258/// NamespaceDecl 259/// FunctionDecl 260/// RecordDecl/CXXRecordDecl 261/// EnumDecl 262/// ObjCMethodDecl 263/// ObjCInterfaceDecl 264/// LinkageSpecDecl 265/// BlockDecl 266class DeclContext { 267 /// DeclKind - This indicates which class this is. 268 Decl::Kind DeclKind : 8; 269 270 /// LookupPtrKind - Describes what kind of pointer LookupPtr 271 /// actually is. 272 enum LookupPtrKind { 273 /// LookupIsMap - Indicates that LookupPtr is actually a map. 274 LookupIsMap = 7 275 }; 276 277 /// LookupPtr - Pointer to a data structure used to lookup 278 /// declarations within this context. If the context contains fewer 279 /// than seven declarations, the number of declarations is provided 280 /// in the 3 lowest-order bits and the upper bits are treated as a 281 /// pointer to an array of ScopedDecl pointers. If the context 282 /// contains seven or more declarations, the upper bits are treated 283 /// as a pointer to a DenseMap<DeclarationName, std::vector<ScopedDecl>>. 284 /// FIXME: We need a better data structure for this. 285 llvm::PointerIntPair<void*, 3> LookupPtr; 286 287 /// Decls - Contains all of the declarations that are defined inside 288 /// this declaration context. 289 std::vector<ScopedDecl*> Decls; 290 291 // Used in the CastTo template to get the DeclKind 292 // from a Decl or a DeclContext. DeclContext doesn't have a getKind() method 293 // to avoid 'ambiguous access' compiler errors. 294 template<typename T> struct KindTrait { 295 static Decl::Kind getKind(const T *D) { return D->getKind(); } 296 }; 297 298 // Used only by the ToDecl and FromDecl methods 299 template<typename To, typename From> 300 static To *CastTo(const From *D) { 301 Decl::Kind DK = KindTrait<From>::getKind(D); 302 switch(DK) { 303 case Decl::TranslationUnit: 304 return static_cast<TranslationUnitDecl*>(const_cast<From*>(D)); 305 case Decl::Namespace: 306 return static_cast<NamespaceDecl*>(const_cast<From*>(D)); 307 case Decl::Enum: 308 return static_cast<EnumDecl*>(const_cast<From*>(D)); 309 case Decl::Record: 310 return static_cast<RecordDecl*>(const_cast<From*>(D)); 311 case Decl::CXXRecord: 312 return static_cast<CXXRecordDecl*>(const_cast<From*>(D)); 313 case Decl::ObjCMethod: 314 return static_cast<ObjCMethodDecl*>(const_cast<From*>(D)); 315 case Decl::ObjCInterface: 316 return static_cast<ObjCInterfaceDecl*>(const_cast<From*>(D)); 317 case Decl::LinkageSpec: 318 return static_cast<LinkageSpecDecl*>(const_cast<From*>(D)); 319 case Decl::Block: 320 return static_cast<BlockDecl*>(const_cast<From*>(D)); 321 default: 322 if (DK >= Decl::FunctionFirst && DK <= Decl::FunctionLast) 323 return static_cast<FunctionDecl*>(const_cast<From*>(D)); 324 325 assert(false && "a decl that inherits DeclContext isn't handled"); 326 return 0; 327 } 328 } 329 330 /// isLookupMap - Determine if the lookup structure is a 331 /// DenseMap. Othewise, it is an array. 332 bool isLookupMap() const { return LookupPtr.getInt() == LookupIsMap; } 333 334protected: 335 DeclContext(Decl::Kind K) : DeclKind(K), LookupPtr() { 336 } 337 338 void DestroyDecls(ASTContext &C); 339 340public: 341 ~DeclContext(); 342 343 /// getParent - Returns the containing DeclContext if this is a ScopedDecl, 344 /// else returns NULL. 345 const DeclContext *getParent() const; 346 DeclContext *getParent() { 347 return const_cast<DeclContext*>( 348 const_cast<const DeclContext*>(this)->getParent()); 349 } 350 351 /// getLexicalParent - Returns the containing lexical DeclContext. May be 352 /// different from getParent, e.g.: 353 /// 354 /// namespace A { 355 /// struct S; 356 /// } 357 /// struct A::S {}; // getParent() == namespace 'A' 358 /// // getLexicalParent() == translation unit 359 /// 360 const DeclContext *getLexicalParent() const; 361 DeclContext *getLexicalParent() { 362 return const_cast<DeclContext*>( 363 const_cast<const DeclContext*>(this)->getLexicalParent()); 364 } 365 366 bool isFunctionOrMethod() const { 367 switch (DeclKind) { 368 case Decl::Block: 369 case Decl::ObjCMethod: 370 return true; 371 372 default: 373 if (DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast) 374 return true; 375 return false; 376 } 377 } 378 379 bool isFileContext() const { 380 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; 381 } 382 383 bool isCXXRecord() const { 384 return DeclKind == Decl::CXXRecord; 385 } 386 387 bool isNamespace() const { 388 return DeclKind == Decl::Namespace; 389 } 390 391 /// isTransparentContext - Determines whether this context is a 392 /// "transparent" context, meaning that the members declared in this 393 /// context are semantically declared in the nearest enclosing 394 /// non-transparent (opaque) context but are lexically declared in 395 /// this context. For example, consider the enumerators of an 396 /// enumeration type: 397 /// @code 398 /// enum E { 399 /// Val1 400 /// }; 401 /// @endcode 402 /// Here, E is a transparent context, so its enumerator (Val1) will 403 /// appear (semantically) that it is in the same context of E. 404 /// Examples of transparent contexts include: enumerations (except for 405 /// C++0x scoped enums), C++ linkage specifications, and C++0x 406 /// inline namespaces. 407 bool isTransparentContext() const; 408 409 bool Encloses(DeclContext *DC) const { 410 for (; DC; DC = DC->getParent()) 411 if (DC == this) 412 return true; 413 return false; 414 } 415 416 /// getPrimaryContext - There may be many different 417 /// declarations of the same entity (including forward declarations 418 /// of classes, multiple definitions of namespaces, etc.), each with 419 /// a different set of declarations. This routine returns the 420 /// "primary" DeclContext structure, which will contain the 421 /// information needed to perform name lookup into this context. 422 DeclContext *getPrimaryContext(ASTContext &Context); 423 424 /// getNextContext - If this is a DeclContext that may have other 425 /// DeclContexts that are semantically connected but syntactically 426 /// different, such as C++ namespaces, this routine retrieves the 427 /// next DeclContext in the link. Iteration through the chain of 428 /// DeclContexts should begin at the primary DeclContext and 429 /// continue until this function returns NULL. For example, given: 430 /// @code 431 /// namespace N { 432 /// int x; 433 /// } 434 /// namespace N { 435 /// int y; 436 /// } 437 /// @endcode 438 /// The first occurrence of namespace N will be the primary 439 /// DeclContext. Its getNextContext will return the second 440 /// occurrence of namespace N. 441 DeclContext *getNextContext(); 442 443 /// decl_iterator - Iterates through the declarations stored 444 /// within this context. 445 typedef std::vector<ScopedDecl*>::const_iterator decl_iterator; 446 447 /// decls_begin/decls_end - Iterate over the declarations stored in 448 /// this context. 449 decl_iterator decls_begin() const { return Decls.begin(); } 450 decl_iterator decls_end() const { return Decls.end(); } 451 452 /// addDecl - Add the declaration D to this scope. Note that 453 /// declarations are added at the beginning of the declaration 454 /// chain, so reverseDeclChain() should be called after all 455 /// declarations have been added. If AllowLookup, also adds this 456 /// declaration into data structure for name lookup. 457 void addDecl(ASTContext &Context, ScopedDecl *D, bool AllowLookup = true); 458 459 void buildLookup(ASTContext &Context, DeclContext *DCtx); 460 461 /// lookup_iterator - An iterator that provides access to the results 462 /// of looking up a name within this context. 463 typedef ScopedDecl **lookup_iterator; 464 465 /// lookup_const_iterator - An iterator that provides non-mutable 466 /// access to the results of lookup up a name within this context. 467 typedef ScopedDecl * const * lookup_const_iterator; 468 469 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result; 470 typedef std::pair<lookup_const_iterator, lookup_const_iterator> 471 lookup_const_result; 472 473 /// lookup - Find the declarations (if any) with the given Name in 474 /// this context. Returns a range of iterators that contains all of 475 /// the declarations with this name (which may be 0, 1, or 2 476 /// declarations). If two declarations are returned, the declaration 477 /// in the "ordinary" identifier namespace will precede the 478 /// declaration in the "tag" identifier namespace (e.g., values 479 /// before types). Note that this routine will not look into parent 480 /// contexts. 481 lookup_result lookup(ASTContext &Context, DeclarationName Name); 482 lookup_const_result lookup(ASTContext &Context, DeclarationName Name) const; 483 484 /// insert - Insert the declaration D into this context. Up to two 485 /// declarations with the same name can be inserted into a single 486 /// declaration context, one in the "tag" namespace (e.g., for 487 /// classes and enums) and one in the "ordinary" namespaces (e.g., 488 /// for variables, functions, and other values). Note that, if there 489 /// is already a declaration with the same name and identifier 490 /// namespace, D will replace it. It is up to the caller to ensure 491 /// that this replacement is semantically correct, e.g., that 492 /// declarations are only replaced by later declarations of the same 493 /// entity and not a declaration of some other kind of entity. 494 void insert(ASTContext &Context, ScopedDecl *D); 495 496 static bool classof(const Decl *D) { 497 switch (D->getKind()) { 498 case Decl::TranslationUnit: 499 case Decl::Namespace: 500 case Decl::Enum: 501 case Decl::Record: 502 case Decl::CXXRecord: 503 case Decl::ObjCMethod: 504 case Decl::ObjCInterface: 505 case Decl::LinkageSpec: 506 case Decl::Block: 507 return true; 508 default: 509 if (D->getKind() >= Decl::FunctionFirst && 510 D->getKind() <= Decl::FunctionLast) 511 return true; 512 return false; 513 } 514 } 515 static bool classof(const DeclContext *D) { return true; } 516 static bool classof(const TranslationUnitDecl *D) { return true; } 517 static bool classof(const NamespaceDecl *D) { return true; } 518 static bool classof(const FunctionDecl *D) { return true; } 519 static bool classof(const RecordDecl *D) { return true; } 520 static bool classof(const CXXRecordDecl *D) { return true; } 521 static bool classof(const EnumDecl *D) { return true; } 522 static bool classof(const ObjCMethodDecl *D) { return true; } 523 static bool classof(const ObjCInterfaceDecl *D) { return true; } 524 static bool classof(const LinkageSpecDecl *D) { return true; } 525 static bool classof(const BlockDecl *D) { return true; } 526 527private: 528 void insertImpl(ScopedDecl *D); 529 530 void EmitOutRec(llvm::Serializer& S) const; 531 void ReadOutRec(llvm::Deserializer& D, ASTContext& C); 532 533 friend class Decl; 534}; 535 536template<> struct DeclContext::KindTrait<DeclContext> { 537 static Decl::Kind getKind(const DeclContext *D) { return D->DeclKind; } 538}; 539 540inline bool Decl::isTemplateParameter() const { 541 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm; 542} 543 544 545} // end clang. 546 547namespace llvm { 548 549/// Implement a isa_impl_wrap specialization to check whether a DeclContext is 550/// a specific Decl. 551template<class ToTy> 552struct isa_impl_wrap<ToTy, 553 const ::clang::DeclContext,const ::clang::DeclContext> { 554 static bool doit(const ::clang::DeclContext &Val) { 555 return ToTy::classof(::clang::Decl::castFromDeclContext(&Val)); 556 } 557}; 558template<class ToTy> 559struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext> 560 : public isa_impl_wrap<ToTy, 561 const ::clang::DeclContext,const ::clang::DeclContext> {}; 562 563/// Implement cast_convert_val for Decl -> DeclContext conversions. 564template<class FromTy> 565struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { 566 static ::clang::DeclContext &doit(const FromTy &Val) { 567 return *FromTy::castToDeclContext(&Val); 568 } 569}; 570 571template<class FromTy> 572struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { 573 static ::clang::DeclContext *doit(const FromTy *Val) { 574 return FromTy::castToDeclContext(Val); 575 } 576}; 577 578template<class FromTy> 579struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { 580 static const ::clang::DeclContext &doit(const FromTy &Val) { 581 return *FromTy::castToDeclContext(&Val); 582 } 583}; 584 585template<class FromTy> 586struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { 587 static const ::clang::DeclContext *doit(const FromTy *Val) { 588 return FromTy::castToDeclContext(Val); 589 } 590}; 591 592/// Implement cast_convert_val for DeclContext -> Decl conversions. 593template<class ToTy> 594struct cast_convert_val<ToTy, 595 const ::clang::DeclContext,const ::clang::DeclContext> { 596 static ToTy &doit(const ::clang::DeclContext &Val) { 597 return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val)); 598 } 599}; 600template<class ToTy> 601struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> 602 : public cast_convert_val<ToTy, 603 const ::clang::DeclContext,const ::clang::DeclContext> {}; 604 605template<class ToTy> 606struct cast_convert_val<ToTy, 607 const ::clang::DeclContext*, const ::clang::DeclContext*> { 608 static ToTy *doit(const ::clang::DeclContext *Val) { 609 return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val)); 610 } 611}; 612template<class ToTy> 613struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> 614 : public cast_convert_val<ToTy, 615 const ::clang::DeclContext*,const ::clang::DeclContext*> {}; 616 617} // end namespace llvm 618 619#endif 620