aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/noder/helpers.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/compile/internal/noder/helpers.go')
-rw-r--r--src/cmd/compile/internal/noder/helpers.go126
1 files changed, 115 insertions, 11 deletions
diff --git a/src/cmd/compile/internal/noder/helpers.go b/src/cmd/compile/internal/noder/helpers.go
index 1210d4b58c..e4a1a54fe8 100644
--- a/src/cmd/compile/internal/noder/helpers.go
+++ b/src/cmd/compile/internal/noder/helpers.go
@@ -67,16 +67,47 @@ func Assert(pos src.XPos, x ir.Node, typ *types.Type) ir.Node {
return typed(typ, ir.NewTypeAssertExpr(pos, x, nil))
}
-func Binary(pos src.XPos, op ir.Op, x, y ir.Node) ir.Node {
+// transformAdd transforms an addition operation (currently just addition of
+// strings). Equivalent to the "binary operators" case in typecheck.typecheck1.
+func transformAdd(n *ir.BinaryExpr) ir.Node {
+ l := n.X
+ if l.Type().IsString() {
+ var add *ir.AddStringExpr
+ if l.Op() == ir.OADDSTR {
+ add = l.(*ir.AddStringExpr)
+ add.SetPos(n.Pos())
+ } else {
+ add = ir.NewAddStringExpr(n.Pos(), []ir.Node{l})
+ }
+ r := n.Y
+ if r.Op() == ir.OADDSTR {
+ r := r.(*ir.AddStringExpr)
+ add.List.Append(r.List.Take()...)
+ } else {
+ add.List.Append(r)
+ }
+ add.SetType(l.Type())
+ return add
+ }
+ return n
+}
+
+func Binary(pos src.XPos, op ir.Op, typ *types.Type, x, y ir.Node) ir.Node {
switch op {
case ir.OANDAND, ir.OOROR:
return typed(x.Type(), ir.NewLogicalExpr(pos, op, x, y))
case ir.OADD:
- if x.Type().IsString() {
- // TODO(mdempsky): Construct OADDSTR directly.
- return typecheck.Expr(ir.NewBinaryExpr(pos, op, x, y))
+ n := ir.NewBinaryExpr(pos, op, x, y)
+ if x.Type().HasTParam() || y.Type().HasTParam() {
+ // Delay transformAdd() if either arg has a type param,
+ // since it needs to know the exact types to decide whether
+ // to transform OADD to OADDSTR.
+ n.SetType(typ)
+ n.SetTypecheck(3)
+ return n
}
- fallthrough
+ n1 := transformAdd(n)
+ return typed(typ, n1)
default:
return typed(x.Type(), ir.NewBinaryExpr(pos, op, x, y))
}
@@ -178,12 +209,56 @@ func Call(pos src.XPos, typ *types.Type, fun ir.Node, args []ir.Node, dots bool)
return n
}
+// transformCompare transforms a compare operation (currently just equals/not
+// equals). Equivalent to the "comparison operators" case in
+// typecheck.typecheck1, including tcArith.
+func transformCompare(n *ir.BinaryExpr) {
+ if (n.Op() == ir.OEQ || n.Op() == ir.ONE) && !types.Identical(n.X.Type(), n.Y.Type()) {
+ // Comparison is okay as long as one side is assignable to the
+ // other. The only allowed case where the conversion is not CONVNOP is
+ // "concrete == interface". In that case, check comparability of
+ // the concrete type. The conversion allocates, so only do it if
+ // the concrete type is huge.
+ l, r := n.X, n.Y
+ lt, rt := l.Type(), r.Type()
+ converted := false
+ if rt.Kind() != types.TBLANK {
+ aop, _ := typecheck.Assignop(lt, rt)
+ if aop != ir.OXXX {
+ types.CalcSize(lt)
+ if rt.IsInterface() == lt.IsInterface() || lt.Width >= 1<<16 {
+ l = ir.NewConvExpr(base.Pos, aop, rt, l)
+ l.SetTypecheck(1)
+ }
+
+ converted = true
+ }
+ }
+
+ if !converted && lt.Kind() != types.TBLANK {
+ aop, _ := typecheck.Assignop(rt, lt)
+ if aop != ir.OXXX {
+ types.CalcSize(rt)
+ if rt.IsInterface() == lt.IsInterface() || rt.Width >= 1<<16 {
+ r = ir.NewConvExpr(base.Pos, aop, lt, r)
+ r.SetTypecheck(1)
+ }
+ }
+ }
+ n.X, n.Y = l, r
+ }
+}
+
func Compare(pos src.XPos, typ *types.Type, op ir.Op, x, y ir.Node) ir.Node {
n := ir.NewBinaryExpr(pos, op, x, y)
- if !types.Identical(x.Type(), y.Type()) {
- // TODO(mdempsky): Handle subtleties of constructing mixed-typed comparisons.
- n = typecheck.Expr(n).(*ir.BinaryExpr)
+ if x.Type().HasTParam() || y.Type().HasTParam() {
+ // Delay transformCompare() if either arg has a type param, since
+ // it needs to know the exact types to decide on any needed conversions.
+ n.SetType(typ)
+ n.SetTypecheck(3)
+ return n
}
+ transformCompare(n)
return typed(typ, n)
}
@@ -267,13 +342,42 @@ func Index(pos src.XPos, typ *types.Type, x, index ir.Node) ir.Node {
return typecheck.Expr(n)
}
-func Slice(pos src.XPos, x, low, high, max ir.Node) ir.Node {
+// transformSlice transforms a slice operation. Equivalent to typecheck.tcSlice.
+func transformSlice(n *ir.SliceExpr) {
+ l := n.X
+ if l.Type().IsArray() {
+ addr := typecheck.NodAddr(n.X)
+ addr.SetImplicit(true)
+ typed(types.NewPtr(n.X.Type()), addr)
+ n.X = addr
+ l = addr
+ }
+ t := l.Type()
+ if t.IsString() {
+ n.SetOp(ir.OSLICESTR)
+ } else if t.IsPtr() && t.Elem().IsArray() {
+ if n.Op().IsSlice3() {
+ n.SetOp(ir.OSLICE3ARR)
+ } else {
+ n.SetOp(ir.OSLICEARR)
+ }
+ }
+}
+
+func Slice(pos src.XPos, typ *types.Type, x, low, high, max ir.Node) ir.Node {
op := ir.OSLICE
if max != nil {
op = ir.OSLICE3
}
- // TODO(mdempsky): Avoid typecheck.Expr.
- return typecheck.Expr(ir.NewSliceExpr(pos, op, x, low, high, max))
+ n := ir.NewSliceExpr(pos, op, x, low, high, max)
+ if x.Type().HasTParam() {
+ // transformSlice needs to know if x.Type() is a string or an array or a slice.
+ n.SetType(typ)
+ n.SetTypecheck(3)
+ return n
+ }
+ transformSlice(n)
+ return typed(typ, n)
}
func Unary(pos src.XPos, op ir.Op, x ir.Node) ir.Node {