code

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2020 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SubTask_PreparationDone = iota
	SubTask_NFilesParsed
	SubTask_ParsePackagesDone
	SubTask_CollectPackages
	SubTask_SortPackagesByDependencies
	SubTask_CollectDeclarations
	SubTask_CollectRuntimeFunctionPositions
	SubTask_FindTypeSources
	SubTask_CollectSelectors
	SubTask_CheckCollectedSelectors
	SubTask_FindImplementations
	SubTask_RegisterInterfaceMethodsForTypes
	SubTask_MakeStatistics
	SubTask_CollectSourceFiles
	SubTask_CollectObjectReferences
	SubTask_CacheSourceFiles
)
View Source
const KindCount = reflect.UnsafePointer + 1

Variables

This section is empty.

Functions

func EmbededFieldsPath

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

func Kind added in v0.0.3

func Kind(tt types.Type) reflect.Kind

func PrintSelectors

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

Types

type AstNode

type AstNode struct {
	Pkg  *Package
	Node ast.Node
}

type AstValueSpecOwner

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

type Attribute

type Attribute uint32
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 funcitons.
	Variadic Attribute = 1 << 8
)

type CodeAnalyzer

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

func (*CodeAnalyzer) AnalyzePackages

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

func (*CodeAnalyzer) BuildCgoFileMappings

func (d *CodeAnalyzer) BuildCgoFileMappings(pkg *Package)

func (*CodeAnalyzer) BuildMethodSignatureFromFuncObject

func (d *CodeAnalyzer) BuildMethodSignatureFromFuncObject(funcObj *types.Func) MethodSignature

func (*CodeAnalyzer) BuildMethodSignatureFromFunctionSignature

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

pkgImportPath should be only passed for unexported method names.

func (*CodeAnalyzer) BuiltinPackge

func (d *CodeAnalyzer) BuiltinPackge() *Package

func (*CodeAnalyzer) CacheSourceFiles added in v0.1.0

func (d *CodeAnalyzer) CacheSourceFiles()

ToDo: can we get the content from the collected AST files?

Need to hack the std packages?

func (*CodeAnalyzer) CheckTypeMethodContributingToTypeImplementations added in v0.0.8

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

typeIndex must be the index of a non-interface type.

func (*CodeAnalyzer) CleanImplements

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

func (*CodeAnalyzer) CollectIdentiferFromFile added in v0.1.0

func (d *CodeAnalyzer) CollectIdentiferFromFile(pkg *Package, fileInfo *SourceFileInfo)

func (*CodeAnalyzer) CollectObjectReferences added in v0.1.0

func (d *CodeAnalyzer) CollectObjectReferences()

func (*CodeAnalyzer) CollectSourceFiles

func (d *CodeAnalyzer) CollectSourceFiles()

func (*CodeAnalyzer) Id1

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

func (*CodeAnalyzer) Id1b

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

func (*CodeAnalyzer) Id2

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

func (*CodeAnalyzer) Id2b

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

func (*CodeAnalyzer) IsStandardPackage

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

func (*CodeAnalyzer) IsStandardPackageByPath

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

ToDo: add Standard field.

func (*CodeAnalyzer) NumPackages

func (d *CodeAnalyzer) NumPackages() int

func (*CodeAnalyzer) NumSourceFiles

func (d *CodeAnalyzer) NumSourceFiles() int

func (*CodeAnalyzer) ObjectReferences added in v0.1.0

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

Package by package, file by file, by positions in source.

func (*CodeAnalyzer) OriginalGoSourceFile

func (d *CodeAnalyzer) OriginalGoSourceFile(filename string) string

func (*CodeAnalyzer) PackageAt

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

func (*CodeAnalyzer) PackageByPath

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

ToDo: remove the second result

func (*CodeAnalyzer) ParsePackages

func (d *CodeAnalyzer) ParsePackages(onSubTaskDone func(int, time.Duration, ...int32), args ...string) bool

func (*CodeAnalyzer) RegisterFunction

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

Declared functions.

func (*CodeAnalyzer) RegisterType

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

func (*CodeAnalyzer) RegisterTypeName

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

The registered name must be a package-level name.

func (*CodeAnalyzer) RetrieveTypeName

func (d *CodeAnalyzer) RetrieveTypeName(t *TypeInfo) (*TypeName, bool)

func (*CodeAnalyzer) RoughExportedIdentifierCount added in v0.0.8

func (d *CodeAnalyzer) RoughExportedIdentifierCount() int32

func (*CodeAnalyzer) RoughTypeNameCount added in v0.0.8

func (d *CodeAnalyzer) RoughTypeNameCount() int32

func (*CodeAnalyzer) RuntimeFunctionCodePosition

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

func (*CodeAnalyzer) RuntimePackage

func (d *CodeAnalyzer) RuntimePackage() *Package

func (*CodeAnalyzer) SourceFile2Package

