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 {...@@ -879,4 +879,8 @@ pub const ZigClangVarDecl_TLSKind = extern enum {
879 Dynamic,879 Dynamic,
880};880};
881881
882pub extern fn ZigClangImplicitCastExpr_getBeginLoc(*const ZigClangImplicitCastExpr) ZigClangSourceLocation;
882pub extern fn ZigClangImplicitCastExpr_getCastKind(*const ZigClangImplicitCastExpr) ZigClangCK;883pub 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 {...@@ -107,6 +107,7 @@ const Context = struct {
107 decl_table: DeclTable,107 decl_table: DeclTable,
108 global_scope: *Scope.Root,108 global_scope: *Scope.Root,
109 mode: Mode,109 mode: Mode,
110 clang_context: *ZigClangASTContext,
110111
111 fn a(c: *Context) *std.mem.Allocator {112 fn a(c: *Context) *std.mem.Allocator {
112 return &c.tree.arena_allocator.allocator;113 return &c.tree.arena_allocator.allocator;
...@@ -183,6 +184,7 @@ pub fn translate(...@@ -183,6 +184,7 @@ pub fn translate(
183 .decl_table = DeclTable.init(arena),184 .decl_table = DeclTable.init(arena),
184 .global_scope = try arena.create(Scope.Root),185 .global_scope = try arena.create(Scope.Root),
185 .mode = mode,186 .mode = mode,
187 .clang_context = ZigClangASTUnit_getASTContext(ast_unit).?,
186 };188 };
187 context.global_scope.* = Scope.Root{189 context.global_scope.* = Scope.Root{
188 .base = Scope{190 .base = Scope{
...@@ -458,7 +460,14 @@ fn transImplicitCastExpr(...@@ -458,7 +460,14 @@ fn transImplicitCastExpr(
458 scope: *Scope,460 scope: *Scope,
459 expr: *const ZigClangImplicitCastExpr,461 expr: *const ZigClangImplicitCastExpr,
460) !TransResult {462) !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 },
462 else => |kind| return revertAndWarn(471 else => |kind| return revertAndWarn(
463 rp,472 rp,
464 error.UnsupportedTranslation,473 error.UnsupportedTranslation,
...@@ -469,6 +478,17 @@ fn transImplicitCastExpr(...@@ -469,6 +478,17 @@ fn transImplicitCastExpr(
469 }478 }
470}479}
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
472fn transExpr(492fn transExpr(
473 rp: RestorePoint,493 rp: RestorePoint,
474 scope: *Scope,494 scope: *Scope,
...@@ -499,6 +519,23 @@ fn qualTypeCanon(qt: ZigClangQualType) *const ZigClangType {...@@ -499,6 +519,23 @@ fn qualTypeCanon(qt: ZigClangQualType) *const ZigClangType {
499 return ZigClangQualType_getTypePtr(canon);519 return ZigClangQualType_getTypePtr(canon);
500}520}
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
502const RestorePoint = struct {539const RestorePoint = struct {
503 c: *Context,540 c: *Context,
504 token_index: ast.TokenIndex,541 token_index: ast.TokenIndex,
src/zig_clang.cpp+16-1
...@@ -1920,7 +1920,22 @@ const struct ZigClangStringLiteral *ZigClangPredefinedExpr_getFunctionName(...@@ -1920,7 +1920,22 @@ const struct ZigClangStringLiteral *ZigClangPredefinedExpr_getFunctionName(
1920 return reinterpret_cast<const struct ZigClangStringLiteral *>(result);1920 return reinterpret_cast<const struct ZigClangStringLiteral *>(result);
1921}1921}
19221922
1923ZigClangSourceLocation ZigClangImplicitCastExpr_getBeginLoc(const struct ZigClangImplicitCastExpr *self) {
1924 auto casted = reinterpret_cast<const clang::ImplicitCastExpr *>(self);
1925 return bitcast(casted->getBeginLoc());
1926}
1927
1923enum ZigClangCK ZigClangImplicitCastExpr_getCastKind(const struct ZigClangImplicitCastExpr *self) {1928enum 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);
1925 return (ZigClangCK)casted->getCastKind();1930 return (ZigClangCK)casted->getCastKind();
1926}1931}
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...@@ -870,6 +870,10 @@ ZIG_EXTERN_C const char *ZigClangStringLiteral_getString_bytes_begin_size(const
870ZIG_EXTERN_C const struct ZigClangStringLiteral *ZigClangPredefinedExpr_getFunctionName(870ZIG_EXTERN_C const struct ZigClangStringLiteral *ZigClangPredefinedExpr_getFunctionName(
871 const struct ZigClangPredefinedExpr *self);871 const struct ZigClangPredefinedExpr *self);
872872
873ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangImplicitCastExpr_getBeginLoc(const struct ZigClangImplicitCastExpr *);
873ZIG_EXTERN_C enum ZigClangCK ZigClangImplicitCastExpr_getCastKind(const struct ZigClangImplicitCastExpr *);874ZIG_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
875#endif879#endif