From 3aa1861bd8b5121e53379b1a00f9d6ad8dead4f6 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 28 Feb 2009 18:42:10 +0000 Subject: [PATCH] "This patch addresses two FIXME on ObjCCategoryImplDecl: /// FIXME: Like ObjCImplementationDecl, this should not be a NamedDecl! /// FIXME: Introduce a new common base class for ObjCImplementationDecl and ObjCCategoryImplDecl It adds an IndentifierInfo ivar to the ObjCCategoryImplDecl, so it can inherits from Decl and not NamedDecl (I'm not sure about the memory management of this ivar). And now that both ObjCImplementationDecl and ObjCCategoryImplDecl have the same super classes, it allow creation of a common base class: ObjCImplDecl" Patch by Jean-Daniel Dupas! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@65703 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/DeclObjC.h | 155 ++++++++++++++--------------------- lib/AST/DeclObjC.cpp | 62 ++------------ 2 files changed, 66 insertions(+), 151 deletions(-) diff --git a/include/clang/AST/DeclObjC.h b/include/clang/AST/DeclObjC.h index 2cc6db1471..bdb8759b58 100644 --- a/include/clang/AST/DeclObjC.h +++ b/include/clang/AST/DeclObjC.h @@ -743,25 +743,10 @@ public: static bool classof(const ObjCCategoryDecl *D) { return true; } }; -/// ObjCCategoryImplDecl - An object of this class encapsulates a category -/// @implementation declaration. If a category class has declaration of a -/// property, its implementation must be specified in the category's -/// @implementation declaration. Example: -/// @interface I @end -/// @interface I(CATEGORY) -/// @property int p1, d1; -/// @end -/// @implementation I(CATEGORY) -/// @dynamic p1,d1; -/// @end -/// -/// FIXME: Like ObjCImplementationDecl, this should not be a NamedDecl! -/// FIXME: Introduce a new common base class for ObjCImplementationDecl and -/// ObjCCategoryImplDecl -class ObjCCategoryImplDecl : public NamedDecl, public DeclContext { +class ObjCImplDecl : public Decl, public DeclContext { /// Class interface for this category implementation ObjCInterfaceDecl *ClassInterface; - + /// implemented instance methods llvm::SmallVector InstanceMethods; @@ -770,18 +755,15 @@ class ObjCCategoryImplDecl : public NamedDecl, public DeclContext { /// Property Implementations in this category llvm::SmallVector PropertyImplementations; - + SourceLocation EndLoc; - - ObjCCategoryImplDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id, - ObjCInterfaceDecl *classInterface) - : NamedDecl(ObjCCategoryImpl, DC, L, Id), DeclContext(ObjCCategoryImpl), - ClassInterface(classInterface) {} +protected: + ObjCImplDecl(Kind DK, DeclContext *DC, SourceLocation L, + ObjCInterfaceDecl *classInterface) + : Decl(DK, DC, L), DeclContext(DK), + ClassInterface(classInterface) {} + public: - static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC, - SourceLocation L, IdentifierInfo *Id, - ObjCInterfaceDecl *classInterface); - const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } ObjCInterfaceDecl *getClassInterface() { return ClassInterface; } @@ -791,7 +773,7 @@ public: void addClassMethod(ObjCMethodDecl *method) { ClassMethods.push_back(method); } - + // Get the local instance/class method declared in this interface. ObjCMethodDecl *getInstanceMethod(Selector Sel) const; ObjCMethodDecl *getClassMethod(Selector Sel) const; @@ -802,7 +784,7 @@ public: void addPropertyImplementation(ObjCPropertyImplDecl *property) { PropertyImplementations.push_back(property); } - + ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId) const; ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const; @@ -830,7 +812,51 @@ public: SourceLocation getLocStart() const { return getLocation(); } SourceLocation getLocEnd() const { return EndLoc; } void setLocEnd(SourceLocation LE) { EndLoc = LE; }; - +}; + +/// ObjCCategoryImplDecl - An object of this class encapsulates a category +/// @implementation declaration. If a category class has declaration of a +/// property, its implementation must be specified in the category's +/// @implementation declaration. Example: +/// @interface I @end +/// @interface I(CATEGORY) +/// @property int p1, d1; +/// @end +/// @implementation I(CATEGORY) +/// @dynamic p1,d1; +/// @end +/// +/// ObjCCategoryImplDecl +class ObjCCategoryImplDecl : public ObjCImplDecl { + // Category name + IdentifierInfo *Id; + + ObjCCategoryImplDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id, + ObjCInterfaceDecl *classInterface) + : ObjCImplDecl(ObjCCategoryImpl, DC, L, classInterface), Id(Id) {} +public: + static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC, + SourceLocation L, IdentifierInfo *Id, + ObjCInterfaceDecl *classInterface); + + /// getIdentifier - Get the identifier that names the class + /// interface associated with this implementation. + IdentifierInfo *getIdentifier() const { + return Id; + } + + /// getNameAsCString - Get the name of identifier for the class + /// interface associated with this implementation as a C string + /// (const char*). + const char *getNameAsCString() const { + return Id->getName(); + } + + /// @brief Get the name of the class associated with this interface. + std::string getNameAsString() const { + return Id->getName(); + } + static bool classof(const Decl *D) { return D->getKind() == ObjCCategoryImpl;} static bool classof(const ObjCCategoryImplDecl *D) { return true; } static DeclContext *castToDeclContext(const ObjCCategoryImplDecl *D) { @@ -855,32 +881,18 @@ public: /// allow instance variables to be specified in the implementation. When /// specified, they need to be *identical* to the interface. /// -class ObjCImplementationDecl : public Decl, public DeclContext { - /// Class interface for this implementation - ObjCInterfaceDecl *ClassInterface; - +class ObjCImplementationDecl : public ObjCImplDecl { /// Implementation Class's super class. ObjCInterfaceDecl *SuperClass; /// Instance variables declared in the @implementation. ObjCList IVars; - /// implemented instance methods - llvm::SmallVector InstanceMethods; - - /// implemented class methods - llvm::SmallVector ClassMethods; - - /// Properties being implemented - llvm::SmallVector PropertyImplementations; - - SourceLocation EndLoc; - ObjCImplementationDecl(DeclContext *DC, SourceLocation L, ObjCInterfaceDecl *classInterface, ObjCInterfaceDecl *superDecl) - : Decl(ObjCImplementation, DC, L), DeclContext(ObjCImplementation), - ClassInterface(classInterface), SuperClass(superDecl){} + : ObjCImplDecl(ObjCImplementation, DC, L, classInterface), + SuperClass(superDecl){} public: static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L, @@ -894,34 +906,6 @@ public: IVars.set(InArray, Num, C); } - void addInstanceMethod(ObjCMethodDecl *method) { - InstanceMethods.push_back(method); - } - void addClassMethod(ObjCMethodDecl *method) { - ClassMethods.push_back(method); - } - - void addPropertyImplementation(ObjCPropertyImplDecl *property) { - PropertyImplementations.push_back(property); - } - - ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId) const; - ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const; - - typedef llvm::SmallVector::const_iterator - propimpl_iterator; - propimpl_iterator propimpl_begin() const { - return PropertyImplementations.begin(); - } - propimpl_iterator propimpl_end() const { - return PropertyImplementations.end(); - } - - // Location information, modeled after the Stmt API. - SourceLocation getLocStart() const { return getLocation(); } - SourceLocation getLocEnd() const { return EndLoc; } - void setLocEnd(SourceLocation LE) { EndLoc = LE; }; - /// getIdentifier - Get the identifier that names the class /// interface associated with this implementation. IdentifierInfo *getIdentifier() const { @@ -941,30 +925,11 @@ public: return getClassInterface()->getNameAsString(); } - const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; } - ObjCInterfaceDecl *getClassInterface() { return ClassInterface; } const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; } ObjCInterfaceDecl *getSuperClass() { return SuperClass; } void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; } - - typedef llvm::SmallVector::const_iterator - instmeth_iterator; - instmeth_iterator instmeth_begin() const { return InstanceMethods.begin(); } - instmeth_iterator instmeth_end() const { return InstanceMethods.end(); } - - typedef llvm::SmallVector::const_iterator - classmeth_iterator; - classmeth_iterator classmeth_begin() const { return ClassMethods.begin(); } - classmeth_iterator classmeth_end() const { return ClassMethods.end(); } - - // Get the local instance/class method declared in this interface. - ObjCMethodDecl *getInstanceMethod(Selector Sel) const; - ObjCMethodDecl *getClassMethod(Selector Sel) const; - ObjCMethodDecl *getMethod(Selector Sel, bool isInstance) const { - return isInstance ? getInstanceMethod(Sel) : getClassMethod(Sel); - } - + typedef ObjCList::iterator ivar_iterator; ivar_iterator ivar_begin() const { return IVars.begin(); } ivar_iterator ivar_end() const { return IVars.end(); } diff --git a/lib/AST/DeclObjC.cpp b/lib/AST/DeclObjC.cpp index 1d2ad16479..bdbcce750d 100644 --- a/lib/AST/DeclObjC.cpp +++ b/lib/AST/DeclObjC.cpp @@ -525,7 +525,7 @@ ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC, /// properties implemented in this category @implementation block and returns /// the implemented property that uses it. /// -ObjCPropertyImplDecl *ObjCCategoryImplDecl:: +ObjCPropertyImplDecl *ObjCImplDecl:: FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const { for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ ObjCPropertyImplDecl *PID = *i; @@ -540,7 +540,7 @@ FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const { /// added to the list of those properties @synthesized/@dynamic in this /// category @implementation block. /// -ObjCPropertyImplDecl *ObjCCategoryImplDecl:: +ObjCPropertyImplDecl *ObjCImplDecl:: FindPropertyImplDecl(IdentifierInfo *Id) const { for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ ObjCPropertyImplDecl *PID = *i; @@ -550,20 +550,20 @@ FindPropertyImplDecl(IdentifierInfo *Id) const { return 0; } -// lookupInstanceMethod - This method returns an instance method by looking in +// getInstanceMethod - This method returns an instance method by looking in // the class implementation. Unlike interfaces, we don't look outside the // implementation. -ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const { +ObjCMethodDecl *ObjCImplDecl::getInstanceMethod(Selector Sel) const { for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) if ((*I)->getSelector() == Sel) return *I; return NULL; } -// lookupClassMethod - This method returns an instance method by looking in +// getClassMethod - This method returns an instance method by looking in // the class implementation. Unlike interfaces, we don't look outside the // implementation. -ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const { +ObjCMethodDecl *ObjCImplDecl::getClassMethod(Selector Sel) const { for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); I != E; ++I) if ((*I)->getSelector() == Sel) @@ -589,56 +589,6 @@ void ObjCImplementationDecl::Destroy(ASTContext &C) { Decl::Destroy(C); } -/// getInstanceMethod - This method returns an instance method by -/// looking in the class implementation. Unlike interfaces, we don't -/// look outside the implementation. -ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const { - for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) - if ((*I)->getSelector() == Sel) - return *I; - return NULL; -} - -/// getClassMethod - This method returns a class method by looking in -/// the class implementation. Unlike interfaces, we don't look outside -/// the implementation. -ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const { - for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); - I != E; ++I) - if ((*I)->getSelector() == Sel) - return *I; - return NULL; -} - -/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl -/// added to the list of those properties @synthesized/@dynamic in this -/// @implementation block. -/// -ObjCPropertyImplDecl *ObjCImplementationDecl:: -FindPropertyImplDecl(IdentifierInfo *Id) const { - for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ - ObjCPropertyImplDecl *PID = *i; - if (PID->getPropertyDecl()->getIdentifier() == Id) - return PID; - } - return 0; -} - -/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of -/// properties implemented in this @implementation block and returns the -/// implemented property that uses it. -/// -ObjCPropertyImplDecl *ObjCImplementationDecl:: -FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const { - for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ - ObjCPropertyImplDecl *PID = *i; - if (PID->getPropertyIvarDecl() && - PID->getPropertyIvarDecl()->getIdentifier() == ivarId) - return PID; - } - return 0; -} - //===----------------------------------------------------------------------===// // ObjCCompatibleAliasDecl //===----------------------------------------------------------------------===// -- 2.40.0