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, |
Steve Naroff | d6a07aa | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 55 | IdentifierInfo *Id, |
| 56 | SourceLocation ClassLoc, |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 57 | bool ForwardDecl, bool isInternal){ |
| 58 | void *Mem = C.getAllocator().Allocate<ObjCInterfaceDecl>(); |
Chris Lattner | b752f28 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 59 | return new (Mem) ObjCInterfaceDecl(atLoc, Id, ClassLoc, ForwardDecl, |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 60 | isInternal); |
| 61 | } |
| 62 | |
Ted Kremenek | 8a77931 | 2008-06-06 16:45:15 +0000 | [diff] [blame] | 63 | ObjCInterfaceDecl::~ObjCInterfaceDecl() { |
| 64 | delete [] Ivars; |
| 65 | delete [] InstanceMethods; |
| 66 | delete [] ClassMethods; |
| 67 | delete [] PropertyDecl; |
| 68 | // FIXME: CategoryList? |
| 69 | } |
| 70 | |
| 71 | void ObjCInterfaceDecl::Destroy(ASTContext& C) { |
| 72 | for (ivar_iterator I=ivar_begin(), E=ivar_end(); I!=E; ++I) |
| 73 | if (*I) (*I)->Destroy(C); |
| 74 | |
| 75 | for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I!=E; ++I) |
| 76 | if (*I) (*I)->Destroy(C); |
| 77 | |
| 78 | for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I!=E; ++I) |
| 79 | if (*I) (*I)->Destroy(C); |
Ted Kremenek | 1a726d7 | 2008-06-06 17:21:42 +0000 | [diff] [blame] | 80 | |
| 81 | // FIXME: Because there is no clear ownership |
| 82 | // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they |
| 83 | // reference, we destroy ObjCPropertyDecls in ~TranslationUnit. |
| 84 | |
Ted Kremenek | 8a77931 | 2008-06-06 16:45:15 +0000 | [diff] [blame] | 85 | Decl::Destroy(C); |
| 86 | } |
| 87 | |
| 88 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 89 | ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L, |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 90 | IdentifierInfo *Id, QualType T, Expr *BW) { |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 91 | void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 92 | return new (Mem) ObjCIvarDecl(L, Id, T, BW); |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 95 | ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, |
| 96 | SourceLocation L, |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 97 | unsigned numRefProtos, |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 98 | IdentifierInfo *Id) { |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 99 | void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>(); |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 100 | return new (Mem) ObjCProtocolDecl(L, numRefProtos, Id); |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Ted Kremenek | 1c8a413 | 2008-06-06 19:48:57 +0000 | [diff] [blame] | 103 | ObjCProtocolDecl::~ObjCProtocolDecl() { |
| 104 | delete [] ReferencedProtocols; |
| 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 | f7b2c98 | 2008-07-21 17:23:15 +0000 | [diff] [blame] | 443 | void ObjCCategoryDecl::addReferencedProtocols(ObjCProtocolDecl **List, |
| 444 | unsigned NumRPs) { |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 445 | assert(NumReferencedProtocols == 0 && "Protocol list already set"); |
| 446 | if (NumRPs == 0) return; |
| 447 | |
| 448 | ReferencedProtocols = new ObjCProtocolDecl*[NumRPs]; |
| 449 | memcpy(ReferencedProtocols, List, NumRPs*sizeof(ObjCProtocolDecl*)); |
| 450 | NumReferencedProtocols = NumRPs; |
| 451 | } |
| 452 | |
| 453 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 454 | /// addMethods - Insert instance and methods declarations into |
| 455 | /// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields. |
| 456 | /// |
| 457 | void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods, |
| 458 | unsigned numInsMembers, |
| 459 | ObjCMethodDecl **clsMethods, |
| 460 | unsigned numClsMembers, |
| 461 | SourceLocation endLoc) { |
| 462 | NumInstanceMethods = numInsMembers; |
| 463 | if (numInsMembers) { |
| 464 | InstanceMethods = new ObjCMethodDecl*[numInsMembers]; |
| 465 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*)); |
| 466 | } |
| 467 | NumClassMethods = numClsMembers; |
| 468 | if (numClsMembers) { |
| 469 | ClassMethods = new ObjCMethodDecl*[numClsMembers]; |
| 470 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*)); |
| 471 | } |
| 472 | AtEndLoc = endLoc; |
| 473 | } |
| 474 | |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 475 | /// FindPropertyDeclaration - Finds declaration of the property given its name |
| 476 | /// in 'PropertyId' and returns it. It returns 0, if not found. |
| 477 | /// |
| 478 | ObjCPropertyDecl * |
| 479 | ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const { |
| 480 | for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(), |
| 481 | E = classprop_end(); I != E; ++I) { |
| 482 | ObjCPropertyDecl *property = *I; |
| 483 | if (property->getIdentifier() == PropertyId) |
| 484 | return property; |
| 485 | } |
| 486 | return 0; |
| 487 | } |
| 488 | |
Steve Naroff | 3d2c22b | 2008-06-05 13:55:23 +0000 | [diff] [blame] | 489 | /// FindPropertyDeclaration - Finds declaration of the property given its name |
| 490 | /// in 'PropertyId' and returns it. It returns 0, if not found. |
| 491 | /// |
| 492 | ObjCPropertyDecl * |
| 493 | ObjCProtocolDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const { |
| 494 | for (ObjCProtocolDecl::classprop_iterator I = classprop_begin(), |
| 495 | E = classprop_end(); I != E; ++I) { |
| 496 | ObjCPropertyDecl *property = *I; |
| 497 | if (property->getIdentifier() == PropertyId) |
| 498 | return property; |
| 499 | } |
| 500 | return 0; |
| 501 | } |
| 502 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 503 | ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable( |
| 504 | IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) { |
| 505 | ObjCInterfaceDecl* ClassDecl = this; |
| 506 | while (ClassDecl != NULL) { |
| 507 | for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end(); |
| 508 | I != E; ++I) { |
| 509 | if ((*I)->getIdentifier() == ID) { |
| 510 | clsDeclared = ClassDecl; |
| 511 | return *I; |
| 512 | } |
| 513 | } |
| 514 | ClassDecl = ClassDecl->getSuperClass(); |
| 515 | } |
| 516 | return NULL; |
| 517 | } |
| 518 | |
| 519 | /// lookupInstanceMethod - This method returns an instance method by looking in |
| 520 | /// the class, its categories, and its super classes (using a linear search). |
| 521 | ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) { |
| 522 | ObjCInterfaceDecl* ClassDecl = this; |
| 523 | ObjCMethodDecl *MethodDecl = 0; |
| 524 | |
| 525 | while (ClassDecl != NULL) { |
| 526 | if ((MethodDecl = ClassDecl->getInstanceMethod(Sel))) |
| 527 | return MethodDecl; |
| 528 | |
| 529 | // Didn't find one yet - look through protocols. |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame^] | 530 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 531 | ClassDecl->getReferencedProtocols(); |
| 532 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 533 | E = Protocols.end(); I != E; ++I) |
| 534 | if ((MethodDecl = (*I)->getInstanceMethod(Sel))) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 535 | return MethodDecl; |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame^] | 536 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 537 | // Didn't find one yet - now look through categories. |
| 538 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 539 | while (CatDecl) { |
| 540 | if ((MethodDecl = CatDecl->getInstanceMethod(Sel))) |
| 541 | return MethodDecl; |
| 542 | CatDecl = CatDecl->getNextClassCategory(); |
| 543 | } |
| 544 | ClassDecl = ClassDecl->getSuperClass(); |
| 545 | } |
| 546 | return NULL; |
| 547 | } |
| 548 | |
| 549 | // lookupClassMethod - This method returns a class method by looking in the |
| 550 | // class, its categories, and its super classes (using a linear search). |
| 551 | ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) { |
| 552 | ObjCInterfaceDecl* ClassDecl = this; |
| 553 | ObjCMethodDecl *MethodDecl = 0; |
| 554 | |
| 555 | while (ClassDecl != NULL) { |
| 556 | if ((MethodDecl = ClassDecl->getClassMethod(Sel))) |
| 557 | return MethodDecl; |
| 558 | |
| 559 | // Didn't find one yet - look through protocols. |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame^] | 560 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 561 | ClassDecl->getReferencedProtocols(); |
| 562 | |
| 563 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 564 | E = Protocols.end(); I != E; ++I) { |
| 565 | if ((MethodDecl = (*I)->getClassMethod(Sel))) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 566 | return MethodDecl; |
| 567 | } |
| 568 | // Didn't find one yet - now look through categories. |
| 569 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 570 | while (CatDecl) { |
| 571 | if ((MethodDecl = CatDecl->getClassMethod(Sel))) |
| 572 | return MethodDecl; |
| 573 | CatDecl = CatDecl->getNextClassCategory(); |
| 574 | } |
| 575 | ClassDecl = ClassDecl->getSuperClass(); |
| 576 | } |
| 577 | return NULL; |
| 578 | } |
| 579 | |
| 580 | /// lookupInstanceMethod - This method returns an instance method by looking in |
| 581 | /// the class implementation. Unlike interfaces, we don't look outside the |
| 582 | /// implementation. |
| 583 | ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) { |
| 584 | for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) |
| 585 | if ((*I)->getSelector() == Sel) |
| 586 | return *I; |
| 587 | return NULL; |
| 588 | } |
| 589 | |
| 590 | /// lookupClassMethod - This method returns a class method by looking in |
| 591 | /// the class implementation. Unlike interfaces, we don't look outside the |
| 592 | /// implementation. |
| 593 | ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) { |
| 594 | for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); |
| 595 | I != E; ++I) |
| 596 | if ((*I)->getSelector() == Sel) |
| 597 | return *I; |
| 598 | return NULL; |
| 599 | } |
| 600 | |
| 601 | // lookupInstanceMethod - This method returns an instance method by looking in |
| 602 | // the class implementation. Unlike interfaces, we don't look outside the |
| 603 | // implementation. |
| 604 | ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) { |
| 605 | for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) |
| 606 | if ((*I)->getSelector() == Sel) |
| 607 | return *I; |
| 608 | return NULL; |
| 609 | } |
| 610 | |
| 611 | // lookupClassMethod - This method returns an instance method by looking in |
| 612 | // the class implementation. Unlike interfaces, we don't look outside the |
| 613 | // implementation. |
| 614 | ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) { |
| 615 | for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); |
| 616 | I != E; ++I) |
| 617 | if ((*I)->getSelector() == Sel) |
| 618 | return *I; |
| 619 | return NULL; |
| 620 | } |
| 621 | |
| 622 | // lookupInstanceMethod - Lookup a instance method in the protocol and protocols |
| 623 | // it inherited. |
| 624 | ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) { |
| 625 | ObjCMethodDecl *MethodDecl = NULL; |
| 626 | |
| 627 | if ((MethodDecl = getInstanceMethod(Sel))) |
| 628 | return MethodDecl; |
| 629 | |
| 630 | if (getNumReferencedProtocols() > 0) { |
| 631 | ObjCProtocolDecl **RefPDecl = getReferencedProtocols(); |
| 632 | |
| 633 | for (unsigned i = 0; i < getNumReferencedProtocols(); i++) { |
| 634 | if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel))) |
| 635 | return MethodDecl; |
| 636 | } |
| 637 | } |
| 638 | return NULL; |
| 639 | } |
| 640 | |
| 641 | // lookupInstanceMethod - Lookup a class method in the protocol and protocols |
| 642 | // it inherited. |
| 643 | ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) { |
| 644 | ObjCMethodDecl *MethodDecl = NULL; |
| 645 | |
| 646 | if ((MethodDecl = getClassMethod(Sel))) |
| 647 | return MethodDecl; |
| 648 | |
| 649 | if (getNumReferencedProtocols() > 0) { |
| 650 | ObjCProtocolDecl **RefPDecl = getReferencedProtocols(); |
| 651 | |
| 652 | for(unsigned i = 0; i < getNumReferencedProtocols(); i++) { |
| 653 | if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel))) |
| 654 | return MethodDecl; |
| 655 | } |
| 656 | } |
| 657 | return NULL; |
| 658 | } |
| 659 | |
| 660 | /// getSynthesizedMethodSize - Compute size of synthesized method name |
| 661 | /// as done be the rewrite. |
| 662 | /// |
| 663 | unsigned ObjCMethodDecl::getSynthesizedMethodSize() const { |
| 664 | // syntesized method name is a concatenation of -/+[class-name selector] |
| 665 | // Get length of this name. |
| 666 | unsigned length = 3; // _I_ or _C_ |
| 667 | length += strlen(getClassInterface()->getName()) +1; // extra for _ |
| 668 | NamedDecl *MethodContext = getMethodContext(); |
| 669 | if (ObjCCategoryImplDecl *CID = |
| 670 | dyn_cast<ObjCCategoryImplDecl>(MethodContext)) |
| 671 | length += strlen(CID->getName()) +1; |
| 672 | length += getSelector().getName().size(); // selector name |
| 673 | return length; |
| 674 | } |
| 675 | |
Chris Lattner | 5619688 | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 676 | ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() { |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 677 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext)) |
| 678 | return ID; |
| 679 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext)) |
| 680 | return CD->getClassInterface(); |
| 681 | if (ObjCImplementationDecl *IMD = |
Chris Lattner | 5619688 | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 682 | dyn_cast<ObjCImplementationDecl>(MethodContext)) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 683 | return IMD->getClassInterface(); |
Chris Lattner | 5619688 | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 684 | if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext)) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 685 | return CID->getClassInterface(); |
| 686 | assert(false && "unknown method context"); |
| 687 | return 0; |
| 688 | } |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 689 | |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 690 | ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C, |
| 691 | SourceLocation atLoc, |
| 692 | SourceLocation L, |
| 693 | ObjCPropertyDecl *property, |
| 694 | PropertyImplKind kind, |
| 695 | ObjCIvarDecl *ivar) { |
| 696 | void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>(); |
| 697 | return new (Mem) ObjCPropertyImplDecl(atLoc, L, property, kind, ivar); |
| 698 | } |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 699 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 700 | |