22
22
#include < optional>
23
23
24
24
using namespace mlir ;
25
+ using llvm::Record;
26
+ using llvm::RecordKeeper;
27
+ using llvm::RecordVal;
28
+ using llvm::SourceMgr;
25
29
26
30
// / Returns the range of a lexical token given a SMLoc corresponding to the
27
31
// / start of an token location. The range is computed heuristically, and
@@ -32,7 +36,7 @@ static SMRange convertTokenLocToRange(SMLoc loc) {
32
36
33
37
// / Returns a language server uri for the given source location. `mainFileURI`
34
38
// / corresponds to the uri for the main file of the source manager.
35
- static lsp::URIForFile getURIFromLoc (const llvm:: SourceMgr &mgr, SMLoc loc,
39
+ static lsp::URIForFile getURIFromLoc (const SourceMgr &mgr, SMLoc loc,
36
40
const lsp::URIForFile &mainFileURI) {
37
41
int bufferId = mgr.FindBufferContainingLoc (loc);
38
42
if (bufferId == 0 || bufferId == static_cast <int >(mgr.getMainFileID ()))
@@ -47,12 +51,12 @@ static lsp::URIForFile getURIFromLoc(const llvm::SourceMgr &mgr, SMLoc loc,
47
51
}
48
52
49
53
// / Returns a language server location from the given source range.
50
- static lsp::Location getLocationFromLoc (llvm:: SourceMgr &mgr, SMRange loc,
54
+ static lsp::Location getLocationFromLoc (SourceMgr &mgr, SMRange loc,
51
55
const lsp::URIForFile &uri) {
52
56
return lsp::Location (getURIFromLoc (mgr, loc.Start , uri),
53
57
lsp::Range (mgr, loc));
54
58
}
55
- static lsp::Location getLocationFromLoc (llvm:: SourceMgr &mgr, SMLoc loc,
59
+ static lsp::Location getLocationFromLoc (SourceMgr &mgr, SMLoc loc,
56
60
const lsp::URIForFile &uri) {
57
61
return getLocationFromLoc (mgr, convertTokenLocToRange (loc), uri);
58
62
}
@@ -61,7 +65,7 @@ static lsp::Location getLocationFromLoc(llvm::SourceMgr &mgr, SMLoc loc,
61
65
static std::optional<lsp::Diagnostic>
62
66
getLspDiagnoticFromDiag (const llvm::SMDiagnostic &diag,
63
67
const lsp::URIForFile &uri) {
64
- auto *sourceMgr = const_cast <llvm:: SourceMgr *>(diag.getSourceMgr ());
68
+ auto *sourceMgr = const_cast <SourceMgr *>(diag.getSourceMgr ());
65
69
if (!sourceMgr || !diag.getLoc ().isValid ())
66
70
return std::nullopt;
67
71
@@ -79,17 +83,17 @@ getLspDiagnoticFromDiag(const llvm::SMDiagnostic &diag,
79
83
80
84
// Convert the severity for the diagnostic.
81
85
switch (diag.getKind ()) {
82
- case llvm:: SourceMgr::DK_Warning:
86
+ case SourceMgr::DK_Warning:
83
87
lspDiag.severity = lsp::DiagnosticSeverity::Warning;
84
88
break ;
85
- case llvm:: SourceMgr::DK_Error:
89
+ case SourceMgr::DK_Error:
86
90
lspDiag.severity = lsp::DiagnosticSeverity::Error;
87
91
break ;
88
- case llvm:: SourceMgr::DK_Note:
92
+ case SourceMgr::DK_Note:
89
93
// Notes are emitted separately from the main diagnostic, so we just treat
90
94
// them as remarks given that we can't determine the diagnostic to relate
91
95
// them to.
92
- case llvm:: SourceMgr::DK_Remark:
96
+ case SourceMgr::DK_Remark:
93
97
lspDiag.severity = lsp::DiagnosticSeverity::Information;
94
98
break ;
95
99
}
@@ -100,16 +104,15 @@ getLspDiagnoticFromDiag(const llvm::SMDiagnostic &diag,
100
104
101
105
// / Get the base definition of the given record value, or nullptr if one
102
106
// / couldn't be found.
103
- static std::pair<const llvm:: Record *, const llvm:: RecordVal *>
104
- getBaseValue (const llvm:: Record *record, const llvm:: RecordVal *value) {
107
+ static std::pair<const Record *, const RecordVal *>
108
+ getBaseValue (const Record *record, const RecordVal *value) {
105
109
if (value->isTemplateArg ())
106
110
return {nullptr , nullptr };
107
111
108
112
// Find a base value for the field in the super classes of the given record.
109
113
// On success, `record` is updated to the new parent record.
110
114
StringRef valueName = value->getName ();
111
- auto findValueInSupers =
112
- [&](const llvm::Record *&record) -> llvm::RecordVal * {
115
+ auto findValueInSupers = [&](const Record *&record) -> RecordVal * {
113
116
for (auto [parentRecord, loc] : record->getSuperClasses ()) {
114
117
if (auto *newBase = parentRecord->getValue (valueName)) {
115
118
record = parentRecord;
@@ -120,8 +123,8 @@ getBaseValue(const llvm::Record *record, const llvm::RecordVal *value) {
120
123
};
121
124
122
125
// Try to find the lowest definition of the record value.
123
- std::pair<const llvm:: Record *, const llvm:: RecordVal *> baseValue = {};
124
- while (const llvm:: RecordVal *newBase = findValueInSupers (record))
126
+ std::pair<const Record *, const RecordVal *> baseValue = {};
127
+ while (const RecordVal *newBase = findValueInSupers (record))
125
128
baseValue = {record, newBase};
126
129
127
130
// Check that the base isn't the same as the current value (e.g. if the value
@@ -140,15 +143,15 @@ namespace {
140
143
// / contains the definition of the symbol, the location of the symbol, and any
141
144
// / recorded references.
142
145
struct TableGenIndexSymbol {
143
- TableGenIndexSymbol (const llvm:: Record *record)
146
+ TableGenIndexSymbol (const Record *record)
144
147
: definition(record),
145
148
defLoc (convertTokenLocToRange(record->getLoc ().front())) {}
146
- TableGenIndexSymbol (const llvm:: RecordVal *value)
149
+ TableGenIndexSymbol (const RecordVal *value)
147
150
: definition(value), defLoc(convertTokenLocToRange(value->getLoc ())) {}
148
151
virtual ~TableGenIndexSymbol () = default ;
149
152
150
153
// The main definition of the symbol.
151
- PointerUnion<const llvm:: Record *, const llvm:: RecordVal *> definition;
154
+ PointerUnion<const Record *, const RecordVal *> definition;
152
155
153
156
// / The source location of the definition.
154
157
SMRange defLoc;
@@ -158,37 +161,33 @@ struct TableGenIndexSymbol {
158
161
};
159
162
// / This class represents a single record symbol.
160
163
struct TableGenRecordSymbol : public TableGenIndexSymbol {
161
- TableGenRecordSymbol (const llvm::Record *record)
162
- : TableGenIndexSymbol(record) {}
164
+ TableGenRecordSymbol (const Record *record) : TableGenIndexSymbol(record) {}
163
165
~TableGenRecordSymbol () override = default ;
164
166
165
167
static bool classof (const TableGenIndexSymbol *symbol) {
166
- return symbol->definition .is <const llvm:: Record *>();
168
+ return symbol->definition .is <const Record *>();
167
169
}
168
170
169
171
// / Return the value of this symbol.
170
- const llvm::Record *getValue () const {
171
- return definition.get <const llvm::Record *>();
172
- }
172
+ const Record *getValue () const { return definition.get <const Record *>(); }
173
173
};
174
174
// / This class represents a single record value symbol.
175
175
struct TableGenRecordValSymbol : public TableGenIndexSymbol {
176
- TableGenRecordValSymbol (const llvm::Record *record,
177
- const llvm::RecordVal *value)
176
+ TableGenRecordValSymbol (const Record *record, const RecordVal *value)
178
177
: TableGenIndexSymbol(value), record(record) {}
179
178
~TableGenRecordValSymbol () override = default ;
180
179
181
180
static bool classof (const TableGenIndexSymbol *symbol) {
182
- return symbol->definition .is <const llvm:: RecordVal *>();
181
+ return symbol->definition .is <const RecordVal *>();
183
182
}
184
183
185
184
// / Return the value of this symbol.
186
- const llvm:: RecordVal *getValue () const {
187
- return definition.get <const llvm:: RecordVal *>();
185
+ const RecordVal *getValue () const {
186
+ return definition.get <const RecordVal *>();
188
187
}
189
188
190
189
// / The parent record of this symbol.
191
- const llvm:: Record *record;
190
+ const Record *record;
192
191
};
193
192
194
193
// / This class provides an index for definitions/uses within a TableGen
@@ -199,7 +198,7 @@ class TableGenIndex {
199
198
TableGenIndex () : intervalMap(allocator) {}
200
199
201
200
// / Initialize the index with the given RecordKeeper.
202
- void initialize (const llvm:: RecordKeeper &records);
201
+ void initialize (const RecordKeeper &records);
203
202
204
203
// / Lookup a symbol for the given location. Returns nullptr if no symbol could
205
204
// / be found. If provided, `overlappedRange` is set to the range that the
@@ -217,15 +216,15 @@ class TableGenIndex {
217
216
llvm::IntervalMapHalfOpenInfo<const char *>>;
218
217
219
218
// / Get or insert a symbol for the given record.
220
- TableGenIndexSymbol *getOrInsertDef (const llvm:: Record *record) {
219
+ TableGenIndexSymbol *getOrInsertDef (const Record *record) {
221
220
auto it = defToSymbol.try_emplace (record, nullptr );
222
221
if (it.second )
223
222
it.first ->second = std::make_unique<TableGenRecordSymbol>(record);
224
223
return &*it.first ->second ;
225
224
}
226
225
// / Get or insert a symbol for the given record value.
227
- TableGenIndexSymbol *getOrInsertDef (const llvm:: Record *record,
228
- const llvm:: RecordVal *value) {
226
+ TableGenIndexSymbol *getOrInsertDef (const Record *record,
227
+ const RecordVal *value) {
229
228
auto it = defToSymbol.try_emplace (value, nullptr );
230
229
if (it.second ) {
231
230
it.first ->second =
@@ -246,7 +245,7 @@ class TableGenIndex {
246
245
};
247
246
} // namespace
248
247
249
- void TableGenIndex::initialize (const llvm:: RecordKeeper &records) {
248
+ void TableGenIndex::initialize (const RecordKeeper &records) {
250
249
intervalMap.clear ();
251
250
defToSymbol.clear ();
252
251
@@ -282,7 +281,7 @@ void TableGenIndex::initialize(const llvm::RecordKeeper &records) {
282
281
llvm::make_pointee_range (llvm::make_second_range (records.getClasses ()));
283
282
auto defs =
284
283
llvm::make_pointee_range (llvm::make_second_range (records.getDefs ()));
285
- for (const llvm:: Record &def : llvm::concat<llvm:: Record>(classes, defs)) {
284
+ for (const Record &def : llvm::concat<Record>(classes, defs)) {
286
285
auto *sym = getOrInsertDef (&def);
287
286
insertRef (sym, sym->defLoc , /* isDef=*/ true );
288
287
@@ -293,7 +292,7 @@ void TableGenIndex::initialize(const llvm::RecordKeeper &records) {
293
292
insertRef (sym, loc);
294
293
295
294
// Add definitions for any values.
296
- for (const llvm:: RecordVal &value : def.getValues ()) {
295
+ for (const RecordVal &value : def.getValues ()) {
297
296
auto *sym = getOrInsertDef (&def, &value);
298
297
insertRef (sym, sym->defLoc , /* isDef=*/ true );
299
298
for (SMRange refLoc : value.getReferenceLocs ())
@@ -359,13 +358,12 @@ class TableGenTextFile {
359
358
360
359
std::optional<lsp::Hover> findHover (const lsp::URIForFile &uri,
361
360
const lsp::Position &hoverPos);
362
- lsp::Hover buildHoverForRecord (const llvm:: Record *record,
361
+ lsp::Hover buildHoverForRecord (const Record *record,
363
362
const SMRange &hoverRange);
364
- lsp::Hover buildHoverForTemplateArg (const llvm:: Record *record,
365
- const llvm:: RecordVal *value,
363
+ lsp::Hover buildHoverForTemplateArg (const Record *record,
364
+ const RecordVal *value,
366
365
const SMRange &hoverRange);
367
- lsp::Hover buildHoverForField (const llvm::Record *record,
368
- const llvm::RecordVal *value,
366
+ lsp::Hover buildHoverForField (const Record *record, const RecordVal *value,
369
367
const SMRange &hoverRange);
370
368
371
369
private:
@@ -383,10 +381,10 @@ class TableGenTextFile {
383
381
std::vector<std::string> includeDirs;
384
382
385
383
// / The source manager containing the contents of the input file.
386
- llvm:: SourceMgr sourceMgr;
384
+ SourceMgr sourceMgr;
387
385
388
386
// / The record keeper containing the parsed tablegen constructs.
389
- std::unique_ptr<llvm:: RecordKeeper> recordKeeper;
387
+ std::unique_ptr<RecordKeeper> recordKeeper;
390
388
391
389
// / The index of the parsed file.
392
390
TableGenIndex index;
@@ -430,8 +428,8 @@ void TableGenTextFile::initialize(const lsp::URIForFile &uri,
430
428
int64_t newVersion,
431
429
std::vector<lsp::Diagnostic> &diagnostics) {
432
430
version = newVersion;
433
- sourceMgr = llvm:: SourceMgr ();
434
- recordKeeper = std::make_unique<llvm:: RecordKeeper>();
431
+ sourceMgr = SourceMgr ();
432
+ recordKeeper = std::make_unique<RecordKeeper>();
435
433
436
434
// Build a buffer for this file.
437
435
auto memBuffer = llvm::MemoryBuffer::getMemBuffer (contents, uri.file ());
@@ -442,7 +440,7 @@ void TableGenTextFile::initialize(const lsp::URIForFile &uri,
442
440
sourceMgr.setIncludeDirs (includeDirs);
443
441
sourceMgr.AddNewSourceBuffer (std::move (memBuffer), SMLoc ());
444
442
445
- // This class provides a context argument for the llvm:: SourceMgr diagnostic
443
+ // This class provides a context argument for the SourceMgr diagnostic
446
444
// handler.
447
445
struct DiagHandlerContext {
448
446
std::vector<lsp::Diagnostic> &diagnostics;
@@ -543,13 +541,13 @@ TableGenTextFile::findHover(const lsp::URIForFile &uri,
543
541
// Build hover for a RecordVal, which is either a template argument or a
544
542
// field.
545
543
auto *recordVal = cast<TableGenRecordValSymbol>(symbol);
546
- const llvm:: RecordVal *value = recordVal->getValue ();
544
+ const RecordVal *value = recordVal->getValue ();
547
545
if (value->isTemplateArg ())
548
546
return buildHoverForTemplateArg (recordVal->record , value, hoverRange);
549
547
return buildHoverForField (recordVal->record , value, hoverRange);
550
548
}
551
549
552
- lsp::Hover TableGenTextFile::buildHoverForRecord (const llvm:: Record *record,
550
+ lsp::Hover TableGenTextFile::buildHoverForRecord (const Record *record,
553
551
const SMRange &hoverRange) {
554
552
lsp::Hover hover (lsp::Range (sourceMgr, hoverRange));
555
553
{
@@ -570,7 +568,7 @@ lsp::Hover TableGenTextFile::buildHoverForRecord(const llvm::Record *record,
570
568
auto printAndFormatField = [&](StringRef fieldName) {
571
569
// Check that the record actually has the given field, and that it's a
572
570
// string.
573
- const llvm:: RecordVal *value = record->getValue (fieldName);
571
+ const RecordVal *value = record->getValue (fieldName);
574
572
if (!value || !value->getValue ())
575
573
return ;
576
574
auto *stringValue = dyn_cast<llvm::StringInit>(value->getValue ());
@@ -593,10 +591,8 @@ lsp::Hover TableGenTextFile::buildHoverForRecord(const llvm::Record *record,
593
591
return hover;
594
592
}
595
593
596
- lsp::Hover
597
- TableGenTextFile::buildHoverForTemplateArg (const llvm::Record *record,
598
- const llvm::RecordVal *value,
599
- const SMRange &hoverRange) {
594
+ lsp::Hover TableGenTextFile::buildHoverForTemplateArg (
595
+ const Record *record, const RecordVal *value, const SMRange &hoverRange) {
600
596
lsp::Hover hover (lsp::Range (sourceMgr, hoverRange));
601
597
{
602
598
llvm::raw_string_ostream hoverOS (hover.contents .value );
@@ -609,8 +605,8 @@ TableGenTextFile::buildHoverForTemplateArg(const llvm::Record *record,
609
605
return hover;
610
606
}
611
607
612
- lsp::Hover TableGenTextFile::buildHoverForField (const llvm:: Record *record,
613
- const llvm:: RecordVal *value,
608
+ lsp::Hover TableGenTextFile::buildHoverForField (const Record *record,
609
+ const RecordVal *value,
614
610
const SMRange &hoverRange) {
615
611
lsp::Hover hover (lsp::Range (sourceMgr, hoverRange));
616
612
{
0 commit comments