-
Notifications
You must be signed in to change notification settings - Fork 131
/
primitivestatictype.go
629 lines (554 loc) Β· 16.1 KB
/
primitivestatictype.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package interpreter
import (
"fmt"
"strconv"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/errors"
"github.com/onflow/cadence/runtime/sema"
)
//go:generate go run golang.org/x/tools/cmd/stringer -type=PrimitiveStaticType -trimprefix=PrimitiveStaticType
// PrimitiveStaticType
var PrimitiveStaticTypes = _PrimitiveStaticType_map
type PrimitiveStaticType uint
var _ StaticType = PrimitiveStaticType(0)
const primitiveStaticTypePrefix = "PrimitiveStaticType"
var primitiveStaticTypeConstantLength = len(primitiveStaticTypePrefix) + 2 // + 2 for parentheses
func NewPrimitiveStaticType(
memoryGauge common.MemoryGauge,
staticType PrimitiveStaticType,
) PrimitiveStaticType {
common.UseMemory(memoryGauge, common.PrimitiveStaticTypeMemoryUsage)
return staticType
}
// !!! *WARNING* !!!
//
// Only add new types by:
// - replacing existing placeholders (`_`) with new types
// - appending new types
//
// Only remove types by:
// - replace existing types with a placeholder `_`
//
// DO *NOT* REPLACE EXISTING TYPES!
// DO *NOT* ADD NEW TYPES IN BETWEEN!
const (
PrimitiveStaticTypeUnknown PrimitiveStaticType = iota
PrimitiveStaticTypeVoid
PrimitiveStaticTypeAny
PrimitiveStaticTypeNever
PrimitiveStaticTypeAnyStruct
PrimitiveStaticTypeAnyResource
PrimitiveStaticTypeBool
PrimitiveStaticTypeAddress
PrimitiveStaticTypeString
PrimitiveStaticTypeCharacter
PrimitiveStaticTypeMetaType
PrimitiveStaticTypeBlock
_
_
_
_
_
_
// Number
PrimitiveStaticTypeNumber
PrimitiveStaticTypeSignedNumber
_
_
_
_
// Integer
PrimitiveStaticTypeInteger
PrimitiveStaticTypeSignedInteger
_
_
_
_
// FixedPoint
PrimitiveStaticTypeFixedPoint
PrimitiveStaticTypeSignedFixedPoint
_
_
_
_
// Int*
PrimitiveStaticTypeInt
PrimitiveStaticTypeInt8
PrimitiveStaticTypeInt16
PrimitiveStaticTypeInt32
PrimitiveStaticTypeInt64
PrimitiveStaticTypeInt128
PrimitiveStaticTypeInt256
_
// UInt*
PrimitiveStaticTypeUInt
PrimitiveStaticTypeUInt8
PrimitiveStaticTypeUInt16
PrimitiveStaticTypeUInt32
PrimitiveStaticTypeUInt64
PrimitiveStaticTypeUInt128
PrimitiveStaticTypeUInt256
_
// Word*
_
PrimitiveStaticTypeWord8
PrimitiveStaticTypeWord16
PrimitiveStaticTypeWord32
PrimitiveStaticTypeWord64
PrimitiveStaticTypeWord128
PrimitiveStaticTypeWord256
_
// Fix*
_
_ // future: Fix8
_ // future: Fix16
_ // future: Fix32
PrimitiveStaticTypeFix64
_ // future: Fix128
_ // future: Fix256
_
// UFix*
_
_ // future: UFix8
_ // future: UFix16
_ // future: UFix32
PrimitiveStaticTypeUFix64
_ // future: UFix128
_ // future: UFix256
_
// Storage
PrimitiveStaticTypePath
PrimitiveStaticTypeCapability
PrimitiveStaticTypeStoragePath
PrimitiveStaticTypeCapabilityPath
PrimitiveStaticTypePublicPath
PrimitiveStaticTypePrivatePath
_
_
_
_
_
_
_
_
PrimitiveStaticTypeAuthAccount
PrimitiveStaticTypePublicAccount
PrimitiveStaticTypeDeployedContract
PrimitiveStaticTypeAuthAccountContracts
PrimitiveStaticTypePublicAccountContracts
PrimitiveStaticTypeAuthAccountKeys
PrimitiveStaticTypePublicAccountKeys
PrimitiveStaticTypeAccountKey
PrimitiveStaticTypeAuthAccountInbox
PrimitiveStaticTypeStorageCapabilityController
PrimitiveStaticTypeAccountCapabilityController
PrimitiveStaticTypeAuthAccountStorageCapabilities
PrimitiveStaticTypeAuthAccountAccountCapabilities
PrimitiveStaticTypeAuthAccountCapabilities
PrimitiveStaticTypePublicAccountCapabilities
// !!! *WARNING* !!!
// ADD NEW TYPES *BEFORE* THIS WARNING.
// DO *NOT* ADD NEW TYPES AFTER THIS LINE!
PrimitiveStaticType_Count
)
func (PrimitiveStaticType) isStaticType() {}
func (t PrimitiveStaticType) elementSize() uint {
switch t {
case
PrimitiveStaticTypeAnyStruct,
PrimitiveStaticTypeAnyResource,
PrimitiveStaticTypeAny:
return UnknownElementSize
case PrimitiveStaticTypeVoid:
return uint(len(cborVoidValue))
case PrimitiveStaticTypeNever:
return cborTagSize + 1
case PrimitiveStaticTypeBool:
return cborTagSize + 1
case PrimitiveStaticTypeAddress:
return cborTagSize + 8 // address length is 8 bytes
case PrimitiveStaticTypeString,
PrimitiveStaticTypeCharacter,
PrimitiveStaticTypeMetaType,
PrimitiveStaticTypeBlock:
return UnknownElementSize
case PrimitiveStaticTypeFixedPoint,
PrimitiveStaticTypeSignedFixedPoint:
return cborTagSize + 8
// values of these types may wrap big.Int
case PrimitiveStaticTypeInt,
PrimitiveStaticTypeUInt,
PrimitiveStaticTypeUInt128,
PrimitiveStaticTypeUInt256,
PrimitiveStaticTypeInt128,
PrimitiveStaticTypeInt256,
PrimitiveStaticTypeWord128,
PrimitiveStaticTypeWord256,
PrimitiveStaticTypeInteger,
PrimitiveStaticTypeSignedInteger,
PrimitiveStaticTypeNumber,
PrimitiveStaticTypeSignedNumber:
return UnknownElementSize
case PrimitiveStaticTypeInt8,
PrimitiveStaticTypeUInt8,
PrimitiveStaticTypeWord8:
return cborTagSize + 2
case PrimitiveStaticTypeInt16,
PrimitiveStaticTypeUInt16,
PrimitiveStaticTypeWord16:
return cborTagSize + 3
case PrimitiveStaticTypeInt32,
PrimitiveStaticTypeUInt32,
PrimitiveStaticTypeWord32:
return cborTagSize + 5
case PrimitiveStaticTypeInt64,
PrimitiveStaticTypeUInt64,
PrimitiveStaticTypeWord64,
PrimitiveStaticTypeFix64,
PrimitiveStaticTypeUFix64:
return cborTagSize + 9
case PrimitiveStaticTypePath,
PrimitiveStaticTypeCapability,
PrimitiveStaticTypeStoragePath,
PrimitiveStaticTypeCapabilityPath,
PrimitiveStaticTypePublicPath,
PrimitiveStaticTypePrivatePath,
PrimitiveStaticTypeAuthAccount,
PrimitiveStaticTypePublicAccount,
PrimitiveStaticTypeDeployedContract,
PrimitiveStaticTypeAuthAccountContracts,
PrimitiveStaticTypePublicAccountContracts,
PrimitiveStaticTypeAuthAccountInbox,
PrimitiveStaticTypeAuthAccountKeys,
PrimitiveStaticTypePublicAccountKeys,
PrimitiveStaticTypeAccountKey,
PrimitiveStaticTypeStorageCapabilityController,
PrimitiveStaticTypeAccountCapabilityController,
PrimitiveStaticTypeAuthAccountStorageCapabilities,
PrimitiveStaticTypeAuthAccountAccountCapabilities,
PrimitiveStaticTypeAuthAccountCapabilities,
PrimitiveStaticTypePublicAccountCapabilities:
return UnknownElementSize
}
return UnknownElementSize
}
func (t PrimitiveStaticType) Equal(other StaticType) bool {
otherPrimitiveType, ok := other.(PrimitiveStaticType)
if !ok {
return false
}
return t == otherPrimitiveType
}
func (t PrimitiveStaticType) MeteredString(memoryGauge common.MemoryGauge) string {
if str, ok := PrimitiveStaticTypes[t]; ok {
common.UseMemory(memoryGauge, common.NewRawStringMemoryUsage(len(str)))
return str
}
memoryAmount := primitiveStaticTypeConstantLength + OverEstimateIntStringLength(int(t))
common.UseMemory(memoryGauge, common.NewRawStringMemoryUsage(memoryAmount))
rawValueStr := strconv.FormatInt(int64(t), 10)
return fmt.Sprintf("%s(%s)", primitiveStaticTypePrefix, rawValueStr)
}
func (t PrimitiveStaticType) ID() TypeID {
return t.SemaType().ID()
}
func (t PrimitiveStaticType) SemaType() sema.Type {
switch t {
case PrimitiveStaticTypeVoid:
return sema.VoidType
case PrimitiveStaticTypeAny:
return sema.AnyType
case PrimitiveStaticTypeNever:
return sema.NeverType
case PrimitiveStaticTypeAnyStruct:
return sema.AnyStructType
case PrimitiveStaticTypeAnyResource:
return sema.AnyResourceType
case PrimitiveStaticTypeBool:
return sema.BoolType
case PrimitiveStaticTypeAddress:
return sema.TheAddressType
case PrimitiveStaticTypeString:
return sema.StringType
case PrimitiveStaticTypeCharacter:
return sema.CharacterType
case PrimitiveStaticTypeMetaType:
return sema.MetaType
case PrimitiveStaticTypeBlock:
return sema.BlockType
// Number
case PrimitiveStaticTypeNumber:
return sema.NumberType
case PrimitiveStaticTypeSignedNumber:
return sema.SignedNumberType
// Integer
case PrimitiveStaticTypeInteger:
return sema.IntegerType
case PrimitiveStaticTypeSignedInteger:
return sema.SignedIntegerType
// FixedPoint
case PrimitiveStaticTypeFixedPoint:
return sema.FixedPointType
case PrimitiveStaticTypeSignedFixedPoint:
return sema.SignedFixedPointType
// Int*
case PrimitiveStaticTypeInt:
return sema.IntType
case PrimitiveStaticTypeInt8:
return sema.Int8Type
case PrimitiveStaticTypeInt16:
return sema.Int16Type
case PrimitiveStaticTypeInt32:
return sema.Int32Type
case PrimitiveStaticTypeInt64:
return sema.Int64Type
case PrimitiveStaticTypeInt128:
return sema.Int128Type
case PrimitiveStaticTypeInt256:
return sema.Int256Type
// UInt*
case PrimitiveStaticTypeUInt:
return sema.UIntType
case PrimitiveStaticTypeUInt8:
return sema.UInt8Type
case PrimitiveStaticTypeUInt16:
return sema.UInt16Type
case PrimitiveStaticTypeUInt32:
return sema.UInt32Type
case PrimitiveStaticTypeUInt64:
return sema.UInt64Type
case PrimitiveStaticTypeUInt128:
return sema.UInt128Type
case PrimitiveStaticTypeUInt256:
return sema.UInt256Type
// Word *
case PrimitiveStaticTypeWord8:
return sema.Word8Type
case PrimitiveStaticTypeWord16:
return sema.Word16Type
case PrimitiveStaticTypeWord32:
return sema.Word32Type
case PrimitiveStaticTypeWord64:
return sema.Word64Type
case PrimitiveStaticTypeWord128:
return sema.Word128Type
case PrimitiveStaticTypeWord256:
return sema.Word256Type
// Fix*
case PrimitiveStaticTypeFix64:
return sema.Fix64Type
// UFix*
case PrimitiveStaticTypeUFix64:
return sema.UFix64Type
// Storage
case PrimitiveStaticTypePath:
return sema.PathType
case PrimitiveStaticTypeStoragePath:
return sema.StoragePathType
case PrimitiveStaticTypeCapabilityPath:
return sema.CapabilityPathType
case PrimitiveStaticTypePublicPath:
return sema.PublicPathType
case PrimitiveStaticTypePrivatePath:
return sema.PrivatePathType
case PrimitiveStaticTypeCapability:
return &sema.CapabilityType{}
case PrimitiveStaticTypeAuthAccount:
return sema.AuthAccountType
case PrimitiveStaticTypePublicAccount:
return sema.PublicAccountType
case PrimitiveStaticTypeDeployedContract:
return sema.DeployedContractType
case PrimitiveStaticTypeAuthAccountContracts:
return sema.AuthAccountContractsType
case PrimitiveStaticTypePublicAccountContracts:
return sema.PublicAccountContractsType
case PrimitiveStaticTypeAuthAccountKeys:
return sema.AuthAccountKeysType
case PrimitiveStaticTypePublicAccountKeys:
return sema.PublicAccountKeysType
case PrimitiveStaticTypeAccountKey:
return sema.AccountKeyType
case PrimitiveStaticTypeAuthAccountInbox:
return sema.AuthAccountInboxType
case PrimitiveStaticTypeStorageCapabilityController:
return sema.StorageCapabilityControllerType
case PrimitiveStaticTypeAccountCapabilityController:
return sema.AccountCapabilityControllerType
case PrimitiveStaticTypeAuthAccountStorageCapabilities:
return sema.AuthAccountStorageCapabilitiesType
case PrimitiveStaticTypeAuthAccountAccountCapabilities:
return sema.AuthAccountAccountCapabilitiesType
case PrimitiveStaticTypeAuthAccountCapabilities:
return sema.AuthAccountCapabilitiesType
case PrimitiveStaticTypePublicAccountCapabilities:
return sema.PublicAccountCapabilitiesType
default:
panic(errors.NewUnexpectedError("missing case for %s", t))
}
}
// ConvertSemaToPrimitiveStaticType converts a `sema.Type` to a `PrimitiveStaticType`.
//
// Returns `PrimitiveStaticTypeUnknown` if the given type is not a primitive type.
func ConvertSemaToPrimitiveStaticType(
memoryGauge common.MemoryGauge,
t sema.Type,
) (typ PrimitiveStaticType) {
switch t {
case sema.StringType:
typ = PrimitiveStaticTypeString
// Number
case sema.NumberType:
typ = PrimitiveStaticTypeNumber
case sema.SignedNumberType:
typ = PrimitiveStaticTypeSignedNumber
// Integer
case sema.IntegerType:
typ = PrimitiveStaticTypeInteger
case sema.SignedIntegerType:
typ = PrimitiveStaticTypeSignedInteger
// FixedPoint
case sema.FixedPointType:
typ = PrimitiveStaticTypeFixedPoint
case sema.SignedFixedPointType:
typ = PrimitiveStaticTypeSignedFixedPoint
// Int*
case sema.IntType:
typ = PrimitiveStaticTypeInt
case sema.Int8Type:
typ = PrimitiveStaticTypeInt8
case sema.Int16Type:
typ = PrimitiveStaticTypeInt16
case sema.Int32Type:
typ = PrimitiveStaticTypeInt32
case sema.Int64Type:
typ = PrimitiveStaticTypeInt64
case sema.Int128Type:
typ = PrimitiveStaticTypeInt128
case sema.Int256Type:
typ = PrimitiveStaticTypeInt256
// UInt*
case sema.UIntType:
typ = PrimitiveStaticTypeUInt
case sema.UInt8Type:
typ = PrimitiveStaticTypeUInt8
case sema.UInt16Type:
typ = PrimitiveStaticTypeUInt16
case sema.UInt32Type:
typ = PrimitiveStaticTypeUInt32
case sema.UInt64Type:
typ = PrimitiveStaticTypeUInt64
case sema.UInt128Type:
typ = PrimitiveStaticTypeUInt128
case sema.UInt256Type:
typ = PrimitiveStaticTypeUInt256
// Word*
case sema.Word8Type:
typ = PrimitiveStaticTypeWord8
case sema.Word16Type:
typ = PrimitiveStaticTypeWord16
case sema.Word32Type:
typ = PrimitiveStaticTypeWord32
case sema.Word64Type:
typ = PrimitiveStaticTypeWord64
case sema.Word128Type:
typ = PrimitiveStaticTypeWord128
case sema.Word256Type:
typ = PrimitiveStaticTypeWord256
// Fix*
case sema.Fix64Type:
typ = PrimitiveStaticTypeFix64
// UFix*
case sema.UFix64Type:
typ = PrimitiveStaticTypeUFix64
case sema.PathType:
typ = PrimitiveStaticTypePath
case sema.StoragePathType:
typ = PrimitiveStaticTypeStoragePath
case sema.CapabilityPathType:
typ = PrimitiveStaticTypeCapabilityPath
case sema.PublicPathType:
typ = PrimitiveStaticTypePublicPath
case sema.PrivatePathType:
typ = PrimitiveStaticTypePrivatePath
case sema.NeverType:
typ = PrimitiveStaticTypeNever
case sema.VoidType:
typ = PrimitiveStaticTypeVoid
case sema.MetaType:
typ = PrimitiveStaticTypeMetaType
case sema.BoolType:
typ = PrimitiveStaticTypeBool
case sema.CharacterType:
typ = PrimitiveStaticTypeCharacter
case sema.AnyType:
typ = PrimitiveStaticTypeAny
case sema.AnyStructType:
typ = PrimitiveStaticTypeAnyStruct
case sema.AnyResourceType:
typ = PrimitiveStaticTypeAnyResource
case sema.AuthAccountType:
typ = PrimitiveStaticTypeAuthAccount
case sema.PublicAccountType:
typ = PrimitiveStaticTypePublicAccount
case sema.BlockType:
typ = PrimitiveStaticTypeBlock
case sema.DeployedContractType:
typ = PrimitiveStaticTypeDeployedContract
case sema.AuthAccountContractsType:
typ = PrimitiveStaticTypeAuthAccountContracts
case sema.PublicAccountContractsType:
typ = PrimitiveStaticTypePublicAccountContracts
case sema.AuthAccountKeysType:
typ = PrimitiveStaticTypeAuthAccountKeys
case sema.PublicAccountKeysType:
typ = PrimitiveStaticTypePublicAccountKeys
case sema.AccountKeyType:
typ = PrimitiveStaticTypeAccountKey
case sema.AuthAccountInboxType:
typ = PrimitiveStaticTypeAuthAccountInbox
case sema.StorageCapabilityControllerType:
typ = PrimitiveStaticTypeStorageCapabilityController
case sema.AccountCapabilityControllerType:
typ = PrimitiveStaticTypeAccountCapabilityController
case sema.AuthAccountStorageCapabilitiesType:
typ = PrimitiveStaticTypeAuthAccountStorageCapabilities
case sema.AuthAccountAccountCapabilitiesType:
typ = PrimitiveStaticTypeAuthAccountAccountCapabilities
case sema.AuthAccountCapabilitiesType:
typ = PrimitiveStaticTypeAuthAccountCapabilities
case sema.PublicAccountCapabilitiesType:
typ = PrimitiveStaticTypePublicAccountCapabilities
}
switch t := t.(type) {
case *sema.AddressType:
typ = PrimitiveStaticTypeAddress
// Storage
case *sema.CapabilityType:
// Only convert unparameterized Capability type
if t.BorrowType == nil {
typ = PrimitiveStaticTypeCapability
}
}
if typ == PrimitiveStaticTypeUnknown {
return
}
return NewPrimitiveStaticType(memoryGauge, typ) // default is 0 aka PrimitiveStaticTypeUnknown
}