示例#1
0
        public IErrorReporter ErrorTemporaryValueReferenceEscapesFromFunction()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("Hi")
                                   .With(FunctionBuilder.Create("give", NameFactory.UnitNameReference(), Block.CreateStatement())));

                Return ret = Return.Create(ExpressionFactory.StackConstructor("Hi"));

                FunctionDefinition func = root_ns.AddBuilder(FunctionBuilder.Create("notimportant",
                                                                                    ExpressionReadMode.OptionalUse,
                                                                                    NameFactory.ReferenceNameReference("Hi"),

                                                                                    Block.CreateStatement(
                                                                                        ret
                                                                                        )));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.EscapingReference, ret.Expr));
            }

            return(resolver);
        }
示例#2
0
        public IErrorReporter ErrorSelfTypeUsage()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    DiscardingAnyExpressionDuringTests = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                NameReference invalid_self1 = NameFactory.SelfNameReference();
                NameReference invalid_self2 = NameFactory.SelfNameReference();

                // in time probably we will use Self type in more places, but for now we forbid everything we don't support
                root_ns.AddBuilder(TypeBuilder.Create("What")
                                   .With(FunctionBuilder.Create("foo", invalid_self1,
                                                                Block.CreateStatement(Return.Create(NameReference.Create("x"))))
                                         .Parameters(FunctionParameter.Create("x", invalid_self2))));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(2, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.SelfTypeOutsideConstructor, invalid_self1));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.SelfTypeOutsideConstructor, invalid_self2));
            }

            return(resolver);
        }
示例#3
0
        public IErrorReporter ErrorNoDefaultConstructor()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                var bar_def = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Bar"))
                                                 .With(FunctionDefinition.CreateInitConstructor(EntityModifier.None,
                                                                                                new[] { FunctionParameter.Create("a", NameFactory.Int64NameReference(),
                                                                                                                                 Variadic.None, null, isNameRequired: false, usageMode: ExpressionReadMode.CannotBeRead) },
                                                                                                Block.CreateStatement())));
                VariableDeclaration field_decl = VariableDeclaration.CreateStatement("x", NameReference.Create("Bar"), null,
                                                                                     EntityModifier.Public);
                var type_def = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Point"))
                                                  .With(field_decl));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.NoDefaultConstructor, field_decl));
            }

            return(resolver);
        }
示例#4
0
        public IErrorReporter AssignmentTypeChecking()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    GlobalVariables = true, RelaxedMode = true
                }.SetMutability(mutability));
                var root_ns   = env.Root;
                var system_ns = env.SystemNamespace;

                root_ns.AddNode(VariableDeclaration.CreateStatement("x", NameFactory.RealNameReference(), Undef.Create()));
                root_ns.AddNode(VariableDeclaration.CreateStatement("y", NameFactory.RealNameReference(), NameReference.Create("x"),
                                                                    modifier: EntityModifier.Public));
                var x_ref = NameReference.Create("x");
                root_ns.AddNode(VariableDeclaration.CreateStatement("z", NameFactory.Int64NameReference(), x_ref,
                                                                    modifier: EntityModifier.Public));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.TypeMismatch, x_ref));
            }

            return(resolver);
        }
示例#5
0
        public IErrorReporter ErrorUnusedVariableWithinUsedExpression()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    DiscardingAnyExpressionDuringTests = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                VariableDeclaration decl = VariableDeclaration.CreateExpression("result", NameFactory.Int64NameReference(), initValue: Undef.Create());
                root_ns.AddBuilder(FunctionBuilder.Create("anything", null,
                                                          ExpressionReadMode.OptionalUse,
                                                          NameFactory.UnitNameReference(),

                                                          Block.CreateStatement(
                                                              // the declaration-expression is used, but the variable itself is not
                                                              // thus we report it as unused (in such code user should pass the init-value itself w/o creating variable)
                                                              ExpressionFactory.Readout(decl)
                                                              )));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.BindableNotUsed, decl.Name));
            }

            return(resolver);
        }
