-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
136 lines (123 loc) · 2.12 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package struc
import (
"fmt"
"reflect"
)
type Type int
const (
Invalid Type = iota
Pad
Bool
Int
Int8
Uint8
Int16
Uint16
Int32
Uint32
Int64
Uint64
Float32
Float64
String
Struct
Ptr
SizeType
OffType
CustomType
)
func (t Type) Resolve(options *Options) Type {
switch t {
case OffType:
switch options.PtrSize {
case 8:
return Int8
case 16:
return Int16
case 32:
return Int32
case 64:
return Int64
default:
panic(fmt.Sprintf("unsupported ptr bits: %d", options.PtrSize))
}
case SizeType:
switch options.PtrSize {
case 8:
return Uint8
case 16:
return Uint16
case 32:
return Uint32
case 64:
return Uint64
default:
panic(fmt.Sprintf("unsupported ptr bits: %d", options.PtrSize))
}
}
return t
}
func (t Type) String() string {
return typeNames[t]
}
func (t Type) Size() int {
switch t {
case SizeType, OffType:
panic("Size_t/Off_t types must be converted to another type using options.PtrSize")
case Pad, String, Int8, Uint8, Bool:
return 1
case Int16, Uint16:
return 2
case Int32, Uint32, Float32:
return 4
case Int64, Uint64, Float64:
return 8
default:
panic("Cannot resolve size of type:" + t.String())
}
}
var typeLookup = map[string]Type{
"pad": Pad,
"bool": Bool,
"byte": Uint8,
"int8": Int8,
"uint8": Uint8,
"int16": Int16,
"uint16": Uint16,
"int32": Int32,
"uint32": Uint32,
"int64": Int64,
"uint64": Uint64,
"float32": Float32,
"float64": Float64,
"size_t": SizeType,
"off_t": OffType,
}
var typeNames = map[Type]string{
CustomType: "Custom",
}
func init() {
for name, enum := range typeLookup {
typeNames[enum] = name
}
}
type Size_t uint64
type Off_t int64
var reflectTypeMap = map[reflect.Kind]Type{
reflect.Bool: Bool,
reflect.Int8: Int8,
reflect.Int16: Int16,
reflect.Int: Int32,
reflect.Int32: Int32,
reflect.Int64: Int64,
reflect.Uint8: Uint8,
reflect.Uint16: Uint16,
reflect.Uint: Uint32,
reflect.Uint32: Uint32,
reflect.Uint64: Uint64,
reflect.Float32: Float32,
reflect.Float64: Float64,
reflect.String: String,
reflect.Struct: Struct,
reflect.Ptr: Ptr,
}