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