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, |
Daniel Dunbar | f641492 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 28 | bool isInstance, |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 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, |
Daniel Dunbar | f641492 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 35 | 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; |
Ted Kremenek | 8a77931 | 2008-06-06 16:45:15 +0000 | [diff] [blame] | 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, |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 90 | IdentifierInfo *Id, QualType T, |
| 91 | AccessControl ac, Expr *BW) { |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 92 | void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>(); |
Ted Kremenek | b8db21d | 2008-07-23 18:04:17 +0000 | [diff] [blame] | 93 | return new (Mem) ObjCIvarDecl(L, Id, T, ac, BW); |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Ted Kremenek | 01e6779 | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 96 | |
| 97 | ObjCAtDefsFieldDecl |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 98 | *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Ted Kremenek | 01e6779 | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 99 | IdentifierInfo *Id, QualType T, Expr *BW) { |
| 100 | void *Mem = C.getAllocator().Allocate<ObjCAtDefsFieldDecl>(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 101 | return new (Mem) ObjCAtDefsFieldDecl(DC, L, Id, T, BW); |
Ted Kremenek | 01e6779 | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) { |
| 105 | this->~ObjCAtDefsFieldDecl(); |
| 106 | C.getAllocator().Deallocate((void *)this); |
| 107 | } |
| 108 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 109 | ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, |
| 110 | SourceLocation L, |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 111 | IdentifierInfo *Id) { |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 112 | void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>(); |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 113 | return new (Mem) ObjCProtocolDecl(L, Id); |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Ted Kremenek | 1c8a413 | 2008-06-06 19:48:57 +0000 | [diff] [blame] | 116 | ObjCProtocolDecl::~ObjCProtocolDecl() { |
Ted Kremenek | 1c8a413 | 2008-06-06 19:48:57 +0000 | [diff] [blame] | 117 | delete [] InstanceMethods; |
| 118 | delete [] ClassMethods; |
| 119 | delete [] PropertyDecl; |
| 120 | } |
| 121 | |
| 122 | void ObjCProtocolDecl::Destroy(ASTContext& C) { |
| 123 | |
| 124 | // Referenced Protocols are not owned, so don't Destroy them. |
| 125 | |
| 126 | for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I!=E; ++I) |
| 127 | if (*I) (*I)->Destroy(C); |
| 128 | |
| 129 | for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I!=E; ++I) |
| 130 | if (*I) (*I)->Destroy(C); |
| 131 | |
| 132 | // FIXME: Because there is no clear ownership |
| 133 | // role between ObjCProtocolDecls and the ObjCPropertyDecls that they |
| 134 | // reference, we destroy ObjCPropertyDecls in ~TranslationUnit. |
| 135 | |
| 136 | Decl::Destroy(C); |
| 137 | } |
| 138 | |
| 139 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 140 | ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, |
| 141 | SourceLocation L, |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 142 | ObjCInterfaceDecl **Elts, unsigned nElts) { |
| 143 | void *Mem = C.getAllocator().Allocate<ObjCClassDecl>(); |
| 144 | return new (Mem) ObjCClassDecl(L, Elts, nElts); |
| 145 | } |
| 146 | |
Ted Kremenek | 400d95f | 2008-06-06 20:11:53 +0000 | [diff] [blame] | 147 | ObjCClassDecl::~ObjCClassDecl() { |
| 148 | delete [] ForwardDecls; |
| 149 | } |
| 150 | |
| 151 | void ObjCClassDecl::Destroy(ASTContext& C) { |
| 152 | |
| 153 | // FIXME: There is no clear ownership policy now for referenced |
| 154 | // ObjCInterfaceDecls. Some of them can be forward declarations that |
| 155 | // are never later defined (in which case the ObjCClassDecl owns them) |
| 156 | // or the ObjCInterfaceDecl later becomes a real definition later. Ideally |
| 157 | // we should have separate objects for forward declarations and definitions, |
| 158 | // obviating this problem. Because of this situation, referenced |
| 159 | // ObjCInterfaceDecls are destroyed in ~TranslationUnit. |
| 160 | |
| 161 | Decl::Destroy(C); |
| 162 | } |
| 163 | |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 164 | ObjCForwardProtocolDecl * |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 165 | ObjCForwardProtocolDecl::Create(ASTContext &C, |
| 166 | SourceLocation L, |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 167 | ObjCProtocolDecl **Elts, unsigned NumElts) { |
| 168 | void *Mem = C.getAllocator().Allocate<ObjCForwardProtocolDecl>(); |
| 169 | return new (Mem) ObjCForwardProtocolDecl(L, Elts, NumElts); |
| 170 | } |
| 171 | |
Ted Kremenek | 05ac3ef | 2008-06-06 21:05:33 +0000 | [diff] [blame] | 172 | ObjCForwardProtocolDecl::~ObjCForwardProtocolDecl() { |
| 173 | delete [] ReferencedProtocols; |
| 174 | } |
| 175 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 176 | ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, |
| 177 | SourceLocation L, |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 178 | IdentifierInfo *Id) { |
| 179 | void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>(); |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 180 | return new (Mem) ObjCCategoryDecl(L, Id); |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 183 | ObjCCategoryImplDecl * |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 184 | ObjCCategoryImplDecl::Create(ASTContext &C, |
| 185 | SourceLocation L,IdentifierInfo *Id, |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 186 | ObjCInterfaceDecl *ClassInterface) { |
| 187 | void *Mem = C.getAllocator().Allocate<ObjCCategoryImplDecl>(); |
| 188 | return new (Mem) ObjCCategoryImplDecl(L, Id, ClassInterface); |
| 189 | } |
| 190 | |
| 191 | ObjCImplementationDecl * |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 192 | ObjCImplementationDecl::Create(ASTContext &C, |
| 193 | SourceLocation L, |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 194 | IdentifierInfo *Id, |
| 195 | ObjCInterfaceDecl *ClassInterface, |
| 196 | ObjCInterfaceDecl *SuperDecl) { |
| 197 | void *Mem = C.getAllocator().Allocate<ObjCImplementationDecl>(); |
| 198 | return new (Mem) ObjCImplementationDecl(L, Id, ClassInterface, SuperDecl); |
| 199 | } |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 200 | |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 201 | ObjCCompatibleAliasDecl * |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 202 | ObjCCompatibleAliasDecl::Create(ASTContext &C, |
| 203 | SourceLocation L, |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 204 | IdentifierInfo *Id, |
| 205 | ObjCInterfaceDecl* AliasedClass) { |
| 206 | void *Mem = C.getAllocator().Allocate<ObjCCompatibleAliasDecl>(); |
| 207 | return new (Mem) ObjCCompatibleAliasDecl(L, Id, AliasedClass); |
| 208 | } |
| 209 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 210 | ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, |
Fariborz Jahanian | dae1a1a | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 211 | SourceLocation L, |
Fariborz Jahanian | 1de1e74 | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 212 | IdentifierInfo *Id, |
Fariborz Jahanian | 46b55e5 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 213 | QualType T, |
| 214 | PropertyControl propControl) { |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 215 | void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>(); |
Fariborz Jahanian | 1de1e74 | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 216 | return new (Mem) ObjCPropertyDecl(L, Id, T); |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 219 | //===----------------------------------------------------------------------===// |
| 220 | // Objective-C Decl Implementation |
| 221 | //===----------------------------------------------------------------------===// |
| 222 | |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 223 | void ObjCMethodDecl::createImplicitParams(ASTContext &Context, |
| 224 | const ObjCInterfaceDecl *OID) { |
Daniel Dunbar | 08a356c | 2008-08-26 06:08:30 +0000 | [diff] [blame] | 225 | QualType selfTy; |
| 226 | if (isInstance()) { |
| 227 | // There may be no interface context due to error in declaration |
| 228 | // of the interface (which has been reported). Recover gracefully. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 229 | if (OID) { |
| 230 | selfTy = Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl *>(OID)); |
Daniel Dunbar | 08a356c | 2008-08-26 06:08:30 +0000 | [diff] [blame] | 231 | selfTy = Context.getPointerType(selfTy); |
| 232 | } else { |
| 233 | selfTy = Context.getObjCIdType(); |
| 234 | } |
| 235 | } else // we have a factory method. |
| 236 | selfTy = Context.getObjCClassType(); |
| 237 | |
| 238 | SelfDecl = ImplicitParamDecl::Create(Context, this, |
| 239 | SourceLocation(), |
| 240 | &Context.Idents.get("self"), |
| 241 | selfTy, 0); |
| 242 | |
| 243 | CmdDecl = ImplicitParamDecl::Create(Context, this, |
| 244 | SourceLocation(), |
| 245 | &Context.Idents.get("_cmd"), |
| 246 | Context.getObjCSelType(), 0); |
| 247 | } |
| 248 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 249 | void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo, |
| 250 | unsigned NumParams) { |
| 251 | assert(ParamInfo == 0 && "Already has param info!"); |
| 252 | |
| 253 | // Zero params -> null pointer. |
| 254 | if (NumParams) { |
| 255 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 256 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 257 | NumMethodParams = NumParams; |
| 258 | } |
| 259 | } |
| 260 | |
Fariborz Jahanian | 31afbf0 | 2008-11-25 21:48:26 +0000 | [diff] [blame] | 261 | /// isPropertyReadonly - Return true if property is a readonly, by seaching |
| 262 | /// for the property in the class and in its categories. |
| 263 | /// |
| 264 | bool ObjCInterfaceDecl::isPropertyReadonly(ObjCPropertyDecl *PDecl) const |
| 265 | { |
| 266 | if (PDecl->isReadOnly()) { |
| 267 | // Main class has the property as 'readyonly'. Must search |
| 268 | // through the category list to see if the property's |
| 269 | // attribute has been over-ridden to 'readwrite'. |
| 270 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 271 | Category; Category = Category->getNextClassCategory()) { |
| 272 | PDecl= Category->FindPropertyDeclaration(PDecl->getIdentifier()); |
| 273 | if (PDecl && !PDecl->isReadOnly()) |
| 274 | return false; |
| 275 | } |
| 276 | return true; |
| 277 | } |
| 278 | return false; |
| 279 | } |
| 280 | |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 281 | /// FindPropertyDeclaration - Finds declaration of the property given its name |
| 282 | /// in 'PropertyId' and returns it. It returns 0, if not found. |
| 283 | /// |
| 284 | ObjCPropertyDecl * |
| 285 | ObjCInterfaceDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const { |
| 286 | for (ObjCInterfaceDecl::classprop_iterator I = classprop_begin(), |
| 287 | E = classprop_end(); I != E; ++I) { |
| 288 | ObjCPropertyDecl *property = *I; |
| 289 | if (property->getIdentifier() == PropertyId) |
| 290 | return property; |
| 291 | } |
Steve Naroff | 6c930f2 | 2008-06-04 04:46:04 +0000 | [diff] [blame] | 292 | // Look through categories. |
| 293 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 294 | Category; Category = Category->getNextClassCategory()) { |
| 295 | ObjCPropertyDecl *property = Category->FindPropertyDeclaration(PropertyId); |
| 296 | if (property) |
| 297 | return property; |
| 298 | } |
Steve Naroff | 3d2c22b | 2008-06-05 13:55:23 +0000 | [diff] [blame] | 299 | // Look through protocols. |
| 300 | for (ObjCInterfaceDecl::protocol_iterator I = protocol_begin(), |
| 301 | E = protocol_end(); I != E; ++I) { |
| 302 | ObjCProtocolDecl *Protocol = *I; |
| 303 | ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId); |
| 304 | if (property) |
| 305 | return property; |
| 306 | } |
Fariborz Jahanian | c70bee8 | 2008-04-21 23:57:08 +0000 | [diff] [blame] | 307 | if (getSuperClass()) |
| 308 | return getSuperClass()->FindPropertyDeclaration(PropertyId); |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | /// FindCategoryDeclaration - Finds category declaration in the list of |
| 313 | /// categories for this class and returns it. Name of the category is passed |
| 314 | /// in 'CategoryId'. If category not found, return 0; |
| 315 | /// |
| 316 | ObjCCategoryDecl * |
| 317 | ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const { |
| 318 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 319 | Category; Category = Category->getNextClassCategory()) |
| 320 | if (Category->getIdentifier() == CategoryId) |
| 321 | return Category; |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | /// FindIvarDeclaration - Find an Ivar declaration in this class given its |
| 326 | /// name in 'IvarId'. On failure to find, return 0; |
| 327 | /// |
| 328 | ObjCIvarDecl * |
| 329 | ObjCInterfaceDecl::FindIvarDeclaration(IdentifierInfo *IvarId) const { |
| 330 | for (ObjCInterfaceDecl::ivar_iterator IVI = ivar_begin(), |
| 331 | IVE = ivar_end(); IVI != IVE; ++IVI) { |
| 332 | ObjCIvarDecl* Ivar = (*IVI); |
| 333 | if (Ivar->getIdentifier() == IvarId) |
| 334 | return Ivar; |
| 335 | } |
Fariborz Jahanian | c70bee8 | 2008-04-21 23:57:08 +0000 | [diff] [blame] | 336 | if (getSuperClass()) |
| 337 | return getSuperClass()->FindIvarDeclaration(IvarId); |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 338 | return 0; |
| 339 | } |
| 340 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 341 | /// ObjCAddInstanceVariablesToClass - Inserts instance variables |
| 342 | /// into ObjCInterfaceDecl's fields. |
| 343 | /// |
| 344 | void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars, |
| 345 | unsigned numIvars, |
| 346 | SourceLocation RBrac) { |
| 347 | NumIvars = numIvars; |
| 348 | if (numIvars) { |
| 349 | Ivars = new ObjCIvarDecl*[numIvars]; |
| 350 | memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*)); |
| 351 | } |
| 352 | setLocEnd(RBrac); |
| 353 | } |
| 354 | |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 355 | /// lookupFieldDeclForIvar - looks up a field decl' in the laid out |
| 356 | /// storage which matches this 'ivar'. |
| 357 | /// |
| 358 | FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context, |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 359 | const ObjCIvarDecl *ivar) { |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame^] | 360 | const RecordDecl *RecordForDecl = Context.addRecordToClass(this); |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 361 | assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class"); |
| 362 | DeclarationName Member = ivar->getDeclName(); |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame^] | 363 | DeclContext::lookup_result Lookup = (const_cast< RecordDecl *>(RecordForDecl)) |
| 364 | ->lookup(Context, Member); |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 365 | assert((Lookup.first != Lookup.second) && "field decl not found"); |
| 366 | FieldDecl *MemberDecl = dyn_cast<FieldDecl>(*Lookup.first); |
| 367 | assert(MemberDecl && "field decl not found"); |
| 368 | return MemberDecl; |
| 369 | } |
| 370 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 371 | /// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance |
| 372 | /// Variables (Ivars) relative to what declared in @implementation;s class. |
| 373 | /// Ivars into ObjCImplementationDecl's fields. |
| 374 | /// |
| 375 | void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl( |
| 376 | ObjCIvarDecl **ivars, unsigned numIvars) { |
| 377 | NumIvars = numIvars; |
| 378 | if (numIvars) { |
| 379 | Ivars = new ObjCIvarDecl*[numIvars]; |
| 380 | memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*)); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /// addMethods - Insert instance and methods declarations into |
| 385 | /// ObjCInterfaceDecl's InsMethods and ClsMethods fields. |
| 386 | /// |
| 387 | void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods, |
| 388 | unsigned numInsMembers, |
| 389 | ObjCMethodDecl **clsMethods, |
| 390 | unsigned numClsMembers, |
| 391 | SourceLocation endLoc) { |
| 392 | NumInstanceMethods = numInsMembers; |
| 393 | if (numInsMembers) { |
| 394 | InstanceMethods = new ObjCMethodDecl*[numInsMembers]; |
| 395 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*)); |
| 396 | } |
| 397 | NumClassMethods = numClsMembers; |
| 398 | if (numClsMembers) { |
| 399 | ClassMethods = new ObjCMethodDecl*[numClsMembers]; |
| 400 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*)); |
| 401 | } |
| 402 | AtEndLoc = endLoc; |
| 403 | } |
| 404 | |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 405 | /// addProperties - Insert property declaration AST nodes into |
| 406 | /// ObjCInterfaceDecl's PropertyDecl field. |
Chris Lattner | 55d13b4 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 407 | /// |
| 408 | void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties, |
| 409 | unsigned NumProperties) { |
| 410 | if (NumProperties == 0) return; |
| 411 | |
| 412 | NumPropertyDecl = NumProperties; |
| 413 | PropertyDecl = new ObjCPropertyDecl*[NumProperties]; |
| 414 | memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*)); |
| 415 | } |
| 416 | |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 417 | /// mergeProperties - Adds properties to the end of list of current properties |
| 418 | /// for this class. |
| 419 | |
| 420 | void ObjCInterfaceDecl::mergeProperties(ObjCPropertyDecl **Properties, |
| 421 | unsigned NumNewProperties) { |
| 422 | if (NumNewProperties == 0) return; |
| 423 | |
| 424 | if (PropertyDecl) { |
| 425 | ObjCPropertyDecl **newPropertyDecl = |
| 426 | new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl]; |
| 427 | ObjCPropertyDecl **buf = newPropertyDecl; |
| 428 | // put back original properties in buffer. |
| 429 | memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*)); |
| 430 | // Add new properties to this buffer. |
| 431 | memcpy(buf+NumPropertyDecl, Properties, |
| 432 | NumNewProperties*sizeof(ObjCPropertyDecl*)); |
Nuno Lopes | 6b3502c | 2008-05-10 10:31:54 +0000 | [diff] [blame] | 433 | delete[] PropertyDecl; |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 434 | PropertyDecl = newPropertyDecl; |
| 435 | NumPropertyDecl += NumNewProperties; |
| 436 | } |
| 437 | else { |
Nuno Lopes | 6b3502c | 2008-05-10 10:31:54 +0000 | [diff] [blame] | 438 | addProperties(Properties, NumNewProperties); |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Daniel Dunbar | 7b47322 | 2008-08-27 02:09:39 +0000 | [diff] [blame] | 442 | static void |
| 443 | addPropertyMethods(Decl *D, |
| 444 | ASTContext &Context, |
| 445 | ObjCPropertyDecl *property, |
Fariborz Jahanian | b85cce6 | 2008-12-02 00:19:12 +0000 | [diff] [blame] | 446 | llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods, |
| 447 | llvm::DenseMap<Selector, const ObjCMethodDecl*> &InsMap) { |
Daniel Dunbar | e86d923 | 2008-08-27 05:13:46 +0000 | [diff] [blame] | 448 | ObjCMethodDecl *GetterDecl, *SetterDecl = 0; |
Fariborz Jahanian | b85cce6 | 2008-12-02 00:19:12 +0000 | [diff] [blame] | 449 | |
| 450 | GetterDecl = const_cast<ObjCMethodDecl*>(InsMap[property->getGetterName()]); |
| 451 | if (!property->isReadOnly()) |
| 452 | SetterDecl = const_cast<ObjCMethodDecl*>(InsMap[property->getSetterName()]); |
| 453 | |
Daniel Dunbar | 3216dcd | 2008-08-26 06:53:45 +0000 | [diff] [blame] | 454 | // FIXME: The synthesized property we set here is misleading. We |
| 455 | // almost always synthesize these methods unless the user explicitly |
| 456 | // provided prototypes (which is odd, but allowed). Sema should be |
| 457 | // typechecking that the declarations jive in that situation (which |
| 458 | // it is not currently). |
| 459 | |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 460 | // Find the default getter and if one not found, add one. |
Daniel Dunbar | 4d7da2f | 2008-08-26 02:32:45 +0000 | [diff] [blame] | 461 | if (!GetterDecl) { |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 462 | // No instance method of same name as property getter name was found. |
| 463 | // Declare a getter method and add it to the list of methods |
| 464 | // for this class. |
Daniel Dunbar | 4d7da2f | 2008-08-26 02:32:45 +0000 | [diff] [blame] | 465 | GetterDecl = |
| 466 | ObjCMethodDecl::Create(Context, property->getLocation(), |
| 467 | property->getLocation(), |
| 468 | property->getGetterName(), |
| 469 | property->getType(), |
Daniel Dunbar | 7b47322 | 2008-08-27 02:09:39 +0000 | [diff] [blame] | 470 | D, |
Daniel Dunbar | 4d7da2f | 2008-08-26 02:32:45 +0000 | [diff] [blame] | 471 | true, false, true, ObjCMethodDecl::Required); |
| 472 | insMethods.push_back(GetterDecl); |
Fariborz Jahanian | b85cce6 | 2008-12-02 00:19:12 +0000 | [diff] [blame] | 473 | InsMap[property->getGetterName()] = GetterDecl; |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 474 | } |
Fariborz Jahanian | 91b51a9 | 2008-12-09 22:43:22 +0000 | [diff] [blame] | 475 | else |
| 476 | // A user declared getter will be synthesize when @synthesize of |
| 477 | // the property with the same name is seen in the @implementation |
| 478 | GetterDecl->setIsSynthesized(); |
Daniel Dunbar | 4d7da2f | 2008-08-26 02:32:45 +0000 | [diff] [blame] | 479 | property->setGetterMethodDecl(GetterDecl); |
| 480 | |
Daniel Dunbar | 394d33f | 2008-08-26 07:16:44 +0000 | [diff] [blame] | 481 | // Skip setter if property is read-only. |
| 482 | if (property->isReadOnly()) |
| 483 | return; |
| 484 | |
Daniel Dunbar | 4d7da2f | 2008-08-26 02:32:45 +0000 | [diff] [blame] | 485 | // Find the default setter and if one not found, add one. |
Daniel Dunbar | 4d7da2f | 2008-08-26 02:32:45 +0000 | [diff] [blame] | 486 | if (!SetterDecl) { |
| 487 | // No instance method of same name as property setter name was found. |
| 488 | // Declare a setter method and add it to the list of methods |
| 489 | // for this class. |
| 490 | SetterDecl = |
| 491 | ObjCMethodDecl::Create(Context, property->getLocation(), |
| 492 | property->getLocation(), |
| 493 | property->getSetterName(), |
Daniel Dunbar | 5fa6331 | 2008-08-26 02:53:23 +0000 | [diff] [blame] | 494 | Context.VoidTy, |
Daniel Dunbar | 7b47322 | 2008-08-27 02:09:39 +0000 | [diff] [blame] | 495 | D, |
Daniel Dunbar | 4d7da2f | 2008-08-26 02:32:45 +0000 | [diff] [blame] | 496 | true, false, true, ObjCMethodDecl::Required); |
| 497 | insMethods.push_back(SetterDecl); |
Fariborz Jahanian | b85cce6 | 2008-12-02 00:19:12 +0000 | [diff] [blame] | 498 | InsMap[property->getSetterName()] = SetterDecl; |
Daniel Dunbar | 4d7da2f | 2008-08-26 02:32:45 +0000 | [diff] [blame] | 499 | // Invent the arguments for the setter. We don't bother making a |
| 500 | // nice name for the argument. |
| 501 | ParmVarDecl *Argument = ParmVarDecl::Create(Context, |
| 502 | SetterDecl, |
| 503 | SourceLocation(), |
| 504 | property->getIdentifier(), |
| 505 | property->getType(), |
| 506 | VarDecl::None, |
| 507 | 0, 0); |
| 508 | SetterDecl->setMethodParams(&Argument, 1); |
| 509 | } |
Fariborz Jahanian | 91b51a9 | 2008-12-09 22:43:22 +0000 | [diff] [blame] | 510 | else |
| 511 | // A user declared setter will be synthesize when @synthesize of |
| 512 | // the property with the same name is seen in the @implementation |
| 513 | SetterDecl->setIsSynthesized(); |
Daniel Dunbar | 4d7da2f | 2008-08-26 02:32:45 +0000 | [diff] [blame] | 514 | property->setSetterMethodDecl(SetterDecl); |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Daniel Dunbar | 7b47322 | 2008-08-27 02:09:39 +0000 | [diff] [blame] | 517 | /// addPropertyMethods - Goes through list of properties declared in this class |
| 518 | /// and builds setter/getter method declartions depending on the setter/getter |
| 519 | /// attributes of the property. |
| 520 | /// |
| 521 | void ObjCInterfaceDecl::addPropertyMethods( |
| 522 | ASTContext &Context, |
| 523 | ObjCPropertyDecl *property, |
Fariborz Jahanian | b85cce6 | 2008-12-02 00:19:12 +0000 | [diff] [blame] | 524 | llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods, |
| 525 | llvm::DenseMap<Selector, const ObjCMethodDecl*> &InsMap) { |
| 526 | ::addPropertyMethods(this, Context, property, insMethods, InsMap); |
Daniel Dunbar | 7b47322 | 2008-08-27 02:09:39 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /// addPropertyMethods - Goes through list of properties declared in this class |
| 530 | /// and builds setter/getter method declartions depending on the setter/getter |
| 531 | /// attributes of the property. |
| 532 | /// |
| 533 | void ObjCCategoryDecl::addPropertyMethods( |
| 534 | ASTContext &Context, |
| 535 | ObjCPropertyDecl *property, |
Fariborz Jahanian | b85cce6 | 2008-12-02 00:19:12 +0000 | [diff] [blame] | 536 | llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods, |
| 537 | llvm::DenseMap<Selector, const ObjCMethodDecl*> &InsMap) { |
| 538 | ::addPropertyMethods(this, Context, property, insMethods, InsMap); |
Daniel Dunbar | 7b47322 | 2008-08-27 02:09:39 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Fariborz Jahanian | 1ac2bc4 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 541 | /// mergeProperties - Adds properties to the end of list of current properties |
| 542 | /// for this category. |
| 543 | |
| 544 | void ObjCCategoryDecl::mergeProperties(ObjCPropertyDecl **Properties, |
| 545 | unsigned NumNewProperties) { |
| 546 | if (NumNewProperties == 0) return; |
| 547 | |
| 548 | if (PropertyDecl) { |
| 549 | ObjCPropertyDecl **newPropertyDecl = |
| 550 | new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl]; |
| 551 | ObjCPropertyDecl **buf = newPropertyDecl; |
| 552 | // put back original properties in buffer. |
| 553 | memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*)); |
| 554 | // Add new properties to this buffer. |
| 555 | memcpy(buf+NumPropertyDecl, Properties, |
| 556 | NumNewProperties*sizeof(ObjCPropertyDecl*)); |
| 557 | delete[] PropertyDecl; |
| 558 | PropertyDecl = newPropertyDecl; |
| 559 | NumPropertyDecl += NumNewProperties; |
| 560 | } |
| 561 | else { |
| 562 | addProperties(Properties, NumNewProperties); |
| 563 | } |
| 564 | } |
| 565 | |
Daniel Dunbar | 7b47322 | 2008-08-27 02:09:39 +0000 | [diff] [blame] | 566 | /// addPropertyMethods - Goes through list of properties declared in this class |
| 567 | /// and builds setter/getter method declartions depending on the setter/getter |
| 568 | /// attributes of the property. |
| 569 | /// |
| 570 | void ObjCProtocolDecl::addPropertyMethods( |
| 571 | ASTContext &Context, |
| 572 | ObjCPropertyDecl *property, |
Fariborz Jahanian | b85cce6 | 2008-12-02 00:19:12 +0000 | [diff] [blame] | 573 | llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods, |
| 574 | llvm::DenseMap<Selector, const ObjCMethodDecl*> &InsMap) { |
| 575 | ::addPropertyMethods(this, Context, property, insMethods, InsMap); |
Daniel Dunbar | 7b47322 | 2008-08-27 02:09:39 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 578 | /// addProperties - Insert property declaration AST nodes into |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 579 | /// ObjCProtocolDecl's PropertyDecl field. |
| 580 | /// |
| 581 | void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties, |
| 582 | unsigned NumProperties) { |
| 583 | if (NumProperties == 0) return; |
| 584 | |
| 585 | NumPropertyDecl = NumProperties; |
| 586 | PropertyDecl = new ObjCPropertyDecl*[NumProperties]; |
| 587 | memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*)); |
| 588 | } |
| 589 | |
| 590 | /// addProperties - Insert property declaration AST nodes into |
Fariborz Jahanian | d9a3c33 | 2008-04-16 21:11:25 +0000 | [diff] [blame] | 591 | /// ObjCCategoryDecl's PropertyDecl field. |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 592 | /// |
| 593 | void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties, |
| 594 | unsigned NumProperties) { |
| 595 | if (NumProperties == 0) return; |
| 596 | |
| 597 | NumPropertyDecl = NumProperties; |
| 598 | PropertyDecl = new ObjCPropertyDecl*[NumProperties]; |
| 599 | memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*)); |
| 600 | } |
| 601 | |
Chris Lattner | 55d13b4 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 602 | /// addMethods - Insert instance and methods declarations into |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 603 | /// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields. |
| 604 | /// |
| 605 | void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods, |
| 606 | unsigned numInsMembers, |
| 607 | ObjCMethodDecl **clsMethods, |
| 608 | unsigned numClsMembers, |
| 609 | SourceLocation endLoc) { |
| 610 | NumInstanceMethods = numInsMembers; |
| 611 | if (numInsMembers) { |
| 612 | InstanceMethods = new ObjCMethodDecl*[numInsMembers]; |
| 613 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*)); |
| 614 | } |
| 615 | NumClassMethods = numClsMembers; |
| 616 | if (numClsMembers) { |
| 617 | ClassMethods = new ObjCMethodDecl*[numClsMembers]; |
| 618 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*)); |
| 619 | } |
| 620 | AtEndLoc = endLoc; |
| 621 | } |
| 622 | |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 623 | |
| 624 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 625 | /// addMethods - Insert instance and methods declarations into |
| 626 | /// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields. |
| 627 | /// |
| 628 | void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods, |
| 629 | unsigned numInsMembers, |
| 630 | ObjCMethodDecl **clsMethods, |
| 631 | unsigned numClsMembers, |
| 632 | SourceLocation endLoc) { |
| 633 | NumInstanceMethods = numInsMembers; |
| 634 | if (numInsMembers) { |
| 635 | InstanceMethods = new ObjCMethodDecl*[numInsMembers]; |
| 636 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*)); |
| 637 | } |
| 638 | NumClassMethods = numClsMembers; |
| 639 | if (numClsMembers) { |
| 640 | ClassMethods = new ObjCMethodDecl*[numClsMembers]; |
| 641 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*)); |
| 642 | } |
| 643 | AtEndLoc = endLoc; |
| 644 | } |
| 645 | |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 646 | /// FindPropertyDeclaration - Finds declaration of the property given its name |
| 647 | /// in 'PropertyId' and returns it. It returns 0, if not found. |
| 648 | /// |
| 649 | ObjCPropertyDecl * |
| 650 | ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const { |
| 651 | for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(), |
| 652 | E = classprop_end(); I != E; ++I) { |
| 653 | ObjCPropertyDecl *property = *I; |
| 654 | if (property->getIdentifier() == PropertyId) |
| 655 | return property; |
| 656 | } |
| 657 | return 0; |
| 658 | } |
| 659 | |
Steve Naroff | 3d2c22b | 2008-06-05 13:55:23 +0000 | [diff] [blame] | 660 | /// FindPropertyDeclaration - Finds declaration of the property given its name |
| 661 | /// in 'PropertyId' and returns it. It returns 0, if not found. |
| 662 | /// |
| 663 | ObjCPropertyDecl * |
| 664 | ObjCProtocolDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const { |
| 665 | for (ObjCProtocolDecl::classprop_iterator I = classprop_begin(), |
| 666 | E = classprop_end(); I != E; ++I) { |
| 667 | ObjCPropertyDecl *property = *I; |
| 668 | if (property->getIdentifier() == PropertyId) |
| 669 | return property; |
| 670 | } |
| 671 | return 0; |
| 672 | } |
| 673 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 674 | ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable( |
| 675 | IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) { |
| 676 | ObjCInterfaceDecl* ClassDecl = this; |
| 677 | while (ClassDecl != NULL) { |
| 678 | for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end(); |
| 679 | I != E; ++I) { |
| 680 | if ((*I)->getIdentifier() == ID) { |
| 681 | clsDeclared = ClassDecl; |
| 682 | return *I; |
| 683 | } |
| 684 | } |
| 685 | ClassDecl = ClassDecl->getSuperClass(); |
| 686 | } |
| 687 | return NULL; |
| 688 | } |
| 689 | |
| 690 | /// lookupInstanceMethod - This method returns an instance method by looking in |
| 691 | /// the class, its categories, and its super classes (using a linear search). |
| 692 | ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) { |
| 693 | ObjCInterfaceDecl* ClassDecl = this; |
| 694 | ObjCMethodDecl *MethodDecl = 0; |
| 695 | |
| 696 | while (ClassDecl != NULL) { |
| 697 | if ((MethodDecl = ClassDecl->getInstanceMethod(Sel))) |
| 698 | return MethodDecl; |
| 699 | |
| 700 | // Didn't find one yet - look through protocols. |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 701 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 702 | ClassDecl->getReferencedProtocols(); |
| 703 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 704 | E = Protocols.end(); I != E; ++I) |
| 705 | if ((MethodDecl = (*I)->getInstanceMethod(Sel))) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 706 | return MethodDecl; |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 707 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 708 | // Didn't find one yet - now look through categories. |
| 709 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 710 | while (CatDecl) { |
| 711 | if ((MethodDecl = CatDecl->getInstanceMethod(Sel))) |
| 712 | return MethodDecl; |
Steve Naroff | b79c01e | 2008-12-08 20:57:28 +0000 | [diff] [blame] | 713 | |
| 714 | // Didn't find one yet - look through protocols. |
| 715 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 716 | CatDecl->getReferencedProtocols(); |
| 717 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 718 | E = Protocols.end(); I != E; ++I) |
| 719 | if ((MethodDecl = (*I)->getInstanceMethod(Sel))) |
| 720 | return MethodDecl; |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 721 | CatDecl = CatDecl->getNextClassCategory(); |
| 722 | } |
| 723 | ClassDecl = ClassDecl->getSuperClass(); |
| 724 | } |
| 725 | return NULL; |
| 726 | } |
| 727 | |
| 728 | // lookupClassMethod - This method returns a class method by looking in the |
| 729 | // class, its categories, and its super classes (using a linear search). |
| 730 | ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) { |
| 731 | ObjCInterfaceDecl* ClassDecl = this; |
| 732 | ObjCMethodDecl *MethodDecl = 0; |
| 733 | |
| 734 | while (ClassDecl != NULL) { |
| 735 | if ((MethodDecl = ClassDecl->getClassMethod(Sel))) |
| 736 | return MethodDecl; |
| 737 | |
| 738 | // Didn't find one yet - look through protocols. |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 739 | for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(), |
| 740 | E = ClassDecl->protocol_end(); I != E; ++I) |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 741 | if ((MethodDecl = (*I)->getClassMethod(Sel))) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 742 | return MethodDecl; |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 743 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 744 | // Didn't find one yet - now look through categories. |
| 745 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 746 | while (CatDecl) { |
| 747 | if ((MethodDecl = CatDecl->getClassMethod(Sel))) |
| 748 | return MethodDecl; |
| 749 | CatDecl = CatDecl->getNextClassCategory(); |
| 750 | } |
| 751 | ClassDecl = ClassDecl->getSuperClass(); |
| 752 | } |
| 753 | return NULL; |
| 754 | } |
| 755 | |
Daniel Dunbar | 3216dcd | 2008-08-26 06:53:45 +0000 | [diff] [blame] | 756 | /// getInstanceMethod - This method returns an instance method by |
| 757 | /// looking in the class implementation. Unlike interfaces, we don't |
| 758 | /// look outside the implementation. |
| 759 | ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const { |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 760 | for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) |
| 761 | if ((*I)->getSelector() == Sel) |
| 762 | return *I; |
| 763 | return NULL; |
| 764 | } |
| 765 | |
Daniel Dunbar | 3216dcd | 2008-08-26 06:53:45 +0000 | [diff] [blame] | 766 | /// getClassMethod - This method returns a class method by looking in |
| 767 | /// the class implementation. Unlike interfaces, we don't look outside |
| 768 | /// the implementation. |
| 769 | ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const { |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 770 | for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); |
| 771 | I != E; ++I) |
| 772 | if ((*I)->getSelector() == Sel) |
| 773 | return *I; |
| 774 | return NULL; |
| 775 | } |
| 776 | |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 777 | /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl |
| 778 | /// added to the list of those properties @synthesized/@dynamic in this |
| 779 | /// @implementation block. |
| 780 | /// |
| 781 | ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplDecl(IdentifierInfo *Id) const { |
| 782 | for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) { |
| 783 | ObjCPropertyImplDecl *PID = *i; |
| 784 | if (PID->getPropertyDecl()->getIdentifier() == Id) |
| 785 | return PID; |
| 786 | } |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of |
Fariborz Jahanian | 9482a4f | 2008-12-05 22:36:19 +0000 | [diff] [blame] | 791 | /// properties implemented in this @implementation block and returns the |
| 792 | /// implemented property that uses it. |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 793 | /// |
| 794 | ObjCPropertyImplDecl *ObjCImplementationDecl::FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const { |
| 795 | for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) { |
| 796 | ObjCPropertyImplDecl *PID = *i; |
| 797 | if (PID->getPropertyIvarDecl() && |
| 798 | PID->getPropertyIvarDecl()->getIdentifier() == ivarId) |
| 799 | return PID; |
| 800 | } |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of |
Fariborz Jahanian | 9482a4f | 2008-12-05 22:36:19 +0000 | [diff] [blame] | 805 | /// properties implemented in this category @implementation block and returns the |
| 806 | /// implemented property that uses it. |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 807 | /// |
| 808 | ObjCPropertyImplDecl *ObjCCategoryImplDecl::FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const { |
| 809 | for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) { |
| 810 | ObjCPropertyImplDecl *PID = *i; |
| 811 | if (PID->getPropertyIvarDecl() && |
| 812 | PID->getPropertyIvarDecl()->getIdentifier() == ivarId) |
| 813 | return PID; |
| 814 | } |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl |
| 819 | /// added to the list of those properties @synthesized/@dynamic in this |
| 820 | /// category @implementation block. |
| 821 | /// |
| 822 | ObjCPropertyImplDecl *ObjCCategoryImplDecl::FindPropertyImplDecl(IdentifierInfo *Id) const { |
| 823 | for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i) { |
| 824 | ObjCPropertyImplDecl *PID = *i; |
| 825 | if (PID->getPropertyDecl()->getIdentifier() == Id) |
| 826 | return PID; |
| 827 | } |
| 828 | return 0; |
| 829 | } |
| 830 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 831 | // lookupInstanceMethod - This method returns an instance method by looking in |
| 832 | // the class implementation. Unlike interfaces, we don't look outside the |
| 833 | // implementation. |
Daniel Dunbar | 3216dcd | 2008-08-26 06:53:45 +0000 | [diff] [blame] | 834 | ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const { |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 835 | for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) |
| 836 | if ((*I)->getSelector() == Sel) |
| 837 | return *I; |
| 838 | return NULL; |
| 839 | } |
| 840 | |
| 841 | // lookupClassMethod - This method returns an instance method by looking in |
| 842 | // the class implementation. Unlike interfaces, we don't look outside the |
| 843 | // implementation. |
Daniel Dunbar | 3216dcd | 2008-08-26 06:53:45 +0000 | [diff] [blame] | 844 | ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const { |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 845 | for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); |
| 846 | I != E; ++I) |
| 847 | if ((*I)->getSelector() == Sel) |
| 848 | return *I; |
| 849 | return NULL; |
| 850 | } |
| 851 | |
| 852 | // lookupInstanceMethod - Lookup a instance method in the protocol and protocols |
| 853 | // it inherited. |
| 854 | ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) { |
| 855 | ObjCMethodDecl *MethodDecl = NULL; |
| 856 | |
| 857 | if ((MethodDecl = getInstanceMethod(Sel))) |
| 858 | return MethodDecl; |
| 859 | |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 860 | for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I) |
| 861 | if ((MethodDecl = (*I)->getInstanceMethod(Sel))) |
| 862 | return MethodDecl; |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 863 | return NULL; |
| 864 | } |
| 865 | |
| 866 | // lookupInstanceMethod - Lookup a class method in the protocol and protocols |
| 867 | // it inherited. |
| 868 | ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) { |
| 869 | ObjCMethodDecl *MethodDecl = NULL; |
| 870 | |
| 871 | if ((MethodDecl = getClassMethod(Sel))) |
| 872 | return MethodDecl; |
| 873 | |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 874 | for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I) |
| 875 | if ((MethodDecl = (*I)->getClassMethod(Sel))) |
| 876 | return MethodDecl; |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 877 | return NULL; |
| 878 | } |
| 879 | |
| 880 | /// getSynthesizedMethodSize - Compute size of synthesized method name |
| 881 | /// as done be the rewrite. |
| 882 | /// |
| 883 | unsigned ObjCMethodDecl::getSynthesizedMethodSize() const { |
| 884 | // syntesized method name is a concatenation of -/+[class-name selector] |
| 885 | // Get length of this name. |
| 886 | unsigned length = 3; // _I_ or _C_ |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 887 | length += getClassInterface()->getNameAsString().size()+1; // extra for _ |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 888 | NamedDecl *MethodContext = getMethodContext(); |
| 889 | if (ObjCCategoryImplDecl *CID = |
| 890 | dyn_cast<ObjCCategoryImplDecl>(MethodContext)) |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 891 | length += CID->getNameAsString().size()+1; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 892 | length += getSelector().getAsString().size(); // selector name |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 893 | return length; |
| 894 | } |
| 895 | |
Chris Lattner | 5619688 | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 896 | ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() { |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 897 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext)) |
| 898 | return ID; |
| 899 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext)) |
| 900 | return CD->getClassInterface(); |
| 901 | if (ObjCImplementationDecl *IMD = |
Chris Lattner | 5619688 | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 902 | dyn_cast<ObjCImplementationDecl>(MethodContext)) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 903 | return IMD->getClassInterface(); |
Chris Lattner | 5619688 | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 904 | if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext)) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 905 | return CID->getClassInterface(); |
| 906 | assert(false && "unknown method context"); |
| 907 | return 0; |
| 908 | } |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 909 | |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 910 | ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C, |
| 911 | SourceLocation atLoc, |
| 912 | SourceLocation L, |
| 913 | ObjCPropertyDecl *property, |
Daniel Dunbar | 9f0afd4 | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 914 | Kind PK, |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 915 | ObjCIvarDecl *ivar) { |
| 916 | void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>(); |
Daniel Dunbar | 9f0afd4 | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 917 | return new (Mem) ObjCPropertyImplDecl(atLoc, L, property, PK, ivar); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 918 | } |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 919 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 920 | |