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