Example #1
0
            ParameterDeclaration CreateParameterForCurrentRangeVariable()
            {
                var param = new ParameterDeclaration();

                if (currentTransparentType.Count == 1)
                {
                    var clonedRangeVariable = (Identifier)currentTransparentType[0].Item1.Clone();
                    if (!rangeVariables.ContainsKey(currentTransparentType[0].Item1))
                    {
                        rangeVariables[currentTransparentType[0].Item1] = param;
                    }
                    param.AddChild(clonedRangeVariable, Roles.Identifier);
                }
                else
                {
                    if (!TransparentIdentifierNamePicker.MoveNext())
                    {
                        TransparentIdentifierNamePicker = FallbackTransparentIdentifierNamePicker().GetEnumerator();
                        TransparentIdentifierNamePicker.MoveNext();
                    }
                    string name = TransparentIdentifierNamePicker.Current;
                    param.AddChild(Identifier.Create(name), Roles.Identifier);
                }
                return(param);
            }
Example #2
0
 public ParameterDeclaration(AstType type, string name, ParameterModifier modifier = ParameterModifier.None)
 {
     Type      = type;
     NameToken = Identifier.Create(name);
     NameToken.AddAnnotation(BoxedTextColor.Parameter);
     ParameterModifier = modifier;
 }
Example #3
0
        public UsingAliasDeclaration(string alias, string nameSpace)
        {
            AddChild(Identifier.Create (alias), AliasRole);
            var st = new SimpleType(nameSpace);

            st.AddAnnotation(BoxedTextColor.Namespace);
            AddChild(st, ImportRole);
        }
Example #4
0
        /// <summary>
        /// Builds an member reference expression using this expression as target.
        /// </summary>
        public virtual MemberReferenceExpression Member(string memberName, object memberAnnotation)
        {
            var id = Identifier.Create(memberName);

            if (memberAnnotation != null)
            {
                id.AddAnnotation(memberAnnotation);
            }
            return(new MemberReferenceExpression {
                Target = this, MemberNameToken = id
            });
        }
Example #5
0
            public override AstNode VisitQueryJoinClause(QueryJoinClause queryJoinClause)
            {
                Expression resultSelectorBody = null;
                var        inExpression       = VisitNested(queryJoinClause.InExpression, null);

                if (!queryJoinClause.Type.IsNull)
                {
                    inExpression = inExpression.Invoke("Cast", new[] { queryJoinClause.Type.Clone() }, EmptyList <Expression> .Instance);
                }
                var key1SelectorFirstParam = CreateParameterForCurrentRangeVariable();
                var key1Selector           = CreateLambda(new[] { key1SelectorFirstParam }, VisitNested(queryJoinClause.OnExpression, key1SelectorFirstParam));
                var key2Param    = CreateParameter(Identifier.Create(queryJoinClause.JoinIdentifier));
                var key2Selector = CreateLambda(new[] { key2Param }, VisitNested(queryJoinClause.EqualsExpression, null));

                var resultSelectorFirstParam = CreateParameterForCurrentRangeVariable();

                var select = GetNextQueryClause(queryJoinClause) as QuerySelectClause;

                if (select != null)
                {
                    resultSelectorBody = VisitNested(select.Expression, resultSelectorFirstParam);
                    eatSelect          = true;
                }

                if (queryJoinClause.IntoKeyword.IsNull)
                {
                    // Normal join
                    if (resultSelectorBody == null)
                    {
                        resultSelectorBody = AddMemberToCurrentTransparentType(resultSelectorFirstParam, queryJoinClause.JoinIdentifierToken, new IdentifierExpression(queryJoinClause.JoinIdentifier), false);
                    }

                    var resultSelector = CreateLambda(new[] { resultSelectorFirstParam, CreateParameter(Identifier.Create(queryJoinClause.JoinIdentifier)) }, resultSelectorBody);
                    rangeVariables[queryJoinClause.JoinIdentifierToken] = key2Param;
                    return(currentResult.Invoke("Join", inExpression, key1Selector, key2Selector, resultSelector));
                }
                else
                {
                    // Group join
                    if (resultSelectorBody == null)
                    {
                        resultSelectorBody = AddMemberToCurrentTransparentType(resultSelectorFirstParam, queryJoinClause.IntoIdentifierToken, new IdentifierExpression(queryJoinClause.IntoIdentifier), false);
                    }

                    var intoParam      = CreateParameter(Identifier.Create(queryJoinClause.IntoIdentifier));
                    var resultSelector = CreateLambda(new[] { resultSelectorFirstParam, intoParam }, resultSelectorBody);
                    rangeVariables[queryJoinClause.IntoIdentifierToken] = intoParam;

                    return(currentResult.Invoke("GroupJoin", inExpression, key1Selector, key2Selector, resultSelector));
                }
            }
            ParameterDeclaration CreateParameterForCurrentRangeVariable()
            {
                var param = new ParameterDeclaration();

                if (currentTransparentType.Count == 1)
                {
                    var clonedRangeVariable = (Identifier)currentTransparentType[0].Item1.Clone();
                    if (!rangeVariables.ContainsKey(currentTransparentType[0].Item1))
                    {
                        rangeVariables[currentTransparentType[0].Item1] = param;
                    }
                    param.AddChild(clonedRangeVariable, Roles.Identifier);
                }
                else
                {
                    param.AddChild(Identifier.Create(string.Format(CultureInfo.InvariantCulture, TransparentParameterNameTemplate, currentTransparentParameter++)), Roles.Identifier);
                }
                return(param);
            }
 static AstType CreateNamespaceNameType(string ns, IAssembly asm)
 {
     var sb = Interlocked.CompareExchange(ref cachedStringBuilder, null, cachedStringBuilder) ?? new StringBuilder();
     string[] parts = ns.Split('.');
     var nsAsm = asm;
     sb.Clear();
     sb.Append(parts[0]);
     SimpleType simpleType;
     AstType nsType = simpleType = new SimpleType(parts[0]).WithAnnotation(BoxedTextColor.Namespace);
     simpleType.IdentifierToken.WithAnnotation(BoxedTextColor.Namespace).WithAnnotation(new NamespaceReference(nsAsm, parts[0]));
     for (int i = 1; i < parts.Length; i++) {
         sb.Append('.');
         sb.Append(parts[i]);
         var nsPart = sb.ToString();
         nsType = new MemberType { Target = nsType, MemberNameToken = Identifier.Create(parts[i]).WithAnnotation(BoxedTextColor.Namespace).WithAnnotation(new NamespaceReference(nsAsm, nsPart)) }.WithAnnotation(BoxedTextColor.Namespace);
     }
     if (sb.Capacity <= 1000)
         cachedStringBuilder = sb;
     return nsType;
 }
