authorgravatar for michaeltnoronha@gmail.comMichael Noronha <michaeltnoronha@gmail.com> 2018-08-20 11:29:26-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-20 14:29:26-04:00
logb8ce8f219c48ae833199130ce575241f848d690b
treed8e17d413ef63a156daafcc679ba4cd62913469f
parent3ee1b60edf8ee53ac7850aaee13c43f2db3a8661

translate-c: Correctly translate enum init values, addressing #1360 (#1377)

* translate-c: Correctly translate enum init values * translate-c: Test enum initialization * translate-c: Flip to positive using APSInt builtins * src/translate_c.cpp: correctly bridge llvm::APSInt with Zig BigInt; ACHTUNG: llvm::APSInt stores an int's sign inside of its getRawData; Internally to Zig we store an integer's sign outside of getRawData! (~aps_int) calls .flip() internally on the raw data to match Zig. * test/translate_c.zig: enum: add wider range of values (u64) to try; closes #1360

2 files changed, 62 insertions(+), 2 deletions(-)

src/translate_c.cpp+8-2
......@@ -458,9 +458,15 @@ static const char *decl_name(const Decl *decl) {
458458static AstNode *trans_create_node_apint(Context *c, const llvm::APSInt &aps_int) {
459459 AstNode *node = trans_create_node(c, NodeTypeIntLiteral);
460460 node->data.int_literal.bigint = allocate<BigInt>(1);
461 bigint_init_data(node->data.int_literal.bigint, aps_int.getRawData(), aps_int.getNumWords(), aps_int.isNegative());
461 bool is_negative = aps_int.isNegative();
462 // ACHTUNG: llvm::APSInt stores an int's sign inside of its getRawData;
463 // Internally to Zig we store an integer's sign outside of getRawData!
464 // ++(~aps_int) calls .flip() internally on the raw data to match Zig.
465 bigint_init_data( node->data.int_literal.bigint
466 , (is_negative ? (++(~aps_int)) : aps_int).getRawData()
467 , aps_int.getNumWords()
468 , is_negative );
462469 return node;
463
464470}
465471
466472static const Type *qual_type_canon(QualType qt) {
test/translate_c.zig+54
......@@ -1358,4 +1358,58 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13581358 \\ }
13591359 \\}
13601360 );
1361
1362 cases.add("correctly translate enum init values",
1363 \\enum EnumWithInits {
1364 \\ VAL01 = 0,
1365 \\ VAL02 = 1,
1366 \\ VAL03 = 2,
1367 \\ VAL04 = 3,
1368 \\ VAL05 = -1,
1369 \\ VAL06 = -2,
1370 \\ VAL07 = -3,
1371 \\ VAL08 = -4,
1372 \\ VAL09 = VAL02 + VAL08,
1373 \\ VAL10 = -1000012000,
1374 \\ VAL11 = -1000161000,
1375 \\ VAL12 = -1000174001,
1376 \\ VAL13 = VAL09,
1377 \\ VAL14 = VAL10,
1378 \\ VAL15 = VAL11,
1379 \\ VAL16 = VAL13,
1380 \\ VAL17 = (VAL16 - VAL10 + 1),
1381 \\ VAL18 = 0x1000000000000000L,
1382 \\ VAL19 = VAL18 + VAL18 + VAL18 - 1,
1383 \\ VAL20 = VAL19 + VAL19,
1384 \\ VAL21 = VAL20 + 0xFFFFFFFFFFFFFFFF,
1385 \\ VAL22 = 0xFFFFFFFFFFFFFFFF + 1,
1386 \\ VAL23 = 0xFFFFFFFFFFFFFFFF,
1387 \\};
1388 ,
1389 \\pub const enum_EnumWithInits = extern enum(c_longlong) {
1390 \\ VAL01 = 0,
1391 \\ VAL02 = 1,
1392 \\ VAL03 = 2,
1393 \\ VAL04 = 3,
1394 \\ VAL05 = -1,
1395 \\ VAL06 = -2,
1396 \\ VAL07 = -3,
1397 \\ VAL08 = -4,
1398 \\ VAL09 = -3,
1399 \\ VAL10 = -1000012000,
1400 \\ VAL11 = -1000161000,
1401 \\ VAL12 = -1000174001,
1402 \\ VAL13 = -3,
1403 \\ VAL14 = -1000012000,
1404 \\ VAL15 = -1000161000,
1405 \\ VAL16 = -3,
1406 \\ VAL17 = 1000011998,
1407 \\ VAL18 = 1152921504606846976,
1408 \\ VAL19 = 3458764513820540927,
1409 \\ VAL20 = 6917529027641081854,
1410 \\ VAL21 = 6917529027641081853,
1411 \\ VAL22 = 0,
1412 \\ VAL23 = -1,
1413 \\};
1414 );
13611415}