[cfe-commits] r42715 - in /cfe/trunk: Lex/IdentifierTable.cpp Sema/SemaDecl.cpp include/clang/Lex/IdentifierTable.h

Chris Lattner sabre at nondot.org
Sat Oct 6 18:33:16 PDT 2007


Author: lattner
Date: Sat Oct  6 20:33:16 2007
New Revision: 42715

URL: http://llvm.org/viewvc/llvm-project?rev=42715&view=rev
Log:
simplify some Selector interfaces.

Modified:
    cfe/trunk/Lex/IdentifierTable.cpp
    cfe/trunk/Sema/SemaDecl.cpp
    cfe/trunk/include/clang/Lex/IdentifierTable.h

Modified: cfe/trunk/Lex/IdentifierTable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Lex/IdentifierTable.cpp?rev=42715&r1=42714&r2=42715&view=diff

==============================================================================
--- cfe/trunk/Lex/IdentifierTable.cpp (original)
+++ cfe/trunk/Lex/IdentifierTable.cpp Sat Oct  6 20:33:16 2007
@@ -239,11 +239,9 @@
     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<char> &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 @@
   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 @@
   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 @@
   return SI->getIdentifierInfoForSlot(argIndex);
 }
 
-char *MultiKeywordSelector::getName(llvm::SmallVectorImpl<char> &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(':');
-  }
-  methodName.push_back('\0');
-  return &methodName[0];
-}
-
-char *Selector::getName(llvm::SmallVectorImpl<char> &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<MultiKeywordSelector *>(InfoPtr);
-    SI->getName(methodName);
+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(':');
+  }
+  
+  return Result;
+}
+
+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<MultiKeywordSelector *>(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<MultiKeywordSelector> *SelTab;
   
   SelTab = static_cast<llvm::FoldingSet<MultiKeywordSelector> *>(Impl);
@@ -336,9 +338,9 @@
   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 = 

Modified: cfe/trunk/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaDecl.cpp?rev=42715&r1=42714&r2=42715&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Sat Oct  6 20:33:16 2007
@@ -1240,9 +1240,8 @@
   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 @@
   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 @@
   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<Selector> ClsMap;
@@ -1290,9 +1287,8 @@
   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 @@
   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<Selector> ClsMap;
@@ -1337,9 +1332,8 @@
   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 @@
         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 @@
         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 {        

Modified: cfe/trunk/include/clang/Lex/IdentifierTable.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/IdentifierTable.h?rev=42715&r1=42714&r2=42715&view=diff

==============================================================================
--- cfe/trunk/include/clang/Lex/IdentifierTable.h (original)
+++ cfe/trunk/include/clang/Lex/IdentifierTable.h Sat Oct  6 20:33:16 2007
@@ -235,12 +235,11 @@
     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<char> &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));





More information about the cfe-commits mailing list