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