Example #1
0
 private static void WriteStatementCollection(LanguageWriter w, IStatementCollection statementCollection)
 {
     foreach (IStatement current in StatementAnalyze.ModifyStatements(statementCollection))
     {
         WriteStatement(w, current);
     }
 }
Example #2
0
 public virtual void VisitStatementCollection(IStatementCollection statements)
 {
     if (statements == null)
     {
         return;
     }
     foreach (var statement in statements)
     {
         VisitStatement(statement);
     }
 }
Example #3
0
        private static bool IsBlank(IStatement state)
        {
            if (state == null)
            {
                return(true);
            }

            IBlockStatement statement = state as IBlockStatement;

            if (statement != null)
            {
                IStatementCollection statements = statement.Statements;
                if (statements != null)
                {
                    return(statements.Count == 0);
                }
            }
            return(false);
        }
        //===========================================================
        //		構文合致: function-try-statement
        //		判定が難しい為之は延期
        //		・catch 等から抜ける全てのパスで throw しているか確認しなければならない
        //===========================================================
        public static bool IsFunctionTry(IStatementCollection states)
        {
            if (states == null || states.Count == 0)
            {
                return(false);
            }

            // 前に連なっても OK なのは T var=nullptr のみ (この場合には最初の使用時に T も一緒に表示させなければならない...)
            int last = states.Count - 1;

            for (int i = 0; i < last; i++)
            {
                IExpressionStatement state_exp = states[i] as IExpressionStatement;
                if (state_exp == null)
                {
                    return(false);
                }

                IAssignExpression exp_assign = state_exp.Expression as IAssignExpression;
                if (exp_assign == null)
                {
                    return(false);
                }

                IVariableDeclaration decl = exp_assign.Target as IVariableDeclaration;
                if (decl == null)
                {
                    return(false);
                }

                ILiteralExpression exp_lit = exp_assign.Expression as ILiteralExpression;
                if (exp_lit == null || exp_lit.Value != null)
                {
                    return(false);
                }
            }

            return(false);
        }
 //===========================================================
 //		構文合致: catch(...)
 //===========================================================
 private static void ModifyCatchClauses(ITryCatchFinallyStatement state_tcf)
 {
     if (state_tcf.CatchClauses != null)
     {
         foreach (ICatchClause clause in state_tcf.CatchClauses)
         {
             TypeRef var_type = new TypeRef(clause.Variable.VariableType);
             if (var_type.IsObject && clause.Condition != null)
             {
                 ISnippetExpression iSnippent = clause.Condition as ISnippetExpression;
                 if (iSnippent != null && iSnippent.Value == "?")
                 {
                     IStatementCollection clause_body = clause.Body.Statements;
                     if (IsCatchAllClause(ref clause_body))
                     {
                         clause.Body      = new CBlockStatement(clause_body);
                         clause.Condition = new IsCatchAll();
                     }
                 }
             }
         }
     }
 }
        private static bool DetectLocalRefVariable(string var_disposable, IStatement next, IStatement next2, out LocalRefVariableStatement ret_lrv)
        {
            ret_lrv = new LocalRefVariableStatement();
            //-------------------------------------------------------
            // 第二文
            //-------------------------------------------------------
            // try{
            //   ... // null-declarations
            //   var_name=var_disposable;
            //   ...
            // }fault{
            //   var_name->Dispose();
            // }
            ITryCatchFinallyStatement state_next = next as ITryCatchFinallyStatement;

            if (state_next == null ||
                state_next.Try == null || state_next.Try.Statements.Count == 0 ||
                state_next.Fault == null || state_next.Fault.Statements.Count != 1
                )
            {
                return(false);
            }

            //
            // null-declarations を飛ばす
            //
            int i_assign = 0;
            IStatementCollection try_states = state_next.Try.Statements;

            while (IsNullDeclaration(try_states[i_assign]))
            {
                if (++i_assign >= try_states.Count)
                {
                    return(false);
                }
            }

            //
            // var_name=var_disposable の var_name を取得
            //
            IAssignExpression exp_assign = GetAssignExpression(state_next.Try.Statements[i_assign]);

            if (exp_assign == null || var_disposable != GetVariableName(exp_assign.Expression))
            {
                return(false);
            }
            ret_lrv.var_name = GetVariableName(exp_assign.Target);
            if (ret_lrv.var_name == null)
            {
                return(false);
            }

            //
            // fault 内の形式の確認
            //
            {
                // **->**();
                IMethodInvokeExpression exp_inv = GetInvokeExpression(state_next.Fault.Statements[0]);
                if (exp_inv == null || exp_inv.Arguments.Count != 0)
                {
                    return(false);
                }
                // **->Dispose();
                IMethodReferenceExpression method = exp_inv.Method as IMethodReferenceExpression;
                if (method == null || method.Method == null || method.Method.Name != "Dispose")
                {
                    return(false);
                }
                // disposable->Dispose();
                if (ret_lrv.var_name != GetVariableName(method.Target))
                {
                    return(false);
                }
            }

            //-------------------------------------------------------
            // 第三文
            //-------------------------------------------------------
            // "Label:"?
            //   var_name->Dispose();
            //
            ILabeledStatement labeled = next2 as ILabeledStatement;

            if (labeled != null)
            {
                ret_lrv.labelname = labeled.Name;
                next2             = labeled.Statement;
            }
            {
                // **->**();
                IMethodInvokeExpression exp_inv = GetInvokeExpression(next2);
                if (exp_inv == null || exp_inv.Arguments.Count != 0)
                {
                    return(false);
                }
                // **->Dispose();
                IMethodReferenceExpression method = exp_inv.Method as IMethodReferenceExpression;
                if (method == null || method.Method == null || method.Method.Name != "Dispose")
                {
                    return(false);
                }
                // disposable->Dispose();
                if (ret_lrv.var_name != GetVariableName(method.Target))
                {
                    return(false);
                }
            }

            ret_lrv.block = state_next.Try;
            ret_lrv.block.Statements.RemoveAt(i_assign);
            return(true);
        }
        //===========================================================
        //		構文合致
        //===========================================================
        /// <summary>
        ///
        /// </summary>
        /// <param name="var_deleted">削除される予定の変数</param>
        /// <param name="next_state">
        /// 本当に削除命令なのかを確認する為に、次の文を指定。
        /// if(disposable!=nullptr)disposable->Dispose(); なら OK
        /// </param>
        /// <returns>delete だったら true。そうでなかったら、false。</returns>
        private static bool DetectDeleteStatement(string disposable_name, IStatement next_state)
        {
            //---------------------------------------------------
            //		if(disposable==nullptr)
            //---------------------------------------------------
            // if
            IConditionStatement cond = next_state as IConditionStatement;

            if (cond == null)
            {
                return(false);
            }

            // ==
            IBinaryExpression exp_bin = cond.Condition as IBinaryExpression;

            if (exp_bin == null || exp_bin.Operator != BinaryOperator.IdentityInequality)
            {
                return(false);
            }

            // disposable
            IVariableReferenceExpression exp_var = exp_bin.Left as IVariableReferenceExpression;

            if (exp_var == null || exp_var.Variable.Resolve().Name != disposable_name)
            {
                return(false);
            }

            // nullptr
            ILiteralExpression exp_lit = exp_bin.Right as ILiteralExpression;

            if (exp_lit == null || exp_lit.Value != null)
            {
                return(false);
            }

            //---------------------------------------------------
            //		disposable->Dispose();
            //---------------------------------------------------
            // 単文
            IStatementCollection states_then = cond.Then.Statements;

            if (states_then == null || states_then.Count != 1)
            {
                return(false);
            }

            IExpressionStatement state_exp = states_then[0] as IExpressionStatement;

            if (state_exp == null)
            {
                return(false);
            }

            // **->**();
            IMethodInvokeExpression exp_inv = state_exp.Expression as IMethodInvokeExpression;

            if (exp_inv == null || exp_inv.Arguments.Count != 0)
            {
                return(false);
            }

            // **->Dispose();
            IMethodReferenceExpression ref_dispose = exp_inv.Method as IMethodReferenceExpression;

            if (ref_dispose == null || ref_dispose.Method.Name != "Dispose")
            {
                return(false);
            }

            // disposable->Dispose();
            exp_var = ref_dispose.Target as IVariableReferenceExpression;
            if (exp_var == null || exp_var.Variable.Resolve().Name != disposable_name)
            {
                return(false);
            }

            //---------------------------------------------------
            //		delete disposable
            //---------------------------------------------------
            return(true);
        }
 private void InsituTransformStatementCollection(IStatementCollection value)
 {
     int index = 0;
     while (index < value.Count)
     {
         IStatement statement = this.TransformStatement(value[index]);
         if (statement != null)
         {
             value[index++] = statement;
         }
         else
         {
             value.RemoveAt(index);
         }
     }
 }
