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 {
36 virtual ~MCAsmParserSemaCallback();
37 virtual void *LookupInlineAsmIdentifier(StringRef Name, void *Loc,
38 unsigned &Length, unsigned &Size,
39 unsigned &Type, bool &IsVarDecl) = 0;
41 virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
42 unsigned &Offset) = 0;
46 /// MCAsmParser - Generic assembler parser interface, for use by target specific
50 typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc);
51 typedef std::pair<MCAsmParserExtension*, DirectiveHandler>
52 ExtensionDirectiveHandler;
55 MCAsmParser(const MCAsmParser &) LLVM_DELETED_FUNCTION;
56 void operator=(const MCAsmParser &) LLVM_DELETED_FUNCTION;
58 MCTargetAsmParser *TargetParser;
60 unsigned ShowParsedOperands : 1;
62 protected: // Can only create subclasses.
66 virtual ~MCAsmParser();
68 virtual void addDirectiveHandler(StringRef Directive,
69 ExtensionDirectiveHandler Handler) = 0;
71 virtual SourceMgr &getSourceManager() = 0;
73 virtual MCAsmLexer &getLexer() = 0;
75 virtual MCContext &getContext() = 0;
77 /// getStreamer - Return the output streamer for the assembler.
78 virtual MCStreamer &getStreamer() = 0;
80 MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
81 void setTargetParser(MCTargetAsmParser &P);
83 virtual unsigned getAssemblerDialect() { return 0;}
84 virtual void setAssemblerDialect(unsigned i) { }
86 bool getShowParsedOperands() const { return ShowParsedOperands; }
87 void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
89 /// Run - Run the parser on the input source buffer.
90 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
92 virtual void setParsingInlineAsm(bool V) = 0;
93 virtual bool isParsingInlineAsm() = 0;
95 /// parseMSInlineAsm - Parse ms-style inline assembly.
96 virtual bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
97 unsigned &NumOutputs, unsigned &NumInputs,
98 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
99 SmallVectorImpl<std::string> &Constraints,
100 SmallVectorImpl<std::string> &Clobbers,
101 const MCInstrInfo *MII,
102 const MCInstPrinter *IP,
103 MCAsmParserSemaCallback &SI) = 0;
105 /// Warning - Emit a warning at the location \p L, with the message \p Msg.
107 /// \return The return value is true, if warnings are fatal.
108 virtual bool Warning(SMLoc L, const Twine &Msg,
109 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
111 /// Error - Emit an error at the location \p L, with the message \p Msg.
113 /// \return The return value is always true, as an idiomatic convenience to
115 virtual bool Error(SMLoc L, const Twine &Msg,
116 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
118 /// Lex - Get the next AsmToken in the stream, possibly handling file
120 virtual const AsmToken &Lex() = 0;
122 /// getTok - Get the current AsmToken from the stream.
123 const AsmToken &getTok();
125 /// \brief Report an error at the current lexer location.
126 bool TokError(const Twine &Msg,
127 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
129 /// parseIdentifier - Parse an identifier or string (as a quoted identifier)
130 /// and set \p Res to the identifier contents.
131 virtual bool parseIdentifier(StringRef &Res) = 0;
133 /// \brief Parse up to the end of statement and return the contents from the
134 /// current token until the end of the statement; the current token on exit
135 /// will be either the EndOfStatement or EOF.
136 virtual StringRef parseStringToEndOfStatement() = 0;
138 /// parseEscapedString - Parse the current token as a string which may include
139 /// escaped characters and return the string contents.
140 virtual bool parseEscapedString(std::string &Data) = 0;
142 /// eatToEndOfStatement - Skip to the end of the current statement, for error
144 virtual void eatToEndOfStatement() = 0;
146 /// parseExpression - Parse an arbitrary expression.
148 /// @param Res - The value of the expression. The result is undefined
150 /// @result - False on success.
151 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
152 bool parseExpression(const MCExpr *&Res);
154 /// parseParenExpression - Parse an arbitrary expression, assuming that an
155 /// initial '(' has already been consumed.
157 /// @param Res - The value of the expression. The result is undefined
159 /// @result - False on success.
160 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
162 /// parseAbsoluteExpression - Parse an expression which must evaluate to an
165 /// @param Res - The value of the absolute expression. The result is undefined
167 /// @result - False on success.
168 virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
170 /// checkForValidSection - Ensure that we have a valid section set in the
171 /// streamer. Otherwise, report an error and switch to .text.
172 virtual void checkForValidSection() = 0;
175 /// \brief Create an MCAsmParser instance.
176 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
177 MCStreamer &, const MCAsmInfo &);
179 } // End llvm namespace