2020-07-24 09:00:02 +01:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Cloner is a tool to automate the creation of a Clone method.
|
|
|
|
//
|
|
|
|
// The result of the Clone method aliases no memory that can be edited
|
|
|
|
// with the original.
|
|
|
|
//
|
|
|
|
// This tool makes lots of implicit assumptions about the types you feed it.
|
|
|
|
// In particular, it can only write relatively "shallow" Clone methods.
|
|
|
|
// That is, if a type contains another named struct type, cloner assumes that
|
|
|
|
// named type will also have a Clone method.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"go/types"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2021-09-16 23:41:57 +01:00
|
|
|
"tailscale.com/util/codegen"
|
2020-07-24 09:00:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
flagTypes = flag.String("type", "", "comma-separated list of types; required")
|
|
|
|
flagBuildTags = flag.String("tags", "", "compiler build tags to apply")
|
2020-10-19 18:46:30 +01:00
|
|
|
flagCloneFunc = flag.Bool("clonefunc", false, "add a top-level Clone func")
|
2020-07-24 09:00:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
log.SetFlags(0)
|
|
|
|
log.SetPrefix("cloner: ")
|
|
|
|
flag.Parse()
|
|
|
|
if len(*flagTypes) == 0 {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
typeNames := strings.Split(*flagTypes, ",")
|
|
|
|
|
2022-05-02 00:15:14 +01:00
|
|
|
pkg, namedTypes, err := codegen.LoadTypes(*flagBuildTags, ".")
|
2020-07-24 09:00:02 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-05-02 00:15:14 +01:00
|
|
|
it := codegen.NewImportTracker(pkg.Types)
|
2020-07-24 09:00:02 +01:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
for _, typeName := range typeNames {
|
2021-09-17 00:24:50 +01:00
|
|
|
typ, ok := namedTypes[typeName]
|
|
|
|
if !ok {
|
2020-07-24 09:00:02 +01:00
|
|
|
log.Fatalf("could not find type %s", typeName)
|
|
|
|
}
|
2022-05-02 00:15:14 +01:00
|
|
|
gen(buf, it, typ)
|
2020-07-24 09:00:02 +01:00
|
|
|
}
|
|
|
|
|
2022-03-16 23:27:57 +00:00
|
|
|
w := func(format string, args ...any) {
|
2020-09-04 23:19:12 +01:00
|
|
|
fmt.Fprintf(buf, format+"\n", args...)
|
|
|
|
}
|
2020-10-19 18:46:30 +01:00
|
|
|
if *flagCloneFunc {
|
|
|
|
w("// Clone duplicates src into dst and reports whether it succeeded.")
|
|
|
|
w("// To succeed, <src, dst> must be of types <*T, *T> or <*T, **T>,")
|
|
|
|
w("// where T is one of %s.", *flagTypes)
|
2022-03-16 23:27:57 +00:00
|
|
|
w("func Clone(dst, src any) bool {")
|
2020-10-19 18:46:30 +01:00
|
|
|
w(" switch src := src.(type) {")
|
|
|
|
for _, typeName := range typeNames {
|
|
|
|
w(" case *%s:", typeName)
|
|
|
|
w(" switch dst := dst.(type) {")
|
|
|
|
w(" case *%s:", typeName)
|
|
|
|
w(" *dst = *src.Clone()")
|
|
|
|
w(" return true")
|
|
|
|
w(" case **%s:", typeName)
|
|
|
|
w(" *dst = src.Clone()")
|
|
|
|
w(" return true")
|
|
|
|
w(" }")
|
|
|
|
}
|
|
|
|
w(" }")
|
|
|
|
w(" return false")
|
|
|
|
w("}")
|
2020-09-04 23:19:12 +01:00
|
|
|
}
|
2022-05-02 00:15:14 +01:00
|
|
|
cloneOutput := pkg.Name + "_clone.go"
|
|
|
|
if err := codegen.WritePackageFile("tailscale.com/cmd/cloner", pkg, cloneOutput, it, buf); err != nil {
|
2020-07-24 09:00:02 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 00:15:14 +01:00
|
|
|
func gen(buf *bytes.Buffer, it *codegen.ImportTracker, typ *types.Named) {
|
2021-09-17 00:00:34 +01:00
|
|
|
t, ok := typ.Underlying().(*types.Struct)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
name := typ.Obj().Name()
|
|
|
|
fmt.Fprintf(buf, "// Clone makes a deep copy of %s.\n", name)
|
|
|
|
fmt.Fprintf(buf, "// The result aliases no memory with the original.\n")
|
|
|
|
fmt.Fprintf(buf, "func (src *%s) Clone() *%s {\n", name, name)
|
2022-03-16 23:27:57 +00:00
|
|
|
writef := func(format string, args ...any) {
|
2021-09-17 00:00:34 +01:00
|
|
|
fmt.Fprintf(buf, "\t"+format+"\n", args...)
|
|
|
|
}
|
|
|
|
writef("if src == nil {")
|
|
|
|
writef("\treturn nil")
|
|
|
|
writef("}")
|
|
|
|
writef("dst := new(%s)", name)
|
|
|
|
writef("*dst = *src")
|
|
|
|
for i := 0; i < t.NumFields(); i++ {
|
|
|
|
fname := t.Field(i).Name()
|
|
|
|
ft := t.Field(i).Type()
|
2022-05-02 00:15:14 +01:00
|
|
|
if !codegen.ContainsPointers(ft) || codegen.HasNoClone(t.Tag(i)) {
|
2021-09-17 00:00:34 +01:00
|
|
|
continue
|
2020-07-24 09:00:02 +01:00
|
|
|
}
|
2022-02-15 16:19:44 +00:00
|
|
|
if named, _ := ft.(*types.Named); named != nil {
|
2022-05-02 00:15:14 +01:00
|
|
|
if codegen.IsViewType(ft) {
|
2022-02-15 16:19:44 +00:00
|
|
|
writef("dst.%s = src.%s", fname, fname)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !hasBasicUnderlying(ft) {
|
|
|
|
writef("dst.%s = *src.%s.Clone()", fname, fname)
|
|
|
|
continue
|
|
|
|
}
|
2021-09-17 00:00:34 +01:00
|
|
|
}
|
|
|
|
switch ft := ft.Underlying().(type) {
|
|
|
|
case *types.Slice:
|
2021-09-17 19:29:17 +01:00
|
|
|
if codegen.ContainsPointers(ft.Elem()) {
|
2022-05-02 00:15:14 +01:00
|
|
|
n := it.QualifiedName(ft.Elem())
|
2021-09-17 00:00:34 +01:00
|
|
|
writef("dst.%s = make([]%s, len(src.%s))", fname, n, fname)
|
|
|
|
writef("for i := range dst.%s {", fname)
|
|
|
|
if _, isPtr := ft.Elem().(*types.Pointer); isPtr {
|
|
|
|
writef("\tdst.%s[i] = src.%s[i].Clone()", fname, fname)
|
2020-07-24 09:00:02 +01:00
|
|
|
} else {
|
2021-09-17 00:00:34 +01:00
|
|
|
writef("\tdst.%s[i] = *src.%s[i].Clone()", fname, fname)
|
2020-07-24 09:00:02 +01:00
|
|
|
}
|
|
|
|
writef("}")
|
2021-09-17 00:00:34 +01:00
|
|
|
} else {
|
|
|
|
writef("dst.%s = append(src.%s[:0:0], src.%s...)", fname, fname, fname)
|
2020-07-24 09:00:02 +01:00
|
|
|
}
|
2021-09-17 00:00:34 +01:00
|
|
|
case *types.Pointer:
|
2021-09-17 19:29:17 +01:00
|
|
|
if named, _ := ft.Elem().(*types.Named); named != nil && codegen.ContainsPointers(ft.Elem()) {
|
2021-09-17 00:00:34 +01:00
|
|
|
writef("dst.%s = src.%s.Clone()", fname, fname)
|
|
|
|
continue
|
|
|
|
}
|
2022-05-02 00:15:14 +01:00
|
|
|
n := it.QualifiedName(ft.Elem())
|
2021-09-17 00:00:34 +01:00
|
|
|
writef("if dst.%s != nil {", fname)
|
|
|
|
writef("\tdst.%s = new(%s)", fname, n)
|
|
|
|
writef("\t*dst.%s = *src.%s", fname, fname)
|
2021-09-17 19:29:17 +01:00
|
|
|
if codegen.ContainsPointers(ft.Elem()) {
|
2021-09-17 00:00:34 +01:00
|
|
|
writef("\t" + `panic("TODO pointers in pointers")`)
|
|
|
|
}
|
|
|
|
writef("}")
|
|
|
|
case *types.Map:
|
|
|
|
writef("if dst.%s != nil {", fname)
|
2022-05-02 00:15:14 +01:00
|
|
|
writef("\tdst.%s = map[%s]%s{}", fname, it.QualifiedName(ft.Key()), it.QualifiedName(ft.Elem()))
|
2021-09-17 00:00:34 +01:00
|
|
|
if sliceType, isSlice := ft.Elem().(*types.Slice); isSlice {
|
2022-05-02 00:15:14 +01:00
|
|
|
n := it.QualifiedName(sliceType.Elem())
|
2021-09-17 00:00:34 +01:00
|
|
|
writef("\tfor k := range src.%s {", fname)
|
|
|
|
// use zero-length slice instead of nil to ensure
|
|
|
|
// the key is always copied.
|
|
|
|
writef("\t\tdst.%s[k] = append([]%s{}, src.%s[k]...)", fname, n, fname)
|
|
|
|
writef("\t}")
|
2021-09-17 19:29:17 +01:00
|
|
|
} else if codegen.ContainsPointers(ft.Elem()) {
|
2021-09-17 00:00:34 +01:00
|
|
|
writef("\tfor k, v := range src.%s {", fname)
|
|
|
|
writef("\t\tdst.%s[k] = v.Clone()", fname)
|
|
|
|
writef("\t}")
|
|
|
|
} else {
|
|
|
|
writef("\tfor k, v := range src.%s {", fname)
|
|
|
|
writef("\t\tdst.%s[k] = v", fname)
|
|
|
|
writef("\t}")
|
|
|
|
}
|
|
|
|
writef("}")
|
|
|
|
default:
|
2021-09-17 00:31:35 +01:00
|
|
|
writef(`panic("TODO: %s (%T)")`, fname, ft)
|
2020-07-24 09:00:02 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-17 00:00:34 +01:00
|
|
|
writef("return dst")
|
|
|
|
fmt.Fprintf(buf, "}\n\n")
|
|
|
|
|
2022-05-02 00:15:14 +01:00
|
|
|
buf.Write(codegen.AssertStructUnchanged(t, name, "Clone", it))
|
2022-02-15 16:19:44 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 00:15:14 +01:00
|
|
|
// hasBasicUnderlying reports true when typ.Underlying() is a slice or a map.
|
2020-07-24 09:00:02 +01:00
|
|
|
func hasBasicUnderlying(typ types.Type) bool {
|
|
|
|
switch typ.Underlying().(type) {
|
|
|
|
case *types.Slice, *types.Map:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|