Example #9
0
 public Account(IAmount amount, IStatementCollection statementCollection)
 {
     Balance             = amount ?? throw new ArgumentNullException(nameof(amount));
     StatementCollection = statementCollection ?? throw new ArgumentNullException(nameof(statementCollection));
 }
Example #10
0
 public static bool Compare(this IStatementCollection source, IStatementCollection n, Func<IStatement, IStatement, bool> checkitem)
 {
     return Compare<IStatement>(source,n,checkitem);
 }
 // write a list of variable definitions by recursing through the statements and define
 //  the corresponding variable names
 private void WriteVariableList(IStatementCollection statements, IFormatter formatter, ref bool hasvar)
 {
     foreach (IStatement statement in statements)
         WriteVariableList(statement, formatter, ref hasvar);
 }
 private void WriteStatementList(IStatementCollection statements, IFormatter formatter)
 {
     this.firstStmt = true;
     // put Delphi Loop detection here for now
     //			DetectDelphiIterationStatement1(statements);
     //			DetectDelphiIterationStatement2(statements);
     //
     for (int i = 0; i < statements.Count; i++)
     {
         this.WriteStatement(statements[i], formatter, (i == statements.Count - 1));
     }
 }
Example #13
0
        private static void WriteTryCatchFinally_catch(LanguageWriter w, ICatchClause clause)
        {
            w.Write(" ");
            w.WriteKeyword("catch");

            // 特別な場合 catch(...) を検出
            // → clauseBody を改変
            //    clauseBody は StatementAnalyze の時点で改変する様にした
            bool catchAll = clause.Condition is StatementAnalyze.IsCatchAll;
            IStatementCollection clause_body = clause.Body == null?null:clause.Body.Statements;

            TypeRef var_type = new TypeRef(clause.Variable.VariableType);

            /*
             * if(var_type.IsObject&&clause.Condition!=null) {
             *      ISnippetExpression iSnippent=clause.Condition as ISnippetExpression;
             *      if(iSnippent!=null&&iSnippent.Value=="?"&&StatementAnalyze.IsCatchAllClause(ref clause_body)){
             *              catchAll=true;
             *      }
             * }
             * //*/

            if (catchAll)
            {
                w.Write(" (...)");
            }
            else
            {
                if (clause.Variable.Name.Length != 0 || !var_type.IsObject)
                {
                    w.scope.RegisterVariable(clause.Variable, false);
                    w.Write(" (");
                    var_type.WriteNameWithRef(w);
                    w.Write(" ");
                    w.WriteDeclaration(clause.Variable.Name);
                    w.Write(")");
                }

                if (clause.Condition != null)
                {
                    w.Write(" ");
                    w.WriteKeyword("when");
                    w.Write(" ");
                    w.Write("(");
                    w.WriteExpression(clause.Condition);
                    w.Write(")");
                }
            }

            w.Write(" {");
            w.WriteLine();
            w.WriteIndent();

            if (clause_body != null)
            {
                for (int num = 0; num < clause_body.Count; num++)
                {
                    IStatement statement3 = clause_body[num];

#if EXTRA_TEMP
#warning catch: "コンストラクタの中で、最後の一行で、throw" の場合は書き込まない?
                    // →之は、恐らく、 constructor に function-try-statement を適用した時の話である
                    // よって無視する
                    if (w.SomeConstructor && num + 1 >= clause_body.Count && statement3 is IThrowExceptionStatement)
                    {
                        break;
                    }
#endif

                    WriteStatement(w, statement3);
                }
            }

            w.WriteOutdent();
            w.Write("}");
        }
