LLVM  8.0.1
MCAsmParser.cpp
Go to the documentation of this file.
1 //===-- MCAsmParser.cpp - Abstract Asm Parser Interface -------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/Config/llvm-config.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/SMLoc.h"
20 #include <cassert>
21 
22 using namespace llvm;
23 
25 
26 MCAsmParser::~MCAsmParser() = default;
27 
29  assert(!TargetParser && "Target parser is already initialized!");
30  TargetParser = &P;
31  TargetParser->Initialize(*this);
32 }
33 
34 const AsmToken &MCAsmParser::getTok() const {
35  return getLexer().getTok();
36 }
37 
39  Loc = getTok().getLoc();
40  return false;
41 }
42 
43 bool MCAsmParser::parseEOL(const Twine &Msg) {
44  if (getTok().getKind() != AsmToken::EndOfStatement)
45  return Error(getTok().getLoc(), Msg);
46  Lex();
47  return false;
48 }
49 
51  if (T == AsmToken::EndOfStatement)
52  return parseEOL(Msg);
53  if (getTok().getKind() != T)
54  return Error(getTok().getLoc(), Msg);
55  Lex();
56  return false;
57 }
58 
59 bool MCAsmParser::parseIntToken(int64_t &V, const Twine &Msg) {
60  if (getTok().getKind() != AsmToken::Integer)
61  return TokError(Msg);
62  V = getTok().getIntVal();
63  Lex();
64  return false;
65 }
66 
68  bool Present = (getTok().getKind() == T);
69  if (Present)
70  parseToken(T);
71  return Present;
72 }
73 
74 bool MCAsmParser::check(bool P, const Twine &Msg) {
75  return check(P, getTok().getLoc(), Msg);
76 }
77 
78 bool MCAsmParser::check(bool P, SMLoc Loc, const Twine &Msg) {
79  if (P)
80  return Error(Loc, Msg);
81  return false;
82 }
83 
84 bool MCAsmParser::TokError(const Twine &Msg, SMRange Range) {
85  return Error(getLexer().getLoc(), Msg, Range);
86 }
87 
88 bool MCAsmParser::Error(SMLoc L, const Twine &Msg, SMRange Range) {
89 
90  MCPendingError PErr;
91  PErr.Loc = L;
92  Msg.toVector(PErr.Msg);
93  PErr.Range = Range;
94  PendingErrors.push_back(PErr);
95 
96  // If we threw this parsing error after a lexing error, this should
97  // supercede the lexing error and so we remove it from the Lexer
98  // before it can propagate
99  if (getTok().is(AsmToken::Error))
100  getLexer().Lex();
101  return true;
102 }
103 
104 bool MCAsmParser::addErrorSuffix(const Twine &Suffix) {
105  // Make sure lexing errors have propagated to the parser.
106  if (getTok().is(AsmToken::Error))
107  Lex();
108  for (auto &PErr : PendingErrors)
109  Suffix.toVector(PErr.Msg);
110  return true;
111 }
112 
113 bool MCAsmParser::parseMany(function_ref<bool()> parseOne, bool hasComma) {
115  return false;
116  while (true) {
117  if (parseOne())
118  return true;
120  return false;
121  if (hasComma && parseToken(AsmToken::Comma))
122  return true;
123  }
124  return false;
125 }
126 
128  SMLoc L;
129  return parseExpression(Res, L);
130 }
131 
133  // Cannot completely remove virtual function even in release mode.
134 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
135  dbgs() << " " << *this;
136 #endif
137 }
Represents a range in source code.
Definition: SMLoc.h:49
const AsmToken & getTok() const
Get the current (last) lexed token.
Definition: MCAsmLexer.h:101
bool addErrorSuffix(const Twine &Suffix)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
virtual void dump() const
dump - Print to the debug stream.
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
MCTargetAsmParser - Generic interface to target specific assembly parsers.
SmallVector< MCPendingError, 0 > PendingErrors
Definition: MCAsmParser.h:128
bool check(bool P, const Twine &Msg)
Definition: MCAsmParser.cpp:74
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:117
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
const AsmToken & getTok() const
Get the current AsmToken from the stream.
Definition: MCAsmParser.cpp:34
bool parseIntToken(int64_t &V, const Twine &ErrMsg)
Definition: MCAsmParser.cpp:59
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
Target independent representation for an assembler token.
Definition: MCAsmMacro.h:22
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
#define T
SMLoc getLoc() const
Definition: MCAsmLexer.cpp:28
bool parseToken(AsmToken::TokenKind T, const Twine &Msg="unexpected token")
Definition: MCAsmParser.cpp:50
#define P(N)
virtual ~MCAsmParser()
virtual MCAsmLexer & getLexer()=0
bool parseTokenLoc(SMLoc &Loc)
Definition: MCAsmParser.cpp:38
int64_t getIntVal() const
Definition: MCAsmMacro.h:116
bool Error(SMLoc L, const Twine &Msg, SMRange Range=None)
Return an error at the location L, with the message Msg.
Definition: MCAsmParser.cpp:88
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:33
bool parseMany(function_ref< bool()> parseOne, bool hasComma=true)
const AsmToken & Lex()
Consume the next token from the input stream and return it.
Definition: MCAsmLexer.h:74
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool TokError(const Twine &Msg, SMRange Range=None)
Report an error at the current lexer location.
Definition: MCAsmParser.cpp:84
bool parseEOL(const Twine &ErrMsg)
Definition: MCAsmParser.cpp:43
void setTargetParser(MCTargetAsmParser &P)
Definition: MCAsmParser.cpp:28
bool parseOptionalToken(AsmToken::TokenKind T)
Attempt to parse and consume token, returning true on success.
Definition: MCAsmParser.cpp:67
Represents a location in source code.
Definition: SMLoc.h:24
TokenKind getKind() const
Definition: MCAsmMacro.h:82