Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Feb 9, 2021
1 parent 6803a80 commit f287790
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 93 deletions.
13 changes: 11 additions & 2 deletions runtime/convertTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/errors"
"github.com/onflow/cadence/runtime/sema"
)

Expand Down Expand Up @@ -193,7 +194,11 @@ func exportCompositeType(t *sema.CompositeType, results map[sema.TypeID]cadence.
for _, identifier := range t.Fields {
member, ok := t.Members.Get(identifier)

if !ok || member.IgnoreInSerialization {
if !ok {
panic(errors.NewUnreachableError())
}

if member.IgnoreInSerialization {
continue
}

Expand Down Expand Up @@ -258,7 +263,11 @@ func exportInterfaceType(t *sema.InterfaceType, results map[sema.TypeID]cadence.
for _, identifier := range t.Fields {
member, ok := t.Members.Get(identifier)

if !ok || member.IgnoreInSerialization {
if !ok {
panic(errors.NewUnreachableError())
}

if member.IgnoreInSerialization {
continue
}

Expand Down
135 changes: 74 additions & 61 deletions runtime/sema/check_composite_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (checker *Checker) visitCompositeDeclaration(declaration *ast.CompositeDecl
if !ok {
continue
}

fieldMembers[member] = field
}

Expand Down Expand Up @@ -541,16 +542,18 @@ func (checker *Checker) declareCompositeMembersAndValue(
nestedCompositeDeclarationVariable :=
checker.valueActivations.Find(identifier.Identifier)

declarationMembers.Set(nestedCompositeDeclarationVariable.Identifier, &Member{
Identifier: identifier,
Access: nestedCompositeDeclaration.Access,
ContainerType: compositeType,
TypeAnnotation: NewTypeAnnotation(nestedCompositeDeclarationVariable.Type),
DeclarationKind: nestedCompositeDeclarationVariable.DeclarationKind,
VariableKind: ast.VariableKindConstant,
IgnoreInSerialization: true,
DocString: nestedCompositeDeclaration.DocString,
})
declarationMembers.Set(
nestedCompositeDeclarationVariable.Identifier,
&Member{
Identifier: identifier,
Access: nestedCompositeDeclaration.Access,
ContainerType: compositeType,
TypeAnnotation: NewTypeAnnotation(nestedCompositeDeclarationVariable.Type),
DeclarationKind: nestedCompositeDeclarationVariable.DeclarationKind,
VariableKind: ast.VariableKindConstant,
IgnoreInSerialization: true,
DocString: nestedCompositeDeclaration.DocString,
})
}

// Declare implicit type requirement conformances, if any,
Expand Down Expand Up @@ -760,16 +763,18 @@ func (checker *Checker) declareEnumConstructor(
if _, ok := constructorMembers.Get(caseName); ok {
continue
}
constructorMembers.Set(caseName, &Member{
ContainerType: constructorType,
// enum cases are always public
Access: ast.AccessPublic,
Identifier: enumCase.Identifier,
TypeAnnotation: memberCaseTypeAnnotation,
DeclarationKind: common.DeclarationKindField,
VariableKind: ast.VariableKindConstant,
DocString: enumCase.DocString,
})
constructorMembers.Set(
caseName,
&Member{
ContainerType: constructorType,
// enum cases are always public
Access: ast.AccessPublic,
Identifier: enumCase.Identifier,
TypeAnnotation: memberCaseTypeAnnotation,
DeclarationKind: common.DeclarationKindField,
VariableKind: ast.VariableKindConstant,
DocString: enumCase.DocString,
})

if checker.originsAndOccurrencesEnabled && constructorOrigins != nil {
constructorOrigins[caseName] =
Expand Down Expand Up @@ -1398,15 +1403,17 @@ func (checker *Checker) defaultMembersAndOrigins(
)
}

members.Set(identifier, &Member{
ContainerType: containerType,
Access: field.Access,
Identifier: field.Identifier,
DeclarationKind: declarationKind,
TypeAnnotation: fieldTypeAnnotation,
VariableKind: field.VariableKind,
DocString: field.DocString,
})
members.Set(
identifier,
&Member{
ContainerType: containerType,
Access: field.Access,
Identifier: field.Identifier,
DeclarationKind: declarationKind,
TypeAnnotation: fieldTypeAnnotation,
VariableKind: field.VariableKind,
DocString: field.DocString,
})

if checker.originsAndOccurrencesEnabled && origins != nil {
origins[identifier] =
Expand Down Expand Up @@ -1461,16 +1468,18 @@ func (checker *Checker) defaultMembersAndOrigins(
)
}

members.Set(identifier, &Member{
ContainerType: containerType,
Access: function.Access,
Identifier: function.Identifier,
DeclarationKind: declarationKind,
TypeAnnotation: fieldTypeAnnotation,
VariableKind: ast.VariableKindConstant,
ArgumentLabels: argumentLabels,
DocString: function.DocString,
})
members.Set(
identifier,
&Member{
ContainerType: containerType,
Access: function.Access,
Identifier: function.Identifier,
DeclarationKind: declarationKind,
TypeAnnotation: fieldTypeAnnotation,
VariableKind: ast.VariableKindConstant,
ArgumentLabels: argumentLabels,
DocString: function.DocString,
})

if checker.originsAndOccurrencesEnabled && origins != nil {
origins[identifier] =
Expand Down Expand Up @@ -1503,14 +1512,16 @@ func (checker *Checker) eventMembersAndOrigins(

fieldNames = append(fieldNames, identifier.Identifier)

members.Set(identifier.Identifier, &Member{
ContainerType: containerType,
Access: ast.AccessPublic,
Identifier: identifier,
DeclarationKind: common.DeclarationKindField,
TypeAnnotation: typeAnnotation,
VariableKind: ast.VariableKindConstant,
})
members.Set(
identifier.Identifier,
&Member{
ContainerType: containerType,
Access: ast.AccessPublic,
Identifier: identifier,
DeclarationKind: common.DeclarationKindField,
TypeAnnotation: typeAnnotation,
VariableKind: ast.VariableKindConstant,
})

if checker.originsAndOccurrencesEnabled && origins != nil {
origins[identifier.Identifier] =
Expand Down Expand Up @@ -1574,17 +1585,19 @@ func (checker *Checker) enumMembersAndOrigins(
// so only has a single member, the raw value field

members = NewStringMemberOrderedMap()
members.Set(EnumRawValueFieldName, &Member{
ContainerType: containerType,
Access: ast.AccessPublic,
Identifier: ast.Identifier{
Identifier: EnumRawValueFieldName,
},
DeclarationKind: common.DeclarationKindField,
TypeAnnotation: NewTypeAnnotation(containerType.EnumRawType),
VariableKind: ast.VariableKindConstant,
DocString: enumRawValueFieldDocString,
})
members.Set(
EnumRawValueFieldName,
&Member{
ContainerType: containerType,
Access: ast.AccessPublic,
Identifier: ast.Identifier{
Identifier: EnumRawValueFieldName,
},
DeclarationKind: common.DeclarationKindField,
TypeAnnotation: NewTypeAnnotation(containerType.EnumRawType),
VariableKind: ast.VariableKindConstant,
DocString: enumRawValueFieldDocString,
})

// No origins available for the only member which was declared above

Expand Down Expand Up @@ -1954,9 +1967,9 @@ func (checker *Checker) checkNoDestructorNoResourceFields(
return
}

for p := members.Oldest(); p != nil; p = p.Next() {
member := p.Value
memberName := p.Key
for pair := members.Oldest(); pair != nil; pair = pair.Next() {
member := pair.Value
memberName := pair.Key

// NOTE: check type, not resource annotation:
// the field could have a wrong annotation
Expand Down
4 changes: 2 additions & 2 deletions runtime/sema/check_event_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func IsValidEventParameterType(t Type) bool {
if t.Kind != common.CompositeKindStructure {
return false
}
for p := t.Members.Oldest(); p != nil; p = p.Next() {
member := p.Value
for pair := t.Members.Oldest(); pair != nil; pair = pair.Next() {
member := pair.Value
if member.DeclarationKind == common.DeclarationKindField &&
!member.IgnoreInSerialization &&
!IsValidEventParameterType(member.TypeAnnotation.Type) {
Expand Down
7 changes: 6 additions & 1 deletion runtime/sema/check_transaction_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ func (checker *Checker) VisitTransactionDeclaration(declaration *ast.Transaction

for _, field := range declaration.Fields {
fieldName := field.Identifier.Identifier
member, _ := transactionType.Members.Get(fieldName)
member, ok := transactionType.Members.Get(fieldName)

if !ok {
panic(errors.NewUnreachableError())
}

fieldMembers[member] = field
}

Expand Down
3 changes: 2 additions & 1 deletion runtime/sema/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,9 @@ func (checker *Checker) convertRestrictedType(t *ast.RestrictedType) Type {

prevMemberType, ok := previousDeclaringInterfaceType.Members.Get(name)
if !ok {
return
panic(errors.NewUnreachableError())
}

previousMemberType := prevMemberType.TypeAnnotation.Type

if !memberType.IsInvalidType() &&
Expand Down
12 changes: 6 additions & 6 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4881,8 +4881,8 @@ func (t *CompositeType) IsStorable(results map[*Member]bool) bool {
// If this composite type has a member which is non-storable,
// then the composite type is not storable.

for p := t.Members.Oldest(); p != nil; p = p.Next() {
if !p.Value.IsStorable(results) {
for pair := t.Members.Oldest(); pair != nil; pair = pair.Next() {
if !pair.Value.IsStorable(results) {
return false
}
}
Expand Down Expand Up @@ -5895,8 +5895,8 @@ func (t *InterfaceType) IsStorable(results map[*Member]bool) bool {
// If this interface type has a member which is non-storable,
// then the interface type is not storable.

for p := t.Members.Oldest(); p != nil; p = p.Next() {
if !p.Value.IsStorable(results) {
for pair := t.Members.Oldest(); pair != nil; pair = pair.Next() {
if !pair.Value.IsStorable(results) {
return false
}
}
Expand All @@ -5913,8 +5913,8 @@ func (t *InterfaceType) IsExternallyReturnable(results map[*Member]bool) bool {
// If this interface type has a member which is not externally returnable,
// then the interface type is not externally returnable.

for p := t.Members.Oldest(); p != nil; p = p.Next() {
if !p.Value.IsExternallyReturnable(results) {
for pair := t.Members.Oldest(); pair != nil; pair = pair.Next() {
if !pair.Value.IsExternallyReturnable(results) {
return false
}
}
Expand Down
14 changes: 8 additions & 6 deletions runtime/stdlib/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,15 @@ func newFlowEventType(identifier string, parameters ...*sema.Parameter) *sema.Co
parameter.Identifier,
)

eventType.Members.Set(parameter.Identifier, sema.NewPublicConstantFieldMember(
eventType,
eventType.Members.Set(
parameter.Identifier,
parameter.TypeAnnotation.Type,
// TODO: add docstring support for parameters
"",
))
sema.NewPublicConstantFieldMember(
eventType,
parameter.Identifier,
parameter.TypeAnnotation.Type,
// TODO: add docstring support for parameters
"",
))

eventType.ConstructorParameters = append(
eventType.ConstructorParameters,
Expand Down
16 changes: 9 additions & 7 deletions runtime/tests/checker/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,14 +703,16 @@ func TestCheckImportVirtual(t *testing.T) {
fooType.Fields = []string{"bar"}

fooType.Members = sema.NewStringMemberOrderedMap()
fooType.Members.Set("bar", sema.NewPublicFunctionMember(
fooType,
fooType.Members.Set(
"bar",
&sema.FunctionType{
ReturnTypeAnnotation: sema.NewTypeAnnotation(&sema.UInt64Type{}),
},
"",
))
sema.NewPublicFunctionMember(
fooType,
"bar",
&sema.FunctionType{
ReturnTypeAnnotation: sema.NewTypeAnnotation(&sema.UInt64Type{}),
},
"",
))

_, err := ParseAndCheckWithOptions(t,
code,
Expand Down
16 changes: 9 additions & 7 deletions runtime/tests/interpreter/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ func TestInterpretVirtualImport(t *testing.T) {
}

fooType.Members = sema.NewStringMemberOrderedMap()
fooType.Members.Set("bar", sema.NewPublicFunctionMember(
fooType,
fooType.Members.Set(
"bar",
&sema.FunctionType{
ReturnTypeAnnotation: sema.NewTypeAnnotation(&sema.UInt64Type{}),
},
"",
))
sema.NewPublicFunctionMember(
fooType,
"bar",
&sema.FunctionType{
ReturnTypeAnnotation: sema.NewTypeAnnotation(&sema.UInt64Type{}),
},
"",
))

const code = `
import Foo
Expand Down

0 comments on commit f287790

Please sign in to comment.