| File: | src/gnu/usr.bin/clang/libclangAST/../../../llvm/clang/lib/AST/DeclObjC.cpp |
| Warning: | line 1687, column 11 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | //===- DeclObjC.cpp - ObjC Declaration AST Node Implementation ------------===// | |||
| 2 | // | |||
| 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 | |||
| 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" | |||
| 15 | #include "clang/AST/ASTMutationListener.h" | |||
| 16 | #include "clang/AST/Attr.h" | |||
| 17 | #include "clang/AST/Decl.h" | |||
| 18 | #include "clang/AST/DeclBase.h" | |||
| 19 | #include "clang/AST/Stmt.h" | |||
| 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" | |||
| 27 | #include "llvm/ADT/SmallString.h" | |||
| 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 <queue> | |||
| 37 | #include <utility> | |||
| 38 | ||||
| 39 | using namespace clang; | |||
| 40 | ||||
| 41 | //===----------------------------------------------------------------------===// | |||
| 42 | // ObjCListBase | |||
| 43 | //===----------------------------------------------------------------------===// | |||
| 44 | ||||
| 45 | void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) { | |||
| 46 | List = nullptr; | |||
| 47 | if (Elts == 0) return; // Setting to an empty list is a noop. | |||
| 48 | ||||
| 49 | List = new (Ctx) void*[Elts]; | |||
| 50 | NumElts = Elts; | |||
| 51 | memcpy(List, InList, sizeof(void*)*Elts); | |||
| 52 | } | |||
| 53 | ||||
| 54 | void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts, | |||
| 55 | const SourceLocation *Locs, ASTContext &Ctx) { | |||
| 56 | if (Elts == 0) | |||
| 57 | return; | |||
| 58 | ||||
| 59 | Locations = new (Ctx) SourceLocation[Elts]; | |||
| 60 | memcpy(Locations, Locs, sizeof(SourceLocation) * Elts); | |||
| 61 | set(InList, Elts, Ctx); | |||
| 62 | } | |||
| 63 | ||||
| 64 | //===----------------------------------------------------------------------===// | |||
| 65 | // ObjCInterfaceDecl | |||
| 66 | //===----------------------------------------------------------------------===// | |||
| 67 | ||||
| 68 | ObjCContainerDecl::ObjCContainerDecl(Kind DK, DeclContext *DC, | |||
| 69 | IdentifierInfo *Id, SourceLocation nameLoc, | |||
| 70 | SourceLocation atStartLoc) | |||
| 71 | : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK) { | |||
| 72 | setAtStartLoc(atStartLoc); | |||
| 73 | } | |||
| 74 | ||||
| 75 | void ObjCContainerDecl::anchor() {} | |||
| 76 | ||||
| 77 | /// getIvarDecl - This method looks up an ivar in this ContextDecl. | |||
| 78 | /// | |||
| 79 | ObjCIvarDecl * | |||
| 80 | ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const { | |||
| 81 | lookup_result R = lookup(Id); | |||
| 82 | for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end(); | |||
| 83 | Ivar != IvarEnd; ++Ivar) { | |||
| 84 | if (auto *ivar = dyn_cast<ObjCIvarDecl>(*Ivar)) | |||
| 85 | return ivar; | |||
| 86 | } | |||
| 87 | return nullptr; | |||
| 88 | } | |||
| 89 | ||||
| 90 | // Get the local instance/class method declared in this interface. | |||
| 91 | ObjCMethodDecl * | |||
| 92 | ObjCContainerDecl::getMethod(Selector Sel, bool isInstance, | |||
| 93 | bool AllowHidden) const { | |||
| 94 | // If this context is a hidden protocol definition, don't find any | |||
| 95 | // methods there. | |||
| 96 | if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) { | |||
| 97 | if (const ObjCProtocolDecl *Def = Proto->getDefinition()) | |||
| 98 | if (!Def->isUnconditionallyVisible() && !AllowHidden) | |||
| 99 | return nullptr; | |||
| 100 | } | |||
| 101 | ||||
| 102 | // Since instance & class methods can have the same name, the loop below | |||
| 103 | // ensures we get the correct method. | |||
| 104 | // | |||
| 105 | // @interface Whatever | |||
| 106 | // - (int) class_method; | |||
| 107 | // + (float) class_method; | |||
| 108 | // @end | |||
| 109 | lookup_result R = lookup(Sel); | |||
| 110 | for (lookup_iterator Meth = R.begin(), MethEnd = R.end(); | |||
| 111 | Meth != MethEnd; ++Meth) { | |||
| 112 | auto *MD = dyn_cast<ObjCMethodDecl>(*Meth); | |||
| 113 | if (MD && MD->isInstanceMethod() == isInstance) | |||
| 114 | return MD; | |||
| 115 | } | |||
| 116 | return nullptr; | |||
| 117 | } | |||
| 118 | ||||
| 119 | /// This routine returns 'true' if a user declared setter method was | |||
| 120 | /// found in the class, its protocols, its super classes or categories. | |||
| 121 | /// It also returns 'true' if one of its categories has declared a 'readwrite' | |||
| 122 | /// property. This is because, user must provide a setter method for the | |||
| 123 | /// category's 'readwrite' property. | |||
| 124 | bool ObjCContainerDecl::HasUserDeclaredSetterMethod( | |||
| 125 | const ObjCPropertyDecl *Property) const { | |||
| 126 | Selector Sel = Property->getSetterName(); | |||
| 127 | lookup_result R = lookup(Sel); | |||
| 128 | for (lookup_iterator Meth = R.begin(), MethEnd = R.end(); | |||
| 129 | Meth != MethEnd; ++Meth) { | |||
| 130 | auto *MD = dyn_cast<ObjCMethodDecl>(*Meth); | |||
| 131 | if (MD && MD->isInstanceMethod() && !MD->isImplicit()) | |||
| 132 | return true; | |||
| 133 | } | |||
| 134 | ||||
| 135 | if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(this)) { | |||
| 136 | // Also look into categories, including class extensions, looking | |||
| 137 | // for a user declared instance method. | |||
| 138 | for (const auto *Cat : ID->visible_categories()) { | |||
| 139 | if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel)) | |||
| 140 | if (!MD->isImplicit()) | |||
| 141 | return true; | |||
| 142 | if (Cat->IsClassExtension()) | |||
| 143 | continue; | |||
| 144 | // Also search through the categories looking for a 'readwrite' | |||
| 145 | // declaration of this property. If one found, presumably a setter will | |||
| 146 | // be provided (properties declared in categories will not get | |||
| 147 | // auto-synthesized). | |||
| 148 | for (const auto *P : Cat->properties()) | |||
| 149 | if (P->getIdentifier() == Property->getIdentifier()) { | |||
| 150 | if (P->getPropertyAttributes() & | |||
| 151 | ObjCPropertyAttribute::kind_readwrite) | |||
| 152 | return true; | |||
| 153 | break; | |||
| 154 | } | |||
| 155 | } | |||
| 156 | ||||
| 157 | // Also look into protocols, for a user declared instance method. | |||
| 158 | for (const auto *Proto : ID->all_referenced_protocols()) | |||
| 159 | if (Proto->HasUserDeclaredSetterMethod(Property)) | |||
| 160 | return true; | |||
| 161 | ||||
| 162 | // And in its super class. | |||
| 163 | ObjCInterfaceDecl *OSC = ID->getSuperClass(); | |||
| 164 | while (OSC) { | |||
| 165 | if (OSC->HasUserDeclaredSetterMethod(Property)) | |||
| 166 | return true; | |||
| 167 | OSC = OSC->getSuperClass(); | |||
| 168 | } | |||
| 169 | } | |||
| 170 | if (const auto *PD = dyn_cast<ObjCProtocolDecl>(this)) | |||
| 171 | for (const auto *PI : PD->protocols()) | |||
| 172 | if (PI->HasUserDeclaredSetterMethod(Property)) | |||
| 173 | return true; | |||
| 174 | return false; | |||
| 175 | } | |||
| 176 | ||||
| 177 | ObjCPropertyDecl * | |||
| 178 | ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC, | |||
| 179 | const IdentifierInfo *propertyID, | |||
| 180 | ObjCPropertyQueryKind queryKind) { | |||
| 181 | // If this context is a hidden protocol definition, don't find any | |||
| 182 | // property. | |||
| 183 | if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(DC)) { | |||
| 184 | if (const ObjCProtocolDecl *Def = Proto->getDefinition()) | |||
| 185 | if (!Def->isUnconditionallyVisible()) | |||
| 186 | return nullptr; | |||
| 187 | } | |||
| 188 | ||||
| 189 | // If context is class, then lookup property in its visible extensions. | |||
| 190 | // This comes before property is looked up in primary class. | |||
| 191 | if (auto *IDecl = dyn_cast<ObjCInterfaceDecl>(DC)) { | |||
| 192 | for (const auto *Ext : IDecl->visible_extensions()) | |||
| 193 | if (ObjCPropertyDecl *PD = ObjCPropertyDecl::findPropertyDecl(Ext, | |||
| 194 | propertyID, | |||
| 195 | queryKind)) | |||
| 196 | return PD; | |||
| 197 | } | |||
| 198 | ||||
| 199 | DeclContext::lookup_result R = DC->lookup(propertyID); | |||
| 200 | ObjCPropertyDecl *classProp = nullptr; | |||
| 201 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; | |||
| 202 | ++I) | |||
| 203 | if (auto *PD = dyn_cast<ObjCPropertyDecl>(*I)) { | |||
| 204 | // If queryKind is unknown, we return the instance property if one | |||
| 205 | // exists; otherwise we return the class property. | |||
| 206 | if ((queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown && | |||
| 207 | !PD->isClassProperty()) || | |||
| 208 | (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_class && | |||
| 209 | PD->isClassProperty()) || | |||
| 210 | (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance && | |||
| 211 | !PD->isClassProperty())) | |||
| 212 | return PD; | |||
| 213 | ||||
| 214 | if (PD->isClassProperty()) | |||
| 215 | classProp = PD; | |||
| 216 | } | |||
| 217 | ||||
| 218 | if (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown) | |||
| 219 | // We can't find the instance property, return the class property. | |||
| 220 | return classProp; | |||
| 221 | ||||
| 222 | return nullptr; | |||
| 223 | } | |||
| 224 | ||||
| 225 | IdentifierInfo * | |||
| 226 | ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const { | |||
| 227 | SmallString<128> ivarName; | |||
| 228 | { | |||
| 229 | llvm::raw_svector_ostream os(ivarName); | |||
| 230 | os << '_' << getIdentifier()->getName(); | |||
| 231 | } | |||
| 232 | return &Ctx.Idents.get(ivarName.str()); | |||
| 233 | } | |||
| 234 | ||||
| 235 | /// FindPropertyDeclaration - Finds declaration of the property given its name | |||
| 236 | /// in 'PropertyId' and returns it. It returns 0, if not found. | |||
| 237 | ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration( | |||
| 238 | const IdentifierInfo *PropertyId, | |||
| 239 | ObjCPropertyQueryKind QueryKind) const { | |||
| 240 | // Don't find properties within hidden protocol definitions. | |||
| 241 | if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) { | |||
| 242 | if (const ObjCProtocolDecl *Def = Proto->getDefinition()) | |||
| 243 | if (!Def->isUnconditionallyVisible()) | |||
| 244 | return nullptr; | |||
| 245 | } | |||
| 246 | ||||
| 247 | // Search the extensions of a class first; they override what's in | |||
| 248 | // the class itself. | |||
| 249 | if (const auto *ClassDecl = dyn_cast<ObjCInterfaceDecl>(this)) { | |||
| 250 | for (const auto *Ext : ClassDecl->visible_extensions()) { | |||
| 251 | if (auto *P = Ext->FindPropertyDeclaration(PropertyId, QueryKind)) | |||
| 252 | return P; | |||
| 253 | } | |||
| 254 | } | |||
| 255 | ||||
| 256 | if (ObjCPropertyDecl *PD = | |||
| 257 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId, | |||
| 258 | QueryKind)) | |||
| 259 | return PD; | |||
| 260 | ||||
| 261 | switch (getKind()) { | |||
| 262 | default: | |||
| 263 | break; | |||
| 264 | case Decl::ObjCProtocol: { | |||
| 265 | const auto *PID = cast<ObjCProtocolDecl>(this); | |||
| 266 | for (const auto *I : PID->protocols()) | |||
| 267 | if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId, | |||
| 268 | QueryKind)) | |||
| 269 | return P; | |||
| 270 | break; | |||
| 271 | } | |||
| 272 | case Decl::ObjCInterface: { | |||
| 273 | const auto *OID = cast<ObjCInterfaceDecl>(this); | |||
| 274 | // Look through categories (but not extensions; they were handled above). | |||
| 275 | for (const auto *Cat : OID->visible_categories()) { | |||
| 276 | if (!Cat->IsClassExtension()) | |||
| 277 | if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration( | |||
| 278 | PropertyId, QueryKind)) | |||
| 279 | return P; | |||
| 280 | } | |||
| 281 | ||||
| 282 | // Look through protocols. | |||
| 283 | for (const auto *I : OID->all_referenced_protocols()) | |||
| 284 | if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId, | |||
| 285 | QueryKind)) | |||
| 286 | return P; | |||
| 287 | ||||
| 288 | // Finally, check the super class. | |||
| 289 | if (const ObjCInterfaceDecl *superClass = OID->getSuperClass()) | |||
| 290 | return superClass->FindPropertyDeclaration(PropertyId, QueryKind); | |||
| 291 | break; | |||
| 292 | } | |||
| 293 | case Decl::ObjCCategory: { | |||
| 294 | const auto *OCD = cast<ObjCCategoryDecl>(this); | |||
| 295 | // Look through protocols. | |||
| 296 | if (!OCD->IsClassExtension()) | |||
| 297 | for (const auto *I : OCD->protocols()) | |||
| 298 | if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId, | |||
| 299 | QueryKind)) | |||
| 300 | return P; | |||
| 301 | break; | |||
| 302 | } | |||
| 303 | } | |||
| 304 | return nullptr; | |||
| 305 | } | |||
| 306 | ||||
| 307 | void ObjCInterfaceDecl::anchor() {} | |||
| 308 | ||||
| 309 | ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const { | |||
| 310 | // If this particular declaration has a type parameter list, return it. | |||
| 311 | if (ObjCTypeParamList *written = getTypeParamListAsWritten()) | |||
| 312 | return written; | |||
| 313 | ||||
| 314 | // If there is a definition, return its type parameter list. | |||
| 315 | if (const ObjCInterfaceDecl *def = getDefinition()) | |||
| 316 | return def->getTypeParamListAsWritten(); | |||
| 317 | ||||
| 318 | // Otherwise, look at previous declarations to determine whether any | |||
| 319 | // of them has a type parameter list, skipping over those | |||
| 320 | // declarations that do not. | |||
| 321 | for (const ObjCInterfaceDecl *decl = getMostRecentDecl(); decl; | |||
| 322 | decl = decl->getPreviousDecl()) { | |||
| 323 | if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten()) | |||
| 324 | return written; | |||
| 325 | } | |||
| 326 | ||||
| 327 | return nullptr; | |||
| 328 | } | |||
| 329 | ||||
| 330 | void ObjCInterfaceDecl::setTypeParamList(ObjCTypeParamList *TPL) { | |||
| 331 | TypeParamList = TPL; | |||
| 332 | if (!TPL) | |||
| 333 | return; | |||
| 334 | // Set the declaration context of each of the type parameters. | |||
| 335 | for (auto *typeParam : *TypeParamList) | |||
| 336 | typeParam->setDeclContext(this); | |||
| 337 | } | |||
| 338 | ||||
| 339 | ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const { | |||
| 340 | // FIXME: Should make sure no callers ever do this. | |||
| 341 | if (!hasDefinition()) | |||
| 342 | return nullptr; | |||
| 343 | ||||
| 344 | if (data().ExternallyCompleted) | |||
| 345 | LoadExternalDefinition(); | |||
| 346 | ||||
| 347 | if (const ObjCObjectType *superType = getSuperClassType()) { | |||
| 348 | if (ObjCInterfaceDecl *superDecl = superType->getInterface()) { | |||
| 349 | if (ObjCInterfaceDecl *superDef = superDecl->getDefinition()) | |||
| 350 | return superDef; | |||
| 351 | ||||
| 352 | return superDecl; | |||
| 353 | } | |||
| 354 | } | |||
| 355 | ||||
| 356 | return nullptr; | |||
| 357 | } | |||
| 358 | ||||
| 359 | SourceLocation ObjCInterfaceDecl::getSuperClassLoc() const { | |||
| 360 | if (TypeSourceInfo *superTInfo = getSuperClassTInfo()) | |||
| 361 | return superTInfo->getTypeLoc().getBeginLoc(); | |||
| 362 | ||||
| 363 | return SourceLocation(); | |||
| 364 | } | |||
| 365 | ||||
| 366 | /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property | |||
| 367 | /// with name 'PropertyId' in the primary class; including those in protocols | |||
| 368 | /// (direct or indirect) used by the primary class. | |||
| 369 | ObjCPropertyDecl * | |||
| 370 | ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass( | |||
| 371 | IdentifierInfo *PropertyId, | |||
| 372 | ObjCPropertyQueryKind QueryKind) const { | |||
| 373 | // FIXME: Should make sure no callers ever do this. | |||
| 374 | if (!hasDefinition()) | |||
| 375 | return nullptr; | |||
| 376 | ||||
| 377 | if (data().ExternallyCompleted) | |||
| 378 | LoadExternalDefinition(); | |||
| 379 | ||||
| 380 | if (ObjCPropertyDecl *PD = | |||
| 381 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId, | |||
| 382 | QueryKind)) | |||
| 383 | return PD; | |||
| 384 | ||||
| 385 | // Look through protocols. | |||
| 386 | for (const auto *I : all_referenced_protocols()) | |||
| 387 | if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId, | |||
| 388 | QueryKind)) | |||
| 389 | return P; | |||
| 390 | ||||
| 391 | return nullptr; | |||
| 392 | } | |||
| 393 | ||||
| 394 | void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM, | |||
| 395 | PropertyDeclOrder &PO) const { | |||
| 396 | for (auto *Prop : properties()) { | |||
| 397 | PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop; | |||
| 398 | PO.push_back(Prop); | |||
| 399 | } | |||
| 400 | for (const auto *Ext : known_extensions()) { | |||
| 401 | const ObjCCategoryDecl *ClassExt = Ext; | |||
| 402 | for (auto *Prop : ClassExt->properties()) { | |||
| 403 | PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop; | |||
| 404 | PO.push_back(Prop); | |||
| 405 | } | |||
| 406 | } | |||
| 407 | for (const auto *PI : all_referenced_protocols()) | |||
| 408 | PI->collectPropertiesToImplement(PM, PO); | |||
| 409 | // Note, the properties declared only in class extensions are still copied | |||
| 410 | // into the main @interface's property list, and therefore we don't | |||
| 411 | // explicitly, have to search class extension properties. | |||
| 412 | } | |||
| 413 | ||||
| 414 | bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const { | |||
| 415 | const ObjCInterfaceDecl *Class = this; | |||
| 416 | while (Class) { | |||
| 417 | if (Class->hasAttr<ArcWeakrefUnavailableAttr>()) | |||
| 418 | return true; | |||
| 419 | Class = Class->getSuperClass(); | |||
| 420 | } | |||
| 421 | return false; | |||
| 422 | } | |||
| 423 | ||||
| 424 | const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const { | |||
| 425 | const ObjCInterfaceDecl *Class = this; | |||
| 426 | while (Class) { | |||
| 427 | if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>()) | |||
| 428 | return Class; | |||
| 429 | Class = Class->getSuperClass(); | |||
| 430 | } | |||
| 431 | return nullptr; | |||
| 432 | } | |||
| 433 | ||||
| 434 | void ObjCInterfaceDecl::mergeClassExtensionProtocolList( | |||
| 435 | ObjCProtocolDecl *const* ExtList, unsigned ExtNum, | |||
| 436 | ASTContext &C) { | |||
| 437 | if (data().ExternallyCompleted) | |||
| 438 | LoadExternalDefinition(); | |||
| 439 | ||||
| 440 | if (data().AllReferencedProtocols.empty() && | |||
| 441 | data().ReferencedProtocols.empty()) { | |||
| 442 | data().AllReferencedProtocols.set(ExtList, ExtNum, C); | |||
| 443 | return; | |||
| 444 | } | |||
| 445 | ||||
| 446 | // Check for duplicate protocol in class's protocol list. | |||
| 447 | // This is O(n*m). But it is extremely rare and number of protocols in | |||
| 448 | // class or its extension are very few. | |||
| 449 | SmallVector<ObjCProtocolDecl *, 8> ProtocolRefs; | |||
| 450 | for (unsigned i = 0; i < ExtNum; i++) { | |||
| 451 | bool protocolExists = false; | |||
| 452 | ObjCProtocolDecl *ProtoInExtension = ExtList[i]; | |||
| 453 | for (auto *Proto : all_referenced_protocols()) { | |||
| 454 | if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) { | |||
| 455 | protocolExists = true; | |||
| 456 | break; | |||
| 457 | } | |||
| 458 | } | |||
| 459 | // Do we want to warn on a protocol in extension class which | |||
| 460 | // already exist in the class? Probably not. | |||
| 461 | if (!protocolExists) | |||
| 462 | ProtocolRefs.push_back(ProtoInExtension); | |||
| 463 | } | |||
| 464 | ||||
| 465 | if (ProtocolRefs.empty()) | |||
| 466 | return; | |||
| 467 | ||||
| 468 | // Merge ProtocolRefs into class's protocol list; | |||
| 469 | ProtocolRefs.append(all_referenced_protocol_begin(), | |||
| 470 | all_referenced_protocol_end()); | |||
| 471 | ||||
| 472 | data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C); | |||
| 473 | } | |||
| 474 | ||||
| 475 | const ObjCInterfaceDecl * | |||
| 476 | ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const { | |||
| 477 | const ObjCInterfaceDecl *IFace = this; | |||
| 478 | while (IFace) { | |||
| 479 | if (IFace->hasDesignatedInitializers()) | |||
| 480 | return IFace; | |||
| 481 | if (!IFace->inheritsDesignatedInitializers()) | |||
| 482 | break; | |||
| 483 | IFace = IFace->getSuperClass(); | |||
| 484 | } | |||
| 485 | return nullptr; | |||
| 486 | } | |||
| 487 | ||||
| 488 | static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) { | |||
| 489 | for (const auto *MD : D->instance_methods()) { | |||
| 490 | if (MD->getMethodFamily() == OMF_init && !MD->isOverriding()) | |||
| 491 | return true; | |||
| 492 | } | |||
| 493 | for (const auto *Ext : D->visible_extensions()) { | |||
| 494 | for (const auto *MD : Ext->instance_methods()) { | |||
| 495 | if (MD->getMethodFamily() == OMF_init && !MD->isOverriding()) | |||
| 496 | return true; | |||
| 497 | } | |||
| 498 | } | |||
| 499 | if (const auto *ImplD = D->getImplementation()) { | |||
| 500 | for (const auto *MD : ImplD->instance_methods()) { | |||
| 501 | if (MD->getMethodFamily() == OMF_init && !MD->isOverriding()) | |||
| 502 | return true; | |||
| 503 | } | |||
| 504 | } | |||
| 505 | return false; | |||
| 506 | } | |||
| 507 | ||||
| 508 | bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const { | |||
| 509 | switch (data().InheritedDesignatedInitializers) { | |||
| 510 | case DefinitionData::IDI_Inherited: | |||
| 511 | return true; | |||
| 512 | case DefinitionData::IDI_NotInherited: | |||
| 513 | return false; | |||
| 514 | case DefinitionData::IDI_Unknown: | |||
| 515 | // If the class introduced initializers we conservatively assume that we | |||
| 516 | // don't know if any of them is a designated initializer to avoid possible | |||
| 517 | // misleading warnings. | |||
| 518 | if (isIntroducingInitializers(this)) { | |||
| 519 | data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited; | |||
| 520 | } else { | |||
| 521 | if (auto SuperD = getSuperClass()) { | |||
| 522 | data().InheritedDesignatedInitializers = | |||
| 523 | SuperD->declaresOrInheritsDesignatedInitializers() ? | |||
| 524 | DefinitionData::IDI_Inherited : | |||
| 525 | DefinitionData::IDI_NotInherited; | |||
| 526 | } else { | |||
| 527 | data().InheritedDesignatedInitializers = | |||
| 528 | DefinitionData::IDI_NotInherited; | |||
| 529 | } | |||
| 530 | } | |||
| 531 | assert(data().InheritedDesignatedInitializers((void)0) | |||
| 532 | != DefinitionData::IDI_Unknown)((void)0); | |||
| 533 | return data().InheritedDesignatedInitializers == | |||
| 534 | DefinitionData::IDI_Inherited; | |||
| 535 | } | |||
| 536 | ||||
| 537 | llvm_unreachable("unexpected InheritedDesignatedInitializers value")__builtin_unreachable(); | |||
| 538 | } | |||
| 539 | ||||
| 540 | void ObjCInterfaceDecl::getDesignatedInitializers( | |||
| 541 | llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const { | |||
| 542 | // Check for a complete definition and recover if not so. | |||
| 543 | if (!isThisDeclarationADefinition()) | |||
| 544 | return; | |||
| 545 | if (data().ExternallyCompleted) | |||
| 546 | LoadExternalDefinition(); | |||
| 547 | ||||
| 548 | const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers(); | |||
| 549 | if (!IFace) | |||
| 550 | return; | |||
| 551 | ||||
| 552 | for (const auto *MD : IFace->instance_methods()) | |||
| 553 | if (MD->isThisDeclarationADesignatedInitializer()) | |||
| 554 | Methods.push_back(MD); | |||
| 555 | for (const auto *Ext : IFace->visible_extensions()) { | |||
| 556 | for (const auto *MD : Ext->instance_methods()) | |||
| 557 | if (MD->isThisDeclarationADesignatedInitializer()) | |||
| 558 | Methods.push_back(MD); | |||
| 559 | } | |||
| 560 | } | |||
| 561 | ||||
| 562 | bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel, | |||
| 563 | const ObjCMethodDecl **InitMethod) const { | |||
| 564 | bool HasCompleteDef = isThisDeclarationADefinition(); | |||
| 565 | // During deserialization the data record for the ObjCInterfaceDecl could | |||
| 566 | // be made invariant by reusing the canonical decl. Take this into account | |||
| 567 | // when checking for the complete definition. | |||
| 568 | if (!HasCompleteDef && getCanonicalDecl()->hasDefinition() && | |||
| 569 | getCanonicalDecl()->getDefinition() == getDefinition()) | |||
| 570 | HasCompleteDef = true; | |||
| 571 | ||||
| 572 | // Check for a complete definition and recover if not so. | |||
| 573 | if (!HasCompleteDef) | |||
| 574 | return false; | |||
| 575 | ||||
| 576 | if (data().ExternallyCompleted) | |||
| 577 | LoadExternalDefinition(); | |||
| 578 | ||||
| 579 | const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers(); | |||
| 580 | if (!IFace) | |||
| 581 | return false; | |||
| 582 | ||||
| 583 | if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) { | |||
| 584 | if (MD->isThisDeclarationADesignatedInitializer()) { | |||
| 585 | if (InitMethod) | |||
| 586 | *InitMethod = MD; | |||
| 587 | return true; | |||
| 588 | } | |||
| 589 | } | |||
| 590 | for (const auto *Ext : IFace->visible_extensions()) { | |||
| 591 | if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) { | |||
| 592 | if (MD->isThisDeclarationADesignatedInitializer()) { | |||
| 593 | if (InitMethod) | |||
| 594 | *InitMethod = MD; | |||
| 595 | return true; | |||
| 596 | } | |||
| 597 | } | |||
| 598 | } | |||
| 599 | return false; | |||
| 600 | } | |||
| 601 | ||||
| 602 | void ObjCInterfaceDecl::allocateDefinitionData() { | |||
| 603 | assert(!hasDefinition() && "ObjC class already has a definition")((void)0); | |||
| 604 | Data.setPointer(new (getASTContext()) DefinitionData()); | |||
| 605 | Data.getPointer()->Definition = this; | |||
| 606 | ||||
| 607 | // Make the type point at the definition, now that we have one. | |||
| 608 | if (TypeForDecl) | |||
| 609 | cast<ObjCInterfaceType>(TypeForDecl)->Decl = this; | |||
| 610 | } | |||
| 611 | ||||
| 612 | void ObjCInterfaceDecl::startDefinition() { | |||
| 613 | allocateDefinitionData(); | |||
| 614 | ||||
| 615 | // Update all of the declarations with a pointer to the definition. | |||
| 616 | for (auto *RD : redecls()) { | |||
| 617 | if (RD != this) | |||
| 618 | RD->Data = Data; | |||
| 619 | } | |||
| 620 | } | |||
| 621 | ||||
| 622 | ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID, | |||
| 623 | ObjCInterfaceDecl *&clsDeclared) { | |||
| 624 | // FIXME: Should make sure no callers ever do this. | |||
| 625 | if (!hasDefinition()) | |||
| 626 | return nullptr; | |||
| 627 | ||||
| 628 | if (data().ExternallyCompleted) | |||
| 629 | LoadExternalDefinition(); | |||
| 630 | ||||
| 631 | ObjCInterfaceDecl* ClassDecl = this; | |||
| 632 | while (ClassDecl != nullptr) { | |||
| 633 | if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) { | |||
| 634 | clsDeclared = ClassDecl; | |||
| 635 | return I; | |||
| 636 | } | |||
| 637 | ||||
| 638 | for (const auto *Ext : ClassDecl->visible_extensions()) { | |||
| 639 | if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) { | |||
| 640 | clsDeclared = ClassDecl; | |||
| 641 | return I; | |||
| 642 | } | |||
| 643 | } | |||
| 644 | ||||
| 645 | ClassDecl = ClassDecl->getSuperClass(); | |||
| 646 | } | |||
| 647 | return nullptr; | |||
| 648 | } | |||
| 649 | ||||
| 650 | /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super | |||
| 651 | /// class whose name is passed as argument. If it is not one of the super classes | |||
| 652 | /// the it returns NULL. | |||
| 653 | ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass( | |||
| 654 | const IdentifierInfo*ICName) { | |||
| 655 | // FIXME: Should make sure no callers ever do this. | |||
| 656 | if (!hasDefinition()) | |||
| 657 | return nullptr; | |||
| 658 | ||||
| 659 | if (data().ExternallyCompleted) | |||
| 660 | LoadExternalDefinition(); | |||
| 661 | ||||
| 662 | ObjCInterfaceDecl* ClassDecl = this; | |||
| 663 | while (ClassDecl != nullptr) { | |||
| 664 | if (ClassDecl->getIdentifier() == ICName) | |||
| 665 | return ClassDecl; | |||
| 666 | ClassDecl = ClassDecl->getSuperClass(); | |||
| 667 | } | |||
| 668 | return nullptr; | |||
| 669 | } | |||
| 670 | ||||
| 671 | ObjCProtocolDecl * | |||
| 672 | ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) { | |||
| 673 | for (auto *P : all_referenced_protocols()) | |||
| 674 | if (P->lookupProtocolNamed(Name)) | |||
| 675 | return P; | |||
| 676 | ObjCInterfaceDecl *SuperClass = getSuperClass(); | |||
| 677 | return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr; | |||
| 678 | } | |||
| 679 | ||||
| 680 | /// lookupMethod - This method returns an instance/class method by looking in | |||
| 681 | /// the class, its categories, and its super classes (using a linear search). | |||
| 682 | /// When argument category "C" is specified, any implicit method found | |||
| 683 | /// in this category is ignored. | |||
| 684 | ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel, | |||
| 685 | bool isInstance, | |||
| 686 | bool shallowCategoryLookup, | |||
| 687 | bool followSuper, | |||
| 688 | const ObjCCategoryDecl *C) const | |||
| 689 | { | |||
| 690 | // FIXME: Should make sure no callers ever do this. | |||
| 691 | if (!hasDefinition()) | |||
| 692 | return nullptr; | |||
| 693 | ||||
| 694 | const ObjCInterfaceDecl* ClassDecl = this; | |||
| 695 | ObjCMethodDecl *MethodDecl = nullptr; | |||
| 696 | ||||
| 697 | if (data().ExternallyCompleted) | |||
| 698 | LoadExternalDefinition(); | |||
| 699 | ||||
| 700 | while (ClassDecl) { | |||
| 701 | // 1. Look through primary class. | |||
| 702 | if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance))) | |||
| 703 | return MethodDecl; | |||
| 704 | ||||
| 705 | // 2. Didn't find one yet - now look through categories. | |||
| 706 | for (const auto *Cat : ClassDecl->visible_categories()) | |||
| 707 | if ((MethodDecl = Cat->getMethod(Sel, isInstance))) | |||
| 708 | if (C != Cat || !MethodDecl->isImplicit()) | |||
| 709 | return MethodDecl; | |||
| 710 | ||||
| 711 | // 3. Didn't find one yet - look through primary class's protocols. | |||
| 712 | for (const auto *I : ClassDecl->protocols()) | |||
| 713 | if ((MethodDecl = I->lookupMethod(Sel, isInstance))) | |||
| 714 | return MethodDecl; | |||
| 715 | ||||
| 716 | // 4. Didn't find one yet - now look through categories' protocols | |||
| 717 | if (!shallowCategoryLookup) | |||
| 718 | for (const auto *Cat : ClassDecl->visible_categories()) { | |||
| 719 | // Didn't find one yet - look through protocols. | |||
| 720 | const ObjCList<ObjCProtocolDecl> &Protocols = | |||
| 721 | Cat->getReferencedProtocols(); | |||
| 722 | for (auto *Protocol : Protocols) | |||
| 723 | if ((MethodDecl = Protocol->lookupMethod(Sel, isInstance))) | |||
| 724 | if (C != Cat || !MethodDecl->isImplicit()) | |||
| 725 | return MethodDecl; | |||
| 726 | } | |||
| 727 | ||||
| 728 | ||||
| 729 | if (!followSuper) | |||
| 730 | return nullptr; | |||
| 731 | ||||
| 732 | // 5. Get to the super class (if any). | |||
| 733 | ClassDecl = ClassDecl->getSuperClass(); | |||
| 734 | } | |||
| 735 | return nullptr; | |||
| 736 | } | |||
| 737 | ||||
| 738 | // Will search "local" class/category implementations for a method decl. | |||
| 739 | // If failed, then we search in class's root for an instance method. | |||
| 740 | // Returns 0 if no method is found. | |||
| 741 | ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod( | |||
| 742 | const Selector &Sel, | |||
| 743 | bool Instance) const { | |||
| 744 | // FIXME: Should make sure no callers ever do this. | |||
| 745 | if (!hasDefinition()) | |||
| 746 | return nullptr; | |||
| 747 | ||||
| 748 | if (data().ExternallyCompleted) | |||
| 749 | LoadExternalDefinition(); | |||
| 750 | ||||
| 751 | ObjCMethodDecl *Method = nullptr; | |||
| 752 | if (ObjCImplementationDecl *ImpDecl = getImplementation()) | |||
| 753 | Method = Instance ? ImpDecl->getInstanceMethod(Sel) | |||
| 754 | : ImpDecl->getClassMethod(Sel); | |||
| 755 | ||||
| 756 | // Look through local category implementations associated with the class. | |||
| 757 | if (!Method) | |||
| 758 | Method = getCategoryMethod(Sel, Instance); | |||
| 759 | ||||
| 760 | // Before we give up, check if the selector is an instance method. | |||
| 761 | // But only in the root. This matches gcc's behavior and what the | |||
| 762 | // runtime expects. | |||
| 763 | if (!Instance && !Method && !getSuperClass()) { | |||
| 764 | Method = lookupInstanceMethod(Sel); | |||
| 765 | // Look through local category implementations associated | |||
| 766 | // with the root class. | |||
| 767 | if (!Method) | |||
| 768 | Method = lookupPrivateMethod(Sel, true); | |||
| 769 | } | |||
| 770 | ||||
| 771 | if (!Method && getSuperClass()) | |||
| 772 | return getSuperClass()->lookupPrivateMethod(Sel, Instance); | |||
| 773 | return Method; | |||
| 774 | } | |||
| 775 | ||||
| 776 | //===----------------------------------------------------------------------===// | |||
| 777 | // ObjCMethodDecl | |||
| 778 | //===----------------------------------------------------------------------===// | |||
| 779 | ||||
| 780 | ObjCMethodDecl::ObjCMethodDecl( | |||
| 781 | SourceLocation beginLoc, SourceLocation endLoc, Selector SelInfo, | |||
| 782 | QualType T, TypeSourceInfo *ReturnTInfo, DeclContext *contextDecl, | |||
| 783 | bool isInstance, bool isVariadic, bool isPropertyAccessor, | |||
| 784 | bool isSynthesizedAccessorStub, bool isImplicitlyDeclared, bool isDefined, | |||
| 785 | ImplementationControl impControl, bool HasRelatedResultType) | |||
| 786 | : NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo), | |||
| 787 | DeclContext(ObjCMethod), MethodDeclType(T), ReturnTInfo(ReturnTInfo), | |||
| 788 | DeclEndLoc(endLoc) { | |||
| 789 | ||||
| 790 | // Initialized the bits stored in DeclContext. | |||
| 791 | ObjCMethodDeclBits.Family = | |||
| 792 | static_cast<ObjCMethodFamily>(InvalidObjCMethodFamily); | |||
| 793 | setInstanceMethod(isInstance); | |||
| 794 | setVariadic(isVariadic); | |||
| 795 | setPropertyAccessor(isPropertyAccessor); | |||
| 796 | setSynthesizedAccessorStub(isSynthesizedAccessorStub); | |||
| 797 | setDefined(isDefined); | |||
| 798 | setIsRedeclaration(false); | |||
| 799 | setHasRedeclaration(false); | |||
| 800 | setDeclImplementation(impControl); | |||
| 801 | setObjCDeclQualifier(OBJC_TQ_None); | |||
| 802 | setRelatedResultType(HasRelatedResultType); | |||
| 803 | setSelLocsKind(SelLoc_StandardNoSpace); | |||
| 804 | setOverriding(false); | |||
| 805 | setHasSkippedBody(false); | |||
| 806 | ||||
| 807 | setImplicit(isImplicitlyDeclared); | |||
| 808 | } | |||
| 809 | ||||
| 810 | ObjCMethodDecl *ObjCMethodDecl::Create( | |||
| 811 | ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc, | |||
| 812 | Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo, | |||
| 813 | DeclContext *contextDecl, bool isInstance, bool isVariadic, | |||
| 814 | bool isPropertyAccessor, bool isSynthesizedAccessorStub, | |||
| 815 | bool isImplicitlyDeclared, bool isDefined, ImplementationControl impControl, | |||
| 816 | bool HasRelatedResultType) { | |||
| 817 | return new (C, contextDecl) ObjCMethodDecl( | |||
| 818 | beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance, | |||
| 819 | isVariadic, isPropertyAccessor, isSynthesizedAccessorStub, | |||
| 820 | isImplicitlyDeclared, isDefined, impControl, HasRelatedResultType); | |||
| 821 | } | |||
| 822 | ||||
| 823 | ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) { | |||
| 824 | return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(), | |||
| 825 | Selector(), QualType(), nullptr, nullptr); | |||
| 826 | } | |||
| 827 | ||||
| 828 | bool ObjCMethodDecl::isDirectMethod() const { | |||
| 829 | return hasAttr<ObjCDirectAttr>() && | |||
| 830 | !getASTContext().getLangOpts().ObjCDisableDirectMethodsForTesting; | |||
| 831 | } | |||
| 832 | ||||
| 833 | bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const { | |||
| 834 | return getMethodFamily() == OMF_init && | |||
| 835 | hasAttr<ObjCDesignatedInitializerAttr>(); | |||
| 836 | } | |||
| 837 | ||||
| 838 | bool ObjCMethodDecl::definedInNSObject(const ASTContext &Ctx) const { | |||
| 839 | if (const auto *PD = dyn_cast<const ObjCProtocolDecl>(getDeclContext())) | |||
| 840 | return PD->getIdentifier() == Ctx.getNSObjectName(); | |||
| 841 | if (const auto *ID = dyn_cast<const ObjCInterfaceDecl>(getDeclContext())) | |||
| 842 | return ID->getIdentifier() == Ctx.getNSObjectName(); | |||
| 843 | return false; | |||
| 844 | } | |||
| 845 | ||||
| 846 | bool ObjCMethodDecl::isDesignatedInitializerForTheInterface( | |||
| 847 | const ObjCMethodDecl **InitMethod) const { | |||
| 848 | if (getMethodFamily() != OMF_init) | |||
| 849 | return false; | |||
| 850 | const DeclContext *DC = getDeclContext(); | |||
| 851 | if (isa<ObjCProtocolDecl>(DC)) | |||
| 852 | return false; | |||
| 853 | if (const ObjCInterfaceDecl *ID = getClassInterface()) | |||
| 854 | return ID->isDesignatedInitializer(getSelector(), InitMethod); | |||
| 855 | return false; | |||
| 856 | } | |||
| 857 | ||||
| 858 | Stmt *ObjCMethodDecl::getBody() const { | |||
| 859 | return Body.get(getASTContext().getExternalSource()); | |||
| 860 | } | |||
| 861 | ||||
| 862 | void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) { | |||
| 863 | assert(PrevMethod)((void)0); | |||
| 864 | getASTContext().setObjCMethodRedeclaration(PrevMethod, this); | |||
| 865 | setIsRedeclaration(true); | |||
| 866 | PrevMethod->setHasRedeclaration(true); | |||
| 867 | } | |||
| 868 | ||||
| 869 | void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C, | |||
| 870 | ArrayRef<ParmVarDecl*> Params, | |||
| 871 | ArrayRef<SourceLocation> SelLocs) { | |||
| 872 | ParamsAndSelLocs = nullptr; | |||
| 873 | NumParams = Params.size(); | |||
| 874 | if (Params.empty() && SelLocs.empty()) | |||
| 875 | return; | |||
| 876 | ||||
| 877 | static_assert(alignof(ParmVarDecl *) >= alignof(SourceLocation), | |||
| 878 | "Alignment not sufficient for SourceLocation"); | |||
| 879 | ||||
| 880 | unsigned Size = sizeof(ParmVarDecl *) * NumParams + | |||
| 881 | sizeof(SourceLocation) * SelLocs.size(); | |||
| 882 | ParamsAndSelLocs = C.Allocate(Size); | |||
| 883 | std::copy(Params.begin(), Params.end(), getParams()); | |||
| 884 | std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs()); | |||
| 885 | } | |||
| 886 | ||||
| 887 | void ObjCMethodDecl::getSelectorLocs( | |||
| 888 | SmallVectorImpl<SourceLocation> &SelLocs) const { | |||
| 889 | for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i) | |||
| 890 | SelLocs.push_back(getSelectorLoc(i)); | |||
| 891 | } | |||
| 892 | ||||
| 893 | void ObjCMethodDecl::setMethodParams(ASTContext &C, | |||
| 894 | ArrayRef<ParmVarDecl*> Params, | |||
| 895 | ArrayRef<SourceLocation> SelLocs) { | |||
| 896 | assert((!SelLocs.empty() || isImplicit()) &&((void)0) | |||
| 897 | "No selector locs for non-implicit method")((void)0); | |||
| 898 | if (isImplicit()) | |||
| 899 | return setParamsAndSelLocs(C, Params, llvm::None); | |||
| 900 | ||||
| 901 | setSelLocsKind(hasStandardSelectorLocs(getSelector(), SelLocs, Params, | |||
| 902 | DeclEndLoc)); | |||
| 903 | if (getSelLocsKind() != SelLoc_NonStandard) | |||
| 904 | return setParamsAndSelLocs(C, Params, llvm::None); | |||
| 905 | ||||
| 906 | setParamsAndSelLocs(C, Params, SelLocs); | |||
| 907 | } | |||
| 908 | ||||
| 909 | /// A definition will return its interface declaration. | |||
| 910 | /// An interface declaration will return its definition. | |||
| 911 | /// Otherwise it will return itself. | |||
| 912 | ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() { | |||
| 913 | ASTContext &Ctx = getASTContext(); | |||
| 914 | ObjCMethodDecl *Redecl = nullptr; | |||
| 915 | if (hasRedeclaration()) | |||
| 916 | Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this)); | |||
| 917 | if (Redecl) | |||
| 918 | return Redecl; | |||
| 919 | ||||
| 920 | auto *CtxD = cast<Decl>(getDeclContext()); | |||
| 921 | ||||
| 922 | if (!CtxD->isInvalidDecl()) { | |||
| 923 | if (auto *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) { | |||
| 924 | if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD)) | |||
| 925 | if (!ImplD->isInvalidDecl()) | |||
| 926 | Redecl = ImplD->getMethod(getSelector(), isInstanceMethod()); | |||
| 927 | ||||
| 928 | } else if (auto *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) { | |||
| 929 | if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD)) | |||
| 930 | if (!ImplD->isInvalidDecl()) | |||
| 931 | Redecl = ImplD->getMethod(getSelector(), isInstanceMethod()); | |||
| 932 | ||||
| 933 | } else if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) { | |||
| 934 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) | |||
| 935 | if (!IFD->isInvalidDecl()) | |||
| 936 | Redecl = IFD->getMethod(getSelector(), isInstanceMethod()); | |||
| 937 | ||||
| 938 | } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) { | |||
| 939 | if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl()) | |||
| 940 | if (!CatD->isInvalidDecl()) | |||
| 941 | Redecl = CatD->getMethod(getSelector(), isInstanceMethod()); | |||
| 942 | } | |||
| 943 | } | |||
| 944 | ||||
| 945 | // Ensure that the discovered method redeclaration has a valid declaration | |||
| 946 | // context. Used to prevent infinite loops when iterating redeclarations in | |||
| 947 | // a partially invalid AST. | |||
| 948 | if (Redecl && cast<Decl>(Redecl->getDeclContext())->isInvalidDecl()) | |||
| 949 | Redecl = nullptr; | |||
| 950 | ||||
| 951 | if (!Redecl && isRedeclaration()) { | |||
| 952 | // This is the last redeclaration, go back to the first method. | |||
| 953 | return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(), | |||
| 954 | isInstanceMethod(), | |||
| 955 | /*AllowHidden=*/true); | |||
| 956 | } | |||
| 957 | ||||
| 958 | return Redecl ? Redecl : this; | |||
| 959 | } | |||
| 960 | ||||
| 961 | ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() { | |||
| 962 | auto *CtxD = cast<Decl>(getDeclContext()); | |||
| 963 | const auto &Sel = getSelector(); | |||
| 964 | ||||
| 965 | if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) { | |||
| 966 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) { | |||
| 967 | // When the container is the ObjCImplementationDecl (the primary | |||
| 968 | // @implementation), then the canonical Decl is either in | |||
| 969 | // the class Interface, or in any of its extension. | |||
| 970 | // | |||
| 971 | // So when we don't find it in the ObjCInterfaceDecl, | |||
| 972 | // sift through extensions too. | |||
| 973 | if (ObjCMethodDecl *MD = IFD->getMethod(Sel, isInstanceMethod())) | |||
| 974 | return MD; | |||
| 975 | for (auto *Ext : IFD->known_extensions()) | |||
| 976 | if (ObjCMethodDecl *MD = Ext->getMethod(Sel, isInstanceMethod())) | |||
| 977 | return MD; | |||
| 978 | } | |||
| 979 | } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) { | |||
| 980 | if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl()) | |||
| 981 | if (ObjCMethodDecl *MD = CatD->getMethod(Sel, isInstanceMethod())) | |||
| 982 | return MD; | |||
| 983 | } | |||
| 984 | ||||
| 985 | if (isRedeclaration()) { | |||
| 986 | // It is possible that we have not done deserializing the ObjCMethod yet. | |||
| 987 | ObjCMethodDecl *MD = | |||
| 988 | cast<ObjCContainerDecl>(CtxD)->getMethod(Sel, isInstanceMethod(), | |||
| 989 | /*AllowHidden=*/true); | |||
| 990 | return MD ? MD : this; | |||
| 991 | } | |||
| 992 | ||||
| 993 | return this; | |||
| 994 | } | |||
| 995 | ||||
| 996 | SourceLocation ObjCMethodDecl::getEndLoc() const { | |||
| 997 | if (Stmt *Body = getBody()) | |||
| 998 | return Body->getEndLoc(); | |||
| 999 | return DeclEndLoc; | |||
| 1000 | } | |||
| 1001 | ||||
| 1002 | ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const { | |||
| 1003 | auto family = static_cast<ObjCMethodFamily>(ObjCMethodDeclBits.Family); | |||
| 1004 | if (family != static_cast<unsigned>(InvalidObjCMethodFamily)) | |||
| 1005 | return family; | |||
| 1006 | ||||
| 1007 | // Check for an explicit attribute. | |||
| 1008 | if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) { | |||
| 1009 | // The unfortunate necessity of mapping between enums here is due | |||
| 1010 | // to the attributes framework. | |||
| 1011 | switch (attr->getFamily()) { | |||
| 1012 | case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break; | |||
| 1013 | case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break; | |||
| 1014 | case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break; | |||
| 1015 | case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break; | |||
| 1016 | case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break; | |||
| 1017 | case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break; | |||
| 1018 | } | |||
| 1019 | ObjCMethodDeclBits.Family = family; | |||
| 1020 | return family; | |||
| 1021 | } | |||
| 1022 | ||||
| 1023 | family = getSelector().getMethodFamily(); | |||
| 1024 | switch (family) { | |||
| 1025 | case OMF_None: break; | |||
| 1026 | ||||
| 1027 | // init only has a conventional meaning for an instance method, and | |||
| 1028 | // it has to return an object. | |||
| 1029 | case OMF_init: | |||
| 1030 | if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType()) | |||
| 1031 | family = OMF_None; | |||
| 1032 | break; | |||
| 1033 | ||||
| 1034 | // alloc/copy/new have a conventional meaning for both class and | |||
| 1035 | // instance methods, but they require an object return. | |||
| 1036 | case OMF_alloc: | |||
| 1037 | case OMF_copy: | |||
| 1038 | case OMF_mutableCopy: | |||
| 1039 | case OMF_new: | |||
| 1040 | if (!getReturnType()->isObjCObjectPointerType()) | |||
| 1041 | family = OMF_None; | |||
| 1042 | break; | |||
| 1043 | ||||
| 1044 | // These selectors have a conventional meaning only for instance methods. | |||
| 1045 | case OMF_dealloc: | |||
| 1046 | case OMF_finalize: | |||
| 1047 | case OMF_retain: | |||
| 1048 | case OMF_release: | |||
| 1049 | case OMF_autorelease: | |||
| 1050 | case OMF_retainCount: | |||
| 1051 | case OMF_self: | |||
| 1052 | if (!isInstanceMethod()) | |||
| 1053 | family = OMF_None; | |||
| 1054 | break; | |||
| 1055 | ||||
| 1056 | case OMF_initialize: | |||
| 1057 | if (isInstanceMethod() || !getReturnType()->isVoidType()) | |||
| 1058 | family = OMF_None; | |||
| 1059 | break; | |||
| 1060 | ||||
| 1061 | case OMF_performSelector: | |||
| 1062 | if (!isInstanceMethod() || !getReturnType()->isObjCIdType()) | |||
| 1063 | family = OMF_None; | |||
| 1064 | else { | |||
| 1065 | unsigned noParams = param_size(); | |||
| 1066 | if (noParams < 1 || noParams > 3) | |||
| 1067 | family = OMF_None; | |||
| 1068 | else { | |||
| 1069 | ObjCMethodDecl::param_type_iterator it = param_type_begin(); | |||
| 1070 | QualType ArgT = (*it); | |||
| 1071 | if (!ArgT->isObjCSelType()) { | |||
| 1072 | family = OMF_None; | |||
| 1073 | break; | |||
| 1074 | } | |||
| 1075 | while (--noParams) { | |||
| 1076 | it++; | |||
| 1077 | ArgT = (*it); | |||
| 1078 | if (!ArgT->isObjCIdType()) { | |||
| 1079 | family = OMF_None; | |||
| 1080 | break; | |||
| 1081 | } | |||
| 1082 | } | |||
| 1083 | } | |||
| 1084 | } | |||
| 1085 | break; | |||
| 1086 | ||||
| 1087 | } | |||
| 1088 | ||||
| 1089 | // Cache the result. | |||
| 1090 | ObjCMethodDeclBits.Family = family; | |||
| 1091 | return family; | |||
| 1092 | } | |||
| 1093 | ||||
| 1094 | QualType ObjCMethodDecl::getSelfType(ASTContext &Context, | |||
| 1095 | const ObjCInterfaceDecl *OID, | |||
| 1096 | bool &selfIsPseudoStrong, | |||
| 1097 | bool &selfIsConsumed) const { | |||
| 1098 | QualType selfTy; | |||
| 1099 | selfIsPseudoStrong = false; | |||
| 1100 | selfIsConsumed = false; | |||
| 1101 | if (isInstanceMethod()) { | |||
| 1102 | // There may be no interface context due to error in declaration | |||
| 1103 | // of the interface (which has been reported). Recover gracefully. | |||
| 1104 | if (OID) { | |||
| 1105 | selfTy = Context.getObjCInterfaceType(OID); | |||
| 1106 | selfTy = Context.getObjCObjectPointerType(selfTy); | |||
| 1107 | } else { | |||
| 1108 | selfTy = Context.getObjCIdType(); | |||
| 1109 | } | |||
| 1110 | } else // we have a factory method. | |||
| 1111 | selfTy = Context.getObjCClassType(); | |||
| 1112 | ||||
| 1113 | if (Context.getLangOpts().ObjCAutoRefCount) { | |||
| 1114 | if (isInstanceMethod()) { | |||
| 1115 | selfIsConsumed = hasAttr<NSConsumesSelfAttr>(); | |||
| 1116 | ||||
| 1117 | // 'self' is always __strong. It's actually pseudo-strong except | |||
| 1118 | // in init methods (or methods labeled ns_consumes_self), though. | |||
| 1119 | Qualifiers qs; | |||
| 1120 | qs.setObjCLifetime(Qualifiers::OCL_Strong); | |||
| 1121 | selfTy = Context.getQualifiedType(selfTy, qs); | |||
| 1122 | ||||
| 1123 | // In addition, 'self' is const unless this is an init method. | |||
| 1124 | if (getMethodFamily() != OMF_init && !selfIsConsumed) { | |||
| 1125 | selfTy = selfTy.withConst(); | |||
| 1126 | selfIsPseudoStrong = true; | |||
| 1127 | } | |||
| 1128 | } | |||
| 1129 | else { | |||
| 1130 | assert(isClassMethod())((void)0); | |||
| 1131 | // 'self' is always const in class methods. | |||
| 1132 | selfTy = selfTy.withConst(); | |||
| 1133 | selfIsPseudoStrong = true; | |||
| 1134 | } | |||
| 1135 | } | |||
| 1136 | return selfTy; | |||
| 1137 | } | |||
| 1138 | ||||
| 1139 | void ObjCMethodDecl::createImplicitParams(ASTContext &Context, | |||
| 1140 | const ObjCInterfaceDecl *OID) { | |||
| 1141 | bool selfIsPseudoStrong, selfIsConsumed; | |||
| 1142 | QualType selfTy = | |||
| 1143 | getSelfType(Context, OID, selfIsPseudoStrong, selfIsConsumed); | |||
| 1144 | auto *Self = ImplicitParamDecl::Create(Context, this, SourceLocation(), | |||
| 1145 | &Context.Idents.get("self"), selfTy, | |||
| 1146 | ImplicitParamDecl::ObjCSelf); | |||
| 1147 | setSelfDecl(Self); | |||
| 1148 | ||||
| 1149 | if (selfIsConsumed) | |||
| 1150 | Self->addAttr(NSConsumedAttr::CreateImplicit(Context)); | |||
| 1151 | ||||
| 1152 | if (selfIsPseudoStrong) | |||
| 1153 | Self->setARCPseudoStrong(true); | |||
| 1154 | ||||
| 1155 | setCmdDecl(ImplicitParamDecl::Create( | |||
| 1156 | Context, this, SourceLocation(), &Context.Idents.get("_cmd"), | |||
| 1157 | Context.getObjCSelType(), ImplicitParamDecl::ObjCCmd)); | |||
| 1158 | } | |||
| 1159 | ||||
| 1160 | ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() { | |||
| 1161 | if (auto *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext())) | |||
| 1162 | return ID; | |||
| 1163 | if (auto *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext())) | |||
| 1164 | return CD->getClassInterface(); | |||
| 1165 | if (auto *IMD = dyn_cast<ObjCImplDecl>(getDeclContext())) | |||
| 1166 | return IMD->getClassInterface(); | |||
| 1167 | if (isa<ObjCProtocolDecl>(getDeclContext())) | |||
| 1168 | return nullptr; | |||
| 1169 | llvm_unreachable("unknown method context")__builtin_unreachable(); | |||
| 1170 | } | |||
| 1171 | ||||
| 1172 | ObjCCategoryDecl *ObjCMethodDecl::getCategory() { | |||
| 1173 | if (auto *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext())) | |||
| 1174 | return CD; | |||
| 1175 | if (auto *IMD = dyn_cast<ObjCCategoryImplDecl>(getDeclContext())) | |||
| 1176 | return IMD->getCategoryDecl(); | |||
| 1177 | return nullptr; | |||
| 1178 | } | |||
| 1179 | ||||
| 1180 | SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const { | |||
| 1181 | const auto *TSI = getReturnTypeSourceInfo(); | |||
| 1182 | if (TSI) | |||
| 1183 | return TSI->getTypeLoc().getSourceRange(); | |||
| 1184 | return SourceRange(); | |||
| 1185 | } | |||
| 1186 | ||||
| 1187 | QualType ObjCMethodDecl::getSendResultType() const { | |||
| 1188 | ASTContext &Ctx = getASTContext(); | |||
| 1189 | return getReturnType().getNonLValueExprType(Ctx) | |||
| 1190 | .substObjCTypeArgs(Ctx, {}, ObjCSubstitutionContext::Result); | |||
| 1191 | } | |||
| 1192 | ||||
| 1193 | QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const { | |||
| 1194 | // FIXME: Handle related result types here. | |||
| 1195 | ||||
| 1196 | return getReturnType().getNonLValueExprType(getASTContext()) | |||
| 1197 | .substObjCMemberType(receiverType, getDeclContext(), | |||
| 1198 | ObjCSubstitutionContext::Result); | |||
| 1199 | } | |||
| 1200 | ||||
| 1201 | static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container, | |||
| 1202 | const ObjCMethodDecl *Method, | |||
| 1203 | SmallVectorImpl<const ObjCMethodDecl *> &Methods, | |||
| 1204 | bool MovedToSuper) { | |||
| 1205 | if (!Container) | |||
| 1206 | return; | |||
| 1207 | ||||
| 1208 | // In categories look for overridden methods from protocols. A method from | |||
| 1209 | // category is not "overridden" since it is considered as the "same" method | |||
| 1210 | // (same USR) as the one from the interface. | |||
| 1211 | if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) { | |||
| 1212 | // Check whether we have a matching method at this category but only if we | |||
| 1213 | // are at the super class level. | |||
| 1214 | if (MovedToSuper) | |||
| 1215 | if (ObjCMethodDecl * | |||
| 1216 | Overridden = Container->getMethod(Method->getSelector(), | |||
| 1217 | Method->isInstanceMethod(), | |||
| 1218 | /*AllowHidden=*/true)) | |||
| 1219 | if (Method != Overridden) { | |||
| 1220 | // We found an override at this category; there is no need to look | |||
| 1221 | // into its protocols. | |||
| 1222 | Methods.push_back(Overridden); | |||
| 1223 | return; | |||
| 1224 | } | |||
| 1225 | ||||
| 1226 | for (const auto *P : Category->protocols()) | |||
| 1227 | CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper); | |||
| 1228 | return; | |||
| 1229 | } | |||
| 1230 | ||||
| 1231 | // Check whether we have a matching method at this level. | |||
| 1232 | if (const ObjCMethodDecl * | |||
| 1233 | Overridden = Container->getMethod(Method->getSelector(), | |||
| 1234 | Method->isInstanceMethod(), | |||
| 1235 | /*AllowHidden=*/true)) | |||
| 1236 | if (Method != Overridden) { | |||
| 1237 | // We found an override at this level; there is no need to look | |||
| 1238 | // into other protocols or categories. | |||
| 1239 | Methods.push_back(Overridden); | |||
| 1240 | return; | |||
| 1241 | } | |||
| 1242 | ||||
| 1243 | if (const auto *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){ | |||
| 1244 | for (const auto *P : Protocol->protocols()) | |||
| 1245 | CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper); | |||
| 1246 | } | |||
| 1247 | ||||
| 1248 | if (const auto *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { | |||
| 1249 | for (const auto *P : Interface->protocols()) | |||
| 1250 | CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper); | |||
| 1251 | ||||
| 1252 | for (const auto *Cat : Interface->known_categories()) | |||
| 1253 | CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper); | |||
| 1254 | ||||
| 1255 | if (const ObjCInterfaceDecl *Super = Interface->getSuperClass()) | |||
| 1256 | return CollectOverriddenMethodsRecurse(Super, Method, Methods, | |||
| 1257 | /*MovedToSuper=*/true); | |||
| 1258 | } | |||
| 1259 | } | |||
| 1260 | ||||
| 1261 | static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container, | |||
| 1262 | const ObjCMethodDecl *Method, | |||
| 1263 | SmallVectorImpl<const ObjCMethodDecl *> &Methods) { | |||
| 1264 | CollectOverriddenMethodsRecurse(Container, Method, Methods, | |||
| 1265 | /*MovedToSuper=*/false); | |||
| 1266 | } | |||
| 1267 | ||||
| 1268 | static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method, | |||
| 1269 | SmallVectorImpl<const ObjCMethodDecl *> &overridden) { | |||
| 1270 | assert(Method->isOverriding())((void)0); | |||
| 1271 | ||||
| 1272 | if (const auto *ProtD = | |||
| 1273 | dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) { | |||
| 1274 | CollectOverriddenMethods(ProtD, Method, overridden); | |||
| 1275 | ||||
| 1276 | } else if (const auto *IMD = | |||
| 1277 | dyn_cast<ObjCImplDecl>(Method->getDeclContext())) { | |||
| 1278 | const ObjCInterfaceDecl *ID = IMD->getClassInterface(); | |||
| 1279 | if (!ID) | |||
| 1280 | return; | |||
| 1281 | // Start searching for overridden methods using the method from the | |||
| 1282 | // interface as starting point. | |||
| 1283 | if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(), | |||
| 1284 | Method->isInstanceMethod(), | |||
| 1285 | /*AllowHidden=*/true)) | |||
| 1286 | Method = IFaceMeth; | |||
| 1287 | CollectOverriddenMethods(ID, Method, overridden); | |||
| 1288 | ||||
| 1289 | } else if (const auto *CatD = | |||
| 1290 | dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) { | |||
| 1291 | const ObjCInterfaceDecl *ID = CatD->getClassInterface(); | |||
| 1292 | if (!ID) | |||
| 1293 | return; | |||
| 1294 | // Start searching for overridden methods using the method from the | |||
| 1295 | // interface as starting point. | |||
| 1296 | if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(), | |||
| 1297 | Method->isInstanceMethod(), | |||
| 1298 | /*AllowHidden=*/true)) | |||
| 1299 | Method = IFaceMeth; | |||
| 1300 | CollectOverriddenMethods(ID, Method, overridden); | |||
| 1301 | ||||
| 1302 | } else { | |||
| 1303 | CollectOverriddenMethods( | |||
| 1304 | dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()), | |||
| 1305 | Method, overridden); | |||
| 1306 | } | |||
| 1307 | } | |||
| 1308 | ||||
| 1309 | void ObjCMethodDecl::getOverriddenMethods( | |||
| 1310 | SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const { | |||
| 1311 | const ObjCMethodDecl *Method = this; | |||
| 1312 | ||||
| 1313 | if (Method->isRedeclaration()) { | |||
| 1314 | Method = cast<ObjCContainerDecl>(Method->getDeclContext()) | |||
| 1315 | ->getMethod(Method->getSelector(), Method->isInstanceMethod(), | |||
| 1316 | /*AllowHidden=*/true); | |||
| 1317 | } | |||
| 1318 | ||||
| 1319 | if (Method->isOverriding()) { | |||
| 1320 | collectOverriddenMethodsSlow(Method, Overridden); | |||
| 1321 | assert(!Overridden.empty() &&((void)0) | |||
| 1322 | "ObjCMethodDecl's overriding bit is not as expected")((void)0); | |||
| 1323 | } | |||
| 1324 | } | |||
| 1325 | ||||
| 1326 | const ObjCPropertyDecl * | |||
| 1327 | ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const { | |||
| 1328 | Selector Sel = getSelector(); | |||
| 1329 | unsigned NumArgs = Sel.getNumArgs(); | |||
| 1330 | if (NumArgs > 1) | |||
| 1331 | return nullptr; | |||
| 1332 | ||||
| 1333 | if (isPropertyAccessor()) { | |||
| 1334 | const auto *Container = cast<ObjCContainerDecl>(getParent()); | |||
| 1335 | // For accessor stubs, go back to the interface. | |||
| 1336 | if (auto *ImplDecl = dyn_cast<ObjCImplDecl>(Container)) | |||
| 1337 | if (isSynthesizedAccessorStub()) | |||
| 1338 | Container = ImplDecl->getClassInterface(); | |||
| 1339 | ||||
| 1340 | bool IsGetter = (NumArgs == 0); | |||
| 1341 | bool IsInstance = isInstanceMethod(); | |||
| 1342 | ||||
| 1343 | /// Local function that attempts to find a matching property within the | |||
| 1344 | /// given Objective-C container. | |||
| 1345 | auto findMatchingProperty = | |||
| 1346 | [&](const ObjCContainerDecl *Container) -> const ObjCPropertyDecl * { | |||
| 1347 | if (IsInstance) { | |||
| 1348 | for (const auto *I : Container->instance_properties()) { | |||
| 1349 | Selector NextSel = IsGetter ? I->getGetterName() | |||
| 1350 | : I->getSetterName(); | |||
| 1351 | if (NextSel == Sel) | |||
| 1352 | return I; | |||
| 1353 | } | |||
| 1354 | } else { | |||
| 1355 | for (const auto *I : Container->class_properties()) { | |||
| 1356 | Selector NextSel = IsGetter ? I->getGetterName() | |||
| 1357 | : I->getSetterName(); | |||
| 1358 | if (NextSel == Sel) | |||
| 1359 | return I; | |||
| 1360 | } | |||
| 1361 | } | |||
| 1362 | ||||
| 1363 | return nullptr; | |||
| 1364 | }; | |||
| 1365 | ||||
| 1366 | // Look in the container we were given. | |||
| 1367 | if (const auto *Found = findMatchingProperty(Container)) | |||
| 1368 | return Found; | |||
| 1369 | ||||
| 1370 | // If we're in a category or extension, look in the main class. | |||
| 1371 | const ObjCInterfaceDecl *ClassDecl = nullptr; | |||
| 1372 | if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) { | |||
| 1373 | ClassDecl = Category->getClassInterface(); | |||
| 1374 | if (const auto *Found = findMatchingProperty(ClassDecl)) | |||
| 1375 | return Found; | |||
| 1376 | } else { | |||
| 1377 | // Determine whether the container is a class. | |||
| 1378 | ClassDecl = cast<ObjCInterfaceDecl>(Container); | |||
| 1379 | } | |||
| 1380 | assert(ClassDecl && "Failed to find main class")((void)0); | |||
| 1381 | ||||
| 1382 | // If we have a class, check its visible extensions. | |||
| 1383 | for (const auto *Ext : ClassDecl->visible_extensions()) { | |||
| 1384 | if (Ext == Container) | |||
| 1385 | continue; | |||
| 1386 | if (const auto *Found = findMatchingProperty(Ext)) | |||
| 1387 | return Found; | |||
| 1388 | } | |||
| 1389 | ||||
| 1390 | assert(isSynthesizedAccessorStub() && "expected an accessor stub")((void)0); | |||
| 1391 | ||||
| 1392 | for (const auto *Cat : ClassDecl->known_categories()) { | |||
| 1393 | if (Cat == Container) | |||
| 1394 | continue; | |||
| 1395 | if (const auto *Found = findMatchingProperty(Cat)) | |||
| 1396 | return Found; | |||
| 1397 | } | |||
| 1398 | ||||
| 1399 | llvm_unreachable("Marked as a property accessor but no property found!")__builtin_unreachable(); | |||
| 1400 | } | |||
| 1401 | ||||
| 1402 | if (!CheckOverrides) | |||
| 1403 | return nullptr; | |||
| 1404 | ||||
| 1405 | using OverridesTy = SmallVector<const ObjCMethodDecl *, 8>; | |||
| 1406 | ||||
| 1407 | OverridesTy Overrides; | |||
| 1408 | getOverriddenMethods(Overrides); | |||
| 1409 | for (const auto *Override : Overrides) | |||
| 1410 | if (const ObjCPropertyDecl *Prop = Override->findPropertyDecl(false)) | |||
| 1411 | return Prop; | |||
| 1412 | ||||
| 1413 | return nullptr; | |||
| 1414 | } | |||
| 1415 | ||||
| 1416 | //===----------------------------------------------------------------------===// | |||
| 1417 | // ObjCTypeParamDecl | |||
| 1418 | //===----------------------------------------------------------------------===// | |||
| 1419 | ||||
| 1420 | void ObjCTypeParamDecl::anchor() {} | |||
| 1421 | ||||
| 1422 | ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc, | |||
| 1423 | ObjCTypeParamVariance variance, | |||
| 1424 | SourceLocation varianceLoc, | |||
| 1425 | unsigned index, | |||
| 1426 | SourceLocation nameLoc, | |||
| 1427 | IdentifierInfo *name, | |||
| 1428 | SourceLocation colonLoc, | |||
| 1429 | TypeSourceInfo *boundInfo) { | |||
| 1430 | auto *TPDecl = | |||
| 1431 | new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index, | |||
| 1432 | nameLoc, name, colonLoc, boundInfo); | |||
| 1433 | QualType TPType = ctx.getObjCTypeParamType(TPDecl, {}); | |||
| 1434 | TPDecl->setTypeForDecl(TPType.getTypePtr()); | |||
| 1435 | return TPDecl; | |||
| 1436 | } | |||
| 1437 | ||||
| 1438 | ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx, | |||
| 1439 | unsigned ID) { | |||
| 1440 | return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr, | |||
| 1441 | ObjCTypeParamVariance::Invariant, | |||
| 1442 | SourceLocation(), 0, SourceLocation(), | |||
| 1443 | nullptr, SourceLocation(), nullptr); | |||
| 1444 | } | |||
| 1445 | ||||
| 1446 | SourceRange ObjCTypeParamDecl::getSourceRange() const { | |||
| 1447 | SourceLocation startLoc = VarianceLoc; | |||
| 1448 | if (startLoc.isInvalid()) | |||
| 1449 | startLoc = getLocation(); | |||
| 1450 | ||||
| 1451 | if (hasExplicitBound()) { | |||
| 1452 | return SourceRange(startLoc, | |||
| 1453 | getTypeSourceInfo()->getTypeLoc().getEndLoc()); | |||
| 1454 | } | |||
| 1455 | ||||
| 1456 | return SourceRange(startLoc); | |||
| 1457 | } | |||
| 1458 | ||||
| 1459 | //===----------------------------------------------------------------------===// | |||
| 1460 | // ObjCTypeParamList | |||
| 1461 | //===----------------------------------------------------------------------===// | |||
| 1462 | ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc, | |||
| 1463 | ArrayRef<ObjCTypeParamDecl *> typeParams, | |||
| 1464 | SourceLocation rAngleLoc) | |||
| 1465 | : Brackets(lAngleLoc, rAngleLoc), NumParams(typeParams.size()) { | |||
| 1466 | std::copy(typeParams.begin(), typeParams.end(), begin()); | |||
| 1467 | } | |||
| 1468 | ||||
| 1469 | ObjCTypeParamList *ObjCTypeParamList::create( | |||
| 1470 | ASTContext &ctx, | |||
| 1471 | SourceLocation lAngleLoc, | |||
| 1472 | ArrayRef<ObjCTypeParamDecl *> typeParams, | |||
| 1473 | SourceLocation rAngleLoc) { | |||
| 1474 | void *mem = | |||
| 1475 | ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()), | |||
| 1476 | alignof(ObjCTypeParamList)); | |||
| 1477 | return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc); | |||
| 1478 | } | |||
| 1479 | ||||
| 1480 | void ObjCTypeParamList::gatherDefaultTypeArgs( | |||
| 1481 | SmallVectorImpl<QualType> &typeArgs) const { | |||
| 1482 | typeArgs.reserve(size()); | |||
| 1483 | for (auto typeParam : *this) | |||
| 1484 | typeArgs.push_back(typeParam->getUnderlyingType()); | |||
| 1485 | } | |||
| 1486 | ||||
| 1487 | //===----------------------------------------------------------------------===// | |||
| 1488 | // ObjCInterfaceDecl | |||
| 1489 | //===----------------------------------------------------------------------===// | |||
| 1490 | ||||
| 1491 | ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C, | |||
| 1492 | DeclContext *DC, | |||
| 1493 | SourceLocation atLoc, | |||
| 1494 | IdentifierInfo *Id, | |||
| 1495 | ObjCTypeParamList *typeParamList, | |||
| 1496 | ObjCInterfaceDecl *PrevDecl, | |||
| 1497 | SourceLocation ClassLoc, | |||
| 1498 | bool isInternal){ | |||
| 1499 | auto *Result = new (C, DC) | |||
| 1500 | ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl, | |||
| 1501 | isInternal); | |||
| 1502 | Result->Data.setInt(!C.getLangOpts().Modules); | |||
| 1503 | C.getObjCInterfaceType(Result, PrevDecl); | |||
| 1504 | return Result; | |||
| 1505 | } | |||
| 1506 | ||||
| 1507 | ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C, | |||
| 1508 | unsigned ID) { | |||
| 1509 | auto *Result = new (C, ID) | |||
| 1510 | ObjCInterfaceDecl(C, nullptr, SourceLocation(), nullptr, nullptr, | |||
| 1511 | SourceLocation(), nullptr, false); | |||
| 1512 | Result->Data.setInt(!C.getLangOpts().Modules); | |||
| 1513 | return Result; | |||
| 1514 | } | |||
| 1515 | ||||
| 1516 | ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC, | |||
| 1517 | SourceLocation AtLoc, IdentifierInfo *Id, | |||
| 1518 | ObjCTypeParamList *typeParamList, | |||
| 1519 | SourceLocation CLoc, | |||
| 1520 | ObjCInterfaceDecl *PrevDecl, | |||
| 1521 | bool IsInternal) | |||
| 1522 | : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc), | |||
| 1523 | redeclarable_base(C) { | |||
| 1524 | setPreviousDecl(PrevDecl); | |||
| 1525 | ||||
| 1526 | // Copy the 'data' pointer over. | |||
| 1527 | if (PrevDecl) | |||
| 1528 | Data = PrevDecl->Data; | |||
| 1529 | ||||
| 1530 | setImplicit(IsInternal); | |||
| 1531 | ||||
| 1532 | setTypeParamList(typeParamList); | |||
| 1533 | } | |||
| 1534 | ||||
| 1535 | void ObjCInterfaceDecl::LoadExternalDefinition() const { | |||
| 1536 | assert(data().ExternallyCompleted && "Class is not externally completed")((void)0); | |||
| 1537 | data().ExternallyCompleted = false; | |||
| 1538 | getASTContext().getExternalSource()->CompleteType( | |||
| 1539 | const_cast<ObjCInterfaceDecl *>(this)); | |||
| 1540 | } | |||
| 1541 | ||||
| 1542 | void ObjCInterfaceDecl::setExternallyCompleted() { | |||
| 1543 | assert(getASTContext().getExternalSource() &&((void)0) | |||
| 1544 | "Class can't be externally completed without an external source")((void)0); | |||
| 1545 | assert(hasDefinition() &&((void)0) | |||
| 1546 | "Forward declarations can't be externally completed")((void)0); | |||
| 1547 | data().ExternallyCompleted = true; | |||
| 1548 | } | |||
| 1549 | ||||
| 1550 | void ObjCInterfaceDecl::setHasDesignatedInitializers() { | |||
| 1551 | // Check for a complete definition and recover if not so. | |||
| 1552 | if (!isThisDeclarationADefinition()) | |||
| 1553 | return; | |||
| 1554 | data().HasDesignatedInitializers = true; | |||
| 1555 | } | |||
| 1556 | ||||
| 1557 | bool ObjCInterfaceDecl::hasDesignatedInitializers() const { | |||
| 1558 | // Check for a complete definition and recover if not so. | |||
| 1559 | if (!isThisDeclarationADefinition()) | |||
| 1560 | return false; | |||
| 1561 | if (data().ExternallyCompleted) | |||
| 1562 | LoadExternalDefinition(); | |||
| 1563 | ||||
| 1564 | return data().HasDesignatedInitializers; | |||
| 1565 | } | |||
| 1566 | ||||
| 1567 | StringRef | |||
| 1568 | ObjCInterfaceDecl::getObjCRuntimeNameAsString() const { | |||
| 1569 | if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>()) | |||
| 1570 | return ObjCRTName->getMetadataName(); | |||
| 1571 | ||||
| 1572 | return getName(); | |||
| 1573 | } | |||
| 1574 | ||||
| 1575 | StringRef | |||
| 1576 | ObjCImplementationDecl::getObjCRuntimeNameAsString() const { | |||
| 1577 | if (ObjCInterfaceDecl *ID = | |||
| 1578 | const_cast<ObjCImplementationDecl*>(this)->getClassInterface()) | |||
| 1579 | return ID->getObjCRuntimeNameAsString(); | |||
| 1580 | ||||
| 1581 | return getName(); | |||
| 1582 | } | |||
| 1583 | ||||
| 1584 | ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const { | |||
| 1585 | if (const ObjCInterfaceDecl *Def = getDefinition()) { | |||
| 1586 | if (data().ExternallyCompleted) | |||
| 1587 | LoadExternalDefinition(); | |||
| 1588 | ||||
| 1589 | return getASTContext().getObjCImplementation( | |||
| 1590 | const_cast<ObjCInterfaceDecl*>(Def)); | |||
| 1591 | } | |||
| 1592 | ||||
| 1593 | // FIXME: Should make sure no callers ever do this. | |||
| 1594 | return nullptr; | |||
| 1595 | } | |||
| 1596 | ||||
| 1597 | void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) { | |||
| 1598 | getASTContext().setObjCImplementation(getDefinition(), ImplD); | |||
| 1599 | } | |||
| 1600 | ||||
| 1601 | namespace { | |||
| 1602 | ||||
| 1603 | struct SynthesizeIvarChunk { | |||
| 1604 | uint64_t Size; | |||
| 1605 | ObjCIvarDecl *Ivar; | |||
| 1606 | ||||
| 1607 | SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar) | |||
| 1608 | : Size(size), Ivar(ivar) {} | |||
| 1609 | }; | |||
| 1610 | ||||
| 1611 | bool operator<(const SynthesizeIvarChunk & LHS, | |||
| 1612 | const SynthesizeIvarChunk &RHS) { | |||
| 1613 | return LHS.Size < RHS.Size; | |||
| 1614 | } | |||
| 1615 | ||||
| 1616 | } // namespace | |||
| 1617 | ||||
| 1618 | /// all_declared_ivar_begin - return first ivar declared in this class, | |||
| 1619 | /// its extensions and its implementation. Lazily build the list on first | |||
| 1620 | /// access. | |||
| 1621 | /// | |||
| 1622 | /// Caveat: The list returned by this method reflects the current | |||
| 1623 | /// state of the parser. The cache will be updated for every ivar | |||
| 1624 | /// added by an extension or the implementation when they are | |||
| 1625 | /// encountered. | |||
| 1626 | /// See also ObjCIvarDecl::Create(). | |||
| 1627 | ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() { | |||
| 1628 | // FIXME: Should make sure no callers ever do this. | |||
| 1629 | if (!hasDefinition()) | |||
| ||||
| 1630 | return nullptr; | |||
| 1631 | ||||
| 1632 | ObjCIvarDecl *curIvar = nullptr; | |||
| 1633 | if (!data().IvarList) { | |||
| 1634 | if (!ivar_empty()) { | |||
| 1635 | ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end(); | |||
| 1636 | data().IvarList = *I; ++I; | |||
| 1637 | for (curIvar = data().IvarList; I != E; curIvar = *I, ++I) | |||
| 1638 | curIvar->setNextIvar(*I); | |||
| 1639 | } | |||
| 1640 | ||||
| 1641 | for (const auto *Ext : known_extensions()) { | |||
| 1642 | if (!Ext->ivar_empty()) { | |||
| 1643 | ObjCCategoryDecl::ivar_iterator | |||
| 1644 | I = Ext->ivar_begin(), | |||
| 1645 | E = Ext->ivar_end(); | |||
| 1646 | if (!data().IvarList) { | |||
| 1647 | data().IvarList = *I; ++I; | |||
| 1648 | curIvar = data().IvarList; | |||
| 1649 | } | |||
| 1650 | for ( ;I != E; curIvar = *I, ++I) | |||
| 1651 | curIvar->setNextIvar(*I); | |||
| 1652 | } | |||
| 1653 | } | |||
| 1654 | data().IvarListMissingImplementation = true; | |||
| 1655 | } | |||
| 1656 | ||||
| 1657 | // cached and complete! | |||
| 1658 | if (!data().IvarListMissingImplementation) | |||
| 1659 | return data().IvarList; | |||
| 1660 | ||||
| 1661 | if (ObjCImplementationDecl *ImplDecl = getImplementation()) { | |||
| 1662 | data().IvarListMissingImplementation = false; | |||
| 1663 | if (!ImplDecl->ivar_empty()) { | |||
| 1664 | SmallVector<SynthesizeIvarChunk, 16> layout; | |||
| 1665 | for (auto *IV : ImplDecl->ivars()) { | |||
| 1666 | if (IV->getSynthesize() && !IV->isInvalidDecl()) { | |||
| 1667 | layout.push_back(SynthesizeIvarChunk( | |||
| 1668 | IV->getASTContext().getTypeSize(IV->getType()), IV)); | |||
| 1669 | continue; | |||
| 1670 | } | |||
| 1671 | if (!data().IvarList) | |||
| 1672 | data().IvarList = IV; | |||
| 1673 | else | |||
| 1674 | curIvar->setNextIvar(IV); | |||
| 1675 | curIvar = IV; | |||
| 1676 | } | |||
| 1677 | ||||
| 1678 | if (!layout.empty()) { | |||
| 1679 | // Order synthesized ivars by their size. | |||
| 1680 | llvm::stable_sort(layout); | |||
| 1681 | unsigned Ix = 0, EIx = layout.size(); | |||
| 1682 | if (!data().IvarList) { | |||
| 1683 | data().IvarList = layout[0].Ivar; Ix++; | |||
| 1684 | curIvar = data().IvarList; | |||
| 1685 | } | |||
| 1686 | for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++) | |||
| 1687 | curIvar->setNextIvar(layout[Ix].Ivar); | |||
| ||||
| 1688 | } | |||
| 1689 | } | |||
| 1690 | } | |||
| 1691 | return data().IvarList; | |||
| 1692 | } | |||
| 1693 | ||||
| 1694 | /// FindCategoryDeclaration - Finds category declaration in the list of | |||
| 1695 | /// categories for this class and returns it. Name of the category is passed | |||
| 1696 | /// in 'CategoryId'. If category not found, return 0; | |||
| 1697 | /// | |||
| 1698 | ObjCCategoryDecl * | |||
| 1699 | ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const { | |||
| 1700 | // FIXME: Should make sure no callers ever do this. | |||
| 1701 | if (!hasDefinition()) | |||
| 1702 | return nullptr; | |||
| 1703 | ||||
| 1704 | if (data().ExternallyCompleted) | |||
| 1705 | LoadExternalDefinition(); | |||
| 1706 | ||||
| 1707 | for (auto *Cat : visible_categories()) | |||
| 1708 | if (Cat->getIdentifier() == CategoryId) | |||
| 1709 | return Cat; | |||
| 1710 | ||||
| 1711 | return nullptr; | |||
| 1712 | } | |||
| 1713 | ||||
| 1714 | ObjCMethodDecl * | |||
| 1715 | ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const { | |||
| 1716 | for (const auto *Cat : visible_categories()) { | |||
| 1717 | if (ObjCCategoryImplDecl *Impl = Cat->getImplementation()) | |||
| 1718 | if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel)) | |||
| 1719 | return MD; | |||
| 1720 | } | |||
| 1721 | ||||
| 1722 | return nullptr; | |||
| 1723 | } | |||
| 1724 | ||||
| 1725 | ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const { | |||
| 1726 | for (const auto *Cat : visible_categories()) { | |||
| 1727 | if (ObjCCategoryImplDecl *Impl = Cat->getImplementation()) | |||
| 1728 | if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel)) | |||
| 1729 | return MD; | |||
| 1730 | } | |||
| 1731 | ||||
| 1732 | return nullptr; | |||
| 1733 | } | |||
| 1734 | ||||
| 1735 | /// ClassImplementsProtocol - Checks that 'lProto' protocol | |||
| 1736 | /// has been implemented in IDecl class, its super class or categories (if | |||
| 1737 | /// lookupCategory is true). | |||
| 1738 | bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto, | |||
| 1739 | bool lookupCategory, | |||
| 1740 | bool RHSIsQualifiedID) { | |||
| 1741 | if (!hasDefinition()) | |||
| 1742 | return false; | |||
| 1743 | ||||
| 1744 | ObjCInterfaceDecl *IDecl = this; | |||
| 1745 | // 1st, look up the class. | |||
| 1746 | for (auto *PI : IDecl->protocols()){ | |||
| 1747 | if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI)) | |||
| 1748 | return true; | |||
| 1749 | // This is dubious and is added to be compatible with gcc. In gcc, it is | |||
| 1750 | // also allowed assigning a protocol-qualified 'id' type to a LHS object | |||
| 1751 | // when protocol in qualified LHS is in list of protocols in the rhs 'id' | |||
| 1752 | // object. This IMO, should be a bug. | |||
| 1753 | // FIXME: Treat this as an extension, and flag this as an error when GCC | |||
| 1754 | // extensions are not enabled. | |||
| 1755 | if (RHSIsQualifiedID && | |||
| 1756 | getASTContext().ProtocolCompatibleWithProtocol(PI, lProto)) | |||
| 1757 | return true; | |||
| 1758 | } | |||
| 1759 | ||||
| 1760 | // 2nd, look up the category. | |||
| 1761 | if (lookupCategory) | |||
| 1762 | for (const auto *Cat : visible_categories()) { | |||
| 1763 | for (auto *PI : Cat->protocols()) | |||
| 1764 | if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI)) | |||
| 1765 | return true; | |||
| 1766 | } | |||
| 1767 | ||||
| 1768 | // 3rd, look up the super class(s) | |||
| 1769 | if (IDecl->getSuperClass()) | |||
| 1770 | return | |||
| 1771 | IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory, | |||
| 1772 | RHSIsQualifiedID); | |||
| 1773 | ||||
| 1774 | return false; | |||
| 1775 | } | |||
| 1776 | ||||
| 1777 | //===----------------------------------------------------------------------===// | |||
| 1778 | // ObjCIvarDecl | |||
| 1779 | //===----------------------------------------------------------------------===// | |||
| 1780 | ||||
| 1781 | void ObjCIvarDecl::anchor() {} | |||
| 1782 | ||||
| 1783 | ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC, | |||
| 1784 | SourceLocation StartLoc, | |||
| 1785 | SourceLocation IdLoc, IdentifierInfo *Id, | |||
| 1786 | QualType T, TypeSourceInfo *TInfo, | |||
| 1787 | AccessControl ac, Expr *BW, | |||
| 1788 | bool synthesized) { | |||
| 1789 | if (DC) { | |||
| 1790 | // Ivar's can only appear in interfaces, implementations (via synthesized | |||
| 1791 | // properties), and class extensions (via direct declaration, or synthesized | |||
| 1792 | // properties). | |||
| 1793 | // | |||
| 1794 | // FIXME: This should really be asserting this: | |||
| 1795 | // (isa<ObjCCategoryDecl>(DC) && | |||
| 1796 | // cast<ObjCCategoryDecl>(DC)->IsClassExtension())) | |||
| 1797 | // but unfortunately we sometimes place ivars into non-class extension | |||
| 1798 | // categories on error. This breaks an AST invariant, and should not be | |||
| 1799 | // fixed. | |||
| 1800 | assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||((void)0) | |||
| 1801 | isa<ObjCCategoryDecl>(DC)) &&((void)0) | |||
| 1802 | "Invalid ivar decl context!")((void)0); | |||
| 1803 | // Once a new ivar is created in any of class/class-extension/implementation | |||
| 1804 | // decl contexts, the previously built IvarList must be rebuilt. | |||
| 1805 | auto *ID = dyn_cast<ObjCInterfaceDecl>(DC); | |||
| 1806 | if (!ID) { | |||
| 1807 | if (auto *IM = dyn_cast<ObjCImplementationDecl>(DC)) | |||
| 1808 | ID = IM->getClassInterface(); | |||
| 1809 | else | |||
| 1810 | ID = cast<ObjCCategoryDecl>(DC)->getClassInterface(); | |||
| 1811 | } | |||
| 1812 | ID->setIvarList(nullptr); | |||
| 1813 | } | |||
| 1814 | ||||
| 1815 | return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW, | |||
| 1816 | synthesized); | |||
| 1817 | } | |||
| 1818 | ||||
| 1819 | ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { | |||
| 1820 | return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(), | |||
| 1821 | nullptr, QualType(), nullptr, | |||
| 1822 | ObjCIvarDecl::None, nullptr, false); | |||
| 1823 | } | |||
| 1824 | ||||
| 1825 | const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const { | |||
| 1826 | const auto *DC = cast<ObjCContainerDecl>(getDeclContext()); | |||
| 1827 | ||||
| 1828 | switch (DC->getKind()) { | |||
| 1829 | default: | |||
| 1830 | case ObjCCategoryImpl: | |||
| 1831 | case ObjCProtocol: | |||
| 1832 | llvm_unreachable("invalid ivar container!")__builtin_unreachable(); | |||
| 1833 | ||||
| 1834 | // Ivars can only appear in class extension categories. | |||
| 1835 | case ObjCCategory: { | |||
| 1836 | const auto *CD = cast<ObjCCategoryDecl>(DC); | |||
| 1837 | assert(CD->IsClassExtension() && "invalid container for ivar!")((void)0); | |||
| 1838 | return CD->getClassInterface(); | |||
| 1839 | } | |||
| 1840 | ||||
| 1841 | case ObjCImplementation: | |||
| 1842 | return cast<ObjCImplementationDecl>(DC)->getClassInterface(); | |||
| 1843 | ||||
| 1844 | case ObjCInterface: | |||
| 1845 | return cast<ObjCInterfaceDecl>(DC); | |||
| 1846 | } | |||
| 1847 | } | |||
| 1848 | ||||
| 1849 | QualType ObjCIvarDecl::getUsageType(QualType objectType) const { | |||
| 1850 | return getType().substObjCMemberType(objectType, getDeclContext(), | |||
| 1851 | ObjCSubstitutionContext::Property); | |||
| 1852 | } | |||
| 1853 | ||||
| 1854 | //===----------------------------------------------------------------------===// | |||
| 1855 | // ObjCAtDefsFieldDecl | |||
| 1856 | //===----------------------------------------------------------------------===// | |||
| 1857 | ||||
| 1858 | void ObjCAtDefsFieldDecl::anchor() {} | |||
| 1859 | ||||
| 1860 | ObjCAtDefsFieldDecl | |||
| 1861 | *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, | |||
| 1862 | SourceLocation StartLoc, SourceLocation IdLoc, | |||
| 1863 | IdentifierInfo *Id, QualType T, Expr *BW) { | |||
| 1864 | return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW); | |||
| 1865 | } | |||
| 1866 | ||||
| 1867 | ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C, | |||
| 1868 | unsigned ID) { | |||
| 1869 | return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(), | |||
| 1870 | SourceLocation(), nullptr, QualType(), | |||
| 1871 | nullptr); | |||
| 1872 | } | |||
| 1873 | ||||
| 1874 | //===----------------------------------------------------------------------===// | |||
| 1875 | // ObjCProtocolDecl | |||
| 1876 | //===----------------------------------------------------------------------===// | |||
| 1877 | ||||
| 1878 | void ObjCProtocolDecl::anchor() {} | |||
| 1879 | ||||
| 1880 | ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC, | |||
| 1881 | IdentifierInfo *Id, SourceLocation nameLoc, | |||
| 1882 | SourceLocation atStartLoc, | |||
| 1883 | ObjCProtocolDecl *PrevDecl) | |||
| 1884 | : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), | |||
| 1885 | redeclarable_base(C) { | |||
| 1886 | setPreviousDecl(PrevDecl); | |||
| 1887 | if (PrevDecl) | |||
| 1888 | Data = PrevDecl->Data; | |||
| 1889 | } | |||
| 1890 | ||||
| 1891 | ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC, | |||
| 1892 | IdentifierInfo *Id, | |||
| 1893 | SourceLocation nameLoc, | |||
| 1894 | SourceLocation atStartLoc, | |||
| 1895 | ObjCProtocolDecl *PrevDecl) { | |||
| 1896 | auto *Result = | |||
| 1897 | new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl); | |||
| 1898 | Result->Data.setInt(!C.getLangOpts().Modules); | |||
| 1899 | return Result; | |||
| 1900 | } | |||
| 1901 | ||||
| 1902 | ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C, | |||
| 1903 | unsigned ID) { | |||
| 1904 | ObjCProtocolDecl *Result = | |||
| 1905 | new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(), | |||
| 1906 | SourceLocation(), nullptr); | |||
| 1907 | Result->Data.setInt(!C.getLangOpts().Modules); | |||
| 1908 | return Result; | |||
| 1909 | } | |||
| 1910 | ||||
| 1911 | bool ObjCProtocolDecl::isNonRuntimeProtocol() const { | |||
| 1912 | return hasAttr<ObjCNonRuntimeProtocolAttr>(); | |||
| 1913 | } | |||
| 1914 | ||||
| 1915 | void ObjCProtocolDecl::getImpliedProtocols( | |||
| 1916 | llvm::DenseSet<const ObjCProtocolDecl *> &IPs) const { | |||
| 1917 | std::queue<const ObjCProtocolDecl *> WorkQueue; | |||
| 1918 | WorkQueue.push(this); | |||
| 1919 | ||||
| 1920 | while (!WorkQueue.empty()) { | |||
| 1921 | const auto *PD = WorkQueue.front(); | |||
| 1922 | WorkQueue.pop(); | |||
| 1923 | for (const auto *Parent : PD->protocols()) { | |||
| 1924 | const auto *Can = Parent->getCanonicalDecl(); | |||
| 1925 | auto Result = IPs.insert(Can); | |||
| 1926 | if (Result.second) | |||
| 1927 | WorkQueue.push(Parent); | |||
| 1928 | } | |||
| 1929 | } | |||
| 1930 | } | |||
| 1931 | ||||
| 1932 | ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) { | |||
| 1933 | ObjCProtocolDecl *PDecl = this; | |||
| 1934 | ||||
| 1935 | if (Name == getIdentifier()) | |||
| 1936 | return PDecl; | |||
| 1937 | ||||
| 1938 | for (auto *I : protocols()) | |||
| 1939 | if ((PDecl = I->lookupProtocolNamed(Name))) | |||
| 1940 | return PDecl; | |||
| 1941 | ||||
| 1942 | return nullptr; | |||
| 1943 | } | |||
| 1944 | ||||
| 1945 | // lookupMethod - Lookup a instance/class method in the protocol and protocols | |||
| 1946 | // it inherited. | |||
| 1947 | ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel, | |||
| 1948 | bool isInstance) const { | |||
| 1949 | ObjCMethodDecl *MethodDecl = nullptr; | |||
| 1950 | ||||
| 1951 | // If there is no definition or the definition is hidden, we don't find | |||
| 1952 | // anything. | |||
| 1953 | const ObjCProtocolDecl *Def = getDefinition(); | |||
| 1954 | if (!Def || !Def->isUnconditionallyVisible()) | |||
| 1955 | return nullptr; | |||
| 1956 | ||||
| 1957 | if ((MethodDecl = getMethod(Sel, isInstance))) | |||
| 1958 | return MethodDecl; | |||
| 1959 | ||||
| 1960 | for (const auto *I : protocols()) | |||
| 1961 | if ((MethodDecl = I->lookupMethod(Sel, isInstance))) | |||
| 1962 | return MethodDecl; | |||
| 1963 | return nullptr; | |||
| 1964 | } | |||
| 1965 | ||||
| 1966 | void ObjCProtocolDecl::allocateDefinitionData() { | |||
| 1967 | assert(!Data.getPointer() && "Protocol already has a definition!")((void)0); | |||
| 1968 | Data.setPointer(new (getASTContext()) DefinitionData); | |||
| 1969 | Data.getPointer()->Definition = this; | |||
| 1970 | } | |||
| 1971 | ||||
| 1972 | void ObjCProtocolDecl::startDefinition() { | |||
| 1973 | allocateDefinitionData(); | |||
| 1974 | ||||
| 1975 | // Update all of the declarations with a pointer to the definition. | |||
| 1976 | for (auto *RD : redecls()) | |||
| 1977 | RD->Data = this->Data; | |||
| 1978 | } | |||
| 1979 | ||||
| 1980 | void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM, | |||
| 1981 | PropertyDeclOrder &PO) const { | |||
| 1982 | if (const ObjCProtocolDecl *PDecl = getDefinition()) { | |||
| 1983 | for (auto *Prop : PDecl->properties()) { | |||
| 1984 | // Insert into PM if not there already. | |||
| 1985 | PM.insert(std::make_pair( | |||
| 1986 | std::make_pair(Prop->getIdentifier(), Prop->isClassProperty()), | |||
| 1987 | Prop)); | |||
| 1988 | PO.push_back(Prop); | |||
| 1989 | } | |||
| 1990 | // Scan through protocol's protocols. | |||
| 1991 | for (const auto *PI : PDecl->protocols()) | |||
| 1992 | PI->collectPropertiesToImplement(PM, PO); | |||
| 1993 | } | |||
| 1994 | } | |||
| 1995 | ||||
| 1996 | void ObjCProtocolDecl::collectInheritedProtocolProperties( | |||
| 1997 | const ObjCPropertyDecl *Property, ProtocolPropertySet &PS, | |||
| 1998 | PropertyDeclOrder &PO) const { | |||
| 1999 | if (const ObjCProtocolDecl *PDecl = getDefinition()) { | |||
| 2000 | if (!PS.insert(PDecl).second) | |||
| 2001 | return; | |||
| 2002 | for (auto *Prop : PDecl->properties()) { | |||
| 2003 | if (Prop == Property) | |||
| 2004 | continue; | |||
| 2005 | if (Prop->getIdentifier() == Property->getIdentifier()) { | |||
| 2006 | PO.push_back(Prop); | |||
| 2007 | return; | |||
| 2008 | } | |||
| 2009 | } | |||
| 2010 | // Scan through protocol's protocols which did not have a matching property. | |||
| 2011 | for (const auto *PI : PDecl->protocols()) | |||
| 2012 | PI->collectInheritedProtocolProperties(Property, PS, PO); | |||
| 2013 | } | |||
| 2014 | } | |||
| 2015 | ||||
| 2016 | StringRef | |||
| 2017 | ObjCProtocolDecl::getObjCRuntimeNameAsString() const { | |||
| 2018 | if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>()) | |||
| 2019 | return ObjCRTName->getMetadataName(); | |||
| 2020 | ||||
| 2021 | return getName(); | |||
| 2022 | } | |||
| 2023 | ||||
| 2024 | //===----------------------------------------------------------------------===// | |||
| 2025 | // ObjCCategoryDecl | |||
| 2026 | //===----------------------------------------------------------------------===// | |||
| 2027 | ||||
| 2028 | void ObjCCategoryDecl::anchor() {} | |||
| 2029 | ||||
| 2030 | ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc, | |||
| 2031 | SourceLocation ClassNameLoc, | |||
| 2032 | SourceLocation CategoryNameLoc, | |||
| 2033 | IdentifierInfo *Id, ObjCInterfaceDecl *IDecl, | |||
| 2034 | ObjCTypeParamList *typeParamList, | |||
| 2035 | SourceLocation IvarLBraceLoc, | |||
| 2036 | SourceLocation IvarRBraceLoc) | |||
| 2037 | : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc), | |||
| 2038 | ClassInterface(IDecl), CategoryNameLoc(CategoryNameLoc), | |||
| 2039 | IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) { | |||
| 2040 | setTypeParamList(typeParamList); | |||
| 2041 | } | |||
| 2042 | ||||
| 2043 | ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC, | |||
| 2044 | SourceLocation AtLoc, | |||
| 2045 | SourceLocation ClassNameLoc, | |||
| 2046 | SourceLocation CategoryNameLoc, | |||
| 2047 | IdentifierInfo *Id, | |||
| 2048 | ObjCInterfaceDecl *IDecl, | |||
| 2049 | ObjCTypeParamList *typeParamList, | |||
| 2050 | SourceLocation IvarLBraceLoc, | |||
| 2051 | SourceLocation IvarRBraceLoc) { | |||
| 2052 | auto *CatDecl = | |||
| 2053 | new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id, | |||
| 2054 | IDecl, typeParamList, IvarLBraceLoc, | |||
| 2055 | IvarRBraceLoc); | |||
| 2056 | if (IDecl) { | |||
| 2057 | // Link this category into its class's category list. | |||
| 2058 | CatDecl->NextClassCategory = IDecl->getCategoryListRaw(); | |||
| 2059 | if (IDecl->hasDefinition()) { | |||
| 2060 | IDecl->setCategoryListRaw(CatDecl); | |||
| 2061 | if (ASTMutationListener *L = C.getASTMutationListener()) | |||
| 2062 | L->AddedObjCCategoryToInterface(CatDecl, IDecl); | |||
| 2063 | } | |||
| 2064 | } | |||
| 2065 | ||||
| 2066 | return CatDecl; | |||
| 2067 | } | |||
| 2068 | ||||
| 2069 | ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C, | |||
| 2070 | unsigned ID) { | |||
| 2071 | return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(), | |||
| 2072 | SourceLocation(), SourceLocation(), | |||
| 2073 | nullptr, nullptr, nullptr); | |||
| 2074 | } | |||
| 2075 | ||||
| 2076 | ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const { | |||
| 2077 | return getASTContext().getObjCImplementation( | |||
| 2078 | const_cast<ObjCCategoryDecl*>(this)); | |||
| 2079 | } | |||
| 2080 | ||||
| 2081 | void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) { | |||
| 2082 | getASTContext().setObjCImplementation(this, ImplD); | |||
| 2083 | } | |||
| 2084 | ||||
| 2085 | void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) { | |||
| 2086 | TypeParamList = TPL; | |||
| 2087 | if (!TPL) | |||
| 2088 | return; | |||
| 2089 | // Set the declaration context of each of the type parameters. | |||
| 2090 | for (auto *typeParam : *TypeParamList) | |||
| 2091 | typeParam->setDeclContext(this); | |||
| 2092 | } | |||
| 2093 | ||||
| 2094 | //===----------------------------------------------------------------------===// | |||
| 2095 | // ObjCCategoryImplDecl | |||
| 2096 | //===----------------------------------------------------------------------===// | |||
| 2097 | ||||
| 2098 | void ObjCCategoryImplDecl::anchor() {} | |||
| 2099 | ||||
| 2100 | ObjCCategoryImplDecl * | |||
| 2101 | ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC, | |||
| 2102 | IdentifierInfo *Id, | |||
| 2103 | ObjCInterfaceDecl *ClassInterface, | |||
| 2104 | SourceLocation nameLoc, | |||
| 2105 | SourceLocation atStartLoc, | |||
| 2106 | SourceLocation CategoryNameLoc) { | |||
| 2107 | if (ClassInterface && ClassInterface->hasDefinition()) | |||
| 2108 | ClassInterface = ClassInterface->getDefinition(); | |||
| 2109 | return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc, | |||
| 2110 | atStartLoc, CategoryNameLoc); | |||
| 2111 | } | |||
| 2112 | ||||
| 2113 | ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C, | |||
| 2114 | unsigned ID) { | |||
| 2115 | return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr, | |||
| 2116 | SourceLocation(), SourceLocation(), | |||
| 2117 | SourceLocation()); | |||
| 2118 | } | |||
| 2119 | ||||
| 2120 | ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const { | |||
| 2121 | // The class interface might be NULL if we are working with invalid code. | |||
| 2122 | if (const ObjCInterfaceDecl *ID = getClassInterface()) | |||
| 2123 | return ID->FindCategoryDeclaration(getIdentifier()); | |||
| 2124 | return nullptr; | |||
| 2125 | } | |||
| 2126 | ||||
| 2127 | void ObjCImplDecl::anchor() {} | |||
| 2128 | ||||
| 2129 | void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) { | |||
| 2130 | // FIXME: The context should be correct before we get here. | |||
| 2131 | property->setLexicalDeclContext(this); | |||
| 2132 | addDecl(property); | |||
| 2133 | } | |||
| 2134 | ||||
| 2135 | void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) { | |||
| 2136 | ASTContext &Ctx = getASTContext(); | |||
| 2137 | ||||
| 2138 | if (auto *ImplD = dyn_cast_or_null<ObjCImplementationDecl>(this)) { | |||
| 2139 | if (IFace) | |||
| 2140 | Ctx.setObjCImplementation(IFace, ImplD); | |||
| 2141 | ||||
| 2142 | } else if (auto *ImplD = dyn_cast_or_null<ObjCCategoryImplDecl>(this)) { | |||
| 2143 | if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier())) | |||
| 2144 | Ctx.setObjCImplementation(CD, ImplD); | |||
| 2145 | } | |||
| 2146 | ||||
| 2147 | ClassInterface = IFace; | |||
| 2148 | } | |||
| 2149 | ||||
| 2150 | /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of | |||
| 2151 | /// properties implemented in this \@implementation block and returns | |||
| 2152 | /// the implemented property that uses it. | |||
| 2153 | ObjCPropertyImplDecl *ObjCImplDecl:: | |||
| 2154 | FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const { | |||
| 2155 | for (auto *PID : property_impls()) | |||
| 2156 | if (PID->getPropertyIvarDecl() && | |||
| 2157 | PID->getPropertyIvarDecl()->getIdentifier() == ivarId) | |||
| 2158 | return PID; | |||
| 2159 | return nullptr; | |||
| 2160 | } | |||
| 2161 | ||||
| 2162 | /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl | |||
| 2163 | /// added to the list of those properties \@synthesized/\@dynamic in this | |||
| 2164 | /// category \@implementation block. | |||
| 2165 | ObjCPropertyImplDecl *ObjCImplDecl:: | |||
| 2166 | FindPropertyImplDecl(IdentifierInfo *Id, | |||
| 2167 | ObjCPropertyQueryKind QueryKind) const { | |||
| 2168 | ObjCPropertyImplDecl *ClassPropImpl = nullptr; | |||
| 2169 | for (auto *PID : property_impls()) | |||
| 2170 | // If queryKind is unknown, we return the instance property if one | |||
| 2171 | // exists; otherwise we return the class property. | |||
| 2172 | if (PID->getPropertyDecl()->getIdentifier() == Id) { | |||
| 2173 | if ((QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown && | |||
| 2174 | !PID->getPropertyDecl()->isClassProperty()) || | |||
| 2175 | (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_class && | |||
| 2176 | PID->getPropertyDecl()->isClassProperty()) || | |||
| 2177 | (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance && | |||
| 2178 | !PID->getPropertyDecl()->isClassProperty())) | |||
| 2179 | return PID; | |||
| 2180 | ||||
| 2181 | if (PID->getPropertyDecl()->isClassProperty()) | |||
| 2182 | ClassPropImpl = PID; | |||
| 2183 | } | |||
| 2184 | ||||
| 2185 | if (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown) | |||
| 2186 | // We can't find the instance property, return the class property. | |||
| 2187 | return ClassPropImpl; | |||
| 2188 | ||||
| 2189 | return nullptr; | |||
| 2190 | } | |||
| 2191 | ||||
| 2192 | raw_ostream &clang::operator<<(raw_ostream &OS, | |||
| 2193 | const ObjCCategoryImplDecl &CID) { | |||
| 2194 | OS << CID.getName(); | |||
| 2195 | return OS; | |||
| 2196 | } | |||
| 2197 | ||||
| 2198 | //===----------------------------------------------------------------------===// | |||
| 2199 | // ObjCImplementationDecl | |||
| 2200 | //===----------------------------------------------------------------------===// | |||
| 2201 | ||||
| 2202 | void ObjCImplementationDecl::anchor() {} | |||
| 2203 | ||||
| 2204 | ObjCImplementationDecl * | |||
| 2205 | ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC, | |||
| 2206 | ObjCInterfaceDecl *ClassInterface, | |||
| 2207 | ObjCInterfaceDecl *SuperDecl, | |||
| 2208 | SourceLocation nameLoc, | |||
| 2209 | SourceLocation atStartLoc, | |||
| 2210 | SourceLocation superLoc, | |||
| 2211 | SourceLocation IvarLBraceLoc, | |||
| 2212 | SourceLocation IvarRBraceLoc) { | |||
| 2213 | if (ClassInterface && ClassInterface->hasDefinition()) | |||
| 2214 | ClassInterface = ClassInterface->getDefinition(); | |||
| 2215 | return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl, | |||
| 2216 | nameLoc, atStartLoc, superLoc, | |||
| 2217 | IvarLBraceLoc, IvarRBraceLoc); | |||
| 2218 | } | |||
| 2219 | ||||
| 2220 | ObjCImplementationDecl * | |||
| 2221 | ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) { | |||
| 2222 | return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr, | |||
| 2223 | SourceLocation(), SourceLocation()); | |||
| 2224 | } | |||
| 2225 | ||||
| 2226 | void ObjCImplementationDecl::setIvarInitializers(ASTContext &C, | |||
| 2227 | CXXCtorInitializer ** initializers, | |||
| 2228 | unsigned numInitializers) { | |||
| 2229 | if (numInitializers > 0) { | |||
| 2230 | NumIvarInitializers = numInitializers; | |||
| 2231 | auto **ivarInitializers = new (C) CXXCtorInitializer*[NumIvarInitializers]; | |||
| 2232 | memcpy(ivarInitializers, initializers, | |||
| 2233 | numInitializers * sizeof(CXXCtorInitializer*)); | |||
| 2234 | IvarInitializers = ivarInitializers; | |||
| 2235 | } | |||
| 2236 | } | |||
| 2237 | ||||
| 2238 | ObjCImplementationDecl::init_const_iterator | |||
| 2239 | ObjCImplementationDecl::init_begin() const { | |||
| 2240 | return IvarInitializers.get(getASTContext().getExternalSource()); | |||
| 2241 | } | |||
| 2242 | ||||
| 2243 | raw_ostream &clang::operator<<(raw_ostream &OS, | |||
| 2244 | const ObjCImplementationDecl &ID) { | |||
| 2245 | OS << ID.getName(); | |||
| 2246 | return OS; | |||
| 2247 | } | |||
| 2248 | ||||
| 2249 | //===----------------------------------------------------------------------===// | |||
| 2250 | // ObjCCompatibleAliasDecl | |||
| 2251 | //===----------------------------------------------------------------------===// | |||
| 2252 | ||||
| 2253 | void ObjCCompatibleAliasDecl::anchor() {} | |||
| 2254 | ||||
| 2255 | ObjCCompatibleAliasDecl * | |||
| 2256 | ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC, | |||
| 2257 | SourceLocation L, | |||
| 2258 | IdentifierInfo *Id, | |||
| 2259 | ObjCInterfaceDecl* AliasedClass) { | |||
| 2260 | return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass); | |||
| 2261 | } | |||
| 2262 | ||||
| 2263 | ObjCCompatibleAliasDecl * | |||
| 2264 | ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { | |||
| 2265 | return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(), | |||
| 2266 | nullptr, nullptr); | |||
| 2267 | } | |||
| 2268 | ||||
| 2269 | //===----------------------------------------------------------------------===// | |||
| 2270 | // ObjCPropertyDecl | |||
| 2271 | //===----------------------------------------------------------------------===// | |||
| 2272 | ||||
| 2273 | void ObjCPropertyDecl::anchor() {} | |||
| 2274 | ||||
| 2275 | ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC, | |||
| 2276 | SourceLocation L, | |||
| 2277 | IdentifierInfo *Id, | |||
| 2278 | SourceLocation AtLoc, | |||
| 2279 | SourceLocation LParenLoc, | |||
| 2280 | QualType T, | |||
| 2281 | TypeSourceInfo *TSI, | |||
| 2282 | PropertyControl propControl) { | |||
| 2283 | return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI, | |||
| 2284 | propControl); | |||
| 2285 | } | |||
| 2286 | ||||
| 2287 | ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C, | |||
| 2288 | unsigned ID) { | |||
| 2289 | return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr, | |||
| 2290 | SourceLocation(), SourceLocation(), | |||
| 2291 | QualType(), nullptr, None); | |||
| 2292 | } | |||
| 2293 | ||||
| 2294 | QualType ObjCPropertyDecl::getUsageType(QualType objectType) const { | |||
| 2295 | return DeclType.substObjCMemberType(objectType, getDeclContext(), | |||
| 2296 | ObjCSubstitutionContext::Property); | |||
| 2297 | } | |||
| 2298 | ||||
| 2299 | bool ObjCPropertyDecl::isDirectProperty() const { | |||
| 2300 | return (PropertyAttributes & ObjCPropertyAttribute::kind_direct) && | |||
| 2301 | !getASTContext().getLangOpts().ObjCDisableDirectMethodsForTesting; | |||
| 2302 | } | |||
| 2303 | ||||
| 2304 | //===----------------------------------------------------------------------===// | |||
| 2305 | // ObjCPropertyImplDecl | |||
| 2306 | //===----------------------------------------------------------------------===// | |||
| 2307 | ||||
| 2308 | ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C, | |||
| 2309 | DeclContext *DC, | |||
| 2310 | SourceLocation atLoc, | |||
| 2311 | SourceLocation L, | |||
| 2312 | ObjCPropertyDecl *property, | |||
| 2313 | Kind PK, | |||
| 2314 | ObjCIvarDecl *ivar, | |||
| 2315 | SourceLocation ivarLoc) { | |||
| 2316 | return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar, | |||
| 2317 | ivarLoc); | |||
| 2318 | } | |||
| 2319 | ||||
| 2320 | ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C, | |||
| 2321 | unsigned ID) { | |||
| 2322 | return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(), | |||
| 2323 | SourceLocation(), nullptr, Dynamic, | |||
| 2324 | nullptr, SourceLocation()); | |||
| 2325 | } | |||
| 2326 | ||||
| 2327 | SourceRange ObjCPropertyImplDecl::getSourceRange() const { | |||
| 2328 | SourceLocation EndLoc = getLocation(); | |||
| 2329 | if (IvarLoc.isValid()) | |||
| 2330 | EndLoc = IvarLoc; | |||
| 2331 | ||||
| 2332 | return SourceRange(AtLoc, EndLoc); | |||
| 2333 | } |
| 1 | //===- DeclObjC.h - Classes for representing declarations -------*- C++ -*-===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the DeclObjC interface and subclasses. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_CLANG_AST_DECLOBJC_H |
| 14 | #define LLVM_CLANG_AST_DECLOBJC_H |
| 15 | |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/DeclBase.h" |
| 18 | #include "clang/AST/DeclObjCCommon.h" |
| 19 | #include "clang/AST/ExternalASTSource.h" |
| 20 | #include "clang/AST/Redeclarable.h" |
| 21 | #include "clang/AST/SelectorLocationsKind.h" |
| 22 | #include "clang/AST/Type.h" |
| 23 | #include "clang/Basic/IdentifierTable.h" |
| 24 | #include "clang/Basic/LLVM.h" |
| 25 | #include "clang/Basic/SourceLocation.h" |
| 26 | #include "clang/Basic/Specifiers.h" |
| 27 | #include "llvm/ADT/ArrayRef.h" |
| 28 | #include "llvm/ADT/DenseMap.h" |
| 29 | #include "llvm/ADT/DenseSet.h" |
| 30 | #include "llvm/ADT/None.h" |
| 31 | #include "llvm/ADT/PointerIntPair.h" |
| 32 | #include "llvm/ADT/STLExtras.h" |
| 33 | #include "llvm/ADT/StringRef.h" |
| 34 | #include "llvm/ADT/iterator_range.h" |
| 35 | #include "llvm/Support/Compiler.h" |
| 36 | #include "llvm/Support/TrailingObjects.h" |
| 37 | #include <cassert> |
| 38 | #include <cstddef> |
| 39 | #include <cstdint> |
| 40 | #include <iterator> |
| 41 | #include <string> |
| 42 | #include <utility> |
| 43 | |
| 44 | namespace clang { |
| 45 | |
| 46 | class ASTContext; |
| 47 | class CompoundStmt; |
| 48 | class CXXCtorInitializer; |
| 49 | class Expr; |
| 50 | class ObjCCategoryDecl; |
| 51 | class ObjCCategoryImplDecl; |
| 52 | class ObjCImplementationDecl; |
| 53 | class ObjCInterfaceDecl; |
| 54 | class ObjCIvarDecl; |
| 55 | class ObjCPropertyDecl; |
| 56 | class ObjCPropertyImplDecl; |
| 57 | class ObjCProtocolDecl; |
| 58 | class Stmt; |
| 59 | |
| 60 | class ObjCListBase { |
| 61 | protected: |
| 62 | /// List is an array of pointers to objects that are not owned by this object. |
| 63 | void **List = nullptr; |
| 64 | unsigned NumElts = 0; |
| 65 | |
| 66 | public: |
| 67 | ObjCListBase() = default; |
| 68 | ObjCListBase(const ObjCListBase &) = delete; |
| 69 | ObjCListBase &operator=(const ObjCListBase &) = delete; |
| 70 | |
| 71 | unsigned size() const { return NumElts; } |
| 72 | bool empty() const { return NumElts == 0; } |
| 73 | |
| 74 | protected: |
| 75 | void set(void *const* InList, unsigned Elts, ASTContext &Ctx); |
| 76 | }; |
| 77 | |
| 78 | /// ObjCList - This is a simple template class used to hold various lists of |
| 79 | /// decls etc, which is heavily used by the ObjC front-end. This only use case |
| 80 | /// this supports is setting the list all at once and then reading elements out |
| 81 | /// of it. |
| 82 | template <typename T> |
| 83 | class ObjCList : public ObjCListBase { |
| 84 | public: |
| 85 | void set(T* const* InList, unsigned Elts, ASTContext &Ctx) { |
| 86 | ObjCListBase::set(reinterpret_cast<void*const*>(InList), Elts, Ctx); |
| 87 | } |
| 88 | |
| 89 | using iterator = T* const *; |
| 90 | |
| 91 | iterator begin() const { return (iterator)List; } |
| 92 | iterator end() const { return (iterator)List+NumElts; } |
| 93 | |
| 94 | T* operator[](unsigned Idx) const { |
| 95 | assert(Idx < NumElts && "Invalid access")((void)0); |
| 96 | return (T*)List[Idx]; |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | /// A list of Objective-C protocols, along with the source |
| 101 | /// locations at which they were referenced. |
| 102 | class ObjCProtocolList : public ObjCList<ObjCProtocolDecl> { |
| 103 | SourceLocation *Locations = nullptr; |
| 104 | |
| 105 | using ObjCList<ObjCProtocolDecl>::set; |
| 106 | |
| 107 | public: |
| 108 | ObjCProtocolList() = default; |
| 109 | |
| 110 | using loc_iterator = const SourceLocation *; |
| 111 | |
| 112 | loc_iterator loc_begin() const { return Locations; } |
| 113 | loc_iterator loc_end() const { return Locations + size(); } |
| 114 | |
| 115 | void set(ObjCProtocolDecl* const* InList, unsigned Elts, |
| 116 | const SourceLocation *Locs, ASTContext &Ctx); |
| 117 | }; |
| 118 | |
| 119 | /// ObjCMethodDecl - Represents an instance or class method declaration. |
| 120 | /// ObjC methods can be declared within 4 contexts: class interfaces, |
| 121 | /// categories, protocols, and class implementations. While C++ member |
| 122 | /// functions leverage C syntax, Objective-C method syntax is modeled after |
| 123 | /// Smalltalk (using colons to specify argument types/expressions). |
| 124 | /// Here are some brief examples: |
| 125 | /// |
| 126 | /// Setter/getter instance methods: |
| 127 | /// - (void)setMenu:(NSMenu *)menu; |
| 128 | /// - (NSMenu *)menu; |
| 129 | /// |
| 130 | /// Instance method that takes 2 NSView arguments: |
| 131 | /// - (void)replaceSubview:(NSView *)oldView with:(NSView *)newView; |
| 132 | /// |
| 133 | /// Getter class method: |
| 134 | /// + (NSMenu *)defaultMenu; |
| 135 | /// |
| 136 | /// A selector represents a unique name for a method. The selector names for |
| 137 | /// the above methods are setMenu:, menu, replaceSubview:with:, and defaultMenu. |
| 138 | /// |
| 139 | class ObjCMethodDecl : public NamedDecl, public DeclContext { |
| 140 | // This class stores some data in DeclContext::ObjCMethodDeclBits |
| 141 | // to save some space. Use the provided accessors to access it. |
| 142 | |
| 143 | public: |
| 144 | enum ImplementationControl { None, Required, Optional }; |
| 145 | |
| 146 | private: |
| 147 | /// Return type of this method. |
| 148 | QualType MethodDeclType; |
| 149 | |
| 150 | /// Type source information for the return type. |
| 151 | TypeSourceInfo *ReturnTInfo; |
| 152 | |
| 153 | /// Array of ParmVarDecls for the formal parameters of this method |
| 154 | /// and optionally followed by selector locations. |
| 155 | void *ParamsAndSelLocs = nullptr; |
| 156 | unsigned NumParams = 0; |
| 157 | |
| 158 | /// List of attributes for this method declaration. |
| 159 | SourceLocation DeclEndLoc; // the location of the ';' or '{'. |
| 160 | |
| 161 | /// The following are only used for method definitions, null otherwise. |
| 162 | LazyDeclStmtPtr Body; |
| 163 | |
| 164 | /// SelfDecl - Decl for the implicit self parameter. This is lazily |
| 165 | /// constructed by createImplicitParams. |
| 166 | ImplicitParamDecl *SelfDecl = nullptr; |
| 167 | |
| 168 | /// CmdDecl - Decl for the implicit _cmd parameter. This is lazily |
| 169 | /// constructed by createImplicitParams. |
| 170 | ImplicitParamDecl *CmdDecl = nullptr; |
| 171 | |
| 172 | ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc, |
| 173 | Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo, |
| 174 | DeclContext *contextDecl, bool isInstance = true, |
| 175 | bool isVariadic = false, bool isPropertyAccessor = false, |
| 176 | bool isSynthesizedAccessorStub = false, |
| 177 | bool isImplicitlyDeclared = false, bool isDefined = false, |
| 178 | ImplementationControl impControl = None, |
| 179 | bool HasRelatedResultType = false); |
| 180 | |
| 181 | SelectorLocationsKind getSelLocsKind() const { |
| 182 | return static_cast<SelectorLocationsKind>(ObjCMethodDeclBits.SelLocsKind); |
| 183 | } |
| 184 | |
| 185 | void setSelLocsKind(SelectorLocationsKind Kind) { |
| 186 | ObjCMethodDeclBits.SelLocsKind = Kind; |
| 187 | } |
| 188 | |
| 189 | bool hasStandardSelLocs() const { |
| 190 | return getSelLocsKind() != SelLoc_NonStandard; |
| 191 | } |
| 192 | |
| 193 | /// Get a pointer to the stored selector identifiers locations array. |
| 194 | /// No locations will be stored if HasStandardSelLocs is true. |
| 195 | SourceLocation *getStoredSelLocs() { |
| 196 | return reinterpret_cast<SourceLocation *>(getParams() + NumParams); |
| 197 | } |
| 198 | const SourceLocation *getStoredSelLocs() const { |
| 199 | return reinterpret_cast<const SourceLocation *>(getParams() + NumParams); |
| 200 | } |
| 201 | |
| 202 | /// Get a pointer to the stored selector identifiers locations array. |
| 203 | /// No locations will be stored if HasStandardSelLocs is true. |
| 204 | ParmVarDecl **getParams() { |
| 205 | return reinterpret_cast<ParmVarDecl **>(ParamsAndSelLocs); |
| 206 | } |
| 207 | const ParmVarDecl *const *getParams() const { |
| 208 | return reinterpret_cast<const ParmVarDecl *const *>(ParamsAndSelLocs); |
| 209 | } |
| 210 | |
| 211 | /// Get the number of stored selector identifiers locations. |
| 212 | /// No locations will be stored if HasStandardSelLocs is true. |
| 213 | unsigned getNumStoredSelLocs() const { |
| 214 | if (hasStandardSelLocs()) |
| 215 | return 0; |
| 216 | return getNumSelectorLocs(); |
| 217 | } |
| 218 | |
| 219 | void setParamsAndSelLocs(ASTContext &C, |
| 220 | ArrayRef<ParmVarDecl*> Params, |
| 221 | ArrayRef<SourceLocation> SelLocs); |
| 222 | |
| 223 | /// A definition will return its interface declaration. |
| 224 | /// An interface declaration will return its definition. |
| 225 | /// Otherwise it will return itself. |
| 226 | ObjCMethodDecl *getNextRedeclarationImpl() override; |
| 227 | |
| 228 | public: |
| 229 | friend class ASTDeclReader; |
| 230 | friend class ASTDeclWriter; |
| 231 | |
| 232 | static ObjCMethodDecl * |
| 233 | Create(ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc, |
| 234 | Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo, |
| 235 | DeclContext *contextDecl, bool isInstance = true, |
| 236 | bool isVariadic = false, bool isPropertyAccessor = false, |
| 237 | bool isSynthesizedAccessorStub = false, |
| 238 | bool isImplicitlyDeclared = false, bool isDefined = false, |
| 239 | ImplementationControl impControl = None, |
| 240 | bool HasRelatedResultType = false); |
| 241 | |
| 242 | static ObjCMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
| 243 | |
| 244 | ObjCMethodDecl *getCanonicalDecl() override; |
| 245 | const ObjCMethodDecl *getCanonicalDecl() const { |
| 246 | return const_cast<ObjCMethodDecl*>(this)->getCanonicalDecl(); |
| 247 | } |
| 248 | |
| 249 | ObjCDeclQualifier getObjCDeclQualifier() const { |
| 250 | return static_cast<ObjCDeclQualifier>(ObjCMethodDeclBits.objcDeclQualifier); |
| 251 | } |
| 252 | |
| 253 | void setObjCDeclQualifier(ObjCDeclQualifier QV) { |
| 254 | ObjCMethodDeclBits.objcDeclQualifier = QV; |
| 255 | } |
| 256 | |
| 257 | /// Determine whether this method has a result type that is related |
| 258 | /// to the message receiver's type. |
| 259 | bool hasRelatedResultType() const { |
| 260 | return ObjCMethodDeclBits.RelatedResultType; |
| 261 | } |
| 262 | |
| 263 | /// Note whether this method has a related result type. |
| 264 | void setRelatedResultType(bool RRT = true) { |
| 265 | ObjCMethodDeclBits.RelatedResultType = RRT; |
| 266 | } |
| 267 | |
| 268 | /// True if this is a method redeclaration in the same interface. |
| 269 | bool isRedeclaration() const { return ObjCMethodDeclBits.IsRedeclaration; } |
| 270 | void setIsRedeclaration(bool RD) { ObjCMethodDeclBits.IsRedeclaration = RD; } |
| 271 | void setAsRedeclaration(const ObjCMethodDecl *PrevMethod); |
| 272 | |
| 273 | /// True if redeclared in the same interface. |
| 274 | bool hasRedeclaration() const { return ObjCMethodDeclBits.HasRedeclaration; } |
| 275 | void setHasRedeclaration(bool HRD) const { |
| 276 | ObjCMethodDeclBits.HasRedeclaration = HRD; |
| 277 | } |
| 278 | |
| 279 | /// Returns the location where the declarator ends. It will be |
| 280 | /// the location of ';' for a method declaration and the location of '{' |
| 281 | /// for a method definition. |
| 282 | SourceLocation getDeclaratorEndLoc() const { return DeclEndLoc; } |
| 283 | |
| 284 | // Location information, modeled after the Stmt API. |
| 285 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return getLocation(); } |
| 286 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)); |
| 287 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
| 288 | return SourceRange(getLocation(), getEndLoc()); |
| 289 | } |
| 290 | |
| 291 | SourceLocation getSelectorStartLoc() const { |
| 292 | if (isImplicit()) |
| 293 | return getBeginLoc(); |
| 294 | return getSelectorLoc(0); |
| 295 | } |
| 296 | |
| 297 | SourceLocation getSelectorLoc(unsigned Index) const { |
| 298 | assert(Index < getNumSelectorLocs() && "Index out of range!")((void)0); |
| 299 | if (hasStandardSelLocs()) |
| 300 | return getStandardSelectorLoc(Index, getSelector(), |
| 301 | getSelLocsKind() == SelLoc_StandardWithSpace, |
| 302 | parameters(), |
| 303 | DeclEndLoc); |
| 304 | return getStoredSelLocs()[Index]; |
| 305 | } |
| 306 | |
| 307 | void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const; |
| 308 | |
| 309 | unsigned getNumSelectorLocs() const { |
| 310 | if (isImplicit()) |
| 311 | return 0; |
| 312 | Selector Sel = getSelector(); |
| 313 | if (Sel.isUnarySelector()) |
| 314 | return 1; |
| 315 | return Sel.getNumArgs(); |
| 316 | } |
| 317 | |
| 318 | ObjCInterfaceDecl *getClassInterface(); |
| 319 | const ObjCInterfaceDecl *getClassInterface() const { |
| 320 | return const_cast<ObjCMethodDecl*>(this)->getClassInterface(); |
| 321 | } |
| 322 | |
| 323 | /// If this method is declared or implemented in a category, return |
| 324 | /// that category. |
| 325 | ObjCCategoryDecl *getCategory(); |
| 326 | const ObjCCategoryDecl *getCategory() const { |
| 327 | return const_cast<ObjCMethodDecl*>(this)->getCategory(); |
| 328 | } |
| 329 | |
| 330 | Selector getSelector() const { return getDeclName().getObjCSelector(); } |
| 331 | |
| 332 | QualType getReturnType() const { return MethodDeclType; } |
| 333 | void setReturnType(QualType T) { MethodDeclType = T; } |
| 334 | SourceRange getReturnTypeSourceRange() const; |
| 335 | |
| 336 | /// Determine the type of an expression that sends a message to this |
| 337 | /// function. This replaces the type parameters with the types they would |
| 338 | /// get if the receiver was parameterless (e.g. it may replace the type |
| 339 | /// parameter with 'id'). |
| 340 | QualType getSendResultType() const; |
| 341 | |
| 342 | /// Determine the type of an expression that sends a message to this |
| 343 | /// function with the given receiver type. |
| 344 | QualType getSendResultType(QualType receiverType) const; |
| 345 | |
| 346 | TypeSourceInfo *getReturnTypeSourceInfo() const { return ReturnTInfo; } |
| 347 | void setReturnTypeSourceInfo(TypeSourceInfo *TInfo) { ReturnTInfo = TInfo; } |
| 348 | |
| 349 | // Iterator access to formal parameters. |
| 350 | unsigned param_size() const { return NumParams; } |
| 351 | |
| 352 | using param_const_iterator = const ParmVarDecl *const *; |
| 353 | using param_iterator = ParmVarDecl *const *; |
| 354 | using param_range = llvm::iterator_range<param_iterator>; |
| 355 | using param_const_range = llvm::iterator_range<param_const_iterator>; |
| 356 | |
| 357 | param_const_iterator param_begin() const { |
| 358 | return param_const_iterator(getParams()); |
| 359 | } |
| 360 | |
| 361 | param_const_iterator param_end() const { |
| 362 | return param_const_iterator(getParams() + NumParams); |
| 363 | } |
| 364 | |
| 365 | param_iterator param_begin() { return param_iterator(getParams()); } |
| 366 | param_iterator param_end() { return param_iterator(getParams() + NumParams); } |
| 367 | |
| 368 | // This method returns and of the parameters which are part of the selector |
| 369 | // name mangling requirements. |
| 370 | param_const_iterator sel_param_end() const { |
| 371 | return param_begin() + getSelector().getNumArgs(); |
| 372 | } |
| 373 | |
| 374 | // ArrayRef access to formal parameters. This should eventually |
| 375 | // replace the iterator interface above. |
| 376 | ArrayRef<ParmVarDecl*> parameters() const { |
| 377 | return llvm::makeArrayRef(const_cast<ParmVarDecl**>(getParams()), |
| 378 | NumParams); |
| 379 | } |
| 380 | |
| 381 | ParmVarDecl *getParamDecl(unsigned Idx) { |
| 382 | assert(Idx < NumParams && "Index out of bounds!")((void)0); |
| 383 | return getParams()[Idx]; |
| 384 | } |
| 385 | const ParmVarDecl *getParamDecl(unsigned Idx) const { |
| 386 | return const_cast<ObjCMethodDecl *>(this)->getParamDecl(Idx); |
| 387 | } |
| 388 | |
| 389 | /// Sets the method's parameters and selector source locations. |
| 390 | /// If the method is implicit (not coming from source) \p SelLocs is |
| 391 | /// ignored. |
| 392 | void setMethodParams(ASTContext &C, |
| 393 | ArrayRef<ParmVarDecl*> Params, |
| 394 | ArrayRef<SourceLocation> SelLocs = llvm::None); |
| 395 | |
| 396 | // Iterator access to parameter types. |
| 397 | struct GetTypeFn { |
| 398 | QualType operator()(const ParmVarDecl *PD) const { return PD->getType(); } |
| 399 | }; |
| 400 | |
| 401 | using param_type_iterator = |
| 402 | llvm::mapped_iterator<param_const_iterator, GetTypeFn>; |
| 403 | |
| 404 | param_type_iterator param_type_begin() const { |
| 405 | return llvm::map_iterator(param_begin(), GetTypeFn()); |
| 406 | } |
| 407 | |
| 408 | param_type_iterator param_type_end() const { |
| 409 | return llvm::map_iterator(param_end(), GetTypeFn()); |
| 410 | } |
| 411 | |
| 412 | /// createImplicitParams - Used to lazily create the self and cmd |
| 413 | /// implicit parameters. This must be called prior to using getSelfDecl() |
| 414 | /// or getCmdDecl(). The call is ignored if the implicit parameters |
| 415 | /// have already been created. |
| 416 | void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID); |
| 417 | |
| 418 | /// \return the type for \c self and set \arg selfIsPseudoStrong and |
| 419 | /// \arg selfIsConsumed accordingly. |
| 420 | QualType getSelfType(ASTContext &Context, const ObjCInterfaceDecl *OID, |
| 421 | bool &selfIsPseudoStrong, bool &selfIsConsumed) const; |
| 422 | |
| 423 | ImplicitParamDecl * getSelfDecl() const { return SelfDecl; } |
| 424 | void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; } |
| 425 | ImplicitParamDecl * getCmdDecl() const { return CmdDecl; } |
| 426 | void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; } |
| 427 | |
| 428 | /// Determines the family of this method. |
| 429 | ObjCMethodFamily getMethodFamily() const; |
| 430 | |
| 431 | bool isInstanceMethod() const { return ObjCMethodDeclBits.IsInstance; } |
| 432 | void setInstanceMethod(bool isInst) { |
| 433 | ObjCMethodDeclBits.IsInstance = isInst; |
| 434 | } |
| 435 | |
| 436 | bool isVariadic() const { return ObjCMethodDeclBits.IsVariadic; } |
| 437 | void setVariadic(bool isVar) { ObjCMethodDeclBits.IsVariadic = isVar; } |
| 438 | |
| 439 | bool isClassMethod() const { return !isInstanceMethod(); } |
| 440 | |
| 441 | bool isPropertyAccessor() const { |
| 442 | return ObjCMethodDeclBits.IsPropertyAccessor; |
| 443 | } |
| 444 | |
| 445 | void setPropertyAccessor(bool isAccessor) { |
| 446 | ObjCMethodDeclBits.IsPropertyAccessor = isAccessor; |
| 447 | } |
| 448 | |
| 449 | bool isSynthesizedAccessorStub() const { |
| 450 | return ObjCMethodDeclBits.IsSynthesizedAccessorStub; |
| 451 | } |
| 452 | |
| 453 | void setSynthesizedAccessorStub(bool isSynthesizedAccessorStub) { |
| 454 | ObjCMethodDeclBits.IsSynthesizedAccessorStub = isSynthesizedAccessorStub; |
| 455 | } |
| 456 | |
| 457 | bool isDefined() const { return ObjCMethodDeclBits.IsDefined; } |
| 458 | void setDefined(bool isDefined) { ObjCMethodDeclBits.IsDefined = isDefined; } |
| 459 | |
| 460 | /// Whether this method overrides any other in the class hierarchy. |
| 461 | /// |
| 462 | /// A method is said to override any method in the class's |
| 463 | /// base classes, its protocols, or its categories' protocols, that has |
| 464 | /// the same selector and is of the same kind (class or instance). |
| 465 | /// A method in an implementation is not considered as overriding the same |
| 466 | /// method in the interface or its categories. |
| 467 | bool isOverriding() const { return ObjCMethodDeclBits.IsOverriding; } |
| 468 | void setOverriding(bool IsOver) { ObjCMethodDeclBits.IsOverriding = IsOver; } |
| 469 | |
| 470 | /// Return overridden methods for the given \p Method. |
| 471 | /// |
| 472 | /// An ObjC method is considered to override any method in the class's |
| 473 | /// base classes (and base's categories), its protocols, or its categories' |
| 474 | /// protocols, that has |
| 475 | /// the same selector and is of the same kind (class or instance). |
| 476 | /// A method in an implementation is not considered as overriding the same |
| 477 | /// method in the interface or its categories. |
| 478 | void getOverriddenMethods( |
| 479 | SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const; |
| 480 | |
| 481 | /// True if the method was a definition but its body was skipped. |
| 482 | bool hasSkippedBody() const { return ObjCMethodDeclBits.HasSkippedBody; } |
| 483 | void setHasSkippedBody(bool Skipped = true) { |
| 484 | ObjCMethodDeclBits.HasSkippedBody = Skipped; |
| 485 | } |
| 486 | |
| 487 | /// True if the method is tagged as objc_direct |
| 488 | bool isDirectMethod() const; |
| 489 | |
| 490 | /// Returns the property associated with this method's selector. |
| 491 | /// |
| 492 | /// Note that even if this particular method is not marked as a property |
| 493 | /// accessor, it is still possible for it to match a property declared in a |
| 494 | /// superclass. Pass \c false if you only want to check the current class. |
| 495 | const ObjCPropertyDecl *findPropertyDecl(bool CheckOverrides = true) const; |
| 496 | |
| 497 | // Related to protocols declared in \@protocol |
| 498 | void setDeclImplementation(ImplementationControl ic) { |
| 499 | ObjCMethodDeclBits.DeclImplementation = ic; |
| 500 | } |
| 501 | |
| 502 | ImplementationControl getImplementationControl() const { |
| 503 | return ImplementationControl(ObjCMethodDeclBits.DeclImplementation); |
| 504 | } |
| 505 | |
| 506 | bool isOptional() const { |
| 507 | return getImplementationControl() == Optional; |
| 508 | } |
| 509 | |
| 510 | /// Returns true if this specific method declaration is marked with the |
| 511 | /// designated initializer attribute. |
| 512 | bool isThisDeclarationADesignatedInitializer() const; |
| 513 | |
| 514 | /// Returns true if the method selector resolves to a designated initializer |
| 515 | /// in the class's interface. |
| 516 | /// |
| 517 | /// \param InitMethod if non-null and the function returns true, it receives |
| 518 | /// the method declaration that was marked with the designated initializer |
| 519 | /// attribute. |
| 520 | bool isDesignatedInitializerForTheInterface( |
| 521 | const ObjCMethodDecl **InitMethod = nullptr) const; |
| 522 | |
| 523 | /// Determine whether this method has a body. |
| 524 | bool hasBody() const override { return Body.isValid(); } |
| 525 | |
| 526 | /// Retrieve the body of this method, if it has one. |
| 527 | Stmt *getBody() const override; |
| 528 | |
| 529 | void setLazyBody(uint64_t Offset) { Body = Offset; } |
| 530 | |
| 531 | CompoundStmt *getCompoundBody() { return (CompoundStmt*)getBody(); } |
| 532 | void setBody(Stmt *B) { Body = B; } |
| 533 | |
| 534 | /// Returns whether this specific method is a definition. |
| 535 | bool isThisDeclarationADefinition() const { return hasBody(); } |
| 536 | |
| 537 | /// Is this method defined in the NSObject base class? |
| 538 | bool definedInNSObject(const ASTContext &) const; |
| 539 | |
| 540 | // Implement isa/cast/dyncast/etc. |
| 541 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 542 | static bool classofKind(Kind K) { return K == ObjCMethod; } |
| 543 | |
| 544 | static DeclContext *castToDeclContext(const ObjCMethodDecl *D) { |
| 545 | return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D)); |
| 546 | } |
| 547 | |
| 548 | static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) { |
| 549 | return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC)); |
| 550 | } |
| 551 | }; |
| 552 | |
| 553 | /// Describes the variance of a given generic parameter. |
| 554 | enum class ObjCTypeParamVariance : uint8_t { |
| 555 | /// The parameter is invariant: must match exactly. |
| 556 | Invariant, |
| 557 | |
| 558 | /// The parameter is covariant, e.g., X<T> is a subtype of X<U> when |
| 559 | /// the type parameter is covariant and T is a subtype of U. |
| 560 | Covariant, |
| 561 | |
| 562 | /// The parameter is contravariant, e.g., X<T> is a subtype of X<U> |
| 563 | /// when the type parameter is covariant and U is a subtype of T. |
| 564 | Contravariant, |
| 565 | }; |
| 566 | |
| 567 | /// Represents the declaration of an Objective-C type parameter. |
| 568 | /// |
| 569 | /// \code |
| 570 | /// @interface NSDictionary<Key : id<NSCopying>, Value> |
| 571 | /// @end |
| 572 | /// \endcode |
| 573 | /// |
| 574 | /// In the example above, both \c Key and \c Value are represented by |
| 575 | /// \c ObjCTypeParamDecl. \c Key has an explicit bound of \c id<NSCopying>, |
| 576 | /// while \c Value gets an implicit bound of \c id. |
| 577 | /// |
| 578 | /// Objective-C type parameters are typedef-names in the grammar, |
| 579 | class ObjCTypeParamDecl : public TypedefNameDecl { |
| 580 | /// Index of this type parameter in the type parameter list. |
| 581 | unsigned Index : 14; |
| 582 | |
| 583 | /// The variance of the type parameter. |
| 584 | unsigned Variance : 2; |
| 585 | |
| 586 | /// The location of the variance, if any. |
| 587 | SourceLocation VarianceLoc; |
| 588 | |
| 589 | /// The location of the ':', which will be valid when the bound was |
| 590 | /// explicitly specified. |
| 591 | SourceLocation ColonLoc; |
| 592 | |
| 593 | ObjCTypeParamDecl(ASTContext &ctx, DeclContext *dc, |
| 594 | ObjCTypeParamVariance variance, SourceLocation varianceLoc, |
| 595 | unsigned index, |
| 596 | SourceLocation nameLoc, IdentifierInfo *name, |
| 597 | SourceLocation colonLoc, TypeSourceInfo *boundInfo) |
| 598 | : TypedefNameDecl(ObjCTypeParam, ctx, dc, nameLoc, nameLoc, name, |
| 599 | boundInfo), |
| 600 | Index(index), Variance(static_cast<unsigned>(variance)), |
| 601 | VarianceLoc(varianceLoc), ColonLoc(colonLoc) {} |
| 602 | |
| 603 | void anchor() override; |
| 604 | |
| 605 | public: |
| 606 | friend class ASTDeclReader; |
| 607 | friend class ASTDeclWriter; |
| 608 | |
| 609 | static ObjCTypeParamDecl *Create(ASTContext &ctx, DeclContext *dc, |
| 610 | ObjCTypeParamVariance variance, |
| 611 | SourceLocation varianceLoc, |
| 612 | unsigned index, |
| 613 | SourceLocation nameLoc, |
| 614 | IdentifierInfo *name, |
| 615 | SourceLocation colonLoc, |
| 616 | TypeSourceInfo *boundInfo); |
| 617 | static ObjCTypeParamDecl *CreateDeserialized(ASTContext &ctx, unsigned ID); |
| 618 | |
| 619 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
| 620 | |
| 621 | /// Determine the variance of this type parameter. |
| 622 | ObjCTypeParamVariance getVariance() const { |
| 623 | return static_cast<ObjCTypeParamVariance>(Variance); |
| 624 | } |
| 625 | |
| 626 | /// Set the variance of this type parameter. |
| 627 | void setVariance(ObjCTypeParamVariance variance) { |
| 628 | Variance = static_cast<unsigned>(variance); |
| 629 | } |
| 630 | |
| 631 | /// Retrieve the location of the variance keyword. |
| 632 | SourceLocation getVarianceLoc() const { return VarianceLoc; } |
| 633 | |
| 634 | /// Retrieve the index into its type parameter list. |
| 635 | unsigned getIndex() const { return Index; } |
| 636 | |
| 637 | /// Whether this type parameter has an explicitly-written type bound, e.g., |
| 638 | /// "T : NSView". |
| 639 | bool hasExplicitBound() const { return ColonLoc.isValid(); } |
| 640 | |
| 641 | /// Retrieve the location of the ':' separating the type parameter name |
| 642 | /// from the explicitly-specified bound. |
| 643 | SourceLocation getColonLoc() const { return ColonLoc; } |
| 644 | |
| 645 | // Implement isa/cast/dyncast/etc. |
| 646 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 647 | static bool classofKind(Kind K) { return K == ObjCTypeParam; } |
| 648 | }; |
| 649 | |
| 650 | /// Stores a list of Objective-C type parameters for a parameterized class |
| 651 | /// or a category/extension thereof. |
| 652 | /// |
| 653 | /// \code |
| 654 | /// @interface NSArray<T> // stores the <T> |
| 655 | /// @end |
| 656 | /// \endcode |
| 657 | class ObjCTypeParamList final |
| 658 | : private llvm::TrailingObjects<ObjCTypeParamList, ObjCTypeParamDecl *> { |
| 659 | /// Location of the left and right angle brackets. |
| 660 | SourceRange Brackets; |
| 661 | /// The number of parameters in the list, which are tail-allocated. |
| 662 | unsigned NumParams; |
| 663 | |
| 664 | ObjCTypeParamList(SourceLocation lAngleLoc, |
| 665 | ArrayRef<ObjCTypeParamDecl *> typeParams, |
| 666 | SourceLocation rAngleLoc); |
| 667 | |
| 668 | public: |
| 669 | friend TrailingObjects; |
| 670 | |
| 671 | /// Create a new Objective-C type parameter list. |
| 672 | static ObjCTypeParamList *create(ASTContext &ctx, |
| 673 | SourceLocation lAngleLoc, |
| 674 | ArrayRef<ObjCTypeParamDecl *> typeParams, |
| 675 | SourceLocation rAngleLoc); |
| 676 | |
| 677 | /// Iterate through the type parameters in the list. |
| 678 | using iterator = ObjCTypeParamDecl **; |
| 679 | |
| 680 | iterator begin() { return getTrailingObjects<ObjCTypeParamDecl *>(); } |
| 681 | |
| 682 | iterator end() { return begin() + size(); } |
| 683 | |
| 684 | /// Determine the number of type parameters in this list. |
| 685 | unsigned size() const { return NumParams; } |
| 686 | |
| 687 | // Iterate through the type parameters in the list. |
| 688 | using const_iterator = ObjCTypeParamDecl * const *; |
| 689 | |
| 690 | const_iterator begin() const { |
| 691 | return getTrailingObjects<ObjCTypeParamDecl *>(); |
| 692 | } |
| 693 | |
| 694 | const_iterator end() const { |
| 695 | return begin() + size(); |
| 696 | } |
| 697 | |
| 698 | ObjCTypeParamDecl *front() const { |
| 699 | assert(size() > 0 && "empty Objective-C type parameter list")((void)0); |
| 700 | return *begin(); |
| 701 | } |
| 702 | |
| 703 | ObjCTypeParamDecl *back() const { |
| 704 | assert(size() > 0 && "empty Objective-C type parameter list")((void)0); |
| 705 | return *(end() - 1); |
| 706 | } |
| 707 | |
| 708 | SourceLocation getLAngleLoc() const { return Brackets.getBegin(); } |
| 709 | SourceLocation getRAngleLoc() const { return Brackets.getEnd(); } |
| 710 | SourceRange getSourceRange() const { return Brackets; } |
| 711 | |
| 712 | /// Gather the default set of type arguments to be substituted for |
| 713 | /// these type parameters when dealing with an unspecialized type. |
| 714 | void gatherDefaultTypeArgs(SmallVectorImpl<QualType> &typeArgs) const; |
| 715 | }; |
| 716 | |
| 717 | enum class ObjCPropertyQueryKind : uint8_t { |
| 718 | OBJC_PR_query_unknown = 0x00, |
| 719 | OBJC_PR_query_instance, |
| 720 | OBJC_PR_query_class |
| 721 | }; |
| 722 | |
| 723 | /// Represents one property declaration in an Objective-C interface. |
| 724 | /// |
| 725 | /// For example: |
| 726 | /// \code{.mm} |
| 727 | /// \@property (assign, readwrite) int MyProperty; |
| 728 | /// \endcode |
| 729 | class ObjCPropertyDecl : public NamedDecl { |
| 730 | void anchor() override; |
| 731 | |
| 732 | public: |
| 733 | enum SetterKind { Assign, Retain, Copy, Weak }; |
| 734 | enum PropertyControl { None, Required, Optional }; |
| 735 | |
| 736 | private: |
| 737 | // location of \@property |
| 738 | SourceLocation AtLoc; |
| 739 | |
| 740 | // location of '(' starting attribute list or null. |
| 741 | SourceLocation LParenLoc; |
| 742 | |
| 743 | QualType DeclType; |
| 744 | TypeSourceInfo *DeclTypeSourceInfo; |
| 745 | unsigned PropertyAttributes : NumObjCPropertyAttrsBits; |
| 746 | unsigned PropertyAttributesAsWritten : NumObjCPropertyAttrsBits; |
| 747 | |
| 748 | // \@required/\@optional |
| 749 | unsigned PropertyImplementation : 2; |
| 750 | |
| 751 | // getter name of NULL if no getter |
| 752 | Selector GetterName; |
| 753 | |
| 754 | // setter name of NULL if no setter |
| 755 | Selector SetterName; |
| 756 | |
| 757 | // location of the getter attribute's value |
| 758 | SourceLocation GetterNameLoc; |
| 759 | |
| 760 | // location of the setter attribute's value |
| 761 | SourceLocation SetterNameLoc; |
| 762 | |
| 763 | // Declaration of getter instance method |
| 764 | ObjCMethodDecl *GetterMethodDecl = nullptr; |
| 765 | |
| 766 | // Declaration of setter instance method |
| 767 | ObjCMethodDecl *SetterMethodDecl = nullptr; |
| 768 | |
| 769 | // Synthesize ivar for this property |
| 770 | ObjCIvarDecl *PropertyIvarDecl = nullptr; |
| 771 | |
| 772 | ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id, |
| 773 | SourceLocation AtLocation, SourceLocation LParenLocation, |
| 774 | QualType T, TypeSourceInfo *TSI, PropertyControl propControl) |
| 775 | : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation), |
| 776 | LParenLoc(LParenLocation), DeclType(T), DeclTypeSourceInfo(TSI), |
| 777 | PropertyAttributes(ObjCPropertyAttribute::kind_noattr), |
| 778 | PropertyAttributesAsWritten(ObjCPropertyAttribute::kind_noattr), |
| 779 | PropertyImplementation(propControl), GetterName(Selector()), |
| 780 | SetterName(Selector()) {} |
| 781 | |
| 782 | public: |
| 783 | static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC, |
| 784 | SourceLocation L, |
| 785 | IdentifierInfo *Id, SourceLocation AtLocation, |
| 786 | SourceLocation LParenLocation, |
| 787 | QualType T, |
| 788 | TypeSourceInfo *TSI, |
| 789 | PropertyControl propControl = None); |
| 790 | |
| 791 | static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
| 792 | |
| 793 | SourceLocation getAtLoc() const { return AtLoc; } |
| 794 | void setAtLoc(SourceLocation L) { AtLoc = L; } |
| 795 | |
| 796 | SourceLocation getLParenLoc() const { return LParenLoc; } |
| 797 | void setLParenLoc(SourceLocation L) { LParenLoc = L; } |
| 798 | |
| 799 | TypeSourceInfo *getTypeSourceInfo() const { return DeclTypeSourceInfo; } |
| 800 | |
| 801 | QualType getType() const { return DeclType; } |
| 802 | |
| 803 | void setType(QualType T, TypeSourceInfo *TSI) { |
| 804 | DeclType = T; |
| 805 | DeclTypeSourceInfo = TSI; |
| 806 | } |
| 807 | |
| 808 | /// Retrieve the type when this property is used with a specific base object |
| 809 | /// type. |
| 810 | QualType getUsageType(QualType objectType) const; |
| 811 | |
| 812 | ObjCPropertyAttribute::Kind getPropertyAttributes() const { |
| 813 | return ObjCPropertyAttribute::Kind(PropertyAttributes); |
| 814 | } |
| 815 | |
| 816 | void setPropertyAttributes(ObjCPropertyAttribute::Kind PRVal) { |
| 817 | PropertyAttributes |= PRVal; |
| 818 | } |
| 819 | |
| 820 | void overwritePropertyAttributes(unsigned PRVal) { |
| 821 | PropertyAttributes = PRVal; |
| 822 | } |
| 823 | |
| 824 | ObjCPropertyAttribute::Kind getPropertyAttributesAsWritten() const { |
| 825 | return ObjCPropertyAttribute::Kind(PropertyAttributesAsWritten); |
| 826 | } |
| 827 | |
| 828 | void setPropertyAttributesAsWritten(ObjCPropertyAttribute::Kind PRVal) { |
| 829 | PropertyAttributesAsWritten = PRVal; |
| 830 | } |
| 831 | |
| 832 | // Helper methods for accessing attributes. |
| 833 | |
| 834 | /// isReadOnly - Return true iff the property has a setter. |
| 835 | bool isReadOnly() const { |
| 836 | return (PropertyAttributes & ObjCPropertyAttribute::kind_readonly); |
| 837 | } |
| 838 | |
| 839 | /// isAtomic - Return true if the property is atomic. |
| 840 | bool isAtomic() const { |
| 841 | return (PropertyAttributes & ObjCPropertyAttribute::kind_atomic); |
| 842 | } |
| 843 | |
| 844 | /// isRetaining - Return true if the property retains its value. |
| 845 | bool isRetaining() const { |
| 846 | return (PropertyAttributes & (ObjCPropertyAttribute::kind_retain | |
| 847 | ObjCPropertyAttribute::kind_strong | |
| 848 | ObjCPropertyAttribute::kind_copy)); |
| 849 | } |
| 850 | |
| 851 | bool isInstanceProperty() const { return !isClassProperty(); } |
| 852 | bool isClassProperty() const { |
| 853 | return PropertyAttributes & ObjCPropertyAttribute::kind_class; |
| 854 | } |
| 855 | bool isDirectProperty() const; |
| 856 | |
| 857 | ObjCPropertyQueryKind getQueryKind() const { |
| 858 | return isClassProperty() ? ObjCPropertyQueryKind::OBJC_PR_query_class : |
| 859 | ObjCPropertyQueryKind::OBJC_PR_query_instance; |
| 860 | } |
| 861 | |
| 862 | static ObjCPropertyQueryKind getQueryKind(bool isClassProperty) { |
| 863 | return isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class : |
| 864 | ObjCPropertyQueryKind::OBJC_PR_query_instance; |
| 865 | } |
| 866 | |
| 867 | /// getSetterKind - Return the method used for doing assignment in |
| 868 | /// the property setter. This is only valid if the property has been |
| 869 | /// defined to have a setter. |
| 870 | SetterKind getSetterKind() const { |
| 871 | if (PropertyAttributes & ObjCPropertyAttribute::kind_strong) |
| 872 | return getType()->isBlockPointerType() ? Copy : Retain; |
| 873 | if (PropertyAttributes & ObjCPropertyAttribute::kind_retain) |
| 874 | return Retain; |
| 875 | if (PropertyAttributes & ObjCPropertyAttribute::kind_copy) |
| 876 | return Copy; |
| 877 | if (PropertyAttributes & ObjCPropertyAttribute::kind_weak) |
| 878 | return Weak; |
| 879 | return Assign; |
| 880 | } |
| 881 | |
| 882 | Selector getGetterName() const { return GetterName; } |
| 883 | SourceLocation getGetterNameLoc() const { return GetterNameLoc; } |
| 884 | |
| 885 | void setGetterName(Selector Sel, SourceLocation Loc = SourceLocation()) { |
| 886 | GetterName = Sel; |
| 887 | GetterNameLoc = Loc; |
| 888 | } |
| 889 | |
| 890 | Selector getSetterName() const { return SetterName; } |
| 891 | SourceLocation getSetterNameLoc() const { return SetterNameLoc; } |
| 892 | |
| 893 | void setSetterName(Selector Sel, SourceLocation Loc = SourceLocation()) { |
| 894 | SetterName = Sel; |
| 895 | SetterNameLoc = Loc; |
| 896 | } |
| 897 | |
| 898 | ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; } |
| 899 | void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; } |
| 900 | |
| 901 | ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; } |
| 902 | void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; } |
| 903 | |
| 904 | // Related to \@optional/\@required declared in \@protocol |
| 905 | void setPropertyImplementation(PropertyControl pc) { |
| 906 | PropertyImplementation = pc; |
| 907 | } |
| 908 | |
| 909 | PropertyControl getPropertyImplementation() const { |
| 910 | return PropertyControl(PropertyImplementation); |
| 911 | } |
| 912 | |
| 913 | bool isOptional() const { |
| 914 | return getPropertyImplementation() == PropertyControl::Optional; |
| 915 | } |
| 916 | |
| 917 | void setPropertyIvarDecl(ObjCIvarDecl *Ivar) { |
| 918 | PropertyIvarDecl = Ivar; |
| 919 | } |
| 920 | |
| 921 | ObjCIvarDecl *getPropertyIvarDecl() const { |
| 922 | return PropertyIvarDecl; |
| 923 | } |
| 924 | |
| 925 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
| 926 | return SourceRange(AtLoc, getLocation()); |
| 927 | } |
| 928 | |
| 929 | /// Get the default name of the synthesized ivar. |
| 930 | IdentifierInfo *getDefaultSynthIvarName(ASTContext &Ctx) const; |
| 931 | |
| 932 | /// Lookup a property by name in the specified DeclContext. |
| 933 | static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC, |
| 934 | const IdentifierInfo *propertyID, |
| 935 | ObjCPropertyQueryKind queryKind); |
| 936 | |
| 937 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 938 | static bool classofKind(Kind K) { return K == ObjCProperty; } |
| 939 | }; |
| 940 | |
| 941 | /// ObjCContainerDecl - Represents a container for method declarations. |
| 942 | /// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl, |
| 943 | /// ObjCProtocolDecl, and ObjCImplDecl. |
| 944 | /// |
| 945 | class ObjCContainerDecl : public NamedDecl, public DeclContext { |
| 946 | // This class stores some data in DeclContext::ObjCContainerDeclBits |
| 947 | // to save some space. Use the provided accessors to access it. |
| 948 | |
| 949 | // These two locations in the range mark the end of the method container. |
| 950 | // The first points to the '@' token, and the second to the 'end' token. |
| 951 | SourceRange AtEnd; |
| 952 | |
| 953 | void anchor() override; |
| 954 | |
| 955 | public: |
| 956 | ObjCContainerDecl(Kind DK, DeclContext *DC, IdentifierInfo *Id, |
| 957 | SourceLocation nameLoc, SourceLocation atStartLoc); |
| 958 | |
| 959 | // Iterator access to instance/class properties. |
| 960 | using prop_iterator = specific_decl_iterator<ObjCPropertyDecl>; |
| 961 | using prop_range = |
| 962 | llvm::iterator_range<specific_decl_iterator<ObjCPropertyDecl>>; |
| 963 | |
| 964 | prop_range properties() const { return prop_range(prop_begin(), prop_end()); } |
| 965 | |
| 966 | prop_iterator prop_begin() const { |
| 967 | return prop_iterator(decls_begin()); |
| 968 | } |
| 969 | |
| 970 | prop_iterator prop_end() const { |
| 971 | return prop_iterator(decls_end()); |
| 972 | } |
| 973 | |
| 974 | using instprop_iterator = |
| 975 | filtered_decl_iterator<ObjCPropertyDecl, |
| 976 | &ObjCPropertyDecl::isInstanceProperty>; |
| 977 | using instprop_range = llvm::iterator_range<instprop_iterator>; |
| 978 | |
| 979 | instprop_range instance_properties() const { |
| 980 | return instprop_range(instprop_begin(), instprop_end()); |
| 981 | } |
| 982 | |
| 983 | instprop_iterator instprop_begin() const { |
| 984 | return instprop_iterator(decls_begin()); |
| 985 | } |
| 986 | |
| 987 | instprop_iterator instprop_end() const { |
| 988 | return instprop_iterator(decls_end()); |
| 989 | } |
| 990 | |
| 991 | using classprop_iterator = |
| 992 | filtered_decl_iterator<ObjCPropertyDecl, |
| 993 | &ObjCPropertyDecl::isClassProperty>; |
| 994 | using classprop_range = llvm::iterator_range<classprop_iterator>; |
| 995 | |
| 996 | classprop_range class_properties() const { |
| 997 | return classprop_range(classprop_begin(), classprop_end()); |
| 998 | } |
| 999 | |
| 1000 | classprop_iterator classprop_begin() const { |
| 1001 | return classprop_iterator(decls_begin()); |
| 1002 | } |
| 1003 | |
| 1004 | classprop_iterator classprop_end() const { |
| 1005 | return classprop_iterator(decls_end()); |
| 1006 | } |
| 1007 | |
| 1008 | // Iterator access to instance/class methods. |
| 1009 | using method_iterator = specific_decl_iterator<ObjCMethodDecl>; |
| 1010 | using method_range = |
| 1011 | llvm::iterator_range<specific_decl_iterator<ObjCMethodDecl>>; |
| 1012 | |
| 1013 | method_range methods() const { |
| 1014 | return method_range(meth_begin(), meth_end()); |
| 1015 | } |
| 1016 | |
| 1017 | method_iterator meth_begin() const { |
| 1018 | return method_iterator(decls_begin()); |
| 1019 | } |
| 1020 | |
| 1021 | method_iterator meth_end() const { |
| 1022 | return method_iterator(decls_end()); |
| 1023 | } |
| 1024 | |
| 1025 | using instmeth_iterator = |
| 1026 | filtered_decl_iterator<ObjCMethodDecl, |
| 1027 | &ObjCMethodDecl::isInstanceMethod>; |
| 1028 | using instmeth_range = llvm::iterator_range<instmeth_iterator>; |
| 1029 | |
| 1030 | instmeth_range instance_methods() const { |
| 1031 | return instmeth_range(instmeth_begin(), instmeth_end()); |
| 1032 | } |
| 1033 | |
| 1034 | instmeth_iterator instmeth_begin() const { |
| 1035 | return instmeth_iterator(decls_begin()); |
| 1036 | } |
| 1037 | |
| 1038 | instmeth_iterator instmeth_end() const { |
| 1039 | return instmeth_iterator(decls_end()); |
| 1040 | } |
| 1041 | |
| 1042 | using classmeth_iterator = |
| 1043 | filtered_decl_iterator<ObjCMethodDecl, |
| 1044 | &ObjCMethodDecl::isClassMethod>; |
| 1045 | using classmeth_range = llvm::iterator_range<classmeth_iterator>; |
| 1046 | |
| 1047 | classmeth_range class_methods() const { |
| 1048 | return classmeth_range(classmeth_begin(), classmeth_end()); |
| 1049 | } |
| 1050 | |
| 1051 | classmeth_iterator classmeth_begin() const { |
| 1052 | return classmeth_iterator(decls_begin()); |
| 1053 | } |
| 1054 | |
| 1055 | classmeth_iterator classmeth_end() const { |
| 1056 | return classmeth_iterator(decls_end()); |
| 1057 | } |
| 1058 | |
| 1059 | // Get the local instance/class method declared in this interface. |
| 1060 | ObjCMethodDecl *getMethod(Selector Sel, bool isInstance, |
| 1061 | bool AllowHidden = false) const; |
| 1062 | |
| 1063 | ObjCMethodDecl *getInstanceMethod(Selector Sel, |
| 1064 | bool AllowHidden = false) const { |
| 1065 | return getMethod(Sel, true/*isInstance*/, AllowHidden); |
| 1066 | } |
| 1067 | |
| 1068 | ObjCMethodDecl *getClassMethod(Selector Sel, bool AllowHidden = false) const { |
| 1069 | return getMethod(Sel, false/*isInstance*/, AllowHidden); |
| 1070 | } |
| 1071 | |
| 1072 | bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const; |
| 1073 | ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const; |
| 1074 | |
| 1075 | ObjCPropertyDecl * |
| 1076 | FindPropertyDeclaration(const IdentifierInfo *PropertyId, |
| 1077 | ObjCPropertyQueryKind QueryKind) const; |
| 1078 | |
| 1079 | using PropertyMap = |
| 1080 | llvm::DenseMap<std::pair<IdentifierInfo *, unsigned/*isClassProperty*/>, |
| 1081 | ObjCPropertyDecl *>; |
| 1082 | using ProtocolPropertySet = llvm::SmallDenseSet<const ObjCProtocolDecl *, 8>; |
| 1083 | using PropertyDeclOrder = llvm::SmallVector<ObjCPropertyDecl *, 8>; |
| 1084 | |
| 1085 | /// This routine collects list of properties to be implemented in the class. |
| 1086 | /// This includes, class's and its conforming protocols' properties. |
| 1087 | /// Note, the superclass's properties are not included in the list. |
| 1088 | virtual void collectPropertiesToImplement(PropertyMap &PM, |
| 1089 | PropertyDeclOrder &PO) const {} |
| 1090 | |
| 1091 | SourceLocation getAtStartLoc() const { return ObjCContainerDeclBits.AtStart; } |
| 1092 | |
| 1093 | void setAtStartLoc(SourceLocation Loc) { |
| 1094 | ObjCContainerDeclBits.AtStart = Loc; |
| 1095 | } |
| 1096 | |
| 1097 | // Marks the end of the container. |
| 1098 | SourceRange getAtEndRange() const { return AtEnd; } |
| 1099 | |
| 1100 | void setAtEndRange(SourceRange atEnd) { AtEnd = atEnd; } |
| 1101 | |
| 1102 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
| 1103 | return SourceRange(getAtStartLoc(), getAtEndRange().getEnd()); |
| 1104 | } |
| 1105 | |
| 1106 | // Implement isa/cast/dyncast/etc. |
| 1107 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 1108 | |
| 1109 | static bool classofKind(Kind K) { |
| 1110 | return K >= firstObjCContainer && |
| 1111 | K <= lastObjCContainer; |
| 1112 | } |
| 1113 | |
| 1114 | static DeclContext *castToDeclContext(const ObjCContainerDecl *D) { |
| 1115 | return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D)); |
| 1116 | } |
| 1117 | |
| 1118 | static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) { |
| 1119 | return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC)); |
| 1120 | } |
| 1121 | }; |
| 1122 | |
| 1123 | /// Represents an ObjC class declaration. |
| 1124 | /// |
| 1125 | /// For example: |
| 1126 | /// |
| 1127 | /// \code |
| 1128 | /// // MostPrimitive declares no super class (not particularly useful). |
| 1129 | /// \@interface MostPrimitive |
| 1130 | /// // no instance variables or methods. |
| 1131 | /// \@end |
| 1132 | /// |
| 1133 | /// // NSResponder inherits from NSObject & implements NSCoding (a protocol). |
| 1134 | /// \@interface NSResponder : NSObject \<NSCoding> |
| 1135 | /// { // instance variables are represented by ObjCIvarDecl. |
| 1136 | /// id nextResponder; // nextResponder instance variable. |
| 1137 | /// } |
| 1138 | /// - (NSResponder *)nextResponder; // return a pointer to NSResponder. |
| 1139 | /// - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer |
| 1140 | /// \@end // to an NSEvent. |
| 1141 | /// \endcode |
| 1142 | /// |
| 1143 | /// Unlike C/C++, forward class declarations are accomplished with \@class. |
| 1144 | /// Unlike C/C++, \@class allows for a list of classes to be forward declared. |
| 1145 | /// Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes |
| 1146 | /// typically inherit from NSObject (an exception is NSProxy). |
| 1147 | /// |
| 1148 | class ObjCInterfaceDecl : public ObjCContainerDecl |
| 1149 | , public Redeclarable<ObjCInterfaceDecl> { |
| 1150 | friend class ASTContext; |
| 1151 | |
| 1152 | /// TypeForDecl - This indicates the Type object that represents this |
| 1153 | /// TypeDecl. It is a cache maintained by ASTContext::getObjCInterfaceType |
| 1154 | mutable const Type *TypeForDecl = nullptr; |
| 1155 | |
| 1156 | struct DefinitionData { |
| 1157 | /// The definition of this class, for quick access from any |
| 1158 | /// declaration. |
| 1159 | ObjCInterfaceDecl *Definition = nullptr; |
| 1160 | |
| 1161 | /// When non-null, this is always an ObjCObjectType. |
| 1162 | TypeSourceInfo *SuperClassTInfo = nullptr; |
| 1163 | |
| 1164 | /// Protocols referenced in the \@interface declaration |
| 1165 | ObjCProtocolList ReferencedProtocols; |
| 1166 | |
| 1167 | /// Protocols reference in both the \@interface and class extensions. |
| 1168 | ObjCList<ObjCProtocolDecl> AllReferencedProtocols; |
| 1169 | |
| 1170 | /// List of categories and class extensions defined for this class. |
| 1171 | /// |
| 1172 | /// Categories are stored as a linked list in the AST, since the categories |
| 1173 | /// and class extensions come long after the initial interface declaration, |
| 1174 | /// and we avoid dynamically-resized arrays in the AST wherever possible. |
| 1175 | ObjCCategoryDecl *CategoryList = nullptr; |
| 1176 | |
| 1177 | /// IvarList - List of all ivars defined by this class; including class |
| 1178 | /// extensions and implementation. This list is built lazily. |
| 1179 | ObjCIvarDecl *IvarList = nullptr; |
| 1180 | |
| 1181 | /// Indicates that the contents of this Objective-C class will be |
| 1182 | /// completed by the external AST source when required. |
| 1183 | mutable unsigned ExternallyCompleted : 1; |
| 1184 | |
| 1185 | /// Indicates that the ivar cache does not yet include ivars |
| 1186 | /// declared in the implementation. |
| 1187 | mutable unsigned IvarListMissingImplementation : 1; |
| 1188 | |
| 1189 | /// Indicates that this interface decl contains at least one initializer |
| 1190 | /// marked with the 'objc_designated_initializer' attribute. |
| 1191 | unsigned HasDesignatedInitializers : 1; |
| 1192 | |
| 1193 | enum InheritedDesignatedInitializersState { |
| 1194 | /// We didn't calculate whether the designated initializers should be |
| 1195 | /// inherited or not. |
| 1196 | IDI_Unknown = 0, |
| 1197 | |
| 1198 | /// Designated initializers are inherited for the super class. |
| 1199 | IDI_Inherited = 1, |
| 1200 | |
| 1201 | /// The class does not inherit designated initializers. |
| 1202 | IDI_NotInherited = 2 |
| 1203 | }; |
| 1204 | |
| 1205 | /// One of the \c InheritedDesignatedInitializersState enumeratos. |
| 1206 | mutable unsigned InheritedDesignatedInitializers : 2; |
| 1207 | |
| 1208 | /// The location of the last location in this declaration, before |
| 1209 | /// the properties/methods. For example, this will be the '>', '}', or |
| 1210 | /// identifier, |
| 1211 | SourceLocation EndLoc; |
| 1212 | |
| 1213 | DefinitionData() |
| 1214 | : ExternallyCompleted(false), IvarListMissingImplementation(true), |
| 1215 | HasDesignatedInitializers(false), |
| 1216 | InheritedDesignatedInitializers(IDI_Unknown) {} |
| 1217 | }; |
| 1218 | |
| 1219 | /// The type parameters associated with this class, if any. |
| 1220 | ObjCTypeParamList *TypeParamList = nullptr; |
| 1221 | |
| 1222 | /// Contains a pointer to the data associated with this class, |
| 1223 | /// which will be NULL if this class has not yet been defined. |
| 1224 | /// |
| 1225 | /// The bit indicates when we don't need to check for out-of-date |
| 1226 | /// declarations. It will be set unless modules are enabled. |
| 1227 | llvm::PointerIntPair<DefinitionData *, 1, bool> Data; |
| 1228 | |
| 1229 | ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC, SourceLocation AtLoc, |
| 1230 | IdentifierInfo *Id, ObjCTypeParamList *typeParamList, |
| 1231 | SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl, |
| 1232 | bool IsInternal); |
| 1233 | |
| 1234 | void anchor() override; |
| 1235 | |
| 1236 | void LoadExternalDefinition() const; |
| 1237 | |
| 1238 | DefinitionData &data() const { |
| 1239 | assert(Data.getPointer() && "Declaration has no definition!")((void)0); |
| 1240 | return *Data.getPointer(); |
| 1241 | } |
| 1242 | |
| 1243 | /// Allocate the definition data for this class. |
| 1244 | void allocateDefinitionData(); |
| 1245 | |
| 1246 | using redeclarable_base = Redeclarable<ObjCInterfaceDecl>; |
| 1247 | |
| 1248 | ObjCInterfaceDecl *getNextRedeclarationImpl() override { |
| 1249 | return getNextRedeclaration(); |
| 1250 | } |
| 1251 | |
| 1252 | ObjCInterfaceDecl *getPreviousDeclImpl() override { |
| 1253 | return getPreviousDecl(); |
| 1254 | } |
| 1255 | |
| 1256 | ObjCInterfaceDecl *getMostRecentDeclImpl() override { |
| 1257 | return getMostRecentDecl(); |
| 1258 | } |
| 1259 | |
| 1260 | public: |
| 1261 | static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC, |
| 1262 | SourceLocation atLoc, |
| 1263 | IdentifierInfo *Id, |
| 1264 | ObjCTypeParamList *typeParamList, |
| 1265 | ObjCInterfaceDecl *PrevDecl, |
| 1266 | SourceLocation ClassLoc = SourceLocation(), |
| 1267 | bool isInternal = false); |
| 1268 | |
| 1269 | static ObjCInterfaceDecl *CreateDeserialized(const ASTContext &C, unsigned ID); |
| 1270 | |
| 1271 | /// Retrieve the type parameters of this class. |
| 1272 | /// |
| 1273 | /// This function looks for a type parameter list for the given |
| 1274 | /// class; if the class has been declared (with \c \@class) but not |
| 1275 | /// defined (with \c \@interface), it will search for a declaration that |
| 1276 | /// has type parameters, skipping any declarations that do not. |
| 1277 | ObjCTypeParamList *getTypeParamList() const; |
| 1278 | |
| 1279 | /// Set the type parameters of this class. |
| 1280 | /// |
| 1281 | /// This function is used by the AST importer, which must import the type |
| 1282 | /// parameters after creating their DeclContext to avoid loops. |
| 1283 | void setTypeParamList(ObjCTypeParamList *TPL); |
| 1284 | |
| 1285 | /// Retrieve the type parameters written on this particular declaration of |
| 1286 | /// the class. |
| 1287 | ObjCTypeParamList *getTypeParamListAsWritten() const { |
| 1288 | return TypeParamList; |
| 1289 | } |
| 1290 | |
| 1291 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
| 1292 | if (isThisDeclarationADefinition()) |
| 1293 | return ObjCContainerDecl::getSourceRange(); |
| 1294 | |
| 1295 | return SourceRange(getAtStartLoc(), getLocation()); |
| 1296 | } |
| 1297 | |
| 1298 | /// Indicate that this Objective-C class is complete, but that |
| 1299 | /// the external AST source will be responsible for filling in its contents |
| 1300 | /// when a complete class is required. |
| 1301 | void setExternallyCompleted(); |
| 1302 | |
| 1303 | /// Indicate that this interface decl contains at least one initializer |
| 1304 | /// marked with the 'objc_designated_initializer' attribute. |
| 1305 | void setHasDesignatedInitializers(); |
| 1306 | |
| 1307 | /// Returns true if this interface decl contains at least one initializer |
| 1308 | /// marked with the 'objc_designated_initializer' attribute. |
| 1309 | bool hasDesignatedInitializers() const; |
| 1310 | |
| 1311 | /// Returns true if this interface decl declares a designated initializer |
| 1312 | /// or it inherites one from its super class. |
| 1313 | bool declaresOrInheritsDesignatedInitializers() const { |
| 1314 | return hasDesignatedInitializers() || inheritsDesignatedInitializers(); |
| 1315 | } |
| 1316 | |
| 1317 | const ObjCProtocolList &getReferencedProtocols() const { |
| 1318 | assert(hasDefinition() && "Caller did not check for forward reference!")((void)0); |
| 1319 | if (data().ExternallyCompleted) |
| 1320 | LoadExternalDefinition(); |
| 1321 | |
| 1322 | return data().ReferencedProtocols; |
| 1323 | } |
| 1324 | |
| 1325 | ObjCImplementationDecl *getImplementation() const; |
| 1326 | void setImplementation(ObjCImplementationDecl *ImplD); |
| 1327 | |
| 1328 | ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const; |
| 1329 | |
| 1330 | // Get the local instance/class method declared in a category. |
| 1331 | ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const; |
| 1332 | ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const; |
| 1333 | |
| 1334 | ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const { |
| 1335 | return isInstance ? getCategoryInstanceMethod(Sel) |
| 1336 | : getCategoryClassMethod(Sel); |
| 1337 | } |
| 1338 | |
| 1339 | using protocol_iterator = ObjCProtocolList::iterator; |
| 1340 | using protocol_range = llvm::iterator_range<protocol_iterator>; |
| 1341 | |
| 1342 | protocol_range protocols() const { |
| 1343 | return protocol_range(protocol_begin(), protocol_end()); |
| 1344 | } |
| 1345 | |
| 1346 | protocol_iterator protocol_begin() const { |
| 1347 | // FIXME: Should make sure no callers ever do this. |
| 1348 | if (!hasDefinition()) |
| 1349 | return protocol_iterator(); |
| 1350 | |
| 1351 | if (data().ExternallyCompleted) |
| 1352 | LoadExternalDefinition(); |
| 1353 | |
| 1354 | return data().ReferencedProtocols.begin(); |
| 1355 | } |
| 1356 | |
| 1357 | protocol_iterator protocol_end() const { |
| 1358 | // FIXME: Should make sure no callers ever do this. |
| 1359 | if (!hasDefinition()) |
| 1360 | return protocol_iterator(); |
| 1361 | |
| 1362 | if (data().ExternallyCompleted) |
| 1363 | LoadExternalDefinition(); |
| 1364 | |
| 1365 | return data().ReferencedProtocols.end(); |
| 1366 | } |
| 1367 | |
| 1368 | using protocol_loc_iterator = ObjCProtocolList::loc_iterator; |
| 1369 | using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>; |
| 1370 | |
| 1371 | protocol_loc_range protocol_locs() const { |
| 1372 | return protocol_loc_range(protocol_loc_begin(), protocol_loc_end()); |
| 1373 | } |
| 1374 | |
| 1375 | protocol_loc_iterator protocol_loc_begin() const { |
| 1376 | // FIXME: Should make sure no callers ever do this. |
| 1377 | if (!hasDefinition()) |
| 1378 | return protocol_loc_iterator(); |
| 1379 | |
| 1380 | if (data().ExternallyCompleted) |
| 1381 | LoadExternalDefinition(); |
| 1382 | |
| 1383 | return data().ReferencedProtocols.loc_begin(); |
| 1384 | } |
| 1385 | |
| 1386 | protocol_loc_iterator protocol_loc_end() const { |
| 1387 | // FIXME: Should make sure no callers ever do this. |
| 1388 | if (!hasDefinition()) |
| 1389 | return protocol_loc_iterator(); |
| 1390 | |
| 1391 | if (data().ExternallyCompleted) |
| 1392 | LoadExternalDefinition(); |
| 1393 | |
| 1394 | return data().ReferencedProtocols.loc_end(); |
| 1395 | } |
| 1396 | |
| 1397 | using all_protocol_iterator = ObjCList<ObjCProtocolDecl>::iterator; |
| 1398 | using all_protocol_range = llvm::iterator_range<all_protocol_iterator>; |
| 1399 | |
| 1400 | all_protocol_range all_referenced_protocols() const { |
| 1401 | return all_protocol_range(all_referenced_protocol_begin(), |
| 1402 | all_referenced_protocol_end()); |
| 1403 | } |
| 1404 | |
| 1405 | all_protocol_iterator all_referenced_protocol_begin() const { |
| 1406 | // FIXME: Should make sure no callers ever do this. |
| 1407 | if (!hasDefinition()) |
| 1408 | return all_protocol_iterator(); |
| 1409 | |
| 1410 | if (data().ExternallyCompleted) |
| 1411 | LoadExternalDefinition(); |
| 1412 | |
| 1413 | return data().AllReferencedProtocols.empty() |
| 1414 | ? protocol_begin() |
| 1415 | : data().AllReferencedProtocols.begin(); |
| 1416 | } |
| 1417 | |
| 1418 | all_protocol_iterator all_referenced_protocol_end() const { |
| 1419 | // FIXME: Should make sure no callers ever do this. |
| 1420 | if (!hasDefinition()) |
| 1421 | return all_protocol_iterator(); |
| 1422 | |
| 1423 | if (data().ExternallyCompleted) |
| 1424 | LoadExternalDefinition(); |
| 1425 | |
| 1426 | return data().AllReferencedProtocols.empty() |
| 1427 | ? protocol_end() |
| 1428 | : data().AllReferencedProtocols.end(); |
| 1429 | } |
| 1430 | |
| 1431 | using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>; |
| 1432 | using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>; |
| 1433 | |
| 1434 | ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); } |
| 1435 | |
| 1436 | ivar_iterator ivar_begin() const { |
| 1437 | if (const ObjCInterfaceDecl *Def = getDefinition()) |
| 1438 | return ivar_iterator(Def->decls_begin()); |
| 1439 | |
| 1440 | // FIXME: Should make sure no callers ever do this. |
| 1441 | return ivar_iterator(); |
| 1442 | } |
| 1443 | |
| 1444 | ivar_iterator ivar_end() const { |
| 1445 | if (const ObjCInterfaceDecl *Def = getDefinition()) |
| 1446 | return ivar_iterator(Def->decls_end()); |
| 1447 | |
| 1448 | // FIXME: Should make sure no callers ever do this. |
| 1449 | return ivar_iterator(); |
| 1450 | } |
| 1451 | |
| 1452 | unsigned ivar_size() const { |
| 1453 | return std::distance(ivar_begin(), ivar_end()); |
| 1454 | } |
| 1455 | |
| 1456 | bool ivar_empty() const { return ivar_begin() == ivar_end(); } |
| 1457 | |
| 1458 | ObjCIvarDecl *all_declared_ivar_begin(); |
| 1459 | const ObjCIvarDecl *all_declared_ivar_begin() const { |
| 1460 | // Even though this modifies IvarList, it's conceptually const: |
| 1461 | // the ivar chain is essentially a cached property of ObjCInterfaceDecl. |
| 1462 | return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin(); |
| 1463 | } |
| 1464 | void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; } |
| 1465 | |
| 1466 | /// setProtocolList - Set the list of protocols that this interface |
| 1467 | /// implements. |
| 1468 | void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num, |
| 1469 | const SourceLocation *Locs, ASTContext &C) { |
| 1470 | data().ReferencedProtocols.set(List, Num, Locs, C); |
| 1471 | } |
| 1472 | |
| 1473 | /// mergeClassExtensionProtocolList - Merge class extension's protocol list |
| 1474 | /// into the protocol list for this class. |
| 1475 | void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List, |
| 1476 | unsigned Num, |
| 1477 | ASTContext &C); |
| 1478 | |
| 1479 | /// Produce a name to be used for class's metadata. It comes either via |
| 1480 | /// objc_runtime_name attribute or class name. |
| 1481 | StringRef getObjCRuntimeNameAsString() const; |
| 1482 | |
| 1483 | /// Returns the designated initializers for the interface. |
| 1484 | /// |
| 1485 | /// If this declaration does not have methods marked as designated |
| 1486 | /// initializers then the interface inherits the designated initializers of |
| 1487 | /// its super class. |
| 1488 | void getDesignatedInitializers( |
| 1489 | llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const; |
| 1490 | |
| 1491 | /// Returns true if the given selector is a designated initializer for the |
| 1492 | /// interface. |
| 1493 | /// |
| 1494 | /// If this declaration does not have methods marked as designated |
| 1495 | /// initializers then the interface inherits the designated initializers of |
| 1496 | /// its super class. |
| 1497 | /// |
| 1498 | /// \param InitMethod if non-null and the function returns true, it receives |
| 1499 | /// the method that was marked as a designated initializer. |
| 1500 | bool |
| 1501 | isDesignatedInitializer(Selector Sel, |
| 1502 | const ObjCMethodDecl **InitMethod = nullptr) const; |
| 1503 | |
| 1504 | /// Determine whether this particular declaration of this class is |
| 1505 | /// actually also a definition. |
| 1506 | bool isThisDeclarationADefinition() const { |
| 1507 | return getDefinition() == this; |
| 1508 | } |
| 1509 | |
| 1510 | /// Determine whether this class has been defined. |
| 1511 | bool hasDefinition() const { |
| 1512 | // If the name of this class is out-of-date, bring it up-to-date, which |
| 1513 | // might bring in a definition. |
| 1514 | // Note: a null value indicates that we don't have a definition and that |
| 1515 | // modules are enabled. |
| 1516 | if (!Data.getOpaqueValue()) |
| 1517 | getMostRecentDecl(); |
| 1518 | |
| 1519 | return Data.getPointer(); |
| 1520 | } |
| 1521 | |
| 1522 | /// Retrieve the definition of this class, or NULL if this class |
| 1523 | /// has been forward-declared (with \@class) but not yet defined (with |
| 1524 | /// \@interface). |
| 1525 | ObjCInterfaceDecl *getDefinition() { |
| 1526 | return hasDefinition()? Data.getPointer()->Definition : nullptr; |
| 1527 | } |
| 1528 | |
| 1529 | /// Retrieve the definition of this class, or NULL if this class |
| 1530 | /// has been forward-declared (with \@class) but not yet defined (with |
| 1531 | /// \@interface). |
| 1532 | const ObjCInterfaceDecl *getDefinition() const { |
| 1533 | return hasDefinition()? Data.getPointer()->Definition : nullptr; |
| 1534 | } |
| 1535 | |
| 1536 | /// Starts the definition of this Objective-C class, taking it from |
| 1537 | /// a forward declaration (\@class) to a definition (\@interface). |
| 1538 | void startDefinition(); |
| 1539 | |
| 1540 | /// Retrieve the superclass type. |
| 1541 | const ObjCObjectType *getSuperClassType() const { |
| 1542 | if (TypeSourceInfo *TInfo = getSuperClassTInfo()) |
| 1543 | return TInfo->getType()->castAs<ObjCObjectType>(); |
| 1544 | |
| 1545 | return nullptr; |
| 1546 | } |
| 1547 | |
| 1548 | // Retrieve the type source information for the superclass. |
| 1549 | TypeSourceInfo *getSuperClassTInfo() const { |
| 1550 | // FIXME: Should make sure no callers ever do this. |
| 1551 | if (!hasDefinition()) |
| 1552 | return nullptr; |
| 1553 | |
| 1554 | if (data().ExternallyCompleted) |
| 1555 | LoadExternalDefinition(); |
| 1556 | |
| 1557 | return data().SuperClassTInfo; |
| 1558 | } |
| 1559 | |
| 1560 | // Retrieve the declaration for the superclass of this class, which |
| 1561 | // does not include any type arguments that apply to the superclass. |
| 1562 | ObjCInterfaceDecl *getSuperClass() const; |
| 1563 | |
| 1564 | void setSuperClass(TypeSourceInfo *superClass) { |
| 1565 | data().SuperClassTInfo = superClass; |
| 1566 | } |
| 1567 | |
| 1568 | /// Iterator that walks over the list of categories, filtering out |
| 1569 | /// those that do not meet specific criteria. |
| 1570 | /// |
| 1571 | /// This class template is used for the various permutations of category |
| 1572 | /// and extension iterators. |
| 1573 | template<bool (*Filter)(ObjCCategoryDecl *)> |
| 1574 | class filtered_category_iterator { |
| 1575 | ObjCCategoryDecl *Current = nullptr; |
| 1576 | |
| 1577 | void findAcceptableCategory(); |
| 1578 | |
| 1579 | public: |
| 1580 | using value_type = ObjCCategoryDecl *; |
| 1581 | using reference = value_type; |
| 1582 | using pointer = value_type; |
| 1583 | using difference_type = std::ptrdiff_t; |
| 1584 | using iterator_category = std::input_iterator_tag; |
| 1585 | |
| 1586 | filtered_category_iterator() = default; |
| 1587 | explicit filtered_category_iterator(ObjCCategoryDecl *Current) |
| 1588 | : Current(Current) { |
| 1589 | findAcceptableCategory(); |
| 1590 | } |
| 1591 | |
| 1592 | reference operator*() const { return Current; } |
| 1593 | pointer operator->() const { return Current; } |
| 1594 | |
| 1595 | filtered_category_iterator &operator++(); |
| 1596 | |
| 1597 | filtered_category_iterator operator++(int) { |
| 1598 | filtered_category_iterator Tmp = *this; |
| 1599 | ++(*this); |
| 1600 | return Tmp; |
| 1601 | } |
| 1602 | |
| 1603 | friend bool operator==(filtered_category_iterator X, |
| 1604 | filtered_category_iterator Y) { |
| 1605 | return X.Current == Y.Current; |
| 1606 | } |
| 1607 | |
| 1608 | friend bool operator!=(filtered_category_iterator X, |
| 1609 | filtered_category_iterator Y) { |
| 1610 | return X.Current != Y.Current; |
| 1611 | } |
| 1612 | }; |
| 1613 | |
| 1614 | private: |
| 1615 | /// Test whether the given category is visible. |
| 1616 | /// |
| 1617 | /// Used in the \c visible_categories_iterator. |
| 1618 | static bool isVisibleCategory(ObjCCategoryDecl *Cat); |
| 1619 | |
| 1620 | public: |
| 1621 | /// Iterator that walks over the list of categories and extensions |
| 1622 | /// that are visible, i.e., not hidden in a non-imported submodule. |
| 1623 | using visible_categories_iterator = |
| 1624 | filtered_category_iterator<isVisibleCategory>; |
| 1625 | |
| 1626 | using visible_categories_range = |
| 1627 | llvm::iterator_range<visible_categories_iterator>; |
| 1628 | |
| 1629 | visible_categories_range visible_categories() const { |
| 1630 | return visible_categories_range(visible_categories_begin(), |
| 1631 | visible_categories_end()); |
| 1632 | } |
| 1633 | |
| 1634 | /// Retrieve an iterator to the beginning of the visible-categories |
| 1635 | /// list. |
| 1636 | visible_categories_iterator visible_categories_begin() const { |
| 1637 | return visible_categories_iterator(getCategoryListRaw()); |
| 1638 | } |
| 1639 | |
| 1640 | /// Retrieve an iterator to the end of the visible-categories list. |
| 1641 | visible_categories_iterator visible_categories_end() const { |
| 1642 | return visible_categories_iterator(); |
| 1643 | } |
| 1644 | |
| 1645 | /// Determine whether the visible-categories list is empty. |
| 1646 | bool visible_categories_empty() const { |
| 1647 | return visible_categories_begin() == visible_categories_end(); |
| 1648 | } |
| 1649 | |
| 1650 | private: |
| 1651 | /// Test whether the given category... is a category. |
| 1652 | /// |
| 1653 | /// Used in the \c known_categories_iterator. |
| 1654 | static bool isKnownCategory(ObjCCategoryDecl *) { return true; } |
| 1655 | |
| 1656 | public: |
| 1657 | /// Iterator that walks over all of the known categories and |
| 1658 | /// extensions, including those that are hidden. |
| 1659 | using known_categories_iterator = filtered_category_iterator<isKnownCategory>; |
| 1660 | using known_categories_range = |
| 1661 | llvm::iterator_range<known_categories_iterator>; |
| 1662 | |
| 1663 | known_categories_range known_categories() const { |
| 1664 | return known_categories_range(known_categories_begin(), |
| 1665 | known_categories_end()); |
| 1666 | } |
| 1667 | |
| 1668 | /// Retrieve an iterator to the beginning of the known-categories |
| 1669 | /// list. |
| 1670 | known_categories_iterator known_categories_begin() const { |
| 1671 | return known_categories_iterator(getCategoryListRaw()); |
| 1672 | } |
| 1673 | |
| 1674 | /// Retrieve an iterator to the end of the known-categories list. |
| 1675 | known_categories_iterator known_categories_end() const { |
| 1676 | return known_categories_iterator(); |
| 1677 | } |
| 1678 | |
| 1679 | /// Determine whether the known-categories list is empty. |
| 1680 | bool known_categories_empty() const { |
| 1681 | return known_categories_begin() == known_categories_end(); |
| 1682 | } |
| 1683 | |
| 1684 | private: |
| 1685 | /// Test whether the given category is a visible extension. |
| 1686 | /// |
| 1687 | /// Used in the \c visible_extensions_iterator. |
| 1688 | static bool isVisibleExtension(ObjCCategoryDecl *Cat); |
| 1689 | |
| 1690 | public: |
| 1691 | /// Iterator that walks over all of the visible extensions, skipping |
| 1692 | /// any that are known but hidden. |
| 1693 | using visible_extensions_iterator = |
| 1694 | filtered_category_iterator<isVisibleExtension>; |
| 1695 | |
| 1696 | using visible_extensions_range = |
| 1697 | llvm::iterator_range<visible_extensions_iterator>; |
| 1698 | |
| 1699 | visible_extensions_range visible_extensions() const { |
| 1700 | return visible_extensions_range(visible_extensions_begin(), |
| 1701 | visible_extensions_end()); |
| 1702 | } |
| 1703 | |
| 1704 | /// Retrieve an iterator to the beginning of the visible-extensions |
| 1705 | /// list. |
| 1706 | visible_extensions_iterator visible_extensions_begin() const { |
| 1707 | return visible_extensions_iterator(getCategoryListRaw()); |
| 1708 | } |
| 1709 | |
| 1710 | /// Retrieve an iterator to the end of the visible-extensions list. |
| 1711 | visible_extensions_iterator visible_extensions_end() const { |
| 1712 | return visible_extensions_iterator(); |
| 1713 | } |
| 1714 | |
| 1715 | /// Determine whether the visible-extensions list is empty. |
| 1716 | bool visible_extensions_empty() const { |
| 1717 | return visible_extensions_begin() == visible_extensions_end(); |
| 1718 | } |
| 1719 | |
| 1720 | private: |
| 1721 | /// Test whether the given category is an extension. |
| 1722 | /// |
| 1723 | /// Used in the \c known_extensions_iterator. |
| 1724 | static bool isKnownExtension(ObjCCategoryDecl *Cat); |
| 1725 | |
| 1726 | public: |
| 1727 | friend class ASTDeclReader; |
| 1728 | friend class ASTDeclWriter; |
| 1729 | friend class ASTReader; |
| 1730 | |
| 1731 | /// Iterator that walks over all of the known extensions. |
| 1732 | using known_extensions_iterator = |
| 1733 | filtered_category_iterator<isKnownExtension>; |
| 1734 | using known_extensions_range = |
| 1735 | llvm::iterator_range<known_extensions_iterator>; |
| 1736 | |
| 1737 | known_extensions_range known_extensions() const { |
| 1738 | return known_extensions_range(known_extensions_begin(), |
| 1739 | known_extensions_end()); |
| 1740 | } |
| 1741 | |
| 1742 | /// Retrieve an iterator to the beginning of the known-extensions |
| 1743 | /// list. |
| 1744 | known_extensions_iterator known_extensions_begin() const { |
| 1745 | return known_extensions_iterator(getCategoryListRaw()); |
| 1746 | } |
| 1747 | |
| 1748 | /// Retrieve an iterator to the end of the known-extensions list. |
| 1749 | known_extensions_iterator known_extensions_end() const { |
| 1750 | return known_extensions_iterator(); |
| 1751 | } |
| 1752 | |
| 1753 | /// Determine whether the known-extensions list is empty. |
| 1754 | bool known_extensions_empty() const { |
| 1755 | return known_extensions_begin() == known_extensions_end(); |
| 1756 | } |
| 1757 | |
| 1758 | /// Retrieve the raw pointer to the start of the category/extension |
| 1759 | /// list. |
| 1760 | ObjCCategoryDecl* getCategoryListRaw() const { |
| 1761 | // FIXME: Should make sure no callers ever do this. |
| 1762 | if (!hasDefinition()) |
| 1763 | return nullptr; |
| 1764 | |
| 1765 | if (data().ExternallyCompleted) |
| 1766 | LoadExternalDefinition(); |
| 1767 | |
| 1768 | return data().CategoryList; |
| 1769 | } |
| 1770 | |
| 1771 | /// Set the raw pointer to the start of the category/extension |
| 1772 | /// list. |
| 1773 | void setCategoryListRaw(ObjCCategoryDecl *category) { |
| 1774 | data().CategoryList = category; |
| 1775 | } |
| 1776 | |
| 1777 | ObjCPropertyDecl |
| 1778 | *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId, |
| 1779 | ObjCPropertyQueryKind QueryKind) const; |
| 1780 | |
| 1781 | void collectPropertiesToImplement(PropertyMap &PM, |
| 1782 | PropertyDeclOrder &PO) const override; |
| 1783 | |
| 1784 | /// isSuperClassOf - Return true if this class is the specified class or is a |
| 1785 | /// super class of the specified interface class. |
| 1786 | bool isSuperClassOf(const ObjCInterfaceDecl *I) const { |
| 1787 | // If RHS is derived from LHS it is OK; else it is not OK. |
| 1788 | while (I != nullptr) { |
| 1789 | if (declaresSameEntity(this, I)) |
| 1790 | return true; |
| 1791 | |
| 1792 | I = I->getSuperClass(); |
| 1793 | } |
| 1794 | return false; |
| 1795 | } |
| 1796 | |
| 1797 | /// isArcWeakrefUnavailable - Checks for a class or one of its super classes |
| 1798 | /// to be incompatible with __weak references. Returns true if it is. |
| 1799 | bool isArcWeakrefUnavailable() const; |
| 1800 | |
| 1801 | /// isObjCRequiresPropertyDefs - Checks that a class or one of its super |
| 1802 | /// classes must not be auto-synthesized. Returns class decl. if it must not |
| 1803 | /// be; 0, otherwise. |
| 1804 | const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const; |
| 1805 | |
| 1806 | ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName, |
| 1807 | ObjCInterfaceDecl *&ClassDeclared); |
| 1808 | ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) { |
| 1809 | ObjCInterfaceDecl *ClassDeclared; |
| 1810 | return lookupInstanceVariable(IVarName, ClassDeclared); |
| 1811 | } |
| 1812 | |
| 1813 | ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name); |
| 1814 | |
| 1815 | // Lookup a method. First, we search locally. If a method isn't |
| 1816 | // found, we search referenced protocols and class categories. |
| 1817 | ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance, |
| 1818 | bool shallowCategoryLookup = false, |
| 1819 | bool followSuper = true, |
| 1820 | const ObjCCategoryDecl *C = nullptr) const; |
| 1821 | |
| 1822 | /// Lookup an instance method for a given selector. |
| 1823 | ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const { |
| 1824 | return lookupMethod(Sel, true/*isInstance*/); |
| 1825 | } |
| 1826 | |
| 1827 | /// Lookup a class method for a given selector. |
| 1828 | ObjCMethodDecl *lookupClassMethod(Selector Sel) const { |
| 1829 | return lookupMethod(Sel, false/*isInstance*/); |
| 1830 | } |
| 1831 | |
| 1832 | ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName); |
| 1833 | |
| 1834 | /// Lookup a method in the classes implementation hierarchy. |
| 1835 | ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel, |
| 1836 | bool Instance=true) const; |
| 1837 | |
| 1838 | ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) { |
| 1839 | return lookupPrivateMethod(Sel, false); |
| 1840 | } |
| 1841 | |
| 1842 | /// Lookup a setter or getter in the class hierarchy, |
| 1843 | /// including in all categories except for category passed |
| 1844 | /// as argument. |
| 1845 | ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel, |
| 1846 | const ObjCCategoryDecl *Cat, |
| 1847 | bool IsClassProperty) const { |
| 1848 | return lookupMethod(Sel, !IsClassProperty/*isInstance*/, |
| 1849 | false/*shallowCategoryLookup*/, |
| 1850 | true /* followsSuper */, |
| 1851 | Cat); |
| 1852 | } |
| 1853 | |
| 1854 | SourceLocation getEndOfDefinitionLoc() const { |
| 1855 | if (!hasDefinition()) |
| 1856 | return getLocation(); |
| 1857 | |
| 1858 | return data().EndLoc; |
| 1859 | } |
| 1860 | |
| 1861 | void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; } |
| 1862 | |
| 1863 | /// Retrieve the starting location of the superclass. |
| 1864 | SourceLocation getSuperClassLoc() const; |
| 1865 | |
| 1866 | /// isImplicitInterfaceDecl - check that this is an implicitly declared |
| 1867 | /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation |
| 1868 | /// declaration without an \@interface declaration. |
| 1869 | bool isImplicitInterfaceDecl() const { |
| 1870 | return hasDefinition() ? data().Definition->isImplicit() : isImplicit(); |
| 1871 | } |
| 1872 | |
| 1873 | /// ClassImplementsProtocol - Checks that 'lProto' protocol |
| 1874 | /// has been implemented in IDecl class, its super class or categories (if |
| 1875 | /// lookupCategory is true). |
| 1876 | bool ClassImplementsProtocol(ObjCProtocolDecl *lProto, |
| 1877 | bool lookupCategory, |
| 1878 | bool RHSIsQualifiedID = false); |
| 1879 | |
| 1880 | using redecl_range = redeclarable_base::redecl_range; |
| 1881 | using redecl_iterator = redeclarable_base::redecl_iterator; |
| 1882 | |
| 1883 | using redeclarable_base::redecls_begin; |
| 1884 | using redeclarable_base::redecls_end; |
| 1885 | using redeclarable_base::redecls; |
| 1886 | using redeclarable_base::getPreviousDecl; |
| 1887 | using redeclarable_base::getMostRecentDecl; |
| 1888 | using redeclarable_base::isFirstDecl; |
| 1889 | |
| 1890 | /// Retrieves the canonical declaration of this Objective-C class. |
| 1891 | ObjCInterfaceDecl *getCanonicalDecl() override { return getFirstDecl(); } |
| 1892 | const ObjCInterfaceDecl *getCanonicalDecl() const { return getFirstDecl(); } |
| 1893 | |
| 1894 | // Low-level accessor |
| 1895 | const Type *getTypeForDecl() const { return TypeForDecl; } |
| 1896 | void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; } |
| 1897 | |
| 1898 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 1899 | static bool classofKind(Kind K) { return K == ObjCInterface; } |
| 1900 | |
| 1901 | private: |
| 1902 | const ObjCInterfaceDecl *findInterfaceWithDesignatedInitializers() const; |
| 1903 | bool inheritsDesignatedInitializers() const; |
| 1904 | }; |
| 1905 | |
| 1906 | /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC |
| 1907 | /// instance variables are identical to C. The only exception is Objective-C |
| 1908 | /// supports C++ style access control. For example: |
| 1909 | /// |
| 1910 | /// \@interface IvarExample : NSObject |
| 1911 | /// { |
| 1912 | /// id defaultToProtected; |
| 1913 | /// \@public: |
| 1914 | /// id canBePublic; // same as C++. |
| 1915 | /// \@protected: |
| 1916 | /// id canBeProtected; // same as C++. |
| 1917 | /// \@package: |
| 1918 | /// id canBePackage; // framework visibility (not available in C++). |
| 1919 | /// } |
| 1920 | /// |
| 1921 | class ObjCIvarDecl : public FieldDecl { |
| 1922 | void anchor() override; |
| 1923 | |
| 1924 | public: |
| 1925 | enum AccessControl { |
| 1926 | None, Private, Protected, Public, Package |
| 1927 | }; |
| 1928 | |
| 1929 | private: |
| 1930 | ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc, |
| 1931 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 1932 | QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW, |
| 1933 | bool synthesized) |
| 1934 | : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW, |
| 1935 | /*Mutable=*/false, /*HasInit=*/ICIS_NoInit), |
| 1936 | DeclAccess(ac), Synthesized(synthesized) {} |
| 1937 | |
| 1938 | public: |
| 1939 | static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC, |
| 1940 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 1941 | IdentifierInfo *Id, QualType T, |
| 1942 | TypeSourceInfo *TInfo, |
| 1943 | AccessControl ac, Expr *BW = nullptr, |
| 1944 | bool synthesized=false); |
| 1945 | |
| 1946 | static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
| 1947 | |
| 1948 | /// Return the class interface that this ivar is logically contained |
| 1949 | /// in; this is either the interface where the ivar was declared, or the |
| 1950 | /// interface the ivar is conceptually a part of in the case of synthesized |
| 1951 | /// ivars. |
| 1952 | const ObjCInterfaceDecl *getContainingInterface() const; |
| 1953 | |
| 1954 | ObjCIvarDecl *getNextIvar() { return NextIvar; } |
| 1955 | const ObjCIvarDecl *getNextIvar() const { return NextIvar; } |
| 1956 | void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; } |
| 1957 | |
| 1958 | void setAccessControl(AccessControl ac) { DeclAccess = ac; } |
| 1959 | |
| 1960 | AccessControl getAccessControl() const { return AccessControl(DeclAccess); } |
| 1961 | |
| 1962 | AccessControl getCanonicalAccessControl() const { |
| 1963 | return DeclAccess == None ? Protected : AccessControl(DeclAccess); |
| 1964 | } |
| 1965 | |
| 1966 | void setSynthesize(bool synth) { Synthesized = synth; } |
| 1967 | bool getSynthesize() const { return Synthesized; } |
| 1968 | |
| 1969 | /// Retrieve the type of this instance variable when viewed as a member of a |
| 1970 | /// specific object type. |
| 1971 | QualType getUsageType(QualType objectType) const; |
| 1972 | |
| 1973 | // Implement isa/cast/dyncast/etc. |
| 1974 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 1975 | static bool classofKind(Kind K) { return K == ObjCIvar; } |
| 1976 | |
| 1977 | private: |
| 1978 | /// NextIvar - Next Ivar in the list of ivars declared in class; class's |
| 1979 | /// extensions and class's implementation |
| 1980 | ObjCIvarDecl *NextIvar = nullptr; |
| 1981 | |
| 1982 | // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum |
| 1983 | unsigned DeclAccess : 3; |
| 1984 | unsigned Synthesized : 1; |
| 1985 | }; |
| 1986 | |
| 1987 | /// Represents a field declaration created by an \@defs(...). |
| 1988 | class ObjCAtDefsFieldDecl : public FieldDecl { |
| 1989 | ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc, |
| 1990 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 1991 | QualType T, Expr *BW) |
| 1992 | : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T, |
| 1993 | /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ? |
| 1994 | BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {} |
| 1995 | |
| 1996 | void anchor() override; |
| 1997 | |
| 1998 | public: |
| 1999 | static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC, |
| 2000 | SourceLocation StartLoc, |
| 2001 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 2002 | QualType T, Expr *BW); |
| 2003 | |
| 2004 | static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
| 2005 | |
| 2006 | // Implement isa/cast/dyncast/etc. |
| 2007 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 2008 | static bool classofKind(Kind K) { return K == ObjCAtDefsField; } |
| 2009 | }; |
| 2010 | |
| 2011 | /// Represents an Objective-C protocol declaration. |
| 2012 | /// |
| 2013 | /// Objective-C protocols declare a pure abstract type (i.e., no instance |
| 2014 | /// variables are permitted). Protocols originally drew inspiration from |
| 2015 | /// C++ pure virtual functions (a C++ feature with nice semantics and lousy |
| 2016 | /// syntax:-). Here is an example: |
| 2017 | /// |
| 2018 | /// \code |
| 2019 | /// \@protocol NSDraggingInfo <refproto1, refproto2> |
| 2020 | /// - (NSWindow *)draggingDestinationWindow; |
| 2021 | /// - (NSImage *)draggedImage; |
| 2022 | /// \@end |
| 2023 | /// \endcode |
| 2024 | /// |
| 2025 | /// This says that NSDraggingInfo requires two methods and requires everything |
| 2026 | /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as |
| 2027 | /// well. |
| 2028 | /// |
| 2029 | /// \code |
| 2030 | /// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo> |
| 2031 | /// \@end |
| 2032 | /// \endcode |
| 2033 | /// |
| 2034 | /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and |
| 2035 | /// protocols are in distinct namespaces. For example, Cocoa defines both |
| 2036 | /// an NSObject protocol and class (which isn't allowed in Java). As a result, |
| 2037 | /// protocols are referenced using angle brackets as follows: |
| 2038 | /// |
| 2039 | /// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo; |
| 2040 | class ObjCProtocolDecl : public ObjCContainerDecl, |
| 2041 | public Redeclarable<ObjCProtocolDecl> { |
| 2042 | struct DefinitionData { |
| 2043 | // The declaration that defines this protocol. |
| 2044 | ObjCProtocolDecl *Definition; |
| 2045 | |
| 2046 | /// Referenced protocols |
| 2047 | ObjCProtocolList ReferencedProtocols; |
| 2048 | }; |
| 2049 | |
| 2050 | /// Contains a pointer to the data associated with this class, |
| 2051 | /// which will be NULL if this class has not yet been defined. |
| 2052 | /// |
| 2053 | /// The bit indicates when we don't need to check for out-of-date |
| 2054 | /// declarations. It will be set unless modules are enabled. |
| 2055 | llvm::PointerIntPair<DefinitionData *, 1, bool> Data; |
| 2056 | |
| 2057 | ObjCProtocolDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id, |
| 2058 | SourceLocation nameLoc, SourceLocation atStartLoc, |
| 2059 | ObjCProtocolDecl *PrevDecl); |
| 2060 | |
| 2061 | void anchor() override; |
| 2062 | |
| 2063 | DefinitionData &data() const { |
| 2064 | assert(Data.getPointer() && "Objective-C protocol has no definition!")((void)0); |
| 2065 | return *Data.getPointer(); |
| 2066 | } |
| 2067 | |
| 2068 | void allocateDefinitionData(); |
| 2069 | |
| 2070 | using redeclarable_base = Redeclarable<ObjCProtocolDecl>; |
| 2071 | |
| 2072 | ObjCProtocolDecl *getNextRedeclarationImpl() override { |
| 2073 | return getNextRedeclaration(); |
| 2074 | } |
| 2075 | |
| 2076 | ObjCProtocolDecl *getPreviousDeclImpl() override { |
| 2077 | return getPreviousDecl(); |
| 2078 | } |
| 2079 | |
| 2080 | ObjCProtocolDecl *getMostRecentDeclImpl() override { |
| 2081 | return getMostRecentDecl(); |
| 2082 | } |
| 2083 | |
| 2084 | public: |
| 2085 | friend class ASTDeclReader; |
| 2086 | friend class ASTDeclWriter; |
| 2087 | friend class ASTReader; |
| 2088 | |
| 2089 | static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC, |
| 2090 | IdentifierInfo *Id, |
| 2091 | SourceLocation nameLoc, |
| 2092 | SourceLocation atStartLoc, |
| 2093 | ObjCProtocolDecl *PrevDecl); |
| 2094 | |
| 2095 | static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
| 2096 | |
| 2097 | const ObjCProtocolList &getReferencedProtocols() const { |
| 2098 | assert(hasDefinition() && "No definition available!")((void)0); |
| 2099 | return data().ReferencedProtocols; |
| 2100 | } |
| 2101 | |
| 2102 | using protocol_iterator = ObjCProtocolList::iterator; |
| 2103 | using protocol_range = llvm::iterator_range<protocol_iterator>; |
| 2104 | |
| 2105 | protocol_range protocols() const { |
| 2106 | return protocol_range(protocol_begin(), protocol_end()); |
| 2107 | } |
| 2108 | |
| 2109 | protocol_iterator protocol_begin() const { |
| 2110 | if (!hasDefinition()) |
| 2111 | return protocol_iterator(); |
| 2112 | |
| 2113 | return data().ReferencedProtocols.begin(); |
| 2114 | } |
| 2115 | |
| 2116 | protocol_iterator protocol_end() const { |
| 2117 | if (!hasDefinition()) |
| 2118 | return protocol_iterator(); |
| 2119 | |
| 2120 | return data().ReferencedProtocols.end(); |
| 2121 | } |
| 2122 | |
| 2123 | using protocol_loc_iterator = ObjCProtocolList::loc_iterator; |
| 2124 | using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>; |
| 2125 | |
| 2126 | protocol_loc_range protocol_locs() const { |
| 2127 | return protocol_loc_range(protocol_loc_begin(), protocol_loc_end()); |
| 2128 | } |
| 2129 | |
| 2130 | protocol_loc_iterator protocol_loc_begin() const { |
| 2131 | if (!hasDefinition()) |
| 2132 | return protocol_loc_iterator(); |
| 2133 | |
| 2134 | return data().ReferencedProtocols.loc_begin(); |
| 2135 | } |
| 2136 | |
| 2137 | protocol_loc_iterator protocol_loc_end() const { |
| 2138 | if (!hasDefinition()) |
| 2139 | return protocol_loc_iterator(); |
| 2140 | |
| 2141 | return data().ReferencedProtocols.loc_end(); |
| 2142 | } |
| 2143 | |
| 2144 | unsigned protocol_size() const { |
| 2145 | if (!hasDefinition()) |
| 2146 | return 0; |
| 2147 | |
| 2148 | return data().ReferencedProtocols.size(); |
| 2149 | } |
| 2150 | |
| 2151 | /// setProtocolList - Set the list of protocols that this interface |
| 2152 | /// implements. |
| 2153 | void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num, |
| 2154 | const SourceLocation *Locs, ASTContext &C) { |
| 2155 | assert(hasDefinition() && "Protocol is not defined")((void)0); |
| 2156 | data().ReferencedProtocols.set(List, Num, Locs, C); |
| 2157 | } |
| 2158 | |
| 2159 | /// This is true iff the protocol is tagged with the |
| 2160 | /// `objc_non_runtime_protocol` attribute. |
| 2161 | bool isNonRuntimeProtocol() const; |
| 2162 | |
| 2163 | /// Get the set of all protocols implied by this protocols inheritance |
| 2164 | /// hierarchy. |
| 2165 | void getImpliedProtocols(llvm::DenseSet<const ObjCProtocolDecl *> &IPs) const; |
| 2166 | |
| 2167 | ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName); |
| 2168 | |
| 2169 | // Lookup a method. First, we search locally. If a method isn't |
| 2170 | // found, we search referenced protocols and class categories. |
| 2171 | ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const; |
| 2172 | |
| 2173 | ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const { |
| 2174 | return lookupMethod(Sel, true/*isInstance*/); |
| 2175 | } |
| 2176 | |
| 2177 | ObjCMethodDecl *lookupClassMethod(Selector Sel) const { |
| 2178 | return lookupMethod(Sel, false/*isInstance*/); |
| 2179 | } |
| 2180 | |
| 2181 | /// Determine whether this protocol has a definition. |
| 2182 | bool hasDefinition() const { |
| 2183 | // If the name of this protocol is out-of-date, bring it up-to-date, which |
| 2184 | // might bring in a definition. |
| 2185 | // Note: a null value indicates that we don't have a definition and that |
| 2186 | // modules are enabled. |
| 2187 | if (!Data.getOpaqueValue()) |
| 2188 | getMostRecentDecl(); |
| 2189 | |
| 2190 | return Data.getPointer(); |
| 2191 | } |
| 2192 | |
| 2193 | /// Retrieve the definition of this protocol, if any. |
| 2194 | ObjCProtocolDecl *getDefinition() { |
| 2195 | return hasDefinition()? Data.getPointer()->Definition : nullptr; |
| 2196 | } |
| 2197 | |
| 2198 | /// Retrieve the definition of this protocol, if any. |
| 2199 | const ObjCProtocolDecl *getDefinition() const { |
| 2200 | return hasDefinition()? Data.getPointer()->Definition : nullptr; |
| 2201 | } |
| 2202 | |
| 2203 | /// Determine whether this particular declaration is also the |
| 2204 | /// definition. |
| 2205 | bool isThisDeclarationADefinition() const { |
| 2206 | return getDefinition() == this; |
| 2207 | } |
| 2208 | |
| 2209 | /// Starts the definition of this Objective-C protocol. |
| 2210 | void startDefinition(); |
| 2211 | |
| 2212 | /// Produce a name to be used for protocol's metadata. It comes either via |
| 2213 | /// objc_runtime_name attribute or protocol name. |
| 2214 | StringRef getObjCRuntimeNameAsString() const; |
| 2215 | |
| 2216 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
| 2217 | if (isThisDeclarationADefinition()) |
| 2218 | return ObjCContainerDecl::getSourceRange(); |
| 2219 | |
| 2220 | return SourceRange(getAtStartLoc(), getLocation()); |
| 2221 | } |
| 2222 | |
| 2223 | using redecl_range = redeclarable_base::redecl_range; |
| 2224 | using redecl_iterator = redeclarable_base::redecl_iterator; |
| 2225 | |
| 2226 | using redeclarable_base::redecls_begin; |
| 2227 | using redeclarable_base::redecls_end; |
| 2228 | using redeclarable_base::redecls; |
| 2229 | using redeclarable_base::getPreviousDecl; |
| 2230 | using redeclarable_base::getMostRecentDecl; |
| 2231 | using redeclarable_base::isFirstDecl; |
| 2232 | |
| 2233 | /// Retrieves the canonical declaration of this Objective-C protocol. |
| 2234 | ObjCProtocolDecl *getCanonicalDecl() override { return getFirstDecl(); } |
| 2235 | const ObjCProtocolDecl *getCanonicalDecl() const { return getFirstDecl(); } |
| 2236 | |
| 2237 | void collectPropertiesToImplement(PropertyMap &PM, |
| 2238 | PropertyDeclOrder &PO) const override; |
| 2239 | |
| 2240 | void collectInheritedProtocolProperties(const ObjCPropertyDecl *Property, |
| 2241 | ProtocolPropertySet &PS, |
| 2242 | PropertyDeclOrder &PO) const; |
| 2243 | |
| 2244 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 2245 | static bool classofKind(Kind K) { return K == ObjCProtocol; } |
| 2246 | }; |
| 2247 | |
| 2248 | /// ObjCCategoryDecl - Represents a category declaration. A category allows |
| 2249 | /// you to add methods to an existing class (without subclassing or modifying |
| 2250 | /// the original class interface or implementation:-). Categories don't allow |
| 2251 | /// you to add instance data. The following example adds "myMethod" to all |
| 2252 | /// NSView's within a process: |
| 2253 | /// |
| 2254 | /// \@interface NSView (MyViewMethods) |
| 2255 | /// - myMethod; |
| 2256 | /// \@end |
| 2257 | /// |
| 2258 | /// Categories also allow you to split the implementation of a class across |
| 2259 | /// several files (a feature more naturally supported in C++). |
| 2260 | /// |
| 2261 | /// Categories were originally inspired by dynamic languages such as Common |
| 2262 | /// Lisp and Smalltalk. More traditional class-based languages (C++, Java) |
| 2263 | /// don't support this level of dynamism, which is both powerful and dangerous. |
| 2264 | class ObjCCategoryDecl : public ObjCContainerDecl { |
| 2265 | /// Interface belonging to this category |
| 2266 | ObjCInterfaceDecl *ClassInterface; |
| 2267 | |
| 2268 | /// The type parameters associated with this category, if any. |
| 2269 | ObjCTypeParamList *TypeParamList = nullptr; |
| 2270 | |
| 2271 | /// referenced protocols in this category. |
| 2272 | ObjCProtocolList ReferencedProtocols; |
| 2273 | |
| 2274 | /// Next category belonging to this class. |
| 2275 | /// FIXME: this should not be a singly-linked list. Move storage elsewhere. |
| 2276 | ObjCCategoryDecl *NextClassCategory = nullptr; |
| 2277 | |
| 2278 | /// The location of the category name in this declaration. |
| 2279 | SourceLocation CategoryNameLoc; |
| 2280 | |
| 2281 | /// class extension may have private ivars. |
| 2282 | SourceLocation IvarLBraceLoc; |
| 2283 | SourceLocation IvarRBraceLoc; |
| 2284 | |
| 2285 | ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc, |
| 2286 | SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc, |
| 2287 | IdentifierInfo *Id, ObjCInterfaceDecl *IDecl, |
| 2288 | ObjCTypeParamList *typeParamList, |
| 2289 | SourceLocation IvarLBraceLoc = SourceLocation(), |
| 2290 | SourceLocation IvarRBraceLoc = SourceLocation()); |
| 2291 | |
| 2292 | void anchor() override; |
| 2293 | |
| 2294 | public: |
| 2295 | friend class ASTDeclReader; |
| 2296 | friend class ASTDeclWriter; |
| 2297 | |
| 2298 | static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC, |
| 2299 | SourceLocation AtLoc, |
| 2300 | SourceLocation ClassNameLoc, |
| 2301 | SourceLocation CategoryNameLoc, |
| 2302 | IdentifierInfo *Id, |
| 2303 | ObjCInterfaceDecl *IDecl, |
| 2304 | ObjCTypeParamList *typeParamList, |
| 2305 | SourceLocation IvarLBraceLoc=SourceLocation(), |
| 2306 | SourceLocation IvarRBraceLoc=SourceLocation()); |
| 2307 | static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
| 2308 | |
| 2309 | ObjCInterfaceDecl *getClassInterface() { return ClassInterface; } |
| 2310 | const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } |
| 2311 | |
| 2312 | /// Retrieve the type parameter list associated with this category or |
| 2313 | /// extension. |
| 2314 | ObjCTypeParamList *getTypeParamList() const { return TypeParamList; } |
| 2315 | |
| 2316 | /// Set the type parameters of this category. |
| 2317 | /// |
| 2318 | /// This function is used by the AST importer, which must import the type |
| 2319 | /// parameters after creating their DeclContext to avoid loops. |
| 2320 | void setTypeParamList(ObjCTypeParamList *TPL); |
| 2321 | |
| 2322 | |
| 2323 | ObjCCategoryImplDecl *getImplementation() const; |
| 2324 | void setImplementation(ObjCCategoryImplDecl *ImplD); |
| 2325 | |
| 2326 | /// setProtocolList - Set the list of protocols that this interface |
| 2327 | /// implements. |
| 2328 | void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num, |
| 2329 | const SourceLocation *Locs, ASTContext &C) { |
| 2330 | ReferencedProtocols.set(List, Num, Locs, C); |
| 2331 | } |
| 2332 | |
| 2333 | const ObjCProtocolList &getReferencedProtocols() const { |
| 2334 | return ReferencedProtocols; |
| 2335 | } |
| 2336 | |
| 2337 | using protocol_iterator = ObjCProtocolList::iterator; |
| 2338 | using protocol_range = llvm::iterator_range<protocol_iterator>; |
| 2339 | |
| 2340 | protocol_range protocols() const { |
| 2341 | return protocol_range(protocol_begin(), protocol_end()); |
| 2342 | } |
| 2343 | |
| 2344 | protocol_iterator protocol_begin() const { |
| 2345 | return ReferencedProtocols.begin(); |
| 2346 | } |
| 2347 | |
| 2348 | protocol_iterator protocol_end() const { return ReferencedProtocols.end(); } |
| 2349 | unsigned protocol_size() const { return ReferencedProtocols.size(); } |
| 2350 | |
| 2351 | using protocol_loc_iterator = ObjCProtocolList::loc_iterator; |
| 2352 | using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>; |
| 2353 | |
| 2354 | protocol_loc_range protocol_locs() const { |
| 2355 | return protocol_loc_range(protocol_loc_begin(), protocol_loc_end()); |
| 2356 | } |
| 2357 | |
| 2358 | protocol_loc_iterator protocol_loc_begin() const { |
| 2359 | return ReferencedProtocols.loc_begin(); |
| 2360 | } |
| 2361 | |
| 2362 | protocol_loc_iterator protocol_loc_end() const { |
| 2363 | return ReferencedProtocols.loc_end(); |
| 2364 | } |
| 2365 | |
| 2366 | ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; } |
| 2367 | |
| 2368 | /// Retrieve the pointer to the next stored category (or extension), |
| 2369 | /// which may be hidden. |
| 2370 | ObjCCategoryDecl *getNextClassCategoryRaw() const { |
| 2371 | return NextClassCategory; |
| 2372 | } |
| 2373 | |
| 2374 | bool IsClassExtension() const { return getIdentifier() == nullptr; } |
| 2375 | |
| 2376 | using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>; |
| 2377 | using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>; |
| 2378 | |
| 2379 | ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); } |
| 2380 | |
| 2381 | ivar_iterator ivar_begin() const { |
| 2382 | return ivar_iterator(decls_begin()); |
| 2383 | } |
| 2384 | |
| 2385 | ivar_iterator ivar_end() const { |
| 2386 | return ivar_iterator(decls_end()); |
| 2387 | } |
| 2388 | |
| 2389 | unsigned ivar_size() const { |
| 2390 | return std::distance(ivar_begin(), ivar_end()); |
| 2391 | } |
| 2392 | |
| 2393 | bool ivar_empty() const { |
| 2394 | return ivar_begin() == ivar_end(); |
| 2395 | } |
| 2396 | |
| 2397 | SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; } |
| 2398 | void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; } |
| 2399 | |
| 2400 | void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; } |
| 2401 | SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; } |
| 2402 | void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; } |
| 2403 | SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; } |
| 2404 | |
| 2405 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 2406 | static bool classofKind(Kind K) { return K == ObjCCategory; } |
| 2407 | }; |
| 2408 | |
| 2409 | class ObjCImplDecl : public ObjCContainerDecl { |
| 2410 | /// Class interface for this class/category implementation |
| 2411 | ObjCInterfaceDecl *ClassInterface; |
| 2412 | |
| 2413 | void anchor() override; |
| 2414 | |
| 2415 | protected: |
| 2416 | ObjCImplDecl(Kind DK, DeclContext *DC, |
| 2417 | ObjCInterfaceDecl *classInterface, |
| 2418 | IdentifierInfo *Id, |
| 2419 | SourceLocation nameLoc, SourceLocation atStartLoc) |
| 2420 | : ObjCContainerDecl(DK, DC, Id, nameLoc, atStartLoc), |
| 2421 | ClassInterface(classInterface) {} |
| 2422 | |
| 2423 | public: |
| 2424 | const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } |
| 2425 | ObjCInterfaceDecl *getClassInterface() { return ClassInterface; } |
| 2426 | void setClassInterface(ObjCInterfaceDecl *IFace); |
| 2427 | |
| 2428 | void addInstanceMethod(ObjCMethodDecl *method) { |
| 2429 | // FIXME: Context should be set correctly before we get here. |
| 2430 | method->setLexicalDeclContext(this); |
| 2431 | addDecl(method); |
| 2432 | } |
| 2433 | |
| 2434 | void addClassMethod(ObjCMethodDecl *method) { |
| 2435 | // FIXME: Context should be set correctly before we get here. |
| 2436 | method->setLexicalDeclContext(this); |
| 2437 | addDecl(method); |
| 2438 | } |
| 2439 | |
| 2440 | void addPropertyImplementation(ObjCPropertyImplDecl *property); |
| 2441 | |
| 2442 | ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId, |
| 2443 | ObjCPropertyQueryKind queryKind) const; |
| 2444 | ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const; |
| 2445 | |
| 2446 | // Iterator access to properties. |
| 2447 | using propimpl_iterator = specific_decl_iterator<ObjCPropertyImplDecl>; |
| 2448 | using propimpl_range = |
| 2449 | llvm::iterator_range<specific_decl_iterator<ObjCPropertyImplDecl>>; |
| 2450 | |
| 2451 | propimpl_range property_impls() const { |
| 2452 | return propimpl_range(propimpl_begin(), propimpl_end()); |
| 2453 | } |
| 2454 | |
| 2455 | propimpl_iterator propimpl_begin() const { |
| 2456 | return propimpl_iterator(decls_begin()); |
| 2457 | } |
| 2458 | |
| 2459 | propimpl_iterator propimpl_end() const { |
| 2460 | return propimpl_iterator(decls_end()); |
| 2461 | } |
| 2462 | |
| 2463 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 2464 | |
| 2465 | static bool classofKind(Kind K) { |
| 2466 | return K >= firstObjCImpl && K <= lastObjCImpl; |
| 2467 | } |
| 2468 | }; |
| 2469 | |
| 2470 | /// ObjCCategoryImplDecl - An object of this class encapsulates a category |
| 2471 | /// \@implementation declaration. If a category class has declaration of a |
| 2472 | /// property, its implementation must be specified in the category's |
| 2473 | /// \@implementation declaration. Example: |
| 2474 | /// \@interface I \@end |
| 2475 | /// \@interface I(CATEGORY) |
| 2476 | /// \@property int p1, d1; |
| 2477 | /// \@end |
| 2478 | /// \@implementation I(CATEGORY) |
| 2479 | /// \@dynamic p1,d1; |
| 2480 | /// \@end |
| 2481 | /// |
| 2482 | /// ObjCCategoryImplDecl |
| 2483 | class ObjCCategoryImplDecl : public ObjCImplDecl { |
| 2484 | // Category name location |
| 2485 | SourceLocation CategoryNameLoc; |
| 2486 | |
| 2487 | ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id, |
| 2488 | ObjCInterfaceDecl *classInterface, |
| 2489 | SourceLocation nameLoc, SourceLocation atStartLoc, |
| 2490 | SourceLocation CategoryNameLoc) |
| 2491 | : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, Id, |
| 2492 | nameLoc, atStartLoc), |
| 2493 | CategoryNameLoc(CategoryNameLoc) {} |
| 2494 | |
| 2495 | void anchor() override; |
| 2496 | |
| 2497 | public: |
| 2498 | friend class ASTDeclReader; |
| 2499 | friend class ASTDeclWriter; |
| 2500 | |
| 2501 | static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC, |
| 2502 | IdentifierInfo *Id, |
| 2503 | ObjCInterfaceDecl *classInterface, |
| 2504 | SourceLocation nameLoc, |
| 2505 | SourceLocation atStartLoc, |
| 2506 | SourceLocation CategoryNameLoc); |
| 2507 | static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
| 2508 | |
| 2509 | ObjCCategoryDecl *getCategoryDecl() const; |
| 2510 | |
| 2511 | SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; } |
| 2512 | |
| 2513 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 2514 | static bool classofKind(Kind K) { return K == ObjCCategoryImpl;} |
| 2515 | }; |
| 2516 | |
| 2517 | raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID); |
| 2518 | |
| 2519 | /// ObjCImplementationDecl - Represents a class definition - this is where |
| 2520 | /// method definitions are specified. For example: |
| 2521 | /// |
| 2522 | /// @code |
| 2523 | /// \@implementation MyClass |
| 2524 | /// - (void)myMethod { /* do something */ } |
| 2525 | /// \@end |
| 2526 | /// @endcode |
| 2527 | /// |
| 2528 | /// In a non-fragile runtime, instance variables can appear in the class |
| 2529 | /// interface, class extensions (nameless categories), and in the implementation |
| 2530 | /// itself, as well as being synthesized as backing storage for properties. |
| 2531 | /// |
| 2532 | /// In a fragile runtime, instance variables are specified in the class |
| 2533 | /// interface, \em not in the implementation. Nevertheless (for legacy reasons), |
| 2534 | /// we allow instance variables to be specified in the implementation. When |
| 2535 | /// specified, they need to be \em identical to the interface. |
| 2536 | class ObjCImplementationDecl : public ObjCImplDecl { |
| 2537 | /// Implementation Class's super class. |
| 2538 | ObjCInterfaceDecl *SuperClass; |
| 2539 | SourceLocation SuperLoc; |
| 2540 | |
| 2541 | /// \@implementation may have private ivars. |
| 2542 | SourceLocation IvarLBraceLoc; |
| 2543 | SourceLocation IvarRBraceLoc; |
| 2544 | |
| 2545 | /// Support for ivar initialization. |
| 2546 | /// The arguments used to initialize the ivars |
| 2547 | LazyCXXCtorInitializersPtr IvarInitializers; |
| 2548 | unsigned NumIvarInitializers = 0; |
| 2549 | |
| 2550 | /// Do the ivars of this class require initialization other than |
| 2551 | /// zero-initialization? |
| 2552 | bool HasNonZeroConstructors : 1; |
| 2553 | |
| 2554 | /// Do the ivars of this class require non-trivial destruction? |
| 2555 | bool HasDestructors : 1; |
| 2556 | |
| 2557 | ObjCImplementationDecl(DeclContext *DC, |
| 2558 | ObjCInterfaceDecl *classInterface, |
| 2559 | ObjCInterfaceDecl *superDecl, |
| 2560 | SourceLocation nameLoc, SourceLocation atStartLoc, |
| 2561 | SourceLocation superLoc = SourceLocation(), |
| 2562 | SourceLocation IvarLBraceLoc=SourceLocation(), |
| 2563 | SourceLocation IvarRBraceLoc=SourceLocation()) |
| 2564 | : ObjCImplDecl(ObjCImplementation, DC, classInterface, |
| 2565 | classInterface ? classInterface->getIdentifier() |
| 2566 | : nullptr, |
| 2567 | nameLoc, atStartLoc), |
| 2568 | SuperClass(superDecl), SuperLoc(superLoc), |
| 2569 | IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc), |
| 2570 | HasNonZeroConstructors(false), HasDestructors(false) {} |
| 2571 | |
| 2572 | void anchor() override; |
| 2573 | |
| 2574 | public: |
| 2575 | friend class ASTDeclReader; |
| 2576 | friend class ASTDeclWriter; |
| 2577 | |
| 2578 | static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC, |
| 2579 | ObjCInterfaceDecl *classInterface, |
| 2580 | ObjCInterfaceDecl *superDecl, |
| 2581 | SourceLocation nameLoc, |
| 2582 | SourceLocation atStartLoc, |
| 2583 | SourceLocation superLoc = SourceLocation(), |
| 2584 | SourceLocation IvarLBraceLoc=SourceLocation(), |
| 2585 | SourceLocation IvarRBraceLoc=SourceLocation()); |
| 2586 | |
| 2587 | static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
| 2588 | |
| 2589 | /// init_iterator - Iterates through the ivar initializer list. |
| 2590 | using init_iterator = CXXCtorInitializer **; |
| 2591 | |
| 2592 | /// init_const_iterator - Iterates through the ivar initializer list. |
| 2593 | using init_const_iterator = CXXCtorInitializer * const *; |
| 2594 | |
| 2595 | using init_range = llvm::iterator_range<init_iterator>; |
| 2596 | using init_const_range = llvm::iterator_range<init_const_iterator>; |
| 2597 | |
| 2598 | init_range inits() { return init_range(init_begin(), init_end()); } |
| 2599 | |
| 2600 | init_const_range inits() const { |
| 2601 | return init_const_range(init_begin(), init_end()); |
| 2602 | } |
| 2603 | |
| 2604 | /// init_begin() - Retrieve an iterator to the first initializer. |
| 2605 | init_iterator init_begin() { |
| 2606 | const auto *ConstThis = this; |
| 2607 | return const_cast<init_iterator>(ConstThis->init_begin()); |
| 2608 | } |
| 2609 | |
| 2610 | /// begin() - Retrieve an iterator to the first initializer. |
| 2611 | init_const_iterator init_begin() const; |
| 2612 | |
| 2613 | /// init_end() - Retrieve an iterator past the last initializer. |
| 2614 | init_iterator init_end() { |
| 2615 | return init_begin() + NumIvarInitializers; |
| 2616 | } |
| 2617 | |
| 2618 | /// end() - Retrieve an iterator past the last initializer. |
| 2619 | init_const_iterator init_end() const { |
| 2620 | return init_begin() + NumIvarInitializers; |
| 2621 | } |
| 2622 | |
| 2623 | /// getNumArgs - Number of ivars which must be initialized. |
| 2624 | unsigned getNumIvarInitializers() const { |
| 2625 | return NumIvarInitializers; |
| 2626 | } |
| 2627 | |
| 2628 | void setNumIvarInitializers(unsigned numNumIvarInitializers) { |
| 2629 | NumIvarInitializers = numNumIvarInitializers; |
| 2630 | } |
| 2631 | |
| 2632 | void setIvarInitializers(ASTContext &C, |
| 2633 | CXXCtorInitializer ** initializers, |
| 2634 | unsigned numInitializers); |
| 2635 | |
| 2636 | /// Do any of the ivars of this class (not counting its base classes) |
| 2637 | /// require construction other than zero-initialization? |
| 2638 | bool hasNonZeroConstructors() const { return HasNonZeroConstructors; } |
| 2639 | void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; } |
| 2640 | |
| 2641 | /// Do any of the ivars of this class (not counting its base classes) |
| 2642 | /// require non-trivial destruction? |
| 2643 | bool hasDestructors() const { return HasDestructors; } |
| 2644 | void setHasDestructors(bool val) { HasDestructors = val; } |
| 2645 | |
| 2646 | /// getIdentifier - Get the identifier that names the class |
| 2647 | /// interface associated with this implementation. |
| 2648 | IdentifierInfo *getIdentifier() const { |
| 2649 | return getClassInterface()->getIdentifier(); |
| 2650 | } |
| 2651 | |
| 2652 | /// getName - Get the name of identifier for the class interface associated |
| 2653 | /// with this implementation as a StringRef. |
| 2654 | // |
| 2655 | // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different |
| 2656 | // meaning. |
| 2657 | StringRef getName() const { |
| 2658 | assert(getIdentifier() && "Name is not a simple identifier")((void)0); |
| 2659 | return getIdentifier()->getName(); |
| 2660 | } |
| 2661 | |
| 2662 | /// Get the name of the class associated with this interface. |
| 2663 | // |
| 2664 | // FIXME: Move to StringRef API. |
| 2665 | std::string getNameAsString() const { return std::string(getName()); } |
| 2666 | |
| 2667 | /// Produce a name to be used for class's metadata. It comes either via |
| 2668 | /// class's objc_runtime_name attribute or class name. |
| 2669 | StringRef getObjCRuntimeNameAsString() const; |
| 2670 | |
| 2671 | const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; } |
| 2672 | ObjCInterfaceDecl *getSuperClass() { return SuperClass; } |
| 2673 | SourceLocation getSuperClassLoc() const { return SuperLoc; } |
| 2674 | |
| 2675 | void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; } |
| 2676 | |
| 2677 | void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; } |
| 2678 | SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; } |
| 2679 | void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; } |
| 2680 | SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; } |
| 2681 | |
| 2682 | using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>; |
| 2683 | using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>; |
| 2684 | |
| 2685 | ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); } |
| 2686 | |
| 2687 | ivar_iterator ivar_begin() const { |
| 2688 | return ivar_iterator(decls_begin()); |
| 2689 | } |
| 2690 | |
| 2691 | ivar_iterator ivar_end() const { |
| 2692 | return ivar_iterator(decls_end()); |
| 2693 | } |
| 2694 | |
| 2695 | unsigned ivar_size() const { |
| 2696 | return std::distance(ivar_begin(), ivar_end()); |
| 2697 | } |
| 2698 | |
| 2699 | bool ivar_empty() const { |
| 2700 | return ivar_begin() == ivar_end(); |
| 2701 | } |
| 2702 | |
| 2703 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 2704 | static bool classofKind(Kind K) { return K == ObjCImplementation; } |
| 2705 | }; |
| 2706 | |
| 2707 | raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID); |
| 2708 | |
| 2709 | /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is |
| 2710 | /// declared as \@compatibility_alias alias class. |
| 2711 | class ObjCCompatibleAliasDecl : public NamedDecl { |
| 2712 | /// Class that this is an alias of. |
| 2713 | ObjCInterfaceDecl *AliasedClass; |
| 2714 | |
| 2715 | ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id, |
| 2716 | ObjCInterfaceDecl* aliasedClass) |
| 2717 | : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {} |
| 2718 | |
| 2719 | void anchor() override; |
| 2720 | |
| 2721 | public: |
| 2722 | static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC, |
| 2723 | SourceLocation L, IdentifierInfo *Id, |
| 2724 | ObjCInterfaceDecl* aliasedClass); |
| 2725 | |
| 2726 | static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C, |
| 2727 | unsigned ID); |
| 2728 | |
| 2729 | const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; } |
| 2730 | ObjCInterfaceDecl *getClassInterface() { return AliasedClass; } |
| 2731 | void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; } |
| 2732 | |
| 2733 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 2734 | static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; } |
| 2735 | }; |
| 2736 | |
| 2737 | /// ObjCPropertyImplDecl - Represents implementation declaration of a property |
| 2738 | /// in a class or category implementation block. For example: |
| 2739 | /// \@synthesize prop1 = ivar1; |
| 2740 | /// |
| 2741 | class ObjCPropertyImplDecl : public Decl { |
| 2742 | public: |
| 2743 | enum Kind { |
| 2744 | Synthesize, |
| 2745 | Dynamic |
| 2746 | }; |
| 2747 | |
| 2748 | private: |
| 2749 | SourceLocation AtLoc; // location of \@synthesize or \@dynamic |
| 2750 | |
| 2751 | /// For \@synthesize, the location of the ivar, if it was written in |
| 2752 | /// the source code. |
| 2753 | /// |
| 2754 | /// \code |
| 2755 | /// \@synthesize int a = b |
| 2756 | /// \endcode |
| 2757 | SourceLocation IvarLoc; |
| 2758 | |
| 2759 | /// Property declaration being implemented |
| 2760 | ObjCPropertyDecl *PropertyDecl; |
| 2761 | |
| 2762 | /// Null for \@dynamic. Required for \@synthesize. |
| 2763 | ObjCIvarDecl *PropertyIvarDecl; |
| 2764 | |
| 2765 | /// The getter's definition, which has an empty body if synthesized. |
| 2766 | ObjCMethodDecl *GetterMethodDecl = nullptr; |
| 2767 | /// The getter's definition, which has an empty body if synthesized. |
| 2768 | ObjCMethodDecl *SetterMethodDecl = nullptr; |
| 2769 | |
| 2770 | /// Null for \@dynamic. Non-null if property must be copy-constructed in |
| 2771 | /// getter. |
| 2772 | Expr *GetterCXXConstructor = nullptr; |
| 2773 | |
| 2774 | /// Null for \@dynamic. Non-null if property has assignment operator to call |
| 2775 | /// in Setter synthesis. |
| 2776 | Expr *SetterCXXAssignment = nullptr; |
| 2777 | |
| 2778 | ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L, |
| 2779 | ObjCPropertyDecl *property, |
| 2780 | Kind PK, |
| 2781 | ObjCIvarDecl *ivarDecl, |
| 2782 | SourceLocation ivarLoc) |
| 2783 | : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc), |
| 2784 | IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl) { |
| 2785 | assert(PK == Dynamic || PropertyIvarDecl)((void)0); |
| 2786 | } |
| 2787 | |
| 2788 | public: |
| 2789 | friend class ASTDeclReader; |
| 2790 | |
| 2791 | static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC, |
| 2792 | SourceLocation atLoc, SourceLocation L, |
| 2793 | ObjCPropertyDecl *property, |
| 2794 | Kind PK, |
| 2795 | ObjCIvarDecl *ivarDecl, |
| 2796 | SourceLocation ivarLoc); |
| 2797 | |
| 2798 | static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
| 2799 | |
| 2800 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
| 2801 | |
| 2802 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return AtLoc; } |
| 2803 | void setAtLoc(SourceLocation Loc) { AtLoc = Loc; } |
| 2804 | |
| 2805 | ObjCPropertyDecl *getPropertyDecl() const { |
| 2806 | return PropertyDecl; |
| 2807 | } |
| 2808 | void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; } |
| 2809 | |
| 2810 | Kind getPropertyImplementation() const { |
| 2811 | return PropertyIvarDecl ? Synthesize : Dynamic; |
| 2812 | } |
| 2813 | |
| 2814 | ObjCIvarDecl *getPropertyIvarDecl() const { |
| 2815 | return PropertyIvarDecl; |
| 2816 | } |
| 2817 | SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; } |
| 2818 | |
| 2819 | void setPropertyIvarDecl(ObjCIvarDecl *Ivar, |
| 2820 | SourceLocation IvarLoc) { |
| 2821 | PropertyIvarDecl = Ivar; |
| 2822 | this->IvarLoc = IvarLoc; |
| 2823 | } |
| 2824 | |
| 2825 | /// For \@synthesize, returns true if an ivar name was explicitly |
| 2826 | /// specified. |
| 2827 | /// |
| 2828 | /// \code |
| 2829 | /// \@synthesize int a = b; // true |
| 2830 | /// \@synthesize int a; // false |
| 2831 | /// \endcode |
| 2832 | bool isIvarNameSpecified() const { |
| 2833 | return IvarLoc.isValid() && IvarLoc != getLocation(); |
| 2834 | } |
| 2835 | |
| 2836 | ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; } |
| 2837 | void setGetterMethodDecl(ObjCMethodDecl *MD) { GetterMethodDecl = MD; } |
| 2838 | |
| 2839 | ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; } |
| 2840 | void setSetterMethodDecl(ObjCMethodDecl *MD) { SetterMethodDecl = MD; } |
| 2841 | |
| 2842 | Expr *getGetterCXXConstructor() const { |
| 2843 | return GetterCXXConstructor; |
| 2844 | } |
| 2845 | |
| 2846 | void setGetterCXXConstructor(Expr *getterCXXConstructor) { |
| 2847 | GetterCXXConstructor = getterCXXConstructor; |
| 2848 | } |
| 2849 | |
| 2850 | Expr *getSetterCXXAssignment() const { |
| 2851 | return SetterCXXAssignment; |
| 2852 | } |
| 2853 | |
| 2854 | void setSetterCXXAssignment(Expr *setterCXXAssignment) { |
| 2855 | SetterCXXAssignment = setterCXXAssignment; |
| 2856 | } |
| 2857 | |
| 2858 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 2859 | static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; } |
| 2860 | }; |
| 2861 | |
| 2862 | template<bool (*Filter)(ObjCCategoryDecl *)> |
| 2863 | void |
| 2864 | ObjCInterfaceDecl::filtered_category_iterator<Filter>:: |
| 2865 | findAcceptableCategory() { |
| 2866 | while (Current && !Filter(Current)) |
| 2867 | Current = Current->getNextClassCategoryRaw(); |
| 2868 | } |
| 2869 | |
| 2870 | template<bool (*Filter)(ObjCCategoryDecl *)> |
| 2871 | inline ObjCInterfaceDecl::filtered_category_iterator<Filter> & |
| 2872 | ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() { |
| 2873 | Current = Current->getNextClassCategoryRaw(); |
| 2874 | findAcceptableCategory(); |
| 2875 | return *this; |
| 2876 | } |
| 2877 | |
| 2878 | inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) { |
| 2879 | return Cat->isUnconditionallyVisible(); |
| 2880 | } |
| 2881 | |
| 2882 | inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) { |
| 2883 | return Cat->IsClassExtension() && Cat->isUnconditionallyVisible(); |
| 2884 | } |
| 2885 | |
| 2886 | inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) { |
| 2887 | return Cat->IsClassExtension(); |
| 2888 | } |
| 2889 | |
| 2890 | } // namespace clang |
| 2891 | |
| 2892 | #endif // LLVM_CLANG_AST_DECLOBJC_H |
| 1 | //===- DeclBase.h - Base Classes for representing declarations --*- C++ -*-===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the Decl and DeclContext interfaces. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_CLANG_AST_DECLBASE_H |
| 14 | #define LLVM_CLANG_AST_DECLBASE_H |
| 15 | |
| 16 | #include "clang/AST/ASTDumperUtils.h" |
| 17 | #include "clang/AST/AttrIterator.h" |
| 18 | #include "clang/AST/DeclarationName.h" |
| 19 | #include "clang/Basic/IdentifierTable.h" |
| 20 | #include "clang/Basic/LLVM.h" |
| 21 | #include "clang/Basic/SourceLocation.h" |
| 22 | #include "clang/Basic/Specifiers.h" |
| 23 | #include "llvm/ADT/ArrayRef.h" |
| 24 | #include "llvm/ADT/PointerIntPair.h" |
| 25 | #include "llvm/ADT/PointerUnion.h" |
| 26 | #include "llvm/ADT/iterator.h" |
| 27 | #include "llvm/ADT/iterator_range.h" |
| 28 | #include "llvm/Support/Casting.h" |
| 29 | #include "llvm/Support/Compiler.h" |
| 30 | #include "llvm/Support/PrettyStackTrace.h" |
| 31 | #include "llvm/Support/VersionTuple.h" |
| 32 | #include <algorithm> |
| 33 | #include <cassert> |
| 34 | #include <cstddef> |
| 35 | #include <iterator> |
| 36 | #include <string> |
| 37 | #include <type_traits> |
| 38 | #include <utility> |
| 39 | |
| 40 | namespace clang { |
| 41 | |
| 42 | class ASTContext; |
| 43 | class ASTMutationListener; |
| 44 | class Attr; |
| 45 | class BlockDecl; |
| 46 | class DeclContext; |
| 47 | class ExternalSourceSymbolAttr; |
| 48 | class FunctionDecl; |
| 49 | class FunctionType; |
| 50 | class IdentifierInfo; |
| 51 | enum Linkage : unsigned char; |
| 52 | class LinkageSpecDecl; |
| 53 | class Module; |
| 54 | class NamedDecl; |
| 55 | class ObjCCategoryDecl; |
| 56 | class ObjCCategoryImplDecl; |
| 57 | class ObjCContainerDecl; |
| 58 | class ObjCImplDecl; |
| 59 | class ObjCImplementationDecl; |
| 60 | class ObjCInterfaceDecl; |
| 61 | class ObjCMethodDecl; |
| 62 | class ObjCProtocolDecl; |
| 63 | struct PrintingPolicy; |
| 64 | class RecordDecl; |
| 65 | class SourceManager; |
| 66 | class Stmt; |
| 67 | class StoredDeclsMap; |
| 68 | class TemplateDecl; |
| 69 | class TemplateParameterList; |
| 70 | class TranslationUnitDecl; |
| 71 | class UsingDirectiveDecl; |
| 72 | |
| 73 | /// Captures the result of checking the availability of a |
| 74 | /// declaration. |
| 75 | enum AvailabilityResult { |
| 76 | AR_Available = 0, |
| 77 | AR_NotYetIntroduced, |
| 78 | AR_Deprecated, |
| 79 | AR_Unavailable |
| 80 | }; |
| 81 | |
| 82 | /// Decl - This represents one declaration (or definition), e.g. a variable, |
| 83 | /// typedef, function, struct, etc. |
| 84 | /// |
| 85 | /// Note: There are objects tacked on before the *beginning* of Decl |
| 86 | /// (and its subclasses) in its Decl::operator new(). Proper alignment |
| 87 | /// of all subclasses (not requiring more than the alignment of Decl) is |
| 88 | /// asserted in DeclBase.cpp. |
| 89 | class alignas(8) Decl { |
| 90 | public: |
| 91 | /// Lists the kind of concrete classes of Decl. |
| 92 | enum Kind { |
| 93 | #define DECL(DERIVED, BASE) DERIVED, |
| 94 | #define ABSTRACT_DECL(DECL) |
| 95 | #define DECL_RANGE(BASE, START, END) \ |
| 96 | first##BASE = START, last##BASE = END, |
| 97 | #define LAST_DECL_RANGE(BASE, START, END) \ |
| 98 | first##BASE = START, last##BASE = END |
| 99 | #include "clang/AST/DeclNodes.inc" |
| 100 | }; |
| 101 | |
| 102 | /// A placeholder type used to construct an empty shell of a |
| 103 | /// decl-derived type that will be filled in later (e.g., by some |
| 104 | /// deserialization method). |
| 105 | struct EmptyShell {}; |
| 106 | |
| 107 | /// IdentifierNamespace - The different namespaces in which |
| 108 | /// declarations may appear. According to C99 6.2.3, there are |
| 109 | /// four namespaces, labels, tags, members and ordinary |
| 110 | /// identifiers. C++ describes lookup completely differently: |
| 111 | /// certain lookups merely "ignore" certain kinds of declarations, |
| 112 | /// usually based on whether the declaration is of a type, etc. |
| 113 | /// |
| 114 | /// These are meant as bitmasks, so that searches in |
| 115 | /// C++ can look into the "tag" namespace during ordinary lookup. |
| 116 | /// |
| 117 | /// Decl currently provides 15 bits of IDNS bits. |
| 118 | enum IdentifierNamespace { |
| 119 | /// Labels, declared with 'x:' and referenced with 'goto x'. |
| 120 | IDNS_Label = 0x0001, |
| 121 | |
| 122 | /// Tags, declared with 'struct foo;' and referenced with |
| 123 | /// 'struct foo'. All tags are also types. This is what |
| 124 | /// elaborated-type-specifiers look for in C. |
| 125 | /// This also contains names that conflict with tags in the |
| 126 | /// same scope but that are otherwise ordinary names (non-type |
| 127 | /// template parameters and indirect field declarations). |
| 128 | IDNS_Tag = 0x0002, |
| 129 | |
| 130 | /// Types, declared with 'struct foo', typedefs, etc. |
| 131 | /// This is what elaborated-type-specifiers look for in C++, |
| 132 | /// but note that it's ill-formed to find a non-tag. |
| 133 | IDNS_Type = 0x0004, |
| 134 | |
| 135 | /// Members, declared with object declarations within tag |
| 136 | /// definitions. In C, these can only be found by "qualified" |
| 137 | /// lookup in member expressions. In C++, they're found by |
| 138 | /// normal lookup. |
| 139 | IDNS_Member = 0x0008, |
| 140 | |
| 141 | /// Namespaces, declared with 'namespace foo {}'. |
| 142 | /// Lookup for nested-name-specifiers find these. |
| 143 | IDNS_Namespace = 0x0010, |
| 144 | |
| 145 | /// Ordinary names. In C, everything that's not a label, tag, |
| 146 | /// member, or function-local extern ends up here. |
| 147 | IDNS_Ordinary = 0x0020, |
| 148 | |
| 149 | /// Objective C \@protocol. |
| 150 | IDNS_ObjCProtocol = 0x0040, |
| 151 | |
| 152 | /// This declaration is a friend function. A friend function |
| 153 | /// declaration is always in this namespace but may also be in |
| 154 | /// IDNS_Ordinary if it was previously declared. |
| 155 | IDNS_OrdinaryFriend = 0x0080, |
| 156 | |
| 157 | /// This declaration is a friend class. A friend class |
| 158 | /// declaration is always in this namespace but may also be in |
| 159 | /// IDNS_Tag|IDNS_Type if it was previously declared. |
| 160 | IDNS_TagFriend = 0x0100, |
| 161 | |
| 162 | /// This declaration is a using declaration. A using declaration |
| 163 | /// *introduces* a number of other declarations into the current |
| 164 | /// scope, and those declarations use the IDNS of their targets, |
| 165 | /// but the actual using declarations go in this namespace. |
| 166 | IDNS_Using = 0x0200, |
| 167 | |
| 168 | /// This declaration is a C++ operator declared in a non-class |
| 169 | /// context. All such operators are also in IDNS_Ordinary. |
| 170 | /// C++ lexical operator lookup looks for these. |
| 171 | IDNS_NonMemberOperator = 0x0400, |
| 172 | |
| 173 | /// This declaration is a function-local extern declaration of a |
| 174 | /// variable or function. This may also be IDNS_Ordinary if it |
| 175 | /// has been declared outside any function. These act mostly like |
| 176 | /// invisible friend declarations, but are also visible to unqualified |
| 177 | /// lookup within the scope of the declaring function. |
| 178 | IDNS_LocalExtern = 0x0800, |
| 179 | |
| 180 | /// This declaration is an OpenMP user defined reduction construction. |
| 181 | IDNS_OMPReduction = 0x1000, |
| 182 | |
| 183 | /// This declaration is an OpenMP user defined mapper. |
| 184 | IDNS_OMPMapper = 0x2000, |
| 185 | }; |
| 186 | |
| 187 | /// ObjCDeclQualifier - 'Qualifiers' written next to the return and |
| 188 | /// parameter types in method declarations. Other than remembering |
| 189 | /// them and mangling them into the method's signature string, these |
| 190 | /// are ignored by the compiler; they are consumed by certain |
| 191 | /// remote-messaging frameworks. |
| 192 | /// |
| 193 | /// in, inout, and out are mutually exclusive and apply only to |
| 194 | /// method parameters. bycopy and byref are mutually exclusive and |
| 195 | /// apply only to method parameters (?). oneway applies only to |
| 196 | /// results. All of these expect their corresponding parameter to |
| 197 | /// have a particular type. None of this is currently enforced by |
| 198 | /// clang. |
| 199 | /// |
| 200 | /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier. |
| 201 | enum ObjCDeclQualifier { |
| 202 | OBJC_TQ_None = 0x0, |
| 203 | OBJC_TQ_In = 0x1, |
| 204 | OBJC_TQ_Inout = 0x2, |
| 205 | OBJC_TQ_Out = 0x4, |
| 206 | OBJC_TQ_Bycopy = 0x8, |
| 207 | OBJC_TQ_Byref = 0x10, |
| 208 | OBJC_TQ_Oneway = 0x20, |
| 209 | |
| 210 | /// The nullability qualifier is set when the nullability of the |
| 211 | /// result or parameter was expressed via a context-sensitive |
| 212 | /// keyword. |
| 213 | OBJC_TQ_CSNullability = 0x40 |
| 214 | }; |
| 215 | |
| 216 | /// The kind of ownership a declaration has, for visibility purposes. |
| 217 | /// This enumeration is designed such that higher values represent higher |
| 218 | /// levels of name hiding. |
| 219 | enum class ModuleOwnershipKind : unsigned { |
| 220 | /// This declaration is not owned by a module. |
| 221 | Unowned, |
| 222 | |
| 223 | /// This declaration has an owning module, but is globally visible |
| 224 | /// (typically because its owning module is visible and we know that |
| 225 | /// modules cannot later become hidden in this compilation). |
| 226 | /// After serialization and deserialization, this will be converted |
| 227 | /// to VisibleWhenImported. |
| 228 | Visible, |
| 229 | |
| 230 | /// This declaration has an owning module, and is visible when that |
| 231 | /// module is imported. |
| 232 | VisibleWhenImported, |
| 233 | |
| 234 | /// This declaration has an owning module, but is only visible to |
| 235 | /// lookups that occur within that module. |
| 236 | ModulePrivate |
| 237 | }; |
| 238 | |
| 239 | protected: |
| 240 | /// The next declaration within the same lexical |
| 241 | /// DeclContext. These pointers form the linked list that is |
| 242 | /// traversed via DeclContext's decls_begin()/decls_end(). |
| 243 | /// |
| 244 | /// The extra two bits are used for the ModuleOwnershipKind. |
| 245 | llvm::PointerIntPair<Decl *, 2, ModuleOwnershipKind> NextInContextAndBits; |
| 246 | |
| 247 | private: |
| 248 | friend class DeclContext; |
| 249 | |
| 250 | struct MultipleDC { |
| 251 | DeclContext *SemanticDC; |
| 252 | DeclContext *LexicalDC; |
| 253 | }; |
| 254 | |
| 255 | /// DeclCtx - Holds either a DeclContext* or a MultipleDC*. |
| 256 | /// For declarations that don't contain C++ scope specifiers, it contains |
| 257 | /// the DeclContext where the Decl was declared. |
| 258 | /// For declarations with C++ scope specifiers, it contains a MultipleDC* |
| 259 | /// with the context where it semantically belongs (SemanticDC) and the |
| 260 | /// context where it was lexically declared (LexicalDC). |
| 261 | /// e.g.: |
| 262 | /// |
| 263 | /// namespace A { |
| 264 | /// void f(); // SemanticDC == LexicalDC == 'namespace A' |
| 265 | /// } |
| 266 | /// void A::f(); // SemanticDC == namespace 'A' |
| 267 | /// // LexicalDC == global namespace |
| 268 | llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx; |
| 269 | |
| 270 | bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); } |
| 271 | bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); } |
| 272 | |
| 273 | MultipleDC *getMultipleDC() const { |
| 274 | return DeclCtx.get<MultipleDC*>(); |
| 275 | } |
| 276 | |
| 277 | DeclContext *getSemanticDC() const { |
| 278 | return DeclCtx.get<DeclContext*>(); |
| 279 | } |
| 280 | |
| 281 | /// Loc - The location of this decl. |
| 282 | SourceLocation Loc; |
| 283 | |
| 284 | /// DeclKind - This indicates which class this is. |
| 285 | unsigned DeclKind : 7; |
| 286 | |
| 287 | /// InvalidDecl - This indicates a semantic error occurred. |
| 288 | unsigned InvalidDecl : 1; |
| 289 | |
| 290 | /// HasAttrs - This indicates whether the decl has attributes or not. |
| 291 | unsigned HasAttrs : 1; |
| 292 | |
| 293 | /// Implicit - Whether this declaration was implicitly generated by |
| 294 | /// the implementation rather than explicitly written by the user. |
| 295 | unsigned Implicit : 1; |
| 296 | |
| 297 | /// Whether this declaration was "used", meaning that a definition is |
| 298 | /// required. |
| 299 | unsigned Used : 1; |
| 300 | |
| 301 | /// Whether this declaration was "referenced". |
| 302 | /// The difference with 'Used' is whether the reference appears in a |
| 303 | /// evaluated context or not, e.g. functions used in uninstantiated templates |
| 304 | /// are regarded as "referenced" but not "used". |
| 305 | unsigned Referenced : 1; |
| 306 | |
| 307 | /// Whether this declaration is a top-level declaration (function, |
| 308 | /// global variable, etc.) that is lexically inside an objc container |
| 309 | /// definition. |
| 310 | unsigned TopLevelDeclInObjCContainer : 1; |
| 311 | |
| 312 | /// Whether statistic collection is enabled. |
| 313 | static bool StatisticsEnabled; |
| 314 | |
| 315 | protected: |
| 316 | friend class ASTDeclReader; |
| 317 | friend class ASTDeclWriter; |
| 318 | friend class ASTNodeImporter; |
| 319 | friend class ASTReader; |
| 320 | friend class CXXClassMemberWrapper; |
| 321 | friend class LinkageComputer; |
| 322 | template<typename decl_type> friend class Redeclarable; |
| 323 | |
| 324 | /// Access - Used by C++ decls for the access specifier. |
| 325 | // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum |
| 326 | unsigned Access : 2; |
| 327 | |
| 328 | /// Whether this declaration was loaded from an AST file. |
| 329 | unsigned FromASTFile : 1; |
| 330 | |
| 331 | /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in. |
| 332 | unsigned IdentifierNamespace : 14; |
| 333 | |
| 334 | /// If 0, we have not computed the linkage of this declaration. |
| 335 | /// Otherwise, it is the linkage + 1. |
| 336 | mutable unsigned CacheValidAndLinkage : 3; |
| 337 | |
| 338 | /// Allocate memory for a deserialized declaration. |
| 339 | /// |
| 340 | /// This routine must be used to allocate memory for any declaration that is |
| 341 | /// deserialized from a module file. |
| 342 | /// |
| 343 | /// \param Size The size of the allocated object. |
| 344 | /// \param Ctx The context in which we will allocate memory. |
| 345 | /// \param ID The global ID of the deserialized declaration. |
| 346 | /// \param Extra The amount of extra space to allocate after the object. |
| 347 | void *operator new(std::size_t Size, const ASTContext &Ctx, unsigned ID, |
| 348 | std::size_t Extra = 0); |
| 349 | |
| 350 | /// Allocate memory for a non-deserialized declaration. |
| 351 | void *operator new(std::size_t Size, const ASTContext &Ctx, |
| 352 | DeclContext *Parent, std::size_t Extra = 0); |
| 353 | |
| 354 | private: |
| 355 | bool AccessDeclContextSanity() const; |
| 356 | |
| 357 | /// Get the module ownership kind to use for a local lexical child of \p DC, |
| 358 | /// which may be either a local or (rarely) an imported declaration. |
| 359 | static ModuleOwnershipKind getModuleOwnershipKindForChildOf(DeclContext *DC) { |
| 360 | if (DC) { |
| 361 | auto *D = cast<Decl>(DC); |
| 362 | auto MOK = D->getModuleOwnershipKind(); |
| 363 | if (MOK != ModuleOwnershipKind::Unowned && |
| 364 | (!D->isFromASTFile() || D->hasLocalOwningModuleStorage())) |
| 365 | return MOK; |
| 366 | // If D is not local and we have no local module storage, then we don't |
| 367 | // need to track module ownership at all. |
| 368 | } |
| 369 | return ModuleOwnershipKind::Unowned; |
| 370 | } |
| 371 | |
| 372 | public: |
| 373 | Decl() = delete; |
| 374 | Decl(const Decl&) = delete; |
| 375 | Decl(Decl &&) = delete; |
| 376 | Decl &operator=(const Decl&) = delete; |
| 377 | Decl &operator=(Decl&&) = delete; |
| 378 | |
| 379 | protected: |
| 380 | Decl(Kind DK, DeclContext *DC, SourceLocation L) |
| 381 | : NextInContextAndBits(nullptr, getModuleOwnershipKindForChildOf(DC)), |
| 382 | DeclCtx(DC), Loc(L), DeclKind(DK), InvalidDecl(false), HasAttrs(false), |
| 383 | Implicit(false), Used(false), Referenced(false), |
| 384 | TopLevelDeclInObjCContainer(false), Access(AS_none), FromASTFile(0), |
| 385 | IdentifierNamespace(getIdentifierNamespaceForKind(DK)), |
| 386 | CacheValidAndLinkage(0) { |
| 387 | if (StatisticsEnabled) add(DK); |
| 388 | } |
| 389 | |
| 390 | Decl(Kind DK, EmptyShell Empty) |
| 391 | : DeclKind(DK), InvalidDecl(false), HasAttrs(false), Implicit(false), |
| 392 | Used(false), Referenced(false), TopLevelDeclInObjCContainer(false), |
| 393 | Access(AS_none), FromASTFile(0), |
| 394 | IdentifierNamespace(getIdentifierNamespaceForKind(DK)), |
| 395 | CacheValidAndLinkage(0) { |
| 396 | if (StatisticsEnabled) add(DK); |
| 397 | } |
| 398 | |
| 399 | virtual ~Decl(); |
| 400 | |
| 401 | /// Update a potentially out-of-date declaration. |
| 402 | void updateOutOfDate(IdentifierInfo &II) const; |
| 403 | |
| 404 | Linkage getCachedLinkage() const { |
| 405 | return Linkage(CacheValidAndLinkage - 1); |
| 406 | } |
| 407 | |
| 408 | void setCachedLinkage(Linkage L) const { |
| 409 | CacheValidAndLinkage = L + 1; |
| 410 | } |
| 411 | |
| 412 | bool hasCachedLinkage() const { |
| 413 | return CacheValidAndLinkage; |
| 414 | } |
| 415 | |
| 416 | public: |
| 417 | /// Source range that this declaration covers. |
| 418 | virtual SourceRange getSourceRange() const LLVM_READONLY__attribute__((__pure__)) { |
| 419 | return SourceRange(getLocation(), getLocation()); |
| 420 | } |
| 421 | |
| 422 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { |
| 423 | return getSourceRange().getBegin(); |
| 424 | } |
| 425 | |
| 426 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { |
| 427 | return getSourceRange().getEnd(); |
| 428 | } |
| 429 | |
| 430 | SourceLocation getLocation() const { return Loc; } |
| 431 | void setLocation(SourceLocation L) { Loc = L; } |
| 432 | |
| 433 | Kind getKind() const { return static_cast<Kind>(DeclKind); } |
| 434 | const char *getDeclKindName() const; |
| 435 | |
| 436 | Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); } |
| 437 | const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();} |
| 438 | |
| 439 | DeclContext *getDeclContext() { |
| 440 | if (isInSemaDC()) |
| 441 | return getSemanticDC(); |
| 442 | return getMultipleDC()->SemanticDC; |
| 443 | } |
| 444 | const DeclContext *getDeclContext() const { |
| 445 | return const_cast<Decl*>(this)->getDeclContext(); |
| 446 | } |
| 447 | |
| 448 | /// Find the innermost non-closure ancestor of this declaration, |
| 449 | /// walking up through blocks, lambdas, etc. If that ancestor is |
| 450 | /// not a code context (!isFunctionOrMethod()), returns null. |
| 451 | /// |
| 452 | /// A declaration may be its own non-closure context. |
| 453 | Decl *getNonClosureContext(); |
| 454 | const Decl *getNonClosureContext() const { |
| 455 | return const_cast<Decl*>(this)->getNonClosureContext(); |
| 456 | } |
| 457 | |
| 458 | TranslationUnitDecl *getTranslationUnitDecl(); |
| 459 | const TranslationUnitDecl *getTranslationUnitDecl() const { |
| 460 | return const_cast<Decl*>(this)->getTranslationUnitDecl(); |
| 461 | } |
| 462 | |
| 463 | bool isInAnonymousNamespace() const; |
| 464 | |
| 465 | bool isInStdNamespace() const; |
| 466 | |
| 467 | ASTContext &getASTContext() const LLVM_READONLY__attribute__((__pure__)); |
| 468 | |
| 469 | /// Helper to get the language options from the ASTContext. |
| 470 | /// Defined out of line to avoid depending on ASTContext.h. |
| 471 | const LangOptions &getLangOpts() const LLVM_READONLY__attribute__((__pure__)); |
| 472 | |
| 473 | void setAccess(AccessSpecifier AS) { |
| 474 | Access = AS; |
| 475 | assert(AccessDeclContextSanity())((void)0); |
| 476 | } |
| 477 | |
| 478 | AccessSpecifier getAccess() const { |
| 479 | assert(AccessDeclContextSanity())((void)0); |
| 480 | return AccessSpecifier(Access); |
| 481 | } |
| 482 | |
| 483 | /// Retrieve the access specifier for this declaration, even though |
| 484 | /// it may not yet have been properly set. |
| 485 | AccessSpecifier getAccessUnsafe() const { |
| 486 | return AccessSpecifier(Access); |
| 487 | } |
| 488 | |
| 489 | bool hasAttrs() const { return HasAttrs; } |
| 490 | |
| 491 | void setAttrs(const AttrVec& Attrs) { |
| 492 | return setAttrsImpl(Attrs, getASTContext()); |
| 493 | } |
| 494 | |
| 495 | AttrVec &getAttrs() { |
| 496 | return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs()); |
| 497 | } |
| 498 | |
| 499 | const AttrVec &getAttrs() const; |
| 500 | void dropAttrs(); |
| 501 | void addAttr(Attr *A); |
| 502 | |
| 503 | using attr_iterator = AttrVec::const_iterator; |
| 504 | using attr_range = llvm::iterator_range<attr_iterator>; |
| 505 | |
| 506 | attr_range attrs() const { |
| 507 | return attr_range(attr_begin(), attr_end()); |
| 508 | } |
| 509 | |
| 510 | attr_iterator attr_begin() const { |
| 511 | return hasAttrs() ? getAttrs().begin() : nullptr; |
| 512 | } |
| 513 | attr_iterator attr_end() const { |
| 514 | return hasAttrs() ? getAttrs().end() : nullptr; |
| 515 | } |
| 516 | |
| 517 | template <typename T> |
| 518 | void dropAttr() { |
| 519 | if (!HasAttrs) return; |
| 520 | |
| 521 | AttrVec &Vec = getAttrs(); |
| 522 | llvm::erase_if(Vec, [](Attr *A) { return isa<T>(A); }); |
| 523 | |
| 524 | if (Vec.empty()) |
| 525 | HasAttrs = false; |
| 526 | } |
| 527 | |
| 528 | template <typename T> |
| 529 | llvm::iterator_range<specific_attr_iterator<T>> specific_attrs() const { |
| 530 | return llvm::make_range(specific_attr_begin<T>(), specific_attr_end<T>()); |
| 531 | } |
| 532 | |
| 533 | template <typename T> |
| 534 | specific_attr_iterator<T> specific_attr_begin() const { |
| 535 | return specific_attr_iterator<T>(attr_begin()); |
| 536 | } |
| 537 | |
| 538 | template <typename T> |
| 539 | specific_attr_iterator<T> specific_attr_end() const { |
| 540 | return specific_attr_iterator<T>(attr_end()); |
| 541 | } |
| 542 | |
| 543 | template<typename T> T *getAttr() const { |
| 544 | return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : nullptr; |
| 545 | } |
| 546 | |
| 547 | template<typename T> bool hasAttr() const { |
| 548 | return hasAttrs() && hasSpecificAttr<T>(getAttrs()); |
| 549 | } |
| 550 | |
| 551 | /// getMaxAlignment - return the maximum alignment specified by attributes |
| 552 | /// on this decl, 0 if there are none. |
| 553 | unsigned getMaxAlignment() const; |
| 554 | |
| 555 | /// setInvalidDecl - Indicates the Decl had a semantic error. This |
| 556 | /// allows for graceful error recovery. |
| 557 | void setInvalidDecl(bool Invalid = true); |
| 558 | bool isInvalidDecl() const { return (bool) InvalidDecl; } |
| 559 | |
| 560 | /// isImplicit - Indicates whether the declaration was implicitly |
| 561 | /// generated by the implementation. If false, this declaration |
| 562 | /// was written explicitly in the source code. |
| 563 | bool isImplicit() const { return Implicit; } |
| 564 | void setImplicit(bool I = true) { Implicit = I; } |
| 565 | |
| 566 | /// Whether *any* (re-)declaration of the entity was used, meaning that |
| 567 | /// a definition is required. |
| 568 | /// |
| 569 | /// \param CheckUsedAttr When true, also consider the "used" attribute |
| 570 | /// (in addition to the "used" bit set by \c setUsed()) when determining |
| 571 | /// whether the function is used. |
| 572 | bool isUsed(bool CheckUsedAttr = true) const; |
| 573 | |
| 574 | /// Set whether the declaration is used, in the sense of odr-use. |
| 575 | /// |
| 576 | /// This should only be used immediately after creating a declaration. |
| 577 | /// It intentionally doesn't notify any listeners. |
| 578 | void setIsUsed() { getCanonicalDecl()->Used = true; } |
| 579 | |
| 580 | /// Mark the declaration used, in the sense of odr-use. |
| 581 | /// |
| 582 | /// This notifies any mutation listeners in addition to setting a bit |
| 583 | /// indicating the declaration is used. |
| 584 | void markUsed(ASTContext &C); |
| 585 | |
| 586 | /// Whether any declaration of this entity was referenced. |
| 587 | bool isReferenced() const; |
| 588 | |
| 589 | /// Whether this declaration was referenced. This should not be relied |
| 590 | /// upon for anything other than debugging. |
| 591 | bool isThisDeclarationReferenced() const { return Referenced; } |
| 592 | |
| 593 | void setReferenced(bool R = true) { Referenced = R; } |
| 594 | |
| 595 | /// Whether this declaration is a top-level declaration (function, |
| 596 | /// global variable, etc.) that is lexically inside an objc container |
| 597 | /// definition. |
| 598 | bool isTopLevelDeclInObjCContainer() const { |
| 599 | return TopLevelDeclInObjCContainer; |
| 600 | } |
| 601 | |
| 602 | void setTopLevelDeclInObjCContainer(bool V = true) { |
| 603 | TopLevelDeclInObjCContainer = V; |
| 604 | } |
| 605 | |
| 606 | /// Looks on this and related declarations for an applicable |
| 607 | /// external source symbol attribute. |
| 608 | ExternalSourceSymbolAttr *getExternalSourceSymbolAttr() const; |
| 609 | |
| 610 | /// Whether this declaration was marked as being private to the |
| 611 | /// module in which it was defined. |
| 612 | bool isModulePrivate() const { |
| 613 | return getModuleOwnershipKind() == ModuleOwnershipKind::ModulePrivate; |
| 614 | } |
| 615 | |
| 616 | /// Return true if this declaration has an attribute which acts as |
| 617 | /// definition of the entity, such as 'alias' or 'ifunc'. |
| 618 | bool hasDefiningAttr() const; |
| 619 | |
| 620 | /// Return this declaration's defining attribute if it has one. |
| 621 | const Attr *getDefiningAttr() const; |
| 622 | |
| 623 | protected: |
| 624 | /// Specify that this declaration was marked as being private |
| 625 | /// to the module in which it was defined. |
| 626 | void setModulePrivate() { |
| 627 | // The module-private specifier has no effect on unowned declarations. |
| 628 | // FIXME: We should track this in some way for source fidelity. |
| 629 | if (getModuleOwnershipKind() == ModuleOwnershipKind::Unowned) |
| 630 | return; |
| 631 | setModuleOwnershipKind(ModuleOwnershipKind::ModulePrivate); |
| 632 | } |
| 633 | |
| 634 | public: |
| 635 | /// Set the FromASTFile flag. This indicates that this declaration |
| 636 | /// was deserialized and not parsed from source code and enables |
| 637 | /// features such as module ownership information. |
| 638 | void setFromASTFile() { |
| 639 | FromASTFile = true; |
| 640 | } |
| 641 | |
| 642 | /// Set the owning module ID. This may only be called for |
| 643 | /// deserialized Decls. |
| 644 | void setOwningModuleID(unsigned ID) { |
| 645 | assert(isFromASTFile() && "Only works on a deserialized declaration")((void)0); |
| 646 | *((unsigned*)this - 2) = ID; |
| 647 | } |
| 648 | |
| 649 | public: |
| 650 | /// Determine the availability of the given declaration. |
| 651 | /// |
| 652 | /// This routine will determine the most restrictive availability of |
| 653 | /// the given declaration (e.g., preferring 'unavailable' to |
| 654 | /// 'deprecated'). |
| 655 | /// |
| 656 | /// \param Message If non-NULL and the result is not \c |
| 657 | /// AR_Available, will be set to a (possibly empty) message |
| 658 | /// describing why the declaration has not been introduced, is |
| 659 | /// deprecated, or is unavailable. |
| 660 | /// |
| 661 | /// \param EnclosingVersion The version to compare with. If empty, assume the |
| 662 | /// deployment target version. |
| 663 | /// |
| 664 | /// \param RealizedPlatform If non-NULL and the availability result is found |
| 665 | /// in an available attribute it will set to the platform which is written in |
| 666 | /// the available attribute. |
| 667 | AvailabilityResult |
| 668 | getAvailability(std::string *Message = nullptr, |
| 669 | VersionTuple EnclosingVersion = VersionTuple(), |
| 670 | StringRef *RealizedPlatform = nullptr) const; |
| 671 | |
| 672 | /// Retrieve the version of the target platform in which this |
| 673 | /// declaration was introduced. |
| 674 | /// |
| 675 | /// \returns An empty version tuple if this declaration has no 'introduced' |
| 676 | /// availability attributes, or the version tuple that's specified in the |
| 677 | /// attribute otherwise. |
| 678 | VersionTuple getVersionIntroduced() const; |
| 679 | |
| 680 | /// Determine whether this declaration is marked 'deprecated'. |
| 681 | /// |
| 682 | /// \param Message If non-NULL and the declaration is deprecated, |
| 683 | /// this will be set to the message describing why the declaration |
| 684 | /// was deprecated (which may be empty). |
| 685 | bool isDeprecated(std::string *Message = nullptr) const { |
| 686 | return getAvailability(Message) == AR_Deprecated; |
| 687 | } |
| 688 | |
| 689 | /// Determine whether this declaration is marked 'unavailable'. |
| 690 | /// |
| 691 | /// \param Message If non-NULL and the declaration is unavailable, |
| 692 | /// this will be set to the message describing why the declaration |
| 693 | /// was made unavailable (which may be empty). |
| 694 | bool isUnavailable(std::string *Message = nullptr) const { |
| 695 | return getAvailability(Message) == AR_Unavailable; |
| 696 | } |
| 697 | |
| 698 | /// Determine whether this is a weak-imported symbol. |
| 699 | /// |
| 700 | /// Weak-imported symbols are typically marked with the |
| 701 | /// 'weak_import' attribute, but may also be marked with an |
| 702 | /// 'availability' attribute where we're targing a platform prior to |
| 703 | /// the introduction of this feature. |
| 704 | bool isWeakImported() const; |
| 705 | |
| 706 | /// Determines whether this symbol can be weak-imported, |
| 707 | /// e.g., whether it would be well-formed to add the weak_import |
| 708 | /// attribute. |
| 709 | /// |
| 710 | /// \param IsDefinition Set to \c true to indicate that this |
| 711 | /// declaration cannot be weak-imported because it has a definition. |
| 712 | bool canBeWeakImported(bool &IsDefinition) const; |
| 713 | |
| 714 | /// Determine whether this declaration came from an AST file (such as |
| 715 | /// a precompiled header or module) rather than having been parsed. |
| 716 | bool isFromASTFile() const { return FromASTFile; } |
| 717 | |
| 718 | /// Retrieve the global declaration ID associated with this |
| 719 | /// declaration, which specifies where this Decl was loaded from. |
| 720 | unsigned getGlobalID() const { |
| 721 | if (isFromASTFile()) |
| 722 | return *((const unsigned*)this - 1); |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | /// Retrieve the global ID of the module that owns this particular |
| 727 | /// declaration. |
| 728 | unsigned getOwningModuleID() const { |
| 729 | if (isFromASTFile()) |
| 730 | return *((const unsigned*)this - 2); |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | private: |
| 735 | Module *getOwningModuleSlow() const; |
| 736 | |
| 737 | protected: |
| 738 | bool hasLocalOwningModuleStorage() const; |
| 739 | |
| 740 | public: |
| 741 | /// Get the imported owning module, if this decl is from an imported |
| 742 | /// (non-local) module. |
| 743 | Module *getImportedOwningModule() const { |
| 744 | if (!isFromASTFile() || !hasOwningModule()) |
| 745 | return nullptr; |
| 746 | |
| 747 | return getOwningModuleSlow(); |
| 748 | } |
| 749 | |
| 750 | /// Get the local owning module, if known. Returns nullptr if owner is |
| 751 | /// not yet known or declaration is not from a module. |
| 752 | Module *getLocalOwningModule() const { |
| 753 | if (isFromASTFile() || !hasOwningModule()) |
| 754 | return nullptr; |
| 755 | |
| 756 | assert(hasLocalOwningModuleStorage() &&((void)0) |
| 757 | "owned local decl but no local module storage")((void)0); |
| 758 | return reinterpret_cast<Module *const *>(this)[-1]; |
| 759 | } |
| 760 | void setLocalOwningModule(Module *M) { |
| 761 | assert(!isFromASTFile() && hasOwningModule() &&((void)0) |
| 762 | hasLocalOwningModuleStorage() &&((void)0) |
| 763 | "should not have a cached owning module")((void)0); |
| 764 | reinterpret_cast<Module **>(this)[-1] = M; |
| 765 | } |
| 766 | |
| 767 | /// Is this declaration owned by some module? |
| 768 | bool hasOwningModule() const { |
| 769 | return getModuleOwnershipKind() != ModuleOwnershipKind::Unowned; |
| 770 | } |
| 771 | |
| 772 | /// Get the module that owns this declaration (for visibility purposes). |
| 773 | Module *getOwningModule() const { |
| 774 | return isFromASTFile() ? getImportedOwningModule() : getLocalOwningModule(); |
| 775 | } |
| 776 | |
| 777 | /// Get the module that owns this declaration for linkage purposes. |
| 778 | /// There only ever is such a module under the C++ Modules TS. |
| 779 | /// |
| 780 | /// \param IgnoreLinkage Ignore the linkage of the entity; assume that |
| 781 | /// all declarations in a global module fragment are unowned. |
| 782 | Module *getOwningModuleForLinkage(bool IgnoreLinkage = false) const; |
| 783 | |
| 784 | /// Determine whether this declaration is definitely visible to name lookup, |
| 785 | /// independent of whether the owning module is visible. |
| 786 | /// Note: The declaration may be visible even if this returns \c false if the |
| 787 | /// owning module is visible within the query context. This is a low-level |
| 788 | /// helper function; most code should be calling Sema::isVisible() instead. |
| 789 | bool isUnconditionallyVisible() const { |
| 790 | return (int)getModuleOwnershipKind() <= (int)ModuleOwnershipKind::Visible; |
| 791 | } |
| 792 | |
| 793 | /// Set that this declaration is globally visible, even if it came from a |
| 794 | /// module that is not visible. |
| 795 | void setVisibleDespiteOwningModule() { |
| 796 | if (!isUnconditionallyVisible()) |
| 797 | setModuleOwnershipKind(ModuleOwnershipKind::Visible); |
| 798 | } |
| 799 | |
| 800 | /// Get the kind of module ownership for this declaration. |
| 801 | ModuleOwnershipKind getModuleOwnershipKind() const { |
| 802 | return NextInContextAndBits.getInt(); |
| 803 | } |
| 804 | |
| 805 | /// Set whether this declaration is hidden from name lookup. |
| 806 | void setModuleOwnershipKind(ModuleOwnershipKind MOK) { |
| 807 | assert(!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned &&((void)0) |
| 808 | MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() &&((void)0) |
| 809 | !hasLocalOwningModuleStorage()) &&((void)0) |
| 810 | "no storage available for owning module for this declaration")((void)0); |
| 811 | NextInContextAndBits.setInt(MOK); |
| 812 | } |
| 813 | |
| 814 | unsigned getIdentifierNamespace() const { |
| 815 | return IdentifierNamespace; |
| 816 | } |
| 817 | |
| 818 | bool isInIdentifierNamespace(unsigned NS) const { |
| 819 | return getIdentifierNamespace() & NS; |
| 820 | } |
| 821 | |
| 822 | static unsigned getIdentifierNamespaceForKind(Kind DK); |
| 823 | |
| 824 | bool hasTagIdentifierNamespace() const { |
| 825 | return isTagIdentifierNamespace(getIdentifierNamespace()); |
| 826 | } |
| 827 | |
| 828 | static bool isTagIdentifierNamespace(unsigned NS) { |
| 829 | // TagDecls have Tag and Type set and may also have TagFriend. |
| 830 | return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type); |
| 831 | } |
| 832 | |
| 833 | /// getLexicalDeclContext - The declaration context where this Decl was |
| 834 | /// lexically declared (LexicalDC). May be different from |
| 835 | /// getDeclContext() (SemanticDC). |
| 836 | /// e.g.: |
| 837 | /// |
| 838 | /// namespace A { |
| 839 | /// void f(); // SemanticDC == LexicalDC == 'namespace A' |
| 840 | /// } |
| 841 | /// void A::f(); // SemanticDC == namespace 'A' |
| 842 | /// // LexicalDC == global namespace |
| 843 | DeclContext *getLexicalDeclContext() { |
| 844 | if (isInSemaDC()) |
| 845 | return getSemanticDC(); |
| 846 | return getMultipleDC()->LexicalDC; |
| 847 | } |
| 848 | const DeclContext *getLexicalDeclContext() const { |
| 849 | return const_cast<Decl*>(this)->getLexicalDeclContext(); |
| 850 | } |
| 851 | |
| 852 | /// Determine whether this declaration is declared out of line (outside its |
| 853 | /// semantic context). |
| 854 | virtual bool isOutOfLine() const; |
| 855 | |
| 856 | /// setDeclContext - Set both the semantic and lexical DeclContext |
| 857 | /// to DC. |
| 858 | void setDeclContext(DeclContext *DC); |
| 859 | |
| 860 | void setLexicalDeclContext(DeclContext *DC); |
| 861 | |
| 862 | /// Determine whether this declaration is a templated entity (whether it is |
| 863 | // within the scope of a template parameter). |
| 864 | bool isTemplated() const; |
| 865 | |
| 866 | /// Determine the number of levels of template parameter surrounding this |
| 867 | /// declaration. |
| 868 | unsigned getTemplateDepth() const; |
| 869 | |
| 870 | /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this |
| 871 | /// scoped decl is defined outside the current function or method. This is |
| 872 | /// roughly global variables and functions, but also handles enums (which |
| 873 | /// could be defined inside or outside a function etc). |
| 874 | bool isDefinedOutsideFunctionOrMethod() const { |
| 875 | return getParentFunctionOrMethod() == nullptr; |
| 876 | } |
| 877 | |
| 878 | /// Determine whether a substitution into this declaration would occur as |
| 879 | /// part of a substitution into a dependent local scope. Such a substitution |
| 880 | /// transitively substitutes into all constructs nested within this |
| 881 | /// declaration. |
| 882 | /// |
| 883 | /// This recognizes non-defining declarations as well as members of local |
| 884 | /// classes and lambdas: |
| 885 | /// \code |
| 886 | /// template<typename T> void foo() { void bar(); } |
| 887 | /// template<typename T> void foo2() { class ABC { void bar(); }; } |
| 888 | /// template<typename T> inline int x = [](){ return 0; }(); |
| 889 | /// \endcode |
| 890 | bool isInLocalScopeForInstantiation() const; |
| 891 | |
| 892 | /// If this decl is defined inside a function/method/block it returns |
| 893 | /// the corresponding DeclContext, otherwise it returns null. |
| 894 | const DeclContext *getParentFunctionOrMethod() const; |
| 895 | DeclContext *getParentFunctionOrMethod() { |
| 896 | return const_cast<DeclContext*>( |
| 897 | const_cast<const Decl*>(this)->getParentFunctionOrMethod()); |
| 898 | } |
| 899 | |
| 900 | /// Retrieves the "canonical" declaration of the given declaration. |
| 901 | virtual Decl *getCanonicalDecl() { return this; } |
| 902 | const Decl *getCanonicalDecl() const { |
| 903 | return const_cast<Decl*>(this)->getCanonicalDecl(); |
| 904 | } |
| 905 | |
| 906 | /// Whether this particular Decl is a canonical one. |
| 907 | bool isCanonicalDecl() const { return getCanonicalDecl() == this; } |
| 908 | |
| 909 | protected: |
| 910 | /// Returns the next redeclaration or itself if this is the only decl. |
| 911 | /// |
| 912 | /// Decl subclasses that can be redeclared should override this method so that |
| 913 | /// Decl::redecl_iterator can iterate over them. |
| 914 | virtual Decl *getNextRedeclarationImpl() { return this; } |
| 915 | |
| 916 | /// Implementation of getPreviousDecl(), to be overridden by any |
| 917 | /// subclass that has a redeclaration chain. |
| 918 | virtual Decl *getPreviousDeclImpl() { return nullptr; } |
| 919 | |
| 920 | /// Implementation of getMostRecentDecl(), to be overridden by any |
| 921 | /// subclass that has a redeclaration chain. |
| 922 | virtual Decl *getMostRecentDeclImpl() { return this; } |
| 923 | |
| 924 | public: |
| 925 | /// Iterates through all the redeclarations of the same decl. |
| 926 | class redecl_iterator { |
| 927 | /// Current - The current declaration. |
| 928 | Decl *Current = nullptr; |
| 929 | Decl *Starter; |
| 930 | |
| 931 | public: |
| 932 | using value_type = Decl *; |
| 933 | using reference = const value_type &; |
| 934 | using pointer = const value_type *; |
| 935 | using iterator_category = std::forward_iterator_tag; |
| 936 | using difference_type = std::ptrdiff_t; |
| 937 | |
| 938 | redecl_iterator() = default; |
| 939 | explicit redecl_iterator(Decl *C) : Current(C), Starter(C) {} |
| 940 | |
| 941 | reference operator*() const { return Current; } |
| 942 | value_type operator->() const { return Current; } |
| 943 | |
| 944 | redecl_iterator& operator++() { |
| 945 | assert(Current && "Advancing while iterator has reached end")((void)0); |
| 946 | // Get either previous decl or latest decl. |
| 947 | Decl *Next = Current->getNextRedeclarationImpl(); |
| 948 | assert(Next && "Should return next redeclaration or itself, never null!")((void)0); |
| 949 | Current = (Next != Starter) ? Next : nullptr; |
| 950 | return *this; |
| 951 | } |
| 952 | |
| 953 | redecl_iterator operator++(int) { |
| 954 | redecl_iterator tmp(*this); |
| 955 | ++(*this); |
| 956 | return tmp; |
| 957 | } |
| 958 | |
| 959 | friend bool operator==(redecl_iterator x, redecl_iterator y) { |
| 960 | return x.Current == y.Current; |
| 961 | } |
| 962 | |
| 963 | friend bool operator!=(redecl_iterator x, redecl_iterator y) { |
| 964 | return x.Current != y.Current; |
| 965 | } |
| 966 | }; |
| 967 | |
| 968 | using redecl_range = llvm::iterator_range<redecl_iterator>; |
| 969 | |
| 970 | /// Returns an iterator range for all the redeclarations of the same |
| 971 | /// decl. It will iterate at least once (when this decl is the only one). |
| 972 | redecl_range redecls() const { |
| 973 | return redecl_range(redecls_begin(), redecls_end()); |
| 974 | } |
| 975 | |
| 976 | redecl_iterator redecls_begin() const { |
| 977 | return redecl_iterator(const_cast<Decl *>(this)); |
| 978 | } |
| 979 | |
| 980 | redecl_iterator redecls_end() const { return redecl_iterator(); } |
| 981 | |
| 982 | /// Retrieve the previous declaration that declares the same entity |
| 983 | /// as this declaration, or NULL if there is no previous declaration. |
| 984 | Decl *getPreviousDecl() { return getPreviousDeclImpl(); } |
| 985 | |
| 986 | /// Retrieve the previous declaration that declares the same entity |
| 987 | /// as this declaration, or NULL if there is no previous declaration. |
| 988 | const Decl *getPreviousDecl() const { |
| 989 | return const_cast<Decl *>(this)->getPreviousDeclImpl(); |
| 990 | } |
| 991 | |
| 992 | /// True if this is the first declaration in its redeclaration chain. |
| 993 | bool isFirstDecl() const { |
| 994 | return getPreviousDecl() == nullptr; |
| 995 | } |
| 996 | |
| 997 | /// Retrieve the most recent declaration that declares the same entity |
| 998 | /// as this declaration (which may be this declaration). |
| 999 | Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); } |
| 1000 | |
| 1001 | /// Retrieve the most recent declaration that declares the same entity |
| 1002 | /// as this declaration (which may be this declaration). |
| 1003 | const Decl *getMostRecentDecl() const { |
| 1004 | return const_cast<Decl *>(this)->getMostRecentDeclImpl(); |
| 1005 | } |
| 1006 | |
| 1007 | /// getBody - If this Decl represents a declaration for a body of code, |
| 1008 | /// such as a function or method definition, this method returns the |
| 1009 | /// top-level Stmt* of that body. Otherwise this method returns null. |
| 1010 | virtual Stmt* getBody() const { return nullptr; } |
| 1011 | |
| 1012 | /// Returns true if this \c Decl represents a declaration for a body of |
| 1013 | /// code, such as a function or method definition. |
| 1014 | /// Note that \c hasBody can also return true if any redeclaration of this |
| 1015 | /// \c Decl represents a declaration for a body of code. |
| 1016 | virtual bool hasBody() const { return getBody() != nullptr; } |
| 1017 | |
| 1018 | /// getBodyRBrace - Gets the right brace of the body, if a body exists. |
| 1019 | /// This works whether the body is a CompoundStmt or a CXXTryStmt. |
| 1020 | SourceLocation getBodyRBrace() const; |
| 1021 | |
| 1022 | // global temp stats (until we have a per-module visitor) |
| 1023 | static void add(Kind k); |
| 1024 | static void EnableStatistics(); |
| 1025 | static void PrintStats(); |
| 1026 | |
| 1027 | /// isTemplateParameter - Determines whether this declaration is a |
| 1028 | /// template parameter. |
| 1029 | bool isTemplateParameter() const; |
| 1030 | |
| 1031 | /// isTemplateParameter - Determines whether this declaration is a |
| 1032 | /// template parameter pack. |
| 1033 | bool isTemplateParameterPack() const; |
| 1034 | |
| 1035 | /// Whether this declaration is a parameter pack. |
| 1036 | bool isParameterPack() const; |
| 1037 | |
| 1038 | /// returns true if this declaration is a template |
| 1039 | bool isTemplateDecl() const; |
| 1040 | |
| 1041 | /// Whether this declaration is a function or function template. |
| 1042 | bool isFunctionOrFunctionTemplate() const { |
| 1043 | return (DeclKind >= Decl::firstFunction && |
| 1044 | DeclKind <= Decl::lastFunction) || |
| 1045 | DeclKind == FunctionTemplate; |
| 1046 | } |
| 1047 | |
| 1048 | /// If this is a declaration that describes some template, this |
| 1049 | /// method returns that template declaration. |
| 1050 | /// |
| 1051 | /// Note that this returns nullptr for partial specializations, because they |
| 1052 | /// are not modeled as TemplateDecls. Use getDescribedTemplateParams to handle |
| 1053 | /// those cases. |
| 1054 | TemplateDecl *getDescribedTemplate() const; |
| 1055 | |
| 1056 | /// If this is a declaration that describes some template or partial |
| 1057 | /// specialization, this returns the corresponding template parameter list. |
| 1058 | const TemplateParameterList *getDescribedTemplateParams() const; |
| 1059 | |
| 1060 | /// Returns the function itself, or the templated function if this is a |
| 1061 | /// function template. |
| 1062 | FunctionDecl *getAsFunction() LLVM_READONLY__attribute__((__pure__)); |
| 1063 | |
| 1064 | const FunctionDecl *getAsFunction() const { |
| 1065 | return const_cast<Decl *>(this)->getAsFunction(); |
| 1066 | } |
| 1067 | |
| 1068 | /// Changes the namespace of this declaration to reflect that it's |
| 1069 | /// a function-local extern declaration. |
| 1070 | /// |
| 1071 | /// These declarations appear in the lexical context of the extern |
| 1072 | /// declaration, but in the semantic context of the enclosing namespace |
| 1073 | /// scope. |
| 1074 | void setLocalExternDecl() { |
| 1075 | Decl *Prev = getPreviousDecl(); |
| 1076 | IdentifierNamespace &= ~IDNS_Ordinary; |
| 1077 | |
| 1078 | // It's OK for the declaration to still have the "invisible friend" flag or |
| 1079 | // the "conflicts with tag declarations in this scope" flag for the outer |
| 1080 | // scope. |
| 1081 | assert((IdentifierNamespace & ~(IDNS_OrdinaryFriend | IDNS_Tag)) == 0 &&((void)0) |
| 1082 | "namespace is not ordinary")((void)0); |
| 1083 | |
| 1084 | IdentifierNamespace |= IDNS_LocalExtern; |
| 1085 | if (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary) |
| 1086 | IdentifierNamespace |= IDNS_Ordinary; |
| 1087 | } |
| 1088 | |
| 1089 | /// Determine whether this is a block-scope declaration with linkage. |
| 1090 | /// This will either be a local variable declaration declared 'extern', or a |
| 1091 | /// local function declaration. |
| 1092 | bool isLocalExternDecl() { |
| 1093 | return IdentifierNamespace & IDNS_LocalExtern; |
| 1094 | } |
| 1095 | |
| 1096 | /// Changes the namespace of this declaration to reflect that it's |
| 1097 | /// the object of a friend declaration. |
| 1098 | /// |
| 1099 | /// These declarations appear in the lexical context of the friending |
| 1100 | /// class, but in the semantic context of the actual entity. This property |
| 1101 | /// applies only to a specific decl object; other redeclarations of the |
| 1102 | /// same entity may not (and probably don't) share this property. |
| 1103 | void setObjectOfFriendDecl(bool PerformFriendInjection = false) { |
| 1104 | unsigned OldNS = IdentifierNamespace; |
| 1105 | assert((OldNS & (IDNS_Tag | IDNS_Ordinary |((void)0) |
| 1106 | IDNS_TagFriend | IDNS_OrdinaryFriend |((void)0) |
| 1107 | IDNS_LocalExtern | IDNS_NonMemberOperator)) &&((void)0) |
| 1108 | "namespace includes neither ordinary nor tag")((void)0); |
| 1109 | assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type |((void)0) |
| 1110 | IDNS_TagFriend | IDNS_OrdinaryFriend |((void)0) |
| 1111 | IDNS_LocalExtern | IDNS_NonMemberOperator)) &&((void)0) |
| 1112 | "namespace includes other than ordinary or tag")((void)0); |
| 1113 | |
| 1114 | Decl *Prev = getPreviousDecl(); |
| 1115 | IdentifierNamespace &= ~(IDNS_Ordinary | IDNS_Tag | IDNS_Type); |
| 1116 | |
| 1117 | if (OldNS & (IDNS_Tag | IDNS_TagFriend)) { |
| 1118 | IdentifierNamespace |= IDNS_TagFriend; |
| 1119 | if (PerformFriendInjection || |
| 1120 | (Prev && Prev->getIdentifierNamespace() & IDNS_Tag)) |
| 1121 | IdentifierNamespace |= IDNS_Tag | IDNS_Type; |
| 1122 | } |
| 1123 | |
| 1124 | if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend | |
| 1125 | IDNS_LocalExtern | IDNS_NonMemberOperator)) { |
| 1126 | IdentifierNamespace |= IDNS_OrdinaryFriend; |
| 1127 | if (PerformFriendInjection || |
| 1128 | (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary)) |
| 1129 | IdentifierNamespace |= IDNS_Ordinary; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | enum FriendObjectKind { |
| 1134 | FOK_None, ///< Not a friend object. |
| 1135 | FOK_Declared, ///< A friend of a previously-declared entity. |
| 1136 | FOK_Undeclared ///< A friend of a previously-undeclared entity. |
| 1137 | }; |
| 1138 | |
| 1139 | /// Determines whether this declaration is the object of a |
| 1140 | /// friend declaration and, if so, what kind. |
| 1141 | /// |
| 1142 | /// There is currently no direct way to find the associated FriendDecl. |
| 1143 | FriendObjectKind getFriendObjectKind() const { |
| 1144 | unsigned mask = |
| 1145 | (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend)); |
| 1146 | if (!mask) return FOK_None; |
| 1147 | return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? FOK_Declared |
| 1148 | : FOK_Undeclared); |
| 1149 | } |
| 1150 | |
| 1151 | /// Specifies that this declaration is a C++ overloaded non-member. |
| 1152 | void setNonMemberOperator() { |
| 1153 | assert(getKind() == Function || getKind() == FunctionTemplate)((void)0); |
| 1154 | assert((IdentifierNamespace & IDNS_Ordinary) &&((void)0) |
| 1155 | "visible non-member operators should be in ordinary namespace")((void)0); |
| 1156 | IdentifierNamespace |= IDNS_NonMemberOperator; |
| 1157 | } |
| 1158 | |
| 1159 | static bool classofKind(Kind K) { return true; } |
| 1160 | static DeclContext *castToDeclContext(const Decl *); |
| 1161 | static Decl *castFromDeclContext(const DeclContext *); |
| 1162 | |
| 1163 | void print(raw_ostream &Out, unsigned Indentation = 0, |
| 1164 | bool PrintInstantiation = false) const; |
| 1165 | void print(raw_ostream &Out, const PrintingPolicy &Policy, |
| 1166 | unsigned Indentation = 0, bool PrintInstantiation = false) const; |
| 1167 | static void printGroup(Decl** Begin, unsigned NumDecls, |
| 1168 | raw_ostream &Out, const PrintingPolicy &Policy, |
| 1169 | unsigned Indentation = 0); |
| 1170 | |
| 1171 | // Debuggers don't usually respect default arguments. |
| 1172 | void dump() const; |
| 1173 | |
| 1174 | // Same as dump(), but forces color printing. |
| 1175 | void dumpColor() const; |
| 1176 | |
| 1177 | void dump(raw_ostream &Out, bool Deserialize = false, |
| 1178 | ASTDumpOutputFormat OutputFormat = ADOF_Default) const; |
| 1179 | |
| 1180 | /// \return Unique reproducible object identifier |
| 1181 | int64_t getID() const; |
| 1182 | |
| 1183 | /// Looks through the Decl's underlying type to extract a FunctionType |
| 1184 | /// when possible. Will return null if the type underlying the Decl does not |
| 1185 | /// have a FunctionType. |
| 1186 | const FunctionType *getFunctionType(bool BlocksToo = true) const; |
| 1187 | |
| 1188 | private: |
| 1189 | void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx); |
| 1190 | void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC, |
| 1191 | ASTContext &Ctx); |
| 1192 | |
| 1193 | protected: |
| 1194 | ASTMutationListener *getASTMutationListener() const; |
| 1195 | }; |
| 1196 | |
| 1197 | /// Determine whether two declarations declare the same entity. |
| 1198 | inline bool declaresSameEntity(const Decl *D1, const Decl *D2) { |
| 1199 | if (!D1 || !D2) |
| 1200 | return false; |
| 1201 | |
| 1202 | if (D1 == D2) |
| 1203 | return true; |
| 1204 | |
| 1205 | return D1->getCanonicalDecl() == D2->getCanonicalDecl(); |
| 1206 | } |
| 1207 | |
| 1208 | /// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when |
| 1209 | /// doing something to a specific decl. |
| 1210 | class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry { |
| 1211 | const Decl *TheDecl; |
| 1212 | SourceLocation Loc; |
| 1213 | SourceManager &SM; |
| 1214 | const char *Message; |
| 1215 | |
| 1216 | public: |
| 1217 | PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L, |
| 1218 | SourceManager &sm, const char *Msg) |
| 1219 | : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {} |
| 1220 | |
| 1221 | void print(raw_ostream &OS) const override; |
| 1222 | }; |
| 1223 | } // namespace clang |
| 1224 | |
| 1225 | // Required to determine the layout of the PointerUnion<NamedDecl*> before |
| 1226 | // seeing the NamedDecl definition being first used in DeclListNode::operator*. |
| 1227 | namespace llvm { |
| 1228 | template <> struct PointerLikeTypeTraits<::clang::NamedDecl *> { |
| 1229 | static inline void *getAsVoidPointer(::clang::NamedDecl *P) { return P; } |
| 1230 | static inline ::clang::NamedDecl *getFromVoidPointer(void *P) { |
| 1231 | return static_cast<::clang::NamedDecl *>(P); |
| 1232 | } |
| 1233 | static constexpr int NumLowBitsAvailable = 3; |
| 1234 | }; |
| 1235 | } |
| 1236 | |
| 1237 | namespace clang { |
| 1238 | /// A list storing NamedDecls in the lookup tables. |
| 1239 | class DeclListNode { |
| 1240 | friend class ASTContext; // allocate, deallocate nodes. |
| 1241 | friend class StoredDeclsList; |
| 1242 | public: |
| 1243 | using Decls = llvm::PointerUnion<NamedDecl*, DeclListNode*>; |
| 1244 | class iterator { |
| 1245 | friend class DeclContextLookupResult; |
| 1246 | friend class StoredDeclsList; |
| 1247 | |
| 1248 | Decls Ptr; |
| 1249 | iterator(Decls Node) : Ptr(Node) { } |
| 1250 | public: |
| 1251 | using difference_type = ptrdiff_t; |
| 1252 | using value_type = NamedDecl*; |
| 1253 | using pointer = void; |
| 1254 | using reference = value_type; |
| 1255 | using iterator_category = std::forward_iterator_tag; |
| 1256 | |
| 1257 | iterator() = default; |
| 1258 | |
| 1259 | reference operator*() const { |
| 1260 | assert(Ptr && "dereferencing end() iterator")((void)0); |
| 1261 | if (DeclListNode *CurNode = Ptr.dyn_cast<DeclListNode*>()) |
| 1262 | return CurNode->D; |
| 1263 | return Ptr.get<NamedDecl*>(); |
| 1264 | } |
| 1265 | void operator->() const { } // Unsupported. |
| 1266 | bool operator==(const iterator &X) const { return Ptr == X.Ptr; } |
| 1267 | bool operator!=(const iterator &X) const { return Ptr != X.Ptr; } |
| 1268 | inline iterator &operator++() { // ++It |
| 1269 | assert(!Ptr.isNull() && "Advancing empty iterator")((void)0); |
| 1270 | |
| 1271 | if (DeclListNode *CurNode = Ptr.dyn_cast<DeclListNode*>()) |
| 1272 | Ptr = CurNode->Rest; |
| 1273 | else |
| 1274 | Ptr = nullptr; |
| 1275 | return *this; |
| 1276 | } |
| 1277 | iterator operator++(int) { // It++ |
| 1278 | iterator temp = *this; |
| 1279 | ++(*this); |
| 1280 | return temp; |
| 1281 | } |
| 1282 | // Enables the pattern for (iterator I =..., E = I.end(); I != E; ++I) |
| 1283 | iterator end() { return iterator(); } |
| 1284 | }; |
| 1285 | private: |
| 1286 | NamedDecl *D = nullptr; |
| 1287 | Decls Rest = nullptr; |
| 1288 | DeclListNode(NamedDecl *ND) : D(ND) {} |
| 1289 | }; |
| 1290 | |
| 1291 | /// The results of name lookup within a DeclContext. |
| 1292 | class DeclContextLookupResult { |
| 1293 | using Decls = DeclListNode::Decls; |
| 1294 | |
| 1295 | /// When in collection form, this is what the Data pointer points to. |
| 1296 | Decls Result; |
| 1297 | |
| 1298 | public: |
| 1299 | DeclContextLookupResult() = default; |
| 1300 | DeclContextLookupResult(Decls Result) : Result(Result) {} |
| 1301 | |
| 1302 | using iterator = DeclListNode::iterator; |
| 1303 | using const_iterator = iterator; |
| 1304 | using reference = iterator::reference; |
| 1305 | |
| 1306 | iterator begin() { return iterator(Result); } |
| 1307 | iterator end() { return iterator(); } |
| 1308 | const_iterator begin() const { |
| 1309 | return const_cast<DeclContextLookupResult*>(this)->begin(); |
| 1310 | } |
| 1311 | const_iterator end() const { return iterator(); } |
| 1312 | |
| 1313 | bool empty() const { return Result.isNull(); } |
| 1314 | bool isSingleResult() const { return Result.dyn_cast<NamedDecl*>(); } |
| 1315 | reference front() const { return *begin(); } |
| 1316 | |
| 1317 | // Find the first declaration of the given type in the list. Note that this |
| 1318 | // is not in general the earliest-declared declaration, and should only be |
| 1319 | // used when it's not possible for there to be more than one match or where |
| 1320 | // it doesn't matter which one is found. |
| 1321 | template<class T> T *find_first() const { |
| 1322 | for (auto *D : *this) |
| 1323 | if (T *Decl = dyn_cast<T>(D)) |
| 1324 | return Decl; |
| 1325 | |
| 1326 | return nullptr; |
| 1327 | } |
| 1328 | }; |
| 1329 | |
| 1330 | /// DeclContext - This is used only as base class of specific decl types that |
| 1331 | /// can act as declaration contexts. These decls are (only the top classes |
| 1332 | /// that directly derive from DeclContext are mentioned, not their subclasses): |
| 1333 | /// |
| 1334 | /// TranslationUnitDecl |
| 1335 | /// ExternCContext |
| 1336 | /// NamespaceDecl |
| 1337 | /// TagDecl |
| 1338 | /// OMPDeclareReductionDecl |
| 1339 | /// OMPDeclareMapperDecl |
| 1340 | /// FunctionDecl |
| 1341 | /// ObjCMethodDecl |
| 1342 | /// ObjCContainerDecl |
| 1343 | /// LinkageSpecDecl |
| 1344 | /// ExportDecl |
| 1345 | /// BlockDecl |
| 1346 | /// CapturedDecl |
| 1347 | class DeclContext { |
| 1348 | /// For makeDeclVisibleInContextImpl |
| 1349 | friend class ASTDeclReader; |
| 1350 | /// For reconcileExternalVisibleStorage, CreateStoredDeclsMap, |
| 1351 | /// hasNeedToReconcileExternalVisibleStorage |
| 1352 | friend class ExternalASTSource; |
| 1353 | /// For CreateStoredDeclsMap |
| 1354 | friend class DependentDiagnostic; |
| 1355 | /// For hasNeedToReconcileExternalVisibleStorage, |
| 1356 | /// hasLazyLocalLexicalLookups, hasLazyExternalLexicalLookups |
| 1357 | friend class ASTWriter; |
| 1358 | |
| 1359 | // We use uint64_t in the bit-fields below since some bit-fields |
| 1360 | // cross the unsigned boundary and this breaks the packing. |
| 1361 | |
| 1362 | /// Stores the bits used by DeclContext. |
| 1363 | /// If modified NumDeclContextBit, the ctor of DeclContext and the accessor |
| 1364 | /// methods in DeclContext should be updated appropriately. |
| 1365 | class DeclContextBitfields { |
| 1366 | friend class DeclContext; |
| 1367 | /// DeclKind - This indicates which class this is. |
| 1368 | uint64_t DeclKind : 7; |
| 1369 | |
| 1370 | /// Whether this declaration context also has some external |
| 1371 | /// storage that contains additional declarations that are lexically |
| 1372 | /// part of this context. |
| 1373 | mutable uint64_t ExternalLexicalStorage : 1; |
| 1374 | |
| 1375 | /// Whether this declaration context also has some external |
| 1376 | /// storage that contains additional declarations that are visible |
| 1377 | /// in this context. |
| 1378 | mutable uint64_t ExternalVisibleStorage : 1; |
| 1379 | |
| 1380 | /// Whether this declaration context has had externally visible |
| 1381 | /// storage added since the last lookup. In this case, \c LookupPtr's |
| 1382 | /// invariant may not hold and needs to be fixed before we perform |
| 1383 | /// another lookup. |
| 1384 | mutable uint64_t NeedToReconcileExternalVisibleStorage : 1; |
| 1385 | |
| 1386 | /// If \c true, this context may have local lexical declarations |
| 1387 | /// that are missing from the lookup table. |
| 1388 | mutable uint64_t HasLazyLocalLexicalLookups : 1; |
| 1389 | |
| 1390 | /// If \c true, the external source may have lexical declarations |
| 1391 | /// that are missing from the lookup table. |
| 1392 | mutable uint64_t HasLazyExternalLexicalLookups : 1; |
| 1393 | |
| 1394 | /// If \c true, lookups should only return identifier from |
| 1395 | /// DeclContext scope (for example TranslationUnit). Used in |
| 1396 | /// LookupQualifiedName() |
| 1397 | mutable uint64_t UseQualifiedLookup : 1; |
| 1398 | }; |
| 1399 | |
| 1400 | /// Number of bits in DeclContextBitfields. |
| 1401 | enum { NumDeclContextBits = 13 }; |
| 1402 | |
| 1403 | /// Stores the bits used by TagDecl. |
| 1404 | /// If modified NumTagDeclBits and the accessor |
| 1405 | /// methods in TagDecl should be updated appropriately. |
| 1406 | class TagDeclBitfields { |
| 1407 | friend class TagDecl; |
| 1408 | /// For the bits in DeclContextBitfields |
| 1409 | uint64_t : NumDeclContextBits; |
| 1410 | |
| 1411 | /// The TagKind enum. |
| 1412 | uint64_t TagDeclKind : 3; |
| 1413 | |
| 1414 | /// True if this is a definition ("struct foo {};"), false if it is a |
| 1415 | /// declaration ("struct foo;"). It is not considered a definition |
| 1416 | /// until the definition has been fully processed. |
| 1417 | uint64_t IsCompleteDefinition : 1; |
| 1418 | |
| 1419 | /// True if this is currently being defined. |
| 1420 | uint64_t IsBeingDefined : 1; |
| 1421 | |
| 1422 | /// True if this tag declaration is "embedded" (i.e., defined or declared |
| 1423 | /// for the very first time) in the syntax of a declarator. |
| 1424 | uint64_t IsEmbeddedInDeclarator : 1; |
| 1425 | |
| 1426 | /// True if this tag is free standing, e.g. "struct foo;". |
| 1427 | uint64_t IsFreeStanding : 1; |
| 1428 | |
| 1429 | /// Indicates whether it is possible for declarations of this kind |
| 1430 | /// to have an out-of-date definition. |
| 1431 | /// |
| 1432 | /// This option is only enabled when modules are enabled. |
| 1433 | uint64_t MayHaveOutOfDateDef : 1; |
| 1434 | |
| 1435 | /// Has the full definition of this type been required by a use somewhere in |
| 1436 | /// the TU. |
| 1437 | uint64_t IsCompleteDefinitionRequired : 1; |
| 1438 | }; |
| 1439 | |
| 1440 | /// Number of non-inherited bits in TagDeclBitfields. |
| 1441 | enum { NumTagDeclBits = 9 }; |
| 1442 | |
| 1443 | /// Stores the bits used by EnumDecl. |
| 1444 | /// If modified NumEnumDeclBit and the accessor |
| 1445 | /// methods in EnumDecl should be updated appropriately. |
| 1446 | class EnumDeclBitfields { |
| 1447 | friend class EnumDecl; |
| 1448 | /// For the bits in DeclContextBitfields. |
| 1449 | uint64_t : NumDeclContextBits; |
| 1450 | /// For the bits in TagDeclBitfields. |
| 1451 | uint64_t : NumTagDeclBits; |
| 1452 | |
| 1453 | /// Width in bits required to store all the non-negative |
| 1454 | /// enumerators of this enum. |
| 1455 | uint64_t NumPositiveBits : 8; |
| 1456 | |
| 1457 | /// Width in bits required to store all the negative |
| 1458 | /// enumerators of this enum. |
| 1459 | uint64_t NumNegativeBits : 8; |
| 1460 | |
| 1461 | /// True if this tag declaration is a scoped enumeration. Only |
| 1462 | /// possible in C++11 mode. |
| 1463 | uint64_t IsScoped : 1; |
| 1464 | |
| 1465 | /// If this tag declaration is a scoped enum, |
| 1466 | /// then this is true if the scoped enum was declared using the class |
| 1467 | /// tag, false if it was declared with the struct tag. No meaning is |
| 1468 | /// associated if this tag declaration is not a scoped enum. |
| 1469 | uint64_t IsScopedUsingClassTag : 1; |
| 1470 | |
| 1471 | /// True if this is an enumeration with fixed underlying type. Only |
| 1472 | /// possible in C++11, Microsoft extensions, or Objective C mode. |
| 1473 | uint64_t IsFixed : 1; |
| 1474 | |
| 1475 | /// True if a valid hash is stored in ODRHash. |
| 1476 | uint64_t HasODRHash : 1; |
| 1477 | }; |
| 1478 | |
| 1479 | /// Number of non-inherited bits in EnumDeclBitfields. |
| 1480 | enum { NumEnumDeclBits = 20 }; |
| 1481 | |
| 1482 | /// Stores the bits used by RecordDecl. |
| 1483 | /// If modified NumRecordDeclBits and the accessor |
| 1484 | /// methods in RecordDecl should be updated appropriately. |
| 1485 | class RecordDeclBitfields { |
| 1486 | friend class RecordDecl; |
| 1487 | /// For the bits in DeclContextBitfields. |
| 1488 | uint64_t : NumDeclContextBits; |
| 1489 | /// For the bits in TagDeclBitfields. |
| 1490 | uint64_t : NumTagDeclBits; |
| 1491 | |
| 1492 | /// This is true if this struct ends with a flexible |
| 1493 | /// array member (e.g. int X[]) or if this union contains a struct that does. |
| 1494 | /// If so, this cannot be contained in arrays or other structs as a member. |
| 1495 | uint64_t HasFlexibleArrayMember : 1; |
| 1496 | |
| 1497 | /// Whether this is the type of an anonymous struct or union. |
| 1498 | uint64_t AnonymousStructOrUnion : 1; |
| 1499 | |
| 1500 | /// This is true if this struct has at least one member |
| 1501 | /// containing an Objective-C object pointer type. |
| 1502 | uint64_t HasObjectMember : 1; |
| 1503 | |
| 1504 | /// This is true if struct has at least one member of |
| 1505 | /// 'volatile' type. |
| 1506 | uint64_t HasVolatileMember : 1; |
| 1507 | |
| 1508 | /// Whether the field declarations of this record have been loaded |
| 1509 | /// from external storage. To avoid unnecessary deserialization of |
| 1510 | /// methods/nested types we allow deserialization of just the fields |
| 1511 | /// when needed. |
| 1512 | mutable uint64_t LoadedFieldsFromExternalStorage : 1; |
| 1513 | |
| 1514 | /// Basic properties of non-trivial C structs. |
| 1515 | uint64_t NonTrivialToPrimitiveDefaultInitialize : 1; |
| 1516 | uint64_t NonTrivialToPrimitiveCopy : 1; |
| 1517 | uint64_t NonTrivialToPrimitiveDestroy : 1; |
| 1518 | |
| 1519 | /// The following bits indicate whether this is or contains a C union that |
| 1520 | /// is non-trivial to default-initialize, destruct, or copy. These bits |
| 1521 | /// imply the associated basic non-triviality predicates declared above. |
| 1522 | uint64_t HasNonTrivialToPrimitiveDefaultInitializeCUnion : 1; |
| 1523 | uint64_t HasNonTrivialToPrimitiveDestructCUnion : 1; |
| 1524 | uint64_t HasNonTrivialToPrimitiveCopyCUnion : 1; |
| 1525 | |
| 1526 | /// Indicates whether this struct is destroyed in the callee. |
| 1527 | uint64_t ParamDestroyedInCallee : 1; |
| 1528 | |
| 1529 | /// Represents the way this type is passed to a function. |
| 1530 | uint64_t ArgPassingRestrictions : 2; |
| 1531 | }; |
| 1532 | |
| 1533 | /// Number of non-inherited bits in RecordDeclBitfields. |
| 1534 | enum { NumRecordDeclBits = 14 }; |
| 1535 | |
| 1536 | /// Stores the bits used by OMPDeclareReductionDecl. |
| 1537 | /// If modified NumOMPDeclareReductionDeclBits and the accessor |
| 1538 | /// methods in OMPDeclareReductionDecl should be updated appropriately. |
| 1539 | class OMPDeclareReductionDeclBitfields { |
| 1540 | friend class OMPDeclareReductionDecl; |
| 1541 | /// For the bits in DeclContextBitfields |
| 1542 | uint64_t : NumDeclContextBits; |
| 1543 | |
| 1544 | /// Kind of initializer, |
| 1545 | /// function call or omp_priv<init_expr> initializtion. |
| 1546 | uint64_t InitializerKind : 2; |
| 1547 | }; |
| 1548 | |
| 1549 | /// Number of non-inherited bits in OMPDeclareReductionDeclBitfields. |
| 1550 | enum { NumOMPDeclareReductionDeclBits = 2 }; |
| 1551 | |
| 1552 | /// Stores the bits used by FunctionDecl. |
| 1553 | /// If modified NumFunctionDeclBits and the accessor |
| 1554 | /// methods in FunctionDecl and CXXDeductionGuideDecl |
| 1555 | /// (for IsCopyDeductionCandidate) should be updated appropriately. |
| 1556 | class FunctionDeclBitfields { |
| 1557 | friend class FunctionDecl; |
| 1558 | /// For IsCopyDeductionCandidate |
| 1559 | friend class CXXDeductionGuideDecl; |
| 1560 | /// For the bits in DeclContextBitfields. |
| 1561 | uint64_t : NumDeclContextBits; |
| 1562 | |
| 1563 | uint64_t SClass : 3; |
| 1564 | uint64_t IsInline : 1; |
| 1565 | uint64_t IsInlineSpecified : 1; |
| 1566 | |
| 1567 | uint64_t IsVirtualAsWritten : 1; |
| 1568 | uint64_t IsPure : 1; |
| 1569 | uint64_t HasInheritedPrototype : 1; |
| 1570 | uint64_t HasWrittenPrototype : 1; |
| 1571 | uint64_t IsDeleted : 1; |
| 1572 | /// Used by CXXMethodDecl |
| 1573 | uint64_t IsTrivial : 1; |
| 1574 | |
| 1575 | /// This flag indicates whether this function is trivial for the purpose of |
| 1576 | /// calls. This is meaningful only when this function is a copy/move |
| 1577 | /// constructor or a destructor. |
| 1578 | uint64_t IsTrivialForCall : 1; |
| 1579 | |
| 1580 | uint64_t IsDefaulted : 1; |
| 1581 | uint64_t IsExplicitlyDefaulted : 1; |
| 1582 | uint64_t HasDefaultedFunctionInfo : 1; |
| 1583 | uint64_t HasImplicitReturnZero : 1; |
| 1584 | uint64_t IsLateTemplateParsed : 1; |
| 1585 | |
| 1586 | /// Kind of contexpr specifier as defined by ConstexprSpecKind. |
| 1587 | uint64_t ConstexprKind : 2; |
| 1588 | uint64_t InstantiationIsPending : 1; |
| 1589 | |
| 1590 | /// Indicates if the function uses __try. |
| 1591 | uint64_t UsesSEHTry : 1; |
| 1592 | |
| 1593 | /// Indicates if the function was a definition |
| 1594 | /// but its body was skipped. |
| 1595 | uint64_t HasSkippedBody : 1; |
| 1596 | |
| 1597 | /// Indicates if the function declaration will |
| 1598 | /// have a body, once we're done parsing it. |
| 1599 | uint64_t WillHaveBody : 1; |
| 1600 | |
| 1601 | /// Indicates that this function is a multiversioned |
| 1602 | /// function using attribute 'target'. |
| 1603 | uint64_t IsMultiVersion : 1; |
| 1604 | |
| 1605 | /// [C++17] Only used by CXXDeductionGuideDecl. Indicates that |
| 1606 | /// the Deduction Guide is the implicitly generated 'copy |
| 1607 | /// deduction candidate' (is used during overload resolution). |
| 1608 | uint64_t IsCopyDeductionCandidate : 1; |
| 1609 | |
| 1610 | /// Store the ODRHash after first calculation. |
| 1611 | uint64_t HasODRHash : 1; |
| 1612 | |
| 1613 | /// Indicates if the function uses Floating Point Constrained Intrinsics |
| 1614 | uint64_t UsesFPIntrin : 1; |
| 1615 | }; |
| 1616 | |
| 1617 | /// Number of non-inherited bits in FunctionDeclBitfields. |
| 1618 | enum { NumFunctionDeclBits = 27 }; |
| 1619 | |
| 1620 | /// Stores the bits used by CXXConstructorDecl. If modified |
| 1621 | /// NumCXXConstructorDeclBits and the accessor |
| 1622 | /// methods in CXXConstructorDecl should be updated appropriately. |
| 1623 | class CXXConstructorDeclBitfields { |
| 1624 | friend class CXXConstructorDecl; |
| 1625 | /// For the bits in DeclContextBitfields. |
| 1626 | uint64_t : NumDeclContextBits; |
| 1627 | /// For the bits in FunctionDeclBitfields. |
| 1628 | uint64_t : NumFunctionDeclBits; |
| 1629 | |
| 1630 | /// 24 bits to fit in the remaining available space. |
| 1631 | /// Note that this makes CXXConstructorDeclBitfields take |
| 1632 | /// exactly 64 bits and thus the width of NumCtorInitializers |
| 1633 | /// will need to be shrunk if some bit is added to NumDeclContextBitfields, |
| 1634 | /// NumFunctionDeclBitfields or CXXConstructorDeclBitfields. |
| 1635 | uint64_t NumCtorInitializers : 21; |
| 1636 | uint64_t IsInheritingConstructor : 1; |
| 1637 | |
| 1638 | /// Whether this constructor has a trail-allocated explicit specifier. |
| 1639 | uint64_t HasTrailingExplicitSpecifier : 1; |
| 1640 | /// If this constructor does't have a trail-allocated explicit specifier. |
| 1641 | /// Whether this constructor is explicit specified. |
| 1642 | uint64_t IsSimpleExplicit : 1; |
| 1643 | }; |
| 1644 | |
| 1645 | /// Number of non-inherited bits in CXXConstructorDeclBitfields. |
| 1646 | enum { |
| 1647 | NumCXXConstructorDeclBits = 64 - NumDeclContextBits - NumFunctionDeclBits |
| 1648 | }; |
| 1649 | |
| 1650 | /// Stores the bits used by ObjCMethodDecl. |
| 1651 | /// If modified NumObjCMethodDeclBits and the accessor |
| 1652 | /// methods in ObjCMethodDecl should be updated appropriately. |
| 1653 | class ObjCMethodDeclBitfields { |
| 1654 | friend class ObjCMethodDecl; |
| 1655 | |
| 1656 | /// For the bits in DeclContextBitfields. |
| 1657 | uint64_t : NumDeclContextBits; |
| 1658 | |
| 1659 | /// The conventional meaning of this method; an ObjCMethodFamily. |
| 1660 | /// This is not serialized; instead, it is computed on demand and |
| 1661 | /// cached. |
| 1662 | mutable uint64_t Family : ObjCMethodFamilyBitWidth; |
| 1663 | |
| 1664 | /// instance (true) or class (false) method. |
| 1665 | uint64_t IsInstance : 1; |
| 1666 | uint64_t IsVariadic : 1; |
| 1667 | |
| 1668 | /// True if this method is the getter or setter for an explicit property. |
| 1669 | uint64_t IsPropertyAccessor : 1; |
| 1670 | |
| 1671 | /// True if this method is a synthesized property accessor stub. |
| 1672 | uint64_t IsSynthesizedAccessorStub : 1; |
| 1673 | |
| 1674 | /// Method has a definition. |
| 1675 | uint64_t IsDefined : 1; |
| 1676 | |
| 1677 | /// Method redeclaration in the same interface. |
| 1678 | uint64_t IsRedeclaration : 1; |
| 1679 | |
| 1680 | /// Is redeclared in the same interface. |
| 1681 | mutable uint64_t HasRedeclaration : 1; |
| 1682 | |
| 1683 | /// \@required/\@optional |
| 1684 | uint64_t DeclImplementation : 2; |
| 1685 | |
| 1686 | /// in, inout, etc. |
| 1687 | uint64_t objcDeclQualifier : 7; |
| 1688 | |
| 1689 | /// Indicates whether this method has a related result type. |
| 1690 | uint64_t RelatedResultType : 1; |
| 1691 | |
| 1692 | /// Whether the locations of the selector identifiers are in a |
| 1693 | /// "standard" position, a enum SelectorLocationsKind. |
| 1694 | uint64_t SelLocsKind : 2; |
| 1695 | |
| 1696 | /// Whether this method overrides any other in the class hierarchy. |
| 1697 | /// |
| 1698 | /// A method is said to override any method in the class's |
| 1699 | /// base classes, its protocols, or its categories' protocols, that has |
| 1700 | /// the same selector and is of the same kind (class or instance). |
| 1701 | /// A method in an implementation is not considered as overriding the same |
| 1702 | /// method in the interface or its categories. |
| 1703 | uint64_t IsOverriding : 1; |
| 1704 | |
| 1705 | /// Indicates if the method was a definition but its body was skipped. |
| 1706 | uint64_t HasSkippedBody : 1; |
| 1707 | }; |
| 1708 | |
| 1709 | /// Number of non-inherited bits in ObjCMethodDeclBitfields. |
| 1710 | enum { NumObjCMethodDeclBits = 24 }; |
| 1711 | |
| 1712 | /// Stores the bits used by ObjCContainerDecl. |
| 1713 | /// If modified NumObjCContainerDeclBits and the accessor |
| 1714 | /// methods in ObjCContainerDecl should be updated appropriately. |
| 1715 | class ObjCContainerDeclBitfields { |
| 1716 | friend class ObjCContainerDecl; |
| 1717 | /// For the bits in DeclContextBitfields |
| 1718 | uint32_t : NumDeclContextBits; |
| 1719 | |
| 1720 | // Not a bitfield but this saves space. |
| 1721 | // Note that ObjCContainerDeclBitfields is full. |
| 1722 | SourceLocation AtStart; |
| 1723 | }; |
| 1724 | |
| 1725 | /// Number of non-inherited bits in ObjCContainerDeclBitfields. |
| 1726 | /// Note that here we rely on the fact that SourceLocation is 32 bits |
| 1727 | /// wide. We check this with the static_assert in the ctor of DeclContext. |
| 1728 | enum { NumObjCContainerDeclBits = 64 - NumDeclContextBits }; |
| 1729 | |
| 1730 | /// Stores the bits used by LinkageSpecDecl. |
| 1731 | /// If modified NumLinkageSpecDeclBits and the accessor |
| 1732 | /// methods in LinkageSpecDecl should be updated appropriately. |
| 1733 | class LinkageSpecDeclBitfields { |
| 1734 | friend class LinkageSpecDecl; |
| 1735 | /// For the bits in DeclContextBitfields. |
| 1736 | uint64_t : NumDeclContextBits; |
| 1737 | |
| 1738 | /// The language for this linkage specification with values |
| 1739 | /// in the enum LinkageSpecDecl::LanguageIDs. |
| 1740 | uint64_t Language : 3; |
| 1741 | |
| 1742 | /// True if this linkage spec has braces. |
| 1743 | /// This is needed so that hasBraces() returns the correct result while the |
| 1744 | /// linkage spec body is being parsed. Once RBraceLoc has been set this is |
| 1745 | /// not used, so it doesn't need to be serialized. |
| 1746 | uint64_t HasBraces : 1; |
| 1747 | }; |
| 1748 | |
| 1749 | /// Number of non-inherited bits in LinkageSpecDeclBitfields. |
| 1750 | enum { NumLinkageSpecDeclBits = 4 }; |
| 1751 | |
| 1752 | /// Stores the bits used by BlockDecl. |
| 1753 | /// If modified NumBlockDeclBits and the accessor |
| 1754 | /// methods in BlockDecl should be updated appropriately. |
| 1755 | class BlockDeclBitfields { |
| 1756 | friend class BlockDecl; |
| 1757 | /// For the bits in DeclContextBitfields. |
| 1758 | uint64_t : NumDeclContextBits; |
| 1759 | |
| 1760 | uint64_t IsVariadic : 1; |
| 1761 | uint64_t CapturesCXXThis : 1; |
| 1762 | uint64_t BlockMissingReturnType : 1; |
| 1763 | uint64_t IsConversionFromLambda : 1; |
| 1764 | |
| 1765 | /// A bit that indicates this block is passed directly to a function as a |
| 1766 | /// non-escaping parameter. |
| 1767 | uint64_t DoesNotEscape : 1; |
| 1768 | |
| 1769 | /// A bit that indicates whether it's possible to avoid coying this block to |
| 1770 | /// the heap when it initializes or is assigned to a local variable with |
| 1771 | /// automatic storage. |
| 1772 | uint64_t CanAvoidCopyToHeap : 1; |
| 1773 | }; |
| 1774 | |
| 1775 | /// Number of non-inherited bits in BlockDeclBitfields. |
| 1776 | enum { NumBlockDeclBits = 5 }; |
| 1777 | |
| 1778 | /// Pointer to the data structure used to lookup declarations |
| 1779 | /// within this context (or a DependentStoredDeclsMap if this is a |
| 1780 | /// dependent context). We maintain the invariant that, if the map |
| 1781 | /// contains an entry for a DeclarationName (and we haven't lazily |
| 1782 | /// omitted anything), then it contains all relevant entries for that |
| 1783 | /// name (modulo the hasExternalDecls() flag). |
| 1784 | mutable StoredDeclsMap *LookupPtr = nullptr; |
| 1785 | |
| 1786 | protected: |
| 1787 | /// This anonymous union stores the bits belonging to DeclContext and classes |
| 1788 | /// deriving from it. The goal is to use otherwise wasted |
| 1789 | /// space in DeclContext to store data belonging to derived classes. |
| 1790 | /// The space saved is especially significient when pointers are aligned |
| 1791 | /// to 8 bytes. In this case due to alignment requirements we have a |
| 1792 | /// little less than 8 bytes free in DeclContext which we can use. |
| 1793 | /// We check that none of the classes in this union is larger than |
| 1794 | /// 8 bytes with static_asserts in the ctor of DeclContext. |
| 1795 | union { |
| 1796 | DeclContextBitfields DeclContextBits; |
| 1797 | TagDeclBitfields TagDeclBits; |
| 1798 | EnumDeclBitfields EnumDeclBits; |
| 1799 | RecordDeclBitfields RecordDeclBits; |
| 1800 | OMPDeclareReductionDeclBitfields OMPDeclareReductionDeclBits; |
| 1801 | FunctionDeclBitfields FunctionDeclBits; |
| 1802 | CXXConstructorDeclBitfields CXXConstructorDeclBits; |
| 1803 | ObjCMethodDeclBitfields ObjCMethodDeclBits; |
| 1804 | ObjCContainerDeclBitfields ObjCContainerDeclBits; |
| 1805 | LinkageSpecDeclBitfields LinkageSpecDeclBits; |
| 1806 | BlockDeclBitfields BlockDeclBits; |
| 1807 | |
| 1808 | static_assert(sizeof(DeclContextBitfields) <= 8, |
| 1809 | "DeclContextBitfields is larger than 8 bytes!"); |
| 1810 | static_assert(sizeof(TagDeclBitfields) <= 8, |
| 1811 | "TagDeclBitfields is larger than 8 bytes!"); |
| 1812 | static_assert(sizeof(EnumDeclBitfields) <= 8, |
| 1813 | "EnumDeclBitfields is larger than 8 bytes!"); |
| 1814 | static_assert(sizeof(RecordDeclBitfields) <= 8, |
| 1815 | "RecordDeclBitfields is larger than 8 bytes!"); |
| 1816 | static_assert(sizeof(OMPDeclareReductionDeclBitfields) <= 8, |
| 1817 | "OMPDeclareReductionDeclBitfields is larger than 8 bytes!"); |
| 1818 | static_assert(sizeof(FunctionDeclBitfields) <= 8, |
| 1819 | "FunctionDeclBitfields is larger than 8 bytes!"); |
| 1820 | static_assert(sizeof(CXXConstructorDeclBitfields) <= 8, |
| 1821 | "CXXConstructorDeclBitfields is larger than 8 bytes!"); |
| 1822 | static_assert(sizeof(ObjCMethodDeclBitfields) <= 8, |
| 1823 | "ObjCMethodDeclBitfields is larger than 8 bytes!"); |
| 1824 | static_assert(sizeof(ObjCContainerDeclBitfields) <= 8, |
| 1825 | "ObjCContainerDeclBitfields is larger than 8 bytes!"); |
| 1826 | static_assert(sizeof(LinkageSpecDeclBitfields) <= 8, |
| 1827 | "LinkageSpecDeclBitfields is larger than 8 bytes!"); |
| 1828 | static_assert(sizeof(BlockDeclBitfields) <= 8, |
| 1829 | "BlockDeclBitfields is larger than 8 bytes!"); |
| 1830 | }; |
| 1831 | |
| 1832 | /// FirstDecl - The first declaration stored within this declaration |
| 1833 | /// context. |
| 1834 | mutable Decl *FirstDecl = nullptr; |
| 1835 | |
| 1836 | /// LastDecl - The last declaration stored within this declaration |
| 1837 | /// context. FIXME: We could probably cache this value somewhere |
| 1838 | /// outside of the DeclContext, to reduce the size of DeclContext by |
| 1839 | /// another pointer. |
| 1840 | mutable Decl *LastDecl = nullptr; |
| 1841 | |
| 1842 | /// Build up a chain of declarations. |
| 1843 | /// |
| 1844 | /// \returns the first/last pair of declarations. |
| 1845 | static std::pair<Decl *, Decl *> |
| 1846 | BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded); |
| 1847 | |
| 1848 | DeclContext(Decl::Kind K); |
| 1849 | |
| 1850 | public: |
| 1851 | ~DeclContext(); |
| 1852 | |
| 1853 | Decl::Kind getDeclKind() const { |
| 1854 | return static_cast<Decl::Kind>(DeclContextBits.DeclKind); |
| 1855 | } |
| 1856 | |
| 1857 | const char *getDeclKindName() const; |
| 1858 | |
| 1859 | /// getParent - Returns the containing DeclContext. |
| 1860 | DeclContext *getParent() { |
| 1861 | return cast<Decl>(this)->getDeclContext(); |
| 1862 | } |
| 1863 | const DeclContext *getParent() const { |
| 1864 | return const_cast<DeclContext*>(this)->getParent(); |
| 1865 | } |
| 1866 | |
| 1867 | /// getLexicalParent - Returns the containing lexical DeclContext. May be |
| 1868 | /// different from getParent, e.g.: |
| 1869 | /// |
| 1870 | /// namespace A { |
| 1871 | /// struct S; |
| 1872 | /// } |
| 1873 | /// struct A::S {}; // getParent() == namespace 'A' |
| 1874 | /// // getLexicalParent() == translation unit |
| 1875 | /// |
| 1876 | DeclContext *getLexicalParent() { |
| 1877 | return cast<Decl>(this)->getLexicalDeclContext(); |
| 1878 | } |
| 1879 | const DeclContext *getLexicalParent() const { |
| 1880 | return const_cast<DeclContext*>(this)->getLexicalParent(); |
| 1881 | } |
| 1882 | |
| 1883 | DeclContext *getLookupParent(); |
| 1884 | |
| 1885 | const DeclContext *getLookupParent() const { |
| 1886 | return const_cast<DeclContext*>(this)->getLookupParent(); |
| 1887 | } |
| 1888 | |
| 1889 | ASTContext &getParentASTContext() const { |
| 1890 | return cast<Decl>(this)->getASTContext(); |
| 1891 | } |
| 1892 | |
| 1893 | bool isClosure() const { return getDeclKind() == Decl::Block; } |
| 1894 | |
| 1895 | /// Return this DeclContext if it is a BlockDecl. Otherwise, return the |
| 1896 | /// innermost enclosing BlockDecl or null if there are no enclosing blocks. |
| 1897 | const BlockDecl *getInnermostBlockDecl() const; |
| 1898 | |
| 1899 | bool isObjCContainer() const { |
| 1900 | switch (getDeclKind()) { |
| 1901 | case Decl::ObjCCategory: |
| 1902 | case Decl::ObjCCategoryImpl: |
| 1903 | case Decl::ObjCImplementation: |
| 1904 | case Decl::ObjCInterface: |
| 1905 | case Decl::ObjCProtocol: |
| 1906 | return true; |
| 1907 | default: |
| 1908 | return false; |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | bool isFunctionOrMethod() const { |
| 1913 | switch (getDeclKind()) { |
| 1914 | case Decl::Block: |
| 1915 | case Decl::Captured: |
| 1916 | case Decl::ObjCMethod: |
| 1917 | return true; |
| 1918 | default: |
| 1919 | return getDeclKind() >= Decl::firstFunction && |
| 1920 | getDeclKind() <= Decl::lastFunction; |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | /// Test whether the context supports looking up names. |
| 1925 | bool isLookupContext() const { |
| 1926 | return !isFunctionOrMethod() && getDeclKind() != Decl::LinkageSpec && |
| 1927 | getDeclKind() != Decl::Export; |
| 1928 | } |
| 1929 | |
| 1930 | bool isFileContext() const { |
| 1931 | return getDeclKind() == Decl::TranslationUnit || |
| 1932 | getDeclKind() == Decl::Namespace; |
| 1933 | } |
| 1934 | |
| 1935 | bool isTranslationUnit() const { |
| 1936 | return getDeclKind() == Decl::TranslationUnit; |
| 1937 | } |
| 1938 | |
| 1939 | bool isRecord() const { |
| 1940 | return getDeclKind() >= Decl::firstRecord && |
| 1941 | getDeclKind() <= Decl::lastRecord; |
| 1942 | } |
| 1943 | |
| 1944 | bool isNamespace() const { return getDeclKind() == Decl::Namespace; } |
| 1945 | |
| 1946 | bool isStdNamespace() const; |
| 1947 | |
| 1948 | bool isInlineNamespace() const; |
| 1949 | |
| 1950 | /// Determines whether this context is dependent on a |
| 1951 | /// template parameter. |
| 1952 | bool isDependentContext() const; |
| 1953 | |
| 1954 | /// isTransparentContext - Determines whether this context is a |
| 1955 | /// "transparent" context, meaning that the members declared in this |
| 1956 | /// context are semantically declared in the nearest enclosing |
| 1957 | /// non-transparent (opaque) context but are lexically declared in |
| 1958 | /// this context. For example, consider the enumerators of an |
| 1959 | /// enumeration type: |
| 1960 | /// @code |
| 1961 | /// enum E { |
| 1962 | /// Val1 |
| 1963 | /// }; |
| 1964 | /// @endcode |
| 1965 | /// Here, E is a transparent context, so its enumerator (Val1) will |
| 1966 | /// appear (semantically) that it is in the same context of E. |
| 1967 | /// Examples of transparent contexts include: enumerations (except for |
| 1968 | /// C++0x scoped enums), and C++ linkage specifications. |
| 1969 | bool isTransparentContext() const; |
| 1970 | |
| 1971 | /// Determines whether this context or some of its ancestors is a |
| 1972 | /// linkage specification context that specifies C linkage. |
| 1973 | bool isExternCContext() const; |
| 1974 | |
| 1975 | /// Retrieve the nearest enclosing C linkage specification context. |
| 1976 | const LinkageSpecDecl *getExternCContext() const; |
| 1977 | |
| 1978 | /// Determines whether this context or some of its ancestors is a |
| 1979 | /// linkage specification context that specifies C++ linkage. |
| 1980 | bool isExternCXXContext() const; |
| 1981 | |
| 1982 | /// Determine whether this declaration context is equivalent |
| 1983 | /// to the declaration context DC. |
| 1984 | bool Equals(const DeclContext *DC) const { |
| 1985 | return DC && this->getPrimaryContext() == DC->getPrimaryContext(); |
| 1986 | } |
| 1987 | |
| 1988 | /// Determine whether this declaration context encloses the |
| 1989 | /// declaration context DC. |
| 1990 | bool Encloses(const DeclContext *DC) const; |
| 1991 | |
| 1992 | /// Find the nearest non-closure ancestor of this context, |
| 1993 | /// i.e. the innermost semantic parent of this context which is not |
| 1994 | /// a closure. A context may be its own non-closure ancestor. |
| 1995 | Decl *getNonClosureAncestor(); |
| 1996 | const Decl *getNonClosureAncestor() const { |
| 1997 | return const_cast<DeclContext*>(this)->getNonClosureAncestor(); |
| 1998 | } |
| 1999 | |
| 2000 | /// getPrimaryContext - There may be many different |
| 2001 | /// declarations of the same entity (including forward declarations |
| 2002 | /// of classes, multiple definitions of namespaces, etc.), each with |
| 2003 | /// a different set of declarations. This routine returns the |
| 2004 | /// "primary" DeclContext structure, which will contain the |
| 2005 | /// information needed to perform name lookup into this context. |
| 2006 | DeclContext *getPrimaryContext(); |
| 2007 | const DeclContext *getPrimaryContext() const { |
| 2008 | return const_cast<DeclContext*>(this)->getPrimaryContext(); |
| 2009 | } |
| 2010 | |
| 2011 | /// getRedeclContext - Retrieve the context in which an entity conflicts with |
| 2012 | /// other entities of the same name, or where it is a redeclaration if the |
| 2013 | /// two entities are compatible. This skips through transparent contexts. |
| 2014 | DeclContext *getRedeclContext(); |
| 2015 | const DeclContext *getRedeclContext() const { |
| 2016 | return const_cast<DeclContext *>(this)->getRedeclContext(); |
| 2017 | } |
| 2018 | |
| 2019 | /// Retrieve the nearest enclosing namespace context. |
| 2020 | DeclContext *getEnclosingNamespaceContext(); |
| 2021 | const DeclContext *getEnclosingNamespaceContext() const { |
| 2022 | return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext(); |
| 2023 | } |
| 2024 | |
| 2025 | /// Retrieve the outermost lexically enclosing record context. |
| 2026 | RecordDecl *getOuterLexicalRecordContext(); |
| 2027 | const RecordDecl *getOuterLexicalRecordContext() const { |
| 2028 | return const_cast<DeclContext *>(this)->getOuterLexicalRecordContext(); |
| 2029 | } |
| 2030 | |
| 2031 | /// Test if this context is part of the enclosing namespace set of |
| 2032 | /// the context NS, as defined in C++0x [namespace.def]p9. If either context |
| 2033 | /// isn't a namespace, this is equivalent to Equals(). |
| 2034 | /// |
| 2035 | /// The enclosing namespace set of a namespace is the namespace and, if it is |
| 2036 | /// inline, its enclosing namespace, recursively. |
| 2037 | bool InEnclosingNamespaceSetOf(const DeclContext *NS) const; |
| 2038 | |
| 2039 | /// Collects all of the declaration contexts that are semantically |
| 2040 | /// connected to this declaration context. |
| 2041 | /// |
| 2042 | /// For declaration contexts that have multiple semantically connected but |
| 2043 | /// syntactically distinct contexts, such as C++ namespaces, this routine |
| 2044 | /// retrieves the complete set of such declaration contexts in source order. |
| 2045 | /// For example, given: |
| 2046 | /// |
| 2047 | /// \code |
| 2048 | /// namespace N { |
| 2049 | /// int x; |
| 2050 | /// } |
| 2051 | /// namespace N { |
| 2052 | /// int y; |
| 2053 | /// } |
| 2054 | /// \endcode |
| 2055 | /// |
| 2056 | /// The \c Contexts parameter will contain both definitions of N. |
| 2057 | /// |
| 2058 | /// \param Contexts Will be cleared and set to the set of declaration |
| 2059 | /// contexts that are semanticaly connected to this declaration context, |
| 2060 | /// in source order, including this context (which may be the only result, |
| 2061 | /// for non-namespace contexts). |
| 2062 | void collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts); |
| 2063 | |
| 2064 | /// decl_iterator - Iterates through the declarations stored |
| 2065 | /// within this context. |
| 2066 | class decl_iterator { |
| 2067 | /// Current - The current declaration. |
| 2068 | Decl *Current = nullptr; |
| 2069 | |
| 2070 | public: |
| 2071 | using value_type = Decl *; |
| 2072 | using reference = const value_type &; |
| 2073 | using pointer = const value_type *; |
| 2074 | using iterator_category = std::forward_iterator_tag; |
| 2075 | using difference_type = std::ptrdiff_t; |
| 2076 | |
| 2077 | decl_iterator() = default; |
| 2078 | explicit decl_iterator(Decl *C) : Current(C) {} |
| 2079 | |
| 2080 | reference operator*() const { return Current; } |
| 2081 | |
| 2082 | // This doesn't meet the iterator requirements, but it's convenient |
| 2083 | value_type operator->() const { return Current; } |
| 2084 | |
| 2085 | decl_iterator& operator++() { |
| 2086 | Current = Current->getNextDeclInContext(); |
| 2087 | return *this; |
| 2088 | } |
| 2089 | |
| 2090 | decl_iterator operator++(int) { |
| 2091 | decl_iterator tmp(*this); |
| 2092 | ++(*this); |
| 2093 | return tmp; |
| 2094 | } |
| 2095 | |
| 2096 | friend bool operator==(decl_iterator x, decl_iterator y) { |
| 2097 | return x.Current == y.Current; |
| 2098 | } |
| 2099 | |
| 2100 | friend bool operator!=(decl_iterator x, decl_iterator y) { |
| 2101 | return x.Current != y.Current; |
| 2102 | } |
| 2103 | }; |
| 2104 | |
| 2105 | using decl_range = llvm::iterator_range<decl_iterator>; |
| 2106 | |
| 2107 | /// decls_begin/decls_end - Iterate over the declarations stored in |
| 2108 | /// this context. |
| 2109 | decl_range decls() const { return decl_range(decls_begin(), decls_end()); } |
| 2110 | decl_iterator decls_begin() const; |
| 2111 | decl_iterator decls_end() const { return decl_iterator(); } |
| 2112 | bool decls_empty() const; |
| 2113 | |
| 2114 | /// noload_decls_begin/end - Iterate over the declarations stored in this |
| 2115 | /// context that are currently loaded; don't attempt to retrieve anything |
| 2116 | /// from an external source. |
| 2117 | decl_range noload_decls() const { |
| 2118 | return decl_range(noload_decls_begin(), noload_decls_end()); |
| 2119 | } |
| 2120 | decl_iterator noload_decls_begin() const { return decl_iterator(FirstDecl); } |
| 2121 | decl_iterator noload_decls_end() const { return decl_iterator(); } |
| 2122 | |
| 2123 | /// specific_decl_iterator - Iterates over a subrange of |
| 2124 | /// declarations stored in a DeclContext, providing only those that |
| 2125 | /// are of type SpecificDecl (or a class derived from it). This |
| 2126 | /// iterator is used, for example, to provide iteration over just |
| 2127 | /// the fields within a RecordDecl (with SpecificDecl = FieldDecl). |
| 2128 | template<typename SpecificDecl> |
| 2129 | class specific_decl_iterator { |
| 2130 | /// Current - The current, underlying declaration iterator, which |
| 2131 | /// will either be NULL or will point to a declaration of |
| 2132 | /// type SpecificDecl. |
| 2133 | DeclContext::decl_iterator Current; |
| 2134 | |
| 2135 | /// SkipToNextDecl - Advances the current position up to the next |
| 2136 | /// declaration of type SpecificDecl that also meets the criteria |
| 2137 | /// required by Acceptable. |
| 2138 | void SkipToNextDecl() { |
| 2139 | while (*Current && !isa<SpecificDecl>(*Current)) |
| 2140 | ++Current; |
| 2141 | } |
| 2142 | |
| 2143 | public: |
| 2144 | using value_type = SpecificDecl *; |
| 2145 | // TODO: Add reference and pointer types (with some appropriate proxy type) |
| 2146 | // if we ever have a need for them. |
| 2147 | using reference = void; |
| 2148 | using pointer = void; |
| 2149 | using difference_type = |
| 2150 | std::iterator_traits<DeclContext::decl_iterator>::difference_type; |
| 2151 | using iterator_category = std::forward_iterator_tag; |
| 2152 | |
| 2153 | specific_decl_iterator() = default; |
| 2154 | |
| 2155 | /// specific_decl_iterator - Construct a new iterator over a |
| 2156 | /// subset of the declarations the range [C, |
| 2157 | /// end-of-declarations). If A is non-NULL, it is a pointer to a |
| 2158 | /// member function of SpecificDecl that should return true for |
| 2159 | /// all of the SpecificDecl instances that will be in the subset |
| 2160 | /// of iterators. For example, if you want Objective-C instance |
| 2161 | /// methods, SpecificDecl will be ObjCMethodDecl and A will be |
| 2162 | /// &ObjCMethodDecl::isInstanceMethod. |
| 2163 | explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) { |
| 2164 | SkipToNextDecl(); |
| 2165 | } |
| 2166 | |
| 2167 | value_type operator*() const { return cast<SpecificDecl>(*Current); } |
| 2168 | |
| 2169 | // This doesn't meet the iterator requirements, but it's convenient |
| 2170 | value_type operator->() const { return **this; } |
| 2171 | |
| 2172 | specific_decl_iterator& operator++() { |
| 2173 | ++Current; |
| 2174 | SkipToNextDecl(); |
| 2175 | return *this; |
| 2176 | } |
| 2177 | |
| 2178 | specific_decl_iterator operator++(int) { |
| 2179 | specific_decl_iterator tmp(*this); |
| 2180 | ++(*this); |
| 2181 | return tmp; |
| 2182 | } |
| 2183 | |
| 2184 | friend bool operator==(const specific_decl_iterator& x, |
| 2185 | const specific_decl_iterator& y) { |
| 2186 | return x.Current == y.Current; |
| 2187 | } |
| 2188 | |
| 2189 | friend bool operator!=(const specific_decl_iterator& x, |
| 2190 | const specific_decl_iterator& y) { |
| 2191 | return x.Current != y.Current; |
| 2192 | } |
| 2193 | }; |
| 2194 | |
| 2195 | /// Iterates over a filtered subrange of declarations stored |
| 2196 | /// in a DeclContext. |
| 2197 | /// |
| 2198 | /// This iterator visits only those declarations that are of type |
| 2199 | /// SpecificDecl (or a class derived from it) and that meet some |
| 2200 | /// additional run-time criteria. This iterator is used, for |
| 2201 | /// example, to provide access to the instance methods within an |
| 2202 | /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and |
| 2203 | /// Acceptable = ObjCMethodDecl::isInstanceMethod). |
| 2204 | template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const> |
| 2205 | class filtered_decl_iterator { |
| 2206 | /// Current - The current, underlying declaration iterator, which |
| 2207 | /// will either be NULL or will point to a declaration of |
| 2208 | /// type SpecificDecl. |
| 2209 | DeclContext::decl_iterator Current; |
| 2210 | |
| 2211 | /// SkipToNextDecl - Advances the current position up to the next |
| 2212 | /// declaration of type SpecificDecl that also meets the criteria |
| 2213 | /// required by Acceptable. |
| 2214 | void SkipToNextDecl() { |
| 2215 | while (*Current && |
| 2216 | (!isa<SpecificDecl>(*Current) || |
| 2217 | (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) |
| 2218 | ++Current; |
| 2219 | } |
| 2220 | |
| 2221 | public: |
| 2222 | using value_type = SpecificDecl *; |
| 2223 | // TODO: Add reference and pointer types (with some appropriate proxy type) |
| 2224 | // if we ever have a need for them. |
| 2225 | using reference = void; |
| 2226 | using pointer = void; |
| 2227 | using difference_type = |
| 2228 | std::iterator_traits<DeclContext::decl_iterator>::difference_type; |
| 2229 | using iterator_category = std::forward_iterator_tag; |
| 2230 | |
| 2231 | filtered_decl_iterator() = default; |
| 2232 | |
| 2233 | /// filtered_decl_iterator - Construct a new iterator over a |
| 2234 | /// subset of the declarations the range [C, |
| 2235 | /// end-of-declarations). If A is non-NULL, it is a pointer to a |
| 2236 | /// member function of SpecificDecl that should return true for |
| 2237 | /// all of the SpecificDecl instances that will be in the subset |
| 2238 | /// of iterators. For example, if you want Objective-C instance |
| 2239 | /// methods, SpecificDecl will be ObjCMethodDecl and A will be |
| 2240 | /// &ObjCMethodDecl::isInstanceMethod. |
| 2241 | explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) { |
| 2242 | SkipToNextDecl(); |
| 2243 | } |
| 2244 | |
| 2245 | value_type operator*() const { return cast<SpecificDecl>(*Current); } |
| 2246 | value_type operator->() const { return cast<SpecificDecl>(*Current); } |
| 2247 | |
| 2248 | filtered_decl_iterator& operator++() { |
| 2249 | ++Current; |
| 2250 | SkipToNextDecl(); |
| 2251 | return *this; |
| 2252 | } |
| 2253 | |
| 2254 | filtered_decl_iterator operator++(int) { |
| 2255 | filtered_decl_iterator tmp(*this); |
| 2256 | ++(*this); |
| 2257 | return tmp; |
| 2258 | } |
| 2259 | |
| 2260 | friend bool operator==(const filtered_decl_iterator& x, |
| 2261 | const filtered_decl_iterator& y) { |
| 2262 | return x.Current == y.Current; |
| 2263 | } |
| 2264 | |
| 2265 | friend bool operator!=(const filtered_decl_iterator& x, |
| 2266 | const filtered_decl_iterator& y) { |
| 2267 | return x.Current != y.Current; |
| 2268 | } |
| 2269 | }; |
| 2270 | |
| 2271 | /// Add the declaration D into this context. |
| 2272 | /// |
| 2273 | /// This routine should be invoked when the declaration D has first |
| 2274 | /// been declared, to place D into the context where it was |
| 2275 | /// (lexically) defined. Every declaration must be added to one |
| 2276 | /// (and only one!) context, where it can be visited via |
| 2277 | /// [decls_begin(), decls_end()). Once a declaration has been added |
| 2278 | /// to its lexical context, the corresponding DeclContext owns the |
| 2279 | /// declaration. |
| 2280 | /// |
| 2281 | /// If D is also a NamedDecl, it will be made visible within its |
| 2282 | /// semantic context via makeDeclVisibleInContext. |
| 2283 | void addDecl(Decl *D); |
| 2284 | |
| 2285 | /// Add the declaration D into this context, but suppress |
| 2286 | /// searches for external declarations with the same name. |
| 2287 | /// |
| 2288 | /// Although analogous in function to addDecl, this removes an |
| 2289 | /// important check. This is only useful if the Decl is being |
| 2290 | /// added in response to an external search; in all other cases, |
| 2291 | /// addDecl() is the right function to use. |
| 2292 | /// See the ASTImporter for use cases. |
| 2293 | void addDeclInternal(Decl *D); |
| 2294 | |
| 2295 | /// Add the declaration D to this context without modifying |
| 2296 | /// any lookup tables. |
| 2297 | /// |
| 2298 | /// This is useful for some operations in dependent contexts where |
| 2299 | /// the semantic context might not be dependent; this basically |
| 2300 | /// only happens with friends. |
| 2301 | void addHiddenDecl(Decl *D); |
| 2302 | |
| 2303 | /// Removes a declaration from this context. |
| 2304 | void removeDecl(Decl *D); |
| 2305 | |
| 2306 | /// Checks whether a declaration is in this context. |
| 2307 | bool containsDecl(Decl *D) const; |
| 2308 | |
| 2309 | /// Checks whether a declaration is in this context. |
| 2310 | /// This also loads the Decls from the external source before the check. |
| 2311 | bool containsDeclAndLoad(Decl *D) const; |
| 2312 | |
| 2313 | using lookup_result = DeclContextLookupResult; |
| 2314 | using lookup_iterator = lookup_result::iterator; |
| 2315 | |
| 2316 | /// lookup - Find the declarations (if any) with the given Name in |
| 2317 | /// this context. Returns a range of iterators that contains all of |
| 2318 | /// the declarations with this name, with object, function, member, |
| 2319 | /// and enumerator names preceding any tag name. Note that this |
| 2320 | /// routine will not look into parent contexts. |
| 2321 | lookup_result lookup(DeclarationName Name) const; |
| 2322 | |
| 2323 | /// Find the declarations with the given name that are visible |
| 2324 | /// within this context; don't attempt to retrieve anything from an |
| 2325 | /// external source. |
| 2326 | lookup_result noload_lookup(DeclarationName Name); |
| 2327 | |
| 2328 | /// A simplistic name lookup mechanism that performs name lookup |
| 2329 | /// into this declaration context without consulting the external source. |
| 2330 | /// |
| 2331 | /// This function should almost never be used, because it subverts the |
| 2332 | /// usual relationship between a DeclContext and the external source. |
| 2333 | /// See the ASTImporter for the (few, but important) use cases. |
| 2334 | /// |
| 2335 | /// FIXME: This is very inefficient; replace uses of it with uses of |
| 2336 | /// noload_lookup. |
| 2337 | void localUncachedLookup(DeclarationName Name, |
| 2338 | SmallVectorImpl<NamedDecl *> &Results); |
| 2339 | |
| 2340 | /// Makes a declaration visible within this context. |
| 2341 | /// |
| 2342 | /// This routine makes the declaration D visible to name lookup |
| 2343 | /// within this context and, if this is a transparent context, |
| 2344 | /// within its parent contexts up to the first enclosing |
| 2345 | /// non-transparent context. Making a declaration visible within a |
| 2346 | /// context does not transfer ownership of a declaration, and a |
| 2347 | /// declaration can be visible in many contexts that aren't its |
| 2348 | /// lexical context. |
| 2349 | /// |
| 2350 | /// If D is a redeclaration of an existing declaration that is |
| 2351 | /// visible from this context, as determined by |
| 2352 | /// NamedDecl::declarationReplaces, the previous declaration will be |
| 2353 | /// replaced with D. |
| 2354 | void makeDeclVisibleInContext(NamedDecl *D); |
| 2355 | |
| 2356 | /// all_lookups_iterator - An iterator that provides a view over the results |
| 2357 | /// of looking up every possible name. |
| 2358 | class all_lookups_iterator; |
| 2359 | |
| 2360 | using lookups_range = llvm::iterator_range<all_lookups_iterator>; |
| 2361 | |
| 2362 | lookups_range lookups() const; |
| 2363 | // Like lookups(), but avoids loading external declarations. |
| 2364 | // If PreserveInternalState, avoids building lookup data structures too. |
| 2365 | lookups_range noload_lookups(bool PreserveInternalState) const; |
| 2366 | |
| 2367 | /// Iterators over all possible lookups within this context. |
| 2368 | all_lookups_iterator lookups_begin() const; |
| 2369 | all_lookups_iterator lookups_end() const; |
| 2370 | |
| 2371 | /// Iterators over all possible lookups within this context that are |
| 2372 | /// currently loaded; don't attempt to retrieve anything from an external |
| 2373 | /// source. |
| 2374 | all_lookups_iterator noload_lookups_begin() const; |
| 2375 | all_lookups_iterator noload_lookups_end() const; |
| 2376 | |
| 2377 | struct udir_iterator; |
| 2378 | |
| 2379 | using udir_iterator_base = |
| 2380 | llvm::iterator_adaptor_base<udir_iterator, lookup_iterator, |
| 2381 | typename lookup_iterator::iterator_category, |
| 2382 | UsingDirectiveDecl *>; |
| 2383 | |
| 2384 | struct udir_iterator : udir_iterator_base { |
| 2385 | udir_iterator(lookup_iterator I) : udir_iterator_base(I) {} |
| 2386 | |
| 2387 | UsingDirectiveDecl *operator*() const; |
| 2388 | }; |
| 2389 | |
| 2390 | using udir_range = llvm::iterator_range<udir_iterator>; |
| 2391 | |
| 2392 | udir_range using_directives() const; |
| 2393 | |
| 2394 | // These are all defined in DependentDiagnostic.h. |
| 2395 | class ddiag_iterator; |
| 2396 | |
| 2397 | using ddiag_range = llvm::iterator_range<DeclContext::ddiag_iterator>; |
| 2398 | |
| 2399 | inline ddiag_range ddiags() const; |
| 2400 | |
| 2401 | // Low-level accessors |
| 2402 | |
| 2403 | /// Mark that there are external lexical declarations that we need |
| 2404 | /// to include in our lookup table (and that are not available as external |
| 2405 | /// visible lookups). These extra lookup results will be found by walking |
| 2406 | /// the lexical declarations of this context. This should be used only if |
| 2407 | /// setHasExternalLexicalStorage() has been called on any decl context for |
| 2408 | /// which this is the primary context. |
| 2409 | void setMustBuildLookupTable() { |
| 2410 | assert(this == getPrimaryContext() &&((void)0) |
| 2411 | "should only be called on primary context")((void)0); |
| 2412 | DeclContextBits.HasLazyExternalLexicalLookups = true; |
| 2413 | } |
| 2414 | |
| 2415 | /// Retrieve the internal representation of the lookup structure. |
| 2416 | /// This may omit some names if we are lazily building the structure. |
| 2417 | StoredDeclsMap *getLookupPtr() const { return LookupPtr; } |
| 2418 | |
| 2419 | /// Ensure the lookup structure is fully-built and return it. |
| 2420 | StoredDeclsMap *buildLookup(); |
| 2421 | |
| 2422 | /// Whether this DeclContext has external storage containing |
| 2423 | /// additional declarations that are lexically in this context. |
| 2424 | bool hasExternalLexicalStorage() const { |
| 2425 | return DeclContextBits.ExternalLexicalStorage; |
| 2426 | } |
| 2427 | |
| 2428 | /// State whether this DeclContext has external storage for |
| 2429 | /// declarations lexically in this context. |
| 2430 | void setHasExternalLexicalStorage(bool ES = true) const { |
| 2431 | DeclContextBits.ExternalLexicalStorage = ES; |
| 2432 | } |
| 2433 | |
| 2434 | /// Whether this DeclContext has external storage containing |
| 2435 | /// additional declarations that are visible in this context. |
| 2436 | bool hasExternalVisibleStorage() const { |
| 2437 | return DeclContextBits.ExternalVisibleStorage; |
| 2438 | } |
| 2439 | |
| 2440 | /// State whether this DeclContext has external storage for |
| 2441 | /// declarations visible in this context. |
| 2442 | void setHasExternalVisibleStorage(bool ES = true) const { |
| 2443 | DeclContextBits.ExternalVisibleStorage = ES; |
| 2444 | if (ES && LookupPtr) |
| 2445 | DeclContextBits.NeedToReconcileExternalVisibleStorage = true; |
| 2446 | } |
| 2447 | |
| 2448 | /// Determine whether the given declaration is stored in the list of |
| 2449 | /// declarations lexically within this context. |
| 2450 | bool isDeclInLexicalTraversal(const Decl *D) const { |
| 2451 | return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl || |
| 2452 | D == LastDecl); |
| 2453 | } |
| 2454 | |
| 2455 | bool setUseQualifiedLookup(bool use = true) const { |
| 2456 | bool old_value = DeclContextBits.UseQualifiedLookup; |
| 2457 | DeclContextBits.UseQualifiedLookup = use; |
| 2458 | return old_value; |
| 2459 | } |
| 2460 | |
| 2461 | bool shouldUseQualifiedLookup() const { |
| 2462 | return DeclContextBits.UseQualifiedLookup; |
| 2463 | } |
| 2464 | |
| 2465 | static bool classof(const Decl *D); |
| 2466 | static bool classof(const DeclContext *D) { return true; } |
| 2467 | |
| 2468 | void dumpDeclContext() const; |
| 2469 | void dumpLookups() const; |
| 2470 | void dumpLookups(llvm::raw_ostream &OS, bool DumpDecls = false, |
| 2471 | bool Deserialize = false) const; |
| 2472 | |
| 2473 | private: |
| 2474 | /// Whether this declaration context has had externally visible |
| 2475 | /// storage added since the last lookup. In this case, \c LookupPtr's |
| 2476 | /// invariant may not hold and needs to be fixed before we perform |
| 2477 | /// another lookup. |
| 2478 | bool hasNeedToReconcileExternalVisibleStorage() const { |
| 2479 | return DeclContextBits.NeedToReconcileExternalVisibleStorage; |
| 2480 | } |
| 2481 | |
| 2482 | /// State that this declaration context has had externally visible |
| 2483 | /// storage added since the last lookup. In this case, \c LookupPtr's |
| 2484 | /// invariant may not hold and needs to be fixed before we perform |
| 2485 | /// another lookup. |
| 2486 | void setNeedToReconcileExternalVisibleStorage(bool Need = true) const { |
| 2487 | DeclContextBits.NeedToReconcileExternalVisibleStorage = Need; |
| 2488 | } |
| 2489 | |
| 2490 | /// If \c true, this context may have local lexical declarations |
| 2491 | /// that are missing from the lookup table. |
| 2492 | bool hasLazyLocalLexicalLookups() const { |
| 2493 | return DeclContextBits.HasLazyLocalLexicalLookups; |
| 2494 | } |
| 2495 | |
| 2496 | /// If \c true, this context may have local lexical declarations |
| 2497 | /// that are missing from the lookup table. |
| 2498 | void setHasLazyLocalLexicalLookups(bool HasLLLL = true) const { |
| 2499 | DeclContextBits.HasLazyLocalLexicalLookups = HasLLLL; |
| 2500 | } |
| 2501 | |
| 2502 | /// If \c true, the external source may have lexical declarations |
| 2503 | /// that are missing from the lookup table. |
| 2504 | bool hasLazyExternalLexicalLookups() const { |
| 2505 | return DeclContextBits.HasLazyExternalLexicalLookups; |
| 2506 | } |
| 2507 | |
| 2508 | /// If \c true, the external source may have lexical declarations |
| 2509 | /// that are missing from the lookup table. |
| 2510 | void setHasLazyExternalLexicalLookups(bool HasLELL = true) const { |
| 2511 | DeclContextBits.HasLazyExternalLexicalLookups = HasLELL; |
| 2512 | } |
| 2513 | |
| 2514 | void reconcileExternalVisibleStorage() const; |
| 2515 | bool LoadLexicalDeclsFromExternalStorage() const; |
| 2516 | |
| 2517 | /// Makes a declaration visible within this context, but |
| 2518 | /// suppresses searches for external declarations with the same |
| 2519 | /// name. |
| 2520 | /// |
| 2521 | /// Analogous to makeDeclVisibleInContext, but for the exclusive |
| 2522 | /// use of addDeclInternal(). |
| 2523 | void makeDeclVisibleInContextInternal(NamedDecl *D); |
| 2524 | |
| 2525 | StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const; |
| 2526 | |
| 2527 | void loadLazyLocalLexicalLookups(); |
| 2528 | void buildLookupImpl(DeclContext *DCtx, bool Internal); |
| 2529 | void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, |
| 2530 | bool Rediscoverable); |
| 2531 | void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal); |
| 2532 | }; |
| 2533 | |
| 2534 | inline bool Decl::isTemplateParameter() const { |
| 2535 | return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm || |
| 2536 | getKind() == TemplateTemplateParm; |
| 2537 | } |
| 2538 | |
| 2539 | // Specialization selected when ToTy is not a known subclass of DeclContext. |
| 2540 | template <class ToTy, |
| 2541 | bool IsKnownSubtype = ::std::is_base_of<DeclContext, ToTy>::value> |
| 2542 | struct cast_convert_decl_context { |
| 2543 | static const ToTy *doit(const DeclContext *Val) { |
| 2544 | return static_cast<const ToTy*>(Decl::castFromDeclContext(Val)); |
| 2545 | } |
| 2546 | |
| 2547 | static ToTy *doit(DeclContext *Val) { |
| 2548 | return static_cast<ToTy*>(Decl::castFromDeclContext(Val)); |
| 2549 | } |
| 2550 | }; |
| 2551 | |
| 2552 | // Specialization selected when ToTy is a known subclass of DeclContext. |
| 2553 | template <class ToTy> |
| 2554 | struct cast_convert_decl_context<ToTy, true> { |
| 2555 | static const ToTy *doit(const DeclContext *Val) { |
| 2556 | return static_cast<const ToTy*>(Val); |
| 2557 | } |
| 2558 | |
| 2559 | static ToTy *doit(DeclContext *Val) { |
| 2560 | return static_cast<ToTy*>(Val); |
| 2561 | } |
| 2562 | }; |
| 2563 | |
| 2564 | } // namespace clang |
| 2565 | |
| 2566 | namespace llvm { |
| 2567 | |
| 2568 | /// isa<T>(DeclContext*) |
| 2569 | template <typename To> |
| 2570 | struct isa_impl<To, ::clang::DeclContext> { |
| 2571 | static bool doit(const ::clang::DeclContext &Val) { |
| 2572 | return To::classofKind(Val.getDeclKind()); |
| 2573 | } |
| 2574 | }; |
| 2575 | |
| 2576 | /// cast<T>(DeclContext*) |
| 2577 | template<class ToTy> |
| 2578 | struct cast_convert_val<ToTy, |
| 2579 | const ::clang::DeclContext,const ::clang::DeclContext> { |
| 2580 | static const ToTy &doit(const ::clang::DeclContext &Val) { |
| 2581 | return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); |
| 2582 | } |
| 2583 | }; |
| 2584 | |
| 2585 | template<class ToTy> |
| 2586 | struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> { |
| 2587 | static ToTy &doit(::clang::DeclContext &Val) { |
| 2588 | return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); |
| 2589 | } |
| 2590 | }; |
| 2591 | |
| 2592 | template<class ToTy> |
| 2593 | struct cast_convert_val<ToTy, |
| 2594 | const ::clang::DeclContext*, const ::clang::DeclContext*> { |
| 2595 | static const ToTy *doit(const ::clang::DeclContext *Val) { |
| 2596 | return ::clang::cast_convert_decl_context<ToTy>::doit(Val); |
| 2597 | } |
| 2598 | }; |
| 2599 | |
| 2600 | template<class ToTy> |
| 2601 | struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> { |
| 2602 | static ToTy *doit(::clang::DeclContext *Val) { |
| 2603 | return ::clang::cast_convert_decl_context<ToTy>::doit(Val); |
| 2604 | } |
| 2605 | }; |
| 2606 | |
| 2607 | /// Implement cast_convert_val for Decl -> DeclContext conversions. |
| 2608 | template<class FromTy> |
| 2609 | struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { |
| 2610 | static ::clang::DeclContext &doit(const FromTy &Val) { |
| 2611 | return *FromTy::castToDeclContext(&Val); |
| 2612 | } |
| 2613 | }; |
| 2614 | |
| 2615 | template<class FromTy> |
| 2616 | struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { |
| 2617 | static ::clang::DeclContext *doit(const FromTy *Val) { |
| 2618 | return FromTy::castToDeclContext(Val); |
| 2619 | } |
| 2620 | }; |
| 2621 | |
| 2622 | template<class FromTy> |
| 2623 | struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { |
| 2624 | static const ::clang::DeclContext &doit(const FromTy &Val) { |
| 2625 | return *FromTy::castToDeclContext(&Val); |
| 2626 | } |
| 2627 | }; |
| 2628 | |
| 2629 | template<class FromTy> |
| 2630 | struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { |
| 2631 | static const ::clang::DeclContext *doit(const FromTy *Val) { |
| 2632 | return FromTy::castToDeclContext(Val); |
| 2633 | } |
| 2634 | }; |
| 2635 | |
| 2636 | } // namespace llvm |
| 2637 | |
| 2638 | #endif // LLVM_CLANG_AST_DECLBASE_H |
| 1 | //===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the SmallVector class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_ADT_SMALLVECTOR_H |
| 14 | #define LLVM_ADT_SMALLVECTOR_H |
| 15 | |
| 16 | #include "llvm/ADT/iterator_range.h" |
| 17 | #include "llvm/Support/Compiler.h" |
| 18 | #include "llvm/Support/ErrorHandling.h" |
| 19 | #include "llvm/Support/MemAlloc.h" |
| 20 | #include "llvm/Support/type_traits.h" |
| 21 | #include <algorithm> |
| 22 | #include <cassert> |
| 23 | #include <cstddef> |
| 24 | #include <cstdlib> |
| 25 | #include <cstring> |
| 26 | #include <functional> |
| 27 | #include <initializer_list> |
| 28 | #include <iterator> |
| 29 | #include <limits> |
| 30 | #include <memory> |
| 31 | #include <new> |
| 32 | #include <type_traits> |
| 33 | #include <utility> |
| 34 | |
| 35 | namespace llvm { |
| 36 | |
| 37 | /// This is all the stuff common to all SmallVectors. |
| 38 | /// |
| 39 | /// The template parameter specifies the type which should be used to hold the |
| 40 | /// Size and Capacity of the SmallVector, so it can be adjusted. |
| 41 | /// Using 32 bit size is desirable to shrink the size of the SmallVector. |
| 42 | /// Using 64 bit size is desirable for cases like SmallVector<char>, where a |
| 43 | /// 32 bit size would limit the vector to ~4GB. SmallVectors are used for |
| 44 | /// buffering bitcode output - which can exceed 4GB. |
| 45 | template <class Size_T> class SmallVectorBase { |
| 46 | protected: |
| 47 | void *BeginX; |
| 48 | Size_T Size = 0, Capacity; |
| 49 | |
| 50 | /// The maximum value of the Size_T used. |
| 51 | static constexpr size_t SizeTypeMax() { |
| 52 | return std::numeric_limits<Size_T>::max(); |
| 53 | } |
| 54 | |
| 55 | SmallVectorBase() = delete; |
| 56 | SmallVectorBase(void *FirstEl, size_t TotalCapacity) |
| 57 | : BeginX(FirstEl), Capacity(TotalCapacity) {} |
| 58 | |
| 59 | /// This is a helper for \a grow() that's out of line to reduce code |
| 60 | /// duplication. This function will report a fatal error if it can't grow at |
| 61 | /// least to \p MinSize. |
| 62 | void *mallocForGrow(size_t MinSize, size_t TSize, size_t &NewCapacity); |
| 63 | |
| 64 | /// This is an implementation of the grow() method which only works |
| 65 | /// on POD-like data types and is out of line to reduce code duplication. |
| 66 | /// This function will report a fatal error if it cannot increase capacity. |
| 67 | void grow_pod(void *FirstEl, size_t MinSize, size_t TSize); |
| 68 | |
| 69 | public: |
| 70 | size_t size() const { return Size; } |
| 71 | size_t capacity() const { return Capacity; } |
| 72 | |
| 73 | LLVM_NODISCARD[[clang::warn_unused_result]] bool empty() const { return !Size; } |
| 74 | |
| 75 | /// Set the array size to \p N, which the current array must have enough |
| 76 | /// capacity for. |
| 77 | /// |
| 78 | /// This does not construct or destroy any elements in the vector. |
| 79 | /// |
| 80 | /// Clients can use this in conjunction with capacity() to write past the end |
| 81 | /// of the buffer when they know that more elements are available, and only |
| 82 | /// update the size later. This avoids the cost of value initializing elements |
| 83 | /// which will only be overwritten. |
| 84 | void set_size(size_t N) { |
| 85 | assert(N <= capacity())((void)0); |
| 86 | Size = N; |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | template <class T> |
| 91 | using SmallVectorSizeType = |
| 92 | typename std::conditional<sizeof(T) < 4 && sizeof(void *) >= 8, uint64_t, |
| 93 | uint32_t>::type; |
| 94 | |
| 95 | /// Figure out the offset of the first element. |
| 96 | template <class T, typename = void> struct SmallVectorAlignmentAndSize { |
| 97 | alignas(SmallVectorBase<SmallVectorSizeType<T>>) char Base[sizeof( |
| 98 | SmallVectorBase<SmallVectorSizeType<T>>)]; |
| 99 | alignas(T) char FirstEl[sizeof(T)]; |
| 100 | }; |
| 101 | |
| 102 | /// This is the part of SmallVectorTemplateBase which does not depend on whether |
| 103 | /// the type T is a POD. The extra dummy template argument is used by ArrayRef |
| 104 | /// to avoid unnecessarily requiring T to be complete. |
| 105 | template <typename T, typename = void> |
| 106 | class SmallVectorTemplateCommon |
| 107 | : public SmallVectorBase<SmallVectorSizeType<T>> { |
| 108 | using Base = SmallVectorBase<SmallVectorSizeType<T>>; |
| 109 | |
| 110 | /// Find the address of the first element. For this pointer math to be valid |
| 111 | /// with small-size of 0 for T with lots of alignment, it's important that |
| 112 | /// SmallVectorStorage is properly-aligned even for small-size of 0. |
| 113 | void *getFirstEl() const { |
| 114 | return const_cast<void *>(reinterpret_cast<const void *>( |
| 115 | reinterpret_cast<const char *>(this) + |
| 116 | offsetof(SmallVectorAlignmentAndSize<T>, FirstEl)__builtin_offsetof(SmallVectorAlignmentAndSize<T>, FirstEl ))); |
| 117 | } |
| 118 | // Space after 'FirstEl' is clobbered, do not add any instance vars after it. |
| 119 | |
| 120 | protected: |
| 121 | SmallVectorTemplateCommon(size_t Size) : Base(getFirstEl(), Size) {} |
| 122 | |
| 123 | void grow_pod(size_t MinSize, size_t TSize) { |
| 124 | Base::grow_pod(getFirstEl(), MinSize, TSize); |
| 125 | } |
| 126 | |
| 127 | /// Return true if this is a smallvector which has not had dynamic |
| 128 | /// memory allocated for it. |
| 129 | bool isSmall() const { return this->BeginX == getFirstEl(); } |
| 130 | |
| 131 | /// Put this vector in a state of being small. |
| 132 | void resetToSmall() { |
| 133 | this->BeginX = getFirstEl(); |
| 134 | this->Size = this->Capacity = 0; // FIXME: Setting Capacity to 0 is suspect. |
| 135 | } |
| 136 | |
| 137 | /// Return true if V is an internal reference to the given range. |
| 138 | bool isReferenceToRange(const void *V, const void *First, const void *Last) const { |
| 139 | // Use std::less to avoid UB. |
| 140 | std::less<> LessThan; |
| 141 | return !LessThan(V, First) && LessThan(V, Last); |
| 142 | } |
| 143 | |
| 144 | /// Return true if V is an internal reference to this vector. |
| 145 | bool isReferenceToStorage(const void *V) const { |
| 146 | return isReferenceToRange(V, this->begin(), this->end()); |
| 147 | } |
| 148 | |
| 149 | /// Return true if First and Last form a valid (possibly empty) range in this |
| 150 | /// vector's storage. |
| 151 | bool isRangeInStorage(const void *First, const void *Last) const { |
| 152 | // Use std::less to avoid UB. |
| 153 | std::less<> LessThan; |
| 154 | return !LessThan(First, this->begin()) && !LessThan(Last, First) && |
| 155 | !LessThan(this->end(), Last); |
| 156 | } |
| 157 | |
| 158 | /// Return true unless Elt will be invalidated by resizing the vector to |
| 159 | /// NewSize. |
| 160 | bool isSafeToReferenceAfterResize(const void *Elt, size_t NewSize) { |
| 161 | // Past the end. |
| 162 | if (LLVM_LIKELY(!isReferenceToStorage(Elt))__builtin_expect((bool)(!isReferenceToStorage(Elt)), true)) |
| 163 | return true; |
| 164 | |
| 165 | // Return false if Elt will be destroyed by shrinking. |
| 166 | if (NewSize <= this->size()) |
| 167 | return Elt < this->begin() + NewSize; |
| 168 | |
| 169 | // Return false if we need to grow. |
| 170 | return NewSize <= this->capacity(); |
| 171 | } |
| 172 | |
| 173 | /// Check whether Elt will be invalidated by resizing the vector to NewSize. |
| 174 | void assertSafeToReferenceAfterResize(const void *Elt, size_t NewSize) { |
| 175 | assert(isSafeToReferenceAfterResize(Elt, NewSize) &&((void)0) |
| 176 | "Attempting to reference an element of the vector in an operation "((void)0) |
| 177 | "that invalidates it")((void)0); |
| 178 | } |
| 179 | |
| 180 | /// Check whether Elt will be invalidated by increasing the size of the |
| 181 | /// vector by N. |
| 182 | void assertSafeToAdd(const void *Elt, size_t N = 1) { |
| 183 | this->assertSafeToReferenceAfterResize(Elt, this->size() + N); |
| 184 | } |
| 185 | |
| 186 | /// Check whether any part of the range will be invalidated by clearing. |
| 187 | void assertSafeToReferenceAfterClear(const T *From, const T *To) { |
| 188 | if (From == To) |
| 189 | return; |
| 190 | this->assertSafeToReferenceAfterResize(From, 0); |
| 191 | this->assertSafeToReferenceAfterResize(To - 1, 0); |
| 192 | } |
| 193 | template < |
| 194 | class ItTy, |
| 195 | std::enable_if_t<!std::is_same<std::remove_const_t<ItTy>, T *>::value, |
| 196 | bool> = false> |
| 197 | void assertSafeToReferenceAfterClear(ItTy, ItTy) {} |
| 198 | |
| 199 | /// Check whether any part of the range will be invalidated by growing. |
| 200 | void assertSafeToAddRange(const T *From, const T *To) { |
| 201 | if (From == To) |
| 202 | return; |
| 203 | this->assertSafeToAdd(From, To - From); |
| 204 | this->assertSafeToAdd(To - 1, To - From); |
| 205 | } |
| 206 | template < |
| 207 | class ItTy, |
| 208 | std::enable_if_t<!std::is_same<std::remove_const_t<ItTy>, T *>::value, |
| 209 | bool> = false> |
| 210 | void assertSafeToAddRange(ItTy, ItTy) {} |
| 211 | |
| 212 | /// Reserve enough space to add one element, and return the updated element |
| 213 | /// pointer in case it was a reference to the storage. |
| 214 | template <class U> |
| 215 | static const T *reserveForParamAndGetAddressImpl(U *This, const T &Elt, |
| 216 | size_t N) { |
| 217 | size_t NewSize = This->size() + N; |
| 218 | if (LLVM_LIKELY(NewSize <= This->capacity())__builtin_expect((bool)(NewSize <= This->capacity()), true )) |
| 219 | return &Elt; |
| 220 | |
| 221 | bool ReferencesStorage = false; |
| 222 | int64_t Index = -1; |
| 223 | if (!U::TakesParamByValue) { |
| 224 | if (LLVM_UNLIKELY(This->isReferenceToStorage(&Elt))__builtin_expect((bool)(This->isReferenceToStorage(&Elt )), false)) { |
| 225 | ReferencesStorage = true; |
| 226 | Index = &Elt - This->begin(); |
| 227 | } |
| 228 | } |
| 229 | This->grow(NewSize); |
| 230 | return ReferencesStorage ? This->begin() + Index : &Elt; |
| 231 | } |
| 232 | |
| 233 | public: |
| 234 | using size_type = size_t; |
| 235 | using difference_type = ptrdiff_t; |
| 236 | using value_type = T; |
| 237 | using iterator = T *; |
| 238 | using const_iterator = const T *; |
| 239 | |
| 240 | using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
| 241 | using reverse_iterator = std::reverse_iterator<iterator>; |
| 242 | |
| 243 | using reference = T &; |
| 244 | using const_reference = const T &; |
| 245 | using pointer = T *; |
| 246 | using const_pointer = const T *; |
| 247 | |
| 248 | using Base::capacity; |
| 249 | using Base::empty; |
| 250 | using Base::size; |
| 251 | |
| 252 | // forward iterator creation methods. |
| 253 | iterator begin() { return (iterator)this->BeginX; } |
| 254 | const_iterator begin() const { return (const_iterator)this->BeginX; } |
| 255 | iterator end() { return begin() + size(); } |
| 256 | const_iterator end() const { return begin() + size(); } |
| 257 | |
| 258 | // reverse iterator creation methods. |
| 259 | reverse_iterator rbegin() { return reverse_iterator(end()); } |
| 260 | const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); } |
| 261 | reverse_iterator rend() { return reverse_iterator(begin()); } |
| 262 | const_reverse_iterator rend() const { return const_reverse_iterator(begin());} |
| 263 | |
| 264 | size_type size_in_bytes() const { return size() * sizeof(T); } |
| 265 | size_type max_size() const { |
| 266 | return std::min(this->SizeTypeMax(), size_type(-1) / sizeof(T)); |
| 267 | } |
| 268 | |
| 269 | size_t capacity_in_bytes() const { return capacity() * sizeof(T); } |
| 270 | |
| 271 | /// Return a pointer to the vector's buffer, even if empty(). |
| 272 | pointer data() { return pointer(begin()); } |
| 273 | /// Return a pointer to the vector's buffer, even if empty(). |
| 274 | const_pointer data() const { return const_pointer(begin()); } |
| 275 | |
| 276 | reference operator[](size_type idx) { |
| 277 | assert(idx < size())((void)0); |
| 278 | return begin()[idx]; |
| 279 | } |
| 280 | const_reference operator[](size_type idx) const { |
| 281 | assert(idx < size())((void)0); |
| 282 | return begin()[idx]; |
| 283 | } |
| 284 | |
| 285 | reference front() { |
| 286 | assert(!empty())((void)0); |
| 287 | return begin()[0]; |
| 288 | } |
| 289 | const_reference front() const { |
| 290 | assert(!empty())((void)0); |
| 291 | return begin()[0]; |
| 292 | } |
| 293 | |
| 294 | reference back() { |
| 295 | assert(!empty())((void)0); |
| 296 | return end()[-1]; |
| 297 | } |
| 298 | const_reference back() const { |
| 299 | assert(!empty())((void)0); |
| 300 | return end()[-1]; |
| 301 | } |
| 302 | }; |
| 303 | |
| 304 | /// SmallVectorTemplateBase<TriviallyCopyable = false> - This is where we put |
| 305 | /// method implementations that are designed to work with non-trivial T's. |
| 306 | /// |
| 307 | /// We approximate is_trivially_copyable with trivial move/copy construction and |
| 308 | /// trivial destruction. While the standard doesn't specify that you're allowed |
| 309 | /// copy these types with memcpy, there is no way for the type to observe this. |
| 310 | /// This catches the important case of std::pair<POD, POD>, which is not |
| 311 | /// trivially assignable. |
| 312 | template <typename T, bool = (is_trivially_copy_constructible<T>::value) && |
| 313 | (is_trivially_move_constructible<T>::value) && |
| 314 | std::is_trivially_destructible<T>::value> |
| 315 | class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> { |
| 316 | friend class SmallVectorTemplateCommon<T>; |
| 317 | |
| 318 | protected: |
| 319 | static constexpr bool TakesParamByValue = false; |
| 320 | using ValueParamT = const T &; |
| 321 | |
| 322 | SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {} |
| 323 | |
| 324 | static void destroy_range(T *S, T *E) { |
| 325 | while (S != E) { |
| 326 | --E; |
| 327 | E->~T(); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /// Move the range [I, E) into the uninitialized memory starting with "Dest", |
| 332 | /// constructing elements as needed. |
| 333 | template<typename It1, typename It2> |
| 334 | static void uninitialized_move(It1 I, It1 E, It2 Dest) { |
| 335 | std::uninitialized_copy(std::make_move_iterator(I), |
| 336 | std::make_move_iterator(E), Dest); |
| 337 | } |
| 338 | |
| 339 | /// Copy the range [I, E) onto the uninitialized memory starting with "Dest", |
| 340 | /// constructing elements as needed. |
| 341 | template<typename It1, typename It2> |
| 342 | static void uninitialized_copy(It1 I, It1 E, It2 Dest) { |
| 343 | std::uninitialized_copy(I, E, Dest); |
| 344 | } |
| 345 | |
| 346 | /// Grow the allocated memory (without initializing new elements), doubling |
| 347 | /// the size of the allocated memory. Guarantees space for at least one more |
| 348 | /// element, or MinSize more elements if specified. |
| 349 | void grow(size_t MinSize = 0); |
| 350 | |
| 351 | /// Create a new allocation big enough for \p MinSize and pass back its size |
| 352 | /// in \p NewCapacity. This is the first section of \a grow(). |
| 353 | T *mallocForGrow(size_t MinSize, size_t &NewCapacity) { |
| 354 | return static_cast<T *>( |
| 355 | SmallVectorBase<SmallVectorSizeType<T>>::mallocForGrow( |
| 356 | MinSize, sizeof(T), NewCapacity)); |
| 357 | } |
| 358 | |
| 359 | /// Move existing elements over to the new allocation \p NewElts, the middle |
| 360 | /// section of \a grow(). |
| 361 | void moveElementsForGrow(T *NewElts); |
| 362 | |
| 363 | /// Transfer ownership of the allocation, finishing up \a grow(). |
| 364 | void takeAllocationForGrow(T *NewElts, size_t NewCapacity); |
| 365 | |
| 366 | /// Reserve enough space to add one element, and return the updated element |
| 367 | /// pointer in case it was a reference to the storage. |
| 368 | const T *reserveForParamAndGetAddress(const T &Elt, size_t N = 1) { |
| 369 | return this->reserveForParamAndGetAddressImpl(this, Elt, N); |
| 370 | } |
| 371 | |
| 372 | /// Reserve enough space to add one element, and return the updated element |
| 373 | /// pointer in case it was a reference to the storage. |
| 374 | T *reserveForParamAndGetAddress(T &Elt, size_t N = 1) { |
| 375 | return const_cast<T *>( |
| 376 | this->reserveForParamAndGetAddressImpl(this, Elt, N)); |
| 377 | } |
| 378 | |
| 379 | static T &&forward_value_param(T &&V) { return std::move(V); } |
| 380 | static const T &forward_value_param(const T &V) { return V; } |
| 381 | |
| 382 | void growAndAssign(size_t NumElts, const T &Elt) { |
| 383 | // Grow manually in case Elt is an internal reference. |
| 384 | size_t NewCapacity; |
| 385 | T *NewElts = mallocForGrow(NumElts, NewCapacity); |
| 386 | std::uninitialized_fill_n(NewElts, NumElts, Elt); |
| 387 | this->destroy_range(this->begin(), this->end()); |
| 388 | takeAllocationForGrow(NewElts, NewCapacity); |
| 389 | this->set_size(NumElts); |
| 390 | } |
| 391 | |
| 392 | template <typename... ArgTypes> T &growAndEmplaceBack(ArgTypes &&... Args) { |
| 393 | // Grow manually in case one of Args is an internal reference. |
| 394 | size_t NewCapacity; |
| 395 | T *NewElts = mallocForGrow(0, NewCapacity); |
| 396 | ::new ((void *)(NewElts + this->size())) T(std::forward<ArgTypes>(Args)...); |
| 397 | moveElementsForGrow(NewElts); |
| 398 | takeAllocationForGrow(NewElts, NewCapacity); |
| 399 | this->set_size(this->size() + 1); |
| 400 | return this->back(); |
| 401 | } |
| 402 | |
| 403 | public: |
| 404 | void push_back(const T &Elt) { |
| 405 | const T *EltPtr = reserveForParamAndGetAddress(Elt); |
| 406 | ::new ((void *)this->end()) T(*EltPtr); |
| 407 | this->set_size(this->size() + 1); |
| 408 | } |
| 409 | |
| 410 | void push_back(T &&Elt) { |
| 411 | T *EltPtr = reserveForParamAndGetAddress(Elt); |
| 412 | ::new ((void *)this->end()) T(::std::move(*EltPtr)); |
| 413 | this->set_size(this->size() + 1); |
| 414 | } |
| 415 | |
| 416 | void pop_back() { |
| 417 | this->set_size(this->size() - 1); |
| 418 | this->end()->~T(); |
| 419 | } |
| 420 | }; |
| 421 | |
| 422 | // Define this out-of-line to dissuade the C++ compiler from inlining it. |
| 423 | template <typename T, bool TriviallyCopyable> |
| 424 | void SmallVectorTemplateBase<T, TriviallyCopyable>::grow(size_t MinSize) { |
| 425 | size_t NewCapacity; |
| 426 | T *NewElts = mallocForGrow(MinSize, NewCapacity); |
| 427 | moveElementsForGrow(NewElts); |
| 428 | takeAllocationForGrow(NewElts, NewCapacity); |
| 429 | } |
| 430 | |
| 431 | // Define this out-of-line to dissuade the C++ compiler from inlining it. |
| 432 | template <typename T, bool TriviallyCopyable> |
| 433 | void SmallVectorTemplateBase<T, TriviallyCopyable>::moveElementsForGrow( |
| 434 | T *NewElts) { |
| 435 | // Move the elements over. |
| 436 | this->uninitialized_move(this->begin(), this->end(), NewElts); |
| 437 | |
| 438 | // Destroy the original elements. |
| 439 | destroy_range(this->begin(), this->end()); |
| 440 | } |
| 441 | |
| 442 | // Define this out-of-line to dissuade the C++ compiler from inlining it. |
| 443 | template <typename T, bool TriviallyCopyable> |
| 444 | void SmallVectorTemplateBase<T, TriviallyCopyable>::takeAllocationForGrow( |
| 445 | T *NewElts, size_t NewCapacity) { |
| 446 | // If this wasn't grown from the inline copy, deallocate the old space. |
| 447 | if (!this->isSmall()) |
| 448 | free(this->begin()); |
| 449 | |
| 450 | this->BeginX = NewElts; |
| 451 | this->Capacity = NewCapacity; |
| 452 | } |
| 453 | |
| 454 | /// SmallVectorTemplateBase<TriviallyCopyable = true> - This is where we put |
| 455 | /// method implementations that are designed to work with trivially copyable |
| 456 | /// T's. This allows using memcpy in place of copy/move construction and |
| 457 | /// skipping destruction. |
| 458 | template <typename T> |
| 459 | class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> { |
| 460 | friend class SmallVectorTemplateCommon<T>; |
| 461 | |
| 462 | protected: |
| 463 | /// True if it's cheap enough to take parameters by value. Doing so avoids |
| 464 | /// overhead related to mitigations for reference invalidation. |
| 465 | static constexpr bool TakesParamByValue = sizeof(T) <= 2 * sizeof(void *); |
| 466 | |
| 467 | /// Either const T& or T, depending on whether it's cheap enough to take |
| 468 | /// parameters by value. |
| 469 | using ValueParamT = |
| 470 | typename std::conditional<TakesParamByValue, T, const T &>::type; |
| 471 | |
| 472 | SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {} |
| 473 | |
| 474 | // No need to do a destroy loop for POD's. |
| 475 | static void destroy_range(T *, T *) {} |
| 476 | |
| 477 | /// Move the range [I, E) onto the uninitialized memory |
| 478 | /// starting with "Dest", constructing elements into it as needed. |
| 479 | template<typename It1, typename It2> |
| 480 | static void uninitialized_move(It1 I, It1 E, It2 Dest) { |
| 481 | // Just do a copy. |
| 482 | uninitialized_copy(I, E, Dest); |
| 483 | } |
| 484 | |
| 485 | /// Copy the range [I, E) onto the uninitialized memory |
| 486 | /// starting with "Dest", constructing elements into it as needed. |
| 487 | template<typename It1, typename It2> |
| 488 | static void uninitialized_copy(It1 I, It1 E, It2 Dest) { |
| 489 | // Arbitrary iterator types; just use the basic implementation. |
| 490 | std::uninitialized_copy(I, E, Dest); |
| 491 | } |
| 492 | |
| 493 | /// Copy the range [I, E) onto the uninitialized memory |
| 494 | /// starting with "Dest", constructing elements into it as needed. |
| 495 | template <typename T1, typename T2> |
| 496 | static void uninitialized_copy( |
| 497 | T1 *I, T1 *E, T2 *Dest, |
| 498 | std::enable_if_t<std::is_same<typename std::remove_const<T1>::type, |
| 499 | T2>::value> * = nullptr) { |
| 500 | // Use memcpy for PODs iterated by pointers (which includes SmallVector |
| 501 | // iterators): std::uninitialized_copy optimizes to memmove, but we can |
| 502 | // use memcpy here. Note that I and E are iterators and thus might be |
| 503 | // invalid for memcpy if they are equal. |
| 504 | if (I != E) |
| 505 | memcpy(reinterpret_cast<void *>(Dest), I, (E - I) * sizeof(T)); |
| 506 | } |
| 507 | |
| 508 | /// Double the size of the allocated memory, guaranteeing space for at |
| 509 | /// least one more element or MinSize if specified. |
| 510 | void grow(size_t MinSize = 0) { this->grow_pod(MinSize, sizeof(T)); } |
| 511 | |
| 512 | /// Reserve enough space to add one element, and return the updated element |
| 513 | /// pointer in case it was a reference to the storage. |
| 514 | const T *reserveForParamAndGetAddress(const T &Elt, size_t N = 1) { |
| 515 | return this->reserveForParamAndGetAddressImpl(this, Elt, N); |
| 516 | } |
| 517 | |
| 518 | /// Reserve enough space to add one element, and return the updated element |
| 519 | /// pointer in case it was a reference to the storage. |
| 520 | T *reserveForParamAndGetAddress(T &Elt, size_t N = 1) { |
| 521 | return const_cast<T *>( |
| 522 | this->reserveForParamAndGetAddressImpl(this, Elt, N)); |
| 523 | } |
| 524 | |
| 525 | /// Copy \p V or return a reference, depending on \a ValueParamT. |
| 526 | static ValueParamT forward_value_param(ValueParamT V) { return V; } |
| 527 | |
| 528 | void growAndAssign(size_t NumElts, T Elt) { |
| 529 | // Elt has been copied in case it's an internal reference, side-stepping |
| 530 | // reference invalidation problems without losing the realloc optimization. |
| 531 | this->set_size(0); |
| 532 | this->grow(NumElts); |
| 533 | std::uninitialized_fill_n(this->begin(), NumElts, Elt); |
| 534 | this->set_size(NumElts); |
| 535 | } |
| 536 | |
| 537 | template <typename... ArgTypes> T &growAndEmplaceBack(ArgTypes &&... Args) { |
| 538 | // Use push_back with a copy in case Args has an internal reference, |
| 539 | // side-stepping reference invalidation problems without losing the realloc |
| 540 | // optimization. |
| 541 | push_back(T(std::forward<ArgTypes>(Args)...)); |
| 542 | return this->back(); |
| 543 | } |
| 544 | |
| 545 | public: |
| 546 | void push_back(ValueParamT Elt) { |
| 547 | const T *EltPtr = reserveForParamAndGetAddress(Elt); |
| 548 | memcpy(reinterpret_cast<void *>(this->end()), EltPtr, sizeof(T)); |
| 549 | this->set_size(this->size() + 1); |
| 550 | } |
| 551 | |
| 552 | void pop_back() { this->set_size(this->size() - 1); } |
| 553 | }; |
| 554 | |
| 555 | /// This class consists of common code factored out of the SmallVector class to |
| 556 | /// reduce code duplication based on the SmallVector 'N' template parameter. |
| 557 | template <typename T> |
| 558 | class SmallVectorImpl : public SmallVectorTemplateBase<T> { |
| 559 | using SuperClass = SmallVectorTemplateBase<T>; |
| 560 | |
| 561 | public: |
| 562 | using iterator = typename SuperClass::iterator; |
| 563 | using const_iterator = typename SuperClass::const_iterator; |
| 564 | using reference = typename SuperClass::reference; |
| 565 | using size_type = typename SuperClass::size_type; |
| 566 | |
| 567 | protected: |
| 568 | using SmallVectorTemplateBase<T>::TakesParamByValue; |
| 569 | using ValueParamT = typename SuperClass::ValueParamT; |
| 570 | |
| 571 | // Default ctor - Initialize to empty. |
| 572 | explicit SmallVectorImpl(unsigned N) |
| 573 | : SmallVectorTemplateBase<T>(N) {} |
| 574 | |
| 575 | public: |
| 576 | SmallVectorImpl(const SmallVectorImpl &) = delete; |
| 577 | |
| 578 | ~SmallVectorImpl() { |
| 579 | // Subclass has already destructed this vector's elements. |
| 580 | // If this wasn't grown from the inline copy, deallocate the old space. |
| 581 | if (!this->isSmall()) |
| 582 | free(this->begin()); |
| 583 | } |
| 584 | |
| 585 | void clear() { |
| 586 | this->destroy_range(this->begin(), this->end()); |
| 587 | this->Size = 0; |
| 588 | } |
| 589 | |
| 590 | private: |
| 591 | template <bool ForOverwrite> void resizeImpl(size_type N) { |
| 592 | if (N < this->size()) { |
| 593 | this->pop_back_n(this->size() - N); |
| 594 | } else if (N > this->size()) { |
| 595 | this->reserve(N); |
| 596 | for (auto I = this->end(), E = this->begin() + N; I != E; ++I) |
| 597 | if (ForOverwrite) |
| 598 | new (&*I) T; |
| 599 | else |
| 600 | new (&*I) T(); |
| 601 | this->set_size(N); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | public: |
| 606 | void resize(size_type N) { resizeImpl<false>(N); } |
| 607 | |
| 608 | /// Like resize, but \ref T is POD, the new values won't be initialized. |
| 609 | void resize_for_overwrite(size_type N) { resizeImpl<true>(N); } |
| 610 | |
| 611 | void resize(size_type N, ValueParamT NV) { |
| 612 | if (N == this->size()) |
| 613 | return; |
| 614 | |
| 615 | if (N < this->size()) { |
| 616 | this->pop_back_n(this->size() - N); |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | // N > this->size(). Defer to append. |
| 621 | this->append(N - this->size(), NV); |
| 622 | } |
| 623 | |
| 624 | void reserve(size_type N) { |
| 625 | if (this->capacity() < N) |
| 626 | this->grow(N); |
| 627 | } |
| 628 | |
| 629 | void pop_back_n(size_type NumItems) { |
| 630 | assert(this->size() >= NumItems)((void)0); |
| 631 | this->destroy_range(this->end() - NumItems, this->end()); |
| 632 | this->set_size(this->size() - NumItems); |
| 633 | } |
| 634 | |
| 635 | LLVM_NODISCARD[[clang::warn_unused_result]] T pop_back_val() { |
| 636 | T Result = ::std::move(this->back()); |
| 637 | this->pop_back(); |
| 638 | return Result; |
| 639 | } |
| 640 | |
| 641 | void swap(SmallVectorImpl &RHS); |
| 642 | |
| 643 | /// Add the specified range to the end of the SmallVector. |
| 644 | template <typename in_iter, |
| 645 | typename = std::enable_if_t<std::is_convertible< |
| 646 | typename std::iterator_traits<in_iter>::iterator_category, |
| 647 | std::input_iterator_tag>::value>> |
| 648 | void append(in_iter in_start, in_iter in_end) { |
| 649 | this->assertSafeToAddRange(in_start, in_end); |
| 650 | size_type NumInputs = std::distance(in_start, in_end); |
| 651 | this->reserve(this->size() + NumInputs); |
| 652 | this->uninitialized_copy(in_start, in_end, this->end()); |
| 653 | this->set_size(this->size() + NumInputs); |
| 654 | } |
| 655 | |
| 656 | /// Append \p NumInputs copies of \p Elt to the end. |
| 657 | void append(size_type NumInputs, ValueParamT Elt) { |
| 658 | const T *EltPtr = this->reserveForParamAndGetAddress(Elt, NumInputs); |
| 659 | std::uninitialized_fill_n(this->end(), NumInputs, *EltPtr); |
| 660 | this->set_size(this->size() + NumInputs); |
| 661 | } |
| 662 | |
| 663 | void append(std::initializer_list<T> IL) { |
| 664 | append(IL.begin(), IL.end()); |
| 665 | } |
| 666 | |
| 667 | void append(const SmallVectorImpl &RHS) { append(RHS.begin(), RHS.end()); } |
| 668 | |
| 669 | void assign(size_type NumElts, ValueParamT Elt) { |
| 670 | // Note that Elt could be an internal reference. |
| 671 | if (NumElts > this->capacity()) { |
| 672 | this->growAndAssign(NumElts, Elt); |
| 673 | return; |
| 674 | } |
| 675 | |
| 676 | // Assign over existing elements. |
| 677 | std::fill_n(this->begin(), std::min(NumElts, this->size()), Elt); |
| 678 | if (NumElts > this->size()) |
| 679 | std::uninitialized_fill_n(this->end(), NumElts - this->size(), Elt); |
| 680 | else if (NumElts < this->size()) |
| 681 | this->destroy_range(this->begin() + NumElts, this->end()); |
| 682 | this->set_size(NumElts); |
| 683 | } |
| 684 | |
| 685 | // FIXME: Consider assigning over existing elements, rather than clearing & |
| 686 | // re-initializing them - for all assign(...) variants. |
| 687 | |
| 688 | template <typename in_iter, |
| 689 | typename = std::enable_if_t<std::is_convertible< |
| 690 | typename std::iterator_traits<in_iter>::iterator_category, |
| 691 | std::input_iterator_tag>::value>> |
| 692 | void assign(in_iter in_start, in_iter in_end) { |
| 693 | this->assertSafeToReferenceAfterClear(in_start, in_end); |
| 694 | clear(); |
| 695 | append(in_start, in_end); |
| 696 | } |
| 697 | |
| 698 | void assign(std::initializer_list<T> IL) { |
| 699 | clear(); |
| 700 | append(IL); |
| 701 | } |
| 702 | |
| 703 | void assign(const SmallVectorImpl &RHS) { assign(RHS.begin(), RHS.end()); } |
| 704 | |
| 705 | iterator erase(const_iterator CI) { |
| 706 | // Just cast away constness because this is a non-const member function. |
| 707 | iterator I = const_cast<iterator>(CI); |
| 708 | |
| 709 | assert(this->isReferenceToStorage(CI) && "Iterator to erase is out of bounds.")((void)0); |
| 710 | |
| 711 | iterator N = I; |
| 712 | // Shift all elts down one. |
| 713 | std::move(I+1, this->end(), I); |
| 714 | // Drop the last elt. |
| 715 | this->pop_back(); |
| 716 | return(N); |
| 717 | } |
| 718 | |
| 719 | iterator erase(const_iterator CS, const_iterator CE) { |
| 720 | // Just cast away constness because this is a non-const member function. |
| 721 | iterator S = const_cast<iterator>(CS); |
| 722 | iterator E = const_cast<iterator>(CE); |
| 723 | |
| 724 | assert(this->isRangeInStorage(S, E) && "Range to erase is out of bounds.")((void)0); |
| 725 | |
| 726 | iterator N = S; |
| 727 | // Shift all elts down. |
| 728 | iterator I = std::move(E, this->end(), S); |
| 729 | // Drop the last elts. |
| 730 | this->destroy_range(I, this->end()); |
| 731 | this->set_size(I - this->begin()); |
| 732 | return(N); |
| 733 | } |
| 734 | |
| 735 | private: |
| 736 | template <class ArgType> iterator insert_one_impl(iterator I, ArgType &&Elt) { |
| 737 | // Callers ensure that ArgType is derived from T. |
| 738 | static_assert( |
| 739 | std::is_same<std::remove_const_t<std::remove_reference_t<ArgType>>, |
| 740 | T>::value, |
| 741 | "ArgType must be derived from T!"); |
| 742 | |
| 743 | if (I == this->end()) { // Important special case for empty vector. |
| 744 | this->push_back(::std::forward<ArgType>(Elt)); |
| 745 | return this->end()-1; |
| 746 | } |
| 747 | |
| 748 | assert(this->isReferenceToStorage(I) && "Insertion iterator is out of bounds.")((void)0); |
| 749 | |
| 750 | // Grow if necessary. |
| 751 | size_t Index = I - this->begin(); |
| 752 | std::remove_reference_t<ArgType> *EltPtr = |
| 753 | this->reserveForParamAndGetAddress(Elt); |
| 754 | I = this->begin() + Index; |
| 755 | |
| 756 | ::new ((void*) this->end()) T(::std::move(this->back())); |
| 757 | // Push everything else over. |
| 758 | std::move_backward(I, this->end()-1, this->end()); |
| 759 | this->set_size(this->size() + 1); |
| 760 | |
| 761 | // If we just moved the element we're inserting, be sure to update |
| 762 | // the reference (never happens if TakesParamByValue). |
| 763 | static_assert(!TakesParamByValue || std::is_same<ArgType, T>::value, |
| 764 | "ArgType must be 'T' when taking by value!"); |
| 765 | if (!TakesParamByValue && this->isReferenceToRange(EltPtr, I, this->end())) |
| 766 | ++EltPtr; |
| 767 | |
| 768 | *I = ::std::forward<ArgType>(*EltPtr); |
| 769 | return I; |
| 770 | } |
| 771 | |
| 772 | public: |
| 773 | iterator insert(iterator I, T &&Elt) { |
| 774 | return insert_one_impl(I, this->forward_value_param(std::move(Elt))); |
| 775 | } |
| 776 | |
| 777 | iterator insert(iterator I, const T &Elt) { |
| 778 | return insert_one_impl(I, this->forward_value_param(Elt)); |
| 779 | } |
| 780 | |
| 781 | iterator insert(iterator I, size_type NumToInsert, ValueParamT Elt) { |
| 782 | // Convert iterator to elt# to avoid invalidating iterator when we reserve() |
| 783 | size_t InsertElt = I - this->begin(); |
| 784 | |
| 785 | if (I == this->end()) { // Important special case for empty vector. |
| 786 | append(NumToInsert, Elt); |
| 787 | return this->begin()+InsertElt; |
| 788 | } |
| 789 | |
| 790 | assert(this->isReferenceToStorage(I) && "Insertion iterator is out of bounds.")((void)0); |
| 791 | |
| 792 | // Ensure there is enough space, and get the (maybe updated) address of |
| 793 | // Elt. |
| 794 | const T *EltPtr = this->reserveForParamAndGetAddress(Elt, NumToInsert); |
| 795 | |
| 796 | // Uninvalidate the iterator. |
| 797 | I = this->begin()+InsertElt; |
| 798 | |
| 799 | // If there are more elements between the insertion point and the end of the |
| 800 | // range than there are being inserted, we can use a simple approach to |
| 801 | // insertion. Since we already reserved space, we know that this won't |
| 802 | // reallocate the vector. |
| 803 | if (size_t(this->end()-I) >= NumToInsert) { |
| 804 | T *OldEnd = this->end(); |
| 805 | append(std::move_iterator<iterator>(this->end() - NumToInsert), |
| 806 | std::move_iterator<iterator>(this->end())); |
| 807 | |
| 808 | // Copy the existing elements that get replaced. |
| 809 | std::move_backward(I, OldEnd-NumToInsert, OldEnd); |
| 810 | |
| 811 | // If we just moved the element we're inserting, be sure to update |
| 812 | // the reference (never happens if TakesParamByValue). |
| 813 | if (!TakesParamByValue && I <= EltPtr && EltPtr < this->end()) |
| 814 | EltPtr += NumToInsert; |
| 815 | |
| 816 | std::fill_n(I, NumToInsert, *EltPtr); |
| 817 | return I; |
| 818 | } |
| 819 | |
| 820 | // Otherwise, we're inserting more elements than exist already, and we're |
| 821 | // not inserting at the end. |
| 822 | |
| 823 | // Move over the elements that we're about to overwrite. |
| 824 | T *OldEnd = this->end(); |
| 825 | this->set_size(this->size() + NumToInsert); |
| 826 | size_t NumOverwritten = OldEnd-I; |
| 827 | this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten); |
| 828 | |
| 829 | // If we just moved the element we're inserting, be sure to update |
| 830 | // the reference (never happens if TakesParamByValue). |
| 831 | if (!TakesParamByValue && I <= EltPtr && EltPtr < this->end()) |
| 832 | EltPtr += NumToInsert; |
| 833 | |
| 834 | // Replace the overwritten part. |
| 835 | std::fill_n(I, NumOverwritten, *EltPtr); |
| 836 | |
| 837 | // Insert the non-overwritten middle part. |
| 838 | std::uninitialized_fill_n(OldEnd, NumToInsert - NumOverwritten, *EltPtr); |
| 839 | return I; |
| 840 | } |
| 841 | |
| 842 | template <typename ItTy, |
| 843 | typename = std::enable_if_t<std::is_convertible< |
| 844 | typename std::iterator_traits<ItTy>::iterator_category, |
| 845 | std::input_iterator_tag>::value>> |
| 846 | iterator insert(iterator I, ItTy From, ItTy To) { |
| 847 | // Convert iterator to elt# to avoid invalidating iterator when we reserve() |
| 848 | size_t InsertElt = I - this->begin(); |
| 849 | |
| 850 | if (I == this->end()) { // Important special case for empty vector. |
| 851 | append(From, To); |
| 852 | return this->begin()+InsertElt; |
| 853 | } |
| 854 | |
| 855 | assert(this->isReferenceToStorage(I) && "Insertion iterator is out of bounds.")((void)0); |
| 856 | |
| 857 | // Check that the reserve that follows doesn't invalidate the iterators. |
| 858 | this->assertSafeToAddRange(From, To); |
| 859 | |
| 860 | size_t NumToInsert = std::distance(From, To); |
| 861 | |
| 862 | // Ensure there is enough space. |
| 863 | reserve(this->size() + NumToInsert); |
| 864 | |
| 865 | // Uninvalidate the iterator. |
| 866 | I = this->begin()+InsertElt; |
| 867 | |
| 868 | // If there are more elements between the insertion point and the end of the |
| 869 | // range than there are being inserted, we can use a simple approach to |
| 870 | // insertion. Since we already reserved space, we know that this won't |
| 871 | // reallocate the vector. |
| 872 | if (size_t(this->end()-I) >= NumToInsert) { |
| 873 | T *OldEnd = this->end(); |
| 874 | append(std::move_iterator<iterator>(this->end() - NumToInsert), |
| 875 | std::move_iterator<iterator>(this->end())); |
| 876 | |
| 877 | // Copy the existing elements that get replaced. |
| 878 | std::move_backward(I, OldEnd-NumToInsert, OldEnd); |
| 879 | |
| 880 | std::copy(From, To, I); |
| 881 | return I; |
| 882 | } |
| 883 | |
| 884 | // Otherwise, we're inserting more elements than exist already, and we're |
| 885 | // not inserting at the end. |
| 886 | |
| 887 | // Move over the elements that we're about to overwrite. |
| 888 | T *OldEnd = this->end(); |
| 889 | this->set_size(this->size() + NumToInsert); |
| 890 | size_t NumOverwritten = OldEnd-I; |
| 891 | this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten); |
| 892 | |
| 893 | // Replace the overwritten part. |
| 894 | for (T *J = I; NumOverwritten > 0; --NumOverwritten) { |
| 895 | *J = *From; |
| 896 | ++J; ++From; |
| 897 | } |
| 898 | |
| 899 | // Insert the non-overwritten middle part. |
| 900 | this->uninitialized_copy(From, To, OldEnd); |
| 901 | return I; |
| 902 | } |
| 903 | |
| 904 | void insert(iterator I, std::initializer_list<T> IL) { |
| 905 | insert(I, IL.begin(), IL.end()); |
| 906 | } |
| 907 | |
| 908 | template <typename... ArgTypes> reference emplace_back(ArgTypes &&... Args) { |
| 909 | if (LLVM_UNLIKELY(this->size() >= this->capacity())__builtin_expect((bool)(this->size() >= this->capacity ()), false)) |
| 910 | return this->growAndEmplaceBack(std::forward<ArgTypes>(Args)...); |
| 911 | |
| 912 | ::new ((void *)this->end()) T(std::forward<ArgTypes>(Args)...); |
| 913 | this->set_size(this->size() + 1); |
| 914 | return this->back(); |
| 915 | } |
| 916 | |
| 917 | SmallVectorImpl &operator=(const SmallVectorImpl &RHS); |
| 918 | |
| 919 | SmallVectorImpl &operator=(SmallVectorImpl &&RHS); |
| 920 | |
| 921 | bool operator==(const SmallVectorImpl &RHS) const { |
| 922 | if (this->size() != RHS.size()) return false; |
| 923 | return std::equal(this->begin(), this->end(), RHS.begin()); |
| 924 | } |
| 925 | bool operator!=(const SmallVectorImpl &RHS) const { |
| 926 | return !(*this == RHS); |
| 927 | } |
| 928 | |
| 929 | bool operator<(const SmallVectorImpl &RHS) const { |
| 930 | return std::lexicographical_compare(this->begin(), this->end(), |
| 931 | RHS.begin(), RHS.end()); |
| 932 | } |
| 933 | }; |
| 934 | |
| 935 | template <typename T> |
| 936 | void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) { |
| 937 | if (this == &RHS) return; |
| 938 | |
| 939 | // We can only avoid copying elements if neither vector is small. |
| 940 | if (!this->isSmall() && !RHS.isSmall()) { |
| 941 | std::swap(this->BeginX, RHS.BeginX); |
| 942 | std::swap(this->Size, RHS.Size); |
| 943 | std::swap(this->Capacity, RHS.Capacity); |
| 944 | return; |
| 945 | } |
| 946 | this->reserve(RHS.size()); |
| 947 | RHS.reserve(this->size()); |
| 948 | |
| 949 | // Swap the shared elements. |
| 950 | size_t NumShared = this->size(); |
| 951 | if (NumShared > RHS.size()) NumShared = RHS.size(); |
| 952 | for (size_type i = 0; i != NumShared; ++i) |
| 953 | std::swap((*this)[i], RHS[i]); |
| 954 | |
| 955 | // Copy over the extra elts. |
| 956 | if (this->size() > RHS.size()) { |
| 957 | size_t EltDiff = this->size() - RHS.size(); |
| 958 | this->uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end()); |
| 959 | RHS.set_size(RHS.size() + EltDiff); |
| 960 | this->destroy_range(this->begin()+NumShared, this->end()); |
| 961 | this->set_size(NumShared); |
| 962 | } else if (RHS.size() > this->size()) { |
| 963 | size_t EltDiff = RHS.size() - this->size(); |
| 964 | this->uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end()); |
| 965 | this->set_size(this->size() + EltDiff); |
| 966 | this->destroy_range(RHS.begin()+NumShared, RHS.end()); |
| 967 | RHS.set_size(NumShared); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | template <typename T> |
| 972 | SmallVectorImpl<T> &SmallVectorImpl<T>:: |
| 973 | operator=(const SmallVectorImpl<T> &RHS) { |
| 974 | // Avoid self-assignment. |
| 975 | if (this == &RHS) return *this; |
| 976 | |
| 977 | // If we already have sufficient space, assign the common elements, then |
| 978 | // destroy any excess. |
| 979 | size_t RHSSize = RHS.size(); |
| 980 | size_t CurSize = this->size(); |
| 981 | if (CurSize >= RHSSize) { |
| 982 | // Assign common elements. |
| 983 | iterator NewEnd; |
| 984 | if (RHSSize) |
| 985 | NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin()); |
| 986 | else |
| 987 | NewEnd = this->begin(); |
| 988 | |
| 989 | // Destroy excess elements. |
| 990 | this->destroy_range(NewEnd, this->end()); |
| 991 | |
| 992 | // Trim. |
| 993 | this->set_size(RHSSize); |
| 994 | return *this; |
| 995 | } |
| 996 | |
| 997 | // If we have to grow to have enough elements, destroy the current elements. |
| 998 | // This allows us to avoid copying them during the grow. |
| 999 | // FIXME: don't do this if they're efficiently moveable. |
| 1000 | if (this->capacity() < RHSSize) { |
| 1001 | // Destroy current elements. |
| 1002 | this->clear(); |
| 1003 | CurSize = 0; |
| 1004 | this->grow(RHSSize); |
| 1005 | } else if (CurSize) { |
| 1006 | // Otherwise, use assignment for the already-constructed elements. |
| 1007 | std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin()); |
| 1008 | } |
| 1009 | |
| 1010 | // Copy construct the new elements in place. |
| 1011 | this->uninitialized_copy(RHS.begin()+CurSize, RHS.end(), |
| 1012 | this->begin()+CurSize); |
| 1013 | |
| 1014 | // Set end. |
| 1015 | this->set_size(RHSSize); |
| 1016 | return *this; |
| 1017 | } |
| 1018 | |
| 1019 | template <typename T> |
| 1020 | SmallVectorImpl<T> &SmallVectorImpl<T>::operator=(SmallVectorImpl<T> &&RHS) { |
| 1021 | // Avoid self-assignment. |
| 1022 | if (this == &RHS) return *this; |
| 1023 | |
| 1024 | // If the RHS isn't small, clear this vector and then steal its buffer. |
| 1025 | if (!RHS.isSmall()) { |
| 1026 | this->destroy_range(this->begin(), this->end()); |
| 1027 | if (!this->isSmall()) free(this->begin()); |
| 1028 | this->BeginX = RHS.BeginX; |
| 1029 | this->Size = RHS.Size; |
| 1030 | this->Capacity = RHS.Capacity; |
| 1031 | RHS.resetToSmall(); |
| 1032 | return *this; |
| 1033 | } |
| 1034 | |
| 1035 | // If we already have sufficient space, assign the common elements, then |
| 1036 | // destroy any excess. |
| 1037 | size_t RHSSize = RHS.size(); |
| 1038 | size_t CurSize = this->size(); |
| 1039 | if (CurSize >= RHSSize) { |
| 1040 | // Assign common elements. |
| 1041 | iterator NewEnd = this->begin(); |
| 1042 | if (RHSSize) |
| 1043 | NewEnd = std::move(RHS.begin(), RHS.end(), NewEnd); |
| 1044 | |
| 1045 | // Destroy excess elements and trim the bounds. |
| 1046 | this->destroy_range(NewEnd, this->end()); |
| 1047 | this->set_size(RHSSize); |
| 1048 | |
| 1049 | // Clear the RHS. |
| 1050 | RHS.clear(); |
| 1051 | |
| 1052 | return *this; |
| 1053 | } |
| 1054 | |
| 1055 | // If we have to grow to have enough elements, destroy the current elements. |
| 1056 | // This allows us to avoid copying them during the grow. |
| 1057 | // FIXME: this may not actually make any sense if we can efficiently move |
| 1058 | // elements. |
| 1059 | if (this->capacity() < RHSSize) { |
| 1060 | // Destroy current elements. |
| 1061 | this->clear(); |
| 1062 | CurSize = 0; |
| 1063 | this->grow(RHSSize); |
| 1064 | } else if (CurSize) { |
| 1065 | // Otherwise, use assignment for the already-constructed elements. |
| 1066 | std::move(RHS.begin(), RHS.begin()+CurSize, this->begin()); |
| 1067 | } |
| 1068 | |
| 1069 | // Move-construct the new elements in place. |
| 1070 | this->uninitialized_move(RHS.begin()+CurSize, RHS.end(), |
| 1071 | this->begin()+CurSize); |
| 1072 | |
| 1073 | // Set end. |
| 1074 | this->set_size(RHSSize); |
| 1075 | |
| 1076 | RHS.clear(); |
| 1077 | return *this; |
| 1078 | } |
| 1079 | |
| 1080 | /// Storage for the SmallVector elements. This is specialized for the N=0 case |
| 1081 | /// to avoid allocating unnecessary storage. |
| 1082 | template <typename T, unsigned N> |
| 1083 | struct SmallVectorStorage { |
| 1084 | alignas(T) char InlineElts[N * sizeof(T)]; |
| 1085 | }; |
| 1086 | |
| 1087 | /// We need the storage to be properly aligned even for small-size of 0 so that |
| 1088 | /// the pointer math in \a SmallVectorTemplateCommon::getFirstEl() is |
| 1089 | /// well-defined. |
| 1090 | template <typename T> struct alignas(T) SmallVectorStorage<T, 0> {}; |
| 1091 | |
| 1092 | /// Forward declaration of SmallVector so that |
| 1093 | /// calculateSmallVectorDefaultInlinedElements can reference |
| 1094 | /// `sizeof(SmallVector<T, 0>)`. |
| 1095 | template <typename T, unsigned N> class LLVM_GSL_OWNER[[gsl::Owner]] SmallVector; |
| 1096 | |
| 1097 | /// Helper class for calculating the default number of inline elements for |
| 1098 | /// `SmallVector<T>`. |
| 1099 | /// |
| 1100 | /// This should be migrated to a constexpr function when our minimum |
| 1101 | /// compiler support is enough for multi-statement constexpr functions. |
| 1102 | template <typename T> struct CalculateSmallVectorDefaultInlinedElements { |
| 1103 | // Parameter controlling the default number of inlined elements |
| 1104 | // for `SmallVector<T>`. |
| 1105 | // |
| 1106 | // The default number of inlined elements ensures that |
| 1107 | // 1. There is at least one inlined element. |
| 1108 | // 2. `sizeof(SmallVector<T>) <= kPreferredSmallVectorSizeof` unless |
| 1109 | // it contradicts 1. |
| 1110 | static constexpr size_t kPreferredSmallVectorSizeof = 64; |
| 1111 | |
| 1112 | // static_assert that sizeof(T) is not "too big". |
| 1113 | // |
| 1114 | // Because our policy guarantees at least one inlined element, it is possible |
| 1115 | // for an arbitrarily large inlined element to allocate an arbitrarily large |
| 1116 | // amount of inline storage. We generally consider it an antipattern for a |
| 1117 | // SmallVector to allocate an excessive amount of inline storage, so we want |
| 1118 | // to call attention to these cases and make sure that users are making an |
| 1119 | // intentional decision if they request a lot of inline storage. |
| 1120 | // |
| 1121 | // We want this assertion to trigger in pathological cases, but otherwise |
| 1122 | // not be too easy to hit. To accomplish that, the cutoff is actually somewhat |
| 1123 | // larger than kPreferredSmallVectorSizeof (otherwise, |
| 1124 | // `SmallVector<SmallVector<T>>` would be one easy way to trip it, and that |
| 1125 | // pattern seems useful in practice). |
| 1126 | // |
| 1127 | // One wrinkle is that this assertion is in theory non-portable, since |
| 1128 | // sizeof(T) is in general platform-dependent. However, we don't expect this |
| 1129 | // to be much of an issue, because most LLVM development happens on 64-bit |
| 1130 | // hosts, and therefore sizeof(T) is expected to *decrease* when compiled for |
| 1131 | // 32-bit hosts, dodging the issue. The reverse situation, where development |
| 1132 | // happens on a 32-bit host and then fails due to sizeof(T) *increasing* on a |
| 1133 | // 64-bit host, is expected to be very rare. |
| 1134 | static_assert( |
| 1135 | sizeof(T) <= 256, |
| 1136 | "You are trying to use a default number of inlined elements for " |
| 1137 | "`SmallVector<T>` but `sizeof(T)` is really big! Please use an " |
| 1138 | "explicit number of inlined elements with `SmallVector<T, N>` to make " |
| 1139 | "sure you really want that much inline storage."); |
| 1140 | |
| 1141 | // Discount the size of the header itself when calculating the maximum inline |
| 1142 | // bytes. |
| 1143 | static constexpr size_t PreferredInlineBytes = |
| 1144 | kPreferredSmallVectorSizeof - sizeof(SmallVector<T, 0>); |
| 1145 | static constexpr size_t NumElementsThatFit = PreferredInlineBytes / sizeof(T); |
| 1146 | static constexpr size_t value = |
| 1147 | NumElementsThatFit == 0 ? 1 : NumElementsThatFit; |
| 1148 | }; |
| 1149 | |
| 1150 | /// This is a 'vector' (really, a variable-sized array), optimized |
| 1151 | /// for the case when the array is small. It contains some number of elements |
| 1152 | /// in-place, which allows it to avoid heap allocation when the actual number of |
| 1153 | /// elements is below that threshold. This allows normal "small" cases to be |
| 1154 | /// fast without losing generality for large inputs. |
| 1155 | /// |
| 1156 | /// \note |
| 1157 | /// In the absence of a well-motivated choice for the number of inlined |
| 1158 | /// elements \p N, it is recommended to use \c SmallVector<T> (that is, |
| 1159 | /// omitting the \p N). This will choose a default number of inlined elements |
| 1160 | /// reasonable for allocation on the stack (for example, trying to keep \c |
| 1161 | /// sizeof(SmallVector<T>) around 64 bytes). |
| 1162 | /// |
| 1163 | /// \warning This does not attempt to be exception safe. |
| 1164 | /// |
| 1165 | /// \see https://llvm.org/docs/ProgrammersManual.html#llvm-adt-smallvector-h |
| 1166 | template <typename T, |
| 1167 | unsigned N = CalculateSmallVectorDefaultInlinedElements<T>::value> |
| 1168 | class LLVM_GSL_OWNER[[gsl::Owner]] SmallVector : public SmallVectorImpl<T>, |
| 1169 | SmallVectorStorage<T, N> { |
| 1170 | public: |
| 1171 | SmallVector() : SmallVectorImpl<T>(N) {} |
| 1172 | |
| 1173 | ~SmallVector() { |
| 1174 | // Destroy the constructed elements in the vector. |
| 1175 | this->destroy_range(this->begin(), this->end()); |
| 1176 | } |
| 1177 | |
| 1178 | explicit SmallVector(size_t Size, const T &Value = T()) |
| 1179 | : SmallVectorImpl<T>(N) { |
| 1180 | this->assign(Size, Value); |
| 1181 | } |
| 1182 | |
| 1183 | template <typename ItTy, |
| 1184 | typename = std::enable_if_t<std::is_convertible< |
| 1185 | typename std::iterator_traits<ItTy>::iterator_category, |
| 1186 | std::input_iterator_tag>::value>> |
| 1187 | SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) { |
| 1188 | this->append(S, E); |
| 1189 | } |
| 1190 | |
| 1191 | template <typename RangeTy> |
| 1192 | explicit SmallVector(const iterator_range<RangeTy> &R) |
| 1193 | : SmallVectorImpl<T>(N) { |
| 1194 | this->append(R.begin(), R.end()); |
| 1195 | } |
| 1196 | |
| 1197 | SmallVector(std::initializer_list<T> IL) : SmallVectorImpl<T>(N) { |
| 1198 | this->assign(IL); |
| 1199 | } |
| 1200 | |
| 1201 | SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(N) { |
| 1202 | if (!RHS.empty()) |
| 1203 | SmallVectorImpl<T>::operator=(RHS); |
| 1204 | } |
| 1205 | |
| 1206 | SmallVector &operator=(const SmallVector &RHS) { |
| 1207 | SmallVectorImpl<T>::operator=(RHS); |
| 1208 | return *this; |
| 1209 | } |
| 1210 | |
| 1211 | SmallVector(SmallVector &&RHS) : SmallVectorImpl<T>(N) { |
| 1212 | if (!RHS.empty()) |
| 1213 | SmallVectorImpl<T>::operator=(::std::move(RHS)); |
| 1214 | } |
| 1215 | |
| 1216 | SmallVector(SmallVectorImpl<T> &&RHS) : SmallVectorImpl<T>(N) { |
| 1217 | if (!RHS.empty()) |
| 1218 | SmallVectorImpl<T>::operator=(::std::move(RHS)); |
| 1219 | } |
| 1220 | |
| 1221 | SmallVector &operator=(SmallVector &&RHS) { |
| 1222 | SmallVectorImpl<T>::operator=(::std::move(RHS)); |
| 1223 | return *this; |
| 1224 | } |
| 1225 | |
| 1226 | SmallVector &operator=(SmallVectorImpl<T> &&RHS) { |
| 1227 | SmallVectorImpl<T>::operator=(::std::move(RHS)); |
| 1228 | return *this; |
| 1229 | } |
| 1230 | |
| 1231 | SmallVector &operator=(std::initializer_list<T> IL) { |
| 1232 | this->assign(IL); |
| 1233 | return *this; |
| 1234 | } |
| 1235 | }; |
| 1236 | |
| 1237 | template <typename T, unsigned N> |
| 1238 | inline size_t capacity_in_bytes(const SmallVector<T, N> &X) { |
| 1239 | return X.capacity_in_bytes(); |
| 1240 | } |
| 1241 | |
| 1242 | /// Given a range of type R, iterate the entire range and return a |
| 1243 | /// SmallVector with elements of the vector. This is useful, for example, |
| 1244 | /// when you want to iterate a range and then sort the results. |
| 1245 | template <unsigned Size, typename R> |
| 1246 | SmallVector<typename std::remove_const<typename std::remove_reference< |
| 1247 | decltype(*std::begin(std::declval<R &>()))>::type>::type, |
| 1248 | Size> |
| 1249 | to_vector(R &&Range) { |
| 1250 | return {std::begin(Range), std::end(Range)}; |
| 1251 | } |
| 1252 | |
| 1253 | } // end namespace llvm |
| 1254 | |
| 1255 | namespace std { |
| 1256 | |
| 1257 | /// Implement std::swap in terms of SmallVector swap. |
| 1258 | template<typename T> |
| 1259 | inline void |
| 1260 | swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) { |
| 1261 | LHS.swap(RHS); |
| 1262 | } |
| 1263 | |
| 1264 | /// Implement std::swap in terms of SmallVector swap. |
| 1265 | template<typename T, unsigned N> |
| 1266 | inline void |
| 1267 | swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) { |
| 1268 | LHS.swap(RHS); |
| 1269 | } |
| 1270 | |
| 1271 | } // end namespace std |
| 1272 | |
| 1273 | #endif // LLVM_ADT_SMALLVECTOR_H |