MCAsmParser.h revision d4727e3798981af3d1308418464a10a398290851
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  /// parsePrimaryExpr - Parse a primary expression.
155  ///
156  /// @param Res - The value of the expression. The result is undefined
157  /// on error.
158  /// @result - False on success.
159  virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0;
160
161  /// parseParenExpression - Parse an arbitrary expression, assuming that an
162  /// initial '(' has already been consumed.
163  ///
164  /// @param Res - The value of the expression. The result is undefined
165  /// on error.
166  /// @result - False on success.
167  virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
168
169  /// parseAbsoluteExpression - Parse an expression which must evaluate to an
170  /// absolute value.
171  ///
172  /// @param Res - The value of the absolute expression. The result is undefined
173  /// on error.
174  /// @result - False on success.
175  virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
176
177  /// checkForValidSection - Ensure that we have a valid section set in the
178  /// streamer. Otherwise, report an error and switch to .text.
179  virtual void checkForValidSection() = 0;
180};
181
182/// \brief Create an MCAsmParser instance.
183MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
184                               MCStreamer &, const MCAsmInfo &);
185
186} // End llvm namespace
187
188#endif
189