diff --git a/README.md b/README.md index dd6348b..0f90b77 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ gogen - Code generator for the Go language [![Coverage Status](https://codecov.io/gh/goplus/gogen/branch/main/graph/badge.svg)](https://codecov.io/gh/goplus/gogen) [![GoDoc](https://pkg.go.dev/badge/github.com/goplus/gogen.svg)](https://pkg.go.dev/github.com/goplus/gogen) +`gogen` is a general-purpose Go code generation toolkit. Like the Go compiler, It can perform type checking of expressions. For example, if you generate an expression like `"Hello" + 1`, it will report the corresponding error. + ## Quick start Create a file named `hellogen.go`, like below: diff --git a/func.go b/func.go index dfb80a3..1473b10 100644 --- a/func.go +++ b/func.go @@ -132,6 +132,7 @@ func (p *Func) End(cb *CodeBuilder, src ast.Node) { } } +// NewFuncDecl creates a new function without function body (declaration only). func (p *Package) NewFuncDecl(pos token.Pos, name string, sig *types.Signature) *Func { f, err := p.NewFuncWith(pos, name, sig, nil) if err != nil { @@ -142,7 +143,7 @@ func (p *Package) NewFuncDecl(pos token.Pos, name string, sig *types.Signature) return f } -// NewFunc func +// NewFunc creates a new function (should have a function body). func (p *Package) NewFunc(recv *Param, name string, params, results *Tuple, variadic bool) *Func { sig := types.NewSignatureType(recv, nil, nil, params, results, variadic) f, err := p.NewFuncWith(token.NoPos, name, sig, nil) @@ -159,7 +160,7 @@ func getRecv(recvTypePos func() token.Pos) token.Pos { return token.NoPos } -// NewFuncWith func +// NewFuncWith creates a new function (should have a function body). func (p *Package) NewFuncWith( pos token.Pos, name string, sig *types.Signature, recvTypePos func() token.Pos) (*Func, error) { if name == "" { diff --git a/import.go b/import.go index f89e301..5959144 100644 --- a/import.go +++ b/import.go @@ -97,6 +97,11 @@ func isGopCommon(name string) bool { // InitThisGopPkg initializes a Go+ package. func InitThisGopPkg(pkg *types.Package) { + InitThisGopPkgEx(pkg, nil) +} + +// InitThisGopPkg initializes a Go+ package. pos map overload name to postion. +func InitThisGopPkgEx(pkg *types.Package, pos map[string]token.Pos) { scope := pkg.Scope() gopos := make([]string, 0, 4) overloads := make(map[omthd][]types.Object) @@ -156,7 +161,7 @@ func InitThisGopPkg(pkg *types.Package) { } } if len(fns) > 0 { - newOverload(pkg, scope, m, fns) + newOverload(pkg, scope, m, fns, pos) } delete(overloads, m) } @@ -164,7 +169,7 @@ func InitThisGopPkg(pkg *types.Package) { for key, items := range overloads { off := len(key.name) + 2 fns := overloadFuncs(off, items) - newOverload(pkg, scope, key, fns) + newOverload(pkg, scope, key, fns, pos) } for name, items := range onameds { off := len(name) + 2 @@ -310,19 +315,19 @@ func checkOverloads(scope *types.Scope, gopoName string) (ret []string, exists b return } -func newOverload(pkg *types.Package, scope *types.Scope, m omthd, fns []types.Object) { +func newOverload(pkg *types.Package, scope *types.Scope, m omthd, fns []types.Object, pos map[string]token.Pos) { if m.typ == nil { if debugImport { log.Println("==> NewOverloadFunc", m.name) } - o := NewOverloadFunc(token.NoPos, pkg, m.name, fns...) + o := NewOverloadFunc(pos[m.name], pkg, m.name, fns...) scope.Insert(o) checkGoptsx(pkg, scope, m.name, o) } else { if debugImport { log.Println("==> NewOverloadMethod", m.typ.Obj().Name(), m.name) } - NewOverloadMethod(m.typ, token.NoPos, pkg, m.name, fns...) + NewOverloadMethod(m.typ, pos[m.typ.Obj().Name()+"."+m.name], pkg, m.name, fns...) } }