public static bool IsReducibleArrayCreateExpression(IExpression expr) { if (expr is IArrayCreateExpression) { IArrayCreateExpression iace = (IArrayCreateExpression)expr; return(iace.Initializer == null); } else if (expr is IObjectCreateExpression) { IObjectCreateExpression ioce = (IObjectCreateExpression)expr; Type type = ioce.Type.DotNetType; Type gtd = type.IsGenericType ? type.GetGenericTypeDefinition() : type; bool reducible = (gtd == typeof(Distributions.DistributionRefArray <,>) && ioce.Arguments.Count == 1) || (gtd == typeof(Distributions.DistributionStructArray <,>) && ioce.Arguments.Count == 1) || (gtd == typeof(Distributions.DistributionRefArray2D <,>) && ioce.Arguments.Count == 2) || (gtd == typeof(Distributions.DistributionStructArray2D <,>) && ioce.Arguments.Count == 2); return(reducible && (ioce.Initializer == null)); } else { return(false); } }
public object Evaluate(IObjectCreateExpression ioce) { Type t = Builder.ToType(ioce.Type); object[] args = new object[ioce.Arguments.Count]; for (int i = 0; i < args.Length; i++) { args[i] = Evaluate(ioce.Arguments[i]); } return(Activator.CreateInstance(t, args)); }
public override void VisitObjectCreateExpression(IObjectCreateExpression value) { _formatter.WriteKeyword("new-object"); WriteWhitespace(); VisitType(value.Constructor.DeclaringType); if (value.Arguments.Count > 0) { _formatter.Write("("); VisitExpressionCollection(value.Arguments, true); _formatter.Write(")"); } }
public virtual void VisitObjectCreateExpression(IObjectCreateExpression value) { VisitType(value.Type); if (value.Constructor != null) { VisitMethodReference(value.Constructor); } VisitExpressionCollection(value.Arguments); if (value.Initializer != null) { VisitBlockExpression(value.Initializer); } }
private void WriteVariableDecl(LanguageWriter w, bool nohandle) { if (nohandle) { IObjectCreateExpression exp_create = (IObjectCreateExpression)this.exp; // 変数型 IOptionalModifier modopt = this.var_type as IOptionalModifier; if (modopt != null && modopt.Modifier.Namespace == "System.Runtime.CompilerServices" && modopt.Modifier.Name == "IsConst") { this.var_type = modopt.ElementType; } new TypeRef(this.var_type).WriteName(w); // 変数名 w.Write(" "); w.WriteDeclaration(this.var_name); // 初期化子 if (exp_create.Arguments.Count == 0) { w.WriteComment(" /* 既定のコンストラクタ */"); } else { w.Write("("); w.WriteExpressionCollection(exp_create.Arguments); w.Write(")"); } } else { // = で代入の場合: ハンドル表記でないと、コンストラクタ呼び出しになってしまう // 変数型 new TypeRef(this.var_type).WriteNameWithRef(w); // 変数名 w.Write(" "); w.WriteDeclaration(this.var_name); // 代入する値 w.Write("="); ExpressionWriter.WriteExpression(w, this.exp, false); } w.Write(";"); }
public override bool Equals(object obj) { if (this == obj) { return(true); } IObjectCreateExpression expression = obj as IObjectCreateExpression; if (expression == null) { return(false); } if (!Constructor.Equals(expression.Constructor)) { return(false); } if (Initializer == null) { if (expression.Initializer != null) { return(false); } } else if (!Initializer.Equals(expression.Initializer)) { return(false); } if (Arguments.Count != expression.Arguments.Count) { return(false); } for (int i = 0; i < Arguments.Count; i++) { if (!(Arguments[i].Equals(expression.Arguments[i]))) { return(false); } } return(true); }
protected override IExpression ConvertAssign(IAssignExpression iae) { iae = (IAssignExpression)base.ConvertAssign(iae); if (iae.Target is IArrayIndexerExpression && iae.Expression is IObjectCreateExpression) { IArrayIndexerExpression target = (IArrayIndexerExpression)iae.Target; IExpression parent = target.Target; IObjectCreateExpression ioce = (IObjectCreateExpression)iae.Expression; Type type = Builder.ToType(ioce.Type); if (MessageTransform.IsFileArray(type) && MessageTransform.IsFileArray(parent.GetExpressionType()) && ioce.Arguments.Count == 2) { // change new FileArray(name, dimensions) into FileArray(parent, index, dimensions) IList <IExpression> args = Builder.ExprCollection(); args.Add(target.Target); args.AddRange(target.Indices); args.Add(ioce.Arguments[1]); return(Builder.AssignExpr(target, Builder.NewObject(type, args))); } } return(iae); }
private static void WriteObjectCreate(LanguageWriter w, IObjectCreateExpression exp) { if (exp.Constructor == null) { // 構造体のデフォルトコンストラクタ (StatementAnalyze でフィルタリングされている筈だけれども) new TypeRef(exp.Type).WriteName(w); #if FALSE w.Write("("); #warning 構造体デフォルトコンストラクタ 対応漏れがあるかもしれないので保留 w.WriteComment("/* C++/CLI では本来は T value; 等と書く筈です。*/"); w.Write(") "); #else w.Write("()"); #endif } else { ITypeReference declaringType = exp.Constructor.DeclaringType as ITypeReference; TypeRef decl_type = new TypeRef(exp.Constructor.DeclaringType); if (decl_type.IsRefType) { w.WriteKeyword("gcnew"); w.Write(" "); } decl_type.WriteName(w); w.Write("("); w.WriteExpressionCollection(exp.Arguments); w.Write(")"); } if (exp.Initializer != null) { WriteBlock(w, exp.Initializer); } }
//=========================================================== // Expression 分岐 //=========================================================== public virtual void WriteExpression(IExpression expression) { if (expression == null) { return; } mwg.Reflector.CppCli.ExpressionWriter.WriteExpression(this, expression, false); #if FALSE #pragma warning disable 612 IMemberInitializerExpression expression3 = expression as IMemberInitializerExpression; if (expression3 != null) { this.WriteMemberInitializerExpression(expression3); return; } IAddressOutExpression expression27 = expression as IAddressOutExpression; if (expression27 != null) { this.WriteAddressOutExpression(expression27); return; } IAddressReferenceExpression expression26 = expression as IAddressReferenceExpression; if (expression26 != null) { this.WriteAddressReferenceExpression(expression26); return; } IDelegateCreateExpression iDelegateCreateExpression = expression as IDelegateCreateExpression; if (iDelegateCreateExpression != null) { this.WriteDelegateCreateExpression(iDelegateCreateExpression); return; } IMethodInvokeExpression iMethodInvokeExpression = expression as IMethodInvokeExpression; if (iMethodInvokeExpression != null) { this.WriteMethodInvokeExpression(iMethodInvokeExpression); return; } IVariableDeclarationExpression expression15 = expression as IVariableDeclarationExpression; if (expression15 != null) { this.WriteVariableDeclaration(expression15.Variable); return; } ITypeOfExpression iTypeOfExpression = expression as ITypeOfExpression; if (iTypeOfExpression != null) { this.WriteTypeOfExpression(iTypeOfExpression); return; } ISnippetExpression iSnippetExpression = expression as ISnippetExpression; if (iSnippetExpression != null) { this.WriteSnippetExpression(iSnippetExpression); return; } IUnaryExpression iUnaryExpression = expression as IUnaryExpression; if (iUnaryExpression != null) { this.WriteUnaryExpression(iUnaryExpression); return; } IObjectCreateExpression iObjectCreateExpression = expression as IObjectCreateExpression; if (iObjectCreateExpression != null) { this.WriteObjectCreateExpression(iObjectCreateExpression); return; } IVariableReferenceExpression iVariableReferenceExpression = expression as IVariableReferenceExpression; if (iVariableReferenceExpression != null) { this.WriteVariableReferenceExpression(iVariableReferenceExpression); return; } IThisReferenceExpression expression12 = expression as IThisReferenceExpression; if (expression12 != null) { this.WriteThisReferenceExpression(expression12); return; } ITryCastExpression iTryCastExpression = expression as ITryCastExpression; if (iTryCastExpression != null) { this.WriteTryCastExpression(iTryCastExpression); return; } IConditionExpression expression9 = expression as IConditionExpression; if (expression9 != null) { this.WriteConditionExpression(expression9); return; } IFieldReferenceExpression iFieldReferenceExpression = expression as IFieldReferenceExpression; if (iFieldReferenceExpression != null) { this.WriteFieldReferenceExpression(iFieldReferenceExpression); return; } IPropertyIndexerExpression iPropertyIndexerExpression = expression as IPropertyIndexerExpression; if (iPropertyIndexerExpression != null) { this.WritePropertyIndexerExpression(iPropertyIndexerExpression); return; } ITypeReferenceExpression iTypeReferenceExpression = expression as ITypeReferenceExpression; if (iTypeReferenceExpression != null) { this.WriteTypeReferenceExpression(iTypeReferenceExpression); return; } IMethodReferenceExpression iMethodReferenceExpression = expression as IMethodReferenceExpression; if (iMethodReferenceExpression != null) { this.WriteMethodReferenceExpression(iMethodReferenceExpression); return; } IPropertyReferenceExpression iPropertyReferenceExpression = expression as IPropertyReferenceExpression; if (iPropertyReferenceExpression != null) { this.WritePropertyReferenceExpression(iPropertyReferenceExpression); return; } ICastExpression expression5 = expression as ICastExpression; if (expression5 != null) { this.WriteCastExpression(expression5); return; } ICanCastExpression iCanCastExpression = expression as ICanCastExpression; if (iCanCastExpression != null) { this.WriteCanCastExpression(iCanCastExpression); return; } ICastExpression iCastExpression = expression as ICastExpression; if (iCastExpression != null) { this.WriteCastExpression(iCastExpression); return; } ILiteralExpression literalExpression = expression as ILiteralExpression; if (literalExpression != null) { this.WriteLiteralExpression(literalExpression); return; } IBinaryExpression iBinaryExpression = expression as IBinaryExpression; if (iBinaryExpression != null) { mwg.Reflector.CppCli.ExpressionWriter.WriteExpression(this, expression, true); //this.WriteBinaryExpression(iBinaryExpression); return; } IArrayIndexerExpression expression30 = expression as IArrayIndexerExpression; if (expression30 != null) { this.WriteArrayIndexerExpression(expression30); return; } IAddressDereferenceExpression expression29 = expression as IAddressDereferenceExpression; if (expression29 != null) { this.WriteAddressDereferenceExpression(expression29); return; } IAddressOfExpression expression28 = expression as IAddressOfExpression; if (expression28 != null) { this.WriteAddressOfExpression(expression28); return; } IArgumentListExpression expression25 = expression as IArgumentListExpression; if (expression25 != null) { this.WriteArgumentListExpression(expression25); return; } IBaseReferenceExpression iBaseReferenceExpression = expression as IBaseReferenceExpression; if (iBaseReferenceExpression != null) { this.WriteBaseReferenceExpression(iBaseReferenceExpression); return; } IArgumentReferenceExpression expression13 = expression as IArgumentReferenceExpression; if (expression13 != null) { this.WriteArgumentReferenceExpression(expression13); return; } IArrayCreateExpression expression10 = expression as IArrayCreateExpression; if (expression10 != null) { this.WriteArrayCreateExpression(expression10); return; } IAssignExpression iAssignExpression = expression as IAssignExpression; if (iAssignExpression != null) { this.WriteAssignExpression(iAssignExpression); return; } IBlockExpression expression2 = expression as IBlockExpression; if (expression2 != null) { this.WriteBlockExpression(expression2); return; } #pragma warning restore 612 this.Write(expression.ToString()); #endif }
//=========================================================== // using の変換 //=========================================================== /// <summary> /// Using 文を他の構文に変換して、yields に変換後の Statement を書き込みます。 /// </summary> /// <param name="yields">変換後の Statement の書き込み先を指定します。</param> /// <param name="state">using 構文を表現する Statement を指定します。</param> /// <param name="last">state が Statements の中で最後の Statement か否かを指定します。</param> public static void TransformUsingStatement(Gen::List <IStatement> yields, IUsingStatement state, bool last) { // 変数の宣言の場合 IAssignExpression assig = state.Expression as IAssignExpression; if (assig != null) { do { IVariableDeclarationExpression var_decl_x = assig.Target as IVariableDeclarationExpression; if (var_decl_x == null) { continue; } IVariableDeclaration var_decl = var_decl_x.Variable as IVariableDeclaration; if (var_decl == null) { continue; } IObjectCreateExpression exp_create = assig.Expression as IObjectCreateExpression; if (exp_create != null) { LocalRefVariableStatement s_lr = new LocalRefVariableStatement(var_decl, assig.Expression, state.Body); s_lr.noblock = last; yields.Add(s_lr); } else { //yields.Add(new ExpressionStatement(assig)); //yields.Add(state.Body); //yields.Add(new DeleteStatement(new VariableReferenceExpression(var_decl))); //↑ 中で例外が起こったときのことを考えていない。 // 宣言部分と代入部分を分離 IStatement s_decl = new ExpressionStatement(var_decl_x); IStatement s_asgn = new ExpressionStatement( new AssignExpression( new VariableReferenceExpression(var_decl), assig.Expression ) ); IStatement s_delete = new DeleteStatement(new VariableReferenceExpression(var_decl)); // 宣言 yields.Add(s_decl); // try-finally BlockStatement try_block = new BlockStatement(); try_block.Statements.Add(s_asgn); try_block.Statements.AddRange(state.Body.Statements); BlockStatement finally_block = new BlockStatement(); finally_block.Statements.Add(s_delete); TryCatchFinallyStatement s_tcf = new TryCatchFinallyStatement(try_block); s_tcf.Finally = finally_block; yields.Add(s_tcf); } return; }while(false); throw new InterfaceNotImplementedException("×実装中×", typeof(IVariableDeclarationExpression), assig.Target); } // 変数の参照の場合 IVariableReferenceExpression varref = state.Expression as IVariableReferenceExpression; if (varref != null) { IStatement s_delete = new DeleteStatement(varref); // try-finally TryCatchFinallyStatement s_tcf = new TryCatchFinallyStatement(state.Body); BlockStatement finally_block = new BlockStatement(); finally_block.Statements.Add(s_delete); s_tcf.Finally = finally_block; yields.Add(s_tcf); return; } throw new InterfaceNotImplementedException( "Unexpected using-statement expression interface (expects IAssignExpression or IVariableReferenceExpression)", typeof(IAssignExpression), state.Expression); }
private void WriteObjectCreateExpression(IObjectCreateExpression value, IFormatter formatter) { formatter.Write("("); formatter.WriteKeyword("new"); formatter.Write(" "); if (value.Constructor != null) { this.WriteTypeReference((ITypeReference)value.Type, formatter, this.GetMethodReferenceDescription(value.Constructor), value.Constructor); } else { this.WriteType(value.Type, formatter); } formatter.Write("()).ctor"); formatter.Write("("); this.WriteExpressionList(value.Arguments, formatter); formatter.Write(")"); IBlockExpression initializer = value.Initializer as IBlockExpression; if ((initializer != null) && (initializer.Expressions.Count > 0)) { formatter.Write(" "); this.WriteExpression(initializer, formatter); } }
private static void WriteLocalRefVariable(LanguageWriter w, LocalRefVariableStatement state) { state.Write(w); return; if (!state.noblock) { w.PushScope(); //--------------------------------------------- w.Write("{"); w.WriteLine(); w.WriteIndent(); } if (!w.SuppressOutput) { //* #warning local-ref: 上層で無駄な宣言を取り除くべき if (w.scope.ContainsVariable(state.var_name)) { w.scope[state.var_name].local_scope = true; } else { w.scope.RegisterVariable(state.var_name, true); } //*/ } IObjectCreateExpression exp_create = state.exp as IObjectCreateExpression; bool nohandle = exp_create != null; // ハンドル表記でなくても良いかどうか if (nohandle) { // 変数型 IOptionalModifier modopt = state.var_type as IOptionalModifier; if (modopt != null && modopt.Modifier.Namespace == "System.Runtime.CompilerServices" && modopt.Modifier.Name == "IsConst") { state.var_type = modopt.ElementType; } new TypeRef(state.var_type).WriteName(w); // 変数名 w.Write(" "); w.WriteDeclaration(state.var_name); // 初期化子 if (exp_create.Arguments.Count == 0) { w.WriteComment(" /* 既定のコンストラクタ */"); } else { w.Write("("); w.WriteExpressionCollection(exp_create.Arguments); w.Write(")"); } } else { // = で代入の場合: ハンドル表記でないと、コンストラクタ呼び出しになってしまう // 変数型 new TypeRef(state.var_type).WriteNameWithRef(w); // 変数名 w.Write(" "); w.WriteDeclaration(state.var_name); // 代入する値 w.Write("="); ExpressionWriter.WriteExpression(w, state.exp, false); } w.Write(";"); // 後に続く内容 w.WriteLine(); WriteBlock(w, state.block); // ハンドル表記で宣言した場合はちゃんと delete if (!nohandle) { WriteStatement(w, new DeleteStatement(new VariableReferenceExpression(state.decl))); } w.WriteOutdent(); w.Write("}"); w.WriteLine(); if (state.labelname != null) { w.__WriteLabel(state.labelname); w.Write(";"); w.WriteLine(); } if (!state.noblock) { //--------------------------------------------- w.PopScope(); } //*/ }
public virtual void VisitObjectCreateExpression(IObjectCreateExpression value) { this.VisitType(value.Type); if (value.Constructor != null) { this.VisitMethodReference(value.Constructor); } this.VisitExpressionCollection(value.Arguments); if (value.Initializer != null) { this.VisitBlockExpression(value.Initializer); } }
public virtual IExpression TransformObjectCreateExpression(IObjectCreateExpression value) { this.InsituTransformExpressionCollection(value.Arguments); if (value.Initializer != null) { value.Initializer = (IBlockExpression)this.TransformBlockExpression(value.Initializer); } return value; }