Example #1
0
 protected IteratorExpressionImpl(LexicalInfo lexicalInfo, Expression expression, Expression iterator, StatementModifier filter) : base(lexicalInfo)
 {
     _declarations = new DeclarationCollection(this);
     Expression    = expression;
     Iterator      = iterator;
     Filter        = filter;
 }
Example #2
0
 protected IteratorExpressionImpl(Expression expression, Expression iterator, StatementModifier filter)
 {
     _declarations = new DeclarationCollection(this);
     Expression    = expression;
     Iterator      = iterator;
     Filter        = filter;
 }
Example #3
0
 public DeclarationsNamespace(INamespace parent, Declaration declaration)
 {
     _parent       = parent;
     _declarations = new DeclarationCollection {
         declaration
     };
 }
Example #4
0
        /// <summary>
        /// Constructs a new file parse tree.
        /// </summary>
        /// <param name="type">The type of the file.</param>
        /// <param name="name">The name of the file.</param>
        /// <param name="declarations">The declarations in the file.</param>
        /// <param name="span">The location of the tree.</param>
        public FileTree(FileType type, string name, DeclarationCollection declarations, Span span) : base(TreeType.FileTree, span)
        {
            SetParent(declarations);

            this.FileType     = FileType.None;
            this.Name         = name;
            this.Declarations = declarations;
        }
Example #5
0
        public UnpackStatement CreateUnpackStatement(DeclarationCollection declarations, Expression expression)
        {
            UnpackStatement unpack = new UnpackStatement(expression.LexicalInfo);

            unpack.Declarations.Extend(declarations);
            unpack.Expression = expression;
            return(unpack);
        }
        /// <summary>
        /// Constructs a new parse tree for a custom property declaration.
        /// </summary>
        /// <param name="attributes">The attributes on the declaration.</param>
        /// <param name="modifiers">The modifiers on the declaration.</param>
        /// <param name="customLocation">The location of the 'Custom' keyword.</param>
        /// <param name="keywordLocation">The location of the keyword.</param>
        /// <param name="name">The name of the custom event.</param>
        /// <param name="asLocation">The location of the 'As', if any.</param>
        /// <param name="resultType">The result type, if any.</param>
        /// <param name="implementsList">The implements list.</param>
        /// <param name="accessors">The custom event accessors.</param>
        /// <param name="endDeclaration">The End Event declaration, if any.</param>
        /// <param name="span">The location of the parse tree.</param>
        /// <param name="comments">The comments for the parse tree.</param>
        public CustomEventDeclaration(AttributeBlockCollection attributes, ModifierCollection modifiers, Location customLocation, Location keywordLocation, SimpleName name, Location asLocation, TypeName resultType, NameCollection implementsList, DeclarationCollection accessors, EndBlockDeclaration endDeclaration,
                                      Span span, IList <Comment> comments) : base(TreeType.CustomEventDeclaration, attributes, modifiers, keywordLocation, name, null, null, asLocation, null, resultType,
                                                                                  span, comments)
        {
            SetParent(accessors);
            SetParent(endDeclaration);
            SetParent(implementsList);

            _CustomLocation = customLocation;
            _ImplementsList = implementsList;
            _Accessors      = accessors;
            _EndDeclaration = endDeclaration;
        }
Example #7
0
        protected PlSqlBlockStatement(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            Declarations      = new DeclarationCollection();
            ExceptionHandlers = new ExceptionHandlerCollection();

            var decls = (SqlStatement[])info.GetValue("Declarations", typeof(SqlStatement[]));

            foreach (var statement in decls)
            {
                Declarations.Add(statement);
            }

            var handlers = (ExceptionHandler[])info.GetValue("ExceptionHandlers", typeof(ExceptionHandler[]));

            foreach (var handler in handlers)
            {
                ExceptionHandlers.Add(handler);
            }
        }
