code

package
v0.6.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 2, 2024 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package code is used to analyse Go code packages. It can find out all the implementation relations between package-level type.

Index

Constants

View Source
const (
	SubTask_PreparationDone = iota
	SubTask_NFilesParsed
	SubTask_ParsePackagesDone
	SubTask_CollectPackages
	SubTask_CollectModules
	SubTask_CollectExamples
	SubTask_SortPackagesByDependencies
	SubTask_CollectDeclarations
	SubTask_CollectRuntimeFunctionPositions
	//SubTask_ConfirmTypeSources
	SubTask_CollectMoreTypes
	SubTask_CollectSelectors
	SubTask_CheckCollectedSelectors
	SubTask_FindImplementations
	SubTask_RegisterInterfaceMethodsForTypes
	SubTask_MakeStatistics
	SubTask_CollectSourceFiles
	SubTask_CollectObjectReferences
	SubTask_CacheSourceFiles
)

The analysis steps.

View Source
const KindCount = reflect.UnsafePointer + 1

Variables

This section is empty.

Functions

func ComparePackagePaths added in v0.5.4

func ComparePackagePaths(pa, pb string, sep byte) bool

Should be faster than using strings.Split or Strings.Tokens Return true for pa <= pb.

func ComparePackagePaths_ThreeWay added in v0.5.4

func ComparePackagePaths_ThreeWay(pa, pb string, sep byte) int

func EmbededFieldsPath

func EmbededFieldsPath(embedding *EmbeddedField, b *strings.Builder, selName string, isField bool) (r string)

EmbededFieldsPath returns the string representation the middle embedding chain of a Selector.

func Kind

func Kind(tt types.Type) reflect.Kind

Kind returns the kinds (as reflect.Kind) for a go/types.Type.

func PrintSelectors

func PrintSelectors(title string, selectors []*Selector)

PrintSelectors prints a lists of Selectors.

Types

type AstValueSpecOwner

type AstValueSpecOwner interface {
	AstValueSpec() *ast.ValueSpec
	Package() *Package
}

AstValueSpecOwneris an interface for Variable/Constant.

type Attribute

type Attribute uint32

A Attribute records some imformations by using bits.

const (

	// Higher bits are for runtime-only flags.
	AtributesPersistentMask Attribute = (1 << 25) - 1

	// For functions, type aliases and named types.
	Builtin Attribute = 1 << 0

	// For type aliases and named types.
	Embeddable    Attribute = 1 << 1
	PtrEmbeddable Attribute = 1 << 2

	// For unnamed struct and interface types.
	HasUnexporteds Attribute = 1 << 3

	// For all types.
	Defined    Attribute = 1 << 4
	Comparable Attribute = 1 << 5

	// For channel types.
	Sendable   Attribute = 1 << 6
	Receivable Attribute = 1 << 7

	// For functions.
	Variadic Attribute = 1 << 8

	// For methods.
	StarReceiver Attribute = 1 << 9

	// For type names.
	Alias Attribute = 1 << 10
)

type CodeAnalyzer

type CodeAnalyzer struct {
	// contains filtered or unexported fields
}

CodeAnalyzer holds all the analysis results and functionalities.

func (*CodeAnalyzer) AnalyzePackages

func (d *CodeAnalyzer) AnalyzePackages(onSubTaskDone func(int, time.Duration, ...int32))

AnalyzePackages analyzes the input packages.

func (*CodeAnalyzer) BuildMethodSignatureFromFunctionSignature

func (d *CodeAnalyzer) BuildMethodSignatureFromFunctionSignature(funcSig *types.Signature, methodName string, pkgImportPath string) MethodSignature

BuildMethodSignatureFromFunctionSignature builds the signature for method function object. pkgImportPath should be only passed for unexported method names.

func (*CodeAnalyzer) BuiltinPackge

func (d *CodeAnalyzer) BuiltinPackge() *Package

BuiltinPackge returns the builtin package.

func (*CodeAnalyzer) CheckTypeMethodContributingToTypeImplementations

func (d *CodeAnalyzer) CheckTypeMethodContributingToTypeImplementations(pkg, typ, methodPkg, method string) bool

CheckTypeMethodContributingToTypeImplementations checks whether or not a method implements some interface methods.

func (*CodeAnalyzer) CleanImplements

func (d *CodeAnalyzer) CleanImplements(self *TypeInfo, includingUnnamed bool) []Implementation

CleanImplements returns a clean list of the implementions for a TypeInfo.

func (*CodeAnalyzer) ExampleFileSet added in v0.2.8

