private void EatOptionValue(ISnapshot snapshot, ExpressionKind kind, ICSharpExpression optionValue)
 {
     var addableKinds = new[] { ExpressionKind.Target, ExpressionKind.Stub, ExpressionKind.Mock };
     if (addableKinds.Contains(kind))
     {
         snapshot.Add(kind, optionValue);
     }
 }
        public ExpressionKind ReferenceKindByParentReferenceKindTest(ExpressionKind kind)
        {
            // Arrange
            var helper = new ExpressionKindHelper();

            // Assert
            return helper.ReferenceKindByParentReferenceKind(kind);
        }
        public ExpressionKind KindOfAssignmentTest(ExpressionKind kind)
        {
            // Arrange
            var helper = new ExpressionKindHelper();

            // Assert
            return helper.KindOfAssignment(kind);
        }
Beispiel #4
0
        public EXPRUNARYOP CreateUnaryOp(ExpressionKind exprKind, CType pType, EXPR pOperand)
        {
            Debug.Assert(exprKind.isUnaryOperator());
            Debug.Assert(pOperand != null);
            EXPRUNARYOP rval = new EXPRUNARYOP();

            rval.kind  = exprKind;
            rval.type  = pType;
            rval.flags = 0;
            rval.Child = pOperand;
            rval.OptionalUserDefinedCall = null;
            rval.UserDefinedCallMethod   = null;
            Debug.Assert(rval != null);
            return(rval);
        }
        private bool TryNameByToMethod(string methodName, ExpressionKind expressionKind)
        {
            int index = methodName.IndexOf("To", 0);

            while (index >= 0)
            {
                if ((index + 2 < methodName.Length) && (char.IsUpper(methodName[index + 2])))
                {
                    SuggestNameForMethod(expressionKind, methodName, index + 2);
                    return(true);
                }
                index = methodName.IndexOf("To", index + 1);
            }
            return(false);
        }
Beispiel #6
0
        public ExprUnaryOp CreateUnaryOp(ExpressionKind exprKind, CType pType, Expr pOperand)
        {
            Debug.Assert(exprKind.isUnaryOperator());
            Debug.Assert(pOperand != null);
            ExprUnaryOp rval = new ExprUnaryOp();

            rval.Kind  = exprKind;
            rval.Type  = pType;
            rval.Flags = 0;
            rval.Child = pOperand;
            rval.OptionalUserDefinedCall = null;
            rval.UserDefinedCallMethod   = null;
            Debug.Assert(rval != null);
            return(rval);
        }
Beispiel #7
0
        public ExpressionKind RetrunKindTest(ExpressionKind leftKind, ExpressionKind rightKind)
        {
            // Arrange
            var snapshot         = Mock.Of <ISnapshot>();
            var left             = Mock.Of <ICSharpExpression>();
            var right            = Mock.Of <ICSharpExpression>();
            var binaryExpression = Mock.Of <IBinaryExpression>(t => t.LeftOperand == left && t.RightOperand == right);

            var eater = Mock.Of <IEater>(t => t.Eat(snapshot, left) == leftKind &&
                                         t.Eat(snapshot, right) == rightKind);
            var binaryExpressionEater = new BinaryExpressionEater(eater);

            // Assert
            return(binaryExpressionEater.Eat(snapshot, binaryExpression));
        }
                /// <summary>
                /// Creates an operation call from the conditional control API, given information
                /// about which operation to call and with what arguments.
                /// </summary>
                private TypedExpression CreateControlCall(BuiltIn opInfo, IEnumerable <OpProperty> properties, TypedExpression args, IEnumerable <ResolvedType> typeArgs)
                {
                    var characteristics = new CallableInformation(
                        ResolvedCharacteristics.FromProperties(properties),
                        new InferredCallableInformation(((BuiltInKind.Operation)opInfo.Kind).IsSelfAdjoint, false));

                    var unitType      = ResolvedType.New(ResolvedTypeKind.UnitType);
                    var operationType = ResolvedType.New(ResolvedTypeKind.NewOperation(
                                                             Tuple.Create(args.ResolvedType, unitType),
                                                             characteristics));

                    // Build the surrounding control call
                    var identifier = new TypedExpression(
                        ExpressionKind.NewIdentifier(
                            Identifier.NewGlobalCallable(opInfo.FullName),
                            typeArgs.Any()
                            ? QsNullable <ImmutableArray <ResolvedType> > .NewValue(typeArgs.ToImmutableArray())
                            : QsNullable <ImmutableArray <ResolvedType> > .Null),
                        typeArgs
                        .Zip(((BuiltInKind.Operation)opInfo.Kind).TypeParameters, (type, param) => Tuple.Create(opInfo.FullName, param, type))
                        .ToImmutableArray(),
                        operationType,
                        new InferredExpressionInformation(false, false),
                        QsNullable <Tuple <QsPositionInfo, QsPositionInfo> > .Null);

                    // Creates type resolutions for the call expression
                    var opTypeArgResolutions = typeArgs
                                               .SelectMany(x =>
                                                           x.Resolution is ResolvedTypeKind.TupleType tup
                            ? tup.Item
                            : ImmutableArray.Create(x))
                                               .Where(x => x.Resolution.IsTypeParameter)
                                               .Select(x => (x.Resolution as ResolvedTypeKind.TypeParameter).Item)
                                               .GroupBy(x => (x.Origin, x.TypeName))
                                               .Select(group =>
                    {
                        var typeParam = group.First();
                        return(Tuple.Create(typeParam.Origin, typeParam.TypeName, ResolvedType.New(ResolvedTypeKind.NewTypeParameter(typeParam))));
                    })
                                               .ToImmutableArray();

                    return(new TypedExpression(
                               ExpressionKind.NewCallLikeExpression(identifier, args),
                               opTypeArgResolutions,
                               unitType,
                               new InferredExpressionInformation(false, true),
                               QsNullable <Tuple <QsPositionInfo, QsPositionInfo> > .Null));
                }
        public override ExpressionKind Eat(ISnapshot snapshot, IArrayCreationExpression expression)
        {
            // TODO : check in functional tests
            foreach (ICSharpExpression size in expression.Sizes)
            {
                ExpressionKind kind = Eater.Eat(snapshot, size);
                if (kind != ExpressionKind.StubCandidate)
                {
                    snapshot.Add(kind, size);
                }
            }

            _variableInitializerEater.Eat(snapshot, expression.ArrayInitializer);

            return(ExpressionKind.StubCandidate);
        }
 internal static TypedExpression CreateIdentifierExpression(Identifier id,
                                                            TypeArgsResolution typeArgsMapping, ResolvedType resolvedType) =>
 new TypedExpression
 (
     ExpressionKind.NewIdentifier(
         id,
         typeArgsMapping.Any()
             ? QsNullable <ImmutableArray <ResolvedType> > .NewValue(typeArgsMapping
                                                                     .Select(argMapping => argMapping.Item3) // This should preserve the order of the type args
                                                                     .ToImmutableArray())
             : QsNullable <ImmutableArray <ResolvedType> > .Null),
     typeArgsMapping,
     resolvedType,
     new InferredExpressionInformation(false, false),
     QsNullable <Tuple <QsPositionInfo, QsPositionInfo> > .Null
 );
        private ExpressionKind EatResults(ISnapshot snapshot, ICSharpExpression initialExpression)
        {
            ExpressionKind expressionKind = _eater.Eat(snapshot, initialExpression);

            if (expressionKind == ExpressionKind.StubCandidate)
            {
                return(ExpressionKind.Stub);
            }

            if (expressionKind == ExpressionKind.TargetCall)
            {
                return(ExpressionKind.Result);
            }

            return(expressionKind);
        }
