DeclBase.h revision 72c3f314d92d65c050ee1c07b7753623c044d6c7
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 21namespace clang { 22class DeclContext; 23class TranslationUnitDecl; 24class NamespaceDecl; 25class ScopedDecl; 26class FunctionDecl; 27class CXXRecordDecl; 28class EnumDecl; 29class ObjCMethodDecl; 30class ObjCInterfaceDecl; 31class BlockDecl; 32 33/// Decl - This represents one declaration (or definition), e.g. a variable, 34/// typedef, function, struct, etc. 35/// 36class Decl { 37public: 38 enum Kind { 39 // This lists the concrete classes of Decl in order of the inheritance 40 // hierarchy. This allows us to do efficient classof tests based on the 41 // enums below. The commented out names are abstract class names. 42 // [DeclContext] indicates that the class also inherits from DeclContext. 43 44 // Decl 45 TranslationUnit, // [DeclContext] 46 // NamedDecl 47 Field, 48 CXXField, 49 ObjCIvar, 50 ObjCAtDefsField, 51 OverloadedFunction, 52 ObjCCategory, 53 ObjCCategoryImpl, 54 ObjCImplementation, 55 ObjCMethod, // [DeclContext] 56 ObjCProtocol, 57 ObjCProperty, 58 // ScopedDecl 59 Namespace, // [DeclContext] 60 // TypeDecl 61 Typedef, 62 // TagDecl 63 Enum, // [DeclContext] 64 Record, 65 CXXRecord, // [DeclContext] 66 TemplateTypeParm, 67 // ValueDecl 68 EnumConstant, 69 Function, // [DeclContext] 70 CXXMethod, 71 CXXConstructor, 72 CXXDestructor, 73 CXXConversion, 74 Var, 75 ImplicitParam, 76 CXXClassVar, 77 ParmVar, 78 NonTypeTemplateParm, 79 ObjCInterface, // [DeclContext] 80 ObjCCompatibleAlias, 81 ObjCClass, 82 ObjCForwardProtocol, 83 ObjCPropertyImpl, 84 LinkageSpec, 85 FileScopeAsm, 86 Block, // [DeclContext] 87 88 // For each non-leaf class, we now define a mapping to the first/last member 89 // of the class, to allow efficient classof. 90 NamedFirst = Field , NamedLast = NonTypeTemplateParm, 91 FieldFirst = Field , FieldLast = ObjCAtDefsField, 92 ScopedFirst = Namespace , ScopedLast = NonTypeTemplateParm, 93 TypeFirst = Typedef , TypeLast = TemplateTypeParm, 94 TagFirst = Enum , TagLast = CXXRecord, 95 RecordFirst = Record , RecordLast = CXXRecord, 96 ValueFirst = EnumConstant , ValueLast = NonTypeTemplateParm, 97 FunctionFirst = Function , FunctionLast = CXXConversion, 98 VarFirst = Var , VarLast = NonTypeTemplateParm 99 }; 100 101 /// IdentifierNamespace - According to C99 6.2.3, there are four namespaces, 102 /// labels, tags, members and ordinary identifiers. These are meant 103 /// as bitmasks, so that searches in C++ can look into the "tag" namespace 104 /// during ordinary lookup. 105 enum IdentifierNamespace { 106 IDNS_Label = 0x1, 107 IDNS_Tag = 0x2, 108 IDNS_Member = 0x4, 109 IDNS_Ordinary = 0x8 110 }; 111 112 /// ObjCDeclQualifier - Qualifier used on types in method declarations 113 /// for remote messaging. They are meant for the arguments though and 114 /// applied to the Decls (ObjCMethodDecl and ParmVarDecl). 115 enum ObjCDeclQualifier { 116 OBJC_TQ_None = 0x0, 117 OBJC_TQ_In = 0x1, 118 OBJC_TQ_Inout = 0x2, 119 OBJC_TQ_Out = 0x4, 120 OBJC_TQ_Bycopy = 0x8, 121 OBJC_TQ_Byref = 0x10, 122 OBJC_TQ_Oneway = 0x20 123 }; 124 125private: 126 /// Loc - The location that this decl. 127 SourceLocation Loc; 128 129 /// DeclKind - This indicates which class this is. 130 Kind DeclKind : 8; 131 132 /// InvalidDecl - This indicates a semantic error occurred. 133 unsigned int InvalidDecl : 1; 134 135 /// HasAttrs - This indicates whether the decl has attributes or not. 136 unsigned int HasAttrs : 1; 137 138 protected: 139 /// Access - Used by C++ decls for the access specifier. 140 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum 141 unsigned Access : 2; 142 friend class CXXClassMemberWrapper; 143 144 Decl(Kind DK, SourceLocation L) : Loc(L), DeclKind(DK), InvalidDecl(0), 145 HasAttrs(false) { 146 if (Decl::CollectingStats()) addDeclKind(DK); 147 } 148 149 virtual ~Decl(); 150 151public: 152 SourceLocation getLocation() const { return Loc; } 153 void setLocation(SourceLocation L) { Loc = L; } 154 155 Kind getKind() const { return DeclKind; } 156 const char *getDeclKindName() const; 157 158 void addAttr(Attr *attr); 159 const Attr *getAttrs() const; 160 void swapAttrs(Decl *D); 161 void invalidateAttrs(); 162 163 template<typename T> const T *getAttr() const { 164 for (const Attr *attr = getAttrs(); attr; attr = attr->getNext()) 165 if (const T *V = dyn_cast<T>(attr)) 166 return V; 167 168 return 0; 169 } 170 171 /// setInvalidDecl - Indicates the Decl had a semantic error. This 172 /// allows for graceful error recovery. 173 void setInvalidDecl() { InvalidDecl = 1; } 174 bool isInvalidDecl() const { return (bool) InvalidDecl; } 175 176 IdentifierNamespace getIdentifierNamespace() const { 177 switch (DeclKind) { 178 default: assert(0 && "Unknown decl kind!"); 179 case ImplicitParam: 180 case Typedef: 181 case Function: 182 case Var: 183 case ParmVar: 184 case EnumConstant: 185 case NonTypeTemplateParm: 186 case ObjCInterface: 187 case ObjCCompatibleAlias: 188 case OverloadedFunction: 189 case CXXField: 190 case CXXMethod: 191 case CXXConversion: 192 case CXXClassVar: 193 return IDNS_Ordinary; 194 case Record: 195 case CXXRecord: 196 case TemplateTypeParm: 197 case Enum: 198 return IDNS_Tag; 199 case Namespace: 200 return IdentifierNamespace(IDNS_Tag | IDNS_Ordinary); 201 } 202 } 203 204 // getBody - If this Decl represents a declaration for a body of code, 205 // such as a function or method definition, this method returns the top-level 206 // Stmt* of that body. Otherwise this method returns null. 207 virtual Stmt* getBody() const { return 0; } 208 209 // global temp stats (until we have a per-module visitor) 210 static void addDeclKind(Kind k); 211 static bool CollectingStats(bool Enable = false); 212 static void PrintStats(); 213 214 // Implement isa/cast/dyncast/etc. 215 static bool classof(const Decl *) { return true; } 216 static DeclContext *castToDeclContext(const Decl *); 217 static Decl *castFromDeclContext(const DeclContext *); 218 219 /// Emit - Serialize this Decl to Bitcode. 220 void Emit(llvm::Serializer& S) const; 221 222 /// Create - Deserialize a Decl from Bitcode. 223 static Decl* Create(llvm::Deserializer& D, ASTContext& C); 224 225 /// Destroy - Call destructors and release memory. 226 virtual void Destroy(ASTContext& C); 227 228protected: 229 /// EmitImpl - Provides the subclass-specific serialization logic for 230 /// serializing out a decl. 231 virtual void EmitImpl(llvm::Serializer& S) const { 232 // FIXME: This will eventually be a pure virtual function. 233 assert (false && "Not implemented."); 234 } 235 236 void EmitInRec(llvm::Serializer& S) const; 237 void ReadInRec(llvm::Deserializer& D, ASTContext& C); 238}; 239 240/// DeclContext - This is used only as base class of specific decl types that 241/// can act as declaration contexts. These decls are: 242/// 243/// TranslationUnitDecl 244/// NamespaceDecl 245/// FunctionDecl 246/// CXXRecordDecl 247/// EnumDecl 248/// ObjCMethodDecl 249/// ObjCInterfaceDecl 250/// BlockDecl 251/// 252class DeclContext { 253 /// DeclKind - This indicates which class this is. 254 Decl::Kind DeclKind : 8; 255 256 /// DeclChain - Linked list of declarations that are defined inside this 257 /// declaration context. 258 ScopedDecl *DeclChain; 259 260 // Used in the CastTo template to get the DeclKind 261 // from a Decl or a DeclContext. DeclContext doesn't have a getKind() method 262 // to avoid 'ambiguous access' compiler errors. 263 template<typename T> struct KindTrait { 264 static Decl::Kind getKind(const T *D) { return D->getKind(); } 265 }; 266 267 // Used only by the ToDecl and FromDecl methods 268 template<typename To, typename From> 269 static To *CastTo(const From *D) { 270 Decl::Kind DK = KindTrait<From>::getKind(D); 271 switch(DK) { 272 case Decl::Block: 273 return static_cast<BlockDecl*>(const_cast<From*>(D)); 274 case Decl::TranslationUnit: 275 return static_cast<TranslationUnitDecl*>(const_cast<From*>(D)); 276 case Decl::Namespace: 277 return static_cast<NamespaceDecl*>(const_cast<From*>(D)); 278 case Decl::Enum: 279 return static_cast<EnumDecl*>(const_cast<From*>(D)); 280 case Decl::CXXRecord: 281 return static_cast<CXXRecordDecl*>(const_cast<From*>(D)); 282 case Decl::ObjCMethod: 283 return static_cast<ObjCMethodDecl*>(const_cast<From*>(D)); 284 case Decl::ObjCInterface: 285 return static_cast<ObjCInterfaceDecl*>(const_cast<From*>(D)); 286 default: 287 if (DK >= Decl::FunctionFirst && DK <= Decl::FunctionLast) 288 return static_cast<FunctionDecl*>(const_cast<From*>(D)); 289 290 assert(false && "a decl that inherits DeclContext isn't handled"); 291 return 0; 292 } 293 } 294 295protected: 296 DeclContext(Decl::Kind K) : DeclKind(K), DeclChain(0) {} 297 298public: 299 /// getParent - Returns the containing DeclContext if this is a ScopedDecl, 300 /// else returns NULL. 301 const DeclContext *getParent() const; 302 DeclContext *getParent() { 303 return const_cast<DeclContext*>( 304 const_cast<const DeclContext*>(this)->getParent()); 305 } 306 307 /// getLexicalParent - Returns the containing lexical DeclContext. May be 308 /// different from getParent, e.g.: 309 /// 310 /// namespace A { 311 /// struct S; 312 /// } 313 /// struct A::S {}; // getParent() == namespace 'A' 314 /// // getLexicalParent() == translation unit 315 /// 316 const DeclContext *getLexicalParent() const; 317 DeclContext *getLexicalParent() { 318 return const_cast<DeclContext*>( 319 const_cast<const DeclContext*>(this)->getLexicalParent()); 320 } 321 322 bool isFunctionOrMethod() const { 323 switch (DeclKind) { 324 case Decl::Block: 325 case Decl::Function: 326 case Decl::CXXMethod: 327 case Decl::ObjCMethod: 328 return true; 329 default: 330 return false; 331 } 332 } 333 334 bool isFileContext() const { 335 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; 336 } 337 338 bool isCXXRecord() const { 339 return DeclKind == Decl::CXXRecord; 340 } 341 342 bool Encloses(DeclContext *DC) const { 343 for (; DC; DC = DC->getParent()) 344 if (DC == this) 345 return true; 346 return false; 347 } 348 349 const ScopedDecl *getDeclChain() const { return DeclChain; } 350 ScopedDecl *getDeclChain() { return DeclChain; } 351 void setDeclChain(ScopedDecl *D) { DeclChain = D; } 352 353 static bool classof(const Decl *D) { 354 switch (D->getKind()) { 355 case Decl::TranslationUnit: 356 case Decl::Namespace: 357 case Decl::Enum: 358 case Decl::CXXRecord: 359 case Decl::ObjCMethod: 360 case Decl::ObjCInterface: 361 case Decl::Block: 362 return true; 363 default: 364 if (D->getKind() >= Decl::FunctionFirst && 365 D->getKind() <= Decl::FunctionLast) 366 return true; 367 return false; 368 } 369 } 370 static bool classof(const DeclContext *D) { return true; } 371 static bool classof(const TranslationUnitDecl *D) { return true; } 372 static bool classof(const NamespaceDecl *D) { return true; } 373 static bool classof(const FunctionDecl *D) { return true; } 374 static bool classof(const CXXRecordDecl *D) { return true; } 375 static bool classof(const EnumDecl *D) { return true; } 376 static bool classof(const ObjCMethodDecl *D) { return true; } 377 static bool classof(const ObjCInterfaceDecl *D) { return true; } 378 static bool classof(const BlockDecl *D) { return true; } 379 380private: 381 void EmitOutRec(llvm::Serializer& S) const; 382 void ReadOutRec(llvm::Deserializer& D, ASTContext& C); 383 384 friend class Decl; 385}; 386 387template<> struct DeclContext::KindTrait<DeclContext> { 388 static Decl::Kind getKind(const DeclContext *D) { return D->DeclKind; } 389}; 390 391} // end clang. 392 393namespace llvm { 394 395/// Implement a isa_impl_wrap specialization to check whether a DeclContext is 396/// a specific Decl. 397template<class ToTy> 398struct isa_impl_wrap<ToTy, 399 const ::clang::DeclContext,const ::clang::DeclContext> { 400 static bool doit(const ::clang::DeclContext &Val) { 401 return ToTy::classof(::clang::Decl::castFromDeclContext(&Val)); 402 } 403}; 404template<class ToTy> 405struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext> 406 : public isa_impl_wrap<ToTy, 407 const ::clang::DeclContext,const ::clang::DeclContext> {}; 408 409/// Implement cast_convert_val for Decl -> DeclContext conversions. 410template<class FromTy> 411struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { 412 static ::clang::DeclContext &doit(const FromTy &Val) { 413 return *FromTy::castToDeclContext(&Val); 414 } 415}; 416 417template<class FromTy> 418struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { 419 static ::clang::DeclContext *doit(const FromTy *Val) { 420 return FromTy::castToDeclContext(Val); 421 } 422}; 423 424/// Implement cast_convert_val for DeclContext -> Decl conversions. 425template<class ToTy> 426struct cast_convert_val<ToTy, 427 const ::clang::DeclContext,const ::clang::DeclContext> { 428 static ToTy &doit(const ::clang::DeclContext &Val) { 429 return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val)); 430 } 431}; 432template<class ToTy> 433struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> 434 : public cast_convert_val<ToTy, 435 const ::clang::DeclContext,const ::clang::DeclContext> {}; 436 437template<class ToTy> 438struct cast_convert_val<ToTy, 439 const ::clang::DeclContext*, const ::clang::DeclContext*> { 440 static ToTy *doit(const ::clang::DeclContext *Val) { 441 return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val)); 442 } 443}; 444template<class ToTy> 445struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> 446 : public cast_convert_val<ToTy, 447 const ::clang::DeclContext*,const ::clang::DeclContext*> {}; 448 449} // end namespace llvm 450 451#endif 452