util/codegen: add ContainsPointers

And use it in cmd/cloner.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
This commit is contained in:
Josh Bleecher Snyder
2021-09-17 11:29:17 -07:00
committed by Josh Bleecher Snyder
parent d5a0a4297e
commit 3cd85c0ca6
2 changed files with 40 additions and 36 deletions

View File

@@ -109,3 +109,38 @@ func importedName(t types.Type, thisPkg *types.Package) (qualifiedName, importPk
}
return types.TypeString(t, qual), importPkg
}
// 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
case "inet.af/netaddr.IP":
return false
}
switch ft := typ.Underlying().(type) {
case *types.Array:
return ContainsPointers(ft.Elem())
case *types.Chan:
return true
case *types.Interface:
return true // a little too broad
case *types.Map:
return true
case *types.Pointer:
return true
case *types.Slice:
return true
case *types.Struct:
for i := 0; i < ft.NumFields(); i++ {
if ContainsPointers(ft.Field(i).Type()) {
return true
}
}
}
return false
}