2024-08-08 06:55:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-11-17 03:10:24 +00:00
|
|
|
"strconv"
|
2024-08-08 06:55:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// astTransformOptional expands all methods with optional parameters into
|
|
|
|
// explicit additional versions.
|
|
|
|
func astTransformOptional(parsed *CppParsedHeader) {
|
|
|
|
for i, c := range parsed.Classes {
|
|
|
|
|
|
|
|
anyChange := false
|
2024-08-25 00:49:51 +00:00
|
|
|
|
|
|
|
// Methods
|
|
|
|
// ```````
|
|
|
|
|
2024-08-08 06:55:22 +00:00
|
|
|
for j, m := range c.Methods {
|
|
|
|
|
2024-11-15 02:37:51 +00:00
|
|
|
// Treat virtual methods as if all parameters are mandatory
|
|
|
|
if m.IsVirtual {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-08-08 06:55:22 +00:00
|
|
|
// 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++ {
|
2024-11-04 09:51:52 +00:00
|
|
|
dupMethod := m // shallow copy
|
2024-11-17 03:10:24 +00:00
|
|
|
dupMethod.Rename(m.MethodName + strconv.Itoa(x+1))
|
2024-09-07 03:25:25 +00:00
|
|
|
dupMethod.Parameters = m.Parameters[0 : x+1]
|
|
|
|
dupMethod.HiddenParams = m.Parameters[x+1:]
|
|
|
|
|
2024-08-08 06:55:22 +00:00
|
|
|
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
|
2024-09-07 03:25:25 +00:00
|
|
|
m.HiddenParams = m.Parameters[optionalStart:]
|
2024-08-08 06:55:22 +00:00
|
|
|
m.Parameters = m.Parameters[0:optionalStart]
|
|
|
|
c.Methods[j] = m
|
|
|
|
}
|
|
|
|
|
2024-08-25 00:49:51 +00:00
|
|
|
// Constructors
|
|
|
|
// ````````````
|
|
|
|
|
|
|
|
for j, m := range c.Ctors {
|
|
|
|
|
|
|
|
// 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 ctor copies
|
|
|
|
for x := optionalStart; x < len(m.Parameters); x++ {
|
2024-11-04 09:51:52 +00:00
|
|
|
dupCtor := m // shallow copy
|
2024-09-07 03:25:25 +00:00
|
|
|
dupCtor.Parameters = m.Parameters[0 : x+1]
|
|
|
|
dupCtor.HiddenParams = m.Parameters[x+1:]
|
2024-08-25 00:49:51 +00:00
|
|
|
c.Ctors = append(c.Ctors, dupCtor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate the original ctor's parameters to only the
|
|
|
|
// mandatory ones
|
2024-09-07 03:25:25 +00:00
|
|
|
m.HiddenParams = m.Parameters[optionalStart:]
|
2024-08-25 00:49:51 +00:00
|
|
|
m.Parameters = m.Parameters[0:optionalStart]
|
|
|
|
c.Ctors[j] = m
|
|
|
|
}
|
|
|
|
|
2024-08-08 06:55:22 +00:00
|
|
|
if anyChange {
|
|
|
|
parsed.Classes[i] = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|