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