コード例 #1
0
        public override void VisitBinaryExpression(BinaryExpression node)
        {
            base.VisitBinaryExpression(node);

            if (node.Operator == BinaryOperator.Assign && node.Right.CodeNodeType == CodeNodeType.ArrayCreationExpression)
            {
                ArrayCreationExpression arrayCreation = node.Right as ArrayCreationExpression;
                bool isInitializerPresent             = Utilities.IsInitializerPresent(arrayCreation.Initializer);

                if (node.Left.CodeNodeType == CodeNodeType.VariableDeclarationExpression)
                {
                    VariableDeclarationExpression variableDeclaration = node.Left as VariableDeclarationExpression;
                    node.Left = new ArrayVariableDeclarationExpression(variableDeclaration, arrayCreation.ElementType,
                                                                       arrayCreation.Dimensions.CloneExpressionsOnly(), isInitializerPresent, null);
                }

                if (node.Left.CodeNodeType == CodeNodeType.VariableReferenceExpression)
                {
                    VariableReferenceExpression variableReference = node.Left as VariableReferenceExpression;
                    node.Left = new ArrayVariableReferenceExpression(variableReference, arrayCreation.ElementType,
                                                                     arrayCreation.Dimensions.CloneExpressionsOnly(), isInitializerPresent, null);
                }

                if (node.Left.CodeNodeType == CodeNodeType.FieldReferenceExpression)
                {
                    FieldReferenceExpression fieldReference = node.Left as FieldReferenceExpression;
                    node.Left = new ArrayAssignmentFieldReferenceExpression(fieldReference, arrayCreation.ElementType,
                                                                            arrayCreation.Dimensions.CloneExpressionsOnly(), isInitializerPresent, null);
                }
            }
        }
コード例 #2
0
 public override void VisitVariableDeclarationExpression(VariableDeclarationExpression node)
 {
     if (node.Variable == oldVariable)
     {
         node.Variable = newVariable;
     }
 }
コード例 #3
0
        public override Ust VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
        {
            if (node.Declaration.Type is TupleTypeSyntax)
            {
                return(HandleTupleStatement(node.Declaration));
            }
            var type = ConvertType(base.Visit(node.Declaration.Type));

            AssignmentExpression[] variables =
                node.Declaration.Variables.Select(v => (AssignmentExpression)VisitAndReturnNullIfError(v))
                .Select(v =>
            {
                //here we set the type text if it is hidden on the right
                if (v is AssignmentExpression assignment &&
                    assignment.Right is ArrayCreationExpression arrayCreation)
                {
                    if (string.IsNullOrEmpty(arrayCreation.Type.TypeText))
                    {
                        arrayCreation.Type.TypeText = type.TypeText;
                    }
                }
                return(v);
            })
                .ToArray();

            var resultExpression = new VariableDeclarationExpression(type, variables, node.Declaration.GetTextSpan());
            var result           = new ExpressionStatement(resultExpression, node.GetTextSpan());

            return(result);
        }
