private bool EvaluateReturn(ReturnExpression expression, InterpreterScope scope) { ExpressionBase result; var returnScope = new InterpreterScope(scope) { Context = new AssignmentExpression(new VariableExpression("@return"), expression.Value) }; if (!expression.Value.ReplaceVariables(returnScope, out result)) { Error = result as ParseErrorExpression; return(false); } var functionCall = result as FunctionCallExpression; if (functionCall != null) { if (!CallFunction(functionCall, returnScope)) { return(false); } scope.ReturnValue = returnScope.ReturnValue; } else { scope.ReturnValue = result; } scope.IsComplete = true; return(true); }
public override ICodeNode VisitObjectCreationExpression(ObjectCreationExpression node) { if (state == State.ReplaceDelegate && node.Arguments != null && node.Arguments.Count == 2 && node.Arguments[0].CodeNodeType == CodeNodeType.VariableReferenceExpression && node.Arguments[1].CodeNodeType == CodeNodeType.MethodReferenceExpression && delegateCopies.Contains((node.Arguments[0] as VariableReferenceExpression).Variable)) { //final check inserted here for optimization TypeDefinition objectType = node.Constructor.DeclaringType.Resolve(); if (objectType == null || objectType.BaseType == null || objectType.BaseType.FullName != "System.MulticastDelegate") { return(base.VisitObjectCreationExpression(node)); } MethodReference methodReference = (node.Arguments[1] as MethodReferenceExpression).Method; MethodDefinition methodDefinition = (node.Arguments[1] as MethodReferenceExpression).MethodDefinition; MethodSpecificContext delegateMethodContext = new MethodSpecificContext(methodDefinition.Body); DecompilationContext innerContext = new DecompilationContext(delegateMethodContext, context.TypeContext, context.ModuleContext, context.AssemblyContext); delegateMethodContext.FieldToExpression = fieldDefToAssignedValueMap; BlockStatement methodStatements = methodDefinition.Body.DecompileLambda(Language, innerContext); if ((methodStatements.Statements.Count == 1) && (methodStatements.Statements[0].CodeNodeType == CodeNodeType.ExpressionStatement) && ((methodStatements.Statements[0] as ExpressionStatement).Expression.CodeNodeType == CodeNodeType.ReturnExpression)) { ReturnExpression returnExpression = (methodStatements.Statements[0] as ExpressionStatement).Expression as ReturnExpression; ShortFormReturnExpression shortFormReturnExpression = new ShortFormReturnExpression(returnExpression.Value, returnExpression.MappedInstructions); methodStatements = new BlockStatement(); methodStatements.Statements.Add(new ExpressionStatement(shortFormReturnExpression)); } this.context.MethodContext.VariableDefinitionToNameMap.AddRange(innerContext.MethodContext.VariableDefinitionToNameMap); this.context.MethodContext.VariableNamesCollection.UnionWith(innerContext.MethodContext.VariableNamesCollection); this.context.MethodContext.AddInnerMethodParametersToContext(innerContext.MethodContext); this.context.MethodContext.GotoStatements.AddRange(innerContext.MethodContext.GotoStatements); this.context.MethodContext.GotoLabels.AddRange(innerContext.MethodContext.GotoLabels); ExpressionCollection expressionCollection = new ExpressionCollection(); bool hasAnonymousParamterer = LambdaExpressionsHelper.HasAnonymousParameter(methodDefinition.Parameters); foreach (ParameterDefinition parameter in methodDefinition.Parameters) { expressionCollection.Add(new LambdaParameterExpression(parameter, !hasAnonymousParamterer, null)); } delegatesFound.Add(methodStatements); LambdaExpression lambdaExpression = new LambdaExpression(expressionCollection, methodStatements, methodDefinition.IsAsync(), methodDefinition.IsFunction(), methodReference.Parameters, false, node.Arguments[1].MappedInstructions) { ExpressionType = objectType }; DelegateCreationExpression result = new DelegateCreationExpression(node.Constructor.DeclaringType, lambdaExpression, node.Arguments[0], node.MappedInstructions); return(result); } return(base.VisitObjectCreationExpression(node)); }
public override void VisitReturnExpression(ReturnExpression node) { this.states.Push(3); this.VisitReturnExpression(node); dummyVar0 = this.states.Pop(); return; }
public static IMaybe <(Block, int)> OneLineStatement(string source, int index) { var oldTabs = Tabs; Tabs = "/s*"; var whenUnlessParser = new WhenUnlessParser(); try { var parser = new StatementParser(); if (parser.Scan(source, index)) { var verb = ReturnExpression.Convert(parser.Verb); whenUnlessParser.OtherVerb = verb; if (whenUnlessParser.Scan(source, parser.Position)) { return((Inline(whenUnlessParser.Verb), whenUnlessParser.Position).Some()); } else { return((Inline(verb), parser.Position).Some()); } } return(none <(Block, int)>()); } finally { Tabs = oldTabs; } }
public virtual Expression Visit(ReturnExpression expression) { return(new ReturnExpression(expression.Token, expression.Value.Accept(this)) { EndToken = expression.EndToken }); }
private bool CheckSetter(BlockStatement setterStatements) { if (setterStatements == null || setterStatements.Statements == null || setterStatements.Statements.Count != 2 || setterStatements.Statements[0].CodeNodeType != CodeNodeType.ExpressionStatement || setterStatements.Statements[1].CodeNodeType != CodeNodeType.ExpressionStatement) { return(false); } ReturnExpression returnExpression = (setterStatements.Statements[1] as ExpressionStatement).Expression as ReturnExpression; if (returnExpression == null || returnExpression.Value != null) { return(false); } BinaryExpression binaryExpression = (setterStatements.Statements[0] as ExpressionStatement).Expression as BinaryExpression; if (binaryExpression == null || !binaryExpression.IsAssignmentExpression || binaryExpression.Left.CodeNodeType != CodeNodeType.FieldReferenceExpression || binaryExpression.Right.CodeNodeType != CodeNodeType.ArgumentReferenceExpression) { return(false); } return(CheckFieldReferenceExpression(binaryExpression.Left as FieldReferenceExpression)); }
public override ICodeNode VisitReturnExpression(ReturnExpression node) { dummyVar0 = this.VisitReturnExpression(node); V_0 = node.get_Value() as LiteralExpression; if (V_0 != null) { if (!this.isBoolReturnType) { if (this.isCharReturnType) { this.FixCharLiteral(V_0); } } else { this.FixBooleanLiteral(V_0); } } V_1 = node.get_Value() as ExplicitCastExpression; if (V_1 != null && String.op_Inequality(V_1.get_ExpressionType().get_FullName(), this.methodContext.get_Method().get_ReturnType().get_FullName()) && this.isCharReturnType && String.op_Equality(V_1.get_ExpressionType().get_FullName(), this.currentTypeSystem.get_UInt16().get_FullName())) { V_1.set_TargetType(this.currentTypeSystem.get_Char()); } return(node); }
public override ICodeNode VisitReturnExpression(ReturnExpression node) { node = (ReturnExpression)this.VisitReturnExpression(node); V_0 = this.context.get_MethodContext().get_Method().get_ReturnType().Resolve(); if (V_0 == null) { return(node); } if (node.get_Value() == null) { return(node); } if (V_0.get_IsEnum() && !this.context.get_MethodContext().get_Method().get_ReturnType().get_IsArray() && node.get_Value().get_ExpressionType() == null || String.op_Inequality(node.get_Value().get_ExpressionType().get_FullName(), V_0.get_FullName())) { if (node.get_Value() as LiteralExpression == null) { if (node.get_Value() as ExplicitCastExpression == null || (node.get_Value() as ExplicitCastExpression).get_Expression() as LiteralExpression == null) { if (node.get_Value().get_HasType() && this.NeedsCast(node.get_Value().get_ExpressionType(), V_0)) { node.set_Value(new ExplicitCastExpression(node.get_Value(), V_0, null)); } } else { node.set_Value(EnumHelper.GetEnumExpression(V_0, (node.get_Value() as ExplicitCastExpression).get_Expression() as LiteralExpression, this.typeSystem)); } } else { node.set_Value(EnumHelper.GetEnumExpression(V_0, node.get_Value() as LiteralExpression, this.typeSystem)); } } return(node); }
public override ICodeNode VisitReturnExpression(ReturnExpression node) { base.VisitReturnExpression(node); LiteralExpression literalValue = node.Value as LiteralExpression; if (literalValue != null) { if (isBoolReturnType) { FixBooleanLiteral(literalValue); } else if (isCharReturnType) { FixCharLiteral(literalValue); } } CastExpression castValue = node.Value as CastExpression; if (castValue != null) { if (castValue.ExpressionType.FullName != methodContext.Method.ReturnType.FullName) { if (isCharReturnType && castValue.ExpressionType.FullName == currentTypeSystem.UInt16.FullName) { castValue.TargetType = currentTypeSystem.Char; } } } return(node); }
public override void VisitReturnExpression(ReturnExpression node) { Visit(node.Value); if (this.searchResult == UsageFinderSearchResult.NotFound) { this.searchResult = UsageFinderSearchResult.Used; } }
private TypeDefinition GetGeneratedType() { if (statements[0].CodeNodeType != CodeNodeType.ExpressionStatement) { return(null); } ExpressionStatement expressionStatement = statements[0] as ExpressionStatement; ObjectCreationExpression objectCreationExpression; if (expressionStatement.Expression.CodeNodeType == CodeNodeType.BinaryExpression) { if (!(expressionStatement.Expression as BinaryExpression).IsAssignmentExpression) { return(null); } BinaryExpression assingExpression = expressionStatement.Expression as BinaryExpression; if (assingExpression.Right.CodeNodeType != CodeNodeType.ObjectCreationExpression) { return(null); } objectCreationExpression = assingExpression.Right as ObjectCreationExpression; } else if (expressionStatement.Expression.CodeNodeType == CodeNodeType.ReturnExpression) { ReturnExpression returnExpression = expressionStatement.Expression as ReturnExpression; if (returnExpression.Value == null || returnExpression.Value.CodeNodeType != CodeNodeType.ObjectCreationExpression) { return(null); } objectCreationExpression = returnExpression.Value as ObjectCreationExpression; } else { return(null); } MethodReference constructor = objectCreationExpression.Constructor; if (constructor == null || constructor.DeclaringType == null) { return(null); } TypeDefinition declaringType = constructor.DeclaringType.Resolve(); if (declaringType == null || !declaringType.IsNestedPrivate || !declaringType.HasCompilerGeneratedAttribute()) { return(null); } return(declaringType); }
public override void VisitReturnExpression(ReturnExpression node) { this.VisitReturnExpression(node); if (node.get_Value() != null && node.get_Value().get_HasType() && this.ShouldAddCast(node.get_Value().get_ExpressionType().Resolve(), this.decompiledMethodReturnType)) { node.set_Value(new ExplicitCastExpression(node.get_Value(), this.decompiledMethodReturnType, null)); } return; }
public void TestAppendString() { var expr = new ReturnExpression(new VariableExpression("variable")); var builder = new StringBuilder(); expr.AppendString(builder); Assert.That(builder.ToString(), Is.EqualTo("return variable")); }
public override void VisitReturnExpression(ReturnExpression node) { this.Visit(node.get_Value()); if (this.searchResult == UsageFinderSearchResult.NotFound) { this.searchResult = 2; } return; }
public void TestNestedExpressions() { var variable = new VariableExpression("variable"); var expr = new ReturnExpression(variable); var nested = ((INestedExpressions)expr).NestedExpressions; Assert.That(nested.Count(), Is.EqualTo(1)); Assert.That(nested.Contains(variable)); }
public override Expression Parse(Parser parser) { parser.Consume(); var returnExpression = new ReturnExpression(); returnExpression.Expression = parser.ParseExpression(); return(returnExpression); }
public void TestGetModifications() { var variable = new VariableExpression("variable"); var expr = new ReturnExpression(variable); var modifications = new HashSet <string>(); ((INestedExpressions)expr).GetModifications(modifications); Assert.That(modifications.Count, Is.EqualTo(0)); }
public int Visit(ReturnExpression expression) { _writer.Write("return"); if (expression.Value != null) { _writer.Write(" "); expression.Value.Accept(this); } return(0); }
public void TestGetDependencies() { var variable = new VariableExpression("variable"); var expr = new ReturnExpression(variable); var dependencies = new HashSet <string>(); ((INestedExpressions)expr).GetDependencies(dependencies); Assert.That(dependencies.Count, Is.EqualTo(1)); Assert.That(dependencies.Contains("variable")); }
private Ast ReturnStatement() { if (!_tokenStream.IsMatch <ReturnToken>()) { return(null); } ReturnToken token = _tokenStream.Take <ReturnToken>(); Ast expr = new ReturnExpression(token, Expression()); _tokenStream.Take <SemicolonToken>(); return(expr); }
public override void VisitReturnExpression(ReturnExpression node) { /// Add cast when the returned value is of enum type, but the signature of the method declares integer type. base.VisitReturnExpression(node); if (node.Value != null && node.Value.HasType) { TypeDefinition returnedType = node.Value.ExpressionType.Resolve(); if (ShouldAddCast(returnedType, decompiledMethodReturnType)) { node.Value = new CastExpression(node.Value, decompiledMethodReturnType, null); } } }
private bool CheckGetter() { BlockStatement getterStatements = GetStatements(propertyDef.GetMethod.Body); if (getterStatements == null || getterStatements.Statements == null || getterStatements.Statements.Count != 1 && getterStatements.Statements.Count != 2 || getterStatements.Statements[0].CodeNodeType != CodeNodeType.ExpressionStatement) { return(false); } FieldReferenceExpression fieldRefExpression; if (getterStatements.Statements.Count == 1) { ReturnExpression returnExpression = (getterStatements.Statements[0] as ExpressionStatement).Expression as ReturnExpression; if (returnExpression == null || returnExpression.Value == null || returnExpression.Value.CodeNodeType != CodeNodeType.FieldReferenceExpression) { return(false); } fieldRefExpression = returnExpression.Value as FieldReferenceExpression; } else { BinaryExpression binaryExpression = (getterStatements.Statements[0] as ExpressionStatement).Expression as BinaryExpression; if (binaryExpression == null || !binaryExpression.IsAssignmentExpression || binaryExpression.Left.CodeNodeType != CodeNodeType.VariableReferenceExpression || binaryExpression.Right.CodeNodeType != CodeNodeType.FieldReferenceExpression) { return(false); } if (getterStatements.Statements[1].CodeNodeType != CodeNodeType.ExpressionStatement) { return(false); } ReturnExpression returnExpression = (getterStatements.Statements[1] as ExpressionStatement).Expression as ReturnExpression; if (returnExpression == null || returnExpression.Value == null || returnExpression.Value.CodeNodeType != CodeNodeType.VariableReferenceExpression) { return(false); } fieldRefExpression = binaryExpression.Right as FieldReferenceExpression; } return(CheckFieldReferenceExpression(fieldRefExpression)); }
public void TestReplaceVariables() { var variable = new VariableExpression("variable"); var value = new IntegerConstantExpression(99); var expr = new ReturnExpression(variable); var scope = new InterpreterScope(); scope.AssignVariable(variable, value); ExpressionBase result; Assert.That(expr.ReplaceVariables(scope, out result), Is.True); Assert.That(result, Is.InstanceOf <ReturnExpression>()); Assert.That(((ReturnExpression)result).Value, Is.EqualTo(value)); }
public override void VisitReturnExpression(ReturnExpression node) { this.VisitReturnExpression(node); if (node.get_Value() == null) { return; } if (node.get_Value().get_CodeNodeType() == 21) { V_0 = (DelegateCreationExpression)node.get_Value(); if (this.CanInferTypeOfDelegateCreation(this.context.get_MethodContext().get_Method().get_ReturnType())) { V_0.set_TypeIsImplicitlyInferable(true); } } return; }
public override void VisitReturnExpression(ReturnExpression node) { base.VisitReturnExpression(node); if (node.Value == null) { return; } if (node.Value.CodeNodeType == CodeNodeType.DelegateCreationExpression) { DelegateCreationExpression theDelegate = (DelegateCreationExpression)node.Value; TypeReference returnType = context.MethodContext.Method.ReturnType; if (CanInferTypeOfDelegateCreation(returnType)) { theDelegate.TypeIsImplicitlyInferable = true; } } }
public override ICodeNode VisitMethodReferenceExpression(MethodReferenceExpression node) { MethodDefinition methodDefinition = node.Method.Resolve(); TypeDefinition currentType = context.TypeContext.CurrentType; if (methodDefinition == null || methodDefinition.DeclaringType != currentType && !methodDefinition.DeclaringType.IsNestedIn(currentType)) { return(base.VisitMethodReferenceExpression(node)); } if ((!methodDefinition.IsGetter) && (!methodDefinition.IsSetter) && (methodDefinition.IsCompilerGenerated() || CheckTypeForCompilerGeneratedAttribute(methodDefinition.DeclaringType))) { BlockStatement statement = null; if (methodDefinition.Body != null) { DecompilationContext innerContext = CreateDecompilationContext(methodDefinition); statement = methodDefinition.Body.DecompileLambda(Language, innerContext); if ((statement.Statements.Count == 1) && (statement.Statements[0].CodeNodeType == CodeNodeType.ExpressionStatement) && ((statement.Statements[0] as ExpressionStatement).Expression.CodeNodeType == CodeNodeType.ReturnExpression)) { ReturnExpression returnExpression = (statement.Statements[0] as ExpressionStatement).Expression as ReturnExpression; ShortFormReturnExpression shortFormReturnExpression = new ShortFormReturnExpression(returnExpression.Value, returnExpression.MappedInstructions); statement = new BlockStatement(); statement.Statements.Add(new ExpressionStatement(shortFormReturnExpression)); } this.context.MethodContext.VariableDefinitionToNameMap.AddRange(innerContext.MethodContext.VariableDefinitionToNameMap); this.context.MethodContext.VariableNamesCollection.UnionWith(innerContext.MethodContext.VariableNamesCollection); this.context.MethodContext.AddInnerMethodParametersToContext(innerContext.MethodContext); this.context.MethodContext.GotoStatements.AddRange(innerContext.MethodContext.GotoStatements); this.context.MethodContext.GotoLabels.AddRange(innerContext.MethodContext.GotoLabels); } ExpressionCollection expressionCollection = new ExpressionCollection(); bool hasAnonymousParameter = LambdaExpressionsHelper.HasAnonymousParameter(methodDefinition.Parameters); foreach (ParameterDefinition parameter in methodDefinition.Parameters) { expressionCollection.Add(new LambdaParameterExpression(parameter, !hasAnonymousParameter, null)); } return(new LambdaExpression(expressionCollection, statement, methodDefinition.IsAsync(), methodDefinition.IsFunction(), node.Method.Parameters, false, node.MappedInstructions)); } return(base.VisitMethodReferenceExpression(node)); }
public override void VisitReturnExpression(ReturnExpression node) { this.VisitReturnExpression(node); if (this.decompiledMethodReturnType != null && node.get_Value() != null && node.get_Value().get_HasType() && this.ShouldAddCastToAssignment(this.decompiledMethodReturnType, node.get_Value().get_ExpressionType())) { V_0 = node.get_Value(); if (node.get_Value() as ExplicitCastExpression != null) { V_1 = node.get_Value() as ExplicitCastExpression; if (this.IsIntegerType(V_1.get_TargetType())) { V_0 = V_1.get_Expression(); } } node.set_Value(new ExplicitCastExpression(V_0, this.decompiledMethodReturnType, null)); } return; }
public override void VisitReturnExpression(ReturnExpression node) { base.VisitReturnExpression(node); if (decompiledMethodReturnType != null && node.Value != null && node.Value.HasType && ShouldAddCastToAssignment(decompiledMethodReturnType, node.Value.ExpressionType)) { Expression toCast = node.Value; if (node.Value is ExplicitCastExpression) { ExplicitCastExpression valueCast = node.Value as ExplicitCastExpression; if (IsIntegerType(valueCast.TargetType)) { toCast = valueCast.Expression; } } node.Value = new ExplicitCastExpression(toCast, decompiledMethodReturnType, null); } }
public void Visit(ReturnExpression returnExpr) { if (returnExpr.Value is ILiteralExpression) { var literal = returnExpr.Value as ILiteralExpression; var type = TypeResolver.ToSailType(literal); _returnValues.Push(new SailObject(null, literal.GetValue(), type)); } else if (returnExpr.Value is IdentifierExpression) { string varName = (returnExpr.Value as IdentifierExpression).Value; var variable = _variables.FirstOrDefault(va => va.Name == varName); if (variable == null) { ErrorManager.CreateError("Can't return non-existant variable!", ErrorType.Error, returnExpr.Line, returnExpr.Column); return; } _returnValues.Push(new SailObject(null, variable.Value, variable.Type)); } }
public override ICodeNode VisitReturnExpression(ReturnExpression node) { node = (ReturnExpression)base.VisitReturnExpression(node); TypeDefinition methodReturnType = context.MethodContext.Method.ReturnType.Resolve(); if (methodReturnType == null) { return(node); } if (node.Value == null) { // Covers methods returning void. return(node); } if (methodReturnType.IsEnum && !context.MethodContext.Method.ReturnType.IsArray) { if (node.Value.ExpressionType == null || node.Value.ExpressionType.FullName != methodReturnType.FullName) { if (node.Value is LiteralExpression) { node.Value = EnumHelper.GetEnumExpression(methodReturnType, node.Value as LiteralExpression, typeSystem); } else if (node.Value is CastExpression && (node.Value as CastExpression).Expression is LiteralExpression) { node.Value = EnumHelper.GetEnumExpression(methodReturnType, (node.Value as CastExpression).Expression as LiteralExpression, typeSystem); } else if (node.Value.HasType && NeedsCast(node.Value.ExpressionType, methodReturnType)) { node.Value = new CastExpression(node.Value, methodReturnType, null); } } } return(node); }