func (d *CodeAnalyzer) ExampleFileSet() *token.FileSet

func (*CodeAnalyzer) Id1

func (d *CodeAnalyzer) Id1(p *types.Package, name string) string

Id1 builds an id from the specified package and identifier name. The result is the same as go/types.Id.

func (*CodeAnalyzer) Id1b

func (d *CodeAnalyzer) Id1b(pkg *Package, name string) string

Id1b builds an id from the specified package and identifier name. The result is almost the same as go/types.Id.

func (*CodeAnalyzer) Id2

func (d *CodeAnalyzer) Id2(p *types.Package, name string) string

Id2 builds an id from the specified package and identifier name.

func (*CodeAnalyzer) Id2b

func (d *CodeAnalyzer) Id2b(pkg *Package, name string) string

Id2b builds an id from the specified package and identifier name.

func (*CodeAnalyzer) IsStandardPackage

func (d *CodeAnalyzer) IsStandardPackage(pkg *Package) bool

IsStandardPackage returns whether or not the given package is a standard package.

func (*CodeAnalyzer) IsStandardPackageByPath

func (d *CodeAnalyzer) IsStandardPackageByPath(path string) bool

IsStandardPackageByPath returns whether or not the package specified by the path is a standard package.

func (*CodeAnalyzer) IterateModule added in v0.2.7

func (d *CodeAnalyzer) IterateModule(f func(*Module))

IterateModule iterates all modules and passes them to the specified callback f.

func (*CodeAnalyzer) LookForType added in v0.2.7

func (d *CodeAnalyzer) LookForType(t types.Type) *TypeInfo

LookForType trys to find out the TypeInfo registered for the spefified types.Type.

func (*CodeAnalyzer) ModuleByPath added in v0.2.7

func (d *CodeAnalyzer) ModuleByPath(path string) *Module

ModuleByPath returns the module corresponding the specified path.

func (*CodeAnalyzer) NumPackages

func (d *CodeAnalyzer) NumPackages() int

NumPackages returns packages count.

func (*CodeAnalyzer) NumSourceFiles

func (d *CodeAnalyzer) NumSourceFiles() int

NumSourceFiles returns the source files count.

func (*CodeAnalyzer) ObjectReferences

func (d *CodeAnalyzer) ObjectReferences(obj types.Object) []Identifier

ObjectReferences returns all the references to the given object.

func (*CodeAnalyzer) PackageAt

func (d *CodeAnalyzer) PackageAt(i int) *Package

PackageAt returns the packages at specified index i.

func (*CodeAnalyzer) PackageByPath

func (d *CodeAnalyzer) PackageByPath(path string) *Package

PackageByPath returns the packages corresponding the specified path.

func (*CodeAnalyzer) ParsePackages

func (d *CodeAnalyzer) ParsePackages(onSubTaskDone func(int, time.Duration, ...int32), completeModuleInfo func(*Module), toolchain ToolchainInfo, args ...string) error

ParsePackages parses input packages.

func (*CodeAnalyzer) RegisterType

func (d *CodeAnalyzer) RegisterType(t types.Type) *TypeInfo

RegisterType registers a go/types.Type as TypeInfo.

func (*CodeAnalyzer) RegisterTypeName

func (d *CodeAnalyzer) RegisterTypeName(tn *TypeName)

RegisterTypeName registers a TypeName. Only origin ones should be registered.

func (*CodeAnalyzer) RetrieveNamedType added in v0.5.4

func (d *CodeAnalyzer) RetrieveNamedType(t *TypeInfo) (*TypeInfo, bool)

RetrieveTypeName trys to retrieve the TypeName from a TypeInfo.

func (*CodeAnalyzer) RoughExportedIdentifierCount

func (d *CodeAnalyzer) RoughExportedIdentifierCount() int32

RoughExportedIdentifierCount returns a rough number of exported identifiers.

func (*CodeAnalyzer) RoughTypeNameCount

func (d *CodeAnalyzer) RoughTypeNameCount() int32

RoughTypeNameCount returns a rough number of all type names.

func (*CodeAnalyzer) RuntimeFunctionCodePosition

func (d *CodeAnalyzer) RuntimeFunctionCodePosition(f string) token.Position

RuntimeFunctionCodePosition returns the position of the specified runtime function f.

func (*CodeAnalyzer) RuntimePackage

func (d *CodeAnalyzer) RuntimePackage() *Package

RuntimePackage returns the runtime package.

func (*CodeAnalyzer) SourceFile added in v0.2.7

