| ... | @@ -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, |
| 111 | | 112 | |
| 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 | } |
| 453 | | 456 | |
| | 457 | fn 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 | |
| 454 | fn transImplicitCastExpr( | 475 | fn 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 | } |
| 513 | | 534 | |
| | 535 | fn 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 | |
| 514 | fn maybeSuppressResult( | 545 | fn maybeSuppressResult( |
| 515 | rp: RestorePoint, | 546 | rp: RestorePoint, |
| 516 | scope: *Scope, | 547 | scope: *Scope, |