예제 #1
0
        public override void WriteTo(CCodeWriterBase c)
        {
            c.TextSpanNewLine("inline static GC_descr __get_type_descriptor()");
            c.OpenBlock();
            c.TextSpan("typedef");
            c.WhiteSpace();
            c.WriteType(this.Type, true, true, true);
            c.WhiteSpace();
            c.TextSpanNewLine("__type;");
            c.TextSpanNewLine("GC_word bitmap[GC_BITMAP_SIZE(__type)] = {0};");

            // set fields
            foreach (var field in this.Type.EnumPossibleReferenceFields())
            {
                c.TextSpan("GC_set_bit(bitmap, GC_WORD_OFFSET(__type,");
                c.WhiteSpace();
                // TODO: fix it, replace with "base" type as generic types causing issues
                ////c.WriteFieldAccessAsStaticField(field);
                c.WriteName(field);
                c.TextSpanNewLine("));");
            }

            c.TextSpanNewLine("return GC_make_descriptor(bitmap, GC_WORD_LEN(__type));");

            c.EndBlock();
            c.Separate();
        }
예제 #2
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            c.TextSpan("if");
            c.WhiteSpace();
            c.TextSpan("(");

            if (!this.JumpIfTrue)
            {
                c.TextSpan("!");
                c.WriteWrappedExpressionIfNeeded(this.Condition);
            }
            else
            {
                this.Condition.WriteTo(c);
            }

            c.TextSpan(")");

            c.NewLine();
            c.OpenBlock();

            new GotoStatement {
                Label = this.Label
            }.WriteTo(c);

            c.EndBlock();

            c.Separate();
        }
예제 #3
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            if (this.locals.Count > 0)
            {
                c.OpenBlock();

                foreach (var statement in this.locals)

                {
                    statement.WriteTo(c);
                }
            }

            c.TextSpan("for");
            c.WhiteSpace();
            c.TextSpan("(");

            var block = this.Initialization as Block;

            if (block != null)
            {
                var any = false;
                foreach (var initializationItem in block.Statements)
                {
                    if (any)
                    {
                        c.TextSpan(",");
                        c.WhiteSpace();
                    }

                    PrintStatementAsExpression(c, initializationItem);
                    any = true;
                }
            }
            else
            {
                PrintStatementAsExpression(c, this.Initialization);
            }

            c.TextSpan(";");
            c.WhiteSpace();

            this.Condition.WriteTo(c);

            c.TextSpan(";");
            c.WhiteSpace();

            PrintStatementAsExpression(c, this.Incrementing);

            c.TextSpan(")");

            c.NewLine();
            base.WriteTo(c);

            if (this.locals.Count > 0)
            {
                c.EndBlock();
            }
        }
예제 #4
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            CCodeWriterBase.SetLocalObjectIDGenerator();

            // get actual statements
            var statements = Statements;

            if (statements.Count == 1 && statements.First().Kind == Kinds.BlockStatement)
            {
                var blockStatement = statements.First() as BlockStatement;
                if (blockStatement != null)
                {
                    var block = blockStatement.Statements as Block;
                    if (block != null)
                    {
                        statements = block.Statements;
                    }
                }
            }

            var extraLocalDecls = this.SanitizeCode(statements);

            var skip = 0;

            ////if (this.MethodSymbol.MethodKind == MethodKind.Constructor)
            ////{
            ////    skip = ConstructorInitializer(c, statements);
            ////}

            c.NewLine();
            c.OpenBlock();

            foreach (var localDecl in extraLocalDecls)
            {
                var loadState = localDecl.Suppressed;
                localDecl.Suppressed = false;
                localDecl.WriteTo(c);
                localDecl.Suppressed = loadState;
            }

            if (MethodSymbol.MethodKind == MethodKind.StaticConstructor)
            {
                c.TextSpanNewLine("_cctor_being_called = true;");
            }

            foreach (var statement in statements.Skip(skip))
            {
                if (MethodSymbol.MethodKind == MethodKind.StaticConstructor && statement.Kind == Kinds.ReturnStatement)
                {
                    c.TextSpanNewLine("_cctor_called = true;");
                    c.TextSpanNewLine("_cctor_being_called = false;");
                }

                statement.WriteTo(c);
            }

            c.EndBlock();
        }
