Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 1 | //===--- DeclObjC.cpp - ObjC 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 Objective-C related Decl classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/DeclObjC.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | using namespace clang; |
| 17 | |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 18 | //===----------------------------------------------------------------------===// |
| 19 | // ObjC Decl Allocation/Deallocation Method Implementations |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 22 | ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C, |
| 23 | SourceLocation beginLoc, |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 24 | SourceLocation endLoc, |
| 25 | Selector SelInfo, QualType T, |
| 26 | Decl *contextDecl, |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 27 | AttributeList *M, bool isInstance, |
| 28 | bool isVariadic, |
Chris Lattner | b06fa3b | 2008-03-16 00:58:16 +0000 | [diff] [blame] | 29 | ImplementationControl impControl) { |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 30 | void *Mem = C.getAllocator().Allocate<ObjCMethodDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 31 | return new (Mem) ObjCMethodDecl(beginLoc, endLoc, |
| 32 | SelInfo, T, contextDecl, |
Chris Lattner | b06fa3b | 2008-03-16 00:58:16 +0000 | [diff] [blame] | 33 | M, isInstance, |
| 34 | isVariadic, impControl); |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 35 | } |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 37 | ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C, |
| 38 | SourceLocation atLoc, |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 39 | unsigned numRefProtos, |
| 40 | IdentifierInfo *Id, |
| 41 | bool ForwardDecl, bool isInternal){ |
| 42 | void *Mem = C.getAllocator().Allocate<ObjCInterfaceDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 43 | return new (Mem) ObjCInterfaceDecl(atLoc, numRefProtos, |
| 44 | Id, ForwardDecl, |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 45 | isInternal); |
| 46 | } |
| 47 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 48 | ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L, |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 49 | IdentifierInfo *Id, QualType T) { |
| 50 | void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>(); |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 51 | return new (Mem) ObjCIvarDecl(L, Id, T); |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 54 | ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, |
| 55 | SourceLocation L, |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 56 | unsigned numRefProtos, |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 57 | IdentifierInfo *Id) { |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 58 | void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>(); |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 59 | return new (Mem) ObjCProtocolDecl(L, numRefProtos, Id); |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 62 | ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, |
| 63 | SourceLocation L, |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 64 | ObjCInterfaceDecl **Elts, unsigned nElts) { |
| 65 | void *Mem = C.getAllocator().Allocate<ObjCClassDecl>(); |
| 66 | return new (Mem) ObjCClassDecl(L, Elts, nElts); |
| 67 | } |
| 68 | |
| 69 | ObjCForwardProtocolDecl * |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 70 | ObjCForwardProtocolDecl::Create(ASTContext &C, |
| 71 | SourceLocation L, |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 72 | ObjCProtocolDecl **Elts, unsigned NumElts) { |
| 73 | void *Mem = C.getAllocator().Allocate<ObjCForwardProtocolDecl>(); |
| 74 | return new (Mem) ObjCForwardProtocolDecl(L, Elts, NumElts); |
| 75 | } |
| 76 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 77 | ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, |
| 78 | SourceLocation L, |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 79 | IdentifierInfo *Id) { |
| 80 | void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>(); |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 81 | return new (Mem) ObjCCategoryDecl(L, Id); |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 84 | ObjCCategoryImplDecl * |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 85 | ObjCCategoryImplDecl::Create(ASTContext &C, |
| 86 | SourceLocation L,IdentifierInfo *Id, |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 87 | ObjCInterfaceDecl *ClassInterface) { |
| 88 | void *Mem = C.getAllocator().Allocate<ObjCCategoryImplDecl>(); |
| 89 | return new (Mem) ObjCCategoryImplDecl(L, Id, ClassInterface); |
| 90 | } |
| 91 | |
| 92 | ObjCImplementationDecl * |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 93 | ObjCImplementationDecl::Create(ASTContext &C, |
| 94 | SourceLocation L, |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 95 | IdentifierInfo *Id, |
| 96 | ObjCInterfaceDecl *ClassInterface, |
| 97 | ObjCInterfaceDecl *SuperDecl) { |
| 98 | void *Mem = C.getAllocator().Allocate<ObjCImplementationDecl>(); |
| 99 | return new (Mem) ObjCImplementationDecl(L, Id, ClassInterface, SuperDecl); |
| 100 | } |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 101 | |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 102 | ObjCCompatibleAliasDecl * |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 103 | ObjCCompatibleAliasDecl::Create(ASTContext &C, |
| 104 | SourceLocation L, |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 105 | IdentifierInfo *Id, |
| 106 | ObjCInterfaceDecl* AliasedClass) { |
| 107 | void *Mem = C.getAllocator().Allocate<ObjCCompatibleAliasDecl>(); |
| 108 | return new (Mem) ObjCCompatibleAliasDecl(L, Id, AliasedClass); |
| 109 | } |
| 110 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 111 | ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, |
| 112 | SourceLocation L) { |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 113 | void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>(); |
| 114 | return new (Mem) ObjCPropertyDecl(L); |
| 115 | } |
| 116 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 117 | //===----------------------------------------------------------------------===// |
| 118 | // Objective-C Decl Implementation |
| 119 | //===----------------------------------------------------------------------===// |
| 120 | |
| 121 | void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo, |
| 122 | unsigned NumParams) { |
| 123 | assert(ParamInfo == 0 && "Already has param info!"); |
| 124 | |
| 125 | // Zero params -> null pointer. |
| 126 | if (NumParams) { |
| 127 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 128 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 129 | NumMethodParams = NumParams; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | ObjCMethodDecl::~ObjCMethodDecl() { |
| 134 | delete[] ParamInfo; |
| 135 | } |
| 136 | |
| 137 | /// ObjCAddInstanceVariablesToClass - Inserts instance variables |
| 138 | /// into ObjCInterfaceDecl's fields. |
| 139 | /// |
| 140 | void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars, |
| 141 | unsigned numIvars, |
| 142 | SourceLocation RBrac) { |
| 143 | NumIvars = numIvars; |
| 144 | if (numIvars) { |
| 145 | Ivars = new ObjCIvarDecl*[numIvars]; |
| 146 | memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*)); |
| 147 | } |
| 148 | setLocEnd(RBrac); |
| 149 | } |
| 150 | |
| 151 | /// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance |
| 152 | /// Variables (Ivars) relative to what declared in @implementation;s class. |
| 153 | /// Ivars into ObjCImplementationDecl's fields. |
| 154 | /// |
| 155 | void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl( |
| 156 | ObjCIvarDecl **ivars, unsigned numIvars) { |
| 157 | NumIvars = numIvars; |
| 158 | if (numIvars) { |
| 159 | Ivars = new ObjCIvarDecl*[numIvars]; |
| 160 | memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*)); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /// addMethods - Insert instance and methods declarations into |
| 165 | /// ObjCInterfaceDecl's InsMethods and ClsMethods fields. |
| 166 | /// |
| 167 | void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods, |
| 168 | unsigned numInsMembers, |
| 169 | ObjCMethodDecl **clsMethods, |
| 170 | unsigned numClsMembers, |
| 171 | SourceLocation endLoc) { |
| 172 | NumInstanceMethods = numInsMembers; |
| 173 | if (numInsMembers) { |
| 174 | InstanceMethods = new ObjCMethodDecl*[numInsMembers]; |
| 175 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*)); |
| 176 | } |
| 177 | NumClassMethods = numClsMembers; |
| 178 | if (numClsMembers) { |
| 179 | ClassMethods = new ObjCMethodDecl*[numClsMembers]; |
| 180 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*)); |
| 181 | } |
| 182 | AtEndLoc = endLoc; |
| 183 | } |
| 184 | |
| 185 | /// addMethods - Insert instance and methods declarations into |
Chris Lattner | 55d13b4 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 186 | /// ObjCInterfaceDecl's InsMethods and ClsMethods fields. |
| 187 | /// |
| 188 | void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties, |
| 189 | unsigned NumProperties) { |
| 190 | if (NumProperties == 0) return; |
| 191 | |
| 192 | NumPropertyDecl = NumProperties; |
| 193 | PropertyDecl = new ObjCPropertyDecl*[NumProperties]; |
| 194 | memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*)); |
| 195 | } |
| 196 | |
| 197 | /// addMethods - Insert instance and methods declarations into |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 198 | /// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields. |
| 199 | /// |
| 200 | void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods, |
| 201 | unsigned numInsMembers, |
| 202 | ObjCMethodDecl **clsMethods, |
| 203 | unsigned numClsMembers, |
| 204 | SourceLocation endLoc) { |
| 205 | NumInstanceMethods = numInsMembers; |
| 206 | if (numInsMembers) { |
| 207 | InstanceMethods = new ObjCMethodDecl*[numInsMembers]; |
| 208 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*)); |
| 209 | } |
| 210 | NumClassMethods = numClsMembers; |
| 211 | if (numClsMembers) { |
| 212 | ClassMethods = new ObjCMethodDecl*[numClsMembers]; |
| 213 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*)); |
| 214 | } |
| 215 | AtEndLoc = endLoc; |
| 216 | } |
| 217 | |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 218 | void ObjCCategoryDecl::setReferencedProtocolList(ObjCProtocolDecl **List, |
| 219 | unsigned NumRPs) { |
| 220 | assert(NumReferencedProtocols == 0 && "Protocol list already set"); |
| 221 | if (NumRPs == 0) return; |
| 222 | |
| 223 | ReferencedProtocols = new ObjCProtocolDecl*[NumRPs]; |
| 224 | memcpy(ReferencedProtocols, List, NumRPs*sizeof(ObjCProtocolDecl*)); |
| 225 | NumReferencedProtocols = NumRPs; |
| 226 | } |
| 227 | |
| 228 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 229 | /// addMethods - Insert instance and methods declarations into |
| 230 | /// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields. |
| 231 | /// |
| 232 | void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods, |
| 233 | unsigned numInsMembers, |
| 234 | ObjCMethodDecl **clsMethods, |
| 235 | unsigned numClsMembers, |
| 236 | SourceLocation endLoc) { |
| 237 | NumInstanceMethods = numInsMembers; |
| 238 | if (numInsMembers) { |
| 239 | InstanceMethods = new ObjCMethodDecl*[numInsMembers]; |
| 240 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*)); |
| 241 | } |
| 242 | NumClassMethods = numClsMembers; |
| 243 | if (numClsMembers) { |
| 244 | ClassMethods = new ObjCMethodDecl*[numClsMembers]; |
| 245 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*)); |
| 246 | } |
| 247 | AtEndLoc = endLoc; |
| 248 | } |
| 249 | |
| 250 | ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable( |
| 251 | IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) { |
| 252 | ObjCInterfaceDecl* ClassDecl = this; |
| 253 | while (ClassDecl != NULL) { |
| 254 | for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end(); |
| 255 | I != E; ++I) { |
| 256 | if ((*I)->getIdentifier() == ID) { |
| 257 | clsDeclared = ClassDecl; |
| 258 | return *I; |
| 259 | } |
| 260 | } |
| 261 | ClassDecl = ClassDecl->getSuperClass(); |
| 262 | } |
| 263 | return NULL; |
| 264 | } |
| 265 | |
| 266 | /// lookupInstanceMethod - This method returns an instance method by looking in |
| 267 | /// the class, its categories, and its super classes (using a linear search). |
| 268 | ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) { |
| 269 | ObjCInterfaceDecl* ClassDecl = this; |
| 270 | ObjCMethodDecl *MethodDecl = 0; |
| 271 | |
| 272 | while (ClassDecl != NULL) { |
| 273 | if ((MethodDecl = ClassDecl->getInstanceMethod(Sel))) |
| 274 | return MethodDecl; |
| 275 | |
| 276 | // Didn't find one yet - look through protocols. |
| 277 | ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols(); |
| 278 | int numProtocols = ClassDecl->getNumIntfRefProtocols(); |
| 279 | for (int pIdx = 0; pIdx < numProtocols; pIdx++) { |
| 280 | if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel))) |
| 281 | return MethodDecl; |
| 282 | } |
| 283 | // Didn't find one yet - now look through categories. |
| 284 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 285 | while (CatDecl) { |
| 286 | if ((MethodDecl = CatDecl->getInstanceMethod(Sel))) |
| 287 | return MethodDecl; |
| 288 | CatDecl = CatDecl->getNextClassCategory(); |
| 289 | } |
| 290 | ClassDecl = ClassDecl->getSuperClass(); |
| 291 | } |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | // lookupClassMethod - This method returns a class method by looking in the |
| 296 | // class, its categories, and its super classes (using a linear search). |
| 297 | ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) { |
| 298 | ObjCInterfaceDecl* ClassDecl = this; |
| 299 | ObjCMethodDecl *MethodDecl = 0; |
| 300 | |
| 301 | while (ClassDecl != NULL) { |
| 302 | if ((MethodDecl = ClassDecl->getClassMethod(Sel))) |
| 303 | return MethodDecl; |
| 304 | |
| 305 | // Didn't find one yet - look through protocols. |
| 306 | ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols(); |
| 307 | int numProtocols = ClassDecl->getNumIntfRefProtocols(); |
| 308 | for (int pIdx = 0; pIdx < numProtocols; pIdx++) { |
| 309 | if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel))) |
| 310 | return MethodDecl; |
| 311 | } |
| 312 | // Didn't find one yet - now look through categories. |
| 313 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 314 | while (CatDecl) { |
| 315 | if ((MethodDecl = CatDecl->getClassMethod(Sel))) |
| 316 | return MethodDecl; |
| 317 | CatDecl = CatDecl->getNextClassCategory(); |
| 318 | } |
| 319 | ClassDecl = ClassDecl->getSuperClass(); |
| 320 | } |
| 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | /// lookupInstanceMethod - This method returns an instance method by looking in |
| 325 | /// the class implementation. Unlike interfaces, we don't look outside the |
| 326 | /// implementation. |
| 327 | ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) { |
| 328 | for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) |
| 329 | if ((*I)->getSelector() == Sel) |
| 330 | return *I; |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | /// lookupClassMethod - This method returns a class method by looking in |
| 335 | /// the class implementation. Unlike interfaces, we don't look outside the |
| 336 | /// implementation. |
| 337 | ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) { |
| 338 | for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); |
| 339 | I != E; ++I) |
| 340 | if ((*I)->getSelector() == Sel) |
| 341 | return *I; |
| 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | // lookupInstanceMethod - This method returns an instance method by looking in |
| 346 | // the class implementation. Unlike interfaces, we don't look outside the |
| 347 | // implementation. |
| 348 | ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) { |
| 349 | for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) |
| 350 | if ((*I)->getSelector() == Sel) |
| 351 | return *I; |
| 352 | return NULL; |
| 353 | } |
| 354 | |
| 355 | // lookupClassMethod - This method returns an instance method by looking in |
| 356 | // the class implementation. Unlike interfaces, we don't look outside the |
| 357 | // implementation. |
| 358 | ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) { |
| 359 | for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); |
| 360 | I != E; ++I) |
| 361 | if ((*I)->getSelector() == Sel) |
| 362 | return *I; |
| 363 | return NULL; |
| 364 | } |
| 365 | |
| 366 | // lookupInstanceMethod - Lookup a instance method in the protocol and protocols |
| 367 | // it inherited. |
| 368 | ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) { |
| 369 | ObjCMethodDecl *MethodDecl = NULL; |
| 370 | |
| 371 | if ((MethodDecl = getInstanceMethod(Sel))) |
| 372 | return MethodDecl; |
| 373 | |
| 374 | if (getNumReferencedProtocols() > 0) { |
| 375 | ObjCProtocolDecl **RefPDecl = getReferencedProtocols(); |
| 376 | |
| 377 | for (unsigned i = 0; i < getNumReferencedProtocols(); i++) { |
| 378 | if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel))) |
| 379 | return MethodDecl; |
| 380 | } |
| 381 | } |
| 382 | return NULL; |
| 383 | } |
| 384 | |
| 385 | // lookupInstanceMethod - Lookup a class method in the protocol and protocols |
| 386 | // it inherited. |
| 387 | ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) { |
| 388 | ObjCMethodDecl *MethodDecl = NULL; |
| 389 | |
| 390 | if ((MethodDecl = getClassMethod(Sel))) |
| 391 | return MethodDecl; |
| 392 | |
| 393 | if (getNumReferencedProtocols() > 0) { |
| 394 | ObjCProtocolDecl **RefPDecl = getReferencedProtocols(); |
| 395 | |
| 396 | for(unsigned i = 0; i < getNumReferencedProtocols(); i++) { |
| 397 | if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel))) |
| 398 | return MethodDecl; |
| 399 | } |
| 400 | } |
| 401 | return NULL; |
| 402 | } |
| 403 | |
| 404 | /// getSynthesizedMethodSize - Compute size of synthesized method name |
| 405 | /// as done be the rewrite. |
| 406 | /// |
| 407 | unsigned ObjCMethodDecl::getSynthesizedMethodSize() const { |
| 408 | // syntesized method name is a concatenation of -/+[class-name selector] |
| 409 | // Get length of this name. |
| 410 | unsigned length = 3; // _I_ or _C_ |
| 411 | length += strlen(getClassInterface()->getName()) +1; // extra for _ |
| 412 | NamedDecl *MethodContext = getMethodContext(); |
| 413 | if (ObjCCategoryImplDecl *CID = |
| 414 | dyn_cast<ObjCCategoryImplDecl>(MethodContext)) |
| 415 | length += strlen(CID->getName()) +1; |
| 416 | length += getSelector().getName().size(); // selector name |
| 417 | return length; |
| 418 | } |
| 419 | |
Chris Lattner | 5619688 | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 420 | ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() { |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 421 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext)) |
| 422 | return ID; |
| 423 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext)) |
| 424 | return CD->getClassInterface(); |
| 425 | if (ObjCImplementationDecl *IMD = |
Chris Lattner | 5619688 | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 426 | dyn_cast<ObjCImplementationDecl>(MethodContext)) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 427 | return IMD->getClassInterface(); |
Chris Lattner | 5619688 | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 428 | if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext)) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 429 | return CID->getClassInterface(); |
| 430 | assert(false && "unknown method context"); |
| 431 | return 0; |
| 432 | } |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 433 | |
| 434 | void ObjCPropertyDecl::setPropertyDeclLists(ObjCIvarDecl **Properties, |
| 435 | unsigned NumProp) { |
| 436 | assert(PropertyDecls == 0 && "Properties already set"); |
| 437 | if (NumProp == 0) return; |
| 438 | NumPropertyDecls = NumProp; |
| 439 | |
| 440 | PropertyDecls = new ObjCIvarDecl*[NumProp]; |
| 441 | memcpy(PropertyDecls, Properties, NumProp*sizeof(ObjCIvarDecl*)); |
| 442 | } |
| 443 | |
| 444 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 445 | |