Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lowered operations in roslyn

Tags:

c#

roslyn

When operations were introduced in Roslyn one of the goals was to provide lowered operations (I think it was in design review meeting video) which as far as I understand should provide explicit operations for implicit compiler actions on high level ones. I see Lowering directory in Roslyn, but classes are internal there. It is possible to get lowered operations now or no public API available yet?

In sample below operations already removing some implicit parts - adding return statement for expression body and expose symbol for overloaded operator. But pre and post increments differs only by kind.

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Semantics;
using System.Linq;

namespace so39468373
{
    internal static class Program
    {
        private static void Main()
        {
            var tree = CSharpSyntaxTree.ParseText(@"
public class c
{
    public static c operator ++(c o) { return o; }
    static c pre(c o) => ++o;
    static c post(c o) => o++;
    public static void Main() {}
}");
            var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
            var compilation = CSharpCompilation.Create(null, new[] { tree }, new[] { mscorlib });
            var model = compilation.GetSemanticModel(tree);
            foreach (var node in tree.GetRoot().DescendantNodes().OfType<ArrowExpressionClauseSyntax>())
            {
                var operation = model.GetOperation(node);
                var block = (IBlockStatement)operation;
                var statement = (IReturnStatement)block.Statements.First();
                var increment = (IIncrementExpression)statement.ReturnedValue;
                // How to get lowered operations for increment here?
            }
        }
    }
}

Code on github - https://github.com/isanych/so-39468373

like image 696
ISanych Avatar asked Sep 13 '16 10:09

ISanych


1 Answers

Different angle on this answer -- what about this aspect of the compiler?

InternalVisibleTo Attribute

link: https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx

Might make an interesting topic of conversation related to a "different angle" to use to approach debugging?

MORE INFO (from the article):

Version Information

Universal Windows Platform Available since 8 .NET Framework
Available since 2.0 Portable Class Library
Supported in: portable .NET platforms Silverlight
Available since 2.0 Windows Phone Silverlight
Available since 7.0 Windows Phone
Available since 8.1

like image 136
Glenn Ferrie Avatar answered Oct 19 '22 05:10

Glenn Ferrie