[llvm-commits] [llvm] r66895 - in /llvm/trunk/utils/TableGen: TGLexer.cpp TGLexer.h TGParser.cpp TGParser.h TGSourceMgr.cpp TGSourceMgr.h TableGen.cpp

Chris Lattner sabre at nondot.org
Fri Mar 13 09:01:54 PDT 2009


Author: lattner
Date: Fri Mar 13 11:01:53 2009
New Revision: 66895

URL: http://llvm.org/viewvc/llvm-project?rev=66895&view=rev
Log:
make "locations" a class instead of a typedef.

Modified:
    llvm/trunk/utils/TableGen/TGLexer.cpp
    llvm/trunk/utils/TableGen/TGLexer.h
    llvm/trunk/utils/TableGen/TGParser.cpp
    llvm/trunk/utils/TableGen/TGParser.h
    llvm/trunk/utils/TableGen/TGSourceMgr.cpp
    llvm/trunk/utils/TableGen/TGSourceMgr.h
    llvm/trunk/utils/TableGen/TableGen.cpp

Modified: llvm/trunk/utils/TableGen/TGLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TGLexer.cpp?rev=66895&r1=66894&r2=66895&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/TGLexer.cpp (original)
+++ llvm/trunk/utils/TableGen/TGLexer.cpp Fri Mar 13 11:01:53 2009
@@ -31,6 +31,10 @@
   TokStart = 0;
 }
 
+TGLoc TGLexer::getLoc() const {
+  return TGLoc::getFromPointer(TokStart);
+}
+
 
 /// ReturnError - Set the error to the specified string at the specified
 /// location.  This is defined to always return tgtok::Error.
@@ -40,10 +44,15 @@
 }
 
 
