2018-12-11 00:18:22 -08:00
|
|
|
package printer
|
|
|
|
|
|
|
|
|
|
import (
|
2022-09-05 22:15:01 -07:00
|
|
|
"go/format"
|
|
|
|
|
|
2026-03-06 20:14:02 +00:00
|
|
|
"sources.truenas.cloud/code/avo/buildtags"
|
|
|
|
|
"sources.truenas.cloud/code/avo/internal/prnt"
|
|
|
|
|
"sources.truenas.cloud/code/avo/ir"
|
2018-12-11 00:18:22 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type stubs struct {
|
|
|
|
|
cfg Config
|
|
|
|
|
prnt.Generator
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-04 19:03:02 -08:00
|
|
|
// NewStubs constructs a printer for writing stub function declarations.
|
2018-12-11 00:18:22 -08:00
|
|
|
func NewStubs(cfg Config) Printer {
|
|
|
|
|
return &stubs{cfg: cfg}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-06 14:21:10 -08:00
|
|
|
func (s *stubs) Print(f *ir.File) ([]byte, error) {
|
2018-12-18 22:57:26 -08:00
|
|
|
s.Comment(s.cfg.GeneratedWarning())
|
2019-01-02 22:01:38 -08:00
|
|
|
|
|
|
|
|
if len(f.Constraints) > 0 {
|
2021-10-29 01:08:02 -07:00
|
|
|
constraints, err := buildtags.Format(f.Constraints)
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.AddError(err)
|
|
|
|
|
}
|
2019-01-02 22:01:38 -08:00
|
|
|
s.NL()
|
2021-10-29 01:08:02 -07:00
|
|
|
s.Printf(constraints)
|
2019-01-02 22:01:38 -08:00
|
|
|
}
|
|
|
|
|
|
2018-12-18 22:57:26 -08:00
|
|
|
s.NL()
|
|
|
|
|
s.Printf("package %s\n", s.cfg.Pkg)
|
2018-12-26 18:58:12 -08:00
|
|
|
for _, fn := range f.Functions() {
|
2018-12-18 22:57:26 -08:00
|
|
|
s.NL()
|
2018-12-27 23:01:27 -08:00
|
|
|
s.Comment(fn.Doc...)
|
2019-09-16 11:01:48 -07:00
|
|
|
for _, pragma := range fn.Pragmas {
|
|
|
|
|
s.pragma(pragma)
|
|
|
|
|
}
|
2018-12-11 00:18:22 -08:00
|
|
|
s.Printf("%s\n", fn.Stub())
|
|
|
|
|
}
|
2022-09-05 22:15:01 -07:00
|
|
|
|
|
|
|
|
// Apply formatting to the result. This is the simplest way to ensure
|
|
|
|
|
// comment formatting rules introduced in Go 1.19 are applied. See
|
|
|
|
|
// https://go.dev/doc/comment.
|
|
|
|
|
src, err := s.Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return format.Source(src)
|
2018-12-11 00:18:22 -08:00
|
|
|
}
|
2019-09-16 11:01:48 -07:00
|
|
|
|
|
|
|
|
func (s *stubs) pragma(p ir.Pragma) {
|
|
|
|
|
s.Printf("//go:%s", p.Directive)
|
|
|
|
|
for _, arg := range p.Arguments {
|
|
|
|
|
s.Printf(" %s", arg)
|
|
|
|
|
}
|
|
|
|
|
s.NL()
|
|
|
|
|
}
|