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