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