authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-16 14:07:41-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-02-16 14:07:41-05:00
logf57182456d52615ece1133db9c33ef57e3ae187b
treed9320d217040c2a2384a615278ed259424043310
parenta97362e67739c94d6266cc2d1352dd9bb1b713be
parent075fda3c732dc0e34d9917cc9c441b4dff75aa0e
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #1898 from Sahnvour/translate-c-arrays

Translate c arrays

2 files changed, 46 insertions(+), 5 deletions(-)

src/translate_c.cpp+16-5
......@@ -479,7 +479,7 @@ static const char *decl_name(const Decl *decl) {
479479static AstNode *trans_create_node_apint(Context *c, const llvm::APSInt &aps_int) {
480480 AstNode *node = trans_create_node(c, NodeTypeIntLiteral);
481481 node->data.int_literal.bigint = allocate<BigInt>(1);
482 bool is_negative = aps_int.isNegative();
482 bool is_negative = aps_int.isSigned() && aps_int.isNegative();
483483 if (!is_negative) {
484484 bigint_init_data(node->data.int_literal.bigint, aps_int.getRawData(), aps_int.getNumWords(), false);
485485 return node;
......@@ -4092,10 +4092,13 @@ static AstNode *trans_ap_value(Context *c, APValue *ap_value, QualType qt, const
40924092 unsigned leftover_count = all_count - init_count;
40934093 AstNode *init_node = trans_create_node(c, NodeTypeContainerInitExpr);
40944094 AstNode *arr_type_node = trans_qual_type(c, qt, source_loc);
4095 if (leftover_count != 0) { // We can't use the size of the final array for a partial initializer.
4096 bigint_init_unsigned(arr_type_node->data.array_type.size->data.int_literal.bigint, init_count);
4097 }
40954098 init_node->data.container_init_expr.type = arr_type_node;
40964099 init_node->data.container_init_expr.kind = ContainerInitKindArray;
40974100
4098 QualType child_qt = qt.getTypePtr()->getLocallyUnqualifiedSingleStepDesugaredType();
4101 QualType child_qt = qt.getTypePtr()->getAsArrayTypeUnsafe()->getElementType();
40994102
41004103 for (size_t i = 0; i < init_count; i += 1) {
41014104 APValue &elem_ap_val = ap_value->getArrayInitializedElt(i);
......@@ -4113,10 +4116,14 @@ static AstNode *trans_ap_value(Context *c, APValue *ap_value, QualType qt, const
41134116 if (filler_node == nullptr)
41144117 return nullptr;
41154118
4119 AstNode* filler_arr_type = trans_create_node(c, NodeTypeArrayType);
4120 *filler_arr_type = *arr_type_node;
4121 filler_arr_type->data.array_type.size = trans_create_node_unsigned(c, 1);
4122
41164123 AstNode *filler_arr_1 = trans_create_node(c, NodeTypeContainerInitExpr);
4117 init_node->data.container_init_expr.type = arr_type_node;
4118 init_node->data.container_init_expr.kind = ContainerInitKindArray;
4119 init_node->data.container_init_expr.entries.append(filler_node);
4124 filler_arr_1->data.container_init_expr.type = filler_arr_type;
4125 filler_arr_1->data.container_init_expr.kind = ContainerInitKindArray;
4126 filler_arr_1->data.container_init_expr.entries.append(filler_node);
41204127
41214128 AstNode *rhs_node;
41224129 if (leftover_count == 1) {
......@@ -4126,6 +4133,10 @@ static AstNode *trans_ap_value(Context *c, APValue *ap_value, QualType qt, const
41264133 rhs_node = trans_create_node_bin_op(c, filler_arr_1, BinOpTypeArrayMult, amt_node);
41274134 }
41284135
4136 if (init_count == 0) {
4137 return rhs_node;
4138 }
4139
41294140 return trans_create_node_bin_op(c, init_node, BinOpTypeArrayCat, rhs_node);
41304141 }
41314142 case APValue::LValue: {
test/translate_c.zig+30
......@@ -1416,4 +1416,34 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14161416 \\ }
14171417 \\}
14181418 );
1419
1420 // cases.add("empty array with initializer",
1421 // "int a[4] = {};"
1422 // ,
1423 // "pub var a: [4]c_int = [1]c_int{0} ** 4;"
1424 // );
1425
1426 // cases.add("array with initialization",
1427 // "int a[4] = {1, 2, 3, 4};"
1428 // ,
1429 // "pub var a: [4]c_int = [4]c_int{1, 2, 3, 4};"
1430 // );
1431
1432 // cases.add("array with incomplete initialization",
1433 // "int a[4] = {3, 4};"
1434 // ,
1435 // "pub var a: [4]c_int = [2]c_int{3, 4} ++ ([1]c_int{0} ** 2);"
1436 // );
1437
1438 // cases.add("2D array with initialization",
1439 // "int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };"
1440 // ,
1441 // "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}};"
1442 // );
1443
1444 // cases.add("2D array with incomplete initialization",
1445 // "int a[3][3] = { {1, 2}, {4, 5, 6} };"
1446 // ,
1447 // "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};"
1448 // );
14191449}