// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. /*============================================================ ** ** This file exists to contain miscellaneous module-level attributes ** and other miscellaneous stuff. ** ** ** ===========================================================*/ using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Collections.Generic; using System.Reflection; using System.Security; using System.StubHelpers; using System.Threading.Tasks; #if FEATURE_COMINTEROP using System.Runtime.InteropServices.WindowsRuntime; #endif // FEATURE_COMINTEROP [assembly: DefaultDependencyAttribute(LoadHint.Always)] // mscorlib would like to have its literal strings frozen if possible [assembly: System.Runtime.CompilerServices.StringFreezingAttribute()] namespace System { static class CommonlyUsedGenericInstantiations { // This method is purely an aid for NGen to statically deduce which // instantiations to save in the ngen image. // Otherwise, the JIT-compiler gets used, which is bad for working-set. // Note that IBC can provide this information too. // However, this helps in keeping the JIT-compiler out even for // test scenarios which do not use IBC. // This can be removed after V2, when we implement other schemes // of keeping the JIT-compiler out for generic instantiations. // Method marked as NoOptimization as we don't want the JIT to // inline any methods or take any short-circuit paths since the // instantiation closure process is driven by "fixup" references // left in the final code stream. [MethodImplAttribute(MethodImplOptions.NoOptimization)] static CommonlyUsedGenericInstantiations() { // Make absolutely sure we include some of the most common // instantiations here in mscorlib's ngen image. // Note that reference type instantiations are already included // automatically for us. // Need to sort non null, len > 1 array or paths will short-circuit Array.Sort(new double[1]); Array.Sort(new int[1]); Array.Sort(new IntPtr[1]); new ArraySegment(new byte[1], 0, 0); new Dictionary(); new Dictionary(); new Dictionary(); new Dictionary(); // Added for Visual Studio 2010 new Dictionary(); new Dictionary(); new Dictionary(); new Dictionary(); new Dictionary(); new Dictionary(); new Dictionary(); new Dictionary(); new Dictionary(); new Dictionary(); new Dictionary(); // Added for Visual Studio 2010 new Dictionary(); // NCL team needs this new Dictionary(); new Dictionary(); new Dictionary(); // to genereate mdil for Dictionary instantiation when key is user defined value type new Dictionary(); // Microsoft.Windows.Design new Dictionary(); new EnumEqualityComparer(); // Microsoft.Expression.DesignModel new Dictionary>(); new Dictionary, Object>(); NullableHelper(); NullableHelper(); NullableHelper(); NullableHelper(); NullableHelper(); NullableHelper(); NullableHelper(); NullableHelper(); NullableHelper(); NullableHelper(); NullableHelper(); NullableHelper(); NullableHelper(); // For SQL new List(); new List(); new List(); new List(); new List(); new List(); new List(); new List(); new List(); new List(); new List(); new List(); new List(); new List(); new List(); new List(); new List(); new List>(); new List(); // NCL team needs this new List(); new KeyValuePair('\0', UInt16.MinValue); new KeyValuePair(UInt16.MinValue, Double.MinValue); new KeyValuePair(String.Empty, Int32.MinValue); new KeyValuePair(Int32.MinValue, Int32.MinValue); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); SZArrayHelper(null); #pragma warning disable 4014 // This is necessary to generate MDIL for AsyncVoidMethodBuilder AsyncHelper(); AsyncHelper2(); AsyncHelper3(); #pragma warning restore 4014 } private static T NullableHelper() where T : struct { Nullable.Compare(null, null); Nullable.Equals(null, null); Nullable nullable = new Nullable(); return nullable.GetValueOrDefault(); } private static void SZArrayHelper(SZArrayHelper oSZArrayHelper) { // Instantiate common methods for IList implementation on Array oSZArrayHelper.get_Count(); oSZArrayHelper.get_Item(0); oSZArrayHelper.GetEnumerator(); } // System.Runtime.CompilerServices.AsyncVoidMethodBuilder // System.Runtime.CompilerServices.TaskAwaiter private static async void AsyncHelper() { await Task.Delay(1); } // System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[System.__Canon] // System.Runtime.CompilerServices.TaskAwaiter'[System.__Canon] private static async Task AsyncHelper2() { return await Task.FromResult(""); } // System.Runtime.CompilerServices.AsyncTaskMethodBuilder // System.Runtime.CompilerServices.AsyncTaskMethodBuilder'1[VoidTaskResult] private static async Task AsyncHelper3() { await Task.FromResult(""); } #if FEATURE_COMINTEROP // Similar to CommonlyUsedGenericInstantiations but for instantiations of marshaling stubs used // for WinRT redirected interfaces. Note that we do care about reference types here as well because, // say, IList and IList cannot share marshaling stubs. // The methods below "call" most commonly used stub methods on redirected interfaces and take arguments // typed as matching instantiations of mscorlib copies of WinRT interfaces (IIterable, IVector, // IMap, ...) which is necessary to generate all required IL stubs. private static void CommonlyUsedWinRTRedirectedInterfaceStubs() { WinRT_IEnumerable(null, null, null); WinRT_IEnumerable(null, null, null); WinRT_IEnumerable(null, null, null); WinRT_IEnumerable(null, null, null); WinRT_IEnumerable(null, null, null); WinRT_IEnumerable(null, null, null); WinRT_IEnumerable(null, null, null); WinRT_IEnumerable(null, null, null); WinRT_IEnumerable(null, null, null); WinRT_IEnumerable(null, null, null); // The underlying WinRT types for shared instantiations have to be referenced explicitly. // They are not guaranteeed to be created indirectly because of generic code sharing. WinRT_IEnumerable(null, null, null); typeof(IIterable).ToString(); typeof(IIterator).ToString(); WinRT_IEnumerable(null, null, null); typeof(IIterable).ToString(); typeof(IIterator).ToString(); WinRT_IList(null, null, null, null); WinRT_IList(null, null, null, null); typeof(IVector).ToString(); WinRT_IList(null, null, null, null); typeof(IVector).ToString(); WinRT_IReadOnlyList(null, null, null); WinRT_IReadOnlyList(null, null, null); typeof(IVectorView).ToString(); WinRT_IReadOnlyList(null, null, null); typeof(IVectorView).ToString(); WinRT_IDictionary(null, null, null, null); typeof(IMap).ToString(); WinRT_IDictionary(null, null, null, null); typeof(IMap).ToString(); WinRT_IDictionary(null, null, null, null); typeof(IMap).ToString(); WinRT_IDictionary(null, null, null, null); typeof(IMap).ToString(); WinRT_IReadOnlyDictionary(null, null, null, null); typeof(IMapView).ToString(); WinRT_IReadOnlyDictionary(null, null, null, null); typeof(IMapView).ToString(); WinRT_IReadOnlyDictionary(null, null, null, null); typeof(IMapView).ToString(); WinRT_IReadOnlyDictionary(null, null, null, null); typeof(IMapView).ToString(); WinRT_Nullable(); WinRT_Nullable(); WinRT_Nullable(); WinRT_Nullable(); WinRT_Nullable(); WinRT_Nullable(); WinRT_Nullable(); WinRT_Nullable(); } private static void WinRT_IEnumerable(IterableToEnumerableAdapter iterableToEnumerableAdapter, EnumerableToIterableAdapter enumerableToIterableAdapter, IIterable iterable) { // instantiate stubs for the one method on IEnumerable and the one method on IIterable iterableToEnumerableAdapter.GetEnumerator_Stub(); enumerableToIterableAdapter.First_Stub(); } private static void WinRT_IList(VectorToListAdapter vectorToListAdapter, VectorToCollectionAdapter vectorToCollectionAdapter, ListToVectorAdapter listToVectorAdapter, IVector vector) { WinRT_IEnumerable(null, null, null); // instantiate stubs for commonly used methods on IList and ICollection vectorToListAdapter.Indexer_Get(0); vectorToListAdapter.Indexer_Set(0, default(T)); vectorToListAdapter.Insert(0, default(T)); vectorToListAdapter.RemoveAt(0); vectorToCollectionAdapter.Count(); vectorToCollectionAdapter.Add(default(T)); vectorToCollectionAdapter.Clear(); // instantiate stubs for commonly used methods on IVector listToVectorAdapter.GetAt(0); listToVectorAdapter.Size(); listToVectorAdapter.SetAt(0, default(T)); listToVectorAdapter.InsertAt(0, default(T)); listToVectorAdapter.RemoveAt(0); listToVectorAdapter.Append(default(T)); listToVectorAdapter.RemoveAtEnd(); listToVectorAdapter.Clear(); } private static void WinRT_IReadOnlyCollection(VectorViewToReadOnlyCollectionAdapter vectorViewToReadOnlyCollectionAdapter) { WinRT_IEnumerable(null, null, null); // instantiate stubs for commonly used methods on IReadOnlyCollection vectorViewToReadOnlyCollectionAdapter.Count(); } private static void WinRT_IReadOnlyList(IVectorViewToIReadOnlyListAdapter vectorToListAdapter, IReadOnlyListToIVectorViewAdapter listToVectorAdapter, IVectorView vectorView) { WinRT_IEnumerable(null, null, null); WinRT_IReadOnlyCollection(null); // instantiate stubs for commonly used methods on IReadOnlyList vectorToListAdapter.Indexer_Get(0); // instantiate stubs for commonly used methods on IVectorView listToVectorAdapter.GetAt(0); listToVectorAdapter.Size(); } private static void WinRT_IDictionary(MapToDictionaryAdapter mapToDictionaryAdapter, MapToCollectionAdapter mapToCollectionAdapter, DictionaryToMapAdapter dictionaryToMapAdapter, IMap map) { WinRT_IEnumerable>(null, null, null); // instantiate stubs for commonly used methods on IDictionary and ICollection> V dummy; mapToDictionaryAdapter.Indexer_Get(default(K)); mapToDictionaryAdapter.Indexer_Set(default(K), default(V)); mapToDictionaryAdapter.ContainsKey(default(K)); mapToDictionaryAdapter.Add(default(K), default(V)); mapToDictionaryAdapter.Remove(default(K)); mapToDictionaryAdapter.TryGetValue(default(K), out dummy); mapToCollectionAdapter.Count(); mapToCollectionAdapter.Add(new KeyValuePair(default(K), default(V))); mapToCollectionAdapter.Clear(); // instantiate stubs for commonly used methods on IMap dictionaryToMapAdapter.Lookup(default(K)); dictionaryToMapAdapter.Size(); dictionaryToMapAdapter.HasKey(default(K)); dictionaryToMapAdapter.Insert(default(K), default(V)); dictionaryToMapAdapter.Remove(default(K)); dictionaryToMapAdapter.Clear(); } private static void WinRT_IReadOnlyDictionary(IMapViewToIReadOnlyDictionaryAdapter mapToDictionaryAdapter, IReadOnlyDictionaryToIMapViewAdapter dictionaryToMapAdapter, IMapView mapView, MapViewToReadOnlyCollectionAdapter mapViewToReadOnlyCollectionAdapter) { WinRT_IEnumerable>(null, null, null); WinRT_IReadOnlyCollection>(null); // instantiate stubs for commonly used methods on IReadOnlyDictionary V dummy; mapToDictionaryAdapter.Indexer_Get(default(K)); mapToDictionaryAdapter.ContainsKey(default(K)); mapToDictionaryAdapter.TryGetValue(default(K), out dummy); // instantiate stubs for commonly used methods in IReadOnlyCollection mapViewToReadOnlyCollectionAdapter.Count(); // instantiate stubs for commonly used methods on IMapView dictionaryToMapAdapter.Lookup(default(K)); dictionaryToMapAdapter.Size(); dictionaryToMapAdapter.HasKey(default(K)); } private static void WinRT_Nullable() where T : struct { Nullable nullable = new Nullable(); NullableMarshaler.ConvertToNative(ref nullable); NullableMarshaler.ConvertToManagedRetVoid(IntPtr.Zero, ref nullable); } #endif // FEATURE_COMINTEROP } }