Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helpers to get constructor and initializer function type for composite type #2805

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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
}

Check warning on line 4802 in runtime/sema/type.go

View check run for this annotation

Codecov / codecov/patch

runtime/sema/type.go#L4798-L4802

Added lines #L4798 - L4802 were not covered by tests

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

Check warning on line 4811 in runtime/sema/type.go

View check run for this annotation

Codecov / codecov/patch

runtime/sema/type.go#L4804-L4811

Added lines #L4804 - L4811 were not covered by tests
}

// Member

type Member struct {
Expand Down
Loading