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