parse-c: array access expression

master
Andrew Kelley 2017-09-21 01:38:16 -04:00
parent 67a5a3f3d7
commit 1360af847e
2 changed files with 32 additions and 6 deletions

View File

@ -1206,6 +1206,7 @@ static AstNode *trans_implicit_cast_expr(Context *c, AstNode *block, ImplicitCas
return trans_c_cast(c, stmt->getExprLoc(), stmt->getType(), target_node);
}
case CK_FunctionToPointerDecay:
case CK_ArrayToPointerDecay:
{
AstNode *target_node = trans_expr(c, true, block, stmt->getSubExpr(), TransRValue);
if (target_node == nullptr)
@ -1239,9 +1240,6 @@ static AstNode *trans_implicit_cast_expr(Context *c, AstNode *block, ImplicitCas
case CK_ToUnion:
emit_warning(c, stmt->getLocStart(), "TODO handle C translation cast CK_ToUnion");
return nullptr;
case CK_ArrayToPointerDecay:
emit_warning(c, stmt->getLocStart(), "TODO handle C translation cast CK_ArrayToPointerDecay");
return nullptr;
case CK_NullToPointer:
emit_warning(c, stmt->getLocStart(), "TODO handle C translation cast CK_NullToPointer");
return nullptr;
@ -1808,6 +1806,22 @@ static AstNode *trans_member_expr(Context *c, AstNode *block, MemberExpr *stmt)
return node;
}
static AstNode *trans_array_subscript_expr(Context *c, AstNode *block, ArraySubscriptExpr *stmt) {
AstNode *container_node = trans_expr(c, true, block, stmt->getBase(), TransRValue);
if (container_node == nullptr)
return nullptr;
AstNode *idx_node = trans_expr(c, true, block, stmt->getIdx(), TransRValue);
if (idx_node == nullptr)
return nullptr;
AstNode *node = trans_create_node(c, NodeTypeArrayAccessExpr);
node->data.array_access_expr.array_ref_expr = container_node;
node->data.array_access_expr.subscript = idx_node;
return node;
}
static AstNode *trans_stmt(Context *c, bool result_used, AstNode *block, Stmt *stmt, TransLRValue lrvalue) {
Stmt::StmtClass sc = stmt->getStmtClass();
switch (sc) {
@ -1841,6 +1855,8 @@ static AstNode *trans_stmt(Context *c, bool result_used, AstNode *block, Stmt *s
return skip_add_to_block_node;
case Stmt::MemberExprClass:
return trans_member_expr(c, block, (MemberExpr *)stmt);
case Stmt::ArraySubscriptExprClass:
return trans_array_subscript_expr(c, block, (ArraySubscriptExpr *)stmt);
case Stmt::CaseStmtClass:
emit_warning(c, stmt->getLocStart(), "TODO handle C CaseStmtClass");
return nullptr;
@ -1901,9 +1917,6 @@ static AstNode *trans_stmt(Context *c, bool result_used, AstNode *block, Stmt *s
case Stmt::ArrayInitLoopExprClass:
emit_warning(c, stmt->getLocStart(), "TODO handle C ArrayInitLoopExprClass");
return nullptr;
case Stmt::ArraySubscriptExprClass:
emit_warning(c, stmt->getLocStart(), "TODO handle C ArraySubscriptExprClass");
return nullptr;
case Stmt::ArrayTypeTraitExprClass:
emit_warning(c, stmt->getLocStart(), "TODO handle C ArrayTypeTraitExprClass");
return nullptr;

View File

@ -532,4 +532,17 @@ pub fn addCases(cases: &tests.ParseCContext) {
,
\\pub var array: [100]c_int = undefined;
);
cases.add("array access",
\\int array[100];
\\int foo(int index) {
\\ return array[index];
\\}
,
\\pub var array: [100]c_int = undefined;
\\export fn foo(index: c_int) -> c_int {
\\ return array[index];
\\}
);
}