SyntaxGenerator.cs 69.3 KB
Newer Older
1 2 3 4
// Copyright (c) Microsoft Open Technologies, Inc.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
5
using System.Collections.Immutable;
6
using System.Linq;
M
mattwar 已提交
7 8
using System.Threading;
using System.Threading.Tasks;
9 10
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Host;
M
mattwar 已提交
11
using Microsoft.CodeAnalysis.Shared.Extensions;
12 13 14 15 16 17 18 19 20 21 22
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.CodeGeneration
{
    /// <summary>
    /// A language agnostic factory for creating syntax nodes.
    /// 
    /// This API can be used to create language specific syntax nodes that are semantically similar between languages.
    /// </summary>
    public abstract class SyntaxGenerator : ILanguageService
    {
23 24
        public static SyntaxRemoveOptions DefaultRemoveOptions = SyntaxRemoveOptions.KeepUnbalancedDirectives | SyntaxRemoveOptions.AddElasticMarker;

25 26 27 28 29 30 31 32
        /// <summary>
        /// Gets the <see cref="SyntaxGenerator"/> for the specified language.
        /// </summary>
        public static SyntaxGenerator GetGenerator(Workspace workspace, string language)
        {
            return workspace.Services.GetLanguageServices(language).GetService<SyntaxGenerator>();
        }

M
mattwar 已提交
33 34 35 36 37 38 39 40
        /// <summary>
        /// Gets the <see cref="SyntaxGenerator"/> for the language corresponding to the document.
        /// </summary>
        public static SyntaxGenerator GetGenerator(Document document)
        {
            return document.Project.LanguageServices.GetService<SyntaxGenerator>();
        }

41 42
        #region Declarations

M
mattwar 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        /// <summary>
        /// Returns the node if it is a declaration, the immediate enclosing declaration if one exists, or null.
        /// </summary>
        public SyntaxNode GetDeclaration(SyntaxNode node)
        {
            while (node != null)
            {
                if (GetDeclarationKind(node) != DeclarationKind.None)
                {
                    return node;
                }
                else
                {
                    node = node.Parent;
                }
            }

            return null;
        }

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        /// <summary>
        /// Returns the enclosing declaration of the specified kind or null.
        /// </summary>
        public SyntaxNode GetDeclaration(SyntaxNode node, DeclarationKind kind)
        {
            while (node != null)
            {
                if (GetDeclarationKind(node) == kind)
                {
                    return node;
                }
                else
                {
                    node = node.Parent;
                }
            }

            return null;
        }

83 84 85 86
        /// <summary>
        /// Creates a field declaration.
        /// </summary>
        public abstract SyntaxNode FieldDeclaration(
87
            string name,
88
            SyntaxNode type,
89
            Accessibility accessibility = Accessibility.NotApplicable,
90
            DeclarationModifiers modifiers = default(DeclarationModifiers),
91 92 93 94 95
            SyntaxNode initializer = null);

        /// <summary>
        /// Creates a field declaration matching an existing field symbol.
        /// </summary>
M
mattwar 已提交
96 97 98 99 100 101 102 103 104 105
        public SyntaxNode FieldDeclaration(IFieldSymbol field)
        {
            var initializer = field.HasConstantValue ? this.LiteralExpression(field.ConstantValue) : null;
            return FieldDeclaration(field, initializer);
        }

        /// <summary>
        /// Creates a field declaration matching an existing field symbol.
        /// </summary>
        public SyntaxNode FieldDeclaration(IFieldSymbol field, SyntaxNode initializer)
106
        {
107 108 109 110
            return FieldDeclaration(
                field.Name,
                TypeExpression(field.Type),
                field.DeclaredAccessibility,
111
                DeclarationModifiers.From(field),
112
                initializer);
113 114 115 116 117 118
        }

        /// <summary>
        /// Creates a method declaration.
        /// </summary>
        public abstract SyntaxNode MethodDeclaration(
119
            string name,
120
            IEnumerable<SyntaxNode> parameters = null,
121 122 123
            IEnumerable<string> typeParameters = null,
            SyntaxNode returnType = null,
            Accessibility accessibility = Accessibility.NotApplicable,
124
            DeclarationModifiers modifiers = default(DeclarationModifiers),
125 126 127 128 129 130 131
            IEnumerable<SyntaxNode> statements = null);

        /// <summary>
        /// Creates a method declaration matching an existing method symbol.
        /// </summary>
        public SyntaxNode MethodDeclaration(IMethodSymbol method, IEnumerable<SyntaxNode> statements = null)
        {
132 133 134 135 136
            var decl = MethodDeclaration(
                method.Name,
                parameters: method.Parameters.Select(p => ParameterDeclaration(p)),
                returnType: TypeExpression(method.ReturnType),
                accessibility: method.DeclaredAccessibility,
137
                modifiers: DeclarationModifiers.From(method),
138 139 140 141 142 143 144 145
                statements: statements);

            if (method.TypeParameters.Length > 0)
            {
                decl = this.WithTypeParametersAndConstraints(decl, method.TypeParameters);
            }

            return decl;
146 147 148 149 150 151
        }

        /// <summary>
        /// Creates a parameter declaration.
        /// </summary>
        public abstract SyntaxNode ParameterDeclaration(
152 153 154 155
            string name,
            SyntaxNode type = null,
            SyntaxNode initializer = null,
            RefKind refKind = RefKind.None);
156 157 158 159 160 161

        /// <summary>
        /// Creates a parameter declaration matching an existing parameter symbol.
        /// </summary>
        public SyntaxNode ParameterDeclaration(IParameterSymbol symbol, SyntaxNode initializer = null)
        {
162 163 164 165 166
            return ParameterDeclaration(
                symbol.Name,
                TypeExpression(symbol.Type),
                initializer,
                symbol.RefKind);
167 168 169 170 171 172
        }

        /// <summary>
        /// Creates a property declaration.
        /// </summary>
        public abstract SyntaxNode PropertyDeclaration(
173
            string name,
174
            SyntaxNode type,
175
            Accessibility accessibility = Accessibility.NotApplicable,
176
            DeclarationModifiers modifiers = default(DeclarationModifiers),
M
mattwar 已提交
177 178
            IEnumerable<SyntaxNode> getAccessorStatements = null,
            IEnumerable<SyntaxNode> setAccessorStatements = null);
179 180 181 182

        /// <summary>
        /// Creates a property declaration using an existing property symbol as a signature.
        /// </summary>
M
mattwar 已提交
183 184 185 186
        public SyntaxNode PropertyDeclaration(
            IPropertySymbol property, 
            IEnumerable<SyntaxNode> getAccessorStatements = null, 
            IEnumerable<SyntaxNode> setAccessorStatements = null)
187
        {
188 189 190
            return PropertyDeclaration(
                    property.Name,
                    TypeExpression(property.Type),
191
                    property.DeclaredAccessibility,
192
                    DeclarationModifiers.From(property),
M
mattwar 已提交
193 194
                    getAccessorStatements,
                    setAccessorStatements);
195 196 197 198 199 200 201
        }

        /// <summary>
        /// Creates an indexer declaration.
        /// </summary>
        public abstract SyntaxNode IndexerDeclaration(
            IEnumerable<SyntaxNode> parameters,
202 203
            SyntaxNode type,
            Accessibility accessibility = Accessibility.NotApplicable,
204
            DeclarationModifiers modifiers = default(DeclarationModifiers),
M
mattwar 已提交
205 206
            IEnumerable<SyntaxNode> getAccessorStatements = null,
            IEnumerable<SyntaxNode> setAccessorStatements = null);
207 208 209 210

        /// <summary>
        /// Creates an indexer declaration matching an existing indexer symbol.
        /// </summary>
M
mattwar 已提交
211 212 213 214
        public SyntaxNode IndexerDeclaration(
            IPropertySymbol indexer, 
            IEnumerable<SyntaxNode> getAccessorStatements = null, 
            IEnumerable<SyntaxNode> setAccessorStatements = null)
215
        {
216 217 218 219
            return IndexerDeclaration(
                indexer.Parameters.Select(p => this.ParameterDeclaration(p)),
                TypeExpression(indexer.Type),
                indexer.DeclaredAccessibility,
220
                DeclarationModifiers.From(indexer),
M
mattwar 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
                getAccessorStatements,
                setAccessorStatements);
        }

        /// <summary>
        /// Creates an event declaration.
        /// </summary>
        public abstract SyntaxNode EventDeclaration(
            string name,
            SyntaxNode type,
            Accessibility accessibility = Accessibility.NotApplicable,
            DeclarationModifiers modifiers = default(DeclarationModifiers));

        /// <summary>
        /// Creates an event declaration from an existing event symbol
        /// </summary>
        public SyntaxNode EventDeclaration(IEventSymbol symbol)
        {
            return EventDeclaration(
                symbol.Name,
                TypeExpression(symbol.Type),
                symbol.DeclaredAccessibility,
                DeclarationModifiers.From(symbol));
        }

        /// <summary>
        /// Creates a custom event declaration.
        /// </summary>
        public abstract SyntaxNode CustomEventDeclaration(
            string name,
            SyntaxNode type,
            Accessibility accessibility = Accessibility.NotApplicable,
            DeclarationModifiers modifiers = default(DeclarationModifiers),
            IEnumerable<SyntaxNode> parameters = null,
            IEnumerable<SyntaxNode> addAccessorStatements = null,
            IEnumerable<SyntaxNode> removeAccessorStatements = null);

        /// <summary>
        /// Creates a custom event declaration from an existing event symbol.
        /// </summary>
        public SyntaxNode CustomEventDeclaration(
            IEventSymbol symbol,
            IEnumerable<SyntaxNode> addAccessorStatements = null,
            IEnumerable<SyntaxNode> removeAccessorStatements = null)
        {
            var invoke = symbol.Type.GetMembers("Invoke").FirstOrDefault(m => m.Kind == SymbolKind.Method) as IMethodSymbol;
            var parameters = invoke != null ? invoke.Parameters.Select(p => this.ParameterDeclaration(p)) : null;

            return CustomEventDeclaration(
                symbol.Name,
                TypeExpression(symbol.Type),
                symbol.DeclaredAccessibility,
                DeclarationModifiers.From(symbol),
                parameters: parameters,
                addAccessorStatements: addAccessorStatements,
                removeAccessorStatements: removeAccessorStatements);
277 278 279 280 281 282
        }

        /// <summary>
        /// Creates a constructor declaration.
        /// </summary>
        public abstract SyntaxNode ConstructorDeclaration(
283
            string containingTypeName = null,
284
            IEnumerable<SyntaxNode> parameters = null,
285
            Accessibility accessibility = Accessibility.NotApplicable,
286
            DeclarationModifiers modifiers = default(DeclarationModifiers),
287 288 289 290 291 292 293 294 295 296 297
            IEnumerable<SyntaxNode> baseConstructorArguments = null,
            IEnumerable<SyntaxNode> statements = null);

        /// <summary>
        /// Create a constructor declaration using 
        /// </summary>
        public SyntaxNode ConstructorDeclaration(
            IMethodSymbol constructorMethod,
            IEnumerable<SyntaxNode> baseConstructorArguments = null,
            IEnumerable<SyntaxNode> statements = null)
        {
298 299 300 301
            return ConstructorDeclaration(
                constructorMethod.ContainingType != null ? constructorMethod.ContainingType.Name : "New",
                constructorMethod.Parameters.Select(p => ParameterDeclaration(p)),
                constructorMethod.DeclaredAccessibility,
302
                DeclarationModifiers.From(constructorMethod),
303 304
                baseConstructorArguments,
                statements);
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
        }

        /// <summary>
        /// Converts method, property and indexer declarations into public interface implementations.
        /// This is equivalent to an implicit C# interface implementation (you can access it via the interface or directly via the named member.)
        /// </summary>
        public abstract SyntaxNode AsPublicInterfaceImplementation(SyntaxNode declaration, SyntaxNode interfaceType);

        /// <summary>
        /// Converts method, property and indexer declarations into private interface implementations.
        /// This is equivalent to a C# explicit interface implementation (you can declare it for access via the interface, but cannot call it directly).
        /// </summary>
        public abstract SyntaxNode AsPrivateInterfaceImplementation(SyntaxNode declaration, SyntaxNode interfaceType);

        /// <summary>
        /// Creates a class declaration.
        /// </summary>
        public abstract SyntaxNode ClassDeclaration(
323 324 325
            string name,
            IEnumerable<string> typeParameters = null,
            Accessibility accessibility = Accessibility.NotApplicable,
326
            DeclarationModifiers modifiers = default(DeclarationModifiers),
327 328
            SyntaxNode baseType = null,
            IEnumerable<SyntaxNode> interfaceTypes = null,
329
            IEnumerable<SyntaxNode> members = null);
330 331 332 333 334

        /// <summary>
        /// Creates a struct declaration.
        /// </summary>
        public abstract SyntaxNode StructDeclaration(
335 336 337
            string name,
            IEnumerable<string> typeParameters = null,
            Accessibility accessibility = Accessibility.NotApplicable,
338
            DeclarationModifiers modifiers = default(DeclarationModifiers),
339
            IEnumerable<SyntaxNode> interfaceTypes = null,
340
            IEnumerable<SyntaxNode> members = null);
341 342 343 344 345

        /// <summary>
        /// Creates a interface declaration.
        /// </summary>
        public abstract SyntaxNode InterfaceDeclaration(
346 347 348
            string name,
            IEnumerable<string> typeParameters = null,
            Accessibility accessibility = Accessibility.NotApplicable,
349
            IEnumerable<SyntaxNode> interfaceTypes = null,
350
            IEnumerable<SyntaxNode> members = null);
351 352 353 354 355

        /// <summary>
        /// Creates an enum declaration.
        /// </summary>
        public abstract SyntaxNode EnumDeclaration(
356 357
            string name,
            Accessibility accessibility = Accessibility.NotApplicable,
M
mattwar 已提交
358
            DeclarationModifiers modifiers = default(DeclarationModifiers),
359
            IEnumerable<SyntaxNode> members = null);
360 361 362 363

        /// <summary>
        /// Creates an enum member
        /// </summary>
364
        public abstract SyntaxNode EnumMember(string name, SyntaxNode expression = null);
365

M
mattwar 已提交
366 367 368 369 370 371 372 373 374 375 376
        /// <summary>
        /// Creates a delegate declaration.
        /// </summary>
        public abstract SyntaxNode DelegateDeclaration(
            string name,
            IEnumerable<SyntaxNode> parameters = null,
            IEnumerable<string> typeParameters = null,
            SyntaxNode returnType = null,
            Accessibility accessibility = Accessibility.NotApplicable,
            DeclarationModifiers modifiers = default(DeclarationModifiers));

377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
        /// <summary>
        /// Creates a declaration matching an existing symbol.
        /// </summary>
        public SyntaxNode Declaration(ISymbol symbol)
        {
            switch (symbol.Kind)
            {
                case SymbolKind.Field:
                    return FieldDeclaration((IFieldSymbol)symbol);

                case SymbolKind.Property:
                    var property = (IPropertySymbol)symbol;
                    if (property.IsIndexer)
                    {
                        return IndexerDeclaration(property);
                    }
                    else
                    {
                        return PropertyDeclaration(property);
                    }

M
mattwar 已提交
398 399 400 401 402 403 404 405 406 407 408
                case SymbolKind.Event:
                    var ev = (IEventSymbol)symbol;
                    if (ev.IsAbstract || ev.IsVirtual || ev.AddMethod != null || ev.RemoveMethod != null)
                    {
                        return CustomEventDeclaration(ev);
                    }
                    else
                    {
                        return EventDeclaration(ev);
                    }

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
                case SymbolKind.Method:
                    var method = (IMethodSymbol)symbol;
                    switch (method.MethodKind)
                    {
                        case MethodKind.Constructor:
                        case MethodKind.SharedConstructor:
                            return ConstructorDeclaration(method);

                        case MethodKind.Ordinary:
                            return MethodDeclaration(method);
                    }

                    break;

                case SymbolKind.Parameter:
                    return ParameterDeclaration((IParameterSymbol)symbol);

                case SymbolKind.NamedType:
                    var type = (INamedTypeSymbol)symbol;
428 429
                    SyntaxNode declaration = null;

430 431 432
                    switch (type.TypeKind)
                    {
                        case TypeKind.Class:
433 434 435
                            declaration = ClassDeclaration(
                                type.Name,
                                accessibility: type.DeclaredAccessibility,
436
                                modifiers: DeclarationModifiers.From(type),
437 438 439 440
                                baseType: TypeExpression(type.BaseType),
                                interfaceTypes: type.Interfaces != null ? type.Interfaces.Select(i => TypeExpression(i)) : null,
                                members: type.GetMembers().Select(m => Declaration(m)));
                            break;
441
                        case TypeKind.Struct:
442 443 444
                            declaration = StructDeclaration(
                                type.Name,
                                accessibility: type.DeclaredAccessibility,
445
                                modifiers: DeclarationModifiers.From(type),
446 447 448
                                interfaceTypes: type.Interfaces != null ? type.Interfaces.Select(i => TypeExpression(i)) : null,
                                members: type.GetMembers().Select(m => Declaration(m)));
                            break;
449
                        case TypeKind.Interface:
450 451 452 453 454 455
                            declaration = InterfaceDeclaration(
                                type.Name,
                                accessibility: type.DeclaredAccessibility,
                                interfaceTypes: type.Interfaces != null ? type.Interfaces.Select(i => TypeExpression(i)) : null,
                                members: type.GetMembers().Select(m => Declaration(m)));
                            break;
456
                        case TypeKind.Enum:
457 458 459 460 461
                            declaration = EnumDeclaration(
                                type.Name,
                                accessibility: type.DeclaredAccessibility,
                                members: type.GetMembers().Select(m => Declaration(m)));
                            break;
M
mattwar 已提交
462 463 464 465 466 467 468 469 470 471
                        case TypeKind.Delegate:
                            var invoke = type.GetMembers("Invoke").First() as IMethodSymbol;

                            declaration = DelegateDeclaration(
                                type.Name,
                                parameters: invoke.Parameters.Select(p => ParameterDeclaration(p)),
                                returnType: TypeExpression(invoke.ReturnType),
                                accessibility: type.DeclaredAccessibility,
                                modifiers: DeclarationModifiers.From(type));
                            break;
472 473 474 475 476
                    }

                    if (declaration != null)
                    {
                        return WithTypeParametersAndConstraints(declaration, type.TypeParameters);
477 478 479 480 481 482 483 484
                    }

                    break;
            }

            throw new ArgumentException("Symbol cannot be converted to a declaration");
        }

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
        private SyntaxNode WithTypeParametersAndConstraints(SyntaxNode declaration, ImmutableArray<ITypeParameterSymbol> typeParameters)
        {
            if (typeParameters.Length > 0)
            {
                declaration = WithTypeParameters(declaration, typeParameters.Select(tp => tp.Name));

                foreach (var tp in typeParameters)
                {
                    if (tp.HasConstructorConstraint || tp.HasReferenceTypeConstraint || tp.HasValueTypeConstraint || tp.ConstraintTypes.Length > 0)
                    {
                        declaration = this.WithTypeConstraint(declaration, tp.Name,
                            kinds: (tp.HasConstructorConstraint ? SpecialTypeConstraintKind.Constructor : SpecialTypeConstraintKind.None)
                                   | (tp.HasReferenceTypeConstraint ? SpecialTypeConstraintKind.ReferenceType : SpecialTypeConstraintKind.None)
                                   | (tp.HasValueTypeConstraint ? SpecialTypeConstraintKind.ValueType : SpecialTypeConstraintKind.None),
                            types: tp.ConstraintTypes.Select(t => TypeExpression(t)));
                    }
                }
            }

            return declaration;
        }

        /// <summary>
        /// Converts a declaration (method, class, etc) into a declaration with type parameters.
        /// </summary>
M
mattwar 已提交
510
        public abstract SyntaxNode WithTypeParameters(SyntaxNode declaration, IEnumerable<string> typeParameters);
511 512 513 514

        /// <summary>
        /// Converts a declaration (method, class, etc) into a declaration with type parameters.
        /// </summary>
M
mattwar 已提交
515
        public SyntaxNode WithTypeParameters(SyntaxNode declaration, params string[] typeParameters)
516
        {
M
mattwar 已提交
517
            return WithTypeParameters(declaration, (IEnumerable<string>)typeParameters);
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
        }

        /// <summary>
        /// Adds a type constraint to a type parameter of a declaration.
        /// </summary>
        public abstract SyntaxNode WithTypeConstraint(SyntaxNode declaration, string typeParameterName, SpecialTypeConstraintKind kinds, IEnumerable<SyntaxNode> types = null);

        /// <summary>
        /// Adds a type constraint to a type parameter of a declaration.
        /// </summary>
        public SyntaxNode WithTypeConstraint(SyntaxNode declaration, string typeParameterName, SpecialTypeConstraintKind kinds, params SyntaxNode[] types)
        {
            return WithTypeConstraint(declaration, typeParameterName, kinds, (IEnumerable<SyntaxNode>)types);
        }

        /// <summary>
        /// Adds a type constraint to a type parameter of a declaration.
        /// </summary>
        public SyntaxNode WithTypeConstraint(SyntaxNode declaration, string typeParameterName, params SyntaxNode[] types)
        {
            return WithTypeConstraint(declaration, typeParameterName, SpecialTypeConstraintKind.None, (IEnumerable<SyntaxNode>)types);
        }

541 542 543
        /// <summary>
        /// Creates a namespace declaration.
        /// </summary>
544 545
        /// <param name="name">The name of the namespace.</param>
        /// <param name="declarations">Zero or more namespace or type declarations.</param>
546 547 548 549 550
        public abstract SyntaxNode NamespaceDeclaration(SyntaxNode name, IEnumerable<SyntaxNode> declarations);

        /// <summary>
        /// Creates a namespace declaration.
        /// </summary>
551 552
        /// <param name="name">The name of the namespace.</param>
        /// <param name="declarations">Zero or more namespace or type declarations.</param>
553 554 555 556 557 558 559 560
        public SyntaxNode NamespaceDeclaration(SyntaxNode name, params SyntaxNode[] declarations)
        {
            return NamespaceDeclaration(name, (IEnumerable<SyntaxNode>)declarations);
        }

        /// <summary>
        /// Creates a namespace declaration.
        /// </summary>
561 562
        /// <param name="name">The name of the namespace.</param>
        /// <param name="declarations">Zero or more namespace or type declarations.</param>
563 564 565 566 567 568 569 570
        public SyntaxNode NamespaceDeclaration(string name, IEnumerable<SyntaxNode> declarations)
        {
            return NamespaceDeclaration(DottedName(name), declarations);
        }

        /// <summary>
        /// Creates a namespace declaration.
        /// </summary>
571 572
        /// <param name="name">The name of the namespace.</param>
        /// <param name="declarations">Zero or more namespace or type declarations.</param>
573 574 575 576 577 578 579 580
        public SyntaxNode NamespaceDeclaration(string name, params SyntaxNode[] declarations)
        {
            return NamespaceDeclaration(DottedName(name), (IEnumerable<SyntaxNode>)declarations);
        }

        /// <summary>
        /// Creates a compilation unit declaration
        /// </summary>
581 582
        /// <param name="declarations">Zero or more namespace import, namespace or type declarations.</param>
        public abstract SyntaxNode CompilationUnit(IEnumerable<SyntaxNode> declarations);
583 584 585 586

        /// <summary>
        /// Creates a compilation unit declaration
        /// </summary>
587
        /// <param name="declarations">Zero or more namespace import, namespace or type declarations.</param>
588 589 590 591 592 593 594 595
        public SyntaxNode CompilationUnit(params SyntaxNode[] declarations)
        {
            return CompilationUnit((IEnumerable<SyntaxNode>)declarations);
        }

        /// <summary>
        /// Creates a namespace import declaration.
        /// </summary>
596
        /// <param name="name">The name of the namespace being imported.</param>
597 598 599 600 601
        public abstract SyntaxNode NamespaceImportDeclaration(SyntaxNode name);

        /// <summary>
        /// Creates a namespace import declaration.
        /// </summary>
602
        /// <param name="name">The name of the namespace being imported.</param>
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
        public SyntaxNode NamespaceImportDeclaration(string name)
        {
            return NamespaceImportDeclaration(DottedName(name));
        }

        /// <summary>
        /// Creates an attribute.
        /// </summary>
        public abstract SyntaxNode Attribute(SyntaxNode name, IEnumerable<SyntaxNode> attributeArguments = null);

        /// <summary>
        /// Creates an attribute.
        /// </summary>
        public SyntaxNode Attribute(string name, IEnumerable<SyntaxNode> attributeArguments = null)
        {
            return Attribute(DottedName(name), attributeArguments);
        }

        /// <summary>
        /// Creates an attribute.
        /// </summary>
        public SyntaxNode Attribute(string name, params SyntaxNode[] attributeArguments)
        {
            return Attribute(name, (IEnumerable<SyntaxNode>)attributeArguments);
        }

        /// <summary>
        /// Creates an attribute matching existing attribute data.
        /// </summary>
        public SyntaxNode Attribute(AttributeData attribute)
        {
            return Attribute(
                name: TypeExpression(attribute.AttributeClass),
                attributeArguments:
                    attribute.ConstructorArguments.Select(a => this.AttributeArgument(this.LiteralExpression(a.Value)))
                    .Concat(attribute.NamedArguments.Select(n => this.AttributeArgument(n.Key, this.LiteralExpression(n.Value.Value)))));
        }

        private IEnumerable<SyntaxNode> GetSymbolAttributes(ISymbol symbol)
        {
            return symbol.GetAttributes().Select(a => Attribute(a));
        }

        /// <summary>
        /// Creates an attribute argument.
        /// </summary>
        public abstract SyntaxNode AttributeArgument(string name, SyntaxNode expression);

        /// <summary>
        /// Creates an attribute argument.
        /// </summary>
        public SyntaxNode AttributeArgument(SyntaxNode expression)
        {
            return AttributeArgument(null, expression);
        }

        /// <summary>
660
        /// Removes all attributes from the declaration, including return attributes.
M
mattwar 已提交
661
        /// </summary>
662
        public abstract SyntaxNode RemoveAllAttributes(SyntaxNode declaration);
M
mattwar 已提交
663 664

        /// <summary>
665
        /// Gets the attributes of a declaration, not including the return attributes.
M
mattwar 已提交
666
        /// </summary>
667
        public abstract IReadOnlyList<SyntaxNode> GetAttributes(SyntaxNode declaration);
M
mattwar 已提交
668

M
mattwar 已提交
669 670 671 672 673
        /// <summary>
        /// Removes specific attributes from the declaration.
        /// </summary>
        public abstract SyntaxNode RemoveAttributes(SyntaxNode declaration, IEnumerable<SyntaxNode> attributes);

674 675 676 677 678 679 680 681 682 683 684 685 686
        /// <summary>
        /// Creates a new instance of the declaration with the attributes inserted.
        /// </summary>
        public abstract SyntaxNode InsertAttributes(SyntaxNode declaration, int index, IEnumerable<SyntaxNode> attributes);

        /// <summary>
        /// Creates a new instance of the declaration with the attributes inserted.
        /// </summary>
        public SyntaxNode InsertAttributes(SyntaxNode declaration, int index, params SyntaxNode[] attributes)
        {
            return this.InsertAttributes(declaration, index, (IEnumerable<SyntaxNode>)attributes);
        }

M
mattwar 已提交
687 688
        /// <summary>
        /// Creates a new instance of a declaration with the specified attributes added.
689
        /// </summary>
690 691 692 693
        public SyntaxNode AddAttributes(SyntaxNode declaration, IEnumerable<SyntaxNode> attributes)
        {
            return this.InsertAttributes(declaration, this.GetAttributes(declaration).Count, attributes);
        }
694 695

        /// <summary>
M
mattwar 已提交
696
        /// Creates a new instance of a declaration with the specified attributes added.
697 698 699 700 701 702
        /// </summary>
        public SyntaxNode AddAttributes(SyntaxNode declaration, params SyntaxNode[] attributes)
        {
            return AddAttributes(declaration, (IEnumerable<SyntaxNode>)attributes);
        }

703 704 705 706
        /// <summary>
        /// Gets the return attributes from the declaration.
        /// </summary>
        public abstract IReadOnlyList<SyntaxNode> GetReturnAttributes(SyntaxNode declaration);
M
mattwar 已提交
707

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
        /// <summary>
        /// Removes the specified return attributes from the declaration.
        /// </summary>
        public abstract SyntaxNode RemoveReturnAttributes(SyntaxNode declaration, IEnumerable<SyntaxNode> attributes);

        /// <summary>
        /// Creates a new instance of a method declaration with return attributes inserted.
        /// </summary>
        public abstract SyntaxNode InsertReturnAttributes(SyntaxNode declaration, int index, IEnumerable<SyntaxNode> attributes);

        /// <summary>
        /// Creates a new instance of a method declaration with return attributes inserted.
        /// </summary>
        public SyntaxNode InsertReturnAttributes(SyntaxNode declaration, int index, params SyntaxNode[] attributes)
        {
            return this.InsertReturnAttributes(declaration, index, attributes);
        }
M
mattwar 已提交
725

726
        /// <summary>
M
mattwar 已提交
727
        /// Creates a new instance of a method declaration with return attributes added.
728
        /// </summary>
729 730 731 732
        public SyntaxNode AddReturnAttributes(SyntaxNode declaration, IEnumerable<SyntaxNode> attributes)
        {
            return this.InsertReturnAttributes(declaration, this.GetReturnAttributes(declaration).Count, attributes);
        }
733 734 735 736

        /// <summary>
        /// Creates a new instance of a method declaration node with return attributes added.
        /// </summary>
737
        public SyntaxNode AddReturnAttributes(SyntaxNode declaration, params SyntaxNode[] attributes)
738
        {
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
            return AddReturnAttributes(declaration, (IEnumerable<SyntaxNode>)attributes);
        }

        /// <summary>
        /// Gets the namespace imports that are part of the declaration.
        /// </summary>
        public abstract IReadOnlyList<SyntaxNode> GetNamespaceImports(SyntaxNode declaration);

        /// <summary>
        /// Creates a new instance of the declaration with the namespace imports inserted.
        /// </summary>
        public abstract SyntaxNode InsertNamespaceImports(SyntaxNode declaration, int index, IEnumerable<SyntaxNode> imports);

        /// <summary>
        /// Creates a new instance of the declaration with the namespace imports inserted.
        /// </summary>
        public SyntaxNode InsertNamespaceImports(SyntaxNode declaration, int index, params SyntaxNode[] imports)
        {
            return this.InsertNamespaceImports(declaration, index, imports);
        }

        /// <summary>
        /// Creates a new instance of the declaration with the namespace imports added.
        /// </summary>
        public SyntaxNode AddNamespaceImports(SyntaxNode declaration, IEnumerable<SyntaxNode> imports)
        {
            return this.InsertNamespaceImports(declaration, this.GetNamespaceImports(declaration).Count, imports);
        }

        /// <summary>
        /// Creates a new instance of the declaration with the namespace imports added.
        /// </summary>
        public SyntaxNode AddNamespaceImports(SyntaxNode declaration, params SyntaxNode[] imports)
        {
            return this.AddNamespaceImports(declaration, (IEnumerable<SyntaxNode>)imports);
        }

        /// <summary>
        /// Creates a new instance of the declaration with the specified namespace imports removed.
        /// </summary>
        public SyntaxNode RemoveNamespaceImports(SyntaxNode declaration, IEnumerable<SyntaxNode> imports)
        {
            return declaration.RemoveNodes(imports, DefaultRemoveOptions);
        }

        /// <summary>
        /// Creates a new instance of the declaration with the specified namespace imports removed.
        /// </summary>
        public SyntaxNode RemoveNamespaceImports(SyntaxNode declaration, params SyntaxNode[] imports)
        {
            return this.RemoveNamespaceImports(declaration, (IEnumerable<SyntaxNode>)imports);
790 791
        }

M
mattwar 已提交
792 793 794 795 796 797
        /// <summary>
        /// Gets the current members of the declaration.
        /// </summary>
        public abstract IReadOnlyList<SyntaxNode> GetMembers(SyntaxNode declaration);

        /// <summary>
798 799 800 801 802 803 804 805 806
        /// Creates a new instance of the declaration with the members removed.
        /// </summary>
        public SyntaxNode RemoveMembers(SyntaxNode declaration, IEnumerable<SyntaxNode> members)
        {
            return declaration.RemoveNodes(members, DefaultRemoveOptions);
        }

        /// <summary>
        /// Creates a new instance of the declaration with the members removed.
M
mattwar 已提交
807
        /// </summary>
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
        public SyntaxNode RemoveMembers(SyntaxNode declaration, params SyntaxNode[] members)
        {
            return this.RemoveMembers(declaration, (IEnumerable<SyntaxNode>)members);
        }

        /// <summary>
        /// Creates a new instance of the declaration with the members inserted.
        /// </summary>
        public abstract SyntaxNode InsertMembers(SyntaxNode declaration, int index, IEnumerable<SyntaxNode> members);

        /// <summary>
        /// Creates a new instance of the declaration with the members inserted.
        /// </summary>
        public SyntaxNode InsertMembers(SyntaxNode declaration, int index, params SyntaxNode[] members)
        {
            return this.InsertMembers(declaration, index, (IEnumerable<SyntaxNode>)members);
        }
M
mattwar 已提交
825 826 827 828

        /// <summary>
        /// Creates a new instance of the declaration with the members added to the end.
        /// </summary>
829 830 831 832
        public SyntaxNode AddMembers(SyntaxNode declaration, IEnumerable<SyntaxNode> members)
        {
            return this.InsertMembers(declaration, this.GetMembers(declaration).Count, members);
        }
M
mattwar 已提交
833

M
mattwar 已提交
834 835 836 837 838 839 840 841
        /// <summary>
        /// Creates a new instance of the declaration with the members added to the end.
        /// </summary>
        public SyntaxNode AddMembers(SyntaxNode declaration, params SyntaxNode[] members)
        {
            return this.AddMembers(declaration, (IEnumerable<SyntaxNode>)members);
        }

M
mattwar 已提交
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
        /// <summary>
        /// Gets the accessibility of the declaration.
        /// </summary>
        public abstract Accessibility GetAccessibility(SyntaxNode declaration);

        /// <summary>
        /// Changes the accessibility of the declaration.
        /// </summary>
        public abstract SyntaxNode WithAccessibility(SyntaxNode declaration, Accessibility accessibility);

        /// <summary>
        /// Gets the <see cref="DeclarationModifiers"/> for the declaration.
        /// </summary>
        public abstract DeclarationModifiers GetModifiers(SyntaxNode declaration);

        /// <summary>
        /// Changes the <see cref="DeclarationModifiers"/> for the declaration.
        /// </summary>
        public abstract SyntaxNode WithModifiers(SyntaxNode declaration, DeclarationModifiers modifiers);

        /// <summary>
        /// Gets the <see cref="DeclarationKind"/> for the declaration.
        /// </summary>
        public abstract DeclarationKind GetDeclarationKind(SyntaxNode declaration);

        /// <summary>
        /// Gets the name of the declaration.
        /// </summary>
        public abstract string GetName(SyntaxNode declaration);

        /// <summary>
        /// Changes the name of the declaration.
        /// </summary>
        public abstract SyntaxNode WithName(SyntaxNode declaration, string name);

        /// <summary>
        /// Gets the type of the declaration.
        /// </summary>
        public abstract SyntaxNode GetType(SyntaxNode declaration);

        /// <summary>
        /// Changes the type of the declaration.
        /// </summary>
        public abstract SyntaxNode WithType(SyntaxNode declaration, SyntaxNode type);

        /// <summary>
        /// Gets the list of parameters for the declaration.
        /// </summary>
        public abstract IReadOnlyList<SyntaxNode> GetParameters(SyntaxNode declaration);

        /// <summary>
893
        /// Inserts the parameters at the specified index into the declaration.
M
mattwar 已提交
894
        /// </summary>
895
        public abstract SyntaxNode InsertParameters(SyntaxNode declaration, int index, IEnumerable<SyntaxNode> parameters);
M
mattwar 已提交
896 897

        /// <summary>
898
        /// Adds the parameters to the declaration.
M
mattwar 已提交
899
        /// </summary>
900 901 902 903 904 905 906 907 908 909 910 911
        public SyntaxNode AddParameters(SyntaxNode declaration, IEnumerable<SyntaxNode> parameters)
        {
            return this.InsertParameters(declaration, this.GetParameters(declaration).Count, parameters);
        }

        /// <summary>
        /// Removes the specified parameters from the declaration.
        /// </summary>
        public SyntaxNode RemoveParameters(SyntaxNode declaration, IEnumerable<SyntaxNode> parameters)
        {
            return declaration.RemoveNodes(parameters, DefaultRemoveOptions);
        }
M
mattwar 已提交
912 913

        /// <summary>
914
        /// Gets the expression associated with the declaration.
M
mattwar 已提交
915
        /// </summary>
916 917 918 919 920 921
        public abstract SyntaxNode GetExpression(SyntaxNode declaration);

        /// <summary>
        /// Changes the expression associated with the declaration.
        /// </summary>
        public abstract SyntaxNode WithExpression(SyntaxNode declaration, SyntaxNode expression);
M
mattwar 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958

        /// <summary>
        /// Gets the statements for the body of the declaration.
        /// </summary>
        public abstract IReadOnlyList<SyntaxNode> GetStatements(SyntaxNode declaration);

        /// <summary>
        /// Changes the statements for the body of the declaration.
        /// </summary>
        public abstract SyntaxNode WithStatements(SyntaxNode declaration, IEnumerable<SyntaxNode> statements);

        /// <summary>
        /// Gets the statements for the body of the get-accessor of the declaration.
        /// </summary>
        public abstract IReadOnlyList<SyntaxNode> GetGetAccessorStatements(SyntaxNode declaration);

        /// <summary>
        /// Changes the statements for the body of the get-accessor of the declaration.
        /// </summary>
        public abstract SyntaxNode WithGetAccessorStatements(SyntaxNode declaration, IEnumerable<SyntaxNode> statements);

        /// <summary>
        /// Gets the statements for the body of the set-accessor of the declaration.
        /// </summary>
        public abstract IReadOnlyList<SyntaxNode> GetSetAccessorStatements(SyntaxNode declaration);

        /// <summary>
        /// Changes the statements for the body of the set-accessor of the declaration.
        /// </summary>
        public abstract SyntaxNode WithSetAccessorStatements(SyntaxNode declaration, IEnumerable<SyntaxNode> statements);

        #endregion

        #region Utility

        protected static SyntaxNode PreserveTrivia<TNode>(TNode node, Func<TNode, SyntaxNode> nodeChanger) where TNode : SyntaxNode
        {
959
            var nodeWithoutTrivia = node.WithoutLeadingTrivia().WithoutTrailingTrivia();
M
mattwar 已提交
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998

            var changedNode = nodeChanger(nodeWithoutTrivia);

            if (changedNode == nodeWithoutTrivia)
            {
                return node;
            }
            else
            {
                return changedNode
                    .WithLeadingTrivia(node.GetLeadingTrivia().Concat(changedNode.GetLeadingTrivia()))
                    .WithTrailingTrivia(changedNode.GetTrailingTrivia().Concat(node.GetTrailingTrivia()));
            }
        }

        protected static SyntaxNode ReplaceWithTrivia(SyntaxNode root, SyntaxNode original, SyntaxNode replacement)
        {
            var combinedTriviaReplacement =
                replacement.WithLeadingTrivia(original.GetLeadingTrivia().AddRange(replacement.GetLeadingTrivia()))
                           .WithTrailingTrivia(replacement.GetTrailingTrivia().AddRange(original.GetTrailingTrivia()));

            return root.ReplaceNode(original, combinedTriviaReplacement);
        }

        protected static SyntaxNode ReplaceWithTrivia<TNode>(SyntaxNode root, TNode original, Func<TNode, SyntaxNode> replacer)
            where TNode : SyntaxNode
        {
            return ReplaceWithTrivia(root, original, replacer(original));
        }

        protected static SyntaxNode ReplaceWithTrivia(SyntaxNode root, SyntaxToken original, SyntaxToken replacement)
        {
            var combinedTriviaReplacement =
                replacement.WithLeadingTrivia(original.LeadingTrivia.AddRange(replacement.LeadingTrivia))
                           .WithTrailingTrivia(replacement.TrailingTrivia.AddRange(original.TrailingTrivia));

            return root.ReplaceToken(original, combinedTriviaReplacement);
        }

M
mattwar 已提交
999 1000 1001 1002 1003 1004 1005
        protected IEnumerable<TNode> ClearTrivia<TNode>(IEnumerable<TNode> nodes) where TNode : SyntaxNode
        {
            return nodes != null ? nodes.Select(n => ClearTrivia(n)) : null;
        }

        protected abstract TNode ClearTrivia<TNode>(TNode node) where TNode : SyntaxNode;

1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
        #endregion

        #region Statements
        /// <summary>
        /// Creates statement that allows an expression to execute in a statement context.
        /// This is typically an invocation or assignment expression.
        /// </summary>
        /// <param name="expression">The expression that is to be executed. This is usually a method invocation expression.</param>
        public abstract SyntaxNode ExpressionStatement(SyntaxNode expression);

        /// <summary>
        /// Creates a statement that can be used to return a value from a method body.
        /// </summary>
        /// <param name="expression">An optional expression that can be returned.</param>
        public abstract SyntaxNode ReturnStatement(SyntaxNode expression = null);

        /// <summary>
        /// Creates a statement that can be used to throw and exception.
        /// </summary>
        /// <param name="expression">An optional expression that can be thrown.</param>
        public abstract SyntaxNode ThrowStatement(SyntaxNode expression = null);

        /// <summary>
        /// Creates a statement that declares a single local variable.
        /// </summary>
        public abstract SyntaxNode LocalDeclarationStatement(SyntaxNode type, string identifier, SyntaxNode initializer = null, bool isConst = false);

        /// <summary>
        /// Creates a statement that declares a single local variable.
        /// </summary>
        public SyntaxNode LocalDeclarationStatement(ITypeSymbol type, string name, SyntaxNode initializer = null, bool isConst = false)
        {
            return LocalDeclarationStatement(TypeExpression(type), name, initializer, isConst);
        }

        /// <summary>
        /// Creates a statement that declares a single local variable.
        /// </summary>
        public SyntaxNode LocalDeclarationStatement(string name, SyntaxNode initializer)
        {
            return LocalDeclarationStatement((SyntaxNode)null, name, initializer);
        }

        /// <summary>
M
mattwar 已提交
1050
        /// Creates an if-statement
1051
        /// </summary>
M
mattwar 已提交
1052
        /// <param name="condition">A condition expression.</param>
1053 1054 1055 1056 1057
        /// <param name="trueStatements">The statements that are executed if the condition is true.</param>
        /// <param name="falseStatements">The statements that are executed if the condition is false.</param>
        public abstract SyntaxNode IfStatement(SyntaxNode condition, IEnumerable<SyntaxNode> trueStatements, IEnumerable<SyntaxNode> falseStatements = null);

        /// <summary>
M
mattwar 已提交
1058
        /// Creates an if statement
1059
        /// </summary>
M
mattwar 已提交
1060
        /// <param name="condition">A condition expression.</param>
1061
        /// <param name="trueStatements">The statements that are executed if the condition is true.</param>
M
mattwar 已提交
1062
        /// <param name="falseStatement">A single statement that is executed if the condition is false.</param>
1063 1064 1065 1066 1067 1068
        public SyntaxNode IfStatement(SyntaxNode condition, IEnumerable<SyntaxNode> trueStatements, SyntaxNode falseStatement)
        {
            return IfStatement(condition, trueStatements, new[] { falseStatement });
        }

        /// <summary>
M
mattwar 已提交
1069
        /// Creates a switch statement that branches to individual sections based on the value of the specified expression.
1070 1071 1072 1073
        /// </summary>
        public abstract SyntaxNode SwitchStatement(SyntaxNode expression, IEnumerable<SyntaxNode> sections);

        /// <summary>
M
mattwar 已提交
1074
        /// Creates a switch statement that branches to individual sections based on the value of the specified expression.
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
        /// </summary>
        public SyntaxNode SwitchStatement(SyntaxNode expression, params SyntaxNode[] sections)
        {
            return SwitchStatement(expression, (IEnumerable<SyntaxNode>)sections);
        }

        /// <summary>
        /// Creates a section for a switch statement.
        /// </summary>
        public abstract SyntaxNode SwitchSection(IEnumerable<SyntaxNode> caseExpressions, IEnumerable<SyntaxNode> statements);

        /// <summary>
        /// Creates a single-case section a switch statement.
        /// </summary>
        public SyntaxNode SwitchSection(SyntaxNode caseExpression, IEnumerable<SyntaxNode> statements)
        {
            return SwitchSection(new[] { caseExpression }, statements);
        }

        /// <summary>
        /// Creates a default section for a switch statement.
        /// </summary>
        public abstract SyntaxNode DefaultSwitchSection(IEnumerable<SyntaxNode> statements);

        /// <summary>
        /// Create a statement that exits a switch statement and continues after it.
        /// </summary>
        public abstract SyntaxNode ExitSwitchStatement();

        /// <summary>
        /// Creates a statement that represents a using-block pattern.
        /// </summary>
        public abstract SyntaxNode UsingStatement(SyntaxNode type, string name, SyntaxNode expression, IEnumerable<SyntaxNode> statements);

        /// <summary>
        /// Creates a statement that represents a using-block pattern.
        /// </summary>
        public SyntaxNode UsingStatement(string name, SyntaxNode expression, IEnumerable<SyntaxNode> statements)
        {
            return UsingStatement(null, name, expression, statements);
        }

        /// <summary>
        /// Creates a statement that represents a using-block pattern.
        /// </summary>
        public abstract SyntaxNode UsingStatement(SyntaxNode expression, IEnumerable<SyntaxNode> statements);

        /// <summary>
        /// Creates a try-catch or try-catch-finally statement.
        /// </summary>
        public abstract SyntaxNode TryCatchStatement(IEnumerable<SyntaxNode> tryStatements, IEnumerable<SyntaxNode> catchClauses, IEnumerable<SyntaxNode> finallyStatements = null);

        /// <summary>
        /// Creates a try-catch or try-catch-finally statement.
        /// </summary>
        public SyntaxNode TryCatchStatement(IEnumerable<SyntaxNode> tryStatements, params SyntaxNode[] catchClauses)
        {
            return TryCatchStatement(tryStatements, (IEnumerable<SyntaxNode>)catchClauses);
        }

        /// <summary>
        /// Creates a try-finally statement.
        /// </summary>
        public SyntaxNode TryFinallyStatement(IEnumerable<SyntaxNode> tryStatements, IEnumerable<SyntaxNode> finallyStatements)
        {
            return TryCatchStatement(tryStatements, catchClauses: null, finallyStatements: finallyStatements);
        }

        /// <summary>
        /// Creates a catch-clause.
        /// </summary>
        public abstract SyntaxNode CatchClause(SyntaxNode type, string identifier, IEnumerable<SyntaxNode> statements);

        /// <summary>
        /// Creates a catch-clause.
        /// </summary>
        public SyntaxNode CatchClause(ITypeSymbol type, string identifer, IEnumerable<SyntaxNode> statements)
        {
            return CatchClause(TypeExpression(type), identifer, statements);
        }

        /// <summary>
        /// Creates a while-loop statement
        /// </summary>
        public abstract SyntaxNode WhileStatement(SyntaxNode condition, IEnumerable<SyntaxNode> statements);

        #endregion

        #region Expressions
        /// <summary>
        /// An expression that represents the default value of a type.
        /// This is typically a null value for reference types or a zero-filled value for value types.
        /// </summary>
        public abstract SyntaxNode DefaultExpression(SyntaxNode type);
        public abstract SyntaxNode DefaultExpression(ITypeSymbol type);

        /// <summary>
M
mattwar 已提交
1172
        /// Creates an expression that denotes the containing method's this-parameter.
1173 1174 1175 1176
        /// </summary>
        public abstract SyntaxNode ThisExpression();

        /// <summary>
M
mattwar 已提交
1177
        /// Creates an expression that denotes the containing method's base-parameter.
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
        /// </summary>
        public abstract SyntaxNode BaseExpression();

        /// <summary>
        /// Creates a literal expression. This is typically numeric primitives, strings or chars.
        /// </summary>
        public abstract SyntaxNode LiteralExpression(object value);

        /// <summary>
        /// Creates an expression that denotes the boolean false literal.
        /// </summary>
        public SyntaxNode FalseLiteralExpression()
        {
            return LiteralExpression(false);
        }

        /// <summary>
        /// Creates an expression that denotes the boolean true literal.
        /// </summary>
        public SyntaxNode TrueLiteralExpression()
        {
            return LiteralExpression(true);
        }

        /// <summary>
        /// Creates an expression that denotes the null literal.
        /// </summary>
        public SyntaxNode NullLiteralExpression()
        {
            return LiteralExpression(null);
        }

        /// <summary>
        /// Creates an expression that denotes a simple identifier name.
        /// </summary>
        /// <param name="identifier"></param>
        /// <returns></returns>
        public abstract SyntaxNode IdentifierName(string identifier);

        /// <summary>
        /// Creates an expression that denotes a generic identifier name.
        /// </summary>
        public abstract SyntaxNode GenericName(string identifier, IEnumerable<SyntaxNode> typeArguments);

        /// <summary>
        /// Creates an expression that denotes a generic identifier name.
        /// </summary>
        public SyntaxNode GenericName(string identifier, IEnumerable<ITypeSymbol> typeArguments)
        {
            return GenericName(identifier, typeArguments.Select(ta => TypeExpression(ta)));
        }

        /// <summary>
        /// Creates an expression that denotes a generic identifier name.
        /// </summary>
        public SyntaxNode GenericName(string identifier, params SyntaxNode[] typeArguments)
        {
            return GenericName(identifier, (IEnumerable<SyntaxNode>)typeArguments);
        }

        /// <summary>
        /// Creates an expression that denotes a generic identifier name.
        /// </summary>
        public SyntaxNode GenericName(string identifier, params ITypeSymbol[] typeArguments)
        {
            return GenericName(identifier, (IEnumerable<ITypeSymbol>)typeArguments);
        }

        /// <summary>
        /// Converts an expression that ends in a name into an expression that ends in a generic name.
        /// If the expression already ends in a generic name, the new type arguments are used instead.
        /// </summary>
1250
        public abstract SyntaxNode WithTypeArguments(SyntaxNode expression, IEnumerable<SyntaxNode> typeArguments);
1251 1252 1253 1254 1255

        /// <summary>
        /// Converts an expression that ends in a name into an expression that ends in a generic name.
        /// If the expression already ends in a generic name, the new type arguments are used instead.
        /// </summary>
1256
        public SyntaxNode WithTypeArguments(SyntaxNode expression, params SyntaxNode[] typeArguments)
1257
        {
1258
            return WithTypeArguments(expression, (IEnumerable<SyntaxNode>)typeArguments);
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
        }

        /// <summary>
        /// Creates a name expression that denotes a qualified name. 
        /// The left operand can be any name expression.
        /// The right operand can be either and identifier or generic name.
        /// </summary>
        public abstract SyntaxNode QualifiedName(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates a name expression from a dotted name string.
        /// </summary>
        public SyntaxNode DottedName(string dottedName)
        {
            if (dottedName == null)
            {
                throw new ArgumentNullException("dottedName");
            }

            var parts = dottedName.Split(dotSeparator);

            SyntaxNode name = null;
            foreach (var part in parts)
            {
                if (name == null)
                {
                    name = IdentifierName(part);
                }
                else
                {
M
mattwar 已提交
1289
                    name = QualifiedName(name, IdentifierName(part)).WithAdditionalAnnotations(Simplification.Simplifier.Annotation);
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
                }
            }

            return name;
        }

        private static readonly char[] dotSeparator = new char[] { '.' };

        /// <summary>
        /// Creates an expression that denotes a type.
        /// </summary>
        public abstract SyntaxNode TypeExpression(ITypeSymbol typeSymbol);

        /// <summary>
        /// Creates an expression that denotes a special type name.
        /// </summary>
        public abstract SyntaxNode TypeExpression(SpecialType specialType);

        /// <summary>
        /// Creates an expression that denotes an array type.
        /// </summary>
        public abstract SyntaxNode ArrayTypeExpression(SyntaxNode type);

        /// <summary>
        /// Creates an expression that denotes a nullable type.
        /// </summary>
        public abstract SyntaxNode NullableTypeExpression(SyntaxNode type);

        /// <summary>
        /// Creates an expression that denotes an assignment from the right argument to left argument.
        /// </summary>
        public abstract SyntaxNode AssignmentStatement(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a value-type equality test operation.
        /// </summary>
        public abstract SyntaxNode ValueEqualsExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a reference-type equality test operation.
        /// </summary>
        public abstract SyntaxNode ReferenceEqualsExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a value-type inequality test operation.
        /// </summary>
        public abstract SyntaxNode ValueNotEqualsExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a reference-type inequality test operation.
        /// </summary>
        public abstract SyntaxNode ReferenceNotEqualsExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a less-than test operation.
        /// </summary>
        public abstract SyntaxNode LessThanExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a less-than-or-equal test operation.
        /// </summary>
        public abstract SyntaxNode LessThanOrEqualExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a greater-than test operation.
        /// </summary>
        public abstract SyntaxNode GreaterThanExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a greater-than-or-equal test operation.
        /// </summary>
        public abstract SyntaxNode GreaterThanOrEqualExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a unary negation operation.
        /// </summary>
        public abstract SyntaxNode NegateExpression(SyntaxNode expression);

        /// <summary>
        /// Creates an expression that denotes an addition operation.
        /// </summary>
        public abstract SyntaxNode AddExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes an subtraction operation.
        /// </summary>
        public abstract SyntaxNode SubtractExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a multiplication operation.
        /// </summary>
        public abstract SyntaxNode MultiplyExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a division operation.
        /// </summary>
        public abstract SyntaxNode DivideExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a modulo operation.
        /// </summary>
        public abstract SyntaxNode ModuloExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a bitwise-and operation.
        /// </summary>
        public abstract SyntaxNode BitwiseAndExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a bitwise-or operation.
        /// </summary>
        public abstract SyntaxNode BitwiseOrExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a bitwise-not operation
        /// </summary>
        public abstract SyntaxNode BitwiseNotExpression(SyntaxNode operand);

        /// <summary>
        /// Creates an expression that denotes a logical-and operation.
        /// </summary>
        public abstract SyntaxNode LogicalAndExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a logical-or operation.
        /// </summary>
        public abstract SyntaxNode LogicalOrExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates an expression that denotes a logical not operation.
        /// </summary>
        public abstract SyntaxNode LogicalNotExpression(SyntaxNode expression);

        /// <summary>
        /// Creates an expression that denotes a conditional evaluation operation.
        /// </summary>
        public abstract SyntaxNode ConditionalExpression(SyntaxNode condition, SyntaxNode whenTrue, SyntaxNode whenFalse);

        /// <summary>
1429
        /// Creates an expression that denotes a coalesce operation. 
1430 1431 1432 1433 1434 1435
        /// </summary>
        public abstract SyntaxNode CoalesceExpression(SyntaxNode left, SyntaxNode right);

        /// <summary>
        /// Creates a member access expression.
        /// </summary>
M
mattwar 已提交
1436
        public abstract SyntaxNode MemberAccessExpression(SyntaxNode expression, SyntaxNode memberName);
1437 1438 1439 1440

        /// <summary>
        /// Creates a member access expression.
        /// </summary>
M
mattwar 已提交
1441
        public SyntaxNode MemberAccessExpression(SyntaxNode expression, string memberName)
1442
        {
M
mattwar 已提交
1443
            return MemberAccessExpression(expression, IdentifierName(memberName));
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
        }

        /// <summary>
        /// Creates an object creation expression.
        /// </summary>
        public abstract SyntaxNode ObjectCreationExpression(SyntaxNode namedType, IEnumerable<SyntaxNode> arguments);

        /// <summary>
        /// Creates an object creation expression.
        /// </summary>
        public SyntaxNode ObjectCreationExpression(ITypeSymbol type, IEnumerable<SyntaxNode> arguments)
        {
            return ObjectCreationExpression(TypeExpression(type), arguments);
        }

        /// <summary>
        /// Creates an object creation expression.
        /// </summary>
        public SyntaxNode ObjectCreationExpression(SyntaxNode type, params SyntaxNode[] arguments)
        {
            return ObjectCreationExpression(type, (IEnumerable<SyntaxNode>)arguments);
        }

        /// <summary>
        /// Creates an object creation expression.
        /// </summary>
        public SyntaxNode ObjectCreationExpression(ITypeSymbol type, params SyntaxNode[] arguments)
        {
            return ObjectCreationExpression(type, (IEnumerable<SyntaxNode>)arguments);
        }

        /// <summary>
        /// Creates a invocation expression.
        /// </summary>
        public abstract SyntaxNode InvocationExpression(SyntaxNode expression, IEnumerable<SyntaxNode> arguments);

        /// <summary>
        /// Creates a invocation expression
        /// </summary>
        public SyntaxNode InvocationExpression(SyntaxNode expression, params SyntaxNode[] arguments)
        {
            return InvocationExpression(expression, (IEnumerable<SyntaxNode>)arguments);
        }

        /// <summary>
        /// Creates a node that is an argument to an invocation.
        /// </summary>
        public abstract SyntaxNode Argument(string name, RefKind refKind, SyntaxNode expression);

        /// <summary>
        /// Creates a node that is an argument to an invocation.
        /// </summary>
        public SyntaxNode Argument(RefKind refKind, SyntaxNode expression)
        {
            return Argument(null, refKind, expression);
        }

        /// <summary>
        /// Creates a node that is an argument to an invocation.
        /// </summary>
        public SyntaxNode Argument(SyntaxNode expression)
        {
            return Argument(null, RefKind.None, expression);
        }

        /// <summary>
        /// Creates an expression that access an element of an array or indexer.
        /// </summary>
        public abstract SyntaxNode ElementAccessExpression(SyntaxNode expression, IEnumerable<SyntaxNode> arguments);

        /// <summary>
        /// Creates an expression that access an element of an array or indexer.
        /// </summary>
        public SyntaxNode ElementAccessExpression(SyntaxNode expression, params SyntaxNode[] arguments)
        {
            return ElementAccessExpression(expression, (IEnumerable<SyntaxNode>)arguments);
        }

        /// <summary>
        /// Creates an expression that denotes an is-type-check operation.
        /// </summary>
M
mattwar 已提交
1525
        public abstract SyntaxNode IsTypeExpression(SyntaxNode expression, SyntaxNode type);
1526 1527 1528 1529

        /// <summary>
        /// Creates an expression that denotes an is-type-check operation.
        /// </summary>
M
mattwar 已提交
1530
        public SyntaxNode IsTypeExpression(SyntaxNode expression, ITypeSymbol type)
1531
        {
M
mattwar 已提交
1532
            return IsTypeExpression(expression, TypeExpression(type));
1533 1534 1535
        }

        /// <summary>
M
mattwar 已提交
1536
        /// Creates an expression that denotes an try-cast operation.
1537
        /// </summary>
M
mattwar 已提交
1538
        public abstract SyntaxNode TryCastExpression(SyntaxNode expression, SyntaxNode type);
1539 1540

        /// <summary>
M
mattwar 已提交
1541
        /// Creates an expression that denotes an try-cast operation.
1542
        /// </summary>
M
mattwar 已提交
1543
        public SyntaxNode TryCastExpression(SyntaxNode expression, ITypeSymbol type)
1544
        {
M
mattwar 已提交
1545
            return TryCastExpression(expression, TypeExpression(type));
1546 1547 1548
        }

        /// <summary>
M
mattwar 已提交
1549
        /// Creates an expression that denotes a type cast operation.
1550 1551 1552 1553
        /// </summary>
        public abstract SyntaxNode CastExpression(SyntaxNode type, SyntaxNode expression);

        /// <summary>
M
mattwar 已提交
1554
        /// Creates an expression that denotes a type cast operation.
1555 1556 1557 1558 1559 1560 1561
        /// </summary>
        public SyntaxNode CastExpression(ITypeSymbol type, SyntaxNode expression)
        {
            return CastExpression(TypeExpression(type), expression);
        }

        /// <summary>
M
mattwar 已提交
1562
        /// Creates an expression that denotes a type conversion operation.
1563 1564 1565 1566
        /// </summary>
        public abstract SyntaxNode ConvertExpression(SyntaxNode type, SyntaxNode expression);

        /// <summary>
M
mattwar 已提交
1567
        /// Creates an expression that denotes a type conversion operation.
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
        /// </summary>
        public SyntaxNode ConvertExpression(ITypeSymbol type, SyntaxNode expression)
        {
            return ConvertExpression(TypeExpression(type), expression);
        }

        /// <summary>
        /// Creates an expression that declares a value returning lambda expression.
        /// </summary>
        public abstract SyntaxNode ValueReturningLambdaExpression(IEnumerable<SyntaxNode> lambdaParameters, SyntaxNode expression);

        /// <summary>
        /// Creates an expression that declares a void returning lambda expression
        /// </summary>
        public abstract SyntaxNode VoidReturningLambdaExpression(IEnumerable<SyntaxNode> lambdaParameters, SyntaxNode expression);

        /// <summary>
        /// Creates an expression that declares a value returning lambda expression.
        /// </summary>
        public abstract SyntaxNode ValueReturningLambdaExpression(IEnumerable<SyntaxNode> lambdaParameters, IEnumerable<SyntaxNode> statements);

        /// <summary>
        /// Creates an expression that declares a void returning lambda expression.
        /// </summary>
        public abstract SyntaxNode VoidReturningLambdaExpression(IEnumerable<SyntaxNode> lambdaParameters, IEnumerable<SyntaxNode> statements);

        /// <summary>
        /// Creates an expression that declares a single parameter value returning lambda expression.
        /// </summary>
        public SyntaxNode ValueReturningLambdaExpression(string parameterName, SyntaxNode expression)
        {
            return ValueReturningLambdaExpression(new[] { LambdaParameter(parameterName) }, expression);
        }

        /// <summary>
        /// Creates an expression that declares a single parameter void returning lambda expression.
        /// </summary>
        public SyntaxNode VoidReturningLambdaExpression(string parameterName, SyntaxNode expression)
        {
            return VoidReturningLambdaExpression(new[] { LambdaParameter(parameterName) }, expression);
        }

        /// <summary>
        /// Creates an expression that declares a single parameter value returning lambda expression.
        /// </summary>
        public SyntaxNode ValueReturningLambdaExpression(string parameterName, IEnumerable<SyntaxNode> statements)
        {
            return ValueReturningLambdaExpression(new[] { LambdaParameter(parameterName) }, statements);
        }

        /// <summary>
        /// Creates an expression that declares a single parameter void returning lambda expression.
        /// </summary>
        public SyntaxNode VoidReturningLambdaExpression(string parameterName, IEnumerable<SyntaxNode> statements)
        {
            return VoidReturningLambdaExpression(new[] { LambdaParameter(parameterName) }, statements);
        }

M
mattwar 已提交
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
        /// <summary>
        /// Creates an expression that declares a zero parameter value returning lambda expression.
        /// </summary>
        public SyntaxNode ValueReturningLambdaExpression(SyntaxNode expression)
        {
            return ValueReturningLambdaExpression((IEnumerable<SyntaxNode>)null, expression);
        }

        /// <summary>
        /// Creates an expression that declares a zero parameter void returning lambda expression.
        /// </summary>
        public SyntaxNode VoidReturningLambdaExpression(SyntaxNode expression)
        {
            return VoidReturningLambdaExpression((IEnumerable<SyntaxNode>)null, expression);
        }

        /// <summary>
        /// Creates an expression that declares a zero parameter value returning lambda expression.
        /// </summary>
        public SyntaxNode ValueReturningLambdaExpression(IEnumerable<SyntaxNode> statements)
        {
            return ValueReturningLambdaExpression((IEnumerable<SyntaxNode>)null, statements);
        }

        /// <summary>
        /// Creates an expression that declares a zero parameter void returning lambda expression.
        /// </summary>
        public SyntaxNode VoidReturningLambdaExpression(IEnumerable<SyntaxNode> statements)
        {
            return VoidReturningLambdaExpression((IEnumerable<SyntaxNode>)null, statements);
        }

1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
        /// <summary>
        /// Creates a lambda parameter.
        /// </summary>
        public abstract SyntaxNode LambdaParameter(string identifier, SyntaxNode type = null);

        /// <summary>
        /// Creates a lambda parameter.
        /// </summary>
        public SyntaxNode LambdaParameter(string identifier, ITypeSymbol type)
        {
            return LambdaParameter(identifier, TypeExpression(type));
        }
        #endregion
    }
}