diff options
author | Matthew Dempsky <mdempsky@google.com> | 2021-04-21 02:11:15 -0700 |
---|---|---|
committer | Matthew Dempsky <mdempsky@google.com> | 2021-05-02 20:38:13 +0000 |
commit | fadad851a3222867b374e901ede9c4919594837f (patch) | |
tree | 7b4efe046ea2da139b27ce5091d0cbd058bdf331 /src/cmd/compile/internal/typecheck/func.go | |
parent | 0d32d9e8a8784cf3ef39c471b73e502c51085b6d (diff) | |
download | go-fadad851a3222867b374e901ede9c4919594837f.tar.gz go-fadad851a3222867b374e901ede9c4919594837f.zip |
cmd/compile: implement unsafe.Add and unsafe.Slice
Updates #19367.
Updates #40481.
Change-Id: Iabd2afdd0d520e5d68fd9e6dedd013335a4b3886
Reviewed-on: https://go-review.googlesource.com/c/go/+/312214
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Trust: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/typecheck/func.go')
-rw-r--r-- | src/cmd/compile/internal/typecheck/func.go | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/typecheck/func.go b/src/cmd/compile/internal/typecheck/func.go index eaae2a81fa..e154c39269 100644 --- a/src/cmd/compile/internal/typecheck/func.go +++ b/src/cmd/compile/internal/typecheck/func.go @@ -430,7 +430,7 @@ func tcCall(n *ir.CallExpr, top int) ir.Node { u := ir.NewUnaryExpr(n.Pos(), l.BuiltinOp, arg) return typecheck(ir.InitExpr(n.Init(), u), top) // typecheckargs can add to old.Init - case ir.OCOMPLEX, ir.OCOPY: + case ir.OCOMPLEX, ir.OCOPY, ir.OUNSAFEADD, ir.OUNSAFESLICE: typecheckargs(n) arg1, arg2, ok := needTwoArgs(n) if !ok { @@ -977,3 +977,39 @@ func tcRecover(n *ir.CallExpr) ir.Node { n.SetType(types.Types[types.TINTER]) return n } + +// tcUnsafeAdd typechecks an OUNSAFEADD node. +func tcUnsafeAdd(n *ir.BinaryExpr) *ir.BinaryExpr { + n.X = AssignConv(Expr(n.X), types.Types[types.TUNSAFEPTR], "argument to unsafe.Add") + n.Y = DefaultLit(Expr(n.Y), types.Types[types.TINT]) + if n.X.Type() == nil || n.Y.Type() == nil { + n.SetType(nil) + return n + } + if !n.Y.Type().IsInteger() { + n.SetType(nil) + return n + } + n.SetType(n.X.Type()) + return n +} + +// tcUnsafeSlice typechecks an OUNSAFESLICE node. +func tcUnsafeSlice(n *ir.BinaryExpr) *ir.BinaryExpr { + n.X = Expr(n.X) + n.Y = Expr(n.Y) + if n.X.Type() == nil || n.Y.Type() == nil { + n.SetType(nil) + return n + } + t := n.X.Type() + if !t.IsPtr() { + base.Errorf("first argument to unsafe.Slice must be pointer; have %L", t) + } + if !checkunsafeslice(&n.Y) { + n.SetType(nil) + return n + } + n.SetType(types.NewSlice(t.Elem())) + return n +} |