Beispiel #12
0
        public ExprBinOp CreateBinop(ExpressionKind exprKind, CType pType, Expr p1, Expr p2)
        {
            //Debug.Assert(exprKind.isBinaryOperator());
            ExprBinOp rval = new ExprBinOp();

            rval.Kind                    = exprKind;
            rval.Type                    = pType;
            rval.Flags                   = EXPRFLAG.EXF_BINOP;
            rval.OptionalLeftChild       = p1;
            rval.OptionalRightChild      = p2;
            rval.IsLifted                = false;
            rval.OptionalUserDefinedCall = null;
            rval.UserDefinedCallMethod   = null;
            Debug.Assert(rval != null);
            return(rval);
        }
        public virtual ExpressionKind InvocationKindByParentReferenceKind(ExpressionKind parentKind)
        {
            switch (parentKind)
            {
                case ExpressionKind.TargetCall:
                    {
                        return ExpressionKind.Result;
                    }
                case ExpressionKind.Target:
                    {
                        return ExpressionKind.TargetCall;
                    }
            }

            return parentKind;
        }
        public EXPRBINOP CreateBinop(ExpressionKind exprKind, CType pType, EXPR p1, EXPR p2)
        {
            //Debug.Assert(exprKind.isBinaryOperator());
            EXPRBINOP rval = new EXPRBINOP();

            rval.kind  = exprKind;
            rval.type  = pType;
            rval.flags = EXPRFLAG.EXF_BINOP;
            rval.SetOptionalLeftChild(p1);
            rval.SetOptionalRightChild(p2);
            rval.isLifted = false;
            rval.SetOptionalUserDefinedCall(null);
            rval.SetUserDefinedCallMethod(null);
            Debug.Assert(rval != null);
            return(rval);
        }
        public virtual ExpressionKind InvocationKindByParentReferenceKind(ExpressionKind parentKind)
        {
            switch (parentKind)
            {
            case ExpressionKind.TargetCall:
            {
                return(ExpressionKind.Result);
            }

            case ExpressionKind.Target:
            {
                return(ExpressionKind.TargetCall);
            }
            }

            return(parentKind);
        }