Example #14
0
        private static void WriteTryCatchFinally_try(LanguageWriter w, IBlockStatement _try)         //,int skipCount
        {
#if FUNC_TRY
            if (skipCount == 0)
            {
#endif
            w.WriteKeyword("try");
            w.Write(" {");
            w.WriteLine();
            w.WriteIndent();
#if FUNC_TRY
        }
#endif
            //
            //	try の中身
            //
            if (_try != null)
            {
#if EXTRA_TEMP
                IStatementCollection statementCollection = _try.Statements;
                int i = 0;
                foreach (IStatement state in _try.Statements)
                {
#warning try: 何故スキップが入るのか?
                    // →コンストラクタ内で  を全て delegation したから。
                    // ・delegation の検索は、最初に delegation が適用出来ない物が出た時に終了する
                    //   但し、その際に、ローカル変数 T var=xxx は無視している?
                    // ・最後の delegation が skipCount
                    if (i < skipCount)
                    {
                        i++;

                        IExpressionStatement state_exp = state as IExpressionStatement;
                        if (state_exp == null)
                        {
                            goto write;
                        }

                        IAssignExpression exp_assign = state_exp.Expression as IAssignExpression;
                        if (exp_assign != null)
                        {
                            // this 以外の物の field もスキップされてしまう...
                            if (exp_assign.Target is IFieldReferenceExpression)
                            {
                                continue;
                            }
                            goto write;
                        }

                        IMethodInvokeExpression exp_inv = state_exp.Expression as IMethodInvokeExpression;
                        if (exp_inv != null)
                        {
                            IMethodReferenceExpression method = exp_inv.Method as IMethodReferenceExpression;
                            if (method != null && method.Target is IBaseReferenceExpression)
                            {
                                continue;
                            }
                            goto write;
                        }
                    }
write:
                    WriteStatement(w, state);
                }
#else
                WriteBlock(w, _try);
#endif
            }
            w.WriteOutdent();
            w.Write("}");
        }
 public virtual void VisitStatementCollection(IStatementCollection value)
 {
     for (int i = 0; i < value.Count; i++)
     {
         this.VisitStatement(value[i]);
     }
 }
