Skip to content

Commit

Permalink
clang2il.go: Check for desugared types (support clang-19+)
Browse files Browse the repository at this point in the history
* Closes mappu#116
  • Loading branch information
rcalixte committed Jan 20, 2025
1 parent a80a939 commit 24cf11d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 22 deletions.
59 changes: 52 additions & 7 deletions cmd/genbindings/clang2il.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,48 @@ nextTopLevel:
return &ret, nil // done
}

// shouldPreferQualType returns true if we should use the qualType instead of
// the desugared type based on certain type patterns
func shouldPreferQualType(qualType string) bool {
if strings.Contains(qualType, "intptr") || strings.Contains(qualType, "_t") ||
strings.HasPrefix(qualType, "uint") || strings.Contains(qualType, "ushort") ||
strings.Contains(qualType, "quint") || strings.Contains(qualType, "qint") ||
strings.Contains(qualType, "qptrdiff") || strings.HasPrefix(qualType, "qsize") ||
strings.HasPrefix(qualType, "QList<") || strings.HasPrefix(qualType, "QPair<") ||
strings.HasPrefix(qualType, "QIntegerForSizeof<") {
return true
}
return false
}

// getPreferredType returns either the desugared type or qual type based on our rules
func getPreferredType(node interface{}) string {
if node == nil {
return ""
}

nodeMap, ok := node.(map[string]interface{})
if !ok {
return ""
}

var desugared, qualType string
if d, ok := nodeMap["desugaredQualType"].(string); ok {
desugared = d
}
if q, ok := nodeMap["qualType"].(string); ok {
qualType = q
}

if qualType != "" && shouldPreferQualType(qualType) {
return qualType
}
if desugared != "" {
return desugared
}
return qualType
}

// processTypedef parses a single C++ typedef into our intermediate format.
func processTypedef(node map[string]interface{}, addNamePrefix string) (CppTypedef, error) {
// Must have a name
Expand All @@ -174,7 +216,8 @@ func processTypedef(node map[string]interface{}, addNamePrefix string) (CppTyped
}

if typ, ok := node["type"].(map[string]interface{}); ok {
if qualType, ok := typ["qualType"].(string); ok {
qualType := getPreferredType(typ)
if qualType != "" {
return CppTypedef{
Alias: addNamePrefix + nodename,
UnderlyingType: parseSingleTypeString(qualType),
Expand Down Expand Up @@ -264,7 +307,8 @@ func processClassType(node map[string]interface{}, addNamePrefix string) (CppCla
}

if typ, ok := base["type"].(map[string]interface{}); ok {
if qualType, ok := typ["qualType"].(string); ok {
qualType := getPreferredType(typ)
if qualType != "" {
ret.DirectInherits = append(ret.DirectInherits, qualType)
}
}
Expand Down Expand Up @@ -512,8 +556,9 @@ func processEnum(node map[string]interface{}, addNamePrefix string) (CppEnum, er
// Underlying type
ret.UnderlyingType = parseSingleTypeString("int")
if nodefut, ok := node["fixedUnderlyingType"].(map[string]interface{}); ok {
if nodequal, ok := nodefut["qualType"].(string); ok {
ret.UnderlyingType = parseSingleTypeString(nodequal)
qualType := getPreferredType(nodefut)
if qualType != "" {
ret.UnderlyingType = parseSingleTypeString(qualType)
}
}

Expand Down Expand Up @@ -652,11 +697,11 @@ nextEnumEntry:
func parseMethod(node map[string]interface{}, mm *CppMethod) error {

if typobj, ok := node["type"].(map[string]interface{}); ok {
if qualType, ok := typobj["qualType"].(string); ok {
qualType := getPreferredType(typobj)
if qualType != "" {
// The qualType is the whole type of the method, including its parameter types
// If anything here is too complicated, skip the whole method

var err error = nil
var err error
mm.ReturnType, mm.Parameters, mm.IsConst, err = parseTypeString(qualType)
if err != nil {
return err
Expand Down
14 changes: 7 additions & 7 deletions cmd/genbindings/clang2il_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ func TestParseMethodTypes(t *testing.T) {
}

cases := []testCase{
testCase{
{
input: "void (bool)",
expectReturn: CppParameter{ParameterType: "void"},
expectParams: []CppParameter{
CppParameter{ParameterType: "bool"},
{ParameterType: "bool"},
},
},
testCase{
{
input: "bool (QList<QPair<Foo, Bar>>, QString)",
expectReturn: CppParameter{ParameterType: "bool"},
expectParams: []CppParameter{
CppParameter{ParameterType: "QList<QPair<Foo, Bar>>"},
CppParameter{ParameterType: "QString"},
{ParameterType: "QList<QPair<Foo, Bar>>"},
{ParameterType: "QString"},
},
// expectErr: true,
},
testCase{
{
input: "bool (QList<QWidget*>)",
expectReturn: CppParameter{ParameterType: "bool"},
expectParams: []CppParameter{
CppParameter{ParameterType: "QList<QWidget*>"},
{ParameterType: "QList<QWidget*>"},
},
},
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/genbindings/transformchildclasses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ func TestChildClassesExtract(t *testing.T) {
src := CppParsedHeader{
Classes: []CppClass{

CppClass{
{
ClassName: "Parent",

ChildClassdefs: []CppClass{

CppClass{
{
ClassName: "Parent::Child",

ChildClassdefs: []CppClass{

CppClass{
{
ClassName: "Parent::Child::Grandchild",
},
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/genbindings/transformtypedefs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ func TestTransformTypedefs(t *testing.T) {
return CppParsedHeader{
Classes: []CppClass{

CppClass{
{
ClassName: "QTestClass",
Ctors: []CppMethod{

CppMethod{
{
Parameters: []CppParameter{
parseSingleTypeString(typeName),
},
Expand Down
6 changes: 3 additions & 3 deletions cmd/genbindings/util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"strconv"
"encoding/json"
"log"
"strconv"
"strings"
)

Expand All @@ -12,7 +12,7 @@ func maybeSuffix(counter int) string {
return ""
}

return strconv.Itoa(counter+1)
return strconv.Itoa(counter + 1)
}

func titleCase(s string) string {
Expand Down Expand Up @@ -53,4 +53,4 @@ func slice_copy[T comparable](input []T) []T {
ret[i] = elem
}
return ret
}
}

0 comments on commit 24cf11d

Please sign in to comment.