Rizin
unix-like reverse engineering framework and cli tools
base.c File Reference
#include <rz_util.h>
#include <rz_type.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  list_kind
 

Functions

RZ_API void rz_type_base_enum_case_free (void *e, void *user)
 
RZ_API void rz_type_base_struct_member_free (void *e, void *user)
 
RZ_API void rz_type_base_union_member_free (void *e, void *user)
 
RZ_API RZ_BORROW const char * rz_type_base_type_kind_as_string (RzBaseTypeKind kind)
 Returns string representing the kind of base type. More...
 
RZ_API RZ_BORROW RzBaseTyperz_type_db_get_base_type (const RzTypeDB *typedb, RZ_NONNULL const char *name)
 Searches for the RzBaseType in the types database given the name. More...
 
RZ_API bool rz_type_db_delete_base_type (RzTypeDB *typedb, RZ_NONNULL RzBaseType *type)
 Removes RzBaseType from the Types DB. More...
 
static bool base_type_kind_collect_cb (void *user, const void *k, const void *v)
 
RZ_API RZ_OWN RzListrz_type_db_get_base_types_of_kind (const RzTypeDB *typedb, RzBaseTypeKind kind)
 Returns the list of all basic types of the chosen kind. More...
 
static bool base_type_collect_cb (void *user, const void *k, const void *v)
 
RZ_API RZ_OWN RzListrz_type_db_get_base_types (const RzTypeDB *typedb)
 Returns the list of all basic types. More...
 
RZ_API void rz_type_base_type_free (RzBaseType *type)
 Frees the RzBaseType instance and all of its members. More...
 
RZ_API RZ_OWN RzBaseTyperz_type_base_type_new (RzBaseTypeKind kind)
 Allocates a new instance of RzBaseType given the kind. More...
 
RZ_API void rz_type_db_save_base_type (const RzTypeDB *typedb, const RzBaseType *type)
 Saves RzBaseType into the Types DB. More...
 
RZ_API RZ_OWN char * rz_type_db_base_type_as_string (const RzTypeDB *typedb, RZ_NONNULL const RzBaseType *btype)
 Returns C representation as string of RzBaseType (see rz_type_db_base_type_as_pretty_string for cusom print options) More...
 
RZ_API RZ_OWN char * rz_type_db_base_type_as_pretty_string (RZ_NONNULL const RzTypeDB *typedb, RZ_NONNULL const RzBaseType *btype, unsigned int opts, int unfold_level)
 Returns C representation as string of RzBaseType. More...
 
RZ_API RZ_BORROW RzBaseTyperz_type_db_get_compound_type (const RzTypeDB *typedb, RZ_NONNULL const char *name)
 Searches for the compound RzBaseType in the types database given the name. More...
 

Function Documentation

◆ base_type_collect_cb()

static bool base_type_collect_cb ( void *  user,
const void *  k,
const void *  v 
)
static

Definition at line 108 of file base.c.

108  {
109  rz_return_val_if_fail(user && k && v, false);
110  RzList *l = user;
111  rz_list_append(l, (void *)v);
112  return true;
113 }
const char * k
Definition: dsignal.c:11
const char * v
Definition: dsignal.c:12
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108

References k, rz_list_append(), rz_return_val_if_fail, and v.

Referenced by rz_type_db_get_base_types().

◆ base_type_kind_collect_cb()

static bool base_type_kind_collect_cb ( void *  user,
const void *  k,
const void *  v 
)
static

Definition at line 85 of file base.c.

85  {
86  struct list_kind *l = user;
87  RzBaseType *btype = (RzBaseType *)v;
88  if (l->kind == btype->kind) {
89  rz_list_append(l->types, btype);
90  }
91  return true;
92 }
Definition: base.c:80
RzBaseTypeKind kind
Definition: base.c:82
RzList * types
Definition: base.c:81
RzBaseTypeKind kind
Definition: rz_type.h:115

References rz_base_type_t::kind, list_kind::kind, rz_list_append(), list_kind::types, and v.

