Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions src/translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4030,24 +4030,26 @@ fn transCPtrCast(
const src_child_type = src_ty.getPointeeType();
const dst_type_node = try transType(c, scope, ty, loc);

if (!src_ty.isArrayType() and ((src_child_type.isConstQualified() and
!child_type.isConstQualified()) or
(src_child_type.isVolatileQualified() and
!child_type.isVolatileQualified())))
// Implicit downcasting from higher to lower alignment values is forbidden,
// use @alignCast to side-step this problem
const rhs = if (qualTypeCanon(child_type).isVoidType())
// void has 1-byte alignment, so @alignCast is not needed
expr
else if (typeIsOpaque(c, qualTypeCanon(child_type), loc))
// For opaque types a ptrCast is enough
expr
else blk: {
break :blk try Tag.align_cast.create(c.arena, expr);
};

if (!src_ty.isArrayType() and
(((src_child_type.isConstQualified() or qualTypeIsFunction(src_child_type)) and
!child_type.isConstQualified()) or
(src_child_type.isVolatileQualified() and
!child_type.isVolatileQualified())))
{
return removeCVQualifiers(c, dst_type_node, expr);
return removeCVQualifiers(c, dst_type_node, rhs);
} else {
// Implicit downcasting from higher to lower alignment values is forbidden,
// use @alignCast to side-step this problem
const rhs = if (qualTypeCanon(child_type).isVoidType())
// void has 1-byte alignment, so @alignCast is not needed
expr
else if (typeIsOpaque(c, qualTypeCanon(child_type), loc))
// For opaque types a ptrCast is enough
expr
else blk: {
break :blk try Tag.align_cast.create(c.arena, expr);
};
return Tag.as.create(c.arena, .{
.lhs = dst_type_node,
.rhs = try Tag.ptr_cast.create(c.arena, rhs),
Expand Down Expand Up @@ -4342,6 +4344,11 @@ fn qualTypeIsBoolean(qt: clang.QualType) bool {
return qualTypeCanon(qt).isBooleanType();
}

fn qualTypeIsFunction(qt: clang.QualType) bool {
const type_class = qualTypeCanon(qt).getTypeClass();
return type_class == .FunctionProto or type_class == .FunctionNoProto;
}

fn qualTypeIntBitWidth(c: *Context, qt: clang.QualType) !u32 {
const ty = qt.getTypePtr();

Expand Down
Loading