コード例 #4
0
 private void CreateMethod(CatchClause catchClause, VariableDeclarationExpression variable, out Expression methodInvocationExpression)
 {
     methodInvocationExpression = null;
     stackVariable4             = catchClause.get_Filter() as BlockStatement;
     V_0 = (((stackVariable4.get_Statements().First <Statement>() as ExpressionStatement).get_Expression() as BinaryExpression).get_Right() as SafeCastExpression).get_Expression() as VariableReferenceExpression;
     V_0.set_Variable(variable.get_Variable());
     V_1 = stackVariable4.get_Statements().Last <Statement>() as ExpressionStatement;
     V_1.set_Expression(new ReturnExpression(V_1.get_Expression(), V_1.get_Expression().get_MappedInstructions()));
     V_2 = this.context.get_TypeContext().get_GeneratedFilterMethods().get_Count() + this.methodsToBeDecompiled.get_Count();
     V_3 = new MethodDefinition(String.Format("JustDecompileGenerated_Filter_{0}", V_2), 1, this.context.get_MethodContext().get_Method().get_Module().get_TypeSystem().get_Boolean());
     V_3.set_Body(new MethodBody(V_3));
     V_3.set_MetadataToken(new MetadataToken(0x6000000, 0xffffff - V_2));
     V_3.set_IsStatic(this.context.get_MethodContext().get_Method().get_IsStatic());
     V_3.set_HasThis(!V_3.get_IsStatic());
     V_3.set_DeclaringType(this.context.get_MethodContext().get_Method().get_DeclaringType());
     V_3.set_SemanticsAttributes(0);
     V_3.set_IsJustDecompileGenerated(true);
     V_4 = new DecompilationContext(this.CloneAndReplaceMethodBody(this.context.get_MethodContext(), V_3.get_Body()), this.context.get_TypeContext(), this.context.get_Language());
     V_5 = V_0.get_Variable().Resolve();
     if (V_4.get_MethodContext().get_VariableDefinitionToNameMap().ContainsKey(V_5))
     {
         V_4.get_MethodContext().get_VariableDefinitionToNameMap().Add(V_5, "JustDecompileGenerated_Exception");
     }
     else
     {
         V_4.get_MethodContext().get_VariableDefinitionToNameMap().set_Item(V_5, "JustDecompileGenerated_Exception");
     }
     dummyVar0 = V_4.get_MethodContext().get_VariablesToNotDeclare().Add(variable.get_Variable());
     this.methodsToBeDecompiled.Add(new FilterMethodToBeDecompiled(V_3, catchClause, V_4, catchClause.get_Filter() as BlockStatement));
     V_6 = this.AddAllParameters(catchClause, V_3, variable);
     methodInvocationExpression = this.CreateMethodInvocation(V_3, V_6);
     return;
 }
コード例 #5
0
        public Ust VisitLocalVariableDeclaration(JavaParser.LocalVariableDeclarationContext context)
        {
            var type = (TypeToken)Visit(context.typeType());

            AssignmentExpression[] initializers = context.variableDeclarators().variableDeclarator()
                                                  .Select(varDec => (AssignmentExpression)Visit(varDec))
                                                  .Where(initializer => initializer != null).ToArray();

            if (initializers.Count() == 1 && initializers.First().Right is MultichildExpression multichildExpression)
            {
                var expressions = multichildExpression.Expressions;
                // is array?
                if (CommonUtils.TryCheckIdTokenValue(expressions.FirstOrDefault(), "{") &&
                    CommonUtils.TryCheckIdTokenValue(expressions.LastOrDefault(), "}"))
                {
                    int dimensions = multichildExpression.GetDepth(1);
                    var sizes      = Enumerable.Range(0, dimensions).Select(
                        _ => new IntLiteral(0, type.TextSpan)).ToList <Expression>();
                    var array_initializers     = expressions.Where(el => !(el is IdToken));
                    initializers.First().Right = new ArrayCreationExpression(
                        new TypeToken(type.TypeText, type.TextSpan), sizes,
                        array_initializers, multichildExpression.TextSpan);
                }
            }

            var result = new VariableDeclarationExpression(type, initializers, context.GetTextSpan());

            return(result);
        }
コード例 #6
0
        /// <summary>
        /// When a name is absent, deconstruct a given tuple, otherwise create virtual member references
        /// </summary>
        private Ust HandleTupleStatement(VariableDeclarationSyntax node)
        {
            List <AssignmentExpression> variables =
                node.Variables.Select(v => (AssignmentExpression)VisitAndReturnNullIfError(v)).ToList();

            var typeNode = (TupleTypeSyntax)node.Type;
            var textSpan = node.GetTextSpan();

            for (int i = 0; i < typeNode.Elements.Count; i++)
            {
                TupleElementSyntax typeElement = typeNode.Elements[i];
                if (typeElement.Identifier == null)
                {
                    continue;
                }

                var initValues = node.Variables.Select(v =>
                {
                    var tuple = v.Initializer?.Value as TupleExpressionSyntax;
                    if (tuple == null)
                    {
                        return(null);
                    }
                    return((Expression)base.Visit(tuple.Arguments.ElementAtOrDefault(i)));
                }).ToList();

                variables = CreateVirtualMemberRefs(variables, typeElement, i, initValues, typeNode.Elements[i].GetTextSpan());
            }
            var declaration = new VariableDeclarationExpression(null, variables, textSpan);

            return(new ExpressionStatement(declaration));
        }