示例#6
0
文件: Templates.cs 项目: macias/Skila
        public IErrorReporter ErrorConflictingConstConstraint()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("Mut").SetModifier(EntityModifier.Mutable));

                NameReference parent_constraint = NameReference.Create("Mut");
                root_ns.AddBuilder(FunctionBuilder.Create("proxy",
                                                          TemplateParametersBuffer.Create().Add("T").Values,
                                                          ExpressionReadMode.CannotBeRead,
                                                          NameFactory.UnitNameReference(),

                                                          Block.CreateStatement())
                                   .Constraints(ConstraintBuilder.Create("T")
                                                .SetModifier(EntityModifier.Const)
                                                .Inherits(parent_constraint)));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(2, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.InheritanceMutabilityViolation, parent_constraint));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.InheritingSealedType, parent_constraint));
            }

            return(resolver);
        }
示例#7
0
        public IErrorReporter ErrorCompoundDefaultValue()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    GlobalVariables = true, RelaxedMode = true, AllowProtocols = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                var decl = root_ns.AddNode(VariableDeclaration.CreateStatement("x", NameReferenceUnion.Create(
                                                                                   new[] { NameFactory.PointerNameReference(NameFactory.BoolNameReference()),
                                                                                           NameFactory.PointerNameReference(NameFactory.Int64NameReference()) }),
                                                                               initValue: null, modifier: EntityModifier.Public));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.CannotAutoInitializeCompoundType, decl));
            }

            return(resolver);
        }
示例#8
0
        public IErrorReporter ErrorShakingOffSelfType()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    DiscardingAnyExpressionDuringTests = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("What")
                                   .SetModifier(EntityModifier.Base)
                                   .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement(ExpressionFactory.Readout("x")))
                                         .SetModifier(EntityModifier.Pinned)
                                         .Parameters(FunctionParameter.Create("x", NameFactory.SelfNameReference()))));

                TypeDefinition next_type = root_ns.AddBuilder(TypeBuilder.Create("Next")
                                                              .Parents("What")
                                                              .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement(ExpressionFactory.Readout("y")))
                                                                    .SetModifier(EntityModifier.Pinned)
                                                                    // this is an error, we should preserve using self type
                                                                    .Parameters(FunctionParameter.Create("y", NameReference.Create("Next")))));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.BaseFunctionMissingImplementation, next_type));
            }

            return(resolver);
        }
示例#9
0
        public IErrorReporter LowestCommonAncestor()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                var abc_type   = root_ns.AddBuilder(TypeBuilder.Create("ABC"));
                var foo_type   = root_ns.AddBuilder(TypeBuilder.Create("Foo").Parents(NameReference.Create("ABC")));
                var bar_type   = root_ns.AddBuilder(TypeBuilder.Create("Bar").Parents(NameReference.Create("ABC")));
                var deriv_type = root_ns.AddBuilder(TypeBuilder.Create("Deriv").Parents(NameReference.Create("Foo")));

                var deriv_ref = root_ns.AddNode(NameReference.Create("Deriv"));
                var bar_ref   = root_ns.AddNode(NameReference.Create("Bar"));
                var abc_ref   = root_ns.AddNode(NameReference.Create("ABC"));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(bar_type, bar_ref.Binding.Match.Instance.Target);
                Assert.AreEqual(deriv_type, deriv_ref.Binding.Match.Instance.Target);
                Assert.AreEqual(abc_type, abc_ref.Binding.Match.Instance.Target);

                bool found = TypeMatcher.LowestCommonAncestor(resolver.Context,
                                                              bar_ref.Binding.Match.Instance, deriv_ref.Binding.Match.Instance, out IEntityInstance common);
                Assert.IsTrue(found);
                Assert.IsTrue(abc_ref.Binding.Match.Instance.HasSameCore(common));
            }

            return(resolver);
        }
示例#10
0
        public IErrorReporter ErrorInheritingFinalType()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Point")));

                NameReference parent_name = NameReference.Create("Point");
                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("PointEx"))
                                   .Parents(parent_name));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.InheritingSealedType, parent_name));
            }

            return(resolver);
        }
示例#11
0
        public IErrorReporter ErrorTypeImplementationAsSecondaryParent()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Point1")).SetModifier(EntityModifier.Base));
                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Point2")).SetModifier(EntityModifier.Base));

                NameReference parent_name = NameReference.Create("Point2");
                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("PointEx"))
                                   .Parents(NameReference.Create("Point1"), parent_name));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.TypeImplementationAsSecondaryParent, parent_name));
            }

            return(resolver);
        }
