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-2025 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
228
229idaman error_t ida_export lex_define_macro(
230 lexer_t *lx,
231 const char *macro,
232 const char *body,
233 int nargs=0,
234 bool isfunc=false);
235
239
240idaman void ida_export lex_undefine_macro(
241 lexer_t *lx,
242 const char *macro);
243
248
249idaman int ida_export lex_set_options(lexer_t *lx, int options);
250
255#define LXOPT_PARSE_FLOATS 0x0001
256#define LXOPT_REQ_SEPARATOR 0x0002
257#define LXOPT_NOCASE_FILES 0x0004
258#define LXOPT_C99_CONSTANTS 0x0008
261#define LXOPT_STR_INCLUDE_BACKSLASHES 0x0010
263
264
269
270idaman error_t ida_export lex_get_token(lexer_t *lx, token_t *t, int32 *p_lnnum=nullptr);
271
272
275
276idaman int ida_export lex_enum_macros(
277 const lexer_t *lx,
278 int idaapi cb(const char *name, const char *body, int nargs, bool isfunc, void *ud),
279 void *ud=nullptr);
280
281
283
284idaman const char *ida_export lex_print_token(qstring *buf, const token_t *t);
285
286
287//-------------------------------------------------------------------------
290
293
294idaman error_t ida_export lex_init_string(
295 lexer_t *lx,
296 const char *line,
297 void *macros=nullptr);
299
300//-------------------------------------------------------------------------
303
306
307idaman error_t ida_export lex_init_file(lexer_t *lx, const char *file);
308
309
313
314idaman const char *ida_export lex_get_file_line(
315 lexer_t *lx,
316 int32 *linenum,
317 const char **lineptr,
318 int level=0);
319
320
322
323idaman void ida_export lex_term_file(lexer_t *lx, bool del_macros);
325
326//-------------------------------------------------------------------------
330
331
336
337inline bool get_token(token_t *t, lexer_t *lx, tokenstack_t &buf)
338{
339 if ( !buf.empty() )
340 *t = buf.pop();
341 else if ( lex_get_token(lx, t) != eOk )
342 return false;
343 return true;
344}
345
346
348
349inline void unget_token(const token_t &t, tokenstack_t &buf)
350{
351 buf.push(t);
352}
353
354
355
357
358idaman bool ida_export is_c_keyword(const char *name);
359
360
361
362#endif // LEX_HPP
Reimplementation of stack class from STL.
Definition pro.h:2774
T pop(void)
Definition pro.h:2777
void push(const T &v)
Definition pro.h:2788
bool empty(void) const
Does the qvector have 0 elements?
Definition pro.h:2424
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:74
IEEE floating point functions.
asize_t size
Definition kernwin.hpp:6339
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:337
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:349
DECLARE_TYPE_AS_MOVABLE(token_t)
qstack< token_t > tokenstack_t
see get_token(), unget_token()
Definition lex.hpp:329
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
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:602
unsigned int uint32
unsigned 32 bit value
Definition pro.h:348
adiff_t sval_t
signed value used by the processor.
Definition pro.h:446
int int32
signed 32 bit value
Definition pro.h:347
int error_t
Error code (errno)
Definition pro.h:458
unsigned short ushort
unsigned 16 bit value
Definition pro.h:338
_qstring< char > qstring
regular string
Definition pro.h:3694
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
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