Example #16
0
 public CBlockStatement(IStatementCollection states)
 {
     this.c = states;
 }
Example #17
0
        public static bool IsCatchAllClause(ref IStatementCollection statements)
        {
            if (statements == null || statements.Count != 3)
            {
                return(false);
            }

            /* uint num1=0;*/ {
                IAssignExpression exp_assign = GetAssignExpression(statements[0]);
                if (exp_assign == null || null == GetVariableName(exp_assign.Target, "System", "UInt32"))
                {
                    return(false);
                }
                ILiteralExpression exp_lit = exp_assign.Expression as ILiteralExpression;
                if (exp_lit == null || (uint)exp_lit.Value != 0u)
                {
                    return(false);
                }
            }

            // 以下の二通りの場合がある (他にもあるかも知れない)
            // (1) int num2=::__CxxRegisterExceptionObject((void*)Marshal::GetExceptionPointers(),(void*)num0);
            // (2) ::__CxxRegisterExceptionObject((void*)Marshal::GetExceptionPointers(),(void*)num0);
            // 更に __Cxx の部分は ___Cxx だったりもする
            {
                IExpressionStatement exp_state = statements[1] as IExpressionStatement;
                if (exp_state == null)
                {
                    return(false);
                }

                IMethodInvokeExpression exp_inv;
                IAssignExpression       exp_assign = exp_state.Expression as IAssignExpression;
                if (exp_assign != null)
                {
                    // (1) に挑戦
                    if (null == GetVariableName(exp_assign.Target, "System", "Int32"))
                    {
                        return(false);
                    }
                    exp_inv = exp_assign.Expression as IMethodInvokeExpression;
                }
                else
                {
                    // (2) に挑戦
                    exp_inv = exp_state.Expression as IMethodInvokeExpression;
                }

                if (exp_inv == null || exp_inv.Arguments.Count != 2)
                {
                    return(false);
                }
                IMethodReferenceExpression exp_mref = exp_inv.Method as IMethodReferenceExpression;
                if (exp_mref == null || exp_mref.Method == null || exp_mref.Method.Name.TrimStart('_') != "CxxRegisterExceptionObject")
                {
                    return(false);
                }
            }

            //	try{
            //		try{
            //		}catch when{}
            //		break;
            //		if(num!=0u)throw;
            //	}finally{
            //		::__CxxUnregisterExceptionObject(esp0,num);
            //	}
            ITryCatchFinallyStatement state_tcf = statements[2] as ITryCatchFinallyStatement;
            {
                // try{
                if (state_tcf == null ||
                    state_tcf.Try == null || state_tcf.Try.Statements.Count != 3 ||
                    state_tcf.Finally == null || state_tcf.Finally.Statements.Count != 1
                    )
                {
                    return(false);
                }

                //     try{
                //	       <statements>
                //	   }catch when{}
                ITryCatchFinallyStatement state_tcf2 = state_tcf.Try.Statements[0] as ITryCatchFinallyStatement;
                if (state_tcf2 == null || state_tcf2.Try == null)
                {
                    return(false);
                }

                //     <break;goto;continue;return; 等>

                //     if(**!=**)throw;
                IConditionStatement state_cond = state_tcf.Try.Statements[state_tcf.Try.Statements.Count - 1] as IConditionStatement;
                if (state_cond == null || state_cond.Then.Statements.Count != 1)
                {
                    return(false);
                }
                IBinaryExpression exp_bin = state_cond.Condition as IBinaryExpression;
                if (exp_bin == null || exp_bin.Operator != BinaryOperator.ValueInequality)
                {
                    return(false);
                }
                IThrowExceptionStatement state_exc = state_cond.Then.Statements[0] as IThrowExceptionStatement;
                if (state_exc == null)
                {
                    return(false);
                }

                // } finally {
                //     ::__CxxUnregisterExceptionObject((void*)num2,(int)num);
                // }
                IMethodInvokeExpression exp_inv = GetInvokeExpression(state_tcf.Finally.Statements[0]);
                if (exp_inv == null || exp_inv.Arguments.Count != 2)
                {
                    return(false);
                }
                IMethodReferenceExpression exp_mref = exp_inv.Method as IMethodReferenceExpression;
                if (exp_mref == null || exp_mref.Method == null || exp_mref.Method.Name.TrimStart('_') != "CxxUnregisterExceptionObject")
                {
                    return(false);
                }

                // <statements>
                statements = state_tcf2.Try.Statements;
                return(true);
            }
        }
