예제 #1
0
        public void Test1()
        {
            var pcl  = ResolutionTests.CreateCache(@"module modA;

class A(T = int)
{
	static int prop;
	static A statA; // 6
}

A a = new A(); // 9

void main()
{
	A.prop = 3; // 13
	int b = A.prop + 4; // 14
	A.statA.statA = new A!float(); // 15
}
");
            var ctxt = ResolutionContext.Create(pcl, null, pcl[0]["modA"]);

            var refs = ReferencesFinder.Scan(pcl[0]["modA"]["A"].First(), ctxt) as List <ISyntaxRegion>;

            Assert.IsNotNull(refs);
            Assert.AreEqual(8, refs.Count);
        }
예제 #2
0
        public void IsExpressionAlias()
        {
            var ctxt = ResolutionTests.CreateCtxt("A", @"module A;
static if(is(const(int)* U == const(U)*))
U var;

U derp;
");

            IExpression  x;
            AbstractType t;
            DSymbol      ds;

            x = DParser.ParseExpression("var");
            (x as IdentifierExpression).Location = new CodeLocation(2, 3);
            t  = ExpressionTypeEvaluation.EvaluateType(x, ctxt);
            ds = t as DSymbol;

            Assert.That(t, Is.TypeOf(typeof(MemberSymbol)));
            Assert.That(ds.Base, Is.TypeOf(typeof(TemplateParameterSymbol)));
            ds = ds.Base as DSymbol;
            Assert.That(ds.Base, Is.TypeOf(typeof(PrimitiveType)));
            Assert.That((ds.Base as PrimitiveType).Modifier, Is.EqualTo(0));

            ctxt.CurrentContext.DeducedTemplateParameters.Clear();

            var dv = ctxt.ParseCache[0]["A"]["derp"].First() as DVariable;

            t = TypeDeclarationResolver.HandleNodeMatch(dv, ctxt);
            Assert.That(t, Is.TypeOf(typeof(MemberSymbol)));
            Assert.That((t as MemberSymbol).Base, Is.Null);
        }
예제 #3
0
        public void ClassInheritanceTest()
        {
            var pcl  = ResolutionTests.CreateCache(@"module modA;
				class A{}
				class B:A {}
				class C:A {}
				class D:C {}"                );
            var ctxt = ResolutionContext.Create(pcl, null, pcl[0]["modA"]);

            var A = GetType("A", ctxt);
            var B = GetType("B", ctxt);
            var C = GetType("C", ctxt);
            var D = GetType("D", ctxt);

            Assert.IsTrue(ResultComparer.IsEqual(A, A));
            Assert.IsTrue(ResultComparer.IsEqual(B, B));
            Assert.IsTrue(ResultComparer.IsEqual(C, C));
            Assert.IsTrue(ResultComparer.IsEqual(D, D));

            Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(A, B));
            Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(A, C));
            Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(A, D));

            Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(B, C));
            Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(C, B));

            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(A, A));
            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(B, A));
            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(C, A));
            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(D, C));
            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(D, A));
        }
예제 #4
0
        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
        }
예제 #5
0
        public void BasicResolution()
        {
            var          ctxt = ResolutionTests.CreateCtxt("modA", @"module modA;
void writeln(T...)(T t) {}
int[] foo(string a) {}
int foo(int a) {}

string globStr;
int globI;
");
            IExpression  x;
            AbstractType t;

            x = DParser.ParseExpression("globStr.foo()");
            t = ExpressionTypeEvaluation.EvaluateType(x, ctxt);
            Assert.That(t, Is.TypeOf(typeof(ArrayType)));

            x = DParser.ParseExpression("globI.foo()");
            t = ExpressionTypeEvaluation.EvaluateType(x, ctxt);
            Assert.That(t, Is.TypeOf(typeof(PrimitiveType)));

            x = DParser.ParseExpression("globStr.writeln()");
            t = ExpressionTypeEvaluation.EvaluateType(x, ctxt);
            Assert.That(t, Is.TypeOf(typeof(PrimitiveType)));
            Assert.That((t as PrimitiveType).TypeToken, Is.EqualTo(DTokens.Void));
        }
예제 #6
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[0]["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.IsInstanceOfType(typeof(VariableValue), v);
            var vv = vp[((VariableValue)v).Variable] as PrimitiveValue;

            Assert.AreEqual(3, vv.Value);
        }
예제 #7
0
                public override bool Matches(object actual)
                {
                    var code = actual as string;

                    if (code == null)
                    {
                        return(false);
                    }

                    code += "\n";

                    var m     = DParser.ParseString(code);
                    var cache = ResolutionTests.CreateCache();

                    (cache [0] as MutableRootPackage).AddModule(m);

                    var ed = new EditorData {
                        ModuleCode    = code,
                        CaretOffset   = code.Length - 1,
                        CaretLocation = DocumentHelper.OffsetToLocation(code, code.Length - 1),
                        SyntaxTree    = m,
                        ParseCache    = cache
                    };

                    var gen = new TestCompletionDataGen(null, null);
                    var res = CodeCompletion.GenerateCompletionData(ed, gen, 'a');

                    return(neg ? !res : res);
                }
예제 #8
0
        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));
        }
