refactor(argparse): 将null替换为nullptr以提高C++兼容性

- 在argparse库中将所有null指针常量替换为nullptr
- 更新头文件和源文件中的指针初始化和比较操作
- 修改测试文件中的相关断言检查
- 更新AST定义文件中的注释说明
This commit is contained in:
zzy
2026-04-05 20:18:09 +08:00
parent 27d86d5685
commit 4144f7841c
76 changed files with 1430 additions and 998 deletions

View File

@@ -130,7 +130,7 @@ static const scc_type_abi_t scc_win_x64_type_abi[] = {
.alignment = 8,
},
{
// NULL
// nullptr
.ast_type = SCC_AST_BUILTIN_TYPE_UNKNOWN,
.ir_type = SCC_IR_TYPE_unknown,
.size = 0,

View File

@@ -12,8 +12,8 @@ static inline void parse_lexme2const_int(const char *lexme,
scc_ir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
scc_ast_type_t *ast_type) {
if (ctx == null || ast_type == null) {
LOG_ERROR("args is null");
if (ctx == nullptr || ast_type == nullptr) {
LOG_ERROR("args is nullptr");
return 0;
}
scc_ir_type_t ir_type;
@@ -42,17 +42,13 @@ scc_ir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
}
break;
}
case SCC_AST_TYPE_POINTER: {
scc_ir_type_init(&ir_type, SCC_IR_TYPE_PTR);
scc_ir_type_ref_t pointee_type =
scc_ast2ir_type(ctx, ast_type->pointer.pointee);
ir_type.data.pointer.base = pointee_type;
break;
}
case SCC_AST_TYPE_ARRAY: {
scc_ir_type_init(&ir_type, SCC_IR_TYPE_ARRAY);
@@ -60,7 +56,6 @@ scc_ir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
scc_ast2ir_type(ctx, ast_type->array.element);
ir_type.data.array.base = element_type;
// TODO: 处理数组大小表达式
ir_type.data.array.len = 0;
if (ast_type->array.size) {
// TODO constant expression
@@ -73,7 +68,6 @@ scc_ir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
}
break;
}
case SCC_AST_TYPE_FUNCTION: {
scc_ir_type_init(&ir_type, SCC_IR_TYPE_FUNC);
@@ -98,13 +92,11 @@ scc_ir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
ir_type.data.function.params = params;
break;
}
// SCC_AST_TYPE_STRUCT, // 结构体类型
// SCC_AST_TYPE_UNION, // 联合类型
// SCC_AST_TYPE_ENUM, // 枚举类型
case SCC_AST_TYPE_TYPEDEF:
break;
default:
LOG_FATAL("Unsupported AST type: %d", ast_type->base.type);
return 0;
@@ -207,8 +199,8 @@ scc_ir_value_ref_t scc_ast2ir_logical_expr(scc_ast2ir_ctx_t *ctx,
*/
scc_ir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
cbool is_lvalue) {
if (ctx == null || expr == null) {
LOG_ERROR("args is null");
if (ctx == nullptr || expr == nullptr) {
LOG_ERROR("args is nullptr");
return 0;
}
@@ -332,7 +324,6 @@ scc_ir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
// 创建操作节点
return scc_ir_builder_binop(&ctx->builder, op, lhs, rhs);
}
case SCC_AST_EXPR_UNARY: {
if (expr->unary.op == SCC_AST_OP_ADDRESS_OF) {
return scc_ast2ir_expr(ctx, expr->unary.operand, true);
@@ -399,13 +390,13 @@ scc_ir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
LOG_FATAL("Unsupported unary operator: %d", expr->unary.op);
return 0;
}
UNREACHABLE();
break;
}
case SCC_AST_EXPR_COND: {
TODO();
break;
}
case SCC_AST_EXPR_CALL: {
// 转换参数
scc_ir_node_ref_vec_t args;
@@ -430,7 +421,6 @@ scc_ir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
scc_vec_free(args);
return node;
}
// SCC_AST_EXPR_ARRAY_SUBSCRIPT, // 数组下标
// SCC_AST_EXPR_MEMBER, // 成员访问 .
// SCC_AST_EXPR_PTR_MEMBER, // 指针成员访问 ->
@@ -440,7 +430,6 @@ scc_ir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
// SCC_AST_EXPR_COMPOUND, // 复合字面量
// SCC_AST_EXPR_LVALUE, // 右值
// SCC_AST_EXPR_BUILTIN,// 内置表达式 ... directive map to ir builtin
case SCC_AST_EXPR_INT_LITERAL: {
// FIXME maybe using some array to int;
scc_ir_type_ref_t type_ref = scc_ir_builder_type_i32(&ctx->builder);
@@ -487,9 +476,8 @@ scc_ir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
TODO();
break;
}
case SCC_AST_EXPR_IDENTIFIER: {
if (expr->identifier._target == null) {
if (expr->identifier._target == nullptr) {
LOG_ERROR("unknown identifier %s", expr->identifier.name);
}
// FIXME hack hashtable
@@ -499,7 +487,20 @@ scc_ir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
if (is_lvalue) {
return in;
} else {
return scc_ir_builder_load(&ctx->builder, in);
// 右值:如果是数组类型,退化为指针(返回地址)
scc_ir_type_t *ir_type =
scc_ir_module_get_type_by_value(ctx->builder.ctx.module, in);
Assert(ir_type->tag == SCC_IR_TYPE_PTR);
scc_ir_type_t *target_type = scc_ir_module_get_type(
ctx->builder.ctx.module, ir_type->data.pointer.base);
if (target_type->tag == SCC_IR_TYPE_ARRAY) {
// 生成 getptr 获取数组首地址
return scc_ir_builder_get_ptr(&ctx->builder, in,
SCC_IR_REF_nullptr);
} else {
// 标量类型:加载值
return scc_ir_builder_load(&ctx->builder, in);
}
}
}
@@ -516,7 +517,7 @@ scc_ir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
* @param stmt
*/
void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt) {
if (stmt == null) {
if (stmt == nullptr) {
return;
}
@@ -537,12 +538,10 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt) {
}
break;
}
case SCC_AST_STMT_EXPR: {
scc_ast2ir_expr(ctx, stmt->expr.expr, false);
break;
}
case SCC_AST_STMT_IF: {
/*
branch cond
@@ -580,7 +579,6 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt) {
scc_ir_builder_set_current_bblock(&ctx->builder, merge_block);
break;
}
case SCC_AST_STMT_WHILE: {
scc_ir_bblock_ref_t cond_block =
scc_ir_builder_bblock(&ctx->builder, "while_cond");
@@ -603,7 +601,6 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt) {
scc_ir_builder_set_current_bblock(&ctx->builder, exit_block);
break;
}
case SCC_AST_STMT_DO_WHILE: {
scc_ir_bblock_ref_t cond_block =
scc_ir_builder_bblock(&ctx->builder, "do_while_cond");
@@ -626,7 +623,6 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt) {
scc_ir_builder_set_current_bblock(&ctx->builder, exit_block);
break;
}
case SCC_AST_STMT_FOR: {
scc_ir_bblock_ref_t cond_block =
scc_ir_builder_bblock(&ctx->builder, "for_while_cond");
@@ -670,7 +666,6 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt) {
scc_ir_builder_set_current_bblock(&ctx->builder, exit_block);
break;
}
// SCC_AST_STMT_SWITCH, // switch 语句
// SCC_AST_STMT_CASE, // case 语句
// SCC_AST_STMT_DEFAULT, // default 语句
@@ -701,7 +696,7 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt) {
* @param decl
*/
void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl) {
if (ctx == null || decl == null) {
if (ctx == nullptr || decl == nullptr) {
LOG_ERROR("Invalid argument");
return;
}
@@ -709,11 +704,10 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl) {
switch (decl->base.type) {
case SCC_AST_DECL_VAR: {
// 转换类型
scc_ir_type_ref_t ir_type = scc_ast2ir_type(ctx, decl->var.type);
scc_ir_type_ref_t ir_type_ref = scc_ast2ir_type(ctx, decl->var.type);
// 创建分配节点
scc_ir_value_ref_t alloc_val_node =
scc_ir_builder_alloca(&ctx->builder, ir_type, decl->name);
scc_ir_builder_alloca(&ctx->builder, ir_type_ref, decl->name);
scc_hashtable_set(&ctx->decl2ir_ref, decl,
(void *)(usize)alloc_val_node);
@@ -724,10 +718,30 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl) {
}
scc_ir_value_ref_t init_val_node =
scc_ast2ir_expr(ctx, decl->var.init, false);
// FIXME array auto calucate size
scc_ir_type_t *ir_type =
scc_ir_module_get_type(ctx->builder.ctx.module, ir_type_ref);
if (ir_type->tag == SCC_IR_TYPE_ARRAY && ir_type->data.array.len == 0) {
scc_ast_expr_t *init_expr = decl->var.init;
if (init_expr->base.type == SCC_AST_EXPR_COMPOUND) {
Panic("TODO: init_expr->base.type == SCC_AST_EXPR_COMPOUND");
} else if (init_expr->base.type == SCC_AST_EXPR_STRING_LITERAL) {
ir_type->data.array.len =
scc_strlen(init_expr->literal.lexme) + 1;
scc_ir_const_int_t len = {.int64 = ir_type->data.array.len};
scc_ir_value_ref_t len_ref = scc_ir_builder_const_int(
&ctx->builder, scc_ir_builder_type_u64(&ctx->builder), len);
scc_ir_builder_builtin_memcpy(&ctx->builder, alloc_val_node,
init_val_node, len_ref);
} else {
Panic("unknown init expr in array decl");
}
}
scc_ir_builder_store(&ctx->builder, alloc_val_node, init_val_node);
break;
}
case SCC_AST_DECL_FUNC: {
scc_ir_type_ref_t func_type_ref = scc_ast2ir_type(ctx, decl->func.type);
scc_ir_func_ref_t func_ref =
@@ -739,15 +753,15 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl) {
(void *)(usize)func_ref);
}
if (decl->func.body == null) {
if (decl->func.body == nullptr) {
// function decl
break;
}
scc_ir_builder_begin_func(&ctx->builder, func_ref, null);
scc_ir_builder_begin_func(&ctx->builder, func_ref, nullptr);
scc_ir_func_t *func =
scc_ir_module_get_func(ctx->builder.ctx.module, func_ref);
Assert(func != null);
Assert(func != nullptr);
scc_ir_builder_begin_bblock(&ctx->builder, "entry");
scc_vec_foreach(decl->func.type->function.params, i) {
scc_ast_decl_t *param =
@@ -756,7 +770,7 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl) {
scc_ir_value_ref_t param_node_ref = scc_vec_at(func->params, i);
scc_ir_value_t *param_node = scc_ir_module_get_value(
ctx->builder.ctx.module, param_node_ref);
Assert(param_node != null);
Assert(param_node != nullptr);
param_node->name = param->name;
scc_hashtable_set(&ctx->decl2ir_ref, param,
(void *)(usize)param_node_ref);
@@ -769,7 +783,6 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl) {
scc_ir_builder_end_func(&ctx->builder);
break;
}
case SCC_AST_DECL_LIST: {
scc_vec_foreach(decl->list.vars, i) {
scc_ast_decl_t *sub_decl = scc_vec_at(decl->list.vars, i);
@@ -784,11 +797,9 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl) {
break;
case SCC_AST_DECL_UNION:
break;
case SCC_AST_DECL_ENUM:
case SCC_AST_DECL_TYPEDEF:
break;
default:
LOG_FATAL("Unsupported declaration type: %d", decl->base.type);
break;
@@ -797,7 +808,7 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl) {
void scc_ast2ir_translation_unit(scc_ast2ir_ctx_t *ctx,
scc_ast_translation_unit_t *tu) {
Assert(ctx != null && tu != null);
Assert(ctx != nullptr && tu != nullptr);
scc_vec_foreach(tu->declarations, i) {
scc_ast_decl_t *decl = scc_vec_at(tu->declarations, i);
@@ -812,7 +823,7 @@ static int scc_cmp_node(const void *key1, const void *key2) {
void scc_ast2ir_ctx_init(scc_ast2ir_ctx_t *ctx, const scc_type_abi_t *abi,
scc_ir_cprog_t *cprog) {
Assert(ctx != null);
Assert(ctx != nullptr);
ctx->abi = abi;
scc_ir_builder_init(&ctx->builder, cprog);
scc_hashtable_init(&ctx->decl2ir_ref, scc_hash_node, scc_cmp_node);