Clean up variadic functions and methods.

This commit is contained in:
Greg 2019-05-28 20:04:39 -04:00
parent 1cc0f0e26a
commit 840a055a11
4 changed files with 58 additions and 15 deletions

View File

@ -78,18 +78,26 @@ func main() {
})
nst := ns.NSStringWithGoString
fmt.Printf("\nGetObjectsAndKeysCount()\n")
fmt.Printf("\nNSDictionaryWithObjectsForKeys()\n")
d := ns.NSDictionaryWithObjectsForKeys(
ns.NSArrayWithObjects(nst("obj1"),nst("obj2")),
ns.NSArrayWithObjects(nst("key1"),nst("key2")),
)
os := make([]ns.Id,4)
fmt.Printf("Length of os is %d\n",len(os))
ks := make([]ns.Id,4)
fmt.Printf("\nGetObjects()\n")
d.GetObjects(&os,&ks,4)
for _,o := range os {
fmt.Printf("--%s\n",o.NSString())
}
for _,o := range ks {
fmt.Printf("--%s\n",o.NSString())
fmt.Printf("Length of os is now %d\n",len(os))
for i,k := range ks {
fmt.Printf("--%s -> %s\n",k.NSString(),os[i].NSString())
}
fmt.Printf("\nNSStringWithContentsOfURLEncoding()\n")
err := make([]ns.NSError,1)
n1 = ns.NSStringWithContentsOfURLEncoding(ns.NSURLWithGoString("htttypo://example.com"),0,&err)
fmt.Printf("err: %s\n",err[0].LocalizedDescription())
fmt.Printf("\nNSStringWithFormat()\n")
str := ns.NSStringWithFormat(nst("(%@) (%@)\n(%@)\n"),n2,n3,s1)
fmt.Printf("%s\n",str)
}

View File

@ -13,6 +13,8 @@ classes:
- NSLocale
- NSCharacterSet
- NSString
- NSURL
- NSError
- NSScanner
- NSFileManager
- NSObject

View File

@ -305,6 +305,16 @@ func (t *Type) IsFunction() bool {
return t.Node.IsFunction()
}
func (t *Type) IsValist() bool {
if t == nil {
return false
}
if t.GoType() == "__va_list_tag" { // OS dependent
return true
}
return false
}
func (t *Type) ReturnType() *Type {
if rt := t.Node.ReturnType(); rt != nil {
return NewType(rt,t.Class)

View File

@ -161,6 +161,12 @@ func (m *Method) LongName() string {
return ret
}
func (m *Method) HasUnsupportedType() bool {
return m.Type.IsFunction() ||
m.Type.IsFunctionPtr() ||
m.hasUnsupportedParam()
}
type Enum struct {
Name string
Type *types.Type
@ -190,7 +196,7 @@ func (a ByParams) Len() int { return len(a) }
func (a ByParams) Swap(i,j int) { a[i], a[j] = a[j], a[i] }
func (a ByParams) Less(i, j int) bool { return len(a[i].Parameters) < len(a[j].Parameters) }
//Disambiguate polyorphic method names
//Disambiguate overloaded method names
func Disambiguate(mc *MethodCollection) {
lst := map[string][]*Method{}
for _,m := range mc.Methods {
@ -229,12 +235,15 @@ func (m Method) isVoid() bool {
return m.Type.CType() == "void"
}
//hasFunctionParam() returns true if a method has a function as a parameter.
func (m Method) hasFunctionParam() bool {
//hasUnsupportedParam() returns true if a method has a function as a parameter.
func (m Method) hasUnsupportedParam() bool {
for _,p := range m.Parameters {
if p.Type.IsFunction() || p.Type.IsFunctionPtr() {
return true
}
if pt := p.Type.PointsTo(); pt.IsValist() {
return true
}
}
return false
}
@ -273,7 +282,7 @@ func (w Wrapper) objcparamlist(m *Method) string {
first = false
} else {
if p.Type.Variadic {
str := []string{p.Pname + ":arr[0]"}
str := []string{p.Pname + ", arr[0]"}
for i := 1; i < w.Vaargs; i++ {
str = append(str,"arr["+strconv.Itoa(i)+"]")
}
@ -391,12 +400,20 @@ func (w *Wrapper) AddFunction(n *ast.FunctionDecl) {
m.Parameters = append(m.Parameters,p)
i++
//fmt.Printf(" %s\n",p.Type.CType())
case *ast.Variadic:
p := &Parameter{
Vname: "object",
Type: types.NewTypeFromString("NSObject*",""),
}
p.Type.Variadic = true
m.Parameters = append(m.Parameters,p)
i++
}
}
if i > 0 && len(f.Children) > i {
if e := f.Children[i]; len(e.Children) > 0 {
//fmt.Println(" Next parameter: ",e.Children[0].String())
m.Parameters[i-1].Type.Variadic = true
//m.Parameters[i-1].Type.Variadic = true
}
}
w.Functions[n.Name] = m
@ -670,7 +687,14 @@ func (w *Wrapper) GetParms(n ast.Node,class string) ([]*Parameter,bool) {
ret = append(ret,p)
j++
case *ast.Variadic:
ret[j-1].Type.Variadic = true
//ret[j-1].Type.Variadic = true
p := &Parameter{
Vname: "object",
Type: types.NewTypeFromString("NSObject*",""),
}
p.Type.Variadic = true
ret = append(ret,p)
j++
case *ast.AvailabilityAttr, *ast.UnavailableAttr, *ast.DeprecatedAttr:
avail.Add(x)
case *ast.Unknown:
@ -847,7 +871,7 @@ func (w *Wrapper) _processMethod(m *Method,fun bool) {
if Debug {
fmt.Printf(" method: %s (%s)\n", m.Name, m.Type)
}
if m.Type.IsFunction() || m.Type.IsFunctionPtr() || m.hasFunctionParam() {
if m.HasUnsupportedType() {
return
}
w.processType(m.Type)
@ -1098,7 +1122,6 @@ func (w *Wrapper) MethodFromSig(sig,class string) *Method {
}
func (w *Wrapper) ProcessSubclass(sname string, sc *Subclass) {
fmt.Printf("Wrapping %s\n",sname)
gname := strings.Title(sname)
types.Wrap(gname)
types.SetSuper(gname,sc.Super)
@ -1187,7 +1210,7 @@ func (w *Wrapper) _ProcessDelSub(dname string, ps map[string][]string, nms []*Me
if !matches(string(m.Name[0])+m.GoName[1:],pats) {
continue
}
if m.Type.IsFunction() || m.Type.IsFunctionPtr() || m.hasFunctionParam() {
if m.HasUnsupportedType() {
continue
}
methods = append(methods,m)