示例#12
0
        public IErrorReporter ErrorNothingToOverride()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                FunctionDefinition function = FunctionBuilder.Create("getSome",
                                                                     ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(),
                                                                     Block.CreateStatement(new[] {
                    Return.Create(Int64Literal.Create("3"))
                }))
                                              .SetModifier(EntityModifier.Override);
                root_ns.AddBuilder(TypeBuilder.Create("GetPos")
                                   .With(function));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.NothingToOverride, function));
            }

            return(resolver);
        }
示例#13
0
        public IErrorReporter ErrorEnumCrossInheritance()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("Whatever")
                                   .SetModifier(EntityModifier.Base));

                TypeDefinition from_reg = root_ns.AddBuilder(TypeBuilder.CreateEnum("Sizing")
                                                             .Parents("Whatever")
                                                             .SetModifier(EntityModifier.Base)
                                                             .With(EnumCaseBuilder.Create("small", "big")));

                TypeDefinition from_enum = root_ns.AddBuilder(TypeBuilder.Create("Another")
                                                              .Parents("Sizing")
                                                              .SetModifier(EntityModifier.Base));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(2, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.EnumCrossInheritance, from_enum));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.EnumCrossInheritance, from_reg));
            }

            return(resolver);
        }
示例#14
0
        public IErrorReporter InheritingConstructorWithSelfType()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    DiscardingAnyExpressionDuringTests = true,
                    DebugThrowOnError = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("What")
                                   .SetModifier(EntityModifier.Base)
                                   .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement(ExpressionFactory.Readout("x")))
                                         .SetModifier(EntityModifier.Pinned)
                                         .Parameters(FunctionParameter.Create("x", NameFactory.SelfNameReference()))));

                TypeDefinition next_type = root_ns.AddBuilder(TypeBuilder.Create("Next")
                                                              .Parents("What")
                                                              .With(FunctionBuilder.CreateInitConstructor(Block.CreateStatement(ExpressionFactory.Readout("y")))
                                                                    .SetModifier(EntityModifier.Pinned | EntityModifier.Override | EntityModifier.UnchainBase)
                                                                    .Parameters(FunctionParameter.Create("y", NameFactory.SelfNameReference()))));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(0, resolver.ErrorManager.Errors.Count);
            }

            return(resolver);
        }
示例#15
0
文件: Templates.cs 项目: macias/Skila
        public IErrorReporter ErrorTraitDefinition()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("Bar")
                                   .SetModifier(EntityModifier.Base));

                TypeDefinition non_generic_trait = root_ns.AddBuilder(TypeBuilder.Create("Bar")
                                                                      .SetModifier(EntityModifier.Trait));

                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Foo", "T", VarianceMode.None)));

                TypeDefinition unconstrained_trait = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Foo", "T", VarianceMode.None))
                                                                        .SetModifier(EntityModifier.Trait));

                TypeDefinition missing_host = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("MissMe", "Y", VarianceMode.None))
                                                                 .SetModifier(EntityModifier.Trait)
                                                                 .Constraints(ConstraintBuilder.Create("Y")
                                                                              .SetModifier(EntityModifier.Const)));

                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Almost", "T", VarianceMode.None)));

                FunctionDefinition  trait_constructor = FunctionBuilder.CreateInitConstructor(Block.CreateStatement());
                VariableDeclaration trait_field       = VariableDeclaration.CreateStatement("f", NameFactory.Int64NameReference(), Int64Literal.Create("5"), EntityModifier.Public);
                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Almost", "T", VarianceMode.None))
                                   .SetModifier(EntityModifier.Trait)
                                   .With(trait_constructor)
                                   .With(trait_field)
                                   .Constraints(ConstraintBuilder.Create("T")
                                                .SetModifier(EntityModifier.Const)));

                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Inheriting", "T", VarianceMode.None)));

                NameReference parent_impl = NameReference.Create("Bar");
                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Inheriting", "T", VarianceMode.None))
                                   .Parents(parent_impl)
                                   .SetModifier(EntityModifier.Trait)
                                   .Constraints(ConstraintBuilder.Create("T")
                                                .SetModifier(EntityModifier.Const)));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(6, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.NonGenericTrait, non_generic_trait));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.UnconstrainedTrait, unconstrained_trait));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.MissingHostTypeForTrait, missing_host));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.TraitConstructor, trait_constructor));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.TraitInheritingTypeImplementation, parent_impl));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.FieldInNonImplementationType, trait_field));
            }

            return(resolver);
        }
