| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
if !token.IsExported(id.Name) { |
| 63 |
return false |
| 64 |
} |
| 65 |
v := reflect.TypeOf(c.Parent()).Elem() |
| 66 |
field, ok := v.FieldByName(c.Name()) |
| 67 |
if !ok { |
| 68 |
panic("missing field") |
| 69 |
} |
| 70 |
t := field.Type |
| 71 |
if c.Index() > 0 { |
| 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 |
|
| 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 |
| 112 |
} |
| 113 |
skippingHeader = false |
| 114 |
} |
| 115 |
|
| 116 |
|
| 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 |
} |