Example #8
0
        public void ConvertSymbol(ISymbol symbol, TokenWriter writer, CSharpFormattingOptions formattingPolicy)
        {
            if (symbol == null)
            {
                throw new ArgumentNullException("symbol");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (formattingPolicy == null)
            {
                throw new ArgumentNullException("formattingPolicy");
            }

            TypeSystemAstBuilder astBuilder = CreateAstBuilder();
            AstNode           node          = astBuilder.ConvertSymbol(symbol);
            EntityDeclaration entityDecl    = node as EntityDeclaration;

            if (entityDecl != null)
            {
                PrintModifiers(entityDecl.Modifiers, writer);
            }

            if ((ConversionFlags & ConversionFlags.ShowDefinitionKeyword) == ConversionFlags.ShowDefinitionKeyword)
            {
                if (node is TypeDeclaration)
                {
                    switch (((TypeDeclaration)node).ClassType)
                    {
                    case ClassType.Class:
                        writer.WriteKeyword(Roles.ClassKeyword, "class");
                        break;

                    case ClassType.Struct:
                        writer.WriteKeyword(Roles.StructKeyword, "struct");
                        break;

                    case ClassType.Interface:
                        writer.WriteKeyword(Roles.InterfaceKeyword, "interface");
                        break;

                    case ClassType.Enum:
                        writer.WriteKeyword(Roles.EnumKeyword, "enum");
                        break;

                    default:
                        throw new Exception("Invalid value for ClassType");
                    }
                    writer.Space();
                }
                else if (node is DelegateDeclaration)
                {
                    writer.WriteKeyword(Roles.DelegateKeyword, "delegate");
                    writer.Space();
                }
                else if (node is EventDeclaration)
                {
                    writer.WriteKeyword(EventDeclaration.EventKeywordRole, "event");
                    writer.Space();
                }
                else if (node is NamespaceDeclaration)
                {
                    writer.WriteKeyword(Roles.NamespaceKeyword, "namespace");
                    writer.Space();
                }
            }

            if ((ConversionFlags & ConversionFlags.ShowReturnType) == ConversionFlags.ShowReturnType)
            {
                var rt = node.GetChildByRole(Roles.Type);
                if (!rt.IsNull)
                {
                    rt.AcceptVisitor(new CSharpOutputVisitor(writer, formattingPolicy));
                    writer.Space();
                }
            }

            if (symbol is ITypeDefinition)
            {
                WriteTypeDeclarationName((ITypeDefinition)symbol, writer, formattingPolicy);
            }
            else if (symbol is IMember)
            {
                WriteMemberDeclarationName((IMember)symbol, writer, formattingPolicy);
            }
            else
            {
                writer.WriteIdentifier(Identifier.Create(symbol.Name));
            }

            if ((ConversionFlags & ConversionFlags.ShowParameterList) == ConversionFlags.ShowParameterList && HasParameters(symbol))
            {
                writer.WriteToken(symbol.SymbolKind == SymbolKind.Indexer ? Roles.LBracket : Roles.LPar, symbol.SymbolKind == SymbolKind.Indexer ? "[" : "(");
                bool first = true;
                foreach (var param in node.GetChildrenByRole(Roles.Parameter))
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        writer.WriteToken(Roles.Comma, ",");
                        writer.Space();
                    }
                    param.AcceptVisitor(new CSharpOutputVisitor(writer, formattingPolicy));
                }
                writer.WriteToken(symbol.SymbolKind == SymbolKind.Indexer ? Roles.RBracket : Roles.RPar, symbol.SymbolKind == SymbolKind.Indexer ? "]" : ")");
            }

            if ((ConversionFlags & ConversionFlags.ShowBody) == ConversionFlags.ShowBody && !(node is TypeDeclaration))
            {
                IProperty property = symbol as IProperty;
                if (property != null)
                {
                    writer.Space();
                    writer.WriteToken(Roles.LBrace, "{");
                    writer.Space();
                    if (property.CanGet)
                    {
                        writer.WriteKeyword(PropertyDeclaration.GetKeywordRole, "get");
                        writer.WriteToken(Roles.Semicolon, ";");
                        writer.Space();
                    }
                    if (property.CanSet)
                    {
                        writer.WriteKeyword(PropertyDeclaration.SetKeywordRole, "set");
                        writer.WriteToken(Roles.Semicolon, ";");
                        writer.Space();
                    }
                    writer.WriteToken(Roles.RBrace, "}");
                }
                else
                {
                    writer.WriteToken(Roles.Semicolon, ";");
                }
            }
        }
