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 return new (C) ObjCMethodDecl(beginLoc, endLoc, 346 SelInfo, T, ResultTInfo, contextDecl, 347 isInstance, 348 isVariadic, isSynthesized, isImplicitlyDeclared, 349 isDefined, 350 impControl, 351 HasRelatedResultType); 352 } 353 354 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) { 355 assert(PrevMethod); 356 getASTContext().setObjCMethodRedeclaration(PrevMethod, this); 357 IsRedeclaration = true; 358 PrevMethod->HasRedeclaration = true; 359 } 360 361 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C, 362 ArrayRef<ParmVarDecl*> Params, 363 ArrayRef<SourceLocation> SelLocs) { 364 ParamsAndSelLocs = 0; 365 NumParams = Params.size(); 366 if (Params.empty() && SelLocs.empty()) 367 return; 368 369 unsigned Size = sizeof(ParmVarDecl *) * NumParams + 370 sizeof(SourceLocation) * SelLocs.size(); 371 ParamsAndSelLocs = C.Allocate(Size); 372 std::copy(Params.begin(), Params.end(), getParams()); 373 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs()); 374 } 375 376 void ObjCMethodDecl::getSelectorLocs( 377 SmallVectorImpl<SourceLocation> &SelLocs) const { 378 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i) 379 SelLocs.push_back(getSelectorLoc(i)); 380 } 381 382 void ObjCMethodDecl::setMethodParams(ASTContext &C, 383 ArrayRef<ParmVarDecl*> Params, 384 ArrayRef<SourceLocation> SelLocs) { 385 assert((!SelLocs.empty() || isImplicit()) && 386 "No selector locs for non-implicit method"); 387 if (isImplicit()) 388 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>()); 389 390 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params, EndLoc); 391 if (SelLocsKind != SelLoc_NonStandard) 392 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>()); 393 394 setParamsAndSelLocs(C, Params, SelLocs); 395 } 396 397 /// \brief A definition will return its interface declaration. 398 /// An interface declaration will return its definition. 399 /// Otherwise it will return itself. 400 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() { 401 ASTContext &Ctx = getASTContext(); 402 ObjCMethodDecl *Redecl = 0; 403 if (HasRedeclaration) 404 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this)); 405 if (Redecl) 406 return Redecl; 407 408 Decl *CtxD = cast<Decl>(getDeclContext()); 409 410 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) { 411 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD)) 412 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod()); 413 414 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) { 415 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD)) 416 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod()); 417 418 } else if (ObjCImplementationDecl *ImplD = 419 dyn_cast<ObjCImplementationDecl>(CtxD)) { 420 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) 421 Redecl = IFD->getMethod(getSelector(), isInstanceMethod()); 422 423 } else if (ObjCCategoryImplDecl *CImplD = 424 dyn_cast<ObjCCategoryImplDecl>(CtxD)) { 425 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl()) 426 Redecl = CatD->getMethod(getSelector(), isInstanceMethod()); 427 } 428 429 if (!Redecl && isRedeclaration()) { 430 // This is the last redeclaration, go back to the first method. 431 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(), 432 isInstanceMethod()); 433 } 434 435 return Redecl ? Redecl : this; 436 } 437 438 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() { 439 Decl *CtxD = cast<Decl>(getDeclContext()); 440 441 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) { 442 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) 443 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(), 444 isInstanceMethod())) 445 return MD; 446 447 } else if (ObjCCategoryImplDecl *CImplD = 448 dyn_cast<ObjCCategoryImplDecl>(CtxD)) { 449 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl()) 450 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(), 451 isInstanceMethod())) 452 return MD; 453 } 454 455 if (isRedeclaration()) 456 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(), 457 isInstanceMethod()); 458 459 return this; 460 } 461 462 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const { 463 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family); 464 if (family != static_cast<unsigned>(InvalidObjCMethodFamily)) 465 return family; 466 467 // Check for an explicit attribute. 468 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) { 469 // The unfortunate necessity of mapping between enums here is due 470 // to the attributes framework. 471 switch (attr->getFamily()) { 472 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break; 473 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break; 474 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break; 475 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break; 476 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break; 477 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break; 478 } 479 Family = static_cast<unsigned>(family); 480 return family; 481 } 482 483 family = getSelector().getMethodFamily(); 484 switch (family) { 485 case OMF_None: break; 486 487 // init only has a conventional meaning for an instance method, and 488 // it has to return an object. 489 case OMF_init: 490 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType()) 491 family = OMF_None; 492 break; 493 494 // alloc/copy/new have a conventional meaning for both class and 495 // instance methods, but they require an object return. 496 case OMF_alloc: 497 case OMF_copy: 498 case OMF_mutableCopy: 499 case OMF_new: 500 if (!getResultType()->isObjCObjectPointerType()) 501 family = OMF_None; 502 break; 503 504 // These selectors have a conventional meaning only for instance methods. 505 case OMF_dealloc: 506 case OMF_finalize: 507 case OMF_retain: 508 case OMF_release: 509 case OMF_autorelease: 510 case OMF_retainCount: 511 case OMF_self: 512 if (!isInstanceMethod()) 513 family = OMF_None; 514 break; 515 516 case OMF_performSelector: 517 if (!isInstanceMethod() || 518 !getResultType()->isObjCIdType()) 519 family = OMF_None; 520 else { 521 unsigned noParams = param_size(); 522 if (noParams < 1 || noParams > 3) 523 family = OMF_None; 524 else { 525 ObjCMethodDecl::arg_type_iterator it = arg_type_begin(); 526 QualType ArgT = (*it); 527 if (!ArgT->isObjCSelType()) { 528 family = OMF_None; 529 break; 530 } 531 while (--noParams) { 532 it++; 533 ArgT = (*it); 534 if (!ArgT->isObjCIdType()) { 535 family = OMF_None; 536 break; 537 } 538 } 539 } 540 } 541 break; 542 543 } 544 545 // Cache the result. 546 Family = static_cast<unsigned>(family); 547 return family; 548 } 549 550 void ObjCMethodDecl::createImplicitParams(ASTContext &Context, 551 const ObjCInterfaceDecl *OID) { 552 QualType selfTy; 553 if (isInstanceMethod()) { 554 // There may be no interface context due to error in declaration 555 // of the interface (which has been reported). Recover gracefully. 556 if (OID) { 557 selfTy = Context.getObjCInterfaceType(OID); 558 selfTy = Context.getObjCObjectPointerType(selfTy); 559 } else { 560 selfTy = Context.getObjCIdType(); 561 } 562 } else // we have a factory method. 563 selfTy = Context.getObjCClassType(); 564 565 bool selfIsPseudoStrong = false; 566 bool selfIsConsumed = false; 567 if (isInstanceMethod() && Context.getLangOptions().ObjCAutoRefCount) { 568 selfIsConsumed = hasAttr<NSConsumesSelfAttr>(); 569 570 // 'self' is always __strong. It's actually pseudo-strong except 571 // in init methods, though. 572 Qualifiers qs; 573 qs.setObjCLifetime(Qualifiers::OCL_Strong); 574 selfTy = Context.getQualifiedType(selfTy, qs); 575 576 // In addition, 'self' is const unless this is an init method. 577 if (getMethodFamily() != OMF_init) { 578 selfTy = selfTy.withConst(); 579 selfIsPseudoStrong = true; 580 } 581 } 582 583 ImplicitParamDecl *self 584 = ImplicitParamDecl::Create(Context, this, SourceLocation(), 585 &Context.Idents.get("self"), selfTy); 586 setSelfDecl(self); 587 588 if (selfIsConsumed) 589 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context)); 590 591 if (selfIsPseudoStrong) 592 self->setARCPseudoStrong(true); 593 594 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(), 595 &Context.Idents.get("_cmd"), 596 Context.getObjCSelType())); 597 } 598 599 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() { 600 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext())) 601 return ID; 602 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext())) 603 return CD->getClassInterface(); 604 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext())) 605 return IMD->getClassInterface(); 606 607 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method"); 608 llvm_unreachable("unknown method context"); 609 } 610 611 //===----------------------------------------------------------------------===// 612 // ObjCInterfaceDecl 613 //===----------------------------------------------------------------------===// 614 615 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C, 616 DeclContext *DC, 617 SourceLocation atLoc, 618 IdentifierInfo *Id, 619 SourceLocation ClassLoc, 620 bool ForwardDecl, bool isInternal){ 621 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl, 622 isInternal); 623 } 624 625 ObjCInterfaceDecl:: 626 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id, 627 SourceLocation CLoc, bool FD, bool isInternal) 628 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc), 629 TypeForDecl(0), SuperClass(0), 630 CategoryList(0), IvarList(0), 631 InitiallyForwardDecl(FD), ForwardDecl(FD), 632 InternalInterface(isInternal), ExternallyCompleted(false) { 633 } 634 635 void ObjCInterfaceDecl::LoadExternalDefinition() const { 636 assert(ExternallyCompleted && "Class is not externally completed"); 637 ExternallyCompleted = false; 638 getASTContext().getExternalSource()->CompleteType( 639 const_cast<ObjCInterfaceDecl *>(this)); 640 } 641 642 void ObjCInterfaceDecl::setExternallyCompleted() { 643 assert(getASTContext().getExternalSource() && 644 "Class can't be externally completed without an external source"); 645 assert(!ForwardDecl && 646 "Forward declarations can't be externally completed"); 647 ExternallyCompleted = true; 648 } 649 650 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const { 651 if (ExternallyCompleted) 652 LoadExternalDefinition(); 653 654 return getASTContext().getObjCImplementation( 655 const_cast<ObjCInterfaceDecl*>(this)); 656 } 657 658 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) { 659 getASTContext().setObjCImplementation(this, ImplD); 660 } 661 662 /// all_declared_ivar_begin - return first ivar declared in this class, 663 /// its extensions and its implementation. Lazily build the list on first 664 /// access. 665 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() { 666 if (IvarList) 667 return IvarList; 668 669 ObjCIvarDecl *curIvar = 0; 670 if (!ivar_empty()) { 671 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end(); 672 IvarList = (*I); ++I; 673 for (curIvar = IvarList; I != E; curIvar = *I, ++I) 674 curIvar->setNextIvar(*I); 675 } 676 677 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl; 678 CDecl = CDecl->getNextClassExtension()) { 679 if (!CDecl->ivar_empty()) { 680 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(), 681 E = CDecl->ivar_end(); 682 if (!IvarList) { 683 IvarList = (*I); ++I; 684 curIvar = IvarList; 685 } 686 for ( ;I != E; curIvar = *I, ++I) 687 curIvar->setNextIvar(*I); 688 } 689 } 690 691 if (ObjCImplementationDecl *ImplDecl = getImplementation()) { 692 if (!ImplDecl->ivar_empty()) { 693 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(), 694 E = ImplDecl->ivar_end(); 695 if (!IvarList) { 696 IvarList = (*I); ++I; 697 curIvar = IvarList; 698 } 699 for ( ;I != E; curIvar = *I, ++I) 700 curIvar->setNextIvar(*I); 701 } 702 } 703 return IvarList; 704 } 705 706 /// FindCategoryDeclaration - Finds category declaration in the list of 707 /// categories for this class and returns it. Name of the category is passed 708 /// in 'CategoryId'. If category not found, return 0; 709 /// 710 ObjCCategoryDecl * 711 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const { 712 if (ExternallyCompleted) 713 LoadExternalDefinition(); 714 715 for (ObjCCategoryDecl *Category = getCategoryList(); 716 Category; Category = Category->getNextClassCategory()) 717 if (Category->getIdentifier() == CategoryId) 718 return Category; 719 return 0; 720 } 721 722 ObjCMethodDecl * 723 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const { 724 for (ObjCCategoryDecl *Category = getCategoryList(); 725 Category; Category = Category->getNextClassCategory()) 726 if (ObjCCategoryImplDecl *Impl = Category->getImplementation()) 727 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel)) 728 return MD; 729 return 0; 730 } 731 732 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const { 733 for (ObjCCategoryDecl *Category = getCategoryList(); 734 Category; Category = Category->getNextClassCategory()) 735 if (ObjCCategoryImplDecl *Impl = Category->getImplementation()) 736 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel)) 737 return MD; 738 return 0; 739 } 740 741 /// ClassImplementsProtocol - Checks that 'lProto' protocol 742 /// has been implemented in IDecl class, its super class or categories (if 743 /// lookupCategory is true). 744 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto, 745 bool lookupCategory, 746 bool RHSIsQualifiedID) { 747 ObjCInterfaceDecl *IDecl = this; 748 // 1st, look up the class. 749 const ObjCList<ObjCProtocolDecl> &Protocols = 750 IDecl->getReferencedProtocols(); 751 752 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(), 753 E = Protocols.end(); PI != E; ++PI) { 754 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI)) 755 return true; 756 // This is dubious and is added to be compatible with gcc. In gcc, it is 757 // also allowed assigning a protocol-qualified 'id' type to a LHS object 758 // when protocol in qualified LHS is in list of protocols in the rhs 'id' 759 // object. This IMO, should be a bug. 760 // FIXME: Treat this as an extension, and flag this as an error when GCC 761 // extensions are not enabled. 762 if (RHSIsQualifiedID && 763 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto)) 764 return true; 765 } 766 767 // 2nd, look up the category. 768 if (lookupCategory) 769 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl; 770 CDecl = CDecl->getNextClassCategory()) { 771 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(), 772 E = CDecl->protocol_end(); PI != E; ++PI) 773 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI)) 774 return true; 775 } 776 777 // 3rd, look up the super class(s) 778 if (IDecl->getSuperClass()) 779 return 780 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory, 781 RHSIsQualifiedID); 782 783 return false; 784 } 785 786 //===----------------------------------------------------------------------===// 787 // ObjCIvarDecl 788 //===----------------------------------------------------------------------===// 789 790 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC, 791 SourceLocation StartLoc, 792 SourceLocation IdLoc, IdentifierInfo *Id, 793 QualType T, TypeSourceInfo *TInfo, 794 AccessControl ac, Expr *BW, 795 bool synthesized) { 796 if (DC) { 797 // Ivar's can only appear in interfaces, implementations (via synthesized 798 // properties), and class extensions (via direct declaration, or synthesized 799 // properties). 800 // 801 // FIXME: This should really be asserting this: 802 // (isa<ObjCCategoryDecl>(DC) && 803 // cast<ObjCCategoryDecl>(DC)->IsClassExtension())) 804 // but unfortunately we sometimes place ivars into non-class extension 805 // categories on error. This breaks an AST invariant, and should not be 806 // fixed. 807 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) || 808 isa<ObjCCategoryDecl>(DC)) && 809 "Invalid ivar decl context!"); 810 // Once a new ivar is created in any of class/class-extension/implementation 811 // decl contexts, the previously built IvarList must be rebuilt. 812 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC); 813 if (!ID) { 814 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) { 815 ID = IM->getClassInterface(); 816 if (BW) 817 IM->setHasSynthBitfield(true); 818 } else { 819 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC); 820 ID = CD->getClassInterface(); 821 if (BW) 822 CD->setHasSynthBitfield(true); 823 } 824 } 825 ID->setIvarList(0); 826 } 827 828 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, 829 ac, BW, synthesized); 830 } 831 832 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const { 833 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext()); 834 835 switch (DC->getKind()) { 836 default: 837 case ObjCCategoryImpl: 838 case ObjCProtocol: 839 llvm_unreachable("invalid ivar container!"); 840 841 // Ivars can only appear in class extension categories. 842 case ObjCCategory: { 843 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC); 844 assert(CD->IsClassExtension() && "invalid container for ivar!"); 845 return CD->getClassInterface(); 846 } 847 848 case ObjCImplementation: 849 return cast<ObjCImplementationDecl>(DC)->getClassInterface(); 850 851 case ObjCInterface: 852 return cast<ObjCInterfaceDecl>(DC); 853 } 854 } 855 856 //===----------------------------------------------------------------------===// 857 // ObjCAtDefsFieldDecl 858 //===----------------------------------------------------------------------===// 859 860 ObjCAtDefsFieldDecl 861 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, 862 SourceLocation StartLoc, SourceLocation IdLoc, 863 IdentifierInfo *Id, QualType T, Expr *BW) { 864 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW); 865 } 866 867 //===----------------------------------------------------------------------===// 868 // ObjCProtocolDecl 869 //===----------------------------------------------------------------------===// 870 871 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC, 872 IdentifierInfo *Id, 873 SourceLocation nameLoc, 874 SourceLocation atStartLoc, 875 bool isForwardDecl) { 876 return new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, isForwardDecl); 877 } 878 879 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) { 880 ObjCProtocolDecl *PDecl = this; 881 882 if (Name == getIdentifier()) 883 return PDecl; 884 885 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I) 886 if ((PDecl = (*I)->lookupProtocolNamed(Name))) 887 return PDecl; 888 889 return NULL; 890 } 891 892 // lookupMethod - Lookup a instance/class method in the protocol and protocols 893 // it inherited. 894 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel, 895 bool isInstance) const { 896 ObjCMethodDecl *MethodDecl = NULL; 897 898 if ((MethodDecl = getMethod(Sel, isInstance))) 899 return MethodDecl; 900 901 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I) 902 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance))) 903 return MethodDecl; 904 return NULL; 905 } 906 907 //===----------------------------------------------------------------------===// 908 // ObjCClassDecl 909 //===----------------------------------------------------------------------===// 910 911 ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L, 912 ObjCInterfaceDecl *const Elt, 913 const SourceLocation Loc, 914 ASTContext &C) 915 : Decl(ObjCClass, DC, L) { 916 setClass(C, Elt, Loc); 917 } 918 919 ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC, 920 SourceLocation L, 921 ObjCInterfaceDecl *const Elt, 922 const SourceLocation Loc) { 923 return new (C) ObjCClassDecl(DC, L, Elt, Loc, C); 924 } 925 926 void ObjCClassDecl::setClass(ASTContext &C, ObjCInterfaceDecl*const Cls, 927 const SourceLocation Loc) { 928 929 ForwardDecl = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef), 930 llvm::alignOf<ObjCClassRef>()); 931 new (ForwardDecl) ObjCClassRef(Cls, Loc); 932 } 933 934 SourceRange ObjCClassDecl::getSourceRange() const { 935 // FIXME: We should include the semicolon 936 return SourceRange(getLocation(), ForwardDecl->getLocation()); 937 } 938 939 //===----------------------------------------------------------------------===// 940 // ObjCForwardProtocolDecl 941 //===----------------------------------------------------------------------===// 942 943 ObjCForwardProtocolDecl:: 944 ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L, 945 ObjCProtocolDecl *const *Elts, unsigned nElts, 946 const SourceLocation *Locs, ASTContext &C) 947 : Decl(ObjCForwardProtocol, DC, L) { 948 ReferencedProtocols.set(Elts, nElts, Locs, C); 949 } 950 951 952 ObjCForwardProtocolDecl * 953 ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC, 954 SourceLocation L, 955 ObjCProtocolDecl *const *Elts, 956 unsigned NumElts, 957 const SourceLocation *Locs) { 958 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C); 959 } 960 961 //===----------------------------------------------------------------------===// 962 // ObjCCategoryDecl 963 //===----------------------------------------------------------------------===// 964 965 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC, 966 SourceLocation AtLoc, 967 SourceLocation ClassNameLoc, 968 SourceLocation CategoryNameLoc, 969 IdentifierInfo *Id, 970 ObjCInterfaceDecl *IDecl) { 971 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, 972 CategoryNameLoc, Id, 973 IDecl); 974 if (IDecl) { 975 // Link this category into its class's category list. 976 CatDecl->NextClassCategory = IDecl->getCategoryList(); 977 IDecl->setCategoryList(CatDecl); 978 if (ASTMutationListener *L = C.getASTMutationListener()) 979 L->AddedObjCCategoryToInterface(CatDecl, IDecl); 980 } 981 982 return CatDecl; 983 } 984 985 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, EmptyShell Empty) { 986 return new (C) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(), 987 SourceLocation(), 0, 0); 988 } 989 990 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const { 991 return getASTContext().getObjCImplementation( 992 const_cast<ObjCCategoryDecl*>(this)); 993 } 994 995 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) { 996 getASTContext().setObjCImplementation(this, ImplD); 997 } 998 999 1000 //===----------------------------------------------------------------------===// 1001 // ObjCCategoryImplDecl 1002 //===----------------------------------------------------------------------===// 1003 1004 ObjCCategoryImplDecl * 1005 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC, 1006 IdentifierInfo *Id, 1007 ObjCInterfaceDecl *ClassInterface, 1008 SourceLocation nameLoc, 1009 SourceLocation atStartLoc) { 1010 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface, 1011 nameLoc, atStartLoc); 1012 } 1013 1014 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const { 1015 // The class interface might be NULL if we are working with invalid code. 1016 if (const ObjCInterfaceDecl *ID = getClassInterface()) 1017 return ID->FindCategoryDeclaration(getIdentifier()); 1018 return 0; 1019 } 1020 1021 1022 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) { 1023 // FIXME: The context should be correct before we get here. 1024 property->setLexicalDeclContext(this); 1025 addDecl(property); 1026 } 1027 1028 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) { 1029 ASTContext &Ctx = getASTContext(); 1030 1031 if (ObjCImplementationDecl *ImplD 1032 = dyn_cast_or_null<ObjCImplementationDecl>(this)) { 1033 if (IFace) 1034 Ctx.setObjCImplementation(IFace, ImplD); 1035 1036 } else if (ObjCCategoryImplDecl *ImplD = 1037 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) { 1038 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier())) 1039 Ctx.setObjCImplementation(CD, ImplD); 1040 } 1041 1042 ClassInterface = IFace; 1043 } 1044 1045 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of 1046 /// properties implemented in this category @implementation block and returns 1047 /// the implemented property that uses it. 1048 /// 1049 ObjCPropertyImplDecl *ObjCImplDecl:: 1050 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const { 1051 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ 1052 ObjCPropertyImplDecl *PID = *i; 1053 if (PID->getPropertyIvarDecl() && 1054 PID->getPropertyIvarDecl()->getIdentifier() == ivarId) 1055 return PID; 1056 } 1057 return 0; 1058 } 1059 1060 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl 1061 /// added to the list of those properties @synthesized/@dynamic in this 1062 /// category @implementation block. 1063 /// 1064 ObjCPropertyImplDecl *ObjCImplDecl:: 1065 FindPropertyImplDecl(IdentifierInfo *Id) const { 1066 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ 1067 ObjCPropertyImplDecl *PID = *i; 1068 if (PID->getPropertyDecl()->getIdentifier() == Id) 1069 return PID; 1070 } 1071 return 0; 1072 } 1073 1074 raw_ostream &clang::operator<<(raw_ostream &OS, 1075 const ObjCCategoryImplDecl *CID) { 1076 OS << CID->getName(); 1077 return OS; 1078 } 1079 1080 //===----------------------------------------------------------------------===// 1081 // ObjCImplementationDecl 1082 //===----------------------------------------------------------------------===// 1083 1084 ObjCImplementationDecl * 1085 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC, 1086 ObjCInterfaceDecl *ClassInterface, 1087 ObjCInterfaceDecl *SuperDecl, 1088 SourceLocation nameLoc, 1089 SourceLocation atStartLoc) { 1090 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl, 1091 nameLoc, atStartLoc); 1092 } 1093 1094 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C, 1095 CXXCtorInitializer ** initializers, 1096 unsigned numInitializers) { 1097 if (numInitializers > 0) { 1098 NumIvarInitializers = numInitializers; 1099 CXXCtorInitializer **ivarInitializers = 1100 new (C) CXXCtorInitializer*[NumIvarInitializers]; 1101 memcpy(ivarInitializers, initializers, 1102 numInitializers * sizeof(CXXCtorInitializer*)); 1103 IvarInitializers = ivarInitializers; 1104 } 1105 } 1106 1107 raw_ostream &clang::operator<<(raw_ostream &OS, 1108 const ObjCImplementationDecl *ID) { 1109 OS << ID->getName(); 1110 return OS; 1111 } 1112 1113 //===----------------------------------------------------------------------===// 1114 // ObjCCompatibleAliasDecl 1115 //===----------------------------------------------------------------------===// 1116 1117 ObjCCompatibleAliasDecl * 1118 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC, 1119 SourceLocation L, 1120 IdentifierInfo *Id, 1121 ObjCInterfaceDecl* AliasedClass) { 1122 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass); 1123 } 1124 1125 //===----------------------------------------------------------------------===// 1126 // ObjCPropertyDecl 1127 //===----------------------------------------------------------------------===// 1128 1129 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC, 1130 SourceLocation L, 1131 IdentifierInfo *Id, 1132 SourceLocation AtLoc, 1133 TypeSourceInfo *T, 1134 PropertyControl propControl) { 1135 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T); 1136 } 1137 1138 //===----------------------------------------------------------------------===// 1139 // ObjCPropertyImplDecl 1140 //===----------------------------------------------------------------------===// 1141 1142 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C, 1143 DeclContext *DC, 1144 SourceLocation atLoc, 1145 SourceLocation L, 1146 ObjCPropertyDecl *property, 1147 Kind PK, 1148 ObjCIvarDecl *ivar, 1149 SourceLocation ivarLoc) { 1150 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar, 1151 ivarLoc); 1152 } 1153 1154 SourceRange ObjCPropertyImplDecl::getSourceRange() const { 1155 SourceLocation EndLoc = getLocation(); 1156 if (IvarLoc.isValid()) 1157 EndLoc = IvarLoc; 1158 1159 return SourceRange(AtLoc, EndLoc); 1160 } 1161