build: fix unsafe.Sizeof for llgo:type C
This commit is contained in:
@@ -18,6 +18,7 @@ package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/constant"
|
||||
"go/token"
|
||||
"go/types"
|
||||
@@ -136,6 +137,12 @@ func Do(args []string, conf *Config) {
|
||||
prog := llssa.NewProgram(nil)
|
||||
sizes := prog.TypeSizes
|
||||
dedup := packages.NewDeduper()
|
||||
dedup.SetPreload(func(pkg *types.Package, files []*ast.File) {
|
||||
if canSkipToBuild(pkg.Path()) {
|
||||
return
|
||||
}
|
||||
cl.ParsePkgSyntax(prog, pkg, files)
|
||||
})
|
||||
|
||||
if patterns == nil {
|
||||
patterns = []string{"."}
|
||||
@@ -256,7 +263,6 @@ func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs
|
||||
case cl.PkgDeclOnly:
|
||||
// skip packages that only contain declarations
|
||||
// and set no export file
|
||||
cl.ParsePkgSyntax(ctx.prog, pkg.Types, pkg.Syntax)
|
||||
pkg.ExportFile = ""
|
||||
case cl.PkgLinkIR, cl.PkgLinkExtern, cl.PkgPyModule:
|
||||
if len(pkg.GoFiles) > 0 {
|
||||
|
||||
@@ -17,17 +17,10 @@
|
||||
package llgen
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"go/types"
|
||||
"os"
|
||||
|
||||
"github.com/goplus/gogen/packages"
|
||||
"github.com/goplus/llgo/cl"
|
||||
"github.com/goplus/llgo/internal/mod"
|
||||
"golang.org/x/tools/go/ssa"
|
||||
"golang.org/x/tools/go/ssa/ssautil"
|
||||
|
||||
llssa "github.com/goplus/llgo/ssa"
|
||||
)
|
||||
@@ -45,51 +38,11 @@ func PkgPath(dir string) string {
|
||||
}
|
||||
|
||||
func Do(pkgPath, inFile, outFile string) {
|
||||
ret := Gen(pkgPath, inFile, nil)
|
||||
ret := genFrom(inFile, pkgPath)
|
||||
err := os.WriteFile(outFile, []byte(ret), 0644)
|
||||
check(err)
|
||||
}
|
||||
|
||||
func Gen(pkgPath, inFile string, src any) string {
|
||||
fset := token.NewFileSet()
|
||||
f, err := parser.ParseFile(fset, inFile, src, parser.ParseComments)
|
||||
check(err)
|
||||
|
||||
files := []*ast.File{f}
|
||||
name := f.Name.Name
|
||||
if pkgPath == "" {
|
||||
pkgPath = name
|
||||
}
|
||||
pkg := types.NewPackage(pkgPath, name)
|
||||
imp := packages.NewImporter(fset)
|
||||
ssaPkg, _, err := ssautil.BuildPackage(
|
||||
&types.Config{Importer: imp}, fset, pkg, files, ssa.SanityCheckFunctions|ssa.InstantiateGenerics)
|
||||
check(err)
|
||||
|
||||
if Verbose {
|
||||
ssaPkg.WriteTo(os.Stderr)
|
||||
}
|
||||
|
||||
prog := llssa.NewProgram(nil)
|
||||
prog.SetRuntime(func() *types.Package {
|
||||
ret, _ := imp.Import(llssa.PkgRuntime)
|
||||
return ret
|
||||
})
|
||||
prog.SetPython(func() *types.Package {
|
||||
ret, _ := imp.Import(llssa.PkgPython)
|
||||
return ret
|
||||
})
|
||||
|
||||
ret, err := cl.NewPackage(prog, ssaPkg, files)
|
||||
check(err)
|
||||
|
||||
if prog.NeedPyInit { // call PyInit if needed
|
||||
ret.PyInit()
|
||||
}
|
||||
|
||||
return ret.String()
|
||||
}
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package llgen
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/types"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -60,12 +61,32 @@ func initRtAndPy(prog llssa.Program, cfg *packages.Config) {
|
||||
}
|
||||
|
||||
func GenFrom(fileOrPkg string) string {
|
||||
return genFrom(fileOrPkg, "")
|
||||
}
|
||||
|
||||
func genFrom(fileOrPkg string, pkgPath string) string {
|
||||
prog := llssa.NewProgram(nil)
|
||||
|
||||
cfg := &packages.Config{
|
||||
Mode: loadSyntax | packages.NeedDeps,
|
||||
}
|
||||
initial, err := packages.LoadEx(nil, prog.TypeSizes, cfg, fileOrPkg)
|
||||
|
||||
dedup := packages.NewDeduper()
|
||||
dedup.SetPkgPath(func(path, name string) string {
|
||||
if path == "command-line-arguments" {
|
||||
if pkgPath != "" {
|
||||
path = pkgPath
|
||||
} else {
|
||||
path = name
|
||||
}
|
||||
}
|
||||
return path
|
||||
})
|
||||
dedup.SetPreload(func(pkg *types.Package, files []*ast.File) {
|
||||
cl.ParsePkgSyntax(prog, pkg, files)
|
||||
})
|
||||
|
||||
initial, err := packages.LoadEx(dedup, prog.TypeSizes, cfg, fileOrPkg)
|
||||
check(err)
|
||||
|
||||
_, pkgs := ssautil.AllPackages(initial, ssa.SanityCheckFunctions|ssa.InstantiateGenerics)
|
||||
|
||||
@@ -110,7 +110,9 @@ type Cached struct {
|
||||
}
|
||||
|
||||
type aDeduper struct {
|
||||
cache sync.Map
|
||||
cache sync.Map
|
||||
setpath func(path string, name string) string
|
||||
preload func(pkg *types.Package, syntax []*ast.File)
|
||||
}
|
||||
|
||||
type Deduper = *aDeduper
|
||||
@@ -119,6 +121,14 @@ func NewDeduper() Deduper {
|
||||
return &aDeduper{}
|
||||
}
|
||||
|
||||
func (p Deduper) SetPreload(fn func(pkg *types.Package, syntax []*ast.File)) {
|
||||
p.preload = fn
|
||||
}
|
||||
|
||||
func (p Deduper) SetPkgPath(fn func(path, name string) string) {
|
||||
p.setpath = fn
|
||||
}
|
||||
|
||||
func (p Deduper) Check(pkgPath string) *Cached {
|
||||
if v, ok := p.cache.Load(pkgPath); ok {
|
||||
return v.(*Cached)
|
||||
@@ -186,6 +196,9 @@ func loadPackageEx(dedup Deduper, ld *loader, lpkg *loaderPackage) {
|
||||
})
|
||||
}
|
||||
}()
|
||||
if dedup.setpath != nil {
|
||||
lpkg.PkgPath = dedup.setpath(lpkg.PkgPath, lpkg.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// Call NewPackage directly with explicit name.
|
||||
@@ -369,6 +382,10 @@ func loadPackageEx(dedup Deduper, ld *loader, lpkg *loaderPackage) {
|
||||
panic("unreachable")
|
||||
})
|
||||
|
||||
if dedup != nil && dedup.preload != nil {
|
||||
dedup.preload(lpkg.Types, lpkg.Syntax)
|
||||
}
|
||||
|
||||
// type-check
|
||||
tc := &types.Config{
|
||||
Importer: importer,
|
||||
|
||||
Reference in New Issue
Block a user