Example #8
0
 /// <summary>
 /// Constructs a new file parse tree.
 /// </summary>
 /// <param name="declarations">The declarations in the file.</param>
 /// <param name="span">The location of the tree.</param>
 public FileTree(DeclarationCollection declarations, Span span) : this(FileType.None, string.Empty, declarations, span)
 {
 }
Example #9
0
        /// <summary>
        /// Optimize the <c>for item in range()</c> construct
        /// </summary>
        /// <param name="node">the for statement to check</param>
        private void CheckForItemInRangeLoop(ForStatement node)
        {
            MethodInvocationExpression mi = node.Iterator as MethodInvocationExpression;

            if (null == mi)
            {
                return;
            }
            if (!IsRangeInvocation(mi))
            {
                return;
            }

            DeclarationCollection declarations = node.Declarations;

            if (declarations.Count != 1)
            {
                return;
            }

            ExpressionCollection args = mi.Arguments;
            Block body = new Block(node.LexicalInfo);

            Expression min;
            Expression max;
            Expression step;

            if (args.Count == 1)
            {
                min  = CodeBuilder.CreateIntegerLiteral(0);
                max  = args[0];
                step = CodeBuilder.CreateIntegerLiteral(1);
            }
            else if (args.Count == 2)
            {
                min  = args[0];
                max  = args[1];
                step = CodeBuilder.CreateIntegerLiteral(1);
            }
            else
            {
                min  = args[0];
                max  = args[1];
                step = args[2];
            }

            InternalLocal numVar = CodeBuilder.DeclareTempLocal(
                _currentMethod,
                TypeSystemServices.IntType);
            Expression numRef = CodeBuilder.CreateReference(numVar);

            // __num = <min>
            body.Add(
                CodeBuilder.CreateAssignment(
                    numRef,
                    min));

            Expression endRef;

            if (max.NodeType == NodeType.IntegerLiteralExpression)
            {
                endRef = max;
            }
            else
            {
                InternalLocal endVar = CodeBuilder.DeclareTempLocal(
                    _currentMethod,
                    TypeSystemServices.IntType);
                endRef = CodeBuilder.CreateReference(endVar);

                // __end = <end>
                body.Add(
                    CodeBuilder.CreateAssignment(
                        endRef,
                        max));
            }

            if (args.Count == 1)
            {
                if (max.NodeType == NodeType.IntegerLiteralExpression)
                {
                    if (((IntegerLiteralExpression)max).Value < 0)
                    {
                        // raise ArgumentOutOfRangeException("max") (if <max> < 0)
                        Statement statement = CodeBuilder.RaiseException(
                            body.LexicalInfo,
                            TypeSystemServices.Map(System_ArgumentOutOfRangeException_ctor),
                            CodeBuilder.CreateStringLiteral("max"));

                        body.Add(statement);
                    }
                }
                else
                {
                    IfStatement ifStatement = new IfStatement(body.LexicalInfo);
                    ifStatement.TrueBlock = new Block();

                    // raise ArgumentOutOfRangeException("max") if __end < 0
                    Statement statement = CodeBuilder.RaiseException(
                        body.LexicalInfo,
                        TypeSystemServices.Map(System_ArgumentOutOfRangeException_ctor),
                        CodeBuilder.CreateStringLiteral("max"));

                    ifStatement.Condition = CodeBuilder.CreateBoundBinaryExpression(
                        TypeSystemServices.BoolType,
                        BinaryOperatorType.LessThan,
                        endRef,
                        CodeBuilder.CreateIntegerLiteral(0));

                    ifStatement.TrueBlock.Add(statement);

                    body.Add(ifStatement);
                }
            }

            Expression stepRef;

            switch (args.Count)
            {
            case 1:
                stepRef = CodeBuilder.CreateIntegerLiteral(1);
                break;

            case 2:
                if ((min.NodeType == NodeType.IntegerLiteralExpression) &&
                    (max.NodeType == NodeType.IntegerLiteralExpression) &&
                    (((IntegerLiteralExpression)max).Value < ((IntegerLiteralExpression)min).Value))
                {
                    // __step = -1
                    stepRef = CodeBuilder.CreateIntegerLiteral(-1);
                }
                else if ((min.NodeType == NodeType.IntegerLiteralExpression) &&
                         (max.NodeType == NodeType.IntegerLiteralExpression))
                {
                    // __step = 1
                    stepRef = CodeBuilder.CreateIntegerLiteral(1);
                }
                else
                {
                    InternalLocal stepVar = CodeBuilder.DeclareTempLocal(
                        _currentMethod,
                        TypeSystemServices.IntType);
                    stepRef = CodeBuilder.CreateReference(stepVar);

                    // __step = 1
                    body.Add(
                        CodeBuilder.CreateAssignment(
                            stepRef,
                            CodeBuilder.CreateIntegerLiteral(1)));

                    // __step = -1 if __end < __num
                    IfStatement ifStatement = new IfStatement(node.LexicalInfo);

                    ifStatement.Condition = CodeBuilder.CreateBoundBinaryExpression(
                        TypeSystemServices.BoolType,
                        BinaryOperatorType.LessThan,
                        endRef,
                        numRef);

                    ifStatement.TrueBlock = new Block();

                    ifStatement.TrueBlock.Add(
                        CodeBuilder.CreateAssignment(
                            stepRef,
                            CodeBuilder.CreateIntegerLiteral(-1)));

                    body.Add(ifStatement);
                }
                break;

            default:
                if (step.NodeType == NodeType.IntegerLiteralExpression)
                {
                    stepRef = step;
                }
                else
                {
                    InternalLocal stepVar = CodeBuilder.DeclareTempLocal(
                        _currentMethod,
                        TypeSystemServices.IntType);
                    stepRef = CodeBuilder.CreateReference(stepVar);

                    // __step = <step>
                    body.Add(
                        CodeBuilder.CreateAssignment(
                            stepRef,
                            step));
                }
                break;
            }


            if (args.Count == 3)
            {
                Expression condition = null;
                bool       run       = false;

                if (step.NodeType == NodeType.IntegerLiteralExpression)
                {
                    if (((IntegerLiteralExpression)step).Value < 0)
                    {
                        if ((max.NodeType == NodeType.IntegerLiteralExpression) &&
                            (min.NodeType == NodeType.IntegerLiteralExpression))
                        {
                            run = (((IntegerLiteralExpression)max).Value > ((IntegerLiteralExpression)min).Value);
                        }
                        else
                        {
                            condition = CodeBuilder.CreateBoundBinaryExpression(
                                TypeSystemServices.BoolType,
                                BinaryOperatorType.GreaterThan,
                                endRef,
                                numRef);
                        }
                    }
                    else
                    {
                        if ((max.NodeType == NodeType.IntegerLiteralExpression) &&
                            (min.NodeType == NodeType.IntegerLiteralExpression))
                        {
                            run = (((IntegerLiteralExpression)max).Value < ((IntegerLiteralExpression)min).Value);
                        }
                        else
                        {
                            condition = CodeBuilder.CreateBoundBinaryExpression(
                                TypeSystemServices.BoolType,
                                BinaryOperatorType.LessThan,
                                endRef,
                                numRef);
                        }
                    }
                }
                else
                {
                    if ((max.NodeType == NodeType.IntegerLiteralExpression) &&
                        (min.NodeType == NodeType.IntegerLiteralExpression))
                    {
                        if (((IntegerLiteralExpression)max).Value < ((IntegerLiteralExpression)min).Value)
                        {
                            condition = CodeBuilder.CreateBoundBinaryExpression(
                                TypeSystemServices.BoolType,
                                BinaryOperatorType.GreaterThan,
                                stepRef,
                                CodeBuilder.CreateIntegerLiteral(0));
                        }
                        else
                        {
                            condition = CodeBuilder.CreateBoundBinaryExpression(
                                TypeSystemServices.BoolType,
                                BinaryOperatorType.LessThan,
                                stepRef,
                                CodeBuilder.CreateIntegerLiteral(0));
                        }
                    }
                    else
                    {
                        condition = CodeBuilder.CreateBoundBinaryExpression(
                            TypeSystemServices.BoolType,
                            BinaryOperatorType.Or,
                            CodeBuilder.CreateBoundBinaryExpression(
                                TypeSystemServices.BoolType,
                                BinaryOperatorType.And,
                                CodeBuilder.CreateBoundBinaryExpression(
                                    TypeSystemServices.BoolType,
                                    BinaryOperatorType.LessThan,
                                    stepRef,
                                    CodeBuilder.CreateIntegerLiteral(0)),
                                CodeBuilder.CreateBoundBinaryExpression(
                                    TypeSystemServices.BoolType,
                                    BinaryOperatorType.GreaterThan,
                                    endRef,
                                    numRef)),
                            CodeBuilder.CreateBoundBinaryExpression(
                                TypeSystemServices.BoolType,
                                BinaryOperatorType.And,
                                CodeBuilder.CreateBoundBinaryExpression(
                                    TypeSystemServices.BoolType,
                                    BinaryOperatorType.GreaterThan,
                                    stepRef,
                                    CodeBuilder.CreateIntegerLiteral(0)),
                                CodeBuilder.CreateBoundBinaryExpression(
                                    TypeSystemServices.BoolType,
                                    BinaryOperatorType.LessThan,
                                    endRef,
                                    numRef)));
                    }
                }

                // raise ArgumentOutOfRangeException("step") if (__step < 0 and __end > __begin) or (__step > 0 and __end < __begin)
                Statement statement = CodeBuilder.RaiseException(
                    body.LexicalInfo,
                    TypeSystemServices.Map(System_ArgumentOutOfRangeException_ctor),
                    CodeBuilder.CreateStringLiteral("step"));

                if (condition != null)
                {
                    IfStatement ifStatement = new IfStatement(body.LexicalInfo);
                    ifStatement.TrueBlock = new Block();

                    ifStatement.Condition = condition;

                    ifStatement.TrueBlock.Add(statement);

                    body.Add(ifStatement);
                }
                else if (run)
                {
                    body.Add(statement);
                }

                // __end = __num + __step * cast(int, Math.Ceiling((__end - __num)/cast(double, __step)))
                if ((step.NodeType == NodeType.IntegerLiteralExpression) &&
                    (max.NodeType == NodeType.IntegerLiteralExpression) &&
                    (min.NodeType == NodeType.IntegerLiteralExpression))
                {
                    int stepVal = (int)((IntegerLiteralExpression)step).Value;
                    int maxVal  = (int)((IntegerLiteralExpression)max).Value;
                    int minVal  = (int)((IntegerLiteralExpression)min).Value;
                    endRef = CodeBuilder.CreateIntegerLiteral(
                        minVal + stepVal * (int)System.Math.Ceiling((maxVal - minVal) / ((double)stepVal)));
                }
                else
                {
                    Expression endBak = endRef;
                    if (max.NodeType == NodeType.IntegerLiteralExpression)
                    {
                        InternalLocal endVar = CodeBuilder.DeclareTempLocal(
                            _currentMethod,
                            TypeSystemServices.IntType);
                        endRef = CodeBuilder.CreateReference(endVar);
                    }

                    body.Add(
                        CodeBuilder.CreateAssignment(
                            endRef,
                            CodeBuilder.CreateBoundBinaryExpression(
                                TypeSystemServices.IntType,
                                BinaryOperatorType.Addition,
                                numRef,
                                CodeBuilder.CreateBoundBinaryExpression(
                                    TypeSystemServices.IntType,
                                    BinaryOperatorType.Multiply,
                                    stepRef,
                                    CodeBuilder.CreateCast(
                                        TypeSystemServices.IntType,
                                        CodeBuilder.CreateMethodInvocation(
                                            TypeSystemServices.Map(System_Math_Ceiling),
                                            CodeBuilder.CreateBoundBinaryExpression(
                                                TypeSystemServices.DoubleType,
                                                BinaryOperatorType.Division,
                                                CodeBuilder.CreateBoundBinaryExpression(
                                                    TypeSystemServices.IntType,
                                                    BinaryOperatorType.Subtraction,
                                                    endBak,
                                                    numRef),
                                                CodeBuilder.CreateCast(
                                                    TypeSystemServices.DoubleType,
                                                    stepRef))))))));
                }
            }

            // while __num != __end:
            WhileStatement ws = new WhileStatement(node.LexicalInfo);

            BinaryOperatorType op = BinaryOperatorType.Inequality;

            if (stepRef.NodeType == NodeType.IntegerLiteralExpression)
            {
                if (((IntegerLiteralExpression)stepRef).Value > 0)
                {
                    op = BinaryOperatorType.LessThan;
                }
                else
                {
                    op = BinaryOperatorType.GreaterThan;
                }
            }

            ws.Condition = CodeBuilder.CreateBoundBinaryExpression(
                TypeSystemServices.BoolType,
                op,
                numRef,
                endRef);
            ws.Condition.LexicalInfo = node.LexicalInfo;

            //	item = __num
            ws.Block.Add(
                CodeBuilder.CreateAssignment(
                    CodeBuilder.CreateReference((InternalLocal)declarations[0].Entity),
                    numRef));

            Block rawBlock = new Block();

            rawBlock["checked"] = false;

            //  __num += __step
            rawBlock.Add(
                CodeBuilder.CreateAssignment(
                    numRef,
                    CodeBuilder.CreateBoundBinaryExpression(
                        TypeSystemServices.IntType,
                        BinaryOperatorType.Addition,
                        numRef,
                        stepRef)));

            ws.Block.Add(rawBlock as Statement);

            //	<block>
            ws.Block.Add(node.Block);

            body.Add(ws);

            ReplaceCurrentNode(body);
        }
