aboutsummaryrefslogtreecommitdiff
path: root/internal/typeparams/copytermlist.go
blob: 5357f9d2fd70cfdc6df1943a53045c1d3e9f948c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build ignore
// +build ignore

// copytermlist.go copies the term list algorithm from GOROOT/src/go/types.

package main

import (
	"bytes"
	"fmt"
	"go/ast"
	"go/format"
	"go/parser"
	"go/token"
	"os"
	"path/filepath"
	"reflect"
	"runtime"
	"strings"

	"golang.org/x/tools/go/ast/astutil"
)

func main() {
	if err := doCopy(); err != nil {
		fmt.Fprintf(os.Stderr, "error copying from go/types: %v", err)
		os.Exit(1)
	}
}

func doCopy() error {
	dir := filepath.Join(runtime.GOROOT(), "src", "go", "types")
	for _, name := range []string{"typeterm.go", "termlist.go"} {
		path := filepath.Join(dir, name)
		fset := token.NewFileSet()
		file, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
		if err != nil {
			return err
		}
		file.Name.Name = "typeparams"
		file.Doc = &ast.CommentGroup{List: []*ast.Comment{{Text: "DO NOT MODIFY"}}}
		var needImport bool
		selectorType := reflect.TypeOf((*ast.SelectorExpr)(nil))
		astutil.Apply(file, func(c *astutil.Cursor) bool {
			if id, _ := c.Node().(*ast.Ident); id != nil {
				// Check if this ident should be qualified with types. For simplicity,
				// assume the copied files do not themselves contain any exported
				// symbols.

				// As a simple heuristic, just verify that the ident may be replaced by
				// a selector.
				if !token.IsExported(id.Name) {
					return false
				}
				v := reflect.TypeOf(c.Parent()).Elem() // ast nodes are all pointers
				field, ok := v.FieldByName(c.Name())
				if !ok {
					panic("missing field")
				}
				t := field.Type
				if c.Index() > 0 { // => t is a slice
					t = t.Elem()
				}
				if !selectorType.AssignableTo(t) {
					return false
				}
				needImport = true
				c.Replace(&ast.SelectorExpr{
					X:   &ast.Ident{NamePos: id.NamePos, Name: "types"},
					Sel: &ast.Ident{NamePos: id.NamePos, Name: id.Name, Obj: id.Obj},
				})
			}
			return true
		}, nil)
		if needImport {
			astutil.AddImport(fset, file, "go/types")
		}

		var b bytes.Buffer
		if err := format.Node(&b, fset, file); err != nil {
			return err
		}

		// Hack in the 'generated' byline.
		content := b.String()
		header := "// Code generated by copytermlist.go DO NOT EDIT.\n\npackage typeparams"
		content = strings.Replace(content, "package typeparams", header, 1)

		if err := os.WriteFile(name, []byte(content), 0644); err != nil {
			return err
		}
	}
	return nil
}