// Copyright (c) AlphaSierraPapa for the SharpDevelop Team // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software // without restriction, including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons // to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; using System.Collections.Generic; using System.Linq; #if CS90 using System.Runtime.InteropServices; #endif namespace LocalFunctions { internal class LocalFunctions { [AttributeUsage(AttributeTargets.All)] internal class MyAttribute : Attribute { } public class Generic where T1 : struct, ICloneable, IConvertible { public int MixedLocalFunction() where T2 : ICloneable, IConvertible { #pragma warning disable CS0219 T2 t2 = default(T2); object z = this; for (int i = 0; i < 10; i++) { int i2 = 0; i2 += NonStaticMethod(0); #if CS90 [My] [return: My] int NonStaticMethod<[My] T3>([My] int unused) #else int NonStaticMethod(int unused) #endif { t2 = default(T2); int l = 0; return NonStaticMethod3() + NonStaticMethod3() + z.GetHashCode(); int NonStaticMethod3() { return i2 + l + NonStaticMethod(0) + StaticMethod(); } } } return MixedLocalFunction() + MixedLocalFunction() + StaticMethod() + StaticMethod() + NonStaticMethod2() + StaticMethod4(null) + StaticMethod5(); int NonStaticMethod2() { return GetHashCode(); } #if CS80 static int StaticMethod() where T3 : struct #else int StaticMethod() where T3 : struct #endif { return typeof(T1).Name.Length + typeof(T2).Name.Length + typeof(T3).Name.Length + StaticMethod() + StaticMethod2() + StaticMethod3(); } #if CS80 static int StaticMethod2() where T3 : struct where T4 : Enum #else int StaticMethod2() where T3 : struct where T4 : Enum #endif { return typeof(T1).Name.Length + typeof(T2).Name.Length + typeof(T3).Name.Length + typeof(T4).Name.Length + StaticMethod() + StaticMethod2(); } #pragma warning disable CS8387 #if CS80 static int StaticMethod3() where T2 : IConvertible where T3 : struct where T4 : Enum #else int StaticMethod3() where T2 : IConvertible where T3 : struct where T4 : Enum #endif #pragma warning restore CS8387 { return typeof(T2).Name.Length; } #if CS80 static int StaticMethod4(T dd) #else int StaticMethod4(T dd) #endif { return 0; } #if CS80 static int StaticMethod5() #else int StaticMethod5() #endif { int k = 0; return k + NonStaticMethod4(); int NonStaticMethod4() { return k; } } #pragma warning restore CS0219 } public int MixedLocalFunction2Delegate() where T2 : ICloneable, IConvertible { T2 t2 = default(T2); object z = this; for (int i = 0; i < 10; i++) { int i2 = 0; i2 += StaticInvokeAsFunc(NonStaticMethod); int NonStaticMethod() { t2 = default(T2); int l = 0; return StaticInvokeAsFunc(NonStaticMethod3) + StaticInvokeAsFunc(NonStaticMethod3) + z.GetHashCode(); int NonStaticMethod3() { return i2 + l + StaticInvokeAsFunc(NonStaticMethod) + StaticInvokeAsFunc(StaticMethod); } } } Console.WriteLine(t2); return StaticInvokeAsFunc(MixedLocalFunction2Delegate) + StaticInvokeAsFunc(MixedLocalFunction2Delegate) + StaticInvokeAsFunc(StaticMethod) + StaticInvokeAsFunc(StaticMethod) + StaticInvokeAsFunc(NonStaticMethod2) + StaticInvokeAsFunc(StaticMethod4) + new Func(StaticMethod5)(null) + StaticInvokeAsFunc2(StaticMethod5) + new Func, int>(StaticInvokeAsFunc2)(StaticMethod5); int NonStaticMethod2() { return GetHashCode(); } #if CS80 static int StaticInvokeAsFunc(Func func) #else int StaticInvokeAsFunc(Func func) #endif { return func(); } #if CS80 static int StaticInvokeAsFunc2(Func func) #else int StaticInvokeAsFunc2(Func func) #endif { return func(default(T)); } #if CS80 static int StaticMethod() where T3 : struct #else int StaticMethod() where T3 : struct #endif { return typeof(T1).Name.Length + typeof(T2).Name.Length + typeof(T3).Name.Length + StaticInvokeAsFunc(StaticMethod) + StaticInvokeAsFunc(StaticMethod2) + StaticInvokeAsFunc(StaticMethod3); } #if CS80 static int StaticMethod2() where T3 : struct where T4 : Enum #else int StaticMethod2() where T3 : struct where T4 : Enum #endif { return typeof(T1).Name.Length + typeof(T2).Name.Length + typeof(T3).Name.Length + typeof(T4).Name.Length + StaticInvokeAsFunc(StaticMethod) + StaticInvokeAsFunc(StaticMethod2); } #pragma warning disable CS8387 #if CS80 static int StaticMethod3() where T2 : IConvertible where T3 : struct where T4 : Enum #else int StaticMethod3() where T2 : IConvertible where T3 : struct where T4 : Enum #endif #pragma warning restore CS8387 { return typeof(T2).Name.Length; } #if CS80 static int StaticMethod4() #else int StaticMethod4() #endif { int k = 0; return k + StaticInvokeAsFunc(NonStaticMethod4); int NonStaticMethod4() { return k; } } #if CS80 static int StaticMethod5(T dd) #else int StaticMethod5(T dd) #endif { return 0; } } public static void Test_CaptureT() { #pragma warning disable CS0219 T2 t2 = default(T2); Method(); void Method() { t2 = default(T2); T2 t2x = t2; T3 t3 = default(T3); Method2(); void Method2() { t2 = default(T2); t2x = t2; t3 = default(T3); } } #pragma warning restore CS0219 } public void TestGenericArgs() where T2 : List { ZZ(null); ZZ3(null); #if CS80 static void Nop(T data) #else void Nop(T data) #endif { } #if CS80 static void ZZ(T3 t3) where T3 : T2 #else void ZZ(T3 t3) where T3 : T2 #endif { Nop>(t3); ZZ2(t3); ZZ4(); void ZZ4() { Nop>(t3); } } #if CS80 static void ZZ2(T3 t3) #else void ZZ2(T3 t3) #endif { Nop>((List)(object)t3); } #if CS80 static void ZZ3(T3 t3) #else void ZZ3(T3 t3) #endif { Nop>((List)(object)t3); } } #if false public void GenericArgsWithAnonymousType() { Method(); #if CS80 static void Method() #else void Method() #endif { int i = 0; var obj2 = new { A = 1 }; Method2(obj2); Method3(obj2); void Method2(T3 obj1) { //keep nested i = 0; } #if CS80 static void Method3(T3 obj1) #else void Method3(T3 obj1) #endif { } } } #if CS80 public void NameConflict() { int i = 0; Method(); void Method() { Method(); void Method() { Method(); i = 0; void Method() { i = 0; Method(); static void Method() { } } } } } #endif #endif } private int field; private Lazy nonCapturinglocalFunctionInLambda = new Lazy(delegate { return CreateValue(); #if CS80 static object CreateValue() #else object CreateValue() #endif { return null; } }); private Lazy capturinglocalFunctionInLambda = new Lazy(delegate { int x = 42; return Do(); object Do() { return CreateValue(); int CreateValue() { return x; } } }); private static void Test(int x) { } private static int GetInt(string a) { return a.Length; } private static string GetString(int a) { return a.ToString(); } public static void StaticContextNoCapture(int length) { for (int i = 0; i < length; i++) { LocalWrite("Hello " + i); } #if CS80 static void LocalWrite(string s) #else void LocalWrite(string s) #endif { Console.WriteLine(s); } } public static void StaticContextSimpleCapture(int length) { for (int i = 0; i < length; i++) { LocalWrite(); } void LocalWrite() { Console.WriteLine("Hello " + length); } } public static void StaticContextCaptureForLoopVariable(int length) { int i; for (i = 0; i < length; i++) { LocalWrite(); } void LocalWrite() { Console.WriteLine("Hello " + i + "/" + length); } } public void ContextNoCapture() { for (int i = 0; i < field; i++) { LocalWrite("Hello " + i); } #if CS80 static void LocalWrite(string s) #else void LocalWrite(string s) #endif { Console.WriteLine(s); } } public void ContextSimpleCapture() { for (int i = 0; i < field; i++) { LocalWrite(); } void LocalWrite() { Console.WriteLine("Hello " + field); } } public void ContextCaptureForLoopVariable() { int i; for (i = 0; i < field; i++) { LocalWrite(); } void LocalWrite() { Console.WriteLine("Hello " + i + "/" + field); } } public void CapturedOutsideLoop() { int i = 0; while (i < field) { i = GetInt("asdf"); LocalWrite(); } void LocalWrite() { Console.WriteLine("Hello " + i + "/" + field); } } public void CapturedInForeachLoop(IEnumerable args) { foreach (string arg2 in args) { string arg = arg2; LocalWrite(); void LocalWrite() { Console.WriteLine("Hello " + arg); } } } public void Overloading() { Test(5); LocalFunctions.Test(2); #if CS80 static void Test(int x) #else void Test(int x) #endif { Console.WriteLine("x: {0}", x); } } private void Name() { } private void LocalFunctionHidingMethod() { Action action = this.Name; Name(); action(); #if CS80 static void Name() #else void Name() #endif { } } public void NamedArgument() { Use(Get(1), Get(2), Get(3)); Use(Get(1), c: Get(2), b: Get(3)); #if CS80 static int Get(int i) #else int Get(int i) #endif { return i; } #if CS80 static void Use(int a, int b, int c) #else void Use(int a, int b, int c) #endif { Console.WriteLine(a + b + c); } } public static Func LambdaInLocalFunction() { int x = (int)Math.Pow(2.0, 10.0); return Create(); Func Create() { return () => x; } } public static Func MethodRef() { int x = (int)Math.Pow(2.0, 10.0); Enumerable.Range(1, 100).Select(LocalFunction); return null; int LocalFunction(int y) { return x * y; } } public static int Fib(int i) { return FibHelper(i); #if CS80 static int FibHelper(int n) #else int FibHelper(int n) #endif { if (n <= 0) { return 0; } return FibHelper(n - 1) + FibHelper(n - 2); } } public int MutuallyRecursiveLocalFunctions() { return B(4) + C(3); #if CS80 static int A(int i) #else int A(int i) #endif { if (i > 0) { return A(i - 1) + 2 * B(i - 1) + 3 * C(i - 1); } return 1; } #if CS80 static int B(int i) #else int B(int i) #endif { if (i > 0) { return 3 * A(i - 1) + B(i - 1); } return 1; } #if CS80 static int C(int i) #else int C(int i) #endif { if (i > 0) { return 2 * A(i - 1) + C(i - 1); } return 1; } } public static int NestedLocalFunctions(int i) { return A(); int A() { double x = Math.Pow(10.0, 2.0); return B(); int B() { return i + (int)x; } } } public static int LocalFunctionInLambda(IEnumerable xs) { return xs.First(delegate (int x) { return Do(); bool Do() { return x == 3; } }); } public static IEnumerable YieldReturn(int n) { return GetNumbers(); IEnumerable GetNumbers() { for (int i = 0; i < n; i++) { yield return i; } } } public void WriteCapturedParameter(int i) { ParamWrite(); Console.WriteLine(i); void ParamWrite() { i++; } } //public static void LocalFunctionInUsing() //{ // using (MemoryStream memoryStream = new MemoryStream()) { // Do(); // void Do() // { // memoryStream.WriteByte(42); // } // } //} public void NestedCapture1() { Method(null); #if CS80 static Action Method(Action action) #else Action Method(Action action) #endif { return Method2; void Method2(object containerBuilder) { Method3(containerBuilder); } void Method3(object containerBuilder) { action(containerBuilder); } } } public int NestedCapture2() { return Method(); #if CS80 static int Method() #else int Method() #endif { int t0 = 0; return ZZZ(); int ZZZ() { t0 = 0; int t2 = t0; return new Func(ZZZ3)(); int ZZZ3() { t0 = 0; t2 = 0; return ZZZ2(); } } int ZZZ2() { t0 = 0; int t3 = t0; return new Func(ZZZ4)(); int ZZZ4() { t0 = 0; t3 = 0; return 0; } } } } public int Issue1798_NestedCapture2() { return Method(); #if CS80 static int Method() #else int Method() #endif { int t0 = 0; return ZZZ(); int ZZZ() { t0 = 0; int t2 = t0; return ((Func)delegate { t0 = 0; t2 = 0; return ZZZ2(); })(); } int ZZZ2() { t0 = 0; int t3 = t0; #if !OPT Func func = delegate { #else return ((Func)delegate { #endif t0 = 0; t3 = 0; return 0; #if !OPT }; return func(); #else })(); #endif } } } public int Issue1798_NestedCapture2b() { return Method(); #if CS80 static int Method() #else int Method() #endif { int t0 = 0; return ZZZ() + ZZZ2(); int ZZZ() { t0 = 0; int t2 = t0; return ((Func)delegate { t0 = 0; t2 = 0; return ZZZ2(); })(); } int ZZZ2() { t0 = 0; int t3 = t0; #if !OPT Func func = delegate { #else return ((Func)delegate { #endif t0 = 0; t3 = 0; return 0; #if !OPT }; return func(); #else })(); #endif } } } #if CS90 public void Issue2196() { EnumWindows(0L, 0L); [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "EnumWindows")] static extern int EnumWindows(long hWnd, long lParam); } #endif } }