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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 20 | // ObjCInterfaceDecl |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 23 | // Get the local instance method declared in this interface. |
| 24 | // FIXME: handle overloading, instance & class methods can have the same name. |
| 25 | ObjCMethodDecl *ObjCContainerDecl::getInstanceMethod(Selector Sel) const { |
| 26 | lookup_const_result MethodResult = lookup(Sel); |
| 27 | if (MethodResult.first) |
| 28 | return const_cast<ObjCMethodDecl*>( |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 29 | dyn_cast<ObjCMethodDecl>(*MethodResult.first)); |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | // Get the local class method declared in this interface. |
| 34 | ObjCMethodDecl *ObjCContainerDecl::getClassMethod(Selector Sel) const { |
| 35 | lookup_const_result MethodResult = lookup(Sel); |
| 36 | if (MethodResult.first) |
| 37 | return const_cast<ObjCMethodDecl*>( |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 38 | dyn_cast<ObjCMethodDecl>(*MethodResult.first)); |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 39 | return 0; |
| 40 | } |
| 41 | |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 42 | /// FindPropertyDeclaration - Finds declaration of the property given its name |
| 43 | /// in 'PropertyId' and returns it. It returns 0, if not found. |
Steve Naroff | dcf1e84 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 44 | /// FIXME: Convert to DeclContext lookup... |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 45 | /// |
| 46 | ObjCPropertyDecl * |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 47 | ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const { |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 48 | for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I) |
| 49 | if ((*I)->getIdentifier() == PropertyId) |
| 50 | return *I; |
| 51 | |
Fariborz Jahanian | faca5e2 | 2009-01-09 21:04:52 +0000 | [diff] [blame] | 52 | const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this); |
| 53 | if (PID) { |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 54 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 55 | E = PID->protocol_end(); I != E; ++I) |
| 56 | if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) |
| 57 | return P; |
Fariborz Jahanian | faca5e2 | 2009-01-09 21:04:52 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Fariborz Jahanian | 6b0ed6f | 2009-01-19 18:16:19 +0000 | [diff] [blame] | 60 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) { |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 61 | // Look through categories. |
| 62 | for (ObjCCategoryDecl *Category = OID->getCategoryList(); |
| 63 | Category; Category = Category->getNextClassCategory()) { |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 64 | if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId)) |
| 65 | return P; |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 66 | } |
| 67 | // Look through protocols. |
| 68 | for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(), |
| 69 | E = OID->protocol_end(); I != E; ++I) { |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 70 | if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) |
| 71 | return P; |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 72 | } |
| 73 | if (OID->getSuperClass()) |
| 74 | return OID->getSuperClass()->FindPropertyDeclaration(PropertyId); |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 75 | } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) { |
Fariborz Jahanian | 6b0ed6f | 2009-01-19 18:16:19 +0000 | [diff] [blame] | 76 | // Look through protocols. |
| 77 | for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(), |
| 78 | E = OCD->protocol_end(); I != E; ++I) { |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 79 | if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) |
| 80 | return P; |
Fariborz Jahanian | 6b0ed6f | 2009-01-19 18:16:19 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
Steve Naroff | d1f0eb4 | 2008-06-05 13:55:23 +0000 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 86 | ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable( |
| 87 | IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) { |
| 88 | ObjCInterfaceDecl* ClassDecl = this; |
| 89 | while (ClassDecl != NULL) { |
| 90 | for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end(); |
| 91 | I != E; ++I) { |
| 92 | if ((*I)->getIdentifier() == ID) { |
| 93 | clsDeclared = ClassDecl; |
| 94 | return *I; |
| 95 | } |
| 96 | } |
| 97 | ClassDecl = ClassDecl->getSuperClass(); |
| 98 | } |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | /// lookupInstanceMethod - This method returns an instance method by looking in |
| 103 | /// the class, its categories, and its super classes (using a linear search). |
| 104 | ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) { |
| 105 | ObjCInterfaceDecl* ClassDecl = this; |
| 106 | ObjCMethodDecl *MethodDecl = 0; |
| 107 | |
| 108 | while (ClassDecl != NULL) { |
| 109 | if ((MethodDecl = ClassDecl->getInstanceMethod(Sel))) |
| 110 | return MethodDecl; |
| 111 | |
| 112 | // Didn't find one yet - look through protocols. |
Chris Lattner | 8bcb525 | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 113 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 114 | ClassDecl->getReferencedProtocols(); |
| 115 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 116 | E = Protocols.end(); I != E; ++I) |
| 117 | if ((MethodDecl = (*I)->getInstanceMethod(Sel))) |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 118 | return MethodDecl; |
Chris Lattner | 8bcb525 | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 120 | // Didn't find one yet - now look through categories. |
| 121 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 122 | while (CatDecl) { |
| 123 | if ((MethodDecl = CatDecl->getInstanceMethod(Sel))) |
| 124 | return MethodDecl; |
Steve Naroff | af15180 | 2008-12-08 20:57:28 +0000 | [diff] [blame] | 125 | |
| 126 | // Didn't find one yet - look through protocols. |
| 127 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 128 | CatDecl->getReferencedProtocols(); |
| 129 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 130 | E = Protocols.end(); I != E; ++I) |
| 131 | if ((MethodDecl = (*I)->getInstanceMethod(Sel))) |
| 132 | return MethodDecl; |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 133 | CatDecl = CatDecl->getNextClassCategory(); |
| 134 | } |
| 135 | ClassDecl = ClassDecl->getSuperClass(); |
| 136 | } |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | // lookupClassMethod - This method returns a class method by looking in the |
| 141 | // class, its categories, and its super classes (using a linear search). |
| 142 | ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) { |
| 143 | ObjCInterfaceDecl* ClassDecl = this; |
| 144 | ObjCMethodDecl *MethodDecl = 0; |
| 145 | |
| 146 | while (ClassDecl != NULL) { |
| 147 | if ((MethodDecl = ClassDecl->getClassMethod(Sel))) |
| 148 | return MethodDecl; |
| 149 | |
| 150 | // Didn't find one yet - look through protocols. |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 151 | for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(), |
| 152 | E = ClassDecl->protocol_end(); I != E; ++I) |
Chris Lattner | 8bcb525 | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 153 | if ((MethodDecl = (*I)->getClassMethod(Sel))) |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 154 | return MethodDecl; |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 156 | // Didn't find one yet - now look through categories. |
| 157 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 158 | while (CatDecl) { |
| 159 | if ((MethodDecl = CatDecl->getClassMethod(Sel))) |
| 160 | return MethodDecl; |
| 161 | CatDecl = CatDecl->getNextClassCategory(); |
| 162 | } |
| 163 | ClassDecl = ClassDecl->getSuperClass(); |
| 164 | } |
| 165 | return NULL; |
| 166 | } |
| 167 | |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 168 | |
| 169 | |
| 170 | //===----------------------------------------------------------------------===// |
| 171 | // ObjCMethodDecl |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | |
| 174 | ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C, |
| 175 | SourceLocation beginLoc, |
| 176 | SourceLocation endLoc, |
| 177 | Selector SelInfo, QualType T, |
| 178 | DeclContext *contextDecl, |
| 179 | bool isInstance, |
| 180 | bool isVariadic, |
| 181 | bool isSynthesized, |
| 182 | ImplementationControl impControl) { |
| 183 | return new (C) ObjCMethodDecl(beginLoc, endLoc, |
| 184 | SelInfo, T, contextDecl, |
| 185 | isInstance, |
| 186 | isVariadic, isSynthesized, impControl); |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 189 | void ObjCMethodDecl::Destroy(ASTContext& C) { |
| 190 | if (Body) Body->Destroy(C); |
| 191 | if (SelfDecl) SelfDecl->Destroy(C); |
| 192 | |
| 193 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 194 | if (*I) (*I)->Destroy(C); |
| 195 | |
| 196 | ParamInfo.clear(); |
| 197 | |
| 198 | Decl::Destroy(C); |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 201 | void ObjCMethodDecl::createImplicitParams(ASTContext &Context, |
| 202 | const ObjCInterfaceDecl *OID) { |
| 203 | QualType selfTy; |
| 204 | if (isInstanceMethod()) { |
| 205 | // There may be no interface context due to error in declaration |
| 206 | // of the interface (which has been reported). Recover gracefully. |
| 207 | if (OID) { |
| 208 | selfTy =Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(OID)); |
| 209 | selfTy = Context.getPointerType(selfTy); |
| 210 | } else { |
| 211 | selfTy = Context.getObjCIdType(); |
| 212 | } |
| 213 | } else // we have a factory method. |
| 214 | selfTy = Context.getObjCClassType(); |
| 215 | |
| 216 | SelfDecl = ImplicitParamDecl::Create(Context, this, SourceLocation(), |
| 217 | &Context.Idents.get("self"), selfTy); |
| 218 | |
| 219 | CmdDecl = ImplicitParamDecl::Create(Context, this, SourceLocation(), |
| 220 | &Context.Idents.get("_cmd"), |
| 221 | Context.getObjCSelType()); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | /// getSynthesizedMethodSize - Compute size of synthesized method name |
| 227 | /// as done be the rewrite. |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 228 | /// |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 229 | unsigned ObjCMethodDecl::getSynthesizedMethodSize() const { |
| 230 | // syntesized method name is a concatenation of -/+[class-name selector] |
| 231 | // Get length of this name. |
| 232 | unsigned length = 3; // _I_ or _C_ |
| 233 | length += getClassInterface()->getNameAsString().size()+1; // extra for _ |
| 234 | if (const ObjCCategoryImplDecl *CID = |
| 235 | dyn_cast<ObjCCategoryImplDecl>(getDeclContext())) |
| 236 | length += CID->getNameAsString().size()+1; |
| 237 | length += getSelector().getAsString().size(); // selector name |
| 238 | return length; |
| 239 | } |
| 240 | |
| 241 | ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() { |
| 242 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext())) |
| 243 | return ID; |
| 244 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext())) |
| 245 | return CD->getClassInterface(); |
| 246 | if (ObjCImplementationDecl *IMD = |
| 247 | dyn_cast<ObjCImplementationDecl>(getDeclContext())) |
| 248 | return IMD->getClassInterface(); |
| 249 | if (ObjCCategoryImplDecl *CID = |
| 250 | dyn_cast<ObjCCategoryImplDecl>(getDeclContext())) |
| 251 | return CID->getClassInterface(); |
| 252 | assert(false && "unknown method context"); |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 256 | //===----------------------------------------------------------------------===// |
| 257 | // ObjCInterfaceDecl |
| 258 | //===----------------------------------------------------------------------===// |
| 259 | |
| 260 | ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C, |
| 261 | DeclContext *DC, |
| 262 | SourceLocation atLoc, |
| 263 | IdentifierInfo *Id, |
| 264 | SourceLocation ClassLoc, |
| 265 | bool ForwardDecl, bool isInternal){ |
| 266 | return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl, |
| 267 | isInternal); |
| 268 | } |
| 269 | |
| 270 | ObjCInterfaceDecl:: |
| 271 | ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id, |
| 272 | SourceLocation CLoc, bool FD, bool isInternal) |
| 273 | : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id), |
| 274 | TypeForDecl(0), SuperClass(0), |
| 275 | CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal), |
| 276 | ClassLoc(CLoc) { |
| 277 | } |
| 278 | |
| 279 | void ObjCInterfaceDecl::Destroy(ASTContext &C) { |
| 280 | for (ivar_iterator I=ivar_begin(), E=ivar_end(); I!=E; ++I) |
| 281 | if (*I) (*I)->Destroy(C); |
| 282 | |
| 283 | IVars.clear(); |
| 284 | // FIXME: CategoryList? |
| 285 | |
| 286 | // FIXME: Because there is no clear ownership |
| 287 | // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they |
| 288 | // reference, we destroy ObjCPropertyDecls in ~TranslationUnit. |
| 289 | Decl::Destroy(C); |
| 290 | } |
| 291 | |
| 292 | |
| 293 | /// FindCategoryDeclaration - Finds category declaration in the list of |
| 294 | /// categories for this class and returns it. Name of the category is passed |
| 295 | /// in 'CategoryId'. If category not found, return 0; |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 296 | /// |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 297 | ObjCCategoryDecl * |
| 298 | ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const { |
| 299 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 300 | Category; Category = Category->getNextClassCategory()) |
| 301 | if (Category->getIdentifier() == CategoryId) |
| 302 | return Category; |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 303 | return 0; |
| 304 | } |
| 305 | |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 306 | /// lookupFieldDeclForIvar - looks up a field decl' in the laid out |
| 307 | /// storage which matches this 'ivar'. |
| 308 | /// |
| 309 | FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context, |
| 310 | const ObjCIvarDecl *IVar) { |
| 311 | const RecordDecl *RecordForDecl = Context.addRecordToClass(this); |
| 312 | assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class"); |
| 313 | DeclarationName Member = IVar->getDeclName(); |
| 314 | DeclContext::lookup_result Lookup = |
| 315 | (const_cast< RecordDecl *>(RecordForDecl))->lookup(Member); |
| 316 | assert((Lookup.first != Lookup.second) && "field decl not found"); |
| 317 | FieldDecl *MemberDecl = dyn_cast<FieldDecl>(*Lookup.first); |
| 318 | assert(MemberDecl && "field decl not found"); |
| 319 | return MemberDecl; |
| 320 | } |
| 321 | |
| 322 | //===----------------------------------------------------------------------===// |
| 323 | // ObjCIvarDecl |
| 324 | //===----------------------------------------------------------------------===// |
| 325 | |
| 326 | ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC, |
| 327 | SourceLocation L, IdentifierInfo *Id, |
| 328 | QualType T, AccessControl ac, Expr *BW) { |
| 329 | return new (C) ObjCIvarDecl(DC, L, Id, T, ac, BW); |
| 330 | } |
| 331 | |
| 332 | |
| 333 | |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | // ObjCAtDefsFieldDecl |
| 336 | //===----------------------------------------------------------------------===// |
| 337 | |
| 338 | ObjCAtDefsFieldDecl |
| 339 | *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 340 | IdentifierInfo *Id, QualType T, Expr *BW) { |
| 341 | return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW); |
| 342 | } |
| 343 | |
| 344 | void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) { |
| 345 | this->~ObjCAtDefsFieldDecl(); |
| 346 | C.Deallocate((void *)this); |
| 347 | } |
| 348 | |
| 349 | //===----------------------------------------------------------------------===// |
| 350 | // ObjCProtocolDecl |
| 351 | //===----------------------------------------------------------------------===// |
| 352 | |
| 353 | ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC, |
| 354 | SourceLocation L, |
| 355 | IdentifierInfo *Id) { |
| 356 | return new (C) ObjCProtocolDecl(DC, L, Id); |
| 357 | } |
| 358 | |
| 359 | void ObjCProtocolDecl::Destroy(ASTContext &C) { |
| 360 | ReferencedProtocols.clear(); |
| 361 | ObjCContainerDecl::Destroy(C); |
| 362 | } |
| 363 | |
| 364 | // lookupInstanceMethod - Lookup a instance method in the protocol and protocols |
| 365 | // it inherited. |
| 366 | ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) { |
| 367 | ObjCMethodDecl *MethodDecl = NULL; |
| 368 | |
| 369 | if ((MethodDecl = getInstanceMethod(Sel))) |
| 370 | return MethodDecl; |
| 371 | |
| 372 | for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I) |
| 373 | if ((MethodDecl = (*I)->lookupInstanceMethod(Sel))) |
| 374 | return MethodDecl; |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | // lookupInstanceMethod - Lookup a class method in the protocol and protocols |
| 379 | // it inherited. |
| 380 | ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) { |
| 381 | ObjCMethodDecl *MethodDecl = NULL; |
| 382 | |
| 383 | if ((MethodDecl = getClassMethod(Sel))) |
| 384 | return MethodDecl; |
| 385 | |
| 386 | for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I) |
| 387 | if ((MethodDecl = (*I)->lookupClassMethod(Sel))) |
| 388 | return MethodDecl; |
| 389 | return NULL; |
| 390 | } |
| 391 | |
| 392 | //===----------------------------------------------------------------------===// |
| 393 | // ObjCClassDecl |
| 394 | //===----------------------------------------------------------------------===// |
| 395 | |
| 396 | ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC, |
| 397 | SourceLocation L, |
| 398 | ObjCInterfaceDecl *const *Elts, |
| 399 | unsigned nElts) { |
| 400 | return new (C) ObjCClassDecl(DC, L, Elts, nElts); |
| 401 | } |
| 402 | |
| 403 | void ObjCClassDecl::Destroy(ASTContext &C) { |
| 404 | |
| 405 | // FIXME: There is no clear ownership policy now for referenced |
| 406 | // ObjCInterfaceDecls. Some of them can be forward declarations that |
| 407 | // are never later defined (in which case the ObjCClassDecl owns them) |
| 408 | // or the ObjCInterfaceDecl later becomes a real definition later. Ideally |
| 409 | // we should have separate objects for forward declarations and definitions, |
| 410 | // obviating this problem. Because of this situation, referenced |
| 411 | // ObjCInterfaceDecls are destroyed in ~TranslationUnit. |
| 412 | |
| 413 | ForwardDecls.clear(); |
| 414 | Decl::Destroy(C); |
| 415 | } |
| 416 | |
| 417 | //===----------------------------------------------------------------------===// |
| 418 | // ObjCForwardProtocolDecl |
| 419 | //===----------------------------------------------------------------------===// |
| 420 | |
| 421 | ObjCForwardProtocolDecl * |
| 422 | ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC, |
| 423 | SourceLocation L, |
| 424 | ObjCProtocolDecl *const *Elts, |
| 425 | unsigned NumElts) { |
| 426 | return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts); |
| 427 | } |
| 428 | |
| 429 | ObjCForwardProtocolDecl:: |
| 430 | ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L, |
| 431 | ObjCProtocolDecl *const *Elts, unsigned nElts) |
| 432 | : Decl(ObjCForwardProtocol, DC, L) { |
| 433 | ReferencedProtocols.set(Elts, nElts); |
| 434 | } |
| 435 | |
| 436 | void ObjCForwardProtocolDecl::Destroy(ASTContext &C) { |
| 437 | ReferencedProtocols.clear(); |
| 438 | Decl::Destroy(C); |
| 439 | } |
| 440 | |
| 441 | //===----------------------------------------------------------------------===// |
| 442 | // ObjCCategoryDecl |
| 443 | //===----------------------------------------------------------------------===// |
| 444 | |
| 445 | ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC, |
| 446 | SourceLocation L, |
| 447 | IdentifierInfo *Id) { |
| 448 | return new (C) ObjCCategoryDecl(DC, L, Id); |
| 449 | } |
| 450 | |
| 451 | //===----------------------------------------------------------------------===// |
| 452 | // ObjCCategoryImplDecl |
| 453 | //===----------------------------------------------------------------------===// |
| 454 | |
| 455 | ObjCCategoryImplDecl * |
| 456 | ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC, |
| 457 | SourceLocation L,IdentifierInfo *Id, |
| 458 | ObjCInterfaceDecl *ClassInterface) { |
| 459 | return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface); |
| 460 | } |
| 461 | |
| 462 | |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 463 | /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of |
Chris Lattner | 715e848 | 2009-02-16 19:24:31 +0000 | [diff] [blame] | 464 | /// properties implemented in this category @implementation block and returns |
| 465 | /// the implemented property that uses it. |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 466 | /// |
Chris Lattner | 715e848 | 2009-02-16 19:24:31 +0000 | [diff] [blame] | 467 | ObjCPropertyImplDecl *ObjCCategoryImplDecl:: |
| 468 | FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const { |
| 469 | for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 470 | ObjCPropertyImplDecl *PID = *i; |
| 471 | if (PID->getPropertyIvarDecl() && |
| 472 | PID->getPropertyIvarDecl()->getIdentifier() == ivarId) |
| 473 | return PID; |
| 474 | } |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl |
| 479 | /// added to the list of those properties @synthesized/@dynamic in this |
| 480 | /// category @implementation block. |
| 481 | /// |
Chris Lattner | 715e848 | 2009-02-16 19:24:31 +0000 | [diff] [blame] | 482 | ObjCPropertyImplDecl *ObjCCategoryImplDecl:: |
| 483 | FindPropertyImplDecl(IdentifierInfo *Id) const { |
| 484 | for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 485 | ObjCPropertyImplDecl *PID = *i; |
| 486 | if (PID->getPropertyDecl()->getIdentifier() == Id) |
| 487 | return PID; |
| 488 | } |
| 489 | return 0; |
| 490 | } |
| 491 | |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 492 | // lookupInstanceMethod - This method returns an instance method by looking in |
| 493 | // the class implementation. Unlike interfaces, we don't look outside the |
| 494 | // implementation. |
Daniel Dunbar | dd24b9a | 2008-08-26 06:53:45 +0000 | [diff] [blame] | 495 | ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const { |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 496 | for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) |
| 497 | if ((*I)->getSelector() == Sel) |
| 498 | return *I; |
| 499 | return NULL; |
| 500 | } |
| 501 | |
| 502 | // lookupClassMethod - This method returns an instance method by looking in |
| 503 | // the class implementation. Unlike interfaces, we don't look outside the |
| 504 | // implementation. |
Daniel Dunbar | dd24b9a | 2008-08-26 06:53:45 +0000 | [diff] [blame] | 505 | ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const { |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 506 | for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); |
| 507 | I != E; ++I) |
| 508 | if ((*I)->getSelector() == Sel) |
| 509 | return *I; |
| 510 | return NULL; |
| 511 | } |
| 512 | |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 513 | //===----------------------------------------------------------------------===// |
| 514 | // ObjCImplementationDecl |
| 515 | //===----------------------------------------------------------------------===// |
| 516 | |
| 517 | ObjCImplementationDecl * |
| 518 | ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC, |
| 519 | SourceLocation L, |
| 520 | ObjCInterfaceDecl *ClassInterface, |
| 521 | ObjCInterfaceDecl *SuperDecl) { |
| 522 | return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl); |
| 523 | } |
| 524 | |
| 525 | /// Destroy - Call destructors and release memory. |
| 526 | void ObjCImplementationDecl::Destroy(ASTContext& C) { |
| 527 | IVars.clear(); |
| 528 | Decl::Destroy(C); |
| 529 | } |
| 530 | |
| 531 | /// getInstanceMethod - This method returns an instance method by |
| 532 | /// looking in the class implementation. Unlike interfaces, we don't |
| 533 | /// look outside the implementation. |
| 534 | ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const { |
| 535 | for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) |
| 536 | if ((*I)->getSelector() == Sel) |
| 537 | return *I; |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 538 | return NULL; |
| 539 | } |
| 540 | |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 541 | /// getClassMethod - This method returns a class method by looking in |
| 542 | /// the class implementation. Unlike interfaces, we don't look outside |
| 543 | /// the implementation. |
| 544 | ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const { |
| 545 | for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); |
| 546 | I != E; ++I) |
| 547 | if ((*I)->getSelector() == Sel) |
| 548 | return *I; |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 549 | return NULL; |
| 550 | } |
| 551 | |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 552 | /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl |
| 553 | /// added to the list of those properties @synthesized/@dynamic in this |
| 554 | /// @implementation block. |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 555 | /// |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 556 | ObjCPropertyImplDecl *ObjCImplementationDecl:: |
| 557 | FindPropertyImplDecl(IdentifierInfo *Id) const { |
| 558 | for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ |
| 559 | ObjCPropertyImplDecl *PID = *i; |
| 560 | if (PID->getPropertyDecl()->getIdentifier() == Id) |
| 561 | return PID; |
| 562 | } |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 563 | return 0; |
| 564 | } |
Chris Lattner | 4485961 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 565 | |
Chris Lattner | 13f6360 | 2009-02-20 20:59:54 +0000 | [diff] [blame^] | 566 | /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of |
| 567 | /// properties implemented in this @implementation block and returns the |
| 568 | /// implemented property that uses it. |
| 569 | /// |
| 570 | ObjCPropertyImplDecl *ObjCImplementationDecl:: |
| 571 | FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const { |
| 572 | for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ |
| 573 | ObjCPropertyImplDecl *PID = *i; |
| 574 | if (PID->getPropertyIvarDecl() && |
| 575 | PID->getPropertyIvarDecl()->getIdentifier() == ivarId) |
| 576 | return PID; |
| 577 | } |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | //===----------------------------------------------------------------------===// |
| 582 | // ObjCCompatibleAliasDecl |
| 583 | //===----------------------------------------------------------------------===// |
| 584 | |
| 585 | ObjCCompatibleAliasDecl * |
| 586 | ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 587 | SourceLocation L, |
| 588 | IdentifierInfo *Id, |
| 589 | ObjCInterfaceDecl* AliasedClass) { |
| 590 | return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass); |
| 591 | } |
| 592 | |
| 593 | //===----------------------------------------------------------------------===// |
| 594 | // ObjCPropertyDecl |
| 595 | //===----------------------------------------------------------------------===// |
| 596 | |
| 597 | ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC, |
| 598 | SourceLocation L, |
| 599 | IdentifierInfo *Id, |
| 600 | QualType T, |
| 601 | PropertyControl propControl) { |
| 602 | return new (C) ObjCPropertyDecl(DC, L, Id, T); |
| 603 | } |
| 604 | |
| 605 | |
| 606 | //===----------------------------------------------------------------------===// |
| 607 | // ObjCPropertyImplDecl |
| 608 | //===----------------------------------------------------------------------===// |
| 609 | |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 610 | ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C, |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 611 | DeclContext *DC, |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 612 | SourceLocation atLoc, |
| 613 | SourceLocation L, |
| 614 | ObjCPropertyDecl *property, |
Daniel Dunbar | 14117fc | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 615 | Kind PK, |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 616 | ObjCIvarDecl *ivar) { |
Steve Naroff | 5abb028 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 617 | return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar); |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 618 | } |
Chris Lattner | 4485961 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 619 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 620 | |