You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
garble/internal/ctrlflow/ssa.go

44 lines
1.1 KiB
Go

package ctrlflow
import (
"go/constant"
"go/types"
"reflect"
"unsafe"
"golang.org/x/tools/go/ssa"
)
// setUnexportedField is used to modify unexported fields of ssa api structures.
// TODO: find an alternative way to access private fields or raise a feature request upstream
func setUnexportedField(objRaw interface{}, name string, valRaw interface{}) {
obj := reflect.ValueOf(objRaw)
for obj.Kind() == reflect.Pointer || obj.Kind() == reflect.Interface {
obj = obj.Elem()
}
field := obj.FieldByName(name)
if !field.IsValid() {
panic("invalid field: " + name)
}
fakeStruct := reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr()))
fakeStruct.Elem().Set(reflect.ValueOf(valRaw))
}
func setBlockParent(block *ssa.BasicBlock, ssaFunc *ssa.Function) {
setUnexportedField(block, "parent", ssaFunc)
}
func setBlock(instr ssa.Instruction, block *ssa.BasicBlock) {
setUnexportedField(instr, "block", block)
}
func setType(instr ssa.Instruction, typ types.Type) {
setUnexportedField(instr, "typ", typ)
}
func makeSsaInt(i int) *ssa.Const {
return ssa.NewConst(constant.MakeInt64(int64(i)), types.Typ[types.Int])
}