From 2f5af21b263403997e154658635d6b6e6eaab453 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 11 Oct 2016 11:30:05 +0200 Subject: [PATCH] types refactoring --- Makefile.frag | 1 + config.m4 | 4 +- php_psi.h | 4 - src/calc.c | 2 + src/calc.h | 3 + src/context.c | 271 +++++- src/context.h | 8 +- src/context_dump.c | 585 ----------- src/context_validate.c | 1672 -------------------------------- src/data.h | 1 + src/engine.c | 100 +- src/engine.h | 20 - src/error.c | 36 + src/error.h | 42 + src/libffi.c | 2 + src/libjit.c | 2 + src/marshal.c | 111 ++- src/marshal.h | 35 +- src/module.c | 2 + src/parser_def.h | 4 - src/parser_def.h.bak | 1206 +++++++++++++++++++++++ src/parser_proc.y | 1 - src/parser_proc.y.bak | 885 +++++++++++++++++ src/token.c | 25 + src/token.h | 53 +- src/types.h | 32 - src/types/const_type.c | 52 + src/types/const_type.h | 45 +- src/types/constant.c | 64 ++ src/types/constant.h | 52 +- src/types/constants.c | 65 ++ src/types/constants.h | 52 +- src/types/decl.c | 127 +++ src/types/decl.h | 58 +- src/types/decl_abi.c | 74 ++ src/types/decl_abi.h | 49 +- src/types/decl_arg.c | 151 +++ src/types/decl_arg.h | 67 +- src/types/decl_args.c | 199 ++++ src/types/decl_args.h | 71 +- src/types/decl_callinfo.h | 29 +- src/types/decl_enum.c | 94 ++ src/types/decl_enum.h | 57 +- src/types/decl_enum_item.c | 54 ++ src/types/decl_enum_item.h | 23 +- src/types/decl_enum_items.c | 83 ++ src/types/decl_enum_items.h | 67 +- src/types/decl_enums.c | 69 ++ src/types/decl_enums.h | 54 +- src/types/decl_file.c | 71 ++ src/types/decl_file.h | 43 +- src/types/decl_libs.c | 55 ++ src/types/decl_libs.h | 48 +- src/types/decl_struct.c | 154 +++ src/types/decl_struct.h | 62 +- src/types/decl_struct_layout.c | 51 + src/types/decl_struct_layout.h | 42 +- src/types/decl_structs.c | 67 ++ src/types/decl_structs.h | 52 +- src/types/decl_type.c | 270 ++++++ src/types/decl_type.h | 76 +- src/types/decl_typedefs.c | 98 ++ src/types/decl_typedefs.h | 56 +- src/types/decl_union.c | 144 +++ src/types/decl_union.h | 57 +- src/types/decl_unions.c | 66 ++ src/types/decl_unions.h | 52 +- src/types/decl_var.c | 86 ++ src/types/decl_var.h | 64 +- src/types/decl_vars.c | 58 ++ src/types/decl_vars.h | 59 +- src/types/decls.c | 64 ++ src/types/decls.h | 52 +- src/types/free_call.c | 64 ++ src/types/free_call.h | 50 +- src/types/free_calls.c | 60 ++ src/types/free_calls.h | 59 +- src/types/free_stmt.c | 129 +++ src/types/free_stmt.h | 47 +- src/types/impl.c | 64 ++ src/types/impl.h | 52 +- src/types/impl_arg.c | 53 + src/types/impl_arg.h | 57 +- src/types/impl_args.c | 84 ++ src/types/impl_args.h | 67 +- src/types/impl_def_val.c | 56 ++ src/types/impl_def_val.h | 45 +- src/types/impl_func.c | 87 ++ src/types/impl_func.h | 54 +- src/types/impl_stmt.c | 60 ++ src/types/impl_stmt.h | 62 +- src/types/impl_stmts.c | 142 +++ src/types/impl_stmts.h | 88 +- src/types/impl_type.c | 48 + src/types/impl_type.h | 45 +- src/types/impl_val.h | 36 +- src/types/impl_var.c | 75 ++ src/types/impl_var.h | 61 +- src/types/impls.c | 64 ++ src/types/impls.h | 52 +- src/types/let_callback.c | 77 ++ src/types/let_callback.h | 53 +- src/types/let_calloc.c | 48 + src/types/let_calloc.h | 45 +- src/types/let_func.c | 154 +++ src/types/let_func.h | 61 +- src/types/let_stmt.c | 128 +++ src/types/let_stmt.h | 57 +- src/types/let_val.c | 276 ++++++ src/types/let_val.h | 110 ++- src/types/let_vals.c | 65 ++ src/types/let_vals.h | 64 +- src/types/num_exp.c | 233 +++++ src/types/num_exp.h | 108 +-- src/types/return_stmt.c | 106 ++ src/types/return_stmt.h | 53 +- src/types/set_func.c | 51 + src/types/set_func.h | 48 +- src/types/set_stmt.c | 124 +++ src/types/set_stmt.h | 53 +- src/types/set_value.c | 251 +++++ src/types/set_value.h | 75 +- src/types/set_values.c | 66 ++ src/types/set_values.h | 66 +- tests/ndbm/ndbm.psi | 15 +- 125 files changed, 9260 insertions(+), 3498 deletions(-) delete mode 100644 src/context_dump.c delete mode 100644 src/context_validate.c create mode 100644 src/error.c create mode 100644 src/error.h create mode 100644 src/parser_def.h.bak create mode 100644 src/parser_proc.y.bak create mode 100644 src/types/const_type.c create mode 100644 src/types/constant.c create mode 100644 src/types/constants.c create mode 100644 src/types/decl.c create mode 100644 src/types/decl_abi.c create mode 100644 src/types/decl_arg.c create mode 100644 src/types/decl_args.c create mode 100644 src/types/decl_enum.c create mode 100644 src/types/decl_enum_item.c create mode 100644 src/types/decl_enum_items.c create mode 100644 src/types/decl_enums.c create mode 100644 src/types/decl_file.c create mode 100644 src/types/decl_libs.c create mode 100644 src/types/decl_struct.c create mode 100644 src/types/decl_struct_layout.c create mode 100644 src/types/decl_structs.c create mode 100644 src/types/decl_type.c create mode 100644 src/types/decl_typedefs.c create mode 100644 src/types/decl_union.c create mode 100644 src/types/decl_unions.c create mode 100644 src/types/decl_var.c create mode 100644 src/types/decl_vars.c create mode 100644 src/types/decls.c create mode 100644 src/types/free_call.c create mode 100644 src/types/free_calls.c create mode 100644 src/types/free_stmt.c create mode 100644 src/types/impl.c create mode 100644 src/types/impl_arg.c create mode 100644 src/types/impl_args.c create mode 100644 src/types/impl_def_val.c create mode 100644 src/types/impl_func.c create mode 100644 src/types/impl_stmt.c create mode 100644 src/types/impl_stmts.c create mode 100644 src/types/impl_type.c create mode 100644 src/types/impl_var.c create mode 100644 src/types/impls.c create mode 100644 src/types/let_callback.c create mode 100644 src/types/let_calloc.c create mode 100644 src/types/let_func.c create mode 100644 src/types/let_stmt.c create mode 100644 src/types/let_val.c create mode 100644 src/types/let_vals.c create mode 100644 src/types/num_exp.c create mode 100644 src/types/return_stmt.c create mode 100644 src/types/set_func.c create mode 100644 src/types/set_stmt.c create mode 100644 src/types/set_value.c create mode 100644 src/types/set_values.c diff --git a/Makefile.frag b/Makefile.frag index bcdaa40..4efd8b3 100644 --- a/Makefile.frag +++ b/Makefile.frag @@ -19,6 +19,7 @@ psi-clean-headers: .PHONY: psi-clean-sources psi-clean-sources: -rm -f $(PHP_PSI_BUILDDIR)/src/*o + -rm -f $(PHP_PSI_BUILDDIR)/src/types/*o -rm -f $(PHP_PSI_SRCDIR)/src/parser.c $(PHP_PSI_SRCDIR)/src/parser_proc.c $(PHP_PSI_SRCDIR)/src/parser_proc.y .PHONY: psi-clean-aux diff --git a/config.m4 b/config.m4 index f9cf24d..47ee1b0 100644 --- a/config.m4 +++ b/config.m4 @@ -111,14 +111,16 @@ if test "$PHP_PSI" != no; then AC_DEFINE_UNQUOTED([PSI_VA_DECLS], [$PSI_VA_DECLS], [Predefined vararg functions]) PHP_ADD_INCLUDE($PHP_PSI_SRCDIR/src) + PHP_ADD_INCLUDE($PHP_PSI_SRCDIR/src/types) PHP_ADD_BUILD_DIR($PHP_PSI_BUILDDIR/src) + PHP_ADD_BUILD_DIR($PHP_PSI_BUILDDIR/src/types) PHP_PSI_HEADERS=`(cd $PHP_PSI_SRCDIR/src && ls *.h types/*.h)` # parser* should come first PHP_PSI_SOURCES=" \ src/parser_proc.c \ src/parser.c \ - `(cd $PHP_PSI_SRCDIR && ls src/*.c | $EGREP -v '^src/parser')` \ + `(cd $PHP_PSI_SRCDIR && ls src/*.c src/types/*.c | $EGREP -v '^src/parser')` \ " PHP_NEW_EXTENSION(psi, $PHP_PSI_SOURCES, $ext_shared) diff --git a/php_psi.h b/php_psi.h index 84dc1b7..3dedff2 100644 --- a/php_psi.h +++ b/php_psi.h @@ -20,10 +20,6 @@ extern zend_module_entry psi_module_entry; #include "context.h" -void psi_error_wrapper(void *context, struct psi_token *t, int type, const char *msg, ...); -void psi_error(int type, const char *fn, unsigned ln, const char *msg, ...); -void psi_verror(int type, const char *fn, unsigned ln, const char *msg, va_list argv); - static inline int psi_check_env(const char *var) { char *set = getenv(var); return (set && *set && '0' != *set); diff --git a/src/calc.c b/src/calc.c index f701b5a..806685e 100644 --- a/src/calc.c +++ b/src/calc.c @@ -1,5 +1,7 @@ #ifdef HAVE_CONFIG_H # include "config.h" +#else +# include "php_config.h" #endif #include "php.h" diff --git a/src/calc.h b/src/calc.h index d00aec0..db8fd7c 100644 --- a/src/calc.h +++ b/src/calc.h @@ -1,6 +1,9 @@ #ifndef _PSI_CALC_H #define _PSI_CALC_H +#include "num_exp.h" +#include "impl_val.h" + int psi_calc_num_exp(num_exp *exp, impl_val *strct, impl_val *res); static inline zend_long psi_long_num_exp(num_exp *exp, impl_val *strct) { diff --git a/src/context.c b/src/context.c index fe2b5bc..b7ae46f 100644 --- a/src/context.c +++ b/src/context.c @@ -1,5 +1,7 @@ #ifdef HAVE_CONFIG_H # include "config.h" +#else +# include "php_config.h" #endif #include "php_psi_stdinc.h" @@ -42,7 +44,7 @@ #include "php_psi_structs.h" #include "php_psi_unions.h" -struct psi_context *psi_context_init(struct psi_context *C, struct psi_context_ops *ops, psi_context_error_func error, unsigned flags) +struct psi_context *psi_context_init(struct psi_context *C, struct psi_context_ops *ops, psi_error_cb error, unsigned flags) { struct psi_data T; struct psi_predef_type *predef_type; @@ -296,6 +298,11 @@ zend_function_entry *psi_context_compile(struct psi_context *C) case PSI_T_FLOAT: convert_to_double(&zc.value); break; + case PSI_T_STRING: + case PSI_T_QUOTED_STRING: + break; + default: + assert(0); } zend_register_constant(&zc); } @@ -406,3 +413,265 @@ void psi_context_free(struct psi_context **C) *C = NULL; } } + +int psi_context_validate(struct psi_context *C, struct psi_parser *P) +{ + struct psi_data *D; + void *dlopened = NULL; + size_t i, count = C->count++, check_round, check_count; + decl_typedefs *check_defs = P->defs; + decl_structs *check_structs = P->structs; + decl_unions *check_unions = P->unions; + decl_enums *check_enums = P->enums; + unsigned flags = C->flags; + + C->data = realloc(C->data, C->count * sizeof(*C->data)); + D = psi_data_exchange(&C->data[count], PSI_DATA(P)); + +#define REVALIDATE(what) do { \ + if (check_round && check_ ##what) { \ + free(check_ ##what->list); \ + free(check_ ##what); \ + } \ + check_ ##what = recheck_ ##what; \ +} while (0) +#define CHECK_TOTAL (CHECK_COUNT(defs) + CHECK_COUNT(structs) + CHECK_COUNT(enums)) +#define CHECK_COUNT(of) (check_ ##of ? check_ ##of->count : 0) + + if (!(flags & PSI_PARSER_SILENT)) { + /* no warnings on first round */ + C->flags |= PSI_PARSER_SILENT; + } + for (check_round = 0, check_count = 0; CHECK_TOTAL && check_count != CHECK_TOTAL; ++check_round) { + decl_typedefs *recheck_defs = NULL; + decl_structs *recheck_structs = NULL; + decl_unions *recheck_unions = NULL; + decl_enums *recheck_enums = NULL; + + check_count = CHECK_TOTAL; + + for (i = 0; i < CHECK_COUNT(defs); ++i) { + if (validate_decl_typedef(PSI_DATA(C), check_defs->list[i])) { + C->defs = add_decl_typedef(C->defs, check_defs->list[i]); + } else { + recheck_defs = add_decl_typedef(recheck_defs, check_defs->list[i]); + } + } + for (i = 0; i < CHECK_COUNT(structs); ++i) { + if (validate_decl_struct(PSI_DATA(C), check_structs->list[i])) { + C->structs = add_decl_struct(C->structs, check_structs->list[i]); + } else { + recheck_structs = add_decl_struct(recheck_structs, check_structs->list[i]); + } + } + for (i = 0; i < CHECK_COUNT(unions); ++i) { + if (validate_decl_union(PSI_DATA(C), check_unions->list[i])) { + C->unions = add_decl_union(C->unions, check_unions->list[i]); + } else { + recheck_unions = add_decl_union(recheck_unions, check_unions->list[i]); + } + } + for (i = 0; i < CHECK_COUNT(enums); ++i) { + if (validate_decl_enum(PSI_DATA(C), check_enums->list[i])) { + C->enums = add_decl_enum(C->enums, check_enums->list[i]); + } else { + recheck_enums = add_decl_enum(recheck_enums, check_enums->list[i]); + } + } + + REVALIDATE(defs); + REVALIDATE(structs); + REVALIDATE(unions); + REVALIDATE(enums); + + if (check_round == 0 && !(flags & PSI_PARSER_SILENT)) { + C->flags ^= PSI_PARSER_SILENT; + } + } + + C->flags = flags; + + if (D->consts) { + for (i = 0; i < D->consts->count; ++i) { + if (validate_constant(PSI_DATA(C), D->consts->list[i])) { + C->consts = add_constant(C->consts, D->consts->list[i]); + } + } + } + + if (!validate_file(D, &dlopened)) { + return 0; + } + + add_decl_lib(&C->psi.libs, dlopened); + + if (D->decls) { + for (i = 0; i < D->decls->count; ++i) { + if (validate_decl(PSI_DATA(C), dlopened, D->decls->list[i])) { + C->decls = add_decl(C->decls, D->decls->list[i]); + } + } + } + if (D->impls) { + for (i = 0; i < D->impls->count; ++i) { + if (validate_impl(PSI_DATA(C), D->impls->list[i])) { + C->impls = add_impl(C->impls, D->impls->list[i]); + } + } + } + + return 1; +} + +int psi_context_validate_data(struct psi_data *dest, struct psi_data *source) +{ + size_t i; + int errors = 0; + + if (source->defs) for (i = 0; i < source->defs->count; ++i) { + decl_arg *def = source->defs->list[i]; + + if (validate_decl_typedef(source, def)) { + if (dest) { + dest->defs = add_decl_typedef(dest->defs, def); + } + } else { + ++errors; + } + } + + if (source->consts) for (i = 0; i < source->consts->count; ++i) { + constant *constant = source->consts->list[i]; + + if (validate_constant(source, constant)) { + if (dest) { + dest->consts = add_constant(dest->consts, constant); + } + } else { + ++errors; + } + } + + if (source->structs) for (i = 0; i < source->structs->count; ++i) { + decl_struct *dstruct = source->structs->list[i]; + + if (validate_decl_struct(source, dstruct)) { + if (dest) { + dest->structs = add_decl_struct(dest->structs, dstruct); + } + } else { + ++errors; + } + } + + if (source->unions) for (i = 0; i < source->unions->count; ++i) { + decl_union *dunion = source->unions->list[i]; + + if (validate_decl_union(source, dunion)) { + if (dest) { + dest->unions = add_decl_union(dest->unions, dunion); + } + } else { + ++errors; + } + } + + if (source->enums) for (i = 0; i < source->enums->count; ++i) { + decl_enum *denum = source->enums->list[i]; + + if (validate_decl_enum(source, denum)) { + if (dest) { + dest->enums = add_decl_enum(dest->enums, denum); + } + } else { + ++errors; + } + } + + if (source->decls) for (i = 0; i < source->decls->count; ++i) { + decl *decl = source->decls->list[i]; + + if (validate_decl(source, NULL, decl)) { + if (dest) { + dest->decls = add_decl(dest->decls, decl); + } + } else { + ++errors; + } + } + + if (source->impls) for (i = 0; i < source->impls->count; ++i) { + impl *impl = source->impls->list[i]; + + if (validate_impl(source, impl)) { + if (dest) { + dest->impls = add_impl(dest->impls, impl); + } + } else { + ++errors; + } + } + + return errors; +} + +static inline void dump_data(int fd, struct psi_data *D) { + if (D->psi.file.fn) { + dprintf(fd, "// psi.filename=%s\n", D->psi.file.fn); + if (D->psi.file.ln) { + dprintf(fd, "lib \"%s\";\n", D->psi.file.ln); + } + } else { + dprintf(fd, "// builtin predef\n"); + } + if (D->defs) { + dump_decl_typedefs(fd, D->defs); + dprintf(fd, "\n"); + } + if (D->unions) { + dump_decl_unions(fd, D->unions); + dprintf(fd, "\n"); + } + if (D->structs) { + dump_decl_structs(fd, D->structs); + dprintf(fd, "\n"); + } + if (D->enums) { + dump_decl_enums(fd, D->enums); + dprintf(fd, "\n"); + } + if (D->consts) { + dump_constants(fd, D->consts); + dprintf(fd, "\n"); + } + if (D->decls) { + dump_decls(fd, D->decls); + dprintf(fd, "\n"); + } + if (D->impls) { + dump_impls(fd, D->impls); + dprintf(fd, "\n"); + } +} + +void psi_context_dump(struct psi_context *C, int fd) +{ + size_t i; + +#ifdef HAVE_LIBJIT + if (C->ops == psi_libjit_ops()) { + dprintf(fd, "// psi.engine=jit\n"); + } +#endif +#ifdef HAVE_LIBFFI + if (C->ops == psi_libffi_ops()) { + dprintf(fd, "// psi.engine=ffi\n"); + } +#endif + dprintf(fd, "\n"); + + for (i = 0; i < C->count; ++i) { + dump_data(fd, &C->data[i]); + } + +} diff --git a/src/context.h b/src/context.h index 98516ac..d132256 100644 --- a/src/context.h +++ b/src/context.h @@ -7,12 +7,6 @@ struct psi_parser; struct decl_callinfo; struct impl_vararg; -#define PSI_PRINT_POINTER_LEVEL(l) l,"************************" -#define PSI_ERROR 16 -#define PSI_WARNING 32 -typedef void (*psi_context_error_func)(void *context, struct psi_token *token, int type, const char *msg, ...); - - struct psi_context_ops { void (*init)(struct psi_context *C); void (*dtor)(struct psi_context *C); @@ -31,7 +25,7 @@ struct psi_context { size_t count; }; -struct psi_context *psi_context_init(struct psi_context *C, struct psi_context_ops *ops, psi_context_error_func error, unsigned flags); +struct psi_context *psi_context_init(struct psi_context *C, struct psi_context_ops *ops, psi_error_cb error, unsigned flags); void psi_context_build(struct psi_context *C, const char *path); int psi_context_validate(struct psi_context *C, struct psi_parser *P); int psi_context_validate_data(struct psi_data *C, struct psi_data *D); diff --git a/src/context_dump.c b/src/context_dump.c deleted file mode 100644 index b84f6c8..0000000 --- a/src/context_dump.c +++ /dev/null @@ -1,585 +0,0 @@ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#include "php.h" -#include "php_psi.h" - -#include "types.h" - -#include "libjit.h" -#include "libffi.h" - -static inline void dump_level(int fd, unsigned level) { - dprintf(fd, "%.*s", level > 30 ? 30 : level, - "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"); -} - -static inline void dump_decl_arg(int fd, decl_arg *darg, unsigned level); -static inline void dump_struct_args(int fd, decl_args *args, unsigned level); -static inline void dump_enum_items(int fd, decl_enum_items *items, unsigned level); - -#define is_anon_type(name, type) !strncmp(name, type "@", sizeof(type)) - -static inline void dump_decl_type(int fd, decl_type *t, unsigned level) { - switch (t->type) { - case PSI_T_POINTER: - dprintf(fd, "%s *", t->name); - return; - - case PSI_T_STRUCT: - dprintf(fd, "struct "); - if (!strncmp(t->name, "struct@", sizeof("struct"))) { - dump_struct_args(fd, t->real.strct->args, level); - return; - } - break; - - case PSI_T_ENUM: - dprintf(fd, "enum "); - if (!strncmp(t->name, "enum@", sizeof("enum"))) { - dump_enum_items(fd, t->real.enm->items, level); - return; - } - break; - - case PSI_T_UNION: - dprintf(fd, "union "); - if (!strncmp(t->name, "union@", sizeof("union"))) { - dump_struct_args(fd, t->real.unn->args, level); - return; - } - break; - } - dprintf(fd, "%s", t->name); -} - -static inline void dump_decl_var(int fd, decl_var *v) { - dprintf(fd, "%.*s%s", v->pointer_level-!!v->array_size, "**********", v->name); - if (v->array_size) { - dprintf(fd, "[%u]", v->array_size); - } -} - -static inline void dump_decl_arg(int fd, decl_arg *a, unsigned level) { - if (a->type->type == PSI_T_FUNCTION) { - dump_decl_type(fd, a->type->real.func->func->type, level); - dprintf(fd, " ("); - dump_decl_var(fd, a->var); - dprintf(fd, ")("); - if (a->type->real.func->args) { - size_t j; - - for (j = 0; j < a->type->real.func->args->count; ++j) { - if (j) { - dprintf(fd, ", "); - } - dump_decl_arg(fd, a->type->real.func->args->args[j], level+1); - } - if (a->type->real.func->args->varargs) { - dprintf(fd, ", ..."); - } - } - dprintf(fd, ")"); - } else { - dump_decl_type(fd, a->type, level); - dprintf(fd, " "); - dump_decl_var(fd, a->var); - } -} - -static inline void dump_num_exp(int fd, num_exp *exp) { - while (exp) { - switch (exp->t) { - case PSI_T_NUMBER: - dprintf(fd, "%s", exp->u.numb); - break; - case PSI_T_NSNAME: - dprintf(fd, "%s", exp->u.cnst->name); - break; - case PSI_T_NAME: - dump_decl_var(fd, exp->u.dvar); - break; - case PSI_T_ENUM: - dprintf(fd, "%s", exp->u.enm->name); - break; - EMPTY_SWITCH_DEFAULT_CASE(); - } - if (exp->operand) { - char op; - - switch (exp->operator) { - case PSI_T_PLUS: op = '+'; break; - case PSI_T_MINUS: op = '-'; break; - case PSI_T_ASTERISK:op = '*'; break; - case PSI_T_SLASH: op = '/'; break; - EMPTY_SWITCH_DEFAULT_CASE(); - } - dprintf(fd, " %c ", op); - } - exp = exp->operand; - } -} - -static inline void dump_impl_set_value(int fd, set_value *set, unsigned level, int last) { - size_t i; - - if (level > 1) { - /* only if not directly after `set ...` */ - dump_level(fd, level); - } - - if (set->func->type == PSI_T_ELLIPSIS) { - dprintf(fd, "%s(", set->outer.set->func->name); - } else { - dprintf(fd, "%s(", set->func->name); - } - - for (i = 0; i < set->vars->count; ++i) { - decl_var *svar = set->vars->vars[i]; - if (i) { - dprintf(fd, ", "); - } - dump_decl_var(fd, svar); - } - - if (set->func->type == PSI_T_ELLIPSIS) { - dprintf(fd, ", ..."); - } - if (set->num) { - dprintf(fd, ", "); - dump_num_exp(fd, set->num); - } - if (set->inner && set->inner->vals && set->func->type != PSI_T_ELLIPSIS) { - dprintf(fd, ",\n"); - for (i = 0; i < set->inner->count; ++i) { - dump_impl_set_value(fd, set->inner->vals[i], level+1, i == (set->inner->count - 1)); - } - /* only if inner stmts, i.e. with new lines, were dumped */ - dump_level(fd, level); - } - if (level > 1) { - dprintf(fd, ")%s\n", last ? "" : ","); - } else { - dprintf(fd, ");"); - } -} - -static inline void dump_typedef(int fd, decl_arg *tdef) { - dprintf(fd, "typedef "); - dump_decl_arg(fd, tdef, 0); - dprintf(fd, ";"); -} - -static inline void dump_typedefs(int fd, decl_typedefs *defs) { - size_t i; - - for (i = 0; i < defs->count; ++i) { - decl_arg *tdef = defs->list[i]; - - dump_typedef(fd, tdef); - dprintf(fd, "\n"); - } -} - -static inline void dump_struct_args(int fd, decl_args *args, unsigned level) { - size_t j; - - dprintf(fd, " {\n"); - if (args) { - ++level; - for (j = 0; j < args->count; ++j) { - decl_arg *sarg = args->args[j]; - - dump_level(fd, level); - dump_decl_arg(fd, sarg, level); - dprintf(fd, "::(%zu, %zu);\n", sarg->layout->pos, sarg->layout->len); - } - --level; - } - dump_level(fd, level); - dprintf(fd, "}"); -} - -static inline void dump_struct(int fd, decl_struct *strct) { - dprintf(fd, "struct %s::(%zu, %zu)", strct->name, strct->align, strct->size); - if (strct->args && strct->args->count) { - dump_struct_args(fd, strct->args, 0); - } else { - dprintf(fd, ";"); - } -} - -static inline void dump_structs(int fd, decl_structs *structs) { - size_t i; - - for (i = 0; i < structs->count; ++i) { - decl_struct *strct = structs->list[i]; - - if (!is_anon_type(strct->name, "struct")) { - dump_struct(fd, strct); - dprintf(fd, "\n"); - } - } -} - -static inline void dump_union(int fd, decl_union *unn) { - size_t j; - - dprintf(fd, "union %s::(%zu, %zu) {\n", unn->name, unn->align, unn->size); - for (j = 0; j < unn->args->count; ++j) { - decl_arg *uarg = unn->args->args[j]; - - dprintf(fd, "\t"); - dump_decl_arg(fd, uarg, 0); - dprintf(fd, "::(%zu, %zu);\n", uarg->layout->pos, uarg->layout->len); - } - dprintf(fd, "}"); -} - -static inline void dump_unions(int fd, decl_unions *unions) { - size_t i; - - for (i = 0; i < unions->count; ++i) { - decl_union *unn = unions->list[i]; - - if (!is_anon_type(unn->name, "union")) { - dump_union(fd, unn); - dprintf(fd, "\n"); - } - } -} - -static inline void dump_enum_items(int fd, decl_enum_items *items, unsigned level) { - size_t j; - - if (items) { - ++level; - for (j = 0; j < items->count; ++j) { - decl_enum_item *i = items->list[j]; - - if (j) { - dprintf(fd, ",\n"); - } - dump_level(fd, level); - dprintf(fd, "%s", i->name); - if (i->num && i->num != &i->inc) { - dprintf(fd, " = "); - dump_num_exp(fd, i->num); - } - } - --level; - } -} - -static inline void dump_enum(int fd, decl_enum *e) { - dprintf(fd, "enum %s {\n", e->name); - dump_enum_items(fd, e->items, 0); - dprintf(fd, "\n}"); -} - -static inline void dump_enums(int fd, decl_enums *enums) { - size_t i; - - for (i = 0; i < enums->count; ++i) { - decl_enum *e = enums->list[i]; - - if (!is_anon_type(e->name, "enum")) { - dump_enum(fd, e); - dprintf(fd, "\n"); - } - } -} -static inline void dump_constant(int fd, constant *cnst) { - dprintf(fd, "const %s %s = ", cnst->type->name, cnst->name); - if (cnst->val->type == PSI_T_QUOTED_STRING) { - dprintf(fd, "\"%s\";", cnst->val->text); - } else { - dprintf(fd, "%s;", cnst->val->text); - } -} - -static inline void dump_constants(int fd, constants *consts) { - size_t i; - - for (i = 0; i < consts->count; ++i) { - constant *cnst = consts->list[i]; - - dump_constant(fd, cnst); - dprintf(fd, "\n"); - } -} - -static inline void dump_decl(int fd, decl *decl) { - size_t j; - - dprintf(fd, "%s ", decl->abi->convention); - dump_decl_arg(fd, decl->func, 0); - dprintf(fd, "("); - if (decl->args) { - for (j = 0; j < decl->args->count; ++j) { - if (j) { - dprintf(fd, ", "); - } - dump_decl_arg(fd, decl->args->args[j], 0); - } - if (decl->args->varargs) { - dprintf(fd, ", ..."); - } - } - dprintf(fd, ");"); -} - -static inline void dump_decls(int fd, decls *decls) { - size_t i; - - for (i = 0; i < decls->count; ++i) { - decl *decl = decls->list[i]; - - dump_decl(fd, decl); - dprintf(fd, "\n"); - } -} - -static inline void dump_impl_func(int fd, impl_func *func) { - size_t j; - - dprintf(fd, "function %s(", func->name); - if (func->args) { - for (j = 0; j < func->args->count; ++j) { - impl_arg *iarg = func->args->args[j]; - - dprintf(fd, "%s%s %s%s", - j ? ", " : "", - iarg->type->name, - iarg->var->reference ? "&" : "", - iarg->var->name); - if (iarg->def) { - dprintf(fd, " = %s", iarg->def->text); - } - } - if (func->args->vararg.name) { - impl_arg *vararg = func->args->vararg.name; - - dprintf(fd, ", %s %s...%s", - vararg->type->name, - vararg->var->reference ? "&" : "", - vararg->var->name); - } - } - dprintf(fd, ") : %s%s", - func->return_reference ? "&":"", - func->return_type->name); -} - -static inline void dump_impl_let_func(int fd, let_func *func, unsigned level); - -static inline void dump_impl_let_val(int fd, let_val *val, unsigned level, int last) { - if (level > 1) { - /* only if not directly after `set ...` */ - dump_level(fd, level); - } - - dprintf(fd, "%s", val->flags.one.is_reference ? "&" : ""); - switch (val->kind) { - case PSI_LET_NULL: - dprintf(fd, "NULL"); - break; - case PSI_LET_TMP: - dump_decl_var(fd, val->data.var); - break; - case PSI_LET_CALLOC: - dprintf(fd, "calloc("); - dump_num_exp(fd, val->data.alloc->nmemb); - dprintf(fd, ", "); - dump_num_exp(fd, val->data.alloc->size); - dprintf(fd, ")"); - break; - case PSI_LET_CALLBACK: - dprintf(fd, "callback %s(%s(", val->data.callback->func->name, - val->data.callback->func->var->name); - if (val->data.callback->args) { - size_t i, c = val->data.callback->args->count; - - dprintf(fd, "\n"); - for (i = 0; i < c; ++i) { - set_value *set = val->data.callback->args->vals[i]; - ++level; - dump_impl_set_value(fd, set, level, i + 1 == c); - --level; - } - dump_level(fd, level); - } - dprintf(fd, "))"); - break; - case PSI_LET_FUNC: - dump_impl_let_func(fd, val->data.func, level); - break; - case PSI_LET_NUMEXP: - dump_num_exp(fd, val->data.num); - break; - - EMPTY_SWITCH_DEFAULT_CASE(); - } - if (level > 1) { - if (!last) { - dprintf(fd, ","); - } - } else { - dprintf(fd, ";"); - } -} - -static inline void dump_impl_let_func(int fd, let_func *func, unsigned level) { - dprintf(fd, "%s(%s", func->name, - func->var->name); - if (func->inner) { - size_t i; - - dprintf(fd, ","); - ++level; - for (i = 0; i < func->inner->count; ++i) { - dprintf(fd, "\n"); - dump_impl_let_val(fd, func->inner->vals[i], level, i+1 == func->inner->count); - } - --level; - dprintf(fd, "\n"); - dump_level(fd, level); - } - dprintf(fd, ")"); -} -static inline void dump_impl_let_stmt(int fd, let_stmt *let) { - dprintf(fd, "\tlet %s", let->var->name); - if (let->val) { - dprintf(fd, " = "); - dump_impl_let_val(fd, let->val, 1, 1); - } -} - -static inline void dump_impl_return_stmt(int fd, return_stmt *ret) { - dprintf(fd, "\treturn "); - dump_impl_set_value(fd, ret->set, 1, 0); -} - -static inline void dump_impl_set_stmt(int fd, set_stmt *set) { - dprintf(fd, "\tset %s = ", set->var->name); - dump_impl_set_value(fd, set->val, 1, 0); -} - -static inline void dump_impl_free_call(int fd, free_call *call) { - size_t l; - - dprintf(fd, "%s(", call->func); - for (l = 0; l < call->vars->count; ++l) { - decl_var *fvar = call->vars->vars[l]; - - dump_decl_var(fd, fvar); - } - dprintf(fd, ");"); -} - -static inline void dump_impl_free_stmt(int fd, free_stmt *fre) { - size_t k; - - dprintf(fd, "\tfree "); - for (k = 0; k < fre->calls->count; ++k) { - free_call *call = fre->calls->list[k]; - - if (k) { - dprintf(fd, ", "); - } - dump_impl_free_call(fd, call); - dprintf(fd, "\n"); - } -} -static inline void dump_impl_stmts(int fd, impl_stmts *stmts) { - size_t j; - - for (j = 0; j < stmts->let.count; ++j) { - let_stmt *let = stmts->let.list[j]; - dump_impl_let_stmt(fd, let); - dprintf(fd, "\n"); - } - for (j = 0; j < stmts->ret.count; ++j) { - return_stmt *ret = stmts->ret.list[j]; - dump_impl_return_stmt(fd, ret); - dprintf(fd, "\n"); - } - for (j = 0; j < stmts->set.count; ++j) { - set_stmt *set = stmts->set.list[j]; - - dump_impl_set_stmt(fd, set); - dprintf(fd, "\n"); - } - for (j = 0; j < stmts->fre.count; ++j) { - free_stmt *fre = stmts->fre.list[j]; - - dump_impl_free_stmt(fd, fre); - dprintf(fd, "\n"); - } -} - -static inline void dump_impl(int fd, impl *impl) { - - dump_impl_func(fd, impl->func); - dprintf(fd, " {\n"); - if (impl->stmts) { - dump_impl_stmts(fd, impl->stmts); - } - dprintf(fd, "}"); -} - -static inline void dump_impls(int fd, impls *impls) { - size_t i; - - for (i = 0; i < impls->count; ++i) { - impl *impl = impls->list[i]; - - dump_impl(fd, impl); - dprintf(fd, "\n"); - } -} - -void psi_context_dump(struct psi_context *C, int fd) -{ -#ifdef HAVE_LIBJIT - if (C->ops == psi_libjit_ops()) { - dprintf(fd, "// psi.engine=jit\n"); - } -#endif -#ifdef HAVE_LIBFFI - if (C->ops == psi_libffi_ops()) { - dprintf(fd, "// psi.engine=ffi\n"); - } -#endif - dprintf(fd, "\n"); - - if (C->defs) { - dump_typedefs(fd, C->defs); - dprintf(fd, "\n"); - } - if (C->unions) { - dump_unions(fd, C->unions); - dprintf(fd, "\n"); - } - if (C->structs) { - dump_structs(fd, C->structs); - dprintf(fd, "\n"); - } - if (C->enums) { - dump_enums(fd, C->enums); - dprintf(fd, "\n"); - } - if (C->consts) { - dump_constants(fd, C->consts); - dprintf(fd, "\n"); - } - if (C->decls) { - dump_decls(fd, C->decls); - dprintf(fd, "\n"); - } - if (C->impls) { - dump_impls(fd, C->impls); - dprintf(fd, "\n"); - } -} diff --git a/src/context_validate.c b/src/context_validate.c deleted file mode 100644 index 3e99e1b..0000000 --- a/src/context_validate.c +++ /dev/null @@ -1,1672 +0,0 @@ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#include "php.h" - -#include -#include - -#include "php_psi.h" -#include "php_psi_stdinc.h" -#include "php_psi_stdtypes.h" -#include "php_psi_macros.h" -#include "php_psi_redirs.h" - -#include "parser.h" -#include "calc.h" -#include "marshal.h" -#include "engine.h" -#include "context.h" - -static int validate_lib(struct psi_data *data, void **dlopened) { - char lib[MAXPATHLEN]; - const char *ptr = data->psi.file.ln; - size_t len; - - if (!ptr) { - /* FIXME: assume stdlib */ - return 1; - } else if (!strchr(ptr, '/')) { - len = snprintf(lib, MAXPATHLEN, "lib%s.%s", ptr, PHP_PSI_SHLIB_SUFFIX); - if (MAXPATHLEN == len) { - data->error(data, NULL, PSI_WARNING, "Library name too long: '%s'", ptr); - } - lib[len] = 0; - ptr = lib; - } - if (!(*dlopened = dlopen(ptr, RTLD_LAZY|RTLD_LOCAL))) { - data->error(data, NULL, PSI_WARNING, "Could not open library '%s': %s.", - data->psi.file.ln, dlerror()); - return 0; - } - return 1; -} - -static inline int locate_decl_type_alias(decl_typedefs *defs, decl_type *type) { - size_t i; - struct psi_std_type *stdtyp; - - if (type->real.def) { - return 1; - } - if (defs) for (i = 0; i < defs->count; ++i) { - decl_arg *def = defs->list[i]; - - if (def->type->type != type->type && !strcmp(def->var->name, type->name)) { - type->real.def = def; - return 1; - } - } - for (stdtyp = &psi_std_types[0]; stdtyp->type_tag; ++stdtyp) { - if (!strcmp(type->name, stdtyp->alias ?: stdtyp->type_name)) { - type->type = stdtyp->type_tag; - return 1; - } - } - - return 0; -} - -static inline int locate_decl_type_struct(decl_structs *structs, decl_type *type) { - size_t i; - - if (type->real.strct) { - return 1; - } - if (structs) for (i = 0; i < structs->count; ++i) { - if (!strcmp(structs->list[i]->name, type->name)) { - type->real.strct = structs->list[i]; - return 1; - } - } - return 0; -} - -static inline int locate_decl_type_union(decl_unions *unions, decl_type *type) { - size_t i; - - if (type->real.unn) { - return 1; - } - if (unions) for (i = 0; i < unions->count; ++i) { - if (!strcmp(unions->list[i]->name, type->name)) { - type->real.unn = unions->list[i]; - return 1; - } - } - return 0; -} - -static inline int locate_decl_type_enum(decl_enums *enums, decl_type *type) { - size_t i; - - if (type->real.enm) { - return 1; - } - if (enums) for (i = 0; i < enums->count; ++i) { - if (!strcmp(enums->list[i]->name, type->name)) { - type->real.enm = enums->list[i]; - return 1; - } - } - return 0; -} - -static inline int locate_decl_type_decl(decls *decls, decl_type *type) { - size_t i; - - if (type->real.func) { - return 1; - } - if (decls) for (i = 0; i < decls->count; ++i) { - if (!strcmp(decls->list[i]->func->var->name, type->name)) { - type->real.func = decls->list[i]; - return 1; - } - } - - return 0; -} - -static inline int validate_decl_nodl(struct psi_data *data, decl *decl); -static inline int validate_decl_struct(struct psi_data *data, decl_struct *s); -static inline int validate_decl_union(struct psi_data *data, decl_union *u); -static inline int validate_decl_enum(struct psi_data *data, decl_enum *e); - -static inline int validate_decl_type(struct psi_data *data, decl_type *type, decl_arg *def) { - if (weak_decl_type(type)) { - if (!locate_decl_type_alias(data->defs, type)) { - return 0; - } - if (type->real.def) { - return validate_decl_type(data, type->real.def->type, type->real.def); - } - return 1; - } - - switch (type->type) { - case PSI_T_STRUCT: - if (!locate_decl_type_struct(data->structs, type)) { - return 0; - } - break; - case PSI_T_UNION: - if (!locate_decl_type_union(data->unions, type)) { - return 0; - } - break; - case PSI_T_ENUM: - if (!locate_decl_type_enum(data->enums, type)) { - return 0; - } - break; - case PSI_T_FUNCTION: - if (!locate_decl_type_decl(data->decls, type)) { - return 0; - } - if (!validate_decl_nodl(data, type->real.func)) { - return 0; - } - break; - } - return 1; -} -static inline int validate_decl_typedef(struct psi_data *data, decl_arg *def) { - if (!validate_decl_type(data, def->type, def)) { - const char *pre; - - switch (def->type->type) { - case PSI_T_STRUCT: pre = "struct "; break; - case PSI_T_UNION: pre = "union "; break; - case PSI_T_ENUM: pre = "enum "; break; - default: pre = ""; break; - } - data->error(data, def->token, PSI_WARNING, - "Type '%s' cannot be aliased to %s'%s'", - def->var->name, pre, def->type->name); - return 0; - } - if (def->type->type == PSI_T_VOID) { - if (def->var->pointer_level) { - def->type->type = PSI_T_POINTER; - } else { - data->error(data, def->token, PSI_WARNING, - "Type '%s' cannot be aliased to 'void'", - def->type->name); - return 0; - } - } - return 1; -} - -static inline int validate_constant(struct psi_data *data, constant *c) { - /* FIXME */ - return 1; -} - -static inline int validate_decl_arg(struct psi_data *data, decl_arg *arg) { - if (!validate_decl_type(data, arg->type, NULL)) { - data->error(data, arg->type->token, PSI_WARNING, - "Cannot use '%s' as type for '%s'", - arg->type->name, arg->var->name); - return 0; - } - return 1; -} - -static int psi_sort_struct_arg_cmp(const void *_a, const void *_b) { - decl_arg *a = *(decl_arg **)_a, *b = *(decl_arg **)_b; - - if (a->layout->pos == b->layout->pos) { - if (a->layout->len == b->layout->len) { - return 0; - } else if (a->layout->len > b->layout->len) { - return -1; - } else { - return 1; - } - } else if (a->layout->pos > b->layout->pos) { - return 1; - } else { - return -1; - } -} -static void psi_sort_struct_arg_swp(void *a, void *b) { - decl_arg **_a = a, **_b = b, *_c; - - _c = *_b; - *_b = *_a; - *_a = _c; -} -static inline void psi_sort_struct_args(void **args, size_t count) { - zend_insert_sort(args, count, sizeof(*args), - psi_sort_struct_arg_cmp, psi_sort_struct_arg_swp); -} - -static inline int validate_decl_struct_darg(struct psi_data *data, decl_arg *darg, void *current) { - decl_type *real = real_decl_type(darg->type); - - /* pre-validate any structs/unions/enums */ - switch (real->type) { - case PSI_T_STRUCT: - if (current && current == real->real.strct) { - return 1; - } - if (!locate_decl_type_struct(data->structs, real)) { - return 0; - } - if (!validate_decl_struct(data, real->real.strct)) { - return 0; - } - break; - case PSI_T_UNION: - if (current && current == real->real.unn) { - return 1; - } - if (!locate_decl_type_union(data->unions, real)) { - return 0; - } - if (!validate_decl_union(data, real->real.unn)) { - return 0; - } - break; - case PSI_T_ENUM: - if (current && current == real->real.enm) { - return 1; - } - if (!locate_decl_type_enum(data->enums, real)) { - return 0; - } - if (!validate_decl_enum(data, real->real.enm)) { - return 0; - } - break; - } - - return 1; -} - -static inline size_t sizeof_decl_arg(decl_arg *darg) { - size_t size; - decl_type *real = real_decl_type(darg->type); - - if (darg->var->array_size) { - if (darg->var->pointer_level > 2) { - size = psi_t_size(PSI_T_POINTER) * darg->var->array_size; - } else { - size = psi_t_size(real->type) * darg->var->array_size; - } - } else if (darg->var->pointer_level) { - size = psi_t_size(PSI_T_POINTER); - } else { - switch (real->type) { - case PSI_T_UNION: - size = real->real.unn->size; - break; - case PSI_T_STRUCT: - size = real->real.strct->size; - break; - case PSI_T_ENUM: - default: - size = psi_t_size(real->type); - break; - } - } - - ZEND_ASSERT(size); - - return size; -} - -static inline size_t alignof_decl_type(decl_type *t); -static inline size_t alignof_decl_arg(decl_arg *darg); -static inline size_t alignof_decl_union(decl_union *u); -static inline size_t alignof_decl_struct(decl_struct *s); - -static inline size_t alignof_decl_args(decl_args *args) { - size_t i, maxalign = 0; - - for (i = 0; i < args->count; ++i) { - decl_arg *darg = args->args[i]; - size_t align = alignof_decl_arg(darg); - - if (align > maxalign) { - maxalign = align; - } - } - - return maxalign; -} - -static inline size_t alignof_decl_struct(decl_struct *s) { - if (!s->align) { - s->align = alignof_decl_args(s->args); - } - return s->align; -} - -static inline size_t alignof_decl_union(decl_union *u) { - if (!u->align) { - u->align = alignof_decl_args(u->args); - } - return u->align; -} - -static inline size_t alignof_decl_type(decl_type *t) { - decl_type *real = real_decl_type(t); - size_t align; - - switch (real->type) { - case PSI_T_STRUCT: - align = alignof_decl_struct(real->real.strct); - break; - case PSI_T_UNION: - align = alignof_decl_union(real->real.unn); - break; - case PSI_T_ENUM: - default: - align = psi_t_alignment(real->type); - } - - return align; -} - -static inline size_t alignof_decl_arg(decl_arg *darg) { - size_t align; - - if (darg->var->pointer_level && (!darg->var->array_size || darg->var->pointer_level > 2)) { - align = psi_t_alignment(PSI_T_POINTER); - } else { - align = alignof_decl_type(darg->type); - } - - return align; -} - -static inline size_t align_decl_arg(decl_arg *darg, size_t *pos, size_t *len) { - size_t align = alignof_decl_arg(darg); - - ZEND_ASSERT(align); - - *len = sizeof_decl_arg(darg); - *pos = psi_align(align, *pos); - - return align; -} - -static inline int validate_decl_struct(struct psi_data *data, decl_struct *s) { - size_t i, pos, len, size, align; - - if (!s->size && !s->args->count) { - data->error(data, s->token, PSI_WARNING, - "Cannot compute size of empty struct '%s'", - s->name); - return 0; - } - - for (i = 0; i < s->args->count; ++i) { - decl_arg *darg = s->args->args[i]; - - if (!validate_decl_arg(data, darg)) { - return 0; - } - - ZEND_ASSERT(!darg->var->arg || darg->var->arg == darg); - darg->var->arg = darg; - - if (!validate_decl_struct_darg(data, darg, s)) { - return 0; - } else if (darg->layout) { - pos = darg->layout->pos; - - align = align_decl_arg(darg, &pos, &len); - - if (darg->layout->len != len) { - data->error(data, darg->token, PSI_WARNING, - "Computed size %zu of %s.%s does not match" - " pre-defined size %zu of type '%s'", - len, s->name, darg->var->name, darg->layout->len, - darg->type->name); - } - if (darg->layout->pos != pos) { - data->error(data, darg->token, PSI_WARNING, - "Computed offset %zu of %s.%s does not match" - " pre-defined offset %zu", - pos, s->name, darg->var->name, darg->layout->pos); - } - } else { - if (i) { - pos = s->args->args[i-1]->layout->pos + - s->args->args[i-1]->layout->len; - } else { - pos = 0; - } - - align = align_decl_arg(darg, &pos, &len); - darg->layout = init_decl_struct_layout(pos, len); - } - - if (align > s->align) { - s->align = align; - } - } - - psi_sort_struct_args((void **) s->args->args, s->args->count); - - if (s->args->count) { - decl_arg *darg = s->args->args[s->args->count-1]; - - size = darg->layout->pos + darg->layout->len; - if (s->size < size) { - s->size = psi_align(size, s->align); - } - } - - return 1; -} - -static inline int validate_decl_union(struct psi_data *data, decl_union *u) { - size_t i, pos, len, size = 0, align; - - if (!u->size && !u->args->count) { - data->error(data, u->token, PSI_WARNING, - "Cannot compute size of empty union %s", - u->name); - return 0; - } - - for (i = 0; i < u->args->count; ++i) { - decl_arg *darg = u->args->args[i]; - - if (!validate_decl_arg(data, darg)) { - return 0; - } - - ZEND_ASSERT(!darg->var->arg || darg->var->arg == darg); - darg->var->arg = darg; - - if (!validate_decl_struct_darg(data, darg, u)) { - return 0; - } else if (darg->layout) { - pos = darg->layout->pos; - - align = align_decl_arg(darg, &pos, &len); - - if (darg->layout->pos != 0) { - data->error(data, darg->token, PSI_WARNING, - "Offset of %s.%s should be 0", - u->name, darg->var->name); - darg->layout->pos = 0; - } - if (darg->layout->len != len) { - data->error(data, darg->token, PSI_WARNING, - "Computed size %zu of %s.%s does not match" - " pre-defined size %zu of type '%s'", - len, u->name, darg->var->name, darg->layout->len, - darg->type->name); - } - } else { - pos = 0; - - align = align_decl_arg(darg, &pos, &len); - darg->layout = init_decl_struct_layout(pos, len); - - } - if (len > size) { - size = len; - } - if (align > u->align) { - u->align = align; - } - } - - psi_sort_struct_args((void **) u->args->args, u->args->count); - - if (u->size < size) { - u->size = psi_align(size, u->align); - } - - return 1; -} - -static const char * const abi_ccs[] = { - "default", /* \ */ - "extern", /* > - all the same */ - "cdecl", /* / */ - "mscdecl", - "stdcall", - "fastcall", -}; -static inline int validate_decl_abi(struct psi_data *data, decl_abi *abi) { - size_t i; - - for (i = 0; i < sizeof(abi_ccs)/sizeof(char*); ++i) { - if (strcasecmp(abi->convention, abi_ccs[i])) { - return 1; - } - } - return 0; -} -static inline int validate_decl_func(struct psi_data *data, void *dl, decl *decl, decl_arg *func) -{ - struct psi_func_redir *redir; - - if (!strcmp(func->var->name, "dlsym")) { - data->error(data, func->token, PSI_WARNING, "Cannot dlsym dlsym (sic!)"); - return 0; - } - - for (redir = &psi_func_redirs[0]; redir->name; ++redir) { - if (!strcmp(func->var->name, redir->name)) { - decl->call.sym = redir->func; - } - } - if (!decl->call.sym) { -#ifndef RTLD_NEXT -# define RTLD_NEXT ((void *) -1l) -#endif - decl->call.sym = dlsym(dl ?: RTLD_NEXT, func->var->name); - if (!decl->call.sym) { - data->error(data, func->token, PSI_WARNING, - "Failed to locate symbol '%s': %s", - func->var->name, dlerror() ?: "not found"); - } - } - return 1; -} -static inline int validate_decl_nodl(struct psi_data *data, decl *decl) { - if (!validate_decl_abi(data, decl->abi)) { - data->error(data, decl->abi->token, PSI_WARNING, - "Invalid calling convention: '%s'", decl->abi->token->text); - return 0; - } - if (!validate_decl_arg(data, decl->func)) { - return 0; - } - if (decl->args) { - size_t i; - - for (i = 0; i < decl->args->count; ++i) { - if (!validate_decl_arg(data, decl->args->args[i])) { - return 0; - } - } - } - return 1; -} -static inline int validate_decl(struct psi_data *data, void *dl, decl *decl) { - if (!validate_decl_nodl(data, decl)) { - return 0; - } - if (!validate_decl_func(data, dl, decl, decl->func)) { - return 0; - } - return 1; -} -static inline decl_arg *locate_decl_arg(decl_args *args, const char *name) { - size_t i; - - if (args) for (i = 0; i < args->count; ++i) { - decl_arg *arg = args->args[i]; - - if (!strcmp(name, arg->var->name)) { - return arg; - } - } - - return NULL; -} -static inline decl_arg *locate_decl_var_arg(decl_var *var, decl_args *args, decl_arg *func) { - decl_arg *arg = locate_decl_arg(args, var->name); - - if (arg) { - ZEND_ASSERT(!var->arg || var->arg == arg); - return var->arg = arg; - } - - if (func && !strcmp(var->name, func->var->name)) { - return var->arg = func; - } - - return NULL; -} -static inline decl_arg *locate_struct_member(decl_struct *s, decl_var *var) { - if (s->args) { - return locate_decl_var_arg(var, s->args, NULL); - } - - return NULL; -} -static inline decl_arg *locate_union_member(decl_union *u, decl_var *var) { - if (u->args) { - return locate_decl_var_arg(var, u->args, NULL); - } - - return NULL; -} -static inline constant *locate_num_exp_constant(num_exp *exp, constants *consts) { - size_t i; - - for (i = 0; i < consts->count; ++i) { - constant *cnst = consts->list[i]; - - if (!strcmp(cnst->name, exp->u.numb)) { - free(exp->u.numb); - return exp->u.cnst = cnst; - } - } - - return NULL; -} -static inline decl_enum_item *locate_num_exp_enum_item_ex(num_exp *exp, decl_enum *e) { - size_t k; - - if (e) for (k = 0; k < e->items->count; ++k) { - decl_enum_item *i = e->items->list[k]; - - if (!strcmp(i->name, exp->u.dvar->name)) { - free_decl_var(exp->u.dvar); - exp->t = PSI_T_ENUM; - exp->u.enm = i; - return i; - } - } - return NULL; -} -static inline decl_enum_item *locate_num_exp_enum_item(num_exp *exp, decl_enums *enums) { - size_t j; - - if (enums) for (j = 0; j < enums->count; ++j) { - decl_enum *e = enums->list[j]; - decl_enum_item *i = locate_num_exp_enum_item_ex(exp, e); - - if (i) { - return i; - } - } - return NULL; -} -static inline int validate_num_exp(struct psi_data *data, num_exp *exp, decl_args *dargs, decl_arg *func, decl_enum *enm) { - if (exp->operand) { - switch (exp->operator) { - case PSI_T_PLUS: - exp->calculator = psi_calc_add; - break; - case PSI_T_MINUS: - exp->calculator = psi_calc_sub; - break; - case PSI_T_ASTERISK: - exp->calculator = psi_calc_mul; - break; - case PSI_T_SLASH: - exp->calculator = psi_calc_div; - break; - EMPTY_SWITCH_DEFAULT_CASE(); - } - if (!validate_num_exp(data, exp->operand, dargs, func, enm)) { - return 0; - } - } - switch (exp->t) { - case PSI_T_NAME: - if (!locate_decl_var_arg(exp->u.dvar, dargs, func)) { - if (!locate_num_exp_enum_item(exp, data->enums) && !locate_num_exp_enum_item_ex(exp, enm)) { - data->error(data, exp->token, PSI_WARNING, "Unknown variable '%s' in numeric expression", - exp->u.dvar->name); - return 0; - } - } - return 1; - case PSI_T_NSNAME: - if (!locate_num_exp_constant(exp, data->consts)) { - data->error(data, exp->token, PSI_WARNING, "Unknown constant '%s' in numeric expression", - exp->u.numb); - return 0; - } - return 1; - case PSI_T_NUMBER: - case PSI_T_ENUM: - return 1; - default: - return 0; - } -} - -static inline int validate_decl_enum(struct psi_data *data, decl_enum *e) { - size_t j; - - if (!e->items || !e->items->count) { - data->error(data, e->token, PSI_WARNING, "Empty enum '%s'", e->name); - return 0; - } - - for (j = 0; j < e->items->count; ++j) { - decl_enum_item *i = e->items->list[j]; - - if (!i->num) { - if (j) { - i->inc.t = PSI_T_NUMBER; - i->inc.u.numb = "1"; - i->inc.operator = PSI_T_PLUS; - i->inc.operand = i->prev->num ?: &i->prev->inc; - i->num = &i->inc; - } else { - i->inc.t = PSI_T_NUMBER; - i->inc.u.numb = "0"; - i->num = &i->inc; - } - } - if (!validate_num_exp(data, i->num, NULL, NULL, e)) { - return 0; - } - } - - return 1; -} - -static inline int validate_set_value_handler(set_value *set) { - switch (set->func->type) { - case PSI_T_TO_BOOL: set->func->handler = psi_to_bool; break; - case PSI_T_TO_INT: set->func->handler = psi_to_int; break; - case PSI_T_TO_FLOAT: set->func->handler = psi_to_double; break; - case PSI_T_TO_STRING: set->func->handler = psi_to_string; break; - case PSI_T_TO_ARRAY: set->func->handler = psi_to_array; break; - case PSI_T_TO_OBJECT: set->func->handler = psi_to_object; break; - case PSI_T_VOID: set->func->handler = psi_to_void; break; - case PSI_T_ZVAL: set->func->handler = psi_to_zval; break; - case PSI_T_ELLIPSIS: - if (set->outer.set && set->outer.set->func->type == PSI_T_TO_ARRAY) { - set->func->handler = psi_to_recursive; - set->inner = set->outer.set->inner; - break; - } - /* no break */ - default: - return 0; - } - return 1; -} -static inline void decl_var_arg_v(decl_args *args, va_list argp) { - int argc; - decl_arg **argv; - - memset(args, 0, sizeof(*args)); - - while ((argc = va_arg(argp, int))) { - argv = va_arg(argp, decl_arg **); - while (argc--) { - add_decl_arg(args, *argv++); - } - } -} -static inline int validate_set_value_ex(struct psi_data *data, set_value *set, decl_arg *ref, decl_args *ref_list) { - size_t i; - decl_type *ref_type; - decl_var *set_var = set->vars->vars[0]; - - if (!validate_set_value_handler(set)) { - data->error(data, set->func->token, PSI_WARNING, "Invalid cast '%s' in `set` statement", set->func->name); - return 0; - } - - for (i = 0; i < set->vars->count; ++i) { - decl_var *svar = set->vars->vars[i]; - if (!svar->arg && !locate_decl_var_arg(svar, ref_list, NULL)) { - data->error(data, svar->token, PSI_WARNING, "Unknown variable '%s' in `set` statement", svar->name); - return 0; - } - } - - if (!ref) { - ref = set_var->arg; - } - ref_type = real_decl_type(ref->type); - - if (set->inner && set->inner->count) { - int is_to_array = (set->func->type == PSI_T_TO_ARRAY); - int is_pointer_to_struct = (ref_type->type == PSI_T_STRUCT && ref->var->pointer_level); - - if (!is_to_array && !is_pointer_to_struct) { - data->error(data, set->func->token, E_WARNING, "Inner `set` statement casts only work with " - "to_array() casts on structs or pointers: %s(%s...", set->func->name, set->vars->vars[0]->name); - return 0; - } - } - if (set->num) { - if (!validate_num_exp(data, set->num, ref_list, ref, NULL)) { - return 0; - } - } - - if (set->inner && (ref_type->type == PSI_T_STRUCT || ref_type->type == PSI_T_UNION)) { - /* to_array(struct, to_...) */ - if (!set->outer.set || set->outer.set->inner->vals != set->inner->vals) { - for (i = 0; i < set->inner->count; ++i) { - decl_var *sub_var = set->inner->vals[i]->vars->vars[0]; - decl_arg *sub_ref; - - switch (ref_type->type) { - case PSI_T_STRUCT: - sub_ref = locate_struct_member(ref_type->real.strct, sub_var); - break; - case PSI_T_UNION: - sub_ref = locate_union_member(ref_type->real.unn, sub_var); - break; - } - - if (sub_ref) { - if (!validate_set_value_ex(data, set->inner->vals[i], sub_ref, ref_type->real.strct->args)) { - return 0; - } - } - } - } - } else if (set->inner && set->inner->count == 1) { - /* to_array(ptr, to_string(*ptr)) */ - decl_var *sub_var = set->inner->vals[0]->vars->vars[0]; - decl_arg *sub_ref = locate_decl_var_arg(sub_var, ref_list, ref); - - if (sub_ref) { - if (strcmp(sub_var->name, set_var->name)) { - data->error(data, sub_var->token, E_WARNING, "Inner `set` statement casts on pointers must reference the same variable"); - return 0; - } - if (!validate_set_value_ex(data, set->inner->vals[0], sub_ref, ref_list)) { - return 0; - } - } - } else if (set->inner && set->inner->count > 1) { - data->error(data, set->func->token, E_WARNING, - "Inner `set` statement casts on pointers may only occur once, " - "unless the outer type is a scruct or union, got '%s%.*s'", - ref_type->name, PSI_PRINT_POINTER_LEVEL(ref->var->pointer_level)); - return 0; - } - - return 1; -} -static inline int validate_set_value(struct psi_data *data, set_value *set, ...) { - va_list argp; - decl_args args = {0}; - int check; - - va_start(argp, set); - decl_var_arg_v(&args, argp); - va_end(argp); - - check = validate_set_value_ex(data, set, NULL, &args); - if (args.args) { - free(args.args); - } - return check; -} -static inline decl *locate_impl_decl(decls *decls, return_stmt *ret) { - if (decls) { - size_t i; - - for (i = 0; i < decls->count; ++i) { - if (!strcmp(decls->list[i]->func->var->name, ret->set->vars->vars[0]->name)) { - ret->decl = decls->list[i]->func; - return decls->list[i]; - } - } - } - - return NULL; -} -static inline int validate_impl_ret_stmt(struct psi_data *data, impl *impl) { - return_stmt *ret; - - /* we must have exactly one ret stmt delcaring the native func to call */ - /* and which type cast to apply */ - if (impl->stmts->ret.count != 1) { - if (impl->stmts->ret.count > 1) { - data->error(data, impl->stmts->ret.list[1]->token, PSI_WARNING, - "Too many `return` statements for implmentation %s;" - " found %zu, exactly one is needed", - impl->func->name, impl->stmts->ret.count); - } else { - data->error(data, impl->func->token, PSI_WARNING, - "Missing `return` statement for implementation %s", - impl->func->name); - } - return 0; - } - - ret = impl->stmts->ret.list[0]; - - if (!(impl->decl = locate_impl_decl(data->decls, ret))) { - data->error(data, ret->token, PSI_WARNING, - "Missing declaration '%s' for `return` statment for implementation %s", - ret->set->vars->vars[0]->name, impl->func->name); - return 0; - } - - if (!validate_set_value(data, ret->set, 1, &ret->decl, impl->decl->args ? (int) impl->decl->args->count : 0, impl->decl->args ? impl->decl->args->args : NULL, 0)) { - return 0; - } - - //impl->decl->impl = impl; - - return 1; -} - -static inline impl_arg *locate_impl_var_arg(impl_var *var, impl_args *args) { - size_t i; - - for (i = 0; i < args->count; ++i) { - impl_arg *iarg = args->args[i]; - - if (!strcmp(var->name, iarg->var->name)) { - return var->arg = iarg; - } - } - - return NULL; -} - -static inline int validate_let_val(struct psi_data *data, let_val *val, decl_var *let_var, impl *impl); - -static inline const char *locate_let_val_varname(let_val *val) { - if (val) { - switch (val->kind) { - case PSI_LET_CALLBACK: - return &val->data.callback->func->var->name[1]; - case PSI_LET_FUNC: - return &val->data.func->var->name[1]; - default: - break; - } - } - return NULL; -} - -static inline int validate_let_func(struct psi_data *data, let_func *func, decl_var *let_var, impl *impl) { - if (func->outer) { - - } - if (impl->func->args) { - locate_impl_var_arg(func->var, impl->func->args); - } - if (!func->var->arg && !func->ref) { - data->error(data, func->var->token, PSI_WARNING, - "Unknown variable '%s%s' of `let` statement" - " for cast '%s' of implementation '%s'", - *func->var->name == '$' ? "" : "$", - func->var->name, func->name, impl->func->name); - return 0; - } - switch (func->type) { - case PSI_T_BOOLVAL: func->handler = psi_let_boolval; break; - case PSI_T_INTVAL: func->handler = psi_let_intval; break; - case PSI_T_FLOATVAL: func->handler = psi_let_floatval; break; - case PSI_T_STRVAL: func->handler = psi_let_strval; break; - case PSI_T_STRLEN: func->handler = psi_let_strlen; break; - case PSI_T_PATHVAL: func->handler = psi_let_pathval; break; - case PSI_T_ARRVAL: func->handler = psi_let_arrval; break; - case PSI_T_OBJVAL: func->handler = psi_let_objval; break; - case PSI_T_ZVAL: func->handler = psi_let_zval; break; - case PSI_T_VOID: func->handler = psi_let_void; break; - EMPTY_SWITCH_DEFAULT_CASE(); - } - - if (func->inner) { - size_t i; - decl_type *var_typ = real_decl_type(let_var->arg->type); - decl_args *sub_args; - - switch (var_typ->type) { - case PSI_T_STRUCT: - sub_args = var_typ->real.strct->args; - break; - case PSI_T_UNION: - sub_args = var_typ->real.unn->args; - break; - default: - data->error(data, let_var->token, PSI_WARNING, - "Inner let statement's values must refer to a structure type, got '%s' for '%s'", - real_decl_type(let_var->arg->type)->name, let_var->name); - return 0; - } - - for (i = 0; i < func->inner->count; ++i) { - let_val *inner = func->inner->vals[i]; - let_val *outer = func->outer; - const char *name = locate_let_val_varname(inner); - decl_arg *sub_arg; - - if (name) { - sub_arg = locate_decl_arg(sub_args, name); - } - if (!name || !sub_arg) { - data->error(data, let_var->token, PSI_WARNING, - "Unknown variable '%s' of '%s'", - name, - var_typ->real.strct->name); - return 0; - } - if (!validate_let_val(data, inner, sub_arg->var, impl)) { - return 0; - } - } - } - return 1; -} - -static inline int validate_let_callback(struct psi_data *data, decl_var *cb_var, let_callback *cb, impl *impl) { - size_t i; - decl *cb_func; - decl_type *cb_type = real_decl_type(cb_var->arg->type); - - if (!validate_let_func(data, cb->func, cb_var, impl)) { - return 0; - } - - if (cb_type->type != PSI_T_FUNCTION) { - data->error(data, cb_var->token, PSI_WARNING, "Not a function: %s", cb_var->name); - return 0; - } - cb_func = cb_type->real.func; - for (i = 0; i < cb->args->count; ++i) { - if (!validate_set_value(data, cb->args->vals[i], cb_func->args->count, cb_func->args->args, 0)) { - return 0; - } - } - - if (!validate_decl_nodl(data, cb_func)) { - return 0; - } - - cb->decl = cb_func; - - return 1; -} - -static inline int validate_let_val(struct psi_data *data, let_val *val, decl_var *let_var, impl *impl) { - - switch (val->kind) { - case PSI_LET_NULL: - break; - case PSI_LET_TMP: - if (!let_var) { - data->error(data, NULL, PSI_WARNING, - "Ivalid let statement value of implementation '%s'", - impl->func->name); - return 0; - } - /* e.g. let bar = &strval($bar); // decl_arg(char **bar) */ - /* e.g. let foo = *bar; */ - let_var->pointer_level = val->data.var->pointer_level; - let_var->arg = init_decl_arg( - init_decl_type( - real_decl_type(val->data.var->arg->type)->type, - real_decl_type(val->data.var->arg->type)->name), - init_decl_var( - let_var->name, - let_var->pointer_level, - let_var->array_size)); - break; - case PSI_LET_NUMEXP: - if (!validate_num_exp(data, val->data.num, impl->decl->args, impl->decl->func, NULL)) { - return 0; - } - break; - case PSI_LET_CALLOC: - if (!validate_num_exp(data, val->data.alloc->nmemb, impl->decl->args, impl->decl->func, NULL)) { - return 0; - } - if (!validate_num_exp(data, val->data.alloc->size, impl->decl->args, impl->decl->func, NULL)) { - return 0; - } - break; - case PSI_LET_CALLBACK: - if (!let_var) { - data->error(data, NULL, PSI_WARNING, - "Ivalid let statement value of implementation '%s'", - impl->func->name); - return 0; - } - if (val->data.callback->func->inner) { - size_t i; - decl_type *var_typ = real_decl_type(let_var->arg->type); - decl_args *sub_args; - - switch (var_typ->type) { - case PSI_T_STRUCT: - sub_args = var_typ->real.strct->args; - break; - case PSI_T_UNION: - sub_args = var_typ->real.unn->args; - break; - default: - data->error(data, let_var->token, PSI_WARNING, - "Inner let statement's values must refer to a structure type, got '%s' for '%s'", - real_decl_type(let_var->arg->type)->name, let_var->name); - return 0; - } - for (i = 0; i < val->data.callback->func->inner->count; ++i) { - let_val *inner = val->data.callback->func->inner->vals[i]; - switch (inner->kind) { - case PSI_LET_FUNC: - inner->data.func->outer = val; - inner->data.func->ref = locate_decl_arg(sub_args, - &inner->data.func->var->name[1]); - break; - - break; - case PSI_LET_CALLBACK: - inner->data.callback->func->outer = val; - inner->data.callback->func->ref = locate_decl_arg(sub_args, - &inner->data.callback->func->var->name[1]); - break; - } - } - } - if (!validate_let_callback(data, let_var, val->data.callback, impl)) { - return 0; - } - break; - case PSI_LET_FUNC: - if (val->data.func->inner) { - size_t i; - decl_type *var_typ = real_decl_type(let_var->arg->type); - decl_args *sub_args; - - switch (var_typ->type) { - case PSI_T_STRUCT: - sub_args = var_typ->real.strct->args; - break; - case PSI_T_UNION: - sub_args = var_typ->real.unn->args; - break; - default: - data->error(data, let_var->token, PSI_WARNING, - "Inner let statement's values must refer to a structure type, got '%s' for '%s'", - real_decl_type(let_var->arg->type)->name, let_var->name); - return 0; - } - for (i = 0; i < val->data.func->inner->count; ++i) { - let_val *inner = val->data.func->inner->vals[i]; - switch (inner->kind) { - case PSI_LET_FUNC: - inner->data.func->outer = val; - inner->data.func->ref = locate_decl_arg(sub_args, - &inner->data.func->var->name[1]); - break; - case PSI_LET_CALLBACK: - inner->data.callback->func->outer = val; - inner->data.callback->func->ref = locate_decl_arg(sub_args, - &inner->data.callback->func->var->name[1]); - break; - } - } - } - - val->data.func->var; - - if (!validate_let_func(data, val->data.func, let_var, impl)) { - return 0; - } - break; - } - - return 1; -} - -static inline int validate_impl_let_stmts(struct psi_data *data, impl *impl) { - size_t i, j; - /* we can have multiple let stmts */ - - /* check that we have a decl arg and impl arg for every let stmt */ - for (i = 0; i < impl->stmts->let.count; ++i) { - let_stmt *let = impl->stmts->let.list[i]; - decl_var *let_var; - impl_var *let_ivar = NULL; - - if (let->val && let->val->kind == PSI_LET_TMP) { - let_var = let->val->data.var; - } else { - let_var = let->var; - } - - if (!locate_decl_var_arg(let_var, impl->decl->args, impl->decl->func)) { - data->error(data, let_var->token, PSI_WARNING, "Unknown variable '%s' in `let` statement" - " of implementation '%s'", let_var->name, impl->func->name); - return 0; - } - switch (let->val->kind) { - case PSI_LET_CALLBACK: - let_ivar = let->val->data.callback->func->var; - break; - case PSI_LET_FUNC: - let_ivar = let->val->data.func->var; - break; - default: - break; - } - if (let_ivar && !locate_impl_var_arg(let_ivar, impl->func->args)) { - data->error(data, let_var->token, PSI_WARNING, "Unknown variable '%s' in `let` statement" - " of implementation '%s'", let_ivar->name, impl->func->name); - return 0; - } - - if (!validate_let_val(data, let->val, let->var, impl)) { - return 0; - } - } - /* check that we have a let stmt for every decl arg */ - if (impl->decl->args) for (i = 0; i < impl->decl->args->count; ++i) { - decl_arg *darg = impl->decl->args->args[i]; - int check = 0; - - for (j = 0; j < impl->stmts->let.count; ++j) { - let_stmt *let = impl->stmts->let.list[j]; - - if (!strcmp(let->var->name, darg->var->name)) { - check = 1; - break; - } - } - if (!check) { - data->error(data, impl->func->token, PSI_WARNING, - "Missing `let` statement for arg '%s %.*s%s'" - " of declaration '%s' for implementation '%s'", - darg->type->name, (int) darg->var->pointer_level, "*****", - darg->var->name, impl->decl->func->var->name, impl->func->name); - return 0; - } - } - - return 1; -} -static inline int validate_impl_set_stmts(struct psi_data *data, impl *impl) { - size_t i, j, k; - /* we can have any count of set stmts; processing out vars */ - /* check that set stmts reference known variables */ - for (i = 0; i < impl->stmts->set.count; ++i) { - set_stmt *set = impl->stmts->set.list[i]; - int check = 0; - - if (impl->func->args) for (j = 0; j < impl->func->args->count; ++j) { - impl_arg *iarg = impl->func->args->args[j]; - - if (!strcmp(set->var->name, iarg->var->name)) { - set->arg = iarg; - check = 1; - break; - } - } - if (!check) { - data->error(data, set->var->token, PSI_WARNING, "Unknown variable '$%s' of `set` statement" - " of implementation '%s'", - set->var->name, impl->func->name); - return 0; - } - - for (j = 0; j < set->val->vars->count; ++j) { - decl_var *set_var = set->val->vars->vars[j]; - - check = 0; - if (impl->decl->args) { - for (k = 0; k < impl->decl->args->count; ++k) { - decl_arg *set_arg = impl->decl->args->args[k]; - - if (!strcmp(set_var->name, set_arg->var->name)) { - check = 1; - set_var->arg = set_arg; - if (!validate_set_value(data, set->val, 1, &set_arg, 1, &impl->decl->func, impl->decl->args->count, impl->decl->args->args, 0)) { - return 0; - } - break; - } - } - } - if (!check) { - for (k = 0; k < impl->stmts->let.count; ++k) { - let_stmt *let = impl->stmts->let.list[k]; - - /* check temp vars */ - if (let->val && let->val->kind == PSI_LET_TMP) { - if (!strcmp(set_var->name, let->var->name)) { - check = 1; - set_var->arg = let->var->arg; - if (!validate_set_value(data, set->val, 1, &set_var->arg, 1, &impl->decl->func, impl->decl->args->count, impl->decl->args->args, 0)) { - return 0; - } - break; - } - } - } - } - - if (!check) { - data->error(data, set_var->token, PSI_WARNING, "Unknown value '%s' of `set` statement" - " for variable '$%s' of implementation '%s'", - set_var->name, set->arg->var->name, impl->func->name); - return 0; - } - } - } - return 1; -} -static inline decl *locate_free_decl(decls *decls, free_call *f) { - if (decls) { - size_t i; - - for (i = 0; i < decls->count; ++i) { - if (!strcmp(decls->list[i]->func->var->name, f->func)) { - f->decl = decls->list[i]; - return decls->list[i]; - } - } - } - - return NULL; -} -static inline int validate_impl_free_stmts(struct psi_data *data, impl *impl) { - size_t i, j, k, l; - /* we can have any count of free stmts; freeing any out vars */ - for (i = 0; i < impl->stmts->fre.count; ++i) { - free_stmt *fre = impl->stmts->fre.list[i]; - - for (j = 0; j < fre->calls->count; ++j) { - free_call *free_call = fre->calls->list[j]; - - /* first find the decl of the free func */ - if (!locate_free_decl(data->decls, free_call)) { - data->error(data, free_call->token, PSI_WARNING, - "Missing declaration '%s' in `free` statement" - " of implementation '%s'", - free_call->func, impl->func->name); - return 0; - } - - - - /* now check for known vars */ - for (l = 0; l < free_call->vars->count; ++l) { - int check = 0; - decl_var *free_var = free_call->vars->vars[l]; - - if (!strcmp(free_var->name, impl->decl->func->var->name)) { - check = 1; - free_var->arg = impl->decl->func; - } else if (impl->decl->args) { - for (k = 0; k < impl->decl->args->count; ++k) { - decl_arg *free_arg = impl->decl->args->args[k]; - - if (!strcmp(free_var->name, free_arg->var->name)) { - check = 1; - free_var->arg = free_arg; - break; - } - } - } - - if (!check) { - data->error(data, free_var->token, PSI_WARNING, - "Unknown variable '%s' of `free` statement" - " of implementation '%s'", - free_var->name, impl->func->name); - return 0; - } - } - } - } - return 1; -} -static inline int validate_impl_stmts(struct psi_data *data, impl *impl) { - if (!impl->stmts) { - data->error(data, impl->func->token, PSI_WARNING, - "Missing body for implementation %s!", - impl->func->name); - return 0; - } - - if (!validate_impl_ret_stmt(data, impl)) { - return 0; - } - - if (!validate_impl_let_stmts(data, impl)) { - return 0; - } - if (!validate_impl_set_stmts(data, impl)) { - return 0; - } - if (!validate_impl_free_stmts(data, impl)) { - return 0; - } - - return 1; -} - -static inline int validate_impl_args(struct psi_data *data, impl *impl) { - int def = 0; - size_t i; - - for (i = 0; i < impl->func->args->count; ++i) { - impl_arg *iarg = impl->func->args->args[i]; - - if (iarg->def) { - def = 1; - } else if (def) { - data->error(data, impl->func->token, PSI_WARNING, - "Non-optional argument %zu '$%s' of implementation '%s'" - " follows optional argument", - i+1, iarg->var->name, impl->func->name); - return 0; - } - } - - return 1; -} - -static inline int validate_impl(struct psi_data *data, impl *impl) { - if (!validate_impl_args(data, impl)) { - return 0; - } - return validate_impl_stmts(data, impl); -} - - -int psi_context_validate(struct psi_context *C, struct psi_parser *P) -{ - struct psi_data *D; - void *dlopened = NULL; - size_t i, count = C->count++, check_round, check_count; - decl_typedefs *check_defs = P->defs; - decl_structs *check_structs = P->structs; - decl_unions *check_unions = P->unions; - decl_enums *check_enums = P->enums; - unsigned flags = C->flags; - - C->data = realloc(C->data, C->count * sizeof(*C->data)); - D = psi_data_exchange(&C->data[count], PSI_DATA(P)); - -#define REVALIDATE(what) do { \ - if (check_round && check_ ##what) { \ - free(check_ ##what->list); \ - free(check_ ##what); \ - } \ - check_ ##what = recheck_ ##what; \ -} while (0) -#define CHECK_TOTAL (CHECK_COUNT(defs) + CHECK_COUNT(structs) + CHECK_COUNT(enums)) -#define CHECK_COUNT(of) (check_ ##of ? check_ ##of->count : 0) - - if (!(flags & PSI_PARSER_SILENT)) { - /* no warnings on first round */ - C->flags |= PSI_PARSER_SILENT; - } - for (check_round = 0, check_count = 0; CHECK_TOTAL && check_count != CHECK_TOTAL; ++check_round) { - decl_typedefs *recheck_defs = NULL; - decl_structs *recheck_structs = NULL; - decl_unions *recheck_unions = NULL; - decl_enums *recheck_enums = NULL; - - check_count = CHECK_TOTAL; - - for (i = 0; i < CHECK_COUNT(defs); ++i) { - if (validate_decl_typedef(PSI_DATA(C), check_defs->list[i])) { - C->defs = add_decl_typedef(C->defs, check_defs->list[i]); - } else { - recheck_defs = add_decl_typedef(recheck_defs, check_defs->list[i]); - } - } - for (i = 0; i < CHECK_COUNT(structs); ++i) { - if (validate_decl_struct(PSI_DATA(C), check_structs->list[i])) { - C->structs = add_decl_struct(C->structs, check_structs->list[i]); - } else { - recheck_structs = add_decl_struct(recheck_structs, check_structs->list[i]); - } - } - for (i = 0; i < CHECK_COUNT(unions); ++i) { - if (validate_decl_union(PSI_DATA(C), check_unions->list[i])) { - C->unions = add_decl_union(C->unions, check_unions->list[i]); - } else { - recheck_unions = add_decl_union(recheck_unions, check_unions->list[i]); - } - } - for (i = 0; i < CHECK_COUNT(enums); ++i) { - if (validate_decl_enum(PSI_DATA(C), check_enums->list[i])) { - C->enums = add_decl_enum(C->enums, check_enums->list[i]); - } else { - recheck_enums = add_decl_enum(recheck_enums, check_enums->list[i]); - } - } - - REVALIDATE(defs); - REVALIDATE(structs); - REVALIDATE(unions); - REVALIDATE(enums); - - if (check_round == 0 && !(flags & PSI_PARSER_SILENT)) { - C->flags ^= PSI_PARSER_SILENT; - } - } - - C->flags = flags; - - if (D->consts) { - for (i = 0; i < D->consts->count; ++i) { - if (validate_constant(PSI_DATA(C), D->consts->list[i])) { - C->consts = add_constant(C->consts, D->consts->list[i]); - } - } - } - - if (!validate_lib(D, &dlopened)) { - return 0; - } - - add_decl_lib(&C->psi.libs, dlopened); - - if (D->decls) { - for (i = 0; i < D->decls->count; ++i) { - if (validate_decl(PSI_DATA(C), dlopened, D->decls->list[i])) { - C->decls = add_decl(C->decls, D->decls->list[i]); - } - } - } - if (D->impls) { - for (i = 0; i < D->impls->count; ++i) { - if (validate_impl(PSI_DATA(C), D->impls->list[i])) { - C->impls = add_impl(C->impls, D->impls->list[i]); - } - } - } - - return 1; -} - -int psi_context_validate_data(struct psi_data *dest, struct psi_data *source) -{ - size_t i; - int errors = 0; - - if (source->defs) for (i = 0; i < source->defs->count; ++i) { - decl_arg *def = source->defs->list[i]; - - if (validate_decl_typedef(source, def)) { - if (dest) { - dest->defs = add_decl_typedef(dest->defs, def); - } - } else { - ++errors; - } - } - - if (source->consts) for (i = 0; i < source->consts->count; ++i) { - constant *constant = source->consts->list[i]; - - if (validate_constant(source, constant)) { - if (dest) { - dest->consts = add_constant(dest->consts, constant); - } - } else { - ++errors; - } - } - - if (source->structs) for (i = 0; i < source->structs->count; ++i) { - decl_struct *dstruct = source->structs->list[i]; - - if (validate_decl_struct(source, dstruct)) { - if (dest) { - dest->structs = add_decl_struct(dest->structs, dstruct); - } - } else { - ++errors; - } - } - - if (source->unions) for (i = 0; i < source->unions->count; ++i) { - decl_union *dunion = source->unions->list[i]; - - if (validate_decl_union(source, dunion)) { - if (dest) { - dest->unions = add_decl_union(dest->unions, dunion); - } - } else { - ++errors; - } - } - - if (source->enums) for (i = 0; i < source->enums->count; ++i) { - decl_enum *denum = source->enums->list[i]; - - if (validate_decl_enum(source, denum)) { - if (dest) { - dest->enums = add_decl_enum(dest->enums, denum); - } - } else { - ++errors; - } - } - - if (source->decls) for (i = 0; i < source->decls->count; ++i) { - decl *decl = source->decls->list[i]; - - if (validate_decl(source, NULL, decl)) { - if (dest) { - dest->decls = add_decl(dest->decls, decl); - } - } else { - ++errors; - } - } - - if (source->impls) for (i = 0; i < source->impls->count; ++i) { - impl *impl = source->impls->list[i]; - - if (validate_impl(source, impl)) { - if (dest) { - dest->impls = add_impl(dest->impls, impl); - } - } else { - ++errors; - } - } - - return errors; -} diff --git a/src/data.h b/src/data.h index cc497fb..c31269c 100644 --- a/src/data.h +++ b/src/data.h @@ -2,6 +2,7 @@ #define _PSI_DATA_H #include "types.h" +#include "error.h" #define PSI_DATA(D) ((struct psi_data *) (D)) diff --git a/src/engine.c b/src/engine.c index 80a2eb5..8c273bc 100644 --- a/src/engine.c +++ b/src/engine.c @@ -1,5 +1,7 @@ #ifdef HAVE_CONFIG_H # include "config.h" +#else +# include "php_config.h" #endif #include "php.h" @@ -12,6 +14,13 @@ #include "calc.h" #include "marshal.h" +static inline void psi_do_set(zval *return_value, set_value *set) +{ + decl_arg *set_arg = set->vars->vars[0]->arg; + + zval_dtor(return_value); + set->func->handler(return_value, set, set_arg->let); +} int psi_internal_type(impl_type *type) { @@ -229,14 +238,14 @@ static inline void *psi_let_val(let_val *val, decl_arg *darg) darg->val.ptr = psi_do_calloc(val->data.alloc); darg->mem = darg->val.ptr; break; - case PSI_LET_CALLBACK: - darg->val.ptr = val->data.callback->decl->call.sym; - break; case PSI_LET_NUMEXP: darg->val.zend.lval = psi_long_num_exp(val->data.num, NULL); break; + case PSI_LET_CALLBACK: + darg->val.ptr = val->data.callback->decl->call.sym; + break; case PSI_LET_FUNC: - if (!psi_let_func(val->data.func, darg)) { + if (!(darg->ptr = psi_let_func(val->data.func, darg))) { return NULL; } break; @@ -249,32 +258,77 @@ static inline void *psi_let_val(let_val *val, decl_arg *darg) } } +static void *marshal_func(void *cb_ctx, impl_val **ptr, decl_arg *spec, token_t cast, zval *zv, void **tmp) { + let_vals *vals = cb_ctx; + size_t i; + + for (i = 0; i < vals->count; ++i) { + impl_var *var = locate_let_val_impl_var(vals->vals[i]); + + if (!strcmp(&var->name[1], spec->var->name)) { + return *ptr = psi_let_val(vals->vals[i], spec); + } + } + + return *ptr = NULL; +} + static inline impl_val *psi_let_func(let_func *func, decl_arg *darg) { - impl_arg *iarg = NULL; + impl_arg *iarg = func->var->arg; - if (0 && func->inner) { - size_t i; + if (func->outer && !iarg) { + impl_arg *outer_arg = locate_let_val_impl_var(func->outer)->arg; + iarg = init_impl_arg( + init_impl_type(PSI_T_MIXED, "mixed"), + copy_impl_var(func->var), NULL); - for (i = 0; i < func->inner->count; ++i) { - let_val *inner = func->inner->vals[i]; - decl_arg *ref = NULL; - switch (inner->kind) { - case PSI_LET_CALLBACK: - ref = inner->data.callback->func->ref; - break; - case PSI_LET_FUNC: - ref = inner->data.func->ref; - break; - EMPTY_SWITCH_DEFAULT_CASE(); + if (!(iarg->_zv = zend_hash_str_find(Z_ARRVAL_P(outer_arg->_zv), &iarg->var->name[1], strlen(iarg->var->name)-1))) { + iarg->_zv = ecalloc(1, sizeof(*iarg->_zv)); + } + } + + switch (func->type) { + case PSI_T_BOOLVAL: + return psi_let_boolval(darg->ptr, darg->type, iarg->type->type, &iarg->val, iarg->_zv, &darg->mem); + case PSI_T_INTVAL: + return psi_let_intval(darg->ptr, darg->type, iarg->type->type, &iarg->val, iarg->_zv, &darg->mem); + case PSI_T_FLOATVAL: + return psi_let_floatval(darg->ptr, darg->type, iarg->type->type, &iarg->val, iarg->_zv, &darg->mem); + case PSI_T_STRVAL: + return psi_let_strval(darg->ptr, darg->type, iarg->type->type, &iarg->val, iarg->_zv, &darg->mem); + case PSI_T_STRLEN: + return psi_let_strlen(darg->ptr, darg->type, iarg->type->type, &iarg->val, iarg->_zv, &darg->mem); + case PSI_T_PATHVAL: + return psi_let_pathval(darg->ptr, darg->type, iarg->type->type, &iarg->val, iarg->_zv, &darg->mem); + case PSI_T_OBJVAL: + return psi_let_objval(darg->ptr, darg->type, iarg->type->type, &iarg->val, iarg->_zv, &darg->mem); + case PSI_T_ZVAL: + return psi_let_zval(darg->ptr, darg->type, iarg->type->type, &iarg->val, iarg->_zv, &darg->mem); + case PSI_T_VOID: + return psi_let_void(darg->ptr, darg->type, iarg->type->type, &iarg->val, iarg->_zv, &darg->mem); + break; + case PSI_T_ARRVAL: + if (func->inner) { + size_t i; + decl_type *real = real_decl_type(darg->type); + + if (iarg->type->type != PSI_T_ARRAY) { + SEPARATE_ARG_IF_REF(iarg->_zv); + convert_to_array(iarg->_zv); } - psi_let_val(inner, ref); + return psi_array_to_struct_ex(real->real.strct, Z_ARRVAL_P(iarg->_zv), marshal_func, func->inner); + } else { + return psi_let_arrval(darg->ptr, darg->type, iarg->type->type, &iarg->val, iarg->_zv, &darg->mem); } + break; + default: + assert(0); } + return NULL; - return darg->ptr = func->handler(darg->ptr, darg->type, func->var->arg, &darg->mem); - + // return darg->ptr = func->handler(darg->ptr, darg->type, func->var->arg, &darg->mem); } static inline void *psi_do_let(let_stmt *let) @@ -433,7 +487,7 @@ static inline impl_vararg *psi_do_varargs(impl *impl) { impl_arg *vaarg = va->args->args[i]; void *to_free = NULL; token_t vatype = va->name->type->type; - let_func_handler let_fn; + psi_marshal_let let_fn; if (vatype == PSI_T_MIXED) { switch (Z_TYPE_P(vaarg->_zv)) { @@ -556,7 +610,7 @@ ZEND_RESULT_CODE psi_callback(let_callback *cb, void *retval, unsigned argc, voi case PSI_T_DOUBLE: zend_parse_arg_double(iarg->_zv, &iarg->val.dval, NULL, 0); break; case PSI_T_STRING: zend_parse_arg_str(iarg->_zv, &iarg->val.zend.str, 0); break; } - result = cb->func->handler(retval, decl_cb->func->type, iarg, &to_free); + result = NULL;//cb->func->handler(retval, decl_cb->func->type, iarg, &to_free); if (result != retval) { *(void **)retval = result; diff --git a/src/engine.h b/src/engine.h index e8d86ff..d7891a2 100644 --- a/src/engine.h +++ b/src/engine.h @@ -1,26 +1,6 @@ #ifndef _PSI_ENGINE_H #define _PSI_ENGINE_H -static inline size_t psi_align(size_t s, size_t a) { - return ((s - 1) | (a - 1)) + 1; -} - -static inline size_t psi_offset_padding(size_t diff, size_t alignment) { - if (diff && diff <= psi_align(diff, alignment)) { - diff = 0; - } - - return diff; -} - -static inline void psi_do_set(zval *return_value, set_value *set) -{ - decl_arg *set_arg = set->vars->vars[0]->arg; - - zval_dtor(return_value); - set->func->handler(return_value, set, set_arg->let); -} - int psi_internal_type(impl_type *type); zend_internal_arg_info *psi_internal_arginfo(impl *impl); size_t psi_num_min_args(impl *impl); diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..2d764bc --- /dev/null +++ b/src/error.c @@ -0,0 +1,36 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "error.h" + diff --git a/src/error.h b/src/error.h new file mode 100644 index 0000000..2c66d88 --- /dev/null +++ b/src/error.h @@ -0,0 +1,42 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_ERROR_H +#define PSI_ERROR_H + +#include + +#include "token.h" + +#define PSI_ERROR 16 +#define PSI_WARNING 32 + +typedef void (*psi_error_cb)(void *context, struct psi_token *token, int type, const char *msg, ...); + +void psi_error_wrapper(void *context, struct psi_token *t, int type, const char *msg, ...); +void psi_error(int type, const char *fn, unsigned ln, const char *msg, ...); +void psi_verror(int type, const char *fn, unsigned ln, const char *msg, va_list argv); + +#endif /* EXT_PSI_SRC_ERROR_H */ diff --git a/src/libffi.c b/src/libffi.c index e2dc81f..5dce76b 100644 --- a/src/libffi.c +++ b/src/libffi.c @@ -1,5 +1,7 @@ #ifdef HAVE_CONFIG_H # include "config.h" +#else +# include "php_config.h" #endif #include "php.h" diff --git a/src/libjit.c b/src/libjit.c index de743a1..f99c039 100644 --- a/src/libjit.c +++ b/src/libjit.c @@ -1,5 +1,7 @@ #ifdef HAVE_CONFIG_H # include "config.h" +#else +# include "php_config.h" #endif #include "php.h" diff --git a/src/marshal.c b/src/marshal.c index 66b6ae6..00c1a81 100644 --- a/src/marshal.c +++ b/src/marshal.c @@ -1,5 +1,7 @@ #ifdef HAVE_CONFIG_H # include "config.h" +#else +# include "php_config.h" #endif #include "php.h" @@ -13,7 +15,8 @@ void psi_to_void(zval *return_value, set_value *set, impl_val *ret_val) RETVAL_NULL(); } -impl_val *psi_let_void(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free) { +impl_val *psi_let_void(impl_val *tmp, decl_type *spec, token_t impl_type, impl_val *ival, zval *zvalue, void **to_free) +{ return tmp; } @@ -21,10 +24,10 @@ void psi_to_zval(zval *return_value, set_value *set, impl_val *ret_val) { RETVAL_ZVAL(ret_val->ptr, 1, 0); } -impl_val *psi_let_zval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free) +impl_val *psi_let_zval(impl_val *tmp, decl_type *spec, token_t impl_type, impl_val *ival, zval *zvalue, void **to_free) { *to_free = tmp->ptr = emalloc(sizeof(zval)); - ZVAL_COPY_VALUE(tmp->ptr, iarg->_zv); + ZVAL_COPY_VALUE(tmp->ptr, zvalue); return tmp; } @@ -54,15 +57,15 @@ static inline impl_val *psi_val_boolval(impl_val *tmp, token_t real_type, zend_b return tmp; } -impl_val *psi_let_boolval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free) +impl_val *psi_let_boolval(impl_val *tmp, decl_type *spec, token_t impl_type, impl_val *ival, zval *zvalue, void **to_free) { zend_bool boolval; - token_t real_type = type ? real_decl_type(type)->type : PSI_T_UINT8; + token_t real_type = spec ? real_decl_type(spec)->type : PSI_T_UINT8; - if (iarg->type->type == PSI_T_BOOL) { - boolval = iarg->val.zend.bval; + if (ival && impl_type == PSI_T_BOOL) { + boolval = ival->zend.bval; } else { - boolval = zend_is_true(iarg->_zv); + boolval = zend_is_true(zvalue); } return psi_val_boolval(tmp, real_type, boolval); @@ -125,15 +128,15 @@ static inline impl_val *psi_val_intval(impl_val *tmp, token_t real_type, zend_lo return tmp; } -impl_val *psi_let_intval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free) +impl_val *psi_let_intval(impl_val *tmp, decl_type *spec, token_t impl_type, impl_val *ival, zval *zvalue, void **to_free) { zend_long intval; - token_t real_type = type ? real_decl_type(type)->type : PSI_T_LONG; + token_t real_type = spec ? real_decl_type(spec)->type : PSI_T_LONG; - if (iarg->type->type == PSI_T_INT) { - intval = iarg->val.zend.lval; + if (ival && impl_type == PSI_T_INT) { + intval = ival->zend.lval; } else { - intval = zval_get_long(iarg->_zv); + intval = zval_get_long(zvalue); } return psi_val_intval(tmp, real_type, intval); @@ -184,15 +187,15 @@ static inline impl_val *psi_val_floatval(impl_val *tmp, token_t real_type, doubl return tmp; } -impl_val *psi_let_floatval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free) +impl_val *psi_let_floatval(impl_val *tmp, decl_type *spec, token_t impl_type, impl_val *ival, zval *zvalue, void **to_free) { double floatval; - token_t real_type = type ? real_decl_type(type)->type : PSI_T_DOUBLE; + token_t real_type = spec ? real_decl_type(spec)->type : PSI_T_DOUBLE; - if (iarg->type->type == PSI_T_FLOAT || iarg->type->type == PSI_T_DOUBLE) { - floatval = iarg->val.dval; + if (ival && (impl_type == PSI_T_FLOAT || impl_type == PSI_T_DOUBLE)) { + floatval = ival->dval; } else { - floatval = zval_get_double(iarg->_zv); + floatval = zval_get_double(zvalue); } return psi_val_floatval(tmp, real_type, floatval); @@ -237,17 +240,17 @@ void psi_to_string(zval *return_value, set_value *set, impl_val *ret_val) convert_to_string(return_value); } -impl_val *psi_let_strval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free) +impl_val *psi_let_strval(impl_val *tmp, decl_type *spec, token_t impl_type, impl_val *ival, zval *zvalue, void **to_free) { - if (iarg->type->type == PSI_T_STRING) { - if (iarg->val.zend.str) { - tmp->ptr = estrndup(iarg->val.zend.str->val, iarg->val.zend.str->len); + if (ival && impl_type == PSI_T_STRING) { + if (ival->zend.str) { + tmp->ptr = estrndup(ival->zend.str->val, ival->zend.str->len); *to_free = tmp->ptr; } else { tmp->ptr = ""; } } else { - zend_string *zs = zval_get_string(iarg->_zv); + zend_string *zs = zval_get_string(zvalue); tmp->ptr = estrdup(zs->val); *to_free = tmp->ptr; zend_string_release(zs); @@ -256,26 +259,27 @@ impl_val *psi_let_strval(impl_val *tmp, decl_type *type, impl_arg *iarg, void ** return tmp; } -impl_val *psi_let_pathval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free) +impl_val *psi_let_pathval(impl_val *tmp, decl_type *spec, token_t impl_type, impl_val *ival, zval *zvalue, void **to_free) { - tmp = psi_let_strval(tmp, type, iarg, to_free); + tmp = psi_let_strval(tmp, spec, impl_type, ival, zvalue, to_free); if (SUCCESS != php_check_open_basedir(tmp->ptr)) { efree(tmp->ptr); + tmp->ptr = NULL; return *to_free = NULL; } return tmp; } -impl_val *psi_let_strlen(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free) +impl_val *psi_let_strlen(impl_val *tmp, decl_type *spec, token_t impl_type, impl_val *ival, zval *zvalue, void **to_free) { - if (iarg->type->type == PSI_T_STRING) { - if (iarg->val.zend.str) { - tmp->lval = iarg->val.zend.str->len; + if (ival && impl_type == PSI_T_STRING) { + if (ival->zend.str) { + tmp->lval = ival->zend.str->len; } else { tmp->lval = 0; } } else { - zend_string *zs = zval_get_string(iarg->_zv); + zend_string *zs = zval_get_string(zvalue); tmp->lval = zs->len; zend_string_release(zs); } @@ -290,7 +294,7 @@ static impl_val *iterate(impl_val *val, size_t size, unsigned i, impl_val *tmp) return tmp; } -void psi_from_zval_ex(impl_val **ptr, decl_arg *spec, token_t cast, zval *zv, void **tmp) +void psi_from_zval_ex(void *cb_ctx, impl_val **ptr, decl_arg *spec, token_t cast, zval *zv, void **tmp) { decl_type *real = real_decl_type(spec->type); impl_val *val = *ptr; @@ -339,10 +343,10 @@ void psi_from_zval_ex(impl_val **ptr, decl_arg *spec, token_t cast, zval *zv, vo val->ival = zval_get_long(zv); break; case PSI_T_STRUCT: - *tmp = *ptr = psi_array_to_struct(real->real.strct, HASH_OF(zv)); + *tmp = *ptr = psi_array_to_struct_ex(real->real.strct, HASH_OF(zv), psi_from_zval_ex, cb_ctx); break; case PSI_T_UNION: - *tmp = *ptr = psi_array_to_union(real->real.unn, HASH_OF(zv)); + *tmp = *ptr = psi_array_to_union_ex(real->real.unn, HASH_OF(zv), psi_from_zval_ex, cb_ctx); break; case PSI_T_FUNCTION: /*FIXME*/ @@ -361,8 +365,7 @@ void psi_from_zval_ex(impl_val **ptr, decl_arg *spec, token_t cast, zval *zv, vo } } -void *psi_array_to_struct(decl_struct *s, HashTable *arr) -{ +void *psi_array_to_struct_ex(decl_struct *s, HashTable *arr, psi_marshal_zval cb, void *cb_ctx) { size_t i, j = 0; char *mem = ecalloc(1, s->size + s->args->count * sizeof(void *)); @@ -375,7 +378,7 @@ void *psi_array_to_struct(decl_struct *s, HashTable *arr) void *tmp = NULL; memset(&val, 0, sizeof(val)); - psi_from_zval_ex(&ptr, darg, /*FIXME*/0, entry, &tmp); + cb(cb_ctx, &ptr, darg, /*FIXME*/0, entry, &tmp); memcpy(mem + darg->layout->pos, ptr, darg->layout->len); if (tmp) { ((void **)(mem + s->size))[j++] = tmp; @@ -385,7 +388,12 @@ void *psi_array_to_struct(decl_struct *s, HashTable *arr) return mem; } -void *psi_array_to_union(decl_union *u, HashTable *arr) { +void *psi_array_to_struct(decl_struct *s, HashTable *arr) +{ + return psi_array_to_struct_ex(s, arr, psi_from_zval_ex, NULL); +} + +void *psi_array_to_union_ex(decl_union *u, HashTable *arr, psi_marshal_zval cb, void *cb_ctx) { size_t i; char *mem = ecalloc(1, u->size + sizeof(void *)); @@ -398,7 +406,7 @@ void *psi_array_to_union(decl_union *u, HashTable *arr) { void *tmp = NULL; memset(&val, 0, sizeof(val)); - psi_from_zval_ex(&ptr, darg, /*FIXME*/0, entry, &tmp); + cb(cb_ctx, &ptr, darg, /*FIXME*/0, entry, &tmp); memcpy(mem, &val, darg->layout->len); if (tmp) { ((void **)(mem + u->size))[0] = tmp; @@ -411,8 +419,11 @@ void *psi_array_to_union(decl_union *u, HashTable *arr) { return mem; } -void psi_to_recursive(zval *return_value, set_value *set, impl_val *r_val) -{ +void *psi_array_to_union(decl_union *u, HashTable *arr) { + return psi_array_to_union_ex(u, arr, psi_from_zval_ex, NULL); +} + +void psi_to_recursive(zval *return_value, set_value *set, impl_val *r_val) { set->outer.set->func->handler(return_value, set, r_val); } @@ -509,16 +520,16 @@ void psi_to_array(zval *return_value, set_value *set, impl_val *r_val) } } -impl_val *psi_let_arrval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free) +impl_val *psi_let_arrval(impl_val *tmp, decl_type *spec, token_t impl_type, impl_val *ival, zval *zvalue, void **to_free) { - decl_type *real = real_decl_type(type); + decl_type *real = real_decl_type(spec); HashTable *arr; - if (iarg->type->type != PSI_T_ARRAY) { - SEPARATE_ARG_IF_REF(iarg->_zv); - convert_to_array(iarg->_zv); + if (impl_type != PSI_T_ARRAY) { + SEPARATE_ARG_IF_REF(zvalue); + convert_to_array(zvalue); } - arr = HASH_OF(iarg->_zv); + arr = HASH_OF(zvalue); switch (real->type) { case PSI_T_STRUCT: @@ -546,16 +557,16 @@ void psi_to_object(zval *return_value, set_value *set, impl_val *r_val) } } -impl_val *psi_let_objval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free) +impl_val *psi_let_objval(impl_val *tmp, decl_type *spec, token_t impl_type, impl_val *ival, zval *zvalue, void **to_free) { psi_object *obj; - if (Z_TYPE_P(iarg->_zv) != IS_OBJECT - || !instanceof_function(Z_OBJCE_P(iarg->_zv), psi_object_get_class_entry())) { + if (Z_TYPE_P(zvalue) != IS_OBJECT + || !instanceof_function(Z_OBJCE_P(zvalue), psi_object_get_class_entry())) { return NULL; } - obj = PSI_OBJ(iarg->_zv, NULL); + obj = PSI_OBJ(zvalue, NULL); tmp->ptr = obj->data; return tmp; diff --git a/src/marshal.h b/src/marshal.h index 4e2aab4..bcf987c 100644 --- a/src/marshal.h +++ b/src/marshal.h @@ -1,6 +1,9 @@ #ifndef _PSI_MARSHAL_H #define _PSI_MARSHAL_H +typedef void (*psi_marshal_set)(zval *return_value, set_value *set, impl_val *ret_val); +typedef impl_val *(*psi_marshal_let)(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free); + void psi_to_void(zval *return_value, set_value *set, impl_val *ret_val); void psi_to_bool(zval *return_value, set_value *set, impl_val *ret_val); void psi_to_int(zval *return_value, set_value *set, impl_val *ret_val); @@ -13,17 +16,25 @@ void psi_to_zval(zval *return_value, set_value *set, impl_val *ret_val); void *psi_array_to_union(decl_union *u, HashTable *arr); void *psi_array_to_struct(decl_struct *s, HashTable *arr); -void psi_from_zval(impl_val *mem, decl_arg *spec, zval *zv, void **tmp); - -impl_val *psi_let_void(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free); -impl_val *psi_let_boolval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free); -impl_val *psi_let_intval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free); -impl_val *psi_let_floatval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free); -impl_val *psi_let_strval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free); -impl_val *psi_let_pathval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free); -impl_val *psi_let_strlen(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free); -impl_val *psi_let_arrval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free); -impl_val *psi_let_objval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free); -impl_val *psi_let_zval(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free); +typedef void (*psi_marshal_zval)(void *cb_ctx, impl_val **ptr, decl_arg *spec, token_t cast, zval *zv, void **tmp); + +void psi_from_zval_ex(void *cb_ctx, impl_val **ptr, decl_arg *spec, token_t cast, zval *zv, void **tmp); + +void *psi_array_to_struct_ex(decl_struct *s, HashTable *arr, psi_marshal_zval cb, void *cb_ctx); +void *psi_array_to_union_ex(decl_union *u, HashTable *arr, psi_marshal_zval cb, void *cb_ctx); + +impl_val *psi_let_void(impl_val *tmp, decl_type *decl_type, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free); +impl_val *psi_let_boolval(impl_val *tmp, decl_type *decl_type, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free); +impl_val *psi_let_intval(impl_val *tmp, decl_type *decl_type, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free); +impl_val *psi_let_floatval(impl_val *tmp, decl_type *decl_type, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free); +impl_val *psi_let_strval(impl_val *tmp, decl_type *decl_type, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free); +impl_val *psi_let_pathval(impl_val *tmp, decl_type *decl_type, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free); +impl_val *psi_let_strlen(impl_val *tmp, decl_type *decl_type, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free); +impl_val *psi_let_arrval(impl_val *tmp, decl_type *decl_type, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free); +impl_val *psi_let_objval(impl_val *tmp, decl_type *decl_type, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free); +impl_val *psi_let_zval(impl_val *tmp, decl_type *decl_type, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free); + + +typedef impl_val *(*psi_let)(impl_val *tmp, decl_type *decl_type, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free); #endif diff --git a/src/module.c b/src/module.c index 33e3ae2..a70a50a 100644 --- a/src/module.c +++ b/src/module.c @@ -1,5 +1,7 @@ #ifdef HAVE_CONFIG_H # include "config.h" +#else +# include "php_config.h" #endif #include "php.h" diff --git a/src/parser_def.h b/src/parser_def.h index b4a4ff0..af3ea8a 100644 --- a/src/parser_def.h +++ b/src/parser_def.h @@ -33,10 +33,6 @@ #define TOKEN_DTOR(token, dtor) #endif -DEF(%include, { -void psi_error(int, const char *, int, const char *, ...); -}) - DEF(%name, psi_parser_proc_) DEF(%token_prefix, PSI_T_) DEF(%token_type, {struct psi_token *}) diff --git a/src/parser_def.h.bak b/src/parser_def.h.bak new file mode 100644 index 0000000..c3861de --- /dev/null +++ b/src/parser_def.h.bak @@ -0,0 +1,1206 @@ + +#define _CONCAT(x,y) x##y +#define CONCAT(x,y) _CONCAT(x,y) +#define COUNTED(x) CONCAT(parse_ ##x## _, __LINE__) + +#ifdef GENERATE +#define DEF(dn, dv) dn dv +#define LET(nt, rule) nt ::= rule. +#define PARSE(nt, rule) nt ::= rule. +#define PARSE_NAMED(nt, nt_name, rule) NAMED(nt, nt_name) ::= rule. +#define PARSE_TYPED(nt, nt_name, rule) TYPED(nt, nt_name) ::= rule. +#define TOKEN(t) t +#define NAMED(t, name) t(name) +#define TYPED(t, name) t(name) +#define TOKEN_TYPE(token, type_) %type token {type_} +#define TOKEN_DTOR(token, dtor) %destructor {dtor} +#else +#ifndef TEST +#include "parser.h" +#endif +#define DEF(dn, dv) +#define LET(nt, rule) +#define PARSE(nt, rule) \ + static void COUNTED(nt) (PSI_Parser *P rule) +#define PARSE_NAMED(nt, nt_name, rule) \ + static void COUNTED(nt) (PSI_Parser *P NAMED(nt, nt_name) rule) +#define PARSE_TYPED(nt, nt_name, rule) \ + static void COUNTED(nt) (PSI_Parser *P TYPED(nt, nt_name) rule) +#define TOKEN(t) +#define NAMED(t, name) , PSI_Token *name +#define TYPED(t, name) , TOKEN_TYPE_NAME(t) name +#define TOKEN_TYPE_NAME(token) _##token##_type +#define TOKEN_TYPE(token, type) typedef type TOKEN_TYPE_NAME(token); +#define TOKEN_DTOR(token, dtor) +#endif + +DEF(%include, { +void psi_error(int, const char *, int, const char *, ...); +}) +DEF(%name, PSI_ParserProc) +DEF(%token_prefix, PSI_T_) +DEF(%token_type, {PSI_Token *}) +DEF(%token_destructor, {free($$);}) +DEF(%default_destructor, {(void)P;}) +DEF(%extra_argument, {PSI_Parser *P}) + +/* TOKEN is defined inside syntax_error */ +DEF(%syntax_error, { + ++P->errors; + if (token && token->type != PSI_T_EOF) { + psi_error(PSI_WARNING, token->file, token->line, "PSI syntax error: Unexpected token '%s'", token->text); + } else { + psi_error(PSI_WARNING, P->psi.file.fn, P->line, "PSI syntax error: Unexpected end of input"); + } +}) + +DEF(%nonassoc, NAME.) +DEF(%left, PLUS MINUS.) +DEF(%left, SLASH ASTERISK.) +DEF(%fallback, NAME TEMP FREE SET LET RETURN CALLOC CALLBACK ZVAL LIB STRING.) + +DEF(%token_class, const_type_token BOOL INT FLOAT STRING.) +DEF(%token_class, decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.) +DEF(%token_class, impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.) +DEF(%token_class, num_exp_token NUMBER NSNAME.) +DEF(%token_class, num_exp_op_token PLUS MINUS ASTERISK SLASH.) +DEF(%token_class, let_func_token ZVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.) +DEF(%token_class, set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL ZVAL VOID.) +DEF(%token_class, impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE.) + +TOKEN_TYPE(decl_enum, decl_enum *) +TOKEN_DTOR(decl_enum, free_decl_enum($$);) +TOKEN_TYPE(decl_enum_items, decl_enum_items*) +TOKEN_DTOR(decl_enum_items, free_decl_enum_items($$);) +TOKEN_TYPE(decl_enum_item, decl_enum_item*) +TOKEN_DTOR(decl_enum_item, free_decl_enum_item($$);) +TOKEN_TYPE(decl_struct_args_block, decl_args*) +TOKEN_DTOR(decl_struct_args_block, free_decl_args($$);) /* there was a typo */ +TOKEN_TYPE(decl_struct_args, decl_args*) +TOKEN_DTOR(decl_struct_args, free_decl_args($$);) +TOKEN_TYPE(decl_struct, decl_struct*) +TOKEN_DTOR(decl_struct, free_decl_struct($$);) +TOKEN_TYPE(align_and_size, decl_struct_layout) +TOKEN_TYPE(decl_union, decl_union*) +TOKEN_DTOR(decl_union, free_decl_union($$);) +TOKEN_TYPE(const_type, const_type*) +TOKEN_DTOR(const_type, free_const_type($$);) +TOKEN_TYPE(constant, constant*) +TOKEN_DTOR(constant, free_constant($$);) +TOKEN_TYPE(decl_typedef, decl_arg*) +TOKEN_DTOR(decl_typedef, free_decl_arg($$);) +TOKEN_TYPE(decl_typedef_body_ex, decl_arg*) +TOKEN_DTOR(decl_typedef_body_ex, free_decl_arg($$);) +TOKEN_TYPE(decl_typedef_body, decl_arg*) +TOKEN_DTOR(decl_typedef_body, free_decl_arg($$);) +TOKEN_TYPE(decl_typedef_body_fn_args, decl_args *) +TOKEN_DTOR(decl_typedef_body_fn_args, free_decl_args($$);) +TOKEN_TYPE(decl, decl*) +TOKEN_DTOR(decl, free_decl($$);) +TOKEN_TYPE(decl_func, decl_arg*) +TOKEN_DTOR(decl_func, free_decl_arg($$);) +TOKEN_TYPE(decl_abi, decl_abi*) +TOKEN_DTOR(decl_abi, free_decl_abi($$);) +TOKEN_TYPE(decl_var, decl_var*) +TOKEN_DTOR(decl_var, free_decl_var($$);) +TOKEN_TYPE(decl_vars, decl_vars*) +TOKEN_DTOR(decl_vars, free_decl_vars($$);) +TOKEN_TYPE(decl_arg, decl_arg*) +TOKEN_DTOR(decl_arg, free_decl_arg($$);) +TOKEN_TYPE(decl_args, decl_args*) +TOKEN_DTOR(decl_args, free_decl_args($$);) +TOKEN_TYPE(struct_args, decl_args*) +TOKEN_DTOR(struct_args, free_decl_args($$);) +TOKEN_TYPE(struct_arg, decl_arg*) +TOKEN_DTOR(struct_arg, free_decl_arg($$);) +TOKEN_TYPE(struct_layout, decl_struct_layout*) +TOKEN_DTOR(struct_layout, free_decl_struct_layout($$);) +TOKEN_TYPE(decl_type, decl_type*) +TOKEN_DTOR(decl_type, free_decl_type($$);) +TOKEN_TYPE(const_decl_type, decl_type*) +TOKEN_DTOR(const_decl_type, free_decl_type($$);) +TOKEN_TYPE(impl, impl*) +TOKEN_DTOR(impl, free_impl($$);) +TOKEN_TYPE(impl_func, impl_func*) +TOKEN_DTOR(impl_func, free_impl_func($$);) +TOKEN_TYPE(impl_def_val, impl_def_val*) +TOKEN_DTOR(impl_def_val, free_impl_def_val($$);) +TOKEN_TYPE(impl_var, impl_var*) +TOKEN_DTOR(impl_var, free_impl_var($$);) +TOKEN_TYPE(impl_arg, impl_arg*) +TOKEN_DTOR(impl_arg, free_impl_arg($$);) +TOKEN_TYPE(impl_args, impl_args*) +TOKEN_DTOR(impl_args, free_impl_args($$);) +TOKEN_TYPE(impl_vararg, impl_arg*) +TOKEN_DTOR(impl_vararg, free_impl_arg($$);) +TOKEN_TYPE(impl_arg_list, impl_args*) +TOKEN_DTOR(impl_arg_list, free_impl_args($$);) +TOKEN_TYPE(impl_stmts, impl_stmts*) +TOKEN_DTOR(impl_stmts, free_impl_stmts($$);) +TOKEN_TYPE(impl_stmt, impl_stmt*) +TOKEN_DTOR(impl_stmt, free_impl_stmt($$);) +TOKEN_TYPE(num_exp, num_exp*) +TOKEN_DTOR(num_exp, free_num_exp($$);) +TOKEN_TYPE(let_stmt, let_stmt*) +TOKEN_DTOR(let_stmt, free_let_stmt($$);) +TOKEN_TYPE(let_calloc, let_calloc*) +TOKEN_DTOR(let_calloc, free_let_calloc($$);) +TOKEN_TYPE(let_func, let_func*) +TOKEN_DTOR(let_func, free_let_func($$);) +TOKEN_TYPE(callback_arg_list, set_values *) +TOKEN_DTOR(callback_arg_list, free_set_values($$);) +TOKEN_TYPE(callback_args, set_values *) +TOKEN_DTOR(callback_args, free_set_values($$);) +TOKEN_TYPE(let_val, let_val*) +TOKEN_DTOR(let_val, free_let_val($$);) +TOKEN_TYPE(set_stmt, set_stmt*) +TOKEN_DTOR(set_stmt, free_set_stmt($$);) +TOKEN_TYPE(set_value, set_value*) +TOKEN_DTOR(set_value, free_set_value($$);) +TOKEN_TYPE(set_vals, set_value*) +TOKEN_DTOR(set_vals, free_set_value($$);) +TOKEN_TYPE(set_func, set_func*) +TOKEN_DTOR(set_func, free_set_func($$);) +TOKEN_TYPE(return_stmt, return_stmt*) +TOKEN_DTOR(return_stmt, free_return_stmt($$);) +TOKEN_TYPE(free_stmt, free_stmt*) +TOKEN_DTOR(free_stmt, free_free_stmt($$);) +TOKEN_TYPE(free_calls, free_calls*) +TOKEN_DTOR(free_calls, free_free_calls($$);) +TOKEN_TYPE(free_call, free_call*) +TOKEN_DTOR(free_call, free_free_call($$);) +TOKEN_TYPE(impl_type, impl_type*) +TOKEN_DTOR(impl_type, free_impl_type($$);) +TOKEN_TYPE(reference, char) +TOKEN_TYPE(indirection, unsigned) +TOKEN_TYPE(pointers, unsigned) + +LET(file, blocks) +LET(blocks, block) +LET(blocks, blocks block) + +LET(block, EOF) +LET(block, EOS) + +PARSE(block, NAMED(LIB, token) NAMED(QUOTED_STRING, libname) TOKEN(EOS)) { + if (P->psi.file.ln) { + P->error(P, token, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text); + } else { + P->psi.file.ln = strndup(libname->text + 1, libname->size - 2); + } + free(libname); + free(token); +} +PARSE(block, TYPED(decl, decl)) { + P->decls = add_decl(P->decls, decl); +} + +PARSE(block, TYPED(impl, impl)) { + P->impls = add_impl(P->impls, impl); +} + +PARSE(block, TYPED(decl_typedef, def)) { + P->defs = add_decl_typedef(P->defs, def); + switch (def->type->type) { + case PSI_T_STRUCT: + if (def->type->real.strct) { + P->structs = add_decl_struct(P->structs, def->type->real.strct); + } + break; + case PSI_T_UNION: + if (def->type->real.unn) { + P->unions = add_decl_union(P->unions, def->type->real.unn); + } + break; + case PSI_T_ENUM: + if (def->type->real.enm) { + P->enums = add_decl_enum(P->enums, def->type->real.enm); + } + break; + } +} + +PARSE(block, TYPED(constant, constant)) { + P->consts = add_constant(P->consts, constant); +} + +PARSE(block, TYPED(decl_struct, strct)) { + P->structs = add_decl_struct(P->structs, strct); +} + +PARSE(block, TYPED(decl_union, u)) { + P->unions = add_decl_union(P->unions, u); +} + +PARSE(block, TYPED(decl_enum, e)) { + P->enums = add_decl_enum(P->enums, e); +} + +PARSE_NAMED(optional_name, n, ) { + n = NULL; +} + +PARSE_NAMED(optional_name, n, + NAMED(NAME, N)) { + n = N; +} + +PARSE_NAMED(enum_name, n, + NAMED(ENUM, E) NAMED(optional_name, N)) { + if (N) { + n = N; + free(E); + } else { + char digest[17]; + + PSI_TokenHash(E, digest); + n = PSI_TokenTranslit(PSI_TokenAppend(E, 1, digest), " ", "@"); + } +} + +PARSE_TYPED(decl_enum, e, + NAMED(enum_name, N) + TOKEN(LBRACE) + TYPED(decl_enum_items, list) + TOKEN(RBRACE)) { + e = init_decl_enum(N->text, list); + e->token = N; +} + +PARSE_TYPED(decl_enum_items, l, + TYPED(decl_enum_item, i)) { + l = init_decl_enum_items(i); +} +PARSE_TYPED(decl_enum_items, l, + TYPED(decl_enum_items, l_) + TOKEN(COMMA) + TYPED(decl_enum_item, i)) { + l = add_decl_enum_item(l_, i); +} + +PARSE_TYPED(decl_enum_item, i, + NAMED(NAME, N) + TOKEN(EQUALS) + TYPED(num_exp, num)) { + i = init_decl_enum_item(N->text, num); + i->token = N; +} +PARSE_TYPED(decl_enum_item, i, + NAMED(NAME, N)) { + i = init_decl_enum_item(N->text, NULL); + i->token = N; +} + +PARSE_NAMED(union_name, n, + NAMED(UNION, U) + NAMED(optional_name, N)) { + if (N) { + n = N; + free(U); + } else { + char digest[17]; + + PSI_TokenHash(U, digest); + n = PSI_TokenTranslit(PSI_TokenAppend(U, 1, digest), " ", "@"); + } +} + +PARSE_NAMED(struct_name, n, + NAMED(STRUCT, S) + NAMED(optional_name, N)) { + if (N) { + n = N; + free(S); + } else { + char digest[17]; + + PSI_TokenHash(S, digest); + n = PSI_TokenTranslit(PSI_TokenAppend(S, 1, digest), " ", "@"); + } +} + +PARSE_TYPED(decl_struct_args_block, args_, + TOKEN(LBRACE) + TYPED(struct_args, args) + TOKEN(RBRACE)) { + args_ = args; +} + +PARSE_TYPED(decl_struct_args, args_, + TYPED(decl_struct_args_block, args)) { + args_ = args; +} +PARSE_TYPED(decl_struct_args, args_, + TOKEN(EOS)) { + args_ = init_decl_args(NULL); +} + +PARSE_TYPED(decl_struct, strct, + TOKEN(STRUCT) + NAMED(NAME, N) + TYPED(align_and_size, as) + TYPED(decl_struct_args, args)) { + strct = init_decl_struct(N->text, args); + strct->align = as.pos; + strct->size = as.len; + strct->token = N; +} + +PARSE_TYPED(align_and_size, as, ) { + as.pos = 0; + as.len = 0; +} +PARSE_TYPED(align_and_size, as, + TOKEN(COLON) + TOKEN(COLON) + TOKEN(LPAREN) + NAMED(NUMBER, A) + TOKEN(COMMA) + NAMED(NUMBER, S) + TOKEN(RPAREN)) { + as.pos = atol(A->text); + as.len = atol(S->text); + free(A); + free(S); +} + +PARSE_TYPED(decl_union, u, + TOKEN(UNION) + NAMED(NAME, N) + TYPED(align_and_size, as) + TYPED(decl_struct_args, args)) { + u = init_decl_union(N->text, args); + u->align = as.pos; + u->size = as.len; + u->token = N; +} + +PARSE_TYPED(const_type, type_, + NAMED(const_type_token, T)) { + type_ = init_const_type(T->type, T->text); + free(T); +} + +PARSE_TYPED(constant, constant, + TOKEN(CONST) + TYPED(const_type, type) + NAMED(NSNAME, T) + TOKEN(EQUALS) + TYPED(impl_def_val, val) + TOKEN(EOS)) { + constant = init_constant(type, T->text, val); + free(T); +} + +PARSE_TYPED(decl_typedef, def, + NAMED(TYPEDEF, T) + TYPED(decl_typedef_body, def_) + TOKEN(EOS)) { + def = def_; + def->token = T; +} + +PARSE_TYPED(decl_typedef_body_ex, def, + NAMED(struct_name, N) + TYPED(align_and_size, as) + TYPED(decl_struct_args_block, args) + TYPED(decl_var, var)) { + def = init_decl_arg(init_decl_type(PSI_T_STRUCT, N->text), var); + def->type->token = PSI_TokenCopy(N); + def->type->real.strct = init_decl_struct(N->text, args); + def->type->real.strct->token = N; + def->type->real.strct->align = as.pos; + def->type->real.strct->size = as.len; +} +PARSE_TYPED(decl_typedef_body_ex, def, + NAMED(union_name, N) + TYPED(align_and_size, as) + TYPED(decl_struct_args_block, args) + TYPED(decl_var, var)) { + def = init_decl_arg(init_decl_type(PSI_T_UNION, N->text), var); + def->type->token = PSI_TokenCopy(N); + def->type->real.unn = init_decl_union(N->text, args); + def->type->real.unn->token = N; + def->type->real.unn->align = as.pos; + def->type->real.unn->size = as.len; +} +PARSE_TYPED(decl_typedef_body_ex, def, + TYPED(decl_enum, e) + NAMED(NAME, ALIAS)) { + def = init_decl_arg(init_decl_type(PSI_T_ENUM, e->name), init_decl_var(ALIAS->text, 0, 0)); + def->var->token = ALIAS; + def->type->token = PSI_TokenCopy(e->token); + def->type->real.enm = e; +} + +PARSE_TYPED(decl_typedef_body, def, + TYPED(decl_typedef_body_ex, def_)) { + def = def_; +} + +PARSE_TYPED(decl_typedef_body_fn_args, args, + TOKEN(LPAREN) + TYPED(decl_args, args_) + TOKEN(RPAREN)) { + args = args_; +} +PARSE_TYPED(decl_typedef_body, def, + TYPED(decl_func, func_) + TYPED(decl_typedef_body_fn_args, args)) { + def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var)); + def->type->token = PSI_TokenCopy(func_->token); + def->type->real.func = init_decl(init_decl_abi("default"), func_, args); +} +PARSE_TYPED(decl_typedef_body, def, + TYPED(decl_arg, arg)) { + def = arg; +} + +PARSE_TYPED(decl, decl, + TYPED(decl_abi, abi) + TYPED(decl_func, func) + TOKEN(LPAREN) + TYPED(decl_args, args) + TOKEN(RPAREN) + TOKEN(EOS)) { + decl = init_decl(abi, func, args); +} + +PARSE_TYPED(decl_func, func, + TYPED(decl_arg, arg)) { + func = arg; +} +/* special case for void functions */ +PARSE_TYPED(decl_func, func, + NAMED(VOID, T) + NAMED(NAME, N)) { + func = init_decl_arg( + init_decl_type(T->type, T->text), + init_decl_var(N->text, 0, 0) + ); + func->type->token = T; + func->var->token = N; + func->token = N; +} +PARSE_TYPED(decl_typedef_body, def, + NAMED(VOID, T) + TYPED(indirection, decl_i) + TOKEN(LPAREN) + TYPED(indirection, type_i) + NAMED(NAME, N) + TOKEN(RPAREN) + TYPED(decl_typedef_body_fn_args, args)) { + decl_arg *func_ = init_decl_arg( + init_decl_type(T->type, T->text), + init_decl_var(N->text, decl_i, 0) + ); + func_->type->token = T; + func_->var->token = N; + func_->token = N; + + def = init_decl_arg( + init_decl_type(PSI_T_FUNCTION, func_->var->name), + copy_decl_var(func_->var) + ); + def->var->pointer_level = type_i; + def->type->token = PSI_TokenCopy(func_->token); + def->type->real.func = init_decl(init_decl_abi("default"), func_, args); +} +PARSE_TYPED(decl_typedef_body, def, + TOKEN(CONST) + NAMED(VOID, T) + TYPED(pointers, decl_i) + TOKEN(LPAREN) + TYPED(indirection, type_i) + NAMED(NAME, N) + TOKEN(RPAREN) + TYPED(decl_typedef_body_fn_args, args)) { + decl_arg *func_ = init_decl_arg( + init_decl_type(T->type, T->text), + init_decl_var(N->text, decl_i, 0) + ); + func_->type->token = T; + func_->var->token = N; + func_->token = N; + + def = init_decl_arg( + init_decl_type(PSI_T_FUNCTION, func_->var->name), + copy_decl_var(func_->var) + ); + def->var->pointer_level = type_i; + def->type->token = PSI_TokenCopy(func_->token); + def->type->real.func = init_decl(init_decl_abi("default"), func_, args); +} + +PARSE_TYPED(decl_abi, abi, + NAMED(NAME, T)) { + abi = init_decl_abi(T->text); + abi->token = T; +} + +PARSE_TYPED(decl_var, var, + TYPED(indirection, p) + NAMED(NAME, T)) { + var = init_decl_var(T->text, p, 0); + var->token = T; +} +PARSE_TYPED(decl_var, var, + TYPED(indirection, p) + NAMED(NAME, T) + TOKEN(LBRACKET) + NAMED(NUMBER, D) + TOKEN(RBRACKET)) { + var = init_decl_var(T->text, p+1, atol(D->text)); + var->token = T; + free(D); +} + +PARSE_TYPED(decl_vars, vars, + TYPED(decl_var, var)) { + vars = init_decl_vars(var); +} +PARSE_TYPED(decl_vars, vars, + TYPED(decl_vars, vars_) + TOKEN(COMMA) + TYPED(decl_var, var)) { + vars = add_decl_var(vars_, var); +} + +PARSE_TYPED(decl_arg, arg_, + TYPED(const_decl_type, type) + TYPED(decl_var, var)) { + arg_ = init_decl_arg(type, var); +} +PARSE_TYPED(decl_typedef_body, def, + TYPED(const_decl_type, type_) + TYPED(indirection, decl_i) + TOKEN(LPAREN) + TYPED(indirection, type_i) + NAMED(NAME, N) + TOKEN(RPAREN) + TYPED(decl_typedef_body_fn_args, args)) { + decl_arg *func_ = init_decl_arg( + type_, + init_decl_var(N->text, decl_i, 0) + ); + func_->var->token = N; + func_->token = N; + + def = init_decl_arg( + init_decl_type(PSI_T_FUNCTION, func_->var->name), + copy_decl_var(func_->var) + ); + def->var->pointer_level = type_i; + def->type->token = PSI_TokenCopy(func_->token); + def->type->real.func = init_decl(init_decl_abi("default"), func_, args); +} + +/* void pointers need a specific rule */ +PARSE_TYPED(decl_arg, arg_, + NAMED(VOID, T) + TYPED(pointers, p) + NAMED(NAME, N)) { + arg_ = init_decl_arg( + init_decl_type(T->type, T->text), + init_decl_var(N->text, p, 0) + ); + arg_->type->token = T; + arg_->var->token = N; + arg_->token = N; +} +PARSE_TYPED(decl_arg, arg_, + TOKEN(CONST) + NAMED(VOID, T) + TYPED(pointers, p) + NAMED(NAME, N)) { + arg_ = init_decl_arg( + init_decl_type(T->type, T->text), + init_decl_var(N->text, p, 0) + ); + arg_->type->token = T; + arg_->var->token = N; + arg_->token = N; +} + +LET(decl_args, ) +LET(decl_args, VOID) +PARSE_TYPED(decl_args, args, + TYPED(decl_arg, arg)) { + args = init_decl_args(arg); +} +PARSE_TYPED(decl_args, args, + TYPED(decl_args, args_) + TOKEN(COMMA) + TYPED(decl_arg, arg)) { + args = add_decl_arg(args_, arg); +} +PARSE_TYPED(decl_args, args, + TYPED(decl_args, args_) + TOKEN(COMMA) + TOKEN(ELLIPSIS)) { + args = args_; + args->varargs = 1; +} + +PARSE_TYPED(struct_args, args, + TYPED(struct_arg, arg)) { + args = init_decl_args(arg); +} +PARSE_TYPED(struct_args, args, + TYPED(struct_args, args_) + TYPED(struct_arg, arg)) { + args = add_decl_arg(args_, arg); +} + +PARSE_TYPED(struct_arg, arg_, + TYPED(decl_typedef_body_ex, def) + TOKEN(EOS)) { + arg_ = def; + switch (def->type->type) { + case PSI_T_STRUCT: + if (def->type->real.strct) { + P->structs = add_decl_struct(P->structs, def->type->real.strct); + } + break; + case PSI_T_UNION: + if (def->type->real.unn) { + P->unions = add_decl_union(P->unions, def->type->real.unn); + } + break; + case PSI_T_ENUM: + if (def->type->real.enm) { + P->enums = add_decl_enum(P->enums, def->type->real.enm); + } + break; + } +} +PARSE_TYPED(struct_arg, arg, + TYPED(decl_arg, arg_) + TYPED(struct_layout, layout_) + TOKEN(EOS)) { + arg_->layout = layout_; + arg = arg_; +} + +PARSE_TYPED(struct_layout, layout, ) { + layout = NULL; +} +PARSE_TYPED(struct_layout, layout, + TOKEN(COLON) + TOKEN(COLON) + TOKEN(LPAREN) + NAMED(NUMBER, POS) + TOKEN(COMMA) + NAMED(NUMBER, SIZ) + TOKEN(RPAREN)) { + layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text)); + free(POS); + free(SIZ); +} + +/* un/signed, urgh */ +PARSE_NAMED(decl_scalar_type, type_, + NAMED(CHAR, C)) { + type_ = C; +} +PARSE_NAMED(decl_scalar_type, type_, + NAMED(SHORT, S) + NAMED(decl_scalar_type_short, s)) { + if (s) { + type_ = PSI_TokenCat(2, S, s); + free(S); + free(s); + } else { + type_ = S; + } +} +PARSE_NAMED(decl_scalar_type_short, s, ) { + s = NULL; +} + +PARSE_NAMED(decl_scalar_type_short, s, + NAMED(INT, I)) { + s = I; +} +PARSE_NAMED(decl_scalar_type, type_, + NAMED(INT, I)) { + type_ = I; +} +PARSE_NAMED(decl_scalar_type, type_, + NAMED(LONG, L) + NAMED(decl_scalar_type_long, l)) { + if (l) { + type_ = PSI_TokenCat(2, L, l); + free(L); + free(l); + } else { + type_ = L; + } +} +PARSE_NAMED(decl_scalar_type_long, l, ) { + l = NULL; +} +PARSE_NAMED(decl_scalar_type_long, l, + NAMED(DOUBLE, D)) { + l = D; +} +PARSE_NAMED(decl_scalar_type_long, l, + NAMED(LONG, L) + NAMED(decl_scalar_type_long_long, ll)) { + if (ll) { + l = PSI_TokenCat(2, L, ll); + free(L); + free(ll); + } else { + l = L; + } +} +PARSE_NAMED(decl_scalar_type_long_long, ll, ) { + ll = NULL; +} +PARSE_NAMED(decl_scalar_type_long_long, ll, + NAMED(INT, I)) { + ll = I; +} +PARSE_TYPED(decl_type, type_, + NAMED(UNSIGNED, U) + NAMED(decl_scalar_type, N)) { + PSI_Token *T = PSI_TokenCat(2, U, N); + type_ = init_decl_type(T->type, T->text); + type_->token = T; + free(U); + free(N); +} +PARSE_TYPED(decl_type, type_, + NAMED(SIGNED, S) + NAMED(decl_scalar_type, N)) { + PSI_Token *T = PSI_TokenCat(2, S, N); + type_ = init_decl_type(T->type, T->text); + type_->token = T; + free(S); + free(N); +} +PARSE_TYPED(decl_type, type_, + NAMED(UNSIGNED, U)) { + type_ = init_decl_type(PSI_T_NAME, U->text); + type_->token = U; +} +PARSE_TYPED(decl_type, type_, + NAMED(SIGNED, S)) { + type_ = init_decl_type(PSI_T_NAME, S->text); + type_->token = S; +} +PARSE_TYPED(decl_type, type_, + NAMED(decl_scalar_type, N)) { + type_ = init_decl_type(N->type, N->text); + type_->token = N; +} +/* structs ! */ +PARSE_TYPED(decl_type, type_, + NAMED(STRUCT, S) + NAMED(NAME, T)) { + type_ = init_decl_type(S->type, T->text); + type_->token = T; + free(S); +} +PARSE_TYPED(decl_type, type_, + NAMED(UNION, U) + NAMED(NAME, T)) { + type_ = init_decl_type(U->type, T->text); + type_->token = T; + free(U); +} +PARSE_TYPED(decl_type, type_, + NAMED(ENUM, E) + NAMED(NAME, T)) { + type_ = init_decl_type(E->type, T->text); + type_->token = T; + free(E); +} +PARSE_TYPED(decl_type, type_, + NAMED(decl_type_token, T)) { + type_ = init_decl_type(T->type, T->text); + type_->token = T; +} + + +PARSE_TYPED(const_decl_type, type, + TYPED(decl_type, type_)) { + type = type_; +} +PARSE_TYPED(const_decl_type, type, + TOKEN(CONST) + TYPED(decl_type, type_)) { + type = type_; +} + +PARSE_TYPED(impl, impl, + TYPED(impl_func, func) + TOKEN(LBRACE) + TYPED(impl_stmts, stmts) + TOKEN(RBRACE)) { + impl = init_impl(func, stmts); +} + +PARSE_TYPED(impl_func, func, + TOKEN(FUNCTION) + TYPED(reference, r) + NAMED(NSNAME, NAME) + TYPED(impl_args, args) + TOKEN(COLON) + TYPED(impl_type, type)) { + func = init_impl_func(NAME->text, args, type, r); + func->token = NAME; +} + +PARSE_TYPED(impl_def_val, def, + NAMED(impl_def_val_token, T)) { + def = init_impl_def_val(T->type, T->text); + free(T); +} + +PARSE_TYPED(impl_var, var, + TYPED(reference, r) + NAMED(DOLLAR_NAME, T)) { + var = init_impl_var(T->text, r); + var->token = T; +} + +PARSE_TYPED(impl_arg, arg, + TYPED(impl_type, type) + TYPED(impl_var, var)) { + arg = init_impl_arg(type, var, NULL); +} +PARSE_TYPED(impl_arg, arg, + TYPED(impl_type, type) + TYPED(impl_var, var) + TOKEN(EQUALS) + TYPED(impl_def_val, def)) { + arg = init_impl_arg(type, var, def); +} + +PARSE_TYPED(impl_args, args, + TOKEN(LPAREN) + TOKEN(RPAREN)) { + args = NULL; +} +PARSE_TYPED(impl_args, args, + TOKEN(LPAREN) + TYPED(impl_arg_list, args_) + TOKEN(RPAREN)) { + args = args_; +} +PARSE_TYPED(impl_args, args, + TOKEN(LPAREN) + TYPED(impl_arg_list, args_) + TOKEN(COMMA) + TYPED(impl_vararg, va) + TOKEN(RPAREN)) { + args = args_; + args->vararg.name = va; +} + +PARSE_TYPED(impl_vararg, va, + TYPED(impl_type, type) + TYPED(reference, r) + TOKEN(ELLIPSIS) + NAMED(DOLLAR_NAME, T)) { + va = init_impl_arg(type, init_impl_var(T->text, r), NULL); + free(T); +} + +PARSE_TYPED(impl_arg_list, args, + TYPED(impl_arg, arg)) { + args = init_impl_args(arg); +} +PARSE_TYPED(impl_arg_list, args, + TYPED(impl_arg_list, args_) + TOKEN(COMMA) + TYPED(impl_arg, arg)) { + args = add_impl_arg(args_, arg); +} + +PARSE_TYPED(impl_stmts, stmts, + TYPED(impl_stmt, stmt)) { + stmts = init_impl_stmts(stmt); +} +PARSE_TYPED(impl_stmts, stmts, + TYPED(impl_stmts, stmts_) + TYPED(impl_stmt, stmt)) { + stmts = add_impl_stmt(stmts_, stmt); +} + +PARSE_TYPED(impl_stmt, stmt, + TYPED(let_stmt, let)) { + stmt = init_impl_stmt(PSI_T_LET, let); +} +PARSE_TYPED(impl_stmt, stmt, + TYPED(set_stmt, set)) { + stmt = init_impl_stmt(PSI_T_SET, set); +} +PARSE_TYPED(impl_stmt, stmt, + TYPED(return_stmt, ret)) { + stmt = init_impl_stmt(PSI_T_RETURN, ret); +} +PARSE_TYPED(impl_stmt, stmt, + TYPED(free_stmt, free)) { + stmt = init_impl_stmt(PSI_T_FREE, free); +} + +PARSE_TYPED(num_exp, exp, + NAMED(num_exp_token, tok)) { + exp = init_num_exp(tok->type, tok->text); + exp->token = tok; +} +PARSE_TYPED(num_exp, exp, + TYPED(decl_var, var)) { + exp = init_num_exp(PSI_T_NAME, var); + exp->token = PSI_TokenCopy(var->token); +} +PARSE_TYPED(num_exp, exp, + TYPED(num_exp, exp_) + NAMED(num_exp_op_token, operator_) + TYPED(num_exp, operand_)) { + exp_->operator = operator_->type; + exp_->operand = operand_; + exp = exp_; + free(operator_); +}; + +PARSE_TYPED(let_stmt, let, + TOKEN(LET) + TYPED(decl_var, var) + TOKEN(EOS)) { + let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL)); +} +PARSE_TYPED(let_stmt, let, + TOKEN(LET) + TYPED(decl_var, var) + TOKEN(EQUALS) + TYPED(reference, r) + TYPED(let_val, val) + TOKEN(EOS)) { + val->flags.one.is_reference = r ? 1 : 0; + let = init_let_stmt(var, val); +} +PARSE_TYPED(let_stmt, let, + TOKEN(TEMP) + TYPED(decl_var, var) + TOKEN(EQUALS) + TYPED(decl_var, val) + TOKEN(EOS)) { + let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val)); +} + +PARSE_TYPED(let_calloc, alloc, + TYPED(num_exp, nmemb) + TOKEN(COMMA) + TYPED(num_exp, size)) { + alloc = init_let_calloc(nmemb, size); +} + +PARSE_TYPED(let_func, func, + NAMED(let_func_token, T) + TOKEN(LPAREN) + TYPED(impl_var, var) + TOKEN(RPAREN)) { + func = init_let_func(T->type, T->text, var); + free(T); +} + +LET(callback_arg_list, ) +PARSE_TYPED(callback_arg_list, args, + TYPED(callback_args, args_)) { + args = args_; +} + +PARSE_TYPED(callback_args, args, + TYPED(set_value, val)) { + args = init_set_values(val); +} +PARSE_TYPED(callback_args, args, + TYPED(callback_args, args_) + TOKEN(COMMA) + TYPED(set_value, val)) { + args = add_set_value(args_, val); +} + +PARSE_TYPED(let_val, val, + TOKEN(NULL)) { + val = init_let_val(PSI_LET_NULL, NULL); +} +PARSE_TYPED(let_val, val, + TYPED(num_exp, exp)) { + val = init_let_val(PSI_LET_NUMEXP, exp); +} +PARSE_TYPED(let_val, val, + TOKEN(CALLOC) + TOKEN(LPAREN) + TYPED(let_calloc, alloc) + TOKEN(RPAREN)) { + val = init_let_val(PSI_LET_CALLOC, alloc); +} +PARSE_TYPED(let_val, val, + TYPED(let_func, func)) { + val = init_let_val(PSI_LET_FUNC, func); +} +PARSE_TYPED(let_val, val, + TOKEN(CALLBACK) + NAMED(let_func_token, F) + TOKEN(LPAREN) + TYPED(impl_var, var) + TOKEN(LPAREN) + TYPED(callback_arg_list, args_) + TOKEN(RPAREN) + TOKEN(RPAREN)) { + val = init_let_val(PSI_LET_CALLBACK, init_let_callback( + init_let_func(F->type, F->text, var), args_)); + free(F); +} + +PARSE_TYPED(set_stmt, set, + TOKEN(SET) + TYPED(impl_var, var) + TOKEN(EQUALS) + TYPED(set_value, val) + TOKEN(EOS)) { + set = init_set_stmt(var, val); +} + +PARSE_TYPED(set_value, val, + TYPED(set_func, func) + TOKEN(LPAREN) + TYPED(decl_var, var) + TOKEN(RPAREN)) { + val = init_set_value(func, init_decl_vars(var)); +} +PARSE_TYPED(set_value, val, + TYPED(set_func, func) + TOKEN(LPAREN) + TYPED(decl_var, var) + TOKEN(COMMA) + TYPED(num_exp, num_) + TOKEN(RPAREN)) { + val = init_set_value(func, init_decl_vars(var)); + val->num = num_; +} +PARSE_TYPED(set_value, val, + TYPED(set_func, func_) + TOKEN(LPAREN) + TYPED(decl_var, var) + TOKEN(COMMA) + NAMED(ELLIPSIS, T) + TOKEN(RPAREN)) { + free_set_func(func_); + val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var)); + val->func->token = T; +} +PARSE_TYPED(set_value, val, + TYPED(set_func, func_) + TOKEN(LPAREN) + TYPED(decl_var, var) + TOKEN(COMMA) + TYPED(set_vals, vals) + TOKEN(RPAREN)) { + val = vals; + val->func = func_; + val->vars = init_decl_vars(var); +} +PARSE_TYPED(set_value, val, + TYPED(set_func, func_) + TOKEN(LPAREN) + TYPED(decl_var, var) + TOKEN(COMMA) + TYPED(num_exp, num_) + TOKEN(COMMA) + TYPED(set_vals, vals) + TOKEN(RPAREN)) { + val = vals; + val->func = func_; + val->num = num_; + val->vars = init_decl_vars(var); +} + +PARSE_TYPED(set_vals, vals, + TYPED(set_value, val)) { + vals = add_inner_set_value(init_set_value(NULL, NULL), val); +} +PARSE_TYPED(set_vals, vals, + TYPED(set_vals, vals_) + TOKEN(COMMA) + TYPED(set_value, val)) { + vals = add_inner_set_value(vals_, val); +} + +PARSE_TYPED(set_func, func, + NAMED(set_func_token, T)) { + func = init_set_func(T->type, T->text); + func->token = T; +} + +PARSE_TYPED(return_stmt, ret, + NAMED(RETURN, T) + TYPED(set_value, val) + TOKEN(EOS)) { + ret = init_return_stmt(val); + ret->token = T; +} + +PARSE_TYPED(free_stmt, free, + TOKEN(FREE) + TYPED(free_calls, calls) + TOKEN(EOS)) { + free = init_free_stmt(calls); +} + +PARSE_TYPED(free_calls, calls, + TYPED(free_call, call)) { + calls = init_free_calls(call); +} +PARSE_TYPED(free_calls, calls, + TYPED(free_calls, calls_) + TOKEN(COMMA) + TYPED(free_call, call)) { + calls = add_free_call(calls_, call); +} + +PARSE_TYPED(free_call, call, + NAMED(NAME, F) + TOKEN(LPAREN) + TYPED(decl_vars, vars) + TOKEN(RPAREN)) { + call = init_free_call(F->text, vars); + call->token = F; +} + +PARSE_TYPED(impl_type, type_, + NAMED(impl_type_token, T)) { + type_ = init_impl_type(T->type, T->text); + free(T); +} + +PARSE_TYPED(reference, r, ) { + r = 0; +} +PARSE_TYPED(reference, r, + TOKEN(AMPERSAND)) { + r = 1; +} + +PARSE_TYPED(indirection, i, ){ + i = 0; +} +PARSE_TYPED(indirection, i, + TYPED(pointers, p)) { + i = p; +} + +PARSE_TYPED(pointers, p, + TOKEN(ASTERISK)) { + p = 1; +} +PARSE_TYPED(pointers, p, + TYPED(pointers, p_) + TOKEN(ASTERISK)) { + p = p_+1; +} diff --git a/src/parser_proc.y b/src/parser_proc.y index 4d96acb..5813b42 100644 --- a/src/parser_proc.y +++ b/src/parser_proc.y @@ -13,7 +13,6 @@ #include "parser.h" } -%include { void psi_error(int, const char *, int, const char *, ...); } %name psi_parser_proc_ %token_prefix PSI_T_ %token_type {struct psi_token *} diff --git a/src/parser_proc.y.bak b/src/parser_proc.y.bak new file mode 100644 index 0000000..b46053a --- /dev/null +++ b/src/parser_proc.y.bak @@ -0,0 +1,885 @@ +%include { +#include +#include +#include +#include +#include + +#include "parser.h" + +void psi_error(int, const char *, int, const char *, ...); +} + +%name PSI_ParserProc +%token_prefix PSI_T_ +%token_type {PSI_Token *} +%token_destructor {free($$);} +%default_destructor {(void)P;} +%extra_argument {PSI_Parser *P} +/* TOKEN is defined inside syntax_error */ +%syntax_error { + ++P->errors; + if (TOKEN && TOKEN->type != PSI_T_EOF) { + psi_error(PSI_WARNING, TOKEN->file, TOKEN->line, "PSI syntax error: Unexpected token '%s'", TOKEN->text); + } else { + psi_error(PSI_WARNING, P->psi.file.fn, P->line, "PSI syntax error: Unexpected end of input"); + } +} + +%nonassoc NAME. +%left PLUS MINUS. +%left SLASH ASTERISK. +%fallback NAME TEMP FREE SET LET RETURN CALLOC CALLBACK ZVAL LIB STRING. + +file ::= blocks. + +blocks ::= block. +blocks ::= blocks block. + +block ::= EOF. +block ::= EOS. + +block ::= LIB(T) QUOTED_STRING(libname) EOS. { + if (P->psi.file.ln) { + P->error(P, T, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text); + } else { + P->psi.file.ln = strndup(libname->text + 1, libname->size - 2); + } + free(libname); + free(T); +} + +block ::= decl(decl). { + P->decls = add_decl(P->decls, decl); +} +block ::= impl(impl). { + P->impls = add_impl(P->impls, impl); +} +block ::= decl_typedef(def). { + P->defs = add_decl_typedef(P->defs, def); + switch (def->type->type) { + case PSI_T_STRUCT: + if (def->type->real.strct) { + P->structs = add_decl_struct(P->structs, def->type->real.strct); + } + break; + case PSI_T_UNION: + if (def->type->real.unn) { + P->unions = add_decl_union(P->unions, def->type->real.unn); + } + break; + case PSI_T_ENUM: + if (def->type->real.enm) { + P->enums = add_decl_enum(P->enums, def->type->real.enm); + } + break; + } +} +block ::= constant(constant). { + P->consts = add_constant(P->consts, constant); +} +block ::= decl_struct(strct). { + P->structs = add_decl_struct(P->structs, strct); +} +block ::= decl_union(u). { + P->unions = add_decl_union(P->unions, u); +} +block ::= decl_enum(e). { + P->enums = add_decl_enum(P->enums, e); +} + +optional_name(n) ::= .{ + n = NULL; +} +optional_name(n) ::= NAME(N). { + n = N; +} + +enum_name(n) ::= ENUM(E) optional_name(N). { + if (N) { + n = N; + free(E); + } else { + char digest[17]; + + PSI_TokenHash(E, digest); + n = PSI_TokenTranslit(PSI_TokenAppend(E, 1, digest), " ", "@"); + } +} + +%type decl_enum {decl_enum *} +%destructor decl_enum {free_decl_enum($$);} +decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. { + e = init_decl_enum(N->text, list); + e->token = N; +} + +%type decl_enum_items {decl_enum_items*} +%destructor decl_enum_items {free_decl_enum_items($$);} +decl_enum_items(l) ::= decl_enum_item(i). { + l = init_decl_enum_items(i); +} +decl_enum_items(l) ::= decl_enum_items(l_) COMMA decl_enum_item(i). { + l = add_decl_enum_item(l_, i); +} + +%type decl_enum_item {decl_enum_item*} +%destructor decl_enum_item {free_decl_enum_item($$);} +decl_enum_item(i) ::= NAME(N) EQUALS num_exp(num). { + i = init_decl_enum_item(N->text, num); + i->token = N; +} +decl_enum_item(i) ::= NAME(N). { + i = init_decl_enum_item(N->text, NULL); + i->token = N; +} + +union_name(n) ::= UNION(U) optional_name(N). { + if (N) { + n = N; + free(U); + } else { + char digest[17]; + + PSI_TokenHash(U, digest); + n = PSI_TokenTranslit(PSI_TokenAppend(U, 1, digest), " ", "@"); + } +} + +struct_name(n) ::= STRUCT(S) optional_name(N). { + if (N) { + n = N; + free(S); + } else { + char digest[17]; + + PSI_TokenHash(S, digest); + n = PSI_TokenTranslit(PSI_TokenAppend(S, 1, digest), " ", "@"); + } +} + +%type decl_struct_args_block {decl_args*} +%destructor decl_struct_args_block {free_decl_args($$);} +decl_struct_args_block(args_) ::= LBRACE struct_args(args) RBRACE. { + args_ = args; +} +%type decl_struct_args {decl_args*} +%destructor decl_struct_args {free_decl_args($$);} +decl_struct_args(args_) ::= decl_struct_args_block(args). { + args_ = args; +} +decl_struct_args(args_) ::= EOS. { + args_ = init_decl_args(NULL); +} + + +%type decl_struct {decl_struct*} +%destructor decl_struct {free_decl_struct($$);} +decl_struct(strct) ::= STRUCT NAME(N) align_and_size(as) decl_struct_args(args). { + strct = init_decl_struct(N->text, args); + strct->align = as.a; + strct->size = as.s; + strct->token = N; +} + +%type align_and_size { struct {size_t a; size_t s; } } +align_and_size(as) ::= . { + as.a = 0; + as.s = 0; +} +align_and_size(as) ::= COLON COLON LPAREN NUMBER(A) COMMA NUMBER(S) RPAREN. { + as.a = atol(A->text); + as.s = atol(S->text); + free(A); + free(S); +} + +%type decl_union {decl_union*} +%destructor decl_union {free_decl_union($$);} +decl_union(u) ::= UNION NAME(N) align_and_size(as) decl_struct_args(args). { + u = init_decl_union(N->text, args); + u->align = as.a; + u->size = as.s; + u->token = N; +} + +%token_class const_type_token BOOL INT FLOAT STRING. +%type const_type {const_type*} +%destructor const_type {free_const_type($$);} +const_type(type_) ::= const_type_token(T). { + type_ = init_const_type(T->type, T->text); + free(T); +} +%type constant {constant*} +%destructor constant {free_constant($$);} +constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. { + constant = init_constant(type, T->text, val); + free(T); +} + +%type decl_typedef {decl_arg*} +%destructor decl_typedef { + free_decl_arg($$);/* + if ($$->type->real.strct) { + free_decl_struct($$->type->real.strct); + } + if ($$->type->real.enm) { + free_decl_enum($$->type->real.enm); + } + if ($$->type->real.func) { + free_decl($$->type->real.func); + }*/ +} +decl_typedef(def) ::= TYPEDEF(T) decl_typedef_body(def_) EOS. { + def = def_; + def->token = T; +} +%type decl_typedef_body_ex {decl_arg*} +%destructor decl_typedef_body_ex { + free_decl_arg($$);/* + if ($$->type->real.strct) { + free_decl_struct($$->type->real.strct); + } + if ($$->type->real.enm) { + free_decl_enum($$->type->real.enm); + } + if ($$->type->real.unn) { + free_decl_union($$->type->real.unn); + } + if ($$->type->real.func) { + free_decl($$->type->real.func); + }*/ +} +decl_typedef_body_ex(def) ::= struct_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). { + def = init_decl_arg(init_decl_type(PSI_T_STRUCT, N->text), var); + def->type->token = PSI_TokenCopy(N); + def->type->real.strct = init_decl_struct(N->text, args); + def->type->real.strct->token = N; + def->type->real.strct->align = as.a; + def->type->real.strct->size = as.s; +} +decl_typedef_body_ex(def) ::= union_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). { + def = init_decl_arg(init_decl_type(PSI_T_UNION, N->text), var); + def->type->token = PSI_TokenCopy(N); + def->type->real.unn = init_decl_union(N->text, args); + def->type->real.unn->token = N; + def->type->real.unn->align = as.a; + def->type->real.unn->size = as.s; +} +decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). { + def = init_decl_arg(init_decl_type(PSI_T_ENUM, e->name), init_decl_var(ALIAS->text, 0, 0)); + def->var->token = ALIAS; + def->type->token = PSI_TokenCopy(e->token); + def->type->real.enm = e; +} +%type decl_typedef_body {decl_arg*} +%destructor decl_typedef_body { + free_decl_arg($$);/* + if ($$->type->real.strct) { + free_decl_struct($$->type->real.strct); + } + if ($$->type->real.enm) { + free_decl_enum($$->type->real.enm); + } + if ($$->type->real.unn) { + free_decl_union($$->type->real.unn); + } + if ($$->type->real.func) { + free_decl($$->type->real.func); + }*/ +} +decl_typedef_body(def) ::= decl_typedef_body_ex(def_). { + def = def_; +} +%type decl_typedef_body_fn_args {decl_args *} +%destructor decl_typedef_body_fn_args {free_decl_args($$);} +decl_typedef_body_fn_args(args) ::= LPAREN decl_args(args_) RPAREN. { + args = args_; +} +decl_typedef_body(def) ::= decl_func(func_) decl_typedef_body_fn_args(args). { + def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var)); + def->type->token = PSI_TokenCopy(func_->token); + def->type->real.func = init_decl(init_decl_abi("default"), func_, args); +} +decl_typedef_body(def) ::= decl_arg(arg). { + def = arg; +} + +%type decl {decl*} +%destructor decl {free_decl($$);} +decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. { + decl = init_decl(abi, func, args); +} + +%type decl_func {decl_arg*} +%destructor decl_func {free_decl_arg($$);} +decl_func(func) ::= decl_arg(arg). { + func = arg; +} +/* special case for void functions */ +decl_func(func) ::= VOID(T) NAME(N). { + func = init_decl_arg( + init_decl_type(T->type, T->text), + init_decl_var(N->text, 0, 0) + ); + func->type->token = T; + func->var->token = N; + func->token = N; +} +decl_typedef_body(def) ::= VOID(T) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). { + decl_arg *func_ = init_decl_arg( + init_decl_type(T->type, T->text), + init_decl_var(N->text, decl_i, 0) + ); + func_->type->token = T; + func_->var->token = N; + func_->token = N; + + def = init_decl_arg( + init_decl_type(PSI_T_FUNCTION, func_->var->name), + copy_decl_var(func_->var) + ); + def->var->pointer_level = type_i; + def->type->token = PSI_TokenCopy(func_->token); + def->type->real.func = init_decl(init_decl_abi("default"), func_, args); +} +decl_typedef_body(def) ::= CONST VOID(T) pointers(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). { + decl_arg *func_ = init_decl_arg( + init_decl_type(T->type, T->text), + init_decl_var(N->text, decl_i, 0) + ); + func_->type->token = T; + func_->var->token = N; + func_->token = N; + + def = init_decl_arg( + init_decl_type(PSI_T_FUNCTION, func_->var->name), + copy_decl_var(func_->var) + ); + def->var->pointer_level = type_i; + def->type->token = PSI_TokenCopy(func_->token); + def->type->real.func = init_decl(init_decl_abi("default"), func_, args); +} + +%type decl_abi {decl_abi*} +%destructor decl_abi {free_decl_abi($$);} +decl_abi(abi) ::= NAME(T). { + abi = init_decl_abi(T->text); + abi->token = T; +} + +%type decl_var {decl_var*} +%destructor decl_var {free_decl_var($$);} +decl_var(var) ::= indirection(p) NAME(T). { + var = init_decl_var(T->text, p, 0); + var->token = T; +} +decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. { + var = init_decl_var(T->text, p+1, atol(D->text)); + var->token = T; + free(D); +} + +%type decl_vars {decl_vars*} +%destructor decl_vars {free_decl_vars($$);} +decl_vars(vars) ::= decl_var(var). { + vars = init_decl_vars(var); +} +decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). { + vars = add_decl_var(vars_, var); +} + +%type decl_arg {decl_arg*} +%destructor decl_arg {free_decl_arg($$);} +decl_arg(arg_) ::= const_decl_type(type) decl_var(var). { + arg_ = init_decl_arg(type, var); +} +decl_typedef_body(def) ::= const_decl_type(type_) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). { + decl_arg *func_ = init_decl_arg( + type_, + init_decl_var(N->text, decl_i, 0) + ); + func_->var->token = N; + func_->token = N; + + def = init_decl_arg( + init_decl_type(PSI_T_FUNCTION, func_->var->name), + copy_decl_var(func_->var) + ); + def->var->pointer_level = type_i; + def->type->token = PSI_TokenCopy(func_->token); + def->type->real.func = init_decl(init_decl_abi("default"), func_, args); +} + +/* void pointers need a specific rule */ +decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). { + arg_ = init_decl_arg( + init_decl_type(T->type, T->text), + init_decl_var(N->text, p, 0) + ); + arg_->type->token = T; + arg_->var->token = N; + arg_->token = N; +} +decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). { + arg_ = init_decl_arg( + init_decl_type(T->type, T->text), + init_decl_var(N->text, p, 0) + ); + arg_->type->token = T; + arg_->var->token = N; + arg_->token = N; +} + +%type decl_args {decl_args*} +%destructor decl_args {free_decl_args($$);} +decl_args ::= . +decl_args ::= VOID. +decl_args(args) ::= decl_arg(arg). { + args = init_decl_args(arg); +} +decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). { + args = add_decl_arg(args_, arg); +} +decl_args(args) ::= decl_args(args_) COMMA ELLIPSIS. { + args = args_; + args->varargs = 1; +} +%type struct_args {decl_args*} +%destructor struct_args {free_decl_args($$);} +struct_args(args) ::= struct_arg(arg). { + args = init_decl_args(arg); +} +struct_args(args) ::= struct_args(args_) struct_arg(arg). { + args = add_decl_arg(args_, arg); +} +%type struct_arg {decl_arg*} +%destructor struct_arg { + free_decl_arg($$); + /* + if ($$->type->real.strct) { + free_decl_struct($$->type->real.strct); + } + if ($$->type->real.enm) { + free_decl_enum($$->type->real.enm); + } + if ($$->type->real.func) { + free_decl($$->type->real.func); + }*/ +} +struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. { + arg_ = def; + switch (def->type->type) { + case PSI_T_STRUCT: + if (def->type->real.strct) { + P->structs = add_decl_struct(P->structs, def->type->real.strct); + } + break; + case PSI_T_UNION: + if (def->type->real.unn) { + P->unions = add_decl_union(P->unions, def->type->real.unn); + } + break; + case PSI_T_ENUM: + if (def->type->real.enm) { + P->enums = add_decl_enum(P->enums, def->type->real.enm); + } + break; + } +} +struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. { + arg_->layout = layout_; + arg = arg_; +} + +%type struct_layout {decl_struct_layout*} +%destructor struct_layout {free_decl_struct_layout($$);} +struct_layout(layout) ::= . { + layout = NULL; +} +struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. { + layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text)); + free(POS); + free(SIZ); +} + +/* un/signed, urgh */ +decl_scalar_type(type_) ::= CHAR(C). { + type_ = C; +} +decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). { + if (s) { + type_ = PSI_TokenCat(2, S, s); + free(S); + free(s); + } else { + type_ = S; + } +} +decl_scalar_type_short(s) ::= . { + s = NULL; +} + +decl_scalar_type_short(s) ::= INT(I). { + s = I; +} +decl_scalar_type(type_) ::= INT(I). { + type_ = I; +} +decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). { + if (l) { + type_ = PSI_TokenCat(2, L, l); + free(L); + free(l); + } else { + type_ = L; + } +} +decl_scalar_type_long(l) ::= . { + l = NULL; +} +decl_scalar_type_long(l) ::= DOUBLE(D). { + l = D; +} +decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). { + if (ll) { + l = PSI_TokenCat(2, L, ll); + free(L); + free(ll); + } else { + l = L; + } +} +decl_scalar_type_long_long(ll) ::= . { + ll = NULL; +} +decl_scalar_type_long_long(ll) ::= INT(I). { + ll = I; +} +decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). { + PSI_Token *T = PSI_TokenCat(2, U, N); + type_ = init_decl_type(T->type, T->text); + type_->token = T; + free(U); + free(N); +} +decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). { + PSI_Token *T = PSI_TokenCat(2, S, N); + type_ = init_decl_type(T->type, T->text); + type_->token = T; + free(S); + free(N); +} +decl_type(type_) ::= UNSIGNED(U). { + type_ = init_decl_type(PSI_T_NAME, U->text); + type_->token = U; +} +decl_type(type_) ::= SIGNED(S). { + type_ = init_decl_type(PSI_T_NAME, S->text); + type_->token = S; +} +decl_type(type_) ::= decl_scalar_type(N). { + type_ = init_decl_type(N->type, N->text); + type_->token = N; +} +/* structs ! */ +decl_type(type_) ::= STRUCT(S) NAME(T). { + type_ = init_decl_type(S->type, T->text); + type_->token = T; + free(S); +} +decl_type(type_) ::= UNION(U) NAME(T). { + type_ = init_decl_type(U->type, T->text); + type_->token = T; + free(U); +} +decl_type(type_) ::= ENUM(E) NAME(T). { + type_ = init_decl_type(E->type, T->text); + type_->token = T; + free(E); +} +%token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME. +%type decl_type {decl_type*} +%destructor decl_type {free_decl_type($$);} +decl_type(type_) ::= decl_type_token(T). { + type_ = init_decl_type(T->type, T->text); + type_->token = T; +} + + +%type const_decl_type {decl_type*} +%destructor const_decl_type {free_decl_type($$);} +const_decl_type(type) ::= decl_type(type_). { + type = type_; +} +const_decl_type(type) ::= CONST decl_type(type_). { + type = type_; +} + +%type impl {impl*} +%destructor impl {free_impl($$);} +impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. { + impl = init_impl(func, stmts); +} + +%type impl_func {impl_func*} +%destructor impl_func {free_impl_func($$);} +impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) impl_args(args) COLON impl_type(type). { + func = init_impl_func(NAME->text, args, type, r); + func->token = NAME; +} + +%token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING. +%type impl_def_val {impl_def_val*} +%destructor impl_def_val {free_impl_def_val($$);} +impl_def_val(def) ::= impl_def_val_token(T). { + def = init_impl_def_val(T->type, T->text); + free(T); +} + +%type impl_var {impl_var*} +%destructor impl_var {free_impl_var($$);} +impl_var(var) ::= reference(r) DOLLAR_NAME(T). { + var = init_impl_var(T->text, r); + var->token = T; +} + +%type impl_arg {impl_arg*} +%destructor impl_arg {free_impl_arg($$);} +impl_arg(arg) ::= impl_type(type) impl_var(var). { + arg = init_impl_arg(type, var, NULL); +} +impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). { + arg = init_impl_arg(type, var, def); +} + +%type impl_args {impl_args*} +%destructor impl_args {free_impl_args($$);} +impl_args(args) ::= LPAREN RPAREN. { + args = NULL; +} +impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. { + args = args_; +} +impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. { + args = args_; + args->vararg.name = va; +} + +%type impl_vararg {impl_arg*} +%destructor impl_vararg {free_impl_arg($$);} +impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR_NAME(T). { + va = init_impl_arg(type, init_impl_var(T->text, r), NULL); + free(T); +} + +%type impl_arg_list {impl_args*} +%destructor impl_arg_list {free_impl_args($$);} +impl_arg_list(args) ::= impl_arg(arg). { + args = init_impl_args(arg); +} +impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). { + args = add_impl_arg(args_, arg); +} + +%type impl_stmts {impl_stmts*} +%destructor impl_stmts {free_impl_stmts($$);} +impl_stmts(stmts) ::= impl_stmt(stmt). { + stmts = init_impl_stmts(stmt); +} +impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). { + stmts = add_impl_stmt(stmts_, stmt); +} + +%type impl_stmt {impl_stmt*} +%destructor impl_stmt {free_impl_stmt($$);} +impl_stmt(stmt) ::= let_stmt(let). { + stmt = init_impl_stmt(PSI_T_LET, let); +} +impl_stmt(stmt) ::= set_stmt(set). { + stmt = init_impl_stmt(PSI_T_SET, set); +} +impl_stmt(stmt) ::= return_stmt(ret). { + stmt = init_impl_stmt(PSI_T_RETURN, ret); +} +impl_stmt(stmt) ::= free_stmt(free). { + stmt = init_impl_stmt(PSI_T_FREE, free); +} + +%token_class num_exp_token NUMBER NSNAME. +%token_class num_exp_op_token PLUS MINUS ASTERISK SLASH. +%type num_exp {num_exp*} +%destructor num_exp {free_num_exp($$);} +num_exp(exp) ::= num_exp_token(tok). { + exp = init_num_exp(tok->type, tok->text); + exp->token = tok; +} +num_exp(exp) ::= decl_var(var). { + exp = init_num_exp(PSI_T_NAME, var); + exp->token = PSI_TokenCopy(var->token); +} +num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). { + exp_->operator = operator_->type; + exp_->operand = operand_; + exp = exp_; + free(operator_); +} + +%type let_stmt {let_stmt*} +%destructor let_stmt {free_let_stmt($$);} +let_stmt(let) ::= LET decl_var(var) EOS. { + let = init_let_stmt(var, init_let_val(PSI_LET_NULL, NULL)); +} +let_stmt(let) ::= LET decl_var(var) EQUALS reference(r) let_val(val) EOS. { + val->flags.one.is_reference = r ? 1 : 0; + let = init_let_stmt(var, val); +} +let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. { + let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val)); +} + +%type let_calloc {let_calloc*} +%destructor let_calloc {free_let_calloc($$);} +let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). { + alloc = init_let_calloc(nmemb, size); +} +%token_class let_func_token ZVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL. +%type let_func {let_func*} +%destructor let_func {free_let_func($$);} +let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. { + func = init_let_func(T->type, T->text, var); + free(T); +} + +%type callback_arg_list {set_values *} +%destructor callback_arg_list {free_set_values($$);} +callback_arg_list ::= . +callback_arg_list(args) ::= callback_args(args_). { + args = args_; +} + +%type callback_args {set_values *} +%destructor callback_args {free_set_values($$);} +callback_args(args) ::= set_value(val). { + args = init_set_values(val); +} +callback_args(args) ::= callback_args(args_) COMMA set_value(val). { + args = add_set_value(args_, val); +} + +%type let_val {let_val*} +%destructor let_val {free_let_val($$);} +let_val(val) ::= NULL. { + val = init_let_val(PSI_LET_NULL, NULL); +} +let_val(val) ::= num_exp(exp). { + val = init_let_val(PSI_LET_NUMEXP, exp); +} +let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. { + val = init_let_val(PSI_LET_CALLOC, alloc); +} +let_val(val) ::= let_func(func). { + val = init_let_val(PSI_LET_FUNC, func); +} +let_val(val) ::= CALLBACK let_func_token(F) LPAREN impl_var(var) LPAREN callback_arg_list(args_) RPAREN RPAREN. { + val = init_let_val(PSI_LET_CALLBACK, init_let_callback( + init_let_func(F->type, F->text, var), args_)); + free(F); +} + +%type set_stmt {set_stmt*} +%destructor set_stmt {free_set_stmt($$);} +set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. { + set = init_set_stmt(var, val); +} + +%type set_value {set_value*} +%destructor set_value {free_set_value($$);} +set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. { + val = init_set_value(func, init_decl_vars(var)); +} +set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. { + val = init_set_value(func, init_decl_vars(var)); + val->num = num_; +} +set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA ELLIPSIS(T) RPAREN. { + free_set_func(func_); + val = init_set_value(init_set_func(T->type, T->text), init_decl_vars(var)); + val->func->token = T; +} +set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. { + val = vals; + val->func = func_; + val->vars = init_decl_vars(var); +} +set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. { + val = vals; + val->func = func_; + val->num = num_; + val->vars = init_decl_vars(var); +} +%type set_vals {set_value*} +%destructor set_vals {free_set_value($$);} +set_vals(vals) ::= set_value(val). { + vals = add_inner_set_value(init_set_value(NULL, NULL), val); +} +set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). { + vals = add_inner_set_value(vals_, val); +} + +%token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL ZVAL VOID. +%type set_func {set_func*} +%destructor set_func {free_set_func($$);} +set_func(func) ::= set_func_token(T). { + func = init_set_func(T->type, T->text); + func->token = T; +} + +%type return_stmt {return_stmt*} +%destructor return_stmt {free_return_stmt($$);} +return_stmt(ret) ::= RETURN(T) set_value(val) EOS. { + ret = init_return_stmt(val); + ret->token = T; +} + +%type free_stmt {free_stmt*} +%destructor free_stmt {free_free_stmt($$);} +free_stmt(free) ::= FREE free_calls(calls) EOS. { + free = init_free_stmt(calls); +} + +%type free_calls {free_calls*} +%destructor free_calls {free_free_calls($$);} +free_calls(calls) ::= free_call(call). { + calls = init_free_calls(call); +} +free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). { + calls = add_free_call(calls_, call); +} + +%type free_call {free_call*} +%destructor free_call {free_free_call($$);} +free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. { + call = init_free_call(F->text, vars); + call->token = F; +} + +%token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE. +%type impl_type {impl_type*} +%destructor impl_type {free_impl_type($$);} +impl_type(type_) ::= impl_type_token(T). { + type_ = init_impl_type(T->type, T->text); + free(T); +} + +%type reference {char} +reference(r) ::= . {r = 0;} +reference(r) ::= AMPERSAND. {r = 1;} + +%type indirection {unsigned} +indirection(i) ::= . {i = 0;} +indirection(i) ::= pointers(p). {i = p;} + +%type pointers {unsigned} +pointers(p) ::= ASTERISK. {p = 1;} +pointers(p) ::= pointers(P) ASTERISK. {p = P+1;} diff --git a/src/token.c b/src/token.c index a9675f8..4860afd 100644 --- a/src/token.c +++ b/src/token.c @@ -1,3 +1,28 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + #ifdef HAVE_CONFIG_H # include "config.h" #else diff --git a/src/token.h b/src/token.h index c7ce751..fcf3f4e 100644 --- a/src/token.h +++ b/src/token.h @@ -1,10 +1,48 @@ -#ifndef _PSI_TOKEN_H -#define _PSI_TOKEN_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TOKEN_H +#define PSI_TOKEN_H #include #include #include #include +#include + +static inline size_t psi_align(size_t s, size_t a) { + return ((s - 1) | (a - 1)) + 1; +} + +static inline size_t psi_offset_padding(size_t diff, size_t alignment) { + if (diff && diff <= psi_align(diff, alignment)) { + diff = 0; + } + + return diff; +} #include "parser_proc.h" @@ -67,6 +105,17 @@ static inline size_t psi_t_size(token_t t) return 0; } +static inline const char *psi_t_indent(unsigned level) { + static const char indent[] = + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; + return &indent[32 - MAX(0, MIN(32, level))]; +} + +static inline const char *psi_t_indirection(unsigned pointer_level) { + static const char indir[] = "********************************"; + return &indir[32 - MAX(0, MIN(32, pointer_level))]; +} + struct psi_token { token_t type; unsigned size, line, col; diff --git a/src/types.h b/src/types.h index a4e5dad..7c4c896 100644 --- a/src/types.h +++ b/src/types.h @@ -1,12 +1,6 @@ #ifndef _PSI_TYPES_H #define _PSI_TYPES_H -#include "Zend/zend_API.h" -typedef struct zend_fcall { - zend_fcall_info fci; - zend_fcall_info_cache fcc; -} zend_fcall; - #include "token.h" #include "types/impl_val.h" @@ -60,27 +54,6 @@ typedef struct zend_fcall { #include "types/decl_file.h" #include "types/decl_libs.h" - -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; - } -} - -static inline decl_type *real_decl_type(decl_type *type) { - while (weak_decl_type(type)) { - type = type->real.def->type; - } - return type; -} - static inline impl_val *deref_impl_val(impl_val *ret_val, decl_var *var) { unsigned i; @@ -133,9 +106,4 @@ static inline impl_val *struct_member_ref(decl_arg *set_arg, impl_val *struct_pt return ptr; } - -#define PSI_ERROR 16 -#define PSI_WARNING 32 -typedef void (*psi_error_cb)(void *context, struct psi_token *token, int type, const char *msg, ...); - #endif diff --git a/src/types/const_type.c b/src/types/const_type.c new file mode 100644 index 0000000..46b5199 --- /dev/null +++ b/src/types/const_type.c @@ -0,0 +1,52 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include + +#include "const_type.h" + +const_type *init_const_type(token_t type, const char *name) { + const_type *ct = calloc(1, sizeof(*ct)); + ct->type = type; + ct->name = strdup(name); + return ct; +} + +void free_const_type(const_type *type) { + free(type->name); + free(type); +} + +void dump_const_type(int fd, const_type *type) { + dprintf(fd, "%s", type->name); +} diff --git a/src/types/const_type.h b/src/types/const_type.h index af9efb7..7ca6d6e 100644 --- a/src/types/const_type.h +++ b/src/types/const_type.h @@ -1,21 +1,40 @@ -#ifndef _PSI_TYPES_CONST_TYPE_H -#define _PSI_TYPES_CONST_TYPE_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_CONST_TYPE_H +#define PSI_TYPES_CONST_TYPE_H + +#include "token.h" typedef struct const_type { token_t type; char *name; } const_type; -static inline const_type *init_const_type(token_t type, const char *name) { - const_type *ct = calloc(1, sizeof(*ct)); - ct->type = type; - ct->name = strdup(name); - return ct; -} - -static inline void free_const_type(const_type *type) { - free(type->name); - free(type); -} +const_type *init_const_type(token_t type, const char *name); +void free_const_type(const_type *type); +void dump_const_type(int fd, const_type *type); #endif diff --git a/src/types/constant.c b/src/types/constant.c new file mode 100644 index 0000000..dcd8450 --- /dev/null +++ b/src/types/constant.c @@ -0,0 +1,64 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +# else +# include "php_config.h" +#endif + +#include +#include +#include + +#include "constant.h" + +constant *init_constant(const_type *type, const char *name, impl_def_val *val) { + constant *c = calloc(1, sizeof(*c)); + c->type = type; + c->name = strdup(name); + c->val = val; + return c; +} + +void free_constant(constant *constant) { + free_const_type(constant->type); + free(constant->name); + free_impl_def_val(constant->val); + free(constant); +} + +void dump_constant(int fd, constant *cnst) { + dprintf(fd, "const "); + dump_const_type(fd, cnst->type); + dprintf(fd, " %s = ", cnst->name); + dump_impl_def_val(fd, cnst->val); + dprintf(fd, ";"); +} + +int validate_constant(struct psi_data *data, constant *c) { + /* FIXME */ + return 1; +} diff --git a/src/types/constant.h b/src/types/constant.h index 85e467b..c09fc10 100644 --- a/src/types/constant.h +++ b/src/types/constant.h @@ -1,5 +1,33 @@ -#ifndef _PSI_TYPES_CONSTANT_H -#define _PSI_TYPES_CONSTANT_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_CONSTANT_H +#define PSI_TYPES_CONSTANT_H + +#include "const_type.h" +#include "impl_def_val.h" typedef struct constant { const_type *type; @@ -7,19 +35,11 @@ typedef struct constant { impl_def_val *val; } constant; -static inline constant *init_constant(const_type *type, const char *name, impl_def_val *val) { - constant *c = calloc(1, sizeof(*c)); - c->type = type; - c->name = strdup(name); - c->val = val; - return c; -} - -static inline void free_constant(constant *constant) { - free_const_type(constant->type); - free(constant->name); - free_impl_def_val(constant->val); - free(constant); -} +constant *init_constant(const_type *type, const char *name, impl_def_val *val); +void free_constant(constant *constant); +void dump_constant(int fd, constant *cnst); + +struct psi_data; +int validate_constant(struct psi_data *data, constant *c); #endif diff --git a/src/types/constants.c b/src/types/constants.c new file mode 100644 index 0000000..2bcbd49 --- /dev/null +++ b/src/types/constants.c @@ -0,0 +1,65 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "constants.h" + +constants *add_constant(constants *consts, constant *constant) { + if (!consts) { + consts = calloc(1, sizeof(*consts)); + } + consts->list = realloc(consts->list, + ++consts->count * sizeof(*consts->list)); + consts->list[consts->count - 1] = constant; + return consts; +} + +void free_constants(constants *consts) { + size_t i; + for (i = 0; i < consts->count; ++i) { + free_constant(consts->list[i]); + } + free(consts->list); + free(consts); +} + +void dump_constants(int fd, constants *consts) { + size_t i; + + for (i = 0; i < consts->count; ++i) { + constant *cnst = consts->list[i]; + + dump_constant(fd, cnst); + dprintf(fd, "\n"); + } +} diff --git a/src/types/constants.h b/src/types/constants.h index 36352ab..a41cde5 100644 --- a/src/types/constants.h +++ b/src/types/constants.h @@ -1,28 +1,40 @@ -#ifndef _PSI_TYPES_CONSTANTS_H -#define _PSI_TYPES_CONSTANTS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_CONSTANTS_H +#define PSI_TYPES_CONSTANTS_H + +#include "constant.h" typedef struct constants { size_t count; constant **list; } constants; -static inline constants *add_constant(constants *constants, constant *constant) { - if (!constants) { - constants = calloc(1, sizeof(*constants)); - } - constants->list = realloc(constants->list, ++constants->count * sizeof(*constants->list)); - constants->list[constants->count-1] = constant; - return constants; -} - -static inline void free_constants(constants *c) { - size_t i; - - for (i = 0; i < c->count; ++i) { - free_constant(c->list[i]); - } - free(c->list); - free(c); -} +constants *add_constant(constants *constants, constant *constant); +void free_constants(constants *constants); +void dump_constants(int fd, constants *constants); #endif diff --git a/src/types/decl.c b/src/types/decl.c new file mode 100644 index 0000000..eda2303 --- /dev/null +++ b/src/types/decl.c @@ -0,0 +1,127 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include "php_psi_macros.h" +#include "php_psi_redirs.h" + +#include +#include +#include +#include + +#include "data.h" + +decl *init_decl(decl_abi *abi, decl_arg *func, decl_args *args) { + decl *d = calloc(1, sizeof(*d)); + d->abi = abi; + d->func = func; + d->args = args; + return d; +} + +void free_decl(decl *d) { + free_decl_abi(d->abi); + free_decl_arg(d->func); + if (d->args) { + free_decl_args(d->args); + } + free(d); +} + +void dump_decl(int fd, decl *decl) { + dump_decl_abi(fd, decl->abi); + dprintf(fd, " "); + dump_decl_arg(fd, decl->func, 0); + dprintf(fd, "("); + if (decl->args) { + dump_decl_args(fd, decl->args, 0); + } + dprintf(fd, ");"); +} + +static inline int validate_decl_func(struct psi_data *data, void *dl, decl *decl, decl_arg *func) +{ + struct psi_func_redir *redir; + + if (!strcmp(func->var->name, "dlsym")) { + data->error(data, func->token, PSI_WARNING, "Cannot dlsym dlsym (sic!)"); + return 0; + } + + for (redir = &psi_func_redirs[0]; redir->name; ++redir) { + if (!strcmp(func->var->name, redir->name)) { + decl->call.sym = redir->func; + } + } + if (!decl->call.sym) { +#ifndef RTLD_NEXT +# define RTLD_NEXT ((void *) -1l) +#endif + decl->call.sym = dlsym(dl ?: RTLD_NEXT, func->var->name); + if (!decl->call.sym) { + data->error(data, func->token, PSI_WARNING, + "Failed to locate symbol '%s': %s", + func->var->name, dlerror() ?: "not found"); + } + } + return 1; +} + +int validate_decl(struct psi_data *data, void *dl, decl *decl) { + if (!validate_decl_nodl(data, decl)) { + return 0; + } + if (!validate_decl_func(data, dl, decl, decl->func)) { + return 0; + } + return 1; +} + +int validate_decl_nodl(struct psi_data *data, decl *decl) { + if (!validate_decl_abi(data, decl->abi)) { + data->error(data, decl->abi->token, PSI_WARNING, + "Invalid calling convention: '%s'", decl->abi->token->text); + return 0; + } + if (!validate_decl_arg(data, decl->func)) { + return 0; + } + if (decl->args) { + size_t i; + + for (i = 0; i < decl->args->count; ++i) { + if (!validate_decl_arg(data, decl->args->args[i])) { + return 0; + } + } + } + return 1; +} diff --git a/src/types/decl.h b/src/types/decl.h index 0a84998..5a51104 100644 --- a/src/types/decl.h +++ b/src/types/decl.h @@ -1,5 +1,35 @@ -#ifndef _PSI_TYPES_DECL_H -#define _PSI_TYPES_DECL_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_H +#define PSI_TYPES_DECL_H + +#include "decl_abi.h" +#include "decl_arg.h" +#include "decl_args.h" +#include "decl_callinfo.h" typedef struct decl { decl_abi *abi; @@ -9,21 +39,13 @@ typedef struct decl { decl_callinfo call; } decl; -static inline decl* init_decl(decl_abi *abi, decl_arg *func, decl_args *args) { - decl *d = calloc(1, 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); - if (d->args) { - free_decl_args(d->args); - } - free(d); -} +decl *init_decl(decl_abi *abi, decl_arg *func, decl_args *args); +void free_decl(decl *d); +void dump_decl(int fd, decl *decl); + +struct psi_data; + +int validate_decl(struct psi_data *data, void *dl, decl *decl); +int validate_decl_nodl(struct psi_data *data, decl *decl); #endif diff --git a/src/types/decl_abi.c b/src/types/decl_abi.c new file mode 100644 index 0000000..2994f25 --- /dev/null +++ b/src/types/decl_abi.c @@ -0,0 +1,74 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include + +#include "data.h" + +decl_abi *init_decl_abi(const char *convention) { + decl_abi *abi = calloc(1, sizeof(*abi)); + abi->convention = strdup(convention); + return abi; +} + +void free_decl_abi(decl_abi *abi) { + if (abi->token) { + free(abi->token); + } + free(abi->convention); + free(abi); +} + +void dump_decl_abi(int fd, decl_abi *abi) { + dprintf(fd, "%s", abi->convention); +} + +static const char * const abi_ccs[] = { + "default", /* \ */ + "extern", /* > - all the same */ + "cdecl", /* / */ + "mscdecl", + "stdcall", + "fastcall", +}; + +int validate_decl_abi(struct psi_data *data, decl_abi *abi) { + size_t i; + + for (i = 0; i < sizeof(abi_ccs)/sizeof(char*); ++i) { + if (strcasecmp(abi->convention, abi_ccs[i])) { + return 1; + } + } + return 0; +} diff --git a/src/types/decl_abi.h b/src/types/decl_abi.h index 48731a3..7b1ac83 100644 --- a/src/types/decl_abi.h +++ b/src/types/decl_abi.h @@ -1,23 +1,42 @@ -#ifndef _PSI_TYPES_DECL_ABI_H -#define _PSI_TYPES_DECL_ABI_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_ABI_H +#define PSI_TYPES_DECL_ABI_H typedef struct decl_abi { struct psi_token *token; char *convention; } decl_abi; -static inline decl_abi *init_decl_abi(const char *convention) { - decl_abi *abi = calloc(1, sizeof(*abi)); - abi->convention = strdup(convention); - return abi; -} - -static inline void free_decl_abi(decl_abi *abi) { - if (abi->token) { - free(abi->token); - } - free(abi->convention); - free(abi); -} +decl_abi *init_decl_abi(const char *convention); +void free_decl_abi(decl_abi *abi); +void dump_decl_abi(int fd, decl_abi *abi); + +struct psi_data; + +int validate_decl_abi(struct psi_data *data, decl_abi *abi); #endif diff --git a/src/types/decl_arg.c b/src/types/decl_arg.c new file mode 100644 index 0000000..d1f8592 --- /dev/null +++ b/src/types/decl_arg.c @@ -0,0 +1,151 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include + +#include "data.h" + +decl_arg *init_decl_arg(decl_type *type, decl_var *var) { + decl_arg *arg = calloc(1, sizeof(*arg)); + arg->token = var->token; + arg->type = type; + arg->var = var; + var->arg = arg; + arg->ptr = &arg->val; + arg->let = arg->ptr; + return arg; +} + +void free_decl_arg(decl_arg *arg) { + if (arg->token && arg->token != arg->var->token) { + free(arg->token); + } + free_decl_type(arg->type); + free_decl_var(arg->var); + if (arg->layout) { + free_decl_struct_layout(arg->layout); + } + free(arg); +} + +void dump_decl_arg(int fd, decl_arg *arg, unsigned level) { + if (arg->type->type == PSI_T_FUNCTION) { + dump_decl_type(fd, arg->type->real.func->func->type, level); + dprintf(fd, " (*"); + dump_decl_var(fd, arg->var); + dprintf(fd, ")("); + if (arg->type->real.func->args) { + size_t j; + + for (j = 0; j < arg->type->real.func->args->count; ++j) { + if (j) { + dprintf(fd, ", "); + } + dump_decl_arg(fd, arg->type->real.func->args->args[j], level+1); + } + if (arg->type->real.func->args->varargs) { + dprintf(fd, ", ..."); + } + } + dprintf(fd, ")"); + } else { + dump_decl_type(fd, arg->type, level); + dprintf(fd, " "); + dump_decl_var(fd, arg->var); + } +} + +int validate_decl_arg(struct psi_data *data, decl_arg *arg) { + if (!validate_decl_type(data, arg->type, NULL)) { + data->error(data, arg->type->token, PSI_WARNING, + "Cannot use '%s' as type for '%s'", + arg->type->name, arg->var->name); + return 0; + } + return 1; +} + +size_t align_decl_arg(decl_arg *darg, size_t *pos, size_t *len) { + size_t align = alignof_decl_arg(darg); + + assert(align > 0); + + *len = sizeof_decl_arg(darg); + *pos = psi_align(align, *pos); + + return align; +} + +size_t alignof_decl_arg(decl_arg *darg) { + size_t align; + + if (darg->var->pointer_level && (!darg->var->array_size || darg->var->pointer_level > 2)) { + align = psi_t_alignment(PSI_T_POINTER); + } else { + align = alignof_decl_type(darg->type); + } + + return align; +} + +size_t sizeof_decl_arg(decl_arg *darg) { + size_t size; + decl_type *real = real_decl_type(darg->type); + + if (darg->var->array_size) { + if (darg->var->pointer_level > 2) { + size = psi_t_size(PSI_T_POINTER) * darg->var->array_size; + } else { + size = psi_t_size(real->type) * darg->var->array_size; + } + } else if (darg->var->pointer_level) { + size = psi_t_size(PSI_T_POINTER); + } else { + switch (real->type) { + case PSI_T_UNION: + size = real->real.unn->size; + break; + case PSI_T_STRUCT: + size = real->real.strct->size; + break; + case PSI_T_ENUM: + default: + size = psi_t_size(real->type); + break; + } + } + + assert(size > 0); + + return size; +} diff --git a/src/types/decl_arg.h b/src/types/decl_arg.h index 22eb0f0..351cb10 100644 --- a/src/types/decl_arg.h +++ b/src/types/decl_arg.h @@ -1,5 +1,35 @@ -#ifndef _PSI_TYPES_DECL_ARG_H -#define _PSI_TYPES_DECL_ARG_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + *Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + *Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_ARG_H +#define PSI_TYPES_DECL_ARG_H + +#include "decl_type.h" +#include "decl_var.h" +#include "decl_struct_layout.h" +#include "impl_val.h" typedef struct decl_arg { struct psi_token *token; @@ -12,27 +42,16 @@ typedef struct decl_arg { void *mem; } decl_arg; -static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) { - decl_arg *arg = calloc(1, sizeof(*arg)); - arg->token = var->token; - arg->type = type; - arg->var = var; - var->arg = arg; - arg->ptr = &arg->val; - arg->let = arg->ptr; - return arg; -} - -static inline void free_decl_arg(decl_arg *arg) { - if (arg->token && arg->token != arg->var->token) { - free(arg->token); - } - free_decl_type(arg->type); - free_decl_var(arg->var); - if (arg->layout) { - free_decl_struct_layout(arg->layout); - } - free(arg); -} +decl_arg *init_decl_arg(decl_type *type, decl_var *var); +void free_decl_arg(decl_arg *arg); +void dump_decl_arg(int fd, decl_arg *arg, unsigned level); + +struct psi_data; + +int validate_decl_arg(struct psi_data *data, decl_arg *arg); + +size_t align_decl_arg(decl_arg *darg, size_t *pos, size_t *len); +size_t alignof_decl_arg(decl_arg *darg); +size_t sizeof_decl_arg(decl_arg *darg); #endif diff --git a/src/types/decl_args.c b/src/types/decl_args.c new file mode 100644 index 0000000..46c0d25 --- /dev/null +++ b/src/types/decl_args.c @@ -0,0 +1,199 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "data.h" + +decl_args *init_decl_args(decl_arg *arg) { + decl_args *args = calloc(1, sizeof(*args)); + if (arg) { + args->count = 1; + args->args = calloc(1, sizeof(*args->args)); + args->args[0] = arg; + } + return args; +} + +decl_args *add_decl_arg(decl_args *args, decl_arg *arg) { + args->args = realloc(args->args, ++args->count * sizeof(*args->args)); + args->args[args->count - 1] = arg; + return args; +} + +void free_decl_args(decl_args *args) { + size_t i; + for (i = 0; i < args->count; ++i) { + free_decl_arg(args->args[i]); + } + free(args->args); + free(args); +} + +void dump_decl_args(int fd, decl_args *args, unsigned level) { + size_t i; + + for (i = 0; i < args->count; ++i) { + if (i) { + dprintf(fd, ", "); + } + dump_decl_arg(fd, args->args[i], level); + } + if (args->varargs) { + dprintf(fd, ", ..."); + } +} + +void dump_decl_args_with_layout(int fd, decl_args *args, unsigned level) { + size_t j; + + dprintf(fd, " {\n"); + if (args) { + ++level; + for (j = 0; j < args->count; ++j) { + decl_arg *sarg = args->args[j]; + + dprintf(fd, "%s", psi_t_indent(level)); + dump_decl_arg(fd, sarg, level); + dprintf(fd, "::(%zu, %zu);\n", sarg->layout->pos, sarg->layout->len); + } + --level; + } + dprintf(fd, "%s", psi_t_indent(level)); + dprintf(fd, "}"); +} + +decl_arg *locate_decl_arg(decl_args *args, const char *name) { + size_t i; + + if (args) for (i = 0; i < args->count; ++i) { + decl_arg *arg = args->args[i]; + + if (!strcmp(name, arg->var->name)) { + return arg; + } + } + + return NULL; +} + +size_t alignof_decl_args(decl_args *args) { + size_t i, maxalign = 0; + + for (i = 0; i < args->count; ++i) { + decl_arg *darg = args->args[i]; + size_t align = alignof_decl_arg(darg); + + if (align > maxalign) { + maxalign = align; + } + } + + return maxalign; +} + +int validate_decl_arg_args(struct psi_data *data, decl_arg *darg, void *current) { + decl_type *real = real_decl_type(darg->type); + + /* pre-validate any structs/unions/enums */ + switch (real->type) { + case PSI_T_STRUCT: + if (current && current == real->real.strct) { + return 1; + } + if (!locate_decl_type_struct(data->structs, real)) { + return 0; + } + if (!validate_decl_struct(data, real->real.strct)) { + return 0; + } + break; + case PSI_T_UNION: + if (current && current == real->real.unn) { + return 1; + } + if (!locate_decl_type_union(data->unions, real)) { + return 0; + } + if (!validate_decl_union(data, real->real.unn)) { + return 0; + } + break; + case PSI_T_ENUM: + if (current && current == real->real.enm) { + return 1; + } + if (!locate_decl_type_enum(data->enums, real)) { + return 0; + } + if (!validate_decl_enum(data, real->real.enm)) { + return 0; + } + break; + } + + return 1; +} + +static int sort_args_cmp(const void *_a, const void *_b) { + decl_arg *a = *(decl_arg **)_a, *b = *(decl_arg **)_b; + + if (a->layout->pos == b->layout->pos) { + if (a->layout->len == b->layout->len) { + return 0; + } else if (a->layout->len > b->layout->len) { + return -1; + } else { + return 1; + } + } else if (a->layout->pos > b->layout->pos) { + return 1; + } else { + return -1; + } +} + +static void sort_args_swp(void *a, void *b) { + decl_arg **_a = a, **_b = b, *_c; + + _c = *_b; + *_b = *_a; + *_a = _c; +} + +#include "Zend/zend_sort.h" + +void sort_decl_args(decl_args *args) { + zend_insert_sort((void *) args->args, args->count, + sizeof(args), sort_args_cmp, sort_args_swp); +} + diff --git a/src/types/decl_args.h b/src/types/decl_args.h index 5987520..37d7a7f 100644 --- a/src/types/decl_args.h +++ b/src/types/decl_args.h @@ -1,5 +1,32 @@ -#ifndef _PSI_TYPES_DECL_ARGS_H -#define _PSI_TYPES_DECL_ARGS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_ARGS_H +#define PSI_TYPES_DECL_ARGS_H + +#include "decl_arg.h" typedef struct decl_args { decl_arg **args; @@ -7,30 +34,20 @@ typedef struct decl_args { unsigned varargs:1; } decl_args; -static inline decl_args *init_decl_args(decl_arg *arg) { - decl_args *args = calloc(1, sizeof(*args)); - if (arg) { - args->count = 1; - args->args = calloc(1, sizeof(*args->args)); - args->args[0] = arg; - } - return args; -} - -static inline decl_args *add_decl_arg(decl_args *args, decl_arg *arg) { - args->args = realloc(args->args, ++args->count * sizeof(*args->args)); - args->args[args->count-1] = arg; - return args; -} - -static inline void free_decl_args(decl_args *args) { - size_t i; - - for (i = 0; i < args->count; ++i) { - free_decl_arg(args->args[i]); - } - free(args->args); - free(args); -} +decl_args *init_decl_args(decl_arg *arg); +decl_args *add_decl_arg(decl_args *args, decl_arg *arg); +void free_decl_args(decl_args *args); +void dump_decl_args(int fd, decl_args *args, unsigned level); +void dump_decl_args_with_layout(int fd, decl_args *args, unsigned level); + +decl_arg *locate_decl_arg(decl_args *args, const char *name); + +struct psi_data; + +int validate_decl_arg_args(struct psi_data *data, decl_arg *darg, void *current); + +size_t alignof_decl_args(decl_args *args); + +void sort_decl_args(decl_args *args); #endif diff --git a/src/types/decl_callinfo.h b/src/types/decl_callinfo.h index 13e4262..a39c467 100644 --- a/src/types/decl_callinfo.h +++ b/src/types/decl_callinfo.h @@ -1,5 +1,30 @@ -#ifndef _PSI_TYPES_DECL_CALLINFO_H -#define _PSI_TYPES_DECL_CALLINFO_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_CALLINFO_H +#define PSI_TYPES_DECL_CALLINFO_H typedef struct decl_callinfo { void *sym; diff --git a/src/types/decl_enum.c b/src/types/decl_enum.c new file mode 100644 index 0000000..ee1d93a --- /dev/null +++ b/src/types/decl_enum.c @@ -0,0 +1,94 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include + +#include "data.h" + +decl_enum *init_decl_enum(const char *name, decl_enum_items *l) { + decl_enum *e = calloc(1, sizeof(*e)); + e->name = strdup(name); + e->items = l; + return e; +} + +void free_decl_enum(decl_enum *e) { + if (e->token) { + free(e->token); + } + if (e->items) { + free_decl_enum_items(e->items); + } + free(e->name); + free(e); +} + +void dump_decl_enum(int fd, decl_enum *e, unsigned level) { + dprintf(fd, "enum %s {\n", e->name); + if (e->items) { + dump_decl_enum_items(fd, e->items, level); + } + dprintf(fd, "\n}"); +} + +int validate_decl_enum(struct psi_data *data, decl_enum *e) { + size_t j; + + if (!e->items || !e->items->count) { + data->error(data, e->token, PSI_WARNING, "Empty enum '%s'", e->name); + return 0; + } + + for (j = 0; j < e->items->count; ++j) { + decl_enum_item *i = e->items->list[j]; + + if (!i->num) { + if (j) { + i->inc.t = PSI_T_NUMBER; + i->inc.u.numb = "1"; + i->inc.operator = PSI_T_PLUS; + i->inc.operand = i->prev->num ?: &i->prev->inc; + i->num = &i->inc; + } else { + i->inc.t = PSI_T_NUMBER; + i->inc.u.numb = "0"; + i->num = &i->inc; + } + } + if (!validate_num_exp(data, i->num, NULL, NULL, e)) { + return 0; + } + } + + return 1; +} diff --git a/src/types/decl_enum.h b/src/types/decl_enum.h index 4a616af..69d1ee4 100644 --- a/src/types/decl_enum.h +++ b/src/types/decl_enum.h @@ -1,5 +1,33 @@ -#ifndef _PSI_TYPES_DECL_ENUM_H -#define _PSI_TYPES_DECL_ENUM_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_ENUM_H +#define PSI_TYPES_DECL_ENUM_H + +#include "token.h" +#include "decl_enum_items.h" typedef struct decl_enum { struct psi_token *token; @@ -7,23 +35,12 @@ typedef struct decl_enum { decl_enum_items *items; } decl_enum; -static inline decl_enum *init_decl_enum(const char *name, decl_enum_items *l) { - decl_enum *e = calloc(1, sizeof(*e)); - - e->name = strdup(name); - e->items = l; - return e; -} - -static inline void free_decl_enum(decl_enum *e) { - if (e->token) { - free(e->token); - } - if (e->items) { - free_decl_enum_items(e->items); - } - free(e->name); - free(e); -} +decl_enum *init_decl_enum(const char *name, decl_enum_items *l); +void free_decl_enum(decl_enum *e); +void dump_decl_enum(int fd, decl_enum *e, unsigned level); + +struct psi_data; + +int validate_decl_enum(struct psi_data *data, decl_enum *e); #endif diff --git a/src/types/decl_enum_item.c b/src/types/decl_enum_item.c new file mode 100644 index 0000000..26e2d26 --- /dev/null +++ b/src/types/decl_enum_item.c @@ -0,0 +1,54 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include + +#include "decl_enum_item.h" + +decl_enum_item *init_decl_enum_item(const char *name, num_exp *num) { + decl_enum_item *i = calloc(1, sizeof(*i)); + i->name = strdup(name); + i->num = num; + return i; +} + +void free_decl_enum_item(decl_enum_item *i) { + if (i->token) { + free(i->token); + } + if (i->num && i->num != &i->inc) { + free_num_exp(i->num); + } + free(i->name); + free(i); +} diff --git a/src/types/decl_enum_item.h b/src/types/decl_enum_item.h index 3b976de..802d535 100644 --- a/src/types/decl_enum_item.h +++ b/src/types/decl_enum_item.h @@ -1,6 +1,9 @@ #ifndef _PSI_TYPES_DECL_ENUM_ITEM_H #define _PSI_TYPES_DECL_ENUM_ITEM_H +#include "token.h" +#include "num_exp.h" + typedef struct decl_enum_item { struct psi_token *token; char *name; @@ -9,23 +12,7 @@ typedef struct decl_enum_item { struct decl_enum_item *prev; } decl_enum_item; -static inline decl_enum_item *init_decl_enum_item(const char *name, num_exp *num) { - decl_enum_item *i = calloc(1, sizeof(*i)); - - i->name = strdup(name); - i->num = num; - return i; -} - -static inline void free_decl_enum_item(decl_enum_item *i) { - if (i->token) { - free(i->token); - } - if (i->num && i->num != &i->inc) { - free_num_exp(i->num); - } - free(i->name); - free(i); -} +decl_enum_item *init_decl_enum_item(const char *name, num_exp *num); +void free_decl_enum_item(decl_enum_item *i); #endif diff --git a/src/types/decl_enum_items.c b/src/types/decl_enum_items.c new file mode 100644 index 0000000..1ddeed9 --- /dev/null +++ b/src/types/decl_enum_items.c @@ -0,0 +1,83 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "decl_enum_items.h" + +decl_enum_items *init_decl_enum_items(decl_enum_item *i) { + decl_enum_items *l = calloc(1, sizeof(*l)); + if (i) { + l->count = 1; + l->list = calloc(1, sizeof(*l->list)); + l->list[0] = i; + } + return l; +} + +decl_enum_items *add_decl_enum_item(decl_enum_items *l, decl_enum_item *i) { + l->list = realloc(l->list, sizeof(*l->list) * (l->count + 1)); + l->list[l->count] = i; + if (l->count) { + i->prev = l->list[l->count - 1]; + } + ++l->count; + return l; +} + +void free_decl_enum_items(decl_enum_items *l) { + if (l->list) { + size_t j; + for (j = 0; j < l->count; ++j) { + free_decl_enum_item(l->list[j]); + } + free(l->list); + } + free(l); +} + +void dump_decl_enum_items(int fd, decl_enum_items *items, unsigned level) { + size_t i; + for (i = 0; i < items->count; ++i) { + decl_enum_item *item = items->list[i]; + + if (i) { + dprintf(fd, ",\n"); + } + dprintf(fd, "%s", psi_t_indent(level+1)); + dprintf(fd, "%s", item->name); + if (item->num && item->num != &item->inc) { + dprintf(fd, " = "); + dump_num_exp(fd, item->num); + } + } +} diff --git a/src/types/decl_enum_items.h b/src/types/decl_enum_items.h index 0debc0a..c52996f 100644 --- a/src/types/decl_enum_items.h +++ b/src/types/decl_enum_items.h @@ -1,42 +1,41 @@ -#ifndef _PSI_TYPES_DECL_ENUM_ITEMS_H -#define _PSI_TYPES_DECL_ENUM_ITEMS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_ENUM_ITEMS_H +#define PSI_TYPES_DECL_ENUM_ITEMS_H + +#include "decl_enum_item.h" typedef struct decl_enum_items { decl_enum_item **list; size_t count; } decl_enum_items; -static inline decl_enum_items *init_decl_enum_items(decl_enum_item *i) { - decl_enum_items *l = calloc(1, sizeof(*l)); - - if (i) { - l->count = 1; - l->list = calloc(1, sizeof(*l->list)); - l->list[0] = i; - } - return l; -} - -static inline decl_enum_items *add_decl_enum_item(decl_enum_items *l, decl_enum_item *i) { - l->list = realloc(l->list, sizeof(*l->list) * (l->count + 1)); - l->list[l->count] = i; - if (l->count) { - i->prev = l->list[l->count - 1]; - } - ++l->count; - return l; -} - -static inline void free_decl_enum_items(decl_enum_items *l) { - if (l->list) { - size_t j; - - for (j = 0; j < l->count; ++j) { - free_decl_enum_item(l->list[j]); - } - free(l->list); - } - free(l); -} +decl_enum_items *init_decl_enum_items(decl_enum_item *i); +decl_enum_items *add_decl_enum_item(decl_enum_items *l, decl_enum_item *i); +void free_decl_enum_items(decl_enum_items *l); +void dump_decl_enum_items(int fd, decl_enum_items *i, unsigned level); #endif diff --git a/src/types/decl_enums.c b/src/types/decl_enums.c new file mode 100644 index 0000000..28933bf --- /dev/null +++ b/src/types/decl_enums.c @@ -0,0 +1,69 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "decl_enums.h" +#include "decl_type.h" + +decl_enums *add_decl_enum(decl_enums *es, decl_enum *e) { + if (!es) { + es = calloc(1, sizeof(*es)); + } + es->list = realloc(es->list, ++es->count * sizeof(*es->list)); + es->list[es->count - 1] = e; + return es; +} + +void free_decl_enums(decl_enums *es) { + if (es->list) { + size_t j; + for (j = 0; j < es->count; ++j) { + free_decl_enum(es->list[j]); + } + } + free(es->list); + free(es); +} + +void dump_decl_enums(int fd, decl_enums *enums) { + size_t i; + + for (i = 0; i < enums->count; ++i) { + decl_enum *e = enums->list[i]; + + if (!is_anon_type(e->name, "enum")) { + dump_decl_enum(fd, e, 0); + dprintf(fd, "\n"); + } + } +} diff --git a/src/types/decl_enums.h b/src/types/decl_enums.h index 61a3f72..2583538 100644 --- a/src/types/decl_enums.h +++ b/src/types/decl_enums.h @@ -1,30 +1,40 @@ -#ifndef _PSI_TYPES_DECL_ENUMS_H -#define _PSI_TYPES_DECL_ENUMS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_ENUMS_H +#define PSI_TYPES_DECL_ENUMS_H + +#include "decl_enum.h" typedef struct decl_enums { decl_enum **list; size_t count; } decl_enums; -static inline decl_enums* add_decl_enum(decl_enums *es, decl_enum *e) { - if (!es) { - es = calloc(1, sizeof(*es)); - } - es->list = realloc(es->list, ++es->count * sizeof(*es->list)); - es->list[es->count-1] = e; - return es; -} - -static inline void free_decl_enums(decl_enums *es) { - if (es->list) { - size_t j; - - for (j = 0; j < es->count; ++j) { - free_decl_enum(es->list[j]); - } - } - free(es->list); - free(es); -} +decl_enums *add_decl_enum(decl_enums *es, decl_enum *e); +void free_decl_enums(decl_enums *es); +void dump_decl_enums(int fd, decl_enums *es); #endif diff --git a/src/types/decl_file.c b/src/types/decl_file.c new file mode 100644 index 0000000..8bd7936 --- /dev/null +++ b/src/types/decl_file.c @@ -0,0 +1,71 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include +#include + +#include "data.h" + +void free_decl_file(decl_file* file) { + if (file->ln) { + free(file->ln); + } + if (file->fn) { + free(file->fn); + } + memset(file, 0, sizeof(*file)); +} + +int validate_file(struct psi_data *data, void **dlopened) { + char lib[MAXPATHLEN]; + const char *ptr = data->psi.file.ln; + size_t len; + + if (!ptr) { + /* FIXME: assume stdlib */ + return 1; + } else if (!strchr(ptr, '/')) { + len = snprintf(lib, MAXPATHLEN, "lib%s.%s", ptr, PHP_PSI_SHLIB_SUFFIX); + if (MAXPATHLEN == len) { + data->error(data, NULL, PSI_WARNING, "Library name too long: '%s'", ptr); + } + lib[len] = 0; + ptr = lib; + } + if (!(*dlopened = dlopen(ptr, RTLD_LAZY|RTLD_LOCAL))) { + data->error(data, NULL, PSI_WARNING, "Could not open library '%s': %s.", + data->psi.file.ln, dlerror()); + return 0; + } + return 1; +} diff --git a/src/types/decl_file.h b/src/types/decl_file.h index 3eb314c..bc853f2 100644 --- a/src/types/decl_file.h +++ b/src/types/decl_file.h @@ -1,19 +1,40 @@ -#ifndef _PSI_TYPES_DECL_FILE_H -#define _PSI_TYPES_DECL_FILE_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_FILE_H +#define PSI_TYPES_DECL_FILE_H typedef struct decl_file { char *ln; char *fn; } decl_file; -static inline void free_decl_file(decl_file *file) { - if (file->ln) { - free(file->ln); - } - if (file->fn) { - free(file->fn); - } - memset(file, 0, sizeof(*file)); -} +void free_decl_file(decl_file *file); + +struct psi_data; + +int validate_file(struct psi_data *data, void **dlopened); #endif diff --git a/src/types/decl_libs.c b/src/types/decl_libs.c new file mode 100644 index 0000000..7a35f8b --- /dev/null +++ b/src/types/decl_libs.c @@ -0,0 +1,55 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include +#include + +#include "decl_libs.h" + +void free_decl_libs(decl_libs *libs) { + if (libs->dl) { + size_t i; + for (i = 0; i < libs->count; ++i) { + if (libs->dl[i]) { + dlclose(libs->dl[i]); + } + } + free(libs->dl); + } + memset(libs, 0, sizeof(*libs)); +} + +void add_decl_lib(decl_libs *libs, void *dlopened) { + libs->dl = realloc(libs->dl, ++libs->count * sizeof(*libs->dl)); + libs->dl[libs->count - 1] = dlopened; +} diff --git a/src/types/decl_libs.h b/src/types/decl_libs.h index 0ac669e..1bfce0e 100644 --- a/src/types/decl_libs.h +++ b/src/types/decl_libs.h @@ -1,27 +1,37 @@ -#ifndef _PSI_TYPES_DECL_LIBS_H -#define _PSI_TYPES_DECL_LIBS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_LIBS_H +#define PSI_TYPES_DECL_LIBS_H typedef struct decl_libs { void **dl; size_t count; } decl_libs; -static inline void free_decl_libs(decl_libs *libs) { - if (libs->dl) { - size_t i; - for (i = 0; i < libs->count; ++i) { - if (libs->dl[i]) { - dlclose(libs->dl[i]); - } - } - free(libs->dl); - } - memset(libs, 0, sizeof(*libs)); -} - -static inline void add_decl_lib(decl_libs *libs, void *dlopened) { - libs->dl = realloc(libs->dl, ++libs->count * sizeof(*libs->dl)); - libs->dl[libs->count-1] = dlopened; -} +void free_decl_libs(decl_libs *libs); +void add_decl_lib(decl_libs *libs, void *dlopened); #endif diff --git a/src/types/decl_struct.c b/src/types/decl_struct.c new file mode 100644 index 0000000..522e405 --- /dev/null +++ b/src/types/decl_struct.c @@ -0,0 +1,154 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include +#include + +#include "data.h" + +decl_struct* init_decl_struct(const char* name, decl_args* args) { + decl_struct* s = calloc(1, sizeof(*s)); + s->name = strdup(name); + s->args = args; + return s; +} + +void free_decl_struct(decl_struct* s) { + if (s->token) { + free(s->token); + } + if (s->args) { + free_decl_args(s->args); + } + if (s->engine.type && s->engine.dtor) { + s->engine.dtor(s->engine.type); + } + free(s->name); + free(s); +} + +void dump_decl_struct(int fd, decl_struct *strct) { + dprintf(fd, "struct %s::(%zu, %zu)", strct->name, strct->align, strct->size); + if (strct->args && strct->args->count) { + dump_decl_args_with_layout(fd, strct->args, 0); + } else { + dprintf(fd, ";"); + } +} + +decl_arg *locate_decl_struct_member(decl_struct *s, decl_var *var) { + if (s->args) { + return locate_decl_var_arg(var, s->args, NULL); + } + + return NULL; +} + +int validate_decl_struct(struct psi_data *data, decl_struct *s) { + size_t i, pos, len, size, align; + + if (!s->size && !s->args->count) { + data->error(data, s->token, PSI_WARNING, + "Cannot compute size of empty struct '%s'", + s->name); + return 0; + } + + for (i = 0; i < s->args->count; ++i) { + decl_arg *darg = s->args->args[i]; + + if (!validate_decl_arg(data, darg)) { + return 0; + } + + assert(!darg->var->arg || darg->var->arg == darg); + + darg->var->arg = darg; + + if (!validate_decl_arg_args(data, darg, s)) { + return 0; + } else if (darg->layout) { + pos = darg->layout->pos; + + align = align_decl_arg(darg, &pos, &len); + + if (darg->layout->len != len) { + data->error(data, darg->token, PSI_WARNING, + "Computed size %zu of %s.%s does not match" + " pre-defined size %zu of type '%s'", + len, s->name, darg->var->name, darg->layout->len, + darg->type->name); + } + if (darg->layout->pos != pos) { + data->error(data, darg->token, PSI_WARNING, + "Computed offset %zu of %s.%s does not match" + " pre-defined offset %zu", + pos, s->name, darg->var->name, darg->layout->pos); + } + } else { + if (i) { + pos = s->args->args[i-1]->layout->pos + + s->args->args[i-1]->layout->len; + } else { + pos = 0; + } + + align = align_decl_arg(darg, &pos, &len); + darg->layout = init_decl_struct_layout(pos, len); + } + + if (align > s->align) { + s->align = align; + } + } + + sort_decl_args(s->args); + + if (s->args->count) { + decl_arg *darg = s->args->args[s->args->count-1]; + + size = darg->layout->pos + darg->layout->len; + if (s->size < size) { + s->size = psi_align(size, s->align); + } + } + + return 1; +} + +size_t alignof_decl_struct(decl_struct *s) { + if (!s->align) { + s->align = alignof_decl_args(s->args); + } + return s->align; +} diff --git a/src/types/decl_struct.h b/src/types/decl_struct.h index 439b4e9..e72aa38 100644 --- a/src/types/decl_struct.h +++ b/src/types/decl_struct.h @@ -1,5 +1,32 @@ -#ifndef _PSI_TYPES_DECL_STRUCT_H -#define _PSI_TYPES_DECL_STRUCT_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_STRUCT_H +#define PSI_TYPES_DECL_STRUCT_H + +#include "decl_args.h" typedef struct decl_struct { struct psi_token *token; @@ -13,25 +40,16 @@ typedef struct decl_struct { } engine; } decl_struct; -static inline decl_struct *init_decl_struct(const char *name, decl_args *args) { - decl_struct *s = calloc(1, sizeof(*s)); - s->name = strdup(name); - s->args = args; - return s; -} - -static inline void free_decl_struct(decl_struct *s) { - if (s->token) { - free(s->token); - } - if (s->args) { - free_decl_args(s->args); - } - if (s->engine.type && s->engine.dtor) { - s->engine.dtor(s->engine.type); - } - free(s->name); - free(s); -} +decl_struct *init_decl_struct(const char *name, decl_args *args); +void free_decl_struct(decl_struct *s); +void dump_decl_struct(int fd, decl_struct *strct); + +decl_arg *locate_decl_struct_member(decl_struct *s, decl_var *var); + +struct psi_data; + +int validate_decl_struct(struct psi_data *data, decl_struct *s); + +size_t alignof_decl_struct(decl_struct *s); #endif diff --git a/src/types/decl_struct_layout.c b/src/types/decl_struct_layout.c new file mode 100644 index 0000000..8868160 --- /dev/null +++ b/src/types/decl_struct_layout.c @@ -0,0 +1,51 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include + +#include "decl_struct_layout.h" + +decl_struct_layout *init_decl_struct_layout(size_t pos, size_t len) { + decl_struct_layout *l = calloc(1, sizeof(*l)); + + assert(pos + len); + + l->pos = pos; + l->len = len; + + return l; +} + +void free_decl_struct_layout(decl_struct_layout *l) { + free(l); +} diff --git a/src/types/decl_struct_layout.h b/src/types/decl_struct_layout.h index 1fcb9f0..1711e1a 100644 --- a/src/types/decl_struct_layout.h +++ b/src/types/decl_struct_layout.h @@ -1,21 +1,37 @@ -#ifndef _PSI_TYPES_DECL_STRUCT_LAYOUT_H -#define _PSI_TYPES_DECL_STRUCT_LAYOUT_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_STRUCT_LAYOUT_H +#define PSI_TYPES_DECL_STRUCT_LAYOUT_H typedef struct decl_struct_layout { size_t pos; size_t len; } decl_struct_layout; -static inline decl_struct_layout *init_decl_struct_layout(size_t pos, size_t len) { - decl_struct_layout *l = calloc(1, sizeof(*l)); - ZEND_ASSERT(pos+len); - l->pos = pos; - l->len = len; - return l; -} - -static inline void free_decl_struct_layout(decl_struct_layout *l) { - free(l); -} +decl_struct_layout *init_decl_struct_layout(size_t pos, size_t len); +void free_decl_struct_layout(decl_struct_layout *l); #endif diff --git a/src/types/decl_structs.c b/src/types/decl_structs.c new file mode 100644 index 0000000..d479a4a --- /dev/null +++ b/src/types/decl_structs.c @@ -0,0 +1,67 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "decl_structs.h" +#include "decl_type.h" + +decl_structs *add_decl_struct(decl_structs *ss, decl_struct *s) { + if (!ss) { + ss = calloc(1, sizeof(*ss)); + } + ss->list = realloc(ss->list, ++ss->count * sizeof(*ss->list)); + ss->list[ss->count - 1] = s; + return ss; +} + +void free_decl_structs(decl_structs *ss) { + size_t i; + for (i = 0; i < ss->count; ++i) { + free_decl_struct(ss->list[i]); + } + free(ss->list); + free(ss); +} + +void dump_decl_structs(int fd, decl_structs *ss) { + size_t i; + + for (i = 0; i < ss->count; ++i) { + decl_struct *strct = ss->list[i]; + + if (!is_anon_type(strct->name, "struct")) { + dump_decl_struct(fd, strct); + dprintf(fd, "\n"); + } + } +} diff --git a/src/types/decl_structs.h b/src/types/decl_structs.h index 1db3396..251d986 100644 --- a/src/types/decl_structs.h +++ b/src/types/decl_structs.h @@ -1,28 +1,40 @@ -#ifndef _PSI_TYPES_DECL_STRUCTS_H -#define _PSI_TYPES_DECL_STRUCTS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_STRUCTS_H +#define PSI_TYPES_DECL_STRUCTS_H + +#include "decl_struct.h" typedef struct decl_structs { size_t count; decl_struct **list; } decl_structs; -static inline decl_structs *add_decl_struct(decl_structs *ss, decl_struct *s) { - if (!ss) { - ss = calloc(1, sizeof(*ss)); - } - ss->list = realloc(ss->list, ++ss->count * sizeof(*ss->list)); - ss->list[ss->count-1] = s; - return ss; -} - -static inline void free_decl_structs(decl_structs *ss) { - size_t i; - - for (i = 0; i < ss->count; ++i) { - free_decl_struct(ss->list[i]); - } - free(ss->list); - free(ss); -} +decl_structs *add_decl_struct(decl_structs *ss, decl_struct *s); +void free_decl_structs(decl_structs *ss); +void dump_decl_structs(int fd, decl_structs *ss); #endif diff --git a/src/types/decl_type.c b/src/types/decl_type.c new file mode 100644 index 0000000..8e5f9e6 --- /dev/null +++ b/src/types/decl_type.c @@ -0,0 +1,270 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "token.h" +#include "php_psi_stdtypes.h" + +#include "data.h" + +decl_type *init_decl_type(token_t type, const char *name) { + decl_type *t = calloc(1, sizeof(*t)); + t->type = type; + t->name = strdup(name); + return t; +} + +void free_decl_type(decl_type *type) { + if (type->token) { + free(type->token); + } + if (type->type == PSI_T_FUNCTION) { + free_decl(type->real.func); + } + free(type->name); + free(type); +} + +decl_args *extract_decl_type_args(decl_type *dtyp, decl_type **real_typ_ptr) { + decl_type *var_typ; + var_typ = real_decl_type(dtyp); + if (real_typ_ptr) { + *real_typ_ptr = var_typ; + } + switch (var_typ->type) { + case PSI_T_STRUCT: + return var_typ->real.strct->args; + case PSI_T_UNION: + return var_typ->real.unn->args; + default: + return NULL; + } +} + + +int locate_decl_type_alias(decl_typedefs *defs, decl_type *type) { + size_t i; + struct psi_std_type *stdtyp; + + if (type->real.def) { + return 1; + } + if (defs) for (i = 0; i < defs->count; ++i) { + decl_arg *def = defs->list[i]; + + if (def->type->type != type->type && !strcmp(def->var->name, type->name)) { + type->real.def = def; + return 1; + } + } + for (stdtyp = &psi_std_types[0]; stdtyp->type_tag; ++stdtyp) { + if (!strcmp(type->name, stdtyp->alias ?: stdtyp->type_name)) { + type->type = stdtyp->type_tag; + return 1; + } + } + + return 0; +} + +int locate_decl_type_struct(decl_structs *structs, decl_type *type) { + size_t i; + + if (type->real.strct) { + return 1; + } + if (structs) for (i = 0; i < structs->count; ++i) { + if (!strcmp(structs->list[i]->name, type->name)) { + type->real.strct = structs->list[i]; + return 1; + } + } + return 0; +} + +int locate_decl_type_union(decl_unions *unions, decl_type *type) { + size_t i; + + if (type->real.unn) { + return 1; + } + if (unions) for (i = 0; i < unions->count; ++i) { + if (!strcmp(unions->list[i]->name, type->name)) { + type->real.unn = unions->list[i]; + return 1; + } + } + return 0; +} + +int locate_decl_type_enum(decl_enums *enums, decl_type *type) { + size_t i; + + if (type->real.enm) { + return 1; + } + if (enums) for (i = 0; i < enums->count; ++i) { + if (!strcmp(enums->list[i]->name, type->name)) { + type->real.enm = enums->list[i]; + return 1; + } + } + return 0; +} + +int locate_decl_type_decl(decls *decls, decl_type *type) { + size_t i; + + if (type->real.func) { + return 1; + } + if (decls) for (i = 0; i < decls->count; ++i) { + if (!strcmp(decls->list[i]->func->var->name, type->name)) { + type->real.func = decls->list[i]; + return 1; + } + } + + return 0; +} + +int validate_decl_type(struct psi_data *data, decl_type *type, decl_arg *def) { + if (weak_decl_type(type)) { + if (!locate_decl_type_alias(data->defs, type)) { + return 0; + } + if (type->real.def) { + return validate_decl_type(data, type->real.def->type, type->real.def); + } + return 1; + } + + switch (type->type) { + case PSI_T_STRUCT: + if (!locate_decl_type_struct(data->structs, type)) { + return 0; + } + break; + case PSI_T_UNION: + if (!locate_decl_type_union(data->unions, type)) { + return 0; + } + break; + case PSI_T_ENUM: + if (!locate_decl_type_enum(data->enums, type)) { + return 0; + } + break; + case PSI_T_FUNCTION: + if (!locate_decl_type_decl(data->decls, type)) { + return 0; + } + if (!validate_decl_nodl(data, type->real.func)) { + return 0; + } + break; + } + return 1; +} + +void dump_decl_type(int fd, decl_type *t, unsigned level) { + switch (t->type) { + case PSI_T_POINTER: + dprintf(fd, "%s *", t->name); + return; + + case PSI_T_ENUM: + dprintf(fd, "enum "); + if (is_anon_type(t->name, "enum")) { + dump_decl_enum_items(fd, t->real.enm->items, level); + return; + } + break; + + case PSI_T_STRUCT: + dprintf(fd, "struct "); + if (is_anon_type(t->name, "struct")) { + dump_decl_args_with_layout(fd, t->real.strct->args, level); + return; + } + break; + + case PSI_T_UNION: + dprintf(fd, "union "); + if (is_anon_type(t->name, "union")) { + dump_decl_args_with_layout(fd, t->real.unn->args, level); + return; + } + break; + } + dprintf(fd, "%s", t->name); +} + +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; + } +} + +decl_type *real_decl_type(decl_type *type) { + while (weak_decl_type(type)) { + type = type->real.def->type; + } + return type; +} + +size_t alignof_decl_type(decl_type *t) { + decl_type *real = real_decl_type(t); + size_t align; + + switch (real->type) { + case PSI_T_STRUCT: + align = alignof_decl_struct(real->real.strct); + break; + case PSI_T_UNION: + align = alignof_decl_union(real->real.unn); + break; + case PSI_T_ENUM: + default: + align = psi_t_alignment(real->type); + } + + return align; +} diff --git a/src/types/decl_type.h b/src/types/decl_type.h index 8cbb2fc..d7c01fb 100644 --- a/src/types/decl_type.h +++ b/src/types/decl_type.h @@ -1,7 +1,32 @@ -#ifndef _PSI_TYPES_DECL_TYPE_H -#define _PSI_TYPES_DECL_TYPE_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. -#include "parser_proc.h" + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_TYPE_H +#define PSI_TYPES_DECL_TYPE_H + +#include "token.h" typedef struct decl_type { struct psi_token *token; @@ -16,24 +41,33 @@ typedef struct decl_type { } real; } decl_type; -static inline decl_type *init_decl_type(token_t type, const char *name) { - decl_type *t = calloc(1, sizeof(*t)); - t->type = type; - t->name = strdup(name); - return t; -} - -static inline void free_decl(struct decl *decl); -static inline void free_decl_type(decl_type *type) { - if (type->token) { - free(type->token); - } - if (type->type == PSI_T_FUNCTION) { - free_decl(type->real.func); - } - free(type->name); - free(type); -} +#include +#define is_anon_type(name, type) !strncmp(name, type "@", sizeof(type)) + +decl_type *init_decl_type(token_t type, const char *name); +void free_decl_type(decl_type *type); +void dump_decl_type(int fd, decl_type *t, unsigned level); + +struct decl_args *extract_decl_type_args(decl_type *dtyp, decl_type** real_typ_ptr); +int weak_decl_type(decl_type *type); +decl_type *real_decl_type(decl_type *type); + +struct decl_typedefs; +struct decl_structs; +struct decl_unions; +struct decl_enums; +struct decls; + +int locate_decl_type_alias(struct decl_typedefs *defs, decl_type *type); +int locate_decl_type_struct(struct decl_structs *structs, decl_type *type); +int locate_decl_type_union(struct decl_unions *unions, decl_type *type); +int locate_decl_type_enum(struct decl_enums *enums, decl_type *type); +int locate_decl_type_decl(struct decls *decls, decl_type *type); + +struct psi_data; + +int validate_decl_type(struct psi_data *data, decl_type *type, struct decl_arg *def); +size_t alignof_decl_type(decl_type *t); #endif diff --git a/src/types/decl_typedefs.c b/src/types/decl_typedefs.c new file mode 100644 index 0000000..8986005 --- /dev/null +++ b/src/types/decl_typedefs.c @@ -0,0 +1,98 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "data.h" + +decl_typedefs *add_decl_typedef(decl_typedefs *defs, decl_arg *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; +} + +void free_decl_typedefs(decl_typedefs *defs) { + size_t i; + for (i = 0; i < defs->count; ++i) { + free_decl_arg(defs->list[i]); + } + free(defs->list); + free(defs); +} + +static inline void dump_typedef(int fd, decl_arg *tdef) { + dprintf(fd, "typedef "); + dump_decl_arg(fd, tdef, 0); + dprintf(fd, ";"); +} + +void dump_decl_typedefs(int fd, decl_typedefs *defs) { + size_t i; + + for (i = 0; i < defs->count; ++i) { + decl_arg *tdef = defs->list[i]; + + dump_typedef(fd, tdef); + dprintf(fd, "\n"); + } +} + +int validate_decl_typedef(struct psi_data *data, decl_arg *def) { + if (!validate_decl_type(data, def->type, def)) { + const char *pre; + + switch (def->type->type) { + case PSI_T_STRUCT: pre = "struct "; break; + case PSI_T_UNION: pre = "union "; break; + case PSI_T_ENUM: pre = "enum "; break; + default: pre = ""; break; + } + data->error(data, def->token, PSI_WARNING, + "Type '%s' cannot be aliased to %s'%s'", + def->var->name, pre, def->type->name); + return 0; + } + if (def->type->type == PSI_T_VOID) { + if (def->var->pointer_level) { + def->type->type = PSI_T_POINTER; + } else { + data->error(data, def->token, PSI_WARNING, + "Type '%s' cannot be aliased to 'void'", + def->type->name); + return 0; + } + } + return 1; +} diff --git a/src/types/decl_typedefs.h b/src/types/decl_typedefs.h index e58a2f5..7b6a515 100644 --- a/src/types/decl_typedefs.h +++ b/src/types/decl_typedefs.h @@ -1,28 +1,44 @@ -#ifndef _PSI_TYPES_DECL_TYPEDEFS_H -#define _PSI_TYPES_DECL_TYPEDEFS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_TYPEDEFS_H +#define PSI_TYPES_DECL_TYPEDEFS_H + +#include "decl_arg.h" typedef struct decl_typedefs { size_t count; decl_arg **list; } decl_typedefs; -static inline decl_typedefs *add_decl_typedef(decl_typedefs *defs, decl_arg *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 inline void free_decl_typedefs(decl_typedefs *defs) { - size_t i; - - for (i = 0; i < defs->count; ++i) { - free_decl_arg(defs->list[i]); - } - free(defs->list); - free(defs); -} +decl_typedefs *add_decl_typedef(decl_typedefs *defs, decl_arg *def); +void free_decl_typedefs(decl_typedefs *defs); +void dump_decl_typedefs(int fd, decl_typedefs *defs); + +struct psi_data; + +int validate_decl_typedef(struct psi_data *data, decl_arg *def); #endif diff --git a/src/types/decl_union.c b/src/types/decl_union.c new file mode 100644 index 0000000..7e58cbf --- /dev/null +++ b/src/types/decl_union.c @@ -0,0 +1,144 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include +#include + +#include "data.h" + +decl_union* init_decl_union(const char* name, decl_args* args) { + decl_union* u = calloc(1, sizeof(*u)); + u->name = strdup(name); + u->args = args; + return u; +} + +void free_decl_union(decl_union* u) { + if (u->token) { + free(u->token); + } + if (u->args) { + free_decl_args(u->args); + } + free(u->name); + free(u); +} + +void dump_decl_union(int fd, decl_union *unn) { + dprintf(fd, "union %s::(%zu, %zu)", unn->name, unn->align, unn->size); + if (unn->args && unn->args->count) { + dump_decl_args_with_layout(fd, unn->args, 0); + } else { + dprintf(fd, ";"); + } +} + +decl_arg *locate_decl_union_member(decl_union *u, decl_var *var) { + if (u->args) { + return locate_decl_var_arg(var, u->args, NULL); + } + + return NULL; +} + +int validate_decl_union(struct psi_data *data, decl_union *u) { + size_t i, pos, len, size = 0, align; + + if (!u->size && !u->args->count) { + data->error(data, u->token, PSI_WARNING, + "Cannot compute size of empty union %s", + u->name); + return 0; + } + + for (i = 0; i < u->args->count; ++i) { + decl_arg *darg = u->args->args[i]; + + if (!validate_decl_arg(data, darg)) { + return 0; + } + + assert(!darg->var->arg || darg->var->arg == darg); + + darg->var->arg = darg; + + if (!validate_decl_arg_args(data, darg, u)) { + return 0; + } else if (darg->layout) { + pos = darg->layout->pos; + + align = align_decl_arg(darg, &pos, &len); + + if (darg->layout->pos != 0) { + data->error(data, darg->token, PSI_WARNING, + "Offset of %s.%s should be 0", + u->name, darg->var->name); + darg->layout->pos = 0; + } + if (darg->layout->len != len) { + data->error(data, darg->token, PSI_WARNING, + "Computed size %zu of %s.%s does not match" + " pre-defined size %zu of type '%s'", + len, u->name, darg->var->name, darg->layout->len, + darg->type->name); + } + } else { + pos = 0; + + align = align_decl_arg(darg, &pos, &len); + darg->layout = init_decl_struct_layout(pos, len); + + } + if (len > size) { + size = len; + } + if (align > u->align) { + u->align = align; + } + } + + sort_decl_args(u->args); + + if (u->size < size) { + u->size = psi_align(size, u->align); + } + + return 1; +} + +size_t alignof_decl_union(decl_union *u) { + if (!u->align) { + u->align = alignof_decl_args(u->args); + } + return u->align; +} diff --git a/src/types/decl_union.h b/src/types/decl_union.h index 3bd031b..b914495 100644 --- a/src/types/decl_union.h +++ b/src/types/decl_union.h @@ -1,4 +1,31 @@ -#ifndef _PSI_TYPES_DECL_UNION_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef PSI_TYPES_DECL_UNION_H +#define PSI_TYPES_DECL_UNION_H + +#include "decl_args.h" typedef struct decl_union { struct psi_token *token; @@ -8,22 +35,16 @@ typedef struct decl_union { size_t align; } decl_union; -static inline decl_union *init_decl_union(const char *name, decl_args *args) { - decl_union *u = calloc(1, sizeof(*u)); - u->name = strdup(name); - u->args = args; - return u; -} - -static inline void free_decl_union(decl_union *u) { - if (u->token) { - free(u->token); - } - if (u->args) { - free_decl_args(u->args); - } - free(u->name); - free(u); -} +decl_union *init_decl_union(const char *name, decl_args *args); +void free_decl_union(decl_union *u); +void dump_decl_union(int fd, decl_union *unn); + +decl_arg *locate_decl_union_member(decl_union *u, decl_var *var); + +struct psi_data; + +int validate_decl_union(struct psi_data *data, decl_union *u); + +size_t alignof_decl_union(decl_union *u); #endif diff --git a/src/types/decl_unions.c b/src/types/decl_unions.c new file mode 100644 index 0000000..1722f3b --- /dev/null +++ b/src/types/decl_unions.c @@ -0,0 +1,66 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include + +decl_unions *add_decl_union(decl_unions *uu, decl_union *u) { + if (!uu) { + uu = calloc(1, sizeof(*uu)); + } + uu->list = realloc(uu->list, ++uu->count * sizeof(*uu->list)); + uu->list[uu->count - 1] = u; + return uu; +} + +void free_decl_unions(decl_unions *uu) { + size_t i; + for (i = 0; i < uu->count; ++i) { + free_decl_union(uu->list[i]); + } + free(uu->list); + free(uu); +} + +void dump_decl_unions(int fd, decl_unions *unions) { + size_t i; + + for (i = 0; i < unions->count; ++i) { + decl_union *unn = unions->list[i]; + + if (!is_anon_type(unn->name, "union")) { + dump_decl_union(fd, unn); + dprintf(fd, "\n"); + } + } +} diff --git a/src/types/decl_unions.h b/src/types/decl_unions.h index 49c6b73..13c254e 100644 --- a/src/types/decl_unions.h +++ b/src/types/decl_unions.h @@ -1,28 +1,40 @@ -#ifndef _PSI_TYPES_DECL_UNOINS_H -#define _PSI_TYPES_DECL_UNOINS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_UNOINS_H +#define PSI_TYPES_DECL_UNOINS_H + +#include "decl_union.h" typedef struct decl_unions { decl_union **list; size_t count; } decl_unions; -static inline decl_unions *add_decl_union(decl_unions *uu, decl_union *u) { - if (!uu) { - uu = calloc(1, sizeof(*uu)); - } - uu->list = realloc(uu->list, ++uu->count * sizeof(*uu->list)); - uu->list[uu->count-1] = u; - return uu; -} - -static inline void free_decl_unions(decl_unions *uu) { - size_t i; - - for (i = 0; i < uu->count; ++i) { - free_decl_union(uu->list[i]); - } - free(uu->list); - free(uu); -} +decl_unions *add_decl_union(decl_unions *uu, decl_union *u); +void free_decl_unions(decl_unions *uu); +void dump_decl_unions(int fd, decl_unions *unions); #endif diff --git a/src/types/decl_var.c b/src/types/decl_var.c new file mode 100644 index 0000000..fa495bf --- /dev/null +++ b/src/types/decl_var.c @@ -0,0 +1,86 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include + +#include "data.h" + +decl_var *init_decl_var(const char *name, unsigned pl, unsigned as) { + decl_var *v = calloc(1, sizeof(*v)); + v->name = (char *) strdup((const char *) name); + v->pointer_level = pl; + v->array_size = as; + return v; +} + +decl_var *copy_decl_var(decl_var *src) { + decl_var *dest = calloc(1, sizeof(*dest)); + memcpy(dest, src, sizeof(*dest)); + dest->name = strdup(dest->name); + if (dest->token) { + dest->token = psi_token_copy(dest->token); + } + return dest; +} + +void free_decl_var(decl_var *var) { + if (var->token) { + free(var->token); + } + free(var->name); + free(var); +} + +void dump_decl_var(int fd, decl_var *var) { + dprintf(fd, "%s%s", + psi_t_indirection(var->pointer_level-!!var->array_size), var->name); + if (var->array_size) { + dprintf(fd, "[%u]", var->array_size); + } +} + +decl_arg *locate_decl_var_arg(decl_var *var, decl_args *args, decl_arg *func) { + decl_arg *arg = locate_decl_arg(args, var->name); + + if (arg) { + assert(!var->arg || var->arg == arg); + + return var->arg = arg; + } + + if (func && !strcmp(var->name, func->var->name)) { + return var->arg = func; + } + + return NULL; +} diff --git a/src/types/decl_var.h b/src/types/decl_var.h index 166a69e..8fd75db 100644 --- a/src/types/decl_var.h +++ b/src/types/decl_var.h @@ -1,5 +1,30 @@ -#ifndef _PSI_TYPES_DECL_VAR_H -#define _PSI_TYPES_DECL_VAR_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_VAR_H +#define PSI_TYPES_DECL_VAR_H typedef struct decl_var { struct psi_token *token; @@ -9,31 +34,14 @@ typedef struct decl_var { struct decl_arg *arg; } decl_var; -static inline decl_var *init_decl_var(const char *name, unsigned pl, unsigned as) { - decl_var *v = calloc(1, sizeof(*v)); - v->name = (char *) strdup((const char *) name); - v->pointer_level = pl; - v->array_size = as; - return v; -} - -static inline decl_var *copy_decl_var(decl_var *src) { - decl_var *dest = calloc(1, sizeof(*dest)); - - memcpy(dest, src, sizeof(*dest)); - dest->name = strdup(dest->name); - if (dest->token) { - dest->token = psi_token_copy(dest->token); - } - return dest; -} - -static inline void free_decl_var(decl_var *var) { - if (var->token) { - free(var->token); - } - free(var->name); - free(var); -} +decl_var *init_decl_var(const char *name, unsigned pl, unsigned as); +decl_var *copy_decl_var(decl_var *src); +void free_decl_var(decl_var *var); +void dump_decl_var(int fd, decl_var *var); + +struct decl_arg; +struct decl_args; + +struct decl_arg *locate_decl_var_arg(decl_var *var, struct decl_args *args, struct decl_arg *func); #endif diff --git a/src/types/decl_vars.c b/src/types/decl_vars.c new file mode 100644 index 0000000..38b6e5d --- /dev/null +++ b/src/types/decl_vars.c @@ -0,0 +1,58 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include + +#include "decl_vars.h" + +decl_vars *init_decl_vars(decl_var *var) { + decl_vars *vars = calloc(1, sizeof(*vars)); + if (var) { + vars->count = 1; + vars->vars = calloc(1, sizeof(*vars->vars)); + vars->vars[0] = var; + } + return vars; +} + +decl_vars *add_decl_var(decl_vars *vars, decl_var *var) { + vars->vars = realloc(vars->vars, ++vars->count * sizeof(*vars->vars)); + vars->vars[vars->count - 1] = var; + return vars; +} + +void free_decl_vars(decl_vars *vars) { + size_t i; + for (i = 0; i < vars->count; ++i) { + free_decl_var(vars->vars[i]); + } + free(vars->vars); + free(vars); +} diff --git a/src/types/decl_vars.h b/src/types/decl_vars.h index 46c65bd..22b03e8 100644 --- a/src/types/decl_vars.h +++ b/src/types/decl_vars.h @@ -1,35 +1,40 @@ -#ifndef _PSI_TYPES_DECL_VARS_H -#define _PSI_TYPES_DECL_VARS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECL_VARS_H +#define PSI_TYPES_DECL_VARS_H + +#include "decl_var.h" typedef struct decl_vars { decl_var **vars; size_t count; } decl_vars; -static inline decl_vars *init_decl_vars(decl_var *var) { - decl_vars *vars = calloc(1, sizeof(*vars)); - if (var) { - vars->count = 1; - vars->vars = calloc(1, sizeof(*vars->vars)); - vars->vars[0] = var; - } - return vars; -} - -static inline decl_vars *add_decl_var(decl_vars *vars, decl_var *var) { - vars->vars = realloc(vars->vars, ++vars->count * sizeof(*vars->vars)); - vars->vars[vars->count-1] = var; - return vars; -} - -static inline void free_decl_vars(decl_vars *vars) { - size_t i; - - for (i = 0; i < vars->count; ++i) { - free_decl_var(vars->vars[i]); - } - free(vars->vars); - free(vars); -} +decl_vars *init_decl_vars(decl_var *var); +decl_vars *add_decl_var(decl_vars *vars, decl_var *var); +void free_decl_vars(decl_vars *vars); #endif diff --git a/src/types/decls.c b/src/types/decls.c new file mode 100644 index 0000000..2dff0a2 --- /dev/null +++ b/src/types/decls.c @@ -0,0 +1,64 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "decls.h" + +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; +} + +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); +} + +void dump_decls(int fd, decls *decls) { + size_t i; + + for (i = 0; i < decls->count; ++i) { + decl *decl = decls->list[i]; + + dump_decl(fd, decl); + dprintf(fd, "\n"); + } +} diff --git a/src/types/decls.h b/src/types/decls.h index 7f40b7c..9ccae74 100644 --- a/src/types/decls.h +++ b/src/types/decls.h @@ -1,28 +1,40 @@ -#ifndef _PSI_TYPES_DECLS_H -#define _PSI_TYPES_DECLS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_DECLS_H +#define PSI_TYPES_DECLS_H + +#include "decl.h" 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); -} +decls *add_decl(decls *decls, decl *decl); +void free_decls(decls *decls); +void dump_decls(int fd, decls *decls); #endif diff --git a/src/types/free_call.c b/src/types/free_call.c new file mode 100644 index 0000000..80f3130 --- /dev/null +++ b/src/types/free_call.c @@ -0,0 +1,64 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include + +#include "free_call.h" + +free_call *init_free_call(const char *func, decl_vars *vars) { + free_call *f = calloc(1, sizeof(*f)); + f->func = strdup(func); + f->vars = vars; + return f; +} + +void free_free_call(free_call *f) { + if (f->token) { + free(f->token); + } + free(f->func); + free_decl_vars(f->vars); + free(f); +} + +void dump_free_call(int fd, free_call *call) { + size_t l; + + dprintf(fd, "%s(", call->func); + for (l = 0; l < call->vars->count; ++l) { + decl_var *fvar = call->vars->vars[l]; + + dump_decl_var(fd, fvar); + } + dprintf(fd, ");"); +} diff --git a/src/types/free_call.h b/src/types/free_call.h index fec0daa..86f2ba7 100644 --- a/src/types/free_call.h +++ b/src/types/free_call.h @@ -1,5 +1,33 @@ -#ifndef _PSI_TYPES_FREE_CALL_H -#define _PSI_TYPES_FREE_CALL_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_FREE_CALL_H +#define PSI_TYPES_FREE_CALL_H + +#include "decl_vars.h" +#include "decl.h" typedef struct free_call { struct psi_token *token; @@ -8,20 +36,8 @@ typedef struct free_call { decl *decl; } free_call; -static inline free_call *init_free_call(const char *func, decl_vars *vars) { - free_call *f = calloc(1, sizeof(*f)); - f->func = strdup(func); - f->vars = vars; - return f; -} - -static inline void free_free_call(free_call *f) { - if (f->token) { - free(f->token); - } - free(f->func); - free_decl_vars(f->vars); - free(f); -} +free_call *init_free_call(const char *func, decl_vars *vars); +void free_free_call(free_call *f); +void dump_free_call(int fd, free_call *call); #endif diff --git a/src/types/free_calls.c b/src/types/free_calls.c new file mode 100644 index 0000000..19739df --- /dev/null +++ b/src/types/free_calls.c @@ -0,0 +1,60 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "free_calls.h" + +free_calls *init_free_calls(free_call *f) { + free_calls *fcs = calloc(1, sizeof(*fcs)); + if (f) { + fcs->count = 1; + fcs->list = calloc(1, sizeof(*fcs->list)); + fcs->list[0] = f; + } + return fcs; +} + +void free_free_calls(free_calls *fcs) { + size_t i; + for (i = 0; i < fcs->count; ++i) { + free_free_call(fcs->list[i]); + } + free(fcs->list); + free(fcs); +} + +free_calls *add_free_call(free_calls *fcs, free_call *f) { + fcs->list = realloc(fcs->list, ++fcs->count * sizeof(*fcs->list)); + fcs->list[fcs->count - 1] = f; + return fcs; +} diff --git a/src/types/free_calls.h b/src/types/free_calls.h index 7d4f575..9e156ed 100644 --- a/src/types/free_calls.h +++ b/src/types/free_calls.h @@ -1,35 +1,40 @@ -#ifndef _PSI_TYPES_FREE_CALLS_H -#define _PSI_TYPES_FREE_CALLS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_FREE_CALLS_H +#define PSI_TYPES_FREE_CALLS_H + +#include "free_call.h" typedef struct free_calls { free_call **list; size_t count; } free_calls; -static inline free_calls *init_free_calls(free_call *f) { - free_calls *fcs = calloc(1, sizeof(*fcs)); - if (f) { - fcs->count = 1; - fcs->list = calloc(1, sizeof(*fcs->list)); - fcs->list[0] = f; - } - return fcs; -} - -static inline void free_free_calls(free_calls *fcs) { - size_t i; - - for (i = 0; i < fcs->count; ++i) { - free_free_call(fcs->list[i]); - } - free(fcs->list); - free(fcs); -} - -static inline free_calls *add_free_call(free_calls *fcs, free_call *f) { - fcs->list = realloc(fcs->list, ++fcs->count * sizeof(*fcs->list)); - fcs->list[fcs->count-1] = f; - return fcs; -} +free_calls *init_free_calls(free_call *f); +void free_free_calls(free_calls *fcs); +free_calls *add_free_call(free_calls *fcs, free_call *f); #endif diff --git a/src/types/free_stmt.c b/src/types/free_stmt.c new file mode 100644 index 0000000..a19c97f --- /dev/null +++ b/src/types/free_stmt.c @@ -0,0 +1,129 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "data.h" + +free_stmt *init_free_stmt(free_calls *calls) { + free_stmt *f = calloc(1, sizeof(*f)); + f->calls = calls; + return f; +} + +void free_free_stmt(free_stmt *f) { + free_free_calls(f->calls); + free(f); +} + +void dump_free_stmt(int fd, free_stmt *fre) { + size_t k; + + dprintf(fd, "\tfree "); + for (k = 0; k < fre->calls->count; ++k) { + free_call *call = fre->calls->list[k]; + + if (k) { + dprintf(fd, ", "); + } + dump_free_call(fd, call); + dprintf(fd, "\n"); + } +} + +static inline decl *locate_free_decl(decls *decls, free_call *f) { + if (decls) { + size_t i; + + for (i = 0; i < decls->count; ++i) { + if (!strcmp(decls->list[i]->func->var->name, f->func)) { + f->decl = decls->list[i]; + return decls->list[i]; + } + } + } + + return NULL; +} + +int validate_free_stmts(struct psi_data *data, impl *impl) { + size_t i, j, k, l; + /* we can have any count of free stmts; freeing any out vars */ + for (i = 0; i < impl->stmts->fre.count; ++i) { + free_stmt *fre = impl->stmts->fre.list[i]; + + for (j = 0; j < fre->calls->count; ++j) { + free_call *free_call = fre->calls->list[j]; + + /* first find the decl of the free func */ + if (!locate_free_decl(data->decls, free_call)) { + data->error(data, free_call->token, PSI_WARNING, + "Missing declaration '%s' in `free` statement" + " of implementation '%s'", + free_call->func, impl->func->name); + return 0; + } + + + + /* now check for known vars */ + for (l = 0; l < free_call->vars->count; ++l) { + int check = 0; + decl_var *free_var = free_call->vars->vars[l]; + + if (!strcmp(free_var->name, impl->decl->func->var->name)) { + check = 1; + free_var->arg = impl->decl->func; + } else if (impl->decl->args) { + for (k = 0; k < impl->decl->args->count; ++k) { + decl_arg *free_arg = impl->decl->args->args[k]; + + if (!strcmp(free_var->name, free_arg->var->name)) { + check = 1; + free_var->arg = free_arg; + break; + } + } + } + + if (!check) { + data->error(data, free_var->token, PSI_WARNING, + "Unknown variable '%s' of `free` statement" + " of implementation '%s'", + free_var->name, impl->func->name); + return 0; + } + } + } + } + return 1; +} diff --git a/src/types/free_stmt.h b/src/types/free_stmt.h index a5eceb8..131f534 100644 --- a/src/types/free_stmt.h +++ b/src/types/free_stmt.h @@ -1,19 +1,44 @@ -#ifndef _PSI_TYPES_FREE_STMT_H -#define _PSI_TYPES_FREE_STMT_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_FREE_STMT_H +#define PSI_TYPES_FREE_STMT_H + +#include "free_calls.h" typedef struct free_stmt { free_calls *calls; } free_stmt; -static inline free_stmt *init_free_stmt(free_calls *calls) { - free_stmt *f = calloc(1, sizeof(*f)); - f->calls = calls; - return f; -} +free_stmt *init_free_stmt(free_calls *calls); +void free_free_stmt(free_stmt *f); +void dump_free_stmt(int fd, free_stmt *fre); + +struct psi_data; +struct impl; -static inline void free_free_stmt(free_stmt *f) { - free_free_calls(f->calls); - free(f); -} +int validate_free_stmts(struct psi_data *data, struct impl *impl); #endif diff --git a/src/types/impl.c b/src/types/impl.c new file mode 100644 index 0000000..e28a7d2 --- /dev/null +++ b/src/types/impl.c @@ -0,0 +1,64 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "data.h" + +impl *init_impl(impl_func *func, impl_stmts *stmts) { + impl *i = calloc(1, sizeof(*i)); + i->func = func; + i->stmts = stmts; + return i; +} + +void free_impl(impl *impl) { + free_impl_func(impl->func); + free_impl_stmts(impl->stmts); + free(impl); +} + +void dump_impl(int fd, impl *impl) { + dump_impl_func(fd, impl->func); + dprintf(fd, " {\n"); + if (impl->stmts) { + dump_impl_stmts(fd, impl->stmts); + } + dprintf(fd, "}"); +} + +int validate_impl(struct psi_data *data, impl *impl) { + if (!validate_impl_args(data, impl)) { + return 0; + } + return validate_impl_stmts(data, impl); +} diff --git a/src/types/impl.h b/src/types/impl.h index 11270fa..5b38020 100644 --- a/src/types/impl.h +++ b/src/types/impl.h @@ -1,5 +1,34 @@ -#ifndef _PSI_TYPES_IMPL_H -#define _PSI_TYPES_IMPL_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_IMPL_H +#define PSI_TYPES_IMPL_H + +#include "impl_func.h" +#include "impl_stmts.h" +#include "decl.h" typedef struct impl { impl_func *func; @@ -7,17 +36,12 @@ typedef struct impl { decl *decl; } impl; -static inline impl *init_impl(impl_func *func, impl_stmts *stmts) { - impl *i = calloc(1, sizeof(*i)); - i->func = func; - i->stmts = stmts; - return i; -} - -static inline void free_impl(impl *impl) { - free_impl_func(impl->func); - free_impl_stmts(impl->stmts); - free(impl); -} +impl *init_impl(impl_func *func, impl_stmts *stmts); +void free_impl(impl *impl); +void dump_impl(int fd, impl *impl); + +struct psi_data; + +int validate_impl(struct psi_data *data, impl *impl); #endif diff --git a/src/types/impl_arg.c b/src/types/impl_arg.c new file mode 100644 index 0000000..8c0f82d --- /dev/null +++ b/src/types/impl_arg.c @@ -0,0 +1,53 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "impl_arg.h" + +impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) { + impl_arg *arg = calloc(1, sizeof(*arg)); + arg->type = type; + arg->var = var; + arg->var->arg = arg; + arg->def = def; + return arg; +} + +void free_impl_arg(impl_arg *arg) { + free_impl_type(arg->type); + free_impl_var(arg->var); + if (arg->def) { + free_impl_def_val(arg->def); + } + free(arg); +} diff --git a/src/types/impl_arg.h b/src/types/impl_arg.h index 4f870e0..4bf5129 100644 --- a/src/types/impl_arg.h +++ b/src/types/impl_arg.h @@ -1,5 +1,39 @@ -#ifndef _PSI_TYPES_IMPL_ARG_H -#define _PSI_TYPES_IMPL_ARG_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_IMPL_ARG_H +#define PSI_TYPES_IMPL_ARG_H + +#include "token.h" + +#include "impl_type.h" +#include "impl_var.h" +#include "impl_def_val.h" +#include "impl_val.h" + +#include "Zend/zend_types.h" typedef struct impl_arg { impl_type *type; @@ -17,22 +51,7 @@ typedef struct impl_vararg { void **free_list; } impl_vararg; -static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) { - impl_arg *arg = calloc(1, sizeof(*arg)); - arg->type = type; - arg->var = var; - arg->var->arg = arg; - arg->def = def; - return arg; -} - -static inline void free_impl_arg(impl_arg *arg) { - free_impl_type(arg->type); - free_impl_var(arg->var); - if (arg->def) { - free_impl_def_val(arg->def); - } - free(arg); -} +impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def); +void free_impl_arg(impl_arg *arg); #endif diff --git a/src/types/impl_args.c b/src/types/impl_args.c new file mode 100644 index 0000000..74e1d31 --- /dev/null +++ b/src/types/impl_args.c @@ -0,0 +1,84 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "data.h" + +impl_args *init_impl_args(impl_arg *arg) { + impl_args *args = calloc(1, sizeof(*args)); + if (arg) { + args->count = 1; + args->args = calloc(1, sizeof(*args->args)); + args->args[0] = arg; + } + return args; +} + +impl_args *add_impl_arg(impl_args *args, impl_arg *arg) { + args->args = realloc(args->args, ++args->count * sizeof(*args->args)); + args->args[args->count - 1] = arg; + return args; +} + +void free_impl_args(impl_args *args) { + size_t i; + for (i = 0; i < args->count; ++i) { + free_impl_arg(args->args[i]); + } + if (args->vararg.name) { + free_impl_arg(args->vararg.name); + } + free(args->args); + free(args); +} + +int validate_impl_args(struct psi_data *data, impl *impl) { + int def = 0; + size_t i; + + for (i = 0; i < impl->func->args->count; ++i) { + impl_arg *iarg = impl->func->args->args[i]; + + if (iarg->def) { + def = 1; + } else if (def) { + data->error(data, impl->func->token, PSI_WARNING, + "Non-optional argument %zu '$%s' of implementation '%s'" + " follows optional argument", + i+1, iarg->var->name, impl->func->name); + return 0; + } + } + + return 1; +} diff --git a/src/types/impl_args.h b/src/types/impl_args.h index 3894405..191670d 100644 --- a/src/types/impl_args.h +++ b/src/types/impl_args.h @@ -1,5 +1,32 @@ -#ifndef _PSI_TYPES_IMPL_ARGS_H -#define _PSI_TYPES_IMPL_ARGS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_IMPL_ARGS_H +#define PSI_TYPES_IMPL_ARGS_H + +#include "impl_arg.h" typedef struct impl_args { impl_arg **args; @@ -7,33 +34,13 @@ typedef struct impl_args { impl_vararg vararg; } impl_args; -static inline impl_args *init_impl_args(impl_arg *arg) { - impl_args *args = calloc(1, sizeof(*args)); - if (arg) { - args->count = 1; - args->args = calloc(1, sizeof(*args->args)); - args->args[0] = arg; - } - return args; -} - -static inline impl_args *add_impl_arg(impl_args *args, impl_arg *arg) { - args->args = realloc(args->args, ++args->count * sizeof(*args->args)); - args->args[args->count-1] = arg; - return args; -} - -static inline void free_impl_args(impl_args *args) { - size_t i; - - for (i = 0; i < args->count; ++i) { - free_impl_arg(args->args[i]); - } - if (args->vararg.name) { - free_impl_arg(args->vararg.name); - } - free(args->args); - free(args); -} +impl_args *init_impl_args(impl_arg *arg); +impl_args *add_impl_arg(impl_args *args, impl_arg *arg); +void free_impl_args(impl_args *args); + +struct psi_data; +struct impl; + +int validate_impl_args(struct psi_data *data, struct impl *impl); #endif diff --git a/src/types/impl_def_val.c b/src/types/impl_def_val.c new file mode 100644 index 0000000..0d176f3 --- /dev/null +++ b/src/types/impl_def_val.c @@ -0,0 +1,56 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "impl_def_val.h" + +void free_impl_def_val(impl_def_val *def) { + free(def->text); + free(def); +} + +impl_def_val *init_impl_def_val(token_t t, const char *text) { + impl_def_val *def = calloc(1, sizeof(*def)); + def->type = t; + def->text = strdup(text); + return def; +} + +void dump_impl_def_val(int fd, impl_def_val *val) { + if (val->type == PSI_T_QUOTED_STRING) { + dprintf(fd, "\"%s\"", val->text); + } else { + dprintf(fd, "%s", val->text); + } + +} diff --git a/src/types/impl_def_val.h b/src/types/impl_def_val.h index 189e349..07c02cd 100644 --- a/src/types/impl_def_val.h +++ b/src/types/impl_def_val.h @@ -1,21 +1,40 @@ -#ifndef _PSI_TYPES_IMPL_DEF_VAL_H -#define _PSI_TYPES_IMPL_DEF_VAL_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_IMPL_DEF_VAL_H +#define PSI_TYPES_IMPL_DEF_VAL_H + +#include "token.h" typedef struct impl_def_val { token_t type; char *text; } impl_def_val; -static inline impl_def_val *init_impl_def_val(token_t t, const char *text) { - impl_def_val *def = calloc(1, sizeof(*def)); - def->type = t; - def->text = strdup(text); - return def; -} - -static inline void free_impl_def_val(impl_def_val *def) { - free(def->text); - free(def); -} +impl_def_val *init_impl_def_val(token_t t, const char *text); +void free_impl_def_val(impl_def_val *def); +void dump_impl_def_val(int fd, impl_def_val *val); #endif diff --git a/src/types/impl_func.c b/src/types/impl_func.c new file mode 100644 index 0000000..690ce4a --- /dev/null +++ b/src/types/impl_func.c @@ -0,0 +1,87 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include + +#include "impl_func.h" + +impl_func *init_impl_func(char *name, impl_args *args, impl_type *type, + int ret_reference) { + impl_func *func = calloc(1, sizeof(*func)); + func->name = strdup(name); + func->args = args ? args : init_impl_args(NULL); + func->return_type = type; + func->return_reference = ret_reference; + return func; +} + +void free_impl_func(impl_func *f) { + if (f->token) { + free(f->token); + } + free_impl_type(f->return_type); + free_impl_args(f->args); + free(f->name); + free(f); +} + +void dump_impl_func(int fd, impl_func *func) { + size_t j; + + dprintf(fd, "function %s(", func->name); + if (func->args) { + for (j = 0; j < func->args->count; ++j) { + impl_arg *iarg = func->args->args[j]; + + dprintf(fd, "%s%s %s%s", + j ? ", " : "", + iarg->type->name, + iarg->var->reference ? "&" : "", + iarg->var->name); + if (iarg->def) { + dprintf(fd, " = %s", iarg->def->text); + } + } + if (func->args->vararg.name) { + impl_arg *vararg = func->args->vararg.name; + + dprintf(fd, ", %s %s...%s", + vararg->type->name, + vararg->var->reference ? "&" : "", + vararg->var->name); + } + } + dprintf(fd, ") : %s%s", + func->return_reference ? "&":"", + func->return_type->name); +} diff --git a/src/types/impl_func.h b/src/types/impl_func.h index a9d6270..740a7aa 100644 --- a/src/types/impl_func.h +++ b/src/types/impl_func.h @@ -1,5 +1,33 @@ -#ifndef _PSI_TYPES_IMPL_FUNC_H -#define _PSI_TYPES_IMPL_FUNC_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_IMPL_FUNC_H +#define PSI_TYPES_IMPL_FUNC_H + +#include "impl_args.h" +#include "impl_type.h" typedef struct impl_func { struct psi_token *token; @@ -9,23 +37,9 @@ typedef struct impl_func { unsigned return_reference:1; } impl_func; -static inline impl_func *init_impl_func(char *name, impl_args *args, impl_type *type, int ret_reference) { - impl_func *func = calloc(1, sizeof(*func)); - func->name = strdup(name); - func->args = args ? args : init_impl_args(NULL); - func->return_type = type; - func->return_reference = ret_reference; - return func; -} - -static inline void free_impl_func(impl_func *f) { - if (f->token) { - free(f->token); - } - free_impl_type(f->return_type); - free_impl_args(f->args); - free(f->name); - free(f); -} +impl_func *init_impl_func(char *name, impl_args *args, impl_type *type, + int ret_reference); +void free_impl_func(impl_func *f); +void dump_impl_func(int fd, impl_func *func); #endif diff --git a/src/types/impl_stmt.c b/src/types/impl_stmt.c new file mode 100644 index 0000000..35a95f3 --- /dev/null +++ b/src/types/impl_stmt.c @@ -0,0 +1,60 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "impl_stmt.h" + +impl_stmt *init_impl_stmt(token_t type, void *ptr) { + impl_stmt *stmt = calloc(1, sizeof(*stmt)); + stmt->type = type; + stmt->s.ptr = ptr; + return stmt; +} + +void free_impl_stmt(impl_stmt *stmt) { + switch (stmt->type) { + case PSI_T_LET: + free_let_stmt(stmt->s.let); + break; + case PSI_T_SET: + free_set_stmt(stmt->s.set); + break; + case PSI_T_RETURN: + free_return_stmt(stmt->s.ret); + break; + case PSI_T_FREE: + free_free_stmt(stmt->s.fre); + break; + } + free(stmt); +} diff --git a/src/types/impl_stmt.h b/src/types/impl_stmt.h index c110ddd..0a916ed 100644 --- a/src/types/impl_stmt.h +++ b/src/types/impl_stmt.h @@ -1,5 +1,37 @@ -#ifndef _PSI_TYPES_IMPL_STMT_H -#define _PSI_TYPES_IMPL_STMT_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_IMPL_STMT_H +#define PSI_TYPES_IMPL_STMT_H + +#include "token.h" + +#include "let_stmt.h" +#include "set_stmt.h" +#include "return_stmt.h" +#include "free_stmt.h" typedef struct impl_stmt { token_t type; @@ -12,29 +44,7 @@ typedef struct impl_stmt { } s; } impl_stmt; -static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) { - impl_stmt *stmt = calloc(1, sizeof(*stmt)); - stmt->type = type; - stmt->s.ptr = ptr; - return stmt; -} - -static inline void free_impl_stmt(impl_stmt *stmt) { - switch (stmt->type) { - case PSI_T_LET: - free_let_stmt(stmt->s.let); - break; - case PSI_T_SET: - free_set_stmt(stmt->s.set); - break; - case PSI_T_RETURN: - free_return_stmt(stmt->s.ret); - break; - case PSI_T_FREE: - free_free_stmt(stmt->s.fre); - break; - } - free(stmt); -} +impl_stmt *init_impl_stmt(token_t type, void *ptr); +void free_impl_stmt(impl_stmt *stmt); #endif diff --git a/src/types/impl_stmts.c b/src/types/impl_stmts.c new file mode 100644 index 0000000..efc7cea --- /dev/null +++ b/src/types/impl_stmts.c @@ -0,0 +1,142 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "data.h" + +void *add_impl_stmt_ex(void *list, size_t count, void *stmt) { + list = realloc(list, count * sizeof(list)); + ((void**) list)[count - 1] = stmt; + return list; +} + +impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) { + switch (stmt->type) { + case PSI_T_RETURN: + stmts->ret.list = add_impl_stmt_ex(stmts->ret.list, ++stmts->ret.count, + stmt->s.ret); + break; + case PSI_T_LET: + stmts->let.list = add_impl_stmt_ex(stmts->let.list, ++stmts->let.count, + stmt->s.let); + break; + case PSI_T_SET: + stmts->set.list = add_impl_stmt_ex(stmts->set.list, ++stmts->set.count, + stmt->s.set); + break; + case PSI_T_FREE: + stmts->fre.list = add_impl_stmt_ex(stmts->fre.list, ++stmts->fre.count, + stmt->s.fre); + break; + } + free(stmt); + return stmts; +} + +impl_stmts *init_impl_stmts(impl_stmt *stmt) { + impl_stmts *stmts = calloc(1, sizeof(*stmts)); + return add_impl_stmt(stmts, stmt); +} + +void free_impl_stmts(impl_stmts *stmts) { + size_t i; + for (i = 0; i < stmts->let.count; ++i) { + free_let_stmt(stmts->let.list[i]); + } + free(stmts->let.list); + for (i = 0; i < stmts->ret.count; ++i) { + free_return_stmt(stmts->ret.list[i]); + } + free(stmts->ret.list); + for (i = 0; i < stmts->set.count; ++i) { + free_set_stmt(stmts->set.list[i]); + } + free(stmts->set.list); + for (i = 0; i < stmts->fre.count; ++i) { + free_free_stmt(stmts->fre.list[i]); + } + free(stmts->fre.list); + free(stmts); +} + +void dump_impl_stmts(int fd, impl_stmts *stmts) { + size_t j; + + for (j = 0; j < stmts->let.count; ++j) { + let_stmt *let = stmts->let.list[j]; + dump_let_stmt(fd, let); + dprintf(fd, "\n"); + } + for (j = 0; j < stmts->ret.count; ++j) { + return_stmt *ret = stmts->ret.list[j]; + dump_return_stmt(fd, ret); + dprintf(fd, "\n"); + } + for (j = 0; j < stmts->set.count; ++j) { + set_stmt *set = stmts->set.list[j]; + + dump_set_stmt(fd, set); + dprintf(fd, "\n"); + } + for (j = 0; j < stmts->fre.count; ++j) { + free_stmt *fre = stmts->fre.list[j]; + + dump_free_stmt(fd, fre); + dprintf(fd, "\n"); + } +} + +int validate_impl_stmts(struct psi_data *data, impl *impl) { + if (!impl->stmts) { + data->error(data, impl->func->token, PSI_WARNING, + "Missing body for implementation %s!", + impl->func->name); + return 0; + } + + if (!validate_return_stmt(data, impl)) { + return 0; + } + + if (!validate_let_stmts(data, impl)) { + return 0; + } + if (!validate_set_stmts(data, impl)) { + return 0; + } + if (!validate_free_stmts(data, impl)) { + return 0; + } + + return 1; +} diff --git a/src/types/impl_stmts.h b/src/types/impl_stmts.h index f7b980d..a346279 100644 --- a/src/types/impl_stmts.h +++ b/src/types/impl_stmts.h @@ -1,5 +1,32 @@ -#ifndef _PSI_TYPES_IMPL_STMTS_H -#define _PSI_TYPES_IMPL_STMTS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_IMPL_STMTS_H +#define PSI_TYPES_IMPL_STMTS_H + +#include "impl_stmt.h" typedef struct impl_stmts { struct { @@ -20,56 +47,15 @@ typedef struct impl_stmts { } fre; } impl_stmts; -static inline void *add_impl_stmt_ex(void *list, size_t count, void *stmt) { - list = realloc(list, count * sizeof(list)); - ((void **)list)[count-1] = stmt; - return list; -} - -static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) { - switch (stmt->type) { - case PSI_T_RETURN: - stmts->ret.list = add_impl_stmt_ex(stmts->ret.list, ++stmts->ret.count, stmt->s.ret); - break; - case PSI_T_LET: - stmts->let.list = add_impl_stmt_ex(stmts->let.list, ++stmts->let.count, stmt->s.let); - break; - case PSI_T_SET: - stmts->set.list = add_impl_stmt_ex(stmts->set.list, ++stmts->set.count, stmt->s.set); - break; - case PSI_T_FREE: - stmts->fre.list = add_impl_stmt_ex(stmts->fre.list, ++stmts->fre.count, stmt->s.fre); - break; - } - free(stmt); - return stmts; -} - -static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) { - impl_stmts *stmts = calloc(1, sizeof(*stmts)); - return add_impl_stmt(stmts, stmt); -} +void *add_impl_stmt_ex(void *list, size_t count, void *stmt); +impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt); +impl_stmts *init_impl_stmts(impl_stmt *stmt); +void free_impl_stmts(impl_stmts *stmts); +void dump_impl_stmts(int fd, impl_stmts *stmts); -static inline void free_impl_stmts(impl_stmts *stmts) { - size_t i; +struct psi_data; +struct impl; - for (i = 0; i < stmts->let.count; ++i) { - free_let_stmt(stmts->let.list[i]); - } - free(stmts->let.list); - for (i = 0; i < stmts->ret.count; ++i) { - free_return_stmt(stmts->ret.list[i]); - } - free(stmts->ret.list); - for (i = 0; i < stmts->set.count; ++i) { - free_set_stmt(stmts->set.list[i]); - } - free(stmts->set.list); - for (i = 0; i < stmts->fre.count; ++i) { - free_free_stmt(stmts->fre.list[i]); - } - free(stmts->fre.list); - free(stmts); -} +int validate_impl_stmts(struct psi_data *data, struct impl *impl); #endif diff --git a/src/types/impl_type.c b/src/types/impl_type.c new file mode 100644 index 0000000..a5b2188 --- /dev/null +++ b/src/types/impl_type.c @@ -0,0 +1,48 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include + +#include "impl_type.h" + +impl_type *init_impl_type(token_t type, const char *name) { + impl_type *t = calloc(1, sizeof(*t)); + t->type = type; + t->name = strdup(name); + return t; +} + +void free_impl_type(impl_type *type) { + free(type->name); + free(type); +} diff --git a/src/types/impl_type.h b/src/types/impl_type.h index 9f7e894..e1197e7 100644 --- a/src/types/impl_type.h +++ b/src/types/impl_type.h @@ -1,22 +1,39 @@ -#ifndef _PSI_TYPES_IMPL_TYPE_H -#define _PSI_TYPES_IMPL_TYPE_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_IMPL_TYPE_H +#define PSI_TYPES_IMPL_TYPE_H + +#include "token.h" typedef struct impl_type { char *name; token_t type; } impl_type; -static inline impl_type *init_impl_type(token_t type, const char *name) { - impl_type *t = calloc(1, sizeof(*t)); - - t->type = type; - t->name = strdup(name); - return t; -} - -static inline void free_impl_type(impl_type *type) { - free(type->name); - free(type); -} +impl_type *init_impl_type(token_t type, const char *name); +void free_impl_type(impl_type *type); #endif diff --git a/src/types/impl_val.h b/src/types/impl_val.h index 4434690..1866320 100644 --- a/src/types/impl_val.h +++ b/src/types/impl_val.h @@ -1,7 +1,38 @@ -#ifndef _PSI_TYPES_IMPL_VAL_H -#define _PSI_TYPES_IMPL_VAL_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_IMPL_VAL_H +#define PSI_TYPES_IMPL_VAL_H #include "Zend/zend_types.h" +#include "Zend/zend_API.h" + +typedef struct zend_fcall { + zend_fcall_info fci; + zend_fcall_info_cache fcc; +} zend_fcall; typedef union impl_val { char cval; @@ -30,5 +61,4 @@ typedef union impl_val { void *ptr; } impl_val; - #endif diff --git a/src/types/impl_var.c b/src/types/impl_var.c new file mode 100644 index 0000000..487a997 --- /dev/null +++ b/src/types/impl_var.c @@ -0,0 +1,75 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include + +#include "data.h" + +impl_var *init_impl_var(const char *name, int is_reference) { + impl_var *var = calloc(1, sizeof(*var)); + var->name = strdup(name); + var->reference = is_reference; + return var; +} + +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_token_copy(cpy->token); + } + return cpy; +} + +void free_impl_var(impl_var *var) { + if (var->token) { + free(var->token); + } + free(var->name); + free(var); +} + +impl_arg *locate_impl_var_arg(impl_var *var, impl_args *args) { + size_t i; + + for (i = 0; i < args->count; ++i) { + impl_arg *iarg = args->args[i]; + + if (!strcmp(var->name, iarg->var->name)) { + return var->arg = iarg; + } + } + + return NULL; +} diff --git a/src/types/impl_var.h b/src/types/impl_var.h index 9b8b39b..e35a8b7 100644 --- a/src/types/impl_var.h +++ b/src/types/impl_var.h @@ -1,5 +1,30 @@ -#ifndef _PSI_TYPES_IMPL_VAR_H -#define _PSI_TYPES_IMPL_VAR_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_IMPL_VAR_H +#define PSI_TYPES_IMPL_VAR_H typedef struct impl_var { struct psi_token *token; @@ -8,30 +33,12 @@ typedef struct impl_var { unsigned reference:1; } impl_var; -static inline impl_var *init_impl_var(const char *name, int is_reference) { - impl_var *var = calloc(1, sizeof(*var)); - var->name = strdup(name); - var->reference = 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_token_copy(cpy->token); - } - return cpy; -} - -static inline void free_impl_var(impl_var *var) { - if (var->token) { - free(var->token); - } - free(var->name); - free(var); -} +impl_var *init_impl_var(const char *name, int is_reference); +impl_var *copy_impl_var(impl_var *var); +void free_impl_var(impl_var *var); + +struct impl_args; + +struct impl_arg *locate_impl_var_arg(impl_var *var, struct impl_args *args); #endif diff --git a/src/types/impls.c b/src/types/impls.c new file mode 100644 index 0000000..033f6d9 --- /dev/null +++ b/src/types/impls.c @@ -0,0 +1,64 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "impls.h" + +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; +} + +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); +} + +void dump_impls(int fd, impls *impls) { + size_t i; + + for (i = 0; i < impls->count; ++i) { + impl *impl = impls->list[i]; + + dump_impl(fd, impl); + dprintf(fd, "\n"); + } +} diff --git a/src/types/impls.h b/src/types/impls.h index 8bf17fb..51de860 100644 --- a/src/types/impls.h +++ b/src/types/impls.h @@ -1,28 +1,40 @@ -#ifndef _PSI_TYPES_IMPLS_H -#define _PSI_TYPES_IMPLS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_IMPLS_H +#define PSI_TYPES_IMPLS_H + +#include "impl.h" typedef struct impls { size_t count; impl **list; } impls; -static inline 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 inline 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); -} +impls *add_impl(impls *impls, impl *impl); +void free_impls(impls *impls); +void dump_impls(int fd, impls *impls); #endif diff --git a/src/types/let_callback.c b/src/types/let_callback.c new file mode 100644 index 0000000..4c5809c --- /dev/null +++ b/src/types/let_callback.c @@ -0,0 +1,77 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "data.h" + +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; +} + +void free_let_callback(let_callback *cb) { + free_let_func(cb->func); + free_set_values(cb->args); + free(cb); +} + +int validate_let_callback(struct psi_data *data, decl_var *cb_var, let_callback *cb, impl *impl) { + size_t i; + decl *cb_func; + decl_type *cb_type = real_decl_type(cb_var->arg->type); + + if (!validate_let_func(data, cb->func, cb_var, impl)) { + return 0; + } + + if (cb_type->type != PSI_T_FUNCTION) { + data->error(data, cb_var->token, PSI_WARNING, "Not a function: %s", cb_var->name); + return 0; + } + cb_func = cb_type->real.func; + for (i = 0; i < cb->args->count; ++i) { + if (!validate_set_value(data, cb->args->vals[i], cb_func->args->count, cb_func->args->args, 0)) { + return 0; + } + } + + if (!validate_decl_nodl(data, cb_func)) { + return 0; + } + + cb->decl = cb_func; + + return 1; +} diff --git a/src/types/let_callback.h b/src/types/let_callback.h index 0653b3d..e461b5a 100644 --- a/src/types/let_callback.h +++ b/src/types/let_callback.h @@ -1,5 +1,32 @@ -#ifndef _PSI_TYPES_LET_CALLBACK_H -#define _PSI_TYPES_LET_CALLBACK_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_LET_CALLBACK_H +#define PSI_TYPES_LET_CALLBACK_H + +#include "decl.h" typedef struct let_callback { struct let_func *func; @@ -7,20 +34,12 @@ typedef struct let_callback { 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); -} +let_callback *init_let_callback(struct let_func *func, struct set_values *args); +void free_let_callback(let_callback *cb); + +struct psi_data; +struct impl; + +int validate_let_callback(struct psi_data *data, decl_var *cb_var, let_callback *cb, struct impl *impl); #endif diff --git a/src/types/let_calloc.c b/src/types/let_calloc.c new file mode 100644 index 0000000..7c23112 --- /dev/null +++ b/src/types/let_calloc.c @@ -0,0 +1,48 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "let_calloc.h" + +let_calloc *init_let_calloc(num_exp *nmemb, num_exp *size) { + let_calloc *alloc = calloc(1, sizeof(*alloc)); + alloc->nmemb = nmemb; + alloc->size = size; + return alloc; +} + +void free_let_calloc(let_calloc *alloc) { + free_num_exp(alloc->nmemb); + free_num_exp(alloc->size); + free(alloc); +} diff --git a/src/types/let_calloc.h b/src/types/let_calloc.h index e6d3f91..dd0e65a 100644 --- a/src/types/let_calloc.h +++ b/src/types/let_calloc.h @@ -1,22 +1,39 @@ -#ifndef _PSI_TYPES_LET_CALLOC_H -#define _PSI_TYPES_LET_CALLOC_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_LET_CALLOC_H +#define PSI_TYPES_LET_CALLOC_H + +#include "num_exp.h" typedef struct let_calloc { num_exp *nmemb; num_exp *size; } let_calloc; -static inline let_calloc *init_let_calloc(num_exp *nmemb, num_exp *size) { - let_calloc *alloc = calloc(1, sizeof(*alloc)); - alloc->nmemb = nmemb; - alloc->size = size; - return alloc; -} - -static inline void free_let_calloc(let_calloc *alloc) { - free_num_exp(alloc->nmemb); - free_num_exp(alloc->size); - free(alloc); -} +let_calloc *init_let_calloc(num_exp *nmemb, num_exp *size); +void free_let_calloc(let_calloc *alloc); #endif diff --git a/src/types/let_func.c b/src/types/let_func.c new file mode 100644 index 0000000..d32b00b --- /dev/null +++ b/src/types/let_func.c @@ -0,0 +1,154 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include +#include + +#include "data.h" +#include "marshal.h" + +let_func *init_let_func(token_t type, const char *name, impl_var *var) { + let_func *func = calloc(1, sizeof(*func)); + func->type = type; + func->name = strdup(name); + func->var = var; + return func; +} + +void free_let_func(let_func *func) { + free_impl_var(func->var); + free(func->name); + if (func->inner) { + free_let_vals(func->inner); + } + free(func); +} + +void dump_let_func(int fd, let_func *func, unsigned level) { + dprintf(fd, "%s(%s", func->name, func->var->name); + + if (func->inner) { + size_t i; + + dprintf(fd, ","); + for (i = 0; i < func->inner->count; ++i) { + dprintf(fd, "\n"); + dump_let_val(fd, func->inner->vals[i], level+1, i+1 == func->inner->count); + } + dprintf(fd, "\n"); + dprintf(fd, "%s", psi_t_indent(level)); + } + dprintf(fd, ")"); +} + +static inline int validate_let_func_type(struct psi_data *data, let_func *func, impl *impl) { + switch (func->type) { + case PSI_T_BOOLVAL: + case PSI_T_INTVAL: + case PSI_T_FLOATVAL: + case PSI_T_STRVAL: + case PSI_T_STRLEN: + case PSI_T_PATHVAL: + case PSI_T_ARRVAL: + case PSI_T_OBJVAL: + case PSI_T_ZVAL: + case PSI_T_VOID: + return 1; + default: + data->error(data, func->var->token, PSI_WARNING, + "Unknown `let` cast function '%s' of implementation '%s'", + func->name, impl->func->name); + return 0; + } +} + +static inline int validate_let_func_inner(struct psi_data *data, let_func *func, decl_var *let_var, impl *impl) { + if (func->inner) { + size_t i; + decl_type *var_typ; + decl_args *sub_args = extract_decl_type_args(let_var->arg->type, &var_typ); + + if (!sub_args) { + data->error(data, let_var->token, PSI_WARNING, + "Inner let statement's values must refer to a structure type," + " got '%s' for '%s'", + var_typ->name, let_var->name); + return 0; + } + + for (i = 0; i < func->inner->count; ++i) { + let_val *inner = func->inner->vals[i]; + let_val *outer = func->outer; + const char *name = locate_let_val_varname(inner); + decl_arg *sub_arg; + + if (name) { + sub_arg = locate_decl_arg(sub_args, name); + } + if (!name || !sub_arg) { + data->error(data, let_var->token, PSI_WARNING, + "Unknown variable '%s' of '%s'", + name, var_typ->real.strct->name); + return 0; + } + if (!validate_let_val(data, inner, sub_arg->var, impl)) { + return 0; + } + } + } + return 1; +} + +int validate_let_func(struct psi_data *data, let_func *func, decl_var *let_var, impl *impl) { + if (func->outer) { + + } + if (impl->func->args) { + locate_impl_var_arg(func->var, impl->func->args); + } + if (!func->var->arg && !func->ref) { + data->error(data, func->var->token, PSI_WARNING, + "Unknown variable '%s%s' of `let` statement" + " for cast '%s' of implementation '%s'", + *func->var->name == '$' ? "" : "$", + func->var->name, func->name, impl->func->name); + return 0; + } + if (!validate_let_func_type(data, func, impl)) { + return 0; + } + if (!validate_let_func_inner(data, func, let_var, impl)) { + return 0; + } + return 1; +} diff --git a/src/types/let_func.h b/src/types/let_func.h index 850eb84..b02d20c 100644 --- a/src/types/let_func.h +++ b/src/types/let_func.h @@ -1,37 +1,52 @@ -#ifndef _PSI_TYPES_LET_FUNC_H -#define _PSI_TYPES_LET_FUNC_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. -typedef impl_val *(*let_func_handler)(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free); + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: -struct let_val; -struct let_vals; -static inline void free_let_vals(struct let_vals *vals); + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_LET_FUNC_H +#define PSI_TYPES_LET_FUNC_H + +#include "token.h" + +#include "impl_var.h" +#include "decl_arg.h" typedef struct let_func { token_t type; char *name; impl_var *var; - let_func_handler handler; struct let_vals *inner; struct let_val *outer; decl_arg *ref; } let_func; -static inline let_func *init_let_func(token_t type, const char *name, impl_var *var) { - let_func *func = calloc(1, sizeof(*func)); - func->type = type; - func->name = strdup(name); - func->var = var; - return func; -} - -static inline void free_let_func(let_func *func) { - free_impl_var(func->var); - free(func->name); - if (func->inner) { - free_let_vals(func->inner); - } - free(func); -} +let_func *init_let_func(token_t type, const char *name, impl_var *var); +void free_let_func(let_func *func); +void dump_let_func(int fd, let_func *func, unsigned level); + +struct psi_data; +struct impl; + +int validate_let_func(struct psi_data *data, let_func *func, decl_var *let_var, struct impl *impl); #endif diff --git a/src/types/let_stmt.c b/src/types/let_stmt.c new file mode 100644 index 0000000..db24d10 --- /dev/null +++ b/src/types/let_stmt.c @@ -0,0 +1,128 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "data.h" + +let_stmt *init_let_stmt(decl_var *var, let_val *val) { + let_stmt *let = calloc(1, sizeof(*let)); + let->var = var; + let->val = val; + return let; +} + +void free_let_stmt(let_stmt *stmt) { + if (stmt->val) { + if (stmt->val->kind == PSI_LET_TMP && stmt->var->arg) { + free_decl_arg(stmt->var->arg); + } + free_let_val(stmt->val); + } + free_decl_var(stmt->var); + free(stmt); +} + +void dump_let_stmt(int fd, let_stmt *let) { + dprintf(fd, "\tlet %s", let->var->name); + if (let->val) { + dprintf(fd, " = "); + dump_let_val(fd, let->val, 1, 1); + } +} + +int validate_let_stmts(struct psi_data *data, impl *impl) { + size_t i, j; + /* we can have multiple let stmts */ + + /* check that we have a decl arg and impl arg for every let stmt */ + for (i = 0; i < impl->stmts->let.count; ++i) { + let_stmt *let = impl->stmts->let.list[i]; + decl_var *let_var; + impl_var *let_ivar = NULL; + + if (let->val && let->val->kind == PSI_LET_TMP) { + let_var = let->val->data.var; + } else { + let_var = let->var; + } + + if (!locate_decl_var_arg(let_var, impl->decl->args, impl->decl->func)) { + data->error(data, let_var->token, PSI_WARNING, "Unknown variable '%s' in `let` statement" + " of implementation '%s'", let_var->name, impl->func->name); + return 0; + } + switch (let->val->kind) { + case PSI_LET_CALLBACK: + let_ivar = let->val->data.callback->func->var; + break; + case PSI_LET_FUNC: + let_ivar = let->val->data.func->var; + break; + default: + break; + } + if (let_ivar && !locate_impl_var_arg(let_ivar, impl->func->args)) { + data->error(data, let_var->token, PSI_WARNING, "Unknown variable '%s' in `let` statement" + " of implementation '%s'", let_ivar->name, impl->func->name); + return 0; + } + + if (!validate_let_val(data, let->val, let->var, impl)) { + return 0; + } + } + /* check that we have a let stmt for every decl arg */ + if (impl->decl->args) for (i = 0; i < impl->decl->args->count; ++i) { + decl_arg *darg = impl->decl->args->args[i]; + int check = 0; + + for (j = 0; j < impl->stmts->let.count; ++j) { + let_stmt *let = impl->stmts->let.list[j]; + + if (!strcmp(let->var->name, darg->var->name)) { + check = 1; + break; + } + } + if (!check) { + data->error(data, impl->func->token, PSI_WARNING, + "Missing `let` statement for arg '%s %.*s%s'" + " of declaration '%s' for implementation '%s'", + darg->type->name, (int) darg->var->pointer_level, "*****", + darg->var->name, impl->decl->func->var->name, impl->func->name); + return 0; + } + } + + return 1; +} diff --git a/src/types/let_stmt.h b/src/types/let_stmt.h index 8415e49..1ffd56e 100644 --- a/src/types/let_stmt.h +++ b/src/types/let_stmt.h @@ -1,27 +1,46 @@ -#ifndef _PSI_TYPES_LET_STMT_H -#define _PSI_TYPES_LET_STMT_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_LET_STMT_H +#define PSI_TYPES_LET_STMT_H + +#include "decl_var.h" +#include "let_val.h" typedef struct let_stmt { decl_var *var; let_val *val; } let_stmt; -static inline let_stmt *init_let_stmt(decl_var *var, let_val *val) { - let_stmt *let = calloc(1, sizeof(*let)); - let->var = var; - let->val = val; - return let; -} - -static inline void free_let_stmt(let_stmt *stmt) { - if (stmt->val) { - if (stmt->val->kind == PSI_LET_TMP && stmt->var->arg) { - free_decl_arg(stmt->var->arg); - } - free_let_val(stmt->val); - } - free_decl_var(stmt->var); - free(stmt); -} +let_stmt *init_let_stmt(decl_var *var, let_val *val); +void free_let_stmt(let_stmt *stmt); +void dump_let_stmt(int fd, let_stmt *let); + +struct psi_data; +struct impl; + +int validate_let_stmts(struct psi_data *data, struct impl *impl); #endif diff --git a/src/types/let_val.c b/src/types/let_val.c new file mode 100644 index 0000000..3be8e88 --- /dev/null +++ b/src/types/let_val.c @@ -0,0 +1,276 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include + +#include "data.h" + +let_val *init_let_val(enum let_val_kind kind, void *data) { + let_val *let = calloc(1, sizeof(*let)); + switch (let->kind = kind) { + case PSI_LET_NULL: + break; + case PSI_LET_NUMEXP: + let->data.num = data; + break; + 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; + case PSI_LET_TMP: + let->data.var = data; + break; + default: + assert(0); + } + return let; +} + +void free_let_val(let_val *let) { + switch (let->kind) { + case PSI_LET_NULL: + break; + case PSI_LET_NUMEXP: + free_num_exp(let->data.num); + break; + 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; + case PSI_LET_TMP: + free_decl_var(let->data.var); + break; + default: + assert(0); + } + free(let); +} + +void dump_let_val(int fd, let_val *val, unsigned level, int last) { + if (level > 1) { + /* only if not directly after `set ...` */ + dprintf(fd, "%s", psi_t_indent(level)); + } + + dprintf(fd, "%s", val->flags.one.is_reference ? "&" : ""); + + switch (val->kind) { + case PSI_LET_NULL: + dprintf(fd, "NULL"); + break; + case PSI_LET_TMP: + dump_decl_var(fd, val->data.var); + break; + case PSI_LET_CALLOC: + dprintf(fd, "calloc("); + dump_num_exp(fd, val->data.alloc->nmemb); + dprintf(fd, ", "); + dump_num_exp(fd, val->data.alloc->size); + dprintf(fd, ")"); + break; + case PSI_LET_CALLBACK: + dprintf(fd, "callback %s(%s(", val->data.callback->func->name, + val->data.callback->func->var->name); + if (val->data.callback->args) { + size_t i, c = val->data.callback->args->count; + + dprintf(fd, "\n"); + for (i = 0; i < c; ++i) { + set_value *set = val->data.callback->args->vals[i]; + ++level; + dump_set_value(fd, set, level, i + 1 == c); + --level; + } + dprintf(fd, "%s", psi_t_indent(level)); + } + dprintf(fd, "))"); + break; + case PSI_LET_FUNC: + dump_let_func(fd, val->data.func, level); + break; + case PSI_LET_NUMEXP: + dump_num_exp(fd, val->data.num); + break; + + default: + assert(0); + } + + if (level > 1) { + if (!last) { + dprintf(fd, ","); + } + } else { + dprintf(fd, ";"); + } +} + +int validate_let_val(struct psi_data *data, let_val *val, decl_var *let_var, impl *impl) { + switch (val->kind) { + case PSI_LET_NULL: + break; + case PSI_LET_TMP: + if (!let_var) { + data->error(data, NULL, PSI_WARNING, + "Ivalid let statement value of implementation '%s'", + impl->func->name); + return 0; + } + /* e.g. let bar = &strval($bar); // decl_arg(char **bar) */ + /* e.g. let foo = *bar; */ + let_var->pointer_level = val->data.var->pointer_level; + let_var->arg = init_decl_arg( + init_decl_type( + real_decl_type(val->data.var->arg->type)->type, + real_decl_type(val->data.var->arg->type)->name), + init_decl_var( + let_var->name, + let_var->pointer_level, + let_var->array_size)); + break; + case PSI_LET_NUMEXP: + if (!validate_num_exp(data, val->data.num, impl->decl->args, impl->decl->func, NULL)) { + return 0; + } + break; + case PSI_LET_CALLOC: + if (!validate_num_exp(data, val->data.alloc->nmemb, impl->decl->args, impl->decl->func, NULL)) { + return 0; + } + if (!validate_num_exp(data, val->data.alloc->size, impl->decl->args, impl->decl->func, NULL)) { + return 0; + } + break; + case PSI_LET_CALLBACK: + if (!let_var) { + data->error(data, NULL, PSI_WARNING, + "Ivalid let statement value of implementation '%s'", + impl->func->name); + return 0; + } + if (val->data.callback->func->inner) { + size_t i; + decl_type *var_typ = real_decl_type(let_var->arg->type); + decl_args *sub_args; + + switch (var_typ->type) { + case PSI_T_STRUCT: + sub_args = var_typ->real.strct->args; + break; + case PSI_T_UNION: + sub_args = var_typ->real.unn->args; + break; + default: + data->error(data, let_var->token, PSI_WARNING, + "Inner let statement's values must refer to a structure type, got '%s' for '%s'", + real_decl_type(let_var->arg->type)->name, let_var->name); + return 0; + } + for (i = 0; i < val->data.callback->func->inner->count; ++i) { + let_val *inner = val->data.callback->func->inner->vals[i]; + switch (inner->kind) { + case PSI_LET_FUNC: + inner->data.func->outer = val; + inner->data.func->ref = locate_decl_arg(sub_args, + &inner->data.func->var->name[1]); + break; + + break; + case PSI_LET_CALLBACK: + inner->data.callback->func->outer = val; + inner->data.callback->func->ref = locate_decl_arg(sub_args, + &inner->data.callback->func->var->name[1]); + break; + } + } + } + if (!validate_let_callback(data, let_var, val->data.callback, impl)) { + return 0; + } + break; + case PSI_LET_FUNC: + if (val->data.func->inner) { + size_t i; + decl_type *var_typ = real_decl_type(let_var->arg->type); + decl_args *sub_args; + + switch (var_typ->type) { + case PSI_T_STRUCT: + sub_args = var_typ->real.strct->args; + break; + case PSI_T_UNION: + sub_args = var_typ->real.unn->args; + break; + default: + data->error(data, let_var->token, PSI_WARNING, + "Inner let statement's values must refer to a structure type, got '%s' for '%s'", + real_decl_type(let_var->arg->type)->name, let_var->name); + return 0; + } + for (i = 0; i < val->data.func->inner->count; ++i) { + let_val *inner = val->data.func->inner->vals[i]; + switch (inner->kind) { + case PSI_LET_FUNC: + inner->data.func->outer = val; + inner->data.func->ref = locate_decl_arg(sub_args, + &inner->data.func->var->name[1]); + break; + case PSI_LET_CALLBACK: + inner->data.callback->func->outer = val; + inner->data.callback->func->ref = locate_decl_arg(sub_args, + &inner->data.callback->func->var->name[1]); + break; + } + } + } + + val->data.func->var; + + if (!validate_let_func(data, val->data.func, let_var, impl)) { + return 0; + } + break; + } + + return 1; +} diff --git a/src/types/let_val.h b/src/types/let_val.h index e2827e2..c3b5f38 100644 --- a/src/types/let_val.h +++ b/src/types/let_val.h @@ -1,6 +1,36 @@ -#ifndef _PSI_TYPES_LET_VAL_H -#define _PSI_TYPES_LET_VAL_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_LET_VAL_H +#define PSI_TYPES_LET_VAL_H + +#include "decl_var.h" +#include "num_exp.h" +#include "let_calloc.h" +#include "let_callback.h" +#include "let_func.h" enum let_val_kind { PSI_LET_NULL, @@ -10,7 +40,9 @@ enum let_val_kind { PSI_LET_FUNC, PSI_LET_TMP, }; + #define PSI_LET_REFERENCE 0x1; + typedef struct let_val { enum let_val_kind kind; decl_var *var; @@ -29,53 +61,51 @@ typedef struct let_val { } flags; } let_val; -static inline let_val *init_let_val(enum let_val_kind kind, void *data) { - let_val *let = calloc(1, sizeof(*let)); - switch (let->kind = kind) { - case PSI_LET_NULL: - break; - case PSI_LET_NUMEXP: - let->data.num = data; - break; - case PSI_LET_CALLOC: - let->data.alloc = data; - break; +let_val* init_let_val(enum let_val_kind kind, void* data); +void free_let_val(let_val* let); +void dump_let_val(int fd, let_val *val, unsigned level, int last); + +struct psi_data; +struct impl; + +int validate_let_val(struct psi_data *data, let_val *val, decl_var *let_var, struct impl *impl); + +static inline decl_arg *locate_let_val_inner_ref(let_val *val) { + decl_arg *ref = NULL; + + switch (val->kind) { case PSI_LET_CALLBACK: - let->data.callback = data; + ref = val->data.callback->func->ref; break; case PSI_LET_FUNC: - let->data.func = data; + ref = val->data.func->ref; break; - case PSI_LET_TMP: - let->data.var = data; + default: break; - EMPTY_SWITCH_DEFAULT_CASE(); } - return let; + return ref; +} +static inline impl_var *locate_let_val_impl_var(let_val *val) { + if (val) { + switch (val->kind) { + case PSI_LET_CALLBACK: + return val->data.callback->func->var; + case PSI_LET_FUNC: + return val->data.func->var; + default: + break; + } + } + return NULL; } -static inline void free_let_val(let_val *let) { - switch (let->kind) { - case PSI_LET_NULL: - break; - case PSI_LET_NUMEXP: - free_num_exp(let->data.num); - break; - 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; - case PSI_LET_TMP: - free_decl_var(let->data.var); - break; - EMPTY_SWITCH_DEFAULT_CASE(); +static inline const char *locate_let_val_varname(let_val *val) { + impl_var *var = locate_let_val_impl_var(val); + + if (var) { + return &var->name[1]; } - free(let); + return NULL; } #endif diff --git a/src/types/let_vals.c b/src/types/let_vals.c new file mode 100644 index 0000000..05b7bc2 --- /dev/null +++ b/src/types/let_vals.c @@ -0,0 +1,65 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "let_vals.h" + +let_vals *init_let_vals(struct let_val *val) { + let_vals *vals = calloc(1, sizeof(*vals)); + if (val) { + vals->count = 1; + vals->vals = calloc(1, sizeof(val)); + vals->vals[0] = val; + } + return vals; +} + +let_vals *add_let_val(let_vals *vals, struct let_val *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; +} + +void free_let_vals(let_vals *vals) { + if (vals->vals) { + size_t i; + for (i = 0; i < vals->count; ++i) { + free_let_val(vals->vals[i]); + } + free(vals->vals); + } + free(vals); +} diff --git a/src/types/let_vals.h b/src/types/let_vals.h index f46345c..442ba59 100644 --- a/src/types/let_vals.h +++ b/src/types/let_vals.h @@ -1,42 +1,40 @@ -#ifndef _PSI_TYPES_LET_VALS_H -#define _PSI_TYPES_LET_VALS_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. -struct let_val; + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_LET_VALS_H +#define PSI_TYPES_LET_VALS_H + +#include "let_val.h" typedef struct let_vals { struct let_val **vals; size_t count; } let_vals; -static inline let_vals *init_let_vals(struct let_val *val) { - let_vals *vals = calloc(1, sizeof(*vals)); - if (val) { - vals->count = 1; - vals->vals = calloc(1, sizeof(val)); - vals->vals[0] = val; - } - return vals; -} - -static inline let_vals *add_let_val(let_vals *vals, struct let_val *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_let_vals(let_vals *vals) { - if (vals->vals) { - size_t i; - - for (i = 0; i < vals->count; ++i) { - free_let_val(vals->vals[i]); - } - free(vals->vals); - } - free(vals); -} +let_vals *init_let_vals(struct let_val *val); +let_vals *add_let_val(let_vals *vals, struct let_val *val); +void free_let_vals(let_vals *vals); #endif diff --git a/src/types/num_exp.c b/src/types/num_exp.c new file mode 100644 index 0000000..d2fcc1a --- /dev/null +++ b/src/types/num_exp.c @@ -0,0 +1,233 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include +#include + +#include "data.h" +#include "calc.h" + +num_exp *init_num_exp(token_t t, void *num) { + num_exp *exp = calloc(1, sizeof(*exp)); + switch (exp->t = t) { + case PSI_T_NUMBER: + case PSI_T_NSNAME: + exp->u.numb = strdup(num); + break; + case PSI_T_NAME: + exp->u.dvar = num; + break; + default: + assert(0); + } + return exp; +} + +num_exp *copy_num_exp(num_exp *exp) { + decl_var *dvar; + num_exp *num = calloc(1, sizeof(*num)); + memcpy(num, exp, sizeof(*num)); + if (num->token) { + num->token = psi_token_copy(num->token); + } + if (num->operand) { + num->operand = copy_num_exp(num->operand); + } + switch (num->t) { + case PSI_T_NUMBER: + case PSI_T_NSNAME: + num->u.numb = strdup(num->u.numb); + break; + case PSI_T_NAME: + dvar = init_decl_var(num->u.dvar->name, num->u.dvar->pointer_level, + num->u.dvar->array_size); + dvar->arg = num->u.dvar->arg; + if (num->u.dvar->token) { + dvar->token = psi_token_copy(num->u.dvar->token); + } + num->u.dvar = dvar; + break; + default: + assert(0); + } + return num; +} + +void free_num_exp(num_exp *exp) { + if (exp->token) { + free(exp->token); + } + switch (exp->t) { + case PSI_T_NUMBER: + free(exp->u.numb); + break; + case PSI_T_NSNAME: + break; + case PSI_T_NAME: + free_decl_var(exp->u.dvar); + break; + case PSI_T_ENUM: + break; + default: + assert(0); + } + if (exp->operand) { + free_num_exp(exp->operand); + } + free(exp); +} + +void dump_num_exp(int fd, num_exp *exp) { + while (exp) { + switch (exp->t) { + case PSI_T_NUMBER: + dprintf(fd, "%s", exp->u.numb); + break; + case PSI_T_NSNAME: + dprintf(fd, "%s", exp->u.cnst->name); + break; + case PSI_T_NAME: + dump_decl_var(fd, exp->u.dvar); + break; + case PSI_T_ENUM: + dprintf(fd, "%s", exp->u.enm->name); + break; + default: + assert(0); + } + if (exp->operand) { + char op; + + switch (exp->operator) { + case PSI_T_PLUS: op = '+'; break; + case PSI_T_MINUS: op = '-'; break; + case PSI_T_ASTERISK:op = '*'; break; + case PSI_T_SLASH: op = '/'; break; + default: + assert(0); + } + dprintf(fd, " %c ", op); + } + exp = exp->operand; + } +} + +static inline constant *locate_num_exp_constant(num_exp *exp, constants *consts) { + size_t i; + + for (i = 0; i < consts->count; ++i) { + constant *cnst = consts->list[i]; + + if (!strcmp(cnst->name, exp->u.numb)) { + free(exp->u.numb); + return exp->u.cnst = cnst; + } + } + + return NULL; +} +static inline decl_enum_item *locate_num_exp_enum_item_ex(num_exp *exp, decl_enum *e) { + size_t k; + + if (e) for (k = 0; k < e->items->count; ++k) { + decl_enum_item *i = e->items->list[k]; + + if (!strcmp(i->name, exp->u.dvar->name)) { + free_decl_var(exp->u.dvar); + exp->t = PSI_T_ENUM; + exp->u.enm = i; + return i; + } + } + return NULL; +} +static inline decl_enum_item *locate_num_exp_enum_item(num_exp *exp, decl_enums *enums) { + size_t j; + + if (enums) for (j = 0; j < enums->count; ++j) { + decl_enum *e = enums->list[j]; + decl_enum_item *i = locate_num_exp_enum_item_ex(exp, e); + + if (i) { + return i; + } + } + return NULL; +} + +int validate_num_exp(struct psi_data *data, num_exp *exp, decl_args *dargs, decl_arg *func, decl_enum *enm) { + if (exp->operand) { + switch (exp->operator) { + case PSI_T_PLUS: + exp->calculator = psi_calc_add; + break; + case PSI_T_MINUS: + exp->calculator = psi_calc_sub; + break; + case PSI_T_ASTERISK: + exp->calculator = psi_calc_mul; + break; + case PSI_T_SLASH: + exp->calculator = psi_calc_div; + break; + default: + assert(0); + } + if (!validate_num_exp(data, exp->operand, dargs, func, enm)) { + return 0; + } + } + switch (exp->t) { + case PSI_T_NAME: + if (!locate_decl_var_arg(exp->u.dvar, dargs, func)) { + if (!locate_num_exp_enum_item(exp, data->enums) && !locate_num_exp_enum_item_ex(exp, enm)) { + data->error(data, exp->token, PSI_WARNING, "Unknown variable '%s' in numeric expression", + exp->u.dvar->name); + return 0; + } + } + return 1; + case PSI_T_NSNAME: + if (!locate_num_exp_constant(exp, data->consts)) { + data->error(data, exp->token, PSI_WARNING, "Unknown constant '%s' in numeric expression", + exp->u.numb); + return 0; + } + return 1; + case PSI_T_NUMBER: + case PSI_T_ENUM: + return 1; + default: + return 0; + } +} diff --git a/src/types/num_exp.h b/src/types/num_exp.h index c54a5af..e1b3b22 100644 --- a/src/types/num_exp.h +++ b/src/types/num_exp.h @@ -1,5 +1,36 @@ -#ifndef _PSI_TYPES_NUM_EXP -#define _PSI_TYPES_NUM_EXP +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_NUM_EXP +#define PSI_TYPES_NUM_EXP + +#include "token.h" + +#include "constant.h" +#include "decl_var.h" +#include "impl_val.h" typedef struct num_exp { struct psi_token *token; @@ -15,71 +46,16 @@ typedef struct num_exp { struct num_exp *operand; } num_exp; -static inline num_exp *init_num_exp(token_t t, void *num) { - num_exp *exp = calloc(1, sizeof(*exp)); - switch (exp->t = t) { - case PSI_T_NUMBER: - case PSI_T_NSNAME: - exp->u.numb = strdup(num); - break; - case PSI_T_NAME: - exp->u.dvar = num; - break; - EMPTY_SWITCH_DEFAULT_CASE(); - } - return exp; -} - -static inline num_exp *copy_num_exp(num_exp *exp) { - decl_var *dvar; - num_exp *num = calloc(1, sizeof(*num)); - - memcpy(num, exp, sizeof(*num)); +num_exp *init_num_exp(token_t t, void *num); +num_exp *copy_num_exp(num_exp *exp); +void free_num_exp(num_exp *exp); +void dump_num_exp(int fd, num_exp *exp); - if (num->token) { - num->token = psi_token_copy(num->token); - } - if (num->operand) { - num->operand = copy_num_exp(num->operand); - } - switch (num->t) { - case PSI_T_NUMBER: - case PSI_T_NSNAME: - num->u.numb = strdup(num->u.numb); - break; - case PSI_T_NAME: - dvar = init_decl_var(num->u.dvar->name, num->u.dvar->pointer_level, num->u.dvar->array_size); - dvar->arg = num->u.dvar->arg; - if (num->u.dvar->token) { - dvar->token = psi_token_copy(num->u.dvar->token); - } - num->u.dvar = dvar; - break; - } - return num; -} +struct psi_data; +struct decl_args; +struct decl_arg; +struct decl_enum; -static inline void free_num_exp(num_exp *exp) { - if (exp->token) { - free(exp->token); - } - switch (exp->t) { - case PSI_T_NUMBER: - free(exp->u.numb); - break; - case PSI_T_NSNAME: - break; - case PSI_T_NAME: - free_decl_var(exp->u.dvar); - break; - case PSI_T_ENUM: - break; - EMPTY_SWITCH_DEFAULT_CASE(); - } - if (exp->operand) { - free_num_exp(exp->operand); - } - free(exp); -} +int validate_num_exp(struct psi_data *data, num_exp *exp, struct decl_args *dargs, struct decl_arg *func, struct decl_enum *enm); #endif diff --git a/src/types/return_stmt.c b/src/types/return_stmt.c new file mode 100644 index 0000000..8d32fc8 --- /dev/null +++ b/src/types/return_stmt.c @@ -0,0 +1,106 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "data.h" + +return_stmt *init_return_stmt(set_value *val) { + return_stmt *ret = calloc(1, sizeof(*ret)); + ret->set = val; + return ret; +} + +void free_return_stmt(return_stmt *ret) { + if (ret->token) { + free(ret->token); + } + free_set_value(ret->set); + free(ret); +} + +void dump_return_stmt(int fd, return_stmt *ret) { + dprintf(fd, "\treturn "); + dump_set_value(fd, ret->set, 1, 0); +} + +static inline decl *locate_impl_decl(decls *decls, return_stmt *ret) { + if (decls) { + size_t i; + + for (i = 0; i < decls->count; ++i) { + if (!strcmp(decls->list[i]->func->var->name, ret->set->vars->vars[0]->name)) { + ret->decl = decls->list[i]->func; + return decls->list[i]; + } + } + } + + return NULL; +} + +int validate_return_stmt(struct psi_data *data, impl *impl) { + return_stmt *ret; + + /* we must have exactly one ret stmt delcaring the native func to call */ + /* and which type cast to apply */ + if (impl->stmts->ret.count != 1) { + if (impl->stmts->ret.count > 1) { + data->error(data, impl->stmts->ret.list[1]->token, PSI_WARNING, + "Too many `return` statements for implmentation %s;" + " found %zu, exactly one is needed", + impl->func->name, impl->stmts->ret.count); + } else { + data->error(data, impl->func->token, PSI_WARNING, + "Missing `return` statement for implementation %s", + impl->func->name); + } + return 0; + } + + ret = impl->stmts->ret.list[0]; + + if (!(impl->decl = locate_impl_decl(data->decls, ret))) { + data->error(data, ret->token, PSI_WARNING, + "Missing declaration '%s' for `return` statment for implementation %s", + ret->set->vars->vars[0]->name, impl->func->name); + return 0; + } + + if (!validate_set_value(data, ret->set, 1, &ret->decl, impl->decl->args ? (int) impl->decl->args->count : 0, impl->decl->args ? impl->decl->args->args : NULL, 0)) { + return 0; + } + + //impl->decl->impl = impl; + + return 1; +} diff --git a/src/types/return_stmt.h b/src/types/return_stmt.h index 12e371e..ee4dc6d 100644 --- a/src/types/return_stmt.h +++ b/src/types/return_stmt.h @@ -1,5 +1,33 @@ -#ifndef _PSI_TYPES_RETURN_STMT_H -#define _PSI_TYPES_RETURN_STMT_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_RETURN_STMT_H +#define PSI_TYPES_RETURN_STMT_H + +#include "set_value.h" +#include "decl_arg.h" typedef struct return_stmt { struct psi_token *token; @@ -7,18 +35,13 @@ typedef struct return_stmt { decl_arg *decl; } return_stmt; -static inline return_stmt *init_return_stmt(set_value *val) { - return_stmt *ret = calloc(1, sizeof(*ret)); - ret->set = val; - return ret; -} - -static inline void free_return_stmt(return_stmt *ret) { - if (ret->token) { - free(ret->token); - } - free_set_value(ret->set); - free(ret); -} +return_stmt *init_return_stmt(set_value *val); +void free_return_stmt(return_stmt *ret); +void dump_return_stmt(int fd, return_stmt *ret); + +struct psi_data; +struct impl; + +int validate_return_stmt(struct psi_data *data, struct impl *impl); #endif diff --git a/src/types/set_func.c b/src/types/set_func.c new file mode 100644 index 0000000..f06fbf6 --- /dev/null +++ b/src/types/set_func.c @@ -0,0 +1,51 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include +#include + +#include "set_func.h" + +set_func *init_set_func(token_t type, const char *name) { + set_func *func = calloc(1, sizeof(*func)); + func->type = type; + func->name = strdup(name); + return func; +} + +void free_set_func(set_func *func) { + if (func->token) { + free(func->token); + } + free(func->name); + free(func); +} diff --git a/src/types/set_func.h b/src/types/set_func.h index 54d61d3..f774114 100644 --- a/src/types/set_func.h +++ b/src/types/set_func.h @@ -1,5 +1,33 @@ -#ifndef _PSI_TYPES_SET_FUNC_H -#define _PSI_TYPES_SET_FUNC_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_SET_FUNC_H +#define PSI_TYPES_SET_FUNC_H + +#include "token.h" +#include "impl_val.h" struct set_value; @@ -10,19 +38,7 @@ typedef struct set_func { void (*handler)(zval *, struct set_value *set, impl_val *ret_val); } set_func; -static inline set_func *init_set_func(token_t type, const char *name) { - set_func *func = calloc(1, sizeof(*func)); - func->type = type; - func->name = strdup(name); - return func; -} - -static inline void free_set_func(set_func *func) { - if (func->token) { - free(func->token); - } - free(func->name); - free(func); -} +set_func *init_set_func(token_t type, const char *name); +void free_set_func(set_func *func); #endif diff --git a/src/types/set_stmt.c b/src/types/set_stmt.c new file mode 100644 index 0000000..ad67340 --- /dev/null +++ b/src/types/set_stmt.c @@ -0,0 +1,124 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "data.h" + +set_stmt *init_set_stmt(impl_var *var, set_value *val) { + set_stmt *set = calloc(1, sizeof(*set)); + set->var = var; + set->val = val; + return set; +} + +void free_set_stmt(set_stmt *set) { + free_impl_var(set->var); + free_set_value(set->val); + free(set); +} + +void dump_set_stmt(int fd, set_stmt *set) { + dprintf(fd, "\tset %s = ", set->var->name); + dump_set_value(fd, set->val, 1, 0); +} + +int validate_set_stmts(struct psi_data *data, impl *impl) { + size_t i, j, k; + /* we can have any count of set stmts; processing out vars */ + /* check that set stmts reference known variables */ + for (i = 0; i < impl->stmts->set.count; ++i) { + set_stmt *set = impl->stmts->set.list[i]; + int check = 0; + + if (impl->func->args) for (j = 0; j < impl->func->args->count; ++j) { + impl_arg *iarg = impl->func->args->args[j]; + + if (!strcmp(set->var->name, iarg->var->name)) { + set->arg = iarg; + check = 1; + break; + } + } + if (!check) { + data->error(data, set->var->token, PSI_WARNING, "Unknown variable '$%s' of `set` statement" + " of implementation '%s'", + set->var->name, impl->func->name); + return 0; + } + + for (j = 0; j < set->val->vars->count; ++j) { + decl_var *set_var = set->val->vars->vars[j]; + + check = 0; + if (impl->decl->args) { + for (k = 0; k < impl->decl->args->count; ++k) { + decl_arg *set_arg = impl->decl->args->args[k]; + + if (!strcmp(set_var->name, set_arg->var->name)) { + check = 1; + set_var->arg = set_arg; + if (!validate_set_value(data, set->val, 1, &set_arg, 1, &impl->decl->func, impl->decl->args->count, impl->decl->args->args, 0)) { + return 0; + } + break; + } + } + } + if (!check) { + for (k = 0; k < impl->stmts->let.count; ++k) { + let_stmt *let = impl->stmts->let.list[k]; + + /* check temp vars */ + if (let->val && let->val->kind == PSI_LET_TMP) { + if (!strcmp(set_var->name, let->var->name)) { + check = 1; + set_var->arg = let->var->arg; + if (!validate_set_value(data, set->val, 1, &set_var->arg, 1, &impl->decl->func, impl->decl->args->count, impl->decl->args->args, 0)) { + return 0; + } + break; + } + } + } + } + + if (!check) { + data->error(data, set_var->token, PSI_WARNING, "Unknown value '%s' of `set` statement" + " for variable '$%s' of implementation '%s'", + set_var->name, set->arg->var->name, impl->func->name); + return 0; + } + } + } + return 1; +} diff --git a/src/types/set_stmt.h b/src/types/set_stmt.h index 360b324..8c96d43 100644 --- a/src/types/set_stmt.h +++ b/src/types/set_stmt.h @@ -1,5 +1,34 @@ -#ifndef _PSI_TYPES_SET_STMT_H -#define _PSI_TYPES_SET_STMT_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_SET_STMT_H +#define PSI_TYPES_SET_STMT_H + +#include "impl_var.h" +#include "impl_arg.h" +#include "set_value.h" typedef struct set_stmt { impl_var *var; @@ -7,17 +36,13 @@ typedef struct set_stmt { impl_arg *arg; } set_stmt; -static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) { - set_stmt *set = calloc(1, sizeof(*set)); - set->var = var; - set->val = val; - return set; -} - -static inline void free_set_stmt(set_stmt *set) { - free_impl_var(set->var); - free_set_value(set->val); - free(set); -} +set_stmt *init_set_stmt(impl_var *var, set_value *val); +void free_set_stmt(set_stmt *set); +void dump_set_stmt(int fd, set_stmt *set); + +struct psi_data; +struct impl; + +int validate_set_stmts(struct psi_data *data, struct impl *impl); #endif diff --git a/src/types/set_value.c b/src/types/set_value.c new file mode 100644 index 0000000..5defbd7 --- /dev/null +++ b/src/types/set_value.c @@ -0,0 +1,251 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "data.h" +#include "marshal.h" + +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; +} + +set_value *add_inner_set_value(set_value *val, set_value *inner) { + val->inner = add_set_value(val->inner, inner); + inner->outer.set = val; + return val; +} + +void free_set_value(set_value *val) { + if (val->func) { + free_set_func(val->func); + } + if (val->vars) { + free_decl_vars(val->vars); + } + if (val->inner + && (!val->outer.set || val->outer.set->inner != val->inner)) { + free_set_values(val->inner); + } + if (val->num) { + free_num_exp(val->num); + } + free(val); +} + +void dump_set_value(int fd, set_value *set, unsigned level, int last) { + size_t i; + + if (level > 1) { + /* only if not directly after `set ...` */ + dprintf(fd, "%s", psi_t_indent(level)); + } + + if (set->func->type == PSI_T_ELLIPSIS) { + dprintf(fd, "%s(", set->outer.set->func->name); + } else { + dprintf(fd, "%s(", set->func->name); + } + + for (i = 0; i < set->vars->count; ++i) { + decl_var *svar = set->vars->vars[i]; + if (i) { + dprintf(fd, ", "); + } + dump_decl_var(fd, svar); + } + + if (set->func->type == PSI_T_ELLIPSIS) { + dprintf(fd, ", ..."); + } + if (set->num) { + dprintf(fd, ", "); + dump_num_exp(fd, set->num); + } + if (set->inner && set->inner->vals && set->func->type != PSI_T_ELLIPSIS) { + dprintf(fd, ",\n"); + for (i = 0; i < set->inner->count; ++i) { + dump_set_value(fd, set->inner->vals[i], level+1, i == (set->inner->count - 1)); + } + /* only if inner stmts, i.e. with new lines, were dumped */ + dprintf(fd, "%s", psi_t_indent(level)); + } + if (level > 1) { + dprintf(fd, ")%s\n", last ? "" : ","); + } else { + dprintf(fd, ");"); + } +} + +static inline void decl_var_arg_v(decl_args *args, va_list argp) { + int argc; + decl_arg **argv; + + memset(args, 0, sizeof(*args)); + + while ((argc = va_arg(argp, int))) { + argv = va_arg(argp, decl_arg **); + while (argc--) { + add_decl_arg(args, *argv++); + } + } +} + +static inline int validate_set_value_handler(set_value *set) { + switch (set->func->type) { + case PSI_T_TO_BOOL: set->func->handler = psi_to_bool; break; + case PSI_T_TO_INT: set->func->handler = psi_to_int; break; + case PSI_T_TO_FLOAT: set->func->handler = psi_to_double; break; + case PSI_T_TO_STRING: set->func->handler = psi_to_string; break; + case PSI_T_TO_ARRAY: set->func->handler = psi_to_array; break; + case PSI_T_TO_OBJECT: set->func->handler = psi_to_object; break; + case PSI_T_VOID: set->func->handler = psi_to_void; break; + case PSI_T_ZVAL: set->func->handler = psi_to_zval; break; + case PSI_T_ELLIPSIS: + if (set->outer.set && set->outer.set->func->type == PSI_T_TO_ARRAY) { + set->func->handler = psi_to_recursive; + set->inner = set->outer.set->inner; + break; + } + /* no break */ + default: + return 0; + } + return 1; +} + +int validate_set_value(struct psi_data *data, set_value *set, ...) { + va_list argp; + decl_args args = {0}; + int check; + + va_start(argp, set); + decl_var_arg_v(&args, argp); + va_end(argp); + + check = validate_set_value_ex(data, set, NULL, &args); + if (args.args) { + free(args.args); + } + return check; +} + +int validate_set_value_ex(struct psi_data *data, set_value *set, decl_arg *ref, decl_args *ref_list) { + size_t i; + decl_type *ref_type; + decl_var *set_var = set->vars->vars[0]; + + if (!validate_set_value_handler(set)) { + data->error(data, set->func->token, PSI_WARNING, "Invalid cast '%s' in `set` statement", set->func->name); + return 0; + } + + for (i = 0; i < set->vars->count; ++i) { + decl_var *svar = set->vars->vars[i]; + if (!svar->arg && !locate_decl_var_arg(svar, ref_list, NULL)) { + data->error(data, svar->token, PSI_WARNING, "Unknown variable '%s' in `set` statement", svar->name); + return 0; + } + } + + if (!ref) { + ref = set_var->arg; + } + ref_type = real_decl_type(ref->type); + + if (set->inner && set->inner->count) { + int is_to_array = (set->func->type == PSI_T_TO_ARRAY); + int is_pointer_to_struct = (ref_type->type == PSI_T_STRUCT && ref->var->pointer_level); + + if (!is_to_array && !is_pointer_to_struct) { + data->error(data, set->func->token, E_WARNING, "Inner `set` statement casts only work with " + "to_array() casts on structs or pointers: %s(%s...", set->func->name, set->vars->vars[0]->name); + return 0; + } + } + if (set->num) { + if (!validate_num_exp(data, set->num, ref_list, ref, NULL)) { + return 0; + } + } + + if (set->inner && (ref_type->type == PSI_T_STRUCT || ref_type->type == PSI_T_UNION)) { + /* to_array(struct, to_...) */ + if (!set->outer.set || set->outer.set->inner->vals != set->inner->vals) { + for (i = 0; i < set->inner->count; ++i) { + decl_var *sub_var = set->inner->vals[i]->vars->vars[0]; + decl_arg *sub_ref; + + switch (ref_type->type) { + case PSI_T_STRUCT: + sub_ref = locate_decl_struct_member(ref_type->real.strct, sub_var); + break; + case PSI_T_UNION: + sub_ref = locate_decl_union_member(ref_type->real.unn, sub_var); + break; + } + + if (sub_ref) { + if (!validate_set_value_ex(data, set->inner->vals[i], sub_ref, ref_type->real.strct->args)) { + return 0; + } + } + } + } + } else if (set->inner && set->inner->count == 1) { + /* to_array(ptr, to_string(*ptr)) */ + decl_var *sub_var = set->inner->vals[0]->vars->vars[0]; + decl_arg *sub_ref = locate_decl_var_arg(sub_var, ref_list, ref); + + if (sub_ref) { + if (strcmp(sub_var->name, set_var->name)) { + data->error(data, sub_var->token, E_WARNING, "Inner `set` statement casts on pointers must reference the same variable"); + return 0; + } + if (!validate_set_value_ex(data, set->inner->vals[0], sub_ref, ref_list)) { + return 0; + } + } + } else if (set->inner && set->inner->count > 1) { + data->error(data, set->func->token, E_WARNING, + "Inner `set` statement casts on pointers may only occur once, " + "unless the outer type is a struct or union, got '%s%s'", + ref_type->name, psi_t_indirection(ref->var->pointer_level)); + return 0; + } + + return 1; +} + diff --git a/src/types/set_value.h b/src/types/set_value.h index 0b6b269..6a5ee82 100644 --- a/src/types/set_value.h +++ b/src/types/set_value.h @@ -1,5 +1,36 @@ -#ifndef _PSI_TYPES_SET_VALUE_H -#define _PSI_TYPES_SET_VALUE_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_SET_VALUE_H +#define PSI_TYPES_SET_VALUE_H + +#include "set_func.h" +#include "decl_vars.h" +#include "decl_args.h" +#include "num_exp.h" +#include "impl_val.h" typedef struct set_value { set_func *func; @@ -12,34 +43,16 @@ typedef struct set_value { struct set_values *inner; } set_value; -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 struct set_values *add_set_value(struct set_values *vals, struct set_value *val); -static inline set_value *add_inner_set_value(set_value *val, set_value *inner) { - val->inner = add_set_value(val->inner, inner); - inner->outer.set = val; - return val; -} - -static inline void free_set_value(set_value *val) { - if (val->func) { - free_set_func(val->func); - } - if (val->vars) { - free_decl_vars(val->vars); - } - if (val->inner && (!val->outer.set || val->outer.set->inner != val->inner)) { - free_set_values(val->inner); - } - if (val->num) { - free_num_exp(val->num); - } - free(val); -} +set_value *init_set_value(set_func *func, decl_vars *vars); +set_value *add_inner_set_value(set_value *val, set_value *inner); +void free_set_value(set_value *val); +void dump_set_value(int fd, set_value *set, unsigned level, int last); + +#include + +struct psi_data; + +int validate_set_value(struct psi_data *data, set_value *set, ...); +int validate_set_value_ex(struct psi_data *data, set_value *set, decl_arg *ref, decl_args *ref_list); #endif diff --git a/src/types/set_values.c b/src/types/set_values.c new file mode 100644 index 0000000..4cbd97d --- /dev/null +++ b/src/types/set_values.c @@ -0,0 +1,66 @@ +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif + +#include +#include + +#include "set_values.h" + +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; +} + +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; +} + +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); +} + diff --git a/src/types/set_values.h b/src/types/set_values.h index b089372..8d78dc8 100644 --- a/src/types/set_values.h +++ b/src/types/set_values.h @@ -1,40 +1,40 @@ -#ifndef _PSI_TYPES_SET_VALUES_H -#define _PSI_TYPES_SET_VALUES_H +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef PSI_TYPES_SET_VALUES_H +#define PSI_TYPES_SET_VALUES_H + +#include "set_value.h" typedef struct set_values { - struct set_value **vals; + set_value **vals; size_t count; } set_values; -static inline set_values *init_set_values(struct 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, struct 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); -} +set_values *init_set_values(set_value *val); +set_values *add_set_value(set_values *vals, set_value *val); +void free_set_values(set_values *vals); #endif diff --git a/tests/ndbm/ndbm.psi b/tests/ndbm/ndbm.psi index e61ba2d..139f446 100644 --- a/tests/ndbm/ndbm.psi +++ b/tests/ndbm/ndbm.psi @@ -7,15 +7,24 @@ function psi\dbm_open(string $file, int $open_flags, int $file_mode) : object { function psi\dbm_store(object $db, array $key, array $data, int $mode) : int { let db = objval($db); - let key = arrval($key); - let content = arrval($data); + let key = arrval($key, + strval($dptr), + intval($dsize) + ); + let content = arrval($data, + strval($dptr), + intval($dsize) + ); let store_mode = intval($mode); return to_int(dbm_store); } function psi\dbm_fetch(object $db, array $key) : array { let db = objval($db); - let key = arrval($key); + let key = arrval($key, + strval($dptr), + intval($dsize) + ); return to_array(dbm_fetch, to_string(dptr, dsize), to_int(dsize) -- 2.30.2