func (d *CodeAnalyzer) SourceFile2Package(path string) (*Package, bool)

func (*CodeAnalyzer) Statistics

func (d *CodeAnalyzer) Statistics() Stats

func (*CodeAnalyzer) TryRegisteringType

func (d *CodeAnalyzer) TryRegisteringType(t types.Type, createOnNonexist bool) *TypeInfo

type Constant

type Constant struct {
	*types.Const

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

func (*Constant) AstValueSpec

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

func (*Constant) Comment

func (c *Constant) Comment() string

func (*Constant) Documentation

func (c *Constant) Documentation() string

func (*Constant) Exported

func (c *Constant) Exported() bool

func (*Constant) Package

func (c *Constant) Package() *Package

func (*Constant) Position

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

func (*Constant) TType

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

func (*Constant) TypeInfo

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

type EmbedMode

type EmbedMode uint8
const (
	EmbedMode_None EmbedMode = iota
	EmbedMode_Direct
	EmbedMode_Indirect
)

type EmbeddedField

type EmbeddedField struct {
	*Field
	Prev *EmbeddedField
}

type Field

type Field struct {
	AstField     *ast.Field
	AstInterface *ast.InterfaceType // for embedding interface in interface (the owner interface)

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

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

func (*Field) Position

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

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
}

func (*Function) AstFuncType added in v0.0.4

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

func (*Function) AstPackage added in v0.0.4

func (f *Function) AstPackage() *Package

func (*Function) Comment

func (f *Function) Comment() string

func (*Function) Documentation

func (f *Function) Documentation() string

func (*Function) Exported

func (f *Function) Exported() bool

func (*Function) IsMethod

func (f *Function) IsMethod() bool

func (*Function) Name

func (f *Function) Name() string

func (*Function) Package

func (f *Function) Package() *Package

func (*Function) Position

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

func (*Function) ReceiverTypeName

func (f *Function) ReceiverTypeName() (paramField *ast.Field, typeIdent *ast.Ident, isStar bool)

Please make sure the Funciton is a method when calling this method.

func (*Function) String

func (f *Function) String() string

func (*Function) TType

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

func (*Function) TypeInfo

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

type FunctionResource added in v0.0.4

type FunctionResource interface {
	ValueResource
	IsMethod() bool
	ReceiverTypeName() (paramField *ast.Field, typeIdent *ast.Ident, 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
}

type Identifier added in v0.1.0

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

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
}

type Import

type Import struct {
	*types.PkgName

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

type InterfaceMethod added in v0.0.4

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

}

As Function.

func (*InterfaceMethod) AstFuncType added in v0.0.4

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

func (*InterfaceMethod) AstPackage added in v0.0.4

func (im *InterfaceMethod) AstPackage() *Package

func (*InterfaceMethod) Comment added in v0.0.4

func (im *InterfaceMethod) Comment() string

func (*InterfaceMethod) Documentation added in v0.0.4

func (im *InterfaceMethod) Documentation() string

func (*InterfaceMethod) Exported added in v0.0.4

func (im *InterfaceMethod) Exported() bool

func (*InterfaceMethod) IsMethod added in v0.0.4

func (im *InterfaceMethod) IsMethod() bool

func (*InterfaceMethod) Name added in v0.0.4

func (im *InterfaceMethod) Name() string

func (*InterfaceMethod) Package added in v0.0.4

func (im *InterfaceMethod) Package() *Package

func (*InterfaceMethod) Position added in v0.0.4

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

func (*InterfaceMethod) ReceiverTypeName added in v0.0.4

func (im *InterfaceMethod) ReceiverTypeName() (paramField *ast.Field, typeIdent *ast.Ident, isStar bool)

Please make sure the Funciton is a method when calling this method.

func (*InterfaceMethod) String added in v0.0.4

func (im *InterfaceMethod) String() string

func (*InterfaceMethod) TType added in v0.0.4

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

func (*InterfaceMethod) TypeInfo added in v0.0.4

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

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
	// contains filtered or unexported fields
}

func (*Method) Position

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

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
}

type Module

type Module struct {
	Dir     string
	Root    string // root import path
	Version string
}

type Package

type Package struct {
	Index int
	PPkg  *packages.Package // ToDo: renamed to PP to be consistent with TypeInfo.TT?

	Mod      *Module
	Deps     []*Package
	DepLevel int // 0 means the level is not determined yet
	DepedBys []*Package

	// This field might be shared with PackageForDisplay
	// for concurrent reads.
	*PackageAnalyzeResult
}

func (*Package) Path

func (p *Package) Path() string

type PackageAnalyzeResult

type PackageAnalyzeResult struct {
	AllTypeNames []*TypeName
	AllFunctions []*Function
	AllVariables []*Variable
	AllConstants []*Constant
	AllImports   []*Import
	SourceFiles  []SourceFileInfo
	Directory    string
}

