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