Go type declarations should point to C typedefs. Add parser for

C enum types.
This commit is contained in:
Greg 2019-04-29 13:31:13 -04:00
parent c03e37bd54
commit 57232548fa
2 changed files with 35 additions and 35 deletions

View File

@ -13,10 +13,6 @@ var super map[string]string
var wrapped map[string]bool var wrapped map[string]bool
func shouldWrap(gt string) bool { func shouldWrap(gt string) bool {
if wrapped == nil {
wrapped = make(map[string]bool)
return false
}
return gt != "" && gt[0] == '*' && wrapped[gt[1:]] return gt != "" && gt[0] == '*' && wrapped[gt[1:]]
} }
@ -25,10 +21,6 @@ func shouldWrap(gt string) bool {
var goInterfaces map[string]bool var goInterfaces map[string]bool
func isGoInterface(gt string) bool { func isGoInterface(gt string) bool {
if goInterfaces == nil {
goInterfaces = make(map[string]bool)
return false
}
return goInterfaces[gt] return goInterfaces[gt]
} }
@ -36,26 +28,30 @@ func isGoInterface(gt string) bool {
//the Objective-C type parameters for that class //the Objective-C type parameters for that class
var TypeParameters map[string]map[string]string var TypeParameters map[string]map[string]string
var Typedefs map[string]*Type //Typedefs maps from C types to the Type of a typedef with that name.
var typedefs map[string]*Type
func (t *Type) Typedef() *Type {
return typedefs[t.BaseType().CType()]
}
func init() {
super = make(map[string]string)
wrapped = make(map[string]bool)
goInterfaces = make(map[string]bool)
TypeParameters = make(map[string]map[string]string)
typedefs = make(map[string]*Type)
}
func Super(c string) string { func Super(c string) string {
if super == nil {
super = make(map[string]string)
}
return super[c] return super[c]
} }
func SetSuper(c, p string) { func SetSuper(c, p string) {
if super == nil {
super = make(map[string]string)
}
super[c] = p super[c] = p
} }
func SetTypeParam(c, n, t string) { func SetTypeParam(c, n, t string) {
if TypeParameters == nil {
TypeParameters = make(map[string]map[string]string)
}
if TypeParameters[c] == nil { if TypeParameters[c] == nil {
TypeParameters[c] = make(map[string]string) TypeParameters[c] = make(map[string]string)
} }
@ -63,10 +59,7 @@ func SetTypeParam(c, n, t string) {
} }
func AddTypedef(n,t string) { func AddTypedef(n,t string) {
if Typedefs == nil { typedefs[n] = NewTypeFromString(t,"")
Typedefs = make(map[string]*Type)
}
Typedefs[n] = NewTypeFromString(t,"")
} }
type Type struct { type Type struct {
@ -117,9 +110,6 @@ func (t *Type) PointsTo() *Type {
} }
func Wrap(s string) { func Wrap(s string) {
if wrapped == nil {
wrapped = make(map[string]bool)
}
// it is the pointers to this type that get wrapped // it is the pointers to this type that get wrapped
wrapped[s] = true wrapped[s] = true
} }
@ -215,10 +205,18 @@ func (t *Type) GoTypeDecl() string {
case "", "Void": case "", "Void":
return "" return ""
default: default:
extra := t.Node.CtypeSimplified()
var cgt string
if td := tp.Typedef(); td != nil {
cgt = td.CGoType()
extra = "typedef " + td.Node.Ctype()
} else {
cgt = tp.CGoType()
}
return fmt.Sprintf(` return fmt.Sprintf(`
//%s //%s (%s)
type %s %s type %s %s
`,t.Node.CtypeSimplified(),gt,tp.CGoType()) `,t.Node.Ctype(),extra,gt,cgt)
} }
} }
@ -228,9 +226,6 @@ func (t *Type) GoInterfaceDecl() string {
gt = gt[1:] // dereference wrapped types gt = gt[1:] // dereference wrapped types
} }
super := Super(gt) super := Super(gt)
if goInterfaces == nil {
goInterfaces = make(map[string]bool)
}
if super == "" { if super == "" {
goInterfaces[gt] = true goInterfaces[gt] = true
return fmt.Sprintf(` return fmt.Sprintf(`
@ -251,14 +246,14 @@ func (o *%s) Ptr() unsafe.Pointer { return o.ptr }
} }
func (t *Type) IsFunction() bool { func (t *Type) IsFunction() bool {
if td,ok := Typedefs[t.BaseType().CType()]; ok { if td := t.Typedef(); td != nil {
return td.IsFunction() return td.IsFunction()
} }
return t.Node.IsFunction() return t.Node.IsFunction()
} }
func (t *Type) IsPointer() bool { func (t *Type) IsPointer() bool {
if td,ok := Typedefs[t.BaseType().CType()]; ok { if td := t.Typedef(); td != nil {
return td.IsPointer() return td.IsPointer()
} }
return t.Node.IsPointer() return t.Node.IsPointer()

View File

@ -270,7 +270,7 @@ func TypeSpecifier(s string, n *Node) (string, *Node) {
Word("_Bool"), Word("_Bool"),
Word("_Complex"), Word("_Complex"),
//StructOrUnionSpecifier, //StructOrUnionSpecifier,
//EnumSpecifier, EnumSpecifier,
//TypedefName, //TypedefName,
))(s,n) ))(s,n)
} }
@ -284,10 +284,10 @@ func TypeQualifier(s string, n *Node) (string, *Node) {
} }
func StructOrUnionSpecifier(s string, n *Node) (string, *Node) { func StructOrUnionSpecifier(s string, n *Node) (string, *Node) {
return NodeNamed("StructOrUnionSpecifier",OneOf( return OneOf(
// Seq(StructOrUnion,Opt(Identifier),StructDeclarationList), // Seq(StructOrUnion,Opt(Identifier),StructDeclarationList),
Nest(StructOrUnion,Identifier), Nest(StructOrUnion,Identifier),
))(s,n) )(s,n)
} }
func StructOrUnion(s string, n *Node) (string, *Node) { func StructOrUnion(s string, n *Node) (string, *Node) {
@ -296,6 +296,11 @@ func StructOrUnion(s string, n *Node) (string, *Node) {
NodeNamed("Union",Word("union")))(s,n) NodeNamed("Union",Word("union")))(s,n)
} }
func EnumSpecifier(s string, n *Node) (string, *Node) {
return Nest(
NodeNamed("Enum",Word("enum")),Identifier)(s,n)
}
func Generic(s string, n *Node) (string, *Node) { func Generic(s string, n *Node) (string, *Node) {
return NodeNamed("Generic",TypeName)(s,n) return NodeNamed("Generic",TypeName)(s,n)
} }