PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/IronPython/Compiler/Ast/AndExpression.cs

#
C# | 106 lines | 78 code | 14 blank | 14 comment | 7 complexity | f112d9edc0b86e17747be290e1006381 MD5 | raw file
  1/* ****************************************************************************
  2 *
  3 * Copyright (c) Microsoft Corporation. 
  4 *
  5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
  6 * copy of the license can be found in the License.html file at the root of this distribution. If 
  7 * you cannot locate the  Apache License, Version 2.0, please send an email to 
  8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  9 * by the terms of the Apache License, Version 2.0.
 10 *
 11 * You must not remove this notice, or any other, from this software.
 12 *
 13 *
 14 * ***************************************************************************/
 15
 16#if !CLR2
 17using MSAst = System.Linq.Expressions;
 18#else
 19using MSAst = Microsoft.Scripting.Ast;
 20#endif
 21
 22using System;
 23using IronPython.Runtime.Binding;
 24using Microsoft.Scripting.Actions;
 25using Microsoft.Scripting.Utils;
 26using AstUtils = Microsoft.Scripting.Ast.Utils;
 27
 28namespace IronPython.Compiler.Ast {
 29    using Ast = MSAst.Expression;
 30
 31    public class AndExpression : Expression {
 32        private readonly Expression _left, _right;
 33
 34        public AndExpression(Expression left, Expression right) {
 35            ContractUtils.RequiresNotNull(left, "left");
 36            ContractUtils.RequiresNotNull(right, "right");
 37
 38            _left = left;
 39            _right = right;
 40            StartIndex = left.StartIndex;
 41            EndIndex = right.EndIndex;
 42        }
 43
 44        public Expression Left {
 45            get { return _left; }
 46        }
 47
 48        public Expression Right {
 49            get { return _right; }
 50        } 
 51
 52        public override MSAst.Expression Reduce() {
 53            MSAst.Expression left = _left;
 54            MSAst.Expression right = _right;
 55
 56            Type t = Type;
 57            MSAst.ParameterExpression tmp = Ast.Variable(t, "__all__");
 58
 59            return Ast.Block(
 60                new [] { tmp },
 61                Ast.Condition(
 62                    GlobalParent.Convert(
 63                        typeof(bool),
 64                        ConversionResultKind.ExplicitCast,
 65                        Ast.Assign(
 66                            tmp,
 67                            AstUtils.Convert(
 68                                left,
 69                                t
 70                            )
 71                        )
 72                    ),
 73                    AstUtils.Convert(
 74                        right,
 75                        t
 76                    ),
 77                    tmp
 78                )
 79            );
 80        }
 81
 82        public override Type Type {
 83            get {
 84                return _left.Type == _right.Type ? _left.Type : typeof(object);
 85            }
 86        }
 87
 88        public override void Walk(PythonWalker walker) {
 89            if (walker.Walk(this)) {
 90                if (_left != null) {
 91                    _left.Walk(walker);
 92                }
 93                if (_right != null) {
 94                    _right.Walk(walker);
 95                }
 96            }
 97            walker.PostWalk(this);
 98        }
 99
100        internal override bool CanThrow {
101            get {
102                return _left.CanThrow || _right.CanThrow;
103            }
104        }
105    }
106}