LLVM  6.0.0svn
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"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/SMLoc.h"
19 #include <cassert>
20 
21 using namespace llvm;
22 
23 MCAsmParser::MCAsmParser() : ShowParsedOperands(0) {}
24 
25 MCAsmParser::~MCAsmParser() = default;
26 
28  assert(!TargetParser && "Target parser is already initialized!");
29  TargetParser = &P;
30  TargetParser->Initialize(*this);
31 }
32 
33 const AsmToken &MCAsmParser::getTok() const {
34  return getLexer().getTok();
35 }
36 
38  Loc = getTok().getLoc();
39  return false;
40 }
41 
42 bool MCAsmParser::parseEOL(const Twine &Msg) {
43  if (getTok().getKind() != AsmToken::EndOfStatement)
44  return Error(getTok().getLoc(), Msg);
45  Lex();
46  return false;
47 }
48 
50  if (T == AsmToken::EndOfStatement)
51  return parseEOL(Msg);
52  if (getTok().getKind() != T)
53  return Error(getTok().getLoc(), Msg);
54  Lex();
55  return false;
56 }
57 
58 bool MCAsmParser::parseIntToken(int64_t &V, const Twine &Msg) {
59  if (getTok().getKind() != AsmToken::Integer)
60  return TokError(Msg);
61  V = getTok().getIntVal();
62  Lex();
63  return false;
64 }
65 
67  bool Present = (getTok().getKind() == T);
68  if (Present)
69  parseToken(T);
70  return Present;
71 }
72 
73 bool MCAsmParser::check(bool P, const Twine &Msg) {
74  return check(P, getTok().getLoc(), Msg);
75 }
76 
77 bool MCAsmParser::check(bool P, SMLoc Loc, const Twine &Msg) {
78  if (P)
79  return Error(Loc, Msg);
80  return false;
81 }
82 
83 bool MCAsmParser::TokError(const Twine &Msg, SMRange Range) {
84  return Error(getLexer().getLoc(), Msg, Range);
85 }
86 
87 bool MCAsmParser::Error(SMLoc L, const Twine &Msg, SMRange Range) {
88  HadError = true;
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:218
bool addErrorSuffix(const Twine &Suffix)
Compute iterated dominance frontiers using a linear time algorithm.
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.
bool check(bool P, const Twine &Msg)
Definition: MCAsmParser.cpp:73
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:89
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:33
bool parseIntToken(int64_t &V, const Twine &ErrMsg)
Definition: MCAsmParser.cpp:58
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: MCAsmLexer.h:27
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
#define T
SMLoc getLoc() const
Definition: MCAsmLexer.cpp:26
bool parseToken(AsmToken::TokenKind T, const Twine &Msg="unexpected token")
Definition: MCAsmParser.cpp:49
#define P(N)
virtual ~MCAsmParser()
virtual MCAsmLexer & getLexer()=0
bool HadError
Flag tracking whether any errors have been encountered.
Definition: MCAsmParser.h:131
bool parseTokenLoc(SMLoc &Loc)
Definition: MCAsmParser.cpp:37
int64_t getIntVal() const
Definition: MCAsmLexer.h:121
bool Error(SMLoc L, const Twine &Msg, SMRange Range=None)
Return an error at the location L, with the message Msg.
Definition: MCAsmParser.cpp:87
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:32
bool parseMany(function_ref< bool()> parseOne, bool hasComma=true)
SmallVector< MCPendingError, 1 > PendingErrors
Definition: MCAsmParser.h:135
const AsmToken & Lex()
Consume the next token from the input stream and return it.
Definition: MCAsmLexer.h:191
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
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:83
bool parseEOL(const Twine &ErrMsg)
Definition: MCAsmParser.cpp:42
void setTargetParser(MCTargetAsmParser &P)
Definition: MCAsmParser.cpp:27
bool parseOptionalToken(AsmToken::TokenKind T)
Attempt to parse and consume token, returning true on success.
Definition: MCAsmParser.cpp:66
Represents a location in source code.
Definition: SMLoc.h:24
TokenKind getKind() const
Definition: MCAsmLexer.h:87