2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-09-16 22:41:57 +00:00
|
|
|
|
|
|
|
// Package codegen contains shared utilities for generating code.
|
|
|
|
package codegen
|
|
|
|
|
|
|
|
import (
|
2021-09-16 22:48:40 +00:00
|
|
|
"bytes"
|
2023-03-27 22:33:42 +00:00
|
|
|
"flag"
|
2021-09-16 22:41:57 +00:00
|
|
|
"fmt"
|
2021-09-16 23:24:50 +00:00
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2021-09-16 22:48:40 +00:00
|
|
|
"go/types"
|
2022-05-01 23:15:14 +00:00
|
|
|
"io"
|
2021-09-16 22:41:57 +00:00
|
|
|
"os"
|
2022-05-01 23:15:14 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2021-09-16 23:24:50 +00:00
|
|
|
|
|
|
|
"golang.org/x/tools/go/packages"
|
2022-04-29 20:30:54 +00:00
|
|
|
"golang.org/x/tools/imports"
|
2022-05-01 23:15:14 +00:00
|
|
|
"tailscale.com/util/mak"
|
2021-09-16 22:41:57 +00:00
|
|
|
)
|
|
|
|
|
2023-03-27 22:33:42 +00:00
|
|
|
var flagCopyright = flag.Bool("copyright", true, "add Tailscale copyright to generated file headers")
|
|
|
|
|
2022-05-01 23:15:14 +00:00
|
|
|
// LoadTypes returns all named types in pkgName, keyed by their type name.
|
|
|
|
func LoadTypes(buildTags string, pkgName string) (*packages.Package, map[string]*types.Named, error) {
|
|
|
|
cfg := &packages.Config{
|
|
|
|
Mode: packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax | packages.NeedName,
|
2024-07-08 15:11:00 +00:00
|
|
|
Tests: buildTags == "test",
|
2022-05-01 23:15:14 +00:00
|
|
|
}
|
2024-07-08 15:11:00 +00:00
|
|
|
if buildTags != "" && !cfg.Tests {
|
2022-05-01 23:15:14 +00:00
|
|
|
cfg.BuildFlags = []string{"-tags=" + buildTags}
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgs, err := packages.Load(cfg, pkgName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2024-07-08 15:11:00 +00:00
|
|
|
if cfg.Tests {
|
|
|
|
pkgs = testPackages(pkgs)
|
|
|
|
}
|
2022-05-01 23:15:14 +00:00
|
|
|
if len(pkgs) != 1 {
|
|
|
|
return nil, nil, fmt.Errorf("wrong number of packages: %d", len(pkgs))
|
|
|
|
}
|
|
|
|
pkg := pkgs[0]
|
|
|
|
return pkg, namedTypes(pkg), nil
|
|
|
|
}
|
|
|
|
|
2024-07-08 15:11:00 +00:00
|
|
|
func testPackages(pkgs []*packages.Package) []*packages.Package {
|
|
|
|
var testPackages []*packages.Package
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
testPackageID := fmt.Sprintf("%[1]s [%[1]s.test]", pkg.PkgPath)
|
|
|
|
if pkg.ID == testPackageID {
|
|
|
|
testPackages = append(testPackages, pkg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return testPackages
|
|
|
|
}
|
|
|
|
|
2022-05-01 23:15:14 +00:00
|
|
|
// HasNoClone reports whether the provided tag has `codegen:noclone`.
|
|
|
|
func HasNoClone(structTag string) bool {
|
|
|
|
val := reflect.StructTag(structTag).Get("codegen")
|
|
|
|
for _, v := range strings.Split(val, ",") {
|
|
|
|
if v == "noclone" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-27 21:36:46 +00:00
|
|
|
const copyrightHeader = `// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-05-01 23:15:14 +00:00
|
|
|
|
2023-01-03 18:59:00 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
const genAndPackageHeader = `// Code generated by %v; DO NOT EDIT.
|
2022-05-01 23:15:14 +00:00
|
|
|
|
|
|
|
package %s
|
|
|
|
`
|
|
|
|
|
|
|
|
func NewImportTracker(thisPkg *types.Package) *ImportTracker {
|
|
|
|
return &ImportTracker{
|
|
|
|
thisPkg: thisPkg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImportTracker provides a mechanism to track and build import paths.
|
|
|
|
type ImportTracker struct {
|
|
|
|
thisPkg *types.Package
|
|
|
|
packages map[string]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *ImportTracker) Import(pkg string) {
|
|
|
|
if pkg != "" && !it.packages[pkg] {
|
|
|
|
mak.Set(&it.packages, pkg, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *ImportTracker) qualifier(pkg *types.Package) string {
|
|
|
|
if it.thisPkg == pkg {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
it.Import(pkg.Path())
|
|
|
|
// TODO(maisem): handle conflicts?
|
|
|
|
return pkg.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
// QualifiedName returns the string representation of t in the package.
|
|
|
|
func (it *ImportTracker) QualifiedName(t types.Type) string {
|
|
|
|
return types.TypeString(t, it.qualifier)
|
|
|
|
}
|
|
|
|
|
2024-07-14 16:45:55 +00:00
|
|
|
// PackagePrefix returns the prefix to be used when referencing named objects from pkg.
|
|
|
|
func (it *ImportTracker) PackagePrefix(pkg *types.Package) string {
|
|
|
|
if s := it.qualifier(pkg); s != "" {
|
|
|
|
return s + "."
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-05-01 23:15:14 +00:00
|
|
|
// Write prints all the tracked imports in a single import block to w.
|
|
|
|
func (it *ImportTracker) Write(w io.Writer) {
|
|
|
|
fmt.Fprintf(w, "import (\n")
|
|
|
|
for s := range it.packages {
|
|
|
|
fmt.Fprintf(w, "\t%q\n", s)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, ")\n\n")
|
|
|
|
}
|
|
|
|
|
2023-01-27 21:36:46 +00:00
|
|
|
func writeHeader(w io.Writer, tool, pkg string) {
|
2023-03-27 22:33:42 +00:00
|
|
|
if *flagCopyright {
|
|
|
|
fmt.Fprint(w, copyrightHeader)
|
|
|
|
}
|
2023-01-03 18:59:00 +00:00
|
|
|
fmt.Fprintf(w, genAndPackageHeader, tool, pkg)
|
2022-05-01 23:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WritePackageFile adds a file with the provided imports and contents to package.
|
|
|
|
// The tool param is used to identify the tool that generated package file.
|
2023-01-27 21:36:46 +00:00
|
|
|
func WritePackageFile(tool string, pkg *packages.Package, path string, it *ImportTracker, contents *bytes.Buffer) error {
|
2022-05-01 23:15:14 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2023-01-27 21:36:46 +00:00
|
|
|
writeHeader(buf, tool, pkg.Name)
|
2022-05-01 23:15:14 +00:00
|
|
|
it.Write(buf)
|
|
|
|
if _, err := buf.Write(contents.Bytes()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return writeFormatted(buf.Bytes(), path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeFormatted writes code to path.
|
2021-09-16 22:41:57 +00:00
|
|
|
// It runs gofmt on it before writing;
|
|
|
|
// if gofmt fails, it writes code unchanged.
|
|
|
|
// Errors can include I/O errors and gofmt errors.
|
|
|
|
//
|
|
|
|
// The advantage of always writing code to path,
|
|
|
|
// even if gofmt fails, is that it makes debugging easier.
|
|
|
|
// The code can be long, but you need it in order to debug.
|
|
|
|
// It is nicer to work with it in a file than a terminal.
|
|
|
|
// It is also easier to interpret gofmt errors
|
|
|
|
// with an editor providing file and line numbers.
|
2022-05-01 23:15:14 +00:00
|
|
|
func writeFormatted(code []byte, path string) error {
|
2022-04-29 20:30:54 +00:00
|
|
|
out, fmterr := imports.Process(path, code, &imports.Options{
|
|
|
|
Comments: true,
|
|
|
|
TabIndent: true,
|
|
|
|
TabWidth: 8,
|
|
|
|
FormatOnly: true, // fancy gofmt only
|
|
|
|
})
|
2021-09-16 22:41:57 +00:00
|
|
|
if fmterr != nil {
|
|
|
|
out = code
|
|
|
|
}
|
|
|
|
ioerr := os.WriteFile(path, out, 0644)
|
|
|
|
// Prefer I/O errors. They're usually easier to fix,
|
|
|
|
// and until they're fixed you can't do much else.
|
|
|
|
if ioerr != nil {
|
|
|
|
return ioerr
|
|
|
|
}
|
|
|
|
if fmterr != nil {
|
|
|
|
return fmt.Errorf("%s:%v", path, fmterr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-16 22:48:40 +00:00
|
|
|
|
2022-05-01 23:15:14 +00:00
|
|
|
// namedTypes returns all named types in pkg, keyed by their type name.
|
|
|
|
func namedTypes(pkg *packages.Package) map[string]*types.Named {
|
2021-09-16 23:24:50 +00:00
|
|
|
nt := make(map[string]*types.Named)
|
|
|
|
for _, file := range pkg.Syntax {
|
|
|
|
for _, d := range file.Decls {
|
|
|
|
decl, ok := d.(*ast.GenDecl)
|
|
|
|
if !ok || decl.Tok != token.TYPE {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, s := range decl.Specs {
|
|
|
|
spec, ok := s.(*ast.TypeSpec)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-05-01 23:15:14 +00:00
|
|
|
typeNameObj, ok := pkg.TypesInfo.Defs[spec.Name]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-16 23:24:50 +00:00
|
|
|
typ, ok := typeNameObj.Type().(*types.Named)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nt[spec.Name.Name] = typ
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nt
|
|
|
|
}
|
|
|
|
|
2021-09-16 22:48:40 +00:00
|
|
|
// AssertStructUnchanged generates code that asserts at compile time that type t is unchanged.
|
2021-09-17 23:43:57 +00:00
|
|
|
// thisPkg is the package containing t.
|
2021-09-16 22:48:40 +00:00
|
|
|
// tname is the named type corresponding to t.
|
|
|
|
// ctx is a single-word context for this assertion, such as "Clone".
|
|
|
|
// If non-nil, AssertStructUnchanged will add elements to imports
|
|
|
|
// for each package path that the caller must import for the returned code to compile.
|
2024-07-08 15:11:00 +00:00
|
|
|
func AssertStructUnchanged(t *types.Struct, tname string, params *types.TypeParamList, ctx string, it *ImportTracker) []byte {
|
2021-09-16 22:48:40 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2022-03-16 23:27:57 +00:00
|
|
|
w := func(format string, args ...any) {
|
2021-09-16 22:48:40 +00:00
|
|
|
fmt.Fprintf(buf, format+"\n", args...)
|
|
|
|
}
|
|
|
|
w("// A compilation failure here means this code must be regenerated, with the command at the top of this file.")
|
2024-07-08 15:11:00 +00:00
|
|
|
|
|
|
|
hasTypeParams := params != nil && params.Len() > 0
|
|
|
|
if hasTypeParams {
|
|
|
|
constraints, identifiers := FormatTypeParams(params, it)
|
|
|
|
w("func _%s%sNeedsRegeneration%s (%s%s) {", tname, ctx, constraints, tname, identifiers)
|
|
|
|
w("_%s%sNeedsRegeneration(struct {", tname, ctx)
|
|
|
|
} else {
|
|
|
|
w("var _%s%sNeedsRegeneration = %s(struct {", tname, ctx, tname)
|
|
|
|
}
|
2021-09-16 22:48:40 +00:00
|
|
|
|
2024-04-16 20:15:13 +00:00
|
|
|
for i := range t.NumFields() {
|
2023-05-09 22:35:47 +00:00
|
|
|
st := t.Field(i)
|
|
|
|
fname := st.Name()
|
2021-09-16 22:48:40 +00:00
|
|
|
ft := t.Field(i).Type()
|
2022-05-01 23:15:14 +00:00
|
|
|
if IsInvalid(ft) {
|
|
|
|
continue
|
2021-09-16 22:48:40 +00:00
|
|
|
}
|
2022-05-01 23:15:14 +00:00
|
|
|
qname := it.QualifiedName(ft)
|
2024-07-08 15:11:00 +00:00
|
|
|
var tag string
|
|
|
|
if hasTypeParams {
|
|
|
|
tag = t.Tag(i)
|
|
|
|
if tag != "" {
|
|
|
|
tag = "`" + tag + "`"
|
|
|
|
}
|
|
|
|
}
|
2023-05-09 22:35:47 +00:00
|
|
|
if st.Anonymous() {
|
2024-08-09 13:09:17 +00:00
|
|
|
w("\t%s %s", qname, tag)
|
2023-05-09 22:35:47 +00:00
|
|
|
} else {
|
2024-07-08 15:11:00 +00:00
|
|
|
w("\t%s %s %s", fname, qname, tag)
|
2023-05-09 22:35:47 +00:00
|
|
|
}
|
2021-09-16 22:48:40 +00:00
|
|
|
}
|
|
|
|
|
2024-07-08 15:11:00 +00:00
|
|
|
if hasTypeParams {
|
|
|
|
w("}{})\n}")
|
|
|
|
} else {
|
|
|
|
w("}{})")
|
|
|
|
}
|
2021-09-16 22:48:40 +00:00
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2022-05-01 23:15:14 +00:00
|
|
|
// IsInvalid reports whether the provided type is invalid. It is used to allow
|
|
|
|
// codegeneration to run even when the target files have build errors or are
|
|
|
|
// missing views.
|
|
|
|
func IsInvalid(t types.Type) bool {
|
|
|
|
return t.String() == "invalid type"
|
2021-09-16 22:48:40 +00:00
|
|
|
}
|
2021-09-17 18:29:17 +00:00
|
|
|
|
|
|
|
// ContainsPointers reports whether typ contains any pointers,
|
|
|
|
// either explicitly or implicitly.
|
|
|
|
// It has special handling for some types that contain pointers
|
|
|
|
// that we know are free from memory aliasing/mutation concerns.
|
|
|
|
func ContainsPointers(typ types.Type) bool {
|
|
|
|
switch typ.String() {
|
|
|
|
case "time.Time":
|
|
|
|
// time.Time contains a pointer that does not need copying
|
|
|
|
return false
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
case "inet.af/netip.Addr", "net/netip.Addr", "net/netip.Prefix", "net/netip.AddrPort":
|
2021-09-17 18:29:17 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch ft := typ.Underlying().(type) {
|
|
|
|
case *types.Array:
|
|
|
|
return ContainsPointers(ft.Elem())
|
2024-07-08 15:11:00 +00:00
|
|
|
case *types.Basic:
|
|
|
|
if ft.Kind() == types.UnsafePointer {
|
|
|
|
return true
|
|
|
|
}
|
2021-09-17 18:29:17 +00:00
|
|
|
case *types.Chan:
|
|
|
|
return true
|
|
|
|
case *types.Interface:
|
2024-07-08 15:11:00 +00:00
|
|
|
if ft.Empty() || ft.IsMethodSet() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for i := 0; i < ft.NumEmbeddeds(); i++ {
|
|
|
|
if ContainsPointers(ft.EmbeddedType(i)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 18:29:17 +00:00
|
|
|
case *types.Map:
|
|
|
|
return true
|
|
|
|
case *types.Pointer:
|
|
|
|
return true
|
|
|
|
case *types.Slice:
|
|
|
|
return true
|
|
|
|
case *types.Struct:
|
2024-04-16 20:15:13 +00:00
|
|
|
for i := range ft.NumFields() {
|
2021-09-17 18:29:17 +00:00
|
|
|
if ContainsPointers(ft.Field(i).Type()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2024-07-08 15:11:00 +00:00
|
|
|
case *types.Union:
|
|
|
|
for i := range ft.Len() {
|
|
|
|
if ContainsPointers(ft.Term(i).Type()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 18:29:17 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-05-01 23:15:14 +00:00
|
|
|
|
|
|
|
// IsViewType reports whether the provided typ is a View.
|
|
|
|
func IsViewType(typ types.Type) bool {
|
|
|
|
t, ok := typ.Underlying().(*types.Struct)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if t.NumFields() != 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return t.Field(0).Name() == "ж"
|
|
|
|
}
|
2024-07-08 15:11:00 +00:00
|
|
|
|
|
|
|
// FormatTypeParams formats the specified params and returns two strings:
|
|
|
|
// - constraints are comma-separated type parameters and their constraints in square brackets (e.g. [T any, V constraints.Integer])
|
|
|
|
// - names are comma-separated type parameter names in square brackets (e.g. [T, V])
|
|
|
|
//
|
|
|
|
// If params is nil or empty, both return values are empty strings.
|
|
|
|
func FormatTypeParams(params *types.TypeParamList, it *ImportTracker) (constraints, names string) {
|
|
|
|
if params == nil || params.Len() == 0 {
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
var constraintList, nameList []string
|
|
|
|
for i := range params.Len() {
|
|
|
|
param := params.At(i)
|
|
|
|
name := param.Obj().Name()
|
|
|
|
constraint := it.QualifiedName(param.Constraint())
|
|
|
|
nameList = append(nameList, name)
|
|
|
|
constraintList = append(constraintList, name+" "+constraint)
|
|
|
|
}
|
|
|
|
constraints = "[" + strings.Join(constraintList, ", ") + "]"
|
|
|
|
names = "[" + strings.Join(nameList, ", ") + "]"
|
|
|
|
return constraints, names
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupMethod returns the method with the specified name in t, or nil if the method does not exist.
|
|
|
|
func LookupMethod(t types.Type, name string) *types.Func {
|
|
|
|
if t, ok := t.(*types.Named); ok {
|
|
|
|
for i := 0; i < t.NumMethods(); i++ {
|
|
|
|
if method := t.Method(i); method.Name() == name {
|
|
|
|
return method
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t, ok := t.Underlying().(*types.Interface); ok {
|
|
|
|
for i := 0; i < t.NumMethods(); i++ {
|
|
|
|
if method := t.Method(i); method.Name() == name {
|
|
|
|
return method
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|