convert

package
v0.0.0-...-d0a659f Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Overview

This file is used to convert ast from "github.com/goplus/llcppg/ast" to "go/ast"

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

Index

Constants

View Source
const (
	SysPkgC       = "github.com/goplus/llgo/c"
	SysPkgOs      = "github.com/goplus/llgo/c/os"
	SysPkgSetjmp  = "github.com/goplus/llgo/c/setjmp"
	SysPkgTime    = "github.com/goplus/llgo/c/time"
	SysPkgMath    = "github.com/goplus/llgo/c/math"
	SysPkgI18n    = "github.com/goplus/llgo/c/i18n"
	SysPkgComplex = "github.com/goplus/llgo/c/math/cmplx"

	// posix
	SysPkgPthread = "github.com/goplus/llgo/c/pthread"
	SysPkgUnixNet = "github.com/goplus/llgo/c/unix/net"
)
View Source
const (
	TYPEC = "// llgo:type C"
)

Variables

View Source
var (
	ErrTypeConv = errors.New("error convert type")
)
View Source
var PkgMappings = []PkgMapping{

	{Pattern: `(^|[^a-zA-Z0-9])stdint[^a-zA-Z0-9]`, Package: SysPkgC},
	{Pattern: `(^|[^a-zA-Z0-9])stddef[^a-zA-Z0-9]`, Package: SysPkgC},
	{Pattern: `(^|[^a-zA-Z0-9])stdio[^a-zA-Z0-9]`, Package: SysPkgC},
	{Pattern: `(^|[^a-zA-Z0-9])stdlib[^a-zA-Z0-9]`, Package: SysPkgC},
	{Pattern: `(^|[^a-zA-Z0-9])string[^a-zA-Z0-9]`, Package: SysPkgC},
	{Pattern: `(^|[^a-zA-Z0-9])stdbool[^a-zA-Z0-9]`, Package: SysPkgC},
	{Pattern: `(^|[^a-zA-Z0-9])stdarg[^a-zA-Z0-9]`, Package: SysPkgC},
	{Pattern: `(^|[^a-zA-Z0-9])limits[^a-zA-Z0-9]`, Package: SysPkgC},
	{Pattern: `(^|[^a-zA-Z0-9])ctype[^a-zA-Z0-9]`, Package: SysPkgC},
	{Pattern: `(^|[^a-zA-Z0-9])uchar[^a-zA-Z0-9]`, Package: SysPkgC},
	{Pattern: `(^|[^a-zA-Z0-9])wchar[^a-zA-Z0-9]`, Package: SysPkgC},
	{Pattern: `(^|[^a-zA-Z0-9])wctype[^a-zA-Z0-9]`, Package: SysPkgC},
	{Pattern: `(^|[^a-zA-Z0-9])inttypes[^a-zA-Z0-9]`, Package: SysPkgC},

	{Pattern: `(^|[^a-zA-Z0-9])signal[^a-zA-Z0-9]`, Package: SysPkgOs},
	{Pattern: `(^|[^a-zA-Z0-9])sig[a-zA-Z]*[^a-zA-Z0-9]`, Package: SysPkgOs},
	{Pattern: `(^|[^a-zA-Z0-9])assert[^a-zA-Z0-9]`, Package: SysPkgOs},
	{Pattern: `(^|[^a-zA-Z0-9])stdalign[^a-zA-Z0-9]`, Package: SysPkgOs},

	{Pattern: `(^|[^a-zA-Z0-9])setjmp[^a-zA-Z0-9]`, Package: SysPkgSetjmp},

	{Pattern: `(^|[^a-zA-Z0-9])math[^a-zA-Z0-9]`, Package: SysPkgMath},
	{Pattern: `(^|[^a-zA-Z0-9])fenv[^a-zA-Z0-9]`, Package: SysPkgMath},
	{Pattern: `(^|[^a-zA-Z0-9])complex[^a-zA-Z0-9]`, Package: SysPkgComplex},

	{Pattern: `(^|[^a-zA-Z0-9])time[^a-zA-Z0-9]`, Package: SysPkgTime},

	{Pattern: `(^|[^a-zA-Z0-9])pthread\w*`, Package: SysPkgPthread},

	{Pattern: `(^|[^a-zA-Z0-9])locale[^a-zA-Z0-9]`, Package: SysPkgI18n},

	{Pattern: `(^|[^a-zA-Z0-9])socket[^a-zA-Z0-9]`, Package: SysPkgUnixNet},
	{Pattern: `(^|[^a-zA-Z0-9])arpa[^a-zA-Z0-9]`, Package: SysPkgUnixNet},
	{Pattern: `(^|[^a-zA-Z0-9])netinet6?[^a-zA-Z0-9]`, Package: SysPkgUnixNet},
	{Pattern: `(^|[^a-zA-Z0-9])net[^a-zA-Z0-9]`, Package: SysPkgUnixNet},

	{Pattern: `_int\d+_t`, Package: SysPkgC},
	{Pattern: `_uint\d+_t`, Package: SysPkgC},
	{Pattern: `_size_t`, Package: SysPkgC},
	{Pattern: `_intptr_t`, Package: SysPkgC},
	{Pattern: `_uintptr_t`, Package: SysPkgC},
	{Pattern: `_ptrdiff_t`, Package: SysPkgC},

	{Pattern: `malloc`, Package: SysPkgC},
	{Pattern: `alloc`, Package: SysPkgC},

	{Pattern: `(^|[^a-zA-Z0-9])clock(id_t|_t)`, Package: SysPkgTime},
	{Pattern: `(^|[^a-zA-Z0-9])(i)?time\w*`, Package: SysPkgTime},
	{Pattern: `(^|[^a-zA-Z0-9])tm[^a-zA-Z0-9]`, Package: SysPkgTime},

	{Pattern: `\w+_t[^a-zA-Z0-9]`, Package: SysPkgC},
	{Pattern: `(^|[^a-zA-Z0-9])types[^a-zA-Z0-9]`, Package: SysPkgC},
	{Pattern: `(^|[^a-zA-Z0-9])sys[^a-zA-Z0-9]`, Package: SysPkgOs},
}