예제 #9
0
        public void InterfaceInheritanceTest()
        {
            var pcl  = ResolutionTests.CreateCache(@"module modA;
				class A {}
				class B {}

				interface IA {}
				interface IB {}

				interface IC : IA {}
				interface ID : IC {}

				class E : A, IA {}
				class F : B, IA {}

				class G : A, IC {}
				class H : B, ID {}"                );
            var ctxt = ResolutionContext.Create(pcl, null, pcl[0]["modA"]);

            var A  = GetType("A", ctxt);
            var B  = GetType("B", ctxt);
            var IA = GetType("IA", ctxt);
            var IB = GetType("IB", ctxt);
            var IC = GetType("IC", ctxt);
            var ID = GetType("ID", ctxt);
            var E  = GetType("E", ctxt);
            var F  = GetType("F", ctxt);
            var G  = GetType("G", ctxt);
            var H  = GetType("H", ctxt);

            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(IC, IA));
            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(ID, IC));
            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(ID, IA));

            Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(IA, IC));
            Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(IA, ID));
            Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(IC, IB));

            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(E, A));
            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(E, IA));

            Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(E, F));
            Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(F, E));

            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(F, B));
            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(F, IA));

            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(G, A));
            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(G, IC));
            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(G, IA));

            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(H, B));
            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(H, ID));
            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(H, IC));
            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(H, IA));
        }
예제 #10
0
        public static EditorData GenEditorData(int caretLine, int caretPos, string focusedModuleCode, params string[] otherModuleCodes)
        {
            var cache = ResolutionTests.CreateCache(otherModuleCodes);
            var ed    = new EditorData {
                ParseCache = cache
            };

            UpdateEditorData(ed, caretLine, caretPos, focusedModuleCode);

            return(ed);
        }
예제 #11
0
        public void IsExpressionAlias_InMethod()
        {
            var          ctxt = ResolutionTests.CreateCtxt("A", @"module A;
void main(){
pre;

static if(is(const(int)* U == const(U)*))
{
U var;
}

post;
}
");
            IExpression  x;
            AbstractType t;
            DSymbol      ds;

            var main = ctxt.ParseCache[0]["A"]["main"].First() as DMethod;

            ctxt.PushNewScope(main, main.Body.SubStatements.First());
            t = TypeDeclarationResolver.ResolveSingle(new IdentifierDeclaration("U")
            {
                Location = main.Body.SubStatements.First().Location
            }, ctxt);

            Assert.That(t, Is.Null);

            ctxt.Pop();
            ctxt.PushNewScope(main, main.Body.SubStatements.ElementAt(2));
            t = TypeDeclarationResolver.ResolveSingle(new IdentifierDeclaration("U")
            {
                Location = main.Body.SubStatements.ElementAt(2).Location
            }, ctxt);

            Assert.That(t, Is.Null);
            ctxt.Pop();

            x = DParser.ParseExpression("var");

            IStatement stmt;

            DResolver.SearchBlockAt(main, (x as IdentifierExpression).Location = new CodeLocation(3, 7), out stmt);

            ctxt.PushNewScope(main, stmt);
            t  = ExpressionTypeEvaluation.EvaluateType(x, ctxt);
            ds = t as DSymbol;

            Assert.That(t, Is.TypeOf(typeof(MemberSymbol)));
            Assert.That(ds.Base, Is.TypeOf(typeof(TemplateParameterSymbol)));
            ds = ds.Base as DSymbol;
            Assert.That(ds.Base, Is.TypeOf(typeof(PrimitiveType)));
            Assert.That((ds.Base as PrimitiveType).Modifier, Is.EqualTo(0));
        }
예제 #12
0
        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));
        }
