authorgravatar for codroid@gmail.comhryx <codroid@gmail.com> 2019-05-27 14:38:09-07:00
committergravatar for codroid@gmail.comhryx <codroid@gmail.com> 2019-05-27 14:38:09-07:00
log3bbee1ba2e758d4bb5ea7d03b27133ffd6e4e9e2
treeda67b9310923766c57b764b38c8325035e953991
parent9c437f90327880a195f1090f726118a8f6742bc6
signature Commit is signed but in an unrecognized format.

expr: BitCast for ImplicitCastExpr


4 files changed, 62 insertions(+), 2 deletions(-)

src-self-hosted/clang.zig+4
......@@ -879,4 +879,8 @@ pub const ZigClangVarDecl_TLSKind = extern enum {
879879 Dynamic,
880880};
881881
882pub extern fn ZigClangImplicitCastExpr_getBeginLoc(*const ZigClangImplicitCastExpr) ZigClangSourceLocation;
882883pub extern fn ZigClangImplicitCastExpr_getCastKind(*const ZigClangImplicitCastExpr) ZigClangCK;
884pub extern fn ZigClangImplicitCastExpr_getSubExpr(*const ZigClangImplicitCastExpr) *const ZigClangExpr;
885
886pub extern fn ZigClangArrayType_getElementType(*const ZigClangArrayType) ZigClangQualType;
src-self-hosted/translate_c.zig+38-1
......@@ -107,6 +107,7 @@ const Context = struct {
107107 decl_table: DeclTable,
108108 global_scope: *Scope.Root,
109109 mode: Mode,
110 clang_context: *ZigClangASTContext,
110111
111112 fn a(c: *Context) *std.mem.Allocator {
112113 return &c.tree.arena_allocator.allocator;
......@@ -183,6 +184,7 @@ pub fn translate(
183184 .decl_table = DeclTable.init(arena),
184185 .global_scope = try arena.create(Scope.Root),
185186 .mode = mode,
187 .clang_context = ZigClangASTUnit_getASTContext(ast_unit).?,
186188 };
187189 context.global_scope.* = Scope.Root{
188190 .base = Scope{
......@@ -458,7 +460,14 @@ fn transImplicitCastExpr(
458460 scope: *Scope,
459461 expr: *const ZigClangImplicitCastExpr,
460462) !TransResult {
461 switch (ZigClangImplicitCastExpr_getCastKind(@ptrCast(*const ZigClangImplicitCastExpr, expr))) {
463 const c = rp.c;
464 switch (ZigClangImplicitCastExpr_getCastKind(expr)) {
465 .BitCast => {
466 const node = try transExpr(rp, scope, @ptrCast(*const ZigClangExpr, expr), .used, .r_value);
467 const dest_type = getExprQualType(c, @ptrCast(*const ZigClangExpr, expr));
468 const src_type = getExprQualType(c, ZigClangImplicitCastExpr_getSubExpr(expr));
469 return try transCCast(rp, scope, ZigClangImplicitCastExpr_getBeginLoc(expr), dest_type, src_type, node.node);
470 },
462471 else => |kind| return revertAndWarn(
463472 rp,
464473 error.UnsupportedTranslation,
......@@ -469,6 +478,17 @@ fn transImplicitCastExpr(
469478 }
470479}
471480
481fn transCCast(
482 rp: RestorePoint,
483 scope: *Scope,
484 loc: ZigClangSourceLocation,
485 dst_type: ZigClangQualType,
486 src_type: ZigClangQualType,
487 target_node: *ast.Node,
488) !TransResult {
489 return revertAndWarn(rp, error.UnsupportedTranslation, loc, "TODO implement translation of C cast");
490}
491
472492fn transExpr(
473493 rp: RestorePoint,
474494 scope: *Scope,
......@@ -499,6 +519,23 @@ fn qualTypeCanon(qt: ZigClangQualType) *const ZigClangType {
499519 return ZigClangQualType_getTypePtr(canon);
500520}
501521
522fn getExprQualType(c: *Context, expr: *const ZigClangExpr) ZigClangQualType {
523 blk: {
524 // If this is a C `char *`, turn it into a `const char *`
525 if (ZigClangExpr_getStmtClass(expr) != .ImplicitCastExprClass) break :blk;
526 const cast_expr = @ptrCast(*const ZigClangImplicitCastExpr, expr);
527 if (ZigClangImplicitCastExpr_getCastKind(cast_expr) != .ArrayToPointerDecay) break :blk;
528 const sub_expr = ZigClangImplicitCastExpr_getSubExpr(cast_expr);
529 if (ZigClangExpr_getStmtClass(sub_expr) != .StringLiteralClass) break :blk;
530 const array_qt = ZigClangExpr_getType(sub_expr);
531 const array_type = @ptrCast(*const ZigClangArrayType, ZigClangQualType_getTypePtr(array_qt));
532 var pointee_qt = ZigClangArrayType_getElementType(array_type);
533 ZigClangQualType_addConst(&pointee_qt);
534 return ZigClangASTContext_getPointerType(c.clang_context, pointee_qt);
535 }
536 return ZigClangExpr_getType(expr);
537}
538
502539const RestorePoint = struct {
503540 c: *Context,
504541 token_index: ast.TokenIndex,
src/zig_clang.cpp+16-1
......@@ -1920,7 +1920,22 @@ const struct ZigClangStringLiteral *ZigClangPredefinedExpr_getFunctionName(
19201920 return reinterpret_cast<const struct ZigClangStringLiteral *>(result);
19211921}
19221922
1923ZigClangSourceLocation ZigClangImplicitCastExpr_getBeginLoc(const struct ZigClangImplicitCastExpr *self) {
1924 auto casted = reinterpret_cast<const clang::ImplicitCastExpr *>(self);
1925 return bitcast(casted->getBeginLoc());
1926}
1927
19231928enum ZigClangCK ZigClangImplicitCastExpr_getCastKind(const struct ZigClangImplicitCastExpr *self) {
1924 auto casted = reinterpret_cast<const clang::CastExpr *>(self);
1929 auto casted = reinterpret_cast<const clang::ImplicitCastExpr *>(self);
19251930 return (ZigClangCK)casted->getCastKind();
19261931}
1932
1933const struct ZigClangExpr *ZigClangImplicitCastExpr_getSubExpr(const struct ZigClangImplicitCastExpr *self) {
1934 auto casted = reinterpret_cast<const clang::ImplicitCastExpr *>(self);
1935 return reinterpret_cast<const struct ZigClangExpr *>(casted->getSubExpr());
1936}
1937
1938struct ZigClangQualType ZigClangArrayType_getElementType(const struct ZigClangArrayType *self) {
1939 auto casted = reinterpret_cast<const clang::ArrayType *>(self);
1940 return bitcast(casted->getElementType());
1941}
src/zig_clang.h+4
......@@ -870,6 +870,10 @@ ZIG_EXTERN_C const char *ZigClangStringLiteral_getString_bytes_begin_size(const
870870ZIG_EXTERN_C const struct ZigClangStringLiteral *ZigClangPredefinedExpr_getFunctionName(
871871 const struct ZigClangPredefinedExpr *self);
872872
873ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangImplicitCastExpr_getBeginLoc(const struct ZigClangImplicitCastExpr *);
873874ZIG_EXTERN_C enum ZigClangCK ZigClangImplicitCastExpr_getCastKind(const struct ZigClangImplicitCastExpr *);
875ZIG_EXTERN_C const struct ZigClangExpr *ZigClangImplicitCastExpr_getSubExpr(const struct ZigClangImplicitCastExpr *);
876
877ZIG_EXTERN_C struct ZigClangQualType ZigClangArrayType_getElementType(const struct ZigClangArrayType *);
874878
875879#endif