This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
type.go
338 lines (301 loc) · 7.06 KB
/
type.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package micheline
import (
"encoding/json"
"fmt"
"strconv"
"time"
"blockwatch.cc/tzgo/tezos"
)
type Type struct {
Prim
}
// Extra Types
const (
TypeStruct = "struct"
TypeUnion = "union"
)
// Default names
const (
CONST_ENTRYPOINT = "@entrypoint"
CONST_KEY = "@key"
CONST_VALUE = "@value"
CONST_ITEM = "@item"
CONST_PARAM = "@param"
CONST_RETURN = "@return"
CONST_UNION_LEFT = "@or_0"
CONST_UNION_RIGHT = "@or_1"
)
type Typedef struct {
Name string `json:"name"` // annotation label | @key | @value | @item | @params | @return
Type string `json:"type"` // opcode or struct | union
Optional bool `json:"optional,omitempty"` // Union only
Args []Typedef `json:"args,omitempty"`
}
func NewType(p Prim) Type {
return Type{p.Clone()}
}
func NewTypePtr(p Prim) *Type {
return &Type{p.Clone()}
}
func (t *Type) UnmarshalBinary(buf []byte) error {
return t.Prim.UnmarshalBinary(buf)
}
func (t Type) Clone() Type {
return Type{t.Prim.Clone()}
}
func (t Type) Label() string {
return t.GetVarAnnoAny()
}
func (t Type) HasLabel() bool {
return t.HasAnno()
}
func (t Type) IsEqual(t2 Type) bool {
return IsEqualPrim(t.Prim, t2.Prim, false)
}
func (t Type) IsEqualWithAnno(t2 Type) bool {
return IsEqualPrim(t.Prim, t2.Prim, true)
}
func (t Type) Left() Type {
if len(t.Args) > 0 {
return Type{t.Args[0]}
}
return Type{}
}
func (t Type) Right() Type {
if len(t.Args) > 1 {
return Type{t.Args[1]}
}
return Type{}
}
func (t Type) Typedef(name string) Typedef {
return buildTypedef(name, t.Prim)
}
func (t Type) TypedefPtr(name string) *Typedef {
td := buildTypedef(name, t.Prim)
return &td
}
func (t Type) MarshalJSON() ([]byte, error) {
return json.Marshal(buildTypedef("", t.Prim))
}
func buildTypedef(name string, typ Prim) Typedef {
if typ.HasAnno() {
name = typ.GetVarAnnoAny()
}
td := Typedef{
Name: name,
Type: typ.OpCode.String(),
}
switch typ.OpCode {
case T_LIST, T_SET:
td.Args = []Typedef{
buildTypedef(CONST_ITEM, typ.Args[0]),
}
case T_MAP, T_BIG_MAP:
td.Args = []Typedef{
buildTypedef(CONST_KEY, typ.Args[0]),
buildTypedef(CONST_VALUE, typ.Args[1]),
}
case T_CONTRACT:
td.Args = make([]Typedef, len(typ.Args))
for i, v := range typ.Args {
td.Args[i] = buildTypedef(strconv.Itoa(i), v)
}
case T_TICKET:
td.Args = []Typedef{
buildTypedef(CONST_VALUE, typ.Args[0]),
}
case T_LAMBDA:
td.Args = make([]Typedef, len(typ.Args))
if len(typ.Args) > 0 {
td.Args[0] = buildTypedef(CONST_PARAM, typ.Args[0])
}
if len(typ.Args) > 1 {
td.Args[1] = buildTypedef(CONST_RETURN, typ.Args[1])
}
case T_PAIR:
typs := typ.UnfoldPair(Type{typ})
td.Type = TypeStruct
td.Args = make([]Typedef, len(typs))
for i, v := range typs {
td.Args[i] = buildTypedef(strconv.Itoa(i), v)
}
case T_OPTION:
child := buildTypedef(name, typ.Args[0])
td.Optional = true
td.Type = child.Type
td.Args = child.Args
case T_OR:
td.Type = TypeUnion
td.Args = make([]Typedef, 0)
label := CONST_UNION_LEFT
for _, v := range typ.Args {
child := buildTypedef(label, v)
if child.Type == TypeUnion {
td.Args = append(td.Args, child.Args...)
} else {
td.Args = append(td.Args, child)
}
label = CONST_UNION_RIGHT
}
case T_SAPLING_STATE, T_SAPLING_TRANSACTION:
td.Type += fmt.Sprintf("(%d)", typ.Args[0].Int.Int64())
default:
// int
// nat
// string
// bytes
// mutez
// bool
// key_hash
// timestamp
// address
// key
// unit
// signature
// operation
// chain_id
// unit
// bls12_381_g1
// bls12_381_g2
// bls12_381_fr
// sapling_state
// sapling_transaction
// never
return Typedef{
Name: name,
Type: typ.OpCode.String(),
}
}
return td
}
// build matching type tree for value
func (p Prim) BuildType() Type {
// Note: don't set WasPacked flag recursively on all children; we set this flag
// once on the top level type during dynamic type detection so that comb unfolding
// works
t := Prim{}
// t := Prim{WasPacked: true}
if p.OpCode.IsTypeCode() {
t.OpCode = p.OpCode
}
switch p.Type {
case PrimInt:
t.OpCode = p.Type.TypeCode()
t.Type = PrimNullary
case PrimBytes:
t.Type = PrimNullary
// detect address encoding first
var addr tezos.Address
if err := addr.UnmarshalBinary(p.Bytes); err == nil {
t.OpCode = T_ADDRESS
} else {
t.OpCode = p.Type.TypeCode()
}
case PrimString:
t.Type = PrimNullary
if len(p.String) > 0 {
// detect timestamp and address encoding first
if _, err := time.Parse(time.RFC3339, p.String); err == nil {
t.OpCode = T_TIMESTAMP
} else if _, err := tezos.ParseAddress(p.String); err == nil {
t.OpCode = T_ADDRESS
} else {
t.OpCode = p.Type.TypeCode()
}
} else {
t.OpCode = p.Type.TypeCode()
}
case PrimSequence:
switch {
case p.LooksLikeMap():
t.OpCode = T_MAP
t.Type = PrimBinary
t.Args = []Prim{
p.Args[0].Args[0].BuildType().Prim, // key type
p.Args[0].Args[1].BuildType().Prim, // value type
}
case p.LooksLikeLambda():
t.Type = PrimNullary // we don't know in/out types
t.OpCode = T_LAMBDA
case p.LooksLikeSet():
t.OpCode = T_SET
t.Type = PrimUnary
t.Args = []Prim{
p.Args[0].BuildType().Prim, // single set type
}
default:
// walk the entire list and generate types for each element in-order
t.OpCode = T_LIST
switch len(p.Args) {
case 0:
t.Type = PrimNullary
case 1:
t.Type = PrimUnary
t.Args = []Prim{p.Args[0].BuildType().Prim}
case 2:
t.Type = PrimBinary
t.Args = []Prim{
p.Args[0].BuildType().Prim,
p.Args[1].BuildType().Prim,
}
default:
t.Type = PrimVariadicAnno
t.Args = make([]Prim, len(p.Args))
for i, v := range p.Args {
t.Args[i] = v.BuildType().Prim
}
}
}
case PrimNullary, PrimNullaryAnno:
t.Type = PrimNullary
t.OpCode = p.OpCode.TypeCode()
case PrimUnary, PrimUnaryAnno:
t.OpCode = p.OpCode.TypeCode()
switch t.OpCode {
case T_OPERATION:
t.Type = PrimNullary
case T_OR:
// in data we only see one branch, so we have to guess the other type
t.Type = PrimBinary
inner := p.Args[0].BuildType().Prim
t.Args = []Prim{inner, inner}
case T_OPTION:
// we only know the embedded type on D_SOME
if p.OpCode == D_SOME {
t.Type = PrimUnary
t.Args = []Prim{p.Args[0].BuildType().Prim}
} else {
t.Type = PrimNullary
}
case T_BOOL, T_UNIT:
t.Type = PrimNullary
case T_TICKET:
t.Type = PrimUnary
t.Args = []Prim{p.Args[0].BuildType().Prim}
}
case PrimBinary, PrimBinaryAnno:
if p.OpCode == D_ELT {
t.OpCode = T_MAP
t.Type = PrimBinary
t.Args = []Prim{
p.Args[0].BuildType().Prim,
p.Args[1].BuildType().Prim,
}
} else {
// probably a regular pair
t.Type = PrimBinary
t.OpCode = p.OpCode.TypeCode()
t.Args = []Prim{
p.Args[0].BuildType().Prim,
p.Args[1].BuildType().Prim,
}
}
case PrimVariadicAnno:
// ? probably an operation
t.Type = PrimNullary
t.OpCode = p.OpCode.TypeCode()
}
return Type{t}
}