Example #18
0
 public static bool Compare(this IStatementCollection source, IStatementCollection n)
 {
     return(Compare <IStatement>(source, n));
 }
Example #19
0
 public static bool Compare(this IStatementCollection source, IStatementCollection n)
 {
     return Compare<IStatement>(source,n);
 }
Example #20
0
 public static bool Compare(this IStatementCollection source, IStatementCollection n, Func <IStatement, IStatement, bool> checkitem)
 {
     return(Compare <IStatement>(source, n, checkitem));
 }
Example #21
0
 public static bool Compare(this IStatementCollection source, IStatementCollection n, Func<IStatement, IStatement, Action<string, string>, bool> checkitem, Action<string, string> errAct)
 {
     return Compare<IStatement>(source,n,checkitem,errAct);
 }
Example #22
0
 public static bool Compare(this IStatementCollection source, IStatementCollection n, Func <IStatement, IStatement, Action <string, string>, bool> checkitem, Action <string, string> errAct)
 {
     return(Compare <IStatement>(source, n, checkitem, errAct));
 }
Example #23
0
        public static Gen::IEnumerable <IStatement> ModifyStatements(IStatementCollection collection)
        {
#if YIELD_LIST
            Gen::List <IStatement> yields = new System.Collections.Generic.List <IStatement>();
#endif

            for (int i = 0, iM = collection.Count; i < iM; i++)
            {
                IStatement s = collection[i];

                ILabeledStatement state_labeled = s as ILabeledStatement;
                if (state_labeled != null)
                {
#if YIELD_LIST
                    yields.Add(new LabelStatement(state_labeled.Name));
#else
                    yield return(new LabelStatement(state_labeled.Name));
#endif
                    s = state_labeled.Statement;
                    if (s == null)
                    {
                        continue;
                    }
                    goto next;
                }

                //
                //	TryCatch から
                //
                ITryCatchFinallyStatement state_tcf = s as ITryCatchFinallyStatement;
                if (state_tcf != null)
                {
                    ModifyCatchClauses(state_tcf);
                    goto next;
                }

                //
                //	IUsingStatement → LocalRefVariableStatement 書き込み
                //
                IUsingStatement state_using = s as IUsingStatement;
                if (state_using != null)
                {
                    TransformUsingStatement(yields, state_using, i + 1 == iM);
                    continue;
                }

                //
                //	代入部分から構文合致を始める場合
                //
                IAssignExpression exp_assign = GetAssignExpression(s);
                if (exp_assign != null)
                {
                    IVariableDeclaration var = GetVariable(exp_assign.Target);
                    if (var == null)
                    {
                        goto next;
                    }
                    TypeRef type = new TypeRef(var.VariableType);

                    //
                    // Detect 'default construction of value class'
                    //
                    //-----------------------------------------------
                    // Value value; // default constructor is called
                    //-----------------------------------------------

                    /*
                     * if(type.IsValueType){
                     *      IObjectCreateExpression exp_create=exp_assign.Expression as IObjectCreateExpression;
                     *      if(exp_create!=null&&exp_create.Constructor==null){
                     #if YIELD_LIST
                     *              yields.Add(new DefaultConstructionStatement(exp_create.Type,var.Name));
                     #else
                     *              yield return new DefaultConstructionStatement(exp_create.Type,var.Name);
                     #endif
                     *              continue;
                     *      }
                     * }
                     * //*/

                    //
                    // Detect 'delete'
                    //
                    //-----------------------------------------------
                    // IDisposable^ disposable= <expression> ;
                    // if(disposable!=nullptr)disposable->Dispose();
                    //-----------------------------------------------
                    if (i + 1 < iM && type.IsType("System", "IDisposable") && DetectDeleteStatement(var.Name, collection[i + 1]))
                    {
                        i++;
#if YIELD_LIST
                        yields.Add(new DeleteStatement(exp_assign.Expression));
#else
                        yield return(new DeleteStatement(exp_assign.Expression));
#endif
                        continue;
                    }

                    //
                    // Detect 'local ref value instance'
                    //
                    //-----------------------------------------------
                    // Class value= <expression> ;
                    // try{
                    //   disposable=value;
                    //   ...
                    // }...
                    // disposable->Dispose();
                    //-----------------------------------------------
                    LocalRefVariableStatement yret_lrv;
                    if (i + 2 < iM && DetectLocalRefVariable(var.Name, collection[i + 1], collection[i + 2], out yret_lrv))
                    {
                        i += 2;
                        yret_lrv.var_type = var.VariableType;
                        yret_lrv.exp      = exp_assign.Expression;
#if YIELD_LIST
#warning local-ref: 更に上層でも削除しなければならない可能性がある
                        RemoveNullDeclaration(yret_lrv.var_name, yields);
                        yields.Add(yret_lrv);
#else
                        yield return(yret_lrv);
#endif
                        continue;
                    }

                    goto next;
                }
next:
#if YIELD_LIST
                yields.Add(s);
#else
                yield return(s);
#endif
            }

#if YIELD_LIST
            return(yields);
#endif
        }
        public virtual IStatementCollection TransformStatementCollection(IStatementCollection value)
        {
            int count = value.Count;
            IStatement[] array = new IStatement[count];
            for (int i = 0; i < count; i++)
            {
                array[i] = this.TransformStatement(value[i]);
            }

            IStatementCollection target = new StatementCollection();
            target.AddRange(array);
            return target;
        }