Create GoString helper methods for anything that has a "WithString"

method. Multiple bug fixes in type system, fixed parsing of id,
instancename and instancetype, allow NullableAnnotations after
generic lists (e.g. "id<ObjectType> _Nullable"). Helper function
to identify types that are function pointers.
This commit is contained in:
gmp
2019-05-03 13:14:30 -04:00
parent 8ea3d57cf0
commit eced251710
9 changed files with 103 additions and 33 deletions
+18 -9
View File
@@ -50,7 +50,7 @@ func init() {
TypeParameters = make(map[string]map[string]string)
typedefs = make(map[string]*Type)
r_id = regexp.MustCompile("\bid\b")
r_id = regexp.MustCompile(`\bid\b`)
r_instancename = regexp.MustCompile(`\binstancename\b`)
r_instancetype = regexp.MustCompile(`\binstancetype\b`)
}
@@ -117,7 +117,6 @@ func NewType(n *Node, c string) *Type {
return &Type{
Node: n2,
Class: c,
//ctype: "",
}
}
@@ -125,19 +124,16 @@ func NewTypeFromString(t,c string) *Type {
//fmt.Printf("t/c: %s/%s\n",t,c)
n,err := Parse(t)
//fmt.Printf("%p %s",n,n.String())
if n.IsId() {
n,err = Parse("NSObject*")
}
if err != nil {
return &Type{}
}
if n2,ok := clean(n, c); ok {
//found type parameters, re-parse
return NewTypeFromString(n2.Ctype(),c)
}
return &Type{
Node: n,
Class: c,
//ctype: "",
}
}
@@ -219,9 +215,9 @@ func (t *Type) _CType(attrib bool) string {
//fmt.Println("nil sent to _CType()")
return ""
}
if !attrib && t.ctype != "" { // cache
return t.ctype
}
//if !attrib && t.ctype != "" { // cache
// return t.ctype
//}
var ct string
if attrib {
ignore := map[string]bool { "GenericList": true }
@@ -296,6 +292,19 @@ func (o *Id) %s() *%s { return (*%s)(unsafe.Pointer(o)) }
`,t.Node.Ctype(),t.BaseType().GoType(),gt,super,gt,gt,gt,gt)
}
func (t *Type) IsFunctionPtr() bool {
if t == nil {
return false
}
if td := t.Typedef(); td != nil {
return td.IsFunctionPtr()
}
for pt := t.PointsTo(); pt != nil; pt = pt.PointsTo() {
return pt.IsFunction()
}
return false
}
func (t *Type) IsFunction() bool {
if t == nil {
fmt.Println("nil sent to IsFunction()")
+15 -5
View File
@@ -81,21 +81,31 @@ digit:
[0-9]
*/
import (
"regexp"
)
var TypeName func(s string, n *Node) (string, *Node)
func init() {
instancename := regexp.MustCompile("instancename")
instancetype := regexp.MustCompile("instancetype")
cacheable := func(s string) bool {
return !instancetype.MatchString(s) && !instancename.MatchString(s)
}
cache := map[string]*Node{}
TypeName = func(s string, n *Node) (string, *Node) {
if n2,ok := cache[s]; ok {
return "",n2
}
s2,n2 := _TypeName(s,n)
if s2 == "" {
if s2 == "" && cacheable(s) {
cache[s] = n2
}
return s2,n2
}
TypeName = _TypeName
//TypeName = _TypeName
}
func _TypeName(s string, n *Node) (string, *Node) {
@@ -318,10 +328,10 @@ func BareTypedefName(s string, n *Node) (string, *Node) {
}
func TypedefName(s string, n *Node) (string, *Node) {
return OneOf(
Seq(BareTypedefName, AngBracketed(GenericList)),
Seq(BareTypedefName, NullableAnnotation),
return Seq(
BareTypedefName,
Opt(AngBracketed(GenericList)),
Opt(NullableAnnotation),
)(s,n)
}
+10 -3
View File
@@ -53,7 +53,7 @@ func (n *Node) stripAbstract(k string) *Node {
return nil
}
ret := NewNode(n.Kind)
cs := n.Children[:]
cs := append([]*Node{},n.Children...)
dbg("stripAbstract(): i = %d\n",i)
//Scan backwords skipping TypeQualifier and NullableAnnotation tags
@@ -97,7 +97,10 @@ func (n *Node) PointsTo() *Node {
//IsPointer returns true if the node is a pointer
func (n *Node) IsPointer() bool {
return n.IsId() || n.IsInstancetype() || n.PointsTo() != nil
if pt := n.PointsTo(); pt != nil {
return true
}
return n.IsInstancetype() || n.IsId()
}
//ArrayOf, when called on an array node returns a node describing the type
@@ -124,6 +127,9 @@ func (n *Node) IsFunction() bool {
if n == nil || len(n.Children) < 1 {
return false
}
if pt := n.PointsTo(); pt != nil {
return false
}
return n.Children[len(n.Children)-1].Kind == "Function"
}
@@ -140,7 +146,8 @@ func (n *Node) IsId() bool {
if n == nil || len(n.Children) < 1 {
return false
}
return n.Children[0].Kind == "TypedefName" &&
return !n.IsFunction() &&
n.Children[0].Kind == "TypedefName" &&
n.Children[0].Content == "id"
}