Example #10
0
 protected IteratorExpressionImpl(LexicalInfo lexicalInfo) : base(lexicalInfo)
 {
     _declarations = new DeclarationCollection(this);
 }
Example #11
0
 protected ForStatementImpl(LexicalInfo lexicalInfo) : base(lexicalInfo)
 {
     _declarations = new DeclarationCollection(this);
     Block         = new Block();
 }
 public static void UnpackExpression(BooCodeBuilder codeBuilder, Method method, Block block, Expression expression, DeclarationCollection declarations)
 {
     if (expression.ExpressionType.IsArray)
     {
         UnpackArray(codeBuilder, method, block, expression, declarations);
     }
     else
     {
         UnpackEnumerable(codeBuilder, method, block, expression, declarations);
     }
 }
        public static void UnpackArray(BooCodeBuilder codeBuilder, Method method, Block block, Expression expression, DeclarationCollection declarations)
        {
            ILocalEntity local = expression.Entity as ILocalEntity;

            if (null == local)
            {
                local = codeBuilder.DeclareTempLocal(method,
                                                     expression.ExpressionType);
                block.Add(
                    codeBuilder.CreateAssignment(
                        codeBuilder.CreateReference(local),
                        expression));
            }
            for (int i = 0; i < declarations.Count; ++i)
            {
                Declaration declaration = declarations[i];
                block.Add(
                    codeBuilder.CreateAssignment(
                        codeBuilder.CreateReference(
                            declaration.Entity),
                        codeBuilder.CreateSlicing(
                            codeBuilder.CreateReference(local),
                            i)));
            }
        }
