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 /// MCAsmParserSemaCallback - Generic Sema callback for assembly parser.
34 class MCAsmParserSemaCallback {
39 unsigned Length, Size, Type;
48 } InlineAsmIdentifierInfo;
50 virtual ~MCAsmParserSemaCallback();
51 virtual void *LookupInlineAsmIdentifier(StringRef &LineBuf,
52 InlineAsmIdentifierInfo &Info) = 0;
54 virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
55 unsigned &Offset) = 0;
58 typedef MCAsmParserSemaCallback::InlineAsmIdentifierInfo
59 InlineAsmIdentifierInfo;
61 /// MCAsmParser - Generic assembler parser interface, for use by target specific
65 typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc);
66 typedef std::pair<MCAsmParserExtension*, DirectiveHandler>
67 ExtensionDirectiveHandler;
70 MCAsmParser(const MCAsmParser &) LLVM_DELETED_FUNCTION;
71 void operator=(const MCAsmParser &) LLVM_DELETED_FUNCTION;
73 MCTargetAsmParser *TargetParser;
75 unsigned ShowParsedOperands : 1;
77 protected: // Can only create subclasses.
81 virtual ~MCAsmParser();
83 virtual void addDirectiveHandler(StringRef Directive,
84 ExtensionDirectiveHandler Handler) = 0;
86 virtual SourceMgr &getSourceManager() = 0;
88 virtual MCAsmLexer &getLexer() = 0;
90 virtual MCContext &getContext() = 0;
92 /// getStreamer - Return the output streamer for the assembler.
93 virtual MCStreamer &getStreamer() = 0;
95 MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
96 void setTargetParser(MCTargetAsmParser &P);
98 virtual unsigned getAssemblerDialect() { return 0;}
99 virtual void setAssemblerDialect(unsigned i) { }
101 bool getShowParsedOperands() const { return ShowParsedOperands; }
102 void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
104 /// Run - Run the parser on the input source buffer.
105 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
107 virtual void setParsingInlineAsm(bool V) = 0;
108 virtual bool isParsingInlineAsm() = 0;
110 /// parseMSInlineAsm - Parse ms-style inline assembly.
111 virtual bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
112 unsigned &NumOutputs, unsigned &NumInputs,
113 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
114 SmallVectorImpl<std::string> &Constraints,
115 SmallVectorImpl<std::string> &Clobbers,
116 const MCInstrInfo *MII,
117 const MCInstPrinter *IP,
118 MCAsmParserSemaCallback &SI) = 0;
120 /// Warning - Emit a warning at the location \p L, with the message \p Msg.
122 /// \return The return value is true, if warnings are fatal.
123 virtual bool Warning(SMLoc L, const Twine &Msg,
124 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
126 /// Error - Emit an error at the location \p L, with the message \p Msg.
128 /// \return The return value is always true, as an idiomatic convenience to
130 virtual bool Error(SMLoc L, const Twine &Msg,
131 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
133 /// Lex - Get the next AsmToken in the stream, possibly handling file
135 virtual const AsmToken &Lex() = 0;
137 /// getTok - Get the current AsmToken from the stream.
138 const AsmToken &getTok();
140 /// \brief Report an error at the current lexer location.
141 bool TokError(const Twine &Msg,
142 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
144 /// parseIdentifier - Parse an identifier or string (as a quoted identifier)
145 /// and set \p Res to the identifier contents.
146 virtual bool parseIdentifier(StringRef &Res) = 0;
148 /// \brief Parse up to the end of statement and return the contents from the
149 /// current token until the end of the statement; the current token on exit
150 /// will be either the EndOfStatement or EOF.
151 virtual StringRef parseStringToEndOfStatement() = 0;
153 /// parseEscapedString - Parse the current token as a string which may include
154 /// escaped characters and return the string contents.
155 virtual bool parseEscapedString(std::string &Data) = 0;
157 /// eatToEndOfStatement - Skip to the end of the current statement, for error
159 virtual void eatToEndOfStatement() = 0;
161 /// parseExpression - Parse an arbitrary expression.
163 /// @param Res - The value of the expression. The result is undefined
165 /// @result - False on success.
166 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
167 bool parseExpression(const MCExpr *&Res);
169 /// parsePrimaryExpr - Parse a primary expression.
171 /// @param Res - The value of the expression. The result is undefined
173 /// @result - False on success.
174 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0;
176 /// parseParenExpression - Parse an arbitrary expression, assuming that an
177 /// initial '(' has already been consumed.
179 /// @param Res - The value of the expression. The result is undefined
181 /// @result - False on success.
182 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
184 /// parseAbsoluteExpression - Parse an expression which must evaluate to an
187 /// @param Res - The value of the absolute expression. The result is undefined
189 /// @result - False on success.
190 virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
192 /// checkForValidSection - Ensure that we have a valid section set in the
193 /// streamer. Otherwise, report an error and switch to .text.
194 virtual void checkForValidSection() = 0;
197 /// \brief Create an MCAsmParser instance.
198 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
199 MCStreamer &, const MCAsmInfo &);
201 } // End llvm namespace