resolve some fixmes and clean up some code by eliminating the get*Vars apis to some classes and use iterators instead.

llvm-svn: 44927
This commit is contained in:
Chris Lattner 2007-12-12 07:46:12 +00:00
parent 854f3167c0
commit 31bc07e6cf
4 changed files with 89 additions and 121 deletions

View File

@ -502,13 +502,10 @@ ObjcMethodDecl *ObjcImplementationDecl::lookupInstanceMethod(Selector Sel) {
/// the class implementation. Unlike interfaces, we don't look outside the /// the class implementation. Unlike interfaces, we don't look outside the
/// implementation. /// implementation.
ObjcMethodDecl *ObjcImplementationDecl::lookupClassMethod(Selector Sel) { ObjcMethodDecl *ObjcImplementationDecl::lookupClassMethod(Selector Sel) {
ObjcMethodDecl *const*methods = getClassMethods(); for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
int methodCount = getNumClassMethods(); I != E; ++I)
for (int i = 0; i < methodCount; ++i) { if ((*I)->getSelector() == Sel)
if (methods[i]->getSelector() == Sel) { return *I;
return methods[i];
}
}
return NULL; return NULL;
} }
@ -516,13 +513,9 @@ ObjcMethodDecl *ObjcImplementationDecl::lookupClassMethod(Selector Sel) {
// the class implementation. Unlike interfaces, we don't look outside the // the class implementation. Unlike interfaces, we don't look outside the
// implementation. // implementation.
ObjcMethodDecl *ObjcCategoryImplDecl::lookupInstanceMethod(Selector &Sel) { ObjcMethodDecl *ObjcCategoryImplDecl::lookupInstanceMethod(Selector &Sel) {
ObjcMethodDecl *const*methods = getInstanceMethods(); for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
int methodCount = getNumInstanceMethods(); if ((*I)->getSelector() == Sel)
for (int i = 0; i < methodCount; ++i) { return *I;
if (methods[i]->getSelector() == Sel) {
return methods[i];
}
}
return NULL; return NULL;
} }
@ -530,13 +523,10 @@ ObjcMethodDecl *ObjcCategoryImplDecl::lookupInstanceMethod(Selector &Sel) {
// the class implementation. Unlike interfaces, we don't look outside the // the class implementation. Unlike interfaces, we don't look outside the
// implementation. // implementation.
ObjcMethodDecl *ObjcCategoryImplDecl::lookupClassMethod(Selector &Sel) { ObjcMethodDecl *ObjcCategoryImplDecl::lookupClassMethod(Selector &Sel) {
ObjcMethodDecl *const*methods = getClassMethods(); for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
int methodCount = getNumClassMethods(); I != E; ++I)
for (int i = 0; i < methodCount; ++i) { if ((*I)->getSelector() == Sel)
if (methods[i]->getSelector() == Sel) { return *I;
return methods[i];
}
}
return NULL; return NULL;
} }

View File

@ -123,9 +123,10 @@ void DeclPrinter::PrintObjcImplementationDecl(ObjcImplementationDecl *OID) {
else else
Out << "@implementation " << I; Out << "@implementation " << I;
for (int i = 0; i < OID->getNumInstanceMethods(); i++) { for (ObjcImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
PrintObjcMethodDecl(OID->getInstanceMethods()[i]); E = OID->instmeth_end(); I != E; ++I) {
ObjcMethodDecl *OMD = OID->getInstanceMethods()[i]; ObjcMethodDecl *OMD = *I;
PrintObjcMethodDecl(OMD);
if (OMD->getBody()) { if (OMD->getBody()) {
Out << ' '; Out << ' ';
OMD->getBody()->printPretty(Out); OMD->getBody()->printPretty(Out);
@ -133,9 +134,10 @@ void DeclPrinter::PrintObjcImplementationDecl(ObjcImplementationDecl *OID) {
} }
} }
for (int i = 0; i < OID->getNumClassMethods(); i++) { for (ObjcImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
PrintObjcMethodDecl(OID->getClassMethods()[i]); E = OID->classmeth_end(); I != E; ++I) {
ObjcMethodDecl *OMD = OID->getClassMethods()[i]; ObjcMethodDecl *OMD = *I;
PrintObjcMethodDecl(OMD);
if (OMD->getBody()) { if (OMD->getBody()) {
Out << ' '; Out << ' ';
OMD->getBody()->printPretty(Out); OMD->getBody()->printPretty(Out);

View File

@ -184,8 +184,9 @@ namespace {
void RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *CDecl, void RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *CDecl,
std::string &Result); std::string &Result);
void RewriteObjcMethodsMetaData(ObjcMethodDecl *const*Methods, typedef ObjcCategoryImplDecl::instmeth_iterator instmeth_iterator;
int NumMethods, void RewriteObjcMethodsMetaData(instmeth_iterator MethodBegin,
instmeth_iterator MethodEnd,
bool IsInstanceMethod, bool IsInstanceMethod,
const char *prefix, const char *prefix,
const char *ClassName, const char *ClassName,
@ -560,16 +561,11 @@ void RewriteTest::RewriteImplementationDecl(NamedDecl *OID) {
else else
Rewrite.InsertText(CID->getLocStart(), "// ", 3); Rewrite.InsertText(CID->getLocStart(), "// ", 3);
int numMethods = IMD ? IMD->getNumInstanceMethods() for (ObjcCategoryImplDecl::instmeth_iterator
: CID->getNumInstanceMethods(); I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(),
E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); I != E; ++I) {
for (int i = 0; i < numMethods; i++) {
std::string ResultStr; std::string ResultStr;
ObjcMethodDecl *OMD; ObjcMethodDecl *OMD = *I;
if (IMD)
OMD = IMD->getInstanceMethods()[i];
else
OMD = CID->getInstanceMethods()[i];
RewriteObjcMethodDecl(OMD, ResultStr); RewriteObjcMethodDecl(OMD, ResultStr);
SourceLocation LocStart = OMD->getLocStart(); SourceLocation LocStart = OMD->getLocStart();
SourceLocation LocEnd = OMD->getBody()->getLocStart(); SourceLocation LocEnd = OMD->getBody()->getLocStart();
@ -580,14 +576,11 @@ void RewriteTest::RewriteImplementationDecl(NamedDecl *OID) {
ResultStr.c_str(), ResultStr.size()); ResultStr.c_str(), ResultStr.size());
} }
numMethods = IMD ? IMD->getNumClassMethods() : CID->getNumClassMethods(); for (ObjcCategoryImplDecl::classmeth_iterator
for (int i = 0; i < numMethods; i++) { I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(),
E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); I != E; ++I) {
std::string ResultStr; std::string ResultStr;
ObjcMethodDecl *OMD; ObjcMethodDecl *OMD = *I;
if (IMD)
OMD = IMD->getClassMethods()[i];
else
OMD = CID->getClassMethods()[i];
RewriteObjcMethodDecl(OMD, ResultStr); RewriteObjcMethodDecl(OMD, ResultStr);
SourceLocation LocStart = OMD->getLocStart(); SourceLocation LocStart = OMD->getLocStart();
SourceLocation LocEnd = OMD->getBody()->getLocStart(); SourceLocation LocEnd = OMD->getBody()->getLocStart();
@ -1747,14 +1740,16 @@ void RewriteTest::SynthesizeObjcInternalStruct(ObjcInterfaceDecl *CDecl,
// RewriteObjcMethodsMetaData - Rewrite methods metadata for instance or // RewriteObjcMethodsMetaData - Rewrite methods metadata for instance or
/// class methods. /// class methods.
void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl *const*Methods, void RewriteTest::RewriteObjcMethodsMetaData(instmeth_iterator MethodBegin,
int NumMethods, instmeth_iterator MethodEnd,
bool IsInstanceMethod, bool IsInstanceMethod,
const char *prefix, const char *prefix,
const char *ClassName, const char *ClassName,
std::string &Result) { std::string &Result) {
if (MethodBegin == MethodEnd) return;
static bool objc_impl_method = false; static bool objc_impl_method = false;
if (NumMethods > 0 && !objc_impl_method) { if (!objc_impl_method) {
/* struct _objc_method { /* struct _objc_method {
SEL _cmd; SEL _cmd;
char *method_types; char *method_types;
@ -1779,40 +1774,39 @@ void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl *const*Methods,
Result += "\tstruct _objc_method method_list[];\n};\n"; Result += "\tstruct _objc_method method_list[];\n};\n";
objc_impl_method = true; objc_impl_method = true;
} }
// Build _objc_method_list for class's methods if needed // Build _objc_method_list for class's methods if needed
if (NumMethods > 0) { Result += "\nstatic struct _objc_method_list _OBJC_";
Result += "\nstatic struct _objc_method_list _OBJC_"; Result += prefix;
Result += prefix; Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; Result += "_METHODS_";
Result += "_METHODS_"; Result += ClassName;
Result += ClassName; Result += " __attribute__ ((section (\"__OBJC, __";
Result += " __attribute__ ((section (\"__OBJC, __"; Result += IsInstanceMethod ? "inst" : "cls";
Result += IsInstanceMethod ? "inst" : "cls"; Result += "_meth\")))= ";
Result += "_meth\")))= "; Result += "{\n\t0, " + utostr(MethodEnd-MethodBegin) + "\n";
Result += "{\n\t0, " + utostr(NumMethods) + "\n";
Result += "\t,{{(SEL)\""; Result += "\t,{{(SEL)\"";
Result += Methods[0]->getSelector().getName().c_str(); 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; std::string MethodTypeString;
Context->getObjcEncodingForMethodDecl(Methods[0], MethodTypeString); Context->getObjcEncodingForMethodDecl(*MethodBegin, MethodTypeString);
Result += "\", \""; Result += "\", \"";
Result += MethodTypeString; Result += MethodTypeString;
Result += "\", "; Result += "\", ";
Result += MethodInternalNames[Methods[0]]; Result += MethodInternalNames[*MethodBegin];
Result += "}\n"; 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. /// RewriteObjcProtocolsMetaData - Rewrite protocols meta-data.
@ -2000,16 +1994,12 @@ void RewriteTest::RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *IDecl,
sprintf(FullCategoryName, "%s_%s", ClassDecl->getName(), IDecl->getName()); sprintf(FullCategoryName, "%s_%s", ClassDecl->getName(), IDecl->getName());
// Build _objc_method_list for class's instance methods if needed // Build _objc_method_list for class's instance methods if needed
RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(), RewriteObjcMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(),
IDecl->getNumInstanceMethods(), true, "CATEGORY_", FullCategoryName, Result);
true,
"CATEGORY_", FullCategoryName, Result);
// Build _objc_method_list for class's class methods if needed // Build _objc_method_list for class's class methods if needed
RewriteObjcMethodsMetaData(IDecl->getClassMethods(), RewriteObjcMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
IDecl->getNumClassMethods(), false, "CATEGORY_", FullCategoryName, Result);
false,
"CATEGORY_", FullCategoryName, Result);
// Protocols referenced in class declaration? // Protocols referenced in class declaration?
// Null CDecl is case of a category implementation with no category interface // 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 // Build _objc_method_list for class's instance methods if needed
RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(), RewriteObjcMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(),
IDecl->getNumInstanceMethods(), true, "", IDecl->getName(), Result);
true,
"", IDecl->getName(), Result);
// Build _objc_method_list for class's class methods if needed // Build _objc_method_list for class's class methods if needed
RewriteObjcMethodsMetaData(IDecl->getClassMethods(), RewriteObjcMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
IDecl->getNumClassMethods(), false, "", IDecl->getName(), Result);
false,
"", IDecl->getName(), Result);
// Protocols referenced in class declaration? // Protocols referenced in class declaration?
RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(), RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
CDecl->getNumIntfRefProtocols(), CDecl->getNumIntfRefProtocols(),
"CLASS", "CLASS", CDecl->getName(), Result);
CDecl->getName(), Result);
// Declaration of class/meta-class metadata // Declaration of class/meta-class metadata

View File

@ -155,8 +155,7 @@ public:
// We also need to record the @end location. // We also need to record the @end location.
SourceLocation getAtEndLoc() const { return AtEndLoc; } SourceLocation getAtEndLoc() const { return AtEndLoc; }
const int getNumPropertyDecl() const { return NumPropertyDecl; } int getNumPropertyDecl() const { return NumPropertyDecl; }
int getNumPropertyDecl() { return NumPropertyDecl; }
void setNumPropertyDecl(int num) { NumPropertyDecl = num; } void setNumPropertyDecl(int num) { NumPropertyDecl = num; }
ObjcPropertyDecl **const getPropertyDecl() const { return PropertyDecl; } ObjcPropertyDecl **const getPropertyDecl() const { return PropertyDecl; }
@ -479,24 +478,14 @@ class ObjcCategoryImplDecl : public NamedDecl {
SourceLocation EndLoc; SourceLocation EndLoc;
public: public:
ObjcCategoryImplDecl(SourceLocation L, IdentifierInfo *Id, ObjcCategoryImplDecl(SourceLocation L, IdentifierInfo *Id,
ObjcInterfaceDecl *classInterface) ObjcInterfaceDecl *classInterface)
: NamedDecl(ObjcCategoryImpl, L, Id), : NamedDecl(ObjcCategoryImpl, L, Id), ClassInterface(classInterface) {}
ClassInterface(classInterface) {}
ObjcInterfaceDecl *getClassInterface() const { return ClassInterface; } ObjcInterfaceDecl *getClassInterface() const { return ClassInterface; }
// FIXME: Figure out how to remove the const pointer below. unsigned getNumInstanceMethods() const { return InstanceMethods.size(); }
ObjcMethodDecl *const*getInstanceMethods() const { unsigned getNumClassMethods() const { return ClassMethods.size(); }
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(); }
void addInstanceMethod(ObjcMethodDecl *method) { void addInstanceMethod(ObjcMethodDecl *method) {
InstanceMethods.push_back(method); InstanceMethods.push_back(method);
@ -507,6 +496,17 @@ public:
ObjcMethodDecl *lookupInstanceMethod(Selector &Sel); ObjcMethodDecl *lookupInstanceMethod(Selector &Sel);
ObjcMethodDecl *lookupClassMethod(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. // Location information, modeled after the Stmt API.
SourceLocation getLocStart() const { return getLocation(); } SourceLocation getLocStart() const { return getLocation(); }
SourceLocation getLocEnd() const { return EndLoc; } SourceLocation getLocEnd() const { return EndLoc; }
@ -577,20 +577,11 @@ public:
void setSuperClass(ObjcInterfaceDecl * superCls) void setSuperClass(ObjcInterfaceDecl * superCls)
{ SuperClass = superCls; } { SuperClass = superCls; }
// FIXME: Figure out how to remove the const pointer below. unsigned getNumInstanceMethods() const { return InstanceMethods.size(); }
ObjcMethodDecl *const*getInstanceMethods() const { unsigned getNumClassMethods() const { return ClassMethods.size(); }
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(); }
ObjcIvarDecl **getImplDeclIVars() const { return Ivars; } ObjcIvarDecl **getImplDeclIVars() const { return Ivars; }
int getImplDeclNumIvars() const { return NumIvars; } unsigned getImplDeclNumIvars() const { return NumIvars; }
typedef llvm::SmallVector<ObjcMethodDecl*, 32>::const_iterator typedef llvm::SmallVector<ObjcMethodDecl*, 32>::const_iterator