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 #116
  • Loading branch information
rcalixte committed Jan 11, 2025
1 parent 3a63a12 commit 7c32809
Show file tree
Hide file tree
Showing 369 changed files with 4,808 additions and 4,785 deletions.
50 changes: 40 additions & 10 deletions cmd/genbindings/clang2il.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,15 @@ 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 {
// Try desugaredQualType first
var qualType string
if desugared, ok := typ["desugaredQualType"].(string); ok {
qualType = desugared
} else if qt, ok := typ["qualType"].(string); ok {
qualType = qt
}

if qualType != "" {
return CppTypedef{
Alias: addNamePrefix + nodename,
UnderlyingType: parseSingleTypeString(qualType),
Expand Down Expand Up @@ -264,7 +272,13 @@ 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 {
var qualType string
if desugared, ok := typ["desugaredQualType"].(string); ok {
qualType = desugared
} else if qt, ok := typ["qualType"].(string); ok {
qualType = qt
}
if qualType != "" {
ret.DirectInherits = append(ret.DirectInherits, qualType)
}
}
Expand Down Expand Up @@ -512,8 +526,14 @@ 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)
var qualType string
if desugared, ok := nodefut["desugaredQualType"].(string); ok {
qualType = desugared
} else if qt, ok := nodefut["qualType"].(string); ok {
qualType = qt
}
if qualType != "" {
ret.UnderlyingType = parseSingleTypeString(qualType)
}
}

Expand Down Expand Up @@ -650,18 +670,20 @@ nextEnumEntry:

// parseMethod parses a Clang method into our CppMethod intermediate format.
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 {
// The qualType is the whole type of the method, including its parameter types
// If anything here is too complicated, skip the whole method
var qualType string
if desugared, ok := typobj["desugaredQualType"].(string); ok {
qualType = desugared
} else if qt, ok := typobj["qualType"].(string); ok {
qualType = qt
}

var err error = nil
if qualType != "" {
var err error
mm.ReturnType, mm.Parameters, mm.IsConst, err = parseTypeString(qualType)
if err != nil {
return err
}

}
}

Expand Down Expand Up @@ -691,6 +713,14 @@ func parseMethod(node map[string]interface{}, mm *CppMethod) error {
parmName, _ := methodObj["name"].(string) // n.b. may be unnamed
if parmName == "" {

// Get the precise parameter type if available
if typ, ok := methodObj["type"].(map[string]interface{}); ok {
if desugared, ok := typ["desugaredQualType"].(string); ok {
// Update the parameter type with the more precise desugared version
mm.Parameters[paramCounter].ParameterType = desugared
}
}

// Generate a default parameter name
// Super nice autogen names if this is a Q_PROPERTY setter:
if len(mm.Parameters) == 1 && strings.HasPrefix(mm.MethodName, "set") {
Expand Down
4 changes: 2 additions & 2 deletions cmd/genbindings/config-allowlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func AllowClass(className string) bool {
return false
}

if strings.HasPrefix(className, `std::`) {
if strings.HasPrefix(className, `std::`) && !strings.HasPrefix(className, `std::pair`) {
return false // Scintilla bindings find some of these
}

Expand Down Expand Up @@ -400,7 +400,7 @@ func AllowType(p CppParameter, isReturnType bool) error {
return ErrTooComplex // Function pointer types in QtWebEngine
}

if strings.HasPrefix(p.ParameterType, "std::") {
if strings.HasPrefix(p.ParameterType, "std::") && !strings.HasPrefix(p.ParameterType, `std::pair<`) {
// std::initializer e.g. qcborarray.h
// std::string QByteArray->toStdString(). There are QString overloads already
// std::nullptr_t Qcborstreamwriter
Expand Down
9 changes: 7 additions & 2 deletions cmd/genbindings/intermediate.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,13 @@ func (p CppParameter) QMapOf() (CppParameter, CppParameter, bool) {
}

func (p CppParameter) QPairOf() (CppParameter, CppParameter, bool) {
if strings.HasPrefix(p.ParameterType, `QPair<`) && strings.HasSuffix(p.ParameterType, `>`) {
interior := tokenizeMultipleParameters(p.ParameterType[6 : len(p.ParameterType)-1])
if (strings.HasPrefix(p.ParameterType, `QPair<`) || strings.HasPrefix(p.ParameterType, `std::pair<`)) &&
strings.HasSuffix(p.ParameterType, `>`) {
index := 6 // QPair<
if strings.HasPrefix(p.ParameterType, `std::pair<`) {
index = 10 // std::pair<
}
interior := tokenizeMultipleParameters(p.ParameterType[index : len(p.ParameterType)-1])
if len(interior) != 2 {
panic("QPair<> has unexpected number of template arguments")
}
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
}
}
Loading

0 comments on commit 7c32809

Please sign in to comment.