예제 #5
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            if (this.FinallyBlockOpt != null)
            {
                var block = this.FinallyBlockOpt as Block;
                if (block != null)
                {
                    block.SuppressNewLineAtEnd = true;
                }

                c.OpenBlock();
                c.TextSpan("Finally");
                c.WhiteSpace();
                c.TextSpan("__finally_block");
                c.TextSpan("(");
                new LambdaExpression()
                {
                    Statements = block
                }.WriteTo(c);
                c.TextSpan(");");
                c.NewLine();
            }

            if (this.catchBlocks.Any())
            {
                c.TextSpan("try");
            }

            c.NewLine();

            c.WriteBlockOrStatementsAsBlock(this.TryBlock);

            foreach (var catchBlock in this.catchBlocks)
            {
                catchBlock.WriteTo(c);
            }

            if (this.FinallyBlockOpt != null)
            {
                c.EndBlock();
            }

            c.Separate();
        }
예제 #6
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            if (this.Locals.Count > 0)
            {
                c.OpenBlock();

                foreach (var statement in this.Locals)
                {
                    statement.WriteTo(c);
                }
            }

            var block = this.Initialization as Block;

            if (block != null)
            {
                var any = false;
                foreach (var initializationItem in block.Statements)
                {
                    if (any)
                    {
                        c.TextSpan(",");
                        c.WhiteSpace();
                    }

                    PrintStatementAsExpression(c, initializationItem);
                    any = true;
                }
            }
            else
            {
                this.Initialization.WriteTo(c);
            }

            this.TryStatement.WriteTo(c);

            if (this.Locals.Count > 0)
            {
                c.EndBlock();

                // No normal ending of Statement as we do not need extra ;
                c.Separate();
            }
        }
예제 #7
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            CCodeWriterBase.SetLocalObjectIDGenerator();

            // get actual statements
            var statements = Statements;

            if (statements.Count == 1 && statements.First().Kind == Kinds.BlockStatement)
            {
                var blockStatement = statements.First() as BlockStatement;
                if (blockStatement != null)
                {
                    var block = blockStatement.Statements as Block;
                    if (block != null)
                    {
                        statements = block.Statements;
                    }
                }
            }

            this.SanitizeCaseLabelsAndSetReturnTypes(statements);

            var skip = 0;

            ////if (this.MethodSymbol.MethodKind == MethodKind.Constructor)
            ////{
            ////    skip = ConstructorInitializer(c, statements);
            ////}

            c.NewLine();
            c.OpenBlock();

            if (this.MethodSymbol.MethodKind == MethodKind.StaticConstructor)
            {
                c.TextSpanNewLine("_cctor_called = true;");
            }

            foreach (var statement in statements.Skip(skip))
            {
                statement.WriteTo(c);
            }

            c.EndBlock();
        }
예제 #8
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            if (this.Sequence)
            {
                var any = false;
                foreach (var statement in this._statements)
                {
                    if (any)
                    {
                        c.TextSpan(",");
                        c.WhiteSpace();
                    }

                    statement.SuppressEnding = true;
                    statement.WriteTo(c);
                    any = true;
                }

                return;
            }

            if (!this.NoParenthesis)
            {
                c.OpenBlock();
            }

            foreach (var statement in this._statements)
            {
                statement.WriteTo(c);
            }

            if (!this.NoParenthesis)
            {
                if (this.SuppressNewLineAtEnd)
                {
                    c.EndBlockWithoutNewLine();
                }
                else
                {
                    c.EndBlock();
                }
            }
        }
예제 #9
0
        public override void WriteTo(CCodeWriterBase c)
        {
            c.TextSpan("class");
            c.WhiteSpace();
            c.WriteTypeName((INamedTypeSymbol)this.Type);
            c.OpenBlock();

            c.DecrementIndent();
            c.TextSpanNewLine("public:");
            c.IncrementIndent();

            foreach (var declarations in this.Declarations)
            {
                declarations.WriteTo(c);
            }

            c.EndBlock();
            c.Separate();
        }
예제 #10
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            if (this.locals.Count > 0)
            {
                c.OpenBlock();

                foreach (var statement in this.locals)
                {
                    statement.WriteTo(c);
                }
            }

            c.TextSpan("for");
            c.WhiteSpace();
            c.TextSpan("(");

            PrintStatementAsExpression(c, this.InitializationOpt);

            c.TextSpan(";");
            c.WhiteSpace();

            if (this.ConditionOpt != null)
            {
                this.ConditionOpt.WriteTo(c);
            }

            c.TextSpan(";");
            c.WhiteSpace();

            PrintStatementAsExpression(c, this.IncrementingOpt);

            c.TextSpan(")");

            c.NewLine();
            base.WriteTo(c);

            if (this.locals.Count > 0)
            {
                c.EndBlock();
            }
        }
