public override BlockStatement ParseBlock()
        {
            lexer.NextToken();
            compilationUnit = new CompilationUnit();

            BlockStatement blockStmt = new BlockStatement();

            blockStmt.StartLocation = la.Location;
            BlockStart(blockStmt);

            while (la.kind != Tokens.EOF)
            {
                Token oldLa = la;
                Statement();
                if (la == oldLa)
                {
                    // did not advance lexer position, we cannot parse this as a statement block
                    return(null);
                }
            }

            BlockEnd();
            // if lexer didn't return any tokens, use position of the EOF token in "la"
            blockStmt.EndLocation = (t ?? la).EndLocation;
            Expect(Tokens.EOF);
            blockStmt.AcceptVisitor(new SetParentVisitor(), null);
            return(blockStmt);
        }
        void ReplaceAllFunctionAssignments(BlockStatement block, string functionName, TypeReference typeReference)
        {
            ReturnStatementForFunctionAssignment visitor = new ReturnStatementForFunctionAssignment(functionName);

            block.AcceptVisitor(visitor, null);
            if (visitor.expressionsToReplace.Count == 1 && !visitor.hasExit && IsAssignmentTo(block.Children.Last(), functionName))
            {
                Expression returnValue = GetAssignmentFromStatement(block.Children.Last()).Right;
                block.Children.RemoveAt(block.Children.Count - 1);
                block.Return(returnValue);
            }
            else
            {
                if (visitor.expressionsToReplace.Count > 0)
                {
                    foreach (var expr in visitor.expressionsToReplace)
                    {
                        expr.Identifier = FunctionReturnValueName;
                    }
                    Expression init;
                    init = ExpressionBuilder.CreateDefaultValueForType(typeReference);
                    block.Children.Insert(0, new LocalVariableDeclaration(new VariableDeclaration(FunctionReturnValueName, init, typeReference)));
                    block.Children[0].Parent = block;
                    block.Return(new IdentifierExpression(FunctionReturnValueName));
                }
            }
        }
        public void VisitBlockStatement_TypeofConstantsIncluded()
        {
            //-- arrange

            var block = new BlockStatement(
                new ReturnStatement {
                Expression = new ConstantExpression {
                    Value = typeof(IDisposable)
                }
            },
                new ReturnStatement {
                Expression = new ConstantExpression {
                    Value = (TypeMember)typeof(IFormattable)
                }
            }
                );

            var foundTypes = new HashSet <TypeMember>();
            var visitor    = new TypeReferenceStatementVisitor(foundTypes);

            //-- act

            block.AcceptVisitor(visitor);

            //-- assert

            foundTypes.Should().BeEquivalentTo(new TypeMember[] { typeof(IDisposable), typeof(IFormattable) });
        }
        private bool HasReferencesInSelection(ISelection selection, Variable variable)
        {
            FindReferenceVisitor frv = new FindReferenceVisitor(CSharpNameComparer, variable.Name, selection.StartPosition, selection.EndPosition);
            var statement            = new BlockStatement();

            statement.Children = selection.Nodes;
            statement.AcceptVisitor(frv, null);
            return(frv.Identifiers.Count > 0);
        }
示例#5
0
            public GLSLVisitor(BlockStatement block, DecompilerContext ctx)
            {
                var trans1 = new ReplaceMethodCallsWithOperators(ctx);
                var trans2 = new RenameLocals();

                ((IAstTransform)trans1).Run(block);
                trans2.Run(block);

                Result  = block.AcceptVisitor(this, 0).ToString();
                Result += Environment.NewLine;
            }
示例#6
0
        public GlslVisitor(BlockStatement block, CustomAttribute attr, DecompilerContext ctx)
            : this()
        {
            _attr = attr;

            var trans1 = new ReplaceMethodCallsWithOperators(ctx);
            var trans2 = new RenameLocals();
            ((IAstTransform)trans1).Run(block);
            trans2.Run(block);

            Result = block.AcceptVisitor(this, 0).ToString();
            Result += Environment.NewLine;
        }
			public override void VisitBlockStatement(BlockStatement blockStatement)
			{
				var assignmentAnalysis = new ConvertToConstantIssue.VariableUsageAnalyzation (ctx);
				var newVars = new List<Tuple<VariableInitializer, IVariable, VariableState>>();
				blockStatement.AcceptVisitor(assignmentAnalysis); 
					foreach (var variable in fieldStack.Pop()) {
						var state = assignmentAnalysis.GetStatus(variable.Item2);
						if (state == VariableState.Changed)
							continue;
						newVars.Add(new Tuple<VariableInitializer, IVariable, VariableState> (variable.Item1, variable.Item2, state));
					}
					fieldStack.Push(newVars);
			}