Example #14
0
 protected UnpackStatementImpl(LexicalInfo lexicalInfo, Expression expression) : base(lexicalInfo)
 {
     _declarations = new DeclarationCollection(this);
     Expression    = expression;
 }
        override public void LeaveForStatement(ForStatement node)
        {
            _iteratorNode         = node.Iterator;
            CurrentEnumeratorType = GetExpressionType(node.Iterator);

            if (null == CurrentBestEnumeratorType)
            {
                return;                 //error
            }
            DeclarationCollection declarations = node.Declarations;
            Block body = new Block(node.LexicalInfo);

            InternalLocal iterator = CodeBuilder.DeclareLocal(_current,
                                                              Context.GetUniqueName("iterator"),
                                                              CurrentBestEnumeratorType);

            if (CurrentBestEnumeratorType == CurrentEnumeratorType)
            {
                //$iterator = <node.Iterator>
                body.Add(
                    CodeBuilder.CreateAssignment(
                        node.LexicalInfo,
                        CodeBuilder.CreateReference(iterator),
                        node.Iterator));
            }
            else
            {
                //$iterator = <node.Iterator>.GetEnumerator()
                body.Add(
                    CodeBuilder.CreateAssignment(
                        node.LexicalInfo,
                        CodeBuilder.CreateReference(iterator),
                        CodeBuilder.CreateMethodInvocation(node.Iterator, CurrentBestGetEnumerator)));
            }

            // while __iterator.MoveNext():
            if (null == CurrentBestMoveNext)
            {
                return;                 //error
            }
            WhileStatement ws = new WhileStatement(node.LexicalInfo);

            ws.Condition = CodeBuilder.CreateMethodInvocation(
                CodeBuilder.CreateReference(iterator),
                CurrentBestMoveNext);

            if (null == CurrentBestGetCurrent)
            {
                return;                 //error
            }
            Expression current = CodeBuilder.CreateMethodInvocation(
                CodeBuilder.CreateReference(iterator),
                CurrentBestGetCurrent);

            if (1 == declarations.Count)
            {
                //	item = __iterator.Current
                ws.Block.Add(
                    CodeBuilder.CreateAssignment(
                        node.LexicalInfo,
                        CodeBuilder.CreateReference((InternalLocal)declarations[0].Entity),
                        current));
            }
            else
            {
                UnpackExpression(ws.Block,
                                 CodeBuilder.CreateCast(
                                     CurrentEnumeratorItemType,
                                     current),
                                 node.Declarations);
            }

            ws.Block.Add(node.Block);
            ws.OrBlock   = node.OrBlock;
            ws.ThenBlock = node.ThenBlock;

            // try:
            //   while...
            // ensure:
            //   d = iterator as IDisposable
            //   d.Dispose() unless d is null
            if (IsAssignableFrom(TypeSystemServices.IDisposableType, CurrentBestEnumeratorType))
            {
                TryStatement tryStatement = new TryStatement();
                tryStatement.ProtectedBlock.Add(ws);
                tryStatement.EnsureBlock = new Block();

                CastExpression castExpression = new CastExpression();
                castExpression.Type           = CodeBuilder.CreateTypeReference(TypeSystemServices.IDisposableType);
                castExpression.Target         = CodeBuilder.CreateReference(iterator);
                castExpression.ExpressionType = TypeSystemServices.IDisposableType;
                tryStatement.EnsureBlock.Add(
                    CodeBuilder.CreateMethodInvocation(castExpression, IDisposable_Dispose));

                body.Add(tryStatement);
            }
            else
            {
                body.Add(ws);
            }

            ReplaceCurrentNode(body);
        }
