public override void VisitArrayCreateExpression(ArrayCreateExpression node) { node.Initializer.Elements.ToList().ForEach ( item => item.AcceptVisitor(this) ); }
public virtual void VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression) { if (this.ThrowException) { throw (Exception)this.CreateException(arrayCreateExpression); } }
public override object VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, object data) { if (arrayCreateExpression.Arguments.Count >= 10) { UnlockWith(arrayCreateExpression); } return base.VisitArrayCreateExpression(arrayCreateExpression, data); }
public virtual object VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, object data) { Debug.Assert((arrayCreateExpression != null)); Debug.Assert((arrayCreateExpression.CreateType != null)); Debug.Assert((arrayCreateExpression.Arguments != null)); Debug.Assert((arrayCreateExpression.ArrayInitializer != null)); arrayCreateExpression.CreateType.AcceptVisitor(this, data); foreach (Expression o in arrayCreateExpression.Arguments) { Debug.Assert(o != null); o.AcceptVisitor(this, data); } return arrayCreateExpression.ArrayInitializer.AcceptVisitor(this, data); }
public override void VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression) { base.VisitArrayCreateExpression(arrayCreateExpression); if (arrayCreateExpression.Initializer.IsNull) { return; } var variableInilizer = arrayCreateExpression.GetParent <VariableInitializer>(); if (variableInilizer == null) { return; } if (variableInilizer.Parent is VariableDeclarationStatement) { var variableDeclaration = variableInilizer.Parent; if (variableDeclaration.GetChildByRole(Roles.Type) is ComposedType) { AddIssue(arrayCreateExpression, arrayCreateExpression.Initializer); } } else if (variableInilizer.Parent is FieldDeclaration) { var filedDeclaration = variableInilizer.Parent; if (filedDeclaration.GetChildByRole(Roles.Type) is ComposedType) { AddIssue(arrayCreateExpression, arrayCreateExpression.Initializer); } } }
public object VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, object data) { if (!arrayCreateExpression.ArrayInitializer.IsNull) { B.ArrayLiteralExpression ale = ConvertArrayLiteralExpression(arrayCreateExpression.ArrayInitializer); if (!arrayCreateExpression.IsImplicitlyTyped) { ale.Type = (B.ArrayTypeReference)ConvertTypeReference(arrayCreateExpression.CreateType); } return(ale); } string builtInName = (arrayCreateExpression.Arguments.Count > 1) ? "matrix" : "array"; B.MethodInvocationExpression mie = new B.MethodInvocationExpression(GetLexicalInfo(arrayCreateExpression), MakeReferenceExpression(builtInName)); TypeReference elementType = arrayCreateExpression.CreateType.Clone(); int[] newRank = new int[elementType.RankSpecifier.Length - 1]; for (int i = 0; i < newRank.Length; i++) { newRank[i] = elementType.RankSpecifier[i + 1]; } elementType.RankSpecifier = newRank; mie.Arguments.Add(MakeReferenceExpression(elementType)); if (arrayCreateExpression.Arguments.Count == 1) { mie.Arguments.Add(ConvertExpression(arrayCreateExpression.Arguments[0])); } else { B.ArrayLiteralExpression dims = new B.ArrayLiteralExpression(GetLexicalInfo(arrayCreateExpression)); ConvertExpressions(arrayCreateExpression.Arguments, dims.Items); mie.Arguments.Add(dims); } return(mie); }
/// <inheritdoc/> public virtual void VisitArrayCreateExpression(ArrayCreateExpression syntax) { VisitNode(syntax); }
private static void HandleCollectionName(Expression expression, QueryFromClause fromClause, QueryExpression queryExpression, ref string entityName) { // from d in docs.Users.SelectMany(x=>x.Roles) ... // from d in docs.Users.Where(x=>x.IsActive) ... var mie = expression as InvocationExpression; string methodToCall = null; if (mie != null) { expression = mie.Target; var target = expression as MemberReferenceExpression; if (target != null) { methodToCall = target.MemberName; expression = target.Target; } } var mre = expression as MemberReferenceExpression; if (mre == null) { return; } string oldIdentifier = fromClause.Identifier; if (mie != null) { fromClause.Identifier += "Item"; } entityName = mre.MemberName; fromClause.Expression = mre.Target; //doc["@metadata"]["Raven-Entity-Name"] var metadata = new IndexerExpression( new IndexerExpression(new IdentifierExpression(fromClause.Identifier), new List <Expression> { new StringLiteralExpression("@metadata") }), new List <Expression> { new StringLiteralExpression(Constants.RavenEntityName) } ); // string.Equals(doc["@metadata"]["Raven-Entity-Name"], "Blogs", StringComparison.OrdinalIgnoreCase) var binaryOperatorExpression = new InvocationExpression( new MemberReferenceExpression(new TypeReferenceExpression(new PrimitiveType("string")), "Equals"), new List <Expression> { metadata, new StringLiteralExpression(mre.MemberName), new MemberReferenceExpression(new TypeReferenceExpression(new SimpleType(typeof(StringComparison).FullName)), "InvariantCultureIgnoreCase") }); var queryWhereClause = new QueryWhereClause { Condition = binaryOperatorExpression }; ((QueryExpression)fromClause.Parent).Clauses.InsertAfter(fromClause, queryWhereClause); if (mie != null) { var newSource = new ArrayCreateExpression { Initializer = new ArrayInitializerExpression(new IdentifierExpression(fromClause.Identifier)), AdditionalArraySpecifiers = { new ArraySpecifier(1) } }; queryExpression.Clauses.InsertAfter(queryWhereClause, new QueryFromClause { Identifier = oldIdentifier, Expression = new InvocationExpression(new MemberReferenceExpression(newSource, methodToCall), mie.Arguments.Select(x => x.Clone())) }); } }
public override object VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, object data) { for (int i = 0; i < arrayCreateExpression.Arguments.Count; i++) { arrayCreateExpression.Arguments[i] = Expression.AddInteger(arrayCreateExpression.Arguments[i], 1); } if (arrayCreateExpression.ArrayInitializer.CreateExpressions.Count == 0) { arrayCreateExpression.ArrayInitializer = null; } return base.VisitArrayCreateExpression(arrayCreateExpression, data); }
Expression ConvertNewArrayBounds(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) return NotSupported(invocation); AstType elementType = ConvertTypeReference(invocation.Arguments.ElementAt(0)); IList<Expression> arguments = ConvertExpressionsArray(invocation.Arguments.ElementAt(1)); if (elementType != null && arguments != null) { if (ContainsAnonymousType(elementType)) { elementType = null; } ArrayCreateExpression ace = new ArrayCreateExpression(); ace.Type = elementType; ace.Arguments.AddRange(arguments); return ace; } return null; }
public override void VisitArrayCreateExpression(ArrayCreateExpression arrayObjectCreateExpression) { FormatCommas(arrayObjectCreateExpression, policy.SpaceBeforeMethodCallParameterComma, policy.SpaceAfterMethodCallParameterComma); base.VisitArrayCreateExpression(arrayObjectCreateExpression); }
public override object VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, object data) { for (int i = 0; i < arrayCreateExpression.Arguments.Count; i++) { arrayCreateExpression.Arguments[i] = Expression.AddInteger(arrayCreateExpression.Arguments[i], -1); } return base.VisitArrayCreateExpression(arrayCreateExpression, data); }
AstNode TransformByteCode(ILExpression byteCode) { object operand = byteCode.Operand; AstType operandAsTypeRef = AstBuilder.ConvertType(operand as ITypeDefOrRef); List<Expression> args = new List<Expression>(); foreach(ILExpression arg in byteCode.Arguments) { args.Add((Expression)TransformExpression(arg)); } Expression arg1 = args.Count >= 1 ? args[0] : null; Expression arg2 = args.Count >= 2 ? args[1] : null; Expression arg3 = args.Count >= 3 ? args[2] : null; switch (byteCode.Code) { #region Arithmetic case ILCode.Add: case ILCode.Add_Ovf: case ILCode.Add_Ovf_Un: { BinaryOperatorExpression boe; if (byteCode.InferredType is PtrSig) { boe = new BinaryOperatorExpression(arg1, BinaryOperatorType.Add, arg2); if (byteCode.Arguments[0].ExpectedType is PtrSig || byteCode.Arguments[1].ExpectedType is PtrSig) { boe.AddAnnotation(IntroduceUnsafeModifier.PointerArithmeticAnnotation); } } else { boe = new BinaryOperatorExpression(arg1, BinaryOperatorType.Add, arg2); } boe.AddAnnotation(byteCode.Code == ILCode.Add ? AddCheckedBlocks.UncheckedAnnotation : AddCheckedBlocks.CheckedAnnotation); return boe; } case ILCode.Sub: case ILCode.Sub_Ovf: case ILCode.Sub_Ovf_Un: { BinaryOperatorExpression boe; if (byteCode.InferredType is PtrSig) { boe = new BinaryOperatorExpression(arg1, BinaryOperatorType.Subtract, arg2); if (byteCode.Arguments[0].ExpectedType is PtrSig) { boe.WithAnnotation(IntroduceUnsafeModifier.PointerArithmeticAnnotation); } } else { boe = new BinaryOperatorExpression(arg1, BinaryOperatorType.Subtract, arg2); } boe.AddAnnotation(byteCode.Code == ILCode.Sub ? AddCheckedBlocks.UncheckedAnnotation : AddCheckedBlocks.CheckedAnnotation); return boe; } case ILCode.Div: return new BinaryOperatorExpression(arg1, BinaryOperatorType.Divide, arg2); case ILCode.Div_Un: return new BinaryOperatorExpression(arg1, BinaryOperatorType.Divide, arg2); case ILCode.Mul: return new BinaryOperatorExpression(arg1, BinaryOperatorType.Multiply, arg2).WithAnnotation(AddCheckedBlocks.UncheckedAnnotation); case ILCode.Mul_Ovf: return new BinaryOperatorExpression(arg1, BinaryOperatorType.Multiply, arg2).WithAnnotation(AddCheckedBlocks.CheckedAnnotation); case ILCode.Mul_Ovf_Un: return new BinaryOperatorExpression(arg1, BinaryOperatorType.Multiply, arg2).WithAnnotation(AddCheckedBlocks.CheckedAnnotation); case ILCode.Rem: return new BinaryOperatorExpression(arg1, BinaryOperatorType.Modulus, arg2); case ILCode.Rem_Un: return new BinaryOperatorExpression(arg1, BinaryOperatorType.Modulus, arg2); case ILCode.And: return new BinaryOperatorExpression(arg1, BinaryOperatorType.BitwiseAnd, arg2); case ILCode.Or: return new BinaryOperatorExpression(arg1, BinaryOperatorType.BitwiseOr, arg2); case ILCode.Xor: return new BinaryOperatorExpression(arg1, BinaryOperatorType.ExclusiveOr, arg2); case ILCode.Shl: return new BinaryOperatorExpression(arg1, BinaryOperatorType.ShiftLeft, arg2); case ILCode.Shr: return new BinaryOperatorExpression(arg1, BinaryOperatorType.ShiftRight, arg2); case ILCode.Shr_Un: return new BinaryOperatorExpression(arg1, BinaryOperatorType.ShiftRight, arg2); case ILCode.Neg: return new UnaryOperatorExpression(UnaryOperatorType.Minus, arg1).WithAnnotation(AddCheckedBlocks.UncheckedAnnotation); case ILCode.Not: return new UnaryOperatorExpression(UnaryOperatorType.BitNot, arg1); case ILCode.PostIncrement: case ILCode.PostIncrement_Ovf: case ILCode.PostIncrement_Ovf_Un: { if (arg1 is DirectionExpression) arg1 = ((DirectionExpression)arg1).Expression.Detach(); var uoe = new UnaryOperatorExpression( (int)byteCode.Operand > 0 ? UnaryOperatorType.PostIncrement : UnaryOperatorType.PostDecrement, arg1); uoe.AddAnnotation((byteCode.Code == ILCode.PostIncrement) ? AddCheckedBlocks.UncheckedAnnotation : AddCheckedBlocks.CheckedAnnotation); return uoe; } #endregion #region Arrays case ILCode.Newarr: { var ace = new ArrayCreateExpression(); ace.Type = operandAsTypeRef; ComposedType ct = operandAsTypeRef as ComposedType; if (ct != null) { // change "new (int[,])[10] to new int[10][,]" ct.ArraySpecifiers.MoveTo(ace.AdditionalArraySpecifiers); } if (byteCode.Code == ILCode.InitArray) { ace.Initializer = new ArrayInitializerExpression(); ace.Initializer.Elements.AddRange(args); } else { ace.Arguments.Add(arg1); } return ace; } case ILCode.InitArray: { var ace = new ArrayCreateExpression(); ace.Type = operandAsTypeRef; ComposedType ct = operandAsTypeRef as ComposedType; var arrayType = (ArraySigBase)((ITypeDefOrRef)operand).ToTypeSig(); ; if (ct != null) { // change "new (int[,])[10] to new int[10][,]" ct.ArraySpecifiers.MoveTo(ace.AdditionalArraySpecifiers); ace.Initializer = new ArrayInitializerExpression(); } var newArgs = new List<Expression>(); if (arrayType is ArraySig) { foreach (var size in arrayType.GetSizes().Skip(1).Reverse()) { int length = (int)size; for (int j = 0; j < args.Count; j += length) { var child = new ArrayInitializerExpression(); child.Elements.AddRange(args.GetRange(j, length)); newArgs.Add(child); } var temp = args; args = newArgs; newArgs = temp; newArgs.Clear(); } } ace.Initializer.Elements.AddRange(args); return ace; } case ILCode.Ldlen: return arg1.Member("Length").WithAnnotation(GetArrayLengthRef()); case ILCode.Ldelem_I: case ILCode.Ldelem_I1: case ILCode.Ldelem_I2: case ILCode.Ldelem_I4: case ILCode.Ldelem_I8: case ILCode.Ldelem_U1: case ILCode.Ldelem_U2: case ILCode.Ldelem_U4: case ILCode.Ldelem_R4: case ILCode.Ldelem_R8: case ILCode.Ldelem_Ref: case ILCode.Ldelem_Any: return arg1.Indexer(arg2); case ILCode.Ldelema: return MakeRef(arg1.Indexer(arg2)); case ILCode.Stelem_I: case ILCode.Stelem_I1: case ILCode.Stelem_I2: case ILCode.Stelem_I4: case ILCode.Stelem_I8: case ILCode.Stelem_R4: case ILCode.Stelem_R8: case ILCode.Stelem_Ref: case ILCode.Stelem_Any: return new AssignmentExpression(arg1.Indexer(arg2), arg3); case ILCode.CompoundAssignment: { CastExpression cast = arg1 as CastExpression; var boe = cast != null ? (BinaryOperatorExpression)cast.Expression : arg1 as BinaryOperatorExpression; // AssignmentExpression doesn't support overloaded operators so they have to be processed to BinaryOperatorExpression if (boe == null) { var tmp = new ParenthesizedExpression(arg1); ReplaceMethodCallsWithOperators.ProcessInvocationExpression((InvocationExpression)arg1); boe = (BinaryOperatorExpression)tmp.Expression; } var assignment = new AssignmentExpression { Left = boe.Left.Detach(), Operator = ReplaceMethodCallsWithOperators.GetAssignmentOperatorForBinaryOperator(boe.Operator), Right = boe.Right.Detach() }.CopyAnnotationsFrom(boe); // We do not mark the resulting assignment as RestoreOriginalAssignOperatorAnnotation, because // the operator cannot be translated back to the expanded form (as the left-hand expression // would be evaluated twice, and might have side-effects) if (cast != null) { cast.Expression = assignment; return cast; } else { return assignment; } } #endregion #region Comparison case ILCode.Ceq: return new BinaryOperatorExpression(arg1, BinaryOperatorType.Equality, arg2); case ILCode.Cne: return new BinaryOperatorExpression(arg1, BinaryOperatorType.InEquality, arg2); case ILCode.Cgt: return new BinaryOperatorExpression(arg1, BinaryOperatorType.GreaterThan, arg2); case ILCode.Cgt_Un: { // can also mean Inequality, when used with object references var arg1Type = byteCode.Arguments[0].InferredType; if (arg1Type != null && !arg1Type.IsValueType) goto case ILCode.Cne; // when comparing signed integral values using Cgt_Un with 0 // the Ast should actually contain InEquality since "(uint)a > 0u" is identical to "a != 0" if (arg1Type.IsSignedIntegralType()) { var p = arg2 as PrimitiveExpression; if (p != null && p.Value.IsZero()) goto case ILCode.Cne; } goto case ILCode.Cgt; } case ILCode.Cle_Un: { // can also mean Equality, when used with object references var arg1Type = byteCode.Arguments[0].InferredType; if (arg1Type != null && !arg1Type.IsValueType) goto case ILCode.Ceq; // when comparing signed integral values using Cle_Un with 0 // the Ast should actually contain Equality since "(uint)a <= 0u" is identical to "a == 0" if (arg1Type.IsSignedIntegralType()) { var p = arg2 as PrimitiveExpression; if (p != null && p.Value.IsZero()) goto case ILCode.Ceq; } goto case ILCode.Cle; } case ILCode.Cle: return new BinaryOperatorExpression(arg1, BinaryOperatorType.LessThanOrEqual, arg2); case ILCode.Cge_Un: case ILCode.Cge: return new BinaryOperatorExpression(arg1, BinaryOperatorType.GreaterThanOrEqual, arg2); case ILCode.Clt_Un: case ILCode.Clt: return new BinaryOperatorExpression(arg1, BinaryOperatorType.LessThan, arg2); #endregion #region Logical case ILCode.LogicNot: return new UnaryOperatorExpression(UnaryOperatorType.Not, arg1); case ILCode.LogicAnd: return new BinaryOperatorExpression(arg1, BinaryOperatorType.ConditionalAnd, arg2); case ILCode.LogicOr: return new BinaryOperatorExpression(arg1, BinaryOperatorType.ConditionalOr, arg2); case ILCode.TernaryOp: return new ConditionalExpression() { Condition = arg1, TrueExpression = arg2, FalseExpression = arg3 }; case ILCode.NullCoalescing: return new BinaryOperatorExpression(arg1, BinaryOperatorType.NullCoalescing, arg2); #endregion #region Branch case ILCode.Br: return new GotoStatement(((ILLabel)byteCode.Operand).Name); case ILCode.Brtrue: return new IfElseStatement() { Condition = arg1, TrueStatement = new BlockStatement() { new GotoStatement(((ILLabel)byteCode.Operand).Name) } }; case ILCode.LoopOrSwitchBreak: return new BreakStatement(); case ILCode.LoopContinue: return new ContinueStatement(); #endregion #region Conversions case ILCode.Conv_I1: case ILCode.Conv_I2: case ILCode.Conv_I4: case ILCode.Conv_I8: case ILCode.Conv_U1: case ILCode.Conv_U2: case ILCode.Conv_U4: case ILCode.Conv_U8: case ILCode.Conv_I: case ILCode.Conv_U: { // conversion was handled by Convert() function using the info from type analysis CastExpression cast = arg1 as CastExpression; if (cast != null) { cast.AddAnnotation(AddCheckedBlocks.UncheckedAnnotation); } return arg1; } case ILCode.Conv_R4: case ILCode.Conv_R8: case ILCode.Conv_R_Un: // TODO return arg1; case ILCode.Conv_Ovf_I1: case ILCode.Conv_Ovf_I2: case ILCode.Conv_Ovf_I4: case ILCode.Conv_Ovf_I8: case ILCode.Conv_Ovf_U1: case ILCode.Conv_Ovf_U2: case ILCode.Conv_Ovf_U4: case ILCode.Conv_Ovf_U8: case ILCode.Conv_Ovf_I1_Un: case ILCode.Conv_Ovf_I2_Un: case ILCode.Conv_Ovf_I4_Un: case ILCode.Conv_Ovf_I8_Un: case ILCode.Conv_Ovf_U1_Un: case ILCode.Conv_Ovf_U2_Un: case ILCode.Conv_Ovf_U4_Un: case ILCode.Conv_Ovf_U8_Un: case ILCode.Conv_Ovf_I: case ILCode.Conv_Ovf_U: case ILCode.Conv_Ovf_I_Un: case ILCode.Conv_Ovf_U_Un: { // conversion was handled by Convert() function using the info from type analysis CastExpression cast = arg1 as CastExpression; if (cast != null) { cast.AddAnnotation(AddCheckedBlocks.CheckedAnnotation); } return arg1; } case ILCode.Unbox_Any: // unboxing does not require a cast if the argument was an isinst instruction if (arg1 is AsExpression && byteCode.Arguments[0].Code == ILCode.Isinst && TypeAnalysis.IsSameType(operand as ITypeDefOrRef, byteCode.Arguments[0].Operand as ITypeDefOrRef)) return arg1; else goto case ILCode.Castclass; case ILCode.Castclass: if ((byteCode.Arguments[0].InferredType != null && byteCode.Arguments[0].InferredType.IsGenericParameter) || ((ITypeDefOrRef)operand).TryGetGenericSig() != null) return arg1.CastTo(new PrimitiveType("object").WithAnnotation(context.CurrentModule.CorLibTypes.Object.ToTypeDefOrRef())).CastTo(operandAsTypeRef); else return arg1.CastTo(operandAsTypeRef); case ILCode.Isinst: return arg1.CastAs(operandAsTypeRef); case ILCode.Box: return arg1; case ILCode.Unbox: return MakeRef(arg1.CastTo(operandAsTypeRef)); #endregion #region Indirect case ILCode.Ldind_Ref: case ILCode.Ldobj: if (arg1 is DirectionExpression) return ((DirectionExpression)arg1).Expression.Detach(); else return new UnaryOperatorExpression(UnaryOperatorType.Dereference, arg1); case ILCode.Stind_Ref: case ILCode.Stobj: if (arg1 is DirectionExpression) return new AssignmentExpression(((DirectionExpression)arg1).Expression.Detach(), arg2); else return new AssignmentExpression(new UnaryOperatorExpression(UnaryOperatorType.Dereference, arg1), arg2); #endregion case ILCode.Arglist: return new UndocumentedExpression { UndocumentedExpressionType = UndocumentedExpressionType.ArgListAccess }; case ILCode.Break: return InlineAssembly(byteCode, args); case ILCode.Call: case ILCode.CallGetter: case ILCode.CallSetter: return TransformCall(false, byteCode, args); case ILCode.Callvirt: case ILCode.CallvirtGetter: case ILCode.CallvirtSetter: return TransformCall(true, byteCode, args); case ILCode.Ldftn: { IMethod method = (IMethod)operand; var expr = new IdentifierExpression(method.Name); expr.TypeArguments.AddRange(ConvertTypeArguments(method)); expr.AddAnnotation(method); return new IdentifierExpression("ldftn").Invoke(expr) .WithAnnotation(new DelegateConstruction.Annotation(false)); } case ILCode.Ldvirtftn: { IMethod method = (IMethod)operand; var expr = new IdentifierExpression(method.Name); expr.TypeArguments.AddRange(ConvertTypeArguments(method)); expr.AddAnnotation(method); return new IdentifierExpression("ldvirtftn").Invoke(expr) .WithAnnotation(new DelegateConstruction.Annotation(true)); } case ILCode.Calli: return InlineAssembly(byteCode, args); case ILCode.Ckfinite: return InlineAssembly(byteCode, args); case ILCode.Constrained: return InlineAssembly(byteCode, args); case ILCode.Cpblk: return InlineAssembly(byteCode, args); case ILCode.Cpobj: return InlineAssembly(byteCode, args); case ILCode.Dup: return arg1; case ILCode.Endfilter: return new ReturnStatement { Expression = arg1 }; case ILCode.Endfinally: return null; case ILCode.Initblk: return InlineAssembly(byteCode, args); case ILCode.Initobj: return InlineAssembly(byteCode, args); case ILCode.DefaultValue: return MakeDefaultValue(((ITypeDefOrRef)operand).ToTypeSig()); case ILCode.Jmp: return InlineAssembly(byteCode, args); case ILCode.Ldc_I4: return AstBuilder.MakePrimitive((int)operand, byteCode.InferredType); case ILCode.Ldc_I8: return AstBuilder.MakePrimitive((long)operand, byteCode.InferredType); case ILCode.Ldc_R4: case ILCode.Ldc_R8: case ILCode.Ldc_Decimal: return new PrimitiveExpression(operand); case ILCode.Ldfld: if (arg1 is DirectionExpression) arg1 = ((DirectionExpression)arg1).Expression.Detach(); return arg1.Member(((IField)operand).Name).WithAnnotation(operand); case ILCode.Ldsfld: return AstBuilder.ConvertType(((IField)operand).DeclaringType) .Member(((IField)operand).Name).WithAnnotation(operand); case ILCode.Stfld: if (arg1 is DirectionExpression) arg1 = ((DirectionExpression)arg1).Expression.Detach(); return new AssignmentExpression(arg1.Member(((IField)operand).Name).WithAnnotation(operand), arg2); case ILCode.Stsfld: return new AssignmentExpression( AstBuilder.ConvertType(((IField)operand).DeclaringType) .Member(((IField)operand).Name).WithAnnotation(operand), arg1); case ILCode.Ldflda: if (arg1 is DirectionExpression) arg1 = ((DirectionExpression)arg1).Expression.Detach(); return MakeRef(arg1.Member(((IField)operand).Name).WithAnnotation(operand)); case ILCode.Ldsflda: return MakeRef( AstBuilder.ConvertType(((IField)operand).DeclaringType) .Member(((IField)operand).Name).WithAnnotation(operand)); case ILCode.Ldloc: { ILVariable v = (ILVariable)operand; if (!v.IsParameter) localVariablesToDefine.Add((ILVariable)operand); Expression expr; if (v.IsParameter && v.OriginalParameter.IsHiddenThisParameter) expr = new ThisReferenceExpression(); else expr = new IdentifierExpression(((ILVariable)operand).Name).WithAnnotation(operand); return v.IsParameter && v.Type is ByRefSig ? MakeRef(expr) : expr; } case ILCode.Ldloca: { ILVariable v = (ILVariable)operand; if (v.IsParameter && v.OriginalParameter.IsHiddenThisParameter) return MakeRef(new ThisReferenceExpression()); if (!v.IsParameter) localVariablesToDefine.Add((ILVariable)operand); return MakeRef(new IdentifierExpression(((ILVariable)operand).Name).WithAnnotation(operand)); } case ILCode.Ldnull: return new NullReferenceExpression(); case ILCode.Ldstr: return new PrimitiveExpression(operand); case ILCode.Ldtoken: if (operand is ITypeDefOrRef) { return AstBuilder.CreateTypeOfExpression(((ITypeDefOrRef)operand).ToTypeSig()).Member("TypeHandle").WithAnnotation(GetTypeHandleRef()); } else { Expression referencedEntity; string loadName; string handleName; MemberRef memberRef; if (dnlibExtensions.IsField(operand)) { loadName = "fieldof"; handleName = "FieldHandle"; memberRef = GetFieldHandleRef(); IField fr = (IField)operand; referencedEntity = AstBuilder.ConvertType(fr.DeclaringType).Member(fr.Name).WithAnnotation(fr); } else if (dnlibExtensions.IsMethod(operand)) { loadName = "methodof"; handleName = "MethodHandle"; memberRef = GetMethodHandleRef(); IMethod mr = (IMethod)operand; var methodParameters = mr.MethodSig.Params.Select(p => new TypeReferenceExpression(AstBuilder.ConvertType(p))); referencedEntity = AstBuilder.ConvertType(mr.DeclaringType).Invoke(mr.Name, methodParameters).WithAnnotation(mr); } else { loadName = "ldtoken"; handleName = "Handle"; memberRef = null; referencedEntity = new IdentifierExpression(FormatByteCodeOperand(byteCode.Operand)); } return new IdentifierExpression(loadName).Invoke(referencedEntity).WithAnnotation(new LdTokenAnnotation()).Member(handleName); } case ILCode.Leave: return new GotoStatement() { Label = ((ILLabel)operand).Name }; case ILCode.Localloc: { PtrSig ptrType = byteCode.InferredType as PtrSig; TypeSig type; if (ptrType != null) { type = ptrType.Next; } else { type = typeSystem.Byte; } return new StackAllocExpression { Type = AstBuilder.ConvertType(type), CountExpression = arg1 }; } case ILCode.Mkrefany: { DirectionExpression dir = arg1 as DirectionExpression; if (dir != null) { return new UndocumentedExpression { UndocumentedExpressionType = UndocumentedExpressionType.MakeRef, Arguments = { dir.Expression.Detach() } }; } else { return InlineAssembly(byteCode, args); } } case ILCode.Refanytype: return new UndocumentedExpression { UndocumentedExpressionType = UndocumentedExpressionType.RefType, Arguments = { arg1 } }.Member("TypeHandle").WithAnnotation(GetTypeHandleRef()); case ILCode.Refanyval: return MakeRef( new UndocumentedExpression { UndocumentedExpressionType = UndocumentedExpressionType.RefValue, Arguments = { arg1, new TypeReferenceExpression(operandAsTypeRef) } }); case ILCode.Newobj: { ITypeDefOrRef declaringType = ((IMethod)operand).DeclaringType; if (declaringType.TryGetArraySig() != null) { ComposedType ct = AstBuilder.ConvertType(declaringType) as ComposedType; if (ct != null && ct.ArraySpecifiers.Count >= 1) { var ace = new ArrayCreateExpression(); ct.ArraySpecifiers.First().Remove(); ct.ArraySpecifiers.MoveTo(ace.AdditionalArraySpecifiers); ace.Type = ct; ace.Arguments.AddRange(args); return ace; } } if (declaringType.IsAnonymousType()) { MethodDef ctor = ((IMethod)operand).ResolveMethodDef(); if (ctor != null) { AnonymousTypeCreateExpression atce = new AnonymousTypeCreateExpression(); if (CanInferAnonymousTypePropertyNamesFromArguments(args, ctor.Parameters)) { atce.Initializers.AddRange(args); } else { for (int i = 0; i < args.Count; i++) { atce.Initializers.Add( new NamedExpression { Name = ctor.Parameters[i].Name, Expression = args[i] }); } } return atce; } } var oce = new ObjectCreateExpression(); oce.Type = AstBuilder.ConvertType(declaringType); oce.Arguments.AddRange(args); return oce.WithAnnotation(operand); } case ILCode.Nop: return null; case ILCode.Pop: return arg1; case ILCode.Readonly: return InlineAssembly(byteCode, args); case ILCode.Ret: if (methodDef.ReturnType.FullName != "System.Void") { return new ReturnStatement { Expression = arg1 }; } else { return new ReturnStatement(); } case ILCode.Rethrow: return new ThrowStatement(); case ILCode.Sizeof: return new SizeOfExpression { Type = operandAsTypeRef }; case ILCode.Stloc: { ILVariable locVar = (ILVariable)operand; if (!locVar.IsParameter) localVariablesToDefine.Add(locVar); return new AssignmentExpression(new IdentifierExpression(locVar.Name).WithAnnotation(locVar), arg1); } case ILCode.Switch: return InlineAssembly(byteCode, args); case ILCode.Tail: return InlineAssembly(byteCode, args); case ILCode.Throw: return new ThrowStatement { Expression = arg1 }; case ILCode.Unaligned: return InlineAssembly(byteCode, args); case ILCode.Volatile: return InlineAssembly(byteCode, args); case ILCode.YieldBreak: return new YieldBreakStatement(); case ILCode.YieldReturn: return new YieldReturnStatement { Expression = arg1 }; case ILCode.InitObject: case ILCode.InitCollection: { ArrayInitializerExpression initializer = new ArrayInitializerExpression(); for (int i = 1; i < args.Count; i++) { Match m = objectInitializerPattern.Match(args[i]); if (m.Success) { MemberReferenceExpression mre = m.Get<MemberReferenceExpression>("left").Single(); var exp = new NamedExpression { Name = mre.MemberName, Expression = m.Get<Expression>("right").Single().Detach() }; exp.NameToken.CopyAnnotationsFrom(mre); initializer.Elements.Add(exp); } else { m = collectionInitializerPattern.Match(args[i]); if (m.Success) { if (m.Get("arg").Count() == 1) { initializer.Elements.Add(m.Get<Expression>("arg").Single().Detach()); } else { ArrayInitializerExpression argList = new ArrayInitializerExpression(); foreach (var expr in m.Get<Expression>("arg")) { argList.Elements.Add(expr.Detach()); } initializer.Elements.Add(argList); } } else { initializer.Elements.Add(args[i]); } } } ObjectCreateExpression oce = arg1 as ObjectCreateExpression; DefaultValueExpression dve = arg1 as DefaultValueExpression; if (oce != null) { oce.Initializer = initializer; return oce; } else if (dve != null) { oce = new ObjectCreateExpression(dve.Type.Detach()); oce.CopyAnnotationsFrom(dve); oce.Initializer = initializer; return oce; } else { return new AssignmentExpression(arg1, initializer); } } case ILCode.InitializedObject: return new InitializedObjectExpression(); case ILCode.Wrap: return arg1.WithAnnotation(PushNegation.LiftedOperatorAnnotation); case ILCode.AddressOf: return MakeRef(arg1); case ILCode.ExpressionTreeParameterDeclarations: args[args.Count - 1].AddAnnotation(new ParameterDeclarationAnnotation(byteCode)); return args[args.Count - 1]; case ILCode.Await: return new UnaryOperatorExpression(UnaryOperatorType.Await, UnpackDirectionExpression(arg1)); case ILCode.NullableOf: case ILCode.ValueOf: return arg1; default: throw new Exception("Unknown OpCode: " + byteCode.Code); } }
public Node VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression) { return(CreateDummy(arrayCreateExpression)); }
protected void VisitArrayCreateExpression() { ArrayCreateExpression arrayCreateExpression = this.ArrayCreateExpression; var rr = this.Emitter.Resolver.ResolveNode(arrayCreateExpression, this.Emitter) as ArrayCreateResolveResult; var at = (ArrayType)rr.Type; var rank = arrayCreateExpression.Arguments.Count; if (arrayCreateExpression.Initializer.IsNull && rank == 1) { this.Write("new Array("); arrayCreateExpression.Arguments.First().AcceptVisitor(this.Emitter); this.Write(")"); return; } if (at.Dimensions > 1) { this.Write("Bridge.Array.create("); var defaultInitializer = new PrimitiveExpression(Inspector.GetDefaultFieldValue(arrayCreateExpression.Type, this.Emitter.Resolver), "?"); if (defaultInitializer == null) { this.Write("Bridge.getDefaultValue(" + Helpers.TranslateTypeReference(arrayCreateExpression.Type, this.Emitter) + ")"); } else { var primitiveExpr = defaultInitializer as PrimitiveExpression; if (primitiveExpr != null && primitiveExpr.Value is AstType) { this.Write("new " + Helpers.TranslateTypeReference((AstType)primitiveExpr.Value, this.Emitter) + "()"); } else { defaultInitializer.AcceptVisitor(this.Emitter); } } this.WriteComma(); } if (rr.InitializerElements != null && rr.InitializerElements.Count > 0) { this.WriteOpenBracket(); var elements = arrayCreateExpression.Initializer.Elements; new ExpressionListBlock(this.Emitter, elements, null).Emit(); this.WriteCloseBracket(); } else if (at.Dimensions > 1) { this.Write("null"); } if (at.Dimensions > 1) { this.Emitter.Comma = true; for (int i = 0; i < rr.SizeArguments.Count; i++) { var a = rr.SizeArguments[i]; this.EnsureComma(false); if (a.IsCompileTimeConstant) { this.Write(a.ConstantValue); } else if (arrayCreateExpression.Arguments.Count > i) { var arg = arrayCreateExpression.Arguments.ElementAt(i); if (arg != null) { arg.AcceptVisitor(this.Emitter); } } this.Emitter.Comma = true; } this.Write(")"); this.Emitter.Comma = false; } }
public object VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, object data) { throw new NotImplementedException(); }
public void VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression) { JsonObject expression = CreateJsonExpression(arrayCreateExpression); AddKeyword(expression, ArrayCreateExpression.NewKeywordRole); expression.AddJsonValue("array-type", GenTypeInfo(arrayCreateExpression.Type)); if (arrayCreateExpression.Arguments.Count > 0) { expression.AddJsonValue("arguments", GetCommaSeparatedList(arrayCreateExpression.Arguments)); } JsonArray specifierArr = new JsonArray(); foreach (var specifier in arrayCreateExpression.AdditionalArraySpecifiers) { specifier.AcceptVisitor(this); var pop = Pop(); if (pop != null) { specifierArr.AddJsonValue(pop); } } if (specifierArr.Count == 0) { specifierArr = null; } expression.AddJsonValue("specifier", specifierArr); expression.AddJsonValue("initializer", GenExpression(arrayCreateExpression.Initializer)); Push(expression); }
public override void VisitArrayCreateExpression (ArrayCreateExpression arrayCreateExpression) { base.VisitArrayCreateExpression (arrayCreateExpression); if (arrayCreateExpression.Arguments.Count != 1 || !arrayCreateExpression.Initializer.IsNull) return; var count = arrayCreateExpression.Arguments.First (); if (count is PrimitiveExpression && Convert.ToInt32 (((PrimitiveExpression)count).Value) == 0) return; var s = arrayCreateExpression.GetParent<Statement> (); if (s == null) return; var find = new FindFirstUseOfArray (); var variableDeclarationStatement = s as VariableDeclarationStatement; if (variableDeclarationStatement != null) { var v = arrayCreateExpression.GetParent <VariableInitializer> (); find.Variable = new IdentifierExpression (v.Name); } else { var es = s as ExpressionStatement; if (es != null && es.Expression is AssignmentExpression) { find.Variable = ((AssignmentExpression)es.Expression).Left; } else { return; // Don't know what's going on } } if (find.Variable == null) return; arrayCreateExpression.GetParent<EntityDeclaration> ().AcceptVisitor (find); if ((find.First is AssignmentExpression)) return; var i = new IdentifierExpression ("_ai"); var def = GetDefaultValue (arrayCreateExpression.Type); var init = new ForStatement { Condition = new BinaryOperatorExpression ( i, BinaryOperatorType.LessThan, new MemberReferenceExpression ((Expression)find.Variable.Clone (), "length")), EmbeddedStatement = new ExpressionStatement ( new AssignmentExpression ( new IndexerExpression ((Expression)find.Variable.Clone (), i.Clone ()), def.Clone ())), }; init.Initializers.Add (new VariableDeclarationStatement (new PrimitiveType ("number"), i.Identifier, new PrimitiveExpression (0))); init.Iterators.Add (new ExpressionStatement (new UnaryOperatorExpression (UnaryOperatorType.Increment, (Expression)i.Clone ()))); s.Parent.InsertChildAfter (s, init, (Role<Statement>)s.Role); }
public override void VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression) { HandleExpressionNode(arrayCreateExpression); }
public override AstNode VisitArrayCreateExpression(ArrayCreateExpression arrayObjectCreateExpression, ILGenerator data) { return(new ArrayCreationEmitter(arrayObjectCreateExpression, ILGenerator, InstructionsIndexer, Visitor, Locals).Emit()); }
protected void VisitArrayCreateExpression() { ArrayCreateExpression arrayCreateExpression = this.ArrayCreateExpression; var rr = this.ArrayCreateResolveResult ?? (this.Emitter.Resolver.ResolveNode(arrayCreateExpression, this.Emitter) as ArrayCreateResolveResult); var at = (ArrayType)rr.Type; var rank = arrayCreateExpression.Arguments.Count; if (arrayCreateExpression.Initializer.IsNull && rank == 1) { string typedArrayName = null; if (this.Emitter.AssemblyInfo.UseTypedArrays && (typedArrayName = Helpers.GetTypedArrayName(at.ElementType)) != null) { this.Write("new ", typedArrayName, "("); if (this.ArrayCreateResolveResult != null) { AttributeCreateBlock.WriteResolveResult(this.ArrayCreateResolveResult.SizeArguments.First(), this); } else { arrayCreateExpression.Arguments.First().AcceptVisitor(this.Emitter); } this.Write(")"); } else { this.Write(JS.Types.SYSTEM_ARRAY + ".init("); if (this.ArrayCreateResolveResult != null) { AttributeCreateBlock.WriteResolveResult(this.ArrayCreateResolveResult.SizeArguments.First(), this); } else { arrayCreateExpression.Arguments.First().AcceptVisitor(this.Emitter); } this.WriteComma(); var def = Inspector.GetDefaultFieldValue(at.ElementType, arrayCreateExpression.Type); if (def == at.ElementType || def is RawValue) { this.WriteFunction(); this.WriteOpenCloseParentheses(); this.BeginBlock(); this.WriteReturn(true); if (def is RawValue) { this.Write(def.ToString()); } else { this.Write(Inspector.GetStructDefaultValue(at.ElementType, this.Emitter)); } this.WriteSemiColon(); this.WriteNewLine(); this.EndBlock(); } else { this.WriteScript(def); } this.Write(")"); } return; } if (at.Dimensions > 1) { this.Write(JS.Types.SYSTEM_ARRAY + ".create("); var defaultInitializer = new PrimitiveExpression(Inspector.GetDefaultFieldValue(at.ElementType, arrayCreateExpression.Type), "?"); if (defaultInitializer.Value is IType) { this.Write(Inspector.GetStructDefaultValue((IType)defaultInitializer.Value, this.Emitter)); } else if (defaultInitializer.Value is RawValue) { this.Write(defaultInitializer.Value.ToString()); } else { defaultInitializer.AcceptVisitor(this.Emitter); } this.WriteComma(); } if (rr.InitializerElements != null && rr.InitializerElements.Count > 0) { this.WriteOpenBracket(); if (this.ArrayCreateResolveResult != null) { bool needComma = false; foreach (ResolveResult item in this.ArrayCreateResolveResult.InitializerElements) { if (needComma) { this.WriteComma(); } needComma = true; AttributeCreateBlock.WriteResolveResult(item, this); } } else { var elements = arrayCreateExpression.Initializer.Elements; new ExpressionListBlock(this.Emitter, elements, null, null, 0).Emit(); } this.WriteCloseBracket(); } else if (at.Dimensions > 1) { this.Write("null"); } else { this.Write("[]"); } if (at.Dimensions > 1) { this.Emitter.Comma = true; for (int i = 0; i < rr.SizeArguments.Count; i++) { var a = rr.SizeArguments[i]; this.EnsureComma(false); if (a.IsCompileTimeConstant) { this.Write(a.ConstantValue); } else if (this.ArrayCreateResolveResult != null) { AttributeCreateBlock.WriteResolveResult(this.ArrayCreateResolveResult.SizeArguments[i], this); } else if (arrayCreateExpression.Arguments.Count > i) { var arg = arrayCreateExpression.Arguments.ElementAt(i); if (arg != null) { arg.AcceptVisitor(this.Emitter); } } this.Emitter.Comma = true; } this.Write(")"); this.Emitter.Comma = false; } }
public ArrayCreateBlock(IEmitter emitter, ArrayCreateExpression arrayCreateExpression) : base(emitter, arrayCreateExpression) { this.Emitter = emitter; this.ArrayCreateExpression = arrayCreateExpression; }
public void VisitArrayCreateExpression(ArrayCreateExpression node) { NotSupported(node); }
void NewExpression( #line 2014 "cs.ATG" out Expression pexpr) { #line 2015 "cs.ATG" pexpr = null; List<Expression> parameters = new List<Expression>(); TypeReference type = null; Expression expr; Expect(89); if (StartOf(10)) { NonArrayType( #line 2022 "cs.ATG" out type); } if (la.kind == 16 || la.kind == 20) { if (la.kind == 20) { #line 2028 "cs.ATG" ObjectCreateExpression oce = new ObjectCreateExpression(type, parameters); lexer.NextToken(); #line 2029 "cs.ATG" if (type == null) Error("Cannot use an anonymous type with arguments for the constructor"); if (StartOf(26)) { Argument( #line 2030 "cs.ATG" out expr); #line 2030 "cs.ATG" SafeAdd(oce, parameters, expr); while (la.kind == 14) { lexer.NextToken(); Argument( #line 2031 "cs.ATG" out expr); #line 2031 "cs.ATG" SafeAdd(oce, parameters, expr); } } Expect(21); #line 2033 "cs.ATG" pexpr = oce; if (la.kind == 16) { CollectionOrObjectInitializer( #line 2034 "cs.ATG" out expr); #line 2034 "cs.ATG" oce.ObjectInitializer = (CollectionInitializerExpression)expr; } } else { #line 2035 "cs.ATG" ObjectCreateExpression oce = new ObjectCreateExpression(type, parameters); CollectionOrObjectInitializer( #line 2036 "cs.ATG" out expr); #line 2036 "cs.ATG" oce.ObjectInitializer = (CollectionInitializerExpression)expr; #line 2037 "cs.ATG" pexpr = oce; } } else if (la.kind == 18) { lexer.NextToken(); #line 2042 "cs.ATG" ArrayCreateExpression ace = new ArrayCreateExpression(type); /* we must not change RankSpecifier on the null type reference*/ if (ace.CreateType.IsNull) { ace.CreateType = new TypeReference(""); } pexpr = ace; int dims = 0; List<int> ranks = new List<int>(); if (la.kind == 14 || la.kind == 19) { while (la.kind == 14) { lexer.NextToken(); #line 2049 "cs.ATG" dims += 1; } Expect(19); #line 2050 "cs.ATG" ranks.Add(dims); dims = 0; while (la.kind == 18) { lexer.NextToken(); while (la.kind == 14) { lexer.NextToken(); #line 2051 "cs.ATG" ++dims; } Expect(19); #line 2051 "cs.ATG" ranks.Add(dims); dims = 0; } #line 2052 "cs.ATG" ace.CreateType.RankSpecifier = ranks.ToArray(); CollectionInitializer( #line 2053 "cs.ATG" out expr); #line 2053 "cs.ATG" ace.ArrayInitializer = (CollectionInitializerExpression)expr; } else if (StartOf(6)) { Expr( #line 2054 "cs.ATG" out expr); #line 2054 "cs.ATG" if (expr != null) parameters.Add(expr); while (la.kind == 14) { lexer.NextToken(); #line 2055 "cs.ATG" dims += 1; Expr( #line 2056 "cs.ATG" out expr); #line 2056 "cs.ATG" if (expr != null) parameters.Add(expr); } Expect(19); #line 2058 "cs.ATG" ranks.Add(dims); ace.Arguments = parameters; dims = 0; while (la.kind == 18) { lexer.NextToken(); while (la.kind == 14) { lexer.NextToken(); #line 2059 "cs.ATG" ++dims; } Expect(19); #line 2059 "cs.ATG" ranks.Add(dims); dims = 0; } #line 2060 "cs.ATG" ace.CreateType.RankSpecifier = ranks.ToArray(); if (la.kind == 16) { CollectionInitializer( #line 2061 "cs.ATG" out expr); #line 2061 "cs.ATG" ace.ArrayInitializer = (CollectionInitializerExpression)expr; } } else SynErr(212); } else SynErr(213); }
public override void VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression) { new ArrayCreateBlock(this, arrayCreateExpression).Emit(); }
public virtual void VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression) { DebugExpression(arrayCreateExpression); StartNode(arrayCreateExpression); WriteKeyword(ArrayCreateExpression.NewKeywordRole); arrayCreateExpression.Type.AcceptVisitor(this); if (arrayCreateExpression.Arguments.Count > 0) { WriteCommaSeparatedListInBrackets(arrayCreateExpression.Arguments, CodeBracesRangeFlags.SquareBrackets); } int count = 0; foreach (var specifier in arrayCreateExpression.AdditionalArraySpecifiers) { if (count-- <= 0) { cancellationToken.ThrowIfCancellationRequested(); count = CANCEL_CHECK_LOOP_COUNT; } specifier.AcceptVisitor(this); } arrayCreateExpression.Initializer.AcceptVisitor(this); EndNode(arrayCreateExpression); }
public Expression ConvertConstantValue(ResolveResult rr) { if (rr == null) throw new ArgumentNullException("rr"); if (rr is ConversionResolveResult) { // unpack ConversionResolveResult if necessary // (e.g. a boxing conversion or string->object reference conversion) rr = ((ConversionResolveResult)rr).Input; } if (rr is TypeOfResolveResult) { return new TypeOfExpression(ConvertType(rr.Type)); } else if (rr is ArrayCreateResolveResult) { ArrayCreateResolveResult acrr = (ArrayCreateResolveResult)rr; ArrayCreateExpression ace = new ArrayCreateExpression(); ace.Type = ConvertType(acrr.Type); ComposedType composedType = ace.Type as ComposedType; if (composedType != null) { composedType.ArraySpecifiers.MoveTo(ace.AdditionalArraySpecifiers); if (!composedType.HasNullableSpecifier && composedType.PointerRank == 0) ace.Type = composedType.BaseType; } if (acrr.SizeArguments != null && acrr.InitializerElements == null) { ace.AdditionalArraySpecifiers.FirstOrNullObject().Remove(); ace.Arguments.AddRange(acrr.SizeArguments.Select(ConvertConstantValue)); } if (acrr.InitializerElements != null) { ArrayInitializerExpression initializer = new ArrayInitializerExpression(); initializer.Elements.AddRange(acrr.InitializerElements.Select(ConvertConstantValue)); ace.Initializer = initializer; } return ace; } else if (rr.IsCompileTimeConstant) { return ConvertConstantValue(rr.Type, rr.ConstantValue); } else { return new ErrorExpression(); } }
public abstract StringBuilder VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, int data);
public override object VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, object data) { return(base.VisitArrayCreateExpression(arrayCreateExpression, data)); }
void VariableDeclaratorPartAfterIdentifier( #line 1406 "VBNET.ATG" List<VariableDeclaration> fieldDeclaration, string name) { #line 1408 "VBNET.ATG" Expression expr = null; TypeReference type = null; ArrayList rank = null; List<Expression> dimension = null; Location startLocation = t.Location; if ( #line 1414 "VBNET.ATG" IsSize() && !IsDims()) { ArrayInitializationModifier( #line 1414 "VBNET.ATG" out dimension); } if ( #line 1415 "VBNET.ATG" IsDims()) { ArrayNameModifier( #line 1415 "VBNET.ATG" out rank); } if ( #line 1417 "VBNET.ATG" IsObjectCreation()) { Expect(50); ObjectCreateExpression( #line 1417 "VBNET.ATG" out expr); #line 1419 "VBNET.ATG" if (expr is ObjectCreateExpression) { type = ((ObjectCreateExpression)expr).CreateType.Clone(); } else { type = ((ArrayCreateExpression)expr).CreateType.Clone(); } } else if (StartOf(23)) { if (la.kind == 50) { lexer.NextToken(); TypeName( #line 1426 "VBNET.ATG" out type); #line 1428 "VBNET.ATG" if (type != null) { for (int i = fieldDeclaration.Count - 1; i >= 0; i--) { VariableDeclaration vd = fieldDeclaration[i]; if (vd.TypeReference.Type.Length > 0) break; TypeReference newType = type.Clone(); newType.RankSpecifier = vd.TypeReference.RankSpecifier; vd.TypeReference = newType; } } } #line 1440 "VBNET.ATG" if (type == null && (dimension != null || rank != null)) { type = new TypeReference(""); } if (dimension != null) { if(type.RankSpecifier != null) { Error("array rank only allowed one time"); } else { if (rank == null) { type.RankSpecifier = new int[] { dimension.Count - 1 }; } else { rank.Insert(0, dimension.Count - 1); type.RankSpecifier = (int[])rank.ToArray(typeof(int)); } expr = new ArrayCreateExpression(type.Clone(), dimension); } } else if (rank != null) { if(type.RankSpecifier != null) { Error("array rank only allowed one time"); } else { type.RankSpecifier = (int[])rank.ToArray(typeof(int)); } } if (la.kind == 10) { lexer.NextToken(); VariableInitializer( #line 1463 "VBNET.ATG" out expr); } } else SynErr(250); #line 1466 "VBNET.ATG" VariableDeclaration varDecl = new VariableDeclaration(name, expr, type); varDecl.StartLocation = startLocation; varDecl.EndLocation = t.Location; fieldDeclaration.Add(varDecl); }
public void VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression) { StartNode(arrayCreateExpression); // WriteKeyword(ArrayCreateExpression.NewKeywordRole); // arrayCreateExpression.Type.AcceptVisitor(this); if (arrayCreateExpression.Arguments.Count > 0) { WriteKeyword ("new"); WriteIdentifier ("Array"); WriteToken (Roles.LChevron); arrayCreateExpression.Type.AcceptVisitor(this); WriteToken (Roles.RChevron); LPar (); WriteCommaSeparatedList(arrayCreateExpression.Arguments); RPar (); } // foreach (var specifier in arrayCreateExpression.AdditionalArraySpecifiers) { // specifier.AcceptVisitor(this); // } arrayCreateExpression.Initializer.AcceptVisitor(this); EndNode(arrayCreateExpression); }
void ObjectCreateExpression( #line 1935 "VBNET.ATG" out Expression oce) { #line 1937 "VBNET.ATG" TypeReference type = null; Expression initializer = null; List<Expression> arguments = null; ArrayList dimensions = null; oce = null; bool canBeNormal; bool canBeReDim; Expect(148); if (StartOf(7)) { NonArrayTypeName( #line 1945 "VBNET.ATG" out type, false); if (la.kind == 25) { lexer.NextToken(); NormalOrReDimArgumentList( #line 1946 "VBNET.ATG" out arguments, out canBeNormal, out canBeReDim); Expect(26); if (la.kind == 23 || #line 1947 "VBNET.ATG" la.kind == Tokens.OpenParenthesis) { if ( #line 1947 "VBNET.ATG" la.kind == Tokens.OpenParenthesis) { ArrayTypeModifiers( #line 1948 "VBNET.ATG" out dimensions); CollectionInitializer( #line 1949 "VBNET.ATG" out initializer); } else { CollectionInitializer( #line 1950 "VBNET.ATG" out initializer); } } #line 1952 "VBNET.ATG" if (canBeReDim && !canBeNormal && initializer == null) initializer = new CollectionInitializerExpression(); } } #line 1956 "VBNET.ATG" if (initializer == null) { oce = new ObjectCreateExpression(type, arguments); } else { if (dimensions == null) dimensions = new ArrayList(); dimensions.Insert(0, (arguments == null) ? 0 : Math.Max(arguments.Count - 1, 0)); type.RankSpecifier = (int[])dimensions.ToArray(typeof(int)); ArrayCreateExpression ace = new ArrayCreateExpression(type, initializer as CollectionInitializerExpression); ace.Arguments = arguments; oce = ace; } if (la.kind == 218) { #line 1970 "VBNET.ATG" NamedArgumentExpression memberInitializer = null; lexer.NextToken(); #line 1974 "VBNET.ATG" CollectionInitializerExpression memberInitializers = new CollectionInitializerExpression(); memberInitializers.StartLocation = la.Location; Expect(23); MemberInitializer( #line 1978 "VBNET.ATG" out memberInitializer); #line 1979 "VBNET.ATG" memberInitializers.CreateExpressions.Add(memberInitializer); while (la.kind == 12) { lexer.NextToken(); MemberInitializer( #line 1981 "VBNET.ATG" out memberInitializer); #line 1982 "VBNET.ATG" memberInitializers.CreateExpressions.Add(memberInitializer); } Expect(24); #line 1986 "VBNET.ATG" memberInitializers.EndLocation = t.Location; if(oce is ObjectCreateExpression) { ((ObjectCreateExpression)oce).ObjectInitializer = memberInitializers; } } }
public override object Visit(ArrayCreateExpression arrayCreateExpression, object data) { ReturnType type = new ReturnType(arrayCreateExpression.CreateType); if (arrayCreateExpression.Parameters != null && arrayCreateExpression.Parameters.Count > 0) { int[] newRank = new int[arrayCreateExpression.Rank.Length + 1]; newRank[0] = arrayCreateExpression.Parameters.Count - 1; Array.Copy(type.ArrayDimensions, 0, newRank, 1, type.ArrayDimensions.Length); type.ArrayDimensions = newRank; } return type; }
public void VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression) { throw new NotImplementedException(); }
public override object Visit (ArrayCreation arrayCreationExpression) { var result = new ArrayCreateExpression (); var location = LocationsBag.GetLocations (arrayCreationExpression); result.AddChild (new CSharpTokenNode (Convert (arrayCreationExpression.Location), "new".Length), ArrayCreateExpression.Roles.Keyword); if (arrayCreationExpression.NewType != null) result.AddChild ((AstNode)arrayCreationExpression.NewType.Accept (this), ArrayCreateExpression.Roles.ReturnType); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), ArrayCreateExpression.Roles.LBracket); if (arrayCreationExpression.Arguments != null) { var commaLocations = LocationsBag.GetLocations (arrayCreationExpression.Arguments); for (int i = 0 ;i < arrayCreationExpression.Arguments.Count; i++) { result.AddChild ((AstNode)arrayCreationExpression.Arguments[i].Accept (this), ObjectCreateExpression.Roles.Initializer); if (commaLocations != null && i > 0) result.AddChild (new CSharpTokenNode (Convert (commaLocations [commaLocations.Count - i]), 1), IndexerExpression.Roles.Comma); } } if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[1]), 1), ArrayCreateExpression.Roles.RBracket); if (arrayCreationExpression.Initializers != null && arrayCreationExpression.Initializers.Count != 0) { var initLocation = LocationsBag.GetLocations (arrayCreationExpression.Initializers); result.AddChild (new CSharpTokenNode (Convert (arrayCreationExpression.Initializers.Location), 1), ArrayCreateExpression.Roles.LBrace); var commaLocations = LocationsBag.GetLocations (arrayCreationExpression.Initializers.Elements); for (int i = 0; i < arrayCreationExpression.Initializers.Count; i++) { result.AddChild ((AstNode)arrayCreationExpression.Initializers[i].Accept (this), ObjectCreateExpression.Roles.Initializer); if (commaLocations != null && i > 0) { result.AddChild (new CSharpTokenNode (Convert (commaLocations [commaLocations.Count - i]), 1), IndexerExpression.Roles.Comma); } } if (initLocation != null) result.AddChild (new CSharpTokenNode (Convert (initLocation[initLocation.Count - 1]), 1), ArrayCreateExpression.Roles.RBrace); } return result; }
public override StringBuilder VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, int data) { throw new NotImplementedException(); }
Expression ConvertTypeIs(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(null); } Expression converted = Convert(invocation.Arguments.ElementAt(0)); AstType type = ConvertTypeReference(invocation.Arguments.ElementAt(1)); if (converted != null && type != null) { return new IsExpression { Expression = converted, Type = type } } ; return(null); } #endregion #region Convert Array Expression ConvertArrayIndex(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(NotSupported(invocation)); } Expression targetConverted = Convert(invocation.Arguments.First()); if (targetConverted == null) { return(null); } Expression index = invocation.Arguments.ElementAt(1); Expression indexConverted = Convert(index); if (indexConverted != null) { return(new IndexerExpression(targetConverted, indexConverted)); } IList <Expression> indexesConverted = ConvertExpressionsArray(index); if (indexesConverted != null) { return(new IndexerExpression(targetConverted, indexesConverted)); } return(null); } Expression ConvertArrayLength(InvocationExpression invocation) { if (invocation.Arguments.Count != 1) { return(NotSupported(invocation)); } Expression targetConverted = Convert(invocation.Arguments.Single()); if (targetConverted != null) { return(targetConverted.Member("Length", TextTokenKind.InstanceProperty).WithAnnotation(Create_SystemArray_get_Length())); } else { return(null); } } ModuleDef GetModule() { if (context.CurrentMethod != null && context.CurrentMethod.Module != null) { return(context.CurrentMethod.Module); } if (context.CurrentType != null && context.CurrentType.Module != null) { return(context.CurrentType.Module); } if (context.CurrentModule != null) { return(context.CurrentModule); } return(null); } IMDTokenProvider Create_SystemArray_get_Length() { if (Create_SystemArray_get_Length_result_initd) { return(Create_SystemArray_get_Length_result); } Create_SystemArray_get_Length_result_initd = true; var module = GetModule(); if (module == null) { return(null); } const string propName = "Length"; var type = module.CorLibTypes.GetTypeRef("System", "Array"); var retType = module.CorLibTypes.Int32; var mr = new MemberRefUser(module, "get_" + propName, MethodSig.CreateInstance(retType), type); Create_SystemArray_get_Length_result = mr; var md = mr.ResolveMethod(); if (md == null || md.DeclaringType == null) { return(mr); } var prop = md.DeclaringType.FindProperty(propName); if (prop == null) { return(mr); } Create_SystemArray_get_Length_result = prop; return(prop); } IMDTokenProvider Create_SystemArray_get_Length_result; bool Create_SystemArray_get_Length_result_initd; Expression ConvertNewArrayInit(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(NotSupported(invocation)); } AstType elementType = ConvertTypeReference(invocation.Arguments.ElementAt(0)); IList <Expression> elements = ConvertExpressionsArray(invocation.Arguments.ElementAt(1)); if (elementType != null && elements != null) { if (ContainsAnonymousType(elementType)) { elementType = null; } return(new ArrayCreateExpression { Type = elementType, AdditionalArraySpecifiers = { new ArraySpecifier() }, Initializer = new ArrayInitializerExpression(elements) }); } return(null); } Expression ConvertNewArrayBounds(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(NotSupported(invocation)); } AstType elementType = ConvertTypeReference(invocation.Arguments.ElementAt(0)); IList <Expression> arguments = ConvertExpressionsArray(invocation.Arguments.ElementAt(1)); if (elementType != null && arguments != null) { if (ContainsAnonymousType(elementType)) { elementType = null; } ArrayCreateExpression ace = new ArrayCreateExpression(); ace.Type = elementType; ace.Arguments.AddRange(arguments); return(ace); } return(null); } bool ContainsAnonymousType(AstType type) { foreach (AstType t in type.DescendantsAndSelf.OfType <AstType>()) { ITypeDefOrRef tr = t.Annotation <ITypeDefOrRef>(); if (tr != null && tr.IsAnonymousType()) { return(true); } } return(false); } #endregion }
/// <summary> /// Gets the initializer statement, if any. /// </summary> /// <returns>The initializer.</returns> /// <param name="element">The variable to get the initializer for.</param> public string GetInitializer(AST.DataElement element) { if (element.DefaultValue == null) { return(string.Empty); } if (element.DefaultValue is AST.ArrayCreateExpression) { var asexp = (AST.ArrayCreateExpression)element.DefaultValue; var nae = new ArrayCreateExpression() { SourceExpression = asexp.SourceExpression, SourceResultType = asexp.SourceResultType, }; nae.ElementExpressions = asexp.ElementExpressions .Select(x => new PrimitiveExpression() { SourceExpression = x.SourceExpression, SourceResultType = x.SourceResultType, Parent = nae, Value = ((PrimitiveExpression)x).Value }).Cast <Expression>().ToArray(); return(RenderExpression(nae)); } else if (element.DefaultValue is Array) { var arr = (Array)element.DefaultValue; var nae = new ArrayCreateExpression() { SourceExpression = null, SourceResultType = element.CecilType.GetArrayElementType(), }; nae.ElementExpressions = Enumerable.Range(0, arr.Length) .Select(x => new PrimitiveExpression() { SourceExpression = null, SourceResultType = element.CecilType.GetArrayElementType(), Parent = nae, Value = arr.GetValue(0) }).Cast <Expression>().ToArray(); return(RenderExpression(nae)); } else if (element.DefaultValue is ICSharpCode.Decompiler.CSharp.Syntax.AstNode) { var eltype = Type.GetType(element.CecilType.FullName); var defaultvalue = eltype != null && element.CecilType.IsValueType ? Activator.CreateInstance(eltype) : null; return(RenderExpression(new AST.PrimitiveExpression() { Value = defaultvalue, SourceResultType = element.CecilType })); } else if (element.DefaultValue is AST.EmptyArrayCreateExpression) { var ese = element.DefaultValue as AST.EmptyArrayCreateExpression; return(RenderExpression(new AST.EmptyArrayCreateExpression() { SizeExpression = ese.SizeExpression.Clone(), SourceExpression = ese.SourceExpression, SourceResultType = ese.SourceResultType })); } else if (element.CecilType.IsArrayType() && element.DefaultValue == null) { return(RenderExpression(new EmptyArrayCreateExpression() { SourceExpression = null, SourceResultType = element.CecilType, SizeExpression = new MemberReferenceExpression() { Name = element.Name, SourceExpression = null, SourceResultType = element.CecilType, Target = element } })); } else { return(RenderExpression(new AST.PrimitiveExpression() { Value = element.DefaultValue, SourceResultType = element.CecilType })); } }
protected void VisitArrayCreateExpression() { ArrayCreateExpression arrayCreateExpression = this.ArrayCreateExpression; var rr = this.Emitter.Resolver.ResolveNode(arrayCreateExpression, this.Emitter) as ArrayCreateResolveResult; var at = (ArrayType)rr.Type; var rank = arrayCreateExpression.Arguments.Count; if (arrayCreateExpression.Initializer.IsNull && rank == 1) { this.Write("Bridge.Array.init("); arrayCreateExpression.Arguments.First().AcceptVisitor(this.Emitter); this.WriteComma(); var def = Inspector.GetDefaultFieldValue(at.ElementType); if (def == at.ElementType) { this.WriteFunction(); this.WriteOpenCloseParentheses(); this.BeginBlock(); this.WriteReturn(true); this.Write(Inspector.GetStructDefaultValue(at.ElementType, this.Emitter)); this.WriteSemiColon(); this.WriteNewLine(); this.EndBlock(); } else { this.WriteScript(def); } this.Write(")"); return; } if (at.Dimensions > 1) { this.Write("Bridge.Array.create("); var defaultInitializer = new PrimitiveExpression(Inspector.GetDefaultFieldValue(at.ElementType), "?"); if (defaultInitializer.Value is IType) { this.Write(Inspector.GetStructDefaultValue((IType)defaultInitializer.Value, this.Emitter)); } else { defaultInitializer.AcceptVisitor(this.Emitter); } this.WriteComma(); } if (rr.InitializerElements != null && rr.InitializerElements.Count > 0) { this.WriteOpenBracket(); var elements = arrayCreateExpression.Initializer.Elements; new ExpressionListBlock(this.Emitter, elements, null).Emit(); this.WriteCloseBracket(); } else if (at.Dimensions > 1) { this.Write("null"); } else { this.Write("[]"); } if (at.Dimensions > 1) { this.Emitter.Comma = true; for (int i = 0; i < rr.SizeArguments.Count; i++) { var a = rr.SizeArguments[i]; this.EnsureComma(false); if (a.IsCompileTimeConstant) { this.Write(a.ConstantValue); } else if (arrayCreateExpression.Arguments.Count > i) { var arg = arrayCreateExpression.Arguments.ElementAt(i); if (arg != null) { arg.AcceptVisitor(this.Emitter); } } this.Emitter.Comma = true; } this.Write(")"); this.Emitter.Comma = false; } }
public JNode VisitArrayCreateExpression(ArrayCreateExpression node) { return(Visit(node.Resolve())); }
public override object Visit (ArrayCreation arrayCreationExpression) { var result = new ArrayCreateExpression (); var location = LocationsBag.GetLocations (arrayCreationExpression); if (arrayCreationExpression.NewType != null) result.AddChild (ConvertToType (arrayCreationExpression.NewType), ArrayCreateExpression.Roles.Type); if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), ArrayCreateExpression.Roles.LBracket); if (arrayCreationExpression.Arguments != null) { var commaLocations = LocationsBag.GetLocations (arrayCreationExpression.Arguments); for (int i = 0 ;i < arrayCreationExpression.Arguments.Count; i++) { result.AddChild ((Expression)arrayCreationExpression.Arguments[i].Accept (this), ArrayCreateExpression.Roles.Argument); if (commaLocations != null && i > 0) result.AddChild (new CSharpTokenNode (Convert (commaLocations [commaLocations.Count - i]), 1), ArrayCreateExpression.Roles.Comma); } } var next = arrayCreationExpression.Rank.Next; while (next != null) { ArraySpecifier spec = new ArraySpecifier (next.Dimension); var loc = LocationsBag.GetLocations (next); spec.AddChild (new CSharpTokenNode (Convert (next.Location), 1), ArraySpecifier.Roles.LBracket); if (loc != null) result.AddChild (new CSharpTokenNode (Convert (loc[0]), 1), ArraySpecifier.Roles.RBracket); result.AddChild (spec, ArrayCreateExpression.AdditionalArraySpecifierRole); next = next.Next; } if (location != null) result.AddChild (new CSharpTokenNode (Convert (location[1]), 1), ArrayCreateExpression.Roles.RBracket); if (arrayCreationExpression.Initializers != null && arrayCreationExpression.Initializers.Count != 0) { var initLocation = LocationsBag.GetLocations (arrayCreationExpression.Initializers); ArrayInitializerExpression initializer = new ArrayInitializerExpression(); initializer.AddChild (new CSharpTokenNode (Convert (arrayCreationExpression.Initializers.Location), 1), ArrayCreateExpression.Roles.LBrace); var commaLocations = LocationsBag.GetLocations (arrayCreationExpression.Initializers.Elements); for (int i = 0; i < arrayCreationExpression.Initializers.Count; i++) { initializer.AddChild ((Expression)arrayCreationExpression.Initializers[i].Accept (this), ArrayInitializerExpression.Roles.Expression); if (commaLocations != null && i > 0) { initializer.AddChild (new CSharpTokenNode (Convert (commaLocations [commaLocations.Count - i]), 1), IndexerExpression.Roles.Comma); } } if (initLocation != null) initializer.AddChild (new CSharpTokenNode (Convert (initLocation[initLocation.Count - 1]), 1), ArrayCreateExpression.Roles.RBrace); result.AddChild (initializer, ArrayCreateExpression.InitializerRole); } return result; }
public override AstNode VisitArrayCreateExpression(ArrayCreateExpression arrayObjectCreateExpression, ILGenerator data) { return(_constructorEmitter.EmitArrayCreationExpression(arrayObjectCreateExpression, data)); }
public override void VisitArrayCreateExpression(ArrayCreateExpression syntax) { _underlyingVisitor.VisitArrayCreateExpression(syntax); }
public override object VisitReDimStatement(ReDimStatement reDimStatement, object data) { base.VisitReDimStatement(reDimStatement, data); if (resolver.CompilationUnit == null) { return(null); } if (reDimStatement.ReDimClauses.Count != 1) { return(null); } if (reDimStatement.IsPreserve) { if (reDimStatement.ReDimClauses[0].Arguments.Count > 1) { // multidimensional Redim Preserve // replace with: // MyArray = (int[,])Microsoft.VisualBasic.CompilerServices.Utils.CopyArray(MyArray, new int[dim1+1, dim2+1]); ResolveResult rr = Resolve(reDimStatement.ReDimClauses[0].TargetObject); if (rr != null && rr.ResolvedType != null && rr.ResolvedType.IsArrayReturnType) { ArrayCreateExpression ace = new ArrayCreateExpression(ConvertType(rr.ResolvedType)); foreach (Expression arg in reDimStatement.ReDimClauses[0].Arguments) { ace.Arguments.Add(Expression.AddInteger(arg, 1)); } ReplaceCurrentNode(new ExpressionStatement( new AssignmentExpression( reDimStatement.ReDimClauses[0].TargetObject, AssignmentOperatorType.Assign, new CastExpression( ace.CreateType, new InvocationExpression( MakeFieldReferenceExpression("Microsoft.VisualBasic.CompilerServices.Utils.CopyArray"), new List <Expression> { reDimStatement.ReDimClauses[0].TargetObject, ace } ), CastType.Cast ) ))); } } } else { // replace with array create expression ResolveResult rr = Resolve(reDimStatement.ReDimClauses[0].TargetObject); if (rr != null && rr.ResolvedType != null && rr.ResolvedType.IsArrayReturnType) { ArrayCreateExpression ace = new ArrayCreateExpression(ConvertType(rr.ResolvedType)); foreach (Expression arg in reDimStatement.ReDimClauses[0].Arguments) { ace.Arguments.Add(Expression.AddInteger(arg, 1)); } ReplaceCurrentNode(new ExpressionStatement( new AssignmentExpression(reDimStatement.ReDimClauses[0].TargetObject, AssignmentOperatorType.Assign, ace))); } } return(null); }
public virtual object VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, object data) { throw new global::System.NotImplementedException("ArrayCreateExpression"); }
Expression ConvertTypeIs(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(null); } Expression converted = Convert(invocation.Arguments.ElementAt(0)); AstType type = ConvertTypeReference(invocation.Arguments.ElementAt(1)); if (converted != null && type != null) { return new IsExpression { Expression = converted, Type = type } } ; return(null); } #endregion #region Convert Array Expression ConvertArrayIndex(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(NotSupported(invocation)); } Expression targetConverted = Convert(invocation.Arguments.First()); if (targetConverted == null) { return(null); } Expression index = invocation.Arguments.ElementAt(1); Expression indexConverted = Convert(index); if (indexConverted != null) { return(new IndexerExpression(targetConverted, indexConverted)); } IList <Expression> indexesConverted = ConvertExpressionsArray(index); if (indexConverted != null) { return(new IndexerExpression(targetConverted, indexesConverted)); } return(null); } Expression ConvertArrayLength(InvocationExpression invocation) { if (invocation.Arguments.Count != 1) { return(NotSupported(invocation)); } Expression targetConverted = Convert(invocation.Arguments.Single()); if (targetConverted != null) { return(targetConverted.Member("Length")); } else { return(null); } } Expression ConvertNewArrayInit(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(NotSupported(invocation)); } AstType elementType = ConvertTypeReference(invocation.Arguments.ElementAt(0)); IList <Expression> elements = ConvertExpressionsArray(invocation.Arguments.ElementAt(1)); if (elementType != null && elements != null) { if (ContainsAnonymousType(elementType)) { elementType = null; } return(new ArrayCreateExpression { Type = elementType, AdditionalArraySpecifiers = { new ArraySpecifier() }, Initializer = new ArrayInitializerExpression(elements) }); } return(null); } Expression ConvertNewArrayBounds(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(NotSupported(invocation)); } AstType elementType = ConvertTypeReference(invocation.Arguments.ElementAt(0)); IList <Expression> arguments = ConvertExpressionsArray(invocation.Arguments.ElementAt(1)); if (elementType != null && arguments != null) { if (ContainsAnonymousType(elementType)) { elementType = null; } ArrayCreateExpression ace = new ArrayCreateExpression(); ace.Type = elementType; ace.Arguments.AddRange(arguments); return(ace); } return(null); } bool ContainsAnonymousType(AstType type) { foreach (AstType t in type.DescendantsAndSelf.OfType <AstType>()) { TypeReference tr = t.Annotation <TypeReference>(); if (tr != null && tr.IsAnonymousType()) { return(true); } } return(false); } #endregion }
public override object Visit(ArrayCreation arrayCreationExpression) { var result = new ArrayCreateExpression(); var location = LocationsBag.GetLocations(arrayCreationExpression); result.AddChild(new CSharpTokenNode(Convert(arrayCreationExpression.Location), ArrayCreateExpression.NewKeywordRole), ArrayCreateExpression.NewKeywordRole); if (arrayCreationExpression.TypeExpression != null) result.AddChild(ConvertToType(arrayCreationExpression.TypeExpression), Roles.Type); var next = arrayCreationExpression.Rank; if (arrayCreationExpression.Arguments != null) { // skip first array rank. next = next.Next; if (location != null) result.AddChild(new CSharpTokenNode(Convert(location [0]), Roles.LBracket), Roles.LBracket); var commaLocations = LocationsBag.GetLocations(arrayCreationExpression.Arguments); for (int i = 0; i < arrayCreationExpression.Arguments.Count; i++) { var arg = arrayCreationExpression.Arguments [i]; if (arg != null) result.AddChild((Expression)arg.Accept(this), Roles.Argument); if (commaLocations != null && i < commaLocations.Count) result.AddChild(new CSharpTokenNode(Convert(commaLocations [i]), Roles.Comma), Roles.Comma); } if (location != null && location.Count > 1) result.AddChild(new CSharpTokenNode(Convert(location [1]), Roles.RBracket), Roles.RBracket); } while (next != null) { var spec = new ArraySpecifier(next.Dimension); var loc = LocationsBag.GetLocations(next); spec.AddChild(new CSharpTokenNode(Convert(next.Location), Roles.LBracket), Roles.LBracket); result.AddChild(spec, ArrayCreateExpression.AdditionalArraySpecifierRole); if (loc != null) result.AddChild(new CSharpTokenNode(Convert(loc [0]), Roles.RBracket), Roles.RBracket); next = next.Next; } if (arrayCreationExpression.Initializers != null) { var initLocation = LocationsBag.GetLocations(arrayCreationExpression.Initializers); var initializer = new ArrayInitializerExpression(); initializer.AddChild(new CSharpTokenNode(Convert(arrayCreationExpression.Initializers.Location), Roles.LBrace), Roles.LBrace); var commaLocations = LocationsBag.GetLocations(arrayCreationExpression.Initializers.Elements); for (int i = 0; i < arrayCreationExpression.Initializers.Count; i++) { var init = arrayCreationExpression.Initializers [i]; if (init == null) continue; initializer.AddChild((Expression)init.Accept(this), Roles.Expression); if (commaLocations != null && i < commaLocations.Count) { initializer.AddChild(new CSharpTokenNode(Convert(commaLocations [i]), Roles.Comma), Roles.Comma); } } if (initLocation != null) { if (initLocation.Count == 2) // optional comma initializer.AddChild(new CSharpTokenNode(Convert(initLocation [0]), Roles.Comma), Roles.Comma); initializer.AddChild(new CSharpTokenNode(Convert(initLocation [initLocation.Count - 1]), Roles.RBrace), Roles.RBrace); } result.AddChild(initializer, ArrayCreateExpression.InitializerRole); } return result; }
public void VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression) { StartNode(arrayCreateExpression); WriteKeyword(ArrayCreateExpression.NewKeywordRole); arrayCreateExpression.Type.AcceptVisitor(this); if (arrayCreateExpression.Arguments.Count > 0) { WriteCommaSeparatedListInBrackets(arrayCreateExpression.Arguments); } foreach (var specifier in arrayCreateExpression.AdditionalArraySpecifiers) { specifier.AcceptVisitor(this); } arrayCreateExpression.Initializer.AcceptVisitor(this); EndNode(arrayCreateExpression); }
public override object VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, object data) { return(CreateReturnType(arrayCreateExpression.CreateType)); }
private void PrimaryExpr(out Expression pexpr) { TypeReference typeReference = null; List<TypeReference> types = null; Expression expression; bool flag = false; pexpr = null; if (this.la.kind == 0x70) { base.lexer.NextToken(); pexpr = new PrimitiveExpression(true, "true"); } else if (this.la.kind == 0x47) { base.lexer.NextToken(); pexpr = new PrimitiveExpression(false, "false"); } else if (this.la.kind == 0x59) { base.lexer.NextToken(); pexpr = new PrimitiveExpression(null, "null"); } else if (this.la.kind == 2) { base.lexer.NextToken(); pexpr = new PrimitiveExpression(this.t.literalValue, this.t.val); } else if ((this.la.kind == 1) && (this.Peek(1).kind == 10)) { base.Expect(1); typeReference = new TypeReference(this.t.val); base.Expect(10); pexpr = new TypeReferenceExpression(typeReference); base.Expect(1); if (typeReference.Type == "global") { typeReference.IsGlobal = true; typeReference.Type = this.t.val ?? "?"; } else { typeReference.Type = typeReference.Type + "." + (this.t.val ?? "?"); } } else if (this.la.kind == 1) { base.lexer.NextToken(); pexpr = new IdentifierExpression(this.t.val); } else if (this.la.kind == 20) { base.lexer.NextToken(); this.Expr(out expression); base.Expect(0x15); pexpr = new ParenthesizedExpression(expression); } else if (!this.StartOf(0x1a)) { if (this.la.kind == 110) { base.lexer.NextToken(); pexpr = new ThisReferenceExpression(); } else if (this.la.kind == 50) { base.lexer.NextToken(); Expression targetObject = new BaseReferenceExpression(); if (this.la.kind == 15) { base.lexer.NextToken(); base.Expect(1); targetObject = new FieldReferenceExpression(targetObject, this.t.val); } else if (this.la.kind == 0x12) { base.lexer.NextToken(); this.Expr(out expression); List<Expression> indices = new List<Expression>(); if (expression != null) { indices.Add(expression); } while (this.la.kind == 14) { base.lexer.NextToken(); this.Expr(out expression); if (expression != null) { indices.Add(expression); } } base.Expect(0x13); targetObject = new IndexerExpression(targetObject, indices); } else { base.SynErr(0xb3); } pexpr = targetObject; } else if (this.la.kind == 0x58) { base.lexer.NextToken(); this.NonArrayType(out typeReference); List<Expression> parameters = new List<Expression>(); if (this.la.kind == 20) { base.lexer.NextToken(); ObjectCreateExpression expression3 = new ObjectCreateExpression(typeReference, parameters); if (this.StartOf(0x15)) { this.Argument(out expression); if (expression != null) { parameters.Add(expression); } while (this.la.kind == 14) { base.lexer.NextToken(); this.Argument(out expression); if (expression != null) { parameters.Add(expression); } } } base.Expect(0x15); pexpr = expression3; } else if (this.la.kind == 0x12) { base.lexer.NextToken(); flag = true; ArrayCreateExpression expression4 = new ArrayCreateExpression(typeReference); pexpr = expression4; int item = 0; List<int> list4 = new List<int>(); if ((this.la.kind == 14) || (this.la.kind == 0x13)) { while (this.la.kind == 14) { base.lexer.NextToken(); item++; } base.Expect(0x13); list4.Add(item); item = 0; while (this.la.kind == 0x12) { base.lexer.NextToken(); while (this.la.kind == 14) { base.lexer.NextToken(); item++; } base.Expect(0x13); list4.Add(item); item = 0; } expression4.CreateType.RankSpecifier = list4.ToArray(); this.ArrayInitializer(out expression); expression4.ArrayInitializer = (ArrayInitializerExpression) expression; } else if (this.StartOf(5)) { this.Expr(out expression); if (expression != null) { parameters.Add(expression); } while (this.la.kind == 14) { base.lexer.NextToken(); item++; this.Expr(out expression); if (expression != null) { parameters.Add(expression); } } base.Expect(0x13); list4.Add(item); expression4.Arguments = parameters; for (item = 0; this.la.kind == 0x12; item = 0) { base.lexer.NextToken(); while (this.la.kind == 14) { base.lexer.NextToken(); item++; } base.Expect(0x13); list4.Add(item); } expression4.CreateType.RankSpecifier = list4.ToArray(); if (this.la.kind == 0x10) { this.ArrayInitializer(out expression); expression4.ArrayInitializer = (ArrayInitializerExpression) expression; } } else { base.SynErr(180); } } else { base.SynErr(0xb5); } } else if (this.la.kind == 0x72) { base.lexer.NextToken(); base.Expect(20); if (this.NotVoidPointer()) { base.Expect(0x7a); typeReference = new TypeReference("void"); } else if (this.StartOf(9)) { this.TypeWithRestriction(out typeReference, true, true); } else { base.SynErr(0xb6); } base.Expect(0x15); pexpr = new TypeOfExpression(typeReference); } else if ((this.la.kind == 0x3e) && (this.Peek(1).kind == 20)) { base.Expect(0x3e); base.Expect(20); this.Type(out typeReference); base.Expect(0x15); pexpr = new DefaultValueExpression(typeReference); } else if (this.la.kind == 0x68) { base.lexer.NextToken(); base.Expect(20); this.Type(out typeReference); base.Expect(0x15); pexpr = new SizeOfExpression(typeReference); } else if (this.la.kind == 0x39) { base.lexer.NextToken(); base.Expect(20); this.Expr(out expression); base.Expect(0x15); pexpr = new CheckedExpression(expression); } else if (this.la.kind == 0x75) { base.lexer.NextToken(); base.Expect(20); this.Expr(out expression); base.Expect(0x15); pexpr = new UncheckedExpression(expression); } else if (this.la.kind == 0x3f) { base.lexer.NextToken(); this.AnonymousMethodExpr(out expression); pexpr = expression; } else { base.SynErr(0xb7); } } else { string typeName = null; switch (this.la.kind) { case 0x3d: base.lexer.NextToken(); typeName = "decimal"; break; case 0x41: base.lexer.NextToken(); typeName = "double"; break; case 0x4a: base.lexer.NextToken(); typeName = "float"; break; case 0x33: base.lexer.NextToken(); typeName = "bool"; break; case 0x35: base.lexer.NextToken(); typeName = "byte"; break; case 0x38: base.lexer.NextToken(); typeName = "char"; break; case 0x51: base.lexer.NextToken(); typeName = "int"; break; case 0x56: base.lexer.NextToken(); typeName = "long"; break; case 90: base.lexer.NextToken(); typeName = "object"; break; case 0x65: base.lexer.NextToken(); typeName = "sbyte"; break; case 0x67: base.lexer.NextToken(); typeName = "short"; break; case 0x6b: base.lexer.NextToken(); typeName = "string"; break; case 0x73: base.lexer.NextToken(); typeName = "uint"; break; case 0x74: base.lexer.NextToken(); typeName = "ulong"; break; case 0x77: base.lexer.NextToken(); typeName = "ushort"; break; } this.t.val = ""; base.Expect(15); base.Expect(1); pexpr = new FieldReferenceExpression(new TypeReferenceExpression(typeName), this.t.val); } while ((this.StartOf(0x1b) || (this.IsGenericFollowedBy(15) && this.IsTypeReferenceExpression(pexpr))) || this.IsGenericFollowedBy(20)) { if ((this.la.kind == 0x1f) || (this.la.kind == 0x20)) { if (this.la.kind == 0x1f) { base.lexer.NextToken(); pexpr = new UnaryOperatorExpression(pexpr, UnaryOperatorType.PostIncrement); } else if (this.la.kind == 0x20) { base.lexer.NextToken(); pexpr = new UnaryOperatorExpression(pexpr, UnaryOperatorType.PostDecrement); } else { base.SynErr(0xb8); } } else { if (this.la.kind == 0x2f) { base.lexer.NextToken(); base.Expect(1); pexpr = new PointerReferenceExpression(pexpr, this.t.val); continue; } if (this.la.kind == 15) { base.lexer.NextToken(); base.Expect(1); pexpr = new FieldReferenceExpression(pexpr, this.t.val); continue; } if (this.IsGenericFollowedBy(15) && this.IsTypeReferenceExpression(pexpr)) { this.TypeArgumentList(out types, false); base.Expect(15); base.Expect(1); pexpr = new FieldReferenceExpression(this.GetTypeReferenceExpression(pexpr, types), this.t.val); continue; } if (this.la.kind == 20) { base.lexer.NextToken(); List<Expression> arguments = new List<Expression>(); if (this.StartOf(0x15)) { this.Argument(out expression); if (expression != null) { arguments.Add(expression); } while (this.la.kind == 14) { base.lexer.NextToken(); this.Argument(out expression); if (expression != null) { arguments.Add(expression); } } } base.Expect(0x15); pexpr = new InvocationExpression(pexpr, arguments); continue; } if (this.IsGenericFollowedBy(20)) { this.TypeArgumentList(out types, false); base.Expect(20); List<Expression> list6 = new List<Expression>(); if (this.StartOf(0x15)) { this.Argument(out expression); if (expression != null) { list6.Add(expression); } while (this.la.kind == 14) { base.lexer.NextToken(); this.Argument(out expression); if (expression != null) { list6.Add(expression); } } } base.Expect(0x15); pexpr = new InvocationExpression(pexpr, list6, types); continue; } if (flag) { this.Error("element access not allow on array creation"); } List<Expression> list7 = new List<Expression>(); base.lexer.NextToken(); this.Expr(out expression); if (expression != null) { list7.Add(expression); } while (this.la.kind == 14) { base.lexer.NextToken(); this.Expr(out expression); if (expression != null) { list7.Add(expression); } } base.Expect(0x13); pexpr = new IndexerExpression(pexpr, list7); } } }
protected void VisitArrayCreateExpression() { ArrayCreateExpression arrayCreateExpression = this.ArrayCreateExpression; var rr = this.Emitter.Resolver.ResolveNode(arrayCreateExpression, this.Emitter) as ArrayCreateResolveResult; var at = (ArrayType)rr.Type; var rank = arrayCreateExpression.Arguments.Count; if (arrayCreateExpression.Initializer.IsNull && rank == 1) { string typedArrayName = null; if (this.Emitter.AssemblyInfo.UseTypedArrays && (typedArrayName = Helpers.GetTypedArrayName(at.ElementType)) != null) { this.Write("new ", typedArrayName, "("); arrayCreateExpression.Arguments.First().AcceptVisitor(this.Emitter); this.Write(")"); } else { string typeName = BridgeTypes.ToJsName(at, this.Emitter); this.Write(typeName); this.WriteOpenParentheses(); arrayCreateExpression.Arguments.First().AcceptVisitor(this.Emitter); this.WriteCloseParentheses(); /* * this.Write("Bridge.Array.init("); * arrayCreateExpression.Arguments.First().AcceptVisitor(this.Emitter); * this.WriteComma(); * * var def = Inspector.GetDefaultFieldValue(at.ElementType); * if (def == at.ElementType) * { * this.WriteFunction(); * this.WriteOpenCloseParentheses(); * this.BeginBlock(); * this.WriteReturn(true); * this.Write(Inspector.GetStructDefaultValue(at.ElementType, this.Emitter)); * this.WriteSemiColon(); * this.WriteNewLine(); * this.EndBlock(); * } * else * { * this.WriteScript(def); * } * * this.Write(")");*/ } return; } /* * if (at.Dimensions > 1) * { * this.Write("Bridge.Array.create("); * var defaultInitializer = new PrimitiveExpression(Inspector.GetDefaultFieldValue(at.ElementType), "?"); * * if (defaultInitializer.Value is IType) * { * this.Write(Inspector.GetStructDefaultValue((IType)defaultInitializer.Value, this.Emitter)); * } * else * { * defaultInitializer.AcceptVisitor(this.Emitter); * } * * this.WriteComma(); * }*/ if (rr.InitializerElements != null && rr.InitializerElements.Count > 0) { if (at.Dimensions > 1) { string typeName = BridgeTypes.ToJsName(((ArrayType)rr.Type).ElementType, this.Emitter); this.Write("System.MultiArray"); this.WriteOpenParentheses(); this.Write(typeName); this.WriteCloseParentheses(); this.WriteOpenParentheses(); this.Emitter.Comma = true; this.WriteOpenBrace(); for (int i = 0; i < rr.SizeArguments.Count; i++) { var a = rr.SizeArguments[i]; if (a.IsCompileTimeConstant) { this.Write(a.ConstantValue); } else if (arrayCreateExpression.Arguments.Count > i) { var arg = arrayCreateExpression.Arguments.ElementAt(i); if (arg != null) { arg.AcceptVisitor(this.Emitter); } } if (i != rr.SizeArguments.Count - 1) { this.EnsureComma(false); } this.Emitter.Comma = true; } this.WriteCloseBrace(); this.Emitter.Comma = false; this.WriteComma(); var elements = arrayCreateExpression.Initializer.Elements; new ExpressionListBlock(this.Emitter, elements, null).Emit(); this.WriteCloseParentheses(); } else { string typeName = BridgeTypes.ToJsName(((ArrayType)rr.Type).ElementType, this.Emitter); this.Write("System.Array"); this.WriteOpenParentheses(); this.Write(typeName); this.WriteCloseParentheses(); this.WriteOpenParentheses(); this.Write(rr.InitializerElements.Count); this.WriteComma(); var elements = arrayCreateExpression.Initializer.Elements; new ExpressionListBlock(this.Emitter, elements, null).Emit(); this.WriteCloseParentheses(); } } else if (at.Dimensions > 1) { this.Write("null"); } else { this.Write("[]"); } /* * if (at.Dimensions > 1) * { * this.Emitter.Comma = true; * * for (int i = 0; i < rr.SizeArguments.Count; i++) * { * var a = rr.SizeArguments[i]; * this.EnsureComma(false); * * if (a.IsCompileTimeConstant) * { * this.Write(a.ConstantValue); * } * else if (arrayCreateExpression.Arguments.Count > i) * { * var arg = arrayCreateExpression.Arguments.ElementAt(i); * * if (arg != null) * { * arg.AcceptVisitor(this.Emitter); * } * } * this.Emitter.Comma = true; * } * * this.Write(")"); * this.Emitter.Comma = false; * } */ }
public virtual S VisitArrayCreateExpression(ArrayCreateExpression arrayObjectCreateExpression, T data) { return(VisitChildren(arrayObjectCreateExpression, data)); }
public virtual void VisitArrayCreateExpression (ArrayCreateExpression arrayCreateExpression) { VisitChildren (arrayCreateExpression); }
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) { ArrayCreateExpression o = other as ArrayCreateExpression; return(o != null && this.Type.DoMatch(o.Type, match) && this.Arguments.DoMatch(o.Arguments, match) && this.AdditionalArraySpecifiers.DoMatch(o.AdditionalArraySpecifiers, match) && this.Initializer.DoMatch(o.Initializer, match)); }
public override void VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression) { throw NotSupportedToConsistency(); }