1 //===-- MCAsmParser.cpp - Abstract Asm Parser Interface -------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/MCParser/MCAsmParser.h"
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/MC/MCParser/MCAsmLexer.h"
14 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
15 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/SMLoc.h"
18 #include "llvm/Support/raw_ostream.h"
23 MCAsmParser::MCAsmParser() {}
25 MCAsmParser::~MCAsmParser() = default;
27 void MCAsmParser::setTargetParser(MCTargetAsmParser
&P
) {
28 assert(!TargetParser
&& "Target parser is already initialized!");
30 TargetParser
->Initialize(*this);
33 const AsmToken
&MCAsmParser::getTok() const {
34 return getLexer().getTok();
37 bool MCAsmParser::parseTokenLoc(SMLoc
&Loc
) {
38 Loc
= getTok().getLoc();
42 bool MCAsmParser::parseEOL(const Twine
&Msg
) {
43 if (getTok().getKind() != AsmToken::EndOfStatement
)
44 return Error(getTok().getLoc(), Msg
);
49 bool MCAsmParser::parseToken(AsmToken::TokenKind T
, const Twine
&Msg
) {
50 if (T
== AsmToken::EndOfStatement
)
52 if (getTok().getKind() != T
)
53 return Error(getTok().getLoc(), Msg
);
58 bool MCAsmParser::parseIntToken(int64_t &V
, const Twine
&Msg
) {
59 if (getTok().getKind() != AsmToken::Integer
)
61 V
= getTok().getIntVal();
66 bool MCAsmParser::parseOptionalToken(AsmToken::TokenKind T
) {
67 bool Present
= (getTok().getKind() == T
);
73 bool MCAsmParser::check(bool P
, const Twine
&Msg
) {
74 return check(P
, getTok().getLoc(), Msg
);
77 bool MCAsmParser::check(bool P
, SMLoc Loc
, const Twine
&Msg
) {
79 return Error(Loc
, Msg
);
83 bool MCAsmParser::TokError(const Twine
&Msg
, SMRange Range
) {
84 return Error(getLexer().getLoc(), Msg
, Range
);
87 bool MCAsmParser::Error(SMLoc L
, const Twine
&Msg
, SMRange Range
) {
91 Msg
.toVector(PErr
.Msg
);
93 PendingErrors
.push_back(PErr
);
95 // If we threw this parsing error after a lexing error, this should
96 // supercede the lexing error and so we remove it from the Lexer
97 // before it can propagate
98 if (getTok().is(AsmToken::Error
))
103 bool MCAsmParser::addErrorSuffix(const Twine
&Suffix
) {
104 // Make sure lexing errors have propagated to the parser.
105 if (getTok().is(AsmToken::Error
))
107 for (auto &PErr
: PendingErrors
)
108 Suffix
.toVector(PErr
.Msg
);
112 bool MCAsmParser::parseMany(function_ref
<bool()> parseOne
, bool hasComma
) {
113 if (parseOptionalToken(AsmToken::EndOfStatement
))
118 if (parseOptionalToken(AsmToken::EndOfStatement
))
120 if (hasComma
&& parseToken(AsmToken::Comma
))
126 bool MCAsmParser::parseExpression(const MCExpr
*&Res
) {
128 return parseExpression(Res
, L
);
131 void MCParsedAsmOperand::dump() const {
132 // Cannot completely remove virtual function even in release mode.
133 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
134 dbgs() << " " << *this;