#include "AsmParserImpl.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/DialectImplementation.h"
#include "llvm/Support/SourceMgr.h"
using namespace mlir;
using namespace mlir::detail;
using llvm::MemoryBuffer;
using llvm::SourceMgr;
namespace {
class CustomDialectAsmParser : public AsmParserImpl<DialectAsmParser> {
public:
CustomDialectAsmParser(StringRef fullSpec, Parser &parser)
: AsmParserImpl<DialectAsmParser>(parser.getToken().getLoc(), parser),
fullSpec(fullSpec) {}
~CustomDialectAsmParser() override = default;
StringRef getFullSymbolSpec() const override { return fullSpec; }
private:
StringRef fullSpec;
};
}
ParseResult Parser::parseDialectSymbolBody(StringRef &body,
bool &isCodeCompletion) {
const char *curPtr = getTokenSpelling().data();
assert(*curPtr == '<');
SmallVector<char, 8> nestedPunctuation;
const char *codeCompleteLoc = state.lex.getCodeCompleteLoc();
do {
if (curPtr == codeCompleteLoc) {
isCodeCompletion = true;
nestedPunctuation.clear();
break;
}
char c = *curPtr++;
switch (c) {
case '\0':
if (!nestedPunctuation.empty()) {
return emitError() << "unbalanced '" << nestedPunctuation.back()
<< "' character in pretty dialect name";
}
return emitError("unexpected nul or EOF in pretty dialect name");
case '<':
case '[':
case '(':
case '{':
nestedPunctuation.push_back(c);
continue;
case '-':
if (*curPtr == '>')
++curPtr;
continue;
case '>':
if (nestedPunctuation.pop_back_val() != '<')
return emitError("unbalanced '>' character in pretty dialect name");
break;
case ']':
if (nestedPunctuation.pop_back_val() != '[')
return emitError("unbalanced ']' character in pretty dialect name");
break;
case ')':
if (nestedPunctuation.pop_back_val() != '(')
return emitError("unbalanced ')' character in pretty dialect name");
break;
case '}':
if (nestedPunctuation.pop_back_val() != '{')
return emitError("unbalanced '}' character in pretty dialect name");
break;
case '"': {
resetToken(curPtr - 1);
curPtr = state.curToken.getEndLoc().getPointer();
if (state.curToken.isCodeCompletion()) {
isCodeCompletion = true;
nestedPunctuation.clear();
break;
}
if (state.curToken.isNot(Token::string))
return failure();
break;
}
default:
continue;
}
} while (!nestedPunctuation.empty());
resetToken(curPtr);
unsigned length = curPtr - body.begin();
body = StringRef(body.data(), length);
return success();
}
template <typename Symbol, typename SymbolAliasMap, typename CreateFn>
static Symbol parseExtendedSymbol(Parser &p, SymbolAliasMap &aliases,
CreateFn &&createSymbol) {
Token tok = p.getToken();
StringRef identifier = tok.getSpelling().drop_front();
if (tok.isCodeCompletion() && identifier.empty())
return p.codeCompleteDialectSymbol(aliases);
SMLoc loc = p.getToken().getLoc();
p.consumeToken();
StringRef dialectName;
StringRef symbolData;
std::tie(dialectName, symbolData) = identifier.split('.');
bool isPrettyName = !symbolData.empty() || identifier.back() == '.';
bool hasTrailingData =
p.getToken().is(Token::less) &&
identifier.bytes_end() == p.getTokenSpelling().bytes_begin();
if (!hasTrailingData && !isPrettyName) {
auto aliasIt = aliases.find(identifier);
if (aliasIt == aliases.end())
return (p.emitWrongTokenError("undefined symbol alias id '" + identifier +
"'"),
nullptr);
return aliasIt->second;
}
if (!isPrettyName) {
symbolData = StringRef(dialectName.end(), 0);
bool isCodeCompletion = false;
if (p.parseDialectSymbolBody(symbolData, isCodeCompletion))
return nullptr;
symbolData = symbolData.drop_front();
if (!isCodeCompletion)
symbolData = symbolData.drop_back();
} else {
loc = SMLoc::getFromPointer(symbolData.data());
if (hasTrailingData && p.parseDialectSymbolBody(symbolData))
return nullptr;
}
return createSymbol(dialectName, symbolData, loc);
}
Attribute Parser::parseExtendedAttr(Type type) {
MLIRContext *ctx = getContext();
Attribute attr = parseExtendedSymbol<Attribute>(
*this, state.symbols.attributeAliasDefinitions,
[&](StringRef dialectName, StringRef symbolData, SMLoc loc) -> Attribute {
Type attrType = type;
if (consumeIf(Token::colon) && !(attrType = parseType()))
return Attribute();
if (Dialect *dialect =
builder.getContext()->getOrLoadDialect(dialectName)) {
const char *curLexerPos = getToken().getLoc().getPointer();
resetToken(symbolData.data());
CustomDialectAsmParser customParser(symbolData, *this);
Attribute attr = dialect->parseAttribute(customParser, attrType);
resetToken(curLexerPos);
return attr;
}
return OpaqueAttr::getChecked(
[&] { return emitError(loc); }, StringAttr::get(ctx, dialectName),
symbolData, attrType ? attrType : NoneType::get(ctx));
});
if (attr && type && attr.getType() != type) {
emitError("attribute type different than expected: expected ")
<< type << ", but got " << attr.getType();
return nullptr;
}
return attr;
}
Type Parser::parseExtendedType() {
MLIRContext *ctx = getContext();
return parseExtendedSymbol<Type>(
*this, state.symbols.typeAliasDefinitions,
[&](StringRef dialectName, StringRef symbolData, SMLoc loc) -> Type {
if (auto *dialect = ctx->getOrLoadDialect(dialectName)) {
const char *curLexerPos = getToken().getLoc().getPointer();
resetToken(symbolData.data());
CustomDialectAsmParser customParser(symbolData, *this);
Type type = dialect->parseType(customParser);
resetToken(curLexerPos);
return type;
}
return OpaqueType::getChecked([&] { return emitError(loc); },
StringAttr::get(ctx, dialectName),
symbolData);
});
}
template <typename T, typename ParserFn>
static T parseSymbol(StringRef inputStr, MLIRContext *context, size_t &numRead,
ParserFn &&parserFn) {
SourceMgr sourceMgr;
auto memBuffer = MemoryBuffer::getMemBuffer(
inputStr, "<mlir_parser_buffer>",
false);
sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());
SymbolState aliasState;
ParserConfig config(context);
ParserState state(sourceMgr, config, aliasState, nullptr,
nullptr);
Parser parser(state);
SourceMgrDiagnosticHandler handler(
const_cast<llvm::SourceMgr &>(parser.getSourceMgr()),
parser.getContext());
Token startTok = parser.getToken();
T symbol = parserFn(parser);
if (!symbol)
return T();
Token endTok = parser.getToken();
numRead = static_cast<size_t>(endTok.getLoc().getPointer() -
startTok.getLoc().getPointer());
return symbol;
}
Attribute mlir::parseAttribute(StringRef attrStr, MLIRContext *context) {
size_t numRead = 0;
return parseAttribute(attrStr, context, numRead);
}
Attribute mlir::parseAttribute(StringRef attrStr, Type type) {
size_t numRead = 0;
return parseAttribute(attrStr, type, numRead);
}
Attribute mlir::parseAttribute(StringRef attrStr, MLIRContext *context,
size_t &numRead) {
return parseSymbol<Attribute>(attrStr, context, numRead, [](Parser &parser) {
return parser.parseAttribute();
});
}
Attribute mlir::parseAttribute(StringRef attrStr, Type type, size_t &numRead) {
return parseSymbol<Attribute>(
attrStr, type.getContext(), numRead,
[type](Parser &parser) { return parser.parseAttribute(type); });
}
Type mlir::parseType(StringRef typeStr, MLIRContext *context) {
size_t numRead = 0;
return parseType(typeStr, context, numRead);
}
Type mlir::parseType(StringRef typeStr, MLIRContext *context, size_t &numRead) {
return parseSymbol<Type>(typeStr, context, numRead,
[](Parser &parser) { return parser.parseType(); });
}