func (d *CodeAnalyzer) SourceFile(pkgFile string) *SourceFileInfo

SourceFile returns the source file coresponding the specified file name. pkgFile: pkg.Path + "/" + file.BareFilename

func (*CodeAnalyzer) StandardPackage added in v0.5.4

func (d *CodeAnalyzer) StandardPackage(path string) *Package

func (*CodeAnalyzer) Statistics

func (d *CodeAnalyzer) Statistics() Stats

Statistics returns the analysis statistics data.

func (*CodeAnalyzer) WorkingDirectoryModule added in v0.2.7

func (d *CodeAnalyzer) WorkingDirectoryModule() *Module

WorkingDirectoryModule returns the module at the working directory. It might be nil.

type Constant

type Constant struct {
	*types.Const

	Type    *TypeInfo
	Pkg     *Package // some duplicated with types.Const.Pkg()
	AstDecl *ast.GenDecl
	AstSpec *ast.ValueSpec
}

Constant represents a constant.

func (*Constant) AstValueSpec

func (c *Constant) AstValueSpec() *ast.ValueSpec

AstValueSpec returns the go/ast.ValueSpec for a Constant.

func (*Constant) Comment

func (c *Constant) Comment() string

Comment returns the comment of a Constant.

func (*Constant) Documentation

func (c *Constant) Documentation() string

Documentation returns the document of a Constant.

func (*Constant) Exported

func (c *Constant) Exported() bool

Exported returns whether or not a Constant is exported.

func (*Constant) Package

func (c *Constant) Package() *Package

Package returns the owner Package of a Constant.

func (*Constant) Position

func (c *Constant) Position() token.Position

Position returns the declaration position of a Constant.

func (*Constant) TType

func (c *Constant) TType() types.Type

TType returns the go/types.Type of a Constant.

func (*Constant) TypeInfo

func (c *Constant) TypeInfo(d *CodeAnalyzer) *TypeInfo

TypeInfo returns the type of a Constant.

type EmbedMode

type EmbedMode uint8
const (
	EmbedMode_None     EmbedMode = iota
	EmbedMode_Direct             // TypeName (note: it might be a pointer alias)
	EmbedMode_Indirect           // *TypeName
)

type EmbeddedField

type EmbeddedField struct {
	*Field
	Prev *EmbeddedField
}

EmbeddedField represengts am embedded field.

type Example added in v0.2.8

type Example struct {
	// contains filtered or unexported fields
}

type Field

type Field struct {
	AstField *ast.Field

	Pkg  *Package // (nil for exported. ??? Seems not true.)
	Name string
	Type *TypeInfo

	Tag  string
	Mode EmbedMode
	// contains filtered or unexported fields
}

Field represents a struct field.

func (*Field) Comment added in v0.1.7

func (fld *Field) Comment() string

Comment returns the comment of a Field.

func (*Field) Documentation added in v0.1.7

func (fld *Field) Documentation() string

Documentation returns the documents of a Field.

func (*Field) Position

func (fld *Field) Position() token.Position

Position returns the code position for a Field.

type Function

type Function struct {
	*types.Func
	*types.Builtin // for builtin functions

	Type    *TypeInfo
	Pkg     *Package // some duplicated with types.Func.Pkg(), except builtin functions
	AstDecl *ast.FuncDecl
	// contains filtered or unexported fields
}

Function represents a function, including non-interface methods.

func (*Function) AstFuncType

func (f *Function) AstFuncType() *ast.FuncType

AstFuncType returns the go/ast.FuncType for a Function.

func (*Function) AstPackage

func (f *Function) AstPackage() *Package

AstPackage returns the same as Package().

func (*Function) Comment

func (f *Function) Comment() string

Comment always return "".

func (*Function) Documentation

func (f *Function) Documentation() string

Documentation return document of a Function.

func (*Function) Exported

func (f *Function) Exported() bool

Exported returns whether or or a Function is exported.

func (*Function) IsMethod

func (f *Function) IsMethod() bool

IsMethod returns whether or not a Function is a method.

func (*Function) Name

func (f *Function) Name() string

Names returns the name of a Function.

func (*Function) Package

func (f *Function) Package() *Package

Package returns the owner of a Function.

func (*Function) Position

func (f *Function) Position() token.Position

Position return the position of a Function.

func (*Function) ReceiverTypeName

func (f *Function) ReceiverTypeName() (paramField *ast.Field, typename *TypeName, isStar bool)

ReceiverTypeName returns the TypeName and whether or not the receiver is a pointer for a method function.

func (*Function) String