示例#8
0
        public HlslVisitor(BlockStatement block, CustomAttribute attr, ResolveVisitor resolver, DecompilerContext ctx)
            : this()
        {
            _attr     = attr;
            _resolver = resolver;
            _resolver.Scan(block);

            var trans1 = new ReplaceMethodCallsWithOperators(ctx);
            var trans2 = new RenameLocals();

            ((IAstTransform)trans1).Run(block);
            trans2.Run(block);

            Result  = block.AcceptVisitor(this, 0).ToString();
            Result += Environment.NewLine;
        }
        void processBody(BlockStatement bs, TBody tb)
        {
            tb.text = bs.GetText();
            var ivisitor = new FindAllMemberReference();

            bs.AcceptVisitor(ivisitor);
            var ilist = ivisitor.memberRefList;

            foreach (var expr in ilist)
            {
                var invoke = new TInvoke();
                invoke.target = expr.Target.GetText();
                invoke.member = expr.MemberName;
                invoke.type   = GetTargetTypeString(expr);
                tb.invokes.Add(invoke);
            }
        }
 public override void VisitBlockStatement(BlockStatement blockStatement)
 {
     base.VisitBlockStatement(blockStatement);
     if (blockStatement.Parent is EntityDeclaration || blockStatement.Parent is Accessor)
     {
         var assignmentAnalysis = new VariableUsageAnalyzation(ctx);
         var newVars            = new List <Tuple <VariableInitializer, IVariable> >();
         blockStatement.AcceptVisitor(assignmentAnalysis);
         foreach (var variable in fieldStack.Pop())
         {
             if (assignmentAnalysis.GetStatus(variable.Item2) == VariableState.Changed)
             {
                 continue;
             }
             newVars.Add(variable);
         }
         fieldStack.Push(newVars);
     }
 }
        public void VisitBlockStatement_LocalVariableTypesIncluded()
        {
            //-- arrange

            var block = new BlockStatement(
                new VariableDeclarationStatement {
                Variable = new LocalVariable {
                    Name = "x", Type = typeof(IDisposable)
                }
            }
                );

            var foundTypes = new HashSet <TypeMember>();
            var visitor    = new TypeReferenceStatementVisitor(foundTypes);

            //-- act

            block.AcceptVisitor(visitor);

            //-- assert

            foundTypes.Should().BeEquivalentTo(new TypeMember[] { typeof(IDisposable) });
        }
示例#12
0
        bool HandleAnonymousMethod(ObjectCreateExpression objectCreateExpression, Expression target, MethodReference methodRef)
        {
            if (!context.Settings.AnonymousMethods)
            {
                return(false);                // anonymous method decompilation is disabled
            }
            if (target != null && !(target is IdentifierExpression || target is ThisReferenceExpression || target is NullReferenceExpression))
            {
                return(false);                // don't copy arbitrary expressions, deal with identifiers only
            }
            // Anonymous methods are defined in the same assembly
            MethodDefinition method = methodRef.ResolveWithinSameModule();

            if (!IsAnonymousMethod(context, method))
            {
                return(false);
            }

            // Create AnonymousMethodExpression and prepare parameters
            AnonymousMethodExpression ame = new AnonymousMethodExpression();

            ame.CopyAnnotationsFrom(objectCreateExpression); // copy ILRanges etc.
            ame.RemoveAnnotations <MethodReference>();       // remove reference to delegate ctor
            ame.AddAnnotation(method);                       // add reference to anonymous method
            ame.Parameters.AddRange(AstBuilder.MakeParameters(method, isLambda: true));
            ame.HasParameterList = true;

            // rename variables so that they don't conflict with the parameters:
            foreach (ParameterDeclaration pd in ame.Parameters)
            {
                EnsureVariableNameIsAvailable(objectCreateExpression, pd.Name);
            }

            // Decompile the anonymous method:

            DecompilerContext subContext = context.Clone();

            subContext.CurrentMethod        = method;
            subContext.CurrentMethodIsAsync = false;
            subContext.ReservedVariableNames.AddRange(currentlyUsedVariableNames);
            BlockStatement body = AstMethodBodyBuilder.CreateMethodBody(method, subContext, ame.Parameters);

            TransformationPipeline.RunTransformationsUntil(body,
                                                           v => v is DelegateConstruction,
                                                           subContext);
            body.AcceptVisitor(this, null);


            bool isLambda = false;

            if (ame.Parameters.All(p => p.ParameterModifier == ParameterModifier.None))
            {
                isLambda = (body.Statements.Count == 1 && body.Statements.Single() is ReturnStatement);
            }
            // Remove the parameter list from an AnonymousMethodExpression if the original method had no names,
            // and the parameters are not used in the method body
            if (!isLambda && method.Parameters.All(p => string.IsNullOrEmpty(p.Name)))
            {
                var parameterReferencingIdentifiers =
                    from ident in body.Descendants.OfType <IdentifierExpression>()
                    let v = ident.Annotation <ILVariable>()
                            where v != null && v.IsParameter && method.Parameters.Contains(v.OriginalParameter)
                            select ident;
                if (!parameterReferencingIdentifiers.Any())
                {
                    ame.Parameters.Clear();
                    ame.HasParameterList = false;
                }
            }

            // Replace all occurrences of 'this' in the method body with the delegate's target:
            foreach (AstNode node in body.Descendants)
            {
                if (node is ThisReferenceExpression)
                {
                    node.ReplaceWith(target.Clone());
                }
            }
            Expression replacement;

            if (isLambda)
            {
                LambdaExpression lambda = new LambdaExpression();
                lambda.CopyAnnotationsFrom(ame);
                ame.Parameters.MoveTo(lambda.Parameters);
                Expression returnExpr = ((ReturnStatement)body.Statements.Single()).Expression;
                returnExpr.Remove();
                lambda.Body = returnExpr;
                replacement = lambda;
            }
            else
            {
                ame.Body    = body;
                replacement = ame;
            }
            var expectedType = objectCreateExpression.Annotation <TypeInformation>().ExpectedType.Resolve();

            if (expectedType != null && !expectedType.IsDelegate())
            {
                var simplifiedDelegateCreation = (ObjectCreateExpression)objectCreateExpression.Clone();
                simplifiedDelegateCreation.Arguments.Clear();
                simplifiedDelegateCreation.Arguments.Add(replacement);
                replacement = simplifiedDelegateCreation;
            }
            objectCreateExpression.ReplaceWith(replacement);
            return(true);
        }
