public AnonRewriter(NamedTypeSymbol type, SemanticModel model, Compilation comp, TypeSyntax newType)
 {
     Type = type;
     Model = model;
     Comp = comp;
     NewType = newType;
 }
Beispiel #2
0
 protected override void CompileDefaultInitialization(VariableDeclaratorSyntax declarator, TypeSyntax type)
 {
     Write("=");
     Write(Space);
     Write("(");
     CompileExpression(type);
     Write(")0");
 }
        public ReplaceVarByConcreteAction(ICodeActionEditFactory editFactory, IDocument document, TypeSyntax typeSyntax)
        {
            this.editFactory = editFactory;
            this.document = document;
            this.typeSyntax = typeSyntax;

            var semanticModel = (SemanticModel)document.GetSemanticModel();
            var syntaxTree = (SyntaxTree)document.GetSyntaxTree();

            ILocation location = syntaxTree.GetLocation(typeSyntax);

            ITypeSymbol variableType = semanticModel.GetSemanticInfo(typeSyntax).Type;
            this.typeName = variableType.ToMinimalDisplayString((Location)location, semanticModel);
        }
Beispiel #4
0
        private SymbolInfo LookupType(BinderContext underlying, TypeSyntax type, int arity = 0)
        {
            using (var context = new DiagnosticBufferBinderContext(underlying))
            {
                var result = context.BindType(type, arity);
                var err = result as ErrorTypeSymbol;
                if (err == null)
                {
                    return new SymbolInfo(result, ConsList.Singleton(result), context.Commit(false));
                }

                var info = err.ErrorInfo;
                var errors = context.Commit(false).ToList();
                if (info != null)
                {
                    errors.Add(new Diagnostic(info, context.Location(type)));
                }

                var syms = (info is CSDiagnosticInfo) ? (info as CSDiagnosticInfo).Symbols : Enumerable.Empty<Symbol>();
                return new SymbolInfo(result, syms, errors);
            }
        }
 public override SymbolInfo LookupType(TypeSyntax type, int arity = 0)
 {
     throw new NotImplementedException();
 }
Beispiel #6
0
 public SymbolInfo LookupType(TypeSyntax type, int arity = 0)
 {
     return LookupType(GetEnclosingContext(type), type, arity);
 }
