diff --git a/runtime/sema/account.gen.go b/runtime/sema/account.gen.go index 8b69d15bbb..6908cf6063 100644 --- a/runtime/sema/account.gen.go +++ b/runtime/sema/account.gen.go @@ -2099,57 +2099,30 @@ var CapabilitiesMappingType = &EntitlementMapType{ func init() { BuiltinEntitlementMappings[AccountMappingType.Identifier] = AccountMappingType - addToBaseActivation(AccountMappingType) BuiltinEntitlementMappings[CapabilitiesMappingType.Identifier] = CapabilitiesMappingType - addToBaseActivation(CapabilitiesMappingType) BuiltinEntitlements[StorageType.Identifier] = StorageType - addToBaseActivation(StorageType) BuiltinEntitlements[SaveValueType.Identifier] = SaveValueType - addToBaseActivation(SaveValueType) BuiltinEntitlements[LoadValueType.Identifier] = LoadValueType - addToBaseActivation(LoadValueType) BuiltinEntitlements[CopyValueType.Identifier] = CopyValueType - addToBaseActivation(CopyValueType) BuiltinEntitlements[BorrowValueType.Identifier] = BorrowValueType - addToBaseActivation(BorrowValueType) BuiltinEntitlements[ContractsType.Identifier] = ContractsType - addToBaseActivation(ContractsType) BuiltinEntitlements[AddContractType.Identifier] = AddContractType - addToBaseActivation(AddContractType) BuiltinEntitlements[UpdateContractType.Identifier] = UpdateContractType - addToBaseActivation(UpdateContractType) BuiltinEntitlements[RemoveContractType.Identifier] = RemoveContractType - addToBaseActivation(RemoveContractType) BuiltinEntitlements[KeysType.Identifier] = KeysType - addToBaseActivation(KeysType) BuiltinEntitlements[AddKeyType.Identifier] = AddKeyType - addToBaseActivation(AddKeyType) BuiltinEntitlements[RevokeKeyType.Identifier] = RevokeKeyType - addToBaseActivation(RevokeKeyType) BuiltinEntitlements[InboxType.Identifier] = InboxType - addToBaseActivation(InboxType) BuiltinEntitlements[PublishInboxCapabilityType.Identifier] = PublishInboxCapabilityType - addToBaseActivation(PublishInboxCapabilityType) BuiltinEntitlements[UnpublishInboxCapabilityType.Identifier] = UnpublishInboxCapabilityType - addToBaseActivation(UnpublishInboxCapabilityType) BuiltinEntitlements[ClaimInboxCapabilityType.Identifier] = ClaimInboxCapabilityType - addToBaseActivation(ClaimInboxCapabilityType) BuiltinEntitlements[CapabilitiesType.Identifier] = CapabilitiesType - addToBaseActivation(CapabilitiesType) BuiltinEntitlements[StorageCapabilitiesType.Identifier] = StorageCapabilitiesType - addToBaseActivation(StorageCapabilitiesType) BuiltinEntitlements[AccountCapabilitiesType.Identifier] = AccountCapabilitiesType - addToBaseActivation(AccountCapabilitiesType) BuiltinEntitlements[PublishCapabilityType.Identifier] = PublishCapabilityType - addToBaseActivation(PublishCapabilityType) BuiltinEntitlements[UnpublishCapabilityType.Identifier] = UnpublishCapabilityType - addToBaseActivation(UnpublishCapabilityType) BuiltinEntitlements[GetStorageCapabilityControllerType.Identifier] = GetStorageCapabilityControllerType - addToBaseActivation(GetStorageCapabilityControllerType) BuiltinEntitlements[IssueStorageCapabilityControllerType.Identifier] = IssueStorageCapabilityControllerType - addToBaseActivation(IssueStorageCapabilityControllerType) BuiltinEntitlements[GetAccountCapabilityControllerType.Identifier] = GetAccountCapabilityControllerType - addToBaseActivation(GetAccountCapabilityControllerType) BuiltinEntitlements[IssueAccountCapabilityControllerType.Identifier] = IssueAccountCapabilityControllerType - addToBaseActivation(IssueAccountCapabilityControllerType) } diff --git a/runtime/sema/account.go b/runtime/sema/account.go index ad7d988c1a..9d7a371fa0 100644 --- a/runtime/sema/account.go +++ b/runtime/sema/account.go @@ -56,4 +56,31 @@ var FullyEntitledAccountReferenceTypeAnnotation = NewTypeAnnotation(FullyEntitle func init() { Account_ContractsTypeAddFunctionType.Arity = &Arity{Min: 2} + addToBaseActivation(AccountMappingType) + addToBaseActivation(CapabilitiesMappingType) + addToBaseActivation(StorageType) + addToBaseActivation(SaveValueType) + addToBaseActivation(LoadValueType) + addToBaseActivation(CopyValueType) + addToBaseActivation(BorrowValueType) + addToBaseActivation(ContractsType) + addToBaseActivation(AddContractType) + addToBaseActivation(UpdateContractType) + addToBaseActivation(RemoveContractType) + addToBaseActivation(KeysType) + addToBaseActivation(AddKeyType) + addToBaseActivation(RevokeKeyType) + addToBaseActivation(InboxType) + addToBaseActivation(PublishInboxCapabilityType) + addToBaseActivation(UnpublishInboxCapabilityType) + addToBaseActivation(ClaimInboxCapabilityType) + addToBaseActivation(CapabilitiesType) + addToBaseActivation(StorageCapabilitiesType) + addToBaseActivation(AccountCapabilitiesType) + addToBaseActivation(PublishCapabilityType) + addToBaseActivation(UnpublishCapabilityType) + addToBaseActivation(GetStorageCapabilityControllerType) + addToBaseActivation(IssueStorageCapabilityControllerType) + addToBaseActivation(GetAccountCapabilityControllerType) + addToBaseActivation(IssueAccountCapabilityControllerType) } diff --git a/runtime/sema/account_capability_controller.gen.go b/runtime/sema/account_capability_controller.gen.go index c91e435e17..02d09d0769 100644 --- a/runtime/sema/account_capability_controller.gen.go +++ b/runtime/sema/account_capability_controller.gen.go @@ -102,7 +102,7 @@ var AccountCapabilityControllerType = &SimpleType{ Name: AccountCapabilityControllerTypeName, QualifiedName: AccountCapabilityControllerTypeName, TypeID: AccountCapabilityControllerTypeName, - tag: AccountCapabilityControllerTypeTag, + TypeTag: AccountCapabilityControllerTypeTag, IsResource: false, Storable: false, Equatable: false, diff --git a/runtime/sema/any_type.go b/runtime/sema/any_type.go index f5e0d97f41..7feaaf28dc 100644 --- a/runtime/sema/any_type.go +++ b/runtime/sema/any_type.go @@ -24,7 +24,7 @@ var AnyType = &SimpleType{ Name: "Any", QualifiedName: "Any", TypeID: "Any", - tag: AnyTypeTag, + TypeTag: AnyTypeTag, IsResource: false, // `Any` is never a valid type in user programs Storable: true, diff --git a/runtime/sema/anyattachment_types.go b/runtime/sema/anyattachment_types.go index c22f19c583..ef154c4926 100644 --- a/runtime/sema/anyattachment_types.go +++ b/runtime/sema/anyattachment_types.go @@ -25,7 +25,7 @@ var AnyResourceAttachmentType = &SimpleType{ Name: AnyResourceAttachmentTypeName, QualifiedName: AnyResourceAttachmentTypeName, TypeID: AnyResourceAttachmentTypeName, - tag: AnyResourceAttachmentTypeTag, + TypeTag: AnyResourceAttachmentTypeTag, IsResource: true, // The actual storability of a value is checked at run-time Storable: true, @@ -43,7 +43,7 @@ var AnyStructAttachmentType = &SimpleType{ Name: AnyStructAttachmentTypeName, QualifiedName: AnyStructAttachmentTypeName, TypeID: AnyStructAttachmentTypeName, - tag: AnyStructAttachmentTypeTag, + TypeTag: AnyStructAttachmentTypeTag, IsResource: false, // The actual storability of a value is checked at run-time Storable: true, diff --git a/runtime/sema/anyresource_type.go b/runtime/sema/anyresource_type.go index b19d72040f..6b20d9563a 100644 --- a/runtime/sema/anyresource_type.go +++ b/runtime/sema/anyresource_type.go @@ -23,7 +23,7 @@ var AnyResourceType = &SimpleType{ Name: "AnyResource", QualifiedName: "AnyResource", TypeID: "AnyResource", - tag: AnyResourceTypeTag, + TypeTag: AnyResourceTypeTag, IsResource: true, // The actual storability of a value is checked at run-time Storable: true, diff --git a/runtime/sema/anystruct_type.go b/runtime/sema/anystruct_type.go index edbc5e8220..a4739610a3 100644 --- a/runtime/sema/anystruct_type.go +++ b/runtime/sema/anystruct_type.go @@ -23,7 +23,7 @@ var AnyStructType = &SimpleType{ Name: "AnyStruct", QualifiedName: "AnyStruct", TypeID: "AnyStruct", - tag: AnyStructTypeTag, + TypeTag: AnyStructTypeTag, IsResource: false, // The actual storability of a value is checked at run-time Storable: true, diff --git a/runtime/sema/block.gen.go b/runtime/sema/block.gen.go index 058282a4ff..7c490c3a0c 100644 --- a/runtime/sema/block.gen.go +++ b/runtime/sema/block.gen.go @@ -69,7 +69,7 @@ var BlockType = &SimpleType{ Name: BlockTypeName, QualifiedName: BlockTypeName, TypeID: BlockTypeName, - tag: BlockTypeTag, + TypeTag: BlockTypeTag, IsResource: false, Storable: false, Equatable: false, diff --git a/runtime/sema/bool_type.go b/runtime/sema/bool_type.go index c83a2eafc1..1870777d78 100644 --- a/runtime/sema/bool_type.go +++ b/runtime/sema/bool_type.go @@ -23,7 +23,7 @@ var BoolType = &SimpleType{ Name: "Bool", QualifiedName: "Bool", TypeID: "Bool", - tag: BoolTypeTag, + TypeTag: BoolTypeTag, IsResource: false, Storable: true, Equatable: true, diff --git a/runtime/sema/character.gen.go b/runtime/sema/character.gen.go index 36d89e50c4..69909c376f 100644 --- a/runtime/sema/character.gen.go +++ b/runtime/sema/character.gen.go @@ -50,7 +50,7 @@ var CharacterType = &SimpleType{ Name: CharacterTypeName, QualifiedName: CharacterTypeName, TypeID: CharacterTypeName, - tag: CharacterTypeTag, + TypeTag: CharacterTypeTag, IsResource: false, Storable: true, Equatable: true, diff --git a/runtime/sema/deployedcontract.gen.go b/runtime/sema/deployedcontract.gen.go index 1e502abccd..864c5dd718 100644 --- a/runtime/sema/deployedcontract.gen.go +++ b/runtime/sema/deployedcontract.gen.go @@ -78,7 +78,7 @@ var DeployedContractType = &SimpleType{ Name: DeployedContractTypeName, QualifiedName: DeployedContractTypeName, TypeID: DeployedContractTypeName, - tag: DeployedContractTypeTag, + TypeTag: DeployedContractTypeTag, IsResource: false, Storable: false, Equatable: false, diff --git a/runtime/sema/entitlements.gen.go b/runtime/sema/entitlements.gen.go index 559ebf6937..5ae0d1b995 100644 --- a/runtime/sema/entitlements.gen.go +++ b/runtime/sema/entitlements.gen.go @@ -33,9 +33,6 @@ var RemoveType = &EntitlementType{ func init() { BuiltinEntitlements[MutateType.Identifier] = MutateType - addToBaseActivation(MutateType) BuiltinEntitlements[InsertType.Identifier] = InsertType - addToBaseActivation(InsertType) BuiltinEntitlements[RemoveType.Identifier] = RemoveType - addToBaseActivation(RemoveType) } diff --git a/runtime/sema/entitlements.go b/runtime/sema/entitlements.go index d2cbeebd04..3870c6e5cf 100644 --- a/runtime/sema/entitlements.go +++ b/runtime/sema/entitlements.go @@ -19,3 +19,9 @@ package sema //go:generate go run ./gen entitlements.cdc entitlements.gen.go + +func init() { + addToBaseActivation(MutateType) + addToBaseActivation(InsertType) + addToBaseActivation(RemoveType) +} diff --git a/runtime/sema/gen/golden_test.go b/runtime/sema/gen/golden_test.go new file mode 100644 index 0000000000..6eccdc779d --- /dev/null +++ b/runtime/sema/gen/golden_test.go @@ -0,0 +1,95 @@ +/* + * 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 main + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/onflow/cadence/runtime/common" + "github.com/onflow/cadence/runtime/sema" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/comparable" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/composite_type_pragma" + "github.com/onflow/cadence/runtime/sema/gen/testdata/constructor" + "github.com/onflow/cadence/runtime/sema/gen/testdata/contract" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/contract" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/docstrings" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/entitlement" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/equatable" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/exportable" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/fields" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/functions" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/importable" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/member_accessible" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/nested" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/simple_resource" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/simple_struct" + _ "github.com/onflow/cadence/runtime/sema/gen/testdata/storable" + "github.com/onflow/cadence/runtime/stdlib" + "github.com/onflow/cadence/runtime/tests/checker" +) + +func TestConstructor(t *testing.T) { + + t.Parallel() + + baseValueActivation := sema.NewVariableActivation(sema.BaseValueActivation) + baseValueActivation.DeclareValue(stdlib.StandardLibraryValue{ + Name: constructor.FooType.Identifier, + Type: constructor.FooTypeConstructorType, + Kind: common.DeclarationKindFunction, + }) + + _, err := checker.ParseAndCheckWithOptions(t, + ` + let x = Foo(bar: 1) + `, + checker.ParseAndCheckOptions{ + Config: &sema.Config{ + BaseValueActivation: baseValueActivation, + }, + }, + ) + require.NoError(t, err) +} + +func TestContract(t *testing.T) { + + t.Parallel() + + baseValueActivation := sema.NewVariableActivation(sema.BaseValueActivation) + baseValueActivation.DeclareValue(stdlib.StandardLibraryValue{ + Name: contract.TestType.Identifier, + Type: contract.TestType, + Kind: common.DeclarationKindContract, + }) + + _, err := checker.ParseAndCheckWithOptions(t, + ` + let x = Test.Foo(bar: 1) + `, + checker.ParseAndCheckOptions{ + Config: &sema.Config{ + BaseValueActivation: baseValueActivation, + }, + }, + ) + require.NoError(t, err) +} diff --git a/runtime/sema/gen/main.go b/runtime/sema/gen/main.go index 3fd35f3e5b..b7778b0a97 100644 --- a/runtime/sema/gen/main.go +++ b/runtime/sema/gen/main.go @@ -38,6 +38,7 @@ import ( "github.com/onflow/cadence/runtime/errors" "github.com/onflow/cadence/runtime/parser" "github.com/onflow/cadence/runtime/pretty" + "github.com/onflow/cadence/runtime/sema" ) const semaPath = "github.com/onflow/cadence/runtime/sema" @@ -161,6 +162,7 @@ type typeDecl struct { memberAccessible bool memberDeclarations []ast.Declaration nestedTypes []*typeDecl + hasConstructor bool } type generator struct { @@ -289,6 +291,7 @@ func (g *generator) addFunctionTypeDeclaration( decl.ParameterList, decl.TypeParameterList, typeParams, + false, ), ), ) @@ -323,8 +326,106 @@ func (g *generator) declarationDocString(decl ast.Declaration) dst.Expr { return renderDocString(docString) } -func (*generator) VisitSpecialFunctionDeclaration(_ *ast.SpecialFunctionDeclaration) struct{} { - panic("special function declarations are not supported") +func (g *generator) VisitSpecialFunctionDeclaration(decl *ast.SpecialFunctionDeclaration) (_ struct{}) { + if decl.Kind != common.DeclarationKindInitializer { + panic(fmt.Errorf( + "%s special function declarations are not supported", + decl.Kind.Name(), + )) + } + + typeDecl := g.currentTypeDecl() + + fullTypeName := typeDecl.fullTypeName + + if typeDecl.hasConstructor { + panic(fmt.Errorf("invalid second initializer for type %s", fullTypeName)) + } + typeDecl.hasConstructor = true + + isResource := typeDecl.compositeKind == common.CompositeKindResource + + typeNames := make([]string, 0, len(g.typeStack)) + for i := 0; i < len(g.typeStack); i++ { + typeNames = append(typeNames, g.typeStack[i].typeName) + } + + g.addConstructorTypeDeclaration(decl, fullTypeName, typeNames, isResource) + + g.addConstructorDocStringDeclaration(decl, fullTypeName) + + return +} + +func (g *generator) addConstructorTypeDeclaration( + initDecl *ast.SpecialFunctionDeclaration, + fullTypeName string, + typeNames []string, + isResource bool, +) { + decl := initDecl.FunctionDeclaration + + parameters := decl.ParameterList.Parameters + + parameterTypeAnnotations := make([]*ast.TypeAnnotation, 0, len(parameters)) + for _, parameter := range parameters { + parameterTypeAnnotations = append( + parameterTypeAnnotations, + parameter.TypeAnnotation, + ) + } + + nestedIdentifiers := make([]ast.Identifier, 0, len(typeNames)-1) + for i := 1; i < len(typeNames); i++ { + typeName := typeNames[i] + nestedIdentifiers = append( + nestedIdentifiers, + ast.Identifier{ + Identifier: typeName, + }, + ) + } + + returnType := &ast.NominalType{ + NestedIdentifiers: nestedIdentifiers, + Identifier: ast.Identifier{ + Identifier: typeNames[0], + }, + } + + g.addDecls( + goVarDecl( + constructorTypeVarName(fullTypeName), + functionTypeExpr( + &ast.FunctionType{ + PurityAnnotation: decl.Purity, + ReturnTypeAnnotation: &ast.TypeAnnotation{ + Type: returnType, + IsResource: isResource, + }, + ParameterTypeAnnotations: parameterTypeAnnotations, + }, + decl.ParameterList, + nil, + nil, + true, + ), + ), + ) +} + +func (g *generator) addConstructorDocStringDeclaration( + decl *ast.SpecialFunctionDeclaration, + fullTypeName string, +) { + docString := g.declarationDocString(decl) + + g.addDecls( + goConstDecl( + constructorDocStringVarName(fullTypeName), + docString, + ), + ) } func (g *generator) VisitCompositeDeclaration(decl *ast.CompositeDeclaration) (_ struct{}) { @@ -471,11 +572,13 @@ func (g *generator) VisitCompositeDeclaration(decl *ast.CompositeDeclaration) (_ typeVarDecl = compositeTypeExpr(typeDecl) } - tyVarName := typeVarName(typeDecl.fullTypeName) + fullTypeName := typeDecl.fullTypeName + + tyVarName := typeVarName(fullTypeName) g.addDecls( goConstDecl( - typeNameVarName(typeDecl.fullTypeName), + typeNameVarName(fullTypeName), goStringLit(typeName), ), goVarDecl( @@ -496,7 +599,7 @@ func (g *generator) VisitCompositeDeclaration(decl *ast.CompositeDeclaration) (_ // } // } - memberResolversFunc := simpleTypeMemberResolversFunc(typeDecl.fullTypeName, memberDeclarations) + memberResolversFunc := simpleTypeMemberResolversFunc(fullTypeName, memberDeclarations) g.addDecls( &dst.FuncDecl{ @@ -527,65 +630,93 @@ func (g *generator) VisitCompositeDeclaration(decl *ast.CompositeDeclaration) (_ // members := []*Member{...} // t.Members = MembersAsMap(members) // t.Fields = MembersFieldNames(members) + // t.ConstructorParameters = ... // } - members := membersExpr(typeDecl.fullTypeName, tyVarName, memberDeclarations) + members := membersExpr( + fullTypeName, + tyVarName, + memberDeclarations, + ) const membersVariableIdentifier = "members" + stmts := []dst.Stmt{ + &dst.DeclStmt{ + Decl: goVarDecl( + membersVariableIdentifier, + members, + ), + }, + &dst.AssignStmt{ + Lhs: []dst.Expr{ + &dst.SelectorExpr{ + X: dst.NewIdent(tyVarName), + Sel: dst.NewIdent("Members"), + }, + }, + Tok: token.ASSIGN, + Rhs: []dst.Expr{ + &dst.CallExpr{ + Fun: &dst.Ident{ + Name: "MembersAsMap", + Path: semaPath, + }, + Args: []dst.Expr{ + dst.NewIdent(membersVariableIdentifier), + }, + }, + }, + }, + &dst.AssignStmt{ + Lhs: []dst.Expr{ + &dst.SelectorExpr{ + X: dst.NewIdent(tyVarName), + Sel: dst.NewIdent("Fields"), + }, + }, + Tok: token.ASSIGN, + Rhs: []dst.Expr{ + &dst.CallExpr{ + Fun: &dst.Ident{ + Name: "MembersFieldNames", + Path: semaPath, + }, + Args: []dst.Expr{ + dst.NewIdent(membersVariableIdentifier), + }, + }, + }, + }, + } + + if typeDecl.hasConstructor { + stmts = append( + stmts, + &dst.AssignStmt{ + Lhs: []dst.Expr{ + &dst.SelectorExpr{ + X: dst.NewIdent(tyVarName), + Sel: dst.NewIdent("ConstructorParameters"), + }, + }, + Tok: token.ASSIGN, + Rhs: []dst.Expr{ + &dst.SelectorExpr{ + X: dst.NewIdent(constructorTypeVarName(fullTypeName)), + Sel: dst.NewIdent("Parameters"), + }, + }, + }, + ) + } + g.addDecls( &dst.FuncDecl{ Name: dst.NewIdent("init"), Type: &dst.FuncType{}, Body: &dst.BlockStmt{ - List: []dst.Stmt{ - &dst.DeclStmt{ - Decl: goVarDecl( - membersVariableIdentifier, - members, - ), - }, - &dst.AssignStmt{ - Lhs: []dst.Expr{ - &dst.SelectorExpr{ - X: dst.NewIdent(tyVarName), - Sel: dst.NewIdent("Members"), - }, - }, - Tok: token.ASSIGN, - Rhs: []dst.Expr{ - &dst.CallExpr{ - Fun: &dst.Ident{ - Name: "MembersAsMap", - Path: semaPath, - }, - Args: []dst.Expr{ - dst.NewIdent(membersVariableIdentifier), - }, - }, - }, - }, - &dst.AssignStmt{ - Lhs: []dst.Expr{ - &dst.SelectorExpr{ - X: dst.NewIdent(tyVarName), - Sel: dst.NewIdent("Fields"), - }, - }, - Tok: token.ASSIGN, - Rhs: []dst.Expr{ - &dst.CallExpr{ - Fun: &dst.Ident{ - Name: "MembersFieldNames", - Path: semaPath, - }, - Args: []dst.Expr{ - dst.NewIdent(membersVariableIdentifier), - }, - }, - }, - }, - }, + List: stmts, }, }, ) @@ -662,7 +793,11 @@ func (g *generator) VisitFieldDeclaration(decl *ast.FieldDeclaration) (_ struct{ } func (g *generator) currentFullTypeName() string { - return g.typeStack[len(g.typeStack)-1].fullTypeName + return g.currentTypeDecl().fullTypeName +} + +func (g *generator) currentTypeDecl() *typeDecl { + return g.typeStack[len(g.typeStack)-1] } func typeExpr(t ast.Type, typeParams map[string]string) dst.Expr { @@ -686,9 +821,14 @@ func typeExpr(t ast.Type, typeParams map[string]string) dst.Expr { } } + inSema := sema.BaseTypeActivation.Find(identifier) != nil + switch identifier { case "": identifier = "Void" + inSema = true + case "Any": + inSema = true case "Address": identifier = "TheAddress" case "Type": @@ -715,7 +855,11 @@ func typeExpr(t ast.Type, typeParams map[string]string) dst.Expr { identifier = fullIdentifier.String() } - return typeVarIdent(identifier) + ident := typeVarIdent(identifier) + if inSema { + ident.Path = semaPath + } + return ident case *ast.OptionalType: innerType := typeExpr(t.Type, typeParams) @@ -810,7 +954,13 @@ func typeExpr(t ast.Type, typeParams map[string]string) dst.Expr { } case *ast.FunctionType: - return functionTypeExpr(t, nil, nil, typeParams) + return functionTypeExpr( + t, + nil, + nil, + typeParams, + false, + ) case *ast.InstantiationType: typeArguments := t.TypeArguments @@ -888,6 +1038,7 @@ func functionTypeExpr( parameters *ast.ParameterList, typeParameterList *ast.TypeParameterList, typeParams map[string]string, + isConstructor bool, ) dst.Expr { // Function purity @@ -1020,7 +1171,14 @@ func functionTypeExpr( if t.ReturnTypeAnnotation != nil { returnTypeExpr = typeExpr(t.ReturnTypeAnnotation.Type, typeParams) } else { - returnTypeExpr = typeVarIdent("Void") + returnTypeExpr = typeExpr( + &ast.NominalType{ + Identifier: ast.Identifier{ + Identifier: "Void", + }, + }, + nil, + ) } returnTypeExpr.Decorations().Before = dst.NewLine @@ -1040,6 +1198,16 @@ func functionTypeExpr( ) } + if isConstructor { + compositeElements = append( + compositeElements, + goKeyValue( + "IsConstructor", + goBoolLit(true), + ), + ) + } + if typeParametersExpr != nil { compositeElements = append( compositeElements, @@ -1106,11 +1274,7 @@ func (*generator) VisitImportDeclaration(_ *ast.ImportDeclaration) struct{} { const typeNameSeparator = '_' -func (g *generator) newFullTypeName(typeName string) string { - if len(g.typeStack) == 0 { - return typeName - } - parentFullTypeName := g.typeStack[len(g.typeStack)-1].fullTypeName +func joinTypeName(parentFullTypeName string, typeName string) string { return fmt.Sprintf( "%s%c%s", escapeTypeName(parentFullTypeName), @@ -1119,6 +1283,14 @@ func (g *generator) newFullTypeName(typeName string) string { ) } +func (g *generator) newFullTypeName(typeName string) string { + if len(g.typeStack) == 0 { + return typeName + } + parentFullTypeName := g.typeStack[len(g.typeStack)-1].fullTypeName + return joinTypeName(parentFullTypeName, typeName) +} + func escapeTypeName(typeName string) string { return strings.ReplaceAll(typeName, string(typeNameSeparator), "__") } @@ -1157,12 +1329,10 @@ func (g *generator) generateTypeInit(program *ast.Program) { // // func init() { // BuiltinEntitlements[FooEntitlement.Identifier] = FooEntitlement - // addToBaseActivation(FooEntitlement) // // ... // // BuiltinEntitlements[BarEntitlementMapping.Identifier] = BarEntitlementMapping - // addToBaseActivation(BarEntitlementMapping) // // ... // } @@ -1205,7 +1375,10 @@ func entitlementMapInitStatements(declaration *ast.EntitlementMappingDeclaration mapUpdateStmt := &dst.AssignStmt{ Lhs: []dst.Expr{ &dst.IndexExpr{ - X: dst.NewIdent(mapName), + X: &dst.Ident{ + Name: mapName, + Path: semaPath, + }, Index: &dst.SelectorExpr{ X: dst.NewIdent(varName), Sel: dst.NewIdent("Identifier"), @@ -1218,18 +1391,8 @@ func entitlementMapInitStatements(declaration *ast.EntitlementMappingDeclaration }, } - typeRegisterStmt := &dst.ExprStmt{ - X: &dst.CallExpr{ - Fun: dst.NewIdent("addToBaseActivation"), - Args: []dst.Expr{ - dst.NewIdent(varName), - }, - }, - } - return []dst.Stmt{ mapUpdateStmt, - typeRegisterStmt, } } @@ -1240,7 +1403,10 @@ func entitlementInitStatements(declaration *ast.EntitlementDeclaration) []dst.St mapUpdateStmt := &dst.AssignStmt{ Lhs: []dst.Expr{ &dst.IndexExpr{ - X: dst.NewIdent(mapName), + X: &dst.Ident{ + Name: mapName, + Path: semaPath, + }, Index: &dst.SelectorExpr{ X: dst.NewIdent(varName), Sel: dst.NewIdent("Identifier"), @@ -1253,18 +1419,8 @@ func entitlementInitStatements(declaration *ast.EntitlementDeclaration) []dst.St }, } - typeRegisterStmt := &dst.ExprStmt{ - X: &dst.CallExpr{ - Fun: dst.NewIdent("addToBaseActivation"), - Args: []dst.Expr{ - dst.NewIdent(varName), - }, - }, - } - return []dst.Stmt{ mapUpdateStmt, - typeRegisterStmt, } } @@ -1345,24 +1501,24 @@ func compositeKindExpr(compositeKind common.CompositeKind) *dst.Ident { } } -func typeVarName(typeName string) string { - return fmt.Sprintf("%sType", typeName) +func typeVarName(fullTypeName string) string { + return fmt.Sprintf("%sType", fullTypeName) } -func typeVarIdent(typeName string) *dst.Ident { - return dst.NewIdent(typeVarName(typeName)) +func typeVarIdent(fullTypeName string) *dst.Ident { + return dst.NewIdent(typeVarName(fullTypeName)) } -func typeNameVarName(typeName string) string { - return fmt.Sprintf("%sTypeName", typeName) +func typeNameVarName(fullTypeName string) string { + return fmt.Sprintf("%sTypeName", fullTypeName) } -func typeNameVarIdent(typeName string) *dst.Ident { - return dst.NewIdent(typeNameVarName(typeName)) +func typeNameVarIdent(fullTypeName string) *dst.Ident { + return dst.NewIdent(typeNameVarName(fullTypeName)) } -func typeTagVarIdent(typeName string) *dst.Ident { - return dst.NewIdent(fmt.Sprintf("%sTypeTag", typeName)) +func typeTagVarIdent(fullTypeName string) *dst.Ident { + return dst.NewIdent(fmt.Sprintf("%sTypeTag", fullTypeName)) } func memberVarName(fullTypeName, fieldName, kind, part string) string { @@ -1391,6 +1547,10 @@ func functionTypeVarName(fullTypeName, functionName string) string { return memberVarName(fullTypeName, functionName, "Function", "Type") } +func constructorTypeVarName(fullTypeName string) string { + return memberVarName(fullTypeName, "", "Constructor", "Type") +} + func functionTypeParameterVarName(fullTypeName, functionName, typeParameterName string) string { return memberVarName(fullTypeName, functionName, "Function", "TypeParameter"+typeParameterName) } @@ -1403,6 +1563,10 @@ func functionDocStringVarName(fullTypeName, functionName string) string { return memberVarName(fullTypeName, functionName, "Function", "DocString") } +func constructorDocStringVarName(fullTypeName string) string { + return memberVarName(fullTypeName, "", "Constructor", "DocString") +} + func simpleTypeLiteral(ty *typeDecl) dst.Expr { // &SimpleType{ @@ -1423,7 +1587,7 @@ func simpleTypeLiteral(ty *typeDecl) dst.Expr { goKeyValue("Name", typeNameVarIdent(ty.fullTypeName)), goKeyValue("QualifiedName", typeNameVarIdent(ty.fullTypeName)), goKeyValue("TypeID", typeNameVarIdent(ty.fullTypeName)), - goKeyValue("tag", typeTagVarIdent(ty.fullTypeName)), + goKeyValue("TypeTag", typeTagVarIdent(ty.fullTypeName)), goKeyValue("IsResource", goBoolLit(isResource)), goKeyValue("Storable", goBoolLit(ty.storable)), goKeyValue("Equatable", goBoolLit(ty.equatable)), @@ -1460,7 +1624,11 @@ func simpleTypeMemberResolversFunc(fullTypeName string, declarations []ast.Decla Path: semaPath, }, Args: []dst.Expr{ - membersExpr(fullTypeName, typeVarName, declarations), + membersExpr( + fullTypeName, + typeVarName, + declarations, + ), }, }, }, @@ -1522,11 +1690,34 @@ func membersExpr( memberName, ) + case common.DeclarationKindInitializer: + // Generated as a member of the container + continue + case common.DeclarationKindStructureInterface, common.DeclarationKindStructure, common.DeclarationKindResource, common.DeclarationKindResourceInterface: + initializers := declaration.DeclarationMembers().Initializers() + if len(initializers) > 0 { + initializer := initializers[0] + + typeName := declaration.DeclarationIdentifier().Identifier + + element := newDeclarationMember( + joinTypeName(fullTypeName, typeName), + typeVarName, + // type name is used instead + "", + initializer, + ) + element.Decorations().Before = dst.NewLine + element.Decorations().After = dst.NewLine + + elements = append(elements, element) + } + continue default: @@ -1536,7 +1727,12 @@ func membersExpr( )) } - element := newDeclarationMember(fullTypeName, typeVarName, memberVarName, declaration) + element := newDeclarationMember( + fullTypeName, + typeVarName, + memberVarName, + declaration, + ) element.Decorations().Before = dst.NewLine element.Decorations().After = dst.NewLine @@ -1658,11 +1854,17 @@ func newDeclarationMember( access := declaration.DeclarationAccess() if access == ast.AccessNotSpecified { - panic(fmt.Errorf( - "member with unspecified access: %s.%s", - fullTypeName, - declarationName, - )) + switch declaration.DeclarationKind() { + case common.DeclarationKindInitializer: + access = ast.AccessAll + + default: + panic(fmt.Errorf( + "member with unspecified access: %s.%s", + fullTypeName, + declarationName, + )) + } } if fieldDeclaration, ok := declaration.(*ast.FieldDeclaration); ok { @@ -1691,9 +1893,10 @@ func newDeclarationMember( declarationKind := declaration.DeclarationKind() - // Function + // Function or initializer - if declarationKind == common.DeclarationKindFunction { + switch declarationKind { + case common.DeclarationKindFunction: args := []dst.Expr{ dst.NewIdent(containerTypeVariableIdentifier), accessExpr(access), @@ -1714,8 +1917,32 @@ func newDeclarationMember( }, Args: args, } + + case common.DeclarationKindInitializer: + args := []dst.Expr{ + dst.NewIdent(containerTypeVariableIdentifier), + accessExpr(access), + typeNameVarIdent(fullTypeName), + dst.NewIdent(constructorTypeVarName(fullTypeName)), + dst.NewIdent(constructorDocStringVarName(fullTypeName)), + } + + for _, arg := range args { + arg.Decorations().Before = dst.NewLine + arg.Decorations().After = dst.NewLine + } + + return &dst.CallExpr{ + Fun: &dst.Ident{ + Name: "NewUnmeteredConstructorMember", + Path: semaPath, + }, + Args: args, + } } + // Unsupported + panic(fmt.Errorf( "%s members are not supported", declarationKind.Name(), diff --git a/runtime/sema/gen/main_test.go b/runtime/sema/gen/main_test.go index 1e8def123d..e1862abb96 100644 --- a/runtime/sema/gen/main_test.go +++ b/runtime/sema/gen/main_test.go @@ -38,21 +38,22 @@ func TestFiles(t *testing.T) { t.Parallel() - test := func(inputPath string) { - // The test name is the filename without the extension. - _, filename := filepath.Split(inputPath) - testname := filename[:len(filename)-len(filepath.Ext(inputPath))] + test := func(dirPath string) { + // The test name is the directory name + _, testName := filepath.Split(dirPath) - t.Run(testname, func(t *testing.T) { + t.Run(testName, func(t *testing.T) { t.Parallel() outFile, err := os.CreateTemp(t.TempDir(), "gen.*.go") require.NoError(t, err) defer outFile.Close() - gen(inputPath, outFile, "github.com/onflow/cadence/runtime/sema") + inputPath := filepath.Join(dirPath, "test.cdc") - goldenPath := filepath.Join(testDataDirectory, testname+".golden.go") + gen(inputPath, outFile, "github.com/onflow/cadence/runtime/sema/gen/"+dirPath) + + goldenPath := filepath.Join(dirPath, "test.golden.go") want, err := os.ReadFile(goldenPath) require.NoError(t, err) @@ -66,7 +67,7 @@ func TestFiles(t *testing.T) { }) } - paths, err := filepath.Glob(filepath.Join(testDataDirectory, "*.cdc")) + paths, err := filepath.Glob(filepath.Join(testDataDirectory, "*")) require.NoError(t, err) for _, path := range paths { diff --git a/runtime/sema/gen/testdata/comparable/helper.go b/runtime/sema/gen/testdata/comparable/helper.go new file mode 100644 index 0000000000..efc83f3b98 --- /dev/null +++ b/runtime/sema/gen/testdata/comparable/helper.go @@ -0,0 +1,23 @@ +/* + * 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 comparable + +import "github.com/onflow/cadence/runtime/sema" + +var TestTypeTag sema.TypeTag diff --git a/runtime/sema/gen/testdata/comparable.cdc b/runtime/sema/gen/testdata/comparable/test.cdc similarity index 100% rename from runtime/sema/gen/testdata/comparable.cdc rename to runtime/sema/gen/testdata/comparable/test.cdc diff --git a/runtime/sema/gen/testdata/comparable.golden.go b/runtime/sema/gen/testdata/comparable/test.golden.go similarity index 82% rename from runtime/sema/gen/testdata/comparable.golden.go rename to runtime/sema/gen/testdata/comparable/test.golden.go index 610a531517..c088f5d175 100644 --- a/runtime/sema/gen/testdata/comparable.golden.go +++ b/runtime/sema/gen/testdata/comparable/test.golden.go @@ -1,4 +1,4 @@ -// Code generated from testdata/comparable.cdc. DO NOT EDIT. +// Code generated from testdata/comparable/test.cdc. DO NOT EDIT. /* * Cadence - The resource-oriented smart contract programming language * @@ -17,15 +17,17 @@ * limitations under the License. */ -package sema +package comparable + +import "github.com/onflow/cadence/runtime/sema" const TestTypeName = "Test" -var TestType = &SimpleType{ +var TestType = &sema.SimpleType{ Name: TestTypeName, QualifiedName: TestTypeName, TypeID: TestTypeName, - tag: TestTypeTag, + TypeTag: TestTypeTag, IsResource: false, Storable: false, Equatable: false, diff --git a/runtime/sema/gen/testdata/composite-type-pragma.cdc b/runtime/sema/gen/testdata/composite_type_pragma/test.cdc similarity index 100% rename from runtime/sema/gen/testdata/composite-type-pragma.cdc rename to runtime/sema/gen/testdata/composite_type_pragma/test.cdc diff --git a/runtime/sema/gen/testdata/composite-type-pragma.golden.go b/runtime/sema/gen/testdata/composite_type_pragma/test.golden.go similarity index 75% rename from runtime/sema/gen/testdata/composite-type-pragma.golden.go rename to runtime/sema/gen/testdata/composite_type_pragma/test.golden.go index f975ff112f..0ce3c47a03 100644 --- a/runtime/sema/gen/testdata/composite-type-pragma.golden.go +++ b/runtime/sema/gen/testdata/composite_type_pragma/test.golden.go @@ -1,4 +1,4 @@ -// Code generated from testdata/composite-type-pragma.cdc. DO NOT EDIT. +// Code generated from testdata/composite_type_pragma/test.cdc. DO NOT EDIT. /* * Cadence - The resource-oriented smart contract programming language * @@ -17,14 +17,17 @@ * limitations under the License. */ -package sema +package composite_type_pragma -import "github.com/onflow/cadence/runtime/common" +import ( + "github.com/onflow/cadence/runtime/common" + "github.com/onflow/cadence/runtime/sema" +) const TestTypeName = "Test" -var TestType = func() *CompositeType { - var t = &CompositeType{ +var TestType = func() *sema.CompositeType { + var t = &sema.CompositeType{ Identifier: TestTypeName, Kind: common.CompositeKindStructure, ImportableBuiltin: false, diff --git a/runtime/sema/gen/testdata/constructor/test.cdc b/runtime/sema/gen/testdata/constructor/test.cdc new file mode 100644 index 0000000000..3c3881ee91 --- /dev/null +++ b/runtime/sema/gen/testdata/constructor/test.cdc @@ -0,0 +1,6 @@ +/// The Foo type +struct Foo { + + /// Constructs a new Foo + init(bar: Int) +} diff --git a/runtime/sema/gen/testdata/constructor/test.golden.go b/runtime/sema/gen/testdata/constructor/test.golden.go new file mode 100644 index 0000000000..3b2809675a --- /dev/null +++ b/runtime/sema/gen/testdata/constructor/test.golden.go @@ -0,0 +1,63 @@ +// Code generated from testdata/constructor/test.cdc. DO NOT EDIT. +/* + * 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 constructor + +import ( + "github.com/onflow/cadence/runtime/common" + "github.com/onflow/cadence/runtime/sema" +) + +var FooTypeConstructorType = &sema.FunctionType{ + IsConstructor: true, + Parameters: []sema.Parameter{ + { + Identifier: "bar", + TypeAnnotation: sema.NewTypeAnnotation(sema.IntType), + }, + }, + ReturnTypeAnnotation: sema.NewTypeAnnotation( + FooType, + ), +} + +const FooTypeConstructorDocString = ` +Constructs a new Foo +` + +const FooTypeName = "Foo" + +var FooType = func() *sema.CompositeType { + var t = &sema.CompositeType{ + Identifier: FooTypeName, + Kind: common.CompositeKindStructure, + ImportableBuiltin: false, + HasComputedMembers: true, + } + + return t +}() + +func init() { + var members = []*sema.Member{} + + FooType.Members = sema.MembersAsMap(members) + FooType.Fields = sema.MembersFieldNames(members) + FooType.ConstructorParameters = FooTypeConstructorType.Parameters +} diff --git a/runtime/sema/gen/testdata/contract/test.cdc b/runtime/sema/gen/testdata/contract/test.cdc new file mode 100644 index 0000000000..51ba964699 --- /dev/null +++ b/runtime/sema/gen/testdata/contract/test.cdc @@ -0,0 +1,10 @@ +access(all) +contract Test { + + /// The Foo type + struct Foo { + + /// Constructs a new Foo + init(bar: Int) + } +} diff --git a/runtime/sema/gen/testdata/contract/test.golden.go b/runtime/sema/gen/testdata/contract/test.golden.go new file mode 100644 index 0000000000..1efdaeff66 --- /dev/null +++ b/runtime/sema/gen/testdata/contract/test.golden.go @@ -0,0 +1,93 @@ +// Code generated from testdata/contract/test.cdc. DO NOT EDIT. +/* + * 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 contract + +import ( + "github.com/onflow/cadence/runtime/ast" + "github.com/onflow/cadence/runtime/common" + "github.com/onflow/cadence/runtime/sema" +) + +var Test_FooTypeConstructorType = &sema.FunctionType{ + IsConstructor: true, + Parameters: []sema.Parameter{ + { + Identifier: "bar", + TypeAnnotation: sema.NewTypeAnnotation(sema.IntType), + }, + }, + ReturnTypeAnnotation: sema.NewTypeAnnotation( + Test_FooType, + ), +} + +const Test_FooTypeConstructorDocString = ` +Constructs a new Foo +` + +const Test_FooTypeName = "Foo" + +var Test_FooType = func() *sema.CompositeType { + var t = &sema.CompositeType{ + Identifier: Test_FooTypeName, + Kind: common.CompositeKindStructure, + ImportableBuiltin: false, + HasComputedMembers: true, + } + + return t +}() + +func init() { + var members = []*sema.Member{} + + Test_FooType.Members = sema.MembersAsMap(members) + Test_FooType.Fields = sema.MembersFieldNames(members) + Test_FooType.ConstructorParameters = Test_FooTypeConstructorType.Parameters +} + +const TestTypeName = "Test" + +var TestType = func() *sema.CompositeType { + var t = &sema.CompositeType{ + Identifier: TestTypeName, + Kind: common.CompositeKindContract, + ImportableBuiltin: false, + HasComputedMembers: true, + } + + t.SetNestedType(Test_FooTypeName, Test_FooType) + return t +}() + +func init() { + var members = []*sema.Member{ + sema.NewUnmeteredConstructorMember( + TestType, + sema.PrimitiveAccess(ast.AccessAll), + Test_FooTypeName, + Test_FooTypeConstructorType, + Test_FooTypeConstructorDocString, + ), + } + + TestType.Members = sema.MembersAsMap(members) + TestType.Fields = sema.MembersFieldNames(members) +} diff --git a/runtime/sema/gen/testdata/docstrings/helper.go b/runtime/sema/gen/testdata/docstrings/helper.go new file mode 100644 index 0000000000..89877cfac0 --- /dev/null +++ b/runtime/sema/gen/testdata/docstrings/helper.go @@ -0,0 +1,23 @@ +/* + * 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 docstrings + +import "github.com/onflow/cadence/runtime/sema" + +var DocstringsTypeTag sema.TypeTag diff --git a/runtime/sema/gen/testdata/docstrings.cdc b/runtime/sema/gen/testdata/docstrings/test.cdc similarity index 100% rename from runtime/sema/gen/testdata/docstrings.cdc rename to runtime/sema/gen/testdata/docstrings/test.cdc diff --git a/runtime/sema/gen/testdata/docstrings.golden.go b/runtime/sema/gen/testdata/docstrings/test.golden.go similarity index 66% rename from runtime/sema/gen/testdata/docstrings.golden.go rename to runtime/sema/gen/testdata/docstrings/test.golden.go index 68f4102b92..a35767c913 100644 --- a/runtime/sema/gen/testdata/docstrings.golden.go +++ b/runtime/sema/gen/testdata/docstrings/test.golden.go @@ -1,4 +1,4 @@ -// Code generated from testdata/docstrings.cdc. DO NOT EDIT. +// Code generated from testdata/docstrings/test.cdc. DO NOT EDIT. /* * Cadence - The resource-oriented smart contract programming language * @@ -17,13 +17,16 @@ * limitations under the License. */ -package sema +package docstrings -import "github.com/onflow/cadence/runtime/ast" +import ( + "github.com/onflow/cadence/runtime/ast" + "github.com/onflow/cadence/runtime/sema" +) const DocstringsTypeOwoFieldName = "owo" -var DocstringsTypeOwoFieldType = IntType +var DocstringsTypeOwoFieldType = sema.IntType const DocstringsTypeOwoFieldDocString = ` This is a 1-line docstring. @@ -31,8 +34,8 @@ This is a 1-line docstring. const DocstringsTypeUwuFieldName = "uwu" -var DocstringsTypeUwuFieldType = &VariableSizedType{ - Type: IntType, +var DocstringsTypeUwuFieldType = &sema.VariableSizedType{ + Type: sema.IntType, } const DocstringsTypeUwuFieldDocString = ` @@ -42,16 +45,16 @@ This is the second line. const DocstringsTypeNwnFunctionName = "nwn" -var DocstringsTypeNwnFunctionType = &FunctionType{ - Parameters: []Parameter{ +var DocstringsTypeNwnFunctionType = &sema.FunctionType{ + Parameters: []sema.Parameter{ { Identifier: "x", - TypeAnnotation: NewTypeAnnotation(IntType), + TypeAnnotation: sema.NewTypeAnnotation(sema.IntType), }, }, - ReturnTypeAnnotation: NewTypeAnnotation( - &OptionalType{ - Type: StringType, + ReturnTypeAnnotation: sema.NewTypeAnnotation( + &sema.OptionalType{ + Type: sema.StringType, }, ), } @@ -64,7 +67,7 @@ And the third line! const DocstringsTypeWithBlanksFieldName = "withBlanks" -var DocstringsTypeWithBlanksFieldType = IntType +var DocstringsTypeWithBlanksFieldType = sema.IntType const DocstringsTypeWithBlanksFieldDocString = ` This is a multiline docstring. @@ -74,9 +77,9 @@ There should be two newlines before this line! const DocstringsTypeIsSmolBeanFunctionName = "isSmolBean" -var DocstringsTypeIsSmolBeanFunctionType = &FunctionType{ - ReturnTypeAnnotation: NewTypeAnnotation( - BoolType, +var DocstringsTypeIsSmolBeanFunctionType = &sema.FunctionType{ + ReturnTypeAnnotation: sema.NewTypeAnnotation( + sema.BoolType, ), } @@ -87,10 +90,10 @@ These should be handled accordingly. const DocstringsTypeRunningOutOfIdeasFunctionName = "runningOutOfIdeas" -var DocstringsTypeRunningOutOfIdeasFunctionType = &FunctionType{ - ReturnTypeAnnotation: NewTypeAnnotation( - &OptionalType{ - Type: UInt64Type, +var DocstringsTypeRunningOutOfIdeasFunctionType = &sema.FunctionType{ + ReturnTypeAnnotation: sema.NewTypeAnnotation( + &sema.OptionalType{ + Type: sema.UInt64Type, }, ), } @@ -103,11 +106,11 @@ Look, I did it ` + "`again`" + `, wowie!! const DocstringsTypeName = "Docstrings" -var DocstringsType = &SimpleType{ +var DocstringsType = &sema.SimpleType{ Name: DocstringsTypeName, QualifiedName: DocstringsTypeName, TypeID: DocstringsTypeName, - tag: DocstringsTypeTag, + TypeTag: DocstringsTypeTag, IsResource: false, Storable: false, Equatable: false, @@ -118,49 +121,49 @@ var DocstringsType = &SimpleType{ } func init() { - DocstringsType.Members = func(t *SimpleType) map[string]MemberResolver { - return MembersAsResolvers([]*Member{ - NewUnmeteredFieldMember( + DocstringsType.Members = func(t *sema.SimpleType) map[string]sema.MemberResolver { + return sema.MembersAsResolvers([]*sema.Member{ + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, DocstringsTypeOwoFieldName, DocstringsTypeOwoFieldType, DocstringsTypeOwoFieldDocString, ), - NewUnmeteredFieldMember( + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, DocstringsTypeUwuFieldName, DocstringsTypeUwuFieldType, DocstringsTypeUwuFieldDocString, ), - NewUnmeteredFunctionMember( + sema.NewUnmeteredFunctionMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), DocstringsTypeNwnFunctionName, DocstringsTypeNwnFunctionType, DocstringsTypeNwnFunctionDocString, ), - NewUnmeteredFieldMember( + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, DocstringsTypeWithBlanksFieldName, DocstringsTypeWithBlanksFieldType, DocstringsTypeWithBlanksFieldDocString, ), - NewUnmeteredFunctionMember( + sema.NewUnmeteredFunctionMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), DocstringsTypeIsSmolBeanFunctionName, DocstringsTypeIsSmolBeanFunctionType, DocstringsTypeIsSmolBeanFunctionDocString, ), - NewUnmeteredFunctionMember( + sema.NewUnmeteredFunctionMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), DocstringsTypeRunningOutOfIdeasFunctionName, DocstringsTypeRunningOutOfIdeasFunctionType, DocstringsTypeRunningOutOfIdeasFunctionDocString, diff --git a/runtime/sema/gen/testdata/entitlement.cdc b/runtime/sema/gen/testdata/entitlement/test.cdc similarity index 100% rename from runtime/sema/gen/testdata/entitlement.cdc rename to runtime/sema/gen/testdata/entitlement/test.cdc diff --git a/runtime/sema/gen/testdata/entitlement.golden.go b/runtime/sema/gen/testdata/entitlement/test.golden.go similarity index 58% rename from runtime/sema/gen/testdata/entitlement.golden.go rename to runtime/sema/gen/testdata/entitlement/test.golden.go index 0296d081f3..1d44250ceb 100644 --- a/runtime/sema/gen/testdata/entitlement.golden.go +++ b/runtime/sema/gen/testdata/entitlement/test.golden.go @@ -1,4 +1,4 @@ -// Code generated from testdata/entitlement.cdc. DO NOT EDIT. +// Code generated from testdata/entitlement/test.cdc. DO NOT EDIT. /* * Cadence - The resource-oriented smart contract programming language * @@ -17,32 +17,34 @@ * limitations under the License. */ -package sema +package entitlement -var FooType = &EntitlementType{ +import "github.com/onflow/cadence/runtime/sema" + +var FooType = &sema.EntitlementType{ Identifier: "Foo", } -var BarType = &EntitlementType{ +var BarType = &sema.EntitlementType{ Identifier: "Bar", } -var BazType = &EntitlementMapType{ +var BazType = &sema.EntitlementMapType{ Identifier: "Baz", IncludesIdentity: false, - Relations: []EntitlementRelation{ - EntitlementRelation{ + Relations: []sema.EntitlementRelation{ + sema.EntitlementRelation{ Input: FooType, Output: BarType, }, }, } -var QuxType = &EntitlementMapType{ +var QuxType = &sema.EntitlementMapType{ Identifier: "Qux", IncludesIdentity: true, - Relations: []EntitlementRelation{ - EntitlementRelation{ + Relations: []sema.EntitlementRelation{ + sema.EntitlementRelation{ Input: FooType, Output: BarType, }, @@ -50,12 +52,8 @@ var QuxType = &EntitlementMapType{ } func init() { - BuiltinEntitlementMappings[BazType.Identifier] = BazType - addToBaseActivation(BazType) - BuiltinEntitlementMappings[QuxType.Identifier] = QuxType - addToBaseActivation(QuxType) - BuiltinEntitlements[FooType.Identifier] = FooType - addToBaseActivation(FooType) - BuiltinEntitlements[BarType.Identifier] = BarType - addToBaseActivation(BarType) + sema.BuiltinEntitlementMappings[BazType.Identifier] = BazType + sema.BuiltinEntitlementMappings[QuxType.Identifier] = QuxType + sema.BuiltinEntitlements[FooType.Identifier] = FooType + sema.BuiltinEntitlements[BarType.Identifier] = BarType } diff --git a/runtime/sema/gen/testdata/equatable/helper.go b/runtime/sema/gen/testdata/equatable/helper.go new file mode 100644 index 0000000000..1fe9bc6a2a --- /dev/null +++ b/runtime/sema/gen/testdata/equatable/helper.go @@ -0,0 +1,23 @@ +/* + * 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 equatable + +import "github.com/onflow/cadence/runtime/sema" + +var TestTypeTag sema.TypeTag diff --git a/runtime/sema/gen/testdata/equatable.cdc b/runtime/sema/gen/testdata/equatable/test.cdc similarity index 100% rename from runtime/sema/gen/testdata/equatable.cdc rename to runtime/sema/gen/testdata/equatable/test.cdc diff --git a/runtime/sema/gen/testdata/equatable.golden.go b/runtime/sema/gen/testdata/equatable/test.golden.go similarity index 83% rename from runtime/sema/gen/testdata/equatable.golden.go rename to runtime/sema/gen/testdata/equatable/test.golden.go index 82320957ee..e102e7e6de 100644 --- a/runtime/sema/gen/testdata/equatable.golden.go +++ b/runtime/sema/gen/testdata/equatable/test.golden.go @@ -1,4 +1,4 @@ -// Code generated from testdata/equatable.cdc. DO NOT EDIT. +// Code generated from testdata/equatable/test.cdc. DO NOT EDIT. /* * Cadence - The resource-oriented smart contract programming language * @@ -17,15 +17,17 @@ * limitations under the License. */ -package sema +package equatable + +import "github.com/onflow/cadence/runtime/sema" const TestTypeName = "Test" -var TestType = &SimpleType{ +var TestType = &sema.SimpleType{ Name: TestTypeName, QualifiedName: TestTypeName, TypeID: TestTypeName, - tag: TestTypeTag, + TypeTag: TestTypeTag, IsResource: false, Storable: false, Equatable: true, diff --git a/runtime/sema/gen/testdata/exportable/helper.go b/runtime/sema/gen/testdata/exportable/helper.go new file mode 100644 index 0000000000..92baaf371c --- /dev/null +++ b/runtime/sema/gen/testdata/exportable/helper.go @@ -0,0 +1,23 @@ +/* + * 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 exportable + +import "github.com/onflow/cadence/runtime/sema" + +var TestTypeTag sema.TypeTag diff --git a/runtime/sema/gen/testdata/exportable.cdc b/runtime/sema/gen/testdata/exportable/test.cdc similarity index 100% rename from runtime/sema/gen/testdata/exportable.cdc rename to runtime/sema/gen/testdata/exportable/test.cdc diff --git a/runtime/sema/gen/testdata/exportable.golden.go b/runtime/sema/gen/testdata/exportable/test.golden.go similarity index 82% rename from runtime/sema/gen/testdata/exportable.golden.go rename to runtime/sema/gen/testdata/exportable/test.golden.go index db6afd0593..3124209d4a 100644 --- a/runtime/sema/gen/testdata/exportable.golden.go +++ b/runtime/sema/gen/testdata/exportable/test.golden.go @@ -1,4 +1,4 @@ -// Code generated from testdata/exportable.cdc. DO NOT EDIT. +// Code generated from testdata/exportable/test.cdc. DO NOT EDIT. /* * Cadence - The resource-oriented smart contract programming language * @@ -17,15 +17,17 @@ * limitations under the License. */ -package sema +package exportable + +import "github.com/onflow/cadence/runtime/sema" const TestTypeName = "Test" -var TestType = &SimpleType{ +var TestType = &sema.SimpleType{ Name: TestTypeName, QualifiedName: TestTypeName, TypeID: TestTypeName, - tag: TestTypeTag, + TypeTag: TestTypeTag, IsResource: false, Storable: false, Equatable: false, diff --git a/runtime/sema/gen/testdata/fields/helper.go b/runtime/sema/gen/testdata/fields/helper.go new file mode 100644 index 0000000000..0fbaa3f0f8 --- /dev/null +++ b/runtime/sema/gen/testdata/fields/helper.go @@ -0,0 +1,26 @@ +/* + * 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 fields + +import "github.com/onflow/cadence/runtime/sema" + +var TestTypeTag sema.TypeTag +var FooType = &sema.CapabilityType{} +var BarType *sema.InterfaceType +var BazType *sema.InterfaceType diff --git a/runtime/sema/gen/testdata/fields.cdc b/runtime/sema/gen/testdata/fields/test.cdc similarity index 99% rename from runtime/sema/gen/testdata/fields.cdc rename to runtime/sema/gen/testdata/fields/test.cdc index 1cf74e4f96..2f5338d1e9 100644 --- a/runtime/sema/gen/testdata/fields.cdc +++ b/runtime/sema/gen/testdata/fields/test.cdc @@ -1,3 +1,4 @@ + access(all) struct Test { /// This is a test integer. access(all) let testInt: UInt64 diff --git a/runtime/sema/gen/testdata/fields.golden.go b/runtime/sema/gen/testdata/fields/test.golden.go similarity index 67% rename from runtime/sema/gen/testdata/fields.golden.go rename to runtime/sema/gen/testdata/fields/test.golden.go index f4af42f973..950c618e93 100644 --- a/runtime/sema/gen/testdata/fields.golden.go +++ b/runtime/sema/gen/testdata/fields/test.golden.go @@ -1,4 +1,4 @@ -// Code generated from testdata/fields.cdc. DO NOT EDIT. +// Code generated from testdata/fields/test.cdc. DO NOT EDIT. /* * Cadence - The resource-oriented smart contract programming language * @@ -17,13 +17,16 @@ * limitations under the License. */ -package sema +package fields -import "github.com/onflow/cadence/runtime/ast" +import ( + "github.com/onflow/cadence/runtime/ast" + "github.com/onflow/cadence/runtime/sema" +) const TestTypeTestIntFieldName = "testInt" -var TestTypeTestIntFieldType = UInt64Type +var TestTypeTestIntFieldType = sema.UInt64Type const TestTypeTestIntFieldDocString = ` This is a test integer. @@ -31,8 +34,8 @@ This is a test integer. const TestTypeTestOptIntFieldName = "testOptInt" -var TestTypeTestOptIntFieldType = &OptionalType{ - Type: UInt64Type, +var TestTypeTestOptIntFieldType = &sema.OptionalType{ + Type: sema.UInt64Type, } const TestTypeTestOptIntFieldDocString = ` @@ -41,9 +44,9 @@ This is a test optional integer. const TestTypeTestRefIntFieldName = "testRefInt" -var TestTypeTestRefIntFieldType = &ReferenceType{ - Type: UInt64Type, - Authorization: UnauthorizedAccess, +var TestTypeTestRefIntFieldType = &sema.ReferenceType{ + Type: sema.UInt64Type, + Authorization: sema.UnauthorizedAccess, } const TestTypeTestRefIntFieldDocString = ` @@ -52,8 +55,8 @@ This is a test integer reference. const TestTypeTestVarIntsFieldName = "testVarInts" -var TestTypeTestVarIntsFieldType = &VariableSizedType{ - Type: UInt64Type, +var TestTypeTestVarIntsFieldType = &sema.VariableSizedType{ + Type: sema.UInt64Type, } const TestTypeTestVarIntsFieldDocString = ` @@ -62,8 +65,8 @@ This is a test variable-sized integer array. const TestTypeTestConstIntsFieldName = "testConstInts" -var TestTypeTestConstIntsFieldType = &ConstantSizedType{ - Type: UInt64Type, +var TestTypeTestConstIntsFieldType = &sema.ConstantSizedType{ + Type: sema.UInt64Type, Size: 2, } @@ -73,9 +76,9 @@ This is a test constant-sized integer array. const TestTypeTestIntDictFieldName = "testIntDict" -var TestTypeTestIntDictFieldType = &DictionaryType{ - KeyType: UInt64Type, - ValueType: BoolType, +var TestTypeTestIntDictFieldType = &sema.DictionaryType{ + KeyType: sema.UInt64Type, + ValueType: sema.BoolType, } const TestTypeTestIntDictFieldDocString = ` @@ -84,7 +87,7 @@ This is a test integer dictionary. const TestTypeTestParamFieldName = "testParam" -var TestTypeTestParamFieldType = MustInstantiate( +var TestTypeTestParamFieldType = sema.MustInstantiate( FooType, BarType, ) @@ -95,7 +98,7 @@ This is a test parameterized-type field. const TestTypeTestAddressFieldName = "testAddress" -var TestTypeTestAddressFieldType = TheAddressType +var TestTypeTestAddressFieldType = sema.TheAddressType const TestTypeTestAddressFieldDocString = ` This is a test address field. @@ -103,7 +106,7 @@ This is a test address field. const TestTypeTestTypeFieldName = "testType" -var TestTypeTestTypeFieldType = MetaType +var TestTypeTestTypeFieldType = sema.MetaType const TestTypeTestTypeFieldDocString = ` This is a test type field. @@ -111,7 +114,7 @@ This is a test type field. const TestTypeTestCapFieldName = "testCap" -var TestTypeTestCapFieldType = &CapabilityType{} +var TestTypeTestCapFieldType = &sema.CapabilityType{} const TestTypeTestCapFieldDocString = ` This is a test unparameterized capability field. @@ -119,9 +122,9 @@ This is a test unparameterized capability field. const TestTypeTestCapIntFieldName = "testCapInt" -var TestTypeTestCapIntFieldType = MustInstantiate( - &CapabilityType{}, - IntType, +var TestTypeTestCapIntFieldType = sema.MustInstantiate( + &sema.CapabilityType{}, + sema.IntType, ) const TestTypeTestCapIntFieldDocString = ` @@ -130,8 +133,8 @@ This is a test parameterized capability field. const TestTypeTestIntersectionWithoutTypeFieldName = "testIntersectionWithoutType" -var TestTypeTestIntersectionWithoutTypeFieldType = &IntersectionType{ - Types: []*InterfaceType{BarType, BazType}, +var TestTypeTestIntersectionWithoutTypeFieldType = &sema.IntersectionType{ + Types: []*sema.InterfaceType{BarType, BazType}, } const TestTypeTestIntersectionWithoutTypeFieldDocString = ` @@ -140,11 +143,11 @@ This is a test intersection type (without type) field. const TestTypeName = "Test" -var TestType = &SimpleType{ +var TestType = &sema.SimpleType{ Name: TestTypeName, QualifiedName: TestTypeName, TypeID: TestTypeName, - tag: TestTypeTag, + TypeTag: TestTypeTag, IsResource: false, Storable: false, Equatable: false, @@ -155,99 +158,99 @@ var TestType = &SimpleType{ } func init() { - TestType.Members = func(t *SimpleType) map[string]MemberResolver { - return MembersAsResolvers([]*Member{ - NewUnmeteredFieldMember( + TestType.Members = func(t *sema.SimpleType) map[string]sema.MemberResolver { + return sema.MembersAsResolvers([]*sema.Member{ + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, TestTypeTestIntFieldName, TestTypeTestIntFieldType, TestTypeTestIntFieldDocString, ), - NewUnmeteredFieldMember( + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, TestTypeTestOptIntFieldName, TestTypeTestOptIntFieldType, TestTypeTestOptIntFieldDocString, ), - NewUnmeteredFieldMember( + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, TestTypeTestRefIntFieldName, TestTypeTestRefIntFieldType, TestTypeTestRefIntFieldDocString, ), - NewUnmeteredFieldMember( + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, TestTypeTestVarIntsFieldName, TestTypeTestVarIntsFieldType, TestTypeTestVarIntsFieldDocString, ), - NewUnmeteredFieldMember( + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, TestTypeTestConstIntsFieldName, TestTypeTestConstIntsFieldType, TestTypeTestConstIntsFieldDocString, ), - NewUnmeteredFieldMember( + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, TestTypeTestIntDictFieldName, TestTypeTestIntDictFieldType, TestTypeTestIntDictFieldDocString, ), - NewUnmeteredFieldMember( + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, TestTypeTestParamFieldName, TestTypeTestParamFieldType, TestTypeTestParamFieldDocString, ), - NewUnmeteredFieldMember( + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, TestTypeTestAddressFieldName, TestTypeTestAddressFieldType, TestTypeTestAddressFieldDocString, ), - NewUnmeteredFieldMember( + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, TestTypeTestTypeFieldName, TestTypeTestTypeFieldType, TestTypeTestTypeFieldDocString, ), - NewUnmeteredFieldMember( + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, TestTypeTestCapFieldName, TestTypeTestCapFieldType, TestTypeTestCapFieldDocString, ), - NewUnmeteredFieldMember( + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, TestTypeTestCapIntFieldName, TestTypeTestCapIntFieldType, TestTypeTestCapIntFieldDocString, ), - NewUnmeteredFieldMember( + sema.NewUnmeteredFieldMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, TestTypeTestIntersectionWithoutTypeFieldName, TestTypeTestIntersectionWithoutTypeFieldType, diff --git a/runtime/sema/gen/testdata/functions/helper.go b/runtime/sema/gen/testdata/functions/helper.go new file mode 100644 index 0000000000..eb10cbad92 --- /dev/null +++ b/runtime/sema/gen/testdata/functions/helper.go @@ -0,0 +1,23 @@ +/* + * 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 functions + +import "github.com/onflow/cadence/runtime/sema" + +var TestTypeTag sema.TypeTag diff --git a/runtime/sema/gen/testdata/functions.cdc b/runtime/sema/gen/testdata/functions/test.cdc similarity index 100% rename from runtime/sema/gen/testdata/functions.cdc rename to runtime/sema/gen/testdata/functions/test.cdc diff --git a/runtime/sema/gen/testdata/functions.golden.go b/runtime/sema/gen/testdata/functions/test.golden.go similarity index 57% rename from runtime/sema/gen/testdata/functions.golden.go rename to runtime/sema/gen/testdata/functions/test.golden.go index 41226f8238..2c22638b28 100644 --- a/runtime/sema/gen/testdata/functions.golden.go +++ b/runtime/sema/gen/testdata/functions/test.golden.go @@ -1,4 +1,4 @@ -// Code generated from testdata/functions.cdc. DO NOT EDIT. +// Code generated from testdata/functions/test.cdc. DO NOT EDIT. /* * Cadence - The resource-oriented smart contract programming language * @@ -17,15 +17,18 @@ * limitations under the License. */ -package sema +package functions -import "github.com/onflow/cadence/runtime/ast" +import ( + "github.com/onflow/cadence/runtime/ast" + "github.com/onflow/cadence/runtime/sema" +) const TestTypeNothingFunctionName = "nothing" -var TestTypeNothingFunctionType = &FunctionType{ - ReturnTypeAnnotation: NewTypeAnnotation( - VoidType, +var TestTypeNothingFunctionType = &sema.FunctionType{ + ReturnTypeAnnotation: sema.NewTypeAnnotation( + sema.VoidType, ), } @@ -35,20 +38,20 @@ This is a test function. const TestTypeParamsFunctionName = "params" -var TestTypeParamsFunctionType = &FunctionType{ - Parameters: []Parameter{ +var TestTypeParamsFunctionType = &sema.FunctionType{ + Parameters: []sema.Parameter{ { Identifier: "a", - TypeAnnotation: NewTypeAnnotation(IntType), + TypeAnnotation: sema.NewTypeAnnotation(sema.IntType), }, { - Label: ArgumentLabelNotRequired, + Label: sema.ArgumentLabelNotRequired, Identifier: "b", - TypeAnnotation: NewTypeAnnotation(StringType), + TypeAnnotation: sema.NewTypeAnnotation(sema.StringType), }, }, - ReturnTypeAnnotation: NewTypeAnnotation( - VoidType, + ReturnTypeAnnotation: sema.NewTypeAnnotation( + sema.VoidType, ), } @@ -58,9 +61,9 @@ This is a test function with parameters. const TestTypeReturnBoolFunctionName = "returnBool" -var TestTypeReturnBoolFunctionType = &FunctionType{ - ReturnTypeAnnotation: NewTypeAnnotation( - BoolType, +var TestTypeReturnBoolFunctionType = &sema.FunctionType{ + ReturnTypeAnnotation: sema.NewTypeAnnotation( + sema.BoolType, ), } @@ -70,20 +73,20 @@ This is a test function with a return type. const TestTypeParamsAndReturnFunctionName = "paramsAndReturn" -var TestTypeParamsAndReturnFunctionType = &FunctionType{ - Parameters: []Parameter{ +var TestTypeParamsAndReturnFunctionType = &sema.FunctionType{ + Parameters: []sema.Parameter{ { Identifier: "a", - TypeAnnotation: NewTypeAnnotation(IntType), + TypeAnnotation: sema.NewTypeAnnotation(sema.IntType), }, { - Label: ArgumentLabelNotRequired, + Label: sema.ArgumentLabelNotRequired, Identifier: "b", - TypeAnnotation: NewTypeAnnotation(StringType), + TypeAnnotation: sema.NewTypeAnnotation(sema.StringType), }, }, - ReturnTypeAnnotation: NewTypeAnnotation( - BoolType, + ReturnTypeAnnotation: sema.NewTypeAnnotation( + sema.BoolType, ), } @@ -93,16 +96,16 @@ This is a test function with parameters and a return type. const TestTypeTypeParamFunctionName = "typeParam" -var TestTypeTypeParamFunctionTypeParameterT = &TypeParameter{ +var TestTypeTypeParamFunctionTypeParameterT = &sema.TypeParameter{ Name: "T", } -var TestTypeTypeParamFunctionType = &FunctionType{ - TypeParameters: []*TypeParameter{ +var TestTypeTypeParamFunctionType = &sema.FunctionType{ + TypeParameters: []*sema.TypeParameter{ TestTypeTypeParamFunctionTypeParameterT, }, - ReturnTypeAnnotation: NewTypeAnnotation( - VoidType, + ReturnTypeAnnotation: sema.NewTypeAnnotation( + sema.VoidType, ), } @@ -112,20 +115,20 @@ This is a test function with a type parameter. const TestTypeTypeParamWithBoundFunctionName = "typeParamWithBound" -var TestTypeTypeParamWithBoundFunctionTypeParameterT = &TypeParameter{ +var TestTypeTypeParamWithBoundFunctionTypeParameterT = &sema.TypeParameter{ Name: "T", - TypeBound: &ReferenceType{ - Type: AnyType, - Authorization: UnauthorizedAccess, + TypeBound: &sema.ReferenceType{ + Type: sema.AnyType, + Authorization: sema.UnauthorizedAccess, }, } -var TestTypeTypeParamWithBoundFunctionType = &FunctionType{ - TypeParameters: []*TypeParameter{ +var TestTypeTypeParamWithBoundFunctionType = &sema.FunctionType{ + TypeParameters: []*sema.TypeParameter{ TestTypeTypeParamWithBoundFunctionTypeParameterT, }, - ReturnTypeAnnotation: NewTypeAnnotation( - VoidType, + ReturnTypeAnnotation: sema.NewTypeAnnotation( + sema.VoidType, ), } @@ -135,24 +138,24 @@ This is a test function with a type parameter and a type bound. const TestTypeTypeParamWithBoundAndParamFunctionName = "typeParamWithBoundAndParam" -var TestTypeTypeParamWithBoundAndParamFunctionTypeParameterT = &TypeParameter{ +var TestTypeTypeParamWithBoundAndParamFunctionTypeParameterT = &sema.TypeParameter{ Name: "T", } -var TestTypeTypeParamWithBoundAndParamFunctionType = &FunctionType{ - TypeParameters: []*TypeParameter{ +var TestTypeTypeParamWithBoundAndParamFunctionType = &sema.FunctionType{ + TypeParameters: []*sema.TypeParameter{ TestTypeTypeParamWithBoundAndParamFunctionTypeParameterT, }, - Parameters: []Parameter{ + Parameters: []sema.Parameter{ { Identifier: "t", - TypeAnnotation: NewTypeAnnotation(&GenericType{ + TypeAnnotation: sema.NewTypeAnnotation(&sema.GenericType{ TypeParameter: TestTypeTypeParamWithBoundAndParamFunctionTypeParameterT, }), }, }, - ReturnTypeAnnotation: NewTypeAnnotation( - VoidType, + ReturnTypeAnnotation: sema.NewTypeAnnotation( + sema.VoidType, ), } @@ -162,10 +165,10 @@ This is a test function with a type parameter and a parameter using it. const TestTypeViewFunctionFunctionName = "viewFunction" -var TestTypeViewFunctionFunctionType = &FunctionType{ - Purity: FunctionPurityView, - ReturnTypeAnnotation: NewTypeAnnotation( - VoidType, +var TestTypeViewFunctionFunctionType = &sema.FunctionType{ + Purity: sema.FunctionPurityView, + ReturnTypeAnnotation: sema.NewTypeAnnotation( + sema.VoidType, ), } @@ -175,11 +178,11 @@ This is a function with 'view' modifier const TestTypeName = "Test" -var TestType = &SimpleType{ +var TestType = &sema.SimpleType{ Name: TestTypeName, QualifiedName: TestTypeName, TypeID: TestTypeName, - tag: TestTypeTag, + TypeTag: TestTypeTag, IsResource: false, Storable: false, Equatable: false, @@ -190,60 +193,60 @@ var TestType = &SimpleType{ } func init() { - TestType.Members = func(t *SimpleType) map[string]MemberResolver { - return MembersAsResolvers([]*Member{ - NewUnmeteredFunctionMember( + TestType.Members = func(t *sema.SimpleType) map[string]sema.MemberResolver { + return sema.MembersAsResolvers([]*sema.Member{ + sema.NewUnmeteredFunctionMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), TestTypeNothingFunctionName, TestTypeNothingFunctionType, TestTypeNothingFunctionDocString, ), - NewUnmeteredFunctionMember( + sema.NewUnmeteredFunctionMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), TestTypeParamsFunctionName, TestTypeParamsFunctionType, TestTypeParamsFunctionDocString, ), - NewUnmeteredFunctionMember( + sema.NewUnmeteredFunctionMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), TestTypeReturnBoolFunctionName, TestTypeReturnBoolFunctionType, TestTypeReturnBoolFunctionDocString, ), - NewUnmeteredFunctionMember( + sema.NewUnmeteredFunctionMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), TestTypeParamsAndReturnFunctionName, TestTypeParamsAndReturnFunctionType, TestTypeParamsAndReturnFunctionDocString, ), - NewUnmeteredFunctionMember( + sema.NewUnmeteredFunctionMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), TestTypeTypeParamFunctionName, TestTypeTypeParamFunctionType, TestTypeTypeParamFunctionDocString, ), - NewUnmeteredFunctionMember( + sema.NewUnmeteredFunctionMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), TestTypeTypeParamWithBoundFunctionName, TestTypeTypeParamWithBoundFunctionType, TestTypeTypeParamWithBoundFunctionDocString, ), - NewUnmeteredFunctionMember( + sema.NewUnmeteredFunctionMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), TestTypeTypeParamWithBoundAndParamFunctionName, TestTypeTypeParamWithBoundAndParamFunctionType, TestTypeTypeParamWithBoundAndParamFunctionDocString, ), - NewUnmeteredFunctionMember( + sema.NewUnmeteredFunctionMember( t, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), TestTypeViewFunctionFunctionName, TestTypeViewFunctionFunctionType, TestTypeViewFunctionFunctionDocString, diff --git a/runtime/sema/gen/testdata/importable/helper.go b/runtime/sema/gen/testdata/importable/helper.go new file mode 100644 index 0000000000..6a02e2c7c5 --- /dev/null +++ b/runtime/sema/gen/testdata/importable/helper.go @@ -0,0 +1,23 @@ +/* + * 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 importable + +import "github.com/onflow/cadence/runtime/sema" + +var TestTypeTag sema.TypeTag diff --git a/runtime/sema/gen/testdata/importable.cdc b/runtime/sema/gen/testdata/importable/test.cdc similarity index 100% rename from runtime/sema/gen/testdata/importable.cdc rename to runtime/sema/gen/testdata/importable/test.cdc diff --git a/runtime/sema/gen/testdata/importable.golden.go b/runtime/sema/gen/testdata/importable/test.golden.go similarity index 82% rename from runtime/sema/gen/testdata/importable.golden.go rename to runtime/sema/gen/testdata/importable/test.golden.go index 39496013d4..243be9b153 100644 --- a/runtime/sema/gen/testdata/importable.golden.go +++ b/runtime/sema/gen/testdata/importable/test.golden.go @@ -1,4 +1,4 @@ -// Code generated from testdata/importable.cdc. DO NOT EDIT. +// Code generated from testdata/importable/test.cdc. DO NOT EDIT. /* * Cadence - The resource-oriented smart contract programming language * @@ -17,15 +17,17 @@ * limitations under the License. */ -package sema +package importable + +import "github.com/onflow/cadence/runtime/sema" const TestTypeName = "Test" -var TestType = &SimpleType{ +var TestType = &sema.SimpleType{ Name: TestTypeName, QualifiedName: TestTypeName, TypeID: TestTypeName, - tag: TestTypeTag, + TypeTag: TestTypeTag, IsResource: false, Storable: false, Equatable: false, diff --git a/runtime/sema/gen/testdata/member_accessible/helper.go b/runtime/sema/gen/testdata/member_accessible/helper.go new file mode 100644 index 0000000000..2632d04b5c --- /dev/null +++ b/runtime/sema/gen/testdata/member_accessible/helper.go @@ -0,0 +1,23 @@ +/* + * 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 member_accessible + +import "github.com/onflow/cadence/runtime/sema" + +var TestTypeTag sema.TypeTag diff --git a/runtime/sema/gen/testdata/member_accessible.cdc b/runtime/sema/gen/testdata/member_accessible/test.cdc similarity index 100% rename from runtime/sema/gen/testdata/member_accessible.cdc rename to runtime/sema/gen/testdata/member_accessible/test.cdc diff --git a/runtime/sema/gen/testdata/member_accessible.golden.go b/runtime/sema/gen/testdata/member_accessible/test.golden.go similarity index 81% rename from runtime/sema/gen/testdata/member_accessible.golden.go rename to runtime/sema/gen/testdata/member_accessible/test.golden.go index d3553ebafd..ec49013df0 100644 --- a/runtime/sema/gen/testdata/member_accessible.golden.go +++ b/runtime/sema/gen/testdata/member_accessible/test.golden.go @@ -1,4 +1,4 @@ -// Code generated from testdata/member_accessible.cdc. DO NOT EDIT. +// Code generated from testdata/member_accessible/test.cdc. DO NOT EDIT. /* * Cadence - The resource-oriented smart contract programming language * @@ -17,15 +17,17 @@ * limitations under the License. */ -package sema +package member_accessible + +import "github.com/onflow/cadence/runtime/sema" const TestTypeName = "Test" -var TestType = &SimpleType{ +var TestType = &sema.SimpleType{ Name: TestTypeName, QualifiedName: TestTypeName, TypeID: TestTypeName, - tag: TestTypeTag, + TypeTag: TestTypeTag, IsResource: false, Storable: false, Equatable: false, diff --git a/runtime/sema/gen/testdata/nested.cdc b/runtime/sema/gen/testdata/nested/test.cdc similarity index 100% rename from runtime/sema/gen/testdata/nested.cdc rename to runtime/sema/gen/testdata/nested/test.cdc diff --git a/runtime/sema/gen/testdata/nested.golden.go b/runtime/sema/gen/testdata/nested/test.golden.go similarity index 65% rename from runtime/sema/gen/testdata/nested.golden.go rename to runtime/sema/gen/testdata/nested/test.golden.go index 73c94c99c1..d5a1058aa0 100644 --- a/runtime/sema/gen/testdata/nested.golden.go +++ b/runtime/sema/gen/testdata/nested/test.golden.go @@ -1,4 +1,4 @@ -// Code generated from testdata/nested.cdc. DO NOT EDIT. +// Code generated from testdata/nested/test.cdc. DO NOT EDIT. /* * Cadence - The resource-oriented smart contract programming language * @@ -17,18 +17,19 @@ * limitations under the License. */ -package sema +package nested import ( "github.com/onflow/cadence/runtime/ast" "github.com/onflow/cadence/runtime/common" + "github.com/onflow/cadence/runtime/sema" ) const FooTypeFooFunctionName = "foo" -var FooTypeFooFunctionType = &FunctionType{ - ReturnTypeAnnotation: NewTypeAnnotation( - VoidType, +var FooTypeFooFunctionType = &sema.FunctionType{ + ReturnTypeAnnotation: sema.NewTypeAnnotation( + sema.VoidType, ), } @@ -46,9 +47,9 @@ Bar const Foo_BarTypeBarFunctionName = "bar" -var Foo_BarTypeBarFunctionType = &FunctionType{ - ReturnTypeAnnotation: NewTypeAnnotation( - VoidType, +var Foo_BarTypeBarFunctionType = &sema.FunctionType{ + ReturnTypeAnnotation: sema.NewTypeAnnotation( + sema.VoidType, ), } @@ -58,8 +59,8 @@ bar const Foo_BarTypeName = "Bar" -var Foo_BarType = func() *CompositeType { - var t = &CompositeType{ +var Foo_BarType = func() *sema.CompositeType { + var t = &sema.CompositeType{ Identifier: Foo_BarTypeName, Kind: common.CompositeKindStructure, ImportableBuiltin: false, @@ -70,24 +71,24 @@ var Foo_BarType = func() *CompositeType { }() func init() { - var members = []*Member{ - NewUnmeteredFunctionMember( + var members = []*sema.Member{ + sema.NewUnmeteredFunctionMember( Foo_BarType, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), Foo_BarTypeBarFunctionName, Foo_BarTypeBarFunctionType, Foo_BarTypeBarFunctionDocString, ), } - Foo_BarType.Members = MembersAsMap(members) - Foo_BarType.Fields = MembersFieldNames(members) + Foo_BarType.Members = sema.MembersAsMap(members) + Foo_BarType.Fields = sema.MembersFieldNames(members) } const FooTypeName = "Foo" -var FooType = func() *CompositeType { - var t = &CompositeType{ +var FooType = func() *sema.CompositeType { + var t = &sema.CompositeType{ Identifier: FooTypeName, Kind: common.CompositeKindStructure, ImportableBuiltin: false, @@ -99,17 +100,17 @@ var FooType = func() *CompositeType { }() func init() { - var members = []*Member{ - NewUnmeteredFunctionMember( + var members = []*sema.Member{ + sema.NewUnmeteredFunctionMember( FooType, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), FooTypeFooFunctionName, FooTypeFooFunctionType, FooTypeFooFunctionDocString, ), - NewUnmeteredFieldMember( + sema.NewUnmeteredFieldMember( FooType, - PrimitiveAccess(ast.AccessAll), + sema.PrimitiveAccess(ast.AccessAll), ast.VariableKindConstant, FooTypeBarFieldName, FooTypeBarFieldType, @@ -117,6 +118,6 @@ func init() { ), } - FooType.Members = MembersAsMap(members) - FooType.Fields = MembersFieldNames(members) + FooType.Members = sema.MembersAsMap(members) + FooType.Fields = sema.MembersFieldNames(members) } diff --git a/runtime/sema/gen/testdata/simple_resource/helper.go b/runtime/sema/gen/testdata/simple_resource/helper.go new file mode 100644 index 0000000000..16de616169 --- /dev/null +++ b/runtime/sema/gen/testdata/simple_resource/helper.go @@ -0,0 +1,23 @@ +/* + * 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 simple_resource + +import "github.com/onflow/cadence/runtime/sema" + +var TestTypeTag sema.TypeTag diff --git a/runtime/sema/gen/testdata/simple-resource.cdc b/runtime/sema/gen/testdata/simple_resource/test.cdc similarity index 100% rename from runtime/sema/gen/testdata/simple-resource.cdc rename to runtime/sema/gen/testdata/simple_resource/test.cdc diff --git a/runtime/sema/gen/testdata/simple-resource.golden.go b/runtime/sema/gen/testdata/simple_resource/test.golden.go similarity index 82% rename from runtime/sema/gen/testdata/simple-resource.golden.go rename to runtime/sema/gen/testdata/simple_resource/test.golden.go index 53ee6c24f7..a6876b92ce 100644 --- a/runtime/sema/gen/testdata/simple-resource.golden.go +++ b/runtime/sema/gen/testdata/simple_resource/test.golden.go @@ -1,4 +1,4 @@ -// Code generated from testdata/simple-resource.cdc. DO NOT EDIT. +// Code generated from testdata/simple_resource/test.cdc. DO NOT EDIT. /* * Cadence - The resource-oriented smart contract programming language * @@ -17,15 +17,17 @@ * limitations under the License. */ -package sema +package simple_resource + +import "github.com/onflow/cadence/runtime/sema" const TestTypeName = "Test" -var TestType = &SimpleType{ +var TestType = &sema.SimpleType{ Name: TestTypeName, QualifiedName: TestTypeName, TypeID: TestTypeName, - tag: TestTypeTag, + TypeTag: TestTypeTag, IsResource: true, Storable: false, Equatable: false, diff --git a/runtime/sema/gen/testdata/simple_struct/helper.go b/runtime/sema/gen/testdata/simple_struct/helper.go new file mode 100644 index 0000000000..b9cf6dd4da --- /dev/null +++ b/runtime/sema/gen/testdata/simple_struct/helper.go @@ -0,0 +1,23 @@ +/* + * 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 simple_struct + +import "github.com/onflow/cadence/runtime/sema" + +var TestTypeTag sema.TypeTag diff --git a/runtime/sema/gen/testdata/simple-struct.cdc b/runtime/sema/gen/testdata/simple_struct/test.cdc similarity index 100% rename from runtime/sema/gen/testdata/simple-struct.cdc rename to runtime/sema/gen/testdata/simple_struct/test.cdc diff --git a/runtime/sema/gen/testdata/simple-struct.golden.go b/runtime/sema/gen/testdata/simple_struct/test.golden.go similarity index 82% rename from runtime/sema/gen/testdata/simple-struct.golden.go rename to runtime/sema/gen/testdata/simple_struct/test.golden.go index 0429d008f3..4606268f1d 100644 --- a/runtime/sema/gen/testdata/simple-struct.golden.go +++ b/runtime/sema/gen/testdata/simple_struct/test.golden.go @@ -1,4 +1,4 @@ -// Code generated from testdata/simple-struct.cdc. DO NOT EDIT. +// Code generated from testdata/simple_struct/test.cdc. DO NOT EDIT. /* * Cadence - The resource-oriented smart contract programming language * @@ -17,15 +17,17 @@ * limitations under the License. */ -package sema +package simple_struct + +import "github.com/onflow/cadence/runtime/sema" const TestTypeName = "Test" -var TestType = &SimpleType{ +var TestType = &sema.SimpleType{ Name: TestTypeName, QualifiedName: TestTypeName, TypeID: TestTypeName, - tag: TestTypeTag, + TypeTag: TestTypeTag, IsResource: false, Storable: false, Equatable: false, diff --git a/runtime/sema/gen/testdata/storable/helper.go b/runtime/sema/gen/testdata/storable/helper.go new file mode 100644 index 0000000000..cd8334e96c --- /dev/null +++ b/runtime/sema/gen/testdata/storable/helper.go @@ -0,0 +1,23 @@ +/* + * 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 storable + +import "github.com/onflow/cadence/runtime/sema" + +var TestTypeTag sema.TypeTag diff --git a/runtime/sema/gen/testdata/storable.cdc b/runtime/sema/gen/testdata/storable/test.cdc similarity index 100% rename from runtime/sema/gen/testdata/storable.cdc rename to runtime/sema/gen/testdata/storable/test.cdc diff --git a/runtime/sema/gen/testdata/storable.golden.go b/runtime/sema/gen/testdata/storable/test.golden.go similarity index 83% rename from runtime/sema/gen/testdata/storable.golden.go rename to runtime/sema/gen/testdata/storable/test.golden.go index c9c5526991..5a01604df9 100644 --- a/runtime/sema/gen/testdata/storable.golden.go +++ b/runtime/sema/gen/testdata/storable/test.golden.go @@ -1,4 +1,4 @@ -// Code generated from testdata/storable.cdc. DO NOT EDIT. +// Code generated from testdata/storable/test.cdc. DO NOT EDIT. /* * Cadence - The resource-oriented smart contract programming language * @@ -17,15 +17,17 @@ * limitations under the License. */ -package sema +package storable + +import "github.com/onflow/cadence/runtime/sema" const TestTypeName = "Test" -var TestType = &SimpleType{ +var TestType = &sema.SimpleType{ Name: TestTypeName, QualifiedName: TestTypeName, TypeID: TestTypeName, - tag: TestTypeTag, + TypeTag: TestTypeTag, IsResource: false, Storable: true, Equatable: false, diff --git a/runtime/sema/invalid_type.go b/runtime/sema/invalid_type.go index d0ce0787ec..409d50f5ab 100644 --- a/runtime/sema/invalid_type.go +++ b/runtime/sema/invalid_type.go @@ -25,7 +25,7 @@ var InvalidType = &SimpleType{ Name: "<>", QualifiedName: "<>", TypeID: "<>", - tag: InvalidTypeTag, + TypeTag: InvalidTypeTag, IsResource: false, Storable: false, Equatable: false, diff --git a/runtime/sema/meta_type.go b/runtime/sema/meta_type.go index 59d6c4281b..6ee455c1ab 100644 --- a/runtime/sema/meta_type.go +++ b/runtime/sema/meta_type.go @@ -37,7 +37,7 @@ var MetaType = &SimpleType{ Name: MetaTypeName, QualifiedName: MetaTypeName, TypeID: MetaTypeName, - tag: MetaTypeTag, + TypeTag: MetaTypeTag, IsResource: false, Storable: true, Equatable: true, diff --git a/runtime/sema/never_type.go b/runtime/sema/never_type.go index 1722259379..0d3fe642f5 100644 --- a/runtime/sema/never_type.go +++ b/runtime/sema/never_type.go @@ -23,7 +23,7 @@ var NeverType = &SimpleType{ Name: "Never", QualifiedName: "Never", TypeID: "Never", - tag: NeverTypeTag, + TypeTag: NeverTypeTag, IsResource: false, Storable: false, Equatable: false, diff --git a/runtime/sema/path_type.go b/runtime/sema/path_type.go index 41b19dd6f1..fa443cd17f 100644 --- a/runtime/sema/path_type.go +++ b/runtime/sema/path_type.go @@ -23,7 +23,7 @@ var PathType = &SimpleType{ Name: "Path", QualifiedName: "Path", TypeID: "Path", - tag: PathTypeTag, + TypeTag: PathTypeTag, IsResource: false, Storable: true, Equatable: true, @@ -43,7 +43,7 @@ var StoragePathType = &SimpleType{ Name: "StoragePath", QualifiedName: "StoragePath", TypeID: "StoragePath", - tag: StoragePathTypeTag, + TypeTag: StoragePathTypeTag, IsResource: false, Storable: true, Equatable: true, @@ -59,7 +59,7 @@ var CapabilityPathType = &SimpleType{ Name: "CapabilityPath", QualifiedName: "CapabilityPath", TypeID: "CapabilityPath", - tag: CapabilityPathTypeTag, + TypeTag: CapabilityPathTypeTag, IsResource: false, Storable: true, Equatable: true, @@ -79,7 +79,7 @@ var PublicPathType = &SimpleType{ Name: "PublicPath", QualifiedName: "PublicPath", TypeID: "PublicPath", - tag: PublicPathTypeTag, + TypeTag: PublicPathTypeTag, IsResource: false, Storable: true, Equatable: true, @@ -95,7 +95,7 @@ var PrivatePathType = &SimpleType{ Name: "PrivatePath", QualifiedName: "PrivatePath", TypeID: "PrivatePath", - tag: PrivatePathTypeTag, + TypeTag: PrivatePathTypeTag, IsResource: false, Storable: true, Equatable: true, diff --git a/runtime/sema/simple_type.go b/runtime/sema/simple_type.go index f137fdf61a..7e6affd690 100644 --- a/runtime/sema/simple_type.go +++ b/runtime/sema/simple_type.go @@ -42,7 +42,7 @@ type SimpleType struct { QualifiedName string TypeID TypeID Name string - tag TypeTag + TypeTag TypeTag memberResolversOnce sync.Once Importable bool Exportable bool @@ -60,7 +60,7 @@ var _ ContainerType = &SimpleType{} func (*SimpleType) IsType() {} func (t *SimpleType) Tag() TypeTag { - return t.tag + return t.TypeTag } func (t *SimpleType) String() string { diff --git a/runtime/sema/storage_capability_controller.gen.go b/runtime/sema/storage_capability_controller.gen.go index c74ed0974a..9ef52376db 100644 --- a/runtime/sema/storage_capability_controller.gen.go +++ b/runtime/sema/storage_capability_controller.gen.go @@ -134,7 +134,7 @@ var StorageCapabilityControllerType = &SimpleType{ Name: StorageCapabilityControllerTypeName, QualifiedName: StorageCapabilityControllerTypeName, TypeID: StorageCapabilityControllerTypeName, - tag: StorageCapabilityControllerTypeTag, + TypeTag: StorageCapabilityControllerTypeTag, IsResource: false, Storable: false, Equatable: false, diff --git a/runtime/sema/string_type.go b/runtime/sema/string_type.go index b4fa762599..6568c5b0a8 100644 --- a/runtime/sema/string_type.go +++ b/runtime/sema/string_type.go @@ -47,7 +47,7 @@ var StringType = &SimpleType{ Name: "String", QualifiedName: "String", TypeID: "String", - tag: StringTypeTag, + TypeTag: StringTypeTag, IsResource: false, Storable: true, Equatable: true, diff --git a/runtime/sema/type.go b/runtime/sema/type.go index 20b942aede..afb3e44486 100644 --- a/runtime/sema/type.go +++ b/runtime/sema/type.go @@ -4906,6 +4906,48 @@ func NewFunctionMember( } } +func NewUnmeteredConstructorMember( + containerType Type, + access Access, + identifier string, + functionType *FunctionType, + docString string, +) *Member { + return NewConstructorMember( + nil, + containerType, + access, + identifier, + functionType, + docString, + ) +} + +func NewConstructorMember( + memoryGauge common.MemoryGauge, + containerType Type, + access Access, + identifier string, + functionType *FunctionType, + docString string, +) *Member { + + return &Member{ + ContainerType: containerType, + Access: access, + Identifier: ast.NewIdentifier( + memoryGauge, + identifier, + ast.EmptyPosition, + ), + DeclarationKind: common.DeclarationKindInitializer, + VariableKind: ast.VariableKindConstant, + TypeAnnotation: NewTypeAnnotation(functionType), + ArgumentLabels: functionType.ArgumentLabels(), + DocString: docString, + } +} + func NewUnmeteredPublicConstantFieldMember( containerType Type, identifier string, diff --git a/runtime/sema/void_type.go b/runtime/sema/void_type.go index a35ab56d9d..4ebe233b58 100644 --- a/runtime/sema/void_type.go +++ b/runtime/sema/void_type.go @@ -23,7 +23,7 @@ var VoidType = &SimpleType{ Name: "Void", QualifiedName: "Void", TypeID: "Void", - tag: VoidTypeTag, + TypeTag: VoidTypeTag, IsResource: false, Storable: false, Equatable: true, diff --git a/runtime/stdlib/bls.gen.go b/runtime/stdlib/bls.gen.go index e88a5939fa..cde94eb341 100644 --- a/runtime/stdlib/bls.gen.go +++ b/runtime/stdlib/bls.gen.go @@ -35,7 +35,7 @@ var BLSTypeAggregateSignaturesFunctionType = &sema.FunctionType{ Identifier: "signatures", TypeAnnotation: sema.NewTypeAnnotation(&sema.VariableSizedType{ Type: &sema.VariableSizedType{ - Type: UInt8Type, + Type: sema.UInt8Type, }, }), }, @@ -43,7 +43,7 @@ var BLSTypeAggregateSignaturesFunctionType = &sema.FunctionType{ ReturnTypeAnnotation: sema.NewTypeAnnotation( &sema.OptionalType{ Type: &sema.VariableSizedType{ - Type: UInt8Type, + Type: sema.UInt8Type, }, }, ), @@ -69,13 +69,13 @@ var BLSTypeAggregatePublicKeysFunctionType = &sema.FunctionType{ Label: sema.ArgumentLabelNotRequired, Identifier: "keys", TypeAnnotation: sema.NewTypeAnnotation(&sema.VariableSizedType{ - Type: PublicKeyType, + Type: sema.PublicKeyType, }), }, }, ReturnTypeAnnotation: sema.NewTypeAnnotation( &sema.OptionalType{ - Type: PublicKeyType, + Type: sema.PublicKeyType, }, ), } diff --git a/runtime/stdlib/builtin.go b/runtime/stdlib/builtin.go index d462a5f83d..d292eb1bef 100644 --- a/runtime/stdlib/builtin.go +++ b/runtime/stdlib/builtin.go @@ -18,10 +18,6 @@ package stdlib -import "github.com/onflow/cadence/runtime/sema" - -var UInt8Type = sema.UInt8Type - type StandardLibraryHandler interface { Logger UnsafeRandomGenerator diff --git a/runtime/stdlib/flow.go b/runtime/stdlib/flow.go index f34cfd633a..47451caf56 100644 --- a/runtime/stdlib/flow.go +++ b/runtime/stdlib/flow.go @@ -193,7 +193,7 @@ var AccountEventCodeHashParameter = sema.Parameter{ var AccountEventPublicKeyParameterAsCompositeType = sema.Parameter{ Identifier: "publicKey", TypeAnnotation: sema.NewTypeAnnotation( - PublicKeyType, + sema.PublicKeyType, ), } diff --git a/runtime/stdlib/publickey.go b/runtime/stdlib/publickey.go index 5b02f4250d..ef27bebc3a 100644 --- a/runtime/stdlib/publickey.go +++ b/runtime/stdlib/publickey.go @@ -25,8 +25,6 @@ import ( "github.com/onflow/cadence/runtime/sema" ) -var PublicKeyType = sema.PublicKeyType - const publicKeyConstructorFunctionDocString = ` Constructs a new public key ` @@ -260,7 +258,7 @@ func newPublicKeyVerifySignatureFunction( inter.ExpectType( publicKeyValue, - PublicKeyType, + sema.PublicKeyType, locationRange, ) @@ -330,7 +328,7 @@ func newPublicKeyVerifyPoPFunction( inter.ExpectType( publicKeyValue, - PublicKeyType, + sema.PublicKeyType, locationRange, ) diff --git a/runtime/stdlib/rlp.gen.go b/runtime/stdlib/rlp.gen.go index 44aac52a78..1fdf0f7895 100644 --- a/runtime/stdlib/rlp.gen.go +++ b/runtime/stdlib/rlp.gen.go @@ -34,13 +34,13 @@ var RLPTypeDecodeStringFunctionType = &sema.FunctionType{ Label: sema.ArgumentLabelNotRequired, Identifier: "input", TypeAnnotation: sema.NewTypeAnnotation(&sema.VariableSizedType{ - Type: UInt8Type, + Type: sema.UInt8Type, }), }, }, ReturnTypeAnnotation: sema.NewTypeAnnotation( &sema.VariableSizedType{ - Type: UInt8Type, + Type: sema.UInt8Type, }, ), } @@ -61,14 +61,14 @@ var RLPTypeDecodeListFunctionType = &sema.FunctionType{ Label: sema.ArgumentLabelNotRequired, Identifier: "input", TypeAnnotation: sema.NewTypeAnnotation(&sema.VariableSizedType{ - Type: UInt8Type, + Type: sema.UInt8Type, }), }, }, ReturnTypeAnnotation: sema.NewTypeAnnotation( &sema.VariableSizedType{ Type: &sema.VariableSizedType{ - Type: UInt8Type, + Type: sema.UInt8Type, }, }, ),