예제 #11
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            Local localCase = null;

            if (this.stringEquality != null)
            {
                c.OpenBlock();

                var localImpl = new LocalImpl {
                    Name = "__SwitchExpression", Type = this.expression.Type
                };
                var local = new Local {
                    LocalSymbol = localImpl
                };

                var localImplCase = new LocalImpl {
                    Name = "__SwitchCase", Type = new TypeImpl {
                        SpecialType = SpecialType.System_Int32
                    }
                };
                localCase = new Local {
                    LocalSymbol = localImplCase
                };

                new VariableDeclaration
                {
                    Statements =
                    {
                        new ExpressionStatement
                        {
                            Expression =
                                new AssignmentOperator
                            {
                                TypeDeclaration = true,
                                Type            = new TypeImpl {
                                    SpecialType = SpecialType.System_Int32
                                },
                                Left  = localCase,
                                Right = new Literal{
                                    Value = ConstantValue.Create(0)
                                }
                            }
                        }
                    }
                }.WriteTo(c);
                new VariableDeclaration
                {
                    Statements =
                    {
                        new ExpressionStatement
                        {
                            Expression =
                                new AssignmentOperator
                            {
                                TypeDeclaration = true,
                                Type            = new TypeImpl {
                                    SpecialType = SpecialType.System_String
                                },
                                Left  = local,
                                Right = this.expression
                            }
                        }
                    }
                }.WriteTo(c);

                // first if
                IfStatement first     = null;
                IfStatement last      = null;
                var         caseIndex = 0;

                foreach (var switchSection in this.switchCases)
                {
                    foreach (var label in switchSection.Labels)
                    {
                        if (label.Value == null)
                        {
                            // default case;
                            continue;
                        }

                        caseIndex++;

                        // compare
                        var callEqual = new Call()
                        {
                            Method = this.stringEquality
                        };
                        callEqual.Arguments.Add(local);
                        callEqual.Arguments.Add(new Literal {
                            Value = label.Value
                        });

                        // set value
                        var setExpr = new ExpressionStatement
                        {
                            Expression =
                                new AssignmentOperator
                            {
                                Left  = localCase,
                                Right = new Literal {
                                    Value = ConstantValue.Create(caseIndex)
                                }
                            }
                        };

                        var ifStatement = new IfStatement
                        {
                            Condition    = callEqual,
                            IfStatements = setExpr
                        };

                        first = first ?? ifStatement;
                        if (last != null)
                        {
                            last.ElseStatementsOpt = ifStatement;
                        }

                        last = ifStatement;

                        // remap case value
                        label.Value = ConstantValue.Create(caseIndex);
                    }
                }

                if (first != null)
                {
                    first.WriteTo(c);
                }

                c.Separate();
            }

            foreach (var statement in this.statements)
            {
                statement.WriteTo(c);
            }

            var generateDefaultLabel = this.isNullableType;

            if (this.isNullableType)
            {
                var nullCase = this.switchCases.FirstOrDefault(sc => sc.Labels.Any(l => l.Value != null && l.Value.IsNull));
                if (nullCase != null)
                {
                    generateDefaultLabel = false;
                }

                var nullCaseOrDefault = nullCase ?? this.switchCases.FirstOrDefault(sc => sc.Labels.Any(l => l.Value == null));
                if (nullCaseOrDefault != null)
                {
                    var effectiveExpression = this.expression;

                    var callExpression = this.expression as Call;
                    if (callExpression != null)
                    {
                        effectiveExpression = new UnaryOperator
                        {
                            OperatorKind = UnaryOperatorKind.LogicalNegation,
                            Operand      = new Call {
                                ReceiverOpt = callExpression.ReceiverOpt, Method = new MethodImpl {
                                    Name = "get_HasValue", Parameters = ImmutableArray <IParameterSymbol> .Empty
                                }
                            }
                        };
                    }

                    // we need to insert nullable goto;
                    var ifStatement = new IfStatement
                    {
                        Condition    = effectiveExpression,
                        IfStatements = new GotoStatement {
                            Label = nullCaseOrDefault.Labels.First(l => l.Value == null || l.Value.IsNull)
                        }
                    };

                    ifStatement.WriteTo(c);
                }
            }

            c.TextSpan("switch");
            c.WhiteSpace();
            c.TextSpan("(");
            if (this.stringEquality != null)
            {
                localCase.WriteTo(c);
            }
            else
            {
                this.expression.WriteTo(c);
            }

            c.TextSpan(")");
            c.NewLine();
            c.OpenBlock();
            foreach (var switchSection in this.switchCases)
            {
                if (!generateDefaultLabel && switchSection.Labels.Any(l => l.Value == null))
                {
                    switchSection.IsNullableType = false;
                }

                switchSection.WriteTo(c);
            }

            c.EndBlock();

            if (this.stringEquality != null)
            {
                c.EndBlock();
            }

            c.Separate();
        }
