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