Better handling of multiple classes and input files. Handle
Objective C type parameter declarations. Check Typedefs when determining if a Type is a pointer or a function.
This commit is contained in:
@@ -102,11 +102,6 @@ func NodeNamed(k string, p Parser) Parser {
|
||||
|
||||
// Combinators
|
||||
|
||||
//Id is the identity parser
|
||||
func Id(s string, n *Node) (string, *Node) {
|
||||
return s,n
|
||||
}
|
||||
|
||||
//Opt optionally runs a Parser, returning the input node if it fails
|
||||
func Opt(p Parser) Parser {
|
||||
return func(s string, n *Node) (string, *Node) {
|
||||
|
||||
+60
-6
@@ -12,6 +12,12 @@ var super map[string]string
|
||||
//go struct.
|
||||
var wrapped map[string]bool
|
||||
|
||||
//TypeParameters maps, for each class, a TypedefName to a type, representing
|
||||
//the Objective-C type parameters for that class
|
||||
var TypeParameters map[string]map[string]string
|
||||
|
||||
var Typedefs map[string]*Type
|
||||
|
||||
func Super(c string) string {
|
||||
if super == nil {
|
||||
super = make(map[string]string)
|
||||
@@ -26,6 +32,23 @@ func SetSuper(c, p string) {
|
||||
super[c] = p
|
||||
}
|
||||
|
||||
func SetTypeParam(c, n, t string) {
|
||||
if TypeParameters == nil {
|
||||
TypeParameters = make(map[string]map[string]string)
|
||||
}
|
||||
if TypeParameters[c] == nil {
|
||||
TypeParameters[c] = make(map[string]string)
|
||||
}
|
||||
TypeParameters[c][n] = t
|
||||
}
|
||||
|
||||
func AddTypedef(n,t string) {
|
||||
if Typedefs == nil {
|
||||
Typedefs = make(map[string]*Type)
|
||||
}
|
||||
Typedefs[n] = NewTypeFromString(t,"")
|
||||
}
|
||||
|
||||
type Type struct {
|
||||
Node *Node
|
||||
Class string
|
||||
@@ -42,12 +65,22 @@ func NewType(n *Node, c string) *Type {
|
||||
|
||||
func NewTypeFromString(t,c string) *Type {
|
||||
n,err := Parse(t)
|
||||
if n.CtypeSimplified() == "id" {
|
||||
if n.IsId() {
|
||||
//if n.CtypeSimplified() == "id" {
|
||||
n,err = Parse("NSObject*")
|
||||
}
|
||||
if err != nil {
|
||||
return &Type{}
|
||||
}
|
||||
if TypeParameters[c] != nil {
|
||||
recur := false
|
||||
for k,v := range TypeParameters[c] {
|
||||
recur = n.renameTypedefs(k,v)
|
||||
}
|
||||
if recur {
|
||||
return NewTypeFromString(n.Ctype(),c)
|
||||
}
|
||||
}
|
||||
return &Type{
|
||||
Node: n,
|
||||
Class: c,
|
||||
@@ -117,19 +150,21 @@ func (t *Type) CTypeAttrib() string {
|
||||
}
|
||||
|
||||
func (t *Type) _CType(attrib bool) string {
|
||||
//if !attrib && c.ctype != "" ... FIXME?
|
||||
if t.ctype != "" { // cache
|
||||
return t.ctype
|
||||
}
|
||||
var ct string
|
||||
if attrib {
|
||||
ct = t.Node.Ctype()
|
||||
ignore := map[string]bool { "GenericList": true }
|
||||
ct = t.Node._Ctype(ignore)
|
||||
} else {
|
||||
ct = t.Node.CtypeSimplified()
|
||||
}
|
||||
ct = strings.ReplaceAll(ct,"instancename",t.Class)
|
||||
ct = strings.ReplaceAll(ct,"instancetype",t.Class + " *")
|
||||
if len(ct) > 1 && ct[:2] == "id" {
|
||||
ct = "NSObject *" + ct[2:]
|
||||
ct = "NSObject*" + ct[2:]
|
||||
}
|
||||
if len(ct) > 11 {
|
||||
if ct[:12] == "instancename" { ct = t.Class + ct[12:] }
|
||||
@@ -148,14 +183,18 @@ func (t *Type) GoTypeDecl() string {
|
||||
return t.GoInterfaceDecl()
|
||||
}
|
||||
tp := t.BaseType()
|
||||
if tp.Node.IsId() {
|
||||
return ""
|
||||
}
|
||||
gt := tp.GoType()
|
||||
switch gt {
|
||||
case "", "Void":
|
||||
return ""
|
||||
default:
|
||||
return fmt.Sprintf(`
|
||||
//%s
|
||||
type %s %s
|
||||
`,gt,tp.CGoType())
|
||||
`,t.Node.CtypeSimplified(),gt,tp.CGoType())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,12 +209,27 @@ func (t *Type) GoInterfaceDecl() string {
|
||||
super = "ptr unsafe.Pointer"
|
||||
}
|
||||
return fmt.Sprintf(`
|
||||
//%s
|
||||
%stype %s struct { %s }
|
||||
`,x,gt,super)
|
||||
`,t.CTypeAttrib(),x,gt,super)
|
||||
}
|
||||
|
||||
func (t *Type) IsFunction() bool {
|
||||
if td,ok := Typedefs[t.BaseType().CType()]; ok {
|
||||
return td.IsFunction()
|
||||
}
|
||||
return t.Node.IsFunction()
|
||||
}
|
||||
|
||||
func (t *Type) IsPointer() bool {
|
||||
if td,ok := Typedefs[t.BaseType().CType()]; ok {
|
||||
return td.IsPointer()
|
||||
}
|
||||
return t.Node.IsPointer()
|
||||
}
|
||||
|
||||
func (t *Type) CToGo(cval string) string { // cast C value to CGo
|
||||
if t.Node.IsPointer() {
|
||||
if t.IsPointer() {
|
||||
cval = "unsafe.Pointer(" + cval + ")"
|
||||
}
|
||||
return fmt.Sprintf("(%s)(%s)",t.GoType(),cval)
|
||||
|
||||
@@ -30,6 +30,7 @@ func AbstractDeclarator(s string, n *Node) (string, *Node) {
|
||||
Opt(Pointer),
|
||||
OneOrMore(DirectAbstractDeclarator)),
|
||||
Pointer,
|
||||
Id,
|
||||
Block,
|
||||
)(s,n)
|
||||
}
|
||||
@@ -136,6 +137,14 @@ func NullableAnnotation(s string, n *Node) (string, *Node) {
|
||||
))(s,n)
|
||||
}
|
||||
|
||||
func Id(s string, n *Node) (string, *Node) {
|
||||
return Seq(
|
||||
NodeNamed("Id",Lit("id")),
|
||||
Opt(TypeQualifierList),
|
||||
Opt(NullableAnnotation),
|
||||
)(s,n)
|
||||
}
|
||||
|
||||
func Pointer(s string, n *Node) (string, *Node) {
|
||||
return Seq(
|
||||
NodeNamed("Pointer",Lit("*")),
|
||||
|
||||
+8
-1
@@ -96,7 +96,7 @@ func (n *Node) PointsTo() *Node {
|
||||
|
||||
//IsPointer returns true if the node is a pointer
|
||||
func (n *Node) IsPointer() bool {
|
||||
return n.PointsTo() != nil
|
||||
return n.IsId() || n.PointsTo() != nil
|
||||
}
|
||||
|
||||
//ArrayOf, when called on an array node returns a node describing the type
|
||||
@@ -126,6 +126,13 @@ func (n *Node) IsFunction() bool {
|
||||
return n.Children[len(n.Children)-1].Kind == "Function"
|
||||
}
|
||||
|
||||
func (n *Node) IsId() bool {
|
||||
if n == nil || len(n.Children) < 1 {
|
||||
return false
|
||||
}
|
||||
return n.Children[0].Kind == "TypedefName" && n.Children[0].Content == "id"
|
||||
}
|
||||
|
||||
//BaseType strips off all layers of pointer indirection
|
||||
func (n *Node) BaseType() *Node {
|
||||
if n == nil {
|
||||
|
||||
@@ -67,6 +67,24 @@ func (n *Node) AddChild(c *Node) *Node {
|
||||
return n
|
||||
}
|
||||
|
||||
//returns true if anything gets renamed
|
||||
func (n *Node) renameTypedefs(a,b string) (ret bool) {
|
||||
ret = false
|
||||
if n == nil { return }
|
||||
for i,c := range n.Children {
|
||||
if c.Kind == "TypedefName" && c.Content == a {
|
||||
ret = true
|
||||
n.Children[i] = NewNode("TypedefName", b)
|
||||
n.Children[i].Children = c.Children
|
||||
}
|
||||
if len(c.Children) > 0 {
|
||||
ret2 := c.renameTypedefs(a,b)
|
||||
ret = ret || ret2
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (n *Node) CtypeSimplified() string {
|
||||
ignore := map[string]bool{
|
||||
"NullableAnnotation": true,
|
||||
|
||||
Reference in New Issue
Block a user