func (f *Function) String() string

String returns the string representation of a Function.

func (*Function) TType

func (f *Function) TType() types.Type

TType returns the go/types.Type for a Function.

func (*Function) TypeInfo

func (f *Function) TypeInfo(d *CodeAnalyzer) *TypeInfo

TypeInfo returns the tyoe of a Function.

type FunctionResource

type FunctionResource interface {
	ValueResource
	IsMethod() bool
	//ReceiverTypeName() (paramField *ast.Field, typeIdent *ast.Ident, isStar bool)
	ReceiverTypeName() (paramField *ast.Field, typename *TypeName, isStar bool)
	AstFuncType() *ast.FuncType

	// For *Function, the result is the same as ValueResource.Package().
	// For *InterfaceMethod, this might be different (caused by embedding, or other reasons).
	AstPackage() *Package
}

FunctionResource is an interface for Function/InterfaceMethod.

type Identifier

type Identifier struct {
	FileInfo *SourceFileInfo
	AstIdent *ast.Ident
}

Identifier represents an identifier occurrence in code.

type Implementation

type Implementation struct {
	Impler    *TypeInfo // a struct or named type (same as the owner), or a pointer to such a type
	Interface *TypeInfo // an interface type
}

Implementation represents an implementation relation.

type Import

type Import struct {
	*types.PkgName

	Pkg     *Package // some duplicated with types.PkgName.Pkg()
	AstDecl *ast.GenDecl
	AstSpec *ast.ImportSpec
}

Import represents an import.

type InstantiatedInfo added in v0.5.4

type InstantiatedInfo struct {
	TypeArgs []TypeExpr
}

type InterfaceMethod

type InterfaceMethod struct {
	InterfaceTypeName *TypeName
	//Method            *Method // .AstFunc == nil, .AstField  != nil
	Selector *Selector // .Method.AstFunc == nil,.Method .AstField  != nil

}

InterfaceMethod represents an interface function.

func (*InterfaceMethod) AstFuncType

func (im *InterfaceMethod) AstFuncType() *ast.FuncType

AstFuncType returns the go/ast.FuncType for a InterfaceMethod.

func (*InterfaceMethod) AstPackage

func (im *InterfaceMethod) AstPackage() *Package

AstPackage returns the Package where a InterfaceMethodis is specified. For embedding reason. The result might be different from the owner package.

func (*InterfaceMethod) Comment

func (im *InterfaceMethod) Comment() string

Name returns the comment of a InterfaceMethod.

func (*InterfaceMethod) Documentation

func (im *InterfaceMethod) Documentation() string

Name returns the document of a InterfaceMethod.

func (*InterfaceMethod) Exported

func (im *InterfaceMethod) Exported() bool

Name returns whether or not a InterfaceMethod is exported.

func (*InterfaceMethod) IsMethod

func (im *InterfaceMethod) IsMethod() bool

Name always returns true.

func (*InterfaceMethod) Name

func (im *InterfaceMethod) Name() string

Name returns the name of a InterfaceMethod.

func (*InterfaceMethod) Package

func (im *InterfaceMethod) Package() *Package

Name returns the owner Package of a InterfaceMethod.

func (*InterfaceMethod) Position

func (im *InterfaceMethod) Position() token.Position

Name returns the code position of a InterfaceMethod.

func (*InterfaceMethod) ReceiverTypeName

func (im *InterfaceMethod) ReceiverTypeName() (paramField *ast.Field, typename *TypeName, isStar bool)

ReceiverTypeName returns the TypeName and whether or not the receiver is a pointer for a method function.

func (*InterfaceMethod) String

func (im *InterfaceMethod) String() string

Name returns the string representation of a InterfaceMethod.

func (*InterfaceMethod) TType

func (im *InterfaceMethod) TType() types.Type

Name returns the go/types.Type for a InterfaceMethod.

func (*InterfaceMethod) TypeInfo

func (im *InterfaceMethod) TypeInfo(d *CodeAnalyzer) *TypeInfo

Name returns the type of a InterfaceMethod.

type LoadError added in v0.4.6

type LoadError struct {
	Errs []error
}

func (*LoadError) Error added in v0.4.6

func (le *LoadError) Error() string

type Method

type Method struct {
	AstFunc *ast.FuncDecl // for concrete methods
	//AstInterface *ast.InterfaceType // for interface methods (the owner interface)
	AstField *ast.Field // for interface methods

	Pkg  *Package // (nil for exported. ??? Seems not true.)
	Name string
	Type *TypeInfo // ToDo: use custom struct including PointerRecv instead.

	PointerRecv         bool // duplicated info, for faster access
	ImplementsSomething bool // false if the method is unimportant for its reveiver to implement some interface type

	//>> 1.18, ToDo
	Parameterized bool // Containing parameter types which are parameterized.
	// contains filtered or unexported fields
}

