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 | /// |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame^] | 296 | void ObjcInterfaceDecl::addInstanceVariablesToClass(ObjcIvarDecl **ivars, |
| 297 | unsigned numIvars, |
| 298 | SourceLocation RB) { |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 299 | NumIvars = numIvars; |
| 300 | if (numIvars) { |
| 301 | Ivars = new ObjcIvarDecl*[numIvars]; |
| 302 | memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*)); |
| 303 | } |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame^] | 304 | RBracLoc = RB; |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 307 | /// ObjcAddInstanceVariablesToClassImpl - Checks for correctness of Instance |
| 308 | /// Variables (Ivars) relative to what declared in @implementation;s class. |
| 309 | /// Ivars into ObjcImplementationDecl's fields. |
| 310 | /// |
| 311 | void ObjcImplementationDecl::ObjcAddInstanceVariablesToClassImpl( |
Fariborz Jahanian | 5951965 | 2007-10-04 17:06:28 +0000 | [diff] [blame] | 312 | ObjcIvarDecl **ivars, unsigned numIvars) { |
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 313 | NumIvars = numIvars; |
| 314 | if (numIvars) { |
| 315 | Ivars = new ObjcIvarDecl*[numIvars]; |
| 316 | memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*)); |
| 317 | } |
| 318 | } |
| 319 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame^] | 320 | /// addMethods - Insert instance and methods declarations into |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 321 | /// ObjcInterfaceDecl's InsMethods and ClsMethods fields. |
| 322 | /// |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame^] | 323 | void ObjcInterfaceDecl::addMethods(ObjcMethodDecl **insMethods, |
| 324 | unsigned numInsMembers, |
| 325 | ObjcMethodDecl **clsMethods, |
| 326 | unsigned numClsMembers, |
| 327 | SourceLocation endLoc) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 328 | NumInstanceMethods = numInsMembers; |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 329 | if (numInsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 330 | InstanceMethods = new ObjcMethodDecl*[numInsMembers]; |
| 331 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 332 | } |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 333 | NumClassMethods = numClsMembers; |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 334 | if (numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 335 | ClassMethods = new ObjcMethodDecl*[numClsMembers]; |
| 336 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 337 | } |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame^] | 338 | EndLoc = endLoc; |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame^] | 341 | /// addMethods - Insert instance and methods declarations into |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 342 | /// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields. |
| 343 | /// |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame^] | 344 | void ObjcProtocolDecl::addMethods(ObjcMethodDecl **insMethods, |
| 345 | unsigned numInsMembers, |
| 346 | ObjcMethodDecl **clsMethods, |
| 347 | unsigned numClsMembers, |
| 348 | SourceLocation AtEndLoc) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 349 | NumInstanceMethods = numInsMembers; |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 350 | if (numInsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 351 | InstanceMethods = new ObjcMethodDecl*[numInsMembers]; |
| 352 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 353 | } |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 354 | NumClassMethods = numClsMembers; |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 355 | if (numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 356 | ClassMethods = new ObjcMethodDecl*[numClsMembers]; |
| 357 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame^] | 361 | /// addMethods - Insert instance and methods declarations into |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 362 | /// ObjcCategoryDecl's CatInsMethods and CatClsMethods fields. |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 363 | /// |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame^] | 364 | void ObjcCategoryDecl::addMethods(ObjcMethodDecl **insMethods, |
| 365 | unsigned numInsMembers, |
| 366 | ObjcMethodDecl **clsMethods, |
| 367 | unsigned numClsMembers, |
| 368 | SourceLocation AtEndLoc) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 369 | NumInstanceMethods = numInsMembers; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 370 | if (numInsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 371 | InstanceMethods = new ObjcMethodDecl*[numInsMembers]; |
| 372 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 373 | } |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 374 | NumClassMethods = numClsMembers; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 375 | if (numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 376 | ClassMethods = new ObjcMethodDecl*[numClsMembers]; |
| 377 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame^] | 381 | /// addMethods - Insert instance and methods declarations into |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 382 | /// ObjcCategoryImplDecl's CatInsMethods and CatClsMethods fields. |
| 383 | /// |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame^] | 384 | void ObjcCategoryImplDecl::addMethods(ObjcMethodDecl **insMethods, |
| 385 | unsigned numInsMembers, |
| 386 | ObjcMethodDecl **clsMethods, |
| 387 | unsigned numClsMembers, |
| 388 | SourceLocation AtEndLoc) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 389 | NumInstanceMethods = numInsMembers; |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 390 | if (numInsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 391 | InstanceMethods = new ObjcMethodDecl*[numInsMembers]; |
| 392 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 393 | } |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 394 | NumClassMethods = numClsMembers; |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 395 | if (numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 396 | ClassMethods = new ObjcMethodDecl*[numClsMembers]; |
| 397 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 401 | /// ObjcAddImplMethods - Insert instance and methods declarations into |
| 402 | /// ObjcImplementationDecl's InsMethods and ClsMethods fields. |
| 403 | /// |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame^] | 404 | void ObjcImplementationDecl::addMethods(ObjcMethodDecl **insMethods, |
| 405 | unsigned numInsMembers, |
| 406 | ObjcMethodDecl **clsMethods, |
| 407 | unsigned numClsMembers, |
| 408 | SourceLocation AtEndLoc) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 409 | NumInstanceMethods = numInsMembers; |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 410 | if (numInsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 411 | InstanceMethods = new ObjcMethodDecl*[numInsMembers]; |
| 412 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 413 | } |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 414 | NumClassMethods = numClsMembers; |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 415 | if (numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 416 | ClassMethods = new ObjcMethodDecl*[numClsMembers]; |
| 417 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | d0b0154 | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 421 | // lookupInstanceMethod - This method returns an instance method by looking in |
| 422 | // 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] | 423 | ObjcMethodDecl *ObjcInterfaceDecl::lookupInstanceMethod(Selector &Sel) { |
| 424 | ObjcInterfaceDecl* ClassDecl = this; |
| 425 | while (ClassDecl != NULL) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 426 | ObjcMethodDecl **methods = ClassDecl->getInstanceMethods(); |
| 427 | int methodCount = ClassDecl->getNumInstanceMethods(); |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 428 | for (int i = 0; i < methodCount; ++i) { |
| 429 | if (methods[i]->getSelector() == Sel) { |
| 430 | return methods[i]; |
| 431 | } |
| 432 | } |
Steve Naroff | ff1afdb | 2007-10-14 23:13:51 +0000 | [diff] [blame] | 433 | // Didn't find one yet - look through protocols. |
| 434 | ObjcProtocolDecl **protocols = ClassDecl->getReferencedProtocols(); |
| 435 | int numProtocols = ClassDecl->getNumIntfRefProtocols(); |
| 436 | for (int pIdx = 0; pIdx < numProtocols; pIdx++) { |
| 437 | ObjcMethodDecl **methods = protocols[pIdx]->getInstanceMethods(); |
| 438 | int methodCount = protocols[pIdx]->getNumInstanceMethods(); |
| 439 | for (int i = 0; i < methodCount; ++i) { |
| 440 | if (methods[i]->getSelector() == Sel) { |
| 441 | return methods[i]; |
| 442 | } |
| 443 | } |
| 444 | } |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 445 | // Didn't find one yet - now look through categories. |
Steve Naroff | ff1afdb | 2007-10-14 23:13:51 +0000 | [diff] [blame] | 446 | ObjcCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 447 | while (CatDecl) { |
| 448 | ObjcMethodDecl **methods = CatDecl->getInstanceMethods(); |
| 449 | int methodCount = CatDecl->getNumInstanceMethods(); |
| 450 | for (int i = 0; i < methodCount; ++i) { |
| 451 | if (methods[i]->getSelector() == Sel) { |
| 452 | return methods[i]; |
| 453 | } |
| 454 | } |
| 455 | CatDecl = CatDecl->getNextClassCategory(); |
| 456 | } |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 457 | ClassDecl = ClassDecl->getSuperClass(); |
| 458 | } |
| 459 | return NULL; |
| 460 | } |
| 461 | |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 462 | // lookupClassMethod - This method returns a class method by looking in the |
| 463 | // 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] | 464 | ObjcMethodDecl *ObjcInterfaceDecl::lookupClassMethod(Selector &Sel) { |
| 465 | ObjcInterfaceDecl* ClassDecl = this; |
| 466 | while (ClassDecl != NULL) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 467 | ObjcMethodDecl **methods = ClassDecl->getClassMethods(); |
| 468 | int methodCount = ClassDecl->getNumClassMethods(); |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 469 | for (int i = 0; i < methodCount; ++i) { |
| 470 | if (methods[i]->getSelector() == Sel) { |
| 471 | return methods[i]; |
| 472 | } |
| 473 | } |
Steve Naroff | ff1afdb | 2007-10-14 23:13:51 +0000 | [diff] [blame] | 474 | // Didn't find one yet - look through protocols. |
| 475 | ObjcProtocolDecl **protocols = ClassDecl->getReferencedProtocols(); |
| 476 | int numProtocols = ClassDecl->getNumIntfRefProtocols(); |
| 477 | for (int pIdx = 0; pIdx < numProtocols; pIdx++) { |
| 478 | ObjcMethodDecl **methods = protocols[pIdx]->getClassMethods(); |
| 479 | int methodCount = protocols[pIdx]->getNumClassMethods(); |
| 480 | for (int i = 0; i < methodCount; ++i) { |
| 481 | if (methods[i]->getSelector() == Sel) { |
| 482 | return methods[i]; |
| 483 | } |
| 484 | } |
| 485 | } |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 486 | // Didn't find one yet - now look through categories. |
Steve Naroff | ff1afdb | 2007-10-14 23:13:51 +0000 | [diff] [blame] | 487 | ObjcCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 488 | while (CatDecl) { |
| 489 | ObjcMethodDecl **methods = CatDecl->getClassMethods(); |
| 490 | int methodCount = CatDecl->getNumClassMethods(); |
| 491 | for (int i = 0; i < methodCount; ++i) { |
| 492 | if (methods[i]->getSelector() == Sel) { |
| 493 | return methods[i]; |
| 494 | } |
| 495 | } |
| 496 | CatDecl = CatDecl->getNextClassCategory(); |
| 497 | } |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 498 | ClassDecl = ClassDecl->getSuperClass(); |
| 499 | } |
| 500 | return NULL; |
| 501 | } |
| 502 | |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 503 | |