[cfe-commits] r53876 - in /cfe/trunk: Driver/RewriteObjC.cpp include/clang/AST/DeclObjC.h lib/AST/DeclObjC.cpp lib/CodeGen/CodeGenModule.cpp lib/Sema/SemaDeclObjC.cpp lib/Sema/SemaExprObjC.cpp
Chris Lattner
sabre at nondot.org
Mon Jul 21 14:32:27 PDT 2008
Author: lattner
Date: Mon Jul 21 16:32:27 2008
New Revision: 53876
URL: http://llvm.org/viewvc/llvm-project?rev=53876&view=rev
Log:
move two more lists of protocols over to use ObjCList<ObjCProtocolDecl>,
simplifying code along the way and fixing a problem and memory leak or two.
Modified:
cfe/trunk/Driver/RewriteObjC.cpp
cfe/trunk/include/clang/AST/DeclObjC.h
cfe/trunk/lib/AST/DeclObjC.cpp
cfe/trunk/lib/CodeGen/CodeGenModule.cpp
cfe/trunk/lib/Sema/SemaDeclObjC.cpp
cfe/trunk/lib/Sema/SemaExprObjC.cpp
Modified: cfe/trunk/Driver/RewriteObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/RewriteObjC.cpp?rev=53876&r1=53875&r2=53876&view=diff
==============================================================================
--- cfe/trunk/Driver/RewriteObjC.cpp (original)
+++ cfe/trunk/Driver/RewriteObjC.cpp Mon Jul 21 16:32:27 2008
@@ -217,8 +217,8 @@
const char *ClassName,
std::string &Result);
- void RewriteObjCProtocolsMetaData(ObjCProtocolDecl *const*Protocols,
- int NumProtocols,
+ void RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl>
+ &Protocols,
const char *prefix,
const char *ClassName,
std::string &Result);
@@ -2524,14 +2524,14 @@
}
/// RewriteObjCProtocolsMetaData - Rewrite protocols meta-data.
-void RewriteObjC::RewriteObjCProtocolsMetaData(ObjCProtocolDecl*const*Protocols,
- int NumProtocols,
- const char *prefix,
- const char *ClassName,
- std::string &Result) {
+void RewriteObjC::
+RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl> &Protocols,
+ const char *prefix,
+ const char *ClassName,
+ std::string &Result) {
static bool objc_protocol_methods = false;
- if (NumProtocols > 0) {
- for (int i = 0; i < NumProtocols; i++) {
+ if (!Protocols.empty()) {
+ for (unsigned i = 0; i != Protocols.size(); i++) {
ObjCProtocolDecl *PDecl = Protocols[i];
// Output struct protocol_methods holder of method selector and type.
if (!objc_protocol_methods && !PDecl->isForwardDecl()) {
@@ -2680,24 +2680,23 @@
Result += "\tstruct _objc_protocol_list *next;\n";
Result += "\tint protocol_count;\n";
Result += "\tstruct _objc_protocol *class_protocols[";
- Result += utostr(NumProtocols);
+ Result += utostr(Protocols.size());
Result += "];\n} _OBJC_";
Result += prefix;
Result += "_PROTOCOLS_";
Result += ClassName;
Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= "
"{\n\t0, ";
- Result += utostr(NumProtocols);
+ Result += utostr(Protocols.size());
Result += "\n";
Result += "\t,{&_OBJC_PROTOCOL_";
Result += Protocols[0]->getName();
Result += " \n";
- for (int i = 1; i < NumProtocols; i++) {
- ObjCProtocolDecl *PDecl = Protocols[i];
+ for (unsigned i = 1; i != Protocols.size(); i++) {
Result += "\t ,&_OBJC_PROTOCOL_";
- Result += PDecl->getName();
+ Result += Protocols[i]->getName();
Result += "\n";
}
Result += "\t }\n};\n";
@@ -2733,9 +2732,7 @@
// Protocols referenced in class declaration?
// Null CDecl is case of a category implementation with no category interface
if (CDecl)
- RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(),
- CDecl->getNumReferencedProtocols(),
- "CATEGORY",
+ RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), "CATEGORY",
FullCategoryName.c_str(), Result);
/* struct _objc_category {
@@ -2789,7 +2786,7 @@
else
Result += "\t, 0\n";
- if (CDecl && CDecl->getNumReferencedProtocols() > 0) {
+ if (CDecl && !CDecl->getReferencedProtocols().empty()) {
Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
Result += FullCategoryName;
Result += "\n";
@@ -2915,8 +2912,7 @@
false, "", IDecl->getName(), Result);
// Protocols referenced in class declaration?
- RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols().begin(),
- CDecl->getReferencedProtocols().size(),
+ RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(),
"CLASS", CDecl->getName(), Result);
Modified: cfe/trunk/include/clang/AST/DeclObjC.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclObjC.h?rev=53876&r1=53875&r2=53876&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclObjC.h (original)
+++ cfe/trunk/include/clang/AST/DeclObjC.h Mon Jul 21 16:32:27 2008
@@ -63,7 +63,7 @@
unsigned size() const { return NumElts; }
bool empty() const { return NumElts == 0; }
- T* get(unsigned idx) const {
+ T* operator[](unsigned idx) const {
assert(idx < NumElts && "Invalid access");
return List[idx];
}
@@ -356,11 +356,10 @@
return ClassMethods+NumClassMethods;
}
-
/// addReferencedProtocols - Set the list of protocols that this interface
/// implements.
- void addReferencedProtocols(ObjCProtocolDecl * const *OID, unsigned NumRPs) {
- ReferencedProtocols.set(OID, NumRPs);
+ void addReferencedProtocols(ObjCProtocolDecl *const*List, unsigned NumRPs) {
+ ReferencedProtocols.set(List, NumRPs);
}
void addInstanceVariablesToClass(ObjCIvarDecl **ivars, unsigned numIvars,
@@ -526,9 +525,8 @@
/// id <NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
///
class ObjCProtocolDecl : public NamedDecl {
- /// referenced protocols
- ObjCProtocolDecl **ReferencedProtocols; // Null if none
- unsigned NumReferencedProtocols; // 0 if none
+ /// Referenced protocols
+ ObjCList<ObjCProtocolDecl> ReferencedProtocols;
/// protocol instance methods
ObjCMethodDecl **InstanceMethods; // Null if not defined
@@ -547,14 +545,12 @@
SourceLocation EndLoc; // marks the '>' or identifier.
SourceLocation AtEndLoc; // marks the end of the entire interface.
- ObjCProtocolDecl(SourceLocation L, unsigned numRefProtos, IdentifierInfo *Id)
+ ObjCProtocolDecl(SourceLocation L, IdentifierInfo *Id)
: NamedDecl(ObjCProtocol, L, Id),
- ReferencedProtocols(0), NumReferencedProtocols(0),
InstanceMethods(0), NumInstanceMethods(0),
ClassMethods(0), NumClassMethods(0),
PropertyDecl(0), NumPropertyDecl(0),
isForwardProtoDecl(true) {
- AllocReferencedProtocols(numRefProtos);
}
virtual ~ObjCProtocolDecl();
@@ -565,33 +561,23 @@
virtual void Destroy(ASTContext& C);
static ObjCProtocolDecl *Create(ASTContext &C, SourceLocation L,
- unsigned numRefProtos, IdentifierInfo *Id);
+ IdentifierInfo *Id);
- void AllocReferencedProtocols(unsigned numRefProtos) {
- if (numRefProtos) {
- ReferencedProtocols = new ObjCProtocolDecl*[numRefProtos];
- memset(ReferencedProtocols, '\0',
- numRefProtos*sizeof(ObjCProtocolDecl*));
- NumReferencedProtocols = numRefProtos;
- }
- }
void addMethods(ObjCMethodDecl **insMethods, unsigned numInsMembers,
ObjCMethodDecl **clsMethods, unsigned numClsMembers,
SourceLocation AtEndLoc);
- void setReferencedProtocols(unsigned idx, ObjCProtocolDecl *OID) {
- assert((idx < NumReferencedProtocols) && "index out of range");
- ReferencedProtocols[idx] = OID;
- }
-
- ObjCProtocolDecl** getReferencedProtocols() const {
- return ReferencedProtocols;
+ const ObjCList<ObjCProtocolDecl> &getReferencedProtocols() const {
+ return ReferencedProtocols;
}
- unsigned getNumReferencedProtocols() const { return NumReferencedProtocols; }
typedef ObjCProtocolDecl * const * protocol_iterator;
- protocol_iterator protocol_begin() const { return ReferencedProtocols; }
- protocol_iterator protocol_end() const {
- return ReferencedProtocols+NumReferencedProtocols;
+ protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
+ protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
+
+ /// addReferencedProtocols - Set the list of protocols that this interface
+ /// implements.
+ void addReferencedProtocols(ObjCProtocolDecl *const*List, unsigned NumRPs) {
+ ReferencedProtocols.set(List, NumRPs);
}
unsigned getNumInstanceMethods() const { return NumInstanceMethods; }
@@ -783,8 +769,7 @@
ObjCInterfaceDecl *ClassInterface;
/// referenced protocols in this category.
- ObjCProtocolDecl **ReferencedProtocols; // Null if none
- unsigned NumReferencedProtocols; // 0 if none
+ ObjCList<ObjCProtocolDecl> ReferencedProtocols;
/// category instance methods
ObjCMethodDecl **InstanceMethods; // Null if not defined
@@ -806,7 +791,7 @@
ObjCCategoryDecl(SourceLocation L, IdentifierInfo *Id)
: NamedDecl(ObjCCategory, L, Id),
- ClassInterface(0), ReferencedProtocols(0), NumReferencedProtocols(0),
+ ClassInterface(0),
InstanceMethods(0), NumInstanceMethods(0),
ClassMethods(0), NumClassMethods(0),
NextClassCategory(0), PropertyDecl(0), NumPropertyDecl(0) {
@@ -822,12 +807,19 @@
/// addReferencedProtocols - Set the list of protocols that this interface
/// implements.
- void addReferencedProtocols(ObjCProtocolDecl **List, unsigned NumRPs);
+ void addReferencedProtocols(ObjCProtocolDecl *const*List, unsigned NumRPs) {
+ ReferencedProtocols.set(List, NumRPs);
+ }
- ObjCProtocolDecl **getReferencedProtocols() const {
- return ReferencedProtocols;
+ const ObjCList<ObjCProtocolDecl> &getReferencedProtocols() const {
+ return ReferencedProtocols;
}
- unsigned getNumReferencedProtocols() const { return NumReferencedProtocols; }
+
+ typedef ObjCProtocolDecl * const * protocol_iterator;
+ protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
+ protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
+
+
unsigned getNumInstanceMethods() const { return NumInstanceMethods; }
unsigned getNumClassMethods() const { return NumClassMethods; }
Modified: cfe/trunk/lib/AST/DeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclObjC.cpp?rev=53876&r1=53875&r2=53876&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclObjC.cpp (original)
+++ cfe/trunk/lib/AST/DeclObjC.cpp Mon Jul 21 16:32:27 2008
@@ -94,14 +94,12 @@
ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C,
SourceLocation L,
- unsigned numRefProtos,
IdentifierInfo *Id) {
void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>();
- return new (Mem) ObjCProtocolDecl(L, numRefProtos, Id);
+ return new (Mem) ObjCProtocolDecl(L, Id);
}
ObjCProtocolDecl::~ObjCProtocolDecl() {
- delete [] ReferencedProtocols;
delete [] InstanceMethods;
delete [] ClassMethods;
delete [] PropertyDecl;
@@ -440,15 +438,6 @@
AtEndLoc = endLoc;
}
-void ObjCCategoryDecl::addReferencedProtocols(ObjCProtocolDecl **List,
- unsigned NumRPs) {
- assert(NumReferencedProtocols == 0 && "Protocol list already set");
- if (NumRPs == 0) return;
-
- ReferencedProtocols = new ObjCProtocolDecl*[NumRPs];
- memcpy(ReferencedProtocols, List, NumRPs*sizeof(ObjCProtocolDecl*));
- NumReferencedProtocols = NumRPs;
-}
/// addMethods - Insert instance and methods declarations into
@@ -557,14 +546,11 @@
return MethodDecl;
// Didn't find one yet - look through protocols.
- const ObjCList<ObjCProtocolDecl> &Protocols =
- ClassDecl->getReferencedProtocols();
-
- for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
- E = Protocols.end(); I != E; ++I) {
+ for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
+ E = ClassDecl->protocol_end(); I != E; ++I)
if ((MethodDecl = (*I)->getClassMethod(Sel)))
return MethodDecl;
- }
+
// Didn't find one yet - now look through categories.
ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
while (CatDecl) {
@@ -627,14 +613,9 @@
if ((MethodDecl = getInstanceMethod(Sel)))
return MethodDecl;
- if (getNumReferencedProtocols() > 0) {
- ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
-
- for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
- if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
- return MethodDecl;
- }
- }
+ for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
+ if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
+ return MethodDecl;
return NULL;
}
@@ -646,14 +627,9 @@
if ((MethodDecl = getClassMethod(Sel)))
return MethodDecl;
- if (getNumReferencedProtocols() > 0) {
- ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
-
- for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
- if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
- return MethodDecl;
- }
- }
+ for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
+ if ((MethodDecl = (*I)->getClassMethod(Sel)))
+ return MethodDecl;
return NULL;
}
Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=53876&r1=53875&r2=53876&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Mon Jul 21 16:32:27 2008
@@ -362,14 +362,15 @@
}
void CodeGenModule::EmitObjCProtocolImplementation(const ObjCProtocolDecl *PD){
llvm::SmallVector<std::string, 16> Protocols;
- for (unsigned i = 0, e = PD->getNumReferencedProtocols() ; i < e ; i++)
- Protocols.push_back(PD->getReferencedProtocols()[i]->getName());
+ for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
+ E = PD->protocol_end(); PI != E; ++PI)
+ Protocols.push_back((*PI)->getName());
llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
- endIter = PD->instmeth_end() ; iter != endIter ; iter++) {
+ E = PD->instmeth_end(); iter != E; iter++) {
std::string TypeStr;
- Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
+ Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
InstanceMethodNames.push_back(
GetAddrOfConstantString((*iter)->getSelector().getName()));
InstanceMethodTypes.push_back(GetAddrOfConstantString(TypeStr));
Modified: cfe/trunk/lib/Sema/SemaDeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclObjC.cpp?rev=53876&r1=53875&r2=53876&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Mon Jul 21 16:32:27 2008
@@ -209,18 +209,17 @@
}
PDecl->setForwardDecl(false);
- PDecl->AllocReferencedProtocols(NumProtoRefs);
} else {
- PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs,
- ProtocolName);
+ PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
PDecl->setForwardDecl(false);
ObjCProtocols[ProtocolName] = PDecl;
}
if (NumProtoRefs) {
/// Check then save referenced protocols.
+ llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols;
for (unsigned int i = 0; i != NumProtoRefs; i++) {
- ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
+ ObjCProtocolDecl *RefPDecl = ObjCProtocols[ProtoRefNames[i]];
if (!RefPDecl)
Diag(ProtocolLoc, diag::err_undef_protocolref,
ProtoRefNames[i]->getName(), ProtocolName->getName());
@@ -228,9 +227,11 @@
if (RefPDecl->isForwardDecl())
Diag(ProtocolLoc, diag::warn_undef_protocolref,
ProtoRefNames[i]->getName(), ProtocolName->getName());
- PDecl->setReferencedProtocols(i, RefPDecl);
+ Protocols.push_back(RefPDecl);
}
}
+ if (!Protocols.empty())
+ PDecl->addReferencedProtocols(&Protocols[0], Protocols.size());
PDecl->setLocEnd(EndProtoLoc);
}
return PDecl;
@@ -389,7 +390,7 @@
ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
if (PDecl == 0) { // Not already seen?
// FIXME: Pass in the location of the identifier!
- PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident);
+ PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, Ident);
}
Protocols.push_back(PDecl);
@@ -627,10 +628,10 @@
method->getImplementationControl() != ObjCMethodDecl::Optional)
WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
}
- // Check on this protocols's referenced protocols, recursively
- ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
- for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
- CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
+ // Check on this protocols's referenced protocols, recursively.
+ for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
+ E = PDecl->protocol_end(); PI != E; ++PI)
+ CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap);
}
void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
@@ -702,12 +703,10 @@
// Check the protocol list for unimplemented methods in the @implementation
// class.
- ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
- for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
- ObjCProtocolDecl* PDecl = protocols[i];
- CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
+ for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
+ E = CatClassDecl->protocol_end(); PI != E; ++PI)
+ CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
InsMap, ClsMap);
- }
}
/// ActOnForwardClassDeclaration -
Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=53876&r1=53875&r2=53876&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Mon Jul 21 16:32:27 2008
@@ -359,9 +359,9 @@
ObjCProtocolDecl *rProto) {
if (lProto == rProto)
return true;
- ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
- for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
- if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
+ for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
+ E = rProto->protocol_end(); PI != E; ++PI)
+ if (ProtocolCompatibleWithProtocol(lProto, *PI))
return true;
return false;
}
@@ -396,11 +396,10 @@
if (lookupCategory)
for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
CDecl = CDecl->getNextClassCategory()) {
- ObjCProtocolDecl **protoList = CDecl->getReferencedProtocols();
- for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
- if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
+ for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
+ E = CDecl->protocol_end(); PI != E; ++PI)
+ if (ProtocolCompatibleWithProtocol(lProto, *PI))
return true;
- }
}
// 3rd, look up the super class(s)
More information about the cfe-commits
mailing list