Beispiel #16
0
        public void AddLocalVariableToSnapshotTest(ExpressionKind initKind, ExpressionKind variableKindMustBe)
        {
            // Arrange
            var snapshot    = new Mock <ISnapshot>();
            var initializer = Mock.Of <IVariableInitializer>();
            var localConstantDeclaration = Mock.Of <ILocalVariableDeclaration>(t => t.Initial == initializer);
            var typeEater        = Mock.Of <ITypeEater>();
            var eater            = Mock.Of <IEater>();
            var initializerEater = Mock.Of <IVariableInitializerEater>(t => t.Eat(snapshot.Object, initializer) == initKind);
            var localConstantDeclarationEater = new LocalVariableDeclarationEater(eater, initializerEater, typeEater);

            // Act
            localConstantDeclarationEater.Eat(snapshot.Object, localConstantDeclaration);

            // Assert
            snapshot.Verify(t => t.Add(variableKindMustBe, localConstantDeclaration), Times.Once);
        }
Beispiel #17
0
        public void Eat([NotNull] ISnapshot snapshot, TreeNodeCollection <ICSharpArgument> arguements)
        {
            if (snapshot == null)
            {
                throw new ArgumentNullException("snapshot");
            }

            foreach (ICSharpArgument arg in arguements)
            {
                ExpressionKind kind = _eater.Eat(snapshot, arg.Value);

                if (kind != ExpressionKind.StubCandidate && !(arg.Value is IReferenceExpression))
                {
                    snapshot.Add(kind, arg);
                }
            }
        }
        public void AddLocalVariableToSnapshotTest(ExpressionKind initKind, ExpressionKind variableKindMustBe)
        {
            // Arrange
            var snapshot = new Mock<ISnapshot>();
            var initializer = Mock.Of<IVariableInitializer>();
            var localConstantDeclaration = Mock.Of<ILocalVariableDeclaration>(t => t.Initial == initializer);
            var typeEater = Mock.Of<ITypeEater>();
            var eater = Mock.Of<IEater>();
            var initializerEater = Mock.Of<IVariableInitializerEater>(t => t.Eat(snapshot.Object, initializer) == initKind);
            var localConstantDeclarationEater = new LocalVariableDeclarationEater(eater, initializerEater, typeEater);

            // Act
            localConstantDeclarationEater.Eat(snapshot.Object, localConstantDeclaration);

            // Assert
            snapshot.Verify(t => t.Add(variableKindMustBe, localConstantDeclaration), Times.Once);
        }
        public EXPR CreateOperator(ExpressionKind exprKind, CType pType, EXPR pArg1, EXPR pOptionalArg2)
        {
            Debug.Assert(pArg1 != null);
            EXPR rval = null;

            if (exprKind.isUnaryOperator())
            {
                Debug.Assert(pOptionalArg2 == null);
                rval = CreateUnaryOp(exprKind, pType, pArg1);
            }
            else
            {
                rval = CreateBinop(exprKind, pType, pArg1, pOptionalArg2);
            }
            Debug.Assert(rval != null);
            return(rval);
        }
Beispiel #20
0
        /// <remarks>
        /// This method encapsulates the creation of functions with a certain number of arguments. That might be useful
        /// if we want to implement internal subclasses of <see cref="Function"/> such as UnaryFunction, BinaryFunction
        /// TernaryFunction and ArbitraryFunction. These might get used for the performance reasons - in order not to
        /// handle an array for every function (and just for the last variant).
        /// </remarks>
        private static Function ArbitraryFunction(
            ExpressionKind kind,
            Sort sort,
            Expression[] operands,
            bool preserveNesting)
        {
            if (preserveNesting || !operands.Any(op => op.Kind == kind && op.Sort == sort))
            {
                return(new Function(kind, sort, operands));
            }

            var mergedOperands = operands
                                 .SelectMany(op => (op.Kind == kind && op.Sort == sort) ? op.Children : Enumerable.Repeat(op, 1))
                                 .ToArray();

            return(new Function(kind, sort, mergedOperands));
        }
        public ExpressionKind Eat([NotNull] ISnapshot snapshot, [NotNull] IVariableInitializer initializer)
        {
            if (snapshot == null)
            {
                throw new ArgumentNullException("snapshot");
            }

            if (initializer == null)
            {
                throw new ArgumentNullException("initializer");
            }

            if (initializer is IArrayInitializer)
            {
                foreach (IVariableInitializer variableInitializer in (initializer as IArrayInitializer).ElementInitializers)
                {
                    ExpressionKind kind = Eat(snapshot, variableInitializer);

                    // TODO : what if stubcandidate
                    snapshot.Add(kind, variableInitializer);
                }

                // TODO : array of target?
                return(ExpressionKind.StubCandidate);
            }

            ICSharpExpression initialExpression = null;

            if (initializer is IExpressionInitializer)
            {
                initialExpression = (initializer as IExpressionInitializer).Value;
            }

            if (initializer is IUnsafeCodeFixedPointerInitializer)
            {
                initialExpression = (initializer as IUnsafeCodeFixedPointerInitializer).Value;
            }

            if (initializer is IUnsafeCodeStackAllocInitializer)
            {
                initialExpression = (initializer as IUnsafeCodeStackAllocInitializer).DimExpr;
            }

            return(EatResults(snapshot, initialExpression));
        }
        public static void CheckExpression(
            Expression expression,
            ExpressionKind kind,
            Sort sort,
            string stringValue,
            int?childrenCount = null)
        {
            Assert.AreNotEqual(null, expression);
            Assert.AreEqual(kind, expression.Kind);
            Assert.AreEqual(sort, expression.Sort);
            Assert.AreEqual(stringValue, expression.ToString());

            // TODO: Consider making it required
            if (childrenCount != null)
            {
                Assert.AreEqual(childrenCount, expression.ChildrenCount);
            }
        }
