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. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | |
| 34 | |
Chris Lattner | 4ee413b | 2009-02-20 21:44:01 +0000 | [diff] [blame] | 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 | |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 45 | /// getIvarDecl - This method looks up an ivar in this ContextDecl. |
| 46 | /// |
| 47 | ObjCIvarDecl * |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 48 | ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const { |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 49 | lookup_const_iterator Ivar, IvarEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 50 | for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) { |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 51 | if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar)) |
| 52 | return ivar; |
| 53 | } |
| 54 | return 0; |
| 55 | } |
| 56 | |
Argyrios Kyrtzidis | 467c0b1 | 2009-07-25 22:15:22 +0000 | [diff] [blame] | 57 | // Get the local instance/class method declared in this interface. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 58 | ObjCMethodDecl * |
Argyrios Kyrtzidis | 467c0b1 | 2009-07-25 22:15:22 +0000 | [diff] [blame] | 59 | ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const { |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 60 | // Since instance & class methods can have the same name, the loop below |
| 61 | // ensures we get the correct method. |
| 62 | // |
| 63 | // @interface Whatever |
| 64 | // - (int) class_method; |
| 65 | // + (float) class_method; |
| 66 | // @end |
| 67 | // |
| 68 | lookup_const_iterator Meth, MethEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 69 | for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) { |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 70 | ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth); |
Argyrios Kyrtzidis | 467c0b1 | 2009-07-25 22:15:22 +0000 | [diff] [blame] | 71 | if (MD && MD->isInstanceMethod() == isInstance) |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 72 | return MD; |
| 73 | } |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 77 | /// FindPropertyDeclaration - Finds declaration of the property given its name |
| 78 | /// in 'PropertyId' and returns it. It returns 0, if not found. |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 79 | /// FIXME: Convert to DeclContext lookup... |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 80 | /// |
| 81 | ObjCPropertyDecl * |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 82 | ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const { |
| 83 | for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 84 | if ((*I)->getIdentifier() == PropertyId) |
| 85 | return *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Fariborz Jahanian | a66793e | 2009-01-09 21:04:52 +0000 | [diff] [blame] | 87 | const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this); |
| 88 | if (PID) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 90 | E = PID->protocol_end(); I != E; ++I) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 91 | if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 92 | return P; |
Fariborz Jahanian | a66793e | 2009-01-09 21:04:52 +0000 | [diff] [blame] | 93 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Fariborz Jahanian | f034e9c | 2009-01-19 18:16:19 +0000 | [diff] [blame] | 95 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) { |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 96 | // Look through categories. |
| 97 | for (ObjCCategoryDecl *Category = OID->getCategoryList(); |
| 98 | Category; Category = Category->getNextClassCategory()) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 99 | if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId)) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 100 | return P; |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 101 | } |
| 102 | // Look through protocols. |
| 103 | for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(), |
| 104 | E = OID->protocol_end(); I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 105 | if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 106 | return P; |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 107 | } |
| 108 | if (OID->getSuperClass()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 109 | return OID->getSuperClass()->FindPropertyDeclaration(PropertyId); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 110 | } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) { |
Fariborz Jahanian | f034e9c | 2009-01-19 18:16:19 +0000 | [diff] [blame] | 111 | // Look through protocols. |
| 112 | for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(), |
| 113 | E = OCD->protocol_end(); I != E; ++I) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 114 | if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 115 | return P; |
Fariborz Jahanian | f034e9c | 2009-01-19 18:16:19 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
Steve Naroff | 3d2c22b | 2008-06-05 13:55:23 +0000 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 121 | void ObjCInterfaceDecl::mergeClassExtensionProtocolList( |
| 122 | ObjCProtocolDecl *const* ExtList, unsigned ExtNum, |
| 123 | ASTContext &C) |
| 124 | { |
| 125 | if (ReferencedProtocols.empty()) { |
| 126 | ReferencedProtocols.set(ExtList, ExtNum, C); |
| 127 | return; |
| 128 | } |
| 129 | // Check for duplicate protocol in class's protocol list. |
| 130 | // This is (O)2. But it is extremely rare and number of protocols in |
| 131 | // class or its extension are very few. |
| 132 | llvm::SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs; |
| 133 | for (unsigned i = 0; i < ExtNum; i++) { |
| 134 | bool protocolExists = false; |
| 135 | ObjCProtocolDecl *ProtoInExtension = ExtList[i]; |
| 136 | for (protocol_iterator p = protocol_begin(), e = protocol_end(); |
| 137 | p != e; p++) { |
| 138 | ObjCProtocolDecl *Proto = (*p); |
| 139 | if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) { |
| 140 | protocolExists = true; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | // Do we want to warn on a protocol in extension class which |
| 145 | // already exist in the class? Probably not. |
| 146 | if (!protocolExists) |
| 147 | ProtocolRefs.push_back(ProtoInExtension); |
| 148 | } |
| 149 | if (ProtocolRefs.empty()) |
| 150 | return; |
Fariborz Jahanian | b106fc6 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 151 | // Merge ProtocolRefs into class's protocol list; |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 152 | for (protocol_iterator p = protocol_begin(), e = protocol_end(); |
| 153 | p != e; p++) |
| 154 | ProtocolRefs.push_back(*p); |
| 155 | ReferencedProtocols.Destroy(C); |
| 156 | unsigned NumProtoRefs = ProtocolRefs.size(); |
| 157 | setProtocolList((ObjCProtocolDecl**)&ProtocolRefs[0], NumProtoRefs, C); |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 160 | ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID, |
| 161 | ObjCInterfaceDecl *&clsDeclared) { |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 162 | ObjCInterfaceDecl* ClassDecl = this; |
| 163 | while (ClassDecl != NULL) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 164 | if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) { |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 165 | clsDeclared = ClassDecl; |
| 166 | return I; |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 167 | } |
| 168 | ClassDecl = ClassDecl->getSuperClass(); |
| 169 | } |
| 170 | return NULL; |
| 171 | } |
| 172 | |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 173 | /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super |
| 174 | /// class whose name is passed as argument. If it is not one of the super classes |
| 175 | /// the it returns NULL. |
| 176 | ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass( |
| 177 | const IdentifierInfo*ICName) { |
| 178 | ObjCInterfaceDecl* ClassDecl = this; |
| 179 | while (ClassDecl != NULL) { |
| 180 | if (ClassDecl->getIdentifier() == ICName) |
| 181 | return ClassDecl; |
| 182 | ClassDecl = ClassDecl->getSuperClass(); |
| 183 | } |
| 184 | return NULL; |
| 185 | } |
| 186 | |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 187 | /// lookupMethod - This method returns an instance/class method by looking in |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 188 | /// the class, its categories, and its super classes (using a linear search). |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 189 | ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel, |
| 190 | bool isInstance) const { |
| 191 | const ObjCInterfaceDecl* ClassDecl = this; |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 192 | ObjCMethodDecl *MethodDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 194 | while (ClassDecl != NULL) { |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 195 | if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance))) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 196 | return MethodDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 198 | // Didn't find one yet - look through protocols. |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 199 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 200 | ClassDecl->getReferencedProtocols(); |
| 201 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 202 | E = Protocols.end(); I != E; ++I) |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 203 | if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance))) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 204 | return MethodDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 206 | // Didn't find one yet - now look through categories. |
| 207 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 208 | while (CatDecl) { |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 209 | if ((MethodDecl = CatDecl->getMethod(Sel, isInstance))) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 210 | return MethodDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Steve Naroff | b79c01e | 2008-12-08 20:57:28 +0000 | [diff] [blame] | 212 | // Didn't find one yet - look through protocols. |
| 213 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 214 | CatDecl->getReferencedProtocols(); |
| 215 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 216 | E = Protocols.end(); I != E; ++I) |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 217 | if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance))) |
Steve Naroff | b558422 | 2009-02-26 11:32:02 +0000 | [diff] [blame] | 218 | return MethodDecl; |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 219 | CatDecl = CatDecl->getNextClassCategory(); |
| 220 | } |
| 221 | ClassDecl = ClassDecl->getSuperClass(); |
| 222 | } |
| 223 | return NULL; |
| 224 | } |
| 225 | |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 226 | ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateInstanceMethod( |
| 227 | const Selector &Sel) { |
| 228 | ObjCMethodDecl *Method = 0; |
| 229 | if (ObjCImplementationDecl *ImpDecl = getImplementation()) |
| 230 | Method = ImpDecl->getInstanceMethod(Sel); |
| 231 | |
| 232 | if (!Method && getSuperClass()) |
| 233 | return getSuperClass()->lookupPrivateInstanceMethod(Sel); |
| 234 | return Method; |
| 235 | } |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 236 | |
| 237 | //===----------------------------------------------------------------------===// |
| 238 | // ObjCMethodDecl |
| 239 | //===----------------------------------------------------------------------===// |
| 240 | |
| 241 | ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | SourceLocation beginLoc, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 243 | SourceLocation endLoc, |
| 244 | Selector SelInfo, QualType T, |
| 245 | DeclContext *contextDecl, |
| 246 | bool isInstance, |
| 247 | bool isVariadic, |
| 248 | bool isSynthesized, |
| 249 | ImplementationControl impControl) { |
| 250 | return new (C) ObjCMethodDecl(beginLoc, endLoc, |
| 251 | SelInfo, T, contextDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | isInstance, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 253 | isVariadic, isSynthesized, impControl); |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 256 | void ObjCMethodDecl::Destroy(ASTContext &C) { |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 257 | if (Body) Body->Destroy(C); |
| 258 | if (SelfDecl) SelfDecl->Destroy(C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 260 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 261 | if (*I) (*I)->Destroy(C); |
| 262 | |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 263 | ParamInfo.Destroy(C); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 264 | |
| 265 | Decl::Destroy(C); |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Argyrios Kyrtzidis | 57ea6be | 2009-07-21 00:06:36 +0000 | [diff] [blame] | 268 | /// \brief A definition will return its interface declaration. |
| 269 | /// An interface declaration will return its definition. |
| 270 | /// Otherwise it will return itself. |
| 271 | ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() { |
| 272 | ASTContext &Ctx = getASTContext(); |
| 273 | ObjCMethodDecl *Redecl = 0; |
| 274 | Decl *CtxD = cast<Decl>(getDeclContext()); |
| 275 | |
| 276 | if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) { |
| 277 | if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD)) |
| 278 | Redecl = ImplD->getMethod(getSelector(), isInstanceMethod()); |
| 279 | |
| 280 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) { |
| 281 | if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD)) |
| 282 | Redecl = ImplD->getMethod(getSelector(), isInstanceMethod()); |
| 283 | |
Argyrios Kyrtzidis | 4292073 | 2009-07-28 05:11:05 +0000 | [diff] [blame] | 284 | } else if (ObjCImplementationDecl *ImplD = |
| 285 | dyn_cast<ObjCImplementationDecl>(CtxD)) { |
Argyrios Kyrtzidis | 57ea6be | 2009-07-21 00:06:36 +0000 | [diff] [blame] | 286 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
| 287 | Redecl = IFD->getMethod(getSelector(), isInstanceMethod()); |
Argyrios Kyrtzidis | 4292073 | 2009-07-28 05:11:05 +0000 | [diff] [blame] | 288 | |
| 289 | } else if (ObjCCategoryImplDecl *CImplD = |
| 290 | dyn_cast<ObjCCategoryImplDecl>(CtxD)) { |
| 291 | if (ObjCCategoryDecl *CatD = CImplD->getCategoryClass()) |
| 292 | Redecl = CatD->getMethod(getSelector(), isInstanceMethod()); |
Argyrios Kyrtzidis | 57ea6be | 2009-07-21 00:06:36 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | return Redecl ? Redecl : this; |
| 296 | } |
| 297 | |
Argyrios Kyrtzidis | e7f9d30 | 2009-07-28 05:11:17 +0000 | [diff] [blame] | 298 | ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() { |
| 299 | Decl *CtxD = cast<Decl>(getDeclContext()); |
| 300 | |
| 301 | if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) { |
| 302 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
| 303 | if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(), |
| 304 | isInstanceMethod())) |
| 305 | return MD; |
| 306 | |
| 307 | } else if (ObjCCategoryImplDecl *CImplD = |
| 308 | dyn_cast<ObjCCategoryImplDecl>(CtxD)) { |
| 309 | if (ObjCCategoryDecl *CatD = CImplD->getCategoryClass()) |
| 310 | if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(), |
| 311 | isInstanceMethod())) |
| 312 | return MD; |
| 313 | } |
| 314 | |
| 315 | return this; |
| 316 | } |
| 317 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | void ObjCMethodDecl::createImplicitParams(ASTContext &Context, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 319 | const ObjCInterfaceDecl *OID) { |
| 320 | QualType selfTy; |
| 321 | if (isInstanceMethod()) { |
| 322 | // There may be no interface context due to error in declaration |
| 323 | // of the interface (which has been reported). Recover gracefully. |
| 324 | if (OID) { |
Daniel Dunbar | 3b3a458 | 2009-04-22 04:34:53 +0000 | [diff] [blame] | 325 | selfTy = Context.getObjCInterfaceType(OID); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 326 | selfTy = Context.getObjCObjectPointerType(selfTy); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 327 | } else { |
| 328 | selfTy = Context.getObjCIdType(); |
| 329 | } |
| 330 | } else // we have a factory method. |
| 331 | selfTy = Context.getObjCClassType(); |
| 332 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(), |
Steve Naroff | 53c9d8a | 2009-04-20 15:06:07 +0000 | [diff] [blame] | 334 | &Context.Idents.get("self"), selfTy)); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 335 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(), |
| 337 | &Context.Idents.get("_cmd"), |
Steve Naroff | 53c9d8a | 2009-04-20 15:06:07 +0000 | [diff] [blame] | 338 | Context.getObjCSelType())); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 341 | ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() { |
| 342 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext())) |
| 343 | return ID; |
| 344 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext())) |
| 345 | return CD->getClassInterface(); |
Argyrios Kyrtzidis | a853037 | 2009-07-28 05:10:52 +0000 | [diff] [blame] | 346 | if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext())) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 347 | return IMD->getClassInterface(); |
Argyrios Kyrtzidis | a853037 | 2009-07-28 05:10:52 +0000 | [diff] [blame] | 348 | |
| 349 | assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method"); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 350 | assert(false && "unknown method context"); |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 354 | //===----------------------------------------------------------------------===// |
| 355 | // ObjCInterfaceDecl |
| 356 | //===----------------------------------------------------------------------===// |
| 357 | |
| 358 | ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C, |
| 359 | DeclContext *DC, |
| 360 | SourceLocation atLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | IdentifierInfo *Id, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 362 | SourceLocation ClassLoc, |
| 363 | bool ForwardDecl, bool isInternal){ |
| 364 | return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl, |
| 365 | isInternal); |
| 366 | } |
| 367 | |
| 368 | ObjCInterfaceDecl:: |
| 369 | ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id, |
| 370 | SourceLocation CLoc, bool FD, bool isInternal) |
| 371 | : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id), |
| 372 | TypeForDecl(0), SuperClass(0), |
| 373 | CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal), |
| 374 | ClassLoc(CLoc) { |
| 375 | } |
| 376 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | void ObjCInterfaceDecl::Destroy(ASTContext &C) { |
Chris Lattner | d13d302 | 2009-03-31 08:36:08 +0000 | [diff] [blame] | 378 | for (ivar_iterator I = ivar_begin(), E = ivar_end(); I != E; ++I) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 379 | if (*I) (*I)->Destroy(C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 381 | IVars.Destroy(C); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 382 | // FIXME: CategoryList? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 384 | // FIXME: Because there is no clear ownership |
| 385 | // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they |
| 386 | // reference, we destroy ObjCPropertyDecls in ~TranslationUnit. |
| 387 | Decl::Destroy(C); |
| 388 | } |
| 389 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 390 | ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const { |
| 391 | return getASTContext().getObjCImplementation( |
| 392 | const_cast<ObjCInterfaceDecl*>(this)); |
| 393 | } |
| 394 | |
| 395 | void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) { |
| 396 | getASTContext().setObjCImplementation(this, ImplD); |
| 397 | } |
| 398 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 399 | |
| 400 | /// FindCategoryDeclaration - Finds category declaration in the list of |
| 401 | /// categories for this class and returns it. Name of the category is passed |
| 402 | /// in 'CategoryId'. If category not found, return 0; |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 403 | /// |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 404 | ObjCCategoryDecl * |
| 405 | ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const { |
| 406 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 407 | Category; Category = Category->getNextClassCategory()) |
| 408 | if (Category->getIdentifier() == CategoryId) |
| 409 | return Category; |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 413 | ObjCMethodDecl * |
| 414 | ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const { |
| 415 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 416 | Category; Category = Category->getNextClassCategory()) |
| 417 | if (ObjCCategoryImplDecl *Impl = Category->getImplementation()) |
| 418 | if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel)) |
| 419 | return MD; |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const { |
| 424 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 425 | Category; Category = Category->getNextClassCategory()) |
| 426 | if (ObjCCategoryImplDecl *Impl = Category->getImplementation()) |
| 427 | if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel)) |
| 428 | return MD; |
| 429 | return 0; |
| 430 | } |
| 431 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 432 | /// ClassImplementsProtocol - Checks that 'lProto' protocol |
| 433 | /// has been implemented in IDecl class, its super class or categories (if |
| 434 | /// lookupCategory is true). |
| 435 | bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto, |
| 436 | bool lookupCategory, |
| 437 | bool RHSIsQualifiedID) { |
| 438 | ObjCInterfaceDecl *IDecl = this; |
| 439 | // 1st, look up the class. |
| 440 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 441 | IDecl->getReferencedProtocols(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 443 | for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(), |
| 444 | E = Protocols.end(); PI != E; ++PI) { |
| 445 | if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 446 | return true; |
| 447 | // This is dubious and is added to be compatible with gcc. In gcc, it is |
| 448 | // also allowed assigning a protocol-qualified 'id' type to a LHS object |
| 449 | // when protocol in qualified LHS is in list of protocols in the rhs 'id' |
| 450 | // object. This IMO, should be a bug. |
| 451 | // FIXME: Treat this as an extension, and flag this as an error when GCC |
| 452 | // extensions are not enabled. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | if (RHSIsQualifiedID && |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 454 | getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto)) |
| 455 | return true; |
| 456 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 458 | // 2nd, look up the category. |
| 459 | if (lookupCategory) |
| 460 | for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl; |
| 461 | CDecl = CDecl->getNextClassCategory()) { |
| 462 | for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(), |
| 463 | E = CDecl->protocol_end(); PI != E; ++PI) |
| 464 | if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 465 | return true; |
| 466 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 468 | // 3rd, look up the super class(s) |
| 469 | if (IDecl->getSuperClass()) |
| 470 | return |
| 471 | IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory, |
| 472 | RHSIsQualifiedID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 474 | return false; |
| 475 | } |
| 476 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 477 | //===----------------------------------------------------------------------===// |
| 478 | // ObjCIvarDecl |
| 479 | //===----------------------------------------------------------------------===// |
| 480 | |
| 481 | ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC, |
| 482 | SourceLocation L, IdentifierInfo *Id, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 483 | QualType T, DeclaratorInfo *DInfo, |
| 484 | AccessControl ac, Expr *BW) { |
| 485 | return new (C) ObjCIvarDecl(DC, L, Id, T, DInfo, ac, BW); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
| 489 | |
| 490 | //===----------------------------------------------------------------------===// |
| 491 | // ObjCAtDefsFieldDecl |
| 492 | //===----------------------------------------------------------------------===// |
| 493 | |
| 494 | ObjCAtDefsFieldDecl |
| 495 | *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 496 | IdentifierInfo *Id, QualType T, Expr *BW) { |
| 497 | return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW); |
| 498 | } |
| 499 | |
| 500 | void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) { |
| 501 | this->~ObjCAtDefsFieldDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | C.Deallocate((void *)this); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | //===----------------------------------------------------------------------===// |
| 506 | // ObjCProtocolDecl |
| 507 | //===----------------------------------------------------------------------===// |
| 508 | |
| 509 | ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | SourceLocation L, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 511 | IdentifierInfo *Id) { |
| 512 | return new (C) ObjCProtocolDecl(DC, L, Id); |
| 513 | } |
| 514 | |
| 515 | void ObjCProtocolDecl::Destroy(ASTContext &C) { |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 516 | ReferencedProtocols.Destroy(C); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 517 | ObjCContainerDecl::Destroy(C); |
| 518 | } |
| 519 | |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 520 | ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) { |
| 521 | ObjCProtocolDecl *PDecl = this; |
| 522 | |
| 523 | if (Name == getIdentifier()) |
| 524 | return PDecl; |
| 525 | |
| 526 | for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I) |
| 527 | if ((PDecl = (*I)->lookupProtocolNamed(Name))) |
| 528 | return PDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 530 | return NULL; |
| 531 | } |
| 532 | |
Argyrios Kyrtzidis | 094e2bb | 2009-07-25 22:15:38 +0000 | [diff] [blame] | 533 | // lookupMethod - Lookup a instance/class method in the protocol and protocols |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 534 | // it inherited. |
Argyrios Kyrtzidis | 094e2bb | 2009-07-25 22:15:38 +0000 | [diff] [blame] | 535 | ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel, |
| 536 | bool isInstance) const { |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 537 | ObjCMethodDecl *MethodDecl = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | |
Argyrios Kyrtzidis | 094e2bb | 2009-07-25 22:15:38 +0000 | [diff] [blame] | 539 | if ((MethodDecl = getMethod(Sel, isInstance))) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 540 | return MethodDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 542 | for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I) |
Argyrios Kyrtzidis | 094e2bb | 2009-07-25 22:15:38 +0000 | [diff] [blame] | 543 | if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance))) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 544 | return MethodDecl; |
| 545 | return NULL; |
| 546 | } |
| 547 | |
| 548 | //===----------------------------------------------------------------------===// |
| 549 | // ObjCClassDecl |
| 550 | //===----------------------------------------------------------------------===// |
| 551 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L, |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 553 | ObjCInterfaceDecl *const *Elts, unsigned nElts, |
| 554 | ASTContext &C) |
| 555 | : Decl(ObjCClass, DC, L) { |
| 556 | ForwardDecls.set(Elts, nElts, C); |
| 557 | } |
| 558 | |
| 559 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 560 | ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC, |
| 561 | SourceLocation L, |
| 562 | ObjCInterfaceDecl *const *Elts, |
| 563 | unsigned nElts) { |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 564 | return new (C) ObjCClassDecl(DC, L, Elts, nElts, C); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | void ObjCClassDecl::Destroy(ASTContext &C) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 569 | // FIXME: There is no clear ownership policy now for referenced |
| 570 | // ObjCInterfaceDecls. Some of them can be forward declarations that |
| 571 | // are never later defined (in which case the ObjCClassDecl owns them) |
| 572 | // or the ObjCInterfaceDecl later becomes a real definition later. Ideally |
| 573 | // we should have separate objects for forward declarations and definitions, |
| 574 | // obviating this problem. Because of this situation, referenced |
| 575 | // ObjCInterfaceDecls are destroyed in ~TranslationUnit. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 577 | ForwardDecls.Destroy(C); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 578 | Decl::Destroy(C); |
| 579 | } |
| 580 | |
| 581 | //===----------------------------------------------------------------------===// |
| 582 | // ObjCForwardProtocolDecl |
| 583 | //===----------------------------------------------------------------------===// |
| 584 | |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 585 | ObjCForwardProtocolDecl:: |
| 586 | ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L, |
| 587 | ObjCProtocolDecl *const *Elts, unsigned nElts, |
| 588 | ASTContext &C) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | : Decl(ObjCForwardProtocol, DC, L) { |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 590 | ReferencedProtocols.set(Elts, nElts, C); |
| 591 | } |
| 592 | |
| 593 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 594 | ObjCForwardProtocolDecl * |
| 595 | ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | SourceLocation L, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 597 | ObjCProtocolDecl *const *Elts, |
| 598 | unsigned NumElts) { |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 599 | return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, C); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | void ObjCForwardProtocolDecl::Destroy(ASTContext &C) { |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 603 | ReferencedProtocols.Destroy(C); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 604 | Decl::Destroy(C); |
| 605 | } |
| 606 | |
| 607 | //===----------------------------------------------------------------------===// |
| 608 | // ObjCCategoryDecl |
| 609 | //===----------------------------------------------------------------------===// |
| 610 | |
| 611 | ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC, |
| 612 | SourceLocation L, |
| 613 | IdentifierInfo *Id) { |
| 614 | return new (C) ObjCCategoryDecl(DC, L, Id); |
| 615 | } |
| 616 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 617 | ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const { |
| 618 | return getASTContext().getObjCImplementation( |
| 619 | const_cast<ObjCCategoryDecl*>(this)); |
| 620 | } |
| 621 | |
| 622 | void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) { |
| 623 | getASTContext().setObjCImplementation(this, ImplD); |
| 624 | } |
| 625 | |
| 626 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 627 | //===----------------------------------------------------------------------===// |
| 628 | // ObjCCategoryImplDecl |
| 629 | //===----------------------------------------------------------------------===// |
| 630 | |
| 631 | ObjCCategoryImplDecl * |
| 632 | ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC, |
| 633 | SourceLocation L,IdentifierInfo *Id, |
| 634 | ObjCInterfaceDecl *ClassInterface) { |
| 635 | return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface); |
| 636 | } |
| 637 | |
Argyrios Kyrtzidis | 4292073 | 2009-07-28 05:11:05 +0000 | [diff] [blame] | 638 | ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryClass() const { |
| 639 | return getClassInterface()->FindCategoryDeclaration(getIdentifier()); |
| 640 | } |
| 641 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 642 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 643 | void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) { |
Douglas Gregor | 2c2d43c | 2009-04-23 02:42:49 +0000 | [diff] [blame] | 644 | // FIXME: The context should be correct before we get here. |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 645 | property->setLexicalDeclContext(this); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 646 | addDecl(property); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 649 | void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) { |
| 650 | ASTContext &Ctx = getASTContext(); |
| 651 | |
| 652 | if (ObjCImplementationDecl *ImplD |
Duncan Sands | 98f2cca | 2009-07-21 07:56:29 +0000 | [diff] [blame] | 653 | = dyn_cast_or_null<ObjCImplementationDecl>(this)) { |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 654 | if (IFace) |
| 655 | Ctx.setObjCImplementation(IFace, ImplD); |
| 656 | |
Duncan Sands | 98f2cca | 2009-07-21 07:56:29 +0000 | [diff] [blame] | 657 | } else if (ObjCCategoryImplDecl *ImplD = |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 658 | dyn_cast_or_null<ObjCCategoryImplDecl>(this)) { |
| 659 | if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier())) |
| 660 | Ctx.setObjCImplementation(CD, ImplD); |
| 661 | } |
| 662 | |
| 663 | ClassInterface = IFace; |
| 664 | } |
| 665 | |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 666 | /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of |
Chris Lattner | d6eed1c | 2009-02-16 19:24:31 +0000 | [diff] [blame] | 667 | /// properties implemented in this category @implementation block and returns |
| 668 | /// the implemented property that uses it. |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 669 | /// |
Chris Lattner | 3aa1861 | 2009-02-28 18:42:10 +0000 | [diff] [blame] | 670 | ObjCPropertyImplDecl *ObjCImplDecl:: |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 671 | FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const { |
| 672 | for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 673 | ObjCPropertyImplDecl *PID = *i; |
| 674 | if (PID->getPropertyIvarDecl() && |
| 675 | PID->getPropertyIvarDecl()->getIdentifier() == ivarId) |
| 676 | return PID; |
| 677 | } |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl |
| 682 | /// added to the list of those properties @synthesized/@dynamic in this |
| 683 | /// category @implementation block. |
| 684 | /// |
Chris Lattner | 3aa1861 | 2009-02-28 18:42:10 +0000 | [diff] [blame] | 685 | ObjCPropertyImplDecl *ObjCImplDecl:: |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 686 | FindPropertyImplDecl(IdentifierInfo *Id) const { |
| 687 | for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 688 | ObjCPropertyImplDecl *PID = *i; |
| 689 | if (PID->getPropertyDecl()->getIdentifier() == Id) |
| 690 | return PID; |
| 691 | } |
| 692 | return 0; |
| 693 | } |
| 694 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 695 | //===----------------------------------------------------------------------===// |
| 696 | // ObjCImplementationDecl |
| 697 | //===----------------------------------------------------------------------===// |
| 698 | |
| 699 | ObjCImplementationDecl * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 700 | ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 701 | SourceLocation L, |
| 702 | ObjCInterfaceDecl *ClassInterface, |
| 703 | ObjCInterfaceDecl *SuperDecl) { |
| 704 | return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl); |
| 705 | } |
| 706 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 707 | //===----------------------------------------------------------------------===// |
| 708 | // ObjCCompatibleAliasDecl |
| 709 | //===----------------------------------------------------------------------===// |
| 710 | |
| 711 | ObjCCompatibleAliasDecl * |
| 712 | ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 713 | SourceLocation L, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | IdentifierInfo *Id, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 715 | ObjCInterfaceDecl* AliasedClass) { |
| 716 | return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass); |
| 717 | } |
| 718 | |
| 719 | //===----------------------------------------------------------------------===// |
| 720 | // ObjCPropertyDecl |
| 721 | //===----------------------------------------------------------------------===// |
| 722 | |
| 723 | ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC, |
| 724 | SourceLocation L, |
| 725 | IdentifierInfo *Id, |
| 726 | QualType T, |
| 727 | PropertyControl propControl) { |
| 728 | return new (C) ObjCPropertyDecl(DC, L, Id, T); |
| 729 | } |
| 730 | |
| 731 | |
| 732 | //===----------------------------------------------------------------------===// |
| 733 | // ObjCPropertyImplDecl |
| 734 | //===----------------------------------------------------------------------===// |
| 735 | |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 736 | ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C, |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 737 | DeclContext *DC, |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 738 | SourceLocation atLoc, |
| 739 | SourceLocation L, |
| 740 | ObjCPropertyDecl *property, |
Daniel Dunbar | 9f0afd4 | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 741 | Kind PK, |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 742 | ObjCIvarDecl *ivar) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 743 | return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 744 | } |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 745 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 746 | |