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_ASMPARSER_PARSERSTATE_H
10 #define MLIR_LIB_ASMPARSER_PARSERSTATE_H
11 
12 #include "Lexer.h"
13 #include "mlir/IR/Attributes.h"
14 #include "mlir/IR/OpImplementation.h"
15 #include "llvm/ADT/StringMap.h"
16 
17 namespace mlir {
18 class OpAsmDialectInterface;
19 
20 namespace detail {
21 
22 //===----------------------------------------------------------------------===//
23 // SymbolState
24 //===----------------------------------------------------------------------===//
25 
26 /// This class contains record of any parsed top-level symbols.
27 struct SymbolState {
28   /// A map from attribute alias identifier to Attribute.
29   llvm::StringMap<Attribute> attributeAliasDefinitions;
30 
31   /// A map from type alias identifier to Type.
32   llvm::StringMap<Type> typeAliasDefinitions;
33 
34   /// A map of dialect resource keys to the resolved resource name and handle
35   /// to use during parsing.
36   DenseMap<const OpAsmDialectInterface *,
37            llvm::StringMap<std::pair<std::string, AsmDialectResourceHandle>>>
38       dialectResources;
39 };
40 
41 //===----------------------------------------------------------------------===//
42 // ParserState
43 //===----------------------------------------------------------------------===//
44 
45 /// This class refers to all of the state maintained globally by the parser,
46 /// such as the current lexer position etc.
47 struct ParserState {
ParserStateParserState48   ParserState(const llvm::SourceMgr &sourceMgr, const ParserConfig &config,
49               SymbolState &symbols, AsmParserState *asmState,
50               AsmParserCodeCompleteContext *codeCompleteContext)
51       : config(config),
52         lex(sourceMgr, config.getContext(), codeCompleteContext),
53         curToken(lex.lexToken()), symbols(symbols), asmState(asmState),
54         codeCompleteContext(codeCompleteContext) {}
55   ParserState(const ParserState &) = delete;
56   void operator=(const ParserState &) = delete;
57 
58   /// The configuration used to setup the parser.
59   const ParserConfig &config;
60 
61   /// The lexer for the source file we're parsing.
62   Lexer lex;
63 
64   /// This is the next token that hasn't been consumed yet.
65   Token curToken;
66 
67   /// The current state for symbol parsing.
68   SymbolState &symbols;
69 
70   /// An optional pointer to a struct containing high level parser state to be
71   /// populated during parsing.
72   AsmParserState *asmState;
73 
74   /// An optional code completion context.
75   AsmParserCodeCompleteContext *codeCompleteContext;
76 
77   // Contains the stack of default dialect to use when parsing regions.
78   // A new dialect get pushed to the stack before parsing regions nested
79   // under an operation implementing `OpAsmOpInterface`, and
80   // popped when done. At the top-level we start with "builtin" as the
81   // default, so that the top-level `module` operation parses as-is.
82   SmallVector<StringRef> defaultDialectStack{"builtin"};
83 };
84 
85 } // namespace detail
86 } // namespace mlir
87 
88 #endif // MLIR_LIB_ASMPARSER_PARSERSTATE_H
89