convert

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

This file is used to convert type from ast type to types.Type

Index

Constants

View Source
const (
	DbgLog     dbgFlags = 1 << iota
	DbgFlagAll          = DbgLog
)
View Source
const (
	FuncDecl nodeKind = iota + 1
	TypeDecl
	TypedefDecl
	EnumTypeDecl
	EnumItem
	Macro
)
View Source
const (
	TYPEC = "// llgo:type C"
)

Variables

View Source
var (
	ErrTypeConv = errors.New("error convert type")
)

Functions

func IsDepStd

func IsDepStd(pkgPath string) (string, bool)

func ModInit

func ModInit(deps []string, outputDir string, modulePath string) error

if modulePath is not empty, init the module by modulePath

func NewCommentGroup

func NewCommentGroup(comments ...*goast.Comment) *goast.CommentGroup

func NewCommentGroupFromC

func NewCommentGroupFromC(doc *ast.CommentGroup) *goast.CommentGroup

func NewFuncDocComment

func NewFuncDocComment(funcName string, goFuncName string) *goast.Comment

func NewTypecDocComment

func NewTypecDocComment() *goast.Comment

func SetDebug

func SetDebug(dbgFlags dbgFlags)

func Sizeof

func Sizeof(T types.Type) int64

Types

type BuiltinTypeMap

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

func NewBuiltinTypeMap

func NewBuiltinTypeMap(pkgPath, name string, conf *gogen.Config) *BuiltinTypeMap

func NewBuiltinTypeMapWithPkgRefS

func NewBuiltinTypeMapWithPkgRefS(pkgs ...gogen.PkgRef) *BuiltinTypeMap

func (*BuiltinTypeMap) CType

func (p *BuiltinTypeMap) CType(typ string) types.Type

func (*BuiltinTypeMap) FindBuiltinType

func (p *BuiltinTypeMap) FindBuiltinType(builtinType ast.BuiltinType) (types.Type, error)

func (*BuiltinTypeMap) IsVoidType

func (p *BuiltinTypeMap) IsVoidType(typ types.Type) bool

type Config

type Config struct {
	OutputDir string
	PkgPath   string
	PkgName   string
	Pkg       *ast.File
	NC        nc.NodeConverter

	Deps []string // dependent packages
	Libs string
}

type ConstGroup

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

func NewConstGroup

func NewConstGroup(pkg *gogen.Package, scope *types.Scope) *ConstGroup

func (*ConstGroup) New

func (p *ConstGroup) New(val any, typ types.Type, name string)

type Converter

type Converter struct {
	Pkg    *ast.File
	GenPkg *Package
	Conf   *Config
	NC     nc.NodeConverter
}

func NewConverter

func NewConverter(config *Config) (*Converter, error)

func (*Converter) Complete

func (p *Converter) Complete() error

func (*Converter) Convert

func (p *Converter) Convert() error

func (*Converter) Process

func (p *Converter) Process() error

type ExprWrap

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

func Expr

func Expr(e ast.Expr) *ExprWrap

func (*ExprWrap) IsVoid

func (p *ExprWrap) IsVoid() bool

func (*ExprWrap) ToChar

func (p *ExprWrap) ToChar() (int8, error)

func (*ExprWrap) ToFloat

func (p *ExprWrap) ToFloat(bitSize int) (float64, error)

func (*ExprWrap) ToInt

func (p *ExprWrap) ToInt() (int, error)

func (*ExprWrap) ToString

func (p *ExprWrap) ToString() (string, error)

type GoFuncSpec

type GoFuncSpec struct {
	GoSymbName string // original full name from input
	FnName     string // function name without receiver
	IsMethod   bool   // if the function canbe a method
	RecvName   string // receiver name
	PtrRecv    bool   // if the receiver is a pointer
}

GoFuncSpec parses and stores components of a Go function name Input examples from llcppg.symb.json's "go" field: 1. Simple function: "AddPatchToArray" 2. Method with pointer receiver: "(*Sqlite3Stmt).Sqlite3BindParameterIndex" 3. Method with value receiver: "CJSONBool.CJSONCreateBool"

func NewGoFuncSpec

