2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-07-24 08:00:02 +00:00
|
|
|
|
|
|
|
// 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 22:41:57 +00:00
|
|
|
"tailscale.com/util/codegen"
|
2020-07-24 08:00:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
flagTypes = flag.String("type", "", "comma-separated list of types; required")
|
|
|
|
flagBuildTags = flag.String("tags", "", "compiler build tags to apply")
|
2020-10-19 17:46:30 +00:00
|
|
|
flagCloneFunc = flag.Bool("clonefunc", false, "add a top-level Clone func")
|
2020-07-24 08:00:02 +00: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-01 23:15:14 +00:00
|
|
|
pkg, namedTypes, err := codegen.LoadTypes(*flagBuildTags, ".")
|
2020-07-24 08:00:02 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-05-01 23:15:14 +00:00
|
|
|
it := codegen.NewImportTracker(pkg.Types)
|
2020-07-24 08:00:02 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
for _, typeName := range typeNames {
|
2021-09-16 23:24:50 +00:00
|
|
|
typ, ok := namedTypes[typeName]
|
|
|
|
if !ok {
|
2020-07-24 08:00:02 +00:00
|
|
|
log.Fatalf("could not find type %s", typeName)
|
|
|
|
}
|
2022-05-01 23:15:14 +00:00
|
|
|
gen(buf, it, typ)
|
2020-07-24 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 23:27:57 +00:00
|
|
|
w := func(format string, args ...any) {
|
2020-09-04 22:19:12 +00:00
|
|
|
fmt.Fprintf(buf, format+"\n", args...)
|
|
|
|
}
|
2020-10-19 17:46:30 +00: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 17:46:30 +00: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 22:19:12 +00:00
|
|
|
}
|
2022-05-01 23:15:14 +00:00
|
|
|
cloneOutput := pkg.Name + "_clone.go"
|
2023-01-03 04:19:33 +00:00
|
|
|
if err := codegen.WritePackageFile("tailscale.com/cmd/cloner", pkg, cloneOutput, codegen.CopyrightYear("."), it, buf); err != nil {
|
2020-07-24 08:00:02 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-01 23:15:14 +00:00
|
|
|
func gen(buf *bytes.Buffer, it *codegen.ImportTracker, typ *types.Named) {
|
2021-09-16 23:00:34 +00: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-16 23:00:34 +00: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-01 23:15:14 +00:00
|
|
|
if !codegen.ContainsPointers(ft) || codegen.HasNoClone(t.Tag(i)) {
|
2021-09-16 23:00:34 +00:00
|
|
|
continue
|
2020-07-24 08:00:02 +00:00
|
|
|
}
|
2022-02-15 16:19:44 +00:00
|
|
|
if named, _ := ft.(*types.Named); named != nil {
|
2022-05-01 23:15:14 +00: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-16 23:00:34 +00:00
|
|
|
}
|
|
|
|
switch ft := ft.Underlying().(type) {
|
|
|
|
case *types.Slice:
|
2021-09-17 18:29:17 +00:00
|
|
|
if codegen.ContainsPointers(ft.Elem()) {
|
2022-05-01 23:15:14 +00:00
|
|
|
n := it.QualifiedName(ft.Elem())
|
2021-09-16 23:00:34 +00:00
|
|
|
writef("dst.%s = make([]%s, len(src.%s))", fname, n, fname)
|
|
|
|
writef("for i := range dst.%s {", fname)
|
2022-05-01 23:15:20 +00:00
|
|
|
if ptr, isPtr := ft.Elem().(*types.Pointer); isPtr {
|
|
|
|
if _, isBasic := ptr.Elem().Underlying().(*types.Basic); isBasic {
|
|
|
|
writef("\tx := *src.%s[i]", fname)
|
|
|
|
writef("\tdst.%s[i] = &x", fname)
|
|
|
|
} else {
|
|
|
|
writef("\tdst.%s[i] = src.%s[i].Clone()", fname, fname)
|
|
|
|
}
|
2020-07-24 08:00:02 +00:00
|
|
|
} else {
|
2021-09-16 23:00:34 +00:00
|
|
|
writef("\tdst.%s[i] = *src.%s[i].Clone()", fname, fname)
|
2020-07-24 08:00:02 +00:00
|
|
|
}
|
|
|
|
writef("}")
|
2021-09-16 23:00:34 +00:00
|
|
|
} else {
|
|
|
|
writef("dst.%s = append(src.%s[:0:0], src.%s...)", fname, fname, fname)
|
2020-07-24 08:00:02 +00:00
|
|
|
}
|
2021-09-16 23:00:34 +00:00
|
|
|
case *types.Pointer:
|
2021-09-17 18:29:17 +00:00
|
|
|
if named, _ := ft.Elem().(*types.Named); named != nil && codegen.ContainsPointers(ft.Elem()) {
|
2021-09-16 23:00:34 +00:00
|
|
|
writef("dst.%s = src.%s.Clone()", fname, fname)
|
|
|
|
continue
|
|
|
|
}
|
2022-05-01 23:15:14 +00:00
|
|
|
n := it.QualifiedName(ft.Elem())
|
2021-09-16 23:00:34 +00: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 18:29:17 +00:00
|
|
|
if codegen.ContainsPointers(ft.Elem()) {
|
2021-09-16 23:00:34 +00:00
|
|
|
writef("\t" + `panic("TODO pointers in pointers")`)
|
|
|
|
}
|
|
|
|
writef("}")
|
|
|
|
case *types.Map:
|
2022-07-13 06:52:44 +00:00
|
|
|
elem := ft.Elem()
|
2021-09-16 23:00:34 +00:00
|
|
|
writef("if dst.%s != nil {", fname)
|
2022-07-13 06:52:44 +00:00
|
|
|
writef("\tdst.%s = map[%s]%s{}", fname, it.QualifiedName(ft.Key()), it.QualifiedName(elem))
|
|
|
|
if sliceType, isSlice := elem.(*types.Slice); isSlice {
|
2022-05-01 23:15:14 +00:00
|
|
|
n := it.QualifiedName(sliceType.Elem())
|
2021-09-16 23:00:34 +00: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}")
|
2022-07-13 06:52:44 +00:00
|
|
|
} else if codegen.ContainsPointers(elem) {
|
2021-09-16 23:00:34 +00:00
|
|
|
writef("\tfor k, v := range src.%s {", fname)
|
2022-07-13 06:52:44 +00:00
|
|
|
switch elem.(type) {
|
|
|
|
case *types.Pointer:
|
|
|
|
writef("\t\tdst.%s[k] = v.Clone()", fname)
|
|
|
|
default:
|
|
|
|
writef("\t\tv2 := v.Clone()")
|
|
|
|
writef("\t\tdst.%s[k] = *v2", fname)
|
|
|
|
}
|
2021-09-16 23:00:34 +00:00
|
|
|
writef("\t}")
|
|
|
|
} else {
|
|
|
|
writef("\tfor k, v := range src.%s {", fname)
|
|
|
|
writef("\t\tdst.%s[k] = v", fname)
|
|
|
|
writef("\t}")
|
|
|
|
}
|
|
|
|
writef("}")
|
|
|
|
default:
|
2021-09-16 23:31:35 +00:00
|
|
|
writef(`panic("TODO: %s (%T)")`, fname, ft)
|
2020-07-24 08:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-16 23:00:34 +00:00
|
|
|
writef("return dst")
|
|
|
|
fmt.Fprintf(buf, "}\n\n")
|
|
|
|
|
2022-05-01 23:15:14 +00:00
|
|
|
buf.Write(codegen.AssertStructUnchanged(t, name, "Clone", it))
|
2022-02-15 16:19:44 +00:00
|
|
|
}
|
|
|
|
|
2022-05-01 23:15:14 +00:00
|
|
|
// hasBasicUnderlying reports true when typ.Underlying() is a slice or a map.
|
2020-07-24 08:00:02 +00:00
|
|
|
func hasBasicUnderlying(typ types.Type) bool {
|
|
|
|
switch typ.Underlying().(type) {
|
|
|
|
case *types.Slice, *types.Map:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|