diff options
author | Chris Lattner <sabre@nondot.org> | 2007-12-12 07:46:12 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2007-12-12 07:46:12 +0000 |
commit | ab4c4d5e5ececa77aae7e291fafcba3049319cdc (patch) | |
tree | ed2ded2c97240e3155e474dc07f3ce12445d8efa | |
parent | 0157c5144513438bb74aebf50d18f95df4104acb (diff) |
resolve some fixmes and clean up some code by eliminating the get*Vars apis to some classes and use iterators instead.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44927 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | AST/Decl.cpp | 32 | ||||
-rw-r--r-- | Driver/ASTConsumers.cpp | 14 | ||||
-rw-r--r-- | Driver/RewriteTest.cpp | 117 | ||||
-rw-r--r-- | include/clang/AST/DeclObjC.h | 49 |
4 files changed, 90 insertions, 122 deletions
diff --git a/AST/Decl.cpp b/AST/Decl.cpp index c71fbca348..1e3636c2d5 100644 --- a/AST/Decl.cpp +++ b/AST/Decl.cpp @@ -502,13 +502,10 @@ ObjcMethodDecl *ObjcImplementationDecl::lookupInstanceMethod(Selector Sel) { /// the class implementation. Unlike interfaces, we don't look outside the /// implementation. ObjcMethodDecl *ObjcImplementationDecl::lookupClassMethod(Selector Sel) { - ObjcMethodDecl *const*methods = getClassMethods(); - int methodCount = getNumClassMethods(); - for (int i = 0; i < methodCount; ++i) { - if (methods[i]->getSelector() == Sel) { - return methods[i]; - } - } + for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); + I != E; ++I) + if ((*I)->getSelector() == Sel) + return *I; return NULL; } @@ -516,13 +513,9 @@ ObjcMethodDecl *ObjcImplementationDecl::lookupClassMethod(Selector Sel) { // the class implementation. Unlike interfaces, we don't look outside the // implementation. ObjcMethodDecl *ObjcCategoryImplDecl::lookupInstanceMethod(Selector &Sel) { - ObjcMethodDecl *const*methods = getInstanceMethods(); - int methodCount = getNumInstanceMethods(); - for (int i = 0; i < methodCount; ++i) { - if (methods[i]->getSelector() == Sel) { - return methods[i]; - } - } + for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) + if ((*I)->getSelector() == Sel) + return *I; return NULL; } @@ -530,13 +523,10 @@ ObjcMethodDecl *ObjcCategoryImplDecl::lookupInstanceMethod(Selector &Sel) { // the class implementation. Unlike interfaces, we don't look outside the // implementation. ObjcMethodDecl *ObjcCategoryImplDecl::lookupClassMethod(Selector &Sel) { - ObjcMethodDecl *const*methods = getClassMethods(); - int methodCount = getNumClassMethods(); - for (int i = 0; i < methodCount; ++i) { - if (methods[i]->getSelector() == Sel) { - return methods[i]; - } - } + for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); + I != E; ++I) + if ((*I)->getSelector() == Sel) + return *I; return NULL; } diff --git a/Driver/ASTConsumers.cpp b/Driver/ASTConsumers.cpp index 85140bc060..22863411b1 100644 --- a/Driver/ASTConsumers.cpp +++ b/Driver/ASTConsumers.cpp @@ -123,9 +123,10 @@ void DeclPrinter::PrintObjcImplementationDecl(ObjcImplementationDecl *OID) { else Out << "@implementation " << I; - for (int i = 0; i < OID->getNumInstanceMethods(); i++) { - PrintObjcMethodDecl(OID->getInstanceMethods()[i]); - ObjcMethodDecl *OMD = OID->getInstanceMethods()[i]; + for (ObjcImplementationDecl::instmeth_iterator I = OID->instmeth_begin(), + E = OID->instmeth_end(); I != E; ++I) { + ObjcMethodDecl *OMD = *I; + PrintObjcMethodDecl(OMD); if (OMD->getBody()) { Out << ' '; OMD->getBody()->printPretty(Out); @@ -133,9 +134,10 @@ void DeclPrinter::PrintObjcImplementationDecl(ObjcImplementationDecl *OID) { } } - for (int i = 0; i < OID->getNumClassMethods(); i++) { - PrintObjcMethodDecl(OID->getClassMethods()[i]); - ObjcMethodDecl *OMD = OID->getClassMethods()[i]; + for (ObjcImplementationDecl::classmeth_iterator I = OID->classmeth_begin(), + E = OID->classmeth_end(); I != E; ++I) { + ObjcMethodDecl *OMD = *I; + PrintObjcMethodDecl(OMD); if (OMD->getBody()) { Out << ' '; OMD->getBody()->printPretty(Out); diff --git a/Driver/RewriteTest.cpp b/Driver/RewriteTest.cpp index bb8f772e70..c8c8f9dd3e 100644 --- a/Driver/RewriteTest.cpp +++ b/Driver/RewriteTest.cpp @@ -184,8 +184,9 @@ namespace { void RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *CDecl, std::string &Result); - void RewriteObjcMethodsMetaData(ObjcMethodDecl *const*Methods, - int NumMethods, + typedef ObjcCategoryImplDecl::instmeth_iterator instmeth_iterator; + void RewriteObjcMethodsMetaData(instmeth_iterator MethodBegin, + instmeth_iterator MethodEnd, bool IsInstanceMethod, const char *prefix, const char *ClassName, @@ -560,16 +561,11 @@ void RewriteTest::RewriteImplementationDecl(NamedDecl *OID) { else Rewrite.InsertText(CID->getLocStart(), "// ", 3); - int numMethods = IMD ? IMD->getNumInstanceMethods() - : CID->getNumInstanceMethods(); - - for (int i = 0; i < numMethods; i++) { + for (ObjcCategoryImplDecl::instmeth_iterator + I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), + E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); I != E; ++I) { std::string ResultStr; - ObjcMethodDecl *OMD; - if (IMD) - OMD = IMD->getInstanceMethods()[i]; - else - OMD = CID->getInstanceMethods()[i]; + ObjcMethodDecl *OMD = *I; RewriteObjcMethodDecl(OMD, ResultStr); SourceLocation LocStart = OMD->getLocStart(); SourceLocation LocEnd = OMD->getBody()->getLocStart(); @@ -580,14 +576,11 @@ void RewriteTest::RewriteImplementationDecl(NamedDecl *OID) { ResultStr.c_str(), ResultStr.size()); } - numMethods = IMD ? IMD->getNumClassMethods() : CID->getNumClassMethods(); - for (int i = 0; i < numMethods; i++) { + for (ObjcCategoryImplDecl::classmeth_iterator + I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), + E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); I != E; ++I) { std::string ResultStr; - ObjcMethodDecl *OMD; - if (IMD) - OMD = IMD->getClassMethods()[i]; - else - OMD = CID->getClassMethods()[i]; + ObjcMethodDecl *OMD = *I; RewriteObjcMethodDecl(OMD, ResultStr); SourceLocation LocStart = OMD->getLocStart(); SourceLocation LocEnd = OMD->getBody()->getLocStart(); @@ -1747,14 +1740,16 @@ void RewriteTest::SynthesizeObjcInternalStruct(ObjcInterfaceDecl *CDecl, // RewriteObjcMethodsMetaData - Rewrite methods metadata for instance or /// class methods. -void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl *const*Methods, - int NumMethods, +void RewriteTest::RewriteObjcMethodsMetaData(instmeth_iterator MethodBegin, + instmeth_iterator MethodEnd, bool IsInstanceMethod, const char *prefix, const char *ClassName, std::string &Result) { + if (MethodBegin == MethodEnd) return; + static bool objc_impl_method = false; - if (NumMethods > 0 && !objc_impl_method) { + if (!objc_impl_method) { /* struct _objc_method { SEL _cmd; char *method_types; @@ -1779,40 +1774,39 @@ void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl *const*Methods, Result += "\tstruct _objc_method method_list[];\n};\n"; objc_impl_method = true; } + // Build _objc_method_list for class's methods if needed - if (NumMethods > 0) { - Result += "\nstatic struct _objc_method_list _OBJC_"; - Result += prefix; - Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; - Result += "_METHODS_"; - Result += ClassName; - Result += " __attribute__ ((section (\"__OBJC, __"; - Result += IsInstanceMethod ? "inst" : "cls"; - Result += "_meth\")))= "; - Result += "{\n\t0, " + utostr(NumMethods) + "\n"; - - Result += "\t,{{(SEL)\""; - Result += Methods[0]->getSelector().getName().c_str(); + Result += "\nstatic struct _objc_method_list _OBJC_"; + Result += prefix; + Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; + Result += "_METHODS_"; + Result += ClassName; + Result += " __attribute__ ((section (\"__OBJC, __"; + Result += IsInstanceMethod ? "inst" : "cls"; + Result += "_meth\")))= "; + Result += "{\n\t0, " + utostr(MethodEnd-MethodBegin) + "\n"; + + Result += "\t,{{(SEL)\""; + Result += (*MethodBegin)->getSelector().getName().c_str(); + std::string MethodTypeString; + Context->getObjcEncodingForMethodDecl(*MethodBegin, MethodTypeString); + Result += "\", \""; + Result += MethodTypeString; + Result += "\", "; + Result += MethodInternalNames[*MethodBegin]; + Result += "}\n"; + for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { + Result += "\t ,{(SEL)\""; + Result += (*MethodBegin)->getSelector().getName().c_str(); std::string MethodTypeString; - Context->getObjcEncodingForMethodDecl(Methods[0], MethodTypeString); + Context->getObjcEncodingForMethodDecl(*MethodBegin, MethodTypeString); Result += "\", \""; Result += MethodTypeString; Result += "\", "; - Result += MethodInternalNames[Methods[0]]; + Result += MethodInternalNames[*MethodBegin]; Result += "}\n"; - for (int i = 1; i < NumMethods; i++) { - Result += "\t ,{(SEL)\""; - Result += Methods[i]->getSelector().getName().c_str(); - std::string MethodTypeString; - Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString); - Result += "\", \""; - Result += MethodTypeString; - Result += "\", "; - Result += MethodInternalNames[Methods[i]]; - Result += "}\n"; - } - Result += "\t }\n};\n"; } + Result += "\t }\n};\n"; } /// RewriteObjcProtocolsMetaData - Rewrite protocols meta-data. @@ -2000,16 +1994,12 @@ void RewriteTest::RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *IDecl, sprintf(FullCategoryName, "%s_%s", ClassDecl->getName(), IDecl->getName()); // Build _objc_method_list for class's instance methods if needed - RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(), - IDecl->getNumInstanceMethods(), - true, - "CATEGORY_", FullCategoryName, Result); + RewriteObjcMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), + true, "CATEGORY_", FullCategoryName, Result); // Build _objc_method_list for class's class methods if needed - RewriteObjcMethodsMetaData(IDecl->getClassMethods(), - IDecl->getNumClassMethods(), - false, - "CATEGORY_", FullCategoryName, Result); + RewriteObjcMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), + false, "CATEGORY_", FullCategoryName, Result); // Protocols referenced in class declaration? // Null CDecl is case of a category implementation with no category interface @@ -2172,22 +2162,17 @@ void RewriteTest::RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl, } // Build _objc_method_list for class's instance methods if needed - RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(), - IDecl->getNumInstanceMethods(), - true, - "", IDecl->getName(), Result); + RewriteObjcMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), + true, "", IDecl->getName(), Result); // Build _objc_method_list for class's class methods if needed - RewriteObjcMethodsMetaData(IDecl->getClassMethods(), - IDecl->getNumClassMethods(), - false, - "", IDecl->getName(), Result); + RewriteObjcMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), + false, "", IDecl->getName(), Result); // Protocols referenced in class declaration? RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(), CDecl->getNumIntfRefProtocols(), - "CLASS", - CDecl->getName(), Result); + "CLASS", CDecl->getName(), Result); // Declaration of class/meta-class metadata diff --git a/include/clang/AST/DeclObjC.h b/include/clang/AST/DeclObjC.h index d9d17b3678..fd763d586c 100644 --- a/include/clang/AST/DeclObjC.h +++ b/include/clang/AST/DeclObjC.h @@ -155,8 +155,7 @@ public: // We also need to record the @end location. SourceLocation getAtEndLoc() const { return AtEndLoc; } - const int getNumPropertyDecl() const { return NumPropertyDecl; } - int getNumPropertyDecl() { return NumPropertyDecl; } + int getNumPropertyDecl() const { return NumPropertyDecl; } void setNumPropertyDecl(int num) { NumPropertyDecl = num; } ObjcPropertyDecl **const getPropertyDecl() const { return PropertyDecl; } @@ -479,24 +478,14 @@ class ObjcCategoryImplDecl : public NamedDecl { SourceLocation EndLoc; public: - ObjcCategoryImplDecl(SourceLocation L, IdentifierInfo *Id, - ObjcInterfaceDecl *classInterface) - : NamedDecl(ObjcCategoryImpl, L, Id), - ClassInterface(classInterface) {} + ObjcCategoryImplDecl(SourceLocation L, IdentifierInfo *Id, + ObjcInterfaceDecl *classInterface) + : NamedDecl(ObjcCategoryImpl, L, Id), ClassInterface(classInterface) {} ObjcInterfaceDecl *getClassInterface() const { return ClassInterface; } - // FIXME: Figure out how to remove the const pointer below. - ObjcMethodDecl *const*getInstanceMethods() const { - return &InstanceMethods[0]; - } - int getNumInstanceMethods() const { return InstanceMethods.size(); } - - // FIXME: Figure out how to remove the const pointer below. - ObjcMethodDecl *const*getClassMethods() const { - return &ClassMethods[0]; - } - int getNumClassMethods() const { return ClassMethods.size(); } + unsigned getNumInstanceMethods() const { return InstanceMethods.size(); } + unsigned getNumClassMethods() const { return ClassMethods.size(); } void addInstanceMethod(ObjcMethodDecl *method) { InstanceMethods.push_back(method); @@ -507,6 +496,17 @@ public: ObjcMethodDecl *lookupInstanceMethod(Selector &Sel); ObjcMethodDecl *lookupClassMethod(Selector &Sel); + typedef llvm::SmallVector<ObjcMethodDecl*, 32>::const_iterator + instmeth_iterator; + instmeth_iterator instmeth_begin() const { return InstanceMethods.begin(); } + instmeth_iterator instmeth_end() const { return InstanceMethods.end(); } + + typedef llvm::SmallVector<ObjcMethodDecl*, 32>::const_iterator + classmeth_iterator; + classmeth_iterator classmeth_begin() const { return ClassMethods.begin(); } + classmeth_iterator classmeth_end() const { return ClassMethods.end(); } + + // Location information, modeled after the Stmt API. SourceLocation getLocStart() const { return getLocation(); } SourceLocation getLocEnd() const { return EndLoc; } @@ -577,20 +577,11 @@ public: void setSuperClass(ObjcInterfaceDecl * superCls) { SuperClass = superCls; } - // FIXME: Figure out how to remove the const pointer below. - ObjcMethodDecl *const*getInstanceMethods() const { - return &InstanceMethods[0]; - } - int getNumInstanceMethods() const { return InstanceMethods.size(); } - - // FIXME: Figure out how to remove the const pointer below. - ObjcMethodDecl *const*getClassMethods() const { - return &ClassMethods[0]; - } - int getNumClassMethods() const { return ClassMethods.size(); } + unsigned getNumInstanceMethods() const { return InstanceMethods.size(); } + unsigned getNumClassMethods() const { return ClassMethods.size(); } ObjcIvarDecl **getImplDeclIVars() const { return Ivars; } - int getImplDeclNumIvars() const { return NumIvars; } + unsigned getImplDeclNumIvars() const { return NumIvars; } typedef llvm::SmallVector<ObjcMethodDecl*, 32>::const_iterator |