コード例 #7
0
            /// <summary>
            /// Checks if the variable is already defined and replaces the VariableDefinition with
            /// VariableReference
            /// </summary>
            /// <param name="node">
            /// A <see cref="VariableDeclarationExpression"/>
            /// </param>
            /// <returns>
            /// A <see cref="ICodeNode"/>
            /// </returns>
            public override ICodeNode VisitVariableDeclarationExpression(VariableDeclarationExpression node)
            {
                VariableDefinition varDef;

                if (localVarSubstitution.TryGetValue(node.Variable, out varDef))
                {
                    if (!isVariableDefined.Add(node.Variable))
                    {
                        var varRef = lastAssignment.Target as VariableDeclarationExpression;
                        if (varRef != null)
                        {
                            if (varRef.Variable == varDef)
                            {
                                throw new NotImplementedException();
                            }
                            else
                            {
                                return(new VariableReferenceExpression(varDef));
                            }
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        node.Variable = varDef;
                    }
                }
                return(base.VisitVariableDeclarationExpression(node));
            }
コード例 #8
0
        private void GenerateForeachStatement()
        {
            if (foreachVariable == null)
            {
                foreachVariable = new VariableDefinition(foreachVariableType, this.methodContext.Method);
                foreachVariableInstructions.Clear();
                this.methodContext.VariablesToRename.Add(foreachVariable);
            }
            VariableDeclarationExpression vd = new VariableDeclarationExpression(foreachVariable, foreachVariableInstructions);

            Expression foreachCollectionExpression = foreachCollection.CloneAndAttachInstructions(foreachCollectionInstructions);

            if (foreachCollectionExpression is BaseReferenceExpression)
            {
                foreachCollectionExpression = new ThisReferenceExpression(this.methodContext.Method.DeclaringType, foreachCollectionExpression.UnderlyingSameMethodInstructions);
            }

            @foreach = new ForEachStatement(vd, foreachCollectionExpression, foreachBody, foreachConditionInstructions, theTry.Finally.UnderlyingSameMethodInstructions);
            GetCurrentFixer gcf = new GetCurrentFixer(theEnumerator, foreachVariable);

            gcf.Visit(@foreach);

            IsEnumeratorUsedVisitor enumeratorUsedVisitor = new IsEnumeratorUsedVisitor(theEnumerator);

            enumeratorUsedVisitor.Visit(@foreach);
            isEnumeratorUsedInsideForEach = enumeratorUsedVisitor.IsEnumeratorUsed;
        }
コード例 #9
0
        public override Expression Parse(Parser parser)
        {
            parser.Consume();

            if (!parser.Match(TokenType.Identifier))
            {
                throw new ParsingException(string.Format("Expected identifier, found: {0}", parser.Lookahead.Type));
            }

            var variableDeclaration = new VariableDeclarationExpression();

            variableDeclaration.Identifier = parser.Lookahead.Value;

            parser.Consume();

            if (!parser.Match(TokenType.Assignment))
            {
                throw new ParsingException(string.Format("Expected assignment, found: {0}", parser.Lookahead.Type));
            }

            parser.Consume();

            variableDeclaration.Expression = parser.ParseExpression();

            return(variableDeclaration);
        }
コード例 #10
0
 public CatchClause(BlockStatement body, TypeReference type, VariableDeclarationExpression variable, Statement filter = null)
 {
     this.Body     = body;
     this.Type     = type;
     this.Variable = variable;
     this.Filter   = filter;
 }
コード例 #11
0
 public override void VisitVariableDeclarationExpression(VariableDeclarationExpression node)
 {
     if ((object)node.get_Variable() == (object)this.oldVariable)
     {
         node.set_Variable(this.newVariable);
     }
     return;
 }
コード例 #12
0
ファイル: CSharpWriter.cs プロジェクト: jma2400/cecil-old
        public override void VisitVariableDeclarationExpression(VariableDeclarationExpression node)
        {
            var variable = node.Variable;

            WriteReference(variable.VariableType);
            WriteSpace();
            Write(variable.Name);
        }
コード例 #13
0
        public override Statement Clone()
        {
            BlockStatement bodyClone = Body != null ? (BlockStatement)Body.Clone() : null;
            VariableDeclarationExpression variableClone = Variable != null ? (VariableDeclarationExpression)Variable.Clone() : null;
            Statement filterClone = Filter != null ? (Statement)Filter.Clone() : null;

            return(new CatchClause(bodyClone, Type, variableClone, filterClone));
        }
コード例 #14
0
 public CatchClause(BlockStatement body, TypeReference type, VariableDeclarationExpression variable, Statement filter = null)
 {
     base();
     this.set_Body(body);
     this.set_Type(type);
     this.set_Variable(variable);
     this.set_Filter(filter);
     return;
 }
コード例 #15
0
 public override void VisitVariableDeclarationExpression(VariableDeclarationExpression node)
 {
     if (node.Variable == variable)
     {
         found = true;
         return;
     }
     base.VisitVariableDeclarationExpression(node);
 }
コード例 #16
0
        /// <summary>
        /// Initializes a new instance of the FixedStatement class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the statement.
        /// </param>
        /// <param name="fixedVariable">
        /// The fixed variable.
        /// </param>
        internal FixedStatement(CsTokenList tokens, VariableDeclarationExpression fixedVariable)
            : base(StatementType.Fixed, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(fixedVariable, "fixedVariable");

            this.fixedVariable = fixedVariable;
            this.AddExpression(fixedVariable);
        }
コード例 #17
0
        public void VariableDeclaration()
        {
            Expression expression = parseStatement("a -> int;");

            VariableDeclarationExpression a = assertTypeAndCast <VariableDeclarationExpression>(expression);

            Assert.AreEqual(a.name, "a");
            Assert.AreEqual(a.typeName.name, "int");
        }
コード例 #18
0
        /// <summary>
        /// Initializes a new instance of the FixedStatement class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the statement.
        /// </param>
        /// <param name="fixedVariable">
        /// The fixed variable.
        /// </param>
        internal FixedStatement(CsTokenList tokens, VariableDeclarationExpression fixedVariable)
            : base(StatementType.Fixed, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(fixedVariable, "fixedVariable");

            this.fixedVariable = fixedVariable;
            this.AddExpression(fixedVariable);
        }
コード例 #19
0
 public override void VisitVariableDeclarationExpression(VariableDeclarationExpression node)
 {
     if (node.get_Variable() == this.variable)
     {
         this.found = true;
         return;
     }
     this.VisitVariableDeclarationExpression(node);
     return;
 }
コード例 #20
0
        private VariableDeclarationExpression GenerateLoopIndexExpression()
        {
            var variableDeclarationExpression = new VariableDeclarationExpression();

            variableDeclarationExpression.Identifier = "index";
            variableDeclarationExpression.Expression = new IntegerLiteralExpression {
                Value = 0
            };
            return(variableDeclarationExpression);
        }
コード例 #21
0
        public override void eSet(int featureID, object newValue)
        {
            switch (featureID)
            {
            case Java_PackageImpl.MODIFIER_VISIBILITY:
                visibility = (VisibilityKind)newValue;
                return;

            case Java_PackageImpl.MODIFIER_INHERITANCE:
                inheritance = (InheritanceKind)newValue;
                return;

            case Java_PackageImpl.MODIFIER_STATIC:
                static_ = (bool)newValue;
                return;

            case Java_PackageImpl.MODIFIER_TRANSIENT:
                transient = (bool)newValue;
                return;

            case Java_PackageImpl.MODIFIER_VOLATILE:
                volatile_ = (bool)newValue;
                return;

            case Java_PackageImpl.MODIFIER_NATIVE:
                native = (bool)newValue;
                return;

            case Java_PackageImpl.MODIFIER_STRICTFP:
                strictfp = (bool)newValue;
                return;

            case Java_PackageImpl.MODIFIER_SYNCHRONIZED:
                synchronized = (bool)newValue;
                return;

            case Java_PackageImpl.MODIFIER_BODYDECLARATION:
                bodyDeclaration = (BodyDeclaration)newValue;
                return;

            case Java_PackageImpl.MODIFIER_SINGLEVARIABLEDECLARATION:
                singleVariableDeclaration = (SingleVariableDeclaration)newValue;
                return;

            case Java_PackageImpl.MODIFIER_VARIABLEDECLARATIONSTATEMENT:
                variableDeclarationStatement = (VariableDeclarationStatement)newValue;
                return;

            case Java_PackageImpl.MODIFIER_VARIABLEDECLARATIONEXPRESSION:
                variableDeclarationExpression = (VariableDeclarationExpression)newValue;
                return;
            }
            base.eSet(featureID, newValue);
        }
コード例 #22
0
        public Ust VisitResource(JavaParser.ResourceContext context)
        {
            var type        = (TypeToken)Visit(context.classOrInterfaceType());
            var id          = (IdToken)Visit(context.variableDeclaratorId());
            var initializer = (Expression)Visit(context.expression());

            var result = new VariableDeclarationExpression(type,
                                                           new[] { new AssignmentExpression(id, initializer, context.variableDeclaratorId().GetTextSpan()) },
                                                           context.GetTextSpan());

            return(result);
        }
コード例 #23
0
        /// <summary>
        /// Determines the type which the <paramref name="variable"/> should have, according to its usage in the <paramref name="expression"/>.
        /// </summary>
        /// <param name="expression">The expression that uses the variable.</param>
        /// <param name="variable">The variable whose type is being infered.</param>
        /// <returns>Returns the ClassHierarchyNode coresponding to the infered type.</returns>
        private TypeReference GetUseExpressionTypeNode(Expression expression, VariableReference variable)
        {
            switch (expression.CodeNodeType)
            {
            case CodeNodeType.BaseCtorExpression:
            case CodeNodeType.ThisCtorExpression:
            case CodeNodeType.MethodInvocationExpression:
            case CodeNodeType.PropertyReferenceExpression:
                return(GetUseInMethodInvocation(expression as MethodInvocationExpression, variable));

            case CodeNodeType.BinaryExpression:
                return(GetUseInBinaryExpression(expression as BinaryExpression, variable));

            case CodeNodeType.VariableDeclarationExpression:
                VariableDeclarationExpression vdEx = expression as VariableDeclarationExpression;
                return(vdEx.Variable.VariableType);

            case CodeNodeType.VariableReferenceExpression:
                VariableReferenceExpression vrEx = expression as VariableReferenceExpression;
                return(vrEx.Variable == variable ? vrEx.Variable.VariableType : null);

            case CodeNodeType.CastExpression:
            case CodeNodeType.SafeCastExpression:
                return(typeSystem.Object);

            case CodeNodeType.ArrayIndexerExpression:
                return(GetUseInArrayIndexer(expression as ArrayIndexerExpression, variable));

            case CodeNodeType.ObjectCreationExpression:
                return(GetUseInObjectCreation(expression as ObjectCreationExpression, variable));

            case CodeNodeType.ReturnExpression:
                return(methodContext.Method.FixedReturnType);

            case CodeNodeType.ArrayCreationExpression:
                return(GetUseInArrayCreation(expression as ArrayCreationExpression, variable));

            case CodeNodeType.UnaryExpression:
                return(GetUseExpressionTypeNode((expression as UnaryExpression).Operand, variable));

            case CodeNodeType.BoxExpression:
                return((expression as BoxExpression).BoxedAs);

            case CodeNodeType.ThrowExpression:
                return(null);

            case CodeNodeType.FieldReferenceExpression:
                return((expression as FieldReferenceExpression).Field.DeclaringType);

            default:
                throw new ArgumentOutOfRangeException("Expression is not evaluated to any type.");
            }
        }
コード例 #24
0
ファイル: StatementVisitor.cs プロジェクト: Yikez978/PT.PM
        public override UstNode VisitVariableDeclaration(VariableDeclarationSyntax node)
        {
            var typeToken = ConvertType(base.Visit(node.Type));

            AssignmentExpression[] vars = node
                                          .Variables.Select(v => (AssignmentExpression)VisitAndReturnNullIfError(v))
                                          .ToArray();

            var result = new VariableDeclarationExpression(typeToken, vars, node.GetTextSpan(), FileNode);

            return(result);
        }
コード例 #25
0
        public UstNode VisitLocalVariableDeclaration(JavaParser.LocalVariableDeclarationContext context)
        {
            var type = (TypeToken)Visit(context.typeType());

            AssignmentExpression[] initializers = context.variableDeclarators().variableDeclarator()
                                                  .Select(varDec => (AssignmentExpression)Visit(varDec))
                                                  .Where(initializer => initializer != null).ToArray();

            var result = new VariableDeclarationExpression(type, initializers, context.GetTextSpan(), FileNode);

            return(result);
        }
コード例 #26
0
        private void CreateMethod(CatchClause catchClause, VariableDeclarationExpression variable, out Expression methodInvocationExpression)
        {
            methodInvocationExpression = null;

            BlockStatement filter = catchClause.Filter as BlockStatement;

            // Fixes the variable definition in the first statement.
            ExpressionStatement         firstStatement    = filter.Statements.First() as ExpressionStatement;
            BinaryExpression            assignment        = firstStatement.Expression as BinaryExpression;
            SafeCastExpression          safeCast          = assignment.Right as SafeCastExpression;
            VariableReferenceExpression variableReference = safeCast.Expression as VariableReferenceExpression;

            variableReference.Variable = variable.Variable;

            // Replace the variable reference in the last statement with return statement.
            ExpressionStatement lastStatement = filter.Statements.Last() as ExpressionStatement;

            lastStatement.Expression = new ReturnExpression(lastStatement.Expression, lastStatement.Expression.MappedInstructions);

            int              methodNumber = this.context.TypeContext.GeneratedFilterMethods.Count + this.methodsToBeDecompiled.Count;
            string           methodName   = string.Format(Constants.JustDecompileGeneratedFilterPattern, methodNumber);
            MethodDefinition method       = new MethodDefinition(methodName, Mono.Cecil.MethodAttributes.Private, this.context.MethodContext.Method.Module.TypeSystem.Boolean);

            method.Body = new MethodBody(method);
            // Practically no chance for duplicates, because of decrementing the MaxRID
            method.MetadataToken            = new MetadataToken(TokenType.Method, MaxRID - (uint)methodNumber);
            method.IsStatic                 = this.context.MethodContext.Method.IsStatic;
            method.HasThis                  = !method.IsStatic;
            method.DeclaringType            = context.MethodContext.Method.DeclaringType;
            method.SemanticsAttributes      = MethodSemanticsAttributes.None;
            method.IsJustDecompileGenerated = true;

            DecompilationContext newContext = new DecompilationContext(CloneAndReplaceMethodBody(this.context.MethodContext, method.Body), this.context.TypeContext);

            VariableDefinition variableDefinition = variableReference.Variable.Resolve();

            if (!newContext.MethodContext.VariableDefinitionToNameMap.ContainsKey(variableDefinition))
            {
                newContext.MethodContext.VariableDefinitionToNameMap[variableDefinition] = Constants.JustDecompileGeneratedException;
            }
            else
            {
                newContext.MethodContext.VariableDefinitionToNameMap.Add(variableDefinition, Constants.JustDecompileGeneratedException);
            }

            newContext.MethodContext.VariablesToNotDeclare.Add(variable.Variable);

            methodsToBeDecompiled.Add(new FilterMethodToBeDecompiled(method, catchClause, newContext, catchClause.Filter as BlockStatement));

            List <Expression> parameters = AddAllParameters(catchClause, method, variable);

            methodInvocationExpression = CreateMethodInvocation(method, parameters);
        }
コード例 #27
0
ファイル: StatementVisitor.cs プロジェクト: Yikez978/PT.PM
        public override UstNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
        {
            var type = ConvertType(base.Visit(node.Declaration.Type));

            AssignmentExpression[] variables =
                node.Declaration.Variables.Select(v => (AssignmentExpression)VisitAndReturnNullIfError(v))
                .ToArray();

            var resultExpression = new VariableDeclarationExpression(type, variables, node.GetTextSpan(), FileNode);
            var result           = new ExpressionStatement(resultExpression);

            return(result);
        }
コード例 #28
0
        /// <summary>
        /// Initializes a new instance of the ForeachStatement class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the statement.
        /// </param>
        /// <param name="variable">
        /// The variable declared in for each statement declaration.
        /// </param>
        /// <param name="item">
        /// The item being iterated over.
        /// </param>
        internal ForeachStatement(CsTokenList tokens, VariableDeclarationExpression variable, Expression item)
            : base(StatementType.Foreach, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(variable, "variable");
            Param.AssertNotNull(item, "item");

            this.variable = variable;
            this.item     = item;

            this.AddExpression(variable);
            this.AddExpression(item);
        }
コード例 #29
0
ファイル: DslUstConverter.cs プロジェクト: Yikez978/PT.PM
        public UstNode VisitVariableDeclarationExpression([NotNull] DslParser.VariableDeclarationExpressionContext context)
        {
            var literal   = (Token)VisitLiteralOrPatternId(context.literalOrPatternId());
            var typeToken = new TypeToken(literal.TextValue, literal.TextSpan, null);
            var left      = (Expression)VisitExpression(context.expression(0));
            var right     = (Expression)VisitExpression(context.expression(1));
            var variables = new AssignmentExpression[] {
                new AssignmentExpression(left, right, left.TextSpan.Union(right.TextSpan), null)
            };
            var result = new VariableDeclarationExpression(typeToken, variables, context.GetTextSpan(), null);

            return(result);
        }
コード例 #30
0
        /// <summary>
        /// Initializes a new instance of the ForeachStatement class.
        /// </summary>
        /// <param name="tokens">
        /// The list of tokens that form the statement.
        /// </param>
        /// <param name="variable">
        /// The variable declared in for each statement declaration.
        /// </param>
        /// <param name="item">
        /// The item being iterated over.
        /// </param>
        internal ForeachStatement(CsTokenList tokens, VariableDeclarationExpression variable, Expression item)
            : base(StatementType.Foreach, tokens)
        {
            Param.AssertNotNull(tokens, "tokens");
            Param.AssertNotNull(variable, "variable");
            Param.AssertNotNull(item, "item");

            this.variable = variable;
            this.item = item;

            this.AddExpression(variable);
            this.AddExpression(item);
        }
コード例 #31
0
        public override Statement CloneStatementOnly()
        {
            BlockStatement bodyClone = Body != null ? (BlockStatement)Body.CloneStatementOnly() : null;

            if (Filter != null)
            {
                return(new CatchClause((BlockStatement)Filter.CloneStatementOnly(), bodyClone));
            }
            else
            {
                VariableDeclarationExpression variableClone = Variable != null ? (VariableDeclarationExpression)Variable.CloneExpressionOnly() : null;
                return(new CatchClause(bodyClone, Type, variableClone));
            }
        }
コード例 #32
0
        public override void VisitVariableDeclarationExpression(VariableDeclarationExpression node)
        {
            try
            {
                var variable = node.Variable;

                WriteReference(variable.VariableType);
                WriteSpace();
                Write(variable.Name);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("in VisitVariableDeclarationExpression: " + ex.Message);
            }
        }
コード例 #33
0
        /// <summary>
        /// Reads an expression beginning with two unknown words.
        /// </summary>
        /// <param name="type">The type of the variable.</param>
        /// <param name="previousPrecedence">The precedence of the previous expression.</param>
        /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param>
        /// <returns>Returns the expression.</returns>
        private VariableDeclarationExpression GetVariableDeclarationExpression(
            Expression type, ExpressionPrecedence previousPrecedence, bool unsafeCode)
        {
            Param.AssertNotNull(type, "type");
            Param.Ignore(previousPrecedence);
            Param.Ignore(unsafeCode);

            Debug.Assert(
                type.ExpressionType == ExpressionType.Literal ||
                type.ExpressionType == ExpressionType.MemberAccess,
                "The left side of a variable declaration must either be a literal or a member access.");

            VariableDeclarationExpression expression = null;
            if (this.CheckPrecedence(previousPrecedence, ExpressionPrecedence.None))
            {
                // Convert the type expression to a literal type token expression.
                LiteralExpression literalType = null;
                if (type.ExpressionType == ExpressionType.Literal)
                {
                    literalType = type as LiteralExpression;
                    if (!(literalType.Token is TypeToken))
                    {
                        literalType = null;
                    }
                }

                if (literalType == null)
                {
                    literalType = this.ConvertTypeExpression(type);
                }

                // Get each declarator.
                List<VariableDeclaratorExpression> declarators = new List<VariableDeclaratorExpression>();

                while (true)
                {
                    // Get the next word.
                    Symbol symbol = this.GetNextSymbol(SymbolType.Other);

                    // Get the identifier.
                    LiteralExpression identifier = this.GetLiteralExpression(unsafeCode);
                    if (identifier == null || identifier.Tokens.First == null)
                    {
                        throw new SyntaxException(this.document.SourceCode, symbol.LineNumber);
                    }

                    // Get the initializer if it exists.
                    Expression initializer = null;

                    symbol = this.GetNextSymbol();
                    if (symbol.SymbolType == SymbolType.Equals)
                    {
                        // Add the equals token.
                        this.tokens.Add(this.GetOperatorToken(OperatorType.Equals));

                        // Check whether this is an array initializer.
                        symbol = this.GetNextSymbol();

                        if (symbol.SymbolType == SymbolType.OpenCurlyBracket)
                        {
                            initializer = this.GetArrayInitializerExpression(unsafeCode);
                        }
                        else
                        {
                            initializer = this.GetNextExpression(ExpressionPrecedence.None, unsafeCode);
                        }
                    }

                    // Create the token list for the declarator.
                    CsTokenList partialTokens = new CsTokenList(
                        this.tokens, identifier.Tokens.First, this.tokens.Last);

                    // Create and add the declarator.
                    declarators.Add(new VariableDeclaratorExpression(partialTokens, identifier, initializer));

                    // Now check if the next character is a comma. If so there is another declarator.
                    symbol = this.GetNextSymbol();
                    if (symbol.SymbolType != SymbolType.Comma)
                    {
                        // There are no more declarators.
                        break;
                    }

                    // Add the comma.
                    this.tokens.Add(this.GetToken(CsTokenType.Comma, SymbolType.Comma));
                }

                // Create the token list for the expression.
                CsTokenList tokenList = new CsTokenList(this.tokens, type.Tokens.First, this.tokens.Last);

                // Create the expression.
                expression = new VariableDeclarationExpression(tokenList, literalType, declarators.ToArray());
            }

            return expression;
        }