@@ -1049,26 +1049,16 @@ func (fc *funcContext) translateBuiltin(name string, sig *types.Signature, args
1049
1049
return fc .formatExpr ("$newDataPointer(%e, %s)" , fc .zeroValue (t .Elem ()), fc .typeName (t ))
1050
1050
}
1051
1051
case "make" :
1052
- switch argType := fc .pkgCtx .TypeOf (args [0 ]).Underlying ().(type ) {
1053
- case * types.Slice :
1054
- t := fc .typeName (fc .pkgCtx .TypeOf (args [0 ]))
1055
- if len (args ) == 3 {
1056
- return fc .formatExpr ("$makeSlice(%s, %f, %f)" , t , args [1 ], args [2 ])
1057
- }
1058
- return fc .formatExpr ("$makeSlice(%s, %f)" , t , args [1 ])
1059
- case * types.Map :
1060
- if len (args ) == 2 && fc .pkgCtx .Types [args [1 ]].Value == nil {
1061
- return fc .formatExpr (`((%1f < 0 || %1f > 2147483647) ? $throwRuntimeError("makemap: size out of range") : new $global.Map())` , args [1 ])
1062
- }
1063
- return fc .formatExpr ("new $global.Map()" )
1064
- case * types.Chan :
1065
- length := "0"
1066
- if len (args ) == 2 {
1067
- length = fc .formatExpr ("%f" , args [1 ]).String ()
1068
- }
1069
- return fc .formatExpr ("new $Chan(%s, %s)" , fc .typeName (fc .pkgCtx .TypeOf (args [0 ]).Underlying ().(* types.Chan ).Elem ()), length )
1052
+ typeName := fc .typeName (fc .pkgCtx .TypeOf (args [0 ]))
1053
+ switch len (args ) {
1054
+ case 1 :
1055
+ return fc .formatExpr ("%s.$make()" , typeName )
1056
+ case 2 :
1057
+ return fc .formatExpr ("%s.$make(%f)" , typeName , args [1 ])
1058
+ case 3 :
1059
+ return fc .formatExpr ("%s.$make(%f, %f)" , typeName , args [1 ], args [2 ])
1070
1060
default :
1071
- panic (fmt .Sprintf ( "Unhandled make type : %T \n " , argType ))
1061
+ panic (fmt .Errorf ( "builtin make(): invalid number of arguments : %d " , len ( args ) ))
1072
1062
}
1073
1063
case "len" :
1074
1064
switch argType := fc .pkgCtx .TypeOf (args [0 ]).Underlying ().(type ) {
@@ -1082,6 +1072,8 @@ func (fc *funcContext) translateBuiltin(name string, sig *types.Signature, args
1082
1072
return fc .formatExpr ("(%e ? %e.size : 0)" , args [0 ], args [0 ])
1083
1073
case * types.Chan :
1084
1074
return fc .formatExpr ("%e.$buffer.length" , args [0 ])
1075
+ case * types.Interface : // *types.TypeParam has interface as underlying type.
1076
+ return fc .formatExpr ("%s.$len(%e)" , fc .typeName (fc .pkgCtx .TypeOf (args [0 ])), args [0 ])
1085
1077
// length of array is constant
1086
1078
default :
1087
1079
panic (fmt .Sprintf ("Unhandled len type: %T\n " , argType ))
@@ -1092,9 +1084,13 @@ func (fc *funcContext) translateBuiltin(name string, sig *types.Signature, args
1092
1084
return fc .formatExpr ("%e.$capacity" , args [0 ])
1093
1085
case * types.Pointer :
1094
1086
return fc .formatExpr ("(%e, %d)" , args [0 ], argType .Elem ().(* types.Array ).Len ())
1095
- // capacity of array is constant
1087
+ case * types.Array :
1088
+ // This should never happen™
1089
+ panic (fmt .Errorf ("array capacity should have been inlined as constant" ))
1090
+ case * types.Interface : // *types.TypeParam has interface as underlying type.
1091
+ return fc .formatExpr ("%s.$cap(%e)" , fc .typeName (fc .pkgCtx .TypeOf (args [0 ])), args [0 ])
1096
1092
default :
1097
- panic (fmt .Sprintf ( "Unhandled cap type: %T\n " , argType ))
1093
+ panic (fmt .Errorf ( "unhandled cap type: %T" , argType ))
1098
1094
}
1099
1095
case "panic" :
1100
1096
return fc .formatExpr ("$panic(%s)" , fc .translateImplicitConversion (args [0 ], types .NewInterface (nil , nil )))
@@ -1103,11 +1099,11 @@ func (fc *funcContext) translateBuiltin(name string, sig *types.Signature, args
1103
1099
argStr := fc .translateArgs (sig , args , ellipsis )
1104
1100
return fc .formatExpr ("$appendSlice(%s, %s)" , argStr [0 ], argStr [1 ])
1105
1101
}
1106
- sliceType := sig .Results ().At (0 ).Type ().Underlying ().( * types.Slice )
1107
- return fc .formatExpr ("$append(%e, %s)" , args [0 ], strings .Join (fc .translateExprSlice (args [1 :], sliceType . Elem () ), ", " ))
1102
+ elType := sig .Params ().At (1 ).Type ().( * types.Slice ). Elem ( )
1103
+ return fc .formatExpr ("$append(%e, %s)" , args [0 ], strings .Join (fc .translateExprSlice (args [1 :], elType ), ", " ))
1108
1104
case "delete" :
1109
1105
args = fc .expandTupleArgs (args )
1110
- keyType := fc . pkgCtx . TypeOf ( args [ 0 ]). Underlying ().( * types. Map ). Key ()
1106
+ keyType := sig . Params (). At ( 1 ). Type ()
1111
1107
return fc .formatExpr (
1112
1108
`$mapDelete(%1e, %2s.keyFor(%3s))` ,
1113
1109
args [0 ],
@@ -1116,10 +1112,8 @@ func (fc *funcContext) translateBuiltin(name string, sig *types.Signature, args
1116
1112
)
1117
1113
case "copy" :
1118
1114
args = fc .expandTupleArgs (args )
1119
- if basic , isBasic := fc .pkgCtx .TypeOf (args [1 ]).Underlying ().(* types.Basic ); isBasic && isString (basic ) {
1120
- return fc .formatExpr ("$copyString(%e, %e)" , args [0 ], args [1 ])
1121
- }
1122
- return fc .formatExpr ("$copySlice(%e, %e)" , args [0 ], args [1 ])
1115
+ dst , src := args [0 ], args [1 ]
1116
+ return fc .formatExpr ("%s.$copy(%e, %e)" , fc .typeName (fc .pkgCtx .TypeOf (src )), dst , src )
1123
1117
case "print" :
1124
1118
args = fc .expandTupleArgs (args )
1125
1119
return fc .formatExpr ("$print(%s)" , strings .Join (fc .translateExprSlice (args , nil ), ", " ))
@@ -1573,11 +1567,16 @@ func (fc *funcContext) formatExprInternal(format string, a []interface{}, parens
1573
1567
out .WriteString (strconv .FormatInt (d , 10 ))
1574
1568
return
1575
1569
}
1576
- if is64Bit ( fc .pkgCtx .TypeOf (e ).Underlying ().(* types.Basic )) {
1570
+ if t , ok := fc .pkgCtx .TypeOf (e ).Underlying ().(* types.Basic ); ok && is64Bit ( t ) {
1577
1571
out .WriteString ("$flatten64(" )
1578
1572
writeExpr ("" )
1579
1573
out .WriteString (")" )
1580
1574
return
1575
+ } else if t , ok := fc .pkgCtx .TypeOf (e ).(* types.TypeParam ); ok {
1576
+ out .WriteString ("$flatten64(" )
1577
+ writeExpr ("" )
1578
+ fmt .Fprintf (out , ", %s)" , fc .typeName (t ))
1579
+ return
1581
1580
}
1582
1581
writeExpr ("" )
1583
1582
case 'h' :
0 commit comments