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