/// <summary>
        /// Match local declarations to names to generate a map from
        /// declaration to local slot. The names are indexed by slot and the
        /// assumption is that declarations are in the same order as slots.
        /// </summary>
        private static ImmutableArray <Microsoft.CodeAnalysis.Emit.EncLocalInfo> GetLocalSlots(
            MethodSymbol method,
            ImmutableArray <string> localNames,
            ImmutableArray <MetadataDecoder.LocalInfo> localInfo)
        {
            var syntaxRefs = method.DeclaringSyntaxReferences;

            // No syntax refs for synthesized methods.
            if (syntaxRefs.Length == 0)
            {
                return(ImmutableArray <Microsoft.CodeAnalysis.Emit.EncLocalInfo> .Empty);
            }

            var syntax  = syntaxRefs[0].GetSyntax();
            var map     = new Dictionary <Microsoft.CodeAnalysis.Emit.EncLocalInfo, int>();
            var visitor = new GetLocalsVisitor(localNames, localInfo, map);

            visitor.Visit(syntax);
            var locals = new Microsoft.CodeAnalysis.Emit.EncLocalInfo[localInfo.Length];

            foreach (var pair in map)
            {
                locals[pair.Value] = pair.Key;
            }

            // Populate any remaining locals that were not matched to source.
            for (int i = 0; i < locals.Length; i++)
            {
                if (locals[i].IsDefault)
                {
                    var info        = localInfo[i];
                    var constraints = GetConstraints(info);
                    locals[i] = new Microsoft.CodeAnalysis.Emit.EncLocalInfo((Cci.ITypeReference)info.Type, constraints);
                }
            }

            return(ImmutableArray.Create(locals));
        }
Exemple #2
0
        /// <summary>
        /// Match local declarations to names to generate a map from
        /// declaration to local slot. The names are indexed by slot and the
        /// assumption is that declarations are in the same order as slots.
        /// </summary>
        private static ImmutableArray<Microsoft.CodeAnalysis.Emit.EncLocalInfo> GetLocalSlots(
            MethodSymbol method,
            ImmutableArray<string> localNames,
            ImmutableArray<MetadataDecoder.LocalInfo> localInfo)
        {
            var syntaxRefs = method.DeclaringSyntaxReferences;

            // No syntax refs for synthesized methods.
            if (syntaxRefs.Length == 0)
            {
                return ImmutableArray<Microsoft.CodeAnalysis.Emit.EncLocalInfo>.Empty;
            }

            var syntax = syntaxRefs[0].GetSyntax();
            var map = new Dictionary<Microsoft.CodeAnalysis.Emit.EncLocalInfo, int>();
            var visitor = new GetLocalsVisitor(localNames, localInfo, map);
            visitor.Visit(syntax);
            var locals = new Microsoft.CodeAnalysis.Emit.EncLocalInfo[localInfo.Length];
            foreach (var pair in map)
            {
                locals[pair.Value] = pair.Key;
            }

            // Populate any remaining locals that were not matched to source.
            for (int i = 0; i < locals.Length; i++)
            {
                if (locals[i].IsDefault)
                {
                    var info = localInfo[i];
                    var constraints = GetConstraints(info);
                    locals[i] = new Microsoft.CodeAnalysis.Emit.EncLocalInfo((Cci.ITypeReference)info.Type, constraints);
                }
            }

            return ImmutableArray.Create(locals);
        }