示例#13
0
        bool HandleAnonymousMethod(ObjectCreateExpression objectCreateExpression, Expression target, MethodReference methodRef)
        {
            if (!context.Settings.AnonymousMethods)
            {
                return(false);                // anonymous method decompilation is disabled
            }
            // Anonymous methods are defined in the same assembly
            MethodDefinition method = methodRef.ResolveWithinSameModule();

            if (!IsAnonymousMethod(context, method))
            {
                return(false);
            }

            // Create AnonymousMethodExpression and prepare parameters
            AnonymousMethodExpression ame = new AnonymousMethodExpression();

            ame.Parameters.AddRange(AstBuilder.MakeParameters(method.Parameters));
            ame.HasParameterList = true;

            // Decompile the anonymous method:

            DecompilerContext subContext = context.Clone();

            subContext.CurrentMethod = method;
            BlockStatement body = AstMethodBodyBuilder.CreateMethodBody(method, subContext, ame.Parameters);

            TransformationPipeline.RunTransformationsUntil(body, v => v is DelegateConstruction, subContext);
            body.AcceptVisitor(this, null);


            bool isLambda = false;

            if (ame.Parameters.All(p => p.ParameterModifier == ParameterModifier.None))
            {
                isLambda = (body.Statements.Count == 1 && body.Statements.Single() is ReturnStatement);
            }
            // Remove the parameter list from an AnonymousMethodExpression if the original method had no names,
            // and the parameters are not used in the method body
            if (!isLambda && method.Parameters.All(p => string.IsNullOrEmpty(p.Name)))
            {
                var parameterReferencingIdentifiers =
                    from ident in body.Descendants.OfType <IdentifierExpression>()
                    let v = ident.Annotation <ILVariable>()
                            where v != null && v.IsParameter && method.Parameters.Contains(v.OriginalParameter)
                            select ident;
                if (!parameterReferencingIdentifiers.Any())
                {
                    ame.Parameters.Clear();
                    ame.HasParameterList = false;
                }
            }

            // Replace all occurrences of 'this' in the method body with the delegate's target:
            foreach (AstNode node in body.Descendants)
            {
                if (node is ThisReferenceExpression)
                {
                    node.ReplaceWith(target.Clone());
                }
            }
            if (isLambda)
            {
                LambdaExpression lambda = new LambdaExpression();
                ame.Parameters.MoveTo(lambda.Parameters);
                Expression returnExpr = ((ReturnStatement)body.Statements.Single()).Expression;
                returnExpr.Remove();
                lambda.Body = returnExpr;
                objectCreateExpression.ReplaceWith(lambda);
            }
            else
            {
                ame.Body = body;
                objectCreateExpression.ReplaceWith(ame);
            }
            return(true);
        }