X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fparser.h;h=105e974a32efd407f777c734ca39d6e4711e6d1c;hb=6b2e6562e64a263a42910d28e23b6ca94f3ecb65;hp=94860c5248d0e0fd9e6076b4ae90e5e4ce5a2fe5;hpb=ef5079cd02c9d8666f6b9336853d2ab393e33467;p=m6w6%2Fext-psi diff --git a/src/parser.h b/src/parser.h index 94860c5..105e974 100644 --- a/src/parser.h +++ b/src/parser.h @@ -7,6 +7,7 @@ #include #include +#include /* fcall */ #include "parser_proc.h" @@ -29,6 +30,11 @@ typedef struct PSI_Token { static inline PSI_Token *PSI_TokenCopy(PSI_Token *src); +typedef struct zend_fcall { + zend_fcall_info fci; + zend_fcall_info_cache fcc; +} zend_fcall; + typedef union impl_val { char cval; int8_t i8; @@ -51,20 +57,22 @@ typedef union impl_val { zend_bool bval; zend_long lval; zend_string *str; + zend_fcall *cb; } zend; void *ptr; - uint8_t _dbg[sizeof(void *)]; } impl_val; typedef struct decl_type { PSI_Token *token; char *name; token_t type; - struct decl_type *real; - struct decl_struct *strct; - struct decl_union *unn; - struct decl_enum *enm; - struct decl *func; + union { + struct decl_arg *def; + struct decl_struct *strct; + struct decl_union *unn; + struct decl_enum *enm; + struct decl *func; + } real; } decl_type; static inline decl_type *init_decl_type(token_t type, const char *name) { @@ -74,11 +82,17 @@ static inline decl_type *init_decl_type(token_t type, const char *name) { return t; } -static inline decl_type *real_decl_type(decl_type *type) { - while (type->real) { - type = type->real; +static inline int weak_decl_type(decl_type *type) { + switch (type->type) { + case PSI_T_CHAR: + case PSI_T_SHORT: + case PSI_T_INT: + case PSI_T_LONG: + case PSI_T_NAME: + return type->type; + default: + return 0; } - return type; } static inline void free_decl(struct decl *decl); @@ -87,7 +101,7 @@ static inline void free_decl_type(decl_type *type) { free(type->token); } if (type->type == PSI_T_FUNCTION) { - free_decl(type->func); + free_decl(type->real.func); } free(type->name); free(type); @@ -150,9 +164,9 @@ typedef struct decl_arg { decl_type *type; decl_var *var; decl_struct_layout *layout; - struct let_stmt *let; impl_val val; void *ptr; + void *let; void *mem; } decl_arg; @@ -163,6 +177,7 @@ static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) { arg->var = var; var->arg = arg; arg->ptr = &arg->val; + arg->let = arg->ptr; return arg; } @@ -178,6 +193,13 @@ static inline void free_decl_arg(decl_arg *arg) { free(arg); } +static inline decl_type *real_decl_type(decl_type *type) { + while (weak_decl_type(type)) { + type = type->real.def->type; + } + return type; +} + typedef struct decl_typedefs { size_t count; decl_arg **list; @@ -470,6 +492,7 @@ static inline void free_impl_type(impl_type *type) { typedef struct impl_var { PSI_Token *token; char *name; + struct impl_arg *arg; unsigned reference:1; } impl_var; @@ -480,6 +503,17 @@ static inline impl_var *init_impl_var(const char *name, int is_reference) { return var; } +static inline impl_var *copy_impl_var(impl_var *var) { + impl_var *cpy = malloc(sizeof(*cpy)); + + memcpy(cpy, var, sizeof(*cpy)); + cpy->name = strdup(cpy->name); + if (cpy->token) { + cpy->token = PSI_TokenCopy(cpy->token); + } + return cpy; +} + static inline void free_impl_var(impl_var *var) { if (var->token) { free(var->token); @@ -579,6 +613,7 @@ static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_v impl_arg *arg = calloc(1, sizeof(*arg)); arg->type = type; arg->var = var; + arg->var->arg = arg; arg->def = def; return arg; } @@ -877,11 +912,35 @@ static inline void free_let_calloc(let_calloc *alloc) { free(alloc); } +typedef struct let_callback { + struct let_func *func; + struct set_values *args; + decl *decl; +} let_callback; + +static inline void free_let_func(struct let_func *func); +static inline void free_set_values(struct set_values *vals); +static inline let_callback *init_let_callback(struct let_func *func, struct set_values *args) { + let_callback *cb = calloc(1, sizeof(*cb)); + + cb->func = func; + cb->args = args; + return cb; +} + +static inline void free_let_callback(let_callback *cb) { + free_let_func(cb->func); + free_set_values(cb->args); + free(cb); +} + +typedef impl_val *(*let_func_handler)(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free); + typedef struct let_func { token_t type; char *name; impl_var *var; - impl_arg *arg; + let_func_handler handler; } let_func; static inline let_func *init_let_func(token_t type, const char *name, impl_var *var) { @@ -904,12 +963,14 @@ typedef struct let_val { PSI_LET_NULL, PSI_LET_NUMEXP, PSI_LET_CALLOC, + PSI_LET_CALLBACK, PSI_LET_FUNC, PSI_LET_TMP, } kind; union { num_exp *num; let_calloc *alloc; + let_callback *callback; let_func *func; decl_var *var; } data; @@ -932,6 +993,9 @@ static inline let_val *init_let_val(enum let_val_kind kind, void *data) { case PSI_LET_CALLOC: let->data.alloc = data; break; + case PSI_LET_CALLBACK: + let->data.callback = data; + break; case PSI_LET_FUNC: let->data.func = data; break; @@ -953,6 +1017,9 @@ static inline void free_let_val(let_val *let) { case PSI_LET_CALLOC: free_let_calloc(let->data.alloc); break; + case PSI_LET_CALLBACK: + free_let_callback(let->data.callback); + break; case PSI_LET_FUNC: free_let_func(let->data.func); break; @@ -967,8 +1034,6 @@ static inline void free_let_val(let_val *let) { typedef struct let_stmt { decl_var *var; let_val *val; - - void *ptr; } let_stmt; static inline let_stmt *init_let_stmt(decl_var *var, let_val *val) { @@ -1021,19 +1086,25 @@ typedef struct set_value { struct set_value *set; impl_val *val; } outer; - struct set_value **inner; - size_t count; + struct set_values *inner; } set_value; +typedef struct set_values { + set_value **vals; + size_t count; +} set_values; + + static inline set_value *init_set_value(set_func *func, decl_vars *vars) { set_value *val = calloc(1, sizeof(*val)); val->func = func; val->vars = vars; return val; } + +static inline set_values *add_set_value(set_values *vals, set_value *val); static inline set_value *add_inner_set_value(set_value *val, set_value *inner) { - val->inner = realloc(val->inner, ++val->count * sizeof(*val->inner)); - val->inner[val->count-1] = inner; + val->inner = add_set_value(val->inner, inner); inner->outer.set = val; return val; } @@ -1046,11 +1117,7 @@ static inline void free_set_value(set_value *val) { free_decl_vars(val->vars); } if (val->inner && (!val->outer.set || val->outer.set->inner != val->inner)) { - size_t i; - for (i = 0; i < val->count; ++i) { - free_set_value(val->inner[i]); - } - free(val->inner); + free_set_values(val->inner); } if (val->num) { free_num_exp(val->num); @@ -1058,6 +1125,37 @@ static inline void free_set_value(set_value *val) { free(val); } +static inline set_values *init_set_values(set_value *val) { + set_values *vals = calloc(1, sizeof(*vals)); + if (val) { + vals->count = 1; + vals->vals = calloc(1, sizeof(val)); + vals->vals[0] = val; + } + return vals; +} + +static inline set_values *add_set_value(set_values *vals, set_value *val) { + if (!vals) { + vals = calloc(1, sizeof(*vals)); + } + vals->vals = realloc(vals->vals, ++vals->count * sizeof(val)); + vals->vals[vals->count-1] = val; + return vals; +} + +static inline void free_set_values(set_values *vals) { + if (vals->vals) { + size_t i; + + for (i = 0; i < vals->count; ++i) { + free_set_value(vals->vals[i]); + } + free(vals->vals); + } + free(vals); +} + typedef struct set_stmt { impl_var *var; set_value *val;