IDA C++ SDK 9.2
Loading...
Searching...
No Matches
lex.hpp
Go to the documentation of this file.
1/*
2 * Interactive disassembler (IDA).
3 * Copyright (c) 1990-2026 Hex-Rays
4 * ALL RIGHTS RESERVED.
5 *
6 */
7
8#ifndef LEX_HPP
9#define LEX_HPP
10
11#include <ieee.h>
12
20
21typedef ushort lxtype;
22typedef uint32 callcnv_t;
23
28const lxtype
29 lx_end = 1,
33 lx_char = 5,
38 lx_key = 128;
44
45
47struct token_t
48{
51 sval_t num = 0;
52 union
53 {
54 bool unicode = false;
56 };
57 union
58 {
62 };
63 token_t() : fnum() {}
64};
66
67class lexer_t; // lexical analyzer, opaque structure
68
69
72
73typedef error_t lx_resolver_t(lexer_t *lx, void *ud, token_t *curtok, sval_t *res);
74
75// Conversion from 'type1 ' to 'type_2' is sign-extended. This may cause unexpected runtime behavior.
76// We want this sign-extension to happen, since it comes mostly from HANDLEs.
77// (see https://msdn.microsoft.com/en-us/library/ms235307.aspx )
79
80
81struct cast_t
82{
83 bool is_unsigned;
84 int size;
85
86 cast_t()
87 {
88 reset();
89 }
90 void reset(void)
91 {
92 is_unsigned = false;
93 size = 0;
94 }
95};
96
98{
100 union
101 {
104 };
105
107 {
108 reset();
109 }
110 void set(const lex_value_t &v)
111 {
112 set_val(v.val, v.is_unsigned);
113 }
114 void reset(void)
115 {
116 set_val(0, true);
117 }
118 void set_val(int64 v, bool _is_unsigned)
119 {
120 is_unsigned = _is_unsigned;
121 val = v;
122 }
123
124 uint64 get_uval(void) const
125 {
126 return val;
127 }
128 int64 get_val(void) const
129 {
130 return val;
131 }
132
133 bool is_zero(void) const
134 {
135 return get_val() == 0;
136 }
137
138 void perform_cast(const cast_t &cast);
139
140 void unary_minus(const lex_value_t &v);
141 void unary_plus(const lex_value_t &v);
142 void unary_not(const lex_value_t &v);
143 void bitwise_not(const lex_value_t &v);
144
145 void mul(const lex_value_t &v);
146 void div(const lex_value_t &v);
147 void mod(const lex_value_t &v);
148 void add(const lex_value_t &v);
149 void sub(const lex_value_t &v);
150
151 void shift_right(const lex_value_t &v);
152 void shift_left(const lex_value_t &v);
153 void bitwise_and(const lex_value_t &v);
154 void bitwise_xor(const lex_value_t &v);
155 void bitwise_or(const lex_value_t &v);
156 void logical_and(const lex_value_t &v);
157 void logical_or(const lex_value_t &v);
158
159 void cmpge(const lex_value_t &v);
160 void cmple(const lex_value_t &v);
161 void cmplt(const lex_value_t &v);
162 void cmpgt(const lex_value_t &v);
163 void cmpeq(const lex_value_t &v);
164 void cmpneq(const lex_value_t &v);
165};
166
168
169
171
172typedef error_t lx_parse_cast_t(lexer_t *lx, cast_t *cast, token_t *ct);
173
177
178typedef int idaapi lx_preprocessor_cb(void *ud, const char *fname, int nl, const char *line);
179
180
183
184typedef int idaapi lx_pragma_cb(void *ud, const char *line);
185
186
189
190typedef int idaapi lx_warning_cb(void *ud, const char *line);
191
192
195
196typedef int idaapi lx_macro_cb(
197 void *ud,
198 const char *name,
199 const char *body,
200 int nargs,
201 bool isfunc,
202 bool is_new_macro);
203
204
207
208typedef int idaapi lx_undef_cb(void *ud, const char *name);
209
210#define LEX_MACRO_SYSTEM 1
211
214
215idaman lexer_t *ida_export create_lexer(
216 const char *const *keys,
217 size_t size,
218 void *ud=nullptr,
219 uint32 macro_flags=0);
220
221
223
224idaman void ida_export destroy_lexer(lexer_t *lx);
225
226
229{
230 void operator()(lexer_t *lx) { destroy_lexer(lx); }
231};
232using lexer_janitor_t = std::unique_ptr<lexer_t, lexer_deleter_t>;
233
234
236
237idaman error_t ida_export lex_define_macro(
238 lexer_t *lx,
239 const char *macro,
240 const char *body,
241 int nargs=0,
242 bool isfunc=false);
243
247
248idaman void ida_export lex_undefine_macro(
249 lexer_t *lx,
250 const char *macro);
251
256
257idaman int ida_export lex_set_options(lexer_t *lx, int options);
258
263#define LXOPT_PARSE_FLOATS 0x0001
264#define LXOPT_REQ_SEPARATOR 0x0002
265#define LXOPT_NOCASE_FILES 0x0004
266#define LXOPT_C99_CONSTANTS 0x0008
269#define LXOPT_STR_INCLUDE_BACKSLASHES 0x0010
271
272
277
278idaman error_t ida_export lex_get_token(lexer_t *lx, token_t *t, int32 *p_lnnum=nullptr);
279
280
283
284idaman int ida_export lex_enum_macros(
285 const lexer_t *lx,
286 int idaapi cb(const char *name, const char *body, int nargs, bool isfunc, void *ud),
287 void *ud=nullptr);
288
289
291
292idaman const char *ida_export lex_print_token(qstring *buf, const token_t *t);
293
294
295//-------------------------------------------------------------------------
298
301
302idaman error_t ida_export lex_init_string(
303 lexer_t *lx,
304 const char *line,
305 void *macros=nullptr);
307
308//-------------------------------------------------------------------------
311
314
315idaman error_t ida_export lex_init_file(lexer_t *lx, const char *file);
316
317
321
322idaman const char *ida_export lex_get_file_line(
323 lexer_t *lx,
324 int32 *linenum,
325 const char **lineptr,
326 int level=0);
327
328
330
331idaman void ida_export lex_term_file(lexer_t *lx, bool del_macros);
333
334//-------------------------------------------------------------------------
338
339
344
345inline bool get_token(token_t *t, lexer_t *lx, tokenstack_t &buf)
346{
347 if ( !buf.empty() )
348 *t = buf.pop();
349 else if ( lex_get_token(lx, t) != eOk )
350 return false;
351 return true;
352}
353
354
356
357inline void unget_token(const token_t &t, tokenstack_t &buf)
358{
359 buf.push(t);
360}
361
362
363
365
366idaman bool ida_export is_c_keyword(const char *name);
367
368
369
370#endif // LEX_HPP
Reimplementation of stack class from STL.
Definition pro.h:2848
T pop(void)
Definition pro.h:2851
void push(const T &v)
Definition pro.h:2862
bool empty(void) const
Does the qvector have 0 elements?
Definition pro.h:2495
const lxtype lx_ident
ident
Definition lex.hpp:30
const lxtype lx_end
no more tokens
Definition lex.hpp:29
const lxtype lx_int64
int64 constant
Definition lex.hpp:36
const lxtype lx_number
long constant
Definition lex.hpp:31
const lxtype lx_string
string constant (token_t.chr != 0 => unicode string)
Definition lex.hpp:32
const lxtype lx_typename
user-defined type
Definition lex.hpp:34
const lxtype lx_key
keywords start.
Definition lex.hpp:38
const lxtype lx_callcnv
custom calling convention
Definition lex.hpp:37
const lxtype lx_char
char constant
Definition lex.hpp:33
const lxtype lx_float
IEEE floating point constant.
Definition lex.hpp:35
uint32 callcnv_t
Definition ida.hpp:75
IEEE floating point functions.
asize_t size
Definition kernwin.hpp:6701
idaman void ida_export lex_undefine_macro(lexer_t *lx, const char *macro)
Undefine a macro.
MSC_DIAG_ON(4826) typedef error_t lx_parse_cast_t(lexer_t *lx
Preprocessor callbacks for casts.
int idaapi lx_undef_cb(void *ud, const char *name)
Callback for undef directives.
Definition lex.hpp:208
cast_t token_t * ct
Definition lex.hpp:172
idaman const char *ida_export lex_print_token(qstring *buf, const token_t *t)
Debug: get text representation of token.
idaman void ida_export lex_term_file(lexer_t *lx, bool del_macros)
Termination: also see lex_init_file()
idaman bool ida_export is_c_keyword(const char *name)
is NAME a valid C keyword (exclude 'this')
idaman const char *ida_export lex_get_file_line(lexer_t *lx, int32 *linenum, const char **lineptr, int level=0)
Error handling.
idaman error_t ida_export lex_init_file(lexer_t *lx, const char *file)
Initialization: file may be nullptr.
error_t lx_resolver_t(lexer_t *lx, void *ud, token_t *curtok, sval_t *res)
Preprocessor callback for unknown tokens.
Definition lex.hpp:73
idaman void ida_export destroy_lexer(lexer_t *lx)
Destroy a lexical analyzer.
idaman error_t ida_export lex_define_macro(lexer_t *lx, const char *macro, const char *body, int nargs=0, bool isfunc=false)
Define a macro.
ushort lxtype
see Parser token types
Definition lex.hpp:21
int idaapi lx_macro_cb(void *ud, const char *name, const char *body, int nargs, bool isfunc, bool is_new_macro)
Callback for define directives.
Definition lex.hpp:196
int idaapi lx_warning_cb(void *ud, const char *line)
Callback for warning directives.
Definition lex.hpp:190
bool get_token(token_t *t, lexer_t *lx, tokenstack_t &buf)
Retrieve token from a stack or lexer.
Definition lex.hpp:345
idaman int ida_export lex_enum_macros(const lexer_t *lx, int idaapi cb(const char *name, const char *body, int nargs, bool isfunc, void *ud), void *ud=nullptr)
Enumerate all macros.
void unget_token(const token_t &t, tokenstack_t &buf)
Push a token back onto the token stack.
Definition lex.hpp:357
DECLARE_TYPE_AS_MOVABLE(token_t)
qstack< token_t > tokenstack_t
see get_token(), unget_token()
Definition lex.hpp:337
int idaapi lx_pragma_cb(void *ud, const char *line)
Callback for pragma directives.
Definition lex.hpp:184
idaman int ida_export lex_set_options(lexer_t *lx, int options)
Set lexer options.
idaman error_t ida_export lex_init_string(lexer_t *lx, const char *line, void *macros=nullptr)
Set the input line and the macro table.
idaman error_t ida_export lex_get_token(lexer_t *lx, token_t *t, int32 *p_lnnum=nullptr)
Get next token.
MSC_DIAG_OFF(4826) struct cast_t
Preprocessor cast.
Definition lex.hpp:78
std::unique_ptr< lexer_t, lexer_deleter_t > lexer_janitor_t
Definition lex.hpp:232
cast_t * cast
Definition lex.hpp:172
idaman lexer_t *ida_export create_lexer(const char *const *keys, size_t size, void *ud=nullptr, uint32 macro_flags=0)
Create new lexical analyzer and set its keyword table.
int idaapi lx_preprocessor_cb(void *ud, const char *fname, int nl, const char *line)
Preprocessor callback.
Definition lex.hpp:178
unsigned __int64 uint64
Definition llong.hpp:13
__int64 int64
Definition llong.hpp:14
idaman size_t const char time_t t
Definition pro.h:606
unsigned int uint32
unsigned 32 bit value
Definition pro.h:352
adiff_t sval_t
signed value used by the processor.
Definition pro.h:450
int int32
signed 32 bit value
Definition pro.h:351
int error_t
Error code (errno)
Definition pro.h:462
unsigned short ushort
unsigned 16 bit value
Definition pro.h:342
_qstring< char > qstring
regular string
Definition pro.h:3771
Processor-independent representation of a floating point value.
Definition ieee.h:98
void reset(void)
Definition lex.hpp:114
int64 val
Definition lex.hpp:102
void bitwise_or(const lex_value_t &v)
void bitwise_and(const lex_value_t &v)
void cmple(const lex_value_t &v)
int64 get_val(void) const
Definition lex.hpp:128
void mod(const lex_value_t &v)
uint64 get_uval(void) const
Definition lex.hpp:124
void cmpge(const lex_value_t &v)
bool is_unsigned
Definition lex.hpp:99
void cmpgt(const lex_value_t &v)
void shift_left(const lex_value_t &v)
void bitwise_xor(const lex_value_t &v)
void logical_or(const lex_value_t &v)
void set_val(int64 v, bool _is_unsigned)
Definition lex.hpp:118
void cmplt(const lex_value_t &v)
void perform_cast(const cast_t &cast)
void set(const lex_value_t &v)
Definition lex.hpp:110
void logical_and(const lex_value_t &v)
void mul(const lex_value_t &v)
void cmpneq(const lex_value_t &v)
lex_value_t()
Definition lex.hpp:106
void bitwise_not(const lex_value_t &v)
void unary_plus(const lex_value_t &v)
void shift_right(const lex_value_t &v)
void unary_not(const lex_value_t &v)
bool is_zero(void) const
Definition lex.hpp:133
void cmpeq(const lex_value_t &v)
void add(const lex_value_t &v)
void unary_minus(const lex_value_t &v)
void div(const lex_value_t &v)
void sub(const lex_value_t &v)
uint64 uval
Definition lex.hpp:103
janitor for a lexical analyzer
Definition lex.hpp:229
void operator()(lexer_t *lx)
Definition lex.hpp:230
Parser token.
Definition lex.hpp:48
bool unicode
(lx_string: != 0 => unicode string)
Definition lex.hpp:54
callcnv_t callcnv
lx_callcnv
Definition lex.hpp:61
bool is_unsigned
(lx_number, lx_int64: != 0 => unsigned value)
Definition lex.hpp:55
int64 i64
lx_int64
Definition lex.hpp:60
sval_t num
long & char constants
Definition lex.hpp:51
lxtype type
see Parser token types
Definition lex.hpp:50
fpvalue_t fnum
floating point constant
Definition lex.hpp:59
token_t()
Definition lex.hpp:63
qstring str
idents & strings
Definition lex.hpp:49