func NewPackageAnalyzeResult

func NewPackageAnalyzeResult() *PackageAnalyzeResult

func (*PackageAnalyzeResult) SourceFileInfoByBareFilename

func (r *PackageAnalyzeResult) SourceFileInfoByBareFilename(bareFilename string) *SourceFileInfo

func (*PackageAnalyzeResult) SourceFileInfoByFilePath

func (r *PackageAnalyzeResult) SourceFileInfoByFilePath(srcPath string) *SourceFileInfo

ToDo: better to maintain a global sourceFilePath => SourceFileInfo table? func (r *PackageAnalyzeResult) SourceFileInfo(srcPath string) *SourceFileInfo {

type RefPos

type RefPos struct {
	Pkg *Package
	Pos token.Pos
}

type Resource

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

type SelectListManager

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

func NewSelectListManager

func NewSelectListManager() *SelectListManager

type Selector

type Selector struct {
	Id string

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

	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
}

func (*Selector) Name

func (s *Selector) Name() string

func (*Selector) Object added in v0.1.0

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

func (*Selector) Package added in v0.1.0

func (s *Selector) Package() *Package

ToDo: change to Package() for consistency.

func (*Selector) PointerReceiverOnly

func (s *Selector) PointerReceiverOnly() bool

func (*Selector) Position

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

func (*Selector) Reset

func (s *Selector) Reset()

func (*Selector) String

func (s *Selector) String() string

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.

	// 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 added in v0.1.0

func (info *SourceFileInfo) AstBareFileName() string

type Stats

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

	FilesWithoutGenerateds int32 // without generated ones
	FilesWithGenerateds    int32 // with generated ones
	CodeLines              int32 // ToDo: stat code lines. Use the info in AstFile?
	BlankCodeLines         int32
	CommentCodeLines       int32

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

	// 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
	ExportedNamedStructsByFieldCount                 [100]int32 // including promoteds and non-exporteds
	ExportedNamedStructsByExplicitFieldCount         [100]int32 // including non-exporteds but not including promoted
	ExportedNamedStructsByExportedFieldCount         [100]int32 // including promoteds
	ExportedNamedStructsByExportedExplicitFieldCount [100]int32 // not including promoteds
	ExportedNamedStructsByExportedPromotedFieldCount [100]int32

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

	ExportedNamedInterfacesByMethodCount         [100]int32
	ExportedNamedInterfacesByExportedMethodCount [100]int32 // the last element means (N-1)+
	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
	ExportedFunctionsByResultCount      [100]int32 // including methods

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

type TypeAlias

type TypeAlias struct {
	Denoting *TypeInfo

	// For named and basic types.
	TypeName *TypeName
	// contains filtered or unexported fields
}

type TypeInfo

type TypeInfo struct {
	TT types.Type

	Underlying *TypeInfo

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

	// For interface types.
	ImplementedBys []*TypeInfo

	// For builtin and unnamed types only.
	Aliases []*TypeAlias // ToDo:

	// For named and basic types.
	TypeName *TypeName

	// For unnamed types.
	UsePositions []token.Position

	// 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{...}.
	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

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

func (*TypeInfo) Kind

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

type TypeName

type TypeName struct {
	// One and only one of the two is nil.
	Alias *TypeAlias
	Named *TypeInfo

	// ToDo: simplify the source defintion.
	// Four kinds of sources to affect promoted selectors:
	// 1. typename
	// 2. *typename
	// 3. unnamed type
	// 4. *unname type
	Source     TypeSource
	StarSource *TypeSource

	UsePositions []token.Position

	*types.TypeName

	Pkg     *Package // some duplicated with types.TypeName.Pkg(), except builtin types
	AstDecl *ast.GenDecl
	AstSpec *ast.TypeSpec
	// contains filtered or unexported fields
}

func (*TypeName) Comment

func (tn *TypeName) Comment() string

func (*TypeName) Denoting

func (tn *TypeName) Denoting() *TypeInfo

func (*TypeName) Documentation

func (tn *TypeName) Documentation() string

func (*TypeName) Exported

func (tn *TypeName) Exported() bool

func (*TypeName) Package

func (tn *TypeName) Package() *Package

func (*TypeName) Position

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

type TypeSource

type TypeSource struct {
	TypeName    *TypeName
	UnnamedType *TypeInfo
}

type ValueResource

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

type Variable

type Variable struct {
	*types.Var

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

func (*Variable) AstValueSpec

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

func (*Variable) Comment

func (v *Variable) Comment() string

func (*Variable) Documentation

func (v *Variable) Documentation() string

func (*Variable) Exported

func (v *Variable) Exported() bool

func (*Variable) Package

func (v *Variable) Package() *Package

func (*Variable) Position

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

func (*Variable) TType

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

func (*Variable) TypeInfo

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

Jump to

Keyboard shortcuts

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