Referenced by rz_type_db_get_base_types_of_kind().

◆ rz_type_base_enum_case_free()

RZ_API void rz_type_base_enum_case_free ( void *  e,
void *  user 
)

Definition at line 8 of file base.c.

8  {
9  (void)user;
10  RzTypeEnumCase *cas = e;
11  free((char *)cas->name);
12 }
#define e(frag)
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References e, free(), and rz_type_enum_case_t::name.

Referenced by parse_enum(), parse_enum_type(), and rz_type_base_type_new().

◆ rz_type_base_struct_member_free()

RZ_API void rz_type_base_struct_member_free ( void *  e,
void *  user 
)

Definition at line 14 of file base.c.

14  {
15  (void)user;
16  RzTypeStructMember *member = e;
17  rz_type_free(member->type);
18  free((char *)member->name);
19 }
RZ_API void rz_type_free(RZ_NULLABLE RzType *type)
Frees the RzType.
Definition: type.c:1273

References e, free(), rz_type_struct_member_t::name, rz_type_free(), and rz_type_struct_member_t::type.

Referenced by parse_structure(), and rz_type_base_type_new().

◆ rz_type_base_type_free()

RZ_API void rz_type_base_type_free ( RzBaseType type)

Frees the RzBaseType instance and all of its members.

Parameters
typeRzBaseType pointer

Definition at line 132 of file base.c.

132  {
134  RZ_FREE(type->name);
135  rz_type_free(type->type);
136  type->type = NULL;
137 
138  switch (type->kind) {
140  rz_vector_fini(&type->struct_data.members);
141  break;
143  rz_vector_fini(&type->union_data.members);
144  break;
146  rz_vector_fini(&type->enum_data.cases);
147  break;
150  break;
151  default:
152  break;
153  }
154  RZ_FREE(type);
155 }
#define NULL
Definition: cris-opc.c:27
int type
Definition: mipsasm.c:17
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
@ RZ_BASE_TYPE_KIND_TYPEDEF
Definition: rz_type.h:76
@ RZ_BASE_TYPE_KIND_ATOMIC
Definition: rz_type.h:77
@ RZ_BASE_TYPE_KIND_UNION
Definition: rz_type.h:74
@ RZ_BASE_TYPE_KIND_STRUCT
Definition: rz_type.h:73
@ RZ_BASE_TYPE_KIND_ENUM
Definition: rz_type.h:75
#define RZ_FREE(x)
Definition: rz_types.h:369
RZ_API void rz_vector_fini(RzVector *vec)
Definition: vector.c:61

References NULL, RZ_BASE_TYPE_KIND_ATOMIC, RZ_BASE_TYPE_KIND_ENUM, RZ_BASE_TYPE_KIND_STRUCT, RZ_BASE_TYPE_KIND_TYPEDEF, RZ_BASE_TYPE_KIND_UNION, RZ_FREE, rz_return_if_fail, rz_type_free(), rz_vector_fini(), and type.

Referenced by c_parser_new_enum_type(), c_parser_new_primitive_type(), c_parser_new_structure_type(), c_parser_new_typedef(), c_parser_new_union_type(), get_atomic_type(), get_enum_type(), get_struct_type(), get_typedef_type(), get_union_type(), parse_enum(), parse_enum_type(), parse_sole_type_name(), parse_structure(), parse_structure_type(), parse_typedef(), parse_union(), rz_type_db_edit_base_type(), and types_ht_free().

◆ rz_type_base_type_kind_as_string()

RZ_API RZ_BORROW const char* rz_type_base_type_kind_as_string ( RzBaseTypeKind  kind)

Returns string representing the kind of base type.

Parameters
kindRzBaseTypeKind to return string representation of

Definition at line 33 of file base.c.

33  {
34  switch (kind) {
36  return "struct";
38  return "union";
40  return "enum";
42  return "typedef";
44  return "atomic";
45  default:
47  return "unknown";
48  }
49 }
#define rz_warn_if_reached()
Definition: rz_assert.h:29

