Skip to content

Commit f5deb2e

Browse files
committed
Replace most enums with enum class
1 parent 146fa52 commit f5deb2e

File tree

6 files changed

+119
-108
lines changed

6 files changed

+119
-108
lines changed

mlir/include/mlir/Query/Matcher/Parser.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ namespace mlir::query::matcher::internal {
3838
// Matcher expression parser.
3939
class Parser {
4040
public:
41+
// Different possible tokens.
42+
enum class TokenKind {
43+
Eof,
44+
NewLine,
45+
OpenParen,
46+
CloseParen,
47+
Comma,
48+
Period,
49+
Literal,
50+
Ident,
51+
InvalidChar,
52+
CodeCompletion,
53+
Error
54+
};
55+
4156
// Interface to connect the parser with the registry and more. The parser uses
4257
// the Sema instance passed into parseMatcherExpression() to handle all
4358
// matcher tokens.

mlir/include/mlir/Query/Matcher/VariantValue.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class VariantMatcher {
8585
// - VariantMatcher
8686
class VariantValue {
8787
public:
88-
VariantValue() : type(VT_Nothing) {}
88+
VariantValue() : type(ValueType::Nothing) {}
8989

9090
VariantValue(const VariantValue &other);
9191
~VariantValue();
@@ -112,10 +112,10 @@ class VariantValue {
112112
void reset();
113113

114114
// All supported value types.
115-
enum ValueType {
116-
VT_Nothing,
117-
VT_String,
118-
VT_Matcher,
115+
enum class ValueType {
116+
Nothing,
117+
String,
118+
Matcher,
119119
};
120120

121121
// All supported value types.

mlir/include/mlir/Query/Query.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
namespace mlir::query {
1818

19-
enum QueryKind { QK_Invalid, QK_NoOp, QK_Help, QK_Match };
19+
enum class QueryKind { Invalid, NoOp, Help, Match };
2020

2121
class QuerySession;
2222

@@ -37,41 +37,49 @@ typedef llvm::IntrusiveRefCntPtr<Query> QueryRef;
3737
// Any query which resulted in a parse error. The error message is in ErrStr.
3838
struct InvalidQuery : Query {
3939
InvalidQuery(const llvm::Twine &errStr)
40-
: Query(QK_Invalid), errStr(errStr.str()) {}
40+
: Query(QueryKind::Invalid), errStr(errStr.str()) {}
4141
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
4242

4343
std::string errStr;
4444

45-
static bool classof(const Query *query) { return query->kind == QK_Invalid; }
45+
static bool classof(const Query *query) {
46+
return query->kind == QueryKind::Invalid;
47+
}
4648
};
4749

4850
// No-op query (i.e. a blank line).
4951
struct NoOpQuery : Query {
50-
NoOpQuery() : Query(QK_NoOp) {}
52+
NoOpQuery() : Query(QueryKind::NoOp) {}
5153
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
5254

53-
static bool classof(const Query *query) { return query->kind == QK_NoOp; }
55+
static bool classof(const Query *query) {
56+
return query->kind == QueryKind::NoOp;
57+
}
5458
};
5559

5660
// Query for "help".
5761
struct HelpQuery : Query {
58-
HelpQuery() : Query(QK_Help) {}
62+
HelpQuery() : Query(QueryKind::Help) {}
5963
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
6064

61-
static bool classof(const Query *query) { return query->kind == QK_Help; }
65+
static bool classof(const Query *query) {
66+
return query->kind == QueryKind::Help;
67+
}
6268
};
6369

6470
// Query for "match MATCHER".
6571
struct MatchQuery : Query {
6672
MatchQuery(StringRef source, const matcher::DynMatcher &matcher)
67-
: Query(QK_Match), matcher(matcher), source(source) {}
73+
: Query(QueryKind::Match), matcher(matcher), source(source) {}
6874
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
6975

7076
const matcher::DynMatcher matcher;
7177

7278
StringRef source;
7379

74-
static bool classof(const Query *query) { return query->kind == QK_Match; }
80+
static bool classof(const Query *query) {
81+
return query->kind == QueryKind::Match;
82+
}
7583
};
7684

7785
} // namespace mlir::query

mlir/lib/Query/Matcher/Parser.cpp

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,6 @@ namespace mlir::query::matcher::internal {
1919

2020
// Simple structure to hold information for one token from the parser.
2121
struct Parser::TokenInfo {
22-
// Different possible tokens.
23-
enum TokenKind {
24-
TK_Eof,
25-
TK_NewLine,
26-
TK_OpenParen,
27-
TK_CloseParen,
28-
TK_Comma,
29-
TK_Period,
30-
TK_Literal,
31-
TK_Ident,
32-
TK_InvalidChar,
33-
TK_CodeCompletion,
34-
TK_Error
35-
};
36-
3722
TokenInfo() = default;
3823

3924
// Method to set the kind and text of the token
@@ -43,7 +28,7 @@ struct Parser::TokenInfo {
4328
}
4429

4530
llvm::StringRef text;
46-
TokenKind kind = TK_Eof;
31+
TokenKind kind = TokenKind::Eof;
4732
SourceRange range;
4833
VariantValue value;
4934
};
@@ -76,19 +61,19 @@ class Parser::CodeTokenizer {
7661

7762
// Skip any newline tokens
7863
TokenInfo skipNewlines() {
79-
while (nextToken.kind == TokenInfo::TK_NewLine)
64+
while (nextToken.kind == TokenKind::NewLine)
8065
nextToken = getNextToken();
8166
return nextToken;
8267
}
8368

8469
// Consume and return next token, ignoring newlines
8570
TokenInfo consumeNextTokenIgnoreNewlines() {
8671
skipNewlines();
87-
return nextToken.kind == TokenInfo::TK_Eof ? nextToken : consumeNextToken();
72+
return nextToken.kind == TokenKind::Eof ? nextToken : consumeNextToken();
8873
}
8974

9075
// Return kind of next token
91-
TokenInfo::TokenKind nextTokenKind() const { return nextToken.kind; }
76+
TokenKind nextTokenKind() const { return nextToken.kind; }
9277

9378
private:
9479
// Helper function to get the first character as a new StringRef and drop it
@@ -108,15 +93,15 @@ class Parser::CodeTokenizer {
10893

10994
// Code completion case
11095
if (codeCompletionLocation && codeCompletionLocation <= code.data()) {
111-
result.set(TokenInfo::TK_CodeCompletion,
96+
result.set(TokenKind::CodeCompletion,
11297
llvm::StringRef(codeCompletionLocation, 0));
11398
codeCompletionLocation = nullptr;
11499
return result;
115100
}
116101

117102
// End of file case
118103
if (code.empty()) {
119-
result.set(TokenInfo::TK_Eof, "");
104+
result.set(TokenKind::Eof, "");
120105
return result;
121106
}
122107

@@ -126,21 +111,21 @@ class Parser::CodeTokenizer {
126111
code = code.drop_until([](char c) { return c == '\n'; });
127112
return getNextToken();
128113
case ',':
129-
result.set(TokenInfo::TK_Comma, firstCharacterAndDrop(code));
114+
result.set(TokenKind::Comma, firstCharacterAndDrop(code));
130115
break;
131116
case '.':
132-
result.set(TokenInfo::TK_Period, firstCharacterAndDrop(code));
117+
result.set(TokenKind::Period, firstCharacterAndDrop(code));
133118
break;
134119
case '\n':
135120
++line;
136121
startOfLine = code.drop_front();
137-
result.set(TokenInfo::TK_NewLine, firstCharacterAndDrop(code));
122+
result.set(TokenKind::NewLine, firstCharacterAndDrop(code));
138123
break;
139124
case '(':
140-
result.set(TokenInfo::TK_OpenParen, firstCharacterAndDrop(code));
125+
result.set(TokenKind::OpenParen, firstCharacterAndDrop(code));
141126
break;
142127
case ')':
143-
result.set(TokenInfo::TK_CloseParen, firstCharacterAndDrop(code));
128+
result.set(TokenKind::CloseParen, firstCharacterAndDrop(code));
144129
break;
145130
case '"':
146131
case '\'':
@@ -170,7 +155,7 @@ class Parser::CodeTokenizer {
170155
continue;
171156
}
172157
if (code[length] == marker) {
173-
result->kind = TokenInfo::TK_Literal;
158+
result->kind = TokenKind::Literal;
174159
result->text = code.substr(0, length + 1);
175160
result->value = code.substr(1, length - 1);
176161
code = code.drop_front(length + 1);
@@ -184,7 +169,7 @@ class Parser::CodeTokenizer {
184169
range.end = currentLocation();
185170
error->addError(range, Diagnostics::ErrorType::ParserStringError)
186171
<< errorText;
187-
result->kind = TokenInfo::TK_Error;
172+
result->kind = TokenKind::Error;
188173
}
189174

190175
void parseIdentifierOrInvalid(TokenInfo *result) {
@@ -198,7 +183,7 @@ class Parser::CodeTokenizer {
198183
// location to become a code completion token.
199184
if (codeCompletionLocation == code.data() + tokenLength) {
200185
codeCompletionLocation = nullptr;
201-
result->kind = TokenInfo::TK_CodeCompletion;
186+
result->kind = TokenKind::CodeCompletion;
202187
result->text = code.substr(0, tokenLength);
203188
code = code.drop_front(tokenLength);
204189
return;
@@ -207,11 +192,11 @@ class Parser::CodeTokenizer {
207192
break;
208193
++tokenLength;
209194
}
210-
result->kind = TokenInfo::TK_Ident;
195+
result->kind = TokenKind::Ident;
211196
result->text = code.substr(0, tokenLength);
212197
code = code.drop_front(tokenLength);
213198
} else {
214-
result->kind = TokenInfo::TK_InvalidChar;
199+
result->kind = TokenKind::InvalidChar;
215200
result->text = code.substr(0, 1);
216201
code = code.drop_front(1);
217202
}
@@ -270,7 +255,7 @@ struct Parser::ScopedContextEntry {
270255
bool Parser::parseIdentifierPrefixImpl(VariantValue *value) {
271256
const TokenInfo nameToken = tokenizer->consumeNextToken();
272257

273-
if (tokenizer->nextTokenKind() != TokenInfo::TK_OpenParen) {
258+
if (tokenizer->nextTokenKind() != TokenKind::OpenParen) {
274259
// Parse as a named value.
275260
auto namedValue =
276261
namedValues ? namedValues->lookup(nameToken.text) : VariantValue();
@@ -281,7 +266,7 @@ bool Parser::parseIdentifierPrefixImpl(VariantValue *value) {
281266
return false;
282267
}
283268

284-
if (tokenizer->nextTokenKind() == TokenInfo::TK_NewLine) {
269+
if (tokenizer->nextTokenKind() == TokenKind::NewLine) {
285270
error->addError(tokenizer->peekNextToken().range,
286271
Diagnostics::ErrorType::ParserNoOpenParen)
287272
<< "NewLine";
@@ -290,10 +275,10 @@ bool Parser::parseIdentifierPrefixImpl(VariantValue *value) {
290275

291276
// If the syntax is correct and the name is not a matcher either, report
292277
// an unknown named value.
293-
if ((tokenizer->nextTokenKind() == TokenInfo::TK_Comma ||
294-
tokenizer->nextTokenKind() == TokenInfo::TK_CloseParen ||
295-
tokenizer->nextTokenKind() == TokenInfo::TK_NewLine ||
296-
tokenizer->nextTokenKind() == TokenInfo::TK_Eof) &&
278+
if ((tokenizer->nextTokenKind() == TokenKind::Comma ||
279+
tokenizer->nextTokenKind() == TokenKind::CloseParen ||
280+
tokenizer->nextTokenKind() == TokenKind::NewLine ||
281+
tokenizer->nextTokenKind() == TokenKind::Eof) &&
297282
!sema->lookupMatcherCtor(nameToken.text)) {
298283
error->addError(nameToken.range,
299284
Diagnostics::ErrorType::RegistryValueNotFound)
@@ -305,9 +290,9 @@ bool Parser::parseIdentifierPrefixImpl(VariantValue *value) {
305290

306291
tokenizer->skipNewlines();
307292

308-
assert(nameToken.kind == TokenInfo::TK_Ident);
293+
assert(nameToken.kind == TokenKind::Ident);
309294
TokenInfo openToken = tokenizer->consumeNextToken();
310-
if (openToken.kind != TokenInfo::TK_OpenParen) {
295+
if (openToken.kind != TokenKind::OpenParen) {
311296
error->addError(openToken.range, Diagnostics::ErrorType::ParserNoOpenParen)
312297
<< openToken.text;
313298
return false;
@@ -324,8 +309,8 @@ bool Parser::parseMatcherArgs(std::vector<ParserValue> &args, MatcherCtor ctor,
324309
const TokenInfo &nameToken, TokenInfo &endToken) {
325310
ScopedContextEntry sce(this, ctor);
326311

327-
while (tokenizer->nextTokenKind() != TokenInfo::TK_Eof) {
328-
if (tokenizer->nextTokenKind() == TokenInfo::TK_CloseParen) {
312+
while (tokenizer->nextTokenKind() != TokenKind::Eof) {
313+
if (tokenizer->nextTokenKind() == TokenKind::CloseParen) {
329314
// end of args.
330315
endToken = tokenizer->consumeNextToken();
331316
break;
@@ -334,7 +319,7 @@ bool Parser::parseMatcherArgs(std::vector<ParserValue> &args, MatcherCtor ctor,
334319
if (!args.empty()) {
335320
// We must find a , token to continue.
336321
TokenInfo commaToken = tokenizer->consumeNextToken();
337-
if (commaToken.kind != TokenInfo::TK_Comma) {
322+
if (commaToken.kind != TokenKind::Comma) {
338323
error->addError(commaToken.range, Diagnostics::ErrorType::ParserNoComma)
339324
<< commaToken.text;
340325
return false;
@@ -380,7 +365,7 @@ bool Parser::parseMatcherExpressionImpl(const TokenInfo &nameToken,
380365
}
381366

382367
// Check for the missing closing parenthesis
383-
if (endToken.kind != TokenInfo::TK_CloseParen) {
368+
if (endToken.kind != TokenKind::CloseParen) {
384369
error->addError(openToken.range, Diagnostics::ErrorType::ParserNoCloseParen)
385370
<< nameToken.text;
386371
return false;
@@ -425,7 +410,7 @@ Parser::getNamedValueCompletions(ArrayRef<ArgKind> acceptedTypes) {
425410

426411
void Parser::addExpressionCompletions() {
427412
const TokenInfo compToken = tokenizer->consumeNextTokenIgnoreNewlines();
428-
assert(compToken.kind == TokenInfo::TK_CodeCompletion);
413+
assert(compToken.kind == TokenKind::CodeCompletion);
429414

430415
// We cannot complete code if there is an invalid element on the context
431416
// stack.
@@ -447,31 +432,31 @@ void Parser::addExpressionCompletions() {
447432
// Parse an <Expresssion>
448433
bool Parser::parseExpressionImpl(VariantValue *value) {
449434
switch (tokenizer->nextTokenKind()) {
450-
case TokenInfo::TK_Literal:
435+
case TokenKind::Literal:
451436
*value = tokenizer->consumeNextToken().value;
452437
return true;
453-
case TokenInfo::TK_Ident:
438+
case TokenKind::Ident:
454439
return parseIdentifierPrefixImpl(value);
455-
case TokenInfo::TK_CodeCompletion:
440+
case TokenKind::CodeCompletion:
456441
addExpressionCompletions();
457442
return false;
458-
case TokenInfo::TK_Eof:
443+
case TokenKind::Eof:
459444
error->addError(tokenizer->consumeNextToken().range,
460445
Diagnostics::ErrorType::ParserNoCode);
461446
return false;
462447

463-
case TokenInfo::TK_Error:
448+
case TokenKind::Error:
464449
// This error was already reported by the tokenizer.
465450
return false;
466-
case TokenInfo::TK_NewLine:
467-
case TokenInfo::TK_OpenParen:
468-
case TokenInfo::TK_CloseParen:
469-
case TokenInfo::TK_Comma:
470-
case TokenInfo::TK_Period:
471-
case TokenInfo::TK_InvalidChar:
451+
case TokenKind::NewLine:
452+
case TokenKind::OpenParen:
453+
case TokenKind::CloseParen:
454+
case TokenKind::Comma:
455+
case TokenKind::Period:
456+
case TokenKind::InvalidChar:
472457
const TokenInfo token = tokenizer->consumeNextToken();
473458
error->addError(token.range, Diagnostics::ErrorType::ParserInvalidToken)
474-
<< (token.kind == TokenInfo::TK_NewLine ? "NewLine" : token.text);
459+
<< (token.kind == TokenKind::NewLine ? "NewLine" : token.text);
475460
return false;
476461
}
477462

@@ -516,8 +501,8 @@ bool Parser::parseExpression(llvm::StringRef &code, Sema *sema,
516501
if (!parser.parseExpressionImpl(value))
517502
return false;
518503
auto nextToken = tokenizer.peekNextToken();
519-
if (nextToken.kind != TokenInfo::TK_Eof &&
520-
nextToken.kind != TokenInfo::TK_NewLine) {
504+
if (nextToken.kind != TokenKind::Eof &&
505+
nextToken.kind != TokenKind::NewLine) {
521506
error->addError(tokenizer.peekNextToken().range,
522507
Diagnostics::ErrorType::ParserTrailingCode);
523508
return false;

0 commit comments

Comments
 (0)