public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { var elementType = design.ReturnType.Type is NullableTypeSyntax nullable ? (TypeBridge)nullable.ElementType : design.ReturnType; var sumVariable = CreateGlobalVariable(design, elementType, 0); var value = args.Length switch { 0 => design.LastValue, 1 => args[0].Inline(design, design.LastValue) }; if (design.ReturnType.Type is NullableTypeSyntax) { value = value.Reusable(design); design.ForAdd(If(value.NotEqual(null), sumVariable.AddAssign(value.Cast(elementType)))); } else { design.ForAdd(sumVariable.AddAssign(value)); } design.ResultAdd(Return(sumVariable)); } }
private static void UnknownSourceSize(RewriteDesign design) { design.Indexer = null; var reverseIndexerVariable = CreateGlobalVariable(design, Int, 8); var currentLengthVariable = CreateGlobalVariable(design, Int, 8); var resultVariable = CreateGlobalVariable(design, design.LastValue.ArrayType(), CreateArray(design.LastValue.ArrayType(), 8)); var sizeVariable = CreateGlobalVariable(design, Int); design.ForAdd(reverseIndexerVariable.PreDecrement()); design.ForAdd(If(reverseIndexerVariable < 0, Block( sizeVariable.Assign(currentLengthVariable), "LinqRewrite".Access("Core", "EnlargeExtensions", "LogEnlargeReverseArray") .Invoke(2, RefArg(resultVariable), RefArg(currentLengthVariable)), reverseIndexerVariable.Assign(currentLengthVariable - sizeVariable - 1)))); design.ForAdd(resultVariable[reverseIndexerVariable].Assign(design.LastValue)); design.Indexer = null; design.Iterators.All.ForEach(x => x.Complete = true); design.AddIterator(new CollectionValueBridge(design, resultVariable.Type, design.LastValue.Type, resultVariable, true)); design.CurrentCollection = design.CurrentIterator.Collection; RewriteCollectionEnumeration.RewriteOther(design, design.CurrentCollection); design.ResultSize = design.SourceSize = currentLengthVariable - reverseIndexerVariable; design.CurrentIterator.ForFrom = reverseIndexerVariable; design.CurrentIterator.ForTo = currentLengthVariable - 1; design.Indexer = null; }
private static VariableBridge KnownSourceSize(RewriteDesign design, ValueBridge currentLength, VariableBridge resultVariable, int enlarging) { var logVariable = CreateGlobalVariable(design, Int, "LinqRewrite".Access("Core", "IntExtensions", "Log2") .Invoke(Parenthesize(design.SourceSize).Cast(SyntaxKind.UIntKeyword)) - Constants.MinArraySizeLog); if (enlarging != 1) { design.PreUseAdd(logVariable.Assign( SyntaxFactory.ConditionalExpression(logVariable.GThan(enlarging), logVariable - logVariable % enlarging, IntValue(enlarging)))); } else { design.PreUseAdd(logVariable.Assign( SyntaxFactory.ConditionalExpression(logVariable.GThan(1), logVariable, IntValue(1)))); } var currentLengthVariable = CreateGlobalVariable(design, Int, currentLength); design.ForAdd(If(design.Indexer >= currentLengthVariable, "LinqRewrite".Access("Core", "EnlargeExtensions", "LogEnlargeArray") .Invoke(enlarging, design.SourceSize, RefArg(resultVariable), RefArg(logVariable), OutArg(currentLengthVariable)))); design.ForAdd(resultVariable[design.Indexer].Assign(design.LastValue)); return(resultVariable); }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args, InvocationExpressionSyntax invocation) { var access = (MemberAccessExpressionSyntax)invocation.Expression; var name = (GenericNameSyntax)access.Name; var type = name.TypeArgumentList.Arguments[0]; var typeSymbol = design.Semantic.GetTypeInfo(type).Type; if (SymbolExtensions.IsSameType(typeSymbol, design.LastValue.Type)) { } else if (design.Unchecked || SymbolExtensions.IsDescendantType(typeSymbol, design.LastValue.Type)) { design.LastValue = design.LastValue.Reusable(design); design.ForAdd(If(Not(design.LastValue.Is(type)), Continue())); design.LastValue = new TypedValueBridge(type, design.LastValue.Cast(type)); } else { design.LastValue = design.LastValue.Reusable(design); design.ForAdd(If(Not(design.LastValue.Is(type)), Continue())); design.LastValue = new TypedValueBridge(type, design.LastValue.Cast(ParseTypeName("object")).Cast(type)); } design.ResultSize = null; design.ListEnumeration = false; design.ModifiedEnumeration = true; }
private static VariableBridge UnknownSourceSize(RewriteDesign design, ValueBridge currentLength, VariableBridge resultVariable, int enlarging) { var currentLengthVariable = CreateGlobalVariable(design, Int, currentLength); design.ForAdd(If(design.Indexer >= currentLengthVariable, "LinqRewrite".Access("Core", "EnlargeExtensions", "LogEnlargeArray") .Invoke(enlarging, RefArg(resultVariable), RefArg(currentLengthVariable)))); design.ForAdd(resultVariable[design.Indexer].Assign(design.LastValue)); return(resultVariable); }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { if (args.Length == 0) { design.ForAdd(Return(design.LastValue)); } else { design.ForAdd(If(args[0].Inline(design, design.LastValue), Return(design.LastValue))); } design.ResultAdd(Return(Default(design.ReturnType))); }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { design.ForAdd(If(Not(args[0].Inline(design, design.LastValue)), Return(false))); design.ResultAdd(Return(true)); }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { if (design.Iterators.Count > 1) { design.ListEnumeration = false; } var takeValue = args[0]; CheckBounds(design, ref takeValue); LocalVariable takeIndexer = CreateGlobalVariable(design, Int, 0); if (!design.ModifiedEnumeration) { design.CurrentIterator.ForTo = design.CurrentIterator.ForFrom + design.CurrentIterator.ForInc * takeValue - design.CurrentIterator.ForInc; design.CurrentIterator.ForTo = design.CurrentIterator.ForTo; } else { design.ForAdd(If(takeIndexer.PostIncrement() >= takeValue, Break())); } if (design.ResultSize != null) { design.ResultSize = takeValue; } else { design.SourceSize = takeValue; } design.Indexer = null; }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { var listVariable = CreateGlobalVariable(design, design.ReturnType, New(design.ReturnType)); design.ForAdd(listVariable.Access("Add").Invoke(design.LastValue)); design.ResultAdd(Return(listVariable)); }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { var collectionValue = args[0]; if (!AssertNotNull(design, collectionValue)) { return; } var methodValue = args[1]; design.WrapWithTry = true; var itemType = collectionValue.ItemType(design); var enumeratorVariable = CreateGlobalVariable(design, ParseTypeName($"System.Collections.Generic.IEnumerator<{itemType}>")); design.InitialAdd(enumeratorVariable.Assign(Parenthesize(collectionValue.Cast(ParseTypeName($"IEnumerable<{itemType}>"))) .Access("GetEnumerator").Invoke())); design.ForAdd(If(Not(enumeratorVariable.Access("MoveNext").Invoke()), Break())); design.LastValue = methodValue.Inline(design, design.LastValue, new TypedValueBridge(collectionValue.ItemType(design), enumeratorVariable.Access("Current"))); design.FinalAdd(enumeratorVariable.Access("Dispose").Invoke()); design.ListEnumeration = false; design.ModifiedEnumeration = true; }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { var innerValue = args[0]; RewrittenValueBridge outerKeySelector = args[1]; RewrittenValueBridge innerKeySelector = args[2]; if (design.CurrentIterator.IgnoreIterator) { return; } var resultSelectorValue = args[3]; var comparerValue = args.Length == 5 ? args[4] : null; var lookupType = ParseTypeName($"LinqRewrite.Core.SimpleLookup<{innerValue.ItemType(design)},{innerKeySelector.ReturnType(design)}>"); var lookupVariable = CreateGlobalVariable(design, lookupType, lookupType.Access("CreateForJoin") .Invoke(innerValue, innerKeySelector, comparerValue)); var itemValue = design.LastValue; var groupingType = ParseTypeName($"LinqRewrite.Core.SimpleLookup<{innerValue.ItemType(design)},{outerKeySelector.ReturnType(design)}>.Grouping"); var groupingVariable = CreateGlobalVariable(design, groupingType); design.ForAdd(groupingVariable.Assign(lookupVariable.Access("GetGrouping") .Invoke(outerKeySelector.Inline(design, itemValue), false))); design.ForAdd(If(groupingVariable.IsEqual(null), Continue())); var rewritten = new RewrittenValueBridge(((LambdaExpressionSyntax)innerKeySelector.Old).ExpressionBody, groupingVariable); var iteratorVariable = CreateGlobalVariable(design, innerKeySelector.ReturnType(design)); design.IncompleteIterators.ToArray().ForEach(x => { var newIterator = new IteratorDesign(design, new CollectionValueBridge(design, groupingType, innerKeySelector.ReturnType(design), rewritten, true)); x.ForBody.Add(newIterator); design.Iterators.Add(newIterator); design.Iterators.Remove(x); design.CurrentIterator = newIterator; RewriteCollectionEnumeration.RewriteOther(design, design.CurrentIterator.Collection, iteratorVariable); }); design.CurrentIterator = design.Iterators.Last(); design.LastValue = resultSelectorValue.Inline(design, itemValue, design.LastValue); design.ModifiedEnumeration = true; design.ListEnumeration = false; design.SourceSize = null; }
private static void CalculateNullableAverage(RewriteDesign design, TypeBridge elementType, TypedValueBridge selectionValue, LocalVariable sumVariable) { var inlinedValue = selectionValue.Reusable(design); design.ForAdd(If(inlinedValue.IsEqual(null), Continue())); design.ForAdd(sumVariable.AddAssign(inlinedValue.Cast(elementType))); design.Indexer = null; if (design.Unchecked) { design.ResultAdd(sumVariable.Cast(design.ReturnType.Type) / design.Indexer); } else { design.ResultAdd(Return(SyntaxFactory.ConditionalExpression(design.Indexer.IsEqual(0), Null, sumVariable.Cast(design.ReturnType.Type) / design.Indexer))); } }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { var positionValue = args[0].ReusableConst(design); design.ForAdd(If(design.Indexer.IsEqual(positionValue), Return(design.LastValue))); design.ResultAdd(Return(Default(design.ReturnType))); }
private static VariableBridge KnownSize(RewriteDesign design, VariableBridge resultVariable) { if (TryGetInt(design.ResultSize, out var resultInt) && resultInt < 0) { InitialErrorAdd(design, Return("System".Access("Array", $"Empty<{design.LastValue.Type}>").Invoke())); } design.ForAdd(resultVariable[design.Indexer].Assign(design.LastValue)); return(resultVariable); }
private static void CalculateSimpleAverage(RewriteDesign design, TypedValueBridge selectionValue, LocalVariable sumVariable) { if (!AssertResultSizeGreaterEqual(design, 1)) { return; } design.ForAdd(sumVariable.AddAssign(selectionValue)); design.ResultAdd(Return(sumVariable / design.GetResultSize())); }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { var foundVariable = CreateGlobalVariable(design, NullableType(design.ReturnType), null); if (args.Length == 0) { design.ForAdd(foundVariable.Assign(design.LastValue)); } else { design.ForAdd(If(args[0].Inline(design, design.LastValue), foundVariable.Assign(design.LastValue))); } design.ResultAdd(If(foundVariable.IsEqual(null), Return(Default(design.ReturnType)), Return(foundVariable.Cast(design.ReturnType)))); }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { if (args.Length != 0) { design.Indexer = null; design.ForAdd(If(Not(args[0].Inline(design, design.LastValue)), Continue())); } design.ResultAdd(Return(design.GetIndexer(Long))); }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { var foundVariable = CreateGlobalVariable(design, NullableType(design.ReturnType), null); if (args.Length == 0) { design.ForAdd(If(foundVariable.IsEqual(null), foundVariable.Assign(design.LastValue), Throw("System.InvalidOperationException", "The sequence contains more than single matching element."))); } else { design.ForAdd(If(args[0].Inline(design, design.LastValue), If(foundVariable.IsEqual(null), foundVariable.Assign(design.LastValue), Throw("System.InvalidOperationException", "The sequence contains more than single matching element.")))); } design.ResultAdd(If(foundVariable.IsEqual(null), Throw("System.InvalidOperationException", "The sequence did not contain any elements."), Return(foundVariable.Cast(design.ReturnType)))); }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { if (!AssertResultSizeGreaterEqual(design, 1)) { return; } var elementType = design.ReturnType.Type is NullableTypeSyntax nullable ? (TypeBridge)nullable.ElementType : design.ReturnType; var minVariable = elementType.ToString() switch { "int" => CreateGlobalVariable(design, Int, int.MaxValue), "long" => CreateGlobalVariable(design, Long, long.MaxValue), "float" => CreateGlobalVariable(design, Float, float.MaxValue), "double" => CreateGlobalVariable(design, Double, double.MaxValue), "decimal" => CreateGlobalVariable(design, Decimal, decimal.MaxValue), _ => null }; var value = args.Length switch { 0 => design.LastValue.Reusable(design), 1 => args[0].Inline(design, design.LastValue).Reusable(design) }; if (design.ReturnType.Type is NullableTypeSyntax) { design.ForAdd(If(value.IsEqual(null).Or(value >= minVariable), Continue())); design.ForAdd(minVariable.Assign(value.Cast(elementType))); } else { design.ForAdd(If(value >= minVariable, Continue())); design.ForAdd(minVariable.Assign(value)); } design.ResultAdd(Return(minVariable)); } } }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { var sourceSizeValue = design.SourceSize; var resultSizeValue = design.ResultSize; var collectionValue = args[0]; if (!AssertNotNull(design, collectionValue)) { return; } LocalVariable itemVariable; var lastVariable = TryGetVariable(design, design.LastValue); if (lastVariable != null) { itemVariable = lastVariable; } else { itemVariable = CreateGlobalVariable(design, design.LastValue.Type); design.ForAdd(itemVariable.Assign(design.LastValue)); design.LastValue = new TypedValueBridge(design.LastValue.Type, itemVariable); } itemVariable.IsGlobal = true; var collectionType = design.Data.GetTypeInfo(collectionValue).Type; design.AddIterator(new CollectionValueBridge(design, collectionType, collectionValue, true)); RewriteCollectionEnumeration.RewriteOther(design, design.CurrentIterator.Collection, itemVariable, true); design.ListEnumeration = design.IncompleteIterators.All(x => x.ListEnumeration); if (sourceSizeValue != null && design.SourceSize != null) { design.SourceSize += sourceSizeValue; } else { design.SourceSize = null; } if (resultSizeValue != null && design.ResultSize != null) { design.ResultSize += resultSizeValue; } else { design.ResultSize = null; } (design.ModifiedEnumeration, design.ResultSize) = (true, design.ResultSize); }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { if (!AssertResultSizeGreaterEqual(design, 0, true)) { return; } if (args.Length == 0) { design.ForAdd(Return(design.LastValue)); } else { design.ForAdd(If(args[0].Inline(design, design.LastValue), Return(design.LastValue))); } if (!design.Unchecked) { design.ResultAdd(Throw("System.InvalidOperationException", "The sequence did not contain any elements.")); } }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { var elementSyntax = design.Node.ArgumentList.Arguments.First().Expression; var elementEqualityValue = args.Length switch { 1 => design.LastValue.IsEqual(elementSyntax), 2 => args[1].ReusableConst(design).Access("Equals").Invoke(design.LastValue.Value, elementSyntax) }; design.ForAdd(If(elementEqualityValue, Return(true))); design.ResultAdd(Return(false)); } }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { var sourceSizeValue = design.SourceSize; var collectionValue = args[0]; if (!AssertNotNull(design, collectionValue)) { return; } var oldLastValue = design.LastValue; var collectionType = design.Data.GetTypeInfo(collectionValue).Type; var oldIterator = design.InsertIterator(new CollectionValueBridge(design, collectionType, collectionValue, true)); RewriteCollectionEnumeration.RewriteOther(design, design.CurrentIterator.Collection); var hashsetType = ParseTypeName($"LinqRewrite.Core.SimpleSet<{design.LastValue.Type}>"); var hashsetCreation = args.Length switch { 1 => New(hashsetType), 2 => New(hashsetType, args[1]) }; var hashsetVariable = CreateGlobalVariable(design, hashsetType, hashsetCreation); design.CurrentForAdd(hashsetVariable.Access("Add").Invoke(design.LastValue)); design.CurrentIterator.Complete = true; design.CurrentIterator = oldIterator; design.LastValue = oldLastValue; design.LastValue = design.LastValue.Reusable(design); design.ForAdd(If(Not(hashsetVariable.Access("Remove").Invoke(design.LastValue)), Continue())); if (sourceSizeValue != null && design.SourceSize != null) { design.SourceSize += sourceSizeValue; } else { design.SourceSize = null; } design.ListEnumeration = false; design.ModifiedEnumeration = true; } }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { if (!AssertGreaterEqual(design, args[0], 0, true, true)) { return; } if (!AssertLesser(design, args[0], design.ResultSize, true, true)) { return; } var positionValue = args[0].ReusableConst(design); design.ForAdd(If(design.Indexer.IsEqual(positionValue), Return(design.LastValue))); if (!design.Unchecked) { design.ResultAdd(Throw("System.InvalidOperationException", "The sequence did not contain enough elements.")); } }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { var collectionValue = args[0]; if (!AssertNotNull(design, collectionValue)) { return; } if (ExpressionSimplifier.TryGetInt(design.ResultSize, out var resultSizeInt)) { if (resultSizeInt == 0) { InitialErrorAdd(design, Return((collectionValue.Access("Count").Invoke()).IsEqual(0))); return; } } design.WrapWithTry = true; var itemType = collectionValue.ItemType(design); var enumeratorVariable = CreateGlobalVariable(design, ParseTypeName($"System.Collections.Generic.IEnumerator<{itemType}>")); design.InitialAdd(enumeratorVariable.Assign(Parenthesize(collectionValue.Cast(ParseTypeName($"IEnumerable<{itemType}>"))) .Access("GetEnumerator").Invoke())); var equalityTestValue = args.Length switch { 1 => enumeratorVariable.Access("Current", "Equals").Invoke(design.LastValue), 2 => args[1].ReusableConst(design).Access("Equals").Invoke(design.LastValue, enumeratorVariable.Access("Current")) }; design.ForAdd(If(Not(enumeratorVariable.Access("MoveNext").Invoke().And(equalityTestValue)), Return(false))); design.ResultAdd(If(enumeratorVariable.Access("MoveNext").Invoke(), Return(false))); design.FinalAdd(enumeratorVariable.Access("Dispose").Invoke()); design.ResultAdd(Return(true)); } }
public static void Rewrite(RewriteDesign design, RewrittenValueBridge[] args) { if (design.Iterators.Count > 1) { design.ListEnumeration = false; } var skippedValue = args[0]; if (!CheckBounds(design, ref skippedValue)) { return; } if (!design.ModifiedEnumeration) { design.CurrentIterator.ForFrom += skippedValue * design.CurrentIterator.ForInc; design.CurrentIterator.ForFrom = design.CurrentIterator.ForFrom; } else { design.ForAdd(If(design.Indexer < skippedValue, Block( design.Indexer.PostIncrement(), Continue() ))); } if (design.ResultSize != null) { design.ResultSize -= skippedValue; } if (TryGetInt(design.ResultSize, out var resultInt) && resultInt < 0) { design.ResultSize = 0; } design.Indexer = null; }