Source file src/cmd/vendor/golang.org/x/tools/internal/stdlib/stdlib.go

     1  // Copyright 2022 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:generate go run generate.go
     6  
     7  // Package stdlib provides a table of all exported symbols in the
     8  // standard library, along with the version at which they first
     9  // appeared.
    10  package stdlib
    11  
    12  import (
    13  	"fmt"
    14  	"strings"
    15  )
    16  
    17  type Symbol struct {
    18  	Name    string
    19  	Kind    Kind
    20  	Version Version // Go version that first included the symbol
    21  }
    22  
    23  // A Kind indicates the kind of a symbol:
    24  // function, variable, constant, type, and so on.
    25  type Kind int8
    26  
    27  const (
    28  	Invalid Kind = iota // Example name:
    29  	Type                // "Buffer"
    30  	Func                // "Println"
    31  	Var                 // "EOF"
    32  	Const               // "Pi"
    33  	Field               // "Point.X"
    34  	Method              // "(*Buffer).Grow"
    35  )
    36  
    37  func (kind Kind) String() string {
    38  	return [...]string{
    39  		Invalid: "invalid",
    40  		Type:    "type",
    41  		Func:    "func",
    42  		Var:     "var",
    43  		Const:   "const",
    44  		Field:   "field",
    45  		Method:  "method",
    46  	}[kind]
    47  }
    48  
    49  // A Version represents a version of Go of the form "go1.%d".
    50  type Version int8
    51  
    52  // String returns a version string of the form "go1.23", without allocating.
    53  func (v Version) String() string { return versions[v] }
    54  
    55  var versions [30]string // (increase constant as needed)
    56  
    57  func init() {
    58  	for i := range versions {
    59  		versions[i] = fmt.Sprintf("go1.%d", i)
    60  	}
    61  }
    62  
    63  // HasPackage reports whether the specified package path is part of
    64  // the standard library's public API.
    65  func HasPackage(path string) bool {
    66  	_, ok := PackageSymbols[path]
    67  	return ok
    68  }
    69  
    70  // SplitField splits the field symbol name into type and field
    71  // components. It must be called only on Field symbols.
    72  //
    73  // Example: "File.Package" -> ("File", "Package")
    74  func (sym *Symbol) SplitField() (typename, name string) {
    75  	if sym.Kind != Field {
    76  		panic("not a field")
    77  	}
    78  	typename, name, _ = strings.Cut(sym.Name, ".")
    79  	return
    80  }
    81  
    82  // SplitMethod splits the method symbol name into pointer, receiver,
    83  // and method components. It must be called only on Method symbols.
    84  //
    85  // Example: "(*Buffer).Grow" -> (true, "Buffer", "Grow")
    86  func (sym *Symbol) SplitMethod() (ptr bool, recv, name string) {
    87  	if sym.Kind != Method {
    88  		panic("not a method")
    89  	}
    90  	recv, name, _ = strings.Cut(sym.Name, ".")
    91  	recv = recv[len("(") : len(recv)-len(")")]
    92  	ptr = recv[0] == '*'
    93  	if ptr {
    94  		recv = recv[len("*"):]
    95  	}
    96  	return
    97  }
    98  

View as plain text