X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=idl%2Ftypes.h;h=e3a0eade8eb133cbabb9406f2fea54e0c97bdf39;hp=b57f1db31588e9472a44a61de1309ef37264801a;hb=69008ba2a7733358974c8985caa2e5bcb1182fe1;hpb=6ed7825ca4ddea88b00968e20cbaeeb1e8eb3de4 diff --git a/idl/types.h b/idl/types.h index b57f1db..e3a0ead 100644 --- a/idl/types.h +++ b/idl/types.h @@ -1,13 +1,23 @@ +typedef int token_t; + +typedef struct PSI_Token { + token_t type; + unsigned line; + size_t size; + char text[1]; +} PSI_Token; + typedef struct decl_type { - text *name; + char *name; token_t type; + struct decl_type *real; } decl_type; -static inline decl_type *init_decl_type(token_t type, text *name) { +static inline decl_type *init_decl_type(token_t type, char *name) { decl_type *t = malloc(sizeof(*t)); t->type = type; - t->name = (text *) strdup((const char *) name); + t->name = strdup(name); return t; } @@ -16,14 +26,56 @@ static inline void free_decl_type(decl_type *type) { free(type); } +typedef struct decl_typedef { + char *alias; + decl_type *type; +} decl_typedef; + +static inline decl_typedef *init_decl_typedef(char *name, decl_type *type) { + decl_typedef *t = malloc(sizeof(*t)); + t->alias = strdup(name); + t->type = type; + return t; +} + +static inline void free_decl_typedef(decl_typedef *t) { + free(t->alias); + free_decl_type(t->type); + free(t); +} + +typedef struct decl_typedefs { + size_t count; + decl_typedef **list; +} decl_typedefs; + +static decl_typedefs *add_decl_typedef(decl_typedefs *defs, decl_typedef *def) { + if (!defs) { + defs = calloc(1, sizeof(*defs)); + } + defs->list = realloc(defs->list, ++defs->count * sizeof(*defs->list)); + defs->list[defs->count-1] = def; + return defs; +} + +static void free_decl_typedefs(decl_typedefs *defs) { + size_t i; + + for (i = 0; i < defs->count; ++i) { + free_decl_typedef(defs->list[i]); + } + free(defs->list); + free(defs); +} + typedef struct decl_var { - text *name; + char *name; unsigned pointer_level; } decl_var; -static inline decl_var *init_decl_var(text *name, unsigned pl) { +static inline decl_var *init_decl_var(char *name, unsigned pl) { decl_var *v = malloc(sizeof(*v)); - v->name = (text *) strdup((const char *) name); + v->name = (char *) strdup((const char *) name); v->pointer_level = pl; return v; } @@ -109,34 +161,77 @@ static inline void free_decl_args(decl_args *args) { free(args); } +typedef struct decl_abi { + char *convention; +} decl_abi; + +static inline decl_abi *init_decl_abi(char *convention) { + decl_abi *abi = malloc(sizeof(*abi)); + abi->convention = strdup(convention); + return abi; +} + +static inline void free_decl_abi(decl_abi *abi) { + free(abi->convention); + free(abi); +} + typedef struct decl { + decl_abi *abi; decl_arg *func; decl_args *args; + void *dlptr; } decl; -static inline decl* init_decl(decl_arg *func, decl_args *args) { +static inline decl* init_decl(decl_abi *abi, decl_arg *func, decl_args *args) { decl *d = malloc(sizeof(*d)); + d->abi = abi; d->func = func; d->args = args; return d; } static inline void free_decl(decl *d) { + free_decl_abi(d->abi); free_decl_arg(d->func); free_decl_args(d->args); free(d); } +typedef struct decls { + size_t count; + decl **list; +} decls; + +static inline decls *add_decl(decls *decls, decl *decl) { + if (!decls) { + decls = calloc(1, sizeof(*decls)); + } + decls->list = realloc(decls->list, ++decls->count * sizeof(*decls->list)); + decls->list[decls->count-1] = decl; + return decls; +} + +static inline void free_decls(decls *decls) { + size_t i; + + for (i = 0; i < decls->count; ++i) { + free_decl(decls->list[i]); + } + free(decls->list); + free(decls); +} + typedef struct impl_type { - text *name; + char *name; token_t type; } impl_type; -static inline impl_type *init_impl_type(token_t type, text *name) { +static inline impl_type *init_impl_type(token_t type, char *name) { impl_type *t = malloc(sizeof(*t)); t->type = type; - t->name = (text *) strdup((const char *) name); + t->name = (char *) strdup((const char *) name); return t; } @@ -146,13 +241,13 @@ static inline void free_impl_type(impl_type *type) { } typedef struct impl_var { - text *name; + char *name; unsigned reference:1; } impl_var; -static inline impl_var *init_impl_var(text *name, int is_reference) { +static inline impl_var *init_impl_var(char *name, int is_reference) { impl_var *var = malloc(sizeof(*var)); - var->name = (text *) strdup((const char *) name); + var->name = (char *) strdup((const char *) name); var->reference = is_reference; return var; } @@ -212,9 +307,14 @@ typedef struct impl_args { static inline impl_args *init_impl_args(impl_arg *arg) { impl_args *args = malloc(sizeof(*args)); - args->count = 1; args->args = malloc(sizeof(*args->args)); - args->args[0] = arg; + if (arg) { + args->count = 1; + args->args[0] = arg; + } else { + args->count = 0; + args->args = NULL; + } return args; } @@ -235,15 +335,15 @@ static inline void free_impl_args(impl_args *args) { } typedef struct impl_func { - text *name; + char *name; impl_args *args; impl_type *return_type; } impl_func; -static inline impl_func *init_impl_func(text *name, impl_args *args, impl_type *type) { +static inline impl_func *init_impl_func(char *name, impl_args *args, impl_type *type) { impl_func *func = malloc(sizeof(*func)); - func->name = (text *) strdup((const char *) name); - func->args = args; + func->name = strdup(name); + func->args = args ? args : init_impl_args(NULL); func->return_type = type; return func; } @@ -257,13 +357,13 @@ static inline void free_impl_func(impl_func *f) { typedef struct let_func { token_t type; - text *name; + char *name; } let_func; -static inline let_func *init_let_func(token_t type, text *name) { +static inline let_func *init_let_func(token_t type, char *name) { let_func *func = malloc(sizeof(*func)); func->type = type; - func->name = (text *) strdup((const char *) name); + func->name = (char *) strdup((const char *) name); return func; } @@ -278,17 +378,11 @@ typedef struct let_value { unsigned null_pointer_ref:1; } let_value; -static inline let_value *init_let_value(let_func *func, impl_var *var) { +static inline let_value *init_let_value(let_func *func, impl_var *var, int null_pointer_ref) { let_value *val = malloc(sizeof(*val)); - - if (!func || !var) { - val->null_pointer_ref = 1; - } else { - val->null_pointer_ref = 0; - } + val->null_pointer_ref = null_pointer_ref; val->func = func; val->var = var; - return val; } @@ -322,13 +416,13 @@ static inline void free_let_stmt(let_stmt *stmt) { typedef struct set_func { token_t type; - text *name; + char *name; } set_func; -static inline set_func *init_set_func(token_t type, text *name) { +static inline set_func *init_set_func(token_t type, char *name) { set_func *func = malloc(sizeof(*func)); func->type = type; - func->name = (text *) strdup((const char *) name); + func->name = (char *) strdup((const char *) name); return func; } @@ -469,3 +563,55 @@ static inline void free_impl(impl *impl) { free_impl_stmts(impl->stmts); free(impl); } + +typedef struct impls { + size_t count; + impl **list; +} impls; + +static impls *add_impl(impls *impls, impl *impl) { + if (!impls) { + impls = calloc(1, sizeof(*impls)); + } + impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list)); + impls->list[impls->count-1] = impl; + return impls; +} + +static void free_impls(impls *impls) { + size_t i; + + for (i = 0; i < impls->count; ++i) { + free_impl(impls->list[i]); + } + free(impls->list); + free(impls); +} + +typedef struct PSI_Data { + decl_typedefs *defs; + decls *decls; + impls *impls; + char *lib; + char *fn; +} PSI_Data; + +EXPORT static inline void PSI_DataExchange(PSI_Data *dest, PSI_Data *src) { + memcpy(dest, src, sizeof(*dest)); + memset(src, 0, sizeof(*src)); +} + +EXPORT static inline void PSI_DataDtor(PSI_Data *data) { + if (data->defs) { + free_decl_typedefs(data->defs); + } + if (data->decls) { + free_decls(data->decls); + } + if (data->impls) { + free_impls(data->impls); + } + if (data->fn) { + free(data->fn); + } +}