Update to Big Sur 15.5.2 / Clang 12.0.5. Improved handing of avaiability

attributes and boolean values.
This commit is contained in:
gmp
2021-11-16 16:02:08 -05:00
parent f4a9dae250
commit e4f7e32887
21 changed files with 482 additions and 9157 deletions
+217 -31
View File
@@ -20,6 +20,7 @@ var (
Arc = false
Autorelease = false
Gogc = false
dbclass = ""
)
type Wrapper struct {
@@ -237,6 +238,7 @@ type Protocol struct {
type MethodCollection struct {
Class, GoClass string
Methods []*Method
Unavailable map[string]bool
}
func NewMethodCollection(class string) *MethodCollection {
@@ -244,6 +246,7 @@ func NewMethodCollection(class string) *MethodCollection {
Class: class,
GoClass: strings.Title(class),
Methods: []*Method{},
Unavailable: make(map[string]bool),
}
return ret
}
@@ -256,6 +259,11 @@ func (a ByParams) Less(i, j int) bool { return len(a[i].Parameters) < len(a[j].P
//Disambiguate overloaded method names
func Disambiguate(mc *MethodCollection) {
prt := false
if mc.Class == dbclass {
prt = true
fmt.Printf("Disambiguate(%s)\n", mc.Class)
}
lst := map[string][]*Method{}
for _, m := range mc.Methods {
lst2 := lst[m.Name]
@@ -285,6 +293,9 @@ func Disambiguate(mc *MethodCollection) {
}
used[pname] = true
m.GoName = strings.Title(pname)
if prt {
fmt.Printf(" -> %s(%s)\n", m.Name, pname)
}
mc.Methods = append(mc.Methods, m)
}
}
@@ -471,6 +482,7 @@ func (w *Wrapper) AddCategory(n *ast.ObjCCategoryDecl) {
}
func (w *Wrapper) AddFunction(n *ast.FunctionDecl) {
prt := false
//treat functions as class methods with no class
tp := types.NewTypeFromString(n.Type, "")
m := &Method{
@@ -517,7 +529,7 @@ func (w *Wrapper) AddFunction(n *ast.FunctionDecl) {
m.Parameters = append(m.Parameters, p)
i++
case *ast.AvailabilityAttr, *ast.UnavailableAttr, *ast.DeprecatedAttr:
a.Add(x)
a.Add(x, prt)
}
}
if a.Available() {
@@ -561,6 +573,16 @@ func (w *Wrapper) AddProtocol(n *ast.ObjCProtocolDecl) {
}
func (w *Wrapper) AddMethod(p *MethodCollection, x *ast.ObjCMethodDecl) {
prt := false
if p.Class == dbclass {
prt = true
fmt.Printf("%s -> adding %s\n", dbclass, x.Name)
}
tp := types.NewTypeFromString(x.Type, p.Class)
if tp.String() == "" {
fmt.Printf("AddMethod(%s): type is empty.\n", x.Name)
fmt.Printf("x.Type = %s\n", x.Type)
}
m := &Method{
Name: x.Name,
GoName: strings.Title(x.Name),
@@ -571,20 +593,32 @@ func (w *Wrapper) AddMethod(p *MethodCollection, x *ast.ObjCMethodDecl) {
Unavailable: false,
}
if Debug {
fmt.Printf(" -- Method %s\n", m.Name)
fmt.Printf(" -- Method %s type %s\n", m.Name, m.Type.CType())
}
var avail bool
m.Parameters, avail, m.Unavailable = w.GetParms(x, p.Class)
m.Parameters, avail, m.Unavailable = w.GetParms(x, p.Class, prt)
if avail {
if Debug {
fmt.Printf("%s: Adding %s (%d)\n", p.Class, m.Name, len(m.Parameters))
if Debug || prt {
fmt.Printf("%s: Adding %s (%d) type %s\n", p.Class, m.Name, len(m.Parameters), m.Type.GoType())
}
p.Methods = append(p.Methods, m)
// p.Methods = append(p.Methods, m)
} else {
if Debug {
m.Unavailable = true
p.Unavailable[m.LongName()] = true
if m.Name == "init" {
y := w.Interfaces[p.Class]
if y != nil {
if prt {
fmt.Printf(" Marking new method as unavailable for %s\n", p.Class)
}
y.ClassMethods.Unavailable["New"] = true
}
}
if Debug || prt {
fmt.Printf("--Method %s is not available\n", m.Name)
}
}
p.Methods = append(p.Methods, m)
}
//FIXME: copied from nswrap/main.go, should put this in a utils package
@@ -598,6 +632,7 @@ func matches(x string, rs []string) bool {
}
func (w *Wrapper) AddEnum(n *ast.EnumDecl, rs []string) {
prt := false
if n.Name != "" && !matches(n.Name, rs) {
return
}
@@ -622,7 +657,7 @@ func (w *Wrapper) AddEnum(n *ast.EnumDecl, rs []string) {
for _, c := range n.Children() {
switch x := c.(type) {
case *ast.AvailabilityAttr, *ast.UnavailableAttr, *ast.DeprecatedAttr:
a.Add(x)
a.Add(x, prt)
case *ast.EnumConstantDecl:
if Debug {
fmt.Printf("*ast.EnumConstantDecl: (%s) '%s': %s\n", n.Type, n.Name, x.Name)
@@ -649,6 +684,13 @@ func (w *Wrapper) AddEnum(n *ast.EnumDecl, rs []string) {
//Add an Interface or add a Category to an Interface
func (w *Wrapper) addIntCat(name string, ns []ast.Node) {
prt := false
if name == dbclass {
prt = true
}
if Debug || prt {
fmt.Printf("addIntCat(%s)\n", name)
}
var i *Interface
var ok bool
goname := strings.Title(name)
@@ -662,6 +704,7 @@ func (w *Wrapper) addIntCat(name string, ns []ast.Node) {
i.Properties = map[string]*Property{}
i.Protocols = []string{}
i.ProcessedInstanceMethods = map[string]bool{}
w.Interfaces[name] = i
}
avail := (*Avail)(&[]AvailAttr{})
mcc := NewMethodCollection(name)
@@ -670,18 +713,19 @@ func (w *Wrapper) addIntCat(name string, ns []ast.Node) {
for _, c := range ns {
switch x := c.(type) {
case *ast.ObjCPropertyDecl:
if Debug {
fmt.Printf("ObjCPropertyDecl: %s\n", x.Name)
}
p := &Property{
Name: x.Name,
Type: types.NewTypeFromString(x.Type, name),
Attr: x.Attr,
}
if Debug {
fmt.Printf("ObjCPropertyDecl: %s %s (%s)\n", x.Type, x.Name, p.Type.GoType())
}
i.Properties[x.Name] = p
case *ast.ObjCMethodDecl:
if Debug {
if Debug || prt {
fmt.Printf("ObjCMethodDecl: %s (%s) %s\n", x.Type, name, x.Name)
fmt.Printf(" type = %s\n", x.Type)
}
if name == "NSObject" && x.Name == "initialize" {
continue
@@ -709,14 +753,15 @@ func (w *Wrapper) addIntCat(name string, ns []ast.Node) {
}
types.SetTypeParam(name, x.Name, x.Type)
case *ast.AvailabilityAttr, *ast.UnavailableAttr, *ast.DeprecatedAttr:
avail.Add(x)
avail.Add(x, prt)
case *ast.Unknown:
if Debug {
fmt.Printf("(*ast.Unkonwn %s: %s)\n", x.Name, x.Content)
}
case *ast.ObjCRootClassAttr, *ast.VisibilityAttr,
*ast.ObjCIvarDecl, *ast.ArcWeakrefUnavailableAttr,
*ast.ObjCExceptionAttr, *ast.SwiftNameAttr:
*ast.ObjCExceptionAttr, *ast.SwiftNameAttr,
*ast.FullComment:
default:
fmt.Printf("AST parse error: node type is %s\n", reflect.TypeOf(x).String())
}
@@ -728,7 +773,25 @@ func (w *Wrapper) addIntCat(name string, ns []ast.Node) {
return
}
i.ClassMethods.Methods = append(i.ClassMethods.Methods, mcc.Methods...)
for k,v := range mcc.Unavailable {
if prt {
fmt.Printf(" class method %s: %t\n", k, v)
}
i.ClassMethods.Unavailable[k] = v
}
i.InstanceMethods.Methods = append(i.InstanceMethods.Methods, mci.Methods...)
for k,v := range mci.Unavailable {
if prt {
fmt.Printf(" instance method %s: %t\n", k, v)
}
i.InstanceMethods.Unavailable[k] = v
if k == "init" {
if prt {
fmt.Printf(" Marking new method as unavailable for %s\n", i.Name)
}
i.ClassMethods.Unavailable["New"] = true
}
}
i.Protocols = append(i.Protocols, prots...)
if Debug {
@@ -748,11 +811,11 @@ type AvailAttr struct {
type Avail []AvailAttr
func (a *Avail) Add(n ast.Node) {
func (a *Avail) Add(n ast.Node, prt bool) {
switch x := n.(type) {
case *ast.AvailabilityAttr:
if Debug {
fmt.Printf(" AvailabilityAttr: OS: %s, Version: %s, Deprecated: %t\n", x.OS, x.Version, (x.Unknown1 != "0") || x.IsUnavailable)
if Debug || prt {
fmt.Printf(" AvailabilityAttr: OS: %s, Version: %s, Deprecated: %t\n", x.OS, x.Version, x.OS == "macos" && ((x.Unknown1 != "0") || x.IsUnavailable))
}
if x.OS != "macos" {
return
@@ -763,7 +826,7 @@ func (a *Avail) Add(n ast.Node) {
Deprecated: (x.Unknown1 != "0") || x.IsUnavailable,
})
case *ast.UnavailableAttr, *ast.DeprecatedAttr:
if Debug {
if Debug || prt {
fmt.Printf(" DeprecatedAttr\n")
}
*a = append(*a, AvailAttr{
@@ -786,14 +849,20 @@ func (a *Avail) Available() bool {
//GetParms returns the parameters of a method declaration and a bool
//indicating whether the given method is available on MacOS and not
//deprecated.
func (w *Wrapper) GetParms(n ast.Node, class string) ([]*Parameter, bool, bool) {
func (w *Wrapper) GetParms(n ast.Node, class string, prt bool) ([]*Parameter, bool, bool) {
ret := make([]*Parameter, 0)
unavail := false
variadic := false
avail := (*Avail)(&[]AvailAttr{})
var parms []string
switch x := n.(type) {
case *ast.ObjCMethodDecl:
parms = x.Parameters
for _,z := range x.Attr {
if z == "variadic" {
variadic = true
}
}
case *ast.FunctionDecl:
default:
panic("GetParms called with wrong node type")
@@ -812,26 +881,35 @@ func (w *Wrapper) GetParms(n ast.Node, class string) ([]*Parameter, bool, bool)
ret = append(ret, p)
j++
case *ast.Variadic:
//ret[j-1].Type.Variadic = true
p := &Parameter{
Vname: "object",
Type: types.NewTypeFromString("NSObject*", ""),
}
p.Type.Variadic = true
ret = append(ret, p)
variadic = true
j++
case *ast.UnavailableAttr:
if prt {
fmt.Printf(" UnavailableAttr\n")
}
unavail = true
case *ast.AvailabilityAttr, *ast.DeprecatedAttr:
avail.Add(x)
avail.Add(x, prt)
case *ast.Unknown:
if Debug {
fmt.Printf("GetParms(): ast.Unknown: %s\n", x.Name)
}
}
}
if variadic {
p := &Parameter{
Vname: "object",
Type: types.NewTypeFromString("NSObject*", ""),
}
p.Type.Variadic = true
ret = append(ret, p)
}
// check that the method is available for this OS and not deprecated
if !avail.Available() {
if unavail || !avail.Available() {
if prt {
fmt.Printf(" it's unavailable\n")
}
return nil, false, unavail
}
return ret, true, unavail
@@ -1027,15 +1105,61 @@ func (w *Wrapper) ProcessFunction(m *Method) {
}
func (w *Wrapper) _processMethod(m *Method, fun bool) {
prt := false
if m.Class == dbclass && m.Name == "new" {
prt = true
}
if Debug {
fmt.Printf(" method: %s (%s)\n", m.Name, m.Type)
fmt.Printf(" (%s) method: %s (%s)\n", m.Class, m.Name, m.Type)
}
if m.HasUnsupportedType() {
return
}
if m.Unavailable {
if prt {
fmt.Printf(" %s unavailable\n")
}
return
}
if x := w.Interfaces[m.Class]; x != nil {
if prt {
fmt.Printf("processMethod (%s) %s\n", m.Class, m.Name)
}
if m.ClassMethod {
if prt {
fmt.Printf(" %s class method", m.Name)
}
y := x.ClassMethods
if y != nil {
if y.Unavailable[m.LongName()] {
if prt {
fmt.Printf(" marked unavailable\n")
}
return
} else {
if prt {
fmt.Printf("\n")
}
}
}
} else {
if prt {
fmt.Printf(" %s instance method", m.Name)
}
y := x.InstanceMethods
if y != nil {
if y.Unavailable[m.LongName()] {
if prt {
fmt.Printf(" marked unavailable")
}
return
}
}
}
if prt {
fmt.Printf("\n")
}
}
w.processType(m.Type)
gname := m.GoName
gname = strings.ReplaceAll(gname, "_", " ")
@@ -1186,6 +1310,10 @@ func %s%s(%s) %s {
if fun {
// return
}
if cmtype == "" {
fmt.Printf("Method: %s type: '%s'\n", cname, cmtype)
fmt.Printf("Node: %s\n", m.Type.Node)
}
if !fun || len(tps) > 0 && tps[lparm].Variadic {
w.cCode.WriteString(fmt.Sprintf(`
%s
@@ -1541,8 +1669,8 @@ func (w *Wrapper) _ProcessDelSub(dname string, ps map[string][]string, nms []*Me
if m.Unavailable {
continue
}
if Debug {
fmt.Printf(" -> %s\n", m.Name)
if x := w.Interfaces[dname]; x != nil && x.InstanceMethods != nil && x.InstanceMethods.Unavailable[m.LongName()] {
continue
}
if matches(string(m.Name[0])+m.GoName[1:], pats) {
mc.Methods = append(mc.Methods, m)
@@ -1556,6 +1684,9 @@ func (w *Wrapper) _ProcessDelSub(dname string, ps map[string][]string, nms []*Me
if m.Unavailable {
continue
}
if x := w.Interfaces[dname]; x != nil && x.InstanceMethods != nil && x.InstanceMethods.Unavailable[m.LongName()] {
continue
}
if Debug {
fmt.Printf(" -> %s\n", m.Name)
}
@@ -2109,9 +2240,37 @@ func (o *%s) Super%s(%s) %s {
//Add class and instance methods from super class
func (w *Wrapper) AddSupermethods(i *Interface) {
prt := false
if i.Name == dbclass {
prt = true
fmt.Printf("%s\n", dbclass)
}
var supmethods func(string)
procsups := func(mc, smc *MethodCollection) {
for _, m := range smc.Methods {
if mc.Unavailable[m.LongName()] {
if prt {
fmt.Printf(" %s (from %s): previously marked unavailable\n", m.Name, smc.Class)
}
continue
}
if m.Unavailable {
if prt {
fmt.Printf(" %s (from %s): marked unavailable\n", m.Name, smc.Class)
}
mc.Unavailable[m.LongName()] = true
if m.Name == "init" {
if prt {
fmt.Printf(" Marking new method unavailable in %s\n", mc.Class)
}
w.Interfaces[mc.Class].ClassMethods.Unavailable["New"] = true
}
continue
}
if prt {
fmt.Printf(" %s (from %s): available\n", m.Name, smc.Class)
}
m2 := &Method{
Name: m.Name,
GoName: m.GoName,
@@ -2130,6 +2289,9 @@ func (w *Wrapper) AddSupermethods(i *Interface) {
}
m2.Parameters = append(m2.Parameters, p2)
}
if prt {
fmt.Printf(" -> adding method %s\n", m.Name)
}
mc.Methods = append(mc.Methods, m2)
}
}
@@ -2148,8 +2310,29 @@ func (w *Wrapper) AddSupermethods(i *Interface) {
//Add methods from a Protocol
func (w *Wrapper) AddProtocolMethods(i *Interface, p *Protocol) {
prt := false
if i.Name == dbclass {
prt = true
fmt.Printf("%s\n",dbclass)
}
procmeths := func(mc, pmc *MethodCollection) {
for _, m := range pmc.Methods {
if mc.Unavailable[m.LongName()] {
if prt {
fmt.Printf(" %s: previously marked unavailable\n", m.Name)
}
continue
}
if m.Unavailable {
if prt {
fmt.Printf(" %s: marked unavailable\n", m.Name)
}
mc.Unavailable[m.LongName()] = true
continue
}
if prt {
fmt.Printf(" %s: available\n", m.Name)
}
m2 := &Method{
Name: m.Name,
GoName: m.GoName,
@@ -2168,6 +2351,9 @@ func (w *Wrapper) AddProtocolMethods(i *Interface, p *Protocol) {
}
m2.Parameters = append(m2.Parameters, p2)
}
if prt {
fmt.Printf(" -> adding method %s\n", m.Name)
}
mc.Methods = append(mc.Methods, m2)
}
}