Skip to content

Commit

Permalink
decouple Value.StaticType from interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Nov 26, 2024
1 parent d555d17 commit 5be8fd1
Show file tree
Hide file tree
Showing 47 changed files with 161 additions and 106 deletions.
55 changes: 55 additions & 0 deletions interpreter/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Flow Foundation
*
* 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 (
"github.com/onflow/cadence/common"
"github.com/onflow/cadence/sema"
)

type TypeConverter interface {
MustConvertStaticToSemaType(staticType StaticType) sema.Type
}

var _ TypeConverter = &Interpreter{}

type SubTypeChecker interface {
IsSubTypeOfSemaType(staticSubType StaticType, superType sema.Type) bool
}

var _ SubTypeChecker = &Interpreter{}

type StorageReader interface {
ReadStored(
storageAddress common.Address,
domain common.StorageDomain,
identifier StorageMapKey,
) Value
}

var _ StorageReader = &Interpreter{}

type StaticTypeGetter interface {
common.MemoryGauge
StorageReader
TypeConverter
SubTypeChecker
}

var _ StaticTypeGetter = &Interpreter{}
2 changes: 1 addition & 1 deletion interpreter/simplecompositevalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (v *SimpleCompositeValue) Walk(_ *Interpreter, walkChild func(Value), _ Loc
})
}

