Пример #1
0
        public static ISymbolValue EvaluateValue(IExpression x, AbstractSymbolValueProvider vp)
        {
            if (vp == null)
                vp = new StandardValueProvider(null);

            return new Evaluation(vp).E(x) as ISymbolValue;
        }
Пример #2
0
        public static ISymbolValue EvaluateValue(IExpression x, AbstractSymbolValueProvider vp)
        {
            if (vp == null)
            {
                vp = new StandardValueProvider(null);
            }

            return(new Evaluation(vp).E(x) as ISymbolValue);
        }
Пример #3
0
        /// <summary>
        /// Uses the standard value provider for expression value evaluation
        /// </summary>
        public static ISymbolValue EvaluateValue(IExpression x, ResolutionContext ctxt, bool lazyVariableValueEvaluation = false)
        {
            var vp = new StandardValueProvider(ctxt);
            var v  = EvaluateValue(x, vp);

            if (v is VariableValue && !lazyVariableValueEvaluation)
            {
                return(EvaluateValue(v as VariableValue, vp));
            }

            return(v);
        }
Пример #4
0
        public static ISymbolValue EvaluateValue(IExpression x, AbstractSymbolValueProvider vp)
        {
            if (vp == null)
            {
                vp = new StandardValueProvider(null);
            }

            var ev = new Evaluation(vp);

            var v = x.Accept(ev);

            if (v == null && ev.Errors.Count != 0)
            {
                return(new ErrorValue(ev.Errors.ToArray()));
            }

            return(v);
        }
Пример #5
0
		public void HashingTest1()
		{
			var hashVis = D_Parser.Dom.Visitors.AstElementHashingVisitor.Instance;
			var vp = new StandardValueProvider (ResolutionTests.CreateCtxt ("A", "module A;"));
			var v1 = Evaluation.EvaluateValue (DParser.ParseExpression ("123"), vp);
			var v2 = Evaluation.EvaluateValue (DParser.ParseExpression ("-123"), vp);

			Assert.That (v1.Accept(hashVis), Is.Not.EqualTo(v2.Accept(hashVis)));
		}
