GoPLS Viewer

Home|gopls/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//go:build ignore
6// +build ignore
7
8// mkstdlib generates the zstdlib.go file, containing the Go standard
9// library API symbols. It's baked into the binary to avoid scanning
10// GOPATH in the common case.
11package main
12
13import (
14    "bufio"
15    "bytes"
16    "fmt"
17    "go/format"
18    "io"
19    "io/ioutil"
20    "log"
21    "os"
22    "path/filepath"
23    "regexp"
24    "runtime"
25    "sort"
26    "strings"
27
28    exec "golang.org/x/sys/execabs"
29)
30
31func mustOpen(name stringio.Reader {
32    ferr := os.Open(name)
33    if err != nil {
34        log.Fatal(err)
35    }
36    return f
37}
38
39func api(base stringstring {
40    return filepath.Join(runtime.GOROOT(), "api"base)
41}
42
43var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`)
44
45var unsafeSyms = map[string]bool{"Alignof"true"ArbitraryType"true"Offsetof"true"Pointer"true"Sizeof"true}
46
47func main() {
48    var buf bytes.Buffer
49    outf := func(format stringargs ...interface{}) {
50        fmt.Fprintf(&bufformatargs...)
51    }
52    outf(`// Copyright 2022 The Go Authors. All rights reserved.
53// Use of this source code is governed by a BSD-style
54// license that can be found in the LICENSE file.
55
56`)
57    outf("// Code generated by mkstdlib.go. DO NOT EDIT.\n\n")
58    outf("package imports\n")
59    outf("var stdlib = map[string][]string{\n")
60    f := readAPI()
61    sc := bufio.NewScanner(f)
62
63    pkgs := map[string]map[string]bool{
64        "unsafe"unsafeSyms,
65    }
66    paths := []string{"unsafe"}
67
68    for sc.Scan() {
69        l := sc.Text()
70        if m := sym.FindStringSubmatch(l); m != nil {
71            pathsym := m[1], m[2]
72
73            if _ok := pkgs[path]; !ok {
74                pkgs[path] = map[string]bool{}
75                paths = append(pathspath)
76            }
77            pkgs[path][sym] = true
78        }
79    }
80    if err := sc.Err(); err != nil {
81        log.Fatal(err)
82    }
83    sort.Strings(paths)
84    for _path := range paths {
85        outf("\t%q: {\n"path)
86        pkg := pkgs[path]
87        var syms []string
88        for sym := range pkg {
89            syms = append(symssym)
90        }
91        sort.Strings(syms)
92        for _sym := range syms {
93            outf("\t\t%q,\n"sym)
94        }
95        outf("},\n")
96    }
97    outf("}\n")
98    fmtbuferr := format.Source(buf.Bytes())
99    if err != nil {
100        log.Fatal(err)
101    }
102    err = ioutil.WriteFile("zstdlib.go"fmtbuf0666)
103    if err != nil {
104        log.Fatal(err)
105    }
106}
107
108// readAPI opens an io.Reader that reads all stdlib API content.
109func readAPI() io.Reader {
110    entrieserr := os.ReadDir(filepath.Join(runtime.GOROOT(), "api"))
111    if err != nil {
112        log.Fatal(err)
113    }
114    var readers []io.Reader
115    for _entry := range entries {
116        name := entry.Name()
117        if strings.HasPrefix(name"go") && strings.HasSuffix(name".txt") {
118            readers = append(readersmustOpen(api(name)))
119        }
120    }
121    // The API of the syscall/js package needs to be computed explicitly,
122    // because it's not included in the GOROOT/api/go1.*.txt files at this time.
123    readers = append(readerssyscallJSAPI())
124    return io.MultiReader(readers...)
125}
126
127// syscallJSAPI returns the API of the syscall/js package.
128// It's computed from the contents of $(go env GOROOT)/src/syscall/js.
129func syscallJSAPI() io.Reader {
130    var exeSuffix string
131    if runtime.GOOS == "windows" {
132        exeSuffix = ".exe"
133    }
134    cmd := exec.Command("go"+exeSuffix"run""cmd/api""-contexts""js-wasm""syscall/js")
135    outerr := cmd.Output()
136    if err != nil {
137        log.Fatalln(err)
138    }
139    return bytes.NewReader(out)
140}
141
MembersX
main.BlockStmt.RangeStmt_1854.BlockStmt.RangeStmt_1951.sym
main.BlockStmt.RangeStmt_1854.BlockStmt.RangeStmt_2029.sym
readAPI.RangeStmt_2512.BlockStmt.name
main.BlockStmt.err
readAPI
mustOpen
mustOpen.name
main.BlockStmt.sc
syscallJSAPI.out
main.BlockStmt.RangeStmt_1854.BlockStmt.syms
readAPI.entries
readAPI.readers
syscallJSAPI.exeSuffix
runtime
main.BlockStmt.buf
main.BlockStmt.pkgs
main.BlockStmt.paths
mustOpen.f
api
main.BlockStmt.fmtbuf
main.BlockStmt.RangeStmt_1854.path
syscallJSAPI
syscallJSAPI.cmd
syscallJSAPI.err
exec
main.BlockStmt.f
main.BlockStmt.BlockStmt.l
readAPI.RangeStmt_2512.entry
mustOpen.err
api.base
main.BlockStmt.BlockStmt.m
readAPI.err
Members
X