func (v *SimpleCompositeValue) StaticType(_ *Interpreter) StaticType {
func (v *SimpleCompositeValue) StaticType(_ StaticTypeGetter) StaticType {
return v.staticType
}

Expand Down
2 changes: 1 addition & 1 deletion interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type Value interface {
isValue()
Accept(interpreter *Interpreter, visitor Visitor, locationRange LocationRange)
Walk(interpreter *Interpreter, walkChild func(Value), locationRange LocationRange)
StaticType(interpreter *Interpreter) StaticType
StaticType(staticTypeGetter StaticTypeGetter) StaticType
// ConformsToStaticType returns true if the value (i.e. its dynamic type)
// conforms to its own static type.
// Non-container values trivially always conform to their own static type.
Expand Down
2 changes: 1 addition & 1 deletion interpreter/value_accountcapabilitycontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (v *AccountCapabilityControllerValue) Walk(_ *Interpreter, walkChild func(V
walkChild(v.CapabilityID)
}

func (v *AccountCapabilityControllerValue) StaticType(_ *Interpreter) StaticType {
func (v *AccountCapabilityControllerValue) StaticType(_ StaticTypeGetter) StaticType {
return PrimitiveStaticTypeAccountCapabilityController
}

Expand Down
4 changes: 2 additions & 2 deletions interpreter/value_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ func (AddressValue) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
// NO-OP
}

func (AddressValue) StaticType(interpreter *Interpreter) StaticType {
return NewPrimitiveStaticType(interpreter, PrimitiveStaticTypeAddress)
func (AddressValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypeAddress)
}

func (AddressValue) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down
6 changes: 3 additions & 3 deletions interpreter/value_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func (v *ArrayValue) Walk(
)
}

func (v *ArrayValue) StaticType(_ *Interpreter) StaticType {
func (v *ArrayValue) StaticType(_ StaticTypeGetter) StaticType {
// TODO meter
return v.Type
}
Expand Down Expand Up @@ -1538,10 +1538,10 @@ func (v *ArrayValue) GetOwner() common.Address {
return common.Address(v.StorageAddress())
}

func (v *ArrayValue) SemaType(interpreter *Interpreter) sema.ArrayType {
func (v *ArrayValue) SemaType(typeConverter TypeConverter) sema.ArrayType {
if v.semaType == nil {
// this function will panic already if this conversion fails
v.semaType, _ = interpreter.MustConvertStaticToSemaType(v.Type).(sema.ArrayType)
v.semaType, _ = typeConverter.MustConvertStaticToSemaType(v.Type).(sema.ArrayType)
}
return v.semaType
}
Expand Down
4 changes: 2 additions & 2 deletions interpreter/value_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (BoolValue) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
// NO-OP
}

func (BoolValue) StaticType(interpreter *Interpreter) StaticType {
return NewPrimitiveStaticType(interpreter, PrimitiveStaticTypeBool)
func (BoolValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypeBool)
}

func (BoolValue) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down
4 changes: 2 additions & 2 deletions interpreter/value_capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ func (v *IDCapabilityValue) Walk(_ *Interpreter, walkChild func(Value), _ Locati
walkChild(v.address)
}

func (v *IDCapabilityValue) StaticType(inter *Interpreter) StaticType {
func (v *IDCapabilityValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewCapabilityStaticType(
inter,
staticTypeGetter,
v.BorrowType,
)
}
Expand Down
4 changes: 2 additions & 2 deletions interpreter/value_character.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (CharacterValue) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
// NO-OP
}

func (CharacterValue) StaticType(interpreter *Interpreter) StaticType {
return NewPrimitiveStaticType(interpreter, PrimitiveStaticTypeCharacter)
func (CharacterValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypeCharacter)
}

func (CharacterValue) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down
4 changes: 2 additions & 2 deletions interpreter/value_composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,12 @@ func (v *CompositeValue) Walk(interpreter *Interpreter, walkChild func(Value), l
}, locationRange)
}

func (v *CompositeValue) StaticType(interpreter *Interpreter) StaticType {
func (v *CompositeValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
if v.staticType == nil {
// NOTE: Instead of using NewCompositeStaticType, which always generates the type ID,
// use the TypeID accessor, which may return an already computed type ID
v.staticType = NewCompositeStaticType(
interpreter,
staticTypeGetter,
v.Location,
v.QualifiedIdentifier,
v.TypeID(),
Expand Down
2 changes: 1 addition & 1 deletion interpreter/value_dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func (v *DictionaryValue) Walk(interpreter *Interpreter, walkChild func(Value),
)
}

func (v *DictionaryValue) StaticType(_ *Interpreter) StaticType {
func (v *DictionaryValue) StaticType(_ StaticTypeGetter) StaticType {
// TODO meter
return v.Type
}
Expand Down
6 changes: 3 additions & 3 deletions interpreter/value_ephemeral_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ func (v *EphemeralReferenceValue) MeteredString(interpreter *Interpreter, seenRe
return v.Value.MeteredString(interpreter, seenReferences, locationRange)
}

func (v *EphemeralReferenceValue) StaticType(inter *Interpreter) StaticType {
func (v *EphemeralReferenceValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewReferenceStaticType(
inter,
staticTypeGetter,
v.Authorization,
v.Value.StaticType(inter),
v.Value.StaticType(staticTypeGetter),
)
}

Expand Down
4 changes: 2 additions & 2 deletions interpreter/value_fix64.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func (Fix64Value) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
// NO-OP
}

func (Fix64Value) StaticType(interpreter *Interpreter) StaticType {
return NewPrimitiveStaticType(interpreter, PrimitiveStaticTypeFix64)
func (Fix64Value) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypeFix64)
}

func (Fix64Value) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down
12 changes: 6 additions & 6 deletions interpreter/value_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func (f *InterpretedFunctionValue) Walk(_ *Interpreter, _ func(Value), _ Locatio
// NO-OP
}

func (f *InterpretedFunctionValue) StaticType(interpreter *Interpreter) StaticType {
return ConvertSemaToStaticType(interpreter, f.Type)
func (f *InterpretedFunctionValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return ConvertSemaToStaticType(staticTypeGetter, f.Type)
}

func (*InterpretedFunctionValue) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down Expand Up @@ -237,8 +237,8 @@ func (f *HostFunctionValue) Walk(_ *Interpreter, _ func(Value), _ LocationRange)
// NO-OP
}

func (f *HostFunctionValue) StaticType(interpreter *Interpreter) StaticType {
return ConvertSemaToStaticType(interpreter, f.Type)
func (f *HostFunctionValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return ConvertSemaToStaticType(staticTypeGetter, f.Type)
}

func (*HostFunctionValue) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down Expand Up @@ -413,8 +413,8 @@ func (f BoundFunctionValue) Walk(_ *Interpreter, _ func(Value), _ LocationRange)
// NO-OP
}

func (f BoundFunctionValue) StaticType(inter *Interpreter) StaticType {
return f.Function.StaticType(inter)
func (f BoundFunctionValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return f.Function.StaticType(staticTypeGetter)
}

func (BoundFunctionValue) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down
4 changes: 2 additions & 2 deletions interpreter/value_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func (IntValue) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
// NO-OP
}

func (IntValue) StaticType(interpreter *Interpreter) StaticType {
return NewPrimitiveStaticType(interpreter, PrimitiveStaticTypeInt)
func (IntValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypeInt)
}

func (IntValue) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down
4 changes: 2 additions & 2 deletions interpreter/value_int128.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func (Int128Value) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
// NO-OP
}

func (Int128Value) StaticType(interpreter *Interpreter) StaticType {
return NewPrimitiveStaticType(interpreter, PrimitiveStaticTypeInt128)
func (Int128Value) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypeInt128)
}

func (Int128Value) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down
4 changes: 2 additions & 2 deletions interpreter/value_int16.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func (Int16Value) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
// NO-OP
}

func (Int16Value) StaticType(interpreter *Interpreter) StaticType {
return NewPrimitiveStaticType(interpreter, PrimitiveStaticTypeInt16)
func (Int16Value) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypeInt16)
}

func (Int16Value) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down
4 changes: 2 additions & 2 deletions interpreter/value_int256.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func (Int256Value) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
// NO-OP
}

