52948f749d64b1cebc37354a446c288f24a3bea2
[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_MCASMPARSER_H
11 #define LLVM_MC_MCASMPARSER_H
12
13 #include "llvm/Support/DataTypes.h"
14 #include "llvm/ADT/ArrayRef.h"
15
16 namespace llvm {
17 class AsmToken;
18 class MCAsmInfo;
19 class MCAsmLexer;
20 class MCAsmParserExtension;
21 class MCContext;
22 class MCExpr;
23 class MCParsedAsmOperand;
24 class MCStreamer;
25 class MCTargetAsmParser;
26 class SMLoc;
27 class SMRange;
28 class SourceMgr;
29 class StringRef;
30 class Twine;
31
32 /// MCAsmParser - Generic assembler parser interface, for use by target specific
33 /// assembly parsers.
34 class MCAsmParser {
35 public:
36   typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc);
37
38 private:
39   MCAsmParser(const MCAsmParser &) LLVM_DELETED_FUNCTION;
40   void operator=(const MCAsmParser &) LLVM_DELETED_FUNCTION;
41
42   MCTargetAsmParser *TargetParser;
43
44   unsigned ShowParsedOperands : 1;
45
46 protected: // Can only create subclasses.
47   MCAsmParser();
48
49 public:
50   virtual ~MCAsmParser();
51
52   virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
53                                    StringRef Directive,
54                                    DirectiveHandler Handler) = 0;
55
56   virtual SourceMgr &getSourceManager() = 0;
57
58   virtual MCAsmLexer &getLexer() = 0;
59
60   virtual MCContext &getContext() = 0;
61
62   /// getStreamer - Return the output streamer for the assembler.
63   virtual MCStreamer &getStreamer() = 0;
64
65   MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
66   void setTargetParser(MCTargetAsmParser &P);
67
68   virtual unsigned getAssemblerDialect() { return 0;}
69   virtual void setAssemblerDialect(unsigned i) { }
70
71   bool getShowParsedOperands() const { return ShowParsedOperands; }
72   void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
73
74   /// Run - Run the parser on the input source buffer.
75   virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
76
77   virtual void setParsingInlineAsm(bool V) = 0;
78   virtual bool isParsingInlineAsm() = 0;
79
80   /// ParseStatement - Parse the next statement.
81   virtual bool ParseStatement() = 0;
82
83   /// getNumParsedOperands - Returns the number of MCAsmParsedOperands from the
84   /// previously parsed statement.
85   virtual unsigned getNumParsedOperands() = 0;
86
87   /// getParsedOperand - Get a MCAsmParsedOperand.
88   virtual MCParsedAsmOperand &getParsedOperand(unsigned OpNum) = 0;
89
90   /// freeParsedOperands - Free the MCAsmParsedOperands.
91   virtual void freeParsedOperands() = 0;
92
93   /// isInstruction - Was the previously parsed statement an instruction?
94   virtual bool isInstruction() = 0;
95
96   /// getOpcode - Get the opcode from the previously parsed instruction.
97   virtual unsigned getOpcode() = 0;
98
99   /// Warning - Emit a warning at the location \p L, with the message \p Msg.
100   ///
101   /// \return The return value is true, if warnings are fatal.
102   virtual bool Warning(SMLoc L, const Twine &Msg,
103                        ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
104
105   /// Error - Emit an error at the location \p L, with the message \p Msg.
106   ///
107   /// \return The return value is always true, as an idiomatic convenience to
108   /// clients.
109   virtual bool Error(SMLoc L, const Twine &Msg,
110                      ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
111
112   /// Lex - Get the next AsmToken in the stream, possibly handling file
113   /// inclusion first.
114   virtual const AsmToken &Lex() = 0;
115
116   /// getTok - Get the current AsmToken from the stream.
117   const AsmToken &getTok();
118
119   /// \brief Report an error at the current lexer location.
120   bool TokError(const Twine &Msg,
121                 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
122
123   /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
124   /// and set \p Res to the identifier contents.
125   virtual bool ParseIdentifier(StringRef &Res) = 0;
126
127   /// \brief Parse up to the end of statement and return the contents from the
128   /// current token until the end of the statement; the current token on exit
129   /// will be either the EndOfStatement or EOF.
130   virtual StringRef ParseStringToEndOfStatement() = 0;
131
132   /// EatToEndOfStatement - Skip to the end of the current statement, for error
133   /// recovery.
134   virtual void EatToEndOfStatement() = 0;
135
136   /// ParseExpression - Parse an arbitrary expression.
137   ///
138   /// @param Res - The value of the expression. The result is undefined
139   /// on error.
140   /// @result - False on success.
141   virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
142   bool ParseExpression(const MCExpr *&Res);
143
144   /// ParseParenExpression - Parse an arbitrary expression, assuming that an
145   /// initial '(' has already been consumed.
146   ///
147   /// @param Res - The value of the expression. The result is undefined
148   /// on error.
149   /// @result - False on success.
150   virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
151
152   /// ParseAbsoluteExpression - Parse an expression which must evaluate to an
153   /// absolute value.
154   ///
155   /// @param Res - The value of the absolute expression. The result is undefined
156   /// on error.
157   /// @result - False on success.
158   virtual bool ParseAbsoluteExpression(int64_t &Res) = 0;
159 };
160
161 /// \brief Create an MCAsmParser instance.
162 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
163                                MCStreamer &, const MCAsmInfo &);
164
165 } // End llvm namespace
166
167 #endif