Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/MC/MCParser/MCAsmParser.h
Line
Count
Source (jump to first uncovered line)
1
//===- llvm/MC/MCAsmParser.h - Abstract Asm Parser Interface ----*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#ifndef LLVM_MC_MCPARSER_MCASMPARSER_H
10
#define LLVM_MC_MCPARSER_MCASMPARSER_H
11
12
#include "llvm/ADT/None.h"
13
#include "llvm/ADT/STLExtras.h"
14
#include "llvm/ADT/SmallString.h"
15
#include "llvm/ADT/SmallVector.h"
16
#include "llvm/ADT/StringRef.h"
17
#include "llvm/ADT/Twine.h"
18
#include "llvm/MC/MCParser/MCAsmLexer.h"
19
#include "llvm/Support/SMLoc.h"
20
#include <cstdint>
21
#include <string>
22
#include <utility>
23
24
namespace llvm {
25
26
class MCAsmInfo;
27
class MCAsmParserExtension;
28
class MCContext;
29
class MCExpr;
30
class MCInstPrinter;
31
class MCInstrInfo;
32
class MCStreamer;
33
class MCTargetAsmParser;
34
class SourceMgr;
35
36
struct InlineAsmIdentifierInfo {
37
  enum IdKind {
38
    IK_Invalid,  // Initial state. Unexpected after a successful parsing.
39
    IK_Label,    // Function/Label reference.
40
    IK_EnumVal,  // Value of enumeration type.
41
    IK_Var       // Variable.
42
  };
43
  // Represents an Enum value
44
  struct EnumIdentifier {
45
    int64_t EnumVal;
46
  };
47
  // Represents a label/function reference
48
  struct LabelIdentifier {
49
    void *Decl;
50
  };
51
  // Represents a variable
52
  struct VariableIdentifier {
53
    void *Decl;
54
    bool IsGlobalLV;
55
    unsigned Length;
56
    unsigned Size;
57
    unsigned Type;
58
  };
59
  // An InlineAsm identifier can only be one of those
60
  union {
61
    EnumIdentifier Enum;
62
    LabelIdentifier Label;
63
    VariableIdentifier Var;
64
  };
65
1.03k
  bool isKind(IdKind kind) const { return Kind == kind; }
66
  // Initializers
67
17
  void setEnum(int64_t enumVal) {
68
17
    assert(isKind(IK_Invalid) && "should be initialized only once");
69
17
    Kind = IK_EnumVal;
70
17
    Enum.EnumVal = enumVal;
71
17
  }
72
13
  void setLabel(void *decl) {
73
13
    assert(isKind(IK_Invalid) && "should be initialized only once");
74
13
    Kind = IK_Label;
75
13
    Label.Decl = decl;
76
13
  }
77
165
  void setVar(void *decl, bool isGlobalLV, unsigned size, unsigned type) {
78
165
    assert(isKind(IK_Invalid) && "should be initialized only once");
79
165
    Kind = IK_Var;
80
165
    Var.Decl = decl;
81
165
    Var.IsGlobalLV = isGlobalLV;
82
165
    Var.Size = size;
83
165
    Var.Type = type;
84
165
    Var.Length = size / type;
85
165
  }
86
9.85k
  InlineAsmIdentifierInfo() : Kind(IK_Invalid) {}
87
88
private:
89
  // Discriminate using the current kind.
90
  IdKind Kind;
91
};
92
93
/// Generic Sema callback for assembly parser.
94
class MCAsmParserSemaCallback {
95
public:
96
  virtual ~MCAsmParserSemaCallback();
97
98
  virtual void LookupInlineAsmIdentifier(StringRef &LineBuf,
99
                                         InlineAsmIdentifierInfo &Info,
100
                                         bool IsUnevaluatedContext) = 0;
101
  virtual StringRef LookupInlineAsmLabel(StringRef Identifier, SourceMgr &SM,
102
                                         SMLoc Location, bool Create) = 0;
103
  virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
104
                                    unsigned &Offset) = 0;
105
};
106
107
/// Generic assembler parser interface, for use by target specific
108
/// assembly parsers.
109
class MCAsmParser {
110
public:
111
  using DirectiveHandler = bool (*)(MCAsmParserExtension*, StringRef, SMLoc);
112
  using ExtensionDirectiveHandler =
113
      std::pair<MCAsmParserExtension*, DirectiveHandler>;
114
115
  struct MCPendingError {
116
    SMLoc Loc;
117
    SmallString<64> Msg;
118
    SMRange Range;
119
  };
120
121
private:
122
  MCTargetAsmParser *TargetParser = nullptr;
123
124
protected: // Can only create subclasses.
125
  MCAsmParser();
126
127
  SmallVector<MCPendingError, 0> PendingErrors;
128
129
  /// Flag tracking whether any errors have been encountered.
130
  bool HadError = false;
131
132
  bool ShowParsedOperands = false;
133
134
public:
135
  MCAsmParser(const MCAsmParser &) = delete;
136
  MCAsmParser &operator=(const MCAsmParser &) = delete;
137
  virtual ~MCAsmParser();
138
139
  virtual void addDirectiveHandler(StringRef Directive,
140
                                   ExtensionDirectiveHandler Handler) = 0;
141
142
  virtual void addAliasForDirective(StringRef Directive, StringRef Alias) = 0;
143
144
  virtual SourceMgr &getSourceManager() = 0;
145
146
  virtual MCAsmLexer &getLexer() = 0;
147
66.0M
  const MCAsmLexer &getLexer() const {
148
66.0M
    return const_cast<MCAsmParser*>(this)->getLexer();
149
66.0M
  }
150
151
  virtual MCContext &getContext() = 0;
152
153
  /// Return the output streamer for the assembler.
154
  virtual MCStreamer &getStreamer() = 0;
155
156
5.38M
  MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
157
  void setTargetParser(MCTargetAsmParser &P);
158
159
0
  virtual unsigned getAssemblerDialect() { return 0;}
160
0
  virtual void setAssemblerDialect(unsigned i) { }
161
162
664k
  bool getShowParsedOperands() const { return ShowParsedOperands; }
163
  void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
164
165
  /// Run the parser on the input source buffer.
166
  virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
167
168
  virtual void setParsingInlineAsm(bool V) = 0;
169
  virtual bool isParsingInlineAsm() = 0;
170
171
  /// Parse MS-style inline assembly.
172
  virtual bool parseMSInlineAsm(
173
      void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
174
      unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
175
      SmallVectorImpl<std::string> &Constraints,
176
      SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
177
      const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) = 0;
178
179
  /// Emit a note at the location \p L, with the message \p Msg.
180
  virtual void Note(SMLoc L, const Twine &Msg, SMRange Range = None) = 0;
181
182
  /// Emit a warning at the location \p L, with the message \p Msg.
183
  ///
184
  /// \return The return value is true, if warnings are fatal.
185
  virtual bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) = 0;
