public void TypeRefFinding() { var pcl = ResolutionTests.CreateDefCtxt(@"module modA; class A(T) { int n; static int prop; static A!float statA; } void main() { auto a = new A!int(); a.n; A.prop = 3; int b = A.prop + 4; A!double.statA.statA = new A!double(); } "); var res = TypeReferenceFinder.Scan(pcl.ParseCache[0]["modA"], pcl); //Assert.AreEqual(6, res.TypeMatches.Count); //TODO: Correct variable recognization }
public void Test2_066UCSnytax() { var x = DParser.ParseExpression("short(3)"); var v = Evaluation.EvaluateValue(x, ResolutionTests.CreateDefCtxt()); Assert.That(v, Is.TypeOf(typeof(PrimitiveValue))); Assert.That((v as PrimitiveValue).BaseTypeToken, Is.EqualTo(DTokens.Short)); Assert.That((v as PrimitiveValue).Value, Is.EqualTo(3m)); }
public void EponymousTemplates() { var ctxt = ResolutionTests.CreateDefCtxt(@"module B; alias Tuple(T...) = T; alias Tup = Tuple!(int, float, string); enum isIntOrFloat(F) = is(F == int) || is(F == float); "); IExpression x; ISymbolValue v; AbstractType t; x = DParser.ParseExpression("isIntOrFloat!(Tup[0])"); v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(PrimitiveValue))); Assert.That((v as PrimitiveValue).Value, Is.Not.EqualTo(0m)); DToken tk; var td = DParser.ParseBasicType("Tuple!(int, float, string)", out tk); //t = TypeDeclarationResolver.ResolveSingle (td, ctxt); //Assert.That (t, Is.TypeOf(typeof(MemberSymbol))); //Assert.That ((t as MemberSymbol).Base, Is.TypeOf(typeof(DTuple))); x = DParser.ParseExpression("Tup[0]"); t = ExpressionTypeEvaluation.EvaluateType(x, ctxt); Assert.That(t, Is.TypeOf(typeof(PrimitiveType))); x = DParser.ParseExpression("Tup[1]"); t = ExpressionTypeEvaluation.EvaluateType(x, ctxt); Assert.That(t, Is.TypeOf(typeof(PrimitiveType))); x = DParser.ParseExpression("Tup[2]"); t = ExpressionTypeEvaluation.EvaluateType(x, ctxt); Assert.That(t, Is.TypeOf(typeof(ArrayType))); x = DParser.ParseExpression("isIntOrFloat!int"); v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(PrimitiveValue))); Assert.That((v as PrimitiveValue).Value, Is.Not.EqualTo(0m)); x = DParser.ParseExpression("isIntOrFloat!float"); v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(PrimitiveValue))); Assert.That((v as PrimitiveValue).Value, Is.Not.EqualTo(0m)); x = DParser.ParseExpression("isIntOrFloat!string"); v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(PrimitiveValue))); Assert.That((v as PrimitiveValue).Value, Is.EqualTo(0m)); }
public static void TestString(string literal, string content, bool ProvideObjModule = true) { ResolutionContext ctxt = null; var block = new DBlockNode(); if (ProvideObjModule) { ctxt = ResolutionTests.CreateDefCtxt(ResolutionTests.CreateCache(), block); } else { ctxt = ResolutionTests.CreateDefCtxt(new ParseCacheView(new string[] { }), block); } var x = DParser.ParseExpression(literal); Assert.That(x, Is.TypeOf(typeof(IdentifierExpression))); var id = (IdentifierExpression)x; var v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(ArrayValue))); var av = (ArrayValue)v; Assert.That(av.IsString, Is.True); Assert.AreEqual(av.StringValue, content); Assert.IsInstanceOfType(typeof(ArrayType), av.RepresentedType); var ar = (ArrayType)av.RepresentedType; switch (id.Subformat) { case LiteralSubformat.Utf8: Assert.AreEqual(ar.DeclarationOrExpressionBase.ToString(), "immutable(char)[]"); break; case LiteralSubformat.Utf16: Assert.AreEqual(ar.DeclarationOrExpressionBase.ToString(), "immutable(wchar)[]"); break; case LiteralSubformat.Utf32: Assert.AreEqual(ar.DeclarationOrExpressionBase.ToString(), "immutable(dchar)[]"); break; default: Assert.Fail(); return; } }
public void AliasedTypeTuple() { var ctxt = ResolutionTests.CreateDefCtxt(@"module A; template Tuple(T...) { alias Tuple = T; } alias Tup = Tuple!(int, float, string); template isIntOrFloat(T) { static if (is(T == int) || is(T == float)) enum isIntOrFloat = true; else enum isIntOrFloat = false; } "); IExpression x; ISymbolValue v; AbstractType t; x = DParser.ParseExpression("Tup[2]"); t = ExpressionTypeEvaluation.EvaluateType(x, ctxt); Assert.That(t, Is.TypeOf(typeof(ArrayType))); x = DParser.ParseExpression("isIntOrFloat!(Tup[2])"); v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(PrimitiveValue))); Assert.That((v as PrimitiveValue).Value, Is.EqualTo(0m)); x = DParser.ParseExpression("Tup[0]"); t = ExpressionTypeEvaluation.EvaluateType(x, ctxt); Assert.That(t, Is.TypeOf(typeof(PrimitiveType))); x = DParser.ParseExpression("Tup[1]"); t = ExpressionTypeEvaluation.EvaluateType(x, ctxt); Assert.That(t, Is.TypeOf(typeof(PrimitiveType))); x = DParser.ParseExpression("isIntOrFloat!(Tup[0])"); v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(PrimitiveValue))); Assert.That((v as PrimitiveValue).Value, Is.Not.EqualTo(0m)); }
public void Traits() { var pcl = ResolutionTests.CreateCache(@"module A; int i; string s; abstract class C { int foo(); } class NC { int foo(); } C c; int[] dynArr; int[5] statArr; auto assocArr = ['c' : 23, 'b' : 84]; struct S { void bar() { } void bar(int i) {} void bar(string s) {} static int statInt; } class D { void bar() { } abstract void absBar(); static void statFoo() {} final void finBar() {}; private int privInt; package int packInt; } class E : D{ final override void absBar() { } final void bar() {} } interface I { void bar(); } template Tmpl(){ void bar(); } ", @"module std.someStd;"); var ctxt = ResolutionTests.CreateDefCtxt(pcl, pcl[0]["A"], null); BoolTrait(ctxt, "isArithmetic, int"); BoolTrait(ctxt, "isArithmetic, i, i+1, int"); BoolTrait(ctxt, "isArithmetic", false); BoolTrait(ctxt, "isArithmetic, int*", false); BoolTrait(ctxt, "isArithmetic, s, 123", false); BoolTrait(ctxt, "isArithmetic, 123, s", false); BoolTrait(ctxt, "isAbstractClass, C, c"); BoolTrait(ctxt, "isAbstractClass, C"); BoolTrait(ctxt, "isAbstractClass, int", false); BoolTrait(ctxt, "isAbstractClass, NC", false); BoolTrait(ctxt, "isAbstractClass", false); BoolTrait(ctxt, "isAssociativeArray, assocArr"); BoolTrait(ctxt, "isAssociativeArray, dynArr", false); BoolTrait(ctxt, "isStaticArray, statArr"); BoolTrait(ctxt, "isStaticArray, dynArr", false); BoolTrait(ctxt, "isVirtualMethod, D.bar"); BoolTrait(ctxt, "isVirtualMethod, D.absBar"); BoolTrait(ctxt, "isVirtualMethod, I.bar"); BoolTrait(ctxt, "isVirtualMethod, Tmpl!().bar"); //BoolTrait(ctxt, "isVirtualMethod, E.bar"); //BoolTrait(ctxt, "isVirtualMethod, E.absBar"); BoolTrait(ctxt, "isVirtualMethod, S.bar", false); BoolTrait(ctxt, "isVirtualMethod, D.statFoo", false); BoolTrait(ctxt, "isVirtualMethod, D.finBar", false); BoolTrait(ctxt, "isVirtualFunction, D.bar"); BoolTrait(ctxt, "isVirtualFunction, D.absBar"); BoolTrait(ctxt, "isVirtualFunction, I.bar"); BoolTrait(ctxt, "isVirtualFunction, Tmpl!().bar"); //BoolTrait(ctxt, "isVirtualFunction, E.bar"); //BoolTrait(ctxt, "isVirtualFunction, E.absBar"); BoolTrait(ctxt, "isVirtualFunction, S.bar", false); BoolTrait(ctxt, "isVirtualFunction, D.statFoo", false); BoolTrait(ctxt, "isVirtualFunction, D.finBar"); BoolTrait(ctxt, "hasMember, C, \"foo\""); BoolTrait(ctxt, "hasMember, c, \"foo\""); BoolTrait(ctxt, "hasMember, C, \"noFoo\"", false); BoolTrait(ctxt, "hasMember, int, \"sizeof\""); var x = DParser.ParseExpression(@"__traits(identifier, C.aso.derp)"); var v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(ArrayValue))); var av = v as ArrayValue; Assert.That(av.IsString, Is.True); Assert.That(av.StringValue, Is.EqualTo("C.aso.derp")); x = DParser.ParseExpression("__traits(getMember, c, \"foo\")"); var t = ExpressionTypeEvaluation.EvaluateType(x, ctxt); Assert.That(t, Is.TypeOf(typeof(MemberSymbol))); x = DParser.ParseExpression("__traits(getOverloads, S, \"bar\")"); v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(TypeValue))); Assert.That((v as TypeValue).RepresentedType, Is.TypeOf(typeof(DTuple))); t = ExpressionTypeEvaluation.EvaluateType(x, ctxt); Assert.That(t, Is.TypeOf(typeof(DTuple))); x = DParser.ParseExpression("__traits(getProtection, D.privInt)"); v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(ArrayValue))); av = v as ArrayValue; Assert.That(av.IsString, Is.True); Assert.That(av.StringValue, Is.EqualTo("private")); x = DParser.ParseExpression("__traits(getProtection, D)"); v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(ArrayValue))); av = v as ArrayValue; Assert.That(av.IsString, Is.True); Assert.That(av.StringValue, Is.EqualTo("public")); x = DParser.ParseExpression("__traits(getProtection, D.packInt)"); v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(ArrayValue))); av = v as ArrayValue; Assert.That(av.IsString, Is.True); Assert.That(av.StringValue, Is.EqualTo("package")); BoolTrait(ctxt, "isSame, int, int"); BoolTrait(ctxt, "isSame, int, double", false); BoolTrait(ctxt, "isSame, C, D", false); BoolTrait(ctxt, "isSame, D, D"); BoolTrait(ctxt, "compiles", false); BoolTrait(ctxt, "compiles, asd.herp", false); BoolTrait(ctxt, "compiles, i"); BoolTrait(ctxt, "compiles, i + 1"); //BoolTrait(ctxt, "compiles, &i + 1", false); //TODO: Check if both operand types match..is this still efficient? BoolTrait(ctxt, "compiles, typeof(1)"); BoolTrait(ctxt, "compiles, S.nonExistingItem", false); //TODO: Make the resolver not resolve non-static items implicitly (i.e. without explicit resolution option) BoolTrait(ctxt, "compiles, S.statInt"); BoolTrait(ctxt, "compiles, 1,2,3,int,long,std"); BoolTrait(ctxt, "compiles, 1,2,3,int,long,3[1]", false); BoolTrait(ctxt, "compiles, 3[1]", false); BoolTrait(ctxt, "compiles, immutable(S44)(3, &i)", false); }