Skip to content

Commit

Permalink
Merge pull request #1898 from Sahnvour/translate-c-arrays
Browse files Browse the repository at this point in the history
Translate c arrays
  • Loading branch information
andrewrk authored Feb 16, 2019
2 parents a97362e + 075fda3 commit f571824
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/translate_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ static const char *decl_name(const Decl *decl) {
static AstNode *trans_create_node_apint(Context *c, const llvm::APSInt &aps_int) {
AstNode *node = trans_create_node(c, NodeTypeIntLiteral);
node->data.int_literal.bigint = allocate<BigInt>(1);
bool is_negative = aps_int.isNegative();
bool is_negative = aps_int.isSigned() && aps_int.isNegative();
if (!is_negative) {
bigint_init_data(node->data.int_literal.bigint, aps_int.getRawData(), aps_int.getNumWords(), false);
return node;
Expand Down Expand Up @@ -4092,10 +4092,13 @@ static AstNode *trans_ap_value(Context *c, APValue *ap_value, QualType qt, const
unsigned leftover_count = all_count - init_count;
AstNode *init_node = trans_create_node(c, NodeTypeContainerInitExpr);
AstNode *arr_type_node = trans_qual_type(c, qt, source_loc);
if (leftover_count != 0) { // We can't use the size of the final array for a partial initializer.
bigint_init_unsigned(arr_type_node->data.array_type.size->data.int_literal.bigint, init_count);
}
init_node->data.container_init_expr.type = arr_type_node;
init_node->data.container_init_expr.kind = ContainerInitKindArray;

QualType child_qt = qt.getTypePtr()->getLocallyUnqualifiedSingleStepDesugaredType();
QualType child_qt = qt.getTypePtr()->getAsArrayTypeUnsafe()->getElementType();

for (size_t i = 0; i < init_count; i += 1) {
APValue &elem_ap_val = ap_value->getArrayInitializedElt(i);
Expand All @@ -4113,10 +4116,14 @@ static AstNode *trans_ap_value(Context *c, APValue *ap_value, QualType qt, const
if (filler_node == nullptr)
return nullptr;

AstNode* filler_arr_type = trans_create_node(c, NodeTypeArrayType);
*filler_arr_type = *arr_type_node;
filler_arr_type->data.array_type.size = trans_create_node_unsigned(c, 1);

AstNode *filler_arr_1 = trans_create_node(c, NodeTypeContainerInitExpr);
init_node->data.container_init_expr.type = arr_type_node;
init_node->data.container_init_expr.kind = ContainerInitKindArray;
init_node->data.container_init_expr.entries.append(filler_node);
filler_arr_1->data.container_init_expr.type = filler_arr_type;
filler_arr_1->data.container_init_expr.kind = ContainerInitKindArray;
filler_arr_1->data.container_init_expr.entries.append(filler_node);

AstNode *rhs_node;
if (leftover_count == 1) {
Expand All @@ -4126,6 +4133,10 @@ static AstNode *trans_ap_value(Context *c, APValue *ap_value, QualType qt, const
rhs_node = trans_create_node_bin_op(c, filler_arr_1, BinOpTypeArrayMult, amt_node);
}

if (init_count == 0) {
return rhs_node;
}

return trans_create_node_bin_op(c, init_node, BinOpTypeArrayCat, rhs_node);
}
case APValue::LValue: {
Expand Down
30 changes: 30 additions & 0 deletions test/translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1416,4 +1416,34 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ }
\\}
);

// cases.add("empty array with initializer",
// "int a[4] = {};"
// ,
// "pub var a: [4]c_int = [1]c_int{0} ** 4;"
// );

// cases.add("array with initialization",
// "int a[4] = {1, 2, 3, 4};"
// ,
// "pub var a: [4]c_int = [4]c_int{1, 2, 3, 4};"
// );

// cases.add("array with incomplete initialization",
// "int a[4] = {3, 4};"
// ,
// "pub var a: [4]c_int = [2]c_int{3, 4} ++ ([1]c_int{0} ** 2);"
// );

// cases.add("2D array with initialization",
// "int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };"
// ,
// "pub var a: [3][3]c_int = [3][3]c_int{[3]c_int{1, 2, 3}, [3]c_int{4, 5, 6}, [3]c_int{7, 8, 9}};"
// );

// cases.add("2D array with incomplete initialization",
// "int a[3][3] = { {1, 2}, {4, 5, 6} };"
// ,
// "pub var a: [3][3]c_int = [2][3]c_int{[2]c_int{1, 2} ++ [1]c_int{0}, [3]c_int{4, 5, 6}} ++ [1][3]c_int{[1]c_int{0} ** 3};"
// );
}

0 comments on commit f571824

Please sign in to comment.