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