Method represent a method.

func (*Method) Comment added in v0.1.7

func (mthd *Method) Comment() string

Comment returns the comment of a Method.

func (*Method) Documentation added in v0.1.7

func (mthd *Method) Documentation() string

Documentation returns the document of a Method.

func (*Method) Position

func (mthd *Method) Position() token.Position

Position returns the code position of a Method.

type MethodSignature

type MethodSignature struct {
	Name string // must be an identifier other than "_"
	Pkg  string // the import path, for unepxorted method names only

	//InOutTypes []int32 // global type indexes
	InOutTypes string

	NumInOutAndVariadic int
}

MethodSignature represents a hashable struct for a method.

type Module

type Module struct {
	Index int // users might make some optimizations by using the index

	Dir string // might be blank for vendored modules

	Path    string
	Version string

	// ...
	Replace moduleReplacement

	RepositoryCommit string // might be the same as Version, or not.
	RepositoryURL    string
	RepositoryDir    string // no much useful

	// Generally blank. But
	// 1. "/src" for std and "/src/cmd" for toolchain.
	// 2. Some modules are not at the root of their repositories.
	//    (Often, such a repository contains multiple modules.
	//    ex., for github.com/aws/aws-sdk-go-v2/service/{ec2,s3},
	//    they are service/s3, service/ec2)
	ExtraPathInRepository string

	Pkgs []*Package // seen packages
	// contains filtered or unexported fields
}

A Module holds the information for a Go module.

func (*Module) ActualDir added in v0.2.8

func (m *Module) ActualDir() string

func (*Module) ActualPath added in v0.2.8

func (m *Module) ActualPath() string

func (*Module) ActualVersion added in v0.2.8

func (m *Module) ActualVersion() string

func (*Module) PackageByPath added in v0.5.4

func (m *Module) PackageByPath(path string) (r *Package)

ToDo: build a trie to run faster?

type Package

type Package struct {
	Index int               // ToDo: use this to do some optimizations
	PPkg  *packages.Package // ToDo: renamed to PP to be consistent with TypeInfo.TT?
	Path  string

	Deps      []*Package
	DepedBys  []*Package
	DepHeight int32 // 0 means the height is not determined yet. The order determines the parse order.
	DepDepth  int32 // 0 means the depth is not determined yet. The value mains how close to main pacakges. (Moved to user space).

	// This field might be shared with PackageForDisplay
	// for concurrent reads.
	*PackageAnalyzeResult                     // ToDo: not as pointer?
	AllResources          map[string]Resource // ToDo: use a slice to save memory
	SourceFiles           []SourceFileInfo
	ExampleFiles          []*ast.File
	Examples              []*doc.Example

	OneLineDoc string
	Directory  string
	// contains filtered or unexported fields
}

Package holds the information and the analysis result of a Go package.

func (*Package) BuildResourceLookupTable added in v0.5.4

func (pkg *Package) BuildResourceLookupTable()

ToDo: build a trie to run faster

func (*Package) ChildByPath added in v0.5.4

func (p *Package) ChildByPath(path string) *Package

func (*Package) IsFake added in v0.5.4

func (p *Package) IsFake() bool

func (*Package) Module added in v0.2.7

func (p *Package) Module() *Module

ModulePath returns the module path of a Package.

func (*Package) ModulePath added in v0.5.2

func (p *Package) ModulePath() string

ModulePath returns the module path of a Package.

func (*Package) ParentPackage added in v0.5.4

func (p *Package) ParentPackage() *Package

func (*Package) RelPathInModule added in v0.5.4

func (p *Package) RelPathInModule() string

func (*Package) SearchResourceByName added in v0.5.4

func (pkg *Package) SearchResourceByName(name string) Resource

func (*Package) SourceFileInfoByBareFilename added in v0.2.8

func (pkg *Package) SourceFileInfoByBareFilename(bareFilename string) *SourceFileInfo

SourceFileInfoByBareFilename returns the SourceFileInfo corresponding the specified bare filename.

func (*Package) SourceFileInfoByFilePath added in v0.2.8

func (pkg *Package) SourceFileInfoByFilePath(srcPath string) *SourceFileInfo

SourceFileInfoByFilePath return the SourceFileInfo corresponding the specified file path.

