Skip to content

Commit

Permalink
Preliminary support for 'copy' builtin function in exp/eval
Browse files Browse the repository at this point in the history
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/2157042
  • Loading branch information
sbinet authored and robpike committed Sep 14, 2010
1 parent 18b02f6 commit caf3b4a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/pkg/exp/eval/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,38 @@ func (a *exprInfo) compileBuiltinCallExpr(b *block, ft *FuncType, as []*expr) *e
}
return expr

case copyType:
if !checkCount(2, 2) {
return nil
}
src := as[1]
dst := as[0]
if src.t != dst.t {
a.diag("arguments to built-in function 'copy' must have same type\nsrc: %s\ndst: %s\n", src.t, dst.t)
return nil
}
if _, ok := src.t.lit().(*SliceType); !ok {
a.diag("src argument to 'copy' must be a slice (got: %s)", src.t)
return nil
}
if _, ok := dst.t.lit().(*SliceType); !ok {
a.diag("dst argument to 'copy' must be a slice (got: %s)", dst.t)
return nil
}
expr := a.newExpr(IntType, "function call")
srcf := src.asSlice()
dstf := dst.asSlice()
expr.eval = func(t *Thread) int64 {
src, dst := srcf(t), dstf(t)
nelems := src.Len
if nelems > dst.Len {
nelems = dst.Len
}
dst.Base.Sub(0, nelems).Assign(t, src.Base.Sub(0, nelems))
return nelems
}
return expr

case lenType:
if !checkCount(1, 1) {
return nil
Expand Down
2 changes: 2 additions & 0 deletions src/pkg/exp/eval/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ var (
panicType = &FuncType{builtin: "panic"}
printType = &FuncType{builtin: "print"}
printlnType = &FuncType{builtin: "println"}
copyType = &FuncType{builtin: "copy"}
)

// Two function types are identical if they have the same number of
Expand Down Expand Up @@ -1249,6 +1250,7 @@ func init() {
universe.DefineConst("cap", universePos, capType, nil)
universe.DefineConst("close", universePos, closeType, nil)
universe.DefineConst("closed", universePos, closedType, nil)
universe.DefineConst("copy", universePos, copyType, nil)
universe.DefineConst("len", universePos, lenType, nil)
universe.DefineConst("make", universePos, makeType, nil)
universe.DefineConst("new", universePos, newType, nil)
Expand Down

0 comments on commit caf3b4a

Please sign in to comment.