codegen.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package codegen contains shared utilities for generating code.
  4. package codegen
  5. import (
  6. "bytes"
  7. "flag"
  8. "fmt"
  9. "go/ast"
  10. "go/token"
  11. "go/types"
  12. "io"
  13. "os"
  14. "reflect"
  15. "strings"
  16. "golang.org/x/tools/go/packages"
  17. "golang.org/x/tools/imports"
  18. "tailscale.com/util/mak"
  19. )
  20. var flagCopyright = flag.Bool("copyright", true, "add Tailscale copyright to generated file headers")
  21. // LoadTypes returns all named types in pkgName, keyed by their type name.
  22. func LoadTypes(buildTags string, pkgName string) (*packages.Package, map[string]types.Type, error) {
  23. cfg := &packages.Config{
  24. Mode: packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax | packages.NeedName,
  25. Tests: buildTags == "test",
  26. }
  27. if buildTags != "" && !cfg.Tests {
  28. cfg.BuildFlags = []string{"-tags=" + buildTags}
  29. }
  30. pkgs, err := packages.Load(cfg, pkgName)
  31. if err != nil {
  32. return nil, nil, err
  33. }
  34. if cfg.Tests {
  35. pkgs = testPackages(pkgs)
  36. }
  37. if len(pkgs) != 1 {
  38. return nil, nil, fmt.Errorf("wrong number of packages: %d", len(pkgs))
  39. }
  40. pkg := pkgs[0]
  41. return pkg, namedTypes(pkg), nil
  42. }
  43. func testPackages(pkgs []*packages.Package) []*packages.Package {
  44. var testPackages []*packages.Package
  45. for _, pkg := range pkgs {
  46. testPackageID := fmt.Sprintf("%[1]s [%[1]s.test]", pkg.PkgPath)
  47. if pkg.ID == testPackageID {
  48. testPackages = append(testPackages, pkg)
  49. }
  50. }
  51. return testPackages
  52. }
  53. // HasNoClone reports whether the provided tag has `codegen:noclone`.
  54. func HasNoClone(structTag string) bool {
  55. val := reflect.StructTag(structTag).Get("codegen")
  56. for _, v := range strings.Split(val, ",") {
  57. if v == "noclone" {
  58. return true
  59. }
  60. }
  61. return false
  62. }
  63. const copyrightHeader = `// Copyright (c) Tailscale Inc & AUTHORS
  64. // SPDX-License-Identifier: BSD-3-Clause
  65. `
  66. const genAndPackageHeader = `// Code generated by %v; DO NOT EDIT.
  67. package %s
  68. `
  69. func NewImportTracker(thisPkg *types.Package) *ImportTracker {
  70. return &ImportTracker{
  71. thisPkg: thisPkg,
  72. }
  73. }
  74. // ImportTracker provides a mechanism to track and build import paths.
  75. type ImportTracker struct {
  76. thisPkg *types.Package
  77. packages map[string]bool
  78. }
  79. func (it *ImportTracker) Import(pkg string) {
  80. if pkg != "" && !it.packages[pkg] {
  81. mak.Set(&it.packages, pkg, true)
  82. }
  83. }
  84. func (it *ImportTracker) qualifier(pkg *types.Package) string {
  85. if it.thisPkg == pkg {
  86. return ""
  87. }
  88. it.Import(pkg.Path())
  89. // TODO(maisem): handle conflicts?
  90. return pkg.Name()
  91. }
  92. // QualifiedName returns the string representation of t in the package.
  93. func (it *ImportTracker) QualifiedName(t types.Type) string {
  94. return types.TypeString(t, it.qualifier)
  95. }
  96. // PackagePrefix returns the prefix to be used when referencing named objects from pkg.
  97. func (it *ImportTracker) PackagePrefix(pkg *types.Package) string {
  98. if s := it.qualifier(pkg); s != "" {
  99. return s + "."
  100. }
  101. return ""
  102. }
  103. // Write prints all the tracked imports in a single import block to w.
  104. func (it *ImportTracker) Write(w io.Writer) {
  105. fmt.Fprintf(w, "import (\n")
  106. for s := range it.packages {
  107. fmt.Fprintf(w, "\t%q\n", s)
  108. }
  109. fmt.Fprintf(w, ")\n\n")
  110. }
  111. func writeHeader(w io.Writer, tool, pkg string) {
  112. if *flagCopyright {
  113. fmt.Fprint(w, copyrightHeader)
  114. }
  115. fmt.Fprintf(w, genAndPackageHeader, tool, pkg)
  116. }
  117. // WritePackageFile adds a file with the provided imports and contents to package.
  118. // The tool param is used to identify the tool that generated package file.
  119. func WritePackageFile(tool string, pkg *packages.Package, path string, it *ImportTracker, contents *bytes.Buffer) error {
  120. buf := new(bytes.Buffer)
  121. writeHeader(buf, tool, pkg.Name)
  122. it.Write(buf)
  123. if _, err := buf.Write(contents.Bytes()); err != nil {
  124. return err
  125. }
  126. return writeFormatted(buf.Bytes(), path)
  127. }
  128. // writeFormatted writes code to path.
  129. // It runs gofmt on it before writing;
  130. // if gofmt fails, it writes code unchanged.
  131. // Errors can include I/O errors and gofmt errors.
  132. //
  133. // The advantage of always writing code to path,
  134. // even if gofmt fails, is that it makes debugging easier.
  135. // The code can be long, but you need it in order to debug.
  136. // It is nicer to work with it in a file than a terminal.
  137. // It is also easier to interpret gofmt errors
  138. // with an editor providing file and line numbers.
  139. func writeFormatted(code []byte, path string) error {
  140. out, fmterr := imports.Process(path, code, &imports.Options{
  141. Comments: true,
  142. TabIndent: true,
  143. TabWidth: 8,
  144. FormatOnly: true, // fancy gofmt only
  145. })
  146. if fmterr != nil {
  147. out = code
  148. }
  149. ioerr := os.WriteFile(path, out, 0644)
  150. // Prefer I/O errors. They're usually easier to fix,
  151. // and until they're fixed you can't do much else.
  152. if ioerr != nil {
  153. return ioerr
  154. }
  155. if fmterr != nil {
  156. return fmt.Errorf("%s:%v", path, fmterr)
  157. }
  158. return nil
  159. }
  160. // namedTypes returns all named types in pkg, keyed by their type name.
  161. func namedTypes(pkg *packages.Package) map[string]types.Type {
  162. nt := make(map[string]types.Type)
  163. for _, file := range pkg.Syntax {
  164. for _, d := range file.Decls {
  165. decl, ok := d.(*ast.GenDecl)
  166. if !ok || decl.Tok != token.TYPE {
  167. continue
  168. }
  169. for _, s := range decl.Specs {
  170. spec, ok := s.(*ast.TypeSpec)
  171. if !ok {
  172. continue
  173. }
  174. typeNameObj, ok := pkg.TypesInfo.Defs[spec.Name]
  175. if !ok {
  176. continue
  177. }
  178. switch typ := typeNameObj.Type(); typ.(type) {
  179. case *types.Alias, *types.Named:
  180. nt[spec.Name.Name] = typ
  181. }
  182. }
  183. }
  184. }
  185. return nt
  186. }
  187. // AssertStructUnchanged generates code that asserts at compile time that type t is unchanged.
  188. // thisPkg is the package containing t.
  189. // tname is the named type corresponding to t.
  190. // ctx is a single-word context for this assertion, such as "Clone".
  191. // If non-nil, AssertStructUnchanged will add elements to imports
  192. // for each package path that the caller must import for the returned code to compile.
  193. func AssertStructUnchanged(t *types.Struct, tname string, params *types.TypeParamList, ctx string, it *ImportTracker) []byte {
  194. buf := new(bytes.Buffer)
  195. w := func(format string, args ...any) {
  196. fmt.Fprintf(buf, format+"\n", args...)
  197. }
  198. w("// A compilation failure here means this code must be regenerated, with the command at the top of this file.")
  199. hasTypeParams := params != nil && params.Len() > 0
  200. if hasTypeParams {
  201. constraints, identifiers := FormatTypeParams(params, it)
  202. w("func _%s%sNeedsRegeneration%s (%s%s) {", tname, ctx, constraints, tname, identifiers)
  203. w("_%s%sNeedsRegeneration(struct {", tname, ctx)
  204. } else {
  205. w("var _%s%sNeedsRegeneration = %s(struct {", tname, ctx, tname)
  206. }
  207. for i := range t.NumFields() {
  208. st := t.Field(i)
  209. fname := st.Name()
  210. ft := t.Field(i).Type()
  211. if IsInvalid(ft) {
  212. continue
  213. }
  214. qname := it.QualifiedName(ft)
  215. var tag string
  216. if hasTypeParams {
  217. tag = t.Tag(i)
  218. if tag != "" {
  219. tag = "`" + tag + "`"
  220. }
  221. }
  222. if st.Anonymous() {
  223. w("\t%s %s", qname, tag)
  224. } else {
  225. w("\t%s %s %s", fname, qname, tag)
  226. }
  227. }
  228. if hasTypeParams {
  229. w("}{})\n}")
  230. } else {
  231. w("}{})")
  232. }
  233. return buf.Bytes()
  234. }
  235. // IsInvalid reports whether the provided type is invalid. It is used to allow
  236. // codegeneration to run even when the target files have build errors or are
  237. // missing views.
  238. func IsInvalid(t types.Type) bool {
  239. return t.String() == "invalid type"
  240. }
  241. // ContainsPointers reports whether typ contains any pointers,
  242. // either explicitly or implicitly.
  243. // It has special handling for some types that contain pointers
  244. // that we know are free from memory aliasing/mutation concerns.
  245. func ContainsPointers(typ types.Type) bool {
  246. switch typ.String() {
  247. case "time.Time":
  248. // time.Time contains a pointer that does not need copying
  249. return false
  250. case "inet.af/netip.Addr", "net/netip.Addr", "net/netip.Prefix", "net/netip.AddrPort":
  251. return false
  252. }
  253. switch ft := typ.Underlying().(type) {
  254. case *types.Array:
  255. return ContainsPointers(ft.Elem())
  256. case *types.Basic:
  257. if ft.Kind() == types.UnsafePointer {
  258. return true
  259. }
  260. case *types.Chan:
  261. return true
  262. case *types.Interface:
  263. if ft.Empty() || ft.IsMethodSet() {
  264. return true
  265. }
  266. for i := 0; i < ft.NumEmbeddeds(); i++ {
  267. if ContainsPointers(ft.EmbeddedType(i)) {
  268. return true
  269. }
  270. }
  271. case *types.Map:
  272. return true
  273. case *types.Pointer:
  274. return true
  275. case *types.Slice:
  276. return true
  277. case *types.Struct:
  278. for i := range ft.NumFields() {
  279. if ContainsPointers(ft.Field(i).Type()) {
  280. return true
  281. }
  282. }
  283. case *types.Union:
  284. for i := range ft.Len() {
  285. if ContainsPointers(ft.Term(i).Type()) {
  286. return true
  287. }
  288. }
  289. }
  290. return false
  291. }
  292. // IsViewType reports whether the provided typ is a View.
  293. func IsViewType(typ types.Type) bool {
  294. t, ok := typ.Underlying().(*types.Struct)
  295. if !ok {
  296. return false
  297. }
  298. if t.NumFields() != 1 {
  299. return false
  300. }
  301. return t.Field(0).Name() == "ж"
  302. }
  303. // FormatTypeParams formats the specified params and returns two strings:
  304. // - constraints are comma-separated type parameters and their constraints in square brackets (e.g. [T any, V constraints.Integer])
  305. // - names are comma-separated type parameter names in square brackets (e.g. [T, V])
  306. //
  307. // If params is nil or empty, both return values are empty strings.
  308. func FormatTypeParams(params *types.TypeParamList, it *ImportTracker) (constraints, names string) {
  309. if params == nil || params.Len() == 0 {
  310. return "", ""
  311. }
  312. var constraintList, nameList []string
  313. for i := range params.Len() {
  314. param := params.At(i)
  315. name := param.Obj().Name()
  316. constraint := it.QualifiedName(param.Constraint())
  317. nameList = append(nameList, name)
  318. constraintList = append(constraintList, name+" "+constraint)
  319. }
  320. constraints = "[" + strings.Join(constraintList, ", ") + "]"
  321. names = "[" + strings.Join(nameList, ", ") + "]"
  322. return constraints, names
  323. }
  324. // LookupMethod returns the method with the specified name in t, or nil if the method does not exist.
  325. func LookupMethod(t types.Type, name string) *types.Func {
  326. switch t := t.(type) {
  327. case *types.Alias:
  328. return LookupMethod(t.Rhs(), name)
  329. case *types.TypeParam:
  330. return LookupMethod(t.Constraint(), name)
  331. case *types.Pointer:
  332. return LookupMethod(t.Elem(), name)
  333. case *types.Named:
  334. switch u := t.Underlying().(type) {
  335. case *types.Interface:
  336. return LookupMethod(u, name)
  337. default:
  338. for i := 0; i < t.NumMethods(); i++ {
  339. if method := t.Method(i); method.Name() == name {
  340. return method
  341. }
  342. }
  343. }
  344. case *types.Interface:
  345. for i := 0; i < t.NumMethods(); i++ {
  346. if method := t.Method(i); method.Name() == name {
  347. return method
  348. }
  349. }
  350. }
  351. return nil
  352. }
  353. // NamedTypeOf is like t.(*types.Named), but also works with type aliases.
  354. func NamedTypeOf(t types.Type) (named *types.Named, ok bool) {
  355. if a, ok := t.(*types.Alias); ok {
  356. return NamedTypeOf(types.Unalias(a))
  357. }
  358. named, ok = t.(*types.Named)
  359. return
  360. }