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