From 668f6b1e3a72be0c1d190561fa43622cb12ea6dd Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 9 Feb 2024 22:16:47 +0800 Subject: [PATCH] pkg.initGopPkg --- builtin_test.go | 11 +++++++++++ import.go | 51 ++++++++++++++++++++++++++++--------------------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/builtin_test.go b/builtin_test.go index 84eb9727..9481b992 100644 --- a/builtin_test.go +++ b/builtin_test.go @@ -44,6 +44,14 @@ func getConf() *Config { return &Config{Fset: fset, Importer: imp} } +func TestInitGopPkg(t *testing.T) { + pkg := NewPackage("", "foo", nil) + pkg.Types.Scope().Insert(types.NewVar( + token.NoPos, pkg.Types, "GopPackage", types.Typ[types.Bool], + )) + pkg.initGopPkg(nil, pkg.Types) +} + func TestCheckOverloads(t *testing.T) { defer func() { if e := recover(); e != "checkOverloads: should be string constant - foo" { @@ -70,6 +78,9 @@ func TestCheckGopPkgNoop(t *testing.T) { } }() var ed expDeps + tyParam := types.NewTypeParam(types.NewTypeName(0, pkg.Types, "T", nil), TyEmptyInterface) + ed.typ(tyParam) + ed.typ(types.NewUnion([]*types.Term{types.NewTerm(false, tyParam)})) ed.typ(&unboundFuncParam{}) } diff --git a/import.go b/import.go index 75b28df9..bb648358 100644 --- a/import.go +++ b/import.go @@ -90,27 +90,8 @@ func isOverload(name string) bool { } // InitThisGopPkg initializes a Go+ package. -func InitThisGopPkg(pkg *types.Package) (deps []string) { +func InitThisGopPkg(pkg *types.Package) { scope := pkg.Scope() - if scope.Lookup(gopPkgInit) != nil { // initialized - return - } - scope.Insert(types.NewConst( - token.NoPos, pkg, gopPkgInit, types.Typ[types.UntypedBool], constant.MakeBool(true), - )) - - pkgDeps := scope.Lookup(gopPackage) - if pkgDeps == nil { // not is a Go+ package - return - } - valDeps := pkgDeps.(*types.Const).Val() - if valDeps.Kind() == constant.String { - deps = strings.Split(constant.StringVal(valDeps), ",") - } - - if debugImport && pkg.Path() != "" { - log.Println("==> Import", pkg.Path()) - } gopos := make([]string, 0, 4) overloads := make(map[omthd][]types.Object) onameds := make(map[string][]*types.Named) @@ -184,7 +165,6 @@ func InitThisGopPkg(pkg *types.Package) (deps []string) { on := NewOverloadNamed(token.NoPos, pkg, name, nameds...) scope.Insert(on) } - return } // name @@ -426,6 +406,7 @@ retry: case *types.Array: typ = t.Elem() goto retry + case *types.TypeParam, *types.Union: default: log.Panicf("expDeps: unknown type - %T\n", typ) } @@ -487,7 +468,33 @@ func (p expDeps) struc(t *types.Struct) { // initGopPkg initializes a Go+ packages. func (p *Package) initGopPkg(importer types.Importer, pkgImp *types.Package) { - gopDeps := InitThisGopPkg(pkgImp) + scope := pkgImp.Scope() + objGopPkg := scope.Lookup(gopPackage) + if objGopPkg == nil { // not is a Go+ package + return + } + + if scope.Lookup(gopPkgInit) != nil { // initialized + return + } + scope.Insert(types.NewConst( + token.NoPos, pkgImp, gopPkgInit, types.Typ[types.UntypedBool], constant.MakeBool(true), + )) + + pkgDeps, ok := objGopPkg.(*types.Const) + if !ok { + return + } + + var gopDeps []string + if v := pkgDeps.Val(); v.Kind() == constant.String { + gopDeps = strings.Split(constant.StringVal(v), ",") + } + + if debugImport { + log.Println("==> Import", pkgImp.Path()) + } + InitThisGopPkg(pkgImp) for _, depPath := range gopDeps { imp, _ := importer.Import(depPath) p.initGopPkg(importer, imp)