1 //===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Objective-C related Decl classes.
12 //===----------------------------------------------------------------------===//
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 #include "llvm/ADT/SmallString.h"
20 using namespace clang;
22 //===----------------------------------------------------------------------===//
24 //===----------------------------------------------------------------------===//
26 void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
28 if (Elts == 0) return; // Setting to an empty list is a noop.
31 List = new (Ctx) void*[Elts];
33 memcpy(List, InList, sizeof(void*)*Elts);
36 void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
37 const SourceLocation *Locs, ASTContext &Ctx) {
41 Locations = new (Ctx) SourceLocation[Elts];
42 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
43 set(InList, Elts, Ctx);
46 //===----------------------------------------------------------------------===//
48 //===----------------------------------------------------------------------===//
50 void ObjCContainerDecl::anchor() { }
52 /// getIvarDecl - This method looks up an ivar in this ContextDecl.
55 ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
56 lookup_const_iterator Ivar, IvarEnd;
57 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
58 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
64 // Get the local instance/class method declared in this interface.
66 ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
67 // Since instance & class methods can have the same name, the loop below
68 // ensures we get the correct method.
70 // @interface Whatever
71 // - (int) class_method;
72 // + (float) class_method;
75 lookup_const_iterator Meth, MethEnd;
76 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
77 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
78 if (MD && MD->isInstanceMethod() == isInstance)
85 ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
86 IdentifierInfo *propertyID) {
88 DeclContext::lookup_const_iterator I, E;
89 llvm::tie(I, E) = DC->lookup(propertyID);
91 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
98 ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
99 SmallString<128> ivarName;
101 llvm::raw_svector_ostream os(ivarName);
102 os << '_' << getIdentifier()->getName();
104 return &Ctx.Idents.get(ivarName.str());
107 /// FindPropertyDeclaration - Finds declaration of the property given its name
108 /// in 'PropertyId' and returns it. It returns 0, if not found.
110 ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
112 if (ObjCPropertyDecl *PD =
113 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
119 case Decl::ObjCProtocol: {
120 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
121 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
122 E = PID->protocol_end(); I != E; ++I)
123 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
127 case Decl::ObjCInterface: {
128 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
129 // Look through categories.
130 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
131 Cat; Cat = Cat->getNextClassCategory())
132 if (!Cat->IsClassExtension())
133 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
136 // Look through protocols.
137 for (ObjCInterfaceDecl::all_protocol_iterator
138 I = OID->all_referenced_protocol_begin(),
139 E = OID->all_referenced_protocol_end(); I != E; ++I)
140 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
143 // Finally, check the super class.
144 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
145 return superClass->FindPropertyDeclaration(PropertyId);
148 case Decl::ObjCCategory: {
149 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
150 // Look through protocols.
151 if (!OCD->IsClassExtension())
152 for (ObjCCategoryDecl::protocol_iterator
153 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
154 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
163 void ObjCInterfaceDecl::anchor() { }
165 /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
166 /// with name 'PropertyId' in the primary class; including those in protocols
167 /// (direct or indirect) used by the primary class.
170 ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
171 IdentifierInfo *PropertyId) const {
172 // FIXME: Should make sure no callers ever do this.
173 if (!hasDefinition())
176 if (data().ExternallyCompleted)
177 LoadExternalDefinition();
179 if (ObjCPropertyDecl *PD =
180 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
183 // Look through protocols.
184 for (ObjCInterfaceDecl::all_protocol_iterator
185 I = all_referenced_protocol_begin(),
186 E = all_referenced_protocol_end(); I != E; ++I)
187 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
193 void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM) const {
194 for (ObjCContainerDecl::prop_iterator P = prop_begin(),
195 E = prop_end(); P != E; ++P) {
196 ObjCPropertyDecl *Prop = *P;
197 PM[Prop->getIdentifier()] = Prop;
199 for (ObjCInterfaceDecl::all_protocol_iterator
200 PI = all_referenced_protocol_begin(),
201 E = all_referenced_protocol_end(); PI != E; ++PI)
202 (*PI)->collectPropertiesToImplement(PM);
203 // Note, the properties declared only in class extensions are still copied
204 // into the main @interface's property list, and therefore we don't
205 // explicitly, have to search class extension properties.
208 void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
209 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
212 if (data().ExternallyCompleted)
213 LoadExternalDefinition();
215 if (data().AllReferencedProtocols.empty() &&
216 data().ReferencedProtocols.empty()) {
217 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
221 // Check for duplicate protocol in class's protocol list.
222 // This is O(n*m). But it is extremely rare and number of protocols in
223 // class or its extension are very few.
224 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
225 for (unsigned i = 0; i < ExtNum; i++) {
226 bool protocolExists = false;
227 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
228 for (all_protocol_iterator
229 p = all_referenced_protocol_begin(),
230 e = all_referenced_protocol_end(); p != e; ++p) {
231 ObjCProtocolDecl *Proto = (*p);
232 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
233 protocolExists = true;
237 // Do we want to warn on a protocol in extension class which
238 // already exist in the class? Probably not.
240 ProtocolRefs.push_back(ProtoInExtension);
243 if (ProtocolRefs.empty())
246 // Merge ProtocolRefs into class's protocol list;
247 for (all_protocol_iterator p = all_referenced_protocol_begin(),
248 e = all_referenced_protocol_end(); p != e; ++p) {
249 ProtocolRefs.push_back(*p);
252 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
255 void ObjCInterfaceDecl::allocateDefinitionData() {
256 assert(!hasDefinition() && "ObjC class already has a definition");
257 Data = new (getASTContext()) DefinitionData();
258 Data->Definition = this;
260 // Make the type point at the definition, now that we have one.
262 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
265 void ObjCInterfaceDecl::startDefinition() {
266 allocateDefinitionData();
268 // Update all of the declarations with a pointer to the definition.
269 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
276 /// getFirstClassExtension - Find first class extension of the given class.
277 ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
278 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
279 CDecl = CDecl->getNextClassCategory())
280 if (CDecl->IsClassExtension())
285 /// getNextClassCategory - Find next class extension in list of categories.
286 const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
287 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
288 CDecl = CDecl->getNextClassCategory())
289 if (CDecl->IsClassExtension())
294 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
295 ObjCInterfaceDecl *&clsDeclared) {
296 // FIXME: Should make sure no callers ever do this.
297 if (!hasDefinition())
300 if (data().ExternallyCompleted)
301 LoadExternalDefinition();
303 ObjCInterfaceDecl* ClassDecl = this;
304 while (ClassDecl != NULL) {
305 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
306 clsDeclared = ClassDecl;
309 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
310 CDecl; CDecl = CDecl->getNextClassExtension()) {
311 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
312 clsDeclared = ClassDecl;
317 ClassDecl = ClassDecl->getSuperClass();
322 /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
323 /// class whose name is passed as argument. If it is not one of the super classes
324 /// the it returns NULL.
325 ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
326 const IdentifierInfo*ICName) {
327 // FIXME: Should make sure no callers ever do this.
328 if (!hasDefinition())
331 if (data().ExternallyCompleted)
332 LoadExternalDefinition();
334 ObjCInterfaceDecl* ClassDecl = this;
335 while (ClassDecl != NULL) {
336 if (ClassDecl->getIdentifier() == ICName)
338 ClassDecl = ClassDecl->getSuperClass();
343 /// lookupMethod - This method returns an instance/class method by looking in
344 /// the class, its categories, and its super classes (using a linear search).
345 ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
347 bool shallowCategoryLookup) const {
348 // FIXME: Should make sure no callers ever do this.
349 if (!hasDefinition())
352 const ObjCInterfaceDecl* ClassDecl = this;
353 ObjCMethodDecl *MethodDecl = 0;
355 if (data().ExternallyCompleted)
356 LoadExternalDefinition();
358 while (ClassDecl != NULL) {
359 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
362 // Didn't find one yet - look through protocols.
363 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
364 E = ClassDecl->protocol_end();
366 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
369 // Didn't find one yet - now look through categories.
370 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
372 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
375 if (!shallowCategoryLookup) {
376 // Didn't find one yet - look through protocols.
377 const ObjCList<ObjCProtocolDecl> &Protocols =
378 CatDecl->getReferencedProtocols();
379 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
380 E = Protocols.end(); I != E; ++I)
381 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
384 CatDecl = CatDecl->getNextClassCategory();
387 ClassDecl = ClassDecl->getSuperClass();
392 // Will search "local" class/category implementations for a method decl.
393 // If failed, then we search in class's root for an instance method.
394 // Returns 0 if no method is found.
395 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
397 bool Instance) const {
398 // FIXME: Should make sure no callers ever do this.
399 if (!hasDefinition())
402 if (data().ExternallyCompleted)
403 LoadExternalDefinition();
405 ObjCMethodDecl *Method = 0;
406 if (ObjCImplementationDecl *ImpDecl = getImplementation())
407 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
408 : ImpDecl->getClassMethod(Sel);
410 // Look through local category implementations associated with the class.
412 Method = Instance ? getCategoryInstanceMethod(Sel)
413 : getCategoryClassMethod(Sel);
415 // Before we give up, check if the selector is an instance method.
416 // But only in the root. This matches gcc's behavior and what the
418 if (!Instance && !Method && !getSuperClass()) {
419 Method = lookupInstanceMethod(Sel);
420 // Look through local category implementations associated
421 // with the root class.
423 Method = lookupPrivateMethod(Sel, true);
426 if (!Method && getSuperClass())
427 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
431 //===----------------------------------------------------------------------===//
433 //===----------------------------------------------------------------------===//
435 ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
436 SourceLocation beginLoc,
437 SourceLocation endLoc,
438 Selector SelInfo, QualType T,
439 TypeSourceInfo *ResultTInfo,
440 DeclContext *contextDecl,
443 bool isPropertyAccessor,
444 bool isImplicitlyDeclared,
446 ImplementationControl impControl,
447 bool HasRelatedResultType) {
448 return new (C) ObjCMethodDecl(beginLoc, endLoc,
449 SelInfo, T, ResultTInfo, contextDecl,
450 isInstance, isVariadic, isPropertyAccessor,
451 isImplicitlyDeclared, isDefined,
453 HasRelatedResultType);
456 ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
457 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
458 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
459 Selector(), QualType(), 0, 0);
462 Stmt *ObjCMethodDecl::getBody() const {
463 return Body.get(getASTContext().getExternalSource());
466 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
468 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
469 IsRedeclaration = true;
470 PrevMethod->HasRedeclaration = true;
473 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
474 ArrayRef<ParmVarDecl*> Params,
475 ArrayRef<SourceLocation> SelLocs) {
476 ParamsAndSelLocs = 0;
477 NumParams = Params.size();
478 if (Params.empty() && SelLocs.empty())
481 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
482 sizeof(SourceLocation) * SelLocs.size();
483 ParamsAndSelLocs = C.Allocate(Size);
484 std::copy(Params.begin(), Params.end(), getParams());
485 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
488 void ObjCMethodDecl::getSelectorLocs(
489 SmallVectorImpl<SourceLocation> &SelLocs) const {
490 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
491 SelLocs.push_back(getSelectorLoc(i));
494 void ObjCMethodDecl::setMethodParams(ASTContext &C,
495 ArrayRef<ParmVarDecl*> Params,
496 ArrayRef<SourceLocation> SelLocs) {
497 assert((!SelLocs.empty() || isImplicit()) &&
498 "No selector locs for non-implicit method");
500 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
502 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
504 if (SelLocsKind != SelLoc_NonStandard)
505 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
507 setParamsAndSelLocs(C, Params, SelLocs);
510 /// \brief A definition will return its interface declaration.
511 /// An interface declaration will return its definition.
512 /// Otherwise it will return itself.
513 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
514 ASTContext &Ctx = getASTContext();
515 ObjCMethodDecl *Redecl = 0;
516 if (HasRedeclaration)
517 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
521 Decl *CtxD = cast<Decl>(getDeclContext());
523 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
524 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
525 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
527 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
528 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
529 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
531 } else if (ObjCImplementationDecl *ImplD =
532 dyn_cast<ObjCImplementationDecl>(CtxD)) {
533 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
534 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
536 } else if (ObjCCategoryImplDecl *CImplD =
537 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
538 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
539 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
542 if (!Redecl && isRedeclaration()) {
543 // This is the last redeclaration, go back to the first method.
544 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
548 return Redecl ? Redecl : this;
551 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
552 Decl *CtxD = cast<Decl>(getDeclContext());
554 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
555 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
556 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
560 } else if (ObjCCategoryImplDecl *CImplD =
561 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
562 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
563 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
568 if (isRedeclaration())
569 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
575 SourceLocation ObjCMethodDecl::getLocEnd() const {
576 if (Stmt *Body = getBody())
577 return Body->getLocEnd();
581 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
582 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
583 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
586 // Check for an explicit attribute.
587 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
588 // The unfortunate necessity of mapping between enums here is due
589 // to the attributes framework.
590 switch (attr->getFamily()) {
591 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
592 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
593 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
594 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
595 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
596 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
598 Family = static_cast<unsigned>(family);
602 family = getSelector().getMethodFamily();
604 case OMF_None: break;
606 // init only has a conventional meaning for an instance method, and
607 // it has to return an object.
609 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
613 // alloc/copy/new have a conventional meaning for both class and
614 // instance methods, but they require an object return.
617 case OMF_mutableCopy:
619 if (!getResultType()->isObjCObjectPointerType())
623 // These selectors have a conventional meaning only for instance methods.
628 case OMF_autorelease:
629 case OMF_retainCount:
631 if (!isInstanceMethod())
635 case OMF_performSelector:
636 if (!isInstanceMethod() ||
637 !getResultType()->isObjCIdType())
640 unsigned noParams = param_size();
641 if (noParams < 1 || noParams > 3)
644 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
645 QualType ArgT = (*it);
646 if (!ArgT->isObjCSelType()) {
653 if (!ArgT->isObjCIdType()) {
665 Family = static_cast<unsigned>(family);
669 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
670 const ObjCInterfaceDecl *OID) {
672 if (isInstanceMethod()) {
673 // There may be no interface context due to error in declaration
674 // of the interface (which has been reported). Recover gracefully.
676 selfTy = Context.getObjCInterfaceType(OID);
677 selfTy = Context.getObjCObjectPointerType(selfTy);
679 selfTy = Context.getObjCIdType();
681 } else // we have a factory method.
682 selfTy = Context.getObjCClassType();
684 bool selfIsPseudoStrong = false;
685 bool selfIsConsumed = false;
687 if (Context.getLangOpts().ObjCAutoRefCount) {
688 if (isInstanceMethod()) {
689 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
691 // 'self' is always __strong. It's actually pseudo-strong except
692 // in init methods (or methods labeled ns_consumes_self), though.
694 qs.setObjCLifetime(Qualifiers::OCL_Strong);
695 selfTy = Context.getQualifiedType(selfTy, qs);
697 // In addition, 'self' is const unless this is an init method.
698 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
699 selfTy = selfTy.withConst();
700 selfIsPseudoStrong = true;
704 assert(isClassMethod());
705 // 'self' is always const in class methods.
706 selfTy = selfTy.withConst();
707 selfIsPseudoStrong = true;
711 ImplicitParamDecl *self
712 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
713 &Context.Idents.get("self"), selfTy);
717 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
719 if (selfIsPseudoStrong)
720 self->setARCPseudoStrong(true);
722 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
723 &Context.Idents.get("_cmd"),
724 Context.getObjCSelType()));
727 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
728 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
730 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
731 return CD->getClassInterface();
732 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
733 return IMD->getClassInterface();
735 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
736 llvm_unreachable("unknown method context");
739 static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
740 const ObjCMethodDecl *Method,
741 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
746 // In categories look for overriden methods from protocols. A method from
747 // category is not "overriden" since it is considered as the "same" method
748 // (same USR) as the one from the interface.
749 if (const ObjCCategoryDecl *
750 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
751 // Check whether we have a matching method at this category but only if we
752 // are at the super class level.
755 Overridden = Container->getMethod(Method->getSelector(),
756 Method->isInstanceMethod()))
757 if (Method != Overridden) {
758 // We found an override at this category; there is no need to look
759 // into its protocols.
760 Methods.push_back(Overridden);
764 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
765 PEnd = Category->protocol_end();
767 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
771 // Check whether we have a matching method at this level.
772 if (const ObjCMethodDecl *
773 Overridden = Container->getMethod(Method->getSelector(),
774 Method->isInstanceMethod()))
775 if (Method != Overridden) {
776 // We found an override at this level; there is no need to look
777 // into other protocols or categories.
778 Methods.push_back(Overridden);
782 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
783 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
784 PEnd = Protocol->protocol_end();
786 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
789 if (const ObjCInterfaceDecl *
790 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
791 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
792 PEnd = Interface->protocol_end();
794 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
796 for (const ObjCCategoryDecl *Category = Interface->getCategoryList();
797 Category; Category = Category->getNextClassCategory())
798 CollectOverriddenMethodsRecurse(Category, Method, Methods,
801 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
802 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
803 /*MovedToSuper=*/true);
807 static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
808 const ObjCMethodDecl *Method,
809 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
810 CollectOverriddenMethodsRecurse(Container, Method, Methods,
811 /*MovedToSuper=*/false);
814 static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
815 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
816 assert(Method->isOverriding());
818 if (const ObjCProtocolDecl *
819 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
820 CollectOverriddenMethods(ProtD, Method, overridden);
822 } else if (const ObjCImplDecl *
823 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
824 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
827 // Start searching for overridden methods using the method from the
828 // interface as starting point.
829 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
830 Method->isInstanceMethod()))
832 CollectOverriddenMethods(ID, Method, overridden);
834 } else if (const ObjCCategoryDecl *
835 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
836 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
839 // Start searching for overridden methods using the method from the
840 // interface as starting point.
841 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
842 Method->isInstanceMethod()))
844 CollectOverriddenMethods(ID, Method, overridden);
847 CollectOverriddenMethods(
848 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
853 static void collectOnCategoriesAfterLocation(SourceLocation Loc,
854 const ObjCInterfaceDecl *Class,
856 const ObjCMethodDecl *Method,
857 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
861 for (const ObjCCategoryDecl *Category = Class->getCategoryList();
862 Category; Category = Category->getNextClassCategory())
863 if (SM.isBeforeInTranslationUnit(Loc, Category->getLocation()))
864 CollectOverriddenMethodsRecurse(Category, Method, Methods, true);
866 collectOnCategoriesAfterLocation(Loc, Class->getSuperClass(), SM,
870 /// \brief Faster collection that is enabled when ObjCMethodDecl::isOverriding()
872 /// You'd think that in that case there are no overrides but categories can
873 /// "introduce" new overridden methods that are missed by Sema because the
874 /// overrides lookup that it does for methods, inside implementations, will
875 /// stop at the interface level (if there is a method there) and not look
876 /// further in super classes.
877 /// Methods in an implementation can overide methods in super class's category
878 /// but not in current class's category. But, such methods
879 static void collectOverriddenMethodsFast(SourceManager &SM,
880 const ObjCMethodDecl *Method,
881 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
882 assert(!Method->isOverriding());
884 const ObjCContainerDecl *
885 ContD = cast<ObjCContainerDecl>(Method->getDeclContext());
886 if (isa<ObjCInterfaceDecl>(ContD) || isa<ObjCProtocolDecl>(ContD))
888 const ObjCInterfaceDecl *Class = Method->getClassInterface();
892 collectOnCategoriesAfterLocation(Class->getLocation(), Class->getSuperClass(),
893 SM, Method, Methods);
896 void ObjCMethodDecl::getOverriddenMethods(
897 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
898 const ObjCMethodDecl *Method = this;
900 if (Method->isRedeclaration()) {
901 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
902 getMethod(Method->getSelector(), Method->isInstanceMethod());
905 if (!Method->isOverriding()) {
906 collectOverriddenMethodsFast(getASTContext().getSourceManager(),
909 collectOverriddenMethodsSlow(Method, Overridden);
910 assert(!Overridden.empty() &&
911 "ObjCMethodDecl's overriding bit is not as expected");
915 const ObjCPropertyDecl *
916 ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
917 Selector Sel = getSelector();
918 unsigned NumArgs = Sel.getNumArgs();
922 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
925 if (isPropertyAccessor()) {
926 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
927 bool IsGetter = (NumArgs == 0);
929 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
930 E = Container->prop_end();
932 Selector NextSel = IsGetter ? (*I)->getGetterName()
933 : (*I)->getSetterName();
938 llvm_unreachable("Marked as a property accessor but no property found!");
944 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
945 OverridesTy Overrides;
946 getOverriddenMethods(Overrides);
947 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
949 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
957 //===----------------------------------------------------------------------===//
959 //===----------------------------------------------------------------------===//
961 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
963 SourceLocation atLoc,
965 ObjCInterfaceDecl *PrevDecl,
966 SourceLocation ClassLoc,
968 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
969 PrevDecl, isInternal);
970 C.getObjCInterfaceType(Result, PrevDecl);
974 ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
976 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
977 return new (Mem) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
982 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
983 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
985 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
986 TypeForDecl(0), Data()
988 setPreviousDeclaration(PrevDecl);
990 // Copy the 'data' pointer over.
992 Data = PrevDecl->Data;
994 setImplicit(isInternal);
997 void ObjCInterfaceDecl::LoadExternalDefinition() const {
998 assert(data().ExternallyCompleted && "Class is not externally completed");
999 data().ExternallyCompleted = false;
1000 getASTContext().getExternalSource()->CompleteType(
1001 const_cast<ObjCInterfaceDecl *>(this));
1004 void ObjCInterfaceDecl::setExternallyCompleted() {
1005 assert(getASTContext().getExternalSource() &&
1006 "Class can't be externally completed without an external source");
1007 assert(hasDefinition() &&
1008 "Forward declarations can't be externally completed");
1009 data().ExternallyCompleted = true;
1012 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
1013 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1014 if (data().ExternallyCompleted)
1015 LoadExternalDefinition();
1017 return getASTContext().getObjCImplementation(
1018 const_cast<ObjCInterfaceDecl*>(Def));
1021 // FIXME: Should make sure no callers ever do this.
1025 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
1026 getASTContext().setObjCImplementation(getDefinition(), ImplD);
1029 /// all_declared_ivar_begin - return first ivar declared in this class,
1030 /// its extensions and its implementation. Lazily build the list on first
1032 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
1033 // FIXME: Should make sure no callers ever do this.
1034 if (!hasDefinition())
1037 if (data().IvarList)
1038 return data().IvarList;
1040 ObjCIvarDecl *curIvar = 0;
1041 if (!ivar_empty()) {
1042 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1043 data().IvarList = *I; ++I;
1044 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1045 curIvar->setNextIvar(*I);
1048 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
1049 CDecl = CDecl->getNextClassExtension()) {
1050 if (!CDecl->ivar_empty()) {
1051 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
1052 E = CDecl->ivar_end();
1053 if (!data().IvarList) {
1054 data().IvarList = *I; ++I;
1055 curIvar = data().IvarList;
1057 for ( ;I != E; curIvar = *I, ++I)
1058 curIvar->setNextIvar(*I);
1062 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1063 if (!ImplDecl->ivar_empty()) {
1064 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1065 E = ImplDecl->ivar_end();
1066 if (!data().IvarList) {
1067 data().IvarList = *I; ++I;
1068 curIvar = data().IvarList;
1070 for ( ;I != E; curIvar = *I, ++I)
1071 curIvar->setNextIvar(*I);
1074 return data().IvarList;
1077 /// FindCategoryDeclaration - Finds category declaration in the list of
1078 /// categories for this class and returns it. Name of the category is passed
1079 /// in 'CategoryId'. If category not found, return 0;
1082 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
1083 // FIXME: Should make sure no callers ever do this.
1084 if (!hasDefinition())
1087 if (data().ExternallyCompleted)
1088 LoadExternalDefinition();
1090 for (ObjCCategoryDecl *Category = getCategoryList();
1091 Category; Category = Category->getNextClassCategory())
1092 if (Category->getIdentifier() == CategoryId)
1098 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1099 for (ObjCCategoryDecl *Category = getCategoryList();
1100 Category; Category = Category->getNextClassCategory())
1101 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
1102 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1107 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1108 for (ObjCCategoryDecl *Category = getCategoryList();
1109 Category; Category = Category->getNextClassCategory())
1110 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
1111 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1116 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1117 /// has been implemented in IDecl class, its super class or categories (if
1118 /// lookupCategory is true).
1119 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1120 bool lookupCategory,
1121 bool RHSIsQualifiedID) {
1122 if (!hasDefinition())
1125 ObjCInterfaceDecl *IDecl = this;
1126 // 1st, look up the class.
1127 for (ObjCInterfaceDecl::protocol_iterator
1128 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
1129 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1131 // This is dubious and is added to be compatible with gcc. In gcc, it is
1132 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1133 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1134 // object. This IMO, should be a bug.
1135 // FIXME: Treat this as an extension, and flag this as an error when GCC
1136 // extensions are not enabled.
1137 if (RHSIsQualifiedID &&
1138 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1142 // 2nd, look up the category.
1144 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
1145 CDecl = CDecl->getNextClassCategory()) {
1146 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
1147 E = CDecl->protocol_end(); PI != E; ++PI)
1148 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1152 // 3rd, look up the super class(s)
1153 if (IDecl->getSuperClass())
1155 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1161 //===----------------------------------------------------------------------===//
1163 //===----------------------------------------------------------------------===//
1165 void ObjCIvarDecl::anchor() { }
1167 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
1168 SourceLocation StartLoc,
1169 SourceLocation IdLoc, IdentifierInfo *Id,
1170 QualType T, TypeSourceInfo *TInfo,
1171 AccessControl ac, Expr *BW,
1174 // Ivar's can only appear in interfaces, implementations (via synthesized
1175 // properties), and class extensions (via direct declaration, or synthesized
1178 // FIXME: This should really be asserting this:
1179 // (isa<ObjCCategoryDecl>(DC) &&
1180 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1181 // but unfortunately we sometimes place ivars into non-class extension
1182 // categories on error. This breaks an AST invariant, and should not be
1184 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1185 isa<ObjCCategoryDecl>(DC)) &&
1186 "Invalid ivar decl context!");
1187 // Once a new ivar is created in any of class/class-extension/implementation
1188 // decl contexts, the previously built IvarList must be rebuilt.
1189 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1191 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
1192 ID = IM->getClassInterface();
1194 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
1199 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1200 ac, BW, synthesized);
1203 ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1204 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1205 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1206 QualType(), 0, ObjCIvarDecl::None, 0, false);
1209 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1210 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
1212 switch (DC->getKind()) {
1214 case ObjCCategoryImpl:
1216 llvm_unreachable("invalid ivar container!");
1218 // Ivars can only appear in class extension categories.
1219 case ObjCCategory: {
1220 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1221 assert(CD->IsClassExtension() && "invalid container for ivar!");
1222 return CD->getClassInterface();
1225 case ObjCImplementation:
1226 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1229 return cast<ObjCInterfaceDecl>(DC);
1233 //===----------------------------------------------------------------------===//
1234 // ObjCAtDefsFieldDecl
1235 //===----------------------------------------------------------------------===//
1237 void ObjCAtDefsFieldDecl::anchor() { }
1240 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1241 SourceLocation StartLoc, SourceLocation IdLoc,
1242 IdentifierInfo *Id, QualType T, Expr *BW) {
1243 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
1246 ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1248 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1249 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1253 //===----------------------------------------------------------------------===//
1255 //===----------------------------------------------------------------------===//
1257 void ObjCProtocolDecl::anchor() { }
1259 ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1260 SourceLocation nameLoc,
1261 SourceLocation atStartLoc,
1262 ObjCProtocolDecl *PrevDecl)
1263 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
1265 setPreviousDeclaration(PrevDecl);
1267 Data = PrevDecl->Data;
1270 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
1272 SourceLocation nameLoc,
1273 SourceLocation atStartLoc,
1274 ObjCProtocolDecl *PrevDecl) {
1275 ObjCProtocolDecl *Result
1276 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
1281 ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1283 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1284 return new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(),
1288 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1289 ObjCProtocolDecl *PDecl = this;
1291 if (Name == getIdentifier())
1294 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1295 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1301 // lookupMethod - Lookup a instance/class method in the protocol and protocols
1303 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1304 bool isInstance) const {
1305 ObjCMethodDecl *MethodDecl = NULL;
1307 if ((MethodDecl = getMethod(Sel, isInstance)))
1310 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1311 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
1316 void ObjCProtocolDecl::allocateDefinitionData() {
1317 assert(!Data && "Protocol already has a definition!");
1318 Data = new (getASTContext()) DefinitionData;
1319 Data->Definition = this;
1322 void ObjCProtocolDecl::startDefinition() {
1323 allocateDefinitionData();
1325 // Update all of the declarations with a pointer to the definition.
1326 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1328 RD->Data = this->Data;
1331 void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM) const {
1332 for (ObjCProtocolDecl::prop_iterator P = prop_begin(),
1333 E = prop_end(); P != E; ++P) {
1334 ObjCPropertyDecl *Prop = *P;
1335 // Insert into PM if not there already.
1336 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
1338 // Scan through protocol's protocols.
1339 for (ObjCProtocolDecl::protocol_iterator PI = protocol_begin(),
1340 E = protocol_end(); PI != E; ++PI)
1341 (*PI)->collectPropertiesToImplement(PM);
1345 //===----------------------------------------------------------------------===//
1347 //===----------------------------------------------------------------------===//
1349 void ObjCCategoryDecl::anchor() { }
1351 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
1352 SourceLocation AtLoc,
1353 SourceLocation ClassNameLoc,
1354 SourceLocation CategoryNameLoc,
1356 ObjCInterfaceDecl *IDecl,
1357 SourceLocation IvarLBraceLoc,
1358 SourceLocation IvarRBraceLoc) {
1359 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1360 CategoryNameLoc, Id,
1362 IvarLBraceLoc, IvarRBraceLoc);
1364 // Link this category into its class's category list.
1365 CatDecl->NextClassCategory = IDecl->getCategoryList();
1366 if (IDecl->hasDefinition()) {
1367 IDecl->setCategoryList(CatDecl);
1368 if (ASTMutationListener *L = C.getASTMutationListener())
1369 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1376 ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1378 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1379 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1380 SourceLocation(), 0, 0);
1383 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1384 return getASTContext().getObjCImplementation(
1385 const_cast<ObjCCategoryDecl*>(this));
1388 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1389 getASTContext().setObjCImplementation(this, ImplD);
1393 //===----------------------------------------------------------------------===//
1394 // ObjCCategoryImplDecl
1395 //===----------------------------------------------------------------------===//
1397 void ObjCCategoryImplDecl::anchor() { }
1399 ObjCCategoryImplDecl *
1400 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
1402 ObjCInterfaceDecl *ClassInterface,
1403 SourceLocation nameLoc,
1404 SourceLocation atStartLoc,
1405 SourceLocation CategoryNameLoc) {
1406 if (ClassInterface && ClassInterface->hasDefinition())
1407 ClassInterface = ClassInterface->getDefinition();
1408 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
1409 nameLoc, atStartLoc, CategoryNameLoc);
1412 ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1414 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1415 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1416 SourceLocation(), SourceLocation());
1419 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
1420 // The class interface might be NULL if we are working with invalid code.
1421 if (const ObjCInterfaceDecl *ID = getClassInterface())
1422 return ID->FindCategoryDeclaration(getIdentifier());
1427 void ObjCImplDecl::anchor() { }
1429 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
1430 // FIXME: The context should be correct before we get here.
1431 property->setLexicalDeclContext(this);
1435 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1436 ASTContext &Ctx = getASTContext();
1438 if (ObjCImplementationDecl *ImplD
1439 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
1441 Ctx.setObjCImplementation(IFace, ImplD);
1443 } else if (ObjCCategoryImplDecl *ImplD =
1444 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1445 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1446 Ctx.setObjCImplementation(CD, ImplD);
1449 ClassInterface = IFace;
1452 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
1453 /// properties implemented in this category \@implementation block and returns
1454 /// the implemented property that uses it.
1456 ObjCPropertyImplDecl *ObjCImplDecl::
1457 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1458 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1459 ObjCPropertyImplDecl *PID = *i;
1460 if (PID->getPropertyIvarDecl() &&
1461 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1467 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1468 /// added to the list of those properties \@synthesized/\@dynamic in this
1469 /// category \@implementation block.
1471 ObjCPropertyImplDecl *ObjCImplDecl::
1472 FindPropertyImplDecl(IdentifierInfo *Id) const {
1473 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1474 ObjCPropertyImplDecl *PID = *i;
1475 if (PID->getPropertyDecl()->getIdentifier() == Id)
1481 raw_ostream &clang::operator<<(raw_ostream &OS,
1482 const ObjCCategoryImplDecl &CID) {
1483 OS << CID.getName();
1487 //===----------------------------------------------------------------------===//
1488 // ObjCImplementationDecl
1489 //===----------------------------------------------------------------------===//
1491 void ObjCImplementationDecl::anchor() { }
1493 ObjCImplementationDecl *
1494 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
1495 ObjCInterfaceDecl *ClassInterface,
1496 ObjCInterfaceDecl *SuperDecl,
1497 SourceLocation nameLoc,
1498 SourceLocation atStartLoc,
1499 SourceLocation IvarLBraceLoc,
1500 SourceLocation IvarRBraceLoc) {
1501 if (ClassInterface && ClassInterface->hasDefinition())
1502 ClassInterface = ClassInterface->getDefinition();
1503 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1504 nameLoc, atStartLoc,
1505 IvarLBraceLoc, IvarRBraceLoc);
1508 ObjCImplementationDecl *
1509 ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1510 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1511 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1515 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1516 CXXCtorInitializer ** initializers,
1517 unsigned numInitializers) {
1518 if (numInitializers > 0) {
1519 NumIvarInitializers = numInitializers;
1520 CXXCtorInitializer **ivarInitializers =
1521 new (C) CXXCtorInitializer*[NumIvarInitializers];
1522 memcpy(ivarInitializers, initializers,
1523 numInitializers * sizeof(CXXCtorInitializer*));
1524 IvarInitializers = ivarInitializers;
1528 raw_ostream &clang::operator<<(raw_ostream &OS,
1529 const ObjCImplementationDecl &ID) {
1534 //===----------------------------------------------------------------------===//
1535 // ObjCCompatibleAliasDecl
1536 //===----------------------------------------------------------------------===//
1538 void ObjCCompatibleAliasDecl::anchor() { }
1540 ObjCCompatibleAliasDecl *
1541 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1544 ObjCInterfaceDecl* AliasedClass) {
1545 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1548 ObjCCompatibleAliasDecl *
1549 ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1550 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1551 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1554 //===----------------------------------------------------------------------===//
1556 //===----------------------------------------------------------------------===//
1558 void ObjCPropertyDecl::anchor() { }
1560 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1563 SourceLocation AtLoc,
1564 SourceLocation LParenLoc,
1566 PropertyControl propControl) {
1567 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
1570 ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1572 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1573 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1578 //===----------------------------------------------------------------------===//
1579 // ObjCPropertyImplDecl
1580 //===----------------------------------------------------------------------===//
1582 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
1584 SourceLocation atLoc,
1586 ObjCPropertyDecl *property,
1589 SourceLocation ivarLoc) {
1590 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1594 ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1596 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1597 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1598 0, Dynamic, 0, SourceLocation());
1601 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1602 SourceLocation EndLoc = getLocation();
1603 if (IvarLoc.isValid())
1606 return SourceRange(AtLoc, EndLoc);