References RZ_BASE_TYPE_KIND_ATOMIC, RZ_BASE_TYPE_KIND_ENUM, RZ_BASE_TYPE_KIND_STRUCT, RZ_BASE_TYPE_KIND_TYPEDEF, RZ_BASE_TYPE_KIND_UNION, and rz_warn_if_reached.

Referenced by parse_enum(), parse_structure(), and parse_union().

◆ rz_type_base_type_new()

RZ_API RZ_OWN RzBaseType* rz_type_base_type_new ( RzBaseTypeKind  kind)

Allocates a new instance of RzBaseType given the kind.

Parameters
kindKind of RzBaseType to create

Definition at line 162 of file base.c.

162  {
164  if (!type) {
165  return NULL;
166  }
167  type->kind = kind;
168  switch (type->kind) {
171  break;
173  rz_vector_init(&type->enum_data.cases, sizeof(RzTypeEnumCase), rz_type_base_enum_case_free, NULL);
174  break;
177  break;
178  default:
179  break;
180  }
181 
182  return type;
183 }
RZ_API void rz_type_base_enum_case_free(void *e, void *user)
Definition: base.c:8
RZ_API void rz_type_base_struct_member_free(void *e, void *user)
Definition: base.c:14
RZ_API void rz_type_base_union_member_free(void *e, void *user)
Definition: base.c:21
#define RZ_NEW0(x)
Definition: rz_types.h:284
RZ_API void rz_vector_init(RzVector *vec, size_t elem_size, RzVectorFree free, void *free_user)
Definition: vector.c:33

References list_kind::kind, NULL, RZ_BASE_TYPE_KIND_ENUM, RZ_BASE_TYPE_KIND_STRUCT, RZ_BASE_TYPE_KIND_UNION, RZ_NEW0, rz_type_base_enum_case_free(), rz_type_base_struct_member_free(), rz_type_base_union_member_free(), rz_vector_init(), and type.

Referenced by c_parser_new_enum_type(), c_parser_new_primitive_type(), c_parser_new_structure_type(), c_parser_new_typedef(), c_parser_new_union_type(), get_atomic_type(), get_enum_type(), get_struct_type(), get_typedef_type(), get_union_type(), parse_atomic_type(), parse_enum(), parse_enum_type(), parse_structure(), parse_structure_type(), parse_type_nest(), parse_typedef(), and parse_union().

◆ rz_type_base_union_member_free()

RZ_API void rz_type_base_union_member_free ( void *  e,
void *  user 
)

Definition at line 21 of file base.c.

21  {
22  (void)user;
23  RzTypeUnionMember *member = e;
24  rz_type_free(member->type);
25  free((char *)member->name);
26 }

References e, free(), rz_type_union_member_t::name, rz_type_free(), and rz_type_union_member_t::type.

Referenced by parse_union(), and rz_type_base_type_new().

◆ rz_type_db_base_type_as_pretty_string()

RZ_API RZ_OWN char* rz_type_db_base_type_as_pretty_string ( RZ_NONNULL const RzTypeDB typedb,
RZ_NONNULL const RzBaseType btype,
unsigned int  opts,
int  unfold_level 
)

Returns C representation as string of RzBaseType.

Parameters
typedbtype database instance
btypeRzBaseType to convert
optsoptions for pretty printing (see RzTypePrintOpts)
unfold_levellevel of unfolding to do in case of nested structures/unions (any negative number means maximum unfolding, i.e. INT32_MAX. 0 means no unfolding, just the typename and identifier, if any)
Returns
char* pretty printed form of the base string (similar to rz_type_as_pretty_string, but for RzBaseType)

Definition at line 219 of file base.c.

