| 1 |
#include <stdlib.h> |
| 2 |
#include <string.h> |
| 3 |
#include <assert.h> |
| 4 |
#include <errno.h> |
| 5 |
|
| 6 |
#define _CFFI_INTERNAL |
| 7 |
#include "../cffi/parse_c_type.h" |
| 8 |
|
| 9 |
|
| 10 |
enum token_e { |
| 11 |
TOK_STAR='*', |
| 12 |
TOK_OPEN_PAREN='(', |
| 13 |
TOK_CLOSE_PAREN=')', |
| 14 |
TOK_OPEN_BRACKET='[', |
| 15 |
TOK_CLOSE_BRACKET=']', |
| 16 |
TOK_COMMA=',', |
| 17 |
|
| 18 |
TOK_START=256, |
| 19 |
TOK_END, |
| 20 |
TOK_ERROR, |
| 21 |
TOK_IDENTIFIER, |
| 22 |
TOK_INTEGER, |
| 23 |
TOK_DOTDOTDOT, |
| 24 |
|
| 25 |
/* keywords */ |
| 26 |
TOK__BOOL, |
| 27 |
TOK_CHAR, |
| 28 |
TOK__COMPLEX, |
| 29 |
TOK_CONST, |
| 30 |
TOK_DOUBLE, |
| 31 |
TOK_ENUM, |
| 32 |
TOK_FLOAT, |
| 33 |
//TOK__IMAGINARY, |
| 34 |
TOK_INT, |
| 35 |
TOK_LONG, |
| 36 |
TOK_SHORT, |
| 37 |
TOK_SIGNED, |
| 38 |
TOK_STRUCT, |
| 39 |
TOK_UNION, |
| 40 |
TOK_UNSIGNED, |
| 41 |
TOK_VOID, |
| 42 |
TOK_VOLATILE, |
| 43 |
|
| 44 |
TOK_CDECL, |
| 45 |
TOK_STDCALL, |
| 46 |
}; |
| 47 |
|
| 48 |
typedef struct { |
| 49 |
struct _cffi_parse_info_s *info; |
| 50 |
const char *input, *p; |
| 51 |
size_t size; // the next token is at 'p' and of length 'size' |
| 52 |
enum token_e kind; |
| 53 |
_cffi_opcode_t *output; |
| 54 |
size_t output_index; |
| 55 |
} token_t; |
| 56 |
|
| 57 |
static int is_space(char x) |
| 58 |
{ |
| 59 |
return (x == ' ' || x == '\f' || x == '\n' || x == '\r' || |
| 60 |
x == '\t' || x == '\v'); |
| 61 |
} |
| 62 |
|
| 63 |
static int is_ident_first(char x) |
| 64 |
{ |
| 65 |
return (('A' <= x && x <= 'Z') || ('a' <= x && x <= 'z') || x == '_' || |
| 66 |
x == '$'); /* '$' in names is supported here, for the struct |
| 67 |
names invented by cparser */ |
| 68 |
} |
| 69 |
|
| 70 |
static int is_digit(char x) |
| 71 |
{ |
| 72 |
return ('0' <= x && x <= '9'); |
| 73 |
} |
| 74 |
|
| 75 |
static int is_hex_digit(char x) |
| 76 |
{ |
| 77 |
return (('0' <= x && x <= '9') || |
| 78 |
('A' <= x && x <= 'F') || |
| 79 |
('a' <= x && x <= 'f')); |
| 80 |
} |
| 81 |
|
| 82 |
static int is_ident_next(char x) |
| 83 |
{ |
| 84 |
return (is_ident_first(x) || is_digit(x)); |
| 85 |
} |
| 86 |
|
| 87 |
static char get_following_char(token_t *tok) |
| 88 |
{ |
| 89 |
const char *p = tok->p + tok->size; |
| 90 |
if (tok->kind == TOK_ERROR) |
| 91 |
return 0; |
| 92 |
while (is_space(*p)) |
| 93 |
p++; |
| 94 |
return *p; |
| 95 |
} |
| 96 |
|
| 97 |
static int number_of_commas(token_t *tok) |
| 98 |
{ |
| 99 |
const char *p = tok->p; |
| 100 |
int result = 0; |
| 101 |
int nesting = 0; |
| 102 |
while (1) { |
| 103 |
switch (*p++) { |
| 104 |
case ',': result += !nesting; break; |
| 105 |
case '(': nesting++; break; |
| 106 |
case ')': if ((--nesting) < 0) return result; break; |
| 107 |
case 0: return result; |
| 108 |
default: break; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
static void next_token(token_t *tok) |
| 114 |
{ |
| 115 |
const char *p = tok->p + tok->size; |
| 116 |
if (tok->kind == TOK_ERROR) |
| 117 |
return; |
| 118 |
while (!is_ident_first(*p)) { |
| 119 |
if (is_space(*p)) { |
| 120 |
p++; |
| 121 |
} |
| 122 |
else if (is_digit(*p)) { |
| 123 |
tok->kind = TOK_INTEGER; |
| 124 |
tok->p = p; |
| 125 |
tok->size = 1; |
| 126 |
if (p[1] == 'x' || p[1] == 'X') |
| 127 |
tok->size = 2; |
| 128 |
while (is_hex_digit(p[tok->size])) |
| 129 |
tok->size++; |
| 130 |
return; |
| 131 |
} |
| 132 |
else if (p[0] == '.' && p[1] == '.' && p[2] == '.') { |
| 133 |
tok->kind = TOK_DOTDOTDOT; |
| 134 |
tok->p = p; |
| 135 |
tok->size = 3; |
| 136 |
return; |
| 137 |
} |
| 138 |
else if (*p) { |
| 139 |
tok->kind = *p; |
| 140 |
tok->p = p; |
| 141 |
tok->size = 1; |
| 142 |
return; |
| 143 |
} |
| 144 |
else { |
| 145 |
tok->kind = TOK_END; |
| 146 |
tok->p = p; |
| 147 |
tok->size = 0; |
| 148 |
return; |
| 149 |
} |
| 150 |
} |
| 151 |
tok->kind = TOK_IDENTIFIER; |
| 152 |
tok->p = p; |
| 153 |
tok->size = 1; |
| 154 |
while (is_ident_next(p[tok->size])) |
| 155 |
tok->size++; |
| 156 |
|
| 157 |
switch (*p) { |
| 158 |
case '_': |
| 159 |
if (tok->size == 5 && !memcmp(p, "_Bool", 5)) tok->kind = TOK__BOOL; |
| 160 |
if (tok->size == 7 && !memcmp(p,"__cdecl",7)) tok->kind = TOK_CDECL; |
| 161 |
if (tok->size == 9 && !memcmp(p,"__stdcall",9))tok->kind = TOK_STDCALL; |
| 162 |
if (tok->size == 8 && !memcmp(p,"_Complex",8)) tok->kind = TOK__COMPLEX; |
| 163 |
break; |
| 164 |
case 'c': |
| 165 |
if (tok->size == 4 && !memcmp(p, "char", 4)) tok->kind = TOK_CHAR; |
| 166 |
if (tok->size == 5 && !memcmp(p, "const", 5)) tok->kind = TOK_CONST; |
| 167 |
break; |
| 168 |
case 'd': |
| 169 |
if (tok->size == 6 && !memcmp(p, "double", 6)) tok->kind = TOK_DOUBLE; |
| 170 |
break; |
| 171 |
case 'e': |
| 172 |
if (tok->size == 4 && !memcmp(p, "enum", 4)) tok->kind = TOK_ENUM; |
| 173 |
break; |
| 174 |
case 'f': |
| 175 |
if (tok->size == 5 && !memcmp(p, "float", 5)) tok->kind = TOK_FLOAT; |
| 176 |
break; |
| 177 |
case 'i': |
| 178 |
if (tok->size == 3 && !memcmp(p, "int", 3)) tok->kind = TOK_INT; |
| 179 |
break; |
| 180 |
case 'l': |
| 181 |
if (tok->size == 4 && !memcmp(p, "long", 4)) tok->kind = TOK_LONG; |
| 182 |
break; |
| 183 |
case 's': |
| 184 |
if (tok->size == 5 && !memcmp(p, "short", 5)) tok->kind = TOK_SHORT; |
| 185 |
if (tok->size == 6 && !memcmp(p, "signed", 6)) tok->kind = TOK_SIGNED; |
| 186 |
if (tok->size == 6 && !memcmp(p, "struct", 6)) tok->kind = TOK_STRUCT; |
| 187 |
break; |
| 188 |
case 'u': |
| 189 |
if (tok->size == 5 && !memcmp(p, "union", 5)) tok->kind = TOK_UNION; |
| 190 |
if (tok->size == 8 && !memcmp(p,"unsigned",8)) tok->kind = TOK_UNSIGNED; |
| 191 |
break; |
| 192 |
case 'v': |
| 193 |
if (tok->size == 4 && !memcmp(p, "void", 4)) tok->kind = TOK_VOID; |
| 194 |
if (tok->size == 8 && !memcmp(p,"volatile",8)) tok->kind = TOK_VOLATILE; |
| 195 |
break; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
static int parse_error(token_t *tok, const char *msg) |
| 200 |
{ |
| 201 |
if (tok->kind != TOK_ERROR) { |
| 202 |
tok->kind = TOK_ERROR; |
| 203 |
tok->info->error_location = tok->p - tok->input; |
| 204 |
tok->info->error_message = msg; |
| 205 |
} |
| 206 |
return -1; |
| 207 |
} |
| 208 |
|
| 209 |
static int write_ds(token_t *tok, _cffi_opcode_t ds) |
| 210 |
{ |
| 211 |
size_t index = tok->output_index; |
| 212 |
if (index >= tok->info->output_size) { |
| 213 |
parse_error(tok, "internal type complexity limit reached"); |
| 214 |
return -1; |
| 215 |
} |
| 216 |
tok->output[index] = ds; |
| 217 |
tok->output_index = index + 1; |
| 218 |
return index; |
| 219 |
} |
| 220 |
|
| 221 |
#define MAX_SSIZE_T (((size_t)-1) >> 1) |
| 222 |
|
| 223 |
static int parse_complete(token_t *tok); |
| 224 |
static const char *get_common_type(const char *search, size_t search_len); |
| 225 |
static int parse_common_type_replacement(token_t *tok, const char *replacement); |
| 226 |
|
| 227 |
static int parse_sequel(token_t *tok, int outer) |
| 228 |
{ |
| 229 |
/* Emit opcodes for the "sequel", which is the optional part of a |
| 230 |
type declaration that follows the type name, i.e. everything |
| 231 |
with '*', '[ ]', '( )'. Returns the entry point index pointing |
| 232 |
the innermost opcode (the one that corresponds to the complete |
| 233 |
type). The 'outer' argument is the index of the opcode outside |
| 234 |
this "sequel". |
| 235 |
*/ |
| 236 |
int check_for_grouping, abi=0; |
| 237 |
_cffi_opcode_t result, *p_current; |
| 238 |
|
| 239 |
header: |
| 240 |
switch (tok->kind) { |
| 241 |
case TOK_STAR: |
| 242 |
outer = write_ds(tok, _CFFI_OP(_CFFI_OP_POINTER, outer)); |
| 243 |
next_token(tok); |
| 244 |
goto header; |
| 245 |
case TOK_CONST: |
| 246 |
/* ignored for now */ |
| 247 |
next_token(tok); |
| 248 |
goto header; |
| 249 |
case TOK_VOLATILE: |
| 250 |
/* ignored for now */ |
| 251 |
next_token(tok); |
| 252 |
goto header; |
| 253 |
case TOK_CDECL: |
| 254 |
case TOK_STDCALL: |
| 255 |
/* must be in a function; checked below */ |
| 256 |
abi = tok->kind; |
| 257 |
next_token(tok); |
| 258 |
goto header; |
| 259 |
default: |
| 260 |
break; |
| 261 |
} |
| 262 |
|
| 263 |
check_for_grouping = 1; |
| 264 |
if (tok->kind == TOK_IDENTIFIER) { |
| 265 |
next_token(tok); /* skip a potential variable name */ |
| 266 |
check_for_grouping = 0; |
| 267 |
} |
| 268 |
|
| 269 |
result = 0; |
| 270 |
p_current = &result; |
| 271 |
|
| 272 |
while (tok->kind == TOK_OPEN_PAREN) { |
| 273 |
next_token(tok); |
| 274 |
|
| 275 |
if (tok->kind == TOK_CDECL || tok->kind == TOK_STDCALL) { |
| 276 |
abi = tok->kind; |
| 277 |
next_token(tok); |
| 278 |
} |
| 279 |
|
| 280 |
if ((check_for_grouping--) == 1 && (tok->kind == TOK_STAR || |
| 281 |
tok->kind == TOK_CONST || |
| 282 |
tok->kind == TOK_VOLATILE || |
| 283 |
tok->kind == TOK_OPEN_BRACKET)) { |
| 284 |
/* just parentheses for grouping. Use a OP_NOOP to simplify */ |
| 285 |
int x; |
| 286 |
assert(p_current == &result); |
| 287 |
x = tok->output_index; |
| 288 |
p_current = tok->output + x; |
| 289 |
|
| 290 |
write_ds(tok, _CFFI_OP(_CFFI_OP_NOOP, 0)); |
| 291 |
|
| 292 |
x = parse_sequel(tok, x); |
| 293 |
result = _CFFI_OP(_CFFI_GETOP(0), x); |
| 294 |
} |
| 295 |
else { |
| 296 |
/* function type */ |
| 297 |
int arg_total, base_index, arg_next, flags=0; |
| 298 |
|
| 299 |
if (abi == TOK_STDCALL) { |
| 300 |
flags = 2; |
| 301 |
/* note that an ellipsis below will overwrite this flags, |
| 302 |
which is the goal: variadic functions are always cdecl */ |
| 303 |
} |
| 304 |
abi = 0; |
| 305 |
|
| 306 |
if (tok->kind == TOK_VOID && get_following_char(tok) == ')') { |
| 307 |
next_token(tok); |
| 308 |
} |
| 309 |
|
| 310 |
/* (over-)estimate 'arg_total'. May return 1 when it is really 0 */ |
| 311 |
arg_total = number_of_commas(tok) + 1; |
| 312 |
|
| 313 |
*p_current = _CFFI_OP(_CFFI_GETOP(*p_current), tok->output_index); |
| 314 |
p_current = tok->output + tok->output_index; |
| 315 |
|
| 316 |
base_index = write_ds(tok, _CFFI_OP(_CFFI_OP_FUNCTION, 0)); |
| 317 |
if (base_index < 0) |
| 318 |
return -1; |
| 319 |
/* reserve (arg_total + 1) slots for the arguments and the |
| 320 |
final FUNCTION_END */ |
| 321 |
for (arg_next = 0; arg_next <= arg_total; arg_next++) |
| 322 |
if (write_ds(tok, _CFFI_OP(0, 0)) < 0) |
| 323 |
return -1; |
| 324 |
|
| 325 |
arg_next = base_index + 1; |
| 326 |
|
| 327 |
if (tok->kind != TOK_CLOSE_PAREN) { |
| 328 |
while (1) { |
| 329 |
int arg; |
| 330 |
_cffi_opcode_t oarg; |
| 331 |
|
| 332 |
if (tok->kind == TOK_DOTDOTDOT) { |
| 333 |
flags = 1; /* ellipsis */ |
| 334 |
next_token(tok); |
| 335 |
break; |
| 336 |
} |
| 337 |
arg = parse_complete(tok); |
| 338 |
switch (_CFFI_GETOP(tok->output[arg])) { |
| 339 |
case _CFFI_OP_ARRAY: |
| 340 |
case _CFFI_OP_OPEN_ARRAY: |
| 341 |
arg = _CFFI_GETARG(tok->output[arg]); |
| 342 |
/* fall-through */ |
| 343 |
case _CFFI_OP_FUNCTION: |
| 344 |
oarg = _CFFI_OP(_CFFI_OP_POINTER, arg); |
| 345 |
break; |
| 346 |
default: |
| 347 |
oarg = _CFFI_OP(_CFFI_OP_NOOP, arg); |
| 348 |
break; |
| 349 |
} |
| 350 |
assert(arg_next - base_index <= arg_total); |
| 351 |
tok->output[arg_next++] = oarg; |
| 352 |
if (tok->kind != TOK_COMMA) |
| 353 |
break; |
| 354 |
next_token(tok); |
| 355 |
} |
| 356 |
} |
| 357 |
tok->output[arg_next] = _CFFI_OP(_CFFI_OP_FUNCTION_END, flags); |
| 358 |
} |
| 359 |
|
| 360 |
if (tok->kind != TOK_CLOSE_PAREN) |
| 361 |
return parse_error(tok, "expected ')'"); |
| 362 |
next_token(tok); |
| 363 |
} |
| 364 |
|
| 365 |
if (abi != 0) |
| 366 |
return parse_error(tok, "expected '('"); |
| 367 |
|
| 368 |
while (tok->kind == TOK_OPEN_BRACKET) { |
| 369 |
*p_current = _CFFI_OP(_CFFI_GETOP(*p_current), tok->output_index); |
| 370 |
p_current = tok->output + tok->output_index; |
| 371 |
|
| 372 |
next_token(tok); |
| 373 |
if (tok->kind != TOK_CLOSE_BRACKET) { |
| 374 |
size_t length; |
| 375 |
int gindex; |
| 376 |
char *endptr; |
| 377 |
|
| 378 |
switch (tok->kind) { |
| 379 |
|
| 380 |
case TOK_INTEGER: |
| 381 |
errno = 0; |
| 382 |
if (sizeof(length) > sizeof(unsigned long)) { |
| 383 |
#ifdef MS_WIN32 |
| 384 |
# ifdef _WIN64 |
| 385 |
length = _strtoui64(tok->p, &endptr, 0); |
| 386 |
# else |
| 387 |
abort(); /* unreachable */ |
| 388 |
# endif |
| 389 |
#else |
| 390 |
length = strtoull(tok->p, &endptr, 0); |
| 391 |
#endif |
| 392 |
} |
| 393 |
else |
| 394 |
length = strtoul(tok->p, &endptr, 0); |
| 395 |
if (endptr != tok->p + tok->size) |
| 396 |
return parse_error(tok, "invalid number"); |
| 397 |
if (errno == ERANGE || length > MAX_SSIZE_T) |
| 398 |
return parse_error(tok, "number too large"); |
| 399 |
break; |
| 400 |
|
| 401 |
case TOK_IDENTIFIER: |
| 402 |
gindex = search_in_globals(tok->info->ctx, tok->p, tok->size); |
| 403 |
if (gindex >= 0) { |
| 404 |
const struct _cffi_global_s *g; |
| 405 |
g = &tok->info->ctx->globals[gindex]; |
| 406 |
if (_CFFI_GETOP(g->type_op) == _CFFI_OP_CONSTANT_INT || |
| 407 |
_CFFI_GETOP(g->type_op) == _CFFI_OP_ENUM) { |
| 408 |
int neg; |
| 409 |
struct _cffi_getconst_s gc; |
| 410 |
gc.ctx = tok->info->ctx; |
| 411 |
gc.gindex = gindex; |
| 412 |
neg = ((int(*)(struct _cffi_getconst_s*))g->address) |
| 413 |
(&gc); |
| 414 |
if (neg == 0 && gc.value > MAX_SSIZE_T) |
| 415 |
return parse_error(tok, |
| 416 |
"integer constant too large"); |
| 417 |
if (neg == 0 || gc.value == 0) { |
| 418 |
length = (size_t)gc.value; |
| 419 |
break; |
| 420 |
} |
| 421 |
if (neg != 1) |
| 422 |
return parse_error(tok, "disagreement about" |
| 423 |
" this constant's value"); |
| 424 |
} |
| 425 |
} |
| 426 |
/* fall-through to the default case */ |
| 427 |
default: |
| 428 |
return parse_error(tok, "expected a positive integer constant"); |
| 429 |
} |
| 430 |
|
| 431 |
next_token(tok); |
| 432 |
|
| 433 |
write_ds(tok, _CFFI_OP(_CFFI_OP_ARRAY, 0)); |
| 434 |
write_ds(tok, (_cffi_opcode_t)length); |
| 435 |
} |
| 436 |
else |
| 437 |
write_ds(tok, _CFFI_OP(_CFFI_OP_OPEN_ARRAY, 0)); |
| 438 |
|
| 439 |
if (tok->kind != TOK_CLOSE_BRACKET) |
| 440 |
return parse_error(tok, "expected ']'"); |
| 441 |
next_token(tok); |
| 442 |
} |
| 443 |
|
| 444 |
*p_current = _CFFI_OP(_CFFI_GETOP(*p_current), outer); |
| 445 |
return _CFFI_GETARG(result); |
| 446 |
} |
| 447 |
|
| 448 |
static int search_sorted(const char *const *base, |
| 449 |
size_t item_size, int array_len, |
| 450 |
const char *search, size_t search_len) |
| 451 |
{ |
| 452 |
int left = 0, right = array_len; |
| 453 |
const char *baseptr = (const char *)base; |
| 454 |
|
| 455 |
while (left < right) { |
| 456 |
int middle = (left + right) / 2; |
| 457 |
const char *src = *(const char *const *)(baseptr + middle * item_size); |
| 458 |
int diff = strncmp(src, search, search_len); |
| 459 |
if (diff == 0 && src[search_len] == '\0') |
| 460 |
return middle; |
| 461 |
else if (diff >= 0) |
| 462 |
right = middle; |
| 463 |
else |
| 464 |
left = middle + 1; |
| 465 |
} |
| 466 |
return -1; |
| 467 |
} |
| 468 |
|
| 469 |
#define MAKE_SEARCH_FUNC(FIELD) \ |
| 470 |
static \ |
| 471 |
int search_in_##FIELD(const struct _cffi_type_context_s *ctx, \ |
| 472 |
const char *search, size_t search_len) \ |
| 473 |
{ \ |
| 474 |
return search_sorted(&ctx->FIELD->name, sizeof(*ctx->FIELD), \ |
| 475 |
ctx->num_##FIELD, search, search_len); \ |
| 476 |
} |
| 477 |
|
| 478 |
MAKE_SEARCH_FUNC(globals) |
| 479 |
MAKE_SEARCH_FUNC(struct_unions) |
| 480 |
MAKE_SEARCH_FUNC(typenames) |
| 481 |
MAKE_SEARCH_FUNC(enums) |
| 482 |
|
| 483 |
#undef MAKE_SEARCH_FUNC |
| 484 |
|
| 485 |
|
| 486 |
static |
| 487 |
int search_standard_typename(const char *p, size_t size) |
| 488 |
{ |
| 489 |
if (size < 6 || p[size-2] != '_' || p[size-1] != 't') |
| 490 |
return -1; |
| 491 |
|
| 492 |
switch (p[4]) { |
| 493 |
|
| 494 |
case '1': |
| 495 |
if (size == 8 && !memcmp(p, "uint16", 6)) return _CFFI_PRIM_UINT16; |
| 496 |
if (size == 8 && !memcmp(p, "char16", 6)) return _CFFI_PRIM_CHAR16; |
| 497 |
break; |
| 498 |
|
| 499 |
case '2': |
| 500 |
if (size == 7 && !memcmp(p, "int32", 5)) return _CFFI_PRIM_INT32; |
| 501 |
break; |
| 502 |
|
| 503 |
case '3': |
| 504 |
if (size == 8 && !memcmp(p, "uint32", 6)) return _CFFI_PRIM_UINT32; |
| 505 |
if (size == 8 && !memcmp(p, "char32", 6)) return _CFFI_PRIM_CHAR32; |
| 506 |
break; |
| 507 |
|
| 508 |
case '4': |
| 509 |
if (size == 7 && !memcmp(p, "int64", 5)) return _CFFI_PRIM_INT64; |
| 510 |
break; |
| 511 |
|
| 512 |
case '6': |
| 513 |
if (size == 8 && !memcmp(p, "uint64", 6)) return _CFFI_PRIM_UINT64; |
| 514 |
if (size == 7 && !memcmp(p, "int16", 5)) return _CFFI_PRIM_INT16; |
| 515 |
break; |
| 516 |
|
| 517 |
case '8': |
| 518 |
if (size == 7 && !memcmp(p, "uint8", 5)) return _CFFI_PRIM_UINT8; |
| 519 |
break; |
| 520 |
|
| 521 |
case 'a': |
| 522 |
if (size == 8 && !memcmp(p, "intmax", 6)) return _CFFI_PRIM_INTMAX; |
| 523 |
break; |
| 524 |
|
| 525 |
case 'e': |
| 526 |
if (size == 7 && !memcmp(p, "ssize", 5)) return _CFFI_PRIM_SSIZE; |
| 527 |
break; |
| 528 |
|
| 529 |
case 'f': |
| 530 |
if (size == 11 && !memcmp(p, "int_fast8", 9)) return _CFFI_PRIM_INT_FAST8; |
| 531 |
if (size == 12 && !memcmp(p, "int_fast16", 10)) return _CFFI_PRIM_INT_FAST16; |
| 532 |
if (size == 12 && !memcmp(p, "int_fast32", 10)) return _CFFI_PRIM_INT_FAST32; |
| 533 |
if (size == 12 && !memcmp(p, "int_fast64", 10)) return _CFFI_PRIM_INT_FAST64; |
| 534 |
break; |
| 535 |
|
| 536 |
case 'i': |
| 537 |
if (size == 9 && !memcmp(p, "ptrdiff", 7)) return _CFFI_PRIM_PTRDIFF; |
| 538 |
if (size == 21 && !memcmp(p, "_cffi_float_complex", 19)) return _CFFI_PRIM_FLOATCOMPLEX; |
| 539 |
if (size == 22 && !memcmp(p, "_cffi_double_complex", 20)) return _CFFI_PRIM_DOUBLECOMPLEX; |
| 540 |
break; |
| 541 |
|
| 542 |
case 'l': |
| 543 |
if (size == 12 && !memcmp(p, "int_least8", 10)) return _CFFI_PRIM_INT_LEAST8; |
| 544 |
if (size == 13 && !memcmp(p, "int_least16", 11)) return _CFFI_PRIM_INT_LEAST16; |
| 545 |
if (size == 13 && !memcmp(p, "int_least32", 11)) return _CFFI_PRIM_INT_LEAST32; |
| 546 |
if (size == 13 && !memcmp(p, "int_least64", 11)) return _CFFI_PRIM_INT_LEAST64; |
| 547 |
break; |
| 548 |
|
| 549 |
case 'm': |
| 550 |
if (size == 9 && !memcmp(p, "uintmax", 7)) return _CFFI_PRIM_UINTMAX; |
| 551 |
break; |
| 552 |
|
| 553 |
case 'p': |
| 554 |
if (size == 9 && !memcmp(p, "uintptr", 7)) return _CFFI_PRIM_UINTPTR; |
| 555 |
break; |
| 556 |
|
| 557 |
case 'r': |
| 558 |
if (size == 7 && !memcmp(p, "wchar", 5)) return _CFFI_PRIM_WCHAR; |
| 559 |
break; |
| 560 |
|
| 561 |
case 't': |
| 562 |
if (size == 8 && !memcmp(p, "intptr", 6)) return _CFFI_PRIM_INTPTR; |
| 563 |
break; |
| 564 |
|
| 565 |
case '_': |
| 566 |
if (size == 6 && !memcmp(p, "size", 4)) return _CFFI_PRIM_SIZE; |
| 567 |
if (size == 6 && !memcmp(p, "int8", 4)) return _CFFI_PRIM_INT8; |
| 568 |
if (size >= 12) { |
| 569 |
switch (p[10]) { |
| 570 |
case '1': |
| 571 |
if (size == 14 && !memcmp(p, "uint_least16", 12)) return _CFFI_PRIM_UINT_LEAST16; |
| 572 |
break; |
| 573 |
case '2': |
| 574 |
if (size == 13 && !memcmp(p, "uint_fast32", 11)) return _CFFI_PRIM_UINT_FAST32; |
| 575 |
break; |
| 576 |
case '3': |
| 577 |
if (size == 14 && !memcmp(p, "uint_least32", 12)) return _CFFI_PRIM_UINT_LEAST32; |
| 578 |
break; |
| 579 |
case '4': |
| 580 |
if (size == 13 && !memcmp(p, "uint_fast64", 11)) return _CFFI_PRIM_UINT_FAST64; |
| 581 |
break; |
| 582 |
case '6': |
| 583 |
if (size == 14 && !memcmp(p, "uint_least64", 12)) return _CFFI_PRIM_UINT_LEAST64; |
| 584 |
if (size == 13 && !memcmp(p, "uint_fast16", 11)) return _CFFI_PRIM_UINT_FAST16; |
| 585 |
break; |
| 586 |
case '8': |
| 587 |
if (size == 13 && !memcmp(p, "uint_least8", 11)) return _CFFI_PRIM_UINT_LEAST8; |
| 588 |
break; |
| 589 |
case '_': |
| 590 |
if (size == 12 && !memcmp(p, "uint_fast8", 10)) return _CFFI_PRIM_UINT_FAST8; |
| 591 |
break; |
| 592 |
default: |
| 593 |
break; |
| 594 |
} |
| 595 |
} |
| 596 |
break; |
| 597 |
|
| 598 |
default: |
| 599 |
break; |
| 600 |
} |
| 601 |
return -1; |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
static int parse_complete(token_t *tok) |
| 606 |
{ |
| 607 |
unsigned int t0; |
| 608 |
_cffi_opcode_t t1; |
| 609 |
_cffi_opcode_t t1complex; |
| 610 |
int modifiers_length, modifiers_sign; |
| 611 |
|
| 612 |
qualifiers: |
| 613 |
switch (tok->kind) { |
| 614 |
case TOK_CONST: |
| 615 |
/* ignored for now */ |
| 616 |
next_token(tok); |
| 617 |
goto qualifiers; |
| 618 |
case TOK_VOLATILE: |
| 619 |
/* ignored for now */ |
| 620 |
next_token(tok); |
| 621 |
goto qualifiers; |
| 622 |
default: |
| 623 |
; |
| 624 |
} |
| 625 |
|
| 626 |
modifiers_length = 0; |
| 627 |
modifiers_sign = 0; |
| 628 |
modifiers: |
| 629 |
switch (tok->kind) { |
| 630 |
|
| 631 |
case TOK_SHORT: |
| 632 |
if (modifiers_length != 0) |
| 633 |
return parse_error(tok, "'short' after another 'short' or 'long'"); |
| 634 |
modifiers_length--; |
| 635 |
next_token(tok); |
| 636 |
goto modifiers; |
| 637 |
|
| 638 |
case TOK_LONG: |
| 639 |
if (modifiers_length < 0) |
| 640 |
return parse_error(tok, "'long' after 'short'"); |
| 641 |
if (modifiers_length >= 2) |
| 642 |
return parse_error(tok, "'long long long' is too long"); |
| 643 |
modifiers_length++; |
| 644 |
next_token(tok); |
| 645 |
goto modifiers; |
| 646 |
|
| 647 |
case TOK_SIGNED: |
| 648 |
if (modifiers_sign) |
| 649 |
return parse_error(tok, "multiple 'signed' or 'unsigned'"); |
| 650 |
modifiers_sign++; |
| 651 |
next_token(tok); |
| 652 |
goto modifiers; |
| 653 |
|
| 654 |
case TOK_UNSIGNED: |
| 655 |
if (modifiers_sign) |
| 656 |
return parse_error(tok, "multiple 'signed' or 'unsigned'"); |
| 657 |
modifiers_sign--; |
| 658 |
next_token(tok); |
| 659 |
goto modifiers; |
| 660 |
|
| 661 |
default: |
| 662 |
break; |
| 663 |
} |
| 664 |
|
| 665 |
t1complex = 0; |
| 666 |
|
| 667 |
if (modifiers_length || modifiers_sign) { |
| 668 |
|
| 669 |
switch (tok->kind) { |
| 670 |
|
| 671 |
case TOK_VOID: |
| 672 |
case TOK__BOOL: |
| 673 |
case TOK_FLOAT: |
| 674 |
case TOK_STRUCT: |
| 675 |
case TOK_UNION: |
| 676 |
case TOK_ENUM: |
| 677 |
case TOK__COMPLEX: |
| 678 |
return parse_error(tok, "invalid combination of types"); |
| 679 |
|
| 680 |
case TOK_DOUBLE: |
| 681 |
if (modifiers_sign != 0 || modifiers_length != 1) |
| 682 |
return parse_error(tok, "invalid combination of types"); |
| 683 |
next_token(tok); |
| 684 |
t0 = _CFFI_PRIM_LONGDOUBLE; |
| 685 |
break; |
| 686 |
|
| 687 |
case TOK_CHAR: |
| 688 |
if (modifiers_length != 0) |
| 689 |
return parse_error(tok, "invalid combination of types"); |
| 690 |
modifiers_length = -2; |
| 691 |
/* fall-through */ |
| 692 |
case TOK_INT: |
| 693 |
next_token(tok); |
| 694 |
/* fall-through */ |
| 695 |
default: |
| 696 |
if (modifiers_sign >= 0) |
| 697 |
switch (modifiers_length) { |
| 698 |
case -2: t0 = _CFFI_PRIM_SCHAR; break; |
| 699 |
case -1: t0 = _CFFI_PRIM_SHORT; break; |
| 700 |
case 1: t0 = _CFFI_PRIM_LONG; break; |
| 701 |
case 2: t0 = _CFFI_PRIM_LONGLONG; break; |
| 702 |
default: t0 = _CFFI_PRIM_INT; break; |
| 703 |
} |
| 704 |
else |
| 705 |
switch (modifiers_length) { |
| 706 |
case -2: t0 = _CFFI_PRIM_UCHAR; break; |
| 707 |
case -1: t0 = _CFFI_PRIM_USHORT; break; |
| 708 |
case 1: t0 = _CFFI_PRIM_ULONG; break; |
| 709 |
case 2: t0 = _CFFI_PRIM_ULONGLONG; break; |
| 710 |
default: t0 = _CFFI_PRIM_UINT; break; |
| 711 |
} |
| 712 |
} |
| 713 |
t1 = _CFFI_OP(_CFFI_OP_PRIMITIVE, t0); |
| 714 |
} |
| 715 |
else { |
| 716 |
switch (tok->kind) { |
| 717 |
case TOK_INT: |
| 718 |
t1 = _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_INT); |
| 719 |
break; |
| 720 |
case TOK_CHAR: |
| 721 |
t1 = _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_CHAR); |
| 722 |
break; |
| 723 |
case TOK_VOID: |
| 724 |
t1 = _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_VOID); |
| 725 |
break; |
| 726 |
case TOK__BOOL: |
| 727 |
t1 = _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_BOOL); |
| 728 |
break; |
| 729 |
case TOK_FLOAT: |
| 730 |
t1 = _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_FLOAT); |
| 731 |
t1complex = _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_FLOATCOMPLEX); |
| 732 |
break; |
| 733 |
case TOK_DOUBLE: |
| 734 |
t1 = _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_DOUBLE); |
| 735 |
t1complex = _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_DOUBLECOMPLEX); |
| 736 |
break; |
| 737 |
case TOK_IDENTIFIER: |
| 738 |
{ |
| 739 |
const char *replacement; |
| 740 |
int n = search_in_typenames(tok->info->ctx, tok->p, tok->size); |
| 741 |
if (n >= 0) { |
| 742 |
t1 = _CFFI_OP(_CFFI_OP_TYPENAME, n); |
| 743 |
break; |
| 744 |
} |
| 745 |
n = search_standard_typename(tok->p, tok->size); |
| 746 |
if (n >= 0) { |
| 747 |
t1 = _CFFI_OP(_CFFI_OP_PRIMITIVE, n); |
| 748 |
break; |
| 749 |
} |
| 750 |
replacement = get_common_type(tok->p, tok->size); |
| 751 |
if (replacement != NULL) { |
| 752 |
n = parse_common_type_replacement(tok, replacement); |
| 753 |
if (n < 0) |
| 754 |
return parse_error(tok, "internal error, please report!"); |
| 755 |
t1 = _CFFI_OP(_CFFI_OP_NOOP, n); |
| 756 |
break; |
| 757 |
} |
| 758 |
return parse_error(tok, "undefined type name"); |
| 759 |
} |
| 760 |
case TOK_STRUCT: |
| 761 |
case TOK_UNION: |
| 762 |
{ |
| 763 |
int n, kind = tok->kind; |
| 764 |
next_token(tok); |
| 765 |
if (tok->kind != TOK_IDENTIFIER) |
| 766 |
return parse_error(tok, "struct or union name expected"); |
| 767 |
|
| 768 |
n = search_in_struct_unions(tok->info->ctx, tok->p, tok->size); |
| 769 |
if (n < 0) { |
| 770 |
if (kind == TOK_STRUCT && tok->size == 8 && |
| 771 |
!memcmp(tok->p, "_IO_FILE", 8)) |
| 772 |
n = _CFFI__IO_FILE_STRUCT; |
| 773 |
else |
| 774 |
return parse_error(tok, "undefined struct/union name"); |
| 775 |
} |
| 776 |
else if (((tok->info->ctx->struct_unions[n].flags & _CFFI_F_UNION) |
| 777 |
!= 0) ^ (kind == TOK_UNION)) |
| 778 |
return parse_error(tok, "wrong kind of tag: struct vs union"); |
| 779 |
|
| 780 |
t1 = _CFFI_OP(_CFFI_OP_STRUCT_UNION, n); |
| 781 |
break; |
| 782 |
} |
| 783 |
case TOK_ENUM: |
| 784 |
{ |
| 785 |
int n; |
| 786 |
next_token(tok); |
| 787 |
if (tok->kind != TOK_IDENTIFIER) |
| 788 |
return parse_error(tok, "enum name expected"); |
| 789 |
|
| 790 |
n = search_in_enums(tok->info->ctx, tok->p, tok->size); |
| 791 |
if (n < 0) |
| 792 |
return parse_error(tok, "undefined enum name"); |
| 793 |
|
| 794 |
t1 = _CFFI_OP(_CFFI_OP_ENUM, n); |
| 795 |
break; |
| 796 |
} |
| 797 |
default: |
| 798 |
return parse_error(tok, "identifier expected"); |
| 799 |
} |
| 800 |
next_token(tok); |
| 801 |
} |
| 802 |
if (tok->kind == TOK__COMPLEX) |
| 803 |
{ |
| 804 |
if (t1complex == 0) |
| 805 |
return parse_error(tok, "_Complex type combination unsupported"); |
| 806 |
t1 = t1complex; |
| 807 |
next_token(tok); |
| 808 |
} |
| 809 |
|
| 810 |
return parse_sequel(tok, write_ds(tok, t1)); |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
static |
| 815 |
int parse_c_type_from(struct _cffi_parse_info_s *info, size_t *output_index, |
| 816 |
const char *input) |
| 817 |
{ |
| 818 |
int result; |
| 819 |
token_t token; |
| 820 |
|
| 821 |
token.info = info; |
| 822 |
token.kind = TOK_START; |
| 823 |
token.input = input; |
| 824 |
token.p = input; |
| 825 |
token.size = 0; |
| 826 |
token.output = info->output; |
| 827 |
token.output_index = *output_index; |
| 828 |
|
| 829 |
next_token(&token); |
| 830 |
result = parse_complete(&token); |
| 831 |
|
| 832 |
*output_index = token.output_index; |
| 833 |
if (token.kind != TOK_END) |
| 834 |
return parse_error(&token, "unexpected symbol"); |
| 835 |
return result; |
| 836 |
} |
| 837 |
|
| 838 |
static |
| 839 |
int parse_c_type(struct _cffi_parse_info_s *info, const char *input) |
| 840 |
{ |
| 841 |
size_t output_index = 0; |
| 842 |
return parse_c_type_from(info, &output_index, input); |
| 843 |
} |
| 844 |
|
| 845 |
static |
| 846 |
int parse_common_type_replacement(token_t *tok, const char *replacement) |
| 847 |
{ |
| 848 |
return parse_c_type_from(tok->info, &tok->output_index, replacement); |
| 849 |
} |