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