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