ARM VDUPLNfq and VDUPLNfd definitions can just be Pat<>s for VDUPLN32q
[oota-llvm.git] / utils / TableGen / TGLexer.cpp
index 264be4b98ba2ef85228f23013aa110084f1f7690..82d2b6491aaca36ec8bd6659e312214a9eef127c 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "TGLexer.h"
-#include "TGSourceMgr.h"
-#include "llvm/Support/Streams.h"
+#include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/MemoryBuffer.h"
-#include <ostream>
 #include "llvm/Config/config.h"
+#include "llvm/ADT/StringSwitch.h"
+#include "llvm/ADT/Twine.h"
 #include <cctype>
 #include <cstdio>
 #include <cstdlib>
 #include <cerrno>
 using namespace llvm;
 
-TGLexer::TGLexer(TGSourceMgr &SM) : SrcMgr(SM) {
+TGLexer::TGLexer(SourceMgr &SM) : SrcMgr(SM) {
   CurBuffer = 0;
   CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
   CurPtr = CurBuf->getBufferStart();
   TokStart = 0;
 }
 
-TGLoc TGLexer::getLoc() const {
-  return TGLoc::getFromPointer(TokStart);
+SMLoc TGLexer::getLoc() const {
+  return SMLoc::getFromPointer(TokStart);
 }
 
 
 /// ReturnError - Set the error to the specified string at the specified
 /// location.  This is defined to always return tgtok::Error.
-tgtok::TokKind TGLexer::ReturnError(const char *Loc, const std::string &Msg) {
+tgtok::TokKind TGLexer::ReturnError(const char *Loc, const Twine &Msg) {
   PrintError(Loc, Msg);
   return tgtok::Error;
 }
 
 
-void TGLexer::PrintError(const char *Loc, const std::string &Msg) const {
-  SrcMgr.PrintError(TGLoc::getFromPointer(Loc), Msg);
+void TGLexer::PrintError(const char *Loc, const Twine &Msg) const {
+  SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg, "error");
 }
 
-void TGLexer::PrintError(TGLoc Loc, const std::string &Msg) const {
-  SrcMgr.PrintError(Loc, Msg);
+void TGLexer::PrintError(SMLoc Loc, const Twine &Msg) const {
+  SrcMgr.PrintMessage(Loc, Msg, "error");
 }
 
 
@@ -66,8 +66,8 @@ int TGLexer::getNextChar() {
     
     // If this is the end of an included file, pop the parent file off the
     // include stack.
-    TGLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
-    if (ParentIncludeLoc != TGLoc()) {
+    SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
+    if (ParentIncludeLoc != SMLoc()) {
       CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
       CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
       CurPtr = ParentIncludeLoc.getPointer();
@@ -97,7 +97,7 @@ tgtok::TokKind TGLexer::LexToken() {
 
   switch (CurChar) {
   default:
-    // Handle letters: [a-zA-Z_]
+    // Handle letters: [a-zA-Z_#]
     if (isalpha(CurChar) || CurChar == '_' || CurChar == '#')
       return LexIdentifier();
       
@@ -216,23 +216,13 @@ tgtok::TokKind TGLexer::LexVarName() {
 
 
 tgtok::TokKind TGLexer::LexIdentifier() {
-  // The first letter is [a-zA-Z_].
+  // The first letter is [a-zA-Z_#].
   const char *IdentStart = TokStart;
   
-  // Match the rest of the identifier regex: [0-9a-zA-Z_]*
-  while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_'
-         || *CurPtr == '#') {
-    // If this contains a '#', make sure it's value
-    if (*CurPtr == '#') {
-      if (strncmp(CurPtr, "#NAME#", 6) != 0) {
-        return tgtok::Error;
-      }
-      CurPtr += 6;
-    }
-    else {
-      ++CurPtr;
-    }
-  }
+  // Match the rest of the identifier regex: [0-9a-zA-Z_#]*
+  while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_' ||
+         *CurPtr == '#')
+    ++CurPtr;
   
   
   // Check to see if this identifier is a keyword.
@@ -278,24 +268,15 @@ bool TGLexer::LexInclude() {
   // Get the string.
   std::string Filename = CurStrVal;
 
-  // Try to find the file.
-  MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str());
-
-  // If the file didn't exist directly, see if it's in an include path.
-  for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
-    std::string IncFile = IncludeDirectories[i] + "/" + Filename;
-    NewBuf = MemoryBuffer::getFile(IncFile.c_str());
-  }
-    
-  if (NewBuf == 0) {
+  
+  CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr));
+  if (CurBuffer == -1) {
     PrintError(getLoc(), "Could not find include file '" + Filename + "'");
     return true;
   }
   
   // Save the line number and lex buffer of the includer.
-  CurBuffer = SrcMgr.AddNewSourceBuffer(NewBuf, TGLoc::getFromPointer(CurPtr));
-  
-  CurBuf = NewBuf;
+  CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
   CurPtr = CurBuf->getBufferStart();
   return false;
 }
@@ -362,19 +343,19 @@ tgtok::TokKind TGLexer::LexNumber() {
       
       // Requires at least one hex digit.
       if (CurPtr == NumStart)
-        return ReturnError(CurPtr-2, "Invalid hexadecimal number");
+        return ReturnError(TokStart, "Invalid hexadecimal number");
 
       errno = 0;
       CurIntVal = strtoll(NumStart, 0, 16);
       if (errno == EINVAL)
-        return ReturnError(CurPtr-2, "Invalid hexadecimal number");
+        return ReturnError(TokStart, "Invalid hexadecimal number");
       if (errno == ERANGE) {
         errno = 0;
         CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
         if (errno == EINVAL)
-          return ReturnError(CurPtr-2, "Invalid hexadecimal number");
+          return ReturnError(TokStart, "Invalid hexadecimal number");
         if (errno == ERANGE)
-          return ReturnError(CurPtr-2, "Hexadecimal number out of range");
+          return ReturnError(TokStart, "Hexadecimal number out of range");
       }
       return tgtok::IntVal;
     } else if (CurPtr[0] == 'b') {
@@ -432,25 +413,30 @@ tgtok::TokKind TGLexer::LexBracket() {
 /// LexExclaim - Lex '!' and '![a-zA-Z]+'.
 tgtok::TokKind TGLexer::LexExclaim() {
   if (!isalpha(*CurPtr))
-    return ReturnError(CurPtr-1, "Invalid \"!operator\"");
+    return ReturnError(CurPtr - 1, "Invalid \"!operator\"");
   
   const char *Start = CurPtr++;
   while (isalpha(*CurPtr))
     ++CurPtr;
   
   // Check to see which operator this is.
-  unsigned Len = CurPtr-Start;
-  
-  if (Len == 3  && !memcmp(Start, "con", 3)) return tgtok::XConcat;
-  if (Len == 3  && !memcmp(Start, "sra", 3)) return tgtok::XSRA;
-  if (Len == 3  && !memcmp(Start, "srl", 3)) return tgtok::XSRL;
-  if (Len == 3  && !memcmp(Start, "shl", 3)) return tgtok::XSHL;
-  if (Len == 9  && !memcmp(Start, "strconcat", 9))   return tgtok::XStrConcat;
-  if (Len == 10 && !memcmp(Start, "nameconcat", 10)) return tgtok::XNameConcat;
-  if (Len == 5 && !memcmp(Start, "subst", 5)) return tgtok::XSubst;
-//   if (Len == 7 && !memcmp(Start, "foreach", 7)) return tgtok::XForEach;
-  if (Len == 4 && !memcmp(Start, "cast", 4)) return tgtok::XCast;
-
-  return ReturnError(Start-1, "Unknown operator");
+  tgtok::TokKind Kind =
+    StringSwitch<tgtok::TokKind>(StringRef(Start, CurPtr - Start))
+    .Case("eq", tgtok::XEq)
+    .Case("if", tgtok::XIf)
+    .Case("head", tgtok::XHead)
+    .Case("tail", tgtok::XTail)
+    .Case("con", tgtok::XConcat)
+    .Case("shl", tgtok::XSHL)
+    .Case("sra", tgtok::XSRA)
+    .Case("srl", tgtok::XSRL)
+    .Case("cast", tgtok::XCast)
+    .Case("empty", tgtok::XEmpty)
+    .Case("subst", tgtok::XSubst)
+    .Case("foreach", tgtok::XForEach)
+    .Case("strconcat", tgtok::XStrConcat)
+    .Default(tgtok::Error);
+
+  return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator");
 }