stage1 整理代码 提供更多错误信息

This commit is contained in:
zzy
2026-07-21 09:56:19 +08:00
parent 459b6188a9
commit 7c5bb41e09
8 changed files with 129 additions and 72 deletions

View File

@@ -618,9 +618,9 @@ static void spl_check_arg_type(spl_comp_t *ctx, const char *fname, int arg_type_
strcmp(spl_type_name(&ctx->tctx, arg_type_idx),
spl_type_name(&ctx->tctx, spl_type_elem_type(&ctx->tctx, param_type_idx))) == 0) {
fprintf(stderr,
"%s: warning: argument %d of '%s' expects '%s*', "
"%s warning: argument %d of '%s' expects '%s*', "
"got '%s' (missing '&'?)\n",
ctx->fname, arg_idx + 1, fname,
spl_tok_loc_str(ctx), arg_idx + 1, fname,
spl_type_name(&ctx->tctx, spl_type_elem_type(&ctx->tctx, param_type_idx)),
spl_type_name(&ctx->tctx, arg_type_idx));
}
@@ -781,6 +781,7 @@ static spl_expr_result_t parse_prefix_op(spl_comp_t *ctx) {
if (!right.is_lvalue) {
spl_comp_error(ctx, "cannot take address of rvalue");
}
right.type_idx = spl_type_ptr(&ctx->tctx, right.type_idx);
break;
case TOK_MUL:
if (right.type_idx >= 0 && spl_type_kind(&ctx->tctx, right.type_idx) == TYPE_PTR &&
@@ -871,6 +872,18 @@ static spl_expr_result_t parse_primary_expr(spl_comp_t *ctx) {
emit_dbg_void(&ctx->emit);
}
return (spl_expr_result_t){spl_type_basic(&ctx->tctx, SPL_VOID), 0};
} else if (strcmp(bname, "sizeof") == 0) {
expect(ctx, TOK_L_PAREN);
int ti = spl_type_parse(&ctx->tctx, ctx);
expect(ctx, TOK_R_PAREN);
if (ti < 0) {
spl_comp_error(ctx, "@sizeof: invalid type");
spl_expr_result_t r = { -1, 0 };
return r;
}
usize sz = spl_type_size(&ctx->tctx, ti);
emit_push_usize(&ctx->emit, sz);
return (spl_expr_result_t){spl_type_basic(&ctx->tctx, SPL_USIZE), 0};
} else {
spl_comp_error(ctx, "unknown builtin '@%s'", bname);
spl_expr_result_t r = {-1, 0};
@@ -935,7 +948,8 @@ static spl_expr_result_t parse_postfix_expr(spl_comp_t *ctx, spl_expr_result_t l
int nargs = 0;
int is_instance = 0;
if (func->nparams > 0 && func->param_type_indices[0] >= 0 &&
if (func->nparams > 0 && func->param_type_indices &&
func->param_type_indices[0] >= 0 &&
spl_type_kind(&ctx->tctx, func->param_type_indices[0]) == TYPE_PTR &&
spl_type_elem_type(&ctx->tctx, func->param_type_indices[0]) ==
methods_type_idx) {
@@ -954,10 +968,6 @@ static spl_expr_result_t parse_postfix_expr(spl_comp_t *ctx, spl_expr_result_t l
{
int *cp = func->param_type_indices;
int cn = func->nparams;
if (is_instance && cn > 0) {
cp++;
cn--;
}
nargs += parse_call_args_checked(ctx, func->name, cp, cn);
}
expect(ctx, TOK_R_PAREN);