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