public override Expression DoResolve(ResolveContext ec) { if (type != null) { return(this); } dimensions = rank.Dimension; best_type_inference = new TypeInference(ec.compilation, ec.conversions); if (!ResolveInitializers(ec)) { return(null); } bool success; array_element_type = best_type_inference.GetBestCommonType(Initializers.Elements, out success); best_type_inference = null; if (!success || array_element_type == null || arguments.Count != rank.Dimension) { ec.Report.Error(0, loc, "The type of an implicitly typed array cannot be inferred from the initializer. Try specifying array type explicitly"); return(null); } // // At this point we found common base type for all initializer elements // but we have to be sure that all static initializer elements are of // same type // UnifyInitializerElement(ec); ResolvedType = new ArrayType(ec.compilation, array_element_type, dimensions); eclass = ExprClass.Value; return(this); }
/// <summary> /// Resolves an array creation. /// </summary> /// <param name="elementType"> /// The array element type. /// Pass null to resolve an implicitly-typed array creation. /// </param> /// <param name="sizeArguments"> /// The size arguments. /// The length of this array will be used as the number of dimensions of the array type. /// The resolver may mutate this array to wrap elements in <see cref="ConversionResolveResult"/>s! /// </param> /// <param name="initializerElements"> /// The initializer elements. May be null if no array initializer was specified. /// The resolver may mutate this array to wrap elements in <see cref="ConversionResolveResult"/>s! /// </param> public ArrayCreateResolveResult ResolveArrayCreation(IType elementType, ResolveResult[] sizeArguments, ResolveResult[] initializerElements = null) { int dimensions = sizeArguments.Length; if (dimensions == 0) throw new ArgumentException("sizeArguments.Length must not be 0"); if (elementType == null) { TypeInference typeInference = new TypeInference(compilation, conversions); bool success; elementType = typeInference.GetBestCommonType(initializerElements, out success); } IType arrayType = new ArrayType(compilation, elementType, dimensions); AdjustArrayAccessArguments(sizeArguments); if (initializerElements != null) { for (int i = 0; i < initializerElements.Length; i++) { initializerElements[i] = Convert(initializerElements[i], elementType); } } return new ArrayCreateResolveResult(arrayType, sizeArguments, initializerElements); }