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