Skip to content

Commit

Permalink
Merge pull request #1488 from xushiwei/q
Browse files Browse the repository at this point in the history
cl: rec.Use PkgName
  • Loading branch information
xushiwei authored Oct 23, 2023
2 parents cd2bd07 + 3edf20b commit 3aacc65
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
43 changes: 25 additions & 18 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,17 @@ type pkgCtx struct {
inInst int // toType in generic instance
}

type pkgImp struct {
*gox.PkgRef
pkgName *types.PkgName
}

type blockCtx struct {
*pkgCtx
pkg *gox.Package
cb *gox.CodeBuilder
fset *token.FileSet
imports map[string]*gox.PkgRef
imports map[string]pkgImp
lookups []*gox.PkgRef
clookups []*cpackages.PkgRef
tlookup *typeParamLookup
Expand All @@ -408,8 +413,8 @@ func (bc *blockCtx) recorder() Recorder {
return nil
}

func (bc *blockCtx) findImport(name string) (pr *gox.PkgRef, ok bool) {
pr, ok = bc.imports[name]
func (bc *blockCtx) findImport(name string) (pi pkgImp, ok bool) {
pi, ok = bc.imports[name]
return
}

Expand Down Expand Up @@ -565,7 +570,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
ctx := &blockCtx{
pkg: p, pkgCtx: ctx, cb: p.CB(), fset: p.Fset, targetDir: targetDir,
fileLine: fileLine, relativePath: conf.RelativePath, isClass: f.IsClass, rec: conf.Recorder,
c2goBase: c2goBase(conf.C2goBase), imports: make(map[string]*gox.PkgRef), isGopFile: true,
c2goBase: c2goBase(conf.C2goBase), imports: make(map[string]pkgImp), isGopFile: true,
}
preloadGopFile(p, ctx, fpath, f, conf)
}
Expand All @@ -576,7 +581,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
gofiles = append(gofiles, f)
ctx := &blockCtx{
pkg: p, pkgCtx: ctx, cb: p.CB(), fset: p.Fset, targetDir: targetDir,
imports: make(map[string]*gox.PkgRef),
imports: make(map[string]pkgImp),
}
preloadFile(p, ctx, fpath, f, false, false)
}
Expand Down Expand Up @@ -1144,21 +1149,25 @@ func loadImport(ctx *blockCtx, spec *ast.ImportSpec) {
} else {
pkg = ctx.pkg.Import(simplifyGopPackage(pkgPath), spec)
}
var name string

var pos token.Pos
var name string
var specName = spec.Name
if specName != nil {
name, pos = specName.Name, specName.Pos()
} else {
name, pos = pkg.Types.Name(), spec.Path.Pos()
}
pkgName := types.NewPkgName(pos, ctx.pkg.Types, name, pkg.Types)
if rec := ctx.recorder(); rec != nil {
defer func() {
pkgName := types.NewPkgName(pos, ctx.pkg.Types, name, pkg.Types)
if specName != nil {
rec.Def(specName, pkgName)
} else {
rec.Implicit(spec, pkgName)
}
}()
if specName != nil {
rec.Def(specName, pkgName)
} else {
rec.Implicit(spec, pkgName)
}
}

if specName != nil {
name, pos = specName.Name, specName.NamePos
if name == "." {
ctx.lookups = append(ctx.lookups, pkg)
return
Expand All @@ -1167,10 +1176,8 @@ func loadImport(ctx *blockCtx, spec *ast.ImportSpec) {
pkg.MarkForceUsed()
return
}
} else {
name, pos = pkg.Types.Name(), spec.Path.Pos()
}
ctx.imports[name] = pkg
ctx.imports[name] = pkgImp{pkg, pkgName}
}

func loadConstSpecs(ctx *blockCtx, cdecl *gox.ConstDefs, specs []ast.Spec) {
Expand Down
7 changes: 3 additions & 4 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,11 @@ func compileIdent(ctx *blockCtx, ident *ast.Ident, flags int) (obj *gox.PkgRef,
if name == "C" && len(ctx.clookups) > 0 {
return nil, objCPkgRef
}
if pr, ok := ctx.findImport(name); ok {
if pi, ok := ctx.findImport(name); ok {
if rec := ctx.recorder(); rec != nil {
pkgName := types.NewPkgName(ident.NamePos, ctx.pkg.Types, name, pr.Types)
rec.Use(ident, pkgName)
rec.Use(ident, pi.pkgName)
}
return pr, objPkgRef
return pi.PkgRef, objPkgRef
}
}

Expand Down
7 changes: 3 additions & 4 deletions cl/func_type_and_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,12 @@ func toChanType(ctx *blockCtx, v *ast.ChanType) *types.Chan {
func toExternalType(ctx *blockCtx, v *ast.SelectorExpr) types.Type {
id := v.X.(*ast.Ident)
name := id.Name
if pr, ok := ctx.findImport(name); ok {
if pi, ok := ctx.findImport(name); ok {
rec := ctx.recorder()
if rec != nil {
pkgName := types.NewPkgName(id.NamePos, ctx.pkg.Types, name, pr.Types)
rec.Use(id, pkgName)
rec.Use(id, pi.pkgName)
}
o := pr.TryRef(v.Sel.Name)
o := pi.TryRef(v.Sel.Name)
if t, ok := o.(*types.TypeName); ok {
if rec != nil {
rec.Use(v.Sel, t)
Expand Down

0 comments on commit 3aacc65

Please sign in to comment.