示例#16
0
        public IErrorReporter ParentNamesResolving()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns   = env.Root;
                var system_ns = env.SystemNamespace;

                var foo_type = TypeBuilder.Create(NameDefinition.Create("Foo", "V", VarianceMode.None)).Build();
                system_ns.AddNode(foo_type);
                var parent_ref = NameReference.Create("Foo", NameReference.Create("T"));
                var tuple_type = TypeBuilder.Create(NameDefinition.Create("Tuple", "T", VarianceMode.None)).Parents(parent_ref).Build();
                system_ns.AddNode(tuple_type);

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, parent_ref.Binding.Matches.Count());
                Assert.AreEqual(foo_type, parent_ref.Binding.Match.Instance.Target);
                Assert.AreEqual(tuple_type.NestedTypes().Single(),
                                parent_ref.Binding.Match.Instance.TemplateArguments.Single().Target());
            }

            return(resolver);
        }
示例#17
0
文件: Templates.cs 项目: macias/Skila
        public IErrorReporter BasicTraitDefinition()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Foo", "T", VarianceMode.None)));

                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Foo", "T", VarianceMode.None))
                                   .SetModifier(EntityModifier.Trait)
                                   .Constraints(ConstraintBuilder.Create("T")
                                                .SetModifier(EntityModifier.Const)));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(0, resolver.ErrorManager.Errors.Count);
            }

            return(resolver);
        }
示例#18
0
        public IErrorReporter ErrorLoopedAncestors()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns   = env.Root;
                var system_ns = env.SystemNamespace;

                system_ns.AddBuilder(TypeBuilder.Create("Foo")
                                     .SetModifier(EntityModifier.Base)
                                     .Parents(NameReference.Create("Bar")));
                system_ns.AddBuilder(TypeBuilder.Create("Bar")
                                     .SetModifier(EntityModifier.Base)
                                     .Parents(NameReference.Create("Foo")));

                // if it does not hang, it is OK
                resolver = NameResolver.Create(env);
                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.AreEqual(ErrorCode.CyclicTypeHierarchy, resolver.ErrorManager.Errors.Single().Code);
            }

            return(resolver);
        }
示例#19
0
        public IErrorReporter ErrorReassigningFixedVariable()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    DiscardingAnyExpressionDuringTests = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                IExpression assignment = Assignment.CreateStatement(NameReference.Create("x"), Int64Literal.Create("5"));
                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "notimportant",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.UnitNameReference(),

                                       Block.CreateStatement(new[] {
                    VariableDeclaration.CreateStatement("x", null, Int64Literal.Create("3")),
                    assignment,
                    ExpressionFactory.Readout("x")
                })));


                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.CannotReassignReadOnlyVariable, assignment));
            }

            return(resolver);
        }
示例#20
0
        public IErrorReporter ErrorMissingFunctionImplementation()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("Inter")
                                   .With(FunctionBuilder.CreateDeclaration("bar",
                                                                           ExpressionReadMode.OptionalUse,
                                                                           NameFactory.Int64NameReference()))
                                   .SetModifier(EntityModifier.Interface));

                root_ns.AddBuilder(TypeBuilder.Create("MiddleImpl")
                                   // ok to ignore the functions inside abstract type
                                   .SetModifier(EntityModifier.Abstract)
                                   .Parents(NameReference.Create("Inter")));

                // there is still function to implement
                TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create("Impl")
                                                              .Parents(NameReference.Create("MiddleImpl")));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.BaseFunctionMissingImplementation, type_impl));
            }

            return(resolver);
        }
示例#21
0
        public IErrorReporter VariableBinding()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns   = env.Root;
                var system_ns = env.SystemNamespace;

                var decl_1 = VariableDeclaration.CreateStatement("x", NameFactory.RealNameReference(), Undef.Create(), modifier: EntityModifier.Public);
                root_ns.AddNode(decl_1);
                var var_1_ref = NameReference.Create("x");
                var decl_2    = root_ns.AddNode(VariableDeclaration.CreateStatement("y", NameFactory.RealNameReference(),
                                                                                    var_1_ref, modifier: EntityModifier.Public));

                resolver = NameResolver.Create(env);
                Assert.AreEqual(1, decl_1.TypeName.Binding().Matches.Count);
                Assert.AreEqual(env.Real64Type, decl_1.TypeName.Binding().Match.Instance.Target);

                Assert.AreEqual(1, var_1_ref.Binding.Matches.Count);
                Assert.AreEqual(decl_1, var_1_ref.Binding.Match.Instance.Target);
            }

            return(resolver);
        }
