Skip to content

Commit

Permalink
Merge pull request #2805 from onflow/bastian/constructor-initializer-…
Browse files Browse the repository at this point in the history
…function-type-helpers
  • Loading branch information
turbolent authored Sep 22, 2023
2 parents f5846e6 + a180af8 commit b1d23de
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
13 changes: 4 additions & 9 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,20 +1092,13 @@ func (interpreter *Interpreter) declareNonEnumCompositeValue(

compositeType := interpreter.Program.Elaboration.CompositeDeclarationType(declaration)

constructorType := &sema.FunctionType{
IsConstructor: true,
Purity: compositeType.ConstructorPurity,
Parameters: compositeType.ConstructorParameters,
ReturnTypeAnnotation: sema.TypeAnnotation{
Type: compositeType,
},
}
initializerType := compositeType.InitializerFunctionType()

var initializerFunction FunctionValue
if declaration.Kind() == common.CompositeKindEvent {
initializerFunction = NewHostFunctionValue(
interpreter,
constructorType,
initializerType,
func(invocation Invocation) Value {
inter := invocation.Interpreter
locationRange := invocation.LocationRange
Expand Down Expand Up @@ -1202,6 +1195,8 @@ func (interpreter *Interpreter) declareNonEnumCompositeValue(

config := interpreter.SharedState.Config

constructorType := compositeType.ConstructorFunctionType()

constructorGenerator := func(address common.Address) *HostFunctionValue {
return NewHostFunctionValue(
interpreter,
Expand Down
27 changes: 18 additions & 9 deletions runtime/sema/check_composite_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,23 +729,31 @@ func (checker *Checker) declareCompositeType(declaration ast.CompositeLikeDeclar
checker.Elaboration.SetCompositeNestedDeclarations(declaration, nestedDeclarations)

for _, nestedEntitlementType := range nestedEntitlementTypes {
compositeType.NestedTypes.Set(nestedEntitlementType.Identifier, nestedEntitlementType)
nestedEntitlementType.SetContainerType(compositeType)
compositeType.SetNestedType(
nestedEntitlementType.Identifier,
nestedEntitlementType,
)
}

for _, nestedEntitlementMapType := range nestedEntitlementMapTypes {
compositeType.NestedTypes.Set(nestedEntitlementMapType.Identifier, nestedEntitlementMapType)
nestedEntitlementMapType.SetContainerType(compositeType)
compositeType.SetNestedType(
nestedEntitlementMapType.Identifier,
nestedEntitlementMapType,
)
}

for _, nestedInterfaceType := range nestedInterfaceTypes {
compositeType.NestedTypes.Set(nestedInterfaceType.Identifier, nestedInterfaceType)
nestedInterfaceType.SetContainerType(compositeType)
compositeType.SetNestedType(
nestedInterfaceType.Identifier,
nestedInterfaceType,
)
}

for _, nestedCompositeType := range nestedCompositeTypes {
compositeType.NestedTypes.Set(nestedCompositeType.Identifier, nestedCompositeType)
nestedCompositeType.SetContainerType(compositeType)
compositeType.SetNestedType(
nestedCompositeType.Identifier,
nestedCompositeType,
)
}

return compositeType
Expand Down Expand Up @@ -837,7 +845,8 @@ func (checker *Checker) declareCompositeLikeMembersAndValue(
ArgumentLabels: nestedCompositeDeclarationVariable.ArgumentLabels,
IgnoreInSerialization: true,
DocString: nestedCompositeDeclaration.DeclarationDocString(),
})
},
)
}
for _, nestedInterfaceDeclaration := range members.Interfaces() {
// resolve conformances
Expand Down
34 changes: 34 additions & 0 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4777,6 +4777,40 @@ func (t *CompositeType) SetNestedType(name string, nestedType ContainedType) {
nestedType.SetContainerType(t)
}

func (t *CompositeType) ConstructorFunctionType() *FunctionType {
return &FunctionType{
IsConstructor: true,
Purity: t.ConstructorPurity,
Parameters: t.ConstructorParameters,
ReturnTypeAnnotation: NewTypeAnnotation(t),
}
}

func (t *CompositeType) InitializerFunctionType() *FunctionType {
return &FunctionType{
IsConstructor: true,
Purity: t.ConstructorPurity,
Parameters: t.ConstructorParameters,
ReturnTypeAnnotation: VoidTypeAnnotation,
}
}

func (t *CompositeType) InitializerEffectiveArgumentLabels() []string {
parameters := t.ConstructorParameters
if len(parameters) == 0 {
return nil
}

argumentLabels := make([]string, 0, len(parameters))
for _, parameter := range parameters {
argumentLabels = append(
argumentLabels,
parameter.EffectiveArgumentLabel(),
)
}
return argumentLabels
}

// Member

type Member struct {
Expand Down

0 comments on commit b1d23de

Please sign in to comment.