DeclBase.cpp revision d2595ecce5f8350e485c83bfe767549a522b2802
1//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===// 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 implements the Decl and DeclContext classes. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/AST/DeclBase.h" 15#include "clang/AST/DeclObjC.h" 16#include "clang/AST/DeclCXX.h" 17#include "clang/AST/ASTContext.h" 18#include "llvm/ADT/DenseMap.h" 19using namespace clang; 20 21//===----------------------------------------------------------------------===// 22// Statistics 23//===----------------------------------------------------------------------===// 24 25// temporary statistics gathering 26static unsigned nFuncs = 0; 27static unsigned nVars = 0; 28static unsigned nParmVars = 0; 29static unsigned nSUC = 0; 30static unsigned nCXXSUC = 0; 31static unsigned nEnumConst = 0; 32static unsigned nEnumDecls = 0; 33static unsigned nNamespaces = 0; 34static unsigned nTypedef = 0; 35static unsigned nFieldDecls = 0; 36static unsigned nCXXFieldDecls = 0; 37static unsigned nInterfaceDecls = 0; 38static unsigned nClassDecls = 0; 39static unsigned nMethodDecls = 0; 40static unsigned nProtocolDecls = 0; 41static unsigned nForwardProtocolDecls = 0; 42static unsigned nCategoryDecls = 0; 43static unsigned nIvarDecls = 0; 44static unsigned nAtDefsFieldDecls = 0; 45static unsigned nObjCImplementationDecls = 0; 46static unsigned nObjCCategoryImpl = 0; 47static unsigned nObjCCompatibleAlias = 0; 48static unsigned nObjCPropertyDecl = 0; 49static unsigned nObjCPropertyImplDecl = 0; 50static unsigned nLinkageSpecDecl = 0; 51static unsigned nFileScopeAsmDecl = 0; 52static unsigned nBlockDecls = 0; 53 54static bool StatSwitch = false; 55 56// This keeps track of all decl attributes. Since so few decls have attrs, we 57// keep them in a hash map instead of wasting space in the Decl class. 58typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy; 59 60static DeclAttrMapTy *DeclAttrs = 0; 61 62const char *Decl::getDeclKindName() const { 63 switch (DeclKind) { 64 default: assert(0 && "Unknown decl kind!"); 65 case Namespace: return "Namespace"; 66 case Typedef: return "Typedef"; 67 case Function: return "Function"; 68 case Var: return "Var"; 69 case ParmVar: return "ParmVar"; 70 case EnumConstant: return "EnumConstant"; 71 case ObjCIvar: return "ObjCIvar"; 72 case ObjCInterface: return "ObjCInterface"; 73 case ObjCClass: return "ObjCClass"; 74 case ObjCMethod: return "ObjCMethod"; 75 case ObjCProtocol: return "ObjCProtocol"; 76 case ObjCForwardProtocol: return "ObjCForwardProtocol"; 77 case Struct: return "Struct"; 78 case Union: return "Union"; 79 case Class: return "Class"; 80 case Enum: return "Enum"; 81 case Block: return "Block"; 82 } 83} 84 85bool Decl::CollectingStats(bool Enable) { 86 if (Enable) 87 StatSwitch = true; 88 return StatSwitch; 89} 90 91void Decl::PrintStats() { 92 fprintf(stderr, "*** Decl Stats:\n"); 93 fprintf(stderr, " %d decls total.\n", 94 int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+nCXXFieldDecls+nCXXSUC+ 95 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+ 96 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+ 97 nAtDefsFieldDecls+nNamespaces)); 98 fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n", 99 nNamespaces, (int)sizeof(NamespaceDecl), 100 int(nNamespaces*sizeof(NamespaceDecl))); 101 fprintf(stderr, " %d function decls, %d each (%d bytes)\n", 102 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl))); 103 fprintf(stderr, " %d variable decls, %d each (%d bytes)\n", 104 nVars, (int)sizeof(VarDecl), 105 int(nVars*sizeof(VarDecl))); 106 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n", 107 nParmVars, (int)sizeof(ParmVarDecl), 108 int(nParmVars*sizeof(ParmVarDecl))); 109 fprintf(stderr, " %d field decls, %d each (%d bytes)\n", 110 nFieldDecls, (int)sizeof(FieldDecl), 111 int(nFieldDecls*sizeof(FieldDecl))); 112 fprintf(stderr, " %d @defs generated field decls, %d each (%d bytes)\n", 113 nAtDefsFieldDecls, (int)sizeof(ObjCAtDefsFieldDecl), 114 int(nAtDefsFieldDecls*sizeof(ObjCAtDefsFieldDecl))); 115 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n", 116 nSUC, (int)sizeof(RecordDecl), 117 int(nSUC*sizeof(RecordDecl))); 118 fprintf(stderr, " %d C++ field decls, %d each (%d bytes)\n", 119 nCXXFieldDecls, (int)sizeof(CXXFieldDecl), 120 int(nCXXFieldDecls*sizeof(CXXFieldDecl))); 121 fprintf(stderr, " %d C++ struct/union/class decls, %d each (%d bytes)\n", 122 nCXXSUC, (int)sizeof(CXXRecordDecl), 123 int(nCXXSUC*sizeof(CXXRecordDecl))); 124 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n", 125 nEnumDecls, (int)sizeof(EnumDecl), 126 int(nEnumDecls*sizeof(EnumDecl))); 127 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n", 128 nEnumConst, (int)sizeof(EnumConstantDecl), 129 int(nEnumConst*sizeof(EnumConstantDecl))); 130 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n", 131 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl))); 132 // Objective-C decls... 133 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n", 134 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl), 135 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl))); 136 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n", 137 nIvarDecls, (int)sizeof(ObjCIvarDecl), 138 int(nIvarDecls*sizeof(ObjCIvarDecl))); 139 fprintf(stderr, " %d class decls, %d each (%d bytes)\n", 140 nClassDecls, (int)sizeof(ObjCClassDecl), 141 int(nClassDecls*sizeof(ObjCClassDecl))); 142 fprintf(stderr, " %d method decls, %d each (%d bytes)\n", 143 nMethodDecls, (int)sizeof(ObjCMethodDecl), 144 int(nMethodDecls*sizeof(ObjCMethodDecl))); 145 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n", 146 nProtocolDecls, (int)sizeof(ObjCProtocolDecl), 147 int(nProtocolDecls*sizeof(ObjCProtocolDecl))); 148 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n", 149 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl), 150 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl))); 151 fprintf(stderr, " %d category decls, %d each (%d bytes)\n", 152 nCategoryDecls, (int)sizeof(ObjCCategoryDecl), 153 int(nCategoryDecls*sizeof(ObjCCategoryDecl))); 154 155 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n", 156 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl), 157 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl))); 158 159 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n", 160 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl), 161 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl))); 162 163 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n", 164 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl), 165 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl))); 166 167 fprintf(stderr, " %d property decls, %d each (%d bytes)\n", 168 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl), 169 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl))); 170 171 fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n", 172 nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl), 173 int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl))); 174 175 fprintf(stderr, "Total bytes = %d\n", 176 int(nFuncs*sizeof(FunctionDecl)+ 177 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+ 178 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+ 179 nCXXFieldDecls*sizeof(CXXFieldDecl)+nCXXSUC*sizeof(CXXRecordDecl)+ 180 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+ 181 nTypedef*sizeof(TypedefDecl)+ 182 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+ 183 nIvarDecls*sizeof(ObjCIvarDecl)+ 184 nClassDecls*sizeof(ObjCClassDecl)+ 185 nMethodDecls*sizeof(ObjCMethodDecl)+ 186 nProtocolDecls*sizeof(ObjCProtocolDecl)+ 187 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+ 188 nCategoryDecls*sizeof(ObjCCategoryDecl)+ 189 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+ 190 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+ 191 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+ 192 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+ 193 nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+ 194 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+ 195 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+ 196 nNamespaces*sizeof(NamespaceDecl))); 197 198} 199 200void Decl::addDeclKind(Kind k) { 201 switch (k) { 202 case Namespace: nNamespaces++; break; 203 case Typedef: nTypedef++; break; 204 case Function: nFuncs++; break; 205 case Var: nVars++; break; 206 case ParmVar: nParmVars++; break; 207 case EnumConstant: nEnumConst++; break; 208 case Field: nFieldDecls++; break; 209 case Struct: case Union: case Class: nSUC++; break; 210 case Enum: nEnumDecls++; break; 211 case ObjCInterface: nInterfaceDecls++; break; 212 case ObjCClass: nClassDecls++; break; 213 case ObjCMethod: nMethodDecls++; break; 214 case ObjCProtocol: nProtocolDecls++; break; 215 case ObjCForwardProtocol: nForwardProtocolDecls++; break; 216 case ObjCCategory: nCategoryDecls++; break; 217 case ObjCIvar: nIvarDecls++; break; 218 case ObjCAtDefsField: nAtDefsFieldDecls++; break; 219 case ObjCImplementation: nObjCImplementationDecls++; break; 220 case ObjCCategoryImpl: nObjCCategoryImpl++; break; 221 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break; 222 case ObjCProperty: nObjCPropertyDecl++; break; 223 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break; 224 case LinkageSpec: nLinkageSpecDecl++; break; 225 case FileScopeAsm: nFileScopeAsmDecl++; break; 226 case Block: nBlockDecls++; break; 227 case ImplicitParam: 228 case TranslationUnit: break; 229 230 case CXXField: nCXXFieldDecls++; break; 231 case CXXStruct: case CXXUnion: case CXXClass: nCXXSUC++; break; 232 // FIXME: Statistics for C++ decls. 233 case CXXMethod: 234 case CXXClassVar: 235 break; 236 } 237} 238 239//===----------------------------------------------------------------------===// 240// Decl Implementation 241//===----------------------------------------------------------------------===// 242 243// Out-of-line virtual method providing a home for Decl. 244Decl::~Decl() { 245 if (!HasAttrs) 246 return; 247 248 DeclAttrMapTy::iterator it = DeclAttrs->find(this); 249 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!"); 250 251 // release attributes. 252 delete it->second; 253 invalidateAttrs(); 254} 255 256void Decl::addAttr(Attr *NewAttr) { 257 if (!DeclAttrs) 258 DeclAttrs = new DeclAttrMapTy(); 259 260 Attr *&ExistingAttr = (*DeclAttrs)[this]; 261 262 NewAttr->setNext(ExistingAttr); 263 ExistingAttr = NewAttr; 264 265 HasAttrs = true; 266} 267 268void Decl::invalidateAttrs() { 269 if (!HasAttrs) return; 270 271 HasAttrs = false; 272 (*DeclAttrs)[this] = 0; 273 DeclAttrs->erase(this); 274 275 if (DeclAttrs->empty()) { 276 delete DeclAttrs; 277 DeclAttrs = 0; 278 } 279} 280 281const Attr *Decl::getAttrs() const { 282 if (!HasAttrs) 283 return 0; 284 285 return (*DeclAttrs)[this]; 286} 287 288void Decl::swapAttrs(Decl *RHS) { 289 bool HasLHSAttr = this->HasAttrs; 290 bool HasRHSAttr = RHS->HasAttrs; 291 292 // Usually, neither decl has attrs, nothing to do. 293 if (!HasLHSAttr && !HasRHSAttr) return; 294 295 // If 'this' has no attrs, swap the other way. 296 if (!HasLHSAttr) 297 return RHS->swapAttrs(this); 298 299 // Handle the case when both decls have attrs. 300 if (HasRHSAttr) { 301 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]); 302 return; 303 } 304 305 // Otherwise, LHS has an attr and RHS doesn't. 306 (*DeclAttrs)[RHS] = (*DeclAttrs)[this]; 307 (*DeclAttrs).erase(this); 308 this->HasAttrs = false; 309 RHS->HasAttrs = true; 310} 311 312 313void Decl::Destroy(ASTContext& C) { 314 315 if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) { 316 317 // Observe the unrolled recursion. By setting N->NextDeclarator = 0x0 318 // within the loop, only the Destroy method for the first ScopedDecl 319 // will deallocate all of the ScopedDecls in a chain. 320 321 ScopedDecl* N = SD->getNextDeclarator(); 322 323 while (N) { 324 ScopedDecl* Tmp = N->getNextDeclarator(); 325 N->NextDeclarator = 0x0; 326 N->Destroy(C); 327 N = Tmp; 328 } 329 } 330 331 this->~Decl(); 332 C.getAllocator().Deallocate((void *)this); 333} 334 335Decl *Decl::castFromDeclContext (const DeclContext *D) { 336 return DeclContext::CastTo<Decl>(D); 337} 338 339DeclContext *Decl::castToDeclContext(const Decl *D) { 340 return DeclContext::CastTo<DeclContext>(D); 341} 342 343//===----------------------------------------------------------------------===// 344// DeclContext Implementation 345//===----------------------------------------------------------------------===// 346 347DeclContext *DeclContext::getParent() { 348 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this)) 349 return SD->getDeclContext(); 350 else if (BlockDecl *BD = dyn_cast<BlockDecl>(this)) 351 return BD->getParentContext(); 352 else 353 return NULL; 354} 355