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