示例#22
0
        public IErrorReporter ErrorCallingHeapMethodOnValue()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("Hi")
                                   .With(FunctionBuilder.Create("give", NameFactory.UnitNameReference(), Block.CreateStatement())
                                         .SetModifier(EntityModifier.HeapOnly)));

                FunctionCall call = FunctionCall.Create(NameReference.Create("v", "give"));
                root_ns.AddBuilder(FunctionBuilder.Create(
                                       "notimportant",
                                       ExpressionReadMode.OptionalUse,
                                       NameFactory.UnitNameReference(),

                                       Block.CreateStatement(
                                           VariableDeclaration.CreateStatement("v", null, ExpressionFactory.StackConstructor("Hi")),
                                           call
                                           )));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.CallingHeapFunctionWithValue, call));
            }

            return(resolver);
        }
示例#23
0
        public IErrorReporter TypeInference()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    DiscardingAnyExpressionDuringTests = true, DebugThrowOnError = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                var var_x = VariableDeclaration.CreateStatement("x", NameFactory.RealNameReference(), Undef.Create());
                var var_y = VariableDeclaration.CreateStatement("y", null, NameReference.Create("x"));

                var func_def_void = root_ns.AddBuilder(FunctionBuilder.Create(
                                                           "notimportant",
                                                           ExpressionReadMode.OptionalUse,
                                                           NameFactory.UnitNameReference(),

                                                           Block.CreateStatement(new[] {
                    var_x,
                    var_y,
                    ExpressionFactory.Readout("y")
                })));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(0, resolver.ErrorManager.Errors.Count);

                Assert.IsTrue(env.Real64Type.InstanceOf.HasSameCore(var_y.Evaluation.Components));
            }

            return(resolver);
        }
示例#24
0
        public IErrorReporter ImplicitPointerReferenceConversion()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    DiscardingAnyExpressionDuringTests = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                var decl_src = VariableDeclaration.CreateStatement("foo", NameFactory.PointerNameReference(NameFactory.Int64NameReference()),
                                                                   initValue: Undef.Create());
                var decl_dst = VariableDeclaration.CreateStatement("bar", NameFactory.ReferenceNameReference(NameFactory.Int64NameReference()),
                                                                   initValue: NameReference.Create("foo"));

                var func_def_void = root_ns.AddBuilder(FunctionBuilder.Create(
                                                           "notimportant",
                                                           ExpressionReadMode.OptionalUse,
                                                           NameFactory.UnitNameReference(),

                                                           Block.CreateStatement(new[] {
                    decl_src,
                    decl_dst,
                    ExpressionFactory.Readout("bar")
                })));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(0, resolver.ErrorManager.Errors.Count);
            }

            return(resolver);
        }
示例#25
0
        public IErrorReporter CatVarianceExample() // Programming in Scala, 2nd ed, p. 399
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                NameReference result_typename = NameReference.Create("Cat",
                                                                     NameReference.Create("Cat", NameReference.Create("U"), NameReference.Create("T")), NameReference.Create("U"));

                root_ns.AddBuilder(TypeBuilder.CreateInterface(NameDefinition.Create("Cat", TemplateParametersBuffer.Create()
                                                                                     .Add("T", VarianceMode.In).Add("U", VarianceMode.Out).Values))
                                   .With(FunctionBuilder.CreateDeclaration("meow", "W", VarianceMode.In, ExpressionReadMode.ReadRequired, result_typename)
                                         .Parameters(FunctionParameter.Create("volume", NameReference.Create("T")),
                                                     FunctionParameter.Create("listener",
                                                                              NameReference.Create("Cat", NameReference.Create("U"), NameReference.Create("T"))))));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(0, resolver.ErrorManager.Errors.Count);
            }

            return(resolver);
        }
示例#26
0
文件: Templates.cs 项目: macias/Skila
        public IErrorReporter ErrorMisplacedConstraint()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                TemplateConstraint constraints = ConstraintBuilder.Create("BOO").SetModifier(EntityModifier.Const);
                root_ns.AddBuilder(TypeBuilder.Create("Greeter", "BOO")
                                   .With(FunctionBuilder.Create("say", ExpressionReadMode.ReadRequired, NameFactory.UnitNameReference(),
                                                                Block.CreateStatement())
                                         .Constraints(constraints)));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.MisplacedConstraint, constraints));
            }

            return(resolver);
        }
