void init_func(void); #define TEST_INIT init_func() #include "parser_test.h" static scc_ast_type_t int_type; static scc_ast_type_t char_type; static scc_ast_type_t void_type; static scc_ast_type_t pointer_int_type; static scc_ast_type_t pointer_char_type; static scc_ast_type_t pointer_void_type; static scc_ast_type_t pointer_pointer_int_type; static scc_ast_type_t va_list_type; void init_func(void) { scc_ast_type_builtin_init(&int_type, SCC_AST_BUILTIN_TYPE_INT, scc_pos_create()); scc_ast_type_builtin_init(&char_type, SCC_AST_BUILTIN_TYPE_CHAR, scc_pos_create()); scc_ast_type_builtin_init(&void_type, SCC_AST_BUILTIN_TYPE_VOID, scc_pos_create()); scc_ast_type_pointer_init(&pointer_int_type, &int_type, scc_pos_create()); scc_ast_type_pointer_init(&pointer_char_type, &char_type, scc_pos_create()); scc_ast_type_pointer_init(&pointer_void_type, &void_type, scc_pos_create()); scc_ast_type_pointer_init(&pointer_pointer_int_type, &pointer_int_type, scc_pos_create()); scc_ast_type_builtin_init(&va_list_type, SCC_AST_BUILTIN_TYPE_VA_LIST, scc_pos_create()); } static void test_parser_unit(void) { // 1. 变量声明 int a; { scc_ast_decl_t int_decl; scc_ast_decl_val_init(&int_decl, &int_type, "a", null, scc_pos_create()); SCC_CHECK_AST(&int_decl.base, "int a;", scc_parse_declaration); } // 2. 函数声明 int main(void) {} { // 构造函数类型:返回 int,参数为空 scc_ast_type_t func_type; scc_ast_decl_vec_t func_params; scc_ast_decl_t void_decl; scc_ast_decl_param_init(&void_decl, &void_type, null, scc_pos_create()); scc_ast_decl_t *array[] = {&void_decl}; scc_vec_unsafe_from_static_array(func_params, array); scc_ast_type_function_init(&func_type, &int_type, &func_params, scc_pos_create()); // 构造复合语句块(空) scc_ast_stmt_t compound; scc_ast_stmt_compound_init(&compound, null, scc_pos_create()); // 构造函数声明 scc_ast_decl_t func_decl; scc_ast_decl_func_init(&func_decl, &func_type, "main", &compound, scc_pos_create()); SCC_CHECK_AST(&func_decl.base, "int main(void) {}", scc_parse_declaration); } // 3. 翻译单元包含一个函数定义 { scc_ast_type_t func_type; scc_ast_decl_vec_t func_params; scc_ast_decl_t void_decl; scc_ast_decl_param_init(&void_decl, &void_type, null, scc_pos_create()); scc_ast_decl_t *array[] = {&void_decl}; scc_vec_unsafe_from_static_array(func_params, array); scc_ast_type_function_init(&func_type, &int_type, &func_params, scc_pos_create()); scc_ast_stmt_t compound; scc_ast_stmt_compound_init(&compound, null, scc_pos_create()); scc_ast_decl_t func_decl; scc_ast_decl_func_init(&func_decl, &func_type, "main", &compound, scc_pos_create()); // 构造翻译单元 scc_ast_decl_vec_t tu_decls; scc_vec_init(tu_decls); scc_vec_push(tu_decls, &func_decl); scc_ast_translation_unit_t tu; scc_ast_translation_unit_init(&tu, &tu_decls, scc_pos_create()); SCC_CHECK_AST(&tu.base, "int main(void) {}", scc_parse_translation_unit); } // 4. 带返回语句的函数 int main(void) { return 65536; } { // 返回语句中的整数常量 scc_ast_expr_t ret_val; scc_ast_expr_literal_int_init(&ret_val, "65536", false, scc_pos_create()); scc_ast_stmt_t ret_stmt; scc_ast_stmt_return_init(&ret_stmt, &ret_val, scc_pos_create()); // 复合语句包含该返回语句 scc_ast_block_item_vec_t items; scc_vec_init(items); scc_vec_push(items, (scc_ast_node_t *)&ret_stmt); scc_ast_stmt_t compound; scc_ast_stmt_compound_init(&compound, &items, scc_pos_create()); // items 被移动 // 函数类型 scc_ast_type_t func_type; scc_ast_decl_vec_t func_params; scc_ast_decl_t void_decl; scc_ast_decl_param_init(&void_decl, &void_type, null, scc_pos_create()); scc_ast_decl_t *array[] = {&void_decl}; scc_vec_unsafe_from_static_array(func_params, array); scc_ast_type_function_init(&func_type, &int_type, &func_params, scc_pos_create()); scc_ast_decl_t func_decl; scc_ast_decl_func_init(&func_decl, &func_type, "main", &compound, scc_pos_create()); scc_ast_decl_vec_t tu_decls; scc_vec_init(tu_decls); scc_vec_push(tu_decls, &func_decl); scc_ast_translation_unit_t tu; scc_ast_translation_unit_init(&tu, &tu_decls, scc_pos_create()); SCC_CHECK_AST(&tu.base, "int main(void) { return 65536; }", scc_parse_translation_unit); } // 5. 多语句函数(复杂示例) { // 变量声明 int a; scc_ast_decl_t a_decl; scc_ast_decl_val_init(&a_decl, &int_type, "a", null, scc_pos_create()); // 变量声明 int b; scc_ast_decl_t b_decl; scc_ast_decl_val_init(&b_decl, &int_type, "b", null, scc_pos_create()); // 表达式 1 + 2 * 3 scc_ast_expr_t lit1, lit2, lit3, mul, add; scc_ast_expr_literal_int_init(&lit1, "1", false, scc_pos_create()); scc_ast_expr_literal_int_init(&lit2, "2", false, scc_pos_create()); scc_ast_expr_literal_int_init(&lit3, "3", false, scc_pos_create()); scc_ast_expr_binary_init(&mul, SCC_AST_OP_MUL, &lit2, &lit3, scc_pos_create()); scc_ast_expr_binary_init(&add, SCC_AST_OP_ADD, &lit1, &mul, scc_pos_create()); // 赋值 a = 1 + 2 * 3; scc_ast_expr_t a_ref1, assign1; scc_ast_expr_identifier_init(&a_ref1, "a", scc_pos_create()); scc_ast_expr_binary_init(&assign1, SCC_AST_OP_ASSIGN, &a_ref1, &add, scc_pos_create()); scc_ast_stmt_t assign1_stmt; scc_ast_stmt_expr_init(&assign1_stmt, &assign1, scc_pos_create()); // 赋值 b = 7; scc_ast_expr_t lit7; scc_ast_expr_literal_int_init(&lit7, "7", false, scc_pos_create()); scc_ast_expr_t b_ref1, assign2; scc_ast_expr_identifier_init(&b_ref1, "b", scc_pos_create()); scc_ast_expr_binary_init(&assign2, SCC_AST_OP_ASSIGN, &b_ref1, &lit7, scc_pos_create()); scc_ast_stmt_t assign2_stmt; scc_ast_stmt_expr_init(&assign2_stmt, &assign2, scc_pos_create()); // 表达式 a - b + 1 scc_ast_expr_t a_ref2, b_ref2, sub, add2, lit1_2; scc_ast_expr_identifier_init(&a_ref2, "a", scc_pos_create()); scc_ast_expr_identifier_init(&b_ref2, "b", scc_pos_create()); scc_ast_expr_binary_init(&sub, SCC_AST_OP_SUB, &a_ref2, &b_ref2, scc_pos_create()); scc_ast_expr_literal_int_init(&lit1_2, "1", false, scc_pos_create()); scc_ast_expr_binary_init(&add2, SCC_AST_OP_ADD, &sub, &lit1_2, scc_pos_create()); // 赋值 a = a - b + 1; scc_ast_expr_t a_ref3, assign3; scc_ast_expr_identifier_init(&a_ref3, "a", scc_pos_create()); scc_ast_expr_binary_init(&assign3, SCC_AST_OP_ASSIGN, &a_ref3, &add2, scc_pos_create()); scc_ast_stmt_t assign3_stmt; scc_ast_stmt_expr_init(&assign3_stmt, &assign3, scc_pos_create()); // return a; scc_ast_expr_t a_ref4; scc_ast_expr_identifier_init(&a_ref4, "a", scc_pos_create()); scc_ast_stmt_t ret_stmt; scc_ast_stmt_return_init(&ret_stmt, &a_ref4, scc_pos_create()); // 复合语句块,按顺序放入 scc_ast_block_item_vec_t items; scc_vec_init(items); scc_vec_push(items, (scc_ast_node_t *)&a_decl); scc_vec_push(items, (scc_ast_node_t *)&b_decl); scc_vec_push(items, (scc_ast_node_t *)&assign1_stmt); scc_vec_push(items, (scc_ast_node_t *)&assign2_stmt); scc_vec_push(items, (scc_ast_node_t *)&assign3_stmt); scc_vec_push(items, (scc_ast_node_t *)&ret_stmt); scc_ast_stmt_t compound; scc_ast_stmt_compound_init(&compound, &items, scc_pos_create()); // 函数类型 scc_ast_type_t func_type; scc_ast_type_function_init(&func_type, (scc_ast_type_t *)&int_type, null, scc_pos_create()); scc_ast_decl_t func_decl; scc_ast_decl_func_init(&func_decl, &func_type, "main", &compound, scc_pos_create()); scc_ast_decl_vec_t tu_decls; scc_vec_init(tu_decls); scc_vec_push(tu_decls, &func_decl); scc_ast_translation_unit_t tu; scc_ast_translation_unit_init(&tu, &tu_decls, scc_pos_create()); const char *input = "int main() {\n" " int a;\n" " int b;\n" " a = 1 + 2 * 3;\n" " b = 7;\n" " a = a - b + 1;\n" " return a;\n" "}\n"; SCC_CHECK_AST(&tu.base, input, scc_parse_translation_unit); } { // 整数字面量 10 scc_ast_expr_t lit10; scc_ast_expr_literal_int_init(&lit10, "10", false, scc_pos_create()); // 变量声明 int x = 10; scc_ast_decl_t x_decl; scc_ast_decl_val_init(&x_decl, (scc_ast_type_t *)&int_type, "x", &lit10, scc_pos_create()); // 表达式 x + 1 scc_ast_expr_t x_ref1, lit1, add; scc_ast_expr_identifier_init(&x_ref1, "x", scc_pos_create()); scc_ast_expr_literal_int_init(&lit1, "1", false, scc_pos_create()); scc_ast_expr_binary_init(&add, SCC_AST_OP_ADD, &x_ref1, &lit1, scc_pos_create()); // 赋值 x = x + 1 scc_ast_expr_t x_ref2, assign; scc_ast_expr_identifier_init(&x_ref2, "x", scc_pos_create()); scc_ast_expr_binary_init(&assign, SCC_AST_OP_ASSIGN, &x_ref2, &add, scc_pos_create()); // 表达式语句 scc_ast_stmt_t assign_stmt; scc_ast_stmt_expr_init(&assign_stmt, &assign, scc_pos_create()); // return x scc_ast_expr_t x_ref3; scc_ast_expr_identifier_init(&x_ref3, "x", scc_pos_create()); scc_ast_stmt_t ret_stmt; scc_ast_stmt_return_init(&ret_stmt, &x_ref3, scc_pos_create()); // 复合语句块 scc_ast_block_item_vec_t items; scc_vec_init(items); scc_vec_push(items, (scc_ast_node_t *)&x_decl); scc_vec_push(items, (scc_ast_node_t *)&assign_stmt); scc_vec_push(items, (scc_ast_node_t *)&ret_stmt); scc_ast_stmt_t compound; scc_ast_stmt_compound_init(&compound, &items, scc_pos_create()); // 函数类型(返回 int,无参数) scc_ast_type_t func_type; scc_ast_type_function_init(&func_type, (scc_ast_type_t *)&int_type, null, scc_pos_create()); // null 表示无参数 // 函数声明 int main() { ... } scc_ast_decl_t func_decl; scc_ast_decl_func_init(&func_decl, &func_type, "main", &compound, scc_pos_create()); // 翻译单元 scc_ast_decl_vec_t tu_decls; scc_vec_init(tu_decls); scc_vec_push(tu_decls, &func_decl); scc_ast_translation_unit_t tu; scc_ast_translation_unit_init(&tu, &tu_decls, scc_pos_create()); // 输入源代码 const char *input = "int main() {\n" " int x = 10;\n" " x = x + 1;\n" " return x;\n" "}\n"; // 比较解析结果与期望 AST SCC_CHECK_AST(&tu.base, input, scc_parse_translation_unit); } { scc_ast_decl_vec_t params; scc_ast_decl_t param0; scc_ast_decl_param_init(¶m0, (scc_ast_type_t *)&int_type, "a", scc_pos_create()); scc_ast_decl_t param1; scc_ast_decl_param_init(¶m1, (scc_ast_type_t *)&int_type, "b", scc_pos_create()); scc_ast_decl_t param2; scc_ast_decl_param_init(¶m2, (scc_ast_type_t *)&va_list_type, null, scc_pos_create()); scc_ast_decl_t *params_array[] = {¶m0, ¶m1, ¶m2}; scc_vec_unsafe_from_static_array(params, params_array); scc_ast_type_t decl_func_type; scc_ast_type_function_init(&decl_func_type, (scc_ast_type_t *)&int_type, ¶ms, scc_pos_create()); scc_ast_decl_t decl_func; scc_ast_decl_func_init(&decl_func, &decl_func_type, "add", null, scc_pos_create()); SCC_CHECK_AST(&decl_func.base, "int add(int a, int b, ...);", scc_parse_declaration); } { scc_ast_decl_t typedef_decl; scc_ast_decl_typedef_init(&typedef_decl, "int32_t", &int_type, scc_pos_create()); scc_ast_type_t typedef_type; scc_ast_type_typedef_init(&typedef_type, "int32_t", &typedef_decl, scc_pos_create()); scc_ast_decl_t i32a_decl; scc_ast_decl_val_init(&i32a_decl, &typedef_type, "a", null, scc_pos_create()); scc_ast_node_t *array[] = {&typedef_decl.base, &i32a_decl.base}; scc_ast_stmt_t stmt; scc_ast_block_item_vec_t items; scc_vec_unsafe_from_static_array(items, array); scc_ast_stmt_compound_init(&stmt, &items, scc_pos_create()); SCC_CHECK_AST_WITH_SEMA(&stmt.base, "{typedef int int32_t;int32_t a;}", scc_parse_statement); scc_ast_type_t void_ptr; scc_ast_type_pointer_init(&void_ptr, &void_type, scc_pos_create()); scc_ast_decl_t void_ptr_decl; scc_ast_decl_typedef_init(&void_ptr_decl, "void_ptr", &void_ptr, scc_pos_create()); scc_ast_type_t void_ptr_type; scc_ast_type_typedef_init(&void_ptr_type, "void_ptr", &void_ptr_decl, scc_pos_create()); scc_ast_decl_t void_ptr_a_decl; scc_ast_decl_val_init(&void_ptr_a_decl, &void_ptr_type, "a", null, scc_pos_create()); scc_ast_node_t *array2[] = {&void_ptr_decl.base, &void_ptr_a_decl.base}; scc_vec_unsafe_from_static_array(items, array2); scc_ast_stmt_compound_init(&stmt, &items, scc_pos_create()); SCC_CHECK_AST_WITH_SEMA(&stmt.base, "{typedef void* void_ptr; void_ptr a;}", scc_parse_statement); SCC_CHECK_AST_WITH_SEMA(&void_ptr_decl.base, "typedef void * void_ptr; ", scc_parse_declaration); } { // struct { int x; } (匿名结构体定义) scc_ast_decl_t field; scc_ast_decl_val_init(&field, (scc_ast_type_t *)&int_type, "x", null, scc_pos_create()); scc_ast_decl_vec_t fields; scc_vec_init(fields); scc_vec_push(fields, &field); scc_ast_decl_t struct_def; scc_ast_decl_struct_init(&struct_def, null, &fields, scc_pos_create()); SCC_CHECK_AST(&struct_def.base, "struct { int x;};", scc_parse_declaration); scc_ast_type_t struct_type; scc_ast_type_struct_init(&struct_type, null, &struct_def, scc_pos_create()); scc_ast_type_t typedef_type; scc_ast_type_typedef_init(&typedef_type, "struct_t", &struct_def, scc_pos_create()); scc_ast_decl_t typedef_decl; scc_ast_decl_typedef_init(&typedef_decl, "struct_t", &struct_type, scc_pos_create()); SCC_CHECK_AST_WITH_SEMA(&typedef_decl.base, "typedef struct { int x; } struct_t;", scc_parse_declaration); scc_ast_decl_t typedef_impl_decl; scc_ast_decl_val_init(&typedef_impl_decl, &typedef_type, "a", null, scc_pos_create()); scc_ast_node_t *array[] = {&typedef_decl.base, &typedef_impl_decl.base}; scc_ast_stmt_t stmt; scc_ast_block_item_vec_t items; scc_vec_unsafe_from_static_array(items, array); scc_ast_stmt_compound_init(&stmt, &items, scc_pos_create()); SCC_CHECK_AST_WITH_SEMA( &stmt.base, "{typedef struct { int x; } struct_t; struct_t a;}", scc_parse_statement); } { scc_ast_decl_t type_decl; scc_ast_decl_typedef_init(&type_decl, "size_t", &int_type, scc_pos_create()); scc_ast_type_t type_type; scc_ast_type_typedef_init(&type_type, "size_t", &type_decl, scc_pos_create()); scc_ast_decl_t param1; scc_ast_decl_param_init(¶m1, &type_type, "a", scc_pos_create()); scc_ast_decl_t param2; scc_ast_decl_param_init(¶m2, &int_type, "b", scc_pos_create()); scc_ast_decl_t param3; scc_ast_decl_param_init(¶m3, &va_list_type, null, scc_pos_create()); scc_ast_decl_t *params_array[] = {¶m1, ¶m2, ¶m3}; scc_ast_decl_vec_t func_params; scc_vec_unsafe_from_static_array(func_params, params_array); scc_ast_type_t func_type; scc_ast_type_t return_type; scc_ast_type_pointer_init(&return_type, &void_type, scc_pos_create()); scc_ast_type_function_init(&func_type, &return_type, &func_params, scc_pos_create()); scc_ast_decl_t func_decl; scc_ast_decl_func_init(&func_decl, &func_type, "func", null, scc_pos_create()); scc_ast_decl_t *decls_array[] = {&type_decl, &func_decl}; scc_ast_translation_unit_t tu; scc_ast_decl_vec_t decls; scc_vec_unsafe_from_static_array(decls, decls_array); scc_ast_translation_unit_init(&tu, &decls, scc_pos_create()); SCC_CHECK_AST_WITH_SEMA(&tu.base, "typedef int size_t;" "void *func(size_t a, int b, ...);", scc_parse_translation_unit); scc_ast_type_t type_func_ptr_type; scc_ast_type_pointer_init(&type_func_ptr_type, &func_type, scc_pos_create()); scc_ast_decl_t type_func_ptr_decl; scc_ast_decl_typedef_init(&type_func_ptr_decl, "func_t", &type_func_ptr_type, scc_pos_create()); scc_ast_decl_t *decls_array2[] = {&type_decl, &type_func_ptr_decl}; scc_vec_unsafe_from_static_array(decls, decls_array2); scc_ast_translation_unit_init(&tu, &decls, scc_pos_create()); SCC_CHECK_AST_WITH_SEMA( &tu.base, "typedef long long size_t;" "typedef void *(*func_t)(size_t a, int b, ...);", scc_parse_translation_unit); } { // 1. 构造参数类型:volatile const char *restrict,并附加 register // 存储类 scc_ast_type_t char_type; scc_ast_type_builtin_init(&char_type, SCC_AST_BUILTIN_TYPE_CHAR, scc_pos_create()); char_type.quals.is_const = true; // const char_type.quals.is_volatile = true; // volatile scc_ast_type_t ptr_to_char; scc_ast_type_pointer_init(&ptr_to_char, &char_type, scc_pos_create()); ptr_to_char.quals.is_restrict = true; // restrict限定指针 ptr_to_char.quals.is_register = true; // register存储类(作用于参数) // 2. 参数声明 scc_ast_decl_t param_decl; scc_ast_decl_param_init(¶m_decl, &ptr_to_char, "fmt", scc_pos_create()); // 3. 返回类型:void * scc_ast_type_t void_type; scc_ast_type_builtin_init(&void_type, SCC_AST_BUILTIN_TYPE_VOID, scc_pos_create()); scc_ast_type_t ptr_to_void; scc_ast_type_pointer_init(&ptr_to_void, &void_type, scc_pos_create()); // 4. 参数列表 scc_ast_decl_vec_t params; scc_vec_init(params); scc_vec_push(params, ¶m_decl); // 5. 函数类型(包含 static 和 inline) scc_ast_type_t func_type; scc_ast_type_function_init(&func_type, &ptr_to_void, ¶ms, scc_pos_create()); func_type.quals.is_static = true; func_type.quals.is_inline = true; // 6. 函数声明 scc_ast_decl_t decl; scc_ast_decl_func_init(&decl, &func_type, "call", null, scc_pos_create()); // 7. 与解析结果比较 SCC_CHECK_AST_WITH_SEMA(&decl.base, "static inline void *call(volatile register " "const char *restrict fmt);", scc_parse_declaration); } { scc_ast_expr_t lvalue; scc_ast_expr_lvalue_init(&lvalue, &void_type, scc_pos_create()); scc_ast_expr_t lhs1; scc_ast_expr_member_init(&lhs1, &lvalue, "data", scc_pos_create()); scc_ast_expr_t lhs2; scc_ast_expr_member_init(&lhs2, &lvalue, "size", scc_pos_create()); scc_ast_expr_t lhs3; scc_ast_expr_member_init(&lhs3, &lvalue, "cap", scc_pos_create()); scc_ast_expr_t rl0; scc_ast_expr_literal_int_init(&rl0, "0", false, scc_pos_create()); scc_ast_type_t void_ptr; scc_ast_type_pointer_init(&void_ptr, &void_type, scc_pos_create()); scc_ast_expr_t rhs1; scc_ast_expr_cast_init(&rhs1, &void_ptr, &rl0, scc_pos_create()); scc_ast_expr_t rhs2; scc_ast_expr_literal_int_init(&rhs2, "0", false, scc_pos_create()); scc_ast_expr_t rhs3; scc_ast_expr_literal_int_init(&rhs3, "0", false, scc_pos_create()); scc_ast_expr_vec_t lhs_exprs; scc_ast_expr_t *lhs_array[] = {&lhs1, &lhs2, &lhs3}; scc_vec_unsafe_from_static_array(lhs_exprs, lhs_array); scc_ast_expr_vec_t rhs_exprs; scc_ast_expr_t *rhs_array[] = {&rhs1, &rhs2, &rhs3}; scc_vec_unsafe_from_static_array(rhs_exprs, rhs_array); scc_ast_expr_t expr; scc_ast_expr_compound_init(&expr, &lvalue, &lhs_exprs, &rhs_exprs, scc_pos_create()); // FIXME use real records type SCC_CHECK_AST(&expr.base, "(void){.data = ((void *)0), .size = 0, .cap = 0}", scc_parse_expression); scc_ast_stmt_t stmt; scc_ast_stmt_return_init(&stmt, &expr, scc_pos_create()); 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_pos_create()); scc_ast_expr_member_init(&lhs5, &lhs4, "b", scc_pos_create()); scc_ast_expr_t lhs6; scc_ast_expr_t lhs7; scc_ast_expr_member_init(&lhs6, &lvalue, "c", scc_pos_create()); scc_ast_expr_array_subscript_init(&lhs7, &lhs6, &rl0, scc_pos_create()); scc_ast_expr_t *lhs_array_hard[] = {&lhs5, &lhs7}; scc_vec_unsafe_from_static_array(lhs_exprs, lhs_array_hard); scc_ast_expr_t *rhs_array_hard[] = {&rhs2, &rhs3}; scc_vec_unsafe_from_static_array(rhs_exprs, rhs_array_hard); scc_ast_expr_compound_init(&expr, &lvalue, &lhs_exprs, &rhs_exprs, scc_pos_create()); 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_pos_create()); 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_pos_create()); 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_pos_create()); SCC_CHECK_AST(&enum_decl.base, "enum E;", scc_parse_declaration); } } { scc_ast_stmt_t continue_stmt; scc_ast_stmt_continue_init(&continue_stmt, scc_pos_create()); scc_ast_stmt_t stmt; scc_ast_stmt_label_init(&stmt, "NEXT", &continue_stmt, scc_pos_create()); SCC_CHECK_AST(&stmt.base, "NEXT: continue;", scc_parse_statement); } { // 测试 int a = *(int*)b; { // 构造类型 int* scc_ast_type_t ptr_to_int; scc_ast_type_pointer_init(&ptr_to_int, (scc_ast_type_t *)&int_type, scc_pos_create()); // 标识符 b scc_ast_expr_t b_expr; scc_ast_expr_identifier_init(&b_expr, "b", scc_pos_create()); // 类型转换 (int*)b scc_ast_expr_t cast_expr; scc_ast_expr_cast_init(&cast_expr, &ptr_to_int, &b_expr, scc_pos_create()); // 解引用 *(int*)b scc_ast_expr_t deref_expr; scc_ast_expr_unary_init(&deref_expr, SCC_AST_OP_INDIRECTION, &cast_expr, scc_pos_create()); // 声明 int a = *(int*)b; scc_ast_decl_t decl; scc_ast_decl_val_init(&decl, (scc_ast_type_t *)&int_type, "a", &deref_expr, scc_pos_create()); 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 *)&int_type, "a", null, scc_pos_create()); scc_ast_decl_val_init(&decl_b, (scc_ast_type_t *)&int_type, "b", null, scc_pos_create()); 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_pos_create()); // 假设存在该函数 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_pos_create()); scc_ast_expr_literal_int_init(&lit2, "2", false, scc_pos_create()); scc_ast_decl_t decl_a, decl_b; scc_ast_decl_val_init(&decl_a, (scc_ast_type_t *)&int_type, "a", &lit1, scc_pos_create()); scc_ast_decl_val_init(&decl_b, (scc_ast_type_t *)&int_type, "b", &lit2, scc_pos_create()); 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_pos_create()); // 假设存在该函数 SCC_CHECK_AST(&decl_list.base, "int a = 1, b = 2;", scc_parse_declaration); } // 测试 "struct list_head *next, *prev;" { // 构造 struct list_head 类型(不完整) scc_ast_type_t struct_list_head; scc_ast_type_struct_init(&struct_list_head, "list_head", null, scc_pos_create()); // 构造两个指针类型(分别用于 next 和 prev,指向同一结构体) scc_ast_type_t ptr_to_struct1, ptr_to_struct2; scc_ast_type_pointer_init(&ptr_to_struct1, &struct_list_head, scc_pos_create()); scc_ast_type_pointer_init(&ptr_to_struct2, &struct_list_head, scc_pos_create()); // 构造变量声明 next 和 prev scc_ast_decl_t next_decl, prev_decl; scc_ast_decl_val_init(&next_decl, &ptr_to_struct1, "next", null, scc_pos_create()); scc_ast_decl_val_init(&prev_decl, &ptr_to_struct2, "prev", null, scc_pos_create()); // 构造声明列表 scc_ast_decl_vec_t decl_vec; scc_vec_init(decl_vec); scc_vec_push(decl_vec, &next_decl); scc_vec_push(decl_vec, &prev_decl); scc_ast_decl_t decl_list; scc_ast_decl_list_init(&decl_list, &decl_vec, scc_pos_create()); SCC_CHECK_AST(&decl_list.base, "struct list_head *next, *prev;", scc_parse_declaration); } // 测试 "typedef struct { int a; } struct_t, *struct_ptr_t;" { // 构造字段 int a; scc_ast_decl_t field_a; scc_ast_decl_val_init(&field_a, (scc_ast_type_t *)&int_type, "a", null, scc_pos_create()); scc_ast_decl_vec_t fields; scc_vec_init(fields); scc_vec_push(fields, &field_a); // 构造匿名结构体定义声明 scc_ast_decl_t struct_def; scc_ast_decl_struct_init(&struct_def, null, &fields, scc_pos_create()); // fields 被移动 // 构造匿名结构体类型 scc_ast_type_t anon_struct_type; scc_ast_type_struct_init(&anon_struct_type, null, &struct_def, scc_pos_create()); // 构造指针类型指向该匿名结构体 scc_ast_type_t ptr_to_anon; scc_ast_type_pointer_init(&ptr_to_anon, &anon_struct_type, scc_pos_create()); // 构造 typedef 声明 struct_t scc_ast_decl_t typedef_struct_t; scc_ast_decl_typedef_init(&typedef_struct_t, "struct_t", &anon_struct_type, scc_pos_create()); // 构造 typedef 声明 struct_ptr_t scc_ast_decl_t typedef_struct_ptr_t; scc_ast_decl_typedef_init(&typedef_struct_ptr_t, "struct_ptr_t", &ptr_to_anon, scc_pos_create()); // 构造声明列表 scc_ast_decl_vec_t typedef_vec; scc_vec_init(typedef_vec); scc_vec_push(typedef_vec, &typedef_struct_t); scc_vec_push(typedef_vec, &typedef_struct_ptr_t); scc_ast_decl_t decl_list; scc_ast_decl_list_init(&decl_list, &typedef_vec, scc_pos_create()); SCC_CHECK_AST(&decl_list.base, "typedef struct { int a; } struct_t, *struct_ptr_t;", scc_parse_declaration); } "__scc_builtin_va_arg(ag, int)"; "__scc_builtin_va_arg(ag, long long)"; "typedef struct a;int a;struct a b;"; } { // int (*(*)(void))[5] (指向函数的指针,该函数返回指向数组的指针) // 步骤: // 1) 数组类型:int [5] scc_ast_expr_t size_5; scc_ast_expr_literal_int_init(&size_5, "5", false, scc_pos_create()); scc_ast_type_t array_of_5_int; scc_ast_type_array_init(&array_of_5_int, (scc_ast_type_t *)&int_type, &size_5, scc_pos_create()); // 2) 函数类型:返回指向数组的指针,无参数 scc_ast_type_t ptr_to_array; scc_ast_type_pointer_init(&ptr_to_array, &array_of_5_int, scc_pos_create()); scc_ast_type_t func_type; scc_ast_decl_vec_t func_params; scc_ast_decl_t void_decl; scc_ast_decl_param_init(&void_decl, &void_type, null, scc_pos_create()); scc_ast_decl_t *array[] = {&void_decl}; scc_vec_unsafe_from_static_array(func_params, array); scc_ast_type_function_init(&func_type, &ptr_to_array, &func_params, scc_pos_create()); // 无参数 // 3) 指向该函数的指针 scc_ast_type_t ptr_to_func; scc_ast_type_pointer_init(&ptr_to_func, &func_type, scc_pos_create()); scc_ast_decl_t ptr_to_func_decl; scc_ast_decl_val_init(&ptr_to_func_decl, &ptr_to_func, "foo", null, scc_pos_create()); SCC_CHECK_AST(&ptr_to_func_decl.base, "int (*(*foo)(void))[5];", scc_parse_declaration); scc_ast_decl_t typedef_func_decl; scc_ast_decl_typedef_init(&typedef_func_decl, "func_t", &ptr_to_func, scc_pos_create()); scc_ast_type_t typedef_func_type; scc_ast_type_typedef_init(&typedef_func_type, "func_t", &typedef_func_decl, scc_pos_create()); scc_ast_decl_t func_hard_decl; scc_ast_type_t func_hard_type; scc_ast_decl_t param1; scc_ast_decl_param_init(¶m1, &ptr_to_func, "bar", scc_pos_create()); scc_ast_decl_t param2; scc_ast_decl_param_init(¶m2, &typedef_func_type, "a", scc_pos_create()); scc_ast_decl_t param3; scc_ast_decl_param_init(¶m1, &va_list_type, null, scc_pos_create()); scc_ast_decl_t *func_hard_array[] = {¶m1, ¶m2, ¶m3}; scc_ast_decl_vec_t func_hard_params; scc_vec_unsafe_from_static_array(func_hard_params, func_hard_array); scc_ast_type_function_init(&func_hard_type, &ptr_to_array, &func_hard_params, scc_pos_create()); scc_ast_decl_func_init(&func_hard_decl, &func_hard_type, "bar", null, scc_pos_create()); scc_ast_decl_vec_t decls; scc_ast_decl_t *decls_array[] = { &typedef_func_decl, &func_hard_decl, }; scc_vec_unsafe_from_static_array(decls, decls_array); scc_ast_translation_unit_t tu; scc_ast_translation_unit_init(&tu, &decls, scc_pos_create()); // SCC_CHECK_AST_WITH_SEMA( // &tu.base, // "typedef int (*(*func_t)(void))[5];" // "int (*(*(bar)(int(*(*foo)(void))[5], func_t a, // ...))(void))[5];", scc_parse_translation_unit); } } TEST_LIST = { {"parser_unit", test_parser_unit}, {null, null}, };