-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't evict global declarations (#72)
Fixes #54 As part of this PR, I revisited how we manage build tags in order to inject the `!durable` tag on input files that we generate the durable coroutine code from.
- Loading branch information
Showing
12 changed files
with
308 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package compiler | ||
|
||
import ( | ||
"go/ast" | ||
"go/build/constraint" | ||
"reflect" | ||
"slices" | ||
) | ||
|
||
func containsExpr(expr, contains constraint.Expr) bool { | ||
switch x := expr.(type) { | ||
case *constraint.AndExpr: | ||
return containsExpr(x.X, contains) || containsExpr(x.Y, contains) | ||
case *constraint.OrExpr: | ||
return containsExpr(x.X, contains) && containsExpr(x.Y, contains) | ||
default: | ||
return reflect.DeepEqual(expr, contains) | ||
} | ||
} | ||
|
||
func withBuildTag(expr constraint.Expr, buildTag *constraint.TagExpr) constraint.Expr { | ||
if buildTag == nil || containsExpr(expr, buildTag) { | ||
return expr | ||
} else if expr == nil { | ||
return buildTag | ||
} else { | ||
return &constraint.AndExpr{X: expr, Y: buildTag} | ||
} | ||
} | ||
|
||
func withoutBuildTag(expr constraint.Expr, buildTag *constraint.TagExpr) constraint.Expr { | ||
notBuildTag := &constraint.NotExpr{X: buildTag} | ||
if buildTag == nil || containsExpr(expr, notBuildTag) { | ||
return expr | ||
} else if expr == nil { | ||
return notBuildTag | ||
} else { | ||
return &constraint.AndExpr{X: expr, Y: notBuildTag} | ||
} | ||
} | ||
|
||
func parseBuildTags(file *ast.File) (constraint.Expr, error) { | ||
groups := commentGroupsOf(file) | ||
|
||
for _, group := range groups { | ||
for _, c := range group.List { | ||
if constraint.IsGoBuild(c.Text) { | ||
return constraint.Parse(c.Text) | ||
} | ||
} | ||
} | ||
|
||
var plusBuildLines constraint.Expr | ||
for _, group := range groups { | ||
for _, c := range group.List { | ||
if constraint.IsPlusBuild(c.Text) { | ||
x, err := constraint.Parse(c.Text) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if plusBuildLines == nil { | ||
plusBuildLines = x | ||
} else { | ||
plusBuildLines = &constraint.AndExpr{X: plusBuildLines, Y: x} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return plusBuildLines, nil | ||
} | ||
|
||
func stripBuildTagsOf(file *ast.File, path string) { | ||
for _, group := range commentGroupsOf(file) { | ||
group.List = slices.DeleteFunc(group.List, func(c *ast.Comment) bool { | ||
return constraint.IsGoBuild(c.Text) || constraint.IsPlusBuild(c.Text) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.