Example #1
0
func (c *funcContext) translateArgs(sig *types.Signature, argExprs []ast.Expr, ellipsis, clone bool) []string {
	if len(argExprs) == 1 {
		if tuple, isTuple := c.p.TypeOf(argExprs[0]).(*types.Tuple); isTuple {
			tupleVar := c.newVariable("_tuple")
			c.Printf("%s = %s;", tupleVar, c.translateExpr(argExprs[0]))
			argExprs = make([]ast.Expr, tuple.Len())
			for i := range argExprs {
				argExprs[i] = c.newIdent(c.formatExpr("%s[%d]", tupleVar, i).String(), tuple.At(i).Type())
			}
		}
	}

	paramsLen := sig.Params().Len()

	var varargType *types.Slice
	if sig.Variadic() && !ellipsis {
		varargType = sig.Params().At(paramsLen - 1).Type().(*types.Slice)
	}

	preserveOrder := false
	for i := 1; i < len(argExprs); i++ {
		preserveOrder = preserveOrder || c.Blocking[argExprs[i]]
	}

	args := make([]string, len(argExprs))
	for i, argExpr := range argExprs {
		var argType types.Type
		switch {
		case varargType != nil && i >= paramsLen-1:
			argType = varargType.Elem()
		default:
			argType = sig.Params().At(i).Type()
		}

		var arg string
		switch {
		case clone:
			arg = c.translateImplicitConversionWithCloning(argExpr, argType).String()
		default:
			arg = c.translateImplicitConversion(argExpr, argType).String()
		}

		if preserveOrder && c.p.Types[argExpr].Value == nil {
			argVar := c.newVariable("_arg")
			c.Printf("%s = %s;", argVar, arg)
			arg = argVar
		}

		args[i] = arg
	}

	if varargType != nil {
		return append(args[:paramsLen-1], fmt.Sprintf("new %s([%s])", c.typeName(varargType), strings.Join(args[paramsLen-1:], ", ")))
	}
	return args
}
Example #2
0
func (c *converter) convertSlice(v *gotypes.Slice) *types.Slice {
	if v == nil {
		return nil
	}
	if v, ok := c.converted[v]; ok {
		return v.(*types.Slice)
	}
	ret := types.NewSlice(c.convertType(v.Elem()))
	c.converted[v] = ret
	return ret
}