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/MC/MCParser/AsmLexer.h"
15 #include "llvm/Support/DataTypes.h"
21 class MCAsmParserExtension;
26 class MCParsedAsmOperand;
28 class MCTargetAsmParser;
35 /// MCAsmParserSemaCallback - Generic Sema callback for assembly parser.
36 class MCAsmParserSemaCallback {
38 virtual ~MCAsmParserSemaCallback();
39 virtual void *LookupInlineAsmIdentifier(StringRef Name, void *Loc,
40 unsigned &Size, bool &IsVarDecl) = 0;
41 virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
42 unsigned &Offset) = 0;
46 typedef std::vector<AsmToken> MCAsmMacroArgument;
49 /// MCAsmParser - Generic assembler parser interface, for use by target specific
53 typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc);
56 MCAsmParser(const MCAsmParser &) LLVM_DELETED_FUNCTION;
57 void operator=(const MCAsmParser &) LLVM_DELETED_FUNCTION;
59 MCTargetAsmParser *TargetParser;
61 unsigned ShowParsedOperands : 1;
63 protected: // Can only create subclasses.
67 virtual ~MCAsmParser();
69 virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
71 DirectiveHandler Handler) = 0;
73 virtual SourceMgr &getSourceManager() = 0;
75 virtual MCAsmLexer &getLexer() = 0;
77 virtual MCContext &getContext() = 0;
79 /// getStreamer - Return the output streamer for the assembler.
80 virtual MCStreamer &getStreamer() = 0;
82 MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
83 void setTargetParser(MCTargetAsmParser &P);
85 virtual unsigned getAssemblerDialect() { return 0;}
86 virtual void setAssemblerDialect(unsigned i) { }
88 bool getShowParsedOperands() const { return ShowParsedOperands; }
89 void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
91 /// Run - Run the parser on the input source buffer.
92 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
94 virtual void setParsingInlineAsm(bool V) = 0;
95 virtual bool isParsingInlineAsm() = 0;
97 /// ParseMSInlineAsm - Parse ms-style inline assembly.
98 virtual bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
99 unsigned &NumOutputs, unsigned &NumInputs,
100 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
101 SmallVectorImpl<std::string> &Constraints,
102 SmallVectorImpl<std::string> &Clobbers,
103 const MCInstrInfo *MII,
104 const MCInstPrinter *IP,
105 MCAsmParserSemaCallback &SI) = 0;
107 /// Warning - Emit a warning at the location \p L, with the message \p Msg.
109 /// \return The return value is true, if warnings are fatal.
110 virtual bool Warning(SMLoc L, const Twine &Msg,
111 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
113 /// Error - Emit an error at the location \p L, with the message \p Msg.
115 /// \return The return value is always true, as an idiomatic convenience to
117 virtual bool Error(SMLoc L, const Twine &Msg,
118 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
120 /// Lex - Get the next AsmToken in the stream, possibly handling file
122 virtual const AsmToken &Lex() = 0;
124 /// getTok - Get the current AsmToken from the stream.
125 const AsmToken &getTok();
127 /// \brief Report an error at the current lexer location.
128 bool TokError(const Twine &Msg,
129 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
131 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
132 /// and set \p Res to the identifier contents.
133 virtual bool ParseIdentifier(StringRef &Res) = 0;
135 /// \brief Parse up to the end of statement and return the contents from the
136 /// current token until the end of the statement; the current token on exit
137 /// will be either the EndOfStatement or EOF.
138 virtual StringRef ParseStringToEndOfStatement() = 0;
140 /// EatToEndOfStatement - Skip to the end of the current statement, for error
142 virtual void EatToEndOfStatement() = 0;
144 /// Control a flag in the parser that enables or disables macros.
145 virtual bool MacrosEnabled() = 0;
146 virtual void SetMacrosEnabled(bool flag) = 0;
148 /// ParseMacroArgument - Extract AsmTokens for a macro argument. If the
149 /// argument delimiter is initially unknown, set it to AsmToken::Eof. It will
150 /// be set to the correct delimiter by the method.
151 virtual bool ParseMacroArgument(MCAsmMacroArgument &MA,
152 AsmToken::TokenKind &ArgumentDelimiter) = 0;
154 /// ParseExpression - Parse an arbitrary expression.
156 /// @param Res - The value of the expression. The result is undefined
158 /// @result - False on success.
159 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
160 bool ParseExpression(const MCExpr *&Res);
162 /// ParseParenExpression - Parse an arbitrary expression, assuming that an
163 /// initial '(' has already been consumed.
165 /// @param Res - The value of the expression. The result is undefined
167 /// @result - False on success.
168 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
170 /// ParseAbsoluteExpression - Parse an expression which must evaluate to an
173 /// @param Res - The value of the absolute expression. The result is undefined
175 /// @result - False on success.
176 virtual bool ParseAbsoluteExpression(int64_t &Res) = 0;
178 /// CheckForValidSection - Ensure that we have a valid section set in the
179 /// streamer. Otherwise, report and error and switch to .text.
180 virtual void CheckForValidSection() = 0;
183 /// \brief Create an MCAsmParser instance.
184 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
185 MCStreamer &, const MCAsmInfo &);
187 } // End llvm namespace