Example #16
0
 protected UnpackStatementImpl()
 {
     _declarations = new DeclarationCollection(this);
 }
Example #17
0
 protected UnpackStatementImpl(Expression expression)
 {
     _declarations = new DeclarationCollection(this);
     Expression    = expression;
 }
Example #18
0
 protected IteratorExpressionImpl()
 {
     _declarations = new DeclarationCollection(this);
 }
Example #19
0
 public DeclarationsNamespace(INamespace parent, TypeSystemServices tagManager, Declaration declaration)
 {
     _parent       = parent;
     _declarations = new DeclarationCollection();
     _declarations.Add(declaration);
 }
Example #20
0
 public DeclarationsNamespace(INamespace parent, TypeSystemServices tagManager, DeclarationCollection declarations)
 {
     _parent       = parent;
     _declarations = declarations;
 }
Example #21
0
 /// <summary>
 /// Unpacks an expression onto a list of declarations.
 /// </summary>
 /// <param name="block">Block this takes place in</param>
 /// <param name="expression">expression to explode</param>
 /// <param name="declarations">list of declarations to set</param>
 void UnpackExpression(Block block, Expression expression, DeclarationCollection declarations)
 {
     NormalizeIterationStatements.UnpackExpression(CodeBuilder, _currentMethod, block, expression, declarations);
 }
