Пример #1
0
        private void WritePart(Expression expression, bool toString, ResolveResult rr)
        {
            if (toString)
            {
                var toStringMethod = rr.Type.GetMembers().FirstOrDefault(m =>
                {
                    if (m.Name == "ToString" && !m.IsStatic && m.ReturnType.IsKnownType(KnownTypeCode.String) && m.IsOverride)
                    {
                        var method = m as IMethod;

                        if (method != null && method.Parameters.Count == 0 && method.TypeParameters.Count == 0)
                        {
                            return(true);
                        }
                    }

                    return(false);
                });

                if (toStringMethod != null)
                {
                    var inline = this.Emitter.GetInline(toStringMethod);

                    if (inline != null)
                    {
                        var writer = new Writer
                        {
                            InlineCode = inline,
                            Output     = this.Emitter.Output,
                            IsNewLine  = this.Emitter.IsNewLine
                        };
                        this.Emitter.IsNewLine = false;
                        this.Emitter.Output    = new StringBuilder();

                        expression.AcceptVisitor(this.Emitter);

                        string result = this.Emitter.Output.ToString();
                        this.Emitter.Output    = writer.Output;
                        this.Emitter.IsNewLine = writer.IsNewLine;

                        var argsInfo = new ArgumentsInfo(this.Emitter, expression, rr);
                        argsInfo.ArgumentsExpressions = new Expression[] { expression };
                        argsInfo.ArgumentsNames       = new string[] { "this" };
                        argsInfo.ThisArgument         = result;
                        new InlineArgumentsBlock(this.Emitter, argsInfo, writer.InlineCode).Emit();

                        //result = writer.InlineCode.Replace("{this}", result);
                        //this.Write(result);

                        return;
                    }
                }

                expression.AcceptVisitor(this.Emitter);
            }
            else
            {
                expression.AcceptVisitor(this.Emitter);
            }
        }
Пример #2
0
        string InsertRequired(Expression expr)
        {
            expr = expr.Clone();
            expr.AcceptVisitor(new InsertParenthesesVisitor {
                InsertParenthesesForReadability = false
            }, null);
            StringWriter w = new StringWriter();

            expr.AcceptVisitor(new OutputVisitor(w, policy), null);
            return(w.ToString());
        }
Пример #3
0
        string InsertRequired(Expression expr)
        {
            expr = expr.Clone();
            expr.AcceptVisitor(new InsertParenthesesVisitor {
                InsertParenthesesForReadability = false
            });
            StringWriter w = new StringWriter();

            w.NewLine = " ";
            expr.AcceptVisitor(new CSharpOutputVisitor(new TextWriterOutputFormatter(w)
            {
                IndentationString = ""
            }, policy));
            return(w.ToString());
        }
Пример #4
0
        void AssertOutput(string expected, Expression expr, CSharpFormattingOptions policy = null)
        {
            if (policy == null)
            {
                policy = new CSharpFormattingOptions();
            }
            ;
            StringWriter w = new StringWriter();

            w.NewLine = "\n";
            expr.AcceptVisitor(new CSharpOutputVisitor(new TextWriterOutputFormatter(w)
            {
                IndentationString = "\t"
            }, policy), null);
            Assert.AreEqual(expected.Replace("\r", ""), w.ToString());
        }
            Expression VisitNested(Expression node, ParameterDeclaration transparentParameter)
            {
                var oldRangeVariableSubstitutions = activeRangeVariableSubstitutions;

                try {
                    if (transparentParameter != null && currentTransparentType.Count > 1)
                    {
                        activeRangeVariableSubstitutions = new Dictionary <string, Expression>(activeRangeVariableSubstitutions);
                        foreach (var t in currentTransparentType)
                        {
                            activeRangeVariableSubstitutions[t.Item1.Name] = MakeNestedMemberAccess(new IdentifierExpression(transparentParameter.Name), t.Item2);
                        }
                    }
                    var result = node.AcceptVisitor(this);
                    return((Expression)(result ?? node.Clone()));
                }
                finally {
                    activeRangeVariableSubstitutions = oldRangeVariableSubstitutions;
                }
            }
Пример #6
0
        IConstantValue ConvertAttributeArgument(Expression expression)
        {
            ConstantValueBuilder b = new ConstantValueBuilder();

            b.convertVisitor      = this;
            b.isAttributeArgument = true;
            ConstantExpression c = expression.AcceptVisitor(b, null);

            if (c == null)
            {
                return(null);
            }
            PrimitiveConstantExpression pc = c as PrimitiveConstantExpression;

            if (pc != null)
            {
                // Save memory by directly using a SimpleConstantValue.
                return(new SimpleConstantValue(pc.Type, pc.Value));
            }
            else
            {
                return(new CSharpConstantValue(c, usingScope, currentTypeDefinition));
            }
        }
Пример #7
0
        private void WritePart(Expression expression, bool toString, ResolveResult rr, bool isCoalescing = false)
        {
            if (isCoalescing)
            {
                ConversionBlock.expressionInWork.Add(expression);
            }

            bool wrapString = false;

            if (this.NullStringCheck && rr.Type.IsKnownType(KnownTypeCode.String))
            {
                wrapString = !(expression is BinaryOperatorExpression) && !(expression is PrimitiveExpression || rr.Type.IsReferenceType != null && !rr.Type.IsReferenceType.Value);
            }

            if (toString)
            {
                var toStringMethod = rr.Type.GetMembers().FirstOrDefault(m =>
                {
                    if (m.Name == CS.Methods.TOSTRING && !m.IsStatic && m.ReturnType.IsKnownType(KnownTypeCode.String) && m.IsOverride)
                    {
                        var method = m as IMethod;

                        if (method != null && method.Parameters.Count == 0 && method.TypeParameters.Count == 0)
                        {
                            return(true);
                        }
                    }

                    return(false);
                });

                if (toStringMethod != null)
                {
                    var inline = this.Emitter.GetInline(toStringMethod);

                    if (inline != null)
                    {
                        var writer = new Writer
                        {
                            InlineCode = inline,
                            Output     = this.Emitter.Output,
                            IsNewLine  = this.Emitter.IsNewLine
                        };
                        this.Emitter.IsNewLine = false;
                        this.Emitter.Output    = new StringBuilder();

                        expression.AcceptVisitor(this.Emitter);

                        string result = this.Emitter.Output.ToString();
                        this.Emitter.Output    = writer.Output;
                        this.Emitter.IsNewLine = writer.IsNewLine;

                        var argsInfo = new ArgumentsInfo(this.Emitter, expression, (IMethod)toStringMethod);
                        argsInfo.ArgumentsExpressions = new Expression[] { expression };
                        argsInfo.ArgumentsNames       = new string[] { "this" };
                        argsInfo.ThisArgument         = result;
                        new InlineArgumentsBlock(this.Emitter, argsInfo, writer.InlineCode).Emit();
                        return;
                    }
                }
            }

            if (wrapString)
            {
                this.Write("(");
            }

            expression.AcceptVisitor(this.Emitter);

            if (wrapString)
            {
                this.Write(" || \"\")");
            }

            if (isCoalescing)
            {
                ConversionBlock.expressionInWork.Remove(expression);
            }
        }
Пример #8
0
 private Expression GetExpression(ICSharpCode.NRefactory.CSharp.Expression expression, ILTranslationContext data)
 => expression?.IsNull != false ? null : expression.AcceptVisitor(this, data).Cast <Expression>().Single();