2018-03-06 16:44:56 -08:00
|
|
|
#include "parsetype.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2018-04-22 20:44:08 -07:00
|
|
|
libab_result libab_parsetpe_init(libab_parsetype* type, libab_basetype* from,
|
2018-05-17 14:53:48 -07:00
|
|
|
size_t n, ...) {
|
2018-04-22 20:44:08 -07:00
|
|
|
libab_result result;
|
|
|
|
va_list params;
|
|
|
|
va_start(params, n);
|
|
|
|
result = libab_parsetype_init_va(type, from, n, params);
|
|
|
|
va_end(params);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
libab_result libab_parsetype_init_va(libab_parsetype* type,
|
|
|
|
libab_basetype* from, size_t n,
|
|
|
|
va_list args) {
|
2018-04-22 20:44:08 -07:00
|
|
|
libab_result result = LIBAB_SUCCESS;
|
|
|
|
size_t base_index = 0, param_index = 0;
|
|
|
|
int free_vec = 0;
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
if (from->count > n)
|
|
|
|
result = LIBAB_BAD_TYPE;
|
|
|
|
while (base_index < from->count && param_index < n) {
|
|
|
|
if (from->params[base_index].variant == BT_NAME) {
|
2018-04-22 20:44:08 -07:00
|
|
|
base_index++;
|
|
|
|
}
|
|
|
|
param_index++;
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
if (param_index < n) {
|
2018-04-22 20:44:08 -07:00
|
|
|
result = LIBAB_BAD_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
type->data_u.base = from;
|
2018-05-17 14:53:48 -07:00
|
|
|
type->variant = LIBABACUS_TYPE_F_RESOLVED;
|
|
|
|
if (result == LIBAB_SUCCESS && n) {
|
2018-04-22 20:44:08 -07:00
|
|
|
type->variant |= LIBABACUS_TYPE_F_PARENT;
|
|
|
|
result = libab_ref_vec_init(&type->children);
|
2018-05-17 14:53:48 -07:00
|
|
|
if (result == LIBAB_SUCCESS)
|
|
|
|
free_vec = 1;
|
2018-04-22 20:44:08 -07:00
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
while (n-- && result == LIBAB_SUCCESS) {
|
2018-04-22 20:44:08 -07:00
|
|
|
libab_ref* ref = va_arg(args, libab_ref*);
|
|
|
|
result = libab_ref_vec_insert(&type->children, ref);
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
if (free_vec) {
|
2018-04-22 20:44:08 -07:00
|
|
|
libab_ref_vec_free(&type->children);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:44:56 -08:00
|
|
|
void libab_parsetype_free(libab_parsetype* type) {
|
2018-04-21 14:09:01 -07:00
|
|
|
if (!(type->variant & LIBABACUS_TYPE_F_RESOLVED)) {
|
2018-04-17 15:49:09 -07:00
|
|
|
free(type->data_u.name);
|
|
|
|
}
|
2018-04-21 14:09:01 -07:00
|
|
|
if (type->variant & LIBABACUS_TYPE_F_PARENT) {
|
2018-04-17 22:14:07 -07:00
|
|
|
libab_ref_vec_free(&(type->children));
|
2018-03-06 16:44:56 -08:00
|
|
|
}
|
|
|
|
}
|