| author | |
| committer | |
| log | 3bbee1ba2e758d4bb5ea7d03b27133ffd6e4e9e2 |
| tree | da67b9310923766c57b764b38c8325035e953991 |
| parent | 9c437f90327880a195f1090f726118a8f6742bc6 |
| signature |
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 | }; |
| 881 | 881 | ||
| 882 | pub extern fn ZigClangImplicitCastExpr_getBeginLoc(*const ZigClangImplicitCastExpr) ZigClangSourceLocation; | ||
| 882 | pub extern fn ZigClangImplicitCastExpr_getCastKind(*const ZigClangImplicitCastExpr) ZigClangCK; | 883 | pub extern fn ZigClangImplicitCastExpr_getCastKind(*const ZigClangImplicitCastExpr) ZigClangCK; |
| 884 | pub extern fn ZigClangImplicitCastExpr_getSubExpr(*const ZigClangImplicitCastExpr) *const ZigClangExpr; | ||
| 885 | |||
| 886 | pub 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, | ||
| 110 | 111 | ||
| 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 | } |
| 471 | 480 | ||
| 481 | fn 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 | |||
| 472 | fn transExpr( | 492 | fn 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 | } |
| 501 | 521 | ||
| 522 | fn 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 | |||
| 502 | const RestorePoint = struct { | 539 | const 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 | } |
| 1922 | 1922 | ||
| 1923 | ZigClangSourceLocation ZigClangImplicitCastExpr_getBeginLoc(const struct ZigClangImplicitCastExpr *self) { | ||
| 1924 | auto casted = reinterpret_cast<const clang::ImplicitCastExpr *>(self); | ||
| 1925 | return bitcast(casted->getBeginLoc()); | ||
| 1926 | } | ||
| 1927 | |||
| 1923 | enum ZigClangCK ZigClangImplicitCastExpr_getCastKind(const struct ZigClangImplicitCastExpr *self) { | 1928 | enum 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 | |||
| 1933 | const 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 | |||
| 1938 | struct 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 |
| 870 | ZIG_EXTERN_C const struct ZigClangStringLiteral *ZigClangPredefinedExpr_getFunctionName( | 870 | ZIG_EXTERN_C const struct ZigClangStringLiteral *ZigClangPredefinedExpr_getFunctionName( |
| 871 | const struct ZigClangPredefinedExpr *self); | 871 | const struct ZigClangPredefinedExpr *self); |
| 872 | 872 | ||
| 873 | ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangImplicitCastExpr_getBeginLoc(const struct ZigClangImplicitCastExpr *); | ||
| 873 | ZIG_EXTERN_C enum ZigClangCK ZigClangImplicitCastExpr_getCastKind(const struct ZigClangImplicitCastExpr *); | 874 | ZIG_EXTERN_C enum ZigClangCK ZigClangImplicitCastExpr_getCastKind(const struct ZigClangImplicitCastExpr *); |
| 875 | ZIG_EXTERN_C const struct ZigClangExpr *ZigClangImplicitCastExpr_getSubExpr(const struct ZigClangImplicitCastExpr *); | ||
| 876 | |||
| 877 | ZIG_EXTERN_C struct ZigClangQualType ZigClangArrayType_getElementType(const struct ZigClangArrayType *); | ||
| 874 | 878 | ||
| 875 | #endif | 879 | #endif |