From: Chris Lattner Date: Sun, 7 Oct 2007 01:33:16 +0000 (+0000) Subject: simplify some Selector interfaces. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f836e3fea2c77fdbb18170fb313ee0d45551320b;p=clang simplify some Selector interfaces. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42715 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Lex/IdentifierTable.cpp b/Lex/IdentifierTable.cpp index f3a6cf7175..aba3f9d1f2 100644 --- a/Lex/IdentifierTable.cpp +++ b/Lex/IdentifierTable.cpp @@ -239,11 +239,9 @@ public: for (unsigned i = 0; i != nKeys; ++i) KeyInfo[i] = IIV[i]; } - // Derive the full selector name, placing the result into methodBuffer. - // As a convenience, a pointer to the first character is returned. - // Example usage: llvm::SmallString<128> mbuf; Selector->getName(mbuf); - char *getName(llvm::SmallVectorImpl &methodBuffer); - + // getName - Derive the full selector name and return it. + std::string getName() const; + unsigned getNumArgs() const { return NumArgs; } typedef IdentifierInfo *const *keyword_iterator; @@ -253,18 +251,15 @@ public: keyword_iterator keyword_end() const { return keyword_begin()+NumArgs; } - IdentifierInfo *getIdentifierInfoForSlot(unsigned i) { - assert((i < NumArgs) && "getIdentifierInfoForSlot(): illegal index"); + IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const { + assert(i < NumArgs && "getIdentifierInfoForSlot(): illegal index"); return keyword_begin()[i]; } static void Profile(llvm::FoldingSetNodeID &ID, keyword_iterator ArgTys, unsigned NumArgs) { ID.AddInteger(NumArgs); - if (NumArgs) { // handle keyword selector. - for (unsigned i = 0; i != NumArgs; ++i) - ID.AddPointer(ArgTys[i]); - } else // handle unary selector. - ID.AddPointer(ArgTys[0]); + for (unsigned i = 0; i != NumArgs; ++i) + ID.AddPointer(ArgTys[i]); } void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, keyword_begin(), NumArgs); @@ -283,10 +278,9 @@ unsigned Selector::getNumArgs() const { return SI->getNumArgs(); } -IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) { - IdentifierInfo *II = getAsIdentifierInfo(); - if (II) { - assert(((argIndex == 0) || (argIndex == 1)) && "illegal keyword index"); +IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const { + if (IdentifierInfo *II = getAsIdentifierInfo()) { + assert(argIndex == 0 && "illegal keyword index"); return II; } // We point to a MultiKeywordSelector (pointer doesn't contain any flags). @@ -294,39 +288,47 @@ IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) { return SI->getIdentifierInfoForSlot(argIndex); } -char *MultiKeywordSelector::getName(llvm::SmallVectorImpl &methodName) { - methodName[0] = '\0'; - keyword_iterator KeyIter = keyword_begin(); - for (unsigned int i = 0; i < NumArgs; i++) { - if (KeyIter[i]) { - unsigned KeyLen = KeyIter[i]->getLength(); - methodName.append(KeyIter[i]->getName(), KeyIter[i]->getName()+KeyLen); - } - methodName.push_back(':'); +std::string MultiKeywordSelector::getName() const { + std::string Result; + unsigned Length = 0; + for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) { + if (*I) + Length += (*I)->getLength(); + ++Length; // : + } + + Result.reserve(Length); + + for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) { + if (*I) + Result.insert(Result.end(), (*I)->getName(), + (*I)->getName()+(*I)->getLength()); + Result.push_back(':'); } - methodName.push_back('\0'); - return &methodName[0]; + + return Result; } -char *Selector::getName(llvm::SmallVectorImpl &methodName) { - methodName[0] = '\0'; - IdentifierInfo *II = getAsIdentifierInfo(); - if (II) { - unsigned NameLen = II->getLength(); - methodName.append(II->getName(), II->getName()+NameLen); - if (getNumArgs() == 1) - methodName.push_back(':'); - methodName.push_back('\0'); - } else { // We have a multiple keyword selector (no embedded flags). - MultiKeywordSelector *SI = reinterpret_cast(InfoPtr); - SI->getName(methodName); +std::string Selector::getName() const { + if (IdentifierInfo *II = getAsIdentifierInfo()) { + if (getNumArgs() == 0) + return II->getName(); + + std::string Res = II->getName(); + Res += ":"; + return Res; } - return &methodName[0]; + + // We have a multiple keyword selector (no embedded flags). + return reinterpret_cast(InfoPtr)->getName(); } -Selector SelectorTable::getKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) -{ +Selector SelectorTable::getKeywordSelector(unsigned nKeys, + IdentifierInfo **IIV) { + if (nKeys == 1) + return Selector(IIV[0], 1); + llvm::FoldingSet *SelTab; SelTab = static_cast *>(Impl); @@ -336,9 +338,9 @@ Selector SelectorTable::getKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) MultiKeywordSelector::Profile(ID, IIV, nKeys); void *InsertPos = 0; - if (MultiKeywordSelector *SI = SelTab->FindNodeOrInsertPos(ID, InsertPos)) { + if (MultiKeywordSelector *SI = SelTab->FindNodeOrInsertPos(ID, InsertPos)) return Selector(SI); - } + // MultiKeywordSelector objects are not allocated with new because they have a // variable size array (for parameter types) at the end of them. MultiKeywordSelector *SI = diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp index 6830d241f1..94230a79ee 100644 --- a/Sema/SemaDecl.cpp +++ b/Sema/SemaDecl.cpp @@ -1240,9 +1240,8 @@ void Sema::CheckProtocolMethodDefs(ObjcProtocolDecl *PDecl, for (int j = 0; j < PDecl->getNumInstanceMethods(); j++) { void * cpv = methods[j]->getSelector().getAsOpaquePtr(); if (!InsMap.count(cpv)) { - llvm::SmallString<128> buf; Diag(methods[j]->getLocation(), diag::warn_undef_method_impl, - methods[j]->getSelector().getName(buf)); + methods[j]->getSelector().getName()); IncompleteImpl = true; } } @@ -1250,9 +1249,8 @@ void Sema::CheckProtocolMethodDefs(ObjcProtocolDecl *PDecl, methods = PDecl->getClassMethods(); for (int j = 0; j < PDecl->getNumClassMethods(); j++) if (!ClsMap.count(methods[j]->getSelector())) { - llvm::SmallString<128> buf; Diag(methods[j]->getLocation(), diag::warn_undef_method_impl, - methods[j]->getSelector().getName(buf)); + methods[j]->getSelector().getName()); IncompleteImpl = true; } @@ -1275,9 +1273,8 @@ void Sema::ImplMethodsVsClassMethods(ObjcImplementationDecl* IMPDecl, methods = IDecl->getInstanceMethods(); for (int j = 0; j < IDecl->getNumInstanceMethods(); j++) if (!InsMap.count(methods[j]->getSelector().getAsOpaquePtr())) { - llvm::SmallString<128> buf; Diag(methods[j]->getLocation(), diag::warn_undef_method_impl, - methods[j]->getSelector().getName(buf)); + methods[j]->getSelector().getName()); IncompleteImpl = true; } llvm::DenseSet ClsMap; @@ -1290,9 +1287,8 @@ void Sema::ImplMethodsVsClassMethods(ObjcImplementationDecl* IMPDecl, methods = IDecl->getClassMethods(); for (int j = 0; j < IDecl->getNumClassMethods(); j++) if (!ClsMap.count(methods[j]->getSelector())) { - llvm::SmallString<128> buf; Diag(methods[j]->getLocation(), diag::warn_undef_method_impl, - methods[j]->getSelector().getName(buf)); + methods[j]->getSelector().getName()); IncompleteImpl = true; } @@ -1322,9 +1318,8 @@ void Sema::ImplCategoryMethodsVsIntfMethods(ObjcCategoryImplDecl *CatImplDecl, methods = CatClassDecl->getInstanceMethods(); for (int j = 0; j < CatClassDecl->getNumInstanceMethods(); j++) if (!InsMap.count(methods[j]->getSelector().getAsOpaquePtr())) { - llvm::SmallString<128> buf; Diag(methods[j]->getLocation(), diag::warn_undef_method_impl, - methods[j]->getSelector().getName(buf)); + methods[j]->getSelector().getName()); IncompleteImpl = true; } llvm::DenseSet ClsMap; @@ -1337,9 +1332,8 @@ void Sema::ImplCategoryMethodsVsIntfMethods(ObjcCategoryImplDecl *CatImplDecl, methods = CatClassDecl->getClassMethods(); for (int j = 0; j < CatClassDecl->getNumClassMethods(); j++) if (!ClsMap.count(methods[j]->getSelector())) { - llvm::SmallString<128> buf; Diag(methods[j]->getLocation(), diag::warn_undef_method_impl, - methods[j]->getSelector().getName(buf)); + methods[j]->getSelector().getName()); IncompleteImpl = true; } @@ -1739,9 +1733,8 @@ void Sema::ActOnAddMethodsToObjcDecl(Scope* S, DeclTy *classDecl, const ObjcMethodDecl *&PrevMethod = InsMap[Method->getSelector().getAsOpaquePtr()]; if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) { - llvm::SmallString<128> buf; Diag(Method->getLocation(), diag::error_duplicate_method_decl, - Method->getSelector().getName(buf)); + Method->getSelector().getName()); Diag(PrevMethod->getLocation(), diag::err_previous_declaration); } else { @@ -1758,9 +1751,8 @@ void Sema::ActOnAddMethodsToObjcDecl(Scope* S, DeclTy *classDecl, const ObjcMethodDecl *&PrevMethod = ClsMap[Method->getSelector().getAsOpaquePtr()]; if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) { - llvm::SmallString<128> buf; Diag(Method->getLocation(), diag::error_duplicate_method_decl, - Method->getSelector().getName(buf)); + Method->getSelector().getName()); Diag(PrevMethod->getLocation(), diag::err_previous_declaration); } else { diff --git a/include/clang/Lex/IdentifierTable.h b/include/clang/Lex/IdentifierTable.h index 9bb6d83cc5..a0f45b35e6 100644 --- a/include/clang/Lex/IdentifierTable.h +++ b/include/clang/Lex/IdentifierTable.h @@ -235,12 +235,11 @@ public: return getIdentifierInfoFlag() == ZeroArg; } unsigned getNumArgs() const; - IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex); + IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const; - // Derive the full selector name, placing the result into methodBuffer. - // As a convenience, a pointer to the first character is returned. - // Example usage: llvm::SmallString<128> mbuf; Selector->getName(mbuf); - char *getName(llvm::SmallVectorImpl &methodBuffer); + /// getName - Derive the full selector name (e.g. "foo:bar:") and return it. + /// + std::string getName() const; static Selector getEmptyMarker() { return Selector(uintptr_t(-1));