func (j *Job) arrayType(n cc.Node, t cc.Type) { if ne := t.Elements(); ne >= 0 { j.body("[%v]", ne) j.typ(n, t.Element()) return } todo(n, t) }
func eqTypes(n cc.Node, a, b cc.Type) bool { if a.Kind() == cc.Ptr { if t := a.Element(); t.Kind() == cc.Function { a = t } } if b.Kind() == cc.Ptr { if t := b.Element(); t.Kind() == cc.Function { b = t } } return removeQualifiers(a.String()) == removeQualifiers(b.String()) }
func (j *Job) addTag(t cc.Type) { for t.Kind() == cc.Ptr || t.Kind() == cc.Array { t = t.Element() } tag := t.Tag() if tag == 0 { return } n := j.In.Declarations.Tags[tag].Node d := t.Declarator() if j.ImportPath(n) == 0 && j.tags[tag] == nil { j.tags[tag] = d } }
func (j *Job) initializerList(st cc.Type, n *cc.InitializerList) { j.typ(n, st) j.body("{\n") defer j.body("\n}") if st.Kind() == cc.Union { return } if st.Kind() == cc.Array { t := st.Element() for ; n != nil; n = n.InitializerList { if n.DesignationOpt != nil { todo(n) } j.initializer(t, n.Initializer) j.body(",") j.body("\n") } return } var m []cc.Member if st != nil { m, _ = st.Members() } i := 0 for ; n != nil; n = n.InitializerList { if n.DesignationOpt != nil { todo(n) } var t cc.Type if i < len(m) { t = m[i].Type } j.initializer(t, n.Initializer) j.body(",") j.body("\n") i++ } }
func (j *Job) addTag2(t cc.Type) { for t.Kind() == cc.Ptr || t.Kind() == cc.Array { t = t.Element() } tag := t.Tag() var n cc.Node if tag != 0 { n = j.In.Declarations.Tags[tag].Node } d := t.Declarator() if (n != nil && j.ImportPath(n) != 0) || j.tags[tag] != nil { return } if tag != 0 { j.tags[tag] = d } m, _ := t.Members() for _, v := range m { j.addTag2(v.Type) } }
func (j *Job) convert(ot cc.Type, n *cc.Expression) { t := n.Type switch otk := ot.Kind(); otk { case cc.Ptr: switch t.Kind() { case cc.Int: if v := n.Value; v != nil && isZero(n, v) { j.body(" nil") return } todo(n, ot, t) } if ot.Element().Kind() == cc.Void { switch t.Kind() { case cc.Ptr: j.imports[idUnsafe] = struct{}{} j.body(" unsafe.Pointer(uintptr(0)/*TODO536*/)") break //TODO j.body(" unsafe.Pointer/*DBG536 %v %s */(", n.Case, n.Type) //TODO j.expression(nil, n) //TODO j.body("/*DBG538*/)") case cc.Array: switch { case t.Elements() > 0: j.imports[idUnsafe] = struct{}{} j.body(" unsafe.Pointer(&") j.expression(nil, n) j.body("[0])") default: todo(n, ot, t) } default: todo(n, ot, t) } break } switch t.Kind() { case cc.Ptr: switch t.Element().Kind() { case cc.Void: j.body("(") j.typ(n, ot) j.body(")(") j.expression(nil, n) j.body(")") default: j.cast(ot, n) } return case cc.Array: switch { case t.Elements() > 0: j.body(" &") j.expression(nil, n) j.body("[0]") default: todo(n, ot, t) } return } switch l := ot.Element(); l.Kind() { case cc.Function: switch t.Kind() { case cc.Function: j.cast(ot, n) default: todo(n, l, t) } case cc.Char: switch t.Kind() { case cc.Ptr: switch r := t.Element(); r.Kind() { case cc.Char: j.expression(nil, n) default: todo(n, l, r) } default: todo(n, l, t) } default: todo(n, ot, t) } case cc.Char, cc.Int, cc.LongLong, cc.SChar, cc.Short, cc.UChar, cc.UInt, cc.ULong, cc.ULongLong, cc.UShort: j.body(" %s(", j.Model.Items[otk].More) j.expression(nil, n) j.body(")") case cc.Void: if isLiteral(n) { break } j.expression(nil, n) default: todo(n, ot, t) } }
func (j *Job) typ(n cc.Node, t cc.Type) { if t.Declarator().RawSpecifier().TypedefName() == idVaList { j.body("[]interface{}") return } stars := 0 for t.Kind() == cc.Ptr { stars++ t = t.Element() } stars0 := stars if (t.Kind() == cc.Void || t.Kind() == cc.Function) && stars > 0 { stars-- } j.body("%s", strings.Repeat("*", stars)) switch k := t.Kind(); k { case cc.Char, cc.Double, cc.Int, cc.Long, cc.LongDouble, cc.LongLong, cc.SChar, cc.Short, cc.UChar, cc.ULong, cc.ULongLong, cc.UInt, cc.UShort: j.body(" %s", j.Model.Items[k].More) case cc.Void: if stars0 == 0 { todo(n) } j.body(" unsafe.Pointer") j.imports[idUnsafe] = struct{}{} case cc.Struct, cc.Union: // Use tag if available. if tag := t.Tag(); tag != 0 { nm := mangleTag(tag, true) j.qualifier(j.In.Declarations.Tags[tag].Node) j.body(" %s", xc.Dict.S(nm)) break } // Use typedef name if available. if tdn := t.Declarator().RawSpecifier().TypedefName(); tdn != 0 { nm := mangleIdent(tdn, true) j.qualifier(j.In.Declarations.Identifiers[tdn].Node) j.body(" %s", xc.Dict.S(nm)) break } j.body(" struct {") switch m, incomplete := t.Members(); { case incomplete: j.body("// Incomplete type\n_ byte") case t.Kind() == cc.Union: j.union(n, t) var f func(cc.Type) f = func(t cc.Type) { j.addTag(t) m, _ := t.Members() for _, v := range m { f(v.Type) } } default: for _, v := range m { if v.Bits != 0 { j.bits(n, v) continue } j.addTag(v.Type) id := v.Name nm := mangleIdent(id, true) j.body("\n%s", xc.Dict.S(nm)) j.typ(v.Declarator, v.Type) if t := v.Type; t.Kind() != cc.Union && t.Kind() != cc.Struct { j.body("// %s", v.Type) } } } j.body("\n}") case cc.Function: j.body(" func") j.signature(n, t) case cc.Array: j.arrayType(n, t) default: todo(n, t, t.Kind()) } }