1 //===- ParserState.h - MLIR ParserState -------------------------*- 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_PARSER_PARSERSTATE_H 10 #define MLIR_LIB_PARSER_PARSERSTATE_H 11 12 #include "Lexer.h" 13 #include "mlir/IR/Attributes.h" 14 #include "llvm/ADT/StringMap.h" 15 16 namespace mlir { 17 namespace detail { 18 19 //===----------------------------------------------------------------------===// 20 // SymbolState 21 //===----------------------------------------------------------------------===// 22 23 /// This class contains record of any parsed top-level symbols. 24 struct SymbolState { 25 // A map from attribute alias identifier to Attribute. 26 llvm::StringMap<Attribute> attributeAliasDefinitions; 27 28 // A map from type alias identifier to Type. 29 llvm::StringMap<Type> typeAliasDefinitions; 30 31 /// A set of locations into the main parser memory buffer for each of the 32 /// active nested parsers. Given that some nested parsers, i.e. custom dialect 33 /// parsers, operate on a temporary memory buffer, this provides an anchor 34 /// point for emitting diagnostics. 35 SmallVector<llvm::SMLoc, 1> nestedParserLocs; 36 37 /// The top-level lexer that contains the original memory buffer provided by 38 /// the user. This is used by nested parsers to get a properly encoded source 39 /// location. 40 Lexer *topLevelLexer = nullptr; 41 }; 42 43 //===----------------------------------------------------------------------===// 44 // ParserState 45 //===----------------------------------------------------------------------===// 46 47 /// This class refers to all of the state maintained globally by the parser, 48 /// such as the current lexer position etc. 49 struct ParserState { ParserStateParserState50 ParserState(const llvm::SourceMgr &sourceMgr, MLIRContext *ctx, 51 SymbolState &symbols) 52 : context(ctx), lex(sourceMgr, ctx), curToken(lex.lexToken()), 53 symbols(symbols), parserDepth(symbols.nestedParserLocs.size()) { 54 // Set the top level lexer for the symbol state if one doesn't exist. 55 if (!symbols.topLevelLexer) 56 symbols.topLevelLexer = &lex; 57 } ~ParserStateParserState58 ~ParserState() { 59 // Reset the top level lexer if it refers the lexer in our state. 60 if (symbols.topLevelLexer == &lex) 61 symbols.topLevelLexer = nullptr; 62 } 63 ParserState(const ParserState &) = delete; 64 void operator=(const ParserState &) = delete; 65 66 /// The context we're parsing into. 67 MLIRContext *const context; 68 69 /// The lexer for the source file we're parsing. 70 Lexer lex; 71 72 /// This is the next token that hasn't been consumed yet. 73 Token curToken; 74 75 /// The current state for symbol parsing. 76 SymbolState &symbols; 77 78 /// The depth of this parser in the nested parsing stack. 79 size_t parserDepth; 80 }; 81 82 } // end namespace detail 83 } // end namespace mlir 84 85 #endif // MLIR_LIB_PARSER_PARSERSTATE_H 86