Skip to content

Commit

Permalink
🔥 move prettyerr, update errors (mac fix?)
Browse files Browse the repository at this point in the history
  • Loading branch information
fennecdjay committed Mar 23, 2024
1 parent c23fff6 commit b1289e8
Show file tree
Hide file tree
Showing 26 changed files with 631 additions and 107 deletions.
8 changes: 2 additions & 6 deletions Makefile
Expand Up @@ -24,7 +24,7 @@ src := $(wildcard src/*.c)
src += $(wildcard src/*/*.c)

CFLAGS += -Iutil/include -Iast/include -D_GNU_SOURCE
CFLAGS += -Iast/libprettyerr/src -Ifmt/include
CFLAGS += -Ifmt/include

# add commit hash to version for now
CFLAGS += -DGWION_VERSION="\"$(shell git log -n1 --format="%h%m%cs")\""
Expand Down Expand Up @@ -64,7 +64,7 @@ include embed/embed.mk
endif

ALMOST_LIBS := fmt/libgwion_fmt.a
ALMOST_LIBS += ast/libgwion_ast.a ast/libprettyerr/libprettyerr.a
ALMOST_LIBS += ast/libgwion_ast.a
ALMOST_LIBS += util/libgwion_util.a
GWLIBS := lib${PRG}.a ${ALMOST_LIBS}
LDFLAGS := ${GWLIBS} ${LDFLAGS}
Expand Down Expand Up @@ -114,9 +114,6 @@ ast/libgwion_ast.a: util/libgwion_util.a
fmt/libgwion_fmt.a: ast/libgwion_ast.a
@+${MAKE} -s -C fmt libgwion_fmt.a

ast/libprettyerr/libprettyerr.a: options-show
@+CFLAGS=-I$(shell pwd)/util/include ${MAKE} -s -C ast/libprettyerr static

ast: ast/libgwion_ast.a
@(info build ast)

Expand All @@ -133,7 +130,6 @@ clean_core:
clean-all: clean
${MAKE} -s -C fmt clean
${MAKE} -s -C ast clean
${MAKE} -s -C ast/libprettyerr clean
${MAKE} -s -C util clean

