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