219  {
220  rz_return_val_if_fail(typedb && btype, NULL);
221 
222  RzType *type = rz_type_identifier_of_base_type(typedb, btype, false);
223  return rz_type_as_pretty_string(typedb, type, NULL, opts, unfold_level);
224 }
RZ_API RZ_OWN RzType * rz_type_identifier_of_base_type(const RzTypeDB *typedb, RZ_NONNULL const RzBaseType *btype, bool is_const)
Creates a new RzType indentifier from the given RzBaseType.
Definition: helpers.c:15
RZ_API RZ_OWN char * rz_type_as_pretty_string(const RzTypeDB *typedb, RZ_NONNULL const RzType *type, RZ_NULLABLE const char *identifier, unsigned int opts, int unfold_level)
Return a string contining the type pretty printed according to the options provided.
Definition: type.c:1130

References NULL, rz_return_val_if_fail, rz_type_as_pretty_string(), rz_type_identifier_of_base_type(), and type.

Referenced by pdb_types_print_standard(), rz_core_base_type_as_c(), rz_core_types_enum_as_c(), rz_core_types_struct_as_c(), rz_core_types_typedef_as_c(), and rz_core_types_union_as_c().

◆ rz_type_db_base_type_as_string()

RZ_API RZ_OWN char* rz_type_db_base_type_as_string ( const RzTypeDB typedb,
RZ_NONNULL const RzBaseType btype 
)

Returns C representation as string of RzBaseType (see rz_type_db_base_type_as_pretty_string for cusom print options)

Parameters
typedbtype database instance
btypeRzBaseType to convert
Returns
char* one line C representation of the string with no semicolon at the end and no unfolding of inner types

Definition at line 203 of file base.c.

203  {
204  rz_return_val_if_fail(typedb && btype, NULL);
205 
206  RzType *type = rz_type_identifier_of_base_type(typedb, btype, false);
208 }
@ RZ_TYPE_PRINT_NO_END_SEMICOLON
Definition: rz_type.h:220
@ RZ_TYPE_PRINT_ZERO_VLA
Definition: rz_type.h:219

References NULL, rz_return_val_if_fail, rz_type_as_pretty_string(), rz_type_identifier_of_base_type(), RZ_TYPE_PRINT_NO_END_SEMICOLON, RZ_TYPE_PRINT_ZERO_VLA, and type.

◆ rz_type_db_delete_base_type()

RZ_API bool rz_type_db_delete_base_type ( RzTypeDB typedb,
RZ_NONNULL RzBaseType type 
)

Removes RzBaseType from the Types DB.

Parameters
typedbType Database instance
typeRzBaseType to remove

Definition at line 74 of file base.c.

74  {
75  rz_return_val_if_fail(typedb && type && type->name, false);
76  ht_pp_delete(typedb->types, type->name);
77  return true;
78 }
HtPP * types
Definition: rz_type.h:33

References rz_return_val_if_fail, type, and rz_type_db_t::types.

Referenced by parse_enum(), parse_structure(), parse_union(), and rz_type_db_del().

◆ rz_type_db_get_base_type()

RZ_API RZ_BORROW RzBaseType* rz_type_db_get_base_type ( const RzTypeDB typedb,
RZ_NONNULL const char *  name 
)

Searches for the RzBaseType in the types database given the name.

Parameters
typedbType Database instance
nameName of the RzBaseType

Definition at line 57 of file base.c.

57  {
58  rz_return_val_if_fail(typedb && name, NULL);
59 
60  bool found = false;
61  RzBaseType *btype = ht_pp_find(typedb->types, name, &found);
62  if (!found || !btype) {
63  return NULL;
64  }
65  return btype;
66 }
RZ_API const KEY_TYPE bool * found
Definition: ht_inc.h:130
Definition: z80asm.h:102

References found, NULL, rz_return_val_if_fail, and rz_type_db_t::types.

