Help us shape the future of Go! Take the 2026 Go Developer Survey

typresolve

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package typresolve provides shared infrastructure for in-process language-specific type resolution. It includes type representation, a type registry, scope chains, and the Resolver interface that per-language resolvers implement.

This package replaces external LSP servers for type resolution, running inside the same process as tree-sitter extraction and sharing the parsed AST directly.

Package typresolve defines the Resolver interface for per-language in-process type resolution and the ResolverEnricher orchestrator that dispatches resolution concurrently across files. Language-specific resolvers (Go, Python, TypeScript, etc.) implement the Resolver interface; the enricher groups files by language, initializes each resolver's workspace, and fans out file resolution with bounded concurrency.

Index

Constants

View Source
const ProvenanceResolverResolved = "resolver_resolved"

ProvenanceResolverResolved is the provenance string for edges produced by in-process type resolution, distinct from "lsp_resolved" edges produced by the external LSP enricher.

View Source
const ResolverConfidence = 0.9

ResolverConfidence is the default confidence for resolver-produced edges.

Variables

This section is empty.

Functions

func RouteEnrichment

func RouteEnrichment(ctx context.Context, store types.GraphStore,
	workspaceRoot string, repoHash types.Hash,
	resolverEnricher *ResolverEnricher,
	lspEnricher LspEnricher,
	fileResults []FileResult) error

RouteEnrichment dispatches file results to either the in-process resolver or the external LSP enricher based on which languages have registered resolvers. Languages with a resolver go through resolverEnricher.Run; languages without one fall back to lspEnricher.Run (if non-nil).

Resolver errors do not prevent the LSP fallback from running. The first error encountered (resolver or LSP) is returned.

func TypesEqual

func TypesEqual(a, b *Type) bool

TypesEqual performs structural equality comparison of two types. Two types are equal if their kinds match and all structural members match recursively. Both nil returns true; one nil returns false.

Types

type ChanDir

type ChanDir int

ChanDir represents the direction of a channel type.

const (
	ChanBidi ChanDir = iota // chan T
	ChanSend                // chan<- T
	ChanRecv                // <-chan T
)

type Field

type Field struct {
	Name string
	Type *Type
}

Field represents a struct field.

type FileResult

type FileResult struct {
	Path       string
	FileHash   types.Hash
	Content    []byte
	ParsedTree types.ParsedTree
	Edges      []types.Edge
	Language   string
}

FileResult wraps extraction output for a single file, used as input to the ResolverEnricher.

type LanguageStats

type LanguageStats struct {
	Language    string
	FileCount   int
	UseResolver bool
}

LanguageStats reports the routing decision for a single language.

func RouteStats

func RouteStats(resolverEnricher *ResolverEnricher, fileResults []FileResult) []LanguageStats

RouteStats returns per-language routing decisions without executing. Useful for logging and testing.

type LspEnricher

type LspEnricher interface {
	Run(ctx context.Context, repoHash types.Hash) error
}

LspEnricher is the interface the existing enrichment.Enricher satisfies. Defined here rather than importing internal/enrichment to avoid circular dependencies. The enricher's Run method has the signature:

Run(ctx context.Context, repoHash types.Hash) error

type Method

type Method struct {
	Name      string
	Signature *Type // KindFunc
}

Method represents an interface method.

type Param

type Param struct {
	Name string
	Type *Type
}

Param represents a function parameter.

type RegisteredFunc

type RegisteredFunc struct {
	QualifiedName string
	ReceiverType  string // empty for free functions
	ShortName     string
	Signature     *Type    // KindFunc
	TypeParams    []string // generic type param names
	MinParams     int      // -1 = unknown
}

RegisteredFunc represents a function or method registered in the type registry. For methods, ReceiverType holds the qualified name of the receiver type.

type RegisteredType

type RegisteredType struct {
	QualifiedName string
	ShortName     string
	Fields        []Field
	MethodNames   []string // parallel with MethodQNs
	MethodQNs     []string // parallel with MethodNames
	EmbeddedTypes []string
	AliasOf       string // empty if not alias
	TypeParams    []string
	IsInterface   bool
}

