Debian Codegraph @

typeparams/copytermlist.go

raw symbols SCIP
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build ignore
6
7 // copytermlist.go copies the term list algorithm from GOROOT/src/go/types.
8 //
9 // Usage:
10 //
11 // Requires running in GOPATH mode (GO111MODULE=off):to avoid adding
12 // the dependency on golang.org/x/tools in go.mod.
13 // 1. Check out golang.org/x/tools at $GOPATH/src/golang.org/x/tools.
14 // 2. Run: GO111MODULE=off go run copytermlist.go
15
16 package main
17
18 import (
19 "bytes"
20 "fmt"
21 "go/ast"
22 "go/format"
23 "go/parser"
24 "go/token"
25 "os"
26 "path/filepath"
27 "reflect"
28 "runtime"
29 "strings"
30
31 "golang.org/x/tools/go/ast/astutil"
32 )
33
34 func main() {
35 if err := doCopy(); err != nil {
36 fmt.Fprintf(os.Stderr, "error copying from go/types: %v", err)
37 os.Exit(1)
38 }
39 }
40
41 func doCopy() error {
42 dir := filepath.Join(runtime.GOROOT(), "src", "go", "types")
43 for _, name := range []string{"typeterm.go", "termlist.go"} {
44 path := filepath.Join(dir, name)
45 fset := token.NewFileSet()
46 file, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
47 if err != nil {
48 return err
49 }
50 file.Name.Name = "typeparams"
51 file.Doc = &ast.CommentGroup{List: []*ast.Comment{&ast.Comment{Text: "DO NOT MODIFY"}}}
52 var needImport bool
53 selectorType := reflect.TypeOf((*ast.SelectorExpr)(nil))
54 astutil.Apply(file, func(c *astutil.Cursor) bool {
55 if id, _ := c.Node().(*ast.Ident); id != nil {
56 // Check if this ident should be qualified with types. For simplicity,
57 // assume the copied files do not themselves contain any exported
58 // symbols.
59
60 // As a simple heuristic, just verify that the ident may be replaced by
61 // a selector.
62 if !token.IsExported(id.Name) {
63 return false
64 }
65 v := reflect.TypeOf(c.Parent()).Elem() // ast nodes are all pointers
66 field, ok := v.FieldByName(c.Name())
67 if !ok {
68 panic("missing field")
69 }
70 t := field.Type
71 if c.Index() > 0 { // => t is a slice
72 t = t.Elem()
73 }
74 if !selectorType.AssignableTo(t) {
75 return false
76 }
77 needImport = true
78 c.Replace(&ast.SelectorExpr{
79 X: &ast.Ident{NamePos: id.Pos(), Name: "types"},
80 Sel: &ast.Ident{NamePos: id.Pos(), Name: id.Name},
81 })
82 }
83 return true
84 }, nil)
85 if needImport {
86 astutil.AddImport(fset, file, "go/types")
87 }
88
89 var b bytes.Buffer
90 if err := format.Node(&b, fset, file); err != nil {
91 return err
92 }
93
94 // Rewrite code gen byline and source info.
95 var res bytes.Buffer
96
97 var skippingHeader = true
98 var wroteFirstContentLine bool
99 for line := range strings.SplitSeq(b.String(), "\n") {
100 if skippingHeader {
101 trimmed := strings.TrimSpace(line)
102 if strings.Contains(trimmed, "DO NOT EDIT") && strings.Contains(trimmed, "Code generated") {
103 res.WriteString("// Code generated by copytermlist.go DO NOT EDIT.\n")
104 continue
105 }
106 if strings.HasPrefix(trimmed, "// Source:") {
107 res.WriteString("// Source: GOROOT/src/go/types/" + name + "\n\n")
108 continue
109 }
110 if trimmed == "" {
111 continue // Skip leading blank lines until we hit the first actual comment/code
112 }
113 skippingHeader = false
114 }
115
116 // Handle line endings to avoid duplicate trailing newlines at the end of the file
117 if !wroteFirstContentLine {
118 res.WriteString(line)
119 wroteFirstContentLine = true
120 } else {
121 res.WriteString("\n")
122 res.WriteString(line)
123 }
124 }
125
126 if err := os.WriteFile(name, res.Bytes(), 0644); err != nil {
127 return err
128 }
129 }
130 return nil
131 }