| OLD | NEW |
| 1 //===- TGLexer.cpp - Lexer for TableGen -----------------------------------===// | 1 //===- TGLexer.cpp - Lexer for TableGen -----------------------------------===// |
| 2 // | 2 // |
| 3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // Implement the Lexer for TableGen. | 10 // Implement the Lexer for TableGen. |
| 11 // | 11 // |
| 12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
| 13 | 13 |
| 14 #include "TGLexer.h" | 14 #include "TGLexer.h" |
| 15 #include "Error.h" | 15 #include "Error.h" |
| 16 #include "llvm/Support/SourceMgr.h" | 16 #include "llvm/Support/SourceMgr.h" |
| 17 #include "llvm/Support/MemoryBuffer.h" | 17 #include "llvm/Support/MemoryBuffer.h" |
| 18 #include "llvm/Config/config.h" | 18 #include "llvm/Config/config.h" |
| 19 #include "llvm/ADT/StringSwitch.h" | 19 #include "llvm/ADT/StringSwitch.h" |
| 20 #include "llvm/ADT/Twine.h" | 20 #include "llvm/ADT/Twine.h" |
| 21 #include <algorithm> |
| 21 #include <cctype> | 22 #include <cctype> |
| 22 #include <cstdio> | 23 #include <cstdio> |
| 23 #include <cstdlib> | 24 #include <cstdlib> |
| 24 #include <cstring> | 25 #include <cstring> |
| 25 #include <cerrno> | 26 #include <cerrno> |
| 26 using namespace llvm; | 27 using namespace llvm; |
| 27 | 28 |
| 28 TGLexer::TGLexer(SourceMgr &SM) : SrcMgr(SM) { | 29 TGLexer::TGLexer(SourceMgr &SM, std::vector<std::string> PPD) |
| 30 : SrcMgr(SM), PreProcDefines(PPD) { |
| 29 CurBuffer = 0; | 31 CurBuffer = 0; |
| 30 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer); | 32 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer); |
| 31 CurPtr = CurBuf->getBufferStart(); | 33 CurPtr = CurBuf->getBufferStart(); |
| 32 TokStart = 0; | 34 TokStart = 0; |
| 33 } | 35 } |
| 34 | 36 |
| 35 SMLoc TGLexer::getLoc() const { | 37 SMLoc TGLexer::getLoc() const { |
| 36 return SMLoc::getFromPointer(TokStart); | 38 return SMLoc::getFromPointer(TokStart); |
| 37 } | 39 } |
| 38 | 40 |
| 41 bool TGLexer::hasPreProcDefine(const std::string &define) const { |
| 42 return std::find(PreProcDefines.begin(), PreProcDefines.end(), |
| 43 define) != PreProcDefines.end(); |
| 44 } |
| 45 |
| 39 /// ReturnError - Set the error to the specified string at the specified | 46 /// ReturnError - Set the error to the specified string at the specified |
| 40 /// location. This is defined to always return tgtok::Error. | 47 /// location. This is defined to always return tgtok::Error. |
| 41 tgtok::TokKind TGLexer::ReturnError(const char *Loc, const Twine &Msg) { | 48 tgtok::TokKind TGLexer::ReturnError(const char *Loc, const Twine &Msg) { |
| 42 PrintError(Loc, Msg); | 49 PrintError(Loc, Msg); |
| 43 return tgtok::Error; | 50 return tgtok::Error; |
| 44 } | 51 } |
| 45 | 52 |
| 46 int TGLexer::getNextChar() { | 53 int TGLexer::getNextChar() { |
| 47 char CurChar = *CurPtr++; | 54 char CurChar = *CurPtr++; |
| 48 switch (CurChar) { | 55 switch (CurChar) { |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 | 235 |
| 229 if (Len == 5 && !memcmp(IdentStart, "class", 5)) return tgtok::Class; | 236 if (Len == 5 && !memcmp(IdentStart, "class", 5)) return tgtok::Class; |
| 230 if (Len == 3 && !memcmp(IdentStart, "def", 3)) return tgtok::Def; | 237 if (Len == 3 && !memcmp(IdentStart, "def", 3)) return tgtok::Def; |
| 231 if (Len == 4 && !memcmp(IdentStart, "defm", 4)) return tgtok::Defm; | 238 if (Len == 4 && !memcmp(IdentStart, "defm", 4)) return tgtok::Defm; |
| 232 if (Len == 10 && !memcmp(IdentStart, "multiclass", 10)) | 239 if (Len == 10 && !memcmp(IdentStart, "multiclass", 10)) |
| 233 return tgtok::MultiClass; | 240 return tgtok::MultiClass; |
| 234 if (Len == 5 && !memcmp(IdentStart, "field", 5)) return tgtok::Field; | 241 if (Len == 5 && !memcmp(IdentStart, "field", 5)) return tgtok::Field; |
| 235 if (Len == 3 && !memcmp(IdentStart, "let", 3)) return tgtok::Let; | 242 if (Len == 3 && !memcmp(IdentStart, "let", 3)) return tgtok::Let; |
| 236 if (Len == 2 && !memcmp(IdentStart, "in", 2)) return tgtok::In; | 243 if (Len == 2 && !memcmp(IdentStart, "in", 2)) return tgtok::In; |
| 237 | 244 |
| 245 if (Len == 10 && !memcmp(IdentStart, "include_if", 10)) { |
| 246 if (LexIncludeIf()) return tgtok::Error; |
| 247 return Lex(); |
| 248 } |
| 238 if (Len == 7 && !memcmp(IdentStart, "include", 7)) { | 249 if (Len == 7 && !memcmp(IdentStart, "include", 7)) { |
| 239 if (LexInclude()) return tgtok::Error; | 250 if (LexInclude()) return tgtok::Error; |
| 240 return Lex(); | 251 return Lex(); |
| 241 } | 252 } |
| 242 | 253 |
| 243 CurStrVal.assign(IdentStart, CurPtr); | 254 CurStrVal.assign(IdentStart, CurPtr); |
| 244 return tgtok::Id; | 255 return tgtok::Id; |
| 245 } | 256 } |
| 246 | 257 |
| 258 /// LexIncludeIf - We just read the "include_if" token. Get the next two |
| 259 /// string tokens. The first is the pre-processor symbol required, and the |
| 260 /// next is the filename. Finally enter the include only if the |
| 261 /// pre-processor symbol was defined at the command-line. |
| 262 bool TGLexer::LexIncludeIf() { |
| 263 |
| 264 // The next two token after the include must be strings. |
| 265 tgtok::TokKind PreProcTok = LexToken(); |
| 266 if (PreProcTok == tgtok::Error) return true; |
| 267 if (PreProcTok != tgtok::StrVal) { |
| 268 PrintError(getLoc(), "Expected pre-proc symbol after include_if"); |
| 269 return true; |
| 270 } |
| 271 // Get the string. |
| 272 std::string PreProcSymbol = CurStrVal; |
| 273 |
| 274 // The token after the include must be a string. |
| 275 tgtok::TokKind FileTok = LexToken(); |
| 276 if (FileTok == tgtok::Error) return true; |
| 277 if (FileTok != tgtok::StrVal) { |
| 278 PrintError(getLoc(), "Expected filename after include_if"); |
| 279 return true; |
| 280 } |
| 281 |
| 282 // Get the string. |
| 283 std::string Filename = CurStrVal; |
| 284 if (hasPreProcDefine(PreProcSymbol)) |
| 285 return AddIncludeFile(Filename); |
| 286 else { |
| 287 return false; |
| 288 } |
| 289 } |
| 290 |
| 247 /// LexInclude - We just read the "include" token. Get the string token that | 291 /// LexInclude - We just read the "include" token. Get the string token that |
| 248 /// comes next and enter the include. | 292 /// comes next and enter the include. |
| 249 bool TGLexer::LexInclude() { | 293 bool TGLexer::LexInclude() { |
| 250 // The token after the include must be a string. | 294 // The token after the include must be a string. |
| 251 tgtok::TokKind Tok = LexToken(); | 295 tgtok::TokKind Tok = LexToken(); |
| 252 if (Tok == tgtok::Error) return true; | 296 if (Tok == tgtok::Error) return true; |
| 253 if (Tok != tgtok::StrVal) { | 297 if (Tok != tgtok::StrVal) { |
| 254 PrintError(getLoc(), "Expected filename after include"); | 298 PrintError(getLoc(), "Expected filename after include"); |
| 255 return true; | 299 return true; |
| 256 } | 300 } |
| 257 | 301 |
| 258 // Get the string. | 302 // Get the string. |
| 259 std::string Filename = CurStrVal; | 303 std::string Filename = CurStrVal; |
| 304 return AddIncludeFile(Filename); |
| 305 } |
| 306 |
| 307 bool TGLexer::AddIncludeFile(const std::string &Filename) { |
| 260 std::string IncludedFile; | 308 std::string IncludedFile; |
| 261 | |
| 262 | |
| 263 CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr), | 309 CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr), |
| 264 IncludedFile); | 310 IncludedFile); |
| 265 if (CurBuffer == -1) { | 311 if (CurBuffer == -1) { |
| 266 PrintError(getLoc(), "Could not find include file '" + Filename + "'"); | 312 PrintError(getLoc(), "Could not find include file '" + Filename + "'"); |
| 267 return true; | 313 return true; |
| 268 } | 314 } |
| 269 | 315 |
| 270 Dependencies.push_back(IncludedFile); | 316 Dependencies.push_back(IncludedFile); |
| 271 // Save the line number and lex buffer of the includer. | 317 // Save the line number and lex buffer of the includer. |
| 272 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer); | 318 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer); |
| 273 CurPtr = CurBuf->getBufferStart(); | 319 CurPtr = CurBuf->getBufferStart(); |
| 274 return false; | 320 return false; |
| 275 } | 321 } |
| 276 | 322 |
| 277 void TGLexer::SkipBCPLComment() { | 323 void TGLexer::SkipBCPLComment() { |
| 278 ++CurPtr; // skip the second slash. | 324 ++CurPtr; // skip the second slash. |
| 279 while (1) { | 325 while (1) { |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 .Case("srl", tgtok::XSRL) | 471 .Case("srl", tgtok::XSRL) |
| 426 .Case("cast", tgtok::XCast) | 472 .Case("cast", tgtok::XCast) |
| 427 .Case("empty", tgtok::XEmpty) | 473 .Case("empty", tgtok::XEmpty) |
| 428 .Case("subst", tgtok::XSubst) | 474 .Case("subst", tgtok::XSubst) |
| 429 .Case("foreach", tgtok::XForEach) | 475 .Case("foreach", tgtok::XForEach) |
| 430 .Case("strconcat", tgtok::XStrConcat) | 476 .Case("strconcat", tgtok::XStrConcat) |
| 431 .Default(tgtok::Error); | 477 .Default(tgtok::Error); |
| 432 | 478 |
| 433 return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator"); | 479 return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator"); |
| 434 } | 480 } |
| 435 | |
| OLD | NEW |