RegisteredType represents a type registered in the type registry. MethodNames and MethodQNs are parallel arrays: MethodNames[i] is the short name and MethodQNs[i] is the qualified name of the same method.

type Registry

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

Registry provides hash-indexed lookup tables for functions and types by qualified name. It supports fallback chaining for two-level lookup (e.g. local workspace registry falling back to a stdlib registry).

Registry is NOT thread-safe for writes. Build it single-threaded during InitWorkspace, then use it read-only for concurrent ResolveFile calls.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty registry with initialized maps.

func (*Registry) AddFunc

func (r *Registry) AddFunc(f RegisteredFunc)

AddFunc registers a function by its qualified name. If the function has a ReceiverType, it is also indexed under "ReceiverType.ShortName" in the methods map for method lookup.

func (*Registry) AddType

func (r *Registry) AddType(t RegisteredType)

AddType registers a type by its qualified name.

func (*Registry) FuncCount

func (r *Registry) FuncCount() int

FuncCount returns the number of functions registered locally (not including fallback).

func (*Registry) IterFuncsByShortName

func (r *Registry) IterFuncsByShortName(shortName string, fn func(*RegisteredFunc) bool)

IterFuncsByShortName calls fn for every registered function whose ShortName matches shortName. Searches this registry and all fallbacks. Used as a last-resort resolution strategy when qualified-name lookup fails.

func (*Registry) LookupFunc

func (r *Registry) LookupFunc(qualifiedName string) *RegisteredFunc

LookupFunc looks up a function by qualified name. Returns nil if not found in this registry or any fallback.

func (*Registry) LookupMethod

func (r *Registry) LookupMethod(receiverQN, methodName string) *RegisteredFunc

LookupMethod looks up a method by receiver qualified name and method name. The key is "receiverQN.methodName". Returns nil if not found.

func (*Registry) LookupSymbol

func (r *Registry) LookupSymbol(packageQN, name string) *RegisteredFunc

LookupSymbol looks up a function by constructing "packageQN.name" and searching the funcs map. Returns nil if not found.

func (*Registry) LookupType

func (r *Registry) LookupType(qualifiedName string) *RegisteredType

LookupType looks up a type by qualified name. Returns nil if not found in this registry or any fallback.

func (*Registry) ResolveAlias

func (r *Registry) ResolveAlias(typeQN string) *RegisteredType

ResolveAlias follows the AliasOf chain on types up to 16 levels deep. Returns the concrete (non-alias) type at the end of the chain, or nil if the type is not found or the chain exceeds 16 levels (cycle detection).

func (*Registry) SetFallback

func (r *Registry) SetFallback(parent *Registry)

SetFallback sets the fallback (parent) registry for chained lookups. When a lookup misses in this registry, it falls through to the fallback.

func (*Registry) TypeCount

func (r *Registry) TypeCount() int

TypeCount returns the number of types registered locally (not including fallback).

type ResolveFileOpts

type ResolveFileOpts struct {
	FilePath   string
	FileHash   types.Hash
	Content    []byte
	ParsedTree types.ParsedTree
	Edges      []types.Edge
}

ResolveFileOpts contains all inputs for resolving a single file.

type Resolver

type Resolver interface {
	// Language returns the language identifier (e.g. "go", "python").
	Language() string

	// InitWorkspace builds the cross-file type registry from all extracted
	// definitions. Called once before any ResolveFile calls. The registry
	// must be read-only after this returns.
	InitWorkspace(ctx context.Context, defs []ResolverDef) error

	// ResolveFile resolves type references in a single file. Thread-safe;
	// called concurrently across files. Returns edges with provenance
	// ProvenanceResolverResolved and confidence ResolverConfidence.
	ResolveFile(ctx context.Context, opts ResolveFileOpts) ([]types.Edge, error)
}

