ast,build: add build constraints to File

Updates #3
This commit is contained in:
Michael McLoughlin
2019-01-02 21:35:17 -08:00
parent d92a147644
commit 6202c2c9aa
5 changed files with 58 additions and 9 deletions

View File

@@ -5,6 +5,7 @@ import (
"go/types"
"github.com/mmcloughlin/avo"
"github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/gotypes"
"github.com/mmcloughlin/avo/operand"
"github.com/mmcloughlin/avo/reg"
@@ -46,6 +47,28 @@ func (c *Context) Package(path string) {
c.pkg = pkg
}
func (c *Context) BuildConstraints(t buildtags.ConstraintsConvertable) {
cs := t.ToConstraints()
if err := cs.Validate(); err != nil {
c.AddError(err)
return
}
c.file.Constraints = cs
}
func (c *Context) BuildConstraint(t buildtags.ConstraintConvertable) {
c.BuildConstraints(append(c.file.Constraints, t.ToConstraint()))
}
func (c *Context) BuildConstraintExpr(expr string) {
constraint, err := buildtags.ParseConstraint(expr)
if err != nil {
c.AddError(err)
return
}
c.BuildConstraint(constraint)
}
func (c *Context) Function(name string) {
c.function = avo.NewFunction(name)
c.file.AddSection(c.function)