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