private static void WriteDo(LanguageWriter w, IDoStatement state)
 {
     w.WriteKeyword("do");
     w.PushScope();
     w.Write(" {");
     w.WriteLine();
     w.WriteIndent();
     if (state.Body != null)
     {
         WriteBlock(w, state.Body);
     }
     w.WriteOutdent();
     w.Write("} ");
     w.PopScope();
     w.WriteKeyword("while");
     w.Write("(");
     if (state.Condition != null)
     {
         w.SkipWriteLine = true;
         ExpressionWriter.WriteExpression(w, state.Condition, false);
         w.SkipWriteLine = false;
     }
     w.Write(");");
     w.WriteLine();
 }
 private static void WriteMethodInvoke(LanguageWriter w, IMethodInvokeExpression exp)
 {
     WriteExpression(w, exp.Method, PREC_MEM_ACCESS > GetExpressionPrecedence(exp.Method));
     w.Write("(");
     w.WriteExpressionCollection(exp.Arguments);
     w.Write(")");
 }
 private static void WriteArrayIndexerExpression(LanguageWriter w, IArrayIndexerExpression exp)
 {
     w.WriteExpression(exp.Target);
     w.Write("[");
     w.WriteExpressionCollection(exp.Indices);
     w.Write("]");
 }
 private static void WriteRemoveEvent(LanguageWriter w, IRemoveEventStatement state)
 {
     ExpressionWriter.WriteEventReference(w, state.Event);
     w.Write(" -= ");
     ExpressionWriter.WriteExpression(w, state.Listener, false);
     w.Write(";");
     w.WriteLine();
 }
 private static void WriteCondition(LanguageWriter w, IConditionExpression exp)
 {
     WriteExpression(w, exp.Condition, PREC_COND > GetExpressionPrecedence(exp.Condition));
     w.Write(" ? ");
     WriteExpression(w, exp.Then, false);
     w.Write(" : ");
     WriteExpression(w, exp.Else, false);
 }
 private static void WriteTryCast(LanguageWriter w, ITryCastExpression exp)
 {
     w.WriteKeyword("dynamic_cast");
     w.Write("<");
     new TypeRef(exp.TargetType).WriteNameWithRef(w);
     w.Write(">(");
     WriteExpression(w, exp.Expression, false);
     w.Write(")");
 }
 private static void WriteDefaultConstruction(LanguageWriter w, DefaultConstructionStatement state)
 {
     new TypeRef(state.var_type).WriteName(w);
     w.Write(" ");
     w.WriteDeclaration(state.var_name);
     w.Write("; ");
     w.WriteComment("// 既定のコンストラクタ");
     w.WriteLine();
 }
        private static void WriteBinary(LanguageWriter w, IBinaryExpression exp)
        {
            WriteExpression(w, exp.Left, prec_binop[exp.Operator] > GetExpressionPrecedence(exp.Left));

            w.Write(" ");
            WriteBinaryOperator(w, exp.Operator);
            w.Write(" ");

            WriteExpression(w, exp.Right, prec_binop[exp.Operator] >= GetExpressionPrecedence(exp.Right));
        }
        private static void WriteMemberAccess(LanguageWriter w, IExpression target)
        {
            if (target == null)
            {
                return;
            }

            bool           isref = true;
            ExpressionType xtype = GetExpressionType(target);
            TypeRef        type;

            switch (xtype)
            {
            case ExpressionType.TypeReference:
                WriteTypeReference(w, (ITypeReferenceExpression)target);
                w.Write("::");
                return;

            case ExpressionType.BaseReference:
                w.baseType.WriteName(w);
                w.Write("::");
                return;

            case ExpressionType.VariableReference:
                IVariableReference var_ref = ((IVariableReferenceExpression)target).Variable;
                if (var_ref == null)
                {
                    break;
                }
                IVariableDeclaration var_decl = var_ref.Resolve();
                if (var_decl == null)
                {
                    break;
                }
                if (w.scope[var_decl.Name].local_scope)
                {
                    isref = false;
                    break;
                }
                type = new TypeRef(var_decl.VariableType);
                goto fromtype;

            default:
                type = TypeRef.GetReturnType(target);
                goto fromtype;
fromtype:
//#warning Modifier や % や & が付いている場合に関しては未だ
                isref = type.IsEmpty || !type.IsValueType && !type.IsPrimitive;
                break;
            }


            WriteExpression(w, target, PREC_MEM_ACCESS > GetExpressionPrecedence(target));
            w.Write(isref?"->":".");
        }
        private static void WriteUnary(LanguageWriter w, IUnaryExpression exp)
        {
            bool   post = false;
            string opName;

            switch (exp.Operator)
            {
            case UnaryOperator.Negate:
                opName = "-";
                break;

            case UnaryOperator.BooleanNot:
                opName = "!";
                break;

            case UnaryOperator.BitwiseNot:
                opName = "~";
                break;

            case UnaryOperator.PreIncrement:
                opName = "++";
                break;

            case UnaryOperator.PreDecrement:
                opName = "--";
                break;

            case UnaryOperator.PostIncrement:
                opName = "++";
                post   = true;
                break;

            case UnaryOperator.PostDecrement:
                opName = "--";
                post   = true;
                break;

            default:
                throw new System.NotSupportedException(
                          "指定した種類の単項演算には対応していません: " + exp.Operator.ToString()
                          );
            }

            // 書込
            if (post)
            {
                WriteExpression(w, exp.Expression, PREC_POST > GetExpressionPrecedence(exp.Expression));
                w.Write(opName);
            }
            else
            {
                w.Write(opName);
                WriteExpression(w, exp.Expression, PREC_PRE > GetExpressionPrecedence(exp.Expression));
            }
        }
 private static void WriteThrowException(LanguageWriter w, IThrowExceptionStatement state)
 {
     w.WriteKeyword("throw");
     if (state.Expression != null)
     {
         w.Write(" ");
         ExpressionWriter.WriteExpression(w, state.Expression, false);
     }
     w.Write(";");
     w.WriteLine();
 }
 private static void WriteMethodReturn(LanguageWriter w, IMethodReturnStatement state)
 {
     w.WriteKeyword("return");
     if (state.Expression != null)
     {
         w.Write(" ");
         ExpressionWriter.WriteExpression(w, state.Expression, false);
     }
     w.Write(";");
     w.WriteLine();
 }
        public void Write(LanguageWriter w)
        {
            w.PushScope();
            //---------------------------------------------
            if (!this.noblock)
            {
                w.Write("{");
                w.WriteLine();
                w.WriteIndent();
            }
            if (!w.SuppressOutput)
            {
                //*
#warning local-ref: 上層で無駄な宣言を取り除くべき
                if (w.scope.ContainsVariable(this.var_name))
                {
                    w.scope[this.var_name].local_scope = true;
                }
                else
                {
                    w.scope.RegisterVariable(this.var_name, true);
                }
                //*/
            }

            bool nohandle = this.exp is IObjectCreateExpression;           // ハンドル表記でなくても良いかどうか

            this.WriteVariableDecl(w, nohandle);

            // 後に続く内容
            w.WriteLine();
            StatementWriter.WriteBlock(w, this.block);

            // ハンドル表記で宣言した場合はちゃんと delete
            if (!nohandle)
            {
                StatementWriter.WriteStatement(w, new DeleteStatement(new VariableReferenceExpression(this.decl)));
            }

            if (!this.noblock)
            {
                w.WriteOutdent();
                w.Write("}");
                w.WriteLine();
            }
            if (this.labelname != null)
            {
                w.__WriteLabel(this.labelname);
                w.Write(";");
                w.WriteLine();
            }
            //---------------------------------------------
            w.PopScope();
        }
 private static void WriteCanCast(LanguageWriter w, ICanCastExpression exp)
 {
     w.WriteKeyword("dynamic_cast");
     w.Write("<");
     new TypeRef(exp.TargetType).WriteNameWithRef(w);
     w.Write(">");
     w.Write("(");
     WriteExpression(w, exp.Expression, false);
     w.Write(")");
     w.Write(" != ");
     w.WriteLiteral("nullptr");
 }
 private static void WriteMemoryInitialize(LanguageWriter w, IMemoryInitializeStatement state)
 {
     w.Write("::");
     w.WriteReference("memset", "Crt 関数 #include <memory.h>", null);
     w.Write("(");
     ExpressionWriter.WriteExpression(w, state.Offset, false);
     w.Write(", ");
     ExpressionWriter.WriteExpression(w, state.Value, false);
     w.Write(", ");
     ExpressionWriter.WriteExpression(w, state.Length, false);
     w.Write(");");
     w.WriteLine();
 }
 private static void WriteMemoryCopy(LanguageWriter w, IMemoryCopyStatement state)
 {
     w.Write("::");
     w.WriteReference("memcpy", "Crt 関数 #include <memory.h>", null);
     w.Write("(");
     ExpressionWriter.WriteExpression(w, state.Destination, false);
     w.Write(", ");
     ExpressionWriter.WriteExpression(w, state.Source, false);
     w.Write(", ");
     ExpressionWriter.WriteExpression(w, state.Length, false);
     w.Write(");");
     w.WriteLine();
 }
 private static void WriteDelegateCreate(LanguageWriter w, IDelegateCreateExpression exp)
 {
     w.WriteKeyword("gcnew");
     w.Write(" ");
     new TypeRef(exp.DelegateType).WriteName(w);
     w.Write("(");
     WriteExpression(w, exp.Target, false);
     w.Write(", &");
     WriteTypeReference(w, (ITypeReference)exp.Method.DeclaringType);
     w.Write("::");
     w.WriteMethodReference(exp.Method);
     w.Write(")");
 }
        private static void WriteBracedStatement(LanguageWriter w, IStatement statement)
        {
            w.PushScope();

            w.Write("{");
            w.WriteLine();
            w.WriteIndent();
            WriteStatement(w, statement);
            w.WriteOutdent();
            w.Write("}");

            w.PopScope();
        }
        private static void WriteCast(LanguageWriter w, ICastExpression exp)
        {
            TypeRef type  = new TypeRef(exp.TargetType);
            bool    paren = !type.IsFunctionPointer;

            if (paren)
            {
                w.Write("(");
            }
            type.WriteNameWithRef(w);
            if (paren)
            {
                w.Write(")");
            }

            WriteExpression(w, exp.Expression, PREC_CAST > GetExpressionPrecedence(exp.Expression));
        }
