15 #define TS_START_END(node, start, end) \
17 start = ts_node_start_byte(node); \
18 end = ts_node_end_byte(node); \
64 return !strcmp(declarator,
"abstract_pointer_declarator") ||
65 !strcmp(declarator,
"abstract_array_declarator") ||
66 !strcmp(declarator,
"abstract_function_declarator");
70 return !strcmp(declarator,
"pointer_declarator") ||
71 !strcmp(declarator,
"array_declarator") ||
72 !strcmp(declarator,
"function_declarator") ||
73 !strcmp(declarator,
"identifier") ||
74 !strcmp(declarator,
"field_identifier");
78 return !strcmp(declarator,
"parenthesized_declarator") ||
79 !strcmp(declarator,
"identifier");
88 parser_debug(
state,
"parse_primitive_type(): %s\n", is_const ?
"const" :
"not const");
108 parser_error(
state,
"Error forming RzType and RzBaseType pair out of primitive type\n");
124 if (strcmp(
ts_node_type(node),
"sized_type_specifier")) {
144 parser_error(
state,
"Error forming RzType and RzBaseType pair out of primitive type\n");
179 parser_debug(
state,
"Already has forward definition of type: \"%s\"\n", real_type);
182 parser_error(
state,
"Error forming naked RzType pair out of simple forward-looking type: \"%s\"\n", real_type);
205 parser_error(
state,
"Error forming RzType and RzBaseType pair out of simple forward-looking type\n");
213 (*tpair)->btype =
NULL;
241 bool is_const =
false;
247 if (!strcmp(leaf_type,
"type_qualifier")) {
250 if (!strcmp(qualifier,
"const")) {
283 const char *declarator_type =
ts_node_type(parameter_declarator);
284 if (!declarator_type) {
314 if (struct_node_child_count < 1 || struct_node_child_count > 2) {
383 char *real_type =
NULL;
384 char *real_identifier =
NULL;
386 for (
i = 0;
i < body_child_count;
i++) {
392 if (!strcmp(node_type,
"comment")) {
398 bool is_const =
false;
409 if (!strcmp(leaf_type,
"type_qualifier")) {
412 if (!strcmp(qualifier,
"const")) {
419 if (strcmp(node_type,
"field_declaration")) {
420 parser_error(
state,
"ERROR: Struct field AST should contain (field_declaration) node!\n");
430 parser_error(
state,
"ERROR: Struct field AST shoudl contain type and declarator items");
440 if (
state->verbose) {
443 if (fieldtext && nodeast) {
458 if (strcmp(bfnode_type,
"primitive_type") && strcmp(bfnode_type,
"type_identifier")) {
459 parser_error(
state,
"ERROR: Struct bitfield cannot contain non-primitive bitfield!\n");
472 free(real_identifier);
474 if (!real_identifier) {
487 parser_error(
state,
"ERROR: Struct bitfield bits AST node should not be NULL!\n");
494 parser_debug(
state,
"field type: %s field_identifier: %s bits: %d\n", real_type, real_identifier,
bits);
503 char *membname =
NULL;
514 .type = membtpair->
type,
520 parser_error(
state,
"Error appending bitfield struct member to the base type\n");
536 free(real_identifier);
538 if (!real_identifier) {
544 parser_debug(
state,
"field type: %s field_declarator: %s\n", real_type, real_identifier);
554 char *membname =
NULL;
565 .type = membtpair->
type,
586 *tpair = struct_pair;
590 free(real_identifier);
606 if (union_node_child_count < 1 || union_node_child_count > 2) {
674 char *real_type =
NULL;
675 char *real_identifier =
NULL;
677 for (
i = 0;
i < body_child_count;
i++) {
683 if (!strcmp(node_type,
"comment")) {
689 bool is_const =
false;
700 if (!strcmp(leaf_type,
"type_qualifier")) {
703 if (!strcmp(qualifier,
"const")) {
710 if (strcmp(node_type,
"field_declaration")) {
711 parser_error(
state,
"ERROR: Union field AST should contain (field_declaration) node!\n");
721 parser_error(
state,
"ERROR: Union field AST shoudl contain type and declarator items");
730 if (
state->verbose) {
733 if (fieldtext && nodeast) {
748 if (strcmp(bfnode_type,
"primitive_type") && strcmp(bfnode_type,
"type_identifier")) {
749 parser_error(
state,
"ERROR: Union bitfield cannot contain non-primitive bitfield!\n");
762 free(real_identifier);
764 if (!real_identifier) {
777 parser_error(
state,
"ERROR: Union bitfield bits AST node should not be NULL!\n");
784 parser_debug(
state,
"field type: %s field_identifier: %s bits: %d\n", real_type, real_identifier,
bits);
793 char *membname =
NULL;
804 .type = membtpair->
type,
826 free(real_identifier);
828 if (!real_identifier) {
834 parser_debug(
state,
"field type: %s field_declarator: %s\n", real_type, real_identifier);
844 char *membname =
NULL;
855 .type = membtpair->
type,
878 free(real_identifier);
893 if (enum_node_child_count < 1 || enum_node_child_count > 2) {
958 for (
i = 0;
i < body_child_count;
i++) {
964 if (!strcmp(node_type,
"comment")) {
969 if (strcmp(node_type,
"enumerator")) {
970 parser_error(
state,
"ERROR: Enum member AST should contain (enumerator) node!\n");
978 if (member_child_count < 1 || member_child_count > 2) {
979 parser_error(
state,
"ERROR: enum member AST cannot contain less than 1 or more than 2 items");
989 if (
state->verbose) {
992 if (membertext && nodeast) {
999 if (member_child_count == 1) {
1014 st64 derived_val = 0;
1017 derived_val = lastcase->
val + 1;
1020 .
name = real_identifier,
1036 parser_error(
state,
"ERROR: Enum case identifier and value should not be NULL!\n");
1045 parser_debug(
state,
"enum member: %s value: %s\n", real_identifier, real_value);
1049 .
name = real_identifier,
1086 if (typedef_node_child_count < 2) {
1092 bool is_const =
false;
1100 if (!strcmp(leaf_type,
"type_qualifier")) {
1103 if (!strcmp(qualifier,
"const")) {
1112 parser_error(
state,
"ERROR: Typedef type and declarator nodes should not be NULL!\n");
1120 if (
state->verbose) {
1123 if (typetext && nodeast) {
1137 char *typedef_name =
NULL;
1148 parser_debug(
state,
"typedef \"%s\" -> \"%s\"\n", typedef_name, base_type_name);
1150 if (!typedef_pair) {
1151 parser_error(
state,
"Error forming RzType and RzBaseType pair out of typedef: \"%s\"\n", typedef_name);
1157 parser_debug(
state,
"storing typedef \"%s\" -> \"%s\"\n", typedef_name, base_type_name);
1165 *tpair = typedef_pair;
1183 if (!strcmp(node_type,
"struct_specifier")) {
1185 if (result || !*tpair) {
1188 }
else if (!strcmp(node_type,
"union_specifier")) {
1190 if (result || !*tpair) {
1193 }
else if (!strcmp(node_type,
"enum_specifier")) {
1195 if (result || !*tpair) {
1198 }
else if (!strcmp(node_type,
"type_definition")) {
1200 if (result || !*tpair) {
1203 }
else if (!strcmp(node_type,
"sized_type_specifier")) {
1205 if (result || !*tpair) {
1208 }
else if (!strcmp(node_type,
"primitive_type")) {
1210 if (result || !*tpair) {
1213 }
else if (!strcmp(node_type,
"type_identifier")) {
1215 if (result || !*tpair) {
1237 parser_error(
state,
"ERROR: Parameter description only acceptable as part of function definition!\n");
1243 if (strcmp(node_type,
"parameter_list")) {
1248 if (paramlist_child_count < 1) {
1253 for (
i = 0;
i < paramlist_child_count;
i++) {
1258 if (strcmp(node_type,
"parameter_declaration") && strcmp(node_type,
"variadic_parameter")) {
1259 parser_error(
state,
"ERROR: Parameter field AST should contain (parameter_declaration|variadic_parameter) node!\n");
1263 if (!strcmp(node_type,
"variadic_parameter")) {
1268 char *identifier =
NULL;
1275 if (!argtpair || !argtpair->
type) {
1304 bool is_const =
false;
1305 bool has_qualifiers =
false;
1308 if (node_child_count > 0) {
1318 if (!strcmp(leaf_type,
"type_qualifier")) {
1321 if (!strcmp(qualifier,
"const")) {
1324 has_qualifiers =
true;
1331 if (!strcmp(node_type,
"abstract_pointer_declarator")) {
1341 type->pointer.is_const = is_const;
1342 type->pointer.type = (*tpair)->type;
1343 (*tpair)->type =
type;
1351 if (pointer_node_child_count > 0) {
1354 parser_error(
state,
"ERROR: Abstract pointer declarator AST should contain at least one node!\n");
1357 (*tpair)->type =
NULL;
1361 const char *declarator_type =
ts_node_type(pointer_declarator);
1362 if (!declarator_type) {
1365 (*tpair)->type =
NULL;
1380 }
else if (!strcmp(node_type,
"abstract_array_declarator")) {
1383 if (array_node_child_count < 0 || array_node_child_count > 2) {
1398 type->array.count = 0;
1401 if (!real_array_size) {
1407 type->array.count = array_sz;
1408 free(real_array_size);
1410 type->array.type = (*tpair)->type;
1411 (*tpair)->type =
type;
1418 const char *declarator_type =
ts_node_type(array_declarator);
1419 if (!declarator_type) {
1431 }
else if (!strcmp(node_type,
"abstract_function_declarator")) {
1436 if (function_node_child_count != 1) {
1445 const char *declarator_type =
ts_node_type(parenthesized_declarator);
1446 if (strcmp(declarator_type,
"parenthesized_declarator")) {
1456 const char *param_list_type =
ts_node_type(parameter_list);
1457 if (strcmp(param_list_type,
"parameter_list")) {
1465 if (!(*tpair)->type) {
1476 (*tpair)->type->callable->ret = parent_type;
1486 return (!strcmp(
type,
"identifier") || !strcmp(
type,
"field_identifier") || !strcmp(
type,
"type_identifier"));
1500 bool is_const =
false;
1503 if (node_child_count > 0) {
1510 if (!strcmp(leaf_type,
"type_qualifier")) {
1513 if (!strcmp(qualifier,
"const")) {
1527 *identifier = real_ident;
1529 }
else if (!strcmp(node_type,
"pointer_declarator")) {
1540 parser_error(
state,
"ERROR: Pointer declarator AST should contain at least one node!\n");
1545 const char *declarator_type =
ts_node_type(pointer_declarator);
1546 if (!declarator_type) {
1560 type->pointer.is_const = is_const;
1561 type->pointer.type = (*tpair)->type;
1562 (*tpair)->type =
type;
1570 }
else if (!strcmp(node_type,
"array_declarator")) {
1580 parser_error(
state,
"ERROR: Array declarator AST should contain at least one node!\n");
1585 const char *declarator_type =
ts_node_type(array_declarator);
1586 if (!declarator_type) {
1598 type->array.count = 0;
1602 if (!real_array_size) {
1608 type->array.count = array_sz;
1609 free(real_array_size);
1611 type->array.type = (*tpair)->type;
1612 (*tpair)->type =
type;
1620 }
else if (!strcmp(node_type,
"function_declarator")) {
1628 if (function_node_child_count > 2) {
1637 const char *declarator_type =
ts_node_type(declarator);
1646 if (!naked_callable) {
1652 (*tpair)->
type = naked_callable;
1664 const char *function_declarator_type =
ts_node_type(function_declarator);
1665 if (!function_declarator_type) {
1691 const char *param_list_type =
ts_node_type(parameter_list);
1692 if (strcmp(param_list_type,
"parameter_list")) {
1698 parent_type = (*tpair)->
type;
1700 (*tpair)->
type = naked_callable;
1703 parser_error(
state,
"ERROR: parsing parameters for callable type: \"%s\"\n", *identifier);
1707 (*tpair)->type = parent_type;
1709 parser_error(
state,
"ERROR: storing the new callable type: \"%s\"\n", *identifier);
1728 if (strcmp(node_type,
"type_descriptor")) {
1734 if (typedesc_node_child_count < 1) {
1745 bool is_const =
false;
1752 if (!strcmp(leaf_type,
"type_qualifier")) {
1755 if (!strcmp(qualifier,
"const")) {
1797 if (strcmp(node_type,
"declaration")) {
1803 if (declaration_node_child_count < 1) {
1814 bool is_const =
false;
1821 if (!strcmp(leaf_type,
"type_qualifier")) {
1824 if (!strcmp(qualifier,
"const")) {
1849 char *identifier =
NULL;
1876 if (!strcmp(node_type,
"struct_specifier")) {
1878 if (result || !tpair) {
1881 }
else if (!strcmp(node_type,
"union_specifier")) {
1883 if (result || !tpair) {
1886 }
else if (!strcmp(node_type,
"enum_specifier")) {
1888 if (result || !tpair) {
1891 }
else if (!strcmp(node_type,
"type_definition")) {
1893 if (result || !tpair) {
1902 if (!strcmp(node_type,
"declaration")) {
1904 if (result || !tpair) {
const char * ts_node_type(TSNode)
TSNode ts_node_named_child(TSNode, uint32_t)
TSNode ts_node_next_named_sibling(TSNode)
char * ts_node_string(TSNode)
uint32_t ts_node_named_child_count(TSNode)
bool ts_node_is_null(TSNode)
TSNode ts_node_child_by_field_name(TSNode self, const char *field_name, uint32_t field_name_length)
bool ts_node_is_named(TSNode)
RZ_API void rz_type_base_type_free(RzBaseType *type)
Frees the RzBaseType instance and all of its members.
int bits(struct state *s, int need)
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
RZ_API void Ht_() free(HtName_(Ht) *ht)
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
#define rz_return_if_fail(expr)
#define rz_return_val_if_fail(expr, val)
RZ_API ut64 rz_num_get(RzNum *num, const char *str)
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
static const char * rz_str_get_null(const char *str)
RZ_API bool RZ_API bool rz_strbuf_vappendf(RzStrBuf *sb, const char *fmt, va_list ap)
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
static void * rz_vector_tail(RzVector *vec)
RZ_API void * rz_vector_push(RzVector *vec, void *x)
static bool rz_vector_empty(const RzVector *vec)
RzBaseTypeStruct struct_data
RzBaseTypeUnion union_data
RZ_API RZ_BORROW const char * rz_type_identifier(RZ_NONNULL const RzType *type)
Returns the type C identifier.
RZ_API void rz_type_free(RZ_NULLABLE RzType *type)
Frees the RzType.
int parse_primitive_type(CParserState *state, TSNode node, const char *text, ParserTypePair **tpair, bool is_const)
int parse_declaration_node(CParserState *state, TSNode node, const char *text, ParserTypePair **tpair)
int parse_type_node_single(CParserState *state, TSNode node, const char *text, ParserTypePair **tpair, bool is_const)
#define TS_START_END(node, start, end)
int parse_union_node(CParserState *state, TSNode node, const char *text, ParserTypePair **tpair, bool is_const)
int parse_type_nodes_save(CParserState *state, TSNode node, const char *text)
void node_malformed_error(CParserState *state, TSNode node, const char *text, const char *nodetype)
int parse_type_descriptor_single(CParserState *state, TSNode node, const char *text, ParserTypePair **tpair)
int parse_parameter_list(CParserState *state, TSNode paramlist, const char *text, ParserTypePair **tpair)
static bool is_function_declarator(const char *declarator)
int parse_typedef_node(CParserState *state, TSNode node, const char *text, ParserTypePair **tpair)
int parse_sole_type_name(CParserState *state, TSNode node, const char *text, ParserTypePair **tpair, bool is_const)
int parse_type_declarator_node(CParserState *state, TSNode node, const char *text, ParserTypePair **tpair, char **identifier)
void parser_debug(CParserState *state, const char *fmt,...)
int parse_parameter_declaration_node(CParserState *state, TSNode node, const char *text, ParserTypePair **tpair, char **identifier)
static bool is_abstract_declarator(const char *declarator)
int parse_struct_node(CParserState *state, TSNode node, const char *text, ParserTypePair **tpair, bool is_const)
static bool is_identifier(const char *type)
int parse_type_abstract_declarator_node(CParserState *state, TSNode node, const char *text, ParserTypePair **tpair)
int parse_sized_primitive_type(CParserState *state, TSNode node, const char *text, ParserTypePair **tpair, bool is_const)
static char * ts_node_sub_string(TSNode node, const char *cstr)
static bool is_declarator(const char *declarator)
void parser_error(CParserState *state, const char *fmt,...)
void parser_warning(CParserState *state, const char *fmt,...)
int parse_enum_node(CParserState *state, TSNode node, const char *text, ParserTypePair **tpair, bool is_const)
RZ_OWN ParserTypePair * c_parser_new_union_naked_type(CParserState *state, RZ_NONNULL const char *name)
Creates new union naked type (without base type) based on the name.
bool c_parser_forward_definition_remove(CParserState *state, RZ_NONNULL const char *name)
bool c_parser_base_type_is_forward_definition(CParserState *state, RZ_NONNULL const char *name)
RZ_OWN ParserTypePair * c_parser_new_unspecified_naked_type(CParserState *state, RZ_NONNULL const char *name, bool is_const)
Creates new unspecified naked type (without base type) based on the name.
RZ_OWN ParserTypePair * c_parser_new_enum_type(CParserState *state, RZ_NONNULL const char *name, size_t cases_count)
Creates new enumeration type based on the name.
bool c_parser_base_type_store(CParserState *state, RZ_NONNULL const char *name, ParserTypePair *tpair)
RZ_OWN ParserTypePair * c_parser_new_structure_type(CParserState *state, RZ_NONNULL const char *name, size_t members_count)
Creates new structure "type + base type" pair based on the name.
RZ_OWN ParserTypePair * c_parser_get_structure_type(CParserState *state, RZ_NONNULL const char *name)
Returns the structure type if matching in the types hashtable.
RZ_OWN ParserTypePair * c_parser_new_primitive_type(CParserState *state, RZ_NONNULL const char *name, bool is_const)
Creates new primitive type based on the name.
RZ_OWN RzType * c_parser_new_callable(CParserState *state, RZ_NONNULL const char *name)
Creates new callable based on the name.
RZ_OWN ParserTypePair * c_parser_new_typedef(CParserState *state, RZ_NONNULL const char *name, RZ_NONNULL const char *base)
Creates new type alias based on the name.
bool c_parser_forward_definition_store(CParserState *state, RZ_NONNULL const char *name)
RZ_OWN char * c_parser_new_anonymous_callable_name(CParserState *state)
RZ_OWN ParserTypePair * c_parser_new_enum_naked_type(CParserState *state, RZ_NONNULL const char *name)
Creates new enum naked type (without base type) based on the name.
RZ_OWN ParserTypePair * c_parser_new_structure_naked_type(CParserState *state, RZ_NONNULL const char *name)
Creates new structure naked type (without base type) based on the name.
RZ_OWN char * c_parser_new_anonymous_enum_name(CParserState *state)
RZ_OWN ParserTypePair * c_parser_get_typedef(CParserState *state, RZ_NONNULL const char *name)
Returns the type if matching in the types hashtable.
RZ_OWN ParserTypePair * c_parser_new_enum_forward_definition(CParserState *state, RZ_NONNULL const char *name)
Creates new enum forward definition.
RZ_OWN char * c_parser_new_anonymous_structure_name(CParserState *state)
bool c_parser_new_callable_argument(CParserState *state, RZ_NONNULL RzCallable *callable, RZ_NONNULL const char *name, RZ_OWN RZ_NONNULL RzType *type)
Adds a new argument to the callable.
RZ_OWN ParserTypePair * c_parser_get_enum_type(CParserState *state, RZ_NONNULL const char *name)
Returns the enum type if matching in the types hashtable.
RZ_OWN char * c_parser_new_anonymous_union_name(CParserState *state)
RZ_OWN ParserTypePair * c_parser_get_union_type(CParserState *state, RZ_NONNULL const char *name)
Returns the union type if matching in the types hashtable.
RZ_OWN ParserTypePair * c_parser_get_primitive_type(CParserState *state, RZ_NONNULL const char *name, bool is_const)
Returns the primitive type if matching in the types hashtable.
RZ_OWN ParserTypePair * c_parser_new_union_type(CParserState *state, RZ_NONNULL const char *name, size_t members_count)
Creates new union "type + base type" pair based on the name.
RZ_OWN ParserTypePair * c_parser_new_union_forward_definition(CParserState *state, RZ_NONNULL const char *name)
Creates new union forward definition.
RZ_OWN RzType * c_parser_new_naked_callable(CParserState *state)
Creates new naked callable without storing it.
RZ_OWN ParserTypePair * c_parser_new_structure_forward_definition(CParserState *state, RZ_NONNULL const char *name)
Creates new structure forward definition.
bool c_parser_callable_type_store(CParserState *state, RZ_NONNULL const char *name, RZ_NONNULL RzType *type)