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