Functions

func IncPathToPkg

func IncPathToPkg(incPath string) (pkg string, isDefault bool)

func NewFuncDocComments

func NewFuncDocComments(funcName string, goFuncName string) *goast.CommentGroup

func NewTypecDocComments

func NewTypecDocComments() *goast.CommentGroup

Types

type AstConvert

type AstConvert struct {
	*visitor.BaseDocVisitor
	Pkg *Package
	// contains filtered or unexported fields
}

func NewAstConvert

func NewAstConvert(config *Config) (*AstConvert, error)

func (*AstConvert) SetVisitDone

func (p *AstConvert) SetVisitDone(fn func(pkg *Package, incPath string))

func (*AstConvert) VisitDone

func (p *AstConvert) VisitDone(incPath string)

func (*AstConvert) VisitEnumTypeDecl

func (p *AstConvert) VisitEnumTypeDecl(enumTypeDecl *ast.EnumTypeDecl)

func (*AstConvert) VisitFuncDecl

func (p *AstConvert) VisitFuncDecl(funcDecl *ast.FuncDecl)

func (*AstConvert) VisitMacro

func (p *AstConvert) VisitMacro(macro *ast.Macro)

func (*AstConvert) VisitStart

func (p *AstConvert) VisitStart(path string, incPath string, isSys bool, fileType llcppg.FileType)

func (*AstConvert) VisitStruct

func (p *AstConvert) VisitStruct(structName *ast.Ident, fields *ast.FieldList, typeDecl *ast.TypeDecl)

func (*AstConvert) VisitTypedefDecl

func (p *AstConvert) VisitTypedefDecl(typedefDecl *ast.TypedefDecl)

func (*AstConvert) VisitUnion

func (p *AstConvert) VisitUnion(unionName *ast.Ident, fields *ast.FieldList, typeDecl *ast.TypeDecl)

func (*AstConvert) WriteLinkFile

func (p *AstConvert) WriteLinkFile()

func (*AstConvert) WritePkgFiles

func (p *AstConvert) WritePkgFiles()

func (*AstConvert) WritePubFile

func (p *AstConvert) WritePubFile()

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

type ConvertComment struct {
	*goast.Comment
}

func Comment

func Comment(doc *ast.Comment) *ConvertComment

type ConvertCommentGroup

type ConvertCommentGroup struct {
	*goast.CommentGroup
}

func CommentGroup

func CommentGroup(doc *ast.CommentGroup) *ConvertCommentGroup

func (*ConvertCommentGroup) AddComment

func (p *ConvertCommentGroup) AddComment(comment *goast.Comment) error

func (*ConvertCommentGroup) AddCommentGroup

func (p *ConvertCommentGroup) AddCommentGroup(doc *goast.CommentGroup)

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}

type Header2Pkg

type Header2Pkg struct {
	Header  *HeaderInfo
	PkgPath string
}

type HeaderFile

type HeaderFile struct {
	File         string
	IncPath      string
	IsHeaderFile bool
	FileType     llcppg.FileType
	IsSys        bool
}

func NewHeaderFile

func NewHeaderFile(file string, incPath string, isHeaderFile bool, fileType llcppg.FileType, isSys bool) *HeaderFile

func (*HeaderFile) InCurPkg

func (p *HeaderFile) InCurPkg() bool

func (*HeaderFile) ToGoFileName

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

type HeaderInfo

type HeaderInfo struct {
	IncPath string // stdlib include path
	Path    string // full path
}

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 Module

type Module = gopmod.Module

type Package

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

In Processing Package

func NewPackage

func NewPackage(config *PackageConfig) *Package

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

func (p *Package) DeclName(name string, toCamel bool) (pubName string, changed bool, err error)

For a decl name, it should be unique

func (*Package) DepIncPaths

func (p *Package) DepIncPaths() []string

Return all include paths of dependent packages

func (*Package) GetGenPackage

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

func (*Package) GetOutputDir

func (p *Package) GetOutputDir() string

func (*Package) GetTypeConv

func (p *Package) GetTypeConv() *TypeConv

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

func (p *Package) WriteDefaultFileToBuffer() (*bytes.Buffer, error)

WriteDefaultFileToBuffer writes the content of the default Go file to a buffer. The default file is named after the package (p.Name() + ".go"). This method is particularly useful for testing type outputs, especially in package tests where there typically isn't (and doesn't need to be) a corresponding header file. Before calling SetCurFile, all type creations are written to this default gogen file. It allows for easy inspection of generated types without the need for actual file I/O.

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

for current package & dependent packages

func NewPkgInfo

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

func (*PkgInfo) GetIncPaths

func (p *PkgInfo) GetIncPaths() ([]string, []string, error)

type PkgMapping

type PkgMapping struct {
	Pattern string
	Package string
}

type TypeConfig

type TypeConfig struct {
	Package      *Package
	Types        *types.Package
	TypeMap      *BuiltinTypeMap
	SymbolTable  *config.SymbolTable
	TrimPrefixes []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 {
	gogen.PkgRef
	SysTypeLoc map[string]*HeaderInfo
	SysTypePkg map[string]*Header2Pkg
	// contains filtered or unexported fields
}

func NewConv

func NewConv(conf *TypeConfig) *TypeConv

func (*TypeConv) LookupSymbol

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

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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