type PackageAnalyzeResult

type PackageAnalyzeResult struct {
	AllTypeNames []*TypeName // not including instantiated ones
	AllFunctions []*Function // not including instantiated ones
	AllVariables []*Variable
	AllConstants []*Constant
	AllImports   []*Import

	CodeLinesWithBlankLines int32
	// contains filtered or unexported fields
}

PackageAnalyzeResult holds the analysis result of a Go package.

func NewPackageAnalyzeResult

func NewPackageAnalyzeResult() *PackageAnalyzeResult

NewPackageAnalyzeResult returns a new initialized PackageAnalyzeResult.

func (*PackageAnalyzeResult) TypeNameByName added in v0.5.4

func (pr *PackageAnalyzeResult) TypeNameByName(name string) *TypeName

type Resource

type Resource interface {
	Name() string
	Exported() bool
	//IndexString() string
	Documentation() string
	Comment() string
	Position() token.Position
	Package() *Package
}

Resource is an interface for Variable/Constant/TypeName/Function/InterfaceMethod.

type Selector

type Selector struct {
	Id string

	// One and only one of the two is nil.
	*Field
	*Method

	//>> 1.18
	Instantiated *InstantiatedInfo
	RealType     *TypeInfo

	EmbeddingChain *EmbeddedField // in the inverse order
	Depth          uint16         // the chain length
	Indirect       bool           // whether the chain contains indirects or not
	// contains filtered or unexported fields
}

Selector represents a selector, either a field or a method.

func (*Selector) Name

func (s *Selector) Name() string

Name returns the name of a Selector.

func (*Selector) Object

func (s *Selector) Object() types.Object

Object returns the go/types.Object represented by a Selector.

func (*Selector) Package

func (s *Selector) Package() *Package

Package returns the owner package of a Selector.

func (*Selector) PointerReceiverOnly

func (s *Selector) PointerReceiverOnly() bool

PointerReceiverOnly returns whether or not a method selector is declared for a pointer type.

func (*Selector) Position

func (s *Selector) Position() token.Position

Position returns the code position of a Selector.

func (*Selector) Reset

func (s *Selector) Reset()

Reset clears the data for a Selector.

func (*Selector) String

func (s *Selector) String() string

String returns the string representation of a Selecctor.

func (*Selector) Type added in v0.5.4

func (s *Selector) Type() *TypeInfo

Name returns the name of a Selector.

type SelectorCond

type SelectorCond uint8
const (
	SelectorCond_Normal SelectorCond = iota
	SelectorCond_Hidden
)

type SeleterMapManager

type SeleterMapManager struct {
	// contains filtered or unexported fields
}

type SourceFileInfo

type SourceFileInfo struct {
	Pkg *Package // to remove one field in Identifier. Also good to external source line generation.

	// Filename only.
	BareFilename          string
	BareGeneratedFilename string

	// The full path of a (Go or others) source file.
	// It might be blank for some cgo generated files.
	OriginalFile string

	// The followings are blank for most files.
	GeneratedFile string

	// Non-nil for Go files.
	// If an original Go file has a corresponding generated file,
	// then the ast file is for that generated file.
	AstFile *ast.File

	// ...
	Content []byte
}

func (*SourceFileInfo) AstBareFileName

func (info *SourceFileInfo) AstBareFileName() string

type Stats

