From 4ca4c0dd9ec4bbfe849b921524e90ccbbc2dccc9 Mon Sep 17 00:00:00 2001 From: mappu Date: Wed, 28 Aug 2024 18:22:05 +1200 Subject: [PATCH] genbindings: variable pointer depth --- cmd/genbindings/clang2il.go | 1 + cmd/genbindings/clang2il_test.go | 15 +++++++++++++++ cmd/genbindings/emitcabi.go | 7 +++++-- cmd/genbindings/intermediate.go | 6 +++++- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/cmd/genbindings/clang2il.go b/cmd/genbindings/clang2il.go index 1104cee7..29e34e7b 100644 --- a/cmd/genbindings/clang2il.go +++ b/cmd/genbindings/clang2il.go @@ -707,6 +707,7 @@ func parseSingleTypeString(p string) CppParameter { } else if tok == "*" { insert.Pointer = true + insert.PointerCount++ } else if len(tok) > 4 && strings.HasSuffix(tok, "List") { // Classes ending in --List are usually better represented as a QList diff --git a/cmd/genbindings/clang2il_test.go b/cmd/genbindings/clang2il_test.go index 435db89e..a6d77263 100644 --- a/cmd/genbindings/clang2il_test.go +++ b/cmd/genbindings/clang2il_test.go @@ -81,3 +81,18 @@ func TestParseInnerListTypes(t *testing.T) { t.Errorf("expected QWidget, got %q", tok.ParameterType) } } + +func TestPointerDepth(t *testing.T) { + for _, testCase := range []string{`char**`, `char * *`} { + l := parseSingleTypeString(testCase) + if l.ParameterType != "char" { + t.Error("expected char") + } + if !l.Pointer { + t.Error("expected pointer") + } + if l.PointerCount != 2 { + t.Errorf("expected pointerCount=2, got %d", l.PointerCount) + } + } +} diff --git a/cmd/genbindings/emitcabi.go b/cmd/genbindings/emitcabi.go index c4a6cae2..3a403b13 100644 --- a/cmd/genbindings/emitcabi.go +++ b/cmd/genbindings/emitcabi.go @@ -57,9 +57,12 @@ func (p CppParameter) RenderTypeCabi() string { } } - if p.Pointer || p.ByRef { + if p.ByRef { ret += "*" } + if p.Pointer { + ret += strings.Repeat("*", p.PointerCount) + } return ret // ignore const } @@ -91,7 +94,7 @@ func (p CppParameter) RenderTypeQtCpp() string { cppType = "const " + cppType } if p.Pointer { - cppType += "*" + cppType += strings.Repeat("*", p.PointerCount) } if p.ByRef { cppType += "&" diff --git a/cmd/genbindings/intermediate.go b/cmd/genbindings/intermediate.go index 5a179fcb..1f72b92c 100644 --- a/cmd/genbindings/intermediate.go +++ b/cmd/genbindings/intermediate.go @@ -32,6 +32,7 @@ type CppParameter struct { TypeAlias string // If we rewrote QStringList->QList, this field contains the original QStringList Const bool Pointer bool + PointerCount int ByRef bool Optional bool } @@ -52,6 +53,7 @@ func (p *CppParameter) CopyWithAlias(alias CppParameter) CppParameter { // WARNING: This can't work for double indirection ret.Const = ret.Const || alias.Const ret.Pointer = ret.Pointer || alias.Pointer + ret.PointerCount += alias.PointerCount ret.ByRef = ret.ByRef || alias.ByRef return ret } @@ -168,7 +170,9 @@ func IsArgcArgv(params []CppParameter, pos int) bool { params[pos].ParameterType == "int" && params[pos].ByRef && params[pos+1].ParameterName == "argv" && - params[pos+1].ParameterType == "char **") + params[pos+1].ParameterType == "char") && + params[pos+1].Pointer && + params[pos+1].PointerCount == 2 } func IsReceiverMethod(params []CppParameter, pos int) bool {