.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.1.0 / internal / imports / mkstdlib.go
1 // Copyright 2019 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 // +build ignore
6
7 // mkstdlib generates the zstdlib.go file, containing the Go standard
8 // library API symbols. It's baked into the binary to avoid scanning
9 // GOPATH in the common case.
10 package main
11
12 import (
13         "bufio"
14         "bytes"
15         "fmt"
16         "go/format"
17         exec "golang.org/x/sys/execabs"
18         "io"
19         "io/ioutil"
20         "log"
21         "os"
22         "path/filepath"
23         "regexp"
24         "runtime"
25         "sort"
26 )
27
28 func mustOpen(name string) io.Reader {
29         f, err := os.Open(name)
30         if err != nil {
31                 log.Fatal(err)
32         }
33         return f
34 }
35
36 func api(base string) string {
37         return filepath.Join(runtime.GOROOT(), "api", base)
38 }
39
40 var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`)
41
42 var unsafeSyms = map[string]bool{"Alignof": true, "ArbitraryType": true, "Offsetof": true, "Pointer": true, "Sizeof": true}
43
44 func main() {
45         var buf bytes.Buffer
46         outf := func(format string, args ...interface{}) {
47                 fmt.Fprintf(&buf, format, args...)
48         }
49         outf("// Code generated by mkstdlib.go. DO NOT EDIT.\n\n")
50         outf("package imports\n")
51         outf("var stdlib = map[string][]string{\n")
52         f := io.MultiReader(
53                 mustOpen(api("go1.txt")),
54                 mustOpen(api("go1.1.txt")),
55                 mustOpen(api("go1.2.txt")),
56                 mustOpen(api("go1.3.txt")),
57                 mustOpen(api("go1.4.txt")),
58                 mustOpen(api("go1.5.txt")),
59                 mustOpen(api("go1.6.txt")),
60                 mustOpen(api("go1.7.txt")),
61                 mustOpen(api("go1.8.txt")),
62                 mustOpen(api("go1.9.txt")),
63                 mustOpen(api("go1.10.txt")),
64                 mustOpen(api("go1.11.txt")),
65                 mustOpen(api("go1.12.txt")),
66                 mustOpen(api("go1.13.txt")),
67                 mustOpen(api("go1.14.txt")),
68                 mustOpen(api("go1.15.txt")),
69
70                 // The API of the syscall/js package needs to be computed explicitly,
71                 // because it's not included in the GOROOT/api/go1.*.txt files at this time.
72                 syscallJSAPI(),
73         )
74         sc := bufio.NewScanner(f)
75
76         pkgs := map[string]map[string]bool{
77                 "unsafe": unsafeSyms,
78         }
79         paths := []string{"unsafe"}
80
81         for sc.Scan() {
82                 l := sc.Text()
83                 if m := sym.FindStringSubmatch(l); m != nil {
84                         path, sym := m[1], m[2]
85
86                         if _, ok := pkgs[path]; !ok {
87                                 pkgs[path] = map[string]bool{}
88                                 paths = append(paths, path)
89                         }
90                         pkgs[path][sym] = true
91                 }
92         }
93         if err := sc.Err(); err != nil {
94                 log.Fatal(err)
95         }
96         sort.Strings(paths)
97         for _, path := range paths {
98                 outf("\t%q: []string{\n", path)
99                 pkg := pkgs[path]
100                 var syms []string
101                 for sym := range pkg {
102                         syms = append(syms, sym)
103                 }
104                 sort.Strings(syms)
105                 for _, sym := range syms {
106                         outf("\t\t%q,\n", sym)
107                 }
108                 outf("},\n")
109         }
110         outf("}\n")
111         fmtbuf, err := format.Source(buf.Bytes())
112         if err != nil {
113                 log.Fatal(err)
114         }
115         err = ioutil.WriteFile("zstdlib.go", fmtbuf, 0666)
116         if err != nil {
117                 log.Fatal(err)
118         }
119 }
120
121 // syscallJSAPI returns the API of the syscall/js package.
122 // It's computed from the contents of $(go env GOROOT)/src/syscall/js.
123 func syscallJSAPI() io.Reader {
124         var exeSuffix string
125         if runtime.GOOS == "windows" {
126                 exeSuffix = ".exe"
127         }
128         cmd := exec.Command("go"+exeSuffix, "run", "cmd/api", "-contexts", "js-wasm", "syscall/js")
129         out, err := cmd.Output()
130         if err != nil {
131                 log.Fatalln(err)
132         }
133         return bytes.NewReader(out)
134 }