186
187
  /// Return an error at the location \p L, with the message \p Msg. This
188
  /// may be modified before being emitted.
189
  ///
190
  /// \return The return value is always true, as an idiomatic convenience to
191
  /// clients.
192
  bool Error(SMLoc L, const Twine &Msg, SMRange Range = None);
193
194
  /// Emit an error at the location \p L, with the message \p Msg.
195
  ///
196
  /// \return The return value is always true, as an idiomatic convenience to
197
  /// clients.
198
  virtual bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) = 0;
199
200
2.55M
  bool hasPendingError() { return !PendingErrors.empty(); }
201
202
100k
  bool printPendingErrors() {
203
100k
    bool rv = !PendingErrors.empty();
204
100k
    for (auto Err : PendingErrors) {
205
80.2k
      printError(Err.Loc, Twine(Err.Msg), Err.Range);
206
80.2k
    }
207
100k
    PendingErrors.clear();
208
100k
    return rv;
209
100k
  }
210
211
0
  void clearPendingErrors() { PendingErrors.clear(); }
212
213
  bool addErrorSuffix(const Twine &Suffix);
214
215
  /// Get the next AsmToken in the stream, possibly handling file
216
  /// inclusion first.
217
  virtual const AsmToken &Lex() = 0;
218
219
  /// Get the current AsmToken from the stream.
