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

v1.15.3 #430

Merged
merged 5 commits into from
May 18, 2024
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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 == "" {
Expand Down
15 changes: 10 additions & 5 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -156,15 +161,15 @@ func InitThisGopPkg(pkg *types.Package) {
}
}
if len(fns) > 0 {
newOverload(pkg, scope, m, fns)
newOverload(pkg, scope, m, fns, pos)
}
delete(overloads, m)
}
}
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
Expand Down Expand Up @@ -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...)
}
}

Expand Down