Use the ExtensionDirectiveHandler type in other places where it makes sense.
[oota-llvm.git] / include / llvm / MC / MCParser / MCAsmParser.h
1 //===-- llvm/MC/MCAsmParser.h - Abstract Asm Parser Interface ---*- C++ -*-===//
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
10 #ifndef LLVM_MC_MCPARSER_MCASMPARSER_H
11 #define LLVM_MC_MCPARSER_MCASMPARSER_H
12
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"
17 #include <vector>
18
19 namespace llvm {
20 class MCAsmInfo;
21 class MCAsmLexer;
22 class MCAsmParserExtension;
23 class MCContext;
24 class MCExpr;
25 class MCInstPrinter;
26 class MCInstrInfo;
27 class MCParsedAsmOperand;
28 class MCStreamer;
29 class MCTargetAsmParser;
30 class SMLoc;
31 class SMRange;
32 class SourceMgr;
33 class StringRef;
34 class Twine;
35
36 /// MCAsmParserSemaCallback - Generic Sema callback for assembly parser.
37 class MCAsmParserSemaCallback {
38 public:
39   virtual ~MCAsmParserSemaCallback(); 
40   virtual void *LookupInlineAsmIdentifier(StringRef Name, void *Loc,
41                                           unsigned &Size, bool &IsVarDecl) = 0;
42   virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
43                                     unsigned &Offset) = 0;
44 };
45
46
47 /// \brief Helper types for tracking macro definitions.
48 typedef std::vector<AsmToken> MCAsmMacroArgument;
49 typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
50 typedef std::pair<StringRef, MCAsmMacroArgument> MCAsmMacroParameter;
51 typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
52
53 struct MCAsmMacro {
54   StringRef Name;
55   StringRef Body;
56   MCAsmMacroParameters Parameters;
57
58 public:
59   MCAsmMacro(StringRef N, StringRef B, const MCAsmMacroParameters &P) :
60     Name(N), Body(B), Parameters(P) {}
61
62   MCAsmMacro(const MCAsmMacro& Other)
63     : Name(Other.Name), Body(Other.Body), Parameters(Other.Parameters) {}
64 };
65
66 /// MCAsmParser - Generic assembler parser interface, for use by target specific
67 /// assembly parsers.
68 class MCAsmParser {
69 public:
70   typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc);
71   typedef std::pair<MCAsmParserExtension*, DirectiveHandler>
72     ExtensionDirectiveHandler;
73
74 private:
75   MCAsmParser(const MCAsmParser &) LLVM_DELETED_FUNCTION;
76   void operator=(const MCAsmParser &) LLVM_DELETED_FUNCTION;
77
78   MCTargetAsmParser *TargetParser;
79
80   unsigned ShowParsedOperands : 1;
81
82 protected: // Can only create subclasses.
83   MCAsmParser();
84
85 public:
86   virtual ~MCAsmParser();
87
88   virtual void AddDirectiveHandler(StringRef Directive,
89                                    ExtensionDirectiveHandler Handler) = 0;
90
91   virtual SourceMgr &getSourceManager() = 0;
92
93   virtual MCAsmLexer &getLexer() = 0;
94
95   virtual MCContext &getContext() = 0;
96
97   /// getStreamer - Return the output streamer for the assembler.
98   virtual MCStreamer &getStreamer() = 0;
99
100   MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
101   void setTargetParser(MCTargetAsmParser &P);
102
103   virtual unsigned getAssemblerDialect() { return 0;}
104   virtual void setAssemblerDialect(unsigned i) { }
105
106   bool getShowParsedOperands() const { return ShowParsedOperands; }
107   void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
108
109   /// Run - Run the parser on the input source buffer.
110   virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
111
112   virtual void setParsingInlineAsm(bool V) = 0;
113   virtual bool isParsingInlineAsm() = 0;
114
115   /// ParseMSInlineAsm - Parse ms-style inline assembly.
116   virtual bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
117                                 unsigned &NumOutputs, unsigned &NumInputs,
118                                 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
119                                 SmallVectorImpl<std::string> &Constraints,
120                                 SmallVectorImpl<std::string> &Clobbers,
121                                 const MCInstrInfo *MII,
122                                 const MCInstPrinter *IP,
123                                 MCAsmParserSemaCallback &SI) = 0;
124
125   /// Warning - Emit a warning at the location \p L, with the message \p Msg.
126   ///
127   /// \return The return value is true, if warnings are fatal.
128   virtual bool Warning(SMLoc L, const Twine &Msg,
129                        ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
130
131   /// Error - Emit an error at the location \p L, with the message \p Msg.
132   ///
133   /// \return The return value is always true, as an idiomatic convenience to
134   /// clients.
135   virtual bool Error(SMLoc L, const Twine &Msg,
136                      ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
137
138   /// Lex - Get the next AsmToken in the stream, possibly handling file
139   /// inclusion first.
140   virtual const AsmToken &Lex() = 0;
141
142   /// getTok - Get the current AsmToken from the stream.
143   const AsmToken &getTok();
144
145   /// \brief Report an error at the current lexer location.
146   bool TokError(const Twine &Msg,
147                 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
148
149   /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
150   /// and set \p Res to the identifier contents.
151   virtual bool ParseIdentifier(StringRef &Res) = 0;
152
153   /// \brief Parse up to the end of statement and return the contents from the
154   /// current token until the end of the statement; the current token on exit
155   /// will be either the EndOfStatement or EOF.
156   virtual StringRef ParseStringToEndOfStatement() = 0;
157
158   /// EatToEndOfStatement - Skip to the end of the current statement, for error
159   /// recovery.
160   virtual void EatToEndOfStatement() = 0;
161
162   /// \brief Are macros enabled in the parser?
163   virtual bool MacrosEnabled() = 0;
164
165   /// \brief Control a flag in the parser that enables or disables macros.
166   virtual void SetMacrosEnabled(bool flag) = 0;
167
168   /// \brief Lookup a previously defined macro.
169   /// \param Name Macro name.
170   /// \returns Pointer to macro. NULL if no such macro was defined.
171   virtual const MCAsmMacro* LookupMacro(StringRef Name) = 0;
172
173   /// \brief Define a new macro with the given name and information.
174   virtual void DefineMacro(StringRef Name, const MCAsmMacro& Macro) = 0;
175
176   /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
177   virtual void UndefineMacro(StringRef Name) = 0;
178
179   /// \brief Are we inside a macro instantiation?
180   virtual bool InsideMacroInstantiation() = 0;
181
182   /// \brief Handle entry to macro instantiation. 
183   ///
184   /// \param M The macro.
185   /// \param NameLoc Instantiation location.
186   virtual bool HandleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) = 0;
187
188   /// \brief Handle exit from macro instantiation.
189   virtual void HandleMacroExit() = 0;
190
191   /// ParseMacroArgument - Extract AsmTokens for a macro argument. If the
192   /// argument delimiter is initially unknown, set it to AsmToken::Eof. It will
193   /// be set to the correct delimiter by the method.
194   virtual bool ParseMacroArgument(MCAsmMacroArgument &MA,
195                                   AsmToken::TokenKind &ArgumentDelimiter) = 0;
196
197   /// ParseExpression - Parse an arbitrary expression.
198   ///
199   /// @param Res - The value of the expression. The result is undefined
200   /// on error.
201   /// @result - False on success.
202   virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
203   bool ParseExpression(const MCExpr *&Res);
204
205   /// ParseParenExpression - Parse an arbitrary expression, assuming that an
206   /// initial '(' has already been consumed.
207   ///
208   /// @param Res - The value of the expression. The result is undefined
209   /// on error.
210   /// @result - False on success.
211   virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
212
213   /// ParseAbsoluteExpression - Parse an expression which must evaluate to an
214   /// absolute value.
215   ///
216   /// @param Res - The value of the absolute expression. The result is undefined
217   /// on error.
218   /// @result - False on success.
219   virtual bool ParseAbsoluteExpression(int64_t &Res) = 0;
220
221   /// CheckForValidSection - Ensure that we have a valid section set in the
222   /// streamer. Otherwise, report and error and switch to .text.
223   virtual void CheckForValidSection() = 0;
224 };
225
226 /// \brief Create an MCAsmParser instance.
227 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
228                                MCStreamer &, const MCAsmInfo &);
229
230 } // End llvm namespace
231
232 #endif