Referenced by get_base_type_typeclass(), get_tpitype_basetype(), get_type_typeclass(), parse_type_nest(), resolve_type_links(), rz_core_types_as_c(), rz_type_array_of_base_type_str(), rz_type_atomic_eq(), rz_type_atomic_str_eq(), rz_type_db_del(), rz_type_db_enum_get_bitfield(), rz_type_db_enum_member_by_name(), rz_type_db_enum_member_by_val(), rz_type_db_get_bitsize(), rz_type_db_get_compound_type(), rz_type_db_get_enum(), rz_type_db_get_struct(), rz_type_db_get_typedef(), rz_type_db_get_union(), rz_type_db_struct_member_offset(), rz_type_db_struct_member_packed_offset(), rz_type_exists(), rz_type_format(), rz_type_get_base_type(), rz_type_identifier_of_base_type_str(), rz_type_integral_set_sign(), rz_type_is_atomic(), rz_type_is_strictly_atomic(), rz_type_kind(), rz_type_new_default(), rz_type_pointer_of_base_type_str(), structured_member_walker(), type_as_pretty_string(), type_decl_as_pretty_string(), type_paths_collect_by_address_cb(), type_paths_collect_by_offset_cb(), type_to_format_pair(), and var_add_structure_fields_to_list().

◆ rz_type_db_get_base_types()

RZ_API RZ_OWN RzList* rz_type_db_get_base_types ( const RzTypeDB typedb)

Returns the list of all basic types.

Parameters
typedbTypes Database instance

Definition at line 120 of file base.c.

120  {
121  rz_return_val_if_fail(typedb, NULL);
122  RzList *types = rz_list_new();
123  ht_pp_foreach(typedb->types, base_type_collect_cb, types);
124  return types;
125 }
static bool base_type_collect_cb(void *user, const void *k, const void *v)
Definition: base.c:108
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
insn_type_descr_t types[]
Definition: or1k_disas.c:7

References base_type_collect_cb(), NULL, rz_list_new(), rz_return_val_if_fail, types, and rz_type_db_t::types.

Referenced by rz_core_types_print_all(), rz_type_db_all(), and rz_type_db_get_by_offset().

◆ rz_type_db_get_base_types_of_kind()

RZ_API RZ_OWN RzList* rz_type_db_get_base_types_of_kind ( const RzTypeDB typedb,
RzBaseTypeKind  kind 
)

◆ rz_type_db_get_compound_type()

RZ_API RZ_BORROW RzBaseType* rz_type_db_get_compound_type ( const RzTypeDB typedb,
RZ_NONNULL const char *  name 
)

Searches for the compound RzBaseType in the types database given the name.

Returns all types except atomic - structures, unions, enums, typedefs

Parameters
typedbType Database instance
nameName of the RzBaseType

Definition at line 234 of file base.c.

234  {
237  if (!t) {
238  RZ_LOG_ERROR("Cannot find type \"%s\"\n", name);
239  return NULL;
240  }
241  if (t->kind == RZ_BASE_TYPE_KIND_ATOMIC) {
242  RZ_LOG_ERROR("Atomic type \"%s\"\n", name);
243  return NULL;
244  }
245  return t;
246 }
RZ_API RZ_BORROW RzBaseType * rz_type_db_get_base_type(const RzTypeDB *typedb, RZ_NONNULL const char *name)
Searches for the RzBaseType in the types database given the name.
Definition: base.c:57
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58

References rz_base_type_t::kind, NULL, RZ_BASE_TYPE_KIND_ATOMIC, RZ_LOG_ERROR, rz_return_val_if_fail, and rz_type_db_get_base_type().

Referenced by rz_type_db_edit_base_type(), and rz_types_open_editor().

◆ rz_type_db_save_base_type()

RZ_API void rz_type_db_save_base_type ( const RzTypeDB typedb,
const RzBaseType type 
)

Saves RzBaseType into the Types DB.

Parameters
typedbType Database instance
typeRzBaseType to save

Definition at line 191 of file base.c.

191  {
192  rz_return_if_fail(typedb && type && type->name);
193  ht_pp_insert(typedb->types, type->name, (void *)type);
194 }

References rz_return_if_fail, type, and rz_type_db_t::types.

Referenced by parse_atomic_type(), parse_enum(), parse_enum_type(), parse_structure(), parse_structure_type(), parse_type_nest(), parse_typedef(), and parse_union().