示例#27
0
        public IErrorReporter ErrorCircularValueNesting()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Language.Environment.Create(new Options()
                {
                    DiscardingAnyExpressionDuringTests = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                VariableDeclaration decl1 = VariableDeclaration.CreateStatement("s", NameReference.Create("Form"), null, EntityModifier.Private);
                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Shape"))
                                   .With(FunctionBuilder.Create("reader", NameFactory.UnitNameReference(),
                                                                Block.CreateStatement(ExpressionFactory.Readout(NameReference.CreateThised("s")))))
                                   .With(decl1));

                VariableDeclaration decl2 = VariableDeclaration.CreateStatement("f", NameReference.Create("Shape"), null, EntityModifier.Private);
                root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Form"))
                                   .With(FunctionBuilder.Create("reader", NameFactory.UnitNameReference(),
                                                                Block.CreateStatement(ExpressionFactory.Readout(NameReference.CreateThised("f")))))
                                   .With(decl2));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(2, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.NestedValueOfItself, decl1));
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.NestedValueOfItself, decl2));
            }

            return(resolver);
        }
示例#28
0
文件: Templates.cs 项目: macias/Skila
        public IErrorReporter ErrorDisabledProtocols()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                // just testing if disabling protocols (default) option really works
                var env = Environment.Create(new Options()
                {
                }.SetMutability(mutability));
                var root_ns = env.Root;

                FunctionDefinition func_constraint = FunctionBuilder.CreateDeclaration("getMe",
                                                                                       ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference());

                FunctionDefinition func = root_ns.AddBuilder(FunctionBuilder.Create("proxy",
                                                                                    TemplateParametersBuffer.Create().Add("T").Values,
                                                                                    ExpressionReadMode.OptionalUse,
                                                                                    NameFactory.UnitNameReference(), Block.CreateStatement())
                                                             .Constraints(ConstraintBuilder.Create("T").Has(func_constraint)));

                resolver = NameResolver.Create(env);

                TypeDefinition template_type = func.NestedTypes().Single();
                EntityModifier type_modifier = template_type.Modifier;

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.DisabledProtocols, type_modifier));
            }

            return(resolver);
        }
示例#29
0
        public IErrorReporter ErrorStaticMemberReference()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    StaticMemberOnlyThroughTypeName = true
                }.SetMutability(mutability));
                var root_ns = env.Root;

                root_ns.AddBuilder(TypeBuilder.Create("Foo")
                                   .With(VariableDeclaration.CreateStatement("field", NameFactory.RealNameReference(), null,
                                                                             EntityModifier.Static | EntityModifier.Public)));

                NameReference field_ref = NameReference.Create("f", "field");
                root_ns.AddBuilder(FunctionBuilder.Create("foo",
                                                          ExpressionReadMode.OptionalUse,
                                                          NameFactory.RealNameReference(),
                                                          Block.CreateStatement(new IExpression[] {
                    VariableDeclaration.CreateStatement("f", NameReference.Create("Foo"), Undef.Create()),
                    Return.Create(field_ref)
                })));

                resolver = NameResolver.Create(env);

                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.StaticMemberAccessInInstanceContext, field_ref));
            }

            return(resolver);
        }
示例#30
0
        public IErrorReporter ErrorReassignableReferenceField()
        {
            NameResolver resolver = null;

            foreach (var mutability in Options.AllMutabilityModes)
            {
                var env = Environment.Create(new Options()
                {
                    AllowReferenceFields = true, DiscardingAnyExpressionDuringTests = true
                }
                                             .SetMutability(mutability));
                var root_ns = env.Root;

                VariableDeclaration decl = VariableDeclaration.CreateStatement("f",
                                                                               NameFactory.ReferenceNameReference(NameFactory.IntNameReference()),
                                                                               Undef.Create(),
                                                                               EntityModifier.Reassignable | EntityModifier.Public);

                root_ns.AddBuilder(TypeBuilder.Create("X")
                                   .SetModifier(EntityModifier.Mutable)
                                   .With(decl));

                resolver = NameResolver.Create(env);

                Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ReferenceFieldCannotBeReassignable, decl));
                Assert.AreEqual(1, resolver.ErrorManager.Errors.Count);
            }

            return(resolver);
        }