MIR Parser: Simplify the handling of quoted tokens. NFC.
[oota-llvm.git] / lib / CodeGen / MIRParser / MILexer.h
1 //===- MILexer.h - Lexer for machine instructions -------------------------===//
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 // This file declares the function that lexes the machine instruction source
11 // string.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
16 #define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
17
18 #include "llvm/ADT/APSInt.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include <functional>
22
23 namespace llvm {
24
25 class Twine;
26
27 /// A token produced by the machine instruction lexer.
28 struct MIToken {
29   enum TokenKind {
30     // Markers
31     Eof,
32     Error,
33
34     // Tokens with no info.
35     comma,
36     equal,
37     underscore,
38     colon,
39     coloncolon,
40     exclaim,
41     lparen,
42     rparen,
43
44     // Keywords
45     kw_implicit,
46     kw_implicit_define,
47     kw_dead,
48     kw_killed,
49     kw_undef,
50     kw_frame_setup,
51     kw_debug_location,
52     kw_cfi_offset,
53     kw_cfi_def_cfa_register,
54     kw_cfi_def_cfa_offset,
55     kw_cfi_def_cfa,
56     kw_blockaddress,
57     kw_target_index,
58     kw_half,
59     kw_float,
60     kw_double,
61     kw_x86_fp80,
62     kw_fp128,
63     kw_ppc_fp128,
64     kw_volatile,
65
66     // Identifier tokens
67     Identifier,
68     NamedRegister,
69     MachineBasicBlock,
70     StackObject,
71     FixedStackObject,
72     NamedGlobalValue,
73     GlobalValue,
74     ExternalSymbol,
75
76     // Other tokens
77     IntegerLiteral,
78     FloatingPointLiteral,
79     VirtualRegister,
80     ConstantPoolItem,
81     JumpTableIndex,
82     NamedIRBlock,
83     IRBlock,
84     NamedIRValue,
85   };
86
87 private:
88   TokenKind Kind;
89   unsigned StringOffset;
90   bool HasStringValue;
91   StringRef Range;
92   std::string StringValue;
93   APSInt IntVal;
94
95 public:
96   MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
97       : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
98         Range(Range) {}
99
100   MIToken(TokenKind Kind, StringRef Range, std::string StringValue,
101           unsigned StringOffset = 0)
102       : Kind(Kind), StringOffset(StringOffset), HasStringValue(true),
103         Range(Range), StringValue(std::move(StringValue)) {}
104
105   MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
106           unsigned StringOffset = 0)
107       : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
108         Range(Range), IntVal(IntVal) {}
109
110   TokenKind kind() const { return Kind; }
111
112   bool isError() const { return Kind == Error; }
113
114   bool isRegister() const {
115     return Kind == NamedRegister || Kind == underscore ||
116            Kind == VirtualRegister;
117   }
118
119   bool isRegisterFlag() const {
120     return Kind == kw_implicit || Kind == kw_implicit_define ||
121            Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
122   }
123
124   bool isMemoryOperandFlag() const { return Kind == kw_volatile; }
125
126   bool is(TokenKind K) const { return Kind == K; }
127
128   bool isNot(TokenKind K) const { return Kind != K; }
129
130   StringRef::iterator location() const { return Range.begin(); }
131
132   /// Return the token's raw string value.
133   ///
134   /// If the string value is quoted, this method returns that quoted string as
135   /// it is, without unescaping the string value.
136   StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
137
138   /// Return the token's string value.
139   StringRef stringValue() const {
140     return HasStringValue ? StringRef(StringValue)
141                           : Range.drop_front(StringOffset);
142   }
143
144   const APSInt &integerValue() const { return IntVal; }
145
146   bool hasIntegerValue() const {
147     return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
148            Kind == StackObject || Kind == FixedStackObject ||
149            Kind == GlobalValue || Kind == VirtualRegister ||
150            Kind == ConstantPoolItem || Kind == JumpTableIndex ||
151            Kind == IRBlock;
152   }
153 };
154
155 /// Consume a single machine instruction token in the given source and return
156 /// the remaining source string.
157 StringRef lexMIToken(
158     StringRef Source, MIToken &Token,
159     function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
160
161 } // end namespace llvm
162
163 #endif