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