type Stats struct {
	Packages            int32
	StdPackages         int32
	AllPackageDeps      int32
	PackagesByDeps      [100]int32
	PackagesDepsTopList TopList

	FilesWithoutGenerateds int32 // without generated ones
	FilesWithGenerateds    int32 // with generated ones

	// To calculate imports per file.
	// Deps per packages are available in other ways.
	AstFiles                int32
	Imports                 int32
	FilesByImportCount      [100]int32
	FilesImportCountTopList TopList

	CodeLinesWithBlankLines           int32
	FilesByCodeLinesWithBlankLines    [100]int32
	FilesCodeLineTopList              TopList
	PackagesByCodeLinesWithBlankLines [100]int32
	PackagesCodeLineTopList           TopList

	// Types
	ExportedTypeNamesByKind    [KindCount]int32
	ExportedTypeNames          int32
	ExportedTypeAliases        int32
	ExportedCompositeTypeNames int32
	ExportedBasicTypeNames     int32
	ExportedNumericTypeNames   int32
	ExportedIntergerTypeNames  int32
	ExportedUnsignedTypeNames  int32

	//ExportedNamedStructTypeNames                        int32 // should be equal to ExportedTypeNamesByKind[reflect.Struct]
	ExportedNamedStructTypesWithEmbeddingFields   int32
	ExportedNamedStructTypesWithPromotedFields    int32
	ExportedNamedStructTypeFields                 int32
	ExportedNamedStructTypeExplicitFields         int32
	ExportedNamedStructTypeExportedFields         int32
	ExportedNamedStructTypeExportedExplicitFields int32

	ExportedNamedStructsByEmbeddingFieldCount        [100]int32
	ExportedNamedStructsEmbeddingFieldCountTopList   TopList
	ExportedNamedStructsByFieldCount                 [100]int32 // including promoteds and non-exporteds
	ExportedNamedStructsFieldCountTopList            TopList
	ExportedNamedStructsByExplicitFieldCount         [100]int32 // including non-exporteds but not including promoted
	ExportedNamedStructsExplicitFieldCountTopList    TopList
	ExportedNamedStructsByExportedFieldCount         [100]int32 // including promoteds
	ExportedNamedStructsExportedFieldCountTopList    TopList
	ExportedNamedStructsByExportedExplicitFieldCount [100]int32 // not including promoteds
	ExportedNamedStructsExportedExplicitFieldCount   TopList
	ExportedNamedStructsByExportedPromotedFieldCount [100]int32
	ExportedNamedStructsExportedPromotedFieldCount   TopList

	ExportedNamedNonInterfaceTypesByMethodCount              [100]int32 // T and *T combined
	ExportedNamedNonInterfaceTypesByExportedMethodCount      [100]int32 // T and *T combined
	ExportedNamedNonInterfaceTypesExportedMethodCountTopList TopList
	ExportedNamedNonInterfacesExportedMethods                int32
	ExportedNamedNonInterfacesWithExportedMethods            int32

	ExportedNamedInterfacesByMethodCount              [100]int32
	ExportedNamedInterfacesByExportedMethodCount      [100]int32 // the last element means (N-1)+
	ExportedNamedInterfacesExportedMethodCountTopList TopList
	ExportedNamedInterfacesExportedMethods            int32

	// Values
	ExportedVariables int32
	ExportedConstants int32
	ExportedFunctions int32
	ExportedMethods   int32 // non-interface methods

	ExportedVariablesByTypeKind [KindCount]int32
	ExportedConstantsByTypeKind [KindCount]int32

	// ToDo: Methods corresponding the same interface method should be viewed as one method in counting.
	ExportedFunctionParameters             int32      // including methods
	ExportedFunctionResults                int32      // including methods
	ExportedFunctionWithLastErrorResult    int32      // including methods
	ExportedFunctionsByParameterCount      [100]int32 // including methods
	ExportedFunctionsParameterCountTopList TopList
	ExportedFunctionsByResultCount         [100]int32 // including methods
	ExportedFunctionsResultCountTopList    TopList

	// Others.
	ExportedIdentifers             int32
	ExportedIdentifersSumLength    int32
	ExportedIdentifiersByLength    [100]int32
	ExportedIdentiferLengthTopList TopList
	// contains filtered or unexported fields
}

A Stats hold all the analysis statistics data.

type ToolchainInfo added in v0.3.2

type ToolchainInfo struct {
	// Three paths
	Root, Src, Cmd string

	// A commit hash or something like "go1.16".
	// ToDo: now lso might be blank, but need some handling ...
	Version string
}

type TopList added in v0.2.0

type TopList struct {
	Criteria int
	Items    []interface{}
}

A TopList specifies the minimu criteria for a top list and hold the top list items.

func (*TopList) Push added in v0.2.0

func (tl *TopList) Push(n int, obj interface{})

Push trys to add a new top item.

func (*TopList) TryToInit added in v0.2.0

func (tl *TopList) TryToInit(n int)

TryToInit inits a TopList if it has not been.

type TypeExpr added in v0.5.4

type TypeExpr struct {
	Expr ast.Expr
	Type *TypeInfo
	Pkg  *Package
}

type TypeInfo

