Add a function to construct and wrap a parsetype in a refcount.

This commit is contained in:
Danila Fedorin 2018-04-22 20:44:30 -07:00
parent db84019846
commit ac26c4dbd0
2 changed files with 50 additions and 0 deletions

View File

@ -56,5 +56,14 @@ libab_result libab_copy_string(char** destination, const char* source);
*/ */
libab_result libab_resolve_parsetype(libab_parsetype* to_resolve, libab_result libab_resolve_parsetype(libab_parsetype* to_resolve,
libab_table* scope); libab_table* scope);
/**
* Creates a new type instance, and stores it into the given reference.
* @param to_instantiate the basetype to instantiate.
* @param into the reference to store the new type into.
* @param n the number of type parameters.
* @return the result of the instantiation.
*/
libab_result libab_instantiate_basetype(libab_basetype* to_instantiate,
libab_ref* into, size_t n, ...);
#endif #endif

View File

@ -1,5 +1,6 @@
#include "util.h" #include "util.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
libab_result libab_convert_lex_result(liblex_result to_convert) { libab_result libab_convert_lex_result(liblex_result to_convert) {
libab_result result = LIBAB_SUCCESS; libab_result result = LIBAB_SUCCESS;
@ -80,3 +81,43 @@ libab_result libab_resolve_parsetype(libab_parsetype* to_resolve,
return result; return result;
} }
void _libab_free_parsetype(void* parsetype) {
libab_parsetype_free(parsetype);
free(parsetype);
}
void _libab_parsetype_free(void* parsetype) {
libab_parsetype_free(parsetype);
free(parsetype);
}
libab_result libab_instantiate_basetype(libab_basetype* to_instantiate,
libab_ref* into, size_t n, ...) {
libab_result result = LIBAB_SUCCESS;
libab_parsetype* parsetype;
va_list params;
va_start(params, n);
if((parsetype = malloc(sizeof(*parsetype)))) {
result = libab_parsetype_init_va(parsetype, to_instantiate, n, params);
} else {
result = LIBAB_MALLOC;
}
if(result == LIBAB_SUCCESS) {
result = libab_ref_new(into, parsetype, _libab_parsetype_free);
if(result != LIBAB_SUCCESS) {
libab_parsetype_free(parsetype);
}
}
if(result != LIBAB_SUCCESS) {
free(parsetype);
}
va_end(params);
return result;
}