Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
ensureSig
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Aug 4, 2022
1 parent b7e8546 commit 5b97cad
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
33 changes: 32 additions & 1 deletion cl/pkginfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,44 @@ func (p Package) InitDependencies() {
real := t.Real
uf, sig = real.Name(), real.Type().(*types.Signature)
}
f, _ := pkg.NewFuncWith(token.NoPos, uf, sig, nil)
f, _ := pkg.NewFuncWith(token.NoPos, uf, ensureSig(sig), nil)
f.BodyStart(pkg).
Val(vPanic).Val("notimpl").Call(1).EndStmt().
End()
}
}

const (
sigParamWithoutName = 1 << iota
sigParamWithName
sigParamInvalid = sigParamWithoutName | sigParamWithName
)

func ensureSig(sig *types.Signature) *types.Signature {
params := sig.Params()
kind := 0
for i, n := 0, params.Len(); i < n; i++ {
param := params.At(i)
if param.Name() != "" {
kind |= sigParamWithName
} else {
kind |= sigParamWithoutName
}
if kind == sigParamInvalid {
newParams := make([]*types.Var, n)
for i = 0; i < n; i++ {
param = params.At(i)
if param.Name() != "" {
param = types.NewParam(param.Pos(), param.Pkg(), "", param.Type())
}
newParams[i] = param
}
return types.NewSignature(nil, types.NewTuple(newParams...), sig.Results(), sig.Variadic())
}
}
return sig
}

func (p Package) WriteDepTo(dst io.Writer) error {
p.InitDependencies()
return gox.WriteTo(dst, p.Package, depsFile)
Expand Down
16 changes: 16 additions & 0 deletions cl/pkginfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@ package cl

import (
"bytes"
"go/types"
"os"
"testing"
)

// -----------------------------------------------------------------------------

func TestEnsureSig(t *testing.T) {
a := types.NewParam(0, nil, "", types.Typ[types.Int])
b := types.NewParam(0, nil, "b", types.Typ[types.Bool])
params := types.NewTuple(a, b)
sig := types.NewSignature(nil, params, nil, false)
if tsig := ensureSig(sig); tsig == sig {
t.Fatal("ensureSig 1:", sig, tsig)
}
params = types.NewTuple(a)
sig = types.NewSignature(nil, params, nil, false)
if tsig := ensureSig(sig); tsig != sig {
t.Fatal("ensureSig 2:", sig, tsig)
}
}

func TestPkgInfo_Basic(t *testing.T) {
pkg := testFunc(t, "Basic", `
Expand Down

0 comments on commit 5b97cad

Please sign in to comment.