Beispiel #23
0
        private void AddAny(ExpressionKind expressionKind, ICSharpTreeNode sharpTreeNode)
        {
            if (expressionKind == ExpressionKind.None || expressionKind == ExpressionKind.StubCandidate)
            {
                return;
            }

            var node = SnapNodes.SingleOrDefault(t => t.Node == sharpTreeNode);

            if (node == null)
            {
                SnapNodes.Add(new SnapNode(sharpTreeNode, expressionKind));
            }
            else
            {
                node.AddKind(expressionKind);
            }
        }
Beispiel #24
0
        public ExprBinOp CreateUserDefinedBinop(ExpressionKind exprKind, CType pType, Expr p1, Expr p2, Expr call, MethPropWithInst pmpwi)
        {
            Debug.Assert(p1 != null);
            Debug.Assert(p2 != null);
            Debug.Assert(call != null);
            ExprBinOp rval = new ExprBinOp(exprKind, pType);

            rval.Flags                   = EXPRFLAG.EXF_BINOP;
            rval.OptionalLeftChild       = p1;
            rval.OptionalRightChild      = p2;
            rval.OptionalUserDefinedCall = call;
            rval.UserDefinedCallMethod   = pmpwi;
            if (call.HasError)
            {
                rval.SetError();
            }
            return(rval);
        }
Beispiel #25
0
        public void FindDeepestNode_Success(string input, int absolutePosition, ExpressionKind expectedExpressionKind, ExpressionKind?parentExpressionKind)
        {
            AssertParser.SucceedsWith(Parsers.Root, input, actualExpression =>
            {
                actualExpression.PostParse(
                    new TextPositions(input)
                    );

                TestOutput.WriteLine("Actual expression:");
                DumpExpression(actualExpression, currentDepth: 1);

                ExpressionNode actualNodeAtPosition = actualExpression.FindDeepestNodeAt(absolutePosition);
                Assert.NotNull(actualNodeAtPosition);

                Assert.Equal(expectedExpressionKind, actualNodeAtPosition.Kind);
                Assert.Equal(parentExpressionKind, actualNodeAtPosition.Parent?.Kind);
            });
        }
Beispiel #26
0
        public ExprUnaryOp CreateUserDefinedUnaryOperator(ExpressionKind exprKind, CType pType, Expr pOperand, ExprCall call, MethPropWithInst pmpwi)
        {
            Debug.Assert(pType != null);
            Debug.Assert(pOperand != null);
            Debug.Assert(call != null);
            Debug.Assert(pmpwi != null);
            ExprUnaryOp rval = new ExprUnaryOp(exprKind, pType);

            rval.Child = pOperand;
            // The call may be lifted, but we do not mark the outer binop as lifted.
            rval.OptionalUserDefinedCall = call;
            rval.UserDefinedCallMethod   = pmpwi;
            if (call.HasError)
            {
                rval.SetError();
            }
            return(rval);
        }
 public virtual ExpressionKind ReferenceKindByParentReferenceKind(ExpressionKind parentKind)
 {
     switch (parentKind)
     {
         case ExpressionKind.TargetCall:
             {
                 return ExpressionKind.Result;
             }
         case ExpressionKind.Target:
             {
                 return ExpressionKind.Result;
             }
         case ExpressionKind.Stub:
             {
                 return ExpressionKind.Stub;
             }
         case ExpressionKind.Mock:
             {
                 return ExpressionKind.Result;
             }
         case ExpressionKind.StubCandidate:
             {
                 return ExpressionKind.StubCandidate;
             }
         case ExpressionKind.Assert:
             {
                 return ExpressionKind.Result;
             }
         case ExpressionKind.None:
             {
                 return ExpressionKind.None;
             }
         case ExpressionKind.Result:
             {
                 return ExpressionKind.Result;
             }
         default:
             {
                 return ExpressionKind.None;
             }
     }
 }
 public static bool isUnaryOperator(this ExpressionKind kind)
 {
     switch (kind)
     {
     case ExpressionKind.EK_TRUE:
     case ExpressionKind.EK_FALSE:
     case ExpressionKind.EK_INC:
     case ExpressionKind.EK_DEC:
     case ExpressionKind.EK_LOGNOT:
     case ExpressionKind.EK_NEG:
     case ExpressionKind.EK_UPLUS:
     case ExpressionKind.EK_BITNOT:
     case ExpressionKind.EK_ADDR:
     case ExpressionKind.EK_DECIMALNEG:
     case ExpressionKind.EK_DECIMALINC:
     case ExpressionKind.EK_DECIMALDEC:
         return(true);
     }
     return(false);
 }
