GoPLS Viewer

Home|gopls/go/gcexportdata/main.go
1// Copyright 2017 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// The gcexportdata command is a diagnostic tool that displays the
9// contents of gc export data files.
10package main
11
12import (
13    "flag"
14    "fmt"
15    "go/token"
16    "go/types"
17    "log"
18    "os"
19
20    "golang.org/x/tools/go/gcexportdata"
21    "golang.org/x/tools/go/types/typeutil"
22)
23
24var packageFlag = flag.String("package""""alternative package to print")
25
26func main() {
27    log.SetPrefix("gcexportdata: ")
28    log.SetFlags(0)
29    flag.Usage = func() {
30        fmt.Fprintln(os.Stderr"usage: gcexportdata [-package path] file.a")
31    }
32    flag.Parse()
33    if flag.NArg() != 1 {
34        flag.Usage()
35        os.Exit(2)
36    }
37    filename := flag.Args()[0]
38
39    ferr := os.Open(filename)
40    if err != nil {
41        log.Fatal(err)
42    }
43
44    rerr := gcexportdata.NewReader(f)
45    if err != nil {
46        log.Fatalf("%s: %s"filenameerr)
47    }
48
49    // Decode the package.
50    const primary = "<primary>"
51    imports := make(map[string]*types.Package)
52    fset := token.NewFileSet()
53    pkgerr := gcexportdata.Read(rfsetimportsprimary)
54    if err != nil {
55        log.Fatalf("%s: %s"filenameerr)
56    }
57
58    // Optionally select an indirectly mentioned package.
59    if *packageFlag != "" {
60        pkg = imports[*packageFlag]
61        if pkg == nil {
62            fmt.Fprintf(os.Stderr"export data file %s does not mention %s; has:\n",
63                filename, *packageFlag)
64            for p := range imports {
65                if p != primary {
66                    fmt.Fprintf(os.Stderr"\t%s\n"p)
67                }
68            }
69            os.Exit(1)
70        }
71    }
72
73    // Print all package-level declarations, including non-exported ones.
74    fmt.Printf("package %s\n"pkg.Name())
75    for _imp := range pkg.Imports() {
76        fmt.Printf("import %q\n"imp.Path())
77    }
78    qual := func(p *types.Packagestring {
79        if pkg == p {
80            return ""
81        }
82        return p.Name()
83    }
84    scope := pkg.Scope()
85    for _name := range scope.Names() {
86        obj := scope.Lookup(name)
87        fmt.Printf("%s: %s\n",
88            fset.Position(obj.Pos()),
89            types.ObjectString(objqual))
90
91        // For types, print each method.
92        if _ok := obj.(*types.TypeName); ok {
93            for _method := range typeutil.IntuitiveMethodSet(obj.Type(), nil) {
94                fmt.Printf("%s: %s\n",
95                    fset.Position(method.Obj().Pos()),
96                    types.SelectionString(methodqual))
97            }
98        }
99    }
100}
101
MembersX
flag
typeutil
main.err
main.RangeStmt_1669.imp
main.f
main.primary
main.pkg
main.RangeStmt_1866.name
main.RangeStmt_1866.BlockStmt.obj
main.fset
main.RangeStmt_1866.BlockStmt.BlockStmt.RangeStmt_2100.method
main
main.r
main.imports
main.BlockStmt.BlockStmt.RangeStmt_1436.p
main.scope
Members
X