예제 #1
0
// indirect(typ) assumes that typ is a pointer type,
// or named alias thereof, and returns its base type.
// Panic ensures if it is not a pointer.
//
func indirectType(ptr types.Type) types.Type {
	if v, ok := underlyingType(ptr).(*types.Pointer); ok {
		return v.Base
	}
	// When debugging it is convenient to comment out this line
	// and let it continue to print the (illegal) SSA form.
	panic("indirect() of non-pointer type: " + ptr.String())
	return nil
}
예제 #2
0
// writeSignature writes to w the signature sig in declaration syntax.
// Derived from types.Signature.String().
//
func writeSignature(w io.Writer, name string, sig *types.Signature, params []*Parameter) {
	io.WriteString(w, "func ")
	if sig.Recv != nil {
		io.WriteString(w, "(")
		if n := params[0].Name(); n != "" {
			io.WriteString(w, n)
			io.WriteString(w, " ")
		}
		io.WriteString(w, params[0].Type().String())
		io.WriteString(w, ") ")
		params = params[1:]
	}
	io.WriteString(w, name)
	io.WriteString(w, "(")
	for i, v := range params {
		if i > 0 {
			io.WriteString(w, ", ")
		}
		io.WriteString(w, v.Name())
		io.WriteString(w, " ")
		if sig.IsVariadic && i == len(params)-1 {
			io.WriteString(w, "...")
			io.WriteString(w, underlyingType(v.Type()).(*types.Slice).Elt.String())
		} else {
			io.WriteString(w, v.Type().String())
		}
	}
	io.WriteString(w, ")")
	if res := sig.Results; res != nil {
		io.WriteString(w, " ")
		var t types.Type
		if len(res) == 1 && res[0].Name == "" {
			t = res[0].Type
		} else {
			t = &types.Result{Values: res}
		}
		io.WriteString(w, t.String())
	}
}
예제 #3
0
// hashType returns a hash for t such that
// types.IsIdentical(x, y) => hashType(x) == hashType(y).
func hashType(t types.Type) int {
	return hashString(t.String()) // TODO(gri): provide a better hash
}