summaryrefslogtreecommitdiff
path: root/vendor/github.com/mmcloughlin/avo/printer/stubs.go
blob: 171bc62991db90578c301bd99735442d896e2e1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package printer

import (
	"github.com/mmcloughlin/avo/internal/prnt"
	"github.com/mmcloughlin/avo/ir"
)

type stubs struct {
	cfg Config
	prnt.Generator
}

// NewStubs constructs a printer for writing stub function declarations.
func NewStubs(cfg Config) Printer {
	return &stubs{cfg: cfg}
}

func (s *stubs) Print(f *ir.File) ([]byte, error) {
	s.Comment(s.cfg.GeneratedWarning())

	if len(f.Constraints) > 0 {
		s.NL()
		s.Printf(f.Constraints.GoString())
	}

	s.NL()
	s.Printf("package %s\n", s.cfg.Pkg)
	for _, fn := range f.Functions() {
		s.NL()
		s.Comment(fn.Doc...)
		for _, pragma := range fn.Pragmas {
			s.pragma(pragma)
		}
		s.Printf("%s\n", fn.Stub())
	}
	return s.Result()
}

func (s *stubs) pragma(p ir.Pragma) {
	s.Printf("//go:%s", p.Directive)
	for _, arg := range p.Arguments {
		s.Printf(" %s", arg)
	}
	s.NL()
}