Esempio n. 1
0
        public void FindOutermostExceptionScopeWithin_MultipleLevelsOfNesting_FoundOuter()
        {
            //-- Arrange

            var rootScope = new StatementScope(m_Class, m_Method, new StatementBlock());
            var homeScope = new StatementScope(new StatementBlock());

            var nestedScope1 = new StatementScope(new StatementBlock());

            var outerTryStatement = new TryStatement(() => { });
            var outerTryScope     = new StatementScope(new StatementBlock(), outerTryStatement, ExceptionBlockType.Try);

            var innerTryStatement = new TryStatement(() => { });
            var innerTryScope     = new StatementScope(new StatementBlock(), innerTryStatement, ExceptionBlockType.Try);

            var nestedScope2 = new StatementScope(new StatementBlock());

            //-- Act

            var result = StatementScope.Current.FindOutermostTryStatementWithin(homeScope);

            //-- Assert

            Assert.That(StatementScope.Current.Depth, Is.EqualTo(5));
            Assert.That(result, Is.SameAs(outerTryStatement));
        }
Esempio n. 2
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        public NewStructExpression(IOperand[] constructorArguments = null)
        {
            m_StatementScope = StatementScope.Current;

            m_StructType           = TypeTemplate.Resolve <TStruct>();
            m_ConstructorArguments = (constructorArguments ?? new IOperand[0]);

            if (m_ConstructorArguments.Length > 0)
            {
                var argumentTypes = m_ConstructorArguments.Select(arg => arg.OperandType).ToArray();
                m_Constructor = m_StructType.GetConstructor(argumentTypes);

                if (m_Constructor == null)
                {
                    throw new ArgumentException("Could not find constructor with specified argument types.");
                }

                foreach (var argument in m_ConstructorArguments.Reverse())
                {
                    m_StatementScope.Consume(argument);
                }
            }

            m_StatementScope.RegisterExpressionStatement(this);
        }
Esempio n. 3
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        protected void WriteMethodBody(MethodWriterBase methodWriter)
        {
            using (StatementScope.Stash())
            {
                using (new StatementScope(m_OwnerClass, methodWriter, statementBlock: m_Statements))
                {
                    methodWriter.Flush();
                }
            }
        }
Esempio n. 4
0
        public void RootScope_AttemptCreateNestedRoot_Throws()
        {
            //-- Arrange

            var rootScope = new StatementScope(m_Class, m_Method, new StatementBlock());

            //-- Act

            var nestedRootScope = new StatementScope(m_Class, m_Method, new StatementBlock());
        }
Esempio n. 5
0
        public void NoExceptionScope_ThisAndInheritedHaveNoValues()
        {
            //-- Act

            var rootScope = new StatementScope(m_Class, m_Method, new StatementBlock());

            //-- Assert

            Assert.That(StatementScope.Current.ThisExceptionBlockType, Is.EqualTo(ExceptionBlockType.None));
            Assert.That(StatementScope.Current.ThisExceptionStatement, Is.Null);
            Assert.That(StatementScope.Current.InheritedExceptionBlockType, Is.EqualTo(ExceptionBlockType.None));
            Assert.That(StatementScope.Current.InheritedExceptionStatement, Is.Null);
        }
Esempio n. 6
0
        public void SetUp()
        {
            StatementScope.Cleanup();

            var key = new TypeKey(baseType: typeof(object));

            m_Class = m_Module.DefineClass(key.BaseType, key, Guid.NewGuid().ToString());

            var classBody = new ImplementationClassWriter <object>(m_Class);

            classBody.Method <string>(x => x.ToString).Implement(m => { });

            m_Method = m_Class.GetMemberByName <MethodMember>("ToString");
        }
Esempio n. 7
0
        public void FindOutermostExceptionScopeWithin_InsideFinally_Throw()
        {
            //-- Arrange

            var rootScope = new StatementScope(m_Class, m_Method, new StatementBlock());
            var homeScope = new StatementScope(new StatementBlock());

            var tryStatement = new TryStatement(() => { });
            var finallyScope = new StatementScope(new StatementBlock(), tryStatement, ExceptionBlockType.Finally);

            //-- Act

            var result = StatementScope.Current.FindOutermostTryStatementWithin(homeScope);
        }
