generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
382 additions
and
210 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,37 @@ | ||
package ssax | ||
|
||
import ( | ||
"iter" | ||
|
||
"github.com/dogmatiq/enginekit/optional" | ||
"golang.org/x/tools/go/ssa" | ||
) | ||
|
||
// Terminator returns the final "transfer of control" instruction in the given | ||
// block. | ||
// | ||
// If the block does not contain any instructions (as is the case for external | ||
// functions), or the terminator instruction is not of type T, ok is false. | ||
// | ||
// The instruction is always [ssa.If], [ssa.Jump], [ssa.Return], or [ssa.Panic]. | ||
func Terminator[T ssa.Instruction](b *ssa.BasicBlock) optional.Optional[T] { | ||
return optional.As[T](optional.Last(b.Instrs)) | ||
} | ||
|
||
// InstructionsBefore yields all instructions in the block that precede the | ||
// given instruction. | ||
// | ||
// It yields all instructions if inst is not in b. | ||
func InstructionsBefore(b *ssa.BasicBlock, inst ssa.Instruction) iter.Seq[ssa.Instruction] { | ||
return func(yield func(ssa.Instruction) bool) { | ||
for _, x := range b.Instrs { | ||
if x == inst { | ||
return | ||
} | ||
|
||
if !yield(x) { | ||
return | ||
} | ||
} | ||
} | ||
} |
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,63 @@ | ||
package ssax | ||
|
||
import ( | ||
"go/constant" | ||
"math" | ||
|
||
"github.com/dogmatiq/enginekit/optional" | ||
"golang.org/x/tools/go/ssa" | ||
) | ||
|
||
// Const returns the singlar constant value of v if possible. | ||
func Const(v ssa.Value) optional.Optional[constant.Value] { | ||
return optional.TryTransform( | ||
StaticValue(v), | ||
func(v ssa.Value) (constant.Value, bool) { | ||
if c, ok := v.(*ssa.Const); ok { | ||
return c.Value, true | ||
} | ||
return nil, false | ||
}, | ||
) | ||
} | ||
|
||
// AsString returns the singular constant string value of v if possible. | ||
func AsString(v ssa.Value) optional.Optional[string] { | ||
return constAs(constant.StringVal, v) | ||
} | ||
|
||
// AsBool returns the singular constant boolean value of v if possible. | ||
func AsBool(v ssa.Value) optional.Optional[bool] { | ||
return constAs(constant.BoolVal, v) | ||
} | ||
|
||
// AsInt returns the singular constant integer value of v if possible. | ||
func AsInt(v ssa.Value) optional.Optional[int] { | ||
return optional.TryTransform( | ||
Const(v), | ||
func(c constant.Value) (_ int, ok bool) { | ||
i, ok := constant.Int64Val(c) | ||
return int(i), ok && i >= math.MinInt && i <= math.MaxInt | ||
}, | ||
) | ||
} | ||
|
||
// constAsX returns the constant value of v, converted to type T by fn. | ||
func constAs[T any]( | ||
fn func(constant.Value) T, | ||
v ssa.Value, | ||
) optional.Optional[T] { | ||
return optional.TryTransform( | ||
Const(v), | ||
func(c constant.Value) (_ T, ok bool) { | ||
defer func() { | ||
if recover() != nil { | ||
// ignore panics about type conversion | ||
ok = false | ||
} | ||
}() | ||
|
||
return fn(c), true | ||
}, | ||
) | ||
} |
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,2 @@ | ||
// Package ssax contains general SSA-related utilities. | ||
package ssax |
Oops, something went wrong.