-void TGLexer::PrintError(LocTy Loc, const std::string &Msg) const {
+void TGLexer::PrintError(const char *Loc, const std::string &Msg) const {
+  SrcMgr.PrintError(TGLoc::getFromPointer(Loc), Msg);
+}
+
+void TGLexer::PrintError(TGLoc Loc, const std::string &Msg) const {
   SrcMgr.PrintError(Loc, Msg);
 }
 
+
 int TGLexer::getNextChar() {
   char CurChar = *CurPtr++;
   switch (CurChar) {
@@ -57,11 +66,11 @@
     
     // If this is the end of an included file, pop the parent file off the
     // include stack.
-    TGLocTy ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
-    if (ParentIncludeLoc != TGLocTy()) {
+    TGLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
+    if (ParentIncludeLoc != TGLoc()) {
       CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
       CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
-      CurPtr = ParentIncludeLoc;
+      CurPtr = ParentIncludeLoc.getPointer();
       return getNextChar();
     }
     
@@ -239,7 +248,7 @@
   }
   
   // Save the line number and lex buffer of the includer.
-  CurBuffer = SrcMgr.AddNewSourceBuffer(NewBuf, CurPtr);
+  CurBuffer = SrcMgr.AddNewSourceBuffer(NewBuf, TGLoc::getFromPointer(CurPtr));
   
   CurBuf = NewBuf;
   CurPtr = CurBuf->getBufferStart();

Modified: llvm/trunk/utils/TableGen/TGLexer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TGLexer.h?rev=66895&r1=66894&r2=66895&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/TGLexer.h (original)
+++ llvm/trunk/utils/TableGen/TGLexer.h Fri Mar 13 11:01:53 2009
@@ -23,6 +23,7 @@
 namespace llvm {
 class MemoryBuffer;
 class TGSourceMgr;
+class TGLoc;
   
 namespace tgtok {
   enum TokKind {
@@ -99,10 +100,10 @@
     return CurIntVal;
   }
 
-  typedef const char* LocTy;
-  LocTy getLoc() const { return TokStart; }
+  TGLoc getLoc() const;
 
-  void PrintError(LocTy Loc, const std::string &Msg) const;
+  void PrintError(const char *Loc, const std::string &Msg) const;
+  void PrintError(TGLoc Loc, const std::string &Msg) const;
   
 private:
   /// LexToken - Read the next token and return its code.

Modified: llvm/trunk/utils/TableGen/TGParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TGParser.cpp?rev=66895&r1=66894&r2=66895&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/TGParser.cpp (original)
+++ llvm/trunk/utils/TableGen/TGParser.cpp Fri Mar 13 11:01:53 2009
@@ -31,17 +31,17 @@
 };
   
 struct SubClassReference {
-  TGParser::LocTy RefLoc;
+  TGLoc RefLoc;
   Record *Rec;
   std::vector<Init*> TemplateArgs;
-  SubClassReference() : RefLoc(0), Rec(0) {}
+  SubClassReference() : Rec(0) {}
   
   bool isInvalid() const { return Rec == 0; }
 };
   
 } // end namespace llvm
 
-bool TGParser::AddValue(Record *CurRec, LocTy Loc, const RecordVal &RV) {
+bool TGParser::AddValue(Record *CurRec, TGLoc Loc, const RecordVal &RV) {
   if (CurRec == 0)
     CurRec = &CurMultiClass->Rec;
   
@@ -60,7 +60,7 @@
 
 /// SetValue -
 /// Return true on error, false on success.
-bool TGParser::SetValue(Record *CurRec, LocTy Loc, const std::string &ValName, 
+bool TGParser::SetValue(Record *CurRec, TGLoc Loc, const std::string &ValName, 
                         const std::vector<unsigned> &BitList, Init *V) {
   if (!V) return false;
 
@@ -357,7 +357,7 @@
   if (Lex.getCode() != tgtok::less)
     return false;
   
-  LocTy StartLoc = Lex.getLoc();
+  TGLoc StartLoc = Lex.getLoc();
   Lex.Lex(); // eat the '<'
   
   // Parse the range list.
@@ -379,7 +379,7 @@
   if (Lex.getCode() != tgtok::l_brace)
     return false;
   
-  LocTy StartLoc = Lex.getLoc();
+  TGLoc StartLoc = Lex.getLoc();
   Lex.Lex(); // eat the '{'
   
   // Parse the range list.
@@ -464,7 +464,7 @@
 Init *TGParser::ParseIDValue(Record *CurRec) {
   assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
   std::string Name = Lex.getCurStrVal();
-  LocTy Loc = Lex.getLoc();
+  TGLoc Loc = Lex.getLoc();
   Lex.Lex();
   return ParseIDValue(CurRec, Name, Loc);
 }
@@ -472,7 +472,7 @@
 /// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
 /// has already been read.
 Init *TGParser::ParseIDValue(Record *CurRec, 
-                             const std::string &Name, LocTy NameLoc) {
+                             const std::string &Name, TGLoc NameLoc) {
   if (CurRec) {
     if (const RecordVal *RV = CurRec->getValue(Name))
       return new VarInit(Name, RV->getType());
@@ -540,7 +540,7 @@
       R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break;
   case tgtok::question: R = new UnsetInit(); Lex.Lex(); break;
   case tgtok::Id: {
-    LocTy NameLoc = Lex.getLoc();
+    TGLoc NameLoc = Lex.getLoc();
     std::string Name = Lex.getCurStrVal();
     if (Lex.Lex() != tgtok::less)  // consume the Id.
       return ParseIDValue(CurRec, Name, NameLoc);    // Value ::= IDValue
@@ -585,7 +585,7 @@
     return new DefInit(NewRec);
   }    
   case tgtok::l_brace: {           // Value ::= '{' ValueList '}'
-    LocTy BraceLoc = Lex.getLoc();
+    TGLoc BraceLoc = Lex.getLoc();
     Lex.Lex(); // eat the '{'
     std::vector<Init*> Vals;
     
@@ -711,7 +711,7 @@
     switch (Lex.getCode()) {
     default: return Result;
     case tgtok::l_brace: {
-      LocTy CurlyLoc = Lex.getLoc();
+      TGLoc CurlyLoc = Lex.getLoc();
       Lex.Lex(); // eat the '{'
       std::vector<unsigned> Ranges = ParseRangeList();
       if (Ranges.empty()) return 0;
@@ -733,7 +733,7 @@
       break;
     }
     case tgtok::l_square: {
-      LocTy SquareLoc = Lex.getLoc();
+      TGLoc SquareLoc = Lex.getLoc();
       Lex.Lex(); // eat the '['
       std::vector<unsigned> Ranges = ParseRangeList();
       if (Ranges.empty()) return 0;
@@ -849,7 +849,7 @@
     return "";
   }
   
-  LocTy IdLoc = Lex.getLoc();
+  TGLoc IdLoc = Lex.getLoc();
   std::string DeclName = Lex.getCurStrVal();
   Lex.Lex();
   
@@ -870,7 +870,7 @@
   // If a value is present, parse it.
   if (Lex.getCode() == tgtok::equal) {
     Lex.Lex();
-    LocTy ValLoc = Lex.getLoc();
+    TGLoc ValLoc = Lex.getLoc();
     Init *Val = ParseValue(CurRec);
     if (Val == 0 ||
         SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
@@ -936,7 +936,7 @@
   if (Lex.Lex() != tgtok::Id)
     return TokError("expected field identifier after let");
   
-  LocTy IdLoc = Lex.getLoc();
+  TGLoc IdLoc = Lex.getLoc();
   std::string FieldName = Lex.getCurStrVal();
   Lex.Lex();  // eat the field name.
   
@@ -1034,7 +1034,7 @@
 ///   DefInst ::= DEF ObjectName ObjectBody
 ///
 llvm::Record *TGParser::ParseDef(MultiClass *CurMultiClass) {
-  LocTy DefLoc = Lex.getLoc();
+  TGLoc DefLoc = Lex.getLoc();
   assert(Lex.getCode() == tgtok::Def && "Unknown tok");
   Lex.Lex();  // Eat the 'def' token.  
 
@@ -1122,7 +1122,7 @@
       return std::vector<LetRecord>();
     }
     std::string Name = Lex.getCurStrVal();
-    LocTy NameLoc = Lex.getLoc();
+    TGLoc NameLoc = Lex.getLoc();
     Lex.Lex();  // Eat the identifier. 
 
     // Check for an optional RangeList.
@@ -1174,7 +1174,7 @@
     if (ParseObject())
       return true;
   } else {   // Object ::= LETCommand '{' ObjectList '}'
-    LocTy BraceLoc = Lex.getLoc();
+    TGLoc BraceLoc = Lex.getLoc();
     // Otherwise, this is a group let.
     Lex.Lex();  // eat the '{'.
     
@@ -1265,7 +1265,7 @@
   if (Lex.Lex() != tgtok::Id)  // eat the defm.
     return TokError("expected identifier after defm");
   
-  LocTy DefmPrefixLoc = Lex.getLoc();
+  TGLoc DefmPrefixLoc = Lex.getLoc();
   std::string DefmPrefix = Lex.getCurStrVal();
   if (Lex.Lex() != tgtok::colon)
     return TokError("expected ':' after defm identifier");
@@ -1273,7 +1273,7 @@
   // eat the colon.
   Lex.Lex();
 
-  LocTy SubClassLoc = Lex.getLoc();
+  TGLoc SubClassLoc = Lex.getLoc();
   SubClassReference Ref = ParseSubClassReference(0, true);
   if (Ref.Rec == 0) return true;
   

Modified: llvm/trunk/utils/TableGen/TGParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TGParser.h?rev=66895&r1=66894&r2=66895&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/TGParser.h (original)
+++ llvm/trunk/utils/TableGen/TGParser.h Fri Mar 13 11:01:53 2009
@@ -15,6 +15,7 @@
 #define TGPARSER_H
 
 #include "TGLexer.h"
+#include "TGSourceMgr.h"
 #include <map>
 
 namespace llvm {
@@ -29,9 +30,9 @@
     std::string Name;
     std::vector<unsigned> Bits;
     Init *Value;
-    TGLexer::LocTy Loc;
+    TGLoc Loc;
     LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
-              TGLexer::LocTy L)
+              TGLoc L)
       : Name(N), Bits(B), Value(V), Loc(L) {
     }
   };
@@ -45,7 +46,6 @@
   /// current value.
   MultiClass *CurMultiClass;
 public:
-  typedef TGLexer::LocTy LocTy;
   
   TGParser(TGSourceMgr &SrcMgr) : Lex(SrcMgr), CurMultiClass(0) {}
   
@@ -55,7 +55,7 @@
   /// routines return true on error, or false on success.
   bool ParseFile();
   
-  bool Error(LocTy L, const std::string &Msg) const {
+  bool Error(TGLoc L, const std::string &Msg) const {
     Lex.PrintError(L, Msg);
     return true;
   }
@@ -63,8 +63,8 @@
     return Error(Lex.getLoc(), Msg);
   }
 private:  // Semantic analysis methods.
-  bool AddValue(Record *TheRec, LocTy Loc, const RecordVal &RV);
-  bool SetValue(Record *TheRec, LocTy Loc, const std::string &ValName, 
+  bool AddValue(Record *TheRec, TGLoc Loc, const RecordVal &RV);
+  bool SetValue(Record *TheRec, TGLoc Loc, const std::string &ValName, 
                 const std::vector<unsigned> &BitList, Init *V);
   bool AddSubClass(Record *Rec, SubClassReference &SubClass);
 
@@ -89,7 +89,7 @@
   SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
 
   Init *ParseIDValue(Record *CurRec);
-  Init *ParseIDValue(Record *CurRec, const std::string &Name, LocTy NameLoc);
+  Init *ParseIDValue(Record *CurRec, const std::string &Name, TGLoc NameLoc);
   Init *ParseSimpleValue(Record *CurRec);
   Init *ParseValue(Record *CurRec);
   std::vector<Init*> ParseValueList(Record *CurRec);

Modified: llvm/trunk/utils/TableGen/TGSourceMgr.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TGSourceMgr.cpp?rev=66895&r1=66894&r2=66895&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/TGSourceMgr.cpp (original)
+++ llvm/trunk/utils/TableGen/TGSourceMgr.cpp Fri Mar 13 11:01:53 2009
@@ -25,17 +25,17 @@
 
 /// FindBufferContainingLoc - Return the ID of the buffer containing the
 /// specified location, returning -1 if not found.
-int TGSourceMgr::FindBufferContainingLoc(TGLocTy Loc) const {
+int TGSourceMgr::FindBufferContainingLoc(TGLoc Loc) const {
   for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
-    if (Loc >= Buffers[i].Buffer->getBufferStart() &&
-        Loc <  Buffers[i].Buffer->getBufferEnd())
+    if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
+        Loc.getPointer() <  Buffers[i].Buffer->getBufferEnd())
       return i;
   return -1;
 }
 
 /// FindLineNumber - Find the line number for the specified location in the
 /// specified file.  This is not a fast method.
-unsigned TGSourceMgr::FindLineNumber(TGLocTy Loc, int BufferID) const {
+unsigned TGSourceMgr::FindLineNumber(TGLoc Loc, int BufferID) const {
   if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
   assert(BufferID != -1 && "Invalid Location!");
   
@@ -47,13 +47,13 @@
   
   const char *Ptr = Buff->getBufferStart();
 
-  for (; Ptr != Loc; ++Ptr)
+  for (; TGLoc::getFromPointer(Ptr) != Loc; ++Ptr)
     if (*Ptr == '\n') ++LineNo;
   return LineNo;
 }
 
-void TGSourceMgr::PrintIncludeStack(TGLocTy IncludeLoc) const {
-  if (IncludeLoc == TGLocTy()) return;  // Top of stack.
+void TGSourceMgr::PrintIncludeStack(TGLoc IncludeLoc) const {
+  if (IncludeLoc == TGLoc()) return;  // Top of stack.
   
   int CurBuf = FindBufferContainingLoc(IncludeLoc);
   assert(CurBuf != -1 && "Invalid or unspecified location!");
@@ -66,7 +66,7 @@
 }
 
 
-void TGSourceMgr::PrintError(TGLocTy ErrorLoc, const std::string &Msg) const {
+void TGSourceMgr::PrintError(TGLoc ErrorLoc, const std::string &Msg) const {
   raw_ostream &OS = errs();
   
   // First thing to do: find the current buffer containing the specified
@@ -83,22 +83,21 @@
      << FindLineNumber(ErrorLoc, CurBuf) << ": ";
   
   OS << Msg << "\n";
-  assert(ErrorLoc && "Location not specified!");
   
   // Scan backward to find the start of the line.
-  const char *LineStart = ErrorLoc;
+  const char *LineStart = ErrorLoc.getPointer();
   while (LineStart != CurMB->getBufferStart() && 
          LineStart[-1] != '\n' && LineStart[-1] != '\r')
     --LineStart;
   // Get the end of the line.
-  const char *LineEnd = ErrorLoc;
+  const char *LineEnd = ErrorLoc.getPointer();
   while (LineEnd != CurMB->getBufferEnd() && 
          LineEnd[0] != '\n' && LineEnd[0] != '\r')
     ++LineEnd;
   // Print out the line.
   OS << std::string(LineStart, LineEnd) << "\n";
   // Print out spaces before the carat.
-  for (const char *Pos = LineStart; Pos != ErrorLoc; ++Pos)
+  for (const char *Pos = LineStart; Pos != ErrorLoc.getPointer(); ++Pos)
     OS << (*Pos == '\t' ? '\t' : ' ');
   OS << "^\n";
 }

Modified: llvm/trunk/utils/TableGen/TGSourceMgr.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TGSourceMgr.h?rev=66895&r1=66894&r2=66895&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/TGSourceMgr.h (original)
+++ llvm/trunk/utils/TableGen/TGSourceMgr.h Fri Mar 13 11:01:53 2009
@@ -20,9 +20,25 @@
 
 namespace llvm {
   class MemoryBuffer;
+  class TGSourceMgr;
   
-/// FIXME: Make this a struct that is opaque.
-typedef const char *TGLocTy;
+class TGLoc {
+  const char *Ptr;
+public:
+  TGLoc() : Ptr(0) {}
+  TGLoc(const TGLoc &RHS) : Ptr(RHS.Ptr) {}
+  
+  bool operator==(const TGLoc &RHS) const { return RHS.Ptr == Ptr; }
+  bool operator!=(const TGLoc &RHS) const { return RHS.Ptr != Ptr; }
+
+  const char *getPointer() const { return Ptr; }
+  
+  static TGLoc getFromPointer(const char *Ptr) {
+    TGLoc L;
+    L.Ptr = Ptr;
+    return L;
+  }
+};
 
 /// TGSourceMgr - This owns the files read by tblgen, handles include stacks,
 /// and handles printing of diagnostics.
@@ -33,7 +49,7 @@
     
     /// IncludeLoc - This is the location of the parent include, or null if at
     /// the top level.
-    TGLocTy IncludeLoc;
+    TGLoc IncludeLoc;
   };
   
   /// Buffers - This is all of the buffers that we are reading from.
@@ -55,12 +71,12 @@
     return Buffers[i].Buffer;
   }
   
-  TGLocTy getParentIncludeLoc(unsigned i) const {
+  TGLoc getParentIncludeLoc(unsigned i) const {
     assert(i < Buffers.size() && "Invalid Buffer ID!");
     return Buffers[i].IncludeLoc;
   }
   
-  unsigned AddNewSourceBuffer(MemoryBuffer *F, TGLocTy IncludeLoc) {
+  unsigned AddNewSourceBuffer(MemoryBuffer *F, TGLoc IncludeLoc) {
     SrcBuffer NB;
     NB.Buffer = F;
     NB.IncludeLoc = IncludeLoc;
@@ -70,19 +86,19 @@
   
   /// FindBufferContainingLoc - Return the ID of the buffer containing the
   /// specified location, returning -1 if not found.
-  int FindBufferContainingLoc(TGLocTy Loc) const;
+  int FindBufferContainingLoc(TGLoc Loc) const;
   
   /// FindLineNumber - Find the line number for the specified location in the
   /// specified file.  This is not a fast method.
-  unsigned FindLineNumber(TGLocTy Loc, int BufferID = -1) const;
+  unsigned FindLineNumber(TGLoc Loc, int BufferID = -1) const;
   
   
   /// PrintError - Emit an error message about the specified location with the
   /// specified string.
-  void PrintError(TGLocTy ErrorLoc, const std::string &Msg) const;
+  void PrintError(TGLoc ErrorLoc, const std::string &Msg) const;
   
 private:
-  void PrintIncludeStack(TGLocTy IncludeLoc) const;
+  void PrintIncludeStack(TGLoc IncludeLoc) const;
 };
   
 }  // end llvm namespace

Modified: llvm/trunk/utils/TableGen/TableGen.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TableGen.cpp?rev=66895&r1=66894&r2=66895&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/TableGen.cpp (original)
+++ llvm/trunk/utils/TableGen/TableGen.cpp Fri Mar 13 11:01:53 2009
@@ -17,7 +17,6 @@
 
 #include "Record.h"
 #include "TGParser.h"
-#include "TGSourceMgr.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Streams.h"
 #include "llvm/System/Signals.h"
@@ -124,7 +123,7 @@
   }
   
   // Tell SrcMgr about this buffer, which is what TGParser will pick up.
-  SrcMgr.AddNewSourceBuffer(F, TGLocTy());
+  SrcMgr.AddNewSourceBuffer(F, TGLoc());
   
   TGParser Parser(SrcMgr);
 





More information about the llvm-commits mailing list