authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-04-16 20:43:08+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-04-19 11:50:30+02:00
log6d95c5d15c93faf1ac2bed4059b9278f5a997654
tree2630c422f1a57f9334753edfb9c2b65a144734a3
parent1fda44cbc85a61da048017b0820ab82d1a9817df

translate-c: Parse float/double literals


1 files changed, 23 insertions(+), 2 deletions(-)

src/translate_c.cpp+23-2
...@@ -522,7 +522,18 @@ static AstNode *trans_create_node_apint(Context *c, const ZigClangAPSInt *aps_in...@@ -522,7 +522,18 @@ static AstNode *trans_create_node_apint(Context *c, const ZigClangAPSInt *aps_in
522 ZigClangAPSInt_getNumWords(negated), true);522 ZigClangAPSInt_getNumWords(negated), true);
523 ZigClangAPSInt_free(negated);523 ZigClangAPSInt_free(negated);
524 return node;524 return node;
525}
525526
527static AstNode *trans_create_node_apfloat(Context *c, const llvm::APFloat &ap_float) {
528 uint8_t buf[128];
529 size_t written = ap_float.convertToHexString((char *)buf, 0, false,
530 llvm::APFloat::rmNearestTiesToEven);
531 AstNode *node = trans_create_node(c, NodeTypeFloatLiteral);
532 node->data.float_literal.bigfloat = allocate<BigFloat>(1);
533 if (bigfloat_init_buf(node->data.float_literal.bigfloat, buf, written)) {
534 node->data.float_literal.overflow = true;
535 }
536 return node;
526}537}
527538
528static const ZigClangType *qual_type_canon(ZigClangQualType qt) {539static const ZigClangType *qual_type_canon(ZigClangQualType qt) {
...@@ -1313,6 +1324,16 @@ static AstNode *trans_integer_literal(Context *c, ResultUsed result_used, const...@@ -1313,6 +1324,16 @@ static AstNode *trans_integer_literal(Context *c, ResultUsed result_used, const
1313 return maybe_suppress_result(c, result_used, node);1324 return maybe_suppress_result(c, result_used, node);
1314}1325}
13151326
1327static AstNode *trans_floating_literal(Context *c, ResultUsed result_used, const clang::FloatingLiteral *stmt) {
1328 llvm::APFloat result{0.0f};
1329 if (!stmt->EvaluateAsFloat(result, *reinterpret_cast<clang::ASTContext *>(c->ctx))) {
1330 emit_warning(c, bitcast(stmt->getBeginLoc()), "invalid floating literal");
1331 return nullptr;
1332 }
1333 AstNode *node = trans_create_node_apfloat(c, result);
1334 return maybe_suppress_result(c, result_used, node);
1335}
1336
1316static AstNode *trans_constant_expr(Context *c, ResultUsed result_used, const clang::ConstantExpr *expr) {1337static AstNode *trans_constant_expr(Context *c, ResultUsed result_used, const clang::ConstantExpr *expr) {
1317 clang::Expr::EvalResult result;1338 clang::Expr::EvalResult result;
1318 if (!expr->EvaluateAsConstantExpr(result, clang::Expr::EvaluateForCodeGen,1339 if (!expr->EvaluateAsConstantExpr(result, clang::Expr::EvaluateForCodeGen,
...@@ -3549,8 +3570,8 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const ZigClangStmt *s...@@ -3549,8 +3570,8 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const ZigClangStmt *s
3549 emit_warning(c, ZigClangStmt_getBeginLoc(stmt), "TODO handle C FixedPointLiteralClass");3570 emit_warning(c, ZigClangStmt_getBeginLoc(stmt), "TODO handle C FixedPointLiteralClass");
3550 return ErrorUnexpected;3571 return ErrorUnexpected;
3551 case ZigClangStmt_FloatingLiteralClass:3572 case ZigClangStmt_FloatingLiteralClass:
3552 emit_warning(c, ZigClangStmt_getBeginLoc(stmt), "TODO handle C FloatingLiteralClass");3573 return wrap_stmt(out_node, out_child_scope, scope,
3553 return ErrorUnexpected;3574 trans_floating_literal(c, result_used, (const clang::FloatingLiteral *)stmt));
3554 case ZigClangStmt_ExprWithCleanupsClass:3575 case ZigClangStmt_ExprWithCleanupsClass:
3555 emit_warning(c, ZigClangStmt_getBeginLoc(stmt), "TODO handle C ExprWithCleanupsClass");3576 emit_warning(c, ZigClangStmt_getBeginLoc(stmt), "TODO handle C ExprWithCleanupsClass");
3556 return ErrorUnexpected;3577 return ErrorUnexpected;