feat(ast): 添加内置表达式支持并完善解析器功能
- 添加SCC_AST_EXPR_BUILTIN枚举值用于表示内置表达式 - 定义scc_ast_builtin_expr_type_t枚举包含va_start、va_end、va_copy、va_arg等内置函数类型 - 在AST表达式结构中添加builtin字段以支持内置表达式存储 - 实现scc_ast_decl_unsafe_val_init内联函数用于不安全的声明初始化 - 修改sizeof和alignof表达式初始化函数以支持类型或表达式参数 - 修复默认语句的字段引用错误(default_stmt而非case_stmt) - 改进词法分析器对整数字面量后缀(U、L、LL等)的处理逻辑 - 重构解析器中的声明解析逻辑,统一使用scc_parse_declarator函数 - 完善结构体、联合体和枚举类型的声明解析,支持仅有名称的前向声明 - 优化初始化列表解析,支持复合字面量的成员访问语法 - 更新参数类型列表解析以正确处理参数声明 - 修复括号表达式的解析逻辑,区分类型转换和普通括号表达式
This commit is contained in:
@@ -597,6 +597,53 @@ static void test_parser_unit(void) {
|
||||
SCC_CHECK_AST(&stmt.base,
|
||||
"return (void){.data = (void *)0, .size = 0, .cap = 0};",
|
||||
scc_parse_statement);
|
||||
|
||||
scc_ast_expr_t lhs4;
|
||||
scc_ast_expr_t lhs5;
|
||||
scc_ast_expr_member_init(&lhs4, &lvalue, "a");
|
||||
scc_ast_expr_member_init(&lhs5, &lhs4, "b");
|
||||
scc_ast_expr_t lhs6;
|
||||
scc_ast_expr_t lhs7;
|
||||
scc_ast_expr_member_init(&lhs6, &lvalue, "c");
|
||||
scc_ast_expr_array_subscript_init(&lhs7, &lhs6, &rl0);
|
||||
|
||||
scc_ast_expr_t *lhs_array_hard[] = {&lhs5, &lhs7};
|
||||
scc_vec_unsafe_from_array(lhs_exprs, lhs_array_hard);
|
||||
scc_ast_expr_t *rhs_array_hard[] = {&rhs2, &rhs3};
|
||||
scc_vec_unsafe_from_array(rhs_exprs, rhs_array_hard);
|
||||
scc_ast_expr_compound_init(&expr, &lvalue, &lhs_exprs, &rhs_exprs);
|
||||
SCC_CHECK_AST(&expr.base, "(void){.a.b = 0, .c[0] = 0}",
|
||||
scc_parse_expression);
|
||||
}
|
||||
|
||||
{
|
||||
// 测试 struct S; 仅标记声明
|
||||
{
|
||||
scc_ast_decl_t struct_decl;
|
||||
scc_ast_decl_vec_t empty_members;
|
||||
scc_vec_init(empty_members);
|
||||
scc_ast_decl_struct_init(&struct_decl, "S", &empty_members);
|
||||
SCC_CHECK_AST(&struct_decl.base, "struct S;",
|
||||
scc_parse_declaration);
|
||||
}
|
||||
|
||||
// 测试 union U; 仅标记声明
|
||||
{
|
||||
scc_ast_decl_t union_decl;
|
||||
scc_ast_decl_vec_t empty_members;
|
||||
scc_vec_init(empty_members);
|
||||
scc_ast_decl_union_init(&union_decl, "U", &empty_members);
|
||||
SCC_CHECK_AST(&union_decl.base, "union U;", scc_parse_declaration);
|
||||
}
|
||||
|
||||
// 测试 enum E; 仅标记声明
|
||||
{
|
||||
scc_ast_decl_t enum_decl;
|
||||
scc_ast_expr_vec_t empty_enumerators;
|
||||
scc_vec_init(empty_enumerators);
|
||||
scc_ast_decl_enum_init(&enum_decl, "E", &empty_enumerators);
|
||||
SCC_CHECK_AST(&enum_decl.base, "enum E;", scc_parse_declaration);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
@@ -611,6 +658,88 @@ static void test_parser_unit(void) {
|
||||
SCC_CHECK_AST(&str.base, "\"a\" \"b\"", scc_parse_expression);
|
||||
}
|
||||
|
||||
{
|
||||
// 测试 int a = *(int*)b;
|
||||
{
|
||||
// 构造类型 int*
|
||||
scc_ast_type_t ptr_to_int;
|
||||
scc_ast_type_pointer_init(
|
||||
&ptr_to_int, (scc_ast_type_t *)&scc_ast_builtin_type_int);
|
||||
|
||||
// 标识符 b
|
||||
scc_ast_expr_t b_expr;
|
||||
scc_ast_expr_identifier_init(&b_expr, "b");
|
||||
|
||||
// 类型转换 (int*)b
|
||||
scc_ast_expr_t cast_expr;
|
||||
scc_ast_expr_cast_init(&cast_expr, &ptr_to_int, &b_expr);
|
||||
|
||||
// 解引用 *(int*)b
|
||||
scc_ast_expr_t deref_expr;
|
||||
scc_ast_expr_unary_init(&deref_expr, SCC_AST_OP_INDIRECTION,
|
||||
&cast_expr);
|
||||
|
||||
// 声明 int a = *(int*)b;
|
||||
scc_ast_decl_t decl;
|
||||
scc_ast_decl_val_init(&decl,
|
||||
(scc_ast_type_t *)&scc_ast_builtin_type_int,
|
||||
"a", &deref_expr);
|
||||
|
||||
SCC_CHECK_AST(&decl.base, "int a = *(int*)b;",
|
||||
scc_parse_declaration);
|
||||
}
|
||||
|
||||
// // 测试 int a, b;
|
||||
// {
|
||||
// scc_ast_decl_t decl_a, decl_b;
|
||||
// scc_ast_decl_val_init(&decl_a,
|
||||
// (scc_ast_type_t
|
||||
// *)&scc_ast_builtin_type_int, "a", null);
|
||||
// scc_ast_decl_val_init(&decl_b,
|
||||
// (scc_ast_type_t
|
||||
// *)&scc_ast_builtin_type_int, "b", null);
|
||||
|
||||
// scc_ast_decl_vec_t decl_vec;
|
||||
// scc_vec_init(decl_vec);
|
||||
// scc_vec_push(decl_vec, &decl_a);
|
||||
// scc_vec_push(decl_vec, &decl_b);
|
||||
|
||||
// scc_ast_decl_t decl_list;
|
||||
// scc_ast_decl_list_init(&decl_list, &decl_vec); // 假设存在该函数
|
||||
|
||||
// SCC_CHECK_AST(&decl_list.base, "int a, b;",
|
||||
// scc_parse_declaration);
|
||||
// }
|
||||
|
||||
// // 测试 int a = 1, b = 2;
|
||||
// {
|
||||
// scc_ast_expr_t lit1, lit2;
|
||||
// scc_ast_expr_literal_int_init(&lit1, "1", false);
|
||||
// scc_ast_expr_literal_int_init(&lit2, "2", false);
|
||||
|
||||
// scc_ast_decl_t decl_a, decl_b;
|
||||
// scc_ast_decl_val_init(&decl_a,
|
||||
// (scc_ast_type_t
|
||||
// *)&scc_ast_builtin_type_int, "a", &lit1);
|
||||
// scc_ast_decl_val_init(&decl_b,
|
||||
// (scc_ast_type_t
|
||||
// *)&scc_ast_builtin_type_int, "b", &lit2);
|
||||
|
||||
// scc_ast_decl_vec_t decl_vec;
|
||||
// scc_vec_init(decl_vec);
|
||||
// scc_vec_push(decl_vec, &decl_a);
|
||||
// scc_vec_push(decl_vec, &decl_b);
|
||||
|
||||
// scc_ast_decl_t decl_list;
|
||||
// scc_ast_decl_list_init(&decl_list, &decl_vec); // 假设存在该函数
|
||||
|
||||
// SCC_CHECK_AST(&decl_list.base, "int a = 1, b = 2;",
|
||||
// scc_parse_declaration);
|
||||
// }
|
||||
"__scc_builtin_va_arg(ag, int)";
|
||||
"__scc_builtin_va_arg(ag, long long)";
|
||||
}
|
||||
|
||||
{
|
||||
// int (*(*)(void))[5] (指向函数的指针,该函数返回指向数组的指针)
|
||||
// 步骤:
|
||||
@@ -795,10 +924,16 @@ static void test_parser_expression(void) {
|
||||
SCC_CHECK_AST(&log_not.base, "!x", scc_parse_expression);
|
||||
|
||||
// sizeof 表达式
|
||||
// TODO
|
||||
// scc_ast_expr_t sizeof_expr;
|
||||
// scc_ast_expr_sizeof_expr_init(&sizeof_expr, &x);
|
||||
// SCC_CHECK_AST(&sizeof_expr.base, "sizeof x", scc_parse_expression);
|
||||
scc_ast_expr_t sizeof_x_expr;
|
||||
scc_ast_expr_sizeof_init(&sizeof_x_expr, null, &x);
|
||||
SCC_CHECK_AST(&sizeof_x_expr.base, "sizeof(x)", scc_parse_expression);
|
||||
|
||||
scc_ast_expr_t sizeof_int_expr;
|
||||
scc_ast_expr_sizeof_init(&sizeof_int_expr,
|
||||
(scc_ast_type_t *)&scc_ast_builtin_type_int,
|
||||
null);
|
||||
SCC_CHECK_AST(&sizeof_int_expr.base, "sizeof(int)",
|
||||
scc_parse_expression);
|
||||
}
|
||||
|
||||
// 4. 类型转换(示例: (int)x )
|
||||
@@ -955,7 +1090,7 @@ static void test_parser_type(void) {
|
||||
// 1. int
|
||||
{
|
||||
SCC_CHECK_AST((scc_ast_node_t *)&scc_ast_builtin_type_int, "int",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
}
|
||||
|
||||
// 2. int *
|
||||
@@ -963,7 +1098,7 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_t ptr_to_int;
|
||||
scc_ast_type_pointer_init(
|
||||
&ptr_to_int, (scc_ast_type_t *)&scc_ast_builtin_type_int);
|
||||
SCC_CHECK_AST(&ptr_to_int.base, "int *", _scc_parse_type);
|
||||
SCC_CHECK_AST(&ptr_to_int.base, "int *", scc_parse_type_name);
|
||||
}
|
||||
|
||||
// 3. int *[3]
|
||||
@@ -977,7 +1112,7 @@ static void test_parser_type(void) {
|
||||
|
||||
scc_ast_type_t array_of_ptr;
|
||||
scc_ast_type_array_init(&array_of_ptr, &ptr_to_int, &size_3);
|
||||
SCC_CHECK_AST(&array_of_ptr.base, "int *[3]", _scc_parse_type);
|
||||
SCC_CHECK_AST(&array_of_ptr.base, "int *[3]", scc_parse_type_name);
|
||||
}
|
||||
|
||||
// 4. int (*)[3]
|
||||
@@ -992,7 +1127,8 @@ static void test_parser_type(void) {
|
||||
|
||||
scc_ast_type_t ptr_to_array;
|
||||
scc_ast_type_pointer_init(&ptr_to_array, &array_of_int);
|
||||
SCC_CHECK_AST(&ptr_to_array.base, "int (*)[3]", _scc_parse_type);
|
||||
SCC_CHECK_AST(&ptr_to_array.base, "int (*)[3]",
|
||||
scc_parse_type_name);
|
||||
}
|
||||
|
||||
// 5. int (*)[*]
|
||||
@@ -1005,7 +1141,7 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_t ptr_to_array_var;
|
||||
scc_ast_type_pointer_init(&ptr_to_array_var, &array_of_int_var);
|
||||
SCC_CHECK_AST(&ptr_to_array_var.base, "int (*)[*]",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
}
|
||||
|
||||
// 6. int *()
|
||||
@@ -1018,7 +1154,7 @@ static void test_parser_type(void) {
|
||||
// 函数类型,返回 int*,无参数
|
||||
scc_ast_type_t func_type;
|
||||
scc_ast_type_function_init(&func_type, &ptr_to_int, null);
|
||||
SCC_CHECK_AST(&func_type.base, "int *()", _scc_parse_type);
|
||||
SCC_CHECK_AST(&func_type.base, "int *()", scc_parse_type_name);
|
||||
}
|
||||
|
||||
// 7. int (*)(void)
|
||||
@@ -1037,7 +1173,8 @@ static void test_parser_type(void) {
|
||||
|
||||
scc_ast_type_t ptr_to_func;
|
||||
scc_ast_type_pointer_init(&ptr_to_func, &func_void);
|
||||
SCC_CHECK_AST(&ptr_to_func.base, "int (*)(void)", _scc_parse_type);
|
||||
SCC_CHECK_AST(&ptr_to_func.base, "int (*)(void)",
|
||||
scc_parse_type_name);
|
||||
}
|
||||
|
||||
// 8. int (*const [])(unsigned int, ...)
|
||||
@@ -1078,51 +1215,59 @@ static void test_parser_type(void) {
|
||||
|
||||
SCC_CHECK_AST(&array_of_ptr.base,
|
||||
"int (*const [])(unsigned int, ...)",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 基本内置类型及组合
|
||||
{
|
||||
// int
|
||||
SCC_CHECK_AST((scc_ast_node_t *)&scc_ast_builtin_type_int, "int",
|
||||
_scc_parse_type);
|
||||
SCC_CHECK_AST(&scc_ast_builtin_type_int.base, "int",
|
||||
scc_parse_type_name);
|
||||
|
||||
// char
|
||||
SCC_CHECK_AST((scc_ast_node_t *)&scc_ast_builtin_type_char, "char",
|
||||
_scc_parse_type);
|
||||
SCC_CHECK_AST(&scc_ast_builtin_type_char.base, "char",
|
||||
scc_parse_type_name);
|
||||
|
||||
// long long
|
||||
SCC_CHECK_AST((scc_ast_node_t *)&scc_ast_builtin_type_long_long,
|
||||
"long long", _scc_parse_type);
|
||||
SCC_CHECK_AST(&scc_ast_builtin_type_long_long.base, "long long",
|
||||
scc_parse_type_name);
|
||||
|
||||
// long long
|
||||
SCC_CHECK_AST(&scc_ast_builtin_type_long_long.base, "long long int",
|
||||
scc_parse_type_name);
|
||||
|
||||
// short
|
||||
SCC_CHECK_AST(&scc_ast_builtin_type_short.base, "short int",
|
||||
scc_parse_type_name);
|
||||
|
||||
// unsigned int
|
||||
SCC_CHECK_AST((scc_ast_node_t *)&scc_ast_builtin_type_unsigned_int,
|
||||
"unsigned int", _scc_parse_type);
|
||||
SCC_CHECK_AST(&scc_ast_builtin_type_unsigned_int.base, "unsigned int",
|
||||
scc_parse_type_name);
|
||||
|
||||
// float
|
||||
SCC_CHECK_AST((scc_ast_node_t *)&scc_ast_builtin_type_float, "float",
|
||||
_scc_parse_type);
|
||||
SCC_CHECK_AST(&scc_ast_builtin_type_float.base, "float",
|
||||
scc_parse_type_name);
|
||||
|
||||
// double
|
||||
SCC_CHECK_AST((scc_ast_node_t *)&scc_ast_builtin_type_double, "double",
|
||||
_scc_parse_type);
|
||||
SCC_CHECK_AST(&scc_ast_builtin_type_double.base, "double",
|
||||
scc_parse_type_name);
|
||||
|
||||
// void
|
||||
SCC_CHECK_AST((scc_ast_node_t *)&scc_ast_builtin_type_void, "void",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
|
||||
// bool
|
||||
SCC_CHECK_AST((scc_ast_node_t *)&scc_ast_builtin_type_bool, "bool",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
|
||||
// long double
|
||||
SCC_CHECK_AST((scc_ast_node_t *)&scc_ast_builtin_type_long_double,
|
||||
"long double", _scc_parse_type);
|
||||
"long double", scc_parse_type_name);
|
||||
|
||||
// _Complex double
|
||||
SCC_CHECK_AST((scc_ast_node_t *)&scc_ast_builtin_type_complex_double,
|
||||
"double complex", _scc_parse_type);
|
||||
"double complex", scc_parse_type_name);
|
||||
}
|
||||
|
||||
// 2. 带类型限定符的基本类型 (const, volatile)
|
||||
@@ -1130,20 +1275,20 @@ static void test_parser_type(void) {
|
||||
// const int
|
||||
scc_ast_type_t const_int = scc_ast_builtin_type_int;
|
||||
const_int.quals.is_const = true;
|
||||
SCC_CHECK_AST(&const_int.base, "const int", _scc_parse_type);
|
||||
SCC_CHECK_AST(&const_int.base, "const int", scc_parse_type_name);
|
||||
|
||||
// volatile unsigned long
|
||||
scc_ast_type_t volatile_ulong = scc_ast_builtin_type_unsigned_long;
|
||||
volatile_ulong.quals.is_volatile = true;
|
||||
SCC_CHECK_AST(&volatile_ulong.base, "volatile unsigned long",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
|
||||
// const volatile char
|
||||
scc_ast_type_t const_volatile_char = scc_ast_builtin_type_char;
|
||||
const_volatile_char.quals.is_const = true;
|
||||
const_volatile_char.quals.is_volatile = true;
|
||||
SCC_CHECK_AST(&const_volatile_char.base, "const volatile char",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
}
|
||||
|
||||
// 3. 指针类型
|
||||
@@ -1152,33 +1297,35 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_t ptr_to_int;
|
||||
scc_ast_type_pointer_init(&ptr_to_int,
|
||||
(scc_ast_type_t *)&scc_ast_builtin_type_int);
|
||||
SCC_CHECK_AST(&ptr_to_int.base, "int *", _scc_parse_type);
|
||||
SCC_CHECK_AST(&ptr_to_int.base, "int *", scc_parse_type_name);
|
||||
|
||||
// int **
|
||||
scc_ast_type_t ptr_to_ptr_to_int;
|
||||
scc_ast_type_pointer_init(&ptr_to_ptr_to_int, &ptr_to_int);
|
||||
SCC_CHECK_AST(&ptr_to_ptr_to_int.base, "int **", _scc_parse_type);
|
||||
SCC_CHECK_AST(&ptr_to_ptr_to_int.base, "int **", scc_parse_type_name);
|
||||
|
||||
// int * const (const pointer to int)
|
||||
scc_ast_type_t const_ptr_to_int;
|
||||
scc_ast_type_pointer_init(&const_ptr_to_int,
|
||||
(scc_ast_type_t *)&scc_ast_builtin_type_int);
|
||||
const_ptr_to_int.quals.is_const = true;
|
||||
SCC_CHECK_AST(&const_ptr_to_int.base, "int * const", _scc_parse_type);
|
||||
SCC_CHECK_AST(&const_ptr_to_int.base, "int * const",
|
||||
scc_parse_type_name);
|
||||
|
||||
// const int * (pointer to const int)
|
||||
scc_ast_type_t const_int_type = scc_ast_builtin_type_int;
|
||||
const_int_type.quals.is_const = true;
|
||||
scc_ast_type_t ptr_to_const_int;
|
||||
scc_ast_type_pointer_init(&ptr_to_const_int, &const_int_type);
|
||||
SCC_CHECK_AST(&ptr_to_const_int.base, "const int *", _scc_parse_type);
|
||||
SCC_CHECK_AST(&ptr_to_const_int.base, "const int *",
|
||||
scc_parse_type_name);
|
||||
|
||||
// const int * const (const pointer to const int)
|
||||
scc_ast_type_t const_ptr_to_const_int;
|
||||
scc_ast_type_pointer_init(&const_ptr_to_const_int, &const_int_type);
|
||||
const_ptr_to_const_int.quals.is_const = true;
|
||||
SCC_CHECK_AST(&const_ptr_to_const_int.base, "const int * const",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
|
||||
// volatile int * restrict
|
||||
scc_ast_type_t volatile_int = scc_ast_builtin_type_int;
|
||||
@@ -1187,7 +1334,7 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_pointer_init(&restrict_ptr_to_volatile_int, &volatile_int);
|
||||
restrict_ptr_to_volatile_int.quals.is_restrict = true;
|
||||
SCC_CHECK_AST(&restrict_ptr_to_volatile_int.base,
|
||||
"volatile int * restrict", _scc_parse_type);
|
||||
"volatile int * restrict", scc_parse_type_name);
|
||||
}
|
||||
|
||||
// 4. 数组类型
|
||||
@@ -1199,14 +1346,15 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_array_init(&array_of_5_int,
|
||||
(scc_ast_type_t *)&scc_ast_builtin_type_int,
|
||||
&size_5);
|
||||
SCC_CHECK_AST(&array_of_5_int.base, "int [5]", _scc_parse_type);
|
||||
SCC_CHECK_AST(&array_of_5_int.base, "int [5]", scc_parse_type_name);
|
||||
|
||||
// int [] (不完整类型)
|
||||
scc_ast_type_t array_of_int_unknown;
|
||||
scc_ast_type_array_init(&array_of_int_unknown,
|
||||
(scc_ast_type_t *)&scc_ast_builtin_type_int,
|
||||
null);
|
||||
SCC_CHECK_AST(&array_of_int_unknown.base, "int []", _scc_parse_type);
|
||||
SCC_CHECK_AST(&array_of_int_unknown.base, "int []",
|
||||
scc_parse_type_name);
|
||||
|
||||
// // int [*] (变长数组原型中的不定长数组)
|
||||
// FIXME
|
||||
@@ -1228,7 +1376,7 @@ static void test_parser_type(void) {
|
||||
&inner_array, (scc_ast_type_t *)&scc_ast_builtin_type_int, &size_3);
|
||||
scc_ast_type_t outer_array;
|
||||
scc_ast_type_array_init(&outer_array, &inner_array, &size_5);
|
||||
SCC_CHECK_AST(&outer_array.base, "int [5][3]", _scc_parse_type);
|
||||
SCC_CHECK_AST(&outer_array.base, "int [5][3]", scc_parse_type_name);
|
||||
|
||||
// int (*)[5] (指向数组的指针) 已在前面测试,这里重复以保持完整性
|
||||
scc_ast_type_t array_of_5_int2;
|
||||
@@ -1237,7 +1385,7 @@ static void test_parser_type(void) {
|
||||
&size_5);
|
||||
scc_ast_type_t ptr_to_array;
|
||||
scc_ast_type_pointer_init(&ptr_to_array, &array_of_5_int2);
|
||||
SCC_CHECK_AST(&ptr_to_array.base, "int (*)[5]", _scc_parse_type);
|
||||
SCC_CHECK_AST(&ptr_to_array.base, "int (*)[5]", scc_parse_type_name);
|
||||
|
||||
// int *[5] (指针数组)
|
||||
scc_ast_type_t ptr_to_int2;
|
||||
@@ -1245,14 +1393,14 @@ static void test_parser_type(void) {
|
||||
(scc_ast_type_t *)&scc_ast_builtin_type_int);
|
||||
scc_ast_type_t array_of_5_ptr;
|
||||
scc_ast_type_array_init(&array_of_5_ptr, &ptr_to_int2, &size_5);
|
||||
SCC_CHECK_AST(&array_of_5_ptr.base, "int *[5]", _scc_parse_type);
|
||||
SCC_CHECK_AST(&array_of_5_ptr.base, "int *[5]", scc_parse_type_name);
|
||||
|
||||
// const int [5] (数组元素为const int)
|
||||
scc_ast_type_t const_int2 = scc_ast_builtin_type_int;
|
||||
const_int2.quals.is_const = true;
|
||||
scc_ast_type_t const_array;
|
||||
scc_ast_type_array_init(&const_array, &const_int2, &size_5);
|
||||
SCC_CHECK_AST(&const_array.base, "const int [5]", _scc_parse_type);
|
||||
SCC_CHECK_AST(&const_array.base, "const int [5]", scc_parse_type_name);
|
||||
}
|
||||
|
||||
// 5. 函数类型
|
||||
@@ -1267,7 +1415,7 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_function_init(&func_void,
|
||||
(scc_ast_type_t *)&scc_ast_builtin_type_int,
|
||||
&func_params);
|
||||
SCC_CHECK_AST(&func_void.base, "int (void)", _scc_parse_type);
|
||||
SCC_CHECK_AST(&func_void.base, "int (void)", scc_parse_type_name);
|
||||
|
||||
// // int () (无参数声明,非原型)
|
||||
// //
|
||||
@@ -1291,7 +1439,7 @@ static void test_parser_type(void) {
|
||||
(scc_ast_type_t *)&scc_ast_builtin_type_int,
|
||||
¶ms);
|
||||
SCC_CHECK_AST(&func_with_params.base, "int (int, float)",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
|
||||
// int (int, ...) (可变参数)
|
||||
scc_ast_decl_t param_int, param_var;
|
||||
@@ -1309,7 +1457,8 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_function_init(&func_varargs,
|
||||
(scc_ast_type_t *)&scc_ast_builtin_type_int,
|
||||
¶ms_var);
|
||||
SCC_CHECK_AST(&func_varargs.base, "int (int, ...)", _scc_parse_type);
|
||||
SCC_CHECK_AST(&func_varargs.base, "int (int, ...)",
|
||||
scc_parse_type_name);
|
||||
|
||||
// int (*)(int) (函数指针)
|
||||
scc_ast_decl_t param;
|
||||
@@ -1324,7 +1473,7 @@ static void test_parser_type(void) {
|
||||
&func_type, (scc_ast_type_t *)&scc_ast_builtin_type_int, ¶ms2);
|
||||
scc_ast_type_t ptr_to_func;
|
||||
scc_ast_type_pointer_init(&ptr_to_func, &func_type);
|
||||
SCC_CHECK_AST(&ptr_to_func.base, "int (*)(int)", _scc_parse_type);
|
||||
SCC_CHECK_AST(&ptr_to_func.base, "int (*)(int)", scc_parse_type_name);
|
||||
}
|
||||
|
||||
// 6. 函数指针和复杂声明符
|
||||
@@ -1360,7 +1509,7 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_pointer_init(&ptr_to_func, &func_type);
|
||||
|
||||
SCC_CHECK_AST(&ptr_to_func.base, "int (*(*)(void))[5]",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
|
||||
// int (*(*)[5])(void) (指向数组的指针,数组元素为函数指针)
|
||||
// 1) 函数类型:返回 int,无参数
|
||||
@@ -1383,7 +1532,7 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_t ptr_to_array_of_ptr;
|
||||
scc_ast_type_pointer_init(&ptr_to_array_of_ptr, &array_of_ptr_to_func);
|
||||
SCC_CHECK_AST(&ptr_to_array_of_ptr.base, "int (*(*)[5])(void)",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
}
|
||||
|
||||
// 7. 结构体/联合/枚举类型(标记和定义)
|
||||
@@ -1392,7 +1541,7 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_t struct_tag;
|
||||
scc_ast_type_struct_init(&struct_tag, "S",
|
||||
null); // name="S", members=null
|
||||
SCC_CHECK_AST(&struct_tag.base, "struct S", _scc_parse_type);
|
||||
SCC_CHECK_AST(&struct_tag.base, "struct S", scc_parse_type_name);
|
||||
|
||||
// struct { int x; } (匿名结构体定义)
|
||||
scc_ast_decl_t field;
|
||||
@@ -1406,18 +1555,19 @@ static void test_parser_type(void) {
|
||||
scc_ast_decl_struct_init(&struct_def, null, &fields);
|
||||
scc_ast_type_t struct_type;
|
||||
scc_ast_type_struct_init(&struct_type, null, &struct_def);
|
||||
SCC_CHECK_AST(&struct_type.base, "struct { int x; }", _scc_parse_type);
|
||||
SCC_CHECK_AST(&struct_type.base, "struct { int x; }",
|
||||
scc_parse_type_name);
|
||||
scc_vec_init(fields);
|
||||
scc_vec_push(fields, &field);
|
||||
scc_ast_decl_struct_init(&struct_def, "A", &fields);
|
||||
scc_ast_type_struct_init(&struct_type, "A", &struct_def);
|
||||
SCC_CHECK_AST(&struct_type.base, "struct A { int x; }",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
|
||||
// union U (不完整类型)
|
||||
scc_ast_type_t union_tag;
|
||||
scc_ast_type_union_init(&union_tag, "U", null);
|
||||
SCC_CHECK_AST(&union_tag.base, "union U", _scc_parse_type);
|
||||
SCC_CHECK_AST(&union_tag.base, "union U", scc_parse_type_name);
|
||||
|
||||
// union { int a; float b; } (匿名联合定义)
|
||||
scc_ast_decl_t field_a, field_b;
|
||||
@@ -1435,16 +1585,16 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_t union_type;
|
||||
scc_ast_type_union_init(&union_type, null, &union_def);
|
||||
SCC_CHECK_AST(&union_type.base, "union { int a; float b; }",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
scc_ast_decl_union_init(&union_def, "Union", &fields_union);
|
||||
scc_ast_type_union_init(&union_type, "Union", &union_def);
|
||||
SCC_CHECK_AST(&union_type.base, "union Union { int a; float b; }",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
|
||||
// enum E (不完整类型)
|
||||
scc_ast_type_t enum_tag;
|
||||
scc_ast_type_enum_init(&enum_tag, "E", null);
|
||||
SCC_CHECK_AST(&enum_tag.base, "enum E", _scc_parse_type);
|
||||
SCC_CHECK_AST(&enum_tag.base, "enum E", scc_parse_type_name);
|
||||
|
||||
// enum { RED, GREEN, BLUE } (匿名枚举定义)
|
||||
scc_ast_expr_t red, green, blue;
|
||||
@@ -1460,13 +1610,13 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_t enum_type;
|
||||
scc_ast_type_enum_init(&enum_type, null, &enum_def);
|
||||
SCC_CHECK_AST(&enum_type.base, "enum { RED, GREEN, BLUE }",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
|
||||
scc_vec_unsafe_from_array(enumerators, array);
|
||||
scc_ast_decl_enum_init(&enum_def, "E", &enumerators);
|
||||
scc_ast_type_enum_init(&enum_type, "E", &enum_def);
|
||||
SCC_CHECK_AST(&enum_type.base, "enum E { RED, GREEN, BLUE, }",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
}
|
||||
|
||||
// 8. typedef 类型
|
||||
@@ -1510,7 +1660,7 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_t ptr_to_array;
|
||||
scc_ast_type_pointer_init(&ptr_to_array, &array_of_ptr);
|
||||
SCC_CHECK_AST(&ptr_to_array.base, "const int * volatile (*)[10]",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
|
||||
// float (*(*)(int, ...))()
|
||||
// 1) float 类型作为内部函数返回类型
|
||||
@@ -1544,7 +1694,7 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_t ptr_to_outer_func;
|
||||
scc_ast_type_pointer_init(&ptr_to_outer_func, &outer_func);
|
||||
SCC_CHECK_AST(&ptr_to_outer_func.base, "float (*(*)(int, ...))()",
|
||||
_scc_parse_type);
|
||||
scc_parse_type_name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user