26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/SourceMgr.h"
34 using llvm::MemoryBuffer;
35 using llvm::SourceMgr;
41 class CustomDialectAsmParser :
public AsmParserImpl<DialectAsmParser> {
43 CustomDialectAsmParser(StringRef fullSpec,
Parser &parser)
46 ~CustomDialectAsmParser()
override =
default;
50 StringRef getFullSymbolSpec()
const override {
return fullSpec; }
67 bool &isCodeCompletion) {
76 assert(*curPtr ==
'<');
81 auto emitPunctError = [&] {
82 return emitError() <<
"unbalanced '" << nestedPunctuation.back()
83 <<
"' character in pretty dialect name";
86 auto checkNestedPunctuation = [&](
char expectedToken) -> ParseResult {
87 if (nestedPunctuation.back() != expectedToken)
88 return emitPunctError();
89 nestedPunctuation.pop_back();
95 if (curPtr == codeCompleteLoc) {
96 isCodeCompletion =
true;
97 nestedPunctuation.clear();
105 if (!nestedPunctuation.empty())
106 return emitPunctError();
107 return emitError(
"unexpected nul or EOF in pretty dialect name");
112 nestedPunctuation.push_back(c);
122 if (failed(checkNestedPunctuation(
'<')))
126 if (failed(checkNestedPunctuation(
'[')))
130 if (failed(checkNestedPunctuation(
'(')))
134 if (failed(checkNestedPunctuation(
'{')))
145 isCodeCompletion =
true;
146 nestedPunctuation.clear();
159 }
while (!nestedPunctuation.empty());
165 unsigned length = curPtr - body.begin();
166 body = StringRef(body.data(), length);
171 template <
typename Symbol,
typename SymbolAliasMap,
typename CreateFn>
173 SymbolAliasMap &aliases,
174 CreateFn &&createSymbol) {
178 StringRef identifier = tok.
getSpelling().drop_front();
188 auto [dialectName, symbolData] = identifier.split(
'.');
189 bool isPrettyName = !symbolData.empty() || identifier.back() ==
'.';
193 bool hasTrailingData =
199 if (!hasTrailingData && !isPrettyName) {
201 auto aliasIt = aliases.find(identifier);
202 if (aliasIt == aliases.end())
207 if constexpr (std::is_same_v<Symbol, Type>)
212 return aliasIt->second;
220 symbolData = StringRef(dialectName.end(), 0);
223 bool isCodeCompletion =
false;
226 symbolData = symbolData.drop_front();
230 if (!isCodeCompletion)
231 symbolData = symbolData.drop_back();
233 loc = SMLoc::getFromPointer(symbolData.data());
241 return createSymbol(dialectName, symbolData, loc);
254 Attribute attr = parseExtendedSymbol<Attribute>(
256 [&](StringRef dialectName, StringRef symbolData, SMLoc loc) ->
Attribute {
258 Type attrType = type;
259 if (consumeIf(Token::colon) && !(attrType = parseType()))
263 if (Dialect *dialect =
264 builder.getContext()->getOrLoadDialect(dialectName)) {
266 const char *curLexerPos = getToken().getLoc().getPointer();
267 resetToken(symbolData.data());
270 CustomDialectAsmParser customParser(symbolData, *this);
271 Attribute attr = dialect->parseAttribute(customParser, attrType);
272 resetToken(curLexerPos);
277 return OpaqueAttr::getChecked(
283 auto typedAttr = dyn_cast_or_null<TypedAttr>(attr);
284 if (type && typedAttr && typedAttr.getType() != type) {
285 emitError(
"attribute type different than expected: expected ")
286 << type <<
", but got " << typedAttr.getType();
299 Type Parser::parseExtendedType() {
301 return parseExtendedSymbol<Type>(
302 *
this, state.asmState, state.symbols.typeAliasDefinitions,
303 [&](StringRef dialectName, StringRef symbolData, SMLoc loc) ->
Type {
305 if (auto *dialect = ctx->getOrLoadDialect(dialectName)) {
307 const char *curLexerPos = getToken().getLoc().getPointer();
308 resetToken(symbolData.data());
311 CustomDialectAsmParser customParser(symbolData, *this);
312 Type type = dialect->parseType(customParser);
313 resetToken(curLexerPos);
318 return OpaqueType::getChecked([&] { return emitError(loc); },
330 template <
typename T,
typename ParserFn>
332 size_t *numReadOut,
bool isKnownNullTerminated,
333 ParserFn &&parserFn) {
337 isKnownNullTerminated
338 ? MemoryBuffer::getMemBuffer(inputStr,
340 : MemoryBuffer::getMemBufferCopy(inputStr, inputStr);
342 sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());
350 T symbol = parserFn(parser);
357 endTok.
getLoc().getPointer() - startTok.
getLoc().getPointer();
359 *numReadOut = numRead;
360 }
else if (numRead != inputStr.size()) {
362 << inputStr.drop_front(numRead) <<
"'";
369 Type type,
size_t *numRead,
370 bool isKnownNullTerminated) {
371 return parseSymbol<Attribute>(
372 attrStr, context, numRead, isKnownNullTerminated,
376 bool isKnownNullTerminated) {
377 return parseSymbol<Type>(typeStr, context, numRead, isKnownNullTerminated,
static Symbol parseExtendedSymbol(Parser &p, AsmParserState *asmState, SymbolAliasMap &aliases, CreateFn &&createSymbol)
Parse an extended dialect symbol.
static T parseSymbol(StringRef inputStr, MLIRContext *context, size_t *numReadOut, bool isKnownNullTerminated, ParserFn &&parserFn)
Parses a symbol, of type 'T', and returns it if parsing was successful.
static MLIRContext * getContext(OpFoldResult val)
This class represents state from a parsed MLIR textual format string.
void addTypeAliasUses(StringRef name, SMRange locations)
void addAttrAliasUses(StringRef name, SMRange locations)
Attributes are known-constant values of operations.
The DialectAsmParser has methods for interacting with the asm parser when parsing attributes and type...
const char * getCodeCompleteLoc() const
Return the code completion location of the lexer, or nullptr if there is none.
MLIRContext is the top-level object for a collection of MLIR operations.
This class represents a configuration for the MLIR assembly parser.
This represents a token in the MLIR syntax.
SMRange getLocRange() const
bool isCodeCompletion() const
Returns true if the current token represents a code completion.
StringRef getSpelling() const
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides the implementation of the generic parser methods within AsmParser.
This class implement support for parsing global entities like attributes and types.
Type parseType()
Parse an arbitrary type.
InFlightDiagnostic emitError(const Twine &message={})
Emit an error and return failure.
ParserState & state
The Parser is subclassed and reinstantiated.
Attribute parseAttribute(Type type={})
Parse an arbitrary attribute with an optional type.
StringRef getTokenSpelling() const
void consumeToken()
Advance the current lexer onto the next token.
ParseResult parseDialectSymbolBody(StringRef &body, bool &isCodeCompletion)
Parse the body of a dialect symbol, which starts and ends with <>'s, and may be recursive.
MLIRContext * getContext() const
InFlightDiagnostic emitWrongTokenError(const Twine &message={})
Emit an error about a "wrong token".
void resetToken(const char *tokPos)
Reset the parser to the given lexer position.
Attribute parseExtendedAttr(Type type)
Parse an extended attribute.
const Token & getToken() const
Return the current token the parser is inspecting.
Attribute codeCompleteDialectSymbol(const llvm::StringMap< Attribute > &aliases)
Include the generated interface declarations.
const FrozenRewritePatternSet GreedyRewriteConfig config
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
Attribute parseAttribute(llvm::StringRef attrStr, MLIRContext *context, Type type={}, size_t *numRead=nullptr, bool isKnownNullTerminated=false)
This parses a single MLIR attribute to an MLIR context if it was valid.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
Type parseType(llvm::StringRef typeStr, MLIRContext *context, size_t *numRead=nullptr, bool isKnownNullTerminated=false)
This parses a single MLIR type to an MLIR context if it was valid.
This class refers to all of the state maintained globally by the parser, such as the current lexer po...
SymbolState & symbols
The current state for symbol parsing.
Lexer lex
The lexer for the source file we're parsing.
Token curToken
This is the next token that hasn't been consumed yet.
AsmParserState * asmState
An optional pointer to a struct containing high level parser state to be populated during parsing.
This class contains record of any parsed top-level symbols.
llvm::StringMap< Attribute > attributeAliasDefinitions
A map from attribute alias identifier to Attribute.