codegen.go 11 KB

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