220
  const AsmToken &getTok() const;
221
222
  /// Report an error at the current lexer location.
223
  bool TokError(const Twine &Msg, SMRange Range = None);
224
225
  bool parseTokenLoc(SMLoc &Loc);
226
  bool parseToken(AsmToken::TokenKind T, const Twine &Msg = "unexpected token");
227
  /// Attempt to parse and consume token, returning true on
228
  /// success.
229
  bool parseOptionalToken(AsmToken::TokenKind T);
230
231
  bool parseEOL(const Twine &ErrMsg);
232
233
  bool parseMany(function_ref<bool()> parseOne, bool hasComma = true);
234
235
  bool parseIntToken(int64_t &V, const Twine &ErrMsg);
236
237
  bool check(bool P, const Twine &Msg);
238
  bool check(bool P, SMLoc Loc, const Twine &Msg);
239
240
  /// Parse an identifier or string (as a quoted identifier) and set \p
241
  /// Res to the identifier contents.
242
  virtual bool parseIdentifier(StringRef &Res) = 0;
243
244
  /// Parse up to the end of statement and return the contents from the
245
  /// current token until the end of the statement; the current token on exit
246
  /// will be either the EndOfStatement or EOF.
247
  virtual StringRef parseStringToEndOfStatement() = 0;
248
249
  /// Parse the current token as a string which may include escaped
250
  /// characters and return the string contents.
251
  virtual bool parseEscapedString(std::string &Data) = 0;
252
253
  /// Skip to the end of the current statement, for error recovery.
254
  virtual void eatToEndOfStatement() = 0;
255
256
  /// Parse an arbitrary expression.
257
  ///
258
  /// \param Res - The value of the expression. The result is undefined
259
  /// on error.
260
  /// \return - False on success.
261
  virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
262
  bool parseExpression(const MCExpr *&Res);
263
264
  /// Parse a primary expression.
265
  ///
266
  /// \param Res - The value of the expression. The result is undefined
267
  /// on error.
268
  /// \return - False on success.
269
  virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0;
270
271
  /// Parse an arbitrary expression, assuming that an initial '(' has
272
  /// already been consumed.
273
  ///
274
  /// \param Res - The value of the expression. The result is undefined
275
  /// on error.
276
  /// \return - False on success.
277
  virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
278
279
  /// Parse an expression which must evaluate to an absolute value.
280
  ///
281
  /// \param Res - The value of the absolute expression. The result is undefined
282
  /// on error.
283
  /// \return - False on success.
284
  virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
285
286
  /// Ensure that we have a valid section set in the streamer. Otherwise,
287
  /// report an error and switch to .text.
288
  /// \return - False on success.
289
  virtual bool checkForValidSection() = 0;
290
291
  /// Parse an arbitrary expression of a specified parenthesis depth,
292
  /// assuming that the initial '(' characters have already been consumed.
293
  ///
294
  /// \param ParenDepth - Specifies how many trailing expressions outside the
295
  /// current parentheses we have to parse.
296
  /// \param Res - The value of the expression. The result is undefined
297
  /// on error.
298
  /// \return - False on success.
299
  virtual bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
300
                                     SMLoc &EndLoc) = 0;
301
};
302
303
/// Create an MCAsmParser instance.
304
MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &,
305
                               const MCAsmInfo &, unsigned CB = 0);
306
307
} // end namespace llvm
308
309
#endif // LLVM_MC_MCPARSER_MCASMPARSER_H