Beispiel #29
0
        public static bool IsUnaryOperator(this ExpressionKind kind)
        {
            switch (kind)
            {
            case ExpressionKind.True:
            case ExpressionKind.False:
            case ExpressionKind.Inc:
            case ExpressionKind.Dec:
            case ExpressionKind.LogicalNot:
            case ExpressionKind.Negate:
            case ExpressionKind.UnaryPlus:
            case ExpressionKind.BitwiseNot:
            case ExpressionKind.Addr:
            case ExpressionKind.DecimalNegate:
            case ExpressionKind.DecimalInc:
            case ExpressionKind.DecimalDec:
                return(true);
            }

            return(false);
        }
 public virtual ExpressionKind KindOfAssignment(ExpressionKind assignSourceKind)
 {
     switch (assignSourceKind)
     {
         case ExpressionKind.TargetCall:
             {
                 return ExpressionKind.Result;
             }
         case ExpressionKind.StubCandidate:
             {
                 return ExpressionKind.Stub;
             }
         case ExpressionKind.Assert:
             {
                 return ExpressionKind.Result;
             }
         default :
         {
             return assignSourceKind;
         }
     }
 }
        public EXPRUNARYOP CreateUserDefinedUnaryOperator(ExpressionKind exprKind, CType pType, EXPR pOperand, EXPR call, MethPropWithInst pmpwi)
        {
            Debug.Assert(pType != null);
            Debug.Assert(pOperand != null);
            Debug.Assert(call != null);
            Debug.Assert(pmpwi != null);
            EXPRUNARYOP rval = new EXPRUNARYOP();

            rval.kind  = exprKind;
            rval.type  = pType;
            rval.flags = 0;
            rval.Child = pOperand;
            // The call may be lifted, but we do not mark the outer binop as lifted.
            rval.OptionalUserDefinedCall = call;
            rval.UserDefinedCallMethod   = pmpwi;
            if (call.HasError())
            {
                rval.SetError();
            }
            Debug.Assert(rval != null);
            return(rval);
        }
        public EXPRBINOP CreateUserDefinedBinop(ExpressionKind exprKind, CType pType, EXPR p1, EXPR p2, EXPR call, MethPropWithInst pmpwi)
        {
            Debug.Assert(p1 != null);
            Debug.Assert(p2 != null);
            Debug.Assert(call != null);
            EXPRBINOP rval = new EXPRBINOP();

            rval.kind  = exprKind;
            rval.type  = pType;
            rval.flags = EXPRFLAG.EXF_BINOP;
            rval.SetOptionalLeftChild(p1);
            rval.SetOptionalRightChild(p2);
            // The call may be lifted, but we do not mark the outer binop as lifted.
            rval.isLifted = false;
            rval.SetOptionalUserDefinedCall(call);
            rval.SetUserDefinedCallMethod(pmpwi);
            if (call.HasError())
            {
                rval.SetError();
            }
            Debug.Assert(rval != null);
            return(rval);
        }
        /// <summary>
        /// Creates an array literal with the given items, setting the range information to Null.
        /// If no items are given, creates an empty array of type Unit[].
        /// The resolved types for all of the given expressions must match,
        /// none of the given expressions should have a local quantum dependency,
        /// and all range information should be stripped from each given expression.
        /// </summary>
        internal static TypedExpression CreateValueArray(params TypedExpression[] expressions)
        {
            ResolvedType type = null;

            if (expressions.Any())
            {
                type = expressions.First().ResolvedType;
                QsCompilerError.Verify(expressions.All(expr => expr.ResolvedType.Equals(type)));
            }
            else
            {
                type = ResolvedType.New(ResolvedTypeKind.UnitType);
            }

            return(new TypedExpression
                   (
                       ExpressionKind.NewValueArray(expressions.ToImmutableArray()),
                       TypeArgsResolution.Empty,
                       ResolvedType.New(ResolvedTypeKind.NewArrayType(type)),
                       new InferredExpressionInformation(false, false),
                       QsNullable <Tuple <QsPositionInfo, QsPositionInfo> > .Null
                   ));
        }
