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