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