aboutsummaryrefslogtreecommitdiff
path: root/src/translate_c.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/translate_c.cpp')
-rw-r--r--src/translate_c.cpp35
1 files changed, 20 insertions, 15 deletions
diff --git a/src/translate_c.cpp b/src/translate_c.cpp
index 6765bf45fb..3acfb05e85 100644
--- a/src/translate_c.cpp
+++ b/src/translate_c.cpp
@@ -221,6 +221,15 @@ static AstNode *trans_create_node_opaque(Context *c) {
return trans_create_node_builtin_fn_call_str(c, "OpaqueType");
}
+static AstNode *trans_create_node_cast(Context *c, AstNode *dest_type, AstNode *operand) {
+ AstNode *node = trans_create_node(c, NodeTypeFnCallExpr);
+ node->data.fn_call_expr.fn_ref_expr = trans_create_node_symbol(c, buf_create_from_str("as"));
+ node->data.fn_call_expr.modifier = CallModifierBuiltin;
+ node->data.fn_call_expr.params.append(dest_type);
+ node->data.fn_call_expr.params.append(operand);
+ return node;
+}
+
static AstNode *trans_create_node_fn_call_1(Context *c, AstNode *fn_ref_expr, AstNode *arg1) {
AstNode *node = trans_create_node(c, NodeTypeFnCallExpr);
node->data.fn_call_expr.fn_ref_expr = fn_ref_expr;
@@ -337,14 +346,6 @@ static AstNode *trans_create_node_unsigned(Context *c, uint64_t x) {
return trans_create_node_unsigned_negative(c, x, false);
}
-static AstNode *trans_create_node_cast(Context *c, AstNode *dest, AstNode *src) {
- AstNode *node = trans_create_node(c, NodeTypeFnCallExpr);
- node->data.fn_call_expr.fn_ref_expr = dest;
- node->data.fn_call_expr.params.resize(1);
- node->data.fn_call_expr.params.items[0] = src;
- return node;
-}
-
static AstNode *trans_create_node_unsigned_negative_type(Context *c, uint64_t x, bool is_negative,
const char *type_name)
{
@@ -701,7 +702,7 @@ static AstNode* trans_c_cast(Context *c, ZigClangSourceLocation source_location,
if (c_is_unsigned_integer(c, dest_type) && qual_type_is_ptr(src_type)) {
AstNode *addr_node = trans_create_node_builtin_fn_call_str(c, "ptrToInt");
addr_node->data.fn_call_expr.params.append(expr);
- return trans_create_node_fn_call_1(c, trans_qual_type(c, dest_type, source_location), addr_node);
+ return trans_create_node_cast(c, trans_qual_type(c, dest_type, source_location), addr_node);
}
if (c_is_unsigned_integer(c, src_type) && qual_type_is_ptr(dest_type)) {
AstNode *ptr_node = trans_create_node_builtin_fn_call_str(c, "intToPtr");
@@ -712,7 +713,7 @@ static AstNode* trans_c_cast(Context *c, ZigClangSourceLocation source_location,
// TODO: maybe widen to increase size
// TODO: maybe bitcast to change sign
// TODO: maybe truncate to reduce size
- return trans_create_node_fn_call_1(c, trans_qual_type(c, dest_type, source_location), expr);
+ return trans_create_node_cast(c, trans_qual_type(c, dest_type, source_location), expr);
}
static bool c_is_signed_integer(Context *c, ZigClangQualType qt) {
@@ -1212,6 +1213,11 @@ static AstNode *trans_type(Context *c, const ZigClangType *ty, ZigClangSourceLoc
const ZigClangAttributedType *attributed_ty = reinterpret_cast<const ZigClangAttributedType *>(ty);
return trans_qual_type(c, ZigClangAttributedType_getEquivalentType(attributed_ty), source_loc);
}
+ case ZigClangType_MacroQualified:
+ {
+ const ZigClangMacroQualifiedType *macroqualified_ty = reinterpret_cast<const ZigClangMacroQualifiedType *>(ty);
+ return trans_qual_type(c, ZigClangMacroQualifiedType_getModifiedType(macroqualified_ty), source_loc);
+ }
case ZigClangType_IncompleteArray:
{
const ZigClangIncompleteArrayType *incomplete_array_ty = reinterpret_cast<const ZigClangIncompleteArrayType *>(ty);
@@ -1261,7 +1267,6 @@ static AstNode *trans_type(Context *c, const ZigClangType *ty, ZigClangSourceLoc
case ZigClangType_DeducedTemplateSpecialization:
case ZigClangType_DependentAddressSpace:
case ZigClangType_DependentVector:
- case ZigClangType_MacroQualified:
emit_warning(c, source_loc, "unsupported type: '%s'", ZigClangType_getTypeClassName(ty));
return nullptr;
}
@@ -1527,7 +1532,7 @@ static AstNode *trans_create_shift_op(Context *c, TransScope *scope, ZigClangQua
AstNode *rhs = trans_expr(c, ResultUsedYes, scope, rhs_expr, TransRValue);
if (rhs == nullptr) return nullptr;
- AstNode *coerced_rhs = trans_create_node_fn_call_1(c, rhs_type, rhs);
+ AstNode *coerced_rhs = trans_create_node_cast(c, rhs_type, rhs);
return trans_create_node_bin_op(c, lhs, bin_op, coerced_rhs);
}
@@ -1702,7 +1707,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
AstNode *rhs = trans_expr(c, ResultUsedYes, scope, ZigClangCompoundAssignOperator_getRHS(stmt), TransRValue);
if (rhs == nullptr) return nullptr;
- AstNode *coerced_rhs = trans_create_node_fn_call_1(c, rhs_type, rhs);
+ AstNode *coerced_rhs = trans_create_node_cast(c, rhs_type, rhs);
return trans_create_node_bin_op(c, lhs, assign_op, coerced_rhs);
} else {
@@ -1733,7 +1738,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
AstNode *rhs = trans_expr(c, ResultUsedYes, &child_scope->base, ZigClangCompoundAssignOperator_getRHS(stmt), TransRValue);
if (rhs == nullptr) return nullptr;
- AstNode *coerced_rhs = trans_create_node_fn_call_1(c, rhs_type, rhs);
+ AstNode *coerced_rhs = trans_create_node_cast(c, rhs_type, rhs);
// operation_type(*_ref)
AstNode *operation_type_cast = trans_c_cast(c, rhs_location,
@@ -2684,7 +2689,7 @@ static AstNode *to_enum_zero_cmp(Context *c, AstNode *expr, AstNode *enum_type)
// @TagType(Enum)(0)
AstNode *zero = trans_create_node_unsigned_negative(c, 0, false);
- AstNode *casted_zero = trans_create_node_fn_call_1(c, tag_type, zero);
+ AstNode *casted_zero = trans_create_node_cast(c, tag_type, zero);
// @bitCast(Enum, @TagType(Enum)(0))
AstNode *bitcast = trans_create_node_builtin_fn_call_str(c, "bitCast");