MCPcopy
hub / github.com/gopherjs/gopherjs / expandTupleArgs

Method expandTupleArgs

compiler/utils.go:132–149  ·  view source on GitHub ↗

expandTupleArgs converts a function call which argument is a tuple returned by another function into a set of individual call arguments corresponding to tuple elements. For example, for functions defined as: func a() (int, string) {return 42, "foo"} func b(a1 int, a2 string) {} ...the following

(argExprs []ast.Expr)

Source from the content-addressed store, hash-verified

130// _tuple := a()
131// b(_tuple[0], _tuple[1])
132func (fc *funcContext) expandTupleArgs(argExprs []ast.Expr) []ast.Expr {
133 if len(argExprs) != 1 {
134 return argExprs
135 }
136
137 tuple, isTuple := fc.typeOf(argExprs[0]).(*types.Tuple)
138 if !isTuple {
139 return argExprs
140 }
141
142 tupleVar := fc.newLocalVariable("_tuple")
143 fc.Printf("%s = %s;", tupleVar, fc.translateExpr(argExprs[0]))
144 argExprs = make([]ast.Expr, tuple.Len())
145 for i := range argExprs {
146 argExprs[i] = fc.newIdent(fc.formatExpr("%s[%d]", tupleVar, i).String(), tuple.At(i).Type())
147 }
148 return argExprs
149}
150
151func (fc *funcContext) translateArgs(sig *types.Signature, argExprs []ast.Expr, ellipsis bool) []string {
152 argExprs = fc.expandTupleArgs(argExprs)

Callers 2

translateArgsMethod · 0.95
translateBuiltinMethod · 0.95

Calls 10

typeOfMethod · 0.95
newLocalVariableMethod · 0.95
PrintfMethod · 0.95
translateExprMethod · 0.95
newIdentMethod · 0.95
formatExprMethod · 0.95
LenMethod · 0.65
StringMethod · 0.65
TypeMethod · 0.65
AtMethod · 0.65

Tested by

no test coverage detected