Exemple #20
0
        //===========================================================
        //		書込
        //===========================================================

        #region 型の名前の書込
        /// <summary>
        /// 型名を書き込みます。参照型の場合には ^ を後に続けます。値型の場合には何も付けません。
        /// </summary>
        /// <param name="w">書き込みに使用する LanguageWriter を指定します。</param>
        public void WriteNameWithRef(LanguageWriter w)
        {
            this.WriteName(w);
            if (IsRefType)
            {
                w.Write("^");
            }
        }
        internal static void WriteVariableDeclaration(LanguageWriter w, IVariableDeclaration var_decl)
        {
            // local 変数の登録
            w.scope.RegisterVariable(var_decl, false);

#warning pinned の情報なども入れる様にする
            new TypeRef(var_decl.VariableType).WriteNameWithRef(w);
            w.Write(" ");
            w.WriteDeclaration(w.scope[var_decl.Name].disp_name);
        }
        private static void WriteFixed(LanguageWriter w, IFixedStatement state)
        {
            w.Write("{");
            w.WriteIndent();
            w.WriteLine();

            w.scope.RegisterVariable(state.Variable, false);
            w.WriteKeyword("pin_ptr");
            w.Write("<");
            new TypeRef(((IPointerType)state.Variable.VariableType).ElementType).WriteNameWithRef(w);
            w.Write("> ");
            w.WriteDeclaration(state.Variable.Name);
            w.Write(" = ");
            ExpressionWriter.WriteExpression(w, state.Expression, false);

            WriteBlock(w, state.Body);
            w.WriteOutdent();
            w.Write("}");
        }
        private static void WriteBinaryNullCoalescing(LanguageWriter w, INullCoalescingExpression exp)
        {
            const int PREC_NULL_COAL = 0;

            WriteExpression(w, exp.Condition, PREC_NULL_COAL > GetExpressionPrecedence(exp.Condition));

            w.Write(" ?? ");

            WriteExpression(w, exp.Expression, PREC_NULL_COAL >= GetExpressionPrecedence(exp.Expression));
        }
        private static void WriteCondition(LanguageWriter w, IConditionStatement state)
        {
#if EXTRA_TEMP
            using (NewBlock block3 = new NewBlock(w)) {
                w.WriteKeyword("if");
                w.Write(" ");
                w.Write("(");
                ExpressionWriter.WriteExpression(w, state.Condition, false);
                w.Write(") ");

                using (NewBlock block2 = new NewBlock(w)) {
                    WriteBracedStatement(w, state.Then);
                    if (!IsBlank(state.Else))
                    {
                        using (NewBlock block = new NewBlock(w)) {
                            w.Write(" ");
                            w.WriteKeyword("else");
                            w.Write(" ");
                            WriteBracedStatement(w, state.Else);
                        }
                    }
                    w.WriteLine();
                }
            }
#else
            w.WriteKeyword("if");
            w.Write(" ");
            w.Write("(");
            ExpressionWriter.WriteExpression(w, state.Condition, false);
            w.Write(") ");

            WriteBracedStatement(w, state.Then);
            if (!IsBlank(state.Else))
            {
                w.Write(" ");
                w.WriteKeyword("else");
                w.Write(" ");
                WriteBracedStatement(w, state.Else);
            }
            w.WriteLine();
#endif
        }
        private static void WriteSwitch_case(LanguageWriter w, IExpression con_case)
        {
            IBinaryExpression exp_bi = con_case as IBinaryExpression;

            if (exp_bi != null)
            {
                if (exp_bi.Operator == BinaryOperator.BooleanOr)
                {
                    WriteSwitch_case(w, exp_bi.Left);
                    WriteSwitch_case(w, exp_bi.Right);
                    return;
                }
            }

            w.WriteKeyword("case");
            w.Write(" ");
            ExpressionWriter.WriteExpression(w, con_case, false);
            w.Write(":");
            w.WriteLine();
        }
 private static void WriteSwitch(LanguageWriter w, ISwitchStatement state)
 {
     w.WriteKeyword("switch");
     w.Write(" (");
     ExpressionWriter.WriteExpression(w, state.Expression, false);
     w.Write(") {");
     w.WriteLine();
     w.WriteIndent();
     foreach (ISwitchCase _case in state.Cases)
     {
         IConditionCase case1 = _case as IConditionCase;
         if (case1 != null)
         {
             WriteSwitch_case(w, case1.Condition);
             w.WriteIndent();
             if (case1.Body != null)
             {
                 WriteStatement(w, case1.Body);
             }
             w.WriteOutdent();
         }
         IDefaultCase case2 = _case as IDefaultCase;
         if (case2 != null)
         {
             w.WriteKeyword("default");
             w.Write(":");
             w.WriteLine();
             w.WriteIndent();
             if (case2.Body != null)
             {
                 WriteStatement(w, case2.Body);
             }
             w.WriteOutdent();
             //this.Write("}");
             //this.WriteLine();
         }
     }
     w.WriteOutdent();
     w.Write("}");
     w.WriteLine();
 }
        private static void WriteExpression(LanguageWriter w, IExpressionStatement state)
        {
            w.WriteExpression(state.Expression);

            if (w.SkipWriteLine)
            {
                return;
            }

            w.Write(";");
            w.WriteLine();
        }
        private static void WriteLock(LanguageWriter w, ILockStatement statement)
        {
            w.Write("{");
            w.WriteIndent();
            w.WriteLine();

            Scope.VariableData data = new Scope.VariableData("<lock_statement>" + WriteLock_counter++.ToString(), true);
            data.disp_name     = "lock";
            w.scope[data.name] = data;

            // msclr::lock を初期化
            w.WriteReference("msclr", "", null);
            w.Write("::");
            w.WriteReference("lock", "#include <msclr/lock.h> で使用して下さい", null);
            w.Write(" ");
            w.WriteDeclaration(data.disp_name);
            w.Write("(");
            ExpressionWriter.WriteExpression(w, statement.Expression, false);
            w.Write(");");
            w.WriteLine();

            // 中身を書込
            if (statement.Body != null)
            {
                WriteBlock(w, statement.Body);
            }

            w.WriteOutdent();
            w.Write("}");
            w.WriteLine();
        }
        private static void WriteArrayCreate(LanguageWriter w, IArrayCreateExpression exp)
        {
            w.WriteKeyword("gcnew");
            w.Write(" ");
            w.WriteKeyword("array");
            w.Write("<");

            new TypeRef(exp.Type).WriteNameWithRef(w);
            if (exp.Dimensions != null && exp.Dimensions.Count > 1)
            {
                w.Write(", ");
                w.WriteAsLiteral(exp.Dimensions.Count);
            }
            w.Write(">");

            // 要素数の指定
            if (exp.Dimensions != null)
            {
                w.Write("(");
                w.WriteExpressionCollection(exp.Dimensions);
                w.Write(")");
            }

            // {a, b, ... 要素の指定}
            IBlockExpression initializer = exp.Initializer;

            if (initializer != null)
            {
                WriteBlock(w, initializer);
            }
        }
 private static void WriteWhile(LanguageWriter w, IWhileStatement state)
 {
     w.WriteKeyword("while");
     w.Write(" ");
     w.Write("(");
     if (state.Condition != null)
     {
         w.SkipWriteLine = true;
         ExpressionWriter.WriteExpression(w, state.Condition, false);
         w.SkipWriteLine = false;
     }
     w.Write(") {");
     w.WriteLine();
     w.WriteIndent();
     if (state.Body != null)
     {
         WriteStatement(w, state.Body);
     }
     w.WriteOutdent();
     w.Write("}");
     w.WriteLine();
 }