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

source code of mlir/lib/AsmParser/ParserState.h