From 338674c084a29e1a49803efa0374d44ecce8f4c3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Tue, 20 Oct 2020 18:57:14 +0200 Subject: [PATCH] Str2type (#218) * :art: Introduce str2type * :art: start moving str2xxx using Gwion * :art: str2sym * :art: str2symlist * :art: str2var * :art: str2varlist * :art: tmpl_list * :art: _str2decl * :art: str2decl * :art: ac->pos * :art: str2type * :art: clean and split * :art: clean headers * :art: Use in foreach * :art: Use in lib_func --- include/import/checker.h | 16 ++- include/import/internals.h | 3 + include/import/special.h | 5 - src/import/cleaner.c | 53 ++++++++ src/import/import_cdef.c | 4 +- src/import/import_checker.c | 228 +++++++++++++++------------------- src/import/import_enum.c | 5 +- src/import/import_fdef.c | 6 +- src/import/import_oper.c | 2 +- src/import/import_tdef.c | 2 +- src/import/import_type.c | 4 +- src/import/import_udef.c | 6 +- src/lib/lib_func.c | 7 +- src/parse/check.c | 28 +---- tests/error/auto_not_array.gw | 2 +- 15 files changed, 191 insertions(+), 180 deletions(-) create mode 100644 src/import/cleaner.c diff --git a/include/import/checker.h b/include/import/checker.h index c0b3486f..3b1cc6cc 100644 --- a/include/import/checker.h +++ b/include/import/checker.h @@ -45,8 +45,20 @@ typedef struct OperCK { // name_checker ? ANN void func_checker_clean(const Gwi gwi, struct ImportCK *ck); ANN m_bool check_typename_def(const Gwi gwi, struct ImportCK *ck); -ANN Symbol str2sym(const Gwi gwi, const m_str path); -ANN ID_List str2symlist(const Gwi gwi, const m_str path); + +ANN Symbol str2sym(const Gwion gwi, const m_str path, const loc_t pos); +ANN ID_List str2symlist(const Gwion gwi, const m_str path, const loc_t); +ANN Var_Decl str2var(const Gwion, const m_str, const loc_t); +ANN Var_Decl_List str2varlist(const Gwion, const m_str, const loc_t); +ANN Type_Decl* str2decl(const Gwion, const m_str, const loc_t); +ANN Type str2type(const Gwion, const m_str, const loc_t); + +#define gwi_str2sym(gwi, path) str2sym(gwi->gwion, path, gwi->loc) +#define gwi_str2symlist(gwi, path) str2symlist(gwi->gwion, path, gwi->loc) +#define gwi_str2var(gwi, path) str2var(gwi->gwion, path, gwi->loc) +#define gwi_str2varlist(gwi, path) str2varlist(gwi->gwion, path, gwi->loc) +#define gwi_str2decl(gwi, path) str2decl(gwi->gwion, path, gwi->loc) +#define gwi_str2type(gwi, path) str2type(gwi->gwion, path, gwi->loc) ANN m_bool ck_ini(const Gwi, const enum importck_type); ANN m_bool ck_ok(const Gwi, const enum importck_type); diff --git a/include/import/internals.h b/include/import/internals.h index fc58056c..bbb3b162 100644 --- a/include/import/internals.h +++ b/include/import/internals.h @@ -4,6 +4,9 @@ #define GWI_ERR(a,...) { env_err(gwi->gwion->env, gwi->loc, (a), ## __VA_ARGS__); } #define GWI_ERR_B(a,...) { GWI_ERR((a), ## __VA_ARGS__); return GW_ERROR; } #define GWI_ERR_O(a,...) { GWI_ERR((a), ## __VA_ARGS__); return NULL; } +#define GWION_ERR(pos, a,...) { env_err(gwion->env, pos, (a), ## __VA_ARGS__); } +#define GWION_ERR_B(pos, a,...) { GWION_ERR(pos, (a), ## __VA_ARGS__); return GW_ERROR; } +#define GWION_ERR_O(pos, a,...) { GWION_ERR(pos, (a), ## __VA_ARGS__); return NULL; } #define ENV_ERR_B(pos, a,...) { env_err(env, pos, (a), ## __VA_ARGS__); return GW_ERROR; } #define ENV_ERR_O(pos, a,...) { env_err(env, pos, (a), ## __VA_ARGS__); return NULL; } diff --git a/include/import/special.h b/include/import/special.h index 4d7e5f83..fcefde15 100644 --- a/include/import/special.h +++ b/include/import/special.h @@ -11,9 +11,4 @@ ANN void gwi_reserve(const Gwi, const m_str); typedef struct SpecialId_* SpecialId; ANN void gwi_specialid(const Gwi gwi, const m_str id, const SpecialId); ANN void gwi_set_loc(const Gwi, const m_str, const uint); - -// TODO: move me -ANN Type_Decl* str2decl(const Gwi, const m_str); -ANN Var_Decl str2var(const Gwi, const m_str); -ANN Var_Decl_List str2varlist(const Gwi, const m_str); #endif diff --git a/src/import/cleaner.c b/src/import/cleaner.c new file mode 100644 index 00000000..aeada70d --- /dev/null +++ b/src/import/cleaner.c @@ -0,0 +1,53 @@ +/** @file: cleaner.c * + * \brief: functions to clean import module * + * */ +#include "gwion_util.h" +#include "gwion_ast.h" +#include "gwion_env.h" +#include "vm.h" +#include "traverse.h" +#include "instr.h" +#include "object.h" +#include "emit.h" +#include "gwion.h" +#include "operator.h" +#include "import.h" +#include "gwi.h" + +ANN m_bool ck_ini(const Gwi gwi, const enum importck_type t) { + if(gwi->ck) // TODO: improve error message + GWI_ERR_B(_("already importing")) + gwi->ck = mp_calloc2(gwi->gwion->mp, sizeof(ImportCK)); + gwi->ck->type = t; + return GW_OK; +} + +ANN m_bool ck_ok(const Gwi gwi, const enum importck_type t) { + if(!gwi->ck) + GWI_ERR_B(_("import not started")) + if(gwi->ck->type == t) + return GW_OK; + // TODO: improve error message + GWI_ERR_B(_("already importing")) +} + +ANN void ck_end(const Gwi gwi) { + mp_free2(gwi->gwion->mp, sizeof(ImportCK), gwi->ck); + gwi->ck = NULL; +} + +typedef void (*cleaner) (MemPool, ImportCK*); +static cleaner cleaners[] = +{ + ck_clean_edef, + ck_clean_udef, + ck_clean_tdef, + NULL,// ck_clean_oper, + ck_clean_item, + ck_clean_fdef +}; + +ANN void ck_clean(const Gwi gwi) { + cleaners[gwi->ck->type](gwi->gwion->mp, gwi->ck); + memset(gwi->ck, 0, sizeof(ImportCK)); +} diff --git a/src/import/import_cdef.c b/src/import/import_cdef.c index 4419336f..e04f8006 100644 --- a/src/import/import_cdef.c +++ b/src/import/import_cdef.c @@ -74,7 +74,7 @@ ANN2(1,2) Type handle_class(const Gwi gwi, Type_Decl *td) { ANN2(1,2) Type gwi_class_ini(const Gwi gwi, const m_str name, const m_str parent) { struct ImportCK ck = { .name=name }; CHECK_BO(check_typename_def(gwi, &ck)) - DECL_OO(Type_Decl *,td, = str2decl(gwi, parent ?: "Object")) + DECL_OO(Type_Decl *,td, = gwi_str2decl(gwi, parent ?: "Object")) Tmpl* tmpl = ck.tmpl ? new_tmpl_base(gwi->gwion->mp, ck.tmpl) : NULL; if(tmpl) CHECK_BO(template_push_types(gwi->gwion->env, tmpl)) @@ -103,7 +103,7 @@ ANN2(1,2) Type gwi_class_ini(const Gwi gwi, const m_str name, const m_str parent } ANN Type gwi_struct_ini(const Gwi gwi, const m_str name) { - CHECK_OO(str2sym(gwi, name)) + CHECK_OO(gwi_str2sym(gwi, name)) const Type t = new_type(gwi->gwion->mp, ++gwi->gwion->env->scope->type_xid, name, gwi->gwion->type[et_compound]); t->e->tuple = new_tupleform(gwi->gwion->mp, NULL); gwi_type_flag(t); diff --git a/src/import/import_checker.c b/src/import/import_checker.c index b10c9fa5..efd1db70 100644 --- a/src/import/import_checker.c +++ b/src/import/import_checker.c @@ -15,23 +15,20 @@ #include "import.h" #include "gwi.h" -__attribute__((returns_nonnull)) -ANN static Symbol gwisym(const Gwi gwi, const m_str str) { - return insert_symbol(gwi->gwion->st, str); -} - struct td_checker { m_str str; + const loc_t pos; }; struct AC { m_str str; Exp base; Exp exp; + loc_t pos; m_uint depth; }; -ANN static m_bool ac_run(const Gwi gwi, struct AC *ac); +ANN static m_bool ac_run(const Gwion gwion, struct AC *const ac); ANN static Array_Sub mk_array(MemPool mp, struct AC *ac) { const Array_Sub array = new_array_sub(mp, ac->base); array->depth = ac->depth; @@ -39,8 +36,12 @@ ANN static Array_Sub mk_array(MemPool mp, struct AC *ac) { return array; } +__attribute__((returns_nonnull)) +ANN static Symbol gwisym(const Gwion gwion, const m_str str) { + return insert_symbol(gwion->st, str); +} -ANN static Symbol __str2sym(const Gwi gwi, struct td_checker *tdc) { +ANN static Symbol __str2sym(const Gwion gwion, struct td_checker *tdc) { char buf[strlen(tdc->str) + 1]; m_str tmp = buf; if(*tdc->str == '@') @@ -52,72 +53,71 @@ ANN static Symbol __str2sym(const Gwi gwi, struct td_checker *tdc) { *tmp++ = *tdc->str++; } if(tmp == buf) - GWI_ERR_O("empty symbol"); + GWION_ERR_O(tdc->pos, "empty symbol"); *tmp = '\0'; - return gwisym(gwi, buf); + return gwisym(gwion, buf); } -ANN static inline Symbol _str2sym(const Gwi gwi, struct td_checker *tdc, const m_str path) { - const Symbol sym = __str2sym(gwi, tdc); +ANN static inline Symbol _str2sym(const Gwion gwion, struct td_checker *tdc, const m_str path) { + const Symbol sym = __str2sym(gwion, tdc); if(*tdc->str && *tdc->str != ':') - GWI_ERR_O(_("illegal character '%c' in path '%s'."), *tdc->str, path) + GWION_ERR_O(tdc->pos, _("illegal character '%c' in path '%s'."), *tdc->str, path) return sym; } /** convert a string to a symbol, with error checking **/ -ANN Symbol str2sym(const Gwi gwi, const m_str path) { - struct td_checker tdc = { .str=path }; - return _str2sym(gwi, &tdc, path); +ANN Symbol str2sym(const Gwion gwion, const m_str path, const loc_t pos) { + struct td_checker tdc = { .str=path, .pos=pos }; + return _str2sym(gwion, &tdc, path); } // only in enum.c -ANN ID_List str2symlist(const Gwi gwi, const m_str path) { - DECL_OO(const Symbol, sym, = str2sym(gwi, path)) - return new_id_list(gwi->gwion->mp, sym, loc(gwi)); +ANN ID_List str2symlist(const Gwion gwion, const m_str path, const loc_t pos) { + DECL_OO(const Symbol, sym, = str2sym(gwion, path, pos)) + return new_id_list(gwion->mp, sym, loc_cpy(gwion->mp, pos)); } -ANN Var_Decl str2var(const Gwi gwi, const m_str path) { - struct td_checker tdc = { .str=path }; - DECL_OO(const Symbol, sym, = __str2sym(gwi, &tdc)) - struct AC ac = { .str = tdc.str }; - CHECK_BO(ac_run(gwi, &ac)) +ANN Var_Decl str2var(const Gwion gwion, const m_str path, const loc_t pos) { + struct td_checker tdc = { .str=path, .pos=pos }; + DECL_OO(const Symbol, sym, = __str2sym(gwion, &tdc)) + struct AC ac = { .str = tdc.str, .pos=pos }; + CHECK_BO(ac_run(gwion, &ac)) const Array_Sub array = ac.depth ? - mk_array(gwi->gwion->mp, &ac) : NULL; - return new_var_decl(gwi->gwion->mp, sym, array, loc(gwi)); + mk_array(gwion->mp, &ac) : NULL; + return new_var_decl(gwion->mp, sym, array, loc_cpy(gwion->mp, pos)); } // only in udef.c -ANN Var_Decl_List str2varlist(const Gwi gwi, const m_str path) { - DECL_OO(const Var_Decl, var, = str2var(gwi, path)) - return new_var_decl_list(gwi->gwion->mp, var, NULL); +ANN Var_Decl_List str2varlist(const Gwion gwion, const m_str path, const loc_t pos) { + DECL_OO(const Var_Decl, var, = str2var(gwion, path, pos)) + return new_var_decl_list(gwion->mp, var, NULL); } -ANN static ID_List _tmpl_list(const Gwi gwi, struct td_checker *tdc) { - DECL_OO(const Symbol, sym, = __str2sym(gwi, tdc)) +ANN static ID_List _tmpl_list(const Gwion gwion, struct td_checker *tdc) { + DECL_OO(const Symbol, sym, = __str2sym(gwion, tdc)) ID_List next = NULL; if(*tdc->str == ',') { ++tdc->str; - if(!(next = _tmpl_list(gwi, tdc)) || next == (ID_List)GW_ERROR) + if(!(next = _tmpl_list(gwion, tdc)) || next == (ID_List)GW_ERROR) return (ID_List)GW_ERROR; } - const ID_List list = new_id_list(gwi->gwion->mp, sym, loc(gwi)); + const ID_List list = new_id_list(gwion->mp, sym, loc_cpy(gwion->mp, tdc->pos)); list->next = next; return list; } -ANN static ID_List __tmpl_list(const Gwi gwi, struct td_checker *tdc) { +ANN static ID_List __tmpl_list(const Gwion gwion, struct td_checker *tdc) { if(tdc->str[0] != ':') return NULL; if(tdc->str[1] != '[') return (ID_List)GW_ERROR; tdc->str += 2; - const ID_List list = _tmpl_list(gwi, tdc); + const ID_List list = _tmpl_list(gwion, tdc); if(list == (ID_List)GW_ERROR) return (ID_List)GW_ERROR; - if(tdc->str[0] != ']') { -// unfinished template + if(tdc->str[0] != ']') { // unfinished template if(list) - free_id_list(gwi->gwion->mp, list); + free_id_list(gwion->mp, list); return (ID_List)GW_ERROR; } ++tdc->str; @@ -125,10 +125,10 @@ ANN static ID_List __tmpl_list(const Gwi gwi, struct td_checker *tdc) { } ANN m_bool check_typename_def(const Gwi gwi, ImportCK *ck) { - struct td_checker tdc = { .str= ck->name }; - if(!(ck->sym = _str2sym(gwi, &tdc, tdc.str))) + struct td_checker tdc = { .str= ck->name, .pos=gwi->loc }; + if(!(ck->sym = _str2sym(gwi->gwion, &tdc, tdc.str))) return GW_ERROR; - ID_List il = __tmpl_list(gwi, &tdc); + ID_List il = __tmpl_list(gwi->gwion, &tdc); if(il == (ID_List)GW_ERROR) return GW_ERROR; ck->tmpl = il; @@ -137,175 +137,145 @@ ANN m_bool check_typename_def(const Gwi gwi, ImportCK *ck) { } -ANN m_bool ck_ini(const Gwi gwi, const enum importck_type t) { - if(gwi->ck) // TODO: improve error message - GWI_ERR_B(_("already importing")) - gwi->ck = mp_calloc2(gwi->gwion->mp, sizeof(ImportCK)); - gwi->ck->type = t; - return GW_OK; -} - -ANN m_bool ck_ok(const Gwi gwi, const enum importck_type t) { - if(!gwi->ck) - GWI_ERR_B(_("import not started")) - if(gwi->ck->type == t) - return GW_OK; - // TODO: improve error message - GWI_ERR_B(_("already importing")) -} - -ANN void ck_end(const Gwi gwi) { - mp_free2(gwi->gwion->mp, sizeof(ImportCK), gwi->ck); - gwi->ck = NULL; -} - -typedef void (*cleaner) (MemPool, ImportCK*); -static cleaner cleaners[] = -{ - ck_clean_edef, - ck_clean_udef, - ck_clean_tdef, -NULL,// ck_clean_oper, - ck_clean_item, - ck_clean_fdef -}; - -ANN void ck_clean(const Gwi gwi) { - cleaners[gwi->ck->type](gwi->gwion->mp, gwi->ck); - memset(gwi->ck, 0, sizeof(ImportCK)); -} - -ANN Type_Decl* _str2decl(const Gwi gwi, struct td_checker *tdc); -ANN Type_List __str2tl(const Gwi gwi, struct td_checker *tdc) { - Type_Decl *td = _str2decl(gwi, tdc); +ANN static Type_Decl* _str2decl(const Gwion gwion, struct td_checker *tdc); +ANN Type_List __str2tl(const Gwion gwion, struct td_checker *tdc) { + Type_Decl *td = _str2decl(gwion, tdc); if(!td) - GWI_ERR_O("invalid types"); + GWION_ERR_O(tdc->pos, "invalid types"); Type_List next = NULL; if(*tdc->str == ',') { ++tdc->str; - if(!(next = __str2tl(gwi, tdc))) { - free_type_decl(gwi->gwion->mp, td); + if(!(next = __str2tl(gwion, tdc))) { + free_type_decl(gwion->mp, td); return NULL; } } - return new_type_list(gwi->gwion->mp, td, next); + return new_type_list(gwion->mp, td, next); } -ANN static Type_List td_tmpl(const Gwi gwi, struct td_checker *tdc) { +ANN static Type_List td_tmpl(const Gwion gwion, struct td_checker *tdc) { if(*tdc->str != ':') return NULL; // GW_PASS ++tdc->str; if(*tdc->str != '[') { - GWI_ERR("invalid character"); + GWION_ERR(tdc->pos, "invalid character"); return (Type_List)GW_ERROR; } ++tdc->str; - Type_List tl = __str2tl(gwi, tdc); + Type_List tl = __str2tl(gwion, tdc); if(!tl) return (Type_List)GW_ERROR; if(tdc->str[0] != ']') { - free_type_list(gwi->gwion->mp, tl); - GWI_ERR("unfinished template"); + free_type_list(gwion->mp, tl); + GWION_ERR(tdc->pos, "unfinished template"); return (Type_List)GW_ERROR; } ++tdc->str; return tl; } -ANN static void ac_add_exp(struct AC *ac, const Exp exp) { - if(ac->exp) - ac->exp = (ac->exp->next = exp); - else - ac->base = ac->exp = exp; -} - - -ANN Type_Decl* _str2decl(const Gwi gwi, struct td_checker *tdc) { - DECL_OO(const Symbol, sym, = __str2sym(gwi, tdc)) - struct AC ac = { .str = tdc->str }; - CHECK_BO(ac_run(gwi, &ac)) +ANN static Type_Decl* _str2decl(const Gwion gwion, struct td_checker *tdc) { + DECL_OO(const Symbol, sym, = __str2sym(gwion, tdc)) + struct AC ac = { .str = tdc->str, .pos=tdc->pos }; + CHECK_BO(ac_run(gwion, &ac)) tdc->str = ac.str; - Type_List tl = td_tmpl(gwi, tdc); + Type_List tl = td_tmpl(gwion, tdc); if(tl == (Type_List)GW_ERROR) return NULL; Type_Decl *next = NULL; if(*tdc->str == '.') { ++tdc->str; - if(!(next = _str2decl(gwi, tdc))) { + if(!(next = _str2decl(gwion, tdc))) { if(tl) - free_type_list(gwi->gwion->mp, tl); + free_type_list(gwion->mp, tl); if(ac.base) - free_exp(gwi->gwion->mp, ac.base); + free_exp(gwion->mp, ac.base); return NULL; } } - Type_Decl *td = new_type_decl(gwi->gwion->mp, sym, loc(gwi)); + Type_Decl *td = new_type_decl(gwion->mp, sym, loc_cpy(gwion->mp, tdc->pos)); td->next = next; td->types = tl; if(ac.depth) - td->array = mk_array(gwi->gwion->mp, &ac); + td->array = mk_array(gwion->mp, &ac); return td; } -ANN Type_Decl* str2decl(const Gwi gwi, const m_str str) { +ANN Type_Decl* str2decl(const Gwion gwion, const m_str str, const loc_t pos) { const ae_flag flag = strncmp(str, "nonnull ", 8) ? ae_flag_none : ae_flag_nonnull; - struct td_checker tdc = { .str=str }; + struct td_checker tdc = { .str=str, .pos=pos }; if(flag == ae_flag_nonnull) tdc.str += 8; - DECL_OO(Type_Decl *, td, = _str2decl(gwi, &tdc)) + DECL_OO(Type_Decl *, td, = _str2decl(gwion, &tdc)) if(*tdc.str) { - free_type_decl(gwi->gwion->mp, td); - GWI_ERR_O("excedental character '%c'", *tdc.str); + free_type_decl(gwion->mp, td); + GWION_ERR_O(pos, "excedental character '%c'", *tdc.str); } td->flag |= flag; return td; } -ANN static inline m_bool ac_finish(const Gwi gwi, struct AC *ac) { +ANN Type str2type(const Gwion gwion, const m_str str, const loc_t pos) { + DECL_OO(Type_Decl *, td, = str2decl(gwion, str, pos)) + const Type t = known_type(gwion->env, td); + if(t) + return t; + free_type_decl(gwion->mp, td); + return NULL; +} + +ANN static inline m_bool ac_finish(const Gwion gwion, const struct AC *ac) { if(*ac->str == ']') return GW_OK; - GWI_ERR_B("unfinished array"); + GWION_ERR_B(ac->pos, "unfinished array"); } -ANN static inline m_bool ac_num(const Gwi gwi, const m_int num) { +ANN static inline m_bool ac_num(const Gwion gwion, const struct AC *ac, const m_int num) { if(num >= 0) return GW_OK; - GWI_ERR_B("negative array dimension") + GWION_ERR_B(ac->pos, "negative array dimension") } -ANN static inline m_bool ac_exp(const Gwi gwi, struct AC *ac) { +ANN static inline m_bool ac_exp(const Gwion gwion, const struct AC *ac) { if(!ac->depth || ac->base) return GW_OK; - GWI_ERR_B("malformed array [][...]") + GWION_ERR_B(ac->pos, "malformed array [][...]") +} + +ANN static void ac_add_exp(struct AC *ac, const Exp exp) { + if(ac->exp) + ac->exp = (ac->exp->next = exp); + else + ac->base = ac->exp = exp; } -ANN static inline m_bool ac_noexp(const Gwi gwi, struct AC *ac) { +ANN static inline m_bool ac_noexp(const Gwion gwion, struct AC *ac) { if(!ac->exp) return GW_OK; - GWI_ERR_B("malformed array [...][]") + GWION_ERR_B(ac->pos, "malformed array [...][]") } -ANN static m_bool _ac_run(const Gwi gwi, struct AC *ac) { +ANN static m_bool _ac_run(const Gwion gwion, struct AC *const ac) { const m_str str = ac->str; const m_int num = strtol(str, &ac->str, 10); - CHECK_BB(ac_finish(gwi, ac)) + CHECK_BB(ac_finish(gwion, ac)) if(str != ac->str) { - CHECK_BB(ac_num(gwi, num)) - CHECK_BB(ac_exp(gwi, ac)) - const Exp exp = new_prim_int(gwi->gwion->mp, num, loc(gwi)); + CHECK_BB(ac_num(gwion, ac, num)) + CHECK_BB(ac_exp(gwion, ac)) + const Exp exp = new_prim_int(gwion->mp, num, loc_cpy(gwion->mp, ac->pos)); ac_add_exp(ac, exp); } else - CHECK_BB(ac_noexp(gwi, ac)) + CHECK_BB(ac_noexp(gwion, ac)) ++ac->str; return GW_OK; } -ANN static m_bool ac_run(const Gwi gwi, struct AC *ac) { +ANN static m_bool ac_run(const Gwion gwion, struct AC *const ac) { while(*ac->str) { if(*ac->str != '[') break; ++ac->str; - CHECK_BB(_ac_run(gwi, ac)) + CHECK_BB(_ac_run(gwion, ac)) ++ac->depth; } return GW_OK; diff --git a/src/import/import_enum.c b/src/import/import_enum.c index 9e423e8f..ee398e37 100644 --- a/src/import/import_enum.c +++ b/src/import/import_enum.c @@ -21,8 +21,7 @@ ANN2(1) m_int gwi_enum_ini(const Gwi gwi, const m_str type) { CHECK_BB(ck_ini(gwi, ck_edef)) if(type) -// CHECK_OB((gwi->ck->sym = str2sym(gwi, type))) - CHECK_OB((gwi->ck->xid = str2sym(gwi, type))) + CHECK_OB((gwi->ck->xid = gwi_str2sym(gwi, type))) vector_init(&gwi->ck->v); return GW_OK; } @@ -50,7 +49,7 @@ void Append(DL_Enum* d, const ID_List list) { //! TODO: change return type to m_bool ANN m_int gwi_enum_add(const Gwi gwi, const m_str name, const m_uint i) { CHECK_BB(ck_ok(gwi, ck_edef)) - DECL_OB(const ID_List, list, = str2symlist(gwi, name)) + DECL_OB(const ID_List, list, = gwi_str2symlist(gwi, name)) add2list(gwi->ck, list); ALLOC_PTR(gwi->gwion->mp, addr, m_int, i); vector_add(&gwi->ck->v, (vtype)addr); diff --git a/src/import/import_fdef.c b/src/import/import_fdef.c index 383fc0a9..9fdf808c 100644 --- a/src/import/import_fdef.c +++ b/src/import/import_fdef.c @@ -20,7 +20,7 @@ ANN2(1,2,3) static m_bool dl_func_init(const Gwi gwi, const restrict m_str t, CHECK_BB(ck_ini(gwi, ck_fdef)) gwi->ck->name = n; CHECK_BB(check_typename_def(gwi, gwi->ck)) - CHECK_OB((gwi->ck->td = str2decl(gwi, t))) + CHECK_OB((gwi->ck->td = gwi_str2decl(gwi, t))) vector_init(&gwi->ck->v); return GW_OK; } @@ -91,8 +91,8 @@ ANN m_int gwi_func_end(const Gwi gwi, const f_xfun addr, const ae_flag flag) { ANN m_int gwi_func_arg(const Gwi gwi, const restrict m_str t, const restrict m_str n) { CHECK_BB(ck_ok(gwi, ck_fdef)) - DECL_OB(Type_Decl*, td, = str2decl(gwi, t)) - const Var_Decl var = str2var(gwi, n); + DECL_OB(Type_Decl*, td, = gwi_str2decl(gwi, t)) + const Var_Decl var = gwi_str2var(gwi, n); if(var) { const Arg_List arg = new_arg_list(gwi->gwion->mp, td, var, NULL); vector_add(&gwi->ck->v, (vtype)arg); diff --git a/src/import/import_oper.c b/src/import/import_oper.c index adcb6004..0d744892 100644 --- a/src/import/import_oper.c +++ b/src/import/import_oper.c @@ -19,7 +19,7 @@ ANN static Type _get_type(const Gwi gwi, const m_str s) { if(s == (m_str)OP_ANY_TYPE) return OP_ANY_TYPE; - Type_Decl *td = str2decl(gwi, s); + Type_Decl *td = gwi_str2decl(gwi, s); const Type t = known_type(gwi->gwion->env, td); free_type_decl(gwi->gwion->mp, td); return t; diff --git a/src/import/import_tdef.c b/src/import/import_tdef.c index 46bd7bcb..32006abc 100644 --- a/src/import/import_tdef.c +++ b/src/import/import_tdef.c @@ -20,7 +20,7 @@ ANN m_int gwi_typedef_ini(const Gwi gwi, const restrict m_str type, const restri CHECK_BB(ck_ini(gwi, ck_tdef)) gwi->ck->name = name; CHECK_BB(check_typename_def(gwi, gwi->ck)) - return (gwi->ck->td = str2decl(gwi, type)) ? GW_OK : GW_ERROR; + return (gwi->ck->td = gwi_str2decl(gwi, type)) ? GW_OK : GW_ERROR; } ANN Type gwi_typedef_end(const Gwi gwi, const ae_flag flag) { diff --git a/src/import/import_type.c b/src/import/import_type.c index 2e9e4c0d..d5a64244 100644 --- a/src/import/import_type.c +++ b/src/import/import_type.c @@ -15,7 +15,7 @@ #include "gwi.h" ANN2(1) static Type get_parent(const Gwi gwi, const m_str parent_name) { - Type_Decl* td = parent_name ? str2decl(gwi, parent_name) : NULL; + Type_Decl* td = parent_name ? gwi_str2decl(gwi, parent_name) : NULL; if(td) { if(td->array || td->types) { const m_str str = td->array ? "array" : "template"; @@ -30,7 +30,7 @@ ANN2(1) static Type get_parent(const Gwi gwi, const m_str parent_name) { } ANN2(1,2) Type gwi_mk_type(const Gwi gwi, const m_str name, const m_uint size, const m_str parent_name) { - CHECK_OO(str2sym(gwi, name)) + CHECK_OO(gwi_str2sym(gwi, name)) const Type parent = get_parent(gwi, parent_name); const Type t = new_type(gwi->gwion->mp, 0, name, parent); t->size = size; diff --git a/src/import/import_udef.c b/src/import/import_udef.c index 4f5e2276..73f8f061 100644 --- a/src/import/import_udef.c +++ b/src/import/import_udef.c @@ -16,8 +16,8 @@ // move me ANN Exp make_exp(const Gwi gwi, const m_str type, const m_str name) { - DECL_OO(Type_Decl*, td, = str2decl(gwi, type)) - const Var_Decl_List vlist = str2varlist(gwi, name); + DECL_OO(Type_Decl*, td, = gwi_str2decl(gwi, type)) + const Var_Decl_List vlist = gwi_str2varlist(gwi, name); if(vlist) return new_exp_decl(gwi->gwion->mp, td, vlist); free_type_decl(gwi->gwion->mp, td); @@ -27,7 +27,7 @@ ANN Exp make_exp(const Gwi gwi, const m_str type, const m_str name) { ANN2(1) m_int gwi_union_ini(const Gwi gwi, const m_str type, const m_str name) { CHECK_BB(ck_ini(gwi, ck_udef)) if(name) - CHECK_OB((gwi->ck->xid = str2sym(gwi, name))) + CHECK_OB((gwi->ck->xid = gwi_str2sym(gwi, name))) gwi->ck->name = type; if(type) CHECK_BB(check_typename_def(gwi, gwi->ck)) diff --git a/src/lib/lib_func.c b/src/lib/lib_func.c index 53410ab7..526c1df2 100644 --- a/src/lib/lib_func.c +++ b/src/lib/lib_func.c @@ -301,10 +301,9 @@ ANN static Type fork_type(const Env env, const Exp_Unary* unary) { fork_exp(env, unary); if(t == env->gwion->type[et_void]) return env->gwion->type[et_fork]; - Type_Decl td0 = { .xid=insert_symbol(t->name), .pos=exp_self(unary)->pos }; - struct Type_List_ tl = { .td=&td0 }; - Type_Decl td = { .xid=insert_symbol("TypedFork"), .types=&tl, .pos=exp_self(unary)->pos }; - return known_type(env, &td); + char c[21 + strlen(t->name)]; + sprintf(c, "nonnull TypedFork:[%s]", t->name); + return str2type(env->gwion, c, exp_self(unary)->pos); } static OP_CHECK(opck_spork) { diff --git a/src/parse/check.c b/src/parse/check.c index 652821df..bf1c580c 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -158,7 +158,6 @@ ANN static inline m_bool inferable(const Env env, const Type t, const loc_t pos) ERR_B(pos, _("can't infer type.")) } -ANN static Type_Decl* type2td(const Env env, const Type t, const loc_t loc); ANN Type check_exp_decl(const Env env, const Exp_Decl* decl) { if(!decl->td->xid) return no_xid(env, decl); @@ -874,21 +873,6 @@ ANN static inline m_bool for_empty(const Env env, const Stmt_For stmt) { return GW_OK; } -// the two next function do not account for arrays. (they are only stmt_each() helpers -ANN static Type_Decl* _type2td(const Env env, const Type t, Type_Decl *next) { - Type_Decl *td = new_type_decl(env->gwion->mp, insert_symbol(t->name), - loc_cpy(env->gwion->mp, td_pos(next))); - td->next = next; - return !t->e->owner_class ? td : _type2td(env, t->e->owner_class, td); - -} - -ANN static Type_Decl* type2td(const Env env, const Type t, const loc_t loc) { - Type_Decl *td = new_type_decl(env->gwion->mp, insert_symbol(t->name), - loc_cpy(env->gwion->mp, loc)); - return !t->e->owner_class ? td : _type2td(env, t->e->owner_class, td); -} - ANN static m_bool do_stmt_each(const Env env, const Stmt_Each stmt) { DECL_OB(Type, t, = check_exp(env, stmt->exp)) while(GET_FLAG(t, typedef)) @@ -897,17 +881,13 @@ ANN static m_bool do_stmt_each(const Env env, const Stmt_Each stmt) { const m_uint depth = t->array_depth - 1; if(!ptr || isa(t, env->gwion->type[et_array]) < 0) ERR_B(stmt_self(stmt)->pos, _("type '%s' is not array.\n" - " This is not allowed in auto loop"), stmt->exp->info->type->name) + " This is not allowed in foreach loop"), stmt->exp->info->type->name) if(stmt->is_ptr) { - struct Type_List_ tl = {}; if(depth) ptr = array_type(env, ptr, depth); - Type_Decl *td0 = type2td(env, ptr, stmt->exp->pos), - td = { .xid=insert_symbol("Ptr"), .types=&tl, .pos=stmt->exp->pos }; - tl.td = td0; - ptr = known_type(env, &td); - td0->array = NULL; - free_type_decl(env->gwion->mp, td0); + char c[15 + strlen(ptr->name)]; + sprintf(c, "nonnull Ptr:[%s]", ptr->name); + ptr = str2type(env->gwion, c, stmt->exp->pos); const Type base = get_type(ptr); if(!GET_FLAG(base, check)) CHECK_BB(ensure_traverse(env, base)) diff --git a/tests/error/auto_not_array.gw b/tests/error/auto_not_array.gw index c3943d69..9ee67a4d 100644 --- a/tests/error/auto_not_array.gw +++ b/tests/error/auto_not_array.gw @@ -1,4 +1,4 @@ -#! [contains] not allowed in auto loop +#! [contains] not allowed in foreach loop var Object i; foreach(a: i) <<< a >>>; -- 2.43.0