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" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 16 | #include "clang/Basic/IdentifierTable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
| 19 | // temporary statistics gathering |
| 20 | static unsigned nFuncs = 0; |
| 21 | static unsigned nBlockVars = 0; |
| 22 | static unsigned nFileVars = 0; |
| 23 | static unsigned nParmVars = 0; |
| 24 | static unsigned nSUC = 0; |
| 25 | static unsigned nEnumConst = 0; |
| 26 | static unsigned nEnumDecls = 0; |
| 27 | static unsigned nTypedef = 0; |
| 28 | static unsigned nFieldDecls = 0; |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 29 | static unsigned nInterfaceDecls = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 30 | static unsigned nClassDecls = 0; |
| 31 | static unsigned nMethodDecls = 0; |
| 32 | static unsigned nProtocolDecls = 0; |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 33 | static unsigned nForwardProtocolDecls = 0; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 34 | static unsigned nCategoryDecls = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 35 | static unsigned nIvarDecls = 0; |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 36 | static unsigned nObjcImplementationDecls = 0; |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 37 | static unsigned nObjcCategoryImpl = 0; |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 38 | static unsigned nObjcCompatibleAlias = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 39 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | static bool StatSwitch = false; |
| 41 | |
Steve Naroff | e5ea380 | 2007-09-17 14:49:06 +0000 | [diff] [blame] | 42 | const char *Decl::getDeclKindName() const { |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 43 | switch (DeclKind) { |
| 44 | default: assert(0 && "Unknown decl kind!"); |
| 45 | case Typedef: |
| 46 | return "Typedef"; |
| 47 | case Function: |
| 48 | return "Function"; |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 49 | case BlockVar: |
| 50 | return "BlockVar"; |
| 51 | case FileVar: |
| 52 | return "FileVar"; |
| 53 | case ParmVar: |
| 54 | return "ParmVar"; |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 55 | case EnumConstant: |
| 56 | return "EnumConstant"; |
| 57 | case ObjcInterface: |
| 58 | return "ObjcInterface"; |
| 59 | case ObjcClass: |
| 60 | return "ObjcClass"; |
| 61 | case ObjcMethod: |
| 62 | return "ObjcMethod"; |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 63 | case ObjcProtocol: |
| 64 | return "ObjcProtocol"; |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 65 | case ObjcForwardProtocol: |
| 66 | return "ObjcForwardProtocol"; |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 67 | case Struct: |
| 68 | return "Struct"; |
| 69 | case Union: |
| 70 | return "Union"; |
| 71 | case Class: |
| 72 | return "Class"; |
| 73 | case Enum: |
| 74 | return "Enum"; |
| 75 | } |
| 76 | } |
| 77 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | bool Decl::CollectingStats(bool enable) { |
| 79 | if (enable) StatSwitch = true; |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 80 | return StatSwitch; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void Decl::PrintStats() { |
| 84 | fprintf(stderr, "*** Decl Stats:\n"); |
| 85 | fprintf(stderr, " %d decls total.\n", |
| 86 | int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+ |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 87 | nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+ |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 88 | nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 89 | fprintf(stderr, " %d function decls, %d each (%d bytes)\n", |
| 90 | nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl))); |
| 91 | fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n", |
| 92 | nBlockVars, (int)sizeof(BlockVarDecl), |
| 93 | int(nBlockVars*sizeof(BlockVarDecl))); |
| 94 | fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n", |
| 95 | nFileVars, (int)sizeof(FileVarDecl), |
| 96 | int(nFileVars*sizeof(FileVarDecl))); |
| 97 | fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n", |
| 98 | nParmVars, (int)sizeof(ParmVarDecl), |
| 99 | int(nParmVars*sizeof(ParmVarDecl))); |
| 100 | fprintf(stderr, " %d field decls, %d each (%d bytes)\n", |
| 101 | nFieldDecls, (int)sizeof(FieldDecl), |
| 102 | int(nFieldDecls*sizeof(FieldDecl))); |
| 103 | fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n", |
| 104 | nSUC, (int)sizeof(RecordDecl), |
| 105 | int(nSUC*sizeof(RecordDecl))); |
| 106 | fprintf(stderr, " %d enum decls, %d each (%d bytes)\n", |
| 107 | nEnumDecls, (int)sizeof(EnumDecl), |
| 108 | int(nEnumDecls*sizeof(EnumDecl))); |
| 109 | fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n", |
| 110 | nEnumConst, (int)sizeof(EnumConstantDecl), |
| 111 | int(nEnumConst*sizeof(EnumConstantDecl))); |
| 112 | fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n", |
| 113 | nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 114 | // Objective-C decls... |
| 115 | fprintf(stderr, " %d interface decls, %d each (%d bytes)\n", |
| 116 | nInterfaceDecls, (int)sizeof(ObjcInterfaceDecl), |
| 117 | int(nInterfaceDecls*sizeof(ObjcInterfaceDecl))); |
| 118 | fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n", |
| 119 | nIvarDecls, (int)sizeof(ObjcIvarDecl), |
| 120 | int(nIvarDecls*sizeof(ObjcIvarDecl))); |
| 121 | fprintf(stderr, " %d class decls, %d each (%d bytes)\n", |
| 122 | nClassDecls, (int)sizeof(ObjcClassDecl), |
| 123 | int(nClassDecls*sizeof(ObjcClassDecl))); |
| 124 | fprintf(stderr, " %d method decls, %d each (%d bytes)\n", |
| 125 | nMethodDecls, (int)sizeof(ObjcMethodDecl), |
| 126 | int(nMethodDecls*sizeof(ObjcMethodDecl))); |
| 127 | fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n", |
| 128 | nProtocolDecls, (int)sizeof(ObjcProtocolDecl), |
| 129 | int(nProtocolDecls*sizeof(ObjcProtocolDecl))); |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 130 | fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n", |
| 131 | nForwardProtocolDecls, (int)sizeof(ObjcForwardProtocolDecl), |
| 132 | int(nForwardProtocolDecls*sizeof(ObjcForwardProtocolDecl))); |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 133 | fprintf(stderr, " %d category decls, %d each (%d bytes)\n", |
| 134 | nCategoryDecls, (int)sizeof(ObjcCategoryDecl), |
| 135 | int(nCategoryDecls*sizeof(ObjcCategoryDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 136 | |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 137 | fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n", |
| 138 | nObjcImplementationDecls, (int)sizeof(ObjcImplementationDecl), |
| 139 | int(nObjcImplementationDecls*sizeof(ObjcImplementationDecl))); |
| 140 | |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 141 | fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n", |
| 142 | nObjcCategoryImpl, (int)sizeof(ObjcCategoryImplDecl), |
| 143 | int(nObjcCategoryImpl*sizeof(ObjcCategoryImplDecl))); |
| 144 | |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 145 | fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n", |
| 146 | nObjcCompatibleAlias, (int)sizeof(ObjcCompatibleAliasDecl), |
| 147 | int(nObjcCompatibleAlias*sizeof(ObjcCompatibleAliasDecl))); |
| 148 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 149 | fprintf(stderr, "Total bytes = %d\n", |
| 150 | int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+ |
| 151 | nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+ |
| 152 | nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+ |
| 153 | nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+ |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 154 | nTypedef*sizeof(TypedefDecl)) /* FIXME: add Objc decls */); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void Decl::addDeclKind(const Kind k) { |
| 158 | switch (k) { |
| 159 | case Typedef: |
| 160 | nTypedef++; |
| 161 | break; |
| 162 | case Function: |
| 163 | nFuncs++; |
| 164 | break; |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 165 | case BlockVar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | nBlockVars++; |
| 167 | break; |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 168 | case FileVar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | nFileVars++; |
| 170 | break; |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 171 | case ParmVar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | nParmVars++; |
| 173 | break; |
| 174 | case EnumConstant: |
| 175 | nEnumConst++; |
| 176 | break; |
| 177 | case Field: |
| 178 | nFieldDecls++; |
| 179 | break; |
| 180 | case Struct: |
| 181 | case Union: |
| 182 | case Class: |
| 183 | nSUC++; |
| 184 | break; |
| 185 | case Enum: |
| 186 | nEnumDecls++; |
| 187 | break; |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 188 | case ObjcInterface: |
| 189 | nInterfaceDecls++; |
| 190 | break; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 191 | case ObjcClass: |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 192 | nClassDecls++; |
| 193 | break; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 194 | case ObjcMethod: |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 195 | nMethodDecls++; |
| 196 | break; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 197 | case ObjcProtocol: |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 198 | nProtocolDecls++; |
| 199 | break; |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 200 | case ObjcForwardProtocol: |
| 201 | nForwardProtocolDecls++; |
| 202 | break; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 203 | case ObjcCategory: |
| 204 | nCategoryDecls++; |
| 205 | break; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 206 | case ObjcIvar: |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 207 | nIvarDecls++; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 208 | break; |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 209 | case ObjcImplementation: |
| 210 | nObjcImplementationDecls++; |
| 211 | break; |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 212 | case ObjcCategoryImpl: |
| 213 | nObjcCategoryImpl++; |
| 214 | break; |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 215 | case CompatibleAlias: |
| 216 | nObjcCompatibleAlias++; |
| 217 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | // Out-of-line virtual method providing a home for Decl. |
| 222 | Decl::~Decl() { |
| 223 | } |
| 224 | |
Chris Lattner | fd5de47 | 2007-10-06 22:53:46 +0000 | [diff] [blame] | 225 | const char *NamedDecl::getName() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 226 | if (const IdentifierInfo *II = getIdentifier()) |
| 227 | return II->getName(); |
| 228 | return ""; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | FunctionDecl::~FunctionDecl() { |
| 233 | delete[] ParamInfo; |
| 234 | } |
| 235 | |
| 236 | unsigned FunctionDecl::getNumParams() const { |
| 237 | return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs(); |
| 238 | } |
| 239 | |
| 240 | void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) { |
| 241 | assert(ParamInfo == 0 && "Already has param info!"); |
| 242 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
| 243 | |
| 244 | // Zero params -> null pointer. |
| 245 | if (NumParams) { |
| 246 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 247 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | |
| 252 | /// defineBody - When created, RecordDecl's correspond to a forward declared |
| 253 | /// record. This method is used to mark the decl as being defined, with the |
| 254 | /// specified contents. |
| 255 | void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) { |
| 256 | assert(!isDefinition() && "Cannot redefine record!"); |
| 257 | setDefinition(true); |
| 258 | NumMembers = numMembers; |
| 259 | if (numMembers) { |
| 260 | Members = new FieldDecl*[numMembers]; |
| 261 | memcpy(Members, members, numMembers*sizeof(Decl*)); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | FieldDecl* RecordDecl::getMember(IdentifierInfo *name) { |
| 266 | if (Members == 0 || NumMembers < 0) |
| 267 | return 0; |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 268 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 269 | // linear search. When C++ classes come along, will likely need to revisit. |
| 270 | for (int i = 0; i < NumMembers; ++i) { |
| 271 | if (Members[i]->getIdentifier() == name) |
| 272 | return Members[i]; |
| 273 | } |
| 274 | return 0; |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 275 | } |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 276 | |
Fariborz Jahanian | 5951965 | 2007-10-04 17:06:28 +0000 | [diff] [blame] | 277 | void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo, |
| 278 | unsigned NumParams) { |
Fariborz Jahanian | e55cd00 | 2007-09-12 18:23:47 +0000 | [diff] [blame] | 279 | assert(ParamInfo == 0 && "Already has param info!"); |
| 280 | |
| 281 | // Zero params -> null pointer. |
| 282 | if (NumParams) { |
| 283 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 284 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 285 | NumMethodParams = NumParams; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | ObjcMethodDecl::~ObjcMethodDecl() { |
| 290 | delete[] ParamInfo; |
| 291 | } |
| 292 | |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 293 | /// ObjcAddInstanceVariablesToClass - Inserts instance variables |
| 294 | /// into ObjcInterfaceDecl's fields. |
| 295 | /// |
| 296 | void ObjcInterfaceDecl::ObjcAddInstanceVariablesToClass(ObjcIvarDecl **ivars, |
Fariborz Jahanian | 5951965 | 2007-10-04 17:06:28 +0000 | [diff] [blame] | 297 | unsigned numIvars) { |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 298 | NumIvars = numIvars; |
| 299 | if (numIvars) { |
| 300 | Ivars = new ObjcIvarDecl*[numIvars]; |
| 301 | memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*)); |
| 302 | } |
| 303 | } |
| 304 | |
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 305 | /// ObjcAddInstanceVariablesToClassImpl - Checks for correctness of Instance |
| 306 | /// Variables (Ivars) relative to what declared in @implementation;s class. |
| 307 | /// Ivars into ObjcImplementationDecl's fields. |
| 308 | /// |
| 309 | void ObjcImplementationDecl::ObjcAddInstanceVariablesToClassImpl( |
Fariborz Jahanian | 5951965 | 2007-10-04 17:06:28 +0000 | [diff] [blame] | 310 | ObjcIvarDecl **ivars, unsigned numIvars) { |
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 311 | NumIvars = numIvars; |
| 312 | if (numIvars) { |
| 313 | Ivars = new ObjcIvarDecl*[numIvars]; |
| 314 | memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*)); |
| 315 | } |
| 316 | } |
| 317 | |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 318 | /// addObjcMethods - Insert instance and methods declarations into |
| 319 | /// ObjcInterfaceDecl's InsMethods and ClsMethods fields. |
| 320 | /// |
| 321 | void ObjcInterfaceDecl::ObjcAddMethods(ObjcMethodDecl **insMethods, |
Fariborz Jahanian | 5951965 | 2007-10-04 17:06:28 +0000 | [diff] [blame] | 322 | unsigned numInsMembers, |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 323 | ObjcMethodDecl **clsMethods, |
| 324 | unsigned numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 325 | NumInstanceMethods = numInsMembers; |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 326 | if (numInsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 327 | InstanceMethods = new ObjcMethodDecl*[numInsMembers]; |
| 328 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 329 | } |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 330 | NumClassMethods = numClsMembers; |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 331 | if (numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 332 | ClassMethods = new ObjcMethodDecl*[numClsMembers]; |
| 333 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 337 | /// ObjcAddProtoMethods - Insert instance and methods declarations into |
| 338 | /// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields. |
| 339 | /// |
| 340 | void ObjcProtocolDecl::ObjcAddProtoMethods(ObjcMethodDecl **insMethods, |
Fariborz Jahanian | 5951965 | 2007-10-04 17:06:28 +0000 | [diff] [blame] | 341 | unsigned numInsMembers, |
| 342 | ObjcMethodDecl **clsMethods, |
| 343 | unsigned numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 344 | NumInstanceMethods = numInsMembers; |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 345 | if (numInsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 346 | InstanceMethods = new ObjcMethodDecl*[numInsMembers]; |
| 347 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 348 | } |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 349 | NumClassMethods = numClsMembers; |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 350 | if (numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 351 | ClassMethods = new ObjcMethodDecl*[numClsMembers]; |
| 352 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 356 | /// ObjcAddCat - Insert instance and methods declarations into |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 357 | /// ObjcCategoryDecl's CatInsMethods and CatClsMethods fields. |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 358 | /// |
| 359 | void ObjcCategoryDecl::ObjcAddCatMethods(ObjcMethodDecl **insMethods, |
Fariborz Jahanian | 5951965 | 2007-10-04 17:06:28 +0000 | [diff] [blame] | 360 | unsigned numInsMembers, |
| 361 | ObjcMethodDecl **clsMethods, |
| 362 | unsigned numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 363 | NumInstanceMethods = numInsMembers; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 364 | if (numInsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 365 | InstanceMethods = new ObjcMethodDecl*[numInsMembers]; |
| 366 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 367 | } |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 368 | NumClassMethods = numClsMembers; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 369 | if (numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 370 | ClassMethods = new ObjcMethodDecl*[numClsMembers]; |
| 371 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 375 | /// ObjcAddCatImplMethods - Insert instance and methods declarations into |
| 376 | /// ObjcCategoryImplDecl's CatInsMethods and CatClsMethods fields. |
| 377 | /// |
| 378 | void ObjcCategoryImplDecl::ObjcAddCatImplMethods(ObjcMethodDecl **insMethods, |
Fariborz Jahanian | 5951965 | 2007-10-04 17:06:28 +0000 | [diff] [blame] | 379 | unsigned numInsMembers, |
| 380 | ObjcMethodDecl **clsMethods, |
| 381 | unsigned numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 382 | NumInstanceMethods = numInsMembers; |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 383 | if (numInsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 384 | InstanceMethods = new ObjcMethodDecl*[numInsMembers]; |
| 385 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 386 | } |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 387 | NumClassMethods = numClsMembers; |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 388 | if (numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 389 | ClassMethods = new ObjcMethodDecl*[numClsMembers]; |
| 390 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 394 | /// ObjcAddImplMethods - Insert instance and methods declarations into |
| 395 | /// ObjcImplementationDecl's InsMethods and ClsMethods fields. |
| 396 | /// |
| 397 | void ObjcImplementationDecl::ObjcAddImplMethods(ObjcMethodDecl **insMethods, |
Fariborz Jahanian | 5951965 | 2007-10-04 17:06:28 +0000 | [diff] [blame] | 398 | unsigned numInsMembers, |
| 399 | ObjcMethodDecl **clsMethods, |
| 400 | unsigned numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 401 | NumInstanceMethods = numInsMembers; |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 402 | if (numInsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 403 | InstanceMethods = new ObjcMethodDecl*[numInsMembers]; |
| 404 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 405 | } |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 406 | NumClassMethods = numClsMembers; |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 407 | if (numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 408 | ClassMethods = new ObjcMethodDecl*[numClsMembers]; |
| 409 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 413 | // lookupInstanceMethod - This method returns an instance method by looking in |
| 414 | // the class, it's categories, and it's super classes (using a linear search). |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 415 | ObjcMethodDecl *ObjcInterfaceDecl::lookupInstanceMethod(Selector &Sel) { |
| 416 | ObjcInterfaceDecl* ClassDecl = this; |
| 417 | while (ClassDecl != NULL) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 418 | ObjcMethodDecl **methods = ClassDecl->getInstanceMethods(); |
| 419 | int methodCount = ClassDecl->getNumInstanceMethods(); |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 420 | for (int i = 0; i < methodCount; ++i) { |
| 421 | if (methods[i]->getSelector() == Sel) { |
| 422 | return methods[i]; |
| 423 | } |
| 424 | } |
Steve Naroff | ff1afdb | 2007-10-14 23:13:51 +0000 | [diff] [blame^] | 425 | // Didn't find one yet - look through protocols. |
| 426 | ObjcProtocolDecl **protocols = ClassDecl->getReferencedProtocols(); |
| 427 | int numProtocols = ClassDecl->getNumIntfRefProtocols(); |
| 428 | for (int pIdx = 0; pIdx < numProtocols; pIdx++) { |
| 429 | ObjcMethodDecl **methods = protocols[pIdx]->getInstanceMethods(); |
| 430 | int methodCount = protocols[pIdx]->getNumInstanceMethods(); |
| 431 | for (int i = 0; i < methodCount; ++i) { |
| 432 | if (methods[i]->getSelector() == Sel) { |
| 433 | return methods[i]; |
| 434 | } |
| 435 | } |
| 436 | } |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 437 | // Didn't find one yet - now look through categories. |
Steve Naroff | ff1afdb | 2007-10-14 23:13:51 +0000 | [diff] [blame^] | 438 | ObjcCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 439 | while (CatDecl) { |
| 440 | ObjcMethodDecl **methods = CatDecl->getInstanceMethods(); |
| 441 | int methodCount = CatDecl->getNumInstanceMethods(); |
| 442 | for (int i = 0; i < methodCount; ++i) { |
| 443 | if (methods[i]->getSelector() == Sel) { |
| 444 | return methods[i]; |
| 445 | } |
| 446 | } |
| 447 | CatDecl = CatDecl->getNextClassCategory(); |
| 448 | } |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 449 | ClassDecl = ClassDecl->getSuperClass(); |
| 450 | } |
| 451 | return NULL; |
| 452 | } |
| 453 | |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 454 | // lookupClassMethod - This method returns a class method by looking in the |
| 455 | // class, it's categories, and it's super classes (using a linear search). |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 456 | ObjcMethodDecl *ObjcInterfaceDecl::lookupClassMethod(Selector &Sel) { |
| 457 | ObjcInterfaceDecl* ClassDecl = this; |
| 458 | while (ClassDecl != NULL) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 459 | ObjcMethodDecl **methods = ClassDecl->getClassMethods(); |
| 460 | int methodCount = ClassDecl->getNumClassMethods(); |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 461 | for (int i = 0; i < methodCount; ++i) { |
| 462 | if (methods[i]->getSelector() == Sel) { |
| 463 | return methods[i]; |
| 464 | } |
| 465 | } |
Steve Naroff | ff1afdb | 2007-10-14 23:13:51 +0000 | [diff] [blame^] | 466 | // Didn't find one yet - look through protocols. |
| 467 | ObjcProtocolDecl **protocols = ClassDecl->getReferencedProtocols(); |
| 468 | int numProtocols = ClassDecl->getNumIntfRefProtocols(); |
| 469 | for (int pIdx = 0; pIdx < numProtocols; pIdx++) { |
| 470 | ObjcMethodDecl **methods = protocols[pIdx]->getClassMethods(); |
| 471 | int methodCount = protocols[pIdx]->getNumClassMethods(); |
| 472 | for (int i = 0; i < methodCount; ++i) { |
| 473 | if (methods[i]->getSelector() == Sel) { |
| 474 | return methods[i]; |
| 475 | } |
| 476 | } |
| 477 | } |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 478 | // Didn't find one yet - now look through categories. |
Steve Naroff | ff1afdb | 2007-10-14 23:13:51 +0000 | [diff] [blame^] | 479 | ObjcCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 480 | while (CatDecl) { |
| 481 | ObjcMethodDecl **methods = CatDecl->getClassMethods(); |
| 482 | int methodCount = CatDecl->getNumClassMethods(); |
| 483 | for (int i = 0; i < methodCount; ++i) { |
| 484 | if (methods[i]->getSelector() == Sel) { |
| 485 | return methods[i]; |
| 486 | } |
| 487 | } |
| 488 | CatDecl = CatDecl->getNextClassCategory(); |
| 489 | } |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 490 | ClassDecl = ClassDecl->getSuperClass(); |
| 491 | } |
| 492 | return NULL; |
| 493 | } |
| 494 | |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 495 | |