예제 #13
0
        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;
            }
        }
예제 #14
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));
        }
예제 #15
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[0]["modA"]));

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

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

            Assert.IsInstanceOfType(typeof(PrimitiveValue), val);
            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);
        }
예제 #16
0
        public void TestTemplateDeductionAsConversion()
        {
            var pcl  = ResolutionTests.CreateCache(@"module modA;
void foo(T:T)(T[] t) {}

int[] p=[1,2,3,4,5];
");
            var ctxt = ResolutionContext.Create(pcl, null, pcl[0]["modA"]);

            var foo = pcl[0]["modA"]["foo"].First() as DMethod;

            ctxt.PushNewScope(foo);
            var foo_firstArg = TypeDeclarationResolver.Resolve(foo.Parameters[0].Type, ctxt);

            var p = TypeDeclarationResolver.ResolveIdentifier("p", ctxt, null)[0] as MemberSymbol;

            Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(p, foo_firstArg[0], ctxt));
            ctxt.Pop();
        }
예제 #17
0
        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));
        }
예제 #18
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[0]["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));
        }
예제 #19
0
        public void ReturnStmt()
        {
            var ctxt = ResolutionTests.CreateCtxt("A", @"module A;
int foo() { return 123; }
string foo(string s) { return s ~ ""gh""; }

string inty(A)() { return ""int y;""; }
");

            IExpression    x;
            ISymbolValue   v;
            ArrayValue     av;
            PrimitiveValue pv;

            x = DParser.ParseExpression("inty!(\"asdf\")");
            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("int y;"));

            x = DParser.ParseExpression("foo");
            v = Evaluation.EvaluateValue(x, ctxt);

            Assert.That(v, Is.TypeOf(typeof(PrimitiveValue)));
            pv = v as PrimitiveValue;
            Assert.That(pv.BaseTypeToken, Is.EqualTo(DTokens.Int));
            Assert.That(pv.Value, Is.EqualTo(123M));

            x = DParser.ParseExpression("foo(\"asdf\")");
            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("asdfgh"));
        }
예제 #20
0
        public void ParsePerformance1()
        {
            //var pc = ParsePhobos(false);
            var pcl = ResolutionTests.CreateCache(@"module modA;

import std.stdio, std.array;

class lol{
	
	static int Object;
	
	int inc(int i, int k)
	{
		
		return i+k;	
	}

	void derp() {}
	
	const void lolBar(this T)() {
		
		auto l=123.inc!int();
		lol.Object;
		writeln(typeid(T));
		
		Object=1;
	}
}

void main()
{
	immutable(char)[] arr;
	destroy(arr);
	for(int i=0;i<10;i++)
		writeln(i);
	return;
	
	auto st=new STest();
	auto tt = new ModClass!int();
	writeln(st.a);
	
	static assert(st.a==34);
	
	int i = delegate int() { return 123; }();	
	//int j= 234++;
	writeln(i);
	writeln(di,123,123);

	lol o = new lol();
	o.derp();
}

");
            //pcl.Add(pc);

            var sw   = new Stopwatch();
            var main = pcl[0]["modA"]["main"].First() as DMethod;

            Assert.AreEqual(0, (pcl[0]["modA"] as DModule).ParseErrors.Count);
            var s    = main.Body.SubStatements.Last() as IExpressionContainingStatement;
            var ctxt = ResolutionContext.Create(pcl, null, main, s.Location);
            //ctxt.ContextIndependentOptions |= ResolutionOptions.StopAfterFirstOverloads | ResolutionOptions.DontResolveBaseClasses | ResolutionOptions.DontResolveBaseTypes;
            var x = s.SubExpressions[0];

            //pc.UfcsCache.Update(pcl);
            sw.Restart();

            var t = ExpressionTypeEvaluation.EvaluateType(x, ctxt);

            sw.Stop();
            Trace.WriteLine("Took " + sw.Elapsed.TotalMilliseconds + "ms to resolve " + x);
        }
예제 #21
0
        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);
        }