convert

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 7, 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 added in v0.2.0

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

if modulePath is not empty, init the module by modulePath

func NewCommentGroup added in v0.3.1

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

func NewCommentGroupFromC added in v0.3.1

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

func NewFuncDocComment added in v0.3.1

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

func NewTypecDocComment added in v0.3.1

func NewTypecDocComment() *goast.Comment

func SetDebug added in v0.3.1

func SetDebug(dbgFlags dbgFlags)

func Sizeof added in v0.3.1

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 {
	PkgName   string
	SymbFile  string // llcppg.symb.json
	CfgFile   string // llcppg.cfg
	PubFile   string // llcppg.pub
	OutputDir string

	Pkg *llconfig.Pkg
}

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    *llconfig.Pkg
	GenPkg *Package
	Conf   *Config
}

func NewConverter

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

func (*Converter) Convert

func (p *Converter) Convert()

func (*Converter) Fmt

func (p *Converter) Fmt()

func (*Converter) Process

func (p *Converter) Process()

func (*Converter) Tidy added in v0.2.0

func (p *Converter) Tidy()

func (*Converter) Write

func (p *Converter) Write()

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 is 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) *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 added in v0.2.1

func (g *GoFuncSpec) IsIgnore() bool

type HeaderFile

type HeaderFile struct {
	File     string
	FileType llcppg.FileType
}

func NewHeaderFile

func NewHeaderFile(file string, fileType llcppg.FileType) *HeaderFile

func (*HeaderFile) InCurPkg

func (p *HeaderFile) InCurPkg() bool

func (*HeaderFile) ToGoFileName

func (p *HeaderFile) ToGoFileName(pkgName string) string

Note:third hfile should not set to gogen.Package

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

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

func NewLitParseError added in v0.3.1

func NewLitParseError(from any, to string) *LitParseError

func (*LitParseError) Error added in v0.3.1

func (p *LitParseError) Error() string

type Module

type Module = gopmod.Module

type NameMethod added in v0.3.1

type NameMethod func(name string) string

type Node added in v0.3.1

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(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)

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

func (*Package) Config added in v0.3.1

func (p *Package) Config() *PackageConfig

func (*Package) GetGenPackage

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

func (*Package) GetOutputDir

func (p *Package) GetOutputDir() string

func (*Package) GetUniqueName added in v0.3.1

func (p *Package) GetUniqueName(node Node, nameMethod NameMethod) (pubName 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 added in v0.3.1

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

func (*Package) LookupSymbol

func (p *Package) LookupSymbol(mangleName config.MangleNameType) (*GoFuncSpec, error)

func (*Package) NewConstGroup

func (p *Package) NewConstGroup() *ConstGroup

func (*Package) NewEnumTypeDecl

func (p *Package) NewEnumTypeDecl(enumTypeDecl *ast.EnumTypeDecl) error

func (*Package) NewFuncDecl

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

func (*Package) NewMacro

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

func (*Package) NewTypeDecl

func (p *Package) NewTypeDecl(typeDecl *ast.TypeDecl) 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(typedefDecl *ast.TypedefDecl) error

func (*Package) NewTypedefs

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

func (*Package) RegisterNode added in v0.3.1

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

func (*Package) SetCurFile

func (p *Package) SetCurFile(hfile *HeaderFile)

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

func (*Package) Write

func (p *Package) Write(headerFile string) error

Write generates a Go file based on the package content. The output file will be generated in a subdirectory named after the package within the outputDir. If outputDir is not provided, the current directory will be used. The header file name is the go file name.

Files that are already processed in dependent packages will not be output.

func (*Package) WriteAutogenFile

func (p *Package) WriteAutogenFile() error

func (*Package) WriteLinkFile

func (p *Package) WriteLinkFile() (string, error)

func (*Package) WritePkgFiles

func (p *Package) WritePkgFiles() error

WritePkgFiles writes all converted header files to Go files. Calls deferTypeBuild() first to complete all incomplete type definitions, because some types may be implemented across multiple files.

func (*Package) WritePubFile

func (p *Package) WritePubFile() error

func (*Package) WriteToBuffer

func (p *Package) WriteToBuffer(genFName string) (*bytes.Buffer, error)

Write the corresponding files in gogen package to the buffer

type PackageConfig

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

type PkgBase

type PkgBase struct {
	PkgPath  string            // package path, e.g. github.com/goplus/llgo/cjson
	CppgConf *llcppg.Config    // llcppg.cfg
	Pubs     map[string]string // llcppg.pub
}

type PkgDepLoader

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

func NewPkgDepLoader

func NewPkgDepLoader(mod *gopmod.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
	Dir  string // absolute local path of the package
}

for current package & dependent packages

func NewPkgInfo

func NewPkgInfo(pkgPath string, pkgDir string, conf *llcppg.Config, pubs map[string]string) *PkgInfo

type ProcessSymbol added in v0.3.1

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

func NewProcessSymbol added in v0.3.1

func NewProcessSymbol() *ProcessSymbol

func (*ProcessSymbol) Lookup added in v0.3.1

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

func (*ProcessSymbol) Register added in v0.3.1

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

type ThirdTypeLoc

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

func NewThirdTypeLoc

func NewThirdTypeLoc() *ThirdTypeLoc

func (*ThirdTypeLoc) Add

func (p *ThirdTypeLoc) Add(ident *ast.Ident, loc *ast.Location)

func (*ThirdTypeLoc) Lookup

func (p *ThirdTypeLoc) Lookup(name string) (string, bool)

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(p *Package) *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 added in v0.3.1

type TypeDefinedError struct {
	Name       string
	OriginName string
}

func NewTypeDefinedError added in v0.3.1

func NewTypeDefinedError(name, originName string) *TypeDefinedError

func (*TypeDefinedError) Error added in v0.3.1

func (p *TypeDefinedError) Error() string

Jump to

Keyboard shortcuts

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