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