mie: vector: store vector components in an anonymous struct; improve macro consistency
This commit is contained in:
@@ -52,15 +52,26 @@ MIE_API bool mie_parser_check_type(struct mie_parser *ctx, enum mie_token_type t
|
||||
MIE_API bool mie_parser_check_symbol(
|
||||
struct mie_parser *ctx, enum mie_token_symbol sym);
|
||||
|
||||
MIE_API bool mie_parser_parse_instname(struct mie_parser *ctx, b_string *out);
|
||||
MIE_API bool mie_parser_parse_graphname(struct mie_parser *ctx, b_string *out);
|
||||
MIE_API bool mie_parser_parse_opname(struct mie_parser *ctx, b_string *out);
|
||||
MIE_API bool mie_parser_parse_vregname(struct mie_parser *ctx, b_string *out);
|
||||
MIE_API bool mie_parser_parse_mregname(struct mie_parser *ctx, b_string *out);
|
||||
MIE_API bool mie_parser_parse_blockname(struct mie_parser *ctx, b_string *out);
|
||||
MIE_API bool mie_parser_parse_typename(struct mie_parser *ctx, b_string *out);
|
||||
MIE_API bool mie_parser_parse_symname(struct mie_parser *ctx, b_string *out);
|
||||
MIE_API bool mie_parser_parse_string(struct mie_parser *ctx, b_string *out);
|
||||
MIE_API bool mie_parser_parse_word(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_instname(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_graphname(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_opname(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_vregname(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_mregname(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_blockname(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_typename(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_symname(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_string(
|
||||
struct mie_parser *ctx, b_string *out, struct mie_file_span *loc);
|
||||
MIE_API bool mie_parser_parse_keyword(struct mie_parser *ctx, const char *kw);
|
||||
MIE_API bool mie_parser_parse_symbol(
|
||||
struct mie_parser *ctx, enum mie_token_symbol sym);
|
||||
@@ -70,13 +81,13 @@ MIE_API bool mie_parser_parse_assignment_list(
|
||||
|
||||
MIE_API bool mie_parser_parse_type(struct mie_parser *ctx, struct mie_type **out);
|
||||
MIE_API bool mie_parser_parse_type_list(
|
||||
struct mie_parser *ctx, MIE_VECTOR_PARAM(struct mie_type *, out));
|
||||
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(const struct mie_type *, out));
|
||||
|
||||
MIE_API bool mie_parser_parse_operand(
|
||||
struct mie_parser *ctx, struct mie_unresolved_operand *out);
|
||||
MIE_API bool mie_parser_parse_operand_list(
|
||||
struct mie_parser *ctx,
|
||||
MIE_VECTOR_PARAM(struct mie_unresolved_operand, out));
|
||||
MIE_VECTOR_REF_PARAM(struct mie_unresolved_operand, out));
|
||||
|
||||
MIE_API bool mie_parser_parse_unknown_keyword(struct mie_parser *ctx, b_string *out);
|
||||
MIE_API bool mie_parser_parse_unknown_symbol(
|
||||
@@ -87,22 +98,22 @@ MIE_API bool mie_parser_parse_register(
|
||||
struct mie_register *out);
|
||||
MIE_API bool mie_parser_parse_register_list(
|
||||
struct mie_parser *ctx, struct mie_name_map *names,
|
||||
MIE_VECTOR_PARAM(struct mie_register, out));
|
||||
MIE_VECTOR_REF_PARAM(struct mie_register, out));
|
||||
|
||||
MIE_API bool mie_parser_parse_region(
|
||||
struct mie_parser *ctx, struct mie_region *region);
|
||||
MIE_API bool mie_parser_parse_region_list(
|
||||
struct mie_parser *ctx, MIE_VECTOR_PARAM(struct mie_region, out));
|
||||
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(struct mie_region, out));
|
||||
|
||||
MIE_API bool mie_parser_parse_attribute(
|
||||
struct mie_parser *ctx, struct mie_op_attribute *attrib);
|
||||
MIE_API bool mie_parser_parse_attribute_list(
|
||||
struct mie_parser *ctx, MIE_VECTOR_PARAM(struct mie_op_attribute, out));
|
||||
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(struct mie_op_attribute, out));
|
||||
|
||||
MIE_API bool mie_parser_parse_successor(
|
||||
struct mie_parser *ctx, struct mie_op_successor *successor);
|
||||
MIE_API bool mie_parser_parse_successor_list(
|
||||
struct mie_parser *ctx, MIE_VECTOR_PARAM(struct mie_op_successor, out));
|
||||
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(struct mie_op_successor, out));
|
||||
|
||||
MIE_API bool mie_parser_parse_module(struct mie_parser *ctx, struct mie_module *mod);
|
||||
MIE_API bool mie_parser_parse_op(
|
||||
|
||||
@@ -4,37 +4,64 @@
|
||||
#include <mie/misc.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#if 0
|
||||
#define MIE_VECTOR_DEFINE(type, name) \
|
||||
size_t name##_count = 0; \
|
||||
size_t name##_max = 0; \
|
||||
type *name = NULL
|
||||
#endif
|
||||
|
||||
#define MIE_VECTOR_DEFINE(type, name) \
|
||||
struct { \
|
||||
size_t count; \
|
||||
size_t max; \
|
||||
type *items; \
|
||||
} name = {0}
|
||||
|
||||
#define MIE_VECTOR_DECLARE(type, name) \
|
||||
size_t name##_count; \
|
||||
size_t name##_max; \
|
||||
type *name
|
||||
struct { \
|
||||
size_t count; \
|
||||
size_t max; \
|
||||
type *items; \
|
||||
} name
|
||||
|
||||
#define MIE_VECTOR_COUNT(name) name##_count
|
||||
#define MIE_VECTOR_MAX(name) name##_max
|
||||
#define MIE_VECTOR_ITEM(name, index) name.items[index]
|
||||
#define MIE_VECTOR_ITEM_PTR(name, index) &name.items[index]
|
||||
#define MIE_VECTOR_COUNT(name) name.count
|
||||
#define MIE_VECTOR_MAX(name) name.max
|
||||
|
||||
#define MIE_VECTOR_PARAM(type, name) \
|
||||
/* use this macros in your function prototype, to allow the function to accept
|
||||
* a reference to a vector as a parameter */
|
||||
#define MIE_VECTOR_REF_PARAM(type, name) \
|
||||
type **name, size_t *name##_count, size_t *name##_max
|
||||
#define MIE_VECTOR_ARG(name) &(name), &(name##_count), &(name##_max)
|
||||
#define MIE_VECTOR_REF_ARG(name) (name), (name##_count), (name##_max)
|
||||
/* use this macro to pass a reference to a vector as a parameter to a function
|
||||
* whose prototype uses MIE_VECTOR_REF_PARAM */
|
||||
#define MIE_VECTOR_REF(name) &(name.items), &(name.count), &(name.max)
|
||||
/* use this macro to forward your reference to a vector (which you got via
|
||||
* MIE_VECTOR_REF_PARAM in your function prototype), to another function whose
|
||||
* prototype also uses MIE_VECTOR_REF_PARAM */
|
||||
#define MIE_VECTOR_REF2(name) &(name.items), &(name.count), &(name.max)
|
||||
|
||||
/* use these functions if you're accessing a vector directly. */
|
||||
#define mie_vector_push_back(vector, ptr) \
|
||||
__mie_vector_push_back( \
|
||||
(void **)&(vector), ptr, sizeof *ptr, &(vector##_count), \
|
||||
&(vector##_max))
|
||||
(void **)&(vector.items), ptr, sizeof *ptr, &(vector.count), \
|
||||
&(vector.max))
|
||||
#define mie_vector_pop_back(vector) \
|
||||
__mie_vector_pop_back( \
|
||||
(void **)&(vector), sizeof *vector, &(vector##_count), \
|
||||
&(vector##_max))
|
||||
(void **)&(vector), sizeof *vector, &(vector.count), \
|
||||
&(vector.max))
|
||||
#define mie_vector_emplace_back(vector) \
|
||||
__mie_vector_emplace_back( \
|
||||
(void **)&(vector), sizeof *vector, &(vector##_count), \
|
||||
&(vector##_max))
|
||||
(void **)&(vector.items), sizeof *vector.items, \
|
||||
&(vector.count), &(vector.max))
|
||||
#define mie_vector_destroy(vector, dtor) \
|
||||
__mie_vector_destroy( \
|
||||
(void **)&(vector.items), sizeof *vector.items, \
|
||||
&(vector.count), &(vector.max), dtor)
|
||||
|
||||
/* use these functions if you're accessing a vector as a reference
|
||||
* via MIE_VECTOR_REF_PARAM. */
|
||||
#define mie_vector_ref_push_back(vector, ptr) \
|
||||
__mie_vector_push_back( \
|
||||
(void **)(vector), ptr, sizeof *ptr, (vector##_count), \
|
||||
@@ -47,18 +74,15 @@
|
||||
__mie_vector_emplace_back( \
|
||||
(void **)(vector), sizeof **vector, (vector##_count), \
|
||||
(vector##_max))
|
||||
|
||||
#define mie_vector_destroy(vector, dtor) \
|
||||
__mie_vector_destroy( \
|
||||
(void **)&(vector), sizeof *vector, &(vector##_count), \
|
||||
&(vector##_max), dtor)
|
||||
#define mie_vector_ref_destroy(vector, dtor) \
|
||||
__mie_vector_destroy( \
|
||||
(void **)(vector), sizeof **vector, (vector##_count), \
|
||||
(vector##_max), dtor)
|
||||
|
||||
/* don't use these functions */
|
||||
MIE_API int __mie_vector_push_back(
|
||||
void **vector, void *item, size_t item_size, size_t *count, size_t *max);
|
||||
void **vector, const void *item, size_t item_size, size_t *count,
|
||||
size_t *max);
|
||||
MIE_API void __mie_vector_pop_back(
|
||||
void **vector, size_t item_size, size_t *count, size_t *max);
|
||||
MIE_API void *__mie_vector_emplace_back(
|
||||
|
||||
@@ -186,7 +186,7 @@ bool mie_parser_parse_operand(
|
||||
|
||||
bool mie_parser_parse_operand_list(
|
||||
struct mie_parser *ctx,
|
||||
MIE_VECTOR_PARAM(struct mie_unresolved_operand, out))
|
||||
MIE_VECTOR_REF_PARAM(struct mie_unresolved_operand, out))
|
||||
{
|
||||
bool ok = false;
|
||||
struct mie_unresolved_operand *operand = NULL;
|
||||
@@ -264,7 +264,7 @@ bool mie_parser_parse_register(
|
||||
|
||||
bool mie_parser_parse_register_list(
|
||||
struct mie_parser *ctx, struct mie_name_map *names,
|
||||
MIE_VECTOR_PARAM(struct mie_register, out))
|
||||
MIE_VECTOR_REF_PARAM(struct mie_register, out))
|
||||
{
|
||||
bool ok = false;
|
||||
struct mie_register *reg = NULL;
|
||||
@@ -344,7 +344,7 @@ bool mie_parser_parse_region(struct mie_parser *ctx, struct mie_region *region)
|
||||
}
|
||||
|
||||
bool mie_parser_parse_region_list(
|
||||
struct mie_parser *ctx, MIE_VECTOR_PARAM(struct mie_region, out))
|
||||
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(struct mie_region, out))
|
||||
{
|
||||
if (!mie_parser_check_symbol(ctx, MIE_SYM_LEFT_BRACE)) {
|
||||
return false;
|
||||
@@ -382,7 +382,7 @@ bool mie_parser_parse_attribute(
|
||||
}
|
||||
|
||||
bool mie_parser_parse_attribute_list(
|
||||
struct mie_parser *ctx, MIE_VECTOR_PARAM(struct mie_op_attribute, out))
|
||||
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(struct mie_op_attribute, out))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -394,7 +394,7 @@ bool mie_parser_parse_successor(
|
||||
}
|
||||
|
||||
bool mie_parser_parse_successor_list(
|
||||
struct mie_parser *ctx, MIE_VECTOR_PARAM(struct mie_op_successor, out))
|
||||
struct mie_parser *ctx, MIE_VECTOR_REF_PARAM(struct mie_op_successor, out))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -424,7 +424,7 @@ static bool parse_generic_op(
|
||||
|
||||
MIE_VECTOR_DEFINE(struct mie_unresolved_operand, operands);
|
||||
|
||||
if (mie_parser_parse_operand_list(ctx, MIE_VECTOR_ARG(operands))) {
|
||||
if (mie_parser_parse_operand_list(ctx, MIE_VECTOR_REF(operands))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -434,7 +434,7 @@ static bool parse_generic_op(
|
||||
|
||||
if (mie_parser_parse_symbol(ctx, MIE_SYM_LEFT_BRACKET)) {
|
||||
if (!mie_parser_parse_successor_list(
|
||||
ctx, MIE_VECTOR_ARG(dest->op_successors))) {
|
||||
ctx, MIE_VECTOR_REF(dest->op_successors))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -445,7 +445,7 @@ static bool parse_generic_op(
|
||||
|
||||
if (mie_parser_parse_symbol(ctx, MIE_SYM_LEFT_PAREN)) {
|
||||
if (!mie_parser_parse_region_list(
|
||||
ctx, MIE_VECTOR_ARG(dest->op_regions))) {
|
||||
ctx, MIE_VECTOR_REF(dest->op_regions))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -456,7 +456,7 @@ static bool parse_generic_op(
|
||||
|
||||
if (mie_parser_parse_symbol(ctx, MIE_SYM_LEFT_BRACE)) {
|
||||
if (!mie_parser_parse_attribute_list(
|
||||
ctx, MIE_VECTOR_ARG(dest->op_attrib))) {
|
||||
ctx, MIE_VECTOR_REF(dest->op_attrib))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ bool mie_parser_parse_op(
|
||||
|
||||
if (mie_parser_check_type(ctx, MIE_TOK_MREGNAME | MIE_TOK_VREGNAME)) {
|
||||
if (!mie_parser_parse_register_list(
|
||||
ctx, names, MIE_VECTOR_ARG(dest->op_result))) {
|
||||
ctx, names, MIE_VECTOR_REF(dest->op_result))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ static int vector_reserve(struct vector *v, size_t new_capacity)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int vector_push_back(struct vector *v, void *item)
|
||||
static int vector_push_back(struct vector *v, const void *item)
|
||||
{
|
||||
int err = vector_reserve(v, v->v_count + 1);
|
||||
if (err != 0) {
|
||||
@@ -108,7 +108,8 @@ static void vector_destroy(struct vector *v, void (*dtor)(void *))
|
||||
}
|
||||
|
||||
int __mie_vector_push_back(
|
||||
void **vector, void *item, size_t item_size, size_t *count, size_t *max)
|
||||
void **vector, const void *item, size_t item_size, size_t *count,
|
||||
size_t *max)
|
||||
{
|
||||
struct vector v = {};
|
||||
int err = 0;
|
||||
|
||||
Reference in New Issue
Block a user