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