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