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 {
50   ParserState(const llvm::SourceMgr &sourceMgr, MLIRContext *ctx,
51               SymbolState &symbols, AsmParserState *asmState)
52       : context(ctx), lex(sourceMgr, ctx), curToken(lex.lexToken()),
53         symbols(symbols), parserDepth(symbols.nestedParserLocs.size()),
54         asmState(asmState) {
55     // Set the top level lexer for the symbol state if one doesn't exist.
56     if (!symbols.topLevelLexer)
57       symbols.topLevelLexer = &lex;
58   }
59   ~ParserState() {
60     // Reset the top level lexer if it refers the lexer in our state.
61     if (symbols.topLevelLexer == &lex)
62       symbols.topLevelLexer = nullptr;
63   }
64   ParserState(const ParserState &) = delete;
65   void operator=(const ParserState &) = delete;
66 
67   /// The context we're parsing into.
68   MLIRContext *const context;
69 
70   /// The lexer for the source file we're parsing.
71   Lexer lex;
72 
73   /// This is the next token that hasn't been consumed yet.
74   Token curToken;
75 
76   /// The current state for symbol parsing.
77   SymbolState &symbols;
78 
79   /// The depth of this parser in the nested parsing stack.
80   size_t parserDepth;
81 
82   /// An optional pointer to a struct containing high level parser state to be
83   /// populated during parsing.
84   AsmParserState *asmState;
85 };
86 
87 } // end namespace detail
88 } // end namespace mlir
89 
90 #endif // MLIR_LIB_PARSER_PARSERSTATE_H
91