Esempio n. 1
0
        public void TestLambdaMethods()
        {
            var    sourceTemplate = @"
using System;

class C
{{
    Func<int> {0}(int p)
    {{
        return () => p - 1;
    }}

    Func<int> {0}1(int p)
    {{
        return () => p + 1;
    }}
}}
";
            int    padding        = GeneratedNames.MakeLambdaMethodName("A", -1, 0, 0).Length - 1;
            string longName       = s_longSymbolName.Substring(padding);
            var    source         = string.Format(sourceTemplate, longName);
            var    comp           = CreateCompilationWithMscorlib(source);

            comp.VerifyDiagnostics();
            comp.VerifyEmitDiagnostics(
                // (13,16): error CS7013: Name '<longName + 1>b__3' exceeds the maximum length allowed in metadata.
                //         return () => p + 1;
                Diagnostic(ErrorCode.ERR_MetadataNameTooLong, "() => p + 1").WithArguments("<" + longName + "1>b__0").WithLocation(13, 16));
        }
Esempio n. 2
0
        internal SynthesizedLambdaMethod(NamedTypeSymbol containingType, MethodSymbol topLevelMethod, BoundLambda node, bool isStatic, TypeCompilationState compilationState)
            : base(containingType,
                   node.Symbol,
                   null,
                   node.SyntaxTree.GetReference(node.Body.Syntax),
                   node.Syntax.GetLocation(),
                   GeneratedNames.MakeLambdaMethodName(topLevelMethod.Name, compilationState.GenerateTempNumber()),
                   (containingType is LambdaFrame ? DeclarationModifiers.Internal : DeclarationModifiers.Private)
                   | (isStatic ? DeclarationModifiers.Static : 0)
                   | (node.Symbol.IsAsync ? DeclarationModifiers.Async : 0))
        {
            TypeMap typeMap;
            ImmutableArray <TypeParameterSymbol> typeParameters;
            LambdaFrame lambdaFrame;

            if (!topLevelMethod.IsGenericMethod)
            {
                typeMap        = TypeMap.Empty;
                typeParameters = ImmutableArray <TypeParameterSymbol> .Empty;
            }
            else if ((object)(lambdaFrame = this.ContainingType as LambdaFrame) != null)
            {
                typeMap        = lambdaFrame.TypeMap;
                typeParameters = ImmutableArray <TypeParameterSymbol> .Empty;
            }
            else
            {
                typeMap = TypeMap.Empty.WithAlphaRename(topLevelMethod, this, out typeParameters);
            }

            AssignTypeMapAndTypeParameters(typeMap, typeParameters);
        }
Esempio n. 3
0
 private static string MakeName(string topLevelMethodName, MethodDebugId topLevelMethodId, ClosureKind closureKind, int lambdaOrdinal)
 {
     // Lambda method name must contain the declaring method ordinal to be unique unless the method is emitted into a closure class exclusive to the declaring method.
     // Lambdas that only close over "this" are emitted directly into the top-level method containing type.
     // Lambdas that don't close over anything (static) are emitted into a shared closure singleton.
     return(GeneratedNames.MakeLambdaMethodName(
                topLevelMethodName,
                (closureKind == ClosureKind.General) ? -1 : topLevelMethodId.Ordinal,
                topLevelMethodId.Generation,
                lambdaOrdinal));
 }
Esempio n. 4
0
        private static string MakeName(VariableSlotAllocator slotAllocatorOpt, TypeCompilationState compilationState, ClosureKind closureKind, MethodSymbol topLevelMethod, int topLevelMethodOrdinal, int lambdaOrdinal)
        {
            // TODO: slotAllocatorOpt?.GetPrevious()

            // Lambda method name must contain the declaring method ordinal to be unique unless the method is emitted into a closure class exclusive to the declaring method.
            // Lambdas that only close over "this" are emitted directly into the top-level method containing type.
            // Lambdas that don't close over anything (static) are emitted into a shared closure singleton.
            return(GeneratedNames.MakeLambdaMethodName(
                       topLevelMethod.Name,
                       (closureKind == ClosureKind.General) ? -1 : topLevelMethodOrdinal,
                       compilationState.ModuleBuilderOpt.CurrentGenerationOrdinal,
                       lambdaOrdinal));
        }