Esempio n. 8
0
        public void FindOutermostExceptionScopeWithin_RootScope_NotFound()
        {
            //-- Arrange

            var rootScope = new StatementScope(m_Class, m_Method, new StatementBlock());

            //-- Act

            var result = StatementScope.Current.FindOutermostTryStatementWithin(rootScope);

            //-- Assert

            Assert.That(result, Is.Null);
        }
Esempio n. 9
0
        public void RegisterNonExpressionOperand_NothingHappens()
        {
            //-- Arrange

            var statements = new StatementBlock();
            var rootScope  = new StatementScope(m_Class, m_Method, statements);

            //-- Act

            rootScope.RegisterExpressionStatement(expression: null);

            //-- Assert

            Assert.That(statements.Count, Is.EqualTo(0));
        }
Esempio n. 10
0
        public void FindOutermostExceptionScopeWithin_NestedScopesNoException_NotFound()
        {
            //-- Arrange

            var rootScope   = new StatementScope(m_Class, m_Method, new StatementBlock());
            var innerScope1 = new StatementScope(new StatementBlock());
            var innerScope2 = new StatementScope(new StatementBlock());

            //-- Act

            var result = StatementScope.Current.FindOutermostTryStatementWithin(rootScope);

            //-- Assert

            Assert.That(StatementScope.Current.Depth, Is.EqualTo(2));
            Assert.That(result, Is.Null);
        }
Esempio n. 11
0
        public void RegisterExpressionStatement_AddedToStatementList()
        {
            //-- Arrange

            var statements = new StatementBlock();
            var rootScope  = new StatementScope(m_Class, m_Method, statements);

            //-- Act

            var expression = new UnaryExpressionOperand <int, int>(new UnaryOperators.OperatorNegation <int>(), new Constant <int>(111));

            //-- Assert

            Assert.That(statements.Count, Is.EqualTo(1));
            Assert.That(statements[0], Is.InstanceOf <ExpressionStatement>());
            Assert.That(((ExpressionStatement)statements[0]).Expression, Is.SameAs(expression));
        }
Esempio n. 12
0
        public void FindOutermostExceptionScopeWithin_RootExceptionHomeInner_NotFound()
        {
            //-- Arrange

            var rootScope  = new StatementScope(m_Class, m_Method, new StatementBlock());
            var tryScope   = new StatementScope(new StatementBlock(), new TryStatement(() => { }), ExceptionBlockType.Try);
            var homeScope  = new StatementScope(new StatementBlock());
            var innerScope = new StatementScope(new StatementBlock());

            //-- Act

            var result = StatementScope.Current.FindOutermostTryStatementWithin(homeScope);

            //-- Assert

            Assert.That(StatementScope.Current.Depth, Is.EqualTo(3));
            Assert.That(result, Is.Null);
        }
Esempio n. 13
0
        public void ExceptionScope_ThisAndInheritedHaveSameValues()
        {
            //-- Arrange

            var rootScope    = new StatementScope(m_Class, m_Method, new StatementBlock());
            var tryStatement = new TryStatement(() => { });

            //-- Act

            var tryScope = new StatementScope(new StatementBlock(), tryStatement, ExceptionBlockType.Try);

            //-- Assert

            Assert.That(StatementScope.Current.ThisExceptionBlockType, Is.EqualTo(ExceptionBlockType.Try));
            Assert.That(StatementScope.Current.ThisExceptionStatement, Is.SameAs(tryStatement));
            Assert.That(StatementScope.Current.InheritedExceptionBlockType, Is.EqualTo(ExceptionBlockType.Try));
            Assert.That(StatementScope.Current.InheritedExceptionStatement, Is.SameAs(tryStatement));
        }
