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); }
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); }
public IInterpreter HasConstraintWithValue() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { AllowInvalidMainResult = true, AllowProtocols = true, DebugThrowOnError = true }.SetMutability(mutability)); var root_ns = env.Root; FunctionDefinition func_constraint = FunctionBuilder.CreateDeclaration("getMe", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference()); root_ns.AddBuilder(FunctionBuilder.Create("proxy", TemplateParametersBuffer.Create().Add("T").Values, ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(FunctionCall.Create(NameReference.Create("t", "getMe"))) })) .Constraints(ConstraintBuilder.Create("T").Has(func_constraint)) .Parameters(FunctionParameter.Create("t", NameReference.Create("T")))); TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create("Y") .With(FunctionBuilder.Create("getMe", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("2")) })))); FunctionCall call = FunctionCall.Create(NameReference.Create("proxy"), FunctionArgument.Create(NameReference.Create("y"))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("y", null, ExpressionFactory.StackConstructor(NameReference.Create("Y"))), Return.Create(call) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
private IInterpreter duckVirtualCallWithGenericBase(Options options) { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(options.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("X", TemplateParametersBuffer.Create() .Add("T").Values)) .SetModifier(options.InterfaceDuckTyping ? EntityModifier.Interface : EntityModifier.Protocol) .With(FunctionBuilder.CreateDeclaration( "bar", ExpressionReadMode.ReadRequired, NameReference.Create("T")))); TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create("Y") .With(FunctionBuilder.Create( "bar", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("2")) })))); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("i", NameFactory.PointerNameReference(NameReference.Create("X", NameFactory.Int64NameReference())), ExpressionFactory.HeapConstructor(NameReference.Create("Y"))), Return.Create(FunctionCall.Create(NameReference.Create("i", "bar"))) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IErrorReporter ProperGenericWithCostraintsMethodOverride() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { DebugThrowOnError = true }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(TypeBuilder.CreateInterface(NameDefinition.Create("IBar", TemplateParametersBuffer.Create("TA").Values)) .With(FunctionBuilder.CreateDeclaration( "barend", "FA", VarianceMode.None, ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference()) .Constraints(ConstraintBuilder.Create("FA").Inherits(NameReference.Create("TA"))) .Parameters(FunctionParameter.Create("x", NameReference.Create("TA"))))); TypeDefinition type_impl = root_ns.AddBuilder( TypeBuilder.Create(NameDefinition.Create("Impl", TemplateParametersBuffer.Create("TB").Values)) .With(FunctionBuilder.Create( "barend", TemplateParametersBuffer.Create("FB").Values, ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("2")) })) .Parameters(FunctionParameter.Create("x", NameReference.Create("TB"), usageMode: ExpressionReadMode.CannotBeRead)) .Constraints(ConstraintBuilder.Create("FB").Inherits(NameReference.Create("TB"))) .SetModifier(EntityModifier.Override)) .Parents(NameReference.Create("IBar", NameReference.Create("TB")))); resolver = NameResolver.Create(env); Assert.AreEqual(0, resolver.ErrorManager.Errors.Count); } return(resolver); }
public IErrorReporter ProperGenericMethodOverride() { 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("IX", TemplateParametersBuffer.Create() .Add("T") .Values)) .With(FunctionBuilder.CreateDeclaration( "bar", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference()) .Parameters(FunctionParameter.Create("x", NameReference.Create("T"), Variadic.None, null, isNameRequired: false))) .SetModifier(EntityModifier.Interface)); TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("X", TemplateParametersBuffer.Create() .Add("V").Values)) .With(FunctionBuilder.Create( "bar", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("2")) })) .Parameters(FunctionParameter.Create("x", NameReference.Create("V"), usageMode: ExpressionReadMode.CannotBeRead)) .SetModifier(EntityModifier.Override)) .Parents(NameReference.Create("IX", NameReference.Create("V")))); resolver = NameResolver.Create(env); Assert.AreEqual(0, resolver.ErrorManager.Errors.Count); } return(resolver); }
public IErrorReporter TranslationTableOfInferredCommonTypes() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; TemplateParameter template_param = TemplateParametersBuffer.Create("T").Values.Single(); root_ns.AddBuilder(FunctionBuilder.Create("getMe", new[] { template_param }, ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement()) .Parameters(FunctionParameter.Create("a", NameFactory.ReferenceNameReference("T"), ExpressionReadMode.CannotBeRead), FunctionParameter.Create("b", NameFactory.ReferenceNameReference("T"), ExpressionReadMode.CannotBeRead))); FunctionCall call = FunctionCall.Create(NameReference.Create("getMe"), NameReference.Create("x"), NameReference.Create("y")); root_ns.AddBuilder(FunctionBuilder.Create("common", NameFactory.UnitNameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("x", NameFactory.ReferenceNameReference(NameFactory.Int64NameReference()), Int64Literal.Create("3")), VariableDeclaration.CreateStatement("y", NameFactory.ReferenceNameReference(NameFactory.BoolNameReference()), BoolLiteral.CreateTrue()), call ))); resolver = NameResolver.Create(env); Assert.AreEqual(0, resolver.ErrorManager.Errors.Count); // the actual point of this test are those two lines checking if we get correct translation table for entire // instance of the called function Assert.IsTrue(call.Resolution.TargetFunctionInstance.Translation.Translate(template_param, out IEntityInstance common_instance)); Assert.AreEqual(resolver.Context.Env.IEquatableType.InstanceOf, common_instance); } return(resolver); }
public IErrorReporter InferredTemplateArgumentsOnConstraints() { 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(FunctionBuilder.Create("part", TemplateParametersBuffer.Create("T", "X").Values, NameFactory.UnitNameReference(), Block.CreateStatement()) .Parameters(FunctionParameter.Create("x", NameReference.Create("T"), ExpressionReadMode.CannotBeRead)) .Constraints(ConstraintBuilder.Create("X") .BaseOf(NameReference.Create("T")))); FunctionCall call = FunctionCall.Create(NameReference.Create("part"), Int64Literal.Create("5")); var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "caller", NameFactory.UnitNameReference(), Block.CreateStatement(new IExpression[] { call }))); resolver = NameResolver.Create(env); Assert.AreEqual(0, resolver.ErrorManager.Errors.Count); Assert.AreEqual(env.Int64Type.InstanceOf, call.Name.TemplateArguments[0].TypeName.Evaluation.Components); Assert.AreEqual(env.Int64Type.InstanceOf, call.Name.TemplateArguments[1].TypeName.Evaluation.Components); } return(resolver); }
public IErrorReporter ErrorInOutVarianceFields() { 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 field_a_typename = NameReference.Create("TA"); NameReference field_b_typename = NameReference.Create("TB"); root_ns.AddBuilder(TypeBuilder.Create( NameDefinition.Create(NameFactory.TupleTypeName, TemplateParametersBuffer.Create().Add("TA", VarianceMode.In).Add("TB", VarianceMode.Out).Values)) .SetModifier(EntityModifier.Mutable) .With(VariableDeclaration.CreateStatement("fa", field_a_typename, Undef.Create(), env.Options.ReassignableModifier() | EntityModifier.Public)) .With(VariableDeclaration.CreateStatement("fb", field_b_typename, Undef.Create(), env.Options.ReassignableModifier() | EntityModifier.Public))); resolver = NameResolver.Create(env); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.VarianceForbiddenPosition, field_a_typename)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.VarianceForbiddenPosition, field_b_typename)); Assert.AreEqual(2, resolver.ErrorManager.Errors.Count); } return(resolver); }
public IErrorReporter ProperGenericMethodOverrideWithGenericOutput() { 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.CreateInterface(NameDefinition.Create("IMyInterface", TemplateParametersBuffer.Create().Add("TI").Values)) .With(FunctionBuilder.CreateDeclaration( "bar", ExpressionReadMode.OptionalUse, NameFactory.ReferenceNameReference(NameReference.Create("TI"))))); TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("MyImpl", TemplateParametersBuffer.Create().Add("MV").Values)) .With(FunctionBuilder.Create( "bar", ExpressionReadMode.OptionalUse, NameFactory.ReferenceNameReference(NameReference.Create("MV")), Block.CreateStatement(new[] { Return.Create(Undef.Create()) })) .SetModifier(EntityModifier.Override)) .Parents(NameReference.Create("IMyInterface", NameReference.Create("MV")))); resolver = NameResolver.Create(env); Assert.AreEqual(0, resolver.ErrorManager.Errors.Count); } return(resolver); }
public IErrorReporter ErrorInOutVarianceProperties() { 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 prop_a_typename = NameReference.Create("TA"); NameReference prop_b_typename = NameReference.Create("TB"); root_ns.AddBuilder(TypeBuilder.Create( NameDefinition.Create(NameFactory.TupleTypeName, TemplateParametersBuffer.Create().Add("TA", VarianceMode.In).Add("TB", VarianceMode.Out).Values)) .SetModifier(EntityModifier.Mutable) .With(ExpressionFactory.BasicConstructor(new[] { "adata", "bdata" }, new[] { NameReference.Create("TA"), NameReference.Create("TB") })) .With(PropertyBuilder.CreateAutoFull(env.Options, "adata", prop_a_typename, Undef.Create())) .With(PropertyBuilder.CreateAutoFull(env.Options, "bdata", prop_b_typename, Undef.Create()))); resolver = NameResolver.Create(env); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.VarianceForbiddenPosition, prop_a_typename)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.VarianceForbiddenPosition, prop_b_typename)); Assert.AreEqual(2, resolver.ErrorManager.Errors.Count); } return(resolver); }
public IErrorReporter ErrorPassingReferenceAsTypeArgument() { 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(FunctionBuilder.Create("proxy", TemplateParametersBuffer.Create().Add("T").Values, ExpressionReadMode.CannotBeRead, NameFactory.UnitNameReference(), Block.CreateStatement())); NameReference function_name = NameReference.Create("proxy", NameFactory.ReferenceNameReference(NameFactory.Int64NameReference())); root_ns.AddBuilder(FunctionBuilder.Create("tester", ExpressionReadMode.CannotBeRead, NameFactory.UnitNameReference(), Block.CreateStatement( FunctionCall.Create(function_name) ))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ReferenceAsTypeArgument, function_name.TemplateArguments.Single())); } return(resolver); }
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); }
public static NameDefinition Create(string name, string paramName, VarianceMode variance) { return(new NameDefinition(name, TemplateParametersBuffer.Create().Add(paramName, variance).Values)); }
public IErrorReporter ErrorHasConstraint() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { AllowInvalidMainResult = true, AllowProtocols = true }.SetMutability(mutability)); var root_ns = env.Root; FunctionDefinition func_constraint = FunctionBuilder.CreateDeclaration("getMe", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference()); // this function accepts any parameter where parameter type has function "getMe" FunctionDefinition constrained_func = root_ns.AddBuilder(FunctionBuilder.Create("proxy", TemplateParametersBuffer.Create().Add("T").Values, ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(FunctionCall.Create(NameReference.Create("t", "getMe"))) })) .Constraints(ConstraintBuilder.Create("T").Has(func_constraint)) .Parameters(FunctionParameter.Create("t", NameFactory.PointerNameReference("T")))); // this type does NOT have function "getMe" TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create("YMan") .With(FunctionBuilder.Create("missing", ExpressionReadMode.ReadRequired, NameFactory.Int64NameReference(), Block.CreateStatement(new[] { Return.Create(Int64Literal.Create("2")) })))); FunctionCall call = FunctionCall.Create(NameReference.Create("proxy"), FunctionArgument.Create(NameReference.Create("y_man"))); root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("y_man", null, ExpressionFactory.HeapConstructor(NameReference.Create("YMan"))), Return.Create(call) }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ViolatedHasFunctionConstraint, call.Callee)); } return(resolver); }
public IErrorReporter ErrorMixedMemoryClassOverrideWithGenericOutput() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; // here we define to return reference and value of generic type root_ns.AddBuilder(TypeBuilder.CreateInterface(NameDefinition.Create("IMyInterface", TemplateParametersBuffer.Create().Add("TI").Values)) .With(FunctionBuilder.CreateDeclaration( "bar", ExpressionReadMode.OptionalUse, NameReference.Create("TI"))) .With(FunctionBuilder.CreateDeclaration( "foo", ExpressionReadMode.OptionalUse, NameFactory.ReferenceNameReference(NameReference.Create("TI"))))); // and here we override the above functions but with changed output -- in case of reference we try to set result type as value // and in case of function with value output we try to set result type as reference // in short wy try to make such overrides (for result types) // V -> &V // &V -> V FunctionDefinition func1_impl = FunctionBuilder.Create( "bar", ExpressionReadMode.OptionalUse, NameFactory.ReferenceNameReference(NameReference.Create("MV")), Block.CreateStatement(new[] { Return.Create(Undef.Create()) })) .SetModifier(EntityModifier.Override); FunctionDefinition func2_impl = FunctionBuilder.Create( "foo", ExpressionReadMode.OptionalUse, NameReference.Create("MV"), Block.CreateStatement(new[] { Return.Create(Undef.Create()) })) .SetModifier(EntityModifier.Override); TypeDefinition type_impl = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("MyImpl", TemplateParametersBuffer.Create().Add("MV").Values)) .With(func1_impl) .With(func2_impl) .Parents(NameReference.Create("IMyInterface", NameReference.Create("MV")))); resolver = NameResolver.Create(env); Assert.AreEqual(4, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.BaseFunctionMissingImplementation, type_impl, 2)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.NothingToOverride, func1_impl)); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.NothingToOverride, func2_impl)); } return(resolver); }
public IErrorReporter ErrorViolatingConstConstraint() { 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("Bar") .SetModifier(EntityModifier.Mutable)); root_ns.AddBuilder(TypeBuilder.Create("Foo")); // we build type Point<T> with enforced "const" on T -- meaning we can pass only trully immutable types // as T VariableDeclaration field = VariableDeclaration.CreateStatement("m", NameReference.Create("T"), Undef.Create(), modifier: EntityModifier.Public); TypeDefinition point_type = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Point", TemplateParametersBuffer.Create().Add("T").Values)) .Constraints(ConstraintBuilder.Create("T") .SetModifier(EntityModifier.Const)) .With(field)); // Bar is mutable type, so we cannot construct Point<Bar> since Point requires immutable type NameReference wrong_type = NameReference.Create("Point", NameReference.Create("Bar")); var func_def = root_ns.AddBuilder(FunctionBuilder.Create( "foo", null, ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement(new[] { VariableDeclaration.CreateStatement("x", NameReference.Create("Point", NameReference.Create("Foo")), Undef.Create()), VariableDeclaration.CreateStatement("y", wrong_type, Undef.Create()), ExpressionFactory.Readout("x"), ExpressionFactory.Readout("y"), }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ViolatedMutabilityConstraint, wrong_type)); } return(resolver); }
public IErrorReporter InternalIndirectTranslationTables() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { MiniEnvironment = true, DiscardingAnyExpressionDuringTests = true }.SetMutability(mutability)); var root_ns = env.Root; const string proxy_typename = "Proxy"; const string proxy_elemtype = "X"; FunctionDefinition get_func = FunctionBuilder.Create("getMe", ExpressionReadMode.OptionalUse, NameReference.Create(proxy_elemtype), Block.CreateStatement(Return.Create(Undef.Create()))); TypeDefinition proxy = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create(proxy_typename, TemplateParametersBuffer.Create(proxy_elemtype).Values)) .With(get_func)); const string parent_typename = "Oldman"; const string parent_elemtype = "PT"; FunctionDefinition access_func = FunctionBuilder.Create("provide", ExpressionReadMode.OptionalUse, NameFactory.ReferenceNameReference(NameReference.Create(proxy_typename, NameReference.Create(parent_elemtype))), Block.CreateStatement(Return.Create(Undef.Create()))); TypeDefinition parent = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create(parent_typename, TemplateParametersBuffer.Create(parent_elemtype).Values)) .SetModifier(EntityModifier.Base) .With(access_func) ); const string child_typename = "Kid"; const string child_elemtype = "CT"; FunctionCall call = FunctionCall.Create(NameReference.Create("i", "getMe")); // with buggy template translation table we would have here a type mismatch error // if this error happens check first if prefix of the call is evaluated correctly VariableDeclaration assignment = VariableDeclaration.CreateStatement("e", NameReference.Create(child_elemtype), call); root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create(child_typename, TemplateParametersBuffer.Create(child_elemtype).Values)) .Parents(NameReference.Create(parent_typename, NameReference.Create(child_elemtype))) .With(FunctionBuilder.Create("process", ExpressionReadMode.CannotBeRead, NameFactory.UnitNameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("i", NameFactory.ReferenceNameReference(NameReference.Create(proxy_typename, NameReference.Create(child_elemtype))), FunctionCall.Create(NameReference.CreateThised("provide"))), ExpressionFactory.Readout("i"), assignment, ExpressionFactory.Readout("e") )))); resolver = NameResolver.Create(env); Assert.AreEqual(0, resolver.ErrorManager.Errors.Count); } return(resolver); }
public IErrorReporter InternalDirectTranslationTables() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { MiniEnvironment = true }.SetMutability(mutability)); var root_ns = env.Root; const string parent_typename = "Oldman"; const string parent_elemtype = "PT"; FunctionDefinition base_func = FunctionBuilder.CreateDeclaration("getMe", ExpressionReadMode.CannotBeRead, NameFactory.ReferenceNameReference(parent_elemtype)); TypeDefinition parent = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create(parent_typename, TemplateParametersBuffer.Create(parent_elemtype).Values)) .SetModifier(EntityModifier.Abstract) .With(base_func)); const string child_typename = "Kid"; const string child_elemtype = "CT"; FunctionDefinition deriv_func = FunctionBuilder.Create("getMe", ExpressionReadMode.CannotBeRead, NameFactory.ReferenceNameReference(child_elemtype), Block.CreateStatement(Return.Create(Undef.Create()))) .SetModifier(EntityModifier.Override); TypeDefinition child = root_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create(child_typename, TemplateParametersBuffer.Create(child_elemtype).Values)) .Parents(NameReference.Create(parent_typename, NameReference.Create(child_elemtype))) .With(deriv_func)); resolver = NameResolver.Create(env); // testing here template translation EntityInstance child_ancestor = child.Inheritance.OrderedAncestorsWithoutObject.Single(); IEntityInstance translated = base_func.ResultTypeName.Evaluation.Components.TranslateThrough(child_ancestor); // we have single function overriden, so it is easy to debug and spot if something goes wrong bool result = FunctionDefinitionExtension.IsDerivedOf(resolver.Context, deriv_func, base_func, child_ancestor); Assert.IsTrue(result); } return(resolver); }
public IErrorReporter ConstraintsMatching() { 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 base_type = system_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Basic"))); var abc_type = system_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("ABC")) .Parents(NameReference.Create("Basic"))); var derived_type = system_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Deriv")) .Parents(NameReference.Create("ABC"))); var foo_type = system_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Foo", TemplateParametersBuffer.Create().Add("V", VarianceMode.Out).Values)) .Constraints(ConstraintBuilder.Create("V").Inherits(NameReference.Create("ABC"))) .Parents(NameReference.Create("ABC"))); var tuple_type = system_ns.AddBuilder(TypeBuilder.Create(NameDefinition.Create("Tuple", TemplateParametersBuffer.Create().Add("T", VarianceMode.None).Values)) .Constraints(ConstraintBuilder.Create("T").BaseOf(NameReference.Create("ABC")))); var tuple_ok_type = TypeBuilder.Create( NameDefinition.Create("TupleOK", TemplateParametersBuffer.Create().Add("U", VarianceMode.None).Values)) .Constraints(ConstraintBuilder.Create("U").BaseOf(NameReference.Create("Basic"))) .Parents(NameReference.Create("Tuple", NameReference.Create("U"))).Build(); system_ns.AddNode(tuple_ok_type); var tuple_bad_type = TypeBuilder.Create( NameDefinition.Create("TupleBad", TemplateParametersBuffer.Create().Add("L", VarianceMode.None).Values)) .Parents(NameReference.Create("Tuple", NameReference.Create("L"))).Build(); system_ns.AddNode(tuple_bad_type); var foo_deriv_ref = system_ns.AddNode(NameReference.Create("Foo", NameReference.Create("Deriv"))); var tuple_basic_ref = system_ns.AddNode(NameReference.Create("Tuple", NameReference.Create("Basic"))); var foo_basic_ref = system_ns.AddNode(NameReference.Create("Foo", NameReference.Create("Basic"))); var tuple_deriv_ref = system_ns.AddNode(NameReference.Create("Tuple", NameReference.Create("Deriv"))); resolver = NameResolver.Create(env); // constraints are matched Assert.AreEqual(1, foo_deriv_ref.Binding.Matches.Count()); Assert.AreEqual(foo_type, foo_deriv_ref.Binding.Match.Instance.Target); Assert.AreEqual(1, tuple_basic_ref.Binding.Matches.Count()); Assert.AreEqual(tuple_type, tuple_basic_ref.Binding.Match.Instance.Target); // failed on constraints Assert.AreEqual(0, foo_basic_ref.Binding.Matches.Count()); Assert.AreEqual(0, tuple_deriv_ref.Binding.Matches.Count()); // constraints matching other constraints Assert.AreEqual(1, tuple_ok_type.ParentNames.Single().Binding.Matches.Count); Assert.AreEqual(tuple_type, tuple_ok_type.ParentNames.Single().Binding.Match.Instance.Target); Assert.AreEqual(0, tuple_bad_type.ParentNames.Single().Binding.Matches.Count); } return(resolver); }