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
522522 ZigClangAPSInt_getNumWords(negated), true);
523523 ZigClangAPSInt_free(negated);
524524 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;
526537}
527538
528539static const ZigClangType *qual_type_canon(ZigClangQualType qt) {
......@@ -1313,6 +1324,16 @@ static AstNode *trans_integer_literal(Context *c, ResultUsed result_used, const
13131324 return maybe_suppress_result(c, result_used, node);
13141325}
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
13161337static AstNode *trans_constant_expr(Context *c, ResultUsed result_used, const clang::ConstantExpr *expr) {
13171338 clang::Expr::EvalResult result;
13181339 if (!expr->EvaluateAsConstantExpr(result, clang::Expr::EvaluateForCodeGen,
......@@ -3549,8 +3570,8 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const ZigClangStmt *s
35493570 emit_warning(c, ZigClangStmt_getBeginLoc(stmt), "TODO handle C FixedPointLiteralClass");
35503571 return ErrorUnexpected;
35513572 case ZigClangStmt_FloatingLiteralClass:
3552 emit_warning(c, ZigClangStmt_getBeginLoc(stmt), "TODO handle C FloatingLiteralClass");
3553 return ErrorUnexpected;
3573 return wrap_stmt(out_node, out_child_scope, scope,
3574 trans_floating_literal(c, result_used, (const clang::FloatingLiteral *)stmt));
35543575 case ZigClangStmt_ExprWithCleanupsClass:
35553576 emit_warning(c, ZigClangStmt_getBeginLoc(stmt), "TODO handle C ExprWithCleanupsClass");
35563577 return ErrorUnexpected;