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