func (c *compiler) evalCallArgs(ftype *types.Signature, args []ast.Expr) []Value { var argValues []Value if len(args) == 0 { return argValues } arg0 := args[0] if _, ok := c.types.expr[arg0].Type.(*types.Tuple); ok { // f(g(...)), where g is multi-value return argValues = c.destructureExpr(args[0]) } else { argValues = make([]Value, len(args)) for i, x := range args { var paramtyp types.Type params := ftype.Params() if ftype.IsVariadic() && i >= int(params.Len()-1) { paramtyp = params.At(int(params.Len() - 1)).Type() } else { paramtyp = params.At(i).Type() } c.convertUntyped(x, paramtyp) argValues[i] = c.VisitExpr(x) } } return argValues }
func (tm *LLVMTypeMap) funcLLVMType(tstr string, f *types.Signature) llvm.Type { typ, ok := tm.types[tstr] if !ok { // If there's a receiver change the receiver to an // additional (first) parameter, and take the value of // the resulting signature instead. var param_types []llvm.Type if recv := f.Recv(); recv != nil { params := f.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...) f := types.NewSignature(nil, params, f.Results(), f.IsVariadic()) return tm.ToLLVM(f) } typ = llvm.GlobalContext().StructCreateNamed("") tm.types[tstr] = typ params := f.Params() nparams := int(params.Len()) for i := 0; i < nparams; i++ { typ := params.At(i).Type() if f.IsVariadic() && i == nparams-1 { typ = types.NewSlice(typ) } llvmtyp := tm.ToLLVM(typ) param_types = append(param_types, llvmtyp) } var return_type llvm.Type results := f.Results() switch nresults := int(results.Len()); nresults { case 0: return_type = llvm.VoidType() case 1: return_type = tm.ToLLVM(results.At(0).Type()) default: elements := make([]llvm.Type, nresults) for i := range elements { result := results.At(i) elements[i] = tm.ToLLVM(result.Type()) } return_type = llvm.StructType(elements, false) } fntyp := llvm.FunctionType(return_type, param_types, false) fnptrtyp := llvm.PointerType(fntyp, 0) i8ptr := llvm.PointerType(llvm.Int8Type(), 0) elements := []llvm.Type{fnptrtyp, i8ptr} // func, closure typ.StructSetBody(elements, false) } return typ }
// changeRecv returns sig with Recv prepended to Params(). func changeRecv(sig *types.Signature) *types.Signature { params := sig.Params() n := params.Len() p2 := make([]*types.Var, n+1) p2[0] = sig.Recv() for i := 0; i < n; i++ { p2[i+1] = params.At(i) } return types.NewSignature(nil, nil, types.NewTuple(p2...), sig.Results(), sig.IsVariadic()) }
func (tm *llvmTypeMap) funcLLVMType(f *types.Signature, name string) llvm.Type { // If there's a receiver change the receiver to an // additional (first) parameter, and take the value of // the resulting signature instead. if recv := f.Recv(); recv != nil { params := f.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...) f := types.NewSignature(nil, nil, params, f.Results(), f.IsVariadic()) return tm.toLLVM(f, name) } if typ, ok := tm.types.At(f).(llvm.Type); ok { return typ } typ := llvm.GlobalContext().StructCreateNamed(name) tm.types.Set(f, typ) params := f.Params() param_types := make([]llvm.Type, params.Len()) for i := range param_types { llvmtyp := tm.ToLLVM(params.At(i).Type()) param_types[i] = llvmtyp } var return_type llvm.Type results := f.Results() switch nresults := int(results.Len()); nresults { case 0: return_type = llvm.VoidType() case 1: return_type = tm.ToLLVM(results.At(0).Type()) default: elements := make([]llvm.Type, nresults) for i := range elements { result := results.At(i) elements[i] = tm.ToLLVM(result.Type()) } return_type = llvm.StructType(elements, false) } fntyp := llvm.FunctionType(return_type, param_types, false) fnptrtyp := llvm.PointerType(fntyp, 0) i8ptr := llvm.PointerType(llvm.Int8Type(), 0) elements := []llvm.Type{fnptrtyp, i8ptr} // func, closure typ.StructSetBody(elements, false) return typ }
func (w *Walker) writeSignature(buf *bytes.Buffer, sig *types.Signature) { w.writeParams(buf, sig.Params(), sig.IsVariadic()) switch res := sig.Results(); res.Len() { case 0: // nothing to do case 1: buf.WriteByte(' ') w.writeType(buf, res.At(0).Type()) default: buf.WriteByte(' ') w.writeParams(buf, res, false) } }
func (tm *TypeMap) funcRuntimeType(f *types.Signature) (global, ptr llvm.Value) { rtype := tm.makeRtype(f, reflect.Func) funcType := llvm.ConstNull(tm.runtimeFuncType) funcType = llvm.ConstInsertValue(funcType, rtype, []uint32{0}) // dotdotdot if f.IsVariadic() { variadic := llvm.ConstInt(llvm.Int1Type(), 1, false) funcType = llvm.ConstInsertValue(funcType, variadic, []uint32{1}) } // TODO in //funcType = llvm.ConstInsertValue(funcType, tm.ToRuntime(p.Elt()), []uint32{2}) // TODO out //funcType = llvm.ConstInsertValue(funcType, tm.ToRuntime(p.Elt()), []uint32{3}) return tm.makeRuntimeTypeGlobal(funcType) }
func (c *PkgContext) translateArgs(sig *types.Signature, args []ast.Expr, ellipsis bool) string { params := make([]string, sig.Params().Len()) for i := range params { if sig.IsVariadic() && i == len(params)-1 && !ellipsis { varargType := sig.Params().At(i).Type().(*types.Slice) varargs := make([]string, len(args)-i) for j, arg := range args[i:] { varargs[j] = c.translateExprToType(arg, varargType.Elem()) } params[i] = fmt.Sprintf("new %s(%s)", c.typeName(varargType), createListComposite(varargType.Elem(), varargs)) break } argType := sig.Params().At(i).Type() params[i] = c.translateExprToType(args[i], argType) } return strings.Join(params, ", ") }
func (tm *TypeMap) funcRuntimeType(tstr string, f *types.Signature) (global, ptr llvm.Value) { rtype := tm.makeRtype(f, reflect.Func) funcType := llvm.ConstNull(tm.runtimeFuncType) global, ptr = tm.makeRuntimeTypeGlobal(funcType) tm.types.record(tstr, global, ptr) funcType = llvm.ConstInsertValue(funcType, rtype, []uint32{0}) // dotdotdot if f.IsVariadic() { variadic := llvm.ConstInt(llvm.Int1Type(), 1, false) funcType = llvm.ConstInsertValue(funcType, variadic, []uint32{1}) } // in intypes := tm.rtypeSlice(f.Params()) funcType = llvm.ConstInsertValue(funcType, intypes, []uint32{2}) // out outtypes := tm.rtypeSlice(f.Results()) funcType = llvm.ConstInsertValue(funcType, outtypes, []uint32{3}) global.SetInitializer(funcType) return global, ptr }
func (c *compiler) evalCallArgs(ftype *types.Signature, args []ast.Expr, dotdotdot bool) []Value { var argValues []Value if len(args) == 0 { return argValues } var argtypes []types.Type if t, ok := c.typeinfo.Types[args[0]].(*types.Tuple); ok { argtypes = make([]types.Type, t.Len()) } else { argtypes = make([]types.Type, len(args)) } params := ftype.Params() for i := range argtypes { if ftype.IsVariadic() && i >= int(params.Len()-1) { argtypes[i] = params.At(int(params.Len() - 1)).Type() argtypes[i] = argtypes[i].(*types.Slice).Elem() } else { argtypes[i] = params.At(i).Type() } } if len(argtypes) > len(args) { // f(g(...)), where g is multi-value return argValues = c.destructureExpr(args[0]) } else { argValues = make([]Value, len(args)) for i, x := range args { c.convertUntyped(x, argtypes[i]) argValues[i] = c.VisitExpr(x) } } for i, v := range argValues { if !dotdotdot || i < int(params.Len()-1) { argValues[i] = v.Convert(argtypes[i]) } } return argValues }
func (ts *TypeStringer) writeSignature(buf *bytes.Buffer, sig *types.Signature) { if recv := sig.Recv(); recv != nil { ts.writeType(buf, recv.Type()) buf.WriteByte(' ') } ts.writeParams(buf, sig.Params(), sig.IsVariadic()) if sig.Results().Len() == 0 { // no result return } buf.WriteByte(' ') if sig.Results().Len() == 1 { // single unnamed result ts.writeType(buf, sig.Results().At(0).Type()) return } // multiple or named result(s) ts.writeParams(buf, sig.Results(), false) }
// 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 recv := sig.Recv(); 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, v.Type().Underlying().(*types.Slice).Elem().String()) } else { io.WriteString(w, v.Type().String()) } } io.WriteString(w, ")") if n := sig.Results().Len(); n > 0 { io.WriteString(w, " ") r := sig.Results() if n == 1 && r.At(0).Name() == "" { io.WriteString(w, r.At(0).Type().String()) } else { io.WriteString(w, r.String()) } } }
func (p *exporter) signature(sig *types.Signature) { // TODO(gri) We only need to record the receiver type // for interface methods if we flatten them // out. If we track embedded types instead, // the information is already present. // We do need the receiver information (T vs *T) // for methods associated with named types. if recv := sig.Recv(); recv != nil { // 1-element tuple p.int(1) p.param(recv) } else { // 0-element tuple p.int(0) } p.tuple(sig.Params()) p.tuple(sig.Results()) if sig.IsVariadic() { p.int(1) } else { p.int(0) } }
func (ts *TypeStringer) writeSignature(buf *bytes.Buffer, sig *types.Signature, unique bool) { if recv := sig.Recv(); recv != nil { if _, ok := recv.Type().Underlying().(*types.Interface); !ok { ts.writeType(buf, recv.Type(), unique) buf.WriteByte(' ') } } ts.writeParams(buf, sig.Params(), sig.IsVariadic(), unique) if sig.Results().Len() == 0 { // no result return } buf.WriteByte(' ') if sig.Results().Len() == 1 { // single unnamed result ts.writeType(buf, sig.Results().At(0).Type(), unique) return } // multiple or named result(s) ts.writeParams(buf, sig.Results(), false, unique) }
func changeRecv(s *types.Signature, recv *types.Var) *types.Signature { return types.NewSignature(nil, recv, s.Params(), s.Results(), s.IsVariadic()) }