Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: xxxError use Pos instead of Position #1509

Merged
merged 7 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 25 additions & 40 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,30 +251,17 @@ func (p *nodeInterp) Position(start token.Pos) token.Position {

func (p *nodeInterp) Caller(node ast.Node) string {
if expr, ok := node.(*ast.CallExpr); ok {
node = expr.Fun
start := node.Pos()
pos := p.fset.Position(start)
f := p.files[pos.Filename]
n := int(node.End() - start)
return string(f.Code[pos.Offset : pos.Offset+n])
return p.LoadExpr(expr.Fun)
}
return "the function call"
}

func (p *nodeInterp) LoadExpr(node ast.Node) (src string, pos token.Position) {
func (p *nodeInterp) LoadExpr(node ast.Node) string {
start := node.Pos()
pos = p.fset.Position(start)
pos := p.fset.Position(start)
f := p.files[pos.Filename]
if f == nil { // not found
return
}
n := int(node.End() - start)
pos.Filename = relFile(p.workingDir, pos.Filename)
if pos.Offset+n < 0 {
log.Println("LoadExpr:", node, pos.Filename, pos.Line, pos.Offset, node.Pos(), node.End(), n)
}
src = string(f.Code[pos.Offset : pos.Offset+n])
return
return string(f.Code[pos.Offset : pos.Offset+n])
}

type loader interface {
Expand Down Expand Up @@ -374,6 +361,7 @@ func doInitMethods(ld *typeLoader) {
type pkgCtx struct {
*nodeInterp
*gmxSettings
fset *token.FileSet
cpkgs *cpackages.Importer
syms map[string]loader
inits []func()
Expand All @@ -394,7 +382,6 @@ type blockCtx struct {
*pkgCtx
pkg *gox.Package
cb *gox.CodeBuilder
fset *token.FileSet
imports map[string]pkgImp
lookups []*gox.PkgRef
clookups []*cpackages.PkgRef
Expand Down Expand Up @@ -422,27 +409,22 @@ func (bc *blockCtx) findImport(name string) (pi pkgImp, ok bool) {
return
}

func newCodeErrorf(pos *token.Position, format string, args ...interface{}) *gox.CodeError {
return &gox.CodeError{Pos: pos, Msg: fmt.Sprintf(format, args...)}
}

func (p *pkgCtx) newCodeError(start token.Pos, msg string) error {
pos := p.Position(start)
return &gox.CodeError{Pos: &pos, Msg: msg}
func (p *pkgCtx) newCodeError(pos token.Pos, msg string) error {
return &gox.CodeError{Fset: p.nodeInterp, Pos: pos, Msg: msg}
}

func (p *pkgCtx) newCodeErrorf(start token.Pos, format string, args ...interface{}) error {
pos := p.Position(start)
return newCodeErrorf(&pos, format, args...)
func (p *pkgCtx) newCodeErrorf(pos token.Pos, format string, args ...interface{}) error {
return &gox.CodeError{Fset: p.nodeInterp, Pos: pos, Msg: fmt.Sprintf(format, args...)}
}

func (p *pkgCtx) handleCodeErrorf(pos *token.Position, format string, args ...interface{}) {
p.handleErr(newCodeErrorf(pos, format, args...))
/*
func (p *pkgCtx) handleError(pos token.Pos, msg string) {
p.handleErr(p.newCodeError(pos, msg))
}
*/

func (p *pkgCtx) handleErrorf(start token.Pos, format string, args ...interface{}) {
pos := p.Position(start)
p.handleErr(newCodeErrorf(&pos, format, args...))
func (p *pkgCtx) handleErrorf(pos token.Pos, format string, args ...interface{}) {
p.handleErr(p.newCodeErrorf(pos, format, args...))
}

func (p *pkgCtx) handleErr(err error) {
Expand Down Expand Up @@ -526,6 +508,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
fset: fset, files: files, workingDir: workingDir,
}
ctx := &pkgCtx{
fset: fset,
syms: make(map[string]loader), nodeInterp: interp, generics: make(map[string]bool),
}
confGox := &gox.Config{
Expand All @@ -539,6 +522,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
DefaultGoFile: defaultGoFile,
NoSkipConstant: conf.NoSkipConstant,
PkgPathIox: ioxPkgPath,
DbgPositioner: interp,
}
if conf.Recorder != nil {
confGox.Recorder = &goxRecorder{rec: conf.Recorder}
Expand Down Expand Up @@ -573,7 +557,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
fileLine := !conf.NoFileLine
fileScope := types.NewScope(p.Types.Scope(), f.Pos(), f.End(), fpath)
ctx := &blockCtx{
pkg: p, pkgCtx: ctx, cb: p.CB(), fset: p.Fset, targetDir: targetDir, fileScope: fileScope,
pkg: p, pkgCtx: ctx, cb: p.CB(), targetDir: targetDir, fileScope: fileScope,
fileLine: fileLine, relativePath: conf.RelativePath, isClass: f.IsClass, rec: conf.Recorder,
c2goBase: c2goBase(conf.C2goBase), imports: make(map[string]pkgImp), isGopFile: true,
}
Expand All @@ -588,7 +572,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
f := fromgo.ASTFile(gof, 0)
gofiles = append(gofiles, f)
ctx := &blockCtx{
pkg: p, pkgCtx: ctx, cb: p.CB(), fset: p.Fset, targetDir: targetDir,
pkg: p, pkgCtx: ctx, cb: p.CB(), targetDir: targetDir,
imports: make(map[string]pkgImp),
}
preloadFile(p, ctx, fpath, f, false, false)
Expand Down Expand Up @@ -903,11 +887,12 @@ func preloadFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, gopFil
case token.TYPE:
for _, spec := range d.Specs {
t := spec.(*ast.TypeSpec)
name := t.Name.Name
tName := t.Name
name := tName.Name
if debugLoad {
log.Println("==> Preload type", name)
}
pos := t.Name.Pos()
pos := tName.Pos()
ld := getTypeLoader(parent, syms, pos, name)
defs := ctx.pkg.NewTypeDefs()
if gopFile {
Expand All @@ -918,13 +903,13 @@ func preloadFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, gopFil
if debugLoad {
log.Println("==> Load > AliasType", name)
}
defs.AliasType(name, toType(ctx, t.Type), t.Pos())
defs.AliasType(name, toType(ctx, t.Type), tName)
return
}
if debugLoad {
log.Println("==> Load > NewType", name)
}
decl := defs.NewType(name, pos)
decl := defs.NewType(name, tName)
if t.Doc != nil {
defs.SetComments(t.Doc)
} else if d.Doc != nil {
Expand All @@ -936,7 +921,7 @@ func preloadFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, gopFil
}
decl.InitType(ctx.pkg, toType(ctx, t.Type))
if rec := ctx.recorder(); rec != nil {
rec.Def(t.Name, decl.Type().Obj())
rec.Def(tName, decl.Type().Obj())
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,8 @@ func compileCallArgs(fn *fnType, fnt types.Type, ctx *blockCtx, v *ast.CallExpr,
if e, ok := r.(error); ok {
err = e
} else {
src, pos := ctx.LoadExpr(v)
err = newCodeErrorf(&pos, "compile func %v error: %v", src, r)
src := ctx.LoadExpr(v)
err = ctx.newCodeErrorf(v.Pos(), "compile func %v error: %v", src, r)
}
ctx.cb.InternalStack().SetLen(n)
}
Expand Down Expand Up @@ -630,7 +630,7 @@ retry:
typ = t.Underlying()
goto retry
}
src, _ := ctx.LoadExpr(toNode)
src := ctx.LoadExpr(toNode)
err := ctx.newCodeErrorf(lambda.Pos(), "cannot use lambda literal as type %v in %v to %v", ftyp, flag, src)
panic(err)
}
Expand Down Expand Up @@ -789,8 +789,8 @@ func compileStructLitInKeyVal(ctx *blockCtx, elts []ast.Expr, t *types.Struct, t
if idx >= 0 {
ctx.cb.Val(idx)
} else {
src, pos := ctx.LoadExpr(name)
err := newCodeErrorf(&pos, "%s undefined (type %v has no field or method %s)", src, typ, name.Name)
src := ctx.LoadExpr(name)
err := ctx.newCodeErrorf(name.Pos(), "%s undefined (type %v has no field or method %s)", src, typ, name.Name)
panic(err)
}
switch expr := kv.Value.(type) {
Expand Down
8 changes: 4 additions & 4 deletions cl/func_type_and_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func getRecvTypeName(ctx *pkgCtx, recv *ast.FieldList, handleErr bool) (string,
return t.Name, true
}
if handleErr {
src, pos := ctx.LoadExpr(typ)
ctx.handleCodeErrorf(&pos, "invalid receiver type %v (%v is not a defined type)", src, src)
src := ctx.LoadExpr(typ)
ctx.handleErrorf(typ.Pos(), "invalid receiver type %v (%v is not a defined type)", src, src)
}
return "", false
}
Expand Down Expand Up @@ -399,8 +399,8 @@ func toInt64(ctx *blockCtx, e ast.Expr, emsg string) int64 {
return v
}
}
src, pos := ctx.LoadExpr(e)
panic(newCodeErrorf(&pos, emsg, src))
src := ctx.LoadExpr(e)
panic(ctx.newCodeErrorf(e.Pos(), emsg, src))
}

func toInterfaceType(ctx *blockCtx, v *ast.InterfaceType) types.Type {
Expand Down
Loading