MCAsmParser.h revision 4913d7b3b22a0302bea69c98673b887ae2d89e40
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
18namespace llvm {
19class MCAsmInfo;
20class MCAsmLexer;
21class MCAsmParserExtension;
22class MCContext;
23class MCExpr;
24class MCInstPrinter;
25class MCInstrInfo;
26class MCStreamer;
27class MCTargetAsmParser;
28class SMLoc;
29class SMRange;
30class SourceMgr;
31class Twine;
32
33/// MCAsmParserSemaCallback - Generic Sema callback for assembly parser.
34class MCAsmParserSemaCallback {
35public:
36  virtual ~MCAsmParserSemaCallback();
37  virtual void *LookupInlineAsmIdentifier(StringRef Name, void *Loc,
38                                          unsigned &Length, unsigned &Size,
39                                          unsigned &Type, bool &IsVarDecl) = 0;
40
41  virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
42                                    unsigned &Offset) = 0;
43};
44
45
46/// MCAsmParser - Generic assembler parser interface, for use by target specific
47/// assembly parsers.
48class MCAsmParser {
49public:
50  typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc);
51  typedef std::pair<MCAsmParserExtension*, DirectiveHandler>
52    ExtensionDirectiveHandler;
53
54private:
55  MCAsmParser(const MCAsmParser &) LLVM_DELETED_FUNCTION;
56  void operator=(const MCAsmParser &) LLVM_DELETED_FUNCTION;
57
58  MCTargetAsmParser *TargetParser;
59
60  unsigned ShowParsedOperands : 1;
61
62protected: // Can only create subclasses.
63  MCAsmParser();
64
65public:
66  virtual ~MCAsmParser();
67
68  virtual void AddDirectiveHandler(StringRef Directive,
69                                   ExtensionDirectiveHandler Handler) = 0;
70
71  virtual SourceMgr &getSourceManager() = 0;
72
73  virtual MCAsmLexer &getLexer() = 0;
74
75  virtual MCContext &getContext() = 0;
76
77  /// getStreamer - Return the output streamer for the assembler.
78  virtual MCStreamer &getStreamer() = 0;
79
80  MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
81  void setTargetParser(MCTargetAsmParser &P);
82
83  virtual unsigned getAssemblerDialect() { return 0;}
84  virtual void setAssemblerDialect(unsigned i) { }
85
86  bool getShowParsedOperands() const { return ShowParsedOperands; }
87  void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
88
89  /// Run - Run the parser on the input source buffer.
90  virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
91
92  virtual void setParsingInlineAsm(bool V) = 0;
93  virtual bool isParsingInlineAsm() = 0;
94
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;
104
105  /// Warning - Emit a warning at the location \p L, with the message \p Msg.
106  ///
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;
110
111  /// Error - Emit an error at the location \p L, with the message \p Msg.
112  ///
113  /// \return The return value is always true, as an idiomatic convenience to
114  /// clients.
115  virtual bool Error(SMLoc L, const Twine &Msg,
116                     ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
117
118  /// Lex - Get the next AsmToken in the stream, possibly handling file
119  /// inclusion first.
120  virtual const AsmToken &Lex() = 0;
121
122  /// getTok - Get the current AsmToken from the stream.
123  const AsmToken &getTok();
124
125  /// \brief Report an error at the current lexer location.
126  bool TokError(const Twine &Msg,
127                ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
128
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;
132
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;
137
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;
141
142  /// EatToEndOfStatement - Skip to the end of the current statement, for error
143  /// recovery.
144  virtual void EatToEndOfStatement() = 0;
145
146  /// ParseExpression - Parse an arbitrary expression.
147  ///
148  /// @param Res - The value of the expression. The result is undefined
149  /// on error.
150  /// @result - False on success.
151  virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
152  bool ParseExpression(const MCExpr *&Res);
153
154  /// ParseParenExpression - Parse an arbitrary expression, assuming that an
155  /// initial '(' has already been consumed.
156  ///
157  /// @param Res - The value of the expression. The result is undefined
158  /// on error.
159  /// @result - False on success.
160  virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
161
162  /// ParseAbsoluteExpression - Parse an expression which must evaluate to an
163  /// absolute value.
164  ///
165  /// @param Res - The value of the absolute expression. The result is undefined
166  /// on error.
167  /// @result - False on success.
168  virtual bool ParseAbsoluteExpression(int64_t &Res) = 0;
169
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;
173};
174
175/// \brief Create an MCAsmParser instance.
176MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
177                               MCStreamer &, const MCAsmInfo &);
178
179} // End llvm namespace
180
181#endif
182