type TypeInfo struct {
	TT types.Type

	// ToDo:
	Exprs []ast.Expr

	Underlying *TypeInfo

	//>> 1.18
	// For named types only.
	// For an origin named type, Instantiated is always nil, and this == TypeName.Denoting.
	// For an instantiated type, Instantiated should not be nil, and this != TypeName.Denoting.
	// But for some diffcualies, the Instantiated field if an instantiated type is nil,
	// in which case, the selectors of the instantiated type is incomplete.
	//
	// ToDo: the Origin field might be useless. It is this if TypeName is nil, otherwise, it is TypeName.Denoting.
	//Origin *TypeInfo
	TypeName     *TypeName
	Instantiated *InstantiatedInfo

	//Implements     []*TypeInfo
	//StarImplements []*TypeInfo // if TT is neither pointer nor interface.
	Implements []Implementation

	// For interface types.
	ImplementedBys []*TypeInfo

	//
	Aliases []*TypeName

	// ToDo: For unnamed and builtin basic types.
	Underlieds []*TypeName

	// Explicit fields and methods.
	// * For named types, only explicitly declared methods are included.
	//   The field is only built for T. (*T).DirectSelectors is always nil.
	// * For named interface types, all explicitly specified methods and embedded types (as fields).
	// * For unnamed struct types, only direct fields. Only built for strct{...}, not for *struct{...}.
	// ToDo: note, for a type may have many alias, the ast.Expr (for types)
	//       recorded in a Selector is just one of many possibilities.
	DirectSelectors []*Selector
	EmbeddingFields int32 // for struct types only now. ToDo: also for interface types.

	// All methods, including extended/promoted ones.
	AllMethods []*Selector

	// All fields, including promoted ones.
	AllFields []*Selector

	// Ror rendering links in doc comments.
	// Built as neede.
	AllSelectors map[string]*Selector

	// For .TypeName != nil
	AsTypesOf   []ValueResource // variables and constants
	AsInputsOf  []ValueResource // variables and functions
	AsOutputsOf []ValueResource // variables and functions
	// contains filtered or unexported fields
}

TypeInfo represents a type and records its analysis result.

func (*TypeInfo) Kind

func (t *TypeInfo) Kind() reflect.Kind

Kind returns the kinds (as reflect.Kind) of a type.

func (*TypeInfo) SelectorByName added in v0.5.4

func (t *TypeInfo) SelectorByName(name string) *Selector

type TypeName

type TypeName struct {
	Pkg     *Package // some duplicated with types.TypeName.Pkg(), except builtin types
	AstDecl *ast.GenDecl
	AstSpec *ast.TypeSpec

	// For alias TypeName, Source.Type is the denoting type.
	Source TypeExpr

	TypeParams []TypeExpr

	// It this is an alias, then Denoting is also the Source type.
	Denoting *TypeInfo

	*types.TypeName
	// contains filtered or unexported fields
}

func (*TypeName) Comment

func (tn *TypeName) Comment() string

Comment returns the comment of a TypeName.

func (*TypeName) Documentation

func (tn *TypeName) Documentation() string

Documentation returns the documents of a TypeName.

func (*TypeName) Exported

func (tn *TypeName) Exported() bool

Exported returns whether or not a TypeName is exported.

func (*TypeName) IsAlias added in v0.5.4

func (tn *TypeName) IsAlias() bool

func (*TypeName) Package

func (tn *TypeName) Package() *Package

Package returns the owner Package of a TypeName.

func (*TypeName) Position

func (tn *TypeName) Position() token.Position

Position returns the declaration position of a TypeName.

type ValueResource

type ValueResource interface {
	Resource
	TType() types.Type // The result should not be used in comparisons.
	TypeInfo(d *CodeAnalyzer) *TypeInfo
}

ValueResource is an interface for Variable/Constant/Function/InterfaceMethod..

type Variable

type Variable struct {
	*types.Var

	Type    *TypeInfo
	Pkg     *Package // some duplicated with types.Var.Pkg()
	AstDecl *ast.GenDecl
	AstSpec *ast.ValueSpec
}

Variable represents a variable.

func (*Variable) AstValueSpec

func (v *Variable) AstValueSpec() *ast.ValueSpec

AstValueSpec returns the go/ast.ValueSpec for a Variable.

func (*Variable) Comment

func (v *Variable) Comment() string

Comment returns the comment of a Variable.

func (*Variable) Documentation

func (v *Variable) Documentation() string

Documentation returns the document of a Variable.

func (*Variable) Exported

func (v *Variable) Exported() bool

Exported returns whether or not a Variable is exported.

func (*Variable) Package

func (v *Variable) Package() *Package

Package returns the owner package of a Variable.

func (*Variable) Position

func (v *Variable) Position() token.Position

Position returns the position in code for a Variable.

func (*Variable) TType

func (v *Variable) TType() types.Type

TType returns the go/types.Type for a Variable.

func (*Variable) TypeInfo

func (v *Variable) TypeInfo(d *CodeAnalyzer) *TypeInfo

TypeInfo returns the type of a Variable.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL