gitlab.com/Raven-IO/raven-delve@v1.22.4/pkg/dwarf/godwarf/fakes.go (about)

     1  package godwarf
     2  
     3  import (
     4  	"fmt"
     5  	"math/bits"
     6  	"reflect"
     7  )
     8  
     9  // FakeSliceType synthesizes a slice type with the given field type.
    10  func FakeSliceType(fieldType Type) Type {
    11  	return &SliceType{
    12  		StructType: StructType{
    13  			CommonType: CommonType{
    14  				ByteSize: 24,
    15  				Name:     "",
    16  			},
    17  			StructName: "[]" + fieldType.Common().Name,
    18  			Kind:       "struct",
    19  			Field:      nil,
    20  		},
    21  		ElemType: fieldType,
    22  	}
    23  }
    24  
    25  // FakeBasicType synthesizes a basic type numeric type (int8, uint16,
    26  // float32, etc)
    27  func FakeBasicType(name string, bitSize int) Type {
    28  	byteSize := bitSize / 8
    29  	szr := bits.OnesCount64(uint64(byteSize^(byteSize-1))) - 1 // position of rightmost 1 bit, minus 1
    30  
    31  	basic := func(kind reflect.Kind) BasicType {
    32  		return BasicType{
    33  			CommonType: CommonType{
    34  				ByteSize:    int64(byteSize),
    35  				Name:        fmt.Sprintf("%s%d", name, bitSize),
    36  				ReflectKind: kind,
    37  			},
    38  			BitSize:   int64(bitSize),
    39  			BitOffset: 0,
    40  		}
    41  	}
    42  
    43  	switch name {
    44  	case "int":
    45  		return &IntType{BasicType: basic(reflect.Int8 + reflect.Kind(szr))}
    46  	case "uint":
    47  		return &UintType{BasicType: basic(reflect.Uint8 + reflect.Kind(szr))}
    48  	case "float":
    49  		return &FloatType{BasicType: basic(reflect.Float32 + reflect.Kind(szr-2))}
    50  	case "complex":
    51  		return &ComplexType{BasicType: basic(reflect.Complex64 + reflect.Kind(szr-3))}
    52  	default:
    53  		panic("unsupported")
    54  	}
    55  }