Resolver is the contract that per-language in-process type resolvers implement. Language() identifies the resolver; InitWorkspace builds a cross-file type registry from extracted definitions (called once, after which the registry is read-only); ResolveFile resolves a single file and is safe for concurrent use.

type ResolverDef

type ResolverDef struct {
	QualifiedName string
	Kind          string // "function", "type", "method", etc.
	Signature     string
	FilePath      string
	PackagePath   string
}

ResolverDef contains definition metadata collected from extraction, used to build the per-language type registry in InitWorkspace.

type ResolverEnricher

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

ResolverEnricher orchestrates per-language type resolution. It groups files by language, initializes each registered resolver, and resolves files concurrently using bounded concurrency with a single DB-writer goroutine.

func NewResolverEnricher

func NewResolverEnricher(store types.GraphStore, concurrency int) *ResolverEnricher

NewResolverEnricher creates a ResolverEnricher backed by the given store. If concurrency is 0 or negative, it defaults to 8.

func (*ResolverEnricher) HasResolver

func (re *ResolverEnricher) HasResolver(language string) bool

HasResolver reports whether a resolver is registered for the given language.

func (*ResolverEnricher) Register

func (re *ResolverEnricher) Register(r Resolver)

Register adds a language-specific resolver. Subsequent calls with the same Language() value overwrite the previous registration.

func (*ResolverEnricher) Run

func (re *ResolverEnricher) Run(ctx context.Context, repoHash types.Hash, fileResults []FileResult) error

Run executes type resolution for all file results. It groups files by language, then for each language with a registered resolver:

  1. Collects ResolverDef entries from the file results
  2. Calls InitWorkspace with the collected definitions
  3. Resolves files concurrently via a channel semaphore
  4. Writes edges through a single writer goroutine calling store.PutEdge

Errors in individual files or languages are logged but do not abort the entire run.

type Scope

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

Scope is a linked-list scope chain for variable-to-type bindings during AST walk. Each scope level holds its own bindings map and a pointer to the enclosing (parent) scope. Scope is NOT thread-safe; it is used within a single ResolveFile call (one goroutine per file).

This is the Go equivalent of CBMScope from codebase-memory's scope.c, simplified to use map[string]*Type per level instead of arena-allocated linked chunks.

func NewScope

func NewScope(parent *Scope) *Scope

NewScope creates a new scope level. Pass nil for the root scope.

func (*Scope) Bind

func (s *Scope) Bind(name string, typ *Type)

Bind binds a variable name to a type in the current scope. If the name already exists in the current scope, it is overwritten.

func (*Scope) Lookup

func (s *Scope) Lookup(name string) *Type

Lookup walks up the scope chain looking for name. Returns the first match found. Returns nil if the name is not bound in any enclosing scope.

func (*Scope) Parent

func (s *Scope) Parent() *Scope

Parent returns the parent scope, or nil for the root scope.

type Type

type Type struct {
	Kind       TypeKind
	Name       string      // qualified name for Named/Builtin/TypeParam/Alias
	Elem       *Type       // element for Pointer/Slice/Array/Channel/Optional/Reference
	Key        *Type       // key for Map
	Value      *Type       // value for Map
	Params     []Param     // parameters for Func
	Returns    []*Type     // return types for Func
	Fields     []Field     // fields for Struct
	Methods    []Method    // methods for Interface
	Elements   []*Type     // elements for Tuple
	TypeParams []TypeParam // generic type parameters
	Underlying *Type       // underlying type for Alias
	ChanDir    ChanDir     // direction for Channel
}

Type is the core type representation struct with kind discrimination. Different fields are populated based on Kind, analogous to the tagged union approach in the C reference implementation (CBMType).

func Alias

func Alias(name string, underlying *Type) *Type

Alias returns a new alias type with the given name and underlying type.

func Array

func Array(elem *Type) *Type

Array returns a new array type with the given element type.

func Builtin

func Builtin(name string) *Type

Builtin returns a new builtin type (int, string, bool, error, etc.).

