1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
6 * copy of the license can be found in the License.html file at the root of this distribution. If
7 * you cannot locate the Apache License, Version 2.0, please send an email to
8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9 * by the terms of the Apache License, Version 2.0.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
18 using System.Diagnostics;
19 using Microsoft.Scripting.Utils;
21 namespace Microsoft.Scripting.Generation {
22 internal static partial class DelegateHelpers {
24 #region Generated Maximum Delegate Arity
26 // *** BEGIN GENERATED CODE ***
27 // generated by function: gen_max_delegate_arity from: generate_dynsites.py
29 private const int MaximumArity = 17;
31 // *** END GENERATED CODE ***
35 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
36 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
37 internal static Type MakeDelegate(Type[] types) {
38 Debug.Assert(types != null && types.Length > 0);
40 // Can only used predefined delegates if we have no byref types and
41 // the arity is small enough to fit in Func<...> or Action<...>
42 if (types.Length > MaximumArity || types.Any(t => t.IsByRef)) {
43 return MakeCustomDelegate(types);
46 Type returnType = types[types.Length - 1];
47 if (returnType == typeof(void)) {
48 types = types.RemoveLast();
49 switch (types.Length) {
50 case 0: return typeof(Action);
51 #region Generated Delegate Action Types
53 // *** BEGIN GENERATED CODE ***
54 // generated by function: gen_delegate_action from: generate_dynsites.py
56 case 1: return typeof(Action<>).MakeGenericType(types);
57 case 2: return typeof(Action<,>).MakeGenericType(types);
58 case 3: return typeof(Action<,,>).MakeGenericType(types);
59 case 4: return typeof(Action<,,,>).MakeGenericType(types);
60 case 5: return typeof(Action<,,,,>).MakeGenericType(types);
61 case 6: return typeof(Action<,,,,,>).MakeGenericType(types);
62 case 7: return typeof(Action<,,,,,,>).MakeGenericType(types);
63 case 8: return typeof(Action<,,,,,,,>).MakeGenericType(types);
64 case 9: return typeof(Action<,,,,,,,,>).MakeGenericType(types);
65 case 10: return typeof(Action<,,,,,,,,,>).MakeGenericType(types);
66 case 11: return typeof(Action<,,,,,,,,,,>).MakeGenericType(types);
67 case 12: return typeof(Action<,,,,,,,,,,,>).MakeGenericType(types);
68 case 13: return typeof(Action<,,,,,,,,,,,,>).MakeGenericType(types);
69 case 14: return typeof(Action<,,,,,,,,,,,,,>).MakeGenericType(types);
70 case 15: return typeof(Action<,,,,,,,,,,,,,,>).MakeGenericType(types);
71 case 16: return typeof(Action<,,,,,,,,,,,,,,,>).MakeGenericType(types);
73 // *** END GENERATED CODE ***
78 switch (types.Length) {
79 #region Generated Delegate Func Types
81 // *** BEGIN GENERATED CODE ***
82 // generated by function: gen_delegate_func from: generate_dynsites.py
84 case 1: return typeof(Func<>).MakeGenericType(types);
85 case 2: return typeof(Func<,>).MakeGenericType(types);
86 case 3: return typeof(Func<,,>).MakeGenericType(types);
87 case 4: return typeof(Func<,,,>).MakeGenericType(types);
88 case 5: return typeof(Func<,,,,>).MakeGenericType(types);
89 case 6: return typeof(Func<,,,,,>).MakeGenericType(types);
90 case 7: return typeof(Func<,,,,,,>).MakeGenericType(types);
91 case 8: return typeof(Func<,,,,,,,>).MakeGenericType(types);
92 case 9: return typeof(Func<,,,,,,,,>).MakeGenericType(types);
93 case 10: return typeof(Func<,,,,,,,,,>).MakeGenericType(types);
94 case 11: return typeof(Func<,,,,,,,,,,>).MakeGenericType(types);
95 case 12: return typeof(Func<,,,,,,,,,,,>).MakeGenericType(types);
96 case 13: return typeof(Func<,,,,,,,,,,,,>).MakeGenericType(types);
97 case 14: return typeof(Func<,,,,,,,,,,,,,>).MakeGenericType(types);
98 case 15: return typeof(Func<,,,,,,,,,,,,,,>).MakeGenericType(types);
99 case 16: return typeof(Func<,,,,,,,,,,,,,,,>).MakeGenericType(types);
100 case 17: return typeof(Func<,,,,,,,,,,,,,,,,>).MakeGenericType(types);
102 // *** END GENERATED CODE ***
107 throw Assert.Unreachable;