public override IExpression Visit(CreateArray createArray) { createArray.ElementType = this.Visit(createArray.ElementType); createArray.Initializers = this.Visit(createArray.Initializers); createArray.Sizes = this.Visit(createArray.Sizes); createArray.Type = this.Visit(createArray.Type); return(createArray); }
public CreateArrayAction(ParseInfo parseInfo, Scope scope, CreateArray createArrayContext) { Values = new IExpression[createArrayContext.Values.Count]; for (int i = 0; i < Values.Length; i++) { Values[i] = parseInfo.GetExpression(scope, createArrayContext.Values[i]); } }
// TODO: Handle multi-dimensional arrays public override IExpression VisitArrayCreationExpression(ArrayCreationExpressionSyntax node) { var o = this.semanticModel.GetSymbolInfo(node); var s = o.Symbol; var arrayType = node.Type; var elementType = this.mapper.Map(this.semanticModel.GetTypeInfo(arrayType.ElementType).Type); var arrayOfType = Microsoft.Cci.Immutable.Vector.GetVector(elementType, this.host.InternFactory); List <IExpression> inits = null; int size = 0; if (node.Initializer != null) { inits = new List <IExpression>(); foreach (var i in node.Initializer.Expressions) { var e = this.Visit(i); inits.Add(e); size++; } } var result = new CreateArray() { ElementType = elementType, Rank = 1, Type = arrayOfType, }; if (inits != null) { result.Initializers = inits; result.Sizes = new List <IExpression> { new CompileTimeConstant() { Value = size, }, }; } else { var rankSpecs = arrayType.RankSpecifiers; foreach (var r in rankSpecs) { foreach (var rs in r.Sizes) { var e = this.Visit(rs); result.Sizes = new List <IExpression> { e, }; break; } break; } } return(result); }
static void CompareByOrder(int size, CreateArray createArray) { int[] arr = createArray(size); int[] arr2 = new int[size]; Array.Copy(arr, arr2, size); Console.WriteLine("Сортировка простыми вставками... "); InsertionSort(arr); Console.WriteLine("Поразрядная сортировка... "); RadixSort(arr2); Console.WriteLine(); }
static void Main(string[] args) { CreateArray ca = Methods.CreateArray; WriteArray wa = Methods.WriteArray; int[] a = ca(52416); int[] arr = { 2, 4, 2, 6, 7, 2, 3, 43, 2, 6 }; wa(arr); Console.WriteLine(ca.Method + " " + ca.Target); Console.WriteLine(wa.Method + " " + wa.Target); }
private static void AddArrayInitializers(CreateArray createArray, IFieldDefinition initialValueField, ulong[] sizes) { ITypeReference elemType = createArray.ElementType; MemoryStream memoryStream = new MemoryStream(new List <byte>(initialValueField.FieldMapping.Data).ToArray()); BinaryReader reader = new BinaryReader(memoryStream, Encoding.Unicode); ulong flatSize = 1; foreach (ulong dimensionSize in sizes) { flatSize *= dimensionSize; } while (flatSize-- > 0) { CompileTimeConstant cc = new CompileTimeConstant(); cc.Value = ReadValue(elemType.TypeCode, reader); cc.Type = elemType; createArray.Initializers.Add(cc); } }
private void ReplaceLocalArrayInitializerPattern(List <IStatement> statements, int i) { if (i > statements.Count - 4) { return; } PushStatement /*?*/ push = statements[i] as PushStatement; if (push == null) { return; } var pushDup = statements[i + 1] as PushStatement; if (pushDup == null || !(pushDup.ValueToPush is Dup)) { return; } CreateArray /*?*/ createArray = push.ValueToPush as CreateArray; if (createArray == null) { return; } ExpressionStatement /*?*/ expressionStatement = statements[i + 2] as ExpressionStatement; if (expressionStatement == null) { return; } MethodCall /*?*/ methodCall = expressionStatement.Expression as MethodCall; if (methodCall == null || !methodCall.IsStaticCall || methodCall.IsJumpCall || methodCall.Arguments.Count != 2) { return; } var pop = methodCall.Arguments[0] as Pop; if (pop == null) { return; } TokenOf /*?*/ tokenOf = methodCall.Arguments[1] as TokenOf; if (tokenOf == null) { return; } IFieldDefinition /*?*/ initialValueField = tokenOf.Definition as IFieldDefinition; if (initialValueField == null || !initialValueField.IsMapped) { return; } if (methodCall.MethodToCall.Name.UniqueKey != this.InitializeArray.UniqueKey) { return; } List <ulong> sizes = new List <ulong>(); foreach (IExpression expr in createArray.Sizes) { IMetadataConstant mdc = expr as IMetadataConstant; if (mdc == null) { return; } sizes.Add(ConvertToUlong(mdc)); } AddArrayInitializers(createArray, initialValueField, sizes.ToArray()); expressionStatement = statements[i + 3] as ExpressionStatement; if (expressionStatement != null) { Assignment /*?*/ assignment = expressionStatement.Expression as Assignment; if (assignment != null) { var pop2 = assignment.Source as Pop; if (pop2 != null) { assignment.Source = createArray; statements[i] = expressionStatement; statements.RemoveRange(i + 1, 3); return; } } } push.ValueToPush = createArray; statements.RemoveRange(i + 1, 2); }
private List<Expression> ParseArrayInitializers(uint rank, TypeExpression elementType, TokenSet followers, bool doNotSkipClosingBrace, SourceLocationBuilder ctx) //^ requires this.currentToken == Token.LeftBrace; //^ ensures followers[this.currentToken] || this.currentToken == Token.EndOfFile; { this.GetNextToken(); List<Expression> initialValues = new List<Expression>(); if (this.currentToken == Token.RightBrace) { ctx.UpdateToSpan(this.scanner.SourceLocationOfLastScannedToken); this.GetNextToken(); initialValues.TrimExcess(); return initialValues; } while (true) { if (rank > 1) { List<Expression> elemArrayInitializers; SourceLocationBuilder ectx = new SourceLocationBuilder(this.scanner.SourceLocationOfLastScannedToken); if (this.currentToken == Token.LeftBrace) { elemArrayInitializers = this.ParseArrayInitializers(rank-1, elementType, followers|Token.Comma|Token.LeftBrace, false, ectx); } else { elemArrayInitializers = new List<Expression>(0); this.SkipTo(followers|Token.Comma|Token.LeftBrace, Error.ExpectedLeftBrace); } CreateArray elemArr = new CreateArray(elementType, elemArrayInitializers.AsReadOnly(), new List<Expression>(0).AsReadOnly(), rank-1, new List<Expression>(0).AsReadOnly(), ectx); initialValues.Add(elemArr); } else { if (this.currentToken == Token.LeftBrace) { this.HandleError(Error.ArrayInitInBadPlace); SourceLocationBuilder ectx = new SourceLocationBuilder(this.scanner.SourceLocationOfLastScannedToken); //^ assume this.currentToken == Token.LeftBrace; List<Expression> elemArrayInitializers = this.ParseArrayInitializers(1, elementType, followers|Token.Comma|Token.LeftBrace, false, ectx); CreateArray elemArr = new CreateArray(elementType, elemArrayInitializers.AsReadOnly(), new List<Expression>(0).AsReadOnly(), 1, new List<Expression>(0).AsReadOnly(), ectx); initialValues.Add(elemArr); } else initialValues.Add(this.ParseExpression(followers|Token.Comma|Token.RightBrace)); } if (this.currentToken != Token.Comma) break; this.GetNextToken(); if (this.currentToken == Token.RightBrace) break; } ctx.UpdateToSpan(this.scanner.SourceLocationOfLastScannedToken); if (!doNotSkipClosingBrace) { this.Skip(Token.RightBrace); this.SkipTo(followers); } initialValues.TrimExcess(); return initialValues; }
private Expression ParseArrayInitializer(ArrayTypeExpression arrayTypeExpression, TokenSet followers) //^ requires this.currentToken == Token.LeftBrace; //^ ensures followers[this.currentToken] || this.currentToken == Token.EndOfFile; { uint rank = arrayTypeExpression.Rank; SourceLocationBuilder slb = new SourceLocationBuilder(this.scanner.SourceLocationOfLastScannedToken); List<Expression> initializers = this.ParseArrayInitializers(rank, arrayTypeExpression.ElementType, followers, false, slb); //^ assert followers[this.currentToken] || this.currentToken == Token.EndOfFile; List<Expression> lowerBounds = new List<Expression>(0); List<Expression> sizes = new List<Expression>(0); Expression result = new CreateArray(arrayTypeExpression.ElementType, initializers, lowerBounds, rank, sizes, slb); //^ assume followers[this.currentToken] || this.currentToken == Token.EndOfFile; return result; }
protected override IExpression ProjectAsFiniteSet() { Expression typePtrRef = NamespaceHelper.CreateInSystemDiagnosticsContractsCodeContractExpr(this.Compilation.NameTable, "TypedPtr"); typePtrRef.SetContainingExpression(this); var arr = new CreateArray(new VccNamedTypeExpression(typePtrRef).Resolve(0), this.Expressions, this.SourceLocation); arr.SetContainingExpression(this); return arr.ProjectAsIExpression(); }