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