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