public override void VisitQueryExpression(QueryExpressionSyntax node) { // Variables declared in [in] expressions of top level from clause and // join clauses are in scope VisitNodeToBind(node.FromClause.Expression); Visit(node.Body); }
/// <inheritdoc /> public override Expression VisitQueryExpression(QueryExpressionSyntax node) { return(Expressive.Query( Enumerable.Repeat(VisitContinuationClause(node.FromClause), 1) .Concat(VisitLinqBody(node.Body)) )); }
public override SyntaxNode VisitQueryExpression(QueryExpressionSyntax node) { if (!_visitQuerySyntax) { return(base.VisitQueryExpression(node)); } SetAnonymousTypeName(node); VisitFromClause(node.FromClause); VisitQueryBody(node.Body); ExpressionSyntax currentExpression = ParenthesizedExpression((InvocationExpressionSyntax)_state.FluentExpression); foreach (var invocation in _pendingInvocationExpressions.AsEnumerable().Reverse()) { var fluentNode = InvocationExpression(MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, currentExpression, ((MemberAccessExpressionSyntax)invocation.Expression).Name), invocation.ArgumentList); currentExpression = fluentNode; } _pendingInvocationExpressions.Clear(); _state.IsAnonymousType = false; return(_state.FluentExpression); }
public override SyntaxNode VisitQueryExpression(QueryExpressionSyntax node) { var transformedQuery = _TransformQuery(node); return(SyntaxFactory.ParenthesizedExpression(transformedQuery)); }
public static Dictionary <string, ContextualLinqParameter> BuildContext(QueryExpressionSyntax query, SyntaxNodeAnalysisContext context, EFUsageContext efContext) { var cparams = new Dictionary <string, ContextualLinqParameter>(); //From x var fromExpr = query.FromClause.Identifier; //in <expr> var inExpr = query.FromClause.Expression; string name = fromExpr.ValueText; var memberExpr = inExpr as MemberAccessExpressionSyntax; if (memberExpr != null) { EFCodeFirstClassInfo cls; if (LinqExpressionValidator.MemberAccessIsAccessingDbContext(memberExpr, context, efContext, out cls)) { cparams[name] = new ContextualLinqParameter(name, cls); } } //Still not set, just set as a contextual parameter with no known type if (!cparams.ContainsKey(name)) { cparams[name] = new ContextualLinqParameter(name); } return(cparams); }
public void VisitQueryExpression(QueryExpressionSyntax node) { if (node == null) { throw new ArgumentNullException("node"); } node.Validate(); ExpressionStart(node); if (_writer.Configuration.Other.AlignMultiLineConstructs.LinqQuery) { _writer.SetAlignmentBreak(true); } node.FromClause.Accept(this); _writer.WriteSpace(); node.Body.Accept(this); if (_writer.Configuration.Other.AlignMultiLineConstructs.LinqQuery) { _writer.SetAlignmentBreak(false); } ExpressionEnd(node); }
private static bool TryFindSyntaxNode(QueryExpressionSyntax query, out SyntaxNode syntaxNode, out string identifier) { var methodDeclarationSyntax = query.GetEnclosing <MethodDeclarationSyntax>(); if (methodDeclarationSyntax != null) { syntaxNode = methodDeclarationSyntax; identifier = methodDeclarationSyntax.Identifier.ValueText; return(true); } // we do not find the enclosing method, so we might have a field (for which we need a variable), such as in MiKo_2071_EnumMethodSummaryAnalyzer var variableDeclaratorSyntax = query.GetEnclosing <VariableDeclaratorSyntax>(); if (variableDeclaratorSyntax != null) { syntaxNode = variableDeclaratorSyntax; identifier = variableDeclaratorSyntax.Identifier.ValueText; return(true); } // we might have a constructor here var ctorDeclarationSyntax = query.GetEnclosing <ConstructorDeclarationSyntax>(); if (ctorDeclarationSyntax != null) { syntaxNode = ctorDeclarationSyntax; identifier = ctorDeclarationSyntax.Identifier.ValueText; return(true); } syntaxNode = null; identifier = null; return(false); }
public override void VisitQueryExpression(QueryExpressionSyntax node) { node.FromClause?.Accept(this); node.Body?.Accept(this); base.VisitQueryExpression(node); }
public override Evaluation VisitQueryExpression(QueryExpressionSyntax node) { node.FromClause?.Accept <Evaluation>(this); node.Body?.Accept <Evaluation>(this); return(base.VisitQueryExpression(node)); }
private Doc PrintQueryExpressionSyntax(QueryExpressionSyntax node) { return(Concat( this.PrintFromClauseSyntax(node.FromClause), Indent(Concat(Line, this.PrintQueryBodySyntax(node.Body))) )); }
internal BoundExpression BindQuery(QueryExpressionSyntax node, DiagnosticBag diagnostics) { var fromClause = node.FromClause; var boundFromExpression = BindLeftOfPotentialColorColorMemberAccess(fromClause.Expression, diagnostics); QueryTranslationState state = new QueryTranslationState(); state.fromExpression = MakeMemberAccessValue(boundFromExpression, diagnostics); var x = state.rangeVariable = state.AddRangeVariable(this, fromClause.Identifier, diagnostics); for (int i = node.Body.Clauses.Count - 1; i >= 0; i--) { state.clauses.Push(node.Body.Clauses[i]); } state.selectOrGroup = node.Body.SelectOrGroup; // A from clause that explicitly specifies a range variable type // from T x in e // is translated into // from x in ( e ) . Cast < T > ( ) BoundExpression cast = null; if (fromClause.Type != null) { var typeRestriction = BindTypeArgument(fromClause.Type, diagnostics); cast = MakeQueryInvocation(fromClause, state.fromExpression, "Cast", fromClause.Type, typeRestriction, diagnostics); state.fromExpression = cast; } state.fromExpression = MakeQueryClause(fromClause, state.fromExpression, x, castInvocation: cast); BoundExpression result = BindQueryInternal1(state, diagnostics); for (QueryContinuationSyntax continuation = node.Body.Continuation; continuation != null; continuation = continuation.Body.Continuation) { // A query expression with a continuation // from ... into x ... // is translated into // from x in ( from ... ) ... state.Clear(); state.fromExpression = result; x = state.rangeVariable = state.AddRangeVariable(this, continuation.Identifier, diagnostics); Debug.Assert(state.clauses.IsEmpty()); var clauses = continuation.Body.Clauses; for (int i = clauses.Count - 1; i >= 0; i--) { state.clauses.Push(clauses[i]); } state.selectOrGroup = continuation.Body.SelectOrGroup; result = BindQueryInternal1(state, diagnostics); result = MakeQueryClause(continuation.Body, result, x); result = MakeQueryClause(continuation, result, x); } state.Free(); return(MakeQueryClause(node, result)); }
public static Doc Print(QueryExpressionSyntax node) { return(Doc.Concat( FromClause.Print(node.FromClause), Doc.Line, QueryBody.Print(node.Body) )); }
public override void VisitQueryExpression(QueryExpressionSyntax queryFromClause) { _indexData.FromExpression = queryFromClause.FromClause.Expression; _indexData.FromIdentifier = queryFromClause.FromClause.Identifier.ValueText; _indexData.NumberOfFromClauses++; VisitQueryBody(queryFromClause.Body); }
private void ThrowIndexRewritingException(QueryExpressionSyntax node, ForEachStatementSyntax stmt) { throw new InvalidOperationException("Rewriting the function to an optimized version resulted in creating invalid indexing outputs. " + $"The output needs to have the following fields: [{string.Join(", ", _validator.Fields)}] " + $"while after the optimization it has: [{string.Join(", ", _validator.ExtractedFields)}].{Environment.NewLine}" + $"Original indexing func:{Environment.NewLine}{node.ToFullString()}{Environment.NewLine}{Environment.NewLine}" + $"Optimized indexing func:{Environment.NewLine}{stmt.ToFullString()}"); }
public static QueryExpressionSyntax WithAllClauses( this QueryExpressionSyntax query, IList <SyntaxNode> allClauses) { var fromClause = (FromClauseSyntax)allClauses.First(); return(query.WithFromClause(fromClause).WithBody(query.Body.WithAllClauses(allClauses.Skip(1)))); }
private Diagnostic AnalyzeQueryExpression(QueryExpressionSyntax query, SemanticModel semanticModel) { var foundNode = TryFindInspectionTarget(query, out var syntaxNode, out var identifier); if (foundNode is false) { return(null); } if (query.HasLinqExtensionMethod(semanticModel)) { // query itself contains Linq calls, so report an issue anyway return(Issue(identifier, query)); } InvocationExpressionSyntax firstCall = null; var calls = 0; foreach (var call in syntaxNode.LinqExtensionMethods(semanticModel)) { calls++; switch (calls) { case 1: firstCall = call; break; case 2: // no need to look further, found at least one additional call return(Issue(identifier, query)); } } if (calls == 0) { return(null); } // ignore "ToList" etc if (firstCall?.Expression is MemberAccessExpressionSyntax m) { switch (m.GetName()) { case nameof(Enumerable.ToList): case nameof(Enumerable.ToArray): case nameof(Enumerable.ToDictionary): case nameof(Enumerable.ToLookup): { // that are the only calls which shall be allowed return(null); } } } return(Issue(identifier, query)); }
public override UstNode VisitQueryExpression(QueryExpressionSyntax node) { var expressions = node.DescendantNodes().OfType <ExpressionSyntax>() .Select(exp => (Expression)VisitAndReturnNullIfError(exp)).ToList(); var result = new MultichildExpression(expressions, node.GetTextSpan(), FileNode); return(result); }
public override void VisitQueryExpression(QueryExpressionSyntax node) { if (entryPoint.IsMethodLevel() && node.IsParent <AnonymousFunctionExpressionSyntax>()) { return; } noscounter++; base.VisitQueryExpression(node); }
/// <summary> /// LINQ Query Syntax do not use symbols from the 'System.Linq' namespace directly, but the using directive is /// still necessary to use the Query Syntax form. /// </summary> public override void VisitQueryExpression(QueryExpressionSyntax node) { if (!this.linqQueryVisited && TryGetSystemLinkNamespace(out var systemLinqNamespaceSymbol)) { this.necessaryNamespaces.Add(systemLinqNamespaceSymbol); } this.linqQueryVisited = true; base.VisitQueryExpression(node); }
public static IList <SyntaxNode> GetAllClauses(this QueryExpressionSyntax query) { var result = new List <SyntaxNode>(); result.Add(query.FromClause); result.AddRange(query.Body.Clauses); result.Add(query.Body.SelectOrGroup); return(result); }
private static ImmutableArray <SyntaxNode> CreateQueryNodeList(QueryExpressionSyntax queryExpression) { var queryNodes = new List <SyntaxNode>(); queryNodes.Add(queryExpression.FromClause); ProcessQueryBody(queryExpression.Body, queryNodes); return(queryNodes.ToImmutableArray()); }
public override LuaSyntaxNode VisitQueryExpression(QueryExpressionSyntax node) { CurCompilationUnit.ImportLinq(); LuaIdentifierNameSyntax rangeVariable = new LuaIdentifierNameSyntax(node.FromClause.Identifier.ValueText); CheckVariableDeclaratorName(ref rangeVariable, node); var fromClauseExpression = (LuaExpressionSyntax)node.FromClause.Expression.Accept(this); return(BuildQueryBody(node.Body, fromClauseExpression, rangeVariable)); }
public override Ust VisitQueryExpression(QueryExpressionSyntax node) { IEnumerable <Expression> expressions = node.DescendantNodes() .OfType <ExpressionSyntax>() .Select(exp => VisitAndReturnNullIfError(exp) as Expression) .Where(expr => expr != null); var result = new MultichildExpression(expressions, node.GetTextSpan()); return(result); }
private static int GetComplexityForClauses([NotNull] QueryExpressionSyntax queryExpression) { int complexity = 0; foreach (QueryClauseSyntax bodyClause in queryExpression.Body.Clauses) { complexity += GetComplexityForClause(bodyClause); } return(complexity); }
public override LuaSyntaxNode VisitQueryExpression(QueryExpressionSyntax node) { CurCompilationUnit.ImportLinq(); var rangeVariable = AddRangeIdentifier(node.FromClause.Identifier); var collection = (LuaExpressionSyntax)node.FromClause.Accept(this); var queryExpression = BuildQueryBody(collection, node.Body, rangeVariable); queryIdentifiers_.Clear(); return(queryExpression); }
private bool?CheckIfAllElementsAreAtTheSameLine(QueryExpressionSyntax query) { var fileLinePositionSpan = query.GetLocation().GetLineSpan(); if (fileLinePositionSpan.IsValid) { return(fileLinePositionSpan.StartLinePosition.Line == fileLinePositionSpan.EndLinePosition.Line); } return(null); }
public override void VisitQueryExpression(QueryExpressionSyntax node) { _linqIndex++; var getMembersVisitor = new GetMembersVisitor(this); getMembersVisitor.Visit(node.FromClause); AddProperties(getMembersVisitor._properties); getMembersVisitor = new GetMembersVisitor(this); getMembersVisitor.Visit(node.Body); AddProperties(getMembersVisitor._properties); _properties.Last = getMembersVisitor._properties.Last; _proceed = true; }
private void SetAnonymousTypeName(QueryExpressionSyntax node) { var rangeVariables = GetRangeVariables(node).Select(rv => rv.ValueText).ToList(); string defaultAnonymousTypeName = "t"; string anonymousTypeName = defaultAnonymousTypeName; int i = 1; while (rangeVariables.Any(val => val == anonymousTypeName)) { anonymousTypeName = defaultAnonymousTypeName + i++; } _state.AnonymousTypeIdentifier = Identifier(anonymousTypeName); }
/// <summary> /// Gets a whitespace trivia containing the proper amount of indentation for new lines in the given query. /// </summary> /// <param name="indentationOptions">The indentation options to use.</param> /// <param name="queryExpression">The query expression to determine indentation from.</param> /// <returns>A whitespace trivia containing the proper amount of indentation.</returns> internal static SyntaxTrivia GetQueryIndentationTrivia(IndentationOptions indentationOptions, QueryExpressionSyntax queryExpression) { var firstTokenOnTextLine = IndentationHelper.GetFirstTokenOnTextLine(queryExpression.FromClause.FromKeyword); var indentationSteps = IndentationHelper.GetIndentationSteps(indentationOptions, firstTokenOnTextLine); // add an extra indentation step when the first from clause is not properly indented yet if (!firstTokenOnTextLine.IsKind(SyntaxKind.OpenParenToken) && (firstTokenOnTextLine != queryExpression.FromClause.FromKeyword)) { indentationSteps++; } return IndentationHelper.GenerateWhitespaceTrivia(indentationOptions, indentationSteps); }
//protected override bool VisitChildren(AstNode node) //{ // return VisitNodeList(node.Children); //} //bool VisitNodeList(IEnumerable<AstNode> nodes) { // return nodes.Any(node => node.AcceptVisitor(this)); //} public override bool VisitQueryExpression(QueryExpressionSyntax node) { //We only care about the first from clause because: //in "from x in Method() select x", Method() might be recursive //but in "from x in Bar() from y in Method() select x + y", even if Method() is recursive //Bar might still be empty. var queryFromClause = node.FromClause; if (queryFromClause == null) { return(true); } return(Visit(queryFromClause)); }
private bool IsQueryParentForNode(SyntaxNode syntaxNode, QueryExpressionSyntax queryExpressionSyntax) { if (syntaxNode == null) { return(false); } if (syntaxNode.IsKind(queryExpressionSyntax.Kind())) { return(syntaxNode == queryExpressionSyntax); } return(this.IsQueryParentForNode(syntaxNode.Parent, queryExpressionSyntax)); }
private static void AnalyzeQueryExpression(SyntaxNodeAnalysisContext context, EFUsageContext efContext, QueryExpressionSyntax query) { //I can't believe how much easier this is compared to Extension Method syntax! Then again //query syntax does mean its own dedicated set of C# keywords, which would means its own //dedicated set of syntax node types //First item on checklist, find out our root queryable EFCodeFirstClassInfo cls; bool isConnectedToDbContext = GetRootEntityTypeFromLinqQuery(query.FromClause.Expression, context, efContext, out cls); if (cls != null) { bool treatAsWarning = !isConnectedToDbContext; var paramNodes = ContextualLinqParameter.BuildContext(query, context, efContext); var descendants = query.Body.DescendantNodes(); LinqExpressionValidator.ValidateLinqToEntitiesUsageInSyntaxNodes(descendants, cls, context, efContext, paramNodes, treatAsWarning); } }
public override void VisitQueryExpression(QueryExpressionSyntax node) { VisitQueryInternal(node.FromClause, node.Body); }
public override void VisitQueryExpression(QueryExpressionSyntax node) { base.VisitQueryExpression(node); }
public override void VisitQueryExpression(QueryExpressionSyntax node) { Visit(node.FromClause.Expression); Visit(node.Body); }
/// <summary> /// /// </summary> /// <param name="node"></param> public override sealed void VisitQueryExpression(QueryExpressionSyntax node) { this.OnNodeVisited(node); if (!this.traverseRootOnly) base.VisitQueryExpression(node); }
public override void VisitQueryExpression(QueryExpressionSyntax node) { // Variables declared in [in] expressions of top level from clause and // join clauses are in scope Visit(node.FromClause.Expression); Visit(node.Body); }
/// <summary> /// /// </summary> /// <param name="node"></param> public override sealed void VisitQueryExpression(QueryExpressionSyntax node) { this.OnNodeVisited(node, this.type.IsInstanceOfType(node)); base.VisitQueryExpression(node); }
public static Dictionary<string, ContextualLinqParameter> BuildContext(QueryExpressionSyntax query, SyntaxNodeAnalysisContext context, EFUsageContext efContext) { var cparams = new Dictionary<string, ContextualLinqParameter>(); //From x var fromExpr = query.FromClause.Identifier; //in <expr> var inExpr = query.FromClause.Expression; string name = fromExpr.ValueText; var memberExpr = inExpr as MemberAccessExpressionSyntax; if (memberExpr != null) { EFCodeFirstClassInfo cls; if (LinqExpressionValidator.MemberAccessIsAccessingDbContext(memberExpr, context, efContext, out cls)) { cparams[name] = new ContextualLinqParameter(name, cls); } } //Still not set, just set as a contextual parameter with no known type if (!cparams.ContainsKey(name)) cparams[name] = new ContextualLinqParameter(name); return cparams; }
public void VisitQueryExpression(QueryExpressionSyntax node) { if (node == null) throw new ArgumentNullException("node"); node.Validate(); ExpressionStart(node); if (_writer.Configuration.Other.AlignMultiLineConstructs.LinqQuery) _writer.SetAlignmentBreak(true); node.FromClause.Accept(this); _writer.WriteSpace(); node.Body.Accept(this); if (_writer.Configuration.Other.AlignMultiLineConstructs.LinqQuery) _writer.SetAlignmentBreak(false); ExpressionEnd(node); }
private static ImmutableArray<SyntaxNode> CreateQueryNodeList(QueryExpressionSyntax queryExpression) { var queryNodes = new List<SyntaxNode>(); queryNodes.Add(queryExpression.FromClause); ProcessQueryBody(queryExpression.Body, queryNodes); return queryNodes.ToImmutableArray(); }
internal BoundExpression BindQuery(QueryExpressionSyntax node, DiagnosticBag diagnostics) { var fromClause = node.FromClause; var boundFromExpression = BindLeftOfPotentialColorColorMemberAccess(fromClause.Expression, diagnostics); // If the from expression is of the type dynamic we can't infer the types for any lambdas that occur in the query. // Only if there are none we could bind the query but we report an error regardless since such queries are not useful. if (boundFromExpression.HasDynamicType()) { diagnostics.Add(ErrorCode.ERR_BadDynamicQuery, fromClause.Expression.Location); boundFromExpression = BadExpression(fromClause.Expression, boundFromExpression); } QueryTranslationState state = new QueryTranslationState(); state.fromExpression = MakeMemberAccessValue(boundFromExpression, diagnostics); var x = state.rangeVariable = state.AddRangeVariable(this, fromClause.Identifier, diagnostics); for (int i = node.Body.Clauses.Count - 1; i >= 0; i--) { state.clauses.Push(node.Body.Clauses[i]); } state.selectOrGroup = node.Body.SelectOrGroup; // A from clause that explicitly specifies a range variable type // from T x in e // is translated into // from x in ( e ) . Cast < T > ( ) BoundExpression cast = null; if (fromClause.Type != null) { var typeRestriction = BindTypeArgument(fromClause.Type, diagnostics); cast = MakeQueryInvocation(fromClause, state.fromExpression, "Cast", fromClause.Type, typeRestriction, diagnostics); state.fromExpression = cast; } state.fromExpression = MakeQueryClause(fromClause, state.fromExpression, x, castInvocation: cast); BoundExpression result = BindQueryInternal1(state, diagnostics); for (QueryContinuationSyntax continuation = node.Body.Continuation; continuation != null; continuation = continuation.Body.Continuation) { // A query expression with a continuation // from ... into x ... // is translated into // from x in ( from ... ) ... state.Clear(); state.fromExpression = result; x = state.rangeVariable = state.AddRangeVariable(this, continuation.Identifier, diagnostics); Debug.Assert(state.clauses.IsEmpty()); var clauses = continuation.Body.Clauses; for (int i = clauses.Count - 1; i >= 0; i--) { state.clauses.Push(clauses[i]); } state.selectOrGroup = continuation.Body.SelectOrGroup; result = BindQueryInternal1(state, diagnostics); result = MakeQueryClause(continuation.Body, result, x); result = MakeQueryClause(continuation, result, x); } state.Free(); return MakeQueryClause(node, result); }
public QueryExpressionTranslation(QueryExpressionSyntax syntax, SyntaxTranslation parent) : base(syntax, parent) { }