1 //===-- llvm/MC/MCAsmParser.h - Abstract Asm Parser Interface ---*- C++ -*-===//
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 #ifndef LLVM_MC_MCPARSER_MCASMPARSER_H
11 #define LLVM_MC_MCPARSER_MCASMPARSER_H
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/MC/MCParser/AsmLexer.h"
16 #include "llvm/Support/DataTypes.h"
21 class MCAsmParserExtension;
27 class MCTargetAsmParser;
33 class InlineAsmIdentifierInfo {
37 unsigned Length, Size, Type;
48 /// \brief Generic Sema callback for assembly parser.
49 class MCAsmParserSemaCallback {
51 virtual ~MCAsmParserSemaCallback();
52 virtual void *LookupInlineAsmIdentifier(StringRef &LineBuf,
53 InlineAsmIdentifierInfo &Info,
54 bool IsUnevaluatedContext) = 0;
55 virtual StringRef LookupInlineAsmLabel(StringRef Identifier, SourceMgr &SM,
56 SMLoc Location, bool Create) = 0;
58 virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
59 unsigned &Offset) = 0;
62 /// \brief Generic assembler parser interface, for use by target specific
66 typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc);
67 typedef std::pair<MCAsmParserExtension*, DirectiveHandler>
68 ExtensionDirectiveHandler;
71 MCAsmParser(const MCAsmParser &) = delete;
72 void operator=(const MCAsmParser &) = delete;
74 MCTargetAsmParser *TargetParser;
76 unsigned ShowParsedOperands : 1;
78 protected: // Can only create subclasses.
82 virtual ~MCAsmParser();
84 virtual void addDirectiveHandler(StringRef Directive,
85 ExtensionDirectiveHandler Handler) = 0;
87 virtual void addAliasForDirective(StringRef Directive, StringRef Alias) = 0;
89 virtual SourceMgr &getSourceManager() = 0;
91 virtual MCAsmLexer &getLexer() = 0;
92 const MCAsmLexer &getLexer() const {
93 return const_cast<MCAsmParser*>(this)->getLexer();
96 virtual MCContext &getContext() = 0;
98 /// \brief Return the output streamer for the assembler.
99 virtual MCStreamer &getStreamer() = 0;
101 MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
102 void setTargetParser(MCTargetAsmParser &P);
104 virtual unsigned getAssemblerDialect() { return 0;}
105 virtual void setAssemblerDialect(unsigned i) { }
107 bool getShowParsedOperands() const { return ShowParsedOperands; }
108 void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
110 /// \brief Run the parser on the input source buffer.
111 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
113 virtual void setParsingInlineAsm(bool V) = 0;
114 virtual bool isParsingInlineAsm() = 0;
116 /// \brief Parse MS-style inline assembly.
117 virtual bool parseMSInlineAsm(
118 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
119 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
120 SmallVectorImpl<std::string> &Constraints,
121 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
122 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) = 0;
124 /// \brief Emit a note at the location \p L, with the message \p Msg.
125 virtual void Note(SMLoc L, const Twine &Msg,
126 ArrayRef<SMRange> Ranges = None) = 0;
128 /// \brief Emit a warning at the location \p L, with the message \p Msg.
130 /// \return The return value is true, if warnings are fatal.
131 virtual bool Warning(SMLoc L, const Twine &Msg,
132 ArrayRef<SMRange> Ranges = None) = 0;
134 /// \brief Emit an error at the location \p L, with the message \p Msg.
136 /// \return The return value is always true, as an idiomatic convenience to
138 virtual bool Error(SMLoc L, const Twine &Msg,
139 ArrayRef<SMRange> Ranges = None) = 0;
141 /// \brief Get the next AsmToken in the stream, possibly handling file
143 virtual const AsmToken &Lex() = 0;
145 /// \brief Get the current AsmToken from the stream.
146 const AsmToken &getTok() const;
148 /// \brief Report an error at the current lexer location.
149 bool TokError(const Twine &Msg, ArrayRef<SMRange> Ranges = None);
151 /// \brief Parse an identifier or string (as a quoted identifier) and set \p
152 /// Res to the identifier contents.
153 virtual bool parseIdentifier(StringRef &Res) = 0;
155 /// \brief Parse up to the end of statement and return the contents from the
156 /// current token until the end of the statement; the current token on exit
157 /// will be either the EndOfStatement or EOF.
158 virtual StringRef parseStringToEndOfStatement() = 0;
160 /// \brief Parse the current token as a string which may include escaped
161 /// characters and return the string contents.
162 virtual bool parseEscapedString(std::string &Data) = 0;
164 /// \brief Skip to the end of the current statement, for error recovery.
165 virtual void eatToEndOfStatement() = 0;
167 /// \brief Parse an arbitrary expression.
169 /// \param Res - The value of the expression. The result is undefined
171 /// \return - False on success.
172 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
173 bool parseExpression(const MCExpr *&Res);
175 /// \brief Parse a primary expression.
177 /// \param Res - The value of the expression. The result is undefined
179 /// \return - False on success.
180 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0;
182 /// \brief Parse an arbitrary expression, assuming that an initial '(' has
183 /// already been consumed.
185 /// \param Res - The value of the expression. The result is undefined
187 /// \return - False on success.
188 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
190 /// \brief Parse an expression which must evaluate to an absolute value.
192 /// \param Res - The value of the absolute expression. The result is undefined
194 /// \return - False on success.
195 virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
197 /// \brief Ensure that we have a valid section set in the streamer. Otherwise,
198 /// report an error and switch to .text.
199 virtual void checkForValidSection() = 0;
201 /// \brief Parse an arbitrary expression of a specified parenthesis depth,
202 /// assuming that the initial '(' characters have already been consumed.
204 /// \param ParenDepth - Specifies how many trailing expressions outside the
205 /// current parentheses we have to parse.
206 /// \param Res - The value of the expression. The result is undefined
208 /// \return - False on success.
209 virtual bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
213 /// \brief Create an MCAsmParser instance.
214 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &,
217 } // End llvm namespace