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