From 4819048d7a8939e315cb5a32b15159b498943021 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Thu, 15 Aug 2019 14:23:43 +0200 Subject: [PATCH] :art: Improve variable declaration --- include/env.h | 2 ++ src/parse/scan1.c | 13 ++++++------- src/parse/type_decl.c | 8 +++++++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/include/env.h b/include/env.h index bbd16cf3..9ba10888 100644 --- a/include/env.h +++ b/include/env.h @@ -52,6 +52,8 @@ ANN Type type_decl_resolve(const Env, const Type_Decl*); ANEW ANN m_str tl2str(const Env, const Type_List); // in type_decl.c ANN m_bool compat_func(const __restrict__ Func_Def, const __restrict__ Func_Def); ANN Type known_type(const Env env, const Type_Decl*); +ANN Type known_type_noref(const Env env, const Type_Decl* td); +ANN Type prim_ref(const Env env, const Type t, const Type_Decl* td); ANN m_bool env_access(const Env env, const ae_flag flag, const loc_t pos); ANN m_bool env_storage(const Env env, ae_flag flag, const loc_t pos); ANN void env_add_type(const Env, const Type); diff --git a/src/parse/scan1.c b/src/parse/scan1.c index d4954890..f029f4c7 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -15,7 +15,7 @@ ANN static m_bool scan1_stmt_list(const Env env, Stmt_List list); ANN static m_bool scan1_stmt(const Env env, Stmt stmt); ANN static Type void_type(const Env env, const Type_Decl* td) { - DECL_OO(const Type, t, = known_type(env, td)) + DECL_OO(const Type, t, = known_type_noref(env, td)) if(t->e->def && !GET_FLAG(t, scan1)) CHECK_BO(scan1_cdef(env, t->e->def)) if(t->size) @@ -66,8 +66,6 @@ ANN static Type scan1_exp_decl_type(const Env env, Exp_Decl* decl) { if(!GET_FLAG(decl->td, static)) SET_FLAG(decl->td, member); } -// if(GET_FLAG(t, abstract) && !GET_FLAG(decl->td, ref)) -// ERR_O(exp_self(decl)->pos, _("Type '%s' is abstract, declare as ref. (use @)"), t->name) if(GET_FLAG(t, private) && t->e->owner != env->curr) ERR_O(exp_self(decl)->pos, _("can't use private type %s"), t->name) if(GET_FLAG(t, protect) && (!env->class_def || isa(t, env->class_def) < 0)) @@ -94,8 +92,7 @@ ANN m_bool scan1_exp_decl(const Env env, const Exp_Decl* decl) { s_name(var->xid)) if(var->array) { if(var->array->exp) { -// if(GET_FLAG(decl->td, ref)) - if(GET_FLAG(decl->td, ref) && isa(t, t_object) < 0) + if(GET_FLAG(decl->td, ref)) ERR_B(td_pos(decl->td), _("ref array must not have array expression.\n" "e.g: int @my_array[];\nnot: int @my_array[2];")) CHECK_BB(scan1_exp(env, var->array->exp)) @@ -103,7 +100,7 @@ ANN m_bool scan1_exp_decl(const Env env, const Exp_Decl* decl) { t = array_type(env, decl->type, var->array->depth); } else if(GET_FLAG(t, abstract) && !GET_FLAG(decl->td, ref)) ERR_B(exp_self(decl)->pos, _("Type '%s' is abstract, declare as ref. (use @)"), t->name) - + CHECK_OB(prim_ref(env, t, decl->td)) //assert(!var->value); const Value v = var->value = former ?: new_value(env->gwion->mp, t, s_name(var->xid)); nspc_add_value(nspc, var->xid, v); @@ -242,8 +239,10 @@ ANN static m_bool scan1_args(const Env env, Arg_List list) { const Var_Decl var = list->var_decl; if(var->xid) CHECK_BB(isres(env, var->xid, var->pos)) - if(list->td) + if(list->td) { CHECK_OB((list->type = void_type(env, list->td))) + CHECK_OB(prim_ref(env, list->type, list->td)) + } } while((list = list->next)); return GW_OK; } diff --git a/src/parse/type_decl.c b/src/parse/type_decl.c index 649ad590..49efe0be 100644 --- a/src/parse/type_decl.c +++ b/src/parse/type_decl.c @@ -66,7 +66,7 @@ ANN static inline void* type_unknown(const Env env, const ID_List id) { return NULL; } -ANN static inline Type prim_ref(const Env env, const Type t, const Type_Decl* td) { +ANN Type prim_ref(const Env env, const Type t, const Type_Decl* td) { if(GET_FLAG(td, ref) && isa(t, t_object) < 0 && isa(t, t_class) < 0) ERR_O(td->xid->pos, _("primitive types cannot be used as reference (@)...\n")) return t; @@ -78,3 +78,9 @@ ANN Type known_type(const Env env, const Type_Decl* td) { const Type t = type_decl_resolve(env, td); return t ? prim_ref(env, t, td) : type_unknown(env, td->xid); } + +ANN Type known_type_noref(const Env env, const Type_Decl* td) { + if(!td->xid) + return t_undefined; + return type_decl_resolve(env, td) ?: type_unknown(env, td->xid); +} -- 2.43.0