Properly encapsulate additional methods and data from AsmParser.
[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
72 private:
73   MCAsmParser(const MCAsmParser &) LLVM_DELETED_FUNCTION;
74   void operator=(const MCAsmParser &) LLVM_DELETED_FUNCTION;
75
76   MCTargetAsmParser *TargetParser;
77
78   unsigned ShowParsedOperands : 1;
79
80 protected: // Can only create subclasses.
81   MCAsmParser();
82
83 public:
84   virtual ~MCAsmParser();
85
86   virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
87                                    StringRef Directive,
88                                    DirectiveHandler Handler) = 0;
89
90   virtual SourceMgr &getSourceManager() = 0;
91
92   virtual MCAsmLexer &getLexer() = 0;
93
94   virtual MCContext &getContext() = 0;
95
96   /// getStreamer - Return the output streamer for the assembler.
97   virtual MCStreamer &getStreamer() = 0;
98
99   MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
100   void setTargetParser(MCTargetAsmParser &P);
101
102   virtual unsigned getAssemblerDialect() { return 0;}
103   virtual void setAssemblerDialect(unsigned i) { }
104
105   bool getShowParsedOperands() const { return ShowParsedOperands; }
106   void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
107
108   /// Run - Run the parser on the input source buffer.
109   virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
110
111   virtual void setParsingInlineAsm(bool V) = 0;
112   virtual bool isParsingInlineAsm() = 0;
113
114   /// ParseMSInlineAsm - Parse ms-style inline assembly.
115   virtual bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
116                                 unsigned &NumOutputs, unsigned &NumInputs,
117                                 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
118                                 SmallVectorImpl<std::string> &Constraints,
119                                 SmallVectorImpl<std::string> &Clobbers,
120                                 const MCInstrInfo *MII,
121                                 const MCInstPrinter *IP,
122                                 MCAsmParserSemaCallback &SI) = 0;
123
124   /// Warning - Emit a warning at the location \p L, with the message \p Msg.
125   ///
126   /// \return The return value is true, if warnings are fatal.
127   virtual bool Warning(SMLoc L, const Twine &Msg,
128                        ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
129
130   /// Error - Emit an error at the location \p L, with the message \p Msg.
131   ///
132   /// \return The return value is always true, as an idiomatic convenience to
133   /// clients.
134   virtual bool Error(SMLoc L, const Twine &Msg,
135                      ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
136
137   /// Lex - Get the next AsmToken in the stream, possibly handling file
138   /// inclusion first.
139   virtual const AsmToken &Lex() = 0;
140
141   /// getTok - Get the current AsmToken from the stream.
142   const AsmToken &getTok();
143
144   /// \brief Report an error at the current lexer location.
145   bool TokError(const Twine &Msg,
146                 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
147
148   /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
149   /// and set \p Res to the identifier contents.
150   virtual bool ParseIdentifier(StringRef &Res) = 0;
151
152   /// \brief Parse up to the end of statement and return the contents from the
153   /// current token until the end of the statement; the current token on exit
154   /// will be either the EndOfStatement or EOF.
155   virtual StringRef ParseStringToEndOfStatement() = 0;
156
157   /// EatToEndOfStatement - Skip to the end of the current statement, for error
158   /// recovery.
159   virtual void EatToEndOfStatement() = 0;
160
161   /// \brief Are macros enabled in the parser?
162   virtual bool MacrosEnabled() = 0;
163
164   /// \brief Control a flag in the parser that enables or disables macros.
165   virtual void SetMacrosEnabled(bool flag) = 0;
166
167   /// \brief Lookup a previously defined macro.
168   /// \param Name Macro name.
169   /// \returns Pointer to macro. NULL if no such macro was defined.
170   virtual const MCAsmMacro* LookupMacro(StringRef Name) = 0;
171
172   /// \brief Define a new macro with the given name and information.
173   virtual void DefineMacro(StringRef Name, const MCAsmMacro& Macro) = 0;
174
175   /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
176   virtual void UndefineMacro(StringRef Name) = 0;
177
178   /// \brief Are we inside a macro instantiation?
179   virtual bool InsideMacroInstantiation() = 0;
180
181   /// \brief Handle entry to macro instantiation. 
182   ///
183   /// \param M The macro.
184   /// \param NameLoc Instantiation location.
185   virtual bool HandleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) = 0;
186
187   /// \brief Handle exit from macro instantiation.
188   virtual void HandleMacroExit() = 0;
189
190   /// ParseMacroArgument - Extract AsmTokens for a macro argument. If the
191   /// argument delimiter is initially unknown, set it to AsmToken::Eof. It will
192   /// be set to the correct delimiter by the method.
193   virtual bool ParseMacroArgument(MCAsmMacroArgument &MA,
194                                   AsmToken::TokenKind &ArgumentDelimiter) = 0;
195
196   /// ParseExpression - Parse an arbitrary expression.
197   ///
198   /// @param Res - The value of the expression. The result is undefined
199   /// on error.
200   /// @result - False on success.
201   virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
202   bool ParseExpression(const MCExpr *&Res);
203
204   /// ParseParenExpression - Parse an arbitrary expression, assuming that an
205   /// initial '(' has already been consumed.
206   ///
207   /// @param Res - The value of the expression. The result is undefined
208   /// on error.
209   /// @result - False on success.
210   virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
211
212   /// ParseAbsoluteExpression - Parse an expression which must evaluate to an
213   /// absolute value.
214   ///
215   /// @param Res - The value of the absolute expression. The result is undefined
216   /// on error.
217   /// @result - False on success.
218   virtual bool ParseAbsoluteExpression(int64_t &Res) = 0;
219
220   /// CheckForValidSection - Ensure that we have a valid section set in the
221   /// streamer. Otherwise, report and error and switch to .text.
222   virtual void CheckForValidSection() = 0;
223 };
224
225 /// \brief Create an MCAsmParser instance.
226 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
227                                MCStreamer &, const MCAsmInfo &);
228
229 } // End llvm namespace
230
231 #endif