Пример #6
0
		public void TestIsExpression()
		{
			var pcl = ResolutionTests.CreateCache(@"module modA;
class A {}
class B : A {}
class C : A {}

struct DynArg(int i) {
        static assert (i >= 0);

        alias i argNr;
}

template isDynArg(T) {
        static if (is(typeof(T.argNr))) {                               // must have the argNr field
                static if(is(T : DynArg!(T.argNr))) {           // now check the exact type
                        static const bool isDynArg = true;
                } else static const bool isDynArg = false;
        } else static const bool isDynArg = false;
}
");

			var vp = new StandardValueProvider(ResolutionContext.Create(pcl, null,pcl.FirstPackage()["modA"]));

			Assert.IsTrue(EvalIsExpression("char*[] T : U[], U : V*, V", vp));
			Assert.IsTrue(EvalIsExpression("string T : U[], U : immutable(V), V : char", vp));
			Assert.IsFalse(EvalIsExpression("int[10] X : X[Y], int Y : 5",vp));

			Assert.IsTrue(EvalIsExpression("bool : bool", vp));
			Assert.IsTrue(EvalIsExpression("bool == bool", vp));
			Assert.IsTrue(EvalIsExpression("C : A", vp));
			Assert.IsTrue(EvalIsExpression("C : C", vp));
			Assert.IsFalse(EvalIsExpression("C == A", vp));
			Assert.IsTrue(EvalIsExpression("immutable(char) == immutable", vp));
			Assert.IsFalse(EvalIsExpression("string == immutable", vp));
			Assert.IsTrue(EvalIsExpression("A == class", vp));
			Assert.IsTrue(EvalIsExpression("typeof(A)", vp));
			Assert.IsFalse(EvalIsExpression("typeof(D)", vp));
		}
Пример #7
0
		public void TestCastExpression1()
		{
			var ctxt = ResolutionTests.CreateCtxt("A",@"module A;");
			var vp = new StandardValueProvider (ctxt);

			IExpression x;
			ISymbolValue v;

			x = DParser.ParseExpression ("cast(ubyte)20");
			v = Evaluation.EvaluateValue (x, vp);

			Assert.That (v, Is.TypeOf (typeof(PrimitiveValue)));
			Assert.That ((v as PrimitiveValue).BaseTypeToken, Is.EqualTo (DTokens.Ubyte));
			Assert.That ((v as PrimitiveValue).Value, Is.EqualTo(20M));
		}
Пример #8
0
		public void TestAccessExpression()
		{
			var pcl = ResolutionTests.CreateCache(@"module modA;

class A
{
	const int someProp=3;
}

A a;");

			var vp = new StandardValueProvider(ResolutionContext.Create(pcl, null, pcl.FirstPackage()["modA"]));
			/*
			var v = E("a.someProp", vp);
			Assert.IsInstanceOfType(v, typeof(PrimitiveValue));
			Assert.AreEqual(((PrimitiveValue)v).Value,3);
			*/
			var v = E("A.someProp", vp);
			Assert.That(v, Is.TypeOf(typeof(VariableValue)));
			var vv = vp[((VariableValue)v).Variable] as PrimitiveValue;
			Assert.AreEqual(3, vv.Value);
		}
Пример #9
0
		public void TestArrays()
		{
			var vp = new StandardValueProvider(null);

			var v = E("[11,22,33,43][1]", vp);
			Assert.That(v, Is.TypeOf(typeof(PrimitiveValue)));
			Assert.AreEqual(((PrimitiveValue)v).Value, 22);

			v = E("[11,22,33,44,55,66,77,88,99,100][1..3]", vp);

			Assert.That(v, Is.TypeOf(typeof(ArrayValue)));
			var av=(ArrayValue)v;
			Assert.AreEqual(av.Elements.Length,2);
			Assert.AreEqual((av.Elements[0] as PrimitiveValue).Value,22);
		}
Пример #10
0
		public void TestConstEval()
		{
			var pcl = ResolutionTests.CreateCache(@"module modA;
const a= 234;
enum b=123;
const int c=125;
enum int d=126;
");
			var vp = new StandardValueProvider(ResolutionContext.Create(pcl, null, pcl.FirstPackage()["modA"]));

			var v = E("a", vp);

			Assert.That(v, Is.TypeOf(typeof(VariableValue)));
			var val = vp[((VariableValue)v).Variable];

			Assert.That(val, Is.TypeOf(typeof(PrimitiveValue)));
			var pv = (PrimitiveValue)val;

			Assert.AreEqual(pv.Value, 234M);

			v = E("b", vp);
			val = vp[((VariableValue)v).Variable];
			pv=(PrimitiveValue)val;
			Assert.AreEqual(pv.Value, 123M);

			v = E("c", vp);
			val = vp[((VariableValue)v).Variable];
			pv = (PrimitiveValue)val;
			Assert.AreEqual(pv.Value, 125M);

			v = E("d", vp);
			val = vp[((VariableValue)v).Variable];
			pv = (PrimitiveValue)val;
			Assert.AreEqual(pv.Value, 126M);

			pv = E("d + 4", vp) as PrimitiveValue;
			Assert.IsNotNull(pv);
			Assert.AreEqual(130M, pv.Value);

			pv = E("d + a", vp) as PrimitiveValue;
			Assert.IsNotNull(pv);
			Assert.AreEqual(360M, pv.Value);
		}
Пример #11
0
        /// <summary>
        /// Associates the given arguments with the template parameters specified in the type/method declarations 
        /// and filters out unmatching overloads.
        /// </summary>
        /// <param name="rawOverloadList">Can be either type results or method results</param>
        /// <param name="givenTemplateArguments">A list of already resolved arguments passed explicitly 
        /// in the !(...) section of a template instantiation 
        /// or call arguments given in the (...) appendix 
        /// that follows a method identifier</param>
        /// <param name="isMethodCall">If true, arguments that exceed the expected parameter count will be ignored as far as all parameters could be satisfied.</param>
        /// <param name="ctxt"></param>
        /// <returns>A filtered list of overloads which mostly fit to the specified arguments.
        /// Usually contains only 1 element.
        /// The 'TemplateParameters' property of the results will be also filled for further usage regarding smart completion etc.</returns>
        public static AbstractType[] DeduceParamsAndFilterOverloads(IEnumerable<AbstractType> rawOverloadList,
			IEnumerable<ISemantic> givenTemplateArguments,
			bool isMethodCall,
			ResolutionContext ctxt,
			bool hasUndeterminedArguments=false)
        {
            if (rawOverloadList == null)
                return null;

            var filteredOverloads = hasUndeterminedArguments ? new List<AbstractType>(rawOverloadList) :
                DeduceOverloads(rawOverloadList, givenTemplateArguments, isMethodCall, ctxt);

            AbstractType[] sortedAndFilteredOverloads;

            // If there are >1 overloads, filter from most to least specialized template param
            if (filteredOverloads.Count > 1)
                sortedAndFilteredOverloads = SpecializationOrdering.FilterFromMostToLeastSpecialized(filteredOverloads, ctxt);
            else if (filteredOverloads.Count == 1)
                sortedAndFilteredOverloads = new[] { filteredOverloads[0] };
            else
                return null;

            if (hasUndeterminedArguments)
                return sortedAndFilteredOverloads;

            if(sortedAndFilteredOverloads!=null)
            {
                filteredOverloads.Clear();
                for(int i = sortedAndFilteredOverloads.Length - 1; i >= 0; i--)
                {
                    var ds = sortedAndFilteredOverloads[i] as DSymbol;
                    if(ds != null && ds.Definition.TemplateConstraint != null)
                    {
                        ctxt.CurrentContext.IntroduceTemplateParameterTypes(ds);
                        try{
                            var v = Evaluation.EvaluateValue(ds.Definition.TemplateConstraint, ctxt);
                            if(v is VariableValue)
                                v = new StandardValueProvider(ctxt)[((VariableValue)v).Variable];
                            if(!Evaluation.IsFalseZeroOrNull(v))
                                filteredOverloads.Add(ds);
                        }catch{} //TODO: Handle eval exceptions
                        ctxt.CurrentContext.RemoveParamTypesFromPreferredLocals(ds);
                    }
                    else
                        filteredOverloads.Add(sortedAndFilteredOverloads[i]);
                }
                if(filteredOverloads.Count == 0)
                    return null;
                sortedAndFilteredOverloads = filteredOverloads.ToArray();
            }

            if (sortedAndFilteredOverloads != null &&
                sortedAndFilteredOverloads.Length == 1)
            {
                var t = sortedAndFilteredOverloads [0];
                if(t is TemplateType)
                    return TryGetImplicitProperty (t as TemplateType, ctxt) ?? sortedAndFilteredOverloads;
                if (t is EponymousTemplateType)
                    return new[]{ DeduceEponymousTemplate(t as EponymousTemplateType, ctxt) };
            }

            return sortedAndFilteredOverloads;
        }
Пример #12
0
        public static List<ISemantic> PreResolveTemplateArgs(TemplateInstanceExpression tix, ResolutionContext ctxt, out bool hasNonFinalArgument)
        {
            hasNonFinalArgument = false;
            // Resolve given argument expressions
            var templateArguments = new List<ISemantic>();

            if (tix != null && tix.Arguments!=null)
                foreach (var arg in tix.Arguments)
                {
                    if (arg is TypeDeclarationExpression)
                    {
                        var tde = (TypeDeclarationExpression)arg;

                        var res = TypeDeclarationResolver.Resolve(tde.Declaration, ctxt);

                        if (ctxt.CheckForSingleResult(res, tde.Declaration) || res != null)
                        {
                            var mr = res[0] as MemberSymbol;
                            if (mr != null && mr.Definition is DVariable)
                            {
                                var dv = (DVariable)mr.Definition;

                                if (dv.IsAlias || dv.Initializer == null)
                                {
                                    templateArguments.Add(mr);
                                    continue;
                                }

                                ISemantic eval = null;

                                try
                                {
                                    eval = new StandardValueProvider(ctxt)[dv];
                                }
                                catch(System.Exception ee) // Should be a non-const-expression error here only
                                {
                                    ctxt.LogError(dv.Initializer, ee.Message);
                                }

                                templateArguments.Add(eval==null ? (ISemantic)mr : eval);
                            }
                            else{
                                if(!hasNonFinalArgument)
                                    hasNonFinalArgument = IsNonFinalArgument(res[0]);
                                templateArguments.Add(res[0]);
                            }
                        }
                    }
                    else
                    {
                        var v = Evaluation.EvaluateValue(arg, ctxt, true);
                        if (v is VariableValue)
                        {
                            var vv = v as VariableValue;
                            if (vv.Variable.IsConst && vv.Variable.Initializer != null)
                                v = Evaluation.EvaluateValue(vv, new StandardValueProvider(ctxt));
                        }
                        if(!hasNonFinalArgument)
                            hasNonFinalArgument = IsNonFinalArgument(v);
                        templateArguments.Add(v);
                    }
                }

            return templateArguments;
        }
Пример #13
0
        public static ISymbolValue EvaluateValue(IExpression x, AbstractSymbolValueProvider vp)
        {
            if (vp == null)
                vp = new StandardValueProvider(null);

            var ev = new Evaluation(vp);

            var v = ev.E(x) as ISymbolValue;

            if(v == null && ev.Errors.Count != 0)
                return new ErrorValue(ev.Errors.ToArray());

            return v;
        }
Пример #14
0
        /// <summary>
        /// Uses the standard value provider for expression value evaluation
        /// </summary>
        public static ISymbolValue EvaluateValue(IExpression x, ResolutionContext ctxt, bool lazyVariableValueEvaluation = false)
        {
            try
            {
                var vp = new StandardValueProvider (ctxt);
                var v = EvaluateValue (x, vp);

                if (v is VariableValue && !lazyVariableValueEvaluation) {
                    return EvaluateValue (v as VariableValue, vp);
                }

                return v;
            }
            catch (Exception ex)
            {
                return new ErrorValue(new EvaluationException(x, ex.Message + "\n\n" + ex.StackTrace));
            }
        }
Пример #15
0
        public static List<ISemantic> PreResolveTemplateArgs(TemplateInstanceExpression tix, ResolutionContext ctxt, out bool hasNonFinalArgument)
        {
            hasNonFinalArgument = false;
            // Resolve given argument expressions
            var templateArguments = new List<ISemantic>();

            if (tix != null && tix.Arguments!=null)
                foreach (var arg in tix.Arguments)
                {
                    if (arg is TypeDeclarationExpression)
                    {
                        var tde = (TypeDeclarationExpression)arg;

                        var res = TypeDeclarationResolver.ResolveSingle(tde.Declaration, ctxt);

                        // Might be a simple symbol without any applied template arguments that is then passed to an template alias parameter
                        if (res == null && tde.Declaration is IdentifierDeclaration)
                            res = TypeDeclarationResolver.ResolveSingle(tde.Declaration as IdentifierDeclaration, ctxt, null, false);

                        var amb = res as AmbiguousType;
                        if (amb != null)
                        {
                            // Error
                            res = amb.Overloads[0];
                        }

                        var mr = res as MemberSymbol;
                        if (mr != null && mr.Definition is DVariable)
                        {
                            var dv = (DVariable)mr.Definition;

                            if (dv.IsAlias || dv.Initializer == null)
                            {
                                templateArguments.Add(mr);
                                continue;
                            }

                            ISemantic eval = null;

                            try
                            {
                                eval = new StandardValueProvider(ctxt)[dv];
                            }
                            catch(System.Exception ee) // Should be a non-const-expression error here only
                            {
                                ctxt.LogError(dv.Initializer, ee.Message);
                            }

                            templateArguments.Add(eval ?? (ISemantic)mr);
                        }
                        else{
                            if(!hasNonFinalArgument)
                                hasNonFinalArgument = IsNonFinalArgument(res);
                            templateArguments.Add(res);
                        }
                    }
                    else
                    {
                        ISemantic v = Evaluation.EvaluateValue(arg, ctxt, true);
                        if (v is VariableValue)
                        {
                            var vv = v as VariableValue;
                            if (vv.Variable.IsConst && vv.Variable.Initializer != null)
                                v = Evaluation.EvaluateValue(vv, new StandardValueProvider(ctxt));
                        }
                        if(!hasNonFinalArgument)
                            hasNonFinalArgument = IsNonFinalArgument(v);
                        v = DResolver.StripValueTypeWrappers(v);
                        templateArguments.Add(v);
                    }
                }

            return templateArguments;
        }