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