func NewGoFuncSpec(name string, field []*ast.Field) *GoFuncSpec

- "AddPatchToArray" -> {goSymbolName: "AddPatchToArray", funcName: "AddPatchToArray"} - "(*Sqlite3Stmt).Sqlite3BindParameterIndex" -> {goSymbolName: "...", recvName: "Sqlite3Stmt", funcName: "Sqlite3BindParameterIndex", ptrRecv: true} - "CJSONBool.CJSONCreateBool" -> {goSymbolName: "...", recvName: "CJSONBool", funcName: "CJSONCreateBool", ptrRecv: false}

func (*GoFuncSpec) IsIgnore

func (g *GoFuncSpec) IsIgnore() bool

type Incomplete

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

type IncompleteTypes

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

func NewIncompleteTypes

func NewIncompleteTypes() *IncompleteTypes

func (*IncompleteTypes) Add

func (it *IncompleteTypes) Add(inc *Incomplete)

func (*IncompleteTypes) Clear

func (it *IncompleteTypes) Clear()

func (*IncompleteTypes) Complete

func (it *IncompleteTypes) Complete(cname string)

func (*IncompleteTypes) IterateIncomplete

func (it *IncompleteTypes) IterateIncomplete(fn func(*Incomplete) error) error

func (*IncompleteTypes) Lookup

func (it *IncompleteTypes) Lookup(cname string) (*Incomplete, bool)

type LitParseError

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

func NewLitParseError

func NewLitParseError(from any, to string) *LitParseError

func (*LitParseError) Error

func (p *LitParseError) Error() string

type Module

type Module = xgomod.Module

type Node

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

type Package

type Package struct {
	*PkgInfo
	// contains filtered or unexported fields
}

In Processing Package

func NewPackage

func NewPackage(pnc nc.NodeConverter, config *PackageConfig) (*Package, error)

When creating a new package for conversion, a Go file named after the package is generated by default. If SetCurFile is not called, all type conversions will be written to this default Go file.

func (*Package) CollectNameMapping

func (p *Package) CollectNameMapping(originName, newName string, pnc nc.NodeConverter)

Collect the name mapping between origin name and pubname if in current package, it will be collected in public symbol table

func (*Package) Complete

func (p *Package) Complete() error

Complete completes all incomplete type definitions, because some types may be implemented across multiple files.

func (*Package) Config

func (p *Package) Config() *PackageConfig

func (*Package) GetUniqueName

func (p *Package) GetUniqueName(node Node, pubName string) (_ string, changed bool)

GetUniqueName generates a unique public name for a given node using the provided name transformation method. It ensures the generated name doesn't conflict with existing names by adding a numeric suffix if needed.

Parameters:

  • node: The node containing the original name to be transformed
  • nameMethod: Function used to transform the original name (e.g., declName, constName)

Returns:

  • pubName: The generated unique public name
  • changed: Whether the generated name differs from the original name

func (*Package) Lookup

func (p *Package) Lookup(name string) types.Object

func (*Package) NewConstGroup

func (p *Package) NewConstGroup() *ConstGroup

func (*Package) NewEnumTypeDecl

func (p *Package) NewEnumTypeDecl(goName string, enumTypeDecl *ast.EnumTypeDecl, pnc nc.NodeConverter) error

func (*Package) NewFuncDecl

func (p *Package) NewFuncDecl(goName string, funcDecl *ast.FuncDecl) error

func (*Package) NewMacro

func (p *Package) NewMacro(goName string, macro *ast.Macro) error

func (*Package) NewTypeDecl

func (p *Package) NewTypeDecl(goName string, typeDecl *ast.TypeDecl, pnc nc.NodeConverter) error

NewTypeDecl converts C/C++ type declarations to Go. Besides regular type declarations, it also supports: - Forward declarations: Pre-registers incomplete types for later definition - Self-referential types: Handles types that reference themselves (like linked lists)

func (*Package) NewTypedefDecl

func (p *Package) NewTypedefDecl(goName string, typedefDecl *ast.TypedefDecl, pnc nc.NodeConverter) error

func (*Package) NewTypedefs

func (p *Package) NewTypedefs(name string, typ types.Type) *gogen.TypeDecl

