public static ExpressionSyntax TryRewrite(ValueBridge collection, TypeSyntax returnType, List <LinqStep> chain, InvocationExpressionSyntax node) { using var design = RewriteParametersFactory.BorrowParameters(); design.SetData(collection, returnType, chain, node, false); string[] names = chain.Select(x => x.MethodName).ToArray(); var(simplePreCheck, simpleResult) = TryRewriteSimple(design, names); if (simplePreCheck && simpleResult != null) { return(simpleResult); } if (simplePreCheck) { design.SetData(collection, returnType, chain, node, true); } design.HasResultMethod = MethodsWithResult.Contains(names.Last()); RewriteComposite(design, names); var body = design.Error ? new [] { design.InitialStatements[0] } : GetMethodBody(design); if (design.NotRewrite) { throw new NotSupportedException("Not good for rewrite"); } if (design.Data.CurrentMethodIsConditional && design.ReturnType.Type.ToString() != "void") { return(ConditionalExpression(design.CurrentCollection.IsEqual(null), Default(design.ReturnType), design.Rewrite.GetMethodInvocationExpression(design, body))); } return(design.Rewrite.GetMethodInvocationExpression(design, body)); }
private static (bool preCheck, ExpressionSyntax result) TryRewriteSimple(RewriteDesign design, string[] names) { if (design.CurrentCollection?.CollectionType == CollectionType.IEnumerable) { return(false, null); } if (names.Any(x => MethodsModifyingEnumeration.Contains(x))) { return(false, null); } if (design.Data.CurrentMethodParams.Any(x => x.Modifiers.Any())) { return(false, null); } if (!MethodsCreateArray.Contains(names.First())) { RewriteCollectionEnumeration.Rewrite(design, Array.Empty <RewrittenValueBridge>(), false); } for (var i = 0; i < names.Length; i++) { ExpressionSyntax rewrittenPart = RewriteSimplePart(names[i], design, i); if (design.Error) { return(true, null); } if (!design.SimpleEnumeration) { return(true, null); } if (rewrittenPart != null) { return(true, rewrittenPart); } } if (!MethodsWithResult.Contains(names.Last())) { ExpressionSyntax rewrittenPart = RewriteToArray.SimpleRewrite(design, Array.Empty <RewrittenValueBridge>()); if (!design.SimpleEnumeration) { return(true, null); } if (rewrittenPart != null) { return(true, rewrittenPart); } } return(true, null); }