Beispiel #34
0
        public ExprBinOp CreateUserDefinedBinop(ExpressionKind exprKind, CType pType, Expr p1, Expr p2, Expr call, MethPropWithInst pmpwi)
        {
            Debug.Assert(p1 != null);
            Debug.Assert(p2 != null);
            Debug.Assert(call != null);
            ExprBinOp rval = new ExprBinOp();

            rval.Kind               = exprKind;
            rval.Type               = pType;
            rval.Flags              = EXPRFLAG.EXF_BINOP;
            rval.OptionalLeftChild  = p1;
            rval.OptionalRightChild = p2;
            // The call may be lifted, but we do not mark the outer binop as lifted.
            rval.IsLifted = false;
            rval.OptionalUserDefinedCall = call;
            rval.UserDefinedCallMethod   = pmpwi;
            if (call.HasError)
            {
                rval.SetError();
            }
            Debug.Assert(rval != null);
            return(rval);
        }
        public virtual ExpressionKind ValueOfKindAsTypeOfKind(ExpressionKind valueKind, ExpressionKind typeKind)
        {
            if (valueKind == ExpressionKind.TargetCall)
            {
                return(ExpressionKind.Result);
            }

            if (valueKind == ExpressionKind.Result)
            {
                return(ExpressionKind.Result);
            }

            if (valueKind == ExpressionKind.Mock)
            {
                return(ExpressionKind.Mock);
            }

            if (valueKind == ExpressionKind.Target || typeKind == ExpressionKind.Target)
            {
                return(ExpressionKind.Target);
            }

            return(valueKind);
        }
        public void IsExpression_Success(string testFileName, int line, int column, ExpressionKind expectedExpressionKind)
        {
            Position testPosition = new Position(line, column);

            string            testXml   = LoadTestFile("TestFiles", testFileName + ".xml");
            TextPositions     positions = new TextPositions(testXml);
            XmlDocumentSyntax document  = Parser.ParseText(testXml);

            XmlLocator  locator  = new XmlLocator(document, positions);
            XmlLocation location = locator.Inspect(testPosition);

            Assert.NotNull(location);

            ExpressionNode actualExpression;
            Range          actualExpressionRange;

            Assert.True(
                location.IsExpression(out actualExpression, out actualExpressionRange),
                "IsExpression"
                );
            Assert.NotNull(actualExpression);

            Assert.Equal(expectedExpressionKind, actualExpression.Kind);
        }
			: super(ExpressionKind.Unary) {
		}
 : super(ExpressionKind.MethodCall, method.getReturnType()) {
			: super(ExpressionKind.Literal) {
		}
Beispiel #40
0
 ////////////////////////////////////////////////////////////////////////////////
 // Report a bad operator types error to the user.
 protected EXPR BadOperatorTypesError(ExpressionKind ek, EXPR pOperand1, EXPR pOperand2)
 {
     return BadOperatorTypesError(ek, pOperand1, pOperand2, null);
 }
			: super(ExpressionKind.Assign) {
		}
Beispiel #42
0
        protected EXPR bindUserDefinedBinOp(ExpressionKind ek, BinOpArgInfo info)
        {
            MethPropWithInst pmpwi = null;
            if (info.pt1 <= PredefinedType.PT_ULONG && info.pt2 <= PredefinedType.PT_ULONG)
            {
                return null;
            }

            EXPR expr = null;

            switch (info.binopKind)
            {
                case BinOpKind.Logical:
                    {
                        // Logical operators cannot be overloaded, but use the bitwise overloads.
                        EXPRCALL call = BindUDBinop((ExpressionKind)(ek - ExpressionKind.EK_LOGAND + ExpressionKind.EK_BITAND), info.arg1, info.arg2, true, out pmpwi);
                        if (call != null)
                        {
                            if (call.isOK())
                            {
                                expr = BindUserBoolOp(ek, call);
                            }
                            else
                            {
                                expr = call;
                            }
                        }
                        break;
                    }
                default:
                    expr = BindUDBinop(ek, info.arg1, info.arg2, false, out pmpwi);
                    break;
            }

            if (expr == null)
            {
                return null;
            }

            return GetExprFactory().CreateUserDefinedBinop(ek, expr.type, info.arg1, info.arg2, expr, pmpwi);
        }
Beispiel #43
0
        protected EXPR BindStandardBinopCore(BinOpArgInfo info, BinOpFullSig bofs, ExpressionKind ek, EXPRFLAG flags)
        {
            if (bofs.pfn == null)
            {
                return BadOperatorTypesError(ek, info.arg1, info.arg2);
            }

            if (!bofs.isLifted() || !bofs.AutoLift())
            {
                EXPR expr1 = info.arg1;
                EXPR expr2 = info.arg2;
                if (bofs.ConvertOperandsBeforeBinding())
                {
                    expr1 = mustConvert(expr1, bofs.Type1());
                    expr2 = mustConvert(expr2, bofs.Type2());
                }
                if (bofs.fnkind == BinOpFuncKind.BoolBitwiseOp)
                {
                    return BindBoolBitwiseOp(ek, flags, expr1, expr2, bofs);
                }
                return bofs.pfn(ek, flags, expr1, expr2);
            }
            Debug.Assert(bofs.fnkind != BinOpFuncKind.BoolBitwiseOp);
            return BindLiftedStandardBinOp(info, bofs, ek, flags);
        }
Beispiel #44
0
        private static BinaryExpression Chain(Expression left, IEnumerator<Expression> e, ExpressionKind op)
        {
           var right = e.Current; 

            return new BinaryExpression(
                left  : left,
                op    : op,
                right : e.MoveNext() ? Chain(right, e, op) : right);
        }
Beispiel #45
0
 public OPINFO(TokenKind t, PredefinedName pn, ExpressionKind e, int c)
 {
     iToken         = t;
     methodName     = pn;
     expressionKind = e;
 }
Beispiel #46
0
 public BinaryExpression(Expression left, ExpressionKind op, Expression right)
 {
     Left = left;
     Right = right;
     Op = op;
 }
Beispiel #47
0
 public UnaryExpression(ExpressionKind op, Expression operand)
 {
     Operand = operand;
     Op = op;
 }
 : super(ExpressionKind.Field, field.getType()) {
Beispiel #49
0
 protected Name ekName(ExpressionKind ek)
 {
     Debug.Assert(ek >= ExpressionKind.EK_FIRSTOP && (ek - ExpressionKind.EK_FIRSTOP) < (int)s_EK2NAME.Length);
     return GetSymbolLoader().GetNameManager().GetPredefName(s_EK2NAME[ek - ExpressionKind.EK_FIRSTOP]);
 }
Beispiel #50
0
        protected EXPR BadOperatorTypesError(ExpressionKind ek, EXPR pOperand1, EXPR pOperand2, CType pTypeErr)
        {
            // This is a hack, but we need to store the operation somewhere... the first argument's as 
            // good a place as any.
            string strOp = pOperand1.errorString;

            pOperand1 = UnwrapExpression(pOperand1);

            if (pOperand1 != null)
            {
                if (pOperand2 != null)
                {
                    pOperand2 = UnwrapExpression(pOperand2);
                    if (pOperand1.type != null &&
                            !pOperand1.type.IsErrorType() &&
                            pOperand2.type != null &&
                            !pOperand2.type.IsErrorType())
                    {
                        ErrorContext.Error(ErrorCode.ERR_BadBinaryOps, strOp, pOperand1.type, pOperand2.type);
                    }
                }
                else if (pOperand1.type != null && !pOperand1.type.IsErrorType())
                {
                    ErrorContext.Error(ErrorCode.ERR_BadUnaryOp, strOp, pOperand1.type);
                }
            }

            if (pTypeErr == null)
            {
                pTypeErr = GetReqPDT(PredefinedType.PT_OBJECT);
            }

            EXPR rval = GetExprFactory().CreateOperator(ek, pTypeErr, pOperand1, pOperand2);
            rval.SetError();
            return rval;
        }
			: super(ExpressionKind.ObjectCreation) {
			this.Arguments = new ArrayList<ExpressionNode>();
		}
			: super(ExpressionKind.ArrayInitializer) {
			this.Values = new ArrayList<ExpressionNode>();
		}
			: super(ExpressionKind.Cast) {
		}
			: super(ExpressionKind.ArrayCreation) {
			this.DimensionExpressions = new ArrayList<ExpressionNode>();
		}
Beispiel #55
0
        private EXPR BindLiftedStandardBinOp(BinOpArgInfo info, BinOpFullSig bofs, ExpressionKind ek, EXPRFLAG flags)
        {
            Debug.Assert(bofs.Type1().IsNullableType() || bofs.Type2().IsNullableType());

            EXPR arg1 = info.arg1;
            EXPR arg2 = info.arg2;

            // We want to get the base types of the arguments and attempt to bind the non-lifted form of the
            // method so that we error report (ie divide by zero etc), and then we store in the resulting
            // binop that we have a lifted operator.

            EXPR pArgument1 = null;
            EXPR pArgument2 = null;
            EXPR nonLiftedArg1 = null;
            EXPR nonLiftedArg2 = null;
            EXPR nonLiftedResult = null;
            CType resultType = null;

            LiftArgument(arg1, bofs.Type1(), bofs.ConvertFirst(), out pArgument1, out nonLiftedArg1);
            LiftArgument(arg2, bofs.Type2(), bofs.ConvertSecond(), out pArgument2, out nonLiftedArg2);

            // Now call the non-lifted method to generate errors, and stash the result.
            if (!nonLiftedArg1.isNull() && !nonLiftedArg2.isNull())
            {
                // Only compute the method if theres no nulls. If there are, we'll special case it
                // later, since operations with a null operand are null.
                nonLiftedResult = bofs.pfn(ek, flags, nonLiftedArg1, nonLiftedArg2);
            }

            // Check if we have a comparison. If so, set the result type to bool.
            if (info.binopKind == BinOpKind.Compare || info.binopKind == BinOpKind.Equal)
            {
                resultType = GetReqPDT(PredefinedType.PT_BOOL);
            }
            else
            {
                if (bofs.fnkind == BinOpFuncKind.EnumBinOp)
                {
                    AggregateType enumType;
                    resultType = GetEnumBinOpType(ek, nonLiftedArg1.type, nonLiftedArg2.type, out enumType);
                }
                else
                {
                    resultType = pArgument1.type;
                }
                resultType = resultType.IsNullableType() ? resultType : GetSymbolLoader().GetTypeManager().GetNullable(resultType);
            }

            EXPRBINOP exprRes = GetExprFactory().CreateBinop(ek, resultType, pArgument1, pArgument2);
            mustCast(nonLiftedResult, resultType, 0);
            exprRes.isLifted = true;
            exprRes.flags |= flags;
            Debug.Assert((exprRes.flags & EXPRFLAG.EXF_LVALUE) == 0);

            return exprRes;
        }
        public virtual ExpressionKind ValueOfKindAsTypeOfKind(ExpressionKind valueKind, ExpressionKind typeKind)
        {
            if (valueKind == ExpressionKind.TargetCall)
            {
                return ExpressionKind.Result;
            }

            if (valueKind == ExpressionKind.Result)
            {
                return ExpressionKind.Result;
            }

            if (valueKind == ExpressionKind.Mock)
            {
                return ExpressionKind.Mock;
            }

            if (valueKind == ExpressionKind.Target || typeKind == ExpressionKind.Target)
            {
                return ExpressionKind.Target;
            }

            return valueKind;
        }
		public MemberAccessExpressionNode() : super(ExpressionKind.MemberAccess) {}
Beispiel #58
0
        internal EXPR bindUDUnop(ExpressionKind ek, EXPR arg)
        {
            Name pName = ekName(ek);
            Debug.Assert(pName != null);

            CType typeSrc = arg.type;

        LAgain:
            switch (typeSrc.GetTypeKind())
            {
                case TypeKind.TK_NullableType:
                    typeSrc = typeSrc.StripNubs();
                    goto LAgain;
                case TypeKind.TK_TypeParameterType:
                    typeSrc = typeSrc.AsTypeParameterType().GetEffectiveBaseClass();
                    goto LAgain;
                case TypeKind.TK_AggregateType:
                    if (!typeSrc.isClassType() && !typeSrc.isStructType() || typeSrc.AsAggregateType().getAggregate().IsSkipUDOps())
                        return null;
                    break;
                default:
                    return null;
            }

            ArgInfos info = new ArgInfos();

            info.carg = 1;
            FillInArgInfoFromArgList(info, arg);

            List<CandidateFunctionMember> methFirstList = new List<CandidateFunctionMember>();
            MethodSymbol methCur = null;
            AggregateType atsCur = typeSrc.AsAggregateType();

            for (; ;)
            {
                // Find the next operator.
                methCur = (methCur == null) ?
                          GetSymbolLoader().LookupAggMember(pName, atsCur.getAggregate(), symbmask_t.MASK_MethodSymbol).AsMethodSymbol() :
                          GetSymbolLoader().LookupNextSym(methCur, atsCur.getAggregate(), symbmask_t.MASK_MethodSymbol).AsMethodSymbol();

                if (methCur == null)
                {
                    // Find the next type.
                    // If we've found some applicable methods in a class then we don't need to look any further.
                    if (!methFirstList.IsEmpty())
                        break;
                    atsCur = atsCur.GetBaseClass();
                    if (atsCur == null)
                        break;
                    continue;
                }

                // Only look at operators with 1 args.
                if (!methCur.isOperator || methCur.Params.size != 1)
                    continue;
                Debug.Assert(methCur.typeVars.size == 0);

                TypeArray paramsCur = GetTypes().SubstTypeArray(methCur.Params, atsCur);
                CType typeParam = paramsCur.Item(0);
                NullableType nubParam;

                if (canConvert(arg, typeParam))
                {
                    methFirstList.Add(new CandidateFunctionMember(
                                    new MethPropWithInst(methCur, atsCur, BSYMMGR.EmptyTypeArray()),
                                    paramsCur,
                                    0,
                                    false));
                }
                else if (GetSymbolLoader().FCanLift() && typeParam.IsNonNubValType() &&
                         GetTypes().SubstType(methCur.RetType, atsCur).IsNonNubValType() &&
                         canConvert(arg, nubParam = GetTypes().GetNullable(typeParam)))
                {
                    methFirstList.Add(new CandidateFunctionMember(
                                    new MethPropWithInst(methCur, atsCur, BSYMMGR.EmptyTypeArray()),
                                    GetGlobalSymbols().AllocParams(1, new CType[] { nubParam }),
                                    1,
                                    false));
                }
            }

            if (methFirstList.IsEmpty())
                return null;

            CandidateFunctionMember pmethAmbig1;
            CandidateFunctionMember pmethAmbig2;
            CandidateFunctionMember pmethBest = FindBestMethod(methFirstList, null, info, out pmethAmbig1, out pmethAmbig2);

            if (pmethBest == null)
            {
                // No winner, so its an ambiguous call...
                ErrorContext.Error(ErrorCode.ERR_AmbigCall, pmethAmbig1.mpwi, pmethAmbig2.mpwi);

                EXPRMEMGRP pMemGroup = GetExprFactory().CreateMemGroup(null, pmethAmbig1.mpwi);
                EXPRCALL rval = GetExprFactory().CreateCall(0, null, arg, pMemGroup, null);
                rval.SetError();
                return rval;
            }

            if (SemanticChecker.CheckBogus(pmethBest.mpwi.Meth()))
            {
                ErrorContext.ErrorRef(ErrorCode.ERR_BindToBogus, pmethBest.mpwi);

                EXPRMEMGRP pMemGroup = GetExprFactory().CreateMemGroup(null, pmethBest.mpwi);
                EXPRCALL rval = GetExprFactory().CreateCall(0, null, arg, pMemGroup, null);
                rval.SetError();
                return rval;
            }

            EXPRCALL call;

            if (pmethBest.ctypeLift != 0)
            {
                call = BindLiftedUDUnop(arg, [email protected](0), pmethBest.mpwi);
            }
            else
            {
                call = BindUDUnopCall(arg, [email protected](0), pmethBest.mpwi);
            }

            return GetExprFactory().CreateUserDefinedUnaryOperator(ek, call.type, arg, call, pmethBest.mpwi);
        }
     : super(ExpressionKind.Value, type) {
     this.Value = value;
 }
		protected MemberAccessExpressionNode(ExpressionKind kind) : super(kind) {}