build: use typepatch.Pkg merge patches of a standard library

This commit is contained in:
xushiwei
2024-06-15 15:52:44 +08:00
parent 994502077a
commit 5011c394d7
3 changed files with 84 additions and 10 deletions

View File

@@ -65,9 +65,12 @@ func (p *pkgSymInfo) addSym(fset *token.FileSet, pos token.Pos, fullName, inPkgN
}
func (p *pkgSymInfo) initLinknames(ctx *context) {
sep := []byte{'\n'}
commentPrefix := []byte{'/', '/'}
for file, b := range p.files {
lines := bytes.Split(b, []byte{'\n'})
lines := bytes.Split(b, sep)
for _, line := range lines {
if bytes.HasPrefix(line, commentPrefix) {
ctx.initLinkname(string(line), func(inPkgName string) (fullName string, isVar, ok bool) {
if sym, ok := p.syms[inPkgName]; ok && file == sym.file {
return sym.fullName, sym.isVar, true
@@ -77,6 +80,7 @@ func (p *pkgSymInfo) initLinknames(ctx *context) {
}
}
}
}
// PkgKindOf returns the kind of a package.
func PkgKindOf(pkg *types.Package) (int, string) {

View File

@@ -33,6 +33,7 @@ import (
"github.com/goplus/llgo/cl"
"github.com/goplus/llgo/internal/packages"
"github.com/goplus/llgo/internal/typepatch"
"github.com/goplus/llgo/xtool/clang"
"github.com/goplus/llgo/xtool/env"
@@ -94,6 +95,7 @@ func Do(args []string, conf *Config) {
cfg := &packages.Config{
Mode: loadSyntax | packages.NeedDeps | packages.NeedModule | packages.NeedExportFile,
BuildFlags: flags,
Fset: token.NewFileSet(),
}
llssa.Initialize(llssa.InitAll)
@@ -365,11 +367,15 @@ func buildPkg(prog llssa.Program, aPkg *aPackage, mode Mode, verbose bool) {
pkg.ExportFile = ""
return
}
altSSA := aPkg.AltSSA
syntax := pkg.Syntax
if altPkg := aPkg.AltPkg; altPkg != nil {
syntax = append(syntax, altPkg.Syntax...)
if altSSA != nil {
altSSA.Pkg = typepatch.Pkg(pkg.Types, altPkg.Types)
}
ret, err := cl.NewPackageEx(prog, aPkg.SSA, aPkg.AltSSA, syntax)
}
ret, err := cl.NewPackageEx(prog, aPkg.SSA, altSSA, syntax)
check(err)
if needLLFile(mode) {
pkg.ExportFile += ".ll"
@@ -421,8 +427,6 @@ func allPkgs(imp importer, initial []*packages.Package, mode ssa.BuilderMode) (p
altPkgPath := "github.com/goplus/llgo/internal/lib/" + p.PkgPath
if altPkg = imp(altPkgPath); altPkg != nil {
altSSA = createAltSSAPkg(prog, altPkg)
altSSA.Pkg = p.Types
// TODO(xsw): merge p.Types and altPkg.Types
}
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package typepatch
import (
"go/token"
"go/types"
"unsafe"
)
type typesScope struct {
parent *types.Scope
children []*types.Scope
number int
elems map[string]types.Object // TODO(xsw): ensure offset of elems
pos, end token.Pos
comment string
isFunc bool
}
type object struct {
parent *types.Scope
pos token.Pos
pkg *types.Package // TODO(xsw): ensure offset of pkg
unused [8]byte
}
type iface struct {
tab unsafe.Pointer
data unsafe.Pointer
}
func setPkg(o types.Object, pkg *types.Package) {
data := (*iface)(unsafe.Pointer(&o)).data
(*object)(data).pkg = pkg
}
func setObject(scope *types.Scope, name string, o types.Object) {
s := (*typesScope)(unsafe.Pointer(scope))
s.elems[name] = o
}
func Pkg(pkg, alt *types.Package) *types.Package {
scope := pkg.Scope()
altScope := alt.Scope()
for _, name := range altScope.Names() {
o := altScope.Lookup(name)
setPkg(o, pkg)
setObject(scope, name, o)
}
return pkg
}