1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
//===- DialectSymbolParser.cpp - MLIR Dialect Symbol Parser --------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements the parser for the dialect symbols, such as extended
// attributes and types.
//
//===----------------------------------------------------------------------===//
#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 {
/// This class provides the main implementation of the DialectAsmParser that
/// allows for dialects to parse attributes and types. This allows for dialect
/// hooking into the main MLIR parsing logic.
class CustomDialectAsmParser : public AsmParserImpl<DialectAsmParser> {
public:
CustomDialectAsmParser(StringRef fullSpec, Parser &parser)
: AsmParserImpl<DialectAsmParser>(parser.getToken().getLoc(), parser),
fullSpec(fullSpec) {}
~CustomDialectAsmParser() override = default;
/// Returns the full specification of the symbol being parsed. This allows
/// for using a separate parser if necessary.
StringRef getFullSymbolSpec() const override { return fullSpec; }
private:
/// The full symbol specification.
StringRef fullSpec;
};
} // namespace
///
/// pretty-dialect-sym-body ::= '<' pretty-dialect-sym-contents+ '>'
/// pretty-dialect-sym-contents ::= pretty-dialect-sym-body
/// | '(' pretty-dialect-sym-contents+ ')'
/// | '[' pretty-dialect-sym-contents+ ']'
/// | '{' pretty-dialect-sym-contents+ '}'
/// | '[^[<({>\])}\0]+'
///
ParseResult Parser::parseDialectSymbolBody(StringRef &body,
bool &isCodeCompletion) {
// Symbol bodies are a relatively unstructured format that contains a series
// of properly nested punctuation, with anything else in the middle. Scan
// ahead to find it and consume it if successful, otherwise emit an error.
const char *curPtr = getTokenSpelling().data();
// Scan over the nested punctuation, bailing out on error and consuming until
// we find the end. We know that we're currently looking at the '<', so we can
// go until we find the matching '>' character.
assert(*curPtr == '<');
SmallVector<char, 8> nestedPunctuation;
const char *codeCompleteLoc = state.lex.getCodeCompleteLoc();
// Functor used to emit an unbalanced punctuation error.
auto emitPunctError = [&] {
return emitError() << "unbalanced '" << nestedPunctuation.back()
<< "' character in pretty dialect name";
};
// Functor used to check for unbalanced punctuation.
auto checkNestedPunctuation = [&](char expectedToken) -> ParseResult {
if (nestedPunctuation.back() != expectedToken)
return emitPunctError();
nestedPunctuation.pop_back();
return success();
};
do {
// Handle code completions, which may appear in the middle of the symbol
// body.
if (curPtr == codeCompleteLoc) {
isCodeCompletion = true;
nestedPunctuation.clear();
break;
}
char c = *curPtr++;
switch (c) {
case '\0':
// This also handles the EOF case.
if (!nestedPunctuation.empty())
return emitPunctError();
return emitError("unexpected nul or EOF in pretty dialect name");
case '<':
case '[':
case '(':
case '{':
nestedPunctuation.push_back(c);
continue;
case '-':
// The sequence `->` is treated as special token.
if (*curPtr == '>')
++curPtr;
continue;
case '>':
if (failed(checkNestedPunctuation('<')))
return failure();
break;
case ']':
if (failed(checkNestedPunctuation('[')))
return failure();
break;
case ')':
if (failed(checkNestedPunctuation('(')))
return failure();
break;
case '}':
if (failed(checkNestedPunctuation('{')))
return failure();
break;
case '"': {
// Dispatch to the lexer to lex past strings.
resetToken(curPtr - 1);
curPtr = state.curToken.getEndLoc().getPointer();
// Handle code completions, which may appear in the middle of the symbol
// body.
if (state.curToken.isCodeCompletion()) {
isCodeCompletion = true;
nestedPunctuation.clear();
break;
}
// Otherwise, ensure this token was actually a string.
if (state.curToken.isNot(Token::string))
return failure();
break;
}
default:
continue;
}
} while (!nestedPunctuation.empty());
// Ok, we succeeded, remember where we stopped, reset the lexer to know it is
// consuming all this stuff, and return.
resetToken(curPtr);
unsigned length = curPtr - body.begin();
body = StringRef(body.data(), length);
return success();
}
/// Parse an extended dialect symbol.
template <typename Symbol, typename SymbolAliasMap, typename CreateFn>
static Symbol parseExtendedSymbol(Parser &p, SymbolAliasMap &aliases,
CreateFn &&createSymbol) {
Token tok = p.getToken();
// Handle code completion of the extended symbol.
StringRef identifier = tok.getSpelling().drop_front();
if (tok.isCodeCompletion() && identifier.empty())
return p.codeCompleteDialectSymbol(aliases);
// Parse the dialect namespace.
SMLoc loc = p.getToken().getLoc();
p.consumeToken();
// Check to see if this is a pretty name.
auto [dialectName, symbolData] = identifier.split('.');
bool isPrettyName = !symbolData.empty() || identifier.back() == '.';
// Check to see if the symbol has trailing data, i.e. has an immediately
// following '<'.
bool hasTrailingData =
p.getToken().is(Token::less) &&
identifier.bytes_end() == p.getTokenSpelling().bytes_begin();
// If there is no '<' token following this, and if the typename contains no
// dot, then we are parsing a symbol alias.
if (!hasTrailingData && !isPrettyName) {
// Check for an alias for this type.
auto aliasIt = aliases.find(identifier);
if (aliasIt == aliases.end())
return (p.emitWrongTokenError("undefined symbol alias id '" + identifier +
"'"),
nullptr);
return aliasIt->second;
}
// If this isn't an alias, we are parsing a dialect-specific symbol. If the
// name contains a dot, then this is the "pretty" form. If not, it is the
// verbose form that looks like <...>.
if (!isPrettyName) {
// Point the symbol data to the end of the dialect name to start.
symbolData = StringRef(dialectName.end(), 0);
// Parse the body of the symbol.
bool isCodeCompletion = false;
if (p.parseDialectSymbolBody(symbolData, isCodeCompletion))
return nullptr;
symbolData = symbolData.drop_front();
// If the body contained a code completion it won't have the trailing `>`
// token, so don't drop it.
if (!isCodeCompletion)
symbolData = symbolData.drop_back();
} else {
loc = SMLoc::getFromPointer(symbolData.data());
// If the dialect's symbol is followed immediately by a <, then lex the body
// of it into prettyName.
if (hasTrailingData && p.parseDialectSymbolBody(symbolData))
return nullptr;
}
return createSymbol(dialectName, symbolData, loc);
}
/// Parse an extended attribute.
///
/// extended-attribute ::= (dialect-attribute | attribute-alias)
/// dialect-attribute ::= `#` dialect-namespace `<` attr-data `>`
/// (`:` type)?
/// | `#` alias-name pretty-dialect-sym-body? (`:` type)?
/// attribute-alias ::= `#` alias-name
///
Attribute Parser::parseExtendedAttr(Type type) {
MLIRContext *ctx = getContext();
Attribute attr = parseExtendedSymbol<Attribute>(
*this, state.symbols.attributeAliasDefinitions,
[&](StringRef dialectName, StringRef symbolData, SMLoc loc) -> Attribute {
// Parse an optional trailing colon type.
Type attrType = type;
if (consumeIf(Token::colon) && !(attrType = parseType()))
return Attribute();
// If we found a registered dialect, then ask it to parse the attribute.
if (Dialect *dialect =
builder.getContext()->getOrLoadDialect(dialectName)) {
// Temporarily reset the lexer to let the dialect parse the attribute.
const char *curLexerPos = getToken().getLoc().getPointer();
resetToken(symbolData.data());
// Parse the attribute.
CustomDialectAsmParser customParser(symbolData, *this);
Attribute attr = dialect->parseAttribute(customParser, attrType);
resetToken(curLexerPos);
return attr;
}
// Otherwise, form a new opaque attribute.
return OpaqueAttr::getChecked(
[&] { return emitError(loc); }, StringAttr::get(ctx, dialectName),
symbolData, attrType ? attrType : NoneType::get(ctx));
});
// Ensure that the attribute has the same type as requested.
auto typedAttr = dyn_cast_or_null<TypedAttr>(attr);
if (type && typedAttr && typedAttr.getType() != type) {
emitError("attribute type different than expected: expected ")
<< type << ", but got " << typedAttr.getType();
return nullptr;
}
return attr;
}
/// Parse an extended type.
///
/// extended-type ::= (dialect-type | type-alias)
/// dialect-type ::= `!` dialect-namespace `<` `"` type-data `"` `>`
/// dialect-type ::= `!` alias-name pretty-dialect-attribute-body?
/// type-alias ::= `!` alias-name
///
Type Parser::parseExtendedType() {
MLIRContext *ctx = getContext();
return parseExtendedSymbol<Type>(
*this, state.symbols.typeAliasDefinitions,
[&](StringRef dialectName, StringRef symbolData, SMLoc loc) -> Type {
// If we found a registered dialect, then ask it to parse the type.
if (auto *dialect = ctx->getOrLoadDialect(dialectName)) {
// Temporarily reset the lexer to let the dialect parse the type.
const char *curLexerPos = getToken().getLoc().getPointer();
resetToken(symbolData.data());
// Parse the type.
CustomDialectAsmParser customParser(symbolData, *this);
Type type = dialect->parseType(customParser);
resetToken(curLexerPos);
return type;
}
// Otherwise, form a new opaque type.
return OpaqueType::getChecked([&] { return emitError(loc); },
StringAttr::get(ctx, dialectName),
symbolData);
});
}
//===----------------------------------------------------------------------===//
// mlir::parseAttribute/parseType
//===----------------------------------------------------------------------===//
/// Parses a symbol, of type 'T', and returns it if parsing was successful. If
/// parsing failed, nullptr is returned.
template <typename T, typename ParserFn>
static T parseSymbol(StringRef inputStr, MLIRContext *context,
size_t *numReadOut, bool isKnownNullTerminated,
ParserFn &&parserFn) {
// Set the buffer name to the string being parsed, so that it appears in error
// diagnostics.
auto memBuffer =
isKnownNullTerminated
? MemoryBuffer::getMemBuffer(inputStr,
/*BufferName=*/inputStr)
: MemoryBuffer::getMemBufferCopy(inputStr, /*BufferName=*/inputStr);
SourceMgr sourceMgr;
sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());
SymbolState aliasState;
ParserConfig config(context);
ParserState state(sourceMgr, config, aliasState, /*asmState=*/nullptr,
/*codeCompleteContext=*/nullptr);
Parser parser(state);
Token startTok = parser.getToken();
T symbol = parserFn(parser);
if (!symbol)
return T();
// Provide the number of bytes that were read.
Token endTok = parser.getToken();
size_t numRead =
endTok.getLoc().getPointer() - startTok.getLoc().getPointer();
if (numReadOut) {
*numReadOut = numRead;
} else if (numRead != inputStr.size()) {
parser.emitError(endTok.getLoc()) << "found trailing characters: '"
<< inputStr.drop_front(numRead) << "'";
return T();
}
return symbol;
}
Attribute mlir::parseAttribute(StringRef attrStr, MLIRContext *context,
Type type, size_t *numRead,
bool isKnownNullTerminated) {
return parseSymbol<Attribute>(
attrStr, context, numRead, isKnownNullTerminated,
[type](Parser &parser) { return parser.parseAttribute(type); });
}
Type mlir::parseType(StringRef typeStr, MLIRContext *context, size_t *numRead,
bool isKnownNullTerminated) {
return parseSymbol<Type>(typeStr, context, numRead, isKnownNullTerminated,
[](Parser &parser) { return parser.parseType(); });
}
|