func Channel

func Channel(elem *Type, dir ChanDir) *Type

Channel returns a new channel type with the given element type and direction.

func Func

func Func(params []Param, returns []*Type) *Type

Func returns a new function type with the given parameters and return types.

func Interface

func Interface(methods []Method) *Type

Interface returns a new interface type with the given methods.

func Map

func Map(key, value *Type) *Type

Map returns a new map type with the given key and value types.

func Named

func Named(qualifiedName string) *Type

Named returns a new named type with the given qualified name.

func Optional

func Optional(elem *Type) *Type

Optional returns a new optional type wrapping elem.

func Pointer

func Pointer(elem *Type) *Type

Pointer returns a new pointer type wrapping elem.

func Ref

func Ref(elem *Type) *Type

Ref returns a new reference type wrapping elem.

func Slice

func Slice(elem *Type) *Type

Slice returns a new slice type with the given element type.

func Struct

func Struct(fields []Field) *Type

Struct returns a new struct type with the given fields.

func Tuple

func Tuple(elems []*Type) *Type

Tuple returns a new tuple type (multi-return) with the given element types.

func TypeParamType

func TypeParamType(name string) *Type

TypeParamType returns a new generic type parameter.

func Unknown

func Unknown() *Type

Unknown returns a new type with KindUnknown.

func (*Type) Deref

func (t *Type) Deref() *Type

Deref removes one pointer level: returns the element type if Pointer, or the type itself otherwise.

func (*Type) GetElem

func (t *Type) GetElem() *Type

GetElem returns the element type for Pointer, Slice, Array, Channel, Optional, and Reference kinds. Returns nil for other kinds.

func (*Type) IsInterface

func (t *Type) IsInterface() bool

IsInterface returns true if the type has KindInterface.

func (*Type) IsPointer

func (t *Type) IsPointer() bool

IsPointer returns true if the type has KindPointer.

func (*Type) IsUnknown

func (t *Type) IsUnknown() bool

IsUnknown returns true if the type has KindUnknown.

type TypeKind

type TypeKind int

TypeKind enumerates the language-agnostic type representation kinds.

const (
	KindUnknown   TypeKind = iota
	KindNamed              // named type: "Database", "http.Request"
	KindPointer            // *T
	KindSlice              // []T
	KindMap                // map[K]V
	KindChannel            // chan T (with direction)
	KindFunc               // func(params) returns
	KindInterface          // interface{...}
	KindStruct             // struct{...}
	KindBuiltin            // int, string, bool, error, etc.
	KindTuple              // multi-return (T1, T2)
	KindTypeParam          // generic type parameter: T, K, V
	KindArray              // [N]T
	KindOptional           // *T or Option<T> language-agnostic optional
	KindReference          // &T (Rust/C++ reference)
	KindAlias              // type alias
)

func (TypeKind) String

func (k TypeKind) String() string

String returns a human-readable name for the TypeKind.

type TypeParam

type TypeParam struct {
	Name       string
	Constraint *Type // nil means "any"
}

TypeParam represents a generic type parameter.

Directories

Path Synopsis
Package csresolve implements the C# language in-process type resolver.
Package csresolve implements the C# language in-process type resolver.
Package goresolve implements the Go language in-process type resolver.
Package goresolve implements the Go language in-process type resolver.
Package javaresolve implements the Java language in-process type resolver.
Package javaresolve implements the Java language in-process type resolver.
Package pyresolve implements the Python language in-process type resolver.
Package pyresolve implements the Python language in-process type resolver.
Package rubyresolve implements the Ruby language in-process type resolver.
Package rubyresolve implements the Ruby language in-process type resolver.
Package rustresolve implements the Rust language in-process type resolver.
Package rustresolve implements the Rust language in-process type resolver.
Package tsresolve implements the TypeScript/JavaScript in-process type resolver.
Package tsresolve implements the TypeScript/JavaScript in-process type resolver.

Jump to

Keyboard shortcuts

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