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