Example #22
0
 protected ForStatementImpl(LexicalInfo lexicalInfo, Expression iterator) : base(lexicalInfo)
 {
     _declarations = new DeclarationCollection(this);
     Block         = new Block();
     Iterator      = iterator;
 }
Example #23
0
        override public void LeaveForStatement(ForStatement node)
        {
            IType enumeratorType               = GetExpressionType(node.Iterator);
            IType enumeratorItemType           = TypeSystemServices.GetEnumeratorItemType(enumeratorType);
            DeclarationCollection declarations = node.Declarations;
            Block body = new Block(node.LexicalInfo);

            InternalLocal iterator = CodeBuilder.DeclareLocal(
                _current,
                "___iterator" + _context.AllocIndex(),
                TypeSystemServices.IEnumeratorType);

            if (TypeSystemServices.IEnumeratorType.IsAssignableFrom(enumeratorType))
            {
                body.Add(
                    CodeBuilder.CreateAssignment(
                        CodeBuilder.CreateReference(iterator),
                        node.Iterator));
            }
            else
            {
                // ___iterator = <node.Iterator>.GetEnumerator()
                body.Add(
                    CodeBuilder.CreateAssignment(
                        CodeBuilder.CreateReference(iterator),
                        CodeBuilder.CreateMethodInvocation(
                            node.Iterator,
                            IEnumerable_GetEnumerator)));
            }

            // while __iterator.MoveNext():
            WhileStatement ws = new WhileStatement(node.LexicalInfo);

            ws.Condition = CodeBuilder.CreateMethodInvocation(
                CodeBuilder.CreateReference(iterator),
                IEnumerator_MoveNext);

            Expression current = CodeBuilder.CreateMethodInvocation(
                CodeBuilder.CreateReference(iterator),
                IEnumerator_get_Current);

            if (1 == declarations.Count)
            {
                //	item = __iterator.Current
                ws.Block.Add(
                    CodeBuilder.CreateAssignment(
                        CodeBuilder.CreateReference((InternalLocal)declarations[0].Entity),
                        current));
            }
            else
            {
                UnpackExpression(ws.Block,
                                 CodeBuilder.CreateCast(
                                     enumeratorItemType,
                                     current),
                                 node.Declarations);
            }

            ws.Block.Add(node.Block);

            body.Add(ws);

            ReplaceCurrentNode(body);
        }