Esempio n. 14
0
        public void RegisterExpressionAndContainingExpression_ContainedExpressionConsumed()
        {
            //-- Arrange

            var statements = new StatementBlock();
            var rootScope  = new StatementScope(m_Class, m_Method, statements);

            //-- Act

            var innerExpression = new UnaryExpressionOperand <int, int>(new UnaryOperators.OperatorNegation <int>(), m_Method.TransparentWriter.Const(111));
            var outerExpression = new UnaryExpressionOperand <int, int>(new UnaryOperators.OperatorPlus <int>(), innerExpression);

            //-- Assert

            Assert.That(statements.Count, Is.EqualTo(1));
            Assert.That(statements[0], Is.InstanceOf <ExpressionStatement>());
            Assert.That(((ExpressionStatement)statements[0]).Expression, Is.SameAs(outerExpression));
        }
Esempio n. 15
0
        public void RootScope_InitialState()
        {
            //-- Act

            var scope = new StatementScope(m_Class, m_Method, new StatementBlock());

            //-- Assert

            Assert.That(scope.OwnerClass, Is.SameAs(m_Class));
            Assert.That(scope.OwnerMethod, Is.SameAs(m_Method));
            Assert.That(scope.Depth, Is.EqualTo(0));
            Assert.That(scope.Previous, Is.Null);
            Assert.That(scope.Root, Is.SameAs(scope));
            Assert.That(scope.ThisExceptionBlockType, Is.EqualTo(ExceptionBlockType.None));
            Assert.That(scope.ThisExceptionStatement, Is.Null);
            Assert.That(scope.InheritedExceptionBlockType, Is.EqualTo(ExceptionBlockType.None));
            Assert.That(scope.InheritedExceptionStatement, Is.Null);
        }
Esempio n. 16
0
        public void Current_RootScope_CreateAndDispose()
        {
            //-- Act

            var scope = new StatementScope(m_Class, m_Method, new StatementBlock());

            var existsInside  = StatementScope.Exists;
            var currentInside = StatementScope.Current;

            scope.Dispose();

            var existsAfter = StatementScope.Exists;

            //-- Assert

            Assert.That(existsInside, Is.True);
            Assert.That(currentInside, Is.SameAs(scope));
            Assert.That(existsAfter, Is.False);
        }
Esempio n. 17
0
        public void FindOutermostExceptionScopeWithin_RootHomeException_Found()
        {
            //-- Arrange

            var rootScope = new StatementScope(m_Class, m_Method, new StatementBlock());
            var homeScope = new StatementScope(new StatementBlock());

            var tryStatement = new TryStatement(() => { });
            var tryScope     = new StatementScope(new StatementBlock(), tryStatement, ExceptionBlockType.Try);

            //-- Act

            var result = StatementScope.Current.FindOutermostTryStatementWithin(homeScope);

            //-- Assert

            Assert.That(StatementScope.Current.Depth, Is.EqualTo(2));
            Assert.That(result, Is.SameAs(tryStatement));
        }
Esempio n. 18
0
        public void ScopeNestedInExceptionScope_ThisHasNoValue_InheritedHasValue()
        {
            //-- Arrange

            var rootScope    = new StatementScope(m_Class, m_Method, new StatementBlock());
            var tryStatement = new TryStatement(() => { });
            var tryScope     = new StatementScope(new StatementBlock(), tryStatement, ExceptionBlockType.Try);

            //-- Act

            var innerScope = new StatementScope(new StatementBlock());

            //-- Assert

            Assert.That(StatementScope.Current.ThisExceptionBlockType, Is.EqualTo(ExceptionBlockType.None));
            Assert.That(StatementScope.Current.ThisExceptionStatement, Is.Null);
            Assert.That(StatementScope.Current.InheritedExceptionBlockType, Is.EqualTo(ExceptionBlockType.Try));
            Assert.That(StatementScope.Current.InheritedExceptionStatement, Is.SameAs(tryStatement));
        }