func (*Package) Pkg

func (p *Package) Pkg() *gogen.Package

func (*Package) RegisterNode

func (p *Package) RegisterNode(node Node, goName string, lookup func(name string, pubName string) types.Object) (pubName string, changed bool, exist bool, err error)

func (*Package) ToSigSignature

func (p *Package) ToSigSignature(recv *types.Var, funcDecl *ast.FuncDecl) (*types.Signature, error)

func (*Package) ToType

func (p *Package) ToType(expr ast.Expr) (types.Type, error)

Convert ast.Expr to types.Type

type PackageConfig

type PackageConfig struct {
	PkgBase
	Name      string // current package name
	OutputDir string
	GenConf   *gogen.Config

	// use to gen link command like $(pkg-config --libs xxx)
	// if have this field,will generate a const named LLGoPackage,and it's not necessary
	LibCommand string
}

type PkgBase

type PkgBase struct {
	PkgPath string            // package path, e.g. github.com/goplus/llgo/cjson
	Deps    []string          // dependent packages
	Pubs    map[string]string // llcppg.pub
}

type PkgDepLoader

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

func NewPkgDepLoader

func NewPkgDepLoader(mod *xgomod.Module, pkg *gogen.Package) *PkgDepLoader

func (*PkgDepLoader) Import

func (pm *PkgDepLoader) Import(pkgPath string) (*PkgInfo, error)

func (*PkgDepLoader) Imports

func (pm *PkgDepLoader) Imports(pkgPaths []string) (pkgs []*PkgInfo, err error)

func (*PkgDepLoader) InitDeps

func (pm *PkgDepLoader) InitDeps(p *PkgInfo) error

func (*PkgDepLoader) LoadDeps

func (pm *PkgDepLoader) LoadDeps(p *PkgInfo) ([]*PkgInfo, error)

LoadDeps loads direct dependencies of the current package and recursively loads their dependencies, to get the complete dependency.

func (*PkgDepLoader) RegisterDep

func (pm *PkgDepLoader) RegisterDep(dep *PkgInfo)

func (*PkgDepLoader) RegisterDeps

func (pm *PkgDepLoader) RegisterDeps(p *PkgInfo)

RegisterDeps registers types from dependent packages into the current conversion project's scope

type PkgInfo

type PkgInfo struct {
	PkgBase
	Deps []*PkgInfo
}

for current package & dependent packages

func NewPkgInfo

func NewPkgInfo(pkgPath string, deps []string, pubs map[string]string) *PkgInfo

type ProcessSymbol

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

func NewProcessSymbol

func NewProcessSymbol() *ProcessSymbol

func (*ProcessSymbol) Lookup

func (p *ProcessSymbol) Lookup(node Node) (string, bool)

func (*ProcessSymbol) Register

func (p *ProcessSymbol) Register(node Node, pubName string) string

type TypeContext

type TypeContext int
const (
	Normal TypeContext = iota
	Param              // In function parameter context
	Record             // In record field context
)

type TypeConv

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

func NewConv

func NewConv(pkg *gogen.Package, types *types.Package, pnc nc.NodeConverter, lookup func(name string, pnc nc.NodeConverter) (types.Type, error)) *TypeConv

func (*TypeConv) RecordTypeToStruct

func (p *TypeConv) RecordTypeToStruct(recordType *ast.RecordType) (types.Type, error)

func (*TypeConv) ToDefaultEnumType

func (p *TypeConv) ToDefaultEnumType() types.Type

func (*TypeConv) ToSignature

func (p *TypeConv) ToSignature(funcType *ast.FuncType, recv *types.Var) (*types.Signature, error)

func (*TypeConv) ToType

func (p *TypeConv) ToType(expr ast.Expr) (types.Type, error)

Convert ast.Expr to types.Type

type TypeDefinedError

type TypeDefinedError struct {
	Name       string
	OriginName string
}

func NewTypeDefinedError

func NewTypeDefinedError(name, originName string) *TypeDefinedError

func (*TypeDefinedError) Error

func (p *TypeDefinedError) Error() string

Jump to

Keyboard shortcuts

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