Skip to content

Commit b6e2df5

Browse files
committed
[MC] Move some member variables from AsmParser to MCAsmParser
to eliminate some virtual functions and avoid duplication between AsmParser/MasmParser.
1 parent 711b15d commit b6e2df5

File tree

4 files changed

+20
-38
lines changed

4 files changed

+20
-38
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "llvm/ADT/StringRef.h"
1616
#include "llvm/ADT/Twine.h"
1717
#include "llvm/MC/MCAsmMacro.h"
18+
#include "llvm/MC/MCContext.h"
19+
#include "llvm/MC/MCParser/AsmLexer.h"
1820
#include "llvm/Support/SMLoc.h"
1921
#include <cstdint>
2022
#include <string>
@@ -25,7 +27,6 @@ namespace llvm {
2527
class MCAsmLexer;
2628
class MCAsmInfo;
2729
class MCAsmParserExtension;
28-
class MCContext;
2930
class MCExpr;
3031
class MCInstPrinter;
3132
class MCInstrInfo;
@@ -136,8 +137,13 @@ class MCAsmParser {
136137
MCTargetAsmParser *TargetParser = nullptr;
137138

138139
protected: // Can only create subclasses.
139-
MCAsmParser();
140+
MCAsmParser(MCContext &, MCStreamer &, SourceMgr &, const MCAsmInfo &);
140141

142+
MCContext &Ctx;
143+
MCStreamer &Out;
144+
SourceMgr &SrcMgr;
145+
const MCAsmInfo &MAI;
146+
AsmLexer Lexer;
141147
SmallVector<MCPendingError, 0> PendingErrors;
142148

143149
/// Flag tracking whether any errors have been encountered.
@@ -155,17 +161,11 @@ class MCAsmParser {
155161

156162
virtual void addAliasForDirective(StringRef Directive, StringRef Alias) = 0;
157163

158-
virtual SourceMgr &getSourceManager() = 0;
159-
160-
virtual MCAsmLexer &getLexer() = 0;
161-
const MCAsmLexer &getLexer() const {
162-
return const_cast<MCAsmParser*>(this)->getLexer();
163-
}
164-
165-
virtual MCContext &getContext() = 0;
166-
167-
/// Return the output streamer for the assembler.
168-
virtual MCStreamer &getStreamer() = 0;
164+
MCContext &getContext() { return Ctx; }
165+
MCStreamer &getStreamer() { return Out; }
166+
SourceMgr &getSourceManager() { return SrcMgr; }
167+
MCAsmLexer &getLexer() { return Lexer; }
168+
const MCAsmLexer &getLexer() const { return Lexer; }
169169

170170
MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
171171
void setTargetParser(MCTargetAsmParser &P);

llvm/lib/MC/MCParser/AsmParser.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,6 @@ struct ParseStatementInfo {
117117
/// The concrete assembly parser instance.
118118
class AsmParser : public MCAsmParser {
119119
private:
120-
AsmLexer Lexer;
121-
MCContext &Ctx;
122-
MCStreamer &Out;
123-
const MCAsmInfo &MAI;
124-
SourceMgr &SrcMgr;
125120
SourceMgr::DiagHandlerTy SavedDiagHandler;
126121
void *SavedDiagContext;
127122
std::unique_ptr<MCAsmParserExtension> PlatformParser;
@@ -222,11 +217,6 @@ class AsmParser : public MCAsmParser {
222217
/// @name MCAsmParser Interface
223218
/// {
224219

225-
SourceMgr &getSourceManager() override { return SrcMgr; }
226-
MCAsmLexer &getLexer() override { return Lexer; }
227-
MCContext &getContext() override { return Ctx; }
228-
MCStreamer &getStreamer() override { return Out; }
229-
230220
CodeViewContext &getCVContext() { return Ctx.getCVContext(); }
231221

232222
unsigned getAssemblerDialect() override {
@@ -773,8 +763,8 @@ enum { DEFAULT_ADDRSPACE = 0 };
773763

774764
AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
775765
const MCAsmInfo &MAI, unsigned CB = 0)
776-
: Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
777-
CurBuffer(CB ? CB : SM.getMainFileID()), MacrosEnabledFlag(true) {
766+
: MCAsmParser(Ctx, Out, SM, MAI), CurBuffer(CB ? CB : SM.getMainFileID()),
767+
MacrosEnabledFlag(true) {
778768
HadError = false;
779769
// Save the old handler.
780770
SavedDiagHandler = SrcMgr.getDiagHandler();

llvm/lib/MC/MCParser/MCAsmParser.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ cl::opt<unsigned> AsmMacroMaxNestingDepth(
2727
cl::desc("The maximum nesting depth allowed for assembly macros."));
2828
}
2929

30-
MCAsmParser::MCAsmParser() = default;
30+
MCAsmParser::MCAsmParser(MCContext &Ctx, MCStreamer &Out, SourceMgr &SM,
31+
const MCAsmInfo &MAI)
32+
: Ctx(Ctx), Out(Out), SrcMgr(SM), MAI(MAI), Lexer(MAI) {}
3133

3234
MCAsmParser::~MCAsmParser() = default;
3335

llvm/lib/MC/MCParser/MasmParser.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,6 @@ FieldInitializer &FieldInitializer::operator=(FieldInitializer &&Initializer) {
375375
// It's a peer of AsmParser, not of COFFAsmParser, WasmAsmParser, etc.
376376
class MasmParser : public MCAsmParser {
377377
private:
378-
AsmLexer Lexer;
379-
MCContext &Ctx;
380-
MCStreamer &Out;
381-
const MCAsmInfo &MAI;
382-
SourceMgr &SrcMgr;
383378
SourceMgr::DiagHandlerTy SavedDiagHandler;
384379
void *SavedDiagContext;
385380
std::unique_ptr<MCAsmParserExtension> PlatformParser;
@@ -481,11 +476,6 @@ class MasmParser : public MCAsmParser {
481476
/// @name MCAsmParser Interface
482477
/// {
483478

484-
SourceMgr &getSourceManager() override { return SrcMgr; }
485-
MCAsmLexer &getLexer() override { return Lexer; }
486-
MCContext &getContext() override { return Ctx; }
487-
MCStreamer &getStreamer() override { return Out; }
488-
489479
unsigned getAssemblerDialect() override {
490480
if (AssemblerDialect == ~0U)
491481
return MAI.getAssemblerDialect();
@@ -973,8 +963,8 @@ enum { DEFAULT_ADDRSPACE = 0 };
973963

974964
MasmParser::MasmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
975965
const MCAsmInfo &MAI, struct tm TM, unsigned CB)
976-
: Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
977-
CurBuffer(CB ? CB : SM.getMainFileID()), TM(TM) {
966+
: MCAsmParser(Ctx, Out, SM, MAI), CurBuffer(CB ? CB : SM.getMainFileID()),
967+
TM(TM) {
978968
HadError = false;
979969
// Save the old handler.
980970
SavedDiagHandler = SrcMgr.getDiagHandler();

0 commit comments

Comments
 (0)