示例#1
0
        public IEnumerable <StatementSyntax> GetFinalization()
        {
            yield return(SF.IfStatement(SF.BinaryExpression(SyntaxKind.GreaterThanExpression, SF.IdentifierName(outputIndex), SH.Literal(0)),
                                        SF.ExpressionStatement(SF.IdentifierName(outputParameter).Dot("Write").Invoke(SF.IdentifierName(outputBuffer), SH.Literal(0), SF.IdentifierName(outputIndex)))));

            yield return(SF.ReturnStatement());
        }
示例#2
0
        public IEnumerable <StatementSyntax> GetYields(IEnumerable <ExpressionSyntax> yields, int numYields)
        {
            if (numYields == 0)
            {
                yield break;
            }
            if (numYields > BufferSize)
            {
                throw new NotImplementedException();
            }
            yield return(SF.IfStatement(SF.BinaryExpression(SyntaxKind.GreaterThanExpression, SF.IdentifierName(outputIndex), SH.Literal(BufferSize - numYields)),
                                        SF.Block(
                                            SF.ExpressionStatement(SF.IdentifierName(outputParameter).Dot("Write").Invoke(SF.IdentifierName(outputBuffer), SH.Literal(0), SF.IdentifierName(outputIndex))),
                                            SH.Assignment(SF.IdentifierName(outputIndex), SH.Literal(0)))));

            int index = 0;

            foreach (var yieldSyntax in yields)
            {
                yield return(SH.Assignment(SF.ElementAccessExpression(SF.IdentifierName(outputBuffer), SF.BracketedArgumentList(SF.SingletonSeparatedList(SF.Argument(
                                                                                                                                                              SF.BinaryExpression(SyntaxKind.AddExpression, SF.IdentifierName(outputIndex), SH.Literal(index)))))),
                                           yieldSyntax));

                ++index;
            }
            yield return(SH.Assignment(SF.IdentifierName(outputIndex), SH.Literal(numYields), SyntaxKind.AddAssignmentExpression));
        }
 public ExpressionSyntax GetInput()
 {
     return(SF.BinaryExpression(SyntaxKind.EqualsExpression,
                                SF.BinaryExpression(SyntaxKind.BitwiseAndExpression,
                                                    SF.IdentifierName(cachedInput),
                                                    SH.Literal(1)).Parenthesize(),
                                SH.Literal(1)));
 }
 public IEnumerable <StatementSyntax> GetMoveNext(StatementSyntax finalizer)
 {
     yield return(SF.IfStatement(SF.BinaryExpression(SyntaxKind.GreaterThanOrEqualExpression,
                                                     SF.PrefixUnaryExpression(SyntaxKind.PreIncrementExpression, SF.IdentifierName(inputIndex)), SF.IdentifierName(read)), SF.Block(
                                     SH.Assignment(SF.IdentifierName(read), SF.IdentifierName(inputParameter).Dot("Read").Invoke(SF.IdentifierName(inputBuffer), SH.Literal(0), SF.IdentifierName(inputBuffer).Dot("Length"))),
                                     SF.IfStatement(SF.BinaryExpression(SyntaxKind.EqualsExpression, SF.IdentifierName(read), SH.Literal(0)), finalizer),
                                     SH.Assignment(SF.IdentifierName(inputIndex), SH.Literal(0))
                                     )));
 }
 public IEnumerable <StatementSyntax> GetMoveNext(StatementSyntax finalizer)
 {
     yield return(SF.IfStatement(SF.BinaryExpression(SyntaxKind.GreaterThanExpression,
                                                     SF.PrefixUnaryExpression(SyntaxKind.PreIncrementExpression, SF.IdentifierName(bitIndex)), SH.Literal(7)),
                                 SF.Block(_wrapped.GetMoveNext(finalizer).Concat(new StatementSyntax[] {
         SH.Assignment(SF.IdentifierName(cachedInput), _wrapped.GetInput()),
         SH.Assignment(SF.IdentifierName(bitIndex), SH.Literal(0))
     })),
                                 SF.ElseClause(SH.Assignment(SF.IdentifierName(cachedInput), SH.Literal(1), SyntaxKind.RightShiftAssignmentExpression))));
 }
        /// <summary>
        /// Generates switch cases for the provided grain type.
        /// </summary>
        /// <param name="grainType">
        /// The grain type.
        /// </param>
        /// <param name="methodIdArgument">
        /// The method id argument, which is used to select the correct switch label.
        /// </param>
        /// <param name="generateMethodHandler">
        /// The function used to generate switch block statements for each method.
        /// </param>
        /// <returns>
        /// The switch cases for the provided grain type.
        /// </returns>
        public static SwitchSectionSyntax[] GenerateGrainInterfaceAndMethodSwitch(
            Type grainType,
            ExpressionSyntax methodIdArgument,
            Func <MethodInfo, StatementSyntax[]> generateMethodHandler)
        {
            var interfaces = GrainInterfaceUtils.GetRemoteInterfaces(grainType);

            interfaces[GrainInterfaceUtils.GetGrainInterfaceId(grainType)] = grainType;

            // Switch on interface id.
            var interfaceCases = new List <SwitchSectionSyntax>();

            foreach (var @interface in interfaces)
            {
                var interfaceType = @interface.Value;
                var interfaceId   = @interface.Key;
                var methods       = GrainInterfaceUtils.GetMethods(interfaceType);

                var methodCases = new List <SwitchSectionSyntax>();

                // Switch on method id.
                foreach (var method in methods)
                {
                    // Generate switch case.
                    var methodId   = GrainInterfaceUtils.ComputeMethodId(method);
                    var methodType = method;

                    // Generate the switch label for this interface id.
                    var methodIdSwitchLabel =
                        SF.CaseSwitchLabel(
                            SF.LiteralExpression(SyntaxKind.NumericLiteralExpression, SF.Literal(methodId)));

                    // Generate the switch body.
                    var methodInvokeStatement = generateMethodHandler(methodType);

                    methodCases.Add(
                        SF.SwitchSection().AddLabels(methodIdSwitchLabel).AddStatements(methodInvokeStatement));
                }

                // Generate the switch label for this interface id.
                var interfaceIdSwitchLabel =
                    SF.CaseSwitchLabel(
                        SF.LiteralExpression(SyntaxKind.NumericLiteralExpression, SF.Literal(interfaceId)));

                // Generate the default case, which will throw a NotImplementedException.
                var errorMessage = SF.BinaryExpression(
                    SyntaxKind.AddExpression,
                    "interfaceId=".GetLiteralExpression(),
                    SF.BinaryExpression(
                        SyntaxKind.AddExpression,
                        SF.LiteralExpression(SyntaxKind.NumericLiteralExpression, SF.Literal(interfaceId)),
                        SF.BinaryExpression(
                            SyntaxKind.AddExpression,
                            ",methodId=".GetLiteralExpression(),
                            methodIdArgument)));
                var throwStatement =
                    SF.ThrowStatement(
                        SF.ObjectCreationExpression(typeof(NotImplementedException).GetTypeSyntax())
                        .AddArgumentListArguments(SF.Argument(errorMessage)));
                var defaultCase = SF.SwitchSection().AddLabels(SF.DefaultSwitchLabel()).AddStatements(throwStatement);

                // Generate switch statements for the methods in this interface.
                var methodSwitchStatements =
                    SF.SwitchStatement(methodIdArgument).AddSections(methodCases.ToArray()).AddSections(defaultCase);

                // Generate the switch section for this interface.
                interfaceCases.Add(
                    SF.SwitchSection().AddLabels(interfaceIdSwitchLabel).AddStatements(methodSwitchStatements));
            }

            return(interfaceCases.ToArray());
        }
 public IEnumerable <StatementSyntax> GetMoveNext(StatementSyntax finalizer)
 {
     yield return(SF.IfStatement(SF.BinaryExpression(SyntaxKind.GreaterThanOrEqualExpression,
                                                     SF.PrefixUnaryExpression(SyntaxKind.PreIncrementExpression, SF.IdentifierName(inputIndex)), SF.IdentifierName(inputParameter).Dot("Length")),
                                 finalizer));
 }