2007-11-18 10:57:27 +08:00
|
|
|
//===- TGLexer.cpp - Lexer for TableGen -----------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2007-11-18 10:57:27 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implement the Lexer for TableGen.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-11-18 13:25:45 +08:00
|
|
|
#include "TGLexer.h"
|
2020-04-21 00:47:54 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2010-12-09 04:02:49 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Config/config.h" // for strtoull()/strtoll() define
|
2016-08-24 01:14:32 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/TableGen/Error.h"
|
2018-11-28 02:57:43 +08:00
|
|
|
#include <algorithm>
|
2007-11-18 10:57:27 +08:00
|
|
|
#include <cctype>
|
2012-12-04 00:50:05 +08:00
|
|
|
#include <cerrno>
|
2016-08-24 01:14:32 +08:00
|
|
|
#include <cstdint>
|
2008-10-08 15:23:46 +08:00
|
|
|
#include <cstdio>
|
2008-02-20 19:08:44 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2011-12-23 07:08:39 +08:00
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2018-11-28 02:57:43 +08:00
|
|
|
namespace {
|
|
|
|
// A list of supported preprocessing directives with their
|
|
|
|
// internal token kinds and names.
|
|
|
|
struct {
|
|
|
|
tgtok::TokKind Kind;
|
|
|
|
const char *Word;
|
|
|
|
} PreprocessorDirs[] = {
|
|
|
|
{ tgtok::Ifdef, "ifdef" },
|
2019-05-14 21:04:25 +08:00
|
|
|
{ tgtok::Ifndef, "ifndef" },
|
2018-11-28 02:57:43 +08:00
|
|
|
{ tgtok::Else, "else" },
|
|
|
|
{ tgtok::Endif, "endif" },
|
|
|
|
{ tgtok::Define, "define" }
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
TGLexer::TGLexer(SourceMgr &SM, ArrayRef<std::string> Macros) : SrcMgr(SM) {
|
2014-07-06 18:33:31 +08:00
|
|
|
CurBuffer = SrcMgr.getMainFileID();
|
2014-07-06 22:24:03 +08:00
|
|
|
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer();
|
|
|
|
CurPtr = CurBuf.begin();
|
2014-04-09 12:50:04 +08:00
|
|
|
TokStart = nullptr;
|
2018-11-28 02:57:43 +08:00
|
|
|
|
|
|
|
// Pretend that we enter the "top-level" include file.
|
|
|
|
PrepIncludeStack.push_back(
|
2019-08-15 23:54:37 +08:00
|
|
|
std::make_unique<std::vector<PreprocessorControlDesc>>());
|
2018-11-28 02:57:43 +08:00
|
|
|
|
|
|
|
// Put all macros defined in the command line into the DefinedMacros set.
|
|
|
|
std::for_each(Macros.begin(), Macros.end(),
|
|
|
|
[this](const std::string &MacroName) {
|
|
|
|
DefinedMacros.insert(MacroName);
|
|
|
|
});
|
2007-11-18 10:57:27 +08:00
|
|
|
}
|
|
|
|
|
2009-06-21 11:39:35 +08:00
|
|
|
SMLoc TGLexer::getLoc() const {
|
|
|
|
return SMLoc::getFromPointer(TokStart);
|
2009-03-14 00:01:53 +08:00
|
|
|
}
|
|
|
|
|
2007-11-19 15:38:58 +08:00
|
|
|
/// ReturnError - Set the error to the specified string at the specified
|
2007-11-23 04:49:04 +08:00
|
|
|
/// location. This is defined to always return tgtok::Error.
|
2018-11-28 02:57:43 +08:00
|
|
|
tgtok::TokKind TGLexer::ReturnError(SMLoc Loc, const Twine &Msg) {
|
2007-11-19 15:38:58 +08:00
|
|
|
PrintError(Loc, Msg);
|
2007-11-23 04:49:04 +08:00
|
|
|
return tgtok::Error;
|
2007-11-18 10:57:27 +08:00
|
|
|
}
|
|
|
|
|
2018-11-28 02:57:43 +08:00
|
|
|
tgtok::TokKind TGLexer::ReturnError(const char *Loc, const Twine &Msg) {
|
|
|
|
return ReturnError(SMLoc::getFromPointer(Loc), Msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TGLexer::processEOF() {
|
|
|
|
SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
|
|
|
|
if (ParentIncludeLoc != SMLoc()) {
|
|
|
|
// If prepExitInclude() detects a problem with the preprocessing
|
|
|
|
// control stack, it will return false. Pretend that we reached
|
|
|
|
// the final EOF and stop lexing more tokens by returning false
|
|
|
|
// to LexToken().
|
|
|
|
if (!prepExitInclude(false))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
|
|
|
|
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer();
|
|
|
|
CurPtr = ParentIncludeLoc.getPointer();
|
|
|
|
// Make sure TokStart points into the parent file's buffer.
|
|
|
|
// LexToken() assigns to it before calling getNextChar(),
|
|
|
|
// so it is pointing into the included file now.
|
|
|
|
TokStart = CurPtr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pretend that we exit the "top-level" include file.
|
|
|
|
// Note that in case of an error (e.g. control stack imbalance)
|
|
|
|
// the routine will issue a fatal error.
|
|
|
|
prepExitInclude(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
int TGLexer::getNextChar() {
|
|
|
|
char CurChar = *CurPtr++;
|
|
|
|
switch (CurChar) {
|
|
|
|
default:
|
2007-11-18 13:48:46 +08:00
|
|
|
return (unsigned char)CurChar;
|
2021-05-12 00:50:09 +08:00
|
|
|
|
2009-03-13 15:05:43 +08:00
|
|
|
case 0: {
|
2021-05-12 00:50:09 +08:00
|
|
|
// A NUL character in the stream is either the end of the current buffer or
|
|
|
|
// a spurious NUL in the file. Disambiguate that here.
|
|
|
|
if (CurPtr - 1 == CurBuf.end()) {
|
|
|
|
--CurPtr; // Arrange for another call to return EOF again.
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
PrintError(getLoc(),
|
|
|
|
"NUL character is invalid in source; treated as space");
|
|
|
|
return ' ';
|
2009-03-13 15:05:43 +08:00
|
|
|
}
|
2021-05-12 00:50:09 +08:00
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
// Handle the newline character by ignoring it and incrementing the line
|
|
|
|
// count. However, be careful about 'dos style' files with \n\r in them.
|
|
|
|
// Only treat a \n\r or \r\n as a single line.
|
|
|
|
if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
|
|
|
|
*CurPtr != CurChar)
|
2007-11-18 13:48:46 +08:00
|
|
|
++CurPtr; // Eat the two char newline sequence.
|
2007-11-18 10:57:27 +08:00
|
|
|
return '\n';
|
2018-03-10 02:32:04 +08:00
|
|
|
}
|
2007-11-18 10:57:27 +08:00
|
|
|
}
|
|
|
|
|
2018-11-28 02:57:43 +08:00
|
|
|
int TGLexer::peekNextChar(int Index) const {
|
2011-10-19 21:03:35 +08:00
|
|
|
return *(CurPtr + Index);
|
|
|
|
}
|
|
|
|
|
2018-11-28 02:57:43 +08:00
|
|
|
tgtok::TokKind TGLexer::LexToken(bool FileOrLineStart) {
|
2007-11-19 15:43:52 +08:00
|
|
|
TokStart = CurPtr;
|
2007-11-18 10:57:27 +08:00
|
|
|
// This always consumes at least one character.
|
|
|
|
int CurChar = getNextChar();
|
|
|
|
|
|
|
|
switch (CurChar) {
|
|
|
|
default:
|
2011-10-19 21:04:43 +08:00
|
|
|
// Handle letters: [a-zA-Z_]
|
|
|
|
if (isalpha(CurChar) || CurChar == '_')
|
2007-11-18 10:57:27 +08:00
|
|
|
return LexIdentifier();
|
2011-10-19 21:04:43 +08:00
|
|
|
|
2007-11-23 04:49:04 +08:00
|
|
|
// Unknown character, emit an error.
|
|
|
|
return ReturnError(TokStart, "Unexpected character");
|
2018-11-28 02:57:43 +08:00
|
|
|
case EOF:
|
|
|
|
// Lex next token, if we just left an include file.
|
|
|
|
// Note that leaving an include file means that the next
|
2020-11-03 01:37:25 +08:00
|
|
|
// symbol is located at the end of the 'include "..."'
|
2018-11-28 02:57:43 +08:00
|
|
|
// construct, so LexToken() is called with default
|
|
|
|
// false parameter.
|
|
|
|
if (processEOF())
|
|
|
|
return LexToken();
|
|
|
|
|
|
|
|
// Return EOF denoting the end of lexing.
|
|
|
|
return tgtok::Eof;
|
|
|
|
|
2007-11-23 04:49:04 +08:00
|
|
|
case ':': return tgtok::colon;
|
|
|
|
case ';': return tgtok::semi;
|
|
|
|
case ',': return tgtok::comma;
|
|
|
|
case '<': return tgtok::less;
|
|
|
|
case '>': return tgtok::greater;
|
|
|
|
case ']': return tgtok::r_square;
|
|
|
|
case '{': return tgtok::l_brace;
|
|
|
|
case '}': return tgtok::r_brace;
|
|
|
|
case '(': return tgtok::l_paren;
|
|
|
|
case ')': return tgtok::r_paren;
|
|
|
|
case '=': return tgtok::equal;
|
|
|
|
case '?': return tgtok::question;
|
2018-11-28 02:57:43 +08:00
|
|
|
case '#':
|
|
|
|
if (FileOrLineStart) {
|
|
|
|
tgtok::TokKind Kind = prepIsDirective();
|
|
|
|
if (Kind != tgtok::Error)
|
|
|
|
return lexPreprocessor(Kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tgtok::paste;
|
|
|
|
|
2020-08-22 05:22:06 +08:00
|
|
|
// The period is a separate case so we can recognize the "..."
|
|
|
|
// range punctuator.
|
|
|
|
case '.':
|
|
|
|
if (peekNextChar(0) == '.') {
|
|
|
|
++CurPtr; // Eat second dot.
|
|
|
|
if (peekNextChar(0) == '.') {
|
|
|
|
++CurPtr; // Eat third dot.
|
|
|
|
return tgtok::dotdotdot;
|
|
|
|
}
|
|
|
|
return ReturnError(TokStart, "Invalid '..' punctuation");
|
|
|
|
}
|
|
|
|
return tgtok::dot;
|
|
|
|
|
2018-11-28 02:57:43 +08:00
|
|
|
case '\r':
|
|
|
|
PrintFatalError("getNextChar() must never return '\r'");
|
|
|
|
return tgtok::Error;
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
case ' ':
|
|
|
|
case '\t':
|
2018-11-17 10:26:34 +08:00
|
|
|
// Ignore whitespace.
|
2018-11-28 02:57:43 +08:00
|
|
|
return LexToken(FileOrLineStart);
|
|
|
|
case '\n':
|
|
|
|
// Ignore whitespace, and identify the new line.
|
|
|
|
return LexToken(true);
|
2007-11-18 10:57:27 +08:00
|
|
|
case '/':
|
|
|
|
// If this is the start of a // comment, skip until the end of the line or
|
|
|
|
// the end of the buffer.
|
|
|
|
if (*CurPtr == '/')
|
|
|
|
SkipBCPLComment();
|
|
|
|
else if (*CurPtr == '*') {
|
|
|
|
if (SkipCComment())
|
2007-11-23 04:49:04 +08:00
|
|
|
return tgtok::Error;
|
|
|
|
} else // Otherwise, this is an error.
|
|
|
|
return ReturnError(TokStart, "Unexpected character");
|
2018-11-28 02:57:43 +08:00
|
|
|
return LexToken(FileOrLineStart);
|
2007-11-18 10:57:27 +08:00
|
|
|
case '-': case '+':
|
|
|
|
case '0': case '1': case '2': case '3': case '4': case '5': case '6':
|
2011-10-19 21:03:39 +08:00
|
|
|
case '7': case '8': case '9': {
|
|
|
|
int NextChar = 0;
|
|
|
|
if (isdigit(CurChar)) {
|
|
|
|
// Allow identifiers to start with a number if it is followed by
|
|
|
|
// an identifier. This can happen with paste operations like
|
|
|
|
// foo#8i.
|
|
|
|
int i = 0;
|
|
|
|
do {
|
|
|
|
NextChar = peekNextChar(i++);
|
|
|
|
} while (isdigit(NextChar));
|
|
|
|
|
|
|
|
if (NextChar == 'x' || NextChar == 'b') {
|
|
|
|
// If this is [0-9]b[01] or [0-9]x[0-9A-fa-f] this is most
|
|
|
|
// likely a number.
|
|
|
|
int NextNextChar = peekNextChar(i);
|
|
|
|
switch (NextNextChar) {
|
|
|
|
default:
|
|
|
|
break;
|
2018-03-10 02:32:04 +08:00
|
|
|
case '0': case '1':
|
2011-10-19 21:03:39 +08:00
|
|
|
if (NextChar == 'b')
|
|
|
|
return LexNumber();
|
2016-08-17 13:10:15 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2011-10-19 21:03:39 +08:00
|
|
|
case '2': case '3': case '4': case '5':
|
|
|
|
case '6': case '7': case '8': case '9':
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
|
|
|
if (NextChar == 'x')
|
|
|
|
return LexNumber();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isalpha(NextChar) || NextChar == '_')
|
|
|
|
return LexIdentifier();
|
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
return LexNumber();
|
2011-10-19 21:03:39 +08:00
|
|
|
}
|
2007-11-18 10:57:27 +08:00
|
|
|
case '"': return LexString();
|
|
|
|
case '$': return LexVarName();
|
|
|
|
case '[': return LexBracket();
|
|
|
|
case '!': return LexExclaim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LexString - Lex "[^"]*"
|
2007-11-23 04:49:04 +08:00
|
|
|
tgtok::TokKind TGLexer::LexString() {
|
2007-11-18 10:57:27 +08:00
|
|
|
const char *StrStart = CurPtr;
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2009-03-14 05:03:27 +08:00
|
|
|
CurStrVal = "";
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
while (*CurPtr != '"') {
|
|
|
|
// If we hit the end of the buffer, report an error.
|
2014-07-06 22:24:03 +08:00
|
|
|
if (*CurPtr == 0 && CurPtr == CurBuf.end())
|
2007-11-19 15:38:58 +08:00
|
|
|
return ReturnError(StrStart, "End of file in string literal");
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2007-11-19 15:38:58 +08:00
|
|
|
if (*CurPtr == '\n' || *CurPtr == '\r')
|
|
|
|
return ReturnError(StrStart, "End of line in string literal");
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2009-03-14 05:03:27 +08:00
|
|
|
if (*CurPtr != '\\') {
|
|
|
|
CurStrVal += *CurPtr++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
++CurPtr;
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2009-03-14 05:03:27 +08:00
|
|
|
switch (*CurPtr) {
|
|
|
|
case '\\': case '\'': case '"':
|
|
|
|
// These turn into their literal character.
|
|
|
|
CurStrVal += *CurPtr++;
|
|
|
|
break;
|
2009-03-14 05:23:43 +08:00
|
|
|
case 't':
|
2009-03-14 05:33:17 +08:00
|
|
|
CurStrVal += '\t';
|
2009-03-14 05:23:43 +08:00
|
|
|
++CurPtr;
|
|
|
|
break;
|
|
|
|
case 'n':
|
2009-03-14 05:33:17 +08:00
|
|
|
CurStrVal += '\n';
|
2009-03-14 05:23:43 +08:00
|
|
|
++CurPtr;
|
|
|
|
break;
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2009-03-14 05:03:27 +08:00
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
return ReturnError(CurPtr, "escaped newlines not supported in tblgen");
|
|
|
|
|
|
|
|
// If we hit the end of the buffer, report an error.
|
|
|
|
case '\0':
|
2014-07-06 22:24:03 +08:00
|
|
|
if (CurPtr == CurBuf.end())
|
2009-03-14 05:03:27 +08:00
|
|
|
return ReturnError(StrStart, "End of file in string literal");
|
2016-08-18 04:30:52 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2009-03-14 05:03:27 +08:00
|
|
|
default:
|
|
|
|
return ReturnError(CurPtr, "invalid escape in string literal");
|
|
|
|
}
|
2007-11-18 10:57:27 +08:00
|
|
|
}
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
++CurPtr;
|
2007-11-23 04:49:04 +08:00
|
|
|
return tgtok::StrVal;
|
2007-11-18 10:57:27 +08:00
|
|
|
}
|
|
|
|
|
2007-11-23 04:49:04 +08:00
|
|
|
tgtok::TokKind TGLexer::LexVarName() {
|
2007-11-18 10:57:27 +08:00
|
|
|
if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
|
2007-11-23 04:49:04 +08:00
|
|
|
return ReturnError(TokStart, "Invalid variable name");
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
// Otherwise, we're ok, consume the rest of the characters.
|
|
|
|
const char *VarNameStart = CurPtr++;
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
|
|
|
|
++CurPtr;
|
|
|
|
|
2007-11-23 04:49:04 +08:00
|
|
|
CurStrVal.assign(VarNameStart, CurPtr);
|
|
|
|
return tgtok::VarName;
|
2007-11-18 10:57:27 +08:00
|
|
|
}
|
|
|
|
|
2007-11-23 04:49:04 +08:00
|
|
|
tgtok::TokKind TGLexer::LexIdentifier() {
|
2018-11-28 02:57:43 +08:00
|
|
|
// The first letter is [a-zA-Z_].
|
2007-11-23 04:49:04 +08:00
|
|
|
const char *IdentStart = TokStart;
|
2011-10-07 02:23:56 +08:00
|
|
|
|
2018-11-28 02:57:43 +08:00
|
|
|
// Match the rest of the identifier regex: [0-9a-zA-Z_]*
|
2011-10-19 21:04:43 +08:00
|
|
|
while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
|
2010-10-06 06:59:29 +08:00
|
|
|
++CurPtr;
|
2011-10-07 02:23:56 +08:00
|
|
|
|
2020-11-03 01:37:25 +08:00
|
|
|
// Check to see if this identifier is a reserved keyword.
|
2011-10-07 02:23:56 +08:00
|
|
|
StringRef Str(IdentStart, CurPtr-IdentStart);
|
|
|
|
|
2011-10-07 02:53:43 +08:00
|
|
|
tgtok::TokKind Kind = StringSwitch<tgtok::TokKind>(Str)
|
|
|
|
.Case("int", tgtok::Int)
|
|
|
|
.Case("bit", tgtok::Bit)
|
|
|
|
.Case("bits", tgtok::Bits)
|
|
|
|
.Case("string", tgtok::String)
|
|
|
|
.Case("list", tgtok::List)
|
|
|
|
.Case("code", tgtok::Code)
|
|
|
|
.Case("dag", tgtok::Dag)
|
|
|
|
.Case("class", tgtok::Class)
|
|
|
|
.Case("def", tgtok::Def)
|
2020-11-03 01:37:25 +08:00
|
|
|
.Case("true", tgtok::TrueVal)
|
|
|
|
.Case("false", tgtok::FalseVal)
|
2012-02-23 00:09:41 +08:00
|
|
|
.Case("foreach", tgtok::Foreach)
|
2011-10-07 02:53:43 +08:00
|
|
|
.Case("defm", tgtok::Defm)
|
2018-03-09 20:24:42 +08:00
|
|
|
.Case("defset", tgtok::Defset)
|
2011-10-07 02:53:43 +08:00
|
|
|
.Case("multiclass", tgtok::MultiClass)
|
|
|
|
.Case("field", tgtok::Field)
|
|
|
|
.Case("let", tgtok::Let)
|
|
|
|
.Case("in", tgtok::In)
|
[TableGen] Introduce a `defvar` statement.
Summary:
This allows you to define a global or local variable to an arbitrary
value, and refer to it in subsequent definitions.
The main use I anticipate for this is if you have to compute some
difficult function of the parameters of a multiclass, and then use it
many times. For example:
multiclass Foo<int i, string s> {
defvar op = !cast<BaseClass>("whatnot_" # s # "_" # i);
def myRecord {
dag a = (op this, (op that, the other), (op x, y, z));
int b = op.subfield;
}
def myOtherRecord<"template params including", op>;
}
There are a couple of ways to do this already, but they're not really
satisfactory. You can replace `defvar x = y` with a loop over a
singleton list, `foreach x = [y] in { ... }` - but that's unintuitive
to someone who hasn't seen that workaround idiom before, and requires
an extra pair of braces that you often didn't really want. Or you can
define a nested pair of multiclasses, with the inner one taking `x` as
a template parameter, and the outer one instantiating it just once
with the desired value of `x` computed from its other parameters - but
that makes it awkward to sequentially compute each value based on the
previous ones. I think `defvar` makes things considerably easier.
You can also use `defvar` at the top level, where it inserts globals
into the same map used by `defset`. That allows you to define global
constants without having to make a dummy record for them to live in:
defvar MAX_BUFSIZE = 512;
// previously:
// def Dummy { int MAX_BUFSIZE = 512; }
// and then refer to Dummy.MAX_BUFSIZE everywhere
Reviewers: nhaehnle, hfinkel
Reviewed By: hfinkel
Subscribers: hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D71407
2020-01-14 17:10:18 +08:00
|
|
|
.Case("defvar", tgtok::Defvar)
|
2020-11-03 01:37:25 +08:00
|
|
|
.Case("include", tgtok::Include)
|
2020-01-14 17:10:34 +08:00
|
|
|
.Case("if", tgtok::If)
|
|
|
|
.Case("then", tgtok::Then)
|
|
|
|
.Case("else", tgtok::ElseKW)
|
2020-12-17 05:25:54 +08:00
|
|
|
.Case("assert", tgtok::Assert)
|
2011-10-07 02:53:43 +08:00
|
|
|
.Default(tgtok::Id);
|
|
|
|
|
2020-11-03 01:37:25 +08:00
|
|
|
// A couple of tokens require special processing.
|
|
|
|
switch (Kind) {
|
|
|
|
case tgtok::Include:
|
|
|
|
if (LexInclude()) return tgtok::Error;
|
|
|
|
return Lex();
|
|
|
|
case tgtok::Id:
|
|
|
|
CurStrVal.assign(Str.begin(), Str.end());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-10-07 02:53:43 +08:00
|
|
|
return Kind;
|
2007-11-18 10:57:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// LexInclude - We just read the "include" token. Get the string token that
|
|
|
|
/// comes next and enter the include.
|
|
|
|
bool TGLexer::LexInclude() {
|
|
|
|
// The token after the include must be a string.
|
2007-11-23 04:49:04 +08:00
|
|
|
tgtok::TokKind Tok = LexToken();
|
|
|
|
if (Tok == tgtok::Error) return true;
|
|
|
|
if (Tok != tgtok::StrVal) {
|
|
|
|
PrintError(getLoc(), "Expected filename after include");
|
2007-11-18 10:57:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the string.
|
2007-11-23 04:49:04 +08:00
|
|
|
std::string Filename = CurStrVal;
|
2011-06-01 21:10:15 +08:00
|
|
|
std::string IncludedFile;
|
2007-11-18 10:57:27 +08:00
|
|
|
|
2011-06-01 21:10:15 +08:00
|
|
|
CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr),
|
|
|
|
IncludedFile);
|
2014-07-06 18:33:31 +08:00
|
|
|
if (!CurBuffer) {
|
2007-11-23 04:49:04 +08:00
|
|
|
PrintError(getLoc(), "Could not find include file '" + Filename + "'");
|
2007-11-18 10:57:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2019-11-21 10:21:50 +08:00
|
|
|
Dependencies.insert(IncludedFile);
|
2007-11-18 10:57:27 +08:00
|
|
|
// Save the line number and lex buffer of the includer.
|
2014-07-06 22:24:03 +08:00
|
|
|
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer();
|
|
|
|
CurPtr = CurBuf.begin();
|
2018-11-28 02:57:43 +08:00
|
|
|
|
|
|
|
PrepIncludeStack.push_back(
|
2019-08-15 23:54:37 +08:00
|
|
|
std::make_unique<std::vector<PreprocessorControlDesc>>());
|
2007-11-18 10:57:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-12 00:50:09 +08:00
|
|
|
/// SkipBCPLComment - Skip over the comment by finding the next CR or LF.
|
|
|
|
/// Or we may end up at the end of the buffer.
|
2007-11-18 10:57:27 +08:00
|
|
|
void TGLexer::SkipBCPLComment() {
|
|
|
|
++CurPtr; // skip the second slash.
|
2021-05-12 00:50:09 +08:00
|
|
|
auto EOLPos = CurBuf.find_first_of("\r\n", CurPtr - CurBuf.data());
|
|
|
|
CurPtr = (EOLPos == StringRef::npos) ? CurBuf.end() : CurBuf.data() + EOLPos;
|
2007-11-18 10:57:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// SkipCComment - This skips C-style /**/ comments. The only difference from C
|
|
|
|
/// is that we allow nesting.
|
|
|
|
bool TGLexer::SkipCComment() {
|
|
|
|
++CurPtr; // skip the star.
|
|
|
|
unsigned CommentDepth = 1;
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2016-08-24 01:14:32 +08:00
|
|
|
while (true) {
|
2007-11-18 10:57:27 +08:00
|
|
|
int CurChar = getNextChar();
|
|
|
|
switch (CurChar) {
|
|
|
|
case EOF:
|
2007-11-23 04:49:04 +08:00
|
|
|
PrintError(TokStart, "Unterminated comment!");
|
2007-11-18 10:57:27 +08:00
|
|
|
return true;
|
|
|
|
case '*':
|
|
|
|
// End of the comment?
|
|
|
|
if (CurPtr[0] != '/') break;
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
++CurPtr; // End the */.
|
|
|
|
if (--CommentDepth == 0)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
// Start of a nested comment?
|
|
|
|
if (CurPtr[0] != '*') break;
|
|
|
|
++CurPtr;
|
|
|
|
++CommentDepth;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LexNumber - Lex:
|
|
|
|
/// [-+]?[0-9]+
|
|
|
|
/// 0x[0-9a-fA-F]+
|
|
|
|
/// 0b[01]+
|
2007-11-23 04:49:04 +08:00
|
|
|
tgtok::TokKind TGLexer::LexNumber() {
|
2007-11-18 10:57:27 +08:00
|
|
|
if (CurPtr[-1] == '0') {
|
|
|
|
if (CurPtr[0] == 'x') {
|
|
|
|
++CurPtr;
|
2007-11-23 04:49:04 +08:00
|
|
|
const char *NumStart = CurPtr;
|
2007-11-18 10:57:27 +08:00
|
|
|
while (isxdigit(CurPtr[0]))
|
|
|
|
++CurPtr;
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2007-11-19 15:38:58 +08:00
|
|
|
// Requires at least one hex digit.
|
|
|
|
if (CurPtr == NumStart)
|
2009-06-22 03:22:49 +08:00
|
|
|
return ReturnError(TokStart, "Invalid hexadecimal number");
|
2007-11-19 15:38:58 +08:00
|
|
|
|
2008-10-17 09:33:43 +08:00
|
|
|
errno = 0;
|
2014-04-09 12:50:04 +08:00
|
|
|
CurIntVal = strtoll(NumStart, nullptr, 16);
|
2008-10-17 09:33:43 +08:00
|
|
|
if (errno == EINVAL)
|
2009-06-22 03:22:49 +08:00
|
|
|
return ReturnError(TokStart, "Invalid hexadecimal number");
|
2008-10-17 09:33:43 +08:00
|
|
|
if (errno == ERANGE) {
|
|
|
|
errno = 0;
|
2014-04-09 12:50:04 +08:00
|
|
|
CurIntVal = (int64_t)strtoull(NumStart, nullptr, 16);
|
2008-10-17 09:33:43 +08:00
|
|
|
if (errno == EINVAL)
|
2009-06-22 03:22:49 +08:00
|
|
|
return ReturnError(TokStart, "Invalid hexadecimal number");
|
2008-10-17 09:33:43 +08:00
|
|
|
if (errno == ERANGE)
|
2009-06-22 03:22:49 +08:00
|
|
|
return ReturnError(TokStart, "Hexadecimal number out of range");
|
2008-10-17 09:33:43 +08:00
|
|
|
}
|
2007-11-23 04:49:04 +08:00
|
|
|
return tgtok::IntVal;
|
2007-11-18 10:57:27 +08:00
|
|
|
} else if (CurPtr[0] == 'b') {
|
|
|
|
++CurPtr;
|
2007-11-23 04:49:04 +08:00
|
|
|
const char *NumStart = CurPtr;
|
2007-11-18 10:57:27 +08:00
|
|
|
while (CurPtr[0] == '0' || CurPtr[0] == '1')
|
|
|
|
++CurPtr;
|
2007-11-19 15:38:58 +08:00
|
|
|
|
|
|
|
// Requires at least one binary digit.
|
|
|
|
if (CurPtr == NumStart)
|
|
|
|
return ReturnError(CurPtr-2, "Invalid binary number");
|
2014-04-09 12:50:04 +08:00
|
|
|
CurIntVal = strtoll(NumStart, nullptr, 2);
|
2014-08-07 13:47:00 +08:00
|
|
|
return tgtok::BinaryIntVal;
|
2007-11-18 10:57:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a sign without a digit.
|
2007-11-23 04:49:04 +08:00
|
|
|
if (!isdigit(CurPtr[0])) {
|
|
|
|
if (CurPtr[-1] == '-')
|
|
|
|
return tgtok::minus;
|
|
|
|
else if (CurPtr[-1] == '+')
|
|
|
|
return tgtok::plus;
|
2007-11-18 10:57:27 +08:00
|
|
|
}
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
while (isdigit(CurPtr[0]))
|
|
|
|
++CurPtr;
|
2014-04-09 12:50:04 +08:00
|
|
|
CurIntVal = strtoll(TokStart, nullptr, 10);
|
2007-11-23 04:49:04 +08:00
|
|
|
return tgtok::IntVal;
|
2007-11-18 10:57:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// LexBracket - We just read '['. If this is a code block, return it,
|
|
|
|
/// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
|
2007-11-23 04:49:04 +08:00
|
|
|
tgtok::TokKind TGLexer::LexBracket() {
|
2007-11-18 10:57:27 +08:00
|
|
|
if (CurPtr[0] != '{')
|
2007-11-23 04:49:04 +08:00
|
|
|
return tgtok::l_square;
|
2007-11-18 10:57:27 +08:00
|
|
|
++CurPtr;
|
|
|
|
const char *CodeStart = CurPtr;
|
2016-08-24 01:14:32 +08:00
|
|
|
while (true) {
|
2007-11-18 10:57:27 +08:00
|
|
|
int Char = getNextChar();
|
|
|
|
if (Char == EOF) break;
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
if (Char != '}') continue;
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
Char = getNextChar();
|
|
|
|
if (Char == EOF) break;
|
|
|
|
if (Char == ']') {
|
2007-11-23 04:49:04 +08:00
|
|
|
CurStrVal.assign(CodeStart, CurPtr-2);
|
|
|
|
return tgtok::CodeFragment;
|
2007-11-18 10:57:27 +08:00
|
|
|
}
|
|
|
|
}
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2020-11-25 02:09:02 +08:00
|
|
|
return ReturnError(CodeStart - 2, "Unterminated code block");
|
2007-11-18 10:57:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// LexExclaim - Lex '!' and '![a-zA-Z]+'.
|
2007-11-23 04:49:04 +08:00
|
|
|
tgtok::TokKind TGLexer::LexExclaim() {
|
2007-11-18 10:57:27 +08:00
|
|
|
if (!isalpha(*CurPtr))
|
2010-12-08 21:03:15 +08:00
|
|
|
return ReturnError(CurPtr - 1, "Invalid \"!operator\"");
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
const char *Start = CurPtr++;
|
|
|
|
while (isalpha(*CurPtr))
|
|
|
|
++CurPtr;
|
2018-03-10 02:32:04 +08:00
|
|
|
|
2007-11-18 10:57:27 +08:00
|
|
|
// Check to see which operator this is.
|
2010-12-09 04:02:49 +08:00
|
|
|
tgtok::TokKind Kind =
|
|
|
|
StringSwitch<tgtok::TokKind>(StringRef(Start, CurPtr - Start))
|
|
|
|
.Case("eq", tgtok::XEq)
|
2018-03-14 19:00:57 +08:00
|
|
|
.Case("ne", tgtok::XNe)
|
|
|
|
.Case("le", tgtok::XLe)
|
|
|
|
.Case("lt", tgtok::XLt)
|
|
|
|
.Case("ge", tgtok::XGe)
|
|
|
|
.Case("gt", tgtok::XGt)
|
2010-12-09 04:02:49 +08:00
|
|
|
.Case("if", tgtok::XIf)
|
2019-01-25 18:25:25 +08:00
|
|
|
.Case("cond", tgtok::XCond)
|
2018-03-09 20:24:06 +08:00
|
|
|
.Case("isa", tgtok::XIsA)
|
2011-01-08 01:05:37 +08:00
|
|
|
.Case("head", tgtok::XHead)
|
|
|
|
.Case("tail", tgtok::XTail)
|
2018-02-23 18:46:07 +08:00
|
|
|
.Case("size", tgtok::XSize)
|
2010-12-09 04:02:49 +08:00
|
|
|
.Case("con", tgtok::XConcat)
|
2018-03-14 19:00:26 +08:00
|
|
|
.Case("dag", tgtok::XDag)
|
2013-01-25 22:49:08 +08:00
|
|
|
.Case("add", tgtok::XADD)
|
2020-10-24 05:02:39 +08:00
|
|
|
.Case("sub", tgtok::XSUB)
|
2019-03-01 17:46:29 +08:00
|
|
|
.Case("mul", tgtok::XMUL)
|
2020-10-13 01:35:23 +08:00
|
|
|
.Case("not", tgtok::XNOT)
|
2014-08-05 17:43:25 +08:00
|
|
|
.Case("and", tgtok::XAND)
|
2016-11-15 14:49:28 +08:00
|
|
|
.Case("or", tgtok::XOR)
|
2020-10-13 01:35:23 +08:00
|
|
|
.Case("xor", tgtok::XXOR)
|
2010-12-09 04:02:49 +08:00
|
|
|
.Case("shl", tgtok::XSHL)
|
|
|
|
.Case("sra", tgtok::XSRA)
|
|
|
|
.Case("srl", tgtok::XSRL)
|
|
|
|
.Case("cast", tgtok::XCast)
|
2011-01-08 01:05:37 +08:00
|
|
|
.Case("empty", tgtok::XEmpty)
|
2010-12-09 04:02:49 +08:00
|
|
|
.Case("subst", tgtok::XSubst)
|
2018-03-06 21:49:16 +08:00
|
|
|
.Case("foldl", tgtok::XFoldl)
|
2010-12-09 04:02:49 +08:00
|
|
|
.Case("foreach", tgtok::XForEach)
|
2020-11-06 01:49:21 +08:00
|
|
|
.Case("filter", tgtok::XFilter)
|
2014-05-07 18:13:19 +08:00
|
|
|
.Case("listconcat", tgtok::XListConcat)
|
2019-04-11 02:26:36 +08:00
|
|
|
.Case("listsplat", tgtok::XListSplat)
|
2010-12-09 04:02:49 +08:00
|
|
|
.Case("strconcat", tgtok::XStrConcat)
|
2020-10-30 23:40:42 +08:00
|
|
|
.Case("interleave", tgtok::XInterleave)
|
2020-12-16 22:55:16 +08:00
|
|
|
.Case("substr", tgtok::XSubstr)
|
2021-04-26 21:53:35 +08:00
|
|
|
.Case("find", tgtok::XFind)
|
2020-10-21 01:50:49 +08:00
|
|
|
.Cases("setdagop", "setop", tgtok::XSetDagOp) // !setop is deprecated.
|
|
|
|
.Cases("getdagop", "getop", tgtok::XGetDagOp) // !getop is deprecated.
|
2010-12-09 04:02:49 +08:00
|
|
|
.Default(tgtok::Error);
|
|
|
|
|
|
|
|
return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator");
|
2007-11-18 10:57:27 +08:00
|
|
|
}
|
2018-11-28 02:57:43 +08:00
|
|
|
|
|
|
|
bool TGLexer::prepExitInclude(bool IncludeStackMustBeEmpty) {
|
|
|
|
// Report an error, if preprocessor control stack for the current
|
|
|
|
// file is not empty.
|
|
|
|
if (!PrepIncludeStack.back()->empty()) {
|
|
|
|
prepReportPreprocessorStackError();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop the preprocessing controls from the include stack.
|
|
|
|
if (PrepIncludeStack.empty()) {
|
|
|
|
PrintFatalError("Preprocessor include stack is empty");
|
|
|
|
}
|
|
|
|
|
|
|
|
PrepIncludeStack.pop_back();
|
|
|
|
|
|
|
|
if (IncludeStackMustBeEmpty) {
|
|
|
|
if (!PrepIncludeStack.empty())
|
|
|
|
PrintFatalError("Preprocessor include stack is not empty");
|
|
|
|
} else {
|
|
|
|
if (PrepIncludeStack.empty())
|
|
|
|
PrintFatalError("Preprocessor include stack is empty");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
tgtok::TokKind TGLexer::prepIsDirective() const {
|
2021-03-07 07:52:55 +08:00
|
|
|
for (const auto &PD : PreprocessorDirs) {
|
2018-11-28 02:57:43 +08:00
|
|
|
int NextChar = *CurPtr;
|
|
|
|
bool Match = true;
|
|
|
|
unsigned I = 0;
|
2021-03-07 07:52:55 +08:00
|
|
|
for (; I < strlen(PD.Word); ++I) {
|
|
|
|
if (NextChar != PD.Word[I]) {
|
2018-11-28 02:57:43 +08:00
|
|
|
Match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
NextChar = peekNextChar(I + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for whitespace after the directive. If there is no whitespace,
|
|
|
|
// then we do not recognize it as a preprocessing directive.
|
|
|
|
if (Match) {
|
2021-03-07 07:52:55 +08:00
|
|
|
tgtok::TokKind Kind = PD.Kind;
|
2018-11-28 02:57:43 +08:00
|
|
|
|
|
|
|
// New line and EOF may follow only #else/#endif. It will be reported
|
|
|
|
// as an error for #ifdef/#define after the call to prepLexMacroName().
|
|
|
|
if (NextChar == ' ' || NextChar == '\t' || NextChar == EOF ||
|
|
|
|
NextChar == '\n' ||
|
|
|
|
// It looks like TableGen does not support '\r' as the actual
|
|
|
|
// carriage return, e.g. getNextChar() treats a single '\r'
|
|
|
|
// as '\n'. So we do the same here.
|
|
|
|
NextChar == '\r')
|
|
|
|
return Kind;
|
|
|
|
|
|
|
|
// Allow comments after some directives, e.g.:
|
|
|
|
// #else// OR #else/**/
|
|
|
|
// #endif// OR #endif/**/
|
|
|
|
//
|
|
|
|
// Note that we do allow comments after #ifdef/#define here, e.g.
|
|
|
|
// #ifdef/**/ AND #ifdef//
|
|
|
|
// #define/**/ AND #define//
|
|
|
|
//
|
|
|
|
// These cases will be reported as incorrect after calling
|
|
|
|
// prepLexMacroName(). We could have supported C-style comments
|
|
|
|
// after #ifdef/#define, but this would complicate the code
|
|
|
|
// for little benefit.
|
|
|
|
if (NextChar == '/') {
|
|
|
|
NextChar = peekNextChar(I + 1);
|
|
|
|
|
|
|
|
if (NextChar == '*' || NextChar == '/')
|
|
|
|
return Kind;
|
|
|
|
|
|
|
|
// Pretend that we do not recognize the directive.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tgtok::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TGLexer::prepEatPreprocessorDirective(tgtok::TokKind Kind) {
|
|
|
|
TokStart = CurPtr;
|
|
|
|
|
2021-03-07 07:52:55 +08:00
|
|
|
for (const auto &PD : PreprocessorDirs)
|
|
|
|
if (PD.Kind == Kind) {
|
2018-11-28 02:57:43 +08:00
|
|
|
// Advance CurPtr to the end of the preprocessing word.
|
2021-03-07 07:52:55 +08:00
|
|
|
CurPtr += strlen(PD.Word);
|
2018-11-28 02:57:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintFatalError("Unsupported preprocessing token in "
|
|
|
|
"prepEatPreprocessorDirective()");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tgtok::TokKind TGLexer::lexPreprocessor(
|
|
|
|
tgtok::TokKind Kind, bool ReturnNextLiveToken) {
|
|
|
|
|
|
|
|
// We must be looking at a preprocessing directive. Eat it!
|
|
|
|
if (!prepEatPreprocessorDirective(Kind))
|
|
|
|
PrintFatalError("lexPreprocessor() called for unknown "
|
|
|
|
"preprocessor directive");
|
|
|
|
|
2019-05-14 21:04:25 +08:00
|
|
|
if (Kind == tgtok::Ifdef || Kind == tgtok::Ifndef) {
|
2018-11-28 02:57:43 +08:00
|
|
|
StringRef MacroName = prepLexMacroName();
|
2019-05-14 21:04:25 +08:00
|
|
|
StringRef IfTokName = Kind == tgtok::Ifdef ? "#ifdef" : "#ifndef";
|
2018-11-28 02:57:43 +08:00
|
|
|
if (MacroName.empty())
|
2019-05-14 21:04:25 +08:00
|
|
|
return ReturnError(TokStart, "Expected macro name after " + IfTokName);
|
2018-11-28 02:57:43 +08:00
|
|
|
|
|
|
|
bool MacroIsDefined = DefinedMacros.count(MacroName) != 0;
|
|
|
|
|
2019-05-14 21:04:25 +08:00
|
|
|
// Canonicalize ifndef to ifdef equivalent
|
|
|
|
if (Kind == tgtok::Ifndef) {
|
|
|
|
MacroIsDefined = !MacroIsDefined;
|
|
|
|
Kind = tgtok::Ifdef;
|
|
|
|
}
|
|
|
|
|
2018-11-28 02:57:43 +08:00
|
|
|
// Regardless of whether we are processing tokens or not,
|
|
|
|
// we put the #ifdef control on stack.
|
|
|
|
PrepIncludeStack.back()->push_back(
|
|
|
|
{Kind, MacroIsDefined, SMLoc::getFromPointer(TokStart)});
|
|
|
|
|
|
|
|
if (!prepSkipDirectiveEnd())
|
2019-05-14 21:04:25 +08:00
|
|
|
return ReturnError(CurPtr, "Only comments are supported after " +
|
|
|
|
IfTokName + " NAME");
|
2018-11-28 02:57:43 +08:00
|
|
|
|
|
|
|
// If we were not processing tokens before this #ifdef,
|
|
|
|
// then just return back to the lines skipping code.
|
|
|
|
if (!ReturnNextLiveToken)
|
|
|
|
return Kind;
|
|
|
|
|
|
|
|
// If we were processing tokens before this #ifdef,
|
|
|
|
// and the macro is defined, then just return the next token.
|
|
|
|
if (MacroIsDefined)
|
|
|
|
return LexToken();
|
|
|
|
|
|
|
|
// We were processing tokens before this #ifdef, and the macro
|
|
|
|
// is not defined, so we have to start skipping the lines.
|
|
|
|
// If the skipping is successful, it will return the token following
|
|
|
|
// either #else or #endif corresponding to this #ifdef.
|
|
|
|
if (prepSkipRegion(ReturnNextLiveToken))
|
|
|
|
return LexToken();
|
|
|
|
|
|
|
|
return tgtok::Error;
|
|
|
|
} else if (Kind == tgtok::Else) {
|
|
|
|
// Check if this #else is correct before calling prepSkipDirectiveEnd(),
|
|
|
|
// which will move CurPtr away from the beginning of #else.
|
|
|
|
if (PrepIncludeStack.back()->empty())
|
2019-05-14 21:04:25 +08:00
|
|
|
return ReturnError(TokStart, "#else without #ifdef or #ifndef");
|
2018-11-28 02:57:43 +08:00
|
|
|
|
|
|
|
PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back()->back();
|
|
|
|
|
|
|
|
if (IfdefEntry.Kind != tgtok::Ifdef) {
|
|
|
|
PrintError(TokStart, "double #else");
|
|
|
|
return ReturnError(IfdefEntry.SrcPos, "Previous #else is here");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the corresponding #ifdef's control with its negation
|
|
|
|
// on the control stack.
|
|
|
|
PrepIncludeStack.back()->pop_back();
|
|
|
|
PrepIncludeStack.back()->push_back(
|
|
|
|
{Kind, !IfdefEntry.IsDefined, SMLoc::getFromPointer(TokStart)});
|
|
|
|
|
|
|
|
if (!prepSkipDirectiveEnd())
|
|
|
|
return ReturnError(CurPtr, "Only comments are supported after #else");
|
|
|
|
|
|
|
|
// If we were processing tokens before this #else,
|
|
|
|
// we have to start skipping lines until the matching #endif.
|
|
|
|
if (ReturnNextLiveToken) {
|
|
|
|
if (prepSkipRegion(ReturnNextLiveToken))
|
|
|
|
return LexToken();
|
|
|
|
|
|
|
|
return tgtok::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return to the lines skipping code.
|
|
|
|
return Kind;
|
|
|
|
} else if (Kind == tgtok::Endif) {
|
|
|
|
// Check if this #endif is correct before calling prepSkipDirectiveEnd(),
|
|
|
|
// which will move CurPtr away from the beginning of #endif.
|
|
|
|
if (PrepIncludeStack.back()->empty())
|
|
|
|
return ReturnError(TokStart, "#endif without #ifdef");
|
|
|
|
|
|
|
|
auto &IfdefOrElseEntry = PrepIncludeStack.back()->back();
|
|
|
|
|
|
|
|
if (IfdefOrElseEntry.Kind != tgtok::Ifdef &&
|
|
|
|
IfdefOrElseEntry.Kind != tgtok::Else) {
|
|
|
|
PrintFatalError("Invalid preprocessor control on the stack");
|
|
|
|
return tgtok::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prepSkipDirectiveEnd())
|
|
|
|
return ReturnError(CurPtr, "Only comments are supported after #endif");
|
|
|
|
|
|
|
|
PrepIncludeStack.back()->pop_back();
|
|
|
|
|
|
|
|
// If we were processing tokens before this #endif, then
|
|
|
|
// we should continue it.
|
|
|
|
if (ReturnNextLiveToken) {
|
|
|
|
return LexToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return to the lines skipping code.
|
|
|
|
return Kind;
|
|
|
|
} else if (Kind == tgtok::Define) {
|
|
|
|
StringRef MacroName = prepLexMacroName();
|
|
|
|
if (MacroName.empty())
|
|
|
|
return ReturnError(TokStart, "Expected macro name after #define");
|
|
|
|
|
|
|
|
if (!DefinedMacros.insert(MacroName).second)
|
|
|
|
PrintWarning(getLoc(),
|
|
|
|
"Duplicate definition of macro: " + Twine(MacroName));
|
|
|
|
|
|
|
|
if (!prepSkipDirectiveEnd())
|
|
|
|
return ReturnError(CurPtr,
|
|
|
|
"Only comments are supported after #define NAME");
|
|
|
|
|
|
|
|
if (!ReturnNextLiveToken) {
|
|
|
|
PrintFatalError("#define must be ignored during the lines skipping");
|
|
|
|
return tgtok::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LexToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintFatalError("Preprocessing directive is not supported");
|
|
|
|
return tgtok::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TGLexer::prepSkipRegion(bool MustNeverBeFalse) {
|
|
|
|
if (!MustNeverBeFalse)
|
|
|
|
PrintFatalError("Invalid recursion.");
|
|
|
|
|
|
|
|
do {
|
|
|
|
// Skip all symbols to the line end.
|
|
|
|
prepSkipToLineEnd();
|
|
|
|
|
|
|
|
// Find the first non-whitespace symbol in the next line(s).
|
|
|
|
if (!prepSkipLineBegin())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If the first non-blank/comment symbol on the line is '#',
|
|
|
|
// it may be a start of preprocessing directive.
|
|
|
|
//
|
|
|
|
// If it is not '#' just go to the next line.
|
|
|
|
if (*CurPtr == '#')
|
|
|
|
++CurPtr;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tgtok::TokKind Kind = prepIsDirective();
|
|
|
|
|
|
|
|
// If we did not find a preprocessing directive or it is #define,
|
|
|
|
// then just skip to the next line. We do not have to do anything
|
|
|
|
// for #define in the line-skipping mode.
|
|
|
|
if (Kind == tgtok::Error || Kind == tgtok::Define)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tgtok::TokKind ProcessedKind = lexPreprocessor(Kind, false);
|
|
|
|
|
|
|
|
// If lexPreprocessor() encountered an error during lexing this
|
|
|
|
// preprocessor idiom, then return false to the calling lexPreprocessor().
|
|
|
|
// This will force tgtok::Error to be returned to the tokens processing.
|
|
|
|
if (ProcessedKind == tgtok::Error)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Kind != ProcessedKind)
|
|
|
|
PrintFatalError("prepIsDirective() and lexPreprocessor() "
|
|
|
|
"returned different token kinds");
|
|
|
|
|
|
|
|
// If this preprocessing directive enables tokens processing,
|
|
|
|
// then return to the lexPreprocessor() and get to the next token.
|
|
|
|
// We can move from line-skipping mode to processing tokens only
|
|
|
|
// due to #else or #endif.
|
|
|
|
if (prepIsProcessingEnabled()) {
|
|
|
|
if (Kind != tgtok::Else && Kind != tgtok::Endif) {
|
|
|
|
PrintFatalError("Tokens processing was enabled by an unexpected "
|
|
|
|
"preprocessing directive");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while (CurPtr != CurBuf.end());
|
|
|
|
|
|
|
|
// We have reached the end of the file, but never left the lines-skipping
|
|
|
|
// mode. This means there is no matching #endif.
|
|
|
|
prepReportPreprocessorStackError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef TGLexer::prepLexMacroName() {
|
|
|
|
// Skip whitespaces between the preprocessing directive and the macro name.
|
|
|
|
while (*CurPtr == ' ' || *CurPtr == '\t')
|
|
|
|
++CurPtr;
|
|
|
|
|
|
|
|
TokStart = CurPtr;
|
|
|
|
// Macro names start with [a-zA-Z_].
|
|
|
|
if (*CurPtr != '_' && !isalpha(*CurPtr))
|
|
|
|
return "";
|
|
|
|
|
|
|
|
// Match the rest of the identifier regex: [0-9a-zA-Z_]*
|
|
|
|
while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
|
|
|
|
++CurPtr;
|
|
|
|
|
|
|
|
return StringRef(TokStart, CurPtr - TokStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TGLexer::prepSkipLineBegin() {
|
|
|
|
while (CurPtr != CurBuf.end()) {
|
|
|
|
switch (*CurPtr) {
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '/': {
|
|
|
|
int NextChar = peekNextChar(1);
|
|
|
|
if (NextChar == '*') {
|
|
|
|
// Skip C-style comment.
|
|
|
|
// Note that we do not care about skipping the C++-style comments.
|
|
|
|
// If the line contains "//", it may not contain any processable
|
|
|
|
// preprocessing directive. Just return CurPtr pointing to
|
|
|
|
// the first '/' in this case. We also do not care about
|
|
|
|
// incorrect symbols after the first '/' - we are in lines-skipping
|
|
|
|
// mode, so incorrect code is allowed to some extent.
|
|
|
|
|
|
|
|
// Set TokStart to the beginning of the comment to enable proper
|
|
|
|
// diagnostic printing in case of error in SkipCComment().
|
|
|
|
TokStart = CurPtr;
|
|
|
|
|
|
|
|
// CurPtr must point to '*' before call to SkipCComment().
|
|
|
|
++CurPtr;
|
|
|
|
if (SkipCComment())
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// CurPtr points to the non-whitespace '/'.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must not increment CurPtr after the comment was lexed.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
++CurPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have reached the end of the file. Return to the lines skipping
|
|
|
|
// code, and allow it to handle the EOF as needed.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TGLexer::prepSkipDirectiveEnd() {
|
|
|
|
while (CurPtr != CurBuf.end()) {
|
|
|
|
switch (*CurPtr) {
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case '/': {
|
|
|
|
int NextChar = peekNextChar(1);
|
|
|
|
if (NextChar == '/') {
|
|
|
|
// Skip C++-style comment.
|
|
|
|
// We may just return true now, but let's skip to the line/buffer end
|
|
|
|
// to simplify the method specification.
|
|
|
|
++CurPtr;
|
|
|
|
SkipBCPLComment();
|
|
|
|
} else if (NextChar == '*') {
|
|
|
|
// When we are skipping C-style comment at the end of a preprocessing
|
|
|
|
// directive, we can skip several lines. If any meaningful TD token
|
|
|
|
// follows the end of the C-style comment on the same line, it will
|
|
|
|
// be considered as an invalid usage of TD token.
|
|
|
|
// For example, we want to forbid usages like this one:
|
|
|
|
// #define MACRO class Class {}
|
|
|
|
// But with C-style comments we also disallow the following:
|
|
|
|
// #define MACRO /* This macro is used
|
|
|
|
// to ... */ class Class {}
|
|
|
|
// One can argue that this should be allowed, but it does not seem
|
|
|
|
// to be worth of the complication. Moreover, this matches
|
|
|
|
// the C preprocessor behavior.
|
|
|
|
|
|
|
|
// Set TokStart to the beginning of the comment to enable proper
|
|
|
|
// diagnostic printer in case of error in SkipCComment().
|
|
|
|
TokStart = CurPtr;
|
|
|
|
++CurPtr;
|
|
|
|
if (SkipCComment())
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
TokStart = CurPtr;
|
|
|
|
PrintError(CurPtr, "Unexpected character");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must not increment CurPtr after the comment was lexed.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Do not allow any non-whitespaces after the directive.
|
|
|
|
TokStart = CurPtr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
++CurPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TGLexer::prepSkipToLineEnd() {
|
|
|
|
while (*CurPtr != '\n' && *CurPtr != '\r' && CurPtr != CurBuf.end())
|
|
|
|
++CurPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TGLexer::prepIsProcessingEnabled() {
|
|
|
|
for (auto I = PrepIncludeStack.back()->rbegin(),
|
|
|
|
E = PrepIncludeStack.back()->rend();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (!I->IsDefined)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TGLexer::prepReportPreprocessorStackError() {
|
|
|
|
if (PrepIncludeStack.back()->empty())
|
|
|
|
PrintFatalError("prepReportPreprocessorStackError() called with "
|
|
|
|
"empty control stack");
|
|
|
|
|
|
|
|
auto &PrepControl = PrepIncludeStack.back()->back();
|
|
|
|
PrintError(CurBuf.end(), "Reached EOF without matching #endif");
|
|
|
|
PrintError(PrepControl.SrcPos, "The latest preprocessor control is here");
|
|
|
|
|
|
|
|
TokStart = CurPtr;
|
|
|
|
}
|