public override void VisitArrayInitializer(IArrayInitializer operation) { LogString(nameof(IArrayInitializer)); LogCommonPropertiesAndNewLine(operation); VisitArray(operation.ElementValues, "Element Values", logElementCount: true); }
public override void VisitArrayInitializer(IArrayInitializer operation) { LogString(nameof(IArrayInitializer)); LogCommonPropertiesAndNewLine(operation); base.VisitArrayInitializer(operation); }
private static void CheckArrayInitializer([NotNull] IArrayInitializer arrayInitializer, [NotNull] IHighlightingConsumer consumer) { ITreeNode start = null, end = null; var variableDeclaration = LocalVariableDeclarationNavigator.GetByInitial(arrayInitializer); if (variableDeclaration?.EquivalenceSign != null) { start = variableDeclaration.NameIdentifier; end = variableDeclaration.EquivalenceSign; } else { var fieldDeclaration = FieldDeclarationNavigator.GetByInitial(arrayInitializer); if (fieldDeclaration?.EquivalenceSign != null) { start = fieldDeclaration.NameIdentifier; end = fieldDeclaration.EquivalenceSign; } } if (start != null && end != null) { var endOffset = end.GetDocumentEndOffset(); var highlighting = new ObjectAllocationEvidentHighlighting(arrayInitializer, "array instantiation"); var documentRange = start.GetDocumentRange().SetEndTo(endOffset); consumer.AddHighlighting(highlighting, documentRange); } }
internal ArrayWithDefaultValuesInitializationSuggestion( [NotNull] string message, [CanBeNull] string suggestedCode, [NotNull] IArrayInitializer arrayInitializer) : base(message) { SuggestedCode = suggestedCode; ArrayInitializer = arrayInitializer; }
internal ArrayWithDefaultValuesInitializationHighlighting( [NotNull] string message, [NotNull] IArrayInitializer arrayInitializer, [NotNull] IType arrayElementType, int elementCount) : base(message) { ArrayInitializer = arrayInitializer; ArrayElementType = arrayElementType; ElementCount = elementCount; }
static bool IsInvalidInitializer(IArrayInitializer initializer) { switch (initializer.ArrayClass) { case ArrayInitializerKind.Dimension: foreach (IArrayInitializer element in ((IDimensionArrayInitializer)initializer).ElementValues) { if (IsInvalidInitializer(element)) { return(true); } } break; case ArrayInitializerKind.Expression: return(((IExpressionArrayInitializer)initializer).ElementValue.IsInvalid); } return(false); }
public override void Initialize(AnalysisContext analysisContext) { analysisContext.RegisterCompilationStartAction(compilationContext => { var formatInfo = new StringFormatInfo(compilationContext.Compilation); compilationContext.RegisterOperationAction(operationContext => { var invocation = (IInvocationExpression)operationContext.Operation; StringFormatInfo.Info info = formatInfo.TryGet(invocation.TargetMethod); if (info == null || invocation.ArgumentsInParameterOrder.Length <= info.FormatStringIndex) { // not a target method return; } IArgument formatStringArgument = invocation.ArgumentsInParameterOrder[info.FormatStringIndex]; if (!object.Equals(formatStringArgument?.Value?.Type, formatInfo.String) || !(formatStringArgument?.Value?.ConstantValue.Value is string)) { // wrong argument return; } var stringFormat = (string)formatStringArgument.Value.ConstantValue.Value; int expectedStringFormatArgumentCount = GetFormattingArguments(stringFormat); // explict parameter case if (info.ExpectedStringFormatArgumentCount >= 0) { // TODO: due to a bug - https://github.com/dotnet/roslyn/issues/7346 // vararg case is disabled. // we might check this only for C# since __arglist is not supported in VB // // we need to implement proper support for __arglist once the bug is fixed. if (invocation.TargetMethod.IsVararg) { // can't deal with this for now. return; } if (info.ExpectedStringFormatArgumentCount != expectedStringFormatArgumentCount) { operationContext.ReportDiagnostic(operationContext.Operation.Syntax.CreateDiagnostic(Rule)); } return; } // params case IArgument paramsArgument = invocation.ArgumentsInParameterOrder[info.FormatStringIndex + 1]; if (paramsArgument.ArgumentKind != ArgumentKind.ParamArray) { // wrong format return; } var arrayCreation = paramsArgument.Value as IArrayCreationExpression; if (arrayCreation == null || !object.Equals(arrayCreation.ElementType, formatInfo.Object) || arrayCreation.DimensionSizes.Length != 1) { // wrong format return; } // compiler generating object array for params case IArrayInitializer intializer = arrayCreation.Initializer; if (intializer == null) { // unsupported format return; } // REVIEW: "ElementValues" is a bit confusing where I need to double dot those to get number of elements int actualArgumentCount = intializer.ElementValues.Length; if (actualArgumentCount != expectedStringFormatArgumentCount) { operationContext.ReportDiagnostic(operationContext.Operation.Syntax.CreateDiagnostic(Rule)); } }, OperationKind.InvocationExpression); }); }
public override void Initialize(AnalysisContext analysisContext) { analysisContext.EnableConcurrentExecution(); analysisContext.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); analysisContext.RegisterCompilationStartAction(compilationContext => { var formatInfo = new StringFormatInfo(compilationContext.Compilation); compilationContext.RegisterOperationActionInternal(operationContext => { var invocation = (IInvocationExpression)operationContext.Operation; StringFormatInfo.Info info = formatInfo.TryGet(invocation.TargetMethod); if (info == null || invocation.ArgumentsInParameterOrder.Length <= info.FormatStringIndex) { // not a target method return; } IArgument formatStringArgument = invocation.ArgumentsInParameterOrder[info.FormatStringIndex]; if (!object.Equals(formatStringArgument?.Value?.Type, formatInfo.String) || !(formatStringArgument?.Value?.ConstantValue.Value is string)) { // wrong argument return; } var stringFormat = (string)formatStringArgument.Value.ConstantValue.Value; int expectedStringFormatArgumentCount = GetFormattingArguments(stringFormat); // explict parameter case if (info.ExpectedStringFormatArgumentCount >= 0) { // __arglist is not supported here if (invocation.TargetMethod.IsVararg) { // can't deal with this for now. return; } if (info.ExpectedStringFormatArgumentCount != expectedStringFormatArgumentCount) { operationContext.ReportDiagnostic(operationContext.Operation.Syntax.CreateDiagnostic(Rule)); } return; } // params case IArgument paramsArgument = invocation.ArgumentsInParameterOrder[info.FormatStringIndex + 1]; if (paramsArgument.ArgumentKind != ArgumentKind.ParamArray) { // wrong format return; } var arrayCreation = paramsArgument.Value as IArrayCreationExpression; if (arrayCreation == null || !object.Equals(arrayCreation.ElementType, formatInfo.Object) || arrayCreation.DimensionSizes.Length != 1) { // wrong format return; } // compiler generating object array for params case IArrayInitializer intializer = arrayCreation.Initializer; if (intializer == null) { // unsupported format return; } // REVIEW: "ElementValues" is a bit confusing where I need to double dot those to get number of elements int actualArgumentCount = intializer.ElementValues.Length; if (actualArgumentCount != expectedStringFormatArgumentCount) { operationContext.ReportDiagnostic(operationContext.Operation.Syntax.CreateDiagnostic(Rule)); } }, OperationKind.InvocationExpression); }); }
public virtual void visit(IArrayInitializer value) { }
static bool IsInvalidInitializer(IArrayInitializer initializer) => initializer.IsInvalid;
private ArrayCreationExpression(ITypeSymbol elementType, ImmutableArray <IOperation> dimensionSizes, IArrayInitializer initializer, SyntaxNode syntax, ITypeSymbol type, Optional <object> constantValue) : this(elementType, dimensionSizes, initializer, initializer.IsInvalid, syntax, type, constantValue) { }
static bool IsInvalidInitializer(IArrayInitializer initializer) { switch (initializer.ArrayClass) { case ArrayInitializerKind.Dimension: foreach (IArrayInitializer element in ((IDimensionArrayInitializer)initializer).ElementValues) { if (IsInvalidInitializer(element)) { return true; } } break; case ArrayInitializerKind.Expression: return ((IExpressionArrayInitializer)initializer).ElementValue.IsInvalid; } return false; }
/// <inheritdoc /> public override IOperation VisitArrayInitializer(IArrayInitializer operation, object argument) { return(base.VisitArrayInitializer(operation, argument)); }
public virtual void VisitArrayInitializer(IArrayInitializer operation) { DefaultVisit(operation); }
public override void VisitArrayInitializer(IArrayInitializer operation) { base.VisitArrayInitializer(operation); }
private void CreateArrayGlobalVariable(ILGenerator il, FieldBuilder fb, TypeInfo ti, IArrayInitializer InitalValue, ITypeNode arr_type) { int rank = 1; if (NETGeneratorTools.IsBoundedArray(ti)) NETGeneratorTools.CreateBoundedArray(il, fb, ti); else { rank = get_rank(arr_type); if (rank == 1) CreateUnsizedArray(il, fb, helper.GetTypeReference(arr_type.element_type), InitalValue.ElementValues.Length); else CreateNDimUnsizedArray(il, fb, arr_type, helper.GetTypeReference(arr_type.element_type), rank, get_sizes(InitalValue, rank)); } if (InitalValue != null) { LocalBuilder lb = null; // if (save_debug_info && comp_opt.GenerateDebugInfoForInitalValue) // { // if (InitalValue.Location != null) // MarkSequencePoint(InitalValue.Location); // } if (NETGeneratorTools.IsBoundedArray(ti)) { lb = il.DeclareLocal(ti.arr_fld.FieldType); il.Emit(OpCodes.Ldsfld, fb); il.Emit(OpCodes.Ldfld, ti.arr_fld); } else { lb = il.DeclareLocal(ti.tp); il.Emit(OpCodes.Ldsfld, fb); } il.Emit(OpCodes.Stloc, lb); if (rank == 1) GenerateArrayInitCode(il, lb, InitalValue, arr_type); else GenerateNDimArrayInitCode(il, lb, InitalValue, arr_type, rank); } }
private static bool IsInvalidInitializer(IArrayInitializer initializer) => initializer.IsInvalid;
private int[] get_sizes(IArrayInitializer InitalValue, int rank) { List<int> sizes = new List<int>(); sizes.Add(InitalValue.ElementValues.Length); if (rank > 1) sizes.AddRange(get_sizes(InitalValue.ElementValues[0] as IArrayInitializer, rank - 1)); return sizes.ToArray(); }
private void CreateArrayForClassField(ILGenerator il, FieldBuilder fb, TypeInfo ti, IArrayInitializer InitalValue, ITypeNode ArrayType) { int rank = 1; il.Emit(OpCodes.Ldarg_0); if (NETGeneratorTools.IsBoundedArray(ti)) NETGeneratorTools.CreateBoundedArray(il, fb, ti); else { rank = get_rank(ArrayType); if (rank == 1) CreateUnsizedArray(il, fb, helper.GetTypeReference(InitalValue.ElementType), InitalValue.ElementValues.Length); else CreateNDimUnsizedArray(il, fb, ArrayType, helper.GetTypeReference(InitalValue.ElementType), rank, get_sizes(InitalValue, rank)); } if (InitalValue != null) { LocalBuilder lb = null; il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, fb); if (NETGeneratorTools.IsBoundedArray(ti)) { lb = il.DeclareLocal(ti.arr_fld.FieldType); il.Emit(OpCodes.Ldfld, ti.arr_fld); } else { lb = il.DeclareLocal(ti.tp); } il.Emit(OpCodes.Stloc, lb); if (rank == 1) GenerateArrayInitCode(il, lb, InitalValue, ArrayType); else GenerateNDimArrayInitCode(il, lb, InitalValue, ArrayType, rank); } }
public override void visit(IArrayInitializer value) { TypeInfo ti = helper.GetTypeReference(value.type); LocalBuilder lb = il.DeclareLocal(ti.tp); CreateArrayLocalVariable(il, lb, ti, value, value.type); il.Emit(OpCodes.Ldloc, lb); }
public override void VisitArrayInitializer(IArrayInitializer operation) { VisitArray(operation.ElementValues); }
public void visit(IArrayInitializer value) { }
private void EmitArrayIndex(ILGenerator il, MethodInfo set_meth, LocalBuilder lb, IArrayInitializer InitalValue, int rank, int act_rank, List<int> indices) { IExpressionNode[] ElementValues = InitalValue.ElementValues; for (int i = 0; i < ElementValues.Length; i++) { if (indices.Count < act_rank) indices.Add(i); else indices[indices.Count - rank] = i; if (rank > 1) EmitArrayIndex(il, set_meth, lb, ElementValues[i] as IArrayInitializer, rank - 1, act_rank, indices); else { if (indices.Count < act_rank) indices.Add(i); else indices[indices.Count - rank] = i; il.Emit(OpCodes.Ldloc, lb); for (int j = 0; j < indices.Count; j++) il.Emit(OpCodes.Ldc_I4, indices[j]); ILGenerator tmp_il = this.il; this.il = il; ElementValues[i].visit(this); EmitBox(ElementValues[i], lb.LocalType.GetElementType()); this.il = tmp_il; il.Emit(OpCodes.Call, set_meth); } } }
public override IOperation VisitArrayInitializer(IArrayInitializer operation, object argument) { return(new ArrayInitializer(VisitArray(operation.ElementValues), ((Operation)operation).SemanticModel, operation.Syntax, operation.Type, operation.ConstantValue, operation.IsImplicit)); }
private void GenerateNDimArrayInitCode(ILGenerator il, LocalBuilder lb, IArrayInitializer InitalValue, ITypeNode ArrayType, int rank) { IExpressionNode[] ElementValues = InitalValue.ElementValues; Type elem_type = helper.GetTypeReference(ArrayType.element_type).tp; MethodInfo set_meth = null; if (ArrayType is ICompiledTypeNode) set_meth = lb.LocalType.GetMethod("Set"); else { List<Type> lst = new List<Type>(); for (int i = 0; i < rank; i++) lst.Add(TypeFactory.Int32Type); lst.Add(elem_type); set_meth = mb.GetArrayMethod(lb.LocalType, "Set", CallingConventions.HasThis, TypeFactory.VoidType, lst.ToArray()); } List<int> indices = new List<int>(); for (int i = 0; i < ElementValues.Length; i++) { if (i == 0) indices.Add(i); else indices[indices.Count - rank] = i; EmitArrayIndex(il, set_meth, lb, ElementValues[i] as IArrayInitializer, rank - 1, rank, indices); } }
private void GenerateArrayInitCode(ILGenerator il, LocalBuilder lb, IArrayInitializer InitalValue, ITypeNode ArrayType) { IExpressionNode[] ElementValues = InitalValue.ElementValues; if (ElementValues[0] is IArrayInitializer) { bool is_unsized_array; Type FieldType, ArrType; int rank = get_rank(ElementValues[0].type); TypeInfo ti = helper.GetTypeReference(ElementValues[0].type); if (NETGeneratorTools.IsBoundedArray(ti)) { is_unsized_array = false; ArrType = ti.tp; FieldType = ti.arr_fld.FieldType; } else { is_unsized_array = true; ArrType = helper.GetTypeReference(ElementValues[0].type).tp; FieldType = ArrType; } LocalBuilder llb = il.DeclareLocal(FieldType); for (int i = 0; i < ElementValues.Length; i++) { il.Emit(OpCodes.Ldloc, lb); PushIntConst(il, i); if (!is_unsized_array) { il.Emit(OpCodes.Ldelem, ArrType); il.Emit(OpCodes.Ldfld, ti.arr_fld); } else { //il.Emit(OpCodes.Ldelema, ArrType); if (rank > 1) CreateNDimUnsizedArray(il, (ElementValues[i] as IArrayInitializer).type, helper.GetTypeReference((ElementValues[i] as IArrayInitializer).type.element_type), rank, get_sizes(ElementValues[0] as IArrayInitializer, rank), lb.LocalType.GetElementType()); else CreateUnsizedArray(il, helper.GetTypeReference((ElementValues[i] as IArrayInitializer).type.element_type), (ElementValues[0] as IArrayInitializer).ElementValues.Length, lb.LocalType.GetElementType()); il.Emit(OpCodes.Stelem, ArrType); il.Emit(OpCodes.Ldloc, lb); PushIntConst(il, i); il.Emit(OpCodes.Ldelem, ArrType); //il.Emit(OpCodes.Ldelem_Ref); } il.Emit(OpCodes.Stloc, llb); if (rank > 1) GenerateNDimArrayInitCode(il, llb, ElementValues[i] as IArrayInitializer, ElementValues[i].type, rank); else GenerateArrayInitCode(il, llb, ElementValues[i] as IArrayInitializer, ArrayType); } } else if (ElementValues[0] is IRecordConstantNode || ElementValues[0] is IRecordInitializer) { TypeInfo ti = helper.GetTypeReference(ElementValues[0].type); LocalBuilder llb = il.DeclareLocal(ti.tp.MakePointerType()); for (int i = 0; i < ElementValues.Length; i++) { il.Emit(OpCodes.Ldloc, lb); PushIntConst(il, i); il.Emit(OpCodes.Ldelema, ti.tp); il.Emit(OpCodes.Stloc, llb); if (ElementValues[0] is IRecordConstantNode) GenerateRecordInitCode(il, llb, ElementValues[i] as IRecordConstantNode); else GenerateRecordInitCode(il, llb, ElementValues[i] as IRecordInitializer, true); } } else for (int i = 0; i < ElementValues.Length; i++) { il.Emit(OpCodes.Ldloc, lb); PushIntConst(il, i); ILGenerator ilb = this.il; TypeInfo ti = helper.GetTypeReference(ElementValues[i].type); if (ti != null && ti.is_set) { this.il = il; IConstantNode cn1 = null; IConstantNode cn2 = null; if (ArrayType != null && ArrayType.element_type.element_type is ICommonTypeNode) { cn1 = (ArrayType.element_type.element_type as ICommonTypeNode).lower_value; cn2 = (ArrayType.element_type.element_type as ICommonTypeNode).upper_value; } if (cn1 != null && cn2 != null) { cn1.visit(this); il.Emit(OpCodes.Box, helper.GetTypeReference(cn1.type).tp); cn2.visit(this); il.Emit(OpCodes.Box, helper.GetTypeReference(cn2.type).tp); } else { il.Emit(OpCodes.Ldnull); il.Emit(OpCodes.Ldnull); } il.Emit(OpCodes.Newobj, ti.def_cnstr); il.Emit(OpCodes.Stelem_Ref); il.Emit(OpCodes.Ldloc, lb); PushIntConst(il, i); this.il = ilb; //il.Emit(OpCodes.Ldelem_Ref); } if (ti != null && ti.tp.IsValueType && !TypeFactory.IsStandType(ti.tp) && !ti.tp.IsEnum) il.Emit(OpCodes.Ldelema, ti.tp); else if (ti != null && ti.assign_meth != null) il.Emit(OpCodes.Ldelem_Ref); this.il = il; ElementValues[i].visit(this); if (ti != null && ti.assign_meth != null) { il.Emit(OpCodes.Call, ti.assign_meth); this.il = ilb; continue; } bool box = EmitBox(ElementValues[i], lb.LocalType.GetElementType()); this.il = ilb; if (ti != null && !box) NETGeneratorTools.PushStelem(il, ti.tp); else il.Emit(OpCodes.Stelem_Ref); } }
/// <inheritdoc /> public override Expression VisitArrayInitializer(IArrayInitializer operation, LocalBinder argument) { return(base.VisitArrayInitializer(operation, argument)); }
public void visit(IArrayInitializer value) { throw new System.NotSupportedException(value.GetType().ToString()); }