Esempio n. 19
0
        public void FindOutermostExceptionScopeWithin_RootFinallyHomeCatch_DoNotThrow_Found()
        {
            //-- Arrange

            var rootScope = new StatementScope(m_Class, m_Method, new StatementBlock());

            var outerTryStatement = new TryStatement(() => { });
            var outerFinallyScope = new StatementScope(new StatementBlock(), outerTryStatement, ExceptionBlockType.Finally);

            var homeScope = new StatementScope(new StatementBlock());

            var innerTryStatement = new TryStatement(() => { });
            var innerCatchScope   = new StatementScope(new StatementBlock(), innerTryStatement, ExceptionBlockType.Catch);

            //-- Act

            var result = StatementScope.Current.FindOutermostTryStatementWithin(homeScope);

            //-- Assert

            Assert.That(result, Is.SameAs(innerTryStatement));
        }
Esempio n. 20
0
        public void Current_NestedScope_CreateAndDispose()
        {
            //-- Arrange

            var rootScope = new StatementScope(m_Class, m_Method, new StatementBlock());

            //-- Act

            var nestedScope = new StatementScope(new StatementBlock());

            var existsInsideNested   = StatementScope.Exists;
            var currentInsideNested  = StatementScope.Current;
            var previousInsideNested = StatementScope.Current.Previous;
            var rootInsideNested     = StatementScope.Current.Root;
            var depthInsideNested    = StatementScope.Current.Depth;

            nestedScope.Dispose();

            var existsAfterNested   = StatementScope.Exists;
            var currentAfterNested  = StatementScope.Current;
            var previousAfterNested = StatementScope.Current.Previous;
            var rootAfterNested     = StatementScope.Current.Root;
            var depthAfterNested    = StatementScope.Current.Depth;

            //-- Assert

            Assert.That(existsInsideNested, Is.True);
            Assert.That(currentInsideNested, Is.SameAs(nestedScope));
            Assert.That(previousInsideNested, Is.SameAs(rootScope));
            Assert.That(rootInsideNested, Is.SameAs(rootScope));
            Assert.That(depthInsideNested, Is.EqualTo(1));

            Assert.That(existsAfterNested, Is.True);
            Assert.That(currentAfterNested, Is.SameAs(rootScope));
            Assert.That(previousAfterNested, Is.Null);
            Assert.That(rootAfterNested, Is.SameAs(rootScope));
            Assert.That(depthAfterNested, Is.EqualTo(0));
        }
Esempio n. 21
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        private void WriteClosureInstanceInitialization()
        {
            using (TypeTemplate.CreateScope <TypeTemplate.TClosure>(m_ClosureClass.TypeBuilder))
            {
                using (var scope = new StatementScope(m_HostScopeBlock, StatementScope.RewriteMode.On))
                {
                    var scopeRewriter   = m_HostMethod.TransparentWriter;
                    var closureInstance = scopeRewriter.Local <TypeTemplate.TClosure>();
                    closureInstance.Assign(new NewObjectExpression <TypeTemplate.TClosure>(m_ClosureClassConstructor, new IOperand[0]));

                    m_ClosureInstanceReference = closureInstance;

                    if (m_Parent != null)
                    {
                        WriteParentFieldInitialization();
                    }

                    foreach (var capture in m_Captures.Where(c => c.HoistingClosure == this))
                    {
                        WriteCaptureFieldInitialization(capture);
                    }
                }
            }
        }
Esempio n. 22
0
            //-------------------------------------------------------------------------------------------------------------------------------------------------

            public OperatorIncrement(UnaryOperatorPosition position, bool positive)
            {
                m_Position       = position;
                m_Positive       = positive;
                m_StatementScope = StatementScope.Current;
            }
Esempio n. 23
0
 public void TearDown()
 {
     StatementScope.Cleanup();
     m_Class  = null;
     m_Method = null;
 }
Esempio n. 24
0
 public void SetUp()
 {
     m_StatementScope = new StatementScope(ownerClass: null, writer: null, statementBlock: new StatementBlock());
 }