codegen.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. s := typ.String()
  251. switch s {
  252. case "time.Time":
  253. // time.Time contains a pointer that does not need cloning.
  254. return false
  255. case "inet.af/netip.Addr":
  256. return false
  257. }
  258. if strings.HasPrefix(s, "unique.Handle[") {
  259. // unique.Handle contains a pointer that does not need cloning.
  260. return false
  261. }
  262. switch ft := typ.Underlying().(type) {
  263. case *types.Array:
  264. return ContainsPointers(ft.Elem())
  265. case *types.Basic:
  266. if ft.Kind() == types.UnsafePointer {
  267. return true
  268. }
  269. case *types.Chan:
  270. return true
  271. case *types.Interface:
  272. if ft.Empty() || ft.IsMethodSet() {
  273. return true
  274. }
  275. for i := 0; i < ft.NumEmbeddeds(); i++ {
  276. if ContainsPointers(ft.EmbeddedType(i)) {
  277. return true
  278. }
  279. }
  280. case *types.Map:
  281. return true
  282. case *types.Pointer:
  283. return true
  284. case *types.Slice:
  285. return true
  286. case *types.Struct:
  287. for i := range ft.NumFields() {
  288. if ContainsPointers(ft.Field(i).Type()) {
  289. return true
  290. }
  291. }
  292. case *types.Union:
  293. for i := range ft.Len() {
  294. if ContainsPointers(ft.Term(i).Type()) {
  295. return true
  296. }
  297. }
  298. }
  299. return false
  300. }
  301. // IsViewType reports whether the provided typ is a View.
  302. func IsViewType(typ types.Type) bool {
  303. t, ok := typ.Underlying().(*types.Struct)
  304. if !ok {
  305. return false
  306. }
  307. if t.NumFields() != 1 {
  308. return false
  309. }
  310. return t.Field(0).Name() == "ж"
  311. }
  312. // FormatTypeParams formats the specified params and returns two strings:
  313. // - constraints are comma-separated type parameters and their constraints in square brackets (e.g. [T any, V constraints.Integer])
  314. // - names are comma-separated type parameter names in square brackets (e.g. [T, V])
  315. //
  316. // If params is nil or empty, both return values are empty strings.
  317. func FormatTypeParams(params *types.TypeParamList, it *ImportTracker) (constraints, names string) {
  318. if params == nil || params.Len() == 0 {
  319. return "", ""
  320. }
  321. var constraintList, nameList []string
  322. for i := range params.Len() {
  323. param := params.At(i)
  324. name := param.Obj().Name()
  325. constraint := it.QualifiedName(param.Constraint())
  326. nameList = append(nameList, name)
  327. constraintList = append(constraintList, name+" "+constraint)
  328. }
  329. constraints = "[" + strings.Join(constraintList, ", ") + "]"
  330. names = "[" + strings.Join(nameList, ", ") + "]"
  331. return constraints, names
  332. }
  333. // LookupMethod returns the method with the specified name in t, or nil if the method does not exist.
  334. func LookupMethod(t types.Type, name string) *types.Func {
  335. switch t := t.(type) {
  336. case *types.Alias:
  337. return LookupMethod(t.Rhs(), name)
  338. case *types.TypeParam:
  339. return LookupMethod(t.Constraint(), name)
  340. case *types.Pointer:
  341. return LookupMethod(t.Elem(), name)
  342. case *types.Named:
  343. switch u := t.Underlying().(type) {
  344. case *types.Interface:
  345. return LookupMethod(u, name)
  346. default:
  347. for i := 0; i < t.NumMethods(); i++ {
  348. if method := t.Method(i); method.Name() == name {
  349. return method
  350. }
  351. }
  352. }
  353. case *types.Interface:
  354. for i := 0; i < t.NumMethods(); i++ {
  355. if method := t.Method(i); method.Name() == name {
  356. return method
  357. }
  358. }
  359. }
  360. return nil
  361. }
  362. // NamedTypeOf is like t.(*types.Named), but also works with type aliases.
  363. func NamedTypeOf(t types.Type) (named *types.Named, ok bool) {
  364. if a, ok := t.(*types.Alias); ok {
  365. return NamedTypeOf(types.Unalias(a))
  366. }
  367. named, ok = t.(*types.Named)
  368. return
  369. }