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