1 //===-- MCAsmParser.cpp - Abstract Asm Parser Interface -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCParser/MCAsmParser.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/ADT/Twine.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() : ShowParsedOperands(0) {}
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::Hash) {
44 StringRef CommentStr = parseStringToEndOfStatement();
46 getLexer().UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr));
48 if (getTok().getKind() != AsmToken::EndOfStatement)
49 return Error(getTok().getLoc(), Msg);
54 bool MCAsmParser::parseToken(AsmToken::TokenKind T, const Twine &Msg) {
55 if (T == AsmToken::EndOfStatement)
57 if (getTok().getKind() != T)
58 return Error(getTok().getLoc(), Msg);
63 bool MCAsmParser::parseIntToken(int64_t &V, const Twine &Msg) {
64 if (getTok().getKind() != AsmToken::Integer)
66 V = getTok().getIntVal();
71 bool MCAsmParser::parseOptionalToken(AsmToken::TokenKind T) {
72 bool Present = (getTok().getKind() == T);
73 // if token is EOL and current token is # this is an EOL comment.
74 if (getTok().getKind() == AsmToken::Hash && T == AsmToken::EndOfStatement)
81 bool MCAsmParser::check(bool P, const Twine &Msg) {
82 return check(P, getTok().getLoc(), Msg);
85 bool MCAsmParser::check(bool P, SMLoc Loc, const Twine &Msg) {
87 return Error(Loc, Msg);
91 bool MCAsmParser::TokError(const Twine &Msg, SMRange Range) {
92 return Error(getLexer().getLoc(), Msg, Range);
95 bool MCAsmParser::Error(SMLoc L, const Twine &Msg, SMRange Range) {
100 Msg.toVector(PErr.Msg);
102 PendingErrors.push_back(PErr);
104 // If we threw this parsing error after a lexing error, this should
105 // supercede the lexing error and so we remove it from the Lexer
106 // before it can propagate
107 if (getTok().is(AsmToken::Error))
112 bool MCAsmParser::addErrorSuffix(const Twine &Suffix) {
113 // Make sure lexing errors have propagated to the parser.
114 if (getTok().is(AsmToken::Error))
116 for (auto &PErr : PendingErrors)
117 Suffix.toVector(PErr.Msg);
121 bool MCAsmParser::parseMany(function_ref<bool()> parseOne, bool hasComma) {
122 if (parseOptionalToken(AsmToken::EndOfStatement))
127 if (parseOptionalToken(AsmToken::EndOfStatement))
129 if (hasComma && parseToken(AsmToken::Comma))
135 bool MCAsmParser::parseExpression(const MCExpr *&Res) {
137 return parseExpression(Res, L);
140 void MCParsedAsmOperand::dump() const {
141 // Cannot completely remove virtual function even in release mode.
142 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
143 dbgs() << " " << *this;