gotypes,build: add Implement (#58)

By using Implement you can provide a definition of a function, taking the signature from a stub in the package. One major benefit of this approach is it makes it easy to handle external types in the function signature.

Updates #55
This commit is contained in:
Michael McLoughlin
2019-01-22 22:35:01 -08:00
committed by GitHub
parent 9c913ee847
commit eb225e9d2c
12 changed files with 191 additions and 2 deletions

View File

@@ -111,6 +111,23 @@ func (c *Context) SignatureExpr(expr string) {
c.Signature(s)
}
// Implement starts building a function of the given name, whose type is
// specified by a stub in the containing package.
func (c *Context) Implement(name string) {
pkg := c.types()
if pkg == nil {
c.adderrormessage("no package specified")
return
}
s, err := gotypes.LookupSignature(pkg, name)
if err != nil {
c.adderror(err)
return
}
c.Function(name)
c.Signature(s)
}
func (c *Context) types() *types.Package {
if c.pkg == nil {
return nil

View File

@@ -118,6 +118,10 @@ func Doc(lines ...string) { ctx.Doc(lines...) }
// Attributes sets function attributes for the currently active function.
func Attributes(a attr.Attribute) { ctx.Attributes(a) }
// Implement starts building a function of the given name, whose type is
// specified by a stub in the containing package.
func Implement(name string) { ctx.Implement(name) }
// AllocLocal allocates size bytes in the stack of the currently active function.
// Returns a reference to the base pointer for the newly allocated region.
func AllocLocal(size int) operand.Mem { return ctx.AllocLocal(size) }