refactor(argparse): 将null替换为nullptr以提高C++兼容性
- 在argparse库中将所有null指针常量替换为nullptr - 更新头文件和源文件中的指针初始化和比较操作 - 修改测试文件中的相关断言检查 - 更新AST定义文件中的注释说明
This commit is contained in:
@@ -5,20 +5,20 @@
|
||||
|
||||
static inline const scc_lexer_tok_t *scc_parser_peek(scc_parser_t *parser) {
|
||||
cbool ok = false;
|
||||
const scc_lexer_tok_t *tok = null;
|
||||
const scc_lexer_tok_t *tok = nullptr;
|
||||
scc_ring_unsafe_peek_ref(*parser->ring, tok, ok);
|
||||
if (ok == false) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
return tok;
|
||||
}
|
||||
|
||||
static inline const scc_lexer_tok_t *scc_parser_next(scc_parser_t *parser) {
|
||||
cbool ok = false;
|
||||
const scc_lexer_tok_t *tok = null;
|
||||
const scc_lexer_tok_t *tok = nullptr;
|
||||
scc_ring_unsafe_next_ref(*parser->ring, tok, ok);
|
||||
if (ok == false) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
return tok;
|
||||
}
|
||||
@@ -26,10 +26,10 @@ static inline const scc_lexer_tok_t *scc_parser_next(scc_parser_t *parser) {
|
||||
static inline cbool scc_parser_consume_if(scc_parser_t *parser,
|
||||
scc_tok_type_t type) {
|
||||
cbool ok = false;
|
||||
scc_lexer_tok_t *tok = null;
|
||||
scc_lexer_tok_t *tok = nullptr;
|
||||
scc_ring_unsafe_peek_ref(*parser->ring, tok, ok);
|
||||
if (ok == false) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
if (tok->type == type) {
|
||||
scc_lexer_tok_drop(tok);
|
||||
@@ -48,13 +48,13 @@ static inline void scc_parser_restore(scc_parser_t *parser) {
|
||||
_scc_ring_probe(*parser->ring) = parser->checkpoint;
|
||||
}
|
||||
|
||||
// tok can null it will be safty free
|
||||
// tok can nullptr it will be safty free
|
||||
static inline cbool scc_parser_next_consume(scc_parser_t *parser,
|
||||
scc_lexer_tok_t *tok) {
|
||||
cbool ok = false;
|
||||
scc_lexer_tok_t *raw_tok_ref = null;
|
||||
scc_lexer_tok_t *raw_tok_ref = nullptr;
|
||||
scc_ring_unsafe_next_ref_consume(*parser->ring, raw_tok_ref, ok);
|
||||
if (tok == null) {
|
||||
if (tok == nullptr) {
|
||||
scc_lexer_tok_drop(raw_tok_ref);
|
||||
} else {
|
||||
scc_lexer_tok_move(tok, raw_tok_ref);
|
||||
@@ -75,7 +75,7 @@ static inline void scc_parser_reset(scc_parser_t *parser) {
|
||||
static inline scc_pos_t scc_parser_got_current_pos(scc_parser_t *parser) {
|
||||
const scc_lexer_tok_t *tok = scc_parser_peek(parser);
|
||||
scc_pos_t pos = scc_pos_create();
|
||||
if (tok != null)
|
||||
if (tok != nullptr)
|
||||
pos = tok->loc;
|
||||
return pos;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ typedef struct scc_parser {
|
||||
* @brief 初始化解析器
|
||||
* @param parser 解析器实例
|
||||
* @param lexer 词法分析器实例
|
||||
* @param callbacks 语义分析回调(可为 null)
|
||||
* @param callbacks 语义分析回调(可为 nullptr)
|
||||
*/
|
||||
void scc_parser_init(scc_parser_t *parser, scc_lexer_tok_ring_t *tok_ring,
|
||||
scc_sema_callbacks_t *callbacks);
|
||||
|
||||
@@ -173,10 +173,10 @@ scc_ast_expr_t *scc_parse_initializer(scc_parser_t *parser,
|
||||
[ constant-expression ]
|
||||
. identifier
|
||||
*/
|
||||
const scc_lexer_tok_t *tok_ptr = null;
|
||||
const scc_lexer_tok_t *tok_ptr = nullptr;
|
||||
scc_lexer_tok_t tok = {0};
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
scc_ast_expr_t *init = null;
|
||||
scc_ast_expr_t *init = nullptr;
|
||||
if (!(tok_ptr && tok_ptr->type == SCC_TOK_L_BRACE)) {
|
||||
// TODO int a = 1, b = 1;
|
||||
init = scc_parse_assignment_expression(parser);
|
||||
@@ -187,23 +187,23 @@ scc_ast_expr_t *scc_parse_initializer(scc_parser_t *parser,
|
||||
scc_lexer_tok_drop(&tok);
|
||||
|
||||
init = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(init != null);
|
||||
Assert(init != nullptr);
|
||||
scc_ast_expr_vec_t lhs_exprs;
|
||||
scc_vec_init(lhs_exprs);
|
||||
scc_ast_expr_vec_t rhs_exprs;
|
||||
scc_vec_init(rhs_exprs);
|
||||
scc_ast_expr_t *lhs = null;
|
||||
scc_ast_expr_t *rhs = null;
|
||||
scc_ast_expr_t *lhs = nullptr;
|
||||
scc_ast_expr_t *rhs = nullptr;
|
||||
scc_ast_expr_t *ptr = base;
|
||||
while (1) {
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr->type == SCC_TOK_DOT) {
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr && tok_ptr->type == SCC_TOK_IDENT) {
|
||||
scc_parser_next_consume(parser, &tok);
|
||||
lhs = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(lhs != null);
|
||||
Assert(lhs != nullptr);
|
||||
scc_ast_expr_member_init(lhs, ptr, scc_str_as_cstr(&tok.lexeme),
|
||||
tok.loc);
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_ASSIGN)) {
|
||||
@@ -211,7 +211,7 @@ scc_ast_expr_t *scc_parse_initializer(scc_parser_t *parser,
|
||||
continue;
|
||||
}
|
||||
rhs = scc_parse_initializer(parser, lhs);
|
||||
if (rhs == null) {
|
||||
if (rhs == nullptr) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Expected initializer");
|
||||
Panic();
|
||||
@@ -224,34 +224,34 @@ scc_ast_expr_t *scc_parse_initializer(scc_parser_t *parser,
|
||||
"Expected 'identifier' after '.'");
|
||||
}
|
||||
} else if (tok_ptr->type == SCC_TOK_L_BRACKET) {
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
scc_ast_expr_t *idx = scc_parser_constant_expression(parser);
|
||||
Assert(idx != null);
|
||||
Assert(idx != nullptr);
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_R_BRACKET)) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser), "Expected ']'");
|
||||
}
|
||||
lhs = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(lhs != null);
|
||||
Assert(lhs != nullptr);
|
||||
scc_ast_expr_array_subscript_init(lhs, ptr, idx, tok_ptr->loc);
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_ASSIGN)) {
|
||||
ptr = lhs;
|
||||
continue;
|
||||
}
|
||||
rhs = scc_parse_initializer(parser, lhs);
|
||||
Assert(rhs != null);
|
||||
Assert(rhs != nullptr);
|
||||
scc_vec_push(lhs_exprs, lhs);
|
||||
scc_vec_push(rhs_exprs, rhs);
|
||||
ptr = base;
|
||||
} else if (tok_ptr->type == SCC_TOK_R_BRACE) {
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
} else if (tok_ptr->type == SCC_TOK_COMMA) {
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
continue;
|
||||
} else {
|
||||
// FIXME
|
||||
scc_ast_expr_t *expr = scc_parse_initializer(parser, base);
|
||||
scc_vec_push(lhs_exprs, null);
|
||||
scc_vec_push(lhs_exprs, nullptr);
|
||||
scc_vec_push(rhs_exprs, expr);
|
||||
}
|
||||
}
|
||||
@@ -260,29 +260,29 @@ scc_ast_expr_t *scc_parse_initializer(scc_parser_t *parser,
|
||||
}
|
||||
|
||||
scc_ast_decl_t *scc_parse_declaration(scc_parser_t *parser) {
|
||||
const scc_lexer_tok_t *tok_ptr = null;
|
||||
const scc_lexer_tok_t *tok_ptr = nullptr;
|
||||
|
||||
scc_ast_decl_t *decl_list = null;
|
||||
scc_ast_decl_t *decl_list = nullptr;
|
||||
scc_ast_decl_vec_t decl_list_vec;
|
||||
scc_vec_init(decl_list_vec);
|
||||
|
||||
scc_ast_type_t *type = scc_parse_declaration_specifiers(parser);
|
||||
if (type == null) {
|
||||
return null;
|
||||
if (type == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
scc_ast_decl_specifier_t spec = type->quals;
|
||||
// FIXME drop typedef inline and ...
|
||||
type->quals.is_typedef = false;
|
||||
|
||||
scc_ast_decl_t *decl = null;
|
||||
scc_ast_decl_t *decl = nullptr;
|
||||
CONTINUE:
|
||||
decl = scc_parse_declarator(parser, type);
|
||||
if (decl == null) {
|
||||
return null;
|
||||
if (decl == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr->type == SCC_TOK_ASSIGN) {
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
// TODO maybe memory leak
|
||||
scc_ast_expr_t *lvalue = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
scc_ast_expr_lvalue_init(lvalue, decl->var.type, decl->base.loc);
|
||||
@@ -290,7 +290,7 @@ CONTINUE:
|
||||
} else if (tok_ptr->type == SCC_TOK_L_BRACE) {
|
||||
scc_parse_decl_sema(parser, decl);
|
||||
parser->sema_callbacks.on_decl(parser->sema_callbacks.context,
|
||||
scc_ast_decl_t_BEGIN, null);
|
||||
scc_ast_decl_t_BEGIN, nullptr);
|
||||
scc_vec_foreach(decl->func.type->function.params, i) {
|
||||
scc_ast_decl_t *param =
|
||||
scc_vec_at(decl->func.type->function.params, i);
|
||||
@@ -299,19 +299,19 @@ CONTINUE:
|
||||
}
|
||||
scc_ast_stmt_t *body = scc_parse_statement(parser);
|
||||
parser->sema_callbacks.on_decl(parser->sema_callbacks.context,
|
||||
scc_ast_decl_t_END, null);
|
||||
scc_ast_decl_t_END, nullptr);
|
||||
Assert(decl->base.type == SCC_AST_DECL_FUNC);
|
||||
decl->func.body = body;
|
||||
Assert(decl->func.type != null);
|
||||
Assert(decl->func.type != nullptr);
|
||||
Assert(decl->func.type->base.type == SCC_AST_TYPE_FUNCTION);
|
||||
Assert(decl->func.body != null);
|
||||
Assert(decl->func.body != nullptr);
|
||||
Assert(decl->func.body->base.type == SCC_AST_STMT_COMPOUND);
|
||||
goto RETURN;
|
||||
}
|
||||
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr->type == SCC_TOK_SEMICOLON) {
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
if (decl_list)
|
||||
scc_vec_push(decl_list_vec, decl);
|
||||
if (spec.is_typedef) {
|
||||
@@ -326,7 +326,7 @@ CONTINUE:
|
||||
decl->base.loc);
|
||||
}
|
||||
}
|
||||
if (decl_list != null) {
|
||||
if (decl_list != nullptr) {
|
||||
scc_vec_foreach(decl_list_vec, i) {
|
||||
decl = scc_vec_at(decl_list_vec, i);
|
||||
scc_parse_decl_sema(parser, decl);
|
||||
@@ -340,10 +340,10 @@ CONTINUE:
|
||||
}
|
||||
goto RETURN;
|
||||
} else if (tok_ptr->type == SCC_TOK_COMMA) {
|
||||
scc_parser_next_consume(parser, null);
|
||||
if (decl_list == null) {
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
if (decl_list == nullptr) {
|
||||
decl_list = scc_malloc(sizeof(scc_ast_decl_t));
|
||||
Assert(decl_list != null);
|
||||
Assert(decl_list != nullptr);
|
||||
scc_vec_push(decl_list_vec, decl);
|
||||
} else {
|
||||
Assert(scc_vec_size(decl_list_vec) != 0);
|
||||
@@ -361,5 +361,5 @@ CONTINUE:
|
||||
RETURN:
|
||||
return decl;
|
||||
ERROR:
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -324,7 +324,7 @@ static scc_ast_expr_op_t map_token_to_assign_op(scc_tok_type_t type) {
|
||||
// 跳过直到遇到同步 token(分号、右括号、逗号、EOF)
|
||||
static void parser_sync(scc_parser_t *parser) {
|
||||
const scc_lexer_tok_t *tok_ptr;
|
||||
while ((tok_ptr = scc_parser_peek(parser)) != null) {
|
||||
while ((tok_ptr = scc_parser_peek(parser)) != nullptr) {
|
||||
scc_tok_type_t type = tok_ptr->type;
|
||||
if (type == SCC_TOK_SEMICOLON || type == SCC_TOK_R_PAREN ||
|
||||
type == SCC_TOK_R_BRACE || type == SCC_TOK_COMMA ||
|
||||
@@ -344,7 +344,7 @@ static scc_ast_expr_t *parse_expression_with_precedence(scc_parser_t *parser,
|
||||
// 从最底层(cast-expression)开始
|
||||
scc_ast_expr_t *left = parse_cast_expression(parser);
|
||||
if (!left)
|
||||
return null;
|
||||
return nullptr;
|
||||
|
||||
while (1) {
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
@@ -367,11 +367,11 @@ static scc_ast_expr_t *parse_expression_with_precedence(scc_parser_t *parser,
|
||||
parse_expression_with_precedence(parser, prec + 1);
|
||||
if (!right) {
|
||||
parser_sync(parser);
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_expr_t *expr = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(expr != null);
|
||||
Assert(expr != nullptr);
|
||||
// FIXME pos
|
||||
scc_ast_expr_binary_init(expr, op, left, right, left->base.loc);
|
||||
left = expr;
|
||||
@@ -382,10 +382,10 @@ static scc_ast_expr_t *parse_expression_with_precedence(scc_parser_t *parser,
|
||||
scc_ast_expr_t *scc_parse_assignment_expression(scc_parser_t *parser) {
|
||||
// 先解析左侧的 unary-expression(C 标准规定赋值左边必须是
|
||||
// unary-expression)
|
||||
scc_ast_expr_t *left = null;
|
||||
scc_ast_expr_t *left = nullptr;
|
||||
left = parse_conditional_expression(parser);
|
||||
if (!left)
|
||||
return null;
|
||||
return nullptr;
|
||||
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
if (!tok_ptr)
|
||||
@@ -406,11 +406,11 @@ scc_ast_expr_t *scc_parse_assignment_expression(scc_parser_t *parser) {
|
||||
if (!right) {
|
||||
// 错误恢复
|
||||
parser_sync(parser);
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_expr_t *expr = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(expr != null);
|
||||
Assert(expr != nullptr);
|
||||
scc_ast_expr_binary_init(expr, op, left, right, pos);
|
||||
left = expr;
|
||||
}
|
||||
@@ -422,7 +422,7 @@ static scc_ast_expr_t *parse_conditional_expression(scc_parser_t *parser) {
|
||||
scc_ast_expr_t *cond_expr =
|
||||
parse_expression_with_precedence(parser, PREC_LOGICAL_OR);
|
||||
if (!cond_expr)
|
||||
return null;
|
||||
return nullptr;
|
||||
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr && tok_ptr->type == SCC_TOK_COND) {
|
||||
@@ -437,7 +437,7 @@ static scc_ast_expr_t *parse_conditional_expression(scc_parser_t *parser) {
|
||||
scc_ast_expr_t *then_expr = scc_parse_expression(parser);
|
||||
if (!then_expr) {
|
||||
parser_sync(parser);
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// 消耗 ':'
|
||||
@@ -445,18 +445,18 @@ static scc_ast_expr_t *parse_conditional_expression(scc_parser_t *parser) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Expected ':' after '?'");
|
||||
parser_sync(parser);
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// 解析 else 部分(右结合,再次调用 parse_conditional_expression)
|
||||
scc_ast_expr_t *else_expr = parse_conditional_expression(parser);
|
||||
if (!else_expr) {
|
||||
parser_sync(parser);
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_expr_t *cond = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(cond != null);
|
||||
Assert(cond != nullptr);
|
||||
scc_ast_expr_cond_init(cond, cond_expr, then_expr, else_expr, pos);
|
||||
cond_expr = cond;
|
||||
}
|
||||
@@ -466,7 +466,7 @@ static scc_ast_expr_t *parse_conditional_expression(scc_parser_t *parser) {
|
||||
// 类型转换表达式 (type-name) cast-expression
|
||||
static scc_ast_expr_t *parse_cast_expression(scc_parser_t *parser) {
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
scc_ast_type_t *type = null;
|
||||
scc_ast_type_t *type = nullptr;
|
||||
if (tok_ptr && tok_ptr->type == SCC_TOK_L_PAREN) {
|
||||
// 尝试解析类型名
|
||||
scc_parser_store(parser);
|
||||
@@ -487,7 +487,7 @@ static scc_ast_expr_t *parse_cast_expression(scc_parser_t *parser) {
|
||||
return operand;
|
||||
}
|
||||
scc_ast_expr_t *expr = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(expr != null);
|
||||
Assert(expr != nullptr);
|
||||
// FIXME pos
|
||||
scc_ast_expr_cast_init(expr, type, operand, type->base.loc);
|
||||
return expr;
|
||||
@@ -523,7 +523,7 @@ static scc_ast_expr_t *parse_unary_expression(scc_parser_t *parser) {
|
||||
*/
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
if (!tok_ptr)
|
||||
return null;
|
||||
return nullptr;
|
||||
|
||||
scc_lexer_tok_t tok = {0};
|
||||
|
||||
@@ -539,14 +539,14 @@ static scc_ast_expr_t *parse_unary_expression(scc_parser_t *parser) {
|
||||
case SCC_TOK_NOT: // !x
|
||||
{
|
||||
if (!scc_parser_next_consume(parser, &tok))
|
||||
return null;
|
||||
return nullptr;
|
||||
scc_ast_expr_op_t op = map_token_to_unary_op(tok.type, true);
|
||||
scc_pos_t pos = tok.loc;
|
||||
scc_lexer_tok_drop(&tok);
|
||||
|
||||
// 一元运算符右结合,递归调用 parse_unary_expression
|
||||
|
||||
scc_ast_expr_t *operand = null;
|
||||
scc_ast_expr_t *operand = nullptr;
|
||||
if (tok_ptr->type == SCC_TOK_ADD_ADD ||
|
||||
tok_ptr->type == SCC_TOK_SUB_SUB) {
|
||||
operand = parse_unary_expression(parser);
|
||||
@@ -555,11 +555,11 @@ static scc_ast_expr_t *parse_unary_expression(scc_parser_t *parser) {
|
||||
}
|
||||
if (!operand) {
|
||||
parser_sync(parser);
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_expr_t *expr = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(expr != null);
|
||||
Assert(expr != nullptr);
|
||||
scc_ast_expr_unary_init(expr, op, operand, pos);
|
||||
return expr;
|
||||
}
|
||||
@@ -575,7 +575,7 @@ static scc_ast_expr_t *parse_sizeof_expression(scc_parser_t *parser) {
|
||||
scc_lexer_tok_t tok_ptr;
|
||||
if (!scc_parser_next_consume(parser, &tok_ptr) ||
|
||||
tok_ptr.type != SCC_TOK_SIZEOF) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
scc_pos_t pos = tok_ptr.loc;
|
||||
scc_lexer_tok_drop(&tok_ptr);
|
||||
@@ -584,7 +584,7 @@ static scc_ast_expr_t *parse_sizeof_expression(scc_parser_t *parser) {
|
||||
if (!next) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Unexpected end after sizeof");
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_expr_t *expr = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
@@ -593,7 +593,7 @@ static scc_ast_expr_t *parse_sizeof_expression(scc_parser_t *parser) {
|
||||
scc_parser_store(parser);
|
||||
scc_parser_next(parser);
|
||||
scc_ast_type_t *type_name = scc_parse_type_name(parser);
|
||||
if (type_name == null) {
|
||||
if (type_name == nullptr) {
|
||||
scc_parser_restore(parser);
|
||||
goto next;
|
||||
}
|
||||
@@ -603,15 +603,15 @@ static scc_ast_expr_t *parse_sizeof_expression(scc_parser_t *parser) {
|
||||
"expected ')' after type-name in sizeof expression");
|
||||
}
|
||||
|
||||
Assert(type_name != null);
|
||||
scc_ast_expr_sizeof_init(expr, type_name, null, pos);
|
||||
Assert(type_name != nullptr);
|
||||
scc_ast_expr_sizeof_init(expr, type_name, nullptr, pos);
|
||||
return expr;
|
||||
}
|
||||
next:
|
||||
// 尝试解析 sizeof unary-expression
|
||||
scc_ast_expr_t *operand = parse_unary_expression(parser);
|
||||
if (operand != null) {
|
||||
scc_ast_expr_sizeof_init(expr, null, operand, pos);
|
||||
if (operand != nullptr) {
|
||||
scc_ast_expr_sizeof_init(expr, nullptr, operand, pos);
|
||||
return expr;
|
||||
}
|
||||
|
||||
@@ -622,12 +622,12 @@ next:
|
||||
// 后缀表达式
|
||||
static scc_ast_expr_t *parse_postfix_expression(scc_parser_t *parser) {
|
||||
scc_ast_expr_t *left = parse_primary_expression(parser);
|
||||
const scc_lexer_tok_t *tok_ptr = null;
|
||||
const scc_lexer_tok_t *tok_ptr = nullptr;
|
||||
scc_pos_t pos = scc_pos_create();
|
||||
if (!left) {
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (!(tok_ptr && tok_ptr->type == SCC_TOK_L_PAREN)) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
pos = tok_ptr->loc;
|
||||
|
||||
@@ -636,7 +636,7 @@ static scc_ast_expr_t *parse_postfix_expression(scc_parser_t *parser) {
|
||||
scc_ast_type_t *type = scc_parse_type_name(parser);
|
||||
if (!type) {
|
||||
scc_parser_restore(parser);
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
scc_parser_commit(parser);
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_R_PAREN)) {
|
||||
@@ -656,30 +656,30 @@ static scc_ast_expr_t *parse_postfix_expression(scc_parser_t *parser) {
|
||||
switch (tok_ptr->type) {
|
||||
case SCC_TOK_L_BRACKET: // left[expr]
|
||||
{
|
||||
if (!scc_parser_next_consume(parser, null))
|
||||
if (!scc_parser_next_consume(parser, nullptr))
|
||||
return left;
|
||||
pos = left->base.loc;
|
||||
|
||||
scc_ast_expr_t *index = scc_parse_expression(parser);
|
||||
if (!index) {
|
||||
parser_sync(parser);
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_R_BRACKET)) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Expected ']' after subscript");
|
||||
parser_sync(parser);
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
scc_ast_expr_t *subscript = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(subscript != null);
|
||||
Assert(subscript != nullptr);
|
||||
scc_ast_expr_array_subscript_init(subscript, left, index, pos);
|
||||
left = subscript;
|
||||
break;
|
||||
}
|
||||
case SCC_TOK_L_PAREN: // left(args)
|
||||
{
|
||||
if (!scc_parser_next_consume(parser, null))
|
||||
if (!scc_parser_next_consume(parser, nullptr))
|
||||
return left;
|
||||
pos = left->base.loc;
|
||||
scc_ast_expr_vec_t args;
|
||||
@@ -705,11 +705,11 @@ static scc_ast_expr_t *parse_postfix_expression(scc_parser_t *parser) {
|
||||
// 释放已解析的参数
|
||||
// TODO: 释放 args 中的表达式
|
||||
parser_sync(parser);
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
scc_ast_expr_t *call = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(call != null);
|
||||
Assert(call != nullptr);
|
||||
scc_ast_expr_call_init(call, left, &args, pos);
|
||||
left = call;
|
||||
break;
|
||||
@@ -726,12 +726,12 @@ static scc_ast_expr_t *parse_postfix_expression(scc_parser_t *parser) {
|
||||
"Expected identifier after member access");
|
||||
scc_lexer_tok_drop(&op_tok);
|
||||
parser_sync(parser);
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
const char *name = scc_str_as_cstr(&ident_tok.lexeme);
|
||||
|
||||
scc_ast_expr_t *member = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(member != null);
|
||||
Assert(member != nullptr);
|
||||
if (op_tok.type == SCC_TOK_DOT) {
|
||||
scc_ast_expr_member_init(member, left, name, ident_tok.loc);
|
||||
} else {
|
||||
@@ -750,7 +750,7 @@ static scc_ast_expr_t *parse_postfix_expression(scc_parser_t *parser) {
|
||||
return left;
|
||||
scc_ast_expr_op_t op = map_token_to_unary_op(op_tok.type, false);
|
||||
scc_ast_expr_t *expr = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(expr != null);
|
||||
Assert(expr != nullptr);
|
||||
scc_ast_expr_unary_init(expr, op, left, op_tok.loc);
|
||||
scc_lexer_tok_drop(&op_tok);
|
||||
left = expr;
|
||||
@@ -776,43 +776,43 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
|
||||
*/
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
if (!tok_ptr)
|
||||
return null;
|
||||
return nullptr;
|
||||
|
||||
scc_lexer_tok_t tok = {0};
|
||||
scc_ast_expr_t *expr = null;
|
||||
scc_ast_expr_t *expr = nullptr;
|
||||
switch (tok_ptr->type) {
|
||||
case SCC_TOK_IDENT: {
|
||||
if (!scc_parser_next_consume(parser, &tok))
|
||||
return null;
|
||||
return nullptr;
|
||||
expr = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(expr != null);
|
||||
Assert(expr != nullptr);
|
||||
scc_ast_expr_identifier_init(expr, scc_str_as_cstr(&tok.lexeme),
|
||||
tok.loc);
|
||||
break;
|
||||
}
|
||||
case SCC_TOK_INT_LITERAL: {
|
||||
if (!scc_parser_next_consume(parser, &tok))
|
||||
return null;
|
||||
return nullptr;
|
||||
expr = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(expr != null);
|
||||
Assert(expr != nullptr);
|
||||
scc_ast_expr_literal_int_init(expr, scc_str_as_cstr(&tok.lexeme), false,
|
||||
tok.loc);
|
||||
break;
|
||||
}
|
||||
case SCC_TOK_FLOAT_LITERAL: {
|
||||
if (!scc_parser_next_consume(parser, &tok))
|
||||
return null;
|
||||
return nullptr;
|
||||
expr = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(expr != null);
|
||||
Assert(expr != nullptr);
|
||||
scc_ast_expr_literal_float_init(expr, scc_str_as_cstr(&tok.lexeme),
|
||||
false, tok.loc);
|
||||
break;
|
||||
}
|
||||
case SCC_TOK_CHAR_LITERAL: {
|
||||
if (!scc_parser_next_consume(parser, &tok))
|
||||
return null;
|
||||
return nullptr;
|
||||
expr = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(expr != null);
|
||||
Assert(expr != nullptr);
|
||||
scc_ast_expr_literal_char_init(expr, scc_str_as_cstr(&tok.lexeme),
|
||||
false, tok.loc);
|
||||
break;
|
||||
@@ -822,7 +822,7 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
|
||||
scc_lexer_tok_t tok;
|
||||
while (1) {
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
break;
|
||||
}
|
||||
if (tok_ptr->type != SCC_TOK_STRING_LITERAL) {
|
||||
@@ -835,14 +835,14 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
|
||||
}
|
||||
|
||||
expr = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(expr != null);
|
||||
Assert(expr != nullptr);
|
||||
// FIXME loc
|
||||
scc_ast_expr_literal_string_init(expr, scc_str_as_cstr(&string), true,
|
||||
tok.loc);
|
||||
break;
|
||||
}
|
||||
case SCC_TOK_L_PAREN:
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
expr = scc_parse_expression(parser);
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_R_PAREN)) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
@@ -853,8 +853,8 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (expr == null) {
|
||||
return null;
|
||||
if (expr == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
scc_parse_expr_sema(parser, expr);
|
||||
return expr;
|
||||
@@ -863,24 +863,24 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
|
||||
scc_ast_expr_t *scc_parse_expression(scc_parser_t *parser) {
|
||||
scc_ast_expr_t *left = scc_parse_assignment_expression(parser);
|
||||
if (!left)
|
||||
return null;
|
||||
return nullptr;
|
||||
|
||||
const scc_lexer_tok_t *tok_ptr = null;
|
||||
const scc_lexer_tok_t *tok_ptr = nullptr;
|
||||
while (1) {
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null || tok_ptr->type != SCC_TOK_COMMA) {
|
||||
if (tok_ptr == nullptr || tok_ptr->type != SCC_TOK_COMMA) {
|
||||
break;
|
||||
}
|
||||
scc_pos_t pos = tok_ptr->loc;
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
|
||||
scc_ast_expr_t *right = scc_parse_assignment_expression(parser);
|
||||
if (!right) {
|
||||
parser_sync(parser);
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
scc_ast_expr_t *expr = scc_malloc(sizeof(scc_ast_expr_t));
|
||||
Assert(expr != null);
|
||||
Assert(expr != nullptr);
|
||||
scc_ast_expr_binary_init(expr, SCC_AST_OP_COMMA, left, right, pos);
|
||||
left = expr;
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ A.2.3 Statements
|
||||
|
||||
static inline scc_ast_stmt_t *ast_stmt_alloc() {
|
||||
scc_ast_stmt_t *stmt = (scc_ast_stmt_t *)scc_malloc(sizeof(scc_ast_stmt_t));
|
||||
if (stmt == null) {
|
||||
if (stmt == nullptr) {
|
||||
LOG_FATAL("Out of memory");
|
||||
}
|
||||
return stmt;
|
||||
@@ -77,7 +77,7 @@ static scc_ast_stmt_t *parse_label_statement(scc_parser_t *parser,
|
||||
scc_pos_t pos) {
|
||||
scc_lexer_tok_t tok = {0};
|
||||
if (!scc_parser_next_consume(parser, &tok)) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_COLON)) {
|
||||
@@ -86,12 +86,12 @@ static scc_ast_stmt_t *parse_label_statement(scc_parser_t *parser,
|
||||
}
|
||||
|
||||
scc_ast_stmt_t *statement = scc_parse_statement(parser);
|
||||
if (statement == null) {
|
||||
if (statement == nullptr) {
|
||||
Panic("expect stmt");
|
||||
}
|
||||
|
||||
scc_ast_stmt_t *stmt = ast_stmt_alloc();
|
||||
Assert(stmt != null);
|
||||
Assert(stmt != nullptr);
|
||||
scc_ast_stmt_label_init(stmt, scc_str_as_cstr(&tok.lexeme), statement, pos);
|
||||
return stmt;
|
||||
}
|
||||
@@ -99,25 +99,25 @@ static scc_ast_stmt_t *parse_label_statement(scc_parser_t *parser,
|
||||
static scc_ast_stmt_t *parse_case_statement(scc_parser_t *parser,
|
||||
scc_pos_t pos) {
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_CASE)) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_expr_t *expr = null;
|
||||
scc_ast_expr_t *expr = nullptr;
|
||||
expr = scc_parser_constant_expression(parser);
|
||||
if (expr == null) {
|
||||
if (expr == nullptr) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Expected constant expression after case.");
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_COLON)) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Expected `:` after case.");
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_stmt_t *statement = scc_parse_statement(parser);
|
||||
if (statement == null) {
|
||||
if (statement == nullptr) {
|
||||
Panic("expect stmt");
|
||||
}
|
||||
|
||||
@@ -129,17 +129,17 @@ static scc_ast_stmt_t *parse_case_statement(scc_parser_t *parser,
|
||||
static scc_ast_stmt_t *parse_default_statement(scc_parser_t *parser,
|
||||
scc_pos_t pos) {
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_DEFAULT)) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_COLON)) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Expected constant expression after case.");
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_stmt_t *statement = scc_parse_statement(parser);
|
||||
if (statement == null) {
|
||||
if (statement == nullptr) {
|
||||
Panic("expect stmt");
|
||||
}
|
||||
|
||||
@@ -151,31 +151,31 @@ static scc_ast_stmt_t *parse_default_statement(scc_parser_t *parser,
|
||||
static scc_ast_stmt_t *parse_compound_statement(scc_parser_t *parser,
|
||||
scc_pos_t pos) {
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_L_BRACE)) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
scc_ast_block_item_vec_t block_items;
|
||||
scc_vec_init(block_items);
|
||||
|
||||
parser->sema_callbacks.on_stmt(parser->sema_callbacks.context,
|
||||
scc_ast_stmt_t_BEGIN, null);
|
||||
scc_ast_stmt_t_BEGIN, nullptr);
|
||||
while (!scc_parser_consume_if(parser, SCC_TOK_R_BRACE)) {
|
||||
/// TODO
|
||||
// scc_parse_is_decl();
|
||||
scc_ast_node_t *ret = null;
|
||||
scc_ast_node_t *ret = nullptr;
|
||||
ret = (scc_ast_node_t *)scc_parse_declaration(parser);
|
||||
if (ret == null) {
|
||||
if (ret == nullptr) {
|
||||
ret = (scc_ast_node_t *)scc_parse_statement(parser);
|
||||
}
|
||||
if (ret == null) {
|
||||
if (ret == nullptr) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser), "Invalid statement");
|
||||
// TODO free
|
||||
parser->errcode = 1;
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
scc_vec_push(block_items, ret);
|
||||
}
|
||||
parser->sema_callbacks.on_stmt(parser->sema_callbacks.context,
|
||||
scc_ast_stmt_t_END, null);
|
||||
scc_ast_stmt_t_END, nullptr);
|
||||
|
||||
scc_ast_stmt_t *stmt = ast_stmt_alloc();
|
||||
scc_ast_stmt_compound_init(stmt, &block_items, pos);
|
||||
@@ -184,17 +184,17 @@ static scc_ast_stmt_t *parse_compound_statement(scc_parser_t *parser,
|
||||
|
||||
static scc_ast_stmt_t *parse_if_statement(scc_parser_t *parser, scc_pos_t pos) {
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_IF)) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_expr_t *expression = ast_parse_paren_expression(parser);
|
||||
scc_ast_stmt_t *statement = scc_parse_statement(parser);
|
||||
scc_ast_stmt_t *opt_else = null;
|
||||
scc_ast_stmt_t *opt_else = nullptr;
|
||||
|
||||
if (scc_parser_consume_if(parser, SCC_TOK_ELSE)) {
|
||||
opt_else = scc_parse_statement(parser);
|
||||
} else {
|
||||
opt_else = null;
|
||||
opt_else = nullptr;
|
||||
}
|
||||
|
||||
scc_ast_stmt_t *stmt = ast_stmt_alloc();
|
||||
@@ -205,7 +205,7 @@ static scc_ast_stmt_t *parse_if_statement(scc_parser_t *parser, scc_pos_t pos) {
|
||||
static scc_ast_stmt_t *parse_switch_statement(scc_parser_t *parser,
|
||||
scc_pos_t pos) {
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_SWITCH)) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_expr_t *expression = ast_parse_paren_expression(parser);
|
||||
@@ -219,7 +219,7 @@ static scc_ast_stmt_t *parse_switch_statement(scc_parser_t *parser,
|
||||
static scc_ast_stmt_t *parse_while_statement(scc_parser_t *parser,
|
||||
scc_pos_t pos) {
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_WHILE)) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_expr_t *expression = ast_parse_paren_expression(parser);
|
||||
@@ -233,7 +233,7 @@ static scc_ast_stmt_t *parse_while_statement(scc_parser_t *parser,
|
||||
static scc_ast_stmt_t *parse_do_while_statement(scc_parser_t *parser,
|
||||
scc_pos_t pos) {
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_DO)) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_stmt_t *statement = scc_parse_statement(parser);
|
||||
@@ -243,7 +243,7 @@ static scc_ast_stmt_t *parse_do_while_statement(scc_parser_t *parser,
|
||||
"Expected 'while' after do.");
|
||||
// TODO 使用更好的错误处理,未来应当采用更好的内存管理器
|
||||
scc_free(statement);
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
scc_ast_expr_t *expression = ast_parse_paren_expression(parser);
|
||||
|
||||
@@ -255,7 +255,7 @@ static scc_ast_stmt_t *parse_do_while_statement(scc_parser_t *parser,
|
||||
static scc_ast_stmt_t *parse_for_statement(scc_parser_t *parser,
|
||||
scc_pos_t pos) {
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_FOR)) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -268,14 +268,14 @@ static scc_ast_stmt_t *parse_for_statement(scc_parser_t *parser,
|
||||
"Expected '(' before like `( expression )` .");
|
||||
}
|
||||
|
||||
scc_ast_node_t *init = null;
|
||||
scc_ast_expr_t *cond = null;
|
||||
scc_ast_expr_t *incr = null;
|
||||
scc_ast_stmt_t *body = null;
|
||||
scc_ast_node_t *init = nullptr;
|
||||
scc_ast_expr_t *cond = nullptr;
|
||||
scc_ast_expr_t *incr = nullptr;
|
||||
scc_ast_stmt_t *body = nullptr;
|
||||
|
||||
// TODO use decl or expr
|
||||
init = (scc_ast_node_t *)scc_parse_declaration(parser);
|
||||
if (init == null) {
|
||||
if (init == nullptr) {
|
||||
init = (scc_ast_node_t *)scc_parse_expression(parser);
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_SEMICOLON)) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
@@ -338,13 +338,13 @@ static scc_ast_stmt_t *parse_expression_statement(scc_parser_t *parser,
|
||||
|
||||
if (scc_parser_consume_if(parser, SCC_TOK_SEMICOLON)) {
|
||||
scc_ast_stmt_t *stmt = ast_stmt_alloc();
|
||||
scc_ast_stmt_expr_init(stmt, null, pos);
|
||||
scc_ast_stmt_expr_init(stmt, nullptr, pos);
|
||||
return stmt;
|
||||
}
|
||||
|
||||
scc_ast_expr_t *expr = scc_parse_expression(parser);
|
||||
if (expr == null) {
|
||||
return null;
|
||||
if (expr == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_stmt_t *stmt = ast_stmt_alloc();
|
||||
@@ -362,7 +362,7 @@ scc_ast_stmt_t *scc_parse_statement(scc_parser_t *parser) {
|
||||
const scc_lexer_tok_t *tok_ref;
|
||||
tok_ref = scc_parser_peek(parser);
|
||||
if (!tok_ref) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
scc_pos_t pos = tok_ref->loc;
|
||||
switch (tok_ref->type) {
|
||||
@@ -376,7 +376,7 @@ scc_ast_stmt_t *scc_parse_statement(scc_parser_t *parser) {
|
||||
case SCC_TOK_IDENT:
|
||||
scc_parser_next(parser);
|
||||
tok_ref = scc_parser_next(parser);
|
||||
if (tok_ref == null || tok_ref->type != SCC_TOK_COLON) {
|
||||
if (tok_ref == nullptr || tok_ref->type != SCC_TOK_COLON) {
|
||||
scc_parser_reset(parser);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -177,7 +177,7 @@ parse_direct_abstract_declarator(scc_parser_t *parser, scc_ast_type_t *base,
|
||||
static inline scc_ast_type_t *ast_type_alloc() {
|
||||
scc_ast_type_t *ast_type = scc_malloc(sizeof(scc_ast_type_t));
|
||||
ast_type->base.type = SCC_AST_UNKNOWN;
|
||||
if (ast_type == null) {
|
||||
if (ast_type == nullptr) {
|
||||
LOG_FATAL("Out of memory");
|
||||
}
|
||||
return ast_type;
|
||||
@@ -194,7 +194,7 @@ static inline scc_ast_type_t *ast_type_alloc() {
|
||||
*/
|
||||
cbool scc_parse_is_decl_specifier_start(scc_parser_t *parser) {
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
return false;
|
||||
}
|
||||
switch (tok_ptr->type) {
|
||||
@@ -230,7 +230,7 @@ cbool scc_parse_is_decl_specifier_start(scc_parser_t *parser) {
|
||||
// typedef 名称(标识符也可能是类型说明符)
|
||||
case SCC_TOK_IDENT:
|
||||
return scc_parse_got_type(parser, scc_str_as_cstr(&tok_ptr->lexeme)) !=
|
||||
null;
|
||||
nullptr;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -238,7 +238,7 @@ cbool scc_parse_is_decl_specifier_start(scc_parser_t *parser) {
|
||||
|
||||
cbool scc_parse_is_type_specifier_start(scc_parser_t *parser) {
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
return false;
|
||||
}
|
||||
switch (tok_ptr->type) {
|
||||
@@ -261,7 +261,7 @@ cbool scc_parse_is_type_specifier_start(scc_parser_t *parser) {
|
||||
case SCC_TOK_IDENT:
|
||||
// 需要检查标识符是否在符号表中定义为 typedef
|
||||
return scc_parse_got_type(parser, scc_str_as_cstr(&tok_ptr->lexeme)) !=
|
||||
null;
|
||||
nullptr;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -269,7 +269,7 @@ cbool scc_parse_is_type_specifier_start(scc_parser_t *parser) {
|
||||
|
||||
cbool scc_parse_is_type_qualifier_start(scc_parser_t *parser) {
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
return false;
|
||||
}
|
||||
switch (tok_ptr->type) {
|
||||
@@ -285,7 +285,7 @@ cbool scc_parse_is_type_qualifier_start(scc_parser_t *parser) {
|
||||
|
||||
cbool scc_parse_is_storage_class_start(scc_parser_t *parser) {
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
return false;
|
||||
}
|
||||
switch (tok_ptr->type) {
|
||||
@@ -320,10 +320,10 @@ parse_type_qualifier_list(scc_parser_t *parser,
|
||||
type-qualifier
|
||||
type-qualifier-list type-qualifier
|
||||
*/
|
||||
const scc_lexer_tok_t *tok_ptr = null;
|
||||
const scc_lexer_tok_t *tok_ptr = nullptr;
|
||||
CONTINUE:
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
return quals;
|
||||
}
|
||||
switch (tok_ptr->type) {
|
||||
@@ -340,7 +340,7 @@ CONTINUE:
|
||||
default:
|
||||
return quals;
|
||||
}
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
goto CONTINUE;
|
||||
duplicate_error:
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser), "Duplicate type specifier");
|
||||
@@ -350,10 +350,10 @@ duplicate_error:
|
||||
static scc_ast_decl_specifier_t
|
||||
parse_declaration_specifiers_list(scc_parser_t *parser,
|
||||
scc_ast_decl_specifier_t quals) {
|
||||
const scc_lexer_tok_t *tok_ptr = null;
|
||||
const scc_lexer_tok_t *tok_ptr = nullptr;
|
||||
CONTINUE:
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
return quals;
|
||||
}
|
||||
switch (tok_ptr->type) {
|
||||
@@ -385,7 +385,7 @@ CONTINUE:
|
||||
default:
|
||||
return quals;
|
||||
}
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
goto CONTINUE;
|
||||
duplicate_error:
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser), "Duplicate type specifier");
|
||||
@@ -459,7 +459,7 @@ static cbool check_type_combinations(scc_parser_t *parser,
|
||||
return false;
|
||||
}
|
||||
// 如果用户定义了类型(struct/typedef),不能与任何其他类型说明符混合
|
||||
if (info->user_type != null && basic_count > 0) {
|
||||
if (info->user_type != nullptr && basic_count > 0) {
|
||||
SCC_ERROR(
|
||||
scc_parser_got_current_pos(parser),
|
||||
"Cannot combine user-defined type with basic type specifiers");
|
||||
@@ -574,12 +574,12 @@ static scc_ast_type_t *parse_record_type(scc_parser_t *parser,
|
||||
scc_lexer_tok_drop(&tok);
|
||||
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser), "Unexpected EOF");
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
const char *name = null;
|
||||
scc_ast_decl_t *decl = null;
|
||||
const char *name = nullptr;
|
||||
scc_ast_decl_t *decl = nullptr;
|
||||
scc_ast_decl_vec_t member;
|
||||
scc_vec_init(member);
|
||||
|
||||
@@ -590,19 +590,19 @@ static scc_ast_type_t *parse_record_type(scc_parser_t *parser,
|
||||
}
|
||||
|
||||
if (tok_ptr && tok_ptr->type == SCC_TOK_L_BRACE) {
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
while (1) {
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Unexpected EOF in enum specifier");
|
||||
break;
|
||||
}
|
||||
|
||||
scc_ast_type_t *type = scc_parse_declaration_specifiers(parser);
|
||||
if (type != null) {
|
||||
if (type != nullptr) {
|
||||
decl = scc_parse_declarator(parser, type);
|
||||
if (decl != null) {
|
||||
if (decl != nullptr) {
|
||||
scc_vec_push(member, decl);
|
||||
continue;
|
||||
}
|
||||
@@ -610,20 +610,20 @@ static scc_ast_type_t *parse_record_type(scc_parser_t *parser,
|
||||
|
||||
if (tok_ptr->type == SCC_TOK_SEMICOLON) {
|
||||
// FIXME check semicolon
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
} else if (tok_ptr->type == SCC_TOK_R_BRACE) {
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
} else {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Unexpected token in struct/union specifier");
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
decl = scc_malloc(sizeof(scc_ast_decl_t));
|
||||
Assert(decl != null);
|
||||
Assert(decl != nullptr);
|
||||
if (type_kind == SCC_AST_TYPE_STRUCT) {
|
||||
scc_ast_decl_struct_init(decl, name, &member, pos);
|
||||
} else {
|
||||
@@ -631,11 +631,11 @@ static scc_ast_type_t *parse_record_type(scc_parser_t *parser,
|
||||
}
|
||||
scc_parse_decl_sema(parser, decl);
|
||||
} else {
|
||||
if (name == null) {
|
||||
if (name == nullptr) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Expected name in struct/union specifier");
|
||||
// FIXME memory leak
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -665,18 +665,18 @@ static scc_ast_type_t *parse_enum_type(scc_parser_t *parser) {
|
||||
scc_pos_t pos = tok.loc;
|
||||
if (tok.type != SCC_TOK_ENUM) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser), "Expected 'enum'");
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
scc_lexer_tok_drop(&tok);
|
||||
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser), "Unexpected EOF");
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
scc_ast_type_t *type = ast_type_alloc();
|
||||
const char *name = null;
|
||||
scc_ast_decl_t *decl = null;
|
||||
const char *name = nullptr;
|
||||
scc_ast_decl_t *decl = nullptr;
|
||||
scc_ast_decl_vec_t member;
|
||||
scc_vec_init(member);
|
||||
|
||||
@@ -687,10 +687,10 @@ static scc_ast_type_t *parse_enum_type(scc_parser_t *parser) {
|
||||
}
|
||||
|
||||
if (tok_ptr && tok_ptr->type == SCC_TOK_L_BRACE) {
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
while (1) {
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Unexpected EOF in enum specifier");
|
||||
break;
|
||||
@@ -703,13 +703,13 @@ static scc_ast_type_t *parse_enum_type(scc_parser_t *parser) {
|
||||
break;
|
||||
}
|
||||
|
||||
scc_ast_expr_t *enum_item_init = null;
|
||||
scc_ast_expr_t *enum_item_init = nullptr;
|
||||
if (scc_parser_consume_if(parser, SCC_TOK_ASSIGN)) {
|
||||
enum_item_init = scc_parser_constant_expression(parser);
|
||||
}
|
||||
|
||||
scc_ast_decl_t *enum_item_decl = scc_malloc(sizeof(scc_ast_decl_t));
|
||||
Assert(enum_item_decl != null);
|
||||
Assert(enum_item_decl != nullptr);
|
||||
scc_ast_decl_val_init(enum_item_decl, type,
|
||||
scc_str_as_cstr(&tok.lexeme), enum_item_init,
|
||||
tok.loc);
|
||||
@@ -719,34 +719,35 @@ static scc_ast_type_t *parse_enum_type(scc_parser_t *parser) {
|
||||
|
||||
cbool got_comma = false;
|
||||
if (tok_ptr && tok_ptr->type == SCC_TOK_COMMA) {
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
got_comma = true;
|
||||
}
|
||||
if (tok_ptr && tok_ptr->type == SCC_TOK_R_BRACE) {
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
} else {
|
||||
if (got_comma) {
|
||||
continue;
|
||||
}
|
||||
SCC_ERROR(tok_ptr != null ? tok_ptr->loc
|
||||
: scc_parser_got_current_pos(parser),
|
||||
SCC_ERROR(tok_ptr != nullptr
|
||||
? tok_ptr->loc
|
||||
: scc_parser_got_current_pos(parser),
|
||||
"Unexpected token in enum specifier");
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
decl = scc_malloc(sizeof(scc_ast_decl_t));
|
||||
Assert(decl != null);
|
||||
Assert(decl != nullptr);
|
||||
scc_ast_decl_enum_init(decl, name, &member, pos);
|
||||
scc_parse_decl_sema(parser, decl);
|
||||
} else {
|
||||
if (name == null) {
|
||||
if (name == nullptr) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Expected name in enum specifier");
|
||||
// FIXME memory leak
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -757,11 +758,11 @@ static scc_ast_type_t *parse_enum_type(scc_parser_t *parser) {
|
||||
static scc_ast_type_t *parse_type_specifier(scc_parser_t *parser) {
|
||||
type_spec_info_t info = {0};
|
||||
if (!scc_parse_is_type_specifier_start(parser))
|
||||
return null;
|
||||
const scc_lexer_tok_t *tok_ptr = null;
|
||||
return nullptr;
|
||||
const scc_lexer_tok_t *tok_ptr = nullptr;
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
return null;
|
||||
if (tok_ptr == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
scc_pos_t pos = tok_ptr->loc;
|
||||
|
||||
@@ -778,13 +779,13 @@ static scc_ast_type_t *parse_type_specifier(scc_parser_t *parser) {
|
||||
case SCC_TOK_IDENT:
|
||||
info.user_type =
|
||||
scc_parse_got_type(parser, scc_str_as_cstr(&tok_ptr->lexeme));
|
||||
if (info.user_type == null) {
|
||||
if (info.user_type == nullptr) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Expected type specifier %s",
|
||||
scc_str_as_cstr(&tok_ptr->lexeme));
|
||||
}
|
||||
scc_parser_next_consume(parser, null);
|
||||
Assert(info.user_type != null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
Assert(info.user_type != nullptr);
|
||||
goto done;
|
||||
default:
|
||||
break;
|
||||
@@ -792,7 +793,7 @@ static scc_ast_type_t *parse_type_specifier(scc_parser_t *parser) {
|
||||
|
||||
while (1) {
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
break;
|
||||
}
|
||||
switch (tok_ptr->type) {
|
||||
@@ -800,32 +801,32 @@ static scc_ast_type_t *parse_type_specifier(scc_parser_t *parser) {
|
||||
if (info.is_void)
|
||||
goto duplicate_error;
|
||||
info.is_void = true;
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
case SCC_TOK_CHAR:
|
||||
if (info.is_char)
|
||||
goto duplicate_error;
|
||||
info.is_char = true;
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
case SCC_TOK_SHORT:
|
||||
if (info.is_short)
|
||||
goto duplicate_error;
|
||||
info.is_short = true;
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
case SCC_TOK_INT:
|
||||
if (info.is_int)
|
||||
goto duplicate_error;
|
||||
info.is_int = true;
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
case SCC_TOK_LONG:
|
||||
// long 可以出现两次
|
||||
if (info.is_long_long) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Three 'long's in type specifier");
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
if (info.is_long) {
|
||||
info.is_long_long = true;
|
||||
@@ -833,49 +834,49 @@ static scc_ast_type_t *parse_type_specifier(scc_parser_t *parser) {
|
||||
} else {
|
||||
info.is_long = true;
|
||||
}
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
case SCC_TOK_FLOAT:
|
||||
if (info.is_float)
|
||||
goto duplicate_error;
|
||||
info.is_float = true;
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
case SCC_TOK_DOUBLE:
|
||||
if (info.is_double)
|
||||
goto duplicate_error;
|
||||
info.is_double = true;
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
case SCC_TOK_BOOL:
|
||||
if (info.is_bool)
|
||||
goto duplicate_error;
|
||||
info.is_bool = true;
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
case SCC_TOK_SIGNED:
|
||||
if (info.is_unsigned || info.is_signed) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Both 'signed' and 'unsigned' in type specifier");
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
info.is_signed = true;
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
case SCC_TOK_UNSIGNED:
|
||||
if (info.is_unsigned || info.is_signed) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Both 'signed' and 'unsigned' in type specifier");
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
info.is_unsigned = true;
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
case SCC_TOK_COMPLEX:
|
||||
if (info.is_complex)
|
||||
goto duplicate_error;
|
||||
info.is_complex = true;
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
break;
|
||||
default:
|
||||
goto done;
|
||||
@@ -883,12 +884,12 @@ static scc_ast_type_t *parse_type_specifier(scc_parser_t *parser) {
|
||||
}
|
||||
done:
|
||||
if (!check_type_combinations(parser, &info)) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
return build_type_from_info(&info, pos);
|
||||
duplicate_error:
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser), "Duplicate type specifier");
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static scc_ast_type_t *parse_pointer(scc_parser_t *parser,
|
||||
@@ -900,15 +901,15 @@ static scc_ast_type_t *parse_pointer(scc_parser_t *parser,
|
||||
* type-qualifier-list(opt) pointer
|
||||
*/
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null || tok_ptr->type != SCC_TOK_MUL) {
|
||||
if (tok_ptr == nullptr || tok_ptr->type != SCC_TOK_MUL) {
|
||||
return pointee;
|
||||
}
|
||||
scc_pos_t pos = tok_ptr->loc;
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
|
||||
scc_ast_type_t *pointer = ast_type_alloc();
|
||||
if (pointee == null) {
|
||||
Assert(delay_pointee_ptr != null);
|
||||
if (pointee == nullptr) {
|
||||
Assert(delay_pointee_ptr != nullptr);
|
||||
*delay_pointee_ptr = pointer;
|
||||
pointee = pointer;
|
||||
}
|
||||
@@ -931,17 +932,17 @@ static void parse_parameter_type_list(scc_parser_t *parser,
|
||||
declaration-specifiers declarator
|
||||
declaration-specifiers abstract-declarator(opt)
|
||||
*/
|
||||
scc_ast_decl_t *param = null;
|
||||
scc_ast_decl_t *decl = null;
|
||||
const scc_lexer_tok_t *tok_ptr = null;
|
||||
scc_ast_decl_t *param = nullptr;
|
||||
scc_ast_decl_t *decl = nullptr;
|
||||
const scc_lexer_tok_t *tok_ptr = nullptr;
|
||||
while (1) {
|
||||
// FIXME
|
||||
scc_ast_type_t *type = scc_parse_declaration_specifiers(parser);
|
||||
if (type == null) {
|
||||
if (type == nullptr) {
|
||||
break;
|
||||
}
|
||||
decl = scc_parse_declarator(parser, type);
|
||||
if (decl == null) {
|
||||
if (decl == nullptr) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -957,18 +958,18 @@ static void parse_parameter_type_list(scc_parser_t *parser,
|
||||
break;
|
||||
}
|
||||
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr->type == SCC_TOK_ELLIPSIS) {
|
||||
param = scc_malloc(sizeof(scc_ast_decl_t));
|
||||
Assert(param != null);
|
||||
Assert(param != nullptr);
|
||||
// FIXME
|
||||
type = scc_malloc(sizeof(scc_ast_type_t));
|
||||
Assert(type != null);
|
||||
Assert(type != nullptr);
|
||||
scc_ast_type_builtin_init(type, SCC_AST_BUILTIN_TYPE_VA_LIST,
|
||||
tok_ptr->loc);
|
||||
scc_ast_decl_param_init(param, type, null, tok_ptr->loc);
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_ast_decl_param_init(param, type, nullptr, tok_ptr->loc);
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
scc_vec_push(*params, param);
|
||||
break;
|
||||
}
|
||||
@@ -977,7 +978,7 @@ static void parse_parameter_type_list(scc_parser_t *parser,
|
||||
|
||||
static inline cbool parse_function_parameters_start(scc_parser_t *parser) {
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
return false;
|
||||
}
|
||||
if (scc_parse_is_decl_specifier_start(parser)) {
|
||||
@@ -1003,14 +1004,14 @@ static void parse_function_parameters(scc_parser_t *parser,
|
||||
}
|
||||
|
||||
static scc_ast_expr_t *parse_array_size_type(scc_parser_t *parser) {
|
||||
const scc_lexer_tok_t *tok_ptr = null;
|
||||
const scc_lexer_tok_t *tok_ptr = nullptr;
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_L_BRACKET)) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser), "Expect '['");
|
||||
parser->errcode = 1;
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_expr_t *size = null;
|
||||
scc_ast_expr_t *size = nullptr;
|
||||
scc_ast_decl_specifier_t quals = {0};
|
||||
if (scc_parse_is_type_qualifier_start(parser)) {
|
||||
// static assignment-expression
|
||||
@@ -1019,7 +1020,7 @@ static scc_ast_expr_t *parse_array_size_type(scc_parser_t *parser) {
|
||||
// TODO
|
||||
}
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr != null && tok_ptr->type != SCC_TOK_R_BRACKET) {
|
||||
if (tok_ptr != nullptr && tok_ptr->type != SCC_TOK_R_BRACKET) {
|
||||
size = scc_parse_expression(parser);
|
||||
}
|
||||
} else if (scc_parser_consume_if(parser, SCC_TOK_MUL)) {
|
||||
@@ -1031,14 +1032,14 @@ static scc_ast_expr_t *parse_array_size_type(scc_parser_t *parser) {
|
||||
} else {
|
||||
// assignment-expression(opt)
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr != null && tok_ptr->type != SCC_TOK_R_BRACKET) {
|
||||
if (tok_ptr != nullptr && tok_ptr->type != SCC_TOK_R_BRACKET) {
|
||||
size = scc_parse_expression(parser);
|
||||
}
|
||||
}
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_R_BRACKET)) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser), "expect ']'");
|
||||
parser->errcode = 1;
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
@@ -1071,16 +1072,16 @@ static scc_ast_type_t *
|
||||
parse_direct_declarator(scc_parser_t *parser, scc_ast_type_t *base,
|
||||
scc_ast_type_t **delay_pointee_ptr,
|
||||
scc_lexer_tok_t *tok_ident) {
|
||||
const scc_lexer_tok_t *tok_ptr = null;
|
||||
scc_ast_type_t *ret = null;
|
||||
const scc_lexer_tok_t *tok_ptr = nullptr;
|
||||
scc_ast_type_t *ret = nullptr;
|
||||
// direct-abstract-declarator
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
return base;
|
||||
}
|
||||
|
||||
if (tok_ptr->type == SCC_TOK_IDENT) {
|
||||
Assert(tok_ident != null);
|
||||
Assert(tok_ident != nullptr);
|
||||
if (tok_ident->type != SCC_TOK_UNKNOWN) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Got double identifier in declarator");
|
||||
@@ -1092,8 +1093,8 @@ parse_direct_declarator(scc_parser_t *parser, scc_ast_type_t *base,
|
||||
tok_ident);
|
||||
} else if (tok_ptr->type == SCC_TOK_L_PAREN) {
|
||||
// () SCC_TOK_L_PAREN
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_ast_type_t *delay_pointee = null;
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
scc_ast_type_t *delay_pointee = nullptr;
|
||||
if (parse_function_parameters_start(parser)) {
|
||||
scc_ast_decl_vec_t params;
|
||||
parse_function_parameters(parser, ¶ms);
|
||||
@@ -1103,10 +1104,10 @@ parse_direct_declarator(scc_parser_t *parser, scc_ast_type_t *base,
|
||||
return parse_direct_declarator(parser, ret, delay_pointee_ptr,
|
||||
tok_ident);
|
||||
} else {
|
||||
ret = parse_declarator(parser, null, &delay_pointee, tok_ident);
|
||||
if (ret == null) {
|
||||
ret = parse_declarator(parser, nullptr, &delay_pointee, tok_ident);
|
||||
if (ret == nullptr) {
|
||||
SCC_ERROR(tok_ident->loc, "parse_declarator failed");
|
||||
Panic("parse_declarator failed ret == null");
|
||||
Panic("parse_declarator failed ret == nullptr");
|
||||
}
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_R_PAREN)) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser), "expect ')'");
|
||||
@@ -1114,7 +1115,7 @@ parse_direct_declarator(scc_parser_t *parser, scc_ast_type_t *base,
|
||||
base = parse_direct_declarator(parser, base, delay_pointee_ptr,
|
||||
tok_ident);
|
||||
Assert(SCC_AST_IS_A(scc_ast_type_t, base));
|
||||
Assert(delay_pointee != null);
|
||||
Assert(delay_pointee != nullptr);
|
||||
delay_pointee->pointer.pointee = base;
|
||||
return ret;
|
||||
}
|
||||
@@ -1158,18 +1159,18 @@ parse_abstract_declarator(scc_parser_t *parser, scc_ast_type_t *base,
|
||||
static scc_ast_type_t *
|
||||
parse_direct_abstract_declarator(scc_parser_t *parser, scc_ast_type_t *base,
|
||||
scc_ast_type_t **delay_pointee_ptr) {
|
||||
const scc_lexer_tok_t *tok_ptr = null;
|
||||
scc_ast_type_t *ret = null;
|
||||
const scc_lexer_tok_t *tok_ptr = nullptr;
|
||||
scc_ast_type_t *ret = nullptr;
|
||||
// direct-abstract-declarator
|
||||
tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
if (tok_ptr == nullptr) {
|
||||
return base;
|
||||
}
|
||||
|
||||
if (tok_ptr->type == SCC_TOK_L_PAREN) {
|
||||
// () SCC_TOK_L_PAREN
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_ast_type_t *delay_pointee = null;
|
||||
scc_parser_next_consume(parser, nullptr);
|
||||
scc_ast_type_t *delay_pointee = nullptr;
|
||||
if (parse_function_parameters_start(parser)) {
|
||||
scc_ast_decl_vec_t params;
|
||||
parse_function_parameters(parser, ¶ms);
|
||||
@@ -1179,14 +1180,14 @@ parse_direct_abstract_declarator(scc_parser_t *parser, scc_ast_type_t *base,
|
||||
return parse_direct_abstract_declarator(parser, ret,
|
||||
delay_pointee_ptr);
|
||||
} else {
|
||||
ret = parse_abstract_declarator(parser, null, &delay_pointee);
|
||||
Assert(ret != null);
|
||||
ret = parse_abstract_declarator(parser, nullptr, &delay_pointee);
|
||||
Assert(ret != nullptr);
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_R_PAREN)) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser), "expect ')'");
|
||||
}
|
||||
base = parse_direct_abstract_declarator(parser, base,
|
||||
delay_pointee_ptr);
|
||||
Assert(delay_pointee != null);
|
||||
Assert(delay_pointee != nullptr);
|
||||
delay_pointee->pointer.pointee = base;
|
||||
}
|
||||
return ret;
|
||||
@@ -1208,20 +1209,21 @@ scc_ast_decl_t *scc_parse_declarator(scc_parser_t *parser,
|
||||
scc_ast_type_t *type) {
|
||||
scc_lexer_tok_t decl_name_tok = {0};
|
||||
scc_ast_type_t *decl_type =
|
||||
parse_declarator(parser, type, null, &decl_name_tok);
|
||||
scc_ast_decl_t *decl = null;
|
||||
parse_declarator(parser, type, nullptr, &decl_name_tok);
|
||||
scc_ast_decl_t *decl = nullptr;
|
||||
decl = scc_malloc(sizeof(scc_ast_decl_t));
|
||||
Assert(decl != null);
|
||||
Assert(decl != nullptr);
|
||||
Assert(decl_name_tok.type == SCC_TOK_IDENT ||
|
||||
decl_name_tok.type == SCC_TOK_UNKNOWN);
|
||||
|
||||
// FIXME memory leak
|
||||
const char *name = decl_name_tok.type == SCC_TOK_IDENT
|
||||
? scc_str_as_cstr(&decl_name_tok.lexeme)
|
||||
: null;
|
||||
: nullptr;
|
||||
|
||||
if (decl_type->base.type == SCC_AST_TYPE_FUNCTION) {
|
||||
scc_ast_decl_func_init(decl, decl_type, name, null, decl_name_tok.loc);
|
||||
scc_ast_decl_func_init(decl, decl_type, name, nullptr,
|
||||
decl_name_tok.loc);
|
||||
// TODO using sema to change it
|
||||
if (type->quals.is_inline) {
|
||||
decl_type->quals.is_inline = true;
|
||||
@@ -1229,7 +1231,7 @@ scc_ast_decl_t *scc_parse_declarator(scc_parser_t *parser,
|
||||
type->quals.is_inline = false;
|
||||
}
|
||||
} else {
|
||||
scc_ast_decl_unsafe_val_init(decl, decl_type, name, null,
|
||||
scc_ast_decl_unsafe_val_init(decl, decl_type, name, nullptr,
|
||||
decl_name_tok.loc);
|
||||
}
|
||||
|
||||
@@ -1237,17 +1239,17 @@ scc_ast_decl_t *scc_parse_declarator(scc_parser_t *parser,
|
||||
if (decl_type->base.type == SCC_AST_TYPE_STRUCT ||
|
||||
decl_type->base.type == SCC_AST_TYPE_UNION ||
|
||||
decl_type->base.type == SCC_AST_TYPE_ENUM) {
|
||||
if (decl_type->record.decl == null) {
|
||||
if (decl_type->record.decl == nullptr) {
|
||||
decl = scc_malloc(sizeof(scc_ast_decl_t));
|
||||
Assert(decl != null);
|
||||
Assert(decl != nullptr);
|
||||
if (decl_type->base.type == SCC_AST_TYPE_STRUCT) {
|
||||
scc_ast_decl_struct_init(decl, decl_type->record.name, null,
|
||||
decl_type->base.loc);
|
||||
scc_ast_decl_struct_init(decl, decl_type->record.name,
|
||||
nullptr, decl_type->base.loc);
|
||||
} else if (decl_type->base.type == SCC_AST_TYPE_UNION) {
|
||||
scc_ast_decl_union_init(decl, decl_type->record.name, null,
|
||||
decl_type->base.loc);
|
||||
scc_ast_decl_union_init(decl, decl_type->record.name,
|
||||
nullptr, decl_type->base.loc);
|
||||
} else {
|
||||
scc_ast_decl_enum_init(decl, type->record.name, null,
|
||||
scc_ast_decl_enum_init(decl, type->record.name, nullptr,
|
||||
decl_type->base.loc);
|
||||
}
|
||||
} else {
|
||||
@@ -1256,7 +1258,7 @@ scc_ast_decl_t *scc_parse_declarator(scc_parser_t *parser,
|
||||
}
|
||||
} else {
|
||||
decl = scc_malloc(sizeof(scc_ast_decl_t));
|
||||
scc_ast_decl_unsafe_val_init(decl, type, null, null,
|
||||
scc_ast_decl_unsafe_val_init(decl, type, nullptr, nullptr,
|
||||
decl_type->base.loc);
|
||||
}
|
||||
}
|
||||
@@ -1265,7 +1267,7 @@ scc_ast_decl_t *scc_parse_declarator(scc_parser_t *parser,
|
||||
|
||||
scc_ast_type_t *scc_parse_declaration_specifiers(scc_parser_t *parser) {
|
||||
if (!scc_parse_is_decl_specifier_start(parser)) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
scc_ast_decl_specifier_t spec = {0};
|
||||
|
||||
@@ -1275,10 +1277,10 @@ scc_ast_type_t *scc_parse_declaration_specifiers(scc_parser_t *parser) {
|
||||
|
||||
spec = parse_declaration_specifiers_list(parser, spec);
|
||||
scc_ast_type_t *specifier = parse_type_specifier(parser);
|
||||
if (specifier == null) {
|
||||
if (specifier == nullptr) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"declaration specifier can't have type specifier");
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
specifier->quals = spec;
|
||||
|
||||
@@ -1288,16 +1290,16 @@ scc_ast_type_t *scc_parse_declaration_specifiers(scc_parser_t *parser) {
|
||||
scc_ast_type_t *scc_parse_type_name(scc_parser_t *parser) {
|
||||
if (!(scc_parse_is_type_specifier_start(parser) ||
|
||||
scc_parse_is_type_qualifier_start(parser))) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_ast_type_t *ret = null;
|
||||
scc_ast_type_t *ret = nullptr;
|
||||
scc_ast_decl_specifier_t spec = {0};
|
||||
spec = parse_type_qualifier_list(parser, spec);
|
||||
ret = parse_type_specifier(parser);
|
||||
if (ret != null) {
|
||||
if (ret != nullptr) {
|
||||
ret->quals = spec;
|
||||
}
|
||||
ret = parse_abstract_declarator(parser, ret, null);
|
||||
ret = parse_abstract_declarator(parser, ret, nullptr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ static scc_ast_type_t *dummy_got_type_callback(void *context,
|
||||
const char *name) {
|
||||
(void)context;
|
||||
(void)name;
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#define ASSIGN_PTR_OR_DEFAULT(assigned_val, value, default) \
|
||||
@@ -21,10 +21,10 @@ static scc_ast_type_t *dummy_got_type_callback(void *context,
|
||||
|
||||
void scc_parser_init(scc_parser_t *parser, scc_lexer_tok_ring_t *tok_ring,
|
||||
scc_sema_callbacks_t *callbacks) {
|
||||
Assert(parser != null && tok_ring != null);
|
||||
Assert(parser != nullptr && tok_ring != nullptr);
|
||||
parser->ring = tok_ring;
|
||||
parser->errcode = 0;
|
||||
parser->translation_unit = null;
|
||||
parser->translation_unit = nullptr;
|
||||
if (callbacks) {
|
||||
ASSIGN_PTR_OR_DEFAULT(parser->sema_callbacks.on_decl,
|
||||
callbacks->on_decl, dummy_sema_callback);
|
||||
@@ -43,7 +43,7 @@ void scc_parser_init(scc_parser_t *parser, scc_lexer_tok_ring_t *tok_ring,
|
||||
parser->sema_callbacks.on_expr = dummy_sema_callback;
|
||||
parser->sema_callbacks.on_type = dummy_sema_callback;
|
||||
parser->sema_callbacks.got_type = dummy_got_type_callback;
|
||||
parser->sema_callbacks.context = null;
|
||||
parser->sema_callbacks.context = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ scc_ast_translation_unit_t *scc_parse_translation_unit(scc_parser_t *parser) {
|
||||
scc_ast_translation_unit_t *unit =
|
||||
scc_malloc(sizeof(scc_ast_translation_unit_t));
|
||||
if (!unit)
|
||||
return null;
|
||||
return nullptr;
|
||||
unit->base.type = SCC_AST_TRANSLATION_UNIT;
|
||||
scc_vec_init(unit->declarations);
|
||||
|
||||
@@ -66,7 +66,7 @@ scc_ast_translation_unit_t *scc_parse_translation_unit(scc_parser_t *parser) {
|
||||
*/
|
||||
while (1) {
|
||||
scc_ast_decl_t *decl = scc_parse_declaration(parser);
|
||||
if (decl != null) {
|
||||
if (decl != nullptr) {
|
||||
scc_vec_push(unit->declarations, decl);
|
||||
} else {
|
||||
break;
|
||||
@@ -77,7 +77,7 @@ scc_ast_translation_unit_t *scc_parse_translation_unit(scc_parser_t *parser) {
|
||||
break;
|
||||
}
|
||||
const scc_lexer_tok_t *tok = scc_parser_peek(parser);
|
||||
if (tok == null || tok->type == SCC_TOK_EOF) {
|
||||
if (tok == nullptr || tok->type == SCC_TOK_EOF) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -87,7 +87,7 @@ scc_ast_translation_unit_t *scc_parse_translation_unit(scc_parser_t *parser) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser), "parser error: %d",
|
||||
parser->errcode);
|
||||
scc_free(unit);
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Assert(unit->base.type == SCC_AST_TRANSLATION_UNIT);
|
||||
|
||||
@@ -15,14 +15,14 @@ static void expr_callback(void *context, scc_ast_node_type_t node_type,
|
||||
void *node) {
|
||||
scc_sema_symtab_t *sema_symtab = context;
|
||||
|
||||
if (node_type == SCC_AST_UNKNOWN || node == null) {
|
||||
if (node_type == SCC_AST_UNKNOWN || node == nullptr) {
|
||||
return;
|
||||
}
|
||||
scc_ast_expr_t *expr = SCC_AST_CAST_TO(scc_ast_expr_t, node);
|
||||
if (node_type == SCC_AST_EXPR_IDENTIFIER) {
|
||||
scc_ast_node_t *node =
|
||||
scc_sema_symtab_lookup_symbol(sema_symtab, expr->identifier.name);
|
||||
if (node == null) {
|
||||
if (node == nullptr) {
|
||||
SCC_ERROR(expr->base.loc, "sema error: Identifier '%s' not found",
|
||||
expr->identifier.name);
|
||||
} else if (!SCC_AST_IS_A(scc_ast_decl_t, node)) {
|
||||
@@ -48,7 +48,7 @@ static void stmt_callback(void *context, scc_ast_node_type_t node_type,
|
||||
scc_sema_symtab_leave_scope(sema_symtab);
|
||||
return;
|
||||
}
|
||||
if (node_type == SCC_AST_UNKNOWN || node == null) {
|
||||
if (node_type == SCC_AST_UNKNOWN || node == nullptr) {
|
||||
return;
|
||||
}
|
||||
return;
|
||||
@@ -66,20 +66,20 @@ static void decl_callback(void *context, scc_ast_node_type_t node_type,
|
||||
scc_sema_symtab_leave_scope(sema_symtab);
|
||||
return;
|
||||
}
|
||||
if (node_type == SCC_AST_UNKNOWN || node == null) {
|
||||
if (node_type == SCC_AST_UNKNOWN || node == nullptr) {
|
||||
return;
|
||||
}
|
||||
scc_ast_decl_t *decl = SCC_AST_CAST_TO(scc_ast_decl_t, node);
|
||||
|
||||
scc_ast_type_t *type = scc_malloc(sizeof(scc_ast_type_t));
|
||||
Assert(type != null);
|
||||
Assert(type != nullptr);
|
||||
|
||||
if (node_type == SCC_AST_DECL_STRUCT) {
|
||||
scc_ast_type_struct_init(type, decl->name, decl, decl->base.loc);
|
||||
// FIXME memory leak
|
||||
scc_str_t name = scc_str_from_cstr("$S_");
|
||||
|
||||
if (decl->name == null) {
|
||||
if (decl->name == nullptr) {
|
||||
decl->name = "<anonymous struct>";
|
||||
}
|
||||
scc_str_append_cstr(&name, decl->name, scc_strlen(decl->name));
|
||||
@@ -88,7 +88,7 @@ static void decl_callback(void *context, scc_ast_node_type_t node_type,
|
||||
} else if (node_type == SCC_AST_DECL_UNION) {
|
||||
scc_ast_type_union_init(type, decl->name, decl, decl->base.loc);
|
||||
scc_str_t name = scc_str_from_cstr("$U_");
|
||||
if (decl->name == null) {
|
||||
if (decl->name == nullptr) {
|
||||
decl->name = "<anonymous union>";
|
||||
}
|
||||
scc_str_append_cstr(&name, decl->name, scc_strlen(decl->name));
|
||||
@@ -97,7 +97,7 @@ static void decl_callback(void *context, scc_ast_node_type_t node_type,
|
||||
} else if (node_type == SCC_AST_DECL_ENUM) {
|
||||
scc_ast_type_enum_init(type, decl->name, decl, decl->base.loc);
|
||||
scc_str_t name = scc_str_from_cstr("$E_");
|
||||
if (decl->name == null) {
|
||||
if (decl->name == nullptr) {
|
||||
decl->name = "<anonymous enum>";
|
||||
}
|
||||
scc_str_append_cstr(&name, decl->name, scc_strlen(decl->name));
|
||||
@@ -111,7 +111,7 @@ static void decl_callback(void *context, scc_ast_node_type_t node_type,
|
||||
// LOG_INFO("enum added %s", enum_decl->name);
|
||||
}
|
||||
} else if (node_type == SCC_AST_DECL_TYPEDEF) {
|
||||
if (decl->name == null) {
|
||||
if (decl->name == nullptr) {
|
||||
SCC_ERROR(decl->base.loc, "typedef without name");
|
||||
return;
|
||||
}
|
||||
@@ -120,7 +120,7 @@ static void decl_callback(void *context, scc_ast_node_type_t node_type,
|
||||
} else if (node_type == SCC_AST_DECL_VAR) {
|
||||
scc_sema_symtab_add_symbol(sema_symtab, decl->name, &decl->base);
|
||||
} else if (node_type == SCC_AST_DECL_PARAM) {
|
||||
if (decl->name == null) {
|
||||
if (decl->name == nullptr) {
|
||||
if (decl->param.type->base.type == SCC_AST_TYPE_BUILTIN &&
|
||||
(decl->param.type->builtin.type ==
|
||||
SCC_AST_BUILTIN_TYPE_VA_LIST ||
|
||||
@@ -132,7 +132,7 @@ static void decl_callback(void *context, scc_ast_node_type_t node_type,
|
||||
}
|
||||
scc_sema_symtab_add_symbol(sema_symtab, decl->name, &decl->base);
|
||||
} else if (node_type == SCC_AST_DECL_FUNC) {
|
||||
if (decl->name == null) {
|
||||
if (decl->name == nullptr) {
|
||||
SCC_ERROR(decl->base.loc, "sema error: Function must have a name");
|
||||
} else {
|
||||
// FIXME 重名函数...
|
||||
@@ -150,12 +150,12 @@ static scc_ast_type_t *got_type_callback(void *context, const char *name) {
|
||||
*type = *(scc_ast_type_t *)node;
|
||||
return type;
|
||||
}
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void scc_sema_init(scc_sema_callbacks_t *callbacks) {
|
||||
scc_sema_symtab_t *sema_symtab = scc_malloc(sizeof(scc_sema_symtab_t));
|
||||
if (sema_symtab == null) {
|
||||
if (sema_symtab == nullptr) {
|
||||
LOG_FATAL("out of memory");
|
||||
return;
|
||||
}
|
||||
@@ -176,15 +176,16 @@ void scc_sema_init(scc_sema_callbacks_t *callbacks) {
|
||||
&type->base);
|
||||
|
||||
scc_ast_decl_t *decl = scc_malloc(sizeof(scc_ast_decl_t));
|
||||
scc_ast_decl_val_init(decl, type, "__scc_builtin__", null,
|
||||
scc_ast_decl_val_init(decl, type, "__scc_builtin__", nullptr,
|
||||
scc_pos_create());
|
||||
scc_sema_symtab_add_symbol(sema_symtab, "__func__", &decl->base);
|
||||
|
||||
scc_ast_type_t *built_func_type = scc_malloc(sizeof(scc_ast_type_t));
|
||||
scc_ast_type_function_init(built_func_type, null, null, scc_pos_create());
|
||||
scc_ast_type_function_init(built_func_type, nullptr, nullptr,
|
||||
scc_pos_create());
|
||||
scc_ast_decl_t *builin_func = scc_malloc(sizeof(scc_ast_decl_t));
|
||||
scc_ast_decl_func_init(builin_func, built_func_type, "__scc_builtin_func",
|
||||
null, scc_pos_create());
|
||||
nullptr, scc_pos_create());
|
||||
scc_sema_symtab_add_symbol(sema_symtab, "__scc_builtin_va_start",
|
||||
&builin_func->base);
|
||||
scc_sema_symtab_add_symbol(sema_symtab, "__scc_builtin_va_end",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <sema_symtab.h>
|
||||
|
||||
void scc_sema_symtab_init(scc_sema_symtab_t *symtab) {
|
||||
symtab->root_scope.parent = null;
|
||||
symtab->root_scope.parent = nullptr;
|
||||
|
||||
scc_hashtable_init(&symtab->root_scope.symbols,
|
||||
(scc_hashtable_hash_func_t)scc_strhash32,
|
||||
@@ -10,7 +10,7 @@ void scc_sema_symtab_init(scc_sema_symtab_t *symtab) {
|
||||
}
|
||||
|
||||
void scc_sema_symtab_drop(scc_sema_symtab_t *symtab) {
|
||||
while (symtab->current_scope != null) {
|
||||
while (symtab->current_scope != nullptr) {
|
||||
scc_hashtable_drop(&symtab->current_scope->symbols);
|
||||
symtab->current_scope = symtab->current_scope->parent;
|
||||
}
|
||||
@@ -18,7 +18,7 @@ void scc_sema_symtab_drop(scc_sema_symtab_t *symtab) {
|
||||
|
||||
void scc_sema_symtab_enter_scope(scc_sema_symtab_t *symtab) {
|
||||
scc_sema_scope_t *scope = scc_malloc(sizeof(scc_sema_scope_t));
|
||||
if (scope == null) {
|
||||
if (scope == nullptr) {
|
||||
LOG_FATAL("out of memory");
|
||||
return;
|
||||
}
|
||||
@@ -47,13 +47,13 @@ scc_ast_node_t *scc_sema_symtab_add_symbol(scc_sema_symtab_t *symtab,
|
||||
|
||||
scc_ast_node_t *scc_sema_symtab_lookup_symbol(scc_sema_symtab_t *symtab,
|
||||
const char *name) {
|
||||
scc_ast_node_t *node = null;
|
||||
for (scc_sema_scope_t *scope = symtab->current_scope; scope != null;
|
||||
scc_ast_node_t *node = nullptr;
|
||||
for (scc_sema_scope_t *scope = symtab->current_scope; scope != nullptr;
|
||||
scope = scope->parent) {
|
||||
node = scc_hashtable_get(&scope->symbols, name);
|
||||
if (node != null) {
|
||||
if (node != nullptr) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ static scc_ast_node_t *process_input(const char *input,
|
||||
scc_sema_init(&sema_callbacks);
|
||||
scc_parser_init(&parser, tok_ring, &sema_callbacks);
|
||||
} else {
|
||||
scc_parser_init(&parser, tok_ring, null);
|
||||
scc_parser_init(&parser, tok_ring, nullptr);
|
||||
}
|
||||
|
||||
scc_ast_node_t *ret = parse_func(&parser);
|
||||
@@ -36,7 +36,7 @@ static scc_ast_node_t *process_input(const char *input,
|
||||
if (not_eof == true) {
|
||||
// FIXME MAYBE free
|
||||
LOG_FATAL("Didn't consume all tokens");
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
scc_lexer_drop_ring(parser.ring);
|
||||
|
||||
@@ -170,14 +170,14 @@ static void test_unary_expr(void) {
|
||||
TEST_CASE("sizeof expression");
|
||||
{
|
||||
scc_ast_expr_t sizeof_expr;
|
||||
scc_ast_expr_sizeof_init(&sizeof_expr, NULL, &x, LOC);
|
||||
scc_ast_expr_sizeof_init(&sizeof_expr, nullptr, &x, LOC);
|
||||
SCC_CHECK_AST(&sizeof_expr.base, "sizeof(x)", scc_parse_expression);
|
||||
}
|
||||
|
||||
TEST_CASE("sizeof type");
|
||||
{
|
||||
scc_ast_expr_t sizeof_type;
|
||||
scc_ast_expr_sizeof_init(&sizeof_type, &int_type, NULL, LOC);
|
||||
scc_ast_expr_sizeof_init(&sizeof_type, &int_type, nullptr, LOC);
|
||||
SCC_CHECK_AST(&sizeof_type.base, "sizeof(int)", scc_parse_expression);
|
||||
}
|
||||
}
|
||||
@@ -391,5 +391,5 @@ TEST_LIST = {
|
||||
{"test_comma_expr", test_comma_expr},
|
||||
{"test_complex_expr", test_complex_expr},
|
||||
{"test_detail_expr", test_detail_expr},
|
||||
{NULL, NULL},
|
||||
{nullptr, nullptr},
|
||||
};
|
||||
|
||||
@@ -106,7 +106,7 @@ static void test_array_type(void) {
|
||||
{
|
||||
// int []
|
||||
scc_ast_type_t array_unknown_int;
|
||||
scc_ast_type_array_init(&array_unknown_int, &int_type, null,
|
||||
scc_ast_type_array_init(&array_unknown_int, &int_type, nullptr,
|
||||
scc_pos_create());
|
||||
SCC_CHECK_AST(&array_unknown_int.base, "int []", scc_parse_type_name);
|
||||
}
|
||||
@@ -159,7 +159,7 @@ static void test_function_type(void) {
|
||||
{
|
||||
// int ()
|
||||
scc_ast_type_t func_type;
|
||||
scc_ast_type_function_init(&func_type, &int_type, null,
|
||||
scc_ast_type_function_init(&func_type, &int_type, nullptr,
|
||||
scc_pos_create());
|
||||
SCC_CHECK_AST(&func_type.base, "int ()", scc_parse_type_name);
|
||||
}
|
||||
@@ -168,7 +168,7 @@ static void test_function_type(void) {
|
||||
{
|
||||
// int (void)
|
||||
scc_ast_decl_t void_param;
|
||||
scc_ast_decl_param_init(&void_param, &void_type, null,
|
||||
scc_ast_decl_param_init(&void_param, &void_type, nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_decl_vec_t params;
|
||||
@@ -189,8 +189,9 @@ static void test_function_type(void) {
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_decl_t param_int, param_float;
|
||||
scc_ast_decl_param_init(¶m_int, &int_type, null, scc_pos_create());
|
||||
scc_ast_decl_param_init(¶m_float, &float_type, null,
|
||||
scc_ast_decl_param_init(¶m_int, &int_type, nullptr,
|
||||
scc_pos_create());
|
||||
scc_ast_decl_param_init(¶m_float, &float_type, nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_decl_vec_t params;
|
||||
@@ -212,7 +213,8 @@ static void test_function_type(void) {
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_decl_t param_int, param_var;
|
||||
scc_ast_decl_param_init(¶m_int, &int_type, null, scc_pos_create());
|
||||
scc_ast_decl_param_init(¶m_int, &int_type, nullptr,
|
||||
scc_pos_create());
|
||||
scc_ast_decl_param_init(¶m_var, &va_list_type, "...",
|
||||
scc_pos_create());
|
||||
|
||||
@@ -231,7 +233,7 @@ static void test_function_type(void) {
|
||||
{
|
||||
// int *()
|
||||
scc_ast_type_t func_type;
|
||||
scc_ast_type_function_init(&func_type, &pointer_int_type, null,
|
||||
scc_ast_type_function_init(&func_type, &pointer_int_type, nullptr,
|
||||
scc_pos_create());
|
||||
SCC_CHECK_AST(&func_type.base, "int *()", scc_parse_type_name);
|
||||
}
|
||||
@@ -240,7 +242,7 @@ static void test_function_type(void) {
|
||||
{
|
||||
// int (*)(void)
|
||||
scc_ast_decl_t void_param;
|
||||
scc_ast_decl_param_init(&void_param, &void_type, null,
|
||||
scc_ast_decl_param_init(&void_param, &void_type, nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_decl_vec_t params;
|
||||
@@ -262,7 +264,7 @@ static void test_struct_union_type(void) {
|
||||
{
|
||||
// struct S
|
||||
scc_ast_type_t struct_type;
|
||||
scc_ast_type_struct_init(&struct_type, "S", null, scc_pos_create());
|
||||
scc_ast_type_struct_init(&struct_type, "S", nullptr, scc_pos_create());
|
||||
SCC_CHECK_AST(&struct_type.base, "struct S", scc_parse_type_name);
|
||||
}
|
||||
|
||||
@@ -270,7 +272,7 @@ static void test_struct_union_type(void) {
|
||||
{
|
||||
// union U
|
||||
scc_ast_type_t union_type;
|
||||
scc_ast_type_union_init(&union_type, "U", null, scc_pos_create());
|
||||
scc_ast_type_union_init(&union_type, "U", nullptr, scc_pos_create());
|
||||
SCC_CHECK_AST(&union_type.base, "union U", scc_parse_type_name);
|
||||
}
|
||||
|
||||
@@ -278,17 +280,19 @@ static void test_struct_union_type(void) {
|
||||
{
|
||||
// struct { int x; }
|
||||
scc_ast_decl_t field_x;
|
||||
scc_ast_decl_val_init(&field_x, &int_type, "x", null, scc_pos_create());
|
||||
scc_ast_decl_val_init(&field_x, &int_type, "x", nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_decl_vec_t fields;
|
||||
scc_vec_init(fields);
|
||||
scc_vec_push(fields, &field_x);
|
||||
|
||||
scc_ast_decl_t struct_decl;
|
||||
scc_ast_decl_struct_init(&struct_decl, null, &fields, scc_pos_create());
|
||||
scc_ast_decl_struct_init(&struct_decl, nullptr, &fields,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_type_t struct_type;
|
||||
scc_ast_type_struct_init(&struct_type, null, &struct_decl,
|
||||
scc_ast_type_struct_init(&struct_type, nullptr, &struct_decl,
|
||||
scc_pos_create());
|
||||
SCC_CHECK_AST(&struct_type.base, "struct { int x; }",
|
||||
scc_parse_type_name);
|
||||
@@ -298,7 +302,8 @@ static void test_struct_union_type(void) {
|
||||
{
|
||||
// struct S { int x; }
|
||||
scc_ast_decl_t field_x;
|
||||
scc_ast_decl_val_init(&field_x, &int_type, "x", null, scc_pos_create());
|
||||
scc_ast_decl_val_init(&field_x, &int_type, "x", nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_decl_vec_t fields;
|
||||
scc_vec_init(fields);
|
||||
@@ -322,8 +327,9 @@ static void test_struct_union_type(void) {
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_decl_t field_a, field_b;
|
||||
scc_ast_decl_val_init(&field_a, &int_type, "a", null, scc_pos_create());
|
||||
scc_ast_decl_val_init(&field_b, &float_type, "b", null,
|
||||
scc_ast_decl_val_init(&field_a, &int_type, "a", nullptr,
|
||||
scc_pos_create());
|
||||
scc_ast_decl_val_init(&field_b, &float_type, "b", nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_decl_vec_t fields;
|
||||
@@ -332,10 +338,11 @@ static void test_struct_union_type(void) {
|
||||
scc_vec_push(fields, &field_b);
|
||||
|
||||
scc_ast_decl_t union_decl;
|
||||
scc_ast_decl_union_init(&union_decl, null, &fields, scc_pos_create());
|
||||
scc_ast_decl_union_init(&union_decl, nullptr, &fields,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_type_t union_type;
|
||||
scc_ast_type_union_init(&union_type, null, &union_decl,
|
||||
scc_ast_type_union_init(&union_type, nullptr, &union_decl,
|
||||
scc_pos_create());
|
||||
SCC_CHECK_AST(&union_type.base, "union { int a; float b; }",
|
||||
scc_parse_type_name);
|
||||
@@ -347,7 +354,7 @@ static void test_enum_type(void) {
|
||||
{
|
||||
// enum E
|
||||
scc_ast_type_t enum_type;
|
||||
scc_ast_type_enum_init(&enum_type, "E", null, scc_pos_create());
|
||||
scc_ast_type_enum_init(&enum_type, "E", nullptr, scc_pos_create());
|
||||
SCC_CHECK_AST(&enum_type.base, "enum E", scc_parse_type_name);
|
||||
}
|
||||
|
||||
@@ -357,10 +364,11 @@ static void test_enum_type(void) {
|
||||
scc_ast_type_t enum_type;
|
||||
|
||||
scc_ast_decl_t red, green, blue;
|
||||
scc_ast_decl_val_init(&red, &enum_type, "RED", null, scc_pos_create());
|
||||
scc_ast_decl_val_init(&green, &enum_type, "GREEN", null,
|
||||
scc_ast_decl_val_init(&red, &enum_type, "RED", nullptr,
|
||||
scc_pos_create());
|
||||
scc_ast_decl_val_init(&blue, &enum_type, "BLUE", null,
|
||||
scc_ast_decl_val_init(&green, &enum_type, "GREEN", nullptr,
|
||||
scc_pos_create());
|
||||
scc_ast_decl_val_init(&blue, &enum_type, "BLUE", nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_decl_vec_t enumerators;
|
||||
@@ -370,10 +378,11 @@ static void test_enum_type(void) {
|
||||
scc_vec_push(enumerators, &blue);
|
||||
|
||||
scc_ast_decl_t enum_decl;
|
||||
scc_ast_decl_enum_init(&enum_decl, null, &enumerators,
|
||||
scc_ast_decl_enum_init(&enum_decl, nullptr, &enumerators,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_type_enum_init(&enum_type, null, &enum_decl, scc_pos_create());
|
||||
scc_ast_type_enum_init(&enum_type, nullptr, &enum_decl,
|
||||
scc_pos_create());
|
||||
SCC_CHECK_AST(&enum_type.base, "enum { RED, GREEN, BLUE }",
|
||||
scc_parse_type_name);
|
||||
}
|
||||
@@ -384,10 +393,11 @@ static void test_enum_type(void) {
|
||||
scc_ast_type_t enum_type;
|
||||
|
||||
scc_ast_decl_t red, green, blue;
|
||||
scc_ast_decl_val_init(&red, &enum_type, "RED", null, scc_pos_create());
|
||||
scc_ast_decl_val_init(&green, &enum_type, "GREEN", null,
|
||||
scc_ast_decl_val_init(&red, &enum_type, "RED", nullptr,
|
||||
scc_pos_create());
|
||||
scc_ast_decl_val_init(&blue, &enum_type, "BLUE", null,
|
||||
scc_ast_decl_val_init(&green, &enum_type, "GREEN", nullptr,
|
||||
scc_pos_create());
|
||||
scc_ast_decl_val_init(&blue, &enum_type, "BLUE", nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_decl_vec_t enumerators;
|
||||
@@ -397,7 +407,7 @@ static void test_enum_type(void) {
|
||||
scc_vec_push(enumerators, &blue);
|
||||
|
||||
scc_ast_decl_t enum_decl;
|
||||
scc_ast_decl_enum_init(&enum_decl, null, &enumerators,
|
||||
scc_ast_decl_enum_init(&enum_decl, nullptr, &enumerators,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_type_enum_init(&enum_type, "E", &enum_decl, scc_pos_create());
|
||||
@@ -485,7 +495,7 @@ static void test_hard_type(void) {
|
||||
{
|
||||
// 1) 函数类型 int (void)
|
||||
scc_ast_decl_t void_param;
|
||||
scc_ast_decl_param_init(&void_param, &void_type, null,
|
||||
scc_ast_decl_param_init(&void_param, &void_type, nullptr,
|
||||
scc_pos_create());
|
||||
scc_ast_decl_vec_t params;
|
||||
scc_vec_init(params);
|
||||
@@ -533,7 +543,7 @@ static void test_hard_type(void) {
|
||||
|
||||
// 3) 函数类型,返回上述指针,无参数
|
||||
scc_ast_decl_t void_param;
|
||||
scc_ast_decl_param_init(&void_param, &void_type, null,
|
||||
scc_ast_decl_param_init(&void_param, &void_type, nullptr,
|
||||
scc_pos_create());
|
||||
scc_ast_decl_vec_t params;
|
||||
scc_vec_init(params);
|
||||
@@ -556,7 +566,7 @@ static void test_hard_type(void) {
|
||||
{
|
||||
// 1) 函数类型 int (void)
|
||||
scc_ast_decl_t void_param;
|
||||
scc_ast_decl_param_init(&void_param, &void_type, null,
|
||||
scc_ast_decl_param_init(&void_param, &void_type, nullptr,
|
||||
scc_pos_create());
|
||||
scc_ast_decl_vec_t params;
|
||||
scc_vec_init(params);
|
||||
@@ -572,7 +582,7 @@ static void test_hard_type(void) {
|
||||
|
||||
// 3) 外部函数类型,返回上述指针,无参数
|
||||
scc_ast_type_t outer_func;
|
||||
scc_ast_type_function_init(&outer_func, &ptr_to_func, null,
|
||||
scc_ast_type_function_init(&outer_func, &ptr_to_func, nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
SCC_CHECK_AST(&outer_func.base, "int (*())(void)", scc_parse_type_name);
|
||||
@@ -588,5 +598,5 @@ TEST_LIST = {
|
||||
{"test_enum_type", test_enum_type},
|
||||
{"test_specifier_type", test_specifier_type},
|
||||
{"test_hard_type", test_hard_type},
|
||||
{null, null},
|
||||
{nullptr, nullptr},
|
||||
};
|
||||
|
||||
@@ -32,7 +32,7 @@ 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_ast_decl_val_init(&int_decl, &int_type, "a", nullptr,
|
||||
scc_pos_create());
|
||||
SCC_CHECK_AST(&int_decl.base, "int a;", scc_parse_declaration);
|
||||
}
|
||||
@@ -43,14 +43,15 @@ static void test_parser_unit(void) {
|
||||
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_param_init(&void_decl, &void_type, nullptr,
|
||||
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_stmt_compound_init(&compound, nullptr, scc_pos_create());
|
||||
|
||||
// 构造函数声明
|
||||
scc_ast_decl_t func_decl;
|
||||
@@ -66,14 +67,15 @@ static void test_parser_unit(void) {
|
||||
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_param_init(&void_decl, &void_type, nullptr,
|
||||
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_stmt_compound_init(&compound, nullptr, scc_pos_create());
|
||||
|
||||
scc_ast_decl_t func_decl;
|
||||
scc_ast_decl_func_init(&func_decl, &func_type, "main", &compound,
|
||||
@@ -114,7 +116,8 @@ static void test_parser_unit(void) {
|
||||
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_param_init(&void_decl, &void_type, nullptr,
|
||||
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,
|
||||
@@ -139,11 +142,13 @@ static void test_parser_unit(void) {
|
||||
{
|
||||
// 变量声明 int a;
|
||||
scc_ast_decl_t a_decl;
|
||||
scc_ast_decl_val_init(&a_decl, &int_type, "a", null, scc_pos_create());
|
||||
scc_ast_decl_val_init(&a_decl, &int_type, "a", nullptr,
|
||||
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());
|
||||
scc_ast_decl_val_init(&b_decl, &int_type, "b", nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
// 表达式 1 + 2 * 3
|
||||
scc_ast_expr_t lit1, lit2, lit3, mul, add;
|
||||
@@ -213,7 +218,7 @@ static void test_parser_unit(void) {
|
||||
// 函数类型
|
||||
scc_ast_type_t func_type;
|
||||
scc_ast_type_function_init(&func_type, (scc_ast_type_t *)&int_type,
|
||||
null, scc_pos_create());
|
||||
nullptr, scc_pos_create());
|
||||
|
||||
scc_ast_decl_t func_decl;
|
||||
scc_ast_decl_func_init(&func_decl, &func_type, "main", &compound,
|
||||
@@ -284,8 +289,8 @@ static void test_parser_unit(void) {
|
||||
// 函数类型(返回 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 表示无参数
|
||||
nullptr,
|
||||
scc_pos_create()); // nullptr 表示无参数
|
||||
|
||||
// 函数声明 int main() { ... }
|
||||
scc_ast_decl_t func_decl;
|
||||
@@ -320,15 +325,15 @@ static void test_parser_unit(void) {
|
||||
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_param_init(¶m2, (scc_ast_type_t *)&va_list_type,
|
||||
nullptr, 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_ast_decl_func_init(&decl_func, &decl_func_type, "add", nullptr,
|
||||
scc_pos_create());
|
||||
SCC_CHECK_AST(&decl_func.base, "int add(int a, int b, ...);",
|
||||
scc_parse_declaration);
|
||||
@@ -343,7 +348,7 @@ static void test_parser_unit(void) {
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_decl_t i32a_decl;
|
||||
scc_ast_decl_val_init(&i32a_decl, &typedef_type, "a", null,
|
||||
scc_ast_decl_val_init(&i32a_decl, &typedef_type, "a", nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_node_t *array[] = {&typedef_decl.base, &i32a_decl.base};
|
||||
@@ -364,7 +369,7 @@ static void test_parser_unit(void) {
|
||||
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_ast_decl_val_init(&void_ptr_a_decl, &void_ptr_type, "a", nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_node_t *array2[] = {&void_ptr_decl.base, &void_ptr_a_decl.base};
|
||||
@@ -381,19 +386,20 @@ static void test_parser_unit(void) {
|
||||
{
|
||||
// struct { int x; } (匿名结构体定义)
|
||||
scc_ast_decl_t field;
|
||||
scc_ast_decl_val_init(&field, (scc_ast_type_t *)&int_type, "x", null,
|
||||
scc_ast_decl_val_init(&field, (scc_ast_type_t *)&int_type, "x", nullptr,
|
||||
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_ast_decl_struct_init(&struct_def, nullptr, &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_ast_type_struct_init(&struct_type, nullptr, &struct_def,
|
||||
scc_pos_create());
|
||||
scc_ast_type_t typedef_type;
|
||||
scc_ast_type_typedef_init(&typedef_type, "struct_t", &struct_def,
|
||||
@@ -406,7 +412,7 @@ static void test_parser_unit(void) {
|
||||
scc_parse_declaration);
|
||||
|
||||
scc_ast_decl_t typedef_impl_decl;
|
||||
scc_ast_decl_val_init(&typedef_impl_decl, &typedef_type, "a", null,
|
||||
scc_ast_decl_val_init(&typedef_impl_decl, &typedef_type, "a", nullptr,
|
||||
scc_pos_create());
|
||||
scc_ast_node_t *array[] = {&typedef_decl.base, &typedef_impl_decl.base};
|
||||
scc_ast_stmt_t stmt;
|
||||
@@ -431,7 +437,8 @@ static void test_parser_unit(void) {
|
||||
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_param_init(¶m3, &va_list_type, nullptr,
|
||||
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);
|
||||
@@ -443,7 +450,7 @@ static void test_parser_unit(void) {
|
||||
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_ast_decl_func_init(&func_decl, &func_type, "func", nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_decl_t *decls_array[] = {&type_decl, &func_decl};
|
||||
@@ -512,7 +519,7 @@ static void test_parser_unit(void) {
|
||||
|
||||
// 6. 函数声明
|
||||
scc_ast_decl_t decl;
|
||||
scc_ast_decl_func_init(&decl, &func_type, "call", null,
|
||||
scc_ast_decl_func_init(&decl, &func_type, "call", nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
// 7. 与解析结果比较
|
||||
@@ -664,9 +671,9 @@ static void test_parser_unit(void) {
|
||||
{
|
||||
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());
|
||||
nullptr, scc_pos_create());
|
||||
scc_ast_decl_val_init(&decl_b, (scc_ast_type_t *)&int_type, "b",
|
||||
null, scc_pos_create());
|
||||
nullptr, scc_pos_create());
|
||||
|
||||
scc_ast_decl_vec_t decl_vec;
|
||||
scc_vec_init(decl_vec);
|
||||
@@ -708,7 +715,7 @@ static void test_parser_unit(void) {
|
||||
{
|
||||
// 构造 struct list_head 类型(不完整)
|
||||
scc_ast_type_t struct_list_head;
|
||||
scc_ast_type_struct_init(&struct_list_head, "list_head", null,
|
||||
scc_ast_type_struct_init(&struct_list_head, "list_head", nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
// 构造两个指针类型(分别用于 next 和 prev,指向同一结构体)
|
||||
@@ -720,9 +727,9 @@ static void test_parser_unit(void) {
|
||||
|
||||
// 构造变量声明 next 和 prev
|
||||
scc_ast_decl_t next_decl, prev_decl;
|
||||
scc_ast_decl_val_init(&next_decl, &ptr_to_struct1, "next", null,
|
||||
scc_ast_decl_val_init(&next_decl, &ptr_to_struct1, "next", nullptr,
|
||||
scc_pos_create());
|
||||
scc_ast_decl_val_init(&prev_decl, &ptr_to_struct2, "prev", null,
|
||||
scc_ast_decl_val_init(&prev_decl, &ptr_to_struct2, "prev", nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
// 构造声明列表
|
||||
@@ -742,7 +749,7 @@ static void test_parser_unit(void) {
|
||||
// 构造字段 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());
|
||||
nullptr, scc_pos_create());
|
||||
|
||||
scc_ast_decl_vec_t fields;
|
||||
scc_vec_init(fields);
|
||||
@@ -750,12 +757,12 @@ static void test_parser_unit(void) {
|
||||
|
||||
// 构造匿名结构体定义声明
|
||||
scc_ast_decl_t struct_def;
|
||||
scc_ast_decl_struct_init(&struct_def, null, &fields,
|
||||
scc_ast_decl_struct_init(&struct_def, nullptr, &fields,
|
||||
scc_pos_create()); // fields 被移动
|
||||
|
||||
// 构造匿名结构体类型
|
||||
scc_ast_type_t anon_struct_type;
|
||||
scc_ast_type_struct_init(&anon_struct_type, null, &struct_def,
|
||||
scc_ast_type_struct_init(&anon_struct_type, nullptr, &struct_def,
|
||||
scc_pos_create());
|
||||
|
||||
// 构造指针类型指向该匿名结构体
|
||||
@@ -807,7 +814,8 @@ static void test_parser_unit(void) {
|
||||
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_param_init(&void_decl, &void_type, nullptr,
|
||||
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,
|
||||
@@ -818,7 +826,7 @@ static void test_parser_unit(void) {
|
||||
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_ast_decl_val_init(&ptr_to_func_decl, &ptr_to_func, "foo", nullptr,
|
||||
scc_pos_create());
|
||||
SCC_CHECK_AST(&ptr_to_func_decl.base, "int (*(*foo)(void))[5];",
|
||||
scc_parse_declaration);
|
||||
@@ -838,13 +846,14 @@ static void test_parser_unit(void) {
|
||||
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_param_init(¶m1, &va_list_type, nullptr,
|
||||
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_ast_decl_func_init(&func_hard_decl, &func_hard_type, "bar", nullptr,
|
||||
scc_pos_create());
|
||||
|
||||
scc_ast_decl_vec_t decls;
|
||||
@@ -865,5 +874,5 @@ static void test_parser_unit(void) {
|
||||
|
||||
TEST_LIST = {
|
||||
{"parser_unit", test_parser_unit},
|
||||
{null, null},
|
||||
{nullptr, nullptr},
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include <utest/acutest.h>
|
||||
|
||||
TEST_LIST = {
|
||||
{NULL, NULL},
|
||||
{nullptr, nullptr},
|
||||
};
|
||||
Reference in New Issue
Block a user