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