예제 #12
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            CCodeWriterBase.SetLocalObjectIDGenerator();

            // get actual statements
            var statements = Statements;

            if (statements.Count == 1 && statements.First().Kind == Kinds.BlockStatement)
            {
                var blockStatement = statements.First() as BlockStatement;
                if (blockStatement != null)
                {
                    var block = blockStatement.Statements as Block;
                    if (block != null)
                    {
                        statements = block.Statements;
                    }
                }
            }

            var extraLocalDecls = this.SanitizeCode(statements);

            var skip = 0;

            ////if (this.MethodSymbol.MethodKind == MethodKind.Constructor)
            ////{
            ////    skip = ConstructorInitializer(c, statements);
            ////}

            c.NewLine();
            c.OpenBlock();

            foreach (var localDecl in extraLocalDecls)
            {
                var loadState = localDecl.Suppressed;
                localDecl.Suppressed = false;
                localDecl.WriteTo(c);
                localDecl.Suppressed = loadState;
            }

            if (MethodSymbol.MethodKind == MethodKind.StaticConstructor)
            {
                c.TextSpanNewLine("_cctor_being_called = true;");
            }

            #region Virtual Generic methods support
            if (MethodSymbol.MethodKind == MethodKind.Constructor)
            {
                foreach (var typeParameter in MethodSymbol.ContainingType.GetTemplateParameters().Where(t => t.HasConstructorConstraint))
                {
                    c.TextSpanNewLine(string.Format("this->construct_{0} = construct_{0};", typeParameter.Name));
                }
            }
            #endregion

            foreach (var statement in statements.Skip(skip))
            {
                if (MethodSymbol.MethodKind == MethodKind.StaticConstructor && statement.Kind == Kinds.ReturnStatement)
                {
                    c.TextSpanNewLine("_cctor_called = true;");
                    c.TextSpanNewLine("_cctor_being_called = false;");
                }

                statement.WriteTo(c);
            }

            c.EndBlock();
        }
예제 #13
0
        internal override void WriteTo(CCodeWriterBase c)
        {
            Local localCase = null;

            if (this.stringEquality != null)
            {
                c.OpenBlock();

                var localImpl = new LocalImpl {
                    Name = "__SwitchExpression", Type = this.expression.Type
                };
                var local = new Local {
                    LocalSymbol = localImpl
                };

                var localImplCase = new LocalImpl {
                    Name = "__SwitchCase", Type = new TypeImpl {
                        SpecialType = SpecialType.System_Int32
                    }
                };
                localCase = new Local {
                    LocalSymbol = localImplCase
                };

                new VariableDeclaration {
                    Local = localCase
                }.WriteTo(c);
                new VariableDeclaration {
                    Local = local
                }.WriteTo(c);

                // first if
                IfStatement first     = null;
                IfStatement last      = null;
                var         caseIndex = 0;

                foreach (var switchSection in this.switchCases)
                {
                    foreach (var label in switchSection.Labels)
                    {
                        if (label.Value == null)
                        {
                            // default case;
                            continue;
                        }

                        caseIndex++;

                        // compare
                        var callEqual = new Call()
                        {
                            Method = this.stringEquality
                        };
                        callEqual.Arguments.Add(local);
                        callEqual.Arguments.Add(new Literal {
                            Value = label.Value
                        });

                        // set value
                        var setExpr = new ExpressionStatement
                        {
                            Expression =
                                new AssignmentOperator
                            {
                                Left  = localCase,
                                Right = new Literal {
                                    Value = ConstantValue.Create(caseIndex)
                                }
                            }
                        };

                        var ifStatement = new IfStatement
                        {
                            Condition =
                                new BinaryOperator
                            {
                                OperatorKind = BinaryOperatorKind.Equal,
                                Left         = new Literal {
                                    Value = ConstantValue.Create(0)
                                },
                                Right = callEqual
                            },
                            IfStatements = setExpr
                        };

                        first = first ?? ifStatement;
                        if (last != null)
                        {
                            last.ElseStatementsOpt = ifStatement;
                        }

                        last = ifStatement;

                        // remap case value
                        label.Value = ConstantValue.Create(caseIndex);
                    }
                }

                if (first != null)
                {
                    first.WriteTo(c);
                }

                c.Separate();
            }

            foreach (var statement in this.statements)
            {
                statement.WriteTo(c);
            }

            c.TextSpan("switch");
            c.WhiteSpace();
            c.TextSpan("(");
            if (this.stringEquality != null)
            {
                localCase.WriteTo(c);
            }
            else
            {
                this.expression.WriteTo(c);
            }

            c.TextSpan(")");
            c.NewLine();
            c.OpenBlock();
            foreach (var switchSection in this.switchCases)
            {
                switchSection.WriteTo(c);
            }

            c.EndBlock();

            if (this.stringEquality != null)
            {
                c.EndBlock();
            }

            c.Separate();
        }