update: clean-all
Expand Down
2 changes: 1 addition & 1 deletion ast
2 changes: 2 additions & 0 deletions compile_flags.txt
@@ -1,3 +1,5 @@
-Wall
-Wextra
-Iutil/include
-Iast/include
-Iast/libprettyerr/src
Expand Down
2 changes: 2 additions & 0 deletions include/env/func.h
Expand Up @@ -50,4 +50,6 @@ ANN static inline m_uint captures_sz(const Capture_List captures) {
ANN static inline bool is_ctor(const Func_Def fdef) {
return !strcmp(s_name(fdef->base->tag.sym), "@ctor");
}

ANN void print_signature(const Gwion gwion, const Func f);
#endif
1 change: 1 addition & 0 deletions include/env/type.h
Expand Up @@ -82,6 +82,7 @@ ANN Type actual_type(const struct Gwion_ *gwion, const Type t);
ANN static inline m_uint env_push_type(const Env env, const Type type) {
return env_push(env, type, type->nspc);
}
ANN bool is_object(const struct Gwion_ *, const Type t);
ANN bool is_func(const struct Gwion_ *, const Type t);
ANN bool is_class(const struct Gwion_ *, const Type t);
ANN __attribute__((returns_nonnull)) static inline Type _class_base(Type t) {
Expand Down
1 change: 1 addition & 0 deletions include/gwion_env.h
Expand Up @@ -39,5 +39,6 @@ ANN static inline void defined_here(const Value v) {
}


#define is_new(a) !strcmp(s_name((a)->base->tag.sym), "new")
#endif

5 changes: 0 additions & 5 deletions include/operator.h
Expand Up @@ -87,11 +87,6 @@ ANN static inline void operator_resume(struct Op_Import *opi) {
*(uintptr_t *)opi->ret = opi->data;
}

ANN static inline void set_decl_ref(Exp* e) {
if (e->exp_type == ae_exp_decl)
SET_FLAG(e->d.exp_decl.var.vd.value, late);
}

ANN void func_operator(const Func_Def fdef, struct Op_Import *opi);
ANN bool add_op_func_check(const Env env, const Type t, const struct Op_Func *opfunc, const m_uint idx);
#endif
1 change: 0 additions & 1 deletion include/parse.h
Expand Up @@ -171,5 +171,4 @@ ANN static inline bool is_static_call(const Gwion gwion, Exp* e) {
member->base->exp_type == ae_exp_cast;
}

#define is_new(a) !strcmp(s_name((a)->base->tag.sym), "new")
#endif
1 change: 0 additions & 1 deletion include/partial.h
Expand Up @@ -2,7 +2,6 @@
#define __PARTIAL_APPLICATION

ANN Type partial_type(const Env, Exp_Call *const);
ANN void print_signature(const Func f);

ANN static inline bool func_match_inner(const Env env, Exp* e,
const Type t, const bool implicit,
Expand Down
4 changes: 4 additions & 0 deletions include/specialid.h
@@ -1,6 +1,9 @@
#ifndef __SPECIALID
#define __SPECIALID

ANN struct SpecialId_ *specialid_get(const Gwion, const Symbol);
#ifndef __NO_EMIT

typedef Type (*idck)(const Env, const Exp_Primary *);
typedef bool (*idem)(const Emitter, const Exp_Primary *);

Expand All @@ -26,3 +29,4 @@ ANN static inline Type specialid_type(const Env env, struct SpecialId_ *spid,

ANN struct SpecialId_ *specialid_get(const Gwion, const Symbol);
#endif
#endif
10 changes: 10 additions & 0 deletions include/validate.h
@@ -0,0 +1,10 @@
#pragma once

ANN bool exp_validation(Env env, Exp* b);
ANN bool func_def_validation(Env env, Func_Def b);
ANN bool union_def_validation(Env env, Union_Def b);
ANN bool enum_def_validation(Env env, Enum_Def b);
ANN bool fptr_def_validation(Env env, Fptr_Def b);
ANN bool type_def_validation(Env env, Type_Def b);
ANN bool class_def_validation(Env env, Class_Def b);
ANN bool ast_validation(Env env, Ast *ast);
66 changes: 15 additions & 51 deletions src/emit/emit.c
Expand Up @@ -452,8 +452,7 @@ ANN static bool emit_symbol_builtin(const Emitter emit, const Symbol *data) {
const m_uint size = v->type->size;
emit_dotstatic(emit, (m_uint)&v->d.ptr, size, emit_addr);
// prevent invalid access to global variables
if(!emit_addr &&
isa(v->type, emit->gwion->type[et_object]))
if(!emit_addr && is_object(emit->gwion, v->type))
emit_fast_except(emit, v->from, prim_pos(data));
} else {
const m_uint size = v->type->size;
Expand All @@ -464,7 +463,7 @@ ANN static bool emit_symbol_builtin(const Emitter emit, const Symbol *data) {
return true;
}

ANN static bool _emit_symbol(const Emitter emit, const Symbol *data) {
ANN static bool emit_symbol(const Emitter emit, const Symbol *data) {
const Value v = prim_self(data)->value;
if (is_class(emit->gwion, v->type)) {
emit_pushimm(emit, (m_uint)actual_type(emit->gwion, v->type));
Expand Down Expand Up @@ -502,17 +501,11 @@ ANN static bool _emit_symbol(const Emitter emit, const Symbol *data) {
else
emit_regpushbase(emit, v->from->offset, size, exp_getvar(prim_exp(data)));
if (GET_FLAG(v, late) && !exp_getvar(prim_exp(data)) &&
isa(v->type, emit->gwion->type[et_object]))
is_object(emit->gwion, v->type))
emit_fast_except(emit, v->from, prim_pos(data));
return true;
}

ANN static bool emit_symbol(const Emitter emit, const Exp_Primary *prim) {
if (!prim->value) // assume it's an operator
ERR_B(exp_self(prim)->loc, "missing value for operator");
return _emit_symbol(emit, &prim->d.var);
}

ANN VM_Code finalyze(const Emitter emit, const f_instr exec) {
emit_add_instr(emit, exec);
const VM_Code code = emit->info->emit_code(emit);
Expand Down Expand Up @@ -725,7 +718,7 @@ ANN static bool emit_prim_id(const Emitter emit, const Symbol *data) {
struct SpecialId_ *spid = specialid_get(emit->gwion, *data);
if (unlikely(spid))
return specialid_instr(emit, spid, prim_self(data));
return emit_symbol(emit, prim_self(data));
return emit_symbol(emit, data);
}

ANN static bool emit_prim_perform(const Emitter emit, const Symbol *xid) {
Expand Down Expand Up @@ -783,7 +776,7 @@ ANN static void emit_gack_type(const Emitter emit, Exp* e) {
if (e->exp_type == ae_exp_cast ||
(e->exp_type == ae_exp_primary && e->d.prim.prim_type == ae_prim_str))
return;
const bool isobj = isa(e->type, emit->gwion->type[et_object]);
const bool isobj = is_object(emit->gwion, e->type);
if (isobj && (tflag(e->type, tflag_ref) || !GET_FLAG(e->type, final)))
emit_add_instr(emit, GackType);
}
Expand Down Expand Up @@ -943,11 +936,12 @@ ANN static bool emit_exp_decl_static(const Emitter emit, const Exp_Decl *decl,
const bool is_ref,
const bool emit_addr) {
const Value v = var_decl->value;
if (isa(v->type, emit->gwion->type[et_object]) && !is_ref)
bool isobj = is_object(emit->gwion, v->type);
if (isobj && !is_ref)
CHECK_B(decl_static(emit, decl, var_decl, 0));
CHECK_B(emit_dot_static_data(emit, v, !struct_ctor(v) ? emit_addr : 1));
if (tflag(v->type, tflag_struct)) CHECK_B(struct_finish(emit, decl));
if (isa(v->type, emit->gwion->type[et_object]) && !is_ref) {
if (isobj && !is_ref) {
if(safe_tflag(emit->env->class_def, tflag_struct) && GET_FLAG(emit->env->class_def, global))
emit_object_addref(emit, 0, emit_addr);
}
Expand Down Expand Up @@ -1009,7 +1003,7 @@ ANN static bool emit_exp_decl_non_static(const Emitter emit,
const uint emit_var) {
const Value v = var_decl->value;
const Type type = v->type;
const bool is_obj = isa(type, emit->gwion->type[et_object]);
const bool is_obj = is_object(emit->gwion, type);
const bool emit_addr = (!is_obj || is_ref) ? emit_var : true;
if (is_obj && !is_ref && !exp_self(decl)->ref) {
if(!decl->args) CHECK_B(emit_instantiate_decl(emit, type, decl->var.td, is_ref));
Expand Down Expand Up @@ -1052,7 +1046,7 @@ ANN static bool emit_exp_decl_global(const Emitter emit, const Exp_Decl *decl,
const uint is_ref, const bool emit_var) {
const Value v = var_decl->value;
const Type type = v->type;
const bool is_obj = isa(type, emit->gwion->type[et_object]);
const bool is_obj = is_object(emit->gwion, type);
const bool emit_addr = (!is_obj || is_ref) ? emit_var : true;
if (is_obj && !is_ref) {
if(!decl->args) CHECK_B(emit_instantiate_decl(emit, type, decl->var.td, is_ref));
Expand All @@ -1074,18 +1068,6 @@ ANN static bool emit_exp_decl_global(const Emitter emit, const Exp_Decl *decl,
return true;
}

ANN static void set_late(const Exp_Decl *decl, const Var_Decl *var) {
const Value v = var->value;
if (!exp_getvar(exp_self(decl)) &&
(GET_FLAG(array_base_simple(v->type), abstract) || GET_FLAG(decl->var.td, late)))
SET_FLAG(v, late);
else UNSET_FLAG(v, late);
}

static inline bool late_array(const Type_Decl* td) {
return !td->array || !td->array->exp;
}

ANN static bool emit_decl(const Emitter emit, Exp_Decl *const decl) {
const bool global = GET_FLAG(decl->var.td, global);
const uint var = exp_getvar(exp_self(decl));
Expand All @@ -1106,26 +1088,8 @@ ANN static bool emit_decl(const Emitter emit, Exp_Decl *const decl) {
.op = insert_symbol("@implicit"),
.rhs = t};
CHECK_B(op_emit(emit, &opi));
}
set_late(decl, vd);
if (!decl->args && !exp_getvar(exp_self(decl)) && GET_FLAG(array_base_simple(v->type), abstract) && !GET_FLAG(decl->var.td, late) &&
GET_FLAG(v, late) && late_array(decl->var.td)
&& GET_FLAG(v->type, abstract)) {
env_err(emit->env, decl->var.td->tag.loc, _("Type '%s' is abstract, use {+G}late{0} instead of {G+}%s{0}"),
v->type->name, !GET_FLAG(decl->var.td, const) ? "var" : "const");
if(v->type->nspc->vtable.ptr) {
const Vector vec = &v->type->nspc->vtable;
for(m_uint i = 0; i < vector_size(vec); i++) {
const Func f = (Func)vector_at(vec, i);
if(is_new(f->def)) {
gw_err(_("maybe use a constructor?\n"));
break;
}
}
}
return false;
}
if(isa(v->type, emit->gwion->type[et_object]) && GET_FLAG(v, late) && exp_getuse(exp_self(decl)))
}
if(is_object(emit->gwion, v->type) /*&& GET_FLAG(v, late) */&& exp_getuse(exp_self(decl)) && !exp_getvar(exp_self(decl)))
emit_add_instr(emit, GWOP_EXCEPT);
return true;
}
Expand All @@ -1140,7 +1104,7 @@ ANN /*static */ bool emit_exp_decl(const Emitter emit, Exp_Decl *const decl) {
!global ? emit->env->scope->depth : emit_push_global(emit);
const bool ret = emit_decl(emit, decl);
if (global) emit_pop(emit, scope);
if(emit->status.in_return && GET_FLAG(decl->var.vd.value, late) && isa(t, emit->gwion->type[et_object]))
if(emit->status.in_return && GET_FLAG(decl->var.vd.value, late) && is_object(emit->gwion, t))
emit_add_instr(emit, GWOP_EXCEPT);
return ret;
}
Expand Down Expand Up @@ -1957,8 +1921,8 @@ ANN2(1) /*static */ bool emit_exp(const Emitter emit, /* const */ Exp* e) {
}
CHECK_B(emit_exp_func[exp->exp_type](emit, &exp->d));
if (exp->cast_to) CHECK_B(emit_implicit_cast(emit, exp, exp->cast_to));
if (isa(e->type, emit->gwion->type[et_object]) &&
(e->cast_to ? isa(e->cast_to, emit->gwion->type[et_object]) : 1) &&
if (is_object(emit->gwion, exp->type) &&
(e->cast_to ? is_object(emit->gwion, e->cast_to) : true) &&
e->exp_type == ae_exp_decl && GET_FLAG(e->d.exp_decl.var.td, late) &&
exp_getuse(e) && !exp_getvar(e) &&
GET_FLAG(e->d.exp_decl.var.vd.value, late))
Expand Down
13 changes: 12 additions & 1 deletion src/env/func.c
@@ -1,6 +1,7 @@
#include "gwion_util.h"
#include "gwion_ast.h"
#include "gwion_env.h"
#include "gwfmt.h"
#include "vm.h"
#include "gwion.h"
#include "clean.h"
Expand Down Expand Up @@ -54,7 +55,17 @@ ANN void builtin_func(const Gwion gwion, const Func f, void *func_ptr) {
if(likely(arg.type == tmplarg_td))
mp_vector_set(f->code->types, Type, n++, known_type(gwion->env, arg.d.td));
}
// f->code->types->len = n;
}
}
}

ANN void print_signature(const Gwion gwion, const Func f) {
struct GwfmtState ls = {.minimize=true, .ppa = gwion->ppa};
text_init(&ls.text, gwion->mp);
Gwfmt l = {.mp = gwion->mp, .st = gwion->st, .ls = &ls, .line = 1, .last = cht_nl };
gwfmt_func_def(&l, f->def);
gwlog_related_from(ls.text.str, f->value_ref->from);
text_release(&ls.text);
}


4 changes: 4 additions & 0 deletions src/env/type.c
Expand Up @@ -139,6 +139,10 @@ ANN bool is_func(const struct Gwion_ *gwion, const Type t) {
return isa(actual_type(gwion, t), gwion->type[et_function]);
}

ANN bool is_object(const struct Gwion_ *gwion, const Type t) {
return isa(t, gwion->type[et_object]);
}

ANN inline bool is_class(const struct Gwion_ *gwion, const Type t) {
// return isa(t, gwion->type[et_class]);
return t->info->parent == gwion->type[et_class];
Expand Down
3 changes: 2 additions & 1 deletion src/import/import_cdef.c
Expand Up @@ -26,7 +26,8 @@ ANN static void mk_dtor(MemPool p, const Type t, const m_uint d) {
ANN2(1, 2) static void import_class_ini(const Env env, const Type t) {
t->nspc = new_nspc(env->gwion->mp, t->name);
t->nspc->parent = env->curr;
if (isa(t, env->gwion->type[et_object])) inherit(t);
if (is_object(env->gwion, t))
inherit(t);
env_push_type(env, t);
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/array.c
Expand Up @@ -403,7 +403,7 @@ ANN static inline bool array_do(const Emitter emit, const Array_Sub array,
access = emit_add_instr(emit, ArrayAccess);
access->m_val = (i+1) * SZ_INT - offset;
access->udata.one = offset;
if(i < get_depth(t) || isa(array_base(t), emit->gwion->type[et_object])) {
if(i < get_depth(t) || is_object(emit->gwion, array_base(t))) {
const Instr ex = emit_add_instr(emit, GWOP_EXCEPT);
ex->m_val = -SZ_INT;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/object_op.c
Expand Up @@ -264,7 +264,7 @@ OP_EMIT(opem_object_dot) {
assert(GET_FLAG(value, static));
emit_dot_static_import_data(emit, value, exp_getvar(exp_self(member)));
}
if(isa(value->type, emit->gwion->type[et_object]) &&
if(is_object(emit->gwion, value->type) &&
!exp_getvar(exp_self(member)) &&
(GET_FLAG(value, static) || GET_FLAG(value, late)))
emit_fast_except(emit, value->from, exp_self(member)->loc);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/opfunc.c
Expand Up @@ -152,7 +152,7 @@ OP_CHECK(opck_new) {
(!array || (array->exp && exp_is_zero(array->exp))))
ERR_N(unary->ctor.td->tag.loc, _("can't use 'new' on abstract type '%s'\n"),
t->name);
if (!isa(t, env->gwion->type[et_object]))
if (!is_object(env->gwion, t))
ERR_N(exp_self(unary)->loc, _("can't use 'new' on non-object types...\n"));
return t;
}
Expand Down
8 changes: 4 additions & 4 deletions src/parse/check.c
Expand Up @@ -189,7 +189,7 @@ ANN Type check_exp_decl(const Env env, Exp_Decl *const decl) {
env_pop(env, scope);
set_vflag(decl->var.vd.value, vflag_direct);
}
env_weight(env, 1 + isa(decl->type, env->gwion->type[et_object]));
env_weight(env, 1 + is_object(env->gwion, decl->type));
return ret ? decl->var.vd.value->type : NULL;
}

Expand Down Expand Up @@ -709,9 +709,9 @@ static void function_alternative(const Env env, const Type t, Exp* args,
gwlog_error("Argument type mismatch", "call site",
env->name, loc, 0);
// TODO: hint valid alternatives
do print_signature(f);
do print_signature(env->gwion, f);
while ((f = f->next));
gw_err(_("and not\n "));
gw_err(_("and not:\n "));
if (args)
print_current_args(args);
else
Expand Down Expand Up @@ -1144,7 +1144,7 @@ ANN static Type check_exp_post(const Env env, const Exp_Postfix *post) {
CHECK_O(opi.lhs);
exp_setuse(post->exp, 1);
const Type t = op_check(env, &opi);
if (t && !isa(t, env->gwion->type[et_object]))
if (t && !is_object(env->gwion, t))
exp_setmeta(exp_self(post), 1);
return t;
}
Expand Down

0 comments on commit b1289e8

Please sign in to comment.