genbindings: track optional parameters in our IL, expand in AST pass

This commit is contained in:
mappu 2024-08-08 18:55:22 +12:00
parent a991feb589
commit 85dd87e6d0
4 changed files with 62 additions and 2 deletions

View File

@ -154,9 +154,15 @@ nextMethod:
// Update the name for the existing nth parameter
mm.Parameters[paramCounter].ParameterName = parmName
paramCounter++
// TODO If this parameter is optional, expand it into multiple function overloads
// If this parameter has any internal AST nodes of its
// own, assume it means it's an optional parameter
if _, ok := methodObj["inner"]; ok {
mm.Parameters[paramCounter].Optional = true
}
// Next
paramCounter++
default:
// Something else inside a declaration??

View File

@ -10,6 +10,7 @@ type CppParameter struct {
Const bool
Pointer bool
ByRef bool
Optional bool
}
type CppProperty struct {

View File

@ -54,6 +54,9 @@ func main() {
os.Exit(0)
}
// AST transforms on our IL
astTransformOptional(parsed)
// Emit 3 code files from the intermediate format
outputName := filepath.Join(*outDir, "gen_"+strings.TrimSuffix(filepath.Base(*inputHeader), `.h`))

View File

@ -0,0 +1,50 @@
package main
import (
"fmt"
)
// astTransformOptional expands all methods with optional parameters into
// explicit additional versions.
func astTransformOptional(parsed *CppParsedHeader) {
for i, c := range parsed.Classes {
anyChange := false
for j, m := range c.Methods {
// Search for first optional parameter (they all must be last)
optionalStart := -1
for k, p := range m.Parameters {
if p.Optional {
optionalStart = k
break
}
}
if optionalStart == -1 {
continue // There were no optional parameters
}
anyChange = true
// Add method copies
for x := optionalStart; x < len(m.Parameters); x++ {
dupMethod := CppMethod{
MethodName: m.MethodName + fmt.Sprintf("%d", x+1),
ReturnType: m.ReturnType,
Parameters: nil,
}
dupMethod.Parameters = append(dupMethod.Parameters, m.Parameters[0:x+1]...)
c.Methods = append(c.Methods, dupMethod) // TODO can we insert them next, instead of at the end?
}
// Truncate the original method's parameters to only the
// mandatory ones
m.Parameters = m.Parameters[0:optionalStart]
c.Methods[j] = m
}
if anyChange {
parsed.Classes[i] = c
}
}
}