From ec2cff8451367ed861dbfbabb44abeaa53a2434f Mon Sep 17 00:00:00 2001 From: billow Date: Fri, 25 Aug 2023 20:21:41 +0800 Subject: [PATCH] RzType add new APIs - rz_base_type_clone_into - rz_base_type_clone --- librz/include/rz_type.h | 5 ++++- librz/type/base.c | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/librz/include/rz_type.h b/librz/include/rz_type.h index 831c2d8c3b..0707ffcb50 100644 --- a/librz/include/rz_type.h +++ b/librz/include/rz_type.h @@ -253,7 +253,10 @@ RZ_API void rz_type_db_reload(RzTypeDB *typedb, const char *dir_prefix); RZ_API void rz_type_db_purge(RzTypeDB *typedb); // Base types - +RZ_API bool rz_base_type_clone_into( + RZ_NONNULL RZ_BORROW RZ_OUT RzBaseType *dst, + RZ_NONNULL RZ_BORROW RZ_IN RzBaseType *src); +RZ_API RZ_OWN RzBaseType *rz_base_type_clone(RZ_NULLABLE RZ_BORROW RzBaseType *b); RZ_API void rz_type_base_type_free(RzBaseType *type); RZ_API RZ_OWN RzBaseType *rz_type_base_type_new(RzBaseTypeKind kind); RZ_API RZ_BORROW const char *rz_type_base_type_kind_as_string(RzBaseTypeKind kind); diff --git a/librz/type/base.c b/librz/type/base.c index 191ccfc136..1b606c66e4 100644 --- a/librz/type/base.c +++ b/librz/type/base.c @@ -124,6 +124,54 @@ RZ_API RZ_OWN RzList /**/ *rz_type_db_get_base_types(const RzTypeD return types; } +/** + * \brief Copy RzBaseType \p src into another RzBaseType \p dst + * \param dst the destination RzBaseType + * \param src the source RzBaseType + * \return true if the copy was successful, false otherwise + */ +RZ_API bool rz_base_type_clone_into( + RZ_NONNULL RZ_BORROW RZ_OUT RzBaseType *dst, + RZ_NONNULL RZ_BORROW RZ_IN RzBaseType *src) { + rz_return_val_if_fail(src && dst, false); + rz_mem_copy(dst, sizeof(RzBaseType), src, sizeof(RzBaseType)); + dst->name = rz_str_new(src->name); + dst->type = src->type ? rz_type_clone(src->type) : NULL; + + switch (src->kind) { + case RZ_BASE_TYPE_KIND_ENUM: + rz_vector_clone_into(&dst->enum_data.cases, &src->enum_data.cases); + break; + case RZ_BASE_TYPE_KIND_STRUCT: + rz_vector_clone_into(&dst->struct_data.members, &src->struct_data.members); + break; + case RZ_BASE_TYPE_KIND_UNION: + rz_vector_clone_into(&dst->union_data.members, &src->union_data.members); + break; + default: break; + } + return true; +} + +/** + * \brief Copy the RzBaseType \p b and all its members + * \param b the RzBaseType to copy + * \return a copy of \p b + */ +RZ_API RZ_OWN RzBaseType *rz_base_type_clone(RZ_NULLABLE RZ_BORROW RzBaseType *b) { + if (!b) { + return NULL; + } + RzBaseType *type = RZ_NEW0(RzBaseType); + if (!type) { + return NULL; + } + if (!rz_base_type_clone_into(type, b)) { + return NULL; + } + return type; +} + /** * \brief Frees the RzBaseType instance and all of its members *