Example #9
0
        void WriteMemberDeclarationName(IMember member, TokenWriter writer, CSharpFormattingOptions formattingPolicy)
        {
            TypeSystemAstBuilder astBuilder = CreateAstBuilder();
            EntityDeclaration    node       = astBuilder.ConvertEntity(member);

            if ((ConversionFlags & ConversionFlags.ShowDeclaringType) == ConversionFlags.ShowDeclaringType && member.DeclaringType != null)
            {
                ConvertType(member.DeclaringType, writer, formattingPolicy);
                writer.WriteToken(Roles.Dot, ".");
            }
            switch (member.SymbolKind)
            {
            case SymbolKind.Indexer:
                writer.WriteKeyword(Roles.Identifier, "this");
                break;

            case SymbolKind.Constructor:
                WriteQualifiedName(member.DeclaringType.Name, writer, formattingPolicy);
                break;

            case SymbolKind.Destructor:
                writer.WriteToken(DestructorDeclaration.TildeRole, "~");
                WriteQualifiedName(member.DeclaringType.Name, writer, formattingPolicy);
                break;

            case SymbolKind.Operator:
                switch (member.Name)
                {
                case "op_Implicit":
                    writer.WriteKeyword(OperatorDeclaration.ImplicitRole, "implicit");
                    writer.Space();
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    ConvertType(member.ReturnType, writer, formattingPolicy);
                    break;

                case "op_Explicit":
                    writer.WriteKeyword(OperatorDeclaration.ExplicitRole, "explicit");
                    writer.Space();
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    ConvertType(member.ReturnType, writer, formattingPolicy);
                    break;

                default:
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    var operatorType = OperatorDeclaration.GetOperatorType(member.Name);
                    if (operatorType.HasValue)
                    {
                        writer.WriteToken(OperatorDeclaration.GetRole(operatorType.Value), OperatorDeclaration.GetToken(operatorType.Value));
                    }
                    else
                    {
                        writer.WriteIdentifier(node.NameToken);
                    }
                    break;
                }
                break;

            default:
                writer.WriteIdentifier(Identifier.Create(member.Name));
                break;
            }
            if ((ConversionFlags & ConversionFlags.ShowTypeParameterList) == ConversionFlags.ShowTypeParameterList && member.SymbolKind == SymbolKind.Method)
            {
                var outputVisitor = new CSharpOutputVisitor(writer, formattingPolicy);
                outputVisitor.WriteTypeParameters(node.GetChildrenByRole(Roles.TypeParameter));
            }
        }
Example #10
0
 public UsingAliasDeclaration(string alias, AstType import)
 {
     AddChild(Identifier.Create (alias), AliasRole);
     AddChild(import, ImportRole);
 }