Repository: goplus/gop
Pull request #1788 (2024-03-05 18:18:21)

rm typesRecorder (merge to goxRecorder


Patch:
diff --git a/cl/builtin.go b/cl/builtin.go
index dda8c6d3..7f2ad239 100644
--- a/cl/builtin.go
+++ b/cl/builtin.go
@@ -25,7 +25,7 @@ import (
 
 // -----------------------------------------------------------------------------
 
-func initMathBig(pkg *gox.Package, conf *gox.Config, big gox.PkgRef) {
+func initMathBig(_ *gox.Package, conf *gox.Config, big gox.PkgRef) {
 	conf.UntypedBigInt = big.Ref("UntypedBigint").Type().(*types.Named)
 	conf.UntypedBigRat = big.Ref("UntypedBigrat").Type().(*types.Named)
 	conf.UntypedBigFloat = big.Ref("UntypedBigfloat").Type().(*types.Named)
@@ -38,7 +38,7 @@ func initBuiltinFns(builtin *types.Package, scope *types.Scope, pkg gox.PkgRef,
 	}
 }
 
-func initBuiltin(pkg *gox.Package, builtin *types.Package, os, fmt, ng, iox, buil gox.PkgRef) {
+func initBuiltin(_ *gox.Package, builtin *types.Package, os, fmt, ng, iox, buil gox.PkgRef) {
 	scope := builtin.Scope()
 	if ng.Types != nil {
 		typs := []string{"bigint", "bigrat", "bigfloat"}
diff --git a/cl/compile.go b/cl/compile.go
index f342c276..bde0459b 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -371,24 +371,24 @@ type blockCtx struct {
 	relBaseDir string
 	classRecv  *ast.FieldList // available when gmxSettings != nil
 	fileScope  *types.Scope   // only valid when isGopFile
-	rec        *typesRecorder
+	rec        *goxRecorder
 
 	fileLine  bool
 	isClass   bool
 	isGopFile bool // is Go+ file or not
 }
 
-func (bc *blockCtx) recorder() *typesRecorder {
-	if bc.isGopFile {
-		return bc.rec
+func (p *blockCtx) recorder() *goxRecorder {
+	if p.isGopFile {
+		return p.rec
 	}
 	return nil
 }
 
-func (bc *blockCtx) findImport(name string) (pi pkgImp, ok bool) {
-	pi, ok = bc.imports[name]
-	if !ok && bc.autoimps != nil {
-		pi, ok = bc.autoimps[name]
+func (p *blockCtx) findImport(name string) (pi pkgImp, ok bool) {
+	pi, ok = p.imports[name]
+	if !ok && p.autoimps != nil {
+		pi, ok = p.autoimps[name]
 	}
 	return
 }
@@ -505,10 +505,10 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
 		PkgPathIox:      ioxPkgPath,
 		DbgPositioner:   interp,
 	}
-	var rec *typesRecorder
+	var rec *goxRecorder
 	if conf.Recorder != nil {
-		rec = newTypeRecord(conf.Recorder)
-		confGox.Recorder = &goxRecorder{rec: rec}
+		rec = newRecorder(conf.Recorder)
+		confGox.Recorder = rec
 	}
 	if enableRecover {
 		defer func() {
@@ -571,14 +571,14 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
 	}
 
 	gofiles := make([]*ast.File, 0, len(pkg.GoFiles))
-	for fpath, gof := range pkg.GoFiles {
+	for _, gof := range pkg.GoFiles {
 		f := fromgo.ASTFile(gof, 0)
 		gofiles = append(gofiles, f)
 		ctx := &blockCtx{
 			pkg: p, pkgCtx: ctx, cb: p.CB(), relBaseDir: relBaseDir,
 			imports: make(map[string]pkgImp),
 		}
-		preloadFile(p, ctx, fpath, f, skippingGoFile, false)
+		preloadFile(p, ctx, f, skippingGoFile, false)
 	}
 
 	initGopPkg(ctx, p, gopSyms)
@@ -869,7 +869,7 @@ func preloadGopFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, con
 			})
 		}
 	}
-	preloadFile(p, ctx, file, f, goFile, !conf.Outline)
+	preloadFile(p, ctx, f, goFile, !conf.Outline)
 	if goxTestFile {
 		parent.inits = append(parent.inits, func() {
 			old, _ := p.SetCurFile(testingGoFile, true)
@@ -893,7 +893,7 @@ retry:
 	panic("TODO: parseTypeEmbedName unexpected")
 }
 
-func preloadFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, goFile string, genFnBody bool) {
+func preloadFile(p *gox.Package, ctx *blockCtx, f *ast.File, goFile string, genFnBody bool) {
 	parent := ctx.pkgCtx
 	syms := parent.syms
 	old, _ := p.SetCurFile(goFile, true)
diff --git a/cl/expr.go b/cl/expr.go
index 365598b1..8ffc3b12 100644
--- a/cl/expr.go
+++ b/cl/expr.go
@@ -180,10 +180,10 @@ find:
 	if rec := ctx.recorder(); rec != nil {
 		e := cb.Get(-1)
 		if oldo != nil && gox.IsTypeEx(e.Type) { // for builtin object
-			rec.recordIdent(ctx, ident, oldo)
+			rec.recordIdent(ident, oldo)
 			return
 		}
-		rec.recordIdent(ctx, ident, o)
+		rec.recordIdent(ident, o)
 	}
 	return
 }
@@ -900,7 +900,7 @@ func compileFuncLit(ctx *blockCtx, v *ast.FuncLit) {
 	comments, once := cb.BackupComments()
 	sig := toFuncType(ctx, v.Type, nil, nil)
 	if rec := ctx.recorder(); rec != nil {
-		rec.recordFuncLit(ctx, v, sig)
+		rec.recordFuncLit(v, sig)
 	}
 	fn := cb.NewClosureWith(sig)
 	if body := v.Body; body != nil {
@@ -988,7 +988,7 @@ const (
 	compositeLitKeyVal = 1
 )
 
-func checkCompositeLitElts(ctx *blockCtx, elts []ast.Expr) (kind int) {
+func checkCompositeLitElts(elts []ast.Expr) (kind int) {
 	for _, elt := range elts {
 		if _, ok := elt.(*ast.KeyValueExpr); ok {
 			return compositeLitKeyVal
@@ -1111,7 +1111,7 @@ func compileCompositeLit(ctx *blockCtx, v *ast.CompositeLit, expected types.Type
 func compileCompositeLitEx(ctx *blockCtx, v *ast.CompositeLit, expected types.Type, mapOrStructOnly bool) error {
 	var hasPtr bool
 	var typ, underlying types.Type
-	var kind = checkCompositeLitElts(ctx, v.Elts)
+	var kind = checkCompositeLitElts(v.Elts)
 	if v.Type != nil {
 		typ = toType(ctx, v.Type)
 		underlying = getUnderlying(ctx, typ)
@@ -1158,7 +1158,7 @@ func compileCompositeLitEx(ctx *blockCtx, v *ast.CompositeLit, expected types.Ty
 		typ = expected
 	}
 	if rec := ctx.recorder(); rec != nil {
-		rec.recordCompositeLit(ctx, v, typ)
+		rec.recordCompositeLit(v, typ)
 	}
 	return nil
 }
diff --git a/cl/func_type_and_var.go b/cl/func_type_and_var.go
index 03fa6fdb..5d139dd6 100644
--- a/cl/func_type_and_var.go
+++ b/cl/func_type_and_var.go
@@ -114,7 +114,7 @@ func toParam(ctx *blockCtx, fld *ast.Field, args []*gox.Param) []*gox.Param {
 func toType(ctx *blockCtx, typ ast.Expr) (t types.Type) {
 	if rec := ctx.recorder(); rec != nil {
 		defer func() {
-			rec.recordType(ctx, typ, t)
+			rec.recordType(typ, t)
 		}()
 	}
 	switch v := typ.(type) {
@@ -232,7 +232,7 @@ func toIdentType(ctx *blockCtx, ident *ast.Ident) (ret types.Type) {
 	if rec := ctx.recorder(); rec != nil {
 		defer func() {
 			if obj != nil {
-				rec.recordIdent(ctx, ident, obj)
+				rec.recordIdent(ident, obj)
 			}
 		}()
 	}
diff --git a/cl/recorder.go b/cl/recorder.go
index 4b480761..8ebe7071 100644
--- a/cl/recorder.go
+++ b/cl/recorder.go
@@ -27,7 +27,13 @@ import (
 )
 
 type goxRecorder struct {
-	rec *typesRecorder
+	Recorder
+	types map[ast.Expr]types.TypeAndValue
+}
+
+func newRecorder(rec Recorder) *goxRecorder {
+	types := make(map[ast.Expr]types.TypeAndValue)
+	return &goxRecorder{rec, types}
 }
 
 // Member maps identifiers to the objects they denote.
@@ -39,45 +45,40 @@ func (p *goxRecorder) Member(id ast.Node, obj types.Object) {
 		if _, ok := fromgo.CheckIdent(sel); !ok {
 			var tv types.TypeAndValue
 			// check v.X call result by value
-			if f, ok := obj.(*types.Var); ok && f.IsField() && p.rec.checkExprByValue(v.X) {
+			if f, ok := obj.(*types.Var); ok && f.IsField() && p.checkExprByValue(v.X) {
 				tv = typesutil.NewTypeAndValueForValue(obj.Type(), nil, typesutil.Value)
 			} else {
 				tv = typesutil.NewTypeAndValueForObject(obj)
 			}
-			p.rec.Use(sel, obj)
-			p.rec.Type(v, tv)
+			p.Use(sel, obj)
+			p.Type(v, tv)
 		}
 	case *ast.Ident: // it's in a classfile and impossible converted from Go
-		p.rec.Use(v, obj)
-		p.rec.Type(v, typesutil.NewTypeAndValueForObject(obj))
+		p.Use(v, obj)
+		p.Type(v, typesutil.NewTypeAndValueForObject(obj))
 	}
 }
 
 func (p *goxRecorder) Call(id ast.Node, obj types.Object) {
 	switch v := id.(type) {
 	case *ast.Ident:
-		p.rec.Use(v, obj)
-		p.rec.Type(v, typesutil.NewTypeAndValueForObject(obj))
+		p.Use(v, obj)
+		p.Type(v, typesutil.NewTypeAndValueForObject(obj))
 	case *ast.SelectorExpr:
-		p.rec.Use(v.Sel, obj)
-		p.rec.Type(v, typesutil.NewTypeAndValueForObject(obj))
+		p.Use(v.Sel, obj)
+		p.Type(v, typesutil.NewTypeAndValueForObject(obj))
 	case *ast.CallExpr:
 		switch id := v.Fun.(type) {
 		case *ast.Ident:
-			p.rec.Use(id, obj)
+			p.Use(id, obj)
 		case *ast.SelectorExpr:
-			p.rec.Use(id.Sel, obj)
+			p.Use(id.Sel, obj)
 		}
-		p.rec.Type(v.Fun, typesutil.NewTypeAndValueForObject(obj))
+		p.Type(v.Fun, typesutil.NewTypeAndValueForObject(obj))
 	}
 }
 
-type typesRecorder struct {
-	Recorder
-	types map[ast.Expr]types.TypeAndValue
-}
-
-func (rec *typesRecorder) checkExprByValue(v ast.Expr) bool {
+func (rec *goxRecorder) checkExprByValue(v ast.Expr) bool {
 	if tv, ok := rec.types[v]; ok {
 		switch v.(type) {
 		case *ast.CallExpr:
@@ -93,12 +94,12 @@ func (rec *typesRecorder) checkExprByValue(v ast.Expr) bool {
 	return false
 }
 
-func (rec *typesRecorder) Type(expr ast.Expr, tv types.TypeAndValue) {
+func (rec *goxRecorder) Type(expr ast.Expr, tv types.TypeAndValue) {
 	rec.types[expr] = tv
 	rec.Recorder.Type(expr, tv)
 }
 
-func (rec *typesRecorder) instantiate(expr ast.Expr, org, typ types.Type) {
+func (rec *goxRecorder) instantiate(expr ast.Expr, _, typ types.Type) {
 	// check gox TyOverloadNamed
 	if tv, ok := rec.types[expr]; ok {
 		tv.Type = typ
@@ -118,17 +119,13 @@ func (rec *typesRecorder) instantiate(expr ast.Expr, org, typ types.Type) {
 	}
 }
 
-func newTypeRecord(rec Recorder) *typesRecorder {
-	return &typesRecorder{rec, make(map[ast.Expr]types.TypeAndValue)}
-}
-
-func (rec *typesRecorder) recordTypeValue(ctx *blockCtx, expr ast.Expr, mode typesutil.OperandMode) {
+func (rec *goxRecorder) recordTypeValue(ctx *blockCtx, expr ast.Expr, mode typesutil.OperandMode) {
 	e := ctx.cb.Get(-1)
 	t, _ := gox.DerefType(e.Type)
 	rec.Type(expr, typesutil.NewTypeAndValueForValue(t, e.CVal, mode))
 }
 
-func (rec *typesRecorder) indexExpr(ctx *blockCtx, expr *ast.IndexExpr) {
+func (rec *goxRecorder) indexExpr(ctx *blockCtx, expr *ast.IndexExpr) {
 	if tv, ok := rec.types[expr.X]; ok {
 		switch tv.Type.(type) {
 		case *types.Map:
@@ -151,7 +148,7 @@ func (rec *typesRecorder) indexExpr(ctx *blockCtx, expr *ast.IndexExpr) {
 	rec.recordTypeValue(ctx, expr, op)
 }
 
-func (rec *typesRecorder) unaryExpr(ctx *blockCtx, expr *ast.UnaryExpr) {
+func (rec *goxRecorder) unaryExpr(ctx *blockCtx, expr *ast.UnaryExpr) {
 	switch expr.Op {
 	case token.ARROW:
 		rec.recordTypeValue(ctx, expr, typesutil.CommaOK)
@@ -160,7 +157,7 @@ func (rec *typesRecorder) unaryExpr(ctx *blockCtx, expr *ast.UnaryExpr) {
 	}
 }
 
-func (rec *typesRecorder) recordCallExpr(ctx *blockCtx, v *ast.CallExpr, fnt types.Type) {
+func (rec *goxRecorder) recordCallExpr(ctx *blockCtx, v *ast.CallExpr, fnt types.Type) {
 	e := ctx.cb.Get(-1)
 	if _, ok := rec.types[v.Fun]; !ok {
 		rec.Type(v.Fun, typesutil.NewTypeAndValueForValue(fnt, nil, typesutil.Value))
@@ -168,26 +165,26 @@ func (rec *typesRecorder) recordCallExpr(ctx *blockCtx, v *ast.CallExpr, fnt typ
 	rec.Type(v, typesutil.NewTypeAndValueForCallResult(e.Type, e.CVal))
 }
 
-func (rec *typesRecorder) recordCompositeLit(ctx *blockCtx, v *ast.CompositeLit, typ types.Type) {
+func (rec *goxRecorder) recordCompositeLit(v *ast.CompositeLit, typ types.Type) {
 	rec.Type(v.Type, typesutil.NewTypeAndValueForType(typ))
 	rec.Type(v, typesutil.NewTypeAndValueForValue(typ, nil, typesutil.Value))
 }
 
-func (rec *typesRecorder) recordFuncLit(ctx *blockCtx, v *ast.FuncLit, typ types.Type) {
+func (rec *goxRecorder) recordFuncLit(v *ast.FuncLit, typ types.Type) {
 	rec.Type(v.Type, typesutil.NewTypeAndValueForType(typ))
 	rec.Type(v, typesutil.NewTypeAndValueForValue(typ, nil, typesutil.Value))
 }
 
-func (rec *typesRecorder) recordType(ctx *blockCtx, typ ast.Expr, t types.Type) {
+func (rec *goxRecorder) recordType(typ ast.Expr, t types.Type) {
 	rec.Type(typ, typesutil.NewTypeAndValueForType(t))
 }
 
-func (rec *typesRecorder) recordIdent(ctx *blockCtx, ident *ast.Ident, obj types.Object) {
+func (rec *goxRecorder) recordIdent(ident *ast.Ident, obj types.Object) {
 	rec.Use(ident, obj)
 	rec.Type(ident, typesutil.NewTypeAndValueForObject(obj))
 }
 
-func (rec *typesRecorder) recordExpr(ctx *blockCtx, expr ast.Expr, rhs bool) {
+func (rec *goxRecorder) recordExpr(ctx *blockCtx, expr ast.Expr, _ bool) {
 	switch v := expr.(type) {
 	case *ast.Ident:
 	case *ast.BasicLit:


Test patch:
diff --git a/cl/builtin_test.go b/cl/builtin_test.go
index 73a88613..cdb95b97 100644
--- a/cl/builtin_test.go
+++ b/cl/builtin_test.go
@@ -211,7 +211,7 @@ func TestErrPreloadFile(t *testing.T) {
 				}},
 			},
 		}
-		preloadFile(pkg, ctx, "foo.gop", &ast.File{Decls: decls}, "", true)
+		preloadFile(pkg, ctx, &ast.File{Decls: decls}, "", true)
 	})
 	t.Run("OverloadFuncDecl: unknown func", func(t *testing.T) {
 		defer func() {
@@ -228,7 +228,7 @@ func TestErrPreloadFile(t *testing.T) {
 				Operator: true,
 			},
 		}
-		preloadFile(pkg, ctx, "foo.gop", &ast.File{Decls: decls}, "", true)
+		preloadFile(pkg, ctx, &ast.File{Decls: decls}, "", true)
 	})
 	t.Run("unknown decl", func(t *testing.T) {
 		defer func() {
@@ -239,7 +239,7 @@ func TestErrPreloadFile(t *testing.T) {
 		decls := []ast.Decl{
 			&ast.BadDecl{},
 		}
-		preloadFile(pkg, ctx, "foo.gop", &ast.File{Decls: decls}, "", true)
+		preloadFile(pkg, ctx, &ast.File{Decls: decls}, "", true)
 	})
 }