authorgravatar for codroid@gmail.comhryx <codroid@gmail.com> 2019-05-27 19:20:23-07:00
committergravatar for codroid@gmail.comhryx <codroid@gmail.com> 2019-05-27 19:20:23-07:00
logb558d0996a8a18475f91483d3e3962502422a315
treea87f2ea79951e5d0d6777edb20404321968a7074
parente07888e54c251b6c493ea911cf65aa46eccba67d
signature Commit is signed but in an unrecognized format.

expr: DeclRefExpr


4 files changed, 40 insertions(+), 0 deletions(-)

src-self-hosted/clang.zig+2
...@@ -884,3 +884,5 @@ pub extern fn ZigClangImplicitCastExpr_getCastKind(*const ZigClangImplicitCastEx...@@ -884,3 +884,5 @@ pub extern fn ZigClangImplicitCastExpr_getCastKind(*const ZigClangImplicitCastEx
884pub extern fn ZigClangImplicitCastExpr_getSubExpr(*const ZigClangImplicitCastExpr) *const ZigClangExpr;884pub extern fn ZigClangImplicitCastExpr_getSubExpr(*const ZigClangImplicitCastExpr) *const ZigClangExpr;
885885
886pub extern fn ZigClangArrayType_getElementType(*const ZigClangArrayType) ZigClangQualType;886pub extern fn ZigClangArrayType_getElementType(*const ZigClangArrayType) ZigClangQualType;
887
888pub extern fn ZigClangDeclRefExpr_getDecl(*const ZigClangDeclRefExpr) *const ZigClangValueDecl;
src-self-hosted/translate_c.zig+31
...@@ -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 ptr_params: std.BufSet,
110 clang_context: *ZigClangASTContext,111 clang_context: *ZigClangASTContext,
111112
112 fn a(c: *Context) *std.mem.Allocator {113 fn a(c: *Context) *std.mem.Allocator {
...@@ -184,6 +185,7 @@ pub fn translate(...@@ -184,6 +185,7 @@ pub fn translate(
184 .decl_table = DeclTable.init(arena),185 .decl_table = DeclTable.init(arena),
185 .global_scope = try arena.create(Scope.Root),186 .global_scope = try arena.create(Scope.Root),
186 .mode = mode,187 .mode = mode,
188 .ptr_params = std.BufSet.init(arena),
187 .clang_context = ZigClangASTUnit_getASTContext(ast_unit).?,189 .clang_context = ZigClangASTUnit_getASTContext(ast_unit).?,
188 };190 };
189 context.global_scope.* = Scope.Root{191 context.global_scope.* = Scope.Root{
...@@ -326,6 +328,7 @@ fn transStmt(...@@ -326,6 +328,7 @@ fn transStmt(
326 switch (sc) {328 switch (sc) {
327 .CompoundStmtClass => return transCompoundStmt(rp, scope, @ptrCast(*const ZigClangCompoundStmt, stmt)),329 .CompoundStmtClass => return transCompoundStmt(rp, scope, @ptrCast(*const ZigClangCompoundStmt, stmt)),
328 .DeclStmtClass => return transDeclStmt(rp, scope, @ptrCast(*const ZigClangDeclStmt, stmt)),330 .DeclStmtClass => return transDeclStmt(rp, scope, @ptrCast(*const ZigClangDeclStmt, stmt)),
331 .DeclRefExprClass => return transDeclRefExpr(rp, scope, @ptrCast(*const ZigClangDeclRefExpr, stmt), lrvalue),
329 .ImplicitCastExprClass => return transImplicitCastExpr(rp, scope, @ptrCast(*const ZigClangImplicitCastExpr, stmt)),332 .ImplicitCastExprClass => return transImplicitCastExpr(rp, scope, @ptrCast(*const ZigClangImplicitCastExpr, stmt)),
330 else => {333 else => {
331 return revertAndWarn(334 return revertAndWarn(
...@@ -451,6 +454,24 @@ fn transDeclStmt(rp: RestorePoint, parent_scope: *Scope, stmt: *const ZigClangDe...@@ -451,6 +454,24 @@ fn transDeclStmt(rp: RestorePoint, parent_scope: *Scope, stmt: *const ZigClangDe
451 };454 };
452}455}
453456
457fn transDeclRefExpr(
458 rp: RestorePoint,
459 scope: *Scope,
460 expr: *const ZigClangDeclRefExpr,
461 lrvalue: LRValue,
462) !TransResult {
463 const value_decl = ZigClangDeclRefExpr_getDecl(expr);
464 const c_name = try rp.c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, value_decl)));
465 const zig_name = transLookupZigIdentifier(scope, c_name);
466 if (lrvalue == .l_value) try rp.c.ptr_params.put(zig_name);
467 const node = try appendIdentifier(rp.c, zig_name);
468 return TransResult{
469 .node = node,
470 .node_scope = scope,
471 .child_scope = scope,
472 };
473}
474
454fn transImplicitCastExpr(475fn transImplicitCastExpr(
455 rp: RestorePoint,476 rp: RestorePoint,
456 scope: *Scope,477 scope: *Scope,
...@@ -511,6 +532,16 @@ fn findBlockScope(inner: *Scope) *Scope.Block {...@@ -511,6 +532,16 @@ fn findBlockScope(inner: *Scope) *Scope.Block {
511 }532 }
512}533}
513534
535fn transLookupZigIdentifier(inner: *Scope, c_name: []const u8) []const u8 {
536 var scope = inner;
537 while (true) : (scope = scope.parent orelse return c_name) {
538 if (scope.id == .Var) {
539 const var_scope = @ptrCast(*const Scope.Var, scope);
540 if (std.mem.eql(u8, var_scope.c_name, c_name)) return var_scope.zig_name;
541 }
542 }
543}
544
514fn maybeSuppressResult(545fn maybeSuppressResult(
515 rp: RestorePoint,546 rp: RestorePoint,
516 scope: *Scope,547 scope: *Scope,
src/zig_clang.cpp+5
...@@ -1939,3 +1939,8 @@ struct ZigClangQualType ZigClangArrayType_getElementType(const struct ZigClangAr...@@ -1939,3 +1939,8 @@ struct ZigClangQualType ZigClangArrayType_getElementType(const struct ZigClangAr
1939 auto casted = reinterpret_cast<const clang::ArrayType *>(self);1939 auto casted = reinterpret_cast<const clang::ArrayType *>(self);
1940 return bitcast(casted->getElementType());1940 return bitcast(casted->getElementType());
1941}1941}
1942
1943const struct ZigClangValueDecl *ZigClangDeclRefExpr_getDecl(const struct ZigClangDeclRefExpr *self) {
1944 auto casted = reinterpret_cast<const clang::DeclRefExpr *>(self);
1945 return reinterpret_cast<const struct ZigClangValueDecl *>(casted->getDecl());
1946}
src/zig_clang.h+2
...@@ -876,4 +876,6 @@ ZIG_EXTERN_C const struct ZigClangExpr *ZigClangImplicitCastExpr_getSubExpr(cons...@@ -876,4 +876,6 @@ ZIG_EXTERN_C const struct ZigClangExpr *ZigClangImplicitCastExpr_getSubExpr(cons
876876
877ZIG_EXTERN_C struct ZigClangQualType ZigClangArrayType_getElementType(const struct ZigClangArrayType *);877ZIG_EXTERN_C struct ZigClangQualType ZigClangArrayType_getElementType(const struct ZigClangArrayType *);
878878
879ZIG_EXTERN_C const struct ZigClangValueDecl *ZigClangDeclRefExpr_getDecl(const struct ZigClangDeclRefExpr *);
880
879#endif881#endif