d9034b2bb11c585f8619e50577454452599cfdc1
[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
18 namespace llvm {
19 class MCAsmInfo;
20 class MCAsmLexer;
21 class MCAsmParserExtension;
22 class MCContext;
23 class MCExpr;
24 class MCInstPrinter;
25 class MCInstrInfo;
26 class MCStreamer;
27 class MCTargetAsmParser;
28 class SMLoc;
29 class SMRange;
30 class SourceMgr;
31 class Twine;
32
33 class InlineAsmIdentifierInfo {
34 public:
35   void *OpDecl;
36   bool IsVarDecl;
37   unsigned Length, Size, Type;
38
39   void clear() {
40     OpDecl = nullptr;
41     IsVarDecl = false;
42     Length = 1;
43     Size = 0;
44     Type = 0;
45   }
46 };
47
48 /// MCAsmParserSemaCallback - Generic Sema callback for assembly parser.
49 class MCAsmParserSemaCallback {
50 public:
51   typedef llvm::InlineAsmIdentifierInfo InlineAsmIdentifierInfo;
52
53   virtual ~MCAsmParserSemaCallback();
54   virtual void *LookupInlineAsmIdentifier(StringRef &LineBuf,
55                                           InlineAsmIdentifierInfo &Info,
56                                           bool IsUnevaluatedContext) = 0;
57
58   virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
59                                     unsigned &Offset) = 0;
60 };
61
62 /// MCAsmParser - Generic assembler parser interface, for use by target specific
63 /// assembly parsers.
64 class MCAsmParser {
65 public:
66   typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc);
67   typedef std::pair<MCAsmParserExtension*, DirectiveHandler>
68     ExtensionDirectiveHandler;
69
70 private:
71   MCAsmParser(const MCAsmParser &) LLVM_DELETED_FUNCTION;
72   void operator=(const MCAsmParser &) LLVM_DELETED_FUNCTION;
73
74   MCTargetAsmParser *TargetParser;
75
76   unsigned ShowParsedOperands : 1;
77
78 protected: // Can only create subclasses.
79   MCAsmParser();
80
81 public:
82   virtual ~MCAsmParser();
83
84   virtual void addDirectiveHandler(StringRef Directive,
85                                    ExtensionDirectiveHandler Handler) = 0;
86
87   virtual SourceMgr &getSourceManager() = 0;
88
89   virtual MCAsmLexer &getLexer() = 0;
90
91   virtual MCContext &getContext() = 0;
92
93   /// getStreamer - Return the output streamer for the assembler.
94   virtual MCStreamer &getStreamer() = 0;
95
96   MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
97   void setTargetParser(MCTargetAsmParser &P);
98
99   virtual unsigned getAssemblerDialect() { return 0;}
100   virtual void setAssemblerDialect(unsigned i) { }
101
102   bool getShowParsedOperands() const { return ShowParsedOperands; }
103   void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
104
105   /// Run - Run the parser on the input source buffer.
106   virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
107
108   virtual void setParsingInlineAsm(bool V) = 0;
109   virtual bool isParsingInlineAsm() = 0;
110
111   /// parseMSInlineAsm - Parse ms-style inline assembly.
112   virtual bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
113                                 unsigned &NumOutputs, unsigned &NumInputs,
114                                 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
115                                 SmallVectorImpl<std::string> &Constraints,
116                                 SmallVectorImpl<std::string> &Clobbers,
117                                 const MCInstrInfo *MII,
118                                 const MCInstPrinter *IP,
119                                 MCAsmParserSemaCallback &SI) = 0;
120
121   /// Note - Emit a note at the location \p L, with the message \p Msg.
122   virtual void Note(SMLoc L, const Twine &Msg,
123                     ArrayRef<SMRange> Ranges = None) = 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 = None) = 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 = None) = 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, ArrayRef<SMRange> Ranges = None);
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   /// parseEscapedString - Parse the current token as a string which may include
158   /// escaped characters and return the string contents.
159   virtual bool parseEscapedString(std::string &Data) = 0;
160
161   /// eatToEndOfStatement - Skip to the end of the current statement, for error
162   /// recovery.
163   virtual void eatToEndOfStatement() = 0;
164
165   /// parseExpression - Parse an arbitrary expression.
166   ///
167   /// @param Res - The value of the expression. The result is undefined
168   /// on error.
169   /// @result - False on success.
170   virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
171   bool parseExpression(const MCExpr *&Res);
172
173   /// parsePrimaryExpr - Parse a primary expression.
174   ///
175   /// @param Res - The value of the expression. The result is undefined
176   /// on error.
177   /// @result - False on success.
178   virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0;
179
180   /// parseParenExpression - Parse an arbitrary expression, assuming that an
181   /// initial '(' has already been consumed.
182   ///
183   /// @param Res - The value of the expression. The result is undefined
184   /// on error.
185   /// @result - False on success.
186   virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
187
188   /// parseAbsoluteExpression - Parse an expression which must evaluate to an
189   /// absolute value.
190   ///
191   /// @param Res - The value of the absolute expression. The result is undefined
192   /// on error.
193   /// @result - False on success.
194   virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
195
196   /// checkForValidSection - Ensure that we have a valid section set in the
197   /// streamer. Otherwise, report an error and switch to .text.
198   virtual void checkForValidSection() = 0;
199 };
200
201 /// \brief Create an MCAsmParser instance.
202 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
203                                MCStreamer &, const MCAsmInfo &);
204
205 } // End llvm namespace
206
207 #endif