Rizin
unix-like reverse engineering framework and cli tools
base.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 Anton Kochkov <anton.kochkov@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_util.h>
5 #include <rz_type.h>
6 #include <string.h>
7 
8 RZ_API void rz_type_base_enum_case_free(void *e, void *user) {
9  (void)user;
10  RzTypeEnumCase *cas = e;
11  free((char *)cas->name);
12 }
13 
14 RZ_API void rz_type_base_struct_member_free(void *e, void *user) {
15  (void)user;
16  RzTypeStructMember *member = e;
17  rz_type_free(member->type);
18  free((char *)member->name);
19 }
20 
21 RZ_API void rz_type_base_union_member_free(void *e, void *user) {
22  (void)user;
23  RzTypeUnionMember *member = e;
24  rz_type_free(member->type);
25  free((char *)member->name);
26 }
27 
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 }
50 
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 }
67 
75  rz_return_val_if_fail(typedb && type && type->name, false);
76  ht_pp_delete(typedb->types, type->name);
77  return true;
78 }
79 
80 struct list_kind {
83 };
84 
85 static bool base_type_kind_collect_cb(void *user, const void *k, const void *v) {
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 }
93 
101  rz_return_val_if_fail(typedb, NULL);
102  RzList *types = rz_list_new();
103  struct list_kind lk = { types, kind };
104  ht_pp_foreach(typedb->types, base_type_kind_collect_cb, &lk);
105  return types;
106 }
107 
108 static bool base_type_collect_cb(void *user, const void *k, const void *v) {
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 }
114 
120 RZ_API RZ_OWN RzList /*<RzBaseType *>*/ *rz_type_db_get_base_types(const RzTypeDB *typedb) {
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 }
126 
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 }
156 
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 }
184 
192  rz_return_if_fail(typedb && type && type->name);
193  ht_pp_insert(typedb->types, type->name, (void *)type);
194 }
195 
204  rz_return_val_if_fail(typedb && btype, NULL);
205 
206  RzType *type = rz_type_identifier_of_base_type(typedb, btype, false);
208 }
209 
219 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) {
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 }
225 
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 }
#define e(frag)
static bool base_type_kind_collect_cb(void *user, const void *k, const void *v)
Definition: base.c:85
RZ_API void rz_type_base_type_free(RzBaseType *type)
Frees the RzBaseType instance and all of its members.
Definition: base.c:132
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 RZ_OWN RzList * rz_type_db_get_base_types(const RzTypeDB *typedb)
Returns the list of all basic types.
Definition: base.c:120
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.
Definition: base.c:234
RZ_API RZ_OWN RzBaseType * rz_type_base_type_new(RzBaseTypeKind kind)
Allocates a new instance of RzBaseType given the kind.
Definition: base.c:162
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.
Definition: base.c:219
RZ_API void rz_type_db_save_base_type(const RzTypeDB *typedb, const RzBaseType *type)
Saves RzBaseType into the Types DB.
Definition: base.c:191
RZ_API RZ_BORROW const char * rz_type_base_type_kind_as_string(RzBaseTypeKind kind)
Returns string representing the kind of base type.
Definition: base.c:33
RZ_API void rz_type_base_union_member_free(void *e, void *user)
Definition: base.c:21
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...
Definition: base.c:203
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
RZ_API bool rz_type_db_delete_base_type(RzTypeDB *typedb, RZ_NONNULL RzBaseType *type)
Removes RzBaseType from the Types DB.
Definition: base.c:74
static bool base_type_collect_cb(void *user, const void *k, const void *v)
Definition: base.c:108
RZ_API RZ_OWN RzList * rz_type_db_get_base_types_of_kind(const RzTypeDB *typedb, RzBaseTypeKind kind)
Returns the list of all basic types of the chosen kind.
Definition: base.c:100
#define RZ_API
#define NULL
Definition: cris-opc.c:27
const char * k
Definition: dsignal.c:11
const char * v
Definition: dsignal.c:12
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 void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API const KEY_TYPE bool * found
Definition: ht_inc.h:130
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
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
int type
Definition: mipsasm.c:17
insn_type_descr_t types[]
Definition: or1k_disas.c:7
#define rz_warn_if_reached()
Definition: rz_assert.h:29
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RzBaseTypeKind
Definition: rz_type.h:72
@ 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
@ RZ_TYPE_PRINT_NO_END_SEMICOLON
Definition: rz_type.h:220
@ RZ_TYPE_PRINT_ZERO_VLA
Definition: rz_type.h:219
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_FREE(x)
Definition: rz_types.h:369
#define RZ_BORROW
Definition: rz_types.h:63
RZ_API void rz_vector_fini(RzVector *vec)
Definition: vector.c:61
RZ_API void rz_vector_init(RzVector *vec, size_t elem_size, RzVectorFree free, void *free_user)
Definition: vector.c:33
Definition: base.c:80
RzBaseTypeKind kind
Definition: base.c:82
RzList * types
Definition: base.c:81
Definition: z80asm.h:102
RzBaseTypeKind kind
Definition: rz_type.h:115
HtPP * types
Definition: rz_type.h:33
RZ_API void rz_type_free(RZ_NULLABLE RzType *type)
Frees the RzType.
Definition: type.c:1273
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