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