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