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, |
Fariborz Jahanian | 4607034 | 2008-05-07 20:53:44 +0000 | [diff] [blame] | 29 | bool isSynthesized, |
Chris Lattner | b06fa3b | 2008-03-16 00:58:16 +0000 | [diff] [blame] | 30 | ImplementationControl impControl) { |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 31 | void *Mem = C.getAllocator().Allocate<ObjCMethodDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 32 | return new (Mem) ObjCMethodDecl(beginLoc, endLoc, |
| 33 | SelInfo, T, contextDecl, |
Chris Lattner | b06fa3b | 2008-03-16 00:58:16 +0000 | [diff] [blame] | 34 | M, isInstance, |
Fariborz Jahanian | 4607034 | 2008-05-07 20:53:44 +0000 | [diff] [blame] | 35 | isVariadic, isSynthesized, impControl); |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 36 | } |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 38 | ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C, |
| 39 | SourceLocation atLoc, |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 40 | unsigned numRefProtos, |
Steve Naroff | d6a07aa | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 41 | IdentifierInfo *Id, |
| 42 | SourceLocation ClassLoc, |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 43 | bool ForwardDecl, bool isInternal){ |
| 44 | void *Mem = C.getAllocator().Allocate<ObjCInterfaceDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 45 | return new (Mem) ObjCInterfaceDecl(atLoc, numRefProtos, |
Steve Naroff | d6a07aa | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 46 | Id, ClassLoc, ForwardDecl, |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 47 | isInternal); |
| 48 | } |
| 49 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 50 | ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L, |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 51 | IdentifierInfo *Id, QualType T) { |
| 52 | void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>(); |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 53 | return new (Mem) ObjCIvarDecl(L, Id, T); |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 56 | ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, |
| 57 | SourceLocation L, |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 58 | unsigned numRefProtos, |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 59 | IdentifierInfo *Id) { |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 60 | void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>(); |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 61 | return new (Mem) ObjCProtocolDecl(L, numRefProtos, Id); |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 64 | ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, |
| 65 | SourceLocation L, |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 66 | ObjCInterfaceDecl **Elts, unsigned nElts) { |
| 67 | void *Mem = C.getAllocator().Allocate<ObjCClassDecl>(); |
| 68 | return new (Mem) ObjCClassDecl(L, Elts, nElts); |
| 69 | } |
| 70 | |
| 71 | ObjCForwardProtocolDecl * |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 72 | ObjCForwardProtocolDecl::Create(ASTContext &C, |
| 73 | SourceLocation L, |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 74 | ObjCProtocolDecl **Elts, unsigned NumElts) { |
| 75 | void *Mem = C.getAllocator().Allocate<ObjCForwardProtocolDecl>(); |
| 76 | return new (Mem) ObjCForwardProtocolDecl(L, Elts, NumElts); |
| 77 | } |
| 78 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 79 | ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, |
| 80 | SourceLocation L, |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 81 | IdentifierInfo *Id) { |
| 82 | void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>(); |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 83 | return new (Mem) ObjCCategoryDecl(L, Id); |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 86 | ObjCCategoryImplDecl * |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 87 | ObjCCategoryImplDecl::Create(ASTContext &C, |
| 88 | SourceLocation L,IdentifierInfo *Id, |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 89 | ObjCInterfaceDecl *ClassInterface) { |
| 90 | void *Mem = C.getAllocator().Allocate<ObjCCategoryImplDecl>(); |
| 91 | return new (Mem) ObjCCategoryImplDecl(L, Id, ClassInterface); |
| 92 | } |
| 93 | |
| 94 | ObjCImplementationDecl * |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 95 | ObjCImplementationDecl::Create(ASTContext &C, |
| 96 | SourceLocation L, |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 97 | IdentifierInfo *Id, |
| 98 | ObjCInterfaceDecl *ClassInterface, |
| 99 | ObjCInterfaceDecl *SuperDecl) { |
| 100 | void *Mem = C.getAllocator().Allocate<ObjCImplementationDecl>(); |
| 101 | return new (Mem) ObjCImplementationDecl(L, Id, ClassInterface, SuperDecl); |
| 102 | } |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 103 | |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 104 | ObjCCompatibleAliasDecl * |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 105 | ObjCCompatibleAliasDecl::Create(ASTContext &C, |
| 106 | SourceLocation L, |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 107 | IdentifierInfo *Id, |
| 108 | ObjCInterfaceDecl* AliasedClass) { |
| 109 | void *Mem = C.getAllocator().Allocate<ObjCCompatibleAliasDecl>(); |
| 110 | return new (Mem) ObjCCompatibleAliasDecl(L, Id, AliasedClass); |
| 111 | } |
| 112 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 113 | ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, |
Fariborz Jahanian | dae1a1a | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 114 | SourceLocation L, |
Fariborz Jahanian | 1de1e74 | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 115 | IdentifierInfo *Id, |
Fariborz Jahanian | 46b55e5 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 116 | QualType T, |
| 117 | PropertyControl propControl) { |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 118 | void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>(); |
Fariborz Jahanian | 1de1e74 | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 119 | return new (Mem) ObjCPropertyDecl(L, Id, T); |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 122 | //===----------------------------------------------------------------------===// |
| 123 | // Objective-C Decl Implementation |
| 124 | //===----------------------------------------------------------------------===// |
| 125 | |
| 126 | void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo, |
| 127 | unsigned NumParams) { |
| 128 | assert(ParamInfo == 0 && "Already has param info!"); |
| 129 | |
| 130 | // Zero params -> null pointer. |
| 131 | if (NumParams) { |
| 132 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 133 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 134 | NumMethodParams = NumParams; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | ObjCMethodDecl::~ObjCMethodDecl() { |
| 139 | delete[] ParamInfo; |
| 140 | } |
| 141 | |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 142 | /// FindPropertyDeclaration - Finds declaration of the property given its name |
| 143 | /// in 'PropertyId' and returns it. It returns 0, if not found. |
| 144 | /// |
| 145 | ObjCPropertyDecl * |
| 146 | ObjCInterfaceDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const { |
| 147 | for (ObjCInterfaceDecl::classprop_iterator I = classprop_begin(), |
| 148 | E = classprop_end(); I != E; ++I) { |
| 149 | ObjCPropertyDecl *property = *I; |
| 150 | if (property->getIdentifier() == PropertyId) |
| 151 | return property; |
| 152 | } |
Fariborz Jahanian | c70bee8 | 2008-04-21 23:57:08 +0000 | [diff] [blame] | 153 | if (getSuperClass()) |
| 154 | return getSuperClass()->FindPropertyDeclaration(PropertyId); |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /// FindCategoryDeclaration - Finds category declaration in the list of |
| 159 | /// categories for this class and returns it. Name of the category is passed |
| 160 | /// in 'CategoryId'. If category not found, return 0; |
| 161 | /// |
| 162 | ObjCCategoryDecl * |
| 163 | ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const { |
| 164 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 165 | Category; Category = Category->getNextClassCategory()) |
| 166 | if (Category->getIdentifier() == CategoryId) |
| 167 | return Category; |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /// FindIvarDeclaration - Find an Ivar declaration in this class given its |
| 172 | /// name in 'IvarId'. On failure to find, return 0; |
| 173 | /// |
| 174 | ObjCIvarDecl * |
| 175 | ObjCInterfaceDecl::FindIvarDeclaration(IdentifierInfo *IvarId) const { |
| 176 | for (ObjCInterfaceDecl::ivar_iterator IVI = ivar_begin(), |
| 177 | IVE = ivar_end(); IVI != IVE; ++IVI) { |
| 178 | ObjCIvarDecl* Ivar = (*IVI); |
| 179 | if (Ivar->getIdentifier() == IvarId) |
| 180 | return Ivar; |
| 181 | } |
Fariborz Jahanian | c70bee8 | 2008-04-21 23:57:08 +0000 | [diff] [blame] | 182 | if (getSuperClass()) |
| 183 | return getSuperClass()->FindIvarDeclaration(IvarId); |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 187 | /// ObjCAddInstanceVariablesToClass - Inserts instance variables |
| 188 | /// into ObjCInterfaceDecl's fields. |
| 189 | /// |
| 190 | void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars, |
| 191 | unsigned numIvars, |
| 192 | SourceLocation RBrac) { |
| 193 | NumIvars = numIvars; |
| 194 | if (numIvars) { |
| 195 | Ivars = new ObjCIvarDecl*[numIvars]; |
| 196 | memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*)); |
| 197 | } |
| 198 | setLocEnd(RBrac); |
| 199 | } |
| 200 | |
| 201 | /// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance |
| 202 | /// Variables (Ivars) relative to what declared in @implementation;s class. |
| 203 | /// Ivars into ObjCImplementationDecl's fields. |
| 204 | /// |
| 205 | void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl( |
| 206 | ObjCIvarDecl **ivars, unsigned numIvars) { |
| 207 | NumIvars = numIvars; |
| 208 | if (numIvars) { |
| 209 | Ivars = new ObjCIvarDecl*[numIvars]; |
| 210 | memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*)); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /// addMethods - Insert instance and methods declarations into |
| 215 | /// ObjCInterfaceDecl's InsMethods and ClsMethods fields. |
| 216 | /// |
| 217 | void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods, |
| 218 | unsigned numInsMembers, |
| 219 | ObjCMethodDecl **clsMethods, |
| 220 | unsigned numClsMembers, |
| 221 | SourceLocation endLoc) { |
| 222 | NumInstanceMethods = numInsMembers; |
| 223 | if (numInsMembers) { |
| 224 | InstanceMethods = new ObjCMethodDecl*[numInsMembers]; |
| 225 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*)); |
| 226 | } |
| 227 | NumClassMethods = numClsMembers; |
| 228 | if (numClsMembers) { |
| 229 | ClassMethods = new ObjCMethodDecl*[numClsMembers]; |
| 230 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*)); |
| 231 | } |
| 232 | AtEndLoc = endLoc; |
| 233 | } |
| 234 | |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 235 | /// addProperties - Insert property declaration AST nodes into |
| 236 | /// ObjCInterfaceDecl's PropertyDecl field. |
Chris Lattner | 55d13b4 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 237 | /// |
| 238 | void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties, |
| 239 | unsigned NumProperties) { |
| 240 | if (NumProperties == 0) return; |
| 241 | |
| 242 | NumPropertyDecl = NumProperties; |
| 243 | PropertyDecl = new ObjCPropertyDecl*[NumProperties]; |
| 244 | memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*)); |
| 245 | } |
| 246 | |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 247 | /// mergeProperties - Adds properties to the end of list of current properties |
| 248 | /// for this class. |
| 249 | |
| 250 | void ObjCInterfaceDecl::mergeProperties(ObjCPropertyDecl **Properties, |
| 251 | unsigned NumNewProperties) { |
| 252 | if (NumNewProperties == 0) return; |
| 253 | |
| 254 | if (PropertyDecl) { |
| 255 | ObjCPropertyDecl **newPropertyDecl = |
| 256 | new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl]; |
| 257 | ObjCPropertyDecl **buf = newPropertyDecl; |
| 258 | // put back original properties in buffer. |
| 259 | memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*)); |
| 260 | // Add new properties to this buffer. |
| 261 | memcpy(buf+NumPropertyDecl, Properties, |
| 262 | NumNewProperties*sizeof(ObjCPropertyDecl*)); |
Nuno Lopes | 6b3502c | 2008-05-10 10:31:54 +0000 | [diff] [blame] | 263 | delete[] PropertyDecl; |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 264 | PropertyDecl = newPropertyDecl; |
| 265 | NumPropertyDecl += NumNewProperties; |
| 266 | } |
| 267 | else { |
Nuno Lopes | 6b3502c | 2008-05-10 10:31:54 +0000 | [diff] [blame] | 268 | addProperties(Properties, NumNewProperties); |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 272 | /// addPropertyMethods - Goes through list of properties declared in this class |
| 273 | /// and builds setter/getter method declartions depending on the setter/getter |
| 274 | /// attributes of the property. |
| 275 | /// |
| 276 | void ObjCInterfaceDecl::addPropertyMethods( |
| 277 | ASTContext &Context, |
| 278 | ObjCPropertyDecl *property, |
| 279 | llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods) { |
| 280 | // Find the default getter and if one not found, add one. |
| 281 | ObjCMethodDecl *GetterDecl = getInstanceMethod(property->getGetterName()); |
| 282 | if (GetterDecl) { |
| 283 | // An instance method with same name as property getter name found. |
| 284 | property->setGetterMethodDecl(GetterDecl); |
| 285 | } |
| 286 | else { |
| 287 | // No instance method of same name as property getter name was found. |
| 288 | // Declare a getter method and add it to the list of methods |
| 289 | // for this class. |
| 290 | QualType resultDeclType = property->getType(); |
| 291 | ObjCMethodDecl* ObjCMethod = |
| 292 | ObjCMethodDecl::Create(Context, property->getLocation(), |
| 293 | property->getLocation(), |
| 294 | property->getGetterName(), resultDeclType, |
| 295 | this, 0, |
Fariborz Jahanian | 4607034 | 2008-05-07 20:53:44 +0000 | [diff] [blame] | 296 | true, false, true, ObjCMethodDecl::Required); |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 297 | property->setGetterMethodDecl(ObjCMethod); |
| 298 | insMethods.push_back(ObjCMethod); |
| 299 | } |
| 300 | } |
| 301 | |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 302 | /// addProperties - Insert property declaration AST nodes into |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 303 | /// ObjCProtocolDecl's PropertyDecl field. |
| 304 | /// |
| 305 | void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties, |
| 306 | unsigned NumProperties) { |
| 307 | if (NumProperties == 0) return; |
| 308 | |
| 309 | NumPropertyDecl = NumProperties; |
| 310 | PropertyDecl = new ObjCPropertyDecl*[NumProperties]; |
| 311 | memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*)); |
| 312 | } |
| 313 | |
| 314 | /// addProperties - Insert property declaration AST nodes into |
Fariborz Jahanian | d9a3c33 | 2008-04-16 21:11:25 +0000 | [diff] [blame] | 315 | /// ObjCCategoryDecl's PropertyDecl field. |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 316 | /// |
| 317 | void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties, |
| 318 | unsigned NumProperties) { |
| 319 | if (NumProperties == 0) return; |
| 320 | |
| 321 | NumPropertyDecl = NumProperties; |
| 322 | PropertyDecl = new ObjCPropertyDecl*[NumProperties]; |
| 323 | memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*)); |
| 324 | } |
| 325 | |
Chris Lattner | 55d13b4 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 326 | /// addMethods - Insert instance and methods declarations into |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 327 | /// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields. |
| 328 | /// |
| 329 | void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods, |
| 330 | unsigned numInsMembers, |
| 331 | ObjCMethodDecl **clsMethods, |
| 332 | unsigned numClsMembers, |
| 333 | SourceLocation endLoc) { |
| 334 | NumInstanceMethods = numInsMembers; |
| 335 | if (numInsMembers) { |
| 336 | InstanceMethods = new ObjCMethodDecl*[numInsMembers]; |
| 337 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*)); |
| 338 | } |
| 339 | NumClassMethods = numClsMembers; |
| 340 | if (numClsMembers) { |
| 341 | ClassMethods = new ObjCMethodDecl*[numClsMembers]; |
| 342 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*)); |
| 343 | } |
| 344 | AtEndLoc = endLoc; |
| 345 | } |
| 346 | |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 347 | void ObjCCategoryDecl::setReferencedProtocolList(ObjCProtocolDecl **List, |
| 348 | unsigned NumRPs) { |
| 349 | assert(NumReferencedProtocols == 0 && "Protocol list already set"); |
| 350 | if (NumRPs == 0) return; |
| 351 | |
| 352 | ReferencedProtocols = new ObjCProtocolDecl*[NumRPs]; |
| 353 | memcpy(ReferencedProtocols, List, NumRPs*sizeof(ObjCProtocolDecl*)); |
| 354 | NumReferencedProtocols = NumRPs; |
| 355 | } |
| 356 | |
| 357 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 358 | /// addMethods - Insert instance and methods declarations into |
| 359 | /// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields. |
| 360 | /// |
| 361 | void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods, |
| 362 | unsigned numInsMembers, |
| 363 | ObjCMethodDecl **clsMethods, |
| 364 | unsigned numClsMembers, |
| 365 | SourceLocation endLoc) { |
| 366 | NumInstanceMethods = numInsMembers; |
| 367 | if (numInsMembers) { |
| 368 | InstanceMethods = new ObjCMethodDecl*[numInsMembers]; |
| 369 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*)); |
| 370 | } |
| 371 | NumClassMethods = numClsMembers; |
| 372 | if (numClsMembers) { |
| 373 | ClassMethods = new ObjCMethodDecl*[numClsMembers]; |
| 374 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*)); |
| 375 | } |
| 376 | AtEndLoc = endLoc; |
| 377 | } |
| 378 | |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 379 | /// FindPropertyDeclaration - Finds declaration of the property given its name |
| 380 | /// in 'PropertyId' and returns it. It returns 0, if not found. |
| 381 | /// |
| 382 | ObjCPropertyDecl * |
| 383 | ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const { |
| 384 | for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(), |
| 385 | E = classprop_end(); I != E; ++I) { |
| 386 | ObjCPropertyDecl *property = *I; |
| 387 | if (property->getIdentifier() == PropertyId) |
| 388 | return property; |
| 389 | } |
| 390 | return 0; |
| 391 | } |
| 392 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 393 | ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable( |
| 394 | IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) { |
| 395 | ObjCInterfaceDecl* ClassDecl = this; |
| 396 | while (ClassDecl != NULL) { |
| 397 | for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end(); |
| 398 | I != E; ++I) { |
| 399 | if ((*I)->getIdentifier() == ID) { |
| 400 | clsDeclared = ClassDecl; |
| 401 | return *I; |
| 402 | } |
| 403 | } |
| 404 | ClassDecl = ClassDecl->getSuperClass(); |
| 405 | } |
| 406 | return NULL; |
| 407 | } |
| 408 | |
| 409 | /// lookupInstanceMethod - This method returns an instance method by looking in |
| 410 | /// the class, its categories, and its super classes (using a linear search). |
| 411 | ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) { |
| 412 | ObjCInterfaceDecl* ClassDecl = this; |
| 413 | ObjCMethodDecl *MethodDecl = 0; |
| 414 | |
| 415 | while (ClassDecl != NULL) { |
| 416 | if ((MethodDecl = ClassDecl->getInstanceMethod(Sel))) |
| 417 | return MethodDecl; |
| 418 | |
| 419 | // Didn't find one yet - look through protocols. |
| 420 | ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols(); |
| 421 | int numProtocols = ClassDecl->getNumIntfRefProtocols(); |
| 422 | for (int pIdx = 0; pIdx < numProtocols; pIdx++) { |
| 423 | if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel))) |
| 424 | return MethodDecl; |
| 425 | } |
| 426 | // Didn't find one yet - now look through categories. |
| 427 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 428 | while (CatDecl) { |
| 429 | if ((MethodDecl = CatDecl->getInstanceMethod(Sel))) |
| 430 | return MethodDecl; |
| 431 | CatDecl = CatDecl->getNextClassCategory(); |
| 432 | } |
| 433 | ClassDecl = ClassDecl->getSuperClass(); |
| 434 | } |
| 435 | return NULL; |
| 436 | } |
| 437 | |
| 438 | // lookupClassMethod - This method returns a class method by looking in the |
| 439 | // class, its categories, and its super classes (using a linear search). |
| 440 | ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) { |
| 441 | ObjCInterfaceDecl* ClassDecl = this; |
| 442 | ObjCMethodDecl *MethodDecl = 0; |
| 443 | |
| 444 | while (ClassDecl != NULL) { |
| 445 | if ((MethodDecl = ClassDecl->getClassMethod(Sel))) |
| 446 | return MethodDecl; |
| 447 | |
| 448 | // Didn't find one yet - look through protocols. |
| 449 | ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols(); |
| 450 | int numProtocols = ClassDecl->getNumIntfRefProtocols(); |
| 451 | for (int pIdx = 0; pIdx < numProtocols; pIdx++) { |
| 452 | if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel))) |
| 453 | return MethodDecl; |
| 454 | } |
| 455 | // Didn't find one yet - now look through categories. |
| 456 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 457 | while (CatDecl) { |
| 458 | if ((MethodDecl = CatDecl->getClassMethod(Sel))) |
| 459 | return MethodDecl; |
| 460 | CatDecl = CatDecl->getNextClassCategory(); |
| 461 | } |
| 462 | ClassDecl = ClassDecl->getSuperClass(); |
| 463 | } |
| 464 | return NULL; |
| 465 | } |
| 466 | |
| 467 | /// lookupInstanceMethod - This method returns an instance method by looking in |
| 468 | /// the class implementation. Unlike interfaces, we don't look outside the |
| 469 | /// implementation. |
| 470 | ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) { |
| 471 | for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) |
| 472 | if ((*I)->getSelector() == Sel) |
| 473 | return *I; |
| 474 | return NULL; |
| 475 | } |
| 476 | |
| 477 | /// lookupClassMethod - This method returns a class method by looking in |
| 478 | /// the class implementation. Unlike interfaces, we don't look outside the |
| 479 | /// implementation. |
| 480 | ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) { |
| 481 | for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); |
| 482 | I != E; ++I) |
| 483 | if ((*I)->getSelector() == Sel) |
| 484 | return *I; |
| 485 | return NULL; |
| 486 | } |
| 487 | |
| 488 | // lookupInstanceMethod - This method returns an instance method by looking in |
| 489 | // the class implementation. Unlike interfaces, we don't look outside the |
| 490 | // implementation. |
| 491 | ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) { |
| 492 | for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) |
| 493 | if ((*I)->getSelector() == Sel) |
| 494 | return *I; |
| 495 | return NULL; |
| 496 | } |
| 497 | |
| 498 | // lookupClassMethod - This method returns an instance method by looking in |
| 499 | // the class implementation. Unlike interfaces, we don't look outside the |
| 500 | // implementation. |
| 501 | ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) { |
| 502 | for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); |
| 503 | I != E; ++I) |
| 504 | if ((*I)->getSelector() == Sel) |
| 505 | return *I; |
| 506 | return NULL; |
| 507 | } |
| 508 | |
| 509 | // lookupInstanceMethod - Lookup a instance method in the protocol and protocols |
| 510 | // it inherited. |
| 511 | ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) { |
| 512 | ObjCMethodDecl *MethodDecl = NULL; |
| 513 | |
| 514 | if ((MethodDecl = getInstanceMethod(Sel))) |
| 515 | return MethodDecl; |
| 516 | |
| 517 | if (getNumReferencedProtocols() > 0) { |
| 518 | ObjCProtocolDecl **RefPDecl = getReferencedProtocols(); |
| 519 | |
| 520 | for (unsigned i = 0; i < getNumReferencedProtocols(); i++) { |
| 521 | if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel))) |
| 522 | return MethodDecl; |
| 523 | } |
| 524 | } |
| 525 | return NULL; |
| 526 | } |
| 527 | |
| 528 | // lookupInstanceMethod - Lookup a class method in the protocol and protocols |
| 529 | // it inherited. |
| 530 | ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) { |
| 531 | ObjCMethodDecl *MethodDecl = NULL; |
| 532 | |
| 533 | if ((MethodDecl = getClassMethod(Sel))) |
| 534 | return MethodDecl; |
| 535 | |
| 536 | if (getNumReferencedProtocols() > 0) { |
| 537 | ObjCProtocolDecl **RefPDecl = getReferencedProtocols(); |
| 538 | |
| 539 | for(unsigned i = 0; i < getNumReferencedProtocols(); i++) { |
| 540 | if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel))) |
| 541 | return MethodDecl; |
| 542 | } |
| 543 | } |
| 544 | return NULL; |
| 545 | } |
| 546 | |
| 547 | /// getSynthesizedMethodSize - Compute size of synthesized method name |
| 548 | /// as done be the rewrite. |
| 549 | /// |
| 550 | unsigned ObjCMethodDecl::getSynthesizedMethodSize() const { |
| 551 | // syntesized method name is a concatenation of -/+[class-name selector] |
| 552 | // Get length of this name. |
| 553 | unsigned length = 3; // _I_ or _C_ |
| 554 | length += strlen(getClassInterface()->getName()) +1; // extra for _ |
| 555 | NamedDecl *MethodContext = getMethodContext(); |
| 556 | if (ObjCCategoryImplDecl *CID = |
| 557 | dyn_cast<ObjCCategoryImplDecl>(MethodContext)) |
| 558 | length += strlen(CID->getName()) +1; |
| 559 | length += getSelector().getName().size(); // selector name |
| 560 | return length; |
| 561 | } |
| 562 | |
Chris Lattner | 5619688 | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 563 | ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() { |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 564 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext)) |
| 565 | return ID; |
| 566 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext)) |
| 567 | return CD->getClassInterface(); |
| 568 | if (ObjCImplementationDecl *IMD = |
Chris Lattner | 5619688 | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 569 | dyn_cast<ObjCImplementationDecl>(MethodContext)) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 570 | return IMD->getClassInterface(); |
Chris Lattner | 5619688 | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 571 | if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext)) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 572 | return CID->getClassInterface(); |
| 573 | assert(false && "unknown method context"); |
| 574 | return 0; |
| 575 | } |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 576 | |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 577 | ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C, |
| 578 | SourceLocation atLoc, |
| 579 | SourceLocation L, |
| 580 | ObjCPropertyDecl *property, |
| 581 | PropertyImplKind kind, |
| 582 | ObjCIvarDecl *ivar) { |
| 583 | void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>(); |
| 584 | return new (Mem) ObjCPropertyImplDecl(atLoc, L, property, kind, ivar); |
| 585 | } |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 586 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 587 | |