func hasError(sig *types.Signature) bool { res := sig.Results() if res == nil || res.Len() <= 0 { return false } nerr := 0 for i := 0; i < res.Len(); i++ { ret := res.At(i) if isErrorType(ret.Type()) { nerr++ } } switch { case nerr == 0: return false case nerr == 1: return true default: panic(fmt.Errorf( "gopy: invalid number of comma-errors (%d)", nerr, )) } return false }
func (p *exporter) signature(sig *types.Signature) { // We need the receiver information (T vs *T) // for methods associated with named types. // We do not record interface receiver types in the // export data because 1) the importer can derive them // from the interface type and 2) they create cycles // in the type graph. if recv := sig.Recv(); recv != nil { if _, ok := recv.Type().Underlying().(*types.Interface); !ok { // 1-element tuple p.int(1) p.param(recv) } else { // 0-element tuple p.int(0) } } else { // 0-element tuple p.int(0) } p.tuple(sig.Params()) p.tuple(sig.Results()) if sig.Variadic() { p.int(1) } else { p.int(0) } }
func (d *DIBuilder) descriptorSignature(t *types.Signature, name string) llvm.Value { // If there's a receiver change the receiver to an // additional (first) parameter, and take the value of // the resulting signature instead. if recv := t.Recv(); recv != nil { params := t.Params() paramvars := make([]*types.Var, int(params.Len()+1)) paramvars[0] = recv for i := 0; i < int(params.Len()); i++ { paramvars[i+1] = params.At(i) } params = types.NewTuple(paramvars...) t := types.NewSignature(nil, nil, params, t.Results(), t.Variadic()) return d.typeDebugDescriptor(t, name) } if dt, ok := d.types.At(t).(llvm.Value); ok { return dt } var returnType llvm.Value results := t.Results() switch n := results.Len(); n { case 0: returnType = d.DIType(nil) // void case 1: returnType = d.DIType(results.At(0).Type()) default: fields := make([]*types.Var, results.Len()) for i := range fields { f := results.At(i) // Structs may not have multiple fields // with the same name, excepting "_". if f.Name() == "" { f = types.NewVar(f.Pos(), f.Pkg(), "_", f.Type()) } fields[i] = f } returnType = d.typeDebugDescriptor(types.NewStruct(fields, nil), "") } var paramTypes []llvm.Value params := t.Params() if params != nil && params.Len() > 0 { paramTypes = make([]llvm.Value, params.Len()+1) paramTypes[0] = returnType for i := range paramTypes[1:] { paramTypes[i+1] = d.DIType(params.At(i).Type()) } } else { paramTypes = []llvm.Value{returnType} } // TODO(axw) get position of type definition for File field return d.builder.CreateSubroutineType(llvm.DISubroutineType{ Parameters: paramTypes, }) }
func newSignatureFrom(pkg *Package, sig *types.Signature) *Signature { var recv *Var if sig.Recv() != nil { recv = newVarFrom(pkg, sig.Recv()) } return &Signature{ ret: newVarsFrom(pkg, sig.Results()), args: newVarsFrom(pkg, sig.Params()), recv: recv, } }
func (c *funcContext) translateCall(e *ast.CallExpr, sig *types.Signature, fun *expression) *expression { args := c.translateArgs(sig, e.Args, e.Ellipsis.IsValid(), false) if c.Blocking[e] { resumeCase := c.caseCounter c.caseCounter++ returnVar := "$r" if sig.Results().Len() != 0 { returnVar = c.newVariable("_r") } c.Printf("%[1]s = %[2]s(%[3]s); /* */ $s = %[4]d; case %[4]d: if($c) { $c = false; %[1]s = %[1]s.$blk(); } if (%[1]s && %[1]s.$blk !== undefined) { break s; }", returnVar, fun, strings.Join(args, ", "), resumeCase) if sig.Results().Len() != 0 { return c.formatExpr("%s", returnVar) } return c.formatExpr("") } return c.formatExpr("%s(%s)", fun, strings.Join(args, ", ")) }
func newFuncFrom(p *Package, parent string, obj types.Object, sig *types.Signature) (Func, error) { haserr := false res := sig.Results() var ret types.Type switch res.Len() { case 2: if !isErrorType(res.At(1).Type()) { return Func{}, fmt.Errorf( "bind: second result value must be of type error: %s", obj, ) } haserr = true ret = res.At(0).Type() case 1: if isErrorType(res.At(0).Type()) { haserr = true ret = nil } else { ret = res.At(0).Type() } case 0: ret = nil default: return Func{}, fmt.Errorf("bind: too many results to return: %v", obj) } id := obj.Pkg().Name() + "_" + obj.Name() if parent != "" { id = obj.Pkg().Name() + "_" + parent + "_" + obj.Name() } return Func{ pkg: p, sig: newSignatureFrom(p, sig), typ: obj.Type(), name: obj.Name(), id: id, doc: p.getDoc(parent, obj), ret: ret, err: haserr, }, nil }
func (tm *llvmTypeMap) getSignatureInfo(sig *types.Signature) functionTypeInfo { var args, results []types.Type if sig.Recv() != nil { recvtype := sig.Recv().Type() if _, ok := recvtype.Underlying().(*types.Pointer); !ok && recvtype != types.Typ[types.UnsafePointer] { recvtype = types.NewPointer(recvtype) } args = []types.Type{recvtype} } for i := 0; i != sig.Params().Len(); i++ { args = append(args, sig.Params().At(i).Type()) } for i := 0; i != sig.Results().Len(); i++ { results = append(results, sig.Results().At(i).Type()) } return tm.getFunctionTypeInfo(args, results) }
func (p *printer) writeSignatureInternal(this *types.Package, sig *types.Signature, visited []types.Type) { p.writeTuple(this, sig.Params(), sig.Variadic(), visited) res := sig.Results() n := res.Len() if n == 0 { // no result return } p.print(" ") if n == 1 && res.At(0).Name() == "" { // single unnamed result p.writeTypeInternal(this, res.At(0).Type(), visited) return } // multiple or named result(s) p.writeTuple(this, res, false, visited) }
func parseMethodType(t *types.Signature) (ptype, rtype types.Type, err error) { mp := t.Params() if mp.Len() != 1 && mp.Len() != 2 { return nil, nil, errgo.New("wrong argument count") } ptype0 := mp.At(mp.Len() - 1).Type() ptype1, ok := ptype0.(*types.Pointer) if !ok { return nil, nil, errgo.New("parameter is not a pointer") } ptype = ptype1.Elem() if _, ok := ptype.Underlying().(*types.Struct); !ok { return nil, nil, errgo.Newf("parameter is %s, not a pointer to struct", ptype1.Elem()) } rp := t.Results() if rp.Len() > 2 { return nil, nil, errgo.New("wrong result count") } if rp.Len() == 2 { rtype = rp.At(0).Type() } return ptype, rtype, nil }
func (c *funcContext) translateBuiltin(name string, sig *types.Signature, args []ast.Expr, ellipsis bool) *expression { switch name { case "new": t := sig.Results().At(0).Type().(*types.Pointer) if c.p.Pkg.Path() == "syscall" && types.Identical(t.Elem().Underlying(), types.Typ[types.Uintptr]) { return c.formatExpr("new Uint8Array(8)") } switch t.Elem().Underlying().(type) { case *types.Struct, *types.Array: return c.formatExpr("%e", c.zeroValue(t.Elem())) default: return c.formatExpr("$newDataPointer(%e, %s)", c.zeroValue(t.Elem()), c.typeName(t)) } case "make": switch argType := c.p.TypeOf(args[0]).Underlying().(type) { case *types.Slice: t := c.typeName(c.p.TypeOf(args[0])) if len(args) == 3 { return c.formatExpr("$makeSlice(%s, %f, %f)", t, args[1], args[2]) } return c.formatExpr("$makeSlice(%s, %f)", t, args[1]) case *types.Map: return c.formatExpr("{}") case *types.Chan: length := "0" if len(args) == 2 { length = c.translateExpr(args[1]).String() } return c.formatExpr("new %s(%s)", c.typeName(c.p.TypeOf(args[0])), length) default: panic(fmt.Sprintf("Unhandled make type: %T\n", argType)) } case "len": switch argType := c.p.TypeOf(args[0]).Underlying().(type) { case *types.Basic: return c.formatExpr("%e.length", args[0]) case *types.Slice: return c.formatExpr("%e.$length", args[0]) case *types.Pointer: return c.formatExpr("(%e, %d)", args[0], argType.Elem().(*types.Array).Len()) case *types.Map: return c.formatExpr("$keys(%e).length", args[0]) case *types.Chan: return c.formatExpr("%e.$buffer.length", args[0]) // length of array is constant default: panic(fmt.Sprintf("Unhandled len type: %T\n", argType)) } case "cap": switch argType := c.p.TypeOf(args[0]).Underlying().(type) { case *types.Slice, *types.Chan: return c.formatExpr("%e.$capacity", args[0]) case *types.Pointer: return c.formatExpr("(%e, %d)", args[0], argType.Elem().(*types.Array).Len()) // capacity of array is constant default: panic(fmt.Sprintf("Unhandled cap type: %T\n", argType)) } case "panic": return c.formatExpr("$panic(%s)", c.translateImplicitConversion(args[0], types.NewInterface(nil, nil))) case "append": if ellipsis || len(args) == 1 { argStr := c.translateArgs(sig, args, ellipsis, false) return c.formatExpr("$appendSlice(%s, %s)", argStr[0], argStr[1]) } sliceType := sig.Results().At(0).Type().Underlying().(*types.Slice) return c.formatExpr("$append(%e, %s)", args[0], strings.Join(c.translateExprSlice(args[1:], sliceType.Elem()), ", ")) case "delete": keyType := c.p.TypeOf(args[0]).Underlying().(*types.Map).Key() return c.formatExpr(`delete %e[%s.keyFor(%s)]`, args[0], c.typeName(keyType), c.translateImplicitConversion(args[1], keyType)) case "copy": if basic, isBasic := c.p.TypeOf(args[1]).Underlying().(*types.Basic); isBasic && isString(basic) { return c.formatExpr("$copyString(%e, %e)", args[0], args[1]) } return c.formatExpr("$copySlice(%e, %e)", args[0], args[1]) case "print", "println": return c.formatExpr("console.log(%s)", strings.Join(c.translateExprSlice(args, nil), ", ")) case "complex": argStr := c.translateArgs(sig, args, ellipsis, false) return c.formatExpr("new %s(%s, %s)", c.typeName(sig.Results().At(0).Type()), argStr[0], argStr[1]) case "real": return c.formatExpr("%e.$real", args[0]) case "imag": return c.formatExpr("%e.$imag", args[0]) case "recover": return c.formatExpr("$recover()") case "close": return c.formatExpr(`$close(%e)`, args[0]) default: panic(fmt.Sprintf("Unhandled builtin: %s\n", name)) } }
func changeRecv(s *types.Signature, recv *types.Var) *types.Signature { return types.NewSignature(recv, s.Params(), s.Results(), s.Variadic()) }