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