/
AsmParserState.h
239 lines (187 loc) · 8.6 KB
/
AsmParserState.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//===- AsmParserState.h -----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_ASMPARSER_ASMPARSERSTATE_H
#define MLIR_ASMPARSER_ASMPARSERSTATE_H
#include "mlir/IR/Attributes.h"
#include "mlir/IR/Types.h"
#include "llvm/Support/SMLoc.h"
#include <cstddef>
namespace mlir {
class Block;
class BlockArgument;
class FileLineColLoc;
class Operation;
class OperationName;
class SymbolRefAttr;
class Value;
/// This class represents state from a parsed MLIR textual format string. It is
/// useful for building additional analysis and language utilities on top of
/// textual MLIR. This should generally not be used for traditional compilation.
class AsmParserState {
public:
/// This class represents a definition within the source manager, containing
/// it's defining location and locations of any uses. SMDefinitions are only
/// provided for entities that have uses within an input file, e.g. SSA
/// values, Blocks, and Symbols.
struct SMDefinition {
SMDefinition() = default;
SMDefinition(SMRange loc) : loc(loc) {}
/// The source location of the definition.
SMRange loc;
/// The source location of all uses of the definition.
SmallVector<SMRange> uses;
};
/// This class represents the information for an operation definition within
/// an input file.
struct OperationDefinition {
struct ResultGroupDefinition {
ResultGroupDefinition(unsigned index, SMRange loc)
: startIndex(index), definition(loc) {}
/// The result number that starts this group.
unsigned startIndex;
/// The source definition of the result group.
SMDefinition definition;
};
OperationDefinition(Operation *op, SMRange loc, SMLoc endLoc)
: op(op), loc(loc), scopeLoc(loc.Start, endLoc) {}
/// The operation representing this definition.
Operation *op;
/// The source location for the operation, i.e. the location of its name.
SMRange loc;
/// The full source range of the operation definition, i.e. a range
/// encompassing the start and end of the full operation definition.
SMRange scopeLoc;
/// Source definitions for any result groups of this operation.
SmallVector<ResultGroupDefinition> resultGroups;
/// If this operation is a symbol operation, this vector contains symbol
/// uses of this operation.
SmallVector<SMRange> symbolUses;
};
/// This class represents the information for a block definition within the
/// input file.
struct BlockDefinition {
BlockDefinition(Block *block, SMRange loc = {})
: block(block), definition(loc) {}
/// The block representing this definition.
Block *block;
/// The source location for the block, i.e. the location of its name, and
/// any uses it has.
SMDefinition definition;
/// Source definitions for any arguments of this block.
SmallVector<SMDefinition> arguments;
};
/// This class represents the information for an attribute alias definition
/// within the input file.
struct AttributeAliasDefinition {
AttributeAliasDefinition(StringRef name, SMRange loc = {},
Attribute value = {})
: name(name), definition(loc), value(value) {}
/// The name of the attribute alias.
StringRef name;
/// The source location for the alias.
SMDefinition definition;
/// The value of the alias.
Attribute value;
};
/// This class represents the information for type definition within the input
/// file.
struct TypeAliasDefinition {
TypeAliasDefinition(StringRef name, SMRange loc, Type value)
: name(name), definition(loc), value(value) {}
/// The name of the attribute alias.
StringRef name;
/// The source location for the alias.
SMDefinition definition;
/// The value of the alias.
Type value;
};
AsmParserState();
~AsmParserState();
AsmParserState &operator=(AsmParserState &&other);
//===--------------------------------------------------------------------===//
// Access State
//===--------------------------------------------------------------------===//
using BlockDefIterator = llvm::pointee_iterator<
ArrayRef<std::unique_ptr<BlockDefinition>>::iterator>;
using OperationDefIterator = llvm::pointee_iterator<
ArrayRef<std::unique_ptr<OperationDefinition>>::iterator>;
using AttributeDefIterator = llvm::pointee_iterator<
ArrayRef<std::unique_ptr<AttributeAliasDefinition>>::iterator>;
using TypeDefIterator = llvm::pointee_iterator<
ArrayRef<std::unique_ptr<TypeAliasDefinition>>::iterator>;
/// Return a range of the BlockDefinitions held by the current parser state.
iterator_range<BlockDefIterator> getBlockDefs() const;
/// Return the definition for the given block, or nullptr if the given
/// block does not have a definition.
const BlockDefinition *getBlockDef(Block *block) const;
/// Return a range of the OperationDefinitions held by the current parser
/// state.
iterator_range<OperationDefIterator> getOpDefs() const;
/// Return the definition for the given operation, or nullptr if the given
/// operation does not have a definition.
const OperationDefinition *getOpDef(Operation *op) const;
/// Return a range of the AttributeAliasDefinitions held by the current parser
/// state.
iterator_range<AttributeDefIterator> getAttributeAliasDefs() const;
/// Return the definition for the given attribute alias, or nullptr if the
/// given alias does not have a definition.
const AttributeAliasDefinition *getAttributeAliasDef(StringRef name) const;
/// Return a range of the TypeAliasDefinitions held by the current parser
/// state.
iterator_range<TypeDefIterator> getTypeAliasDefs() const;
/// Return the definition for the given type alias, or nullptr if the given
/// alias does not have a definition.
const TypeAliasDefinition *getTypeAliasDef(StringRef name) const;
/// Returns (heuristically) the range of an identifier given a SMLoc
/// corresponding to the start of an identifier location.
static SMRange convertIdLocToRange(SMLoc loc);
//===--------------------------------------------------------------------===//
// Populate State
//===--------------------------------------------------------------------===//
/// Initialize the state in preparation for populating more parser state under
/// the given top-level operation.
void initialize(Operation *topLevelOp);
/// Finalize any in-progress parser state under the given top-level operation.
void finalize(Operation *topLevelOp);
/// Start a definition for an operation with the given name.
void startOperationDefinition(const OperationName &opName);
/// Finalize the most recently started operation definition.
void finalizeOperationDefinition(
Operation *op, SMRange nameLoc, SMLoc endLoc,
ArrayRef<std::pair<unsigned, SMLoc>> resultGroups = std::nullopt);
/// Start a definition for a region nested under the current operation.
void startRegionDefinition();
/// Finalize the most recently started region definition.
void finalizeRegionDefinition();
/// Add a definition of the given entity.
void addDefinition(Block *block, SMLoc location);
void addDefinition(BlockArgument blockArg, SMLoc location);
void addAttrAliasDefinition(StringRef name, SMRange location,
Attribute value);
void addTypeAliasDefinition(StringRef name, SMRange location, Type value);
/// Add a source uses of the given value.
void addUses(Value value, ArrayRef<SMLoc> locations);
void addUses(Block *block, ArrayRef<SMLoc> locations);
void addAttrAliasUses(StringRef name, SMRange locations);
void addTypeAliasUses(StringRef name, SMRange locations);
/// Add source uses for all the references nested under `refAttr`. The
/// provided `locations` should match 1-1 with the number of references in
/// `refAttr`, i.e.:
/// nestedReferences.size() + /*leafReference=*/1 == refLocations.size()
void addUses(SymbolRefAttr refAttr, ArrayRef<SMRange> refLocations);
/// Refine the `oldValue` to the `newValue`. This is used to indicate that
/// `oldValue` was a placeholder, and the uses of it should really refer to
/// `newValue`.
void refineDefinition(Value oldValue, Value newValue);
private:
struct Impl;
/// A pointer to the internal implementation of this class.
std::unique_ptr<Impl> impl;
};
} // namespace mlir
#endif // MLIR_ASMPARSER_ASMPARSERSTATE_H