internal override IMethodSymbol GetDelegatingConstructor( SemanticDocument document, ObjectCreationExpressionSyntax objectCreation, INamedTypeSymbol namedType, ISet <IMethodSymbol> candidates, CancellationToken cancellationToken) { var model = document.SemanticModel; var oldNode = objectCreation .AncestorsAndSelf(ascendOutOfTrivia: false) .Where(node => SpeculationAnalyzer.CanSpeculateOnNode(node)) .LastOrDefault(); var typeNameToReplace = objectCreation.Type; var newTypeName = namedType.GenerateTypeSyntax(); var newObjectCreation = objectCreation.WithType(newTypeName).WithAdditionalAnnotations(s_annotation); var newNode = oldNode.ReplaceNode(objectCreation, newObjectCreation); var speculativeModel = SpeculationAnalyzer.CreateSpeculativeSemanticModelForNode(oldNode, newNode, model); if (speculativeModel != null) { newObjectCreation = (ObjectCreationExpressionSyntax)newNode.GetAnnotatedNodes(s_annotation).Single(); var symbolInfo = speculativeModel.GetSymbolInfo(newObjectCreation, cancellationToken); var parameterTypes = newObjectCreation.ArgumentList.Arguments.Select( a => a.DetermineParameterType(speculativeModel, cancellationToken)).ToList(); return(GenerateConstructorHelpers.GetDelegatingConstructor( document, symbolInfo, candidates, namedType, parameterTypes)); } return(null); }
protected override SemanticModel GetSpeculativeSemanticModel(ref SyntaxNode nodeToSpeculate, SemanticModel originalSemanticModel, SyntaxNode originalNode) { var syntaxNodeToSpeculate = nodeToSpeculate; Contract.ThrowIfNull(syntaxNodeToSpeculate); Contract.ThrowIfFalse(SpeculationAnalyzer.CanSpeculateOnNode(nodeToSpeculate)); return(SpeculationAnalyzer.CreateSpeculativeSemanticModelForNode(originalNode, syntaxNodeToSpeculate, (SemanticModel)originalSemanticModel)); }
public override SyntaxNode Visit(SyntaxNode node) { if (node == null) { return(node); } if (_isNodeOrTokenOutsideSimplifySpans(node)) { if (_simplifyAllDescendants) { // One of the ancestor node is within a simplification span, but this node is outside all simplification spans. // Add DontSimplifyAnnotation to node to ensure it doesn't get simplified. return(node.WithAdditionalAnnotations( SimplificationHelpers.DontSimplifyAnnotation )); } else { return(node); } } var savedSimplifyAllDescendants = _simplifyAllDescendants; _simplifyAllDescendants = _simplifyAllDescendants || node.HasAnnotation(Simplifier.Annotation); if (!_insideSpeculatedNode && SpeculationAnalyzer.CanSpeculateOnNode(node)) { if ( _simplifyAllDescendants || node.DescendantNodesAndTokens( s_containsAnnotations, descendIntoTrivia: true ) .Any(s_hasSimplifierAnnotation) ) { _insideSpeculatedNode = true; var rewrittenNode = base.Visit(node); _nodesAndTokensToReduce.Add( new NodeOrTokenToReduce(rewrittenNode, _simplifyAllDescendants, node) ); _insideSpeculatedNode = false; } } else if (node.ContainsAnnotations || savedSimplifyAllDescendants) { node = base.Visit(node); } _simplifyAllDescendants = savedSimplifyAllDescendants; return(node); }
internal override IMethodSymbol GetDelegatingConstructor( State state, SemanticDocument document, int argumentCount, INamedTypeSymbol namedType, ISet <IMethodSymbol> candidates, CancellationToken cancellationToken) { var oldToken = state.Token; var tokenKind = oldToken.Kind(); if (state.IsConstructorInitializerGeneration) { SyntaxToken thisOrBaseKeyword; SyntaxKind newCtorInitializerKind; if (tokenKind != SyntaxKind.BaseKeyword && state.TypeToGenerateIn == namedType) { thisOrBaseKeyword = SyntaxFactory.Token(SyntaxKind.ThisKeyword); newCtorInitializerKind = SyntaxKind.ThisConstructorInitializer; } else { thisOrBaseKeyword = SyntaxFactory.Token(SyntaxKind.BaseKeyword); newCtorInitializerKind = SyntaxKind.BaseConstructorInitializer; } var ctorInitializer = (ConstructorInitializerSyntax)oldToken.Parent; var oldArgumentList = ctorInitializer.ArgumentList; var newArgumentList = GetNewArgumentList(oldArgumentList, argumentCount); var newCtorInitializer = SyntaxFactory.ConstructorInitializer(newCtorInitializerKind, ctorInitializer.ColonToken, thisOrBaseKeyword, newArgumentList); SemanticModel speculativeModel; if (document.SemanticModel.TryGetSpeculativeSemanticModel(ctorInitializer.Span.Start, newCtorInitializer, out speculativeModel)) { var symbolInfo = speculativeModel.GetSymbolInfo(newCtorInitializer, cancellationToken); return(GenerateConstructorHelpers.GetDelegatingConstructor( document, symbolInfo, candidates, namedType, state.ParameterTypes)); } } else { var oldNode = oldToken.Parent .AncestorsAndSelf(ascendOutOfTrivia: false) .Where(node => SpeculationAnalyzer.CanSpeculateOnNode(node)) .LastOrDefault(); var typeNameToReplace = (TypeSyntax)oldToken.Parent; TypeSyntax newTypeName; if (namedType != state.TypeToGenerateIn) { while (true) { var parentType = typeNameToReplace.Parent as TypeSyntax; if (parentType == null) { break; } typeNameToReplace = parentType; } newTypeName = namedType.GenerateTypeSyntax().WithAdditionalAnnotations(s_annotation); } else { newTypeName = typeNameToReplace.WithAdditionalAnnotations(s_annotation); } var newNode = oldNode.ReplaceNode(typeNameToReplace, newTypeName); newTypeName = (TypeSyntax)newNode.GetAnnotatedNodes(s_annotation).Single(); var oldArgumentList = (ArgumentListSyntax)newTypeName.Parent.ChildNodes().FirstOrDefault(n => n is ArgumentListSyntax); if (oldArgumentList != null) { var newArgumentList = GetNewArgumentList(oldArgumentList, argumentCount); if (newArgumentList != oldArgumentList) { newNode = newNode.ReplaceNode(oldArgumentList, newArgumentList); newTypeName = (TypeSyntax)newNode.GetAnnotatedNodes(s_annotation).Single(); } } var speculativeModel = SpeculationAnalyzer.CreateSpeculativeSemanticModelForNode(oldNode, newNode, document.SemanticModel); if (speculativeModel != null) { var symbolInfo = speculativeModel.GetSymbolInfo(newTypeName.Parent, cancellationToken); return(GenerateConstructorHelpers.GetDelegatingConstructor( document, symbolInfo, candidates, namedType, state.ParameterTypes)); } } return(null); }