Example #24
0
 public DeclarationsNamespace(BindingManager bindingManager, DeclarationCollection declarations)
 {
     _bindingManager = bindingManager;
     _declarations   = declarations;
 }
 void UnpackExpression(Block block, Expression expression, DeclarationCollection declarations)
 {
     UnpackExpression(CodeBuilder, _current, block, expression, declarations);
 }
Example #26
0
 public DeclarationsNamespace(BindingManager bindingManager, Declaration declaration)
 {
     _bindingManager = bindingManager;
     _declarations   = new DeclarationCollection();
     _declarations.Add(declaration);
 }
        public static void UnpackEnumerable(BooCodeBuilder codeBuilder, Method method, Block block, Expression expression, DeclarationCollection declarations)
        {
            TypeSystemServices tss = codeBuilder.TypeSystemServices;

            InternalLocal local = codeBuilder.DeclareTempLocal(method,
                                                               tss.IEnumeratorType);

            IType expressionType = expression.ExpressionType;

            if (expressionType.IsSubclassOf(codeBuilder.TypeSystemServices.IEnumeratorType))
            {
                block.Add(
                    codeBuilder.CreateAssignment(
                        codeBuilder.CreateReference(local),
                        expression));
            }
            else
            {
                if (!expressionType.IsSubclassOf(codeBuilder.TypeSystemServices.IEnumerableType))
                {
                    expression = codeBuilder.CreateMethodInvocation(
                        RuntimeServices_GetEnumerable, expression);
                }

                block.Add(
                    codeBuilder.CreateAssignment(
                        block.LexicalInfo,
                        codeBuilder.CreateReference(local),
                        codeBuilder.CreateMethodInvocation(
                            expression, IEnumerable_GetEnumerator)));
            }

            for (int i = 0; i < declarations.Count; ++i)
            {
                Declaration declaration = declarations[i];

                block.Add(
                    codeBuilder.CreateAssignment(
                        codeBuilder.CreateReference(declaration.Entity),
                        codeBuilder.CreateMethodInvocation(RuntimeServices_MoveNext,
                                                           codeBuilder.CreateReference(local))));
            }
        }