func (Int256Value) StaticType(interpreter *Interpreter) StaticType {
return NewPrimitiveStaticType(interpreter, PrimitiveStaticTypeInt256)
func (Int256Value) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypeInt256)
}

func (Int256Value) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down
4 changes: 2 additions & 2 deletions interpreter/value_int32.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func (Int32Value) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
// NO-OP
}

func (Int32Value) StaticType(interpreter *Interpreter) StaticType {
return NewPrimitiveStaticType(interpreter, PrimitiveStaticTypeInt32)
func (Int32Value) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypeInt32)
}

func (Int32Value) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down
4 changes: 2 additions & 2 deletions interpreter/value_int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func (Int64Value) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
// NO-OP
}

func (Int64Value) StaticType(interpreter *Interpreter) StaticType {
return NewPrimitiveStaticType(interpreter, PrimitiveStaticTypeInt64)
func (Int64Value) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypeInt64)
}

func (Int64Value) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down
4 changes: 2 additions & 2 deletions interpreter/value_int8.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func (Int8Value) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
// NO-OP
}

func (Int8Value) StaticType(interpreter *Interpreter) StaticType {
return NewPrimitiveStaticType(interpreter, PrimitiveStaticTypeInt8)
func (Int8Value) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypeInt8)
}

func (Int8Value) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down
10 changes: 5 additions & 5 deletions interpreter/value_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ func (v PathLinkValue) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
panic(errors.NewUnreachableError())
}

func (v PathLinkValue) StaticType(interpreter *Interpreter) StaticType {
func (v PathLinkValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
// When iterating over public/private paths,
// the values at these paths are PathLinkValues,
// placed there by the `link` function.
//
// These are loaded as links, however,
// for the purposes of checking their type,
// we treat them as capabilities
return NewCapabilityStaticType(interpreter, v.Type)
return NewCapabilityStaticType(staticTypeGetter, v.Type)
}

func (PathLinkValue) IsImportable(_ *Interpreter, _ LocationRange) bool {
Expand Down Expand Up @@ -184,7 +184,7 @@ func (AccountLinkValue) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
panic(errors.NewUnreachableError())
}

func (v AccountLinkValue) StaticType(interpreter *Interpreter) StaticType {
func (v AccountLinkValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
// When iterating over public/private paths,
// the values at these paths are AccountLinkValues,
// placed there by the `linkAccount` function.
Expand All @@ -193,9 +193,9 @@ func (v AccountLinkValue) StaticType(interpreter *Interpreter) StaticType {
// for the purposes of checking their type,
// we treat them as capabilities
return NewCapabilityStaticType(
interpreter,
staticTypeGetter,
NewReferenceStaticType(
interpreter,
staticTypeGetter,
FullyEntitledAccountAccess,
PrimitiveStaticTypeAccount,
),
Expand Down
6 changes: 3 additions & 3 deletions interpreter/value_nil.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ func (NilValue) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
// NO-OP
}

func (NilValue) StaticType(interpreter *Interpreter) StaticType {
func (NilValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewOptionalStaticType(
interpreter,
NewPrimitiveStaticType(interpreter, PrimitiveStaticTypeNever),
staticTypeGetter,
NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypeNever),
)
}

Expand Down
8 changes: 4 additions & 4 deletions interpreter/value_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ func (PathValue) Walk(_ *Interpreter, _ func(Value), _ LocationRange) {
// NO-OP
}

func (v PathValue) StaticType(interpreter *Interpreter) StaticType {
func (v PathValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
switch v.Domain {
case common.PathDomainStorage:
return NewPrimitiveStaticType(interpreter, PrimitiveStaticTypeStoragePath)
return NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypeStoragePath)
case common.PathDomainPublic:
return NewPrimitiveStaticType(interpreter, PrimitiveStaticTypePublicPath)
return NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypePublicPath)
case common.PathDomainPrivate:
return NewPrimitiveStaticType(interpreter, PrimitiveStaticTypePrivatePath)
return NewPrimitiveStaticType(staticTypeGetter, PrimitiveStaticTypePrivatePath)
default:
panic(errors.NewUnreachableError())
}
Expand Down
4 changes: 2 additions & 2 deletions interpreter/value_pathcapability.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ func (v *PathCapabilityValue) Walk(_ *Interpreter, walkChild func(Value), _ Loca
walkChild(v.Path)
}

func (v *PathCapabilityValue) StaticType(inter *Interpreter) StaticType {
func (v *PathCapabilityValue) StaticType(staticTypeGetter StaticTypeGetter) StaticType {
return NewCapabilityStaticType(
inter,
staticTypeGetter,
v.BorrowType,
)
}
Expand Down
Loading

0 comments on commit 5be8fd1

Please sign in to comment.