Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Decl.cpp - Declaration AST Node Implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Decl class and subclasses. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Decl.h" |
| 15 | #include "clang/Lex/IdentifierTable.h" |
| 16 | using namespace clang; |
| 17 | |
| 18 | // temporary statistics gathering |
| 19 | static unsigned nFuncs = 0; |
| 20 | static unsigned nBlockVars = 0; |
| 21 | static unsigned nFileVars = 0; |
| 22 | static unsigned nParmVars = 0; |
| 23 | static unsigned nSUC = 0; |
| 24 | static unsigned nEnumConst = 0; |
| 25 | static unsigned nEnumDecls = 0; |
| 26 | static unsigned nTypedef = 0; |
| 27 | static unsigned nFieldDecls = 0; |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 28 | static unsigned nInterfaceDecls = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 29 | static unsigned nClassDecls = 0; |
| 30 | static unsigned nMethodDecls = 0; |
| 31 | static unsigned nProtocolDecls = 0; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame^] | 32 | static unsigned nCategoryDecls = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 33 | static unsigned nIvarDecls = 0; |
| 34 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | static bool StatSwitch = false; |
| 36 | |
Steve Naroff | e5ea380 | 2007-09-17 14:49:06 +0000 | [diff] [blame] | 37 | const char *Decl::getDeclKindName() const { |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 38 | switch (DeclKind) { |
| 39 | default: assert(0 && "Unknown decl kind!"); |
| 40 | case Typedef: |
| 41 | return "Typedef"; |
| 42 | case Function: |
| 43 | return "Function"; |
| 44 | case BlockVariable: |
| 45 | return "BlockVariable"; |
| 46 | case FileVariable: |
| 47 | return "FileVariable"; |
| 48 | case ParmVariable: |
| 49 | return "ParmVariable"; |
| 50 | case EnumConstant: |
| 51 | return "EnumConstant"; |
| 52 | case ObjcInterface: |
| 53 | return "ObjcInterface"; |
| 54 | case ObjcClass: |
| 55 | return "ObjcClass"; |
| 56 | case ObjcMethod: |
| 57 | return "ObjcMethod"; |
| 58 | case ObjcProtoMethod: |
| 59 | return "ObjcProtoMethod"; |
| 60 | case ObjcProtocol: |
| 61 | return "ObjcProtocol"; |
| 62 | case Struct: |
| 63 | return "Struct"; |
| 64 | case Union: |
| 65 | return "Union"; |
| 66 | case Class: |
| 67 | return "Class"; |
| 68 | case Enum: |
| 69 | return "Enum"; |
| 70 | } |
| 71 | } |
| 72 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | bool Decl::CollectingStats(bool enable) { |
| 74 | if (enable) StatSwitch = true; |
| 75 | return StatSwitch; |
| 76 | } |
| 77 | |
| 78 | void Decl::PrintStats() { |
| 79 | fprintf(stderr, "*** Decl Stats:\n"); |
| 80 | fprintf(stderr, " %d decls total.\n", |
| 81 | int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+ |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 82 | nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+ |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame^] | 83 | nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 84 | fprintf(stderr, " %d function decls, %d each (%d bytes)\n", |
| 85 | nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl))); |
| 86 | fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n", |
| 87 | nBlockVars, (int)sizeof(BlockVarDecl), |
| 88 | int(nBlockVars*sizeof(BlockVarDecl))); |
| 89 | fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n", |
| 90 | nFileVars, (int)sizeof(FileVarDecl), |
| 91 | int(nFileVars*sizeof(FileVarDecl))); |
| 92 | fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n", |
| 93 | nParmVars, (int)sizeof(ParmVarDecl), |
| 94 | int(nParmVars*sizeof(ParmVarDecl))); |
| 95 | fprintf(stderr, " %d field decls, %d each (%d bytes)\n", |
| 96 | nFieldDecls, (int)sizeof(FieldDecl), |
| 97 | int(nFieldDecls*sizeof(FieldDecl))); |
| 98 | fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n", |
| 99 | nSUC, (int)sizeof(RecordDecl), |
| 100 | int(nSUC*sizeof(RecordDecl))); |
| 101 | fprintf(stderr, " %d enum decls, %d each (%d bytes)\n", |
| 102 | nEnumDecls, (int)sizeof(EnumDecl), |
| 103 | int(nEnumDecls*sizeof(EnumDecl))); |
| 104 | fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n", |
| 105 | nEnumConst, (int)sizeof(EnumConstantDecl), |
| 106 | int(nEnumConst*sizeof(EnumConstantDecl))); |
| 107 | fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n", |
| 108 | nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 109 | // Objective-C decls... |
| 110 | fprintf(stderr, " %d interface decls, %d each (%d bytes)\n", |
| 111 | nInterfaceDecls, (int)sizeof(ObjcInterfaceDecl), |
| 112 | int(nInterfaceDecls*sizeof(ObjcInterfaceDecl))); |
| 113 | fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n", |
| 114 | nIvarDecls, (int)sizeof(ObjcIvarDecl), |
| 115 | int(nIvarDecls*sizeof(ObjcIvarDecl))); |
| 116 | fprintf(stderr, " %d class decls, %d each (%d bytes)\n", |
| 117 | nClassDecls, (int)sizeof(ObjcClassDecl), |
| 118 | int(nClassDecls*sizeof(ObjcClassDecl))); |
| 119 | fprintf(stderr, " %d method decls, %d each (%d bytes)\n", |
| 120 | nMethodDecls, (int)sizeof(ObjcMethodDecl), |
| 121 | int(nMethodDecls*sizeof(ObjcMethodDecl))); |
| 122 | fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n", |
| 123 | nProtocolDecls, (int)sizeof(ObjcProtocolDecl), |
| 124 | int(nProtocolDecls*sizeof(ObjcProtocolDecl))); |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame^] | 125 | fprintf(stderr, " %d category decls, %d each (%d bytes)\n", |
| 126 | nCategoryDecls, (int)sizeof(ObjcCategoryDecl), |
| 127 | int(nCategoryDecls*sizeof(ObjcCategoryDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 128 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | fprintf(stderr, "Total bytes = %d\n", |
| 130 | int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+ |
| 131 | nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+ |
| 132 | nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+ |
| 133 | nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+ |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 134 | nTypedef*sizeof(TypedefDecl)) /* FIXME: add Objc decls */); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void Decl::addDeclKind(const Kind k) { |
| 138 | switch (k) { |
| 139 | case Typedef: |
| 140 | nTypedef++; |
| 141 | break; |
| 142 | case Function: |
| 143 | nFuncs++; |
| 144 | break; |
| 145 | case BlockVariable: |
| 146 | nBlockVars++; |
| 147 | break; |
| 148 | case FileVariable: |
| 149 | nFileVars++; |
| 150 | break; |
| 151 | case ParmVariable: |
| 152 | nParmVars++; |
| 153 | break; |
| 154 | case EnumConstant: |
| 155 | nEnumConst++; |
| 156 | break; |
| 157 | case Field: |
| 158 | nFieldDecls++; |
| 159 | break; |
| 160 | case Struct: |
| 161 | case Union: |
| 162 | case Class: |
| 163 | nSUC++; |
| 164 | break; |
| 165 | case Enum: |
| 166 | nEnumDecls++; |
| 167 | break; |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 168 | case ObjcInterface: |
| 169 | nInterfaceDecls++; |
| 170 | break; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 171 | case ObjcClass: |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 172 | nClassDecls++; |
| 173 | break; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 174 | case ObjcMethod: |
| 175 | case ObjcProtoMethod: |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 176 | nMethodDecls++; |
| 177 | break; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 178 | case ObjcProtocol: |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 179 | nProtocolDecls++; |
| 180 | break; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame^] | 181 | case ObjcCategory: |
| 182 | nCategoryDecls++; |
| 183 | break; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 184 | case ObjcIvar: |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 185 | nIvarDecls++; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 186 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | // Out-of-line virtual method providing a home for Decl. |
| 191 | Decl::~Decl() { |
| 192 | } |
| 193 | |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 194 | const char *FieldDecl::getName() const { |
| 195 | if (const IdentifierInfo *II = getIdentifier()) |
| 196 | return II->getName(); |
| 197 | return ""; |
| 198 | } |
| 199 | |
| 200 | const char *ScopedDecl::getName() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | if (const IdentifierInfo *II = getIdentifier()) |
| 202 | return II->getName(); |
| 203 | return ""; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | FunctionDecl::~FunctionDecl() { |
| 208 | delete[] ParamInfo; |
| 209 | } |
| 210 | |
| 211 | unsigned FunctionDecl::getNumParams() const { |
| 212 | return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs(); |
| 213 | } |
| 214 | |
| 215 | void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) { |
| 216 | assert(ParamInfo == 0 && "Already has param info!"); |
| 217 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
| 218 | |
| 219 | // Zero params -> null pointer. |
| 220 | if (NumParams) { |
| 221 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 222 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | |
| 227 | /// defineBody - When created, RecordDecl's correspond to a forward declared |
| 228 | /// record. This method is used to mark the decl as being defined, with the |
| 229 | /// specified contents. |
| 230 | void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) { |
| 231 | assert(!isDefinition() && "Cannot redefine record!"); |
| 232 | setDefinition(true); |
| 233 | NumMembers = numMembers; |
| 234 | if (numMembers) { |
| 235 | Members = new FieldDecl*[numMembers]; |
| 236 | memcpy(Members, members, numMembers*sizeof(Decl*)); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | FieldDecl* RecordDecl::getMember(IdentifierInfo *name) { |
| 241 | if (Members == 0 || NumMembers < 0) |
| 242 | return 0; |
| 243 | |
| 244 | // linear search. When C++ classes come along, will likely need to revisit. |
| 245 | for (int i = 0; i < NumMembers; ++i) { |
| 246 | if (Members[i]->getIdentifier() == name) |
| 247 | return Members[i]; |
| 248 | } |
| 249 | return 0; |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 250 | } |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 251 | |
Fariborz Jahanian | e55cd00 | 2007-09-12 18:23:47 +0000 | [diff] [blame] | 252 | void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo, |
| 253 | unsigned NumParams) { |
| 254 | assert(ParamInfo == 0 && "Already has param info!"); |
| 255 | |
| 256 | // Zero params -> null pointer. |
| 257 | if (NumParams) { |
| 258 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 259 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 260 | NumMethodParams = NumParams; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | ObjcMethodDecl::~ObjcMethodDecl() { |
| 265 | delete[] ParamInfo; |
| 266 | } |
| 267 | |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 268 | /// ObjcAddInstanceVariablesToClass - Inserts instance variables |
| 269 | /// into ObjcInterfaceDecl's fields. |
| 270 | /// |
| 271 | void ObjcInterfaceDecl::ObjcAddInstanceVariablesToClass(ObjcIvarDecl **ivars, |
| 272 | unsigned numIvars) { |
| 273 | NumIvars = numIvars; |
| 274 | if (numIvars) { |
| 275 | Ivars = new ObjcIvarDecl*[numIvars]; |
| 276 | memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*)); |
| 277 | } |
| 278 | } |
| 279 | |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 280 | /// addObjcMethods - Insert instance and methods declarations into |
| 281 | /// ObjcInterfaceDecl's InsMethods and ClsMethods fields. |
| 282 | /// |
| 283 | void ObjcInterfaceDecl::ObjcAddMethods(ObjcMethodDecl **insMethods, |
| 284 | unsigned numInsMembers, |
| 285 | ObjcMethodDecl **clsMethods, |
| 286 | unsigned numClsMembers) { |
| 287 | NumInsMethods = numInsMembers; |
| 288 | if (numInsMembers) { |
| 289 | InsMethods = new ObjcMethodDecl*[numInsMembers]; |
| 290 | memcpy(InsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
| 291 | } |
| 292 | NumClsMethods = numClsMembers; |
| 293 | if (numClsMembers) { |
| 294 | ClsMethods = new ObjcMethodDecl*[numClsMembers]; |
| 295 | memcpy(ClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
| 296 | } |
| 297 | } |
| 298 | |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 299 | /// ObjcAddProtoMethods - Insert instance and methods declarations into |
| 300 | /// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields. |
| 301 | /// |
| 302 | void ObjcProtocolDecl::ObjcAddProtoMethods(ObjcMethodDecl **insMethods, |
| 303 | unsigned numInsMembers, |
| 304 | ObjcMethodDecl **clsMethods, |
| 305 | unsigned numClsMembers) { |
| 306 | NumProtoInsMethods = numInsMembers; |
| 307 | if (numInsMembers) { |
Fariborz Jahanian | 146fbb0 | 2007-09-17 22:36:42 +0000 | [diff] [blame] | 308 | ProtoInsMethods = new ObjcMethodDecl*[numInsMembers]; |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 309 | memcpy(ProtoInsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
| 310 | } |
| 311 | NumProtoClsMethods = numClsMembers; |
| 312 | if (numClsMembers) { |
Fariborz Jahanian | 146fbb0 | 2007-09-17 22:36:42 +0000 | [diff] [blame] | 313 | ProtoClsMethods = new ObjcMethodDecl*[numClsMembers]; |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 314 | memcpy(ProtoClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
| 315 | } |
| 316 | } |
| 317 | |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame^] | 318 | /// ObjcAddCat - Insert instance and methods declarations into |
| 319 | /// ObjcProtocolDecl's CatInsMethods and CatClsMethods fields. |
| 320 | /// |
| 321 | void ObjcCategoryDecl::ObjcAddCatMethods(ObjcMethodDecl **insMethods, |
| 322 | unsigned numInsMembers, |
| 323 | ObjcMethodDecl **clsMethods, |
| 324 | unsigned numClsMembers) { |
| 325 | NumCatInsMethods = numInsMembers; |
| 326 | if (numInsMembers) { |
| 327 | CatInsMethods = new ObjcMethodDecl*[numInsMembers]; |
| 328 | memcpy(CatInsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
| 329 | } |
| 330 | NumCatClsMethods = numClsMembers; |
| 331 | if (numClsMembers) { |
| 332 | CatClsMethods = new ObjcMethodDecl*[numClsMembers]; |
| 333 | memcpy(CatClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
| 334 | } |
| 335 | } |
| 336 | |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 337 | |