| 1 | //===- Parser.h - MLIR Base Parser Class ------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef MLIR_LIB_ASMPARSER_PARSER_H |
| 10 | #define MLIR_LIB_ASMPARSER_PARSER_H |
| 11 | |
| 12 | #include "ParserState.h" |
| 13 | #include "mlir/IR/Builders.h" |
| 14 | #include "mlir/IR/OpImplementation.h" |
| 15 | #include <optional> |
| 16 | |
| 17 | namespace mlir { |
| 18 | namespace detail { |
| 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | // Parser |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | /// This class implement support for parsing global entities like attributes and |
| 25 | /// types. It is intended to be subclassed by specialized subparsers that |
| 26 | /// include state. |
| 27 | class Parser { |
| 28 | public: |
| 29 | using Delimiter = OpAsmParser::Delimiter; |
| 30 | |
| 31 | Builder builder; |
| 32 | |
| 33 | Parser(ParserState &state) |
| 34 | : builder(state.config.getContext()), state(state) {} |
| 35 | |
| 36 | // Helper methods to get stuff from the parser-global state. |
| 37 | ParserState &getState() const { return state; } |
| 38 | MLIRContext *getContext() const { return state.config.getContext(); } |
| 39 | const llvm::SourceMgr &getSourceMgr() { return state.lex.getSourceMgr(); } |
| 40 | |
| 41 | /// Parse a comma-separated list of elements up until the specified end token. |
| 42 | ParseResult |
| 43 | parseCommaSeparatedListUntil(Token::Kind rightToken, |
| 44 | function_ref<ParseResult()> parseElement, |
| 45 | bool allowEmptyList = true); |
| 46 | |
| 47 | /// Parse a list of comma-separated items with an optional delimiter. If a |
| 48 | /// delimiter is provided, then an empty list is allowed. If not, then at |
| 49 | /// least one element will be parsed. |
| 50 | ParseResult |
| 51 | parseCommaSeparatedList(Delimiter delimiter, |
| 52 | function_ref<ParseResult()> parseElementFn, |
| 53 | StringRef contextMessage = StringRef()); |
| 54 | |
| 55 | /// Parse a comma separated list of elements that must have at least one entry |
| 56 | /// in it. |
| 57 | ParseResult |
| 58 | parseCommaSeparatedList(function_ref<ParseResult()> parseElementFn) { |
| 59 | return parseCommaSeparatedList(delimiter: Delimiter::None, parseElementFn); |
| 60 | } |
| 61 | |
| 62 | /// Parse the body of a dialect symbol, which starts and ends with <>'s, and |
| 63 | /// may be recursive. Return with the 'body' StringRef encompassing the entire |
| 64 | /// body. `isCodeCompletion` is set to true if the body contained a code |
| 65 | /// completion location, in which case the body is only populated up to the |
| 66 | /// completion. |
| 67 | ParseResult parseDialectSymbolBody(StringRef &body, bool &isCodeCompletion); |
| 68 | ParseResult parseDialectSymbolBody(StringRef &body) { |
| 69 | bool isCodeCompletion = false; |
| 70 | return parseDialectSymbolBody(body, isCodeCompletion); |
| 71 | } |
| 72 | |
| 73 | // We have two forms of parsing methods - those that return a non-null |
| 74 | // pointer on success, and those that return a ParseResult to indicate whether |
| 75 | // they returned a failure. The second class fills in by-reference arguments |
| 76 | // as the results of their action. |
| 77 | |
| 78 | //===--------------------------------------------------------------------===// |
| 79 | // Error Handling |
| 80 | //===--------------------------------------------------------------------===// |
| 81 | |
| 82 | /// Emit an error and return failure. |
| 83 | InFlightDiagnostic emitError(const Twine &message = {}); |
| 84 | InFlightDiagnostic emitError(SMLoc loc, const Twine &message = {}); |
| 85 | |
| 86 | /// Emit an error about a "wrong token". If the current token is at the |
| 87 | /// start of a source line, this will apply heuristics to back up and report |
| 88 | /// the error at the end of the previous line, which is where the expected |
| 89 | /// token is supposed to be. |
| 90 | InFlightDiagnostic emitWrongTokenError(const Twine &message = {}); |
| 91 | |
| 92 | /// Encode the specified source location information into an attribute for |
| 93 | /// attachment to the IR. |
| 94 | Location getEncodedSourceLocation(SMLoc loc) { |
| 95 | return state.lex.getEncodedSourceLocation(loc); |
| 96 | } |
| 97 | |
| 98 | //===--------------------------------------------------------------------===// |
| 99 | // Token Parsing |
| 100 | //===--------------------------------------------------------------------===// |
| 101 | |
| 102 | /// Return the current token the parser is inspecting. |
| 103 | const Token &getToken() const { return state.curToken; } |
| 104 | StringRef getTokenSpelling() const { return state.curToken.getSpelling(); } |
| 105 | |
| 106 | /// Return the last parsed token. |
| 107 | const Token &getLastToken() const { return state.lastToken; } |
| 108 | |
| 109 | /// If the current token has the specified kind, consume it and return true. |
| 110 | /// If not, return false. |
| 111 | bool consumeIf(Token::Kind kind) { |
| 112 | if (state.curToken.isNot(k: kind)) |
| 113 | return false; |
| 114 | consumeToken(kind); |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | /// Advance the current lexer onto the next token. |
| 119 | void consumeToken() { |
| 120 | assert(state.curToken.isNot(Token::eof, Token::error) && |
| 121 | "shouldn't advance past EOF or errors" ); |
| 122 | state.lastToken = state.curToken; |
| 123 | state.curToken = state.lex.lexToken(); |
| 124 | } |
| 125 | |
| 126 | /// Advance the current lexer onto the next token, asserting what the expected |
| 127 | /// current token is. This is preferred to the above method because it leads |
| 128 | /// to more self-documenting code with better checking. |
| 129 | void consumeToken(Token::Kind kind) { |
| 130 | assert(state.curToken.is(kind) && "consumed an unexpected token" ); |
| 131 | consumeToken(); |
| 132 | } |
| 133 | |
| 134 | /// Reset the parser to the given lexer position. Resetting the parser/lexer |
| 135 | /// position does not update 'state.lastToken'. 'state.lastToken' is the |
| 136 | /// last parsed token, and is used to provide the scope end location for |
| 137 | /// OperationDefinitions. To ensure the correctness of the end location, the |
| 138 | /// last consumed token of an OperationDefinition needs to be the last token |
| 139 | /// belonging to it. |
| 140 | void resetToken(const char *tokPos) { |
| 141 | state.lex.resetPointer(newPointer: tokPos); |
| 142 | state.curToken = state.lex.lexToken(); |
| 143 | } |
| 144 | |
| 145 | /// Consume the specified token if present and return success. On failure, |
| 146 | /// output a diagnostic and return failure. |
| 147 | ParseResult parseToken(Token::Kind expectedToken, const Twine &message); |
| 148 | |
| 149 | /// Parses a quoted string token if present. |
| 150 | ParseResult parseOptionalString(std::string *string); |
| 151 | |
| 152 | /// Parse an optional integer value from the stream. |
| 153 | OptionalParseResult parseOptionalInteger(APInt &result); |
| 154 | |
| 155 | /// Parse an optional integer value only in decimal format from the stream. |
| 156 | OptionalParseResult parseOptionalDecimalInteger(APInt &result); |
| 157 | |
| 158 | /// Parse a floating point value from a literal. |
| 159 | ParseResult parseFloatFromLiteral(std::optional<APFloat> &result, |
| 160 | const Token &tok, bool isNegative, |
| 161 | const llvm::fltSemantics &semantics); |
| 162 | |
| 163 | /// Parse a floating point value from an integer literal token. |
| 164 | ParseResult parseFloatFromIntegerLiteral(std::optional<APFloat> &result, |
| 165 | const Token &tok, bool isNegative, |
| 166 | const llvm::fltSemantics &semantics); |
| 167 | |
| 168 | /// Returns true if the current token corresponds to a keyword. |
| 169 | bool isCurrentTokenAKeyword() const { |
| 170 | return getToken().isAny(k1: Token::bare_identifier, k2: Token::inttype) || |
| 171 | getToken().isKeyword(); |
| 172 | } |
| 173 | |
| 174 | /// Parse a keyword, if present, into 'keyword'. |
| 175 | ParseResult parseOptionalKeyword(StringRef *keyword); |
| 176 | |
| 177 | /// Parse an optional keyword or string and set instance into 'result'.` |
| 178 | ParseResult parseOptionalKeywordOrString(std::string *result); |
| 179 | |
| 180 | //===--------------------------------------------------------------------===// |
| 181 | // Resource Parsing |
| 182 | //===--------------------------------------------------------------------===// |
| 183 | |
| 184 | /// Parse a handle to a dialect resource within the assembly format. |
| 185 | FailureOr<AsmDialectResourceHandle> |
| 186 | parseResourceHandle(const OpAsmDialectInterface *dialect, std::string &name); |
| 187 | FailureOr<AsmDialectResourceHandle> parseResourceHandle(Dialect *dialect); |
| 188 | |
| 189 | //===--------------------------------------------------------------------===// |
| 190 | // Type Parsing |
| 191 | //===--------------------------------------------------------------------===// |
| 192 | |
| 193 | /// Invoke the `getChecked` method of the given Attribute or Type class, using |
| 194 | /// the provided location to emit errors in the case of failure. Note that |
| 195 | /// unlike `OpBuilder::getType`, this method does not implicitly insert a |
| 196 | /// context parameter. |
| 197 | template <typename T, typename... ParamsT> |
| 198 | T getChecked(SMLoc loc, ParamsT &&...params) { |
| 199 | return T::getChecked([&] { return emitError(loc); }, |
| 200 | std::forward<ParamsT>(params)...); |
| 201 | } |
| 202 | |
| 203 | ParseResult parseFunctionResultTypes(SmallVectorImpl<Type> &elements); |
| 204 | ParseResult parseTypeListNoParens(SmallVectorImpl<Type> &elements); |
| 205 | ParseResult parseTypeListParens(SmallVectorImpl<Type> &elements); |
| 206 | |
| 207 | /// Optionally parse a type. |
| 208 | OptionalParseResult parseOptionalType(Type &type); |
| 209 | |
| 210 | /// Parse an arbitrary type. |
| 211 | Type parseType(); |
| 212 | |
| 213 | /// Parse a complex type. |
| 214 | Type parseComplexType(); |
| 215 | |
| 216 | /// Parse an extended type. |
| 217 | Type parseExtendedType(); |
| 218 | |
| 219 | /// Parse a function type. |
| 220 | Type parseFunctionType(); |
| 221 | |
| 222 | /// Parse a memref type. |
| 223 | Type parseMemRefType(); |
| 224 | |
| 225 | /// Parse a non function type. |
| 226 | Type parseNonFunctionType(); |
| 227 | |
| 228 | /// Parse a tensor type. |
| 229 | Type parseTensorType(); |
| 230 | |
| 231 | /// Parse a tuple type. |
| 232 | Type parseTupleType(); |
| 233 | |
| 234 | /// Parse a vector type. |
| 235 | VectorType parseVectorType(); |
| 236 | ParseResult parseVectorDimensionList(SmallVectorImpl<int64_t> &dimensions, |
| 237 | SmallVectorImpl<bool> &scalableDims); |
| 238 | ParseResult parseDimensionListRanked(SmallVectorImpl<int64_t> &dimensions, |
| 239 | bool allowDynamic = true, |
| 240 | bool withTrailingX = true); |
| 241 | ParseResult parseIntegerInDimensionList(int64_t &value); |
| 242 | ParseResult parseXInDimensionList(); |
| 243 | |
| 244 | //===--------------------------------------------------------------------===// |
| 245 | // Attribute Parsing |
| 246 | //===--------------------------------------------------------------------===// |
| 247 | |
| 248 | /// Parse an arbitrary attribute with an optional type. |
| 249 | Attribute parseAttribute(Type type = {}); |
| 250 | |
| 251 | /// Parse an optional attribute with the provided type. |
| 252 | OptionalParseResult parseOptionalAttribute(Attribute &attribute, |
| 253 | Type type = {}); |
| 254 | OptionalParseResult parseOptionalAttribute(ArrayAttr &attribute, Type type); |
| 255 | OptionalParseResult parseOptionalAttribute(StringAttr &attribute, Type type); |
| 256 | OptionalParseResult parseOptionalAttribute(SymbolRefAttr &result, Type type); |
| 257 | |
| 258 | /// Parse an optional attribute that is demarcated by a specific token. |
| 259 | template <typename AttributeT> |
| 260 | OptionalParseResult parseOptionalAttributeWithToken(Token::Kind kind, |
| 261 | AttributeT &attr, |
| 262 | Type type = {}) { |
| 263 | if (getToken().isNot(k: kind)) |
| 264 | return std::nullopt; |
| 265 | |
| 266 | if (Attribute parsedAttr = parseAttribute(type)) { |
| 267 | attr = cast<AttributeT>(parsedAttr); |
| 268 | return success(); |
| 269 | } |
| 270 | return failure(); |
| 271 | } |
| 272 | |
| 273 | /// Parse an attribute dictionary. |
| 274 | ParseResult parseAttributeDict(NamedAttrList &attributes); |
| 275 | |
| 276 | /// Parse a distinct attribute. |
| 277 | Attribute parseDistinctAttr(Type type); |
| 278 | |
| 279 | /// Parse an extended attribute. |
| 280 | Attribute parseExtendedAttr(Type type); |
| 281 | |
| 282 | /// Parse a float attribute. |
| 283 | Attribute parseFloatAttr(Type type, bool isNegative); |
| 284 | |
| 285 | /// Parse a decimal or a hexadecimal literal, which can be either an integer |
| 286 | /// or a float attribute. |
| 287 | Attribute parseDecOrHexAttr(Type type, bool isNegative); |
| 288 | |
| 289 | /// Parse a dense elements attribute. |
| 290 | Attribute parseDenseElementsAttr(Type attrType); |
| 291 | ShapedType parseElementsLiteralType(SMLoc loc, Type type); |
| 292 | |
| 293 | /// Parse a dense resource elements attribute. |
| 294 | Attribute parseDenseResourceElementsAttr(Type attrType); |
| 295 | |
| 296 | /// Parse a DenseArrayAttr. |
| 297 | Attribute parseDenseArrayAttr(Type type); |
| 298 | |
| 299 | /// Parse a sparse elements attribute. |
| 300 | Attribute parseSparseElementsAttr(Type attrType); |
| 301 | |
| 302 | /// Parse a strided layout attribute. |
| 303 | Attribute parseStridedLayoutAttr(); |
| 304 | |
| 305 | //===--------------------------------------------------------------------===// |
| 306 | // Location Parsing |
| 307 | //===--------------------------------------------------------------------===// |
| 308 | |
| 309 | /// Parse a raw location instance. |
| 310 | ParseResult parseLocationInstance(LocationAttr &loc); |
| 311 | |
| 312 | /// Parse a callsite location instance. |
| 313 | ParseResult parseCallSiteLocation(LocationAttr &loc); |
| 314 | |
| 315 | /// Parse a fused location instance. |
| 316 | ParseResult parseFusedLocation(LocationAttr &loc); |
| 317 | |
| 318 | /// Parse a name or FileLineCol location instance. |
| 319 | ParseResult parseNameOrFileLineColRange(LocationAttr &loc); |
| 320 | |
| 321 | //===--------------------------------------------------------------------===// |
| 322 | // Affine Parsing |
| 323 | //===--------------------------------------------------------------------===// |
| 324 | |
| 325 | /// Parse a reference to either an affine map, expr, or an integer set. |
| 326 | ParseResult parseAffineMapOrIntegerSetReference(AffineMap &map, |
| 327 | IntegerSet &set); |
| 328 | ParseResult parseAffineMapReference(AffineMap &map); |
| 329 | ParseResult |
| 330 | parseAffineExprReference(ArrayRef<std::pair<StringRef, AffineExpr>> symbolSet, |
| 331 | AffineExpr &expr); |
| 332 | ParseResult parseIntegerSetReference(IntegerSet &set); |
| 333 | |
| 334 | /// Parse an AffineMap where the dim and symbol identifiers are SSA ids. |
| 335 | ParseResult |
| 336 | parseAffineMapOfSSAIds(AffineMap &map, |
| 337 | function_ref<ParseResult(bool)> parseElement, |
| 338 | Delimiter delimiter); |
| 339 | |
| 340 | /// Parse an AffineExpr where dim and symbol identifiers are SSA ids. |
| 341 | ParseResult |
| 342 | parseAffineExprOfSSAIds(AffineExpr &expr, |
| 343 | function_ref<ParseResult(bool)> parseElement); |
| 344 | |
| 345 | //===--------------------------------------------------------------------===// |
| 346 | // Code Completion |
| 347 | //===--------------------------------------------------------------------===// |
| 348 | |
| 349 | /// The set of various code completion methods. Every completion method |
| 350 | /// returns `failure` to signal that parsing should abort after any desired |
| 351 | /// completions have been enqueued. Note that `failure` is does not mean |
| 352 | /// completion failed, it's just a signal to the parser to stop. |
| 353 | |
| 354 | ParseResult codeCompleteDialectName(); |
| 355 | ParseResult codeCompleteOperationName(StringRef dialectName); |
| 356 | ParseResult codeCompleteDialectOrElidedOpName(SMLoc loc); |
| 357 | ParseResult codeCompleteStringDialectOrOperationName(StringRef name); |
| 358 | ParseResult codeCompleteExpectedTokens(ArrayRef<StringRef> tokens); |
| 359 | ParseResult codeCompleteOptionalTokens(ArrayRef<StringRef> tokens); |
| 360 | |
| 361 | Attribute codeCompleteAttribute(); |
| 362 | Type codeCompleteType(); |
| 363 | Attribute |
| 364 | codeCompleteDialectSymbol(const llvm::StringMap<Attribute> &aliases); |
| 365 | Type codeCompleteDialectSymbol(const llvm::StringMap<Type> &aliases); |
| 366 | |
| 367 | protected: |
| 368 | /// The Parser is subclassed and reinstantiated. Do not add additional |
| 369 | /// non-trivial state here, add it to the ParserState class. |
| 370 | ParserState &state; |
| 371 | }; |
| 372 | } // namespace detail |
| 373 | } // namespace mlir |
| 374 | |
| 375 | #endif // MLIR_LIB_ASMPARSER_PARSER_H |
| 376 | |