LLVM  4.0.0
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/Twine.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/SourceMgr.h"
18 using namespace llvm;
19 
21  : TargetParser(nullptr), ShowParsedOperands(0), HadError(false),
22  PendingErrors() {}
23 
25 }
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::Hash) {
45  getLexer().Lex();
47  }
48  if (getTok().getKind() != AsmToken::EndOfStatement)
49  return Error(getTok().getLoc(), Msg);
50  Lex();
51  return false;
52 }
53 
55  if (T == AsmToken::EndOfStatement)
56  return parseEOL(Msg);
57  if (getTok().getKind() != T)
58  return Error(getTok().getLoc(), Msg);
59  Lex();
60  return false;
61 }
62 
63 bool MCAsmParser::parseIntToken(int64_t &V, const Twine &Msg) {
64  if (getTok().getKind() != AsmToken::Integer)
65  return TokError(Msg);
66  V = getTok().getIntVal();
67  Lex();
68  return false;
69 }
70 
72  bool Present = (getTok().getKind() == T);
73  // if token is EOL and current token is # this is an EOL comment.
75  Present = true;
76  if (Present)
77  parseToken(T);
78  return Present;
79 }
80 
81 bool MCAsmParser::check(bool P, const Twine &Msg) {
82  return check(P, getTok().getLoc(), Msg);
83 }
84 
85 bool MCAsmParser::check(bool P, SMLoc Loc, const Twine &Msg) {
86  if (P)
87  return Error(Loc, Msg);
88  return false;
89 }
90 
91 bool MCAsmParser::TokError(const Twine &Msg, SMRange Range) {
92  return Error(getLexer().getLoc(), Msg, Range);
93 }
94 
95 bool MCAsmParser::Error(SMLoc L, const Twine &Msg, SMRange Range) {
96  HadError = true;
97 
98  MCPendingError PErr;
99  PErr.Loc = L;
100  Msg.toVector(PErr.Msg);
101  PErr.Range = Range;
102  PendingErrors.push_back(PErr);
103 
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))
108  getLexer().Lex();
109  return true;
110 }
111 
112 bool MCAsmParser::addErrorSuffix(const Twine &Suffix) {
113  // Make sure lexing errors have propagated to the parser.
114  if (getTok().is(AsmToken::Error))
115  Lex();
116  for (auto &PErr : PendingErrors)
117  Suffix.toVector(PErr.Msg);
118  return true;
119 }
120 
121 bool MCAsmParser::parseMany(std::function<bool()> parseOne, bool hasComma) {
123  return false;
124  while (1) {
125  if (parseOne())
126  return true;
128  return false;
129  if (hasComma && parseToken(AsmToken::Comma))
130  return true;
131  }
132  return false;
133 }
134 
136  SMLoc L;
137  return parseExpression(Res, L);
138 }
139 
141  dbgs() << " " << *this;
142 }
MachineLoop * L
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:32
Represents a range in source code.
Definition: SMLoc.h:49
bool addErrorSuffix(const Twine &Suffix)
TokenKind getKind() const
Definition: MCAsmLexer.h:85
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:450
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
MCTargetAsmParser - Generic interface to target specific assembly parsers.
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
virtual void dump() const
dump - Print to the debug stream.
const AsmToken & getTok() const
Get the current (last) lexed token.
Definition: MCAsmLexer.h:207
bool parseIntToken(int64_t &V, const Twine &ErrMsg)
Definition: MCAsmParser.cpp:63
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:34
Target independent representation for an assembler token.
Definition: MCAsmLexer.h:25
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
bool parseMany(std::function< bool()> parseOne, bool hasComma=true)
#define T
Function Alias Analysis false
int64_t getIntVal() const
Definition: MCAsmLexer.h:119
const AsmToken & getTok() const
Get the current AsmToken from the stream.
Definition: MCAsmParser.cpp:33
bool parseToken(AsmToken::TokenKind T, const Twine &Msg="unexpected token")
Definition: MCAsmParser.cpp:54
#define P(N)
virtual ~MCAsmParser()
Flag tracking whether any errors have been encountered.
Definition: MCAsmParser.cpp:24
SMLoc getLoc() const
Definition: MCAsmLexer.cpp:28
virtual MCAsmLexer & getLexer()=0
bool parseTokenLoc(SMLoc &Loc)
Definition: MCAsmParser.cpp:37
bool Error(SMLoc L, const Twine &Msg, SMRange Range=None)
Return an error at the location L, with the message Msg.
Definition: MCAsmParser.cpp:95
virtual StringRef parseStringToEndOfStatement()=0
Parse up to the end of statement and return the contents from the current token until the end of the ...
void UnLex(AsmToken const &Token)
Definition: MCAsmLexer.h:194
SmallVector< MCPendingError, 1 > PendingErrors
Definition: MCAsmParser.h:91
const AsmToken & Lex()
Consume the next token from the input stream and return it.
Definition: MCAsmLexer.h:180
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
bool check(bool P, const llvm::Twine &Msg)
Definition: MCAsmParser.cpp:81
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:91
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:71
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
Represents a location in source code.
Definition: SMLoc.h:24