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