Beispiel #7
0
 /// <summary>
 /// Speculative binding: bind a type as if it appeared at a given point in the syntax
 /// of a program.  After this has been done, 
 /// the GetBinding methods can be used on parts of the "text" tree to get further information about
 /// the results of binding.
 /// </summary>
 /// <param name="location">The location where the type expression should be bound</param>
 /// <param name="typeName">The type name expression to be bound</param>
 /// <param name="arity">The arity of the type name to be bound</param>
 /// <returns></returns>
 public SymbolInfo BindType(SyntaxNode location, TypeSyntax typeName, int arity = 0)
 {
     return LookupType(GetEnclosingContext(location), typeName, arity);
 }
        /// <summary>
        /// Adds a new internal class with the given name equivalent to the passed type to the given syntax tree.
        /// 
        /// Sets `created` to a reference to the newly created type.
        /// </summary>
        private static SyntaxNode AddType(NamedTypeSymbol equivTo, string withName, SyntaxNode tree, out TypeSyntax created)
        {
            created = Syntax.ParseTypeName(withName);

            var members =
                equivTo.GetMembers()
                .OfType<PropertySymbol>()
                .Select(
                    s =>
                    {
                        var prop =
                            Syntax.PropertyDeclaration(
                                null,
                                Syntax.TokenList(Syntax.ParseToken("public")),
                                Syntax.ParseTypeName(s.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)).WithLeadingTrivia(Syntax.ParseLeadingTrivia(" ")).WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")),
                                null,
                                Syntax.Identifier(s.Name),
                                Syntax.AccessorList(
                                    Syntax.List(
                                        Syntax.AccessorDeclaration(
                                            SyntaxKind.GetAccessorDeclaration,
                                            null,
                                            Syntax.TokenList(),
                                            Syntax.ParseToken("get"),
                                            null,
                                            Syntax.ParseToken(";")
                                        ),
                                        Syntax.AccessorDeclaration(
                                            SyntaxKind.SetAccessorDeclaration,
                                            null,
                                            Syntax.TokenList(),
                                            Syntax.ParseToken("set"),
                                            null,
                                            Syntax.ParseToken(";")
                                        )
                                    )
                                )
                            );

                        return prop;
                    }
                )
                .Cast<MemberDeclarationSyntax>().ToList();

            var separators = new List<SyntaxToken>();
            for(var i = 0; i < members.Count - 1; i++) separators.Add(Syntax.ParseToken(","));

            var obj =
                Syntax.AnonymousObjectCreationExpression(
                    Syntax.SeparatedList(
                        equivTo.GetMembers()
                        .OfType<PropertySymbol>()
                        .Select(
                            p =>
                            {
                                var exp = Syntax.IdentifierName(p.Name);
                                return Syntax.AnonymousObjectMemberDeclarator(exp);
                            }
                        ),
                        separators
                    )
                ).WithLeadingTrivia(Syntax.ParseLeadingTrivia(" "));

            // Build the ToString() method that anonymous types have to have
            var toStringRef = Syntax.MemberAccessExpression(SyntaxKind.MemberAccessExpression, obj, Syntax.ParseToken("."), Syntax.IdentifierName("ToString"));
            var toStringAccess = Syntax.InvocationExpression(toStringRef);
            var toStringRet = Syntax.ReturnStatement(toStringAccess);
            var toStringStatements = Syntax.List<StatementSyntax>(toStringRet);
            var toStringBody =
                Syntax.Block(
                    Syntax.ParseToken("{"),
                    toStringStatements,
                    Syntax.ParseToken("}")
                );
            var toString =
                Syntax.MethodDeclaration(
                    null,
                    Syntax.TokenList(Syntax.ParseToken("public").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")), Syntax.ParseToken("override").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" "))),
                    Syntax.ParseTypeName("string").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")),
                    null,
                    Syntax.Identifier("ToString"),
                    null,
                    Syntax.ParameterList(),
                    null,
                    toStringBody
                );
            members.Add(toString);

            // Adding GetHashCode override anonymous types must have
            var hashCodeRef = Syntax.MemberAccessExpression(SyntaxKind.MemberAccessExpression, obj, Syntax.ParseToken("."), Syntax.IdentifierName("GetHashCode"));
            var hashCodeAccess = Syntax.InvocationExpression(hashCodeRef);
            var hashCodeRet = Syntax.ReturnStatement(hashCodeAccess);
            var hashCodeStatements = Syntax.List<StatementSyntax>(hashCodeRet);
            var hashCodeBody =
                Syntax.Block(
                    Syntax.ParseToken("{"),
                    hashCodeStatements,
                    Syntax.ParseToken("}")
                );
            var hashCode =
                Syntax.MethodDeclaration(
                    null,
                    Syntax.TokenList(Syntax.ParseToken("public").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")), Syntax.ParseToken("override").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" "))),
                    Syntax.ParseTypeName("int").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")),
                    null,
                    Syntax.Identifier("GetHashCode"),
                    null,
                    Syntax.ParameterList(),
                    null,
                    hashCodeBody
                );
            members.Add(hashCode);

            // Adding Equals method anonymous types must have
            var equalsAs = Syntax.ParseExpression("o as " + withName);
            var equalsAssign =
                Syntax.VariableDeclaration(
                    created.WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")),
                    Syntax.SeparatedList(
                        Syntax.VariableDeclarator(
                            Syntax.Identifier("other"),
                            null,
                            Syntax.EqualsValueClause(equalsAs)
                        )
                    )
                );
            var equalsEqualsRef = Syntax.MemberAccessExpression(SyntaxKind.MemberAccessExpression, obj, Syntax.ParseToken("."), Syntax.IdentifierName("Equals"));
            var equalsAccess = Syntax.InvocationExpression(equalsEqualsRef, Syntax.ArgumentList(Syntax.SeparatedList(Syntax.Argument(Syntax.ParseExpression("o")))));
            var equalsIf =
                Syntax.IfStatement(
                    Syntax.ParseExpression("other == null"),
                    Syntax.ReturnStatement(equalsAccess)
                );
            var equalsEqualsExps =
                equivTo.GetMembers()
                .OfType<PropertySymbol>()
                .Select(
                    p =>
                    {
                        var n = p.Name;
                        ExpressionSyntax ret;

                        if (p.Type.IsReferenceType)
                        {
                            var strExp = "(" + n + " != null ? " + n + ".Equals(other." + n + ") : (other." + n + " != null ? other." + n + ".Equals(" + n + ") : true ))";

                            ret = Syntax.ParseExpression(strExp);
                        }
                        else
                        {
                            ret = Syntax.ParseExpression(n + " == other." + n);
                        }

                        return ret.WithLeadingTrivia(Syntax.ParseLeadingTrivia(" ")).WithTrailingTrivia(Syntax.ParseTrailingTrivia(" "));
                    }
                ).ToList();
            ExpressionSyntax equalsBinary = equalsEqualsExps.First();
            for (var i = 1; i < equalsEqualsExps.Count; i++) equalsBinary = Syntax.BinaryExpression(SyntaxKind.LogicalAndExpression, equalsBinary, equalsEqualsExps[i]);
            var equalsBinaryRet = Syntax.ReturnStatement(equalsBinary.WithLeadingTrivia(Syntax.ParseLeadingTrivia(" ")));
            var equalsStatements =
                Syntax.List(
                    (StatementSyntax)Syntax.LocalDeclarationStatement(equalsAssign),
                    (StatementSyntax)equalsIf,
                    (StatementSyntax)equalsBinaryRet
                );
            var equalsBody =
                Syntax.Block(
                    Syntax.ParseToken("{"),
                    equalsStatements,
                    Syntax.ParseToken("}")
                );
            var equals =
                Syntax.MethodDeclaration(
                    null,
                    Syntax.TokenList(Syntax.ParseToken("public").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")), Syntax.ParseToken("override").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" "))),
                    Syntax.ParseTypeName("bool").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")),
                    null,
                    Syntax.Identifier("Equals"),
                    null,
                    Syntax.ParameterList(
                        Syntax.SeparatedList(
                            Syntax.Parameter(
                                null,
                                Syntax.TokenList(),
                                Syntax.ParseTypeName("object").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")),
                                Syntax.Identifier("o"),
                                null
                            )
                        )
                    ),
                    null,
                    equalsBody
                );
            members.Add(equals);

            var equiv =
                Syntax.ClassDeclaration(
                    null,
                    Syntax.ParseToken("internal").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")),
                    Syntax.Identifier(withName).WithLeadingTrivia(Syntax.ParseLeadingTrivia(" ")).WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")),
                    null,
                    null,
                    null,
                    Syntax.List<MemberDeclarationSyntax>(members)
                );

            var namespaces = tree.ChildNodes().OfType<NamespaceDeclarationSyntax>();

            if (namespaces.Count() == 0 || namespaces.Count() > 1)
            {
                // HACK, better way to insert classes should be considered
                throw new Exception("Making some assumptions about namespaces, you can only have 1");
            }

            var @namespace = namespaces.Single();

            var updated =
                @namespace.WithMembers(
                    @namespace.Members.Add(equiv)
                );

            return tree.ReplaceNode(@namespace, updated);
        }
Beispiel #9
0
        protected override void CompileType(TypeSyntax type)
        {
            var info = GetModel(type).GetSymbolInfo(type);

            var symbol = info.Symbol;
            if (symbol != null && TranslationLookup.SymbolMap.ContainsKey(symbol))
            {
                Write(TranslationLookup.SymbolMap[symbol].Translation);
            }
            else
            {
                var predefined = type as PredefinedTypeSyntax;
                if (null != predefined)
                {
                    if (predefined.Keyword.ToString() == "void")
                    {
                        Write("void");
                    }
                }
                else
                {
                    Write("ERROR(Unsupported type : {0})", type);
                }
            }
        }
 protected abstract void CompileVariableDeclarator(VariableDeclaratorSyntax declarator, TypeSyntax type);
 protected abstract void CompileType(TypeSyntax type);