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" |
Argyrios Kyrtzidis | e6b8d68 | 2011-09-01 00:58:55 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTMutationListener.h" |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// |
Chris Lattner | 11e1e1a | 2009-02-20 21:16:26 +0000 | [diff] [blame] | 22 | // ObjCListBase |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 25 | void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) { |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 26 | List = 0; |
Chris Lattner | 11e1e1a | 2009-02-20 21:16:26 +0000 | [diff] [blame] | 27 | if (Elts == 0) return; // Setting to an empty list is a noop. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 28 | |
| 29 | |
Chris Lattner | 4ee413b | 2009-02-20 21:44:01 +0000 | [diff] [blame] | 30 | List = new (Ctx) void*[Elts]; |
Chris Lattner | 11e1e1a | 2009-02-20 21:16:26 +0000 | [diff] [blame] | 31 | NumElts = Elts; |
| 32 | memcpy(List, InList, sizeof(void*)*Elts); |
| 33 | } |
| 34 | |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 35 | void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts, |
| 36 | const SourceLocation *Locs, ASTContext &Ctx) { |
| 37 | if (Elts == 0) |
| 38 | return; |
| 39 | |
| 40 | Locations = new (Ctx) SourceLocation[Elts]; |
| 41 | memcpy(Locations, Locs, sizeof(SourceLocation) * Elts); |
| 42 | set(InList, Elts, Ctx); |
| 43 | } |
| 44 | |
Chris Lattner | 11e1e1a | 2009-02-20 21:16:26 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 46 | // ObjCInterfaceDecl |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 49 | /// getIvarDecl - This method looks up an ivar in this ContextDecl. |
| 50 | /// |
| 51 | ObjCIvarDecl * |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 52 | ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const { |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 53 | lookup_const_iterator Ivar, IvarEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 54 | for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) { |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 55 | if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar)) |
| 56 | return ivar; |
| 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | |
Argyrios Kyrtzidis | 467c0b1 | 2009-07-25 22:15:22 +0000 | [diff] [blame] | 61 | // Get the local instance/class method declared in this interface. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 62 | ObjCMethodDecl * |
Argyrios Kyrtzidis | 467c0b1 | 2009-07-25 22:15:22 +0000 | [diff] [blame] | 63 | ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const { |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 64 | // Since instance & class methods can have the same name, the loop below |
| 65 | // ensures we get the correct method. |
| 66 | // |
| 67 | // @interface Whatever |
| 68 | // - (int) class_method; |
| 69 | // + (float) class_method; |
| 70 | // @end |
| 71 | // |
| 72 | lookup_const_iterator Meth, MethEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 73 | for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) { |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 74 | ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth); |
Argyrios Kyrtzidis | 467c0b1 | 2009-07-25 22:15:22 +0000 | [diff] [blame] | 75 | if (MD && MD->isInstanceMethod() == isInstance) |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 76 | return MD; |
| 77 | } |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
Ted Kremenek | 9f550ff | 2010-03-15 20:11:46 +0000 | [diff] [blame] | 81 | ObjCPropertyDecl * |
Ted Kremenek | de09d0c | 2010-03-15 20:11:53 +0000 | [diff] [blame] | 82 | ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC, |
Ted Kremenek | 9f550ff | 2010-03-15 20:11:46 +0000 | [diff] [blame] | 83 | IdentifierInfo *propertyID) { |
| 84 | |
Ted Kremenek | de09d0c | 2010-03-15 20:11:53 +0000 | [diff] [blame] | 85 | DeclContext::lookup_const_iterator I, E; |
Ted Kremenek | 9f550ff | 2010-03-15 20:11:46 +0000 | [diff] [blame] | 86 | llvm::tie(I, E) = DC->lookup(propertyID); |
| 87 | for ( ; I != E; ++I) |
| 88 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I)) |
| 89 | return PD; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 94 | /// FindPropertyDeclaration - Finds declaration of the property given its name |
| 95 | /// in 'PropertyId' and returns it. It returns 0, if not found. |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 96 | ObjCPropertyDecl * |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 97 | ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | de09d0c | 2010-03-15 20:11:53 +0000 | [diff] [blame] | 99 | if (ObjCPropertyDecl *PD = |
| 100 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId)) |
| 101 | return PD; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | de09d0c | 2010-03-15 20:11:53 +0000 | [diff] [blame] | 103 | switch (getKind()) { |
| 104 | default: |
| 105 | break; |
| 106 | case Decl::ObjCProtocol: { |
| 107 | const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this); |
| 108 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 109 | E = PID->protocol_end(); I != E; ++I) |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 110 | if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) |
| 111 | return P; |
Ted Kremenek | de09d0c | 2010-03-15 20:11:53 +0000 | [diff] [blame] | 112 | break; |
| 113 | } |
| 114 | case Decl::ObjCInterface: { |
| 115 | const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this); |
| 116 | // Look through categories. |
| 117 | for (ObjCCategoryDecl *Cat = OID->getCategoryList(); |
| 118 | Cat; Cat = Cat->getNextClassCategory()) |
| 119 | if (!Cat->IsClassExtension()) |
| 120 | if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId)) |
| 121 | return P; |
| 122 | |
| 123 | // Look through protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 124 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 125 | I = OID->all_referenced_protocol_begin(), |
| 126 | E = OID->all_referenced_protocol_end(); I != E; ++I) |
Ted Kremenek | de09d0c | 2010-03-15 20:11:53 +0000 | [diff] [blame] | 127 | if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) |
| 128 | return P; |
| 129 | |
| 130 | // Finally, check the super class. |
| 131 | if (const ObjCInterfaceDecl *superClass = OID->getSuperClass()) |
| 132 | return superClass->FindPropertyDeclaration(PropertyId); |
| 133 | break; |
| 134 | } |
| 135 | case Decl::ObjCCategory: { |
| 136 | const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this); |
| 137 | // Look through protocols. |
| 138 | if (!OCD->IsClassExtension()) |
| 139 | for (ObjCCategoryDecl::protocol_iterator |
| 140 | I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I) |
| 141 | if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) |
| 142 | return P; |
| 143 | |
| 144 | break; |
Fariborz Jahanian | f034e9c | 2009-01-19 18:16:19 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
Steve Naroff | 3d2c22b | 2008-06-05 13:55:23 +0000 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 150 | /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property |
| 151 | /// with name 'PropertyId' in the primary class; including those in protocols |
Ted Kremenek | 37cafb0 | 2010-03-15 20:30:07 +0000 | [diff] [blame] | 152 | /// (direct or indirect) used by the primary class. |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 153 | /// |
| 154 | ObjCPropertyDecl * |
Ted Kremenek | 37cafb0 | 2010-03-15 20:30:07 +0000 | [diff] [blame] | 155 | ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass( |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 156 | IdentifierInfo *PropertyId) const { |
Douglas Gregor | 26ac3f3 | 2010-12-01 23:49:52 +0000 | [diff] [blame] | 157 | if (ExternallyCompleted) |
| 158 | LoadExternalDefinition(); |
| 159 | |
Ted Kremenek | 37cafb0 | 2010-03-15 20:30:07 +0000 | [diff] [blame] | 160 | if (ObjCPropertyDecl *PD = |
| 161 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId)) |
| 162 | return PD; |
| 163 | |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 164 | // Look through protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 165 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 166 | I = all_referenced_protocol_begin(), |
| 167 | E = all_referenced_protocol_end(); I != E; ++I) |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 168 | if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) |
| 169 | return P; |
Ted Kremenek | 37cafb0 | 2010-03-15 20:30:07 +0000 | [diff] [blame] | 170 | |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 174 | void ObjCInterfaceDecl::mergeClassExtensionProtocolList( |
| 175 | ObjCProtocolDecl *const* ExtList, unsigned ExtNum, |
| 176 | ASTContext &C) |
| 177 | { |
Douglas Gregor | 26ac3f3 | 2010-12-01 23:49:52 +0000 | [diff] [blame] | 178 | if (ExternallyCompleted) |
| 179 | LoadExternalDefinition(); |
| 180 | |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 181 | if (AllReferencedProtocols.empty() && ReferencedProtocols.empty()) { |
| 182 | AllReferencedProtocols.set(ExtList, ExtNum, C); |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 183 | return; |
| 184 | } |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 185 | |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 186 | // Check for duplicate protocol in class's protocol list. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 187 | // This is O(n*m). But it is extremely rare and number of protocols in |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 188 | // class or its extension are very few. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 189 | SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs; |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 190 | for (unsigned i = 0; i < ExtNum; i++) { |
| 191 | bool protocolExists = false; |
| 192 | ObjCProtocolDecl *ProtoInExtension = ExtList[i]; |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 193 | for (all_protocol_iterator |
| 194 | p = all_referenced_protocol_begin(), |
| 195 | e = all_referenced_protocol_end(); p != e; ++p) { |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 196 | ObjCProtocolDecl *Proto = (*p); |
| 197 | if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) { |
| 198 | protocolExists = true; |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | // Do we want to warn on a protocol in extension class which |
| 203 | // already exist in the class? Probably not. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 204 | if (!protocolExists) |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 205 | ProtocolRefs.push_back(ProtoInExtension); |
| 206 | } |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 207 | |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 208 | if (ProtocolRefs.empty()) |
| 209 | return; |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 210 | |
Fariborz Jahanian | b106fc6 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 211 | // Merge ProtocolRefs into class's protocol list; |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 212 | for (all_protocol_iterator p = all_referenced_protocol_begin(), |
| 213 | e = all_referenced_protocol_end(); p != e; ++p) { |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 214 | ProtocolRefs.push_back(*p); |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 215 | } |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 216 | |
| 217 | AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(), C); |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 220 | /// getFirstClassExtension - Find first class extension of the given class. |
| 221 | ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const { |
| 222 | for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl; |
Fariborz Jahanian | 0e5ad25 | 2010-02-23 01:26:30 +0000 | [diff] [blame] | 223 | CDecl = CDecl->getNextClassCategory()) |
| 224 | if (CDecl->IsClassExtension()) |
| 225 | return CDecl; |
| 226 | return 0; |
| 227 | } |
| 228 | |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 229 | /// getNextClassCategory - Find next class extension in list of categories. |
| 230 | const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const { |
| 231 | for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl; |
| 232 | CDecl = CDecl->getNextClassCategory()) |
| 233 | if (CDecl->IsClassExtension()) |
| 234 | return CDecl; |
| 235 | return 0; |
| 236 | } |
| 237 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 238 | ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID, |
| 239 | ObjCInterfaceDecl *&clsDeclared) { |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 240 | ObjCInterfaceDecl* ClassDecl = this; |
| 241 | while (ClassDecl != NULL) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 242 | if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) { |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 243 | clsDeclared = ClassDecl; |
| 244 | return I; |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 245 | } |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 246 | for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension(); |
| 247 | CDecl; CDecl = CDecl->getNextClassExtension()) { |
Fariborz Jahanian | 0e5ad25 | 2010-02-23 01:26:30 +0000 | [diff] [blame] | 248 | if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) { |
| 249 | clsDeclared = ClassDecl; |
| 250 | return I; |
| 251 | } |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 252 | } |
Fariborz Jahanian | 0e5ad25 | 2010-02-23 01:26:30 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 254 | ClassDecl = ClassDecl->getSuperClass(); |
| 255 | } |
| 256 | return NULL; |
| 257 | } |
| 258 | |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 259 | /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super |
| 260 | /// class whose name is passed as argument. If it is not one of the super classes |
| 261 | /// the it returns NULL. |
| 262 | ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass( |
| 263 | const IdentifierInfo*ICName) { |
| 264 | ObjCInterfaceDecl* ClassDecl = this; |
| 265 | while (ClassDecl != NULL) { |
| 266 | if (ClassDecl->getIdentifier() == ICName) |
| 267 | return ClassDecl; |
| 268 | ClassDecl = ClassDecl->getSuperClass(); |
| 269 | } |
| 270 | return NULL; |
| 271 | } |
| 272 | |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 273 | /// lookupMethod - This method returns an instance/class method by looking in |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 274 | /// the class, its categories, and its super classes (using a linear search). |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 275 | ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel, |
| 276 | bool isInstance) const { |
| 277 | const ObjCInterfaceDecl* ClassDecl = this; |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 278 | ObjCMethodDecl *MethodDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Douglas Gregor | 26ac3f3 | 2010-12-01 23:49:52 +0000 | [diff] [blame] | 280 | if (ExternallyCompleted) |
| 281 | LoadExternalDefinition(); |
| 282 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 283 | while (ClassDecl != NULL) { |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 284 | if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance))) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 285 | return MethodDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 287 | // Didn't find one yet - look through protocols. |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 288 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 289 | ClassDecl->getReferencedProtocols(); |
| 290 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 291 | E = Protocols.end(); I != E; ++I) |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 292 | if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance))) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 293 | return MethodDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 295 | // Didn't find one yet - now look through categories. |
| 296 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 297 | while (CatDecl) { |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 298 | if ((MethodDecl = CatDecl->getMethod(Sel, isInstance))) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 299 | return MethodDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
Steve Naroff | b79c01e | 2008-12-08 20:57:28 +0000 | [diff] [blame] | 301 | // Didn't find one yet - look through protocols. |
| 302 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 303 | CatDecl->getReferencedProtocols(); |
| 304 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 305 | E = Protocols.end(); I != E; ++I) |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 306 | if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance))) |
Steve Naroff | b558422 | 2009-02-26 11:32:02 +0000 | [diff] [blame] | 307 | return MethodDecl; |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 308 | CatDecl = CatDecl->getNextClassCategory(); |
| 309 | } |
| 310 | ClassDecl = ClassDecl->getSuperClass(); |
| 311 | } |
| 312 | return NULL; |
| 313 | } |
| 314 | |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 315 | ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod( |
| 316 | const Selector &Sel, |
| 317 | bool Instance) { |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 318 | ObjCMethodDecl *Method = 0; |
| 319 | if (ObjCImplementationDecl *ImpDecl = getImplementation()) |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 320 | Method = Instance ? ImpDecl->getInstanceMethod(Sel) |
| 321 | : ImpDecl->getClassMethod(Sel); |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 322 | |
| 323 | if (!Method && getSuperClass()) |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 324 | return getSuperClass()->lookupPrivateMethod(Sel, Instance); |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 325 | return Method; |
| 326 | } |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 327 | |
| 328 | //===----------------------------------------------------------------------===// |
| 329 | // ObjCMethodDecl |
| 330 | //===----------------------------------------------------------------------===// |
| 331 | |
| 332 | ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | SourceLocation beginLoc, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 334 | SourceLocation endLoc, |
| 335 | Selector SelInfo, QualType T, |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 336 | TypeSourceInfo *ResultTInfo, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 337 | DeclContext *contextDecl, |
| 338 | bool isInstance, |
| 339 | bool isVariadic, |
| 340 | bool isSynthesized, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 341 | bool isImplicitlyDeclared, |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 342 | bool isDefined, |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 343 | ImplementationControl impControl, |
Argyrios Kyrtzidis | da92a7f | 2011-10-03 06:36:29 +0000 | [diff] [blame] | 344 | bool HasRelatedResultType) { |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 345 | return new (C) ObjCMethodDecl(beginLoc, endLoc, |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 346 | SelInfo, T, ResultTInfo, contextDecl, |
| 347 | isInstance, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 348 | isVariadic, isSynthesized, isImplicitlyDeclared, |
| 349 | isDefined, |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 350 | impControl, |
Argyrios Kyrtzidis | da92a7f | 2011-10-03 06:36:29 +0000 | [diff] [blame] | 351 | HasRelatedResultType); |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Argyrios Kyrtzidis | 3a919e7 | 2011-10-14 08:02:31 +0000 | [diff] [blame^] | 354 | void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) { |
| 355 | assert(PrevMethod); |
| 356 | getASTContext().setObjCMethodRedeclaration(PrevMethod, this); |
| 357 | IsRedeclaration = true; |
| 358 | } |
| 359 | |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 360 | void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C, |
| 361 | ArrayRef<ParmVarDecl*> Params, |
| 362 | ArrayRef<SourceLocation> SelLocs) { |
| 363 | ParamsAndSelLocs = 0; |
| 364 | NumParams = Params.size(); |
| 365 | if (Params.empty() && SelLocs.empty()) |
| 366 | return; |
| 367 | |
| 368 | unsigned Size = sizeof(ParmVarDecl *) * NumParams + |
| 369 | sizeof(SourceLocation) * SelLocs.size(); |
| 370 | ParamsAndSelLocs = C.Allocate(Size); |
| 371 | std::copy(Params.begin(), Params.end(), getParams()); |
| 372 | std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs()); |
| 373 | } |
| 374 | |
| 375 | void ObjCMethodDecl::getSelectorLocs( |
| 376 | SmallVectorImpl<SourceLocation> &SelLocs) const { |
| 377 | for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i) |
| 378 | SelLocs.push_back(getSelectorLoc(i)); |
| 379 | } |
| 380 | |
| 381 | void ObjCMethodDecl::setMethodParams(ASTContext &C, |
| 382 | ArrayRef<ParmVarDecl*> Params, |
| 383 | ArrayRef<SourceLocation> SelLocs) { |
| 384 | assert((!SelLocs.empty() || isImplicit()) && |
| 385 | "No selector locs for non-implicit method"); |
| 386 | if (isImplicit()) |
| 387 | return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>()); |
| 388 | |
| 389 | SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params, EndLoc); |
| 390 | if (SelLocsKind != SelLoc_NonStandard) |
| 391 | return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>()); |
| 392 | |
| 393 | setParamsAndSelLocs(C, Params, SelLocs); |
| 394 | } |
| 395 | |
Argyrios Kyrtzidis | 57ea6be | 2009-07-21 00:06:36 +0000 | [diff] [blame] | 396 | /// \brief A definition will return its interface declaration. |
| 397 | /// An interface declaration will return its definition. |
| 398 | /// Otherwise it will return itself. |
| 399 | ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() { |
| 400 | ASTContext &Ctx = getASTContext(); |
Argyrios Kyrtzidis | b40034c | 2011-10-14 06:48:06 +0000 | [diff] [blame] | 401 | ObjCMethodDecl *Redecl = |
| 402 | const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this)); |
| 403 | if (Redecl) |
| 404 | return Redecl; |
| 405 | |
Argyrios Kyrtzidis | 57ea6be | 2009-07-21 00:06:36 +0000 | [diff] [blame] | 406 | Decl *CtxD = cast<Decl>(getDeclContext()); |
| 407 | |
| 408 | if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) { |
| 409 | if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD)) |
| 410 | Redecl = ImplD->getMethod(getSelector(), isInstanceMethod()); |
| 411 | |
| 412 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) { |
| 413 | if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD)) |
| 414 | Redecl = ImplD->getMethod(getSelector(), isInstanceMethod()); |
| 415 | |
Argyrios Kyrtzidis | 4292073 | 2009-07-28 05:11:05 +0000 | [diff] [blame] | 416 | } else if (ObjCImplementationDecl *ImplD = |
| 417 | dyn_cast<ObjCImplementationDecl>(CtxD)) { |
Argyrios Kyrtzidis | 57ea6be | 2009-07-21 00:06:36 +0000 | [diff] [blame] | 418 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
| 419 | Redecl = IFD->getMethod(getSelector(), isInstanceMethod()); |
Argyrios Kyrtzidis | 4292073 | 2009-07-28 05:11:05 +0000 | [diff] [blame] | 420 | |
| 421 | } else if (ObjCCategoryImplDecl *CImplD = |
| 422 | dyn_cast<ObjCCategoryImplDecl>(CtxD)) { |
Steve Naroff | 0d69b8c | 2009-10-29 21:11:04 +0000 | [diff] [blame] | 423 | if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl()) |
Argyrios Kyrtzidis | 4292073 | 2009-07-28 05:11:05 +0000 | [diff] [blame] | 424 | Redecl = CatD->getMethod(getSelector(), isInstanceMethod()); |
Argyrios Kyrtzidis | 57ea6be | 2009-07-21 00:06:36 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Argyrios Kyrtzidis | 3a919e7 | 2011-10-14 08:02:31 +0000 | [diff] [blame^] | 427 | if (!Redecl && isRedeclaration()) { |
| 428 | // This is the last redeclaration, go back to the first method. |
| 429 | return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(), |
| 430 | isInstanceMethod()); |
| 431 | } |
| 432 | |
Argyrios Kyrtzidis | 57ea6be | 2009-07-21 00:06:36 +0000 | [diff] [blame] | 433 | return Redecl ? Redecl : this; |
| 434 | } |
| 435 | |
Argyrios Kyrtzidis | e7f9d30 | 2009-07-28 05:11:17 +0000 | [diff] [blame] | 436 | ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() { |
| 437 | Decl *CtxD = cast<Decl>(getDeclContext()); |
| 438 | |
| 439 | if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) { |
| 440 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
| 441 | if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(), |
| 442 | isInstanceMethod())) |
| 443 | return MD; |
| 444 | |
| 445 | } else if (ObjCCategoryImplDecl *CImplD = |
| 446 | dyn_cast<ObjCCategoryImplDecl>(CtxD)) { |
Steve Naroff | 0d69b8c | 2009-10-29 21:11:04 +0000 | [diff] [blame] | 447 | if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl()) |
Argyrios Kyrtzidis | e7f9d30 | 2009-07-28 05:11:17 +0000 | [diff] [blame] | 448 | if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(), |
| 449 | isInstanceMethod())) |
| 450 | return MD; |
| 451 | } |
| 452 | |
| 453 | return this; |
| 454 | } |
| 455 | |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 456 | ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const { |
| 457 | ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family); |
John McCall | d976c8e | 2011-03-02 21:01:41 +0000 | [diff] [blame] | 458 | if (family != static_cast<unsigned>(InvalidObjCMethodFamily)) |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 459 | return family; |
| 460 | |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 461 | // Check for an explicit attribute. |
| 462 | if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) { |
| 463 | // The unfortunate necessity of mapping between enums here is due |
| 464 | // to the attributes framework. |
| 465 | switch (attr->getFamily()) { |
| 466 | case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break; |
| 467 | case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break; |
| 468 | case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break; |
| 469 | case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break; |
| 470 | case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break; |
| 471 | case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break; |
| 472 | } |
| 473 | Family = static_cast<unsigned>(family); |
| 474 | return family; |
| 475 | } |
| 476 | |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 477 | family = getSelector().getMethodFamily(); |
| 478 | switch (family) { |
| 479 | case OMF_None: break; |
| 480 | |
| 481 | // init only has a conventional meaning for an instance method, and |
| 482 | // it has to return an object. |
| 483 | case OMF_init: |
| 484 | if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType()) |
| 485 | family = OMF_None; |
| 486 | break; |
| 487 | |
| 488 | // alloc/copy/new have a conventional meaning for both class and |
| 489 | // instance methods, but they require an object return. |
| 490 | case OMF_alloc: |
| 491 | case OMF_copy: |
| 492 | case OMF_mutableCopy: |
| 493 | case OMF_new: |
| 494 | if (!getResultType()->isObjCObjectPointerType()) |
| 495 | family = OMF_None; |
| 496 | break; |
| 497 | |
| 498 | // These selectors have a conventional meaning only for instance methods. |
| 499 | case OMF_dealloc: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 500 | case OMF_finalize: |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 501 | case OMF_retain: |
| 502 | case OMF_release: |
| 503 | case OMF_autorelease: |
| 504 | case OMF_retainCount: |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 505 | case OMF_self: |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 506 | if (!isInstanceMethod()) |
| 507 | family = OMF_None; |
| 508 | break; |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 509 | |
| 510 | case OMF_performSelector: |
| 511 | if (!isInstanceMethod() || |
| 512 | !getResultType()->isObjCIdType()) |
| 513 | family = OMF_None; |
| 514 | else { |
| 515 | unsigned noParams = param_size(); |
| 516 | if (noParams < 1 || noParams > 3) |
| 517 | family = OMF_None; |
| 518 | else { |
| 519 | ObjCMethodDecl::arg_type_iterator it = arg_type_begin(); |
| 520 | QualType ArgT = (*it); |
| 521 | if (!ArgT->isObjCSelType()) { |
| 522 | family = OMF_None; |
| 523 | break; |
| 524 | } |
| 525 | while (--noParams) { |
| 526 | it++; |
| 527 | ArgT = (*it); |
| 528 | if (!ArgT->isObjCIdType()) { |
| 529 | family = OMF_None; |
| 530 | break; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | break; |
| 536 | |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | // Cache the result. |
| 540 | Family = static_cast<unsigned>(family); |
| 541 | return family; |
| 542 | } |
| 543 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | void ObjCMethodDecl::createImplicitParams(ASTContext &Context, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 545 | const ObjCInterfaceDecl *OID) { |
| 546 | QualType selfTy; |
| 547 | if (isInstanceMethod()) { |
| 548 | // There may be no interface context due to error in declaration |
| 549 | // of the interface (which has been reported). Recover gracefully. |
| 550 | if (OID) { |
Daniel Dunbar | 3b3a458 | 2009-04-22 04:34:53 +0000 | [diff] [blame] | 551 | selfTy = Context.getObjCInterfaceType(OID); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 552 | selfTy = Context.getObjCObjectPointerType(selfTy); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 553 | } else { |
| 554 | selfTy = Context.getObjCIdType(); |
| 555 | } |
| 556 | } else // we have a factory method. |
| 557 | selfTy = Context.getObjCClassType(); |
| 558 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 559 | bool selfIsPseudoStrong = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 560 | bool selfIsConsumed = false; |
| 561 | if (isInstanceMethod() && Context.getLangOptions().ObjCAutoRefCount) { |
| 562 | selfIsConsumed = hasAttr<NSConsumesSelfAttr>(); |
| 563 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 564 | // 'self' is always __strong. It's actually pseudo-strong except |
| 565 | // in init methods, though. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 566 | Qualifiers qs; |
| 567 | qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 568 | selfTy = Context.getQualifiedType(selfTy, qs); |
| 569 | |
| 570 | // In addition, 'self' is const unless this is an init method. |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 571 | if (getMethodFamily() != OMF_init) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 572 | selfTy = selfTy.withConst(); |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 573 | selfIsPseudoStrong = true; |
| 574 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | ImplicitParamDecl *self |
| 578 | = ImplicitParamDecl::Create(Context, this, SourceLocation(), |
| 579 | &Context.Idents.get("self"), selfTy); |
| 580 | setSelfDecl(self); |
| 581 | |
| 582 | if (selfIsConsumed) |
| 583 | self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context)); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 584 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 585 | if (selfIsPseudoStrong) |
| 586 | self->setARCPseudoStrong(true); |
| 587 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 588 | setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(), |
| 589 | &Context.Idents.get("_cmd"), |
Steve Naroff | 53c9d8a | 2009-04-20 15:06:07 +0000 | [diff] [blame] | 590 | Context.getObjCSelType())); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 593 | ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() { |
| 594 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext())) |
| 595 | return ID; |
| 596 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext())) |
| 597 | return CD->getClassInterface(); |
Argyrios Kyrtzidis | a853037 | 2009-07-28 05:10:52 +0000 | [diff] [blame] | 598 | if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext())) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 599 | return IMD->getClassInterface(); |
Argyrios Kyrtzidis | a853037 | 2009-07-28 05:10:52 +0000 | [diff] [blame] | 600 | |
| 601 | assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method"); |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 602 | llvm_unreachable("unknown method context"); |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 605 | //===----------------------------------------------------------------------===// |
| 606 | // ObjCInterfaceDecl |
| 607 | //===----------------------------------------------------------------------===// |
| 608 | |
| 609 | ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C, |
| 610 | DeclContext *DC, |
| 611 | SourceLocation atLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | IdentifierInfo *Id, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 613 | SourceLocation ClassLoc, |
| 614 | bool ForwardDecl, bool isInternal){ |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 615 | return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl, |
| 616 | isInternal); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | ObjCInterfaceDecl:: |
| 620 | ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id, |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 621 | SourceLocation CLoc, bool FD, bool isInternal) |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 622 | : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc), |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 623 | TypeForDecl(0), SuperClass(0), |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 624 | CategoryList(0), IvarList(0), |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 625 | ForwardDecl(FD), InternalInterface(isInternal), ExternallyCompleted(false) { |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Douglas Gregor | 26ac3f3 | 2010-12-01 23:49:52 +0000 | [diff] [blame] | 628 | void ObjCInterfaceDecl::LoadExternalDefinition() const { |
| 629 | assert(ExternallyCompleted && "Class is not externally completed"); |
| 630 | ExternallyCompleted = false; |
| 631 | getASTContext().getExternalSource()->CompleteType( |
| 632 | const_cast<ObjCInterfaceDecl *>(this)); |
| 633 | } |
| 634 | |
| 635 | void ObjCInterfaceDecl::setExternallyCompleted() { |
| 636 | assert(getASTContext().getExternalSource() && |
| 637 | "Class can't be externally completed without an external source"); |
| 638 | assert(!ForwardDecl && |
| 639 | "Forward declarations can't be externally completed"); |
| 640 | ExternallyCompleted = true; |
| 641 | } |
| 642 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 643 | ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const { |
Douglas Gregor | 26ac3f3 | 2010-12-01 23:49:52 +0000 | [diff] [blame] | 644 | if (ExternallyCompleted) |
| 645 | LoadExternalDefinition(); |
| 646 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 647 | return getASTContext().getObjCImplementation( |
| 648 | const_cast<ObjCInterfaceDecl*>(this)); |
| 649 | } |
| 650 | |
| 651 | void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) { |
| 652 | getASTContext().setObjCImplementation(this, ImplD); |
| 653 | } |
| 654 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 655 | /// all_declared_ivar_begin - return first ivar declared in this class, |
| 656 | /// its extensions and its implementation. Lazily build the list on first |
| 657 | /// access. |
| 658 | ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() { |
| 659 | if (IvarList) |
| 660 | return IvarList; |
| 661 | |
| 662 | ObjCIvarDecl *curIvar = 0; |
| 663 | if (!ivar_empty()) { |
| 664 | ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end(); |
| 665 | IvarList = (*I); ++I; |
| 666 | for (curIvar = IvarList; I != E; curIvar = *I, ++I) |
| 667 | curIvar->setNextIvar(*I); |
| 668 | } |
| 669 | |
| 670 | for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl; |
| 671 | CDecl = CDecl->getNextClassExtension()) { |
| 672 | if (!CDecl->ivar_empty()) { |
| 673 | ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(), |
| 674 | E = CDecl->ivar_end(); |
| 675 | if (!IvarList) { |
| 676 | IvarList = (*I); ++I; |
| 677 | curIvar = IvarList; |
| 678 | } |
| 679 | for ( ;I != E; curIvar = *I, ++I) |
| 680 | curIvar->setNextIvar(*I); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | if (ObjCImplementationDecl *ImplDecl = getImplementation()) { |
| 685 | if (!ImplDecl->ivar_empty()) { |
| 686 | ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(), |
| 687 | E = ImplDecl->ivar_end(); |
| 688 | if (!IvarList) { |
| 689 | IvarList = (*I); ++I; |
| 690 | curIvar = IvarList; |
| 691 | } |
| 692 | for ( ;I != E; curIvar = *I, ++I) |
| 693 | curIvar->setNextIvar(*I); |
| 694 | } |
| 695 | } |
| 696 | return IvarList; |
| 697 | } |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 698 | |
| 699 | /// FindCategoryDeclaration - Finds category declaration in the list of |
| 700 | /// categories for this class and returns it. Name of the category is passed |
| 701 | /// in 'CategoryId'. If category not found, return 0; |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 702 | /// |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 703 | ObjCCategoryDecl * |
| 704 | ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const { |
Douglas Gregor | 26ac3f3 | 2010-12-01 23:49:52 +0000 | [diff] [blame] | 705 | if (ExternallyCompleted) |
| 706 | LoadExternalDefinition(); |
| 707 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 708 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 709 | Category; Category = Category->getNextClassCategory()) |
| 710 | if (Category->getIdentifier() == CategoryId) |
| 711 | return Category; |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 712 | return 0; |
| 713 | } |
| 714 | |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 715 | ObjCMethodDecl * |
| 716 | ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const { |
| 717 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 718 | Category; Category = Category->getNextClassCategory()) |
| 719 | if (ObjCCategoryImplDecl *Impl = Category->getImplementation()) |
| 720 | if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel)) |
| 721 | return MD; |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const { |
| 726 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 727 | Category; Category = Category->getNextClassCategory()) |
| 728 | if (ObjCCategoryImplDecl *Impl = Category->getImplementation()) |
| 729 | if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel)) |
| 730 | return MD; |
| 731 | return 0; |
| 732 | } |
| 733 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 734 | /// ClassImplementsProtocol - Checks that 'lProto' protocol |
| 735 | /// has been implemented in IDecl class, its super class or categories (if |
| 736 | /// lookupCategory is true). |
| 737 | bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto, |
| 738 | bool lookupCategory, |
| 739 | bool RHSIsQualifiedID) { |
| 740 | ObjCInterfaceDecl *IDecl = this; |
| 741 | // 1st, look up the class. |
| 742 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 743 | IDecl->getReferencedProtocols(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 745 | for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(), |
| 746 | E = Protocols.end(); PI != E; ++PI) { |
| 747 | if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 748 | return true; |
| 749 | // This is dubious and is added to be compatible with gcc. In gcc, it is |
| 750 | // also allowed assigning a protocol-qualified 'id' type to a LHS object |
| 751 | // when protocol in qualified LHS is in list of protocols in the rhs 'id' |
| 752 | // object. This IMO, should be a bug. |
| 753 | // FIXME: Treat this as an extension, and flag this as an error when GCC |
| 754 | // extensions are not enabled. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | if (RHSIsQualifiedID && |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 756 | getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto)) |
| 757 | return true; |
| 758 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 759 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 760 | // 2nd, look up the category. |
| 761 | if (lookupCategory) |
| 762 | for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl; |
| 763 | CDecl = CDecl->getNextClassCategory()) { |
| 764 | for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(), |
| 765 | E = CDecl->protocol_end(); PI != E; ++PI) |
| 766 | if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 767 | return true; |
| 768 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 769 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 770 | // 3rd, look up the super class(s) |
| 771 | if (IDecl->getSuperClass()) |
| 772 | return |
| 773 | IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory, |
| 774 | RHSIsQualifiedID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 776 | return false; |
| 777 | } |
| 778 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 779 | //===----------------------------------------------------------------------===// |
| 780 | // ObjCIvarDecl |
| 781 | //===----------------------------------------------------------------------===// |
| 782 | |
Daniel Dunbar | a065492 | 2010-04-02 20:10:03 +0000 | [diff] [blame] | 783 | ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 784 | SourceLocation StartLoc, |
| 785 | SourceLocation IdLoc, IdentifierInfo *Id, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 786 | QualType T, TypeSourceInfo *TInfo, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 787 | AccessControl ac, Expr *BW, |
| 788 | bool synthesized) { |
Daniel Dunbar | a065492 | 2010-04-02 20:10:03 +0000 | [diff] [blame] | 789 | if (DC) { |
| 790 | // Ivar's can only appear in interfaces, implementations (via synthesized |
| 791 | // properties), and class extensions (via direct declaration, or synthesized |
| 792 | // properties). |
| 793 | // |
| 794 | // FIXME: This should really be asserting this: |
| 795 | // (isa<ObjCCategoryDecl>(DC) && |
| 796 | // cast<ObjCCategoryDecl>(DC)->IsClassExtension())) |
| 797 | // but unfortunately we sometimes place ivars into non-class extension |
| 798 | // categories on error. This breaks an AST invariant, and should not be |
| 799 | // fixed. |
| 800 | assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) || |
| 801 | isa<ObjCCategoryDecl>(DC)) && |
| 802 | "Invalid ivar decl context!"); |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 803 | // Once a new ivar is created in any of class/class-extension/implementation |
| 804 | // decl contexts, the previously built IvarList must be rebuilt. |
| 805 | ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC); |
| 806 | if (!ID) { |
Fariborz Jahanian | 000835d | 2010-08-23 18:51:39 +0000 | [diff] [blame] | 807 | if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 808 | ID = IM->getClassInterface(); |
Fariborz Jahanian | 000835d | 2010-08-23 18:51:39 +0000 | [diff] [blame] | 809 | if (BW) |
| 810 | IM->setHasSynthBitfield(true); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 811 | } else { |
Fariborz Jahanian | 000835d | 2010-08-23 18:51:39 +0000 | [diff] [blame] | 812 | ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC); |
| 813 | ID = CD->getClassInterface(); |
| 814 | if (BW) |
| 815 | CD->setHasSynthBitfield(true); |
| 816 | } |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 817 | } |
| 818 | ID->setIvarList(0); |
Daniel Dunbar | a065492 | 2010-04-02 20:10:03 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 821 | return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, |
| 822 | ac, BW, synthesized); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Daniel Dunbar | 27a961a | 2010-04-02 21:13:59 +0000 | [diff] [blame] | 825 | const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const { |
| 826 | const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext()); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 827 | |
Daniel Dunbar | 27a961a | 2010-04-02 21:13:59 +0000 | [diff] [blame] | 828 | switch (DC->getKind()) { |
| 829 | default: |
| 830 | case ObjCCategoryImpl: |
| 831 | case ObjCProtocol: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 832 | llvm_unreachable("invalid ivar container!"); |
Daniel Dunbar | 27a961a | 2010-04-02 21:13:59 +0000 | [diff] [blame] | 833 | |
| 834 | // Ivars can only appear in class extension categories. |
| 835 | case ObjCCategory: { |
| 836 | const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC); |
| 837 | assert(CD->IsClassExtension() && "invalid container for ivar!"); |
| 838 | return CD->getClassInterface(); |
| 839 | } |
| 840 | |
| 841 | case ObjCImplementation: |
| 842 | return cast<ObjCImplementationDecl>(DC)->getClassInterface(); |
| 843 | |
| 844 | case ObjCInterface: |
| 845 | return cast<ObjCInterfaceDecl>(DC); |
| 846 | } |
| 847 | } |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 848 | |
| 849 | //===----------------------------------------------------------------------===// |
| 850 | // ObjCAtDefsFieldDecl |
| 851 | //===----------------------------------------------------------------------===// |
| 852 | |
| 853 | ObjCAtDefsFieldDecl |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 854 | *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, |
| 855 | SourceLocation StartLoc, SourceLocation IdLoc, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 856 | IdentifierInfo *Id, QualType T, Expr *BW) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 857 | return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 860 | //===----------------------------------------------------------------------===// |
| 861 | // ObjCProtocolDecl |
| 862 | //===----------------------------------------------------------------------===// |
| 863 | |
| 864 | ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC, |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 865 | IdentifierInfo *Id, |
| 866 | SourceLocation nameLoc, |
| 867 | SourceLocation atStartLoc) { |
| 868 | return new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 871 | ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) { |
| 872 | ObjCProtocolDecl *PDecl = this; |
| 873 | |
| 874 | if (Name == getIdentifier()) |
| 875 | return PDecl; |
| 876 | |
| 877 | for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I) |
| 878 | if ((PDecl = (*I)->lookupProtocolNamed(Name))) |
| 879 | return PDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 880 | |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 881 | return NULL; |
| 882 | } |
| 883 | |
Argyrios Kyrtzidis | 094e2bb | 2009-07-25 22:15:38 +0000 | [diff] [blame] | 884 | // lookupMethod - Lookup a instance/class method in the protocol and protocols |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 885 | // it inherited. |
Argyrios Kyrtzidis | 094e2bb | 2009-07-25 22:15:38 +0000 | [diff] [blame] | 886 | ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel, |
| 887 | bool isInstance) const { |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 888 | ObjCMethodDecl *MethodDecl = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 889 | |
Argyrios Kyrtzidis | 094e2bb | 2009-07-25 22:15:38 +0000 | [diff] [blame] | 890 | if ((MethodDecl = getMethod(Sel, isInstance))) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 891 | return MethodDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 893 | for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I) |
Argyrios Kyrtzidis | 094e2bb | 2009-07-25 22:15:38 +0000 | [diff] [blame] | 894 | if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance))) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 895 | return MethodDecl; |
| 896 | return NULL; |
| 897 | } |
| 898 | |
| 899 | //===----------------------------------------------------------------------===// |
| 900 | // ObjCClassDecl |
| 901 | //===----------------------------------------------------------------------===// |
| 902 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L, |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 904 | ObjCInterfaceDecl *const Elt, |
| 905 | const SourceLocation Loc, |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 906 | ASTContext &C) |
| 907 | : Decl(ObjCClass, DC, L) { |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 908 | setClass(C, Elt, Loc); |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 909 | } |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 910 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 911 | ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC, |
| 912 | SourceLocation L, |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 913 | ObjCInterfaceDecl *const Elt, |
| 914 | const SourceLocation Loc) { |
| 915 | return new (C) ObjCClassDecl(DC, L, Elt, Loc, C); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 918 | void ObjCClassDecl::setClass(ASTContext &C, ObjCInterfaceDecl*const Cls, |
| 919 | const SourceLocation Loc) { |
| 920 | |
| 921 | ForwardDecl = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef), |
| 922 | llvm::alignOf<ObjCClassRef>()); |
| 923 | new (ForwardDecl) ObjCClassRef(Cls, Loc); |
| 924 | } |
| 925 | |
Ted Kremenek | 2dbdd62 | 2009-11-18 01:26:56 +0000 | [diff] [blame] | 926 | SourceRange ObjCClassDecl::getSourceRange() const { |
| 927 | // FIXME: We should include the semicolon |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 928 | return SourceRange(getLocation(), ForwardDecl->getLocation()); |
Ted Kremenek | 2dbdd62 | 2009-11-18 01:26:56 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 931 | //===----------------------------------------------------------------------===// |
| 932 | // ObjCForwardProtocolDecl |
| 933 | //===----------------------------------------------------------------------===// |
| 934 | |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 935 | ObjCForwardProtocolDecl:: |
| 936 | ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L, |
| 937 | ObjCProtocolDecl *const *Elts, unsigned nElts, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 938 | const SourceLocation *Locs, ASTContext &C) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | : Decl(ObjCForwardProtocol, DC, L) { |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 940 | ReferencedProtocols.set(Elts, nElts, Locs, C); |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 944 | ObjCForwardProtocolDecl * |
| 945 | ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | SourceLocation L, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 947 | ObjCProtocolDecl *const *Elts, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 948 | unsigned NumElts, |
| 949 | const SourceLocation *Locs) { |
| 950 | return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 953 | //===----------------------------------------------------------------------===// |
| 954 | // ObjCCategoryDecl |
| 955 | //===----------------------------------------------------------------------===// |
| 956 | |
| 957 | ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 3db211b | 2010-01-16 16:38:58 +0000 | [diff] [blame] | 958 | SourceLocation AtLoc, |
| 959 | SourceLocation ClassNameLoc, |
| 960 | SourceLocation CategoryNameLoc, |
Argyrios Kyrtzidis | 955fadb | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 961 | IdentifierInfo *Id, |
| 962 | ObjCInterfaceDecl *IDecl) { |
| 963 | ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, |
| 964 | CategoryNameLoc, Id, |
| 965 | IDecl); |
| 966 | if (IDecl) { |
| 967 | // Link this category into its class's category list. |
| 968 | CatDecl->NextClassCategory = IDecl->getCategoryList(); |
| 969 | IDecl->setCategoryList(CatDecl); |
Argyrios Kyrtzidis | e6b8d68 | 2011-09-01 00:58:55 +0000 | [diff] [blame] | 970 | if (ASTMutationListener *L = C.getASTMutationListener()) |
| 971 | L->AddedObjCCategoryToInterface(CatDecl, IDecl); |
Argyrios Kyrtzidis | 955fadb | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | return CatDecl; |
| 975 | } |
| 976 | |
| 977 | ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, EmptyShell Empty) { |
| 978 | return new (C) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(), |
| 979 | SourceLocation(), 0, 0); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 982 | ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const { |
| 983 | return getASTContext().getObjCImplementation( |
| 984 | const_cast<ObjCCategoryDecl*>(this)); |
| 985 | } |
| 986 | |
| 987 | void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) { |
| 988 | getASTContext().setObjCImplementation(this, ImplD); |
| 989 | } |
| 990 | |
| 991 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 992 | //===----------------------------------------------------------------------===// |
| 993 | // ObjCCategoryImplDecl |
| 994 | //===----------------------------------------------------------------------===// |
| 995 | |
| 996 | ObjCCategoryImplDecl * |
| 997 | ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC, |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 998 | IdentifierInfo *Id, |
| 999 | ObjCInterfaceDecl *ClassInterface, |
| 1000 | SourceLocation nameLoc, |
| 1001 | SourceLocation atStartLoc) { |
| 1002 | return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface, |
| 1003 | nameLoc, atStartLoc); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Steve Naroff | 0d69b8c | 2009-10-29 21:11:04 +0000 | [diff] [blame] | 1006 | ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const { |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 1007 | // The class interface might be NULL if we are working with invalid code. |
| 1008 | if (const ObjCInterfaceDecl *ID = getClassInterface()) |
| 1009 | return ID->FindCategoryDeclaration(getIdentifier()); |
| 1010 | return 0; |
Argyrios Kyrtzidis | 4292073 | 2009-07-28 05:11:05 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1013 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1014 | void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) { |
Douglas Gregor | 2c2d43c | 2009-04-23 02:42:49 +0000 | [diff] [blame] | 1015 | // FIXME: The context should be correct before we get here. |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1016 | property->setLexicalDeclContext(this); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1017 | addDecl(property); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1020 | void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) { |
| 1021 | ASTContext &Ctx = getASTContext(); |
| 1022 | |
| 1023 | if (ObjCImplementationDecl *ImplD |
Duncan Sands | 98f2cca | 2009-07-21 07:56:29 +0000 | [diff] [blame] | 1024 | = dyn_cast_or_null<ObjCImplementationDecl>(this)) { |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1025 | if (IFace) |
| 1026 | Ctx.setObjCImplementation(IFace, ImplD); |
| 1027 | |
Duncan Sands | 98f2cca | 2009-07-21 07:56:29 +0000 | [diff] [blame] | 1028 | } else if (ObjCCategoryImplDecl *ImplD = |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1029 | dyn_cast_or_null<ObjCCategoryImplDecl>(this)) { |
| 1030 | if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier())) |
| 1031 | Ctx.setObjCImplementation(CD, ImplD); |
| 1032 | } |
| 1033 | |
| 1034 | ClassInterface = IFace; |
| 1035 | } |
| 1036 | |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 1037 | /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of |
Chris Lattner | d6eed1c | 2009-02-16 19:24:31 +0000 | [diff] [blame] | 1038 | /// properties implemented in this category @implementation block and returns |
| 1039 | /// the implemented property that uses it. |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 1040 | /// |
Chris Lattner | 3aa1861 | 2009-02-28 18:42:10 +0000 | [diff] [blame] | 1041 | ObjCPropertyImplDecl *ObjCImplDecl:: |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1042 | FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const { |
| 1043 | for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 1044 | ObjCPropertyImplDecl *PID = *i; |
| 1045 | if (PID->getPropertyIvarDecl() && |
| 1046 | PID->getPropertyIvarDecl()->getIdentifier() == ivarId) |
| 1047 | return PID; |
| 1048 | } |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl |
| 1053 | /// added to the list of those properties @synthesized/@dynamic in this |
| 1054 | /// category @implementation block. |
| 1055 | /// |
Chris Lattner | 3aa1861 | 2009-02-28 18:42:10 +0000 | [diff] [blame] | 1056 | ObjCPropertyImplDecl *ObjCImplDecl:: |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1057 | FindPropertyImplDecl(IdentifierInfo *Id) const { |
| 1058 | for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 1059 | ObjCPropertyImplDecl *PID = *i; |
| 1060 | if (PID->getPropertyDecl()->getIdentifier() == Id) |
| 1061 | return PID; |
| 1062 | } |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1066 | raw_ostream &clang::operator<<(raw_ostream &OS, |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 1067 | const ObjCCategoryImplDecl *CID) { |
| 1068 | OS << CID->getName(); |
| 1069 | return OS; |
| 1070 | } |
| 1071 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1072 | //===----------------------------------------------------------------------===// |
| 1073 | // ObjCImplementationDecl |
| 1074 | //===----------------------------------------------------------------------===// |
| 1075 | |
| 1076 | ObjCImplementationDecl * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1078 | ObjCInterfaceDecl *ClassInterface, |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 1079 | ObjCInterfaceDecl *SuperDecl, |
| 1080 | SourceLocation nameLoc, |
| 1081 | SourceLocation atStartLoc) { |
| 1082 | return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl, |
| 1083 | nameLoc, atStartLoc); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
John McCall | da6d976 | 2011-07-22 04:15:06 +0000 | [diff] [blame] | 1086 | void ObjCImplementationDecl::setIvarInitializers(ASTContext &C, |
| 1087 | CXXCtorInitializer ** initializers, |
| 1088 | unsigned numInitializers) { |
| 1089 | if (numInitializers > 0) { |
| 1090 | NumIvarInitializers = numInitializers; |
| 1091 | CXXCtorInitializer **ivarInitializers = |
| 1092 | new (C) CXXCtorInitializer*[NumIvarInitializers]; |
| 1093 | memcpy(ivarInitializers, initializers, |
| 1094 | numInitializers * sizeof(CXXCtorInitializer*)); |
| 1095 | IvarInitializers = ivarInitializers; |
| 1096 | } |
| 1097 | } |
| 1098 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1099 | raw_ostream &clang::operator<<(raw_ostream &OS, |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 1100 | const ObjCImplementationDecl *ID) { |
| 1101 | OS << ID->getName(); |
| 1102 | return OS; |
| 1103 | } |
| 1104 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1105 | //===----------------------------------------------------------------------===// |
| 1106 | // ObjCCompatibleAliasDecl |
| 1107 | //===----------------------------------------------------------------------===// |
| 1108 | |
| 1109 | ObjCCompatibleAliasDecl * |
| 1110 | ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 1111 | SourceLocation L, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1112 | IdentifierInfo *Id, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1113 | ObjCInterfaceDecl* AliasedClass) { |
| 1114 | return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass); |
| 1115 | } |
| 1116 | |
| 1117 | //===----------------------------------------------------------------------===// |
| 1118 | // ObjCPropertyDecl |
| 1119 | //===----------------------------------------------------------------------===// |
| 1120 | |
| 1121 | ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC, |
| 1122 | SourceLocation L, |
| 1123 | IdentifierInfo *Id, |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 1124 | SourceLocation AtLoc, |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 1125 | TypeSourceInfo *T, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1126 | PropertyControl propControl) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 1127 | return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1130 | //===----------------------------------------------------------------------===// |
| 1131 | // ObjCPropertyImplDecl |
| 1132 | //===----------------------------------------------------------------------===// |
| 1133 | |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1134 | ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C, |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1135 | DeclContext *DC, |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1136 | SourceLocation atLoc, |
| 1137 | SourceLocation L, |
| 1138 | ObjCPropertyDecl *property, |
Daniel Dunbar | 9f0afd4 | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 1139 | Kind PK, |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1140 | ObjCIvarDecl *ivar, |
| 1141 | SourceLocation ivarLoc) { |
| 1142 | return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar, |
| 1143 | ivarLoc); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1144 | } |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 1145 | |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1146 | SourceRange ObjCPropertyImplDecl::getSourceRange() const { |
| 1147 | SourceLocation EndLoc = getLocation(); |
| 1148 | if (IvarLoc.isValid()) |
| 1149 | EndLoc = IvarLoc; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1150 | |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1151 | return SourceRange(AtLoc, EndLoc); |
| 1152 | } |