diff --git a/cl/import.go b/cl/import.go index 970758a2..171ea4d9 100644 --- a/cl/import.go +++ b/cl/import.go @@ -65,15 +65,19 @@ 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 { - 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 - } - return - }) + 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 + } + return + }) + } } } } diff --git a/internal/build/build.go b/internal/build/build.go index fdbc92bb..f1f260f0 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -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 } } } diff --git a/internal/typepatch/patch.go b/internal/typepatch/patch.go new file mode 100644 index 00000000..ae17a2a5 --- /dev/null +++ b/internal/typepatch/patch.go @@ -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 +}