Beispiel #1
0
// writeSignature writes to buf the signature sig in declaration syntax.
func writeSignature(buf *bytes.Buffer, from *types.Package, name string, sig *types.Signature, params []*Parameter) {
	buf.WriteString("func ")
	if recv := sig.Recv(); recv != nil {
		buf.WriteString("(")
		if n := params[0].Name(); n != "" {
			buf.WriteString(n)
			buf.WriteString(" ")
		}
		types.WriteType(buf, from, params[0].Type())
		buf.WriteString(") ")
	}
	buf.WriteString(name)
	types.WriteSignature(buf, from, sig)
}
Beispiel #2
0
func (cdd *CDD) Type(w *bytes.Buffer, typ types.Type) (dim []string, acds []*CDD) {
	direct := true

writeType:
	switch t := typ.(type) {
	case *types.Basic:
		if t.Kind() == types.UnsafePointer {
			w.WriteString("unsafe$Pointer")
		} else {
			types.WriteType(w, nil, t)
		}

	case *types.Named:
		tn := t.Obj()
		if tn.Name() == "error" {
			w.WriteString("error")
		} else {
			cdd.Name(w, tn, direct)
		}

	case *types.Pointer:
		typ = t.Elem()
		direct = false
		dim = append(dim, "*")
		goto writeType

	case *types.Struct:
		if t.NumFields() == 0 {
			w.WriteString("empty")
			break
		}
		w.WriteString("struct {\n")
		cdd.il++
		for i, n := 0, t.NumFields(); i < n; i++ {
			f := t.Field(i)
			cdd.indent(w)
			if tag := t.Tag(i); tag != "" {
				w.WriteString(reflect.StructTag(tag).Get("C"))
				w.WriteByte(' ')
			}
			d, a := cdd.Type(w, f.Type())
			acds = append(acds, a...)
			if !f.Anonymous() {
				w.WriteByte(' ')
				name := dimFuncPtr(f.Name(), d)
				if name == "_" {
					name += strconv.Itoa(i) + "$"
				}
				w.WriteString(name)
			}
			w.WriteString(";\n")
		}

		cdd.il--
		cdd.indent(w)
		w.WriteByte('}')

	case *types.Array:
		dim = append(dim, "["+strconv.FormatInt(t.Len(), 10)+"]")
		d, a := cdd.Type(w, t.Elem())
		dim = append(dim, d...)
		acds = append(acds, a...)

	case *types.Slice:
		w.WriteString("slice")

	case *types.Map:
		w.WriteString("map")

	case *types.Chan:
		w.WriteString("chan")

	case *types.Signature:
		res, params := cdd.signature(t, true, noNames)
		w.WriteString(res.typ)
		dim = append(dim, params)
		dim = append(dim, res.dim...)
		acds = append(acds, res.acds...)

	default:
		fmt.Fprintf(w, "<%T>", t)
	}
	return
}