private IEnumerable <Instruction> ProcessElementAccess(ElementAccessExpressionSyntax elementAccessSyntax) { if (elementAccessSyntax.GetSelfOrTopParenthesizedExpression().Parent is AssignmentExpressionSyntax assignmentSyntax && assignmentSyntax.IsKind(SyntaxKind.SimpleAssignmentExpression) && assignmentSyntax.Left.RemoveParentheses() == elementAccessSyntax) { return(NoInstructions); } var elementAccessType = this.semanticModel.GetTypeInfo(elementAccessSyntax.Expression).ConvertedType; if (elementAccessType == null) { return(NoInstructions); } if (elementAccessType.TypeKind == TypeKind.Array) { return(BuildArrayGetCall(elementAccessSyntax, elementAccessType, GetTargetExpression(elementAccessSyntax, elementAccessType))); } var indexerPropertySymbol = GetNodeSymbol(elementAccessSyntax) as IPropertySymbol; if (indexerPropertySymbol == null) { return(NoInstructions); } return(BuildFunctionCall(elementAccessSyntax, indexerPropertySymbol.GetMethod, GetTargetExpression(elementAccessSyntax, indexerPropertySymbol), GetInvocationExpressions(indexerPropertySymbol.Parameters, elementAccessSyntax.ArgumentList))); }
private IEnumerable <Instruction> ProcessElementAccessExpression(ElementAccessExpressionSyntax elementAccessExpression) { if (!IsArray(elementAccessExpression.Expression)) { expressionService.Associate(elementAccessExpression, expressionService.CreateConstant()); return(NoInstructions); } var targetObject = expressionService.GetExpression(elementAccessExpression.Expression); var elementAccess = expressionService.CreateArrayAccess( semanticModel.GetSymbolInfo(elementAccessExpression.Expression).Symbol, targetObject); // handling for parenthesized left side of an assignment (x[5]) = s var topParenthesized = elementAccessExpression.GetSelfOrTopParenthesizedExpression(); // When the array access is on the left side of an assignment expression we will generate the // set instruction in the assignment expression handler, hence we just associate the two // syntax and the ucfg expression. if (IsLeftSideOfAssignment(topParenthesized)) { expressionService.Associate(elementAccessExpression, elementAccess); return(NoInstructions); } // for anything else we generate __arrayGet instruction return(CreateArrayGetCall(elementAccessExpression, elementAccess.TypeSymbol, targetObject)); bool IsLeftSideOfAssignment(SyntaxNode syntaxNode) => syntaxNode.Parent is AssignmentExpressionSyntax assignmentExpression && assignmentExpression.Left == syntaxNode; bool IsArray(ExpressionSyntax expression) { var elementAccessType = semanticModel.GetTypeInfo(expression).ConvertedType; return(elementAccessType != null && elementAccessType.TypeKind == TypeKind.Array); } }
private static bool IsDictionarySetItem(ElementAccessExpressionSyntax elementAccess) => (elementAccess.GetSelfOrTopParenthesizedExpression().Parent as AssignmentExpressionSyntax) ?.Left.RemoveParentheses() == elementAccess;