From b558d0996a8a18475f91483d3e3962502422a315 Mon Sep 17 00:00:00 2001 From: hryx Date: Mon, 27 May 2019 19:20:23 -0700 Subject: [PATCH] expr: DeclRefExpr --- src-self-hosted/clang.zig | 2 ++ src-self-hosted/translate_c.zig | 31 +++++++++++++++++++++++++++++++ src/zig_clang.cpp | 5 +++++ src/zig_clang.h | 2 ++ 4 files changed, 40 insertions(+) diff --git a/src-self-hosted/clang.zig b/src-self-hosted/clang.zig index ea8f3a6cc6..d1fe5153bb 100644 --- a/src-self-hosted/clang.zig +++ b/src-self-hosted/clang.zig @@ -884,3 +884,5 @@ pub extern fn ZigClangImplicitCastExpr_getCastKind(*const ZigClangImplicitCastEx pub extern fn ZigClangImplicitCastExpr_getSubExpr(*const ZigClangImplicitCastExpr) *const ZigClangExpr; pub extern fn ZigClangArrayType_getElementType(*const ZigClangArrayType) ZigClangQualType; + +pub extern fn ZigClangDeclRefExpr_getDecl(*const ZigClangDeclRefExpr) *const ZigClangValueDecl; diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index 2ccd4eca6d..ffb325d192 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -107,6 +107,7 @@ const Context = struct { decl_table: DeclTable, global_scope: *Scope.Root, mode: Mode, + ptr_params: std.BufSet, clang_context: *ZigClangASTContext, fn a(c: *Context) *std.mem.Allocator { @@ -184,6 +185,7 @@ pub fn translate( .decl_table = DeclTable.init(arena), .global_scope = try arena.create(Scope.Root), .mode = mode, + .ptr_params = std.BufSet.init(arena), .clang_context = ZigClangASTUnit_getASTContext(ast_unit).?, }; context.global_scope.* = Scope.Root{ @@ -326,6 +328,7 @@ fn transStmt( switch (sc) { .CompoundStmtClass => return transCompoundStmt(rp, scope, @ptrCast(*const ZigClangCompoundStmt, stmt)), .DeclStmtClass => return transDeclStmt(rp, scope, @ptrCast(*const ZigClangDeclStmt, stmt)), + .DeclRefExprClass => return transDeclRefExpr(rp, scope, @ptrCast(*const ZigClangDeclRefExpr, stmt), lrvalue), .ImplicitCastExprClass => return transImplicitCastExpr(rp, scope, @ptrCast(*const ZigClangImplicitCastExpr, stmt)), else => { return revertAndWarn( @@ -451,6 +454,24 @@ fn transDeclStmt(rp: RestorePoint, parent_scope: *Scope, stmt: *const ZigClangDe }; } +fn transDeclRefExpr( + rp: RestorePoint, + scope: *Scope, + expr: *const ZigClangDeclRefExpr, + lrvalue: LRValue, +) !TransResult { + const value_decl = ZigClangDeclRefExpr_getDecl(expr); + const c_name = try rp.c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, value_decl))); + const zig_name = transLookupZigIdentifier(scope, c_name); + if (lrvalue == .l_value) try rp.c.ptr_params.put(zig_name); + const node = try appendIdentifier(rp.c, zig_name); + return TransResult{ + .node = node, + .node_scope = scope, + .child_scope = scope, + }; +} + fn transImplicitCastExpr( rp: RestorePoint, scope: *Scope, @@ -511,6 +532,16 @@ fn findBlockScope(inner: *Scope) *Scope.Block { } } +fn transLookupZigIdentifier(inner: *Scope, c_name: []const u8) []const u8 { + var scope = inner; + while (true) : (scope = scope.parent orelse return c_name) { + if (scope.id == .Var) { + const var_scope = @ptrCast(*const Scope.Var, scope); + if (std.mem.eql(u8, var_scope.c_name, c_name)) return var_scope.zig_name; + } + } +} + fn maybeSuppressResult( rp: RestorePoint, scope: *Scope, diff --git a/src/zig_clang.cpp b/src/zig_clang.cpp index 2edfda8efa..251c50ae73 100644 --- a/src/zig_clang.cpp +++ b/src/zig_clang.cpp @@ -1939,3 +1939,8 @@ struct ZigClangQualType ZigClangArrayType_getElementType(const struct ZigClangAr auto casted = reinterpret_cast(self); return bitcast(casted->getElementType()); } + +const struct ZigClangValueDecl *ZigClangDeclRefExpr_getDecl(const struct ZigClangDeclRefExpr *self) { + auto casted = reinterpret_cast(self); + return reinterpret_cast(casted->getDecl()); +} diff --git a/src/zig_clang.h b/src/zig_clang.h index 0afe30c474..086fd7a029 100644 --- a/src/zig_clang.h +++ b/src/zig_clang.h @@ -876,4 +876,6 @@ ZIG_EXTERN_C const struct ZigClangExpr *ZigClangImplicitCastExpr_getSubExpr(cons ZIG_EXTERN_C struct ZigClangQualType ZigClangArrayType_getElementType(const struct ZigClangArrayType *); +ZIG_EXTERN_C const struct ZigClangValueDecl *ZigClangDeclRefExpr_getDecl(const struct ZigClangDeclRefExpr *); + #endif