DeclObjC.cpp revision 3060178ad9df29789505c1e6debcfc80a3a13587
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/Stmt.h" 17#include "llvm/ADT/STLExtras.h" 18using namespace clang; 19 20//===----------------------------------------------------------------------===// 21// ObjCListBase 22//===----------------------------------------------------------------------===// 23 24void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) { 25 List = 0; 26 if (Elts == 0) return; // Setting to an empty list is a noop. 27 28 29 List = new (Ctx) void*[Elts]; 30 NumElts = Elts; 31 memcpy(List, InList, sizeof(void*)*Elts); 32} 33 34void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts, 35 const SourceLocation *Locs, ASTContext &Ctx) { 36 if (Elts == 0) 37 return; 38 39 Locations = new (Ctx) SourceLocation[Elts]; 40 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts); 41 set(InList, Elts, Ctx); 42} 43 44//===----------------------------------------------------------------------===// 45// ObjCInterfaceDecl 46//===----------------------------------------------------------------------===// 47 48/// getIvarDecl - This method looks up an ivar in this ContextDecl. 49/// 50ObjCIvarDecl * 51ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const { 52 lookup_const_iterator Ivar, IvarEnd; 53 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) { 54 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar)) 55 return ivar; 56 } 57 return 0; 58} 59 60// Get the local instance/class method declared in this interface. 61ObjCMethodDecl * 62ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const { 63 // Since instance & class methods can have the same name, the loop below 64 // ensures we get the correct method. 65 // 66 // @interface Whatever 67 // - (int) class_method; 68 // + (float) class_method; 69 // @end 70 // 71 lookup_const_iterator Meth, MethEnd; 72 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) { 73 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth); 74 if (MD && MD->isInstanceMethod() == isInstance) 75 return MD; 76 } 77 return 0; 78} 79 80ObjCPropertyDecl * 81ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC, 82 IdentifierInfo *propertyID) { 83 84 DeclContext::lookup_const_iterator I, E; 85 llvm::tie(I, E) = DC->lookup(propertyID); 86 for ( ; I != E; ++I) 87 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I)) 88 return PD; 89 90 return 0; 91} 92 93/// FindPropertyDeclaration - Finds declaration of the property given its name 94/// in 'PropertyId' and returns it. It returns 0, if not found. 95ObjCPropertyDecl * 96ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const { 97 98 if (ObjCPropertyDecl *PD = 99 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId)) 100 return PD; 101 102 switch (getKind()) { 103 default: 104 break; 105 case Decl::ObjCProtocol: { 106 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this); 107 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), 108 E = PID->protocol_end(); I != E; ++I) 109 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) 110 return P; 111 break; 112 } 113 case Decl::ObjCInterface: { 114 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this); 115 // Look through categories. 116 for (ObjCCategoryDecl *Cat = OID->getCategoryList(); 117 Cat; Cat = Cat->getNextClassCategory()) 118 if (!Cat->IsClassExtension()) 119 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId)) 120 return P; 121 122 // Look through protocols. 123 for (ObjCInterfaceDecl::all_protocol_iterator 124 I = OID->all_referenced_protocol_begin(), 125 E = OID->all_referenced_protocol_end(); I != E; ++I) 126 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) 127 return P; 128 129 // Finally, check the super class. 130 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass()) 131 return superClass->FindPropertyDeclaration(PropertyId); 132 break; 133 } 134 case Decl::ObjCCategory: { 135 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this); 136 // Look through protocols. 137 if (!OCD->IsClassExtension()) 138 for (ObjCCategoryDecl::protocol_iterator 139 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I) 140 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) 141 return P; 142 143 break; 144 } 145 } 146 return 0; 147} 148 149/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property 150/// with name 'PropertyId' in the primary class; including those in protocols 151/// (direct or indirect) used by the primary class. 152/// 153ObjCPropertyDecl * 154ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass( 155 IdentifierInfo *PropertyId) const { 156 if (ExternallyCompleted) 157 LoadExternalDefinition(); 158 159 if (ObjCPropertyDecl *PD = 160 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId)) 161 return PD; 162 163 // Look through protocols. 164 for (ObjCInterfaceDecl::all_protocol_iterator 165 I = all_referenced_protocol_begin(), 166 E = all_referenced_protocol_end(); I != E; ++I) 167 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) 168 return P; 169 170 return 0; 171} 172 173void ObjCInterfaceDecl::mergeClassExtensionProtocolList( 174 ObjCProtocolDecl *const* ExtList, unsigned ExtNum, 175 ASTContext &C) 176{ 177 if (ExternallyCompleted) 178 LoadExternalDefinition(); 179 180 if (AllReferencedProtocols.empty() && ReferencedProtocols.empty()) { 181 AllReferencedProtocols.set(ExtList, ExtNum, C); 182 return; 183 } 184 185 // Check for duplicate protocol in class's protocol list. 186 // This is O(n*m). But it is extremely rare and number of protocols in 187 // class or its extension are very few. 188 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs; 189 for (unsigned i = 0; i < ExtNum; i++) { 190 bool protocolExists = false; 191 ObjCProtocolDecl *ProtoInExtension = ExtList[i]; 192 for (all_protocol_iterator 193 p = all_referenced_protocol_begin(), 194 e = all_referenced_protocol_end(); p != e; ++p) { 195 ObjCProtocolDecl *Proto = (*p); 196 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) { 197 protocolExists = true; 198 break; 199 } 200 } 201 // Do we want to warn on a protocol in extension class which 202 // already exist in the class? Probably not. 203 if (!protocolExists) 204 ProtocolRefs.push_back(ProtoInExtension); 205 } 206 207 if (ProtocolRefs.empty()) 208 return; 209 210 // Merge ProtocolRefs into class's protocol list; 211 for (all_protocol_iterator p = all_referenced_protocol_begin(), 212 e = all_referenced_protocol_end(); p != e; ++p) { 213 ProtocolRefs.push_back(*p); 214 } 215 216 AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(), C); 217} 218 219/// getFirstClassExtension - Find first class extension of the given class. 220ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const { 221 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl; 222 CDecl = CDecl->getNextClassCategory()) 223 if (CDecl->IsClassExtension()) 224 return CDecl; 225 return 0; 226} 227 228/// getNextClassCategory - Find next class extension in list of categories. 229const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const { 230 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl; 231 CDecl = CDecl->getNextClassCategory()) 232 if (CDecl->IsClassExtension()) 233 return CDecl; 234 return 0; 235} 236 237ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID, 238 ObjCInterfaceDecl *&clsDeclared) { 239 ObjCInterfaceDecl* ClassDecl = this; 240 while (ClassDecl != NULL) { 241 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) { 242 clsDeclared = ClassDecl; 243 return I; 244 } 245 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension(); 246 CDecl; CDecl = CDecl->getNextClassExtension()) { 247 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) { 248 clsDeclared = ClassDecl; 249 return I; 250 } 251 } 252 253 ClassDecl = ClassDecl->getSuperClass(); 254 } 255 return NULL; 256} 257 258/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super 259/// class whose name is passed as argument. If it is not one of the super classes 260/// the it returns NULL. 261ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass( 262 const IdentifierInfo*ICName) { 263 ObjCInterfaceDecl* ClassDecl = this; 264 while (ClassDecl != NULL) { 265 if (ClassDecl->getIdentifier() == ICName) 266 return ClassDecl; 267 ClassDecl = ClassDecl->getSuperClass(); 268 } 269 return NULL; 270} 271 272/// lookupMethod - This method returns an instance/class method by looking in 273/// the class, its categories, and its super classes (using a linear search). 274ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel, 275 bool isInstance) const { 276 const ObjCInterfaceDecl* ClassDecl = this; 277 ObjCMethodDecl *MethodDecl = 0; 278 279 if (ExternallyCompleted) 280 LoadExternalDefinition(); 281 282 while (ClassDecl != NULL) { 283 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance))) 284 return MethodDecl; 285 286 // Didn't find one yet - look through protocols. 287 const ObjCList<ObjCProtocolDecl> &Protocols = 288 ClassDecl->getReferencedProtocols(); 289 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 290 E = Protocols.end(); I != E; ++I) 291 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance))) 292 return MethodDecl; 293 294 // Didn't find one yet - now look through categories. 295 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); 296 while (CatDecl) { 297 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance))) 298 return MethodDecl; 299 300 // Didn't find one yet - look through protocols. 301 const ObjCList<ObjCProtocolDecl> &Protocols = 302 CatDecl->getReferencedProtocols(); 303 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 304 E = Protocols.end(); I != E; ++I) 305 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance))) 306 return MethodDecl; 307 CatDecl = CatDecl->getNextClassCategory(); 308 } 309 ClassDecl = ClassDecl->getSuperClass(); 310 } 311 return NULL; 312} 313 314ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod( 315 const Selector &Sel, 316 bool Instance) { 317 ObjCMethodDecl *Method = 0; 318 if (ObjCImplementationDecl *ImpDecl = getImplementation()) 319 Method = Instance ? ImpDecl->getInstanceMethod(Sel) 320 : ImpDecl->getClassMethod(Sel); 321 322 if (!Method && getSuperClass()) 323 return getSuperClass()->lookupPrivateMethod(Sel, Instance); 324 return Method; 325} 326 327//===----------------------------------------------------------------------===// 328// ObjCMethodDecl 329//===----------------------------------------------------------------------===// 330 331ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C, 332 SourceLocation beginLoc, 333 SourceLocation endLoc, 334 Selector SelInfo, QualType T, 335 TypeSourceInfo *ResultTInfo, 336 DeclContext *contextDecl, 337 bool isInstance, 338 bool isVariadic, 339 bool isSynthesized, 340 bool isImplicitlyDeclared, 341 bool isDefined, 342 ImplementationControl impControl, 343 bool HasRelatedResultType, 344 unsigned numSelectorArgs) { 345 return new (C) ObjCMethodDecl(beginLoc, endLoc, 346 SelInfo, T, ResultTInfo, contextDecl, 347 isInstance, 348 isVariadic, isSynthesized, isImplicitlyDeclared, 349 isDefined, 350 impControl, 351 HasRelatedResultType, 352 numSelectorArgs); 353} 354 355/// \brief A definition will return its interface declaration. 356/// An interface declaration will return its definition. 357/// Otherwise it will return itself. 358ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() { 359 ASTContext &Ctx = getASTContext(); 360 ObjCMethodDecl *Redecl = 0; 361 Decl *CtxD = cast<Decl>(getDeclContext()); 362 363 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) { 364 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD)) 365 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod()); 366 367 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) { 368 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD)) 369 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod()); 370 371 } else if (ObjCImplementationDecl *ImplD = 372 dyn_cast<ObjCImplementationDecl>(CtxD)) { 373 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) 374 Redecl = IFD->getMethod(getSelector(), isInstanceMethod()); 375 376 } else if (ObjCCategoryImplDecl *CImplD = 377 dyn_cast<ObjCCategoryImplDecl>(CtxD)) { 378 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl()) 379 Redecl = CatD->getMethod(getSelector(), isInstanceMethod()); 380 } 381 382 return Redecl ? Redecl : this; 383} 384 385ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() { 386 Decl *CtxD = cast<Decl>(getDeclContext()); 387 388 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) { 389 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) 390 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(), 391 isInstanceMethod())) 392 return MD; 393 394 } else if (ObjCCategoryImplDecl *CImplD = 395 dyn_cast<ObjCCategoryImplDecl>(CtxD)) { 396 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl()) 397 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(), 398 isInstanceMethod())) 399 return MD; 400 } 401 402 return this; 403} 404 405ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const { 406 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family); 407 if (family != static_cast<unsigned>(InvalidObjCMethodFamily)) 408 return family; 409 410 // Check for an explicit attribute. 411 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) { 412 // The unfortunate necessity of mapping between enums here is due 413 // to the attributes framework. 414 switch (attr->getFamily()) { 415 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break; 416 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break; 417 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break; 418 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break; 419 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break; 420 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break; 421 } 422 Family = static_cast<unsigned>(family); 423 return family; 424 } 425 426 family = getSelector().getMethodFamily(); 427 switch (family) { 428 case OMF_None: break; 429 430 // init only has a conventional meaning for an instance method, and 431 // it has to return an object. 432 case OMF_init: 433 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType()) 434 family = OMF_None; 435 break; 436 437 // alloc/copy/new have a conventional meaning for both class and 438 // instance methods, but they require an object return. 439 case OMF_alloc: 440 case OMF_copy: 441 case OMF_mutableCopy: 442 case OMF_new: 443 if (!getResultType()->isObjCObjectPointerType()) 444 family = OMF_None; 445 break; 446 447 // These selectors have a conventional meaning only for instance methods. 448 case OMF_dealloc: 449 case OMF_retain: 450 case OMF_release: 451 case OMF_autorelease: 452 case OMF_retainCount: 453 case OMF_self: 454 if (!isInstanceMethod()) 455 family = OMF_None; 456 break; 457 458 case OMF_performSelector: 459 if (!isInstanceMethod() || 460 !getResultType()->isObjCIdType()) 461 family = OMF_None; 462 else { 463 unsigned noParams = param_size(); 464 if (noParams < 1 || noParams > 3) 465 family = OMF_None; 466 else { 467 ObjCMethodDecl::arg_type_iterator it = arg_type_begin(); 468 QualType ArgT = (*it); 469 if (!ArgT->isObjCSelType()) { 470 family = OMF_None; 471 break; 472 } 473 while (--noParams) { 474 it++; 475 ArgT = (*it); 476 if (!ArgT->isObjCIdType()) { 477 family = OMF_None; 478 break; 479 } 480 } 481 } 482 } 483 break; 484 485 } 486 487 // Cache the result. 488 Family = static_cast<unsigned>(family); 489 return family; 490} 491 492void ObjCMethodDecl::createImplicitParams(ASTContext &Context, 493 const ObjCInterfaceDecl *OID) { 494 QualType selfTy; 495 if (isInstanceMethod()) { 496 // There may be no interface context due to error in declaration 497 // of the interface (which has been reported). Recover gracefully. 498 if (OID) { 499 selfTy = Context.getObjCInterfaceType(OID); 500 selfTy = Context.getObjCObjectPointerType(selfTy); 501 } else { 502 selfTy = Context.getObjCIdType(); 503 } 504 } else // we have a factory method. 505 selfTy = Context.getObjCClassType(); 506 507 bool selfIsPseudoStrong = false; 508 bool selfIsConsumed = false; 509 if (isInstanceMethod() && Context.getLangOptions().ObjCAutoRefCount) { 510 selfIsConsumed = hasAttr<NSConsumesSelfAttr>(); 511 512 // 'self' is always __strong. It's actually pseudo-strong except 513 // in init methods, though. 514 Qualifiers qs; 515 qs.setObjCLifetime(Qualifiers::OCL_Strong); 516 selfTy = Context.getQualifiedType(selfTy, qs); 517 518 // In addition, 'self' is const unless this is an init method. 519 if (getMethodFamily() != OMF_init) { 520 selfTy = selfTy.withConst(); 521 selfIsPseudoStrong = true; 522 } 523 } 524 525 ImplicitParamDecl *self 526 = ImplicitParamDecl::Create(Context, this, SourceLocation(), 527 &Context.Idents.get("self"), selfTy); 528 setSelfDecl(self); 529 530 if (selfIsConsumed) 531 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context)); 532 533 if (selfIsPseudoStrong) 534 self->setARCPseudoStrong(true); 535 536 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(), 537 &Context.Idents.get("_cmd"), 538 Context.getObjCSelType())); 539} 540 541ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() { 542 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext())) 543 return ID; 544 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext())) 545 return CD->getClassInterface(); 546 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext())) 547 return IMD->getClassInterface(); 548 549 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method"); 550 assert(false && "unknown method context"); 551 return 0; 552} 553 554//===----------------------------------------------------------------------===// 555// ObjCInterfaceDecl 556//===----------------------------------------------------------------------===// 557 558ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C, 559 DeclContext *DC, 560 SourceLocation atLoc, 561 IdentifierInfo *Id, 562 SourceLocation ClassLoc, 563 bool ForwardDecl, bool isInternal){ 564 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl, 565 isInternal); 566} 567 568ObjCInterfaceDecl:: 569ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id, 570 SourceLocation CLoc, bool FD, bool isInternal) 571 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id), 572 TypeForDecl(0), SuperClass(0), 573 CategoryList(0), IvarList(0), 574 ForwardDecl(FD), InternalInterface(isInternal), ExternallyCompleted(false), 575 ClassLoc(CLoc) { 576} 577 578void ObjCInterfaceDecl::LoadExternalDefinition() const { 579 assert(ExternallyCompleted && "Class is not externally completed"); 580 ExternallyCompleted = false; 581 getASTContext().getExternalSource()->CompleteType( 582 const_cast<ObjCInterfaceDecl *>(this)); 583} 584 585void ObjCInterfaceDecl::setExternallyCompleted() { 586 assert(getASTContext().getExternalSource() && 587 "Class can't be externally completed without an external source"); 588 assert(!ForwardDecl && 589 "Forward declarations can't be externally completed"); 590 ExternallyCompleted = true; 591} 592 593ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const { 594 if (ExternallyCompleted) 595 LoadExternalDefinition(); 596 597 return getASTContext().getObjCImplementation( 598 const_cast<ObjCInterfaceDecl*>(this)); 599} 600 601void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) { 602 getASTContext().setObjCImplementation(this, ImplD); 603} 604 605/// all_declared_ivar_begin - return first ivar declared in this class, 606/// its extensions and its implementation. Lazily build the list on first 607/// access. 608ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() { 609 if (IvarList) 610 return IvarList; 611 612 ObjCIvarDecl *curIvar = 0; 613 if (!ivar_empty()) { 614 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end(); 615 IvarList = (*I); ++I; 616 for (curIvar = IvarList; I != E; curIvar = *I, ++I) 617 curIvar->setNextIvar(*I); 618 } 619 620 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl; 621 CDecl = CDecl->getNextClassExtension()) { 622 if (!CDecl->ivar_empty()) { 623 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(), 624 E = CDecl->ivar_end(); 625 if (!IvarList) { 626 IvarList = (*I); ++I; 627 curIvar = IvarList; 628 } 629 for ( ;I != E; curIvar = *I, ++I) 630 curIvar->setNextIvar(*I); 631 } 632 } 633 634 if (ObjCImplementationDecl *ImplDecl = getImplementation()) { 635 if (!ImplDecl->ivar_empty()) { 636 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(), 637 E = ImplDecl->ivar_end(); 638 if (!IvarList) { 639 IvarList = (*I); ++I; 640 curIvar = IvarList; 641 } 642 for ( ;I != E; curIvar = *I, ++I) 643 curIvar->setNextIvar(*I); 644 } 645 } 646 return IvarList; 647} 648 649/// FindCategoryDeclaration - Finds category declaration in the list of 650/// categories for this class and returns it. Name of the category is passed 651/// in 'CategoryId'. If category not found, return 0; 652/// 653ObjCCategoryDecl * 654ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const { 655 if (ExternallyCompleted) 656 LoadExternalDefinition(); 657 658 for (ObjCCategoryDecl *Category = getCategoryList(); 659 Category; Category = Category->getNextClassCategory()) 660 if (Category->getIdentifier() == CategoryId) 661 return Category; 662 return 0; 663} 664 665ObjCMethodDecl * 666ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const { 667 for (ObjCCategoryDecl *Category = getCategoryList(); 668 Category; Category = Category->getNextClassCategory()) 669 if (ObjCCategoryImplDecl *Impl = Category->getImplementation()) 670 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel)) 671 return MD; 672 return 0; 673} 674 675ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const { 676 for (ObjCCategoryDecl *Category = getCategoryList(); 677 Category; Category = Category->getNextClassCategory()) 678 if (ObjCCategoryImplDecl *Impl = Category->getImplementation()) 679 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel)) 680 return MD; 681 return 0; 682} 683 684/// ClassImplementsProtocol - Checks that 'lProto' protocol 685/// has been implemented in IDecl class, its super class or categories (if 686/// lookupCategory is true). 687bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto, 688 bool lookupCategory, 689 bool RHSIsQualifiedID) { 690 ObjCInterfaceDecl *IDecl = this; 691 // 1st, look up the class. 692 const ObjCList<ObjCProtocolDecl> &Protocols = 693 IDecl->getReferencedProtocols(); 694 695 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(), 696 E = Protocols.end(); PI != E; ++PI) { 697 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI)) 698 return true; 699 // This is dubious and is added to be compatible with gcc. In gcc, it is 700 // also allowed assigning a protocol-qualified 'id' type to a LHS object 701 // when protocol in qualified LHS is in list of protocols in the rhs 'id' 702 // object. This IMO, should be a bug. 703 // FIXME: Treat this as an extension, and flag this as an error when GCC 704 // extensions are not enabled. 705 if (RHSIsQualifiedID && 706 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto)) 707 return true; 708 } 709 710 // 2nd, look up the category. 711 if (lookupCategory) 712 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl; 713 CDecl = CDecl->getNextClassCategory()) { 714 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(), 715 E = CDecl->protocol_end(); PI != E; ++PI) 716 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI)) 717 return true; 718 } 719 720 // 3rd, look up the super class(s) 721 if (IDecl->getSuperClass()) 722 return 723 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory, 724 RHSIsQualifiedID); 725 726 return false; 727} 728 729//===----------------------------------------------------------------------===// 730// ObjCIvarDecl 731//===----------------------------------------------------------------------===// 732 733ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC, 734 SourceLocation StartLoc, 735 SourceLocation IdLoc, IdentifierInfo *Id, 736 QualType T, TypeSourceInfo *TInfo, 737 AccessControl ac, Expr *BW, 738 bool synthesized) { 739 if (DC) { 740 // Ivar's can only appear in interfaces, implementations (via synthesized 741 // properties), and class extensions (via direct declaration, or synthesized 742 // properties). 743 // 744 // FIXME: This should really be asserting this: 745 // (isa<ObjCCategoryDecl>(DC) && 746 // cast<ObjCCategoryDecl>(DC)->IsClassExtension())) 747 // but unfortunately we sometimes place ivars into non-class extension 748 // categories on error. This breaks an AST invariant, and should not be 749 // fixed. 750 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) || 751 isa<ObjCCategoryDecl>(DC)) && 752 "Invalid ivar decl context!"); 753 // Once a new ivar is created in any of class/class-extension/implementation 754 // decl contexts, the previously built IvarList must be rebuilt. 755 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC); 756 if (!ID) { 757 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) { 758 ID = IM->getClassInterface(); 759 if (BW) 760 IM->setHasSynthBitfield(true); 761 } else { 762 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC); 763 ID = CD->getClassInterface(); 764 if (BW) 765 CD->setHasSynthBitfield(true); 766 } 767 } 768 ID->setIvarList(0); 769 } 770 771 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, 772 ac, BW, synthesized); 773} 774 775const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const { 776 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext()); 777 778 switch (DC->getKind()) { 779 default: 780 case ObjCCategoryImpl: 781 case ObjCProtocol: 782 assert(0 && "invalid ivar container!"); 783 return 0; 784 785 // Ivars can only appear in class extension categories. 786 case ObjCCategory: { 787 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC); 788 assert(CD->IsClassExtension() && "invalid container for ivar!"); 789 return CD->getClassInterface(); 790 } 791 792 case ObjCImplementation: 793 return cast<ObjCImplementationDecl>(DC)->getClassInterface(); 794 795 case ObjCInterface: 796 return cast<ObjCInterfaceDecl>(DC); 797 } 798} 799 800//===----------------------------------------------------------------------===// 801// ObjCAtDefsFieldDecl 802//===----------------------------------------------------------------------===// 803 804ObjCAtDefsFieldDecl 805*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, 806 SourceLocation StartLoc, SourceLocation IdLoc, 807 IdentifierInfo *Id, QualType T, Expr *BW) { 808 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW); 809} 810 811//===----------------------------------------------------------------------===// 812// ObjCProtocolDecl 813//===----------------------------------------------------------------------===// 814 815ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC, 816 SourceLocation L, 817 IdentifierInfo *Id) { 818 return new (C) ObjCProtocolDecl(DC, L, Id); 819} 820 821ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) { 822 ObjCProtocolDecl *PDecl = this; 823 824 if (Name == getIdentifier()) 825 return PDecl; 826 827 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I) 828 if ((PDecl = (*I)->lookupProtocolNamed(Name))) 829 return PDecl; 830 831 return NULL; 832} 833 834// lookupMethod - Lookup a instance/class method in the protocol and protocols 835// it inherited. 836ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel, 837 bool isInstance) const { 838 ObjCMethodDecl *MethodDecl = NULL; 839 840 if ((MethodDecl = getMethod(Sel, isInstance))) 841 return MethodDecl; 842 843 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I) 844 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance))) 845 return MethodDecl; 846 return NULL; 847} 848 849//===----------------------------------------------------------------------===// 850// ObjCClassDecl 851//===----------------------------------------------------------------------===// 852 853ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L, 854 ObjCInterfaceDecl *const *Elts, 855 const SourceLocation *Locs, 856 unsigned nElts, 857 ASTContext &C) 858 : Decl(ObjCClass, DC, L) { 859 setClassList(C, Elts, Locs, nElts); 860} 861 862void ObjCClassDecl::setClassList(ASTContext &C, ObjCInterfaceDecl*const*List, 863 const SourceLocation *Locs, unsigned Num) { 864 ForwardDecls = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef)*Num, 865 llvm::alignOf<ObjCClassRef>()); 866 for (unsigned i = 0; i < Num; ++i) 867 new (&ForwardDecls[i]) ObjCClassRef(List[i], Locs[i]); 868 869 NumDecls = Num; 870} 871 872ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC, 873 SourceLocation L, 874 ObjCInterfaceDecl *const *Elts, 875 const SourceLocation *Locs, 876 unsigned nElts) { 877 return new (C) ObjCClassDecl(DC, L, Elts, Locs, nElts, C); 878} 879 880SourceRange ObjCClassDecl::getSourceRange() const { 881 // FIXME: We should include the semicolon 882 assert(NumDecls); 883 return SourceRange(getLocation(), ForwardDecls[NumDecls-1].getLocation()); 884} 885 886//===----------------------------------------------------------------------===// 887// ObjCForwardProtocolDecl 888//===----------------------------------------------------------------------===// 889 890ObjCForwardProtocolDecl:: 891ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L, 892 ObjCProtocolDecl *const *Elts, unsigned nElts, 893 const SourceLocation *Locs, ASTContext &C) 894: Decl(ObjCForwardProtocol, DC, L) { 895 ReferencedProtocols.set(Elts, nElts, Locs, C); 896} 897 898 899ObjCForwardProtocolDecl * 900ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC, 901 SourceLocation L, 902 ObjCProtocolDecl *const *Elts, 903 unsigned NumElts, 904 const SourceLocation *Locs) { 905 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C); 906} 907 908//===----------------------------------------------------------------------===// 909// ObjCCategoryDecl 910//===----------------------------------------------------------------------===// 911 912ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC, 913 SourceLocation AtLoc, 914 SourceLocation ClassNameLoc, 915 SourceLocation CategoryNameLoc, 916 IdentifierInfo *Id) { 917 return new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id); 918} 919 920ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const { 921 return getASTContext().getObjCImplementation( 922 const_cast<ObjCCategoryDecl*>(this)); 923} 924 925void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) { 926 getASTContext().setObjCImplementation(this, ImplD); 927} 928 929 930//===----------------------------------------------------------------------===// 931// ObjCCategoryImplDecl 932//===----------------------------------------------------------------------===// 933 934ObjCCategoryImplDecl * 935ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC, 936 SourceLocation L,IdentifierInfo *Id, 937 ObjCInterfaceDecl *ClassInterface) { 938 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface); 939} 940 941ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const { 942 // The class interface might be NULL if we are working with invalid code. 943 if (const ObjCInterfaceDecl *ID = getClassInterface()) 944 return ID->FindCategoryDeclaration(getIdentifier()); 945 return 0; 946} 947 948 949void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) { 950 // FIXME: The context should be correct before we get here. 951 property->setLexicalDeclContext(this); 952 addDecl(property); 953} 954 955void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) { 956 ASTContext &Ctx = getASTContext(); 957 958 if (ObjCImplementationDecl *ImplD 959 = dyn_cast_or_null<ObjCImplementationDecl>(this)) { 960 if (IFace) 961 Ctx.setObjCImplementation(IFace, ImplD); 962 963 } else if (ObjCCategoryImplDecl *ImplD = 964 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) { 965 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier())) 966 Ctx.setObjCImplementation(CD, ImplD); 967 } 968 969 ClassInterface = IFace; 970} 971 972/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of 973/// properties implemented in this category @implementation block and returns 974/// the implemented property that uses it. 975/// 976ObjCPropertyImplDecl *ObjCImplDecl:: 977FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const { 978 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ 979 ObjCPropertyImplDecl *PID = *i; 980 if (PID->getPropertyIvarDecl() && 981 PID->getPropertyIvarDecl()->getIdentifier() == ivarId) 982 return PID; 983 } 984 return 0; 985} 986 987/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl 988/// added to the list of those properties @synthesized/@dynamic in this 989/// category @implementation block. 990/// 991ObjCPropertyImplDecl *ObjCImplDecl:: 992FindPropertyImplDecl(IdentifierInfo *Id) const { 993 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ 994 ObjCPropertyImplDecl *PID = *i; 995 if (PID->getPropertyDecl()->getIdentifier() == Id) 996 return PID; 997 } 998 return 0; 999} 1000 1001raw_ostream &clang::operator<<(raw_ostream &OS, 1002 const ObjCCategoryImplDecl *CID) { 1003 OS << CID->getName(); 1004 return OS; 1005} 1006 1007//===----------------------------------------------------------------------===// 1008// ObjCImplementationDecl 1009//===----------------------------------------------------------------------===// 1010 1011ObjCImplementationDecl * 1012ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC, 1013 SourceLocation L, 1014 ObjCInterfaceDecl *ClassInterface, 1015 ObjCInterfaceDecl *SuperDecl) { 1016 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl); 1017} 1018 1019void ObjCImplementationDecl::setIvarInitializers(ASTContext &C, 1020 CXXCtorInitializer ** initializers, 1021 unsigned numInitializers) { 1022 if (numInitializers > 0) { 1023 NumIvarInitializers = numInitializers; 1024 CXXCtorInitializer **ivarInitializers = 1025 new (C) CXXCtorInitializer*[NumIvarInitializers]; 1026 memcpy(ivarInitializers, initializers, 1027 numInitializers * sizeof(CXXCtorInitializer*)); 1028 IvarInitializers = ivarInitializers; 1029 } 1030} 1031 1032raw_ostream &clang::operator<<(raw_ostream &OS, 1033 const ObjCImplementationDecl *ID) { 1034 OS << ID->getName(); 1035 return OS; 1036} 1037 1038//===----------------------------------------------------------------------===// 1039// ObjCCompatibleAliasDecl 1040//===----------------------------------------------------------------------===// 1041 1042ObjCCompatibleAliasDecl * 1043ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC, 1044 SourceLocation L, 1045 IdentifierInfo *Id, 1046 ObjCInterfaceDecl* AliasedClass) { 1047 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass); 1048} 1049 1050//===----------------------------------------------------------------------===// 1051// ObjCPropertyDecl 1052//===----------------------------------------------------------------------===// 1053 1054ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC, 1055 SourceLocation L, 1056 IdentifierInfo *Id, 1057 SourceLocation AtLoc, 1058 TypeSourceInfo *T, 1059 PropertyControl propControl) { 1060 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T); 1061} 1062 1063//===----------------------------------------------------------------------===// 1064// ObjCPropertyImplDecl 1065//===----------------------------------------------------------------------===// 1066 1067ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C, 1068 DeclContext *DC, 1069 SourceLocation atLoc, 1070 SourceLocation L, 1071 ObjCPropertyDecl *property, 1072 Kind PK, 1073 ObjCIvarDecl *ivar, 1074 SourceLocation ivarLoc) { 1075 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar, 1076 ivarLoc); 1077} 1078 1079SourceRange ObjCPropertyImplDecl::getSourceRange() const { 1080 SourceLocation EndLoc = getLocation(); 1081 if (IvarLoc.isValid()) 1082 EndLoc = IvarLoc; 1083 1084 return SourceRange(AtLoc, EndLoc); 1085} 1086