Example #28
0
        private void CreateMoveNext()
        {
            BooMethodBuilder method = _enumerator.AddVirtualMethod("MoveNext", TypeSystemServices.BoolType);

            Expression moveNext = CodeBuilder.CreateMethodInvocation(
                CodeBuilder.CreateReference((InternalField)_enumeratorField.Entity),
                TypeSystemServices.Map(Methods.InstanceFunctionOf <IEnumerator, bool>(e => e.MoveNext)));

            Expression current = CodeBuilder.CreateMethodInvocation(
                CodeBuilder.CreateReference((InternalField)_enumeratorField.Entity),
                ((IProperty)GetMember(_sourceEnumeratorType, "Current", EntityType.Property)).GetGetMethod());

            Statement filter = null;
            Statement stmt;
            Block     outerBlock;
            Block     innerBlock;

            if (null == _generator.Filter)
            {
                IfStatement istmt = new IfStatement(moveNext, new Block(), null);
                outerBlock = innerBlock = istmt.TrueBlock;

                stmt = istmt;
            }
            else
            {
                WhileStatement wstmt = new WhileStatement(moveNext);
                outerBlock = wstmt.Block;

                if (StatementModifierType.If == _generator.Filter.Type)
                {
                    IfStatement ifstmt = new IfStatement(_generator.Filter.Condition, new Block(), null);
                    innerBlock = ifstmt.TrueBlock;
                    filter     = ifstmt;
                }
                else
                {
                    UnlessStatement ustmt = new UnlessStatement(_generator.Filter.Condition);
                    innerBlock = ustmt.Block;
                    filter     = ustmt;
                }

                stmt = wstmt;
            }

            DeclarationCollection declarations = _generator.Declarations;

            if (declarations.Count > 1)
            {
                NormalizeIterationStatements.UnpackExpression(CodeBuilder,
                                                              method.Method,
                                                              outerBlock,
                                                              current,
                                                              declarations);

                foreach (Declaration declaration in declarations)
                {
                    method.Locals.Add(((InternalLocal)declaration.Entity).Local);
                }
            }
            else
            {
                InternalLocal local = (InternalLocal)declarations[0].Entity;
                method.Locals.Add(local.Local);
                outerBlock.Add(CodeBuilder.CreateAssignment(
                                   CodeBuilder.CreateReference(local),
                                   current));
            }

            if (null != filter)
            {
                outerBlock.Add(filter);
            }

            innerBlock.Add(CodeBuilder.CreateAssignment(
                               CodeBuilder.CreateReference((InternalField)_current.Entity),
                               _generator.Expression));
            innerBlock.Add(new ReturnStatement(new BoolLiteralExpression(true)));

            method.Body.Add(stmt);
            method.Body.Add(new ReturnStatement(new BoolLiteralExpression(false)));
        }
Example #29
0
 protected UnpackStatementImpl(LexicalInfo lexicalInfo) : base(lexicalInfo)
 {
     _declarations = new DeclarationCollection(this);
 }
Example #30
0
 public DeclarationsNamespace(INamespace parent, DeclarationCollection declarations)
 {
     _parent       = parent;
     _declarations = declarations;
 }