Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support direct array access; fixes #44 #45

Merged
merged 2 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ loop:
switch s := t.(type) {
case types.PtrType:
t = s.Elem()
case types.ArrayType:
t = s.Elem()
case types.Named:
t = s.Underlying()
default:
Expand Down Expand Up @@ -1009,7 +1011,21 @@ func (g *translator) convertPostfixExpr(d *cc.PostfixExpression) Expr {
args = append(args, g.convertAssignExpr(it.AssignmentExpression))
}
return g.NewCCallExpr(g.ToFunc(fnc, ToFuncExpr(fnc.CType(nil))), args)
case cc.PostfixExpressionSelect, cc.PostfixExpressionPSelect: // x.y, x->y
case cc.PostfixExpressionPSelect: // x->y
exp := g.convertPostfixExpr(d.PostfixExpression)
if _, ok := exp.CType(nil).(types.ArrayType); ok { // pointer accesses might be an array
return NewCSelectExpr(
g.NewCIndexExpr(
exp,
cUintLit(0), // index the first element
g.convertTypeOper(d.Operand, d.Position()),
), g.convertIdentOn(exp.CType(nil), d.Token2),
)
}
return NewCSelectExpr(
exp, g.convertIdentOn(exp.CType(nil), d.Token2),
)
case cc.PostfixExpressionSelect: // x.y
exp := g.convertPostfixExpr(d.PostfixExpression)
return NewCSelectExpr(
exp, g.convertIdentOn(exp.CType(nil), d.Token2),
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,9 @@ lukechampine.com/uint128 v1.1.1 h1:pnxCASz787iMf+02ssImqk6OLt+Z5QHMoZyUXR4z6JU=
lukechampine.com/uint128 v1.1.1/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
maze.io/x/math32 v0.0.0-20181106113604-c78ed91899f1 h1:SgIJGhlYkU+wmyP0xBEOdLVRIwVR4HSp/9kfv1BSvKQ=
maze.io/x/math32 v0.0.0-20181106113604-c78ed91899f1/go.mod h1:OL5aVu+KUQ0mSadkiIseNiKIX6dJ2Ul1RlNWSMCP5y8=
modernc.org/cc/v3 v3.34.1-0.20210914102530-cf6502699529 h1:XFtypetV2ZM14Ry+cyARJLkdddiW3SGjvpBluODDSxc=
modernc.org/cc/v3 v3.34.1-0.20210914102530-cf6502699529/go.mod h1:iPJg1pkwXqAV16SNgFBVYmggfMg6xhs+2oiO0vclK3g=
modernc.org/cc/v3 v3.35.17 h1:sWWFJxgj2whIJ5P/rzgHalMgpcIhkVSRgiLV0XA7p6Y=
modernc.org/cc/v3 v3.35.17/go.mod h1:iPJg1pkwXqAV16SNgFBVYmggfMg6xhs+2oiO0vclK3g=
modernc.org/mathutil v1.2.2/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/mathutil v1.4.0 h1:GCjoRaBew8ECCKINQA2nYjzvufFW9YiEuuB+rQ9bn2E=
modernc.org/mathutil v1.4.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/mathutil v1.4.1 h1:ij3fYGe8zBF4Vu+g0oT7mB06r8sqGWKuJu1yXeR4by8=
modernc.org/mathutil v1.4.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
Expand Down
29 changes: 29 additions & 0 deletions pointers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,35 @@ func bar(arg *A) {
func foo(arg *B) {
bar(&arg.A)
}
`,
},
{
name: "implicit array access",
src: `
struct s {
int i;
};

struct s ss[] = {
{0},
};


void foo() {
int i = ss->i;
}
`,
exp: `
type s struct {
I int32
}

var ss [1]s = [1]s{}

func foo() {
var i int32 = ss[0].I
_ = i
}
`,
},
{
Expand Down