From 6d95c5d15c93faf1ac2bed4059b9278f5a997654 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 16 Apr 2019 20:43:08 +0200 Subject: [PATCH] translate-c: Parse float/double literals --- src/translate_c.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/translate_c.cpp b/src/translate_c.cpp index e466e9d56e..b6841cbada 100644 --- a/src/translate_c.cpp +++ b/src/translate_c.cpp @@ -522,7 +522,18 @@ static AstNode *trans_create_node_apint(Context *c, const ZigClangAPSInt *aps_in ZigClangAPSInt_getNumWords(negated), true); ZigClangAPSInt_free(negated); return node; +} +static AstNode *trans_create_node_apfloat(Context *c, const llvm::APFloat &ap_float) { + uint8_t buf[128]; + size_t written = ap_float.convertToHexString((char *)buf, 0, false, + llvm::APFloat::rmNearestTiesToEven); + AstNode *node = trans_create_node(c, NodeTypeFloatLiteral); + node->data.float_literal.bigfloat = allocate(1); + if (bigfloat_init_buf(node->data.float_literal.bigfloat, buf, written)) { + node->data.float_literal.overflow = true; + } + return node; } static const ZigClangType *qual_type_canon(ZigClangQualType qt) { @@ -1313,6 +1324,16 @@ static AstNode *trans_integer_literal(Context *c, ResultUsed result_used, const return maybe_suppress_result(c, result_used, node); } +static AstNode *trans_floating_literal(Context *c, ResultUsed result_used, const clang::FloatingLiteral *stmt) { + llvm::APFloat result{0.0f}; + if (!stmt->EvaluateAsFloat(result, *reinterpret_cast(c->ctx))) { + emit_warning(c, bitcast(stmt->getBeginLoc()), "invalid floating literal"); + return nullptr; + } + AstNode *node = trans_create_node_apfloat(c, result); + return maybe_suppress_result(c, result_used, node); +} + static AstNode *trans_constant_expr(Context *c, ResultUsed result_used, const clang::ConstantExpr *expr) { clang::Expr::EvalResult result; if (!expr->EvaluateAsConstantExpr(result, clang::Expr::EvaluateForCodeGen, @@ -3549,8 +3570,8 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const ZigClangStmt *s emit_warning(c, ZigClangStmt_getBeginLoc(stmt), "TODO handle C FixedPointLiteralClass"); return ErrorUnexpected; case ZigClangStmt_FloatingLiteralClass: - emit_warning(c, ZigClangStmt_getBeginLoc(stmt), "TODO handle C FloatingLiteralClass"); - return ErrorUnexpected; + return wrap_stmt(out_node, out_child_scope, scope, + trans_floating_literal(c, result_used, (const clang::FloatingLiteral *)stmt)); case ZigClangStmt_ExprWithCleanupsClass: emit_warning(c, ZigClangStmt_getBeginLoc(stmt), "TODO handle C ExprWithCleanupsClass"); return ErrorUnexpected;