internal static bool CanSimplifyAnonymousTypeMemberName( AnonymousObjectMemberDeclaratorSyntax node ) { if (node.NameEquals == null) { return(false); } if ( RemovalCausesAmbiguity( ((AnonymousObjectCreationExpressionSyntax)node.Parent !).Initializers, node ) ) { return(false); } var inferredName = node.Expression.TryGetInferredMemberName(); if (inferredName == null || inferredName != node.NameEquals.Name.Identifier.ValueText) { return(false); } return(true); }
private static async Task FindAnonymousTypesWithMessageContractsInTree(IDictionary <AnonymousObjectCreationExpressionSyntax, ITypeSymbol> dictionary, AnonymousObjectMemberDeclaratorSyntax initializer, IPropertySymbol contractProperty, SemanticModel semanticModel) { if (initializer.Expression is ImplicitArrayCreationExpressionSyntax implicitArrayCreationExpressionSyntax) { if (contractProperty.Type.IsImmutableArray(out var contractElementType) || contractProperty.Type.IsList(out contractElementType) || contractProperty.Type.IsArray(out contractElementType)) { await FindAnonymousTypesWithMessageContractsInTree(dictionary, implicitArrayCreationExpressionSyntax, contractElementType, semanticModel) .ConfigureAwait(false); } } else if (initializer.Expression is AnonymousObjectCreationExpressionSyntax anonymousObjectProperty) { await FindAnonymousTypesWithMessageContractsInTree(dictionary, anonymousObjectProperty, contractProperty.Type, semanticModel) .ConfigureAwait(false); } else if (initializer.Expression is InvocationExpressionSyntax invocationExpressionSyntax && semanticModel.GetSymbolInfo(invocationExpressionSyntax).Symbol is IMethodSymbol method && method.ReturnType.IsList(out var methodReturnTypeArgument) && methodReturnTypeArgument.IsAnonymousType) { if (contractProperty.Type.IsImmutableArray(out var contractElementType) || contractProperty.Type.IsList(out contractElementType) || contractProperty.Type.IsArray(out contractElementType)) { await FindAnonymousTypesWithMessageContractsInTree(dictionary, methodReturnTypeArgument, contractElementType, semanticModel) .ConfigureAwait(false); } } }
// An explicit name cannot be removed if some other position would produce it as inferred name private static bool RemovalCausesAmbiguity( SeparatedSyntaxList <AnonymousObjectMemberDeclaratorSyntax> initializers, AnonymousObjectMemberDeclaratorSyntax toRemove ) { Contract.ThrowIfNull(toRemove.NameEquals); var name = toRemove.NameEquals.Name.Identifier.ValueText; foreach (var initializer in initializers) { if (initializer == toRemove) { continue; } if ( initializer.NameEquals is null && initializer.Expression.TryGetInferredMemberName()?.Equals(name) == true ) { return(true); } } return(false); }
public override Evaluation VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) { node.Expression?.Accept <Evaluation>(this); node.NameEquals?.Accept <Evaluation>(this); return(base.VisitAnonymousObjectMemberDeclarator(node)); }
public override void VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) { node.Expression?.Accept(this); node.NameEquals?.Accept(this); base.VisitAnonymousObjectMemberDeclarator(node); }
private bool ReplacementBreaksAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax originalAnonymousObjectMemberDeclarator, AnonymousObjectMemberDeclaratorSyntax replacedAnonymousObjectMemberDeclarator) { var originalExpressionType = this.OriginalSemanticModel.GetTypeInfo(originalAnonymousObjectMemberDeclarator.Expression, this.CancellationToken).Type; var newExpressionType = this.SpeculativeSemanticModel.GetTypeInfo(replacedAnonymousObjectMemberDeclarator.Expression, this.CancellationToken).Type; return(!object.Equals(originalExpressionType, newExpressionType)); }
public override void VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) { var expressionVisitor = new GetMembersVisitor(this); expressionVisitor.Visit(node.Expression); _properties.PropertiesDependences.Add(node.NameEquals == null ? ((MemberAccessExpressionSyntax)node.Expression).Name.Identifier.ValueText : node.NameEquals.Name.Identifier.ValueText, expressionVisitor._properties); }
public override LuaSyntaxNode VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) { var name = (LuaIdentifierNameSyntax)node.NameEquals.Accept(this); var expression = (LuaExpressionSyntax)node.Expression.Accept(this); return(new LuaKeyValueTableItemSyntax(new LuaTableLiteralKeySyntax(name), expression)); }
public override SyntaxNode VisitAnonymousObjectMemberDeclarator( AnonymousObjectMemberDeclaratorSyntax node ) { var nameEquals = node.NameEquals; var expression = node.Expression; var identifier = expression as IdentifierNameSyntax; if ( nameEquals != null || identifier == null || !IsReference(identifier) || HasConflict(identifier, _variableDeclarator) ) { return(base.VisitAnonymousObjectMemberDeclarator(node)); } // Special case inlining into anonymous types to ensure that we keep property names: // // E.g. // int x = 42; // var a = new { x; }; // // Should become: // var a = new { x = 42; }; nameEquals = SyntaxFactory.NameEquals(identifier); expression = (ExpressionSyntax)Visit(expression); return(node.Update(nameEquals, expression) .WithAdditionalAnnotations(Simplifier.Annotation, Formatter.Annotation)); }
public static bool IsInitializerForArrayOrCollectionCreationExpression(this SyntaxNode node) { var initializer = node as InitializerExpressionSyntax; AnonymousObjectMemberDeclaratorSyntax anonymousObjectInitializer = null; if (initializer == null) { anonymousObjectInitializer = node as AnonymousObjectMemberDeclaratorSyntax; if (anonymousObjectInitializer == null) { return(false); } } var parent = initializer != null ? initializer.Parent : anonymousObjectInitializer.Parent; if (parent is ArrayCreationExpressionSyntax || parent is ImplicitArrayCreationExpressionSyntax || parent is EqualsValueClauseSyntax || parent.Kind() == SyntaxKind.SimpleAssignmentExpression) { return(true); } if (parent is ObjectCreationExpressionSyntax) { return(!IsInitializerForObjectOrAnonymousObjectCreationExpression(initializer)); } return(false); }
public override SyntaxNode VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) { return(SimplifyNode( node, parentNode: node.Parent, newNode: base.VisitAnonymousObjectMemberDeclarator(node), simplifier: s_simplifyAnonymousTypeMemberName)); }
private GeneratedPropertyInfo CreatePropertyInfo(AnonymousObjectMemberDeclaratorSyntax initializer, int index, SemanticModel semanticModel) { INamedTypeSymbol type = GetPropertyType(initializer, semanticModel); SyntaxToken nameToken = initializer.NameEquals != null ? initializer.NameEquals.Name.Identifier : ExtractAnonymousTypeMemberName(initializer.Expression); string name = nameToken.Text.NullIfEmpty() ?? $"{type.Name}Property{index}"; return(new GeneratedPropertyInfo(type, name)); }
private static SyntaxNode SimplifyAnonymousTypeMemberName(AnonymousObjectMemberDeclaratorSyntax node, SemanticModel semanticModel, OptionSet optionSet, CancellationToken canellationToken) { if (CanSimplifyAnonymousTypeMemberName(node)) { return(node.WithNameEquals(null).WithTriviaFrom(node)); } return(node); }
private ExpressionSyntax MapInitialiser(AnonymousObjectMemberDeclaratorSyntax node) { return(AssignmentExpression( kind: SyntaxKind.SimpleAssignmentExpression, left: node.NameEquals.Name, operatorToken: node.NameEquals.EqualsToken, right: node.Expression ).WithTriviaFrom(node)); }
public override void VisitAnonymousObjectMemberDeclarator( AnonymousObjectMemberDeclaratorSyntax node ) { // Simple projection with named field (~`AS` alias) IncrementNodeCount(1); AddOperator("="); // object member assignment/initialization base.VisitAnonymousObjectMemberDeclarator(node); }
public static string Name(this AnonymousObjectMemberDeclaratorSyntax member) { if (member.NameEquals != null) { return(member.NameEquals.Name.Identifier.ValueText); } else { return(member.Expression.ToString()); } }
public override void VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) { var name = node.NameEquals?.Name; if (name != null) { FindSpellingMistakesForIdentifier(name.Identifier); } base.VisitAnonymousObjectMemberDeclarator(node); }
public static Doc Print(AnonymousObjectMemberDeclaratorSyntax node) { var docs = new List <Doc>(); if (node.NameEquals != null) { docs.Add(Token.PrintWithSuffix(node.NameEquals.Name.Identifier, " ")); docs.Add(Token.PrintWithSuffix(node.NameEquals.EqualsToken, " ")); } docs.Add(Node.Print(node.Expression)); return(Doc.Concat(docs)); }
public override void VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) { if (!PreVisit(node)) { return; } node.Expression?.Accept(this); node.NameEquals?.Accept(this); base.VisitAnonymousObjectMemberDeclarator(node); PostVisit(node); }
private static SyntaxNode SimplifyAnonymousTypeMemberName(AnonymousObjectMemberDeclaratorSyntax node, SemanticModel semanticModel, OptionSet optionSet, CancellationToken canellationToken) { if (node.NameEquals == null) { return(node); } var inferredName = node.Expression.TryGetInferredMemberName(); if (inferredName == null || inferredName != node.NameEquals.Name.Identifier.ValueText) { return(node); } return(node.WithNameEquals(null).WithTriviaFrom(node)); }
internal static bool CanSimplifyAnonymousTypeMemberName(AnonymousObjectMemberDeclaratorSyntax node) { if (node.NameEquals == null) { return(false); } var inferredName = node.Expression.TryGetInferredMemberName(); if (inferredName == null || inferredName != node.NameEquals.Name.Identifier.ValueText) { return(false); } return(true); }
static string GetName(AnonymousObjectMemberDeclaratorSyntax initializer) { string name; if (initializer.NameEquals == null) { var expression = (MemberAccessExpressionSyntax)initializer.Expression; name = expression.Name.Identifier.Text; } else { name = initializer.NameEquals.Name.Identifier.Text; } return(name); }
private Doc PrintAnonymousObjectMemberDeclaratorSyntax( AnonymousObjectMemberDeclaratorSyntax node) { var parts = new Parts(); if (node.NameEquals != null) { parts.Push( this.PrintSyntaxToken(node.NameEquals.Name.Identifier, " ") ); parts.Push( this.PrintSyntaxToken(node.NameEquals.EqualsToken, " ") ); } parts.Push(this.Print(node.Expression)); return(Concat(parts)); }
public override SyntaxNode?VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) { if (node.NameEquals == null && node.Expression is IdentifierNameSyntax identifier && _nonConflictReferences.Contains(identifier)) { // Special case inlining into anonymous types to ensure that we keep property names: // // E.g. // int x = 42; // var a = new { x; }; // // Should become: // var a = new { x = 42; }; return(node.Update(SyntaxFactory.NameEquals(identifier), (ExpressionSyntax)Visit(node.Expression))); } return(base.VisitAnonymousObjectMemberDeclarator(node)); }
private void CollectFieldNamesAndSelectsFromMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax expression) { string name; if (expression.NameEquals != null) { name = expression.NameEquals.Name.Identifier.ValueText; } else if (expression.Expression is MemberAccessExpressionSyntax memberAccess) { name = memberAccess.Name.Identifier.ValueText; } else { return; } _fieldNames.Add(name); _selectExpressions[name] = expression.Expression; }
public override SyntaxNode VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) { var nameEquals = node.NameEquals; var expression = node.Expression; var identifier = expression as IdentifierNameSyntax; if (nameEquals != null || identifier == null || !IsReference(identifier) || HasConflict(identifier, _variableDeclarator)) { return base.VisitAnonymousObjectMemberDeclarator(node); } // Special case inlining into anonymous types to ensure that we keep property names: // // E.g. // int x = 42; // var a = new { x; }; // // Should become: // var a = new { x = 42; }; nameEquals = SyntaxFactory.NameEquals(identifier); expression = (ExpressionSyntax)this.Visit(expression); return node.Update(nameEquals, expression).WithAdditionalAnnotations(Simplifier.Annotation, Formatter.Annotation); }
public static bool IsInitializerForObjectOrAnonymousObjectCreationExpression(this SyntaxNode node) { var initializer = node as InitializerExpressionSyntax; AnonymousObjectMemberDeclaratorSyntax anonymousObjectInitializer = null; if (initializer == null) { anonymousObjectInitializer = node as AnonymousObjectMemberDeclaratorSyntax; if (anonymousObjectInitializer == null) { return(false); } } var parent = initializer != null ? initializer.Parent : anonymousObjectInitializer.Parent; if (parent is AnonymousObjectCreationExpressionSyntax) { return(true); } if (parent is ObjectCreationExpressionSyntax) { if (initializer.Expressions.Count <= 0) { return(true); } var expression = initializer.Expressions[0]; if (expression.Kind() == SyntaxKind.SimpleAssignmentExpression) { return(true); } } return(false); }
public override IPropertySymbol GetDeclaredSymbol(AnonymousObjectMemberDeclaratorSyntax declaratorSyntax, CancellationToken cancellationToken = default(CancellationToken)) { CheckSyntaxNode(declaratorSyntax); var model = this.GetMemberModel(declaratorSyntax); return (model == null) ? null : model.GetDeclaredSymbol(declaratorSyntax, cancellationToken); }
/// <summary> /// /// </summary> /// <param name="node"></param> public override sealed void VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) { this.OnNodeVisited(node); if (!this.traverseRootOnly) base.VisitAnonymousObjectMemberDeclarator(node); }
private ArgumentSyntax CreateArgument(AnonymousObjectMemberDeclaratorSyntax decl) => SyntaxFactory.Argument(decl.Expression);
public override IPropertySymbol GetDeclaredSymbol(AnonymousObjectMemberDeclaratorSyntax declaratorSyntax, CancellationToken cancellationToken = default(CancellationToken)) { CheckSyntaxNode(declaratorSyntax); var anonymousObjectCreation = (AnonymousObjectCreationExpressionSyntax)declaratorSyntax.Parent; if (anonymousObjectCreation == null) { return null; } var bound = this.GetLowerBoundNode(anonymousObjectCreation) as BoundAnonymousObjectCreationExpression; if (bound == null) { return null; } var anonymousType = bound.Type as NamedTypeSymbol; if ((object)anonymousType == null) { return null; } int index = anonymousObjectCreation.Initializers.IndexOf(declaratorSyntax); Debug.Assert(index >= 0); Debug.Assert(index < anonymousObjectCreation.Initializers.Count); return AnonymousTypeManager.GetAnonymousTypeProperty(anonymousType, index); }
/// <summary> /// Given a syntax node of anonymous object creation initializer, get the anonymous object property symbol. /// </summary> /// <param name="declaratorSyntax">The syntax node that declares a property.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The symbol that was declared.</returns> public abstract IPropertySymbol GetDeclaredSymbol(AnonymousObjectMemberDeclaratorSyntax declaratorSyntax, CancellationToken cancellationToken = default(CancellationToken));
private IEnumerable<ITypeSymbol> InferTypeInMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax memberDeclarator, SyntaxToken? previousTokenOpt = null) { if (memberDeclarator.NameEquals != null && memberDeclarator.Parent is AnonymousObjectCreationExpressionSyntax) { // If we're position based, then we have to be after the = if (previousTokenOpt.HasValue && previousTokenOpt.Value != memberDeclarator.NameEquals.EqualsToken) { return SpecializedCollections.EmptyEnumerable<ITypeSymbol>(); } var types = InferTypes((AnonymousObjectCreationExpressionSyntax)memberDeclarator.Parent); return types.Where(t => t.IsAnonymousType()) .SelectMany(t => t.GetValidAnonymousTypeProperties() .Where(p => p.Name == memberDeclarator.NameEquals.Name.Identifier.ValueText) .Select(p => p.Type)); } return SpecializedCollections.EmptyEnumerable<ITypeSymbol>(); }
public override void VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) { this.Found |= node.NameEquals?.Name?.Identifier.ValueText == this.name; base.VisitAnonymousObjectMemberDeclarator(node); }
public void VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) { if (node == null) throw new ArgumentNullException("node"); node.Validate(); if (node.NameEquals != null) node.NameEquals.Accept(this); node.Expression.Accept(this); }
/// <summary> /// /// </summary> /// <param name="node"></param> public override sealed void VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) { this.OnNodeVisited(node, this.type.IsInstanceOfType(node)); base.VisitAnonymousObjectMemberDeclarator(node); }
public IPropertySymbol?ToSymbol( AnonymousObjectMemberDeclaratorSyntax node) => Model.GetDeclaredSymbol(node, CancellationToken);
private BoundExpression BindAnonymousObjectCreation(AnonymousObjectCreationExpressionSyntax node, DiagnosticBag diagnostics) { // prepare var initializers = node.Initializers; int fieldCount = initializers.Count; bool hasError = false; // bind field initializers BoundExpression[] boundExpressions = new BoundExpression[fieldCount]; AnonymousTypeField[] fields = new AnonymousTypeField[fieldCount]; CSharpSyntaxNode[] fieldSyntaxNodes = new CSharpSyntaxNode[fieldCount]; // WARNING: Note that SemanticModel.GetDeclaredSymbol for field initializer node relies on // the fact that the order of properties in anonymous type template corresponds // 1-to-1 to the appropriate filed initializer syntax nodes; This means such // correspondence must be preserved all the time including erroneos scenarios // set of names already used HashSet <string> uniqueFieldNames = new HashSet <string>(); for (int i = 0; i < fieldCount; i++) { AnonymousObjectMemberDeclaratorSyntax fieldInitializer = initializers[i]; NameEqualsSyntax nameEquals = fieldInitializer.NameEquals; ExpressionSyntax expression = fieldInitializer.Expression; SyntaxToken nameToken = default(SyntaxToken); if (nameEquals != null) { nameToken = nameEquals.Name.Identifier; } else { nameToken = expression.ExtractAnonymousTypeMemberName(); } hasError = hasError || expression.HasErrors; boundExpressions[i] = this.BindValue(expression, diagnostics, BindValueKind.RValue); // check the name to be unique string fieldName = null; if (nameToken.CSharpKind() == SyntaxKind.IdentifierToken) { fieldName = nameToken.ValueText; if (uniqueFieldNames.Contains(fieldName)) { // name duplication Error(diagnostics, ErrorCode.ERR_AnonymousTypeDuplicatePropertyName, fieldInitializer); hasError = true; fieldName = null; } else { uniqueFieldNames.Add(fieldName); } } else { // there is something wrong with field's name hasError = true; } // calculate the expression's type and report errors if needed TypeSymbol fieldType = GetAnonymousTypeFieldType(boundExpressions[i], fieldInitializer, diagnostics, ref hasError); // build anonymous type field descriptor fieldSyntaxNodes[i] = (nameToken.CSharpKind() == SyntaxKind.IdentifierToken) ? (CSharpSyntaxNode)nameToken.Parent : fieldInitializer; fields[i] = new AnonymousTypeField(fieldName == null ? '$' + i.ToString() : fieldName, fieldSyntaxNodes[i].Location, fieldType); // NOTE: ERR_InvalidAnonymousTypeMemberDeclarator (CS0746) would be generated by parser if needed } // Create anonymous type AnonymousTypeManager manager = this.Compilation.AnonymousTypeManager; AnonymousTypeDescriptor descriptor = new AnonymousTypeDescriptor(fields.AsImmutableOrNull(), node.NewKeyword.GetLocation()); NamedTypeSymbol anonymousType = manager.ConstructAnonymousTypeSymbol(descriptor); // declarators - bound nodes created for providing semantic info // on anonymous type fields having explicitly specified name ArrayBuilder <BoundAnonymousPropertyDeclaration> declarators = ArrayBuilder <BoundAnonymousPropertyDeclaration> .GetInstance(); for (int i = 0; i < fieldCount; i++) { NameEqualsSyntax explicitName = initializers[i].NameEquals; if (explicitName != null) { AnonymousTypeField field = fields[i]; if (field.Name != null) { // get property symbol and create a bound property declaration node foreach (var symbol in anonymousType.GetMembers(field.Name)) { if (symbol.Kind == SymbolKind.Property) { declarators.Add(new BoundAnonymousPropertyDeclaration(fieldSyntaxNodes[i], (PropertySymbol)symbol, field.Type)); break; } } } } } // check if anonymous object creation is allowed in this context if (!this.IsAnonymousTypesAllowed()) { Error(diagnostics, ErrorCode.ERR_AnonymousTypeNotAvailable, node.NewKeyword); hasError = true; } // Finally create a bound node return(new BoundAnonymousObjectCreationExpression( node, anonymousType.InstanceConstructors[0], boundExpressions.AsImmutableOrNull(), declarators.ToImmutableAndFree(), anonymousType, hasError)); }
public override IPropertySymbol GetDeclaredSymbol(AnonymousObjectMemberDeclaratorSyntax declaratorSyntax, CancellationToken cancellationToken = default(CancellationToken)) { using (Logger.LogBlock(FunctionId.CSharp_SemanticModel_GetDeclaredSymbol, message: this.SyntaxTree.FilePath, cancellationToken: cancellationToken)) { CheckSyntaxNode(declaratorSyntax); var model = this.GetMemberModel(declaratorSyntax); return (model == null) ? null : model.GetDeclaredSymbol(declaratorSyntax, cancellationToken); } }