Skip to content

Commit 92c93f5

Browse files
committed
[MC] Merge MCAsmLexer and AsmLexer
Follow-up to #134207 Both classes define `IsAtStartOfStatement` but the semantics are confusingly different. Rename the base class one.
1 parent 46e2c07 commit 92c93f5

File tree

3 files changed

+25
-53
lines changed

3 files changed

+25
-53
lines changed

llvm/include/llvm/MC/MCParser/AsmLexer.h

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,24 @@ class AsmCommentConsumer {
3939
virtual void HandleComment(SMLoc Loc, StringRef CommentText) = 0;
4040
};
4141

42-
/// Generic assembler lexer interface, for use by target specific assembly
43-
/// lexers.
44-
class MCAsmLexer {
42+
class AsmLexer {
4543
/// The current token, stored in the base class for faster access.
4644
SmallVector<AsmToken, 1> CurTok;
4745

46+
const char *CurPtr = nullptr;
47+
StringRef CurBuf;
48+
4849
/// The location and description of the current error
4950
SMLoc ErrLoc;
5051
std::string Err;
5152

52-
protected: // Can only create subclasses.
53+
const MCAsmInfo &MAI;
54+
55+
bool IsAtStartOfLine = true;
56+
bool AtStartOfStatement = true;
57+
bool IsPeeking = false;
58+
bool EndStatementAtEOF = true;
59+
5360
const char *TokStart = nullptr;
5461
bool SkipSpace = true;
5562
bool AllowAtInIdentifier = false;
@@ -65,19 +72,17 @@ class MCAsmLexer {
6572
bool LexHLASMStrings = false;
6673
AsmCommentConsumer *CommentConsumer = nullptr;
6774

68-
MCAsmLexer();
69-
70-
virtual AsmToken LexToken() = 0;
75+
AsmToken LexToken();
7176

7277
void SetError(SMLoc errLoc, const std::string &err) {
7378
ErrLoc = errLoc;
7479
Err = err;
7580
}
7681

7782
public:
78-
MCAsmLexer(const MCAsmLexer &) = delete;
79-
MCAsmLexer &operator=(const MCAsmLexer &) = delete;
80-
virtual ~MCAsmLexer();
83+
AsmLexer(const MCAsmInfo &MAI);
84+
AsmLexer(const AsmLexer &) = delete;
85+
AsmLexer &operator=(const AsmLexer &) = delete;
8186

8287
/// Consume the next token from the input stream and return it.
8388
///
@@ -86,7 +91,7 @@ class MCAsmLexer {
8691
const AsmToken &Lex() {
8792
assert(!CurTok.empty());
8893
// Mark if we parsing out a EndOfStatement.
89-
IsAtStartOfStatement = CurTok.front().getKind() == AsmToken::EndOfStatement;
94+
AtStartOfStatement = CurTok.front().getKind() == AsmToken::EndOfStatement;
9095
CurTok.erase(CurTok.begin());
9196
// LexToken may generate multiple tokens via UnLex but will always return
9297
// the first one. Place returned value at head of CurTok vector.
@@ -98,16 +103,16 @@ class MCAsmLexer {
98103
}
99104

100105
void UnLex(AsmToken const &Token) {
101-
IsAtStartOfStatement = false;
106+
AtStartOfStatement = false;
102107
CurTok.insert(CurTok.begin(), Token);
103108
}
104109

105-
bool isAtStartOfStatement() { return IsAtStartOfStatement; }
110+
bool isAtStartOfStatement() { return AtStartOfStatement; }
106111

107-
virtual StringRef LexUntilEndOfStatement() = 0;
112+
StringRef LexUntilEndOfStatement();
108113

109114
/// Get the current source location.
110-
SMLoc getLoc() const;
115+
SMLoc getLoc() const { return SMLoc::getFromPointer(TokStart); }
111116

112117
/// Get the current (last) lexed token.
113118
const AsmToken &getTok() const { return CurTok[0]; }
@@ -126,8 +131,7 @@ class MCAsmLexer {
126131
}
127132

128133
/// Look ahead an arbitrary number of tokens.
129-
virtual size_t peekTokens(MutableArrayRef<AsmToken> Buf,
130-
bool ShouldSkipSpace = true) = 0;
134+
size_t peekTokens(MutableArrayRef<AsmToken> Buf, bool ShouldSkipSpace = true);
131135

132136
/// Get the current error location
133137
SMLoc getErrLoc() { return ErrLoc; }
@@ -185,37 +189,10 @@ class MCAsmLexer {
185189
/// setting this option to true, will disable lexing for character and string
186190
/// literals.
187191
void setLexHLASMStrings(bool V) { LexHLASMStrings = V; }
188-
};
189-
190-
/// AsmLexer - Lexer class for assembly files.
191-
class AsmLexer final : public MCAsmLexer {
192-
const MCAsmInfo &MAI;
193-
194-
const char *CurPtr = nullptr;
195-
StringRef CurBuf;
196-
bool IsAtStartOfLine = true;
197-
bool IsAtStartOfStatement = true;
198-
bool IsPeeking = false;
199-
bool EndStatementAtEOF = true;
200-
201-
protected:
202-
/// LexToken - Read the next token and return its code.
203-
AsmToken LexToken() override;
204-
205-
public:
206-
AsmLexer(const MCAsmInfo &MAI);
207-
AsmLexer(const AsmLexer &) = delete;
208-
AsmLexer &operator=(const AsmLexer &) = delete;
209-
~AsmLexer() override;
210192

211193
void setBuffer(StringRef Buf, const char *ptr = nullptr,
212194
bool EndStatementAtEOF = true);
213195

214-
StringRef LexUntilEndOfStatement() override;
215-
216-
size_t peekTokens(MutableArrayRef<AsmToken> Buf,
217-
bool ShouldSkipSpace = true) override;
218-
219196
const MCAsmInfo &getMAI() const { return MAI; }
220197

221198
private:
@@ -237,6 +214,8 @@ class AsmLexer final : public MCAsmLexer {
237214
StringRef LexUntilEndOfLine();
238215
};
239216

217+
using MCAsmLexer = AsmLexer;
218+
240219
} // end namespace llvm
241220

242221
#endif // LLVM_MC_MCPARSER_ASMLEXER_H

llvm/include/llvm/MC/MCParser/MCAsmParser.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
namespace llvm {
2626

27-
class MCAsmLexer;
2827
class MCAsmInfo;
2928
class MCAsmParserExtension;
3029
class MCExpr;

llvm/lib/MC/MCParser/AsmLexer.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,15 @@ void AsmToken::dump(raw_ostream &OS) const {
111111
OS << "\")";
112112
}
113113

114-
MCAsmLexer::MCAsmLexer() { CurTok.emplace_back(AsmToken::Space, StringRef()); }
115-
116-
MCAsmLexer::~MCAsmLexer() = default;
117-
118-
SMLoc MCAsmLexer::getLoc() const { return SMLoc::getFromPointer(TokStart); }
119-
120114
AsmLexer::AsmLexer(const MCAsmInfo &MAI) : MAI(MAI) {
121115
// For COFF targets, this is true, while for ELF targets, it should be false.
122116
// Currently, @specifier parsing depends on '@' being included in the token.
123117
AllowAtInIdentifier = !StringRef(MAI.getCommentString()).starts_with("@") &&
124118
MAI.useAtForSpecifier();
125119
LexMotorolaIntegers = MAI.shouldUseMotorolaIntegers();
126-
}
127120

128-
AsmLexer::~AsmLexer() = default;
121+
CurTok.emplace_back(AsmToken::Space, StringRef());
122+
}
129123

130124
void AsmLexer::setBuffer(StringRef Buf, const char *ptr,
131125
bool EndStatementAtEOF) {

0 commit comments

Comments
 (0)